@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
224 lines • 11 kB
JavaScript
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { CompactContentStore, CompactWriteError } from "./compact-content-store.js";
import { CompactReadError, CompactReadLease } from "./compact-read-lease.js";
import { FRAGMENT_MAX_BYTES, producePage } from "./compact-recovery-bytes.js";
import { protocolId } from "./compact-recovery-ids.js";
import { stageRecord } from "./compact-record-staging.js";
import { recovered, repairNeeded, storageUnavailable, } from "./compact-recovery-types.js";
import { StagedReadError, TransferStaging, TransferStagingError, } from "./compact-transfer-staging.js";
/** F (R4.A3): the largest compact physical record, its newline included; two D-byte fragments. */
export const COMPACT_RECORD_MAX_BYTES = 2 * FRAGMENT_MAX_BYTES;
/** Live event leases one peer incarnation may hold (RULING-65 S13-Q1 (2)); its end frees every one of them. */
const PEER_EVENT_LEASES_MAX = 32;
/** Released lease ids a peer remembers so a retried receipt is a typed `duplicate`, the oldest forgotten first. */
const RELEASED_LEASES_REMEMBERED = 256;
/** The one component an event lease names: the accepted record's exact JSON text. */
const EVENT_RECORD_COMPONENT = "record";
/**
* The ordinary-event leases of one host (S13, R4.B6; RULING-65 S13-Q1/Q2 = A). A record past F for a peer that selected
* compact events is staged once, as its exact JSON text, into this owner's own spillable store and leased to each such
* peer at the record's accepted sequence. A lease is scoped like a checkpoint token (another peer or session is
* `unknown_token`, with no state), never mints a generation and holds no lane root, so a checkpoint's lifetime and a
* superseded root are untouched by it. It is released only by its consumer's receipt, sent after the consumer
* committed the content (the named commit point: desktop's successful command-transaction dispatch); transport drain,
* worker credit or staging every page release nothing. Per (peer, lease) the referencing admissions are counted,
* released at zero, never below; a retried receipt is the typed `duplicate`. A peer's end releases all of its leases.
*/
export class EventLeases {
constructor(peerLimit = PEER_EVENT_LEASES_MAX) {
this.peers = new Map();
this.shared = new WeakMap();
this.ended = new Set();
this.refusals = new Map();
this.open = 0;
this.minted = 0;
this.released = 0;
this.duplicates = 0;
this.abandoned = 0;
this.contentBytes = 0;
this.peerLimit = peerLimit;
}
/** Runs once per lease when it ends, however it ended (a receipt or its peer's end). */
onLeaseEnd(listener) {
this.ended.add(listener);
}
/**
* A lease for one peer admission on the record `line` serializes (keyed by `record`, so every peer shares one staged
* copy), taken synchronously at acceptance: later mutation of the provider object cannot reach the staged text.
*/
mint(caller, at, record, content) {
const peer = this.peers.get(caller.socketEpoch) ?? { held: new Map(), released: new Map() };
if (peer.held.size >= this.peerLimit)
return this.refuse("lease_limit");
const shared = this.stage(record, content);
if (!shared)
return this.refuse("storage_unavailable");
const { entry } = shared.content.ref;
const lease = Object.freeze({
kind: "event",
id: protocolId(`event:${caller.socketEpoch}:${++this.minted}`),
admissionId: caller.admissionId,
...at,
componentRef: EVENT_RECORD_COMPONENT,
length: entry.length,
digest: entry.digest,
});
shared.holders += 1;
const held = {
lease,
sessionId: caller.sessionId,
shared,
admissions: new Set([caller.admissionId]),
reads: new CompactReadLease(() => this.dispose(peer, held)),
};
peer.held.set(lease.id, held);
this.peers.set(caller.socketEpoch, peer);
this.open += 1;
return { ok: true, value: lease };
}
/** The caller's own live lease, or `unknown_token` with no state. */
claim(caller, leaseId, admissionId) {
const held = this.peers.get(caller.socketEpoch)?.held.get(leaseId);
if (!held || held.sessionId !== caller.sessionId || !held.admissions.has(admissionId))
return { ok: false, failure: repairNeeded("unknown_token") };
return recovered(held);
}
/** One bounded page of the leased record, its whole digest verified once before any page is served. */
async page(held, componentRef, offset, signal) {
const staging = this.staging;
if (componentRef !== EVENT_RECORD_COMPONENT || !staging)
return { ok: false, failure: storageUnavailable("range") };
try {
if (staging.store.health.needsRecovery)
await staging.store.repair();
return recovered(await held.reads.run(async () => {
held.shared.opened ??= staging.open(held.shared.content);
const bytes = await held.shared.opened;
if (signal?.aborted)
throw new CompactReadError("aborted");
return producePage(bytes, offset, () => held.reads.assert());
}));
}
catch (cause) {
held.shared.opened = undefined;
if (cause instanceof StagedReadError)
return { ok: false, failure: storageUnavailable("integrity") };
if (!(cause instanceof CompactReadError))
throw cause;
const failure = cause.reason === "released" ? repairNeeded("released") : storageUnavailable(cause.reason);
return { ok: false, failure };
}
}
/** The consumer's receipt for one admission's reference (RULING-65 S13-Q2): idempotent, identity-bound, never below 0. */
release(caller, leaseId, admissionId) {
const peer = this.peers.get(caller.socketEpoch);
const receipt = (outcome, remaining) => recovered({ lease: { kind: "event", id: leaseId }, outcome, remaining });
if (!peer)
return { ok: false, failure: repairNeeded("unknown_token") };
const held = peer.held.get(leaseId);
if (held?.sessionId === caller.sessionId && held.admissions.delete(admissionId)) {
this.remember(peer, held, admissionId);
if (held.admissions.size > 0)
return receipt("held", held.admissions.size);
this.released += 1;
held.reads.release();
return receipt("released", 0);
}
const done = peer.released.get(leaseId);
if (done?.sessionId === caller.sessionId && done.admissions.has(admissionId)) {
this.duplicates += 1;
return receipt("duplicate", held?.admissions.size ?? 0);
}
return { ok: false, failure: repairNeeded("unknown_token") };
}
/**
* The envelope naming this lease never reached its peer (superseded, shed or cut by the socket actor, S13a review):
* the lease ends with no receipt, at once and only once; an unknown or already ended lease is a no-op.
*/
abandon(socketEpoch, leaseId) {
const held = this.peers.get(socketEpoch)?.held.get(leaseId);
if (!held || !held.reads.release())
return;
this.abandoned += 1;
}
/** A retired admission can no longer send receipts; drop only its references, preserving other holders. */
forgetAdmission(caller, admissionId) {
for (const held of this.peers.get(caller.socketEpoch)?.held.values() ?? []) {
if (held.sessionId !== caller.sessionId || !held.admissions.delete(admissionId))
continue;
if (held.admissions.size === 0)
this.abandon(caller.socketEpoch, held.lease.id);
}
}
/** A peer incarnation is gone: every lease it holds ends once the reads in flight on it unwind. */
forgetPeer(socketEpoch) {
const peer = this.peers.get(socketEpoch);
this.peers.delete(socketEpoch);
for (const held of peer?.held.values() ?? [])
held.reads.release();
}
status() {
return {
open: this.open,
peers: this.peers.size,
minted: this.minted,
released: this.released,
duplicates: this.duplicates,
abandoned: this.abandoned,
contentBytes: this.contentBytes,
refused: Object.fromEntries(this.refusals),
};
}
/** The record's text staged once and adopted by this owner; undefined when the store could not take it. */
stage(record, content) {
const existing = this.shared.get(record);
if (existing && existing.holders > 0)
return existing;
this.staging ??= new TransferStaging(new CompactContentStore({ dir: join(tmpdir(), `senpi-event-leases-${randomUUID()}`) }));
try {
const staged = stageRecord(this.staging, record, content);
this.staging.adopt(staged);
const shared = { content: staged, holders: 0 };
this.contentBytes += staged.ref.entry.length;
this.shared.set(record, shared);
return shared;
}
catch (cause) {
if (cause instanceof TransferStagingError ||
cause instanceof CompactWriteError ||
cause instanceof CompactReadError) {
process.stderr.write(`senpi rpc event lease: record not staged (${cause.message})\n`);
return undefined;
}
throw cause;
}
}
remember(peer, held, admissionId) {
const admissions = new Set([...(peer.released.get(held.lease.id)?.admissions ?? []), admissionId]);
peer.released.set(held.lease.id, { sessionId: held.sessionId, admissions });
for (const oldest of peer.released.keys()) {
if (peer.released.size <= RELEASED_LEASES_REMEMBERED)
break;
peer.released.delete(oldest);
}
}
/** The lease's last operation unwound after its release: its content goes with the last lease sharing it. */
dispose(peer, held) {
peer.held.delete(held.lease.id);
this.open -= 1;
held.shared.holders -= 1;
if (held.shared.holders === 0 && this.staging) {
this.staging.release(held.shared.content);
this.contentBytes -= held.shared.content.ref.entry.length;
}
for (const listener of this.ended)
listener(held.lease.id);
}
refuse(reason) {
this.refusals.set(reason, (this.refusals.get(reason) ?? 0) + 1);
return { ok: false, reason };
}
}
//# sourceMappingURL=compact-event-leases.js.map