Skip to content

fix(agent-core-v2): align compaction injections and goal tools with v1 - #1602

Merged
kermanx merged 2 commits into
mainfrom
fix/agent-core-v2-v1-parity
Jul 13, 2026
Merged

fix(agent-core-v2): align compaction injections and goal tools with v1#1602
kermanx merged 2 commits into
mainfrom
fix/agent-core-v2-v1-parity

Conversation

@kermanx

@kermanx kermanx commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below (found during a v1/v2 parity audit of the full-compaction path).

Problem

agent-core-v2's full-compaction path diverged from v1 (agent-core) in several model-visible ways:

  1. Auto permission mode reminder lost after compaction. The v2 injector deduped purely on an in-memory lastMode; once compaction spliced the reminder out of history it was never re-announced (v1 re-arms via onContextCompacted).
  2. Goal reminders rendered extra blank lines. v2 renders them from nunjucks templates without trimBlocks, so block-tag lines around optional sections (completion criterion, budgets, guidance band) leaked blank lines; v1 builds the same text inline.
  3. Background-task reminder wording drifted from v1 (hyphen vs em dash, a task result vs a task’s result).
  4. Goal tools were registered for every agent, so subagents could enter goal mode and receive goal reminders; v1 gates them with agent.type === 'main'.

What changed

  • PermissionModeInjection now derives dedup from the reminder's live positions in history (the same "history is the ledger" pattern plan_mode and loadable-tools use): the reminder is re-announced after compaction/undo, and a fresh instance after session restore announces the current mode exactly as v1 does — without inspecting surviving reminder content, so a stale exit reminder can never suppress the auto-mode announcement.
  • Rewrote the three goal reminder templates with inline block tags; verified byte-identical rendering against v1's builders across 14 input combinations (criterion × budgets × nearing × reason).
  • Aligned the background-task guidance text with v1 verbatim.
  • Added a main-agent-only when guard to the four goal tool contributions.
  • Tests: compaction re-injection and fresh-instance restore behavior (permission mode), compact-text assertions (goal injection), exact guidance text (task service), main-only gating (goal tools). Full agent-core-v2 suite passes; typecheck clean.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update — v2-internal parity fixes; documented CLI (v1) behavior is unchanged.

@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fb62b36

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

This PR includes changesets to release 3 packages
Name Type
@moonshot-ai/agent-core-v2 Patch
@moonshot-ai/kap-server Patch
@moonshot-ai/klient 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 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: c036994fed

ℹ️ 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".

Comment on lines +48 to +50
if (previousMode === undefined && injectedPositions.length > 0) {
this.lastMode = currentMode;
return 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 Check restored reminder text before adopting it

This adopts any surviving permission_mode injection solely from its position, but the injector tracks only the variant, not whether the live message is the auto-enter or auto-exit reminder. If a session has an exit reminder in history, the user switches back to auto, and the process restores before the next injection, this branch seeds lastMode as auto and suppresses AUTO_MODE_ENTER_REMINDER; the model is left with stale “auto no longer active” context while auto approvals are active. Only adopt restored history when the surviving reminder actually matches the current mode, or re-announce auto in this case.

Useful? React with 👍 / 👎.

}

registerTool(CreateGoalTool);
// Goal tools are main-agent-only (v1 parity: `agent.type === 'main'`).

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 Move guard notes into the file headers

packages/agent-core-v2/AGENTS.md says comments in this package must live solely in the top-of-file /** */ block and never beside statements, but this added // Goal tools are main-agent-only... comment sits next to the registerTool statement (and the same pattern was added to the other goal tool modules). Please move this rationale into the header comment or let the guarded registration stand without the inline comment.

Useful? React with 👍 / 👎.

- Re-announce the auto permission mode reminder after compaction by deriving
  dedup from the reminder's live history positions (also self-heals undo and
  session restore).
- Render goal reminders byte-identical to v1 by inlining nunjucks block tags
  that previously left blank lines around optional sections.
- Match v1's background-task reminder guidance text exactly.
- Gate the goal tools to the main agent via a `when` contribution guard, as
  v1 does with agent.type === 'main'.
@kermanx
kermanx force-pushed the fix/agent-core-v2-v1-parity branch from c036994 to b85fada Compare July 13, 2026 10:14
@pkg-pr-new

pkg-pr-new Bot commented Jul 13, 2026

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

commit: fb62b36

…, header-only comments

- Drop the silent-adoption of surviving reminders on fresh instances: a stale
  exit reminder in restored history could suppress the auto-mode announcement.
  A fresh instance now announces the current mode exactly as v1 does.
- Move the main-agent-only rationale for goal tool registration into the
  file-header comment blocks per the package comment convention.
@kermanx

kermanx commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: fb62b365e6

ℹ️ 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".

@kermanx
kermanx merged commit 09c1c32 into main Jul 13, 2026
11 checks passed
@kermanx
kermanx deleted the fix/agent-core-v2-v1-parity branch July 13, 2026 10:30
@github-actions github-actions Bot mentioned this pull request Jul 13, 2026
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.

1 participant