Skip to content

Commit 36dfa93

Browse files
committed
fix(server): forks no longer inherit pull request watches
1 parent a1db449 commit 36dfa93

3 files changed

Lines changed: 225 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadFork.execution.test.ts‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "@t3tools/contracts";
1717
import * as DateTime from "effect/DateTime";
1818
import * as Effect from "effect/Effect";
19+
import * as TestClock from "effect/testing/TestClock";
1920

2021
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
2122
import { 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+
57201
it.effect.each(forkCases)(
58202
"bounds $driver context when continuing a fork of a $status run",
59203
({ driver, status, instanceId, modelSelection, layer }) =>

‎apps/server/src/orchestration-v2/ThreadForkService.test.ts‎

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ProviderThreadId,
1212
RunId,
1313
ThreadId,
14+
type ThreadPullRequestLink,
1415
} from "@t3tools/contracts";
1516
import * as DateTime from "effect/DateTime";
1617
import * as Effect from "effect/Effect";
@@ -106,11 +107,14 @@ function makeSourceProjection(sourceRun: OrchestrationV2Run): OrchestrationV2Thr
106107
};
107108
}
108109

109-
const planFork = (sourceRun: OrchestrationV2Run) =>
110+
const planFork = (
111+
sourceRun: OrchestrationV2Run,
112+
sourceProjection = makeSourceProjection(sourceRun),
113+
) =>
110114
Effect.gen(function* () {
111115
const service = yield* ThreadForkService.ThreadForkServiceV2;
112116
return yield* service.plan({
113-
sourceProjection: makeSourceProjection(sourceRun),
117+
sourceProjection,
114118
sourceRun,
115119
sourceProviderThread: undefined,
116120
canonicalSourcePoint: {
@@ -179,6 +183,77 @@ it.effect("forks from a usage-limited failed run", () =>
179183
}),
180184
);
181185

186+
it.effect("preserves pull request context without inheriting any watches", () =>
187+
Effect.gen(function* () {
188+
const sourceRun = makeSourceRun("completed");
189+
const watch = {
190+
startedAt: DateTime.formatIso(sourceCreatedAt),
191+
headSha: "source-head",
192+
failedChecks: ["lint"],
193+
passed: false,
194+
passedChecks: [],
195+
remarksThrough: DateTime.formatIso(snoozedAt),
196+
remarkIds: ["review-1"],
197+
conflicting: true,
198+
wakes: 2,
199+
};
200+
const links = (["agent", "stack", "manual", "stack-dismissed"] as const).map(
201+
(source, index) =>
202+
({
203+
host: "github.com",
204+
repository: "example/repo",
205+
number: index + 1,
206+
url: `https://github.com/example/repo/pull/${index + 1}`,
207+
source,
208+
linkedAt: DateTime.formatIso(sourceCreatedAt),
209+
snapshot: {
210+
state: "open",
211+
title: `Pull request ${index + 1}`,
212+
headBranch: `feature-${index + 1}`,
213+
baseBranch: "main",
214+
isDraft: false,
215+
updatedAt: null,
216+
syncedAt: DateTime.formatIso(snoozedAt),
217+
},
218+
stack: {
219+
kind: "native",
220+
id: "stack-1",
221+
number: 1,
222+
url: "https://github.com/example/repo/stack/1",
223+
base: "main",
224+
layers: [{ number: 1, headBranch: "feature-1", state: "open" }],
225+
},
226+
}) satisfies ThreadPullRequestLink,
227+
);
228+
const watchedLinks: ReadonlyArray<ThreadPullRequestLink> = links.map((link, index) =>
229+
index < 2 ? { ...link, watch } : link,
230+
);
231+
const legacyLink = {
232+
projectId: makeSourceThread().projectId,
233+
repository: "example/repo",
234+
number: 1,
235+
url: links[0]!.url,
236+
};
237+
const sourceProjection = {
238+
...makeSourceProjection(sourceRun),
239+
thread: {
240+
...makeSourceThread(),
241+
pullRequests: watchedLinks,
242+
linkedPullRequest: legacyLink,
243+
branchPullRequest: legacyLink,
244+
},
245+
};
246+
const result = yield* planFork(sourceRun, sourceProjection);
247+
248+
assert.deepEqual(result.targetThread.pullRequests, links);
249+
assert.deepEqual(result.targetThread.linkedPullRequest, legacyLink);
250+
assert.deepEqual(result.targetThread.branchPullRequest, legacyLink);
251+
assert.deepEqual(sourceProjection.thread.pullRequests, watchedLinks);
252+
assert.deepEqual(sourceProjection.thread.pullRequests[0]?.watch, watch);
253+
assert.deepEqual(sourceProjection.thread.pullRequests[1]?.watch, watch);
254+
}),
255+
);
256+
182257
it.effect("rejects in-progress and rolled-back fork sources", () =>
183258
Effect.gen(function* () {
184259
for (const status of ["running", "rolled_back"] as const) {

‎apps/server/src/orchestration-v2/ThreadForkService.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed(
8787
}
8888
const targetThread: OrchestrationV2AppThread = {
8989
...input.sourceProjection.thread,
90+
// PR context follows the conversation; watching remains the source thread's responsibility.
91+
pullRequests: input.sourceProjection.thread.pullRequests?.map(
92+
({ watch: _watch, ...link }) => link,
93+
),
9094
createdBy: input.createdBy,
9195
creationSource: input.creationSource,
9296
id: input.targetThreadId,

0 commit comments

Comments
 (0)