Skip to content

Commit d7b45d7

Browse files
fix(worktrees): retire redundant removed snapshots explicitly (#156464)
* fix(worktrees): retire redundant removed snapshots explicitly * fix(worktrees): preserve current recovery owners during explicit retirement * test(session-share): run integration catalog service lifecycle
1 parent d17bc21 commit d7b45d7

8 files changed

Lines changed: 956 additions & 22 deletions

File tree

‎docs/concepts/managed-worktrees.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,39 @@ Restore recreates `openclaw/<name>` at the original pre-snapshot commit, reusing
278278

279279
A branch at a shallow history boundary can still be snapshotted and restored. If a later depth-limited fetch makes the snapshot commit itself shallow, Git may no longer resolve its parent. Restore then preserves the snapshot and reports the repository and snapshot commit with `git fetch --unshallow` guidance. OpenClaw does not deepen automatically: origin may not contain the local snapshot, so even a successful fetch cannot guarantee recovery. If the snapshot remains shallow after fetching, recover its parent from the original repository before retrying.
280280

281+
## Retire an already removed snapshot early
282+
283+
Use `openclaw worktrees retire-snapshot` only when the removed snapshot is redundant
284+
with a retained local branch or remote-tracking commit. This local CLI operation
285+
requires the exact worktree ID, snapshot ref and commit, recorded `removedAt`
286+
milliseconds, and retained source ref and commit. Read those identities from the
287+
removed record and repository before invoking it:
288+
289+
```bash
290+
openclaw worktrees retire-snapshot <id> \
291+
--expected-ref refs/openclaw/snapshots/<id> --expected-oid <snapshot-commit> \
292+
--removed-at <milliseconds> \
293+
--retained-ref refs/heads/<retained-branch> --retained-oid <source-commit> --json
294+
```
295+
296+
Retirement applies only to ordinary snapshots, not exact-state recovery snapshots or
297+
their retained checkouts. It requires identical source trees and retained snapshot-parent history.
298+
It refuses a live or reappeared checkout, Git registration, changed identity,
299+
pending removal, run/removal consumer, unknown or nonempty provisioned-file
300+
inventory, or any retained local workspace projection. Git equality does not prove
301+
that accepted ignored projection data is redundant. It uses the existing allocation
302+
lease and projection owner, then atomically verifies the retained ref and deletes
303+
only the expected snapshot ref. It does not create a backup, delete the retained
304+
source or PR outcome refs, run global garbage collection, or report reclaimed disk
305+
bytes. Git object storage can remain shared after ref retirement.
306+
307+
A successful JSON result is `{ "retired": true, "id": "<id>" }`; the removed
308+
registry entry is then absent and cannot be restored. A refusal is not cleanup
309+
success. If an interruption occurs after the ref is retired, inspect the remaining
310+
record and projection custody before further cleanup; this command does not infer
311+
safe retirement from a missing ref. This operation is CLI-only, not a Gateway or
312+
Control UI action.
313+
281314
## Exact-state detached retirement
282315

283316
Ordinary removal, forced removal, and `--if-lossless` still refuse a detached

‎src/agents/worktrees/registry.ts‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
runOpenClawStateWriteTransaction,
1313
} from "../../state/openclaw-state-db.js";
1414
import {
15+
getRegistryWorktreeProvisionedStateInDatabase,
1516
listRegistryWorktreesInDatabase,
1617
rowToRecord,
1718
WORKTREE_RECORD_COLUMNS,
@@ -356,15 +357,73 @@ export function updateRegistryWorktree(
356357
);
357358
}
358359

360+
/** Retired snapshots cannot discard provisioned data or another lifecycle's custody. */
361+
function assertSnapshotRetirementInDatabase(
362+
db: DatabaseSync,
363+
observed: ManagedWorktreeRecord,
364+
): void {
365+
const row = executeSqliteQuerySync(
366+
db,
367+
kyselyFor(db).selectFrom("worktrees").selectAll().where("id", "=", observed.id),
368+
).rows[0];
369+
if (
370+
observed.removedAt === undefined ||
371+
!row ||
372+
JSON.stringify(rowToRecord(row)) !== JSON.stringify(observed)
373+
) {
374+
throw new Error("Worktree snapshot retirement identity changed");
375+
}
376+
const provisioned = getRegistryWorktreeProvisionedStateInDatabase(db, observed.id);
377+
const chunk = executeSqliteQuerySync(
378+
db,
379+
kyselyProvisionedFor(db)
380+
.selectFrom("worktree_provisioned_file_chunks")
381+
.select("worktree_id")
382+
.where("worktree_id", "=", observed.id)
383+
.limit(1),
384+
).rows[0];
385+
if (provisioned === undefined || provisioned.length !== 0 || chunk) {
386+
throw new Error("Worktree snapshot retains provisioned data; retain its custody");
387+
}
388+
const leases = collectLiveRunLeases(
389+
db,
390+
kyselyLeaseFor(db),
391+
worktreeRunLeaseScope(observed.id),
392+
{},
393+
);
394+
if (leases.liveCount !== 0 || leases.removingToken !== undefined) {
395+
throw new Error("Worktree snapshot has an active or unresolved run/removal consumer");
396+
}
397+
}
398+
399+
export function assertRegistrySnapshotRetirement(
400+
env: NodeJS.ProcessEnv,
401+
observed: ManagedWorktreeRecord,
402+
): void {
403+
runOpenClawStateWriteTransaction(({ db }) => assertSnapshotRetirementInDatabase(db, observed), {
404+
env,
405+
});
406+
}
407+
359408
export function deleteRegistryWorktree(
360409
env: NodeJS.ProcessEnv,
361410
id: string,
362-
options: { assertCurrent?: () => void; removalToken?: string } = {},
411+
options: {
412+
assertCurrent?: () => void;
413+
removalToken?: string;
414+
expectedRetired?: ManagedWorktreeRecord;
415+
} = {},
363416
): void {
364417
runOpenClawStateWriteTransaction(
365418
({ db }) => {
366419
// Validate before deleting either the record or its provisioned recovery chunks.
367420
options.assertCurrent?.();
421+
if (options.expectedRetired) {
422+
if (options.expectedRetired.id !== id) {
423+
throw new Error("Worktree snapshot retirement ID changed");
424+
}
425+
assertSnapshotRetirementInDatabase(db, options.expectedRetired);
426+
}
368427
assertRegistryMutationCustody(db, kyselyLeaseFor(db), id, options.removalToken);
369428
executeSqliteQuerySync(
370429
db,

0 commit comments

Comments
 (0)