Repository navigation
fix(ai): release completed runs held by cached WebSockets - #174752
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 SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex review: needs maintainer review before merge. What this changesDetach cached OpenAI WebSockets and expiry timers from completed runs so their execution contexts can be collected. Example: Complete 24 concurrent runs while keeping their WebSockets cached.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR addresses a concrete retention bug still present on main; the test correction resolves the previous finding, and no actionable patch defects remain. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe AI provider transports turn authenticated model requests into WebSocket streams and retain session-owned connections, continuation state, and cooldowns between requests. flowchart LR
A[Agent run] --> B[Provider transport]
B --> C[Captured transport host]
C --> D[Detached resource creation]
D --> E[Cached WebSocket]
D --> F[Expiry and cooldown timers]
E --> G[Provider response stream]
Technical reviewBest possible solution: Retain reusable connections under their existing runtime owners while severing completed-request async and lexical retention. Do we have a high-confidence way to reproduce the issue? Current-main source exposes both async-context and cooldown-closure retention paths, supported by reported baseline-failing regressions and Gateway snapshots; this read-only review executed no target code. Is this the best way to solve the issue? The patch repairs resource creation at both transport owners using the existing host capability and preserves the original cache, fallback, and cleanup contracts. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 297448e524f0. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
PR surfaceSource +24, Tests +156, Docs +1. Total +181 across 6 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes:
Label justifications:
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 (1 earlier review cycle)
Reviewed October 16, 2026, 6:45 PM ET / 22:45 UTC (Revision 2). |
|
@clawsweeper re-review The branch and PR evidence are current. The test-only follow-up fixes the Node rejection-matcher retention artifact and preserves the fixture's literal role type. Independent review is clean, and the PR body now includes completed before/after measurements, heap-snapshot attribution, repeated-cohort results, and the separately reproduced baseline LRU failure. |
|
🦞👀 Re-review progress:
|
|
Merged reviewed head CI run 38976694995 completed with one failing test job: checks-node-changed-compact-small-2. Its All other selected test/static-check leaves, UI checks, The earlier cooldown-test review finding was repaired without weakening the GC or cooldown assertions; its exact-head CI job now passes. The fixture type correction also passes CI. Independent review of the production change and the follow-up test correction found no actionable P0–P2 findings. The PR body records the completed memory proof: post-GC heap residue 77.77 → 38.82 MiB, finished session-owner counts 25 → 1, and repeated 24-run cohorts settling at 419.657 → 419.588 MiB with cached sockets still open. RSS and the remaining attribution limits are stated separately. |
…74752) * fix(ai): release completed runs held by cached WebSockets * test(ai): avoid fixture retention in WebSocket GC checks
What Problem This Solves
Completed OpenAI runs remain reachable through cached WebSocket native resources and failure-cooldown timers until those resources expire. A connection created inside a run inherits its async context, which can keep the session, runtime lease, and model catalog graph alive after completion.
User Impact
Completed execution contexts can be collected while idle WebSockets remain reusable. Cached continuation, transport fallback, and the existing five-minute idle expiry keep their behavior; no configuration or migration is required.
Why This Change Was Made
Socket creation and cache expiry use the existing transport host's detached async context. The generic Responses cooldown callback moves out of the request factory's closure, removing its reference to the failed request and restore callback. Connection resources retain their intended runtime lifetime without inheriting the completed caller's lifetime.
Production code grows by 24 lines (+65/-41): preserving connection reuse and cooldown semantics requires detaching both native sockets and timers and isolating the cooldown callback's lexical scope. This removes the old request-scoped timer closure rather than adding a parallel path. No new cache, manager, API, compatibility layer, dependency, or schema is introduced.
Measurements
Matched before/after measurements used an isolated Linux Gateway with synthetic state: 5,000 sessions, 6,000 retained runs, 200 approvals, 100 disabled cron jobs, and 12 projects. Each cohort used distinct existing workspace checkouts, real Code Mode file reads, a loopback cached-WebSocket provider, verified concurrent responses, completion, archive, and a ten-second settle. Active sessions began with empty histories. One completed/archived run warmed the process before measurement.
MiB; active growth is
(active - created) / runs. Heap is the main isolate after forced GC. RSS includes process workers, excludes child processes, and comes from the pass without heap snapshots; phase GC still affects later RSS samples.Across all 33 runs, final post-GC heap growth over the warmed baseline fell from 77.77 to 38.82 MiB. Final RSS was 2,073.25 → 2,081.73 MiB, essentially neutral in these single passes; differing warmed baselines and GC/allocator timing prevent claiming a comparable RSS reduction. Both passes retained 34 open sockets, made 68 WebSocket requests, and used 34 cached continuations: the heap reduction does not come from closing connections.
A separate 24-run snapshot pass isolates reachability. Active post-GC heap was essentially unchanged (442.84 → 442.73 MiB), but completed heap fell from 429.85 to 398.69 MiB and warmed-to-settled heap growth fell from 61.40 to 30.31 MiB. Snapshot-pass RSS is excluded because taking snapshots inflates it.
Baseline snapshots show a native TCP → AsyncContextFrame → run owner → runtime lease/catalog path. The fix returns session-owner counts to baseline while retaining the sockets. Remaining registry paths are associated with prepared-plugin snapshots; WeakMap ephemeron paths alone do not establish that they are leaked. Shared retained subtrees must not be added together as independent per-run memory.
A follow-up fixed-build pass ran two consecutive 24-run cohorts using the same workspace pool. In the second cohort, post-GC heap was 417.07 MiB at idle and 419.59 MiB after settle (+2.52 MiB total, 0.105 MiB/run); the final sample was 418.24 MiB. Comparing settled samples directly, the first cohort ended at 419.66 MiB and the second at 419.59 MiB (−0.07 MiB). RSS fell from 2,204.41 to 2,096.35 MiB. Open sockets increased from 25 to 49 without expiry, and all 48 measured runs completed and archived. This supports a plateau after workspace warmup, with small residual drift, rather than accumulation of one retained session graph per completed run. The existing runtime deliberately retains up to eight idle Gateway run owners; not every surviving registry was attributed to that cache.
An SSE control was neutral: at 24 runs, active RSS/run was 10.86 → 10.47 MiB and idle-to-settled RSS growth was 60.02 → 58.65 MiB. These measurements establish completed-run retention reduction, not a general active-run RSS improvement or full return of total memory to baseline. Long active transcripts and worker-isolate heap attribution remain outside this fixture's coverage.
Evidence
Three retention regressions fail on unchanged production code: the generic transport retains its completed caller; its cooldown retains the failed caller, request, and restore callback; and a real loopback ChatGPT WebSocket retains its completed caller. They pass with the fix while verifying continuation reuse or live cooldown behavior.
assert.rejectspreserves the error and GC assertions. The corrected transport file passes all 13 tests under CI's Node/unit-support configuration; unchanged production still fails both retention tests (11 pass, 2 expected failures).role: stringin the shared test fixture. Preserving the literal"user"type fixes the restore callback contract without changing runtime behavior.The first CI run also failed
src/plugins/runtime/runtime-llm.prepared-owner.test.tsinkeeps completion ownership coherent: lru(expected 3 to be 2, line 923). The exact file reproduces identically on this PR's production code and baselineb921f805a3e535642d29c232c22ece7c831cb5a1: 1 failed, 24 passed in both runs. The fixture usesopenai-completionsSSE, outside the changed WebSocket branches; its owner-reuse assertion passes, while background catalog publication adds an existing registry fork. Relevant test and fork-producing ownership source is unchanged through CI main parent87c1f1c673b136b23c374596bc718e317b8810bd; inspected indirect changes do not alter the assertion or extra fork. This is an inherited failure; no CI rerun was requested to chase green.On corrected head
02657d8c7375938d9d88340392f080e0c0666645, the WebSocket test job and the previously failing test-type stripe pass. All other test/static-check leaves pass; the inherited LRU job reproduces the same single assertion. Its owner-reuse counters remainfirstBuilds=2,buildsBeforeSecond=11,secondBuilds=11, matching the local baseline and head. The earlier PR-context failure was a section heading mismatch (Proofversus requiredEvidence); the corrected body passes that check.