Skip to content

fix: show skill roots of every project folder, including after resume - #5573

Merged
RealKai42 merged 2 commits into
mainfrom
fix/multi-dir-skill-roots
Nov 27, 2026
Merged

RealKai42 merged 2 commits into
mainfrom
fix/multi-dir-skill-roots

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在多文件夹项目里,模型看到的技能清单末尾的 ### Skill roots 缺了附加文件夹。Skill 工具不可用时,模型要自己去读技能文件,就会找错位置:技能在 ~/.agents/skills,它却去 ~/.kimi-code 下找。排查下来有两个根因:

  • 会话关闭期间给项目加了文件夹,恢复后模型再也看不到这个文件夹。
  • 相对路径的 extra_skill_dirs 只按工作目录解析。

合入后,不论是新会话还是恢复的会话,模型都能看到每个项目文件夹的技能和它们的 skill roots。

Bug Reproduction Steps

会话恢复(桌面端最常见,平时大部分会话都没有加载):

  1. 建一个只有 app 文件夹的项目,开一个会话发一条消息,然后关掉会话或重启服务。
  2. 编辑项目,加入文件夹 lib,其中有 lib/.agents/skills/<name>/SKILL.md。
  3. 回到这个会话再发一条消息。模型请求的系统提示词和提醒里都没有 lib 的技能,也没有它的 skill root。

相对额外目录:

  1. 在 config.toml 里配置 extra_skill_dirs = ["team-skills"]。
  2. 项目有附加文件夹 lib,其中有 lib/team-skills/<name>/SKILL.md。
  3. 这个技能不会出现在清单里,lib/team-skills 也不在 ### Skill roots 里。只有主目录下的 team-skills 生效。

Root Cause

  1. 恢复会话:恢复时,系统提示词连同当时冻结的技能清单从 journal 回放。变更提醒只在目录、技能实时变化的事件里,或执行 /reload 时才发。会话关闭期间发生的变化,恢复时没有任何代码去比对,所以模型一直不知道。这是根本修复:恢复后补做 /reload 已有的那套比对。
  2. 相对 extra_skill_dirs:configuredRoots 只接收一个工作目录来解析相对路径,额外目录来源也不监听项目目录集合的变化。这也是根本修复:改为按项目的每个目录解析,并在目录变化时重新加载。

Code Changes

核心思路:恢复会话时不立刻做检查,只记一个「待检查」标记。到恢复后(以及撤销一轮后)的第一步开始前,再执行一次 /reload 同款比对,产生的提醒就进入这一步的请求。相对 extra_skill_dirs 和项目级技能用同一份目录列表:主目录、工作目录、共享附加目录,主目录优先。同一时刻变化的多个技能来源合并成一次重载,模型只会收到一份完整清单。

sequenceDiagram
    participant Lifecycle as agent 生命周期
    participant Notifier as 变更通知服务
    participant Reminder as 提醒服务(每步开始前)
    participant Model as 模型请求
    Lifecycle->>Notifier: onDidRestore(回放完成)
    Notifier->>Notifier: 已绑定 profile 的 main agent:记下待检查
    Note over Lifecycle,Notifier: 恢复本身不写任何记录,状态和恢复前一致
    Reminder->>Notifier: 第一步开始前调用 restored_changes provider
    Notifier->>Notifier: notifyAllChanges():技能清单、AGENTS.md、子 agent 列表、项目目录
    Notifier-->>Model: 有变化的项各带一条提醒
Loading

撤销一轮(ContextUndone)时,提醒和通知快照会一起回退,所以撤销后也重新置上标记,下一步再比对一次。

相对额外目录的解析:

 configuredRootCandidates(dirs, workDirs)
-  projectRoot = findProjectRoot(workDir)
-  return dirs.map(resolve against projectRoot)
+  for workDir of unique(workDirs)          # 主目录、工作目录、共享附加目录
+    projectRoot = findProjectRoot(workDir)
+    for dir of dirs
+      add resolve(dir, projectRoot) once   # ~ 和绝对路径只会出现一次

技能来源的变更事件:

 on source change
-  reload this source; remerge; fire(sourceId)
+  queue source; on next microtask:
+    reload each queued source once (in parallel)
+    remerge once
+    fire(sourceId) for every queued event

