Skip to content

fix: recover Gateway ownership after sleep and clock changes - #170790

Merged
steipete merged 1 commit into
mainfrom
upd/upd-l1588
Oct 13, 2026
Merged

steipete merged 1 commit into
mainfrom
upd/upd-l1588

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes Gateway state ownership being permanently fenced after host sleep or a backward clock change, leaving database-backed work unavailable while health checks still succeed.

Fixes #170525. Fixes #170597. Fixes #169829. Supersedes #170020, thanks @jjjhenriksen.

User Impact

Release-note context: the Gateway renews ownership after sleep and backward clock changes when its original locks remain held. Real ownership loss still fences database access, but an authorized recovery restart proceeds even when the fenced database cannot supply its restart request. Errors identify ownership loss and tell the operator to restart the Gateway.

No configuration, command-line, dependency, or database-schema changes are required. Update and Doctor maintenance retain their existing ownership and release contracts.

Why This Change Was Made

Wall-clock age does not prove that another process owns the state. The heartbeat owner now verifies the held file, inode, and acquisition bytes before renewing expired or future timestamps. Parent inspection shares the worker's physical renewal primitive; removed/replaced locks and failed renewal remain failures. Authorized local process control can recover without reading the fenced database.

A bounded fixture repair avoids loading the TypeScript handoff resolver for pure SQLite revision-probe children, which never access the handoff store. Real revision hashes and all actual handoff-consumer isolation guards remain.

The patch also fixes a startup-migration test race by checking preserved history after awaited maintenance for all three store layouts. The old asynchronous projection rebuild could race that assertion. An existing unauthorized-SIGUSR2 regression was moved into the run-loop ownership support file without removing coverage.

Production LOC: +128/-98, net +30. Test/support LOC: +272/-149, net +123. Documentation: +8. Dead time-only fencing guards and ignored diagnostic-drain bookkeeping were removed.

Evidence

  • The expired/future heartbeat regressions and owner-loss/restart-intent-read regression failed against the original implementation. The focused run passed 206 tests; the later sibling run passed the affected infra and CLI files, including restart/reacquisition and identical-byte replacement-inode coverage.
  • Focused wall times before the rebase: infra ownership/heartbeat files 134.91 seconds; run-loop file 170.63 seconds. The new restart regression took 134 milliseconds after removing irrelevant compiled-worker startup from that fixture.
  • The three startup-migration layout cases passed in 5–6 seconds each after the fixture correction. Prior timings were 20–40 seconds, with the early history read failing while projection maintenance was still running.
  • Core types and gateway-root/infra/config-cli test types passed. All planned changed-file check steps completed after fixing two type errors. Targeted lint, formatting, docs checks, and git diff --check passed. Prior Knip scans reported no unused exports; Madge reported 0 cycles.
  • Rebased onto current main to incorporate test(config): migrate preflight snapshot reader ownership #170619's disposable preflight-reader test migration; no duplicate fix was added. The affected production files and installation fixture were unchanged by the rebase.
  • Installation comparison used node scripts/run-vitest.mjs src/commands/doctor-maintenance.installation.test.ts --maxWorkers=1, sequentially with identical dependencies and the parent pinned at 203fefe8832af7bc18973d64d8c98e5f98277170. The clean detached base passed 21/21 in 465.01 seconds wall time. The rebased candidate initially passed 16/21 and timed out in five cases (writer-unavailable, no-consent, slow-admission, paired-device stop failure, competing update); its wrapper took 931.48 seconds. These failures are not classified as inherited.
  • Tracing the writer-unavailable case found the blocking await admission: 210 real synchronous SQLite revision probes consumed 104.28 seconds before the 120-second timeout, with no parent heartbeat renewals observed. The fixture's handoff resolver was being booted for every unrelated pure probe. After removing that overhead, the entire installation file passed 21/21 in 565.93 seconds wall time (554.32 seconds Vitest duration; 95% tests). Writer-unavailable fell from 141.26 to 101.18 seconds; no-consent from 142.29 to 75.60; slow-admission from 123.73 to 72.40; paired-device stop failure from 128.51 to 85.77. The heartbeat implementation, deadlines, and assertions were unchanged during this repair. The file remains slow; broad CI is the remaining proof.
  • Managed-handoff helper tests passed 12/12 in 35.44 seconds wall time (5.06 seconds test time), including all existing alias/hardlink/parent replacement and real consumer-binding guards. The new real revision-worker regression failed against the original helper at the intended unnecessary-preload assertion, then passed in 461 milliseconds. Targeted root test types and lint passed.
  • The initial heartbeat/restart autoreview could not establish its required isolation and produced no review result; per the campaign exception, the lead reviews that implementation at the PR head. A fresh isolated Codex review of the helper repair was scoped-clean at P0–P2. PR CI supplies broad proof; the full sibling sweep was not repeated after rebase.

