@@ -26,6 +26,7 @@ import {
2626 ProviderTurnId ,
2727 RunAttemptId ,
2828 RunId ,
29+ ScheduledTaskId ,
2930 ThreadId ,
3031} from "@t3tools/contracts" ;
3132import { assert , describe , it } from "@effect/vitest" ;
@@ -2060,7 +2061,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
20602061
20612062 const makeWakeHarnessWithOptions = ( options ?: {
20622063 readonly close ?: ( sdkMessages : Queue . Queue < SDKMessage > ) => Effect . Effect < void > ;
2063- readonly interrupt ?: Effect . Effect < void > ;
2064+ readonly interrupt ?: Effect . Effect < void , ClaudeAdapterV2 . ClaudeAgentSdkQueryRunnerError > ;
20642065 readonly environment ?: NodeJS . ProcessEnv ;
20652066 } ) =>
20662067 Effect . gen ( function * ( ) {
@@ -2553,6 +2554,106 @@ describe("ClaudeAdapterV2 background wake turns", () => {
25532554 ) ,
25542555 ) ;
25552556
2557+ it . effect . each ( [
2558+ { sender : "a user's" , createdBy : "user" , scheduled : false , interrupts : true } ,
2559+ { sender : "an agent's" , createdBy : "agent" , scheduled : false , interrupts : false } ,
2560+ { sender : "a scheduled task's" , createdBy : "user" , scheduled : true , interrupts : false } ,
2561+ ] as const ) (
2562+ "$sender steer interrupts the turn before it is offered: $interrupts" ,
2563+ ( { createdBy, scheduled, interrupts } ) =>
2564+ Effect . scoped (
2565+ Effect . gen ( function * ( ) {
2566+ // Offers already sent when each interrupt reached the CLI.
2567+ const offersAtInterrupt : Array < number > = [ ] ;
2568+ let offered : ReadonlyArray < SDKUserMessage > = [ ] ;
2569+ const harness = yield * makeWakeHarnessWithOptions ( {
2570+ interrupt : Effect . sync ( ( ) => {
2571+ offersAtInterrupt . push ( offered . length ) ;
2572+ } ) ,
2573+ } ) ;
2574+ offered = harness . offeredMessages ;
2575+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
2576+ const attemptId = RunAttemptId . make ( "attempt-steer-interrupt" ) ;
2577+ const input = makeClaudeTestTurnInput ( {
2578+ threadId : harness . threadId ,
2579+ providerThread : harness . providerThread ,
2580+ now : yield * DateTime . now ,
2581+ attemptId,
2582+ text : "Scan the repository." ,
2583+ attachments : [ ] ,
2584+ } ) ;
2585+ yield * harness . runtime . startTurn ( input ) ;
2586+ yield * harness . runtime . steerTurn ( {
2587+ threadId : harness . threadId ,
2588+ runId : input . runId ,
2589+ providerThread : harness . providerThread ,
2590+ providerTurnId : idAllocator . derive . providerTurn ( {
2591+ driver : ClaudeAdapterV2 . CLAUDE_PROVIDER ,
2592+ nativeTurnId : `turn:${ attemptId } ` ,
2593+ } ) ,
2594+ message : {
2595+ createdBy,
2596+ creationSource : scheduled ? "server" : "web" ,
2597+ messageId : MessageId . make ( "message-steer-interrupt" ) ,
2598+ text : "Stop waiting and summarize what you have." ,
2599+ attachments : [ ] ,
2600+ ...( scheduled ? { scheduledTaskId : ScheduledTaskId . make ( "scheduled-scan" ) } : { } ) ,
2601+ } ,
2602+ } ) ;
2603+
2604+ // The interrupt lands before the steer is offered, so the steer is
2605+ // never folded into the turn the interrupt aborts.
2606+ assert . deepEqual ( offersAtInterrupt , interrupts ? [ 1 ] : [ ] ) ;
2607+ assert . equal ( harness . offeredMessages [ 1 ] ?. priority , "now" ) ;
2608+ } ) . pipe ( Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
2609+ ) ,
2610+ ) ;
2611+
2612+ it . effect ( "still offers a user steer when the interrupt fails" , ( ) =>
2613+ Effect . scoped (
2614+ Effect . gen ( function * ( ) {
2615+ const harness = yield * makeWakeHarnessWithOptions ( {
2616+ interrupt : Effect . fail (
2617+ new ClaudeAdapterV2 . ClaudeAgentSdkQueryRunnerError ( {
2618+ method : "interrupt" ,
2619+ cause : "forced interrupt failure" ,
2620+ } ) ,
2621+ ) ,
2622+ } ) ;
2623+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
2624+ const attemptId = RunAttemptId . make ( "attempt-steer-interrupt-failed" ) ;
2625+ const input = makeClaudeTestTurnInput ( {
2626+ threadId : harness . threadId ,
2627+ providerThread : harness . providerThread ,
2628+ now : yield * DateTime . now ,
2629+ attemptId,
2630+ text : "Scan the repository." ,
2631+ attachments : [ ] ,
2632+ } ) ;
2633+ yield * harness . runtime . startTurn ( input ) ;
2634+ yield * harness . runtime . steerTurn ( {
2635+ threadId : harness . threadId ,
2636+ runId : input . runId ,
2637+ providerThread : harness . providerThread ,
2638+ providerTurnId : idAllocator . derive . providerTurn ( {
2639+ driver : ClaudeAdapterV2 . CLAUDE_PROVIDER ,
2640+ nativeTurnId : `turn:${ attemptId } ` ,
2641+ } ) ,
2642+ message : {
2643+ createdBy : "user" ,
2644+ creationSource : "web" ,
2645+ messageId : MessageId . make ( "message-steer-interrupt-failed" ) ,
2646+ text : "Stop waiting and summarize what you have." ,
2647+ attachments : [ ] ,
2648+ } ,
2649+ } ) ;
2650+
2651+ assert . lengthOf ( harness . offeredMessages , 2 ) ;
2652+ assert . equal ( harness . offeredMessages [ 1 ] ?. priority , "now" ) ;
2653+ } ) . pipe ( Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
2654+ ) ,
2655+ ) ;
2656+
25562657 it . effect ( "announces usage-limit pauses once per window and again on a new turn" , ( ) =>
25572658 Effect . gen ( function * ( ) {
25582659 const harness = yield * makeWakeHarness ;
0 commit comments