@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
110 lines • 3.92 kB
JavaScript
/** Speculation is bounded independently of the unlimited in-process session registry. */
const MAX_PARKS = 4;
const PARK_TTL_MS = 60_000;
/** Owns both in-flight builds and ready parks, including the races with claim, expiry and shutdown. */
export class HostParkLedger {
constructor(close) {
this.parks = new Map();
this.replacements = new Map();
this.stopped = false;
this.close = close;
}
get size() {
return this.parks.size;
}
async park(key, build) {
const existing = this.parks.get(key);
if (existing && !existing.claim && !existing.closing) {
await existing.ready;
return "already_parked";
}
// A park being claimed or closed is on its way out; the replacement scheduled after it builds the next.
if (existing)
return "full";
const replacement = this.replacements.get(key);
if (replacement !== undefined) {
clearTimeout(replacement);
this.replacements.delete(key);
}
if (this.stopped)
throw new Error("host_draining");
if (this.parks.size + this.replacements.size >= MAX_PARKS)
return "full";
const ready = Promise.withResolvers();
ready.promise.catch(() => { });
const park = { ready: ready.promise };
// Reserved before build's first await: a second request joins, never starts another runtime.
this.parks.set(key, park);
try {
park.opened = await build();
if (!this.stopped) {
park.expiry = setTimeout(() => void this.retire(key, park), PARK_TTL_MS);
park.expiry.unref?.();
}
ready.resolve();
if (this.stopped)
await this.retire(key, park);
return "parked";
}
catch (cause) {
this.parks.delete(key);
ready.reject(cause);
throw cause;
}
}
/** A claim never waits for a build, and consumes the ready park synchronously. */
take(key) {
const park = this.parks.get(key);
if (this.stopped || !park?.opened || park.claim || park.closing)
return undefined;
clearTimeout(park.expiry);
park.claim = Promise.withResolvers();
return park.opened;
}
finish(key) {
const park = this.parks.get(key);
this.parks.delete(key);
park?.claim?.resolve();
}
replace(key, build) {
if (this.stopped || this.replacements.has(key))
return;
const timer = setTimeout(() => {
this.replacements.delete(key);
void this.park(key, build).catch((cause) => {
process.stderr.write(`senpi rpc replacement park failed: ${String(cause)}\n`);
});
}, 0);
timer.unref?.();
this.replacements.set(key, timer);
}
/** Fences new builds synchronously; every build already accepted is closed when it finishes. */
async stop() {
this.stopped = true;
for (const timer of this.replacements.values())
clearTimeout(timer);
this.replacements.clear();
await Promise.all([...this.parks].map(async ([key, park]) => {
await park.ready.catch(() => { });
if (park.claim)
await park.claim.promise;
else
await this.retire(key, park);
}));
}
retire(key, park) {
clearTimeout(park.expiry);
park.closing ??= (async () => {
try {
if (park.opened)
await this.close(park.opened);
}
finally {
if (this.parks.get(key) === park)
this.parks.delete(key);
}
})();
return park.closing;
}
}
//# sourceMappingURL=host-park-ledger.js.map