Skip to content

fix(aimux): read openai-codex text-block thinking tags as thinking - #2322

Open
yyhhyyyyyy wants to merge 2 commits into
mainfrom
hui/codex-thinking-tags
Open

yyhhyyyyyy wants to merge 2 commits into
mainfrom
hui/codex-thinking-tags

Conversation

@yyhhyyyyyy

Copy link
Copy Markdown
Collaborator

What

The managed openai-codex provider writes its reasoning into the text stream itself, wrapped in <thinking>…</thinking>. Dim streamed it as assistant text, so the tags reached the reply and the reasoning entered the next request as ordinary assistant text. This change converts the tags at the provider boundary the issue names, scoped to the openai-codex preset: the conversion runs only when the driver's connection carries preset == "openai-codex", which stream::convert receives as its new scan_thinking_tags parameter at crates/dim-agent-aimux/src/driver.rs:953.

When a text block's first non-whitespace content is the open tag, the block converts to thinking until the close tag, and what follows the close tag is text again. Both tags complete across deltas: a strict prefix of the open tag buffers undecided, and the tail of the thinking holds back the longest suffix that may still complete the close tag, so no fragmentation of the provider's deltas leaks half a tag. A block that never closes the tag keeps its content as thinking. A reply of nothing but tagged thinking answers the turn instead of failing as an empty response. The tags never enter step/text and never enter the next request: the converted thinking part carries no continuation metadata, so the existing replay rule for unsigned thinking in crates/dim-agent-aimux/src/request.rs keeps it out, while the item's continuation metadata lands on the trailing text part.

Boundary behavior

Each behavior is pinned by a test in crates/dim-agent-aimux/src/stream/tests.rs unless noted otherwise.

  • A tag inline, mid-block, or inside a code fence stays verbatim text (thinking_tags_inside_text_stay_verbatim).
  • Near-openings that are not the tag (<thin, < thinking>, <thinkingx, <think>) replay their buffered bytes verbatim, nothing lost or repeated (thinking_tags_almost_openings_are_buffered_verbatim).
  • Several text blocks in one turn, for example around a tool call, scan independently (thinking_tags_scoped_blocks_scan_independently).
  • A connection without the preset streams the same bytes untouched (thinking_tags_other_providers_stream_them_verbatim, and end-to-end in crates/dim-agent-aimux/tests/model_flow.rs responses_without_the_codex_preset_streams_tags_verbatim).
  • An empty tag <thinking></thinking> has no visible output and commits no step/thinking: exactly like empty native reasoning, the reply fails as MalformedResponse and goes through the single empty-reply retry (thinking_tags_empty_section_answers_nothing).
  • Whitespace-only content inside the tags is a different case: it commits a blank step/thinking, which is not replay-safe, so the turn fails directly without a retry, like non-empty native reasoning.
  • Leading whitespace before the open tag is not text (thinking_tags_leading_whitespace_before_the_open_tag); text on the same line right after the close tag is streamed as text (thinking_tags_close_followed_by_text_on_the_same_line); a tag unclosed at the block end keeps its content as thinking (thinking_tags_unclosed_at_the_block_end_stay_thinking).

Acceptance criteria of the issue

  1. First non-whitespace content <thinking> converts until </thinking>, then text again; both tags may complete across deltas, down to one byte per delta: thinking_tags_convert_a_codex_block_into_thinking_then_text and thinking_tags_split_across_deltas_convert_identically (which covers <thin+king>, </think+ing>, and a per-byte splitting).
  2. Tags never enter step/text or the next request's assistant body, and the converted thinking carries no continuation metadata: end-to-end codex_thinking_tags_leave_text_as_thinking_and_the_next_request drives a scripted local Responses stream through the real driver into a Session and asserts the second request body contains neither the tags nor the thinking text; thinking_tags_continuation_metadata_reaches_only_the_text_part asserts the metadata lands only on the trailing text part.
  3. Only a tag at the block start converts; mid-text, inline and code-fence occurrences stay verbatim: thinking_tags_inside_text_stay_verbatim and thinking_tags_almost_openings_are_buffered_verbatim.
  4. A block that ends before </thinking> keeps its content as thinking: thinking_tags_unclosed_at_the_block_end_stay_thinking.
  5. The TUI end-to-end check was not performed, and the reason is factual: the conversion only activates for the managed openai-codex account login, whose production OAuth configuration hardcodes the real issuer and relay endpoints with no override, while hand-written or static-provider configurations of this preset are rejected during provider assembly, so no test harness can hand the isolated dimcode TUI a connection with this preset without changing production code outside this change's scope. Substitute evidence: the end-to-end test asserts field by field that the converted thinking is structurally identical to native reasoning on the Timeline (EventKind::Thinking before EventKind::Text, no tag characters anywhere), and the TUI's rendering of thinking (the streamed block, its settled Thought row and its rewrap across resizes) is the same render path already covered by the tmux scenarios resize_during_thinking_rewraps_multiline_history and composer_borders_match_in_scrollback_and_fullscreen in scripts/tui-tmux/scenarios.mjs. A reply of nothing but tagged thinking completes the turn without an empty-reply retry: thinking_tags_only_reply_answers_the_turn and end-to-end codex_thinking_tags_only_reply_completes_the_turn (which also asserts exactly one request was issued).

Existing tests

crates/dim-agent-aimux/src/stream/tests.rs gained one false argument at each of the four existing stream::convert call sites in the fixture helpers, because convert gained the scan_thinking_tags parameter. No existing assertion changed.

Verification

