Skip to content

fix(crabbox): keep coordinator retry deadline monotonic across clock skew - #165889

Merged
fuller-stack-dev merged 2 commits into
openclaw:mainfrom
SunnyShu0925:fix/crabbox-worker-command-coordinator-retry-deadline-wall-clock
Oct 16, 2026
Merged

fuller-stack-dev merged 2 commits into
openclaw:mainfrom
SunnyShu0925:fix/crabbox-worker-command-coordinator-retry-deadline-wall-clock

Conversation

@SunnyShu0925

@SunnyShu0925 SunnyShu0925 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

When a laptop wakes from sleep or an NTP correction steps the system clock backward, crabbox stop commands hang far longer than their configured timeout, delaying cloud lease reclamation. The retry deadline is seeded with Date.now() and remaining budget is read with Date.now(), then passed to runCommand → setTimeout (monotonic). On wall-clock rewind, remaining = deadline - Date.now() is amplified by the rewind delta, so a lease stop configured for CRABBOX_STOP_TIMEOUT_MS (~16 min) can hang ~18× longer.

  • Solution: Replace all five Date.now() calls with performance.now(), which is monotonic and immune to wall-clock skew.
  • What changed: crabbox-worker-command.ts — 5 calls at L181/196/201 (retry loop) and L285/308 (stop + Azure recovery); crabbox-worker-command.test.ts — spy moves to performance.now, plus a new rewind regression test; crabbox-worker-stop-lifetime.test.ts — Azure recovery spy moves to performance.now, plus a new Azure recovery rewind regression test.
  • What did NOT change: resolveTimerTimeoutMs, CRABBOX_STOP_TIMEOUT_MS, retry/backoff constants, error messages, coordinator-failure classification. No new imports (Node 16+ exposes performance globally). Sibling Date.now() deadline patterns in crabbox-worker-provider.ts, crabbox-worker-warm-image.ts, and crabbox-worker-node-process.ts are out of scope; separate budgets, tracked by fix(crabbox): use monotonic clock for provision deadline budget #162813/fix(crabbox): measure warm-image cleanup deadlines on the monotonic clock #169908.

Why This Change Was Made

The fix puts the deadline seed and remaining-budget read on the same monotonic clock as the setTimeout consumer, so remaining is always bounded by the original budget.

  • Best fix: performance.now() is the canonical monotonic clock in Node and matches the setTimeout consumer's clock domain. Mechanical, line-for-line replacement with no logic change.
  • Alternative: process.hrtime.bigint() is also monotonic but returns nanoseconds and requires conversion.
  • Risk: None. On systems with no clock skew, both clocks advance at the same rate.

User Impact

After a laptop wakes from sleep or an NTP correction steps the clock back, Crabbox lease stop commands now respect the configured timeout and give up on time, so workers and cloud leases are reclaimed within their expected cleanup window.

Evidence

Dispatcher-triggered teardown (running Gateway, real client, shipped dispatcher)

The real Gateway is started via startGatewayServer (shipped server kernel + RPC dispatcher); a real GatewayClient completes the token-authenticated handshake. client.request("environments.destroy") is routed through the running Gateway's shipped dispatcher to the environments.destroy handler, which calls the shipped createCrabboxWorkerProvider factory → provider.destroy(lease) → stopLease() → stopCrabboxLease() → runCrabboxCommandWithCoordinatorRetry(). performance.now() is native (not mocked); Date.now() is rewound 90 s between the deadline seed and the budget read.

$ node --import tsx proof-dispatcher-triggered.mts postfix
[gateway] REAL gateway listening on ws://127.0.0.1:18889
[gateway] REAL client authenticated & connected
[rpc] sending environments.destroy through REAL running Gateway (shipped dispatcher)...
[dispatch] environments.destroy handler invoked through shipped dispatcher!
[results] stop timeoutMs: [1005000, 1003830, 1001759]
VERDICT: PASS — bounded within CRABBOX_STOP_TIMEOUT_MS — max=1005000

