@devries-cameron20 / t3code-1 / commits / a3477a5dd9
Fork 0

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

fix(server): branch refresh no longer waits for detailed Git status (#36072)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

MINIlaolao committed Nov 14, 2026 at 16:07 UTC a3477a5dd93b34b4508784382ffad5ae698c6fde
2 files changed +209 -49
apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+117
@@ -349,6 +349,80 @@ describe("VcsStatusBroadcaster", () => {
349 },
350 );
351
352 + it.effect("publishes the new branch before pending detailed status completes", () => {
353 + const state = newState();
354 + return Effect.gen(function* () {
355 + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
356 + const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" }));
357 + yield* branches;
358 + const reading = yield* Deferred.make<void>();
359 + const resume = yield* Deferred.make<void>();
360 + state.beforeLocalStatus = Deferred.succeed(reading, undefined).pipe(
361 + Effect.andThen(Deferred.await(resume)),
362 + );
363 + const details = yield* Stream.runHead(
364 + broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }),
365 + ).pipe(Effect.forkScoped);
366 + yield* Deferred.await(reading);
367 + state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next" };
368 + const update = yield* branches.pipe(Effect.forkScoped({ startImmediately: true }));
369 + yield* broadcaster.refreshBranchStatus({ cwd: "/repo" });
370 + assert.deepEqual(
371 + update.pollUnsafe(),
372 + Exit.succeed([{ refName: "feature/next", isRepo: true }]),
373 + );
374 + state.beforeLocalStatus = Effect.void;
375 + yield* Deferred.succeed(resume, undefined);
376 + yield* Fiber.join(details);
377 + yield* broadcaster.getStatus({ cwd: "/repo" });
378 + state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/latest" };
379 + yield* broadcaster.refreshBranchStatus({ cwd: "/repo" });
380 + assert.deepEqual(yield* branches, [{ refName: "feature/latest", isRepo: true }]);
381 + }).pipe(Effect.provide(layerTestFor(state)));
382 + });
383 +
384 + it.effect("invalidates totals for a refresh arriving after detailed demand closes", () => {
385 + const state = newState();
386 + return Effect.gen(function* () {
387 + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
388 + const detailScope = yield* Scope.make();
389 + const details = yield* Stream.toPull(
390 + broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }),
391 + ).pipe(Scope.provide(detailScope));
392 + yield* details;
393 + const reading = yield* Deferred.make<void>();
394 + const resume = yield* Deferred.make<void>();
395 + state.currentLocalStatus = {
396 + ...baseLocalStatus,
397 + refName: "feature/next",
398 + workingTree: { files: [], insertions: 1, deletions: 0 },
399 + };
400 + state.beforeLocalStatus = Deferred.succeed(reading, undefined).pipe(
401 + Effect.andThen(Deferred.await(resume)),
402 + );
403 + yield* broadcaster.refreshBranchStatus({ cwd: "/repo" });
404 + yield* Deferred.await(reading);
405 + yield* Scope.close(detailScope, Exit.void);
406 + state.currentLocalStatus = {
407 + ...state.currentLocalStatus,
408 + workingTree: { files: [], insertions: 2, deletions: 0 },
409 + };
410 + const trailing = yield* broadcaster
411 + .refreshBranchStatus({ cwd: "/repo" })
412 + .pipe(Effect.forkScoped({ startImmediately: true }));
413 + state.beforeLocalStatus = Effect.void;
414 + yield* Deferred.succeed(resume, undefined);
415 + yield* Fiber.join(trailing);
416 + const reopened = yield* Stream.runHead(
417 + broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }),
418 + );
419 + assert.deepEqual(
420 + reopened,
421 + Option.some({ _tag: "snapshot" as const, local: state.currentLocalStatus, remote: null }),
422 + );
423 + }).pipe(Effect.provide(layerTestFor(state)));
424 + });
425 +
426 it.effect("upgrades an in-flight branch refresh when detailed demand arrives", () => {
427 const state: Parameters<typeof layerTestFor>[0] = {
428 currentLocalStatus: baseLocalStatus,
@@ -1419,6 +1493,49 @@ describe("VcsStatusBroadcaster", () => {
1493 }).pipe(Effect.provide(NodeServices.layer)),
1494 );
1495
1496 + it.effect("a terminal checkout updates the branch before pending detailed status completes", () =>
1497 + Effect.gen(function* () {
1498 + const fs = yield* FileSystem.FileSystem;
1499 + const path = yield* Path.Path;
1500 + const gitDir = yield* fs.makeTempDirectoryScoped({ prefix: "vcs-head-watch-" });
1501 + const headPath = path.join(gitDir, "HEAD");
1502 + yield* fs.writeFileString(headPath, "ref: refs/heads/main\n");
1503 + const state: Parameters<typeof layerTestFor>[0] = {
1504 + currentLocalStatus: { ...baseLocalStatus, refName: "main" },
1505 + currentRemoteStatus: null,
1506 + localStatusCalls: 0,
1507 + remoteStatusCalls: 0,
1508 + localInvalidationCalls: 0,
1509 + remoteInvalidationCalls: 0,
1510 + headPath,
1511 + };
1512 +
1513 + yield* Effect.gen(function* () {
1514 + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
1515 + const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" }));
1516 + yield* branches;
1517 + const details = yield* Stream.toPull(
1518 + broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }),
1519 + );
1520 + yield* details;
1521 +
1522 + const resume = yield* Deferred.make<void>();
1523 + state.beforeLocalStatus = Deferred.await(resume);
1524 + state.currentLocalStatus = { ...baseLocalStatus, refName: "repro" };
1525 + const lockPath = path.join(gitDir, "HEAD.lock");
1526 + yield* fs.writeFileString(lockPath, "ref: refs/heads/repro\n");
1527 + yield* fs.rename(lockPath, headPath);
1528 +
1529 + assert.deepEqual(yield* branches, [{ refName: "repro", isRepo: true }]);
1530 + state.currentLocalStatus = { ...baseLocalStatus, refName: "second" };
1531 + yield* fs.writeFileString(lockPath, "ref: refs/heads/second\n");
1532 + yield* fs.rename(lockPath, headPath);
1533 + assert.deepEqual(yield* branches, [{ refName: "second", isRepo: true }]);
1534 + yield* Deferred.succeed(resume, undefined);
1535 + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
1536 + }).pipe(Effect.provide(NodeServices.layer)),
1537 + );
1538 +
1539 it.effect("a checkout read that fails while index.lock is held retries until it succeeds", () =>
1540 Effect.gen(function* () {
1541 const events = yield* Queue.unbounded<FileSystem.WatchEvent>();
apps/server/src/vcs/VcsStatusBroadcaster.ts
+92 -49
@@ -286,17 +286,29 @@ export const make = Effect.gen(function* () {
286 const detailedSubscribers = new Map<string, number>();
287 const branches = new Map<
288 string,
289 - { readonly refName: string | null; readonly isRepo: boolean; readonly revision: number }
289 + {
290 + readonly refName: string | null;
291 + readonly isRepo: boolean;
292 + readonly revision: number;
293 + readonly observation: number;
294 + }
295 >();
296 let branchRevision = 0;
297 + // A slow detailed read must not replace a newer HEAD-only observation.
298 + let branchObservation = 0;
299 const publishBranch = Effect.fn("VcsStatusBroadcaster.publishBranch")(function* (
300 cwd: string,
301 { refName, isRepo }: { readonly refName: string | null; readonly isRepo: boolean },
302 + observation: number,
303 ) {
304 const previous = branches.get(cwd);
297 - if (previous?.refName === refName && previous.isRepo === isRepo) return;
305 + if (previous && previous.observation > observation) return;
306 + if (previous?.refName === refName && previous.isRepo === isRepo) {
307 + branches.set(cwd, { ...previous, observation });
308 + return;
309 + }
310 const revision = ++branchRevision;
299 - branches.set(cwd, { refName, isRepo, revision });
311 + branches.set(cwd, { refName, isRepo, revision, observation });
312 yield* PubSub.publish(branchChangesPubSub, { cwd, refName, isRepo, revision });
313 });
314 const localRefreshLocks = yield* KeyedLock.make<string>();
@@ -317,7 +329,12 @@ export const make = Effect.gen(function* () {
329 });
330
331 const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")(
320 - function* (cwd: string, local: VcsStatusLocalResult, options?: { publish?: boolean }) {
332 + function* (
333 + cwd: string,
334 + local: VcsStatusLocalResult,
335 + observation: number,
336 + options?: { publish?: boolean },
337 + ) {
338 const nextLocal = { fingerprint: fingerprintStatusPart(local), value: local };
339 const [localChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => {
340 const previous = cache.get(cwd) ?? { local: null, remote: null };
@@ -327,7 +344,7 @@ export const make = Effect.gen(function* () {
344 new Map(cache).set(cwd, { local: nextLocal, remote }),
345 ] as const;
346 });
330 - yield* publishBranch(cwd, local);
347 + yield* publishBranch(cwd, local, observation);
348 if (options?.publish && remoteCleared) {
349 yield* PubSub.publish(changesPubSub, {
350 cwd,
@@ -366,9 +383,11 @@ export const make = Effect.gen(function* () {
383
384 const loadLocalStatus = Effect.fn("VcsStatusBroadcaster.loadLocalStatus")(function* (
385 cwd: string,
386 + options?: { publish?: boolean },
387 ) {
388 + const observation = ++branchObservation;
389 const local = yield* workflow.localStatus({ cwd });
371 - return yield* updateCachedLocalStatus(cwd, local);
390 + return yield* updateCachedLocalStatus(cwd, local, observation, options);
391 });
392
393 const getOrLoadLocalStatusUnlocked = Effect.fn("VcsStatusBroadcaster.getOrLoadLocalStatus")(
@@ -378,9 +397,7 @@ export const make = Effect.gen(function* () {
397 const branch = yield* workflow.currentBranch(cwd);
398 if (branch === cached.local.value.refName) return cached.local.value;
399 yield* workflow.invalidateLocalStatus(cwd);
381 - return yield* updateCachedLocalStatus(cwd, yield* workflow.localStatus({ cwd }), {
382 - publish: true,
383 - });
400 + return yield* loadLocalStatus(cwd, { publish: true });
401 }
402 return yield* loadLocalStatus(cwd);
403 },
@@ -439,9 +456,10 @@ export const make = Effect.gen(function* () {
456 if (shared !== undefined && shared.startedAt >= requestedAt) return shared.local;
457 const startedAt = localRefreshRequests.get(cwd) ?? requestedAt;
458 yield* workflow.invalidateLocalStatus(cwd);
459 + const observation = ++branchObservation;
460 const local = yield* workflow.localStatus({ cwd });
461 localRefreshResults.set(cwd, { startedAt, local });
444 - return yield* updateCachedLocalStatus(cwd, local, { publish: true });
462 + return yield* updateCachedLocalStatus(cwd, local, observation, { publish: true });
463 }),
464 );
465 },
@@ -850,7 +868,15 @@ export const make = Effect.gen(function* () {
868 return yield* Deferred.await(shared);
869 });
870
853 - // Serialize branch publication and cache invalidation with detailed reads and refreshes.
871 + const readHeadBranch = Effect.fn("VcsStatusBroadcaster.readHeadBranch")(function* (cwd: string) {
872 + const observation = ++branchObservation;
873 + const refName = yield* workflow.currentBranch(cwd);
874 + // A null branch is a detached HEAD or a checkout that is gone, such as a removed worktree.
875 + const isRepo = refName !== null || (yield* workflow.repositoryRoot(cwd)) !== null;
876 + return { observation, refName, isRepo };
877 + });
878 +
879 + // Keep cache invalidation serialized with detailed reads and refreshes.
880 const refreshBranchStatusCore = Effect.fn("VcsStatusBroadcaster.refreshBranchStatusCore")(
881 (cwd: string) =>
882 localRefreshLocks.withLock(
@@ -858,15 +884,11 @@ export const make = Effect.gen(function* () {
884 Effect.gen(function* () {
885 if (detailedSubscribers.has(cwd)) {
886 const local = yield* getOrLoadLocalStatusUnlocked(cwd);
861 - return {
862 - refName: local.refName,
863 - isRepo: local.isRepo,
864 - revision: branches.get(cwd)?.revision ?? 0,
865 - };
887 + return (
888 + branches.get(cwd) ?? { refName: local.refName, isRepo: local.isRepo, revision: 0 }
889 + );
890 }
867 - const refName = yield* workflow.currentBranch(cwd);
868 - // A null branch is a detached HEAD or a checkout that is gone, such as a removed worktree.
869 - const isRepo = refName !== null || (yield* workflow.repositoryRoot(cwd)) !== null;
891 + const { observation, refName, isRepo } = yield* readHeadBranch(cwd);
892 // HEAD cannot certify working-tree totals, even when it has not moved.
893 localRefreshResults.delete(cwd);
894 yield* workflow.invalidateLocalStatus(cwd);
@@ -875,34 +897,49 @@ export const make = Effect.gen(function* () {
897 return previous ? new Map(cache).set(cwd, { ...previous, local: null }) : cache;
898 });
899 if (detailedSubscribers.has(cwd)) {
878 - const local = yield* workflow.localStatus({ cwd });
879 - yield* updateCachedLocalStatus(cwd, local, { publish: true });
880 - return {
881 - refName: local.refName,
882 - isRepo: local.isRepo,
883 - revision: branches.get(cwd)?.revision ?? 0,
884 - };
900 + const local = yield* loadLocalStatus(cwd, { publish: true });
901 + return (
902 + branches.get(cwd) ?? { refName: local.refName, isRepo: local.isRepo, revision: 0 }
903 + );
904 }
886 - yield* publishBranch(cwd, { refName, isRepo });
887 - return { refName, isRepo, revision: branches.get(cwd)?.revision ?? 0 };
905 + yield* publishBranch(cwd, { refName, isRepo }, observation);
906 + return branches.get(cwd) ?? { refName, isRepo, revision: 0 };
907 }),
908 ),
909 );
910
911 // A checkout outside T3 Code, such as `git switch` in the terminal, calls no refresh RPC.
912 // Rereading remote too, without a fetch, sends the new branch with its own PR and counts.
894 - const refreshCheckedOutRef = Effect.fn("VcsStatusBroadcaster.refreshCheckedOutRef")(function* (
913 + const publishCheckedOutRef = Effect.fn("VcsStatusBroadcaster.publishCheckedOutRef")(function* (
914 cwd: string,
915 ) {
897 - if (!detailedSubscribers.has(cwd)) {
898 - yield* refreshBranchStatusCore(cwd);
899 - } else if ((yield* getCachedStatus(cwd))?.remote) {
900 - yield* refreshRemoteStatus(cwd, { refreshUpstream: false });
901 - } else {
902 - yield* refreshLocalStatusCore(cwd);
903 - }
916 + if (!detailedSubscribers.has(cwd)) return yield* refreshBranchStatusCore(cwd);
917 + const { observation, refName, isRepo } = yield* readHeadBranch(cwd);
918 + yield* publishBranch(cwd, { refName, isRepo }, observation);
919 });
920
921 + const reconcileCheckedOutRef = Effect.fn("VcsStatusBroadcaster.reconcileCheckedOutRef")(
922 + function* (cwd: string) {
923 + if (!detailedSubscribers.has(cwd)) return;
924 + if ((yield* getCachedStatus(cwd))?.remote) {
925 + yield* refreshRemoteStatus(cwd, { refreshUpstream: false });
926 + } else {
927 + yield* refreshLocalStatusCore(cwd);
928 + }
929 + },
930 + );
931 +
932 + const retryCheckedOutRef = <E>(cwd: string, refresh: Effect.Effect<unknown, E>) =>
933 + refresh.pipe(
934 + Effect.retry(CHECKOUT_REFRESH_RETRY),
935 + Effect.catchCause((cause) =>
936 + Effect.logWarning("VCS checked-out ref refresh failed", {
937 + cwdLength: cwd.length,
938 + ...remoteRefreshFailureDiagnostics(cause),
939 + }),
940 + ),
941 + );
942 +
943 // Runs for the subscription lifetime, so tracing would leave a span open indefinitely.
944 const watchHead = Effect.fnUntraced(function* (cwd: string, ready: Deferred.Deferred<void>) {
945 const headPath = yield* workflow.headPath(cwd);
@@ -923,17 +960,10 @@ export const make = Effect.gen(function* () {
960 Stream.filter((event) => path.basename(event.path) === "HEAD"),
961 // A rebase moves HEAD many times; refresh once for whatever arrived during a refresh.
962 Stream.buffer({ capacity: 1, strategy: "sliding" }),
926 - Stream.runForEach(() =>
927 - refreshCheckedOutRef(cwd).pipe(
928 - Effect.retry(CHECKOUT_REFRESH_RETRY),
929 - Effect.catchCause((cause) =>
930 - Effect.logWarning("VCS checked-out ref refresh failed", {
931 - cwdLength: cwd.length,
932 - ...remoteRefreshFailureDiagnostics(cause),
933 - }),
934 - ),
935 - ),
936 - ),
963 + Stream.mapEffect(() => retryCheckedOutRef(cwd, publishCheckedOutRef(cwd))),
964 + // Detailed status waits behind its own reads; the branch keeps up on the stage above.
965 + Stream.buffer({ capacity: 1, strategy: "sliding" }),
966 + Stream.runForEach(() => retryCheckedOutRef(cwd, reconcileCheckedOutRef(cwd))),
967 );
968 });
969
@@ -1044,8 +1074,21 @@ export const make = Effect.gen(function* () {
1074 const refreshBranchStatus: VcsStatusBroadcaster["Service"]["refreshBranchStatus"] = Effect.fn(
1075 "VcsStatusBroadcaster.refreshBranchStatus",
1076 )(function* (input) {
1047 - const { refName, isRepo } = yield* refreshBranchStatusCore(
1048 - yield* withFileSystem(normalizeCwd(input.cwd)),
1077 + const cwd = yield* withFileSystem(normalizeCwd(input.cwd));
1078 + if (!detailedSubscribers.has(cwd)) {
1079 + const { refName, isRepo } = yield* refreshBranchStatusCore(cwd);
1080 + return { refName, isRepo };
1081 + }
1082 + const { observation, refName, isRepo } = yield* readHeadBranch(cwd);
1083 + yield* publishBranch(cwd, { refName, isRepo }, observation);
1084 + yield* refreshBranchStatusCore(cwd).pipe(
1085 + Effect.catchCause((cause) =>
1086 + Effect.logWarning("VCS branch refresh failed", {
1087 + cwdLength: cwd.length,
1088 + ...remoteRefreshFailureDiagnostics(cause),
1089 + }),
1090 + ),
1091 + Effect.forkIn(broadcasterScope),
1092 );
1093 return { refName, isRepo };
1094 });