@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
36 lines • 1.74 kB
JavaScript
import { COMPACT_LEAF_CODE_UNITS } from "./compact-content-store.js";
import { CompactTransitionError } from "./compact-transition.js";
export function appendBlock(store, block, text) {
block.units += text.length;
let rest = text;
while (block.tail.length + rest.length >= COMPACT_LEAF_CODE_UNITS) {
const take = COMPACT_LEAF_CODE_UNITS - block.tail.length;
block.root = store.text.append(block.root, block.tail + rest.slice(0, take));
store.adjust(block.tailRoot, -1, 0);
block.tailRoot = 0;
block.tail = "";
rest = rest.slice(take);
}
// Only the suffix enters structural storage; sealing coalesces it into one payload leaf.
if (rest.length)
block.tailRoot = store.text.append(block.tailRoot, rest, true);
block.tail += rest;
}
export function adjustBlock(store, block, live) {
store.adjust(block.root, live, 0);
store.adjust(block.tailRoot, live, 0);
}
/** Validate before touching the live text owner or its replay witness. */
export function deltaBlock(store, block, offset, text) {
if (offset !== block.emitted)
throw new CompactTransitionError("non_contiguous", `offset ${offset}, expected ${block.emitted}`);
const overlapEnd = Math.min(offset + text.length, block.units);
const overlap = Math.max(0, overlapEnd - offset);
if (overlap > 0 && text.slice(0, overlap) !== block.witness.slice(offset, overlapEnd))
throw new CompactTransitionError("conflicting_overlap", `[${offset}, ${overlapEnd})`);
appendBlock(store, block, text.slice(overlap));
block.emitted = offset + text.length;
if (block.emitted >= block.covered)
block.witness = "";
}
//# sourceMappingURL=compact-active-block.js.map