@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
58 lines • 2.26 kB
JavaScript
import { SESSION_WORKER_LIMITS } from "./session-worker-protocol.js";
/** Wire code each denial is reported with; distinct so clients can retry only the retryable one. */
export const RESERVATION_DENIAL_CODES = {
conflict: "session_path_in_use",
limit: "session_reservation_limit",
};
/**
* Canonical session-path ownership for the shared host: one path, one worker handle.
*
* A grant lives as long as the writer that holds it. Reconciliation against a worker's
* reported live writers releases superseded paths, so a session replaced by /new, /resume
* or /fork no longer keeps its previous file hostage or spends the per-worker budget.
*/
export class SessionPathReservations {
constructor() {
this.owners = new Map();
}
owner(path) {
return this.owners.get(path);
}
count(handle) {
let count = 0;
for (const owner of this.owners.values())
if (owner === handle)
count++;
return count;
}
/** Grants `path` to `handle`; at the budget cap, the live view first releases superseded paths. */
reserve(handle, path, live) {
const owner = this.owners.get(path);
if (owner)
return owner === handle ? "granted" : "conflict";
if (this.count(handle) >= SESSION_WORKER_LIMITS.reservations) {
if (!live)
return "limit";
this.reconcile(handle, live);
if (this.count(handle) >= SESSION_WORKER_LIMITS.reservations)
return "limit";
}
this.owners.set(path, handle);
return "granted";
}
/** Releases every path of `handle` that none of its live writers owns anymore. */
reconcile(handle, live) {
for (const [path, owner] of this.owners) {
if (owner !== handle || path === live.sessionPath || live.livePaths.includes(path))
continue;
this.owners.delete(path);
}
}
/** Drops the whole budget of a handle; only a real worker exit may call this. */
releaseAll(handle) {
for (const [path, owner] of this.owners)
if (owner === handle)
this.owners.delete(path);
}
}
//# sourceMappingURL=session-path-reservations.js.map