@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
63 lines • 2.6 kB
JavaScript
import { BaselineBarrier } from "./socket-baseline-barrier.js";
import { isBarrier } from "./socket-event-queue.js";
/** One peer's pending cut: which records it keeps, where a reply goes, and when the cut is read. */
export class SocketPeerShed {
constructor(queue, mode) {
this.queue = queue;
this.mode = mode;
}
get isPending() {
return this.pending !== undefined;
}
/** True while the pending recovery drops the records it does not capture: today's path, or a capture gone stale. */
get dropsRecords() {
return this.pending !== undefined && (this.mode === "drop" || this.pending.stale);
}
/**
* Freeze the cut C now: keep the replies, release every record and baseline the cut covers, and queue the
* recovery (gap, then C's frames). With `capture`, the records that follow are kept behind it.
*/
cut(reason, snapshot, live) {
this.queue.retain((item) => !isBarrier(item) && item.reply);
const cut = snapshot(reason);
this.pending = {
kind: "recovery",
reason,
sessions: cut.sessions,
barrier: new BaselineBarrier(cut, live),
stale: false,
};
this.queue.push(this.pending);
}
/**
* The captured suffix overflowed: release it and the cut at C, and read one fresh cut when the gap is
* delivered. Until then records are dropped like today's path, one freeze instead of one per overflowing record.
*/
supersede() {
if (!this.pending || this.pending.stale)
return;
const recovery = this.pending;
this.queue.retain((item) => (isBarrier(item) ? item === recovery : item.reply));
recovery.barrier.cancel();
recovery.stale = true;
}
/** A reply keeps today's place ahead of the gap while records are dropped; a captured one follows the cut. */
admitReply(entry) {
if (this.pending && this.mode === "drop")
this.queue.insertBefore(this.pending, entry);
else
this.queue.push(entry);
}
/** The recovery reaches delivery: a stale one reads its fresh cut now, carrying the sessions of the cut it replaces. */
take(entry, snapshot, live) {
if (entry.stale && snapshot)
entry.barrier = new BaselineBarrier(snapshot(entry.reason, entry.sessions), live);
if (this.pending === entry)
this.pending = undefined;
return entry.barrier;
}
clear() {
this.pending = undefined;
}
}
//# sourceMappingURL=socket-peer-shed.js.map