11import { getRuntimeConfig } from "../io.js" ;
22import { resolveStorePath } from "./paths.js" ;
3+ import { hasMismatchedCaseSensitiveDeliveryProof } from "./store-entry.js" ;
34import {
45 archiveRemovedSessionTranscripts ,
56 loadSessionStore ,
@@ -63,6 +64,11 @@ export async function patchSessionLifecycleEntry(
6364 if ( ! existing ) {
6465 return { changed : false , entry : null } ;
6566 }
67+ const storageKey = resolveLifecyclePatchStorageKey ( {
68+ store,
69+ resolved,
70+ requestedKey : scope . sessionKey ,
71+ } ) ;
6672 const patch = await update ( structuredClone ( existing ) , {
6773 existingEntry : resolved . existing ? structuredClone ( resolved . existing ) : undefined ,
6874 sessionKey : resolved . normalizedKey ,
@@ -78,8 +84,11 @@ export async function patchSessionLifecycleEntry(
7884 : options . preserveActivity
7985 ? mergeSessionEntryPreserveActivity ( existing , patch )
8086 : mergeSessionEntry ( existing , patch ) ;
81- store [ resolved . normalizedKey ] = next ;
87+ store [ storageKey ] = next ;
8288 for ( const legacyKey of resolved . legacyKeys ) {
89+ if ( legacyKey === storageKey ) {
90+ continue ;
91+ }
8392 delete store [ legacyKey ] ;
8493 }
8594 return { changed : true , entry : next } ;
@@ -219,6 +228,35 @@ function collectReferencedSessionIds(store: Record<string, SessionEntry>): Set<s
219228 ) ;
220229}
221230
231+ // Preserve the concrete row key for behavior-neutral file-backed updates. Callers
232+ // still receive the normalized key in context for canonical decisions.
233+ function resolveLifecyclePatchStorageKey ( params : {
234+ store : Record < string , SessionEntry > ;
235+ requestedKey : string ;
236+ resolved : ReturnType < typeof resolveSessionStoreEntry > ;
237+ } ) : string {
238+ if (
239+ params . resolved . existing &&
240+ params . store [ params . resolved . normalizedKey ] === params . resolved . existing
241+ ) {
242+ return params . resolved . normalizedKey ;
243+ }
244+ const canonicalEntry = params . store [ params . resolved . normalizedKey ] ;
245+ if (
246+ canonicalEntry &&
247+ ! hasMismatchedCaseSensitiveDeliveryProof ( canonicalEntry , params . resolved . normalizedKey )
248+ ) {
249+ return params . resolved . normalizedKey ;
250+ }
251+ const existingLegacyKey = params . resolved . legacyKeys . find (
252+ ( legacyKey ) => params . store [ legacyKey ] === params . resolved . existing ,
253+ ) ;
254+ if ( existingLegacyKey ) {
255+ return existingLegacyKey ;
256+ }
257+ return params . requestedKey . trim ( ) || params . resolved . normalizedKey ;
258+ }
259+
222260function rememberRemovedSessionFile (
223261 removedSessionFiles : Map < string , string | undefined > ,
224262 entry : SessionEntry ,
0 commit comments