88 MessageId ,
99 NodeId ,
1010 type OrchestrationV2Run ,
11+ type OrchestratorMcpThreadReadInput ,
1112 ProjectId ,
1213 ProviderDriverKind ,
1314 ProviderInstanceId ,
@@ -30,6 +31,7 @@ import * as Ref from "effect/Ref";
3031import * as Schedule from "effect/Schedule" ;
3132import * as Stream from "effect/Stream" ;
3233import * as SubscriptionRef from "effect/SubscriptionRef" ;
34+ import * as FetchHttpClient from "effect/http/FetchHttpClient" ;
3335import type * as SqlClient from "effect/sql/SqlClient" ;
3436import { McpSchema , McpServer } from "effect/ai" ;
3537
@@ -82,7 +84,17 @@ type LaunchedWith = Pick<
8284> ;
8385
8486/** The thread the box launches for the task, which the test finishes. */
87+ interface TimelineEntry {
88+ readonly id : TurnItemId ;
89+ readonly runId : RunId ;
90+ readonly text : string ;
91+ readonly type : "assistant_message" | "error" ;
92+ }
93+
8594interface BoxThread {
95+ readonly timeline : Ref . Ref < ReadonlyArray < TimelineEntry > | null > ;
96+ readonly pageSize : Ref . Ref < number > ;
97+ readonly holdRefill : Ref . Ref < boolean > ;
8698 readonly launched : Ref . Ref < ThreadId | null > ;
8799 /** The link session that launched it, as the box stamps it. */
88100 readonly linkOrigin : Ref . Ref < { readonly sessionId : string ; readonly label : string } | null > ;
@@ -131,6 +143,9 @@ interface BoxThread {
131143
132144const makeBoxThread = Effect . gen ( function * ( ) {
133145 return {
146+ timeline : yield * Ref . make < ReadonlyArray < TimelineEntry > | null > ( null ) ,
147+ pageSize : yield * Ref . make ( 1 ) ,
148+ holdRefill : yield * Ref . make ( false ) ,
134149 launched : yield * Ref . make < ThreadId | null > ( null ) ,
135150 linkOrigin : yield * Ref . make < { readonly sessionId : string ; readonly label : string } | null > (
136151 null ,
@@ -300,7 +315,10 @@ const boxToolkitLayer = (thread: BoxThread) => {
300315 } ) ,
301316 getTimelinePage : ( threadId , options ) =>
302317 Effect . gen ( function * ( ) {
303- if ( yield * Ref . get ( thread . holdRead ) ) {
318+ if (
319+ ( yield * Ref . get ( thread . holdRead ) ) ||
320+ ( options . itemId !== undefined && ( yield * Ref . get ( thread . holdRefill ) ) )
321+ ) {
304322 yield * Deferred . succeed ( thread . readStarted , undefined ) ;
305323 yield * Deferred . await ( thread . releaseRead ) ;
306324 }
@@ -309,41 +327,43 @@ const boxToolkitLayer = (thread: BoxThread) => {
309327 const error = yield * Ref . get ( thread . error ) ;
310328 const laterError = yield * Ref . get ( thread . laterError ) ;
311329 // One item per page, with errors available only in the activity view.
312- const replies = [
313- ...( reply === null
314- ? [ ]
315- : [
316- {
317- id : TurnItemId . make ( "item:reply" ) ,
318- runId,
319- text : reply ,
320- type : "assistant_message" ,
321- } ,
322- ] ) ,
323- ...( error === null
324- ? [ ]
325- : [ { id : TurnItemId . make ( "item:error" ) , runId, text : error , type : "error" } ] ) ,
326- ...( later === null
327- ? [ ]
328- : [
329- {
330- id : TurnItemId . make ( "item:later-reply" ) ,
331- runId : boxLaterRunId ,
332- text : later ,
333- type : "assistant_message" ,
334- } ,
335- ] ) ,
336- ...( laterError === null
337- ? [ ]
338- : [
339- {
340- id : TurnItemId . make ( "item:later-error" ) ,
341- runId : boxLaterRunId ,
342- text : laterError ,
343- type : "error" ,
344- } ,
345- ] ) ,
346- ] . map ( ( entry , position ) => ( { ...entry , position } ) ) ;
330+ const replies = (
331+ ( yield * Ref . get ( thread . timeline ) ) ?? [
332+ ...( reply === null
333+ ? [ ]
334+ : [
335+ {
336+ id : TurnItemId . make ( "item:reply" ) ,
337+ runId,
338+ text : reply ,
339+ type : "assistant_message" ,
340+ } ,
341+ ] ) ,
342+ ...( error === null
343+ ? [ ]
344+ : [ { id : TurnItemId . make ( "item:error" ) , runId, text : error , type : "error" } ] ) ,
345+ ...( later === null
346+ ? [ ]
347+ : [
348+ {
349+ id : TurnItemId . make ( "item:later-reply" ) ,
350+ runId : boxLaterRunId ,
351+ text : later ,
352+ type : "assistant_message" ,
353+ } ,
354+ ] ) ,
355+ ...( laterError === null
356+ ? [ ]
357+ : [
358+ {
359+ id : TurnItemId . make ( "item:later-error" ) ,
360+ runId : boxLaterRunId ,
361+ text : laterError ,
362+ type : "error" ,
363+ } ,
364+ ] ) ,
365+ ]
366+ ) . map ( ( entry , position ) => ( { ...entry , position } ) ) ;
347367 const visible =
348368 options . itemId === undefined
349369 ? replies . filter (
@@ -352,7 +372,7 @@ const boxToolkitLayer = (thread: BoxThread) => {
352372 : replies . filter ( ( entry ) => entry . id === options . itemId ) ;
353373 const matching = visible
354374 . filter ( ( entry ) => entry . position > ( options . afterPosition ?? - 1 ) )
355- . slice ( 0 , 1 ) ;
375+ . slice ( 0 , yield * Ref . get ( thread . pageSize ) ) ;
356376 return {
357377 items : matching . map ( ( { id, runId : itemRunId , text, position, type } ) => ( {
358378 position,
@@ -1238,6 +1258,96 @@ it.effect.each([false, true])(
12381258 ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
12391259) ;
12401260
1261+ it . effect . each ( [ "tail-error" , "middle-error" , "reply-fallback" , "cancel-refill" ] as const ) (
1262+ "reads a failed task through full activity pages (%s)" ,
1263+ ( scenario ) =>
1264+ Effect . gen ( function * ( ) {
1265+ const reads : Array < OrchestratorMcpThreadReadInput > = [ ] ;
1266+ const nativeFetch = yield * FetchHttpClient . Fetch ;
1267+ const fetch : typeof globalThis . fetch = ( input , init ) => {
1268+ const body = init ?. body instanceof Uint8Array ? new TextDecoder ( ) . decode ( init . body ) : null ;
1269+ if ( body ?. startsWith ( "{" ) ) {
1270+ const rpc : {
1271+ method ?: string ;
1272+ params ?: { name ?: string ; arguments ?: OrchestratorMcpThreadReadInput } ;
1273+ } = JSON . parse ( body ) ;
1274+ if (
1275+ rpc . method === "tools/call" &&
1276+ rpc . params ?. name === "t3_thread_read" &&
1277+ rpc . params . arguments !== undefined
1278+ ) {
1279+ reads . push ( rpc . params . arguments ) ;
1280+ }
1281+ }
1282+ return nativeFetch ( input , init ) ;
1283+ } ;
1284+ return yield * Effect . scoped (
1285+ Effect . gen ( function * ( ) {
1286+ const thread = yield * makeBoxThread ;
1287+ const b = yield * serveBox ( thread ) ;
1288+ const offers = yield * SubscriptionRef . make <
1289+ ReadonlyArray < ProviderContinuationRequests . ProviderContinuationRequest >
1290+ > ( [ ] ) ;
1291+ const database = yield * SqlitePersistence . layerMemory . pipe ( Layer . build ) ;
1292+ const a = yield * makeLaptop ( database , offers ) ;
1293+ yield * linkTo ( a . links , b , "full-access" ) ;
1294+ yield * seedParent ( a ) ;
1295+ const delegated = yield * delegateToBox ( a ) ;
1296+ const result = `Provider details: ${ "detail " . repeat ( 8_000 ) } end.` ;
1297+ const middle = scenario !== "tail-error" ;
1298+ const ownType = scenario === "reply-fallback" ? "assistant_message" : "error" ;
1299+ const history = ( count : number , runId : RunId , prefix : string ) : Array < TimelineEntry > =>
1300+ Array . from ( { length : count } , ( _ , index ) => ( {
1301+ id : TurnItemId . make ( `${ prefix } :${ index } ` ) ,
1302+ runId,
1303+ text : "discarded history " . repeat ( 120 ) ,
1304+ type : "assistant_message" ,
1305+ } ) ) ;
1306+ yield * Ref . set ( thread . timeline , [
1307+ ...history ( middle ? 150 : 300 , boxRunId , "before" ) ,
1308+ { id : TurnItemId . make ( "selected" ) , runId : boxRunId , text : result , type : ownType } ,
1309+ ...( middle ? history ( 200 , boxLaterRunId , "later" ) : [ ] ) ,
1310+ {
1311+ id : TurnItemId . make ( "other-error" ) ,
1312+ runId : boxLaterRunId ,
1313+ text : "Wrong run" ,
1314+ type : "error" ,
1315+ } ,
1316+ ] ) ;
1317+ yield * Ref . set ( thread . pageSize , 100 ) ;
1318+ yield * Ref . set ( thread . status , "failed" ) ;
1319+ if ( scenario === "cancel-refill" ) yield * Ref . set ( thread . holdRefill , true ) ;
1320+ yield * Deferred . succeed ( thread . finished , undefined ) ;
1321+ if ( scenario === "cancel-refill" ) {
1322+ yield * Deferred . await ( thread . readStarted ) ;
1323+ yield * a . threads . stopDelegatedTasks ( {
1324+ threadId : parentThreadId ,
1325+ commandId : CommandId . make ( "command:stop:mid-refill" ) ,
1326+ } ) ;
1327+ yield * Deferred . succeed ( thread . releaseRead , undefined ) ;
1328+ expect ( yield * taskResult ( a , delegated . taskId ) ) . toMatchObject ( { status : "interrupted" } ) ;
1329+ yield * Deferred . await ( thread . interruptedOnce ) ;
1330+ expect ( yield * Ref . get ( thread . stops ) ) . toContain ( "thread.stop" ) ;
1331+ } else {
1332+ expect ( yield * taskResult ( a , delegated . taskId ) ) . toMatchObject ( {
1333+ status : "failed" ,
1334+ result,
1335+ } ) ;
1336+ }
1337+ const pages = reads . filter ( ( read ) => read . itemId === undefined ) ;
1338+ expect ( pages . map ( ( read ) => read . afterPosition ) ) . toEqual ( [ undefined , 99 , 199 , 299 ] ) ;
1339+ expect ( pages . map ( ( read ) => read . maxCharsPerItem ) ) . toEqual ( [ undefined , 1 , 1 , undefined ] ) ;
1340+ const refills = reads . filter ( ( read ) => read . itemId !== undefined ) ;
1341+ expect ( refills [ 0 ] ) . toMatchObject ( { itemId : "selected" , textOffset : middle ? 1 : 20_000 } ) ;
1342+ if ( scenario !== "cancel-refill" ) expect ( refills ) . toHaveLength ( middle ? 2 : 1 ) ;
1343+ } ) ,
1344+ ) . pipe (
1345+ Effect . provide ( NodeHttpServer . layerTest ) ,
1346+ Effect . provideService ( FetchHttpClient . Fetch , fetch ) ,
1347+ ) ;
1348+ } ) ,
1349+ ) ;
1350+
12411351it . effect ( "keeps a successful remote reply despite an error item in the run's activity" , ( ) =>
12421352 Effect . scoped (
12431353 Effect . gen ( function * ( ) {
0 commit comments