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: preserve TUI startup ownership during runner reconnect
Adam Eivye500f7a4f1aa
diff --git a/server/services/agentRunnerSync.js b/server/services/agentRunnerSync.jsindex b0c91d00d4..eb08c0c629 100644--- a/server/services/agentRunnerSync.js+++ b/server/services/agentRunnerSync.js@@ -15,7 +15,7 @@ import { maintenance } from '../lib/maintenanceAdmission.js';import { connectTuiSessionViaRunner, getActiveAgentsFromRunner, terminateAgentViaRunner } from './cosRunnerClient.js';import { runnerEntryShieldsRunningRecord } from '../lib/runnerAgentLiveness.js';import { isInternalTaskId } from '../lib/taskParser.js';-import { isAgentOwnedLocally, runnerAgents } from './agentState.js';+import { isAgentOwnedLocally, runnerAgents, spawningTasks } from './agentState.js';import { AGENT_RECORD_UNREADABLE, isLiveAgentRecord, readAgentRecordOrUnreadable } from './cosAgentLifecycle.js';import { appendRunEvent } from './agentRunEventLog.js';import * as shellService from './shell.js';@@ -102,6 +102,10 @@ export async function syncRunnerAgents() {// re-adopt — adopting it would put it in runnerAgents and the orphan// sweep would treat that local map as ownership. Live rows still sync.if (!(await runnerEntryShieldsRunningRecord(agent))) continue;+ // A reconnect can list a PTY while this process still awaits its spawn+ // acknowledgement/ledger write. Its controller owns startup already, even+ // though it has not registered in activeAgents yet.+ if (spawningTasks.has(agent.taskId)) continue;maintenance.recoverOwned('agent', agent.id);if (runnerAgents.get(agent.id)?.startupErrorAnalysis) {await stopInterruptedStartup(agent.id);diff --git a/server/services/agentRunnerSync.test.js b/server/services/agentRunnerSync.test.jsindex 0fc0a45e01..656d2a2b6a 100644--- a/server/services/agentRunnerSync.test.js+++ b/server/services/agentRunnerSync.test.js@@ -49,7 +49,7 @@ vi.mock('./agentRunEventLog.js', () => ({ appendRunEvent }));import { connectTuiSessionViaRunner, getActiveAgentsFromRunner, terminateAgentViaRunner } from './cosRunnerClient.js';import * as shellService from './shell.js';import { AGENT_RECORD_UNREADABLE, readAgentRecordOrUnreadable } from './cosAgentLifecycle.js';-import { activeAgents, runnerAgents } from './agentState.js';+import { activeAgents, runnerAgents, spawningTasks } from './agentState.js';import { syncRunnerAgents } from './agentRunnerSync.js';describe('syncRunnerAgents runner-owned TUI recovery', () => {@@ -57,6 +57,7 @@ describe('syncRunnerAgents runner-owned TUI recovery', () => {vi.clearAllMocks();runnerAgents.clear();activeAgents.clear();+ spawningTasks.clear();vi.mocked(shellService.getSession).mockReturnValue(null);vi.mocked(readAgentRecordOrUnreadable).mockResolvedValue({ id: 'agent-1', status: 'running' });});@@ -220,6 +221,7 @@ describe('syncRunnerAgents — reconnect boundary (#4540)', () => {vi.clearAllMocks();runnerAgents.clear();activeAgents.clear();+ spawningTasks.clear();vi.mocked(shellService.getSession).mockReturnValue(null);vi.mocked(readAgentRecordOrUnreadable).mockResolvedValue({ status: 'running', metadata: { runId: 'run-abc123' } });});@@ -303,4 +305,13 @@ describe('syncRunnerAgents — reconnect boundary (#4540)', () => {expect(terminateAgentViaRunner).toHaveBeenCalledTimes(2);expect(shellService.registerExternalSession).toHaveBeenCalledTimes(1);});++ it('leaves a late-acknowledged spawn with its live controller on reconnect', async () => {+ spawningTasks.add(survivingTui.taskId);+ getActiveAgentsFromRunner.mockResolvedValue([{ ...survivingTui, promptSubmitted: false }]);+ await expect(syncRunnerAgents()).resolves.toBe(0);+ expect(runnerAgents.has('agent-1')).toBe(false);+ expect(terminateAgentViaRunner).not.toHaveBeenCalled();+ expect(shellService.registerExternalSession).not.toHaveBeenCalled();+ });});