@@ -26,6 +26,13 @@ function mergeWithDisk(delta: Partial<HistoryItem>): (existing: unknown, incomin
2626 return ( existing , incoming ) => mergeHistoryDelta ( existing , incoming as HistoryItem , delta )
2727}
2828
29+ /** Thrown inside the `setTitleIfUnset` merge callback to abort the write without touching the file. */
30+ class TitleWriteSkipped extends Error {
31+ constructor ( readonly reason : "missing" | "titled" ) {
32+ super ( `title write skipped: ${ reason } ` )
33+ }
34+ }
35+
2936/**
3037 * Durable intent for the one repair that spans an active delegated child and
3138 * its parent. Task files remain authoritative; this file only records the
@@ -1002,7 +1009,7 @@ export class TaskHistoryStore {
10021009 * provider on the same storage path) saved a manual title, and a delta merge would
10031010 * otherwise overwrite it.
10041011 *
1005- * @returns "conflict" when a title already exists (the cache is refreshed from disk ).
1012+ * @returns "conflict" when a title already exists or the record was deleted on disk (the cache is refreshed).
10061013 * @throws If the task ID is not present in the cache.
10071014 */
10081015 public setTitleIfUnset ( taskId : string , title : string ) : Promise < "applied" | "conflict" > {
@@ -1017,23 +1024,37 @@ export class TaskHistoryStore {
10171024
10181025 const filePath = await this . getTaskFilePath ( taskId )
10191026 let written : HistoryItem = { ...current , title }
1020- let conflict = false
1021- await safeWriteJson ( filePath , written , {
1022- merge : ( existing , incoming ) => {
1023- if ( existing && typeof existing === "object" && ( existing as HistoryItem ) . title ) {
1024- conflict = true
1025- written = existing as HistoryItem
1026- return existing
1027+ try {
1028+ await safeWriteJson ( filePath , written , {
1029+ merge : ( existing , incoming ) => {
1030+ // Throwing aborts the write, so a deleted record is never recreated from the cache
1031+ // and an existing title is never rewritten.
1032+ if ( ! existing || typeof existing !== "object" || ! ( "id" in existing ) ) {
1033+ throw new TitleWriteSkipped ( "missing" )
1034+ }
1035+ if ( ( existing as HistoryItem ) . title ) {
1036+ throw new TitleWriteSkipped ( "titled" )
1037+ }
1038+ written = mergeHistoryDelta ( existing , incoming as HistoryItem , { title } )
1039+ return written
1040+ } ,
1041+ } )
1042+ } catch ( error ) {
1043+ if ( ! ( error instanceof TitleWriteSkipped ) ) {
1044+ throw error
1045+ }
1046+ if ( error . reason === "missing" ) {
1047+ this . cache . delete ( taskId )
1048+ } else {
1049+ const diskItem = await this . readTaskFile ( taskId )
1050+ if ( diskItem ) {
1051+ this . cache . set ( taskId , diskItem )
10271052 }
1028- written = mergeHistoryDelta ( existing , incoming as HistoryItem , { title } )
1029- return written
1030- } ,
1031- } )
1032-
1033- this . cache . set ( taskId , written )
1034- if ( conflict ) {
1053+ }
10351054 return "conflict"
10361055 }
1056+
1057+ this . cache . set ( taskId , written )
10371058 if ( this . onWrite ) {
10381059 await this . onWrite ( this . getAll ( ) )
10391060 }
0 commit comments