Skip to content

fix(agents): keep compaction safety deadline on the monotonic clock - #184293

Merged
obviyus merged 2 commits into
openclaw:mainfrom
SunnyShu0925:fix/compaction-safety-timeout-deadline-monotonic-186
Oct 27, 2026
Merged

obviyus merged 2 commits into
openclaw:mainfrom
SunnyShu0925:fix/compaction-safety-timeout-deadline-monotonic-186

Conversation

@SunnyShu0925

@SunnyShu0925 SunnyShu0925 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Compaction enforces two nested deadlines: a per-progress window (timeoutMs, 180 s by default) re-armed on every progress tick, and a hard ceiling of ten windows. The helper owning both seeded the ceiling with Date.now() and re-read Date.now() on every re-arm, while setTimeout runs on Node's monotonic clock.

A host clock correction (NTP resync, manual time change) therefore distorted the remaining budget: a backward jump inflated it so a hung compaction lost its safety abort; a forward jump collapsed it, aborting compaction mid-write.

Why This Change Was Made

The deadline and the timer enforcing it must share one clock. src/node-host/with-timeout.ts owns both, so the ceiling is seeded and re-read with performance.now(); that single owner fixes every runAbortableTimeout caller.

deadlineAt stays epoch-valued. compactWithSafetyTimeout is exported from openclaw/plugin-sdk/agent-harness-runtime (v2026.10.7) and documents deadlineAt as epoch ms, shared by the host watchdog and the delegated summary; CompactionWatchdog.deadlineAt and compactionDeadlineAt stay epoch-valued too. The wrapper converts that epoch ceiling once into an elapsed budget (deadlineAt - Date.now()) and the helper enforces the budget monotonically, so an existing plugin passing deadlineAt: Date.now() + 1000 still gets one second.

Boundary: the conversion happens once, at wrapper entry. For a delegated summary that entry runs after the caller's awaits, so a wall-clock step in that gap still moves that summary's budget by the step, capped at one window; the host ceiling bounding the whole operation is monotonic, so the operation still cannot outlive it.

User Impact

Compaction is no longer aborted prematurely or left hanging when the host clock jumps mid-operation, at any window size, and plugin-supplied deadlineAt values keep their epoch meaning.

Evidence

Real Gateway compaction over the shipped CLI

node openclaw.mjs gateway serves the session and node openclaw.mjs sessions compact agent:main:proof --json starts a real manual compaction. A loopback OpenAI-compatible provider streams the summary so the watchdog sees progress; only Date.now is shifted, 2.5 s into the summary. The ceiling is 10 s (10 × 1 s windows).

run wall-clock step summary aborted elapsed
post-fix backward -300 s yes 8,690 ms
post-fix forward +8 s yes 8,667 ms
pre-fix backward -300 s no (stream alive at the 60 s stub cap; ceiling ~310 s) 60,139 ms
pre-fix forward +8 s yes, early 3,408 ms

Pre-fix is the same build with the two with-timeout.ts lines reverted to Date.now() (matching origin/main); post-fix is the PR head (a92551de).

pre-backward  {"label":"pre-backward","summaryElapsedMs":60139,"ceilingMs":10000,"gatewayAborted":false,"verdict":"CEILING_INFLATED"}
post-backward {"label":"post-backward","summaryElapsedMs":8690,"ceilingMs":10000,"gatewayAborted":true,"verdict":"CEILING_HELD"}
post-forward  {"label":"post-forward","summaryElapsedMs":8667,"ceilingMs":10000,"gatewayAborted":true,"verdict":"CEILING_HELD"}
pre-forward   {"label":"pre-forward","summaryElapsedMs":3408,"ceilingMs":10000,"gatewayAborted":true,"verdict":"CEILING_COLLAPSED"}
post CLI      {"ok":false,"key":"agent:main:proof","compacted":false,"reason":"Compaction timed out"}

Shipped SDK matrix (9 cases)

compactContextEngineWithSafetyTimeout / compactWithSafetyTimeout in a real Node process with real timers and aborts; only Date.now shifted. Current head (a92551de) passes 9/9, origin/main 5/9 (backward rows keep running, forward rows abort early, epoch rows misread). Key rows (windows are timeoutMs; ceiling is ten):

