Repository navigation
fix(codex): discover on demand and retire unowned clients - #168387
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesThis PR defers native model discovery until demand and retires Codex clients after 30 seconds without owned work. Example: A Gateway has 17 agents, including 15 using Codex.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this PR open: both prior findings are addressed, the resource-retention fix remains necessary, and no new actionable defect was found; current-head CI is still pending. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherGateway catalogue requests enter the prepared model owner, which combines provider inventory with native harness observations and uses the Codex shared-client owner to acquire and retire physical clients. flowchart TD
A[Gateway startup] --> B[Provider inventory]
C[Picker or native execution] --> D[Prepared model owner]
B --> D
D --> E[Codex shared client]
E --> F[Native model and account observations]
F --> D
E --> G[Ownership aware idle retirement]
Technical reviewBest possible solution: Land the existing demand-driven discovery and ownership-aware retirement implementation once the normal merge gates pass. Do we have a high-confidence way to reproduce the issue? Current-main source contains startup native acquisition and no general idle-retirement timer, consistent with the reported multi-agent Gateway observations; this read-only review ran no target code. Is this the best way to solve the issue? The patch repairs discovery demand and client lifetime in their existing owners while retaining provider publication, warm subscriptions, and execution error contracts. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against e09de50421ef. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +161, Tests +642, Docs +21, Other -2. Total +822 across 36 files. Added test files: 2. View PR surface stats
LabelsLabel changes:
Label justifications:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (2 earlier review cycles)
Reviewed October 10, 2026, 8:33 AM ET / 12:33 UTC (Revision 3). |
Merge main while retaining discovery-only provider reacquisition and demand-driven native catalogs. Ordinary inventory reads retain published rows and refreshFailed when optional native hooks reject; selected execution and explicit refresh still reject. Add handler-boundary coverage for saved rows, owner supersession, and authorization invalidation.
Main now reacquires discovery-only providers after credential invalidation. Serve current empty discovery auth, join that acquisition, and assert one additional request while retaining the no-rediscovery checks for compatible reloads. The prior expectation fails with exact merged-main production as well.
Record rejected native observations in the existing runtime outcome map. Preserve healthy runtime readiness checks after unrelated failures, including runtimes sharing a provider. Publish failed observations before retaining selected and explicit-refresh rejection. Add failing-before handler regressions for unscoped and shared-provider failures.
Only unscoped native acquisition completes global inventory readiness. Check outcome ownership before indexing arbitrary runtime IDs. Handler regressions prove an initial provider-scoped refresh still acquires the remaining runtime and inherited property names cannot break picker reads.
|
@clawsweeper re-review Both findings from the review of |
|
🦞👀 Re-review progress:
|
|
@clawsweeper approve Maintainer decision (Peter, 2026-10-09, in chat): Demand-driven Codex discovery - no boot spawns for unused agents, lazy discovery on first use, retire clients that own no work after a short grace; first use pays the cold start. The completed review of |
|
🦞👀 Command router queued. I will update this comment with the next step. |
Demand-driven discovery in #168387 leaves configured model hints with unknown native readiness until discovery supplies an observation. Check that this state does not renew, then supply the observed native variant before exercising the existing exact renewal and revocation assertions. Preserve every original case and assertion. Production discovery behavior is unchanged.
…68387) * fix(codex): discover on demand and retire unowned clients * test(models): align reload proof with provider reacquisition Main now reacquires discovery-only providers after credential invalidation. Serve current empty discovery auth, join that acquisition, and assert one additional request while retaining the no-rediscovery checks for compatible reloads. The prior expectation fails with exact merged-main production as well. * fix(models): scope native discovery failures to their runtime Record rejected native observations in the existing runtime outcome map. Preserve healthy runtime readiness checks after unrelated failures, including runtimes sharing a provider. Publish failed observations before retaining selected and explicit-refresh rejection. Add failing-before handler regressions for unscoped and shared-provider failures. * fix(models): preserve native inventory scope and runtime keys Only unscoped native acquisition completes global inventory readiness. Check outcome ownership before indexing arbitrary runtime IDs. Handler regressions prove an initial provider-scoped refresh still acquires the remaining runtime and inherited property names cannot break picker reads. * test(codex): align catalog fixtures with demand discovery
Demand-driven discovery in openclaw#168387 leaves configured model hints with unknown native readiness until discovery supplies an observation. Check that this state does not renew, then supply the observed native variant before exercising the existing exact renewal and revocation assertions. Preserve every original case and assertion. Production discovery behavior is unchanged.
… refresh Align the scoped-refresh case with the #168315 backport, which reacquires discovered providers a config rebuild cannot retain. Main carries the same case as "retains compatible discovery and reacquires it after credentials change" (from a825abb); this ports only that test case, not #168387's Codex discovery changes.
The metadata fixture asserted complete plugin discovery after a bounded foreground catalog read, which can legitimately return seeded rows while acquisition remains pending. Use the existing explicit refresh wait contract and retain all metadata, catalog-row, native-auth and single-acquisition assertions. The five-second foreground behavior introduced in #168387 remains unchanged.
The metadata fixture asserted complete plugin discovery after a bounded foreground catalog read, which can legitimately return seeded rows while acquisition remains pending. Use the existing explicit refresh wait contract and retain all metadata, catalog-row, native-auth and single-acquisition assertions. The five-second foreground behavior introduced in openclaw#168387 remains unchanged.
Shared Codex app-server clients stayed resident until Gateway restart once their last lease released, so per-agent discovery and status requests left idle app-server process pairs behind (#165663). A shared client with no active leases, no pending acquires, and no native thread ownership (retained, claimed, releasing, or protected subscriptions) now retires 30 seconds after it goes idle. Lease acquisition and thread ownership changes cancel or reschedule the timer, closing the client clears it, and the next request starts a fresh client through the normal acquisition path. (cherry picked from commit a825abb) Adapted for 2026.9.x: ported only the idle-retirement slice of a825abb (shared-client-lifecycle.ts idle timer, shared-client.ts hasOwnedThreads and ownership-change wiring, client-runtime.ts ownership hook and hasCodexAppServerThreadOwnership, shared-client-idle.test-support.ts). Left out on-demand discovery, the model catalog and prepared-runtime changes, and the harness/gateway changes. 9.x has no closeSharedClientEntryIfUnclaimed, so that cancel site is skipped; closeRetiredSharedClientEntry and the client close handler still clear the timer. The grace stays at a825's 30 seconds instead of main's later 5 minutes (b39a055). The eviction-timer clear in client-runtime.ts uses main's two-line form so the file stays within the 700-line lint limit.
What Problem This Solves
Every agent on the Codex harness got its own Codex app-server at Gateway boot, even if nobody used that agent, and the process stayed resident forever. On a dev Gateway with 18 agents (15 of them on the Codex runtime):
codex app-server scope timed out phase=acquire-client ... requestStartedCount=0), about a minute afterready, with no client request in flight;The cause: startup's changed-only model catalog refresh acquired native inventory (
model/list,account/read) for every configured agent, after the provider concurrency limit. Releasing that discovery request left the client resident, and nothing retired idle shared clients.Why This Change Was Made
Maintainer decision (Peter, 2026-10-09): demand-driven Codex discovery. No boot spawns for unused agents, lazy discovery on first real use, and retirement of clients that own no work. The first use of an idle agent pays the cold start.
src/agents/prepared-model-runtime.catalog-access.ts). The real picker handler (models.list) and catalog reader trigger native discovery for the requested agent only, coalesce concurrent requests, and reacquire after retirement. Until discovery runs, native readiness reports unknown instead of a false negative.extensions/codex/src/app-server/shared-client-lifecycle.tsretires a shared client after a 30 s grace (about 6× the measured worst cold start of 5.4 s) once it holds no leases or pending acquisitions and owns no live thread, turn, native child, background terminal, or ephemeral history (client-runtime.ts).docs/concepts/models.md, Codex harness model-discovery reference) describe the new lifecycle.User Impact
A Gateway with many Codex agents boots without spawning an app-server per agent, so boot-time load drops and the acquisition timeouts are gone. Idle agents stop holding about 100 MiB each. The first picker read or turn for an idle agent pays a cold start of about 1 to 5 s.
Evidence
falseinstead of unknown. With the fix, these pass.acquire-clienttimeouts per boot; 9 resident app-servers about 1.3 GB RSS after 19 idle minutes.models.listfor an idle agent: 5.5 s and 37 models, which spawned 1 app-server. A first turn on another idle agent repliedok(app-server cold start 10.4 s; first output 19 s into the turn on a host at load about 60); resume in the same session repliedagainin 10 s; a repeatmodels.listtook 74 ms.Follow-up after review (head 8917a77)
The first landing attempt stopped on a ClawSweeper P2:
models.listawaited native discovery, so an immediate Codex startup ormodel/listfailure rejected an ordinary picker read even when provider rows were already published. The branch also conflicted with main.origin/mainis merged in, keeping main'slistChangedProviders-based reacquisition of discovery-only providers.f186a7d413ee: an ordinary picker read returns the saved rows withrefreshFailedwhen optional native discovery rejects. Selected execution, explicit refresh, superseded-owner and authorization errors still reject. A handler-boundary regression covers published provider rows with a native hook that rejects before 5 s.d66abd017595and8917a773f7c4fix three readiness defects found in review: native discovery failures are now scoped to their own runtime, partial discovery no longer marks the whole inventory complete, and runtime-key lookups are preserved.CI fixture follow-up (head a5fa979)
ClawSweeper's review of
8917a773f7c4confirmed the optional-discovery failure repair and identified two stale sibling test contracts in CI run 38050401013. This follow-up changes tests only: the metadata lifecycle fixture explicitly acquires native inventory before testing retained observations and after generation replacement; the registered Codex runtime-choice test distinguishes unknown readiness from an observed native row that is unavailable. Pin authority, revocation, full discovered metadata, and passive repeated-read checks remain covered.Both files passed on captured main
e3415ea8c554and reproduced the reviewed-head failures locally before correction. The final focused runs pass all 20 cases: Gateway lifecycle (18 tests, 28.33 s wall) and registered runtime choices (2 tests, 19.25 s wall). Targeted lint, Gateway test types, formatting, andgit diff --checkpass. Independent review of the production diff and the final test changes found no actionable P0–P2 findings. Exact-head CI run 38051919906 passed on attempt 1 (62 successful jobs, 23 skipped, no failures). ClawSweeper completed its exact-head review on October 10 at 12:33 UTC with no findings or before-merge items. Telegram Test Server menu E2E remains an optional proof improvement; the supplied real-provider Gateway proof is sufficient. No rerun or admin exception was used.