Files
paseo/packages/server/src/utils/git-process-scheduler.ts
T
Mohamed Boudra 48a1497fc0 Load the Changes diff quickly while the daemon reconciles worktrees (#9243)
* fix(server): keep cold diffs out from behind background git fan-out

Add a background lane to the git process scheduler and run reconciliation
checkout reads, observation-driven workspace refreshes and background fetches
in it. Bound reconciliation checkout reads to four at a time. Stop the Linux
file observer's startup scan from reporting existing files as creates, which
made every cold diff compute and discard its first revision.

* fix(server): pick the git job when its start token fires

Background jobs that already reserved a start-rate token no longer start
ahead of normal work submitted after them.

* fix(server): re-read the working tree once its watcher covers it

The Linux observer no longer reports pre-existing files at startup, so an
edit made between a workspace's first snapshot and its watcher subscribing
reached no consumer. WorkspaceGit now runs one background working-tree read
once observation setup completes with a live watcher.

Demand refreshes keep the caller's Git lane instead of forcing normal, so
user actions running at high priority are not demoted.

Scope the populated-tree observer test to Linux (FSEvents replays recent
writes) and update the observer benchmark test, which expected the removed
startup batch.

* fix(server): re-read checkout metadata once observation covers it

A branch switch made before the Git metadata watcher subscribed left the
cached branch stale. The post-coverage read now refreshes structure as well
as the working tree, matching the refresh the Linux startup events used to
trigger.
2026-11-10 10:21:13 +07:00

155 lines
4.6 KiB
TypeScript

import { setImmediate as scheduleImmediate } from "node:timers";
import pThrottle from "p-throttle";
export interface GitProcessPolicy {
maxProcessesPerSecond: number;
maxProcessConcurrency: number;
}
export const DEFAULT_GIT_PROCESS_POLICY: GitProcessPolicy = {
maxProcessesPerSecond: 64,
maxProcessConcurrency: 8,
};
export interface ScheduledGitProcess<T> {
result: Promise<T>;
exited: Promise<void>;
}
// Lanes drain in this order. "background" is for daemon-initiated fan-out
// (reconciliation, observation-driven refreshes, fetches) so it never sits in
// front of work a user is waiting for.
export type GitProcessPriority = "high" | "normal" | "background";
function parsePositiveInteger(value: string | undefined): number | undefined {
if (value === undefined) {
return undefined;
}
const parsed = Number(value);
return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined;
}
export function resolveGitProcessPolicy(input: {
env: NodeJS.ProcessEnv;
persisted?: Partial<GitProcessPolicy>;
}): GitProcessPolicy {
return {
maxProcessesPerSecond:
parsePositiveInteger(input.env.PASEO_GIT_MAX_PROCESSES_PER_SECOND) ??
input.persisted?.maxProcessesPerSecond ??
DEFAULT_GIT_PROCESS_POLICY.maxProcessesPerSecond,
maxProcessConcurrency:
parsePositiveInteger(input.env.PASEO_GIT_MAX_PROCESS_CONCURRENCY) ??
// COMPAT(gitConcurrencyEnv): renamed in v0.2.6; remove after 2027-02-02.
parsePositiveInteger(input.env.PASEO_GIT_CONCURRENCY) ??
input.persisted?.maxProcessConcurrency ??
DEFAULT_GIT_PROCESS_POLICY.maxProcessConcurrency,
};
}
// Admission reserves a process slot and a start-rate token; the job runs only
// once its token fires. The job is chosen when the token fires, not when the
// slot is reserved, so work submitted while background jobs hold every reserved
// slot still takes the next start.
export class GitProcessScheduler {
private readonly startThrottled;
private readonly queues: Record<GitProcessPriority, Array<() => Promise<void>>> = {
high: [],
normal: [],
background: [],
};
private admitted = 0;
private reservedStarts = 0;
private running = 0;
private waiting = 0;
private drainScheduled = false;
constructor(readonly policy: GitProcessPolicy) {
const throttle = pThrottle({
limit: policy.maxProcessesPerSecond,
interval: 1_000,
strict: true,
});
this.startThrottled = throttle(() => this.startNext());
}
get activeCount(): number {
return this.running;
}
get pendingCount(): number {
return this.waiting;
}
run<T>(
start: () => ScheduledGitProcess<T>,
options?: { priority?: GitProcessPriority },
): Promise<T> {
this.waiting += 1;
return new Promise<T>((resolve, reject) => {
this.queues[options?.priority ?? "normal"].push(() => this.execute(start, resolve, reject));
if (this.admitted === 0 && !this.drainScheduled) {
this.drainOne();
} else {
this.scheduleDrain();
}
});
}
private canReserveStart(): boolean {
return this.admitted < this.policy.maxProcessConcurrency && this.waiting > this.reservedStarts;
}
private scheduleDrain(): void {
if (this.drainScheduled || !this.canReserveStart()) {
return;
}
this.drainScheduled = true;
scheduleImmediate(() => this.drainOne());
}
private drainOne(): void {
this.drainScheduled = false;
if (!this.canReserveStart()) {
return;
}
this.admitted += 1;
this.reservedStarts += 1;
void this.startThrottled();
// Spawning can be synchronous and expensive on a throttled host. Let timers,
// sockets, and child exits run before admitting another Git process.
this.scheduleDrain();
}
private startNext(): Promise<void> {
this.reservedStarts -= 1;
// Every reservation is backed by a queued job: jobs leave the queues only here.
const next =
this.queues.high.shift() ?? this.queues.normal.shift() ?? this.queues.background.shift();
if (!next) {
throw new Error("Git process start fired without a queued process");
}
return next();
}
private async execute<T>(
start: () => ScheduledGitProcess<T>,
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: unknown) => void,
): Promise<void> {
this.waiting = Math.max(0, this.waiting - 1);
this.running += 1;
try {
const process = start();
void process.result.then(resolve, reject);
await process.exited;
} catch (error) {
reject(error);
} finally {
this.running = Math.max(0, this.running - 1);
this.admitted = Math.max(0, this.admitted - 1);
this.scheduleDrain();
}
}
}