UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

61 lines • 2.33 kB
import { RpcSessionRegistryError } from "./session-registry-types.js"; /** Claims precede write grants; native exit also removes claims whose publication is still pending. */ export class WorkerSessionClaims { constructor(reservations) { this.paths = new Map(); this.releases = new Set(); this.closed = false; this.reservations = reservations; } async claim(path, attached = true) { if (this.closed) throw new RpcSessionRegistryError("session_closing"); if (!this.reservations) return undefined; const previous = this.paths.get(path); if (previous) return previous.ownership; const record = { ownership: this.reservations.claim(path, attached), attached }; this.paths.set(path, record); const owner = await record.ownership; if (owner && this.paths.get(path) === record) this.paths.delete(path); return owner; } setAttached(attached) { for (const [path, record] of this.paths) { if (record.attached === attached) continue; record.attached = attached; void record.ownership.then((owner) => { if (!owner && this.paths.get(path) === record) this.reservations?.setAttached(path, record.attached); }); } } reconcile(livePaths, attached) { const live = new Set(livePaths); for (const path of this.paths.keys()) if (!live.has(path)) void this.release(path); this.setAttached(attached); } release(path) { const record = this.paths.get(path); if (!record) return Promise.resolve(); this.paths.delete(path); const removal = record.ownership.then(async (owner) => { if (!owner) await this.reservations?.release(path); }); this.releases.add(removal); void removal.then(() => this.releases.delete(removal), () => this.releases.delete(removal)); return removal; } async close() { this.closed = true; await Promise.all([...this.paths.keys()].map((path) => this.release(path)).concat([...this.releases])); } } //# sourceMappingURL=worker-session-claims.js.map