From 092bf2561d5ef14ec502cd88b0024798c4193a5f Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sat, 21 Nov 2026 00:15:08 +0000 Subject: [PATCH 1/3] perf(server): skip local git status on unchanged idle remote polls Track the ref snapshot alongside cached local VCS status and skip refreshLocalStatusCore during periodic remote polls when refs and shallow state have not moved and local status already satisfies branch-changes demand. Keep unconditional local refreshes for HEAD-watcher checkouts, moved refs, missing local snapshots, and unreadable ref snapshots. --- .../src/vcs/VcsStatusBroadcaster.test.ts | 122 ++++++++++++------ apps/server/src/vcs/VcsStatusBroadcaster.ts | 41 ++++-- 2 files changed, 113 insertions(+), 50 deletions(-) diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index e367bacb5a7dc..c306823ddfe19 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -1293,44 +1293,78 @@ describe("VcsStatusBroadcaster", () => { }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); }); - it.effect("a remote poll refreshes Changes totals when divergence counts stay the same", () => { - const state: Parameters[0] = { - currentLocalStatus: { - ...baseLocalStatus, - branchChanges: { baseRef: "origin/main", insertions: 745, deletions: 24 }, - }, - currentRemoteStatus: baseRemoteStatus, - localStatusCalls: 0, - remoteStatusCalls: 0, - localInvalidationCalls: 0, - remoteInvalidationCalls: 0, - }; - - return Effect.gen(function* () { - const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; - yield* broadcaster.getStatus({ cwd: "/repo" }); - const subscribed = yield* Deferred.make(); - const polled = yield* Deferred.make(); - yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( - Stream.runForEach((event) => { - if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); - if (event._tag === "remoteUpdated") return Deferred.succeed(polled, undefined); - return Effect.void; - }), - Effect.forkScoped, - ); - yield* Deferred.await(subscribed); - state.currentLocalStatus = { - ...baseLocalStatus, - branchChanges: { baseRef: "origin/main", insertions: 107, deletions: 4 }, + it.effect( + "a remote poll refreshes Changes totals only when refs moved or cannot be snapshotted", + () => { + const state: Parameters[0] = { + currentLocalStatus: { + ...baseLocalStatus, + branchChanges: { baseRef: "origin/main", insertions: 745, deletions: 24 }, + }, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, }; - state.currentRemoteStatus = remoteStatusWithPr; - yield* TestClock.adjust("30 seconds"); - yield* Deferred.await(polled); - const status = yield* broadcaster.getStatus({ cwd: "/repo" }); - assert.deepStrictEqual(status.branchChanges, state.currentLocalStatus.branchChanges); - }).pipe(Effect.provide(layerTestFor(state))); - }); + + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + yield* broadcaster.getStatus({ cwd: "/repo" }); + const subscribed = yield* Deferred.make(); + const polled = yield* Deferred.make(); + const movedTotalsPublished = yield* Deferred.make(); + yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( + Stream.runForEach((event) => { + if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); + if (event._tag === "remoteUpdated") return Deferred.succeed(polled, undefined); + if (event._tag === "localUpdated" && event.local.branchChanges?.insertions === 12) { + return Deferred.succeed(movedTotalsPublished, event.local); + } + return Effect.void; + }), + Effect.forkScoped, + ); + yield* Deferred.await(subscribed); + state.currentLocalStatus = { + ...baseLocalStatus, + branchChanges: { baseRef: "origin/main", insertions: 107, deletions: 4 }, + }; + state.currentRemoteStatus = remoteStatusWithPr; + yield* TestClock.adjust("30 seconds"); + yield* Deferred.await(polled); + const status = yield* broadcaster.getStatus({ cwd: "/repo" }); + assert.deepStrictEqual(status.branchChanges, state.currentLocalStatus.branchChanges); + + // Unchanged idle polls read remote status without re-reading local status. + const remoteBeforeIdle = state.remoteStatusCalls; + const localBeforeIdle = state.localStatusCalls; + yield* TestClock.adjust("30 seconds"); + yield* TestClock.adjust("30 seconds"); + assert.equal(state.remoteStatusCalls, remoteBeforeIdle + 2); + assert.equal(state.localStatusCalls, localBeforeIdle); + + // Moving a ref during a poll refreshes local totals even when remote status is unchanged. + state.beforeRemoteStatus = Effect.sync(() => { + state.refSnapshot = "moved"; + state.currentLocalStatus = { + ...baseLocalStatus, + branchChanges: { baseRef: "origin/main", insertions: 12, deletions: 1 }, + }; + }); + yield* TestClock.adjust("30 seconds"); + const movedLocal = yield* Deferred.await(movedTotalsPublished); + assert.deepStrictEqual(movedLocal.branchChanges, state.currentLocalStatus.branchChanges); + assert.equal(state.localStatusCalls, localBeforeIdle + 1); + + // Falling back to null ref snapshots conservatively refreshes local status. + state.beforeRemoteStatus = Effect.void; + state.refSnapshot = null; + yield* TestClock.adjust("30 seconds"); + assert.equal(state.localStatusCalls, localBeforeIdle + 2); + }).pipe(Effect.provide(layerTestFor(state))); + }, + ); it.effect("reads Changes totals only while a subscriber shows them", () => { const branchChanges = { baseRef: "origin/main", insertions: 3, deletions: 1 }; @@ -1820,6 +1854,7 @@ describe("VcsStatusBroadcaster", () => { const clientStates: Array = []; const subscribed = yield* Deferred.make(); const checkedOut = yield* Deferred.make(); + const sameRefReconciled = yield* Deferred.make(); yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => Effect.gen(function* () { const next = applyGitStatusStreamEvent(clientStates.at(-1) ?? null, event); @@ -1829,6 +1864,9 @@ describe("VcsStatusBroadcaster", () => { if (event._tag === "remoteUpdated" && event.remote && next.refName === "repro") { yield* Deferred.succeed(checkedOut, undefined); } + if (event._tag === "localUpdated" && event.local.hasWorkingTreeChanges) { + yield* Deferred.succeed(sameRefReconciled, undefined); + } }), ).pipe(Effect.forkScoped); yield* Deferred.await(subscribed); @@ -1847,6 +1885,16 @@ describe("VcsStatusBroadcaster", () => { ); // A checkout must not trigger a network fetch. assert.deepStrictEqual(state.remoteStatusRefreshUpstreamValues.slice(1), [false]); + + // A HEAD watcher event with unchanged refs (e.g. a same-branch checkout) still refreshes local status. + state.currentLocalStatus = { + ...state.currentLocalStatus, + hasWorkingTreeChanges: true, + }; + yield* fs.writeFileString(lockPath, "ref: refs/heads/repro\n"); + yield* fs.rename(lockPath, headPath); + yield* Deferred.await(sameRefReconciled); + assert.isTrue((yield* broadcaster.getStatus({ cwd: "/repo" })).hasWorkingTreeChanges); }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); }).pipe(Effect.provide(NodeServices.layer)), ); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index c39b826951025..9633558d1c49a 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -146,6 +146,7 @@ interface CachedRemoteStatus extends CachedValue { interface CachedLocalStatus extends CachedValue { readonly includesBranchChanges: boolean; + readonly refSnapshot: string | null; } interface CachedVcsStatus { @@ -410,13 +411,14 @@ export const make = Effect.gen(function* () { cwd: string, read: { readonly local: VcsStatusLocalResult; readonly includesBranchChanges: boolean }, observation: number, - options?: { publish?: boolean }, + options?: { publish?: boolean; refSnapshot?: string | null }, ) { const { local, includesBranchChanges } = read; - const nextLocal = { + const nextLocal: CachedLocalStatus = { fingerprint: fingerprintStatusPart(local), value: local, includesBranchChanges, + refSnapshot: options?.refSnapshot ?? null, }; const [localChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => { const previous = cache.get(cwd) ?? { local: null, remote: null }; @@ -523,7 +525,7 @@ export const make = Effect.gen(function* () { }, Effect.scoped); const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")( - function* (cwd: string) { + function* (cwd: string, options?: { readonly refSnapshot?: string | null }) { const requestedAt = (localRefreshRequests.get(cwd) ?? 0) + 1; localRefreshRequests.set(cwd, requestedAt); return yield* localRefreshLocks.withLock( @@ -542,7 +544,10 @@ export const make = Effect.gen(function* () { const observation = ++branchObservation; const read = yield* readLocalStatus(cwd); localRefreshResults.set(cwd, { startedAt, ...read }); - return yield* updateCachedLocalStatus(cwd, read, observation, { publish: true }); + return yield* updateCachedLocalStatus(cwd, read, observation, { + publish: true, + refSnapshot: options?.refSnapshot ?? null, + }); }), ); }, @@ -668,6 +673,7 @@ export const make = Effect.gen(function* () { options?: { readonly refreshUpstream?: boolean; readonly policyCwds?: ReadonlyArray; + readonly alwaysRefreshLocal?: boolean; }, ) { return yield* withRemoteWriteLock( @@ -679,8 +685,18 @@ export const make = Effect.gen(function* () { ? null : yield* maybeAutoPull(cwd, remoteRead, options?.policyCwds ?? [cwd]); if (pulled !== null) return pulled.remote; - // Base refs and merge bases can move without changing divergence counts. - yield* refreshLocalStatusCore(cwd); + const refs = yield* workflow.readRefSnapshot(cwd); + const local = (yield* getCachedStatus(cwd))?.local ?? null; + const localIsFresh = + !options?.alwaysRefreshLocal && + refs !== null && + local !== null && + local.refSnapshot === refs && + (local.includesBranchChanges || !wantsBranchChanges(cwd)); + if (!localIsFresh) { + // Base refs and merge bases can move without changing divergence counts. + yield* refreshLocalStatusCore(cwd, { refSnapshot: refs }); + } return yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); }), ); @@ -702,11 +718,10 @@ export const make = Effect.gen(function* () { if (pulled !== null) return mergeGitStatusParts(pulled.local, pulled.remote); yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); // A fetch, ours or not, can move the base used for local Changes totals. - if ( - refsBeforeLocalRead === null || - (yield* workflow.readRefSnapshot(cwd)) !== refsBeforeLocalRead - ) { - yield* refreshLocalStatusCore(cwd); + const refsAfter = + refsBeforeLocalRead === null ? null : yield* workflow.readRefSnapshot(cwd); + if (refsBeforeLocalRead === null || refsAfter !== refsBeforeLocalRead) { + yield* refreshLocalStatusCore(cwd, { refSnapshot: refsAfter }); } const parts = yield* getStatusParts(cwd); return mergeGitStatusParts(parts.local, parts.remote); @@ -721,7 +736,7 @@ export const make = Effect.gen(function* () { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); yield* acquireBranchChangesDemand(cwd); const refsBeforeLocalRead = options?.localOnly ? null : yield* workflow.readRefSnapshot(cwd); - yield* refreshLocalStatusCore(cwd); + yield* refreshLocalStatusCore(cwd, { refSnapshot: refsBeforeLocalRead }); if (options?.localOnly) { // Never cache a remote here: readers take `value: null` as "read and found no PR". const parts = yield* getStatusParts(cwd); @@ -1035,7 +1050,7 @@ export const make = Effect.gen(function* () { function* (cwd: string) { if (!detailedSubscribers.has(cwd)) return; if ((yield* getCachedStatus(cwd))?.remote) { - yield* refreshRemoteStatus(cwd, { refreshUpstream: false }); + yield* refreshRemoteStatus(cwd, { refreshUpstream: false, alwaysRefreshLocal: true }); } else { yield* refreshLocalStatusCore(cwd); } From ce36b9214a83102333ab4deb0a269a6c57b1b72f Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sat, 21 Nov 2026 00:48:55 +0000 Subject: [PATCH 2/3] fix(server): retry idle VCS local status when Changes totals are missing --- apps/server/src/vcs/VcsStatusBroadcaster.test.ts | 16 ++++++++++++++++ apps/server/src/vcs/VcsStatusBroadcaster.ts | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index c306823ddfe19..df25690d4ce20 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -1362,6 +1362,22 @@ describe("VcsStatusBroadcaster", () => { state.refSnapshot = null; yield* TestClock.adjust("30 seconds"); assert.equal(state.localStatusCalls, localBeforeIdle + 2); + + // Omitted Changes totals (e.g. transient totals failure) retry on the next poll even with unchanged refs. + state.refSnapshot = "moved"; + state.currentLocalStatus = { ...baseLocalStatus }; + yield* TestClock.adjust("30 seconds"); + assert.equal(state.localStatusCalls, localBeforeIdle + 3); + state.currentLocalStatus = { + ...baseLocalStatus, + branchChanges: { baseRef: "origin/main", insertions: 5, deletions: 2 }, + }; + yield* TestClock.adjust("30 seconds"); + assert.equal(state.localStatusCalls, localBeforeIdle + 4); + assert.deepStrictEqual( + (yield* broadcaster.getStatus({ cwd: "/repo" })).branchChanges, + state.currentLocalStatus.branchChanges, + ); }).pipe(Effect.provide(layerTestFor(state))); }, ); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index 9633558d1c49a..e840f4377e4d6 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -692,7 +692,8 @@ export const make = Effect.gen(function* () { refs !== null && local !== null && local.refSnapshot === refs && - (local.includesBranchChanges || !wantsBranchChanges(cwd)); + ((local.includesBranchChanges && local.value.branchChanges !== undefined) || + !wantsBranchChanges(cwd)); if (!localIsFresh) { // Base refs and merge bases can move without changing divergence counts. yield* refreshLocalStatusCore(cwd, { refSnapshot: refs }); From 52449349708327fdf3d62e85439c97826e48f21e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 03:39:55 +0000 Subject: [PATCH 3/3] fix(server): idle polls re-read local status when the working tree changes Unchanged refs alone no longer mark cached local status fresh: the poll also compares a working-tree snapshot (porcelain status with index hashes and untracked files, plus size and mtime of each listed path), so edits made outside T3 Code still reach clients on the next poll. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/git/GitWorkflowService.ts | 2 + apps/server/src/vcs/GitVcsDriver.ts | 2 + apps/server/src/vcs/GitVcsDriverCore.test.ts | 32 ++++++++++++++++ apps/server/src/vcs/GitVcsDriverCore.ts | 38 +++++++++++++++++++ .../src/vcs/VcsStatusBroadcaster.test.ts | 38 ++++++++++++++++--- apps/server/src/vcs/VcsStatusBroadcaster.ts | 31 ++++++++------- 6 files changed, 125 insertions(+), 18 deletions(-) diff --git a/apps/server/src/git/GitWorkflowService.ts b/apps/server/src/git/GitWorkflowService.ts index f012a65bb0692..e56ba2fa20202 100644 --- a/apps/server/src/git/GitWorkflowService.ts +++ b/apps/server/src/git/GitWorkflowService.ts @@ -69,6 +69,7 @@ export class GitWorkflowService extends Context.Service< readonly invalidateLocalStatus: (cwd: string) => Effect.Effect; readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect; readonly readRefSnapshot: (cwd: string) => Effect.Effect; + readonly readWorkingTreeSnapshot: (cwd: string) => Effect.Effect; readonly invalidateStatus: (cwd: string) => Effect.Effect; /** The checkout's own HEAD file, inside a linked worktree's admin directory when there is one. */ readonly headPath: (cwd: string) => Effect.Effect; @@ -397,6 +398,7 @@ export const make = Effect.gen(function* () { invalidateLocalStatus: gitManager.invalidateLocalStatus, invalidateRemoteStatus: gitManager.invalidateRemoteStatus, readRefSnapshot: git.readRefSnapshot, + readWorkingTreeSnapshot: git.readWorkingTreeSnapshot, invalidateStatus: gitManager.invalidateStatus, headPath: Effect.fn("GitWorkflowService.headPath")(function* (cwd) { // Clients keep watching threads whose worktree was removed; spawning there fails noisily. diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index 9ccba049c79e0..971b5cd878a2d 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -350,6 +350,8 @@ export class GitVcsDriver extends Context.Service< ) => Effect.Effect; /** Refs, HEAD and shallow boundaries. Null when the snapshot cannot be read. */ readonly readRefSnapshot: (cwd: string) => Effect.Effect; + /** Index and working-tree state, including untracked files. Null when it cannot be read. */ + readonly readWorkingTreeSnapshot: (cwd: string) => Effect.Effect; readonly prepareCommitContext: ( cwd: string, filePaths?: readonly string[], diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index c51f6fef60260..4ebd38849f842 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -5627,6 +5627,38 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("a working-tree snapshot detects edits that move no ref", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + const snapshot = driver.readWorkingTreeSnapshot(cwd); + const seen: Array = [yield* snapshot]; + const expectNew = Effect.fn(function* () { + const next = yield* snapshot; + assert.isNotNull(next); + assert.notInclude(seen, next); + assert.equal(yield* snapshot, next); + seen.push(next); + }); + assert.isNotNull(seen[0]); + assert.equal(yield* snapshot, seen[0]); + + yield* writeTextFile(cwd, "README.md", "edited\n"); + yield* expectNew(); + yield* writeTextFile(cwd, "README.md", "edited again\n"); + yield* expectNew(); + yield* git(cwd, ["add", "README.md"]); + yield* expectNew(); + yield* writeTextFile(cwd, "nested/untracked.txt", "new\n"); + yield* expectNew(); + yield* writeTextFile(cwd, "nested/untracked.txt", "new and longer\n"); + yield* expectNew(); + + assert.isNull(yield* driver.readWorkingTreeSnapshot(yield* makeTmpDir())); + }), + ); + it.effect.each([ { fetch: "statusDetailsRemote" }, // Storage cleanup's merge check fetches the default branch on every sweep. diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index a4bde8c983817..c3cab5111cf38 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1947,6 +1947,43 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* Effect.orElseSucceed(() => null), ); + const readWorkingTreeSnapshot: GitVcsDriver.GitVcsDriver["Service"]["readWorkingTreeSnapshot"] = + Effect.fn("GitVcsDriver.readWorkingTreeSnapshot")( + function* (cwd) { + const worktreeRoot = (yield* resolveRepositoryPaths(cwd))?.worktreeRoot; + if (!worktreeRoot) return null; + const result = yield* executeGit( + "GitVcsDriver.readWorkingTreeSnapshot", + worktreeRoot, + ["status", "--porcelain=2", "-z", "--untracked-files=all", "--no-ahead-behind"], + { allowNonZeroExit: true, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, + ); + if (result.exitCode !== 0) return null; + // Records carry index hashes; size and mtime stand in for unhashed working-tree content. + const records = result.stdout.split("\0"); + const paths: string[] = []; + for (let index = 0; index < records.length; index++) { + const record = records[index]!; + const filePath = parsePorcelainPath(record); + if (filePath) paths.push(filePath); + if (record.startsWith("2 ")) index++; + } + const stats = yield* Effect.forEach( + paths, + (filePath) => + fileSystem.stat(path.join(worktreeRoot, filePath)).pipe( + Effect.map( + (info) => `${info.size}:${Option.getOrUndefined(info.mtime)?.getTime() ?? ""}`, + ), + Effect.orElseSucceed(() => "-"), + ), + { concurrency: 16 }, + ); + return `${result.stdout}\0${stats.join("\0")}`; + }, + Effect.orElseSucceed(() => null), + ); + const refreshStatusUpstreamIfStale = Effect.fn("refreshStatusUpstreamIfStale")(function* ( cwd: string, ) { @@ -6498,6 +6535,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* statusDetailsLocal, statusDetailsRemote, readRefSnapshot, + readWorkingTreeSnapshot, prepareCommitContext, commit: Effect.fnUntraced(function* (cwd, subject, body, options) { return yield* withListRefsInvalidation(cwd, commit(cwd, subject, body, options)); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index df25690d4ce20..4beef8454e44b 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -80,6 +80,7 @@ function layerTestFor(state: { remoteStatusRefreshUpstreamValues?: Array; localStatusBranchChanges?: Array; refSnapshot?: string | null; + workingTreeSnapshot?: string | null; remoteStatusError?: GitManagerError; backgroundWorkEnabled?: boolean; automaticFetchInterval?: Effect.Effect; @@ -143,6 +144,10 @@ function layerTestFor(state: { ), readRefSnapshot: () => Effect.sync(() => (state.refSnapshot === undefined ? "refs" : state.refSnapshot)), + readWorkingTreeSnapshot: () => + Effect.sync(() => + state.workingTreeSnapshot === undefined ? "tree" : state.workingTreeSnapshot, + ), upstreamDivergence: () => Effect.sync(() => state.currentRemoteStatus?.hasUpstream && state.currentLocalStatus.refName !== null @@ -926,6 +931,7 @@ describe("VcsStatusBroadcaster", () => { localStatus: () => Effect.succeed(localStatus), currentBranch: () => Effect.succeed(localStatus.refName), readRefSnapshot: () => Effect.succeed("refs"), + readWorkingTreeSnapshot: () => Effect.succeed("tree"), remoteStatus: () => Effect.succeed(remoteStatus), invalidateLocalStatus: () => Effect.void, invalidateRemoteStatus: () => Effect.void, @@ -1023,6 +1029,7 @@ describe("VcsStatusBroadcaster", () => { localStatus: () => Effect.succeed(baseLocalStatus), currentBranch: () => Effect.succeed(baseLocalStatus.refName), readRefSnapshot: () => Effect.succeed("refs"), + readWorkingTreeSnapshot: () => Effect.succeed("tree"), remoteStatus: () => Effect.gen(function* () { remoteReads += 1; @@ -1070,6 +1077,7 @@ describe("VcsStatusBroadcaster", () => { localStatus: () => Effect.succeed(baseLocalStatus), currentBranch: () => Effect.succeed(baseLocalStatus.refName), readRefSnapshot: () => Effect.succeed("refs"), + readWorkingTreeSnapshot: () => Effect.succeed("tree"), remoteStatus: () => Effect.gen(function* () { remoteReads += 1; @@ -1124,6 +1132,7 @@ describe("VcsStatusBroadcaster", () => { }), currentBranch: () => Effect.succeed(state.currentLocalStatus.refName), readRefSnapshot: () => Effect.succeed("refs"), + readWorkingTreeSnapshot: () => Effect.succeed("tree"), remoteStatus: () => Effect.suspend(() => { state.remoteStatusCalls += 1; @@ -1294,7 +1303,7 @@ describe("VcsStatusBroadcaster", () => { }); it.effect( - "a remote poll refreshes Changes totals only when refs moved or cannot be snapshotted", + "a remote poll refreshes local status only when refs or the working tree changed", () => { const state: Parameters[0] = { currentLocalStatus: { @@ -1344,6 +1353,24 @@ describe("VcsStatusBroadcaster", () => { assert.equal(state.remoteStatusCalls, remoteBeforeIdle + 2); assert.equal(state.localStatusCalls, localBeforeIdle); + // Editing a tracked file moves no ref but still refreshes local status on the next poll. + state.workingTreeSnapshot = "edited"; + state.currentLocalStatus = { + ...state.currentLocalStatus, + hasWorkingTreeChanges: true, + workingTree: { + files: [{ path: "a.ts", insertions: 1, deletions: 0 }], + insertions: 1, + deletions: 0, + }, + }; + yield* TestClock.adjust("30 seconds"); + assert.equal(state.localStatusCalls, localBeforeIdle + 1); + assert.isTrue((yield* broadcaster.getStatus({ cwd: "/repo" })).hasWorkingTreeChanges); + yield* TestClock.adjust("30 seconds"); + assert.equal(state.localStatusCalls, localBeforeIdle + 1); + const localBeforeMove = state.localStatusCalls; + // Moving a ref during a poll refreshes local totals even when remote status is unchanged. state.beforeRemoteStatus = Effect.sync(() => { state.refSnapshot = "moved"; @@ -1355,25 +1382,25 @@ describe("VcsStatusBroadcaster", () => { yield* TestClock.adjust("30 seconds"); const movedLocal = yield* Deferred.await(movedTotalsPublished); assert.deepStrictEqual(movedLocal.branchChanges, state.currentLocalStatus.branchChanges); - assert.equal(state.localStatusCalls, localBeforeIdle + 1); + assert.equal(state.localStatusCalls, localBeforeMove + 1); // Falling back to null ref snapshots conservatively refreshes local status. state.beforeRemoteStatus = Effect.void; state.refSnapshot = null; yield* TestClock.adjust("30 seconds"); - assert.equal(state.localStatusCalls, localBeforeIdle + 2); + assert.equal(state.localStatusCalls, localBeforeMove + 2); // Omitted Changes totals (e.g. transient totals failure) retry on the next poll even with unchanged refs. state.refSnapshot = "moved"; state.currentLocalStatus = { ...baseLocalStatus }; yield* TestClock.adjust("30 seconds"); - assert.equal(state.localStatusCalls, localBeforeIdle + 3); + assert.equal(state.localStatusCalls, localBeforeMove + 3); state.currentLocalStatus = { ...baseLocalStatus, branchChanges: { baseRef: "origin/main", insertions: 5, deletions: 2 }, }; yield* TestClock.adjust("30 seconds"); - assert.equal(state.localStatusCalls, localBeforeIdle + 4); + assert.equal(state.localStatusCalls, localBeforeMove + 4); assert.deepStrictEqual( (yield* broadcaster.getStatus({ cwd: "/repo" })).branchChanges, state.currentLocalStatus.branchChanges, @@ -1520,6 +1547,7 @@ describe("VcsStatusBroadcaster", () => { currentBranch: () => Effect.succeed(baseLocalStatus.refName), invalidateRemoteStatus: () => Effect.void, readRefSnapshot: () => Effect.sync(() => refSnapshot), + readWorkingTreeSnapshot: () => Effect.succeed("tree"), remoteStatus: () => Deferred.succeed(fetchStarted, undefined).pipe( Effect.andThen(Deferred.await(finishFetch)), diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index e840f4377e4d6..292518cae8faa 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -146,7 +146,8 @@ interface CachedRemoteStatus extends CachedValue { interface CachedLocalStatus extends CachedValue { readonly includesBranchChanges: boolean; - readonly refSnapshot: string | null; + /** Refs and working tree read before this status, set only by remote polls. */ + readonly snapshot: string | null; } interface CachedVcsStatus { @@ -411,14 +412,14 @@ export const make = Effect.gen(function* () { cwd: string, read: { readonly local: VcsStatusLocalResult; readonly includesBranchChanges: boolean }, observation: number, - options?: { publish?: boolean; refSnapshot?: string | null }, + options?: { publish?: boolean; snapshot?: string | null }, ) { const { local, includesBranchChanges } = read; const nextLocal: CachedLocalStatus = { fingerprint: fingerprintStatusPart(local), value: local, includesBranchChanges, - refSnapshot: options?.refSnapshot ?? null, + snapshot: options?.snapshot ?? null, }; const [localChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => { const previous = cache.get(cwd) ?? { local: null, remote: null }; @@ -525,7 +526,7 @@ export const make = Effect.gen(function* () { }, Effect.scoped); const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")( - function* (cwd: string, options?: { readonly refSnapshot?: string | null }) { + function* (cwd: string, options?: { readonly snapshot?: string | null }) { const requestedAt = (localRefreshRequests.get(cwd) ?? 0) + 1; localRefreshRequests.set(cwd, requestedAt); return yield* localRefreshLocks.withLock( @@ -546,7 +547,7 @@ export const make = Effect.gen(function* () { localRefreshResults.set(cwd, { startedAt, ...read }); return yield* updateCachedLocalStatus(cwd, read, observation, { publish: true, - refSnapshot: options?.refSnapshot ?? null, + snapshot: options?.snapshot ?? null, }); }), ); @@ -685,18 +686,21 @@ export const make = Effect.gen(function* () { ? null : yield* maybeAutoPull(cwd, remoteRead, options?.policyCwds ?? [cwd]); if (pulled !== null) return pulled.remote; + // Working-tree edits move no ref, so both must match the snapshot taken before the cached read. const refs = yield* workflow.readRefSnapshot(cwd); + const workingTree = refs === null ? null : yield* workflow.readWorkingTreeSnapshot(cwd); + const snapshot = workingTree === null ? null : `${refs}\0${workingTree}`; const local = (yield* getCachedStatus(cwd))?.local ?? null; const localIsFresh = !options?.alwaysRefreshLocal && - refs !== null && + snapshot !== null && local !== null && - local.refSnapshot === refs && + local.snapshot === snapshot && ((local.includesBranchChanges && local.value.branchChanges !== undefined) || !wantsBranchChanges(cwd)); if (!localIsFresh) { // Base refs and merge bases can move without changing divergence counts. - yield* refreshLocalStatusCore(cwd, { refSnapshot: refs }); + yield* refreshLocalStatusCore(cwd, { snapshot }); } return yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); }), @@ -719,10 +723,11 @@ export const make = Effect.gen(function* () { if (pulled !== null) return mergeGitStatusParts(pulled.local, pulled.remote); yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); // A fetch, ours or not, can move the base used for local Changes totals. - const refsAfter = - refsBeforeLocalRead === null ? null : yield* workflow.readRefSnapshot(cwd); - if (refsBeforeLocalRead === null || refsAfter !== refsBeforeLocalRead) { - yield* refreshLocalStatusCore(cwd, { refSnapshot: refsAfter }); + if ( + refsBeforeLocalRead === null || + (yield* workflow.readRefSnapshot(cwd)) !== refsBeforeLocalRead + ) { + yield* refreshLocalStatusCore(cwd); } const parts = yield* getStatusParts(cwd); return mergeGitStatusParts(parts.local, parts.remote); @@ -737,7 +742,7 @@ export const make = Effect.gen(function* () { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); yield* acquireBranchChangesDemand(cwd); const refsBeforeLocalRead = options?.localOnly ? null : yield* workflow.readRefSnapshot(cwd); - yield* refreshLocalStatusCore(cwd, { refSnapshot: refsBeforeLocalRead }); + yield* refreshLocalStatusCore(cwd); if (options?.localOnly) { // Never cache a remote here: readers take `value: null` as "read and found no PR". const parts = yield* getStatusParts(cwd);