Skip to content

Commit 27059e3

Browse files
committed
Preserve cleanup cancellation reasons
1 parent bf65dec commit 27059e3

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎agents/adapters/supervisor.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const diagnosticFor = (reason: StopReason, detail?: string) => Buffer.from(
6262
const retainCleanupOwnership = (profile: ContainerProfile, detail: string, register = true): InvocationHandle => {
6363
const invocation = assertPhasePolicy(profile.policy);
6464
let resolveSettled!: (result: InvocationResult) => void, cleaning = false, complete = false;
65+
let cancelReason: StopReason | undefined;
6566
let handle!: InvocationHandle;
6667
let timer: ReturnType<typeof setTimeout> | undefined;
6768
const settled = new Promise<InvocationResult>(resolve => { resolveSettled = resolve; });
@@ -81,8 +82,8 @@ const retainCleanupOwnership = (profile: ContainerProfile, detail: string, regis
8182
activeProfiles.delete(profile);
8283
cleanupRecoveries.delete(handle);
8384
resolveSettled(Object.freeze({ attemptId: invocation.attemptId, context: invocation.context,
84-
exitCode: null, signal: null, stopReason: 'capture-failure', stdout: '',
85-
stderr: diagnosticFor('capture-failure', detail).toString('utf8') }));
85+
exitCode: null, signal: null, stopReason: cancelReason ?? 'capture-failure', stdout: '',
86+
stderr: diagnosticFor(cancelReason ?? 'capture-failure', detail).toString('utf8') }));
8687
} catch {
8788
cleaning = false;
8889
schedule();
@@ -91,7 +92,12 @@ const retainCleanupOwnership = (profile: ContainerProfile, detail: string, regis
9192
cleaning = false;
9293
};
9394
handle = Object.freeze({ attemptId: invocation.attemptId, settled,
94-
cancel: () => { if (timer) clearTimeout(timer); timer = undefined; retry(); } });
95+
cancel: (reason: StopReason) => {
96+
cancelReason ??= reason;
97+
if (timer) clearTimeout(timer);
98+
timer = undefined;
99+
retry();
100+
} });
95101
activeProfiles.add(profile);
96102
if (register) active.set(invocation.attemptId, handle);
97103
else cleanupRecoveries.add(handle);
@@ -111,6 +117,7 @@ export function retainSetupCleanup(invocation: InvocationInput, retryCleanup: ()
111117
startupError: unknown, cleanupError: unknown, kind = 'setup cleanup'): InvocationHandle {
112118
const register = !ownsAttempt(invocation.attemptId);
113119
let resolveSettled!: (result: InvocationResult) => void, cleaning = false, complete = false;
120+
let cancelReason: StopReason | undefined;
114121
let handle!: InvocationHandle;
115122
let timer: ReturnType<typeof setTimeout> | undefined;
116123
const settled = new Promise<InvocationResult>(resolve => { resolveSettled = resolve; });
@@ -126,8 +133,8 @@ export function retainSetupCleanup(invocation: InvocationInput, retryCleanup: ()
126133
if (active.get(invocation.attemptId) === handle) active.delete(invocation.attemptId);
127134
cleanupRecoveries.delete(handle);
128135
resolveSettled(Object.freeze({ attemptId: invocation.attemptId, context: invocation.context,
129-
exitCode: null, signal: null, stopReason: 'capture-failure', stdout: '',
130-
stderr: diagnosticFor('capture-failure', detail).toString('utf8') }));
136+
exitCode: null, signal: null, stopReason: cancelReason ?? 'capture-failure', stdout: '',
137+
stderr: diagnosticFor(cancelReason ?? 'capture-failure', detail).toString('utf8') }));
131138
} catch {
132139
cleaning = false;
133140
if (!timer) {
@@ -138,7 +145,12 @@ export function retainSetupCleanup(invocation: InvocationInput, retryCleanup: ()
138145
cleaning = false;
139146
};
140147
handle = Object.freeze({ attemptId: invocation.attemptId, settled,
141-
cancel: () => { if (timer) clearTimeout(timer); timer = undefined; retry(); } });
148+
cancel: (reason: StopReason) => {
149+
cancelReason ??= reason;
150+
if (timer) clearTimeout(timer);
151+
timer = undefined;
152+
retry();
153+
} });
142154
if (register) active.set(invocation.attemptId, handle);
143155
else cleanupRecoveries.add(handle);
144156
timer = setTimeout(() => { timer = undefined; retry(); }, 1_000);

‎test/agent-supervisor.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { join } from 'node:path';
55
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
66
import { startClaudeInvocation } from '../agents/adapters/claude.ts';
77
import { readCodexOutput, startCodexInvocation } from '../agents/adapters/codex.ts';
8-
import { isInvocationActive, readBoundedContainerFile, startProfileInvocation } from '../agents/adapters/supervisor.ts';
8+
import { isInvocationActive, readBoundedContainerFile, retainSetupCleanup,
9+
startProfileInvocation } from '../agents/adapters/supervisor.ts';
910
import { captureInvocation, type InvocationInput } from '../agents/contract.ts';
1011
import { buildAgentImage } from '../agents/container/image.ts';
1112
import { createContainerProfile, disposeContainerProfile, type ContainerProfile } from '../agents/container/profile.ts';
@@ -62,6 +63,23 @@ afterAll(() => {
6263
}, 3 * 60_000);
6364

6465
describe('container invocation supervisor', () => {
66+
it('preserves the first cancellation reason while retained setup cleanup settles', async () => {
67+
const data = fixture(), captured = invocation(data, 'cancel-setup-cleanup');
68+
let attempts = 0;
69+
const handle = retainSetupCleanup(captured, () => {
70+
attempts += 1;
71+
if (attempts === 1) return;
72+
throw new Error('unexpected repeated cleanup');
73+
}, new Error('startup failed'), new Error('cleanup failed'));
74+
handle.cancel('shutdown');
75+
handle.cancel('cancelled');
76+
const result = await handle.settled;
77+
expect(result.stopReason).toBe('shutdown');
78+
expect(result.stderr).toContain('[codeboost: shutdown:');
79+
expect(attempts).toBe(1);
80+
expect(isInvocationActive('cancel-setup-cleanup')).toBe(false);
81+
});
82+
6583
it('captures finite output and releases ownership only after cleanup', async () => {
6684
const current = profile(fixture(), 'finite-output', 'finite');
6785
const handle = startProfileInvocation(current);

0 commit comments

Comments
 (0)