Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: fence cleanup verification against explicit task retries

Adam Eivy217206ec3084
diff --git a/server/services/agentRepoStateVerification.js b/server/services/agentRepoStateVerification.js
index 292c87e7af..bf19e6ea34 100644
--- a/server/services/agentRepoStateVerification.js
+++ b/server/services/agentRepoStateVerification.js
@@ -254,7 +254,23 @@ const verificationRuns = new Map();
export async function verifyAgentRepoState(input) {
if (verificationRuns.has(input.agentId)) return verificationRuns.get(input.agentId);
const cleanupPending = input.agentState?.result?.cleanupPending === true;
- const run = verifyRunBranches({ ...input, success: input.success || cleanupPending }).then(async result => {
+ const verify = async () => {
+ if (cleanupPending) {
+ const { getTaskById } = await import('./cosTaskStore.js');
+ const current = await getTaskById(input.task.id);
+ if (!current) return skipped('cleanup-task-unavailable');
+ if (current.status !== 'in_progress' || current.metadata?.deliveryRecovery !== 'cleanup'
+ || current.metadata.deliveryRecoveryAgentId !== input.agentId) {
+ const { recordRepoStateVerification } = await import('./cosAgentLifecycle.js');
+ await recordRepoStateVerification(input.agentId, { status: 'completed', reasons: ['cleanup-owner-changed'] });
+ return skipped('cleanup-owner-changed');
+ }
+ input = { ...input, task: current };
+ }
+ return verifyRunBranches({ ...input, success: input.success || cleanupPending });
+ };
+ const run = verify().then(async result => {
+ if (result.skipReason === 'cleanup-owner-changed' || result.skipReason === 'cleanup-task-unavailable') return result;
const outcomes = result.branches || [result];
const unsupported = outcomes.find(item => [REPO_STATE_SKIPS.DISABLED, REPO_STATE_SKIPS.NOT_WORKTREE,
REPO_STATE_SKIPS.PERSISTENT_WORKTREE, REPO_STATE_SKIPS.DISCARDED_WORKTREE,
diff --git a/server/services/agentRepoStateVerification.test.js b/server/services/agentRepoStateVerification.test.js
index b7dec1e8a9..9b46e60a5a 100644
--- a/server/services/agentRepoStateVerification.test.js
+++ b/server/services/agentRepoStateVerification.test.js
@@ -148,8 +148,9 @@ beforeEach(() => {
findMissingCheckoutPreservation.mockResolvedValue(null);
inspectOrQueueCompletedReview.mockResolvedValue({ disposition: 'held', reason: 'delivery-ci-or-review-pending' });
readBranchCompletionPolicies.mockResolvedValue([]);
- getTaskById.mockResolvedValue(null);
updateTask.mockResolvedValue({ status: 'completed' });
+ getTaskById.mockResolvedValue({ ...task(), status: 'in_progress', metadata: { ...task().metadata,
+ deliveryRecovery: 'cleanup', deliveryRecoveryAgentId: 'agent-1' } });
recordBranchReconcileOutcome.mockResolvedValue({ retirementObligations: [{ ...RELEASE, createdAt: CREATED_AT }] });
prepareInventoriedCheckoutRetirement.mockResolvedValue({ ok: true, review: review() });
retireInventoriedCheckout.mockResolvedValue({ ok: true, archiveDir: '/private/archive', branchDeleted: true });
@@ -174,6 +175,15 @@ beforeEach(() => {
});
describe('verifyAgentRepoState — clean runs', () => {
+ it('retires the old verification owner after an explicit retry without touching Git', async () => {
+ getTaskById.mockResolvedValue({ ...task(), status: 'pending' });
+ const state = { ...agentState({ repoStateVerification: { status: 'pending' } }), result: { cleanupPending: true } };
+ expect(await run({ success: false, agentState: state })).toMatchObject({ skipReason: 'cleanup-owner-changed' });
+ expect(retireInventoriedCheckout).not.toHaveBeenCalled();
+ expect(updateTask).not.toHaveBeenCalled();
+ expect(recordRepoStateVerification).toHaveBeenLastCalledWith('agent-1', { status: 'completed', reasons: ['cleanup-owner-changed'] });
+ });
+
it('transfers unsupported cleanup verification back to bounded recovery', async () => {
updateTask.mockResolvedValueOnce({ status: 'in_progress' });
const state = { ...agentState({ isPersistentWorktree: true, repoStateVerification: { status: 'pending' } }),
@@ -800,6 +810,7 @@ describe('durable completion replay', () => {
expect(addTask).not.toHaveBeenCalled();
});
it('keeps missing policy pending with an updated retry time rather than granting authority or starving other audits', async () => {
+ getTaskById.mockResolvedValue(null);
readBranchCompletionPolicies.mockResolvedValue([archived()]);
await sweepRepoStateVerifications();
expect(recordRepoStateVerification).toHaveBeenCalledWith('agent-1', expect.objectContaining({ status: 'pending', reasons: ['task-unavailable'], checkedAt: expect.any(String) }));