Skip to content

fix(anthropic): preserve signed thinking blocks on Kimi-family replay (#66948 salvage) - #67391

Merged
teknium1 merged 2 commits into
mainfrom
hermes/hermes-cc28a546
Jul 19, 2026
Merged

teknium1 merged 2 commits into
mainfrom
hermes/hermes-cc28a546

Conversation

@teknium1

Copy link
Copy Markdown
Collaborator

Summary

Salvage of #66948 by @FuryMartin — Kimi-family Anthropic endpoints now replay thinking blocks as-is (signed and unsigned) instead of stripping signed ones, restoring cross-turn reasoning recall on Kimi For Coding / Moonshot.

Root cause: the #13848-era contract assumed Kimi cannot validate Anthropic thinking signatures and stripped every signed block on replay. Live probes show the whole Kimi family accepts signed blocks (HTTP 200 even with mutated signatures) — the strip silently deleted the model's prior reasoning each turn.

Changes

  • agent/anthropic_adapter.py: _manage_thinking_signatures splits the shared Kimi/DeepSeek branch — Kimi family replays thinking unchanged; DeepSeek keeps strip-signed/keep-unsigned (DeepSeek /anthropic (V4 thinking): stripped thinking blocks cause HTTP 400 on replay #16748); third-party and direct Anthropic untouched.
  • tests/agent/test_anthropic_kimi_signed_thinking_replay.py: 6 new regression tests, incl. orphan-tool-turn case verifying the internal _thinking_signature_invalidated marker never reaches the wire.
  • contributors/emails/fanyu@moonshot.cn: contributor mapping for @FuryMartin.

Validation

Check Result
Targeted tests (new + thinking-block-order + adapter) 186 passed, 0 failed
OpenRouter Kimi (chat_completions path) wire A/B byte-identical request hash, main vs PR
Live OpenRouter kimi-k3 two-turn recall probe HTTP 200, model recalls prior-turn reasoning
Contributor's live probes (KFC + Moonshot /anthropic) 200 on verbatim AND mutated signed blocks

Cherry-picked with @FuryMartin's authorship preserved; rebase-merge.

Closes #66948.

Infographic

kimi-signed-thinking-replay

fanyu and others added 2 commits July 19, 2026 01:32
…replay

_manage_thinking_signatures treated every Kimi-family endpoint with the
#13848-era contract: strip signed Anthropic thinking blocks from replayed
history, assuming the upstream cannot validate Anthropic signatures.

Live probing shows that contract is outdated for the whole Kimi family:

- Kimi For Coding (api.kimi.com/coding) issues AND validates its own
  thinking signatures (K3+): both verbatim and content-mutated signed
  blocks replay with HTTP 200;
- Moonshot's Anthropic surface (api.moonshot.cn/anthropic) accepts signed
  blocks the same way (200 on both verbatim and mutated);
- every other harness that replays signed blocks to KFC (Claude Code, pi,
  Kilo Code) round-trips fine.

Stripping signed blocks there silently discarded the model's prior
chain-of-thought in multi-turn conversations — e.g. a two-turn recall
probe loses the reasoning between turns while the text answer survives
(agent.log: turn-2 input ≈ turn-1 input + a few dozen tokens instead of
+thinking).  With this change, the same probe recalls the exact hidden
values from turn-1 thinking (+230 tokens on turn 2).

So: on _is_kimi_family_endpoint, keep signed and unsigned thinking blocks
unchanged on replay — one uniform rule for the whole Kimi family, no
/coding-vs-Moonshot split.  DeepSeek keeps the #16748 contract (strip
signed, preserve unsigned).  Third-party and direct-Anthropic behavior is
untouched.

Add tests/agent/test_anthropic_kimi_signed_thinking_replay.py pinning the
unified behavior (Kimi /coding + Moonshot keep signed and unsigned) and
the unchanged neighbors (DeepSeek strips, direct Anthropic keeps).
@teknium1
teknium1 force-pushed the hermes/hermes-cc28a546 branch from b57db55 to 51f8ed0 Compare July 19, 2026 08:32
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/kimi Kimi / Moonshot P2 Medium — degraded but workaround exists needs-decision Awaiting maintainer decision before any implementation labels Jul 19, 2026
@alt-glitch

Copy link
Copy Markdown
Contributor

This was generated by AI during triage.

Related to open #66948: both preserve signed Kimi thinking replay, but this salvage broadens coverage from the Kimi /coding endpoint to the Kimi family while retaining DeepSeek handling. This is competing scope, not a duplicate.

@teknium1
teknium1 merged commit 94f8166 into main Jul 19, 2026
33 checks passed
@teknium1
teknium1 deleted the hermes/hermes-cc28a546 branch July 19, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint needs-decision Awaiting maintainer decision before any implementation P2 Medium — degraded but workaround exists provider/kimi Kimi / Moonshot type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants