@@ -16,7 +16,7 @@ afterEach(async () => {
1616test ( "OpenCodeAdapter collect parses export payload wrapped in extra text" , async ( ) => {
1717 const tempDir = await createTempDir ( ) ;
1818 const adapter = new ExportingOpenCodeAdapter ( {
19- exportStdout : JSON . stringify ( {
19+ exportStdout : `Exporting session: ses_123\n ${ JSON . stringify ( {
2020 info : { id : "ses_123" } ,
2121 messages : [
2222 {
@@ -36,7 +36,7 @@ test("OpenCodeAdapter collect parses export payload wrapped in extra text", asyn
3636 ] ,
3737 } ,
3838 ] ,
39- } ) ,
39+ } ) } ` ,
4040 } ) ;
4141
4242 const input = {
@@ -88,6 +88,46 @@ test("OpenCodeAdapter collect parses export payload wrapped in extra text", asyn
8888 } ) ;
8989} ) ;
9090
91+ test ( "OpenCodeAdapter collect falls back to runtime log session id" , async ( ) => {
92+ const tempDir = await createTempDir ( ) ;
93+ const adapter = new ExportingOpenCodeAdapter ( {
94+ exportStdout : JSON . stringify ( {
95+ info : { id : "ses_fromlog" } ,
96+ messages : [ ] ,
97+ } ) ,
98+ expectedSessionId : "ses_fromlog" ,
99+ } ) ;
100+
101+ const input = {
102+ ...createRunInput ( ) ,
103+ cwd : tempDir ,
104+ artifactsDir : path . join ( tempDir , "artifacts" ) ,
105+ } satisfies RunInput ;
106+ const logDir = path . join ( input . artifactsDir , "opencode-xdg" , "data" , "opencode" , "log" ) ;
107+ await mkdir ( logDir , { recursive : true } ) ;
108+ await writeFile (
109+ path . join ( logDir , "2026-05-15T115637.log" ) ,
110+ "INFO service=session id=ses_fromlog created\n" ,
111+ "utf8" ,
112+ ) ;
113+
114+ const stdoutPath = path . join ( tempDir , "stdout.log" ) ;
115+ const stderrPath = path . join ( tempDir , "stderr.log" ) ;
116+ await writeFile ( stdoutPath , "" , "utf8" ) ;
117+ await writeFile ( stderrPath , "Database migration complete.\n" , "utf8" ) ;
118+
119+ const artifacts = await adapter . collect (
120+ {
121+ ...createArtifacts ( ) ,
122+ stdoutPath,
123+ stderrPath,
124+ } ,
125+ input ,
126+ ) ;
127+
128+ expect ( artifacts . sessionId ) . toBe ( "ses_fromlog" ) ;
129+ } ) ;
130+
91131test ( "OpenCodeAdapter collect fails when export output is invalid JSON" , async ( ) => {
92132 const tempDir = await createTempDir ( ) ;
93133 const adapter = new ExportingOpenCodeAdapter ( {
@@ -335,7 +375,7 @@ async function createTempDir(): Promise<string> {
335375class ExportingOpenCodeAdapter extends OpenCodeAdapter {
336376 exportEnv ?: Record < string , string > ;
337377
338- constructor ( private readonly testOptions : { exportStdout : string } ) {
378+ constructor ( private readonly testOptions : { exportStdout : string ; expectedSessionId ?: string } ) {
339379 super ( ) ;
340380 }
341381
@@ -346,7 +386,7 @@ class ExportingOpenCodeAdapter extends OpenCodeAdapter {
346386 options ?: { env ?: Record < string , string > } ,
347387 ) {
348388 expect ( command ) . toBe ( "opencode" ) ;
349- expect ( args ) . toEqual ( [ "export" , "ses_123" ] ) ;
389+ expect ( args ) . toEqual ( [ "export" , this . testOptions . expectedSessionId ?? "ses_123" ] ) ;
350390 this . exportEnv = options ?. env ;
351391
352392 const stdoutPath = path . join ( input . artifactsDir , "stdout.log" ) ;
0 commit comments