Skip to content
Merged
251 changes: 250 additions & 1 deletion packages/daemon/src/resources/artifact-tracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,128 @@ import { buffer } from 'node:stream/consumers';
import { gunzipSync } from 'node:zlib';
import { expect, test, vi } from 'vitest';
import { mkdtempForTestSync } from '@agent-device/test-kit';
import { AppError, isRequestCanceledError, normalizeError } from '@agent-device/kernel/errors';
import { createRuntimeStateHandle } from '@agent-device/host-kit/runtime-slots';
import * as command from '@agent-device/host-kit/command';
import {
cleanupDownloadableArtifact,
listDownloadableArtifacts,
openDownloadableArtifact,
trackDownloadableArtifact,
trackUploadedArtifact,
type OpenedDownloadableArtifact,
} from './artifact-tracking.ts';

test.each(['cleanup', 'expiry'] as const)(
'%s aborts an inventory-only archive and removes its source after tar settles',
async (trigger) => {
const root = mkdtempForTestSync('fu-art-inventory-expiry-');
const artifactPath = path.join(root, 'capture.trace');
fs.mkdirSync(artifactPath);
fs.writeFileSync(path.join(artifactPath, 'payload'), 'pending inventory archive');
const runtime = createRuntimeStateHandle();
let start!: () => void;
const started = new Promise<void>((resolve) => {
start = resolve;
});
let finish!: () => void;
const stopped = new Promise<void>((resolve) => {
finish = resolve;
});
let archivePath = '';
let signal: AbortSignal | undefined;
const runCmd = command.runCmd;
const archive = vi.spyOn(command, 'runCmd').mockImplementationOnce(async (...args) => {
archivePath = String(args[1]?.[1]);
signal = args[2]?.signal;
fs.writeFileSync(archivePath, 'partial tar output');
start();
await stopped;
return await runCmd(...args);
});
let listing: ReturnType<typeof listDownloadableArtifacts> | undefined;
try {
const timers = vi.spyOn(globalThis, 'setTimeout');
let id: string;
let expire: (() => void) | undefined;
try {
id = runtime.run(() =>
trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }),
);
expire = timers.mock.calls.find(([, delay]) => delay === 15 * 60 * 1000)?.[0];
} finally {
timers.mockRestore();
}
listing = runtime.run(() => listDownloadableArtifacts(undefined, process.env));
await started;
expect(fs.existsSync(archivePath)).toBe(true);
if (trigger === 'expiry') {
expect(expire).toBeTypeOf('function');
runtime.run(() => expire!());
} else {
runtime.run(() => cleanupDownloadableArtifact(id));
}
expect(signal?.aborted).toBe(true);
expect(signal?.reason).toMatchObject({
code: 'COMMAND_FAILED',
details: { reason: 'RESOURCE_EXPIRED' },
});
expect(fs.existsSync(artifactPath)).toBe(true);
expect(fs.existsSync(archivePath)).toBe(true);
finish();
expect(await listing).toEqual([]);
expect(fs.existsSync(artifactPath)).toBe(false);
expect(fs.existsSync(path.dirname(archivePath))).toBe(false);
expect(archive).toHaveBeenCalledTimes(1);
} finally {
finish();
if (listing) await listing;
runtime.dispose();
archive.mockRestore();
}
},
);

test('a missing cached directory archive is rebuilt from the live source', async () => {
const root = mkdtempForTestSync('fu-art-cache-rebuild-');
const artifactPath = path.join(root, 'capture.trace');
const expected = Buffer.from('live source after cached archive disappears');
fs.mkdirSync(artifactPath);
fs.writeFileSync(path.join(artifactPath, 'payload'), expected);
const runtime = createRuntimeStateHandle();
const downloads: OpenedDownloadableArtifact[] = [];
try {
const id = runtime.run(() =>
trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }),
);
const first = await runtime.run(() => openDownloadableArtifact(id, undefined, process.env));
downloads.push(first);
expect(gunzipSync(await buffer(first.stream)).includes(expected)).toBe(true);
await vi.waitFor(() => expect(first.stream.closed).toBe(true));
first.release();
const oldArchive = String(first.stream.path);
fs.unlinkSync(oldArchive);
expect(fs.existsSync(artifactPath)).toBe(true);
const second = await runtime.run(() => openDownloadableArtifact(id, undefined, process.env));
downloads.push(second);
expect(gunzipSync(await buffer(second.stream)).includes(expected)).toBe(true);
await vi.waitFor(() => expect(second.stream.closed).toBe(true));
second.release();
expect(String(second.stream.path)).not.toBe(oldArchive);
expect(fs.existsSync(path.dirname(oldArchive))).toBe(false);
runtime.run(() => cleanupDownloadableArtifact(id));
expect(fs.existsSync(artifactPath)).toBe(false);
expect(fs.existsSync(path.dirname(String(second.stream.path)))).toBe(false);
} finally {
for (const download of downloads) {
download.stream.destroy();
await vi.waitFor(() => expect(download.stream.closed).toBe(true));
download.release();
}
runtime.dispose();
}
});

test('cancelling the archive builder preserves a second download waiting for its full archive', async () => {
const root = mkdtempForTestSync('fu-art-shared-build-');
const artifactPath = path.join(root, 'capture.trace');
Expand Down Expand Up @@ -84,6 +196,143 @@ test('cancelling the archive builder preserves a second download waiting for its
}
});

