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([cos-agent-output-write-amplification]): address review — close runner-batcher Map leak, drain-loop on flush

[._.]/ Adam Eivy4002adeec8ab
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index 4c2bea2bfa..ebc8d118ae 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -10,7 +10,7 @@ import { readFile, unlink, rm } from 'fs/promises';
import { existsSync } from 'fs';
import { execGit } from '../lib/execGit.js';
import { cosEvents, emitLog } from './cosEvents.js';
-import { registerAgent, updateAgent, completeAgent, appendAgentOutput } from './cosAgents.js';
+import { registerAgent, updateAgent, completeAgent } from './cosAgents.js';
import { getConfig, updateTask, addTask, getTaskById, checkStagePrecondition } from './cos.js';
import { spawnAgentViaRunner, getActiveAgentsFromRunner, getRunnerHealth } from './cosRunnerClient.js';
import { getActiveProvider, getAllProviders, getProviderById } from './providers.js';
diff --git a/server/services/cosAgents.js b/server/services/cosAgents.js
index 83aa646c43..7598b87585 100644
--- a/server/services/cosAgents.js
+++ b/server/services/cosAgents.js
@@ -451,12 +451,14 @@ export function createAgentOutputBatcher(agentId, { intervalMs = OUTPUT_FLUSH_IN
}
schedule();
},
- // Wait for any in-flight drain, then synchronously empty `pending`. A push
- // can race in during the awaited drain, so drain once more to be sure.
+ // Wait for any in-flight drain, then fully empty `pending`. A push can race
+ // in during an awaited drain, so loop until nothing is left rather than
+ // draining a fixed number of times (which could strand a late line to the
+ // debounce timer). flush() is only called once the producer has stopped, so
+ // the loop terminates promptly.
async flush() {
if (flushing) await flushing;
- await drain();
- if (pending.length > 0) await drain();
+ while (pending.length > 0) await drain();
},
};
}
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index 7fc51a3d35..17cb21bd0d 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -51,8 +51,12 @@ function getRunnerOutputBatcher(agentId) {
async function flushRunnerOutputBatcher(agentId) {
const batcher = runnerOutputBatchers.get(agentId);
if (!batcher) return;
- runnerOutputBatchers.delete(agentId);
+ // Flush BEFORE deleting: the agent is still in `runnerAgents` at this point
+ // (handleAgentCompletion removes it afterwards), so a line racing in during
+ // the awaited flush lands in this same batcher instead of orphaning a new
+ // one. The `agent:output` guard below drops any truly post-completion stray.
await batcher.flush();
+ runnerOutputBatchers.delete(agentId);
}
@@ -118,6 +122,12 @@ export async function initSpawner() {
// Set up event handlers for runner events
onCosRunnerEvent('agent:output', async (data) => {
const { agentId, text } = data;
+ // Drop output for an agent that's already finalized/removed. The runner
+ // registers the agent in `runnerAgents` before it spawns the process
+ // (agentLifecycle spawnViaRunner), so this never drops legitimate early
+ // output — it only ignores a stray event arriving after completion, which
+ // would otherwise lazily create a never-drained batcher (Map leak).
+ if (!runnerAgents.has(agentId)) return;
getRunnerOutputBatcher(agentId).push(text);
// Update phase on first output