@@ -236,6 +236,126 @@ describe("TaskHistoryStore cross-instance safety", () => {
236236 } )
237237 } )
238238
239+ describe ( "setTitleManually" , ( ) => {
240+ it ( "removes a title another instance wrote even when this cache has none" , async ( ) => {
241+ await storeA . initialize ( )
242+ await storeB . initialize ( )
243+ await storeA . upsert ( makeHistoryItem ( { id : "manual-clear" , task : "Prompt" } ) )
244+ await storeB . reconcile ( )
245+
246+ // Instance B renames the task on disk; instance A's cache still has no title.
247+ await storeB . atomicReadAndUpdate ( "manual-clear" , ( item ) => ( { ...item , title : "Manual title" } ) )
248+ expect ( storeA . get ( "manual-clear" ) ?. title ) . toBeUndefined ( )
249+
250+ // A clear from instance A must remove the disk title, not just the cached one.
251+ await expect ( storeA . setTitleManually ( "manual-clear" , undefined ) ) . resolves . toMatchObject ( {
252+ id : "manual-clear" ,
253+ titleCleared : true ,
254+ } )
255+
256+ const filePath = path . join ( tmpDir , "tasks" , "manual-clear" , GlobalFileNames . historyItem )
257+ const onDisk = JSON . parse ( await fs . readFile ( filePath , "utf8" ) )
258+ expect ( onDisk . title ) . toBeUndefined ( )
259+ expect ( onDisk . titleCleared ) . toBe ( true )
260+ } )
261+
262+ it ( "keeps a manual title that commits while the automatic title is generating" , async ( ) => {
263+ await storeA . initialize ( )
264+ await storeB . initialize ( )
265+ await storeA . upsert ( makeHistoryItem ( { id : "manual-wins" , task : "Prompt" } ) )
266+ await storeB . reconcile ( )
267+
268+ await storeB . setTitleManually ( "manual-wins" , "Manual title" )
269+ // The generating host still has the untitled record cached.
270+ expect ( storeA . get ( "manual-wins" ) ?. title ) . toBeUndefined ( )
271+
272+ await expect ( storeA . setTitleIfUnset ( "manual-wins" , "Auto title" ) ) . resolves . toBe ( "conflict" )
273+ expect ( storeA . get ( "manual-wins" ) ?. title ) . toBe ( "Manual title" )
274+ } )
275+
276+ it ( "returns undefined for a task that is not in the cache" , async ( ) => {
277+ await storeA . initialize ( )
278+ await expect ( storeA . setTitleManually ( "missing-id" , "Manual title" ) ) . resolves . toBeUndefined ( )
279+ } )
280+
281+ it ( "drops the cache entry for a malformed disk record without rewriting it" , async ( ) => {
282+ await storeA . initialize ( )
283+ await storeA . upsert ( makeHistoryItem ( { id : "manual-malformed" , task : "Prompt" } ) )
284+ const filePath = path . join ( tmpDir , "tasks" , "manual-malformed" , GlobalFileNames . historyItem )
285+ const malformed = JSON . stringify ( { id : 123 } )
286+ await fs . writeFile ( filePath , malformed , "utf8" )
287+
288+ await expect ( storeA . setTitleManually ( "manual-malformed" , "Manual title" ) ) . resolves . toBeUndefined ( )
289+
290+ expect ( storeA . get ( "manual-malformed" ) ) . toBeUndefined ( )
291+ expect ( await fs . readFile ( filePath , "utf8" ) ) . toBe ( malformed )
292+ } )
293+
294+ it ( "drops the cache entry when the disk record belongs to another task" , async ( ) => {
295+ await storeA . initialize ( )
296+ await storeA . upsert ( makeHistoryItem ( { id : "manual-mismatched" , task : "Prompt" } ) )
297+ const filePath = path . join ( tmpDir , "tasks" , "manual-mismatched" , GlobalFileNames . historyItem )
298+ const otherTask = JSON . stringify ( makeHistoryItem ( { id : "other-task" } ) , null , "\t" )
299+ await fs . writeFile ( filePath , otherTask , "utf8" )
300+
301+ await expect ( storeA . setTitleManually ( "manual-mismatched" , "Manual title" ) ) . resolves . toBeUndefined ( )
302+
303+ expect ( storeA . get ( "manual-mismatched" ) ) . toBeUndefined ( )
304+ expect ( await fs . readFile ( filePath , "utf8" ) ) . toBe ( otherTask )
305+ } )
306+
307+ it ( "re-throws unexpected errors from safeWriteJson" , async ( ) => {
308+ await storeA . initialize ( )
309+ await storeA . upsert ( makeHistoryItem ( { id : "manual-io-err" , task : "Prompt" } ) )
310+ const { safeWriteJson } = await import ( "../../../utils/safeWriteJson" )
311+ vi . mocked ( safeWriteJson ) . mockRejectedValueOnce ( new Error ( "disk IO error" ) )
312+
313+ await expect ( storeA . setTitleManually ( "manual-io-err" , "Manual title" ) ) . rejects . toThrow ( "disk IO error" )
314+ } )
315+
316+ it ( "notifies onWrite when a manual title is applied" , async ( ) => {
317+ const onWriteSpy = vi . fn ( ) . mockResolvedValue ( undefined )
318+ const storeWithOnWrite = new TaskHistoryStore ( tmpDir , { onWrite : onWriteSpy } )
319+ await storeWithOnWrite . initialize ( )
320+ await storeWithOnWrite . upsert ( makeHistoryItem ( { id : "manual-onwrite" , task : "Prompt" } ) )
321+ onWriteSpy . mockClear ( )
322+
323+ await expect ( storeWithOnWrite . setTitleManually ( "manual-onwrite" , "Notified title" ) ) . resolves . toMatchObject ( {
324+ id : "manual-onwrite" ,
325+ title : "Notified title" ,
326+ } )
327+ expect ( onWriteSpy ) . toHaveBeenCalledWith (
328+ expect . arrayContaining ( [ expect . objectContaining ( { id : "manual-onwrite" , title : "Notified title" } ) ] ) ,
329+ )
330+ storeWithOnWrite . dispose ( )
331+ } )
332+
333+ it ( "writes a title on a record this cache has never seen the latest fields of" , async ( ) => {
334+ await storeA . initialize ( )
335+ await storeB . initialize ( )
336+ await storeA . upsert ( makeHistoryItem ( { id : "manual-set" , task : "Prompt" , tokensIn : 1 } ) )
337+ await storeB . reconcile ( )
338+
339+ await storeB . atomicReadAndUpdate ( "manual-set" , ( item ) => ( { ...item , tokensIn : 99 } ) )
340+ await storeA . setTitleManually ( "manual-set" , "Manual title" )
341+
342+ const filePath = path . join ( tmpDir , "tasks" , "manual-set" , GlobalFileNames . historyItem )
343+ const onDisk = JSON . parse ( await fs . readFile ( filePath , "utf8" ) )
344+ expect ( onDisk ) . toMatchObject ( { title : "Manual title" , tokensIn : 99 } )
345+ } )
346+
347+ it ( "drops the task when another instance deleted it" , async ( ) => {
348+ await storeA . initialize ( )
349+ await storeB . initialize ( )
350+ await storeA . upsert ( makeHistoryItem ( { id : "manual-deleted" , task : "Prompt" } ) )
351+ await storeB . reconcile ( )
352+ await storeB . delete ( "manual-deleted" )
353+
354+ await expect ( storeA . setTitleManually ( "manual-deleted" , "Manual title" ) ) . resolves . toBeUndefined ( )
355+ expect ( storeA . get ( "manual-deleted" ) ) . toBeUndefined ( )
356+ } )
357+ } )
358+
239359 it ( "two instances can write different tasks without conflict" , async ( ) => {
240360 await storeA . initialize ( )
241361 await storeB . initialize ( )
0 commit comments