Kimi Code 的 `/init` 指令内核:默认初始化提示词与 AGENTS.md 生成机制解析
发布时间:2026/9/28 17:27:40来源:尧图网络
AI Agent代码智能体人工智能大模型CLI【免费下载链接】kimi-codeKimi Code CLI — The Starting Point for Next-Gen Agents项目地址https://gitcode.com/gh_mirrors/ki/kimi-code点击查看免费下载Kimi Code仓库路径gh_mirrors/ki/kimi-code内置了/init斜杠指令用于让 Agent 分析当前代码库并自动生成AGENTS.md。本文以该功能的核心提示词模板 init.md 为主线结合其上层服务、上下文加载逻辑与测试用例完整还原这一能力的设计意图、触发链路与落地细节。读完本文你将理解/init从派生子 Agent到回填 AGENTS.md的完整机制并掌握在实际项目中使用与验证该功能的方法。一、init.md 是什么一份写给 Agent 的任务说明书在 Kimi Code 的源码中init.md 并不是给人阅读的开发文档而是一份系统提示词模板prompt template——它是/init指令运行时注入给 Agent 的核心指令文本通过构建工具以?raw方式被原样加载为字符串。该提示词全文如下共 18 行实际运行时逐字注入You are a software engineering expert with many years of programming experience. Please explore the current project directory to understand the projects architecture and main details. Task requirements: 1. Analyze the project structure and identify key configuration files (such as pyproject.toml, package.json, Cargo.toml, etc.). 2. Understand the projects technology stack, build process and runtime architecture. 3. Identify how the code is organized and main module divisions. 4. Discover project-specific development conventions, testing strategies, and deployment processes. After the exploration, do a thorough summary of your findings and write it to the AGENTS.md file in the project root, replacing the files previous content. If the file already exists, read it first and carry forward whatever is still accurate — the result should be one coherent, up-to-date file, not an append. For your information, AGENTS.md is a file intended to be read by AI coding agents. Expect the reader of this file to know nothing about the project. You should compose this file according to the actual project content. Do not make any assumptions or generalizations. Ensure the information is accurate and useful. You must use the natural language that is mainly used in the projects comments and documentation. Popular sections that people usually write in AGENTS.md are: - Project overview - Build and test commands - Code style guidelines - Testing instructions - Security considerations从这份提示词可以看出它的设计要点角色设定Agent 被赋予多年经验的软件工程专家身份任务是先探索再总结而不是凭空生成四个探索维度项目结构、关键技术配置文件pyproject.toml、package.json、Cargo.toml等、技术栈与构建/运行时架构、代码组织与模块划分、项目特有的开发约定/测试策略/部署流程写入规则结果必须写入项目根目录的AGENTS.md若文件已存在必须先读取并保留仍然准确的内容整体重写为一份连贯的最新文件而不是追加读者定位明确AGENTS.md的读者是对项目一无所知的 AI 编码 Agent因此要求内容准确、有用、基于实际项目不得臆测或泛化且必须使用项目注释与文档中使用的主要自然语言即项目语言优先推荐章节给出了常见的AGENTS.md章节骨架项目概览、构建与测试命令、代码风格、测试说明、安全考量。二、提示词在源码中的挂载点profile/init.tspackages/agent-core-v2/src/features/sessionInit/profile/init.ts 是整个 sessionInit 功能的提示词仓库import initMd from ./init.md?raw; export const DEFAULT_INIT_PROMPT initMd; export function initCompletionReminder(agentsMd: string): string { const latest agentsMd.trim().length 0 ? No AGENTS.md content was found after /init completed. : agentsMd; return [ The user just ran /init slash command., The system has analyzed the codebase and generated an AGENTS.md file., , Latest AGENTS.md file content:, latest, ].join(\n); }这里有两个关键导出DEFAULT_INIT_PROMPT即 init.md 的原样内容作为/init运行时派生子 Agent 的提示词initCompletionReminder(agentsMd)在/init完成后构造一条完成提醒通知主 Agent用户刚刚执行了/init、系统已生成AGENTS.md并把最新文件内容附上若生成的AGENTS.md为空则提醒内容会替换为No AGENTS.md content was found after \/init completed.避免空内容误导后续对话。该文件通过 packages/agent-core-v2/src/index.ts 的export * from #/features/sessionInit/profile/init;对外导出是整个功能对外的公共 API 之一。三、/init的完整触发链路SessionInitService 做了什么在用户侧/init是一个会话级斜杠指令官方文档 docs/zh/reference/slash-commands.md 的说明为分析当前代码库并生成AGENTS.md。指令背后的核心实现位于 packages/agent-core-v2/src/features/sessionInit/sessionInitService.ts其generateAgentsMd()方法第 44 行起完整串联了以下步骤const INIT_PROFILE_NAME coder; const INIT_PARENT_TOOL_CALL_ID generate-agents-md; const INIT_DESCRIPTION Initialize AGENTS.md; const INIT_LABELS: ReadonlyRecordstring, string { sessionInit: agents-md };获取主 Agent通过agentLifecycle.handleOf(MAIN_AGENT_ID)拿到主 Agent若不存在则抛出AGENT_NOT_FOUND校验模型绑定读取主 Agent 的 profile 数据若modelAlias undefined抛出SESSION_INIT_FAILEDMain agent has no model bound即主 Agent 未绑定模型时/init无法运行创建子 Agent以coderprofile 为模板创建子 Agent绑定主 Agent 的modelAlias与thinkingLevel并打上{ sessionInit: agents-md }标签同时把主 Agent 当前的权限模式permission mode同步给子 Agent派发 spawn 事件通过emitAgentRunSpawned广播subagent.spawned事件parentToolCallId为generate-agents-md运行子 Agent调用subagents.run请求类型为{ kind: prompt, prompt: DEFAULT_INIT_PROMPT }——init.md 正是在这里作为提示词被注入同时通过mirrorAgentRun把子 Agent 的运行镜像到主 Agent 的会话中让用户可以实时看到探索过程取消支持整个流程受AbortController控制cancelInit()第 40-42 行可随时中止进行中的初始化且用户取消会以UserCancellationError原样向上传播、不会被包装成失败测试第 234-253 行专门验证了这一点重新加载 AGENTS.md子 Agent 运行结束后调用loadAgentsMdDetailed从当前工作目录重新读取AGENTS.md回填与提醒通过IAgentAgentsMdReminderService.seedInjected(agentsMdPaths, cwd)把生成的AGENTS.md路径注入主 Agent 的提醒状态再通过IAgentReminderService.notify(initCompletionReminder(agentsMd), { variant: init })发送完成提醒冲刷事件await main.accessor.get(IEventDispatcher).flush()确保所有事件在返回前落库/广播。错误处理第 102-113 行值得一提用户取消isUserCancellation/isAbortError与已知的SESSION_INIT_FAILED直接透传其余任何子 Agent 失败都会被包装为ErrorCodes.SESSION_INIT_FAILED并保留原始错误消息作为 cause。服务注册与接口契约packages/agent-core-v2/src/features/sessionInit/sessionInitFeature.ts 将SessionInitService注册为LifecycleScope.Session作用域的会话级服务Feature.name sessionInitpackages/agent-core-v2/src/features/sessionInit/sessionInit.ts 定义了ISessionInitService接口仅暴露两个方法generateAgentsMd(): Promisevoid与cancelInit(): void——这恰好对应/init的执行与中断两种交互。四、AGENTS.md 的读取规则它会被放在哪里子 Agent 完成任务后系统通过 packages/agent-core-v2/src/agent/profile/context.ts 中的loadAgentsMdDetailed第 65-71 行重新定位AGENTS.md。该文件定义了几个重要的查找规则候选文件名第 79-87 行AGENTS_MD_PLAIN_NAMES [AGENTS.md, agents.md]此外每个目录下还会优先检查.kimi-code/AGENTS.mddotKimiAgentsMdPath返回join(dir, .kimi-code, AGENTS.md)最终候选路径依次为.kimi-code/AGENTS.md、AGENTS.md、agents.md目录搜索方向从工作目录沿目录树自叶向根dirsRootToLeaf逐层查找并结合 Git 工作树findGitWorkTree定位项目根大小建议第 8 行AGENTS_MD_RECOMMENDED_MAX_BYTES 32 * 1024即推荐AGENTS.md不超过 32KB文档 docs/zh/reference/server-api.md 也确认会话级告警agents-md-oversized正由AGENTS.md 过大检查产生系统提示注入AGENTS.md内容还会通过!-- From: path --注释标记被提取进系统提示第 89-100 行使主 Agent 在后续对话中始终感知到项目约定。在数据层面AGENTS.md还遵循两层约定见 docs/en/configuration/data-locations.md项目根目录的AGENTS.md是/init的默认写入目标$KIMI_CODE_HOME/AGENTS.md默认~/.kimi-code/AGENTS.md则存放全局性的 Kimi 专属 Agent 指令。同时 docs/en/configuration/config-files.md 指出文件系统 watcher 会监听AGENTS.md的变更并热重载默认开启可通过watch.enabled false关闭。五、测试如何验证这条链路packages/agent-core-v2/test/features/sessionInit/sessionInit.test.ts 用一组单元测试锁定了上述全部行为可以作为阅读源码时的对照实验主流程测试第 158-206 行模拟coderprofile 子 Agent 的创建与运行断言create的 binding 为{ profile: coder, model: mock-model, thinking: off }labels 为{ sessionInit: agents-md }注入的 prompt 内容包含Task requirements:即 init.md 的正文片段完成提醒内容包含The user just ran \/init slash command.与Latest AGENTS.md file content:并携带最新AGENTS.md 内容seedInjected被以([AGENTS_MD_PATH], WORK_DIR)调用事件序列中依次出现subagent.spawned→agent.status.updated→subagent.completed失败包装第 208-220 行子 Agent 抛错时外层错误 code 为SESSION_INIT_FAILED且保留原始消息coder exploded主 Agent 缺失第 222-232 行抛出AGENT_NOT_FOUND取消语义第 234-253 行cancelInit()中止在途运行错误类型为UserCancellationError且不会产生subagent.failed事件空转保护第 255-258 行无在途初始化时调用cancelInit()是无害的 no-op。六、实际使用与二次开发建议在项目中使用/init进入项目目录启动 Kimi Code 会话输入/init即可。系统会自动派生子 Agent 探索代码库并把结果写入项目根目录的AGENTS.md若存在.kimi-code/AGENTS.md则写入该处。生成后可立即在对话中看到已完成初始化的提醒及文件内容摘要文件后续变更会被 watcher 热加载无需重启会话。理解其边界/init需要主 Agent 已绑定模型否则报Main agent has no model bound生成的AGENTS.md应控制在 32KB 以内避免触发agents-md-oversized告警文件语言遵循项目注释与文档的主要语言原则中文项目会得到中文的AGENTS.md。定制提示词DEFAULT_INIT_PROMPT是硬编码的默认值若需在 fork 中调整探索维度例如增加API 面盘点或性能敏感路径章节可在 init.md 基础上扩展提示词文本再通过?raw导入后替换DEFAULT_INIT_PROMPT的取值上层generateAgentsMd()对提示词内容无格式依赖只需保持探索 → 写入 → 替换式重写的指令语义即可。小结/init之所以能稳定地产出高质量AGENTS.md关键在于 init.md 这份提示词对探索目标、写入规则、读者定位、语言要求、章节骨架做了完整而克制的约束再由 sessionInitService.ts 以独立coder子 Agent 镜像运行 结果回填提醒的架构执行。理解这层机制既能帮你更准确地预期/init的行为也能为定制或扩展项目初始化能力提供清晰的切入点——相关实现、测试与文档均已在本仓库可查证。赞分享AI Agent代码智能体人工智能大模型CLI【免费下载链接】kimi-codeKimi Code CLI — The Starting Point for Next-Gen Agents项目地址https://gitcode.com/gh_mirrors/ki/kimi-code点击查看免费下载相关推荐Kimi Code CLI 的 /init 命令深度解析从 init.md 提示词到 AGENTS.md 自动生成机制Kimi Code CLI 的 /init 命令深度解析从 init.md 提示词到 AGENTS.md 自动生成机制 本文以仓库内 init.md http人工智能AI Agent代码智能体交互助手CLI工具调用Open Interpreter 的 /init 命令解析一条提示词如何自动生成 AGENTS.md 项目指南Open Interpreter 的 /init 命令解析一条提示词如何自动生成 AGENTS.md 项目指南 本文解析 Open Interpreterc人工智能大模型AI Agent代码智能体AI 应用CLINotepad-- 跨平台文本编辑器Windows、Linux、macOS 三端完整上手指南Notepad 跨平台文本编辑器Windows、Linux、macOS 三端完整上手指南 Notepad 是一款国产跨平台文本编辑器基于 Scintilla桌面应用上一篇解决WaveTerm终端连接管理的5大痛点从SSH到WSL全攻略下一篇VUX 贡献指南基于 metas.yml 变更记录与 next 版本号机制的 PR 文档维护实践创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网