Skip to content

Commit a191812

Browse files
Gustav-SimonssonTheGreatAxios
authored andcommitted
perf(tests): cut remaining dominant waits from the slowest files
1 parent 18cf8b0 commit a191812

16 files changed

Lines changed: 721 additions & 666 deletions

‎scripts/ci-timings.json‎

Lines changed: 272 additions & 272 deletions
Large diffs are not rendered by default.

‎scripts/test-parallel.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ function processAlive(pid: number): boolean {
1515
}
1616

1717
// These probes are `bun -e` one-liners, so they never import project modules
18-
// and finish in seconds. The tick test keeps a ~0.9s stall window: under the
18+
// and finish in seconds. The tick test keeps a ~0.7s stall window: under the
1919
// `--parallel` load this runner exists to enable, a delayed output tick can
2020
// look like a stall against a tighter window, so the window keeps several
2121
// ticks of scheduling headroom while a broken never-resetting timer still
2222
// fires mid-run (the tick test's total runtime exceeds the window). The
2323
// remaining tests drive children that never emit output, so their stall is
2424
// declared deterministically on the watchdog's first tick; they use a short
2525
// window so the suite does not pay the tick test's headroom in wall clock.
26-
const TEST_STALL_MS = 900;
27-
const SILENT_CHILD_STALL_MS = 400;
26+
const TEST_STALL_MS = 700;
27+
const SILENT_CHILD_STALL_MS = 250;
2828

