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

refuse to retire the unscoped .agent-done when a run has no agent id

[._.]/ Adam Eivy412c2b3525e9
diff --git a/server/services/agentCompletionCleanup.js b/server/services/agentCompletionCleanup.js
index 2354006ebb..d3be827f36 100644
--- a/server/services/agentCompletionCleanup.js
+++ b/server/services/agentCompletionCleanup.js
@@ -499,6 +499,11 @@ async function advanceCreativeDirectorIfNeeded({ agentId, task, effectiveSuccess
* the agent record stamped at registration.
*/
export async function removeCompletionSentinel({ agentId, agent, agentState, workspacePath }) {
+ // A missing id makes `doneSentinelName` fall back to the bare, unscoped
+ // `.agent-done` — which belongs to no run and may be a legacy agent's live
+ // signal. The sweep protects it with an age floor for exactly that reason, so
+ // never let a resolution failure here aim at it.
+ if (typeof agentId !== 'string' || !agentId.trim()) return;
const workspace = workspacePath || agentState?.metadata?.workspacePath || agent?.workspacePath || null;
if (!workspace) return;
// `rmGuarded`, like the stale-sentinel sweep that deletes these same files.
diff --git a/server/services/agentCompletionCleanup.test.js b/server/services/agentCompletionCleanup.test.js
index d061f86fe7..2f7c6c1c19 100644
--- a/server/services/agentCompletionCleanup.test.js
+++ b/server/services/agentCompletionCleanup.test.js
@@ -38,7 +38,7 @@ vi.mock('./notifications.js', () => ({
vi.mock('./creativeDirector/completionHook.js', () => ({ handleCreativeDirectorCompletion: vi.fn().mockResolvedValue({}) }));
vi.mock('./taskPromptService.js', () => ({ getStagePrompt: vi.fn().mockResolvedValue('do stage work in {appName}') }));
-import { handlePipelineProgression, runAgentCompletionCleanup, runSpawnerCompletionCleanup } from './agentCompletionCleanup.js';
+import { handlePipelineProgression, runAgentCompletionCleanup, runSpawnerCompletionCleanup, removeCompletionSentinel } from './agentCompletionCleanup.js';
import { updateTask, addTask, reviveBlockedTask, getAgent } from './cos.js';
import { cleanupAgentWorktree, releaseRetryHold, spawnMergeRecoveryTask } from './agentWorktreeCleanup.js';
import { resolveReviewLoopOptions } from './codeReview.js';
@@ -717,3 +717,18 @@ describe('spawner sentinel removal without a persisted record', () => {
await rm(workspace, { recursive: true, force: true });
});
});
+
+// `doneSentinelName` falls back to the bare, unscoped `.agent-done` for a
+// missing id — a file that belongs to no run and that the stale sweep protects
+// with an age floor precisely because it can never be matched to one.
+describe('completion sentinel removal never targets the unscoped sentinel', () => {
+ it('does nothing when the run has no agent id', async () => {
+ const workspace = await mkdtemp(join(tmpdir(), 'completion-unscoped-'));
+ await writeFile(join(workspace, '.agent-done'), '## Summary\nA legacy run still going');
+
+ await removeCompletionSentinel({ agentId: ' ', workspacePath: workspace });
+
+ expect(await readFile(join(workspace, '.agent-done'), 'utf8')).toContain('still going');
+ await rm(workspace, { recursive: true, force: true });
+ });
+});
diff --git a/server/services/agentSentinelSweep.js b/server/services/agentSentinelSweep.js
index 2d316a6ae4..ddab5690c2 100644
--- a/server/services/agentSentinelSweep.js
+++ b/server/services/agentSentinelSweep.js
@@ -1,19 +1,23 @@
/**
* Stale `.agent-done-*` sentinel sweep.
*
- * A run that reaches an outcome deletes its own sentinel: every completion path
- * — the TUI spawner, the direct-CLI spawner, and the runner-event path — funnels
- * through `removeCompletionSentinel` in agentCompletionCleanup.js, and the
- * post-restart recovery in agentLifecycle.js removes it too. So in the happy
- * case nothing is left behind. What still can be:
+ * A run that reaches an outcome deletes its own sentinel, through
+ * `removeCompletionSentinel` in agentCompletionCleanup.js: the step list every
+ * live completion path shares (TUI spawner, direct-CLI spawner, runner event),
+ * plus the two recovery paths that retire a run without reaching it — the
+ * post-restart completion in agentLifecycle.js and the orphan sweep in
+ * agentManagement.js, which is where a hard kill (`pm2 restart`, SIGKILL,
+ * reboot) lands. So in the happy case nothing is left behind. What still can be:
*
- * - a hard kill (`pm2 restart`, SIGKILL, a machine reboot) never reaches any
- * cleanup at all;
+ * - a run whose record the orphan sweep never reaps (no durable record, or a
+ * workspace the record does not name);
* - a host shutdown ABANDONS the run rather than finalizing it (#3202), so no
* completion cleanup runs — the abandon gate only fires when the run wrote
* NO sentinel, but a race can still strand one;
* - a completion whose sentinel removal failed (an unreadable agent record,
- * a transient I/O error).
+ * a transient I/O error);
+ * - the bare unscoped `.agent-done`, which names no run, so no completion
+ * path will ever claim it.
*
* Worktree-less agents share a real checkout — the PortOS repo, or a managed
* app's own repo — so for them a skipped cleanup leaves an untracked file in the