@clawsweeper

clawsweeper Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime cli CLI command changes size: L labels Oct 13, 2026
@steipete
steipete marked this pull request as ready for review October 13, 2026 07:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T09:06:51.744282Z 03a625d New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 36de469a2d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +97 to +98
Atomics.store(lastBeat, 0, BigInt(renewalStartedAt));
return 60_000;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore worker renewal after parent recovery

When the heartbeat worker exits cleanly or stops after a transient renewal failure, this recovery updates lastBeat once but never starts a replacement worker, leaving all subsequent renewal to the 60-second main-thread watcher. A later synchronous main-thread stall can therefore exceed the 90-second stale threshold, allowing a foreign-namespace process to reclaim the lock while database workers still consider this Gateway authoritative; restart the worker after successful physical recovery, or fence instead of permanently degrading to main-thread renewal.

AGENTS.md reference: AGENTS.md:L70-L70

Useful? React with 👍 / 👎.

Reverify physical lock custody before renewing expired or future heartbeat
stamps. Preserve authorized restart recovery when the fenced state database
cannot supply the durable restart request, and name actual ownership loss.

Fixes #170525. Fixes #170597. Fixes #169829.
Supersedes #170020, thanks @jjjhenriksen.
Release notes: Gateway state ownership survives host sleep and backward clock
changes while the original locks remain held; real ownership loss can recover
through an authorized restart even when the state database is fenced.

Co-authored-by: Jacqueline Henriksen <[email protected]>
@steipete
steipete merged commit d0554f4 into main Oct 13, 2026
141 of 142 checks passed
@steipete
steipete deleted the upd/upd-l1588 branch October 13, 2026 09:19
steipete added a commit that referenced this pull request Oct 13, 2026
Portable workers start the state-owner heartbeat during SQLite admission, but the archive omitted its entry. Build and stage the existing worker, register its sealed sibling URL, and resolve that URL through the existing runtime owner. Keep custody and error handling intact.

The existing relocated authorization regression now uses a dist/worker path and existing artifact fixtures include the heartbeat. Culprit: 99fccf4 (#164236), exposed by d0554f4 (#170790). Scoped Codex review and changed-file format/lint checks pass; focused hosted execution is pending.
RomneyDa added a commit that referenced this pull request Oct 14, 2026
…backport

The #170790 backport (b085d30) is a faithful port: its
gateway-state-owner*.ts hunks match main d0554f4 line for line. It
deliberately renamed local ownership-loss errors from "could not be
verified; retry after maintenance finishes" to "was lost ...; restart the
Gateway", and main updated its surviving expectation in
gateway-state-owner.test.ts the same way. The read-window and expiry
timing are unchanged.

Two release-only suites still asserted the old text. Main no longer has
them: #168859 (19fe879) removed the read-verification window and
gateway-state-owner.read.test.ts, and #166619 (c9acd71) deleted
openclaw-state-read-worker.ownership.test.ts. Neither change was
backported. Point those assertions at the #170790 wording and keep their
timing and no-dispatch checks.

The expired-read path also interpolated the recorded loss Error into a
second "; restart the Gateway." template, which printed
"was lost: Error: <lock>: ...; restart the Gateway.; restart the Gateway.".
Rethrow a recorded loss unwrapped, as the strict access path already
does. The queued-read regression anchors the message end, so it fails on
the doubled text. Main d0554f4 has the same doubled-message bug.
RomneyDa pushed a commit that referenced this pull request Oct 14, 2026
…170790)

The state-owner heartbeat fenced on wall-clock age alone, so macOS lid-close sleep, DarkWake mid-startup, or a WSL2 backward clock step made a live Gateway fence itself out of its own state DB; on WSL2 the SIGUSR2 restart then could not read its intent from the fenced DB and only logged, leaving a Gateway that answered /health while every DB-backed job failed. Both heartbeat paths now re-verify the held lock file (nlink, dev/ino, exact bytes) after an expired or future timestamp and renew an unchanged lock; removed or replaced locks still fence. State-owner reclaim still requires a definitely-dead owner PID, so renewal cannot split ownership. An authorized local restart proceeds when the durable restart intent cannot be read, and errors name ownership loss and say to restart the Gateway. Fixes #170525. Fixes #170597. Fixes #169829. Supersedes #170020, thanks @jjjhenriksen.

