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: prevent false orphan detection by checking PID before marking agents
Adam Eivye9eefa04a2fc
diff --git a/server/cos-runner/index.js b/server/cos-runner/index.jsindex e4d43ae087..65d65d7cab 100644--- a/server/cos-runner/index.js+++ b/server/cos-runner/index.js@@ -649,6 +649,7 @@ io.on('connection', (socket) => {/*** Cleanup orphaned agents on startup* Checks if PIDs from state are still running+ * Emits completion events for dead agents so main server can retry tasks*/async function cleanupOrphanedAgents() {const state = await loadState();@@ -658,8 +659,18 @@ async function cleanupOrphanedAgents() {// Check if process is still runningconst isRunning = await checkProcessRunning(agentInfo.pid);if (!isRunning) {- orphaned.push(agentId);+ orphaned.push({ agentId, taskId: agentInfo.taskId });delete state.agents[agentId];++ // Emit completion event so main server knows the agent died+ emitToServer('agent:completed', {+ agentId,+ taskId: agentInfo.taskId,+ exitCode: -1,+ success: false,+ orphaned: true,+ error: 'Agent process died (runner restart detected dead PID)'+ });}}@@ -695,16 +706,19 @@ async function checkProcessRunning(pid) {server.listen(PORT, HOST, async () => {console.log(`🤖 CoS Agent Runner started on http://${HOST}:${PORT}`);- // Cleanup orphaned agents- const orphaned = await cleanupOrphanedAgents();- if (orphaned.length > 0) {- console.log(`🧹 Cleaned ${orphaned.length} orphaned agent(s)`);- }-// Ensure agents directory existsif (!existsSync(AGENTS_DIR)) {await mkdir(AGENTS_DIR, { recursive: true });}++ // Delay orphan cleanup to allow socket connections to establish+ // This ensures completion events reach the main server for task retry+ setTimeout(async () => {+ const orphaned = await cleanupOrphanedAgents();+ if (orphaned.length > 0) {+ console.log(`🧹 Cleaned ${orphaned.length} orphaned agent(s)`);+ }+ }, 3000);});// Graceful shutdowndiff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.jsindex 976022e0f2..b399bd9616 100644--- a/server/services/subAgentSpawner.js+++ b/server/services/subAgentSpawner.js@@ -1154,6 +1154,18 @@ export async function killAllAgents() {// Max retries before creating investigation taskconst MAX_ORPHAN_RETRIES = 3;+/**+ * Check if a process is running by PID+ */+async function isPidAlive(pid) {+ if (!pid) return false;+ const { exec } = await import('child_process');+ const { promisify } = await import('util');+ const execAsync = promisify(exec);+ const result = await execAsync(`ps -p ${pid} -o pid= 2>/dev/null`).catch(() => ({ stdout: '' }));+ return result.stdout.trim() !== '';+}+/*** Clean up orphaned agents on startup* Agents marked as "running" in state but not tracked anywhere are orphaned@@ -1197,7 +1209,18 @@ export async function cleanupOrphanedAgents() {const inRemoteRunner = runnerActiveIds.has(agent.id);if (!inLocalDirect && !inLocalRunner && !inRemoteRunner) {- console.log(`🧹 Cleaning up orphaned agent ${agent.id}`);+ // Before marking as orphaned, check if the process is actually still running+ // This prevents false positives when servers restart but agent process survives+ if (agent.pid) {+ const stillAlive = await isPidAlive(agent.pid);+ if (stillAlive) {+ console.log(`🔄 Agent ${agent.id} (PID ${agent.pid}) still running, re-syncing to runner tracking`);+ runnerAgents.set(agent.id, { id: agent.id, pid: agent.pid, taskId: agent.taskId });+ continue;+ }+ }++ console.log(`🧹 Cleaning up orphaned agent ${agent.id} (PID ${agent.pid || 'unknown'} not running)`);await markComplete(agent.id, {success: false,error: 'Agent process was orphaned (server restart)',