@@ -1413,7 +1413,7 @@ function writeSessionLog(session: SessionState): void {
14131413 const safeName = session . name . replace ( / [ ^ a - z A - Z 0 - 9 . _ - ] / g, '_' ) ;
14141414 const timestamp = new Date ( session . createdAt ) . toISOString ( ) . replace ( / [: .] / g, '-' ) ;
14151415 const scriptPath = path . join ( sessionsDir , `${ safeName } -${ timestamp } .ad` ) ;
1416- const filePath = path . join ( sessionsDir , ` ${ safeName } - ${ timestamp } .json` ) ;
1416+ const filePath = resolveSessionJsonPath ( session , safeName , timestamp ) ;
14171417 const payload = {
14181418 name : session . name ,
14191419 device : session . device ,
@@ -1425,13 +1425,41 @@ function writeSessionLog(session: SessionState): void {
14251425 const script = formatScript ( session , payload . optimizedActions ) ;
14261426 fs . writeFileSync ( scriptPath , script ) ;
14271427 if ( session . actions . some ( ( action ) => action . flags ?. recordJson ) ) {
1428+ fs . mkdirSync ( path . dirname ( filePath ) , { recursive : true } ) ;
14281429 fs . writeFileSync ( filePath , JSON . stringify ( payload , null , 2 ) ) ;
14291430 }
14301431 } catch {
14311432 // ignore
14321433 }
14331434}
14341435
1436+ function resolveSessionJsonPath ( session : SessionState , safeName : string , timestamp : string ) : string {
1437+ const defaultFile = path . join ( sessionsDir , `${ safeName } -${ timestamp } .json` ) ;
1438+ const actionWithOut = [ ...session . actions ]
1439+ . reverse ( )
1440+ . find ( ( action ) => action . flags ?. recordJson && typeof action . flags ?. out === 'string' && action . flags . out . trim ( ) . length > 0 ) ;
1441+ if ( ! actionWithOut || ! actionWithOut . flags ?. out ) {
1442+ return defaultFile ;
1443+ }
1444+
1445+ const rawOut = actionWithOut . flags . out . trim ( ) ;
1446+ const resolvedOut = expandHome ( rawOut ) ;
1447+ const wantsDirectory = rawOut . endsWith ( '/' ) || rawOut . endsWith ( '\\' ) ;
1448+ if ( wantsDirectory ) {
1449+ return path . join ( resolvedOut , `${ safeName } -${ timestamp } .json` ) ;
1450+ }
1451+
1452+ try {
1453+ if ( fs . existsSync ( resolvedOut ) && fs . statSync ( resolvedOut ) . isDirectory ( ) ) {
1454+ return path . join ( resolvedOut , `${ safeName } -${ timestamp } .json` ) ;
1455+ }
1456+ } catch {
1457+ return defaultFile ;
1458+ }
1459+
1460+ return resolvedOut ;
1461+ }
1462+
14351463function defaultTracePath ( session : SessionState ) : string {
14361464 const safeName = session . name . replace ( / [ ^ a - z A - Z 0 - 9 . _ - ] / g, '_' ) ;
14371465 const timestamp = new Date ( ) . toISOString ( ) . replace ( / [: .] / g, '-' ) ;
0 commit comments