(cherry picked from commit d0554f4)

Backport note: the release line has no run-loop-restart-ownership.test-support.ts
(split out on main by #168587); registerGatewayRestartOwnershipTests still lives in
run-loop.test-support.ts, so the moved SIGUSR2 regression and the new owner-loss
restart regression were applied there. The release-only "bounded Gateway state
reads" test now expects the new ownership-loss messages. The restart message in
assertStateDatabaseAccessAllowed is held in a local so gateway-state-owner.ts stays
within the 700-line lint limit; wording is unchanged.

Co-authored-by: Jacqueline Henriksen <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 14, 2026
…penclaw#170790)

The state-owner heartbeat fenced on wall-clock age alone, so macOS lid-close sleep, DarkWake mid-startup, or a WSL2 backward clock step made a live Gateway fence itself out of its own state DB; on WSL2 the SIGUSR2 restart then could not read its intent from the fenced DB and only logged, leaving a Gateway that answered /health while every DB-backed job failed. Both heartbeat paths now re-verify the held lock file (nlink, dev/ino, exact bytes) after an expired or future timestamp and renew an unchanged lock; removed or replaced locks still fence. State-owner reclaim still requires a definitely-dead owner PID, so renewal cannot split ownership. An authorized local restart proceeds when the durable restart intent cannot be read, and errors name ownership loss and say to restart the Gateway. Fixes openclaw#170525. Fixes openclaw#170597. Fixes openclaw#169829. Supersedes openclaw#170020, thanks @jjjhenriksen.

Co-authored-by: Jacqueline Henriksen <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 14, 2026
Portable workers start the state-owner heartbeat during SQLite admission, but the archive omitted its entry. Build and stage the existing worker, register its sealed sibling URL, and resolve that URL through the existing runtime owner. Keep custody and error handling intact.

The existing relocated authorization regression now uses a dist/worker path and existing artifact fixtures include the heartbeat. Culprit: 0625edd (openclaw#164236), exposed by 1fa2181 (openclaw#170790). Scoped Codex review and changed-file format/lint checks pass; focused hosted execution is pending.
RomneyDa pushed a commit that referenced this pull request Oct 14, 2026
Portable workers start the state-owner heartbeat during SQLite admission, but the archive omitted its entry. Build and stage the existing worker, register its sealed sibling URL, and resolve that URL through the existing runtime owner. Keep custody and error handling intact.

The existing relocated authorization regression now uses a dist/worker path and existing artifact fixtures include the heartbeat. Culprit: 99fccf4 (#164236), exposed by d0554f4 (#170790). Scoped Codex review and changed-file format/lint checks pass; focused hosted execution is pending.

(cherry picked from commit 9e52f76)
RomneyDa added a commit that referenced this pull request Oct 14, 2026
The release-only read-worker ownership test still expected the retired
'could not be verified; retry after maintenance' wording. #170790 changed
the owner contract: a removed or replaced lock now reports ownership loss
and tells the operator to restart the Gateway. Main deleted this test in
#168859, so assert the new messages here instead of weakening the checks.
github-actions Bot pushed a commit to clawhaven/openclaw that referenced this pull request Oct 16, 2026
…penclaw#170790)

The state-owner heartbeat fenced on wall-clock age alone, so macOS lid-close sleep, DarkWake mid-startup, or a WSL2 backward clock step made a live Gateway fence itself out of its own state DB; on WSL2 the SIGUSR2 restart then could not read its intent from the fenced DB and only logged, leaving a Gateway that answered /health while every DB-backed job failed. Both heartbeat paths now re-verify the held lock file (nlink, dev/ino, exact bytes) after an expired or future timestamp and renew an unchanged lock; removed or replaced locks still fence. State-owner reclaim still requires a definitely-dead owner PID, so renewal cannot split ownership. An authorized local restart proceeds when the durable restart intent cannot be read, and errors name ownership loss and say to restart the Gateway. Fixes openclaw#170525. Fixes openclaw#170597. Fixes openclaw#169829. Supersedes openclaw#170020, thanks @jjjhenriksen.

Co-authored-by: Jacqueline Henriksen <[email protected]>

(cherry picked from commit d0554f4)
Backport adaptation: ported the fenced-intent restart fallback into the release branch's inline SIGUSR2 handler in run-loop.ts (no run-loop-signals.ts) with a run-loop.test.ts regression, and dropped the main-only session-startup-migration test race fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes docs Improvements or additions to documentation gateway Gateway runtime size: L

Projects

None yet

1 participant