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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(cos): prevent spurious orphan retries from agent completion race
Adam Eivy9da9ab0eafa5
diff --git a/server/services/cos.js b/server/services/cos.jsindex f36cb809e0..d76c31aa79 100644--- a/server/services/cos.js+++ b/server/services/cos.js@@ -816,7 +816,18 @@ async function resetOrphanedTasks() {.filter(a => a.status === 'running').map(a => a.taskId);- emitLog('debug', `Running agents: ${runningAgentTaskIds.length}`, { taskIds: runningAgentTaskIds });+ // Also track tasks with recently-completed agents to avoid race condition:+ // Between completeAgent() and updateTask(), the agent is "completed" but the+ // task is still "in_progress". Without this check, resetOrphanedTasks treats+ // such tasks as orphaned and increments orphanRetryCount spuriously.+ const recentlyCompletedTaskIds = new Set(+ Object.values(state.agents)+ .filter(a => a.status === 'completed' && a.completedAt &&+ (Date.now() - new Date(a.completedAt).getTime()) < 60000)+ .map(a => a.taskId)+ );++ emitLog('debug', `Running agents: ${runningAgentTaskIds.length}, recently completed: ${recentlyCompletedTaskIds.size}`, { taskIds: runningAgentTaskIds });// Route orphaned tasks through handleOrphanedTask for consistent retry counting,// cooldown enforcement, and max-spawn limits (prevents runaway respawning)@@ -824,10 +835,15 @@ async function resetOrphanedTasks() {const processOrphanedTasks = async (tasks) => {for (const task of tasks) {- if (!runningAgentTaskIds.includes(task.id)) {- emitLog('info', `Found orphaned in_progress task ${task.id}, routing through retry handler`, { taskId: task.id });- await handleOrphanedTask(task.id, 'unknown-reset', getTaskById);+ if (runningAgentTaskIds.includes(task.id)) continue;+ // Skip tasks whose agent just completed — updateTask will set them to+ // completed shortly; treating them as orphaned causes spurious retries+ if (recentlyCompletedTaskIds.has(task.id)) {+ emitLog('debug', `Skipping task ${task.id} — agent recently completed, awaiting task status update`, { taskId: task.id });+ continue;}+ emitLog('info', `Found orphaned in_progress task ${task.id}, routing through retry handler`, { taskId: task.id });+ await handleOrphanedTask(task.id, 'unknown-reset', getTaskById);}};diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.jsindex c745e77d40..083610bf71 100644--- a/server/services/subAgentSpawner.js+++ b/server/services/subAgentSpawner.js@@ -3433,6 +3433,13 @@ export async function handleOrphanedTask(taskId, agentId, getTaskById) {return;}+ // Skip tasks already completed — prevents race where completeAgent() finishes+ // before updateTask() and a health check calls handleOrphanedTask in between+ if (task.status === 'completed') {+ emitLog('debug', `⏭️ Skipping orphaned task ${taskId} — already completed`, { taskId, agentId });+ return;+ }+// Check if the agent actually committed work before treating as orphanedconst commitFound = checkForTaskCommit(taskId);if (commitFound) {