Skip to content

Commit 641f2de

Browse files
committed
fix: clean stale prompt snapshots
1 parent 151670d commit 641f2de

2 files changed

Lines changed: 55 additions & 16 deletions

File tree

scripts/generate-prompt-snapshots.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ async function readSnapshotFiles(root: string, files: PromptSnapshotFile[]) {
5454
);
5555
}
5656

57+
async function listCommittedSnapshotArtifactPaths(root: string): Promise<string[]> {
58+
let committedEntries: string[];
59+
try {
60+
committedEntries = await fs.readdir(path.resolve(root, HAPPY_PATH_PROMPT_SNAPSHOT_DIR));
61+
} catch (error) {
62+
if (!hasErrorCode(error, "ENOENT")) {
63+
throw error;
64+
}
65+
committedEntries = [];
66+
}
67+
return committedEntries
68+
.filter((entry) => entry.endsWith(".md") || entry.endsWith(".json"))
69+
.map((entry) => path.join(HAPPY_PATH_PROMPT_SNAPSHOT_DIR, entry));
70+
}
71+
72+
export async function deleteStalePromptSnapshotFiles(
73+
root: string,
74+
files: Array<{ path: string }>,
75+
): Promise<string[]> {
76+
const expectedPaths = new Set(files.map((file) => file.path));
77+
const stalePaths = (await listCommittedSnapshotArtifactPaths(root)).filter(
78+
(snapshotPath) => !expectedPaths.has(snapshotPath),
79+
);
80+
await Promise.all(stalePaths.map((snapshotPath) => fs.rm(path.resolve(root, snapshotPath))));
81+
return stalePaths;
82+
}
83+
5784
export async function createFormattedPromptSnapshotFiles(): Promise<PromptSnapshotFile[]> {
5885
const files = createHappyPathPromptSnapshotFiles();
5986
const tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-prompt-snapshots-"));
@@ -69,8 +96,10 @@ export async function createFormattedPromptSnapshotFiles(): Promise<PromptSnapsh
6996
async function writeSnapshots() {
7097
const files = await createFormattedPromptSnapshotFiles();
7198
await fs.mkdir(path.resolve(repoRoot, HAPPY_PATH_PROMPT_SNAPSHOT_DIR), { recursive: true });
99+
const deleted = await deleteStalePromptSnapshotFiles(repoRoot, files);
72100
await writeSnapshotFiles(repoRoot, files);
73-
console.log(`Wrote ${files.length} prompt snapshot files.`);
101+
const deletedSummary = deleted.length > 0 ? ` Deleted ${deleted.length} stale file(s).` : "";
102+
console.log(`Wrote ${files.length} prompt snapshot files.${deletedSummary}`);
74103
}
75104

76105
async function checkSnapshots() {
@@ -90,20 +119,7 @@ async function checkSnapshots() {
90119
mismatches.push(`${file.path}: differs from generated output`);
91120
}
92121
}
93-
let committedEntries: string[];
94-
try {
95-
committedEntries = await fs.readdir(path.resolve(repoRoot, HAPPY_PATH_PROMPT_SNAPSHOT_DIR));
96-
} catch (error) {
97-
if (!hasErrorCode(error, "ENOENT")) {
98-
throw error;
99-
}
100-
committedEntries = [];
101-
}
102-
for (const entry of committedEntries) {
103-
if (!entry.endsWith(".md") && !entry.endsWith(".json")) {
104-
continue;
105-
}
106-
const snapshotPath = path.join(HAPPY_PATH_PROMPT_SNAPSHOT_DIR, entry);
122+
for (const snapshotPath of await listCommittedSnapshotArtifactPaths(repoRoot)) {
107123
if (!expectedPaths.has(snapshotPath)) {
108124
mismatches.push(`${snapshotPath}: stale file (not generated)`);
109125
}

test/scripts/prompt-snapshots.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import fs from "node:fs";
2+
import os from "node:os";
23
import path from "node:path";
34
import { describe, expect, it } from "vitest";
4-
import { createFormattedPromptSnapshotFiles } from "../../scripts/generate-prompt-snapshots.js";
5+
import {
6+
createFormattedPromptSnapshotFiles,
7+
deleteStalePromptSnapshotFiles,
8+
} from "../../scripts/generate-prompt-snapshots.js";
59
import { HAPPY_PATH_PROMPT_SNAPSHOT_DIR } from "../helpers/agents/happy-path-prompt-snapshots.js";
610

711
describe("happy path prompt snapshots", () => {
@@ -17,4 +21,23 @@ describe("happy path prompt snapshots", () => {
1721
.map((entry) => path.join(HAPPY_PATH_PROMPT_SNAPSHOT_DIR, entry));
1822
expect(committed.toSorted()).toEqual([...expectedPaths].toSorted());
1923
});
24+
25+
it("deletes stale generated snapshot artifacts", async () => {
26+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-prompt-snapshot-stale-"));
27+
try {
28+
const snapshotDir = path.join(root, HAPPY_PATH_PROMPT_SNAPSHOT_DIR);
29+
fs.mkdirSync(snapshotDir, { recursive: true });
30+
const stalePath = path.join(HAPPY_PATH_PROMPT_SNAPSHOT_DIR, "stale-snapshot.md");
31+
fs.writeFileSync(path.join(root, stalePath), "stale\n");
32+
33+
const deleted = await deleteStalePromptSnapshotFiles(root, [
34+
{ path: path.join(HAPPY_PATH_PROMPT_SNAPSHOT_DIR, "current.md") },
35+
]);
36+
37+
expect(deleted).toEqual([stalePath]);
38+
expect(fs.existsSync(path.join(root, stalePath))).toBe(false);
39+
} finally {
40+
fs.rmSync(root, { recursive: true, force: true });
41+
}
42+
});
2043
});

0 commit comments

Comments
 (0)