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(cos): prevent false orphan detection when agents complete successfully

Adam Eivyfeb995d77752
diff --git a/server/services/cos.js b/server/services/cos.js
index 0c826ce50f..376b0a10c0 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -846,6 +846,16 @@ async function resetOrphanedTasks() {
.map(a => a.taskId)
);
+ // Track tasks whose agents completed successfully — if handleAgentCompletion's
+ // updateTask call failed silently (e.g., file write race after server restart),
+ // we should complete the task here rather than treating it as orphaned.
+ const successfullyCompletedTaskIds = new Map();
+ for (const agent of Object.values(state.agents)) {
+ if (agent.status === 'completed' && agent.result?.success) {
+ successfullyCompletedTaskIds.set(agent.taskId, agent.id);
+ }
+ }
+
emitLog('debug', `Running agents: ${runningAgentTaskIds.length}, recently completed: ${recentlyCompletedTaskIds.size}`, { taskIds: runningAgentTaskIds });
// Route orphaned tasks through handleOrphanedTask for consistent retry counting,
@@ -861,6 +871,14 @@ async function resetOrphanedTasks() {
emitLog('debug', `Skipping task ${task.id} — agent recently completed, awaiting task status update`, { taskId: task.id });
continue;
}
+ // If the agent completed successfully but task wasn't updated (silent updateTask failure),
+ // complete the task now instead of treating it as orphaned
+ const successAgentId = successfullyCompletedTaskIds.get(task.id);
+ if (successAgentId) {
+ emitLog('warn', `🔧 Task ${task.id} still in_progress but agent ${successAgentId} completed successfully — completing task now (missed update)`, { taskId: task.id, agentId: successAgentId });
+ await updateTask(task.id, { status: 'completed' }, task.taskType || (task.id.startsWith('sys-') ? 'internal' : 'user'));
+ continue;
+ }
emitLog('info', `Found orphaned in_progress task ${task.id}, routing through retry handler`, { taskId: task.id });
await handleOrphanedTask(task.id, 'unknown-reset', getTaskById);
}
@@ -3686,6 +3704,7 @@ export async function updateTask(taskId, updates, taskType = 'user') {
: join(ROOT_DIR, state.config.cosTasksFile);
if (!existsSync(filePath)) {
+ console.log(`⚠️ updateTask: file not found for ${taskId} (taskType=${taskType}, path=${filePath})`);
return { error: 'Task file not found' };
}
@@ -3694,6 +3713,7 @@ export async function updateTask(taskId, updates, taskType = 'user') {
const taskIndex = tasks.findIndex(t => t.id === taskId);
if (taskIndex === -1) {
+ console.log(`⚠️ updateTask: task ${taskId} not found in ${filePath} (taskType=${taskType}, parsed ${tasks.length} tasks, status update: ${updates.status || 'none'})`);
return { error: 'Task not found' };
}
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index 293442b4b8..3cf081e223 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -1987,10 +1987,16 @@ async function handleAgentCompletion(agentId, exitCode, success, duration) {
// Update task status with retry tracking
if (effectiveSuccess) {
- await updateTask(task.id, { status: 'completed' }, task.taskType || 'user');
+ const result = await updateTask(task.id, { status: 'completed' }, task.taskType || 'user');
+ if (result?.error) {
+ emitLog('warn', `⚠️ Failed to mark task ${task.id} completed: ${result.error} (taskType=${task.taskType})`, { taskId: task.id, agentId, error: result.error });
+ }
} else {
const failedUpdate = await resolveFailedTaskUpdate(task, errorAnalysis, agentId);
- await updateTask(task.id, failedUpdate, task.taskType || 'user');
+ const result = await updateTask(task.id, failedUpdate, task.taskType || 'user');
+ if (result?.error) {
+ emitLog('warn', `⚠️ Failed to update failed task ${task.id}: ${result.error} (taskType=${task.taskType})`, { taskId: task.id, agentId, error: result.error });
+ }
// Handle provider status updates on failure
if (errorAnalysis) {