Skip to content

fix(agent-core-v2): inherit baseline tools when forking agents - #4832

Merged
sailist merged 1 commit into
mainfrom
fix-btw-fork-baseline-tools
Nov 11, 2026
Merged

sailist merged 1 commit into
mainfrom
fix-btw-fork-baseline-tools

Conversation

@sailist

@sailist sailist commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Side questions started with /btw can lose nearly all prompt-cache reuse when the main agent's available tools change after its Loop has started. The parent continues to request with its original baseline tools, while the fork recomputes a different baseline from the current profile. In the investigated session, cache hit rate dropped from 99.18% to 13.03%.

This PR makes same-profile forks inherit the parent's exact baseline tools so their requests preserve the prompt-cache prefix.

Bug Reproduction Steps

  1. Start the main Loop and issue a request, freezing its baseline tool descriptions.
  2. Change activeToolNames through a runtime profile or tool-state update.
  3. Start /btw and send a side question.
  4. Compare the parent and child llm.tools_snapshot and usage.record: before the fix, the parent could send 40 tools while the child sent 30. The investigated response had 111,980 input tokens, with only 14,592 read from cache and 97,388 uncached.

The existing forkParity cache-parity test now covers this sequence instead of only configuring tools before the first request.

Root Cause

Loop initialization stores a ToolDescription[] baseline separately from ProfileBindingSnapshot.activeToolNames. Later profile and runtime tool updates do not refresh that baseline. fork copied only the parent's current profile data, so the child Loop computed a fresh baseline that could differ from the parent's actual request tools.

This is a fundamental fix: the parent's baseline is passed through the agent-creation path into the child Loop at startup. The Wire format is unchanged, and no workaround or second tool-definition source is introduced.

Code Changes

  • Add IAgentLoopService.baselineTools() and allow start(tools) to accept an initial baseline.
  • Add CreateAgentOptions.initialTools and pass the parent's baseline to same-profile forks.
  • Keep explicit profile overrides on the existing recompute path; normal create/resume behavior is unchanged.
  • Preserve the inherited baseline through history initialization and reset.
  • Update the existing btw cache-parity test without increasing the test count, adjust test doubles, and include a patch changeset.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Same-profile fork, including /btw Child recomputed baseline tools from the current profile, which could diverge from the parent's actual request Child inherits the parent's exact baseline tools No supported workflow relies on a cache-breaking parent/child schema mismatch Not needed; this restores the intended cache-parity behavior
Fork with an explicit new profile Baseline computed for the selected profile Unchanged Callers selecting a different profile Existing behavior retained
Normal create/resume and Wire persistence Baseline computed at startup; existing record format Unchanged Existing sessions and resumed agents Existing behavior retained

Coverage: forkParity verifies system prompt, outbound tools, and inherited history after a post-start tool-state change; agentLifecycle covers fork lifecycle behavior; toolSelectService covers tool selection behavior and updated test doubles.

Checklist

  • I have read the CONTRIBUTING document.
  • This PR includes a related problem description and direct reproduction steps; there is no linked issue.
  • I have updated tests that prove the fix works, without increasing the test count.
  • The behavior-change table above covers affected paths; no supported behavior or default is removed.
  • Added .changeset/fork-baseline-tools.md as a patch changeset. The gen-changesets skill was unavailable in the current skill listing, so the file follows the repository's existing format directly.
  • No documentation update is required for this internal cache-consistency fix.

@changeset-bot

changeset-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5cbd117

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

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code 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 11, 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-11T09:40:15.723391Z 5cbd117 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 11, 2026

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

commit: 5cbd117

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5cbd1174d4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

thinking: override.thinking ?? sourceData.thinkingLevel,
},
initialProfile: override?.profile === undefined ? sourceData : undefined,
initialTools: override?.profile === undefined ? sourceLoop.baselineTools() : undefined,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reconcile inherited schemas with the child tool registry

When an MCP or other runtime tool is removed or changes its schema after the parent's last request but before the fork, sourceLoop.baselineTools() still contains the old definition while the newly created child registry reflects the current tool set. Passing that stale list as initialTools is not corrected by AgentToolSelectService.requestTools, which treats a supplied base as authoritative, so the child advertises a removed tool or obsolete arguments and subsequent calls fail or reach the updated implementation with the wrong schema. Previously the child derived its baseline from its fresh registry; preserve the cached list only when its tool signatures still match, or carry the corresponding frozen registrations into the child.

Useful? React with 👍 / 👎.

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