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: order the reconnect dequeue behind runner reconciliation (#4134)
Adam Eivy19475e7bfc99
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.jsindex 8de563890b..c0e999c3d3 100644--- a/server/services/subAgentSpawner.js+++ b/server/services/subAgentSpawner.js@@ -56,6 +56,10 @@ const RUNS_DIR = PATHS.runs;// Coalesce reconnect storms (a crash-looping runner) into one dequeue.const RECONNECT_DEQUEUE_DEBOUNCE_MS = 1000;let reconnectDequeueTimer = null;+// In-flight runner reconciliation, awaited by the reconnect dequeue so held+// tasks resume against a settled `runnerAgents` map — the ordering the boot+// path gets for free by awaiting recovery before it wires anything.+let runnerRecovery = Promise.resolve();/*** Decline a `task:ready` dispatch WITHOUT failing the task.@@ -193,6 +197,11 @@ async function runInitSpawner() {// here would silently convert every held task into a direct spawn — a child// of `portos-server` again — which is the orphaning runner mode exists to// prevent. The outage is a hold; it clears on `connection:ready` below.+ //+ // Drop any armed reconnect dequeue: a drop inside the debounce window would+ // otherwise still announce "resuming held agent tasks" and drive a dequeue+ // cycle into a runner that is gone again. A reconnect re-arms it.+ clearTimeout(reconnectDequeueTimer);emitLog('warn', '⏸️ CoS Runner disconnected — staying in runner mode, holding agent tasks until it returns');});@@ -208,14 +217,20 @@ async function runInitSpawner() {// in the CoS log stream the UI reads, or a remote install sees the outage// reported and never its resolution.emitLog('info', '🔼 CoS Runner came up — promoting agent spawning from direct to runner mode');- recoverRunnerAgents().catch(err =>- console.error(`❌ Failed to recover runner agents after promotion: ${err.message}`)- );+ runnerRecovery = recoverRunnerAgents();}clearTimeout(reconnectDequeueTimer);reconnectDequeueTimer = setTimeout(() => {- emitLog('info', '▶️ CoS Runner reconnected — resuming held agent tasks');- cosEvents.emit('cos:dequeue-requested');+ // Resume only once reconciliation has settled, so the dequeue counts the+ // agents the runner was already driving. `recoverRunnerAgents` swallows+ // its own failures, so this never rejects — which matters here, on a timer+ // callback outside any request lifecycle.+ runnerRecovery+ .then(() => {+ emitLog('info', '▶️ CoS Runner reconnected — resuming held agent tasks');+ cosEvents.emit('cos:dequeue-requested');+ })+ .catch(err => console.error(`❌ Failed to resume held agent tasks: ${err.message}`));}, RECONNECT_DEQUEUE_DEBOUNCE_MS);reconnectDequeueTimer.unref?.();});diff --git a/server/services/subAgentSpawner.runnerPromotion.test.js b/server/services/subAgentSpawner.runnerPromotion.test.jsindex 0d70d93df9..027280493a 100644--- a/server/services/subAgentSpawner.runnerPromotion.test.js+++ b/server/services/subAgentSpawner.runnerPromotion.test.js@@ -54,13 +54,16 @@ vi.mock('fs', () => ({ existsSync: vi.fn().mockReturnValue(false) }));import { initSpawner } from './subAgentSpawner.js';import { initCosRunnerConnection } from './cosRunnerClient.js';import { syncRunnerAgents } from './agentRunnerSync.js';-import { emitLog } from './cosEvents.js';+import { cosEvents, emitLog } from './cosEvents.js';import * as agentState from './agentState.js';// Read through the module namespace: `useRunner` is reassigned by `setUseRunner`,// and a destructured copy would freeze the value at import time.const currentMode = () => agentState.useRunner;+// Mirrors RECONNECT_DEQUEUE_DEBOUNCE_MS in the module under test.+const RECONNECT_DEQUEUE_DEBOUNCE_MS = 1000;+// Boot-time call counts, snapshotted before `vi.clearAllMocks()` erases them.const bootCalls = { initConnection: 0, sync: 0, mode: null };@@ -146,6 +149,38 @@ describe('subAgentSpawner — runner promotion (#4134)', () => {expect(currentMode()).toBe(true);});+ it('resumes held tasks only after reconciliation settles', async () => {+ let finishSync;+ vi.mocked(syncRunnerAgents).mockReturnValueOnce(new Promise(resolve => { finishSync = resolve; }));+ vi.useFakeTimers();++ runnerHandlers.get('connection:ready')();+ await vi.advanceTimersByTimeAsync(RECONNECT_DEQUEUE_DEBOUNCE_MS);++ // The debounce elapsed, but the runner has not finished listing its agents:+ // dequeuing now would size capacity against a half-populated map.+ expect(cosEvents.emit).not.toHaveBeenCalledWith('cos:dequeue-requested');++ finishSync(0);+ await vi.advanceTimersByTimeAsync(0);+ vi.useRealTimers();++ expect(cosEvents.emit).toHaveBeenCalledWith('cos:dequeue-requested');+ });++ it('drops an armed reconnect dequeue when the runner drops again inside the debounce window', async () => {+ vi.useFakeTimers();++ runnerHandlers.get('connection:ready')();+ runnerHandlers.get('connection:lost')();+ await vi.advanceTimersByTimeAsync(RECONNECT_DEQUEUE_DEBOUNCE_MS);+ vi.useRealTimers();++ // Otherwise the outage announces "resuming held agent tasks" and drives a+ // full dequeue cycle into a runner that is gone again.+ expect(cosEvents.emit).not.toHaveBeenCalledWith('cos:dequeue-requested');+ });+it('keeps runner mode when the connection drops, so tasks hold instead of spawning direct', () => {runnerHandlers.get('connection:ready')();expect(currentMode()).toBe(true);