$ node --import tsx proof-dispatcher-triggered.mts prefix
[gateway] REAL gateway listening on ws://127.0.0.1:18889
[gateway] REAL client authenticated & connected
[rpc] sending environments.destroy through REAL running Gateway (shipped dispatcher)...
[dispatch] environments.destroy handler invoked through shipped dispatcher!
[results] stop timeoutMs: [1005000, 1095000, 1095000]
VERDICT: BUG REPRODUCED — amplified beyond CRABBOX_STOP_TIMEOUT_MS — max=1095000
pre-fix (Date.now()) post-fix (performance.now())
running Gateway, client.request("environments.destroy") through shipped dispatcher, shipped createCrabboxWorkerProvider → provider.destroy → stopCrabboxLease, 90s rewind max 1 095 000 ms — amplified +90 000 ms beyond CRABBOX_STOP_TIMEOUT_MS (FAIL) max 1 005 000 ms — bounded within CRABBOX_STOP_TIMEOUT_MS (PASS)

Focused test suite (pre-fix fail / post-fix pass)

$ npx vitest run extensions/crabbox/src/crabbox-worker-command.test.ts extensions/crabbox/src/crabbox-worker-stop-lifetime.test.ts
 Tests  62 passed (62)   Duration  28.14s

Includes "keeps the coordinator retry deadline bounded when the wall clock rewinds" (pre-fix fails 94000 > 5000, post-fix passes) and "keeps Azure recovery bounded when the wall clock rewinds" (pre-fix fails with amplified recoveryTimeoutMs, post-fix passes).

What was not tested

  • Full integration with a real Crabbox coordinator endpoint (requires cloud credentials). The proof exercises the deadline math through the shipped provider factory and production runCommandWithTimeout with real child processes; only the Crabbox CLI binary is substituted.
  • Actual NTP step-back in a live process (simulated via Date.now rewind). The proof confirms correct native monotonic behavior with the real Gateway dispatcher; the rewind confirms the bug's credible failure and fix.

@clawsweeper

clawsweeper Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review in progress

ClawSweeper is reviewing this revision. This supersedes any previous blocked status.

View the workflow run.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 6, 2026
@clawsweeper

clawsweeper Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

The PR uses monotonic time for Crabbox coordinator retry and Azure stop-recovery budgets.

Example: The wall clock rewinds 90 seconds during a stop command with a 1,005,000 ms budget.

  • Before: The supplied Gateway trace shows subsequent command allowances reaching 1,095,000 ms.
  • After: The supplied trace shows allowances remaining at or below 1,005,000 ms.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, correct repair with meaningful Gateway-path proof and regression coverage; cloud transport and wall-clock changes are simulated.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): Supplied terminal output exercises the changed retry owner through a running Gateway, authenticated client, shipped dispatcher, provider and subprocess runner; a simulated 90-second rewind inflates the baseline allowance while native monotonic timing keeps the patched allowance bounded. The substituted Crabbox executable limits this evidence to deadline accounting rather than live cloud cleanup.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Wall-clock corrections can delay cloud-worker cleanup or prematurely exhaust command allowances.
Reason: The patch restores the existing original-budget contract with five clock substitutions and focused regression coverage.

Merge readiness

✅ Ready for maintainer review

This PR fixes a concrete deadline-accounting bug still present on current main, with no actionable correctness findings.

Priority: P2
Reviewed head: 20961bce27a8a3ab5b7100d8556e02377ce64467

Before merge

None.

Findings

None.

Agent review details

How this fits together

The Crabbox plugin translates Gateway worker lifecycle requests into CLI subprocesses and returns their results; its command owner shares elapsed-time budgets across coordinator retries and Azure recovery.

flowchart TD
  A[Gateway lifecycle request] --> B[Worker environment service]
  B --> C[Crabbox provider]
  C --> D[Stop and retry owner]
  D --> E[Process runtime]
  E --> F[Crabbox CLI]
  F --> D
Loading

Technical review

Best possible solution:

Land the focused monotonic-clock correction in the existing Crabbox command owner.

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

Current-main deadline arithmetic directly establishes the rewind failure, and the contributor supplies a matching before/after Gateway trace; this read-only review did not rerun it.

Is this the best way to solve the issue?

