Skip to content

feat: lower Codex functions.exec children into typed actions - #3063

Merged
Sinity merged 1 commit into
masterfrom
feature/gpt-pro/lin04-codex
Jul 18, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/gpt-pro/lin04-codex

Conversation

@Sinity

@Sinity Sinity commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Summary

Modern Codex Code Mode wraps nested tool operations (exec_command,
apply_patch, write_stdin, update_plan, wait, web search, image,
MCP resource calls) inside a JavaScript envelope that is itself the
argument to an outer exec/functions.exec function_call. Polylogue
previously only saw the outer transport call, so a session full of
nested Code Mode calls produced zero normalized file paths and zero
structured outcomes on the child operations even when the child results
carried real structured outcomes.

Problem

In a 100-session live sample cited on the tracking bead, every session
had nested tools.* calls: 14,004 envelopes held child operations and
19,180 results yielded zero structured paths/outcomes, although 1,444 of
those result texts textually contained exit_code — Polylogue had no
way to see any of that structure because it only parsed the outer
transport call.

Solution

  • polylogue/sources/parsers/codex.py: a conservative JS-literal scanner
    (_JsLiteralParser + _scan_code_mode_child_calls) walks
    tools.*/functions.* member-call syntax in the Code Mode source
    string without ever evaluating JavaScript. It recognizes a typed
    registry of child kinds (exec_command, apply_patch, write_stdin,
    update_plan, wait, web, image, mcp, unknown). Unparseable or
    dynamic arguments are retained as raw evidence with
    parse_state="malformed" rather than guessed. Each child call/result
    is lowered into an ordinary tool_use/tool_result block pair with a
    deterministic tool_id (<transport_tool_id>::polylogue-child::<n>),
    transport-linked via a _polylogue provenance object on tool_input.
    Outcome fields (is_error/exit_code) are populated only from
    structural JSON fields
    (top-level or nested under
    metadata/result/output) — free text containing "exit_code=N" is
    never promoted to a structural outcome. Repeated calls sharing one
    transport tool_id pair deterministically by occurrence rank (Nth use
    to Nth result), including when a result record precedes its call in
    the stream — no recovery/guessing is invented.
  • polylogue/archive/viewport/tools.py: adds apply_patch to
    FILE_EDIT and wait to the Codex agent-tool alias set so the new
    child tool names classify correctly on query/facet surfaces.
  • devtools/codex_exec_child_census.py (new): a reporting-only script
    that counts transport vs. typed-child actions, paired/orphaned
    results, path and structured-outcome coverage, plus an explicit
    outer-transport-only counterfactual baseline, over one or more Codex
    session files.
  • New fixtures
    tests/data/codex_event_stream/functions_exec_{single,multiple}.jsonl:
    the multiple fixture spans every registry type plus one unknown and
    one malformed call, and includes two results whose free text contains
    "exit_code=7"/"exit_code=9" that must not be promoted.
  • tests/unit/sources/test_codex_event_stream_contract.py
    (TestFunctionsExecLowering): pins per-fixture lowering, repeated/
    out-of-order call_id pairing (exercised against the production
    action_pairs SQL, not a test-local reimplementation), structured-vs-
    prose outcome handling, and asserts session_content_hash differs
    between the lowered projection and an outer-only projection of the
    same parsed session.
  • tests/unit/cli/test_query_expression.py and
    tests/unit/sources/test_tool_aliases.py gain coverage for the new
    child action names/categories.

Compatibility / migration notes

This adds new tool_use/tool_result blocks to existing Codex sessions
that use functions.exec/exec with nested Code Mode calls. Session
content hashes for those sessions will change
— this is asserted
directly by
TestFunctionsExecLowering.test_lowered_blocks_change_semantic_content_hash —
so the next polylogued run / reprocess will re-parse and re-materialize
affected sessions (expected, not a regression).

No source.db/user.db migration and no index.db schema version
bump: polylogue/storage/sqlite/action_pairs.py is unmodified by this
PR — it already pairs tool_use/tool_result rows per
(session_id, tool_id) via ROW_NUMBER(), so the new deterministic
child tool_id scheme (<transport>::polylogue-child::<n>) is paired
by the existing relation with zero new DDL.

