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: wake CoS dequeue when completed tasks return to pending (#12058)

Adam Eivyc94b4e2f90eb
diff --git a/server/lib/taskStatusTransition.js b/server/lib/taskStatusTransition.js
index 9fe5ba5456..b4674c2ee5 100644
--- a/server/lib/taskStatusTransition.js
+++ b/server/lib/taskStatusTransition.js
@@ -62,7 +62,7 @@ export function resolveTaskStatusTransition(previousStatus, nextStatus) {
// A revive: the task was parked and is spawnable again. Drives the `unblocked`
// action, which is what re-runs cos.init's dequeue (#2614).
- const isRevive = resultingStatus === 'pending' && previousStatus === 'blocked';
+ const isRevive = resultingStatus === 'pending' && (previousStatus === 'blocked' || previousStatus === 'completed');
// The one BACKWARD step in the lifecycle (#3376) — the orphan sweep and the
// retry-hold release both perform it, and the federated merge has to tell it
// apart from an ordinary edit landing on a peer's stale `pending` copy.
diff --git a/server/lib/taskStatusTransition.test.js b/server/lib/taskStatusTransition.test.js
index e0a9715801..a179129216 100644
--- a/server/lib/taskStatusTransition.test.js
+++ b/server/lib/taskStatusTransition.test.js
@@ -30,6 +30,15 @@ describe('resolveTaskStatusTransition', () => {
expect(t.isTerminal).toBe(false);
});
+ it('revives a completed task without treating it as leaving blocked (#12058)', () => {
+ const t = resolveTaskStatusTransition('completed', 'pending');
+ expect(t.action).toBe('unblocked');
+ expect(t.isRevive).toBe(true);
+ expect(t.leavesBlocked).toBe(false);
+ expect(t.isRequeue).toBe(false);
+ expect(t.isTerminal).toBe(false);
+ });
+
it('classifies a requeue (in_progress → pending)', () => {
const t = resolveTaskStatusTransition('in_progress', 'pending');
expect(t.action).toBe('requeued');
diff --git a/server/services/cos.dequeueGuard.test.js b/server/services/cos.dequeueGuard.test.js
index 839b7d23b3..d2d352071a 100644
--- a/server/services/cos.dequeueGuard.test.js
+++ b/server/services/cos.dequeueGuard.test.js
@@ -29,6 +29,7 @@ vi.mock('./cosState.js', async (importOriginal) => ({
const { init } = await import('./cos.js');
const { cosEvents } = await import('./cosEvents.js');
+const { resolveTaskStatusTransition } = await import('../lib/taskStatusTransition.js');
// One macrotask turn is enough: the listener schedules with setImmediate and the
// wrapper's .catch settles on the microtask queue right after.
@@ -53,6 +54,12 @@ describe('scheduleDequeue — rejections stay inside the cycle (#5644)', () => {
// Two independent event sources, so a guard added to only one of them fails.
cosEvents.emit('tasks:cos:added');
cosEvents.emit('tasks:changed', { action: 'approved' });
+ // A completed task returned to pending must wake this real listener (#12058).
+ cosEvents.emit('tasks:changed', {
+ action: resolveTaskStatusTransition('completed', 'pending').action,
+ previousStatus: 'completed',
+ task: { id: 'task-revived', status: 'pending' }
+ });
await drainScheduled();
process.off('unhandledRejection', onUnhandled);
@@ -61,7 +68,7 @@ describe('scheduleDequeue — rejections stay inside the cycle (#5644)', () => {
const logged = errorSpy.mock.calls
.map(args => args.join(' '))
.filter(line => line.includes('CoS dequeue cycle failed'));
- expect(logged).toHaveLength(2);
+ expect(logged).toHaveLength(3);
expect(logged[0]).toContain(STATE_ERROR);
});
});
diff --git a/server/services/cos.js b/server/services/cos.js
index d36fd02d0b..eb2a79b47c 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -1596,7 +1596,7 @@ export async function init() {
// the evaluation interval that's meant for system task generation.
// - 'approved': a newly approved task can now spawn — re-run dequeue. Either
// queue: a user row withheld by the #7300 hold is released the same way.
- // - 'unblocked': a blocked task flipped back to pending (revive/retry, #2614)
+ // - 'unblocked': a blocked or completed task flipped back to pending (#2614, #12058)
// is newly spawnable exactly like an approval — re-run dequeue.
// - 'requeued': an in_progress task flipped back to pending — a failed run's
// retry released from its cleanup hold (#3373) or an orphan sweep requeue.
diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.js
index f600613a93..2e73e064e3 100644
--- a/server/services/cosTaskStore.test.js
+++ b/server/services/cosTaskStore.test.js
@@ -1278,11 +1278,11 @@ describe('cosTaskStore.addTask', () => {
expect(second.duplicate).toBe(true);
});
- it('emits tasks:changed action "unblocked" on a blocked → pending flip (#2614)', async () => {
+ it.each(['blocked', 'completed'])('emits tasks:changed action "unblocked" on a %s → pending flip (#2614, #12058)', async (status) => {
// cos.init re-runs the dequeue on 'unblocked' (like 'approved') so a
// revived task spawns without waiting for an unrelated event or timer.
const task = await addTask({ description: 'revive me', app: 'portos', id: 'sys-revive' }, 'internal');
- await updateTask(task.id, { status: 'blocked', metadata: { blockedCategory: 'max-retries' } }, 'internal');
+ await updateTask(task.id, { status, metadata: { blockedCategory: 'max-retries' } }, 'internal');
mock.events.length = 0;
await updateTask(task.id, { status: 'pending' }, 'internal');
const evt = mock.events.find(e => e.name === 'tasks:changed');