Skip to content

Commit e5f4096

Browse files
committed
refactor(capture-kit): share the bounded provider app-log poller
Both providers shipped a near-identical ~200-line app-log poller with the same acquisition rollback, polling state machine, tail-overlap algorithm, finish memoization and disposal mechanics. Extract the shared lifecycle into capture-kit (ADR 0019 §5) behind a narrow reader input, leaving each provider a thin adapter that supplies backend identity and its own cleanup wording. Cancellation is characterized, not unified. Doublespeed's reader forwards the poller's abort signal; Limrun's WebSocket-backed read stays uncancelable. The shared bounded read forwards the signal and always settles on its own timeout, so neither provider's guarantee changes and abort never implies the request ended. Provider poller source drops 395 -> 276 lines; the tail algorithm and lifecycle now live once. No provider name leaks into shared policy. Issue: #2616 (stacked on #2112)
1 parent 8dad650 commit e5f4096

9 files changed

Lines changed: 750 additions & 592 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { AppLogRuntimeHost } from '@agent-device/contracts/app-log-runtime';
2+
3+
export type DeferredSleeps = Readonly<{
4+
wait(milliseconds: number): Promise<void>;
5+
resolveNext(milliseconds: number): void;
6+
resolveAny(): void;
7+
hasPending(milliseconds: number): boolean;
8+
}>;
9+
10+
export function createDeferredSleeps(): DeferredSleeps {
11+
const pending: Array<{ milliseconds: number; resolve: () => void }> = [];
12+
return {
13+
wait: async (milliseconds: number) =>
14+
await new Promise<void>((resolve) => pending.push({ milliseconds, resolve })),
15+
resolveNext: (milliseconds: number) => {
16+
const index = pending.findIndex((entry) => entry.milliseconds === milliseconds);
17+
if (index < 0) throw new Error(`No ${milliseconds}ms sleep is pending`);
18+
pending.splice(index, 1)[0]?.resolve();
19+
},
20+
resolveAny: () => pending.shift()?.resolve(),
21+
hasPending: (milliseconds: number) =>
22+
pending.some((entry) => entry.milliseconds === milliseconds),
23+
};
24+
}
25+
26+
export function createPollerHost(options: {
27+
existingTail: string;
28+
writes: string[];
29+
sleeps: DeferredSleeps;
30+
onOutputDispose?: () => void;
31+
outputDisposeError?: boolean;
32+
failure?: 'readTail' | 'openAppend';
33+
}): AppLogRuntimeHost {
34+
return {
35+
appleTools: {
36+
isXcrunAvailable: async () => false,
37+
run: async () => {
38+
throw new Error('unused');
39+
},
40+
},
41+
toolchains: { prepare: async () => undefined },
42+
artifacts: {
43+
resolveSession: () => ({
44+
outputPath: '/sessions/one/app.log',
45+
pidPath: '/sessions/one/app-log.pid',
46+
}),
47+
},
48+
commands: {
49+
which: async () => undefined,
50+
run: async () => ({ stdout: '', stderr: '', exitCode: 0 }),
51+
},
52+
outputs: {
53+
readTail: async () => {
54+
if (options.failure === 'readTail') throw new Error('tail failed');
55+
return options.existingTail;
56+
},
57+
openAppend: async () => {
58+
if (options.failure === 'openAppend') throw new Error('open failed');
59+
return {
60+
write: async (chunk) => {
61+
options.writes.push(String(chunk));
62+
},
63+
[Symbol.asyncDispose]: async () => {
64+
if (options.outputDisposeError) throw new Error('output cleanup failed');
65+
options.onOutputDispose?.();
66+
},
67+
};
68+
},
69+
},
70+
processTransports: {
71+
resolve: async () => ({ mode: 'local' }),
72+
},
73+
processes: {
74+
start: async () => {
75+
throw new Error('unused');
76+
},
77+
readMarker: async () => ({ status: 'missing' }),
78+
clearMarker: async () => {},
79+
inspect: async () => 'missing',
80+
terminate: async () => 'already-missing',
81+
},
82+
clock: {
83+
now: () => 100,
84+
sleep: async (milliseconds) => await options.sleeps.wait(milliseconds),
85+
},
86+
};
87+
}

0 commit comments

Comments
 (0)