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([subagent-spawner-top-level-side-effects]): address codex/agy/claude review

[._.]/ Adam Eivyda2d88b7fee5
diff --git a/server/index.js b/server/index.js
index 3edce6bb6d..95015aee1e 100644
--- a/server/index.js
+++ b/server/index.js
@@ -348,9 +348,11 @@ initAutoFixer();
initTaskLearning();
// Initialize the CoS agent spawner (event wiring + orphan cleanup) explicitly,
-// now that the runner patch + task learning are ready. Fire-and-forget: the
-// spawner sets up its own listeners and recovers in-flight agents in the background.
-initSpawner().catch(err => {
+// now that the runner patch + task learning are ready. Capture the promise so
+// CoS auto-start can wait for the spawner's `task:ready` listener before it
+// emits (see cos.init below). The `.catch` resolves the chain even on failure,
+// so a spawner init error never blocks CoS init.
+const spawnerReady = initSpawner().catch(err => {
console.error(`❌ Failed to initialize spawner: ${err.message}`);
});
@@ -483,8 +485,13 @@ app.use('/api/ask', askRoutes);
// /api/image-gen can enqueue (otherwise persist() can race with ensureDir).
// Explicit call (not a module-level side effect) so test imports of cos.js
-// don't spin up its event listeners and timers.
-cos.init().catch(err => console.error(`❌ CoS init failed: ${err.message}`));
+// don't spin up its event listeners and timers. Gated on the spawner being
+// ready: CoS auto-start (alwaysOn) can emit `task:ready` for pending tasks
+// during init, which would be dropped if the spawner hadn't yet registered its
+// listener — so wait for `spawnerReady` before kicking off CoS init.
+spawnerReady
+ .then(() => cos.init())
+ .catch(err => console.error(`❌ CoS init failed: ${err.message}`));
// Initialize agent automation scheduler and action executor
automationScheduler.init().catch(err => console.error(`❌ Agent scheduler init failed: ${err.message}`));
diff --git a/server/services/agentState.js b/server/services/agentState.js
index 555bf218eb..4305e00b9c 100644
--- a/server/services/agentState.js
+++ b/server/services/agentState.js
@@ -25,9 +25,9 @@ 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.
+// zombie sweeps) can read it without importing the heavier `subAgentSpawner.js`
+// orchestrator (which re-exports the whole agent-lifecycle module graph).
+// `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)
diff --git a/server/services/autonomousJobs.js b/server/services/autonomousJobs.js
index ed3d95cd05..290608b5c5 100644
--- a/server/services/autonomousJobs.js
+++ b/server/services/autonomousJobs.js
@@ -87,8 +87,8 @@ function runMoltworldExploration() {
async function agentDataCleanup() {
// Active agent IDs so we never delete data / worktrees for running agents.
// `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.
+ // script job doesn't pull in the heavier `subAgentSpawner.js` orchestrator
+ // (which re-exports the whole agent-lifecycle module graph) just to read the maps.
const activeIds = new Set(getActiveAgentIds())
// Also protect PAUSED agents. Pausing intentionally preserves the agent's
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index 41ec8b5b44..d515d74ed2 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -73,10 +73,19 @@ export async function loadSlashdoCommand(commandName) {
return content;
}
+// Guard against double-init. Module import is side-effect-free now, so init is
+// an explicit call (server/index.js); the flag keeps a second call (e.g. a test
+// that re-invokes it, or a future re-init path) from double-binding the
+// cosEvents listeners and scheduling a duplicate orphan-cleanup timer.
+let spawnerInitialized = false;
+
/**
* Initialize the spawner — listen for task:ready events.
*/
export async function initSpawner() {
+ if (spawnerInitialized) return;
+ spawnerInitialized = true;
+
// Initialize provider status tracking
await initProviderStatus().catch(err => {
console.error(`⚠️ Failed to initialize provider status: ${err.message}`);