@@ -14,11 +14,7 @@ import {
1414 type SessionEntry ,
1515 updateSessionStore ,
1616} from "../config/sessions.js" ;
17- import {
18- resolveSessionFilePath ,
19- resolveSessionFilePathOptions ,
20- resolveSessionTranscriptPathInDir ,
21- } from "../config/sessions/paths.js" ;
17+ import { resolveSessionFilePath , resolveSessionFilePathOptions } from "../config/sessions/paths.js" ;
2218import { logVerbose } from "../globals.js" ;
2319import { createInternalHookEvent , triggerInternalHook } from "../hooks/internal-hooks.js" ;
2420import { closeTrackedBrowserTabsForSessions } from "../plugin-sdk/browser-runtime.js" ;
@@ -46,6 +42,38 @@ function getChannelRuntime() {
4642 return cachedChannelRuntime ;
4743}
4844
45+ function extractGeneratedTranscriptSessionId ( sessionFile ?: string ) : string | undefined {
46+ const trimmed = sessionFile ?. trim ( ) ;
47+ if ( ! trimmed ) {
48+ return undefined ;
49+ }
50+ const base = path . basename ( trimmed ) ;
51+ if ( ! base . endsWith ( ".jsonl" ) ) {
52+ return undefined ;
53+ }
54+ const withoutExt = base . slice ( 0 , - ".jsonl" . length ) ;
55+ const topicIndex = withoutExt . indexOf ( "-topic-" ) ;
56+ if ( topicIndex > 0 ) {
57+ const topicSessionId = withoutExt . slice ( 0 , topicIndex ) ;
58+ return looksLikeGeneratedSessionId ( topicSessionId ) ? topicSessionId : undefined ;
59+ }
60+ const forkMatch = withoutExt . match (
61+ / ^ ( \d { 4 } - \d { 2 } - \d { 2 } T [ \w - ] + (?: Z | [ + - ] \d { 2 } (?: - \d { 2 } ) ? ) ? ) _ ( .+ ) $ / ,
62+ ) ;
63+ if ( forkMatch ?. [ 2 ] ) {
64+ return looksLikeGeneratedSessionId ( forkMatch [ 2 ] ) ? forkMatch [ 2 ] : undefined ;
65+ }
66+ return looksLikeGeneratedSessionId ( withoutExt ) ? withoutExt : undefined ;
67+ }
68+
69+ function looksLikeGeneratedSessionId ( value : string ) : boolean {
70+ return / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 8 ] [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i. test ( value ) ;
71+ }
72+
73+ function isGeneratedTranscriptPath ( sessionFile : string ) : boolean {
74+ return Boolean ( extractGeneratedTranscriptSessionId ( sessionFile ) ) ;
75+ }
76+
4977function stripRuntimeModelState ( entry ?: SessionEntry ) : SessionEntry | undefined {
5078 if ( ! entry ) {
5179 return entry ;
@@ -319,26 +347,14 @@ export async function performGatewaySessionReset(params: {
319347 storePath,
320348 agentId : sessionAgentId ,
321349 } ) ;
322- // Determine whether the current entry uses a custom transcript directory
323- // (i.e. one outside the default sessions dir). If so, preserve that
324- // directory and only rotate the filename (new sessionId) so spawned/owned
325- // sessions don't silently relocate their transcripts. See #55474.
326- const defaultSessionFile = resolveSessionFilePath (
327- currentEntry ?. sessionId ?? nextSessionId ,
328- undefined ,
329- pathOpts ,
330- ) ;
331- const defaultSessionsDir = path . dirname ( defaultSessionFile ) ;
332- const explicitSessionFileDir = currentEntry ?. sessionFile
333- ? path . dirname ( path . resolve ( currentEntry . sessionFile ) )
350+ const defaultSessionFile = resolveSessionFilePath ( nextSessionId , undefined , pathOpts ) ;
351+ const explicitSessionFile = currentEntry ?. sessionFile
352+ ? resolveSessionFilePath ( nextSessionId , { sessionFile : currentEntry . sessionFile } , pathOpts )
334353 : undefined ;
335- const hasCustomSessionFileDir =
336- explicitSessionFileDir !== undefined &&
337- path . relative ( defaultSessionsDir , explicitSessionFileDir ) . startsWith ( ".." ) ;
338-
339- const sessionFile = hasCustomSessionFileDir
340- ? resolveSessionTranscriptPathInDir ( nextSessionId , explicitSessionFileDir )
341- : resolveSessionFilePath ( nextSessionId , undefined , pathOpts ) ;
354+ const sessionFile =
355+ explicitSessionFile && ! isGeneratedTranscriptPath ( explicitSessionFile )
356+ ? explicitSessionFile
357+ : defaultSessionFile ;
342358 const nextEntry : SessionEntry = {
343359 sessionId : nextSessionId ,
344360 sessionFile,
0 commit comments