Repository navigation
fix(codex): use monotonic clock for retained thread eviction deadline - #168771
Conversation
Seed, consume, and compare the retained-live-thread eviction deadline in the monotonic performance.now() clock domain instead of the wall clock. A wall-clock jump between the deadline seed (retain/claim/release/protect) and the eviction timer's remaining-delay computation can stretch the idle window past its 30-minute intent (idle memory leak) or collapse it to zero (active thread wrongly evicted). performance.now() is unaffected by clock adjustments and is safe here because expiresAt is a purely in-process deadline (0 sqlite/bridge/worker persistence hits). Also floor the setTimeout delay to keep the fractional performance.now() reading integral, per the repo convention for monotonic timers.
|
🦞👀 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 measures Codex idle-thread retention with a monotonic clock so system-clock adjustments cannot change its duration. Example: The system clock moves forward 90 seconds between retaining an idle thread and scheduling eviction.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR fixes a current clock-skew defect with a focused change; no actionable code defects remain, and the updated transport evidence addresses the earlier proof gap. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe Codex plugin's physical-client runtime retains idle native subscriptions, schedules their expiration, and releases them through thread/unsubscribe while excluding claimed threads and protected parents. flowchart TD
A[Completed Codex turn] --> B[Physical client runtime]
B --> C[Idle thread registry]
C --> D[Monotonic eviction timer]
D --> E[Expired idle thread release]
E --> F[Codex app server]
F --> G[Unsubscribe acknowledgment]
Technical reviewBest possible solution: Keep the existing thread owner and retention policy, using monotonic elapsed time consistently for every idle deadline. Do we have a high-confidence way to reproduce the issue? Current-main source exposes the mismatched elapsed-time behavior directly, and contributor terminal evidence records the clock-step failure; no target code was executed during this read-only review. Is this the best way to solve the issue? The patch repairs the existing owner with a complete clock-domain substitution, preserving active-thread protection, ephemeral retention, retry behavior, and the 30-minute policy. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against ebdc7d54c084. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource 0, Tests +4. Total +4 across 2 files. Added test files: 0. View PR surface stats
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 (3 earlier review cycles)
Reviewed October 11, 2026, 10:47 AM ET / 14:47 UTC (Revision 4). |
|
🦞🧹 Reason: re-review requires an open issue or PR. |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Landed in 7aeeaa3. Thanks @SunnyShu0925, Codex idle threads no longer follow wall-clock jumps. |
…lock (#169908) Crabbox warm-image cleanup measured its subprocess time budget with wall-clock time, so a wall-clock correction exhausted the budget early after a forward jump, or let cleanup overrun after a backward jump. The budget now follows monotonic elapsed time, matching #168771, and persisted image timestamps are unchanged. Proof: an isolated Gateway with the Crabbox extension, a fake crabbox CLI and wall-clock jumps simulated inside the process only. Normal cleanup took about 4s on both. With +2h, main's first sweep exhausted its budget after about 2s, while the PR completed in 4.3s. With -2h, main ran 67.2s, while the PR stopped at the 60s budget. A slow no-jump control stopped at about 60s on both. Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]>
…oss wall-clock skew (#168731) The Codex inference WebSocket handshake deadline used wall-clock time, so a wall-clock jump failed an admitted handshake almost immediately after a forward jump, or let it hang past its 10-second budget after a backward jump. The handshake deadline and its callback checks now use monotonic elapsed time, matching #168771 and #169908. Persisted state is unchanged. The queued-admission permit deadline still uses wall-clock time; that is noted as a follow-up. Proof: an isolated built Gateway running the native Codex runtime against the repository's fake app-server, with a loopback TLS WebSocket upstream and clock jumps simulated inside the inference and permit code only. Normal open: 12 ms on main, 11 ms with the PR. +2h: HTTP 504 after 6 ms on main, after 10,037 ms with the PR. -2h: still pending at 15,018 ms on main, 504 at 10,075 ms with the PR. A slow upstream with no jump timed out at about 10 s on both. Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]>
…openclaw#168771) Idle native Codex threads used wall-clock time for their 30-minute idle deadline, so a wall-clock correction (an NTP step or a sleep/wake adjustment) evicted a thread early after a forward jump, or kept it past its TTL after a backward jump. The idle deadline now uses process-local monotonic elapsed time. The TTL is unchanged, and no persisted timestamp changes. Proof: an isolated Gateway driving the native Codex runtime against the repository's fake app-server over real stdio, with wall-clock jumps simulated inside the process only. On main a +2h jump evicted the thread early and a -2h jump kept it past the TTL; with the PR both followed elapsed time, and the no-jump control expired at the TTL on both. Related, not fixed here: openclaw#169829. Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]>
…lock (openclaw#169908) Crabbox warm-image cleanup measured its subprocess time budget with wall-clock time, so a wall-clock correction exhausted the budget early after a forward jump, or let cleanup overrun after a backward jump. The budget now follows monotonic elapsed time, matching openclaw#168771, and persisted image timestamps are unchanged. Proof: an isolated Gateway with the Crabbox extension, a fake crabbox CLI and wall-clock jumps simulated inside the process only. Normal cleanup took about 4s on both. With +2h, main's first sweep exhausted its budget after about 2s, while the PR completed in 4.3s. With -2h, main ran 67.2s, while the PR stopped at the 60s budget. A slow no-jump control stopped at about 60s on both. Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]>
…oss wall-clock skew (openclaw#168731) The Codex inference WebSocket handshake deadline used wall-clock time, so a wall-clock jump failed an admitted handshake almost immediately after a forward jump, or let it hang past its 10-second budget after a backward jump. The handshake deadline and its callback checks now use monotonic elapsed time, matching openclaw#168771 and openclaw#169908. Persisted state is unchanged. The queued-admission permit deadline still uses wall-clock time; that is noted as a follow-up. Proof: an isolated built Gateway running the native Codex runtime against the repository's fake app-server, with a loopback TLS WebSocket upstream and clock jumps simulated inside the inference and permit code only. Normal open: 12 ms on main, 11 ms with the PR. +2h: HTTP 504 after 6 ms on main, after 10,037 ms with the PR. -2h: still pending at 15,018 ms on main, 504 at 10,075 ms with the PR. A slow upstream with no jump timed out at about 10 s on both. Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]>
…openclaw#168771) Idle native Codex threads used wall-clock time for their 30-minute idle deadline, so a wall-clock correction (an NTP step or a sleep/wake adjustment) evicted a thread early after a forward jump, or kept it past its TTL after a backward jump. The idle deadline now uses process-local monotonic elapsed time. The TTL is unchanged, and no persisted timestamp changes. Proof: an isolated Gateway driving the native Codex runtime against the repository's fake app-server over real stdio, with wall-clock jumps simulated inside the process only. On main a +2h jump evicted the thread early and a -2h jump kept it past the TTL; with the PR both followed elapsed time, and the no-jump control expired at the TTL on both. Related, not fixed here: openclaw#169829. Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 7aeeaa3) Co-authored-by: RomneyDa <[email protected]>
…oss wall-clock skew (openclaw#168731) The Codex inference WebSocket handshake deadline used wall-clock time, so a wall-clock jump failed an admitted handshake almost immediately after a forward jump, or let it hang past its 10-second budget after a backward jump. The handshake deadline and its callback checks now use monotonic elapsed time, matching openclaw#168771 and openclaw#169908. Persisted state is unchanged. The queued-admission permit deadline still uses wall-clock time; that is noted as a follow-up. Proof: an isolated built Gateway running the native Codex runtime against the repository's fake app-server, with a loopback TLS WebSocket upstream and clock jumps simulated inside the inference and permit code only. Normal open: 12 ms on main, 11 ms with the PR. +2h: HTTP 504 after 6 ms on main, after 10,037 ms with the PR. -2h: still pending at 15,018 ms on main, 504 at 10,075 ms with the PR. A slow upstream with no jump timed out at about 10 s on both. Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 805f43b) Co-authored-by: RomneyDa <[email protected]>
What Problem This Solves
Retained codex idle live-thread eviction can expire an idle warm session earlier or later than the intended 30-minute window when the wall clock jumps between the expiry seed and the eviction timer's remaining-delay computation. The fix moves the retained-thread eviction deadline onto the monotonic
performance.now()clock so a clock adjustment can no longer stretch the idle window (idle memory leak on rewind) or collapse it to zero (idle thread expired early on forward jump).expiresAtis seeded, read, and compared with the wall clock (Date.now()). A wall-clock jump between the seed and the eviction timer'sMath.max(0, expiresAt - Date.now())remaining-delay computation inflates the idle window (leak on rewind) or shrinks it to zero (idle thread expired early on forward jump).performance.now()clock domain, andMath.floorthe fractional reading before feedingsetTimeout.extensions/codex/src/app-server/client-runtime.ts(4 seed sites, 1 eviction-timer delay, 1 evict comparison) and its test file (3 eviction-boundary tests now driveperformance.now()via a spy).expiresAt; all public signatures and failure/behavior contracts are unchanged. Only otherwise-idle retained threads are governed by this deadline — active claimed threads are removed fromretainedThreads, and native-child parents are excluded from expiration, on both base and head.Why This Change Was Made
The retained-live-thread eviction deadline is entirely in-process:
expiresAtis never persisted, never crosses a process boundary, and has zero sqlite/bridge/worker hits. That makes the monotonic clock the correct clock for this deadline, matching the repo's established convention for in-process idle timers.Date.now()→performance.now()substitutions plus aMath.flooron thesetTimeoutdelay are the minimal, absorbed change. No guard branches were added.performance.now()via a spy to preserve their assertion contract.User Impact
Idle codex warm sessions no longer expire earlier (forward clock jump) or linger past their 30-minute intent (rewind) when the machine clock is adjusted. The 30-minute idle retention window now reflects true elapsed time regardless of clock adjustments; active claimed sessions are unaffected by this deadline on both base and head.
Evidence
Real codex transport (shipped client-runtime + real codex app-server)
Spawns a real
codex app-serversubprocess over real stdio JSON-RPC, attaches the shippedclient-runtime.ts, and drives the retention flow with real elapsed time;performance.now()is never faked. OnlyDate.now()is skewed ±90s between seed and delay computation:Date.now())performance.now())Real elapsed-time eviction
With the idle timeout temporarily shortened to 3s (local proof run, source restored), a real retained idle thread survives before its real elapsed deadline, then is evicted and issued a real, acknowledged
thread/unsubscribe:Pre-fix RED / Post-fix GREEN (deterministic assertion)
Reverted to the
Date.now()version, kept the proof test (counter model rewinds the wall clock 90s), and ran the eviction-delay assertion:Existing test suite
Full
client-runtime.test.ts(27 tests, incl. the 3 eviction-boundary tests) passes; related fake-timer suites stay green (run-attempt.native-followup-custody4,compact.cancellation6,run-attempt-native-process-authority13).What was not tested
Real NTP/step-change under long-running production load was not exercised; the trace applies a deterministic +90s step with a real monotonic clock.
Math.floorrounds sub-ms differences down, the intended convention for monotonic timers feedingsetTimeout.Maintainer provenance
No overlap with Pash/Sarah changes: neither has authored commits on the two changed files in the inspected main history. Thanks @SunnyShu0925 for the fix and original transport evidence.
Issue search found no exact open report for Codex idle-thread clock skew. Related but not resolved by this change: #169829 covers a separate wall-clock rewind failure in Gateway state-ownership heartbeats.
Additional Gateway proof
Built and exercised baseline
d2af38cb258982dcd598e49f31616c7f9450f39aand candidate08ca9eea4e451256a3bfc780989fa2c75abd7c68in separate isolated containers. Authenticatedchat.sendrequests ran the native Codex harness against the repository fake app-server over real stdio. After completing the first turn, a second user turn refreshed ownership scheduling while the first native thread was idle. Observations are actualthread/unsubscriberequests, not deadline calculations.All six Gateway cases passed their discriminating assertions. Time was advanced with an in-process preload scoped to the Codex retention owner's clock reads and timer scheduling; unrelated Gateway clocks remained real. The configured 1,800,000 ms TTL and product source were unchanged. This is deterministic process-local fault injection, not a natural NTP or suspend/resume test.
On the unchanged candidate head, the 27 runtime tests passed (33.29 seconds including the runner), and targeted Oxfmt/Oxlint checks passed for both changed files. A separate bounded real-provider CLI request to
gpt-5-minicompleted with the requested exact reply in one provider request. Its credential file was deleted after the run.Merge danger: two-way door; reverting fully restores prior in-memory timing behavior. No stored timestamps, schema or config migrate. Blast radius is idle retained native Codex subscriptions; configured 30-minute retention, active claims and protected native-child parents retain their existing policy.