@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
110 lines • 4.86 kB
JavaScript
import { catalogDigest, catalogDigester, storedBytes } from "./compact-catalog-codec.js";
import { CompactReadError } from "./compact-read-lease.js";
/** D (R4.B3): the most raw bytes one fragment carries; its base64 (174,764) plus H (16,384) fits F (262,144). */
export const FRAGMENT_MAX_BYTES = 131_072;
/**
* The page budget on the wire (RULING-69 (1)): the base64 envelope of a full fragment, derived from D (four characters
* per three raw bytes, the last group padded); S12's initial worker credit is one such envelope (RULING-63).
*/
export const FRAGMENT_ENVELOPE_MAX_BYTES = Math.ceil(FRAGMENT_MAX_BYTES / 3) * 4;
const isIndex = (value) => Number.isSafeInteger(value) && value >= 0;
/** The checks every `FrozenBytes.range` makes before touching a byte: shape, the D bound, then the stream's end. */
export function assertRange(offset, length, total) {
if (!isIndex(offset) || !isIndex(length))
throw new CompactReadError("range");
if (length > FRAGMENT_MAX_BYTES)
throw new CompactReadError("size_limit");
if (offset + length > total)
throw new CompactReadError("range");
}
/** The index of the last of `count` ascending starts at or before `offset`: the unit whose byte range holds it. */
export function startAtOrBefore(starts, count, offset) {
let low = 0;
let high = count - 1;
while (low < high) {
const middle = (low + high + 1) >> 1;
if ((starts[middle] ?? Number.POSITIVE_INFINITY) <= offset)
low = middle;
else
high = middle - 1;
}
return low;
}
class FrozenLeafBytes {
constructor(digest, leaves, starts, read) {
this.peakResidentLeaves = 0;
this.resident = 0;
this.length = starts[leaves.length] ?? 0;
this.digest = digest;
this.leaves = leaves;
this.starts = starts;
this.read = read;
}
async range(offset, length, valid) {
assertRange(offset, length, this.length);
const out = Buffer.allocUnsafe(length);
let written = 0;
for (let index = startAtOrBefore(this.starts, this.leaves.length, offset); written < length; index++) {
const leaf = this.leaves[index];
const start = this.starts[index];
if (leaf === undefined || start === undefined)
throw new CompactReadError("integrity");
this.peakResidentLeaves = Math.max(this.peakResidentLeaves, ++this.resident);
try {
const stored = storedBytes(await this.read(leaf));
valid?.();
const from = Math.max(0, offset - start);
written += stored.copy(out, written, from, Math.min(stored.length, from + length - written));
}
finally {
this.resident -= 1;
}
}
return out;
}
}
/**
* Opens a component's stored leaves as `FrozenBytes` after verifying them, one leaf resident, against the catalog
* entry's length and digest; a mismatch is `integrity` and no byte source exists. `valid` runs after every leaf read.
* The verification belongs to the request that asked for it (RULING-69 (2)): once `signal` aborts, no further leaf is
* read and the open is the typed `aborted`, holding nothing.
*/
export async function openFrozenBytes(entry, leaves, read, valid, signal) {
const digester = catalogDigester("leaf", entry.length);
const starts = [0];
let fed = 0;
for (const leaf of leaves) {
if (signal?.aborted)
throw new CompactReadError("aborted");
const stored = storedBytes(await read(leaf));
valid();
fed += stored.length;
if (fed > entry.length)
throw new CompactReadError("integrity");
digester.update(stored);
starts.push(fed);
}
if (fed !== entry.length || digester.finish() !== entry.digest)
throw new CompactReadError("integrity");
return new FrozenLeafBytes(entry.digest, leaves, starts, read);
}
/**
* The page at a raw byte offset: at most `FRAGMENT_MAX_BYTES` bytes, `next` the offset the following page starts at
* (null once the stream is complete). An offset past the last byte is `range`; an empty component is one empty page.
*/
export async function producePage(bytes, offset, valid) {
if (!isIndex(offset) || (offset >= bytes.length && bytes.length > 0))
throw new CompactReadError("range");
const length = Math.min(FRAGMENT_MAX_BYTES, bytes.length - offset);
const page = await bytes.range(offset, length, valid);
const next = offset + length;
return {
offset,
length,
bytes: page,
digest: catalogDigest("fragment", page),
next: next < bytes.length ? next : null,
complete: next === bytes.length,
};
}
//# sourceMappingURL=compact-recovery-bytes.js.map