Skip to content

Commit 4b1c0f7

Browse files
mchwangclaude
andcommitted
Treat a Docker client that never started as having created nothing
Review fix (Copilot, #69 summary): a failed create with no exit status was read as "the request may have reached the daemon". A spawn failure (ENOENT, EACCES, EAGAIN and similar) sent nothing, but it too was treated as unknown, so cleanup waited out a settle window and could report a never-created agent container, network or proxy as unreleased. One shared classifier (`createOutcomeUnknown`) now separates a daemon answer, a client that never started, and a killed client, for both the agent container and the egress objects. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 879e62a commit 4b1c0f7

5 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎agents/client-outcome.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** Spawn errors: the Docker client process never started, so it cannot have sent any request to the daemon. */
2+
const NOT_STARTED = new Set(['ENOENT', 'EACCES', 'EPERM', 'EAGAIN', 'EMFILE', 'ENFILE', 'ENOMEM', 'E2BIG', 'ENOEXEC']);
3+
4+
/**
5+
* Whether a failed Docker create may still have reached the daemon, so the object may exist. A numeric exit status
6+
* means the daemon answered (and refused); a client that never started sent nothing. Anything else, such as a client
7+
* killed at its deadline, leaves the outcome unknown.
8+
*/
9+
export function createOutcomeUnknown(error: unknown): boolean {
10+
const value = error as { status?: unknown; code?: unknown; cause?: { code?: unknown } } | undefined;
11+
if (typeof value?.status === 'number') return false;
12+
const code = typeof value?.code === 'string' ? value.code : value?.cause?.code;
13+
return !(typeof code === 'string' && NOT_STARTED.has(code));
14+
}

‎agents/container/run.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { execFileSync, spawnSync } from 'node:child_process';
22
import { realpathSync } from 'node:fs';
3+
import { createOutcomeUnknown } from '../client-outcome.ts';
34
import type { UnreleasedResource } from '../contract.ts';
45
import { assertContainerProfile, assertContainerProfileAuthenticity, disposeContainerProfile,
56
isContainerProfileAuthentic, profileTimeout,
@@ -363,7 +364,7 @@ export function createValidatedContainer(profile: ContainerProfile, timeoutMs =
363364
}
364365
catch (error) {
365366
// A nonzero exit means the daemon answered; a killed client leaves the request in flight.
366-
createUnsettled = typeof (error as { status?: unknown }).status !== 'number';
367+
createUnsettled = createOutcomeUnknown(error);
367368
if (createUnsettled) createdContainers.set(profile, undefined);
368369
throw error;
369370
}

‎agents/network/network.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { execFileSync, spawnSync } from 'node:child_process';
22
import { randomUUID } from 'node:crypto';
33
import { assertCapturedInvocation, type InvocationInput, type UnreleasedResource } from '../contract.ts';
4+
import { createOutcomeUnknown } from '../client-outcome.ts';
45
import { assertBuiltAgentImage } from '../container/image.ts';
56

67
export const VENDOR_HOSTS = Object.freeze({
@@ -208,7 +209,7 @@ export function createVendorNetwork(invocation: InvocationInput, imageId: string
208209
created.add(object);
209210
return output;
210211
} catch (error) {
211-
if (typeof (error as { status?: unknown }).status !== 'number') unsettled.add(object);
212+
if (createOutcomeUnknown(error)) unsettled.add(object);
212213
throw error;
213214
}
214215
};

‎test/agent-client-outcome.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { execFileSync } from 'node:child_process';
2+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
3+
import { tmpdir } from 'node:os';
4+
import { join } from 'node:path';
5+
import { afterAll, describe, expect, it } from 'vitest';
6+
import { createOutcomeUnknown } from '../agents/client-outcome.ts';
7+
8+
// A failed create leaves its object possibly present only if the request may have reached the daemon (#51 item 1).
9+
describe('create outcome classification', () => {
10+
const dir = mkdtempSync(join(tmpdir(), 'client-outcome-'));
11+
afterAll(() => rmSync(dir, { recursive: true, force: true }));
12+
const failure = (run: () => unknown) => { try { run(); } catch (error) { return error; } throw new Error('did not fail'); };
13+
14+
it('treats a client that never started as having created nothing', () => {
15+
expect(createOutcomeUnknown(failure(() => execFileSync('codeboost-no-such-docker', [],
16+
{ env: { PATH: dir }, stdio: 'ignore' })))).toBe(false);
17+
const notExecutable = join(dir, 'docker');
18+
writeFileSync(notExecutable, '#!/bin/sh\n', { mode: 0o644 });
19+
expect(createOutcomeUnknown(failure(() => execFileSync(notExecutable, [], { stdio: 'ignore' })))).toBe(false);
20+
});
21+
22+
it('treats a daemon-answered failure as known and a killed client as unknown', () => {
23+
expect(createOutcomeUnknown(failure(() => execFileSync('sh', ['-c', 'exit 3'], { stdio: 'ignore' })))).toBe(false);
24+
expect(createOutcomeUnknown(failure(() => execFileSync('sleep', ['5'],
25+
{ timeout: 50, killSignal: 'SIGKILL', stdio: 'ignore' })))).toBe(true);
26+
});
27+
28+
it('reads the cause of a wrapped client error', () => {
29+
expect(createOutcomeUnknown({ status: null, cause: { code: 'ENOENT' } })).toBe(false);
30+
expect(createOutcomeUnknown({ status: null, cause: { code: 'ETIMEDOUT' } })).toBe(true);
31+
expect(createOutcomeUnknown({ status: null })).toBe(true);
32+
});
33+
});

‎test/agent-network.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ describe('vendor-only egress', () => {
147147
expect(survived).toBe(true);
148148
}, 60_000);
149149

150+
it('reports a Docker client that could not start as a plain failure, with nothing to clean up', () => {
151+
const shim = mkdtempSync(join(tmpdir(), 'docker-shim-'));
152+
// A `docker` that cannot be executed: the create never reached the daemon, so nothing can exist.
153+
writeFileSync(join(shim, 'docker'), '#!/bin/sh\n', { mode: 0o644 });
154+
const blocked = captureInvocation({ ...invocation, attemptId: `unstartable-${randomUUID()}`,
155+
deadline: Date.now() + 60_000 });
156+
const path = process.env.PATH, started = performance.now();
157+
process.env.PATH = shim;
158+
let error: unknown;
159+
try { try { createVendorNetwork(blocked, imageId); } catch (caught) { error = caught; } }
160+
finally { process.env.PATH = path; rmSync(shim, { recursive: true, force: true }); }
161+
expect(error).toBeInstanceOf(Error);
162+
expect(error).not.toBeInstanceOf(VendorNetworkCreationCleanupError);
163+
// No settle window was waited out for a create that never happened.
164+
expect(performance.now() - started).toBeLessThan(5_000);
165+
}, 60_000);
166+
150167
it('does not delete a same-named stand-in when setup fails after the proxy exists', () => {
151168
const shim = mkdtempSync(join(tmpdir(), 'docker-shim-')), recorded = join(shim, 'impostor');
152169
const realDocker = execFileSync('sh', ['-c', 'command -v docker'], { encoding: 'utf8' }).trim();

0 commit comments

Comments
 (0)