@@ -126,6 +126,7 @@ function makeClaudeTestTurnInput(input: {
126126 readonly text : string ;
127127 readonly attachments : ProviderAdapterV2TurnInput [ "message" ] [ "attachments" ] ;
128128 readonly providerTurnOrdinal ?: number ;
129+ readonly nativeThreadHasTurns ?: boolean ;
129130 readonly messageCreatedBy ?: ProviderAdapterV2TurnInput [ "message" ] [ "createdBy" ] ;
130131 readonly messageCreationSource ?: ProviderAdapterV2TurnInput [ "message" ] [ "creationSource" ] ;
131132 readonly modelSelection ?: ModelSelection ;
@@ -137,6 +138,9 @@ function makeClaudeTestTurnInput(input: {
137138 runId : RunId . make ( `run-${ input . attemptId } ` ) ,
138139 runOrdinal : 1 ,
139140 providerTurnOrdinal : input . providerTurnOrdinal ?? 1 ,
141+ ...( input . nativeThreadHasTurns === undefined
142+ ? { }
143+ : { nativeThreadHasTurns : input . nativeThreadHasTurns } ) ,
140144 attemptId : input . attemptId ,
141145 rootNodeId : NodeId . make ( `node-${ input . attemptId } ` ) ,
142146 providerThread : input . providerThread ,
@@ -1783,7 +1787,7 @@ describe("ClaudeAdapterV2 native fork", () => {
17831787} ) ;
17841788
17851789describe ( "ClaudeAdapterV2 native session identity" , ( ) => {
1786- const openTurnWithOrdinal = ( providerTurnOrdinal : number ) =>
1790+ const openTurnWithOrdinal = ( providerTurnOrdinal : number , nativeThreadHasTurns ?: boolean ) =>
17871791 Effect . scoped (
17881792 Effect . gen ( function * ( ) {
17891793 const fileSystem = yield * FileSystem . FileSystem ;
@@ -1842,6 +1846,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
18421846 text : "Respond with identity ok" ,
18431847 attachments : [ ] ,
18441848 providerTurnOrdinal,
1849+ ...( nativeThreadHasTurns === undefined ? { } : { nativeThreadHasTurns } ) ,
18451850 } ) ,
18461851 ) ;
18471852 return openedQueries ;
@@ -1867,6 +1872,14 @@ describe("ClaudeAdapterV2 native session identity", () => {
18671872 assert . equal ( openedQueries [ 0 ] ?. options . sessionId , undefined ) ;
18681873 } ) ,
18691874 ) ;
1875+
1876+ it . effect ( "creates a fresh native session despite earlier provider-thread turns" , ( ) =>
1877+ Effect . gen ( function * ( ) {
1878+ const openedQueries = yield * openTurnWithOrdinal ( 4 , false ) ;
1879+ assert . equal ( openedQueries [ 0 ] ?. options . sessionId , "native-session-identity" ) ;
1880+ assert . equal ( openedQueries [ 0 ] ?. options . resume , undefined ) ;
1881+ } ) ,
1882+ ) ;
18701883} ) ;
18711884
18721885const encodeJsonString = Schema . encodeSync ( Schema . fromJsonString ( Schema . Unknown ) ) ;
@@ -2182,6 +2195,68 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21822195 } ) ;
21832196 const makeWakeHarness = makeWakeHarnessWithOptions ( ) ;
21842197
2198+ it . effect (
2199+ "reuses a background shell's query for omitted and explicit Normal, but blocks Fast" ,
2200+ ( ) =>
2201+ Effect . scoped (
2202+ Effect . gen ( function * ( ) {
2203+ const harness = yield * makeWakeHarness ;
2204+ const now = yield * DateTime . now ;
2205+ const normal = {
2206+ instanceId : ClaudeAdapterV2 . CLAUDE_DEFAULT_INSTANCE_ID ,
2207+ model : "claude-opus-5-5" ,
2208+ } satisfies ModelSelection ;
2209+ const turn = ( ordinal : number , modelSelection : ModelSelection ) =>
2210+ makeClaudeTestTurnInput ( {
2211+ threadId : harness . threadId ,
2212+ providerThread : harness . providerThread ,
2213+ now,
2214+ attemptId : RunAttemptId . make ( `attempt-normal-background:${ ordinal } ` ) ,
2215+ text : `Request ${ ordinal } ` ,
2216+ attachments : [ ] ,
2217+ providerTurnOrdinal : ordinal ,
2218+ modelSelection,
2219+ } ) ;
2220+ yield * harness . runtime . startTurn ( turn ( 1 , normal ) ) ;
2221+ const originalOptions = harness . getOpenedOptions ( ) ;
2222+ yield * harness . offerAndWait ( wakeTaskStarted ) ;
2223+ yield * harness . offerAndWait ( turnOneResult ) ;
2224+ yield * Queue . take ( harness . terminalReceipts ) ;
2225+ assert . isTrue ( yield * harness . hasPendingBackgroundWork ) ;
2226+
2227+ yield * harness . runtime . startTurn (
2228+ turn ( 2 , {
2229+ ...normal ,
2230+ options : [ { id : "fastMode" , value : false } ] ,
2231+ } ) ,
2232+ ) ;
2233+ assert . strictEqual ( harness . getOpenedOptions ( ) , originalOptions ) ;
2234+ assert . lengthOf ( harness . offeredMessages , 2 ) ;
2235+ yield * harness . offerAndWait ( turnOneResult ) ;
2236+ yield * Queue . take ( harness . terminalReceipts ) ;
2237+
2238+ const refused = yield * harness . runtime
2239+ . startTurn (
2240+ turn ( 3 , {
2241+ ...normal ,
2242+ options : [ { id : "fastMode" , value : true } ] ,
2243+ } ) ,
2244+ )
2245+ . pipe ( Effect . result ) ;
2246+ assert . equal ( refused . _tag , "Failure" ) ;
2247+ if ( refused . _tag === "Failure" ) {
2248+ assert . instanceOf (
2249+ refused . failure . cause ,
2250+ ClaudeAdapterV2 . ClaudeBackgroundWorkBlocksQueryReplacementError ,
2251+ ) ;
2252+ }
2253+ assert . strictEqual ( harness . getOpenedOptions ( ) , originalOptions ) ;
2254+ assert . lengthOf ( harness . offeredMessages , 2 ) ;
2255+ assert . isTrue ( yield * harness . hasPendingBackgroundWork ) ;
2256+ } ) . pipe ( Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
2257+ ) ,
2258+ ) ;
2259+
21852260 it . effect . each ( [ "completed" , "interrupted" ] as const ) (
21862261 "projects Claude thinking blocks when %s" ,
21872262 ( status ) =>
0 commit comments