Verification

devtools test tests/unit/sources/test_codex_event_stream_contract.py \
  tests/unit/sources/test_tool_aliases.py \
  tests/unit/devtools/test_codex_exec_child_census.py \
  tests/unit/cli/test_query_expression.py
-> 458 passed, 1 skipped

python -m devtools.codex_exec_child_census \
  tests/data/codex_event_stream/functions_exec_single.jsonl \
  tests/data/codex_event_stream/functions_exec_multiple.jsonl
-> transport_actions=2, typed_child_actions=11, paired_child_results=11,
   child_actions_with_path=3, child_results_with_structured_outcome=3

devtools verify --quick -> exit 0 (ruff format, ruff check, mypy --strict, render all --check)
devtools render all --check -> no drift (no polylogue/ module was added; devtools/ is outside the topology projection's tracked tree)

Anti-vacuity: temporarily forcing _code_mode_exec_envelopes() to return
{} (disabling child lowering entirely, i.e. the historical outer-only
behavior) makes all 9 lowering-dependent tests fail, including the
content-hash test and the census test — confirming the tests actually
exercise the new lowering path rather than passing vacuously. Reverted
after confirming the failure; suite is green again.

Ref polylogue-j2zz

@coderabbitai

coderabbitai Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 57 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: b5f4c9ea-5b85-4680-b898-36e5dece338c

📥 Commits

Reviewing files that changed from the base of the PR and between 38faf99 and 71402f8.

📒 Files selected for processing (9)
  • devtools/codex_exec_child_census.py
  • polylogue/archive/viewport/tools.py
  • polylogue/sources/parsers/codex.py
  • tests/data/codex_event_stream/functions_exec_multiple.jsonl
  • tests/data/codex_event_stream/functions_exec_single.jsonl
  • tests/unit/cli/test_query_expression.py
  • tests/unit/devtools/test_codex_exec_child_census.py
  • tests/unit/sources/test_codex_event_stream_contract.py
  • tests/unit/sources/test_tool_aliases.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/gpt-pro/lin04-codex

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@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: 170ce42b38

ℹ️ 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 on lines +874 to +878
calls.append(
_CodexExecChildCall(
tool_path=tool_path,
tool_name=_code_mode_tool_name(tool_path, registry_type),
registry_type=registry_type,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Do not equate JavaScript call sites with executed actions

When a functions.exec script contains control flow, such as a tool call in an untaken branch or a loop over several commands, this scanner emits exactly one tool_use for each syntactic call site without knowing whether or how often it ran. This fabricates actions for untaken branches and collapses repeated loop invocations into one action, causing incorrect action counts, commands, and result pairing; static call sites should not become ordinary actions unless runtime evidence establishes their invocation multiplicity.

Useful? React with 👍 / 👎.

Comment on lines +1151 to +1155
emitted: list[object] = []
for item in parsed:
emitted.extend(_decoded_structural_text_item(item))
if len(emitted) >= child_count:
break

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 Avoid treating every JSON emission as a child result

When valid Code Mode source emits an unrelated JSON diagnostic through text() before emitting the actual tool result, every decodable JSON content item is accepted as a child result. For a single child this loop reaches child_count on the diagnostic and stops, discarding the real result and potentially attaching the diagnostic's path or outcome fields to the action; ambiguous JSON emissions need recognizable result provenance before positional pairing.

Useful? React with 👍 / 👎.

Problem: modern Codex Code Mode wraps nested tool operations
(exec_command, apply_patch, write_stdin, update_plan, wait, web search,
image, MCP resource calls) inside a JavaScript envelope passed as the
argument to an outer exec/functions.exec function_call. Polylogue only
ever saw the outer transport call, so a session with 14,004 nested calls
projected zero normalized file paths and zero structured outcomes even
though many results were expressible outcomes.

What changed:
- polylogue/sources/parsers/codex.py: add a conservative JS-literal
  scanner (_JsLiteralParser + _scan_code_mode_child_calls) that walks
  `tools.*`/`functions.*` member-call syntax without ever evaluating
  JavaScript. Unparseable/dynamic arguments are retained as raw evidence
  with parse_state="malformed" rather than guessed. Each recognized
  child call/result is lowered into an ordinary child tool_use/tool_result
  block pair (tool_id = `<transport_tool_id>::polylogue-child::<n>`),
  transport-linked via a `_polylogue` provenance object on tool_input.
  Outcome fields (is_error/exit_code) are populated only from structural
  JSON fields (exit_code/is_error, optionally nested under
  metadata/result/output) — free-text mentions of "exit_code=N" are never
  promoted, matching the "structural or unknown, never regex-guessed"
  invariant. Repeated calls against the same transport tool_id pair
  deterministically by occurrence rank (Nth use to Nth result), including
  when a result envelope arrives before its call.
- polylogue/archive/viewport/tools.py: add apply_patch to FILE_EDIT and
  wait to the Codex agent-tool alias set so the new child tool names
  classify correctly for query/facet surfaces.
- devtools/codex_exec_child_census.py (new): reporting-only script that
  counts transport vs. typed-child actions, paired/orphaned results, path
  and structured-outcome coverage, and an explicit outer-transport-only
  counterfactual baseline, over one or more Codex session files.
- Fixtures: tests/data/codex_event_stream/functions_exec_{single,multiple}.jsonl
  cover a single child call, and ten children spanning every registry
  type plus an unknown and a malformed call, including two results whose
  free text contains "exit_code=N" strings that must NOT be promoted.
- Tests: TestFunctionsExecLowering in test_codex_event_stream_contract.py
  pins per-fixture lowering, repeated/out-of-order call_id pairing against
  the production action_pairs SQL, structured-vs-prose outcome handling,
  and asserts session_content_hash differs between the lowered projection
  and an outer-only projection of the same session. test_query_expression
  and test_tool_aliases gain coverage for the new child action names.

Compatibility/migration notes: this adds new tool_use/tool_result blocks
to existing Codex sessions that use functions.exec/exec with nested Code
Mode calls. Session content hashes for those sessions will change (this
is asserted directly by
TestFunctionsExecLowering.test_lowered_blocks_change_semantic_content_hash),
so the next `polylogued run` / reprocess will re-parse and re-materialize
affected sessions. No source.db/user.db migration, no index.db schema
version bump: action_pairs (polylogue/storage/sqlite/action_pairs.py) is
unmodified and already pairs tool_use/tool_result rows per (session_id,
tool_id), so the new deterministic child tool_id scheme is paired by the
existing view-backed relation with no new DDL.

Verification:
- devtools test tests/unit/sources/test_codex_event_stream_contract.py \
    tests/unit/sources/test_tool_aliases.py \
    tests/unit/devtools/test_codex_exec_child_census.py \
    tests/unit/cli/test_query_expression.py
  -> 458 passed, 1 skipped
- python -m devtools.codex_exec_child_census tests/data/codex_event_stream/functions_exec_{single,multiple}.jsonl
  -> transport_actions=2, typed_child_actions=11, paired_child_results=11,
     child_actions_with_path=3, child_results_with_structured_outcome=3
- devtools verify --quick -> exit 0 (ruff format, ruff check, mypy --strict, render all --check)
- devtools render all --check -> no drift
- Anti-vacuity: temporarily forcing _code_mode_exec_envelopes() to return
  {} (disabling child lowering) fails all 9 lowering-dependent tests,
  including the content-hash and census tests; reverted after
  confirming the failure.

Ref polylogue-j2zz

Co-Authored-By: Claude <[email protected]>
@Sinity
Sinity force-pushed the feature/gpt-pro/lin04-codex branch from 170ce42 to 71402f8 Compare July 18, 2026 03:00
@Sinity
Sinity merged commit 46e478f into master Jul 18, 2026
1 of 2 checks passed
@Sinity
Sinity deleted the feature/gpt-pro/lin04-codex branch July 18, 2026 03:00
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