Using one monotonic clock for each deadline and remaining-budget read repairs the accounting at its existing owner without changing configured durations, retry classification or recovery policy.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against d07bd8b6ad7b.

Provenance checked

  • Coordinator retry budget keeps the original intent (2288edb: Retry coordinator failures inside the original phase budget without replaying scripts that may have started.)
  • Azure stop recovery budget keeps the original intent (db01f6d: Recover fixed leases after local create-intent loss while retaining the original stop deadline.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 2288edb adds extensions/crabbox/src/crabbox-worker-command.ts:183 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 2288edbca9dd; files: extensions/crabbox/src/crabbox-worker-command.ts)
  • Galin Iliev: Raw commit db01f6d adds extensions/crabbox/src/crabbox-worker-command.ts:310 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: db01f6d520b8; files: extensions/crabbox/src/crabbox-worker-command.ts)

PR surface

Source 0, Tests +72. Total +72 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 5 5 0
Tests 2 74 2 +72
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 79 7 +72

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.

Label justifications:

  • P2: Clock corrections can extend cloud-worker command and cleanup allowances, with impact limited to the Crabbox lifecycle path.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/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.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (11 earlier review cycles; latest 8 shown)
  • reviewed 2026-10-06T02:16:07.895Z sha 577e99ff7827b546dcdf72f2bb32d69b90b473ed :: needs changes before merge. :: none
  • reviewed 2026-10-06T02:35:21.629Z sha f291fe91b55dd26af6018de6c4c5d7781cdd8d85 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-06T15:39:19.767Z sha 735245a735158e17038a853c166528192ff66f93 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-14T02:14:20.095Z sha 338c2dc381c97a67345f5dd86b969b34d36a21fe :: needs maintainer review before merge. :: none
  • reviewed 2026-10-14T04:08:56.271Z sha 20961bc :: needs maintainer review before merge. :: none
  • reviewed 2026-10-14T06:17:30.218Z sha 20961bc :: needs maintainer review before merge. :: none
  • reviewed 2026-10-14T06:44:02.724Z sha 20961bc :: needs maintainer review before merge. :: none
  • reviewed 2026-10-14T07:08:16.220Z sha 20961bc :: needs maintainer review before merge. :: none

Reviewed October 14, 2026, 3:28 AM ET / 07:28 UTC (Revision 12).

@SunnyShu0925
SunnyShu0925 force-pushed the fix/crabbox-worker-command-coordinator-retry-deadline-wall-clock branch 3 times, most recently from 02526bf to efb8a00 Compare October 6, 2026 01:42
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 6, 2026
@SunnyShu0925
SunnyShu0925 force-pushed the fix/crabbox-worker-command-coordinator-retry-deadline-wall-clock branch from efb8a00 to 577e99f Compare October 6, 2026 02:01
@clawsweeper

clawsweeper Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@SunnyShu0925
SunnyShu0925 force-pushed the fix/crabbox-worker-command-coordinator-retry-deadline-wall-clock branch 2 times, most recently from f291fe9 to 735245a Compare October 6, 2026 15:30
@SunnyShu0925
SunnyShu0925 force-pushed the fix/crabbox-worker-command-coordinator-retry-deadline-wall-clock branch from 735245a to 338c2dc Compare October 14, 2026 02:05
…skew

runCrabboxCommandWithCoordinatorRetry and stopCrabboxLease seed their
deadline with Date.now() and read remaining budget with Date.now(), then
pass the remaining to runCommand which feeds setTimeout (monotonic) via
resolveTimerTimeoutMs. When the wall clock rewinds (NTP step-back or
suspend-resume), the remaining is amplified by the rewind delta, so lease
commands hang far longer than the configured timeout.

Switch all five Date.now() calls in crabbox-worker-command.ts to
performance.now(), which is monotonic and immune to wall-clock skew. The
existing 'keeps the stop deadline' test spy moves from Date.now to
performance.now to match. A new regression test spies Date.now to rewind
the wall clock between seed and read and asserts every retry timeoutMs
stays within the original budget; pre-fix this fails with 94000 > 5000.
@SunnyShu0925
SunnyShu0925 force-pushed the fix/crabbox-worker-command-coordinator-retry-deadline-wall-clock branch from 338c2dc to 20961bc Compare October 14, 2026 04:01
@clawsweeper

