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 agent orphaning during rolling restarts
Adam Eivyff531a8cee6f
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.jsindex fded51274c..6150b23231 100644--- a/server/services/subAgentSpawner.js+++ b/server/services/subAgentSpawner.js@@ -15,7 +15,7 @@ import { homedir } from 'os';import { v4 as uuidv4 } from 'uuid';import { cosEvents, registerAgent, updateAgent, completeAgent, appendAgentOutput, getConfig, updateTask, addTask, emitLog } from './cos.js';import { startAppCooldown, markAppReviewCompleted } from './appActivity.js';-import { isRunnerAvailable, spawnAgentViaRunner, terminateAgentViaRunner, killAgentViaRunner, getAgentStatsFromRunner, initCosRunnerConnection, onCosRunnerEvent, getActiveAgentsFromRunner } from './cosRunnerClient.js';+import { isRunnerAvailable, spawnAgentViaRunner, terminateAgentViaRunner, killAgentViaRunner, getAgentStatsFromRunner, initCosRunnerConnection, onCosRunnerEvent, getActiveAgentsFromRunner, getRunnerHealth } from './cosRunnerClient.js';import { getActiveProvider } from './providers.js';import { recordSession, recordMessages } from './usage.js';import { buildPrompt } from './promptService.js';@@ -635,10 +635,44 @@ export async function spawnAgentForTask(task) {return spawnDirectly(agentId, task, prompt, workspacePath, selectedModel, provider, runId, claudePath, agentDir);}+/**+ * Minimum runner uptime (seconds) before spawning agents.+ * Prevents race condition during rolling restarts where server starts+ * before runner, spawns an agent, then runner restarts and orphans it.+ */+const RUNNER_MIN_UPTIME_SECONDS = 10;++/**+ * Wait for runner to be stable (sufficient uptime) before spawning+ */+async function waitForRunnerStability() {+ const maxWaitMs = 15000;+ const checkIntervalMs = 1000;+ const startTime = Date.now();++ while (Date.now() - startTime < maxWaitMs) {+ const health = await getRunnerHealth();+ if (health.available && health.uptime >= RUNNER_MIN_UPTIME_SECONDS) {+ return true;+ }+ if (health.available && health.uptime < RUNNER_MIN_UPTIME_SECONDS) {+ const waitTime = Math.ceil(RUNNER_MIN_UPTIME_SECONDS - health.uptime);+ emitLog('info', `Waiting ${waitTime}s for runner stability (uptime: ${Math.floor(health.uptime)}s)`, { uptime: health.uptime });+ }+ await new Promise(resolve => setTimeout(resolve, checkIntervalMs));+ }++ emitLog('warning', 'Runner stability check timed out, proceeding anyway', {});+ return false;+}+/*** Spawn agent via CoS Runner (isolated PM2 process)*/async function spawnViaRunner(agentId, task, prompt, workspacePath, model, provider, runId, claudePath) {+ // Wait for runner to be stable to prevent orphaned agents during rolling restarts+ await waitForRunnerStability();+// Store tracking info for runner-spawned agentsconst agentInfo = {taskId: task.id,