Skip to content

Commit bf01203

Browse files
committed
fix(server): match symlinked worktree paths during branch sync
1 parent c51a06e commit bf01203

5 files changed

Lines changed: 71 additions & 29 deletions

File tree

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import {
77
import * as Deferred from "effect/Deferred";
88
import * as Stream from "effect/Stream";
99
import * as Effect from "effect/Effect";
10+
import * as FileSystem from "effect/FileSystem";
1011
import * as Layer from "effect/Layer";
1112

1213
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
1314
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
15+
import * as ProjectionStore from "./ProjectionStore.ts";
1416
import * as Orchestrator from "./Orchestrator.ts";
1517
import * as ThreadManagement from "./ThreadManagementService.ts";
1618
import * as WorktreeBranchSync from "./WorktreeBranchSync.ts";
@@ -27,7 +29,7 @@ it.effect.each([false, true])(
2729
snapshotSequence: 0,
2830
threads: [
2931
thread("generated", "t3/unify-converter-branches", "/worktree"),
30-
thread("shared", "t3/bfc349ae", "/worktree"),
32+
thread("shared", "t3/bfc349ae", "/link/worktree"),
3133
thread("current", "union", "/worktree"),
3234
thread("root", "main", null),
3335
thread("other", "feature", "/other"),
@@ -50,25 +52,31 @@ it.effect.each([false, true])(
5052
}),
5153
[
5254
["generated", "union", "t3/unify-converter-branches", "/worktree"],
53-
["shared", "union", "t3/bfc349ae", "/worktree"],
55+
["shared", "union", "t3/bfc349ae", "/link/worktree"],
5456
["archived", "union", "t3/bfc349ae", "/worktree"],
5557
],
5658
);
5759
}).pipe(
5860
Effect.provide(
5961
Layer.mergeAll(
62+
FileSystem.layerNoop({
63+
realPath: (path) => Effect.succeed(path === "/link/worktree" ? "/worktree" : path),
64+
}),
65+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
66+
getThreadWorktreePaths: () =>
67+
Effect.succeed(
68+
[...snapshot.threads, ...snapshot.archivedThreads].flatMap((thread) =>
69+
thread.worktreePath === null
70+
? []
71+
: [{ threadId: thread.id, worktreePath: thread.worktreePath }],
72+
),
73+
),
74+
}),
6075
Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
61-
streamLocalChanges: Stream.fromIterable([null, "t3/bfc349ae", "union"]).pipe(
76+
streamBranchChanges: Stream.fromIterable([null, "t3/bfc349ae", "union"]).pipe(
6277
Stream.map((refName) => ({
6378
cwd: "/worktree",
64-
local: {
65-
isRepo: true,
66-
refName,
67-
isDefaultRef: false,
68-
hasPrimaryRemote: false,
69-
hasWorkingTreeChanges: false,
70-
workingTree: { files: [], insertions: 0, deletions: 0 },
71-
},
79+
refName,
7280
})),
7381
Stream.ensuring(Deferred.succeed(reconciled, undefined)),
7482
),

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
import { CommandId } from "@t3tools/contracts";
22
import { randomUuidV4 } from "@t3tools/provider-core/server/randomUuid";
33
import * as Effect from "effect/Effect";
4+
import * as FileSystem from "effect/FileSystem";
45
import * as Layer from "effect/Layer";
56
import * as Stream from "effect/Stream";
67

78
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
89
import { forkParked } from "../serverActivation.ts";
910
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
11+
import * as ProjectionStore from "./ProjectionStore.ts";
1012
import * as ThreadManagement from "./ThreadManagementService.ts";
1113

1214
const make = Effect.gen(function* () {
1315
const threads = yield* ThreadManagement.ThreadManagementService;
1416
const git = yield* GitWorkflowService.GitWorkflowService;
17+
const projections = yield* ProjectionStore.ProjectionStoreV2;
18+
const fs = yield* FileSystem.FileSystem;
1519
return Effect.fn("WorktreeBranchSync.reconcile")(function* (input: {
1620
readonly cwd: string;
1721
readonly refName: string | null;
1822
}) {
1923
if (input.refName === null) return;
20-
const snapshot = yield* threads.getShellSnapshot({ worktreePath: input.cwd });
24+
const paths = yield* projections.getThreadWorktreePaths({ includeArchived: true });
25+
const matchingPaths = new Set<string>();
26+
for (const worktreePath of new Set(paths.map((thread) => thread.worktreePath))) {
27+
const realPath =
28+
worktreePath === input.cwd
29+
? worktreePath
30+
: yield* fs.realPath(worktreePath).pipe(Effect.orElseSucceed(() => null));
31+
if (realPath === input.cwd) matchingPaths.add(worktreePath);
32+
}
33+
const matchingThreads = new Map(
34+
paths
35+
.filter((thread) => matchingPaths.has(thread.worktreePath))
36+
.map((thread) => [thread.threadId, thread.worktreePath]),
37+
);
38+
if (matchingThreads.size === 0) return;
39+
const snapshot = yield* threads.getShellSnapshot({ threadIds: [...matchingThreads.keys()] });
2140
const sharing = [...snapshot.threads, ...snapshot.archivedThreads].filter(
22-
(thread) => thread.worktreePath === input.cwd && thread.branch !== input.refName,
41+
(thread) =>
42+
thread.worktreePath === matchingThreads.get(thread.id) && thread.branch !== input.refName,
2343
);
2444
if (sharing.length === 0) return;
2545
// Status events may queue behind a generated rename or another checkout.
@@ -52,13 +72,10 @@ export const layer = Layer.effectDiscard(
5272
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
5373
const reconcile = yield* make;
5474
yield* forkParked(
55-
Stream.runForEach(broadcaster.streamLocalChanges, ({ cwd, local }) =>
56-
Effect.gen(function* () {
57-
if (!local.isRepo) return;
58-
yield* reconcile({ cwd, refName: local.refName });
59-
}).pipe(
75+
Stream.runForEach(broadcaster.streamBranchChanges, (input) =>
76+
reconcile(input).pipe(
6077
Effect.catchCause((cause) =>
61-
Effect.logWarning("Failed to reconcile worktree branch", { cwd, cause }),
78+
Effect.logWarning("Failed to reconcile worktree branch", { cwd: input.cwd, cause }),
6279
),
6380
),
6481
),

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ export const layer = Layer.mergeAll(
369369
);
370370

371371
export const layerProduction = Layer.mergeAll(
372-
WorktreeBranchSync.layer.pipe(Layer.provide(layerThreadManagementProvided)),
372+
WorktreeBranchSync.layer.pipe(
373+
Layer.provide(Layer.merge(layerThreadManagementProvided, ProjectionStore.layer)),
374+
),
373375
layer.pipe(Layer.provide(layerProjectService)),
374376
layerProjectService,
375377
layerManagedProjectFoldersProvided,

‎apps/server/src/vcs/VcsStatusBroadcaster.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ describe("VcsStatusBroadcaster", () => {
220220
return Effect.gen(function* () {
221221
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
222222
yield* broadcaster.getStatus({ cwd: "/repo" });
223+
const branchEvents = yield* broadcaster.streamBranchChanges.pipe(
224+
Stream.take(2),
225+
Stream.runCollect,
226+
Effect.forkScoped({ startImmediately: true }),
227+
);
223228
const subscribed = yield* Deferred.make<void>();
224229
const events = yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe(
225230
Stream.tap((event) =>
@@ -243,6 +248,8 @@ describe("VcsStatusBroadcaster", () => {
243248
{ _tag: "localUpdated", local: state.currentLocalStatus },
244249
]);
245250
assert.isNull((yield* broadcaster.getStatus({ cwd: "/repo" })).pr);
251+
state.currentLocalStatus = { ...state.currentLocalStatus, hasWorkingTreeChanges: true };
252+
yield* broadcaster.refreshLocalStatus("/repo");
246253
state.currentLocalStatus = { ...baseLocalStatus, refName: "union" };
247254
const snapshot = yield* broadcaster
248255
.streamStatus({ cwd: "/repo", includeRemote: false })
@@ -252,6 +259,10 @@ describe("VcsStatusBroadcaster", () => {
252259
local: state.currentLocalStatus,
253260
remote: null,
254261
});
262+
assert.deepStrictEqual(yield* Fiber.join(branchEvents), [
263+
{ cwd: "/repo", refName: "main" },
264+
{ cwd: "/repo", refName: "union" },
265+
]);
255266
}).pipe(Effect.provide(layerTestFor(state)));
256267
});
257268

‎apps/server/src/vcs/VcsStatusBroadcaster.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ function remoteRefreshFailureDelay(
205205
export class VcsStatusBroadcaster extends Context.Service<
206206
VcsStatusBroadcaster,
207207
{
208-
readonly streamLocalChanges: Stream.Stream<{
208+
readonly streamBranchChanges: Stream.Stream<{
209209
readonly cwd: string;
210-
readonly local: VcsStatusLocalResult;
210+
readonly refName: string | null;
211211
}>;
212212
readonly getStatus: (
213213
input: VcsStatusInput,
@@ -249,8 +249,8 @@ export const make = Effect.gen(function* () {
249249
PubSub.unbounded<VcsStatusChange>(),
250250
(pubsub) => PubSub.shutdown(pubsub),
251251
);
252-
const localChangesPubSub = yield* Effect.acquireRelease(
253-
PubSub.unbounded<{ readonly cwd: string; readonly local: VcsStatusLocalResult }>(),
252+
const branchChangesPubSub = yield* Effect.acquireRelease(
253+
PubSub.unbounded<{ readonly cwd: string; readonly refName: string | null }>(),
254254
(pubsub) => PubSub.shutdown(pubsub),
255255
);
256256
const broadcasterScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
@@ -274,16 +274,20 @@ export const make = Effect.gen(function* () {
274274
const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")(
275275
function* (cwd: string, local: VcsStatusLocalResult, options?: { publish?: boolean }) {
276276
const nextLocal = { fingerprint: fingerprintStatusPart(local), value: local };
277-
const [localChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => {
277+
const [localChanged, branchChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => {
278278
const previous = cache.get(cwd) ?? { local: null, remote: null };
279279
const remote = previous.remote?.branch === local.refName ? previous.remote : null;
280280
return [
281-
[previous.local?.fingerprint !== nextLocal.fingerprint, previous.remote !== remote],
281+
[
282+
previous.local?.fingerprint !== nextLocal.fingerprint,
283+
previous.local?.value.refName !== local.refName,
284+
previous.remote !== remote,
285+
],
282286
new Map(cache).set(cwd, { local: nextLocal, remote }),
283287
] as const;
284288
});
285-
if (localChanged) {
286-
yield* PubSub.publish(localChangesPubSub, { cwd, local });
289+
if (branchChanged && local.isRepo) {
290+
yield* PubSub.publish(branchChangesPubSub, { cwd, refName: local.refName });
287291
}
288292
if (options?.publish && remoteCleared) {
289293
yield* PubSub.publish(changesPubSub, {
@@ -765,7 +769,7 @@ export const make = Effect.gen(function* () {
765769
);
766770

767771
return VcsStatusBroadcaster.of({
768-
streamLocalChanges: Stream.fromPubSub(localChangesPubSub),
772+
streamBranchChanges: Stream.fromPubSub(branchChangesPubSub),
769773
getStatus,
770774
refreshLocalStatus,
771775
refreshStatus,

0 commit comments

Comments
 (0)