Skip to content

feat(#497): CodexDriver core — subprocess codex exec --json, SDK-typed parser, three-level outcome evaluator - #1062

Merged
admarble merged 4 commits into
mainfrom
feature/1058-feat-497-codexdriver-core-subprocess-codex-exec-js
Sep 15, 2026
Merged

admarble merged 4 commits into
mainfrom
feature/1058-feat-497-codexdriver-core-subprocess-codex-exec-js

Conversation

@admarble

@admarble admarble commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Adds CodexDriver (src/lib/workflow/drivers/codex.ts): spawns codex exec --json -C <cwd> -s workspace-write --dangerously-bypass-hook-trust [-m <model>] <prompt> as a subprocess and parses its JSONL stream, following OpencodeDriver's buildXArgs / XStreamParser / evaluateXRun split.
  • Wires codex end to end: CodexSettings on RunSettings.codex (+ zod schema, validator, KNOWN_KEYS, settings-reference docs), codexSettings on ExecutionConfig, buildExecutionConfig, the DRIVERS registry, and all three getDriver call sites in phase-executor.ts.
  • Types come from @openai/codex-sdk as a devDependency used for types only — import type at every site, and nothing referencing it survives into dist/.

Deliberately not the SDK's Codex/Thread classes: --dangerously-bypass-hook-trust is CLI-only, Thread._exec is private (no stderrTail/exitCode), and the SDK pins the binary version exactly.

AC verification

AC Description Status Evidence
AC-1 Parses the recorded fixture ✅ codex.test.ts -t "497 AC-1" — 15 passed. Asserts agent_message.text → output, thread.started.thread_id → resumeHandle.token, all five usage counters → modelUsage, and that the three item.completed/error items do not set success: false
AC-2 Spawn contract ✅ -t "497 AC-2" — 8 passed. Exact argv, stdio: ["ignore","pipe","pipe"], detached: true, unmodified config.env, abort → SIGTERM on -pid, both RingBuffer tails, exitCode
AC-3 Three-level outcome ✅ -t "497 AC-3" — 7 passed; all four mutations verified below
AC-4 Resume honors #674 ✅ resume-semantics.test.ts -t "codex" — 7 passed. canResume false across cwd and across driver; resume args are exec resume <token> --json … with no -C
AC-5 Registry + settings + CLI ✅ drivers/index.test.ts -t "497" — 3 passed; node dist/bin/cli.js run 1 --agent codex --dry-run prints Driver: codex three times with no spawn
AC-6 Warnings surfaced ✅ -t "497 AC-6" — 2 passed. Hook-trust banner and model-metadata notice reach config.onStderr; run still succeeds
AC-7 Live SKIP-when-absent ✅ codex.live.integration.test.ts — gate test passes, 2 live tests reported skipped (never passed). Mirrors opencode.live.integration.test.ts's skipReason()/it.skipIf shape, as the AC's own assumption requires
AC-8 SDK is types-only ✅ grep -r "codex-sdk" dist/ | wc -l → 0 after npm run build; devDependencies-only asserted in-suite
AC-9 Execution-config parity ✅ execution-config-parity.test.ts — 16 passed, incl. a new codex-configured scenario and an explicit codexSettings equality assertion across both producers
AC-10 Version floor ✅ -t "497 AC-10" — 7 passed. CODEX_MIN_VERSION = "0.154.0" exported; isAvailable() parses codex --version; getCodexVersionError() names the floor
Derived
AC-11 reasoning_output_tokens explicitly mapped ✅ Folded into outputTokens (3306 + 3200) with a comment stating the choice — toModelUsage in codex.ts
AC-12 .agents/skills caveat documented ✅ See "Known gaps" below and the resolvesSkills doc comment

Mutation verification (AC-3)

Each mutation was applied to codex.ts from a verified-clean tree (git status --short | wc -l = 0), run against codex.test.ts, then reverted with git checkout.

Mutation-verified: AC-3 — deleted the turn.failed fatal branch from evaluateCodexRun; codex.test.ts > 497 AC-3(a) turn.failed is fatal even on a clean exit failed (1 failed / 35 passed); restored.

Mutation-verified: AC-3 — deleted the top-level error-event fatal branch from evaluateCodexRun; codex.test.ts > 497 AC-3(b) a top-level error event is fatal failed (1 failed / 35 passed); restored.

Mutation-verified: AC-3 — deleted the noTerminalTurn branch, leaving only the generic non-zero-exit path; codex.test.ts > 497 AC-3(c) exit != 0 with no turn.completed fails with a distinct code failed (1 failed / 35 passed); restored.

Mutation-verified: AC-3 — reclassified non-fatal error items as fatal stream errors in the parser; codex.test.ts > 497 AC-3(d) an added item.completed/error alone is still a success failed (7 failed / 29 passed — the fixture contains three such items, so every test depending on it failing-closed also went red); restored.

Design notes worth a reviewer's attention

Four deliberate divergences from OpencodeDriver, each documented in codex.ts's header:

  1. No shim preflight. codex has no "the hooks loaded" signal to check — --dangerously-bypass-hook-trust is the enablement mechanism.
  2. config.env forwarded unmodified. No XDG_CONFIG_HOME redirect; inventing one would break codex login's stored credentials.
  3. Exit 0 with no turn.completed is not fatal. opencode treats the analogous case as failure (spike(opencode): does opencode run --command <phase> load the sequant skill? (#862 P0 precondition, resolves OQ-8/OQ-9) #992); AC-3 scopes the codex equivalent to exit≠0, and failing the exit-0 case would reject runs no evidence says are broken.
  4. isAvailable() enforces the version floor, which opencode/aider leave to doctor. AC-10 asks for it explicitly. Nothing on the execution path calls isAvailable() today, so it is inert until a caller opts in.

Two more:

  • modelUsage key. codex reports usage per turn and never names the model, so the map is keyed by settings.model, falling back to "codex-default" when unpinned rather than silently merging into another backend's totals.
  • AC-8's guard shapes the comments. tsc copies comments into dist/ verbatim, so the package name appears only in the (erased) import type and in package.json — never in prose. Spelling it out in a comment turns the AC's grep permanently red and stops it meaning "no runtime import survived". codex.ts says so explicitly, to stop a future edit from reintroducing it.

Incidental fix

run.codex was rejected by the settings key allowlist (⚠ Unknown key 'run.codex' in settings.json (ignored) on every run), which would have made the AC-5 interface a dead setting. Registered in KNOWN_KEYS and documented in the settings reference alongside run.aider/run.opencode.

Known gaps

Test plan

  • npm run build — clean
  • npm run lint — clean (0 errors, 0 warnings)
  • Full suite: 6397 passed, 3 failed, 4 skipped (6405)
  • All 3 failures confirmed pre-existing: each reproduces identically at the base commit e1b867c4 in the same worktree with these changes absent — relay-hook.integration.test.ts, prompt-wait.test.ts, and batch-executor.test.ts > withActivityHook (the last asserts withActivityHook returns its input unchanged, but it now installs onActivity/onAutoWait regardless; batch-executor.ts is untouched by this PR)
  • Live smoke against a real binary: CODEX_LIVE=1 npx vitest run src/lib/workflow/drivers/codex.live.integration.test.ts — 2 passed against codex 0.154.0 with a real CODEX_API_KEY (post-QA; see below)

Post-QA fixes (9eb18cc8, after merging origin/main at v2.15.1)

Second-look review with the real binary and key found two defects the phase agents could not have seen without them, plus the §6i gap QA reported:

  1. Reasoning tokens were double-counted. toModelUsage summed output_tokens + reasoning_output_tokens, but Codex's reasoning_output_tokens is a breakdown of output_tokens (Responses-API shape). Every probe run shows it — output − reasoning equals the visible text (fixture: 3306 − 3200 = 106 tokens for 165 chars; a bare PONG reply reports 6/0). outputTokens now takes output_tokens as-is; the AC-1 assertion drops from 6506 to 3306. The original AC-11 comment's "3200 of 6506" was citing a number produced by the bug.
  2. The AC-7 live test could never pass. It spawned in a bare mkdtemp dir and codex exec refuses a non-git cwd (Not inside a trusted directory and --skip-git-repo-check was not specified, exit 1, zero events). The test now git inits the temp dir — the driver deliberately does not pass --skip-git-repo-check, since sequant only ever hands it a worktree. The former "parses a real stream" test fed a synthetic line; it is replaced by re-parsing the captured raw stream of the live run and asserting it agrees with the driver's result.
  3. §6i mutation markers were test_not_in_diff. failedTest lacked the file prefix the gate keys on; all four now read codex.test.ts > 497 AC-3(x) … and classify valid.

Also verified on the merged branch: npm run lint clean, npm run build clean, grep -r codex-sdk dist/ → 0, run 1 --agent codex --dry-run → Driver: codex ×3. Full suite: 6398 passed; the 2 remaining failures (version-preflight.docs, tui/load) are ~5 s load-timeouts that pass 2/2 when run targeted, and the earlier doctor pair was the v2.15.0→2.15.1 version-drift artifact, gone after the merge.

Closes #1058

🤖 Generated with Claude Code

t added 4 commits September 15, 2026 01:18
…d parser, three-level outcome evaluator

Entire-Checkpoint: cd1d5c7b7037
…-codexdriver-core-subprocess-codex-exec-js

# Conflicts:
#	CHANGELOG.md
…-codexdriver-core-subprocess-codex-exec-js

# Conflicts:
#	CHANGELOG.md
… smoke test run in a git repo

- toModelUsage summed output_tokens + reasoning_output_tokens, but reasoning is a breakdown of output_tokens (every probe run: output - reasoning equals the visible text). outputTokens now takes output_tokens as-is; the AC-1 assertion drops from 6506 to 3306.
- The AC-7 live test spawned in a bare mkdtemp dir, which codex exec refuses (Not inside a trusted directory), so it could never pass. It now git-inits the temp dir (the driver only ever sees worktrees, so it deliberately does not pass --skip-git-repo-check), and re-parses the captured raw stream in place of the former synthetic-line real-stream test. Verified green against codex 0.154.0 with a real key.

Claude-Session: https://claude.ai/code/session_01LKP1FKm7sK7PEegt7vWhaJ
@admarble
admarble merged commit bf35818 into main Sep 15, 2026
6 checks passed
admarble added a commit that referenced this pull request Sep 15, 2026
…off the 5s unit test budget (#1063)

Follow-up to the #1062 review, where three unrelated test failures rotated through full-suite runs on main:

- doctor.test.ts: two scenario assertions depended on the live npm registry via checkVersionThorough(), so they failed on the day 2.15.1 was published while the branch still said 2.15.0. The module is now partially mocked (importOriginal + a pinned up-to-date result), the same class of fix as #1056's snapshot pin.
- tui/load.test.ts: the first loadTui() paid the cold Ink/React import (~5 s under load) inside a test with the unit project's 5 s testTimeout. The import is now warmed in beforeAll (60 s hookTimeout); loadTui() sets/restores NODE_ENV on every call, so the restore behaviour each test guards is unchanged.
- version-preflight.docs.test.ts: the one async test did an await import() of the manifest/version-check graph inside its 5 s budget. Hoisted to a static import (precedent: version-preflight.test.ts), paid at collection time.

No tolerance was widened; the unit project keeps its 5 s default.

Claude-Session: https://claude.ai/code/session_01LKP1FKm7sK7PEegt7vWhaJ
Entire-Checkpoint: bd730d3b45a1

Co-authored-by: t <[email protected]>
admarble added a commit that referenced this pull request Sep 16, 2026
…nexecuted declared evidence (#1065)

Section 6h (Declared-Evidence Execution) now reads the PR body's Test plan / Verification section: an unchecked '- [ ]' item naming a backtick-quoted command is handled like a declared-evidence AC that was not run — execute it if the environment can, otherwise list it as Executed? No and emit a 6j test_gap with pause_for_human naming what the human must supply.

Motivating case: PR #1062's one unchecked box ('CODEX_LIVE=1 npx vitest run ...codex.live...', annotated 'not run here; reports SKIPPED') hid a test that could not pass in any environment (non-git temp cwd the CLI refuses). QA marked the AC MET from the box's own wording; the owner's second look with a real binary found it.

Mirrored to templates/skills and skills/ via check-skill-sync --fix; skill-gates, skill-calls, skill-sync, constitution-dod lints clean.

Claude-Session: https://claude.ai/code/session_01LKP1FKm7sK7PEegt7vWhaJ
Entire-Checkpoint: a36c796c63ec

Co-authored-by: t <[email protected]>
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.

feat(#497): CodexDriver core — subprocess codex exec --json, SDK-typed parser, three-level outcome evaluator

1 participant