Skip to content

fix(codex): keep code-tool calls in session transcripts - #361

Merged
zpzjzj merged 5 commits into
alibaba:mainfrom
karanb192:fix/codex-session-mcp-calls
Nov 2, 2026
Merged

zpzjzj merged 5 commits into
alibaba:mainfrom
karanb192:fix/codex-session-mcp-calls

Conversation

@karanb192

@karanb192 karanb192 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Newer Codex CLIs run MCP tools and shell commands from inside their exec code tool. In the session file, such a call appears only as an event_msg item_completed event, with an McpToolCall or CommandExecution item. The session parser skipped these events. When the session transcript was longer than the stream transcript, skill-up used it, so tool_called failed on runs where the tool did run.

This PR teaches the session parser both item types, in the form the stream transcript uses. It also keeps the error message of a failed MCP call as the tool result content, and reports a command that failed before it started as an error, on both the stream and the session path.

Related issues

Closes #359

Changes

  • internal/agent/codex.go:
    • The session parser turns an item_completed event into a tool call and a tool result when the item is an McpToolCall (named mcp__<server>__<tool>, as on the stream path) or a CommandExecution (named command_execution, with the argv joined into the shell string the stream reports). It skips other item types.
    • Codex also writes a CommandExecution item for a direct exec_command function call, with the same ID, after the function_call line. The parser records such a call once: the call takes the item's name and arguments (command_execution with a command string, as on the stream path), and the result keeps the function_call_output content. If the item reports failure, the result is an error, whichever of the two lines comes first.
    • A failed MCP call has result: null and an error.message. The tool result content is now that message instead of an empty string, on the stream path (mcp_tool_call) and the session path.
    • A command that failed before its process started has no exit code. A failed status now makes its result an error, on both paths.
  • internal/agent/codex_test.go: tests built from hand-written event lines in the shape Codex CLI 0.166 writes:
    • session file with a completed McpToolCall item and a CommandExecution item, next to custom_tool_call lines and an AgentMessage item that stay ignored;
    • session file with a failed McpToolCall item and a failed CommandExecution item;
    • session file with a direct exec_command call and its matching CommandExecution item, which must appear once, in the stream's form, with the function_call_output content;
    • a direct call whose item failed without an exit code, in both line orders, which must stay an error;
    • a command that failed before it started, on the session and the stream path;
    • stream mcp_tool_call that failed with an error message;
    • Run where the session transcript is longer than the stream one and wins. The test checks the session-only call IDs, so it fails if the stream transcript is used or if the session one drops the command. This is the bug(codex): tool_called misses MCP calls Codex makes from its code tool #359 case.
  • CHANGELOG.md: one line under Unreleased, Fixed.

Test plan

  • make test passes
  • make verify: fmt and vet pass. Lint reports 4 staticcheck SA5011 findings in internal/agent/custom_test.go, a file this PR does not touch; on my machine clean main reports the same 4. golangci-lint run ./... outside the Makefile cache and the pre-commit incremental lint report 0 issues.
  • The session-item tests and the Run test fail without the parser change and pass with it. The direct-call and failed-command tests fail on the earlier commits of this branch and pass on the head.
  • Manual: the bug(codex): tool_called misses MCP calls Codex makes from its code tool #359 repro (Codex CLI 0.166.0, environment.type: none, bypass_sandbox: "true", existing login) went from 50% (tool_called false) to PASS 100%. On that run's files the session transcript has 12 messages and the stream 6; both now carry the same two calls (command_execution, mcp__shop__get_order).

Notes for reviewers

  • I checked the event shapes against local session files from Codex CLI 0.160 to 0.166. None records an MCP call both as a function_call and as an McpToolCall item. Every direct exec_command call (347 of them) also has a CommandExecution item with the same ID, always after the function_call; the merge above records such a call once and keeps its output. I had no session files from older CLIs such as the default pin 0.80.0. There, the parser behaves as before unless such items appear.
  • Sessions from CLIs that write no items (I had none from the default pin 0.80.0) keep today's form: a direct call stays exec_command with the raw arguments string there.
  • custom_tool_call and custom_tool_call_output (the code tool itself) are still skipped. Their output is an array in 0.166 and does not decode into the current payload struct. The calls the code ran are what judges need, and those now come from the items.

Newer Codex CLIs call MCP tools from inside their exec code tool. The
session file then records the call only as an event_msg item_completed
event with an McpToolCall item, which the session parser skipped. When
the session transcript was longer than the stream one, skill-up used it
and tool_called missed calls that did run.

Parse McpToolCall items into a tool call and tool result. A failed MCP
call now keeps its error message as the tool result content on both the
stream and the session path.

Closes alibaba#359
…ipts

Adding MCP calls made the session transcript longer, so it could now win
over a stream transcript that held command_execution calls the session
parser still dropped. Parse CommandExecution items as well, joining the
session argv into the shell string the stream reports.

Codex also writes a CommandExecution item for a direct exec_command
function call, under the same ID and after the function_call line, so
skip items whose call is already recorded.
@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T02:46:41.190558Z 6ac3296 Manual request
ℹ️ 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: 1debbd5fb6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread internal/agent/codex.go Resolved
Comment thread internal/agent/codex.go Resolved
A direct exec_command call is recorded twice in the session file: as a
function_call and as a CommandExecution item under the same ID. Instead
of dropping the item, give the recorded call the item's name and
arguments (command_execution with a command string), so a winning
session transcript matches what the stream reports. The result still
comes from function_call_output.

A command that fails before its process starts has no exit code. Treat
a failed status as an error on both the stream and the session path.
@karanb192

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 6ac3296a11

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@zpzjzj zpzjzj 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.

LGTM

@zpzjzj
zpzjzj added this pull request to the merge queue Nov 2, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 2, 2026
@zpzjzj
zpzjzj added this pull request to the merge queue Nov 2, 2026
Merged via the queue into alibaba:main with commit e4a41d7 Nov 2, 2026
14 checks passed
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.

bug(codex): tool_called misses MCP calls Codex makes from its code tool

2 participants