Skip to content

fix(state,cli,tui-gateway): keep reasoning fields intact across forks and branches (#57240) - #82109

Merged
teknium1 merged 1 commit into
mainfrom
fix/57240-reasoning-roundtrip
Aug 9, 2026
Merged

fix(state,cli,tui-gateway): keep reasoning fields intact across forks and branches (#57240)#82109
teknium1 merged 1 commit into
mainfrom
fix/57240-reasoning-roundtrip

Conversation

@teknium1

@teknium1 teknium1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Session forks and branches now preserve all structured reasoning fields — previously every fork double-encoded reasoning_details / codex_reasoning_items / codex_message_items into JSON strings that all replay consumers silently dropped, and the CLI/TUI /branch writers didn't copy them at all (#57240).

Root cause: get_messages() returns those columns as raw stored TEXT, and both DB writers ran unguarded json.dumps() on whatever they received — so the fork endpoint's get_messages() → replace_messages() round-trip wrapped one extra quote layer per fork.

Salvaged from PR #57248 by @cryptoyasenka (commit intact with authorship); it was the more complete of the two contributor fixes for #57240 (covers the branch-writer gaps in hermes_cli + both TUI paths, not just the DB layer).

Changes

  • hermes_state.py: _reasoning_json_text() — strings that are already valid column TEXT are stored as-is; structures are dumped. Applied to both writers (append_message, _insert_message_rows).
  • hermes_cli/cli_commands_mixin.py, tui_gateway/methods_session.py, tui_gateway/server.py: /branch copy loops now forward the full reasoning field set (previously each surface copied a different subset).
  • Tests: tests/hermes_state/test_reasoning_roundtrip.py (fork round-trip regression incl. double-fork), branch-command and TUI-branch coverage.

Why this matters more since #81747

codex_reasoning_items now also carries native server-side compaction checkpoints — a forked gpt-5.6 session was silently losing its checkpoint, ballooning the fork's first request back to full history cost on top of the reasoning-continuity loss originally reported.

Validation

Result
Salvaged + existing suites (roundtrip, branch, tui_gateway) 538/538
Live E2E: source → fork → double-fork via real get_messages()→replace_messages() codex_reasoning_items/reasoning_details stay lists; compaction checkpoint byte-intact at every depth
Attribution audit clean

Infographic

Reasoning roundtrip

… and branches

get_messages() only deserializes content and tool_calls; the structured
reasoning columns (reasoning_details, codex_reasoning_items,
codex_message_items) come back as the raw TEXT they were stored as.
Feeding those rows straight back into a write, which is exactly what
the POST /api/sessions/{id}/fork handler does by piping get_messages()
into replace_messages(), hit an unguarded json.dumps() and stored the
already-serialized string encoded a second time. On replay of the fork,
json.loads() then yields the inner string instead of a list, and every
consumer's isinstance(..., list) gate silently drops it: preserved
Anthropic thinking blocks, Codex encrypted-reasoning/message-item
replay, and OpenRouter multi-turn reasoning context are all lost after
a fork, with one more encoding layer added per fork.

The /branch copy loop had the same defect from the other side: it
forwarded reasoning but none of the structured columns, and both TUI
branch writers persisted role/content alone, dropping reasoning and
reasoning_content along with them.

Route the six dumps sites in append_message and _insert_message_rows
through a shared guard that keeps already-serialized strings as-is;
structured values from the live runtime are dumped exactly as before.
Forward the reasoning fields in all three branch writers, matching the
set gateway/slash_commands.py already forwards on its own /branch path.
@teknium1
teknium1 force-pushed the fix/57240-reasoning-roundtrip branch from 971e915 to 6229f48 Compare August 9, 2026 00:24
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

૮ >ﻌ< ა ci review

ran on 6229f48

⚠️ Warnings

OSV vulnerability scan · View job

21 known vulnerabilities found in pinned dependencies.

How to fix:

Review the findings in the Security tab. Update the affected dependencies if a patched version is available.


debug info

CI timings

CI timings · View report · View job

Wall time 3m40s vs 5m24s (-32.1%). 9 job(s) slower, 12 faster, 2 unchanged.

  • Python tests / Run tests slice 6/12: -39.0s
  • Python tests / Run tests slice 12/12: -23.0s
  • Python tests / Run tests slice 2/12: +21.0s
  • Python tests / Run tests slice 8/12: +18.0s
  • Python tests / Run tests slice 4/12: +17.0s

@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 comp/cli CLI entry point, hermes_cli/, setup wizard comp/tui Terminal UI (ui-tui/ + tui_gateway/) provider/openai OpenAI / Codex Responses API P2 Medium — degraded but workaround exists sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state labels Aug 9, 2026
@teknium1
teknium1 merged commit f2d03c1 into main Aug 9, 2026
47 checks passed
@teknium1
teknium1 deleted the fix/57240-reasoning-roundtrip branch August 9, 2026 00:37
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 comp/cli CLI entry point, hermes_cli/, setup wizard comp/tui Terminal UI (ui-tui/ + tui_gateway/) P2 Medium — degraded but workaround exists provider/openai OpenAI / Codex Responses API sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants