@@ -20,7 +20,6 @@ import * as Cause from "effect/Cause";
2020import * as DateTime from "effect/DateTime" ;
2121import * as Duration from "effect/Duration" ;
2222import * as Effect from "effect/Effect" ;
23- import * as Exit from "effect/Exit" ;
2423import * as Fiber from "effect/Fiber" ;
2524import * as FileSystem from "effect/FileSystem" ;
2625import * as Layer from "effect/Layer" ;
@@ -89,10 +88,7 @@ interface FakePi {
8988 readonly resolveDeferredState : ( data : unknown ) => Effect . Effect < void > ;
9089 /** Reject the next `get_state` request. */
9190 readonly failNextState : ( ) => void ;
92- /** Leave the next request of this type unanswered. */
93- readonly deferNextLifecycle : (
94- type : "switch_session" | "new_session" | "set_session_name" ,
95- ) => void ;
91+ readonly deferNextLifecycle : ( type : "switch_session" | "new_session" ) => void ;
9692 readonly queueModels : ( models : ReadonlyArray < unknown > ) => void ;
9793 readonly vetoNextNewSession : ( ) => void ;
9894 /** Every request received by the fake process. */
@@ -1187,186 +1183,6 @@ describe("PiAdapterV2", () => {
11871183 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
11881184 ) ;
11891185
1190- it . effect ( "leaves no turn behind when a start is interrupted before its prompt" , ( ) =>
1191- Effect . gen ( function * ( ) {
1192- const fake = yield * makeFakePi ;
1193- const { runtime, takeEvent } = yield * openRuntime ( fake ) ;
1194- const providerThread = yield * runtime . ensureThread ( {
1195- threadId : THREAD_ID ,
1196- modelSelection : modelSelection ( "default" ) ,
1197- runtimePolicy,
1198- } ) ;
1199- // The start waits on naming the session before it sends the prompt.
1200- fake . deferNextLifecycle ( "set_session_name" ) ;
1201- const starting = yield * startTurn ( runtime , providerThread ) . pipe ( Effect . forkChild ) ;
1202- yield * fake . takeRequest ( "set_session_name" ) ;
1203- const interrupted = yield * Fiber . interrupt ( starting ) . pipe (
1204- Effect . andThen ( Fiber . await ( starting ) ) ,
1205- ) ;
1206- assert . isTrue ( Exit . isFailure ( interrupted ) && Cause . hasInterruptsOnly ( interrupted . cause ) ) ;
1207-
1208- // Nothing is left installed, so the next turn starts and completes.
1209- yield * startTurn ( runtime , providerThread , "default" , [ ] , "Second turn" , undefined , 2 ) ;
1210- yield * fake . takeRequest ( "prompt" ) ;
1211- yield * fake . emit ( { type : "agent_start" } ) ;
1212- yield * fake . emit ( { type : "agent_settled" } ) ;
1213- const terminal = yield * takeEvent ( ( event ) => event . type === "turn.terminal" ) ;
1214- assert . isTrue ( terminal . type === "turn.terminal" && terminal . status === "completed" ) ;
1215- // Only the second turn's prompt ever reached Pi.
1216- assert . deepStrictEqual (
1217- fake
1218- . allRequests ( )
1219- . filter ( ( request ) => request [ "type" ] === "prompt" )
1220- . map ( ( request ) => request [ "message" ] ) ,
1221- [ "Second turn" ] ,
1222- ) ;
1223- } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1224- ) ;
1225-
1226- it . effect ( "keeps a turn whose start is interrupted after its prompt is sent" , ( ) =>
1227- Effect . gen ( function * ( ) {
1228- const fake = yield * makeFakePi ;
1229- const { runtime, takeEvent } = yield * openRuntime ( fake ) ;
1230- const providerThread = yield * runtime . ensureThread ( {
1231- threadId : THREAD_ID ,
1232- modelSelection : modelSelection ( "default" ) ,
1233- runtimePolicy,
1234- } ) ;
1235- const appThread = yield * makeAppThread ( "default" ) ;
1236- const runId = RunId . make ( `run:${ THREAD_ID } :1` ) ;
1237- // The install block first reads `runOrdinal` after it sent the prompt.
1238- // Interrupting the starting fiber there is a Stop that lands once Pi is
1239- // already running the prompt, with no wait to race. Later reads (the
1240- // event pump settling the turn) are left alone.
1241- let interruptOnRead = true ;
1242- const turnInput = {
1243- appThread,
1244- threadId : THREAD_ID ,
1245- runId,
1246- get runOrdinal ( ) {
1247- if ( interruptOnRead ) {
1248- interruptOnRead = false ;
1249- Fiber . getCurrent ( ) ?. interruptUnsafe ( ) ;
1250- }
1251- return 1 ;
1252- } ,
1253- providerTurnOrdinal : 1 ,
1254- attemptId : RunAttemptId . make ( `run-attempt:${ runId } :1` ) ,
1255- rootNodeId : NodeId . make ( `node:${ runId } :root` ) ,
1256- providerThread,
1257- message : {
1258- messageId : `message:${ THREAD_ID } :1` as never ,
1259- text : "Hello pi" ,
1260- attachments : [ ] ,
1261- createdBy : "user" as const ,
1262- creationSource : "web" as const ,
1263- } ,
1264- modelSelection : modelSelection ( "default" ) ,
1265- runtimePolicy,
1266- } ;
1267- const starting = yield * runtime . startTurn ( turnInput ) . pipe ( Effect . forkChild ) ;
1268- const interrupted = yield * Fiber . await ( starting ) ;
1269- assert . isTrue ( Exit . isFailure ( interrupted ) && Cause . hasInterruptsOnly ( interrupted . cause ) ) ;
1270- yield * fake . takeRequest ( "prompt" ) ;
1271- const running = yield * takeEvent (
1272- ( event ) =>
1273- event . type === "provider_turn.updated" && event . providerTurn . status === "running" ,
1274- ) ;
1275- const providerTurnId =
1276- running . type === "provider_turn.updated" ? running . providerTurn . id : undefined ;
1277- assert . isDefined ( providerTurnId ) ;
1278-
1279- // Pi is running that prompt, so the turn still owns the thread.
1280- const rejected = yield * startTurn (
1281- runtime ,
1282- providerThread ,
1283- "default" ,
1284- [ ] ,
1285- "Second turn" ,
1286- undefined ,
1287- 2 ,
1288- ) . pipe ( Effect . flip ) ;
1289- assert . include ( String ( rejected . cause ) , "already has an active turn" ) ;
1290-
1291- // Its events stay its own, and Stop settles it as interrupted.
1292- yield * fake . emit ( { type : "agent_start" } ) ;
1293- yield * runtime . interruptTurn ( { providerThread, providerTurnId : providerTurnId ! } ) ;
1294- yield * fake . takeRequest ( "abort" ) ;
1295- yield * fake . emit ( { type : "agent_settled" } ) ;
1296- const terminal = yield * takeEvent ( ( event ) => event . type === "turn.terminal" ) ;
1297- assert . isTrue ( terminal . type === "turn.terminal" && terminal . status === "interrupted" ) ;
1298- } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1299- ) ;
1300-
1301- it . effect ( "fails a kept turn when Pi rejects the prompt it was interrupted after sending" , ( ) =>
1302- Effect . gen ( function * ( ) {
1303- const fake = yield * makeFakePi ;
1304- const { runtime, takeEvent } = yield * openRuntime ( fake ) ;
1305- const providerThread = yield * runtime . ensureThread ( {
1306- threadId : THREAD_ID ,
1307- modelSelection : modelSelection ( "default" ) ,
1308- runtimePolicy,
1309- } ) ;
1310- const appThread = yield * makeAppThread ( "default" ) ;
1311- const runId = RunId . make ( `run:${ THREAD_ID } :1` ) ;
1312- // The install block first reads `runOrdinal` after it sent the prompt.
1313- // Interrupting the starting fiber there is a Stop that lands once Pi is
1314- // already running the prompt, with no wait to race. Later reads (the
1315- // event pump settling the turn) are left alone.
1316- let interruptOnRead = true ;
1317- const turnInput = {
1318- appThread,
1319- threadId : THREAD_ID ,
1320- runId,
1321- get runOrdinal ( ) {
1322- if ( interruptOnRead ) {
1323- interruptOnRead = false ;
1324- Fiber . getCurrent ( ) ?. interruptUnsafe ( ) ;
1325- }
1326- return 1 ;
1327- } ,
1328- providerTurnOrdinal : 1 ,
1329- attemptId : RunAttemptId . make ( `run-attempt:${ runId } :1` ) ,
1330- rootNodeId : NodeId . make ( `node:${ runId } :root` ) ,
1331- providerThread,
1332- message : {
1333- messageId : `message:${ THREAD_ID } :1` as never ,
1334- text : "Hello pi" ,
1335- attachments : [ ] ,
1336- createdBy : "user" as const ,
1337- creationSource : "web" as const ,
1338- } ,
1339- modelSelection : modelSelection ( "default" ) ,
1340- runtimePolicy,
1341- } ;
1342- const starting = yield * runtime . startTurn ( turnInput ) . pipe ( Effect . forkChild ) ;
1343- const interrupted = yield * Fiber . await ( starting ) ;
1344- assert . isTrue ( Exit . isFailure ( interrupted ) && Cause . hasInterruptsOnly ( interrupted . cause ) ) ;
1345- yield * fake . takeRequest ( "prompt" ) ;
1346- const running = yield * takeEvent (
1347- ( event ) =>
1348- event . type === "provider_turn.updated" && event . providerTurn . status === "running" ,
1349- ) ;
1350- const providerTurnId =
1351- running . type === "provider_turn.updated" ? running . providerTurn . id : undefined ;
1352- assert . isDefined ( providerTurnId ) ;
1353-
1354- // The kept turn still has the pending response that settles or fails it.
1355- yield * fake . emit ( {
1356- type : "response" ,
1357- command : "prompt" ,
1358- success : false ,
1359- error : "Prompt rejected" ,
1360- } ) ;
1361- const terminal = yield * takeEvent ( ( event ) => event . type === "turn.terminal" ) ;
1362- assert . isTrue (
1363- terminal . type === "turn.terminal" &&
1364- terminal . status === "failed" &&
1365- terminal . providerTurnId === providerTurnId ,
1366- ) ;
1367- } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1368- ) ;
1369-
13701186 it . effect ( "leaves /compacted as an ordinary prompt" , ( ) =>
13711187 Effect . gen ( function * ( ) {
13721188 const fake = yield * makeFakePi ;
0 commit comments