Skip to content

Commit 54955fa

Browse files
committed
fix: make cron and gateway stress tests deterministic
1 parent 15866ac commit 54955fa

3 files changed

Lines changed: 197 additions & 34 deletions

File tree

‎scripts/test-projects.test-support.mjs‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import os from "node:os";
88
import path from "node:path";
99
import {
1010
agentsCoreIsolatedTestFiles,
11+
agentsEmbeddedIncompleteTurnTestFiles,
12+
agentsEmbeddedOverflowCompactionTestFiles,
1113
isAgentsCoreIsolatedTestFile,
1214
} from "../test/vitest/vitest.agents-paths.mjs";
1315
import { isChannelSurfaceTestFile } from "../test/vitest/vitest.channel-paths.mjs";
@@ -88,6 +90,7 @@ import {
8890
} from "./run-vitest.mjs";
8991

9092
const DEFAULT_VITEST_CONFIG = "test/vitest/vitest.unit.config.ts";
93+
const AGENTS_EMBEDDED_AGENT_TEST_ROOT = "src/agents/embedded-agent-runner";
9194
const AGENTS_CORE_ISOLATED_VITEST_CONFIG = "test/vitest/vitest.agents-core-isolated.config.ts";
9295
const AGENTS_CORE_VITEST_CONFIG = "test/vitest/vitest.agents-core.config.ts";
9396
const AGENTS_EMBEDDED_AGENT_VITEST_CONFIG = "test/vitest/vitest.agents-embedded-agent.config.ts";
@@ -4247,7 +4250,32 @@ function classifyTarget(arg, cwd) {
42474250
return "autoReply";
42484251
}
42494252
if (isPathAtOrUnder(relative, "src/agents")) {
4250-
return "agent";
4253+
// Focused runs must preserve the full suite's isolated harness and hook-timeout contracts.
4254+
if (
4255+
relative === "src/agents" ||
4256+
relative === AGENTS_EMBEDDED_AGENT_TEST_ROOT ||
4257+
isGlobTarget(relative)
4258+
) {
4259+
return "agent";
4260+
}
4261+
if (agentsEmbeddedIncompleteTurnTestFiles.includes(relative)) {
4262+
return "agentEmbeddedIncompleteTurn";
4263+
}
4264+
if (agentsEmbeddedOverflowCompactionTestFiles.includes(relative)) {
4265+
return "agentEmbeddedOverflowCompaction";
4266+
}
4267+
if (isPathAtOrUnder(relative, `${AGENTS_EMBEDDED_AGENT_TEST_ROOT}/run`)) {
4268+
return "agentEmbeddedRun";
4269+
}
4270+
if (isPathAtOrUnder(relative, AGENTS_EMBEDDED_AGENT_TEST_ROOT)) {
4271+
return "agentEmbedded";
4272+
}
4273+
if (isPathAtOrUnder(relative, "src/agents/tools")) {
4274+
return "agentTools";
4275+
}
4276+
return isFileLikeTarget(relative) && path.posix.dirname(relative) === "src/agents"
4277+
? "agentCore"
4278+
: "agentSupport";
42514279
}
42524280
if (isPathAtOrUnder(relative, "src/plugins")) {
42534281
return "plugin";
@@ -4409,6 +4437,27 @@ export function buildVitestRunPlans(
44094437

44104438
const groupedTargets = new Map();
44114439
for (const targetArg of activeTargetArgs) {
4440+
if (!watchMode && toRepoRelativeTarget(targetArg, cwd) === AGENTS_EMBEDDED_AGENT_TEST_ROOT) {
4441+
// The recursive parent spans four harness owners; keep every isolated project intact.
4442+
const embeddedTargetsByKind = [
4443+
["agentEmbedded", [`${AGENTS_EMBEDDED_AGENT_TEST_ROOT}/*.test.ts`]],
4444+
["agentEmbeddedIncompleteTurn", agentsEmbeddedIncompleteTurnTestFiles],
4445+
["agentEmbeddedOverflowCompaction", agentsEmbeddedOverflowCompactionTestFiles],
4446+
["agentEmbeddedRun", [`${AGENTS_EMBEDDED_AGENT_TEST_ROOT}/run`]],
4447+
];
4448+
4449+
for (const [kind, targets] of embeddedTargetsByKind) {
4450+
const current = groupedTargets.get(kind) ?? [];
4451+
for (const target of targets) {
4452+
if (!current.includes(target)) {
4453+
current.push(target);
4454+
}
4455+
}
4456+
groupedTargets.set(kind, current);
4457+
}
4458+
continue;
4459+
}
4460+
44124461
const kind = classifyTarget(targetArg, cwd);
44134462
const current = groupedTargets.get(kind) ?? [];
44144463
current.push(targetArg);

‎src/gateway/server/ws-connection.startup.test.ts‎

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
GATEWAY_STARTUP_PENDING_CLOSE_CAUSE,
1414
GATEWAY_STARTUP_UNAVAILABLE_REASON,
1515
} from "../../../packages/gateway-protocol/src/startup-unavailable.js";
16+
import { createDeferred } from "../../test-utils/deferred.js";
1617
import { attachGatewayWsConnectionHandler } from "./ws-connection.js";
1718
import {
1819
attachGatewayWsForTest,
@@ -25,10 +26,28 @@ describe("attachGatewayWsConnectionHandler startup readiness", () => {
2526
it.each([GATEWAY_STARTUP_CLOSE_CODE, 1006])(
2627
"keeps startup-unavailable close code %i at debug level",
2728
async (observedCloseCode) => {
28-
const sent: unknown[] = [];
29+
const responseReceived = createDeferred<{
30+
type?: unknown;
31+
id?: unknown;
32+
ok?: unknown;
33+
error?: {
34+
code?: unknown;
35+
retryable?: unknown;
36+
retryAfterMs?: unknown;
37+
details?: unknown;
38+
};
39+
}>();
2940
const socket = createGatewayWsTestSocket({
3041
onSend: (data) => {
31-
sent.push(JSON.parse(data));
42+
const frame = JSON.parse(data) as unknown;
43+
if (
44+
typeof frame === "object" &&
45+
frame !== null &&
46+
(frame as { type?: unknown }).type === "res" &&
47+
(frame as { id?: unknown }).id === "connect-1"
48+
) {
49+
responseReceived.resolve(frame);
50+
}
3251
},
3352
});
3453
const logWsControl = createGatewayWsTestLogger();
@@ -65,37 +84,8 @@ describe("attachGatewayWsConnectionHandler startup readiness", () => {
6584
}),
6685
);
6786

68-
await vi.waitFor(() => {
69-
expect(
70-
sent.some(
71-
(frame) =>
72-
typeof frame === "object" &&
73-
frame !== null &&
74-
(frame as { type?: unknown; id?: unknown; ok?: unknown }).type === "res" &&
75-
(frame as { id?: unknown }).id === "connect-1",
76-
),
77-
).toBe(true);
78-
});
79-
80-
const response = sent.find(
81-
(frame) =>
82-
typeof frame === "object" &&
83-
frame !== null &&
84-
(frame as { type?: unknown; id?: unknown }).type === "res" &&
85-
(frame as { id?: unknown }).id === "connect-1",
86-
) as
87-
| {
88-
type?: unknown;
89-
id?: unknown;
90-
ok?: unknown;
91-
error?: {
92-
code?: unknown;
93-
retryable?: unknown;
94-
retryAfterMs?: unknown;
95-
details?: unknown;
96-
};
97-
}
98-
| undefined;
87+
// The handler is lazy-loaded; wait for its actual frame instead of a one-second poll.
88+
const response = await responseReceived.promise;
9989
expect(response?.type).toBe("res");
10090
expect(response?.id).toBe("connect-1");
10191
expect(response?.ok).toBe(false);

‎test/scripts/test-projects.test.ts‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,6 +2631,130 @@ describe("scripts/test-projects changed-target routing", () => {
26312631
},
26322632
);
26332633

2634+
it.each([
2635+
["src/agents/agent-scope.test.ts", "test/vitest/vitest.agents-core.config.ts"],
2636+
[
2637+
"src/agents/embedded-agent-runner/run.before-agent-reply-cron.test.ts",
2638+
"test/vitest/vitest.agents-embedded-agent.config.ts",
2639+
],
2640+
[
2641+
"src/agents/embedded-agent-runner/run.incomplete-turn.test.ts",
2642+
"test/vitest/vitest.agents-embedded-agent-incomplete-turn.config.ts",
2643+
],
2644+
[
2645+
"src/agents/embedded-agent-runner/run.overflow-compaction.test.ts",
2646+
"test/vitest/vitest.agents-embedded-agent-overflow-compaction.config.ts",
2647+
],
2648+
[
2649+
"src/agents/embedded-agent-runner/run/attempt.abort-race.test.ts",
2650+
"test/vitest/vitest.agents-embedded-agent-run.config.ts",
2651+
],
2652+
["src/agents/runtime-plan/tools.test.ts", "test/vitest/vitest.agents-support.config.ts"],
2653+
["src/agents/tools/cron-tool.pacing.test.ts", "test/vitest/vitest.agents-tools.config.ts"],
2654+
])("routes focused agent test %s to its owning shard", (testFile, config) => {
2655+
expect(buildVitestRunPlans([testFile])).toEqual([
2656+
{
2657+
config,
2658+
forwardedArgs: [],
2659+
includePatterns: [testFile],
2660+
watchMode: false,
2661+
},
2662+
]);
2663+
});
2664+
2665+
it.each([
2666+
[
2667+
"src/agents/embedded-agent-runner/run",
2668+
"test/vitest/vitest.agents-embedded-agent-run.config.ts",
2669+
],
2670+
["src/agents/runtime-plan", "test/vitest/vitest.agents-support.config.ts"],
2671+
["src/agents/tools", "test/vitest/vitest.agents-tools.config.ts"],
2672+
])("routes focused agent directory %s to its owning shard", (directory, config) => {
2673+
const plans = buildVitestRunPlans([directory]);
2674+
2675+
expect(plans).toEqual(
2676+
expect.arrayContaining([
2677+
{
2678+
config,
2679+
forwardedArgs: [],
2680+
includePatterns: [`${directory}/**/*.test.ts`],
2681+
watchMode: false,
2682+
},
2683+
]),
2684+
);
2685+
expect(plans.map((plan) => plan.config)).not.toContain(
2686+
"test/vitest/vitest.agents-core.config.ts",
2687+
);
2688+
});
2689+
2690+
it("splits the embedded-agent parent directory across every isolated harness", () => {
2691+
const root = "src/agents/embedded-agent-runner";
2692+
const plans = buildVitestRunPlans([root]);
2693+
2694+
expect(plans).toEqual(
2695+
expect.arrayContaining([
2696+
{
2697+
config: "test/vitest/vitest.agents-embedded-agent.config.ts",
2698+
forwardedArgs: [],
2699+
includePatterns: [`${root}/*.test.ts`],
2700+
watchMode: false,
2701+
},
2702+
{
2703+
config: "test/vitest/vitest.agents-embedded-agent-incomplete-turn.config.ts",
2704+
forwardedArgs: [],
2705+
includePatterns: [`${root}/run.incomplete-turn.test.ts`],
2706+
watchMode: false,
2707+
},
2708+
{
2709+
config: "test/vitest/vitest.agents-embedded-agent-overflow-compaction.config.ts",
2710+
forwardedArgs: [],
2711+
includePatterns: [`${root}/run.overflow-compaction.test.ts`],
2712+
watchMode: false,
2713+
},
2714+
{
2715+
config: "test/vitest/vitest.agents-embedded-agent-run.config.ts",
2716+
forwardedArgs: [],
2717+
includePatterns: [`${root}/run/**/*.test.ts`],
2718+
watchMode: false,
2719+
},
2720+
]),
2721+
);
2722+
expect(plans.map((plan) => plan.config)).not.toContain("test/vitest/vitest.agents.config.ts");
2723+
});
2724+
2725+
it("keeps the broad agent test glob in the all-agents shard", () => {
2726+
const target = "src/agents/**/*.test.ts";
2727+
2728+
expect(buildVitestRunPlans([target])).toEqual([
2729+
{
2730+
config: "test/vitest/vitest.agents.config.ts",
2731+
forwardedArgs: [],
2732+
includePatterns: [target],
2733+
watchMode: false,
2734+
},
2735+
]);
2736+
});
2737+
2738+
it("keeps mixed embedded-agent and cron-tool targets in their owning shards", () => {
2739+
const embeddedTest = "src/agents/embedded-agent-runner/run.before-agent-reply-cron.test.ts";
2740+
const cronToolTest = "src/agents/tools/cron-tool.pacing.test.ts";
2741+
2742+
expect(buildVitestRunPlans([embeddedTest, cronToolTest])).toEqual([
2743+
{
2744+
config: "test/vitest/vitest.agents-embedded-agent.config.ts",
2745+
forwardedArgs: [],
2746+
includePatterns: [embeddedTest],
2747+
watchMode: false,
2748+
},
2749+
{
2750+
config: "test/vitest/vitest.agents-tools.config.ts",
2751+
forwardedArgs: [],
2752+
includePatterns: [cronToolTest],
2753+
watchMode: false,
2754+
},
2755+
]);
2756+
});
2757+
26342758
it("routes Docker E2E script targets to their owner tooling tests", () => {
26352759
const targets = [
26362760
"scripts/e2e/kitchen-sink-plugin-docker.sh",

0 commit comments

Comments
 (0)