Skip to content

Commit 63fb317

Browse files
perf(tests): cut remaining dominant waits from the slowest files
1 parent 349e7e4 commit 63fb317

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
@@ -43,7 +43,7 @@ import {
4343
type SubAgentSessionStore,
4444
} from "../subagent/index.js";
4545
import { getProcessAdmissionQueue } from "../subagent/admission.js";
46-
import { awaitCloseWithoutHidingLeftover } from "../subagent/dispose.js";
46+
import { disposeExecRuntime, formatCaughtError } from "./dispose.js";
4747
import type {
4848
ContextStore,
4949
InferenceSource,
@@ -131,11 +131,6 @@ import { WorkflowHost } from "../workflows/host.js";
131131

132132
const logger = getLogger([LOG_NAMESPACE_ROOT, "exec"]);
133133

134-
/** Normalize unknown catch values for structured warn/error logs. */
135-
export function formatCaughtError(err: unknown): string {
136-
return err instanceof Error ? err.message : String(err);
137-
}
138-
139134
const SELECTED_PROVIDER_FAILURE = "SelectedProviderFailure";
140135

141136
export async function refreshSelectedProviderCredential<T>(
@@ -183,72 +178,6 @@ export function execUserFailureMessage(
183178
return formatCaughtError(err);
184179
}
185180

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

0 commit comments

Comments
 (0)