Skip to content

[Bug] Subagents cannot discover package-level AGENTS.md due to shared Kaos cwd #173

@thecannabisapp

Description

@thecannabisapp

问题发现 / Discovery

在将 kimi-cli 的 scoped subagent YAML 工作流迁移到 kimi-code 的 hierarchical AGENTS.md 模型时,我们发现子代理无法发现包级别的 AGENTS.md 文件。

When migrating kimi-cli scoped subagent YAML workflows to kimi-code's hierarchical AGENTS.md model, we discovered subagents cannot discover package-level AGENTS.md files.

根本原因 / Root Cause

prepareSystemPromptContext(kaos) 通过 kaos.getcwd() 向上遍历目录树来加载 AGENTS.md。会话中的所有代理共享同一个 Kaos 实例(session.config.runtime.kaos)。SessionSubagentHost.spawn() 创建的子代理继承此共享实例,而 configureChild 无条件将子代理的 cwd 设为父代理的 cwd。因此,子代理的 AGENTS.md 发现范围永远被锁定在父代理的工作目录,无法访问包级规则。

prepareSystemPromptContext(kaos) loads AGENTS.md by walking up the directory tree from kaos.getcwd(). All agents in a session share the same Kaos instance (session.config.runtime.kaos). SessionSubagentHost.spawn() creates subagents that inherit this shared instance, and configureChild unconditionally sets the child's cwd to the parent's cwd. Consequently, a subagent's AGENTS.md discovery is permanently locked to the parent agent's working directory — package-level rules are invisible.

影响 / Impact

  • 在 monorepo 中,无法为 packages/domain、packages/infra 等包作用域子代理加载特定的 AGENTS.md 规则。

  • 破坏了 kimi-cli 中通过 scoped subagent YAML 实现的 monorepo 工作流。

  • In a monorepo, package-scoped subagents (e.g. packages/domain, packages/infra) cannot load package-specific AGENTS.md rules.

  • Breaks monorepo workflows that worked in kimi-cli via scoped subagent YAMLs.

最小复现 / Minimal Reproduction

  1. 在 monorepo 的根目录和 packages/domain/AGENTS.md 分别创建不同的 AGENTS.md。

  2. 从根目录启动会话。

  3. 使用 Agent 工具生成一个 coder 子代理,任务涉及 packages/domain。

  4. 子代理的系统提示中只会包含根目录的 AGENTS.md,packages/domain/AGENTS.md 不会出现。

  5. Create distinct AGENTS.md files at repo root and at packages/domain/AGENTS.md.

  6. Start a session from the repo root.

  7. Use the Agent tool to spawn a coder subagent with a task involving packages/domain.

  8. The subagent's system prompt only contains the root AGENTS.md; packages/domain/AGENTS.md is absent.

提议修复 / Proposed Fix

已在 PR #172 中实现,核心思路:

Implemented in PR #172. Core approach:

  1. 为 AgentToolInputSchema 添加可选的 cwd 参数。
    Add an optional cwd parameter to AgentToolInputSchema.
  2. 当 cwd 提供时,SessionSubagentHost.spawn() 通过 parent.runtime.kaos.withCwd(cwd) 创建一个独立的 Kaos 实例作为子代理的运行时。
    When cwd is provided, SessionSubagentHost.spawn() creates an isolated Kaos via parent.runtime.kaos.withCwd(cwd) and passes it as the subagent's runtime.
  3. Session.instantiateAgent 允许 config.runtime 覆盖默认的会话级运行时。
    Session.instantiateAgent allows config.runtime to override the default session-level runtime.
  4. configureChild 在提供 cwd 时使用该值,否则回退到父代理的 cwd。
    configureChild uses the provided cwd if given, otherwise falls back to the parent's cwd.

此方案默认行为完全不变;仅当调用方显式传入 cwd 时才启用隔离工作目录。全部 2,132 项现有测试通过。

Default behaviour is completely unchanged; isolated working directories are only enabled when the caller explicitly passes cwd. All 2,132 existing tests pass.

问题 / Question for the Team

这是预期的设计权衡,还是应当视为 bug 修复?在 kimi-cli 中,scoped subagent YAML 显式定义了工作目录和工具集;kimi-code 的 hierarchical AGENTS.md 模型本应提供更优雅的发现机制,但子代理层面的缺失使其在 monorepo 场景下退化了。

Is this an intended design trade-off, or should it be treated as a bug? In kimi-cli, scoped subagent YAMLs explicitly defined working directories and toolsets; kimi-code's hierarchical AGENTS.md model was meant to provide a more elegant discovery mechanism, but the absence at the subagent level causes a regression in monorepo scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions