TypeScript
fix(server): threads follow their worktree's branch changes (#31599)
Yash Singh02cb2fc4653f
diff --git a/apps/server/src/orchestration-v2/WorktreeBranchSync.test.ts b/apps/server/src/orchestration-v2/WorktreeBranchSync.test.tsnew file mode 100644index 00000000000..bb95778860e--- /dev/null+++ b/apps/server/src/orchestration-v2/WorktreeBranchSync.test.ts@@ -0,0 +1,111 @@+import { assert, it } from "@effect/vitest";+import {+ ThreadId,+ type OrchestrationV2ThreadShell,+ type OrchestrationV2ServerCommand,+} from "@t3tools/contracts";+import * as Deferred from "effect/Deferred";+import * as Stream from "effect/Stream";+import * as Effect from "effect/Effect";+import * as FileSystem from "effect/FileSystem";+import * as Layer from "effect/Layer";++import * as GitWorkflowService from "../git/GitWorkflowService.ts";+import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";+import * as ProjectionStore from "./ProjectionStore.ts";+import * as Orchestrator from "./Orchestrator.ts";+import * as ThreadManagement from "./ThreadManagementService.ts";+import * as WorktreeBranchSync from "./WorktreeBranchSync.ts";++it.effect.each([false, true])(+ "reconciles shared worktree branches without changing root checkouts or detached HEADs, conflict=%s",+ (conflict) => {+ const reconciled = Deferred.makeUnsafe<void>();+ const commands: OrchestrationV2ServerCommand[] = [];+ const thread = (id: string, branch: string, worktreePath: string | null) =>+ ({ id: ThreadId.make(id), branch, worktreePath }) as OrchestrationV2ThreadShell;+ const snapshot = {+ schemaVersion: 2,+ snapshotSequence: 0,+ threads: [+ thread("generated", "t3/unify-converter-branches", "/worktree"),+ thread("shared", "t3/bfc349ae", "/link/worktree"),+ thread("current", "union", "/worktree"),+ thread("root", "main", null),+ thread("other", "feature", "/other"),+ ],+ archivedThreads: [thread("archived", "t3/bfc349ae", "/worktree")],+ };+ return Effect.gen(function* () {+ yield* Layer.build(WorktreeBranchSync.layer);+ yield* Deferred.await(reconciled);+ assert.deepEqual(+ commands.map((command) => {+ assert.equal(command.type, "thread.metadata.update");+ if (command.type !== "thread.metadata.update") throw new Error("Unexpected command");+ return [+ command.threadId,+ command.branch,+ command.expectedBranch,+ command.expectedWorktreePath,+ ];+ }),+ [+ ["generated", "union", "t3/unify-converter-branches", "/worktree"],+ ["shared", "union", "t3/bfc349ae", "/link/worktree"],+ ["archived", "union", "t3/bfc349ae", "/worktree"],+ ],+ );+ }).pipe(+ Effect.provide(+ Layer.mergeAll(+ FileSystem.layerNoop({+ realPath: (path) => Effect.succeed(path === "/link/worktree" ? "/worktree" : path),+ }),+ Layer.mock(ProjectionStore.ProjectionStoreV2)({+ getThreadWorktreePaths: () =>+ Effect.succeed(+ [...snapshot.threads, ...snapshot.archivedThreads].flatMap((thread) =>+ thread.worktreePath === null+ ? []+ : [{ threadId: thread.id, worktreePath: thread.worktreePath }],+ ),+ ),+ }),+ Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({+ streamBranchChanges: Stream.fromIterable([null, "t3/bfc349ae", "union"]).pipe(+ Stream.map((refName) => ({+ cwd: "/worktree",+ refName,+ })),+ Stream.ensuring(Deferred.succeed(reconciled, undefined)),+ ),+ }),+ Layer.mock(GitWorkflowService.GitWorkflowService)({+ currentBranch: () => Effect.succeed("union"),+ }),+ Layer.mock(ThreadManagement.ThreadManagementService)({+ getShellSnapshot: () => Effect.succeed(snapshot),+ dispatch: (command) =>+ Effect.gen(function* () {+ commands.push(command);+ if (+ command.type === "thread.metadata.update" &&+ command.threadId === ThreadId.make("generated") &&+ conflict+ ) {+ return yield* new Orchestrator.OrchestratorDispatchError({+ commandId: command.commandId,+ commandType: command.type,+ cause: "Thread moved before branch metadata could be applied.",+ });+ }+ return { sequence: commands.length, storedEvents: [] };+ }),+ }),+ ),+ ),+ Effect.scoped,+ );+ },+);diff --git a/apps/server/src/orchestration-v2/WorktreeBranchSync.ts b/apps/server/src/orchestration-v2/WorktreeBranchSync.tsnew file mode 100644index 00000000000..d821e192dca--- /dev/null+++ b/apps/server/src/orchestration-v2/WorktreeBranchSync.ts@@ -0,0 +1,84 @@+import { CommandId } from "@t3tools/contracts";+import { randomUuidV4 } from "@t3tools/provider-core/server/randomUuid";+import * as Effect from "effect/Effect";+import * as FileSystem from "effect/FileSystem";+import * as Layer from "effect/Layer";+import * as Stream from "effect/Stream";++import * as GitWorkflowService from "../git/GitWorkflowService.ts";+import { forkParked } from "../serverActivation.ts";+import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";+import * as ProjectionStore from "./ProjectionStore.ts";+import * as ThreadManagement from "./ThreadManagementService.ts";++const make = Effect.gen(function* () {+ const threads = yield* ThreadManagement.ThreadManagementService;+ const git = yield* GitWorkflowService.GitWorkflowService;+ const projections = yield* ProjectionStore.ProjectionStoreV2;+ const fs = yield* FileSystem.FileSystem;+ return Effect.fn("WorktreeBranchSync.reconcile")(function* (input: {+ readonly cwd: string;+ readonly refName: string | null;+ }) {+ if (input.refName === null) return;+ const paths = yield* projections.getThreadWorktreePaths({ includeArchived: true });+ const matchingPaths = new Set<string>();+ for (const worktreePath of new Set(paths.map((thread) => thread.worktreePath))) {+ const realPath =+ worktreePath === input.cwd+ ? worktreePath+ : yield* fs.realPath(worktreePath).pipe(Effect.orElseSucceed(() => null));+ if (realPath === input.cwd) matchingPaths.add(worktreePath);+ }+ const matchingThreads = new Map(+ paths+ .filter((thread) => matchingPaths.has(thread.worktreePath))+ .map((thread) => [thread.threadId, thread.worktreePath]),+ );+ if (matchingThreads.size === 0) return;+ const snapshot = yield* threads.getShellSnapshot({ threadIds: [...matchingThreads.keys()] });+ const sharing = [...snapshot.threads, ...snapshot.archivedThreads].filter(+ (thread) =>+ thread.worktreePath === matchingThreads.get(thread.id) && thread.branch !== input.refName,+ );+ if (sharing.length === 0) return;+ // Status events may queue behind a generated rename or another checkout.+ if ((yield* git.currentBranch(input.cwd)) !== input.refName) return;+ for (const thread of sharing) {+ yield* threads+ .dispatch({+ type: "thread.metadata.update",+ commandId: CommandId.make(yield* randomUuidV4),+ threadId: thread.id,+ branch: input.refName,+ expectedBranch: thread.branch,+ expectedWorktreePath: thread.worktreePath,+ })+ .pipe(+ Effect.catchTags({+ OrchestratorDispatchError: (cause) =>+ Effect.logWarning("Worktree branch update was rejected", {+ threadId: thread.id,+ cause,+ }),+ }),+ );+ }+ });+});++export const layer = Layer.effectDiscard(+ Effect.gen(function* () {+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;+ const reconcile = yield* make;+ yield* forkParked(+ Stream.runForEach(broadcaster.streamBranchChanges, (input) =>+ reconcile(input).pipe(+ Effect.catchCause((cause) =>+ Effect.logWarning("Failed to reconcile worktree branch", { cwd: input.cwd, cause }),+ ),+ ),+ ),+ );+ }),+);diff --git a/apps/server/src/orchestration-v2/runtimeLayer.ts b/apps/server/src/orchestration-v2/runtimeLayer.tsindex 1b546275953..ba1dadad186 100644--- a/apps/server/src/orchestration-v2/runtimeLayer.ts+++ b/apps/server/src/orchestration-v2/runtimeLayer.ts@@ -1,3 +1,4 @@+import * as WorktreeBranchSync from "./WorktreeBranchSync.ts";import * as GuestShareService from "../guest/GuestShareService.ts";import * as UsageLimitRecoveryWorker from "./UsageLimitRecoveryWorker.ts";import * as Scheduler from "../scheduling/Scheduler.ts";@@ -368,6 +369,9 @@ export const layer = Layer.mergeAll();export const layerProduction = Layer.mergeAll(+ WorktreeBranchSync.layer.pipe(+ Layer.provide(Layer.merge(layerThreadManagementProvided, ProjectionStore.layer)),+ ),layer.pipe(Layer.provide(layerProjectService)),layerProjectService,layerManagedProjectFoldersProvided,diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.tsindex 45b130f9a2b..60f39b592d6 100644--- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts+++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts@@ -207,8 +207,8 @@ describe("VcsStatusBroadcaster", () => {}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);});- it.effect("publishes a terminal branch switch without reading remote status", () => {- const state = {+ it.effect("publishes terminal branch switches and validates cached status reads", () => {+ const state: Parameters<typeof layerTestFor>[0] = {currentLocalStatus: baseLocalStatus,currentRemoteStatus: remoteStatusWithPr,localStatusCalls: 0,@@ -220,6 +220,11 @@ describe("VcsStatusBroadcaster", () => {return Effect.gen(function* () {const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;yield* broadcaster.getStatus({ cwd: "/repo" });+ const branchEvents = yield* broadcaster.streamBranchChanges.pipe(+ Stream.take(2),+ Stream.runCollect,+ Effect.forkScoped({ startImmediately: true }),+ );const subscribed = yield* Deferred.make<void>();const events = yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe(Stream.tap((event) =>@@ -243,6 +248,13 @@ describe("VcsStatusBroadcaster", () => {{ _tag: "localUpdated", local: state.currentLocalStatus },]);assert.isNull((yield* broadcaster.getStatus({ cwd: "/repo" })).pr);+ state.currentLocalStatus = { ...state.currentLocalStatus, hasWorkingTreeChanges: true };+ yield* broadcaster.refreshLocalStatus("/repo");+ state.currentLocalStatus = { ...baseLocalStatus, refName: "union" };+ state.currentRemoteStatus = null;+ const queried = yield* broadcaster.getStatus({ cwd: "/repo" });+ assert.equal(queried.refName, "union");+ assert.isNull(queried.pr);const snapshot = yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe(Stream.take(1), Stream.runCollect);@@ -251,6 +263,10 @@ describe("VcsStatusBroadcaster", () => {local: state.currentLocalStatus,remote: null,});+ assert.deepStrictEqual(yield* Fiber.join(branchEvents), [+ { cwd: "/repo", refName: "main" },+ { cwd: "/repo", refName: "union" },+ ]);}).pipe(Effect.provide(layerTestFor(state)));});diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.tsindex 7fee5379e4a..3921077ebbb 100644--- a/apps/server/src/vcs/VcsStatusBroadcaster.ts+++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts@@ -205,6 +205,10 @@ function remoteRefreshFailureDelay(export class VcsStatusBroadcaster extends Context.Service<VcsStatusBroadcaster,{+ readonly streamBranchChanges: Stream.Stream<{+ readonly cwd: string;+ readonly refName: string | null;+ }>;readonly getStatus: (input: VcsStatusInput,) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;@@ -245,6 +249,10 @@ export const make = Effect.gen(function* () {PubSub.unbounded<VcsStatusChange>(),(pubsub) => PubSub.shutdown(pubsub),);+ const branchChangesPubSub = yield* Effect.acquireRelease(+ PubSub.unbounded<{ readonly cwd: string; readonly refName: string | null }>(),+ (pubsub) => PubSub.shutdown(pubsub),+ );const broadcasterScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>Scope.close(scope, Exit.void),);@@ -266,14 +274,21 @@ export const make = Effect.gen(function* () {const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")(function* (cwd: string, local: VcsStatusLocalResult, options?: { publish?: boolean }) {const nextLocal = { fingerprint: fingerprintStatusPart(local), value: local };- const [localChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => {+ const [localChanged, branchChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => {const previous = cache.get(cwd) ?? { local: null, remote: null };const remote = previous.remote?.branch === local.refName ? previous.remote : null;return [- [previous.local?.fingerprint !== nextLocal.fingerprint, previous.remote !== remote],+ [+ previous.local?.fingerprint !== nextLocal.fingerprint,+ previous.local?.value.refName !== local.refName,+ previous.remote !== remote,+ ],new Map(cache).set(cwd, { local: nextLocal, remote }),] as const;});+ if (branchChanged && local.isRepo) {+ yield* PubSub.publish(branchChangesPubSub, { cwd, refName: local.refName });+ }if (options?.publish && remoteCleared) {yield* PubSub.publish(changesPubSub, {cwd,@@ -322,7 +337,9 @@ export const make = Effect.gen(function* () {) {const cached = yield* getCachedStatus(cwd);if (cached?.local) {- return cached.local.value;+ const branch = yield* workflow.currentBranch(cwd);+ if (branch === cached.local.value.refName) return cached.local.value;+ return yield* refreshLocalStatusCore(cwd);}return yield* loadLocalStatus(cwd);});@@ -341,11 +358,11 @@ export const make = Effect.gen(function* () {"VcsStatusBroadcaster.getStatus",)(function* (input) {const cwd = yield* withFileSystem(normalizeCwd(input.cwd));+ yield* getOrLoadLocalStatus(cwd);const cached = yield* getCachedStatus(cwd);if (cached?.local && cached.remote) {return mergeGitStatusParts(cached.local.value, cached.remote.value);}- yield* getOrLoadLocalStatus(cwd);return yield* withRemoteWriteLock(cwd,Effect.gen(function* () {@@ -752,6 +769,7 @@ export const make = Effect.gen(function* () {);return VcsStatusBroadcaster.of({+ streamBranchChanges: Stream.fromPubSub(branchChangesPubSub),getStatus,refreshLocalStatus,refreshStatus,