@@ -16,6 +16,7 @@ import {
1616} from "@t3tools/contracts" ;
1717import * as DateTime from "effect/DateTime" ;
1818import * as Effect from "effect/Effect" ;
19+ import * as TestClock from "effect/testing/TestClock" ;
1920
2021import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts" ;
2122import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts" ;
@@ -54,6 +55,149 @@ const forkCases = (["codex", "claudeAgent"] as const).flatMap((driverName) => {
5455 } ) ) ;
5556} ) ;
5657
58+ it . effect ( "a persisted fork leaves PR watching with its parent until explicitly opted in" , ( ) =>
59+ Effect . gen ( function * ( ) {
60+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
61+ const eventSink = yield * EventSink . EventSinkV2 ;
62+ const now = yield * DateTime . now ;
63+ const { instanceId, modelSelection } = forkCases [ 0 ] ! ;
64+ const sourceThreadId = ThreadId . make ( "fork-watch-source" ) ;
65+ const targetThreadId = ThreadId . make ( "fork-watch-target" ) ;
66+ const sourceRunId = RunId . make ( "fork-watch-source-run" ) ;
67+ const key = { host : "github.com" , repository : "example/repo" , number : 1 } ;
68+ const link = { url : "https://github.com/example/repo/pull/1" , source : "agent" as const } ;
69+
70+ yield * orchestrator . dispatch ( {
71+ type : "thread.create" ,
72+ commandId : CommandId . make ( "fork-watch-create" ) ,
73+ threadId : sourceThreadId ,
74+ projectId : ProjectId . make ( "fork-watch-project" ) ,
75+ title : "Watching source" ,
76+ modelSelection,
77+ runtimeMode : "full-access" ,
78+ interactionMode : "default" ,
79+ branch : null ,
80+ worktreePath : null ,
81+ createdBy : "user" ,
82+ creationSource : "web" ,
83+ } ) ;
84+ yield * eventSink . write ( {
85+ events : [
86+ {
87+ id : EventId . make ( "fork-watch-source-run" ) ,
88+ type : "run.created" ,
89+ threadId : sourceThreadId ,
90+ runId : sourceRunId ,
91+ occurredAt : now ,
92+ payload : {
93+ id : sourceRunId ,
94+ threadId : sourceThreadId ,
95+ ordinal : 1 ,
96+ providerInstanceId : instanceId ,
97+ modelSelection,
98+ providerThreadId : null ,
99+ userMessageId : MessageId . make ( "fork-watch-source-message" ) ,
100+ rootNodeId : null ,
101+ activeAttemptId : null ,
102+ status : "completed" ,
103+ requestedAt : now ,
104+ startedAt : now ,
105+ completedAt : now ,
106+ checkpointId : null ,
107+ contextHandoffId : null ,
108+ } ,
109+ } ,
110+ ] ,
111+ } ) ;
112+ yield * orchestrator . dispatch ( {
113+ type : "thread.pull-request.watch" ,
114+ commandId : CommandId . make ( "fork-watch-parent-start" ) ,
115+ threadId : sourceThreadId ,
116+ ...key ,
117+ watching : true ,
118+ link,
119+ } ) ;
120+ const parentBefore = yield * orchestrator . getThreadShell ( sourceThreadId ) ;
121+ const parentWatch = parentBefore ?. pullRequests ?. [ 0 ] ?. watch ;
122+ assert . isDefined ( parentWatch ) ;
123+ assert . lengthOf ( parentBefore ?. pendingBackgroundTasks ?? [ ] , 1 ) ;
124+
125+ yield * orchestrator . dispatch ( {
126+ type : "thread.fork" ,
127+ commandId : CommandId . make ( "fork-watch-fork" ) ,
128+ sourceThreadId,
129+ targetThreadId,
130+ sourcePoint : { type : "run" , runId : sourceRunId } ,
131+ createdBy : "user" ,
132+ creationSource : "mcp" ,
133+ } ) ;
134+ const fork = yield * orchestrator . getThreadShell ( targetThreadId ) ;
135+ assert . isNotNull ( fork ) ;
136+ assert . deepEqual ( fork . pullRequests , [
137+ {
138+ ...key ,
139+ ...link ,
140+ linkedAt : parentWatch . startedAt ,
141+ snapshot : null ,
142+ stack : null ,
143+ } ,
144+ ] ) ;
145+ assert . deepEqual ( fork . pendingBackgroundTasks , [ ] ) ;
146+ assert . deepEqual ( yield * orchestrator . getThreadShell ( sourceThreadId ) , parentBefore ) ;
147+
148+ // A delayed watcher update cannot reinstate the parent's subscription on the fork.
149+ const stale = yield * orchestrator
150+ . dispatch ( {
151+ type : "thread.pull-request-watch.sync" ,
152+ commandId : CommandId . make ( "fork-watch-stale-sync" ) ,
153+ threadId : targetThreadId ,
154+ ...key ,
155+ startedAt : parentWatch . startedAt ,
156+ watch : parentWatch ,
157+ wake : {
158+ messageId : MessageId . make ( "fork-watch-unwanted-wake" ) ,
159+ text : "A check failed" ,
160+ notification : {
161+ source : { kind : "monitor" } ,
162+ outcome : "updated" ,
163+ summary : "A check failed" ,
164+ } ,
165+ } ,
166+ } )
167+ . pipe ( Effect . flip ) ;
168+ assert . equal ( stale . _tag , "OrchestratorDispatchError" ) ;
169+ assert . deepEqual ( ( yield * orchestrator . getThreadProjection ( targetThreadId ) ) . runs , [ ] ) ;
170+
171+ // Advance virtual time only to distinguish the independent watch's start time.
172+ yield * TestClock . adjust ( "1 second" ) ;
173+ yield * orchestrator . dispatch ( {
174+ type : "thread.pull-request.watch" ,
175+ commandId : CommandId . make ( "fork-watch-opt-in" ) ,
176+ threadId : targetThreadId ,
177+ ...key ,
178+ watching : true ,
179+ link,
180+ } ) ;
181+ const watchingFork = yield * orchestrator . getThreadShell ( targetThreadId ) ;
182+ assert . isDefined ( watchingFork ?. pullRequests ?. [ 0 ] ?. watch ) ;
183+ assert . notEqual ( watchingFork . pullRequests [ 0 ] . watch . startedAt , parentWatch . startedAt ) ;
184+ assert . lengthOf ( watchingFork . pendingBackgroundTasks ?? [ ] , 1 ) ;
185+ assert . deepEqual ( yield * orchestrator . getThreadShell ( sourceThreadId ) , parentBefore ) ;
186+
187+ yield * orchestrator . dispatch ( {
188+ type : "thread.pull-request.watch" ,
189+ commandId : CommandId . make ( "fork-watch-opt-out" ) ,
190+ threadId : targetThreadId ,
191+ ...key ,
192+ watching : false ,
193+ } ) ;
194+ const stoppedFork = yield * orchestrator . getThreadShell ( targetThreadId ) ;
195+ assert . deepEqual ( stoppedFork ?. pullRequests , fork . pullRequests ) ;
196+ assert . deepEqual ( stoppedFork ?. pendingBackgroundTasks , [ ] ) ;
197+ assert . deepEqual ( yield * orchestrator . getThreadShell ( sourceThreadId ) , parentBefore ) ;
198+ } ) . pipe ( Effect . provide ( forkCases [ 0 ] ! . layer ) ) ,
199+ ) ;
200+
57201it . effect . each ( forkCases ) (
58202 "bounds $driver context when continuing a fork of a $status run" ,
59203 ( { driver, status, instanceId, modelSelection, layer } ) =>
0 commit comments