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: resolve false 'unknown agent' warnings on app restart

Adam Eivyd1e6ecd434bb
diff --git a/.changelog/v0.11.x.md b/.changelog/v0.11.x.md
index 12aacd41e3..810ef489ab 100644
--- a/.changelog/v0.11.x.md
+++ b/.changelog/v0.11.x.md
@@ -195,6 +195,11 @@ This release focuses on security improvements, bug fixes for the AI runner syste
- **Removed noise patterns**: Eliminated pattern extractors for "no issues found", "already well-optimized", conclusion sections, and positive assessments — these were generating the bulk of low-value 0% confidence approval requests
- **Key test**: Every candidate memory now passes the question "Would a brand-new agent benefit from knowing this even with full code access?" — if it's about code, reject; if it's about the user, keep
+### Orphaned Agent Completion Warnings on Restart
+- **Eliminated false "unknown agent" warnings**: On app restart, the runner's orphan cleanup would emit completion events for dead agents, but the server's in-memory tracking map was already cleared — causing noisy `⚠️ Received completion for unknown agent` warnings for every finished agent
+- **Graceful state reconciliation**: `handleAgentCompletion()` now checks cos persistent state when an agent isn't in the in-memory map. If already completed by the server's own cleanup, it logs a quiet confirmation instead of a warning. If still marked as running, it properly completes the agent and updates its task.
+- **No more duplicate work**: The two parallel orphan cleanup paths (server at 2s, runner at 3s) now cooperate — whichever runs first handles the agent, and the second silently skips it
+
### Task Progress Bars Use P80 Estimates Instead of Average
- **More accurate progress**: Task progress bars now use a P80 (80th percentile approximation) estimate instead of the simple average duration, preventing progress bars from hitting 100% when only ~50% through a task
- **Variance tracking**: The learning system now tracks `maxDurationMs` per task type and computes `p80DurationMs` (avg + 60% of the gap to max) for more realistic progress estimates
diff --git a/server/services/subAgentSpawner.js b/server/services/subAgentSpawner.js
index 44b3057636..50c47060f0 100644
--- a/server/services/subAgentSpawner.js
+++ b/server/services/subAgentSpawner.js
@@ -1339,7 +1339,37 @@ async function spawnViaRunner(agentId, task, prompt, workspacePath, model, provi
async function handleAgentCompletion(agentId, exitCode, success, duration) {
const agent = runnerAgents.get(agentId);
if (!agent) {
- console.log(`⚠️ Received completion for unknown agent: ${agentId}`);
+ // Agent not in memory map (server restarted). Check cos state for context.
+ const { getAgent: getAgentState } = await import('./cos.js');
+ const cosAgent = await getAgentState(agentId).catch(() => null);
+ if (!cosAgent) {
+ console.log(`⚠️ Received completion for unknown agent: ${agentId} (not in cos state)`);
+ return;
+ }
+ if (cosAgent.status === 'completed') {
+ console.log(`✅ Agent ${agentId} already completed (handled by orphan cleanup)`);
+ return;
+ }
+ // Agent still running in cos state but not in memory - handle completion with cos data
+ console.log(`🔄 Completing untracked agent ${agentId} from cos state (post-restart)`);
+ await completeAgent(agentId, {
+ success,
+ exitCode,
+ duration,
+ orphaned: true,
+ error: success ? undefined : 'Agent completed after server restart'
+ });
+ if (cosAgent.taskId) {
+ const task = await getTaskById(cosAgent.taskId).catch(() => null);
+ if (task && task.status !== 'completed') {
+ const taskType = task.taskType || 'user';
+ if (success) {
+ await updateTask(cosAgent.taskId, { status: 'completed' }, taskType);
+ } else {
+ await updateTask(cosAgent.taskId, { status: 'ready', metadata: { retryReason: 'orphaned-agent' } }, taskType);
+ }
+ }
+ }
return;
}