Skip to content

fix(qwen): preserve text order around transcript tool calls - #457

Open
karanb192 wants to merge 2 commits into
alibaba:mainfrom
karanb192:fix/preserve-qwen-part-order
Open

karanb192 wants to merge 2 commits into
alibaba:mainfrom
karanb192:fix/preserve-qwen-part-order

Conversation

@karanb192

@karanb192 karanb192 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a Qwen session message contained text, a tool call and more text, the transcript showed the tool call first, followed by both text fragments.

The parser now emits each run of adjacent text before the next tool call or result, so the transcript keeps the original order.

Splitting a message this way has two side effects, and this PR handles both:

  • Turns. countTurns counts every assistant transcript entry, so one split message would count as several turns. The parser now counts assistant lines that have visible text, which is the same count as before the split. Turns and turns_executed use that count when a session file was parsed. The stdout fallback still uses countTurns.
  • Final message. The evaluator replaces FinalMessage with the transcript's last assistant entry. The parser now returns the same text: the text after the last tool event. This matches Claude Code and Codex, whose final message is the last assistant text after the last tool call. It only differs from before when the last assistant line itself carries a tool call.

Test plan

The session-file regression expects three transcript entries in order. It also checks that the parser's final message equals Transcript.FinalAssistantMessage() and that the split message counts as one response. The thought-text test checks that thought-only and tool-only lines are not counted.

Local checks: go test -race ./... and make verify.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T03:12:36.426482Z 8773f5f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8773f5fd21

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/agent/qwen_code.go Outdated
Comment on lines +478 to +480
flushText()
if role == transcript.RoleAssistant {
return content
return strings.Join(textParts, "\n\n")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the combined final response through evaluator normalization

For the new Checking./tool/Done. case on a normal single-shot Qwen run, the combined response returned here does not survive evaluation: normalizeSessionResult in internal/evaluator/evaluator.go:1715-1716 replaces FinalMessage with Transcript.FinalAssistantMessage(), which now returns only Done.. Consequently, reports and output_contains/output_matches rules receive a truncated answer, potentially changing verdicts for text before the tool event. Preserve the combined response through normalization and cover the evaluator path rather than only the parser.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in b336894. The parser's final message is now the text after the last tool event, the same entry the evaluator's normalization picks, consistent with Claude Code and Codex. The new test checks the two agree.

Comment thread internal/agent/qwen_code.go Outdated
flushText := func() {
if emitted < len(textParts) {
content := strings.Join(textParts[emitted:], "\n\n")
*messages = append(*messages, transcript.Message{Role: role, Content: content, Turn: turn})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Count Qwen turns independently of rendered text fragments

When one assistant session record contains text runs separated by tool events, each flush creates another RoleAssistant entry, but buildSessionResult still derives Turns through countTurns(trans), which increments for every assistant transcript entry. The new regression fixture therefore reports two turns instead of the previous one despite containing only one source assistant response; further interleavings inflate it further. This incorrect count propagates into report turns and judge turns_executed, so preserve the source-response count independently of transcript splitting.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in b336894. Turns are now counted per source assistant response with visible text, so a message split around a tool call counts once.

Splitting a session line around a tool call gave it several assistant
transcript entries. countTurns counted each one, so a single response
raised turns and turns_executed. Count assistant lines with visible text
in the parser instead, as before the split.

The parser also returned the joined text of the line as the final
message, while the evaluator replaces FinalMessage with the transcript's
last assistant entry. Return the text after the last tool event, so both
agree and match the other engines.

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.

1 participant