Skip to content

refactor(codex): split app server run attempt - #106773

Merged
steipete merged 3 commits into
mainfrom
codex/refactor-codex-run-attempt-deep
Jul 13, 2026
Merged

steipete merged 3 commits into
mainfrom
codex/refactor-codex-run-attempt-deep

Conversation

@steipete

@steipete steipete commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

extensions/codex/src/app-server/run-attempt.ts had grown to 4,137 lines and mixed connection setup, prompt preparation, routing, turn lifecycle, notification handling, finalization, and cleanup in one module. This made ownership and review boundaries hard to follow. Tracks #106503.

Why This Change Was Made

Split the runtime by lifecycle phase and owner boundary. The entry point is now an 81-line orchestrator; every extracted production module is 480 lines or fewer. Existing Codex app-server protocol behavior, public surfaces, and runtime policy remain unchanged.

Review found and fixed three failure-path regressions: deferred cancellation snapshot timing, atomic context-projection metadata, and immediate ownership of startup cleanup handles.

User Impact

No intended user-visible behavior change. Maintainers get smaller modules with explicit startup, active-turn, notification, finalization, and cleanup boundaries.

Evidence

  • Exact head: 98650a37030dd588a80c2d75e563c034b3ae44ba
  • Blacksmith Testbox: tbx_01kxerd15gtzxtprmvnmke7ggm
  • Testbox workflow run: https://github.com/openclaw/openclaw/actions/runs/29288894688
  • Focused oxfmt --check and oxlint passed for run-attempt.ts and run-attempt-*.ts.
  • corepack pnpm tsgo:extensions
  • 10 focused run-attempt*.test.ts files — 301 tests passed
  • corepack pnpm deadcode:exports reports only current main baseline drift outside this branch.
  • Three size-bounded autoreview ownership bundles plus focused fix reruns; no accepted/actionable findings remain.
  • git diff --check
  • Upstream Codex contract checked in codex-rs/app-server-protocol/src/protocol/common.rs, codex-rs/app-server/src/bespoke_event_handling.rs, and codex-rs/app-server/src/request_processors/turn_processor.rs.

@steipete steipete self-assigned this Jul 13, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Land-ready proof for 4a31af44a31fd5c18e8a4bd91abb0a42564e7424:

  • run-attempt.ts: 4,137 -> 81 lines; every extracted production module is <=480 lines.
  • Blacksmith Testbox tbx_01kxegyk1se1k7sdvyc68brg6m: pnpm tsgo:extensions passed.
  • Same exact head: all 10 focused run-attempt*.test.ts files passed, 301 tests total.
  • git diff --check passed.
  • Autoreview found one cancellation-timing regression; fixed by restoring the deferred final abort snapshot. Fresh autoreview then returned no actionable findings.
  • No intended user-visible behavior, protocol, config, docs, or changelog change.

Required hosted landing checks remain enforced by scripts/pr prepare-run.

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

Copy link
Copy Markdown
Contributor

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: 4a31af44a3

ℹ️ 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".

