@@ -290,8 +290,14 @@ function makeLocalCommandHarness(input: {
290290 readonly interruptFailure ?: boolean ;
291291 readonly interruptCurrentRun ?: boolean ;
292292 } ;
293- /** A side chat whose context targets this run, or an earlier run that failed before the provider. */
294- readonly sideChat ?: { readonly contextRun : "current" | "failed" } ;
293+ /**
294+ * A side chat whose context targets this run, an earlier run that failed before the provider,
295+ * or an earlier run that delivered it. `nativeForkFrom` plans a native fork of the parent.
296+ */
297+ readonly sideChat ?: {
298+ readonly contextRun : "current" | "failed" | "delivered" ;
299+ readonly nativeForkFrom ?: "idle" | "busy" ;
300+ } ;
295301} ) {
296302 const now = DateTime . makeUnsafe ( "2026-09-04T12:00:00Z" ) ;
297303 const threadId = ThreadId . make ( "thread-native-account-command" ) ;
@@ -465,9 +471,52 @@ function makeLocalCommandHarness(input: {
465471 checkpoints : [ ] ,
466472 updatedAt : now ,
467473 } ;
474+ const parentThreadId = ThreadId . make ( "side-chat-parent" ) ;
475+ const parentRunId = RunId . make ( "side-chat-parent-run" ) ;
476+ const parentProviderThread = {
477+ ...providerThread ,
478+ id : ProviderThreadId . make ( "side-chat-parent-provider-thread" ) ,
479+ appThreadId : parentThreadId ,
480+ nativeThreadRef : {
481+ driver : providerThread . driver ,
482+ nativeId : "side-chat-parent-native" ,
483+ strength : "strong" as const ,
484+ } ,
485+ } ;
486+ const parentRecords = {
487+ ...projection ,
488+ thread : { ...projection . thread , id : parentThreadId } ,
489+ runs : [
490+ {
491+ ...run ,
492+ id : parentRunId ,
493+ threadId : parentThreadId ,
494+ ordinal : 1 ,
495+ providerThreadId : parentProviderThread . id ,
496+ activeAttemptId : null ,
497+ status : "completed" as const ,
498+ completedAt : now ,
499+ } ,
500+ ...( input . sideChat ?. nativeForkFrom === "busy"
501+ ? [
502+ {
503+ ...run ,
504+ id : RunId . make ( "side-chat-parent-busy-run" ) ,
505+ threadId : parentThreadId ,
506+ ordinal : 2 ,
507+ providerThreadId : parentProviderThread . id ,
508+ status : "running" as const ,
509+ } ,
510+ ]
511+ : [ ] ) ,
512+ ] ,
513+ providerThreads : [ parentProviderThread ] ,
514+ attempts : [ ] ,
515+ providerTurns : [ ] ,
516+ turnItems : [ ] ,
517+ } ;
468518 if ( input . sideChat !== undefined ) {
469- const parentThreadId = ThreadId . make ( "side-chat-parent" ) ;
470- const failedRunId = RunId . make ( "side-chat-failed-run" ) ;
519+ const earlierRunId = RunId . make ( "side-chat-earlier-run" ) ;
471520 projection = {
472521 ...projection ,
473522 thread : {
@@ -481,26 +530,49 @@ function makeLocalCommandHarness(input: {
481530 } ,
482531 } ,
483532 runs :
484- input . sideChat . contextRun === "failed"
485- ? [
533+ input . sideChat . contextRun === "current"
534+ ? projection . runs
535+ : [
486536 {
487537 ...run ,
488- id : failedRunId ,
538+ id : earlierRunId ,
489539 ordinal : 1 ,
490- userMessageId : MessageId . make ( "side-chat-failed -message" ) ,
491- activeAttemptId : RunAttemptId . make ( "side-chat-failed -attempt" ) ,
492- status : "failed" ,
540+ userMessageId : MessageId . make ( "side-chat-earlier -message" ) ,
541+ activeAttemptId : RunAttemptId . make ( "side-chat-earlier -attempt" ) ,
542+ status : input . sideChat . contextRun === "failed" ? "failed" : "completed ",
493543 completedAt : now ,
494544 } ,
495545 run ,
496- ]
497- : projection . runs ,
546+ ] ,
547+ contextTransfers :
548+ input . sideChat . nativeForkFrom === undefined
549+ ? [ ]
550+ : [
551+ {
552+ id : ContextTransferId . make ( "side-chat-fork" ) ,
553+ type : "fork" ,
554+ sourceThreadId : parentThreadId ,
555+ targetThreadId : threadId ,
556+ sourcePoint : { threadId : parentThreadId , runId : parentRunId } ,
557+ basePoint : null ,
558+ sourceProviderInstanceId : newInstanceId ,
559+ targetProviderInstanceId : newInstanceId ,
560+ targetRunId : runId ,
561+ status : "pending" ,
562+ resolution : null ,
563+ createdBy : "user" ,
564+ error : null ,
565+ createdAt : now ,
566+ updatedAt : now ,
567+ consumedAt : null ,
568+ } ,
569+ ] ,
498570 contextHandoffs : [
499571 {
500572 id : ContextHandoffId . make ( "side-chat-context" ) ,
501573 transferId : ContextTransferId . make ( "side-chat-fork" ) ,
502574 threadId,
503- targetRunId : input . sideChat . contextRun === "failed " ? failedRunId : runId ,
575+ targetRunId : input . sideChat . contextRun === "current " ? runId : earlierRunId ,
504576 fromProviderThreadIds : [ ] ,
505577 toProviderThreadId : providerThreadId ,
506578 coveredRunOrdinals : { from : 1 , to : 1 } ,
@@ -511,6 +583,15 @@ function makeLocalCommandHarness(input: {
511583 createdByProviderInstanceId : null ,
512584 createdAt : now ,
513585 updatedAt : now ,
586+ ...( input . sideChat . contextRun === "delivered"
587+ ? {
588+ delivery : {
589+ nativeThreadId : "side-chat-native" ,
590+ status : "inline" as const ,
591+ itemIds : [ ] ,
592+ } ,
593+ }
594+ : { } ) ,
514595 } ,
515596 ] ,
516597 } ;
@@ -647,18 +728,21 @@ function makeLocalCommandHarness(input: {
647728 lastError : null ,
648729 } ;
649730 const startTurn = vi . fn ( ( _turn : ProviderAdapter . ProviderAdapterV2TurnInput ) => Effect . void ) ;
731+ const sideChatThread = {
732+ ...providerThread ,
733+ nativeThreadRef : {
734+ driver : providerThread . driver ,
735+ nativeId : "side-chat-native" ,
736+ strength : "strong" as const ,
737+ } ,
738+ } ;
739+ const forkThread = vi . fn ( ( ) => Effect . succeed ( sideChatThread ) ) ;
650740 const sideChatSession = {
651741 driver : providerThread . driver ,
652742 providerSession,
653- ensureThread : ( ) =>
654- Effect . succeed ( {
655- ...providerThread ,
656- nativeThreadRef : {
657- driver : providerThread . driver ,
658- nativeId : "side-chat-native" ,
659- strength : "strong" as const ,
660- } ,
661- } ) ,
743+ ensureThread : ( ) => Effect . succeed ( sideChatThread ) ,
744+ resumeThread : ( ) => Effect . succeed ( sideChatThread ) ,
745+ forkThread,
662746 startTurn,
663747 } ;
664748 const open = vi . fn ( ( ) =>
@@ -757,8 +841,8 @@ function makeLocalCommandHarness(input: {
757841 const createWorktree = vi . fn < GitWorkflow . GitWorkflowService [ "Service" ] [ "createWorktree" ] > ( ( ) =>
758842 Effect . succeed ( { } as never ) ,
759843 ) ;
760- const getThreadRecords = vi . fn < ProjectionStore . ProjectionStoreV2Shape [ "getThreadRecords" ] > ( ( ) =>
761- Effect . succeed ( projection ) ,
844+ const getThreadRecords = vi . fn < ProjectionStore . ProjectionStoreV2Shape [ "getThreadRecords" ] > ( ( id ) =>
845+ Effect . succeed ( id === parentThreadId ? parentRecords : projection ) ,
762846 ) ;
763847 const getTurnStartContext = vi . fn ( ( ) =>
764848 Effect . succeed ( {
@@ -774,7 +858,18 @@ function makeLocalCommandHarness(input: {
774858 Layer . provide (
775859 Layer . mergeAll (
776860 Layer . mock ( ContextHandoffService . ContextHandoffServiceV2 ) ( {
777- prepareProviderHandoff : ( ) => Effect . die ( "history read must fail first" ) ,
861+ prepareProviderHandoff : ( handoff ) =>
862+ input . sideChat ?. nativeForkFrom === undefined
863+ ? Effect . die ( "history read must fail first" )
864+ : Effect . succeed ( {
865+ ...handoff ,
866+ id : ContextHandoffId . make ( "side-chat-fork-context" ) ,
867+ createdByProviderInstanceId : handoff . toProviderInstanceId ,
868+ status : "ready" ,
869+ summaryMessageId : null ,
870+ summaryText : "PARENT_HISTORY" ,
871+ updatedAt : handoff . createdAt ,
872+ } ) ,
778873 } ) ,
779874 Layer . mock ( EventSink . EventSinkV2 ) ( {
780875 writeIfRunCurrent,
@@ -860,6 +955,7 @@ function makeLocalCommandHarness(input: {
860955 writeIfRunCurrent,
861956 startRootRun,
862957 startTurn,
958+ forkThread,
863959 interruptTurn,
864960 tryHandlePromptCommand,
865961 events,
@@ -1064,11 +1160,12 @@ effectIt.effect("recovers an abandoned failed turn before continuing its native
10641160) ;
10651161
10661162effectIt . effect . each ( [
1067- { contextRun : "current" , runtimeMode : "approval-required" } ,
1068- { contextRun : "failed" , runtimeMode : "full-access" } ,
1163+ { contextRun : "current" , runtimeMode : "approval-required" , context : "SIDE_CHAT_CONTEXT" } ,
1164+ { contextRun : "failed" , runtimeMode : "approval-required" , context : "SIDE_CHAT_CONTEXT" } ,
1165+ { contextRun : "delivered" , runtimeMode : "full-access" , context : "" } ,
10691166] as const ) (
10701167 "sends side chat context inline once, for the $contextRun run's turn" ,
1071- ( { contextRun, runtimeMode } ) =>
1168+ ( { contextRun, runtimeMode, context } ) =>
10721169 Effect . gen ( function * ( ) {
10731170 const harness = makeLocalCommandHarness ( {
10741171 text : "What is the main thread doing?" ,
@@ -1077,12 +1174,12 @@ effectIt.effect.each([
10771174
10781175 yield * harness . start ;
10791176
1080- // Only the first turn is clamped; a later one follows the thread's mode.
1177+ // The turn that delivers the context is clamped; a later one follows the thread's mode.
10811178 expect ( harness . open ) . toHaveBeenCalledWith (
10821179 expect . objectContaining ( { runtimePolicy : expect . objectContaining ( { runtimeMode } ) } ) ,
10831180 ) ;
10841181 expect ( harness . startTurn . mock . calls . map ( ( [ turn ] ) => turn . message . text ) ) . toEqual ( [
1085- withInlineContext ( "SIDE_CHAT_CONTEXT" , "What is the main thread doing?" ) ,
1182+ withInlineContext ( context , "What is the main thread doing?" ) ,
10861183 ] ) ;
10871184 expect ( harness . projection ( ) . contextHandoffs ) . toMatchObject ( [
10881185 {
@@ -1093,6 +1190,30 @@ effectIt.effect.each([
10931190 } ) ,
10941191) ;
10951192
1193+ effectIt . effect . each ( [
1194+ { nativeForkFrom : "idle" , forks : true , transferStatus : "consumed" } ,
1195+ { nativeForkFrom : "busy" , forks : false , transferStatus : "resolved_portable" } ,
1196+ ] as const ) (
1197+ "runs a planned native side chat fork when the parent is $nativeForkFrom" ,
1198+ ( { nativeForkFrom, forks, transferStatus } ) =>
1199+ Effect . gen ( function * ( ) {
1200+ const harness = makeLocalCommandHarness ( {
1201+ text : "What is the main thread doing?" ,
1202+ sideChat : { contextRun : "current" , nativeForkFrom } ,
1203+ } ) ;
1204+
1205+ yield * harness . start ;
1206+
1207+ expect ( harness . forkThread ) . toHaveBeenCalledTimes ( forks ? 1 : 0 ) ;
1208+ expect ( harness . projection ( ) . contextTransfers ) . toMatchObject ( [
1209+ { id : "side-chat-fork" , status : transferStatus } ,
1210+ ] ) ;
1211+ expect ( harness . startTurn . mock . calls [ 0 ] ?. [ 0 ] . message . text . includes ( "PARENT_HISTORY" ) ) . toBe (
1212+ ! forks ,
1213+ ) ;
1214+ } ) ,
1215+ ) ;
1216+
10961217effectIt . effect . each ( [
10971218 { runStatus : "failed" , turnStatus : "completed" } ,
10981219 { runStatus : "completed" , turnStatus : "completed" } ,
0 commit comments