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: orphan handler — skip already-blocked tasks to avoid duplicate auto-fix

[._.]/ Adam Eivy5a6b41d5fd7e
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 54436489d2..1eaa465d6e 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -373,6 +373,19 @@
## Fixed
+- **CoS orphan handler — no more duplicate `[Auto-Fix]` investigation tasks.**
+ When `cleanupOrphanedAgents` swept up two stale "running" agents that shared a
+ taskId, `handleOrphanedTask` ran once per agent. The first call blocked the
+ task with `blockedCategory='max-retries'` and spawned an investigation task;
+ the second call fell through the existing guard (which only short-circuits on
+ `'user-terminated'`), incremented `orphanRetryCount` past its ceiling, and
+ spawned a SECOND investigation task with the same `[Auto-Fix] Investigate
+ repeated agent orphaning for task <id>` headline (the `addTask` dedup at
+ `cos.js:2194` missed it because the description body embeds the per-agent
+ `retryCount`/`agentId`). The handler now also early-returns when the task is
+ already blocked with `'max-retries'` or `'orphan-cooldown'`. Adds
+ `agentManagement.test.js` covering all four short-circuit branches.
+
- **Media Jobs Queue — empty prompt tooltip.** Job rows without a prompt
no longer render an empty `title=""` tooltip. Rows with a prompt now
show the full prompt on hover (the visible cell is still truncated to
diff --git a/server/services/agentManagement.js b/server/services/agentManagement.js
index 2a9370dda7..757685ddc3 100644
--- a/server/services/agentManagement.js
+++ b/server/services/agentManagement.js
@@ -374,6 +374,19 @@ export async function handleOrphanedTask(taskId, agentId, getTaskByIdFn) {
return;
}
+ // If a prior orphan in this sweep already routed the task through this handler
+ // (max-retries → investigation task created, or orphan-cooldown → blocked until later),
+ // skip — otherwise each additional orphaned agent for the same task spawns a
+ // duplicate investigation task and inflates orphanRetryCount past its ceiling.
+ if (task.status === 'blocked' &&
+ (task.metadata?.blockedCategory === 'max-retries' ||
+ task.metadata?.blockedCategory === 'orphan-cooldown')) {
+ emitLog('info', `⏭️ Skipping orphaned task ${taskId} — already handled (${task.metadata.blockedCategory})`, {
+ taskId, agentId, blockedCategory: task.metadata.blockedCategory
+ });
+ return;
+ }
+
// Skip tasks already completed
if (task.status === 'completed') {
emitLog('debug', `⏭️ Skipping orphaned task ${taskId} — already completed`, { taskId, agentId });
diff --git a/server/services/agentManagement.test.js b/server/services/agentManagement.test.js
new file mode 100644
index 0000000000..2cba058bc9
--- /dev/null
+++ b/server/services/agentManagement.test.js
@@ -0,0 +1,141 @@
+/**
+ * Tests for handleOrphanedTask — guards that prevent duplicate investigation
+ * tasks when the same underlying task is orphaned by multiple agents in the
+ * same cleanup sweep.
+ *
+ * The bug: cleanupOrphanedAgents iterates over all stale "running" agents and
+ * calls handleOrphanedTask once per agent. If two agents shared a taskId, the
+ * first call would block the task with 'max-retries' and spawn an investigation
+ * task; the second call would see the (now-blocked) task, increment
+ * orphanRetryCount again, and spawn ANOTHER investigation task. The addTask
+ * dedup at cos.js:2194 doesn't catch it because the description body embeds
+ * per-agent retryCount/agentId, so the strings differ.
+ *
+ * The guard added at agentManagement.js:381 short-circuits handleOrphanedTask
+ * when the task is already blocked with 'max-retries' or 'orphan-cooldown'.
+ */
+
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+vi.mock('./cos.js', () => ({
+ updateTask: vi.fn().mockResolvedValue(true),
+ addTask: vi.fn().mockResolvedValue({ id: 'sys-mocked' }),
+ getTaskById: vi.fn(),
+ getAllTasks: vi.fn()
+}));
+
+vi.mock('./cosEvents.js', () => ({
+ emitLog: vi.fn()
+}));
+
+vi.mock('./agentRunTracking.js', () => ({
+ checkForTaskCommit: vi.fn().mockReturnValue(false)
+}));
+
+// Stub other transitive imports we don't exercise in handleOrphanedTask.
+vi.mock('./cosAgents.js', () => ({ completeAgent: vi.fn() }));
+vi.mock('./cosRunnerClient.js', () => ({
+ terminateAgentViaRunner: vi.fn(),
+ killAgentViaRunner: vi.fn(),
+ getAgentStatsFromRunner: vi.fn(),
+ getActiveAgentsFromRunner: vi.fn().mockResolvedValue([])
+}));
+vi.mock('./agents.js', () => ({ unregisterSpawnedAgent: vi.fn() }));
+vi.mock('./agentLifecycle.js', () => ({
+ cleanupAgentWorktree: vi.fn(),
+ syncRunnerAgents: vi.fn().mockResolvedValue(0)
+}));
+vi.mock('./worktreeManager.js', () => ({ cleanupOrphanedWorktrees: vi.fn() }));
+
+import { handleOrphanedTask } from './agentManagement.js';
+import { updateTask, addTask } from './cos.js';
+
+describe('handleOrphanedTask — duplicate-investigation guard', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it('skips tasks already blocked with blockedCategory=max-retries (no new investigation task)', async () => {
+ const blockedTask = {
+ id: 'task-foo',
+ status: 'blocked',
+ taskType: 'user',
+ description: 'Original work',
+ metadata: {
+ blockedCategory: 'max-retries',
+ blockedReason: 'orphan retries exceeded (3/3)',
+ orphanRetryCount: 3,
+ totalSpawnCount: 3
+ }
+ };
+ const getTaskById = vi.fn().mockResolvedValue(blockedTask);
+
+ await handleOrphanedTask('task-foo', 'agent-second', getTaskById);
+
+ expect(updateTask).not.toHaveBeenCalled();
+ expect(addTask).not.toHaveBeenCalled();
+ });
+
+ it('skips tasks already blocked with blockedCategory=orphan-cooldown', async () => {
+ const cooldownTask = {
+ id: 'task-foo',
+ status: 'blocked',
+ taskType: 'user',
+ description: 'Original work',
+ metadata: {
+ blockedCategory: 'orphan-cooldown',
+ cooldownUntil: new Date(Date.now() + 60000).toISOString(),
+ orphanRetryCount: 1
+ }
+ };
+ const getTaskById = vi.fn().mockResolvedValue(cooldownTask);
+
+ await handleOrphanedTask('task-foo', 'agent-second', getTaskById);
+
+ expect(updateTask).not.toHaveBeenCalled();
+ expect(addTask).not.toHaveBeenCalled();
+ });
+
+ it('still skips user-terminated tasks (preserves prior behavior)', async () => {
+ const terminatedTask = {
+ id: 'task-foo',
+ status: 'blocked',
+ taskType: 'user',
+ description: 'Original work',
+ metadata: { blockedCategory: 'user-terminated' }
+ };
+ const getTaskById = vi.fn().mockResolvedValue(terminatedTask);
+
+ await handleOrphanedTask('task-foo', 'agent-x', getTaskById);
+
+ expect(updateTask).not.toHaveBeenCalled();
+ expect(addTask).not.toHaveBeenCalled();
+ });
+
+ it('still processes a fresh in_progress task (resets to pending for retry)', async () => {
+ const inProgressTask = {
+ id: 'task-foo',
+ status: 'in_progress',
+ taskType: 'user',
+ description: 'Original work',
+ metadata: { orphanRetryCount: 0, totalSpawnCount: 1 }
+ };
+ const getTaskById = vi.fn().mockResolvedValue(inProgressTask);
+
+ await handleOrphanedTask('task-foo', 'agent-orphaned', getTaskById);
+
+ expect(updateTask).toHaveBeenCalledTimes(1);
+ expect(updateTask).toHaveBeenCalledWith(
+ 'task-foo',
+ expect.objectContaining({
+ status: 'pending',
+ metadata: expect.objectContaining({
+ orphanRetryCount: 1,
+ lastOrphanedAgentId: 'agent-orphaned'
+ })
+ }),
+ 'user'
+ );
+ expect(addTask).not.toHaveBeenCalled();
+ });
+});