@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
35 lines • 1.8 kB
JavaScript
/**
* The opportunistic gc passes this process has in flight, keyed by agent directory. `host-gc-pass.ts`
* starts them; this leaf only records them, so a caller that must not remove an agent directory under
* a running pass (a test teardown, senpi#4382) can join them without loading the gc module graph.
*/
import { isAbsolute, join, relative, resolve, sep } from "node:path";
const passesRunning = new Map();
export function opportunisticHostGcRunning(agentDir) {
return passesRunning.has(agentDir);
}
export function trackOpportunisticHostGc(agentDir, pass) {
passesRunning.set(agentDir, pass);
void pass.finally(() => {
if (passesRunning.get(agentDir) === pass)
passesRunning.delete(agentDir);
});
}
/** Resolves once the pass this process scheduled for `agentDir` (if any) has finished; it never rejects. */
export function settledOpportunisticHostGc(agentDir) {
return passesRunning.get(agentDir) ?? Promise.resolve();
}
/** The agent directories with a pass in flight whose `rpc-host-daemon` (where a pass writes) lies inside `path`. */
export function opportunisticHostGcWithin(path) {
return [...passesRunning.keys()].filter((agentDir) => contains(path, join(agentDir, "rpc-host-daemon")));
}
export async function settledOpportunisticHostGcWithin(path) {
await Promise.all(opportunisticHostGcWithin(path).map(settledOpportunisticHostGc));
}
// Lexical containment: a removal through another spelling of the same directory (a symlinked alias such
// as macOS /var vs /private/var) is not joined.
function contains(parent, child) {
const path = relative(resolve(parent), resolve(child));
return path === "" || (path !== ".." && !path.startsWith(`..${sep}`) && !isAbsolute(path));
}
//# sourceMappingURL=host-gc-pass-registry.js.map