fix(git): the interval fetches one run per folder, and the rest of the review (review)

The scheduler is the part that runs with nobody watching, so it is decided per folder
rather than per session: sessions sharing a folder are the same clone and share one run
that both see, `inFlight`/`lastRunAt` are keyed by the resolved folder, the schedule is
stamped when a folder is picked instead of when its run starts, and a tick is skipped
while the previous one is still going. The click route joins a run already going for the
same folder rather than starting a second fetch in one clone.

The tick keeps ONE bound for the git processes of the whole run and hands it to every
folder, and bounds its folders with a bound of its own: the same semaphore cannot serve
both levels, because a folder holding a slot would then wait for a slot its own fetch
needs. Both bounds are `FETCH_CONCURRENCY`, so the ceiling is unchanged. The bound claim
in CLAUDE.md, the `Limiter` jsdoc and docs/api-reference.md now describes it as it is.

Before anything is fetched the folder is probed with the bounded path probe, so a case on
a mount that stopped answering is skipped and never touched.

The rest of the review: the hooks are named as they really are (`reference-transaction`,
`pre-auto-gc` through the maintenance a fetch triggers, config commands such as
`credential.helper`) and `FETCH_CONFIG_ARGS` turns maintenance and gc off for an
unattended run; the claim that an agent in the same repository is not disturbed is
replaced by the ref-lock collision it actually is; the click route publishes its own run
as `trigger: 'click'`; `autoSaveGitFetch` uses `_apiJson` so a 400 shows the error toast
and applies the returned settings; and the two wording fixes.

Tests: the scheduler pins one run per folder, the two bounds, the probe before anything
runs, and that closing the session holding the override keeps the folder while another
session is still open on it; the route test pins the click publish and the join, asserting
the join before the held run is released — releasing first lets the run finish and clears
the lane, so the joiner would be entitled to its own fetch and the test would pass for the
wrong reason.
This commit is contained in:
Devvyn committed 2026-10-25 20:04:01 +08:00
1 parent 9432d9f9e2
commit ea4318947b
20 files changed
+774 -209

No files matched your search

+51
View File
@@ -11,6 +11,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createRouteTestHarness } from './_route-test-utils.js';
import { registerGitStatusRoutes } from '../../src/web/routes/git-status-routes.js';
import { clearGitStatusCache, runGit, type GitRunner } from '../../src/git-workspace-status.js';
import { clearGitFetchLane } from '../../src/git-fetch.js';
const ENV = {
...process.env,
@@ -44,6 +45,8 @@ async function setup(
beforeEach(() => {
clearGitStatusCache();
// The fetch lane is module state: a run left in it by the previous test would be joined by the next one.
clearGitFetchLane();
dir = mkdtempSync(join(tmpdir(), 'git-status-route-'));
});
afterEach(() => {
@@ -467,6 +470,54 @@ describe('POST /api/sessions/:id/git-fetch', () => {
expect(fetches()).toEqual([]);
});
it("publishes what the click got as the session's own fetch state, naming the trigger the user asked for", async () => {
git(dir, 'init', '-q', '-b', 'main');
const { runner } = recordingRunner();
const { app, ctx } = await setup({ git: runner });
const res = await app.inject({ method: 'POST', url: '/api/sessions/test-session-1/git-fetch' });
expect(res.statusCode).toBe(200);
// The response body is one tab's copy of the run; the state is what every other tab open on the same
// folder sees, and a number the user did not ask for has to read differently from one they did.
expect(session.gitFetch).toMatchObject({ trigger: 'click', state: 'ok', repos: 1, failed: 0 });
expect(ctx.persistSessionState).toHaveBeenCalled();
expect(ctx.broadcast).toHaveBeenCalled();
});
it('joins a run already going for the same folder instead of starting a second git process in the same clone', async () => {
git(dir, 'init', '-q', '-b', 'main');
let releaseRun = () => {};
const held = new Promise<void>((resolve) => (releaseRun = resolve));
let running = () => {};
const runStarted = new Promise<void>((resolve) => (running = resolve));
const fetches: string[] = [];
const runner: GitRunner = async (cwd, args) => {
if (args[0] === 'fetch') {
fetches.push(cwd);
running();
await held;
}
return execFileSyncGit(cwd, args);
};
const { app } = await setup({ git: runner });
const first = app.inject({ method: 'POST', url: '/api/sessions/test-session-1/git-fetch' });
await runStarted; // the run is going, and still going
// A click from another tab open on the same clone. Two fetches there would collide on git's ref lock, so
// this one joins the run already going rather than starting its own.
const second = app.inject({ method: 'POST', url: '/api/sessions/test-session-1/git-fetch' });
// Assert the join BEFORE releasing the run: the lane entry goes away when the run finishes, so releasing
// first would let the run complete before this request reached the lane, and it would then be entitled to
// start its own fetch — a test that passes for the wrong reason. One tick of the loop is enough for both
// handlers to reach the lane while the run is still held open.
await new Promise((resolve) => setImmediate(resolve));
expect(fetches).toHaveLength(1); // one run, and the second request is waiting on it, not working
releaseRun();
const a = (await first).json().data;
const b = (await second).json().data;
expect(b).toEqual(a); // both tabs get the same run's answer, not a partial one
});
it('404s an unknown session, running no fetch', async () => {
const { calls, fetches, runner } = recordingRunner();
const { app } = await setup({ git: runner });