case origin/main current head
backward 300 s @300 ms, 100 ms window 2,200 ms cap, still running 1,001 ms
forward 300 s @300 ms, 100 ms window 382 ms, aborted early 999 ms
deadlineAt = Date.now()+400 401 ms 401 ms

Regression tests

Both jump directions fail with src/node-host/with-timeout.ts reverted to origin/main (expected false to be true backward, expected true to be false forward); treats a caller-supplied deadlineAt as epoch ms fails on the earlier head and passes now. Suites: with-timeout.test.ts 6/6, embedded-agent-runner.compaction-safety-timeout.test.ts 10/10, delegate-watchdog.test.ts 5/5, agent-session-compaction.test.ts 18/18, run.compaction-runtime.test.ts 27/27.

Collision check: no open PR touches with-timeout.ts or compaction-safety-timeout.ts, and origin/main still seeds the ceiling with Date.now().

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Oct 26, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Enforce timeout ceilings with monotonic time while preserving epoch-valued compaction deadline options.

Example: A streaming compaction has a 10-second ceiling when the host clock moves backward by 300 seconds.

  • Before: The safety budget expands, and the supplied Gateway reproduction remains streaming beyond 60 seconds.
  • After: The supplied Gateway reproduction aborts the summary after about 8.7 seconds, within the operation ceiling.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Focused owner-level repair with shipped-entry-point evidence, meaningful regressions, preserved SDK semantics, and passing exact-head checks.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): At head a92551d, the contributor exercises real Gateway compaction through the shipped sessions compact CLI against a streaming loopback provider, shifts Date.now in both directions, and records summary cancellation within the ceiling; the same build with the clock reads reverted demonstrates both failures.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A host clock correction can stop healthy compaction early or prevent its safety ceiling from stopping continuously progressing work.
Reason: The repair restores the documented bounded-compaction contract without adding configuration, changing defaults, or replacing an existing owner.

Merge readiness

✅ Ready for maintainer review

This PR provides a focused, correct repair for clock-sensitive compaction deadlines still present on current main; no actionable findings remain.

Priority: P2
Reviewed head: a92551de90bf470b047b54fbbc0c53159db68b45

Before merge

None.

Findings

None.

Agent review details

How this fits together

Compaction passes progress callbacks and cancellation signals through the host watchdog to the shared timeout helper, which schedules aborts and returns completion or timeout errors.

flowchart TD
 A[Sessions compact CLI] --> B[Gateway compaction]
 B --> C[Host watchdog]
 C --> D[Context engine and summary]
 D --> E[Progress callback]
 E --> F[Shared monotonic timeout]
 F --> D
Loading

Technical review

Best possible solution:

Merge the focused clock repair with the existing timeout owner, epoch deadline contract, and regression coverage intact.

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

Current-main source shows wall-clock subtraction controlling timer rearming, and the contributor supplies before/after Gateway traces for both jump directions; this review executed no target code.

Is this the best way to solve the issue?

This is the smallest complete repair: use monotonic time for the existing helper's elapsed ceiling and retain epoch conversion at the public wrapper boundary.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Shared timeout ceiling and compaction deadline conversion keeps the original intent (5f497e7: Bound continuously streaming compaction at ten windows while allowing progress to refresh the idle timeout and reserving a summary cutoff before the host ceiling.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Elharony Claw: Raw commit d942bc5 adds src/agents/embedded-agent-runner/compaction-safety-timeout.ts:99 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: d942bc5b1fb3; files: src/agents/embedded-agent-runner/compaction-safety-timeout.ts)

PR surface

Source +5, Tests +54. Total +59 across 4 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 7 2 +5
Tests 2 54 0 +54
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 61 2 +59

Labels

Label changes:

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

Label justifications:

  • P2: Host clock corrections can prematurely abort streaming compaction or extend its safety ceiling, with limited environmental exposure.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster 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 (3 earlier review cycles)
  • reviewed 2026-10-26T06:44:33.131Z sha ec5a187dca6f488749280e6cb970878f6a78bf9e :: needs real behavior proof before merge. :: [P1] Preserve epoch semantics for the exported deadline option
  • reviewed 2026-10-26T08:22:48.862Z sha 899f6725201aab572cc7c0f4002df1d36e17d05b :: needs maintainer review before merge. :: none
  • reviewed 2026-10-26T08:56:58.168Z sha b0136ac :: needs maintainer review before merge. :: none

Reviewed October 26, 2026, 9:41 PM ET / October 27, 2026, 01:41 UTC (Revision 4).

