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

address review (codex): exclude completed task from perpetual refill snapshot

Adam Eivy4aefb1daf2e7
diff --git a/server/services/cos.js b/server/services/cos.js
index 9c8a2e45a6..d08971dc9d 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -978,8 +978,20 @@ async function refillPerpetualForCompletedAgent(agent) {
const state = await loadState();
if (!canQueueImprovementTasks(state)) return;
+ // `agent:completed` fires from `completeAgent` BEFORE the completion flow's
+ // `updateTask` marks this agent's task done (agentLifecycle.js: completeAgent
+ // emits, THEN updateTask persists). So the just-finished task can still read as
+ // `in_progress` here — and `queueEligibleImprovementTasks` caps each app at one
+ // pending improvement, so it would treat this app as still-busy and skip the
+ // re-queue, defeating the back-to-back drain. Drop the completed task from the
+ // snapshot we hand it so eligibility is computed as if the task is already
+ // done; this is correct regardless of whether updateTask has landed yet.
const cosTaskData = await getCosTasks();
- await queueEligibleImprovementTasks(state, cosTaskData);
+ const completedTaskId = agent?.taskId;
+ const refillTaskData = completedTaskId
+ ? { ...cosTaskData, tasks: (cosTaskData.tasks || []).filter(t => t.id !== completedTaskId) }
+ : cosTaskData;
+ await queueEligibleImprovementTasks(state, refillTaskData);
setImmediate(() => dequeueNextTask());
}
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index d9080c6764..4fb304a8c6 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -1124,6 +1124,27 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {
'agent:completed handler must call refillPerpetualForCompletedAgent'
).toBe(true);
});
+
+ it('the refill excludes the just-completed task from the queueEligible snapshot (avoids the completeAgent-before-updateTask race)', () => {
+ // agent:completed fires before the completion flow's updateTask marks the
+ // task done, so the just-finished task can still read as in_progress. If the
+ // refill handed the raw cosTaskData to queueEligibleImprovementTasks, its
+ // one-pending-per-app cap would skip the app and the drain would stall —
+ // exactly the bug this whole change fixes. Pin the filter so a refactor
+ // can't silently reintroduce the race.
+ const fnIdx = COS_SRC.indexOf('async function refillPerpetualForCompletedAgent');
+ expect(fnIdx, 'refillPerpetualForCompletedAgent must exist').toBeGreaterThan(-1);
+ const fnSlice = COS_SRC.slice(fnIdx, fnIdx + 2500);
+ expect(
+ /filter\(\s*t\s*=>\s*t\.id\s*!==\s*completedTaskId\s*\)/.test(fnSlice),
+ 'refill must drop the completed task (agent.taskId) before queueEligibleImprovementTasks'
+ ).toBe(true);
+ // And the filtered snapshot — not the raw one — must be what is passed in.
+ expect(
+ /queueEligibleImprovementTasks\(\s*state\s*,\s*refillTaskData\s*\)/.test(fnSlice),
+ 'refill must pass the filtered refillTaskData to queueEligibleImprovementTasks'
+ ).toBe(true);
+ });
});
// Shared autonomous-queuing gate (cosState.canQueueImprovementTasks). Extracted