const drainNotificationQueue = async () => {
await resourceState.turnRoute?.drain();
};
registerNativeSubagentMonitor(resourceState.thread.threadId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge Keep monitor registration under route cleanup

When codexNativeSubagentMonitorRuntime.register throws (for example, an existing monitor has the same parent thread bound to a different requester session), this eager registration happens after startCodexAttemptRuntime has acquired the app-server client/route/native hook relay/sandbox resources but before prepareCodexAttemptRoute enters its cleanup try. The previous inlined flow registered inside ensureCurrentThreadRoute's guarded block, so the route, lease, relay, sandbox exec environment, and upstream abort listener were released on this failure; now this path leaks them and leaves the attempt partially attached. Move this registration back under the route cleanup guard or wrap it with equivalent cleanup.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 13, 2026
@clawsweeper

clawsweeper Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 13, 2026, 4:58 PM ET / 20:58 UTC.

Summary
Splits the Codex app-server run-attempt monolith into lifecycle-focused connection, prompt, routing, turn, notification, finalization, and cleanup modules behind a small orchestrator.

PR surface: Source +755. Total +755 across 19 files.

Reproducibility: no. live failure was reproduced, but the exception path is source-reproducible with high confidence: a throwing monitor registration occurs after resource acquisition and before the route cleanup guard.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: persistent cache schema: extensions/codex/src/app-server/run-attempt-active-turn.ts, persistent cache schema: extensions/codex/src/app-server/run-attempt-turn-request.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #106503
Summary: This PR is the Codex-specific implementation candidate for one module identified by the canonical oversized-runtime refactor issue.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🦐 gold shrimp
Proof: 🌊 off-meta tidepool
Patch quality: 🦐 gold shrimp
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Move monitor registration under the route cleanup owner and add a regression test where registration throws.
  • Refresh the branch onto current main and rerun the focused Codex attempt, extension type, and runtime topology checks.

Risk before merge

  • [P1] If native subagent monitor registration throws, the attempt can retain an app-server route, shared-client lease, native relay, sandbox environment, and upstream abort listener, leaving the runtime partially attached and potentially degrading later Codex attempts.
  • [P1] The branch is behind current main and several required checks on the recorded head are failing or incomplete, so the repaired head needs a fresh merge-base review and exact-head validation before landing.

Maintainer options:

  1. Restore guarded registration (recommended)
    Move monitor registration under the route cleanup guard, add a throwing-registration regression test, and rerun exact-head Codex validation before merge.
  2. Pause the refactor
    Keep the PR open but do not merge until resource ownership and current-main drift are resolved on a refreshed head.

Next step before merge

  • [P1] The repair is narrow, but the protected maintainer label requires explicit maintainer handling rather than ClawSweeper work-lane promotion.

Security
Cleared: The refactor adds no dependency, workflow, secret, permission, package-resolution, or downloaded-code changes, and no concrete security or supply-chain regression was found.

Review findings

  • [P2] Keep monitor registration inside the cleanup guard — extensions/codex/src/app-server/run-attempt.ts:47
Review details

Best possible solution:

Keep the lifecycle split, but place native subagent monitor registration inside the route-owned cleanup guard or add equivalent exception cleanup, cover the throwing registration path with a focused regression test, then refresh onto current main and rerun the Codex attempt and architecture checks.

Do we have a high-confidence way to reproduce the issue?

No live failure was reproduced, but the exception path is source-reproducible with high confidence: a throwing monitor registration occurs after resource acquisition and before the route cleanup guard.

Is this the best way to solve the issue?

No, not at the current head. The owner-aligned extraction is the right direction, but registration must remain within the cleanup ownership boundary so the refactor preserves prior failure semantics.

Full review comments:

  • [P2] Keep monitor registration inside the cleanup guard — extensions/codex/src/app-server/run-attempt.ts:47
    registerNativeSubagentMonitor can throw after the app-server route, shared-client lease, native relay, sandbox environment, and abort listener have been acquired, but this call now occurs before prepareCodexAttemptRoute establishes its cleanup guard. Preserve the previous failure semantics by registering inside that guard or performing equivalent cleanup on registration failure; otherwise the attempt remains partially attached and leaks resources.
    Confidence: 0.98

Overall correctness: patch is incorrect
Overall confidence: 0.97

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 995c75baa00e.

Label changes

Label changes:

  • add P2: This is a useful behavior-preserving cleanup with a concrete but bounded exception-path resource leak that should block merge.
  • add merge-risk: 🚨 availability: The changed registration order can leak runtime resources when monitor registration throws, potentially stalling or degrading subsequent Codex attempts.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: The maintainer-labeled PR is a behavior-neutral internal refactor with exact-head hosted focused tests; real user-facing demonstration is not required, though the exception-path defect still needs regression coverage.

Label justifications:

  • P2: This is a useful behavior-preserving cleanup with a concrete but bounded exception-path resource leak that should block merge.
  • merge-risk: 🚨 availability: The changed registration order can leak runtime resources when monitor registration throws, potentially stalling or degrading subsequent Codex attempts.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: The maintainer-labeled PR is a behavior-neutral internal refactor with exact-head hosted focused tests; real user-facing demonstration is not required, though the exception-path defect still needs regression coverage.
Evidence reviewed

PR surface:

Source +755. Total +755 across 19 files.

View PR surface stats
Area Files Added Removed Net
Source 19 4470 3715 +755
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 19 4470 3715 +755

What I checked:

  • Cleanup regression: The current head eagerly registers the native subagent monitor after acquiring app-server route, lease, relay, sandbox, and abort resources but before entering the route cleanup guard; if registration throws, those resources are not released. This matches the actionable review at refactor(codex): split app server run attempt #106773 (comment). (extensions/codex/src/app-server/run-attempt.ts:47, 4a31af44a31f)
  • Cleanup ownership: The extracted route module owns route, shared-client lease, native relay, sandbox environment, and abort-listener cleanup, so monitor registration must occur within that guarded ownership boundary or receive equivalent failure cleanup. (extensions/codex/src/app-server/run-attempt-route.ts:10, 4a31af44a31f)
  • Current main still needs the refactor: Current main retains the oversized single run-attempt implementation, so the PR's central maintainability work has not been superseded or implemented on main. (extensions/codex/src/app-server/run-attempt.ts:1, 995c75baa00e)
  • Focused validation supplied: The PR reports exact-head Blacksmith Testbox extension typechecking, 301 focused run-attempt tests, git diff validation, and a clean final autoreview after an earlier cancellation-timing repair; these are useful supplemental checks but do not disprove the remaining exception-cleanup defect. (extensions/codex/src/app-server/run-attempt.ts:1, 4a31af44a31f)
  • Upstream Codex lifecycle contract: Upstream Codex processes turn start and completion as distinct app-server lifecycle phases; the inspected contract surfaces support retaining OpenClaw's explicit start, active-turn, finalization, and cleanup boundaries without changing protocol semantics. (codex-rs/app-server/src/request_processors/turn_processor.rs:2515)
  • Upstream Codex notification contract: The upstream bespoke event handling and common protocol definitions were inspected for turn notification and completion behavior; no upstream contract requires the leaking registration order introduced by this refactor. (codex-rs/app-server/src/bespoke_event_handling.rs:1)

Likely related people:

  • steipete: Introduced the broader Codex app-server integration on current main and authored recent lifecycle and diagnostics work in the same runtime surface, beyond proposing this PR. (role: recent area contributor; confidence: high; commits: b4fdd1470b70, 4a31af44a31f; files: extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/run-attempt-route.ts, extensions/codex/src/app-server/run-attempt-resources.ts)
  • funmerlin: Recent merged work adjusted Codex app-server lifecycle and diagnostic behavior in the same run-attempt path. (role: recent area contributor; confidence: medium; commits: 127156a88a29; files: extensions/codex/src/app-server/run-attempt.ts)
  • joshavant: Recent merged Codex app-server telemetry and runtime work touched the same central attempt execution surface. (role: adjacent owner; confidence: medium; commits: 5fdef4c39e75; files: extensions/codex/src/app-server/run-attempt.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete force-pushed the codex/refactor-codex-run-attempt-deep branch from 4a31af4 to 98650a3 Compare July 13, 2026 22:22
@steipete

Copy link
Copy Markdown
Contributor Author

Land-ready proof for exact head 98650a37030dd588a80c2d75e563c034b3ae44ba:

The dead-export check reports only current main drift outside this branch. Upstream Codex protocol/runtime sources were checked directly. Hosted required CI is pending.

@steipete
steipete merged commit 5c9e09a into main Jul 13, 2026
103 checks passed
@steipete
steipete deleted the codex/refactor-codex-run-attempt-deep branch July 13, 2026 22:33
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: codex maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XL status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant