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: release feature-agent run pointer when dispatch is held or spawn fails (#11763)

[._.]/ Adam Eivya11dc430f51d
diff --git a/server/services/featureAgents.js b/server/services/featureAgents.js
index 8cac04c7e4..f52264757c 100644
--- a/server/services/featureAgents.js
+++ b/server/services/featureAgents.js
@@ -657,15 +657,20 @@ cosEvents.on('agent:completed', async (agentData) => {
});
});
-// Provider/worktree setup can fail before a CoS agent record exists, so no
-// completion event will clear the temporary task pointer. Release that pointer
-// when the spawn path reports the task-level error.
-cosEvents.on('agent:error', async (agentData) => {
- if (!agentData?.taskId) return;
+async function releasePendingTask(taskId) {
+ if (!taskId) return;
const data = await readData().catch(() => null);
- const agent = data?.agents?.find(candidate => candidate.currentAgentId === agentData.taskId);
+ const agent = data?.agents?.find(candidate => candidate.currentAgentId === taskId);
if (!agent) return;
- await clearPendingAgentTask(agent.id, agentData.taskId).catch(err => {
+ await clearPendingAgentTask(agent.id, taskId).catch(err => {
console.log(`⚠️ Failed to clear feature agent pending task: ${err.message}`);
});
-});
+}
+
+// Provider/worktree setup can fail before a CoS agent record exists, so no
+// completion event will clear the temporary task pointer. Release that pointer
+// when the spawn path reports the task-level error. A held dispatch
+// (`task:held`) drops the unpersisted task the same way; lastRunAt and backoff
+// stay untouched so the agent is due again on the next dequeue.
+cosEvents.on('agent:error', (agentData) => releasePendingTask(agentData?.taskId));
+cosEvents.on('task:held', (held) => releasePendingTask(held?.taskId));
diff --git a/server/services/featureAgents.trigger.test.js b/server/services/featureAgents.trigger.test.js
index 461929b939..5224e252b5 100644
--- a/server/services/featureAgents.trigger.test.js
+++ b/server/services/featureAgents.trigger.test.js
@@ -229,3 +229,31 @@ describe('triggerFeatureAgent', () => {
expect(saved.agents[0].currentAgentId).toBeNull();
});
});
+
+describe('pending task pointer when dispatch never spawns', () => {
+ const pointer = async () => JSON.parse(await readFile(dataPath(), 'utf8')).agents[0].currentAgentId;
+
+ it.each([
+ ['task:held', (taskId) => ({ taskId, reason: 'local endpoint at capacity' })],
+ ['agent:error', (taskId) => ({ taskId, error: 'worktree creation failed' })]
+ ])('clears the pointer on %s so the agent is due and triggerable again', async (event, payload) => {
+ // Admission sets currentAgentId to the unpersisted task id; a held or failed
+ // spawn emits no agent lifecycle event, which used to wedge the agent forever.
+ const first = await triggerFeatureAgent(agent.id);
+ expect(first.triggered).toBe(true);
+ expect(await pointer()).toBe(first.taskId);
+
+ cosEvents.emit(event, payload(first.taskId));
+ await vi.waitFor(async () => expect(await pointer()).toBeNull());
+
+ const again = await triggerFeatureAgent(agent.id);
+ expect(again.triggered).toBe(true);
+ });
+
+ it('ignores a held task that belongs to no feature agent', async () => {
+ const first = await triggerFeatureAgent(agent.id);
+ cosEvents.emit('task:held', { taskId: 'other-task', reason: 'x' });
+ await new Promise(resolve => setTimeout(resolve, 20));
+ expect(await pointer()).toBe(first.taskId);
+ });
+});
diff --git a/server/services/subAgentSpawner.dispatchHolds.test.js b/server/services/subAgentSpawner.dispatchHolds.test.js
index 81ae6aa6de..d955790c1f 100644
--- a/server/services/subAgentSpawner.dispatchHolds.test.js
+++ b/server/services/subAgentSpawner.dispatchHolds.test.js
@@ -429,3 +429,25 @@ describe('subAgentSpawner — runner connection events', () => {
expect(dequeues).toHaveLength(1);
});
});
+
+describe('subAgentSpawner — release signals for unpersisted tasks', () => {
+ beforeEach(async () => {
+ await initSpawner();
+ vi.clearAllMocks();
+ setUseRunner(true);
+ isRunnerReachable.mockResolvedValue(true);
+ isUpdateInProgress.mockReturnValue(false);
+ });
+
+ it('emits task:held when a hold drops the task', async () => {
+ isRunnerReachable.mockResolvedValue(false);
+ await dispatch({ id: 'fa-run-1', metadata: {} });
+ expect(cosEvents.emit).toHaveBeenCalledWith('task:held', { taskId: 'fa-run-1', reason: 'CoS Runner is down' });
+ });
+
+ it('emits agent:error when spawnAgentForTask throws', async () => {
+ spawnAgentForTask.mockRejectedValueOnce(new Error('worktree failed'));
+ await dispatch({ id: 'fa-run-2', metadata: {} });
+ expect(cosEvents.emit).toHaveBeenCalledWith('agent:error', { taskId: 'fa-run-2', error: 'worktree failed' });
+ });
+});
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index 27ba74ac17..a32b7b0bc3 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -95,6 +95,9 @@ async function holdTask(task, reason) {
if (task.metadata?.jobId) {
cosEvents.emit('job:spawn-failed', { jobId: task.metadata.jobId, reason: 'held' });
}
+ // Unpersisted tasks (feature-agent runs) hold a pending pointer that only a
+ // lifecycle event clears; a held dispatch emits none of the usual ones.
+ cosEvents.emit('task:held', { taskId: task.id, reason });
}
/**
@@ -224,6 +227,8 @@ async function handleTaskReady(task) {
if (jobId) {
cosEvents.emit('job:spawn-failed', { jobId, reason: 'failed' });
}
+ // Same task-level error contract as the workspace-prep failure path.
+ cosEvents.emit('agent:error', { taskId: task.id, error: err?.message || String(err) });
} finally {
localSlot.release();
}