UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

93 lines • 4.23 kB
export const BOUNDED_ID_MAX_BYTES = 128; const COUNTER_DIGITS = 16; /** Child descriptors per catalog node (R4.B1), the bound of every child step. */ export const CATALOG_NODE_MAX_ENTRIES = 128; const STEP_DIGITS = 3; /** The deepest ref: 128^8 addressable children, far past any catalog, in 50 of the 128 bytes an id may have. */ export const CATALOG_REF_MAX_STEPS = 8; const REF_ROOT_LENGTH = "c-".length + COUNTER_DIGITS; const REF_STEP_LENGTH = ":".length + STEP_DIGITS; /** No path separator, dot, quote, backslash, space, control or non-ASCII character, so '..' cannot be formed. */ const BOUNDED_ID = /^[A-Za-z0-9][A-Za-z0-9:_-]*$/; const CONTENT_DIGEST = /^[0-9a-f]{64}$/; const CONTENT_REF = /^c-[0-9]{16}(?::[0-9]{3})*$/; const BASE_REF = /^c-[0-9]{16}:base$/; /** The base step at any position of a ref: reserved, never a component step (RULING-71 (1)). */ const RESERVED_STEP = /:base(?::|$)/; const refused = (reason) => ({ ok: false, reason }); /** The allow-list check every identifier passes before any use as a key, a path element or a log field. */ export function parseBoundedId(value) { if (value.length === 0) return refused("empty"); if (Buffer.byteLength(value) > BOUNDED_ID_MAX_BYTES) return refused("overlong"); if (!BOUNDED_ID.test(value)) return refused("forbidden_character"); return { ok: true, value: value }; } export function parseContentDigest(value) { if (value.length === 0) return refused("empty"); if (!CONTENT_DIGEST.test(value)) return refused("malformed"); return { ok: true, value: value }; } export function parseContentRef(value) { const bounded = parseBoundedId(value); if (!bounded.ok) return bounded; if (RESERVED_STEP.test(value)) return refused("reserved"); if (!CONTENT_REF.test(value)) return refused("malformed"); if ((value.length - REF_ROOT_LENGTH) / REF_STEP_LENGTH > CATALOG_REF_MAX_STEPS) return refused("too_deep"); return { ok: true, value: value }; } function parseBaseRef(value) { const bounded = parseBoundedId(value); if (!bounded.ok) return bounded; if (!BASE_REF.test(value)) return refused("malformed"); return { ok: true, value: value }; } /** Either grammar, checked before any use; the two never overlap (`base` is not three digits). */ export function parsePageRef(value) { const base = parseBaseRef(value); if (base.ok) return { ok: true, value: { kind: "base", ref: base.value } }; const content = parseContentRef(value); return content.ok ? { ok: true, value: { kind: "catalog", ref: content.value } } : content; } /** A literal or generated protocol identifier; a refusal is a programming error, never input. */ export function protocolId(value) { const parsed = parseBoundedId(value); if (!parsed.ok) throw new RangeError(`not a bounded protocol id (${parsed.reason}): ${value.length} chars`); return parsed.value; } function fixed(value, digits, bound, what) { if (!Number.isSafeInteger(value) || value < 0 || value >= bound) throw new RangeError(`${what} out of range: ${value}`); return String(value).padStart(digits, "0"); } /** Every component ref is minted through the component grammar, so a reserved step can never be minted. */ function minted(value) { const parsed = parseContentRef(value); if (!parsed.ok) throw new RangeError(`catalog ref ${parsed.reason}`); return parsed.value; } export const catalogRootRef = (counter) => minted(`c-${fixed(counter, COUNTER_DIGITS, 10 ** COUNTER_DIGITS, "catalog counter")}`); export const childRef = (parent, index) => minted(`${parent}:${fixed(index, STEP_DIGITS, CATALOG_NODE_MAX_ENTRIES, "catalog child index")}`); /** The one base ref under a catalog root; a ref with steps is a programming error. */ export function baseRef(root) { const parsed = parseBaseRef(`${root}:base`); if (!parsed.ok) throw new RangeError(`base ref ${parsed.reason}`); return parsed.value; } /** The child indexes from the catalog root to the ref. */ export const refSteps = (ref) => ref.split(":").slice(1).map(Number); //# sourceMappingURL=compact-recovery-ids.js.map