@@ -17,6 +17,7 @@ import {
1717 type OrchestrationV2ProviderTurn ,
1818} from "@t3tools/contracts" ;
1919import * as Effect from "effect/Effect" ;
20+ import * as Clock from "effect/Clock" ;
2021import * as DateTime from "effect/DateTime" ;
2122import * as Deferred from "effect/Deferred" ;
2223import * as Fiber from "effect/Fiber" ;
@@ -1367,6 +1368,167 @@ describe("OpenCodeAdapterV2", () => {
13671368 } ) . pipe ( Effect . provide ( idAllocatorLayer ) , Effect . scoped ) ,
13681369 ) ;
13691370
1371+ it . effect ( "fails an active turn when the OpenCode event stream ends cleanly" , ( ) =>
1372+ Effect . gen ( function * ( ) {
1373+ const nativeEvents = asyncEventStream ( ) ;
1374+ const harness = yield * makeOpenCodeRuntimeHarness (
1375+ "clean-event-eof" ,
1376+ "native-opencode-clean-event-eof" ,
1377+ {
1378+ event : {
1379+ subscribe : async ( _input : unknown , options : { signal ?: AbortSignal } ) => {
1380+ options . signal ?. addEventListener ( "abort" , ( ) => nativeEvents . close ( ) , { once : true } ) ;
1381+ return { stream : nativeEvents . stream } ;
1382+ } ,
1383+ } ,
1384+ session : {
1385+ create : async ( ) => ( {
1386+ data : { id : "native-opencode-clean-event-eof" , time : { created : 1 , updated : 1 } } ,
1387+ } ) ,
1388+ promptAsync : async ( ) => ( { data : true } ) ,
1389+ } ,
1390+ } ,
1391+ ) ;
1392+ yield * harness . startTurn ( ) ;
1393+ const terminalEvents = yield * harness . runtime . events . pipe (
1394+ Stream . runCollect ,
1395+ Effect . forkScoped ,
1396+ ) ;
1397+
1398+ nativeEvents . close ( ) ;
1399+ const received = Array . from ( yield * Fiber . join ( terminalEvents ) ) ;
1400+ assert . isTrue (
1401+ received . some (
1402+ ( event ) =>
1403+ event . type === "provider_session.updated" && event . providerSession . status === "error" ,
1404+ ) ,
1405+ ) ;
1406+ const terminal = received . find ( ( event ) => event . type === "turn.terminal" ) ;
1407+ assert . equal ( terminal ?. status , "failed" ) ;
1408+ assert . equal ( terminal ?. failure ?. class , "transport_error" ) ;
1409+ assert . equal ( ( yield * Effect . exit ( harness . startTurn ( ) ) ) . _tag , "Failure" ) ;
1410+ } ) . pipe ( Effect . provide ( idAllocatorLayer ) , Effect . scoped ) ,
1411+ ) ;
1412+
1413+ it . effect ( "fails compaction when its response races stream termination" , ( ) =>
1414+ Effect . gen ( function * ( ) {
1415+ const nativeEvents = asyncEventStream ( ) ;
1416+ const summarizeStarted = promiseGate < void > ( ) ;
1417+ const summarizeResult = promiseGate < { data : boolean } > ( ) ;
1418+ const baseClock = yield * Clock . Clock ;
1419+ const eofClockRead = yield * Deferred . make < void > ( ) ;
1420+ const releaseEofClockRead = yield * Deferred . make < void > ( ) ;
1421+ let blockNextClockRead = false ;
1422+ const blockingClock : Clock . Clock = {
1423+ ...baseClock ,
1424+ currentTimeMillis : Effect . suspend ( ( ) => {
1425+ if ( ! blockNextClockRead ) return baseClock . currentTimeMillis ;
1426+ blockNextClockRead = false ;
1427+ return Deferred . succeed ( eofClockRead , undefined ) . pipe (
1428+ Effect . andThen ( Deferred . await ( releaseEofClockRead ) ) ,
1429+ Effect . andThen ( baseClock . currentTimeMillis ) ,
1430+ ) ;
1431+ } ) ,
1432+ } ;
1433+ const harness = yield * makeOpenCodeRuntimeHarness (
1434+ "compaction-eof-race" ,
1435+ "native-opencode-compaction-eof-race" ,
1436+ {
1437+ event : { subscribe : async ( ) => ( { stream : nativeEvents . stream } ) } ,
1438+ session : {
1439+ create : async ( ) => ( {
1440+ data : { id : "native-opencode-compaction-eof-race" , time : { created : 1 , updated : 1 } } ,
1441+ } ) ,
1442+ summarize : ( ) => {
1443+ summarizeStarted . resolve ( ) ;
1444+ return summarizeResult . promise ;
1445+ } ,
1446+ } ,
1447+ } ,
1448+ ) . pipe ( Effect . provideService ( Clock . Clock , blockingClock ) ) ;
1449+ const events = yield * harness . runtime . events . pipe ( Stream . runCollect , Effect . forkScoped ) ;
1450+ const start = yield * harness . startTurn ( "/compact" ) . pipe ( Effect . forkScoped ) ;
1451+ yield * Effect . promise ( ( ) => summarizeStarted . promise ) ;
1452+ blockNextClockRead = true ;
1453+ nativeEvents . close ( ) ;
1454+ yield * Deferred . await ( eofClockRead ) ;
1455+ summarizeResult . resolve ( { data : true } ) ;
1456+ yield * Fiber . join ( start ) ;
1457+ yield * Deferred . succeed ( releaseEofClockRead , undefined ) ;
1458+ const received = Array . from ( yield * Fiber . join ( events ) ) ;
1459+ const terminals = received . filter ( ( event ) => event . type === "turn.terminal" ) ;
1460+ assert . lengthOf ( terminals , 1 ) ;
1461+ assert . equal ( terminals [ 0 ] ?. status , "failed" ) ;
1462+ assert . equal ( terminals [ 0 ] ?. failure ?. class , "transport_error" ) ;
1463+ assert . isFalse (
1464+ received . some (
1465+ ( event ) =>
1466+ event . type === "turn_item.updated" &&
1467+ event . turnItem . type === "compaction" &&
1468+ event . turnItem . status === "completed" ,
1469+ ) ,
1470+ ) ;
1471+ } ) . pipe ( Effect . provide ( idAllocatorLayer ) , Effect . scoped ) ,
1472+ ) ;
1473+
1474+ it . effect ( "does not register a turn after the OpenCode event stream ends" , ( ) =>
1475+ Effect . gen ( function * ( ) {
1476+ const nativeEvents = asyncEventStream ( ) ;
1477+ let promptCalls = 0 ;
1478+ const harness = yield * makeOpenCodeRuntimeHarness (
1479+ "event-eof-start-race" ,
1480+ "native-opencode-event-eof-start-race" ,
1481+ {
1482+ event : {
1483+ subscribe : async ( _input : unknown , options : { signal ?: AbortSignal } ) => {
1484+ options . signal ?. addEventListener ( "abort" , ( ) => nativeEvents . close ( ) , { once : true } ) ;
1485+ return { stream : nativeEvents . stream } ;
1486+ } ,
1487+ } ,
1488+ session : {
1489+ create : async ( ) => ( {
1490+ data : {
1491+ id : "native-opencode-event-eof-start-race" ,
1492+ time : { created : 1 , updated : 1 } ,
1493+ } ,
1494+ } ) ,
1495+ promptAsync : async ( ) => {
1496+ promptCalls += 1 ;
1497+ return { data : true } ;
1498+ } ,
1499+ } ,
1500+ } ,
1501+ ) ;
1502+ const baseClock = yield * Clock . Clock ;
1503+ const startClockRead = yield * Deferred . make < void > ( ) ;
1504+ const releaseStartClockRead = yield * Deferred . make < void > ( ) ;
1505+ let blockNextClockRead = true ;
1506+ const blockingClock : Clock . Clock = {
1507+ ...baseClock ,
1508+ currentTimeMillis : Effect . suspend ( ( ) => {
1509+ if ( ! blockNextClockRead ) return baseClock . currentTimeMillis ;
1510+ blockNextClockRead = false ;
1511+ return Deferred . succeed ( startClockRead , undefined ) . pipe (
1512+ Effect . andThen ( Deferred . await ( releaseStartClockRead ) ) ,
1513+ Effect . andThen ( baseClock . currentTimeMillis ) ,
1514+ ) ;
1515+ } ) ,
1516+ } ;
1517+ const start = yield * harness
1518+ . startTurn ( )
1519+ . pipe ( Effect . provideService ( Clock . Clock , blockingClock ) , Effect . exit , Effect . forkScoped ) ;
1520+ yield * Deferred . await ( startClockRead ) ;
1521+
1522+ const events = yield * harness . runtime . events . pipe ( Stream . runCollect , Effect . forkScoped ) ;
1523+ nativeEvents . close ( ) ;
1524+ yield * Fiber . join ( events ) ;
1525+ yield * Deferred . succeed ( releaseStartClockRead , undefined ) ;
1526+
1527+ assert . isTrue ( Exit . isFailure ( yield * Fiber . join ( start ) ) ) ;
1528+ assert . equal ( promptCalls , 0 ) ;
1529+ } ) . pipe ( Effect . provide ( idAllocatorLayer ) , Effect . scoped ) ,
1530+ ) ;
1531+
13701532 it ( "holds stale idle through prompt admission until the new user message is observed" , ( ) => {
13711533 const admission = {
13721534 admissionPending : true ,
0 commit comments