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

refactor(cos): make getActiveAgentIds a side-effect-free agentState helper

Adam Eivy7cd9cfb323c4
diff --git a/server/services/agentState.js b/server/services/agentState.js
index 67bdf78e5b..2787f3a249 100644
--- a/server/services/agentState.js
+++ b/server/services/agentState.js
@@ -19,6 +19,13 @@ export const spawningTasks = new Set();
export let useRunner = false;
export const setUseRunner = (val) => { useRunner = val; };
+// Active agent IDs (direct-mode + runner-mode), for zombie/orphan detection.
+// Lives here in the side-effect-free state module so callers (cleanup jobs,
+// zombie sweeps) can read it without importing `subAgentSpawner.js`, whose
+// module load runs `initSpawner()` + schedules orphan cleanup. `subAgentSpawner`
+// re-exports it for backward compatibility.
+export const getActiveAgentIds = () => [...activeAgents.keys(), ...runnerAgents.keys()];
+
// Metadata booleans may arrive as true/'true' or false/'false' (JSON vs TASKS.md string round-trip)
export const isTruthyMeta = (value) => value === true || value === 'true';
export const isFalsyMeta = (value) => value === false || value === 'false';
diff --git a/server/services/autonomousJobs.js b/server/services/autonomousJobs.js
index 03073c2f36..b0d8bd7d7b 100644
--- a/server/services/autonomousJobs.js
+++ b/server/services/autonomousJobs.js
@@ -29,7 +29,7 @@ import { validateCommand, redactOutput, ALLOWED_COMMANDS_SORTED } from '../lib/c
import { getUserTimezone, getLocalParts, nextLocalTime } from '../lib/timezone.js'
import { parseCronToNextRun } from './eventScheduler.js'
import { cleanupOrphanedWorktrees } from './worktreeManager.js'
-import { activeAgents, runnerAgents } from './agentState.js'
+import { getActiveAgentIds } from './agentState.js'
/**
* Run the moltworld-explore.mjs script as a child process (no AI agent needed).
@@ -86,11 +86,10 @@ function runMoltworldExploration() {
*/
async function agentDataCleanup() {
// Active agent IDs so we never delete data / worktrees for running agents.
- // Read straight from the side-effect-free `agentState.js` maps rather than
- // importing `subAgentSpawner.js` (which runs `initSpawner()` + schedules
- // orphan cleanup at module load) — this is exactly what `getActiveAgentIds()`
- // returns, minus the import-time side effects.
- const activeIds = new Set([...activeAgents.keys(), ...runnerAgents.keys()])
+ // `getActiveAgentIds` lives in the side-effect-free `agentState.js`, so this
+ // script job doesn't pull in `subAgentSpawner.js` (and its module-load
+ // `initSpawner()` + scheduled orphan cleanup) just to read the maps.
+ const activeIds = new Set(getActiveAgentIds())
const agentsDir = join(PATHS.cos, 'agents')
let cleaned = 0
diff --git a/server/services/cosAgents.js b/server/services/cosAgents.js
index e5b4bb58bd..928c8d4d2c 100644
--- a/server/services/cosAgents.js
+++ b/server/services/cosAgents.js
@@ -594,8 +594,9 @@ async function isPidAlive(pid) {
// Cleanup zombie agents - agents marked as running but whose process is dead
export async function cleanupZombieAgents() {
- // Check local tracking maps
- const { getActiveAgentIds } = await import('./subAgentSpawner.js');
+ // Check local tracking maps (read from the side-effect-free state module, not
+ // subAgentSpawner — avoids triggering its module-load init from a zombie sweep).
+ const { getActiveAgentIds } = await import('./agentState.js');
const activeIds = getActiveAgentIds();
// Also check with the CoS runner for agents it's actively tracking
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index baed6229eb..e3fb8d60ad 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -16,8 +16,8 @@ import { onCosRunnerEvent, initCosRunnerConnection, isRunnerAvailable } from './
import { ensureDir, loadSlashdoFile, PATHS } from '../lib/fileUtils.js';
// ─── Shared state (imported from agentState.js) ──────────────────────────────
-export { activeAgents, runnerAgents, userTerminatedAgents, spawningTasks, useRunner, isTruthyMeta, isFalsyMeta } from './agentState.js';
-import { activeAgents, runnerAgents, setUseRunner } from './agentState.js';
+export { activeAgents, runnerAgents, userTerminatedAgents, spawningTasks, useRunner, isTruthyMeta, isFalsyMeta, getActiveAgentIds } from './agentState.js';
+import { runnerAgents, setUseRunner } from './agentState.js';
// ─── Sub-module re-exports ────────────────────────────────────────────────────
export { selectModelForTask } from './agentModelSelection.js';
@@ -42,16 +42,6 @@ export async function loadSlashdoCommand(commandName) {
return content;
}
-/**
- * Get list of active agent IDs (for zombie detection).
- * Includes both direct mode and runner mode agents.
- */
-export function getActiveAgentIds() {
- const directIds = Array.from(activeAgents.keys());
- const runnerIds = Array.from(runnerAgents.keys());
- return [...directIds, ...runnerIds];
-}
-
/**
* Initialize the spawner — listen for task:ready events.
*/