@devries-cameron20 / t3code-1 / commits / 1852121afa
Fork 0

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

fix(server): failed background status fetches no longer leave partial packs (#39264)

Julius Marminge committed Nov 19, 2026 at 11:11 UTC 1852121afac633f7b4dd85e4965f679e66de21dd
2 files changed +205 -28
apps/server/src/vcs/GitVcsDriverCore.test.ts
+77 -2
@@ -1285,7 +1285,8 @@ describe("pullCurrentBranch", () => {
1285 }
1286 }
1287 const handle = yield* delegate.spawn(command);
1288 - if (!ChildProcess.isStandardCommand(command) || !command.args.includes("fetch"))
1288 + // A status fetch downloads once, then fetches again only to move refs.
1289 + if (!ChildProcess.isStandardCommand(command) || !command.args.includes("--dry-run"))
1290 return handle;
1291 const commandCwd = String(command.options.cwd);
1292 if (commandCwd === targetPath) {
@@ -1436,7 +1437,8 @@ describe("pullCurrentBranch", () => {
1437 const result = yield* Fiber.join(pull);
1438 assert.equal(result.status, "pulled");
1439 assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
1439 - assert.deepEqual(events, ["fetch-exit", "pull-spawn"]);
1440 + // The status fetch downloads, then fetches again to move the refs it found.
1441 + assert.deepEqual(events, ["fetch-exit", "fetch-exit", "pull-spawn"]);
1442 }).pipe(Effect.ensuring(Deferred.succeed(releaseFetch, undefined)));
1443 }),
1444 ).pipe(Effect.provide(layerTest)),
@@ -5604,6 +5606,79 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
5606 }),
5607 );
5608
5609 + // Cutting a transfer short takes a shell `upload-pack` wrapper.
5610 + describe.skipIf(HostProcess.Platform.defaultValue() === "win32")(
5611 + "background upstream fetch objects",
5612 + () => {
5613 + it.effect.each([
5614 + { transfer: "cut short", unpackLimit: "1", behindCount: 0 },
5615 + { transfer: "complete as a pack", unpackLimit: "1", behindCount: 1 },
5616 + { transfer: "complete as loose objects", unpackLimit: "100", behindCount: 1 },
5617 + { transfer: "followed by a push", unpackLimit: "100", behindCount: 0 },
5618 + ] as const)(
5619 + "leaves only finished objects after a background upstream fetch that is $transfer",
5620 + ({ transfer, unpackLimit, behindCount }) =>
5621 + Effect.gen(function* () {
5622 + const path = yield* Path.Path;
5623 + const cwd = yield* makeTmpDir();
5624 + const remote = yield* makeTmpDir("git-vcs-driver-remote-");
5625 + const updater = yield* makeTmpDir("git-vcs-driver-updater-");
5626 + const { initialBranch } = yield* initRepoWithCommit(cwd);
5627 + yield* git(remote, ["init", "--bare"]);
5628 + yield* git(cwd, ["remote", "add", "origin", remote]);
5629 + yield* git(cwd, ["push", "-u", "origin", initialBranch]);
5630 +
5631 + yield* git(updater, ["clone", remote, "."]);
5632 + yield* git(updater, ["config", "user.email", "test@test.com"]);
5633 + yield* git(updater, ["config", "user.name", "Test"]);
5634 + const incompressible = new Uint8Array(150_000);
5635 + for (let index = 0, state = 1; index < incompressible.length; index++) {
5636 + state = (Math.imul(state, 1_103_515_245) + 12_345) >>> 0;
5637 + incompressible[index] = state >>> 16;
5638 + }
5639 + NodeFS.writeFileSync(path.join(updater, "remote.bin"), incompressible);
5640 + yield* git(updater, ["add", "remote.bin"]);
5641 + yield* git(updater, ["commit", "-m", "remote commit"]);
5642 + yield* git(updater, ["push", "origin", initialBranch]);
5643 + NodeFS.writeFileSync(path.join(updater, "pushed.bin"), incompressible.reverse());
5644 + yield* git(updater, ["add", "pushed.bin"]);
5645 + yield* git(updater, ["commit", "-m", "pushed between connections"]);
5646 + yield* git(updater, ["push", "origin", "HEAD:refs/pending/push"]);
5647 +
5648 + // The first transfer is whole and the branch may move right after it. The fetch
5649 + // that moves the refs then meets a remote its download did not see, and must not
5650 + // point a ref at the push: nothing has put those objects in the repository.
5651 + const served = path.join(remote, "served");
5652 + if (transfer === "cut short") NodeFS.writeFileSync(served, "");
5653 + const push =
5654 + transfer === "followed by a push"
5655 + ? `git --git-dir='${remote}' update-ref refs/heads/${initialBranch} refs/pending/push;`
5656 + : "";
5657 + const later =
5658 + transfer === "followed by a push" ? "" : "| dd bs=1 count=50000 2>/dev/null";
5659 + yield* git(cwd, [
5660 + "config",
5661 + "remote.origin.uploadpack",
5662 + `f() { if [ -e '${served}' ]; then git upload-pack "$@" ${later}; else : > '${served}'; git upload-pack "$@"; ${push} fi; }; f`,
5663 + ]);
5664 + yield* git(cwd, ["config", "fetch.unpackLimit", unpackLimit]);
5665 + const objects = path.join(cwd, ".git", "objects");
5666 + NodeFS.writeFileSync(path.join(objects, "pack", "tmp_pack_other"), "another fetch");
5667 +
5668 + const status = yield* (yield* GitVcsDriver.GitVcsDriver).statusDetailsRemote(cwd);
5669 +
5670 + assert.equal(status.behindCount, behindCount);
5671 + assert.deepEqual(
5672 + [...NodeFS.readdirSync(objects), ...NodeFS.readdirSync(path.join(objects, "pack"))]
5673 + .filter((name) => name.startsWith("tmp_"))
5674 + .toSorted(),
5675 + ["tmp_pack_other"],
5676 + );
5677 + }),
5678 + );
5679 + },
5680 + );
5681 +
5682 it.effect("uses origin HEAD for default-branch detection with a non-origin upstream", () =>
5683 Effect.gen(function* () {
5684 const cwd = yield* makeTmpDir();
apps/server/src/vcs/GitVcsDriverCore.ts
+128 -26
@@ -1437,6 +1437,55 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1437 );
1438 });
1439
1440 + // Makes Git write new objects to `privateObjects` and read the repository's. Lists every object
1441 + // directory Git resolved (`count-objects -v` output) flat, since nesting the repository's objects
1442 + // one level deeper can pass Git's depth limit.
1443 + const privateObjectDirectoryEnv = (
1444 + privateObjects: string,
1445 + repositoryObjects: string,
1446 + countObjects: string,
1447 + ) => {
1448 + // Git prints an entry C-quoted when it needs quoting; quote the rest the same way, so a
1449 + // path holding the list separator stays one entry (Git 2.11.1+).
1450 + const quoted = (entry: string) =>
1451 + entry.startsWith('"') ? entry : `"${entry.replace(/[\\"]/g, "\\$&")}"`;
1452 + const alternates = [
1453 + repositoryObjects,
1454 + ...countObjects
1455 + .split("\n")
1456 + .flatMap((line) =>
1457 + line.startsWith("alternate: ") ? [line.slice("alternate: ".length)] : [],
1458 + ),
1459 + ];
1460 + return {
1461 + GIT_OBJECT_DIRECTORY: privateObjects,
1462 + GIT_ALTERNATE_OBJECT_DIRECTORIES: alternates
1463 + .map(quoted)
1464 + .join(hostPlatform === "win32" ? ";" : ":"),
1465 + } satisfies NodeJS.ProcessEnv;
1466 + };
1467 +
1468 + // Moves finished packs and loose objects into the repository, as Git's own quarantine does. A
1469 + // pack's index goes last: Git reads a pack as soon as it sees the index.
1470 + const adoptPrivateObjects = Effect.fn("GitVcsDriver.adoptPrivateObjects")(function* (
1471 + privateObjects: string,
1472 + repositoryObjects: string,
1473 + ) {
1474 + for (const directory of yield* fileSystem.readDirectory(privateObjects)) {
1475 + if (directory !== "pack" && !/^[0-9a-f]{2}$/.test(directory)) continue;
1476 + const names = (yield* fileSystem.readDirectory(path.join(privateObjects, directory)))
1477 + .filter((name) => !name.startsWith("tmp_"))
1478 + .toSorted((a, b) => Number(a.endsWith(".idx")) - Number(b.endsWith(".idx")));
1479 + if (names.length === 0) continue;
1480 + yield* fileSystem.makeDirectory(path.join(repositoryObjects, directory), { recursive: true });
1481 + for (const name of names) {
1482 + const target = path.join(repositoryObjects, directory, name);
1483 + if (yield* fileSystem.exists(target)) continue;
1484 + yield* fileSystem.rename(path.join(privateObjects, directory, name), target);
1485 + }
1486 + }
1487 + });
1488 +
1489 const fetchRemoteForStatus = Effect.fn("GitVcsDriver.fetchRemoteForStatus.wait")(function* (
1490 gitCommonDir: string,
1491 remoteName: string,
@@ -1447,11 +1496,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1496 // this poll from starting `git gc --auto`. When that gc fails, for example on a repository
1497 // with missing objects, Git retries it on every fetch and leaves a full-size `tmp_pack_*`
1498 // behind each time, so a background poll could fill the disk.
1450 - const args = [
1499 + const fetchArgs = (mode: "--dry-run" | "--quiet") => [
1500 "--git-dir",
1501 gitCommonDir,
1502 "fetch",
1454 - "--quiet",
1503 + mode,
1504 "--no-tags",
1505 "--no-auto-gc",
1506 // FETCH_HEAD is written without a lock; overlapping a user's `git pull` makes that pull fail.
@@ -1459,6 +1508,77 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1508 "--",
1509 remoteName,
1510 ];
1511 + const objectsPath = path.join(gitCommonDir, "objects");
1512 + const fetchOptions = {
1513 + fallbackErrorDetail: "Background Git fetch exited with a non-zero status.",
1514 + timeoutMs: Duration.toMillis(STATUS_UPSTREAM_FETCH_TIMEOUT),
1515 + };
1516 + // A failed or killed fetch leaves its partial `tmp_pack_*` behind, and nothing tells it apart
1517 + // from another Git process's. So the download goes to a directory only this fetch writes to,
1518 + // named like Git's own quarantine so `git gc` prunes one a crash left. A dry run downloads
1519 + // without moving refs, which must not point at objects the repository does not hold yet.
1520 + const isolatedFetch = Effect.acquireUseRelease(
1521 + fileSystem.makeTempDirectory({ directory: objectsPath, prefix: "tmp_objdir-t3code-" }),
1522 + Effect.fnUntraced(function* (privateObjects) {
1523 + const objectDirectories = yield* runGitStdout(
1524 + "GitVcsDriver.fetchRemoteForStatus.alternates",
1525 + fetchCwd,
1526 + ["--git-dir", gitCommonDir, "count-objects", "-v"],
1527 + );
1528 + const options = (writableObjects: string) => ({
1529 + ...fetchOptions,
1530 + env: {
1531 + ...STATUS_UPSTREAM_REFRESH_ENV,
1532 + ...privateObjectDirectoryEnv(writableObjects, objectsPath, objectDirectories),
1533 + },
1534 + });
1535 + const { stderr } = yield* executeGit(
1536 + "GitVcsDriver.fetchRemoteForStatus.download",
1537 + fetchCwd,
1538 + fetchArgs("--dry-run"),
1539 + options(privateObjects),
1540 + );
1541 + yield* adoptPrivateObjects(privateObjects, objectsPath);
1542 + // A dry run names each ref it would move on stderr and is silent when none would.
1543 + if (stderr.trim() === "") return;
1544 + // This fetch may only move refs to objects the repository now holds. If the remote moved
1545 + // on since the dry run it would download, so it gets an object directory where `pack` is
1546 + // a file and every transfer is a pack: it then fails, moves nothing, and the next poll
1547 + // downloads. Git reports the unreadable `pack` on stderr and carries on.
1548 + const sealedObjects = path.join(privateObjects, "sealed");
1549 + yield* fileSystem.makeDirectory(sealedObjects);
1550 + yield* fileSystem.writeFileString(path.join(sealedObjects, "pack"), "");
1551 + yield* executeGit(
1552 + "GitVcsDriver.fetchRemoteForStatus",
1553 + fetchCwd,
1554 + ["-c", "fetch.unpackLimit=1", ...fetchArgs("--quiet")],
1555 + options(sealedObjects),
1556 + );
1557 + }),
1558 + (privateObjects) =>
1559 + fileSystem
1560 + .remove(privateObjects, { recursive: true, force: true })
1561 + .pipe(
1562 + Effect.catchCause((cause) =>
1563 + Effect.logWarning(
1564 + "Could not remove a background Git fetch's object directory",
1565 + cause,
1566 + ),
1567 + ),
1568 + ),
1569 + ).pipe(
1570 + Effect.mapError((error) =>
1571 + error._tag === "GitCommandError"
1572 + ? error
1573 + : new GitCommandError({
1574 + operation: "GitVcsDriver.fetchRemoteForStatus",
1575 + command: "git fetch",
1576 + cwd: fetchCwd,
1577 + detail: "Background Git fetch could not use its object directory.",
1578 + cause: error,
1579 + }),
1580 + ),
1581 + );
1582 const key = `${gitCommonDir}\0${remoteName}`;
1583 const failure = statusRemoteRefreshFailures.get(key);
1584 if (failure && (yield* Clock.currentTimeMillis) < failure.retryAt) return;
@@ -1472,12 +1592,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1592 // Frequent poll deadlines and disconnected clients must not interrupt a
1593 // healthy writer. A separate safety limit prevents a hung transport from
1594 // occupying its worker slot indefinitely.
1475 - const fetch = executeGit("GitVcsDriver.fetchRemoteForStatus", fetchCwd, args, {
1476 - env: STATUS_UPSTREAM_REFRESH_ENV,
1477 - fallbackErrorDetail: "Background Git fetch exited with a non-zero status.",
1478 - timeoutMs: Duration.toMillis(STATUS_UPSTREAM_FETCH_TIMEOUT),
1479 - }).pipe(
1480 - Effect.asVoid,
1595 + const fetch = isolatedFetch.pipe(
1596 Effect.tap(() =>
1597 Effect.sync(() => {
1598 statusRemoteRefreshFailures.delete(key);
@@ -2995,33 +3110,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3110 });
3111 const tempIndexPath = path.join(directory, "index");
3112 // Staging writes blobs; put them in the temp directory so a draft leaves nothing behind.
2998 - // List every object directory Git resolved (nested and environment alternates) flat,
2999 - // since nesting the repository's objects one level deeper can pass Git's depth limit.
3113 const objectDirectories = yield* runGitStdout(
3114 "GitVcsDriver.prepareCommitContext.alternates",
3115 cwd,
3116 ["count-objects", "-v"],
3117 );
3005 - // Git prints an entry C-quoted when it needs quoting; quote the rest the same way, so a
3006 - // path holding the list separator stays one entry (Git 2.11.1+).
3007 - const quoted = (entry: string) =>
3008 - entry.startsWith('"') ? entry : `"${entry.replace(/[\\"]/g, "\\$&")}"`;
3009 - const alternates = [
3010 - path.resolve(physicalCwd, objectsValue.trim()),
3011 - ...objectDirectories
3012 - .split("\n")
3013 - .flatMap((line) =>
3014 - line.startsWith("alternate: ") ? [line.slice("alternate: ".length)] : [],
3015 - ),
3016 - ];
3118 const tempObjectsPath = path.join(directory, "objects");
3119 yield* fileSystem.makeDirectory(tempObjectsPath);
3120 const env = {
3121 GIT_INDEX_FILE: tempIndexPath,
3021 - GIT_OBJECT_DIRECTORY: tempObjectsPath,
3022 - GIT_ALTERNATE_OBJECT_DIRECTORIES: alternates
3023 - .map(quoted)
3024 - .join(hostPlatform === "win32" ? ";" : ":"),
3122 + ...privateObjectDirectoryEnv(
3123 + tempObjectsPath,
3124 + path.resolve(physicalCwd, objectsValue.trim()),
3125 + objectDirectories,
3126 + ),
3127 } satisfies NodeJS.ProcessEnv;
3128 const config = [
3129 "-c",