@@ -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+
5784export 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
6996async 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
76105async 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 }
0 commit comments