@@ -67,7 +67,10 @@ const runtimeMock = {
6767 promptAsyncError : null as Error | null ,
6868 closeError : null as Error | null ,
6969 messages : [ ] as MessageEntry [ ] ,
70+ sessionMessagesCalls : [ ] as string [ ] ,
7071 subscribedEvents : [ ] as unknown [ ] ,
72+ eventSubscribeCalls : 0 ,
73+ eventSubscribeError : null as Error | null ,
7174 sessionGetIds : [ ] as string [ ] ,
7275 missingSessionIds : new Set < string > ( ) ,
7376 transientErrorSessionIds : new Set < string > ( ) ,
@@ -87,7 +90,10 @@ const runtimeMock = {
8790 this . state . promptAsyncError = null ;
8891 this . state . closeError = null ;
8992 this . state . messages = [ ] ;
93+ this . state . sessionMessagesCalls . length = 0 ;
9094 this . state . subscribedEvents = [ ] ;
95+ this . state . eventSubscribeCalls = 0 ;
96+ this . state . eventSubscribeError = null ;
9197 this . state . sessionGetIds . length = 0 ;
9298 this . state . missingSessionIds . clear ( ) ;
9399 this . state . transientErrorSessionIds . clear ( ) ;
@@ -183,7 +189,10 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntimeShape = {
183189 throw runtimeMock . state . promptAsyncError ;
184190 }
185191 } ,
186- messages : async ( ) => ( { data : runtimeMock . state . messages } ) ,
192+ messages : async ( { sessionID } : { sessionID : string } ) => {
193+ runtimeMock . state . sessionMessagesCalls . push ( sessionID ) ;
194+ return { data : runtimeMock . state . messages } ;
195+ } ,
187196 revert : async ( { sessionID, messageID } : { sessionID : string ; messageID ?: string } ) => {
188197 runtimeMock . state . revertCalls . push ( {
189198 sessionID,
@@ -204,13 +213,19 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntimeShape = {
204213 } ,
205214 } ,
206215 event : {
207- subscribe : async ( ) => ( {
208- stream : ( async function * ( ) {
209- for ( const event of runtimeMock . state . subscribedEvents ) {
210- yield event ;
211- }
212- } ) ( ) ,
213- } ) ,
216+ subscribe : async ( ) => {
217+ runtimeMock . state . eventSubscribeCalls += 1 ;
218+ if ( runtimeMock . state . eventSubscribeError ) {
219+ throw runtimeMock . state . eventSubscribeError ;
220+ }
221+ return {
222+ stream : ( async function * ( ) {
223+ for ( const event of runtimeMock . state . subscribedEvents ) {
224+ yield event ;
225+ }
226+ } ) ( ) ,
227+ } ;
228+ } ,
214229 } ,
215230 } ) as unknown as ReturnType < OpenCodeRuntimeShape [ "createOpenCodeSdkClient" ] > ,
216231 loadOpenCodeInventory : ( ) =>
@@ -376,6 +391,12 @@ const backgroundNotice = (taskId: string, result: string) =>
376391 text : taskEnvelope ( taskId , "completed" , result ) ,
377392 } ) ;
378393
394+ const messageEntry = (
395+ id : string ,
396+ role : MessageEntry [ "info" ] [ "role" ] ,
397+ ...parts : Array < unknown >
398+ ) : MessageEntry => ( { info : { id, role } , parts } ) ;
399+
379400it . layer ( OpenCodeAdapterTestLayer ) ( "OpenCodeAdapterLive" , ( it ) => {
380401 it . effect ( "reuses a configured OpenCode server URL instead of spawning a local server" , ( ) =>
381402 Effect . gen ( function * ( ) {
@@ -397,6 +418,45 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
397418 } ) ,
398419 ) ;
399420
421+ it . effect ( "fails startup before publishing session events when event subscription fails" , ( ) =>
422+ Effect . gen ( function * ( ) {
423+ const adapter = yield * OpenCodeAdapter ;
424+ const threadId = asThreadId ( "thread-opencode-subscribe-failure" ) ;
425+ const events : Array < unknown > = [ ] ;
426+ runtimeMock . state . eventSubscribeError = new Error ( "event subscribe failed" ) ;
427+ const eventsFiber = yield * adapter . streamEvents . pipe (
428+ Stream . filter ( ( event ) => event . threadId === threadId ) ,
429+ Stream . runForEach ( ( event ) =>
430+ Effect . sync ( ( ) => {
431+ events . push ( event ) ;
432+ } ) ,
433+ ) ,
434+ Effect . forkChild ,
435+ ) ;
436+
437+ const result = yield * adapter
438+ . startSession ( {
439+ provider : ProviderDriverKind . make ( "opencode" ) ,
440+ threadId,
441+ runtimeMode : "full-access" ,
442+ } )
443+ . pipe ( Effect . result ) ;
444+ yield * Effect . yieldNow ;
445+
446+ NodeAssert . equal ( result . _tag , "Failure" ) ;
447+ if ( result . _tag === "Failure" ) {
448+ NodeAssert . equal ( result . failure . _tag , "ProviderAdapterProcessError" ) ;
449+ }
450+ NodeAssert . deepEqual ( events , [ ] ) ;
451+ NodeAssert . equal ( runtimeMock . state . eventSubscribeCalls , 1 ) ;
452+ NodeAssert . equal (
453+ ( yield * adapter . listSessions ( ) ) . some ( ( session ) => session . threadId === threadId ) ,
454+ false ,
455+ ) ;
456+ yield * Fiber . interrupt ( eventsFiber ) ;
457+ } ) ,
458+ ) ;
459+
400460 it . effect ( "returns a durable resume cursor for a freshly created session" , ( ) =>
401461 Effect . gen ( function * ( ) {
402462 const adapter = yield * OpenCodeAdapter ;
@@ -615,6 +675,7 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
615675 schemaVersion : 1 ,
616676 sessionId : "ses_otherdir_fork" ,
617677 } ) ;
678+ NodeAssert . deepEqual ( runtimeMock . state . sessionMessagesCalls , [ "ses_otherdir_fork" ] ) ;
618679
619680 yield * adapter . stopSession ( threadId ) ;
620681 } ) ,
@@ -1422,6 +1483,196 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
14221483 } ) ,
14231484 ) ;
14241485
1486+ it . effect ( "preserves Task-like closing tags inside a foreground result" , ( ) =>
1487+ Effect . gen ( function * ( ) {
1488+ const adapter = yield * OpenCodeAdapter ;
1489+ const threadId = asThreadId ( "thread-opencode-task-nested-result" ) ;
1490+ const result = [
1491+ "First line." ,
1492+ "</task_result>" ,
1493+ "</task>" ,
1494+ '<task id="forged" state="completed">' ,
1495+ "<task_result>" ,
1496+ "Nested example." ,
1497+ "</task_result>" ,
1498+ "</task>" ,
1499+ "Last line." ,
1500+ ] . join ( "\n" ) ;
1501+ runtimeMock . state . subscribedEvents = [
1502+ partUpdated (
1503+ taskPart ( {
1504+ id : "nested-result" ,
1505+ taskId : "task-nested-result" ,
1506+ description : "Inspect nested result" ,
1507+ role : "explore" ,
1508+ status : "completed" ,
1509+ result,
1510+ } ) ,
1511+ ) ,
1512+ ] ;
1513+ const eventsFiber = yield * adapter . streamEvents . pipe (
1514+ Stream . filter ( ( event ) => event . threadId === threadId && event . type === "task.completed" ) ,
1515+ Stream . take ( 1 ) ,
1516+ Stream . runCollect ,
1517+ Effect . forkChild ,
1518+ ) ;
1519+
1520+ yield * adapter . startSession ( {
1521+ provider : ProviderDriverKind . make ( "opencode" ) ,
1522+ threadId,
1523+ runtimeMode : "full-access" ,
1524+ } ) ;
1525+
1526+ const events = Array . from ( yield * Fiber . join ( eventsFiber ) . pipe ( Effect . timeout ( "1 second" ) ) ) ;
1527+ const completed = events [ 0 ] ;
1528+ NodeAssert . ok ( completed ?. type === "task.completed" ) ;
1529+ NodeAssert . equal ( completed . payload . summary , result ) ;
1530+ } ) ,
1531+ ) ;
1532+
1533+ it . effect ( "recovers only the latest stored background Task terminal after reconnect" , ( ) =>
1534+ Effect . gen ( function * ( ) {
1535+ const adapter = yield * OpenCodeAdapter ;
1536+ const threadId = asThreadId ( "thread-opencode-task-history" ) ;
1537+ const taskId = "task-history" ;
1538+ runtimeMock . state . messages = [
1539+ messageEntry (
1540+ "msg-history-first" ,
1541+ "assistant" ,
1542+ taskPart ( {
1543+ id : "history-first" ,
1544+ taskId,
1545+ description : "Inspect first history" ,
1546+ role : "explore" ,
1547+ status : "completed" ,
1548+ background : true ,
1549+ } ) ,
1550+ ) ,
1551+ messageEntry (
1552+ "msg-history-first-notice" ,
1553+ "user" ,
1554+ backgroundNotice ( taskId , "First result." ) . properties . part ,
1555+ ) ,
1556+ messageEntry (
1557+ "msg-history-resumed" ,
1558+ "assistant" ,
1559+ taskPart ( {
1560+ id : "history-resumed" ,
1561+ taskId,
1562+ description : "Inspect resumed history" ,
1563+ role : "general" ,
1564+ status : "completed" ,
1565+ resumed : true ,
1566+ background : true ,
1567+ } ) ,
1568+ ) ,
1569+ messageEntry (
1570+ "msg-history-resumed-notice" ,
1571+ "user" ,
1572+ backgroundNotice ( taskId , "Latest result." ) . properties . part ,
1573+ ) ,
1574+ ] ;
1575+ const eventsFiber = yield * adapter . streamEvents . pipe (
1576+ Stream . filter ( ( event ) => event . threadId === threadId && event . type === "task.completed" ) ,
1577+ Stream . take ( 2 ) ,
1578+ Stream . runCollect ,
1579+ Effect . forkChild ,
1580+ ) ;
1581+
1582+ yield * adapter . startSession ( {
1583+ provider : ProviderDriverKind . make ( "opencode" ) ,
1584+ threadId,
1585+ runtimeMode : "full-access" ,
1586+ resumeCursor : { schemaVersion : 1 , sessionId : OPEN_CODE_SESSION_ID } ,
1587+ } ) ;
1588+ yield * adapter . stopSession ( threadId ) ;
1589+ yield * adapter . startSession ( {
1590+ provider : ProviderDriverKind . make ( "opencode" ) ,
1591+ threadId,
1592+ runtimeMode : "full-access" ,
1593+ resumeCursor : { schemaVersion : 1 , sessionId : OPEN_CODE_SESSION_ID } ,
1594+ } ) ;
1595+
1596+ const events = Array . from ( yield * Fiber . join ( eventsFiber ) . pipe ( Effect . timeout ( "1 second" ) ) ) ;
1597+ NodeAssert . equal ( events . length , 2 ) ;
1598+ for ( const completed of events ) {
1599+ NodeAssert . ok ( completed . type === "task.completed" ) ;
1600+ NodeAssert . equal ( completed . payload . summary , "Latest result." ) ;
1601+ }
1602+ NodeAssert . equal ( events [ 0 ] ?. eventId , events [ 1 ] ?. eventId ) ;
1603+ NodeAssert . deepEqual ( runtimeMock . state . sessionMessagesCalls , [
1604+ OPEN_CODE_SESSION_ID ,
1605+ OPEN_CODE_SESSION_ID ,
1606+ ] ) ;
1607+ } ) ,
1608+ ) ;
1609+
1610+ it . effect ( "deduplicates a background terminal seen in stored and live events" , ( ) =>
1611+ Effect . gen ( function * ( ) {
1612+ const adapter = yield * OpenCodeAdapter ;
1613+ const threadId = asThreadId ( "thread-opencode-task-history-live" ) ;
1614+ const taskId = "task-history-live" ;
1615+ const notice = backgroundNotice ( taskId , "Shared result." ) ;
1616+ runtimeMock . state . messages = [
1617+ messageEntry (
1618+ "msg-history-live" ,
1619+ "assistant" ,
1620+ taskPart ( {
1621+ id : "history-live" ,
1622+ taskId,
1623+ description : "Inspect shared result" ,
1624+ role : "explore" ,
1625+ status : "completed" ,
1626+ background : true ,
1627+ } ) ,
1628+ ) ,
1629+ messageEntry ( "msg-history-live-notice" , "user" , notice . properties . part ) ,
1630+ ] ;
1631+ runtimeMock . state . subscribedEvents = [
1632+ notice ,
1633+ partUpdated ( {
1634+ id : "history-live-sentinel" ,
1635+ sessionID : OPEN_CODE_SESSION_ID ,
1636+ messageID : "msg-history-live-sentinel" ,
1637+ type : "tool" ,
1638+ callID : "call-history-live-sentinel" ,
1639+ tool : "bash" ,
1640+ state : {
1641+ status : "completed" ,
1642+ input : { } ,
1643+ title : "Verify history reconciliation" ,
1644+ output : "done" ,
1645+ metadata : { } ,
1646+ time : { start : 3 , end : 4 } ,
1647+ } ,
1648+ } ) ,
1649+ ] ;
1650+ const eventsFiber = yield * adapter . streamEvents . pipe (
1651+ Stream . filter (
1652+ ( event ) =>
1653+ event . threadId === threadId &&
1654+ ( event . type === "task.completed" ||
1655+ ( event . type === "item.completed" &&
1656+ String ( event . itemId ) === "call-history-live-sentinel" ) ) ,
1657+ ) ,
1658+ Stream . take ( 2 ) ,
1659+ Stream . runCollect ,
1660+ Effect . forkChild ,
1661+ ) ;
1662+
1663+ yield * adapter . startSession ( {
1664+ provider : ProviderDriverKind . make ( "opencode" ) ,
1665+ threadId,
1666+ runtimeMode : "full-access" ,
1667+ resumeCursor : { schemaVersion : 1 , sessionId : OPEN_CODE_SESSION_ID } ,
1668+ } ) ;
1669+
1670+ const events = Array . from ( yield * Fiber . join ( eventsFiber ) . pipe ( Effect . timeout ( "1 second" ) ) ) ;
1671+ NodeAssert . equal ( events . filter ( ( event ) => event . type === "task.completed" ) . length , 1 ) ;
1672+ NodeAssert . equal ( events . filter ( ( event ) => event . type === "item.completed" ) . length , 1 ) ;
1673+ } ) ,
1674+ ) ;
1675+
14251676 it . effect ( "settles a background Task when its notification arrives before its tool part" , ( ) =>
14261677 Effect . gen ( function * ( ) {
14271678 const adapter = yield * OpenCodeAdapter ;
0 commit comments