The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 23:30 UTC
fix(server): new worktrees refresh their index once, so captures and status stop re-hashing every file (#21341)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Joseph Vidal committed
Nov 11, 2026 at 04:16 UTC
25631c3c838daf468005e825a034bd75bbb42372
2 files changed
+132
apps/server/src/vcs/GitVcsDriverCore.test.ts
+74
@@ -13,6 +13,7 @@ import * as Exit from "effect/Exit";
13
import * as FileSystem from "effect/FileSystem";
14
import * as Fiber from "effect/Fiber";
15
import * as Layer from "effect/Layer";
16
+import * as Option from "effect/Option";
17
import * as Path from "effect/Path";
18
import * as Queue from "effect/Queue";
19
import * as PlatformError from "effect/PlatformError";
@@ -4678,6 +4679,79 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4679
assert.isTrue(Result.isSuccess(result), "worktree creation failed in the hook");
4680
}),
4681
);
4682
+ it.effect("refreshes a new worktree's index once its checkout second is over", () =>
4683
+ Effect.gen(function* () {
4684
+ const cwd = yield* makeTmpDir();
4685
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
4686
+ const fs = yield* FileSystem.FileSystem;
4687
+ const path = yield* Path.Path;
4688
+ const driver = yield* GitVcsDriver.GitVcsDriver;
4689
+ for (let file = 0; file < 50; file++) {
4690
+ yield* writeTextFile(cwd, `src/file-${file}.txt`, `contents ${file}\n`);
4691
+ }
4692
+ yield* git(cwd, ["add", "."]);
4693
+ yield* git(cwd, ["commit", "-m", "more files"]);
4694
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "fresh");
4695
+
4696
+ // The refresh waits on the clock that stamps the files.
4697
+ yield* TestClock.withLive(
4698
+ driver.createWorktree({
4699
+ cwd,
4700
+ path: worktreePath,
4701
+ refName: initialBranch,
4702
+ newRefName: "feature/fresh",
4703
+ }),
4704
+ );
4705
+
4706
+ // Git re-reads every entry stamped in the index's own second or later.
4707
+ const indexPath = path.resolve(
4708
+ worktreePath,
4709
+ yield* git(worktreePath, ["rev-parse", "--git-path", "index"]),
4710
+ );
4711
+ const indexMtime = Option.getOrThrow((yield* fs.stat(indexPath)).mtime);
4712
+ const indexSecond = Math.floor(indexMtime.getTime() / 1000);
4713
+ const entrySeconds = Array.from(
4714
+ (yield* git(worktreePath, ["ls-files", "--debug"])).matchAll(/^ {2}mtime: (\d+):/gm),
4715
+ ([, seconds]) => Number(seconds),
4716
+ );
4717
+ assert.equal(entrySeconds.length, 51);
4718
+ assert.deepEqual(
4719
+ entrySeconds.filter((seconds) => seconds >= indexSecond),
4720
+ [],
4721
+ );
4722
+ }),
4723
+ );
4724
+ it.effect("returns the worktree when its index refresh fails", () =>
4725
+ Effect.gen(function* () {
4726
+ const cwd = yield* makeTmpDir();
4727
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
4728
+ const fs = yield* FileSystem.FileSystem;
4729
+ const path = yield* Path.Path;
4730
+ const driver = yield* GitVcsDriver.GitVcsDriver;
4731
+ // Something else takes the new worktree's index lock as soon as its checkout ends.
4732
+ yield* writeTextFile(
4733
+ cwd,
4734
+ ".git/hooks/post-checkout",
4735
+ '#!/bin/sh\ntouch "$(git rev-parse --git-path index).lock"\n',
4736
+ );
4737
+ yield* fs.chmod(path.join(cwd, ".git/hooks/post-checkout"), 0o755);
4738
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "locked");
4739
+
4740
+ const created = yield* driver.createWorktree({
4741
+ cwd,
4742
+ path: worktreePath,
4743
+ refName: initialBranch,
4744
+ newRefName: "feature/locked",
4745
+ });
4746
+
4747
+ assert.equal(created.worktree.path, worktreePath);
4748
+ const indexPath = path.resolve(
4749
+ worktreePath,
4750
+ yield* git(worktreePath, ["rev-parse", "--git-path", "index"]),
4751
+ );
4752
+ assert.isTrue(yield* fs.exists(`${indexPath}.lock`));
4753
+ }),
4754
+ );
4755
it.effect.each(["commit", "short-commit", "tag", "revision"] as const)(
4756
"creates a worktree from a %s without recording it as a PR base",
4757
(kind) =>
apps/server/src/vcs/GitVcsDriverCore.ts
+58
@@ -65,6 +65,7 @@ const branchBaseConfigKey = (branch: string) => `branch.${branch}.t3-merge-base`
65
const legacyBranchBaseConfigKey = (branch: string) => `branch.${branch}.gh-merge-base`;
66
const gitProcesses = Semaphore.makeUnsafe(8);
67
const backgroundGitProcesses = Semaphore.makeUnsafe(2);
68
+const WORKTREE_INDEX_REFRESH_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5));
69
const WORKTREE_REMOVE_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5));
70
const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
71
const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]";
@@ -4247,6 +4248,52 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4248
},
4249
);
4250
4251
+ // A checkout stamps most files in the second it writes the index, and Git re-reads every file
4252
+ // stamped in the index's own second or later ("racily clean") on each status, diff and add.
4253
+ // T3's own git calls never rewrite a worktree's index, so refresh it once.
4254
+ const refreshNewWorktreeIndex = Effect.fn("refreshNewWorktreeIndex")(function* (
4255
+ worktreePath: string,
4256
+ ) {
4257
+ const indexPath = path.resolve(
4258
+ worktreePath,
4259
+ (yield* runGitStdout("GitVcsDriver.createWorktree.indexPath", worktreePath, [
4260
+ "rev-parse",
4261
+ "--git-path",
4262
+ "index",
4263
+ ])).trim(),
4264
+ );
4265
+ const indexSecond = fileSystem.stat(indexPath).pipe(
4266
+ Effect.map(({ mtime }) => Option.map(mtime, (date) => Math.floor(date.getTime() / 1000))),
4267
+ Effect.orElseSucceed(() => Option.none<number>()),
4268
+ );
4269
+ // Older Git (such as 2.30) skips the write when racy entries merely check out clean.
4270
+ // A refresh re-reads every checked-out file, so allow five minutes outside shared permits.
4271
+ const refresh = runGit(
4272
+ "GitVcsDriver.createWorktree.refreshIndex",
4273
+ worktreePath,
4274
+ ["update-index", "-q", "--refresh", "--force-write-index"],
4275
+ { timeoutMs: WORKTREE_INDEX_REFRESH_TIMEOUT_MS },
4276
+ );
4277
+ const checkoutSecond = yield* indexSecond;
4278
+ yield* refresh;
4279
+ const refreshedSecond = yield* indexSecond;
4280
+ if (
4281
+ Option.isNone(checkoutSecond) ||
4282
+ Option.isNone(refreshedSecond) ||
4283
+ refreshedSecond.value > checkoutSecond.value
4284
+ ) {
4285
+ return;
4286
+ }
4287
+ // The refresh finished within the checkout's second, so its index is just as racy. Wait that
4288
+ // second out (file timestamps can trail the clock by a kernel tick) and refresh again. Two
4289
+ // seconds or more means this clock does not match the file timestamps, as on a share stamped
4290
+ // by another machine, so it cannot say when that second ends.
4291
+ const delay = (checkoutSecond.value + 1) * 1000 + 50 - (yield* Clock.currentTimeMillis);
4292
+ if (delay >= 2_000) return;
4293
+ if (delay > 0) yield* Effect.sleep(Duration.millis(delay));
4294
+ yield* refresh;
4295
+ });
4296
+
4297
const createWorktree: GitVcsDriver.GitVcsDriver["Service"]["createWorktree"] = Effect.fn(
4298
"createWorktree",
4299
)(function* (input, options) {
@@ -4444,6 +4491,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4491
);
4492
}
4493
4494
+ // Before the worktree is returned: a refresh holds index.lock, so running it in the
4495
+ // background would race the setup script's and the agent's own git commands.
4496
+ yield* refreshNewWorktreeIndex(worktreePath).pipe(
4497
+ Effect.catch((cause) =>
4498
+ Effect.logWarning(
4499
+ "worktree index refresh failed; Git keeps re-reading its files until the index is rewritten",
4500
+ { worktreePath, cause },
4501
+ ),
4502
+ ),
4503
+ );
4504
+
4505
if (newRefName && base) {
4506
yield* Effect.gen(function* () {
4507
yield* writeConfigValue({