@devries-cameron20 / t3code-1 / commits / 76e4cb5c8a
Fork 0

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

fix(server): removing a worktree on Windows no longer spins the server until it hangs (#34774)

Dara Adedeji committed Nov 12, 2026 at 13:17 UTC 76e4cb5c8af631c920bad9202436348bf4d5b50f
2 files changed +52 -1
apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+44
@@ -1527,6 +1527,50 @@ describe("VcsStatusBroadcaster", () => {
1527 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
1528 );
1529
1530 + it.effect("a HEAD watch ends when its git directory is removed", () =>
1531 + Effect.gen(function* () {
1532 + const gitDir = "/repo/.git/worktrees/feature";
1533 + const events = yield* Queue.unbounded<FileSystem.WatchEvent>();
1534 + const closed = yield* Deferred.make<void>();
1535 + let gitDirExists = true;
1536 + const state: Parameters<typeof layerTestFor>[0] = {
1537 + currentLocalStatus: { ...baseLocalStatus, refName: "main" },
1538 + currentRemoteStatus: null,
1539 + localStatusCalls: 0,
1540 + remoteStatusCalls: 0,
1541 + localInvalidationCalls: 0,
1542 + remoteInvalidationCalls: 0,
1543 + headPath: `${gitDir}/HEAD`,
1544 + fileSystem: FileSystem.makeNoop({
1545 + realPath: (target) => Effect.succeed(target),
1546 + exists: (target) => Effect.succeed(target !== gitDir || gitDirExists),
1547 + watch: (_target, options) =>
1548 + Stream.fromQueue(events).pipe(
1549 + Stream.onStart(Effect.sync(() => options?.onReady?.())),
1550 + Stream.ensuring(Deferred.succeed(closed, undefined)),
1551 + ),
1552 + }),
1553 + };
1554 +
1555 + yield* Effect.gen(function* () {
1556 + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
1557 + const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" }));
1558 + assert.deepEqual(yield* branches, [{ refName: "main" }]);
1559 +
1560 + // Every checkout removes HEAD.lock, which leaves the watch running.
1561 + state.currentLocalStatus = { ...baseLocalStatus, refName: "other" };
1562 + yield* Queue.offer(events, { _tag: "Remove", path: "HEAD.lock" });
1563 + yield* Queue.offer(events, { _tag: "Update", path: "HEAD" });
1564 + assert.deepEqual(yield* branches, [{ refName: "other" }]);
1565 +
1566 + // `git worktree remove` deletes the watched directory; Windows keeps reporting it.
1567 + gitDirExists = false;
1568 + yield* Queue.offer(events, { _tag: "Remove", path: gitDir });
1569 + yield* Deferred.await(closed);
1570 + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
1571 + }),
1572 + );
1573 +
1574 it.effect("releases remote demand while passive observers remain", () => {
1575 const state = {
1576 currentLocalStatus: baseLocalStatus,
apps/server/src/vcs/VcsStatusBroadcaster.ts
+8 -1
@@ -822,12 +822,19 @@ export const make = Effect.gen(function* () {
822 const watchHead = Effect.fnUntraced(function* (cwd: string, ready: Deferred.Deferred<void>) {
823 const headPath = yield* workflow.headPath(cwd);
824 if (headPath === null) return;
825 + const headDirectory = path.dirname(headPath);
826 // Git replaces HEAD by renaming HEAD.lock, which a watch on the file itself would lose.
827 yield* fs
827 - .watch(path.dirname(headPath), {
828 + .watch(headDirectory, {
829 onReady: () => Deferred.doneUnsafe(ready, Exit.void),
830 })
831 .pipe(
832 + // On Windows a watch outlives its deleted directory and reports it ~150k times a second.
833 + Stream.takeUntilEffect((event) =>
834 + event._tag === "Remove"
835 + ? fs.exists(headDirectory).pipe(Effect.map((exists) => !exists))
836 + : Effect.succeed(false),
837 + ),
838 Stream.filter((event) => path.basename(event.path) === "HEAD"),
839 // A rebase moves HEAD many times; refresh once for whatever arrived during a refresh.
840 Stream.buffer({ capacity: 1, strategy: "sliding" }),