Skip to content

fix(agent-core): correct MCP tool-name hash and post-compaction injection marker#732

Open
Dmatut7 wants to merge 3 commits into
MoonshotAI:mainfrom
Dmatut7:fix/agent-core-mcp-hash-injection
Open

fix(agent-core): correct MCP tool-name hash and post-compaction injection marker#732
Dmatut7 wants to merge 3 commits into
MoonshotAI:mainfrom
Dmatut7:fix/agent-core-mcp-hash-injection

Conversation

@Dmatut7

@Dmatut7 Dmatut7 commented Jun 13, 2026

Copy link
Copy Markdown

Related Issue

No existing issue — two agent-core correctness bugs explained below.

Problem

(1) stableHash8 hex-encoded a Math.imul result (signed 32-bit); for ~half of inputs the hash is negative, so toString(16) produced a 9-char -xxxxxxxx suffix (with a -, not a hex digit) instead of the documented 8-char hash used when a qualified MCP tool name exceeds 64 chars. (2) DynamicInjector.onContextCompacted remapped an injection's index with a >= 0 guard, but compaction puts a single summary at index 0 so a surviving injection maps to >= 1; when the injection was the last message folded into the summary (injectedAt === compactedCount - 1, which yields 0) it was pinned to the summary instead of cleared — so plugin session-start blocks never re-injected and plan-mode reminders dropped to sparse/none right after a full one was compacted away.

What changed

(1) Coerce the hash to unsigned 32-bit before encoding → always 8 lowercase-hex chars. (2) Require newInjectedAt >= 1, so a compacted-away injection becomes null and re-injects. Tests added for both.

Checklist

  • I have read the CONTRIBUTING document.
  • I have explained the problem above.
  • I have added tests (fail-before/pass-after).
  • Changeset included (patch, @moonshot-ai/kimi-code + @moonshot-ai/kimi-code-sdk).
  • This PR needs no doc update.

Dmatut7 and others added 3 commits June 14, 2026 03:46
`stableHash8` hex-encoded a value produced by `Math.imul`, which returns a
signed 32-bit int. For the ~50% of inputs that hash negative,
`toString(16)` emitted a leading `-`, yielding a 9-char `-xxxxxxxx` suffix
(and `-` is not a hex digit) instead of the documented deterministic
8-char hash used when a qualified MCP tool name exceeds 64 chars.

Coerce to unsigned 32-bit before encoding so the suffix is always 8
lowercase-hex characters.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…summary

`onContextCompacted` remaps an injection's stored index after compaction
replaces the first `compactedCount` messages with a single summary at
index 0. A surviving injection (old index >= compactedCount) maps to new
index >= 1, but the guard kept any `newInjectedAt >= 0`. When the
injection was the last message in the compacted prefix
(`injectedAt === compactedCount - 1`), `newInjectedAt` is 0 — so it was
wrongly pinned to the summary message instead of being cleared.

That stale marker made PluginSessionStartInjector never re-inject and
PlanModeInjector emit a weaker (sparse/none) reminder right after the full
one was compacted away. Require `newInjectedAt >= 1` so a compacted-away
injection becomes null and re-injects as intended.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Jun 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 89bc94e

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

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/kimi-code Patch
@moonshot-ai/kimi-code-sdk 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

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