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

test: check retireDeadAgent reads the sentinel before removing it (#11885)

Adam Eivy798600a2d4f7
diff --git a/server/services/agentFinalization.outputHooks.test.js b/server/services/agentFinalization.outputHooks.test.js
index 82fa5b306e..175ecaaac2 100644
--- a/server/services/agentFinalization.outputHooks.test.js
+++ b/server/services/agentFinalization.outputHooks.test.js
@@ -84,10 +84,6 @@ import { MAX_TASK_RETRIES, resolveFailedTaskUpdate } from './agentErrorAnalysis.
const __dirname = dirname(fileURLToPath(import.meta.url));
const MANAGEMENT_SOURCE = readFileSync(join(__dirname, 'agentManagement.js'), 'utf8');
const LIFECYCLE_SOURCE = readFileSync(join(__dirname, 'agentLifecycle.js'), 'utf8');
-// The orphan sweep and post-restart recovery both retire through the shared
-// `retireDeadAgent` (#8440) now, so its own ordering is the thing to pin — see
-// the source-text tests just below.
-const FINALIZATION_SOURCE = readFileSync(join(__dirname, 'agentFinalization.js'), 'utf8');
const TASK = {
id: 'sys-example',
taskType: 'internal',
@@ -549,15 +545,6 @@ describe('recovery path wiring (#3182, #8440)', () => {
// recovery in agentLifecycle.js) call the shared `retireDeadAgent` (#8440) for
// this step list now, so the ordering guard moved to pin ITS body instead of
// each caller's inlined copy.
- it('retireDeadAgent dispatches the output hook before marking the agent complete', () => {
- const start = FINALIZATION_SOURCE.indexOf('export async function retireDeadAgent');
- expect(start, 'retireDeadAgent must exist').toBeGreaterThan(-1);
- const end = FINALIZATION_SOURCE.indexOf('\n}', start);
- const body = FINALIZATION_SOURCE.slice(start, end);
- expect(body.indexOf('dispatchRecoveredTaskOutputHook({')).toBeGreaterThan(-1);
- expect(body.indexOf('dispatchRecoveredTaskOutputHook({')).toBeLessThan(body.indexOf('await completeAgent(agent.id'));
- });
-
it('both recovery paths retire through the shared retireDeadAgent', () => {
const sweepStart = MANAGEMENT_SOURCE.indexOf('async function runCleanupOrphanedAgents');
const sweepBody = MANAGEMENT_SOURCE.slice(sweepStart, sweepStart + 12_000);
diff --git a/server/services/agentFinalization.retireDeadAgent.test.js b/server/services/agentFinalization.retireDeadAgent.test.js
index 7b4567c218..848a950b94 100644
--- a/server/services/agentFinalization.retireDeadAgent.test.js
+++ b/server/services/agentFinalization.retireDeadAgent.test.js
@@ -57,6 +57,7 @@ vi.mock('./codeReview.js', async (importOriginal) => ({
import { completeAgent } from './cosAgentLifecycle.js';
import { removeCompletionSentinel } from './agentCompletionCleanup.js';
+import { getTaskOutputHook } from './taskTypeHooks.js';
import { completeAgentRun } from './agentRunTracking.js';
import { retireDeadAgent } from './agentFinalization.js';
import { PRIVATE_SECURITY_TASK_TYPE } from '../lib/privateSecurityPolicy.js';
@@ -121,10 +122,23 @@ describe('retireDeadAgent (#8440)', () => {
executionInterruption, errorAnalysis: expect.objectContaining({ category: 'content-refusal' }) }));
});
- it('removes the completion sentinel before checking for a runId', async () => {
- await retireDeadAgent({ agent: AGENT, task: TASK, success: false, exitCode: 1, duration: 0 });
+ it('dispatches the output hook before removing the sentinel, and the run and agent records close after both', async () => {
+ const order = [];
+ getTaskOutputHook.mockResolvedValueOnce(async () => { order.push('hook'); return { accepted: true }; });
+ removeCompletionSentinel.mockImplementation(async () => { order.push('sentinel-removed'); });
+ completeAgentRun.mockImplementation(async () => { order.push('run'); });
+ completeAgent.mockImplementation(async () => { order.push('agent'); });
+
+ await retireDeadAgent({
+ agent: { ...AGENT, metadata: { runId: 'run-1' } },
+ task: TASK,
+ success: false,
+ exitCode: 1,
+ duration: 0,
+ });
- expect(removeCompletionSentinel).toHaveBeenCalledWith({ agentId: 'agent-dead', agentState: AGENT });
+ expect(removeCompletionSentinel).toHaveBeenCalledWith({ agentId: 'agent-dead', agentState: expect.objectContaining({ id: 'agent-dead' }) });
+ expect(order).toEqual(['hook', 'sentinel-removed', 'run', 'agent']);
});
// No immutable source inventory survives to validate a recovered private-security