Skip to content

fix(update): retire database snapshots of earlier updates after a verified update - #202591

Merged
jalehman merged 8 commits into
mainfrom
openclaw/gentle-langoustine
Nov 21, 2026
Merged

jalehman merged 8 commits into
mainfrom
openclaw/gentle-langoustine

Conversation

@jalehman

@jalehman jalehman commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

An update can leave a full copy of the databases in a .databases directory. Only the creating run's own verified completion removes it. Nothing removes one that a failed update, an update without automatic restoration, or an older updater left behind. On one host, eight of them held 63 GiB.

On today's main (after #200578 and #201711):

User Impact

User impact: after a later update is verified healthy, update cleanup removes the snapshot directories earlier updates left behind, both automatically and through openclaw update cleanup (and the Gateway's update.cleanup, including dry run). This includes the ones older updaters left in a Git checkout's .artifacts. The ones it keeps are listed with their size and reason. The latest verified update's own snapshot waits for the next verified update. Snapshots of a Git checkout outside the home directory are not found and stay, as do snapshots no listed update recorded. With database.postgres.recoveryCaptures: "server", cleanup drops a snapshot's PostgreSQL capture schemas before deleting it, and keeps and reports the snapshot when that drop fails.

Why This Change Was Made

A snapshot directory's name carries a transaction id, not a run id, and nothing inside it names a run (schema-discovery-ownership.json maps databases to owners only). The only record linking it to an update is the database snapshot location in update history. Cleanup now reads that location and treats a snapshot as superseded once a verified successful update started after its run finished. That is the rule original-state captures already follow, decided by run order and never by file times.

  • Kept and reported: a snapshot of an unfinished run, of a run with a pending recovery, of the latest verified run or anything after it, and a snapshot-named directory no listed run recorded (orphan-database-snapshot; only file times could order it).
  • Automatic cleanup never releases the triggering update's own snapshot, also when a clock set back makes an earlier verified run look like its successor.
  • Deletion reuses the capture retirement path: no symlinks, same directory identity as when selected, same cleanup authority.
  • PostgreSQL server captures keep the lifecycle their owning update gives them (feat(database): keep PostgreSQL rollback captures in PostgreSQL #196236). A snapshot's <image>.postgres export manifests are the only record of the capture schemas on the server. Cleanup passes the export directories that hold a manifest to the existing discardPostgresSchemaCaptures first, with the configuration cleanup already reads. If the drop fails, the directory stays and the item reports failed / database-snapshot-retirement-failed with PostgreSQL capture cleanup remains pending at <directory>; a later cleanup retries. A .postgres directory without a manifest is a mirrored source directory or the rest of an interrupted removal, and does not block cleanup.
  • Previous install roots are in scope only when a run recorded a location that still resolves. Update history shortens the home directory to ~ and redacts other install roots to [path], so a checkout outside the home directory cannot be resolved. When the OpenClaw home and the OS home differ, ~ names two directories and attributes none.
  • An earlier install root that is unreadable, or whose path is no longer a directory path, is skipped instead of failing the whole cleanup inventory.

Decisions for the maintainer:

  1. Attribution parses a path-redacted diagnostic. A structured, unredacted receipt in the run record would also cover checkouts outside the home directory, but needs a schema change and would not help snapshots already on disk.
  2. A run's own verified success does not release its snapshot here, because the updater says non-restorable snapshots remain available after activation. Captures do release on their own success.
  3. Orphans are report-only; --include-unmanifested cannot select them.
  4. Status now counts these snapshots under update captures. Doctor's core/doctor/update-snapshots check still advises manual removal.
  5. The server-capture drop uses the current configuration's connection, as openclaw update finish does for its own receipt. Accepted: if the configuration now names a different PostgreSQL server than the one the snapshot was captured on, DROP SCHEMA IF EXISTS finds nothing there, the snapshot is deleted, and the capture schema on the old server stays.
  6. A superseded snapshot is removed without checking that the live databases it copied still exist. Original-state captures follow the same rule; Doctor backups and moved-aside copies do check. A review asked for that check here; it is not added, because a later update was verified healthy on that state. Say so if snapshots should get the check too.

Evidence

Head 3d509d0, rebased onto main 4bedaed. The real runs below used 9334a04; 3d509d0 differs from it in one test file only.

Tests (each red on the base, green on the branch)

  • src/cli/update-cli/update-command-database-snapshot-cleanup.test.ts (new, 7 tests) runs the real updateCleanupCommand and cleanupCompletedUpdateRecovery over a temp home with real update-history rows:
    • dry run reports each snapshot's run, bytes and reason and deletes nothing;
    • --yes and automatic cleanup remove only the two superseded snapshots (a legacy Git one under a previous checkout root, a package one outside home);
    • kept: pending recovery, latest verified run, a failure after it, an unfinished run, an orphan, and the triggering run's own snapshot; untouched: a non-updater name, a redacted-root snapshot, a recorded path replaced by a symlink, and home-relative snapshots while two homes are in play;
    • a former install root replaced by a regular file (ENOTDIR) no longer aborts cleanup;
    • the server-capture drop receives only the export directory that holds a manifest, while that manifest still exists; when it rejects, the snapshot stays failed and the command exits 1.
  • src/infra/database-engine/backup-schema-capture.test.ts against real PostgreSQL 17 (scripts/test-database-conformance.mjs, disposable pgvector/pgvector:pg17 container): a real update capture creates a restore_… schema and updateCleanupCommand({ yes: true }) runs twice. With an unreachable connection the schema and manifest both remain and the item reports failed. With the real connection the capture schema is dropped, the live store's schema remains and the directory is gone. Run on 84954c9, not repeated after the later two commits.
  • On the rebased branch all 7 tests in the first file fail with the wiring reverted to main, and the PostgreSQL test fails the same way. The triggering-run and manifest cases were each red without their fix.
  • pnpm check:changed on 9334a04: exit 0. Net test lines against the merge base: +400 of 400 (9334a04 failed checks-fast-baseline-ratchets at +409; 3d509d0 folds one test into another).

It works: legacy Git snapshots (Crabbox local-container on the Mac Studio, node:24.22.1-trixie, lease cbx_d34709ead512, run run_534ff1aa121f62943602014dc138d057, exit 0)

  • An updater from before feat(update): run every Git install from releases and delete the in-place Git runner #200578 (44ec89e) updates a source checkout in place four times while the Gateway runs by hand: twice under ~/Projects/openclaw/.worktrees/live-main-ba9f1ea, twice under ~/Projects/openclaw after the install moves. Each run keeps its .artifacts/dist-runtime.openclaw-update-<uuid>.tmp.databases (17.8 MB together).
  • The four update-history rows, written by those real updates, were then rewritten in the state database to the shapes recorded on the affected host (run failed / post-update-failed, a failed database rollback step): two with the Oct 27 2026 wording, two with the Oct 3 to Oct 20 wording. This is the only state edit.
  • The Gateway service is installed and the checkout updates to main 4bedaed. All four directories remain, and main's openclaw update cleanup --dry-run --json lists no database-snapshot artifact.
  • The next update, to this PR, finishes verified (serviceRunning, versionMatch, settled, coreReady, no plugin errors). Its automatic cleanup reports Removed …/.worktrees/live-main-ba9f1ea/.artifacts/dist-runtime.openclaw-update-26124ce4-….tmp.databases (4272383 logical bytes) and the other three. No snapshot directory remains.
  • Caveat: the update to main exited 1 (post-update-failed: the old checkout build refused the new state schema) although the Gateway then served from the release. So this run shows that main neither lists nor removes these directories, not that they survive a verified update on main. That failure happened before any code from this PR was installed.
  • Substituted: the container has no systemd, so the service manager is the repository's upgrade e2e stand-in.

It works: package install (lease cbx_c94e7446a4a3, run run_f7324d80b838230c88083cd349e11a46, exit 0)

  • npm install -g [email protected], then openclaw update --tag <tarball> to main~6 (a27f0c3) and to this PR with the Gateway service running. Both finish verified. Each run snapshots to ~/.npm-global/lib/node_modules/.openclaw.package-backup-<pid>-<ts>.databases and removes it on its own verified completion; none remain and the dry run is empty.
  • This shows a package upgrade from stable works on the PR. It did not produce a kept package snapshot: with the Gateway run by hand, 2026.11.6 refuses the update (gateway-stop-unverified) before taking one.

It does not break existing upgrades (repository upgrade lanes, scripts/e2e/upgrade-survivor-docker.sh, run from lease cbx_fc8ff4647655 through the Studio's Docker engine, candidate 9334a04)

Lane Baseline Result
published upgrade survivor (package) [email protected] passed, 660 s
git-release (Git install, kill points stage build publish link-switch launcher-restore) tag v2026.11.6 passed over two runs
recovery-cleanup [email protected] failed at package-swap, inherited
  • git-release: the first run passed every phase through git-kill-build, then failed in git-kill-publish because the harness missed its kill window ("missed":"the link moved before the release smoke was seen"). The second run, with kill points publish link-switch launcher-restore, passed (Git release survivor passed: route baseline.).
  • recovery-cleanup: State schema inspection failed … during pre-migration database backup for …/client/credentials.sqlite … hardlink publication target changed, then a clean package rollback. fix(update): reclaim abandoned updater runtimes on a busy host #202590 saw the same signature on main (lease cbx_e9fabf91f712). It was not rerun on main here.
  • All three leases were stopped.

Earlier proof on 5fb7e5a, before the rebase (leases cbx_5fe14464ed65, cbx_892d7fe68601, cbx_f37a9e6d9f22): base kept three in-place Git snapshots after a verified update and did not list them; head listed them as awaiting-later-verified-update and removed them after the verified update.

Not proven

  • No run against the affected host's own update history.
  • A kept package snapshot (failed update, or restoration unavailable) retired through a real update; covered by the test file only.
  • openclaw update cleanup --yes removing a snapshot candidate through the real CLI: automatic cleanup always got there first.
  • The server-capture drop through a full openclaw update cycle on a PostgreSQL install.
  • Windows ~\ expansion, receipt wording of updaters before Oct 3 2026, and an install root failing with EACCES (same guard as ENOTDIR, no test).
  • Update history's secret redaction can mask part of a long checkout path (seen once as /srv/p…law/.worktrees/…). Such a receipt does not resolve, so that snapshot stays.

🦞 Made with OpenClaw

@clawsweeper

clawsweeper Bot commented Nov 20, 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 docs Improvements or additions to documentation cli CLI command changes commands Command implementations size: M labels Nov 20, 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. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 20, 2026
@clawsweeper

clawsweeper Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: stale review; fresh review needed.

Summary
The latest durable ClawSweeper review was for head 5fb7e5abb4d005a26852d133e3ff1d6903cd9bee, but the PR head is now 84954c9d5af1126ae02f2499bb0aa3cd91aee2c3. Its old verdict and PR readiness labels are no longer current.

Next step
Comment @clawsweeper re-review when the current head is ready; pushes after the first review are not reviewed automatically.

Review history (1 earlier review cycle)
  • reviewed 2026-11-20T17:04:09.102Z sha 5fb7e5a :: blocked before merge. :: [P2] Retire PostgreSQL server captures before deleting their manifests | [P2] Catch failures while inspecting each historical snapshot parent

@jalehman
jalehman marked this pull request as ready for review November 20, 2026 23:27
@roboclaw-bot

Copy link
Copy Markdown
Collaborator

Exact-head follow-up on 5fb7e5abb4d0: the selected CI is green, and I see that database-snapshot now uses the existing capture-retirement path. The unreadable-parent finding still applies: lstatSync(parent, { throwIfNoEntry: false }) sits outside the try in update-database-snapshot-cleanup.ts. That flag handles a missing path, not an EACCES or ENOTDIR from an inaccessible/replaced ancestor. One historical root can therefore abort discovery before a valid unrelated root is processed. Please move the parent stat into the existing per-parent guard and add a regression with one failing historical parent and one valid superseded snapshot. Keep the existing ownership and deletion checks intact.

…ed update

Update cleanup now lists the pre-migration .databases snapshot directory each update recorded in update history and removes it once a later update that started after that run finished is verified successful. Unfinished runs, pending recoveries, the latest verified run and unrecorded directories keep theirs and are reported with size and reason.
…ified update

OpenClaw-Publication: db146582-b8a2-8de7-9073-17ee98535f6f
@clawsweeper clawsweeper Bot removed the rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. label Nov 21, 2026
@jalehman
jalehman force-pushed the openclaw/gentle-langoustine branch from 96e891b to 84954c9 Compare November 21, 2026 19:12
@clawsweeper clawsweeper Bot removed the status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. label Nov 21, 2026
@jalehman
jalehman merged commit 84f9d21 into main Nov 21, 2026
129 checks passed
@jalehman
jalehman deleted the openclaw/gentle-langoustine branch November 21, 2026 22:40
@jalehman

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 22, 2026
…ified update (openclaw#202591)

* fix(update): retire database snapshots of earlier runs after a verified update

Update cleanup now lists the pre-migration .databases snapshot directory each update recorded in update history and removes it once a later update that started after that run finished is verified successful. Unfinished runs, pending recoveries, the latest verified run and unrecorded directories keep theirs and are reported with size and reason.

* fix(update): keep ambiguous and unreadable snapshot locations out of cleanup

* fix(update): retire database snapshots of earlier updates after a verified update

OpenClaw-Publication: db146582-b8a2-8de7-9073-17ee98535f6f

* fix(update): drop server captures before retiring a snapshot and tolerate replaced roots

* test(update): keep snapshot cleanup tests within the test-line budget

* docs(update): describe in-place Git snapshots as left by older updaters

* fix(update): keep the triggering run snapshot and resume interrupted snapshot removal

* test(update): keep snapshot cleanup tests within the test-line budget
tideclaw-bot pushed a commit that referenced this pull request Nov 24, 2026
…e Node (#203152)

* fix(update): drain a Gateway that runtime recovery reran under another Node

A service whose Node is unsupported or lacks a recommended security patch
reruns the Gateway under a compatible Node as a child of the launcher, so the
service's main PID never equals the PID the resident reports. Maintenance drain
refused every such update with 'Gateway process changed during maintenance
drain'. Accept the resident when every Gateway port listener is that process
and its parent is the service's main process, matching restart port ownership.

Refs #202954

* docs(update): runtime recovery reruns the Gateway only when its Node fails admission

Co-authored-by: Peter Steinberger <[email protected]>
(cherry picked from commit a27f0c3)
Release-companions: #204017 is a separate Linux release-install Doctor fallback when lsof/ss cannot attribute listeners; it is not required for this attributed-listener runtime-recovery case. #203486 pnpm recovery, #201668 data-risk repair, and #202591 snapshot cleanup only overlap documentation; #203236 is independent auto-update retry policy. The neighboring native drain suite remains unchanged.
Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes commands Command implementations docs Improvements or additions to documentation P2 Normal backlog priority with limited blast radius. size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants