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

+134 -62
View File
@@ -4,34 +4,50 @@
*
* The fetch module (src/git-fetch.ts) is the only place Codeman runs a git command that writes, and on a
* timer it is the only one that can run without being asked — which is why the whole feature is off until
* the user turns it on, in Settings → Bottom bar, per case in Session Options → Git.
* the user turns it on, in Settings → Bottom bar, per session in Session Options → Git.
*
* The scheduler holds no timers of its own. The server ticks it (`GIT_FETCH_TICK_INTERVAL_MS`) and every
* tick reads the app setting FRESH, so a live toggle takes effect on the next tick instead of at boot, and
* turning the feature off actually stops the fetching. A case can override both the switch and the
* interval; a case with no override follows the app.
* turning the feature off actually stops the fetching. A session can override both the switch and the
* interval; a session with no override follows the app.
*
* Rules the code keeps and the tests pin:
* - Nothing runs when nothing is on: no repository set is read and nothing is spawned, so a quiet
* install costs one settings read per tick. A case that turned itself on still runs even while the
* app-wide switch is off — that is what a per-case switch has to mean.
* - Remote and Docker cases are never fetched, exactly as the Git panel excludes them, and the Docker
* workspace roots are checked again every tick, so a case that became a Docker case stops fetching at
* the next tick.
* - One run per case at a time: a tick skips a case still being fetched rather than stacking runs on it.
* - At most `FETCH_CONCURRENCY` cases at a time across all of them, because each is a git process.
* install costs one settings read per tick. A session that turned itself on still runs even while the
* app-wide switch is off — that is what a per-session switch has to mean.
* - Remote and Docker sessions are never fetched, exactly as the Git panel excludes them, and the Docker
* workspace roots are checked again every tick, so a session whose case became a Docker case stops
* fetching at the next tick.
* - A folder is fetched by at most one run at a time, and the run belongs to the FOLDER, not to one
* session: two sessions open on the same folder are the same clone, so they share one run and both get
* its result. State is keyed by `resolve(workingDir)` — deliberately not `realpath`, because `realpath`
* is itself one of the unbounded fs calls rule #516 bounds; the bounded probe below is the fs read.
* Every session open on a folder is remembered, so closing the one holding the override does not stop
* the folder's fetching while another session is still on it.
* - Before anything is fetched, the folder is probed BOUNDED with the default options (rule #516): a case
* on a mount that stopped answering is skipped, never touched, and a probe that answers `unknown` is not
* read as "not a folder".
* - A schedule is stamped at SELECTION, when a session goes into `due`, not when its run starts, so a
* tick that is still running cannot pick the same folder again; and a tick is skipped while the previous
* one is still going, so the bound below is the bound for the whole tick, not per tick.
* - At most `FETCH_CONCURRENCY` git processes at a time across the whole tick, because each is a git
* process: ONE bound is shared by every run of the tick, so `FETCH_CONCURRENCY` is the tick's ceiling
* rather than each folder's own. The folders get their OWN bound (`mapLimited`), because one semaphore
* cannot serve both levels: a folder that holds a slot and then waits for one for its own fetch is
* waiting on itself, and the folders that fill the bound are exactly the ones blocking it.
* - The interval is timed from the START of a run, so a run that takes three minutes does not push the
* next one out by three minutes; it lands on the tick after its own interval elapses.
* - The per-repository limit and the git timeout stay the module's defaults (`MAX_REPOS`,
* `DEFAULT_GIT_TIMEOUT_MS`): both settings are per device and never reach the server, so the server
* cannot know what a device chose.
* - A failure is reported, not retried harder: the case keeps its reason and gets its next run at its
* - A failure is reported, not retried harder: the folder keeps its schedule and gets its next run at its
* own interval. A settings read that fails skips this tick only, because settings.json is rewritten in
* place and a kept fallback would hold the feature off until the next settings change.
*
* @module git-fetch-scheduler
*/
import { resolve } from 'node:path';
import { getErrorMessage } from './types.js';
import {
FETCH_CONCURRENCY,
@@ -43,9 +59,10 @@ import {
type GitFetchSettings,
type GitFetchStatus,
} from './git-fetch.js';
import { clampInt, DEFAULT_GIT_TIMEOUT_MS, MAX_REPOS, mapLimited } from './git-workspace-status.js';
import { clampInt, DEFAULT_GIT_TIMEOUT_MS, MAX_REPOS, createLimiter, mapLimited } from './git-workspace-status.js';
import type { PathProbeKind } from './utils/bounded-path-probe.js';
/** What the scheduler needs from a case: the facts a decision needs, and nothing else. */
/** What the scheduler needs from a session: the facts a decision needs, and nothing else. */
export interface GitFetchSessionView {
id: string;
workingDir: string;
@@ -62,13 +79,20 @@ export interface GitFetchPorts {
/** Read FRESH every tick; that is the whole point of the tick-driven shape. */
readSettings(): Promise<GitFetchSettings>;
dockerWorkspaces(): Promise<string[]>;
/**
* Bounded probe (rule #516), default options: a bulk scan keeps the stall cap, so this must not be called
* with `pastCap`. Anything that is not `directory` — including `unknown`, which is never `absent` — is
* skipped, not fetched.
*/
probePath(path: string): Promise<PathProbeKind>;
/** Called through `fetchWorkspaceOnce`, so a run already going for this folder is joined, not stacked. */
fetch(cwd: string, opts: GitFetchOptions): Promise<GitFetchReport>;
publish(id: string, status: GitFetchStatus): void;
log?(message: string): void;
}
/**
* What one case will actually do: its own override wins, otherwise the app-wide setting. The interval is
* What one session will actually do: its own override wins, otherwise the app-wide setting. The interval is
* clamped again here because a persisted value can predate the schema's bounds.
*/
export function effectiveGitFetch(
@@ -88,78 +112,126 @@ export function effectiveGitFetch(
export class GitFetchScheduler {
private readonly ports: GitFetchPorts;
/** When each case last started a run; timed from the start, not the end. */
/** When each FOLDER last started a run; timed from the start, not the end. */
private readonly lastRunAt = new Map<string, number>();
private readonly inFlight = new Set<string>();
/** Session → folder, so `remove()` can keep the folder's schedule while other sessions are still on it. */
private readonly folderOf = new Map<string, string>();
private running = false;
private stopped = false;
constructor(ports: GitFetchPorts) {
this.ports = ports;
}
/** One tick: fetch every case that is on and due. Returns how many ran. */
/** One tick: fetch every folder whose sessions are on and due. Returns how many folders ran. */
async tick(now = Date.now()): Promise<number> {
if (this.stopped) return 0;
let settings: GitFetchSettings;
if (this.stopped || this.running) return 0;
this.running = true;
try {
settings = await this.ports.readSettings();
} catch (err) {
// A read can land mid-write. Skipping this tick costs one interval; deciding from a fallback would
// hold the feature off until the next settings change.
this.ports.log?.(`git fetch: settings unreadable, tick skipped: ${getErrorMessage(err)}`);
return 0;
}
const due: GitFetchSessionView[] = [];
for (const id of this.ports.sessionIds()) {
const view = this.ports.getSession(id);
if (!view || view.remote || view.docker) continue;
const effective = effectiveGitFetch(view, settings);
if (!effective.enabled || this.inFlight.has(id)) continue;
const started = this.lastRunAt.get(id);
if (started !== undefined && now - started < effective.intervalSeconds * 1000) continue;
due.push(view);
}
if (!due.length) return 0;
// Only now, because nothing above needs it: a quiet tick does not read the Docker case list.
const dockerWorkspaces = await this.ports.dockerWorkspaces();
await mapLimited(due, FETCH_CONCURRENCY, async (view) => {
this.inFlight.add(view.id);
// The clock starts at the run, so a slow repository does not shift the case's own schedule.
this.lastRunAt.set(view.id, now);
let settings: GitFetchSettings;
try {
const report = await this.ports.fetch(view.workingDir, {
dockerWorkspaces,
maxRepos: MAX_REPOS,
timeoutMs: DEFAULT_GIT_TIMEOUT_MS,
});
this.ports.publish(view.id, gitFetchStatusFrom(report, 'interval'));
settings = await this.ports.readSettings();
} catch (err) {
// The run that failed keeps its reason in the panel's own row; here only the case-level failure is
// worth a line, because nothing else would say a scheduled fetch could not run at all.
this.ports.log?.(`git fetch: ${view.id} could not run: ${getErrorMessage(err)}`);
} finally {
this.inFlight.delete(view.id);
// A read can land mid-write. Skipping this tick costs one interval; deciding from a fallback would
// hold the feature off until the next settings change.
this.ports.log?.(`git fetch: settings unreadable, tick skipped: ${getErrorMessage(err)}`);
return 0;
}
});
return due.length;
// One entry per folder, holding every session open on it. Stamped here, at selection: a tick that is
// still running must not pick the same folder again, and a folder already being fetched is not picked
// at all.
const due = new Map<string, GitFetchSessionView[]>();
for (const id of this.ports.sessionIds()) {
const view = this.ports.getSession(id);
if (!view || view.remote || view.docker) continue;
const effective = effectiveGitFetch(view, settings);
if (!effective.enabled) continue;
const folder = resolve(view.workingDir);
// Recorded for EVERY session on the folder, not just the one that got picked, so `remove()` can still
// see who is open on it when the session holding the override is the one that closes.
this.folderOf.set(id, folder);
// A session on a folder this tick already picked is the same clone: it joins that folder's run rather
// than starting its own. Two fetches in one clone collide on git's ref lock, so one run has to serve
// every session open on the folder.
const group = due.get(folder);
if (group) {
group.push(view);
continue;
}
if (this.inFlight.has(folder)) continue;
const started = this.lastRunAt.get(folder);
if (started !== undefined && now - started < effective.intervalSeconds * 1000) continue;
const kind = await this.ports.probePath(view.workingDir);
if (kind !== 'directory') {
this.ports.log?.(`git fetch: ${view.id} skipped, folder answered ${kind}`);
continue;
}
this.inFlight.add(folder);
// The clock starts at the selection, so a slow folder does not shift its own schedule.
this.lastRunAt.set(folder, now);
due.set(folder, [view]);
}
if (!due.size) return 0;
// Only now, because nothing above needs it: a quiet tick does not read the Docker case list.
const dockerWorkspaces = await this.ports.dockerWorkspaces();
// ONE bound for the git processes of the whole tick, handed to every run so no folder gets its own:
// each fetch is one git process, and a folder can hold up to `MAX_REPOS_LIMIT` of them, so a bound per
// run would let `FETCH_CONCURRENCY` folders × `MAX_REPOS_LIMIT` processes run at once.
const fetches = createLimiter(FETCH_CONCURRENCY);
// The folders get their OWN bound, because the same semaphore cannot serve both levels: a folder that
// holds a slot and then waits for a slot for its own fetch is waiting on itself — once
// `FETCH_CONCURRENCY` folders fill the bound, the ones holding slots are exactly the ones that can
// never get one back. Both bounds are `FETCH_CONCURRENCY`, so the tick still never runs more git
// processes than the module's ceiling.
await mapLimited([...due.entries()], FETCH_CONCURRENCY, async ([folder, group]) => {
try {
const report = await this.ports.fetch(folder, {
dockerWorkspaces,
maxRepos: MAX_REPOS,
timeoutMs: DEFAULT_GIT_TIMEOUT_MS,
limiter: fetches,
});
for (const view of group) this.ports.publish(view.id, gitFetchStatusFrom(report, 'interval'));
} catch (err) {
// The run that failed keeps its reason in the panel's own row; here only the folder-level failure is
// worth a line, because nothing else would say a scheduled fetch could not run at all.
this.ports.log?.(`git fetch: ${folder} could not run: ${getErrorMessage(err)}`);
} finally {
this.inFlight.delete(folder);
}
});
return due.size;
} finally {
this.running = false;
}
}
/** A case that closed or was detached: its schedule and its in-flight flag go with it. */
/**
* A session that closed or was detached: its schedule goes with it, but the FOLDER's stays while another
* session is still open on it — the run belongs to the clone, not to the session that asked for it.
*/
remove(id: string): void {
this.lastRunAt.delete(id);
this.inFlight.delete(id);
const folder = this.folderOf.get(id);
this.folderOf.delete(id);
if (!folder) return;
const stillOpen = [...this.folderOf.values()].includes(folder);
if (!stillOpen) {
this.lastRunAt.delete(folder);
this.inFlight.delete(folder);
}
}
stop(): void {
this.stopped = true;
this.lastRunAt.clear();
this.inFlight.clear();
this.folderOf.clear();
}
/** Cases the scheduler still remembers a run for, so a test can see a `remove()` actually took. */
/** Folders the scheduler still remembers a run for, so a test can see a `remove()` actually took. */
tracked(): number {
return new Set([...this.lastRunAt.keys(), ...this.inFlight]).size;
}
+76 -10
View File
@@ -7,7 +7,10 @@
* fetch, so a number that is stale can only be made current by fetching. This is the one place Codeman
* runs a git command that writes, and on a timer it is also the only one that can run without being asked.
* Nothing here commits, checks out, merges or touches the working tree: `git fetch` updates remote-tracking
* refs and nothing else, so an agent working in the same repository is not disturbed.
* refs and nothing else, and `FETCH_CONFIG_ARGS` keeps it from starting a gc in a repository an agent is
* already using. It is not harmless in the way a read is: a fetch holds git's ref lock, so an agent's own
* `git pull` in the same clone can collide with it (`cannot lock ref`), which is why one folder is fetched
* by at most one run at a time (`fetchWorkspaceOnce`).
*
* Same split as git-clone.ts: the pure half (`buildFetchArgs`, `describeFetchFailure`,
* `gitFetchSettingsFrom`, `gitFetchStatusFrom`) holds every decision and tests without spawning, and the IO
@@ -17,10 +20,15 @@
* - The repository set is `resolveWorkspaceRepos`, the single source the panel and the diff route
* already use, so Fetch cannot reach a repository the panel would not show. The Docker rule is
* checked before any git runs: a repository a container can write to is refused outright, because a
* planted config would run on the host — and `git fetch` is exactly the command that runs a
* repository's own hooks (`fetch-post`), which is why a timer only runs it after the user opts in.
* - Bounded: the git timeout from Settings → Bottom bar per repository, `FETCH_CONCURRENCY` at a time,
* `MAX_REPOS_LIMIT` repositories at most, and a hard `timeout` on each spawn.
* planted config would run on the host — and `git fetch` is exactly the command that can run a
* repository's own hooks (`reference-transaction`, and `pre-auto-gc` through the automatic maintenance
* that follows a fetch) and its config commands such as `credential.helper`, which is why a timer only
* runs it after the user opts in.
* - One run per folder at a time: `fetchWorkspaceOnce` joins a run already going for the same folder
* instead of starting a second one, because two fetches in one clone collide on the ref lock.
* - Bounded: the git timeout from Settings → Bottom bar per repository, `MAX_REPOS_LIMIT` repositories at
* most, a hard `timeout` on each spawn, and `FETCH_CONCURRENCY` git processes at a time for the whole
* run — one `Limiter`, shared by the scheduler's tick, not one bound per folder.
* - Non-interactive: `GIT_TERMINAL_PROMPT=0` and BatchMode ssh, so a private repository that needs a
* login fails fast instead of hanging the request on an invisible username prompt. Codeman collects
* no credentials; a repository the user's own git is already signed in to works because that is the
@@ -32,12 +40,13 @@
*/
import { execFile } from 'node:child_process';
import { basename, relative } from 'node:path';
import { basename, relative, resolve } from 'node:path';
import { promisify } from 'node:util';
import { classifyGitFailure, gitNonInteractiveEnv, sanitizeGitOutput, type GitFailureCode } from './git-clone.js';
import {
createLimiter,
DEFAULT_GIT_TIMEOUT_MS,
mapLimited,
type Limiter,
resolveOverviewLimits,
resolveWorkspaceRepos,
type GitRunner,
@@ -45,11 +54,25 @@ import {
const execFileAsync = promisify(execFile);
/** Fetches in flight at once: each is one git process, and a folder can hold up to `MAX_REPOS_LIMIT`. */
/**
* Git processes in flight at once, for one run — and for a whole scheduler tick, because the tick hands ONE
* bound to every run (see `createLimiter`): each fetch is one git process, and a folder can hold up to
* `MAX_REPOS_LIMIT`. The tick bounds its folders separately, because the same semaphore cannot serve both
* levels — a folder holding a slot would then wait for a slot its own fetch needs.
*/
export const FETCH_CONCURRENCY = 4;
/** A fetch on a busy remote can print a lot; a bound on what we will hold. */
const MAX_FETCH_OUTPUT_BYTES = 2 * 1024 * 1024;
/**
* Config overrides set ahead of the argv, so an unattended fetch cannot do more than move refs: no
* fsmonitor hook, and no automatic maintenance or gc. A fetch is the git command that can run a
* repository's own hooks (`reference-transaction`, and `pre-auto-gc` through the maintenance that follows a
* fetch), and a gc started by a timer in a repository an agent is working in is exactly the kind of extra
* write this module must not do. Kept as a const so a test can pin it without spawning git.
*/
export const FETCH_CONFIG_ARGS = ['-c', 'core.fsmonitor=false', '-c', 'maintenance.auto=false', '-c', 'gc.auto=0'];
/** The interval the user can turn on (Settings → Bottom bar, and Session Options → Git): bounds and default. */
export const DEFAULT_GIT_FETCH_INTERVAL_SECONDS = 300;
export const MIN_GIT_FETCH_INTERVAL_SECONDS = 30;
@@ -85,6 +108,10 @@ export function buildFetchArgs(): string[] {
}
/**
* The `git` process also gets the `FETCH_CONFIG_ARGS` overrides ahead of this argv; they are kept separate
* so pinning the verb here does not have to say anything about maintenance, and so both can be tested
* without spawning.
*
* A repository with no remote is a fact, not a fault. Git's wordings for it differ by version — 2.47
* exits quietly and this code is never reached, older git says "no default remote repository" — so the
* branch is pinned on literal fixtures and the code the panel shows is the code this writes.
@@ -126,7 +153,7 @@ export function describeFetchFailure(err: unknown): FetchFailure {
/** Fetch is allowed to write refs, so this runner does not carry `--no-optional-locks`: fetch takes a required lock anyway. */
export const runFetch: GitRunner = async (cwd, args, opts) => {
const { stdout } = await execFileAsync('git', ['-c', 'core.fsmonitor=false', ...args], {
const { stdout } = await execFileAsync('git', [...FETCH_CONFIG_ARGS, ...args], {
cwd,
timeout: opts?.timeoutMs ?? DEFAULT_GIT_TIMEOUT_MS,
maxBuffer: MAX_FETCH_OUTPUT_BYTES,
@@ -198,6 +225,12 @@ export interface GitFetchOptions {
dockerWorkspaces?: string[];
maxRepos?: number;
timeoutMs?: number;
/**
* The shared slot count for a whole scheduler tick. Passed in so `FETCH_CONCURRENCY` bounds the tick
* rather than each folder separately; without it, a tick over more than `FETCH_CONCURRENCY` folders could
* have that many git processes going at once inside each one.
*/
limiter?: Limiter;
}
export const emptyReport = (state: GitFetchReport['state'], extra: Partial<GitFetchReport> = {}): GitFetchReport => ({
@@ -230,7 +263,8 @@ export async function fetchGitWorkspaceOverview(cwd: string, opts: GitFetchOptio
if (!dirs.length) return emptyReport('not-a-repo');
const { timeoutMs } = resolveOverviewLimits(opts);
const runner = opts.git ?? runFetch;
const failures = await mapLimited(dirs, FETCH_CONCURRENCY, (dir) => fetchOne(dir, runner, timeoutMs));
const run = opts.limiter ?? createLimiter(FETCH_CONCURRENCY);
const failures = await run(dirs, (dir) => fetchOne(dir, runner, timeoutMs));
const repos: GitFetchEntry[] = dirs.map((dir, i) => {
const base = { name: basename(dir), path: relative(cwd, dir) || '.' };
const failure = failures[i];
@@ -244,3 +278,35 @@ export async function fetchGitWorkspaceOverview(cwd: string, opts: GitFetchOptio
fetchedAt: Date.now(),
};
}
/**
* Runs already going, keyed by the resolved folder. A request for a folder another run is already working on
* JOINS that run instead of starting a second `git fetch` in the same clone: two fetches there collide on
* git's ref lock (`cannot lock ref 'refs/remotes/origin/main'`), which is what a click during a tick would
* otherwise do. A joiner gets the run already going's answer, including its limits — the point is not to
* fetch twice, not to fetch with the freshest options.
*
* Keyed by `resolve`, not `realpath`, because `realpath` is itself one of the unbounded fs calls rule #516
* bounds for user-chosen paths; the scheduler probes the folder bounded and calls this. Two paths that
* resolve the same are the same folder here; a workspace reached through a symlink resolves to the link, so
* a link and its target can be fetched by two runs — the accepted trade for keeping the probe bounded.
*/
const lane = new Map<string, Promise<GitFetchReport>>();
export async function fetchWorkspaceOnce(folder: string, opts: GitFetchOptions = {}): Promise<GitFetchReport> {
const key = resolve(folder);
const going = lane.get(key);
if (going) return going;
const run = fetchGitWorkspaceOverview(key, opts).finally(() => {
// Only the run that is still the lane's own: a later run for the same folder must not be dropped by an
// earlier one finishing late.
if (lane.get(key) === run) lane.delete(key);
});
lane.set(key, run);
return run;
}
/** Test seam: an in-memory lane is not cleared by stopping the scheduler, so a test has to clear it itself. */
export function clearGitFetchLane(): void {
lane.clear();
}
+48 -1
View File
@@ -597,7 +597,54 @@ export async function discoverChildRepos(
const discoveryCache = new Map<string, { at: number; value: { dirs: string[]; truncated: boolean } }>();
/** Run `fn` over `items` with at most `limit` in flight, keeping the input order. Shared with the fetch path. */
/**
* A slot count fixed ONCE, so callers share it instead of each getting its own bound (see the fetch path):
* the count lives in the closure, so `limit` is the ceiling for everything going through THIS limiter at
* once, not for each call to it — which is what makes a scheduler tick's bound honest.
* ⚠️ One limiter cannot bound two levels of the same work: a caller that holds a slot and then waits for
* another slot for the work IT starts cannot get one, because the holders are exactly the ones waiting. The
* outer level needs its own bound (`mapLimited`), which is why the tick shares one for fetches and keeps a
* separate one for folders.
*/
export type Limiter = <T, R>(items: T[], fn: (item: T) => Promise<R>) => Promise<R[]>;
export function createLimiter(limit: number): Limiter {
let inUse = 0;
const waiting: Array<() => void> = [];
const acquire = (): Promise<void> => {
if (inUse < limit) {
inUse++;
return Promise.resolve();
}
return new Promise<void>((done) => {
waiting.push(() => {
inUse++;
done();
});
});
};
const release = () => {
inUse--;
const next = waiting.shift();
if (next) next();
};
return async <T, R>(items: T[], fn: (item: T) => Promise<R>): Promise<R[]> => {
const out: R[] = new Array(items.length);
await Promise.all(
items.map(async (item, i) => {
await acquire();
try {
out[i] = await fn(item);
} finally {
release();
}
})
);
return out;
};
}
/** Run `fn` over `items` with at most `limit` in flight, keeping the input order. Per call; `createLimiter` shares one bound across calls. */
export async function mapLimited<T, R>(items: T[], limit: number, fn: (item: T) => Promise<R>): Promise<R[]> {
const out: R[] = new Array(items.length);
let next = 0;
+7 -3
View File
@@ -893,14 +893,18 @@ export interface SessionState {
/** What the keep-warm is doing right now; ephemeral, never restored. */
keepWarm?: KeepWarmStatus;
/**
* Git fetch on an interval for this case's repositories; absent = follow the app-wide
* Git fetch on an interval for this session's repositories; absent = follow the app-wide
* `gitFetchEnabled` default. Restored on a restart, like `keepWarmEnabled`, because the choice is the
* user's and outlives the run.
*/
gitFetchEnabled?: boolean;
/** Seconds between this case's runs; absent = follow the app-wide `gitFetchIntervalSeconds` default. */
/** Seconds between this session's runs; absent = follow the app-wide `gitFetchIntervalSeconds` default. */
gitFetchIntervalSeconds?: number;
/** What this case's last fetch did; ephemeral, never restored. */
/**
* What this session's last fetch did. Written to disk with the rest of the record, but the restore copies
* only the fields its own config lists and this is not one of them, so it is the panel's truth for the
* life of the session, not across a restart.
*/
gitFetch?: GitFetchStatus;
/** Pinned to the top of the session manager list (COD-139) */
pinned?: boolean;
+10 -10
View File
@@ -541,22 +541,22 @@
// Git fetch on an interval (App Settings → Bottom bar, Session Options → Git, and the save toast in
// session-ui.js). 'Status' and 'Git' are not added: the first already has a key, the second stays a
// technical name, and the status row's text is written at runtime.
"Whether Codeman fetches this case's repositories on a timer.": '{name} 是否按间隔拉取此案例的仓库。',
"Whether Codeman fetches this session's repositories on a timer.": '{name} 是否按间隔拉取此会话的仓库。',
'Git fetch on an interval': 'Git 定时拉取',
'Runs `git fetch` for the repositories the Git window lists on a timer, so the "behind" number is current without a click. Off by default: this is the one git command Codeman runs that writes, and the one that runs a repository\'s own hooks, so nothing runs on a timer until you turn it on. A case can override both the switch and the interval in Session Options → Git.':
'按间隔为 Git 窗口列出的仓库运行拉取,这样“落后”的数字无需点击就是最新的。默认关闭:这是 {name} 运行的唯一会写入的 git 命令,也是唯一会运行仓库自身钩子的命令,所以在您开启之前不会有任何定时运行。案例可以在“会话选项 → Git”中同时覆盖开关和间隔。',
'Runs `git fetch` for the repositories the Git window lists on a timer, so the "behind" number is current without a click. Off by default: this is the one git command Codeman runs that writes, and the one that runs a repository\'s own hooks, so nothing runs on a timer until you turn it on. A session can override both the switch and the interval in Session Options → Git.':
'按间隔为 Git 窗口列出的仓库运行拉取,这样“落后”的数字无需点击就是最新的。默认关闭:这是 {name} 运行的唯一会写入的 git 命令,也是唯一会运行仓库自身钩子的命令,所以在您开启之前不会有任何定时运行。会话可以在“会话选项 → Git”中同时覆盖开关和间隔。',
'Git fetch: how often': 'Git 定时拉取:多久一次',
'Seconds between runs for a case that has no override of its own (30 to 3600, default 300). Only a case that is on — here, or in Session Options → Git — is fetched.':
'没有自己覆盖设置的案例,两次运行之间的秒数(30 到 3600,默认 300)。只有已开启的案例——这里,或在“会话选项 → Git”——才会被拉取。',
'Seconds between runs for a session that has no override of its own (30 to 3600, default 300). Only a session that is on — here, or in Session Options → Git — is fetched, and sessions sharing one folder are fetched by one run.':
'没有自己覆盖设置的会话,两次运行之间的秒数(30 到 3600,默认 300)。只有已开启的会话——这里,或在“会话选项 → Git”——才会被拉取,共用一个文件夹的会话由一次运行拉取。',
'Fetch on an interval': '按间隔拉取',
"Fetching is the only git command Codeman runs that writes, and the only one that runs a repository's own hooks, so nothing runs on a timer until you turn it on — here, or in App Settings → Bottom bar. The panel's Fetch button is always just a click.":
'拉取是 {name} 运行的唯一会写入的 git 命令,也是唯一会运行仓库自身钩子的命令,所以在您开启之前(这里,或“应用设置 → 底部栏”)不会有任何定时运行。该窗口中的拉取按钮始终只是一次点击。',
'Follow the app setting': '跟随应用设置',
'Uses the app-wide switch and interval for this case. Switch it off to pick your own.':
'为此案例使用应用级的开关和间隔。关闭它即可自行选择。',
'Fetch this case': '拉取此案例',
'Runs on its own even while the app-wide switch is off: a case that turned itself on means exactly that.':
'即使应用级开关关闭也自行运行:一个自己开启的案例正是这个意思。',
'Uses the app-wide switch and interval for this session. Switch it off to pick your own.':
'为此会话使用应用级的开关和间隔。关闭它即可自行选择。',
'Fetch this session': '拉取此会话',
"Runs on its own even while the app-wide switch is off: a session that turned itself on means exactly that. Sessions sharing one folder share one run, and each sees that run's result.":
'即使应用级开关关闭也自行运行:一个自己开启的会话正是这个意思。共用一个文件夹的会话共享一次运行,每个会话都会看到该运行的结果。',
'How often': '多久一次',
'30 sec to 1 hr, timed from the start of a run, so a slow repository does not push the next one out.':
'30 秒到 1 小时,从一次运行的开始计时,所以较慢的仓库不会把下一次推远。',
+11 -9
View File
@@ -1332,8 +1332,9 @@
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 3v18h18"/><polyline points="7 14 11 10 14 13 20 7"/></svg>
<span>Summary</span>
</button>
<!-- Git is last: a case that has not turned fetching on shows nothing here, so the section only
earns its place once the feature is in use. -->
<!-- Git is last: the section shows for every local session, but until the interval feature is
in use it holds one toggle and a status row that has never run, so it earns its place at the
end of the rail rather than among the tabs a session uses every day. -->
<button type="button" class="set-rail-item" data-tab="git" onclick="app.switchOptionsTab('git')">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M6 3v12"/><path d="M18 9a9 9 0 0 1-9 9"/></svg>
<span>Git</span>
@@ -1733,13 +1734,14 @@
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M6 3v12"/><path d="M18 9a9 9 0 0 1-9 9"/></svg>
<h2>Git</h2>
</div>
<p class="set-section-blurb">Whether Codeman fetches this case's repositories on a timer.</p>
<p class="set-section-blurb">Whether Codeman fetches this session's repositories on a timer.</p>
<div class="set-group">
<div class="set-group-head"><h4>Fetch on an interval</h4></div>
<p class="set-group-hint">Fetching is the only git command Codeman runs that writes, and the only one that runs a repository's own hooks, so nothing runs on a timer until you turn it on &mdash; here, or in App Settings &rarr; Bottom bar. The panel's Fetch button is always just a click.</p>
<div class="set-group-body">
<!-- Live from the case's own last run (session-ui.js: updateGitFetchStatus). -->
<!-- Live from the session's own last run (session-ui.js: updateGitFetchStatus), which is the
run its folder got: several sessions on one folder share one run and read the same row. -->
<div class="set-row">
<div class="set-row-text">
<span class="set-row-label">Status</span>
@@ -1749,14 +1751,14 @@
<div class="set-row">
<div class="set-row-text">
<span class="set-row-label">Follow the app setting</span>
<span class="set-row-desc">Uses the app-wide switch and interval for this case. Switch it off to pick your own.</span>
<span class="set-row-desc">Uses the app-wide switch and interval for this session. Switch it off to pick your own.</span>
</div>
<label class="switch switch-sm"><input type="checkbox" id="modalGitFetchFollow" checked onchange="app.autoSaveGitFetch()"><span class="slider"></span></label>
</div>
<div class="set-row" id="gitFetchEnabledRow">
<div class="set-row-text">
<span class="set-row-label">Fetch this case</span>
<span class="set-row-desc">Runs on its own even while the app-wide switch is off: a case that turned itself on means exactly that.</span>
<span class="set-row-label">Fetch this session</span>
<span class="set-row-desc">Runs on its own even while the app-wide switch is off: a session that turned itself on means exactly that. Sessions sharing one folder share one run, and each sees that run's result.</span>
</div>
<label class="switch switch-sm"><input type="checkbox" id="modalGitFetchEnabled" onchange="app.autoSaveGitFetch()"><span class="slider"></span></label>
</div>
@@ -2278,14 +2280,14 @@
<div class="set-row" data-search="git fetch interval timer behind remote tracking refs poll run automatically">
<div class="set-row-text">
<span class="set-row-label">Git fetch on an interval <span class="set-scope">synced</span></span>
<span class="set-row-desc">Runs `git fetch` for the repositories the Git window lists on a timer, so the "behind" number is current without a click. Off by default: this is the one git command Codeman runs that writes, and the one that runs a repository's own hooks, so nothing runs on a timer until you turn it on. A case can override both the switch and the interval in Session Options → Git.</span>
<span class="set-row-desc">Runs `git fetch` for the repositories the Git window lists on a timer, so the "behind" number is current without a click. Off by default: this is the one git command Codeman runs that writes, and the one that runs a repository's own hooks, so nothing runs on a timer until you turn it on. A session can override both the switch and the interval in Session Options → Git.</span>
</div>
<label class="switch switch-sm"><input type="checkbox" id="appSettingsGitFetchEnabled"><span class="slider"></span></label>
</div>
<div class="set-row" data-search="git fetch how often seconds between runs interval case override session options">
<div class="set-row-text">
<span class="set-row-label">Git fetch: how often <span class="set-scope">synced</span></span>
<span class="set-row-desc">Seconds between runs for a case that has no override of its own (30 to 3600, default 300). Only a case that is on — here, or in Session Options → Git — is fetched.</span>
<span class="set-row-desc">Seconds between runs for a session that has no override of its own (30 to 3600, default 300). Only a session that is on — here, or in Session Options → Git — is fetched, and sessions sharing one folder are fetched by one run.</span>
</div>
<input type="number" id="appSettingsGitFetchIntervalSeconds" class="set-num" value="300" min="30" max="3600" step="1">
</div>
+13 -4
View File
@@ -3048,7 +3048,7 @@ Object.assign(CodemanApp.prototype, {
if (!session) return;
const app = this.loadAppSettingsFromStorage() || {};
// No choice of its own on either field means the case follows the app. A partial one — its own switch,
// No choice of its own on either field means the session follows the app. A partial one — its own switch,
// the app's interval — means it does not, and the row that still follows shows the app's value.
const follows = session.gitFetchEnabled === undefined && session.gitFetchIntervalSeconds === undefined;
const enabled = follows ? app.gitFetchEnabled === true : session.gitFetchEnabled ?? false;
@@ -3069,7 +3069,7 @@ Object.assign(CodemanApp.prototype, {
run.state === 'ok' ? `${run.repos} repositories` : run.state,
run.failed ? `${run.failed} failed` : '',
]
: [follows ? 'Nothing fetched on a timer yet, and this case follows the app setting.' : 'Nothing fetched on a timer yet for this case.'];
: [follows ? 'Nothing fetched on a timer yet, and this session follows the app setting.' : 'Nothing fetched on a timer yet for this session.'];
statusEl.textContent = parts.filter(Boolean).join(' · ');
statusEl.hidden = false;
},
@@ -3096,9 +3096,18 @@ Object.assign(CodemanApp.prototype, {
),
};
try {
const data = await this._apiPost(`/api/sessions/${this.editingSessionId}/git-fetch-settings`, body);
// `_apiPost` hands back the Response without checking it, so a refused body (a 400) still read as a
// save. `_apiJson` is the one that says whether the call went through, and null here means it did not.
const data = await this._apiJson(`/api/sessions/${this.editingSessionId}/git-fetch-settings`, {
method: 'POST',
body,
});
if (!data) {
this.showToast('Failed to save Git fetch settings', 'error');
return;
}
const session = this.sessions.get(this.editingSessionId);
const saved = data?.gitFetchSettings;
const saved = data.gitFetchSettings;
if (session && saved) {
session.gitFetchEnabled = saved.enabled ?? undefined;
session.gitFetchIntervalSeconds = saved.intervalSeconds ?? undefined;
+25 -15
View File
@@ -16,10 +16,10 @@
import type { FastifyInstance } from 'fastify';
import { ApiErrorCode, createErrorResponse, getErrorMessage, type ApiResponse } from '../../types.js';
import { redactGitCredentials } from '../../git-clone.js';
import { emptyReport, fetchGitWorkspaceOverview, type GitFetchReport } from '../../git-fetch.js';
import { emptyReport, fetchWorkspaceOnce, gitFetchStatusFrom, type GitFetchReport } from '../../git-fetch.js';
import { readDockerCases } from '../../docker-hosts.js';
import { getDataDir } from '../../config/instance.js';
import { findSessionOrFail } from '../route-helpers.js';
import { findSessionOrFail, persistAndBroadcastSession } from '../route-helpers.js';
import {
emptyOverview,
findWorkspaceRepo,
@@ -32,7 +32,7 @@ import {
type GitRunner,
type GitWorkspaceOverview,
} from '../../git-workspace-status.js';
import type { SessionPort } from '../ports/index.js';
import type { EventPort, SessionPort } from '../ports/index.js';
/**
* Host paths of every Docker case workspace: repositories at or inside these are never inspected.
@@ -58,7 +58,9 @@ function limitsFrom(query: { maxRepos?: unknown; timeout?: unknown }) {
export function registerGitStatusRoutes(
app: FastifyInstance,
ctx: SessionPort,
// EventPort as well as SessionPort: the fetch route publishes the run it did, and a route that broadcasts
// has to say so in its own signature.
ctx: SessionPort & EventPort,
git?: GitRunner,
dockerWorkspaces: () => Promise<string[]> = defaultDockerWorkspaces
): void {
@@ -131,9 +133,14 @@ export function registerGitStatusRoutes(
// Fetch, and only because the user clicked Fetch in the panel (src/git-fetch.ts): one
// `git fetch --quiet` per repository the panel would show, through the same resolver so the two cannot
// disagree about which repositories belong to the workspace. This is the only git command Codeman runs
// that writes, and it writes remote-tracking refs only — nothing commits, checks out or merges, so an
// agent working in the same repository is not disturbed. Remote and Docker sessions answer `unsupported`
// like the status route, and a repository a container can write to is refused before any git runs.
// that writes, and it writes remote-tracking refs only — nothing commits, checks out or merges. It is not
// as harmless as a read though: a fetch holds git's ref lock, so an agent's own `git pull` in the same
// clone can collide with it, which is why the run goes through `fetchWorkspaceOnce` — a click while a tick
// is already fetching this folder joins that run instead of starting a second one. It can also run a
// repository's own hooks (`reference-transaction`, `pre-auto-gc` through the maintenance git runs after a
// fetch) and its config commands such as `credential.helper`, so the `FETCH_CONFIG_ARGS` overrides turn
// maintenance and gc off, and a repository a container can write to is refused before any git runs. Remote
// and Docker sessions answer `unsupported` like the status route.
app.post('/api/sessions/:id/git-fetch', async (req): Promise<ApiResponse<GitFetchReport>> => {
const { id } = req.params as { id: string };
const query = req.query as { maxRepos?: unknown; timeout?: unknown };
@@ -141,13 +148,16 @@ export function registerGitStatusRoutes(
const session = findSessionOrFail(ctx, id, req);
if (session.remote) return { success: true, data: emptyReport('unsupported', { reason: 'remote' }) };
if (session.docker) return { success: true, data: emptyReport('unsupported', { reason: 'docker' }) };
return {
success: true,
data: await fetchGitWorkspaceOverview(session.workingDir, {
git,
...limits,
dockerWorkspaces: await dockerWorkspaces(),
}),
};
const report = await fetchWorkspaceOnce(session.workingDir, {
git,
...limits,
dockerWorkspaces: await dockerWorkspaces(),
});
// The response answers the click, and the run is also the session's current truth: what the panel's
// "behind" number is based on. Publishing it is what the api docs say a click does, and what makes a
// second tab open on the same folder see the same run — including a click that joined a tick already
// going, where the row has to say who last fetched.
if (session.setGitFetchStatus(gitFetchStatusFrom(report, 'click'))) persistAndBroadcastSession(ctx, session);
return { success: true, data: report };
});
}
+7 -5
View File
@@ -3611,11 +3611,13 @@ export function registerSessionRoutes(
// ========== Git fetch on an interval ==========
// Per-case override for the fetch interval (src/git-fetch-scheduler.ts). `null` on either field clears
// the case's choice so the app-wide setting applies again, and a field left out of the body leaves the
// other half as it was: the Session Options rows save on their own. Refused for a remote or Docker
// session, exactly as the Git panel excludes them — a container can write to the repository it mounts,
// so git there would run that repository's own configuration on the host.
// Per-session override for the fetch interval (src/git-fetch-scheduler.ts), which is what the Session
// Options rows say it is: two sessions on one folder can hold different choices, and that folder is
// fetched when the first of them is due. `null` on either field clears this session's choice so the
// app-wide setting applies again, and a field left out of the body leaves the other half as it was: the
// Session Options rows save on their own. Refused for a remote or Docker session, exactly as the Git
// panel excludes them — a container can write to the repository it mounts, so git there would run that
// repository's own configuration on the host.
app.post('/api/sessions/:id/git-fetch-settings', async (req) => {
const { id } = req.params as { id: string };
const body = parseBody(GitFetchSettingsSchema, req.body, 'Invalid request body');
+5 -3
View File
@@ -1492,9 +1492,11 @@ export const SettingsUpdateSchema = z
* Git fetch on an interval (src/git-fetch-scheduler.ts): `git fetch` for the repositories the Git panel
* lists, run on a timer instead of only on the panel's Fetch click. SYNCED, default OFF, because the
* server is what runs it: `git fetch` is the only git command Codeman runs that writes, and the only
* one that runs a repository's own hooks, so nothing runs on a timer until the user turns this on.
* Cap in seconds between runs (30 to 3600, default 300). A case can override both in Session Options
* → Git; `null` there clears the override.
* one that can run a repository's own hooks (`reference-transaction`, `pre-auto-gc` through the
* automatic maintenance a fetch triggers, config commands such as `credential.helper`), so nothing runs
* on a timer until the user turns this on. Cap in seconds between runs (30 to 3600, default 300). A
* session can override both in Session Options → Git; `null` there clears the override, and sessions
* sharing one folder are fetched by one run.
*/
gitFetchEnabled: z.boolean().optional(),
gitFetchIntervalSeconds: z.number().int().min(30).max(3600).optional(),
+11 -3
View File
@@ -124,8 +124,9 @@ import { AI_CHECK_MODEL } from '../config/ai-defaults.js';
import { approvalInbox } from './approval-inbox.js';
import { SCREEN_BLOCKED_SOURCE } from './screen-blocked.js';
import { PromptCacheKeepWarmService } from '../prompt-cache-keepwarm.js';
import { fetchGitWorkspaceOverview } from '../git-fetch.js';
import { fetchWorkspaceOnce } from '../git-fetch.js';
import { GitFetchScheduler } from '../git-fetch-scheduler.js';
import { probePathKind } from '../utils/bounded-path-probe.js';
import { defaultDockerWorkspaces } from './routes/git-status-routes.js';
import { composerIsEmpty, plainFrame } from '../session-submit-verifier.js';
import { detectUsageLimitPause } from '../usage-limit-patterns.js';
@@ -405,12 +406,19 @@ export class WebServer extends EventEmitter {
readSettings: readGitFetchSettings,
// The same Docker list the panel's routes use: the rule has to be one rule.
dockerWorkspaces: defaultDockerWorkspaces,
fetch: (cwd, opts) => fetchGitWorkspaceOverview(cwd, opts),
// Bounded probe, DEFAULT options (rule #516): a tick is a bulk scan, so it keeps the stall cap and must
// never be handed `pastCap`. A folder on a mount that stopped answering is skipped, not fetched.
probePath: (path) => probePathKind(path),
// Through the join, so the tick and the panel's Fetch button are the same run: a click on a folder the
// tick is already fetching joins it rather than starting a second `git fetch` in the same clone.
fetch: (cwd, opts) => fetchWorkspaceOnce(cwd, opts),
publish: (id, status) => {
const session = this.sessions.get(id);
if (!session?.setGitFetchStatus(status)) return;
// No new SSE event: the status rides the session state the page already listens for, and the Git
// panel polls on its own clock. Persisted, because what a run found is the panel's current truth.
// panel polls on its own clock. It is written to disk with the rest of the record, but the restore
// copies only the fields its own config lists and this is not one of them (src/types/session.ts), so
// what a run found is the panel's truth for the life of the session, not across a restart.
this.persistSessionState(session);
this.broadcast(SseEvent.SessionUpdated, this.getSessionStateWithRespawn(session));
},