@@ -54,6 +54,16 @@ function historyFilePath(storagePath: string, taskId: string): string {
5454 return path . join ( storagePath , "tasks" , taskId , GlobalFileNames . historyItem )
5555}
5656
57+ // The lock key is canonicalized through the parent directory, so the expected key is
58+ // the canonical directory plus the basename rather than the path built from
59+ // os.tmpdir(), which can be an 8.3 short path on the Windows runner. canonicalDirKey,
60+ // in src/services/file-safety/safeWriteText.ts, canonicalizes the nearest existing
61+ // ancestor with fs/promises realpath, and that call expands the short spelling, so the
62+ // expectation is canonicalized the way the delete takes the lock.
63+ async function canonicalKey ( filePath : string ) : Promise < string > {
64+ return path . join ( await actualFs . realpath ( path . dirname ( filePath ) ) , path . basename ( filePath ) )
65+ }
66+
5767function storeInternals ( store : TaskHistoryStore ) : {
5868 cache : Map < string , HistoryItem >
5969 taskFileMtimes : Map < string , number >
@@ -152,12 +162,17 @@ describe("TaskHistoryStore best-effort deletion semantics", () => {
152162 const store = createStore ( )
153163 await store . initialize ( )
154164 await store . upsert ( makeHistoryItem ( { id : "locked-delete" } ) )
165+ // The lock spy is created once for the whole file, so clear it to make the
166+ // assertion below about this delete and not about every case before it.
167+ vi . mocked ( withFileLock ) . mockClear ( )
155168 onWrite . mockClear ( )
156169
157170 await expect ( store . delete ( "locked-delete" ) ) . resolves . toBeUndefined ( )
158171
172+ // The delete takes the lock on the canonical key, so the expectation is built the
173+ // same way rather than from the spelling os.tmpdir handed to mkdtemp.
159174 expect ( vi . mocked ( withFileLock ) ) . toHaveBeenCalledWith (
160- historyFilePath ( storagePath , "locked-delete" ) ,
175+ await canonicalKey ( historyFilePath ( storagePath , "locked-delete" ) ) ,
161176 expect . any ( Function ) ,
162177 )
163178 await expect ( fs . access ( historyFilePath ( storagePath , "locked-delete" ) ) ) . rejects . toMatchObject ( {
0 commit comments