Skip to content

fix(tier3): decode inline and Promise.allSettled Codex wrappers (#235) - #325

Merged
rng1995 merged 21 commits into
NVIDIA:mainfrom
shetsecure:fix/codex-wrapper-grammar
Nov 13, 2026
Merged

rng1995 merged 21 commits into
NVIDIA:mainfrom
shetsecure:fix/codex-wrapper-grammar

Conversation

@shetsecure

@shetsecure shetsecure commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Codex 0.161.0 emits native exec wrappers that the bounded static grammar currently classifies as unsupported_native_codex_exec_wrapper. Those wrappers then score security, skill execution, and parts of behavior at the neutral fallback and compress measured lift.

This PR extends the static grammar without evaluating JavaScript. It decodes:

  • inline text(await tools.NAME(...))
  • text((await tools.NAME(...)).output)
  • single-quoted object and string literals
  • tools.apply_patch('...') string arguments
  • a missing final semicolon
  • literal Promise.allSettled([...]) batches rendered by an indexed for, indexed forEach, or forEach(text)

Multi-call batches are decoded only when each call owns its own JSON record of the outer output. Dynamic arguments, shadowed or duplicate bindings, computed keys, spreads, template literals, unsupported renderers, and missing, extra, reordered, changed, or truncated records remain unsupported.

Error status

A call's own record decides whether it failed, and the outer script's exit status does not override it:

  • A rejected Promise.allSettled record marks its call failed and adds a status=failed line to the call's observation.
  • A fulfilled record, or an inline result, with a nonzero integer exit_code marks its call failed and adds the exit_code=N line the scorers already parse. A fulfilled promise only proves the tool returned, not that the command succeeded.
  • A record with no exit_code (a running process, write_stdin, apply_patch) leaves the outcome unknown, so is_error is not set and scoring falls back to the observation text.

normalized_tool_call_error_flag is wrapped so extract_tool_calls_as_dicts carries is_error for these calls. Duplicate JSON keys anywhere in a record, such as a repeated i, status or value, leave the whole wrapper unsupported, since the last duplicate would otherwise decide ownership or outcome.

The extended grammar now runs first for every exec wrapper, so a failed call grades the same whether it is double- or single-quoted, bound or inline. A lone call is graded by its own record when the record states an outcome. When the outer output has the exec header and a body that opens like a record but does not parse (truncated, trailing text, duplicate key), a lone call is left unsupported, so cut-off evidence cannot score as a clean run. Plain-text results such as apply_patch keep their whole output.

To fail closed where JavaScript would not run the script, the // @exec: pragma line ends at U+2028/U+2029, names are checked with str.isidentifier() (so r² is rejected and résultat still decodes), and wrappers using U+001C–U+001F or U+0085 as whitespace are unsupported. Both grammars reject a non-finite number such as 1e400, NaN or Infinity, which JavaScript serializes as null, and the new grammar also rejects a line break before => or ++.

check_error_recovery also reads the output field of a whole JSON record, whatever the settled field is called, so a no-match grep/rg that exits 1 keeps its quiet-probe exemption inside a batch. It ignores a record followed by extra text and no longer raises on deeply nested input.

Verification

  • ruff check src tests — passes
  • python -m build — builds the sdist and wheel
  • Focused module: uv run pytest tests/tier3/test_codex_tool_call_normalization.py -q — 642 passed
  • Affected consumers (normalizer, error recovery, failed skill reads, shared ATIF staging): 1,251 passed
  • Full suite: uv run pytest -q — 26,480 passed, 39 skipped, 31 failed

The 31 failures are 29 in tests/test_harbor_secure_docker_environment.py and 2 chmod-fallback tests in tests/test_harbor_secure_copy.py. The same 31 fail on unmodified main in my environment (I compared the test IDs), and CI covers them on a clean machine. Three tests that hang here were deselected: the two Ctrl-C tests in tests/ci/test_run_tier1.py and test_windows_transfer_repeated_cancellation_reaps_process_and_removes_partial_output.

New tests cover inline/single-quoted/apply_patch forms, every allSettled renderer and record shape, rejected records (Error object, empty and string reasons), fulfilled records with a nonzero exit code, lone calls with complete, truncated and duplicate-key records, record-versus-outer-failure precedence, duplicate index/status/value/nested keys, the pragma line terminators, JavaScript names and whitespace, whitespace before a lone record, the quiet-probe field name (including a field called output), dynamic fail-closed cases, and security scanning. The error-recovery and skill-activation cases run through both the host and bundled-verifier paths.

Prior art

The extended grammar follows the downstream Apache-2.0 skill-ci workaround for this issue (commit 60c92130 by @dirien), which reports 148 real calls decode. That workaround is a downstream patch, not an upstream fix; this PR upstreams the capability with tests and fail-closed coverage. Thanks to @dirien for the reference implementation.

Refs #235.

Signed-off-by: Amine Marzouki [email protected]

…IA#235)

Extend the bounded static Codex exec grammar to decode the wrappers that Codex 0.161.0 emits: inline text(await tools.NAME(...)), text((await tools.NAME(...)).output), single-quoted string literals, tools.apply_patch string arguments, a missing final semicolon, and literal Promise.allSettled batches rendered by an indexed for, indexed forEach, or forEach(text).

A multi-call wrapper decodes only when each call owns its own JSON record of the outer output. Dynamic arguments, shadowed bindings, unsupported renderers, and missing, extra, reordered, changed, or truncated records remain unsupported.

The extended grammar was validated in the Apache-2.0 skill-ci project (commit 60c92130). Refs: NVIDIA#235

Signed-off-by: Amine Marzouki <[email protected]>
Wrap normalized_tool_call_error_flag so extracted tool calls carry is_error for rejected Promise.allSettled records. A call's own record status now wins over the outer tool_result_is_error flag, so a fulfilled record is not reported as failed just because the outer script exited non-zero.

Add extractor and flag regression tests for fulfilled and rejected records, including an outer-failure observation.

Signed-off-by: Amine Marzouki <[email protected]>
…mmar

Signed-off-by: Amine Marzouki <[email protected]>

# Conflicts:
#	CHANGELOG.md

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

Reviewed head 6dee63f4840cbf7782c2ffaf847c88b956fed5c9 against current main 847bdb41885c804949355e2a937a0da6d0aad752 and issue #235. Requesting changes for the three inline findings: fulfilled tool failures are converted to success, rejected records can still receive clean error-recovery scores, and duplicate JSON keys bypass record-ownership/outcome validation.

Independent verification: 436 normalization tests and 3,085 affected-consumer tests passed. Real native command outputs were replayed through Harbor 0.24.0 and both host and bundled verifier scoring paths; the nonzero-exit scoring defect reproduced. Rejected-result and malformed-record findings were reproduced with targeted fixtures. This was not a fresh Codex 0.161.0 model-driven Harbor workload, and I did not independently rerun the full 18,830-test suite.

Please correct these cases, add scoring-level regressions, resolve the CHANGELOG conflict with current main, and rerun validation on the updated head.

Fail nonzero exit_code and rejected records; reject duplicate JSON keys.
Signed-off-by: Amine Marzouki <[email protected]>
@shetsecure

Copy link
Copy Markdown
Contributor Author

Thanks @chrisknvidia, all three are fixed, and main is merged so the CHANGELOG conflict is gone.

Each fix has regressions that run through check_error_recovery and check_activation, on both the host and bundled paths. Duplicate keys, including nested ones, leave the wrapper unsupported. A record with no exit code stays unknown.

One addition you didn't ask for: once nonzero exits counted as failures, a no-match grep exiting 1 inside a batch scored 0.0. check_error_recovery now reads the output field of a whole record, so that stays clean while a probe with a real diagnostic still fails. It touches the shared scorer, so please look at it closely.

Full suite: 22,041 passed, 0 failed. I left out the Docker environment, chmod-fallback and Ctrl-C tests, which also fail on unmodified main on my machine. I haven't run a fresh Codex 0.161.0 workload, so a replay on the new head would help.

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

Reviewed head 2c8cd16e against main 0ae3fdbf.

The three findings from the earlier review are fixed for the newly decoded forms. Rejected records and records with a nonzero exit_code now fail check_error_recovery and check_activation on both the host and bundled paths, and duplicate keys leave the wrapper unsupported. Locally, the 490 normalizer tests and 1,249 consumer tests pass (error recovery, activation, security, plugin signals, shared ATIF staging).

I've left two P2s inline:

  • Grading still depends on how Codex spelled the wrapper, because the record rules only run where the old grammar gave up.
  • A U+2028 or U+2029 on the // @exec: line hides code from the decoder.

The three P3s cover the lone-call record path, lexing differences between Python and JavaScript, and the quiet-probe field name.

The branch conflicts with current main again (8a851c6b). The conflict is only in the checks.py imports (import json against import bisect); keep both.

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

@shetsecure follow-up review on the current head. The three points from my earlier review are addressed, so I resolved those threads. Narendran's five open threads still apply; I left inline notes on each, with a reproduction for the quiet-probe field name. The branch also conflicts with main and needs main merged in.

A failed call now grades the same whether it is single- or double-quoted.
Signed-off-by: Amine Marzouki <[email protected]>
JavaScript ends a // comment there, so code after either one still runs.
Signed-off-by: Amine Marzouki <[email protected]>
A successful record now decides the grade, as it does for several calls.
Signed-off-by: Amine Marzouki <[email protected]>
Reject what JavaScript rejects: odd whitespace, non-ASCII names, line breaks before => or ++, 1e400.
Signed-off-by: Amine Marzouki <[email protected]>
The probe exemption no longer depends on the field name in text({i, NAME: r}).
Signed-off-by: Amine Marzouki <[email protected]>
Reject r² as a name while valid ones like résultat still decode.
Signed-off-by: Amine Marzouki <[email protected]>
A truncated or duplicate-key record no longer scores as a clean run.
Signed-off-by: Amine Marzouki <[email protected]>
U+001C-U+001F and U+0085 are whitespace in Python but not in JavaScript.
Signed-off-by: Amine Marzouki <[email protected]>
JavaScript serializes 1e400, NaN and Infinity as null, so decoding them to inf or nan was wrong.
Signed-off-by: Amine Marzouki <[email protected]>
@shetsecure

Copy link
Copy Markdown
Contributor Author

@rng1995 @chrisknvidia thanks, all five are fixed and main is merged, so the conflict is gone.

  • Spelling decides the grade: the extended grammar now runs first for every exec wrapper, so a failed call scores the same in all four spellings, including the double-quoted bound form.
  • U+2028 / U+2029: the // @exec: pragma line now ends at either character, and your inline and const examples fail closed.
  • Lone call and its record: a lone call is graded by its own record whenever the record states an outcome, and the record wins over the outer flag. A lone call with a truncated, trailing-text or duplicate-key record is unsupported.
  • JavaScript lexing: the new grammar uses JavaScript's whitespace, rejects a line break before => or ++, and both grammars reject 1e400, NaN and Infinity. Names go through isidentifier(), so r² is out and résultat still decodes. The older grammar's forms with U+001C–U+001F or U+0085 are unsupported too.
  • Quiet probe: the exemption follows the record's lone object-valued field, so grep -q scores 1.0 whatever the settled field is called.

Each one has a regression test. Full suite: 25,165 passed, 31 failed, and the same 31 fail on unmodified main on my machine (Docker environment and chmod tests).

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

Automated review: 2 findings (0 P1, 0 P2, 2 P3); 2 posted inline.

Re-reviewed at 28e776c. Status of the earlier comments, each re-checked with a probe:

  • My P2 (record outcome applied only where the old grammar gave up): fixed. The extended grammar runs first, and every spelling of a failed call grades the same.
  • My P2 (U+2028/U+2029 on the // @exec: line): fixed; it now fails closed.
  • My P3 (lone call ignoring an exit-0 record / outer-flag precedence): fixed.
  • My P3 (Python vs JS lexing): the four points raised are fixed and covered by tests.
  • My P3 (quiet-probe output found only via value/result): fixed except for a settled field named output; see the new inline comment at checks.py:4635.
  • chrisknvidia's P1 (fulfilled promise loses the tool outcome) and P2s (rejected records, duplicate keys): fixed.

A differential fuzz of about 86.5k single-character mutations across head, main and Node found no new mis-accepts or wrong rejections. The 620 normalizer tests and 919 consumer tests pass, and ruff is clean. The branch conflicts with main again, in CHANGELOG.md only, after #371.

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

Re-reviewed at 28e776c. My three earlier findings are still fixed, and so are rng1995's earlier findings; his two newer P3s still apply (replies inline on those threads).

Checked locally in both the host and the bundled extractors:

  • U+2028/U+2029 on the // @exec: line now fails closed (security 0.5, not passed) for the inline and const forms.
  • The wrappers JavaScript rejects (\x1c/\x85 as whitespace, r², a line break before =>/++, 1e400) stay unsupported.
  • A lone call's record decides its outcome, including for the const form the old grammar already accepted: an exit-2 record now scores error recovery 0.0 (main gives 1.0).
  • Quiet probes keep the exit-1 exemption under the result, outcome and settled field names, but not output (inline).
  • tests/tier3 passes (9952 passed, 10 skipped).

Nothing blocking from me beyond those two P3s.

Resolve the CHANGELOG.md conflict by keeping main's entries and this PR's entry.

Signed-off-by: Christopher Kevin <[email protected]>
… field

Start past leading whitespace, and keep descending when a settled field is itself named output.
Signed-off-by: Amine Marzouki <[email protected]>
@shetsecure

Copy link
Copy Markdown
Contributor Author

@rng1995 @chrisknvidia both new P3s are fixed, and main is merged again with the CHANGELOG conflict resolved. A record after leading whitespace now decodes, and a settled field named output keeps the quiet-probe exemption. Full suite: 26,480 passed, and the same 31 fail on unmodified main on my machine.

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

Re-checked all seven earlier comments at 7295301 on both the host and bundled-verifier paths, and each one is addressed: the extended program parser runs first with a fallback, the pragma regex stops at Unicode line separators, a lone exit-0 record with a traceback is graded from its record, malformed wrappers fail closed, _record_output follows a nested object-valued output, and leading whitespace no longer breaks the lone-call decoder. The two merges only resolve CHANGELOG. Approving.

rng1995 and others added 2 commits November 12, 2026 23:07
The main merge moved exit-status matching to _EXIT_STATUS_RE, but the new JSON quiet-probe helper still referenced the removed local name. Reuse the module-level matcher so Codex records can be scored on the host and bundled-verifier paths.

Signed-off-by: Narendran Raghavan <[email protected]>
@rng1995
rng1995 dismissed chrisknvidia’s stale review November 13, 2026 07:31

The requested fulfilled/rejected outcome handling and duplicate-key protections are fixed, their threads were resolved by the reviewer, and the reviewer subsequently acknowledged those fixes. The changelog conflict is also resolved. Verified current head 0d11179 with 1,464 focused tests; dismissing this superseded changes request after review of the fixes.

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

Thank you @shetsecure for your contribution and for your patience throughout the review process. All seven remaining review findings are addressed, including fail-closed parsing and quiet-probe behavior on the host and bundled-verifier paths. Fixed the merge-introduced regex reference in 0d11179 and verified 1,464 focused tests plus full source/test Ruff checks. The PR merges cleanly with main. Approved; auto-merge remains subject to required CI checks.

@rng1995
rng1995 enabled auto-merge (squash) November 13, 2026 07:32
@rng1995
rng1995 merged commit aa09db2 into NVIDIA:main Nov 13, 2026
26 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.

3 participants