@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
44 lines • 1.73 kB
JavaScript
import { headerSessionId, ownershipStep, SessionMovedAwayError } from "./ownership.js";
/**
* The session-manager write funnel asks this before every persisted write. It folds only the entries appended since
* the last call (the funnel appends in place), and starts over when the entry array is replaced by a load or branch.
*/
export class SessionWriteGuard {
constructor() {
this.seen = 0;
this.verdict = { writable: true };
}
assertWritable(entries, entry, sessionFile) {
const ownership = this.ownership(entries);
if (ownership.writable)
return;
if (ownershipStep(ownership, entry, this.sessionId).kind !== "apply")
throw new SessionMovedAwayError(sessionFile, ownership);
}
assertRewritable(entries, sessionFile) {
const ownership = this.ownership(entries);
if (!ownership.writable)
throw new SessionMovedAwayError(sessionFile, ownership);
}
isWritable(entries) {
return this.ownership(entries).writable;
}
ownership(entries) {
if (entries !== this.source || entries.length < this.seen) {
this.source = entries;
this.seen = 0;
this.sessionId = headerSessionId(entries);
this.verdict = { writable: true };
}
for (; this.seen < entries.length; this.seen += 1) {
const entry = entries[this.seen];
if (entry === undefined)
continue;
const step = ownershipStep(this.verdict, entry, this.sessionId);
if (step.kind === "apply")
this.verdict = step.next;
}
return this.verdict;
}
}
//# sourceMappingURL=write-guard.js.map