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

main
ts 90 lines 3.58 KB
Raw
1 import { CommandId } from "@t3tools/contracts";
2 import { randomUuidV4 } from "@t3tools/provider-core/server/randomUuid";
3 import * as KeyedLock from "@t3tools/shared/KeyedLock";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Layer from "effect/Layer";
7 import * as Stream from "effect/Stream";
8
9 import * as GitWorkflowService from "../git/GitWorkflowService.ts";
10 import { forkParked } from "../serverActivation.ts";
11 import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
12 import * as ProjectionStore from "./ProjectionStore.ts";
13 import * as ThreadManagement from "./ThreadManagementService.ts";
14
15 export const makeReconcile = Effect.gen(function* () {
16 const threads = yield* ThreadManagement.ThreadManagementService;
17 const git = yield* GitWorkflowService.GitWorkflowService;
18 const projections = yield* ProjectionStore.ProjectionStoreV2;
19 const fs = yield* FileSystem.FileSystem;
20 return Effect.fn("WorktreeBranchSync.reconcile")(function* (input: {
21 readonly cwd: string;
22 readonly refName: string | null;
23 }) {
24 if (input.refName === null) return;
25 const paths = yield* projections.getThreadWorktreePaths({ includeArchived: true });
26 const matchingPaths = new Set<string>();
27 for (const worktreePath of new Set(paths.map((thread) => thread.worktreePath))) {
28 const realPath =
29 worktreePath === input.cwd
30 ? worktreePath
31 : yield* fs.realPath(worktreePath).pipe(Effect.orElseSucceed(() => null));
32 if (realPath === input.cwd) matchingPaths.add(worktreePath);
33 }
34 const matchingThreads = new Map(
35 paths
36 .filter((thread) => matchingPaths.has(thread.worktreePath))
37 .map((thread) => [thread.threadId, thread.worktreePath]),
38 );
39 if (matchingThreads.size === 0) return;
40 const snapshot = yield* threads.getShellSnapshot({ threadIds: [...matchingThreads.keys()] });
41 const sharing = [...snapshot.threads, ...snapshot.archivedThreads].filter(
42 (thread) =>
43 thread.worktreePath === matchingThreads.get(thread.id) && thread.branch !== input.refName,
44 );
45 if (sharing.length === 0) return;
46 // Status events may queue behind a generated rename or another checkout.
47 if ((yield* git.currentBranch(input.cwd)) !== input.refName) return;
48 for (const thread of sharing) {
49 yield* threads
50 .dispatch({
51 type: "thread.metadata.update",
52 commandId: CommandId.make(yield* randomUuidV4),
53 threadId: thread.id,
54 branch: input.refName,
55 expectedBranch: thread.branch,
56 expectedWorktreePath: thread.worktreePath,
57 })
58 .pipe(
59 Effect.catchTags({
60 OrchestratorDispatchError: (cause) =>
61 Effect.logWarning("Worktree branch update was rejected", {
62 threadId: thread.id,
63 cause,
64 }),
65 }),
66 );
67 }
68 });
69 });
70
71 export const layer = Layer.effectDiscard(
72 Effect.gen(function* () {
73 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
74 const reconcile = yield* makeReconcile;
75 // Each update waits on its thread's lock, so a busy thread delays only its own worktree.
76 const worktrees = yield* KeyedLock.makeRunner<string>();
77 yield* forkParked(
78 Stream.runForEach(broadcaster.streamBranchChanges, (input) =>
79 worktrees.run(
80 input.cwd,
81 reconcile(input).pipe(
82 Effect.catchCause((cause) =>
83 Effect.logWarning("Failed to reconcile worktree branch", { cwd: input.cwd, cause }),
84 ),
85 ),
86 ),
87 ),
88 );
89 }),
90 );