diff --git a/apps/server/src/git/GitWorkflowService.ts b/apps/server/src/git/GitWorkflowService.ts index f012a65bb069..e56ba2fa2020 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 9ccba049c79e..971b5cd878a2 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 5e070c3d42a5..739449d6a979 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -5648,6 +5648,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 fcf3b3cbbd60..5455c6f6ab72 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1958,6 +1958,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, ) { @@ -6536,6 +6573,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 69c33b785bd4..eea48b8a72d9 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; @@ -1293,48 +1302,120 @@ 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 local status only when refs or the working tree changed", + () => { + 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); + + // 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"; + 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, 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, 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, localBeforeMove + 3); + state.currentLocalStatus = { + ...baseLocalStatus, + branchChanges: { baseRef: "origin/main", insertions: 5, deletions: 2 }, + }; + yield* TestClock.adjust("30 seconds"); + assert.equal(state.localStatusCalls, localBeforeMove + 4); + assert.deepStrictEqual( + (yield* broadcaster.getStatus({ cwd: "/repo" })).branchChanges, + state.currentLocalStatus.branchChanges, + ); + }).pipe(Effect.provide(layerTestFor(state))); + }, + ); it.effect("a remote poll that moves no ref does not re-read local status", () => { const state: Parameters[0] = { - currentLocalStatus: baseLocalStatus, + // Subscribers want Changes totals; a read without them is retried on the next poll. + currentLocalStatus: { + ...baseLocalStatus, + branchChanges: { baseRef: "origin/main", insertions: 1, deletions: 0 }, + }, currentRemoteStatus: baseRemoteStatus, localStatusCalls: 0, remoteStatusCalls: 0, @@ -1514,6 +1595,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)), @@ -1864,6 +1946,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); @@ -1873,6 +1956,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); @@ -1891,6 +1977,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 8a507e799c82..1060ba7f466e 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -668,7 +668,7 @@ export const make = Effect.gen(function* () { options?: { readonly refreshUpstream?: boolean; readonly policyCwds?: ReadonlyArray; - /** Refs seen by this caller's last local read; skips the re-read while they hold. */ + /** Snapshot at this caller's last complete local read; skips re-reads while it holds. */ readonly refsAtLocalRead?: Ref.Ref; }, ) { @@ -686,11 +686,17 @@ export const make = Effect.gen(function* () { if (refsAtLocalRead === undefined) { yield* refreshLocalStatusCore(cwd); } else { + // Working-tree edits move no ref, so they are part of the snapshot too. const refs = yield* workflow.readRefSnapshot(cwd); - if (refs === null || refs !== (yield* Ref.get(refsAtLocalRead))) { + const workingTree = refs === null ? null : yield* workflow.readWorkingTreeSnapshot(cwd); + const snapshot = workingTree === null ? null : `${refs}\0${workingTree}`; + if (snapshot === null || snapshot !== (yield* Ref.get(refsAtLocalRead))) { yield* Ref.set(refsAtLocalRead, null); - yield* refreshLocalStatusCore(cwd); - yield* Ref.set(refsAtLocalRead, refs); + const local = yield* refreshLocalStatusCore(cwd); + // A read that omitted wanted Changes totals retries on the next poll. + if (local.branchChanges !== undefined || !wantsBranchChanges(cwd)) { + yield* Ref.set(refsAtLocalRead, snapshot); + } } } return yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true });