@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
70 lines • 3.29 kB
JavaScript
/**
* A second checkpoint token tried to pin a frozen root another live token still pins: the one-token-per-root invariant
* the verified-component cache rests on is broken. An internal defect, never a refusal a peer can cause or recover from.
*/
export class FrozenRootPinnedError extends Error {
constructor(tokenId, holder) {
super(`frozen root already pinned by token ${holder}; token ${tokenId} cannot pin it`);
this.name = "FrozenRootPinnedError";
this.tokenId = tokenId;
this.holder = holder;
}
}
/**
* The verified byte sources of one host's frozen roots (RULING-69 (2)): a component's open-time verification runs once
* per frozen root, so a re-open on that root serves pages without re-reading a leaf. Entries are keyed by the root and
* dropped with the lease of the one token that pins it.
*
* Invariant: a frozen root is pinned by exactly one live token. `RecoveryCheckpoints.freeze` is the only place a token
* is minted, and it pins the root its own `lane.freeze()` call just built; every freeze builds a new root object with
* its own references (`SessionAcceptanceLane.freeze`, `CompactLaneCurrent.freeze`, `CompactSessionState.freeze`), so
* two peers, a re-attach or a shed's resync each get a root of their own, and `FrozenCheckpoint.retain` (on the
* checkpoint `RecoveryCheckpoints.freeze` builds) hands a root to a non-token consumer under the same token's lease. A
* second peer on the same session therefore never shares an entry; it verifies its own root once. `pin` enforces the
* invariant with `FrozenRootPinnedError` instead of serving or overwriting another token's entries.
* An entry holds positions only, never bytes: at most one number per stored leaf of a component, or per row of the
* base stream (`compact-recovery-base.ts`).
*/
export class VerifiedComponents {
constructor() {
this.roots = new Map();
this.pinned = new Map();
}
get size() {
let size = 0;
for (const { components } of this.roots.values())
size += components.size;
return size;
}
/** Registers `tokenId` as the root's one holder; idempotent for that token, a typed internal error for another. */
pin(tokenId, root) {
const held = this.roots.get(root);
if (held) {
if (held.token !== tokenId)
throw new FrozenRootPinnedError(tokenId, held.token);
return;
}
this.roots.set(root, { token: tokenId, components: new Map() });
this.pinned.set(tokenId, root);
}
get(root, ref) {
return this.roots.get(root)?.components.get(ref);
}
/** Cached only while `tokenId` still pins `root`; once its lease dropped the entry, a late open caches nothing. */
set(tokenId, root, ref, bytes) {
const held = this.roots.get(root);
if (!held)
return;
if (held.token !== tokenId)
throw new FrozenRootPinnedError(tokenId, held.token);
held.components.set(ref, bytes);
}
drop(tokenId) {
const root = this.pinned.get(tokenId);
if (!root)
return;
this.pinned.delete(tokenId);
this.roots.delete(root);
}
}
//# sourceMappingURL=compact-verified-components.js.map