Skip to content

Commit aa15534

Browse files
committed
refactor: deduplicate Codex prompt snapshot fixtures
1 parent 7cc2e6d commit aa15534

8 files changed

Lines changed: 316 additions & 3238 deletions

File tree

‎scripts/generate-prompt-snapshots.ts‎

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ const oxfmtPath = path.resolve(
2424
const execFileAsync = promisify(execFile);
2525

2626
type PromptSnapshotFile = Awaited<ReturnType<typeof createHappyPathPromptSnapshotFiles>>[number];
27+
type CodexDynamicToolSnapshotSpec = { name: string };
28+
type CodexDynamicToolSnapshotOverrides = {
29+
base: string;
30+
replace: Record<string, CodexDynamicToolSnapshotSpec>;
31+
};
32+
33+
const CODEX_DYNAMIC_TOOL_SNAPSHOT_PREFIX = "codex-dynamic-tools.";
34+
const CODEX_DYNAMIC_TOOL_BASE_SNAPSHOT = `${CODEX_DYNAMIC_TOOL_SNAPSHOT_PREFIX}telegram-direct.json`;
2735

2836
function describeError(error: unknown): string {
2937
return error instanceof Error ? error.message : String(error);
@@ -60,8 +68,49 @@ async function readSnapshotFiles(root: string, files: PromptSnapshotFile[]) {
6068
);
6169
}
6270

63-
export async function createFormattedPromptSnapshotFiles(): Promise<PromptSnapshotFile[]> {
64-
const files = await createHappyPathPromptSnapshotFiles();
71+
/** Keep complete Codex tool specs on the wire; deduplicate only their committed review fixtures. */
72+
function factorCodexDynamicToolSnapshotFiles(files: PromptSnapshotFile[]): PromptSnapshotFile[] {
73+
const base = files.find((file) => path.basename(file.path) === CODEX_DYNAMIC_TOOL_BASE_SNAPSHOT);
74+
if (!base) {
75+
return files;
76+
}
77+
const baseTools = JSON.parse(base.content) as CodexDynamicToolSnapshotSpec[];
78+
if (new Set(baseTools.map((tool) => tool.name)).size !== baseTools.length) {
79+
return files;
80+
}
81+
82+
return files.map((file) => {
83+
const fileName = path.basename(file.path);
84+
if (
85+
fileName === CODEX_DYNAMIC_TOOL_BASE_SNAPSHOT ||
86+
!fileName.startsWith(CODEX_DYNAMIC_TOOL_SNAPSHOT_PREFIX) ||
87+
!fileName.endsWith(".json")
88+
) {
89+
return file;
90+
}
91+
const tools = JSON.parse(file.content) as CodexDynamicToolSnapshotSpec[];
92+
if (
93+
tools.length !== baseTools.length ||
94+
tools.some((tool, index) => tool.name !== baseTools[index]?.name)
95+
) {
96+
return file;
97+
}
98+
const replace = Object.fromEntries(
99+
tools.flatMap((tool, index) =>
100+
JSON.stringify(tool) === JSON.stringify(baseTools[index]) ? [] : [[tool.name, tool]],
101+
),
102+
);
103+
const overrides: CodexDynamicToolSnapshotOverrides = {
104+
base: CODEX_DYNAMIC_TOOL_BASE_SNAPSHOT,
105+
replace,
106+
};
107+
return { ...file, content: `${JSON.stringify(overrides, null, 2)}\n` };
108+
});
109+
}
110+
111+
async function formatPromptSnapshotFiles(
112+
files: PromptSnapshotFile[],
113+
): Promise<PromptSnapshotFile[]> {
65114
const tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-prompt-snapshots-"));
66115
try {
67116
await writeSnapshotFiles(tmpRoot, files);
@@ -72,6 +121,56 @@ export async function createFormattedPromptSnapshotFiles(): Promise<PromptSnapsh
72121
}
73122
}
74123

124+
export async function createFormattedPromptSnapshotFiles(): Promise<PromptSnapshotFile[]> {
125+
const files = await createHappyPathPromptSnapshotFiles();
126+
return await formatPromptSnapshotFiles(factorCodexDynamicToolSnapshotFiles(files));
127+
}
128+
129+
/** Materialize one complete, formatted Codex dynamic-tool catalog for human review. */
130+
export async function materializeCodexDynamicToolSnapshot(scenario: string): Promise<string> {
131+
if (!/^[a-z][a-z0-9-]*$/u.test(scenario)) {
132+
throw new Error(`Invalid Codex dynamic-tool snapshot scenario: ${scenario}`);
133+
}
134+
const snapshotPath = path.join(
135+
CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR,
136+
`${CODEX_DYNAMIC_TOOL_SNAPSHOT_PREFIX}${scenario}.json`,
137+
);
138+
const snapshot = JSON.parse(await fs.readFile(path.resolve(repoRoot, snapshotPath), "utf8")) as
139+
| CodexDynamicToolSnapshotSpec[]
140+
| CodexDynamicToolSnapshotOverrides;
141+
let tools: CodexDynamicToolSnapshotSpec[];
142+
if (Array.isArray(snapshot)) {
143+
tools = snapshot;
144+
} else {
145+
if (snapshot.base !== CODEX_DYNAMIC_TOOL_BASE_SNAPSHOT) {
146+
throw new Error(`Unsupported Codex dynamic-tool snapshot base: ${snapshot.base}`);
147+
}
148+
const basePath = path.join(CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR, snapshot.base);
149+
const base = JSON.parse(await fs.readFile(path.resolve(repoRoot, basePath), "utf8")) as
150+
| CodexDynamicToolSnapshotSpec[]
151+
| CodexDynamicToolSnapshotOverrides;
152+
if (!Array.isArray(base)) {
153+
throw new Error("The canonical Codex dynamic-tool snapshot must contain complete tools");
154+
}
155+
const baseNames = new Set(base.map((tool) => tool.name));
156+
for (const [name, replacement] of Object.entries(snapshot.replace)) {
157+
if (!baseNames.has(name) || replacement.name !== name) {
158+
throw new Error(`Invalid Codex dynamic-tool snapshot replacement: ${name}`);
159+
}
160+
}
161+
tools = base.map((tool) =>
162+
Object.hasOwn(snapshot.replace, tool.name) ? snapshot.replace[tool.name]! : tool,
163+
);
164+
}
165+
const [formatted] = await formatPromptSnapshotFiles([
166+
{ path: snapshotPath, content: `${JSON.stringify(tools, null, 2)}\n` },
167+
]);
168+
if (!formatted) {
169+
throw new Error(`Failed to materialize Codex dynamic-tool snapshot: ${scenario}`);
170+
}
171+
return formatted.content;
172+
}
173+
75174
async function writeSnapshots() {
76175
const files = await createFormattedPromptSnapshotFiles();
77176
await fs.mkdir(path.resolve(repoRoot, CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR), {
@@ -117,6 +216,18 @@ async function checkSnapshots() {
117216
}
118217

119218
async function runPromptSnapshotGenerator(argv = process.argv.slice(2)) {
219+
if (argv[0] === "--materialize") {
220+
const scenario = argv[1];
221+
if (!scenario || argv.length !== 2) {
222+
console.error(
223+
"Usage: node --import tsx scripts/generate-prompt-snapshots.ts --materialize <scenario>",
224+
);
225+
process.exitCode = 2;
226+
return;
227+
}
228+
process.stdout.write(await materializeCodexDynamicToolSnapshot(scenario));
229+
return;
230+
}
120231
const mode = argv.includes("--write") ? "write" : argv.includes("--check") ? "check" : undefined;
121232

122233
if (!mode) {

‎test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ The workspace bootstrap simulation includes dummy workspace contents so prompt r
1414

1515
The tool catalog is pinned to the canonical happy-path OpenClaw tools so optional locally installed plugin tools do not create fixture churn.
1616

17+
The Telegram JSON is the complete shared tool catalog. Discord and heartbeat JSON fixtures contain readable, complete replacements for their changed top-level tools or namespaces; their `base` field points to the Telegram catalog.
18+
19+
Materialize the complete, formatted tool catalog for a scenario with:
20+
21+
```sh
22+
node --import tsx scripts/generate-prompt-snapshots.ts --materialize discord-group
23+
```
24+
25+
Replace `discord-group` with `heartbeat-turn` to inspect the complete heartbeat catalog.
26+
1727
The Codex model prompt fixture is generated from the same Codex model catalog/cache shape that the Codex runtime uses for remote model metadata. Regenerate it from Codex's runtime cache or, when present, a local Codex checkout with:
1828

1929
```sh

0 commit comments

Comments
 (0)