Skip to content

Commit e4da939

Browse files
committed
fix(agents): scope stale cleanup outcomes to their observed lifecycle
A stale remover's retained/failed write raced a concurrent remove-plus- restore: the revived row is live again, so the live-row condition alone could stamp a prior-lifecycle outcome. Condition those writes on the activity stamp the remover observed; restore bumps lastActiveAt, making any prior-lifecycle write a no-op.
1 parent 0836780 commit e4da939

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎src/agents/worktrees/registry.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ export function updateRegistryWorktree(
401401
provisionedPaths?: readonly string[];
402402
provisionedState?: readonly ProvisionedFileState[];
403403
},
404-
options: { onlyIfLive?: boolean } = {},
404+
options: { onlyIfLive?: boolean; onlyIfActiveAt?: number } = {},
405405
): void {
406406
const db = dbFor(env);
407407
const values: Partial<WorktreeRow> = {};
@@ -424,13 +424,18 @@ export function updateRegistryWorktree(
424424
values.provisioned_paths_json = JSON.stringify(patch.provisionedPaths);
425425
}
426426
runOpenClawStateWriteTransaction(() => {
427-
const update = kyselyFor(db).updateTable("worktrees").set(values).where("id", "=", id);
428-
// Busy is authoritative only while the row is live; the conditional write cannot
429-
// overwrite a concurrent remover's terminal cleanup outcome after finalization.
430-
executeSqliteQuerySync(
431-
db,
432-
options.onlyIfLive ? update.where("removed_at", "is", null) : update,
433-
);
427+
let update = kyselyFor(db).updateTable("worktrees").set(values).where("id", "=", id);
428+
// Busy/retained/failed outcomes are authoritative only for the lifecycle the
429+
// writer observed: the live condition blocks post-finalization overwrites, and
430+
// the activity condition blocks prior-lifecycle writes after a concurrent
431+
// remove-plus-restore revives the row (restore bumps last_active_at).
432+
if (options.onlyIfLive) {
433+
update = update.where("removed_at", "is", null);
434+
}
435+
if (options.onlyIfActiveAt !== undefined) {
436+
update = update.where("last_active_at", "=", options.onlyIfActiveAt);
437+
}
438+
executeSqliteQuerySync(db, update);
434439
});
435440
}
436441

‎src/agents/worktrees/service.run-end-cleanup.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ describe("ManagedWorktreeService run-end cleanup outcomes", () => {
139139
});
140140
});
141141

142+
it("drops a prior-lifecycle outcome write after a concurrent remove and restore", async () => {
143+
const created = await materialize("aba-restore-race");
144+
const staleActiveAt = created.lastActiveAt;
145+
// Restore must move the activity stamp, so this test needs a ticking clock.
146+
let tick = now;
147+
const tickingService = new ManagedWorktreeService({ env, now: () => ++tick });
148+
await tickingService.acquire(created.id);
149+
await expect(tickingService.removeIfLossless(created.id)).resolves.toBe(true);
150+
const restored = await tickingService.restore({ id: created.id });
151+
expect(restored.lastActiveAt).not.toBe(staleActiveAt);
152+
153+
// A stale remover from the pre-restore lifecycle writes with the activity
154+
// stamp it observed (recordOutcome's condition); against the revived row it
155+
// must be a no-op instead of stamping a prior-lifecycle outcome.
156+
updateRegistryWorktree(
157+
env,
158+
created.id,
159+
{ runEndCleanup: { outcome: "retained-dirty", at: now + 1 } },
160+
{ onlyIfLive: true, onlyIfActiveAt: staleActiveAt },
161+
);
162+
163+
expect(getRegistryWorktree(env, created.id)?.runEndCleanup).toBeUndefined();
164+
});
165+
142166
it("records dirty retention and keeps the checkout intact", async () => {
143167
const created = await materialize("dirty");
144168
await service.acquire(created.id);

‎src/agents/worktrees/service.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,10 +1062,11 @@ export class ManagedWorktreeService {
10621062
const claimToken = randomUUID();
10631063
const recordOutcome = (outcome: ManagedWorktreeRunEndCleanupOutcome, error?: unknown) => {
10641064
// Retained/failed writes happen after this remover released or aborted its
1065-
// claim, so a racing remover may have finalized the row; the live-row
1066-
// condition keeps a winner's removed-lossless authoritative. The winning
1067-
// removal itself persists its outcome atomically inside remove()'s
1068-
// finalization update, never through this path.
1065+
// claim, so racing removers may have finalized the row, or removed AND
1066+
// restored it into a new lifecycle. The live condition blocks the first;
1067+
// conditioning on the activity stamp this remover observed blocks the
1068+
// second (restore bumps lastActiveAt). The winning removal persists its
1069+
// outcome atomically inside remove()'s finalization update, never here.
10691070
updateRegistryWorktree(
10701071
this.env,
10711072
id,
@@ -1078,7 +1079,7 @@ export class ManagedWorktreeService {
10781079
: {}),
10791080
},
10801081
},
1081-
{ onlyIfLive: true },
1082+
{ onlyIfLive: true, onlyIfActiveAt: record.lastActiveAt },
10821083
);
10831084
};
10841085
// Run-end cleanup must leave a durable outcome even when safety retains the checkout.

0 commit comments

Comments
 (0)