Run locally on this head:

  • cargo fmt --all -- --check --config error_on_line_overflow=true
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo test --locked -p dim-agent-aimux
  • cargo test --locked -p dim-agent-types
  • cargo test --locked -p dim-app-server --test app_server_flow
  • cargo test --locked -p dim-agent-capabilities --test capabilities_flow
  • cargo test --locked -p dim-agent-integration-tests --test integration_flow
  • cargo test --locked -p dim-app-server --test transport_flow
  • cargo check --locked -p dim-agent-aimux --target x86_64-pc-windows-gnu
  • cargo run --locked -p xtask -- check
  • the Node-backed transport_flow node_ and app_server_flow mcp_http_flow::node_ ignored tests through the repository's scripts/run-rust-test.mjs runner

Fixes #2300

The managed openai-codex provider writes its reasoning into the text stream itself, wrapped in `<thinking>`…`</thinking>`, where every other consumer of the account sees it as reasoning; Dim streamed it as assistant text, so the tags and the reasoning reached the reply and the reasoning entered the next request as signed-off text.

Convert at the provider boundary the issue names, scoped to the openai-codex preset the driver already gates on: when a text block's first non-whitespace content opens the tag, the block converts to thinking until the close tag, and what follows is text again. Both tags complete across deltas: a strict prefix of the open tag buffers undecided, and the tail of the thinking holds back the longest suffix that may still complete the close tag, so a split the provider fragments never leaks half a tag. A block that turns out not to open the tag replays its buffer verbatim, bytes neither lost nor repeated; a block that never closes the tag keeps its content as thinking; a reply of nothing but its tagged thinking answers the turn instead of failing as an empty response. The converted part carries no continuation metadata, so the existing replay rule for unsigned thinking keeps it out of the next request, while the item's metadata lands on the trailing text part.

End-to-end tests drive the conversion through a scripted local Responses stream into a Session: thinking precedes text on the Timeline, the next request carries neither the tags nor the unsigned thinking, a tag inside the text stays verbatim, and a connection without the preset streams the same bytes untouched.

Fixes #2300
Keep the open-tag strip in the shape clippy reads as a strip_prefix, and expect the conversion's one-argument-per-handle signature the way the crate's other host-assembled functions do.

Refs #2300

@eric8810 eric8810 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The preset scope and provider-side placement match #2300, and the Session fixture is useful evidence for the normal path and next-request filtering. Two acceptance gaps remain:

  1. Flush an open tag scan when a valid length finish arrives without TextEnd. The new scan is drained only in TextEnd, but Finish intentionally accepts active blocks on MaxTokens. The existing truncated-block contract test pins that path. A Codex text block truncated at a near-opening like <thin loses the buffered text; an open thinking section truncated during a potential closing tag loses its held suffix and never emits ThinkingEnd from the scan. Drain pending scans before reporting the length stop, preserving the existing MaxTokens partial-output policy. Add scoped tests for both pending states with no TextEnd; ordinary Stop still must reject incomplete upstream blocks.
  2. Provide the terminal evidence in #2300. Timeline and second-request assertions establish the adapter output, not that Dim's TUI renders the normalized section as a foldable thinking row next to the answer. No production OAuth override is needed for that part: use the normalized Timeline events from the local fixture as TUI replay input, or provide a sanitized terminal capture of the real path. Please show the answer without tags and the thinking row collapsed and expanded. Keep the existing provider fixture for the next-request assertion.

Static review and fixture inspection; no live provider call. CI for this head was still running.

@eric8810 eric8810 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope correction from the maintainer: this is a rendering-only change. This supersedes my previous review’s acceptance criteria and the wording in #2300 about removing the tagged content from the next request.

Hide the <thinking> delimiters in the rendered transcript. If the view presents the enclosed text in a collapsible thinking row, that grouping is for display only. Keep the provider stream, stored Timeline facts, and next-turn assistant input unchanged from the base, including the original tags, text, and continuation metadata. Do not replace a persisted text part with unsigned thinking or change replay policy for this fix.

Acceptance: exercise the same tagged reply in the rendered terminal (including a literal mention of the tag in normal text), and assert the stored text and next-turn request still carry the original bytes. The previous request to flush the provider-side tag scanner on MaxTokens is superseded if that scanner is removed; the display should still preserve incomplete literal text rather than drop it.

@zerob13 zerob13 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fragment handling and literal-tag cases look good on the normal path. Two correctness issues remain in the adapter implementation currently in this PR:

  1. A length-limited finish can drop buffered output. Finish handling accepts an open block on MaxTokens, but the tag scan only drains at TextEnd. With no TextEnd, an undecided text prefix can disappear, and a normalized block can lose its buffered tail and completion. If the scanner stays, verify that both pending states preserve the expected partial output and still report the length limit. Ordinary stop should still reject an incomplete upstream block.
  2. Replay behavior changes across providers. Same-provider filtering does not establish that converted parts stay out of a later request after switching providers. Source tracing shows that a destination serializer can include unsigned parts. If adapter normalization is the chosen scope, verify with synthetic content that the ordinary answer survives and converted content stays out of the next request even after a switch.

#2300 describes adapter normalization, while the latest scope review proposes display only. These are alternative contracts. If display-only is confirmed, the adapter findings do not require new adapter logic: keep the original stream, Timeline and replay unchanged, and verify the rendered behavior with synthetic content. Source and fixture inspection only; no live provider calls. Exact CI was still running Linux workspace tests.

This branch has not been deployed

No deployments
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.

[models] openai-codex 正文中的 <thinking> 标签按思考解析,不进入助手正文

3 participants