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(cos): recheck investigation origins before reaping

[._.]/ Adam Eivy9c663cbc8599
diff --git a/server/services/cosTaskStore.js b/server/services/cosTaskStore.js
index b4e6e69d02..0511210d0f 100644
--- a/server/services/cosTaskStore.js
+++ b/server/services/cosTaskStore.js
@@ -1233,22 +1233,36 @@ export async function sweepResolvedFailureTasks({
let staleBlocks = 0;
let investigations = 0;
for (const { task, type, reason } of targets) {
- // Guard the flip under the write lock: a Resume/Run or dequeue that landed
- // after the snapshot must not be overwritten by a stale auto-expire (#12059).
- const updated = await updateTask(task.id, {
- status: 'completed',
- metadata: { resolution: 'auto-expired', autoExpiredReason: reason, autoExpiredAt: new Date(now).toISOString() }
- }, type, {
- now,
- expectedStatus: task.status,
- // A revived task can fail again before this write and return to blocked.
- // Preserve the category and every age fallback that justified this sweep.
- expectedMetadata: reason === 'stale-failure-block' ? {
- blockedCategory: task.metadata?.blockedCategory,
- blockedAt: task.metadata?.blockedAt,
- lastFailureAt: task.metadata?.lastFailureAt,
- updatedAt: task.metadata?.updatedAt,
- } : null,
+ // Re-read BOTH queues and re-evaluate the reason under the same lock as the
+ // write. Status/metadata pins protect a target from its own stale snapshot,
+ // but an investigation also depends on its origins: one can be resumed after
+ // this sweep's snapshot while the investigation itself stays pending (#12059).
+ const updated = await withStateLock(async () => {
+ const current = await getAllTasks();
+ const currentTask = (type === 'user' ? current.user.tasks : current.cos.tasks)
+ .find((candidate) => candidate.id === task.id);
+ if (!currentTask) return { statusChanged: true };
+ const currentById = new Map([...current.user.tasks, ...current.cos.tasks].map((candidate) => [candidate.id, candidate]));
+ const currentReason = isReapableBlockedFailure(currentTask, { now, maxAgeMs })
+ ? 'stale-failure-block'
+ : (isReapableInvestigation(currentTask, currentById) ? 'investigation-resolved' : null);
+ if (currentReason !== reason) return { statusChanged: true, task: currentTask };
+
+ return writeTaskUpdateLocked(task.id, {
+ status: 'completed',
+ metadata: { resolution: 'auto-expired', autoExpiredReason: reason, autoExpiredAt: new Date(now).toISOString() }
+ }, type, {
+ now,
+ expectedStatus: task.status,
+ // A revived task can fail again before this write and return to blocked.
+ // Preserve the category and every age fallback that justified this sweep.
+ expectedMetadata: reason === 'stale-failure-block' ? {
+ blockedCategory: task.metadata?.blockedCategory,
+ blockedAt: task.metadata?.blockedAt,
+ lastFailureAt: task.metadata?.lastFailureAt,
+ updatedAt: task.metadata?.updatedAt,
+ } : null,
+ });
});
if (updated?.error || updated?.statusChanged) continue;
if (reason === 'stale-failure-block') staleBlocks++; else investigations++;
diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.js
index 3a341800bf..29a35a1a0a 100644
--- a/server/services/cosTaskStore.test.js
+++ b/server/services/cosTaskStore.test.js
@@ -2468,6 +2468,37 @@ describe('cosTaskStore — stale failure-artifact reaper (#2619)', () => {
expect(statuses).toEqual(['completed', 'in_progress']);
});
+ it('does not reap an investigation after its completed origin is resumed between flips', async () => {
+ await addTask({ description: 'stale failure', id: 'task-race-origin-block', priority: 'HIGH' }, 'user', { now: NOW });
+ await updateTask('task-race-origin-block', {
+ status: 'blocked', metadata: { blockedCategory: 'max-retries', blockedAt: daysAgo(20) },
+ }, 'user', { now: NOW });
+ await addTask({ description: 'completed origin', id: 'task-race-origin', priority: 'HIGH' }, 'user', { now: NOW });
+ await updateTask('task-race-origin', { status: 'completed' }, 'user', { now: NOW });
+ await addTask({
+ description: '[Auto] Investigate agent failure [race]: origin was completed',
+ id: 'sys-race-origin-investigation',
+ isInvestigation: true,
+ affectedTasks: ['task-race-origin'],
+ }, 'internal', { now: NOW });
+
+ // The first user-queue flip lands before the investigation's planned
+ // internal-queue flip. Resume the origin in that gap; the investigation
+ // must be checked against this fresh origin state, not the sweep snapshot.
+ mock.afterWrite = (path) => {
+ if (path !== USER_FILE) return false;
+ const tasks = parseTasksMarkdown(mock.files.get(path));
+ tasks.find(task => task.id === 'task-race-origin').status = 'pending';
+ mock.files.set(path, generateTasksMarkdown(tasks));
+ mock.mtimes.set(path, (mock.mtimes.get(path) || 0) + 1000);
+ return true;
+ };
+
+ const result = await sweepResolvedFailureTasks({ now: NOW });
+ expect(result).toMatchObject({ reaped: 1, staleBlocks: 1, investigations: 0 });
+ expect((await getCosTasks()).tasks.find(task => task.id === 'sys-race-origin-investigation').status).toBe('pending');
+ });
+
it.each([
['blockedAt', new Date(NOW).toISOString()],
['blockedCategory', 'provider-config'],