@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 507 lines 20.4 KB
Raw
1 import {
2 CommandId,
3 type OrchestrationProjectShell,
4 type OrchestrationV2DomainEvent,
5 type OrchestrationV2ThreadShell,
6 type PullRequestSummary,
7 type ThreadId,
8 type ThreadLinkedPullRequest,
9 } from "@t3tools/contracts";
10 import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
11 import * as Cause from "effect/Cause";
12 import * as Context from "effect/Context";
13 import * as SecureRandom from "effect/crypto/SecureRandom";
14 import * as DateTime from "effect/DateTime";
15 import * as Effect from "effect/Effect";
16 import * as FileSystem from "effect/FileSystem";
17 import * as Option from "effect/Option";
18 import * as Path from "effect/Path";
19 import * as Queue from "effect/Queue";
20 import * as Schedule from "effect/Schedule";
21 import type * as Scope from "effect/Scope";
22 import * as Stream from "effect/Stream";
23
24 import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
25 import * as GitManager from "../git/GitManager.ts";
26 import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
27 import * as ProjectStore from "./ProjectStore.ts";
28 import * as PullRequestService from "../pullRequest/PullRequestService.ts";
29 import { makeLinkedThreadsChangeDetector } from "../pullRequest/linkedThreads.ts";
30 import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
31 import { forkParked } from "../serverActivation.ts";
32 import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
33 import { makeNoPullRequestRechecks } from "./noPullRequestRechecks.ts";
34 import * as Orchestrator from "./Orchestrator.ts";
35
36 class ThreadPullRequestServiceV2 extends Context.Service<
37 ThreadPullRequestServiceV2,
38 {
39 readonly start: () => Effect.Effect<void, never, Scope.Scope>;
40 readonly drain: Effect.Effect<void>;
41 }
42 >()("t3/orchestration-v2/ThreadPullRequestService/ThreadPullRequestServiceV2") {}
43
44 function samePullRequest(
45 left: ThreadLinkedPullRequest | null | undefined,
46 right: ThreadLinkedPullRequest | null,
47 ): boolean {
48 if (left == null || right === null) return left == null && right === null;
49 return (
50 left.projectId === right.projectId &&
51 left.repository.toLowerCase() === right.repository.toLowerCase() &&
52 left.number === right.number &&
53 left.url === right.url
54 );
55 }
56
57 function pullRequestPredatesThread(
58 pullRequest: Pick<PullRequestSummary, "state" | "closedAt" | "mergedAt">,
59 createdAt: DateTime.Utc,
60 ): boolean {
61 const terminalAt =
62 pullRequest.state === "merged"
63 ? pullRequest.mergedAt
64 : pullRequest.state === "closed"
65 ? pullRequest.closedAt
66 : null;
67 return terminalAt != null && Date.parse(terminalAt) < DateTime.toEpochMillis(createdAt);
68 }
69
70 function pullRequestMatchesProject(
71 pullRequest: GitManager.GitBranchPullRequest,
72 project: OrchestrationProjectShell,
73 ): boolean {
74 return (
75 pullRequest.repositoryKey !== null &&
76 project.repositoryIdentity != null &&
77 sourceControlDefinitions.canonicalRepositoryKey(pullRequest.repositoryKey) ===
78 sourceControlDefinitions.canonicalRepositoryKey(project.repositoryIdentity.canonicalKey)
79 );
80 }
81
82 const resolveProjectForPullRequestDiscovery = Effect.fn(
83 "ThreadPullRequestServiceV2.resolveProject",
84 )(function* (
85 project: OrchestrationProjectShell,
86 repositoryIdentities: RepositoryIdentityResolver.RepositoryIdentityResolver["Service"],
87 options?: { readonly refresh?: boolean },
88 ) {
89 // Post-turn discovery refreshes 15-minute identity caches because agents may add remotes.
90 const repositoryIdentity = yield* repositoryIdentities.resolve(project.workspaceRoot, {
91 refresh: options?.refresh ?? false,
92 });
93 return {
94 project: { ...project, repositoryIdentity },
95 repository: sourceControlDefinitions.repositorySelector(repositoryIdentity),
96 };
97 });
98
99 export function projectWorkspaceMatchesSnapshot(
100 currentProject: Option.Option<Pick<OrchestrationProjectShell, "workspaceRoot">>,
101 expectedWorkspaceRoot: string,
102 ): boolean {
103 return (
104 Option.isSome(currentProject) && currentProject.value.workspaceRoot === expectedWorkspaceRoot
105 );
106 }
107
108 const BACKFILL_ATTEMPTS = 5;
109
110 interface RefreshRequest {
111 readonly threadId: ThreadId | null;
112 readonly refresh: boolean;
113 readonly backfill?: boolean;
114 }
115
116 export const make = Effect.gen(function* () {
117 const orchestrator = yield* Orchestrator.OrchestratorV2;
118 const projectStore = yield* ProjectStore.ProjectStoreV2;
119 const git = yield* GitManager.GitManager;
120 const pullRequests = yield* PullRequestService.PullRequestService;
121 const repositoryIdentities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
122 const random = yield* SecureRandom.SecureRandom;
123 const fileSystem = yield* FileSystem.FileSystem;
124 const path = yield* Path.Path;
125 const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
126 const pendingBackfill = new Map<ThreadId, number>();
127 const noPullRequestRechecks = makeNoPullRequestRechecks();
128
129 const finishBackfill = (threads: ReadonlyArray<Pick<OrchestrationV2ThreadShell, "id">>) => {
130 for (const thread of threads) pendingBackfill.delete(thread.id);
131 };
132 const failBackfill = (threads: ReadonlyArray<Pick<OrchestrationV2ThreadShell, "id">>) => {
133 for (const thread of threads) {
134 const remaining = pendingBackfill.get(thread.id);
135 if (remaining === undefined) continue;
136 if (remaining <= 1) pendingBackfill.delete(thread.id);
137 else pendingBackfill.set(thread.id, remaining - 1);
138 }
139 };
140
141 const readThreadSnapshot = ({ threadId, backfill }: RefreshRequest) =>
142 orchestrator.getPullRequestSnapshot(
143 threadId === null ? { unsettledOnly: !(backfill || pendingBackfill.size > 0) } : { threadId },
144 );
145
146 const synchronize = Effect.fn("ThreadPullRequestServiceV2.synchronize")(function* (
147 request: RefreshRequest,
148 ) {
149 const [threadSnapshot, projectShells] = yield* Effect.all([
150 readThreadSnapshot(request),
151 projectStore.listShells(),
152 ]);
153 // Subagent children refresh after their own runs; sweeping them costs a lookup each per minute.
154 const snapshotThreads =
155 request.threadId === null
156 ? threadSnapshot.threads.filter(
157 (thread) => thread.lineage.relationshipToParent !== "subagent",
158 )
159 : threadSnapshot.threads;
160 const projects = new Map(projectShells.map((project) => [project.id, project]));
161 const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
162 if (request.backfill) {
163 for (const thread of snapshotThreads) {
164 if (
165 (thread.settledOverride === "settled" || thread.settledAt !== null) &&
166 thread.branchPullRequest == null
167 ) {
168 pendingBackfill.set(thread.id, BACKFILL_ATTEMPTS);
169 }
170 }
171 }
172 const visibleThreadIds = new Set(
173 snapshotThreads.filter((thread) => thread.branch !== null).map((thread) => thread.id),
174 );
175 const checkedIds = request.threadId === null ? [...pendingBackfill.keys()] : [request.threadId];
176 for (const threadId of checkedIds) {
177 if (!visibleThreadIds.has(threadId)) pendingBackfill.delete(threadId);
178 }
179 const threads = snapshotThreads.filter(
180 (thread) =>
181 thread.archivedAt === null &&
182 ((thread.settledOverride !== "settled" && thread.settledAt === null) ||
183 request.threadId !== null ||
184 pendingBackfill.has(thread.id)) &&
185 (thread.branch !== null || thread.branchPullRequest != null),
186 );
187 const groups = Map.groupBy(threads, (thread) =>
188 JSON.stringify([thread.projectId, thread.worktreePath, thread.branch]),
189 );
190
191 // A thread's own events and a backfill read at once; only scheduled sweeps wait.
192 const scheduled = new Map(
193 request.threadId !== null
194 ? []
195 : [...groups].filter(
196 ([, group]) =>
197 group[0]!.branch !== null &&
198 group.every(
199 (thread) => thread.branchPullRequest == null && !pendingBackfill.has(thread.id),
200 ),
201 ),
202 );
203 const scheduledLookups = noPullRequestRechecks.select(
204 nowMs,
205 Array.from(scheduled, ([key, group]) => ({
206 key,
207 activityAtMs: Math.max(...group.map((thread) => DateTime.toEpochMillis(thread.updatedAt))),
208 })),
209 );
210
211 yield* Effect.forEach(
212 groups,
213 ([key, group]) =>
214 Effect.gen(function* () {
215 const first = group[0]!;
216 const project = projects.get(first.projectId);
217 if (project === undefined) return finishBackfill(group);
218 if (scheduled.has(key) && !scheduledLookups.has(key)) return;
219 const { project: resolvedProject, repository } =
220 yield* resolveProjectForPullRequestDiscovery(project, repositoryIdentities, {
221 refresh: request.refresh,
222 });
223 if (first.branch !== null && repository === null) return finishBackfill(group);
224 // A worktree folder can outlive its .git, and git there fails with not_a_repository.
225 // An unreadable .git keeps the worktree rather than failing the whole group.
226 const cwd =
227 first.worktreePath !== null &&
228 (yield* fileSystem
229 .exists(path.join(first.worktreePath, ".git"))
230 .pipe(Effect.orElseSucceed(() => true)))
231 ? first.worktreePath
232 : project.workspaceRoot;
233 // A detected PR is confirmed against fresh git state before it is saved.
234 const detected =
235 first.branch === null
236 ? null
237 : yield* git.branchPullRequest(
238 { cwd, branch: first.branch },
239 { refresh: request.refresh, confirmTerminal: false },
240 );
241 if (first.branch !== null) noPullRequestRechecks.record(key, nowMs, detected !== null);
242 if (detected !== null && !pullRequestMatchesProject(detected, resolvedProject)) {
243 return finishBackfill(group);
244 }
245 const detectedReference =
246 detected !== null && repository !== null
247 ? {
248 projectId: project.id,
249 repository,
250 number: detected.number,
251 url: detected.url,
252 }
253 : null;
254
255 // Only a new association is refused: a thread keeps a PR it already saved, and a PR the
256 // user checked out stays with the threads made for it, however long ago it closed.
257 const isHistoricalFor = (thread: (typeof group)[number]) =>
258 detected !== null &&
259 pullRequestPredatesThread(detected, thread.createdAt) &&
260 !samePullRequest(thread.branchPullRequest, detectedReference);
261 const dropsHistorical =
262 detected !== null &&
263 first.branch !== null &&
264 group.some(isHistoricalFor) &&
265 !(yield* git.checkedOutPullRequest({ cwd, branch: first.branch, url: detected.url }));
266
267 const plans = yield* Effect.forEach(group, (thread) =>
268 Effect.gen(function* () {
269 let branchPullRequest =
270 dropsHistorical && isHistoricalFor(thread) ? null : detectedReference;
271 if (
272 branchPullRequest === null &&
273 thread.branch !== null &&
274 thread.worktreePath === null &&
275 thread.branchPullRequest != null
276 ) {
277 const previous = yield* pullRequests.summary(thread.branchPullRequest, {
278 recoverTransientFailure: false,
279 });
280 if (
281 previous.headBranch === thread.branch &&
282 (previous.state === "merged" || previous.state === "closed")
283 ) {
284 branchPullRequest = thread.branchPullRequest;
285 }
286 }
287
288 let replacement: ThreadLinkedPullRequest | undefined;
289 if (
290 (thread.pullRequests ?? []).length === 0 &&
291 thread.linkedPullRequest != null &&
292 detected?.state === "open" &&
293 detectedReference !== null &&
294 !samePullRequest(thread.linkedPullRequest, detectedReference)
295 ) {
296 const linked = yield* pullRequests.summary(thread.linkedPullRequest, {
297 recoverTransientFailure: false,
298 });
299 if (linked.state === "merged" || linked.state === "closed") {
300 replacement = detectedReference;
301 }
302 }
303 if (
304 samePullRequest(thread.branchPullRequest, branchPullRequest) &&
305 replacement === undefined
306 ) {
307 pendingBackfill.delete(thread.id);
308 return null;
309 }
310 return { thread, branchPullRequest, replacement };
311 }).pipe(
312 Effect.catchCauseIf(
313 (cause) => !Cause.hasInterruptsOnly(cause),
314 (cause) =>
315 Effect.logWarning("thread pull request discovery failed", {
316 threadId: thread.id,
317 cause: Cause.pretty(cause),
318 }).pipe(
319 Effect.tap(() => Effect.sync(() => failBackfill([thread]))),
320 Effect.as(null),
321 ),
322 ),
323 ),
324 );
325 const updates = plans.filter((plan) => plan !== null);
326 if (updates.length === 0) return;
327
328 if (detected !== null && first.branch !== null) {
329 const current = yield* git.branchPullRequest(
330 { cwd, branch: first.branch },
331 { freshGitState: true },
332 );
333 const currentIdentity = yield* repositoryIdentities.resolve(project.workspaceRoot, {
334 refresh: true,
335 });
336 if (
337 current === null ||
338 current.number !== detected.number ||
339 current.url !== detected.url ||
340 current.state !== detected.state ||
341 current.repositoryKey !== detected.repositoryKey ||
342 !pullRequestMatchesProject(current, {
343 ...project,
344 repositoryIdentity: currentIdentity,
345 })
346 ) {
347 return failBackfill(updates.map((update) => update.thread));
348 }
349 }
350
351 yield* Effect.forEach(
352 updates,
353 ({ thread, branchPullRequest, replacement }) =>
354 Effect.gen(function* () {
355 // Discovery yields to network I/O; recheck the project at commit to reject deleted projects or changed checkouts.
356 const currentProject = yield* projectStore.getShell(project.id);
357 if (!projectWorkspaceMatchesSnapshot(currentProject, project.workspaceRoot)) {
358 return failBackfill([thread]);
359 }
360 const uuid = yield* random.uuidV4();
361 yield* orchestrator.dispatch({
362 type: "thread.pull-request.sync",
363 commandId: CommandId.make(`server:thread-pull-request:${thread.id}:${uuid}`),
364 threadId: thread.id,
365 projectId: project.id,
366 snapshotSequence: threadSnapshot.snapshotSequence,
367 expected: {
368 workspaceRoot: project.workspaceRoot,
369 branch: thread.branch,
370 worktreePath: thread.worktreePath,
371 linkedPullRequest: thread.linkedPullRequest ?? null,
372 branchPullRequest: thread.branchPullRequest ?? null,
373 },
374 branchPullRequest,
375 ...(replacement === undefined ? {} : { linkedPullRequest: replacement }),
376 });
377 pendingBackfill.delete(thread.id);
378 }).pipe(
379 Effect.catchCauseIf(
380 (cause) => !Cause.hasInterruptsOnly(cause),
381 (cause) =>
382 Effect.logWarning("thread pull request update failed", {
383 threadId: thread.id,
384 cause: Cause.pretty(cause),
385 }).pipe(Effect.tap(() => Effect.sync(() => failBackfill([thread])))),
386 ),
387 ),
388 { discard: true },
389 );
390 }).pipe(
391 Effect.catchCauseIf(
392 (cause) => !Cause.hasInterruptsOnly(cause),
393 (cause) =>
394 Effect.logWarning("thread branch pull request lookup failed", {
395 threadIds: group.map((thread) => thread.id),
396 cause: Cause.pretty(cause),
397 }).pipe(Effect.tap(() => Effect.sync(() => failBackfill(group)))),
398 ),
399 ),
400 // Batch concurrent branch lookups into one GitHub GraphQL document.
401 { concurrency: 32, discard: true },
402 );
403 if (request.threadId === null) noPullRequestRechecks.retain(groups);
404 });
405
406 // At most one queued request per thread, so a turn end's events share one lookup.
407 // A request after that lookup starts queues another, so later host changes are still read.
408 const queuedThreadRequests = new Map<ThreadId, { threadId: ThreadId; refresh: boolean }>();
409
410 const worker = yield* makeDrainableWorker((request: RefreshRequest) =>
411 Effect.suspend(() => {
412 if (request.threadId !== null) queuedThreadRequests.delete(request.threadId);
413 return synchronize(request);
414 }).pipe(
415 Effect.provideService(GitVcsDriver.BackgroundGitWork, true),
416 Effect.catchCauseIf(
417 (cause) => !Cause.hasInterruptsOnly(cause),
418 (cause) =>
419 Effect.logWarning("thread pull request refresh failed", {
420 cause: Cause.pretty(cause),
421 }),
422 ),
423 ),
424 );
425
426 const enqueueThread = (threadId: ThreadId, refresh: boolean) =>
427 Effect.suspend(() => {
428 const queued = queuedThreadRequests.get(threadId);
429 if (queued !== undefined) {
430 queued.refresh ||= refresh;
431 return Effect.void;
432 }
433 const request = { threadId, refresh };
434 queuedThreadRequests.set(threadId, request);
435 return worker.enqueue(request);
436 });
437
438 const linkedThreadsChange = makeLinkedThreadsChangeDetector();
439 // A running linked thread can reorder its answers on every event, so reorders refresh readers at
440 // most once per 10s, the cadence clients used to poll at.
441 const linkedThreadReorders = yield* Queue.dropping<void>(1);
442
443 const notifyLinkedThreads = (event: OrchestrationV2DomainEvent) => {
444 switch (linkedThreadsChange(event)) {
445 case "changed":
446 return pullRequests.notifyLinkedThreadsChanged;
447 case "reordered":
448 return Queue.offer(linkedThreadReorders, undefined).pipe(Effect.asVoid);
449 case null:
450 return Effect.void;
451 }
452 };
453
454 const processEvent = (event: OrchestrationV2DomainEvent) =>
455 Effect.andThen(notifyLinkedThreads(event), refreshForEvent(event));
456
457 const refreshForEvent = (event: OrchestrationV2DomainEvent) => {
458 switch (event.type) {
459 case "thread.created":
460 case "thread.unarchived":
461 case "thread.metadata-updated":
462 return enqueueThread(event.threadId, false);
463 case "thread.unsettled":
464 case "checkpoint.captured":
465 return enqueueThread(event.threadId, true);
466 case "run.updated":
467 if (
468 event.payload.status === "completed" ||
469 event.payload.status === "failed" ||
470 event.payload.status === "cancelled" ||
471 event.payload.status === "interrupted"
472 ) {
473 return enqueueThread(event.threadId, true);
474 }
475 break;
476 }
477 return Effect.void;
478 };
479
480 const start: ThreadPullRequestServiceV2["Service"]["start"] = Effect.fn(
481 "ThreadPullRequestServiceV2.start",
482 )(function* () {
483 yield* forkParked(Stream.runForEach(orchestrator.streamDomainEvents, processEvent));
484 yield* forkParked(
485 Queue.take(linkedThreadReorders).pipe(
486 Effect.andThen(pullRequests.notifyLinkedThreadsChanged),
487 Effect.andThen(Effect.sleep("10 seconds")),
488 Effect.forever,
489 ),
490 );
491 yield* forkParked(
492 Effect.gen(function* () {
493 yield* worker.enqueue({ threadId: null, refresh: false, backfill: true });
494 yield* worker.drain;
495 yield* Effect.gen(function* () {
496 // The sweep only catches pull requests opened outside a turn, which
497 // nobody sees without a client. Thread events still refresh headless work.
498 if (!(yield* backgroundPolicy.shouldRunOpportunisticWork)) return;
499 yield* worker.enqueue({ threadId: null, refresh: false });
500 yield* worker.drain;
501 }).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.delay("1 minute"));
502 }).pipe(Effect.asVoid),
503 );
504 });
505
506 return { start, drain: worker.drain } satisfies ThreadPullRequestServiceV2["Service"];
507 });