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: stop the zombie sweep from archiving agents whose spawn is still in flight

[._.]/ Adam Eivy32d03b96e803
diff --git a/server/services/cosAgentLifecycle.js b/server/services/cosAgentLifecycle.js
index 71ac5e604c..9064165b5f 100644
--- a/server/services/cosAgentLifecycle.js
+++ b/server/services/cosAgentLifecycle.js
@@ -668,7 +668,7 @@ export async function cleanupZombieAgents() {
// Direct-spawn handles in this process (not leftover runnerAgents adopts —
// those are only a handle, same as GET /agents). Read from the side-effect-free
// state module, not subAgentSpawner.
- const { activeAgents } = await import('./agentState.js');
+ const { activeAgents, spawningTasks } = await import('./agentState.js');
// Also check with the CoS runner for agents it's actively tracking
const { getActiveAgentsFromRunner } = await import('./cosRunnerClient.js');
@@ -694,6 +694,12 @@ export async function cleanupZombieAgents() {
// processActive from onExit/pid probe). Leftover runnerAgents ownership
// from an earlier adopt is not.
if (activeAgents.has(agent.id)) continue;
+ // The record is written before the spawn finishes (prompt build, claim
+ // update, forge/env lookups, PTY open), and that window can run for
+ // minutes under load — well past the pid grace below. While the dispatch
+ // still owns the task, a missing pid means 'not spawned yet', not 'never
+ // started'. Reaping here archives a run that goes on to launch normally.
+ if (agent.taskId && spawningTasks.has(agent.taskId)) continue;
const executionMode = agent.metadata?.executionMode;
const runnerOwned = agent.metadata?.useRunner === true
|| agent.metadata?.useRunner === 'true'
diff --git a/server/services/cosAgentLifecycle.test.js b/server/services/cosAgentLifecycle.test.js
index 06af3d085f..f7ac5cc998 100644
--- a/server/services/cosAgentLifecycle.test.js
+++ b/server/services/cosAgentLifecycle.test.js
@@ -64,7 +64,7 @@ import { recordDomainUsage } from './domainUsage.js';
import { cosEvents } from './cosEvents.js';
import { loadAgentIndex } from './cosAgentIndex.js';
import { getActiveAgentsFromRunner } from './cosRunnerClient.js';
-import { activeAgents, runnerAgents } from './agentState.js';
+import { activeAgents, runnerAgents, spawningTasks } from './agentState.js';
describe('cosAgentLifecycle', () => {
beforeEach(async () => {
@@ -670,12 +670,34 @@ describe('cleanupZombieAgents — runner listing is not proof of life', () => {
vi.mocked(getActiveAgentsFromRunner).mockResolvedValue([]);
activeAgents.clear();
runnerAgents.clear();
+ spawningTasks.clear();
});
afterEach(async () => {
+ spawningTasks.clear();
await rm(mockCosState.agentsDir, { recursive: true, force: true });
});
+ it('does not reap a pid-less record while its task is still being dispatched', async () => {
+ await writeFile(join(mockCosState.agentsDir, 'index.json'), '{}');
+ mockCosState.state.agents['agent-slow'] = {
+ id: 'agent-slow',
+ taskId: 'task-slow',
+ status: 'running',
+ startedAt: new Date(Date.now() - 5 * 60_000).toISOString(),
+ };
+ spawningTasks.add('task-slow');
+
+ expect((await cleanupZombieAgents()).cleaned).toEqual([]);
+ expect(mockCosState.state.agents['agent-slow'].status).toBe('running');
+
+ // Once the dispatch gives up the task without ever recording a pid, the
+ // record really is a never-started zombie again.
+ spawningTasks.delete('task-slow');
+ expect((await cleanupZombieAgents()).cleaned).toEqual(['agent-slow']);
+ expect(mockCosState.state.agents['agent-slow'].result.error).toMatch(/never started/);
+ });
+
it('does not reap a runner-owned TUI while the runner probe is unavailable', async () => {
mockCosState.state.agents['agent-tui'] = {
id: 'agent-tui',