test.each(['deadline-aborted', 'late-aborted'] as const)(
'a live artifact starts a fresh archive while a %s build is settling',
async (failure) => {
const root = mkdtempForTestSync('fu-art-retry-build-');
const artifactPath = path.join(root, 'capture.trace');
fs.mkdirSync(artifactPath);
const expected = Buffer.from('fresh archive payload');
fs.writeFileSync(path.join(artifactPath, 'payload'), expected);
const runtime = createRuntimeStateHandle();
let finishAborted!: () => void;
const settling = new Promise<void>((resolve) => {
finishAborted = resolve;
});
let aborted!: () => void;
const cancelled = new Promise<void>((resolve) => {
aborted = resolve;
});
let tarSucceeded!: () => void;
const succeeded = new Promise<void>((resolve) => {
tarSucceeded = resolve;
});
let firstArchiveDir = '';
const runCmd = command.runCmd;
const archive = vi.spyOn(command, 'runCmd').mockImplementationOnce(async (...args) => {
firstArchiveDir = path.dirname(String(args[1]?.[1]));
args[2]?.signal?.addEventListener('abort', aborted, { once: true });
if (failure === 'late-aborted') {
const result = await runCmd(...args);
tarSucceeded();
await settling;
return result;
}
await settling;
return await runCmd(...args);
});
const openings: Array<Promise<OpenedDownloadableArtifact>> = [];
try {
const id = runtime.run(() =>
trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }),
);
const timers = vi.spyOn(globalThis, 'setTimeout');
const first = runtime.run(() => openDownloadableArtifact(id, undefined, process.env));
openings.push(first);
void first.catch(() => {});
const deadline = timers.mock.calls.find(([, delay]) => delay === 15 * 60 * 1000)?.[0];
timers.mockRestore();
if (failure === 'late-aborted') await succeeded;
expect(deadline).toBeTypeOf('function');
runtime.run(() => deadline!());
await cancelled;
const second = runtime.run(() => openDownloadableArtifact(id, undefined, process.env));
openings.push(second);
void second.catch(() => {});
finishAborted();
await expect(first).rejects.toMatchObject({ details: { reason: 'RESOURCE_EXPIRED' } });
expect(fs.existsSync(firstArchiveDir)).toBe(false);
const download = await second;
const closed = once(download.stream, 'close');
const bytes = await buffer(download.stream);
await closed;
download.release();
expect(gunzipSync(bytes).includes(expected)).toBe(true);
expect(archive).toHaveBeenCalledTimes(2);
if (failure === 'late-aborted') runtime.dispose();
else runtime.run(() => cleanupDownloadableArtifact(id));
expect(fs.existsSync(artifactPath)).toBe(false);
expect(fs.existsSync(path.dirname(String(download.stream.path)))).toBe(false);
} finally {
finishAborted();
for (const result of await Promise.allSettled(openings)) {
if (result.status !== 'fulfilled') continue;
const download = result.value;
if (!download.stream.closed) {
const closed = once(download.stream, 'close');
download.stream.destroy();
await closed;
}
download.release();
}
runtime.dispose();
archive.mockRestore();
}
},
);

test.each(['before opening', 'while archiving'] as const)(
'caller cancellation %s has a typed reason without forwarding caller data',
async (phase) => {
const root = mkdtempForTestSync('fu-art-client-cancel-');
const artifactPath = path.join(root, 'capture.trace');
fs.mkdirSync(artifactPath);
fs.writeFileSync(path.join(artifactPath, 'payload'), 'cancelled payload');
const runtime = createRuntimeStateHandle();
const archive = vi.spyOn(command, 'runCmd').mockImplementationOnce(async (...args) => {
const signal = args[2]!.signal!;
await new Promise<void>((_resolve, reject) => {
signal.addEventListener('abort', () => reject(signal.reason), { once: true });
});
throw new Error('aborted tar cannot complete');
});
const caller = new AbortController();
const reason = new AppError('COMMAND_FAILED', 'private caller cancellation data', {
reason: 'RESOURCE_EXPIRED',
});
let opening: Promise<OpenedDownloadableArtifact> | undefined;
try {
const id = runtime.run(() =>
trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }),
);
if (phase === 'before opening') caller.abort(reason);
opening = runtime.run(() =>
openDownloadableArtifact(id, undefined, process.env, caller.signal),
);
void opening.catch(() => {});
if (phase === 'while archiving') caller.abort(reason);
const error = await opening.catch((error: unknown) => error);
expect(isRequestCanceledError(error)).toBe(true);
const normalized = normalizeError(error);
expect(normalized).toMatchObject({
code: 'COMMAND_FAILED',
message: 'request canceled',
details: { reason: 'request_canceled' },
});
expect(JSON.stringify(normalized)).not.toContain('private caller cancellation data');
expect(JSON.stringify(normalized)).not.toContain('RESOURCE_EXPIRED');
expect(fs.existsSync(artifactPath)).toBe(true);
runtime.run(() => cleanupDownloadableArtifact(id));
expect(fs.existsSync(artifactPath)).toBe(false);
} finally {
caller.abort();
if (opening) await Promise.allSettled([opening]);
runtime.dispose();
archive.mockRestore();
}
},
);

test('disposing every archive waiter aborts the shared build and removes its temporary directory', async () => {
const root = mkdtempForTestSync('fu-art-cancel-all-');
const artifactPath = path.join(root, 'capture.trace');
Expand Down Expand Up @@ -118,7 +367,7 @@ test('disposing every archive waiter aborts the shared build and removes its tem
openings.push(runtime.run(() => openDownloadableArtifact(id, undefined, process.env)));
for (const opening of openings) void opening.catch(() => {});
runtime.dispose();
expect(archiveSignal?.aborted).toBe(true);
await vi.waitFor(() => expect(archiveSignal?.aborted).toBe(true));
expect(fs.existsSync(artifactPath)).toBe(true);
expect(fs.existsSync(archiveDir)).toBe(true);
continueArchive();
Expand Down
Loading
Loading