@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
84 lines • 3.57 kB
JavaScript
import { hasFunctions, realmRegistry } from "@earendil-works/pi-realm-registry";
/** The session file a live persisted writer owns; `undefined` for an owner another copy shaped differently. */
function persistedPath(owner) {
const isPersisted = Reflect.get(owner, "isPersisted");
const getSessionFile = Reflect.get(owner, "getSessionFile");
if (typeof isPersisted !== "function" || typeof getSessionFile !== "function")
return undefined;
if (Reflect.apply(isPersisted, owner, []) !== true)
return undefined;
const path = Reflect.apply(getSessionFile, owner, []);
return typeof path === "string" && path !== "" ? path : undefined;
}
const sessionWriters = realmRegistry("@code-yeongyu/senpi/session-writers/v1", () => {
let reservation;
// Weak on purpose: a writer nobody references anymore writes nothing, so its grant must not
// be kept alive by this registry. Identity removal needs the same ref the set holds.
const liveWriters = new Set();
const writerRefs = new WeakMap();
/** The live writers; collected ones are pruned here. */
const liveOwners = () => {
const owners = [];
for (const ref of liveWriters) {
const owner = ref.deref();
if (owner === undefined)
liveWriters.delete(ref);
else
owners.push(owner);
}
return owners;
};
return {
installReservation: (next) => {
if (reservation)
throw new Error("Session write reservation already installed");
reservation = next;
},
reserve: (path) => {
reservation?.(path);
},
register: (owner) => {
if (writerRefs.has(owner))
return;
const ref = new WeakRef(owner);
writerRefs.set(owner, ref);
liveWriters.add(ref);
},
unregister: (owner) => {
const ref = writerRefs.get(owner);
if (!ref)
return;
writerRefs.delete(owner);
liveWriters.delete(ref);
},
hasOtherLive: (path, self) => liveOwners().some((owner) => owner !== self && persistedPath(owner) === path),
livePaths: () => liveOwners()
.map(persistedPath)
.filter((path) => path !== undefined),
};
}, (value) => hasFunctions(value, ["installReservation", "reserve", "register", "unregister", "hasOtherLive", "livePaths"]));
export function installSessionWriteReservation(reservation) {
sessionWriters.installReservation(reservation);
}
/** Synchronous SessionManager entry points must obtain the host grant before touching a writer. */
export function reserveSessionWrite(path) {
sessionWriters.reserve(path);
}
export function registerSessionWriter(owner) {
sessionWriters.register(owner);
}
export function unregisterSessionWriter(owner) {
sessionWriters.unregister(owner);
}
/**
* Whether a persisted writer other than `self` still owns `path`, in any engine copy of this realm. Callers that
* release per-session caches ask this so the last owner is the one that clears them; collected writers are pruned here.
*/
export function hasOtherLiveSessionWriter(path, self) {
return sessionWriters.hasOtherLive(path, self);
}
/** Session files still owned by a live persisted writer of any engine copy; collected writers are pruned here. */
export function liveSessionWritePaths() {
return sessionWriters.livePaths();
}
//# sourceMappingURL=session-write-reservation.js.map