Skip to content

fix(kimi): preserve reasoning_content across replayed tool turns#13864

Closed
helix4u wants to merge 1 commit into
NousResearch:mainfrom
helix4u:codex/fix-kimi-reasoning-content-replay
Closed

fix(kimi): preserve reasoning_content across replayed tool turns#13864
helix4u wants to merge 1 commit into
NousResearch:mainfrom
helix4u:codex/fix-kimi-reasoning-content-replay

Conversation

@helix4u

@helix4u helix4u commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the Kimi/Moonshot replay path that can send assistant tool-call messages back to the API without reasoning_content after a resumed or persisted session.

The change preserves provider-native reasoning_content separately from normalized reasoning, carries it through SQLite persistence and gateway transcript rewrites, and uses the explicit provider field when rebuilding outbound API history. For Kimi and Moonshot coding endpoints, replayed assistant tool-call messages now send reasoning_content: "" instead of omitting the field entirely when thinking is enabled and no scratchpad survived.

Related Issue

N/A

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • Added schema v7 in hermes_state.py to persist reasoning_content on assistant messages and restore it during conversation replay.
  • Updated run_agent.py to preserve provider-native reasoning_content when normalizing assistant messages and to prefer that field when rebuilding outbound API messages.
  • Added a Kimi/Moonshot strict replay guard so assistant tool-call messages keep a reasoning_content field even when the scratchpad is empty.
  • Threaded reasoning_content through gateway transcript append/rewrite and branch-copy paths in gateway/session.py and gateway/run.py.
  • Added regression coverage in tests/test_hermes_state.py, tests/gateway/test_session.py, and tests/run_agent/test_run_agent.py for migration, transcript rewrite, assistant normalization, and strict-provider replay.

How to Test

  1. Configure Hermes to use kimi-coding on https://api.kimi.com/coding with thinking enabled.
  2. Reproduce a tool-call turn, then resume or replay a session path that previously resent the assistant tool-call message.
  3. Confirm the next request no longer fails with thinking is enabled but reasoning_content is missing in assistant tool call message.
  4. Run source venv/bin/activate && scripts/run_tests.sh tests/test_hermes_state.py tests/gateway/test_session.py tests/run_agent/test_run_agent.py -k 'reasoning_content or schema_version or migration_from_v2 or RewriteTranscriptPreservesReasoning or TestReasoningReplayForStrictProviders or TestBuildAssistantMessage'.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Linux 6.6.87.2-microsoft-standard-WSL2

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

Repro symptom before the fix:

HTTP 400: thinking is enabled but reasoning_content is missing in assistant tool call message

@helix4u helix4u changed the title [codex] fix: preserve reasoning_content on Kimi replay fix(kimi): preserve reasoning_content across replayed tool turns Apr 22, 2026
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder comp/gateway Gateway runner, session dispatch, delivery provider/kimi Kimi / Moonshot labels Apr 22, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #13975 — your commit was cherry-picked onto current main with your authorship preserved (commit a7d78d3). Thanks for the fix and the thorough tests!

#13975

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder comp/gateway Gateway runner, session dispatch, delivery 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.

3 participants