@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
122 lines • 6.48 kB
JavaScript
/**
* The budgeted gc pass `ensureHost` schedules after it has returned a host: the same per-endpoint
* removal as `senpi host gc` (`gcEndpoint` - each endpoint's own ensure lock with its 2 s wait, the
* three-part evidence, siblings/socket/directory in that order), run without an operator so dead
* endpoint records stop accumulating under an agent directory.
*
* It is never on the awaited start path: `scheduleOpportunisticHostGc` returns at once and the pass
* runs from an unref'd immediate, so a first child never waits on it, and a short-lived CLI process
* simply exits under it (the marker is written only after a COMPLETED pass, so the next long-lived
* process picks the work up). At most one pass per agent directory every `minIntervalMs`, stopped by
* a time and a count budget. It never considers - and so never locks - the endpoint the ensure was
* for, and never lowers the evidence bar. The one process it may end is a stale older-engine host
* (`host-stale-engine.ts`, #3268), judged and stopped under that endpoint's lock exactly as
* `senpi host gc` does.
*
* Fairness: endpoints are walked in sorted directory order starting AFTER the marker's `cursor` (the
* last one the previous pass examined), wrapping around, so one uncollectable endpoint costs at most
* one slot per pass and every endpoint is reached within ceil(total / maxEndpoints) passes. An
* endpoint that stayed is skipped until its `skipUntil` (10 min, doubling to 24 h), so a stuck one
* stops consuming budget altogether.
*/
import { appendFile } from "node:fs/promises";
import { basename, join } from "node:path";
import { createHostDaemonPaths, sameEndpoint } from "./host-daemon-paths.js";
import { listHostEndpoints } from "./host-endpoints.js";
import { gcEndpoint } from "./host-gc.js";
import { nextSkip, readHostGcPassMarker, writeHostGcPassMarker, } from "./host-gc-pass-marker.js";
import { opportunisticHostGcRunning, settledOpportunisticHostGc, trackOpportunisticHostGc, } from "./host-gc-pass-registry.js";
import { gcRpcPublicKeys } from "./rpc-public-key.js";
export { settledOpportunisticHostGc };
export const OPPORTUNISTIC_GC_BUDGET = { budgetMs: 1_500, maxEndpoints: 32, minIntervalMs: 300_000 };
export async function gcHostEndpointsOpportunistically(options) {
const now = options.now ?? Date.now;
const budgetMs = options.budgetMs ?? OPPORTUNISTIC_GC_BUDGET.budgetMs;
const maxEndpoints = options.maxEndpoints ?? OPPORTUNISTIC_GC_BUDGET.maxEndpoints;
const minIntervalMs = options.minIntervalMs ?? OPPORTUNISTIC_GC_BUDGET.minIntervalMs;
const previous = await readHostGcPassMarker(options.agentDir);
const startedAt = now();
if (previous !== undefined && startedAt - previous.completedAt < minIntervalMs)
return { ran: false };
if (process.platform !== "win32")
await gcRpcPublicKeys(options.agentDir, { deadline: startedAt + budgetMs, maxRecords: maxEndpoints });
const candidates = await gcCandidates(options);
const present = new Set(candidates.map((endpoint) => endpoint.name));
const skip = {};
for (const [name, entry] of Object.entries(previous?.skip ?? {})) {
if (present.has(name))
skip[name] = entry;
}
let cursor = previous?.cursor ?? null;
let examined = 0;
let removed = 0;
let stoppedBy = "exhausted";
for (const endpoint of rotateAfter(candidates, cursor)) {
if ((skip[endpoint.name]?.skipUntil ?? 0) > now())
continue;
if (examined >= maxEndpoints) {
stoppedBy = "count";
break;
}
if (now() - startedAt >= budgetMs) {
stoppedBy = "time";
break;
}
examined += 1;
cursor = endpoint.name;
const outcome = await gcEndpoint(endpoint.socket, endpoint.dir, { staleEngine: {} }).catch(() => ({ removed: false }));
// A stopped stale host leaves a dead endpoint the next pass collects without waiting out a backoff.
if ("stopped" in outcome)
delete skip[endpoint.name];
else if (outcome.removed) {
removed += 1;
delete skip[endpoint.name];
}
else {
skip[endpoint.name] = nextSkip(skip[endpoint.name], now());
}
}
const marker = { completedAt: now(), cursor, examined, removed, stoppedBy, skip };
await writeHostGcPassMarker(options.agentDir, marker);
return { ran: true, marker };
}
/** Starts a pass off the caller's path; at most one at a time per agent directory in this process. */
export function scheduleOpportunisticHostGc(options) {
if (opportunisticHostGcRunning(options.agentDir))
return;
const pass = new Promise((resolve) => {
setImmediate(() => {
gcHostEndpointsOpportunistically(options)
.then(() => undefined, (error) => recordPassFailure(options.agentDir, error))
.finally(resolve);
}).unref();
});
trackOpportunisticHostGc(options.agentDir, pass);
}
async function gcCandidates(options) {
const targetName = basename(createHostDaemonPaths({ socket: options.exclude, agentDir: options.agentDir }).dir);
const candidates = [];
for (const endpoint of await listHostEndpoints(options.agentDir)) {
const name = basename(endpoint.dir);
if (endpoint.socket === null || name === targetName || sameEndpoint(endpoint.socket, options.exclude))
continue;
candidates.push({ ...endpoint, socket: endpoint.socket, name });
}
return candidates;
}
function rotateAfter(endpoints, cursor) {
if (cursor === null)
return endpoints;
const start = endpoints.findIndex((endpoint) => endpoint.name > cursor);
return start <= 0 ? endpoints : [...endpoints.slice(start), ...endpoints.slice(0, start)];
}
async function recordPassFailure(agentDir, error) {
const line = `${new Date().toISOString()} gc pass failed: ${error instanceof Error ? error.message : String(error)}\n`;
await appendFile(join(agentDir, "rpc-host-daemon", "gc-pass.log"), line, { mode: 0o600 }).catch((logError) => {
// The agent directory itself is gone: nothing is left to collect or to report to.
if (logError instanceof Error && "code" in logError && logError.code === "ENOENT")
return;
process.emitWarning(`host gc pass failed and could not be recorded: ${String(logError)}`);
});
}
//# sourceMappingURL=host-gc-pass.js.map