@SunnyShu0925
SunnyShu0925 force-pushed the fix/compaction-safety-timeout-deadline-monotonic-186 branch from ec5a187 to 899f672 Compare October 26, 2026 08:10
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. 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: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 26, 2026
runAbortableTimeout seeded the max-total deadline with Date.now() and
re-read Date.now() on every progress reset, while setTimeout arms on the
monotonic clock. A wall-clock jump during compaction inflated the remaining
budget (a hung compaction lost its safety abort) or collapsed it (premature
abort mid-write).

Seed and re-read the ceiling with performance.now() in that helper, the one
owner of both the deadline and the timer.

Keep the plugin-SDK deadlineAt option epoch-valued: the host watchdog and
the delegated summary share that ceiling, so compactWithSafetyTimeout
converts it once into an elapsed budget instead of moving a shipped
parameter to another clock.
@SunnyShu0925
SunnyShu0925 force-pushed the fix/compaction-safety-timeout-deadline-monotonic-186 branch from 899f672 to b0136ac Compare October 26, 2026 08:25
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 27, 2026
@obviyus

obviyus commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Runtime proof on head a92551d against main 137e96f: built Gateway in a secretless Node 24.22.1 container. History was created through normal Gateway turns, then the shipped sessions.compact ran with a slow summarizer that streamed progress continuously, using the supported 2-second progress window (the roughly 20-second hard ceiling unchanged). Date.now was stepped forward 60 seconds inside the process only.

Run Outcome
Main, +60 s step (twice) Premature timeout after 2.785 s and 2.644 s
Candidate, +60 s step (twice) Compacted after 14.834 s and 17.932 s
Main / candidate, no step Both compacted (16.451 s / 15.403 s)

@obviyus
obviyus merged commit c61f6e9 into openclaw:main Oct 27, 2026
151 checks passed
@obviyus

obviyus commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Landed in c61f6e9. Thanks @SunnyShu0925. Compaction no longer times out early after a clock jump.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 28, 2026
…penclaw#184293)

fix(agents): keep the compaction safety deadline on the monotonic clock

The compaction safety deadline was measured with the wall clock, so a forward clock step ended a healthy compaction early with a timeout. The deadline now uses monotonic time; the progress window and hard ceiling are unchanged.

Evidence: shipped sessions.compact on a built Gateway with a slow, continuously progressing summarizer and Date.now stepped forward 60 seconds inside the process only. On main compaction timed out after 2.8s and 2.6s (twice); with this change it completed in 14.8s and 17.9s (twice), like the no-step runs on both refs. Production 9 lines.

Thanks @SunnyShu0925.

Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Oct 28, 2026
…184293)

fix(agents): keep the compaction safety deadline on the monotonic clock

The compaction safety deadline was measured with the wall clock, so a forward clock step ended a healthy compaction early with a timeout. The deadline now uses monotonic time; the progress window and hard ceiling are unchanged.

Evidence: shipped sessions.compact on a built Gateway with a slow, continuously progressing summarizer and Date.now stepped forward 60 seconds inside the process only. On main compaction timed out after 2.8s and 2.6s (twice); with this change it completed in 14.8s and 17.9s (twice), like the no-step runs on both refs. Production 9 lines.

Thanks @SunnyShu0925.

Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit c61f6e9)

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 8bb15647bfa8b8054e92728d6ea0b8abe5b92920)
RomneyDa added a commit that referenced this pull request Oct 28, 2026
…184293)

fix(agents): keep the compaction safety deadline on the monotonic clock

The compaction safety deadline was measured with the wall clock, so a forward clock step ended a healthy compaction early with a timeout. The deadline now uses monotonic time; the progress window and hard ceiling are unchanged.

Evidence: shipped sessions.compact on a built Gateway with a slow, continuously progressing summarizer and Date.now stepped forward 60 seconds inside the process only. On main compaction timed out after 2.8s and 2.6s (twice); with this change it completed in 14.8s and 17.9s (twice), like the no-step runs on both refs. Production 9 lines.

Thanks @SunnyShu0925.

Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Ayaan Zaidi <[email protected]>

Beta.5 baseline adaptation: Retain the native beta.5 owners, fixtures and public contracts; add focused regression controls and bounded fixture extraction; apply checkout formatting.

(cherry picked from commit c61f6e9)

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

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor 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