2929
describe("runWithWatchdog", () => {
3030
test("passes through a successful run without retrying", async () => {
@@ -56,13 +56,13 @@ describe("runWithWatchdog", () => {
5656
});
5757

5858
test("output resets the stall timer", async () => {
59-
// Prints every 150ms for ~1.2s against a 0.9s stall window: a broken
59+
// Prints every 120ms for ~1s against a 0.7s stall window: a broken
6060
// timer that never reset would fire mid-run.
6161
const result = await runWithWatchdog({
6262
command: process.execPath,
6363
args: [
6464
"-e",
65-
"for (let i = 0; i < 8; i++) { console.log('tick', i); await new Promise(r => setTimeout(r, 150)); }",
65+
"for (let i = 0; i < 8; i++) { console.log('tick', i); await new Promise(r => setTimeout(r, 120)); }",
6666
],
6767
stallMs: TEST_STALL_MS,
6868
});
@@ -104,7 +104,7 @@ describe("runWithWatchdog", () => {
104104
command: process.execPath,
105105
args: [
106106
"-e",
107-
'process.on("SIGTERM", () => {}); setTimeout(() => process.exit(3), 800);',
107+
'process.on("SIGTERM", () => {}); setTimeout(() => process.exit(3), 600);',
108108
],
109109
stallMs: SILENT_CHILD_STALL_MS,
110110
onStall: () => {

‎src/exec/dispose.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { getLogger } from "@intx/log";
2+
3+
import { LOG_NAMESPACE_ROOT } from "../branding.js";
4+
import { awaitCloseWithoutHidingLeftover } from "../subagent/dispose.js";
5+
import type { SubAgentSessionStore } from "../subagent/index.js";
6+
7+
const logger = getLogger([LOG_NAMESPACE_ROOT, "exec"]);
8+
9+
/** Normalize unknown catch values for structured warn/error logs. */
10+
export function formatCaughtError(err: unknown): string {
11+
return err instanceof Error ? err.message : String(err);
12+
}
13+
14+
/**
15+
* Headless analogue of TUI `runtime-shutdown`: dispose the toolset (posix
16+
* process-group reap) before waiting on agent.close so a hung close cannot
17+
* skip killing detached run_shell children. `cancelAll` is awaited so a
18+
* leftover-child throw is visible. Once-only per runtime object so the send
19+
* path, `finally`, and signal host cannot double-dispose.
20+
*
21+
* Lives in its own module (not exec/runner.ts, whose import graph costs
22+
* ~0.4s in a fresh process) so the exec-shutdown-reap fixture can exercise
23+
* the real dispose path without paying that in every subprocess it spawns.
24+
*/
25+
const execDisposeInFlight = new WeakMap<object, Promise<void>>();
26+
27+
function rethrowExecDisposeFailures(failures: unknown[]): void {
28+
const first = failures[0];
29+
if (first === undefined) return;
30+
if (failures.length === 1) throw first;
31+
throw new AggregateError(failures, "exec runtime dispose failed");
32+
}
33+
34+
export function disposeExecRuntime(args: {
35+
agent: { close: () => Promise<unknown> } | null;
36+
toolset: { dispose: () => Promise<unknown> } | null;
37+
subAgentSessions: Pick<SubAgentSessionStore, "cancelAll"> | null;
38+
}): Promise<void> {
39+
const key = args.toolset ?? args.agent ?? args.subAgentSessions;
40+
if (key !== null) {
41+
const existing = execDisposeInFlight.get(key);
42+
if (existing !== undefined) return existing;
43+
}
44+
45+
const run = runExecDispose(args);
46+
if (key !== null) execDisposeInFlight.set(key, run);
47+
return run;
48+
}
49+
50+
async function runExecDispose(args: {
51+
agent: { close: () => Promise<unknown> } | null;
52+
toolset: { dispose: () => Promise<unknown> } | null;
53+
subAgentSessions: Pick<SubAgentSessionStore, "cancelAll"> | null;
54+
}): Promise<void> {
55+
const failures: unknown[] = [];
56+
if (args.toolset !== null) {
57+
try {
58+
await args.toolset.dispose();
59+
} catch (err: unknown) {
60+
logger.debug("toolset.dispose during exec finally failed: {error}", {
61+
error: formatCaughtError(err),
62+
});
63+
failures.push(err);
64+
}
65+
}
66+
try {
67+
await args.subAgentSessions?.cancelAll("Session closed");
68+
} catch (err) {
69+
failures.push(err);
70+
}
71+
if (args.agent !== null) {
72+
try {
73+
await awaitCloseWithoutHidingLeftover(args.agent.close(), failures[0]);
74+
} catch (err: unknown) {
75+
logger.debug("agent.close during exec finally failed: {error}", {
76+
error: formatCaughtError(err),
77+
});
78+
failures.push(err);
79+
}
80+
}
81+
rethrowExecDisposeFailures(failures);
82+
}

‎src/exec/runner.ts‎

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
type SubAgentSessionStore,
4545
} from "../subagent/index.js";
4646
import { getProcessAdmissionQueue } from "../subagent/admission.js";
47-
import { awaitCloseWithoutHidingLeftover } from "../subagent/dispose.js";
47+
import { disposeExecRuntime, formatCaughtError } from "./dispose.js";
4848
import type {
4949
ContextStore,
5050
InferenceSource,
@@ -143,11 +143,6 @@ import { WorkflowHost } from "../workflows/host.js";
143143

144144
const logger = getLogger([LOG_NAMESPACE_ROOT, "exec"]);
145145

146-
/** Normalize unknown catch values for structured warn/error logs. */
147-
export function formatCaughtError(err: unknown): string {
148-
return err instanceof Error ? err.message : String(err);
149-
}
150-
151146
const SELECTED_PROVIDER_FAILURE = "SelectedProviderFailure";
152147

153148
export async function refreshSelectedProviderCredential<T>(
@@ -195,72 +190,6 @@ export function execUserFailureMessage(
195190
return formatCaughtError(err);
196191
}
197192

198-
/**
199-
* Headless analogue of TUI `runtime-shutdown`: dispose the toolset (posix
200-
* process-group reap) before waiting on agent.close so a hung close cannot
201-
* skip killing detached run_shell children. `cancelAll` is awaited so a
202-
* leftover-child throw is visible. Once-only per runtime object so the send
203-
* path, `finally`, and signal host cannot double-dispose.
204-
*/
205-
const execDisposeInFlight = new WeakMap<object, Promise<void>>();
206-
207-
function rethrowExecDisposeFailures(failures: unknown[]): void {
208-
const first = failures[0];
209-
if (first === undefined) return;
210-
if (failures.length === 1) throw first;
211-
throw new AggregateError(failures, "exec runtime dispose failed");
212-
}
213-
214-
export function disposeExecRuntime(args: {
215-
agent: { close: () => Promise<unknown> } | null;
216-
toolset: { dispose: () => Promise<unknown> } | null;
217-
subAgentSessions: Pick<SubAgentSessionStore, "cancelAll"> | null;
218-
}): Promise<void> {
219-
const key = args.toolset ?? args.agent ?? args.subAgentSessions;
220-
if (key !== null) {
221-
const existing = execDisposeInFlight.get(key);
222-
if (existing !== undefined) return existing;
223-
}
224-
225-
const run = runExecDispose(args);
226-
if (key !== null) execDisposeInFlight.set(key, run);
227-
return run;
228-
}
229-
230-
async function runExecDispose(args: {
231-
agent: { close: () => Promise<unknown> } | null;
232-
toolset: { dispose: () => Promise<unknown> } | null;
233-
subAgentSessions: Pick<SubAgentSessionStore, "cancelAll"> | null;
234-
}): Promise<void> {
235-
const failures: unknown[] = [];
236-
if (args.toolset !== null) {
237-
try {
238-
await args.toolset.dispose();
239-
} catch (err: unknown) {
240-
logger.debug("toolset.dispose during exec finally failed: {error}", {
241-
error: formatCaughtError(err),
242-
});
243-
failures.push(err);
244-
}
245-
}
246-
try {
247-
await args.subAgentSessions?.cancelAll("Session closed");
248-
} catch (err) {
249-
failures.push(err);
250-
}
251-
if (args.agent !== null) {
252-
try {
253-
await awaitCloseWithoutHidingLeftover(args.agent.close(), failures[0]);
254-
} catch (err: unknown) {
255-
logger.debug("agent.close during exec finally failed: {error}", {
256-
error: formatCaughtError(err),
257-
});
258-
failures.push(err);
259-
}
260-
}
261-
rethrowExecDisposeFailures(failures);
262-
}
263-
264193
/**
265194
* Exec-primary director overlay. Omit / skywalker keep the product default
266195
* (`loadSessionChatPrompt` + advertised session tools). Any other closed-fleet

0 commit comments

Comments
 (0)