关键决策

决策 选择 没选 理由
恢复检查在哪里跑 恢复后第一步开始前(提醒 provider) 在恢复钩子里直接比对 直接比对会在什么都没变时也补写簿记快照,恢复后的状态就和原来不一样了。而且排进队列的提醒会被 fork 回放,导致对一个刚恢复、还没发消息的会话做 fork 时失败
清单里要不要加回每个技能的路径 不加,只保留 ### Skill roots 加回 Path: 维护者决定保持 #4713 的做法,避免模型绕过 Skill 工具直接读文件
附加文件夹在某个 git 仓库内部时 维持按仓库根查找,只补文档 同时扫描文件夹自身的技能目录 维护者决定
相对额外目录的解析范围 主目录、工作目录、每个共享附加目录 只按主目录 和项目级技能的多目录合并用同一份目录列表;--skills-dir 仍只按工作目录
多来源同时变化时的合并方式 技能目录层:同一 tick 内的变化一起重载 在变更通知服务里加定时去抖 定时去抖会引入固定等待,现有测试按事件同步;在目录层合并以后,第一个事件看到的就是最终结果,同一份清单的后续检查按哈希判断,不会重复提醒

阅读顺序

  1. packages/agent-core-v2/src/agent/changeNotifier/changeNotifierService.ts:恢复和撤销时怎么记待检查、第一步怎么触发比对。
  2. packages/agent-core-v2/src/features/skill/catalog/skillRoots.ts:相对路径按目录列表解析的规则。
  3. packages/agent-core-v2/src/features/skill/project/extraFileSkillSource.ts:额外目录来源怎么拿目录列表、什么时候重新加载。
  4. packages/agent-core-v2/src/features/skill/project/projectSkillCatalogService.ts:同一 tick 的来源变化怎么合并。

可以略读:rootFileSkillSource.ts 抽出的共用目录列表、explicitFileSkillSource.ts 和 configuredSkillRootWatcher.ts 的签名透传、programWorkdirs.ts 的接线、kap-server 预览接口的一行改动,以及测试和文档。

Behavior Changes and Affected Users

