Skip to content

Commit 78e11ca

Browse files
authored
perf(ci): keep prompt snapshot generation off the jiti loader (5.1min lane -> ~1.5min) (#109757)
Measured breakdown of check-prompt-snapshots in run 29557851276 (5m03s): - 37s checkout + Node env setup - 4m25s inside one `pnpm prompt:snapshots:check` process: - ~35s tsx import of the generator graph - ~3min first createOpenClawCodingTools call: the message-tool schema build resolves the scenario channel plugin, the empty loaded registry falls back to the bundled-channel jiti loader (extensions/telegram/index.ts), and jiti re-transpiles the core source graph (136.6s jiti self time in --cpu-prof; second generation call in the same process takes 0.3s) - ~1min more for the cold provider-plugin lookup in normalizeAgentRuntimeTools; no provider plugin owns tool-schema hooks for the `codex` harness provider, so the jiti load only rediscovers a no-op - <2s actual rendering + oxfmt + drift compare Split axis decision: the planned 2-3 way scenario shard matrix cannot lower the pole because the cost is a one-time per-process transpile; every shard would pay the full ~4min again (jiti fsCache does not help: a warm-cache rerun still took 200s). Fix the generator instead: - Preload each scenario's channel plugin surface (channel-plugin-api.js, resolved via the bundled-plugin public-surface resolver) through the ambient tsx/vitest ESM graph, and pin exactly that channel in a scoped loaded registry while the scenario's tools are built. Snapshot bytes are unchanged: the loaded plugin object is the same export the bundled jiti fallback materializes, and cross-channel action discovery still sees no other loaded channels. - Pass allowProviderRuntimePluginLoad=false to normalizeAgentRuntimeTools; registry-only resolution returns the same undefined plugin instantly. Coverage guard: test/scripts/prompt-snapshots.test.ts now generates the snapshot files and asserts zero jiti plugin-loader calls (with the offending transform targets in the failure message), so a future scenario or channel that misses the preload fails the suite instead of silently restoring the 4-minute lane. Proof: `pnpm prompt:snapshots:check` passes byte-identical against the committed fixtures in 54s wall / 35s CPU locally (was 354s wall / 306s CPU); focused vitest suites (32 tests incl. new guard), tsgo core-test + extensions-test lanes, scoped oxlint/oxfmt, and `git diff --check` are green. Expected CI pole when the lane fires: ~5.1min -> ~1.5-2min.
1 parent 6651813 commit 78e11ca

4 files changed

Lines changed: 135 additions & 10 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,8 +2113,9 @@ jobs:
21132113
group: boundaries
21142114
boundary_shard: 2/4,3/4,4/4
21152115
runner: blacksmith-8vcpu-ubuntu-2404
2116-
# Prompt snapshot regeneration runs real embedded-agent turns (~2min)
2117-
# and used to own the boundaries-a wall clock; keep it in its own lane.
2116+
# Prompt snapshot regeneration evaluates the full agent tool/prompt
2117+
# import graph and used to own the boundaries-a wall clock; keep it
2118+
# in its own lane.
21182119
- check_name: check-prompt-snapshots
21192120
group: prompt-snapshots
21202121
runner: blacksmith-8vcpu-ubuntu-2404

‎test/helpers/agents/happy-path-prompt-snapshots.ts‎

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ import type {
2323
} from "../../../src/plugin-sdk/agent-harness-runtime.js";
2424
import { normalizeAgentRuntimeTools } from "../../../src/plugin-sdk/agent-harness-runtime.js";
2525
import { createOpenClawCodingTools } from "../../../src/plugin-sdk/agent-harness.js";
26+
import type { PluginRegistry } from "../../../src/plugins/registry.js";
27+
import {
28+
getActivePluginRegistry,
29+
resetPluginRuntimeStateForTest,
30+
setActivePluginRegistry,
31+
} from "../../../src/plugins/runtime.js";
2632
import { resolveRelativeBundledPluginPublicModuleId } from "../../../src/test-utils/bundled-plugin-public-surface.js";
33+
import { createTestRegistry } from "../../../src/test-utils/channel-plugins.js";
2734
import {
2835
CODEX_MODEL_PROMPT_FIXTURE_DIR,
2936
CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR,
@@ -151,6 +158,81 @@ async function loadCodexPromptSnapshotApi(): Promise<CodexPromptSnapshotApi> {
151158
return (await import(CODEX_TEST_API_MODULE_ID)) as CodexPromptSnapshotApi;
152159
}
153160

161+
type ScenarioChannelPluginFixture = {
162+
pluginId: string;
163+
plugin: unknown;
164+
};
165+
166+
const scenarioChannelPluginFixtures = new Map<string, Promise<ScenarioChannelPluginFixture>>();
167+
168+
/**
169+
* Tool construction resolves the scenario's channel plugin for message-tool
170+
* schema data. Without a loaded channel registry that lookup falls back to the
171+
* bundled-channel jiti loader, which re-transpiles the core source graph and
172+
* stalls snapshot generation by minutes. Import the entry-declared channel
173+
* plugin surface through the ambient (tsx/vitest) module graph instead so the
174+
* already-evaluated core modules are reused.
175+
*/
176+
function scenarioChannelPluginFixture(pluginId: string): Promise<ScenarioChannelPluginFixture> {
177+
const cached = scenarioChannelPluginFixtures.get(pluginId);
178+
if (cached) {
179+
return cached;
180+
}
181+
const loading = loadScenarioChannelPluginFixture(pluginId);
182+
scenarioChannelPluginFixtures.set(pluginId, loading);
183+
return loading;
184+
}
185+
186+
async function loadScenarioChannelPluginFixture(
187+
pluginId: string,
188+
): Promise<ScenarioChannelPluginFixture> {
189+
const moduleId = resolveRelativeBundledPluginPublicModuleId({
190+
fromModuleUrl: import.meta.url,
191+
pluginId,
192+
artifactBasename: "channel-plugin-api.js",
193+
});
194+
const moduleNamespace = (await import(moduleId)) as Record<string, unknown>;
195+
// Bundled channel entries (extensions/<id>/index.ts) declare their channel
196+
// plugin export from channel-plugin-api.js as `<id>Plugin`; the setup-only
197+
// sibling export shares the channel id, so name selection stays exact.
198+
const exportName = `${pluginId}Plugin`;
199+
const plugin = moduleNamespace[exportName];
200+
if (!plugin || typeof plugin !== "object") {
201+
throw new Error(
202+
`missing channel plugin export "${exportName}" in ${moduleId}; align the snapshot helper with the ${pluginId} plugin entry`,
203+
);
204+
}
205+
return { pluginId, plugin };
206+
}
207+
208+
/**
209+
* Pins exactly the scenario channel while its tools are built. Cross-channel
210+
* action discovery walks every loaded channel plugin, so registering more than
211+
* the scenario channel would change the generated message-tool schema bytes.
212+
*/
213+
function withScenarioChannelRegistry<T>(fixture: ScenarioChannelPluginFixture, build: () => T): T {
214+
const previousRegistry: PluginRegistry | null = getActivePluginRegistry();
215+
setActivePluginRegistry(
216+
createTestRegistry([
217+
{
218+
pluginId: fixture.pluginId,
219+
plugin: fixture.plugin,
220+
source: "prompt-snapshot-fixture",
221+
origin: "bundled",
222+
},
223+
]),
224+
);
225+
try {
226+
return build();
227+
} finally {
228+
if (previousRegistry) {
229+
setActivePluginRegistry(previousRegistry);
230+
} else {
231+
resetPluginRuntimeStateForTest();
232+
}
233+
}
234+
}
235+
154236
const CODEX_WORKSPACE_BOOTSTRAP_CONTEXT_FILES = [
155237
{
156238
path: path.join(WORKSPACE_DIR, "MEMORY.md"),
@@ -446,14 +528,32 @@ function createDynamicTools(params: {
446528
modelId: MODEL_ID,
447529
modelApi: "responses",
448530
model: happyPathModel,
531+
// No provider runtime plugin owns tool-schema hooks for the `codex`
532+
// harness provider, so a runtime plugin load can only rediscover that
533+
// through the jiti source loader (minutes of core re-transpilation).
534+
// Registry-only resolution keeps the same no-op outcome instantly.
535+
allowProviderRuntimePluginLoad: false,
449536
});
450537
return params.codexApi.createCodexDynamicToolSpecsForPromptSnapshot({
451538
tools: normalized.filter((tool) => HAPPY_PATH_TOOL_NAMES.has(tool.name)),
452539
directToolNames: ["message"],
453540
});
454541
}
455542

456-
function createScenarios(codexApi: CodexPromptSnapshotApi): PromptScenario[] {
543+
async function createScenarioDynamicTools(params: {
544+
codexApi: CodexPromptSnapshotApi;
545+
ctx: TemplateContext;
546+
trigger: "user" | "heartbeat";
547+
}): Promise<CodexDynamicToolSpec[]> {
548+
const provider = params.ctx.Provider;
549+
if (!provider) {
550+
throw new Error("prompt snapshot scenarios must set ctx.Provider for channel tool fixtures");
551+
}
552+
const fixture = await scenarioChannelPluginFixture(provider);
553+
return withScenarioChannelRegistry(fixture, () => createDynamicTools(params));
554+
}
555+
556+
async function createScenarios(codexApi: CodexPromptSnapshotApi): Promise<PromptScenario[]> {
457557
const telegramDirectCtx: TemplateContext = {
458558
Provider: "telegram",
459559
Surface: "telegram",
@@ -506,17 +606,21 @@ function createScenarios(codexApi: CodexPromptSnapshotApi): PromptScenario[] {
506606
Body: resolveHeartbeatPromptForResponseTool(),
507607
BodyStripped: resolveHeartbeatPromptForResponseTool(),
508608
};
509-
const telegramDirectTools = createDynamicTools({
609+
const telegramDirectTools = await createScenarioDynamicTools({
510610
codexApi,
511611
ctx: telegramDirectCtx,
512612
trigger: "user",
513613
});
514-
const discordGroupTools = createDynamicTools({
614+
const discordGroupTools = await createScenarioDynamicTools({
515615
codexApi,
516616
ctx: discordGroupCtx,
517617
trigger: "user",
518618
});
519-
const heartbeatTools = createDynamicTools({ codexApi, ctx: heartbeatCtx, trigger: "heartbeat" });
619+
const heartbeatTools = await createScenarioDynamicTools({
620+
codexApi,
621+
ctx: heartbeatCtx,
622+
trigger: "heartbeat",
623+
});
520624

521625
return [
522626
{
@@ -927,7 +1031,7 @@ function renderReadme(scenarios: PromptScenario[]): string {
9271031
/** Build all Codex happy-path prompt snapshot files without writing them. */
9281032
export async function createHappyPathPromptSnapshotFiles(): Promise<PromptSnapshotFile[]> {
9291033
const codexApi = await loadCodexPromptSnapshotApi();
930-
const scenarios = createScenarios(codexApi);
1034+
const scenarios = await createScenarios(codexApi);
9311035
const files = [
9321036
{
9331037
path: path.join(CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR, "README.md"),

‎test/scripts/prompt-snapshots.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import {
1313
renderCodexModelInstructions,
1414
runCodexModelPromptFixtureSync,
1515
} from "../../scripts/sync-codex-model-prompt-fixture.js";
16+
import { getPluginModuleLoaderStats } from "../../src/plugins/plugin-module-loader-cache.js";
1617
import { expectNoReaddirSyncDuring } from "../../src/test-utils/fs-scan-assertions.js";
1718
import { toRepoRelativePath } from "../../src/test-utils/repo-files.js";
19+
import { createHappyPathPromptSnapshotFiles } from "../helpers/agents/happy-path-prompt-snapshots.js";
1820
import {
1921
CODEX_MODEL_PROMPT_FIXTURE_DIR,
2022
CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR,
@@ -131,6 +133,24 @@ describe("happy path prompt snapshots", () => {
131133
]);
132134
});
133135

136+
it("generates snapshots without jiti plugin-loader fallbacks", async () => {
137+
// Perf contract for the check-prompt-snapshots CI lane: scenario channel
138+
// plugins are preloaded through the ambient module graph. A jiti
139+
// plugin-loader call here means a scenario channel (or another plugin
140+
// surface) fell back to source re-transpilation, which re-evaluates the
141+
// core graph and stalls the lane by minutes.
142+
const callsBefore = getPluginModuleLoaderStats().calls;
143+
const files = await createHappyPathPromptSnapshotFiles();
144+
expect(files.length).toBeGreaterThan(0);
145+
const stats = getPluginModuleLoaderStats();
146+
expect(
147+
stats.calls - callsBefore,
148+
`prompt snapshot generation hit the jiti plugin loader; targets: ${stats.topSourceTransformTargets
149+
.map((entry) => entry.target)
150+
.join(", ")}`,
151+
).toBe(0);
152+
});
153+
134154
it("deletes stale generated snapshot artifacts", async () => {
135155
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-prompt-snapshot-stale-"));
136156
try {

‎test/scripts/run-additional-boundary-checks.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ async function waitForChildClose(
110110

111111
describe("run-additional-boundary-checks", () => {
112112
it("keeps prompt snapshot drift checks in their dedicated CI lane", () => {
113-
// The snapshot check regenerates prompts with real embedded-agent turns
114-
// (~2min); packing it into a boundary shard makes that shard the PR wall
115-
// clock, so it owns the check-prompt-snapshots lane instead.
113+
// The snapshot check regenerates prompt fixtures over the full agent
114+
// tool/prompt import graph; packing it into a boundary shard makes that
115+
// shard the PR wall clock, so it owns the check-prompt-snapshots lane.
116116
expect(BOUNDARY_CHECKS.some((check) => check.label === "prompt:snapshots:check")).toBe(false);
117117
const workflow = fs.readFileSync(".github/workflows/ci.yml", "utf8");
118118
expect(workflow).toContain("check_name: check-prompt-snapshots");

0 commit comments

Comments
 (0)