clawsweeper Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Oct 14, 2026
@clawsweeper

clawsweeper Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper

clawsweeper Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper

clawsweeper Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper

clawsweeper Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Oct 14, 2026
@fuller-stack-dev
fuller-stack-dev enabled auto-merge (squash) October 16, 2026 02:52
@fuller-stack-dev
fuller-stack-dev merged commit 9a7d4d1 into openclaw:main Oct 16, 2026
94 checks passed
@SunnyShu0925

Copy link
Copy Markdown
Contributor Author

@fuller-stack-dev Thanks~

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 16, 2026
…skew (openclaw#165889)

* fix(crabbox): keep coordinator retry deadline monotonic across clock skew

runCrabboxCommandWithCoordinatorRetry and stopCrabboxLease seed their
deadline with Date.now() and read remaining budget with Date.now(), then
pass the remaining to runCommand which feeds setTimeout (monotonic) via
resolveTimerTimeoutMs. When the wall clock rewinds (NTP step-back or
suspend-resume), the remaining is amplified by the rewind delta, so lease
commands hang far longer than the configured timeout.

Switch all five Date.now() calls in crabbox-worker-command.ts to
performance.now(), which is monotonic and immune to wall-clock skew. The
existing 'keeps the stop deadline' test spy moves from Date.now to
performance.now to match. A new regression test spies Date.now to rewind
the wall clock between seed and read and asserts every retry timeoutMs
stays within the original budget; pre-fix this fails with 94000 > 5000.

* test(crabbox): prove shared budgets across independent clock steps

---------

Co-authored-by: Jason (Json) <[email protected]>
hilsbos pushed a commit to hilsbos/openclaw that referenced this pull request Oct 22, 2026
…skew (openclaw#165889)

* fix(crabbox): keep coordinator retry deadline monotonic across clock skew

runCrabboxCommandWithCoordinatorRetry and stopCrabboxLease seed their
deadline with Date.now() and read remaining budget with Date.now(), then
pass the remaining to runCommand which feeds setTimeout (monotonic) via
resolveTimerTimeoutMs. When the wall clock rewinds (NTP step-back or
suspend-resume), the remaining is amplified by the rewind delta, so lease
commands hang far longer than the configured timeout.

Switch all five Date.now() calls in crabbox-worker-command.ts to
performance.now(), which is monotonic and immune to wall-clock skew. The
existing 'keeps the stop deadline' test spy moves from Date.now to
performance.now to match. A new regression test spies Date.now to rewind
the wall clock between seed and read and asserts every retry timeoutMs
stays within the original budget; pre-fix this fails with 94000 > 5000.

* test(crabbox): prove shared budgets across independent clock steps

---------

Co-authored-by: Jason (Json) <[email protected]>
(cherry picked from commit 9a7d4d1)
Co-authored-by: RomneyDa <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 27, 2026
…skew (#165889)

* fix(crabbox): keep coordinator retry deadline monotonic across clock skew

runCrabboxCommandWithCoordinatorRetry and stopCrabboxLease seed their
deadline with Date.now() and read remaining budget with Date.now(), then
pass the remaining to runCommand which feeds setTimeout (monotonic) via
resolveTimerTimeoutMs. When the wall clock rewinds (NTP step-back or
suspend-resume), the remaining is amplified by the rewind delta, so lease
commands hang far longer than the configured timeout.

Switch all five Date.now() calls in crabbox-worker-command.ts to
performance.now(), which is monotonic and immune to wall-clock skew. The
existing 'keeps the stop deadline' test spy moves from Date.now to
performance.now to match. A new regression test spies Date.now to rewind
the wall clock between seed and read and asserts every retry timeoutMs
stays within the original budget; pre-fix this fails with 94000 > 5000.

* test(crabbox): prove shared budgets across independent clock steps

---------

Co-authored-by: Jason (Json) <[email protected]>
(cherry picked from commit 9a7d4d1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: crabbox P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants