Skip to content

Fix Codex tool history hygiene before upstream pairing - #36

Closed
jt-wang wants to merge 1 commit into
architectds:mainfrom
jt-wang:cursor/fix-tool-history-hygiene
Closed

jt-wang wants to merge 1 commit into
architectds:mainfrom
jt-wang:cursor/fix-tool-history-hygiene

Conversation

@jt-wang

@jt-wang jt-wang commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add uniquifyReusedToolCallIds so Codex histories that reuse short call_ids (e.g. exec_command_0 across turns) stay pairable instead of losing later turns during dedupe/pairing.
  • Add flattenChatToolCallsToResponses so assistant tool_calls rows paired with top-level function_call_output items are rewritten into standard Responses function_call / function_call_output pairs before forwarding.
  • Wire both into normalizeGatewayInput ahead of the existing pairing and dedupe passes.
  • Extend gateway tests for reused call_ids, chat-tool flattening, and the updated duplicate-call behavior on OpenCode Flash normalization.

Motivation

Long Codex sessions with repeated shell/tool turns can produce histories where:

  1. The same call_id appears in multiple turns (Codex reuse pattern).
  2. Tool results live as top-level Responses outputs while the assistant turn still carries chat-style tool_calls.

Strict upstream validators then reject the request or drop valid turns. These rewrites are provider-neutral bridge fixes — they do not change routing, profiles, or product features.

Test plan

  • Added unit tests in test/gateway.test.mjs
  • CI npm test

Made with Cursor

Uniquify reused call_ids after an earlier call/output pair closes so cross-turn
Codex reuse stays pairable while hybrid Pro duplicate repair still dedupes within
a turn. Flatten chat assistant.tool_calls into Responses function_call/output
pairs on the xAI input path only, preserving OpenCode Pro chat tool history.
@jt-wang
jt-wang force-pushed the cursor/fix-tool-history-hygiene branch from 46f245b to 629debd Compare August 31, 2026 00:18
architectds added a commit that referenced this pull request Sep 13, 2026
…ration

Fix tool history hygiene on current main (integrates #36)
@architectds

Copy link
Copy Markdown
Owner

Thank you @jt-wang. By the time this PR was reviewed, the gateway source had already drifted from the original base, including its native relay API and shared tool-history normalization path.

We preserved your tool-history contribution and original author attribution, adapted it to current main, and carried it forward in #39, which is now merged as 2c3f9ff6c2d0e264b3a4d384819dd2958be09031. The integration commit 987d0dfed23470de74e4c6088760d98bf29690d8 retains Jingtao Wang as its author and references your original commit 629debd2ea069f2964f2a418e3b13d88242ac006.

The adaptation puts mixed Chat/Responses conversion before the common pairing/deduplication pass. It also covers duplicate pending copies, reuse after completion, alias collisions, structured outputs and reasoning, rather than maintaining a second provider-specific pairing path.

Verification: the full local suite passed (1,141 pass, 7 skip, 0 fail), and all three CI jobs on #39 passed, including the actual macOS mock-install step. The built-bundle regression replays the sanitized 1,659-item captured session with the full Codex tool envelope, eight additional mixed tool rounds, Chat/Responses switching, compaction and resumption. Live provider checks were attempted but blocked by OpenCode Go credit exhaustion and Command Code's weekly quota; these are not claimed as successful inference tests.

Closing this original PR as superseded by the merged #39, not because the contribution was discarded. No release version was changed and no installed gateway/local model was restarted.

zxfandyy pushed a commit to zxfandyy/modeldock that referenced this pull request Sep 13, 2026
Integrate jt-wang's PR architectds#36 (629debd).
The original patch predates current gateway/native relay changes. Preserve
its reused-call identity and Chat-to-Responses conversion work and tests,
adapting them into the current shared pairing boundary.

Flatten mixed dialects before identity, pairing and deduplication. Keep
completed invocations distinct, coalesce identical pending copies, avoid
alias collisions, and retain structured output and visible reasoning.
Reject genuinely ambiguous pending calls instead of guessing their results.

Test all routed normalizers and the Chat bridge from one history fixture.
Replay a sanitized 1,659-item session plus eight mixed tool rounds through
the built bundle, including Chat/Responses switching and compaction.

Full Windows suite: 1141 passed, 7 skipped, 0 failed (1148 total).
Live Go and Command Code probes were blocked by account quota limits;
no successful live-provider inference is claimed.

Original-PR: architectds#36
Original-Commit: 629debd
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.

2 participants