@@ -252,6 +252,7 @@ it.effect.each([
252252 : [ { id : requestRunId , ordinal : scenario . requestOrdinal , status : "running" } ] ) ,
253253 ] ,
254254 nodes : [ { id : "node:request" , runId : requestRunId } ] ,
255+ turnItems : [ ] ,
255256 runtimeRequests : [
256257 {
257258 id : "request:input" ,
@@ -296,7 +297,64 @@ it.effect.each([
296297 } ) ,
297298) ;
298299
299- it . effect . each ( [ "command" , "user_input" ] as const ) (
300+ it . effect . each ( [
301+ { selected : "running" , owner : "running" , ordinal : 1 , status : "waiting" , blocks : true } ,
302+ { selected : "queued" , owner : "running" , ordinal : 0 , status : "waiting" , blocks : true } ,
303+ { selected : "queued" , owner : "running" , ordinal : 2 , status : "waiting" , blocks : true } ,
304+ { selected : "running" , owner : "running" , ordinal : 2 , status : "waiting" , blocks : false } ,
305+ { selected : "running" , owner : "completed" , ordinal : 0 , status : "waiting" , blocks : false } ,
306+ { selected : "running" , owner : "running" , ordinal : 1 , status : "completed" , blocks : false } ,
307+ { selected : "completed" , owner : "completed" , ordinal : 1 , status : "waiting" , blocks : false } ,
308+ ] as const ) ( "waitForThread scopes secret input to unfinished work: %j" , ( scenario ) =>
309+ Effect . gen ( function * ( ) {
310+ const projectId = ProjectId . make ( "project:wait-secret" ) ;
311+ const threadId = ThreadId . make ( "thread:wait-secret" ) ;
312+ const runId = RunId . make ( "run:wait-secret" ) ;
313+ const ownerId = scenario . ordinal === 1 ? runId : RunId . make ( "run:secret-owner" ) ;
314+ const projection = {
315+ thread : { id : threadId , projectId, deletedAt : null } ,
316+ runs : [
317+ { id : runId , ordinal : 1 , status : scenario . selected } ,
318+ ...( ownerId === runId
319+ ? [ ]
320+ : [ { id : ownerId , ordinal : scenario . ordinal , status : scenario . owner } ] ) ,
321+ ] ,
322+ nodes : [ ] ,
323+ runtimeRequests : [ ] ,
324+ turnItems : [
325+ { type : "secret_request" , runId : ownerId , nodeId : "node:secret" , status : scenario . status } ,
326+ ] ,
327+ } as unknown as OrchestrationV2ThreadProjection ;
328+ const layerTest = ThreadManagementService . layer . pipe (
329+ Layer . provide ( Layer . mock ( ProviderSessionManager . ProviderSessionManagerV2 ) ( { } ) ) ,
330+ Layer . provide (
331+ Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
332+ getThreadEventSequence : ( ) => Effect . succeed ( 0 ) ,
333+ getThreadRecords : ( ) => Effect . succeed ( projection ) ,
334+ restartContinuationPending : ( ) => Effect . succeed ( false ) ,
335+ streamStoredEventsFrom : ( ) => Stream . never ,
336+ } ) ,
337+ ) ,
338+ Layer . provide ( ServerSettings . layerTest ( ) ) ,
339+ Layer . provide ( layerReceiptsTest ) ,
340+ ) ;
341+ const service = yield * ThreadManagementService . ThreadManagementService . pipe (
342+ Effect . provide ( layerTest ) ,
343+ ) ;
344+ const fiber = yield * service
345+ . waitForThread ( { projectId, threadId, runId, timeoutMs : 60_000 } )
346+ . pipe ( Effect . forkChild ) ;
347+ yield * TestClock . adjust ( "1 minute" ) ;
348+ expect ( yield * Fiber . join ( fiber ) ) . toMatchObject ( {
349+ run : { id : runId , status : scenario . selected } ,
350+ timedOut : ! scenario . blocks && scenario . selected !== "completed" ,
351+ hasPendingQuestion : scenario . blocks ,
352+ pendingApproval : null ,
353+ } ) ;
354+ } ) ,
355+ ) ;
356+
357+ it . effect . each ( [ "command" , "user_input" , "secret_request" ] as const ) (
300358 "waitForThread wakes on a new %s while terminal-only callers keep waiting" ,
301359 ( kind ) =>
302360 Effect . gen ( function * ( ) {
@@ -307,23 +365,27 @@ it.effect.each(["command", "user_input"] as const)(
307365 const events = yield * Queue . unbounded < OrchestrationV2StoredEvent > ( ) ;
308366 const now = yield * DateTime . now ;
309367 let requests : OrchestrationV2ThreadProjection [ "runtimeRequests" ] = [ ] ;
368+ let turnItems : OrchestrationV2ThreadProjection [ "turnItems" ] = [ ] ;
369+ let reads = 0 ;
370+ const eventType = kind === "secret_request" ? "turn-item.updated" : "runtime-request.updated" ;
310371 const layerTest = ThreadManagementService . layer . pipe (
311372 Layer . provide ( Layer . mock ( ProviderSessionManager . ProviderSessionManagerV2 ) ( { } ) ) ,
312373 Layer . provide (
313374 Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
314375 getThreadEventSequence : ( ) => Effect . succeed ( 7 ) ,
315376 getThreadRecords : ( ) =>
316- Effect . sync (
317- ( ) =>
318- ( {
319- thread : { id : threadId , projectId, deletedAt : null } ,
320- runs : [ { id : runId , ordinal : 1 , status : "running" } ] ,
321- nodes : [ { id : "node:request" , runId } ] ,
322- runtimeRequests : requests ,
323- } ) as unknown as OrchestrationV2ThreadProjection ,
324- ) ,
377+ Effect . sync ( ( ) => {
378+ reads += 1 ;
379+ return {
380+ thread : { id : threadId , projectId, deletedAt : null } ,
381+ runs : [ { id : runId , ordinal : 1 , status : "running" } ] ,
382+ nodes : [ { id : "node:request" , runId } ] ,
383+ runtimeRequests : requests ,
384+ turnItems,
385+ } as unknown as OrchestrationV2ThreadProjection ;
386+ } ) ,
325387 streamStoredEventsFrom : ( input ) =>
326- input ?. eventType === "runtime-request.updated"
388+ input ?. eventType === eventType
327389 ? Stream . fromEffect ( Deferred . succeed ( subscribed , undefined ) ) . pipe (
328390 Stream . drain ,
329391 Stream . concat ( Stream . fromQueue ( events ) ) ,
@@ -341,25 +403,36 @@ it.effect.each(["command", "user_input"] as const)(
341403 . waitForThread ( { projectId, threadId, runId, timeoutMs : 600_000 } )
342404 . pipe ( Effect . forkChild ) ;
343405 yield * Deferred . await ( subscribed ) ;
344- requests = [
345- {
346- id : "request:approval" ,
347- nodeId : "node:request" ,
348- kind,
349- status : "pending" ,
350- responseCapability : { type : "live" } ,
351- createdAt : now ,
352- } ,
353- ] as unknown as OrchestrationV2ThreadProjection [ "runtimeRequests" ] ;
406+ if ( kind === "secret_request" ) {
407+ turnItems = [
408+ { type : kind , runId, nodeId : "node:secret" , status : "waiting" } ,
409+ ] as unknown as OrchestrationV2ThreadProjection [ "turnItems" ] ;
410+ yield * Queue . offer ( events , {
411+ sequence : 8 ,
412+ event : { type : eventType , threadId, payload : { type : "assistant_message" } } ,
413+ } as unknown as OrchestrationV2StoredEvent ) ;
414+ } else {
415+ requests = [
416+ {
417+ id : "request:approval" ,
418+ nodeId : "node:request" ,
419+ kind,
420+ status : "pending" ,
421+ responseCapability : { type : "live" } ,
422+ createdAt : now ,
423+ } ,
424+ ] as unknown as OrchestrationV2ThreadProjection [ "runtimeRequests" ] ;
425+ }
354426 yield * Queue . offer ( events , {
355- sequence : 8 ,
356- event : { type : "runtime-request.updated" , threadId, payload : requests [ 0 ] } ,
427+ sequence : 9 ,
428+ event : { type : eventType , threadId, payload : turnItems [ 0 ] ?? requests [ 0 ] } ,
357429 } as unknown as OrchestrationV2StoredEvent ) ;
358430 expect ( yield * Fiber . join ( fiber ) ) . toMatchObject ( {
359431 timedOut : false ,
360- hasPendingQuestion : kind === "user_input ",
432+ hasPendingQuestion : kind !== "command ",
361433 pendingApproval : kind === "command" ? { requestId : "request:approval" , kind, runId } : null ,
362434 } ) ;
435+ expect ( reads ) . toBe ( 2 ) ;
363436 const terminalWait = yield * service
364437 . waitForThread ( { projectId, threadId, runId, timeoutMs : 60_000 , terminalOnly : true } )
365438 . pipe ( Effect . forkChild ) ;
@@ -410,6 +483,7 @@ it.effect("waitForThread follows a restart continuation instead of the cut run",
410483 runs,
411484 nodes : [ ] ,
412485 runtimeRequests : [ ] ,
486+ turnItems : [ ] ,
413487 } ) as unknown as OrchestrationV2ThreadProjection ,
414488 ) ,
415489 } ) ,
0 commit comments