行为 之前 之后 谁依赖旧行为 退路
相对 extra_skill_dirs,项目有多个目录时 只按工作目录的项目根解析 按主目录、工作目录和每个共享附加目录的项目根各解析一次,主目录优先 配了相对额外目录、项目又有附加目录的 TUI、desktop 与 web、ACP、SDK 用户。附加文件夹下的同名目录之前被忽略,现在会多加载其中的技能 改用绝对路径或 ~ 路径,只解析一次
多个文件夹的额外目录里有同名技能 不适用(之前只扫一个目录) 主目录那份为默认,附加文件夹里的同名技能不作为变体保留,与「Extra」低于「Project」的优先级规则一致 无 无
单目录项目、绝对或 ~ 路径的额外目录、--skills-dir — 不变:解析结果和顺序都和以前一样 — —
项目目录集合变化时 额外目录来源不重新加载 配了 extra_skill_dirs 时重新加载;没配时不触发 无 无
同一时刻多个技能来源变化(加文件夹且配了额外目录、切换 merge_all_available_skills) 每个来源各自重载、各自合并、各自发事件;监听方会看到中间状态,模型可能收到多条不完整的清单 一起重载、合并一次后,按原次数发事件;监听方第一次收到事件时看到的就是最终结果。事件晚一个微任务送达 订阅技能目录 onDidChange 的内部消费者:session 目录、变更提醒、插件服务的事件计数。计数保持一一对应 无
恢复已绑定 profile 的 main agent 不和当前状态比对 恢复后第一步比对技能清单、AGENTS.md、子 agent 列表和项目目录,有变化就在这一步的请求里带上对应提醒 恢复旧会话的所有客户端:TUI、desktop 与 web、VS Code、ACP、SDK,以及通过 KAP 驱动会话的外部 agent 无开关;这些提醒和 /reload 发出的相同
旧版本写入的会话被恢复 不提醒 记录下来的清单和当前格式不一致时(例如 #4713 之前带 Path: 的清单),第一步收到一次完整的当前清单 同上 无
撤销一轮之后 不重新比对 下一步再比对一次,把随撤销一起回退的提醒补发回来 同上 无
恢复过程本身 — 不写新的 journal 记录,状态和恢复前一致;对刚恢复的空闲会话做 fork 不受影响 — —
新会话前的项目技能预览(GET /api/v1/workspaces/{workspace_id}/skills) 相对额外目录只按项目根目录解析 按根目录和附加目录解析,和会话里的结果一致 desktop 与 web 新会话的技能菜单 无
子 agent、尚未绑定 profile 的新 agent、fork 出来的 agent — 不变:不做恢复检查 — —
  • 没有改动任何提示词文本(packages/agent-core-v2/src/**/*.md、工具描述、提醒文案)。新增的只是已有的提醒会在新的时机发出。
  • 没有改动契约文件(config-manifest.toml、wire-manifest.d.ts、state-manifest.d.ts、kap.md、contract.txt、node-sdk 导出)。REST 响应的结构没变。
  • 平台:只在 macOS 上验证过。路径解析仍用原来的 findProjectRoot 和 resolveConfiguredDir,只是对每个目录各调一次;Windows 路径(盘符、UNC)未验证。

受影响模块与测试覆盖

  • agent-core-v2 的 skill 模块:
    • configuredRoots 的解析规则(相对路径逐个目录解析,绝对路径只解析一次,主目录优先),见 skillRoots.test.ts。
    • 多目录项目里加载附加文件夹的相对额外目录、roots 出现在模型清单里;中途加文件夹后,第一个变更事件就同时含有项目级技能和额外目录技能。见 skillCatalog.test.ts 新增的两个用例。
  • agent-core-v2 的变更通知:恢复后只在第一步提醒一次;未绑定 profile 时不提醒;撤销后再检查,且只认本 agent 的撤销。见 changeNotifier.test.ts 新增的三个用例。
  • kap-server:预览接口按每个目录解析,主目录同名优先,结果和会话一致。见 skills.test.ts 新增用例。
  • KAP 场景:会话关闭期间给项目加文件夹,恢复后的请求里恰好带一份含新技能和新 root 的清单,以及一条目录变更提醒。见 tests/scenarios/conversation.test.ts 新增用例。
验证过程
  • 修复前后对照:上面这些修复点对应的测试,都先在去掉对应修复的代码上跑过、确认失败,再在修复后通过。
  • pnpm lint 以 0 退出;pnpm run check:duplication 相对目标分支通过;agent-core-v2、kap-server、tests/scenarios 的 tsc --noEmit 都通过。
  • 完整运行了受影响的测试集:agent-core-v2 的 test/agent、session、integration、features、program、project,共 4,793 个;tests/scenarios、kap-server、node-sdk、acp-server,共 2,146 个。
  • 运行期间,本机同时有其他任务在跑测试,负载平均值最高到 120(10 核)。每轮都有少量用例因超时或 KAP 子进程 30 秒内没起来而失败,而且每轮失败的用例都不一样。负载降下来后单独重跑这些文件,全部通过(最后一轮 5 个文件、307 个用例)。
  • rebase 到最新 main 后,重跑了 lint 和直接相关的 17 个测试文件(315 个用例),全部通过。
  • 第一版是在恢复钩子里直接比对。它让 fullCompaction.test.ts 和 dateChangeInjection.test.ts 的「恢复后状态一致」断言失败,并让 binding.test.ts 里对恢复后会话的 fork 失败。改成第一步再检查后,这三个文件都通过。
已知、但不在本 PR 范围内的问题
  • Skill 工具的 path 参数说明写着「从清单里复制 Path」,但清单已经不列路径,所以多文件夹里的同名技能,模型只能调到默认那份。
  • ACP 里同名技能会出现两条 skill:<name> 命令,都只运行默认那份。
  • 以下几条只是读代码时发现的,未核实:远程(ssh、docker)会话里,本机的用户级和额外技能路径在远端读不到;恢复的子 agent 收不到变更提醒;会话中途开关 Skill 工具后,提示词里的说明不会跟着变;软链接的单文件技能在两套发现逻辑里结果不一致。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Relative extra_skill_dirs entries were resolved only against the working
directory, so in a multi-directory project the extra skill dirs under the
additional directories were never scanned and their skill roots were missing
from the model listing. Resolve them against the primary directory, the
working directory and every shared additional directory, primary first, and
reload them when the directory set changes. Explicit --skills-dir entries
keep resolving against the working directory.

Reload skill sources that change in the same tick together and remerge once,
so a directory change no longer produces one partial skill listing per
source. The workspace skill preview route follows the same resolution.
A resumed main agent kept the skill listing, AGENTS.md files, subagent list
and project directories frozen in its replayed system prompt, and nothing
compared them with the current state until a later live change or /reload.
A folder added to a multi-directory project while the session was closed
therefore never reached the model.

Arm a one-time check when the agent is restored and after an undo, and run
the same change checks /reload runs at the next step, so the reminders land
in that step's request. Restoring itself records nothing, which keeps resumed
state identical and leaves forks of an idle resumed session unaffected.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0b52188

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@moonshot-ai/kimi-code Patch
kimi-code-app Patch
@moonshot-ai/agent-core-v2 Patch
@moonshot-ai/kap-server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T16:35:53.781144Z 0b52188 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@0b52188
npx https://pkg.pr.new/@moonshot-ai/kimi-code@0b52188

commit: 0b52188

@RealKai42
RealKai42 merged commit abde108 into main Nov 27, 2026
22 checks passed
@RealKai42
RealKai42 deleted the fix/multi-dir-skill-roots branch November 27, 2026 14:53
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Dec 2, 2026
`bun scripts/upstream-ledger.mjs --refresh` at upstream/main 4eb2919
added 126 rows, so every open upstream commit and every PR it references
now has a ledger row: 0/596, down from 125 at 8bf8ba3 and 126 at the
fetched tip. The refresh only reads `upstream/main ^ main`, so the three
release-2.6.2 commits named by the mission (MoonshotAI#4875/MoonshotAI#4905/MoonshotAI#4922) gained
rows with the refresh's row shape.

Settled with live probes in this tree:

- ported: MoonshotAI#5533 (docs 2.8.1 changelog), MoonshotAI#4875 (registry search_url and
  feedback_url plus the feedback service), MoonshotAI#4905 (Kimi upload status
  validation in packages/core), MoonshotAI#4922 (swarm task events in
  sessionTaskFeed), MoonshotAI#5438 (unload only declared dynamic tools), MoonshotAI#5592
  (dynamic tool replacement order), MoonshotAI#5595 (append-log known length),
  MoonshotAI#5573 (skill roots for every work dir), MoonshotAI#5576 (date reminder cwd
  comparison), MoonshotAI#5618 (session titles for skill openers), MoonshotAI#5638 (frozen
  loop tool baseline), MoonshotAI#5626 (legacy turn-input origins)
