kestrel.markets
Version:
A typed, token-efficient language + runtime for agentic trading: agents author bounded plans, the runtime fires them at the tick. CLI + typed library + MCP server.
74 lines (73 loc) • 4.32 kB
JavaScript
/**
* The ONE cockpit-kernel traversal: mandate/brief prefix (present-only) then the 8 fixed-order
* sections, each ALWAYS visited. This is the single source of the kernel's SECTION ORDER and its
* absent-not-hidden completeness — both format adapters get an identical structure from it. PURE.
*/
export function walkKernel(kernel, frameKind, sink) {
sink.sentinel(frameKind);
sink.frameLine(frameKind);
if (kernel.mandate !== undefined && kernel.mandate !== null)
sink.mandate(kernel.mandate);
if (kernel.brief !== undefined && kernel.brief !== null)
sink.brief(kernel.brief);
sink.wake(kernel.wake); // §1
sink.dataHealth(kernel.dataHealth ?? [], kernel.unavailable ?? []); // §2
sink.positions(kernel.positions ?? []); // §3
sink.resting(kernel.resting ?? []); // §4
sink.budget(kernel.budgetEnvelope); // §5
sink.ownerActs(kernel.budgetEnvelope, kernel.ownerActs ?? []); // §6
sink.engineLog(kernel.engineLog ?? []); // §7
sink.claims(kernel.claims ?? []); // §8
}
// ─────────────────────────────────────────────────────────────────────────────
// Shared kernel helpers (single-source logic; per-adapter vocabulary passed in)
// ─────────────────────────────────────────────────────────────────────────────
/** The four engine-log buckets, in fixed render order — shared by both adapters. */
export const ENGINE_LOG_BUCKETS = ["fired", "cancelled", "rejected", "clamped"];
/** Classify an engine log into the fixed buckets (order-preserving) + the catch-all `other`. PURE. */
export function bucketEngineLog(elog) {
const known = new Set(ENGINE_LOG_BUCKETS);
return {
buckets: ENGINE_LOG_BUCKETS.map((kind) => ({ kind, items: elog.filter((e) => e.kind === kind) })),
other: elog.filter((e) => !known.has(e.kind)),
};
}
/** The resting-order state label under an adapter's vocabulary. `live`/`clamped` independent
* (fail-closed honesty: a clamped price is never hidden by a LIVE label). PURE. */
export function restingLabel(o, labels) {
const live = o.live === true;
const clamped = o.clamped === true;
if (live)
return clamped ? labels.liveClamped : labels.live;
return clamped ? labels.clamped : labels.off;
}
/** An engine action's render token `id@<seqPrefix><ordinal>` plus `(reason)` WHEN present (the
* engine-authored cause — never-naked / plan-budget / regime-UNKNOWN — that must not be silently
* dropped at the render seam, kestrel-75n). PURE. */
export function actionToken(e, style) {
const reason = e.reason != null && e.reason !== "" ? `${style.reasonSpace ? " " : ""}(${e.reason})` : "";
return `${e.id}@${style.seqPrefix}${e.asofSeq}${reason}`;
}
/**
* A bucket's action tokens with each CONSECUTIVE RUN of items sharing an identical `(id, reason)`
* collapsed to `<count>x <exemplar token>` instead of N near-duplicate tokens (kestrel-7bip). A plan
* whose STATE trigger holds every tick emits the SAME refusal (`uncovered sell refused: never naked`)
* once per tick — 24 byte-identical entries in one frame — flooding the very percept the token-optimal
* renderer exists to conserve. The de-dup is a RENDER-time collapse ONLY (the engine still authors and
* counts every action; the bucket's `items.length` count is unchanged): a run of one renders as the
* bare token (byte-identical to pre-collapse, so no existing frame churns), and the exemplar keeps its
* real `@seq` so the collapsed line stays a concrete, addressable receipt. Order-preserving; PURE. */
export function collapseActionTokens(items, style) {
const out = [];
for (let i = 0; i < items.length;) {
const e = items[i];
let j = i + 1;
while (j < items.length && items[j].id === e.id && (items[j].reason ?? null) === (e.reason ?? null))
j++;
const count = j - i;
const token = actionToken(e, style);
out.push(count > 1 ? `${count}x ${token}` : token);
i = j;
}
return out;
}