@@ -48,6 +48,10 @@ import * as ProviderContinuationRequests from "../ProviderContinuationRequests.t
4848import { OPENCODE_PROVIDER } from "./OpenCodeAdapterV2.ts" ;
4949import { OPENCODE_2_STILL_STOPPING } from "./OpenCode2AdapterV2.ts" ;
5050import { openCode2ReplayRuntime } from "./OpenCode2AdapterV2.testkit.ts" ;
51+ import {
52+ makeProviderReplayGate ,
53+ type ProviderReplayGate ,
54+ } from "../testkit/ProviderReplayGate.testkit.ts" ;
5155
5256const SESSION = "ses_f148ca2deffeJcwCnRQtb0YFNX" ;
5357const WORK = "/work/opencode2" ;
@@ -64,12 +68,18 @@ const reply = (operation: string, data: unknown): ProviderReplayEntry => ({
6468 frame : { type : "sdk.response" , operation, data } ,
6569} ) ;
6670const replyData = ( operation : string , data : unknown ) => reply ( operation , { data } ) ;
67- const event = ( type : string , data : Record < string , unknown > ) : ProviderReplayEntry => ( {
71+ const event = (
72+ type : string ,
73+ data : Record < string , unknown > ,
74+ /** Holds the event until a test's replay gate releases this label. */
75+ label ?: string ,
76+ ) : ProviderReplayEntry => ( {
6877 type : "emit_inbound" ,
6978 frame : {
7079 type : "sdk.event" ,
7180 event : { id : `evt_${ type . replaceAll ( "." , "" ) } 0000` , created : 1 , type, data, ...durable } ,
7281 } ,
82+ ...( label === undefined ? { } : { label } ) ,
7383} ) ;
7484const durable = { durable : { aggregateID : SESSION , seq : 1 , version : 1 } } ;
7585
@@ -313,7 +323,11 @@ const colorForm = {
313323 */
314324const resumed = (
315325 entries : ReadonlyArray < ProviderReplayEntry > ,
316- options ?: { readonly external ?: boolean ; readonly supervised ?: boolean } ,
326+ options ?: {
327+ readonly external ?: boolean ;
328+ readonly supervised ?: boolean ;
329+ readonly replayGate ?: ProviderReplayGate ;
330+ } ,
317331) =>
318332 Effect . gen ( function * ( ) {
319333 const runtime = yield * openCode2ReplayRuntime (
@@ -332,7 +346,10 @@ const resumed = (
332346 : [ ] ) ,
333347 ...entries ,
334348 ] ) ,
335- options ?. external === undefined ? undefined : { external : options . external } ,
349+ {
350+ ...( options ?. external === undefined ? { } : { external : options . external } ) ,
351+ ...( options ?. replayGate === undefined ? { } : { replayGate : options . replayGate } ) ,
352+ } ,
336353 ) ;
337354 const thread = yield * runtime . resumeThread ( {
338355 providerThread : providerThread ( yield * DateTime . now ) ,
@@ -1469,6 +1486,123 @@ describe("OpenCode2 adapter", () => {
14691486 } ) . pipe ( Effect . scoped , Effect . provide ( TestClock . layer ( ) ) ) ,
14701487 ) ;
14711488
1489+ it . effect ( "ends a background reply's continuation after a lost stream took a Stop's end" , ( ) =>
1490+ Effect . gen ( function * ( ) {
1491+ const offered = yield * Deferred . make < void > ( ) ;
1492+ const gate = makeProviderReplayGate ( [ "drop" , "reply" ] ) ;
1493+ const { runtime, thread } = yield * resumed (
1494+ [
1495+ ...backgroundLaunch ( CHILD ) ,
1496+ // Restarting the turn stops only the parent, and the server never answers.
1497+ out ( "session.interrupt" , { sessionID : SESSION } ) ,
1498+ reply ( "session.interrupt" , "<hang>" ) ,
1499+ // The restarted turn finds the parent still stopping and fails.
1500+ out ( "session.active" ) ,
1501+ replyData ( "session.active" , { [ SESSION ] : { type : "running" } } ) ,
1502+ out ( "session.interrupt" , { sessionID : SESSION } ) ,
1503+ reply ( "session.interrupt" , { interrupted : true } ) ,
1504+ // The stream drops before the parent's end; the reconnect finds it idle.
1505+ event ( "session.usage.updated" , { sessionID : SESSION } , "drop" ) ,
1506+ { type : "runtime_exit" , status : "success" } as const ,
1507+ out ( "event.subscribe" ) ,
1508+ out ( "session.active" ) ,
1509+ replyData ( "session.active" , { [ CHILD ] : { type : "running" } } ) ,
1510+ out ( "permission.list" , { sessionID : CHILD } ) ,
1511+ replyData ( "permission.list" , [ ] ) ,
1512+ out ( "session.form.list" , { sessionID : CHILD } ) ,
1513+ replyData ( "session.form.list" , [ ] ) ,
1514+ // The subagent ends and OpenCode answers its report on its own.
1515+ event ( "session.execution.succeeded" , { sessionID : CHILD } ) ,
1516+ event ( "session.inbox.enqueued" , {
1517+ inboxID : "msg_report" ,
1518+ sessionID : SESSION ,
1519+ item : {
1520+ type : "synthetic" ,
1521+ payload : {
1522+ text : `<subagent sessionID="${ CHILD } " state="completed" description="Sleep">\nCHILD_OK\n</subagent>` ,
1523+ description : "Sleep" ,
1524+ metadata : {
1525+ source : "subagent" ,
1526+ childID : CHILD ,
1527+ agent : "General" ,
1528+ state : "completed" ,
1529+ } ,
1530+ } ,
1531+ delivery : "steer" ,
1532+ } ,
1533+ } ) ,
1534+ event ( "session.execution.started" , { sessionID : SESSION } ) ,
1535+ event (
1536+ "session.text.ended" ,
1537+ {
1538+ sessionID : SESSION ,
1539+ assistantMessageID : "msg_followup" ,
1540+ ordinal : 0 ,
1541+ text : "CHILD_OK" ,
1542+ } ,
1543+ "reply" ,
1544+ ) ,
1545+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
1546+ // The subagent's session runs again: its turn marks that the reply's end was handled.
1547+ event ( "session.execution.started" , { sessionID : CHILD } ) ,
1548+ ] ,
1549+ { replayGate : gate } ,
1550+ ) . pipe (
1551+ Effect . provideService ( ProviderContinuationRequests . ProviderContinuationRequests , {
1552+ offer : ( ) => Deferred . succeed ( offered , undefined ) . pipe ( Effect . asVoid ) ,
1553+ take : Effect . never ,
1554+ } ) ,
1555+ ) ;
1556+ // The single reader of the runtime's events.
1557+ const attached = yield * Deferred . make < void > ( ) ;
1558+ const collected = yield * runtime . events . pipe (
1559+ Stream . tap ( ( event ) =>
1560+ event . type === "subagent.updated" && event . subagent . childThreadId !== null
1561+ ? Deferred . succeed ( attached , undefined )
1562+ : Effect . void ,
1563+ ) ,
1564+ Stream . takeUntil (
1565+ ( event ) =>
1566+ event . type === "provider_turn.updated" &&
1567+ event . providerTurn . nativeTurnRef ?. nativeId ?. startsWith ( `${ CHILD } :turn:` ) === true ,
1568+ ) ,
1569+ Stream . runCollect ,
1570+ Effect . forkScoped ,
1571+ ) ;
1572+ yield * runtime . startTurn ( withLineage ( thread ) ) ;
1573+ yield * Deferred . await ( attached ) ;
1574+ const interrupt = yield * runtime
1575+ . interruptTurn ( { providerThread : thread , providerTurnId : yield * providerTurnId } )
1576+ . pipe ( Effect . forkScoped ) ;
1577+ yield * TestClock . adjust ( "11 seconds" ) ;
1578+ yield * Fiber . join ( interrupt ) ;
1579+ yield * runtime . startTurn ( { ...secondTurn ( thread ) , appThread : withLineage ( thread ) . appThread } ) ;
1580+ gate . release ( "drop" ) ;
1581+ yield * Deferred . await ( offered ) ;
1582+ yield * runtime . startTurn ( {
1583+ ...withLineage ( thread ) ,
1584+ runId : RunId . make ( "run:opencode2-adapter:wake" ) ,
1585+ runOrdinal : 3 ,
1586+ providerTurnOrdinal : 3 ,
1587+ attemptId : RunAttemptId . make ( "attempt:opencode2-adapter:wake" ) ,
1588+ message : {
1589+ ...turnInput ( thread ) . message ,
1590+ messageId : MessageId . make ( "message:opencode2-adapter:wake" ) ,
1591+ createdBy : "agent" as const ,
1592+ creationSource : "provider" as const ,
1593+ } ,
1594+ } ) ;
1595+ gate . release ( "reply" ) ;
1596+ // The reply's own end finishes its continuation: the lost Stop's end does not take it.
1597+ assert . deepEqual (
1598+ [ ...( yield * Fiber . join ( collected ) ) ] . flatMap ( ( event ) =>
1599+ event . type === "turn.terminal" ? [ event . status ] : [ ] ,
1600+ ) ,
1601+ [ "interrupted" , "failed" , "completed" ] ,
1602+ ) ;
1603+ } ) . pipe ( Effect . scoped , Effect . provide ( TestClock . layer ( ) ) ) ,
1604+ ) ;
1605+
14721606 it . effect ( "does not report a Stop the server says did nothing" , ( ) =>
14731607 Effect . gen ( function * ( ) {
14741608 const { runtime, thread } = yield * resumed ( [
0 commit comments