- partial: MoonshotAI#5505 (processLiveness source and consumers present; the
  upstream unit test file was never carried), MoonshotAI#5571 (program-resume and
  OSC 7577 present; the upstream test helper was never carried), MoonshotAI#5532
  (gitService half present; fsService still leaks the environment lease
  at fsService.ts:1133 and :1267), MoonshotAI#5557 (app-client icon registry kept;
  the move to app-ui was never carried)
- not-applicable: MoonshotAI#4655 (upstream reverted auto review in MoonshotAI#5649), MoonshotAI#5333
  (PLAYBOOK:111 deliberate flat experimental flags)

M39's "likely present" list was checked, not trusted: MoonshotAI#5438 is present
(upstream test passes), MoonshotAI#5487 is not (nativeStartSettleMs/leg.nativeSettled
absent), MoonshotAI#5635 is not (ToolsSettled absent), MoonshotAI#5429 needs a visual probe.
The remaining 194 pending rows stay open with their reasons; no feature
was ported and no upstream-drift allowlist entry was added.

Verification: verify -> 332 closed rows, 332 carry every file of their
PR; 317 probes pass, 2 foreign-runner skips, 0 fail; a second verify is
byte-identical (ledger sha256 cbf53638...). Probe tests 29 pass; bun run
lint and node scripts/checks/ratchet.mjs green.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants