@orkestrel/agent 0.0.36 → 0.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/index.cjs +578 -84
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +324 -32
- package/dist/src/core/index.d.ts +324 -32
- package/dist/src/core/index.js +567 -85
- package/dist/src/core/index.js.map +1 -1
- package/package.json +7 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["holds","INTRINSICS","isObject","isFunction","isString","isNumber","arrayOf","objectShape","stringShape","recordShape","jsonShape","literalShape","optionalShape","arrayShape","unionShape","nullableShape","numberShape","createContract","attempt","parseJSON","isString","preview","describeFaults","isFiniteNumber","attempt","isArray","isRecord","isString","isArray","isJSONValue","isRecord","attempt","nullableOf","arrayOf","optionalOf","holds","isFiniteNumber","holds","INTRINSICS","isObject","isFunction","isString","isBoolean","isNumber","arrayOf","objectShape","arrayShape","optionalShape","stringShape","recordShape","jsonShape","booleanShape","unionShape","literalShape","createContract","objectOf","isString","arrayOf","isToolCall","isBudgetAmount","isBoolean","attempt","isRecord","optionalOf","nullableOf","isNumber","boundsOf","isArray","attempt","isObject","isFunction","preview","canonicalStringify","failure","success","boundsOf","isArray","isNumber","isTokenUsage","Timeout","createNDJSONParser","attempt","cloneJSONValue","isRecord","Timeout","parseJSON","describeFaults","preview","attempt","cloneJSONValue","preview","parseJSONAs","holds","INTRINSICS","isObject","isFunction","unionShape","objectShape","stringShape","arrayShape","numberShape","optionalShape","createContract","attempt","isRecord","isString","arrayOf","isToolCall","unionOf","recordOf","isFiniteNumber","isTokenUsage","holds","optionalOf","isObject","isBoolean","isFunction","preview","isRecord","isJSONValue","describeFaults","attempt","isArray","isString","isArray","isRecord","describeFaults","Emitter","isArray","preview","isArray","preview","describeFaults","createMemoryDriver","stringShape","rawShape","createDatabase","holds","INTRINSICS","isObject","isFunction","Emitter","isArray","isBinary","objectShape","stringShape","numberShape","optionalShape","arrayShape","createContract","holds","isRecord","isString","isFiniteNumber","arrayOf","describeFaults","Emitter","isArray","Emitter","isArray","Emitter","isArray","createFile","preview","describeFaults","WorkspaceManager","ToolManager","isTextContent","createMemoryDriver","stringShape","rawShape","createDatabase","holds","INTRINSICS","isObject","isFunction","isString","isNumber","isBoolean","objectShape","stringShape","optionalShape","arrayShape","numberShape","recordShape","jsonShape","unionShape","literalShape","booleanShape","createContract","objectOf","isString","attempt","isObject","isString","isBoolean","isFiniteNumber","parseJSONValue","isBudgetAmount","isBudgetError","preview","canonicalStringify","createContract","schemaToShape","parseJSON","describeFaults","objectOf","isBudget","ContractError","Emitter","attempt","describeFaults","ContractError","preview","createTimeout","createAbort","createTokenMeter","failure","isObject","errorToMessage","success","preview","describeFaults","createTokenBudget","ToolManager","createQueue","createRunner","holds","INTRINSICS","isObject","isFunction","isError","isString","canonicalStringify","isFiniteNumber","isError","errorToMessage","parseJSONAs","isArray","isString","isFiniteNumber","canonicalStringify","objectShape","arrayShape","stringShape","optionalShape","numberShape","createContract","isFiniteNumber","holds","isRecord","isString","arrayOf","describeFaults","createToolManager","createTool","objectShape","stringShape","createAbort","isFiniteNumber","attempt","isError","isString"],"sources":["../../../src/core/constants.ts","../../../src/core/errors.ts","../../../src/core/shapers.ts","../../../src/core/contracts.ts","../../../src/core/cloners.ts","../../../src/core/helpers.ts","../../../src/core/validators.ts","../../../src/core/providers/constants.ts","../../../src/core/providers/errors.ts","../../../src/core/providers/shapers.ts","../../../src/core/providers/contracts.ts","../../../src/core/providers/validators.ts","../../../src/core/providers/helpers.ts","../../../src/core/providers/RelayStream.ts","../../../src/core/providers/ThinkSplitter.ts","../../../src/core/providers/AgentProvider.ts","../../../src/core/providers/RelayProvider.ts","../../../src/core/providers/AgentJudge.ts","../../../src/core/providers/SystemOneJudge.ts","../../../src/core/providers/factories.ts","../../../src/core/conversations/constants.ts","../../../src/core/conversations/errors.ts","../../../src/core/conversations/shapers.ts","../../../src/core/conversations/contracts.ts","../../../src/core/conversations/validators.ts","../../../src/core/conversations/helpers.ts","../../../src/core/conversations/JudgmentManager.ts","../../../src/core/conversations/Conversation.ts","../../../src/core/conversations/ConversationManager.ts","../../../src/core/conversations/stores/MemoryConversationStore.ts","../../../src/core/conversations/stores/DatabaseConversationStore.ts","../../../src/core/conversations/factories.ts","../../../src/core/contexts/constants.ts","../../../src/core/contexts/errors.ts","../../../src/core/contexts/instructions/Instruction.ts","../../../src/core/contexts/instructions/InstructionManager.ts","../../../src/core/contexts/helpers.ts","../../../src/core/contexts/shapers.ts","../../../src/core/contexts/contracts.ts","../../../src/core/contexts/validators.ts","../../../src/core/contexts/memos/MemoManager.ts","../../../src/core/contexts/scopes/Scope.ts","../../../src/core/contexts/scopes/ScopeManager.ts","../../../src/core/contexts/skills/SkillManager.ts","../../../src/core/contexts/stores/MemoryMemoStore.ts","../../../src/core/contexts/stores/DatabaseMemoStore.ts","../../../src/core/contexts/AgentContext.ts","../../../src/core/contexts/factories.ts","../../../src/core/agents/constants.ts","../../../src/core/agents/errors.ts","../../../src/core/agents/shapers.ts","../../../src/core/agents/contracts.ts","../../../src/core/agents/ChannelCell.ts","../../../src/core/agents/Channel.ts","../../../src/core/agents/validators.ts","../../../src/core/agents/helpers.ts","../../../src/core/agents/Agent.ts","../../../src/core/agents/AgentFanout.ts","../../../src/core/agents/Authority.ts","../../../src/core/agents/Approver.ts","../../../src/core/agents/AgentRegistry.ts","../../../src/core/agents/factories.ts","../../../src/core/ledgers/constants.ts","../../../src/core/ledgers/errors.ts","../../../src/core/ledgers/helpers.ts","../../../src/core/ledgers/Classifier.ts","../../../src/core/ledgers/Gauge.ts","../../../src/core/ledgers/LedgerPass.ts","../../../src/core/ledgers/LedgerRequest.ts","../../../src/core/ledgers/LedgerScan.ts","../../../src/core/ledgers/shapers.ts","../../../src/core/ledgers/contracts.ts","../../../src/core/ledgers/validators.ts","../../../src/core/ledgers/Ledger.ts","../../../src/core/ledgers/factories.ts"],"sourcesContent":["/**\n * Lists the roles any message can play, the built `system` head included, in the order the wire\n * contract names them — the one list the {@link import('./types.js').MessageRole} union derives\n * from, the message guard tests membership against, and the message shape passes to its literal\n * contract.\n */\nexport const MESSAGE_ROLES = Object.freeze(['system', 'user', 'assistant', 'tool'] as const)\n\n/**\n * Lists the roles a stored conversation message can play: every `MESSAGE_ROLES` member except\n * `system`, in wire order — the one list the {@link import('./types.js').ConversationRole} union\n * derives from and a conversation checks each stored or selected role against.\n */\nexport const CONVERSATION_ROLES = Object.freeze(['user', 'assistant', 'tool'] as const)\n","import type { JudgeResult } from './types.js'\nimport {\n\tINTRINSICS,\n\tholds,\n\tisObject,\n\tisFunction,\n\tisNumber,\n\tisString,\n\tarrayOf,\n} from '@orkestrel/contract'\n\n/**\n * Reports a judge call cancelled by the caller's signal or its deadline, carrying the\n * {@link JudgeResult} merged from the calls that completed before the cancel and the\n * machine-readable `code` `'ABORT'`.\n *\n * @remarks\n * `partial` keeps the answers and the usage of every completed call, so usage reported by\n * completed calls is retained; a cancel before the first call carries an empty partial. `cause` holds the failure the\n * cancel superseded when a throw raced the abort, and is undefined when the cancel was the only\n * failure.\n */\nexport class JudgeAbortError extends Error {\n\t/** Names the machine-readable condition — `'ABORT'`: a judge call cancelled mid-flight. */\n\treadonly code = 'ABORT' as const\n\treadonly partial: JudgeResult\n\n\tconstructor(partial: JudgeResult, options?: ErrorOptions) {\n\t\tsuper('judge call aborted', options)\n\t\tthis.name = 'JudgeAbortError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.JudgeAbortError'), { value: this })\n\t\tthis.partial = partial\n\t}\n}\n\n/**\n * Narrows a caught value to a {@link JudgeAbortError} through its identity brand and declared members, so a `catch` can\n * recover its `partial` result.\n *\n * @param value - The caught value\n * @returns True if the value is a {@link JudgeAbortError}; false otherwise\n * @example\n * ```ts\n * isJudgeAbortError(new JudgeAbortError({ model: 'tev1:0.8b', answers: {} })) // true\n * ```\n */\nexport function isJudgeAbortError(value: unknown): value is JudgeAbortError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.JudgeAbortError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'ABORT')) return false\n\t\tconst partial = INTRINSICS.reflect.read(value, 'partial')\n\t\tif (!(isObject(partial) || isFunction(partial))) return false\n\t\tconst answers = INTRINSICS.reflect.read(partial, 'answers')\n\t\tconst refusals = INTRINSICS.reflect.read(partial, 'refusals')\n\t\tconst usage = INTRINSICS.reflect.read(partial, 'usage')\n\t\treturn (\n\t\t\tisString(INTRINSICS.reflect.read(partial, 'model')) &&\n\t\t\t(usage === undefined ||\n\t\t\t\t((isObject(usage) || isFunction(usage)) &&\n\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(usage, key)),\n\t\t\t\t\t))) &&\n\t\t\t(isObject(answers) || isFunction(answers)) &&\n\t\t\tINTRINSICS.values(answers).every((answer: unknown) => {\n\t\t\t\tif (!(isObject(answer) || isFunction(answer))) return false\n\t\t\t\tconst form = INTRINSICS.reflect.read(answer, 'form')\n\t\t\t\tif (form === 'noul') return isNumber(INTRINSICS.reflect.read(answer, 'noul'))\n\t\t\t\tconst probabilities = INTRINSICS.reflect.read(answer, 'probabilities')\n\t\t\t\tif (form === 'score') return arrayOf(isNumber)(probabilities)\n\t\t\t\treturn (\n\t\t\t\t\tform === 'choice' &&\n\t\t\t\t\t(isObject(probabilities) || isFunction(probabilities)) &&\n\t\t\t\t\tINTRINSICS.values(probabilities).every(isNumber)\n\t\t\t\t)\n\t\t\t}) &&\n\t\t\t(refusals === undefined ||\n\t\t\t\t((isObject(refusals) || isFunction(refusals)) &&\n\t\t\t\t\tINTRINSICS.values(refusals).every(\n\t\t\t\t\t\t(refusal: unknown) =>\n\t\t\t\t\t\t\t(isObject(refusal) || isFunction(refusal)) &&\n\t\t\t\t\t\t\tarrayOf(isString)(INTRINSICS.reflect.read(refusal, 'missing')),\n\t\t\t\t\t)))\n\t\t)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport { MESSAGE_ROLES } from './constants.js'\nimport {\n\tarrayShape,\n\tjsonShape,\n\tliteralShape,\n\tnullableShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\trecordShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\n\n/**\n * Describes a tool call's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than the domain: non-JSON arguments are refused.\n * Calls carry only id, name, and arguments; execution context stays local.\n * The guard refuses every extra member; the contract parser drops extra members.\n */\nexport const toolCallShape = objectShape({\n\tid: stringShape(),\n\tname: stringShape(),\n\targuments: recordShape(jsonShape()),\n}) satisfies ContractShape\n\n/**\n * Describes a message input's JSON projection: a {@link messageShape} with no `id`, which the\n * storing conversation mints.\n *\n * @remarks\n * A conversation's `add` checks a hoisted hand guard on every message and reads this shape's `audit`\n * only to fill a refusal's faults. A job's seed messages cross it at `build`. The role literal admits\n * `system`, so the conversation's `ROLE` refusal explains that case.\n */\nexport const messageInputShape = objectShape({\n\trole: literalShape(MESSAGE_ROLES),\n\tcontent: stringShape(),\n\tcalls: optionalShape(arrayShape(toolCallShape)),\n\tcall: optionalShape(stringShape()),\n\timages: optionalShape(arrayShape(stringShape())),\n\tthinking: optionalShape(stringShape()),\n}) satisfies ContractShape\n\n/**\n * Describes a conversation message's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than Message: non-JSON call arguments are refused,\n * and execution context stays local. The guard refuses extra members; the parser drops them.\n */\nexport const messageShape = objectShape({\n\tid: stringShape(),\n\trole: literalShape(MESSAGE_ROLES),\n\tcontent: stringShape(),\n\tcalls: optionalShape(arrayShape(toolCallShape)),\n\tcall: optionalShape(stringShape()),\n\timages: optionalShape(arrayShape(stringShape())),\n\tthinking: optionalShape(stringShape()),\n}) satisfies ContractShape\n\n/**\n * Describes the text or JSON a judge reads: a non-empty string, a non-empty JSON array, or a JSON record.\n *\n * @remarks\n * A shape has no `minProperties`, so this one admits the empty record that `isJudgeEntry` refuses;\n * the guard is the door, and this shape only fills a refusal's faults.\n */\nexport const judgeEntryShape = unionShape(\n\tstringShape({ min: 1 }),\n\tarrayShape(jsonShape(), { min: 1 }),\n\trecordShape(jsonShape()),\n) satisfies ContractShape\n\n/**\n * Describes a judge question: one exact object per form, the schema an LLM-facing wire can publish.\n *\n * @remarks\n * `isJudgeQuestion` is the door; this shape fills a refusal's faults. A choice needing at least 2\n * options is the guard's alone, because a shape has no `minProperties`.\n */\nexport const judgeQuestionShape = unionShape(\n\tobjectShape({\n\t\tform: literalShape(['choice']),\n\t\tinstructions: optionalShape(judgeEntryShape),\n\t\tcriteria: recordShape(nullableShape(judgeEntryShape)),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['score']),\n\t\tinstructions: optionalShape(judgeEntryShape),\n\t\tcriteria: arrayShape(nullableShape(judgeEntryShape), { min: 2 }),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['noul']),\n\t\tinstructions: optionalShape(judgeEntryShape),\n\t\tcriteria: optionalShape(\n\t\t\tobjectShape({\n\t\t\t\ttrue: optionalShape(judgeEntryShape),\n\t\t\t\tfalse: optionalShape(judgeEntryShape),\n\t\t\t}),\n\t\t),\n\t}),\n) satisfies ContractShape\n\n/**\n * Describes a judge answer: one exact object per form, each probability in `[0, 1]`.\n *\n * @remarks\n * The ties an answer holds to its question run at the write doors through `scanJudgmentTies`, which a\n * shape over the answer alone cannot state.\n */\nexport const judgeAnswerShape = unionShape(\n\tobjectShape({\n\t\tform: literalShape(['choice']),\n\t\tprobabilities: recordShape(numberShape({ min: 0, max: 1 })),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['score']),\n\t\tprobabilities: arrayShape(numberShape({ min: 0, max: 1 })),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['noul']),\n\t\tnoul: numberShape({ min: 0, max: 1 }),\n\t}),\n) satisfies ContractShape\n\n/**\n * Describes a token usage record's JSON wire projection — its `prompt`, `completion`, and `total`\n * counts, shared by the provider result and the agent frame wires.\n *\n * @remarks\n * Each count is a nonnegative number. The guard refuses extra members; the contract parser drops them.\n */\nexport const tokenUsageShape = objectShape({\n\tprompt: numberShape({ min: 0 }),\n\tcompletion: numberShape({ min: 0 }),\n\ttotal: numberShape({ min: 0 }),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { judgeAnswerShape, judgeQuestionShape, messageInputShape, messageShape } from './shapers.js'\n\n/** Validates and projects conversation messages at a JSON wire boundary. */\nexport const messageContract = createContract(messageShape)\n\n/** Reads the faults of a refused judge question. */\nexport const judgeQuestionContract = createContract(judgeQuestionShape)\n\n/** Reads the faults of a refused judge answer. */\nexport const judgeAnswerContract = createContract(judgeAnswerShape)\n\n/** Reads the faults of a refused message input and validates a job's seed messages. */\nexport const messageInputContract = createContract(messageInputShape)\n","import { attempt, parseJSON } from '@orkestrel/contract'\n\n/**\n * Owns a value by serializing it to JSON and parsing the text, so the copy shares nothing with its source.\n *\n * @remarks\n * A value that JSON cannot carry, such as a cycle or a bigint, and a value that serializes to\n * nothing, such as a function, both return undefined, so a guard over the result refuses them.\n * A proxied value serializes through its traps, where a structured clone refuses it.\n *\n * @param value - The value to own\n * @returns The owned JSON copy, or undefined when the value is not JSON\n * @example\n * ```ts\n * copyJSON({ state: 'A ticket.' }) // { state: 'A ticket.' }\n * copyJSON(() => 1) // undefined\n * ```\n */\nexport function copyJSON(value: unknown): unknown {\n\tconst text = attempt(() => JSON.stringify(value))\n\tif (!text.success || typeof text.value !== 'string') return undefined\n\treturn parseJSON(text.value)\n}\n\n/**\n * Owns a value through JSON and freezes every record and list of the copy, leaving the value unchanged.\n *\n * @remarks\n * Returns undefined exactly where `copyJSON` does, a value too deep for `JSON.stringify` among them.\n * A worklist over the owned copy does the freezing, so any depth `JSON.stringify` holds also freezes.\n *\n * @param value - The value to own and freeze\n * @returns The frozen JSON copy, or undefined when the value is not JSON\n * @example\n * ```ts\n * Object.isFrozen(freezeJSON({ state: 'A ticket.' })) // true\n * freezeJSON(() => 1) // undefined\n * ```\n */\nexport function freezeJSON(value: unknown): unknown {\n\tconst copy = copyJSON(value)\n\tif (typeof copy !== 'object' || copy === null) return copy\n\tconst worklist: object[] = [copy]\n\tfor (const node of worklist) {\n\t\tObject.freeze(node)\n\t\tconst children: readonly unknown[] = Object.values(node)\n\t\tfor (const child of children) {\n\t\t\tif (typeof child === 'object' && child !== null) worklist.push(child)\n\t\t}\n\t}\n\treturn copy\n}\n","import type {\n\tJudgeAnswer,\n\tJudgeQuestion,\n\tJudgment,\n\tLiteralSet,\n\tMessage,\n\tThinkingReplay,\n} from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport type { AuditFault, JSONSchema } from '@orkestrel/contract'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tisArray,\n\tisFiniteNumber,\n\tisRecord,\n\tisString,\n\tpreview,\n} from '@orkestrel/contract'\n\n/**\n * Filters a list of members by a {@link import('./contexts/index.js').ScopeInterface} allow-list of keys —\n * `undefined` passes everything, `[]` passes nothing, and a non-empty list passes the listed keys\n * alone, order preserved. The pure, total set-membership primitive the context's build step and\n * the agent loop's tool-advertise step apply a scope through.\n *\n * @remarks\n * Varies by the allow-list's shape, so a `Scope` category expresses \"all /\n * none / only these\":\n * - `undefined` ⇒ no constraint — every member passes (returned unchanged).\n * - `[]` (empty) ⇒ none pass (no key is in an empty set).\n * - a non-empty list ⇒ only members whose `key(member)` is in the list pass.\n *\n * Preserves the members' order. Keys are matched by a `Set` for membership.\n *\n * @typeParam T - The member type being filtered\n * @param allow - The allow-list of keys (`undefined` ⇒ all, `[]` ⇒ none, else only-listed)\n * @param members - The members to filter (returned unchanged when `allow` is `undefined`)\n * @param key - Extracts the key a member is matched on (for example an instruction's `name`)\n * @returns The members that pass the allow-list, in their original order\n *\n * @example\n * ```ts\n * const members = [{ name: 'refunds' }, { name: 'billing' }]\n * filterAllowList(undefined, members, (member) => member.name) // [{ name: 'refunds' }, { name: 'billing' }]\n * filterAllowList([], members, (member) => member.name) // []\n * filterAllowList(['billing'], members, (member) => member.name) // [{ name: 'billing' }]\n * ```\n */\nexport function filterAllowList<T>(\n\tallow: readonly string[] | undefined,\n\tmembers: readonly T[],\n\tkey: (member: T) => string,\n): readonly T[] {\n\tif (allow === undefined) return members\n\tif (allow.length === 0) return []\n\tconst set = new Set(allow)\n\treturn members.filter((member) => set.has(key(member)))\n}\n\n/**\n * Joins the reasoning a run's provider calls separated from the answer — the first call\n * seeds the accumulation, a later call appends blank-line separated so each turn's reasoning\n * stays readable.\n *\n * @remarks\n * Pure and total. `prior` is `undefined` until a call surfaces reasoning, so the first join\n * returns `next` verbatim. Each call's non-empty thinking is recorded on the assistant message\n * that call appends. The joined result also includes thinking from calls that appended no\n * message, such as an aborted call. Only recorded thinking can return to a provider,\n * as its `replay` policy allows. Thinking stays out of `content`.\n *\n * @param prior - The reasoning accumulated so far (`undefined` before the first)\n * @param next - This call's separated reasoning\n * @returns The joined reasoning\n *\n * @example\n * ```ts\n * joinThinking(undefined, 'first') // 'first'\n * joinThinking('', 'x') // 'x'\n * joinThinking('first', 'second') // 'first\\n\\nsecond'\n * ```\n */\nexport function joinThinking(prior: string | undefined, next: string): string {\n\tif (prior === undefined || prior.length === 0) return next\n\treturn next.length === 0 ? prior : `${prior}\\n\\n${next}`\n}\n\n/**\n * Applies a {@link ThinkingReplay} policy to a conversation, returning the messages a provider\n * sends with only the assistant thinking the policy allows.\n *\n * @remarks\n * Pure and total. `'all'` returns the input array itself. `'none'` drops `thinking` from every\n * message that carries it. `'turn'` drops it from every message at or before the last `user`\n * message and keeps it after, the turn in progress; with no `user` message every message counts\n * as inside the turn. A message without `thinking`, or one that keeps it, is the same object;\n * a message that loses it is a copy without that member, so no `undefined` member is written.\n *\n * @param messages - The conversation to project (left unchanged)\n * @param replay - The policy naming which thinking stays\n * @returns The messages with the policy applied\n *\n * @example\n * ```ts\n * const messages = [\n * \t{ id: '1', role: 'user', content: 'Plan the trip' },\n * \t{ id: '2', role: 'assistant', content: 'Booked', thinking: 'Compare fares first' },\n * ]\n * stripThinking(messages, 'none') // [{ id: '1', ... }, { id: '2', role: 'assistant', content: 'Booked' }]\n * stripThinking(messages, 'turn') // the thinking on '2' stays: it follows the last user message\n * ```\n */\nexport function stripThinking(\n\tmessages: readonly Message[],\n\treplay: ThinkingReplay,\n): readonly Message[] {\n\tif (replay === 'all') return messages\n\tconst last =\n\t\treplay === 'none' ? messages.length - 1 : messages.findLastIndex((one) => one.role === 'user')\n\treturn messages.map((message, index) => {\n\t\tif (message.thinking === undefined || index > last) return message\n\t\tconst { thinking: _thinking, ...rest } = message\n\t\treturn rest\n\t})\n}\n\n/**\n * Estimates the context-token footprint of a string — the deterministic `ceil(length / 4)`\n * character heuristic {@link import('./agents/helpers.js').estimateMessages} sums over a conversation's\n * messages (the default context-budget estimator).\n *\n * @remarks\n * Approximates `ceil(length / 4)` (4 characters per token), so the same input always yields the same estimate (no model round-trip).\n * Empty text is `0`. This is a planning heuristic for reasoning about how much a turn's\n * messages cost the next request, not an exact tokenizer count — it never calls a provider,\n * so the agent layer stays provider-agnostic and synchronous where it can be.\n *\n * @param text - The text to estimate (a section summary, a message's content)\n * @returns The estimated token count (`ceil(text.length / 4)`; `0` for empty text)\n *\n * @example\n * ```ts\n * estimateTokens('') // 0\n * estimateTokens('hello') // 2 (ceil(5 / 4))\n * estimateTokens('a'.repeat(40)) // 10\n * ```\n */\nexport function estimateTokens(text: string): number {\n\treturn Math.ceil(text.length / 4)\n}\n\n/**\n * Describes a snapshot a store refuses, naming its id, the first fault, and what a host does about it.\n *\n * @remarks\n * A store's row is the host's table, so the text tells the host to read the row itself and rewrite it,\n * or to delete it, rather than reading a refused row as absent, which would let `open` overwrite it.\n * The id and each fault segment are JSON-encoded and length-capped.\n *\n * @param noun - What the snapshot holds, such as `conversation`\n * @param id - The id the row is keyed by\n * @param faults - The faults of the snapshot's shape\n * @returns The refusal's text\n * @example\n * ```ts\n * describeSnapshotRefusal('conversation', 'c1', [{ reason: 'extra', path: ['summary'] }])\n * // 'conversation \"c1\" holds a snapshot this version refuses (extra key \"summary\" at the root; ...'\n * ```\n */\nexport function describeSnapshotRefusal(\n\tnoun: string,\n\tid: unknown,\n\tfaults: readonly AuditFault[],\n): string {\n\tconst [first] = faults\n\tconst path = first === undefined ? [] : isString(first.path) ? [first.path] : first.path\n\tconst held = preview(id)\n\tconst finding =\n\t\tfirst?.reason === 'extra'\n\t\t\t? `extra key ${preview(path.at(-1))} at ${path.length === 1 ? 'the root' : preview(path.slice(0, -1).join('.'))}`\n\t\t\t: first === undefined\n\t\t\t\t? 'a value its shape cannot name'\n\t\t\t\t: `${first.reason} at ${path.length === 0 ? 'the root' : preview(path.join('.'))}`\n\tconst remedy =\n\t\tfirst?.reason === 'extra' ? 'rewrite it without the key' : 'rewrite it to the declared shape'\n\treturn `${noun} ${held} holds a snapshot this version refuses (${finding}; a snapshot written before this change can carry it); read the row through your own table and ${remedy}, or delete it with store.delete(${held})`\n}\n\n/**\n * Describes why a judge question refuses, in the text both of its doors throw.\n *\n * @remarks\n * The rule travels in the text because the question shape cannot state it: a record has no minimum\n * size, so a single-option choice refuses at the guard with no fault for the list to name.\n *\n * @param id - The question's id\n * @param faults - What the question shape's audit found\n * @param schema - The question shape's JSON Schema\n * @returns The refusal text, with the fault list on the lines after it\n * @example\n * ```ts\n * describeQuestionRefusal('team', [], judgeQuestionContract.schema)\n * // 'question team is malformed; a choice names two or more criteria, a score two or more levels, and a noul only true and false'\n * ```\n */\nexport function describeQuestionRefusal(\n\tid: string,\n\tfaults: readonly AuditFault[],\n\tschema: JSONSchema,\n): string {\n\tconst refusal = `question ${id} is malformed; a choice names two or more criteria, a score two or more levels, and a noul only true and false`\n\treturn faults.length === 0 ? refusal : `${refusal}\\n${describeFaults(faults, schema)}`\n}\n\n/**\n * Scans what a judge's answer must hold to its question: the same form, every probability in\n * `[0, 1]`, a choice's keys equal to the criteria keys, and a score as long as its levels.\n *\n * @remarks\n * These ties run where a judgment is written, at `JudgmentManager.add` and the `AgentJudge` result\n * ingestion. A read of a stored record checks only exact keys, literal forms, and number types,\n * because records written before the ties can miss them.\n *\n * @param question - The question that was asked\n * @param answer - The answer the judge gave\n * @returns Every broken tie as a fault, or an empty list\n * @example\n * ```ts\n * scanJudgmentTies({ form: 'noul' }, { form: 'noul', noul: 2 })\n * // [{ reason: 'constraint', path: ['answer', 'noul'], expected: 'number', constraint: 'max', limit: 1, received: '2' }]\n * ```\n */\nexport function scanJudgmentTies(\n\tquestion: JudgeQuestion,\n\tanswer: JudgeAnswer,\n): readonly AuditFault[] {\n\tif (question.form === 'noul' && answer.form === 'noul') {\n\t\treturn scanProbability(answer.noul, ['answer', 'noul'])\n\t}\n\tif (question.form === 'choice' && answer.form === 'choice') {\n\t\tconst wanted = Object.keys(question.criteria)\n\t\tconst held = Object.keys(answer.probabilities)\n\t\treturn [\n\t\t\t...wanted\n\t\t\t\t.filter((key) => !Object.hasOwn(answer.probabilities, key))\n\t\t\t\t.map((key): AuditFault => ({\n\t\t\t\t\treason: 'missing',\n\t\t\t\t\tpath: ['answer', 'probabilities', key],\n\t\t\t\t\texpected: 'number',\n\t\t\t\t})),\n\t\t\t...held\n\t\t\t\t.filter((key) => !Object.hasOwn(question.criteria, key))\n\t\t\t\t.map((key): AuditFault => ({\n\t\t\t\t\treason: 'extra',\n\t\t\t\t\tpath: ['answer', 'probabilities', key],\n\t\t\t\t})),\n\t\t\t...held.flatMap((key) =>\n\t\t\t\tscanProbability(answer.probabilities[key], ['answer', 'probabilities', key]),\n\t\t\t),\n\t\t]\n\t}\n\tif (question.form === 'score' && answer.form === 'score') {\n\t\tconst levels = question.criteria.length\n\t\tconst count = answer.probabilities.length\n\t\treturn [\n\t\t\t...(count === levels\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\treason: 'constraint',\n\t\t\t\t\t\t\tpath: ['answer', 'probabilities'],\n\t\t\t\t\t\t\texpected: 'array',\n\t\t\t\t\t\t\tconstraint: count < levels ? 'min' : 'max',\n\t\t\t\t\t\t\tlimit: levels,\n\t\t\t\t\t\t\treceived: String(count),\n\t\t\t\t\t\t} satisfies AuditFault,\n\t\t\t\t\t]),\n\t\t\t...answer.probabilities.flatMap((value, index) =>\n\t\t\t\tscanProbability(value, ['answer', 'probabilities', String(index)]),\n\t\t\t),\n\t\t]\n\t}\n\treturn [\n\t\t{\n\t\t\treason: 'type',\n\t\t\tpath: ['answer', 'form'],\n\t\t\texpected: 'literal',\n\t\t\treceived: answer.form,\n\t\t},\n\t]\n}\n\n/**\n * Scans one probability against `[0, 1]`.\n *\n * @param value - The probability\n * @param path - Where it sits\n * @returns A constraint fault when it is out of bounds, or an empty list\n * @example\n * ```ts\n * scanProbability(0.5, ['answer', 'noul']) // []\n * ```\n */\nexport function scanProbability(\n\tvalue: number | undefined,\n\tpath: readonly string[],\n): readonly AuditFault[] {\n\tif (value !== undefined && value >= 0 && value <= 1) return []\n\treturn [\n\t\t{\n\t\t\treason: 'constraint',\n\t\t\tpath,\n\t\t\texpected: 'number',\n\t\t\tconstraint: value !== undefined && value < 0 ? 'min' : 'max',\n\t\t\tlimit: value !== undefined && value < 0 ? 0 : 1,\n\t\t\treceived: String(value),\n\t\t},\n\t]\n}\n\n/**\n * Sanitizes one reported token count into a non-negative integer — a non-finite or\n * non-positive value becomes `0`, and a positive fractional value floors down.\n *\n * @param value - The token count to sanitize\n * @returns The floored count, or `0` when the value is non-finite or non-positive\n * @example\n * ```ts\n * sanitizeToken(12.7) // 12\n * sanitizeToken(-1) // 0\n * sanitizeToken(Number.NaN) // 0\n * ```\n */\nexport function sanitizeToken(value: number): number {\n\treturn isFiniteNumber(value) && value > 0 ? Math.floor(value) : 0\n}\n\n/**\n * Sanitizes a {@link TokenUsage} into non-negative integers — the guard an agent's\n * abort-usage path applies to a provider's partial usage before it is charged against a\n * budget or folded into the run total, because a cancelled stream has no settled result to refuse;\n * a settled result's usage crosses the provider-result door instead.\n *\n * @remarks\n * Per field (`prompt` / `completion` / `total`): a non-finite value (`NaN`, `+Infinity`,\n * `-Infinity`) or a negative value floors to `0`; a fractional value floors to its\n * non-negative integer part. No upper cap is applied. Total — never throws.\n *\n * @param usage - The token usage to sanitize (for example a provider's abort-partial usage)\n * @returns A new {@link TokenUsage} with every field a non-negative integer\n *\n * @example\n * ```ts\n * sanitizeUsage({ prompt: -5, completion: NaN, total: 12.7 }) // { prompt: 0, completion: 0, total: 12 }\n * ```\n */\nexport function sanitizeUsage(usage: TokenUsage): TokenUsage {\n\treturn {\n\t\tprompt: sanitizeToken(usage.prompt),\n\t\tcompletion: sanitizeToken(usage.completion),\n\t\ttotal: sanitizeToken(usage.total),\n\t}\n}\n\n/**\n * Adds two {@link TokenUsage} values field by field — the running total an agent run keeps\n * across its provider calls.\n *\n * @remarks\n * Pure and total: the first call seeds the total (`prior` `undefined` returns `next`\n * unchanged), later calls accumulate. No sanitization happens here — charge a provider's\n * reported usage through {@link sanitizeUsage} first.\n *\n * @param prior - The total so far (`undefined` before the first usage-bearing call)\n * @param next - This call's reported usage\n * @returns The summed usage\n *\n * @example\n * ```ts\n * sumUsage(undefined, { prompt: 2, completion: 1, total: 3 }) // { prompt: 2, completion: 1, total: 3 }\n * sumUsage({ prompt: 2, completion: 1, total: 3 }, { prompt: 1, completion: 1, total: 2 })\n * // { prompt: 3, completion: 2, total: 5 }\n * ```\n */\nexport function sumUsage(prior: TokenUsage | undefined, next: TokenUsage): TokenUsage {\n\tif (prior === undefined) return next\n\treturn {\n\t\tprompt: prior.prompt + next.prompt,\n\t\tcompletion: prior.completion + next.completion,\n\t\ttotal: prior.total + next.total,\n\t}\n}\n\n/**\n * Removes each key through a single-key remover and folds the outcomes, so a batch `remove`\n * reports whether the whole batch applied.\n *\n * @remarks\n * Every key is passed to `remove` even after one is missing, so a present key still takes\n * effect (and emits) when an earlier key was absent.\n *\n * @typeParam K - The key type the remover accepts\n * @param keys - The keys to remove, in order\n * @param remove - Removes one key and returns whether it was present\n * @returns True if every key was present and removed; false otherwise (an empty list returns true)\n *\n * @example\n * ```ts\n * const stored = new Set(['refunds', 'billing'])\n * removeEntries(['refunds', 'escalations'], (key) => stored.delete(key)) // false\n * removeEntries(['billing'], (key) => stored.delete(key)) // true\n * ```\n */\nexport function removeEntries<K>(keys: readonly K[], remove: (key: K) => boolean): boolean {\n\t// `Array.from` passes a hole in `keys` to `remove` as `undefined`, as iteration does; `map` skips it.\n\treturn Array.from(keys, (key) => remove(key)).every((removed) => removed)\n}\n\n/**\n * Collects whole exchanges, joining every exchange spanned by a tool group.\n *\n * @remarks\n * A user message opens an exchange that ends before the next user message. Leading messages\n * form their own exchange. A tool group joins every exchange between its first and last member.\n *\n * @param messages - The messages in prompt order\n * @returns The exchanges in prompt order, with each message retained unchanged\n * @example\n * ```ts\n * collectExchanges([\n * \t{ id: 'greeting', role: 'assistant', content: 'Welcome.' },\n * \t{ id: 'request', role: 'user', content: 'Read the order.' },\n * ]) // a leading exchange and a request exchange\n * ```\n */\nexport function collectExchanges(messages: readonly Message[]): ReadonlyArray<readonly Message[]> {\n\tif (messages.length === 0) return []\n\tconst positions = new Map(messages.map((message, index) => [message, index]))\n\tconst spans = collectToolGroups(messages).map((group) => {\n\t\tconst spanned = group.flatMap((message) => {\n\t\t\tconst position = positions.get(message)\n\t\t\treturn position === undefined ? [] : [position]\n\t\t})\n\t\treturn {\n\t\t\tstart: spanned.reduce((least, position) => Math.min(least, position), messages.length),\n\t\t\tend: spanned.reduce((most, position) => Math.max(most, position), 0),\n\t\t}\n\t})\n\tconst starts = [\n\t\t...new Set([\n\t\t\t0,\n\t\t\t...[...messages.entries()].flatMap(([index, message]) =>\n\t\t\t\tmessage.role === 'user' ? [index] : [],\n\t\t\t),\n\t\t]),\n\t].filter((boundary) => !spans.some((span) => span.start < boundary && boundary <= span.end))\n\treturn starts.map((start, position) =>\n\t\tmessages.slice(start, starts[position + 1] ?? messages.length),\n\t)\n}\n\n/**\n * Checks whether a message carries at least one tool call.\n *\n * @remarks\n * Pure and total. An absent `calls` member and an empty list both return false, so a tool-free\n * assistant turn and a turn whose provider reported an empty call list read alike.\n *\n * @param message - The message to inspect; only its `calls` member is read\n * @returns True if `calls` holds at least one call; false otherwise\n * @example\n * ```ts\n * hasCalls({ calls: [{ id: 'order', name: 'lookup', arguments: {} }] }) // true\n * hasCalls({ calls: [] }) // false\n * hasCalls({}) // false\n * ```\n */\nexport function hasCalls(message: Pick<Message, 'calls'>): boolean {\n\treturn (message.calls?.length ?? 0) > 0\n}\n\n/**\n * Collects each assistant message that carries calls together with the tool messages that answer\n * it, then each run of tool messages that no assistant message owns.\n *\n * @remarks\n * A tool message belongs to the one assistant message whose calls hold its `call` id. Without that\n * unique owner, it belongs to the assistant message that leads its run of tool messages when that\n * leader holds the id, repeats a call id, or the tool message has no `call`. Any other tool message\n * joins the orphan run it sits in. Compaction and the stock selection each keep a group on one side\n * of their cut.\n *\n * @param messages - The messages in prompt order\n * @returns The owned groups in owner order, then the orphan runs, each in prompt order\n * @example\n * ```ts\n * collectToolGroups([\n * \t{ id: 'lookup', role: 'assistant', content: '', calls: [{ id: 'order', name: 'lookup', arguments: {} }] },\n * \t{ id: 'result', role: 'tool', content: 'LH-81660 is late', call: 'order' },\n * ]) // one group holding the lookup call and its result\n * ```\n */\nexport function collectToolGroups(messages: readonly Message[]): ReadonlyArray<readonly Message[]> {\n\tconst leaders = messages.filter((message) => message.role === 'assistant' && hasCalls(message))\n\tconst distinct = new Set(leaders)\n\t// `Array.from` passes a hole in `calls` to the mapper as `undefined`, as iteration does; `map` skips it.\n\tconst owners = Map.groupBy(\n\t\tleaders.flatMap((leader) =>\n\t\t\tArray.from(leader.calls ?? [], (call) => ({ id: call.id, leader })),\n\t\t),\n\t\t(entry) => entry.id,\n\t)\n\tconst starts = [...messages.entries()].flatMap(([index, message]) =>\n\t\tmessage.role === 'tool' ? [] : [index],\n\t)\n\t// A run is one non-tool message, which leads it when it owns calls, and the tool messages after it.\n\tconst runs = [\n\t\t{ leader: undefined, tools: messages.slice(0, starts[0] ?? messages.length) },\n\t\t...starts.map((start, position) => {\n\t\t\tconst message = messages[start]\n\t\t\treturn {\n\t\t\t\tleader: message !== undefined && distinct.has(message) ? message : undefined,\n\t\t\t\ttools: messages.slice(start + 1, starts[position + 1] ?? messages.length),\n\t\t\t}\n\t\t}),\n\t]\n\tconst placed = runs.map(({ leader, tools }) => {\n\t\tconst local = leader?.calls ?? []\n\t\tconst duplicate = new Set(local.map((call) => call.id)).size !== local.length\n\t\treturn tools.map((message) => {\n\t\t\tconst paired =\n\t\t\t\tleader !== undefined &&\n\t\t\t\t(duplicate || message.call === undefined || local.some((call) => call.id === message.call))\n\t\t\tconst claims = message.call === undefined ? undefined : owners.get(message.call)\n\t\t\tconst owner = claims?.length === 1 ? claims[0]?.leader : paired ? leader : undefined\n\t\t\treturn { message, owner }\n\t\t})\n\t})\n\tconst owned = Map.groupBy(\n\t\tplaced.flat().filter((entry) => entry.owner !== undefined),\n\t\t(entry) => entry.owner,\n\t)\n\tconst groups = [...distinct].map((leader) => [\n\t\tleader,\n\t\t...(owned.get(leader) ?? []).map((entry) => entry.message),\n\t])\n\tconst orphans = placed.flatMap((run) => {\n\t\tconst orphan = run.filter((entry) => entry.owner === undefined).map((entry) => entry.message)\n\t\treturn orphan.length > 0 ? [orphan] : []\n\t})\n\treturn [...groups, ...orphans]\n}\n\n/**\n * Renders a value as the JSON text `JSON.stringify` produces, or `undefined` when JSON cannot\n * carry the value.\n *\n * @remarks\n * Pure and total. Where `JSON.stringify` returns text, the result is the same text, property order\n * included; `canonicalStringify` from `@orkestrel/contract` sorts keys instead. A value\n * `JSON.stringify` throws on, such as a `BigInt` or a cycle, and a value it renders as nothing,\n * such as `undefined`, a function, or a symbol, return `undefined`.\n *\n * @param value - The value to render\n * @returns The JSON text, or `undefined` when the value has no JSON text\n * @example\n * ```ts\n * renderJSON({ order: 'LH-81660', late: true }) // '{\"order\":\"LH-81660\",\"late\":true}'\n * renderJSON(10n) // undefined\n * renderJSON(undefined) // undefined\n * ```\n */\nexport function renderJSON(value: unknown): string | undefined {\n\tconst outcome = attempt((): string | undefined => JSON.stringify(value))\n\treturn outcome.success ? outcome.value : undefined\n}\n\n/**\n * Matches two values of JSON data by JSON value, whatever their key order, and reads a value with no JSON text as no match.\n *\n * @remarks\n * True exactly when the two values' JSON texts, as `JSON.stringify` renders them, parse to the\n * same JSON value, and false when either side has no JSON text, a cycle aside. A record member\n * whose value is `undefined`, a function, or a symbol is absent, as JSON renders it, and each\n * member is read as an own member, so a `__proto__` or `constructor` key counts only on a side\n * that holds it. A list compares by position and length. The walk stops at the first difference.\n *\n * Bounded to JSON data. It promises nothing for a cycle, a `toJSON` object, `NaN`, an infinity, or\n * an `undefined`, function, symbol, or hole inside a list.\n *\n * @param left - One value\n * @param right - The other value\n * @returns True if the two values are the same JSON value; false otherwise\n * @example\n * ```ts\n * matchesJSON({ order: 'LH-81660', late: true }, { late: true, order: 'LH-81660' }) // true\n * matchesJSON(['LH-81660', 'LH-81661'], ['LH-81661', 'LH-81660']) // false\n * matchesJSON(undefined, undefined) // false\n * ```\n */\nexport function matchesJSON(left: unknown, right: unknown): boolean {\n\tif (typeof left !== 'object' || left === null) {\n\t\treturn (\n\t\t\tleft === right &&\n\t\t\t(typeof left === 'string' ||\n\t\t\t\ttypeof left === 'number' ||\n\t\t\t\ttypeof left === 'boolean' ||\n\t\t\t\tleft === null)\n\t\t)\n\t}\n\tif (isArray(left)) {\n\t\treturn (\n\t\t\tisArray(right) &&\n\t\t\tleft.length === right.length &&\n\t\t\tleft.every((member, index) => matchesJSON(member, right[index]))\n\t\t)\n\t}\n\tif (!isRecord(left) || !isRecord(right)) return false\n\tconst ours = Object.keys(left).filter(\n\t\t(key) =>\n\t\t\tleft[key] !== undefined && typeof left[key] !== 'function' && typeof left[key] !== 'symbol',\n\t)\n\tconst theirs = Object.keys(right).filter(\n\t\t(key) =>\n\t\t\tright[key] !== undefined &&\n\t\t\ttypeof right[key] !== 'function' &&\n\t\t\ttypeof right[key] !== 'symbol',\n\t)\n\treturn (\n\t\tours.length === theirs.length &&\n\t\tours.every((key) => Object.hasOwn(right, key) && matchesJSON(left[key], right[key]))\n\t)\n}\n\n/**\n * Matches a recorded question by JSON value, the ordered sources, the rendered state, and the judge identity.\n *\n * @param judgment - The recorded judgment to compare\n * @param question - The question to ask\n * @param sources - The ordered source message ids\n * @param state - The rendered state to compare\n * @param model - The configured judge identity\n * @returns True if every identity component matches; false otherwise\n * @example\n * ```ts\n * matchesJudgment(judgment, question, ['message-a'], 'Charged twice', judge.model)\n * ```\n */\nexport function matchesJudgment(\n\tjudgment: Judgment,\n\tquestion: JudgeQuestion,\n\tsources: readonly string[],\n\tstate: string,\n\tmodel: string,\n): boolean {\n\treturn (\n\t\tjudgment.model === model &&\n\t\tjudgment.state === state &&\n\t\tjudgment.sources.length === sources.length &&\n\t\tjudgment.sources.every((id, index) => id === sources[index]) &&\n\t\tmatchesJSON(judgment.question, question)\n\t)\n}\n\n/**\n * Reads the ids and the numbers of a text.\n *\n * @param text - The text to read\n * @returns The uppercased hyphenated ids that hold a digit, and the numbers outside those ids with grouping commas removed\n *\n * @example\n * ```ts\n * const literals = extractLiterals('Order bw-5512 totals 1,200.50')\n * // literals.ids is Set { 'BW-5512' }, literals.numbers is Set { 1200.5 }\n * ```\n */\nexport function extractLiterals(text: string): LiteralSet {\n\tconst idShape = /\\b[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+\\b/g\n\tconst ids = new Set(\n\t\t[...text.matchAll(idShape)].flatMap(([token]) =>\n\t\t\t/\\d/.test(token) ? [token.toUpperCase()] : [],\n\t\t),\n\t)\n\tconst rest = text.replace(idShape, (token) => (/\\d/.test(token) ? ' ' : token))\n\tconst numbers = new Set(\n\t\t[...rest.matchAll(/(?<![\\w.])\\d[\\d,]*(?:\\.\\d+)?/g)].flatMap(([token]) => {\n\t\t\tconst value = Number(token.replace(/,/g, ''))\n\t\t\treturn Number.isFinite(value) ? [value] : []\n\t\t}),\n\t)\n\treturn { ids, numbers }\n}\n","import type { JudgeAnswer, JudgeEntry, JudgeQuestion } from './types.js'\nimport {\n\tarrayOf,\n\tattempt,\n\tholds,\n\tisArray,\n\tisFiniteNumber,\n\tisJSONValue,\n\tisRecord,\n\tisString,\n\tnullableOf,\n\toptionalOf,\n} from '@orkestrel/contract'\n\n/**\n * Checks whether a value is a judge entry: a non-empty string, a non-empty JSON record, or a non-empty\n * JSON array.\n *\n * @remarks\n * Total: a cycle, a non-JSON member, a class instance, and an untrusted input return false. `''`,\n * `[]`, and `{}` give a judge nothing to read, so they return false. `null` is not an entry; a\n * criteria guard admits it where the protocol keeps it.\n *\n * @param value - The unknown entry candidate\n * @returns True if the value is a non-empty string or JSON structure the model can read; false otherwise\n * @example\n * ```ts\n * isJudgeEntry('Customer asks for a refund') // true\n * isJudgeEntry({ ticket: 4182, tags: ['billing'] }) // true\n * isJudgeEntry(null) // false\n * isJudgeEntry({ opened: new Date() }) // false\n * ```\n */\nexport function isJudgeEntry(value: unknown): value is JudgeEntry {\n\tif (isString(value)) return value.length > 0\n\tif (isArray(value)) return isJSONValue(value) && value.length > 0\n\treturn isRecord(value) && isJSONValue(value) && Object.keys(value).length > 0\n}\n\n/**\n * Checks whether a value is a well-formed judge question of the choice, score, or noul form.\n *\n * @remarks\n * Total: an untrusted input returns false. A choice needs at least 2 options and a score at least 2\n * levels, because the confidence formula divides by the candidate count; a description or a level\n * can be `null`. `instructions` and noul `criteria` are omitted when absent and never `null`. The\n * guard is exact on the question's own keys: an unknown member, and an own `undefined` optional,\n * return false, as `judgeQuestionContract` does. Server limits on option, level, and question counts\n * are left to the server.\n *\n * @param value - The unknown question candidate\n * @returns True if the value is a question the judge engine can send; false otherwise\n * @example\n * ```ts\n * isJudgeQuestion({ form: 'choice', criteria: { billing: null, bug: 'Software defect' } }) // true\n * isJudgeQuestion({ form: 'score', criteria: ['Cosmetic', null, 'Blocking'] }) // true\n * isJudgeQuestion({ form: 'noul', instructions: 'Is a refund owed?' }) // true\n * isJudgeQuestion({ form: 'choice', criteria: { billing: null } }) // false\n * ```\n */\nexport function isJudgeQuestion(value: unknown): value is JudgeQuestion {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { form, instructions, criteria } = value\n\t\tif (instructions !== undefined && !isJudgeEntry(instructions)) return false\n\t\tconst owned = 1 + (instructions === undefined ? 0 : 1) + (criteria === undefined ? 0 : 1)\n\t\tif (Object.keys(value).length !== owned) return false\n\t\tif (form === 'choice') {\n\t\t\treturn (\n\t\t\t\tisRecord(criteria) &&\n\t\t\t\tObject.keys(criteria).length >= 2 &&\n\t\t\t\tObject.values(criteria).every(nullableOf(isJudgeEntry))\n\t\t\t)\n\t\t}\n\t\tif (form === 'score') {\n\t\t\treturn arrayOf(nullableOf(isJudgeEntry))(criteria) && criteria.length >= 2\n\t\t}\n\t\tif (form !== 'noul') return false\n\t\tif (criteria === undefined) return true\n\t\tif (!isRecord(criteria)) return false\n\t\tconst held = (criteria.true === undefined ? 0 : 1) + (criteria.false === undefined ? 0 : 1)\n\t\treturn (\n\t\t\tObject.keys(criteria).length === held &&\n\t\t\toptionalOf(isJudgeEntry)(criteria.true) &&\n\t\t\toptionalOf(isJudgeEntry)(criteria.false)\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a value is a stored judge answer of the choice, score, or noul form.\n *\n * @remarks\n * Total: an untrusted input returns false. The guard is exact on the answer's own keys and reads each\n * probability as a finite number, with noul bounded to `[0, 1]`. Whether the answer fits\n * its question belongs to the write. Every\n * judgment of a hydrate reads this guard, so it counts keys by hand rather than through a built one,\n * and `judgeAnswerContract` holds it in lockstep: an answer the contract accepts passes here.\n *\n * @param value - The value to test\n * @returns True if the value has the shape of a {@link JudgeAnswer}; false otherwise\n * @example\n * ```ts\n * isJudgeAnswer({ form: 'noul', noul: 0.9 }) // true\n * isJudgeAnswer({ form: 'choice', probabilities: { billing: 1 } }) // true\n * isJudgeAnswer({ form: 'noul', noul: 0.9, note: 'extra' }) // false\n * ```\n */\nexport function isJudgeAnswer(value: unknown): value is JudgeAnswer {\n\treturn holds(() => {\n\t\tif (!isRecord(value) || Object.keys(value).length !== 2) return false\n\t\tconst { form, noul, probabilities } = value\n\t\tif (form === 'noul') return isFiniteNumber(noul) && noul >= 0 && noul <= 1\n\t\tif (form === 'score') return arrayOf(isFiniteNumber)(probabilities)\n\t\treturn (\n\t\t\tform === 'choice' &&\n\t\t\tisRecord(probabilities) &&\n\t\t\tObject.values(probabilities).every(isFiniteNumber)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a positive safe integer.\n * @param value - The value to inspect\n * @returns True if the value is a positive safe integer; false otherwise\n * @example\n * ```ts\n * isPositiveSafeInteger(4096) // true\n * isPositiveSafeInteger(0) // false\n * ```\n */\nexport function isPositiveSafeInteger(value: unknown): value is number {\n\treturn isFiniteNumber(value) && Number.isSafeInteger(value) && value > 0\n}\n\n/**\n * Checks whether a value is a non-negative safe integer, `-0` included.\n *\n * @remarks\n * Total: a non-number, `NaN`, an infinity, a fraction, a negative number, and an integer past\n * `Number.MAX_SAFE_INTEGER` return false. It differs from `isNonNegativeInteger` in\n * `@orkestrel/contract` at two values: that guard rejects `-0` and admits an unsafe integer such\n * as `2 ** 53`, while this guard admits `-0` and rejects `2 ** 53`.\n *\n * @param value - The unknown count or limit candidate\n * @returns True if the value is a safe integer at or above zero; false otherwise\n * @example\n * ```ts\n * isNonNegativeSafeInteger(0) // true\n * isNonNegativeSafeInteger(-0) // true\n * isNonNegativeSafeInteger(2 ** 53) // false\n * isNonNegativeSafeInteger(1.5) // false\n * ```\n */\nexport function isNonNegativeSafeInteger(value: unknown): value is number {\n\treturn isFiniteNumber(value) && Number.isSafeInteger(value) && value >= 0\n}\n","/**\n * Names the opening tag a {@link import('./ThinkSplitter.js').ThinkSplitter} recognizes as the start of\n * an in-content reasoning span — `'<think>'`, the de-facto wire convention thinking models\n * (qwen3, DeepSeek-R1 family) emit their chain-of-thought under when a daemon renders it inline\n * instead of on a separate wire field. Paired with {@link THINK_CLOSE}.\n */\nexport const THINK_OPEN = '<think>'\n\n/**\n * Names the closing tag that ends a {@link THINK_OPEN} reasoning span — `'</think>'`. A span the\n * stream never closes (the model was cut off mid-reasoning) is treated as thinking to its end,\n * and {@link import('./types.js').ThinkSplitterInterface.flush} settles it.\n */\nexport const THINK_CLOSE = '</think>'\n\n/**\n * Holds the default provider deadline in milliseconds — `120_000`, the wall-clock bound a call\n * runs under when `AgentProviderInput.timeout` is omitted, folded with the caller's signal so\n * whichever trips first cancels the call.\n */\nexport const DEFAULT_PROVIDER_TIMEOUT = 120_000\n\n/**\n * Bounds the decoded error excerpt's input in bytes — `2048`, the leading bytes of a non-OK\n * response body handed to the decoder before the read cancels the remainder, so a `ProviderError`\n * message never carries a longer excerpt.\n */\nexport const MAX_ERROR_BODY_LENGTH = 2048\n\n/**\n * Holds the default relay request limit in bytes — `1_048_576`, the byte budget a relay applies\n * to an inbound body when `RelayOptions.limit` is omitted, refusing a body that reaches it.\n */\nexport const DEFAULT_RELAY_LIMIT = 1_048_576\n\n/**\n * Names the relay's newline-delimited JSON content type — `'application/x-ndjson; charset=utf-8'`,\n * the header a relay response carries beside `cache-control: no-store`.\n */\nexport const RELAY_CONTENT_TYPE = 'application/x-ndjson; charset=utf-8'\n\n/**\n * Names the public message for an unexpected upstream relay failure — `'relay provider failed'`,\n * the fixed text every `error` frame carries, so an upstream failure's own message never reaches\n * the browser.\n */\nexport const RELAY_PROVIDER_MESSAGE = 'relay provider failed'\n\n/**\n * Names the status a relay answers when the `authorize` callback returns anything but `true` or throws —\n * `401`, carried with no body and reaching the browser as a `ProviderError` with the `HTTP` code.\n */\nexport const UNAUTHORIZED_RELAY_STATUS = 401\n\n/**\n * Names the status a relay answers when the upstream provider call cannot be constructed — `502`,\n * carried with no body after `provider.stream` was entered and threw before returning its\n * iterator.\n */\nexport const UPSTREAM_RELAY_STATUS = 502\n\n/**\n * Names the status a relay answers for a body that is missing, unreadable, or rejected by\n * `providerRequestContract` — `400`, carried with no body and reaching the browser as a\n * `ProviderError` with the `HTTP` code.\n */\nexport const INVALID_RELAY_STATUS = 400\n\n/**\n * Names the status a relay answers for a request body at or above its byte budget — `413`,\n * carried with no body and answered for an aborted inbound read as well.\n */\nexport const OVERSIZED_RELAY_STATUS = 413\n\n/**\n * Names the status a relay answers for a page system message or tool definition its policy\n * refuses — `422`, carrying one {@link REFUSED_RELAY_CONTENT_TYPE} line per refused member and\n * reaching the browser as a `ProviderError` with the `HTTP` code.\n */\nexport const REFUSED_RELAY_STATUS = 422\n\n/**\n * Names the content type of a relay's `422` refusal body — `'text/plain; charset=utf-8'`, the\n * header a refusal carries beside `cache-control: no-store`.\n */\nexport const REFUSED_RELAY_CONTENT_TYPE = 'text/plain; charset=utf-8'\n\n/** Names the System One decision endpoint shared by compatible servers. */\nexport const SYSTEM_ONE_PATH = '/v1/systemone'\n","import type {\n\tJudgeErrorCode,\n\tProviderErrorCode,\n\tProviderErrorOptions,\n\tProviderResult,\n} from './types.js'\nimport {\n\tINTRINSICS,\n\tholds,\n\tisObject,\n\tisFunction,\n\tisNumber,\n\tisString,\n\tisBoolean,\n\tarrayOf,\n} from '@orkestrel/contract'\n\n/**\n * Reports a provider stream cancelled mid-flight by its bound signal — thrown by the\n * `stream` method of a {@link ProviderInterface}, carrying the {@link ProviderResult} assembled from\n * whatever streamed before the cancel and the machine-readable `code` `'ABORT'`.\n *\n * @remarks\n * Lets a caller recover the partial content (and any tool calls / usage seen so far)\n * on cancellation: `catch` the throw, narrow with {@link isProviderAbortError}, and\n * read `partial`. `code` is the machine-readable condition (`'ABORT'` — the only one this\n * error reports), so a `catch` branches on it rather than on the message string. `cause`\n * holds the failure the cancel superseded when a throw raced the abort — the wire decoder's\n * {@link ProviderError}, say — and is undefined when the cancel was the only failure.\n */\nexport class ProviderAbortError extends Error {\n\t/** Names the machine-readable condition — `'ABORT'`: a stream cancelled mid-flight. */\n\treadonly code = 'ABORT' as const\n\treadonly partial: ProviderResult\n\n\tconstructor(partial: ProviderResult, options?: ErrorOptions) {\n\t\tsuper('provider stream aborted', options)\n\t\tthis.name = 'ProviderAbortError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ProviderAbortError'), { value: this })\n\t\tthis.partial = partial\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link ProviderAbortError} through its identity brand and declared members, so a\n * `catch` can recover its `partial` result.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link ProviderAbortError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tfor await (const delta of provider.stream(messages, signal)) render(delta)\n * } catch (error) {\n * \tif (isProviderAbortError(error)) keep(error.partial.content) // recover partial\n * }\n * ```\n */\nexport function isProviderAbortError(value: unknown): value is ProviderAbortError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ProviderAbortError'))?.value ===\n\t\t\t\t\tvalue\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'ABORT')) return false\n\t\tconst partial = INTRINSICS.reflect.read(value, 'partial')\n\t\tif (!(isObject(partial) || isFunction(partial))) return false\n\t\tconst usage = INTRINSICS.reflect.read(partial, 'usage')\n\t\tconst tools = INTRINSICS.reflect.read(partial, 'tools')\n\t\treturn (\n\t\t\tisString(INTRINSICS.reflect.read(partial, 'content')) &&\n\t\t\t(INTRINSICS.reflect.read(partial, 'thinking') === undefined ||\n\t\t\t\tisString(INTRINSICS.reflect.read(partial, 'thinking'))) &&\n\t\t\t(INTRINSICS.reflect.read(partial, 'truncated') === undefined ||\n\t\t\t\tisBoolean(INTRINSICS.reflect.read(partial, 'truncated'))) &&\n\t\t\t(usage === undefined ||\n\t\t\t\t((isObject(usage) || isFunction(usage)) &&\n\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(usage, key)),\n\t\t\t\t\t))) &&\n\t\t\t(tools === undefined ||\n\t\t\t\tarrayOf(\n\t\t\t\t\t(tool: unknown) =>\n\t\t\t\t\t\t(isObject(tool) || isFunction(tool)) &&\n\t\t\t\t\t\tisString(INTRINSICS.reflect.read(tool, 'id')) &&\n\t\t\t\t\t\tisString(INTRINSICS.reflect.read(tool, 'name')) &&\n\t\t\t\t\t\t(isObject(INTRINSICS.reflect.read(tool, 'arguments')) ||\n\t\t\t\t\t\t\tisFunction(INTRINSICS.reflect.read(tool, 'arguments'))),\n\t\t\t\t)(tools))\n\t\t)\n\t})\n}\n\n/**\n * Reports a coded provider failure with its HTTP status and underlying cause when available.\n *\n * @remarks\n * The provider base throws this error for a non-OK HTTP response, a missing response\n * body, a stream that ended with no settled result and no terminal record, a result or delta the published contract refuses, and with code `'PROTOCOL'`\n * when the framing seam's `parse` or `finish` throws anything but a provider error, with the throw\n * as `cause`. Concrete wire decoders also use it for malformed records and relayed provider\n * failures.\n * `status` is present only for an `HTTP` failure; other codes leave it undefined.\n */\nexport class ProviderError extends Error {\n\t/** Names the machine-readable condition — `'HTTP'`: a non-OK response, including a relay refusing an oversized request with 413; `'PROTOCOL'`: a missing response body, a malformed wire record, or a strict stream with no settled result; `'PROVIDER'`: an upstream failure carried by a relay error record. */\n\treadonly code: ProviderErrorCode\n\t/** Holds the response status for an HTTP failure, or undefined for other codes. */\n\treadonly status: number | undefined\n\t/** Holds what the refusal found, such as its faults, or undefined when it carries none. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\tconstructor(code: ProviderErrorCode, message: string, options?: ProviderErrorOptions) {\n\t\tsuper(message, options)\n\t\tthis.name = 'ProviderError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ProviderError'), { value: this })\n\t\tthis.code = code\n\t\tthis.status = options?.status\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows a caught value to the provider failure class through its identity brand and declared members.\n *\n * @param value - The caught value\n * @returns True if the value is a provider failure; false otherwise\n * @example\n * ```ts\n * isProviderError(new ProviderError('HTTP', 'unavailable', { status: 503 })) // true\n * ```\n */\nexport function isProviderError(value: unknown): value is ProviderError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ProviderError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'HTTP' || code === 'PROTOCOL' || code === 'PROVIDER')) return false\n\t\tconst status = INTRINSICS.reflect.read(value, 'status')\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn (\n\t\t\t(status === undefined || isNumber(status)) &&\n\t\t\t(context === undefined || isObject(context) || isFunction(context))\n\t\t)\n\t})\n}\n\n/**\n * Reports a coded judge failure with its HTTP status and underlying cause when available.\n *\n * @remarks\n * The judge engine throws this error for a non-OK HTTP response, a missing or unparsable\n * response body, and a request refused before inference. Concrete wire decoders also use it for\n * a response they cannot read and for a wire limit. `status` is present only for an `HTTP`\n * failure; other codes leave it undefined.\n */\nexport class JudgeError extends Error {\n\t/** Names the machine-readable condition — `'HTTP'`: a non-OK response; `'PROTOCOL'`: a missing, unparsable, or unreadable response body; `'QUESTION'`: a request refused before inference. */\n\treadonly code: JudgeErrorCode\n\t/** Holds the response status for an HTTP failure, or undefined for other codes. */\n\treadonly status: number | undefined\n\t/** Holds the values that locate the failure, such as the `faults` of a refused question, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\tconstructor(code: JudgeErrorCode, message: string, options?: ProviderErrorOptions) {\n\t\tsuper(message, options)\n\t\tthis.name = 'JudgeError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.JudgeError'), { value: this })\n\t\tthis.code = code\n\t\tthis.status = options?.status\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows a caught value to the judge failure class through its identity brand and declared members.\n *\n * @param value - The caught value\n * @returns True if the value is a {@link JudgeError}; false otherwise\n * @example\n * ```ts\n * isJudgeError(new JudgeError('HTTP', 'judge error: 429', { status: 429 })) // true\n * ```\n */\nexport function isJudgeError(value: unknown): value is JudgeError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.JudgeError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'HTTP' || code === 'PROTOCOL' || code === 'QUESTION')) return false\n\t\tconst status = INTRINSICS.reflect.read(value, 'status')\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn (\n\t\t\t(status === undefined || isNumber(status)) &&\n\t\t\t(context === undefined || isObject(context) || isFunction(context))\n\t\t)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tbooleanShape,\n\tjsonShape,\n\tliteralShape,\n\tobjectShape,\n\toptionalShape,\n\trecordShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\nimport { messageShape, tokenUsageShape, toolCallShape } from '../shapers.js'\n\n/**\n * Describes a provider request's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than `ProviderRequest`: non-JSON arguments, parameters,\n * or schema members are refused. Execution context stays local; the guard refuses\n * extra members, and the parser drops them.\n */\nexport const providerRequestShape = objectShape({\n\tmessages: arrayShape(messageShape),\n\ttools: optionalShape(\n\t\tarrayShape(\n\t\t\tobjectShape({\n\t\t\t\tname: stringShape(),\n\t\t\t\tdescription: optionalShape(stringShape()),\n\t\t\t\tparameters: optionalShape(recordShape(jsonShape())),\n\t\t\t}),\n\t\t),\n\t),\n\toptions: optionalShape(\n\t\tobjectShape({\n\t\t\tthink: optionalShape(booleanShape()),\n\t\t\tschema: optionalShape(recordShape(jsonShape())),\n\t\t}),\n\t),\n}) satisfies ContractShape\n\n/**\n * Describes a provider result's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than `ProviderResult`: non-JSON call arguments are\n * refused. Execution context stays local; the guard refuses extra members, and the parser drops them.\n */\nexport const providerResultShape = objectShape({\n\tcontent: stringShape(),\n\tthinking: optionalShape(stringShape()),\n\ttools: optionalShape(arrayShape(toolCallShape)),\n\tusage: optionalShape(tokenUsageShape),\n\ttruncated: optionalShape(booleanShape()),\n}) satisfies ContractShape\n\n/**\n * Describes the channel-discriminated JSON relay wire projection.\n *\n * @remarks\n * The wire is strictly narrower than `RelayFrame` through its `result` and `partial`\n * fields: non-JSON arguments are refused. Execution context stays local; the guard\n * refuses extra members, and the parser drops them.\n */\nexport const relayFrameShape = unionShape(\n\tobjectShape({ channel: literalShape(['content', 'thinking']), text: stringShape() }),\n\tobjectShape({ channel: literalShape(['result']), result: providerResultShape }),\n\tobjectShape({ channel: literalShape(['abort']), partial: providerResultShape }),\n\tobjectShape({ channel: literalShape(['error']), message: stringShape() }),\n) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { providerRequestShape, providerResultShape, relayFrameShape } from './shapers.js'\n\n/** Validates and projects provider requests at a JSON wire boundary. */\nexport const providerRequestContract = createContract(providerRequestShape)\n\n/** Validates and projects provider results at a JSON wire boundary. */\nexport const providerResultContract = createContract(providerResultShape)\n\n/** Validates and projects channel-discriminated relay frames at a JSON wire boundary. */\nexport const relayFrameContract = createContract(relayFrameShape)\n","import type { ProviderResult, SystemOneAnswer, SystemOneResponse } from './types.js'\nimport type { Guard } from '@orkestrel/contract'\nimport { isBudgetAmount } from '@orkestrel/budget'\nimport {\n\tarrayOf,\n\tattempt,\n\tboundsOf,\n\tisArray,\n\tisBoolean,\n\tisNumber,\n\tisRecord,\n\tisString,\n\tnullableOf,\n\tobjectOf,\n\toptionalOf,\n} from '@orkestrel/contract'\nimport { isToolCall } from '@orkestrel/tool'\n\n/**\n * Checks whether a value carries the members {@link ProviderResult} publishes: a string `content`, and\n * `thinking`, `tools`, `usage`, and `truncated` absent or of their published types.\n *\n * @remarks\n * A provider is a foreign interface, so the guard is open: unknown members and a class instance pass,\n * and each `usage` count is a finite nonnegative number with no integer check. An unreadable member\n * returns false. {@link import('./helpers.js').requireProviderResult} runs it on an owned copy, the one\n * door a run's result crosses.\n *\n * @param value - The unknown result candidate\n * @returns True if the value satisfies the published result; false otherwise\n *\n * @example\n * ```ts\n * isProviderResult({ content: 'answer', extra: true }) // true\n * isProviderResult({}) // false\n * isProviderResult({ content: 'answer', usage: { prompt: -1, completion: 0, total: 0 } }) // false\n * ```\n */\nexport const isProviderResult: Guard<ProviderResult> = objectOf(\n\t{\n\t\tcontent: isString,\n\t\tthinking: isString,\n\t\ttools: arrayOf(isToolCall),\n\t\tusage: objectOf({ prompt: isBudgetAmount, completion: isBudgetAmount, total: isBudgetAmount }),\n\t\ttruncated: isBoolean,\n\t},\n\t['thinking', 'tools', 'usage', 'truncated'],\n)\n\n/**\n * Checks whether a value is a System One response envelope with optional model and usage.\n *\n * @remarks\n * Answers remain unknown until checked against their questions. Missing and null usage counts\n * are accepted. Extra members are ignored, and unreadable fields return false.\n *\n * @param value - The unknown response candidate\n * @returns True if the envelope fields have their wire types; false otherwise\n * @example\n * ```ts\n * isSystemOneResponse({ answers: {}, usage: { input_tokens: null } }) // true\n * ```\n */\nexport function isSystemOneResponse(value: unknown): value is SystemOneResponse {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { model, answers, usage } = value\n\t\tif (!optionalOf(isString)(model) || !isRecord(answers)) return false\n\t\tif (usage === undefined) return true\n\t\tif (!isRecord(usage)) return false\n\t\treturn (\n\t\t\toptionalOf(nullableOf(isNumber))(usage.input_tokens) &&\n\t\t\toptionalOf(nullableOf(isNumber))(usage.output_tokens)\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a value is a System One answer whose type and distribution the wire can read.\n *\n * @remarks\n * The guard checks `type` and the distribution the wire dereferences: the `noul` number for a\n * noul, the `probabilities` map for a choice, and the `probabilities` map or dense array for a\n * score, each probability a finite number in [0, 1]. The `choice`, `score`, `confidence`, and\n * `legend` members are carried unchecked, because the wire never reads them, so a value this\n * guard accepts can hold any value in those members. Distribution sums are not constrained;\n * values are never normalized. A read that throws returns false.\n *\n * @param value - The unknown answer candidate\n * @returns True if the type and the distribution satisfy the wire contract; false otherwise\n * @example\n * ```ts\n * isSystemOneAnswer({ type: 'noul', noul: 0.9 }) // true\n * isSystemOneAnswer({ type: 'score', probabilities: [0.2, 0.8] }) // true\n * isSystemOneAnswer({ type: 'noul', noul: 1.1 }) // false\n * ```\n */\nexport function isSystemOneAnswer(value: unknown): value is SystemOneAnswer {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { type } = value\n\t\tif (type === 'noul') return boundsOf(0, 1)(value.noul)\n\t\tif (type !== 'choice' && type !== 'score') return false\n\t\tconst { probabilities } = value\n\t\tif (type === 'score' && isArray(probabilities)) {\n\t\t\treturn arrayOf(boundsOf(0, 1))(probabilities)\n\t\t}\n\t\treturn isRecord(probabilities) && Object.values(probabilities).every(boundsOf(0, 1))\n\t})\n\treturn checked.success && checked.value\n}\n","import type {\n\tAnswerMeasure,\n\tFailureRead,\n\tProviderDelta,\n\tProviderOptions,\n\tProviderRequest,\n\tProviderResult,\n\tRelayOptions,\n\tRelayRefusal,\n\tSystemOneAnswer,\n\tSystemOneQuestion,\n\tSystemOneUsage,\n\tTextRead,\n} from './types.js'\nimport type { JudgeAnswer, JudgeQuestion, JudgeResult, Message, Refusal } from '../types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport type { Result } from '@orkestrel/contract'\nimport type { ToolCall, ToolDefinition } from '@orkestrel/tool'\nimport { isTokenUsage } from '@orkestrel/budget'\nimport {\n\tattempt,\n\tboundsOf,\n\tcanonicalStringify,\n\tfailure,\n\tisArray,\n\tisFunction,\n\tisNumber,\n\tisObject,\n\tpreview,\n\tsuccess,\n} from '@orkestrel/contract'\nimport { JudgeError, ProviderError } from './errors.js'\nimport { MAX_ERROR_BODY_LENGTH } from './constants.js'\nimport { isProviderResult } from './validators.js'\nimport { sanitizeUsage, sumUsage } from '../helpers.js'\n\n/**\n * Assembles a provider result with only populated optional fields.\n *\n * @param content - The authoritative answer\n * @param thinking - The joined reasoning\n * @param tools - The accumulated calls\n * @param usage - The reported token usage\n * @param truncated - Whether the model stopped at its own limit, or undefined when the wire did not report it\n * @returns The assembled result\n * @example\n * ```ts\n * buildProviderResult('ok', '', [], undefined, undefined) // { content: 'ok' }\n * ```\n */\nexport function buildProviderResult(\n\tcontent: string,\n\tthinking: string,\n\ttools: readonly ToolCall[],\n\tusage: TokenUsage | undefined,\n\ttruncated: boolean | undefined,\n): ProviderResult {\n\treturn {\n\t\tcontent,\n\t\t...(thinking.length === 0 ? {} : { thinking }),\n\t\t...(tools.length === 0 ? {} : { tools }),\n\t\t...(usage === undefined ? {} : { usage }),\n\t\t...(truncated === undefined ? {} : { truncated }),\n\t}\n}\n\n/**\n * Reads a foreign delta's two members once into an owned copy, the value a run checks and consumes.\n *\n * @remarks\n * A stream checks every delta, so this is a hand read rather than a record guard. Reading each member\n * once means a getter can't pass the check with one value and hand the run another. Unknown members\n * and a class instance pass, and an unreadable member reads as no delta.\n *\n * @param value - The value the stream yielded\n * @returns The owned delta, or undefined when the value is not one\n *\n * @example\n * ```ts\n * readProviderDelta({ channel: 'content', text: 'Hi' }) // { channel: 'content', text: 'Hi' }\n * readProviderDelta({ channel: 'audio', text: 'Hi' }) // undefined\n * ```\n */\nexport function readProviderDelta(value: unknown): ProviderDelta | undefined {\n\tif (typeof value !== 'object' || value === null) return undefined\n\ttry {\n\t\tconst { channel, text }: { readonly channel?: unknown; readonly text?: unknown } = value\n\t\treturn (channel === 'content' || channel === 'thinking') && typeof text === 'string'\n\t\t\t? { channel, text }\n\t\t\t: undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * Owns a provider's settled result: reads each published member of the foreign value once into a fresh\n * record, checks that copy, and returns it, or throws a coded error the run rejects with.\n *\n * @remarks\n * The check is {@link isProviderResult}, so a class instance carrying the published members passes and\n * an unknown member is left behind. `usage` is copied count by count; `tools` keeps the array the\n * provider returned. An absent `usage` stays absent, which the loop bills as its estimate.\n *\n * @param value - The value a provider's stream returned\n * @returns The owned result\n * @throws {ProviderError} Thrown when the value does not carry the published members (code `PROTOCOL`)\n *\n * @example\n * ```ts\n * requireProviderResult({ content: 'answer', extra: true }) // { content: 'answer' }\n * requireProviderResult({}) // throws ProviderError ('PROTOCOL')\n * ```\n */\nexport function requireProviderResult(value: unknown): ProviderResult {\n\tconst owned = attempt(() => {\n\t\tif (!isObject(value) && !isFunction(value)) return undefined\n\t\tconst thinking = Reflect.get(value, 'thinking')\n\t\tconst tools = Reflect.get(value, 'tools')\n\t\tconst usage = Reflect.get(value, 'usage')\n\t\tconst truncated = Reflect.get(value, 'truncated')\n\t\treturn {\n\t\t\tcontent: Reflect.get(value, 'content'),\n\t\t\t...(thinking === undefined ? {} : { thinking }),\n\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t...(usage === undefined\n\t\t\t\t? {}\n\t\t\t\t: {\n\t\t\t\t\t\tusage: isObject(usage)\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tprompt: Reflect.get(usage, 'prompt'),\n\t\t\t\t\t\t\t\t\tcompletion: Reflect.get(usage, 'completion'),\n\t\t\t\t\t\t\t\t\ttotal: Reflect.get(usage, 'total'),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: usage,\n\t\t\t\t\t}),\n\t\t\t...(truncated === undefined ? {} : { truncated }),\n\t\t}\n\t})\n\tif (owned.success && isProviderResult(owned.value)) return owned.value\n\tthrow new ProviderError(\n\t\t'PROTOCOL',\n\t\t`provider error: a provider must return string content, with thinking, tools, usage, and truncated absent or of their published types, and usage counts finite and nonnegative; received ${preview(owned.success ? owned.value : value)}`,\n\t\towned.success ? undefined : { cause: owned.error },\n\t)\n}\n\n/**\n * Cancels a stream reader and releases its lock, swallowing a cancellation failure so the\n * caller's own outcome stands.\n *\n * @param reader - The reader to cancel and release\n * @returns A promise that settles after the lock is released\n *\n * @example\n * ```ts\n * const body = new Response('answer').body\n * if (body !== null) await releaseReader(body.getReader())\n * ```\n */\nexport async function releaseReader(\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n): Promise<void> {\n\ttry {\n\t\tawait reader.cancel()\n\t} catch {\n\t\t// Preserve the caller's outcome when the source refuses cancellation.\n\t} finally {\n\t\treader.releaseLock()\n\t}\n}\n\n/**\n * Arms signal propagation to a byte reader and returns the controller that removes the listener.\n *\n * @param reader - The reader whose pending read an abort releases\n * @param signal - The optional caller signal\n * @returns The listener cleanup controller after any already-aborted cancellation settles\n * @example\n * ```ts\n * const reader = new ReadableStream<Uint8Array>().getReader()\n * const cleanup = await armReaderAbort(reader, new AbortController().signal)\n * cleanup.abort()\n * await releaseReader(reader)\n * ```\n */\nexport async function armReaderAbort(\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n\tsignal: AbortSignal | undefined,\n): Promise<AbortController> {\n\tconst cleanup = new AbortController()\n\tsignal?.addEventListener(\n\t\t'abort',\n\t\t() => {\n\t\t\tvoid reader.cancel(signal.reason).catch(() => {\n\t\t\t\t// Preserve the caller's outcome when source cancellation rejects.\n\t\t\t})\n\t\t},\n\t\t{ once: true, signal: cleanup.signal },\n\t)\n\tif (signal?.aborted) {\n\t\tawait reader.cancel(signal.reason).catch(() => {\n\t\t\t// An already-aborted read retains the same cancellation contract.\n\t\t})\n\t\tcleanup.abort()\n\t}\n\treturn cleanup\n}\n\n/**\n * Reads a bounded HTTP failure excerpt and preserves an unreadable body's cause.\n *\n * @remarks\n * Both HTTP engines, the provider and the judge, translate a non-OK response here and throw\n * their own coded error from the result. When the excerpt is read and `signal` has aborted, the\n * abort is the call's outcome, so the signal's reason is thrown. When the body read fails, the\n * failure is returned as `cause` whether or not `signal` has aborted, so a read that raced the\n * abort stays recorded on the error the caller throws.\n *\n * @param response - The failed HTTP response\n * @param prefix - The error taxonomy prefix, including its trailing colon\n * @param signal - The call's combined caller and deadline signal\n * @returns The status message with any excerpt, the response status, and any body-read failure\n * @throws Thrown when the excerpt is read after `signal` aborts, with the signal's reason\n * @example\n * ```ts\n * await readFailure(new Response('unavailable', { status: 503 }), 'provider error:', new AbortController().signal)\n * // { message: 'provider error: 503 - unavailable', status: 503 }\n * ```\n */\nexport async function readFailure(\n\tresponse: Response,\n\tprefix: string,\n\tsignal: AbortSignal,\n): Promise<FailureRead> {\n\tconst status = response.status\n\tconst body = response.body\n\tif (body === null) {\n\t\tsignal.throwIfAborted()\n\t\treturn { message: `${prefix} ${status}`, status }\n\t}\n\treturn readBodyText(body, MAX_ERROR_BODY_LENGTH, signal).then(\n\t\t(read) => {\n\t\t\tsignal.throwIfAborted()\n\t\t\treturn { message: `${prefix} ${status}${read.text === '' ? '' : ` - ${read.text}`}`, status }\n\t\t},\n\t\t(cause: unknown) => ({\n\t\t\tmessage: `${prefix} ${status} - (error body unavailable)`,\n\t\t\tstatus,\n\t\t\t...(cause === undefined ? {} : { cause }),\n\t\t}),\n\t)\n}\n\n/**\n * Reads a UTF-8 prefix of a byte stream and cancels its remainder.\n *\n * @remarks\n * The `limit` parameter bounds bytes passed to the decoder, including a partial final\n * character. An omitted limit reads to completion. A source may deliver a chunk larger\n * than the remaining limit; its unused bytes are discarded without decoding.\n * The read can overshoot by one source chunk. An abort leaves completion false.\n *\n * @param body - The readable byte stream\n * @param limit - The maximum byte prefix, or undefined for the complete stream\n * @param signal - The optional cancellation bound; abort returns the decoded prefix\n * @returns The decoded prefix and whether EOF occurred within the byte budget\n * @example\n * ```ts\n * import { readBodyText } from '@orkestrel/agent'\n *\n * const body = new Response('answer').body\n * if (body !== null) await readBodyText(body, 3) // { text: 'ans', complete: false }\n * ```\n */\nexport async function readBodyText(\n\tbody: ReadableStream<Uint8Array>,\n\tlimit?: number,\n\tsignal?: AbortSignal,\n): Promise<TextRead> {\n\tconst reader = body.getReader()\n\tconst decoder = new TextDecoder()\n\ttry {\n\t\tconst cleanup = await armReaderAbort(reader, signal)\n\t\ttry {\n\t\t\t// Each step's decoded text and the byte budget left after it: the next read's budget is the\n\t\t\t// latest step's, and the text joins every step.\n\t\t\tconst steps: Array<{ readonly text: string; readonly remaining: number }> = []\n\t\t\tfor (;;) {\n\t\t\t\tconst remaining = steps.at(-1)?.remaining ?? limit ?? Infinity\n\t\t\t\tconst step = remaining > 0 && signal?.aborted !== true ? await reader.read() : undefined\n\t\t\t\tif (step === undefined || signal?.aborted === true || step.done) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttext: steps.map((one) => one.text).join('') + decoder.decode(),\n\t\t\t\t\t\tcomplete: step?.done === true && signal?.aborted !== true,\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst bytes = step.value.subarray(0, remaining)\n\t\t\t\tsteps.push({\n\t\t\t\t\ttext: decoder.decode(bytes, { stream: true }),\n\t\t\t\t\tremaining: remaining - bytes.byteLength,\n\t\t\t\t})\n\t\t\t}\n\t\t} finally {\n\t\t\tcleanup.abort()\n\t\t}\n\t} finally {\n\t\tawait releaseReader(reader)\n\t}\n}\n\n/**\n * Decodes UTF-8 chunks with a final flush and releases the stream on every exit.\n *\n * @param body - The readable byte stream\n * @param signal - The optional cancellation bound; abort ends iteration without further chunks\n * @returns Decoded text chunks, including a held decoder tail\n * @example\n * ```ts\n * const body = new Response('answer').body\n * if (body !== null) for await (const chunk of readChunks(body)) render(chunk)\n * ```\n */\nexport async function* readChunks(\n\tbody: ReadableStream<Uint8Array>,\n\tsignal?: AbortSignal,\n): AsyncGenerator<string> {\n\tconst reader = body.getReader()\n\tconst decoder = new TextDecoder()\n\ttry {\n\t\tconst cleanup = await armReaderAbort(reader, signal)\n\t\ttry {\n\t\t\tif (signal?.aborted) return\n\t\t\tfor (;;) {\n\t\t\t\tconst step = await reader.read()\n\t\t\t\tif (signal?.aborted) return\n\t\t\t\tif (step.done) break\n\t\t\t\tconst chunk = decoder.decode(step.value, { stream: true })\n\t\t\t\tif (chunk.length > 0) yield chunk\n\t\t\t}\n\t\t\tconst tail = decoder.decode()\n\t\t\tif (tail.length > 0) yield tail\n\t\t} finally {\n\t\t\tcleanup.abort()\n\t\t}\n\t} finally {\n\t\tawait releaseReader(reader)\n\t}\n}\n\n/**\n * Derives the winner, its probability, the published confidence, and a score answer's expected\n * level from a judge answer's distribution.\n *\n * @remarks\n * The winner is the first strictly greatest candidate in enumeration order: an option name in\n * criteria order for a fresh answer and in the order its store returned for a restored record,\n * a level index, or `'false'` then `'true'` for a noul, so a noul of exactly 0.5\n * names `'false'`. Confidence follows the TypeSafe formulas, whatever confidence a server sent: a\n * choice reads `(max(p) - 1/n) / (1 - 1/n)`, a noul reads `|2p - 1|` (the choice formula at\n * n = 2), and a score reads `max(0, 1 - spread / even_spread)`, where `spread` sums each level's\n * probability times its distance from the winning level and `even_spread` is the mean distance\n * from the middle level. `score` is the expected level `sum(i * p_i)` and is set only for a score\n * answer.\n *\n * @param answer - The answer whose distribution is read\n * @returns The derived measures\n * @throws JudgeError Thrown when a choice or score answer has fewer than 2 candidates\n * (code `PROTOCOL`), because the confidence formulas divide by the candidate count\n * @example\n * ```ts\n * measureAnswer({ form: 'choice', probabilities: { billing: 0.88, technical: 0.12, sales: 0 } })\n * // { winner: 'billing', probability: 0.88, confidence: 0.82 } to 2 decimals\n * measureAnswer({ form: 'score', probabilities: [0, 0.57, 0.43] })\n * // { winner: '1', probability: 0.57, confidence: 0.355, score: 1.43 } to 3 decimals\n * measureAnswer({ form: 'noul', noul: 0.5 }) // { winner: 'false', probability: 0.5, confidence: 0 }\n * ```\n */\nexport function measureAnswer(answer: JudgeAnswer): AnswerMeasure {\n\tif (answer.form === 'noul') {\n\t\tconst yes = answer.noul > 0.5\n\t\treturn {\n\t\t\twinner: yes ? 'true' : 'false',\n\t\t\tprobability: yes ? answer.noul : 1 - answer.noul,\n\t\t\tconfidence: Math.abs(2 * answer.noul - 1),\n\t\t}\n\t}\n\tconst candidates: ReadonlyArray<readonly [string, number]> =\n\t\tanswer.form === 'choice'\n\t\t\t? Object.entries(answer.probabilities)\n\t\t\t: answer.probabilities.map((probability, level) => [String(level), probability])\n\tconst count = candidates.length\n\tif (count < 2) {\n\t\tthrow new JudgeError('PROTOCOL', 'judge error: an answer needs at least 2 candidates')\n\t}\n\t// The first strictly greatest candidate wins a tie. The spread visits a hole in a score\n\t// distribution as an undefined candidate, which the destructuring refuses; `reduce` skips it.\n\tconst { winner, index, probability } = [...candidates].reduce(\n\t\t(top, [name, candidate], position) =>\n\t\t\tcandidate > top.probability ? { winner: name, index: position, probability: candidate } : top,\n\t\t{ winner: '', index: 0, probability: -Infinity },\n\t)\n\tif (answer.form === 'choice') {\n\t\treturn { winner, probability, confidence: (probability - 1 / count) / (1 - 1 / count) }\n\t}\n\tconst spread = answer.probabilities.reduce(\n\t\t(sum, candidate, level) => sum + candidate * Math.abs(level - index),\n\t\t0,\n\t)\n\tconst even = answer.probabilities.reduce(\n\t\t(sum, _candidate, level) => sum + Math.abs(level - (count - 1) / 2),\n\t\t0,\n\t)\n\tconst score = answer.probabilities.reduce((sum, candidate, level) => sum + level * candidate, 0)\n\treturn { winner, probability, confidence: Math.max(0, 1 - spread / (even / count)), score }\n}\n\n/**\n * Merges the results of a judge request's calls into one result.\n *\n * @remarks\n * Pure and total. Answers and refusals are joined by question id; `refusals` is omitted when no\n * call refused a question. Each call's usage passes through {@link sanitizeUsage} and the totals\n * add through {@link sumUsage}; `usage` is omitted when no call reported one. The model is the\n * first call's, or the given model when the list is empty, as the empty partial of a cancel\n * before the first call requires.\n *\n * @param model - The judge's configured model, reported when no call completed\n * @param results - The completed calls' results in call order\n * @returns The merged result\n * @example\n * ```ts\n * buildJudgeResult('tev1:0.8b', []) // { model: 'tev1:0.8b', answers: {} }\n * buildJudgeResult('jev-latest', [\n * \t{ model: 'jev-1.13.0', answers: { urgent: { form: 'noul', noul: 0.95 } } },\n * \t{ model: 'jev-1.13.0', answers: {}, refusals: { team: { missing: ['sales'] } } },\n * ])\n * // { model: 'jev-1.13.0', answers: { urgent: ... }, refusals: { team: { missing: ['sales'] } } }\n * ```\n */\nexport function buildJudgeResult(model: string, results: readonly JudgeResult[]): JudgeResult {\n\t// The spread visits a hole in `results` as an undefined result, as iteration does; `reduce` skips it.\n\tconst calls = [...results]\n\tconst answers = calls.reduce<Readonly<Record<string, JudgeAnswer>>>(\n\t\t(merged, result) => ({ ...merged, ...result.answers }),\n\t\t{},\n\t)\n\tconst refusals = calls.reduce<Readonly<Record<string, Refusal>>>(\n\t\t(merged, result) => ({ ...merged, ...result.refusals }),\n\t\t{},\n\t)\n\tconst usage = calls.reduce<TokenUsage | undefined>(\n\t\t(total, result) =>\n\t\t\tresult.usage === undefined ? total : sumUsage(total, sanitizeUsage(result.usage)),\n\t\tundefined,\n\t)\n\treturn {\n\t\tmodel: results[0]?.model ?? model,\n\t\tanswers,\n\t\t...(Object.keys(refusals).length === 0 ? {} : { refusals }),\n\t\t...(usage === undefined ? {} : { usage }),\n\t}\n}\n\n/**\n * Builds a request's JSON headers, awaiting the caller's header hook inside the call's\n * cancellation bound.\n *\n * @remarks\n * The headers start with `Content-Type: application/json`; each entry the hook returns is set\n * over them, so the hook overrides the content type only when it returns that header. The hook\n * receives `signal` and races its abort, and the abort listener is removed on every exit. Both\n * HTTP engines, the provider and the judge, read their headers here.\n *\n * @param hook - The caller's header hook, or undefined for the JSON content type alone\n * @param signal - The call's combined caller and deadline signal\n * @returns The request headers\n * @throws Thrown when the signal aborts before the hook settles, with the signal's reason, and\n * when the hook throws or rejects, with the hook's own failure\n * @example\n * ```ts\n * const signal = new AbortController().signal\n * // API_KEY stands for the server's bearer key.\n * const headers = await buildProviderHeaders(() => ({ authorization: 'Bearer API_KEY' }), signal)\n * headers.get('authorization') // 'Bearer API_KEY'\n * headers.get('content-type') // 'application/json'\n * ```\n */\nexport async function buildProviderHeaders(\n\thook: ProviderOptions['headers'],\n\tsignal: AbortSignal,\n): Promise<Headers> {\n\tconst headers = new Headers({ 'Content-Type': 'application/json' })\n\tif (hook === undefined) return headers\n\tconst cleanup = new AbortController()\n\tconst aborted = Promise.withResolvers<never>()\n\tsignal.addEventListener('abort', () => aborted.reject(signal.reason), {\n\t\tonce: true,\n\t\tsignal: cleanup.signal,\n\t})\n\ttry {\n\t\tsignal.throwIfAborted()\n\t\tconst entries = await Promise.race([\n\t\t\tPromise.resolve().then(() => hook(signal)),\n\t\t\taborted.promise,\n\t\t])\n\t\tfor (const [key, value] of Object.entries(entries)) headers.set(key, value)\n\t\treturn headers\n\t} finally {\n\t\tcleanup.abort()\n\t}\n}\n\n/**\n * Projects a provider request onto the declared members the relay wire carries.\n *\n * @remarks\n * Each message keeps `id`, `role`, `content`, and any `calls`, `call`, `images`, and `thinking`;\n * each call keeps only `id`, `name`, and `arguments`, so a local execution context never reaches\n * the wire. Each tool keeps `name` and any `description` and `parameters`, and the options keep\n * any `think` and `schema`. An absent member stays absent. The projection shares every nested\n * value with the input rather than copying it, so the caller snapshots and validates the result;\n * a throwing getter propagates its failure.\n *\n * @param request - The domain conversation and call configuration\n * @returns The request with only the members the relay wire declares\n * @example\n * ```ts\n * requestToRelay({\n * \tmessages: [{ id: 'turn-1', role: 'user', content: 'Track BW-5512' }],\n * \toptions: { think: false },\n * })\n * // { messages: [{ id: 'turn-1', role: 'user', content: 'Track BW-5512' }], options: { think: false } }\n * ```\n */\nexport function requestToRelay(request: ProviderRequest): ProviderRequest {\n\treturn {\n\t\tmessages: request.messages.map((message) => ({\n\t\t\tid: message.id,\n\t\t\trole: message.role,\n\t\t\tcontent: message.content,\n\t\t\t...(message.calls === undefined\n\t\t\t\t? {}\n\t\t\t\t: {\n\t\t\t\t\t\tcalls: message.calls.map((call) => ({\n\t\t\t\t\t\t\tid: call.id,\n\t\t\t\t\t\t\tname: call.name,\n\t\t\t\t\t\t\targuments: call.arguments,\n\t\t\t\t\t\t})),\n\t\t\t\t\t}),\n\t\t\t...(message.call === undefined ? {} : { call: message.call }),\n\t\t\t...(message.images === undefined ? {} : { images: message.images }),\n\t\t\t...(message.thinking === undefined ? {} : { thinking: message.thinking }),\n\t\t})),\n\t\t...(request.tools === undefined\n\t\t\t? {}\n\t\t\t: {\n\t\t\t\t\ttools: request.tools.map((tool) => ({\n\t\t\t\t\t\tname: tool.name,\n\t\t\t\t\t\t...(tool.description === undefined ? {} : { description: tool.description }),\n\t\t\t\t\t\t...(tool.parameters === undefined ? {} : { parameters: tool.parameters }),\n\t\t\t\t\t})),\n\t\t\t\t}),\n\t\t...(request.options === undefined\n\t\t\t? {}\n\t\t\t: {\n\t\t\t\t\toptions: {\n\t\t\t\t\t\t...(request.options.think === undefined ? {} : { think: request.options.think }),\n\t\t\t\t\t\t...(request.options.schema === undefined ? {} : { schema: request.options.schema }),\n\t\t\t\t\t},\n\t\t\t\t}),\n\t}\n}\n\n/**\n * Builds the request a relay forwards under its system turn and tool definition policy, or every\n * refusal that policy finds.\n *\n * @remarks\n * One pass collects every refused member. A page `system` message is refused unless\n * `policy.system.accept` is `true`; under it, only one content-only page message at index 0 is\n * admitted. When `policy.system.text` is defined, the forwarded request starts with one composed\n * system message whose content is that text, joined with `'\\n\\n'` before an admitted page head's\n * content, and the page head is removed; when it is undefined, an admitted page head is forwarded\n * unchanged. A page tool definition of a declared name must equal the declared one after both are\n * projected as {@link requestToRelay} projects them, key order ignored, and the declared copy is\n * forwarded; a definition of an undeclared name is forwarded only when `policy.tools.accept` is\n * `true`; each occurrence of a name after its first is refused. The forwarded tools keep the\n * page's order and stay absent when the page sent none; the messages after the head and the\n * options pass unchanged. The helper is deterministic except the composed head's id, which takes\n * `crypto.randomUUID()`.\n *\n * @param request - The page request the relay contract admitted\n * @param policy - The relay's system turn and tool definition policy\n * @returns The request to forward, or every refusal in message then tool order\n * @throws {ContractError} Thrown when comparing a page definition with the declared one of its\n * name reads a `parameters` member that throws, such as a getter or a `Proxy` trap, which parsed\n * JSON never has\n * @example\n * ```ts\n * const built = buildRelayRequest(\n * \t{ messages: [{ id: 'u1', role: 'user', content: 'Track BW-5512' }] },\n * \t{ system: { text: 'Answer for the shipping desk.' } },\n * )\n * built.success && built.value.messages[0]?.content // 'Answer for the shipping desk.'\n * ```\n */\nexport function buildRelayRequest(\n\trequest: ProviderRequest,\n\tpolicy: Pick<RelayOptions, 'system' | 'tools'>,\n): Result<ProviderRequest, readonly RelayRefusal[]> {\n\tconst refusals: RelayRefusal[] = []\n\tconst admits = policy.system?.accept === true\n\tfor (const [index, message] of request.messages.entries()) {\n\t\tif (message.role !== 'system') continue\n\t\tif (!admits) {\n\t\t\trefusals.push({ reason: 'system', index })\n\t\t\tcontinue\n\t\t}\n\t\tif (\n\t\t\tindex !== 0 ||\n\t\t\tmessage.thinking !== undefined ||\n\t\t\tmessage.calls !== undefined ||\n\t\t\tmessage.call !== undefined ||\n\t\t\tmessage.images !== undefined\n\t\t) {\n\t\t\trefusals.push({ reason: 'head', index })\n\t\t}\n\t}\n\tconst declared = new Map<string, ToolDefinition>()\n\tconst definitions = policy.tools?.definitions ?? []\n\tfor (const tool of requestToRelay({ messages: [], tools: definitions }).tools ?? []) {\n\t\tdeclared.set(tool.name, tool)\n\t}\n\tconst sent =\n\t\trequest.tools === undefined\n\t\t\t? undefined\n\t\t\t: requestToRelay({ messages: [], tools: request.tools }).tools\n\tconst forwarded: ToolDefinition[] = []\n\tconst seen = new Set<string>()\n\tfor (const [index, tool] of (sent ?? []).entries()) {\n\t\tconst server = declared.get(tool.name)\n\t\tif (seen.has(tool.name)) {\n\t\t\trefusals.push({ reason: 'mismatch', index, name: tool.name })\n\t\t} else if (server !== undefined) {\n\t\t\tconst encoded = canonicalStringify(server)\n\t\t\tif (encoded === undefined || encoded !== canonicalStringify(tool)) {\n\t\t\t\trefusals.push({ reason: 'mismatch', index, name: tool.name })\n\t\t\t} else forwarded.push(server)\n\t\t} else if (policy.tools?.accept === true) {\n\t\t\tforwarded.push(tool)\n\t\t} else refusals.push({ reason: 'tool', index, name: tool.name })\n\t\tseen.add(tool.name)\n\t}\n\tif (refusals.length > 0) return failure(refusals)\n\tconst text = policy.system?.text\n\tconst first = request.messages[0]\n\tconst page = first?.role === 'system' ? first : undefined\n\tconst head: Message | undefined =\n\t\ttext === undefined\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\tid: crypto.randomUUID(),\n\t\t\t\t\trole: 'system',\n\t\t\t\t\tcontent: page === undefined ? text : `${text}\\n\\n${page.content}`,\n\t\t\t\t}\n\tconst messages =\n\t\thead === undefined\n\t\t\t? request.messages\n\t\t\t: [head, ...(page === undefined ? request.messages : request.messages.slice(1))]\n\treturn success({\n\t\tmessages,\n\t\t...(sent === undefined ? {} : { tools: forwarded }),\n\t\t...(request.options === undefined ? {} : { options: request.options }),\n\t})\n}\n\n/**\n * Describes one relay refusal as the line its `422` response body carries.\n *\n * @remarks\n * A line holds only the member's key path, the expected shape, the option that admits the member,\n * and, for a tool, the name the page sent as `preview` renders it, so no page content or server\n * text reaches it. `preview` JSON-encodes the name; an encoding longer than 64 characters renders\n * as its opening quote, at most 63 escaped characters that split no escape or surrogate pair, and\n * `…`, so a rendered name holds at most 65 characters. A line names only the tool names the page\n * sent, so the page learns no server tool name it did not send.\n *\n * @param refusal - The refused member and where it sits\n * @returns The refusal line\n * @example\n * ```ts\n * describeRelayRefusal({ reason: 'tool', index: 0, name: 'weather' })\n * // 'tools[0] \"weather\": the relay refuses a tool it does not declare; declare it in tools.definitions, or set tools.accept to admit page tools'\n * ```\n */\nexport function describeRelayRefusal(refusal: RelayRefusal): string {\n\tswitch (refusal.reason) {\n\t\tcase 'system':\n\t\t\treturn `messages[${refusal.index}]: the relay refuses a system message; the server writes the system turn through system.text, and system.accept admits one page system message at index 0`\n\t\tcase 'head':\n\t\t\treturn `messages[${refusal.index}]: the relay admits one system message, at index 0, carrying content only`\n\t\tcase 'tool':\n\t\t\treturn `tools[${refusal.index}] ${preview(refusal.name)}: the relay refuses a tool it does not declare; declare it in tools.definitions, or set tools.accept to admit page tools`\n\t\tcase 'mismatch':\n\t\t\treturn `tools[${refusal.index}] ${preview(refusal.name)}: the definition must equal the relay's declared definition of that name, and a name appears once`\n\t}\n}\n\n/**\n * Projects a judge question onto the System One wire while preserving omitted members.\n *\n * @param question - The domain question to send\n * @returns The wire question with `type` replacing `form`\n * @example\n * ```ts\n * questionToSystemOne({ form: 'noul' }) // { type: 'noul' }\n * ```\n */\nexport function questionToSystemOne(question: JudgeQuestion): SystemOneQuestion {\n\treturn {\n\t\ttype: question.form,\n\t\t...(question.instructions === undefined ? {} : { instructions: question.instructions }),\n\t\t...(question.criteria === undefined ? {} : { criteria: question.criteria }),\n\t}\n}\n\n/**\n * Extracts a System One distribution in question criteria order and drops server measures.\n *\n * @remarks\n * Every requested candidate must have a finite probability in [0, 1]; the helper checks each\n * requested candidate itself and reads no other member. Score maps and arrays project onto the\n * requested levels. Additional candidates are ignored, sums are unconstrained, and probabilities\n * are preserved without normalization.\n *\n * @param answer - The wire answer to decode\n * @param question - The question defining the form and candidate order\n * @returns The domain answer, or undefined for a mismatched form or a requested candidate whose\n * probability is missing, not finite, or outside [0, 1]\n * @example\n * ```ts\n * extractSystemOneAnswer({ type: 'noul', noul: 0.9 }, { form: 'noul' })\n * // { form: 'noul', noul: 0.9 }\n * ```\n */\nexport function extractSystemOneAnswer(\n\tanswer: SystemOneAnswer,\n\tquestion: JudgeQuestion,\n): JudgeAnswer | undefined {\n\tconst bounded = boundsOf(0, 1)\n\tif (answer.type === 'noul' && question.form === 'noul') {\n\t\treturn bounded(answer.noul) ? { form: 'noul', noul: answer.noul } : undefined\n\t}\n\tif (answer.type === 'choice' && question.form === 'choice') {\n\t\tconst labels = Object.keys(question.criteria)\n\t\tconst entries = labels.flatMap((label) => {\n\t\t\tconst probability = Object.hasOwn(answer.probabilities, label)\n\t\t\t\t? answer.probabilities[label]\n\t\t\t\t: undefined\n\t\t\treturn bounded(probability) ? [[label, probability] as const] : []\n\t\t})\n\t\treturn entries.length === labels.length\n\t\t\t? { form: 'choice', probabilities: Object.fromEntries(entries) }\n\t\t\t: undefined\n\t}\n\tif (answer.type !== 'score' || question.form !== 'score') return undefined\n\t// `keys()` visits a hole in the criteria list too, so a hole is a requested level.\n\tconst levels = [...question.criteria.keys()]\n\tconst probabilities = levels.flatMap((level) => {\n\t\tconst probability = Object.hasOwn(answer.probabilities, level)\n\t\t\t? isArray(answer.probabilities)\n\t\t\t\t? answer.probabilities[level]\n\t\t\t\t: answer.probabilities[String(level)]\n\t\t\t: undefined\n\t\treturn bounded(probability) ? [probability] : []\n\t})\n\treturn probabilities.length === levels.length ? { form: 'score', probabilities } : undefined\n}\n\n/**\n * Maps complete System One token counts onto validated token usage.\n *\n * @param usage - The optional wire token counts\n * @returns Token usage, or undefined for missing, null, negative, or non-finite counts\n * @example\n * ```ts\n * extractSystemOneUsage({ input_tokens: 975, output_tokens: 4 })\n * // { prompt: 975, completion: 4, total: 979 }\n * ```\n */\nexport function extractSystemOneUsage(usage: SystemOneUsage | undefined): TokenUsage | undefined {\n\tconst extracted = attempt(() => {\n\t\tif (usage === undefined) return undefined\n\t\tconst { input_tokens: prompt, output_tokens: completion } = usage\n\t\tif (!isNumber(prompt) || !isNumber(completion)) return undefined\n\t\tconst result = { prompt, completion, total: prompt + completion }\n\t\treturn isTokenUsage(result) ? result : undefined\n\t})\n\treturn extracted.success ? extracted.value : undefined\n}\n","import type { ProviderDelta, ProviderResult, RelayFrame, RelayStreamOptions } from './types.js'\nimport type { ThinkingReplay } from '../types.js'\nimport { RELAY_CONTENT_TYPE, RELAY_PROVIDER_MESSAGE } from './constants.js'\nimport { ProviderError, isProviderAbortError } from './errors.js'\nimport { relayFrameContract } from './contracts.js'\nimport { stripThinking } from '../helpers.js'\n\n/**\n * Streams a provider call as validated NDJSON frames under response backpressure.\n *\n * @remarks\n * Cancellation aborts upstream before returning its iterator. Request listeners are\n * released on settlement. Unexpected provider failures carry a fixed public message.\n * An inbound abort leaves the response body neither closed nor errored — a server runtime\n * cancels that body when the client disconnects — so a consumer that aborts the inbound\n * signal itself must cancel the body rather than keep reading it.\n *\n * @example\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { RelayStream } from '@orkestrel/agent'\n *\n * export function respond(provider: ProviderInterface, signal: AbortSignal): Response {\n * \treturn new RelayStream({ provider, request: { messages: [] }, signal }).response\n * }\n * ```\n */\nexport class RelayStream {\n\treadonly #replay: ThinkingReplay\n\treadonly #upstream = new AbortController()\n\treadonly #encoder = new TextEncoder()\n\treadonly #iterator: AsyncGenerator<ProviderDelta, ProviderResult>\n\treadonly #signal: AbortSignal\n\treadonly #listener: () => void\n\treadonly #response: Response\n\t#settled = false\n\n\tconstructor(options: RelayStreamOptions) {\n\t\tthis.#replay = options.provider.replay ?? 'none'\n\t\tthis.#signal = options.signal\n\t\tthis.#listener = this.#abort.bind(this)\n\t\tif (this.#signal.aborted) this.#abortProvider()\n\t\tthis.#iterator = options.provider.stream(\n\t\t\tstripThinking(options.request.messages, this.#replay),\n\t\t\tthis.#upstream.signal,\n\t\t\toptions.request.tools,\n\t\t\toptions.request.options,\n\t\t)\n\t\tthis.#signal.addEventListener('abort', this.#listener, { once: true })\n\t\tthis.#response = new Response(\n\t\t\tnew ReadableStream<Uint8Array>({\n\t\t\t\tpull: this.#pull.bind(this),\n\t\t\t\tcancel: this.#abort.bind(this),\n\t\t\t}),\n\t\t\t{ headers: { 'content-type': RELAY_CONTENT_TYPE, 'cache-control': 'no-store' } },\n\t\t)\n\t}\n\n\t/** Exposes the pull-driven response for the provider call. */\n\tget response(): Response {\n\t\treturn this.#response\n\t}\n\n\tasync #pull(controller: ReadableStreamDefaultController<Uint8Array>): Promise<void> {\n\t\tif (this.#settled) return\n\t\ttry {\n\t\t\tconst step = await this.#iterator.next()\n\t\t\tif (this.#settled) return\n\t\t\tthis.#write(controller, step.done ? { channel: 'result', result: step.value } : step.value)\n\t\t\tif (step.done) this.#finish(controller)\n\t\t} catch (error) {\n\t\t\tif (this.#settled) return\n\t\t\tconst frame: RelayFrame = isProviderAbortError(error)\n\t\t\t\t? { channel: 'abort', partial: error.partial }\n\t\t\t\t: { channel: 'error', message: RELAY_PROVIDER_MESSAGE }\n\t\t\tthis.#write(\n\t\t\t\tcontroller,\n\t\t\t\trelayFrameContract.is(frame)\n\t\t\t\t\t? frame\n\t\t\t\t\t: { channel: 'error', message: RELAY_PROVIDER_MESSAGE },\n\t\t\t)\n\t\t\tthis.#finish(controller)\n\t\t\tthis.#abortProvider()\n\t\t\tawait this.#return()\n\t\t}\n\t}\n\n\tasync #abort(): Promise<void> {\n\t\tif (this.#settled) return\n\t\tthis.#settled = true\n\t\tthis.#abortProvider()\n\t\ttry {\n\t\t\tawait this.#return()\n\t\t} finally {\n\t\t\tthis.#release()\n\t\t}\n\t}\n\n\tasync #return(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.#iterator.return({ content: '' })\n\t\t} catch {\n\t\t\t// A settled response has no error channel for upstream cleanup failures.\n\t\t}\n\t}\n\n\t#write(controller: ReadableStreamDefaultController<Uint8Array>, frame: RelayFrame): void {\n\t\tif (!relayFrameContract.is(frame)) throw new ProviderError('PROTOCOL', 'invalid relay frame')\n\t\tcontroller.enqueue(this.#encoder.encode(`${JSON.stringify(frame)}\\n`))\n\t}\n\n\t#finish(controller: ReadableStreamDefaultController<Uint8Array>): void {\n\t\tthis.#settled = true\n\t\tthis.#release()\n\t\tcontroller.close()\n\t}\n\n\t#abortProvider(): void {\n\t\tthis.#upstream.abort(this.#signal.reason)\n\t}\n\n\t#release(): void {\n\t\tthis.#signal.removeEventListener('abort', this.#listener)\n\t}\n}\n","import type { ThinkSplitterInterface } from './types.js'\nimport { THINK_CLOSE, THINK_OPEN } from './constants.js'\n\n/**\n * Separates `<think>…</think>` spans from raw content deltas with per-stream state: text inside a\n * span accumulates on `thinking`, and text outside it returns as clean content, so a provider\n * yields the answer alone and surfaces the reasoning as\n * {@link import('./types.js').ProviderResult.thinking}. A tag split across deltas is held until\n * disambiguated, `flush()` settles the stream end, and one splitter serves one stream.\n *\n * @remarks\n * - **Cross-chunk tags.** A tag may arrive split across wire deltas (`'<thi'` then\n * `'nk>'`): any suffix of the pending text that is a strict prefix of a tag being\n * scanned for is held back (neither surfaced nor routed) until the next delta — or\n * `flush()` — disambiguates it. A held tag prefix that never completes is real\n * content; a held close-tag prefix inside a span is thinking.\n * - **The implicit leading open (the qwen3-template shape).** Some chat templates\n * pre-seed `<think>` into the prompt scaffold, so the wire stream begins\n * mid-reasoning and only a bare `</think>` appears. Before any tag event, a bare\n * close therefore reclassifies everything surfaced so far (plus the pre-close\n * pending) as thinking — `content` is corrected retroactively (the already-returned\n * prefix cannot be recalled, so `content` is the authoritative accumulation). The\n * rule is one-shot: after any tag event a bare `</think>` is plain text.\n * - **Multiple spans** accumulate onto `thinking` in stream order. A nested-looking\n * `<think>` inside an open span is thinking text (no nesting is tracked — the\n * first `</think>` closes the span), matching how the models emit it.\n * - **Unclosed span at stream end.** `flush()` routes the open span's tail (including\n * any held partial close tag) to `thinking` — a cut-off model was still reasoning.\n * - **One splitter, one stream.** State is per-stream; create a fresh instance per\n * provider call ({@link import('./factories.js').createThinkSplitter}).\n *\n * @example\n * ```ts\n * const splitter = new ThinkSplitter()\n * splitter.split('<thi') // '' (held — ambiguous)\n * splitter.split('nk>plan</think>ok') // 'ok'\n * splitter.thinking // 'plan'\n * splitter.content // 'ok'\n * splitter.flush() // '' (nothing held)\n * ```\n */\nexport class ThinkSplitter implements ThinkSplitterInterface {\n\t// The undecided tail of the stream — text not yet routed to content or thinking\n\t// (at most one partial tag's worth between calls; transiently the whole delta).\n\t#pending = ''\n\t// Whether the scanner is inside an open `<think>` span.\n\t#inside = false\n\t// Whether a tag event has occurred (an explicit open, or the one-shot implicit-open\n\t// close) — before it, a bare `</think>` closes the implicit span a chat template\n\t// pre-seeded; after it, a bare close is plain text.\n\t#opened = false\n\t#content = ''\n\t#thinking = ''\n\n\tget content(): string {\n\t\treturn this.#content\n\t}\n\n\tget thinking(): string {\n\t\treturn this.#thinking\n\t}\n\n\tsplit(delta: string): string {\n\t\tconst out = this.#scan(delta)\n\t\tthis.#content += out\n\t\treturn out\n\t}\n\n\tflush(): string {\n\t\tconst pending = this.#pending\n\t\tthis.#pending = ''\n\t\tif (this.#inside) {\n\t\t\t// An unclosed span at stream end — the tail (held partial close tag included)\n\t\t\t// is thinking: the model was cut off mid-reasoning, never mid-answer.\n\t\t\tthis.#thinking += pending\n\t\t\tthis.#inside = false\n\t\t\treturn ''\n\t\t}\n\t\t// A held partial tag that never completed was real content after all.\n\t\tthis.#content += pending\n\t\treturn pending\n\t}\n\n\t// Drive the state machine over one delta, returning the clean content it surfaces —\n\t// the wrapper folds the return into `#content`, which the implicit-open branch may\n\t// have reset a moment earlier (the reclassification).\n\t#scan(delta: string): string {\n\t\tthis.#pending += delta\n\t\treturn [...this.#surface()].join('')\n\t}\n\n\t// Walk the pending text, yielding each clean content piece in order and routing reasoning to\n\t// `#thinking`; return when the remainder is undecided and held for the next delta. A piece is\n\t// yielded only after `#opened` is set, so the reclassification below never meets one.\n\t*#surface(): Generator<string, void> {\n\t\tfor (;;) {\n\t\t\tif (this.#inside) {\n\t\t\t\tconst close = this.#pending.indexOf(THINK_CLOSE)\n\t\t\t\tif (close === -1) {\n\t\t\t\t\t// No close yet — route all but a possible partial close-tag suffix to\n\t\t\t\t\t// thinking, holding that suffix for the next delta to disambiguate.\n\t\t\t\t\tthis.#thinking += this.#hold([THINK_CLOSE])\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tthis.#thinking += this.#pending.slice(0, close)\n\t\t\t\tthis.#pending = this.#pending.slice(close + THINK_CLOSE.length)\n\t\t\t\tthis.#inside = false\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst open = this.#pending.indexOf(THINK_OPEN)\n\t\t\tif (!this.#opened) {\n\t\t\t\t// The implicit leading open: before any tag event, a bare close (arriving\n\t\t\t\t// ahead of any explicit open) means the stream began inside a pre-seeded\n\t\t\t\t// span — everything surfaced so far was reasoning. Reclassify it.\n\t\t\t\tconst close = this.#pending.indexOf(THINK_CLOSE)\n\t\t\t\tif (close !== -1 && (open === -1 || close < open)) {\n\t\t\t\t\tthis.#thinking += this.#content + this.#pending.slice(0, close)\n\t\t\t\t\tthis.#content = ''\n\t\t\t\t\tthis.#pending = this.#pending.slice(close + THINK_CLOSE.length)\n\t\t\t\t\tthis.#opened = true\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (open === -1) {\n\t\t\t\t// No open tag — surface all but a possible partial tag suffix as content\n\t\t\t\t// (before any tag event a split leading close must not leak either, so the\n\t\t\t\t// close tag's prefixes are held back too).\n\t\t\t\tconst tags = this.#opened ? [THINK_OPEN] : [THINK_OPEN, THINK_CLOSE]\n\t\t\t\tyield this.#hold(tags)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.#opened = true\n\t\t\tyield this.#pending.slice(0, open)\n\t\t\tthis.#pending = this.#pending.slice(open + THINK_OPEN.length)\n\t\t\tthis.#inside = true\n\t\t}\n\t}\n\n\t// Settle the pending text against the given tags' possible partial suffixes: the\n\t// longest ambiguous suffix stays pending for the next delta; everything before it\n\t// is returned for routing.\n\t#hold(tags: readonly string[]): string {\n\t\tconst keep = Math.max(...tags.map((tag) => this.#overlap(tag)))\n\t\tconst cut = this.#pending.length - keep\n\t\tconst settled = this.#pending.slice(0, cut)\n\t\tthis.#pending = this.#pending.slice(cut)\n\t\treturn settled\n\t}\n\n\t// The length of the longest strict prefix of `tag` that suffixes the pending text —\n\t// how many trailing characters are an ambiguous partial tag and must be held back.\n\t#overlap(tag: string): number {\n\t\tconst max = Math.min(this.#pending.length, tag.length - 1)\n\t\treturn (\n\t\t\tArray.from({ length: max }, (_, index) => max - index).find((length) =>\n\t\t\t\tthis.#pending.endsWith(tag.slice(0, length)),\n\t\t\t) ?? 0\n\t\t)\n\t}\n}\n","import type {\n\tAgentProviderInput,\n\tAgentProviderInterface,\n\tProviderDelta,\n\tProviderIncrement,\n\tProviderOptions,\n\tProviderParserInterface,\n\tProviderRequest,\n\tProviderResult,\n\tProviderStreamOptions,\n\tThinkSplitterInterface,\n} from './types.js'\nimport type { Message, ThinkingReplay } from '../types.js'\nimport type { ToolDefinition } from '@orkestrel/tool'\nimport { Timeout } from '@orkestrel/timeout'\nimport { DEFAULT_PROVIDER_TIMEOUT } from './constants.js'\nimport {\n\tisProviderAbortError,\n\tisProviderError,\n\tProviderAbortError,\n\tProviderError,\n} from './errors.js'\nimport { ThinkSplitter } from './ThinkSplitter.js'\nimport { buildProviderResult, readChunks, buildProviderHeaders, readFailure } from './helpers.js'\nimport { joinThinking } from '../helpers.js'\n\n/**\n * Implements bounded HTTP streaming and result assembly behind concrete wire seams.\n *\n * @remarks\n * Every call owns its parser, splitter, deadline, and accumulation. Success bodies\n * have no size limit. A qwen3 implicit-open reclassification corrects the final\n * content while content deltas already yielded cannot be recalled. A subclass fills\n * `name`, `frame`, `encode`, `read`, and `finish`; the constructor takes the `split`\n * and `strict` switches to control reasoning separation and whether a stream must end with a settled\n * `result` or a `done` record, which it must by default.\n * The `replay` input names which stored thinking the agent and relay send back. Default: `'none'`.\n * A throw from the framing seam (`parse`, or `finish`) becomes a `ProviderError` with the\n * `PROTOCOL` code whose `cause` is the throw, unless it is already a `ProviderError` or\n * `ProviderAbortError`, which reaches the caller unchanged. A throw from `read`, `frame`, or\n * `clear` reaches the caller unchanged, and a framing throw that races a cancel is the `cause` of\n * the `ProviderAbortError`.\n *\n * @example Writing a provider for a new wire\n * ```ts\n * import type {\n * \tProviderIncrement,\n * \tProviderOptions,\n * \tProviderParserInterface,\n * \tProviderRequest,\n * } from '@orkestrel/agent'\n * import { AgentProvider } from '@orkestrel/agent'\n *\n * class TextFrame implements ProviderParserInterface<string> {\n * \tparse(chunk: string): readonly string[] {\n * \t\treturn [chunk]\n * \t}\n * \tclear(): void {} // Raw text retains no framing state.\n * }\n *\n * interface TextOptions extends ProviderOptions {\n * \treadonly url: string\n * }\n *\n * class TextProvider extends AgentProvider<string> {\n * \treadonly name = 'text'\n * \tconstructor(options: TextOptions) {\n * \t\t// Raw text has no terminal record, so a stream that stops reads as complete.\n * \t\tsuper({ ...options, path: '/generate', strict: false })\n * \t}\n * \tframe(): ProviderParserInterface<string> {\n * \t\treturn new TextFrame()\n * \t}\n * \tencode(request: ProviderRequest): object {\n * \t\treturn { messages: request.messages }\n * \t}\n * \tread(record: string): ProviderIncrement {\n * \t\treturn { content: record, thinking: '', tools: [] }\n * \t}\n * \tfinish(_parser: ProviderParserInterface<string>): readonly string[] {\n * \t\treturn [] // Raw text retains no records at end of input.\n * \t}\n * }\n * ```\n */\nexport abstract class AgentProvider<\n\tTRecord = Readonly<Record<string, unknown>>,\n> implements AgentProviderInterface<TRecord> {\n\treadonly #id: string\n\treadonly #replay: ThinkingReplay\n\treadonly #url: string\n\treadonly #path: string\n\treadonly #timeout: number\n\treadonly #transport: typeof globalThis.fetch\n\treadonly #headers: ProviderOptions['headers']\n\treadonly #split: boolean\n\treadonly #strict: boolean\n\n\tconstructor(input: AgentProviderInput) {\n\t\tthis.#id = crypto.randomUUID()\n\t\tthis.#replay = input.replay ?? 'none'\n\t\tthis.#url = input.url\n\t\tthis.#path = input.path ?? ''\n\t\tthis.#timeout = input.timeout ?? DEFAULT_PROVIDER_TIMEOUT\n\t\tthis.#transport = input.fetch ?? globalThis.fetch.bind(globalThis)\n\t\tthis.#headers = input.headers\n\t\tthis.#split = input.split ?? true\n\t\tthis.#strict = input.strict ?? true\n\t}\n\n\t/** Identifies the concrete backend. */\n\tabstract readonly name: string\n\n\t/** Exposes the instance's minted UUID. */\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\t/** Names which stored assistant thinking the agent and relay send back. */\n\tget replay(): ThinkingReplay {\n\t\treturn this.#replay\n\t}\n\n\t/** Creates fresh framing state for the call. */\n\tabstract frame(): ProviderParserInterface<TRecord>\n\t/** Projects a domain request onto the concrete protocol's wire body. */\n\tabstract encode(request: ProviderRequest): object\n\t/** Decodes a framed record into a turn increment. */\n\tabstract read(record: TRecord): ProviderIncrement\n\t/** Returns any records retained at end of input. */\n\tabstract finish(parser: ProviderParserInterface<TRecord>): readonly TRecord[]\n\n\t/**\n\t * Generates a complete turn by draining the shared stream engine.\n\t *\n\t * @param messages - The conversation turns\n\t * @param signal - The caller's cancellation bound\n\t * @param tools - The advertised tool definitions\n\t * @param options - The per-call generation configuration\n\t * @returns The terminal stream result\n\t */\n\tasync generate(\n\t\tmessages: readonly Message[],\n\t\tsignal: AbortSignal,\n\t\ttools?: readonly ToolDefinition[],\n\t\toptions?: ProviderStreamOptions,\n\t): Promise<ProviderResult> {\n\t\tconst stream = this.stream(messages, signal, tools, options)\n\t\tfor (;;) {\n\t\t\tconst step = await stream.next()\n\t\t\tif (step.done) return step.value\n\t\t}\n\t}\n\n\t/**\n\t * Streams decoded deltas and returns the authoritative or assembled turn result.\n\t *\n\t * @param messages - The conversation turns\n\t * @param signal - The caller's cancellation bound\n\t * @param tools - The advertised tool definitions\n\t * @param options - The per-call generation configuration\n\t * @returns Content and native thinking deltas followed by the settled result\n\t * @throws ProviderAbortError Thrown when the combined cancellation bound fires\n\t * @throws ProviderError Thrown when the response is non-OK, the response has no body, a wire record\n\t * is malformed or carries an upstream failure, or a strict stream ends without a settled result\n\t */\n\tasync *stream(\n\t\tmessages: readonly Message[],\n\t\tsignal: AbortSignal,\n\t\ttools?: readonly ToolDefinition[],\n\t\toptions?: ProviderStreamOptions,\n\t): AsyncGenerator<ProviderDelta, ProviderResult> {\n\t\tif (signal.aborted) throw new ProviderAbortError({ content: '' })\n\t\tconst timeout = new Timeout({ ms: this.#timeout })\n\t\ttimeout.start()\n\t\tconst combined = AbortSignal.any([timeout.signal, signal])\n\t\ttry {\n\t\t\tconst parser = this.#frame(combined)\n\t\t\ttry {\n\t\t\t\treturn yield* this.#read(parser, messages, tools, options, combined)\n\t\t\t} finally {\n\t\t\t\tparser.clear()\n\t\t\t}\n\t\t} finally {\n\t\t\ttimeout.clear()\n\t\t}\n\t}\n\n\t// Creates the call's framing state; a failure that raced the cancel becomes the empty partial.\n\t#frame(combined: AbortSignal): ProviderParserInterface<TRecord> {\n\t\ttry {\n\t\t\treturn this.frame()\n\t\t} catch (error) {\n\t\t\tthrow this.#translate(error, combined, undefined, [])\n\t\t}\n\t}\n\n\t// Requests, frames, folds, and settles one call. Each decoded increment joins a local list\n\t// before its deltas are exposed, so a cancel during a yield assembles from what the body produced.\n\tasync *#read(\n\t\tparser: ProviderParserInterface<TRecord>,\n\t\tmessages: readonly Message[],\n\t\ttools: readonly ToolDefinition[] | undefined,\n\t\toptions: ProviderStreamOptions | undefined,\n\t\tcombined: AbortSignal,\n\t): AsyncGenerator<ProviderDelta, ProviderResult> {\n\t\tconst splitter = this.#split ? new ThinkSplitter() : undefined\n\t\tconst increments: ProviderIncrement[] = []\n\t\ttry {\n\t\t\tconst response = await this.#request(\n\t\t\t\t{\n\t\t\t\t\tmessages,\n\t\t\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t\t\t...(options === undefined ? {} : { options }),\n\t\t\t\t},\n\t\t\t\tcombined,\n\t\t\t)\n\t\t\tif (response.body === null) {\n\t\t\t\tthrow new ProviderError('PROTOCOL', 'provider error: no response body')\n\t\t\t}\n\t\t\tfor await (const chunk of readChunks(response.body, combined)) {\n\t\t\t\tcombined.throwIfAborted()\n\t\t\t\tfor (const record of this.#parse(parser, chunk, combined)) {\n\t\t\t\t\tconst increment = this.read(record)\n\t\t\t\t\tincrements.push(increment)\n\t\t\t\t\tif (increment.result === undefined) yield* this.#emit(increment, splitter, combined)\n\t\t\t\t\tcombined.throwIfAborted()\n\t\t\t\t\tif (increment.result !== undefined) return increment.result\n\t\t\t\t}\n\t\t\t}\n\t\t\tcombined.throwIfAborted()\n\t\t\tfor (const record of this.#finish(parser, combined)) {\n\t\t\t\tconst increment = this.read(record)\n\t\t\t\tincrements.push(increment)\n\t\t\t\tif (increment.result === undefined) yield* this.#emit(increment, splitter, combined)\n\t\t\t\tcombined.throwIfAborted()\n\t\t\t\tif (increment.result !== undefined) return increment.result\n\t\t\t}\n\t\t\tconst tail = splitter?.flush() ?? ''\n\t\t\tif (tail.length > 0) yield { channel: 'content', text: tail }\n\t\t\tcombined.throwIfAborted()\n\t\t\tif (this.#strict && increments.at(-1)?.done !== true) {\n\t\t\t\tthrow new ProviderError(\n\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t'provider error: the stream ended with no settled result and no terminal record; the response was cut, or a wire with no terminal record passes strict: false',\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn this.#assemble(splitter, increments)\n\t\t} catch (error) {\n\t\t\tthrow this.#translate(error, combined, splitter, increments)\n\t\t}\n\t}\n\n\t// A throw from parse becomes a PROTOCOL ProviderError unless cancelled or already a provider error.\n\t#parse(\n\t\tparser: ProviderParserInterface<TRecord>,\n\t\tchunk: string,\n\t\tcombined: AbortSignal,\n\t): readonly TRecord[] {\n\t\ttry {\n\t\t\treturn parser.parse(chunk)\n\t\t} catch (error) {\n\t\t\tif (combined.aborted || isProviderError(error) || isProviderAbortError(error)) throw error\n\t\t\tthrow new ProviderError('PROTOCOL', 'provider error: malformed wire record', { cause: error })\n\t\t}\n\t}\n\n\t// A throw from finish becomes a PROTOCOL ProviderError unless cancelled or already a provider error.\n\t#finish(parser: ProviderParserInterface<TRecord>, combined: AbortSignal): readonly TRecord[] {\n\t\ttry {\n\t\t\treturn this.finish(parser)\n\t\t} catch (error) {\n\t\t\tif (combined.aborted || isProviderError(error) || isProviderAbortError(error)) throw error\n\t\t\tthrow new ProviderError('PROTOCOL', 'provider error: malformed wire record', { cause: error })\n\t\t}\n\t}\n\n\t// Surfaces one increment's deltas: the split content, then — unless the call was cancelled in\n\t// between — its native thinking.\n\t*#emit(\n\t\tincrement: ProviderIncrement,\n\t\tsplitter: ThinkSplitterInterface | undefined,\n\t\tsignal: AbortSignal,\n\t): Generator<ProviderDelta> {\n\t\tconst content = splitter?.split(increment.content) ?? increment.content\n\t\tif (content.length > 0) yield { channel: 'content', text: content }\n\t\tsignal.throwIfAborted()\n\t\tif (increment.thinking.length > 0) yield { channel: 'thinking', text: increment.thinking }\n\t}\n\n\t// The error a failed call throws: a cancel becomes a ProviderAbortError carrying the partial\n\t// assembled so far (a throw that raced the cancel is its cause); any other failure is itself.\n\t#translate(\n\t\terror: unknown,\n\t\tcombined: AbortSignal,\n\t\tsplitter: ThinkSplitterInterface | undefined,\n\t\tincrements: readonly ProviderIncrement[],\n\t): unknown {\n\t\tif (!combined.aborted) return error\n\t\tsplitter?.flush()\n\t\treturn new ProviderAbortError(\n\t\t\tthis.#assemble(splitter, increments),\n\t\t\terror === combined.reason ? undefined : { cause: error },\n\t\t)\n\t}\n\n\t// Both exits assemble alike, so an aborted call's partial carries what a settled call would. A\n\t// settled-result increment replaces the fold; a `done` increment closes it.\n\t#assemble(\n\t\tsplitter: ThinkSplitterInterface | undefined,\n\t\tincrements: readonly ProviderIncrement[],\n\t): ProviderResult {\n\t\tconst last = increments.at(-1)\n\t\tconst folded = last?.result === undefined ? increments : [last]\n\t\treturn buildProviderResult(\n\t\t\tsplitter?.content ?? folded.map((one) => one.content).join(''),\n\t\t\tjoinThinking(splitter?.thinking, folded.map((one) => one.thinking).join('')),\n\t\t\tfolded.flatMap((one) => [...one.tools]),\n\t\t\tfolded.findLast((one) => one.usage !== undefined)?.usage,\n\t\t\tfolded.findLast((one) => one.truncated !== undefined)?.truncated,\n\t\t)\n\t}\n\n\t// Send one request and translate its HTTP failure into the shared provider taxonomy.\n\tasync #request(request: ProviderRequest, signal: AbortSignal): Promise<Response> {\n\t\tconst headers = await buildProviderHeaders(this.#headers, signal)\n\t\tsignal.throwIfAborted()\n\t\tconst response = await this.#transport(this.#url + this.#path, {\n\t\t\tmethod: 'POST',\n\t\t\theaders,\n\t\t\tbody: JSON.stringify(this.encode(request)),\n\t\t\tsignal,\n\t\t})\n\t\tif (!response.ok) {\n\t\t\tconst { message, ...options } = await readFailure(response, 'provider error:', signal)\n\t\t\tthrow new ProviderError('HTTP', message, options)\n\t\t}\n\t\treturn response\n\t}\n}\n","import type {\n\tProviderIncrement,\n\tProviderParserInterface,\n\tProviderRequest,\n\tRelayProviderOptions,\n} from './types.js'\nimport { attempt, cloneJSONValue } from '@orkestrel/contract'\nimport { createNDJSONParser } from '@orkestrel/ndjson'\nimport { AgentProvider } from './AgentProvider.js'\nimport { ProviderAbortError, ProviderError } from './errors.js'\nimport { requestToRelay } from './helpers.js'\nimport { providerRequestContract, relayFrameContract } from './contracts.js'\n\n/**\n * Carries provider calls over an authenticated NDJSON relay endpoint.\n *\n * @remarks\n * Tool execution context stays local; calls carry only `id`, `name`, and `arguments`.\n * The wire body is an owned snapshot of\n * the projection read through property descriptors, so a serializer reachable only through a\n * `get` trap or a prototype is never consulted; an own function-valued property such as a\n * `toJSON` method is a value outside JSON and is refused before fetching, with the clone's\n * failure as the refusal's `cause`. A remote abort reconstructs a `ProviderAbortError` instance\n * without aborting the\n * local signal; the `Agent` runtime treats that instance as an error unless its own bound\n * signal is aborted.\n * Content is preserved verbatim, including literal thinking tags.\n * A refusal reaches the browser as a `ProviderError` instance with the `HTTP` code and status.\n * Without a `parser` option each response frames through a fresh strict `@orkestrel/ndjson`\n * parser, so a malformed or non-object line rejects the call with a `ProviderError` carrying the\n * `PROTOCOL` code.\n * This is the browser end alone; {@link createRelay} mounts the server end.\n * The example of {@link createRelayProvider} is the browser half of that pair.\n *\n * @example\n * ```ts\n * import type { ProviderResult } from '@orkestrel/agent'\n * import { RelayProvider } from '@orkestrel/agent'\n *\n * export function ask(bearer: string, signal: AbortSignal): Promise<ProviderResult> {\n * \tconst browser = new RelayProvider({\n * \t\turl: 'https://relay.example/relay',\n * \t\theaders: () => ({ authorization: `Bearer ${bearer}` }),\n * \t})\n * \treturn browser.generate([{ id: 'ask', role: 'user', content: 'ping' }], signal)\n * }\n * ```\n */\nexport class RelayProvider extends AgentProvider {\n\treadonly #parser: (() => ProviderParserInterface) | undefined\n\n\tconstructor(options: RelayProviderOptions) {\n\t\tconst { url, timeout, fetch, headers, replay } = options\n\t\tsuper({\n\t\t\turl,\n\t\t\tsplit: false,\n\t\t\tstrict: true,\n\t\t\t...(timeout === undefined ? {} : { timeout }),\n\t\t\t...(fetch === undefined ? {} : { fetch }),\n\t\t\t...(headers === undefined ? {} : { headers }),\n\t\t\t...(replay === undefined ? {} : { replay }),\n\t\t})\n\t\tthis.#parser = options.parser\n\t}\n\n\t/** Identifies the relay backend. */\n\treadonly name = 'relay'\n\n\t/** Creates fresh framing state for each response. */\n\tframe(): ProviderParserInterface {\n\t\treturn this.#parser === undefined ? createNDJSONParser({ strict: true }) : this.#parser()\n\t}\n\n\t/**\n\t * Projects declared request fields and refuses values the JSON wire cannot carry.\n\t *\n\t * @remarks\n\t * The snapshot is taken from property descriptors, so a custom serializer a projected\n\t * value carries is ignored rather than consulted and cannot reach the wire.\n\t *\n\t * @param request - The domain conversation and call configuration\n\t * @returns The validated wire request\n\t * @throws {ProviderError} Thrown when the snapshot cannot be taken — a throwing read or a\n\t * value outside JSON — carrying that failure as its `cause`, and when the snapshot the\n\t * projection produced is not a valid wire request\n\t */\n\tencode(request: ProviderRequest): object {\n\t\tconst snapshot = attempt(() => cloneJSONValue(requestToRelay(request)))\n\t\tif (!snapshot.success) {\n\t\t\tthrow new ProviderError('PROTOCOL', 'relay request is not JSON', { cause: snapshot.error })\n\t\t}\n\t\tif (!providerRequestContract.is(snapshot.value)) {\n\t\t\tthrow new ProviderError('PROTOCOL', 'relay request is not JSON')\n\t\t}\n\t\treturn snapshot.value\n\t}\n\n\t/**\n\t * Validates a relay frame and translates its channel into the shared stream engine.\n\t *\n\t * @param record - The framed wire record\n\t * @returns A delta contribution or authoritative result\n\t * @throws {ProviderAbortError} Thrown when the frame's channel is `abort`, carrying its partial result\n\t * @throws {ProviderError} Thrown when the frame fails validation or its channel is `error`\n\t */\n\tread(record: Readonly<Record<string, unknown>>): ProviderIncrement {\n\t\tif (!relayFrameContract.is(record)) {\n\t\t\tthrow new ProviderError('PROTOCOL', 'invalid relay frame')\n\t\t}\n\t\tswitch (record.channel) {\n\t\t\tcase 'content':\n\t\t\t\treturn { content: record.text, thinking: '', tools: [] }\n\t\t\tcase 'thinking':\n\t\t\t\treturn { content: '', thinking: record.text, tools: [] }\n\t\t\tcase 'result':\n\t\t\t\treturn { content: '', thinking: '', tools: [], result: record.result }\n\t\t\tcase 'abort':\n\t\t\t\tthrow new ProviderAbortError(record.partial)\n\t\t\tcase 'error':\n\t\t\t\tthrow new ProviderError('PROVIDER', record.message)\n\t\t}\n\t}\n\n\t/**\n\t * Recovers an unterminated final frame by completing its NDJSON line.\n\t *\n\t * @param parser - The call's retained framing state\n\t * @returns Records completed by the final newline\n\t */\n\tfinish(parser: ProviderParserInterface): ReadonlyArray<Readonly<Record<string, unknown>>> {\n\t\treturn parser.parse('\\n')\n\t}\n}\n","import type { AgentJudgeInput, AgentJudgeInterface, ProviderOptions } from './types.js'\nimport type { JudgeRequest, JudgeResult } from '../types.js'\nimport { describeFaults, isRecord, parseJSON } from '@orkestrel/contract'\nimport { Timeout } from '@orkestrel/timeout'\nimport { DEFAULT_PROVIDER_TIMEOUT } from './constants.js'\nimport { JudgeError } from './errors.js'\nimport { copyJSON } from '../cloners.js'\nimport { judgeAnswerContract, judgeQuestionContract } from '../contracts.js'\nimport { JudgeAbortError } from '../errors.js'\nimport { describeQuestionRefusal, estimateTokens, scanJudgmentTies } from '../helpers.js'\nimport { buildJudgeResult, buildProviderHeaders, readFailure, readBodyText } from './helpers.js'\nimport { isJudgeEntry, isJudgeQuestion } from '../validators.js'\n\n/**\n * Implements the bounded HTTP calls, validation, and result merging of a judge behind concrete\n * wire seams.\n *\n * @remarks\n * A subclass fills `name`, `encode`, and `read`; the constructor takes the `batch` switch that\n * decides whether one call carries every question or each question gets its own call in key\n * order. `ask` validates the request before any call, builds every body before the first call so\n * a wire limit refuses the request before inference, and runs the calls one after another, each\n * under its own deadline folded with the caller's signal. A cancel throws a `JudgeAbortError`\n * whose `partial` merges the calls that completed. A transport error reaches the caller unchanged. An\n * answer that does not fit its question throws code `PROTOCOL`, and a call whose `read` reports no\n * usage bills the estimate of its response text.\n *\n * @example Writing a judge wire\n * ```ts\n * import type { JudgeRequest, JudgeResult } from '@orkestrel/agent'\n * import { AgentJudge, JudgeError } from '@orkestrel/agent'\n * import { isFiniteNumber, isRecord } from '@orkestrel/contract'\n *\n * // A wire whose server answers one yes/no question per call as { \"yes\": 0.93 }.\n * class YesJudge extends AgentJudge {\n * \treadonly name = 'yes'\n * \tencode(request: JudgeRequest): object {\n * \t\treturn { model: this.model, state: request.state, questions: request.questions }\n * \t}\n * \tread(value: unknown, request: JudgeRequest): JudgeResult {\n * \t\tconst [id] = Object.keys(request.questions)\n * \t\tif (id === undefined || !isRecord(value) || !isFiniteNumber(value.yes)) {\n * \t\t\tthrow new JudgeError('PROTOCOL', 'judge error: unreadable answer')\n * \t\t}\n * \t\treturn { model: this.model, answers: { [id]: { form: 'noul', noul: value.yes } } }\n * \t}\n * }\n *\n * const judge = new YesJudge({\n * \turl: 'http://localhost:8010',\n * \tpath: '/v1/yes',\n * \tmodel: 'yes-1',\n * \tbatch: false,\n * })\n * ```\n */\nexport abstract class AgentJudge implements AgentJudgeInterface {\n\treadonly #id: string\n\treadonly #url: string\n\treadonly #path: string\n\treadonly #model: string\n\treadonly #timeout: number\n\treadonly #transport: typeof globalThis.fetch\n\treadonly #headers: ProviderOptions['headers']\n\treadonly #batch: boolean\n\n\tconstructor(input: AgentJudgeInput) {\n\t\tthis.#id = crypto.randomUUID()\n\t\tthis.#url = input.url\n\t\tthis.#path = input.path ?? ''\n\t\tthis.#model = input.model\n\t\tthis.#timeout = input.timeout ?? DEFAULT_PROVIDER_TIMEOUT\n\t\tthis.#transport = input.fetch ?? globalThis.fetch.bind(globalThis)\n\t\tthis.#headers = input.headers\n\t\tthis.#batch = input.batch ?? true\n\t}\n\n\t/** Identifies the concrete wire. */\n\tabstract readonly name: string\n\n\t/** Exposes the instance's minted UUID. */\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\t/** Exposes the configured model identity. */\n\tget model(): string {\n\t\treturn this.#model\n\t}\n\n\t/** Projects one call's request onto the concrete protocol's wire body. */\n\tabstract encode(request: JudgeRequest): object\n\t/** Decodes one call's parsed response body into the answers for that call's questions. */\n\tabstract read(value: unknown, request: JudgeRequest): JudgeResult\n\n\t/**\n\t * Asks every question of the request about its state and merges the answers of every call.\n\t *\n\t * @param request - The state and the questions keyed by caller id\n\t * @param signal - The caller's cancellation bound\n\t * @returns The merged answers, refusals, and usage of every call\n\t * @throws JudgeAbortError Thrown when the caller's signal or a call's deadline fires, carrying\n\t * the merged result of the completed calls\n\t * @throws JudgeError Thrown when the question map is empty, a question or the state is malformed,\n\t * or the wire refuses the request before any call (code `QUESTION`); when a response is non-OK\n\t * (code `HTTP`); and when a response body is missing or unparsable (code `PROTOCOL`)\n\t */\n\tasync ask(request: JudgeRequest, signal: AbortSignal): Promise<JudgeResult> {\n\t\tif (signal.aborted) throw new JudgeAbortError(buildJudgeResult(this.#model, []))\n\t\t// The request is owned through JSON before validation, so a caller mutating it while a call\n\t\t// is pending cannot make `read` decode against criteria that were never sent, and a proxied\n\t\t// request (a reactive view) is owned where a structured clone would refuse it.\n\t\tconst state = copyJSON(request.state)\n\t\tif (!isJudgeEntry(state)) {\n\t\t\tthrow new JudgeError('QUESTION', 'judge error: state is not a judge entry')\n\t\t}\n\t\tconst questions = isRecord(request.questions) ? Object.entries(request.questions) : []\n\t\tif (questions.length === 0) throw new JudgeError('QUESTION', 'judge error: no questions')\n\t\tconst entries = questions.map(([id, question]) => {\n\t\t\tconst copy = copyJSON(question)\n\t\t\tif (!isJudgeQuestion(copy)) {\n\t\t\t\tconst faults = judgeQuestionContract.audit(copy)\n\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t'QUESTION',\n\t\t\t\t\t`judge error: ${describeQuestionRefusal(id, faults, judgeQuestionContract.schema)}`,\n\t\t\t\t\t{ context: { question: id, faults } },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn [id, copy] as const\n\t\t})\n\t\tconst owned: JudgeRequest = { state, questions: Object.fromEntries(entries) }\n\t\tconst parts: readonly JudgeRequest[] = this.#batch\n\t\t\t? [owned]\n\t\t\t: entries.map(([id, question]) => ({ state, questions: { [id]: question } }))\n\t\tconst calls = parts.map((part) => ({ request: part, body: JSON.stringify(this.encode(part)) }))\n\t\tconst results: JudgeResult[] = []\n\t\tfor (const call of calls) {\n\t\t\tresults.push(await this.#call(call.request, call.body, signal, results))\n\t\t}\n\t\treturn buildJudgeResult(this.#model, results)\n\t}\n\n\tasync #call(\n\t\trequest: JudgeRequest,\n\t\tbody: string,\n\t\tsignal: AbortSignal,\n\t\tcompleted: readonly JudgeResult[],\n\t): Promise<JudgeResult> {\n\t\tconst timeout = new Timeout({ ms: this.#timeout })\n\t\ttimeout.start()\n\t\tconst combined = AbortSignal.any([timeout.signal, signal])\n\t\ttry {\n\t\t\tconst headers = await buildProviderHeaders(this.#headers, combined)\n\t\t\tcombined.throwIfAborted()\n\t\t\tconst response = await this.#transport(this.#url + this.#path, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\theaders,\n\t\t\t\tbody,\n\t\t\t\tsignal: combined,\n\t\t\t})\n\t\t\tif (!response.ok) {\n\t\t\t\tconst { message, ...options } = await readFailure(response, 'judge error:', combined)\n\t\t\t\tthrow new JudgeError('HTTP', message, options)\n\t\t\t}\n\t\t\tif (response.body === null) {\n\t\t\t\tthrow new JudgeError('PROTOCOL', 'judge error: no response body')\n\t\t\t}\n\t\t\tconst text = await readBodyText(response.body, undefined, combined)\n\t\t\tcombined.throwIfAborted()\n\t\t\tconst value = parseJSON(text.text)\n\t\t\tif (value === undefined) throw new JudgeError('PROTOCOL', 'judge error: invalid JSON body')\n\t\t\tconst result = this.read(value, request)\n\t\t\tcombined.throwIfAborted()\n\t\t\tfor (const [id, answer] of Object.entries(result.answers)) {\n\t\t\t\tconst question = Object.hasOwn(request.questions, id) ? request.questions[id] : undefined\n\t\t\t\tconst faults = question === undefined ? [] : scanJudgmentTies(question, answer)\n\t\t\t\tif (faults.length > 0) {\n\t\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t\t`judge error: the answer to question ${id} does not fit it\\n${describeFaults(faults, judgeAnswerContract.schema)}`,\n\t\t\t\t\t\t{ context: { question: id, faults } },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (result.usage !== undefined) return result\n\t\t\t// A call that reports no usage still spent tokens, so it bills its estimate as a provider call does.\n\t\t\tconst estimate = estimateTokens(text.text)\n\t\t\treturn { ...result, usage: { prompt: 0, completion: estimate, total: estimate } }\n\t\t} catch (error) {\n\t\t\tif (combined.aborted) {\n\t\t\t\t// A throw that raced the cancel is the call's real failure, so it is recorded as the cause.\n\t\t\t\tthrow new JudgeAbortError(\n\t\t\t\t\tbuildJudgeResult(this.#model, completed),\n\t\t\t\t\terror === combined.reason ? undefined : { cause: error },\n\t\t\t\t)\n\t\t\t}\n\t\t\tthrow error\n\t\t} finally {\n\t\t\ttimeout.clear()\n\t\t}\n\t}\n}\n","import type { SystemOneJudgeOptions, SystemOneRequest } from './types.js'\nimport type { JudgeRequest, JudgeResult } from '../types.js'\nimport { preview } from '@orkestrel/contract'\nimport { AgentJudge } from './AgentJudge.js'\nimport { SYSTEM_ONE_PATH } from './constants.js'\nimport { extractSystemOneAnswer, extractSystemOneUsage, questionToSystemOne } from './helpers.js'\nimport { isSystemOneAnswer, isSystemOneResponse } from './validators.js'\nimport { JudgeError } from './errors.js'\n\n/**\n * Carries judge questions over the System One protocol and derives answers from server distributions.\n *\n * @remarks\n * The caller supplies the server origin and model. Every question travels in one request.\n * Server measures are ignored; the response model is preserved.\n * `headers` supplies authentication through the shared judge engine.\n * The example readings are those of the response Ollama 0.40.0 returned for that request on 2026-10-07.\n *\n * @example Asking a System One server a choice, a noul, and a score\n * ```ts\n * import { measureAnswer, createSystemOneJudge } from '@orkestrel/agent'\n *\n * const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })\n * const result = await judge.ask(\n * \t{\n * \t\tstate: 'Our checkout has returned 500 errors since 9am. I want a refund for today.',\n * \t\tquestions: {\n * \t\t\tlabel: {\n * \t\t\t\tform: 'choice',\n * \t\t\t\tinstructions: 'Which label fits this ticket?',\n * \t\t\t\tcriteria: { billing: 'Payments and refunds', bug: 'Software errors', account: null },\n * \t\t\t},\n * \t\t\trefund: {\n * \t\t\t\tform: 'noul',\n * \t\t\t\tinstructions: 'Does the customer ask for money back?',\n * \t\t\t\tcriteria: {\n * \t\t\t\t\ttrue: 'The customer asks for a refund or for money back.',\n * \t\t\t\t\tfalse: 'The customer does not ask for money back.',\n * \t\t\t\t},\n * \t\t\t},\n * \t\t\tseverity: {\n * \t\t\t\tform: 'score',\n * \t\t\t\tinstructions: 'How severe is the reported issue?',\n * \t\t\t\tcriteria: ['Cosmetic; no impact', 'Degraded, workaround exists', 'Blocking; no workaround'],\n * \t\t\t},\n * \t\t},\n * \t},\n * \tAbortSignal.timeout(30_000),\n * )\n * const measures = Object.fromEntries(\n * \tObject.entries(result.answers).map(([id, answer]) => [id, measureAnswer(answer)]),\n * )\n * result.model // 'tev1:0.8b' — the model the server named\n * result.usage // { prompt: 975, completion: 4, total: 979 }\n * measures.label // { winner: 'bug', probability: 0.9691, confidence: 0.9536 } to 4 decimals\n * measures.refund // { winner: 'true', probability: 0.9979, confidence: 0.9958 } to 4 decimals\n * measures.severity // { winner: '1', probability: 0.9494, confidence: 0.9241, score: 0.9919 } to 4 decimals\n * ```\n */\nexport class SystemOneJudge extends AgentJudge {\n\tconstructor(options: SystemOneJudgeOptions) {\n\t\tsuper({ ...options, path: SYSTEM_ONE_PATH, batch: true })\n\t}\n\n\t/** Identifies the System One protocol. */\n\treadonly name = 'systemone'\n\n\t/**\n\t * Projects the state and questions onto the System One request with the configured model.\n\t *\n\t * @param request - The state and questions keyed by caller id\n\t * @returns The System One request body\n\t */\n\tencode(request: JudgeRequest): SystemOneRequest {\n\t\treturn {\n\t\t\tstate: request.state,\n\t\t\tmodel: this.model,\n\t\t\tquestions: Object.fromEntries(\n\t\t\t\tObject.entries(request.questions).map(([id, question]) => [\n\t\t\t\t\tid,\n\t\t\t\t\tquestionToSystemOne(question),\n\t\t\t\t]),\n\t\t\t),\n\t\t}\n\t}\n\n\t/**\n\t * Decodes requested System One answers and reports the server model and available usage.\n\t *\n\t * @param value - The parsed response body\n\t * @param request - The questions defining the expected answers\n\t * @returns The decoded distributions, model, and available usage\n\t * @throws JudgeError Thrown when the envelope is malformed or an answer is invalid (code `PROTOCOL`);\n\t * the message names the question id of an invalid answer\n\t */\n\tread(value: unknown, request: JudgeRequest): JudgeResult {\n\t\tif (!isSystemOneResponse(value)) {\n\t\t\tthrow new JudgeError(\n\t\t\t\t'PROTOCOL',\n\t\t\t\t`judge error: invalid System One response envelope; received ${preview(value)}`,\n\t\t\t)\n\t\t}\n\t\tconst entries = Object.entries(request.questions).map(([id, question]) => {\n\t\t\tif (!Object.hasOwn(value.answers, id)) {\n\t\t\t\tthrow new JudgeError('PROTOCOL', `judge error: question ${id} has no System One answer`)\n\t\t\t}\n\t\t\tconst wire = value.answers[id]\n\t\t\tif (!isSystemOneAnswer(wire)) {\n\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t`judge error: question ${id} has an invalid System One answer; received ${preview(wire)}`,\n\t\t\t\t)\n\t\t\t}\n\t\t\tconst answer = extractSystemOneAnswer(wire, question)\n\t\t\tif (answer === undefined) {\n\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t`judge error: question ${id} has a mismatched or incomplete System One answer`,\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn [id, answer] as const\n\t\t})\n\t\tconst usage = extractSystemOneUsage(value.usage)\n\t\treturn {\n\t\t\tmodel: value.model ?? this.model,\n\t\t\tanswers: Object.fromEntries(entries),\n\t\t\t...(usage === undefined ? {} : { usage }),\n\t\t}\n\t}\n}\n","import type {\n\tProviderInterface,\n\tRelayHandler,\n\tRelayOptions,\n\tRelayProviderOptions,\n\tRelaySystemOptions,\n\tRelayToolOptions,\n\tSystemOneJudgeOptions,\n\tThinkSplitterInterface,\n} from './types.js'\nimport type { JudgeInterface } from '../types.js'\nimport { attempt, cloneJSONValue, parseJSONAs, preview } from '@orkestrel/contract'\nimport {\n\tDEFAULT_RELAY_LIMIT,\n\tINVALID_RELAY_STATUS,\n\tOVERSIZED_RELAY_STATUS,\n\tREFUSED_RELAY_CONTENT_TYPE,\n\tREFUSED_RELAY_STATUS,\n\tUNAUTHORIZED_RELAY_STATUS,\n\tUPSTREAM_RELAY_STATUS,\n} from './constants.js'\nimport { buildRelayRequest, describeRelayRefusal, readBodyText, requestToRelay } from './helpers.js'\nimport { ProviderError } from './errors.js'\nimport { providerRequestContract } from './contracts.js'\nimport { RelayStream } from './RelayStream.js'\nimport { RelayProvider } from './RelayProvider.js'\nimport { ThinkSplitter } from './ThinkSplitter.js'\nimport { SystemOneJudge } from './SystemOneJudge.js'\n\n/**\n * Creates an authorized relay handler that validates a bounded JSON request before streaming.\n *\n * @remarks\n * Answers with the `401` status when authorization refuses or throws, the `400`\n * status when the body is missing, unreadable, or invalid, the `413` status when\n * the body fills its byte budget or the inbound read is aborted, the `422` status when\n * the request carries a page `system` message or tool definition that `options.system` or\n * `options.tools` does not admit, and the `502` status when the upstream provider call cannot\n * be constructed. A `422` refusal carries one `text/plain` line per refused member, as\n * {@link describeRelayRefusal} writes it, and the page reads at most `MAX_ERROR_BODY_LENGTH`\n * bytes of that body into its error message; every other refusal carries no body. An admitted\n * request is forwarded as {@link buildRelayRequest} builds it, the server's text first, and\n * `limit` counts only the inbound bytes. The handler copies `options.system` and the tool\n * definitions at construction, so a later change to either has no effect.\n *\n * @param options - The upstream provider, authorization decision, optional byte budget, and\n * system turn and tool definition policy\n * @returns A fetch-standard handler suitable for a router\n * @throws {ProviderError} Thrown with the `PROTOCOL` code when `options.tools.definitions` is not\n * JSON or repeats a name\n * @example Mounting the relay on your server\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createRelay } from '@orkestrel/agent'\n * import { createDispatcher } from '@orkestrel/router'\n * import { createServer } from '@orkestrel/server'\n *\n * declare const upstream: ProviderInterface // the server-side provider holding the credential\n * declare const bearer: string\n *\n * const handler = createRelay({\n * \tprovider: upstream,\n * \tauthorize: (request) => request.headers.get('authorization') === `Bearer ${bearer}`,\n * })\n * const dispatcher = createDispatcher({\n * \troutes: [{ method: 'POST', path: '/relay', handler }],\n * })\n *\n * export function serve(request: Request): Promise<Response> {\n * \treturn dispatcher.handle(request, undefined)\n * }\n *\n * const server = createServer({ dispatcher, state: () => undefined })\n * await server.start()\n * process.on('SIGTERM', () => server.stop()) // signal cancellation, drain, then close the listener\n * ```\n */\nexport function createRelay(options: RelayOptions): RelayHandler {\n\tconst { provider, authorize, limit } = options\n\tconst system: RelaySystemOptions = { ...options.system }\n\tconst { definitions, accept } = options.tools ?? {}\n\tconst snapshot = attempt(() =>\n\t\tcloneJSONValue(requestToRelay({ messages: [], tools: definitions ?? [] })),\n\t)\n\tif (!snapshot.success) {\n\t\tthrow new ProviderError('PROTOCOL', 'relay tool definitions are not JSON', {\n\t\t\tcause: snapshot.error,\n\t\t})\n\t}\n\tif (!providerRequestContract.is(snapshot.value)) {\n\t\tthrow new ProviderError('PROTOCOL', 'relay tool definitions are not JSON')\n\t}\n\tconst owned = snapshot.value.tools ?? []\n\tconst names = new Set<string>()\n\tfor (const tool of owned) {\n\t\tif (names.has(tool.name)) {\n\t\t\tthrow new ProviderError(\n\t\t\t\t'PROTOCOL',\n\t\t\t\t`relay tool definitions repeat the name ${preview(tool.name)}`,\n\t\t\t)\n\t\t}\n\t\tnames.add(tool.name)\n\t}\n\tconst tools: RelayToolOptions = {\n\t\t...(definitions === undefined ? {} : { definitions: owned }),\n\t\t...(accept === undefined ? {} : { accept }),\n\t}\n\treturn async (request) => {\n\t\ttry {\n\t\t\tif ((await authorize(request)) !== true) {\n\t\t\t\treturn new Response(undefined, { status: UNAUTHORIZED_RELAY_STATUS })\n\t\t\t}\n\t\t} catch {\n\t\t\treturn new Response(undefined, { status: UNAUTHORIZED_RELAY_STATUS })\n\t\t}\n\t\tif (request.body === null) return new Response(undefined, { status: INVALID_RELAY_STATUS })\n\t\tconst read = await readBodyText(\n\t\t\trequest.body,\n\t\t\tlimit ?? DEFAULT_RELAY_LIMIT,\n\t\t\trequest.signal,\n\t\t).catch(() => undefined)\n\t\tif (read === undefined) {\n\t\t\treturn new Response(undefined, {\n\t\t\t\tstatus: request.signal.aborted ? OVERSIZED_RELAY_STATUS : INVALID_RELAY_STATUS,\n\t\t\t})\n\t\t}\n\t\tif (!read.complete) return new Response(undefined, { status: OVERSIZED_RELAY_STATUS })\n\t\tconst parsed = parseJSONAs(read.text, providerRequestContract.is)\n\t\tif (parsed === undefined) return new Response(undefined, { status: INVALID_RELAY_STATUS })\n\t\tconst built = buildRelayRequest(parsed, { system, tools })\n\t\tif (!built.success) {\n\t\t\treturn new Response(built.error.map(describeRelayRefusal).join('\\n'), {\n\t\t\t\tstatus: REFUSED_RELAY_STATUS,\n\t\t\t\theaders: { 'content-type': REFUSED_RELAY_CONTENT_TYPE, 'cache-control': 'no-store' },\n\t\t\t})\n\t\t}\n\t\ttry {\n\t\t\treturn new RelayStream({ provider, request: built.value, signal: request.signal }).response\n\t\t} catch {\n\t\t\treturn new Response(undefined, { status: UPSTREAM_RELAY_STATUS })\n\t\t}\n\t}\n}\n\n/**\n * Creates a provider that carries calls through a relay endpoint.\n *\n * @remarks\n * This is the browser end alone. {@link createRelay} mounts the server end, and its example\n * is the server half this one pairs with.\n *\n * @param options - The endpoint, optional parser factory, and HTTP call configuration\n * @returns The relay provider behind the shared provider interface; construct\n * {@link RelayProvider} directly to reach its wire seams\n * @example Reaching the relay from the browser\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createRelayProvider } from '@orkestrel/agent'\n * import { createAbort } from '@orkestrel/abort'\n *\n * declare const bearer: string\n * const abort = createAbort()\n * const messages = [{ id: '1', role: 'user', content: 'Say hello.' }] as const\n *\n * const browser: ProviderInterface = createRelayProvider({\n * \turl: 'https://app.example/relay',\n * \theaders: () => ({ authorization: `Bearer ${bearer}` }),\n * })\n * const result = await browser.generate(messages, abort.signal) // a ProviderResult like a local provider's\n * ```\n */\nexport function createRelayProvider(options: RelayProviderOptions): ProviderInterface {\n\treturn new RelayProvider(options)\n}\n\n/**\n * Creates a fresh stream-stateful `<think>` separator — a {@link ThinkSplitterInterface} that\n * splits a thinking model's in-content `<think>…</think>` reasoning spans away from the answer,\n * delta by delta, so a provider yields clean content alone and surfaces the accumulated\n * reasoning as {@link import('./types.js').ProviderResult.thinking}. One splitter serves one\n * stream.\n *\n * @remarks\n * Feed each raw wire delta through `split(delta)` (it returns the clean content to\n * surface — possibly `''` mid-think) and settle the stream end with `flush()` (a held\n * partial open tag that never completed returns as final content; an unclosed think\n * span lands on `thinking`). Tags split across deltas are held back until\n * disambiguated, multiple spans accumulate in order, and a nested-looking `<think>`\n * inside an open span is thinking text. One splitter serves one stream — create\n * a fresh one per provider call.\n *\n * @returns A fresh {@link ThinkSplitterInterface} (state empty, outside any span)\n *\n * @example\n * ```ts\n * import { createThinkSplitter } from '@orkestrel/agent'\n *\n * const splitter = createThinkSplitter()\n * const clean = splitter.split('<think>plan the answer</think>Here it is.')\n * clean // 'Here it is.'\n * splitter.thinking // 'plan the answer'\n * ```\n */\nexport function createThinkSplitter(): ThinkSplitterInterface {\n\treturn new ThinkSplitter()\n}\n\n/**\n * Creates a judge that sends every question through the configured System One server.\n *\n * @param options - The server origin, model, and optional transport, headers, and timeout\n * @returns The configured judge behind its shared interface\n * @example\n * ```ts\n * const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })\n * ```\n */\nexport function createSystemOneJudge(options: SystemOneJudgeOptions): JudgeInterface {\n\treturn new SystemOneJudge(options)\n}\n","/**\n * Sets the default number of recent live messages the `compact()` method of a\n * {@link ConversationInterface} retains verbatim — `0`, so a manual `compact()` keeps no recent tail and folds every\n * exchange before the newest user message into one summarized section. A caller retains a recent\n * tail by passing `keep` (on\n * {@link ConversationOptions}, {@link ConversationManagerOptions}, or per-fold through\n * {@link CompactOptions}), folding at most the older `count - keep` messages, cut back to whole\n * exchanges, and leaving at least the most recent `keep` live for the next turn. Overridable everywhere `keep` is accepted.\n */\nexport const DEFAULT_CONVERSATION_KEEP = 0\n\n/**\n * Names the framing label the `view()` method of a {@link ConversationInterface} prefixes onto the\n * summary of each compacted section so a small model reads it as a condensed recap of earlier turns — the lean\n * `'[Summary of earlier messages] '` marker, never a literal assistant turn to echo or treat as\n * the live answer.\n *\n * @remarks\n * Deliberately a fixed, lean handful of tokens (a short bracketed marker) so the framing adds a\n * bounded `prefix × sections` overhead and never an open-ended blow-up — the\n * {@link ConversationInterface} no-bloat test guard pins exactly that. Kept here (beside\n * {@link DEFAULT_CONVERSATION_KEEP}) as the one tunable framing constant of the conversation\n * layer, so the wording has a single source of truth (the `view()` recap framing is distinct\n * from the cross-conversation provenance marker of `reference()`, which is rendered inline\n * because it interpolates the per-call provenance `label`).\n */\nexport const CONVERSATION_RECAP_PREFIX = '[Summary of earlier messages] '\n","import type { ConversationErrorCode } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport { INTRINSICS, holds, isObject, isFunction } from '@orkestrel/contract'\n\n/**\n * Reports a conversation with no {@link ConversationSummaryHandler} to fold its messages with, a\n * `sections` cap that is not a positive safe integer, a `keep` that is not a nonnegative safe integer, a summarizer digest\n * whose trimmed text is empty, a judgment or judge request that JSON cannot carry, a stored or\n * selected message whose role is not a conversation role, or a malformed snapshot — thrown by the\n * `compact()` and `add` methods of a {@link ConversationInterface}, its construction, or its\n * judgment store, or returned as the `fault` of an agent selection, carrying the machine-readable\n * `code` `'SUMMARIZER' | 'SECTIONS' | 'JUDGMENT' | 'KEEP' | 'SUMMARY' | 'ROLE' | 'MESSAGE' | 'SNAPSHOT' | 'ID'`.\n *\n * @remarks\n * Compaction requires a summarizer (it digests the folded slice into a section summary); a\n * conversation created without one can still store + `view()` its live tail, but a `compact()`\n * is a programmer error and throws this with `'SUMMARIZER'`.\n * A `sections` cap (on {@link import('./types.js').ConversationOptions} /\n * {@link import('./types.js').ConversationManagerOptions} /\n * {@link import('./types.js').CompactOptions}) must be a positive safe integer, and cannot combine with\n * `cumulative` — either is a programmer error and throws this with `'SECTIONS'`. A `keep` on the same options must be a nonnegative\n * safe integer — any other value is a programmer error and throws this with `'KEEP'`. A judgment\n * record or a judge request that JSON cannot carry, or whose copy fails its guard, is a\n * programmer error and throws this with `'JUDGMENT'`, its `context` naming the `question` id\n * where one exists. A summarizer digest whose trimmed text is empty rejects the `compact()` with\n * `'SUMMARY'`. A stored message plays a `ConversationRole`: `add` refuses any other role with\n * `'ROLE'` before it stores any message of the batch, a hydrating snapshot that holds one throws\n * `'ROLE'`, and an agent's selection that holds one becomes a `'ROLE'` fault. `add` refuses an input\n * whose other members fail `isMessageInput` with `'MESSAGE'`, its `context` holding the `faults`,\n * before it stores any message of the batch. A hydrating snapshot that fails\n * `isConversationSnapshot` or repeats an id, and a store row or written snapshot that does, throw\n * `'SNAPSHOT'`; a judgment the guard admits but JSON cannot carry still throws `'JUDGMENT'`. A\n * manager's `add` of an id it already holds throws `'ID'`. Narrow a caught value with\n * {@link isConversationError} and branch on `error.code`.\n */\nexport class ConversationError extends Error {\n\t/** Names the machine-readable condition — `'SUMMARIZER'`: a `compact()` with no summarizer; `'SECTIONS'`: a `sections` cap that is not a positive safe integer, or a cap combined with `cumulative`; `'JUDGMENT'`: a judgment or judge request that is not JSON; `'KEEP'`: a `keep` that is not a nonnegative safe integer; `'SUMMARY'`: a summarizer digest whose trimmed text is empty; `'ROLE'`: a stored or selected message whose role is not a `ConversationRole`; `'MESSAGE'`: a message input whose other members fail `isMessageInput`; `'SNAPSHOT'`: a snapshot that fails `isConversationSnapshot` or repeats an id; `'ID'`: a manager's `add` of an id it already holds. */\n\treadonly code: ConversationErrorCode\n\t/** Holds the values that locate the failure, such as the `question` id of a malformed question, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Creates the error with its code, message, and optional cause and context.\n\t *\n\t * @param code - The machine-readable {@link ConversationErrorCode}\n\t * @param message - The human-readable description\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: ConversationErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'ConversationError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ConversationError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link ConversationError} through its identity brand and declared members, so a\n * `catch` can branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link ConversationError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tawait conversation.compact()\n * } catch (error) {\n * \tif (isConversationError(error) && error.code === 'SUMMARIZER') addSummarizer()\n * }\n * ```\n */\nexport function isConversationError(value: unknown): value is ConversationError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ConversationError'))?.value ===\n\t\t\t\t\tvalue\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (\n\t\t\t!(\n\t\t\t\tcode === 'SUMMARIZER' ||\n\t\t\t\tcode === 'SECTIONS' ||\n\t\t\t\tcode === 'JUDGMENT' ||\n\t\t\t\tcode === 'KEEP' ||\n\t\t\t\tcode === 'SUMMARY' ||\n\t\t\t\tcode === 'ROLE' ||\n\t\t\t\tcode === 'MESSAGE' ||\n\t\t\t\tcode === 'SNAPSHOT' ||\n\t\t\t\tcode === 'ID'\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\nimport { judgeAnswerShape, judgeQuestionShape, messageShape, tokenUsageShape } from '../shapers.js'\n\n/**\n * Describes a stored judgment: exactly one of an answer or a refusal, with its question, sources,\n * state, model, and storage time.\n *\n * @remarks\n * `isJudgment` is the door a hydrate reads and this shape fills a refusal's faults, because hydrate is\n * a per-element path. A lockstep test holds that `is` implies the guard.\n */\nexport const judgmentShape = unionShape(\n\tobjectShape({\n\t\tid: stringShape(),\n\t\tquestion: judgeQuestionShape,\n\t\tmodel: stringShape(),\n\t\tsources: arrayShape(stringShape()),\n\t\tstate: stringShape(),\n\t\ttime: numberShape(),\n\t\tusage: optionalShape(tokenUsageShape),\n\t\tanswer: judgeAnswerShape,\n\t}),\n\tobjectShape({\n\t\tid: stringShape(),\n\t\tquestion: judgeQuestionShape,\n\t\tmodel: stringShape(),\n\t\tsources: arrayShape(stringShape()),\n\t\tstate: stringShape(),\n\t\ttime: numberShape(),\n\t\tusage: optionalShape(tokenUsageShape),\n\t\trefusal: objectShape({ missing: arrayShape(stringShape()) }),\n\t}),\n) satisfies ContractShape\n\n/** Describes a compacted section: its id, its summary, and the originals it folded. */\nexport const sectionShape = objectShape({\n\tid: stringShape(),\n\tsummary: stringShape(),\n\tmessages: arrayShape(messageShape),\n}) satisfies ContractShape\n\n/**\n * Describes a conversation snapshot exactly: an unknown key refuses, so a key a later version adds never\n * reads as absent.\n *\n * @remarks\n * `isConversationSnapshot` is the door a hydrate and a store read, and this shape fills a refusal's\n * faults. A lockstep test holds that `is` implies the guard, and on the snapshot fixtures the converse.\n */\nexport const conversationSnapshotShape = objectShape({\n\tid: stringShape(),\n\tsections: arrayShape(sectionShape),\n\tmessages: arrayShape(messageShape),\n\tjudgments: optionalShape(arrayShape(judgmentShape)),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { conversationSnapshotShape, judgmentShape, sectionShape } from './shapers.js'\n\n/** Reads the faults of a refused judgment. */\nexport const judgmentContract = createContract(judgmentShape)\n\n/** Reads the faults of a refused section. */\nexport const sectionContract = createContract(sectionShape)\n\n/** Reads the faults of a refused conversation snapshot. */\nexport const conversationSnapshotContract = createContract(conversationSnapshotShape)\n","import type { ConversationInterface, ConversationSnapshot, Section } from './types.js'\nimport type { Judgment, Message, MessageInput } from '../types.js'\nimport type { Guard } from '@orkestrel/contract'\nimport { isTokenUsage } from '@orkestrel/budget'\nimport {\n\tarrayOf,\n\tattempt,\n\tholds,\n\tisBoolean,\n\tisFiniteNumber,\n\tisFunction,\n\tisObject,\n\tisRecord,\n\tisString,\n\toptionalOf,\n\trecordOf,\n\tunionOf,\n} from '@orkestrel/contract'\nimport { isToolCall } from '@orkestrel/tool'\nimport { MESSAGE_ROLES } from '../constants.js'\nimport { isJudgeAnswer, isJudgeQuestion } from '../validators.js'\n\n/**\n * Checks whether a value satisfies the domain conversation-message contract.\n *\n * @remarks\n * Roles belong to MessageRole, image elements are strings, `thinking` is a string, and `call` is a string on any\n * role, as the flat Message type admits. Tool arguments may carry non-JSON values, as the\n * domain type permits; the message wire contract is narrower. The guard is exact on the message's own\n * keys: an unknown member, and an own `undefined` optional, return false. Unreadable fields and\n * untrusted inputs return false.\n *\n * @param value - The unknown message candidate\n * @returns True if the domain message fields are valid; false otherwise\n * @example\n * ```ts\n * isMessage({ id: '1', role: 'user', content: 'hi' }) // true\n * isMessage({ id: '1', role: 'other', content: '' }) // false\n * isMessage({ id: '1', role: 'user', content: '', images: [1] }) // false\n * isMessage({ id: '1', role: 'assistant', content: '', thinking: 1 }) // false\n * ```\n */\nexport function isMessage(value: unknown): value is Message {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { id, role, content, calls, call, images, thinking } = value\n\t\tif (!isString(id) || !isString(content)) return false\n\t\tif (!MESSAGE_ROLES.some((one) => one === role)) return false\n\t\tif (calls !== undefined && !arrayOf(isToolCall)(calls)) return false\n\t\tif (call !== undefined && !isString(call)) return false\n\t\tif (images !== undefined && !arrayOf(isString)(images)) return false\n\t\tif (thinking !== undefined && !isString(thinking)) return false\n\t\t// Three members are required, and a key the count misses is an unknown or an own undefined.\n\t\treturn (\n\t\t\tObject.keys(value).length ===\n\t\t\t3 +\n\t\t\t\t(calls === undefined ? 0 : 1) +\n\t\t\t\t(call === undefined ? 0 : 1) +\n\t\t\t\t(images === undefined ? 0 : 1) +\n\t\t\t\t(thinking === undefined ? 0 : 1)\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a value is a message input a conversation can store: a string `content`, and `calls`,\n * `call`, `images`, and `thinking` absent or of their declared types.\n *\n * @remarks\n * A hand guard, because `add` checks every message: it reads the members once and composes no record\n * guard. The role is `requireRole`'s to refuse, so its text can name the role received. Unknown members\n * are left behind by the store. An unreadable member returns false.\n *\n * @param value - The unknown input candidate\n * @returns True if the input's members are valid; false otherwise\n * @example\n * ```ts\n * isMessageInput({ role: 'user', content: 'hi' }) // true\n * isMessageInput({ role: 'user' }) // false\n * ```\n */\nexport function isMessageInput(value: unknown): value is MessageInput {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { content, calls, call, images, thinking } = value\n\t\treturn (\n\t\t\tisString(content) &&\n\t\t\t(calls === undefined || arrayOf(isToolCall)(calls)) &&\n\t\t\t(call === undefined || isString(call)) &&\n\t\t\t(images === undefined || arrayOf(isString)(images)) &&\n\t\t\t(thinking === undefined || isString(thinking))\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a stored judgment carries a valid question and exactly one answer or refusal.\n *\n * @remarks\n * Unknown members, malformed members, and unreadable inputs return false, the answer's and the\n * refusal's own included. Answer fields follow the root judge value types; this storage guard does not\n * impose a wire's probability or candidate limits, and the ties an answer holds to its question run\n * where a judgment is written, because records written before them can miss them. The `time`, `noul`,\n * and probability numbers must be finite, because a stored record is owned through JSON, which turns\n * `NaN` and the infinities into `null`; so every record this guard accepts hydrates through a\n * `JudgmentManager`.\n *\n * @param value - The unknown stored record\n * @returns True if the record satisfies the judgment contract; false otherwise\n * @example\n * ```ts\n * isJudgment({ id: 'q', question: { form: 'noul' }, answer: { form: 'noul', noul: 0.9 }, model: 'judge', sources: [], state: 'text', time: 0 }) // true\n * isJudgment({ id: 'q' }) // false\n * ```\n */\nexport const isJudgment: Guard<Judgment> = unionOf(\n\trecordOf(\n\t\t{\n\t\t\tid: isString,\n\t\t\tquestion: isJudgeQuestion,\n\t\t\tmodel: isString,\n\t\t\tsources: arrayOf(isString),\n\t\t\tstate: isString,\n\t\t\ttime: isFiniteNumber,\n\t\t\tusage: isTokenUsage,\n\t\t\tanswer: isJudgeAnswer,\n\t\t},\n\t\t['usage'],\n\t),\n\trecordOf(\n\t\t{\n\t\t\tid: isString,\n\t\t\tquestion: isJudgeQuestion,\n\t\t\tmodel: isString,\n\t\t\tsources: arrayOf(isString),\n\t\t\tstate: isString,\n\t\t\ttime: isFiniteNumber,\n\t\t\tusage: isTokenUsage,\n\t\t\trefusal: recordOf({ missing: arrayOf(isString) }),\n\t\t},\n\t\t['usage'],\n\t),\n)\n\n/**\n * Checks whether an `unknown` is structurally a {@link Section} record — a `string` `id` and\n * `summary` beside a `messages` array of valid {@link Message} records, the per-section step of\n * the {@link isConversationSnapshot} read-boundary narrow. Total, never throwing, and never an\n * assertion.\n *\n * @remarks\n * A total guard (it never throws — untrusted input, a throwing getter included, returns\n * `false`). It checks the shape of the section: a record with a `string` `id`, a `string`\n * `summary`, and a dense `messages` array every element of which is a valid {@link Message}\n * record ({@link isMessage}), and no other member; a sparse `messages` array is refused. Enough to\n * safely impose the {@link Section} type at a storage boundary without a cast.\n *\n * @param value - The value to test (one element of a snapshot's `sections` array)\n * @returns True if `value` has the structural shape of a {@link Section}; false otherwise\n *\n * @example\n * ```ts\n * isSection({ id: 's', summary: 'recap', messages: [{ id: '1', role: 'user', content: 'hi' }] }) // true\n * isSection({ id: 's', summary: 'recap', messages: 'nope' }) // false\n * isSection({ id: 's', messages: [] }) // false (missing summary)\n * ```\n */\nexport function isSection(value: unknown): value is Section {\n\treturn holds(() => {\n\t\tif (!isRecord(value) || Object.keys(value).length !== 3) return false\n\t\tconst { id, summary, messages } = value\n\t\treturn isString(id) && isString(summary) && arrayOf(isMessage)(messages)\n\t})\n}\n\n/**\n * Narrows an `unknown` to a {@link ConversationSnapshot} — a `string` `id` and valid\n * `sections` and `messages` arrays; the total boundary guard for an untrusted snapshot read (a storage row a\n * {@link import('./stores/DatabaseConversationStore.js').DatabaseConversationStore}\n * reads back from its opaque JSON column, a snapshot loaded from disk), never throwing. The exact\n * analogue of {@link import('@orkestrel/workspace').isWorkspaceSnapshot}.\n *\n * @remarks\n * A total guard (it never throws — untrusted input returns `false`). It checks the shape of the\n * snapshot: a `string` `id`, a `sections` array every element of which is a valid\n * {@link Section} ({@link isSection}), a `messages` array every element of which is a\n * valid {@link Message} ({@link isMessage}), and optional valid `judgments` — enough to safely\n * impose the\n * {@link ConversationSnapshot} type at a storage boundary without a cast. The structural twin of\n * {@link import('@orkestrel/workspace').isWorkspaceSnapshot}. The guard is exact: an unknown member, a\n * 0.0.35 snapshot's conversation `summary` included, and an own `undefined` `judgments` return\n * `false`, as a malformed blob (a non-record, a missing / non-string `id`, a non-array `sections` /\n * `messages`, or any malformed element) does, so a\n * {@link import('./stores/DatabaseConversationStore.js').DatabaseConversationStore}\n * read refuses it rather than seating a broken conversation. The guard is structural: it admits a\n * stored `system` role and a repeated id, which `requireConversationSnapshot` and the\n * {@link import('./Conversation.js').Conversation} constructor then refuse as the stored-data\n * check a conversation runs at hydrate.\n *\n * @param value - The value to test (an opaque storage read)\n * @returns True if `value` has the structural shape of a {@link ConversationSnapshot}; false otherwise\n *\n * @example\n * ```ts\n * isConversationSnapshot({ id: 'c1', sections: [], messages: [] }) // true\n * isConversationSnapshot({ id: 'c1', sections: 'nope', messages: [] }) // false\n * isConversationSnapshot({ sections: [], messages: [] }) // false (missing id)\n * ```\n */\nexport function isConversationSnapshot(value: unknown): value is ConversationSnapshot {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { id, sections, messages, judgments } = value\n\t\treturn (\n\t\t\tObject.keys(value).length === (judgments === undefined ? 3 : 4) &&\n\t\t\tisString(id) &&\n\t\t\tarrayOf(isSection)(sections) &&\n\t\t\tarrayOf(isMessage)(messages) &&\n\t\t\toptionalOf(arrayOf(isJudgment))(judgments)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value carries the {@link ConversationInterface} members a run reads: a string\n * `id`, a boolean `summarizable`, and `view`, `add`, `compact`, and `messages` methods.\n *\n * @remarks\n * A total guard: a throwing getter, a revoked `Proxy`, and any other malformed input return false.\n * It checks each member as its declared type and admits unknown members and class instances, so\n * any implementation the interface admits passes; it never calls a method.\n *\n * @param value - The value to test\n * @returns True if `value` carries the conversation members a run reads; false otherwise\n * @example\n * ```ts\n * isConversation(createConversation()) // true\n * isConversation({ id: 'c1' }) // false\n * ```\n */\nexport function isConversation(value: unknown): value is ConversationInterface {\n\tconst checked = attempt(() => {\n\t\tif (!isObject(value)) return false\n\t\treturn (\n\t\t\tisString(Reflect.get(value, 'id')) &&\n\t\t\tisBoolean(Reflect.get(value, 'summarizable')) &&\n\t\t\tisFunction(Reflect.get(value, 'view')) &&\n\t\t\tisFunction(Reflect.get(value, 'add')) &&\n\t\t\tisFunction(Reflect.get(value, 'compact')) &&\n\t\t\tisFunction(Reflect.get(value, 'messages'))\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n","import type { ConversationSnapshot, Section } from './types.js'\nimport type {\n\tConversationRole,\n\tJudgeAnswer,\n\tJudgeRequest,\n\tJudgeResult,\n\tJudgment,\n\tJudgmentInput,\n\tMessage,\n\tMessageInput,\n\tRefusal,\n} from '../types.js'\nimport type { ContractInterface } from '@orkestrel/contract'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tisArray,\n\tisJSONValue,\n\tisRecord,\n\tisString,\n\tpreview,\n} from '@orkestrel/contract'\nimport { CONVERSATION_RECAP_PREFIX } from './constants.js'\nimport { ConversationError } from './errors.js'\nimport { isConversationSnapshot, isJudgment, isMessageInput } from './validators.js'\nimport { CONVERSATION_ROLES } from '../constants.js'\nimport { isNonNegativeSafeInteger, isPositiveSafeInteger } from '../validators.js'\n\n/**\n * Reads what a judge result holds for one question, from its own keys alone.\n *\n * @param result - The reported answers and refusals\n * @param id - The question's id\n * @returns The answer and the refusal, each undefined when the result holds none\n * @example\n * ```ts\n * readJudgeOutcome({ model: 'm', answers: {} }, 'q') // { answer: undefined, refusal: undefined }\n * ```\n */\nexport function readJudgeOutcome(\n\tresult: JudgeResult,\n\tid: string,\n): { readonly answer: JudgeAnswer | undefined; readonly refusal: Refusal | undefined } {\n\treturn {\n\t\tanswer: Object.hasOwn(result.answers, id) ? result.answers[id] : undefined,\n\t\trefusal:\n\t\t\tresult.refusals !== undefined && Object.hasOwn(result.refusals, id)\n\t\t\t\t? result.refusals[id]\n\t\t\t\t: undefined,\n\t}\n}\n\n/**\n * Returns after checking a complete judge result answered or refused every question it was asked, or\n * throws a coded error naming those it left out.\n *\n * @remarks\n * A cancelled call's partial is not complete, so `JudgmentManager` reads this on a settled result only,\n * and {@link buildJudgments} stores whatever a partial holds.\n *\n * An undefined value is no answer, as {@link buildJudgments} reads it, so a key alone doesn't count.\n *\n * @param request - The request whose question keys must be answered\n * @param result - The result the judge settled\n * @throws ConversationError Thrown when a question has neither an answer nor a refusal (code `'JUDGMENT'`), naming them under `context.questions`\n * @example\n * ```ts\n * requireJudgeResult({ state: 'text', questions: { a: { form: 'noul' } } }, { model: 'm', answers: {} })\n * // throws ConversationError ('JUDGMENT')\n * ```\n */\nexport function requireJudgeResult(request: JudgeRequest, result: JudgeResult): void {\n\tconst left = Object.keys(request.questions).filter((id) => {\n\t\tconst { answer, refusal } = readJudgeOutcome(result, id)\n\t\treturn answer === undefined && refusal === undefined\n\t})\n\tif (left.length === 0) return\n\tthrow new ConversationError(\n\t\t'JUDGMENT',\n\t\t`conversation error: the judge left ${left.map((id) => preview(id)).join(', ')} neither answered nor refused; a judge must answer or refuse every question it is asked`,\n\t\t{ context: { questions: left } },\n\t)\n}\n\n/**\n * Builds records for answered or refused request keys, attaching usage only for a single question.\n *\n * @param request - The request whose question keys define record order\n * @param result - The reported answers, refusals, model, and usage\n * @param sources - The ordered source message ids\n * @param state - The rendered state read by the judge\n * @param model - The configured judge identity the records carry\n * @returns Inputs for completed question keys in request order\n * @example\n * ```ts\n * buildJudgments(request, result, ['message-a'], 'Charged twice', judge.model)\n * ```\n */\nexport function buildJudgments(\n\trequest: JudgeRequest,\n\tresult: JudgeResult,\n\tsources: readonly string[],\n\tstate: string,\n\tmodel: string,\n): readonly JudgmentInput[] {\n\tconst single = Object.keys(request.questions).length === 1\n\treturn Object.entries(request.questions).flatMap(([id, question]): readonly JudgmentInput[] => {\n\t\tconst { answer, refusal } = readJudgeOutcome(result, id)\n\t\tif (answer === undefined && refusal === undefined) return []\n\t\treturn [\n\t\t\t{\n\t\t\t\tid,\n\t\t\t\tquestion,\n\t\t\t\tmodel,\n\t\t\t\tsources,\n\t\t\t\tstate,\n\t\t\t\t...(answer !== undefined ? { answer } : refusal !== undefined ? { refusal } : {}),\n\t\t\t\t...(single && result.usage !== undefined ? { usage: result.usage } : {}),\n\t\t\t},\n\t\t]\n\t})\n}\n\n/**\n * Builds the raw synthetic summary message for one compacted section — role `'assistant'`, the\n * section's stable `id`, and its `summary` verbatim as content.\n *\n * @remarks\n * Pure and total. This is the unframed form a `sections` cap merge digests when it folds the\n * oldest sections into one (a summary over their section summaries); the recap label is absent\n * from that merge input. A cumulative fold instead re-reads the framed recap — see\n * {@link buildRecapMessage}.\n *\n * @param section - The compacted section to render\n * @returns The synthetic summary message\n *\n * @example\n * ```ts\n * buildSummaryMessage({ id: 's1', summary: 'recap', messages: [] })\n * // { id: 's1', role: 'assistant', content: 'recap' }\n * ```\n */\nexport function buildSummaryMessage(section: Section): Message {\n\treturn { id: section.id, role: 'assistant', content: section.summary }\n}\n\n/**\n * Builds the framed recap message for one compacted section — the same role and stable `id` as\n * {@link buildSummaryMessage}, with the content prefixed by {@link\n * import('./constants.js').CONVERSATION_RECAP_PREFIX}.\n *\n * @remarks\n * Pure and total. The prefix is what makes a small model read the message as a condensed\n * recap of earlier turns rather than a literal assistant turn to echo or answer from. It is a\n * fixed handful of tokens, so a conversation's `view()` stays lean however many sections it\n * carries.\n *\n * @param section - The compacted section to render\n * @returns The framed recap message\n *\n * @example\n * ```ts\n * buildRecapMessage({ id: 's1', summary: 'recap', messages: [] })\n * // { id: 's1', role: 'assistant', content: `${CONVERSATION_RECAP_PREFIX}recap` }\n * ```\n */\nexport function buildRecapMessage(section: Section): Message {\n\treturn {\n\t\tid: section.id,\n\t\trole: 'assistant',\n\t\tcontent: `${CONVERSATION_RECAP_PREFIX}${section.summary}`,\n\t}\n}\n\n/**\n * Returns a stored judgment record after checking it, or throws a coded error.\n *\n * @remarks\n * A stored record is the JSON copy of its input, so a proxied record is accepted and the stored\n * value is exactly what a snapshot can carry. The check is {@link isJudgment}.\n *\n * @param value - The JSON copy of a judgment record or input\n * @returns The value, narrowed to a {@link Judgment}\n * @throws ConversationError Thrown when the value is not a valid judgment record (code `'JUDGMENT'`)\n *\n * @example\n * ```ts\n * requireJudgment(copyJSON(record)) // the record, narrowed\n * requireJudgment({ id: 'refund' }) // throws ConversationError ('JUDGMENT')\n * ```\n */\nexport function requireJudgment(value: unknown): Judgment {\n\tif (!isJudgment(value)) {\n\t\tthrow new ConversationError('JUDGMENT', 'conversation error: a judgment must be a JSON record')\n\t}\n\treturn value\n}\n\n/**\n * Returns a stored message role after checking it is a conversation role, or throws a coded error.\n *\n * @remarks\n * A stored conversation message plays a `ConversationRole`; the `system` message is built per\n * request and never stored. The error message names the received role JSON-encoded and cut to 32\n * characters, or its `typeof` when it is not a string, and for the role `'system'` adds that host\n * text goes in the `system` option or an instruction.\n *\n * @param role - The role to check\n * @returns The role, narrowed to a `ConversationRole`\n * @throws ConversationError Thrown when the role is not a `CONVERSATION_ROLES` member (code `'ROLE'`)\n *\n * @example\n * ```ts\n * requireRole('user') // 'user'\n * requireRole('system') // throws ConversationError ('ROLE')\n * ```\n */\nexport function requireRole(role: unknown): ConversationRole {\n\tconst stored = CONVERSATION_ROLES.find((one) => one === role)\n\tif (stored === undefined) {\n\t\tthrow new ConversationError(\n\t\t\t'ROLE',\n\t\t\t`a stored message role must be one of ${CONVERSATION_ROLES.map((one) => JSON.stringify(one)).join(', ')}; received ${typeof role === 'string' ? JSON.stringify(role.slice(0, 32)) : typeof role}${role === 'system' ? '; host text goes in the system option or an instruction' : ''}`,\n\t\t)\n\t}\n\treturn stored\n}\n\n/**\n * Returns a message input after checking its role and its members, or throws a coded error.\n *\n * @remarks\n * The role goes first, through {@link requireRole}, so its refusal names what it received. The members\n * cross {@link isMessageInput}, and `audit` runs only after it refused, so a stored message costs one\n * guard read. Tool-call arguments must also be JSON values before a message is stored.\n *\n * @param input - The message input to check\n * @param contract - The message input contract, whose `audit` is read only on a refusal\n * @returns The input, narrowed\n * @throws ConversationError Thrown when the role is not a `ConversationRole` (code `'ROLE'`) or a member fails `isMessageInput` (code `'MESSAGE'`), with the faults under `context.faults`\n *\n * @example\n * ```ts\n * requireMessageInput({ role: 'user', content: 'hi' }, messageInputContract) // the input\n * requireMessageInput({ role: 'user' }, messageInputContract) // throws ConversationError ('MESSAGE')\n * ```\n */\nexport function requireMessageInput(\n\tinput: unknown,\n\tcontract: Pick<ContractInterface<unknown>, 'audit' | 'schema'>,\n): MessageInput {\n\trequireRole(isRecord(input) ? input.role : undefined)\n\tif (\n\t\tisMessageInput(input) &&\n\t\t(input.calls === undefined || input.calls.every((call) => isJSONValue(call.arguments)))\n\t)\n\t\treturn input\n\tconst faults = contract.audit(input)\n\tthrow new ConversationError(\n\t\t'MESSAGE',\n\t\t`a message input must carry string content, and calls, call, images, and thinking of their declared types\\n${describeFaults(faults, contract.schema)}`,\n\t\t{ context: { faults } },\n\t)\n}\n\n/**\n * Returns a conversation snapshot after checking its shape, its stored roles, and its ids, or\n * throws a coded error.\n *\n * @remarks\n * The check reads every own member of `value` once and builds a candidate from those reads: each\n * array member becomes a fresh array holding the same element objects, and a member that is not an\n * array stays as read, so the guard refuses it. The candidate must pass `isConversationSnapshot`, which\n * is exact, so an unknown key refuses rather than being dropped; every section message and live message\n * must play a `ConversationRole`; and no id may repeat across section ids, section message ids, and live\n * message ids. The `'ROLE'` refusal of a `system` message names the row a snapshot written before\n * 0.0.35 can hold, and each `'SNAPSHOT'` refusal asks for what `snapshot()` returned. The candidate\n * is returned, never `value`, so the check and the seating share one read. Element objects are kept\n * by reference, so a host is trusted not to edit a seated message object or to back its members\n * with accessors. The contract's `audit` runs only after the guard refused, to fill `context.faults`.\n *\n * @param value - The snapshot to check, typically a stored read\n * @param contract - The snapshot contract, whose `audit` is read only on a refusal\n * @returns The candidate snapshot\n * @throws ConversationError Thrown when the value fails `isConversationSnapshot` (code `'SNAPSHOT'`, its faults under `context.faults`), repeats an id (code `'SNAPSHOT'`), or holds a message whose role is not a `ConversationRole` (code `'ROLE'`)\n *\n * @example\n * ```ts\n * requireConversationSnapshot({ id: 'c1', sections: [], messages: [] }, conversationSnapshotContract) // { id: 'c1', sections: [], messages: [] }\n * requireConversationSnapshot({ id: 'c1', sections: 'nope', messages: [] }, conversationSnapshotContract) // throws ConversationError ('SNAPSHOT')\n * ```\n */\nexport function requireConversationSnapshot(\n\tvalue: unknown,\n\tcontract: Pick<ContractInterface<unknown>, 'audit' | 'schema'>,\n): ConversationSnapshot {\n\tconst candidate = attempt(() => {\n\t\tif (!isRecord(value)) return undefined\n\t\tconst read = { ...value }\n\t\tconst { sections, messages, judgments } = read\n\t\treturn {\n\t\t\t...read,\n\t\t\tsections: isArray(sections) ? Array.from(sections) : sections,\n\t\t\tmessages: isArray(messages) ? Array.from(messages) : messages,\n\t\t\t...(judgments === undefined\n\t\t\t\t? {}\n\t\t\t\t: { judgments: isArray(judgments) ? Array.from(judgments) : judgments }),\n\t\t}\n\t})\n\tif (!candidate.success) {\n\t\tthrow new ConversationError(\n\t\t\t'SNAPSHOT',\n\t\t\t'a conversation snapshot must be readable; pass what snapshot() returned',\n\t\t\t{ cause: candidate.error },\n\t\t)\n\t}\n\tif (!isConversationSnapshot(candidate.value)) {\n\t\tconst faults = contract.audit(candidate.value)\n\t\tthrow new ConversationError(\n\t\t\t'SNAPSHOT',\n\t\t\t`a conversation snapshot must carry a string id, valid sections and messages, valid judgments when present, and no other member; pass what snapshot() returned\\n${describeFaults(faults, contract.schema)}`,\n\t\t\t{ context: { faults } },\n\t\t)\n\t}\n\tconst snapshot = candidate.value\n\tsnapshot.sections\n\t\t.flatMap((section) => section.messages)\n\t\t.concat(snapshot.messages)\n\t\t.forEach((message) => {\n\t\t\tif (message.role === 'system')\n\t\t\t\tthrow new ConversationError(\n\t\t\t\t\t'ROLE',\n\t\t\t\t\t'a conversation snapshot holds a system message; a snapshot written before 0.0.35 can hold a system row; remove it before loading',\n\t\t\t\t)\n\t\t\trequireRole(message.role)\n\t\t})\n\tconst ids = [\n\t\t...snapshot.sections.map((section) => section.id),\n\t\t...snapshot.sections.flatMap((section) => section.messages.map((message) => message.id)),\n\t\t...snapshot.messages.map((message) => message.id),\n\t]\n\t// Built from the reversed pairs, the map keeps each id's first index.\n\tconst pairs = ids.map((id, index): readonly [string, number] => [id, index])\n\tconst firsts = new Map(pairs.toReversed())\n\tconst repeated = ids.find((id, index) => firsts.get(id) !== index)\n\tif (repeated !== undefined) {\n\t\tthrow new ConversationError(\n\t\t\t'SNAPSHOT',\n\t\t\t`a conversation snapshot repeats the id ${JSON.stringify(repeated.slice(0, 64))}; pass what snapshot() returned`,\n\t\t)\n\t}\n\treturn snapshot\n}\n\n/**\n * Returns a `sections` cap after checking it is a positive safe integer, or throws a coded error.\n *\n * @remarks\n * A fraction, positive infinity, and `NaN` are refused with `0` and a negative, because a count of\n * sections is a whole number. An absent cap leaves the sections list unbounded.\n *\n * @param cap - The cap to check, or `undefined` for no cap\n * @returns The cap unchanged, or `undefined` when none was supplied\n * @throws ConversationError Thrown when the cap is not a positive safe integer (code `'SECTIONS'`)\n *\n * @example\n * ```ts\n * requireSectionsCap(2) // 2\n * requireSectionsCap(undefined) // undefined\n * requireSectionsCap(0) // throws ConversationError ('SECTIONS')\n * ```\n */\nexport function requireSectionsCap(cap: number | undefined): number | undefined {\n\tif (cap !== undefined && !isPositiveSafeInteger(cap)) {\n\t\tthrow new ConversationError('SECTIONS', 'a sections cap must be a positive safe integer', {\n\t\t\tcontext: { sections: cap },\n\t\t})\n\t}\n\treturn cap\n}\n\n/**\n * Returns the cumulative switch after checking it does not combine with a `sections` cap, or throws a\n * coded error.\n *\n * @remarks\n * A cumulative conversation keeps one section, so a cap has nothing to bound. The cap checked is the\n * effective one: inherited from a manager, configured on the conversation, or supplied to one\n * `compact()` call.\n *\n * @param cumulative - Whether folds accumulate every prior section into one\n * @param cap - The effective `sections` cap, or `undefined` for none\n * @returns The cumulative switch unchanged\n * @throws ConversationError Thrown when `cumulative` is true and a cap is present (code `'SECTIONS'`)\n *\n * @example\n * ```ts\n * requireCumulative(true, undefined) // true\n * requireCumulative(false, 2) // false\n * requireCumulative(true, 2) // throws ConversationError ('SECTIONS')\n * ```\n */\nexport function requireCumulative(cumulative: boolean, cap: number | undefined): boolean {\n\tif (cumulative && cap !== undefined) {\n\t\tthrow new ConversationError('SECTIONS', 'cumulative cannot combine with a sections cap', {\n\t\t\tcontext: { sections: cap },\n\t\t})\n\t}\n\treturn cumulative\n}\n\n/**\n * Returns a retained-tail size after checking it is a nonnegative safe integer, or throws a coded\n * error.\n *\n * @remarks\n * The check is {@link import('../validators.js').isNonNegativeSafeInteger}, so `0` and `-0`\n * pass while `NaN`, a fraction, a negative, and either infinity are refused. The error's `context`\n * carries the refused value as `keep`.\n *\n * @param keep - The retained-tail size to check\n * @returns The size unchanged\n * @throws ConversationError Thrown when `keep` is not a nonnegative safe integer (code `'KEEP'`)\n *\n * @example\n * ```ts\n * requireKeep(2) // 2\n * requireKeep(1.5) // throws ConversationError ('KEEP')\n * ```\n */\nexport function requireKeep(keep: number): number {\n\tif (!isNonNegativeSafeInteger(keep)) {\n\t\tthrow new ConversationError('KEEP', 'a keep must be a nonnegative safe integer', {\n\t\t\tcontext: { keep },\n\t\t})\n\t}\n\treturn keep\n}\n\n/**\n * Returns a summarizer digest after checking it carries non-whitespace text, or throws a coded error.\n *\n * @remarks\n * A digest whose trimmed text is empty would commit a section that recaps nothing, so it is\n * refused. Any digest with a non-whitespace character passes unchanged, surrounding whitespace\n * included.\n *\n * @param summary - The summarizer digest to check\n * @returns The digest unchanged\n * @throws ConversationError Thrown when the digest is not a string or its trimmed text is empty (code `'SUMMARY'`)\n *\n * @example\n * ```ts\n * requireSummary('recap') // 'recap'\n * requireSummary(' \\n') // throws ConversationError ('SUMMARY')\n * ```\n */\nexport function requireSummary(summary: string): string {\n\tif (!isString(summary) || summary.trim() === '') {\n\t\tthrow new ConversationError('SUMMARY', 'a summarizer digest must carry non-whitespace text')\n\t}\n\treturn summary\n}\n\n/**\n * Settles with a promise, or rejects with the reason of a signal that aborts first, so a caller\n * is released at once even when the work behind the promise ignores the signal.\n *\n * @remarks\n * An absent signal returns `promise` itself. An already-aborted signal rejects with its reason\n * without waiting. The abort listener is removed as soon as `promise` settles. A rejection of\n * `promise` after the abort is handled here and never surfaces as unhandled.\n *\n * @param promise - The work to wait for\n * @param signal - The cancellation signal, or `undefined` for none\n * @returns A promise that settles as `promise` does, or rejects with `signal.reason` on abort\n *\n * @example\n * ```ts\n * const controller = new AbortController()\n * const raced = raceSignal(new Promise<string>(() => {}), controller.signal)\n * controller.abort(new Error('stop'))\n * await raced // rejects with the Error 'stop'\n * ```\n */\nexport function raceSignal<T>(promise: Promise<T>, signal: AbortSignal | undefined): Promise<T> {\n\tif (signal === undefined) return promise\n\treturn new Promise<T>((resolve, reject) => {\n\t\t// Aborting `settled` removes the abort listener after the promise settles.\n\t\tconst settled = new AbortController()\n\t\tif (signal.aborted) reject(signal.reason)\n\t\telse {\n\t\t\tsignal.addEventListener('abort', () => reject(signal.reason), {\n\t\t\t\tonce: true,\n\t\t\t\tsignal: settled.signal,\n\t\t\t})\n\t\t}\n\t\t// Settling an already-rejected race is a no-op, and this handler is what keeps a late\n\t\t// rejection of `promise` from surfacing as unhandled.\n\t\tvoid promise.then(resolve, reject).finally(() => settled.abort())\n\t})\n}\n","import type { JudgmentManagerInterface } from './types.js'\nimport type { JudgeInterface, JudgeRequest, Judgment, JudgmentInput } from '../types.js'\nimport { describeFaults, isArray, isRecord } from '@orkestrel/contract'\nimport { ConversationError } from './errors.js'\nimport { buildJudgments, requireJudgeResult, requireJudgment } from './helpers.js'\nimport { copyJSON, freezeJSON } from '../cloners.js'\nimport { judgeAnswerContract, judgeQuestionContract } from '../contracts.js'\nimport { JudgeAbortError, isJudgeAbortError } from '../errors.js'\nimport {\n\tdescribeQuestionRefusal,\n\tmatchesJudgment,\n\tremoveEntries,\n\tscanJudgmentTies,\n} from '../helpers.js'\nimport { isJudgeEntry, isJudgeQuestion } from '../validators.js'\n\n/**\n * Stores judgments in insertion order and asks a judge only for unmatched question identities.\n *\n * @remarks\n * Adding an existing key replaces it without changing its insertion position. Construction\n * restores recorded times; adding stamps the current epoch milliseconds. Caller input is owned\n * through JSON on arrival, so a proxied record or request is accepted where a structured clone\n * refuses it. Each record is stored deep-frozen, and every read returns that frozen record, so a\n * write to one throws in strict-mode code and a caller that edits a record copies it first.\n *\n * @example\n * ```ts\n * const judgments = new JudgmentManager()\n * const records = await judgments.resolve(judge, request, ['message-a'], signal)\n * ```\n */\nexport class JudgmentManager implements JudgmentManagerInterface {\n\treadonly #judgments: Map<string, Judgment>\n\n\t/**\n\t * Restores records without replacing their storage times.\n\t * @param judgments - The previously stored judgments to restore. Default: an empty list\n\t */\n\tconstructor(judgments: readonly Judgment[] = []) {\n\t\t// `Array.from` visits a hole in `judgments` as an undefined record, which `requireJudgment` refuses; `map` skips it.\n\t\tthis.#judgments = new Map(\n\t\t\tArray.from(judgments, (judgment) => requireJudgment(freezeJSON(judgment))).map((owned) => [\n\t\t\t\towned.id,\n\t\t\t\towned,\n\t\t\t]),\n\t\t)\n\t}\n\n\tget count(): number {\n\t\treturn this.#judgments.size\n\t}\n\n\tadd(input: JudgmentInput): Judgment\n\tadd(inputs: readonly JudgmentInput[]): readonly Judgment[]\n\tadd(input: JudgmentInput | readonly JudgmentInput[]): Judgment | readonly Judgment[] {\n\t\tif (isArray(input)) return input.map((one) => this.#store(one))\n\t\treturn this.#store(input)\n\t}\n\n\tjudgment(id: string): Judgment | undefined {\n\t\treturn this.#judgments.get(id)\n\t}\n\n\tjudgments(): readonly Judgment[] {\n\t\treturn [...this.#judgments.values()]\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) return removeEntries(ids, (id) => this.#judgments.delete(id))\n\t\treturn this.#judgments.delete(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#judgments.clear()\n\t}\n\n\tasync resolve(\n\t\tjudge: JudgeInterface,\n\t\trequest: JudgeRequest,\n\t\tsources: readonly string[],\n\t\tsignal: AbortSignal,\n\t): Promise<readonly Judgment[]> {\n\t\t// The request is owned through JSON before it is compared or sent, as the judge engine owns\n\t\t// it, so a proxied request is accepted and a caller mutating it mid-flight changes nothing.\n\t\tconst state = copyJSON(request.state)\n\t\tif (!isJudgeEntry(state)) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t'conversation error: the judge state is not a judge entry',\n\t\t\t)\n\t\t}\n\t\tconst rendered = typeof state === 'string' ? state : JSON.stringify(state)\n\t\tconst origins = [...sources]\n\t\tconst entries = isRecord(request.questions) ? Object.entries(request.questions) : []\n\t\tif (entries.length === 0) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t'conversation error: a judge request needs at least one question, keyed by id',\n\t\t\t)\n\t\t}\n\t\tconst checked = entries.map(([id, raw]) => {\n\t\t\tconst question = copyJSON(raw)\n\t\t\tif (!isJudgeQuestion(question)) {\n\t\t\t\tconst faults = judgeQuestionContract.audit(question)\n\t\t\t\tthrow new ConversationError(\n\t\t\t\t\t'JUDGMENT',\n\t\t\t\t\t`conversation error: ${describeQuestionRefusal(id, faults, judgeQuestionContract.schema)}`,\n\t\t\t\t\t{ context: { question: id, faults } },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn [id, question] as const\n\t\t})\n\t\tconst matched = new Map(\n\t\t\tchecked.flatMap(([id, question]) => {\n\t\t\t\tconst judgment = this.#judgments.get(id)\n\t\t\t\treturn judgment !== undefined &&\n\t\t\t\t\tmatchesJudgment(judgment, question, origins, rendered, judge.model)\n\t\t\t\t\t? [[id, judgment] as const]\n\t\t\t\t\t: []\n\t\t\t}),\n\t\t)\n\t\tconst pending = checked.filter(([id]) => !matched.has(id))\n\t\tconst added =\n\t\t\tpending.length === 0\n\t\t\t\t? []\n\t\t\t\t: await this.#ask(\n\t\t\t\t\t\tjudge,\n\t\t\t\t\t\t{ state, questions: Object.fromEntries(pending) },\n\t\t\t\t\t\torigins,\n\t\t\t\t\t\trendered,\n\t\t\t\t\t\tsignal,\n\t\t\t\t\t)\n\t\tconst resolved = new Map([\n\t\t\t...matched,\n\t\t\t...added.map((judgment) => [judgment.id, judgment] as const),\n\t\t])\n\t\treturn entries.flatMap(([id]) => {\n\t\t\tconst judgment = resolved.get(id)\n\t\t\treturn judgment === undefined ? [] : [judgment]\n\t\t})\n\t}\n\n\t// Asks the judge for the unmatched questions and stores what it answered. An aborted ask stores\n\t// its partial answers before it rethrows, so finished work survives the cancellation.\n\tasync #ask(\n\t\tjudge: JudgeInterface,\n\t\tunmatched: JudgeRequest,\n\t\torigins: readonly string[],\n\t\trendered: string,\n\t\tsignal: AbortSignal,\n\t): Promise<readonly Judgment[]> {\n\t\tif (signal.aborted) throw new JudgeAbortError({ model: judge.model, answers: {} })\n\t\ttry {\n\t\t\tconst result = await judge.ask(unmatched, signal)\n\t\t\trequireJudgeResult(unmatched, result)\n\t\t\treturn this.add(buildJudgments(unmatched, result, origins, rendered, judge.model))\n\t\t} catch (error) {\n\t\t\tif (isJudgeAbortError(error))\n\t\t\t\tthis.add(buildJudgments(unmatched, error.partial, origins, rendered, judge.model))\n\t\t\tthrow error\n\t\t}\n\t}\n\n\t#store(input: JudgmentInput): Judgment {\n\t\tconst copy = copyJSON(input)\n\t\tif (isRecord(copy) && copy.answer !== undefined && !judgeAnswerContract.is(copy.answer)) {\n\t\t\tconst faults = judgeAnswerContract.audit(copy.answer)\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t`conversation error: a judgment answer must satisfy its shape\\n${describeFaults(faults, judgeAnswerContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tconst judgment = requireJudgment(\n\t\t\tfreezeJSON(isRecord(copy) ? { ...copy, time: Date.now() } : copy),\n\t\t)\n\t\t// The ties run on a write alone, because a record written before them can miss them.\n\t\tconst faults =\n\t\t\tjudgment.answer === undefined ? [] : scanJudgmentTies(judgment.question, judgment.answer)\n\t\tif (faults.length > 0) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t`conversation error: judgment ${judgment.id} answers its question wrongly\\n${describeFaults(faults, judgeAnswerContract.schema)}`,\n\t\t\t\t{ context: { question: judgment.id, faults } },\n\t\t\t)\n\t\t}\n\t\tthis.#judgments.set(judgment.id, judgment)\n\t\treturn judgment\n\t}\n}\n","import type {\n\tCompactOptions,\n\tConversationEventMap,\n\tConversationInterface,\n\tConversationOptions,\n\tConversationReferenceOptions,\n\tConversationSnapshot,\n\tConversationSummaryHandler,\n\tJudgmentManagerInterface,\n\tSection,\n} from './types.js'\nimport type { Message, MessageInput } from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { messageInputContract } from '../contracts.js'\nimport { collectExchanges, removeEntries, stripThinking } from '../helpers.js'\nimport { conversationSnapshotContract } from './contracts.js'\nimport { DEFAULT_CONVERSATION_KEEP } from './constants.js'\nimport { ConversationError } from './errors.js'\nimport {\n\tbuildRecapMessage,\n\tbuildSummaryMessage,\n\traceSignal,\n\trequireConversationSnapshot,\n\trequireMessageInput,\n\trequireKeep,\n\trequireCumulative,\n\trequireSectionsCap,\n\trequireSummary,\n} from './helpers.js'\nimport { JudgmentManager } from './JudgmentManager.js'\n\n/**\n * Represents a conversation — a live uncompacted tail of messages it owns directly above a flat\n * message store, plus compacted, summarized {@link Section} records and a `summarizable` flag,\n * with on-demand `rehydrate` and substring `search`, driven by a provider-agnostic\n * {@link ConversationSummaryHandler} seam so `core` never imports a provider. Observable through\n * its own `emitter`.\n *\n * @remarks\n * - **Live tail + sections.** The conversation owns its live tail directly — `#messages` is an\n * insertion-ordered `Map` of immutable {@link Message} records keyed by their minted id\n * (`add` / `message` / `messages` / `remove` / `clear` / `count`), exactly as a `Workspace`\n * owns its files (no separate per-value manager). `#sections` are the compacted history\n * (oldest → newest), each a summarized slice that retains its originals.\n * - **Stored roles.** Every stored message plays a `ConversationRole`. `add` checks every role of\n * a batch before it stores any and throws `ConversationError` code `'ROLE'` for a `system` or\n * unknown role; the `system` message is built per request and never stored. A hydrating\n * `snapshot` is checked by `requireConversationSnapshot` before any member is seated: a\n * malformed snapshot or a repeated id across section ids, section message ids, and live message\n * ids throws `'SNAPSHOT'`, and a stored `system` message throws `'ROLE'`. The checked\n * snapshot's top-level members are read once, and its section and message objects are seated by\n * reference.\n * - **`view()`.** Each section folds to one synthetic summary message (role `'assistant'` — a\n * prior-context recap — keyed by the stable `id` of the section), then the live messages\n * verbatim. The per-section summaries are the compaction benefit.\n * - **`compact()`.** Folds the oldest `count - keep` live messages into a section (its\n * `summary` from `#summarize`), removes them from the live tail by id, and emits `compact`.\n * The fold stops before the newest user message, so the request a run serves and its turns\n * stay live. An exchange is a user message and every message after it up to the next user\n * message. Leading messages form a separate exchange, retained until the first user exchange\n * can also fold. A cut inside an exchange moves back to its start, so a fold removes whole\n * exchanges. A cut inside an assistant call group, which only a group spanning two exchanges\n * allows, moves before the group, so a tool result never stays live without its call. Returns\n * the section, or `undefined` when nothing folds. A {@link ConversationError} is thrown when no\n * `#summarize` was supplied. Calls run one at a time, a fold commits only when its messages are\n * still the live entries after the summarizer settles, and `options.signal` rejects the call\n * with its reason at once and commits nothing after the abort. A digest that is not a string or\n * whose trimmed text is empty refuses the fold it would commit with `'SUMMARY'`; such a digest for\n * a capped merge refuses the merge and keeps the fold already committed, one section over the cap,\n * which the next sound merge collapses.\n * - **Cumulative.** With `cumulative`, a fold makes one summarizer call over the framed recaps of all\n * prior sections then the slice, and replaces those sections with one that retains their\n * originals then the slice. It emits `compact` once and never `collapse`, commits nothing when\n * the call throws, aborts, or the slice or prior sections changed, and cannot combine with a\n * `sections` cap (`'SECTIONS'`).\n * - **`rehydrate(id)` / `search(query)`.** `rehydrate` returns the full original messages of a\n * known section and emits its `id` through `rehydrate`, even when the list is empty. An unknown\n * id returns `undefined` without an event. The read never reinserts messages. `search` is a case-insensitive\n * substring scan of `content` across all messages (the originals of every section + the live\n * tail).\n * - **Observable.** The owned {@link emitter} ({@link ConversationEventMap}) carries\n * `compact` / `collapse` / `rehydrate`, emitted directly, strictly after the state change;\n * the emitter isolates a listener throw and routes it to its `error` handler (the `error`\n * option), so a buggy observer can never corrupt a compaction.\n *\n * @example\n * ```ts\n * const conversation = new Conversation({ summarize: async (m) => `recap of ${m.length}` })\n * conversation.add([\n * \t{ role: 'user', content: 'Hello' },\n * \t{ role: 'assistant', content: 'Hi there' },\n * \t{ role: 'user', content: 'What did I say?' },\n * ])\n * const section = await conversation.compact() // folds the first two into one summarized section\n * conversation.view() // [<recap of 2>, { role: 'user', content: 'What did I say?' }]\n * ```\n */\nexport class Conversation implements ConversationInterface {\n\treadonly #id: string\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a compaction.\n\treadonly #emitter: Emitter<ConversationEventMap>\n\t// The provider-agnostic summarizer seam — `undefined` ⇒ `compact()` throws (a conversation\n\t// can still store + view a live tail; it cannot fold).\n\treadonly #summarize: ConversationSummaryHandler | undefined\n\t// How many recent live messages a `compact()` retains verbatim (older ones fold).\n\treadonly #keep: number\n\t// The optional cap on the compacted sections list — `undefined` ⇒ unlimited. Enforced\n\t// after appending a fresh `compact()` fold: an overflow folds the oldest sections into one.\n\treadonly #cap: number | undefined\n\t// Whether a fold replaces every prior section with one that digests their recaps and the slice.\n\t// Never combined with a cap.\n\treadonly #cumulative: boolean\n\t// The compacted history, oldest → newest — each summarized slice retains its originals.\n\t// Replaced on every change rather than mutated in place.\n\t#sections: readonly Section[]\n\t// The live uncompacted tail the conversation owns directly — an insertion-ordered Map of\n\t// immutable messages keyed by their minted id.\n\treadonly #messages: Map<string, Message>\n\treadonly #judgments: JudgmentManager\n\t// The folds in flight, one per `compact()` call from its start until it settles. A later call\n\t// waits on every member it finds, so an aborted waiter never releases it ahead of a fold still\n\t// running.\n\treadonly #pending = new Set<Promise<Section | undefined>>()\n\n\tconstructor(options?: ConversationOptions) {\n\t\t// A summarizer is a function and is never serialized, so it comes from options beside the\n\t\t// snapshot. Restoring is silent: nothing was edited, so no event fires. The snapshot is checked\n\t\t// before any member is seated, and every member is seated from the checked read alone.\n\t\tconst supplied = options?.snapshot\n\t\tconst snapshot =\n\t\t\tsupplied === undefined\n\t\t\t\t? undefined\n\t\t\t\t: requireConversationSnapshot(supplied, conversationSnapshotContract)\n\t\tthis.#judgments = new JudgmentManager(snapshot?.judgments)\n\t\tthis.#id = snapshot?.id ?? options?.id ?? crypto.randomUUID()\n\t\tthis.#emitter = new Emitter<ConversationEventMap>(options)\n\t\tthis.#summarize = options?.summarize\n\t\tthis.#keep = requireKeep(options?.keep ?? DEFAULT_CONVERSATION_KEEP)\n\t\tthis.#cap = requireSectionsCap(options?.sections)\n\t\tthis.#cumulative = options?.cumulative ?? false\n\t\trequireCumulative(this.#cumulative, this.#cap)\n\t\tthis.#sections = snapshot === undefined ? [] : [...snapshot.sections]\n\t\tthis.#messages = new Map(snapshot?.messages.map((message) => [message.id, message]))\n\t}\n\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\tget judgments(): JudgmentManagerInterface {\n\t\treturn this.#judgments\n\t}\n\n\tget emitter(): EmitterInterface<ConversationEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget sections(): readonly Section[] {\n\t\treturn [...this.#sections]\n\t}\n\n\tget summarizable(): boolean {\n\t\t// True exactly when a summarizer was supplied — the clean signal the automatic compaction of\n\t\t// the agent loop gates on (a non-summarizable conversation is never auto-compacted, so the\n\t\t// auto path never throws the `SUMMARIZER` ConversationError). A manual `compact()` still throws.\n\t\treturn this.#summarize !== undefined\n\t}\n\n\tget count(): number {\n\t\treturn this.#messages.size\n\t}\n\n\tadd(input: MessageInput): Message\n\tadd(inputs: readonly MessageInput[]): readonly Message[]\n\tadd(input: MessageInput | readonly MessageInput[]): Message | readonly Message[] {\n\t\tif (isArray(input)) {\n\t\t\t// Every input is checked before any element is stored, so a refused batch stores nothing.\n\t\t\tinput.forEach((one) => requireMessageInput(one, messageInputContract))\n\t\t\treturn input.map((one) => this.#create(one))\n\t\t}\n\t\treturn this.#create(requireMessageInput(input, messageInputContract))\n\t}\n\n\tmessage(id: string): Message | undefined {\n\t\treturn this.#messages.get(id)\n\t}\n\n\tmessages(): readonly Message[] {\n\t\treturn [...this.#messages.values()]\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) {\n\t\t\treturn removeEntries(ids, (id) => this.#messages.delete(id))\n\t\t}\n\t\treturn this.#messages.delete(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#messages.clear()\n\t}\n\n\tview(): readonly Message[] {\n\t\t// The recap is framed (a `[Summary of earlier messages]` label prefix) so a small model reads\n\t\t// it as a condensed recap of prior turns, not as a literal assistant turn it must echo / treat\n\t\t// as the latest answer — a lean label (a handful of tokens), proven no-bloat by a test guard.\n\t\treturn [\n\t\t\t...this.#sections.map((section) => buildRecapMessage(section)),\n\t\t\t...this.#messages.values(),\n\t\t]\n\t}\n\n\tasync compact(options?: CompactOptions): Promise<Section | undefined> {\n\t\tconst summarize = this.#summarize\n\t\tif (summarize === undefined) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'SUMMARIZER',\n\t\t\t\t'cannot compact a conversation without a summarizer',\n\t\t\t)\n\t\t}\n\t\tconst cap = requireSectionsCap(options?.sections ?? this.#cap)\n\t\trequireCumulative(this.#cumulative, cap)\n\t\tconst keep = requireKeep(options?.keep ?? this.#keep)\n\t\tconst signal = options?.signal\n\t\tsignal?.throwIfAborted()\n\t\t// Queue behind every earlier fold, captured before this call adds its own. An earlier waiter\n\t\t// that aborts leaves the folds it waited on in the set, so a later call still waits for them.\n\t\tconst previous = this.#pending.size === 0 ? undefined : Promise.allSettled([...this.#pending])\n\t\tconst fold = this.#fold(previous, summarize, cap, keep, signal)\n\t\tthis.#pending.add(fold)\n\t\tvoid fold.then(\n\t\t\t() => this.#pending.delete(fold),\n\t\t\t() => this.#pending.delete(fold),\n\t\t)\n\t\treturn fold\n\t}\n\n\trehydrate(id: string): readonly Message[] | undefined {\n\t\tconst section = this.#sections.find((one) => one.id === id)\n\t\tif (section === undefined) return undefined\n\t\t// Emitted after resolving, because a read has no mutation for a listener to perturb.\n\t\tthis.#emitter.emit('rehydrate', id)\n\t\treturn section.messages\n\t}\n\n\tsearch(query: string): readonly Message[] {\n\t\t// Section originals (oldest → newest) come before the live tail.\n\t\tconst needle = query.toLowerCase()\n\t\tconst all = [\n\t\t\t...this.#sections.flatMap((section) => section.messages),\n\t\t\t...this.#messages.values(),\n\t\t]\n\t\treturn all.filter((message) => message.content.toLowerCase().includes(needle))\n\t}\n\n\treference(options?: ConversationReferenceOptions): string {\n\t\t// The leading marker states the block is not part of the live conversation, so a small\n\t\t// model reads the cherry-picked excerpts as foreign material it attributes to that source,\n\t\t// never as its own latest turns.\n\t\tconst label = options?.label ?? this.#id\n\t\tconst messages = options?.messages ?? []\n\t\t// `Array.from` visits a hole in `messages` as an undefined message, as iteration does; `map` skips it.\n\t\treturn [\n\t\t\t`[Reference — conversation \"${label}\" — NOT part of this conversation]`,\n\t\t\t...(messages.length > 0\n\t\t\t\t? [\n\t\t\t\t\t\t'Relevant messages:',\n\t\t\t\t\t\t...Array.from(messages, (message) => `- ${message.role}: ${message.content}`),\n\t\t\t\t\t]\n\t\t\t\t: []),\n\t\t].join('\\n')\n\t}\n\n\tsnapshot(): ConversationSnapshot {\n\t\t// The summarizer / keep are not serialized — they are live config re-supplied on hydrate\n\t\t// (a ConversationSummaryHandler is a function, not data).\n\t\treturn {\n\t\t\tid: this.#id,\n\t\t\tsections: this.sections,\n\t\t\tmessages: this.messages(),\n\t\t\t...(this.#judgments.count === 0 ? {} : { judgments: this.#judgments.judgments() }),\n\t\t}\n\t}\n\n\t// One serialized fold: wait for the earlier call, read the live tail, summarize, then commit\n\t// only what is unchanged. Every await is raced against the signal and followed by a check, so\n\t// an abort commits nothing after it.\n\tasync #fold(\n\t\tprevious: Promise<unknown> | undefined,\n\t\tsummarize: ConversationSummaryHandler,\n\t\tcap: number | undefined,\n\t\tkeep: number,\n\t\tsignal: AbortSignal | undefined,\n\t): Promise<Section | undefined> {\n\t\tif (previous !== undefined) await raceSignal(previous, signal)\n\t\tsignal?.throwIfAborted()\n\t\tconst live = [...this.#messages.values()]\n\t\t// The newest user message is the request a run serves, so it and its turns stay live.\n\t\tconst newest = live.findLastIndex((message) => message.role === 'user')\n\t\tconst first = live.findIndex((message) => message.role === 'user')\n\t\tconst wanted = Math.min(\n\t\t\tkeep <= 0 ? live.length : live.length - keep,\n\t\t\tnewest === -1 ? live.length : newest,\n\t\t)\n\t\t// A cut inside an exchange moves back to that exchange's start so a whole exchange folds or\n\t\t// stays. At most one exchange strictly contains the cut.\n\t\tconst { aligned } = collectExchanges(live).reduce(\n\t\t\t(cut, exchange) => {\n\t\t\t\tconst end = cut.start + exchange.length\n\t\t\t\treturn { start: end, aligned: cut.start < wanted && wanted < end ? cut.start : cut.aligned }\n\t\t\t},\n\t\t\t{ start: 0, aligned: wanted },\n\t\t)\n\t\t// Retain the leading context until the first user exchange can fold with it.\n\t\tconst fold = first >= 0 && aligned <= first ? 0 : aligned\n\t\tif (fold <= 0) return undefined\n\t\tconst slice = live.slice(0, fold)\n\t\t// A cumulative fold digests every prior recap (framed, oldest first) ahead of the slice in one\n\t\t// call; the originals behind those recaps are never sent.\n\t\tconst prior = this.#cumulative ? this.#sections : []\n\t\tconst request = [...prior.map((one) => buildRecapMessage(one)), ...stripThinking(slice, 'none')]\n\t\tconst summary = await raceSignal(summarize(request, signal), signal)\n\t\tsignal?.throwIfAborted()\n\t\trequireSummary(summary)\n\t\t// A `remove` or `clear` while the summarizer ran changed what this fold covers, so commit\n\t\t// nothing rather than fold messages the caller dropped back into a section.\n\t\tif (!slice.every((message) => this.#messages.get(message.id) === message)) return undefined\n\t\t// The recaps the digest covers must still be the sections held, or the replacement would drop\n\t\t// a section it never summarized.\n\t\tif (\n\t\t\tthis.#cumulative &&\n\t\t\t(this.#sections.length !== prior.length ||\n\t\t\t\t!prior.every((one, index) => this.#sections[index] === one))\n\t\t) {\n\t\t\treturn undefined\n\t\t}\n\t\tconst section: Section = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\tsummary,\n\t\t\tmessages: this.#cumulative ? [...prior.flatMap((one) => one.messages), ...slice] : slice,\n\t\t}\n\t\tfor (const message of slice) this.#messages.delete(message.id)\n\t\tthis.#sections = this.#cumulative ? [section] : [...this.#sections, section]\n\t\t// Preserve the original sections if the merge fails by replacing them only after summarizing.\n\t\tif (cap !== undefined && this.#sections.length > cap) {\n\t\t\tconst overflow = this.#sections.length - cap + 1\n\t\t\tconst folded = this.#sections.slice(0, overflow)\n\t\t\tconst digest = await raceSignal(\n\t\t\t\tsummarize(\n\t\t\t\t\tfolded.map((one) => buildSummaryMessage(one)),\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t\tsignal,\n\t\t\t)\n\t\t\tsignal?.throwIfAborted()\n\t\t\trequireSummary(digest)\n\t\t\t// Only a fold changes `#sections`, and folds run one at a time, so the head holds here;\n\t\t\t// the check keeps a merge from ever replacing sections it did not summarize.\n\t\t\tif (!folded.every((one, index) => this.#sections[index] === one)) return undefined\n\t\t\tconst merged: Section = {\n\t\t\t\tid: crypto.randomUUID(),\n\t\t\t\tsummary: digest,\n\t\t\t\tmessages: folded.flatMap((one) => one.messages),\n\t\t\t}\n\t\t\tthis.#sections = [merged, ...this.#sections.slice(overflow)]\n\t\t\tthis.#emitter.emit('collapse', merged)\n\t\t}\n\t\t// Observe the section last, so a swallowed listener throw can't perturb the fold.\n\t\tthis.#emitter.emit('compact', section)\n\t\treturn section\n\t}\n\n\t// Spread each optional only when present, so an absent optional is never stored as `undefined`.\n\t#create(input: MessageInput): Message {\n\t\tconst message: Message = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\trole: input.role,\n\t\t\tcontent: input.content,\n\t\t\t...(input.calls === undefined\n\t\t\t\t? {}\n\t\t\t\t: {\n\t\t\t\t\t\tcalls: input.calls.map(({ id, name, arguments: arguments_ }) => ({\n\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\tname,\n\t\t\t\t\t\t\targuments: arguments_,\n\t\t\t\t\t\t})),\n\t\t\t\t\t}),\n\t\t\t...(input.call === undefined ? {} : { call: input.call }),\n\t\t\t...(input.images === undefined ? {} : { images: input.images }),\n\t\t\t...(input.thinking === undefined ? {} : { thinking: input.thinking }),\n\t\t}\n\t\tthis.#messages.set(message.id, message)\n\t\treturn message\n\t}\n}\n","import type {\n\tConversationInput,\n\tConversationInterface,\n\tConversationManagerInterface,\n\tConversationManagerOptions,\n\tConversationStoreInterface,\n\tConversationSummaryHandler,\n} from './types.js'\nimport { isArray, preview } from '@orkestrel/contract'\nimport { DEFAULT_CONVERSATION_KEEP } from './constants.js'\nimport { Conversation } from './Conversation.js'\nimport { ConversationError } from './errors.js'\nimport { requireKeep } from './helpers.js'\nimport { removeEntries } from '../helpers.js'\n\n/**\n * Registers {@link Conversation} instances keyed by `id`, in insertion order, with an active pointer —\n * the id-keyed store over the conversation layer, the `active` / `switch` seam the\n * {@link import('../contexts/index.js').AgentContext} renders, and the durable `open` / `save`\n * store seam. Event-free (a registry, like\n * {@link import('@orkestrel/workspace').WorkspaceManager}); the observability lives on each\n * {@link Conversation}.\n *\n * @remarks\n * - **Registry.** Conversations live in an insertion-ordered `Map` keyed by `id`. `add(input?)`\n * mints a {@link Conversation} (its `id` from `input` or `crypto.randomUUID()`), flowing the\n * default `#summarize` / `#keep` of the manager in unless the `input` overrides them, and stores\n * it (an already-present `id` throws a `ConversationError` coded `'ID'`, because replacing a live\n * conversation would orphan its turns). `count` is the map size,\n * `conversation(id)` looks one up, `conversations()` lists them in insertion order.\n * - **Active pointer.** `active` is the active conversation (the conversation an agent run binds\n * when it names none), `undefined` until the first `add` (which auto-activates it — a registry\n * with conversations always has one active). A subsequent `add` leaves `active` unchanged.\n * `switch(id)` re-points `active` to the conversation with `id` and returns it; an unknown `id`\n * returns `undefined` and leaves `active` unchanged (the lenient lookup style — never throws,\n * no added error code).\n * - **Removal.** `remove` drops one by id, or a batch — `true` only when every supplied id was\n * removed; removing the active conversation sets `active` to `undefined`. `clear` empties the registry\n * and sets `active` to `undefined`.\n * - **Event-free.** A purely registry store — no Emitter, no events (each conversation owns\n * its own observable `emitter`).\n *\n * @example\n * ```ts\n * const manager = new ConversationManager({ summarize: async (m) => `recap of ${m.length}` })\n * const conversation = manager.add() // auto-activates — active === conversation\n * manager.add({ id: 'scratch' }) // leaves active unchanged\n * manager.switch('scratch') // re-points active to the 'scratch' conversation\n * manager.count // 2\n * ```\n */\nexport class ConversationManager implements ConversationManagerInterface {\n\treadonly #conversations = new Map<string, ConversationInterface>()\n\t// The active conversation's id — the message source the context renders; `undefined` until the\n\t// first `add` auto-activates one (kept as an id, never a stale reference, so a re-add / removal\n\t// of that id is reflected through the live map lookup in `active`).\n\t#active: string | undefined\n\t// The default summarizer flowed into every conversation `add` creates (a per-`add` override\n\t// wins); a conversation created with neither cannot `compact` (it throws a ConversationError).\n\treadonly #summarize: ConversationSummaryHandler | undefined\n\t// The default retained-tail size flowed into every conversation `add` creates (overridable).\n\treadonly #keep: number\n\t// The default `sections` cap flowed into every conversation `add` creates (overridable); `undefined` ⇒ unlimited.\n\treadonly #sections: number | undefined\n\t// The optional durable store backing `open` / `save`; `undefined` ⇒ registry-only (both lenient).\n\treadonly #store: ConversationStoreInterface | undefined\n\n\tconstructor(options?: ConversationManagerOptions) {\n\t\tthis.#summarize = options?.summarize\n\t\tthis.#keep = requireKeep(options?.keep ?? DEFAULT_CONVERSATION_KEEP)\n\t\tthis.#sections = options?.sections\n\t\tthis.#store = options?.store\n\t}\n\n\tget count(): number {\n\t\treturn this.#conversations.size\n\t}\n\n\tget active(): ConversationInterface | undefined {\n\t\treturn this.#active === undefined ? undefined : this.#conversations.get(this.#active)\n\t}\n\n\tconversation(id: string): ConversationInterface | undefined {\n\t\treturn this.#conversations.get(id)\n\t}\n\n\tconversations(): readonly ConversationInterface[] {\n\t\treturn [...this.#conversations.values()]\n\t}\n\n\tadd(input?: ConversationInput): ConversationInterface {\n\t\tconst held = input?.id ?? input?.snapshot?.id\n\t\tif (held !== undefined && this.#conversations.has(held)) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'ID',\n\t\t\t\t`conversation ${preview(held)} is already live; pick another id, or remove it first`,\n\t\t\t\t{ context: { conversation: held } },\n\t\t\t)\n\t\t}\n\t\t// An optional `snapshot` hydrates through the declared `ConversationOptions.snapshot` seam,\n\t\t// so the live summarize / keep ride beside it in the same options object.\n\t\tconst sections = input?.sections ?? this.#sections\n\t\tconst summarize = input?.summarize ?? this.#summarize\n\t\tconst conversation = new Conversation({\n\t\t\t...(input?.id === undefined ? {} : { id: input.id }),\n\t\t\t...(input?.on === undefined ? {} : { on: input.on }),\n\t\t\t...(input?.error === undefined ? {} : { error: input.error }),\n\t\t\t...(summarize === undefined ? {} : { summarize }),\n\t\t\tkeep: input?.keep ?? this.#keep,\n\t\t\t...(sections === undefined ? {} : { sections }),\n\t\t\t...(input?.cumulative === undefined ? {} : { cumulative: input.cumulative }),\n\t\t\t...(input?.snapshot === undefined ? {} : { snapshot: input.snapshot }),\n\t\t})\n\t\tthis.#conversations.set(conversation.id, conversation)\n\t\t// First conversation auto-activates: a registry that holds conversations always has one active.\n\t\tif (this.#active === undefined) this.#active = conversation.id\n\t\treturn conversation\n\t}\n\n\tswitch(id: string): ConversationInterface | undefined {\n\t\t// Lenient (mirrors `conversation(id)`): an unknown id re-points nothing and returns undefined.\n\t\tconst conversation = this.#conversations.get(id)\n\t\tif (conversation === undefined) return undefined\n\t\tthis.#active = id\n\t\treturn conversation\n\t}\n\n\tasync open(id: string): Promise<ConversationInterface | undefined> {\n\t\tconst existing = this.#conversations.get(id)\n\t\tif (existing !== undefined) {\n\t\t\tthis.#active = id\n\t\t\treturn existing\n\t\t}\n\t\tif (this.#store === undefined) return undefined\n\t\tconst snapshot = await this.#store.get(id)\n\t\t// A conversation registered while the read was pending is live state; the stored snapshot\n\t\t// is older, so it must not overwrite it.\n\t\tconst registered = this.#conversations.get(id)\n\t\tif (registered !== undefined) {\n\t\t\tthis.#active = id\n\t\t\treturn registered\n\t\t}\n\t\tif (snapshot === undefined) return undefined\n\t\t// A store answering with another conversation's snapshot would register and activate the\n\t\t// wrong id, so only a snapshot of `id` hydrates.\n\t\tif (snapshot.id !== id) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`the store answered ${preview(id)} with the snapshot of ${preview(snapshot.id)}; a store returns only the row of the id it is asked`,\n\t\t\t)\n\t\t}\n\t\t// Reuse `add` so the default summarize / keep of the manager flow into the rehydrated conversation.\n\t\tconst conversation = this.add({ snapshot })\n\t\t// Re-point `active` explicitly: `add` only auto-activates the first conversation, so an open\n\t\t// into a non-empty registry must still make the rehydrated one active.\n\t\tthis.#active = conversation.id\n\t\treturn conversation\n\t}\n\n\tasync save(id: string): Promise<boolean> {\n\t\tconst conversation = this.#conversations.get(id)\n\t\tif (this.#store === undefined || conversation === undefined) return false\n\t\tawait this.#store.set(conversation.snapshot())\n\t\treturn true\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) {\n\t\t\t// Each present id still clears `active` when it was the active conversation.\n\t\t\treturn removeEntries(ids, (id) => this.#drop(id))\n\t\t}\n\t\treturn this.#drop(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#conversations.clear()\n\t\t// An emptied registry has no active conversation.\n\t\tthis.#active = undefined\n\t}\n\n\t// Delete one conversation by id; when it was the active one, clear the active pointer (a removed\n\t// conversation can never stay active).\n\t#drop(id: string): boolean {\n\t\tconst removed = this.#conversations.delete(id)\n\t\tif (removed && this.#active === id) this.#active = undefined\n\t\treturn removed\n\t}\n}\n","import type { ConversationSnapshot, ConversationStoreInterface } from '../types.js'\n\n/**\n * Implements the {@link ConversationStoreInterface} in memory — a process-lifetime `Map` of\n * {@link ConversationSnapshot} records keyed by conversation id, the default store\n * {@link import('../factories.js').createMemoryConversationStore} builds and the default\n * backing for `open` / `save`. The exact twin of\n * {@link import('@orkestrel/workspace').MemoryWorkspaceStore}.\n *\n * @remarks\n * A plain `Map<string, ConversationSnapshot>` — the snapshot is already pure,\n * self-contained JSON, so no encoding is needed for the memory tier. Like the\n * {@link import('@orkestrel/workspace').MemoryWorkspaceStore} it twins,\n * there is no idle-TTL and no eviction: a persisted conversation lives until an explicit `delete`. A\n * durable backend (JSON / SQLite / IndexedDB) swaps in through the same interface without touching\n * the {@link import('../ConversationManager.js').ConversationManager} or the\n * {@link import('../Conversation.js').Conversation} — its driver-pluggable twin is\n * {@link import('./DatabaseConversationStore.js').DatabaseConversationStore} (the snapshot as one\n * opaque JSON column).\n *\n * - **`get` resolves the persisted snapshot for an id**, or `undefined` if none is stored.\n * - **`set` inserts / replaces under the snapshot's own `id`** (no separate id param).\n * - **`delete` drops a snapshot by id**; an absent id is a no-op (no throw).\n * - **`get` hands back the stored snapshot object itself**: its messages and sections are shared\n * with the store, so an edit to them changes what the next `get` returns, and its judgments are\n * frozen records, so a write to one throws `TypeError` in strict-mode code; copy a judgment\n * through `copyJSON` to migrate or redact it.\n *\n * The public surface is exactly `get` / `set` / `delete` — no extra members (the method\n * bijection with {@link ConversationStoreInterface}). Hydration is a caller concern: a\n * {@link import('../ConversationManager.js').ConversationManager} reads a snapshot back and rebuilds\n * the live conversation through the `snapshot` option (its `open` / `save`).\n *\n * @example\n * ```ts\n * import { createConversation, createMemoryConversationStore } from '@orkestrel/agent'\n *\n * const store = createMemoryConversationStore()\n * const conversation = createConversation()\n * conversation.add({ role: 'user', content: 'hello' })\n * await store.set(conversation.snapshot()) // persist the conversation\n * const snapshot = await store.get(conversation.id)\n * await store.delete(conversation.id) // drop it\n * ```\n */\nexport class MemoryConversationStore implements ConversationStoreInterface {\n\treadonly #snapshots = new Map<string, ConversationSnapshot>()\n\n\t/**\n\t * Resolves the persisted snapshot for `id`, or `undefined` if none is stored.\n\t *\n\t * @param id - The conversation id to resolve (a {@link ConversationSnapshot.id})\n\t * @returns The persisted snapshot, or `undefined` if absent\n\t */\n\tget(id: string): Promise<ConversationSnapshot | undefined> {\n\t\treturn Promise.resolve(this.#snapshots.get(id))\n\t}\n\n\t/**\n\t * Inserts or replaces a snapshot under its own `snapshot.id` (no separate id param —\n\t * mirroring the `set` method of {@link import('@orkestrel/workspace').WorkspaceStoreInterface}).\n\t *\n\t * @param snapshot - The snapshot to store (keyed by its `id`)\n\t * @returns A promise that resolves after the snapshot is stored\n\t */\n\tset(snapshot: ConversationSnapshot): Promise<void> {\n\t\tthis.#snapshots.set(snapshot.id, snapshot)\n\t\treturn Promise.resolve()\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op (no throw).\n\t *\n\t * @param id - The conversation id to drop\n\t * @returns A promise that resolves after the snapshot is dropped\n\t */\n\tdelete(id: string): Promise<void> {\n\t\t// `Map.delete` of an absent id is a no-op, so no presence check is needed.\n\t\tthis.#snapshots.delete(id)\n\t\treturn Promise.resolve()\n\t}\n}\n","import type {\n\tConversationSnapshot,\n\tConversationSnapshotRow,\n\tConversationStoreInterface,\n} from '../types.js'\nimport type { AuditFault } from '@orkestrel/contract'\nimport type { TableInterface } from '@orkestrel/database'\nimport { describeFaults, preview } from '@orkestrel/contract'\nimport { conversationSnapshotContract } from '../contracts.js'\nimport { ConversationError } from '../errors.js'\nimport { isConversationSnapshot } from '../validators.js'\nimport { describeSnapshotRefusal } from '../../helpers.js'\n\n/**\n * Backs a {@link ConversationStoreInterface} with one table of the `databases` layer — a\n * conversation's durable state is a row holding the snapshot as one opaque JSON column, narrowed\n * back on `get` by {@link import('../validators.js').isConversationSnapshot}, so persistence\n * reduces to keyed point-access (`get` / `set` / `delete`) over a {@link TableInterface}. The\n * driver-pluggable twin of the plain-`Map`\n * {@link import('./MemoryConversationStore.js').MemoryConversationStore}, and the exact twin of\n * {@link import('@orkestrel/workspace').DatabaseWorkspaceStore}.\n *\n * @remarks\n * The store is driver-agnostic: it holds a single {@link TableInterface} whose backend (memory,\n * JSON, SQLite, IndexedDB) is chosen by whoever builds it (the factories), so a JSON / SQLite /\n * IndexedDB backend swaps in without touching the\n * {@link import('../ConversationManager.js').ConversationManager} or the\n * {@link import('../Conversation.js').Conversation} — the same seam as\n * {@link import('@orkestrel/workspace').DatabaseWorkspaceStore}. Default driver: memory\n * ({@link import('../factories.js').createDatabaseConversationStore} passes\n * `createMemoryDriver()`), so it also works in memory out of the box; you opt into the\n * durable plumbing by passing a JSON / SQLite / IndexedDB driver.\n *\n * The {@link ConversationSnapshot} is stored as one opaque JSON column — the table is a row of\n * `{ id; snapshot }` ({@link ConversationSnapshotRow}), the snapshot the whole JSON blob (a\n * `rawShape` column the factory builds) — exactly as `DatabaseWorkspaceStore` stores its snapshot.\n * The snapshot is already a complete, self-contained, pure-JSON payload, so storing it whole is\n * lossless and keeps the row type flat (`snapshot` reads back as `unknown`).\n *\n * - **`set(snapshot)` upserts under the snapshot's own `id`** (no separate id param) — it writes\n * the row `{ id: snapshot.id, snapshot }`.\n * - **`get(id)` resolves the stored snapshot for an id**, narrowing the opaque JSON column back to\n * a {@link ConversationSnapshot} ({@link import('../validators.js').isConversationSnapshot} — the\n * total guard for an untrusted storage read), or `undefined` only when no row is stored. A row whose\n * blob is malformed or names another id throws a `ConversationError` coded `'SNAPSHOT'`, so `open`\n * and `create` stop rather than overwrite it, and `delete` still removes it.\n * - **`delete(id)` drops a snapshot by id**; an absent id is a no-op (no throw).\n *\n * Unlike a session store there is no idle-TTL / eviction — a persisted conversation lives until an\n * explicit `delete`. The public surface is exactly `get` / `set` / `delete` — no extra members (the\n * method bijection with {@link ConversationStoreInterface}). Hydration stays a caller concern: a\n * {@link import('../ConversationManager.js').ConversationManager} reads a snapshot back and rebuilds\n * the live conversation through the `snapshot` option (its `open` / `save`).\n *\n * @example\n * ```ts\n * import { createConversation, createDatabaseConversationStore } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseConversationStore(createMemoryDriver()) // a durable driver swaps in here\n * const conversation = createConversation()\n * conversation.add({ role: 'user', content: 'hello' })\n * await store.set(conversation.snapshot()) // persist the conversation (one JSON column)\n * const snapshot = await store.get(conversation.id)\n * await store.delete(conversation.id) // drop it\n * ```\n */\nexport class DatabaseConversationStore implements ConversationStoreInterface {\n\treadonly #table: TableInterface<ConversationSnapshotRow>\n\n\t/**\n\t * Wraps a table as a conversation store.\n\t *\n\t * @param table - The {@link TableInterface} holding the snapshots — its row is the\n\t * {@link ConversationSnapshotRow} `{ id; snapshot }` shape (the snapshot one opaque JSON column)\n\t */\n\tconstructor(table: TableInterface<ConversationSnapshotRow>) {\n\t\tthis.#table = table\n\t}\n\n\t/**\n\t * Resolves the persisted snapshot for `id`, narrowing the opaque JSON column back to a\n\t * `ConversationSnapshot`.\n\t *\n\t * @remarks\n\t * The column is an untrusted storage read, so a blob that fails\n\t * {@link import('../validators.js').isConversationSnapshot}, or whose own `id` is not `id`, throws\n\t * rather than reading as absent, which would let a later `set` overwrite it.\n\t *\n\t * @param id - The conversation id to resolve (the row key)\n\t * @returns The persisted snapshot, or `undefined` when no row is stored\n\t * @throws {ConversationError} Thrown when the row's blob is malformed or names another id (code `'SNAPSHOT'`), with the faults under `context.faults`\n\t */\n\tasync get(id: string): Promise<ConversationSnapshot | undefined> {\n\t\tconst row = await this.#table.get(id)\n\t\tif (row === undefined) return undefined\n\t\t// The snapshot crosses back as an untrusted storage read (a structured clone / a JSON row),\n\t\t// so narrow the opaque JSON column with the total boundary guard rather than a cast;\n\t\t// `audit` runs only after the guard refused, to fill the faults.\n\t\tconst snapshot = row.snapshot\n\t\tif (isConversationSnapshot(snapshot)) {\n\t\t\tif (snapshot.id === id) return snapshot\n\t\t\tconst faults: readonly AuditFault[] = [\n\t\t\t\t{\n\t\t\t\t\treason: 'type',\n\t\t\t\t\tpath: ['id'],\n\t\t\t\t\texpected: 'literal',\n\t\t\t\t\treceived: preview(snapshot.id),\n\t\t\t\t},\n\t\t\t]\n\t\t\tthrow new ConversationError('SNAPSHOT', describeSnapshotRefusal('conversation', id, faults), {\n\t\t\t\tcontext: { id, faults },\n\t\t\t})\n\t\t}\n\t\tconst faults = conversationSnapshotContract.audit(snapshot)\n\t\tthrow new ConversationError('SNAPSHOT', describeSnapshotRefusal('conversation', id, faults), {\n\t\t\tcontext: { id, faults },\n\t\t})\n\t}\n\n\t/**\n\t * Inserts or replaces under the snapshot's own `id` (no separate id param) — the row is\n\t * `{ id, snapshot }`.\n\t *\n\t * @param snapshot - The snapshot to store, keyed by its `id`\n\t * @returns A promise that resolves after the row is written\n\t * @throws {ConversationError} Thrown before any write when the snapshot fails the guard `get` reads (code `'SNAPSHOT'`), so no row is written that `get` refuses\n\t */\n\tasync set(snapshot: ConversationSnapshot): Promise<void> {\n\t\tif (!isConversationSnapshot(snapshot)) {\n\t\t\tconst unchecked: unknown = snapshot\n\t\t\tconst faults = conversationSnapshotContract.audit(unchecked)\n\t\t\tthrow new ConversationError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`a conversation store writes only what snapshot() returned\\n${describeFaults(faults, conversationSnapshotContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tawait this.#table.set({ id: snapshot.id, snapshot })\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op (no throw).\n\t *\n\t * @param id - The conversation id to drop\n\t * @returns A promise that resolves after the row is gone\n\t */\n\tasync delete(id: string): Promise<void> {\n\t\tawait this.#table.remove(id)\n\t}\n}\n","import type {\n\tConversationInterface,\n\tConversationManagerInterface,\n\tConversationManagerOptions,\n\tConversationOptions,\n\tConversationSnapshotRow,\n\tConversationStoreInterface,\n} from './types.js'\nimport type { DriverInterface, TableInterface } from '@orkestrel/database'\nimport { rawShape, stringShape } from '@orkestrel/contract'\nimport { createDatabase, createMemoryDriver } from '@orkestrel/database'\nimport { Conversation } from './Conversation.js'\nimport { ConversationManager } from './ConversationManager.js'\nimport { MemoryConversationStore } from './stores/MemoryConversationStore.js'\nimport { DatabaseConversationStore } from './stores/DatabaseConversationStore.js'\n\n/**\n * Creates a conversation — a {@link ConversationInterface} grouping messages above a flat\n * message store it owns directly, with compaction into summarized sections, on-demand\n * `rehydrate`, and substring `search`, driven by a provider-agnostic\n * {@link ConversationSummaryHandler} seam.\n *\n * @remarks\n * Append turns through the conversation's own `add` (the live tail it owns); `view()` is the model input\n * (each section as a summary message, then the live tail). `compact()` folds the older live\n * messages into a summarized {@link Section}, whole exchanges at a time — it requires a\n * `summarize` (without one `compact()` throws a `ConversationError`); `keep` retains a recent\n * tail (Default: `DEFAULT_CONVERSATION_KEEP`, which folds up to the newest user message).\n * `rehydrate(id)` / `search(query)` read the retained originals. Observable (`emitter` —\n * `compact` / `collapse` / `rehydrate`), wired\n * through the reserved `on` option; the emitter isolates a listener throw and routes it to\n * its `error` handler (the `error` option), so it can never corrupt a compaction.\n *\n * @param options - Optional `id` / `on` hooks + the `summarize` seam + `keep` + `sections` (see {@link ConversationOptions})\n * @returns A working {@link ConversationInterface}\n *\n * @example Conversations & compaction\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createConversation } from '@orkestrel/agent'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * // The summarizer seam — built from the provider by the runtime; core stays provider-agnostic.\n * // Append the instruction as the FINAL user turn: a chat model emits nothing when the prompt\n * // ends on an assistant turn, so a leading-system instruction is unreliable.\n * const conversation = createConversation({\n * \tsummarize: async (messages, signal) =>\n * \t\t(\n * \t\t\tawait provider.generate(\n * \t\t\t\t[\n * \t\t\t\t\t...messages,\n * \t\t\t\t\t{ id: 's', role: 'user', content: 'Summarize the conversation so far concisely.' },\n * \t\t\t\t],\n * \t\t\t\tsignal ?? AbortSignal.timeout(30_000), // the compact() call's signal, when it passed one\n * \t\t\t)\n * \t\t).content,\n * \tkeep: 2, // retain at least the 2 most recent messages verbatim on each compaction\n * })\n * conversation.add([\n * \t{ role: 'user', content: 'My name is Ada.' },\n * \t{ role: 'assistant', content: 'Nice to meet you, Ada.' },\n * \t{ role: 'user', content: 'Book a table for two at 19:00.' },\n * \t{ role: 'assistant', content: 'Booked for two at 19:00.' },\n * \t{ role: 'user', content: 'What did I say my name was?' },\n * ])\n *\n * const section = await conversation.compact() // folds the first exchange → a summarized section\n * conversation.view() // [<section summary message>, ...the retained recent exchanges] — the model input\n * conversation.search('ada') // case-insensitive across sections' originals + the live tail\n * section && conversation.rehydrate(section.id) // the section's full original messages (a pure read)\n * ```\n */\nexport function createConversation(options?: ConversationOptions): ConversationInterface {\n\treturn new Conversation(options)\n}\n\n/**\n * Creates a conversation registry — a {@link ConversationManagerInterface} holding\n * {@link ConversationInterface} instances keyed by their `id`, in insertion order, with an active pointer:\n * the id-keyed store over the conversation layer plus the `active` / `switch` seam the context\n * renders. `add` auto-activates the first conversation and flows the registry's default\n * `summarize` / `keep` into every conversation it creates.\n *\n * @remarks\n * Starts empty; `add(input?)` mints a {@link ConversationInterface} (its `id` from the input\n * or a random UUID), flowing the manager's default `summarize` / `keep` in unless the input\n * overrides them, and stores it (an already-present `id` throws, code `'ID'`) — and\n * auto-activates the first one (a registry with conversations always has one `active`); a later\n * `add` leaves `active` unchanged. `switch(id)` re-points `active` (an unknown `id` returns\n * `undefined`, leaving `active` unchanged — lenient, never throws); `conversation(id)` /\n * `conversations()` look up; `remove` (one or a batch) reports `true` only when every supplied id\n * was removed and clears `active` if it was a removed one; `clear` empties it and clears `active`.\n * Event-free\n * (each conversation owns its own observable `emitter`). A conversation created with neither a\n * manager default nor a per-`add` `summarize` cannot `compact` (it throws a `ConversationError`).\n *\n * @param options - Optional default `summarize` / `keep` (see {@link ConversationManagerOptions})\n * @returns An empty {@link ConversationManagerInterface}\n *\n * @example\n * ```ts\n * import { createConversationManager } from '@orkestrel/agent'\n *\n * const conversations = createConversationManager({ summarize: async (m) => `recap of ${m.length}` })\n * const chat = conversations.add() // auto-activates — conversations.active === chat\n * chat.add({ role: 'user', content: 'Hello' })\n * ```\n */\nexport function createConversationManager(\n\toptions?: ConversationManagerOptions,\n): ConversationManagerInterface {\n\treturn new ConversationManager(options)\n}\n\n/**\n * Creates the in-memory conversation store — a {@link ConversationStoreInterface} backed by a\n * process-lifetime `Map` of {@link import('./types.js').ConversationSnapshot} records keyed by conversation\n * id, the default backing for the durable {@link ConversationManagerInterface.open} /\n * {@link ConversationManagerInterface.save} seam. The exact twin of\n * {@link import('@orkestrel/workspace').createMemoryWorkspaceStore}.\n *\n * @remarks\n * A plain `Map` (the snapshot is already pure JSON, so no encoding is needed for the memory tier),\n * the structural twin of {@link import('@orkestrel/workspace').createMemoryWorkspaceStore}.\n * `get` / `set` / `delete` are async (the\n * same shape a durable backend fits); unlike a session store there is no idle-TTL / eviction — a\n * persisted conversation lives until an explicit `delete`. Its driver-pluggable twin is\n * {@link createDatabaseConversationStore} (the snapshot as one opaque JSON column over a `databases`\n * table) — for a durable store pass it a JSON / SQLite / IndexedDB driver, and it swaps in without\n * touching the manager or the conversation. Hydration stays a manager concern: read a snapshot back\n * and rebuild the live conversation through the `snapshot` option (re-supplying the live\n * `summarize` / `keep`).\n *\n * @returns A memory-backed {@link ConversationStoreInterface}\n *\n * @example\n * ```ts\n * import { createConversationManager, createMemoryConversationStore } from '@orkestrel/agent'\n *\n * const store = createMemoryConversationStore()\n * const manager = createConversationManager({ store })\n * const conversation = manager.add()\n * conversation.add({ role: 'user', content: 'hello' })\n * await manager.save(conversation.id) // persist the conversation\n * ```\n */\nexport function createMemoryConversationStore(): ConversationStoreInterface {\n\treturn new MemoryConversationStore()\n}\n\n/**\n * Creates a {@link DatabaseConversationStore} over any {@link DriverInterface} — the durable,\n * driver-pluggable backing for the conversation persistence\n * seam, holding each snapshot as one opaque JSON column and standing as the opt-in twin of\n * {@link createMemoryConversationStore}. The exact twin of\n * {@link import('@orkestrel/workspace').createDatabaseWorkspaceStore}.\n *\n * @remarks\n * Builds a one-table database (`conversations`, keyed by `id`) over the supplied driver, the snapshot\n * held as one opaque JSON column — the column map is `{ id; snapshot }` where `snapshot` is a\n * `rawShape` (a JSON blob), exactly as\n * {@link import('@orkestrel/workspace').createDatabaseWorkspaceStore} stores its snapshot. The\n * snapshot is already a complete, self-contained, pure-JSON payload, so storing it whole is lossless\n * and keeps the row type flat (the column reads back as `unknown`, narrowed on `get` by\n * {@link import('./validators.js').isConversationSnapshot}). Default driver:\n * {@link createMemoryDriver}, so the store also works in memory out of the box; pass a server\n * `createJSONDriver` / `createSQLiteDriver` (or a browser IndexedDB driver) for a persistent one —\n * the durability is the driver's job, the store engine is shared. It swaps in behind\n * {@link ConversationStoreInterface} without touching the manager or the conversation.\n *\n * @param driver - The storage backend the snapshots persist to. Default: {@link createMemoryDriver}\n * @returns A {@link ConversationStoreInterface} over the driver\n *\n * @example\n * ```ts\n * import { createConversationManager, createDatabaseConversationStore } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseConversationStore(createMemoryDriver()) // a durable driver swaps in here\n * const manager = createConversationManager({ store })\n * const conversation = manager.add()\n * conversation.add({ role: 'user', content: 'hello' })\n * await manager.save(conversation.id) // persist the conversation (one JSON column)\n * ```\n */\nexport function createDatabaseConversationStore(\n\tdriver: DriverInterface = createMemoryDriver(),\n): ConversationStoreInterface {\n\t// The snapshot is stored as one opaque JSON column (`rawShape`), so the row infers flat —\n\t// `{ id: string; snapshot: unknown }` = `ConversationSnapshotRow` — and the sections/messages\n\t// snapshot shape never forces a contract `Infer`.\n\tconst columns = { id: stringShape(), snapshot: rawShape({}) }\n\tconst database = createDatabase({ driver, tables: { conversations: columns } })\n\tconst table: TableInterface<ConversationSnapshotRow> = database.table('conversations')\n\treturn new DatabaseConversationStore(table)\n}\n","/**\n * Names the section header the `build()` method of {@link import('./AgentContext.js').AgentContext}\n * renders the active workspace's text files under — `'## Workspace'`, the line that opens the\n * workspace section of the user context message that follows the system message.\n *\n * @remarks\n * `build()` owns the workspace render (a `Workspace` / `WorkspaceManager` stays file-focused — no\n * `open` / `format` getters), so this header lives here as the contexts module's one\n * workspace-section framing constant rather than on a manager. Each workspace text file renders\n * beneath it as a fenced `` File: <path>\\n```<language>\\n<text>\\n``` `` block through\n * {@link import('./helpers.js').renderFencedFile}, and the section follows any memos index in the\n * context message, and is never part of the system message. The workspace's image files render no\n * section: their `base64` payload attaches to the last user message.\n */\nexport const WORKSPACE_SECTION_HEADER = '## Workspace'\n\n/**\n * Names the built-in section header a {@link import('./types.js').MemoManagerInterface} renders\n * its index under — `'## Memos'`, the `open` value when no `format.open` overrides it.\n *\n * @remarks\n * Each memo renders beneath it as one `- name: description` line through\n * {@link import('./helpers.js').renderIndexEntry}.\n */\nexport const MEMO_SECTION_HEADER = '## Memos'\n\n/**\n * Names the built-in section header a {@link import('./types.js').SkillManagerInterface} renders\n * its index under — `'## Skills'`, the `open` value when no `format.open` overrides it.\n *\n * @remarks\n * Each skill renders beneath it as one `- name: description` line through\n * {@link import('./helpers.js').renderIndexEntry}.\n */\nexport const SKILL_SECTION_HEADER = '## Skills'\n","import type { ContextErrorCode } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport { INTRINSICS, holds, isObject, isFunction } from '@orkestrel/contract'\n\n/**\n * Reports a context snapshot that `isMemoSnapshot` refuses, thrown by the construction of a\n * {@link import('./memos/MemoManager.js').MemoManager} before it seats any state or by a memo store,\n * or image files a build has no user turn to carry, carrying the machine-readable `code`\n * `'SNAPSHOT' | 'IMAGES'`.\n *\n * @remarks\n * A `snapshot` option crosses a foreign boundary (a client, a store, a file), so the manager copies\n * it through JSON and checks the copy: a value JSON cannot carry, a revision missing a field, a\n * non-finite `time`, a sparse `memos` array, a non-string `id`, or a non-array `memos` is a\n * programmer error and throws this with `'SNAPSHOT'`, its `context` holding the `faults`. A build\n * over a conversation that holds turns and no user turn, with image files in the active workspace,\n * throws `'IMAGES'` rather than dropping them. Narrow a caught value with {@link isContextError} and\n * branch on `error.code`.\n */\nexport class ContextError extends Error {\n\t/** Names the machine-readable condition — `'SNAPSHOT'`: a snapshot that is not a memo snapshot; `'IMAGES'`: image files with no user turn to carry them. */\n\treadonly code: ContextErrorCode\n\t/** Holds the values that locate the failure, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Creates the error with its code, message, and optional cause and context.\n\t *\n\t * @param code - The machine-readable {@link ContextErrorCode}\n\t * @param message - The human-readable description\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: ContextErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'ContextError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ContextError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link ContextError} through its identity brand and declared members, so a\n * `catch` can branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link ContextError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tnew MemoManager({ snapshot })\n * } catch (error) {\n * \tif (isContextError(error) && error.code === 'SNAPSHOT') discard(snapshot)\n * }\n * ```\n */\nexport function isContextError(value: unknown): value is ContextError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ContextError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (code !== 'SNAPSHOT' && code !== 'IMAGES') return false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { InstructionInput, InstructionInterface } from '../types.js'\n\n/**\n * Represents an immutable named directive — an {@link InstructionInterface} assembled once from\n * its input (`name` / `content` and an optional `priority`), the `id` minted at\n * construction. Default: `0` for `priority`.\n *\n * @remarks\n * A thin immutable value object (mirroring {@link import('@orkestrel/tool').Tool}): the\n * constructor mints a fresh `id` (`crypto.randomUUID()`), copies the input's `name` /\n * `content`, resolves `priority` to the input's value or `0`, and carries the input's\n * per-item `override` value when supplied; otherwise that property is `undefined`.\n * Never mutated after construction. An\n * {@link import('./InstructionManager.js').InstructionManager} keys it by `name` and\n * renders it (highest `priority` first) under its section header.\n *\n * @example\n * ```ts\n * const instruction = new Instruction({ name: 'tone', content: 'Be concise.', priority: 5 })\n * instruction.priority // 5\n * ```\n */\nexport class Instruction implements InstructionInterface {\n\treadonly id: string\n\treadonly name: string\n\treadonly content: string\n\treadonly priority: number\n\treadonly override?: string\n\n\tconstructor(input: InstructionInput) {\n\t\tthis.id = crypto.randomUUID()\n\t\tthis.name = input.name\n\t\tthis.content = input.content\n\t\tthis.priority = input.priority ?? 0\n\t\tif (input.override !== undefined) this.override = input.override\n\t}\n}\n","import type {\n\tContextSectionFormat,\n\tInstructionInput,\n\tInstructionInterface,\n\tInstructionManagerEventMap,\n\tInstructionManagerInterface,\n\tInstructionManagerOptions,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { removeEntries } from '../../helpers.js'\nimport { Instruction } from './Instruction.js'\n\n/**\n * Registers the immutable {@link Instruction} instances a richer context assembles a directives block\n * from — keyed by `name` so adding an instruction whose name is already stored overwrites it,\n * last write wins, and listed by descending `priority`, carrying the `open` / `render` / `close` build contract and an observable `emitter`.\n *\n * @remarks\n * - **Registry.** Instructions live in an insertion-ordered `Map` keyed by `name`;\n * `add` takes one {@link InstructionInput} or a batch, mints each instruction's\n * `id`, and adding an instruction whose name is already stored overwrites it (last write wins).\n * `count` is the map size, `instruction(name)` looks one up, and `instructions()` lists them sorted by\n * descending `priority` (a stable sort, so equal priorities keep insertion order).\n * - **Build contract (the whole format cascade).** `open` is the section header a context\n * renders the instructions under, `render(instruction)` renders one instruction, and\n * `close` is the line after them. Each resolves the cascade most-specific-first: `render`\n * returns the instruction's {@link InstructionInput.override}, else the\n * `InstructionManagerOptions.format` `render`, else its `content`; `open` returns the\n * options `open`, else the built-in header; `close` returns the options `close`, else\n * `undefined`. A context reads the three and frames the section from them (see\n * {@link import('../AgentContext.js').AgentContext}).\n * - **Removal.** `remove` drops one by name, or a batch — `true` only when every supplied\n * name was removed; `clear` empties the registry.\n * - **Observable.** The owned {@link emitter} ({@link InstructionManagerEventMap})\n * carries `add` (the created instruction) / `remove` (the name) / `clear` for\n * fire-and-forget observers. Every event is emitted directly, strictly after the map\n * mutation completes; the emitter isolates a listener throw and routes it to its `error`\n * handler (the `error` option), so a buggy observer can never corrupt a mutation.\n *\n * @example\n * ```ts\n * const manager = new InstructionManager()\n * manager.add([\n * \t{ name: 'tone', content: 'Be concise.', priority: 1 },\n * \t{ name: 'safety', content: 'Refuse unsafe requests.', priority: 10 },\n * ])\n * manager.instructions().map((one) => one.name) // ['safety', 'tone'] — highest priority first\n * ```\n */\nexport class InstructionManager implements InstructionManagerInterface {\n\treadonly #instructions = new Map<string, InstructionInterface>()\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<InstructionManagerEventMap>\n\treadonly #format: ContextSectionFormat<InstructionInterface> | undefined\n\n\tconstructor(options?: InstructionManagerOptions) {\n\t\tthis.#emitter = new Emitter<InstructionManagerEventMap>(options)\n\t\tthis.#format = options?.format\n\t}\n\n\tget emitter(): EmitterInterface<InstructionManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn this.#instructions.size\n\t}\n\n\tget open(): string {\n\t\treturn this.#format?.open ?? '## Instructions'\n\t}\n\n\tget close(): string | undefined {\n\t\treturn this.#format?.close\n\t}\n\n\tadd(input: InstructionInput): InstructionInterface\n\tadd(inputs: readonly InstructionInput[]): readonly InstructionInterface[]\n\tadd(\n\t\tinput: InstructionInput | readonly InstructionInput[],\n\t): InstructionInterface | readonly InstructionInterface[] {\n\t\tif (isArray(input)) return input.map((one) => this.#create(one))\n\t\treturn this.#create(input)\n\t}\n\n\tinstruction(name: string): InstructionInterface | undefined {\n\t\treturn this.#instructions.get(name)\n\t}\n\n\tinstructions(): readonly InstructionInterface[] {\n\t\t// A stable descending-priority sort — Array.prototype.sort is stable, so equal\n\t\t// priorities keep their insertion order (the map's iteration order).\n\t\treturn [...this.#instructions.values()].sort((a, b) => b.priority - a.priority)\n\t}\n\n\trender(instruction: InstructionInterface): string {\n\t\treturn instruction.override ?? this.#format?.render?.(instruction) ?? instruction.content\n\t}\n\n\tremove(name: string): boolean\n\tremove(names: readonly string[]): boolean\n\tremove(names: string | readonly string[]): boolean {\n\t\tif (isArray(names)) {\n\t\t\t// Each present name still emits.\n\t\t\treturn removeEntries(names, (name) => this.#delete(name))\n\t\t}\n\t\treturn this.#delete(names)\n\t}\n\n\tclear(): void {\n\t\tthis.#instructions.clear()\n\t\t// Observe the cleared registry — after the map emptied, so a swallowed listener\n\t\t// throw can never alter the clear (no payload — `clear` is a pure signal).\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\t// Mint an immutable instruction, store it by name (overwriting a same-name one), and\n\t// emit `add` after the map mutation — so a swallowed listener throw can't perturb it.\n\t#create(input: InstructionInput): InstructionInterface {\n\t\tconst instruction = new Instruction(input)\n\t\tthis.#instructions.set(instruction.name, instruction)\n\t\tthis.#emitter.emit('add', instruction)\n\t\treturn instruction\n\t}\n\n\t// Delete one instruction, emitting `remove` only when one was actually removed (a\n\t// delete of an absent name returns `false` and emits nothing) — after the deletion.\n\t#delete(name: string): boolean {\n\t\tconst removed = this.#instructions.delete(name)\n\t\tif (removed) this.#emitter.emit('remove', name)\n\t\treturn removed\n\t}\n}\n","import type { Memo } from './types.js'\nimport type { Message } from '../types.js'\nimport type { FileInterface } from '@orkestrel/workspace'\nimport { isBinary } from '@orkestrel/workspace'\nimport { ContextError } from './errors.js'\n\n/**\n * Renders a path-addressed text body as a fenced reference block — a `File: <path>` label line\n * over a language-tagged fence, the framing the\n * active-workspace text-file render of {@link import('./AgentContext.js').AgentContext} emits.\n *\n * @remarks\n * Produces `` File: <path>\\n```<language>\\n<content>\\n``` `` — the `File:` label line, then a\n * fenced code block tagged with `language`, the `content` verbatim inside. The fence is one\n * backtick longer than the longest backtick run in `content`, and never shorter than three (the\n * CommonMark rule), so file text holding a triple backtick cannot close the block early and\n * land outside it; content with no run of three or more keeps the three-backtick fence. Pure\n * string assembly, total — never throws. The one fenced-file format string for the whole\n * module — `AgentContext.build()` frames an active workspace's text files with it (each carries\n * its own `language` on its {@link import('@orkestrel/workspace').FileContent} text arm).\n *\n * @param path - The file path shown on the `File:` label line\n * @param language - The fenced-code language tag (for example `'typescript'`)\n * @param content - The file body rendered verbatim inside the fence\n * @returns The fenced reference block\n *\n * @example\n * ```ts\n * import { renderFencedFile } from '@orkestrel/agent'\n *\n * renderFencedFile('src/main.ts', 'typescript', 'const x = 1')\n * // 'File: src/main.ts\\n```typescript\\nconst x = 1\\n```'\n * ```\n */\nexport function renderFencedFile(path: string, language: string, content: string): string {\n\tconst runs = content.match(/`+/g) ?? []\n\tconst longest = runs.reduce((most, run) => Math.max(most, run.length), 0)\n\tconst fence = '`'.repeat(Math.max(3, longest + 1))\n\treturn `File: ${path}\\n${fence}${language}\\n${content}\\n${fence}`\n}\n\n/**\n * Renders one context section — the resolved `open`, each item's rendering, and the resolved\n * `close` when one exists, blank-line joined; `undefined` when the section has no items.\n *\n * @remarks\n * Pure and total. A section with no items renders nothing (`undefined`), so an empty or fully\n * scoped-out manager stays silent — its `open` / `close` appear only when the section has items.\n * An empty or absent slot is dropped before the join, so an `open: ''`, a `close: ''`, an unset\n * `close` (there is no built-in close), and an item rendered to `''` each add no blank line; a\n * section whose every slot is empty renders nothing (`undefined`).\n *\n * @typeParam T - The section item being rendered\n * @param open - The section's resolved leading text\n * @param members - The already scope-filtered items\n * @param render - Renders one item to its prompt text\n * @param close - The section's resolved trailing text, or `undefined` for none\n * @returns The rendered section, or `undefined` when there are no items or every slot is empty\n *\n * @example\n * ```ts\n * renderSection('## Instructions', [{ content: 'Be terse.' }], (one) => one.content, undefined)\n * // '## Instructions\\n\\nBe terse.'\n * renderSection('<rules>', [], (one) => one.content, '</rules>') // undefined (no items)\n * ```\n */\nexport function renderSection<T>(\n\topen: string,\n\tmembers: readonly T[],\n\trender: (member: T) => string,\n\tclose: string | undefined,\n): string | undefined {\n\tif (members.length === 0) return undefined\n\tconst lines = [open, ...members.map(render), close].flatMap((line) =>\n\t\tline === undefined || line === '' ? [] : [line],\n\t)\n\treturn lines.length === 0 ? undefined : lines.join('\\n\\n')\n}\n\n/**\n * Copies a message with image data merged onto its `images` — the message's own images first,\n * then the attached data, keeping every other field and never mutating the original.\n *\n * @remarks\n * Pure and total: the original message is never mutated. Every other field the source message\n * carries (`calls`, `call`, `thinking`) is kept as given, and an absent one stays absent,\n * mirroring the store's present-when-given convention.\n *\n * @param message - The message to copy (left unchanged)\n * @param payloads - The base64 image data to attach\n * @returns A new message carrying the merged `images`\n *\n * @example\n * ```ts\n * // IMAGE_BASE64 stands for the base64 text of one image.\n * attachImages({ id: '1', role: 'user', content: 'Describe' }, ['IMAGE_BASE64'])\n * // { id: '1', role: 'user', content: 'Describe', images: ['IMAGE_BASE64'] }\n * ```\n */\nexport function attachImages(message: Message, payloads: readonly string[]): Message {\n\treturn { ...message, images: [...(message.images ?? []), ...payloads] }\n}\n\n/**\n * Attaches image data to a conversation's last user message — the turn a vision provider reads\n * images off — as a new array with that one message replaced by its carrying copy, and unchanged\n * when there is no data or no user turn.\n *\n * @remarks\n * Pure and total: the conversation and its messages are never mutated, and the returned array\n * replaces exactly the one target message with the copy {@link attachImages} builds. Empty\n * data returns the conversation unchanged. Data with no user message to carry it refuses, an empty\n * conversation included, because leaving the images out would run without them. A hole in a sparse\n * conversation is not a user message.\n *\n * @param conversation - The messages to attach into (left unchanged)\n * @param payloads - The base64 image data to attach\n * @returns The conversation with its last user message replaced by the carrying copy\n * @throws ContextError Thrown when there is data and no user message to carry it (code `'IMAGES'`)\n *\n * @example\n * ```ts\n * attachUserImages([{ id: '1', role: 'user', content: 'Describe' }], ['IMAGE_BASE64'])\n * // [{ id: '1', role: 'user', content: 'Describe', images: ['IMAGE_BASE64'] }]\n * ```\n */\nexport function attachUserImages(\n\tconversation: readonly Message[],\n\tpayloads: readonly string[],\n): readonly Message[] {\n\tif (payloads.length === 0) return conversation\n\tconst target = conversation.findLastIndex(\n\t\t(message: Message | undefined) => message?.role === 'user',\n\t)\n\tif (target === -1) {\n\t\tthrow new ContextError(\n\t\t\t'IMAGES',\n\t\t\t`the active workspace holds ${payloads.length} image file(s) and the conversation holds no user turn to carry them; add a user message or scope the files out`,\n\t\t)\n\t}\n\treturn conversation.map((message, index) =>\n\t\tindex === target ? attachImages(message, payloads) : message,\n\t)\n}\n\n/**\n * Collects the `base64` payload of the image files in a workspace file list — the data an agent\n * context attaches to the last user message.\n *\n * @remarks\n * Pure and total. `isBinary` narrows the tagless content to its binary arm (a total guard,\n * never an assertion), then the MIME prefix gates it to an image, so a text file and a non-image\n * binary (a PDF) are both skipped. Order follows the file list.\n *\n * @param files - The (already scope-filtered) workspace files\n * @returns The `base64` payload of each image file, in file order\n *\n * @example\n * ```ts\n * collectImageData([createFile({ path: 'a.png', content: { base64: 'IMAGE_BASE64', mime: 'image/png' } })])\n * // ['IMAGE_BASE64']\n * ```\n */\nexport function collectImageData(files: readonly FileInterface[]): readonly string[] {\n\t// The spread visits a hole in `files` as an undefined file, as iteration does; `flatMap` skips it.\n\treturn [...files].flatMap((file) =>\n\t\tisBinary(file.content) && file.content.mime.startsWith('image/') ? [file.content.base64] : [],\n\t)\n}\n\n/**\n * Intersects two scope category lists under the \"`undefined` is the universal set\" rule — a\n * fresh copy that can only tighten, and the primitive a scope narrows through.\n *\n * @remarks\n * Pure and total, and it can only tighten: `undefined` ∩ `undefined` is `undefined` (still no\n * constraint); `undefined` ∩ a list is a copy of that list (the `undefined` side imposes\n * nothing); a list ∩ a list keeps the child keys the parent also allows, so a parent-excluded\n * key can never be re-admitted. Every returned list is a fresh copy, so a later mutation of\n * either input cannot leak into the result.\n *\n * @param parent - The parent's allow-list (`undefined` ⇒ no constraint)\n * @param child - The narrowing allow-list (`undefined` ⇒ no constraint)\n * @returns The intersected allow-list, or `undefined` when neither side constrains\n *\n * @example\n * ```ts\n * intersectKeys(['read', 'write'], ['write', 'admin']) // ['write']\n * intersectKeys(undefined, ['read']) // ['read']\n * intersectKeys(undefined, undefined) // undefined\n * ```\n */\nexport function intersectKeys(\n\tparent: readonly string[] | undefined,\n\tchild: readonly string[] | undefined,\n): readonly string[] | undefined {\n\tif (parent === undefined) return child === undefined ? undefined : [...child]\n\tif (child === undefined) return [...parent]\n\tconst allowed = new Set(parent)\n\treturn child.filter((key) => allowed.has(key))\n}\n\n/**\n * Renders one index line for a memo or a skill — `- name: description`, the built-in `render` of\n * the memo and skill managers.\n *\n * @remarks\n * Pure and total. A run of line-break code points in the name or the description renders as one\n * space, so the entry is one line.\n *\n * @param entry - The memo or skill whose `name` and `description` the line shows\n * @returns The index line\n *\n * @example\n * ```ts\n * import { renderIndexEntry } from '@orkestrel/agent'\n *\n * renderIndexEntry({ name: 'tone', description: 'Prefers short replies.' })\n * // '- tone: Prefers short replies.'\n * ```\n */\nexport function renderIndexEntry(entry: Pick<Memo, 'name' | 'description'>): string {\n\treturn `- ${entry.name}: ${entry.description}`.replace(/[\\n\\v\\f\\r\\u0085\\u2028\\u2029]+/g, ' ')\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\tstringShape,\n} from '@orkestrel/contract'\n\n/** Describes a stored memo revision exactly. */\nexport const memoShape = objectShape({\n\tid: stringShape(),\n\tname: stringShape(),\n\tdescription: stringShape(),\n\tcontent: stringShape(),\n\ttime: numberShape(),\n\torigin: optionalShape(\n\t\tobjectShape({\n\t\t\tconversation: stringShape(),\n\t\t\tmessage: optionalShape(stringShape()),\n\t\t}),\n\t),\n}) satisfies ContractShape\n\n/**\n * Describes a stored memo set exactly: an unknown key refuses.\n *\n * @remarks\n * `isMemoSnapshot` is the door a hydrate and a store read, and this shape fills a refusal's faults. A\n * lockstep test holds that `is` implies the guard, and on the snapshot fixtures the converse.\n */\nexport const memoSnapshotShape = objectShape({\n\tid: stringShape(),\n\tmemos: arrayShape(memoShape),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { memoShape, memoSnapshotShape } from './shapers.js'\n\n/** Reads the faults of a refused memo revision. */\nexport const memoContract = createContract(memoShape)\n\n/** Reads the faults of a refused memo snapshot. */\nexport const memoSnapshotContract = createContract(memoSnapshotShape)\n","import type { Memo, MemoOrigin, MemoSnapshot } from './types.js'\nimport { arrayOf, holds, isFiniteNumber, isRecord, isString } from '@orkestrel/contract'\n\n/**\n * Checks whether a value is a memo's origin: a record with a string `conversation` and an optional\n * string `message`.\n *\n * @remarks\n * The guard is exact: an unknown member and an own `undefined` `message` return false. Malformed and\n * unreadable inputs return false.\n *\n * @param value - The value to test\n * @returns True if `value` has the shape of a {@link MemoOrigin}; false otherwise\n * @example\n * ```ts\n * isMemoOrigin({ conversation: 'c1', message: 'm1' }) // true\n * isMemoOrigin({ conversation: 'c1', extra: true }) // false\n * ```\n */\nexport function isMemoOrigin(value: unknown): value is MemoOrigin {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { conversation, message } = value\n\t\treturn (\n\t\t\tisString(conversation) &&\n\t\t\t(message === undefined || isString(message)) &&\n\t\t\tObject.keys(value).length === (message === undefined ? 1 : 2)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a stored memo revision: a record with a string `id`, `name`,\n * `description`, and `content`, a finite `time`, and an optional `origin`.\n *\n * @remarks\n * An `origin`, when present, is a record with a string `conversation` and an optional string\n * `message`. The guard is exact: an unknown member and an own `undefined` optional return false. The\n * `time` must be finite, because a stored revision is owned through JSON, which turns `NaN` and the\n * infinities into `null`. Malformed and unreadable inputs, a throwing getter or a revoked proxy\n * included, return false. Every revision of a hydrate reads this guard, so it counts keys by hand\n * rather than through a built one, and `memoSnapshotContract` holds it in lockstep.\n *\n * @param value - The value to test\n * @returns True if `value` has the shape of a {@link Memo}; false otherwise\n *\n * @example\n * ```ts\n * import { isMemo } from '@orkestrel/agent'\n *\n * isMemo({ id: 'm1', name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.', time: 0 }) // true\n * isMemo({ id: 'm1', name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.', time: Number.NaN }) // false\n * ```\n */\nexport function isMemo(value: unknown): value is Memo {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { id, name, description, content, time, origin } = value\n\t\treturn (\n\t\t\tisString(id) &&\n\t\t\tisString(name) &&\n\t\t\tisString(description) &&\n\t\t\tisString(content) &&\n\t\t\tisFiniteNumber(time) &&\n\t\t\t(origin === undefined || isMemoOrigin(origin)) &&\n\t\t\tObject.keys(value).length === (origin === undefined ? 5 : 6)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a stored memo set: a record with a string `id` and a dense `memos`\n * array of {@link isMemo} values.\n *\n * @remarks\n * The guard is exact: an unknown member returns false, as a sparse `memos` array, a malformed\n * revision, and unreadable inputs, a throwing getter or a revoked proxy included, do.\n *\n * @param value - The value to test (an opaque storage read)\n * @returns True if `value` has the shape of a {@link MemoSnapshot}; false otherwise\n *\n * @example\n * ```ts\n * import { isMemoSnapshot } from '@orkestrel/agent'\n *\n * isMemoSnapshot({ id: 'ada', memos: [] }) // true\n * isMemoSnapshot({ id: 'ada', memos: [{ name: 'tone' }] }) // false\n * ```\n */\nexport function isMemoSnapshot(value: unknown): value is MemoSnapshot {\n\treturn holds(() => {\n\t\tif (!isRecord(value) || Object.keys(value).length !== 2) return false\n\t\tconst { id, memos } = value\n\t\treturn isString(id) && arrayOf(isMemo)(memos)\n\t})\n}\n","import type {\n\tContextSectionFormat,\n\tMemo,\n\tMemoInput,\n\tMemoManagerEventMap,\n\tMemoManagerInterface,\n\tMemoManagerOptions,\n\tMemoSnapshot,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { describeFaults, isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { copyJSON } from '../../cloners.js'\nimport { removeEntries } from '../../helpers.js'\nimport { MEMO_SECTION_HEADER } from '../constants.js'\nimport { ContextError } from '../errors.js'\nimport { renderIndexEntry } from '../helpers.js'\nimport { memoSnapshotContract } from '../contracts.js'\nimport { isMemoSnapshot } from '../validators.js'\n\n/**\n * Registers the revisions of a memo set — durable facts keyed by `name`, where a same-name `add`\n * with changed text appends a revision and the earlier ones stay readable — carrying the `open` /\n * `render` / `close` build contract and an observable `emitter`.\n *\n * @remarks\n * - **Revisions.** Every revision lives in one append-ordered list, so the current revisions,\n * each name's history, and the snapshot order all derive from it. `add` appends a revision with\n * a minted `id` and `time` unless the name's current revision holds the same `description` and\n * `content`; then it returns that revision and emits nothing. `memo(name)` is the name's last\n * revision, `memos()` lists each name's last revision by its first revision, and `count` is the\n * number of names.\n * - **Removal.** `remove` drops every revision of a name, so a re-added name starts a fresh\n * history at the end of `memos()`; `clear` drops every revision.\n * - **Identity.** `id` is the `snapshot.id`, else the `id` option, else a minted UUID. A\n * `snapshot` option is copied through JSON and checked with `isMemoSnapshot`, then seeds the\n * revisions silently; a malformed one throws a `ContextError` with code `'SNAPSHOT'`. `snapshot()`\n * returns owned copies of the revisions, oldest first.\n * - **Build contract.** `open` returns the options `open`, else `'## Memos'`; `render(memo)`\n * returns the options `render`, else `- name: description`; `close` returns the options\n * `close`, else `undefined`.\n * - **Observable.** The owned {@link emitter} ({@link MemoManagerEventMap}) carries `add` (the\n * appended revision) / `remove` (the name) / `clear`, each emitted after the mutation completes;\n * the emitter isolates a listener throw and routes it to its `error` handler.\n *\n * @example\n * ```ts\n * const memos = new MemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * memos.add({ name: 'tone', description: 'Prefers bullet lists.', content: 'Answer in bullets.' })\n * memos.history('tone').length // 2\n * memos.memo('tone')?.description // 'Prefers bullet lists.'\n * ```\n */\nexport class MemoManager implements MemoManagerInterface {\n\treadonly #id: string\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<MemoManagerEventMap>\n\treadonly #format: ContextSectionFormat<Memo> | undefined\n\t// Every revision, oldest first. Replaced on every change rather than mutated in place.\n\t#revisions: readonly Memo[]\n\n\t/**\n\t * Creates the memo set, owning a supplied `snapshot` through a JSON copy.\n\t *\n\t * @param options - The optional {@link MemoManagerOptions}\n\t * @throws {ContextError} Thrown when the `snapshot` option cannot be copied through JSON or its copy is not a memo snapshot (code `'SNAPSHOT'`); nothing is seated and nothing is emitted\n\t */\n\tconstructor(options?: MemoManagerOptions) {\n\t\t// A snapshot is a foreign value: own a JSON copy first so the caller's objects never alias the\n\t\t// state, then refuse a malformed one, or one JSON cannot copy, before anything is seated.\n\t\tconst supplied = options?.snapshot\n\t\tconst owned = supplied === undefined ? undefined : copyJSON(supplied)\n\t\tconst snapshot = isMemoSnapshot(owned) ? owned : undefined\n\t\tif (supplied !== undefined && snapshot === undefined) {\n\t\t\tconst faults = memoSnapshotContract.audit(owned)\n\t\t\tthrow new ContextError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`context error: memo snapshot is malformed; pass what snapshot() returned\\n${describeFaults(faults, memoSnapshotContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\t// Restoring is silent: nothing was edited, so no event fires.\n\t\tthis.#id = snapshot?.id ?? options?.id ?? crypto.randomUUID()\n\t\tthis.#emitter = new Emitter<MemoManagerEventMap>(options)\n\t\tthis.#format = options?.format\n\t\tthis.#revisions = snapshot === undefined ? [] : snapshot.memos\n\t}\n\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\tget emitter(): EmitterInterface<MemoManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn new Set(this.#revisions.map((memo) => memo.name)).size\n\t}\n\n\tget open(): string {\n\t\treturn this.#format?.open ?? MEMO_SECTION_HEADER\n\t}\n\n\tget close(): string | undefined {\n\t\treturn this.#format?.close\n\t}\n\n\tadd(input: MemoInput): Memo\n\tadd(inputs: readonly MemoInput[]): readonly Memo[]\n\tadd(input: MemoInput | readonly MemoInput[]): Memo | readonly Memo[] {\n\t\tif (isArray(input)) return input.map((one) => this.#append(one))\n\t\treturn this.#append(input)\n\t}\n\n\tmemo(name: string): Memo | undefined {\n\t\treturn this.#revisions.findLast((memo) => memo.name === name)\n\t}\n\n\tmemos(): readonly Memo[] {\n\t\t// A Map keeps a key's first insertion position while each later set replaces its value, so\n\t\t// every name lands at its first revision holding its last one.\n\t\treturn [...new Map(this.#revisions.map((memo) => [memo.name, memo])).values()]\n\t}\n\n\thistory(name: string): readonly Memo[] {\n\t\treturn this.#revisions.filter((memo) => memo.name === name)\n\t}\n\n\trender(memo: Memo): string {\n\t\treturn this.#format?.render?.(memo) ?? renderIndexEntry(memo)\n\t}\n\n\tremove(name: string): boolean\n\tremove(names: readonly string[]): boolean\n\tremove(names: string | readonly string[]): boolean {\n\t\tif (isArray(names)) {\n\t\t\t// Each present name still emits.\n\t\t\treturn removeEntries(names, (name) => this.#delete(name))\n\t\t}\n\t\treturn this.#delete(names)\n\t}\n\n\tclear(): void {\n\t\tthis.#revisions = []\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\tsnapshot(): MemoSnapshot {\n\t\t// Owned copies: a returned snapshot never aliases the manager's revisions or their origins.\n\t\treturn {\n\t\t\tid: this.#id,\n\t\t\tmemos: this.#revisions.map((memo) => ({\n\t\t\t\t...memo,\n\t\t\t\t...(memo.origin === undefined ? {} : { origin: { ...memo.origin } }),\n\t\t\t})),\n\t\t}\n\t}\n\n\t// Append a revision unless the name's current one already holds the same text, and emit `add`\n\t// after the list changed, so a swallowed listener throw can't perturb it.\n\t#append(input: MemoInput): Memo {\n\t\tconst current = this.memo(input.name)\n\t\tif (current?.description === input.description && current.content === input.content) {\n\t\t\treturn current\n\t\t}\n\t\tconst memo: Memo = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\tname: input.name,\n\t\t\tdescription: input.description,\n\t\t\tcontent: input.content,\n\t\t\ttime: Date.now(),\n\t\t\t...(input.origin === undefined ? {} : { origin: { ...input.origin } }),\n\t\t}\n\t\tthis.#revisions = [...this.#revisions, memo]\n\t\tthis.#emitter.emit('add', memo)\n\t\treturn memo\n\t}\n\n\t// Drop every revision of one name, emitting `remove` only when one was present — after the\n\t// list changed.\n\t#delete(name: string): boolean {\n\t\tconst kept = this.#revisions.filter((memo) => memo.name !== name)\n\t\tif (kept.length === this.#revisions.length) return false\n\t\tthis.#revisions = kept\n\t\tthis.#emitter.emit('remove', name)\n\t\treturn true\n\t}\n}\n","import type { ScopeFilter, ScopeInput, ScopeInterface, SelectionHandler } from '../types.js'\nimport { intersectKeys } from '../helpers.js'\n\n/**\n * Represents a named, immutable filter over a richer context's items — an optional allow-list per\n * category (`instructions` / `skills` / `memos` / `tools` / `files`), each keyed by that category's\n * identity (an instruction's, a skill's, a memo's, or a tool's `name`, a workspace file's `path`)\n * and read as an allow-list:\n * `undefined` lets everything pass, `[]` lets nothing pass, and a non-empty list passes the listed\n * keys alone. `narrow` composes a tighter child by set intersection.\n *\n * @remarks\n * - **A category list is three-way.** `undefined` ⇒ no constraint on that category (all\n * pass); `[]` ⇒ none pass; a non-empty list ⇒ only the listed keys pass. The build\n * step / loop apply this through `filterAllowList`.\n * - **Immutable.** The `id` is minted at construction; every supplied list is copied in\n * (so a later mutation of the caller's array can't leak in), and the lists are\n * `readonly`. A `Scope` is never mutated after construction — `narrow` returns a new\n * one rather than altering this one.\n * - **`narrow` is set-intersection (immutable composition).** A child scope's visible set\n * per category is the intersection of this scope's list and the config's list — but\n * `undefined` means \"no constraint\", so it acts as the universal set: intersecting\n * `undefined` with a list yields the list, and `undefined` with `undefined` stays\n * `undefined`. Narrowing can only tighten, never widen — a key excluded by a parent\n * can never be re-admitted by a child. The child keeps this scope's `name` and `select` handler.\n *\n * @example\n * ```ts\n * const scope = new Scope({ name: 'reader', tools: ['search', 'read'] })\n * // narrow intersects: tools ∩ ['read', 'write'] = ['read'] (write was never in the parent).\n * const tighter = scope.narrow({ tools: ['read', 'write'] })\n * tighter.tools // ['read']\n * // instructions had no parent constraint (undefined) → the child's list passes through.\n * tighter.narrow({ instructions: ['safety'] }).instructions // ['safety']\n * ```\n */\nexport class Scope implements ScopeInterface {\n\treadonly id: string = crypto.randomUUID()\n\treadonly name: string\n\treadonly instructions?: readonly string[]\n\treadonly skills?: readonly string[]\n\treadonly memos?: readonly string[]\n\treadonly tools?: readonly string[]\n\treadonly files?: readonly string[]\n\treadonly select?: SelectionHandler\n\n\tconstructor(input: ScopeInput) {\n\t\tthis.name = input.name\n\t\tif (input.select !== undefined) this.select = input.select\n\t\t// Copy each supplied list in (a later mutation of the caller's array can't leak in);\n\t\t// an omitted list stays `undefined` — the absence of a constraint.\n\t\tif (input.instructions !== undefined) this.instructions = [...input.instructions]\n\t\tif (input.skills !== undefined) this.skills = [...input.skills]\n\t\tif (input.memos !== undefined) this.memos = [...input.memos]\n\t\tif (input.tools !== undefined) this.tools = [...input.tools]\n\t\tif (input.files !== undefined) this.files = [...input.files]\n\t}\n\n\tnarrow(config: ScopeFilter): ScopeInterface {\n\t\tconst instructions = intersectKeys(this.instructions, config.instructions)\n\t\tconst skills = intersectKeys(this.skills, config.skills)\n\t\tconst memos = intersectKeys(this.memos, config.memos)\n\t\tconst tools = intersectKeys(this.tools, config.tools)\n\t\tconst files = intersectKeys(this.files, config.files)\n\t\treturn new Scope({\n\t\t\tname: this.name,\n\t\t\t...(this.select === undefined ? {} : { select: this.select }),\n\t\t\t...(instructions === undefined ? {} : { instructions }),\n\t\t\t...(skills === undefined ? {} : { skills }),\n\t\t\t...(memos === undefined ? {} : { memos }),\n\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t...(files === undefined ? {} : { files }),\n\t\t})\n\t}\n}\n","import type {\n\tScopeInput,\n\tScopeInterface,\n\tScopeManagerEventMap,\n\tScopeManagerInterface,\n\tScopeManagerOptions,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { removeEntries } from '../../helpers.js'\nimport { Scope } from './Scope.js'\n\n/**\n * Registers the named filters a richer context reuses — immutable {@link Scope} instances keyed by their\n * minted `id`, in insertion order, where `add` always mints and stores rather than overwriting,\n * and an observable `emitter` reports each change.\n *\n * @remarks\n * - **Registry.** Scopes live in an insertion-ordered `Map` keyed by their minted `id`;\n * `add` mints a {@link Scope} from a {@link ScopeInput} (an `id` plus the per-category\n * allow-lists), stores it, and returns it. `count` is the map size, `scope(id)` looks\n * one up, and `scopes()` lists them in insertion order. (Unlike the name-keyed\n * instruction registry, a scope's key is its minted `id`, so two scopes may share a\n * `name`; `add` therefore never overwrites.)\n * - **Removal.** `remove` drops one by id, or a batch — `true` only when every supplied id\n * was removed; `clear` empties the registry.\n * - **Observable.** The owned {@link emitter} ({@link ScopeManagerEventMap}) carries\n * `add` (the added scope) / `remove` (the id) / `clear`. Every event is emitted\n * directly, strictly after the map mutation completes; the emitter isolates a listener\n * throw and routes it to its `error` handler (the `error` option), so a buggy observer can\n * never corrupt a mutation.\n *\n * @example\n * ```ts\n * const manager = new ScopeManager()\n * const reader = manager.add({ name: 'reader', tools: ['search', 'read'] })\n * manager.scope(reader.id) // the same scope\n * manager.count // 1\n * ```\n */\nexport class ScopeManager implements ScopeManagerInterface {\n\treadonly #scopes = new Map<string, ScopeInterface>()\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<ScopeManagerEventMap>\n\n\tconstructor(options?: ScopeManagerOptions) {\n\t\tthis.#emitter = new Emitter<ScopeManagerEventMap>(options)\n\t}\n\n\tget emitter(): EmitterInterface<ScopeManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn this.#scopes.size\n\t}\n\n\tadd(input: ScopeInput): ScopeInterface {\n\t\tconst scope = new Scope(input)\n\t\tthis.#scopes.set(scope.id, scope)\n\t\t// Observe the added scope — after the map set, so a swallowed listener throw can't\n\t\t// perturb the store the caller is about to use.\n\t\tthis.#emitter.emit('add', scope)\n\t\treturn scope\n\t}\n\n\tscope(id: string): ScopeInterface | undefined {\n\t\treturn this.#scopes.get(id)\n\t}\n\n\tscopes(): readonly ScopeInterface[] {\n\t\treturn [...this.#scopes.values()]\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) {\n\t\t\t// Each present id still emits.\n\t\t\treturn removeEntries(ids, (id) => this.#delete(id))\n\t\t}\n\t\treturn this.#delete(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#scopes.clear()\n\t\t// Observe the cleared registry — after the map emptied, so a swallowed listener\n\t\t// throw can never alter the clear (no payload — `clear` is a pure signal).\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\t// Delete one scope, emitting `remove` only when one was actually removed (a delete of\n\t// an absent id returns `false` and emits nothing) — after the deletion.\n\t#delete(id: string): boolean {\n\t\tconst removed = this.#scopes.delete(id)\n\t\tif (removed) this.#emitter.emit('remove', id)\n\t\treturn removed\n\t}\n}\n","import type {\n\tContextSectionFormat,\n\tSkill,\n\tSkillInput,\n\tSkillManagerEventMap,\n\tSkillManagerInterface,\n\tSkillManagerOptions,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { createFile } from '@orkestrel/workspace'\nimport { removeEntries } from '../../helpers.js'\nimport { SKILL_SECTION_HEADER } from '../constants.js'\nimport { renderIndexEntry } from '../helpers.js'\n\n/**\n * Registers the skills a host supplies — instruction bundles keyed by `name`, listed in insertion\n * order, where adding a skill whose name is already stored overwrites it, last write wins —\n * carrying the `open` / `render` / `close` build contract and an observable `emitter`.\n *\n * @remarks\n * - **Registry.** Skills live in an insertion-ordered `Map` keyed by `name`; `add` takes one\n * {@link SkillInput} or a batch, mints each skill's `id`, and mints each file with `createFile`\n * from `@orkestrel/workspace`. A same-name `add` replaces the stored skill with a fresh `id` and\n * keeps the name's position. `count` is the map size, `skill(name)` looks one up, and `skills()`\n * lists them in insertion order.\n * - **Removal.** `remove` drops one by name, or a batch — `true` only when every supplied name was\n * removed; `clear` empties the registry.\n * - **Build contract.** `open` returns the options `open`, else `'## Skills'`; `render(skill)`\n * returns the options `render`, else `- name: description`; `close` returns the options\n * `close`, else `undefined`.\n * - **Observable.** The owned {@link emitter} ({@link SkillManagerEventMap}) carries `add` (the\n * created skill) / `remove` (the name) / `clear`, each emitted after the mutation completes; the\n * emitter isolates a listener throw and routes it to its `error` handler.\n *\n * @example\n * ```ts\n * const skills = new SkillManager()\n * const review = skills.add({\n * \tname: 'review',\n * \tdescription: 'Reviews a change for defects.',\n * \tcontent: 'Read the diff, then list each defect with its line.',\n * \tfiles: [{ path: 'checklist.md', content: { text: '- Tests pass', language: 'markdown' } }],\n * })\n * review.files[0]?.path // 'checklist.md'\n * skills.render(review) // '- review: Reviews a change for defects.'\n * ```\n */\nexport class SkillManager implements SkillManagerInterface {\n\treadonly #skills = new Map<string, Skill>()\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<SkillManagerEventMap>\n\treadonly #format: ContextSectionFormat<Skill> | undefined\n\n\tconstructor(options?: SkillManagerOptions) {\n\t\tthis.#emitter = new Emitter<SkillManagerEventMap>(options)\n\t\tthis.#format = options?.format\n\t}\n\n\tget emitter(): EmitterInterface<SkillManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn this.#skills.size\n\t}\n\n\tget open(): string {\n\t\treturn this.#format?.open ?? SKILL_SECTION_HEADER\n\t}\n\n\tget close(): string | undefined {\n\t\treturn this.#format?.close\n\t}\n\n\tadd(input: SkillInput): Skill\n\tadd(inputs: readonly SkillInput[]): readonly Skill[]\n\tadd(input: SkillInput | readonly SkillInput[]): Skill | readonly Skill[] {\n\t\tif (isArray(input)) return input.map((one) => this.#create(one))\n\t\treturn this.#create(input)\n\t}\n\n\tskill(name: string): Skill | undefined {\n\t\treturn this.#skills.get(name)\n\t}\n\n\tskills(): readonly Skill[] {\n\t\treturn [...this.#skills.values()]\n\t}\n\n\trender(skill: Skill): string {\n\t\treturn this.#format?.render?.(skill) ?? renderIndexEntry(skill)\n\t}\n\n\tremove(name: string): boolean\n\tremove(names: readonly string[]): boolean\n\tremove(names: string | readonly string[]): boolean {\n\t\tif (isArray(names)) {\n\t\t\t// Each present name still emits.\n\t\t\treturn removeEntries(names, (name) => this.#delete(name))\n\t\t}\n\t\treturn this.#delete(names)\n\t}\n\n\tclear(): void {\n\t\tthis.#skills.clear()\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\t// Mint a skill and its files, store it by name (a same-name one is replaced in place), and emit\n\t// `add` after the map changed, so a swallowed listener throw can't perturb it.\n\t#create(input: SkillInput): Skill {\n\t\tconst skill: Skill = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\tname: input.name,\n\t\t\tdescription: input.description,\n\t\t\tcontent: input.content,\n\t\t\tfiles: (input.files ?? []).map((file) => createFile(file)),\n\t\t}\n\t\tthis.#skills.set(skill.name, skill)\n\t\tthis.#emitter.emit('add', skill)\n\t\treturn skill\n\t}\n\n\t// Delete one skill, emitting `remove` only when one was present — after the deletion.\n\t#delete(name: string): boolean {\n\t\tconst removed = this.#skills.delete(name)\n\t\tif (removed) this.#emitter.emit('remove', name)\n\t\treturn removed\n\t}\n}\n","import type { MemoSnapshot, MemoStoreInterface } from '../types.js'\n\n/**\n * Implements the {@link MemoStoreInterface} in memory — a process-lifetime map of\n * {@link MemoSnapshot} records keyed by memo set id, the store\n * {@link import('../factories.js').createMemoryMemoStore} builds.\n *\n * @remarks\n * The snapshot is plain JSON data, so the memory tier needs no encoding. A stored set lives until\n * an explicit `delete`. Its driver-pluggable twin is\n * {@link import('./DatabaseMemoStore.js').DatabaseMemoStore} (the snapshot as one opaque JSON\n * column).\n *\n * This store copies nothing: `get` returns the object `set` received, as a readonly\n * {@link MemoSnapshot}, where the twin reads back the copy its driver made. `createMemoManager`\n * copies a snapshot through JSON, so a restored manager shares no state with this store.\n *\n * - **`get` resolves the stored snapshot for an id**, or `undefined` if none is stored.\n * - **`set` inserts or replaces under the snapshot's own `id`** (no separate id param).\n * - **`delete` drops a snapshot by id**; an absent id is a no-op (no throw).\n *\n * @example\n * ```ts\n * import { createMemoManager, createMemoryMemoStore } from '@orkestrel/agent'\n *\n * const store = createMemoryMemoStore()\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set\n * const snapshot = await store.get('ada')\n * await store.delete('ada') // drop it\n * ```\n */\nexport class MemoryMemoStore implements MemoStoreInterface {\n\t// Every stored set by id. Replaced on every change rather than mutated in place.\n\t#snapshots: ReadonlyMap<string, MemoSnapshot> = new Map()\n\n\t/**\n\t * Resolves the stored snapshot for `id`, or `undefined` if none is stored.\n\t *\n\t * @param id - The memo set id to resolve (a {@link MemoSnapshot.id})\n\t * @returns The stored snapshot, or `undefined` if absent\n\t */\n\tget(id: string): Promise<MemoSnapshot | undefined> {\n\t\treturn Promise.resolve(this.#snapshots.get(id))\n\t}\n\n\t/**\n\t * Inserts or replaces a snapshot under its own `snapshot.id`.\n\t *\n\t * @param snapshot - The snapshot to store, keyed by its `id`\n\t */\n\tset(snapshot: MemoSnapshot): Promise<void> {\n\t\tthis.#snapshots = new Map([...this.#snapshots, [snapshot.id, snapshot]])\n\t\treturn Promise.resolve()\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op.\n\t *\n\t * @param id - The memo set id to drop\n\t */\n\tdelete(id: string): Promise<void> {\n\t\tthis.#snapshots = new Map([...this.#snapshots].filter(([key]) => key !== id))\n\t\treturn Promise.resolve()\n\t}\n}\n","import type { MemoSnapshot, MemoSnapshotRow, MemoStoreInterface } from '../types.js'\nimport type { AuditFault } from '@orkestrel/contract'\nimport type { TableInterface } from '@orkestrel/database'\nimport { describeFaults, preview } from '@orkestrel/contract'\nimport { memoSnapshotContract } from '../contracts.js'\nimport { ContextError } from '../errors.js'\nimport { isMemoSnapshot } from '../validators.js'\nimport { describeSnapshotRefusal } from '../../helpers.js'\n\n/**\n * Backs a {@link MemoStoreInterface} with one table of the `databases` layer — a memo set's durable\n * state is a row holding the snapshot as one opaque JSON column, narrowed back on `get` by\n * {@link import('../validators.js').isMemoSnapshot}.\n *\n * @remarks\n * The store is driver-agnostic: it holds a single {@link TableInterface} whose backend (memory,\n * JSON, SQLite, IndexedDB) is chosen by whoever builds it (the factories), so a JSON / SQLite /\n * IndexedDB backend swaps in without touching the\n * {@link import('../memos/MemoManager.js').MemoManager}. Default driver: memory\n * ({@link import('../factories.js').createDatabaseMemoStore} passes `createMemoryDriver()`), so it\n * also works in memory out of the box; pass a JSON / SQLite / IndexedDB driver for a durable store.\n *\n * The {@link MemoSnapshot} is stored as one opaque JSON column — the table is a row of\n * `{ id; snapshot }` ({@link MemoSnapshotRow}), the snapshot the whole JSON blob (a `rawShape`\n * column the factory builds). The snapshot is already a complete, self-contained, pure-JSON payload,\n * so storing it whole is lossless and keeps the row type flat (`snapshot` reads back as `unknown`).\n *\n * - **`set(snapshot)` upserts under the snapshot's own `id`** (no separate id param) — it writes\n * the row `{ id: snapshot.id, snapshot }`.\n * - **`get(id)` resolves the stored snapshot for an id**, narrowing the opaque JSON column back to\n * a {@link MemoSnapshot} ({@link import('../validators.js').isMemoSnapshot} — the total guard for\n * an untrusted storage read), or `undefined` only when no row is stored. A row whose blob is malformed\n * or names another id throws a `ContextError` coded `'SNAPSHOT'`, so a host never overwrites it\n * unawares, and `delete` still removes it.\n * - **`delete(id)` drops a snapshot by id**; an absent id is a no-op (no throw).\n *\n * @example\n * ```ts\n * import { createDatabaseMemoStore, createMemoManager } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseMemoStore(createMemoryDriver()) // a durable driver swaps in here\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set (one JSON column)\n * const snapshot = await store.get('ada')\n * await store.delete('ada') // drop it\n * ```\n */\nexport class DatabaseMemoStore implements MemoStoreInterface {\n\treadonly #table: TableInterface<MemoSnapshotRow>\n\n\t/**\n\t * Wraps a table as a memo store.\n\t *\n\t * @param table - The {@link TableInterface} holding the snapshots — its row is the\n\t * {@link MemoSnapshotRow} `{ id; snapshot }` shape (the snapshot one opaque JSON column)\n\t */\n\tconstructor(table: TableInterface<MemoSnapshotRow>) {\n\t\tthis.#table = table\n\t}\n\n\t/**\n\t * Resolves the stored snapshot for `id`, narrowing the opaque JSON column back to a\n\t * `MemoSnapshot`.\n\t *\n\t * @remarks\n\t * The column is an untrusted storage read, so a blob that fails\n\t * {@link import('../validators.js').isMemoSnapshot}, or whose own `id` is not `id`, throws rather\n\t * than reading as absent.\n\t *\n\t * @param id - The memo set id to resolve (the row key)\n\t * @returns The stored snapshot, or `undefined` when no row is stored\n\t * @throws {ContextError} Thrown when the row's blob is malformed or names another id (code `'SNAPSHOT'`), with the faults under `context.faults`\n\t */\n\tasync get(id: string): Promise<MemoSnapshot | undefined> {\n\t\tconst row = await this.#table.get(id)\n\t\tif (row === undefined) return undefined\n\t\t// The snapshot crosses back as an untrusted storage read (a structured clone / a JSON row),\n\t\t// so narrow the opaque JSON column with the total boundary guard rather than a cast;\n\t\t// `audit` runs only after the guard refused, to fill the faults.\n\t\tconst snapshot = row.snapshot\n\t\tif (isMemoSnapshot(snapshot)) {\n\t\t\tif (snapshot.id === id) return snapshot\n\t\t\tconst faults: readonly AuditFault[] = [\n\t\t\t\t{\n\t\t\t\t\treason: 'type',\n\t\t\t\t\tpath: ['id'],\n\t\t\t\t\texpected: 'literal',\n\t\t\t\t\treceived: preview(snapshot.id),\n\t\t\t\t},\n\t\t\t]\n\t\t\tthrow new ContextError('SNAPSHOT', describeSnapshotRefusal('memo set', id, faults), {\n\t\t\t\tcontext: { id, faults },\n\t\t\t})\n\t\t}\n\t\tconst faults = memoSnapshotContract.audit(snapshot)\n\t\tthrow new ContextError('SNAPSHOT', describeSnapshotRefusal('memo set', id, faults), {\n\t\t\tcontext: { id, faults },\n\t\t})\n\t}\n\n\t/**\n\t * Inserts or replaces under the snapshot's own `id` (no separate id param) — the row is\n\t * `{ id, snapshot }`.\n\t *\n\t * @param snapshot - The snapshot to store, keyed by its `id`\n\t * @returns A promise that resolves after the row is written\n\t * @throws {ContextError} Thrown before any write when the snapshot fails the guard `get` reads (code `'SNAPSHOT'`), so no row is written that `get` refuses\n\t */\n\tasync set(snapshot: MemoSnapshot): Promise<void> {\n\t\tif (!isMemoSnapshot(snapshot)) {\n\t\t\tconst unchecked: unknown = snapshot\n\t\t\tconst faults = memoSnapshotContract.audit(unchecked)\n\t\t\tthrow new ContextError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`a memo store writes only what snapshot() returned\\n${describeFaults(faults, memoSnapshotContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tawait this.#table.set({ id: snapshot.id, snapshot })\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op (no throw).\n\t *\n\t * @param id - The memo set id to drop\n\t * @returns A promise that resolves after the row is gone\n\t */\n\tasync delete(id: string): Promise<void> {\n\t\tawait this.#table.remove(id)\n\t}\n}\n","import type {\n\tAgentContextInterface,\n\tAgentContextOptions,\n\tInstructionManagerInterface,\n\tMemoManagerInterface,\n\tScopeInterface,\n\tSelection,\n\tSelectionHandler,\n\tSkillManagerInterface,\n} from './types.js'\nimport type { ConversationInterface, ConversationManagerInterface } from '../conversations/index.js'\nimport type { Message } from '../types.js'\nimport type { ToolManagerInterface } from '@orkestrel/tool'\nimport type { WorkspaceManagerInterface } from '@orkestrel/workspace'\nimport { ToolManager } from '@orkestrel/tool'\nimport { isTextContent, WorkspaceManager } from '@orkestrel/workspace'\nimport { InstructionManager } from './instructions/InstructionManager.js'\nimport { MemoManager } from './memos/MemoManager.js'\nimport { SkillManager } from './skills/SkillManager.js'\nimport { ConversationError, ConversationManager } from '../conversations/index.js'\nimport { CONVERSATION_ROLES } from '../constants.js'\nimport { filterAllowList } from '../helpers.js'\nimport { attachUserImages, collectImageData, renderFencedFile, renderSection } from './helpers.js'\nimport { WORKSPACE_SECTION_HEADER } from './constants.js'\n\n/**\n * Assembles a provider request from the richer turn context — the optional system prompt, the\n * observable context managers (instructions / skills / memos / workspaces), the\n * {@link ConversationManagerInterface} message source whose active conversation is `messages`, the\n * {@link ToolManagerInterface} registry, and an active {@link ScopeInterface} changed through\n * {@link AgentContextInterface.apply}. `build()` folds the system prompt, the scoped instructions,\n * and the skills index into one system message, the memos index and the active workspace's text into\n * one `user` context message after it, then the conversation, and never reads `tools`.\n *\n * @remarks\n * - **Composition.** `system` is the optional system prompt; `instructions` / `skills` / `memos` /\n * `tools` / `workspaces` / `conversations` are the registries passed in `options` (bring your own),\n * or fresh empty ones when omitted (so `workspaces` is always present); `messages` is the active\n * conversation's live tail (always defined — see the following item). `scope` is the active filter —\n * `undefined` (the default) ⇒ no filtering; change it through `apply(scope)`. The structural\n * `workspaces` / `conversations` registries are fixed at construction; switch their active\n * members through their own `switch(id)` methods.\n * - **The default conversation — the conversation registry's active conversation.** `conversations`\n * is a {@link ConversationManagerInterface}. The constructor adds a default conversation when the\n * manager has no active one, so the dynamic `messages` getter — `this.#conversations.active` — is\n * always defined. `messages` returns the active conversation itself (it owns the live tail + the\n * message verbs directly, satisfying\n * {@link import('../conversations/index.js').MessageManagerInterface} structurally — the same\n * reference, no duplication), and `build()` with no selection folds that conversation's `view()`\n * (its per-section summaries + live tail) as the authoritative message inclusion — the scope does\n * not filter the conversation (it owns inclusion through compaction; scope filters only\n * instructions / skills / memos / tools / workspace files). An agent run binds a conversation at\n * its call, the active one unless the run names another, and builds through a selection over that\n * conversation, so `conversations.switch(id)` sets the next run's default at any moment and never\n * moves a live run; `select(conversation, request, signal)` checks the conversation it is given.\n * - **`build()` — the scoped assembly + the format cascade.** It folds, in order,\n * the system prompt, the scope-filtered instructions (a block: the section's `open` text,\n * each item's rendering, then any `close` text), then the scope-filtered skills index (a block\n * whose entries join with a single newline) into one leading `system` message (prepended only\n * when at least one part exists), then the scope-filtered memos index (the same block) and the\n * active workspace's text files into one `user` context message (present only when the memos or\n * workspace section renders), then\n * appends the active conversation's `view()` (the conversation owns message inclusion through\n * compaction — the scope does not filter the conversation). The instruction manager resolves\n * each `open` / item / `close` most-specific-first: `open` = manager-options-override >\n * built-in; per item = item-override > manager-options-override > built-in; `close` =\n * manager-options-override (no built-in ⇒ no closing line when unset) (see\n * {@link AgentContextInterface.build}). With no override set, each section is its built-in\n * header + items, no closing line. The active workspace's scoped-in image files' `base64` payload is attached to\n * the last user message (a vision provider reads images off a user turn); when no user message\n * exists the attachment is skipped. Built fresh each call (recomputed, never cached), so it\n * always reflects the current managers / messages / scope / active workspace; it never mutates a\n * manager or the stored messages. `build(selection)` adds at most one `system` message of its own\n * and reads the selection's messages as conversation parts without a role check; the select path\n * refuses a selected `system` role as a `'ROLE'` fault over `view()`; a host's direct\n * `build(selection)` is host-trusted, and a conversation a host implements itself whose `view()`\n * holds a `system` message is read as host-trusted too.\n * - **The active workspace, rendered by carrier — the sole document/image context.**\n * `workspaces.active` (when set) has its {@link import('@orkestrel/workspace').FileInterface} values scope-filtered by `scope.files`,\n * then split: text files fold into a dedicated `## Workspace` section (fenced reference blocks)\n * in the context message — a `user` message with a minted id, never stored — and image files'\n * `base64` payload attaches to the last user message of the conversation, never to the context\n * message. The `system` message holds host-authored text alone: the system prompt, the\n * instructions, and the skills index. Active-only — never the other registered workspaces; with no\n * active workspace nothing renders for workspaces. `build()` owns this render (a `Workspace` /\n * `WorkspaceManager` stays file-focused).\n * - **Tools are structural, not in the prompt.** The registry is advertised to the provider\n * through `tools.definitions()` (scope-filtered by the loop), never serialized into the\n * message array — so the output of the `build()` method carries no tool content, scoped or not.\n * - **Event-free context; observable managers.** The context itself owns no Emitter; the\n * context managers each carry their own (the push observation surface).\n *\n * @example\n * ```ts\n * const context = new AgentContext({ system: 'You are concise.' })\n * context.instructions.add({ name: 'tone', content: 'Be terse.' })\n * context.messages.add({ role: 'user', content: 'Hi' })\n * context.build() // [{ role: 'system', content: 'You are concise.\\n\\n## Instructions\\n\\nBe terse.' }, { role: 'user', content: 'Hi' }]\n * ```\n */\nexport class AgentContext implements AgentContextInterface {\n\treadonly #system: string | undefined\n\treadonly #instructions: InstructionManagerInterface\n\treadonly #skills: SkillManagerInterface\n\treadonly #memos: MemoManagerInterface\n\t// The workspace registry whose active workspace `build()` renders by carrier (text files →\n\t// the context message, image files → the last user message). Always present (a fresh empty\n\t// manager when none was supplied). The registry is structural; switch its active workspace\n\t// through `workspaces.switch(id)`. `build()` reads `active` / its `files()` fresh each call.\n\treadonly #workspaces: WorkspaceManagerInterface\n\t// The conversation registry whose active conversation is a run's default: the dynamic `messages`\n\t// getter returns `#conversations.active`, which the constructor seeds when absent, and `build()`\n\t// with no selection folds that conversation's `view()`. The registry is structural; switch its\n\t// active conversation through `conversations.switch(id)`.\n\treadonly #conversations: ConversationManagerInterface\n\treadonly #tools: ToolManagerInterface\n\t// The agent default; the active scope's `select` overrides it at each `select` call.\n\treadonly #select: SelectionHandler | undefined\n\t#scope: ScopeInterface | undefined\n\n\tconstructor(options?: AgentContextOptions) {\n\t\tthis.#system = options?.system\n\t\tthis.#instructions = options?.instructions ?? new InstructionManager()\n\t\tthis.#skills = options?.skills ?? new SkillManager()\n\t\tthis.#memos = options?.memos ?? new MemoManager()\n\t\t// Default to a fresh empty registry so `context.workspaces` is always present (mirroring\n\t\t// the always-present instruction manager); a supplied one is reused. The active workspace\n\t\t// is the sole document/image context.\n\t\tthis.#workspaces = options?.workspaces ?? new WorkspaceManager()\n\t\t// The conversation registry the default conversation flows from — a supplied one is reused,\n\t\t// else a fresh empty one. Seed an active conversation so `context.messages` (the active conversation's\n\t\t// live tail) is always defined: when the manager has none active, call `add()` for a default\n\t\t// (which auto-activates it). NB: `messages` is not captured here — it is computed dynamically\n\t\t// (the getter reads `#conversations.active`), so it always tracks the current active\n\t\t// conversation's live tail, no duplication.\n\t\tthis.#conversations = options?.conversations ?? new ConversationManager()\n\t\tif (this.#conversations.active === undefined) this.#conversations.add()\n\t\tthis.#tools = options?.tools ?? new ToolManager()\n\t\tthis.#select = options?.select\n\t\tthis.#scope = options?.scope\n\t}\n\n\tget system(): string | undefined {\n\t\treturn this.#system\n\t}\n\n\tget instructions(): InstructionManagerInterface {\n\t\treturn this.#instructions\n\t}\n\n\tget skills(): SkillManagerInterface {\n\t\treturn this.#skills\n\t}\n\n\tget memos(): MemoManagerInterface {\n\t\treturn this.#memos\n\t}\n\n\tget workspaces(): WorkspaceManagerInterface {\n\t\treturn this.#workspaces\n\t}\n\n\t// Dynamic — the active conversation itself (it owns its live tail + the message verbs directly,\n\t// like a `Workspace` owns its files), always defined: the constructor adds a default when the\n\t// registry has no active conversation. Computed on every read (never captured), so\n\t// `context.messages` always points at the current active conversation (the same reference — no\n\t// duplication) and follows a `conversations.switch(id)`. It is the default a run binds, never a\n\t// live run's target. The `#ensureConversation()` method re-seats a default if a caller's supplied\n\t// manager was emptied (for example `clear()`), so the getter is total — never undefined.\n\tget messages(): ConversationInterface {\n\t\treturn this.#ensureConversation()\n\t}\n\n\tget conversations(): ConversationManagerInterface {\n\t\treturn this.#conversations\n\t}\n\n\tget tools(): ToolManagerInterface {\n\t\treturn this.#tools\n\t}\n\n\tget scope(): ScopeInterface | undefined {\n\t\treturn this.#scope\n\t}\n\n\tapply(scope: ScopeInterface | undefined): void {\n\t\tthis.#scope = scope\n\t}\n\n\tselect(\n\t\tconversation: ConversationInterface,\n\t\trequest: Message,\n\t\tsignal: AbortSignal,\n\t): Promise<Selection> | undefined {\n\t\t// Returning `undefined` synchronously keeps the loop's default path free of an `await`.\n\t\tconst handler = this.#scope?.select ?? this.#select\n\t\tif (handler === undefined) return undefined\n\t\treturn this.#check(handler, conversation, request, signal)\n\t}\n\n\tbuild(selection?: Selection): readonly Message[] {\n\t\tconst scope = this.#scope\n\t\t// Tools reach the provider structurally. Each manager resolves its format cascade.\n\t\tconst parts: string[] = []\n\t\t// Configured by `=== undefined`, not falsiness — an explicitly supplied '' (or a\n\t\t// whitespace-only) system is opted in and prepended verbatim; a truthiness check would drop it.\n\t\tif (this.#system !== undefined) parts.push(this.#system)\n\t\tconst instructions = filterAllowList(\n\t\t\tscope?.instructions,\n\t\t\tthis.#instructions.instructions(),\n\t\t\t(one) => one.name,\n\t\t)\n\t\tconst instructed = renderSection(\n\t\t\tthis.#instructions.open,\n\t\t\tinstructions,\n\t\t\t(one) => this.#instructions.render(one),\n\t\t\tthis.#instructions.close,\n\t\t)\n\t\tif (instructed !== undefined) parts.push(instructed)\n\t\tconst skills = filterAllowList(scope?.skills, this.#skills.skills(), (one) => one.name)\n\t\tconst indexed = renderSection(\n\t\t\tthis.#skills.open,\n\t\t\tskills.length === 0 ? [] : [skills.map((one) => this.#skills.render(one)).join('\\n')],\n\t\t\t(block) => block,\n\t\t\tthis.#skills.close,\n\t\t)\n\t\tif (indexed !== undefined) parts.push(indexed)\n\t\t// The active workspace is the sole document/image context. `build()` owns this render\n\t\t// because a `Workspace` / `WorkspaceManager` stays file-focused.\n\t\tconst files = filterAllowList(\n\t\t\tscope?.files,\n\t\t\tthis.#workspaces.active?.files() ?? [],\n\t\t\t(one) => one.path,\n\t\t)\n\t\tconst workspaceTexts = files.flatMap((file) =>\n\t\t\tisTextContent(file.content)\n\t\t\t\t? [{ path: file.path, language: file.content.language, text: file.content.text }]\n\t\t\t\t: [],\n\t\t)\n\t\tconst documented = renderSection(\n\t\t\tWORKSPACE_SECTION_HEADER,\n\t\t\tworkspaceTexts,\n\t\t\t(file) => renderFencedFile(file.path, file.language, file.text),\n\t\t\tundefined,\n\t\t)\n\t\tconst memos = filterAllowList(scope?.memos, this.#memos.memos(), (one) => one.name)\n\t\tconst remembered = renderSection(\n\t\t\tthis.#memos.open,\n\t\t\tmemos.length === 0 ? [] : [memos.map((one) => this.#memos.render(one)).join('\\n')],\n\t\t\t(block) => block,\n\t\t\tthis.#memos.close,\n\t\t)\n\t\t// With no selection, the active conversation's `view()` is authoritative (the per-section\n\t\t// summaries + the live tail) — the conversation owns message inclusion through compaction, so the\n\t\t// scope does not filter the conversation here (scope filters only the preceding instructions /\n\t\t// skills / memos / tools / workspace files). The active conversation is always present (the\n\t\t// constructor adds one), with `#ensureConversation()` as a total fallback if a caller emptied its\n\t\t// supplied registry.\n\t\tconst conversation = selection?.messages ?? this.#ensureConversation().view()\n\t\t// A vision provider reads image payloads from a user turn.\n\t\tconst tail = attachUserImages(conversation, collectImageData(files))\n\t\tconst slots = [remembered, documented].filter((slot) => slot !== undefined)\n\t\tconst context: readonly Message[] =\n\t\t\tslots.length === 0\n\t\t\t\t? []\n\t\t\t\t: [{ id: crypto.randomUUID(), role: 'user', content: slots.join('\\n\\n') }]\n\n\t\tif (parts.length === 0) return [...context, ...tail]\n\t\tconst system: Message = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\trole: 'system',\n\t\t\tcontent: parts.join('\\n\\n'),\n\t\t}\n\t\treturn [system, ...context, ...tail]\n\t}\n\n\t// Another run can append to the conversation while the handler awaits, so the view's ids are\n\t// recorded before the call (an async body runs synchronously up to its first `await`) and\n\t// compared after it; a changed view turns the handler's selection into a fault over `view()`.\n\t// Over an unchanged view, a selected message whose role is not a conversation role does the same.\n\tasync #check(\n\t\thandler: SelectionHandler,\n\t\tconversation: ConversationInterface,\n\t\trequest: Message,\n\t\tsignal: AbortSignal,\n\t): Promise<Selection> {\n\t\tconst before = conversation.view().map((message) => message.id)\n\t\tconst selection = await handler(conversation, request, signal)\n\t\tconst after = conversation.view()\n\t\tif (\n\t\t\tafter.length === before.length &&\n\t\t\tafter.every((message, index) => message.id === before[index])\n\t\t) {\n\t\t\tconst refused = selection.messages.find(\n\t\t\t\t(message) => !CONVERSATION_ROLES.some((role) => role === message.role),\n\t\t\t)\n\t\t\tif (refused === undefined) return selection\n\t\t\tconst role =\n\t\t\t\ttypeof refused.role === 'string'\n\t\t\t\t\t? JSON.stringify(refused.role.slice(0, 32))\n\t\t\t\t\t: typeof refused.role\n\t\t\tconst reason = `a selected message role must be one of ${CONVERSATION_ROLES.map((one) => JSON.stringify(one)).join(', ')}; message ${selection.messages.indexOf(refused)} has ${role}`\n\t\t\treturn {\n\t\t\t\tmessages: after,\n\t\t\t\tjudgments: selection.judgments,\n\t\t\t\t...(selection.usage === undefined ? {} : { usage: selection.usage }),\n\t\t\t\tfault: new ConversationError(\n\t\t\t\t\t'ROLE',\n\t\t\t\t\treason,\n\t\t\t\t\tselection.fault === undefined ? undefined : { cause: selection.fault },\n\t\t\t\t),\n\t\t\t}\n\t\t}\n\t\tconst text = `conversation ${conversation.id} changed during selection: ${before.length} messages before, ${after.length} after`\n\t\treturn {\n\t\t\tmessages: after,\n\t\t\tjudgments: selection.judgments,\n\t\t\t...(selection.usage === undefined ? {} : { usage: selection.usage }),\n\t\t\tfault:\n\t\t\t\tselection.fault === undefined\n\t\t\t\t\t? new Error(text)\n\t\t\t\t\t: new Error(text, { cause: selection.fault }),\n\t\t}\n\t}\n\n\t// A supplied registry can be emptied after construction. Re-seat a default so the message\n\t// source stays defined, preferring the active conversation an add observer may have selected.\n\t#ensureConversation(): ConversationInterface {\n\t\tif (this.#conversations.active !== undefined) return this.#conversations.active\n\t\tconst conversation = this.#conversations.add()\n\t\treturn this.#conversations.active ?? conversation\n\t}\n}\n","import type {\n\tAgentContextInterface,\n\tAgentContextOptions,\n\tInstructionInput,\n\tInstructionInterface,\n\tInstructionManagerInterface,\n\tInstructionManagerOptions,\n\tMemoManagerInterface,\n\tMemoManagerOptions,\n\tMemoSnapshotRow,\n\tMemoStoreInterface,\n\tScopeInput,\n\tScopeInterface,\n\tScopeManagerInterface,\n\tScopeManagerOptions,\n\tSkillManagerInterface,\n\tSkillManagerOptions,\n} from './types.js'\nimport type { DriverInterface, TableInterface } from '@orkestrel/database'\nimport { rawShape, stringShape } from '@orkestrel/contract'\nimport { createDatabase, createMemoryDriver } from '@orkestrel/database'\nimport { Instruction } from './instructions/Instruction.js'\nimport { InstructionManager } from './instructions/InstructionManager.js'\nimport { MemoManager } from './memos/MemoManager.js'\nimport { Scope } from './scopes/Scope.js'\nimport { ScopeManager } from './scopes/ScopeManager.js'\nimport { SkillManager } from './skills/SkillManager.js'\nimport { MemoryMemoStore } from './stores/MemoryMemoStore.js'\nimport { DatabaseMemoStore } from './stores/DatabaseMemoStore.js'\nimport { AgentContext } from './AgentContext.js'\n\n/**\n * Creates an instruction — an immutable {@link InstructionInterface} (a named directive)\n * from its `name` / `content` and optional `priority`, the `id` minted at construction.\n *\n * @remarks\n * Only `name` / `content` are required; `priority` orders the instruction in the\n * rendered list of an {@link InstructionManagerInterface} (higher first). Default: `0`.\n * Stored immutable — never mutated after creation.\n *\n * @param input - `name` / `content` (required) and an optional `priority` (see\n * {@link InstructionInput})\n * @returns A working {@link InstructionInterface}\n *\n * @example\n * ```ts\n * import { createInstruction } from '@orkestrel/agent'\n *\n * const instruction = createInstruction({ name: 'tone', content: 'Be concise.', priority: 5 })\n * ```\n */\nexport function createInstruction(input: InstructionInput): InstructionInterface {\n\treturn new Instruction(input)\n}\n\n/**\n * Creates an instruction registry — an {@link InstructionManagerInterface} holding\n * immutable instructions keyed by `name`, listed by descending `priority`.\n *\n * @remarks\n * Starts empty; `add` (one or a batch) mints each `id` and overwrites a same-name\n * instruction (last write wins); `instructions()` lists them sorted by descending\n * `priority` (stable for ties); `open` / `render` / `close` are the build contract a richer\n * context renders an instructions block with; `remove` (one or a batch) reports `true` only\n * when every supplied name was removed; `clear` empties it. Carries an observable `emitter`\n * ({@link import('./types.js').InstructionManagerEventMap}) wired through the reserved `on`\n * option; the emitter isolates a listener throw and routes it to its `error` handler\n * (the `error` option), so it can never corrupt a mutation. An optional `format`\n * override is the manager-options level of the `AgentContext` build cascade (consulted by\n * `open` / `render` / `close`, beating the built-in; a per-item\n * `InstructionInput.override` still beats it).\n *\n * @param options - Optional `on` hooks + a `format` override (see {@link InstructionManagerOptions})\n * @returns An empty {@link InstructionManagerInterface}\n *\n * @example\n * ```ts\n * import { createInstructionManager } from '@orkestrel/agent'\n *\n * const instructions = createInstructionManager()\n * instructions.add({ name: 'tone', content: 'Be concise.', priority: 5 })\n * ```\n */\nexport function createInstructionManager(\n\toptions?: InstructionManagerOptions,\n): InstructionManagerInterface {\n\treturn new InstructionManager(options)\n}\n\n/**\n * Creates a named scope — an immutable {@link ScopeInterface} from its `name` and its\n * per-category allow-lists, the `id` minted at construction.\n *\n * @remarks\n * Each list is three-way: `undefined` ⇒ no constraint on that category (all pass), `[]` ⇒\n * none pass, a non-empty list ⇒ only the listed keys pass. `narrow(config)` composes a\n * tighter child by set-intersection (an `undefined` side imposing no constraint). Stored\n * immutable — never mutated after creation (`narrow` returns a new scope).\n *\n * @param input - `name` (required) and the optional `instructions` / `skills` / `memos` /\n * `tools` / `files` allow-lists (see {@link ScopeInput})\n * @returns A working {@link ScopeInterface}\n *\n * @example\n * ```ts\n * import { createScope } from '@orkestrel/agent'\n *\n * const reader = createScope({ name: 'reader', tools: ['search', 'read'] })\n * reader.narrow({ tools: ['read', 'write'] }).tools // ['read'] — intersection tightens\n * ```\n */\nexport function createScope(input: ScopeInput): ScopeInterface {\n\treturn new Scope(input)\n}\n\n/**\n * Creates a scope registry — a {@link ScopeManagerInterface} holding immutable scopes keyed\n * by their minted `id`, in insertion order.\n *\n * @remarks\n * Starts empty; `add` mints each scope's `id` and stores it (keyed by `id`, so it\n * never overwrites — two scopes may share a `name`); `scopes()` lists them in insertion order;\n * `remove` (one or a batch) reports `true` only when every supplied id was removed; `clear`\n * empties it. Carries an observable `emitter`\n * ({@link import('./types.js').ScopeManagerEventMap}) wired through the reserved `on`\n * option; the emitter isolates a listener throw and routes it to its `error` handler\n * (the `error` option), so it can never corrupt a mutation.\n *\n * @param options - Optional `on` hooks (see {@link ScopeManagerOptions})\n * @returns An empty {@link ScopeManagerInterface}\n *\n * @example\n * ```ts\n * import { createScopeManager } from '@orkestrel/agent'\n *\n * const scopes = createScopeManager()\n * const reader = scopes.add({ name: 'reader', tools: ['search'] })\n * ```\n */\nexport function createScopeManager(options?: ScopeManagerOptions): ScopeManagerInterface {\n\treturn new ScopeManager(options)\n}\n\n/**\n * Creates a memo set — a {@link MemoManagerInterface} holding durable facts keyed by `name`, where\n * a same-name `add` with changed text appends a revision and the earlier ones stay readable.\n *\n * @remarks\n * Starts empty unless a `snapshot` restores revisions, silently. `id` names the set: the\n * `snapshot.id`, else the `id` option, else a minted UUID. Re-adding unchanged text is a no-op that\n * emits nothing; `remove` forgets a memo and its whole history. An optional `format` override is\n * the manager-options level of the build cascade, beating the built-in `'## Memos'` header and the\n * `- name: description` index line.\n *\n * @param options - Optional `id`, `on` hooks, `error` handler, `format` override, and `snapshot`\n * (see {@link MemoManagerOptions})\n * @returns A {@link MemoManagerInterface}\n *\n * @example\n * ```ts\n * import { createMemoManager } from '@orkestrel/agent'\n *\n * const memos = createMemoManager({ id: 'ada' })\n * const tone = memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * memos.render(tone) // '- tone: Prefers short replies.'\n * ```\n */\nexport function createMemoManager(options?: MemoManagerOptions): MemoManagerInterface {\n\treturn new MemoManager(options)\n}\n\n/**\n * Creates the in-memory memo store — a {@link MemoStoreInterface} backed by a process-lifetime map,\n * the default twin of {@link createDatabaseMemoStore}.\n *\n * @remarks\n * The snapshot is already plain JSON data, so the memory tier needs no encoding. `get` / `set` /\n * `delete` are async (the same shape a durable backend fits), and a stored memo set lives until an\n * explicit `delete`. Its driver-pluggable twin is {@link createDatabaseMemoStore} (the snapshot as\n * one opaque JSON column over a `databases` table). Restoring stays a caller concern: read a\n * snapshot back and pass it to `createMemoManager({ snapshot })`.\n *\n * @returns A memory-backed {@link MemoStoreInterface}\n *\n * @example\n * ```ts\n * import { createMemoManager, createMemoryMemoStore } from '@orkestrel/agent'\n *\n * const store = createMemoryMemoStore()\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set\n * const snapshot = await store.get('ada')\n * ```\n */\nexport function createMemoryMemoStore(): MemoStoreInterface {\n\treturn new MemoryMemoStore()\n}\n\n/**\n * Creates a {@link DatabaseMemoStore} over any {@link DriverInterface} — the durable, driver-pluggable\n * backing for a memo set, holding each snapshot as one opaque JSON column in a `memos` table.\n *\n * @remarks\n * Builds a one-table database (`memos`, keyed by `id`) over the supplied driver, the snapshot held\n * as one opaque JSON column — the column map is `{ id; snapshot }` where `snapshot` is a `rawShape`\n * (a JSON blob). The snapshot is already a complete, self-contained, pure-JSON payload, so storing\n * it whole is lossless and keeps the row type flat (the column reads back as `unknown`, narrowed on\n * `get` by {@link import('./validators.js').isMemoSnapshot}). Default driver:\n * {@link createMemoryDriver}, so the store also works in memory out of the box; pass a server\n * `createJSONDriver` / `createSQLiteDriver` (or a browser IndexedDB driver) for a persistent one —\n * the durability is the driver's job, the store engine is shared. It swaps in behind\n * {@link MemoStoreInterface} without touching the manager.\n *\n * @param driver - The storage backend the snapshots persist to. Default: {@link createMemoryDriver}\n * @returns A {@link MemoStoreInterface} over the driver\n *\n * @example\n * ```ts\n * import { createDatabaseMemoStore, createMemoManager } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseMemoStore(createMemoryDriver()) // a durable driver swaps in here\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set (one JSON column)\n * const snapshot = await store.get('ada')\n * ```\n */\nexport function createDatabaseMemoStore(\n\tdriver: DriverInterface = createMemoryDriver(),\n): MemoStoreInterface {\n\t// The snapshot is stored as one opaque JSON column (`rawShape`), so the row infers flat —\n\t// `{ id: string; snapshot: unknown }` = `MemoSnapshotRow` — and the revisions' snapshot shape\n\t// never forces a contract `Infer`.\n\tconst columns = { id: stringShape(), snapshot: rawShape({}) }\n\tconst database = createDatabase({ driver, tables: { memos: columns } })\n\tconst table: TableInterface<MemoSnapshotRow> = database.table('memos')\n\treturn new DatabaseMemoStore(table)\n}\n\n/**\n * Creates a skill registry — a {@link SkillManagerInterface} holding the skills a host supplies,\n * keyed by `name` and listed in insertion order.\n *\n * @remarks\n * Starts empty; `add` (one or a batch) mints each `id` and each file through `createFile`, and\n * overwrites a same-name skill (last write wins, position kept). An optional `format` override is\n * the manager-options level of the build cascade, beating the built-in `'## Skills'` header and the\n * `- name: description` index line.\n *\n * @param options - Optional `on` hooks, `error` handler, and `format` override (see\n * {@link SkillManagerOptions})\n * @returns An empty {@link SkillManagerInterface}\n *\n * @example\n * ```ts\n * import { createSkillManager } from '@orkestrel/agent'\n *\n * const skills = createSkillManager()\n * const review = skills.add({ name: 'review', description: 'Reviews a change.', content: 'Read the diff.' })\n * skills.render(review) // '- review: Reviews a change.'\n * ```\n */\nexport function createSkillManager(options?: SkillManagerOptions): SkillManagerInterface {\n\treturn new SkillManager(options)\n}\n\n/**\n * Creates a richer turn context — an {@link AgentContextInterface} assembling a provider request\n * from the optional system prompt, the instruction registry, the skill registry, the memo set, the\n * workspace registry (the only document channel), the conversation registry that is its `messages`\n * source, the tool registry, and the active scope, which `build()` folds into the next turn's input.\n *\n * @remarks\n * `system` is the optional system prompt; `tools` / `instructions` / `skills` / `memos` /\n * `workspaces` are pre-built managers to reuse (empty ones are created when omitted, so\n * `context.workspaces` is always present); `scope` is the initial active filter (`undefined` ⇒ no\n * filtering, changeable afterwards through `context.apply(...)`). `conversations` is the message\n * source: a supplied manager's active conversation is reused as `messages`, and a default\n * conversation is added only when it has none (a fresh registry with one default conversation is\n * created when omitted). `build()` folds the system prompt, the scoped instructions, and the scoped\n * skills index into one leading `system` message, renders the scoped memos index and the active\n * workspace's scope-filtered text files (fenced) into one `user` context message after it, and\n * appends the active conversation's `view()` (attaching the active workspace's scope-filtered image\n * files' `base64` payload to the last user message), built\n * fresh each call; the scope never filters the conversation. The active workspace is the sole\n * document/image context. Tools are advertised structurally (through\n * `tools.definitions()`, scope-filtered by the loop), never serialized into the prompt.\n *\n * @param options - Optional `system` / `tools` / `instructions` / `skills` / `memos` /\n * `workspaces` / `conversations` / `scope` / `select` (see {@link AgentContextOptions})\n * @returns A working {@link AgentContextInterface}\n *\n * @example\n * ```ts\n * import { createAgentContext } from '@orkestrel/agent'\n *\n * const context = createAgentContext({ system: 'You are concise.' })\n * context.instructions.add({ name: 'tone', content: 'Be terse.' })\n * context.messages.add({ role: 'user', content: 'Hi' })\n * context.build() // [{ role: 'system', content: 'You are concise.\\n\\n## Instructions\\n\\nBe terse.' }, { role: 'user', content: 'Hi' }]\n * ```\n */\nexport function createAgentContext(options?: AgentContextOptions): AgentContextInterface {\n\treturn new AgentContext(options)\n}\n","import type { AuthorityNote } from './types.js'\n\n/**\n * Caps an {@link AgentInterface} turn's tool iterations by default — `10` context → provider →\n * tools cycles before the loop stops, so a model that keeps requesting tools can never loop\n * forever. Overridable per agent through `AgentOptions.limit`.\n */\nexport const DEFAULT_AGENT_LIMIT = 10\n\n/**\n * Holds the model-facing texts the loop writes after a tool name in a denial result — the\n * `denied` note for a deny with no reason, an evaluate throw, an invalid decision, or a tool\n * changed after its decision; the `refused` note for an ask the approver refused; and the\n * `unanswered` note for an ask no approver answered, frozen.\n */\nexport const AUTHORITY_NOTES: AuthorityNote = Object.freeze({\n\tdenied: 'it is not allowed. Do not call it again; answer with what you have.',\n\trefused: 'it was refused. Do not call it again; continue without it.',\n\tunanswered:\n\t\t'it needs approval and none was given. Do not call it again; tell the user it needs approval.',\n})\n\n/**\n * Holds the model-facing text the loop writes after a tool name in the denial result of a call\n * outside the active scope's tool allow-list or whose name no definition advertises this turn.\n */\nexport const SCOPE_NOTE =\n\t'it is not one of the listed tools. Use a listed tool or answer with what you have.'\n\n/**\n * Estimates the role and framing overhead added to each message by {@link estimateMessages}.\n */\nexport const MESSAGE_TOKEN_OVERHEAD = 4\n\n/**\n * Names the coarse, deliberately approximate per-image token cost\n * {@link import('./helpers.js').estimateMessages} charges for each attached image — `512`, because\n * a base64 payload's length is no reliable token proxy.\n *\n * @remarks\n * A base64 image payload's length is not a reliable token proxy (a vision model's actual image\n * token cost depends on resolution / tiling, not byte size), so this is a fixed, coarse\n * per-image estimate rather than a derivation from `image.length` — a planning heuristic, not an\n * exact count.\n */\nexport const IMAGE_TOKEN_ESTIMATE = 512\n","import type { AgentErrorCode, AgentResult } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport {\n\tINTRINSICS,\n\tholds,\n\tisObject,\n\tisFunction,\n\tisNumber,\n\tisString,\n\tisBoolean,\n} from '@orkestrel/contract'\n\n// A real error type, not a sentinel. An agent job treats a partial result\n// (a job committed early from an abort / budget / timeout) as a failure by default — the\n// queue / runner handler throws this so the Queue's retries + a Runner's fail-fast\n// engage. It carries the partial AgentResult so a caller (or a `retries: 0` enqueue that\n// rejects with it) can still inspect what accumulated.\n\n/**\n * Reports an {@link AgentInterface} run that ended {@link AgentResult.partial} under a\n * `partial` policy of `false` (the default) — thrown by an agent-job handler (a\n * `createAgentQueue` / `createAgentRunner` job), carrying the partial {@link AgentResult} so\n * the failure stays inspectable, and the machine-readable `code` `'PARTIAL'`.\n *\n * @remarks\n * A partial result means the agent was cancelled (an external `signal` abort, a queue /\n * runner abort threaded in, a `timeout` deadline, or an exhausted token `budget`) rather\n * than finishing naturally. For a durable job that is a failure by default: throwing this\n * lets the Queue's retries re-run the job and a Runner's fail-fast abort its siblings.\n * Set `partial: true` (see `AgentQueueOptions` / `AgentRunnerOptions`) to treat a\n * partial as success instead, in which case this is never thrown. Narrow a caught value\n * with {@link isAgentJobError} to read `partial`. `code` is the machine-readable condition\n * (`'PARTIAL'` — the only one this error reports), so a `catch` branches on it rather than on\n * the message string.\n */\nexport class AgentJobError extends Error {\n\t/** Names the machine-readable condition — `'PARTIAL'`: a job that ended partial under a disallowing policy. */\n\treadonly code = 'PARTIAL' as const\n\t/** Holds the partial {@link AgentResult} the cancelled job produced. */\n\treadonly partial: AgentResult\n\n\tconstructor(message: string, partial: AgentResult) {\n\t\tsuper(message)\n\t\tthis.name = 'AgentJobError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.AgentJobError'), { value: this })\n\t\tthis.partial = partial\n\t}\n}\n\n/**\n * Narrows an unknown caught value to an {@link AgentJobError} through its identity brand and declared members, so a\n * `catch` can recover its `partial` result.\n *\n * @param value - The value to test (typically a `catch` binding or a rejected enqueue)\n * @returns True if `value` is an {@link AgentJobError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tawait queue.enqueue(job) // retries: 0 → a partial rejects with the error\n * } catch (error) {\n * \tif (isAgentJobError(error)) keep(error.partial.content) // recover the partial content\n * }\n * ```\n */\nexport function isAgentJobError(value: unknown): value is AgentJobError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.AgentJobError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'PARTIAL')) return false\n\t\tconst partial = INTRINSICS.reflect.read(value, 'partial')\n\t\tif (!(isObject(partial) || isFunction(partial))) return false\n\t\tconst usage = INTRINSICS.reflect.read(partial, 'usage')\n\t\tconst stop = INTRINSICS.reflect.read(partial, 'stop')\n\t\tif (stop !== undefined) {\n\t\t\tif (!(isObject(stop) || isFunction(stop))) return false\n\t\t\tconst layer = INTRINSICS.reflect.read(stop, 'layer')\n\t\t\tconst spent = INTRINSICS.reflect.read(stop, 'usage')\n\t\t\tif (\n\t\t\t\t!isString(INTRINSICS.reflect.read(stop, 'id')) ||\n\t\t\t\t!(\n\t\t\t\t\tINTRINSICS.reflect.read(stop, 'estimate') === undefined ||\n\t\t\t\t\tisNumber(INTRINSICS.reflect.read(stop, 'estimate'))\n\t\t\t\t) ||\n\t\t\t\t!(\n\t\t\t\t\tspent === undefined ||\n\t\t\t\t\t((isObject(spent) || isFunction(spent)) &&\n\t\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(spent, key)),\n\t\t\t\t\t\t))\n\t\t\t\t)\n\t\t\t)\n\t\t\t\treturn false\n\t\t\tif (\n\t\t\t\tlayer !== 'model' &&\n\t\t\t\t!(\n\t\t\t\t\t(layer === 'meter' || layer === 'budget') &&\n\t\t\t\t\tisNumber(INTRINSICS.reflect.read(stop, 'max')) &&\n\t\t\t\t\tisNumber(INTRINSICS.reflect.read(stop, 'consumed'))\n\t\t\t\t)\n\t\t\t)\n\t\t\t\treturn false\n\t\t}\n\t\treturn (\n\t\t\tisString(INTRINSICS.reflect.read(partial, 'content')) &&\n\t\t\t(INTRINSICS.reflect.read(partial, 'thinking') === undefined ||\n\t\t\t\tisString(INTRINSICS.reflect.read(partial, 'thinking'))) &&\n\t\t\tisBoolean(INTRINSICS.reflect.read(partial, 'partial')) &&\n\t\t\t(usage === undefined ||\n\t\t\t\t((isObject(usage) || isFunction(usage)) &&\n\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(usage, key)),\n\t\t\t\t\t)))\n\t\t)\n\t})\n}\n\n// A real error type, not a sentinel. A run that would share an accounting instance or a\n// conversation with a live run would corrupt that accounting or interleave that thread, so\n// `stream()` throws this synchronously, before any state mutation or emit, rather than letting the\n// runs race. Carries a machine-readable `code` so a\n// `catch` branches on `error.code`, mirroring `ConversationError` in `conversations/errors.ts`.\n\n/**\n * Reports an `ask` on an authority that has no approver or that its approver refuses, an authority missing its fallback or a rule's verdict, tools set with no authority, a run sharing accounting or a conversation with a live run, a `limit` that is not a positive safe integer, a name absent from a registry pool, a strict run's answer that fails its schema, or a strict run whose request exceeds its window. Agent construction, the `stream` and `generate` methods, and registry accessors throw synchronously, before returning a handle or promise; a `'WINDOW'` refusal rejects the run's `result` instead, and an `'APPROVER'` refusal rejects the promise the `ask` method of an `Authority` or the `ask` handler of an `Approver` returns. Await the call inside `try`/`catch` to catch it.\n *\n * @remarks\n * `'APPROVER'` reports an `ask` on an `Authority` built with no `ask` handler, or an ask an\n * `Approver` refuses: a call framed while no ask waited for it, a second ask for a waiting call, or\n * arguments it cannot render. The agent loop reads it as an unanswered ask and denies the call. Its\n * `context` names the asked `tool`.\n * `'AUTHORITY'` reports an `Authority` built with no valid `fallback`, a rule with no valid `verdict`\n * (its `context` names the `rule` index), and an agent built with `tools` and no `authority`. Each\n * throws at construction, with the options a host passes to correct it in the message.\n * `'CONCURRENCY'` reports a run started while another run on the same agent is live, when the\n * agent carries a `window` (a shared context budget) or when the run's effective `budget` or\n * `allowance` is an instance a live run holds — the second run's `start()` would replace that\n * instance's signal and race its charges against the first run's — or when a live run is bound to\n * the run's conversation, whose turns the two runs would interleave; its `context` names the\n * `agent` id, and the `conversation` id for a shared conversation. For concurrent runs, use\n * separate agents, or one agent with no `window`, distinct budget and allowance instances per\n * run, and a distinct conversation per run.\n * `'LIMIT'` reports a construction or per-run `limit` that is not a positive safe integer,\n * including one a rehydrated job carries into `build`. `'REGISTRY'` reports a rehydration name\n * absent from its registry pool, on `provider` / `tool` / `authority` / `scheduler` / `build`,\n * whose `context` names the `category` and the `name`, and a job that names `tools` and no\n * `authority` at `build`, whose `context` names the `tools`. `'WINDOW'` reports a `strict` run whose\n * complete provider request is still over the `window` budget, equal to its `max` included, after\n * the one permitted fold or with no fold possible; it settles the run `error` before the provider\n * is called, and its `context` holds the `agent` id, the measured `consumed`, and the window `max`.\n * Narrow a caught value with {@link isAgentError} and branch on `error.code`.\n */\nexport class AgentError extends Error {\n\t/** Names the machine-readable condition — `'APPROVER'`: an `ask` on an authority that has no approver, or one its approver refuses; `'AUTHORITY'`: an authority missing its fallback or a rule's verdict, or tools set with no authority; `'CONCURRENCY'`: a run sharing accounting or a conversation with a live run; `'LIMIT'`: a `limit` that is not a positive safe integer; `'REGISTRY'`: a rehydration name absent from its registry pool; `'WINDOW'`: a strict run whose complete request is still over its window budget. */\n\treadonly code: AgentErrorCode\n\t/** Holds the values that locate the failure, such as the `agent` id of a refused run, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Creates the error with its code, message, and optional cause and context.\n\t *\n\t * @param code - The machine-readable {@link AgentErrorCode}\n\t * @param message - The human-readable description\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: AgentErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'AgentError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.AgentError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to an {@link AgentError} through its identity brand and declared members, so a `catch`\n * can branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is an {@link AgentError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tagent.stream()\n * } catch (error) {\n * \tif (isAgentError(error) && error.code === 'CONCURRENCY') useSeparateAgents()\n * }\n * ```\n */\nexport function isAgentError(value: unknown): value is AgentError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.AgentError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (\n\t\t\t!(\n\t\t\t\tcode === 'APPROVER' ||\n\t\t\t\tcode === 'AUTHORITY' ||\n\t\t\t\tcode === 'CONCURRENCY' ||\n\t\t\t\tcode === 'LIMIT' ||\n\t\t\t\tcode === 'REGISTRY' ||\n\t\t\t\tcode === 'SCHEMA' ||\n\t\t\t\tcode === 'WINDOW'\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tbooleanShape,\n\tjsonShape,\n\tliteralShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\trecordShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\nimport { messageInputShape, tokenUsageShape } from '../shapers.js'\n\n/**\n * Describes a durable agent job — the record `AgentRegistry.build` checks exactly, so a misspelled key\n * refuses instead of running an agent without it.\n *\n * @remarks\n * `children` holds JSON records, because each child crosses this shape at its own `build`. `limit`\n * reads as a number here and as a positive safe integer at the agent's construction, which owns that\n * refusal.\n */\nexport const agentJobShape = objectShape({\n\tprovider: stringShape(),\n\tmessages: optionalShape(arrayShape(messageInputShape)),\n\tsystem: optionalShape(stringShape()),\n\ttools: optionalShape(arrayShape(stringShape())),\n\tauthority: optionalShape(stringShape()),\n\tscheduler: optionalShape(stringShape()),\n\tlimit: optionalShape(numberShape()),\n\ttimeout: optionalShape(numberShape()),\n\tbudget: optionalShape(numberShape()),\n\tchildren: optionalShape(arrayShape(recordShape(jsonShape()))),\n}) satisfies ContractShape\n\n/**\n * Describes the agent frame wire — one exact object per `AgentFrame` arm, discriminated by\n * `category`.\n *\n * @remarks\n * A `tool` frame carries the tool `name` on success and nothing on failure; an `ask` frame carries\n * the minted `id`, the tool `name`, the decision's `zone`, and the `arguments` text; a `finish` frame\n * carries the result's `content`, `partial`, `usage`, and stop `layer`; an `error` frame carries\n * nothing. The guard refuses every extra member; the contract parser drops extra members.\n */\nexport const agentFrameShape = unionShape(\n\tobjectShape({ category: literalShape(['token']), content: stringShape() }),\n\tobjectShape({ category: literalShape(['think']), content: stringShape() }),\n\tobjectShape({\n\t\tcategory: literalShape(['tool']),\n\t\tsuccess: literalShape([true]),\n\t\tname: stringShape(),\n\t}),\n\tobjectShape({ category: literalShape(['tool']), success: literalShape([false]) }),\n\tobjectShape({\n\t\tcategory: literalShape(['usage']),\n\t\tusage: tokenUsageShape,\n\t\testimate: numberShape(),\n\t}),\n\tobjectShape({\n\t\tcategory: literalShape(['ask']),\n\t\tid: stringShape(),\n\t\tname: stringShape(),\n\t\tzone: stringShape(),\n\t\targuments: stringShape(),\n\t}),\n\tobjectShape({\n\t\tcategory: literalShape(['finish']),\n\t\tcontent: stringShape(),\n\t\tpartial: booleanShape(),\n\t\tusage: optionalShape(tokenUsageShape),\n\t\tlayer: optionalShape(literalShape(['meter', 'budget', 'model'])),\n\t}),\n\tobjectShape({ category: literalShape(['error']) }),\n) satisfies ContractShape\n\n/**\n * Describes the agent prompt wire — the `content` and optional `images` of one user turn, read by\n * the run's `prompt` option and by a page's request body.\n *\n * @remarks\n * The guard refuses every other member, a `role`, an `id`, and a message history included; the\n * contract parser drops extra members.\n */\nexport const agentPromptShape = objectShape({\n\tcontent: stringShape(),\n\timages: optionalShape(arrayShape(stringShape())),\n}) satisfies ContractShape\n\n/**\n * Describes the agent answer wire — the `id` of one `ask` frame and a boolean `approved`.\n *\n * @remarks\n * The guard refuses every extra member and an `approved` that is not a boolean; the contract parser\n * drops extra members.\n */\nexport const agentAnswerShape = objectShape({\n\tid: stringShape(),\n\tapproved: booleanShape(),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { agentAnswerShape, agentFrameShape, agentJobShape, agentPromptShape } from './shapers.js'\n\n/** Validates and projects agent frames at a JSON wire boundary. */\nexport const agentFrameContract = createContract(agentFrameShape)\n\n/** Validates and projects agent prompts at the run's `prompt` option and a page's request body. */\nexport const agentPromptContract = createContract(agentPromptShape)\n\n/** Checks a durable agent job exactly at `AgentRegistry.build`. */\nexport const agentJobContract = createContract(agentJobShape)\n\n/** Validates and projects a page's answers to `ask` frames at a JSON wire boundary. */\nexport const agentAnswerContract = createContract(agentAnswerShape)\n","/**\n * Holds one value a `Channel` buffers and the cell pushed after it.\n *\n * @remarks\n * A `Channel` keeps its unread values as a chain of these cells, from its `#head` cell to its\n * `#tail` cell, so no public signature names this class. The `#next` field is a latch: the\n * `link` method sets it once, and a later call keeps the first cell.\n */\nexport class ChannelCell<T> {\n\treadonly #value: T\n\t#next: ChannelCell<T> | undefined\n\n\t/**\n\t * Holds a value with no next cell.\n\t *\n\t * @param value - The value the cell holds\n\t */\n\tconstructor(value: T) {\n\t\tthis.#value = value\n\t}\n\n\t/**\n\t * Returns the value the cell holds.\n\t *\n\t * @returns The value\n\t */\n\tget value(): T {\n\t\treturn this.#value\n\t}\n\n\t/**\n\t * Returns the cell pushed after this one.\n\t *\n\t * @returns The next cell, or undefined before one is linked\n\t */\n\tget next(): ChannelCell<T> | undefined {\n\t\treturn this.#next\n\t}\n\n\t/**\n\t * Links the cell pushed after this one, once.\n\t *\n\t * @param cell - The cell pushed after this one\n\t */\n\tlink(cell: ChannelCell<T>): void {\n\t\tthis.#next ??= cell\n\t}\n}\n","import type { ChannelInterface } from './types.js'\nimport { ChannelCell } from './ChannelCell.js'\n\n/**\n * Buffers chunks in a minimal unbounded async channel — the eager pump writes them in (`push`)\n * and ends it (`close` / `fail`) regardless of consumption, while a consumer reads them back live\n * through the `drain` async-iterator. Decoupling write from read is what lets a producer make\n * progress without a consumer pulling, and it is why an agent's `result` settles whether or not\n * its `events` are drained.\n *\n * @remarks\n * A waiting `drain` call parks its void resolver in the `#waiting` field. The `push` method links a\n * cell after the `#tail` cell, and the `close` and `fail` methods flag the end; each then fires and\n * clears every parked resolver, so each parked reader wakes, reads the `#head` cell, and either\n * takes the next chunk, returns (on `close`), throws (on `fail`), or parks again when another\n * reader took the chunk first. Taking a chunk reads its value and moves `#head` to the next cell\n * synchronously, so each value reaches exactly one reader, a push and a take each cost constant\n * time, and every taken cell but the newest is released at once; the `#tail` field holds the newest\n * cell until the next push links after it, so a channel retains at most one taken value. A reader\n * suspended at its `yield` expression holds only the value it took, never a cell, so the bound on\n * retained values covers both the channel's fields and a suspended reader.\n * A `return()` issued while a read is parked waits for the next `push`, `close`, or `fail`.\n * The parked read takes a pushed value before that `return()` settles.\n *\n * @example\n * ```ts\n * import { Channel } from '@orkestrel/agent'\n *\n * const channel = new Channel<number>()\n * channel.push(1)\n * channel.close()\n * for await (const value of channel.drain()) {\n * \tvalue // 1\n * }\n * ```\n */\nexport class Channel<T> implements ChannelInterface<T> {\n\t#head: ChannelCell<T> | undefined\n\t#tail: ChannelCell<T> | undefined\n\t#waiting: ReadonlyArray<() => void> = []\n\t#closed = false\n\t#failure: { error: unknown } | undefined\n\n\tpush(value: T): void {\n\t\tconst cell = new ChannelCell(value)\n\t\tthis.#tail?.link(cell)\n\t\tthis.#tail = cell\n\t\tthis.#head ??= cell\n\t\tthis.#signal()\n\t}\n\n\tclose(): void {\n\t\tthis.#closed = true\n\t\tthis.#signal()\n\t}\n\n\tfail(error: unknown): void {\n\t\t// First failure wins; a later close / fail can't override a recorded error.\n\t\tif (this.#failure === undefined) this.#failure = { error }\n\t\tthis.#closed = true\n\t\tthis.#signal()\n\t}\n\n\tasync *drain(): AsyncGenerator<T, void> {\n\t\tfor (;;) {\n\t\t\t// Yield everything buffered before checking for end, so a close / fail that\n\t\t\t// arrives alongside the last chunks still delivers those chunks first.\n\t\t\twhile (this.#head !== undefined) {\n\t\t\t\tconst value = this.#head.value\n\t\t\t\tthis.#head = this.#head.next\n\t\t\t\tyield value\n\t\t\t}\n\t\t\tif (this.#failure !== undefined) throw this.#failure.error\n\t\t\tif (this.#closed) return\n\t\t\tawait this.#park()\n\t\t}\n\t}\n\n\t// Wake every parked reader, not one: with concurrent readers, a single woken reader would\n\t// leave the rest parked past a `close` / `fail`. The list is cleared before firing, so a\n\t// reader that re-parks arms the next wait. A `signal` with no parked reader is a no-op (the\n\t// buffer / flags are already set, so the next `drain` pass reads them without parking).\n\t#signal(): void {\n\t\tconst waiting = this.#waiting\n\t\tthis.#waiting = []\n\t\tfor (const wake of waiting) wake()\n\t}\n\n\t#park(): Promise<void> {\n\t\treturn new Promise<void>((resolve) => {\n\t\t\tthis.#waiting = [...this.#waiting, resolve]\n\t\t})\n\t}\n}\n","import type { AuthorityDecision, AuthorityVerdict } from './types.js'\nimport { isString, objectOf } from '@orkestrel/contract'\n\n/**\n * Checks whether a value is an authority verdict: `allow`, `ask`, or `deny`.\n *\n * @param value - The unknown verdict candidate\n * @returns True if the value names a verdict; false otherwise\n *\n * @example\n * ```ts\n * isAuthorityVerdict('ask') // true\n * isAuthorityVerdict('maybe') // false\n * ```\n */\nexport function isAuthorityVerdict(value: unknown): value is AuthorityVerdict {\n\treturn value === 'allow' || value === 'ask' || value === 'deny'\n}\n\n/**\n * Checks whether a value is an authority decision: an object whose `zone` is a string, whose\n * `verdict` is `allow`, `ask`, or `deny`, and whose `reason` is absent or a string.\n *\n * @remarks\n * Total: each member is read once, unknown members are admitted, and a non-object, a 0.0.34\n * `{ zone, allowed }` decision, an unknown verdict, a throwing getter, and a revoked or throwing\n * `Proxy` return false rather than throw.\n *\n * @param value - The unknown decision candidate\n * @returns True if the value satisfies the decision contract; false otherwise\n *\n * @example\n * ```ts\n * isAuthorityDecision({ zone: 'default', verdict: 'ask', reason: 'confirm' }) // true\n * isAuthorityDecision({ zone: 'default', allowed: true }) // false\n * ```\n */\nexport function isAuthorityDecision(value: unknown): value is AuthorityDecision {\n\treturn objectOf({ zone: isString, verdict: isAuthorityVerdict, reason: isString }, ['reason'])(\n\t\tvalue,\n\t)\n}\n","import type { Message } from '../types.js'\nimport type { ProviderRequest } from '../providers/index.js'\nimport type { ConversationInterface } from '../conversations/index.js'\nimport type {\n\tAgentChunk,\n\tAgentFrame,\n\tAgentInterface,\n\tAgentJobInput,\n\tAgentRegistryInterface,\n\tAgentResult,\n\tAgentRunResult,\n\tAgentStop,\n\tAgentStreamInterface,\n\tAgentTurn,\n\tApproverInterface,\n\tAuthorityDecision,\n\tAuthorityOptions,\n\tAuthorityRule,\n\tMessageMeasure,\n\tRequestMeasure,\n} from './types.js'\nimport type { BudgetInterface, TokenUsage } from '@orkestrel/budget'\nimport type { AuditFault, JSONValue } from '@orkestrel/contract'\nimport type { QueueContext, QueueOptions } from '@orkestrel/queue'\nimport type { ToolCall, ToolDefinition, ToolResult } from '@orkestrel/tool'\nimport { isBudget, isBudgetAmount, isBudgetError } from '@orkestrel/budget'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tcanonicalStringify,\n\tContractError,\n\tcreateContract,\n\tisBoolean,\n\tisFiniteNumber,\n\tisObject,\n\tisString,\n\tobjectOf,\n\tparseJSON,\n\tparseJSONValue,\n\tpreview,\n\tschemaToShape,\n} from '@orkestrel/contract'\nimport { AUTHORITY_NOTES, IMAGE_TOKEN_ESTIMATE, MESSAGE_TOKEN_OVERHEAD } from './constants.js'\nimport { AgentError, AgentJobError } from './errors.js'\nimport { isConversation } from '../conversations/index.js'\nimport { isAuthorityDecision, isAuthorityVerdict } from './validators.js'\nimport { estimateTokens, hasCalls, renderJSON } from '../helpers.js'\nimport { isPositiveSafeInteger } from '../validators.js'\n\n/**\n * Projects an unknown value onto a fresh, exact `JSONValue` representation of an\n * {@link AgentResult} — capturing each structural field once through a total boundary,\n * accepting conforming accessors and inherited properties, preserving fractional usage counts,\n * dropping extras, and resolving `undefined` for a malformed field, a negative or non-finite usage\n * number, a throwing getter, or a malformed or revoked proxy.\n *\n * @remarks\n * This is a total projection that reads untrusted input. Each structural field is captured once\n * through Contract's sanctioned exception boundary, so conforming accessors and inherited\n * properties are supported while a throwing getter or revoked proxy returns `undefined`.\n * Present usage counts must be finite and nonnegative; fractional values are preserved, not\n * normalized. The result is exact `{ content, thinking?, usage?, partial, stop? }`. A present\n * `stop` is read once, then its `layer`, `id`, `max`, `consumed`, `estimate`, and `usage` once each\n * in that order, `max` and `consumed` only on a `meter` or `budget` arm; the arm is rebuilt\n * exactly, omitting absent readings, and a malformed `stop` refuses the whole value. Extra input\n * properties are dropped while a fresh exact plain object is rebuilt and deep-gated through\n * {@link import('@orkestrel/contract').parseJSONValue}.\n *\n * @param value - The unknown value to project\n * @returns A fresh JSON value containing only AgentResult fields, or `undefined` when invalid\n *\n * @example\n * ```ts\n * import { agentResultToJSON } from '@orkestrel/agent'\n *\n * agentResultToJSON({ content: 'done', usage: { prompt: 2, completion: 1, total: 3 }, partial: false })\n * // { content: 'done', usage: { prompt: 2, completion: 1, total: 3 }, partial: false }\n * ```\n */\nexport function agentResultToJSON(value: unknown): JSONValue | undefined {\n\tconst captured = attempt(() => {\n\t\tif (!isObject(value)) return undefined\n\n\t\tconst content = Reflect.get(value, 'content')\n\t\tconst thinking = Reflect.get(value, 'thinking')\n\t\tconst usage = Reflect.get(value, 'usage')\n\t\tconst partial = Reflect.get(value, 'partial')\n\t\tconst stop = Reflect.get(value, 'stop')\n\t\tif (!isString(content) || !isBoolean(partial)) return undefined\n\t\tif (thinking !== undefined && !isString(thinking)) return undefined\n\t\tconst projectedUsage = usage === undefined ? undefined : tokenUsageToJSON(usage)\n\t\tif (usage !== undefined && projectedUsage === undefined) return undefined\n\t\tif (stop !== undefined && !isObject(stop)) return undefined\n\n\t\tconst layer = isObject(stop) ? Reflect.get(stop, 'layer') : undefined\n\t\tconst id = isObject(stop) ? Reflect.get(stop, 'id') : undefined\n\t\tconst ranked = layer === 'meter' || layer === 'budget'\n\t\tconst max = isObject(stop) && ranked ? Reflect.get(stop, 'max') : undefined\n\t\tconst consumed = isObject(stop) && ranked ? Reflect.get(stop, 'consumed') : undefined\n\t\tconst estimate = isObject(stop) ? Reflect.get(stop, 'estimate') : undefined\n\t\tconst spent = isObject(stop) ? Reflect.get(stop, 'usage') : undefined\n\t\tconst projectedSpent = spent === undefined ? undefined : tokenUsageToJSON(spent)\n\t\tconst measured = {\n\t\t\t...(isFiniteNumber(estimate) ? { estimate } : {}),\n\t\t\t...(projectedSpent === undefined ? {} : { usage: projectedSpent }),\n\t\t}\n\t\tconst projectedStop =\n\t\t\t!isString(id) ||\n\t\t\t(estimate !== undefined && !isFiniteNumber(estimate)) ||\n\t\t\t(spent !== undefined && projectedSpent === undefined)\n\t\t\t\t? undefined\n\t\t\t\t: ranked && isFiniteNumber(max) && isFiniteNumber(consumed)\n\t\t\t\t\t? { layer, id, max, consumed, ...measured }\n\t\t\t\t\t: layer === 'model'\n\t\t\t\t\t\t? { layer, id, ...measured }\n\t\t\t\t\t\t: undefined\n\t\tif (stop !== undefined && projectedStop === undefined) return undefined\n\n\t\treturn {\n\t\t\tcontent,\n\t\t\t...(thinking === undefined ? {} : { thinking }),\n\t\t\t...(projectedUsage === undefined ? {} : { usage: projectedUsage }),\n\t\t\tpartial,\n\t\t\t...(projectedStop === undefined ? {} : { stop: projectedStop }),\n\t\t}\n\t})\n\n\treturn captured.success ? parseJSONValue(captured.value) : undefined\n}\n\n/**\n * Projects an unknown value onto a fresh, exact `TokenUsage` — capturing `prompt`, `completion`, and\n * `total` once each through a total boundary, preserving fractional counts, dropping extras, and\n * resolving `undefined` for a non-object, a missing, negative, or non-finite count, a throwing getter,\n * or a malformed or revoked proxy.\n *\n * @remarks\n * The capture runs inside Contract's sanctioned exception boundary, so conforming accessors and\n * inherited properties are read while a throwing getter or a revoked proxy returns `undefined`.\n * Counts are not normalized. A caller that treats an absent usage as valid checks for `undefined`\n * before it projects, because this function refuses `undefined` like any other non-object.\n *\n * @param value - The unknown value to project\n * @returns A fresh `{ prompt, completion, total }`, or `undefined` when the value is not a finite nonnegative usage triple\n *\n * @example\n * ```ts\n * import { tokenUsageToJSON } from '@orkestrel/agent'\n *\n * tokenUsageToJSON({ prompt: 2, completion: 1, total: 1.5, cached: 4 }) // { prompt: 2, completion: 1, total: 1.5 }\n * tokenUsageToJSON({ prompt: 2, completion: -1, total: 1 }) // undefined\n * tokenUsageToJSON({ prompt: 2, completion: Infinity, total: 3 }) // undefined\n * ```\n */\nexport function tokenUsageToJSON(value: unknown): TokenUsage | undefined {\n\tconst captured = attempt(() => {\n\t\tif (!isObject(value)) return undefined\n\t\tconst prompt = Reflect.get(value, 'prompt')\n\t\tconst completion = Reflect.get(value, 'completion')\n\t\tconst total = Reflect.get(value, 'total')\n\t\treturn isBudgetAmount(prompt) && isBudgetAmount(completion) && isBudgetAmount(total)\n\t\t\t? { prompt, completion, total }\n\t\t\t: undefined\n\t})\n\treturn captured.success ? captured.value : undefined\n}\n\n/**\n * Projects one {@link AgentChunk} onto the {@link AgentFrame} a page receives, member by member, or\n * returns `undefined` for an `ask` chunk.\n *\n * @remarks\n * A `tool` chunk projects to `{ category: 'tool', success: true, name }` only when its result\n * succeeded, and to `{ category: 'tool', success: false }` otherwise, so neither the tool's value nor\n * a denial's text crosses. An `ask` chunk returns `undefined`, because only the approver holding the\n * ask knows its id: read its frame through the `frame` method of an {@link ApproverInterface}.\n *\n * @param chunk - The chunk the run yielded\n * @returns The chunk's frame, or `undefined` for an `ask` chunk\n *\n * @example\n * ```ts\n * agentChunkToFrame({ category: 'token', content: 'Hi' }) // { category: 'token', content: 'Hi' }\n * ```\n */\nexport function agentChunkToFrame(chunk: AgentChunk): AgentFrame | undefined {\n\tswitch (chunk.category) {\n\t\tcase 'token':\n\t\t\treturn { category: 'token', content: chunk.content }\n\t\tcase 'think':\n\t\t\treturn { category: 'think', content: chunk.content }\n\t\tcase 'tool':\n\t\t\treturn chunk.result.success === true\n\t\t\t\t? { category: 'tool', success: true, name: chunk.call.name }\n\t\t\t\t: { category: 'tool', success: false }\n\t\tcase 'usage':\n\t\t\treturn {\n\t\t\t\tcategory: 'usage',\n\t\t\t\tusage: {\n\t\t\t\t\tprompt: chunk.usage.prompt,\n\t\t\t\t\tcompletion: chunk.usage.completion,\n\t\t\t\t\ttotal: chunk.usage.total,\n\t\t\t\t},\n\t\t\t\testimate: chunk.estimate,\n\t\t\t}\n\t\tcase 'ask':\n\t\t\treturn undefined\n\t}\n}\n\n/**\n * Projects a settled {@link AgentResult} onto the terminal `finish` frame a page receives.\n *\n * @remarks\n * Keeps `content`, `partial`, `usage` when present, and the stop's `layer` when a layer stopped the\n * run; drops `thinking` and every other stop member.\n *\n * @param result - The run's settled result\n * @returns A fresh `finish` frame\n *\n * @example\n * ```ts\n * agentResultToFrame({ content: 'Done.', partial: false }) // { category: 'finish', content: 'Done.', partial: false }\n * ```\n */\nexport function agentResultToFrame(\n\tresult: AgentResult,\n): Extract<AgentFrame, { readonly category: 'finish' }> {\n\treturn {\n\t\tcategory: 'finish',\n\t\tcontent: result.content,\n\t\tpartial: result.partial,\n\t\t...(result.usage === undefined\n\t\t\t? {}\n\t\t\t: {\n\t\t\t\t\tusage: {\n\t\t\t\t\t\tprompt: result.usage.prompt,\n\t\t\t\t\t\tcompletion: result.usage.completion,\n\t\t\t\t\t\ttotal: result.usage.total,\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t...(result.stop === undefined ? {} : { layer: result.stop.layer }),\n\t}\n}\n\n/**\n * Projects a run's live handle onto the frames a page receives — one frame per projected chunk, in\n * order, then exactly one terminal `finish` or `error`, last.\n *\n * @remarks\n * Reads `events` and `result` once each. An `ask` chunk crosses as the frame `approver` holds for\n * its call, and every other chunk through {@link agentChunkToFrame}; after the drain ends, the\n * settled result crosses through {@link agentResultToFrame}, and any throw ends the frames with a\n * bare `{ category: 'error' }`. It never calls `abort` itself, and drained to the end it yields\n * exactly one terminal frame, last. A consumer's `return()` while it waits at a chunk's frame\n * closes the drain of `events`, whose `return()` aborts the run, and no terminal frame is yielded.\n * When the run aborts while an ask waits and the reader pulls the `ask` chunk afterwards, the\n * approver holds no waiting ask, marks the call, and frames nothing, so no `ask` frame crosses and\n * the frames end `finish` with `partial: true`.\n *\n * @param stream - The run's live handle\n * @param approver - The approver wired as the authority's `ask` handler; with `undefined` every\n * `ask` chunk is dropped, so pass `undefined` only when an in-process handler answers the asks.\n * An `Approver` wired as the handler but passed as `undefined` leaves each ask waiting with no\n * frame until the run `timeout` or an abort.\n * @returns An async generator of the run's frames\n *\n * @example\n * ```ts\n * const approver = createApprover()\n * const authority = createAuthority({\n * \trules,\n * \tfallback: { zone: 'default', verdict: 'deny' },\n * \task: approver.ask,\n * })\n * const agent = createAgent(provider, { tools, authority })\n * for await (const frame of agentStreamToFrames(agent.stream({ prompt }), approver)) {\n * \tsend(JSON.stringify(frame))\n * }\n * ```\n */\nexport async function* agentStreamToFrames(\n\tstream: AgentStreamInterface,\n\tapprover: ApproverInterface | undefined,\n): AsyncGenerator<AgentFrame, void, undefined> {\n\tconst events = stream.events\n\tconst result = stream.result\n\ttry {\n\t\tfor await (const chunk of events) {\n\t\t\tconst frame =\n\t\t\t\tchunk.category === 'ask' ? approver?.frame(chunk.call) : agentChunkToFrame(chunk)\n\t\t\tif (frame !== undefined) yield frame\n\t\t}\n\t\tyield agentResultToFrame(await result)\n\t} catch {\n\t\tyield { category: 'error' }\n\t}\n}\n\n/**\n * Infers which of a run's three failure layers stopped it — the meter allowance, a cost budget, or\n * the model's own token limit — and returns the stop record with the final call's readings, or\n * `undefined` when no layer stopped the run.\n *\n * @remarks\n * The layer that stopped the final provider call first wins. A model-truncated final answer whose\n * call returned unaborted is a `model` stop, even when that call's reconciled usage then exhausted a\n * budget or a cancel then landed before the run settled. Otherwise a `BudgetError` reason is a\n * `meter` stop when the allowance is exhausted, the reason is the allowance signal's own reason, its\n * context carries the allowance's `id` and `max`, and either the allowance is a separate instance or\n * the run had aborted when the call returned; any other `BudgetError` is a `budget` stop. The\n * identity test tells an allowance and a budget that share an `id` apart, and the readings test\n * tells a parent budget linked through the allowance's `signal` option apart from the allowance. An\n * external cancel, a deadline, or an `abort()` reason yields `undefined`. Two inputs still read as\n * the meter's: a charge a caller makes to the run's shared allowance from its own code while a call\n * streams, and a parent with the allowance's own `id` and `max` that aborted before `start()` when\n * the allowance then exhausts.\n *\n * @param reason - The run signal's abort reason (`undefined` while it is unaborted)\n * @param allowance - The run's meter allowance, or `undefined` for a run with no meter layer\n * @param budget - The run's cost budget, or `undefined` for an unbudgeted run\n * @param provider - The provider's trace `id`, named by a `model` stop\n * @param turn - The run's final turn record, or `undefined` when the run took no turn\n * @returns The stop record, or `undefined` when no layer stopped the run\n *\n * @example\n * ```ts\n * const allowance = createTokenBudget({ id: 'meter', max: 2 })\n * allowance.start()\n * allowance.consume({ prompt: 0, completion: 3, total: 3 })\n * inferStop(allowance.signal.reason, allowance, undefined, 'ollama', undefined)\n * // { layer: 'meter', id: 'meter', max: 2, consumed: 3 }\n * ```\n */\nexport function inferStop(\n\treason: unknown,\n\tallowance: BudgetInterface<TokenUsage> | undefined,\n\tbudget: BudgetInterface<TokenUsage> | undefined,\n\tprovider: string,\n\tturn: AgentTurn | undefined,\n): AgentStop | undefined {\n\tconst measured = {\n\t\t...(turn?.estimate === undefined ? {} : { estimate: turn.estimate }),\n\t\t...(turn?.usage === undefined ? {} : { usage: turn.usage }),\n\t}\n\tconst interrupted = turn?.interrupted === true\n\tif (!interrupted && turn?.truncated === true) return { layer: 'model', id: provider, ...measured }\n\tif (!isBudgetError(reason)) return undefined\n\tconst metered =\n\t\tallowance !== undefined &&\n\t\tallowance.exhausted &&\n\t\treason === allowance.signal.reason &&\n\t\treason.context.id === allowance.id &&\n\t\treason.context.max === allowance.max &&\n\t\t(allowance !== budget || interrupted)\n\treturn {\n\t\tlayer: metered ? 'meter' : 'budget',\n\t\tid: reason.context.id,\n\t\tmax: reason.context.max,\n\t\tconsumed: reason.context.consumed,\n\t\t...measured,\n\t}\n}\n\n/**\n * Estimates the context-token footprint of a batch of messages — each message's content plus\n * {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD}, a tool-call JSON estimate, a thinking estimate, and\n * {@link import('./constants.js').IMAGE_TOKEN_ESTIMATE} for each attached image. The {@link estimateRequest} message term\n * for an agent's context budget (the\n * {@link import('./types.js').AgentOptions} `window`), a provider-independent approximation for conforming messages rather than an exact tokenizer count.\n *\n * @remarks\n * Sums, per message, {@link estimateTokens} over its `content` (the `ceil(length / 4)` character\n * heuristic) plus {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD} (a fixed per-message\n * role/framing overhead) plus, when present, {@link estimateTokens} over its JSON-stringified\n * `calls` plus, when present, {@link estimateTokens} over its `thinking` plus `images.length * `{@link import('./constants.js').IMAGE_TOKEN_ESTIMATE} (a coarse,\n * deliberately-approximate per-image cost — a base64 length is not a token proxy). Deterministic\n * and provider-free — the same messages always yield the same estimate, with an empty batch `0`.\n * It is the message term of the request estimate an agent's auto-compaction context budget\n * measures; a caller wanting a sharper count supplies its own `consumer` to `createBudget`\n * instead. A `calls` list JSON cannot carry (a circular or `BigInt`\n * `ToolCall.arguments`) contributes a fixed\n * {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD} (the same per-message overhead scale)\n * instead of an estimate of the (unreachable) serialized length.\n *\n * @param messages - The messages to estimate (a turn's appended assistant + tool messages)\n * @returns The summed estimated token count (`0` when empty)\n *\n * @example\n * ```ts\n * estimateMessages([]) // 0\n * estimateMessages([{ id: '1', role: 'user', content: 'hello' }]) // 6 (2 content + 4 overhead)\n * ```\n */\nexport function estimateMessages(messages: readonly Message[]): number {\n\treturn messages.reduce((sum, message) => {\n\t\tconst measure = measureMessage(message)\n\t\treturn (\n\t\t\tsum + measure.content + measure.uniform + measure.calls + measure.thinking + measure.images\n\t\t)\n\t}, 0)\n}\n\n/**\n * Measures the disjoint estimate components of one message — its content, framing, rendered calls,\n * thinking, and attached images — as the same heuristic units {@link estimateMessages} sums.\n *\n * @remarks\n * The result is a structural estimate, not a tokenizer allocation. `id` and `role` are joins back to\n * the message, not token components, and a provider's template effects and the estimation error are\n * not individually attributed. `content` is `ceil(length / 4)`, `uniform` is\n * {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD}, `calls` is the estimate of the JSON-rendered\n * calls (`0` when the message carries none, the framing overhead when they cannot be rendered),\n * `thinking` is the estimate of the thinking (`0` when absent), and `images` is the attached image\n * count times {@link import('./constants.js').IMAGE_TOKEN_ESTIMATE}. The message is not mutated.\n *\n * @param message - The message to measure\n * @returns The message's identity and its estimate components\n *\n * @example\n * ```ts\n * measureMessage({ id: '1', role: 'user', content: 'hello' })\n * // { id: '1', role: 'user', content: 2, uniform: 4, calls: 0, thinking: 0, images: 0 }\n * ```\n */\nexport function measureMessage(message: Message): MessageMeasure {\n\tconst rendered = hasCalls(message) ? renderJSON(message.calls) : ''\n\tconst calls = rendered === undefined ? MESSAGE_TOKEN_OVERHEAD : estimateTokens(rendered)\n\treturn {\n\t\tid: message.id,\n\t\trole: message.role,\n\t\tcontent: estimateTokens(message.content),\n\t\tuniform: MESSAGE_TOKEN_OVERHEAD,\n\t\tcalls,\n\t\tthinking: message.thinking === undefined ? 0 : estimateTokens(message.thinking),\n\t\timages: (message.images?.length ?? 0) * IMAGE_TOKEN_ESTIMATE,\n\t}\n}\n\n/**\n * Estimates the context-token footprint of a complete provider request — its messages through\n * {@link estimateMessages}, plus the rendered advertised `tools`, plus the rendered\n * `options.schema`. The `consumer` estimator an agent's `window` budget measures every request\n * through, a provider-independent heuristic rather than an exact tokenizer count.\n *\n * @remarks\n * Adds {@link estimateTokens} over the JSON rendering of `tools` and of `options.schema` to the\n * message estimate. An absent or empty `tools` list and an absent or empty `schema` add `0`, so a\n * request that advertises neither equals {@link estimateMessages} of its messages. A value JSON\n * cannot carry contributes a fixed {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD}. The\n * estimate counts JSON characters, not a provider's template: the chat template's own tokens, how\n * a provider encodes tool definitions, and the `think` flag are not counted, and no tokenizer\n * runs. A model's real prompt count can exceed it, so choose the window `max` with headroom\n * measured against the provider.\n *\n * @param request - The {@link ProviderRequest} to estimate: messages, advertised tools, call options\n * @returns The estimated token count (`0` for an empty request)\n *\n * @example\n * ```ts\n * estimateRequest({ messages: [{ id: '1', role: 'user', content: 'hello' }] }) // 6, equal to estimateMessages\n * ```\n */\nexport function estimateRequest(request: ProviderRequest): number {\n\tconst measure = measureRequest(request)\n\treturn measure.messages.reduce(\n\t\t(sum, message) =>\n\t\t\tsum + message.content + message.uniform + message.calls + message.thinking + message.images,\n\t\tmeasure.tools + measure.schema,\n\t)\n}\n\n/**\n * Measures the disjoint estimate components of a complete provider request — each message through\n * {@link measureMessage}, plus the rendered advertised `tools` and `options.schema` — as the same\n * heuristic units {@link estimateRequest} sums.\n *\n * @remarks\n * The result is a structural estimate, not a tokenizer allocation; a provider's template effects, how\n * it encodes tool definitions, the `think` flag, and the estimation error are not individually\n * attributed. `messages` keeps the request's message order, duplicate ids included. `tools` and\n * `schema` are each `0` when absent or empty and the estimate of their JSON rendering otherwise, or\n * the {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD} when JSON cannot carry them. The request\n * is not mutated.\n *\n * @param request - The {@link ProviderRequest} to measure: messages, advertised tools, call options\n * @returns The per-message components and the tools and schema estimates\n *\n * @example\n * ```ts\n * measureRequest({ messages: [{ id: '1', role: 'user', content: 'hello' }] })\n * // { messages: [{ id: '1', role: 'user', content: 2, uniform: 4, calls: 0, thinking: 0, images: 0 }], tools: 0, schema: 0 }\n * ```\n */\nexport function measureRequest(request: ProviderRequest): RequestMeasure {\n\tconst tools = request.tools !== undefined && request.tools.length > 0 ? request.tools : undefined\n\tconst schema = request.options?.schema\n\tconst [toolsEstimate = 0, schemaEstimate = 0] = [\n\t\ttools,\n\t\tschema !== undefined && Object.keys(schema).length > 0 ? schema : undefined,\n\t].map((part) => {\n\t\tif (part === undefined) return 0\n\t\tconst rendered = renderJSON(part)\n\t\treturn rendered === undefined ? MESSAGE_TOKEN_OVERHEAD : estimateTokens(rendered)\n\t})\n\treturn {\n\t\tmessages: request.messages.map((message) => measureMessage(message)),\n\t\ttools: toolsEstimate,\n\t\tschema: schemaEstimate,\n\t}\n}\n\n/**\n * Runs one rehydrated agent and applies the partial-as-configurable-failure policy — a partial\n * run throws an {@link import('./errors.js').AgentJobError} unless the `partial` policy allows\n * it, and a natural finish resolves. The shared job-handler step `createAgentQueue` and\n * `createAgentRunner` both settle each job through, so the policy can never diverge between\n * them.\n *\n * @remarks\n * A turn that committed partial (a cancel — abort / budget / timeout) is by default a\n * failure, so it throws an {@link import('./errors.js').AgentJobError} carrying the partial\n * (the Queue's retries + a Runner's fail-fast then engage); the `partial` policy resolves\n * it as success instead. A natural finish always resolves with its result.\n *\n * @param agent - The rehydrated {@link AgentInterface} to run to its {@link AgentResult}\n * @param partial - The partial policy. If `true`, a partial result resolves as success; if\n * `false` (the default policy), a partial result throws an {@link AgentJobError}\n * @returns The agent's {@link AgentResult} (a natural finish, or a partial one under the\n * `partial` policy)\n * @throws {AgentJobError} Thrown when the run ended partial and the `partial` policy is `false`\n *\n * @example\n * ```ts\n * const result = await settleAgentJob(registry.build(input, signal), false)\n * ```\n */\nexport async function settleAgentJob(\n\tagent: AgentInterface,\n\tpartial: boolean,\n): Promise<AgentResult> {\n\tconst result = await agent.generate()\n\tif (result.partial && !partial) throw new AgentJobError('agent job ended partial', result)\n\treturn result\n}\n\n/**\n * Handles one queued agent job by rehydrating it through a registry with the queue\n * attempt's signal, then applying the shared partial-result policy.\n *\n * @param registry - The registry that rehydrates the serializable job\n * @param partial - The partial policy. If `true`, a partial result resolves; if `false`, it throws\n * @param input - The serializable agent job\n * @param context - The queue attempt whose signal bounds the agent\n * @returns The settled agent result\n *\n * @example\n * ```ts\n * const result = await handleAgentQueueJob(registry, false, input, context)\n * ```\n */\nexport function handleAgentQueueJob(\n\tregistry: AgentRegistryInterface,\n\tpartial: boolean,\n\tinput: AgentJobInput,\n\tcontext: QueueContext,\n): Promise<AgentResult> {\n\treturn settleAgentJob(registry.build(input, context.signal), partial)\n}\n\n/**\n * Assembles the settled {@link AgentResult} from a run's {@link AgentRunResult} — `thinking`,\n * `usage`, and `stop` are carried only when the run surfaced them, and the loop-internal\n * `exhausted` flag is left out.\n *\n * @remarks\n * Pure and total. An absent optional is omitted rather than stored as `undefined` (the\n * present-when-given convention the message store follows), so a settled result JSON\n * round-trips without an explicit `undefined` field. The keys follow `content`, `partial`,\n * `thinking`, `usage`, then `stop`. `exhausted` is loop bookkeeping and does not reach the public\n * result — the `exhaust` event carries it instead.\n *\n * @param outcome - The run's settled outcome\n * @returns The public {@link AgentResult}\n *\n * @example\n * ```ts\n * assembleResult({ content: 'hi', thinking: undefined, usage: undefined, partial: false, exhausted: false, stop: undefined })\n * // { content: 'hi', partial: false }\n * ```\n */\nexport function assembleResult(outcome: AgentRunResult): AgentResult {\n\treturn {\n\t\tcontent: outcome.content,\n\t\tpartial: outcome.partial,\n\t\t...(outcome.thinking === undefined ? {} : { thinking: outcome.thinking }),\n\t\t...(outcome.usage === undefined ? {} : { usage: outcome.usage }),\n\t\t...(outcome.stop === undefined ? {} : { stop: outcome.stop }),\n\t}\n}\n\n/**\n * Synthesizes the denial {@link ToolResult} a blocked call is fed back with — the call's `id` /\n * `name` keyed back, carrying a denial `error` instead of a value.\n *\n * @remarks\n * Pure and total. The `error` is `<name> did not run: <reason>`, where the model-supplied name\n * is JSON-encoded and capped at `PREVIEW_LIMIT` characters by\n * {@link import('@orkestrel/contract').preview}, and an absent `reason` reads the `denied` note\n * of {@link AUTHORITY_NOTES}. There is no `value`, so the agent loop feeds it back exactly like\n * a tool error and the model can react to it.\n *\n * @param call - The denied {@link ToolCall}\n * @param reason - A rule's `deny` reason or a note, or `undefined` for the `denied` note\n * @returns The failure-arm {@link ToolResult}\n *\n * @example\n * ```ts\n * denyCall({ id: '1', name: 'drop', arguments: {} }, 'read-only mode')\n * // { success: false, id: '1', name: 'drop', error: '\"drop\" did not run: read-only mode' }\n * ```\n */\nexport function denyCall(call: ToolCall, reason: string | undefined): ToolResult {\n\treturn {\n\t\tsuccess: false,\n\t\tid: call.id,\n\t\tname: call.name,\n\t\terror: `${preview(call.name)} did not run: ${reason ?? AUTHORITY_NOTES.denied}`,\n\t}\n}\n\n/**\n * Renders a tool call's arguments as escaped canonical JSON text — sorted keys, with every control,\n * format, line-separator, and paragraph-separator code point written as `\\uXXXX` — or returns\n * `undefined` when they cannot be rendered.\n *\n * @remarks\n * Keeps the semantics of `canonicalStringify` from `@orkestrel/contract`, so `NaN` renders as `null`\n * and an `undefined` member is dropped, as `JSON.stringify` does; the exact round trip through\n * `JSON.parse` is promised for JSON arguments only, which a provider's parsed call carries. A cycle,\n * a throwing getter, or a value JSON cannot encode returns `undefined`. Each escaped code point is\n * written over its UTF-16 code units, so bidi overrides, zero-width characters, U+0085, U+2028,\n * U+2029, U+FEFF, and tag characters appear only encoded. The text carries no length cap.\n *\n * @param value - The call's arguments\n * @returns The rendered text, or `undefined` when the arguments cannot be rendered\n *\n * @example\n * ```ts\n * renderArguments({ path: 'a\\u202Eb', mode: 'r' }) // '{\"mode\":\"r\",\"path\":\"a\\\\u202eb\"}'\n * ```\n */\nexport function renderArguments(value: Readonly<Record<string, unknown>>): string | undefined {\n\tconst rendered = attempt(() => canonicalStringify(value))\n\tif (!rendered.success || rendered.value === undefined) return undefined\n\treturn rendered.value.replace(/[\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}]/gu, (match) =>\n\t\tArray.from(\n\t\t\t{ length: match.length },\n\t\t\t(_, index) => `\\\\u${match.charCodeAt(index).toString(16).padStart(4, '0')}`,\n\t\t).join(''),\n\t)\n}\n\n/**\n * Applies the `consequential` floor to one {@link AuthorityDecision} — an `allow` for a tool\n * whose advertised definition is annotated `consequential` becomes an `ask`.\n *\n * @remarks\n * Pure and total, and monotone: it raises `allow` to `ask` and never loosens a verdict, so an\n * `ask` and a `deny` return with the same verdict. The result is always a fresh decision.\n * An unannotated or absent definition leaves the verdict unchanged.\n *\n * @param decision - The authority's decision on one call\n * @param definition - The advertised definition named by the call, or `undefined` when none is\n * @returns A fresh decision carrying the raised or unchanged verdict\n *\n * @example\n * ```ts\n * const definition = { name: 'write', description: 'Write a file', annotations: { consequential: true } }\n * resolveDecision({ zone: 'default', verdict: 'allow' }, definition) // { zone: 'default', verdict: 'ask' }\n * ```\n */\nexport function resolveDecision(\n\tdecision: AuthorityDecision,\n\tdefinition: ToolDefinition | undefined,\n): AuthorityDecision {\n\tconst raised = decision.verdict === 'allow' && definition?.annotations?.consequential === true\n\treturn { ...decision, verdict: raised ? 'ask' : decision.verdict }\n}\n\n/**\n * Checks a run's final answer against its schema: the content must parse as JSON and satisfy the\n * schema, or the call throws a coded error carrying the faults.\n *\n * @remarks\n * The schema enters through the lenient `schemaToShape`, which widens every keyword it cannot express\n * and never narrows, because a provider constrains its own output and a false refusal would discard a\n * good answer. The check is `is`, because `explain` alone passes what `parse` would repair. Each call\n * compiles once, and a run has one final answer.\n *\n * @param content - The final answer's text\n * @param schema - The run's JSON Schema\n * @throws {AgentError} Thrown when the content is not JSON, misses the schema, or the schema does not compile (code `'SCHEMA'`), with the faults under `context.faults` for the first two\n *\n * @example\n * ```ts\n * requireAnswer('{\"a\":5}', { type: 'object', properties: { a: { type: 'number' } } })\n * requireAnswer('nope', { type: 'object' }) // throws AgentError ('SCHEMA')\n * ```\n */\nexport function requireAnswer(content: string, schema: Readonly<Record<string, unknown>>): void {\n\tconst compiled = attempt(() => createContract(schemaToShape(schema)))\n\tif (!compiled.success) {\n\t\tthrow new AgentError(\n\t\t\t'SCHEMA',\n\t\t\t'the run schema does not compile; pass a JSON Schema built from plain values',\n\t\t\t{ cause: compiled.error },\n\t\t)\n\t}\n\tconst parsed = parseJSON(content)\n\tconst faults: readonly AuditFault[] =\n\t\tparsed === undefined\n\t\t\t? [\n\t\t\t\t\t{\n\t\t\t\t\t\treason: 'type',\n\t\t\t\t\t\tpath: [],\n\t\t\t\t\t\texpected: 'json',\n\t\t\t\t\t\treceived: preview(content),\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t: compiled.value.is(parsed)\n\t\t\t\t? []\n\t\t\t\t: compiled.value.audit(parsed)\n\tif (faults.length > 0) {\n\t\tthrow new AgentError(\n\t\t\t'SCHEMA',\n\t\t\t`the final answer does not satisfy the run schema; pass strict: false to createAgent to take the raw content from a provider that ignores schemas\\n${describeFaults(faults, compiled.value.schema)}`,\n\t\t\t{ context: { faults } },\n\t\t)\n\t}\n}\n\n/**\n * Returns an authority's no-match decision after checking the options carry one, or throws a coded\n * error naming the two fallbacks a host chooses between.\n *\n * @remarks\n * A rules list says nothing about the calls it does not match, so no fallback is assumed: a denylist\n * and an allowlist differ only here. The check is {@link isAuthorityDecision}, a total guard, so a\n * missing options object and a malformed fallback refuse alike.\n *\n * @param options - The authority's options as a host passed them\n * @returns The fallback, narrowed to an {@link AuthorityDecision}\n * @throws {AgentError} Thrown when the options carry no valid `fallback` (code `'AUTHORITY'`)\n *\n * @example\n * ```ts\n * requireFallback({ fallback: { zone: 'default', verdict: 'deny' } }) // { zone: 'default', verdict: 'deny' }\n * ```\n */\nexport function requireFallback(options: AuthorityOptions): AuthorityDecision {\n\tconst fallback = attempt(() => (isObject(options) ? Reflect.get(options, 'fallback') : undefined))\n\tif (fallback.success && isAuthorityDecision(fallback.value)) return fallback.value\n\tthrow new AgentError(\n\t\t'AUTHORITY',\n\t\t\"an authority needs a fallback for the calls no rule matches; pass fallback: { zone: 'default', verdict: 'deny' } for an allowlist, or fallback: { zone: 'default', verdict: 'allow' } for a denylist\",\n\t)\n}\n\n/**\n * Returns an authority's rules after checking each one names a verdict, or throws a coded error naming\n * the first rule that does not.\n *\n * @remarks\n * Absent rules return an empty list. A rule's verdict decides a tool call, so a rule with none is\n * refused rather than read as an allow. The error's `context` carries the rule's index as `rule`.\n *\n * @param rules - The ordered rules, or `undefined` for none\n * @returns The rules unchanged, or an empty list when none were supplied\n * @throws {AgentError} Thrown when a rule has no valid `verdict` (code `'AUTHORITY'`)\n *\n * @example\n * ```ts\n * requireRules([{ match: () => true, zone: 'files', verdict: 'ask' }]).length // 1\n * ```\n */\nexport function requireRules(\n\trules: readonly AuthorityRule[] | undefined,\n): readonly AuthorityRule[] {\n\tif (rules === undefined) return []\n\t// `Array.from` visits a hole as an undefined rule, which the guard refuses; `findIndex` reads it too.\n\tconst refused = Array.from(rules).findIndex(\n\t\t(rule) => !objectOf({ verdict: isAuthorityVerdict })(rule),\n\t)\n\tif (refused !== -1) {\n\t\tthrow new AgentError(\n\t\t\t'AUTHORITY',\n\t\t\t`rule ${refused} has no verdict; set verdict: 'allow' | 'ask' | 'deny'`,\n\t\t\t{ context: { rule: refused } },\n\t\t)\n\t}\n\treturn rules\n}\n\n/**\n * Consumes a reported usage against a budget over what was already charged, so a turn's total\n * draw is never less than the report and nothing is charged twice.\n *\n * @remarks\n * The full `prompt` count is consumed because no earlier charge covers it. Each of `completion`\n * and `total` is consumed less `charged`, floored at 0. An estimate above the report is not\n * refunded. The usage must already be sanitized: a\n * non-finite field yields a non-finite charge, which the installed `Budget` refuses by throwing\n * a `range` `ContractError`. Without a budget the call consumes nothing.\n *\n * @param budget - The budget to consume against, or `undefined` for an unmetered run\n * @param usage - The sanitized usage the provider reported\n * @param charged - The completion tokens already consumed this turn\n *\n * @example\n * ```ts\n * const budget = createBudget<TokenUsage>({ max: 1000, consumer: (usage) => usage.total })\n * chargeUsage(budget, { prompt: 20, completion: 30, total: 50 }, 10)\n * budget.consumed // 40\n * ```\n */\nexport function chargeUsage(\n\tbudget: BudgetInterface<TokenUsage> | undefined,\n\tusage: TokenUsage,\n\tcharged: number,\n): void {\n\tbudget?.consume({\n\t\tprompt: usage.prompt,\n\t\tcompletion: Math.max(0, usage.completion - charged),\n\t\ttotal: Math.max(0, usage.total - charged),\n\t})\n}\n\n/**\n * Returns a tool-iteration cap after checking it is a positive safe integer, or throws a coded error.\n *\n * @remarks\n * The check is {@link import('../validators.js').isPositiveSafeInteger}, so `0`, `-0`, `NaN`, a\n * fraction, a negative, and either infinity are refused, because a run of no turns asks the provider\n * nothing and answers nothing. The error's `context` carries the refused value as `limit`.\n *\n * @param limit - The tool-iteration cap to check\n * @returns The cap unchanged\n * @throws {AgentError} Thrown when `limit` is not a positive safe integer (code `'LIMIT'`)\n *\n * @example\n * ```ts\n * requireLimit(3) // 3\n * requireLimit(0) // throws AgentError ('LIMIT')\n * ```\n */\nexport function requireLimit(limit: number): number {\n\tif (!isPositiveSafeInteger(limit)) {\n\t\tthrow new AgentError(\n\t\t\t'LIMIT',\n\t\t\t'a limit must be a positive safe integer, because a run of no turns answers nothing',\n\t\t\t{ context: { limit } },\n\t\t)\n\t}\n\treturn limit\n}\n\n/**\n * Returns a run's budget or allowance after checking it implements `BudgetInterface`, or throws a\n * coded error before the run starts it.\n *\n * @remarks\n * The check is `isBudget` from `@orkestrel/budget`, a total guard. `undefined` passes, because an\n * absent bound is no bound. The error's `context` carries the option path, the limit\n * `'BudgetInterface'`, and a preview of the refused value.\n *\n * @param value - The effective `budget` or `allowance`, or `undefined`\n * @param key - The option the value came from, named in the error's path\n * @returns The value unchanged\n * @throws {ContractError} Thrown when `value` is defined and does not implement `BudgetInterface` (code `'placement'`)\n *\n * @example\n * ```ts\n * requireBudget(createTokenBudget({ max: 10 }), 'budget') // the budget\n * requireBudget({ consume() {} }, 'allowance') // throws ContractError ('placement')\n * ```\n */\nexport function requireBudget(\n\tvalue: BudgetInterface<TokenUsage> | undefined,\n\tkey: 'budget' | 'allowance',\n): BudgetInterface<TokenUsage> | undefined {\n\tif (value === undefined || isBudget(value)) return value\n\tthrow new ContractError(`Agent: ${key} must implement BudgetInterface`, {\n\t\tcode: 'placement',\n\t\tcontext: { path: ['options', key], limit: 'BudgetInterface', received: preview(value) },\n\t})\n}\n\n/**\n * Returns a run's `conversation` option after checking it implements `ConversationInterface`, or\n * throws a coded error before the run starts.\n *\n * @remarks\n * The check is {@link isConversation}, a total guard. `undefined` passes, because an absent option\n * binds the run to the active conversation. The error's `context` carries the option path, the\n * limit `'ConversationInterface'`, and a preview of the refused value.\n *\n * @param value - The run's `conversation` option, or `undefined`\n * @returns The value unchanged\n * @throws {ContractError} Thrown when `value` is defined and does not implement `ConversationInterface` (code `'placement'`)\n *\n * @example\n * ```ts\n * requireConversation(createConversation()) // the conversation\n * requireConversation({ id: 'c1' }) // throws ContractError ('placement')\n * ```\n */\nexport function requireConversation(value: unknown): ConversationInterface | undefined {\n\tif (value === undefined || isConversation(value)) return value\n\tthrow new ContractError('Agent: conversation must implement ConversationInterface', {\n\t\tcode: 'placement',\n\t\tcontext: {\n\t\t\tpath: ['options', 'conversation'],\n\t\t\tlimit: 'ConversationInterface',\n\t\t\treceived: preview(value),\n\t\t},\n\t})\n}\n\n/**\n * Renders a tool's output as the string content of its tool message, or returns `undefined` when JSON\n * cannot carry it.\n *\n * @remarks\n * A string passes through unchanged and `undefined` renders as `''`. Any other value renders as\n * its `JSON.stringify` text, byte for byte, when JSON can carry it. A value JSON cannot carry (a\n * `BigInt`, a cycle, a symbol, a function, a throwing `toJSON`) returns `undefined`, which\n * {@link settleToolResult} turns into a tool failure.\n *\n * @param value - The tool's output (`ToolSuccess.value`, which may be `undefined`)\n * @returns The message content, or `undefined` when JSON cannot carry the value\n *\n * @example\n * ```ts\n * renderToolValue('sunny') // 'sunny'\n * renderToolValue({ count: 2 }) // '{\"count\":2}'\n * renderToolValue(undefined) // ''\n * renderToolValue(10n) // undefined\n * ```\n */\nexport function renderToolValue(value: unknown): string | undefined {\n\tif (typeof value === 'string') return value\n\tif (value === undefined) return ''\n\treturn renderJSON(value)\n}\n\n/**\n * Settles one dispatched result into the result the loop reports and the content of its tool message,\n * turning a success whose value JSON cannot carry into a tool failure the model reads.\n *\n * @remarks\n * A failure passes through with its `error` as the content. A success renders through\n * {@link renderToolValue}. A success that renders to `undefined` becomes the failure arm with the\n * call's `id` and `name`, whose `error` names the tool and the remedy.\n *\n * @param result - The result the tool manager returned for one call\n * @returns The result to report and the tool message content\n *\n * @example\n * ```ts\n * settleToolResult({ success: true, id: '1', name: 'read', value: { count: 2 } }).content // '{\"count\":2}'\n * settleToolResult({ success: true, id: '1', name: 'read', value: 10n }).outcome.success // false\n * ```\n */\nexport function settleToolResult(result: ToolResult): {\n\treadonly outcome: ToolResult\n\treadonly content: string\n} {\n\tif (!result.success) return { outcome: result, content: result.error }\n\tconst content = renderToolValue(result.value)\n\tif (content !== undefined) return { outcome: result, content }\n\tconst error = `${preview(result.name)} returned a value JSON cannot carry; return strings, numbers, booleans, null, arrays, and plain records`\n\treturn {\n\t\toutcome: { success: false, id: result.id, name: result.name, error },\n\t\tcontent: error,\n\t}\n}\n\n/**\n * Returns a registered value or throws when its name is absent.\n * @param pool - The named values\n * @param category - The registry category named in an error\n * @param name - The requested key\n * @returns The registered value\n * @throws {AgentError} Thrown when the name is absent, with code REGISTRY, the name encoded and capped in its text because a tool refusal reaches a model, and a `context` naming the `category` and the `name`.\n * @example\n * ```ts\n * requireEntry(new Map([['main', provider]]), 'provider', 'main') // provider\n * ```\n */\nexport function requireEntry<T>(pool: ReadonlyMap<string, T>, category: string, name: string): T {\n\tconst value = pool.get(name)\n\tif (value === undefined) {\n\t\tthrow new AgentError('REGISTRY', `unknown ${category}: ${preview(name)}`, {\n\t\t\tcontext: { category, name },\n\t\t})\n\t}\n\treturn value\n}\n\n/**\n * Extracts the present concurrency, retry, and deadline options for an agent job queue.\n * @param options - The queue options to project\n * @returns The present queue options, omitting absent keys\n * @example\n * ```ts\n * extractQueueOptions({ concurrency: 2 }) // { concurrency: 2 }\n * ```\n */\nexport function extractQueueOptions(\n\toptions: Pick<QueueOptions<AgentJobInput, AgentResult>, 'concurrency' | 'retries' | 'timeout'>,\n): Pick<QueueOptions<AgentJobInput, AgentResult>, 'concurrency' | 'retries' | 'timeout'> {\n\tconst { concurrency, retries, timeout } = options\n\treturn {\n\t\t...(concurrency === undefined ? {} : { concurrency }),\n\t\t...(retries === undefined ? {} : { retries }),\n\t\t...(timeout === undefined ? {} : { timeout }),\n\t}\n}\n","import type {\n\tAgentChunk,\n\tAgentEventMap,\n\tAgentInterface,\n\tAgentOptions,\n\tAgentResult,\n\tAgentRunOptions,\n\tAgentStatus,\n\tAgentStreamInterface,\n\tAgentTurn,\n\tAuthorityDecision,\n\tAuthorityInterface,\n\tAgentRunResult,\n} from './types.js'\nimport type { ProviderInterface, ProviderRequest, ProviderResult } from '../providers/index.js'\nimport type { AgentContextInterface, Selection } from '../contexts/index.js'\nimport type { ConversationInterface, Section } from '../conversations/index.js'\nimport type { Message, ThinkingReplay } from '../types.js'\nimport type { AbortInterface } from '@orkestrel/abort'\nimport type { BudgetInterface, MeterInterface, TokenUsage } from '@orkestrel/budget'\nimport type { Result } from '@orkestrel/contract'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport type {\n\tToolCall,\n\tToolDefinition,\n\tToolInterface,\n\tToolManagerInterface,\n\tToolResult,\n} from '@orkestrel/tool'\nimport type { SchedulerInterface } from '@orkestrel/workflow'\nimport type { TimeoutInterface } from '@orkestrel/timeout'\nimport { createAbort } from '@orkestrel/abort'\nimport { createTokenMeter } from '@orkestrel/budget'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tContractError,\n\terrorToMessage,\n\tfailure,\n\tisObject,\n\tpreview,\n\tsuccess,\n} from '@orkestrel/contract'\nimport { createTimeout } from '@orkestrel/timeout'\nimport { Emitter } from '@orkestrel/emitter'\nimport { AgentContext } from '../contexts/index.js'\nimport { raceSignal } from '../conversations/index.js'\nimport { AUTHORITY_NOTES, DEFAULT_AGENT_LIMIT, SCOPE_NOTE } from './constants.js'\nimport { agentPromptContract } from './contracts.js'\nimport { AgentError } from './errors.js'\nimport { Channel } from './Channel.js'\nimport {\n\tassembleResult,\n\tchargeUsage,\n\tdenyCall,\n\tinferStop,\n\trequireAnswer,\n\trequireBudget,\n\trequireConversation,\n\trequireLimit,\n\tresolveDecision,\n\tsettleToolResult,\n} from './helpers.js'\nimport { isAuthorityDecision } from './validators.js'\nimport {\n\testimateTokens,\n\tfilterAllowList,\n\tsanitizeUsage,\n\tstripThinking,\n\tsumUsage,\n} from '../helpers.js'\nimport {\n\tisProviderAbortError,\n\tProviderError,\n\treadProviderDelta,\n\trequireProviderResult,\n} from '../providers/index.js'\n\n/**\n * Composes a {@link ProviderInterface}, an {@link AgentContext}, and a\n * {@link ToolManagerInterface} into a bounded context → provider → tools → repeat turn, exposed\n * as both a one-shot `generate` and a live `stream` that share one private run — bounded by the\n * run `signal`, the `timeout`, and the `budget` folded through `AbortSignal.any`, paced by\n * `scheduler`, with tool iteration capped at `limit`.\n *\n * @remarks\n * - **One loop, two faces.** A single private async generator (`#execute`) drives the\n * whole run. `stream` drives it eagerly into a private {@link Channel} and settles `result`\n * from the outcome the generator returns, through one `.then` — so `result` settles\n * whether or not the live `events` are drained; `generate` awaits that same\n * settled `result` — so the two can never diverge.\n * - **The turn.** `#execute` builds the provider input once, then runs up to `limit` turns,\n * each appending one readonly {@link AgentTurn} record: drive `provider.stream(...)`,\n * yielding each content delta as a `token` chunk and charging its estimate to the\n * `allowance` when one is set; reconcile the turn's usage against the `budget`, before a\n * genuine provider error rejects the run too, and yield a `usage` chunk; if the model\n * requested tools, yield an `ask` chunk per asked call after its approver is entered,\n * append the assistant turn, pass them to the tool manager's `execute`\n * method, yield a `tool` chunk per call, append each tool result message, and continue;\n * otherwise append the final assistant message and stop. The run's content,\n * thinking, usage, and stop derive from the turn records. Each assistant message stores its\n * call's non-empty thinking.\n * Provider calls and prompt estimates apply the provider's `replay` policy to the working\n * array; an absent policy means `'none'`.\n * - **Bounded.** Each run arms one cancel through `createAbort({ signal: AbortSignal.any([\n * …]) })` folding the external `signal`, the `timeout` deadline, and the `budget` and\n * `allowance` signals; `abort()` fires it. Any trip stops the loop and commits a partial result\n * (the `result` promise resolves, never rejects, on a cancel) — only a genuine\n * provider / tool error rejects.\n * - **Paced + capped.** The loop calls the `scheduler` object's `yield` method between turns when given; tool\n * iteration is capped at `limit`.\n * - **Two observation surfaces.** The pull {@link AgentChunk} stream carries per-token\n * deltas (+ usage/ask/tool chunks); the push {@link emitter} ({@link AgentEventMap}) carries\n * lifecycle + usage/tool/allow/ask/deny moments for fire-and-forget observers. Every event is\n * emitted directly, after the relevant state transition / settle; the emitter isolates a\n * listener throw and routes it to its `error` handler (the `error` option), so a buggy\n * observer can never escape into / reorder / corrupt the settle-once loop — observation is\n * purely a side-channel.\n *\n * @example\n * ```ts\n * const agent = new Agent(provider, { system: 'You are concise.' })\n * agent.context.messages.add({ role: 'user', content: 'Say hi.' })\n * const result = await agent.generate()\n * ```\n */\nexport class Agent implements AgentInterface {\n\treadonly #id: string\n\treadonly #provider: ProviderInterface\n\treadonly #replay: ThinkingReplay\n\treadonly #context: AgentContextInterface\n\treadonly #limit: number\n\treadonly #timeoutMs: number | undefined\n\treadonly #budget: BudgetInterface<TokenUsage> | undefined\n\t// The meter layer's allowance: the budget each content delta's estimate is charged against while\n\t// a call streams. `undefined` ⇒ no meter layer; the estimate is still measured for `usage`.\n\treadonly #allowance: BudgetInterface<TokenUsage> | undefined\n\treadonly #scheduler: SchedulerInterface | undefined\n\treadonly #signal: AbortSignal | undefined\n\t// The gate consulted for each admitted call; `undefined` only on an agent built with no `tools`,\n\t// where a tool a host adds later through `context.tools` is denied rather than run.\n\treadonly #authority: AuthorityInterface | undefined\n\t// The context budget that admits each provider request — its `consumer` is a token estimator\n\t// over the complete `ProviderRequest`, its `max` the context window. `#admit` re-measures the\n\t// exact request (replay-filtered messages, advertised tools, per-call options) against it before\n\t// every dispatch; `undefined` ⇒ disabled, and the loop never calls `#admit`. Reset (`clear()`)\n\t// at run entry so no stale `consumed` carries across runs / a conversation switch. Not the hard\n\t// cost `budget` ceiling — when the request reaches its `max` this compacts + continues\n\t// (non-fatal on a summarizer throw, futile-guarded) and never aborts; only `strict` refuses.\n\treadonly #window: BudgetInterface<ProviderRequest> | undefined\n\t// True by default: a summarizer failure during automatic compaction rethrows (after the `fault`\n\t// event) instead of skipping compaction and continuing over-window, a request still over the\n\t// window after the permitted fold is refused with a WINDOW error before dispatch, and an answer\n\t// that fails the run's `schema` is refused with a SCHEMA error.\n\treadonly #strict: boolean\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into the loop. No\n\t// `destroy()`: the Agent holds no other teardownable resources, and an `Emitter` owns\n\t// only listener sets (no timers / handles), so it is reclaimed with the agent — there\n\t// is no leak to clear, and adding lifecycle the entity does not otherwise need is avoided.\n\treadonly #emitter: Emitter<AgentEventMap>\n\t// The label the last run settled on. `status` derives the live answer from `#runs`, so an\n\t// overlapping run can never report `done` while another is still in flight.\n\t#settled: AgentStatus = 'idle'\n\t// Every in-flight run's abort handle, mapped to the distinct budget and allowance instances that\n\t// run holds and the conversation it is bound to — a run adds its entry on `stream()` and its\n\t// settle removes it, so `abort()` fires every live run, not only the most recent, and the\n\t// concurrency guards read which instances and conversations are held. Per-run, never a single\n\t// shared slot a later `stream()` could clobber: `generate`/`stream` are reusable and may overlap,\n\t// and each run must cancel independently (its own `stream.abort()` fires its own handle;\n\t// `agent.abort()` fires them all).\n\treadonly #runs = new Map<\n\t\tAbortInterface,\n\t\t{\n\t\t\treadonly budgets: ReadonlyArray<BudgetInterface<TokenUsage>>\n\t\t\treadonly conversation: ConversationInterface\n\t\t}\n\t>()\n\n\tconstructor(provider: ProviderInterface, options?: AgentOptions) {\n\t\tif (options?.tools !== undefined && options.authority === undefined) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'AUTHORITY',\n\t\t\t\t\"tools are set and no authority is; pass authority: createAuthority({ fallback: { zone: 'default', verdict: 'allow' } }) to allow every call, or rules with a deny fallback\",\n\t\t\t)\n\t\t}\n\t\tthis.#limit = requireLimit(options?.limit ?? DEFAULT_AGENT_LIMIT)\n\t\tthis.#id = crypto.randomUUID()\n\t\tthis.#provider = provider\n\t\tthis.#replay = provider.replay ?? 'none'\n\t\tthis.#context = new AgentContext({\n\t\t\t...(options?.system === undefined ? {} : { system: options.system }),\n\t\t\t...(options?.tools === undefined ? {} : { tools: options.tools }),\n\t\t\t...(options?.instructions === undefined ? {} : { instructions: options.instructions }),\n\t\t\t...(options?.skills === undefined ? {} : { skills: options.skills }),\n\t\t\t...(options?.memos === undefined ? {} : { memos: options.memos }),\n\t\t\t...(options?.workspaces === undefined ? {} : { workspaces: options.workspaces }),\n\t\t\t...(options?.scope === undefined ? {} : { scope: options.scope }),\n\t\t\t...(options?.conversations === undefined ? {} : { conversations: options.conversations }),\n\t\t\t...(options?.select === undefined ? {} : { select: options.select }),\n\t\t})\n\t\tthis.#timeoutMs = options?.timeout\n\t\tthis.#budget = options?.budget\n\t\tthis.#allowance = options?.allowance\n\t\tthis.#scheduler = options?.scheduler\n\t\tthis.#signal = options?.signal\n\t\tthis.#authority = options?.authority\n\t\tthis.#window = options?.window\n\t\tthis.#strict = options?.strict ?? true\n\t\tthis.#emitter = new Emitter<AgentEventMap>(options)\n\t}\n\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\tget emitter(): EmitterInterface<AgentEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget status(): AgentStatus {\n\t\t// Derived, never stored: a live run outranks any settled label, so overlapping runs can\n\t\t// never report `done` while one is still in flight, and a settled `error` is not erased\n\t\t// by a later run's `done` until that later run itself settles.\n\t\treturn this.#runs.size > 0 ? 'running' : this.#settled\n\t}\n\n\tget context(): AgentContextInterface {\n\t\treturn this.#context\n\t}\n\n\tgenerate(options?: AgentRunOptions): Promise<AgentResult> {\n\t\t// Zero loop logic of its own — start the stream (whose eager drive settles `result`\n\t\t// independently of any consumer) and await that same settled result, so `generate`\n\t\t// and `stream` can never diverge. No manual drain needed: the drive runs regardless.\n\t\treturn this.stream(options).result\n\t}\n\n\tstream(options?: AgentRunOptions): AgentStreamInterface {\n\t\t// The prompt is checked exactly before every other guard, so a refused prompt starts nothing.\n\t\tconst prompt = options?.prompt\n\t\tif (prompt !== undefined && !agentPromptContract.is(prompt)) {\n\t\t\t// A contract error's context declares no faults, so they travel in the text it carries.\n\t\t\tconst faults = attempt(() =>\n\t\t\t\tdescribeFaults(agentPromptContract.audit(prompt), agentPromptContract.schema),\n\t\t\t)\n\t\t\tthrow new ContractError(\n\t\t\t\t`Agent: prompt must be an exact AgentPrompt${faults.success ? `\\n${faults.value}` : ''}`,\n\t\t\t\t{\n\t\t\t\t\tcode: 'placement',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\tpath: ['options', 'prompt'],\n\t\t\t\t\t\tlimit: 'AgentPrompt',\n\t\t\t\t\t\treceived: preview(prompt),\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\t\t// Resolve effective per-run bounds — a per-run override wins, else the construction\n\t\t// default. `limit`, `budget`, and `allowance` also thread into `#execute` (the loop bound,\n\t\t// the reconcile, and the mid-stream meter); each budget is the same instance folded into\n\t\t// `#combineSignals`, so its trip both aborts the run and is the budget the run charges.\n\t\tconst limit = options?.limit === undefined ? this.#limit : requireLimit(options.limit)\n\t\t// Both bounds are checked before the concurrency guard and before either `start()`, so a\n\t\t// refused bound leaves the other untouched and nothing emitted.\n\t\tconst budget = requireBudget(options?.budget ?? this.#budget, 'budget')\n\t\tconst allowance = requireBudget(options?.allowance ?? this.#allowance, 'allowance')\n\t\t// The distinct accounting instances this run holds, cost budget first: an allowance that is\n\t\t// the budget itself is held, started, and folded once.\n\t\tconst held = [budget, allowance].filter(\n\t\t\t(one, index, all): one is BudgetInterface<TokenUsage> =>\n\t\t\t\tone !== undefined && all.indexOf(one) === index,\n\t\t)\n\t\t// Concurrency guard: a `window` is one context budget every run clears and measures, and a\n\t\t// budget instance carries one signal that `start()` replaces — a second live run on either\n\t\t// would race the first run's accounting or orphan its bound. Thrown synchronously, before\n\t\t// any state mutation or emit, so a sequential/awaited caller is never affected and runs\n\t\t// holding distinct budget instances with no `window` still overlap.\n\t\tif (\n\t\t\tthis.#runs.size > 0 &&\n\t\t\t(this.#window !== undefined ||\n\t\t\t\t[...this.#runs.values()].some((live) => live.budgets.some((one) => held.includes(one))))\n\t\t) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'CONCURRENCY',\n\t\t\t\t'a live run on this agent holds the window or this budget or allowance instance; use separate agents or distinct instances per run',\n\t\t\t\t{ context: { agent: this.#id } },\n\t\t\t)\n\t\t}\n\t\t// The run binds its conversation here, at the call: a later `switch`, `open`, `remove`,\n\t\t// `clear`, or same-id `add` moves the pointer, never this run. One live run per conversation —\n\t\t// two would interleave their turns in one thread — refused synchronously, before any `start()`\n\t\t// or emit, like the guard above.\n\t\tconst conversation = requireConversation(options?.conversation) ?? this.#context.messages\n\t\tif ([...this.#runs.values()].some((live) => live.conversation === conversation)) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'CONCURRENCY',\n\t\t\t\t'a live run on this agent holds this conversation; await it or bind the run to another conversation',\n\t\t\t\t{ context: { agent: this.#id, conversation: conversation.id } },\n\t\t\t)\n\t\t}\n\t\t// The deadline is created unarmed: its signal keeps its identity when `start()` arms it, so\n\t\t// it can be composed first and armed only after nothing left can throw.\n\t\tconst timeoutMs = options?.timeout ?? this.#timeoutMs\n\t\tconst timeout = timeoutMs === undefined ? undefined : createTimeout({ ms: timeoutMs })\n\t\t// The prompt joins the conversation from its named members after every guard, before `start()`.\n\t\tif (prompt !== undefined) {\n\t\t\tconversation.add({\n\t\t\t\trole: 'user',\n\t\t\t\tcontent: prompt.content,\n\t\t\t\t...(prompt.images === undefined ? {} : { images: prompt.images }),\n\t\t\t})\n\t\t}\n\t\t// `start()` replaces each held budget's signal, so it precedes the composition that reads it.\n\t\tfor (const one of held) one.start()\n\t\t// Fold every present bound (external signal + a per-run signal + deadline + budget +\n\t\t// allowance) into one cancel the run races against; this run's own `abort()` fires this handle.\n\t\tconst signal = this.#combineSignals(timeout, budget, allowance, options?.signal)\n\t\tconst abort = createAbort(signal === undefined ? {} : { signal })\n\t\ttimeout?.start()\n\t\tthis.#runs.set(abort, { budgets: held, conversation })\n\t\t// Observe the run begin — after the run joins `#runs` (which is what `status` derives\n\t\t// `running` from), so a swallowed listener throw can't perturb the state the drive is\n\t\t// about to change.\n\t\tthis.#emitter.emit('start', this.id)\n\t\tconst channel = new Channel<AgentChunk>()\n\t\tconst settled = Promise.withResolvers<AgentResult>()\n\t\t// Kick off the eager drive synchronously (not lazily on first `events` pull): it drives\n\t\t// `#execute` into the channel, and the run settles from its outcome regardless of whether\n\t\t// anyone drains `events`. The per-run `think` / `schema` preferences ride through to\n\t\t// `provider.stream`; `limit` / `budget` / `allowance` ride through as the effective run bounds.\n\t\tvoid this.#drive(\n\t\t\tabort,\n\t\t\tconversation,\n\t\t\tchannel,\n\t\t\toptions?.think,\n\t\t\toptions?.schema,\n\t\t\tlimit,\n\t\t\tbudget,\n\t\t\tallowance,\n\t\t).then(\n\t\t\t(outcome) => this.#finish(abort, timeout, channel, settled, limit, outcome),\n\t\t\t(error: unknown) => this.#fail(abort, timeout, channel, settled, error),\n\t\t)\n\t\t// An abandoned handle (neither `events` drained nor `result` awaited) must not surface an\n\t\t// unhandledRejection on a genuine error — guard the public result, where the rejection lives\n\t\t// (`#fail` rejects `settled` without re-throwing, so the drive's settle itself resolves).\n\t\t// A caller who awaits `result` still gets the rejection: `.catch` returns a derived promise, it\n\t\t// does not consume the original's rejection.\n\t\tsettled.promise.catch(() => {})\n\t\treturn {\n\t\t\tevents: this.#drainEvents(channel, abort),\n\t\t\tresult: settled.promise,\n\t\t\t// Fire this run's own handle (the closed-over `abort`), never a shared field a\n\t\t\t// later `stream()` could have replaced — so a handle's `abort()` always cancels\n\t\t\t// the run it belongs to, even when runs overlap.\n\t\t\tabort: abort.abort.bind(abort),\n\t\t}\n\t}\n\n\tabort(reason?: unknown): void {\n\t\t// Cancel every in-flight run — iterate a snapshot so a settle-driven `#runs.delete`\n\t\t// (a cancelled run unwinding) can't disturb the walk. Aborting is idempotent, so an\n\t\t// already-finished or already-cancelled handle is a harmless no-op.\n\t\tfor (const abort of [...this.#runs.keys()]) abort.abort(reason)\n\t}\n\n\t// Settles a finished or cancelled run: the deadline clears, the run leaves `#runs` (releasing its\n\t// budget instances), the channel closes, and the assembled result resolves before it is observed.\n\t// A cancel still resolves a partial, so a cancelled run emits `abort` (the cancel reason) then\n\t// `finish`; limit exhaustion is not a cancel and emits `exhaust` (the turn count) instead. Every\n\t// emit follows the resolve, so an isolated listener throw can't reorder the settle.\n\t#finish(\n\t\tabort: AbortInterface,\n\t\ttimeout: TimeoutInterface | undefined,\n\t\tchannel: Channel<AgentChunk>,\n\t\tsettled: PromiseWithResolvers<AgentResult>,\n\t\tlimit: number,\n\t\toutcome: AgentRunResult,\n\t): void {\n\t\ttimeout?.clear()\n\t\tthis.#runs.delete(abort)\n\t\tthis.#settled = 'done'\n\t\tchannel.close()\n\t\tconst result = assembleResult(outcome)\n\t\tsettled.resolve(result)\n\t\tif (outcome.exhausted) this.#emitter.emit('exhaust', limit)\n\t\telse if (outcome.partial) this.#emitter.emit('abort', abort.signal.reason)\n\t\tthis.#emitter.emit('finish', result)\n\t}\n\n\t// Settles a run a genuine (non-cancel) provider / tool error stopped: the channel fails, the\n\t// result rejects, and the failure is observed after the reject.\n\t#fail(\n\t\tabort: AbortInterface,\n\t\ttimeout: TimeoutInterface | undefined,\n\t\tchannel: Channel<AgentChunk>,\n\t\tsettled: PromiseWithResolvers<AgentResult>,\n\t\terror: unknown,\n\t): void {\n\t\ttimeout?.clear()\n\t\tthis.#runs.delete(abort)\n\t\tthis.#settled = 'error'\n\t\tchannel.fail(error)\n\t\tsettled.reject(error)\n\t\tthis.#emitter.emit('error', error)\n\t}\n\n\t// Drives `#execute` by hand, pushing each chunk into the channel, and returns the generator's\n\t// return value; a genuine provider / tool error rejects. `for await` would discard the return.\n\tasync #drive(\n\t\tabort: AbortInterface,\n\t\tconversation: ConversationInterface,\n\t\tchannel: Channel<AgentChunk>,\n\t\tthink: boolean | undefined,\n\t\tschema: Readonly<Record<string, unknown>> | undefined,\n\t\tlimit: number,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t): Promise<AgentRunResult> {\n\t\tconst run = this.#execute(abort, conversation, think, schema, limit, budget, allowance)\n\t\tfor (;;) {\n\t\t\tconst next = await run.next()\n\t\t\tif (next.done === true) return next.value\n\t\t\tchannel.push(next.value)\n\t\t}\n\t}\n\n\t// The live event stream: drain the channel the drive writes into, yielding each `AgentChunk` as it\n\t// is pushed (and throwing if the drive called `fail` on the channel). Its `return()` — fired when\n\t// a consumer exits with `break` early — fires the turn abort, so the run stops promptly: the drive\n\t// then completes the loop with `partial: true`, calls `clear` on the deadline, and settles\n\t// `result` to a non-misleading `{ partial: true }`, leaving `status` no longer `running`.\n\tasync *#drainEvents(\n\t\tchannel: Channel<AgentChunk>,\n\t\tabort: AbortInterface,\n\t): AsyncGenerator<AgentChunk, void> {\n\t\ttry {\n\t\t\tyield* channel.drain()\n\t\t} finally {\n\t\t\t// Early break (consumer stopped pulling): cancel the run so the drive unwinds and\n\t\t\t// settles partial. A natural end reaches here too with the signal already done, so\n\t\t\t// this abort is a harmless no-op then.\n\t\t\tabort.abort()\n\t\t}\n\t}\n\n\t// The core loop, shared by generate + stream. Builds the provider input once, then runs turns up\n\t// to `limit`, appending each turn's readonly record, and continues only while the latest turn\n\t// requested tools. A cancel (the bound abort) ends a turn with a `cancel` record — it never\n\t// throws; only a genuine provider / tool error propagates. Every record is local to one call, so\n\t// concurrent runs share nothing, and the settled `AgentRunResult` derives from the records.\n\t// Each turn runs in this generator, which delegates only the provider stream to `#provide` and the\n\t// ask pass to `#approve`: a delta crosses these two layers before `#drive` pushes it, and no\n\t// per-turn generator settles between a turn's chunks. Every further layer would let a consumer's\n\t// early `return()` cancel the provider a step sooner.\n\tasync *#execute(\n\t\tabort: AbortInterface,\n\t\tconversation: ConversationInterface,\n\t\tthink: boolean | undefined,\n\t\tschema: Readonly<Record<string, unknown>> | undefined,\n\t\tlimit: number,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t): AsyncGenerator<AgentChunk, AgentRunResult> {\n\t\t// Compaction can fold the request into a section, so every select site receives the object\n\t\t// captured at run entry.\n\t\tconst last = conversation.view().at(-1)\n\t\tconst request = last?.role === 'user' ? last : undefined\n\t\t// `#select` returns `undefined` when no handler is set, so the default path awaits nothing\n\t\t// before the first provider request.\n\t\tconst selection = this.#select(conversation, request, abort, budget)\n\t\tconst entry =\n\t\t\tselection === undefined\n\t\t\t\t? { messages: this.#build(conversation), usage: undefined }\n\t\t\t\t: await selection\n\t\tconst tools = this.#context.tools\n\t\t// The per-call options are fixed for the run, so every request carries this one snapshot;\n\t\t// absent when neither preference is set.\n\t\tconst options: ProviderRequest['options'] =\n\t\t\tthink === undefined && schema === undefined\n\t\t\t\t? undefined\n\t\t\t\t: {\n\t\t\t\t\t\t...(think === undefined ? {} : { think }),\n\t\t\t\t\t\t...(schema === undefined ? {} : { schema }),\n\t\t\t\t\t}\n\t\t// Reset `#window` at run entry so no stale `consumed` carries across runs / a conversation\n\t\t// switch.\n\t\tthis.#window?.clear()\n\t\t// One readonly record per turn; the turn index is the list length.\n\t\tconst turns: AgentTurn[] = []\n\t\t// One turn per step: observe it, pace the boundary, admit the request, stream the provider,\n\t\t// reconcile the usage, then dispatch the requested tools or record the final answer. A cancel\n\t\t// appends a `cancel` record, which ends the loop; a genuine error throws. The section before\n\t\t// the provider call adds no `await` on the default path, so the provider's `stream()` is\n\t\t// entered in `agent.stream()`'s own call.\n\t\twhile (turns.length < limit && (turns.at(-1)?.outcome ?? 'tools') === 'tools') {\n\t\t\tconst index = turns.length\n\t\t\tconst base = turns.at(-1)?.messages ?? entry.messages\n\t\t\tconst futile = turns.at(-1)?.futile ?? false\n\t\t\t// The record of a turn a cancel stopped before its provider call.\n\t\t\tconst stopped: AgentTurn = {\n\t\t\t\tcontent: '',\n\t\t\t\tthinking: undefined,\n\t\t\t\tusage: undefined,\n\t\t\t\tspent: undefined,\n\t\t\t\tmessages: base,\n\t\t\t\toutcome: 'cancel',\n\t\t\t\tpartial: true,\n\t\t\t\tfutile,\n\t\t\t\testimate: undefined,\n\t\t\t\tinterrupted: false,\n\t\t\t\ttruncated: false,\n\t\t\t}\n\t\t\t// Observe each turn begin (the turn index). The emitter isolates a listener throw, so it\n\t\t\t// can't perturb the turn that immediately follows.\n\t\t\tthis.#emitter.emit('turn', index)\n\t\t\t// Pace between expensive turns — never before the first. The yield is raced against the run\n\t\t\t// signal, so a scheduler that ignores it still ends on a cancel; a cancel landing at the turn\n\t\t\t// boundary surfaces here as a throw and stops the turn like any cancel, while a genuine\n\t\t\t// scheduler fault propagates.\n\t\t\tconst scheduler = this.#scheduler\n\t\t\tif (index > 0 && scheduler !== undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tawait raceSignal(scheduler.yield({ signal: abort.signal }), abort.signal)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (!abort.signal.aborted) throw error\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (abort.signal.aborted) {\n\t\t\t\tturns.push(stopped)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// Snapshot the allow-list before listeners can change the active scope this turn.\n\t\t\tconst names = this.#context.scope?.tools?.slice()\n\t\t\tconst advertised = filterAllowList(\n\t\t\t\tnames,\n\t\t\t\ttools.definitions(),\n\t\t\t\t(definition) => definition.name,\n\t\t\t)\n\t\t\t// The instance each advertised name holds at this point: every decision of this turn applies to it.\n\t\t\tconst held = new Map(\n\t\t\t\tadvertised.map((definition) => [definition.name, tools.tool(definition.name)]),\n\t\t\t)\n\t\t\tconst definitions = advertised.length > 0 ? advertised : undefined\n\t\t\t// Admit the request: compose it once from the replay-filtered messages and this turn's tool\n\t\t\t// and option snapshot, measure it against `#window`, and dispatch exactly the request the\n\t\t\t// window last measured (the rebuilt one after a fold). With no `window` it is composed and\n\t\t\t// dispatched with no extra `await`.\n\t\t\tconst composed = this.#compose(base, definitions, options)\n\t\t\tconst admitted =\n\t\t\t\tthis.#window === undefined\n\t\t\t\t\t? { dispatch: composed, messages: base, spent: undefined, futile }\n\t\t\t\t\t: await this.#admit(\n\t\t\t\t\t\t\tthis.#window,\n\t\t\t\t\t\t\tconversation,\n\t\t\t\t\t\t\tcomposed,\n\t\t\t\t\t\t\tbase,\n\t\t\t\t\t\t\trequest,\n\t\t\t\t\t\t\tindex > 0,\n\t\t\t\t\t\t\tfutile,\n\t\t\t\t\t\t\tabort,\n\t\t\t\t\t\t\tbudget,\n\t\t\t\t\t\t)\n\t\t\tif (admitted.dispatch === undefined) {\n\t\t\t\tturns.push({\n\t\t\t\t\t...stopped,\n\t\t\t\t\tmessages: admitted.messages,\n\t\t\t\t\tspent: admitted.spent,\n\t\t\t\t\tfutile: admitted.futile,\n\t\t\t\t})\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// The turn's streamed content, one entry per content delta; the meter charges only when an\n\t\t\t// allowance exists.\n\t\t\tconst streamed: string[] = []\n\t\t\tconst meter =\n\t\t\t\tallowance === undefined\n\t\t\t\t\t? undefined\n\t\t\t\t\t: createTokenMeter({ budget: allowance, estimator: estimateTokens })\n\t\t\tconst provided = yield* this.#provide(admitted.dispatch, abort.signal, streamed, meter)\n\t\t\tconst settled = this.#reconcile(provided, abort, streamed, admitted, meter, budget, allowance)\n\t\t\tif (!provided.success) {\n\t\t\t\tturns.push({ ...settled, outcome: 'cancel', partial: true })\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst result = provided.value\n\t\t\tconst recorded = settled.thinking === undefined ? {} : { thinking: settled.thinking }\n\t\t\tif (settled.usage !== undefined) {\n\t\t\t\t// Observe this turn's usage — the result already exists; emit beside the yield.\n\t\t\t\tthis.#emitter.emit('usage', settled.usage, settled.estimate)\n\t\t\t\tyield { category: 'usage', usage: settled.usage, estimate: settled.estimate }\n\t\t\t}\n\t\t\tif (result.tools !== undefined && result.tools.length > 0) {\n\t\t\t\tconst admittedCalls = filterAllowList(names, result.tools, (call) => call.name)\n\t\t\t\tconst authorization = this.#authorize(result.tools, admittedCalls, advertised, held)\n\t\t\t\t// The ask pass runs only for an asked call, so a turn with no `ask` verdict adds no `await`.\n\t\t\t\tif (authorization.asked.length > 0 && !abort.signal.aborted) {\n\t\t\t\t\tyield* this.#approve(tools, authorization, abort.signal)\n\t\t\t\t}\n\t\t\t\tif (!abort.signal.aborted) this.#confirm(tools, authorization, advertised)\n\t\t\t\tif (abort.signal.aborted) {\n\t\t\t\t\tturns.push({ ...settled, outcome: 'cancel', partial: true })\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst assistant = conversation.add({\n\t\t\t\t\trole: 'assistant',\n\t\t\t\t\tcontent: result.content,\n\t\t\t\t\tcalls: result.tools,\n\t\t\t\t\t...recorded,\n\t\t\t\t})\n\t\t\t\tconst results = await this.#dispatch(\n\t\t\t\t\ttools,\n\t\t\t\t\tresult.tools,\n\t\t\t\t\tabort.signal,\n\t\t\t\t\tauthorization,\n\t\t\t\t\tadvertised,\n\t\t\t\t)\n\t\t\t\t// Each dispatched call's tool message, appended per step of a loop that yields.\n\t\t\t\tconst replies: Message[] = []\n\t\t\t\tfor (const [position, call] of result.tools.entries()) {\n\t\t\t\t\tconst dispatched = results[position]\n\t\t\t\t\tif (dispatched === undefined) continue\n\t\t\t\t\tconst { outcome, content } = settleToolResult(dispatched)\n\t\t\t\t\t// Observe the dispatched tool + its result — beside the `tool` yield (the result\n\t\t\t\t\t// already exists). Carries the same pair the chunk carries.\n\t\t\t\t\tthis.#emitter.emit('tool', call, outcome)\n\t\t\t\t\tyield { category: 'tool', call, result: outcome }\n\t\t\t\t\t// `call` comes from the paired call rather than the result's `id`, so tool message\n\t\t\t\t\t// `position` names `calls[position]` even when a supplied tool manager returns another id.\n\t\t\t\t\treplies.push(conversation.add({ role: 'tool', content, call: call.id }))\n\t\t\t\t}\n\t\t\t\t// The next turn's admission measures and folds the grown prompt, so a final answer never\n\t\t\t\t// pays for a fold. A tools turn's truncation does not end the run, so it is not recorded.\n\t\t\t\tturns.push({\n\t\t\t\t\t...settled,\n\t\t\t\t\tmessages: [...admitted.messages, assistant, ...replies],\n\t\t\t\t\toutcome: 'tools',\n\t\t\t\t\tpartial: true,\n\t\t\t\t})\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// No tools: this turn's content is the final answer. A truncated answer is the model's stop,\n\t\t\t// and a cancel already settles the run, so neither meets the schema.\n\t\t\tif (schema !== undefined && result.truncated !== true && !abort.signal.aborted) {\n\t\t\t\tconst answered = attempt(() => requireAnswer(result.content, schema))\n\t\t\t\tif (!answered.success) {\n\t\t\t\t\tif (this.#strict) throw answered.error\n\t\t\t\t\tthis.#emitter.emit('fault', answered.error)\n\t\t\t\t}\n\t\t\t}\n\t\t\tconversation.add({ role: 'assistant', content: result.content, ...recorded })\n\t\t\tturns.push({\n\t\t\t\t...settled,\n\t\t\t\tcontent: result.content,\n\t\t\t\toutcome: 'answer',\n\t\t\t\tpartial: abort.signal.aborted,\n\t\t\t\ttruncated: result.truncated === true,\n\t\t\t})\n\t\t}\n\t\tconst final = turns.at(-1)\n\t\tconst thinkings = turns.flatMap((turn) => (turn.thinking === undefined ? [] : [turn.thinking]))\n\t\treturn {\n\t\t\tcontent:\n\t\t\t\tfinal?.outcome === 'answer' ? final.content : turns.map((turn) => turn.content).join(''),\n\t\t\tthinking: thinkings.length === 0 ? undefined : thinkings.join('\\n\\n'),\n\t\t\t// Every turn's usage, then the entry selection's, then each rebuild's, in turn order.\n\t\t\tusage: [\n\t\t\t\t...turns.map((turn) => turn.usage),\n\t\t\t\tentry.usage,\n\t\t\t\t...turns.map((turn) => turn.spent),\n\t\t\t].reduce<TokenUsage | undefined>(\n\t\t\t\t(total, one) => (one === undefined ? total : sumUsage(total, one)),\n\t\t\t\tundefined,\n\t\t\t),\n\t\t\tpartial: final?.partial ?? abort.signal.aborted,\n\t\t\t// Every allowed turn requested tools; a cancel during the last one is an `abort`, never an\n\t\t\t// `exhaust`, so the signal is read here.\n\t\t\texhausted: final?.outcome === 'tools' && !abort.signal.aborted,\n\t\t\tstop: inferStop(abort.signal.reason, allowance, budget, this.#provider.id, final),\n\t\t}\n\t}\n\n\t// Settles one provider call into its turn's record fields, charges the cost budget at the one\n\t// charge site, then rethrows a genuine failure. A cancelled call's fields still describe what it\n\t// streamed; the caller adds the ending.\n\t#reconcile(\n\t\tprovided: Result<ProviderResult>,\n\t\tabort: AbortInterface,\n\t\tstreamed: readonly string[],\n\t\tadmitted: Pick<AgentTurn, 'messages' | 'spent' | 'futile'>,\n\t\tmeter: MeterInterface | undefined,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\t// Read before any reconcile: an abort seen here landed while the call ran, and a charge that\n\t\t// trips a bound later in this method cannot turn a genuine failure into a partial.\n\t\tconst interrupted = abort.signal.aborted\n\t\t// The abort partial of an interrupted call is the only carrier of its reasoning and usage; the\n\t\t// streamed deltas already hold its content. A genuine failure contributes no usage.\n\t\tconst partial =\n\t\t\t!provided.success && interrupted && isProviderAbortError(provided.error)\n\t\t\t\t? provided.error.partial\n\t\t\t\t: undefined\n\t\tconst surfaced = provided.success ? provided.value.thinking : partial?.thinking\n\t\tconst thinking = surfaced !== undefined && surfaced.length > 0 ? surfaced : undefined\n\t\tconst content = streamed.join('')\n\t\tconst estimate = estimateTokens(content)\n\t\t// A settled result's usage crossed the provider-result door; only an abort partial is sanitized.\n\t\tconst usage = provided.success\n\t\t\t? provided.value.usage\n\t\t\t: partial?.usage === undefined\n\t\t\t\t? undefined\n\t\t\t\t: sanitizeUsage(partial.usage)\n\t\t// The one charge site for every path, a genuine failure included. The estimate is already on\n\t\t// the cost budget only when the meter charged that same instance; a call that reported no usage\n\t\t// keeps its estimate on the cost budget, unless the meter put it there.\n\t\tconst charged = allowance === budget ? (meter?.charged ?? 0) : 0\n\t\tconst billed =\n\t\t\tusage ??\n\t\t\t(allowance === budget ? undefined : { prompt: 0, completion: estimate, total: estimate })\n\t\tif (billed !== undefined) chargeUsage(budget, billed, charged)\n\t\tif (!provided.success && !interrupted) throw provided.error\n\t\treturn {\n\t\t\tcontent,\n\t\t\tthinking,\n\t\t\tusage,\n\t\t\tspent: admitted.spent,\n\t\t\tmessages: admitted.messages,\n\t\t\tfutile: admitted.futile,\n\t\t\testimate,\n\t\t\tinterrupted,\n\t\t\ttruncated: false,\n\t\t}\n\t}\n\n\t// Composes the exact request one provider dispatch sends: the working messages after the\n\t// provider's replay policy, the advertised tool definitions, and the per-call `think` / `schema`\n\t// options. Keys are omitted when undefined, so the provider receives no tools and no options\n\t// object when there are none. The request owns its messages array, a shallow copy after the\n\t// replay filter, because that filter returns the working array itself for a policy that keeps\n\t// every message. The admission step measures this object and `#provide` sends it, so the\n\t// measured and the dispatched request can never diverge.\n\t#compose(\n\t\tmessages: readonly Message[],\n\t\ttools: ProviderRequest['tools'],\n\t\toptions: ProviderRequest['options'],\n\t): ProviderRequest {\n\t\treturn {\n\t\t\tmessages: [...stripThinking(messages, this.#replay)],\n\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t...(options === undefined ? {} : { options }),\n\t\t}\n\t}\n\n\t// Admits one request against the context `window` budget immediately before its provider\n\t// dispatch, so only called when a window is set. `clear()` then consuming the request makes\n\t// `window.consumed` its estimated footprint, and `exhausted` means it has reached `max`. A\n\t// request over the window folds the run's conversation once when it is summarizable and not\n\t// latched futile, then rebuilds through selection, recomposes with the same tool and option\n\t// snapshot, and measures again; the request that measures last is the one dispatched. A\n\t// non-summarizable conversation is measured but never folded, so the auto path never throws the\n\t// `compact()` SUMMARIZER error. A cancel anywhere returns no dispatch so the run settles partial,\n\t// and wins over `strict`; otherwise `strict` refuses a request still over the window with a\n\t// WINDOW error before any dispatch, while `strict: false` sends it.\n\tasync #admit(\n\t\twindow: BudgetInterface<ProviderRequest>,\n\t\tconversation: ConversationInterface,\n\t\tcomposed: ProviderRequest,\n\t\tmessages: readonly Message[],\n\t\trequest: Message | undefined,\n\t\tlatch: boolean,\n\t\tfutile: boolean,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\twindow.clear()\n\t\twindow.consume(composed)\n\t\tif (!window.exhausted) return { dispatch: composed, messages, spent: undefined, futile }\n\t\t// A cancelled run is about to settle, so a fold would spend summarizer time on a request no\n\t\t// provider reads; the next run's first admission folds instead.\n\t\tif (abort.signal.aborted) return { dispatch: undefined, messages, spent: undefined, futile }\n\t\tif (!conversation.summarizable || futile) {\n\t\t\treturn { dispatch: this.#enforce(window, composed), messages, spent: undefined, futile }\n\t\t}\n\t\t// The run signal bounds the fold: `compact()` rejects with its reason on a cancel and\n\t\t// commits nothing, even when the summarizer ignores the signal.\n\t\treturn conversation.compact({ signal: abort.signal }).then(\n\t\t\t(section) =>\n\t\t\t\tthis.#refold(\n\t\t\t\t\tsection,\n\t\t\t\t\twindow,\n\t\t\t\t\tconversation,\n\t\t\t\t\tcomposed,\n\t\t\t\t\tmessages,\n\t\t\t\t\trequest,\n\t\t\t\t\tlatch,\n\t\t\t\t\tabort,\n\t\t\t\t\tbudget,\n\t\t\t\t),\n\t\t\t(error: unknown) => this.#unfold(error, composed, messages, abort),\n\t\t)\n\t}\n\n\t// A fold that settled: a cancel wins; nothing folded latches on a later turn, where the tail had\n\t// its chance to grow, but not on the first; otherwise rebuild through selection (compaction\n\t// changed the view the handler evaluates) and measure the rebuilt request.\n\tasync #refold(\n\t\tsection: Section | undefined,\n\t\twindow: BudgetInterface<ProviderRequest>,\n\t\tconversation: ConversationInterface,\n\t\tcomposed: ProviderRequest,\n\t\tmessages: readonly Message[],\n\t\trequest: Message | undefined,\n\t\tlatch: boolean,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\tif (abort.signal.aborted)\n\t\t\treturn { dispatch: undefined, messages, spent: undefined, futile: false }\n\t\tif (section === undefined) {\n\t\t\treturn {\n\t\t\t\tdispatch: this.#enforce(window, composed),\n\t\t\t\tmessages,\n\t\t\t\tspent: undefined,\n\t\t\t\tfutile: latch,\n\t\t\t}\n\t\t}\n\t\tconst selection = this.#select(conversation, request, abort, budget)\n\t\tconst rebuilt =\n\t\t\tselection === undefined\n\t\t\t\t? { messages: this.#build(conversation), usage: undefined }\n\t\t\t\t: await selection\n\t\tif (abort.signal.aborted) {\n\t\t\treturn {\n\t\t\t\tdispatch: undefined,\n\t\t\t\tmessages: rebuilt.messages,\n\t\t\t\tspent: rebuilt.usage,\n\t\t\t\tfutile: false,\n\t\t\t}\n\t\t}\n\t\tconst dispatch = this.#compose(rebuilt.messages, composed.tools, composed.options)\n\t\twindow.clear()\n\t\twindow.consume(dispatch)\n\t\treturn {\n\t\t\tdispatch: this.#enforce(window, dispatch),\n\t\t\tmessages: rebuilt.messages,\n\t\t\tspent: rebuilt.usage,\n\t\t\tfutile: false,\n\t\t}\n\t}\n\n\t// A fold that rejected: a cancel wins over the failure it caused, as on the select path;\n\t// otherwise surface the summarizer failure observably, rethrow under `strict`, and send the\n\t// over-window request leniently.\n\t#unfold(\n\t\terror: unknown,\n\t\tcomposed: ProviderRequest,\n\t\tmessages: readonly Message[],\n\t\tabort: AbortInterface,\n\t) {\n\t\tif (abort.signal.aborted)\n\t\t\treturn { dispatch: undefined, messages, spent: undefined, futile: false }\n\t\tthis.#emitter.emit('fault', error)\n\t\t// A fault listener can cancel the run synchronously; the cancel then wins over strict, as in #reject.\n\t\tif (abort.signal.aborted)\n\t\t\treturn { dispatch: undefined, messages, spent: undefined, futile: false }\n\t\tif (this.#strict) throw error\n\t\treturn { dispatch: composed, messages, spent: undefined, futile: false }\n\t}\n\n\t// Refuses, under `strict`, a request the window still measures over its max; returns it otherwise.\n\t#enforce(window: BudgetInterface<ProviderRequest>, request: ProviderRequest): ProviderRequest {\n\t\tif (this.#strict && window.exhausted) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'WINDOW',\n\t\t\t\t`the request measures ${window.consumed} against a window of ${window.max} and this run folds no further`,\n\t\t\t\t{ context: { agent: this.#id, consumed: window.consumed, max: window.max } },\n\t\t\t)\n\t\t}\n\t\treturn request\n\t}\n\n\t// One select site: the handler's pending selection settled into the working messages and the\n\t// usage it spent, or `undefined` when there is no request or no handler, so the caller builds from\n\t// `view()` without an await.\n\t#select(\n\t\tconversation: ConversationInterface,\n\t\trequest: Message | undefined,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\tconst pending =\n\t\t\trequest === undefined ? undefined : this.#context.select(conversation, request, abort.signal)\n\t\treturn pending === undefined\n\t\t\t? undefined\n\t\t\t: pending.then(\n\t\t\t\t\t(selection) => this.#adopt(conversation, selection, abort, budget),\n\t\t\t\t\t(error: unknown) => this.#reject(conversation, error, abort),\n\t\t\t\t)\n\t}\n\n\t// A resolved selection: charge its usage before the abort check, because judge calls were spent\n\t// either way and the charge can itself trip the run's bound; then build from it unless a cancel or\n\t// a strict fault intervenes. A cancelled site builds from `view()` and emits no `select`.\n\t#adopt(\n\t\tconversation: ConversationInterface,\n\t\tselection: Selection,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\tconst usage = selection.usage === undefined ? undefined : sanitizeUsage(selection.usage)\n\t\tif (usage !== undefined) chargeUsage(budget, usage, 0)\n\t\tif (abort.signal.aborted) return { messages: this.#build(conversation), usage }\n\t\tif (selection.fault !== undefined) {\n\t\t\tthis.#emitter.emit('fault', selection.fault)\n\t\t\tif (abort.signal.aborted) return { messages: this.#build(conversation), usage }\n\t\t\tif (this.#strict) throw selection.fault\n\t\t}\n\t\tconst messages = this.#build(conversation, selection)\n\t\tthis.#emitter.emit('select', selection)\n\t\treturn { messages, usage }\n\t}\n\n\t// A rejected selection: a fault unless cancelled, rethrown under `strict`, then the default build.\n\t#reject(conversation: ConversationInterface, error: unknown, abort: AbortInterface) {\n\t\tif (!abort.signal.aborted) {\n\t\t\tthis.#emitter.emit('fault', error)\n\t\t\t// A `fault` listener can cancel the run synchronously; the cancel then wins over `strict`.\n\t\t\tif (this.#strict && !abort.signal.aborted) throw error\n\t\t}\n\t\treturn { messages: this.#build(conversation), usage: undefined }\n\t}\n\n\t// Every build of a run renders its bound conversation: a selection's messages when one is given,\n\t// else that conversation's `view()`, never the pointer a later `switch` may have moved.\n\t#build(conversation: ConversationInterface, selection?: Selection): readonly Message[] {\n\t\treturn this.#context.build(selection ?? { messages: conversation.view(), judgments: [] })\n\t}\n\n\t// Evaluate policy and emit each verdict before the run's final pre-dispatch abort guard, in call\n\t// order with no `await`: policy callbacks and decision listeners can cancel the run synchronously.\n\t// Only an `allow` verdict enters the allowed map, seated with the instance the turn held when it\n\t// advertised the call; an asked call is queued with that same instance, so a decision applies to\n\t// that instance alone and a tool replaced later is refused.\n\t#authorize(\n\t\tcalls: readonly ToolCall[],\n\t\tadmitted: readonly ToolCall[],\n\t\tadvertised: readonly ToolDefinition[],\n\t\theld: ReadonlyMap<string, ToolInterface | undefined>,\n\t) {\n\t\tconst allowed = new Map<\n\t\t\tnumber,\n\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t>()\n\t\tconst denials = new Map<number, ToolResult>()\n\t\tconst asked: Array<{\n\t\t\treadonly index: number\n\t\t\treadonly call: ToolCall\n\t\t\treadonly decision: AuthorityDecision\n\t\t\treadonly tool: ToolInterface | undefined\n\t\t}> = []\n\t\t// `entries()` visits a hole as `undefined`, as a `for…of` over the calls does.\n\t\tfor (const [index, call] of calls.entries()) {\n\t\t\tconst decided = this.#decide(call, admitted, advertised)\n\t\t\tif (!decided.success) {\n\t\t\t\tdenials.set(index, decided.error)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst decision = decided.value\n\t\t\tif (decision.verdict === 'allow') {\n\t\t\t\tallowed.set(index, { call, tool: held.get(call.name) })\n\t\t\t\tthis.#emitter.emit('allow', call, decision)\n\t\t\t} else if (decision.verdict === 'ask') {\n\t\t\t\tasked.push({ index, call, decision, tool: held.get(call.name) })\n\t\t\t} else {\n\t\t\t\tdenials.set(index, denyCall(call, decision.reason))\n\t\t\t\tthis.#emitter.emit('deny', call, decision.reason, decision)\n\t\t\t}\n\t\t}\n\t\treturn { allowed, denials, asked }\n\t}\n\n\t// Decides one call — scope, then authority, then the `consequential` floor. A scope miss (a call\n\t// the allow-list excludes, or whose name no definition advertises this turn), an evaluate throw,\n\t// and an invalid decision emit `deny` with no decision and fail with the denial; otherwise the\n\t// owned copy of the decision, raised by the advertised definition, succeeds. The scope miss is\n\t// decided before the authority is read, so it never sees a name no definition advertises.\n\t#decide(\n\t\tcall: ToolCall,\n\t\tadmitted: readonly ToolCall[],\n\t\tadvertised: readonly ToolDefinition[],\n\t): Result<AuthorityDecision, ToolResult> {\n\t\tconst definition = admitted.includes(call)\n\t\t\t? advertised.find((advertisement) => advertisement.name === call.name)\n\t\t\t: undefined\n\t\tif (definition === undefined) {\n\t\t\tconst denial = denyCall(call, SCOPE_NOTE)\n\t\t\tthis.#emitter.emit('deny', call, `${call.name} is not in the active scope`, undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\tconst authority = this.#authority\n\t\tif (authority === undefined) {\n\t\t\tconst denial = denyCall(call, undefined)\n\t\t\tthis.#emitter.emit('deny', call, 'no authority is set', undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\t// A security gate must fail closed: if a policy `evaluate` throws, or a member read of its\n\t\t// decision throws, the call is not cleared, so it must not run. Synthesize a denial instead of\n\t\t// letting the throw reject the whole run — the tool stays unexecuted and the model still\n\t\t// receives a denial it can react to, exactly like an explicit `deny`.\n\t\tconst evaluated = attempt(() => {\n\t\t\tconst decision = authority.evaluate({ call })\n\t\t\tif (!isObject(decision)) return undefined\n\t\t\t// The decision is foreign: read each member once into an owned copy, then validate the copy.\n\t\t\tconst { zone, verdict, reason } = decision\n\t\t\tconst copy = { zone, verdict, ...(reason === undefined ? {} : { reason }) }\n\t\t\tif (!isAuthorityDecision(copy)) return undefined\n\t\t\t// The floor reads this turn's advertised definition, so a structural authority cannot skip it.\n\t\t\treturn resolveDecision(copy, definition)\n\t\t})\n\t\tif (!evaluated.success) {\n\t\t\t// The declared `@orkestrel/contract` normalizer, so the `deny` event carries a non-empty\n\t\t\t// explanation: an `Error` with an empty `message` normalizes to real text, and a throw whose\n\t\t\t// stringification itself fails is caught rather than escaping the gate and rejecting the\n\t\t\t// run. The model reads the `denied` note, never the error text.\n\t\t\tconst denial = denyCall(call, undefined)\n\t\t\tthis.#emitter.emit('deny', call, errorToMessage(evaluated.error), undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\tif (evaluated.value === undefined) {\n\t\t\tconst denial = denyCall(call, undefined)\n\t\t\tthis.#emitter.emit('deny', call, 'invalid decision', undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\treturn success(evaluated.value)\n\t}\n\n\t// Hands each asked call to the approver in call order, entering handler k+1 only after handler k\n\t// settles, and races each answer against the run signal. Each call's `ask` chunk is yielded after\n\t// its handler is entered and before its answer is awaited, so a reader that sees the chunk finds\n\t// the ask registered. An abort returns at once, so the pending call emits nothing further and no\n\t// later handler is entered; `raceSignal` absorbs a late settle. An approval of a tool that is no\n\t// longer the turn's held instance is denied as unanswered; any other approval seats that instance.\n\tasync *#approve(\n\t\ttools: ToolManagerInterface,\n\t\tauthorization: {\n\t\t\treadonly allowed: Map<\n\t\t\t\tnumber,\n\t\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t\t>\n\t\t\treadonly denials: Map<number, ToolResult>\n\t\t\treadonly asked: ReadonlyArray<{\n\t\t\t\treadonly index: number\n\t\t\t\treadonly call: ToolCall\n\t\t\t\treadonly decision: AuthorityDecision\n\t\t\t\treadonly tool: ToolInterface | undefined\n\t\t\t}>\n\t\t},\n\t\tsignal: AbortSignal,\n\t): AsyncGenerator<AgentChunk, void> {\n\t\tconst authority = this.#authority\n\t\tfor (const { index, call, decision, tool } of authorization.asked) {\n\t\t\tif (signal.aborted) return\n\t\t\tthis.#emitter.emit('ask', call, decision)\n\t\t\t// An `ask` listener can cancel the run before the handler is entered.\n\t\t\tif (signal.aborted) return\n\t\t\t// The approver is foreign: the executor reads it and enters it, so a throwing `ask` getter\n\t\t\t// and a synchronous handler throw both become a rejection, which the failure arm reads as\n\t\t\t// unanswered. An absent approver leaves the ask unanswered with no reason.\n\t\t\tconst answer = new Promise<{\n\t\t\t\treadonly approved: boolean\n\t\t\t\treadonly reason: string | undefined\n\t\t\t\treadonly note: string\n\t\t\t}>((resolve) => {\n\t\t\t\tconst ask = authority?.ask\n\t\t\t\tresolve(\n\t\t\t\t\task === undefined\n\t\t\t\t\t\t? { approved: false, reason: undefined, note: AUTHORITY_NOTES.unanswered }\n\t\t\t\t\t\t: Promise.resolve(ask.call(authority, { call }, decision, signal)).then((value) => ({\n\t\t\t\t\t\t\t\tapproved: value === true,\n\t\t\t\t\t\t\t\treason: undefined,\n\t\t\t\t\t\t\t\tnote: value === true ? AUTHORITY_NOTES.unanswered : AUTHORITY_NOTES.refused,\n\t\t\t\t\t\t\t})),\n\t\t\t\t)\n\t\t\t}).catch((error: unknown) => ({\n\t\t\t\tapproved: false,\n\t\t\t\treason: errorToMessage(error),\n\t\t\t\tnote: AUTHORITY_NOTES.unanswered,\n\t\t\t}))\n\t\t\tyield { category: 'ask', call, decision }\n\t\t\tconst outcome = await raceSignal(answer, signal).catch(() => undefined)\n\t\t\tif (outcome === undefined || signal.aborted) return\n\t\t\tif (outcome.approved && tools.tool(call.name) === tool) {\n\t\t\t\tauthorization.allowed.set(index, { call, tool })\n\t\t\t\tthis.#emitter.emit('allow', call, decision)\n\t\t\t} else {\n\t\t\t\tauthorization.denials.set(index, denyCall(call, outcome.note))\n\t\t\t\tthis.#emitter.emit('deny', call, outcome.reason, decision)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Whether a call's tool is no longer what its decision cleared: the instance the turn held, with the\n\t// `consequential` annotation the turn advertised, which is what the floor read. The compare is by\n\t// identity, so a host that re-adds the same instance still runs. The tool is foreign, so a read that\n\t// throws counts as a change.\n\t#changed(\n\t\ttools: ToolManagerInterface,\n\t\tcall: ToolCall,\n\t\ttool: ToolInterface | undefined,\n\t\tadvertised: readonly ToolDefinition[],\n\t): boolean {\n\t\tconst same = attempt(() => {\n\t\t\tconst current = tools.tool(call.name)\n\t\t\tconst definition = advertised.find((entry) => entry.name === call.name)\n\t\t\treturn (\n\t\t\t\tcurrent === tool &&\n\t\t\t\t(current?.annotations?.consequential === true) ===\n\t\t\t\t\t(definition?.annotations?.consequential === true)\n\t\t\t)\n\t\t})\n\t\treturn !same.success || !same.value\n\t}\n\n\t// Denies each allowed call whose tool changed since its decision, with no `await` and before the\n\t// pre-dispatch abort guard, so a `deny` listener that cancels here stores nothing.\n\t#confirm(\n\t\ttools: ToolManagerInterface,\n\t\tauthorization: {\n\t\t\treadonly allowed: Map<\n\t\t\t\tnumber,\n\t\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t\t>\n\t\t\treadonly denials: Map<number, ToolResult>\n\t\t},\n\t\tadvertised: readonly ToolDefinition[],\n\t): void {\n\t\tfor (const [index, { call, tool }] of authorization.allowed) {\n\t\t\tif (!this.#changed(tools, call, tool, advertised)) continue\n\t\t\tauthorization.allowed.delete(index)\n\t\t\tauthorization.denials.set(index, denyCall(call, AUTHORITY_NOTES.denied))\n\t\t\tthis.#emitter.emit('deny', call, `${call.name} changed after its decision`, undefined)\n\t\t\t// A deny listener can change a tool this walk already read, so walk them all again.\n\t\t\tthis.#confirm(tools, authorization, advertised)\n\t\t\treturn\n\t\t}\n\t}\n\n\t// Carry cancellation into entered handlers, then merge executed results and denials in call\n\t// order. An all-denied turn costs no tool run. The allowed entries are rebuilt in call order,\n\t// because approved calls join the map after the synchronous pass. The manager resolves a tool by\n\t// name when it starts a call, and a handler's synchronous prefix runs before the next start, so\n\t// each call is checked again right before it starts: the check and the manager's lookup read one\n\t// instance, and the manager keeps its own policy for the call.\n\tasync #dispatch(\n\t\ttools: ToolManagerInterface,\n\t\tcalls: readonly ToolCall[],\n\t\tsignal: AbortSignal,\n\t\tauthorization: {\n\t\t\treadonly allowed: ReadonlyMap<\n\t\t\t\tnumber,\n\t\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t\t>\n\t\t\treadonly denials: ReadonlyMap<number, ToolResult>\n\t\t},\n\t\tadvertised: readonly ToolDefinition[],\n\t): Promise<readonly ToolResult[]> {\n\t\tconst { allowed, denials } = authorization\n\t\tconst entries = [...allowed].sort(([left], [right]) => left - right)\n\t\tconst started = entries.map(([, { call, tool }]) => {\n\t\t\tif (!this.#changed(tools, call, tool, advertised)) return tools.execute(call, { signal })\n\t\t\tthis.#emitter.emit('deny', call, `${call.name} changed after its decision`, undefined)\n\t\t\treturn denyCall(call, AUTHORITY_NOTES.denied)\n\t\t})\n\t\tconst executed = started.length > 0 ? await Promise.all(started) : []\n\t\t// Denials and executed results never share an index.\n\t\tconst results = new Map<number, ToolResult>([\n\t\t\t...denials,\n\t\t\t...entries.flatMap(([index], offset) => {\n\t\t\t\tconst result = executed[offset]\n\t\t\t\treturn result === undefined ? [] : [[index, result] as const]\n\t\t\t}),\n\t\t])\n\t\treturn calls.map((call, index) => results.get(index) ?? denyCall(call, undefined))\n\t}\n\n\t// Drive one provider stream turn: read the `channel` of each {@link ProviderDelta} — a `'content'`\n\t// delta is the answer (appended to the turn's `streamed` log, charged through the meter, surfaced\n\t// as a `token` chunk, in that order); a `'thinking'` delta is live reasoning (surfaced as a `think`\n\t// chunk, never metered — reasoning is not answer content) — returning the provider's assembled\n\t// result or its failure as a Result built through the installed `attempt`, so the turn reads a\n\t// provider or meter failure from a value. It sends the composed {@link ProviderRequest}\n\t// unchanged — the messages already carry only the thinking the provider's replay policy permits\n\t// (default `'none'`), and the tools and options are the turn's snapshot, absent when there are\n\t// none. It pulls the provider itself, so no generator layer sits between a delta and the turn.\n\tasync *#provide(\n\t\trequest: ProviderRequest,\n\t\tsignal: AbortSignal,\n\t\tstreamed: string[],\n\t\tmeter: MeterInterface | undefined,\n\t): AsyncGenerator<AgentChunk, Result<ProviderResult>> {\n\t\ttry {\n\t\t\tconst generator = this.#provider.stream(\n\t\t\t\trequest.messages,\n\t\t\t\tsignal,\n\t\t\t\trequest.tools,\n\t\t\t\trequest.options,\n\t\t\t)\n\t\t\tfor (;;) {\n\t\t\t\tconst next = await generator.next()\n\t\t\t\tif (next.done === true) {\n\t\t\t\t\tconst result = next.value\n\t\t\t\t\treturn attempt(() => requireProviderResult(result))\n\t\t\t\t}\n\t\t\t\tconst delta = readProviderDelta(next.value)\n\t\t\t\tif (delta === undefined) {\n\t\t\t\t\t// The run ends here, so the foreign stream is released rather than held to its deadline.\n\t\t\t\t\tvoid generator.return({ content: '' }).catch(() => {})\n\t\t\t\t\tthrow new ProviderError(\n\t\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t\t`provider error: a delta must carry a channel of 'content' or 'thinking' and string text; received ${preview(next.value)}`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tif (delta.channel === 'content') {\n\t\t\t\t\tstreamed.push(delta.text)\n\t\t\t\t\tmeter?.charge(delta.text)\n\t\t\t\t\tyield { category: 'token', content: delta.text }\n\t\t\t\t} else {\n\t\t\t\t\tyield { category: 'think', content: delta.text }\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\treturn attempt((): ProviderResult => {\n\t\t\t\tthrow error\n\t\t\t})\n\t\t}\n\t}\n\n\t// The parent signal for a run's abort: the external signal, an optional per-run signal\n\t// (composed with, never replacing, the construction `signal`), the deadline, the effective\n\t// budget, and an allowance that is not the budget itself, folded through `AbortSignal.any` —\n\t// or a lone present one, or `undefined` when none. The budget precedes the allowance, so when\n\t// both are exhausted before the run the budget's reason is the run's.\n\t#combineSignals(\n\t\ttimeout: TimeoutInterface | undefined,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t\tsignal: AbortSignal | undefined,\n\t): AbortSignal | undefined {\n\t\tconst signals = [\n\t\t\tthis.#signal,\n\t\t\tsignal,\n\t\t\ttimeout?.signal,\n\t\t\tbudget?.signal,\n\t\t\tallowance === budget ? undefined : allowance?.signal,\n\t\t].filter((one): one is AbortSignal => one !== undefined)\n\t\tif (signals.length === 0) return undefined\n\t\tif (signals.length === 1) return signals[0]\n\t\treturn AbortSignal.any(signals)\n\t}\n}\n","import type { AgentJobInput, AgentRegistryInterface, AgentResult } from './types.js'\nimport type { ControllerInterface } from '@orkestrel/workflow'\nimport { settleAgentJob } from './helpers.js'\n\n/**\n * Executes one runner's agent jobs — spawning each unit's declared children on its first attempt\n * only, then rehydrating the unit through a registry with the controller signal and settling it\n * under the shared partial-result policy.\n *\n * @remarks\n * One instance serves one runner, because it records every unit it has fanned out. The runner\n * calls its handler once per attempt with the same unit id, so a retried attempt finds its unit\n * recorded and spawns no child again. Children are fired through the controller without being\n * awaited inline, so a bounded runner frees the parent's slot for them. `createAgentRunner`\n * creates one instance per runner and binds its `execute` method as the runner's handler.\n *\n * @example\n * ```ts\n * import type { AgentJobInput, ProviderInterface } from '@orkestrel/agent'\n * import { AgentFanout, createAgentRegistry } from '@orkestrel/agent'\n * import { createRunner } from '@orkestrel/workflow'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const fanout = new AgentFanout(createAgentRegistry({ providers: { main: provider } }), false)\n * const runner = createRunner({ handler: fanout.execute.bind(fanout), retries: 1 })\n * const child: AgentJobInput = { provider: 'main', messages: [{ role: 'user', content: 'child' }] }\n * const parent: AgentJobInput = { ...child, messages: [{ role: 'user', content: 'parent' }], children: [child] }\n * const results = await runner.execute([parent]) // the parent's result, then the child's\n * ```\n */\nexport class AgentFanout {\n\treadonly #registry: AgentRegistryInterface\n\treadonly #partial: boolean\n\t// The ids of the units whose children this runner already spawned. Copy-on-write, and never\n\t// shrunk: a unit id stays recorded so every later attempt of that unit skips the fan-out.\n\t#spawned: ReadonlySet<string> = new Set<string>()\n\n\t/**\n\t * Creates the fan-out for one runner over a registry and a partial policy.\n\t *\n\t * @param registry - The registry that rehydrates each serializable job\n\t * @param partial - The partial policy. If `true`, a partial result resolves; if `false`, it throws an `AgentJobError`\n\t */\n\tconstructor(registry: AgentRegistryInterface, partial: boolean) {\n\t\tthis.#registry = registry\n\t\tthis.#partial = partial\n\t}\n\n\t/**\n\t * Executes one unit: spawns its declared children unless an earlier attempt of the unit already\n\t * did, then runs the rehydrated agent and settles it under the partial policy.\n\t *\n\t * @param controller - The runner controller for this unit's attempt\n\t * @returns The settled agent result of the unit\n\t * @throws {AgentJobError} Thrown when the run ended partial and the partial policy is `false`\n\t */\n\texecute(controller: ControllerInterface<AgentJobInput, AgentResult>): Promise<AgentResult> {\n\t\tconst children = controller.input.children\n\t\tif (children !== undefined && !this.#spawned.has(controller.id)) {\n\t\t\tthis.#spawned = new Set([...this.#spawned, controller.id])\n\t\t\tfor (const child of children) void controller.spawn(child)\n\t\t}\n\t\treturn settleAgentJob(this.#registry.build(controller.input, controller.signal), this.#partial)\n\t}\n}\n","import type {\n\tAuthorityContext,\n\tAuthorityDecision,\n\tAuthorityHandler,\n\tAuthorityInterface,\n\tAuthorityOptions,\n\tAuthorityRule,\n} from './types.js'\nimport { AgentError } from './errors.js'\nimport { requireFallback, requireRules } from './helpers.js'\n\n/**\n * Gates the agent loop's tool calls — the synchronous policy consulted before each call runs,\n * turning one {@link AuthorityContext} into an {@link AuthorityDecision} by walking the ordered\n * rules first-match-wins and falling back to the required `fallback`, and the approver that answers\n * an `ask` verdict.\n *\n * @remarks\n * - **Refuses at construction.** The constructor throws an `AgentError` coded `'AUTHORITY'` when the\n * `fallback` is missing or malformed, or when a rule has no valid `verdict`, so a policy with a\n * hole in it never runs.\n * - **Ordered, first-match-wins.** `evaluate` walks the configured rules in order and\n * returns the first whose `match(context)` is true as `{ zone, verdict, reason }`.\n * - **Fallback.** When no rule matches, `evaluate` returns the configured `fallback`: a\n * `verdict: 'deny'` fallback makes an allowlist (only matched rules that allow get through), and a\n * `verdict: 'allow'` fallback makes a denylist.\n * - **Consulted before each tool call.** The agent loop calls `evaluate({ call })` for\n * every {@link import('@orkestrel/tool').ToolCall} the model emits; a denied call is fed\n * back to the model as a denial {@link import('@orkestrel/tool').ToolResult} (a `tool`\n * chunk + a tool message) instead of being executed — no tool execution —\n * so the model receives the denial and can react.\n * - **Approver.** `ask` answers an `ask` verdict through the `ask` option's\n * {@link AuthorityHandler}, as a promise for a synchronous or an async handler; a handler\n * throw rejects it. With no handler it rejects with an `AgentError` coded `'APPROVER'`, which\n * the loop reads as an unanswered ask. It never throws synchronously.\n * - **Synchronous.** `evaluate` returns the verdict directly; only `ask` is async.\n * - **Event-free.** A purely functional gate — no Emitter, no events.\n *\n * @example\n * ```ts\n * // A denylist: deny the `delete` tool, allow everything else.\n * const authority = new Authority({\n * \trules: [{ match: (c) => c.call.name === 'delete', zone: 'restricted', verdict: 'deny' }],\n * \tfallback: { zone: 'default', verdict: 'allow' },\n * })\n * authority.evaluate({ call: { id: '1', name: 'delete', arguments: {} } }) // { zone: 'restricted', verdict: 'deny' }\n * authority.evaluate({ call: { id: '2', name: 'add', arguments: {} } }) // { zone: 'default', verdict: 'allow' }\n * ```\n */\nexport class Authority implements AuthorityInterface {\n\treadonly #rules: readonly AuthorityRule[]\n\treadonly #fallback: AuthorityDecision\n\treadonly #handler: AuthorityHandler | undefined\n\n\tconstructor(options: AuthorityOptions) {\n\t\tthis.#fallback = requireFallback(options)\n\t\tthis.#rules = requireRules(options.rules)\n\t\tthis.#handler = options.ask\n\t}\n\n\tevaluate(context: AuthorityContext): AuthorityDecision {\n\t\tfor (const rule of this.#rules) {\n\t\t\tif (rule.match(context))\n\t\t\t\treturn {\n\t\t\t\t\tzone: rule.zone,\n\t\t\t\t\tverdict: rule.verdict,\n\t\t\t\t\t...(rule.reason === undefined ? {} : { reason: rule.reason }),\n\t\t\t\t}\n\t\t}\n\t\treturn this.#fallback\n\t}\n\n\tasync ask(\n\t\tcontext: AuthorityContext,\n\t\tdecision: AuthorityDecision,\n\t\tsignal: AbortSignal,\n\t): Promise<boolean> {\n\t\tif (this.#handler === undefined) {\n\t\t\tthrow new AgentError('APPROVER', 'an asked call needs an approver and none is set', {\n\t\t\t\tcontext: { tool: context.call.name },\n\t\t\t})\n\t\t}\n\t\treturn this.#handler(context, decision, signal)\n\t}\n}\n","import type {\n\tAgentAnswer,\n\tAgentFrame,\n\tApproverInterface,\n\tAuthorityContext,\n\tAuthorityDecision,\n\tAuthorityHandler,\n} from './types.js'\nimport type { ToolCall } from '@orkestrel/tool'\nimport { preview } from '@orkestrel/contract'\nimport { AgentError } from './errors.js'\nimport { renderArguments } from './helpers.js'\n\n/**\n * Holds a server's waiting asks between the run that asked them and the page that answers them — an\n * {@link ApproverInterface} whose `ask` is the authority's handler, keyed by the asked call object.\n *\n * @remarks\n * - **One bound handler.** The constructor binds `ask` once, so `ask: approver.ask` and a\n * destructured `ask` both register on this approver, and every read returns the same function.\n * - **Keyed by the call.** Each waiting ask is found through the call object the run handed the\n * handler, never through `call.id`, which a provider can repeat. The ask mints its own `id`, and\n * its frame carries that `id`, the tool `name`, the decision's `zone`, and the call's `arguments`\n * rendered by {@link renderArguments}.\n * - **Fail closed.** A call framed while no ask waits for it, a second ask for a waiting call, and\n * arguments that cannot be rendered each reject with `AgentError` code `'APPROVER'`, which the\n * loop reads as unanswered. A run abort deletes the waiting ask and resolves it `false`.\n * - **Strict approval.** An answer settles only the ask whose `id` it names, and only\n * `approved: true` approves.\n *\n * @example\n * ```ts\n * const approver = new Approver()\n * const authority = createAuthority({\n * \trules,\n * \tfallback: { zone: 'default', verdict: 'deny' },\n * \task: approver.ask,\n * })\n * approver.answer({ id, approved: true }) // true while that ask waits\n * ```\n */\nexport class Approver implements ApproverInterface {\n\t// The waiting asks, keyed by the call object the run handed the handler; each entry is deleted on\n\t// every path that settles it.\n\treadonly #asks = new Map<\n\t\tToolCall,\n\t\t{\n\t\t\treadonly id: string\n\t\t\treadonly frame: AgentFrame\n\t\t\treadonly resolve: (approved: boolean) => void\n\t\t\treadonly settled: AbortController\n\t\t}\n\t>()\n\t// The calls framed while no ask waited for them; a later ask for one is refused.\n\treadonly #missed = new WeakSet<ToolCall>()\n\t// The handler bound once to this approver, so every read of `ask` returns the same function.\n\treadonly #ask: AuthorityHandler\n\n\tconstructor() {\n\t\tthis.#ask = this.#register.bind(this)\n\t}\n\n\tget ask(): AuthorityHandler {\n\t\treturn this.#ask\n\t}\n\n\tframe(call: ToolCall): AgentFrame | undefined {\n\t\tconst entry = this.#asks.get(call)\n\t\tif (entry === undefined) {\n\t\t\tthis.#missed.add(call)\n\t\t\treturn undefined\n\t\t}\n\t\treturn entry.frame\n\t}\n\n\tanswer(answer: AgentAnswer): boolean {\n\t\tconst found = [...this.#asks].find(([, entry]) => entry.id === answer.id)\n\t\tif (found === undefined) return false\n\t\tconst [call, entry] = found\n\t\tthis.#asks.delete(call)\n\t\tentry.settled.abort()\n\t\tentry.resolve(answer.approved === true)\n\t\treturn true\n\t}\n\n\t// Registers one ask before its first suspension, so a reader that pulls the run's `ask` chunk\n\t// finds it waiting; a run abort deletes it and resolves it `false`.\n\tasync #register(\n\t\tcontext: AuthorityContext,\n\t\tdecision: AuthorityDecision,\n\t\tsignal: AbortSignal,\n\t): Promise<boolean> {\n\t\tconst call = context.call\n\t\tif (signal.aborted) return false\n\t\tif (this.#missed.has(call)) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'APPROVER',\n\t\t\t\t`the ask for ${preview(call.name)} arrived after its chunk was framed; enter the approver synchronously from the authority's ask handler`,\n\t\t\t\t{ context: { tool: preview(call.name) } },\n\t\t\t)\n\t\t}\n\t\tif (this.#asks.has(call)) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'APPROVER',\n\t\t\t\t`an ask for ${preview(call.name)} already waits; ask each call once`,\n\t\t\t\t{ context: { tool: preview(call.name) } },\n\t\t\t)\n\t\t}\n\t\tconst rendered = renderArguments(call.arguments)\n\t\tif (rendered === undefined) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'APPROVER',\n\t\t\t\t`the arguments of ${preview(call.name)} cannot be rendered as JSON; pass JSON arguments`,\n\t\t\t\t{ context: { tool: preview(call.name) } },\n\t\t\t)\n\t\t}\n\t\tconst id = crypto.randomUUID()\n\t\tconst settled = new AbortController()\n\t\tconst pending = Promise.withResolvers<boolean>()\n\t\tthis.#asks.set(call, {\n\t\t\tid,\n\t\t\tframe: { category: 'ask', id, name: call.name, zone: decision.zone, arguments: rendered },\n\t\t\tresolve: pending.resolve,\n\t\t\tsettled,\n\t\t})\n\t\tsignal.addEventListener(\n\t\t\t'abort',\n\t\t\t() => {\n\t\t\t\tthis.#asks.delete(call)\n\t\t\t\tpending.resolve(false)\n\t\t\t},\n\t\t\t{ once: true, signal: settled.signal },\n\t\t)\n\t\treturn pending.promise\n\t}\n}\n","import type {\n\tAgentInterface,\n\tAgentJobInput,\n\tAgentOptions,\n\tAgentRegistryInterface,\n\tAgentRegistryOptions,\n\tAuthorityInterface,\n} from './types.js'\nimport type { ProviderInterface } from '../providers/index.js'\nimport type { ConversationStoreInterface } from '../conversations/index.js'\nimport type { ToolInterface } from '@orkestrel/tool'\nimport type { SchedulerInterface } from '@orkestrel/workflow'\nimport { describeFaults } from '@orkestrel/contract'\nimport { createTokenBudget } from '@orkestrel/budget'\nimport { ToolManager } from '@orkestrel/tool'\nimport { Agent } from './Agent.js'\nimport { agentJobContract } from './contracts.js'\nimport { AgentError } from './errors.js'\nimport { ConversationManager } from '../conversations/index.js'\nimport { requireEntry } from './helpers.js'\n\n/**\n * Makes a durable, JSON-serializable {@link AgentJobInput} runnable — holds the named pools of\n * live, non-serializable pieces (providers, tools, authorities, schedulers), throws on a name\n * absent from its pool, and its `build` method returns a seeded, signal-wired {@link Agent} from a job's names and\n * data.\n *\n * @remarks\n * - **Why it exists.** An `AgentJobInput` is serializable so it can survive a crash in a\n * Queue's store; the live objects it needs (a provider with sockets, tools / rules /\n * schedulers carrying functions) cannot serialize. The registry closes that gap:\n * construct it once with the live pools, then a queue / runner handler calls `build`\n * on each (possibly restored) job to get a ready agent.\n * - **Accessors throw on a miss.** `provider` / `tool` / `authority` / `scheduler` resolve a\n * name against their pool and throw an {@link AgentError} carrying `code: 'REGISTRY'` and\n * the message `unknown <category>: <name>` when it is absent — an unknown name in a\n * rehydrated job is a programmer / config error that must fail loudly at build time, never\n * silently resolve to `undefined` and run an agent missing a dependency.\n * - **`build` rehydrates.** Resolve the job's `provider`; assemble a fresh\n * {@link ToolManager} from the `tools` names, which refuses with `code: 'REGISTRY'` when the job\n * names no `authority`; rebuild the token `budget` from its\n * ceiling (`createTokenBudget({ max })`); resolve the optional `authority` /\n * `scheduler` names; construct the {@link Agent} with `system` / `limit` / `timeout` /\n * the threaded `signal`; seed its context with the job's `messages`; return it. The\n * `signal` is the queue attempt's / runner unit's cancel, so a bounded abort propagates\n * into the agent (which commits a partial — the job's `partial` policy then\n * decides success vs. retry). A job `limit` that is not a positive safe integer throws an\n * {@link AgentError} carrying `code: 'LIMIT'` from the agent's construction.\n * - **Event-free.** A pure resolver — no Emitter, no events.\n *\n * @example\n * ```ts\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const registry = new AgentRegistry({ providers: { main: provider } })\n * const agent = registry.build({ provider: 'main', messages: [{ role: 'user', content: 'Say ok.' }] })\n * const result = await agent.generate()\n * ```\n */\nexport class AgentRegistry implements AgentRegistryInterface {\n\treadonly #providers: ReadonlyMap<string, ProviderInterface>\n\treadonly #tools: ReadonlyMap<string, ToolInterface>\n\treadonly #authorities: ReadonlyMap<string, AuthorityInterface>\n\treadonly #schedulers: ReadonlyMap<string, SchedulerInterface>\n\treadonly #store: ConversationStoreInterface | undefined\n\n\tconstructor(options: AgentRegistryOptions) {\n\t\tthis.#providers = new Map(Object.entries(options.providers))\n\t\tthis.#tools = new Map(Object.entries(options.tools ?? {}))\n\t\tthis.#authorities = new Map(Object.entries(options.authorities ?? {}))\n\t\tthis.#schedulers = new Map(Object.entries(options.schedulers ?? {}))\n\t\tthis.#store = options.store\n\t}\n\n\tprovider(name: string): ProviderInterface {\n\t\treturn requireEntry(this.#providers, 'provider', name)\n\t}\n\n\ttool(name: string): ToolInterface {\n\t\treturn requireEntry(this.#tools, 'tool', name)\n\t}\n\n\tauthority(name: string): AuthorityInterface {\n\t\treturn requireEntry(this.#authorities, 'authority', name)\n\t}\n\n\tscheduler(name: string): SchedulerInterface {\n\t\treturn requireEntry(this.#schedulers, 'scheduler', name)\n\t}\n\n\tbuild(input: AgentJobInput, signal?: AbortSignal): AgentInterface {\n\t\t// A job is an owned record, so it crosses exactly: no key is repaired, and a typo refuses.\n\t\tconst faults = agentJobContract.audit(input)\n\t\tif (faults.length > 0) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'REGISTRY',\n\t\t\t\t`a job must hold only the declared members of AgentJobInput\\n${describeFaults(faults, agentJobContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tconst provider = this.provider(input.provider)\n\t\tconst agent = new Agent(provider, this.#buildOptions(input, signal))\n\t\t// Seed the conversation onto the rehydrated agent's context — the serializable\n\t\t// MessageInputs become stored messages (each id minted by the manager).\n\t\tfor (const message of input.messages ?? []) agent.context.messages.add(message)\n\t\treturn agent\n\t}\n\n\t// Assemble the AgentOptions for one job: a fresh ToolManager loaded from the named\n\t// tools, the rebuilt token budget, the resolved authority / scheduler, plus the data\n\t// fields and the threaded cancel. Optional fields are omitted (not set to `undefined`)\n\t// so the Agent's `?? default` fallbacks behave exactly as for a hand-built agent. When\n\t// the registry carries a conversation store, thread a fresh store-backed\n\t// ConversationManager for this build (a fresh conversation id per build ⇒ no\n\t// collisions in the shared store) — omitted when no store is set, so the shape matches a\n\t// registry with no `store`.\n\t#buildOptions(input: AgentJobInput, signal: AbortSignal | undefined): AgentOptions {\n\t\tconst names = input.tools ?? []\n\t\tif (names.length > 0 && input.authority === undefined) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'REGISTRY',\n\t\t\t\t\"a job that names tools and no authority cannot run them; add authority: 'NAME' to the job and register that NAME under authorities\",\n\t\t\t\t{ context: { tools: names } },\n\t\t\t)\n\t\t}\n\t\tconst budget = input.budget === undefined ? undefined : createTokenBudget({ max: input.budget })\n\t\treturn {\n\t\t\t...(names.length === 0 ? {} : { tools: this.#buildToolManager(names) }),\n\t\t\t...(input.system === undefined ? {} : { system: input.system }),\n\t\t\t...(input.limit === undefined ? {} : { limit: input.limit }),\n\t\t\t...(input.timeout === undefined ? {} : { timeout: input.timeout }),\n\t\t\t...(budget === undefined ? {} : { budget }),\n\t\t\t...(input.authority === undefined ? {} : { authority: this.authority(input.authority) }),\n\t\t\t...(input.scheduler === undefined ? {} : { scheduler: this.scheduler(input.scheduler) }),\n\t\t\t...(this.#store === undefined\n\t\t\t\t? {}\n\t\t\t\t: { conversations: new ConversationManager({ store: this.#store }) }),\n\t\t\t...(signal === undefined ? {} : { signal }),\n\t\t}\n\t}\n\n\t// A fresh tool registry loaded with the named tools (each resolved — an unknown name\n\t// throws). Always a new manager per build, so concurrent jobs never share one.\n\t#buildToolManager(names: readonly string[]): ToolManager {\n\t\tconst manager = new ToolManager()\n\t\tfor (const name of names) manager.add(this.tool(name))\n\t\treturn manager\n\t}\n}\n","import type { ProviderInterface } from '../providers/index.js'\nimport type {\n\tAgentInterface,\n\tAgentJobInput,\n\tAgentOptions,\n\tAgentQueueOptions,\n\tAgentRegistryInterface,\n\tAgentRegistryOptions,\n\tAgentResult,\n\tAgentRunnerOptions,\n\tApproverInterface,\n\tAuthorityInterface,\n\tAuthorityOptions,\n\tChannelInterface,\n} from './types.js'\nimport type { QueueInterface } from '@orkestrel/queue'\nimport type { RunnerInterface } from '@orkestrel/workflow'\nimport { createQueue } from '@orkestrel/queue'\nimport { createRunner } from '@orkestrel/workflow'\nimport { Agent } from './Agent.js'\nimport { AgentFanout } from './AgentFanout.js'\nimport { Channel } from './Channel.js'\nimport { Authority } from './Authority.js'\nimport { Approver } from './Approver.js'\nimport { AgentRegistry } from './AgentRegistry.js'\nimport { extractQueueOptions, handleAgentQueueJob } from './helpers.js'\n\n/**\n * Creates an agent loop — an {@link AgentInterface} composing a\n * {@link ProviderInterface}, its {@link AgentContextInterface}, and a tool registry\n * into a bounded context → provider → tools → repeat turn, exposed as a one-shot\n * `generate` and a live `stream`.\n *\n * @remarks\n * One private loop drives the turn; `generate` awaits the settled result of the same run\n * `stream` exposes and leaves its live chunks undrained, so they can never diverge. Each turn is bounded by one cancel folded from\n * `signal` + `timeout` + `budget` + `allowance` (through `AbortSignal.any`) — any trip (or `abort()`)\n * commits a partial result (the stream's `result` resolves on a cancel, rejects only\n * on a genuine provider / tool error). The `scheduler` paces between turns; tool\n * iteration is capped at `limit`. Default: `DEFAULT_AGENT_LIMIT`. Tools are advertised\n * structurally through `context.tools.definitions()`. Two observation surfaces: the\n * {@link AgentChunk} stream (pull — per-token content, usage, `ask`, and `tool` chunks) and a typed `emitter` (push —\n * lifecycle + `usage` / `tool` / `deny` for fire-and-forget observers).\n *\n * @param provider - The {@link ProviderInterface} the loop drives each turn\n * @param options - Optional `system` / `tools` / `limit` / `timeout` / `budget` /\n * `scheduler` / `signal` (see {@link AgentOptions})\n * @returns A working {@link AgentInterface}\n *\n * @example\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createAgent } from '@orkestrel/agent'\n * import { createTokenBudget } from '@orkestrel/budget'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const agent = createAgent(provider, {\n * \tsystem: 'You are concise.',\n * \tbudget: createTokenBudget({ max: 50_000, scope: 'total' }),\n * })\n * agent.context.messages.add({ role: 'user', content: 'Say hi.' })\n *\n * const stream = agent.stream()\n * for await (const chunk of stream.events) {\n * \tif (chunk.category === 'token') process.stdout.write(chunk.content)\n * }\n * const result = await stream.result // { content, usage?, partial }\n * ```\n */\nexport function createAgent(provider: ProviderInterface, options?: AgentOptions): AgentInterface {\n\treturn new Agent(provider, options)\n}\n\n/**\n * Creates an empty unbounded async channel — a {@link ChannelInterface} a producer writes\n * values into (`push`) and ends (`close` / `fail`) regardless of consumption, while a\n * consumer reads them back live through `drain`.\n *\n * @remarks\n * Write and read are decoupled, so the producer never waits for a consumer: an agent's eager\n * pump writes each chunk into one, which is why a run's `result` settles whether or not the\n * live events are drained. A value pushed at an already-parked consumer is delivered, buffered\n * values are yielded before the end is reported, and the first failure wins.\n *\n * @typeParam T - The value type the channel carries\n * @returns A fresh, empty {@link ChannelInterface}\n *\n * @example\n * ```ts\n * import { createChannel } from '@orkestrel/agent'\n *\n * const channel = createChannel<number>()\n * channel.push(1)\n * channel.close()\n * for await (const value of channel.drain()) {\n * \tvalue // 1\n * }\n * ```\n */\nexport function createChannel<T>(): ChannelInterface<T> {\n\treturn new Channel<T>()\n}\n\n/**\n * Creates a policy gate — an {@link AuthorityInterface} the agent loop consults before\n * each tool call runs, evaluating the ordered rules first-match-wins and falling back\n * to the required `fallback` when none match.\n *\n * @remarks\n * `rules` are evaluated in order — the first whose `match` is true decides, and each rule names its\n * `verdict`. When no rule matches, the `fallback` decides: a `verdict: 'deny'` fallback makes an\n * allowlist, and a `verdict: 'allow'` fallback makes a denylist. `ask` answers each `ask` verdict;\n * with none, every ask is refused as unanswered. Wire the result into `createAgent` through\n * `AgentOptions.authority`: a denied call is fed back to the model as a denial `ToolResult` (not\n * executed), so the model can react. Synchronous — `evaluate` returns the verdict directly; only\n * `ask` is async.\n *\n * @param options - The `fallback`, and optional `rules` (ordered) and `ask` (see {@link AuthorityOptions})\n * @returns A working {@link AuthorityInterface}\n * @throws {AgentError} Thrown when the `fallback` is missing or malformed, or a rule has no valid `verdict` (code `'AUTHORITY'`)\n *\n * @example\n * ```ts\n * import { createAgent, createAuthority } from '@orkestrel/agent'\n *\n * // Deny the `delete` tool, allow everything else.\n * const authority = createAuthority({\n * \trules: [{ match: (c) => c.call.name === 'delete', zone: 'restricted', verdict: 'deny' }],\n * \tfallback: { zone: 'default', verdict: 'allow' },\n * })\n * const agent = createAgent(provider, { tools, authority })\n * ```\n */\nexport function createAuthority(options: AuthorityOptions): AuthorityInterface {\n\treturn new Authority(options)\n}\n\n/**\n * Creates an approver — an {@link ApproverInterface} that holds a server's waiting asks between\n * the run that asked them and the page that answers them.\n *\n * @remarks\n * Wire its bound `ask` as the authority's handler, project the run through\n * `agentStreamToFrames(stream, approver)`, and settle each `ask` frame with `answer`. One approver\n * serves one principal: an answer settles only an ask this approver holds.\n *\n * @returns A working {@link ApproverInterface} with no waiting ask\n *\n * @example\n * ```ts\n * const approver = createApprover()\n * const authority = createAuthority({\n * \trules,\n * \tfallback: { zone: 'default', verdict: 'deny' },\n * \task: approver.ask,\n * })\n * const agent = createAgent(provider, { tools, authority })\n * const stream = agent.stream({ prompt: { content: 'Tidy the notes.' } })\n * for await (const frame of agentStreamToFrames(stream, approver)) {\n * \tif (frame.category === 'ask') approver.answer({ id: frame.id, approved: true })\n * }\n * ```\n */\nexport function createApprover(): ApproverInterface {\n\treturn new Approver()\n}\n\n/**\n * Creates a registry of live providers, tools, authorities, and schedulers whose `build` method returns a seeded, signal-wired {@link AgentInterface} from a serializable job.\n *\n * @remarks\n * `providers` is required; `tools` / `authorities` / `schedulers` are optional pools.\n * The accessors (`provider` / `tool` / `authority` / `scheduler`) throw an\n * {@link import('./errors.js').AgentError} carrying `code: 'REGISTRY'` and the message\n * `unknown <category>: <name>` on an unregistered name — a misconfigured or crash-restored\n * job fails loudly rather than running with a missing dependency. `build(input, signal)`\n * resolves the names, rebuilds the token budget from its ceiling, seeds the agent's\n * context with the job's messages, and threads `signal` so a queue / runner abort\n * propagates. This is the bridge that makes durable, serializable agent jobs runnable.\n *\n * @param options - The named pools (see {@link AgentRegistryOptions})\n * @returns A working {@link AgentRegistryInterface}\n *\n * @example\n * ```ts\n * import { createAgentRegistry, createAuthority } from '@orkestrel/agent'\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { numberShape, objectShape } from '@orkestrel/contract'\n * import { createTool } from '@orkestrel/tool'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const registry = createAgentRegistry({\n * \tproviders: { main: provider },\n * \ttools: {\n * \t\tadd: createTool({\n * \t\t\tname: 'add',\n * \t\t\tcontract: objectShape({ x: numberShape(), y: numberShape() }),\n * \t\t\texecute: (a) => Number(a.x) + Number(a.y),\n * \t\t}),\n * \t},\n * \tauthorities: { open: createAuthority({ fallback: { zone: 'default', verdict: 'allow' } }) },\n * })\n * const agent = registry.build({\n * \tprovider: 'main',\n * \tmessages: [{ role: 'user', content: 'Hi.' }],\n * \ttools: ['add'],\n * \tauthority: 'open',\n * })\n * ```\n */\nexport function createAgentRegistry(options: AgentRegistryOptions): AgentRegistryInterface {\n\treturn new AgentRegistry(options)\n}\n\n/**\n * Creates a durable, bounded-concurrency agent-job queue — a {@link QueueInterface} over\n * serializable values of {@link AgentJobInput} that composes `createQueue`: each job is rehydrated\n * through the `registry` into a live {@link AgentInterface}, run to its {@link AgentResult},\n * and subjected to the partial-as-configurable-failure policy.\n *\n * @remarks\n * - **Composes the substrate (no new engine).** The handler is the only new logic;\n * bounded `concurrency`, `retries`, the per-attempt `timeout`, and durable persistence\n * through `store` (+ `restore()` after a crash) are all the backing Queue's. `enqueue`\n * returns a per-job promise.\n * - **Durable + serializable.** Because `AgentJobInput` is JSON-serializable, a `store`\n * persists outstanding jobs; `restore()` re-enqueues them and the `registry` rehydrates the\n * live pieces from the names. A job survives a crash only through a store whose rows outlive\n * the process (for example `createDatabaseQueueStore`); a `createMemoryQueueStore` row is\n * gone when the process exits.\n * - **Partial policy.** A partial result throws an\n * {@link import('./errors.js').AgentJobError} by default, so a job cancelled by its\n * attempt deadline / a queue abort retries while attempts remain; `partial: true`\n * resolves the partial as success instead.\n * - **Cancellation threads through.** The handler passes `context.signal` into\n * `registry.build`, so a queue `abort()` or a per-attempt timeout cancels the in-flight\n * agent (which commits a partial → throws → retries / fails per policy).\n *\n * @param options - The `registry`, the `partial` policy, and the substrate knobs\n * (`concurrency` / `retries` / `timeout` / `store`) (see {@link AgentQueueOptions})\n * @returns A {@link QueueInterface} of {@link AgentJobInput} → {@link AgentResult}\n *\n * @example\n * ```ts\n * import type { AgentJobInput, ProviderInterface } from '@orkestrel/agent'\n * import type { QueueStoreInterface } from '@orkestrel/queue'\n * import { createAgentQueue, createAgentRegistry } from '@orkestrel/agent'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * declare const store: QueueStoreInterface<AgentJobInput> // a durable store the host supplies\n * const registry = createAgentRegistry({ providers: { main: provider } })\n * const queue = createAgentQueue({ registry, concurrency: 2, retries: 1, store })\n * const result = await queue.enqueue({ provider: 'main', messages: [{ role: 'user', content: 'ok?' }] })\n * ```\n */\nexport function createAgentQueue(\n\toptions: AgentQueueOptions,\n): QueueInterface<AgentJobInput, AgentResult> {\n\tconst { registry, partial = false, store } = options\n\treturn createQueue<AgentJobInput, AgentResult>({\n\t\t...extractQueueOptions(options),\n\t\t...(store === undefined ? {} : { store }),\n\t\thandler: handleAgentQueueJob.bind(undefined, registry, partial),\n\t})\n}\n\n/**\n * Creates an agent-job runner — a {@link RunnerInterface} over serializable\n * values of {@link AgentJobInput} that composes `createRunner` (one-shot, ordered, fail-fast), each unit\n * rehydrated through the `registry` and subjected to the partial policy. The runner also carries\n * sub-agent fan-out: a parent job's handler can call `controller.spawn` with a child job.\n *\n * @remarks\n * - **Composes the substrate (no new engine).** Bounded `concurrency`, `retries`, the\n * per-attempt `timeout`, ordered results, and fail-fast are all the backing Runner's;\n * the handler adds only rehydration + the partial policy.\n * - **Sub-agent fan-out.** Each unit's handler receives a `ControllerInterface` whose\n * `spawn(childJob)` launches a child agent job through the same bounded queue (the\n * child's result joins the run after the declared units, in spawn order). A unit spawns its\n * declared `children` on its first attempt only, so a retried parent never runs a child\n * twice. On a bounded\n * runner, fan out and return — do not await a spawn inline from within the handler (a\n * slot-holding handler awaiting its own spawn can deadlock; see `ControllerInterface`).\n * - **Partial policy + cancellation.** Same as `createAgentQueue`: a partial result\n * throws by default (the run's fail-fast engages), `partial: true` resolves it; the\n * handler threads `controller.signal` into `registry.build`, so a runner abort / a\n * per-attempt timeout cancels the agent.\n *\n * @param options - The `registry`, the `partial` policy, and the substrate knobs\n * (`concurrency` / `retries` / `timeout`) (see {@link AgentRunnerOptions})\n * @returns A {@link RunnerInterface} of {@link AgentJobInput} → {@link AgentResult}\n *\n * @example\n * ```ts\n * import { createAgentRunner, createAgentRegistry } from '@orkestrel/agent'\n *\n * const registry = createAgentRegistry({ providers: { main: provider } })\n * const runner = createAgentRunner({ registry, concurrency: 2 })\n * // Run one job that fans out a child; results hold the parent, then the spawn.\n * const child = { provider: 'main', messages: [{ role: 'user', content: 'child' }] }\n * const parent = { provider: 'main', messages: [{ role: 'user', content: 'parent' }], children: [child] }\n * const results = await runner.execute([parent]) // declared first, then any spawns\n * ```\n */\nexport function createAgentRunner(\n\toptions: AgentRunnerOptions,\n): RunnerInterface<AgentJobInput, AgentResult> {\n\tconst { registry, partial = false } = options\n\tconst fanout = new AgentFanout(registry, partial)\n\treturn createRunner<AgentJobInput, AgentResult>({\n\t\t...extractQueueOptions(options),\n\t\thandler: fanout.execute.bind(fanout),\n\t})\n}\n","import type { LedgerCategory, LedgerNote, LedgerQuestion, LedgerShare } from './types.js'\n\n/**\n * Lists the categories the ledger files a message under, in the order the category question\n * names them — the one list the {@link LedgerCategory} union derives from.\n */\nexport const LEDGER_CATEGORIES = Object.freeze([\n\t'fact',\n\t'rule',\n\t'correction',\n\t'request',\n\t'opinion',\n\t'chatter',\n\t'distractor',\n] as const)\n\n/** Lists the categories whose messages the projection leaves out as quiet. */\nexport const QUIET_CATEGORIES: readonly LedgerCategory[] = Object.freeze(['chatter', 'distractor'])\n\n/** Lists the categories whose messages state what the desk acts on, which the records render. */\nexport const DECISIVE_CATEGORIES: readonly LedgerCategory[] = Object.freeze([\n\t'fact',\n\t'rule',\n\t'correction',\n])\n\n/** Lists the categories that place a message no owner claims on the rules record. */\nexport const PLACED_CATEGORIES: readonly LedgerCategory[] = Object.freeze(['rule', 'correction'])\n\n/** Names the key of the record that holds the rules no owner claims. */\nexport const LEDGER_RULES_KEY = 'rules'\n\n/** Prefixes the key of an owner's record, which the owner's id follows. */\nexport const LEDGER_OWNER_PREFIX = 'owner:'\n\n/**\n * Supplies the wording of every question the ledger asks its judge.\n *\n * @remarks\n * The wording names a support desk. Fit the cutoffs for the wording and judge you use.\n */\nexport const LEDGER_QUESTIONS: LedgerQuestion = Object.freeze({\n\tcategory: Object.freeze({\n\t\tform: 'choice',\n\t\tinstructions: 'Which category best describes what this support-desk message states?',\n\t\tcriteria: Object.freeze({\n\t\t\tfact: 'States a fact about a customer, an order, an account, the desk, or the day',\n\t\t\trule: 'States a standing rule, policy, or instruction the desk must follow',\n\t\t\tcorrection: 'Corrects, replaces, or withdraws a value or rule stated earlier',\n\t\t\trequest: 'Asks the assistant to do a task or to answer a question',\n\t\t\topinion: 'States a personal view or judgment rather than a fact',\n\t\t\tchatter: 'Talk with the agent that states nothing the desk acts on',\n\t\t\tdistractor: \"A statement about something outside the desk's work\",\n\t\t}),\n\t}),\n\ttopic: 'Does this support-desk message concern the named desk topic?',\n\tamends: Object.freeze({\n\t\tform: 'noul',\n\t\tinstructions:\n\t\t\t'Does the later message replace or withdraw any value or rule the earlier message states?',\n\t\tcriteria: Object.freeze({\n\t\t\ttrue: 'The later message replaces or withdraws at least one value or rule the earlier message states',\n\t\t\tfalse:\n\t\t\t\t'Every value and rule the earlier message states stays in force after the later message',\n\t\t}),\n\t}),\n\tsupersedes: Object.freeze({\n\t\tform: 'noul',\n\t\tinstructions:\n\t\t\t'Does the later message replace or withdraw everything the earlier message states?',\n\t\tcriteria: Object.freeze({\n\t\t\ttrue: 'The later message replaces or withdraws everything the earlier message states',\n\t\t\tfalse: 'Some value or rule the earlier message states stays in force after the later message',\n\t\t}),\n\t}),\n})\n\n/**\n * Supplies the text of each ledger note, worded for a model that answers in its final\n * message.\n */\nexport const LEDGER_NOTES: LedgerNote = Object.freeze({\n\tcue: 'Give your complete answer to the request now, from what you already have.',\n\tresults: 'Your lookups and recalls in this request returned:',\n\trepeat:\n\t\t'You already have this result earlier in this request; give your complete answer now as your final message.',\n\tclosure:\n\t\t'recall is closed for the rest of this request; give your final answer from what you have',\n})\n\n/**\n * Supplies the default prompt and tail shares.\n */\nexport const DEFAULT_LEDGER_SHARE: LedgerShare = Object.freeze({\n\tprompt: 0.7,\n\ttail: 0.35,\n})\n\n/**\n * Caps the tool-iteration turns of a ledger's agent at 8 turns.\n */\nexport const DEFAULT_LEDGER_LIMIT = 8\n\n/**\n * Caps the `recall` calls of one request at 2 calls.\n */\nexport const DEFAULT_RECALL_LIMIT = 2\n\n/**\n * Sets the 5 milliseconds a paced classifier files between host turns.\n *\n * @remarks\n * Time, not a count of steps, so a slice on a slow device is the same 5 milliseconds.\n */\nexport const LEDGER_SLICE = 5\n\n/**\n * Holds back the share of the prompt budget the scale can rise by between calibration and a\n * request's first call.\n *\n * @remarks\n * Across the `ledger-deny` and `ledger-admit` series of 2026-10-08, a\n * request's first-call tokens per estimate unit rose at most from 1.150 at the seed to 1.212, 5.4\n * percent; the ledger divides its prompt budget by 1.06.\n */\nexport const LEDGER_SCALE_DRIFT = 0.06\n\n/**\n * Matches a run of letters and digits, which the ledger reads as one word.\n *\n * @remarks\n * `String.prototype.match` resets the pattern's `lastIndex`, so this shared instance keeps no\n * position between reads.\n */\nexport const LEDGER_WORD = /[\\p{L}\\p{N}]+/gu\n\n/** Matches the logprob decoding failure `invalid or duplicate top logprob token`, which the ledger holds for the ledger's life. */\nexport const DETERMINISTIC_JUDGE_ERROR = /invalid or duplicate top logprob token/\n","import type { LedgerErrorCode } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport { INTRINSICS, holds, isFunction, isObject } from '@orkestrel/contract'\n\n/**\n * Reports an invalid ledger configuration, failed calibration, or unowned request selection,\n * carrying the machine-readable `code`.\n *\n * @remarks\n * A threshold, share, capacity, limit, topic, lookup, question, or gauge outside its bounds is a\n * programmer error and throws this with the matching {@link LedgerErrorCode}. A calibration call that reports\n * no prompt usage throws this with `'GAUGE'`. A snapshot option that `isLedgerSnapshot` refuses\n * throws this with `'SNAPSHOT'`, its `context` holding the `faults`. Narrow a caught value with {@link isLedgerError} and\n * branch on `error.code`. A selection without an active `respond` call, for a conversation other\n * than the ledger's, or for a request other than that call's request or a ledger note, reports\n * `'REQUEST'` through the selection's fault.\n */\nexport class LedgerError extends Error {\n\t/** Names the machine-readable condition; the {@link LedgerErrorCode} union describes each code. */\n\treadonly code: LedgerErrorCode\n\t/** Holds the values that locate the failure, such as the `faults` of a refused snapshot, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Records the failure condition and its description.\n\t *\n\t * @param code - The machine-readable condition, exposed as `error.code`\n\t * @param message - The human-readable description of the failure\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: LedgerErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'LedgerError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.LedgerError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link LedgerError} through its identity brand and declared members, so a `catch` can\n * branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link LedgerError}; false otherwise\n *\n * @example\n * ```ts\n * isLedgerError(new LedgerError('GAUGE', 'calibration reported no prompt usage')) // true\n * ```\n */\nexport function isLedgerError(value: unknown): value is LedgerError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.LedgerError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (\n\t\t\t!(\n\t\t\t\tcode === 'REQUEST' ||\n\t\t\t\tcode === 'THRESHOLD' ||\n\t\t\t\tcode === 'SHARE' ||\n\t\t\t\tcode === 'CAPACITY' ||\n\t\t\t\tcode === 'LIMIT' ||\n\t\t\t\tcode === 'TOPIC' ||\n\t\t\t\tcode === 'LOOKUP' ||\n\t\t\t\tcode === 'GAUGE' ||\n\t\t\t\tcode === 'REFERENCE' ||\n\t\t\t\tcode === 'SNAPSHOT' ||\n\t\t\t\tcode === 'QUESTION'\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { Message } from '../types.js'\nimport type {\n\tGaugeCall,\n\tLedgerAmendment,\n\tLedgerCategory,\n\tLedgerIndex,\n\tLedgerLine,\n\tLedgerLookupFinding,\n\tLedgerLookupState,\n\tLedgerPlanGroup,\n\tLedgerProjection,\n\tLedgerProjectionInput,\n\tLedgerProjectionRequest,\n\tLedgerRecord,\n\tLedgerRegistry,\n\tLedgerRenderContext,\n\tLedgerTopic,\n\tLedgerWordSet,\n} from './types.js'\nimport type { ToolCall } from '@orkestrel/tool'\nimport { canonicalStringify, isError, isFiniteNumber, isString } from '@orkestrel/contract'\nimport { estimateMessages } from '../agents/helpers.js'\nimport { extractLiterals, renderJSON } from '../helpers.js'\nimport { isNonNegativeSafeInteger } from '../validators.js'\nimport {\n\tLEDGER_OWNER_PREFIX,\n\tLEDGER_RULES_KEY,\n\tLEDGER_WORD,\n\tPLACED_CATEGORIES,\n} from './constants.js'\nimport { LedgerError } from './errors.js'\n\n/**\n * Collects the live message ids placed in records or left without a record.\n * @param projection - The projected records and unplaced ids\n * @returns The distinct live message ids\n * @example\n * ```ts\n * collectProjectionIds({ records: [], amendments: [], orphans: ['message-1'] }) // Set { 'message-1' }\n * ```\n */\nexport function collectProjectionIds(projection: LedgerProjection): ReadonlySet<string> {\n\treturn new Set([...projection.orphans, ...projection.records.flatMap((record) => record.members)])\n}\n\n/**\n * Scans admitted amendments in breadth-first order without revisiting a message.\n * @param amendments - The later messages attached to each earlier message\n * @param start - The first message to visit\n * @param admit - The predicate that admits a message and its descendants\n * @returns The admitted message ids in visit order\n * @example\n * ```ts\n * scanAmendments(new Map([['first', ['second']]]), 'first', () => true) // ['first', 'second']\n * ```\n */\nexport function scanAmendments(\n\tamendments: ReadonlyMap<string, readonly string[]>,\n\tstart: string,\n\tadmit: (id: string) => boolean,\n): readonly string[] {\n\tconst queue = [start]\n\tconst seen = new Set<string>()\n\tconst admitted: string[] = []\n\tfor (const id of queue) {\n\t\tif (seen.has(id)) continue\n\t\tseen.add(id)\n\t\tif (!admit(id)) continue\n\t\tadmitted.push(id)\n\t\tfor (const later of amendments.get(id) ?? []) queue.push(later)\n\t}\n\treturn admitted\n}\n\n/**\n * Builds the placeholder assistant recall call used to price recall framing.\n * @param topic - The recall topic to price\n * @returns The assistant message carrying the recall call\n * @example\n * ```ts\n * buildRecallMessage('refunds').calls?.[0]?.arguments // { topic: 'refunds' }\n * ```\n */\nexport function buildRecallMessage(topic: string): Message {\n\treturn {\n\t\tid: 'call',\n\t\trole: 'assistant',\n\t\tcontent: '',\n\t\tcalls: [{ id: 'call_00000000', name: 'recall', arguments: { topic } }],\n\t}\n}\n\n/**\n * Builds the copy of a request turn that carries the rendered records ahead of the request.\n * @param request - The stored request message\n * @param records - The rendered records, or empty text when nothing renders\n * @returns The request with its content set to the records, a `## Request` heading, and the request content, or the request itself when the records are empty\n * @example\n * ```ts\n * buildRequestMessage({ id: 'request', role: 'user', content: 'Check BW-5512.' }, '## Records\\n- Refund issued.').content\n * // '## Records\\n- Refund issued.\\n\\n## Request\\nCheck BW-5512.'\n * ```\n */\nexport function buildRequestMessage(request: Message, records: string): Message {\n\tif (records === '') return request\n\treturn { ...request, content: `${records}\\n\\n## Request\\n${request.content}` }\n}\n\n/**\n * Renders topic names as a comma-separated list in their supplied order.\n * @param topics - The configured desk topics\n * @returns The topic names joined by a comma and a space\n * @example\n * ```ts\n * renderTopicNames([{ name: 'refunds', criterion: 'Refund amounts' }]) // 'refunds'\n * ```\n */\nexport function renderTopicNames(topics: readonly LedgerTopic[]): string {\n\treturn topics.map((topic) => topic.name).join(', ')\n}\n\n/**\n * Renders an error and its causes, stopping after 4 entries.\n * @param error - The thrown value to describe\n * @returns The names and messages joined by a cause separator\n * @example\n * ```ts\n * renderCauseChain(new Error('refused', { cause: 'offline' })) // 'Error: refused <- offline'\n * ```\n */\nexport function renderCauseChain(error: unknown): string {\n\t// The error, then at most three causes: each step extends the chain by the last entry's cause.\n\treturn Array.from({ length: 3 })\n\t\t.reduce<readonly unknown[]>(\n\t\t\t(chain) => {\n\t\t\t\tconst last = chain.at(-1)\n\t\t\t\tconst cause = isError(last) ? last.cause : undefined\n\t\t\t\treturn cause === undefined ? chain : [...chain, cause]\n\t\t\t},\n\t\t\terror === undefined ? [] : [error],\n\t\t)\n\t\t.map((one) => (isError(one) ? `${one.name}: ${one.message}` : String(one)))\n\t\t.join(' <- ')\n}\n\n/**\n * Extracts lowercase words and capitalized names that do not open a sentence.\n * @param message - The message whose text supplies the words\n * @returns The distinct words and non-opening names\n * @example\n * ```ts\n * extractWords({ id: 'note', role: 'user', content: 'Ask Odile.' }).names // Set { 'odile' }\n * ```\n */\nexport function extractWords(message: Message): LedgerWordSet {\n\treturn {\n\t\twords: new Set(message.content.toLowerCase().match(LEDGER_WORD) ?? []),\n\t\tnames: new Set(\n\t\t\t[...message.content.matchAll(/(?<![\\p{L}\\p{N}'-])\\p{Lu}\\p{Ll}+(?![\\p{L}\\p{N}-])/gu)]\n\t\t\t\t.filter((match) => {\n\t\t\t\t\tconst before = message.content.slice(0, match.index).trimEnd()\n\t\t\t\t\treturn before !== '' && !/[.!?:;]$/.test(before)\n\t\t\t\t})\n\t\t\t\t.map((match) => match[0].toLowerCase()),\n\t\t),\n\t}\n}\n\n/**\n * Splits a topic part into lowercase words with surrounding punctuation removed.\n * @param part - The topic part to split\n * @returns The nonempty normalized words\n * @example\n * ```ts\n * splitWords('Refunds, BW-5512!') // ['refunds', 'bw-5512']\n * ```\n */\nexport function splitWords(part: string): readonly string[] {\n\treturn part\n\t\t.split(/\\s+/)\n\t\t.map((word) => word.replace(/^[\\p{P}\\p{S}]+|[\\p{P}\\p{S}]+$/gu, '').toLowerCase())\n\t\t.filter((word) => word !== '')\n}\n\n/**\n * Finds the call that owns a tool message within a collected tool group.\n * @param group - The assistant leader followed by its tool results\n * @param message - The tool result to pair; distinct leader ids pair by result id when any result has one\n * @returns The call matching the result's call id, or its position when the leader repeats an id or every result lacks an id; undefined when unpaired\n * @example\n * ```ts\n * findLedgerCall(group, result)?.arguments\n * ```\n */\nexport function findLedgerCall(group: readonly Message[], message: Message): ToolCall | undefined {\n\tconst [leader, ...results] = group\n\tconst at = results.findIndex((result) => result.id === message.id)\n\tif (message.role !== 'tool' || at < 0) return undefined\n\tconst calls = leader?.calls ?? []\n\tconst duplicate = new Set(calls.map((call) => call.id)).size !== calls.length\n\treturn !duplicate && results.some((result) => result.call !== undefined)\n\t\t? calls.find((call) => call.id === message.call)\n\t\t: calls[at]\n}\n\n/**\n * Ranks a source the records render for removal before the next prompt.\n *\n * @param group - The planning group: 1 for a topic match, 2 for an off-topic rule or correction, or 3 for a name match\n * @param unsettled - If `true`, the source is a user message without a decisive category; if `false`, it is another source\n * @param category - The source's recorded category, or undefined when undecided\n * @returns The ascending removal rank: name matches, unsettled sources, off-topic corrections, off-topic rules, then topic matches\n * @remarks\n * The planner removes lower ranks first. Equal ranks 0, 1, and 4 remove lower scores first;\n * every equal rank then removes later conversation positions first.\n * @example\n * ```ts\n * rankLedgerCut(2, false, 'rule') // 3\n * ```\n */\nexport function rankLedgerCut(\n\tgroup: LedgerPlanGroup,\n\tunsettled: boolean,\n\tcategory: LedgerCategory | undefined,\n): number {\n\treturn group === 3 ? 0 : unsettled ? 1 : group === 2 ? (category === 'rule' ? 3 : 2) : 4\n}\n\n/**\n * Finds the first cut in a range whose probe returns a value, by bisection.\n *\n * @param low - The first cut to try; 0 tries that cut alone before any bisection\n * @param high - The end of the range, which the search never probes\n * @param probe - Reads a cut and returns its value when the cut fits, or undefined when it does not\n * @param found - The value of the earliest fitting cut the search has probed\n * @returns The value of the fitting cut the search lands on, or undefined when none fits\n * @remarks\n * A range of `S` cuts takes at most `ceil(log2(S)) + 1` probes and one probe when cut 0 fits. The\n * cut it lands on is the range's first fitting cut while no cut past a fitting cut stops fitting.\n * @example\n * ```ts\n * findLedgerCut(0, 8, (cut) => (cut >= 3 ? cut : undefined)) // 3\n * ```\n */\nexport function findLedgerCut<T>(\n\tlow: number,\n\thigh: number,\n\tprobe: (cut: number) => T | undefined,\n\tfound?: T,\n): T | undefined {\n\tif (low >= high) return found\n\tif (low === 0) return probe(0) ?? findLedgerCut(1, high, probe)\n\tconst middle = Math.floor((low + high) / 2)\n\tconst value = probe(middle)\n\treturn value === undefined\n\t\t? findLedgerCut(middle + 1, high, probe, found)\n\t\t: findLedgerCut(low, middle, probe, value)\n}\n\n/**\n * Builds the cut the given steps leave: each step drops the line it is, by identity, or the source it names.\n *\n * @param steps - The lines and source ids to drop\n * @param records - The records to drop lines from\n * @param included - The source ids to drop sources from\n * @returns The records without the dropped lines and the sources without the dropped ids\n * @example\n * ```ts\n * buildLedgerCut(['a'], [], new Set(['a', 'b'])).included // Set { 'b' }\n * ```\n */\nexport function buildLedgerCut(\n\tsteps: ReadonlyArray<LedgerLine | string>,\n\trecords: readonly LedgerRecord[],\n\tincluded: ReadonlySet<string>,\n): { readonly records: readonly LedgerRecord[]; readonly included: ReadonlySet<string> } {\n\tconst dropped = new Set(steps)\n\treturn {\n\t\trecords: records.map((record) => ({\n\t\t\t...record,\n\t\t\tlines: record.lines.filter((line) => !dropped.has(line)),\n\t\t})),\n\t\tincluded: new Set([...included].filter((source) => !dropped.has(source))),\n\t}\n}\n\n/**\n * Resolves the generation cap and checks it against the context capacity.\n *\n * @param predict - The optional generation cap in tokens; see {@link LedgerOptions}\n * @param capacity - The context capacity in tokens\n * @returns The validated cap\n * @throws {LedgerError} Thrown when the cap is not a nonnegative safe integer less than capacity (code `'CAPACITY'`)\n * @example\n * ```ts\n * resolvePredict(1024, 4096) // 1024\n * ```\n */\nexport function resolvePredict(predict: number | undefined, capacity: number): number {\n\tconst resolved = predict ?? 0\n\tif (!isNonNegativeSafeInteger(resolved) || resolved >= capacity)\n\t\tthrow new LedgerError(\n\t\t\t'CAPACITY',\n\t\t\t'predict must be a nonnegative safe integer less than capacity',\n\t\t)\n\treturn resolved\n}\n\n/**\n * Computes the completion tokens attributable to a message's thinking.\n *\n * @remarks\n * Weights the completion by thinking characters divided by the combined characters of thinking,\n * content, and JSON-serialized calls. Rounds to the nearest integer and caps at the completion.\n * An empty generation yields 0. The completion must be a finite nonnegative integer token count.\n * Calls that cannot be JSON-serialized contribute 0 characters.\n *\n * @param message - The generated thinking, content, and optional calls\n * @param completion - The tokens reported for the completion\n * @returns The thinking share in tokens, or 0 when no thinking characters exist\n *\n * @example\n * ```ts\n * computeThinking({ thinking: 'plan', content: 'ok' }, 10) // 7\n * ```\n */\nexport function computeThinking(\n\tmessage: Pick<Message, 'thinking' | 'content' | 'calls'>,\n\tcompletion: number,\n): number {\n\tconst thinking = message.thinking?.length ?? 0\n\tif (thinking === 0) return 0\n\tconst calls = message.calls === undefined ? 0 : (renderJSON(message.calls)?.length ?? 0)\n\tconst generated = thinking + message.content.length + calls\n\treturn Math.min(completion, Math.round(completion * (thinking / generated)))\n}\n\n/**\n * Splits a message into its sentences.\n *\n * @remarks\n * A sentence ends at a period, question mark, or exclamation mark followed by a space and a capital\n * letter, a digit, or a quote, so a decimal point, an id, or an amount never splits one.\n *\n * @param text - The message text\n * @returns The trimmed, non-empty sentences in order\n *\n * @example\n * ```ts\n * splitSentences('Refunds over $200 need a manager. Ask Odile.')\n * // ['Refunds over $200 need a manager.', 'Ask Odile.']\n * ```\n */\nexport function splitSentences(text: string): readonly string[] {\n\treturn text\n\t\t.split(/(?<=[.!?])\\s+(?=[\\p{Lu}\\d\"'“])/u)\n\t\t.map((sentence) => sentence.trim())\n\t\t.filter((sentence) => sentence !== '')\n}\n\n/**\n * Collects the capitalized name runs of a text, leaving out the run that opens each sentence.\n *\n * @remarks\n * A sentence opens with a capital whatever its first word is, so its opening run proves nothing.\n * This is the reading the person prefix of {@link buildRecords} rests on.\n *\n * @param text - The text to read\n * @returns The capitalized runs in order, repeats included\n *\n * @example\n * ```ts\n * collectNames('Odile phoned. We asked about Odile Marlow.') // ['Odile Marlow']\n * ```\n */\nexport function collectNames(text: string): readonly string[] {\n\treturn splitSentences(text).flatMap((sentence) =>\n\t\t[...sentence.matchAll(/(?<![\\p{L}\\p{N}'-])\\p{Lu}\\p{Ll}+(?: \\p{Lu}\\p{Ll}+)*(?![\\p{L}\\p{N}-])/gu)]\n\t\t\t.filter((match) => (match.index ?? 0) > 0)\n\t\t\t.map((match) => match[0]),\n\t)\n}\n\n/**\n * Normalizes a lookup argument string to the spelling that lookup identity and owner linking compare.\n *\n * @remarks\n * {@link identifyLookup} and {@link linkOwners} read an argument through this one form, so a finding\n * a later lookup replaces and the owner link of its argument agree on the spelling.\n *\n * @param value - The string argument a lookup call carried\n * @returns The argument trimmed and uppercased\n *\n * @example\n * ```ts\n * normalizeArgument(' bw-5512 ') // 'BW-5512'\n * ```\n */\nexport function normalizeArgument(value: string): string {\n\treturn value.trim().toUpperCase()\n}\n\n/**\n * Splits a tool result into its lines at each line feed, with or without a carriage return before it.\n *\n * @remarks\n * The recall listing keys each recalled line by its first line, and the answer note reads a result\n * line by line, so both read lines through this one split.\n *\n * @param text - The tool result text\n * @returns The lines in order, an empty line included; one empty line for an empty text\n *\n * @example\n * ```ts\n * splitResultLines('Account BW-20931.\\r\\nRefund is $148.50.') // ['Account BW-20931.', 'Refund is $148.50.']\n * ```\n */\nexport function splitResultLines(text: string): readonly string[] {\n\treturn text.split(/\\r\\n|\\n/)\n}\n\n/**\n * Renders a lookup call as its tool name and its JSON arguments.\n *\n * @remarks\n * The arguments keep the text `JSON.stringify` writes, key order included. Arguments JSON cannot\n * carry, such as a `BigInt` or a cycle, render the bare tool name instead of throwing.\n *\n * @param name - The lookup tool name\n * @param args - The arguments the call carried\n * @returns The tool name, a space, and the JSON arguments; the bare tool name when JSON cannot carry them\n *\n * @example\n * ```ts\n * renderLookupCall('lookup_order', { id: 'BW-5512' }) // 'lookup_order {\"id\":\"BW-5512\"}'\n * renderLookupCall('lookup_order', { count: 1n }) // 'lookup_order'\n * ```\n */\nexport function renderLookupCall(name: string, args: Readonly<Record<string, unknown>>): string {\n\tconst rendered = renderJSON(args)\n\treturn rendered === undefined ? name : `${name} ${rendered}`\n}\n\n/**\n * Identifies a lookup finding for projection by its tool name and normalized arguments.\n *\n * @remarks\n * Two calls share an identity whatever the key order of their arguments at any depth. A top-level\n * string argument passes through {@link normalizeArgument} first, so `\"lh-1 \"` and `\"LH-1\"` name one call.\n * The ledger's repeat stop instead compares the tool name and canonical arguments without this\n * string normalization, so those argument spellings remain distinct within a request.\n * An uncanonicalizable call has no identity and neither replaces nor is replaced by another finding.\n *\n * @param name - The tool name\n * @param args - The arguments the call carried\n * @returns The name, a space, and the canonical arguments, or `undefined` when uncanonicalizable\n *\n * @example\n * ```ts\n * identifyLookup('lookup_order', { id: 'bw-5512', opts: { tier: 2, region: 'north' } }) ===\n * \tidentifyLookup('lookup_order', { opts: { region: 'north', tier: 2 }, id: ' BW-5512' }) // true\n * ```\n */\nexport function identifyLookup(\n\tname: string,\n\targs: Readonly<Record<string, unknown>>,\n): string | undefined {\n\tconst normalized = Object.fromEntries(\n\t\tObject.entries(args).map(([key, value]) => [\n\t\t\tkey,\n\t\t\tisString(value) ? normalizeArgument(value) : value,\n\t\t]),\n\t)\n\tconst canonical = canonicalStringify(normalized)\n\treturn canonical === undefined ? undefined : `${name} ${canonical}`\n}\n\n/**\n * Links each id-shaped lookup argument to its owner.\n *\n * @remarks\n * An owner argument links to itself. Any other id-shaped argument links to the single owner id its\n * finding's text names. An empty finding links nothing, and a later finding overwrites an earlier\n * link of the same argument.\n *\n * @param findings - The lookup findings in conversation order\n * @param owners - The owners keyed by id\n * @returns The owner id of each linked argument id; an argument whose text names no owner or several is absent\n *\n * @example\n * ```ts\n * linkOwners(\n * \t[{ id: 'tool-1', name: 'lookup_order', arguments: { id: 'BW-5512' }, text: 'Order BW-5512 for account BW-20931.', result: { ids: [], owners: [] } }],\n * \tnew Map([['BW-20931', ['Brightwater Studio']]]),\n * ) // Map { 'BW-5512' => 'BW-20931' }\n * ```\n */\nexport function linkOwners(\n\tfindings: readonly LedgerLookupFinding[],\n\towners: ReadonlyMap<string, readonly string[]>,\n): ReadonlyMap<string, string> {\n\t// A later pair of the same argument overwrites an earlier one, as the `Map` constructor does. The\n\t// spread visits a hole in `findings` as an undefined finding, as iteration does; `flatMap` skips it.\n\treturn new Map(\n\t\t[...findings].flatMap((finding) => {\n\t\t\tif (finding.result === undefined) return []\n\t\t\tconst named = [...extractLiterals(finding.text).ids].filter((id) => owners.has(id))\n\t\t\tconst [only] = named\n\t\t\treturn Object.values(finding.arguments)\n\t\t\t\t.filter(isString)\n\t\t\t\t.map((value) => normalizeArgument(value))\n\t\t\t\t.filter((subject) => extractLiterals(subject).ids.has(subject))\n\t\t\t\t.flatMap((subject) => {\n\t\t\t\t\tif (owners.has(subject)) return [[subject, subject] as const]\n\t\t\t\t\treturn named.length === 1 && only !== undefined ? [[subject, only] as const] : []\n\t\t\t\t})\n\t\t}),\n\t)\n}\n\n/**\n * Collects the ids and the owner names the lookup findings named.\n *\n * @remarks\n * Every finding counts, a replaced one included, because an id a lookup named stays registered.\n * An empty finding names nothing. An owner name is trimmed, and a name without a letter is dropped.\n *\n * @param findings - The lookup findings in conversation order\n * @returns The registry: every id named, and each owner's names in the order read\n *\n * @example\n * ```ts\n * const registry = collectRegistry([\n * \t{ id: 'tool-1', name: 'lookup_customer', arguments: { account: 'BW-20931' }, text: 'Account BW-20931: Brightwater Studio', result: { ids: ['BW-20931'], owners: [{ id: 'BW-20931', names: ['Brightwater Studio'] }] } },\n * ])\n * // registry.owners is Map { 'BW-20931' => ['Brightwater Studio'] }\n * ```\n */\nexport function collectRegistry(findings: readonly LedgerLookupFinding[]): LedgerRegistry {\n\t// The spread and `Array.from` visit a hole in `findings` or in an owner list as an undefined\n\t// entry, as iteration does; `flatMap` and `map` skip it.\n\tconst results = [...findings].flatMap((finding) =>\n\t\tfinding.result === undefined ? [] : [finding.result],\n\t)\n\treturn {\n\t\tids: new Set(\n\t\t\tresults.flatMap((result) => [\n\t\t\t\t...result.ids,\n\t\t\t\t...Array.from(result.owners, (owner) => owner.id),\n\t\t\t]),\n\t\t),\n\t\towners: new Map(\n\t\t\t[\n\t\t\t\t...Map.groupBy(\n\t\t\t\t\tresults.flatMap((result) => Array.from(result.owners)),\n\t\t\t\t\t(owner) => owner.id,\n\t\t\t\t),\n\t\t\t].map(([id, listed]) => [\n\t\t\t\tid,\n\t\t\t\t[\n\t\t\t\t\t...new Set(\n\t\t\t\t\t\tlisted.flatMap((owner) =>\n\t\t\t\t\t\t\towner.names.map((name) => name.trim()).filter((name) => /\\p{L}/u.test(name)),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t],\n\t\t\t]),\n\t\t),\n\t}\n}\n\n/**\n * Normalizes a run of letters and digits to the one form each of its cases folds to.\n *\n * @remarks\n * The word is one run as {@link LEDGER_WORD} matches it, not a `carriers` word split on whitespace.\n * The lowercase comes first and last so a capital sharp s, a long s, and a Greek final sigma each\n * reach the form of their plain letters.\n *\n * @param word - The run to normalize\n * @returns The run's one form\n *\n * @example\n * ```ts\n * normalizeWord('ΟΔΟΣ') // 'οδος'\n * normalizeWord('ſtudio') // 'studio'\n * ```\n */\nexport function normalizeWord(word: string): string {\n\treturn word.toLowerCase().toUpperCase().toLowerCase()\n}\n\n/**\n * Builds a registry's matching index: each name's owners, each word's carriers, the needles, keys, and ranks.\n *\n * @remarks\n * The index holds the registry's own `ids` set. A needle escapes its text's pattern characters and\n * matches at word edges: a whole name in any case, and a capitalized name word that only one name\n * carries in its own case, so a common word that spells a first name does not. `keys` enters each\n * name under its least-carried run of {@link LEDGER_WORD}, a tie going to its first run, and under\n * each word its word needles hold. `ranks` holds each id's and each name's position, so\n * {@link matchEntities} returns registry order whatever order a text names them in.\n *\n * @param registry - The registry to index\n * @returns The index, which {@link matchEntities} reads without building anything\n * @throws {TypeError} Thrown when an owner's name list holds a hole\n *\n * @example\n * ```ts\n * const index = buildLedgerIndex({ ids: new Set(['BW-20931']), owners: new Map([['BW-20931', ['Brightwater Studio']]]) })\n * // index.aliases is Map { 'Brightwater Studio' => ['BW-20931'] }\n * ```\n */\nexport function buildLedgerIndex(registry: LedgerRegistry): LedgerIndex {\n\t// `Array.from` visits a hole in an owner's names as an undefined name, as iteration does; `map` skips it.\n\tconst aliases = new Map(\n\t\t[\n\t\t\t...Map.groupBy(\n\t\t\t\t[...registry.owners].flatMap(([id, names]) => Array.from(names, (name) => ({ name, id }))),\n\t\t\t\t(entry) => entry.name,\n\t\t\t),\n\t\t].map(([name, entries]) => [name, entries.map((entry) => entry.id)]),\n\t)\n\t// A word's carriers are the distinct names it appears in.\n\tconst carriers = new Map(\n\t\t[\n\t\t\t...Map.groupBy(\n\t\t\t\t[...aliases.keys()].flatMap((name) =>\n\t\t\t\t\t[...new Set(name.split(/\\s+/))].map((word) => ({ word, name })),\n\t\t\t\t),\n\t\t\t\t(entry) => entry.word,\n\t\t\t),\n\t\t].map(([word, entries]) => [word, entries.map((entry) => entry.name)]),\n\t)\n\t// Neither flag set carries `g` or `y`, so a shared needle keeps no `lastIndex` between tests.\n\tconst needles = [...aliases.keys()].flatMap((name) =>\n\t\t[\n\t\t\t{ name, needle: name, whole: true },\n\t\t\t...name\n\t\t\t\t.split(/\\s+/)\n\t\t\t\t.filter((word) => /^\\p{Lu}\\p{L}+$/u.test(word) && carriers.get(word)?.length === 1)\n\t\t\t\t.map((word) => ({ name, needle: word, whole: false })),\n\t\t].map((entry) => ({\n\t\t\t...entry,\n\t\t\tpattern: new RegExp(\n\t\t\t\t`(?<![\\\\p{L}\\\\p{N}])${entry.needle.replace(/[.*+?^$()|{}[\\]\\\\]/g, '\\\\$&')}(?![\\\\p{L}\\\\p{N}])`,\n\t\t\t\tentry.whole ? 'iu' : 'u',\n\t\t\t),\n\t\t})),\n\t)\n\tconst unique = Map.groupBy(\n\t\tneedles.filter((entry) => !entry.whole),\n\t\t(entry) => entry.name,\n\t)\n\tconst runs = new Map(\n\t\t[...aliases.keys()].map((name) => [\n\t\t\tname,\n\t\t\t[...new Set((name.match(LEDGER_WORD) ?? []).map((run) => normalizeWord(run)))],\n\t\t]),\n\t)\n\tconst counts = new Map(\n\t\t[...Map.groupBy([...runs.values()].flat(), (run) => run)].map(([run, names]) => [\n\t\t\trun,\n\t\t\tnames.length,\n\t\t]),\n\t)\n\t// A stable sort gives a tie to the run that comes first in the name.\n\tconst keyed = [...runs].flatMap(([name, held]) => {\n\t\tconst anchor = held\n\t\t\t.toSorted((left, right) => (counts.get(left) ?? 0) - (counts.get(right) ?? 0))\n\t\t\t.slice(0, 1)\n\t\tconst words = (unique.get(name) ?? []).map((entry) => normalizeWord(entry.needle))\n\t\treturn [...new Set([...anchor, ...words])].map((key) => ({ key, name }))\n\t})\n\treturn {\n\t\tids: registry.ids,\n\t\taliases,\n\t\tcarriers,\n\t\tnames: new Map(\n\t\t\tneedles.filter((entry) => entry.whole).map((entry) => [entry.name, entry.pattern]),\n\t\t),\n\t\twords: new Map(\n\t\t\t[...unique].map(([name, entries]) => [name, entries.map((entry) => entry.pattern)]),\n\t\t),\n\t\tkeys: new Map(\n\t\t\t[...Map.groupBy(keyed, (entry) => entry.key)].map(([key, entries]) => [\n\t\t\t\tkey,\n\t\t\t\tentries.map((entry) => entry.name),\n\t\t\t]),\n\t\t),\n\t\tranks: {\n\t\t\tids: new Map([...registry.ids].map((id, at) => [id, at])),\n\t\t\tnames: new Map([...aliases.keys()].map((name, at) => [name, at])),\n\t\t},\n\t}\n}\n\n/**\n * Matches the registry ids and owners a text names.\n *\n * @remarks\n * An id counts when the text holds it as an id token. An owner counts when the text holds one of\n * its names whole. With `partial`, a capitalized name word that only one name carries also names\n * that name's owners, matched case-sensitively so a common word that spells a first name does not.\n * The text's words select candidates through `keys`, and the needles confirm each candidate. A\n * pure-ASCII text takes one lowercase, which equals {@link normalizeWord} on ASCII. Any other text\n * folds run by run, because folding it whole would read a final sigma across a run's edge.\n *\n * The lookup carries two limits. U+0345 folds to iota under the needles' `iu` flags but is no\n * letter, so a text or a name using it in iota's place can pass the needle and miss the key. A name\n * holding no letter or digit run has no key and never matches; {@link collectRegistry} keeps no\n * such name, so only a hand-built registry reaches that limit. The fold was proven on Node 22's\n * Unicode data; a browser's tables are unread.\n *\n * @param index - The registry's index, from {@link buildLedgerIndex}\n * @param text - The text to read\n * @param partial - If `true`, a name word only one name carries also names its owners; if `false`, only a whole name does\n * @returns The registry ids and owner ids the text names\n *\n * @example\n * ```ts\n * const index = buildLedgerIndex({ ids: new Set(['BW-20931']), owners: new Map([['BW-20931', ['Brightwater Studio']]]) })\n * matchEntities(index, 'Ask Brightwater about it', true) // Set { 'BW-20931' }\n * matchEntities(index, 'Ask Brightwater about it', false) // Set {}\n * ```\n */\nexport function matchEntities(\n\tindex: LedgerIndex,\n\ttext: string,\n\tpartial: boolean,\n): ReadonlySet<string> {\n\tconst ids = [...extractLiterals(text).ids]\n\t\t.filter((id) => index.ids.has(id))\n\t\t.toSorted((left, right) => (index.ranks.ids.get(left) ?? 0) - (index.ranks.ids.get(right) ?? 0))\n\tif (index.keys.size === 0) return new Set(ids)\n\tconst runs: readonly string[] = /\\P{ASCII}/u.test(text)\n\t\t? (text.match(LEDGER_WORD) ?? []).map((run) => normalizeWord(run))\n\t\t: (text.toLowerCase().match(LEDGER_WORD) ?? [])\n\tconst names = [...new Set(runs.flatMap((run) => index.keys.get(run) ?? []))]\n\t\t.filter(\n\t\t\t(name) =>\n\t\t\t\tindex.names.get(name)?.test(text) === true ||\n\t\t\t\t(partial && (index.words.get(name) ?? []).some((needle) => needle.test(text))),\n\t\t)\n\t\t.toSorted(\n\t\t\t(left, right) => (index.ranks.names.get(left) ?? 0) - (index.ranks.names.get(right) ?? 0),\n\t\t)\n\treturn new Set([...ids, ...names.flatMap((name) => index.aliases.get(name) ?? [])])\n}\n\n/**\n * Projects the owner records and the rules record from a conversation's messages.\n *\n * @remarks\n * Each line is a sentence of a live message, verbatim except that a sentence that opens with a pronoun opens with its party and a colon. A message is live when it is a user message,\n * or a tool message whose finding is the last finding of its call, and the input neither excludes it nor\n * files it quiet or replaced. A finding with an undefined `result` still replaces the earlier\n * finding of the same call, so the earlier result leaves every record.\n *\n * A live message joins the owner records its entities name, directly or through a linked lookup\n * argument. A message that names no owner joins where its earlier side of an amendment pair joins,\n * or on the rules record when it is filed as a rule or a correction, and is an orphan otherwise.\n *\n * Every live sentence is retained. Accepted direct edges are listed in `amendments`. An amending message\n * that is itself superseded retains its accepted edge without restoring its text.\n *\n * A sentence that opens with a pronoun takes the party named in the sentence before it. The reading\n * trusts capitals, so a capitalized word that is no person, such as a carrier named mid-sentence,\n * is read as the party. This is a documented limit: the prefix carries\n * the follow-up facts the records exist for.\n *\n * @param input - The messages, findings, entities, and classification to project\n * @returns The records with owner records ordered by their first member and the rules record last, the accepted amendments, and the live message ids no record placed\n *\n * @example\n * ```ts\n * const projection = buildRecords({\n * \tsystem: 'You staff the desk.',\n * \texclusions: [],\n * \towners: new Map([['BW-20931', ['Brightwater Studio']]]),\n * \tmessages: [{ id: 'user-1', role: 'user', content: 'Brightwater Studio asked for a refund.' }],\n * \tfindings: [],\n * \tentities: new Map([['user-1', ['BW-20931']]]),\n * \tclassification: { quiet: new Set(), categories: new Map(), topics: new Map(), amendments: new Map(), supersessions: new Map() },\n * })\n * // projection.records[0].title === 'Brightwater Studio (BW-20931)'\n * ```\n */\nexport function buildRecords(input: LedgerProjectionInput): LedgerProjection {\n\tconst position = new Map(input.messages.map((message, at) => [message.id, at]))\n\tconst byId = new Map(input.messages.map((message) => [message.id, message]))\n\tconst live = collectLive(input)\n\tconst links = linkOwners(input.findings, input.owners)\n\tconst amended = new Map(\n\t\t[\n\t\t\t...Map.groupBy(\n\t\t\t\t[...input.classification.amendments].flatMap(([earlier, laters]) =>\n\t\t\t\t\tlaters.map((later) => ({ earlier, later })),\n\t\t\t\t),\n\t\t\t\t(pair) => pair.later,\n\t\t\t),\n\t\t].map(([later, pairs]) => [later, pairs.map((pair) => pair.earlier)]),\n\t)\n\tconst amendments = collectAmendments(input, byId, live)\n\tconst holders = [...input.owners.values()].flat()\n\tconst system = collectNames(input.system)\n\t// The memo belongs to this projection alone: `placeMember` inserts each placement once.\n\tconst settled = new Map<string, ReadonlySet<string>>()\n\tconst placements = live.map((id) => ({\n\t\tid,\n\t\tkeys: placeMember(input, links, amended, id, new Set(), settled),\n\t}))\n\tconst orphans = placements.filter((placement) => placement.keys.size === 0).map(({ id }) => id)\n\tconst members = Map.groupBy(\n\t\tplacements.flatMap(({ id, keys }) => [...keys].map((key) => ({ key, id }))),\n\t\t(entry) => entry.key,\n\t)\n\tconst records = [...members]\n\t\t.map(([key, entries]) => {\n\t\t\tconst ids = entries.map((entry) => entry.id)\n\t\t\tconst lines = ids.flatMap((id) => buildLines(input, byId, id, holders, system))\n\t\t\tconst owner = key.slice(LEDGER_OWNER_PREFIX.length)\n\t\t\tconst holder = input.owners.get(owner)?.[0]\n\t\t\tconst title =\n\t\t\t\tkey === LEDGER_RULES_KEY ? 'Rules' : holder === undefined ? owner : `${holder} (${owner})`\n\t\t\tconst first = position.get(ids[0] ?? '') ?? 0\n\t\t\treturn {\n\t\t\t\tfirst,\n\t\t\t\trecord: {\n\t\t\t\t\tkey,\n\t\t\t\t\ttitle,\n\t\t\t\t\tmembers: ids,\n\t\t\t\t\tlines,\n\t\t\t\t} satisfies LedgerRecord,\n\t\t\t}\n\t\t})\n\t\t.sort(\n\t\t\t(left, right) =>\n\t\t\t\tNumber(left.record.key === LEDGER_RULES_KEY) -\n\t\t\t\t\tNumber(right.record.key === LEDGER_RULES_KEY) ||\n\t\t\t\tleft.first - right.first ||\n\t\t\t\t(left.record.key < right.record.key ? -1 : left.record.key > right.record.key ? 1 : 0),\n\t\t)\n\t\t.map(({ record }) => record)\n\treturn { records, amendments, orphans }\n}\n\n/**\n * Selects a request's records, collapsing owners with equal ordered member ids under titles joined with `; `.\n *\n * @remarks\n * The view holds the owner records the request names in the order it names them, then the rules\n * record with the lines whose topics meet the request's first, each group in position order. The\n * returned records and lines are copies. Owner records with the same member ids in the same order\n * collapse into the first requested record, keeping its key, members, and lines and joining their\n * titles with `; ` before trimming.\n *\n * @param projection - The output of {@link buildRecords}\n * @param request - The owners and the desk topics the request names\n * @returns The selected records; an owner with no record yields none\n *\n * @example\n * ```ts\n * selectRecords(projection, { owners: ['BW-20931'], topics: ['refunds'] }).map((record) => record.key)\n * // ['owner:BW-20931', 'rules']\n * ```\n */\nexport function selectRecords(\n\tprojection: LedgerProjection,\n\trequest: LedgerProjectionRequest,\n): readonly LedgerRecord[] {\n\tconst topics = new Set(request.topics)\n\tconst rules = projection.records.find((one) => one.key === LEDGER_RULES_KEY)\n\t// Lines that meet a request topic lead; each side keeps its position order. `groupBy` visits a\n\t// hole in `lines` as an undefined line, as iteration does; `filter` skips it.\n\tconst sides = Map.groupBy(\n\t\t(rules?.lines ?? []).filter(() => true),\n\t\t(line) => line.topics.some((topic) => topics.has(topic)),\n\t)\n\tconst owners = [...new Set(request.owners)].flatMap((owner) => {\n\t\tconst record = projection.records.find((one) => one.key === `${LEDGER_OWNER_PREFIX}${owner}`)\n\t\treturn record === undefined ? [] : [record]\n\t})\n\tconst groups = Map.groupBy(owners, (record) => JSON.stringify(record.members))\n\treturn [\n\t\t...[...groups.values()].flatMap(([record, ...rest]) =>\n\t\t\trecord === undefined\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\tstructuredClone({\n\t\t\t\t\t\t\t...record,\n\t\t\t\t\t\t\ttitle: [record.title, ...rest.map((one) => one.title)].join('; '),\n\t\t\t\t\t\t}),\n\t\t\t\t\t],\n\t\t),\n\t\t...(rules === undefined\n\t\t\t? []\n\t\t\t: [\n\t\t\t\t\tstructuredClone({\n\t\t\t\t\t\t...rules,\n\t\t\t\t\t\tlines: [...(sides.get(true) ?? []), ...(sides.get(false) ?? [])],\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t]\n}\n\n/**\n * Renders one record as a heading and one list item per line.\n *\n * @remarks\n * The ledger joins rendered records with one blank line.\n *\n * @param record - A record or view with a `title` and `lines`\n * @param context - Rendering facts for optional accepted amendment annotations\n * @returns `## TITLE` followed by `- LINE` for each line\n *\n * @example\n * ```ts\n * renderLedgerRecord({ title: 'Rules', lines: [{ text: 'Refunds need a manager.', source: 'user-1', sentence: 0, topics: [], role: 'user' }] })\n * // '## Rules\\n- Refunds need a manager.'\n * ```\n */\nexport function renderLedgerRecord(\n\trecord: Pick<LedgerRecord, 'title' | 'lines'>,\n\tcontext?: LedgerRenderContext,\n): string {\n\treturn [\n\t\t`## ${record.title}`,\n\t\t...record.lines.flatMap((line) =>\n\t\t\t[context === undefined ? '' : renderLedgerAnnotation(line, context), `- ${line.text}`].filter(\n\t\t\t\t(text) => text !== '',\n\t\t\t),\n\t\t),\n\t].join('\\n')\n}\n\n/**\n * Renders one owner record under a `###` heading, which the records nest under their one `## Records` heading.\n *\n * @param record - An owner record or view with a `title` and `lines`\n * @param context - Rendering facts for optional accepted amendment annotations\n * @returns `### TITLE` followed by `- LINE` for each line\n *\n * @example\n * ```ts\n * renderLedgerPinned({ title: 'Odile Marlow (OM-30418)', lines: [] }) // '### Odile Marlow (OM-30418)'\n * ```\n */\nexport function renderLedgerPinned(\n\trecord: Pick<LedgerRecord, 'title' | 'lines'>,\n\tcontext?: LedgerRenderContext,\n): string {\n\treturn `#${renderLedgerRecord(record, context)}`\n}\n\n/**\n * Derives complete source identities, first occurrences, and local references from the actual lines of the records.\n * @param messages - Original messages whose indexed sentences establish completeness\n * @param lines - Retained lines in rendering order after deduplication and trimming\n * @param amendments - Accepted direct message amendment edges\n * @returns Immutable rendering facts for this retained population\n *\n * @example\n * ```ts\n * buildLedgerRenderContext([], [], []).complete.size // 0\n * ```\n */\nexport function buildLedgerRenderContext(\n\tmessages: readonly Message[],\n\tlines: readonly LedgerLine[],\n\tamendments: readonly LedgerAmendment[],\n): LedgerRenderContext {\n\tconst identities = new Set(lines.map((line) => `${line.source} ${line.sentence}`))\n\treturn {\n\t\tamendments,\n\t\tcomplete: new Set(\n\t\t\tmessages\n\t\t\t\t.filter((message) => {\n\t\t\t\t\tconst sentences = splitSentences(message.content)\n\t\t\t\t\treturn (\n\t\t\t\t\t\tsentences.length > 0 &&\n\t\t\t\t\t\tsentences.every((_, index) => identities.has(`${message.id} ${index}`))\n\t\t\t\t\t)\n\t\t\t\t})\n\t\t\t\t.map((message) => message.id),\n\t\t),\n\t\tfirst: new Map(lines.toReversed().map((line) => [line.source, line])),\n\t\treferences: new Map(\n\t\t\t[...new Set(lines.map((line) => line.source))].map((source, index) => [\n\t\t\t\tsource,\n\t\t\t\tString(index + 1),\n\t\t\t]),\n\t\t),\n\t}\n}\n\n/**\n * Renders a source's reference and its later changes at the source's first retained line.\n * @param line - The retained occurrence to annotate\n * @param context - Actual complete sources, first occurrences, local references, and accepted amendment edges\n * @returns The source's reference and its later-change notes, or empty text for an unrelated or repeated occurrence\n * @throws {LedgerError} Thrown when the context holds no reference for the annotated source or a fully included amender (code `'REFERENCE'`)\n *\n * @example\n * ```ts\n * renderLedgerAnnotation({ text: 'Unchanged.', source: 'user-1', sentence: 0, topics: [], role: 'user' }, buildLedgerRenderContext([], [], [])) // ''\n * ```\n */\nexport function renderLedgerAnnotation(line: LedgerLine, context: LedgerRenderContext): string {\n\tif (context.first.get(line.source) !== line) return ''\n\tconst amendments = context.amendments.filter((pair) => pair.earlier === line.source)\n\tconst endpoint =\n\t\tcontext.complete.has(line.source) &&\n\t\tcontext.amendments.some((pair) => pair.later === line.source && context.first.has(pair.earlier))\n\tif (amendments.length === 0 && !endpoint) return ''\n\tconst included = amendments.filter((pair) => context.complete.has(pair.later))\n\tif (\n\t\tcontext.references.get(line.source) === undefined ||\n\t\tincluded.some((pair) => context.references.get(pair.later) === undefined)\n\t)\n\t\tthrow new LedgerError('REFERENCE', 'Ledger rendering requires retained source references')\n\treturn [\n\t\t`[${context.references.get(line.source)}]`,\n\t\t...included.map((pair) => ` Changed later by [${context.references.get(pair.later)}].`),\n\t\t...(amendments.some((pair) => !context.complete.has(pair.later))\n\t\t\t? [' A later change is not shown.']\n\t\t\t: []),\n\t].join('')\n}\n\n/**\n * Splits a recall topic at its joints.\n *\n * @remarks\n * The joints are a comma, a semicolon, a slash, and the word `and`. A topic with no joint is\n * returned whole, so a model that joins a name and an id recalls each part alone.\n *\n * @param topic - The topic the model asked for\n * @returns The trimmed, non-empty parts when there are at least 2; otherwise the topic itself\n *\n * @example\n * ```ts\n * splitTopic('BW-5512, Odile Marlow and refunds') // ['BW-5512', 'Odile Marlow', 'refunds']\n * ```\n */\nexport function splitTopic(topic: string): readonly string[] {\n\tconst parts = topic\n\t\t.split(/\\s*[,;/]\\s*|\\s+and\\s+/i)\n\t\t.map((part) => part.trim())\n\t\t.filter((part) => part !== '')\n\treturn parts.length > 1 ? parts : [topic]\n}\n\n/**\n * Cuts entries to a room and names how many it left out.\n *\n * @remarks\n * The cut keeps at least one entry. When it leaves entries out, its last line reads\n * `N older items not shown; name a narrower topic to narrow the recall`, which\n * {@link matchesCutLine} recognizes.\n *\n * @param entries - The entries in the order they are kept\n * @param room - The estimate units the joined entries can take\n * @returns The kept entries followed by the cut line when any were left out, joined by newlines\n *\n * @example\n * ```ts\n * cutListing(['first entry', 'second entry'], 1) // 'first entry\\n1 older item not shown; name a narrower topic to narrow the recall'\n * ```\n */\nexport function cutListing(entries: readonly string[], room: number): string {\n\t// The cut keeps at least one entry, and stops at the first entry whose addition passes the room.\n\tconst stop = [...entries.keys()].findIndex(\n\t\t(at) =>\n\t\t\tat > 0 &&\n\t\t\testimateMessages([\n\t\t\t\t{\n\t\t\t\t\tid: 'recall',\n\t\t\t\t\trole: 'tool',\n\t\t\t\t\tcontent: entries.slice(0, at + 1).join('\\n'),\n\t\t\t\t},\n\t\t\t]) > room,\n\t)\n\tconst kept = entries.slice(0, stop === -1 ? undefined : stop)\n\tconst left = entries.length - kept.length\n\treturn [\n\t\t...kept,\n\t\t...(left > 0\n\t\t\t? [\n\t\t\t\t\t`${left} older item${left === 1 ? '' : 's'} not shown; name a narrower topic to narrow the recall`,\n\t\t\t\t]\n\t\t\t: []),\n\t].join('\\n')\n}\n\n/**\n * Checks whether a line is the cut line {@link cutListing} writes.\n *\n * @param line - The line to check\n * @returns True if the line is a cut line; false otherwise\n *\n * @example\n * ```ts\n * matchesCutLine('2 older items not shown; name a narrower topic to narrow the recall') // true\n * ```\n */\nexport function matchesCutLine(line: string): boolean {\n\treturn /^\\d+ older items? not shown; /.test(line)\n}\n\n/**\n * Renders the tail stub of a lookup result from an earlier request.\n *\n * @param name - The lookup tool name\n * @param args - The arguments the call carried; arguments JSON cannot carry leave the call as its bare tool name, per {@link renderLookupCall}\n * @param state - What became of the result: `failed`, `empty`, `shown` in the records, or `hidden` from them\n * @returns The stub text, which carries the call and its state\n *\n * @example\n * ```ts\n * renderStub('lookup_order', { id: 'BW-5512' }, 'hidden')\n * // 'lookup_order {\"id\":\"BW-5512\"}: result not shown; call recall with BW-5512'\n * ```\n */\nexport function renderStub(\n\tname: string,\n\targs: Readonly<Record<string, unknown>>,\n\tstate: LedgerLookupState,\n): string {\n\tconst head = renderLookupCall(name, args)\n\tif (state === 'failed') return `${head}: failed`\n\tif (state === 'empty') return `${head}: no record`\n\tif (state === 'shown') return `${head}: result shown under Records below`\n\tconst id = Object.values(args).find(isString)\n\treturn `${head}: result not shown; call recall with ${(id ?? '').trim() || 'its id'}`\n}\n\n/**\n * Fits the marginal tokens one estimate unit adds within a request.\n *\n * @remarks\n * The fit is the least-squares slope of prompt tokens over estimate, taken within each set of calls\n * that advertised the same number of tools and pooled over every group. The estimate counts no tool\n * schema, so a pooled fit across tool counts would read the dropped schemas as a falling rate.\n *\n * @param groups - The calls of each request\n * @returns The slope, or undefined when no set holds 2 calls with a prompt count and an estimate that differ\n *\n * @example\n * ```ts\n * fitSlope([[{ estimate: 100, prompt: 130, tools: 2 }, { estimate: 200, prompt: 260, tools: 2 }]]) // 1.3\n * ```\n */\nexport function fitSlope(groups: ReadonlyArray<readonly GaugeCall[]>): number | undefined {\n\t// The spread visits a hole in `groups` as an undefined group, as iteration does; `flatMap` skips it.\n\tconst terms = [...groups].flatMap((group) =>\n\t\t[...Map.groupBy(group, (call) => call.tools).values()].flatMap((set) => {\n\t\t\tconst points = set.flatMap((call) =>\n\t\t\t\tisFiniteNumber(call.prompt) && call.estimate > 0\n\t\t\t\t\t? [{ estimate: call.estimate, prompt: call.prompt }]\n\t\t\t\t\t: [],\n\t\t\t)\n\t\t\tif (points.length < 2) return []\n\t\t\tconst meanX = points.reduce((sum, point) => sum + point.estimate, 0) / points.length\n\t\t\tconst meanY = points.reduce((sum, point) => sum + point.prompt, 0) / points.length\n\t\t\treturn points.map((point) => ({\n\t\t\t\tspread: (point.estimate - meanX) ** 2,\n\t\t\t\tproduct: (point.estimate - meanX) * (point.prompt - meanY),\n\t\t\t}))\n\t\t}),\n\t)\n\tconst spread = terms.reduce((sum, term) => sum + term.spread, 0)\n\tconst product = terms.reduce((sum, term) => sum + term.product, 0)\n\treturn spread > 0 ? product / spread : undefined\n}\n\n/**\n * Collects the ids of the live messages in conversation order.\n *\n * @remarks\n * A message is live when it is a user message, or a tool message whose finding is the last finding of its\n * call, and the input neither excludes it nor files it quiet or replaced. A finding replaces any\n * earlier finding of the same call, an empty one included, so an empty lookup leaves the earlier\n * result out of every record. A finding with no canonical identity stays live as its own source\n * and participates in no replacement.\n *\n * @param input - The messages, findings, and classification to read\n * @returns The live message ids\n *\n * @example\n * ```ts\n * collectLive(input) // ['user-1', 'tool-2']\n * ```\n */\nexport function collectLive(input: LedgerProjectionInput): readonly string[] {\n\tconst { classification } = input\n\tconst excluded = new Set(input.exclusions)\n\tconst position = new Map(input.messages.map((message, at) => [message.id, at]))\n\tconst findings = input.findings.filter((finding) => position.has(finding.id))\n\tconst identities = new Map(\n\t\tfindings.map((finding) => [finding.id, identifyLookup(finding.name, finding.arguments)]),\n\t)\n\tconst current = new Set(\n\t\tfindings\n\t\t\t.filter(\n\t\t\t\t(finding) =>\n\t\t\t\t\tfinding.result !== undefined &&\n\t\t\t\t\t!findings.some(\n\t\t\t\t\t\t(later) =>\n\t\t\t\t\t\t\tidentities.get(finding.id) !== undefined &&\n\t\t\t\t\t\t\t(position.get(later.id) ?? -1) > (position.get(finding.id) ?? -1) &&\n\t\t\t\t\t\t\tidentities.get(later.id) === identities.get(finding.id),\n\t\t\t\t\t),\n\t\t\t)\n\t\t\t.map((finding) => finding.id),\n\t)\n\treturn [...new Map(input.messages.map((message) => [message.id, message])).values()]\n\t\t.filter(\n\t\t\t(message) => message.role === 'user' || (message.role === 'tool' && current.has(message.id)),\n\t\t)\n\t\t.map((message) => message.id)\n\t\t.filter(\n\t\t\t(id) =>\n\t\t\t\t!excluded.has(id) &&\n\t\t\t\t!classification.quiet.has(id) &&\n\t\t\t\t(classification.supersessions.get(id) ?? []).length === 0,\n\t\t)\n}\n\n/**\n * Places a message on the record keys it joins.\n *\n * @remarks\n * A message joins the owner records its entities name, directly or through a linked lookup argument.\n * A message that names no owner joins where the earlier side of its amendment pair joins, and a\n * message with no earlier side joins the rules record when it is filed as a rule or a correction.\n * A message that is not live still places, because a correction joins where its earlier side would\n * join.\n *\n * With `settled`, the walk returns a placement the map holds without walking again, and records each\n * placement whose walk, its nested walks included, excluded no earlier side through `seen`. Such a\n * walk reaches no cycle. While every `seen` holds only the path that reached the message, as\n * {@link buildRecords} passes it, that path never holds a message the recorded walk reached, so a\n * fresh walk returns the same keys in the same order. A placement a cycle cut stays unrecorded,\n * because its keys depend on the path that reached it. Sharing one map across the members of one\n * input bounds a dense amendment graph to one walk per message.\n *\n * @param input - The owners, entities, and classification to read\n * @param links - The owner of each linked lookup argument, from {@link linkOwners}\n * @param amended - The earlier sides of each message's amendment pairs\n * @param id - The message id\n * @param seen - The ids already visited, which stops a cycle of amendment pairs\n * @param settled - The placements already walked for the same `input`, `links`, and `amended`; the walk adds to it. Default: no map, so every call walks\n * @returns The record keys, empty when the message is an orphan\n *\n * @example\n * ```ts\n * placeMember(input, new Map(), new Map(), 'user-1', new Set()) // Set { 'owner:BW-20931' }\n * ```\n */\nexport function placeMember(\n\tinput: LedgerProjectionInput,\n\tlinks: ReadonlyMap<string, string>,\n\tamended: ReadonlyMap<string, readonly string[]>,\n\tid: string,\n\tseen: ReadonlySet<string>,\n\tsettled?: Map<string, ReadonlySet<string>>,\n): ReadonlySet<string> {\n\tconst known = settled?.get(id)\n\tif (known !== undefined) return known\n\tconst owners = (input.entities.get(id) ?? []).flatMap((entity) => {\n\t\tconst owner = input.owners.has(entity) ? entity : links.get(entity)\n\t\treturn owner === undefined ? [] : [`${LEDGER_OWNER_PREFIX}${owner}`]\n\t})\n\tconst owned = owners.length > 0\n\tconst sides = owned ? [] : (amended.get(id) ?? []).filter((side) => side !== id)\n\tconst earlier = sides.filter((side) => !seen.has(side))\n\tconst category = input.classification.categories.get(id)\n\tconst ruled =\n\t\t!owned && earlier.length === 0 && category !== undefined && PLACED_CATEGORIES.includes(category)\n\tconst keys = new Set([\n\t\t...owners,\n\t\t...earlier.flatMap((side) => [\n\t\t\t...placeMember(input, links, amended, side, new Set([...seen, id]), settled),\n\t\t]),\n\t\t...(ruled ? [LEDGER_RULES_KEY] : []),\n\t])\n\tif (\n\t\tsettled !== undefined &&\n\t\tearlier.length === sides.length &&\n\t\tearlier.every((side) => settled.has(side))\n\t)\n\t\tsettled.set(id, keys)\n\treturn keys\n}\n\n/**\n * Collects accepted direct amendments of live messages.\n *\n * @remarks\n * An accepted edge relates an earlier live message to an amender without claiming which sentences\n * changed. An amender takes effect while live and retains its edge after another message supersedes\n * it, without restoring the superseded text. An excluded or quiet user or tool message has no effect.\n *\n * @param input - The exclusions and classification to read\n * @param byId - The messages by id\n * @param live - The live message ids from {@link collectLive}\n * @returns The distinct accepted direct edges in conversation order\n *\n * @example\n * ```ts\n * collectAmendments(input, byId, ['user-1', 'user-3']) // [{ earlier: 'user-1', later: 'user-3' }]\n * ```\n */\nexport function collectAmendments(\n\tinput: LedgerProjectionInput,\n\tbyId: ReadonlyMap<string, Message>,\n\tlive: readonly string[],\n): readonly LedgerAmendment[] {\n\tconst { classification } = input\n\tconst excluded = new Set(input.exclusions)\n\tconst living = new Set(live)\n\tconst ordered = [...new Set(input.messages.map((message) => message.id))]\n\tconst positions = new Map(ordered.map((id, index) => [id, index]))\n\tconst effective = new Set([\n\t\t...live,\n\t\t...[...byId.values()]\n\t\t\t.filter(\n\t\t\t\t(message) =>\n\t\t\t\t\tmessage.role !== 'assistant' &&\n\t\t\t\t\t!excluded.has(message.id) &&\n\t\t\t\t\t!classification.quiet.has(message.id) &&\n\t\t\t\t\t(classification.supersessions.get(message.id) ?? []).length > 0,\n\t\t\t)\n\t\t\t.map((message) => message.id),\n\t])\n\treturn ordered\n\t\t.filter((id) => living.has(id))\n\t\t.flatMap((earlier) =>\n\t\t\t[...new Set(classification.amendments.get(earlier) ?? [])]\n\t\t\t\t.filter((later) => effective.has(later) && positions.has(later))\n\t\t\t\t.sort((a, b) => (positions.get(a) ?? 0) - (positions.get(b) ?? 0))\n\t\t\t\t.map((later) => ({ earlier, later })),\n\t\t)\n}\n\n/**\n * Builds the record lines of one message.\n *\n * @remarks\n * Each original indexed sentence becomes a line. A sentence that opens with a pronoun and follows\n * another sentence takes the last name of the sentence before it that is neither an owner name nor\n * a system name, and its line text opens with that party and a colon.\n *\n * @param input - The classification whose topics the lines carry\n * @param byId - The messages by id\n * @param id - The message id\n * @param holders - The owner names, which never serve as a party\n * @param system - The system text's names, which never serve as a party\n * @returns The lines in sentence order; empty when the message is absent\n *\n * @example\n * ```ts\n * buildLines(input, byId, 'user-1', [], []).map((line) => line.text)\n * // ['Odile Marlow phoned about BW-5512.', 'Odile Marlow: She wants a refund.']\n * ```\n */\nexport function buildLines(\n\tinput: LedgerProjectionInput,\n\tbyId: ReadonlyMap<string, Message>,\n\tid: string,\n\tholders: readonly string[],\n\tsystem: readonly string[],\n): readonly LedgerLine[] {\n\tconst message = byId.get(id)\n\tif (message === undefined) return []\n\tconst sentences = splitSentences(message.content)\n\tconst topics = input.classification.topics.get(id) ?? []\n\treturn sentences.flatMap((sentence, at) => {\n\t\tconst party =\n\t\t\t/^(?:He|She|They|His|Her|Their)(?![\\p{L}\\p{N}])/u.test(sentence) && at > 0\n\t\t\t\t? collectNames(sentences[at - 1] ?? '')\n\t\t\t\t\t\t.filter((name) => !holders.includes(name) && !system.includes(name))\n\t\t\t\t\t\t.at(-1)\n\t\t\t\t: undefined\n\t\tconst line = {\n\t\t\ttext: party === undefined ? sentence : `${party}: ${sentence}`,\n\t\t\tsource: id,\n\t\t\tsentence: at,\n\t\t\ttopics: [...topics],\n\t\t\trole: message.role,\n\t\t}\n\t\treturn [party === undefined ? line : { ...line, party }]\n\t})\n}\n","import type { ChoiceAnswer, Judgment, JudgmentInput, LiteralSet, Message } from '../types.js'\nimport type {\n\tClassifierInterface,\n\tClassifierOptions,\n\tClassifierPhase,\n\tClassifierAttempt,\n\tClassifierResult,\n\tLedgerCategory,\n\tLedgerClassification,\n\tLedgerTopic,\n} from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport { errorToMessage, isArray, isError, isString, parseJSONAs } from '@orkestrel/contract'\nimport { raceSignal } from '../conversations/index.js'\nimport { isJudgeAbortError } from '../errors.js'\nimport { extractLiterals, matchesJudgment, renderJSON, sumUsage } from '../helpers.js'\nimport { isJudgeError } from '../providers/errors.js'\nimport {\n\tDECISIVE_CATEGORIES,\n\tDETERMINISTIC_JUDGE_ERROR,\n\tLEDGER_CATEGORIES,\n\tLEDGER_SLICE,\n\tQUIET_CATEGORIES,\n} from './constants.js'\nimport { renderCauseChain } from './helpers.js'\n\n/**\n * Files messages through the conversation's judgment manager and reads their categories and corrections.\n *\n * @example\n * ```ts\n * const classifier = new Classifier(options)\n * await classifier.classify(new Set(), signal)\n * const classification = classifier.classification()\n * ```\n */\nexport class Classifier implements ClassifierInterface {\n\treadonly #options: ClassifierOptions\n\treadonly #failed = new Set<string>()\n\t#slice = 0\n\n\t/**\n\t * Creates the filing engine with the ledger's handlers and calibrated questions.\n\t * @param options - The conversation, judge, questions, cutoffs, and handlers\n\t */\n\tconstructor(options: ClassifierOptions) {\n\t\tthis.#options = options\n\t}\n\n\t/**\n\t * Asks the judge every question the filing still lacks, in the measured order.\n\t * @param requests - The request ids, which skip category questions and receive only enabled request-topic questions\n\t * @param signal - The caller's signal; an abort returns a fault with completed judgments and usage\n\t * @returns The judgment keys, summed usage, and one attempt per logical question, in asking order; a throw from the assign or entities handler, a scheduler rejection, or a caller abort returns the partial result with `fault`; a judge rejection under a live signal leaves that question undecided and sets `failure`\n\t */\n\tasync classify(requests: ReadonlySet<string>, signal: AbortSignal): Promise<ClassifierResult> {\n\t\tconst results: ClassifierResult[] = []\n\t\tconst fault = await this.#gather(requests, signal, results).then(\n\t\t\t(): Error | undefined => undefined,\n\t\t\t(error: unknown) => (isError(error) ? error : new Error(errorToMessage(error))),\n\t\t)\n\t\tconst usage = results.reduce<TokenUsage | undefined>(\n\t\t\t(total, result) => (result.usage === undefined ? total : sumUsage(total, result.usage)),\n\t\t\tundefined,\n\t\t)\n\t\tconst failure = results.find((result) => result.failure !== undefined)?.failure\n\t\treturn {\n\t\t\tjudgments: results.flatMap((result) => result.judgments),\n\t\t\tattempts: results.flatMap((result) => result.attempts),\n\t\t\t...(fault === undefined ? {} : { fault }),\n\t\t\t...(failure === undefined ? {} : { failure }),\n\t\t\t...(usage === undefined ? {} : { usage }),\n\t\t}\n\t}\n\n\t/**\n\t * Reads the category a message is filed under.\n\t * @param id - The message id\n\t * @returns The assigned category, the recorded category at the cutoff, or `undefined`\n\t */\n\tcategory(id: string): LedgerCategory | undefined {\n\t\tconst message = this.#options.conversation.message(id)\n\t\tif (message === undefined) return undefined\n\t\tconst assigned = this.#options.assign(message)\n\t\tif (assigned !== undefined) return assigned\n\t\tconst answer = this.#readCategories(id)\n\t\tconst cutoff = this.#options.thresholds.category\n\t\tconst candidates = LEDGER_CATEGORIES.flatMap((category) => {\n\t\t\tconst probability = answer?.probabilities[category]\n\t\t\treturn probability !== undefined && probability >= cutoff ? [{ category, probability }] : []\n\t\t})\n\t\t// The first category in `LEDGER_CATEGORIES` order with the highest probability wins a tie.\n\t\treturn candidates.reduce<{ category: LedgerCategory; probability: number } | undefined>(\n\t\t\t(best, candidate) =>\n\t\t\t\tbest === undefined || candidate.probability > best.probability ? candidate : best,\n\t\t\tundefined,\n\t\t)?.category\n\t}\n\n\t/**\n\t * Reports whether a message files under a quiet category.\n\t * @param id - The message id\n\t * @returns True if the assigned or recorded category is quiet; false otherwise.\n\t */\n\tquiet(id: string): boolean {\n\t\tconst message = this.#options.conversation.message(id)\n\t\tif (message === undefined) return false\n\t\tconst assigned = this.#options.assign(message)\n\t\tif (assigned !== undefined) return QUIET_CATEGORIES.includes(assigned)\n\t\treturn this.#weigh(id, QUIET_CATEGORIES) >= this.#options.thresholds.category\n\t}\n\n\t/**\n\t * Reports whether a message files under a decisive category.\n\t * @param id - The message id\n\t * @returns True if the recorded decisive weight reaches the category cutoff; false otherwise.\n\t */\n\tdecisive(id: string): boolean {\n\t\treturn this.#weigh(id, DECISIVE_CATEGORIES) >= this.#options.thresholds.category\n\t}\n\n\t/**\n\t * Reads the topics a message concerns.\n\t * @param id - The message id\n\t * @returns The names of the topics whose recorded weight reaches the topic cutoff\n\t */\n\ttopics(id: string): ReadonlySet<string> {\n\t\tconst message = this.#options.conversation.message(id)\n\t\t// The spread visits a hole in the topic list as an undefined topic, as iteration does; `filter` skips it.\n\t\treturn new Set(\n\t\t\tmessage === undefined\n\t\t\t\t? []\n\t\t\t\t: [...this.#options.topics]\n\t\t\t\t\t\t.filter(\n\t\t\t\t\t\t\t(topic) =>\n\t\t\t\t\t\t\t\t(this.#readNoul(this.#buildTopic(id, topic)) ?? 0) >=\n\t\t\t\t\t\t\t\tthis.#options.thresholds.topic,\n\t\t\t\t\t\t)\n\t\t\t\t\t\t.map((topic) => topic.name),\n\t\t)\n\t}\n\n\t/**\n\t * Reads the whole filing from the recorded judgments.\n\t * @returns The quiet ids, categories, topics, and the amendment and supersession marks\n\t */\n\tclassification(): LedgerClassification {\n\t\tconst messages = this.#options.conversation.messages()\n\t\tconst positions = new Map(messages.map((message, at) => [message.id, at]))\n\t\t// Each message files in HEAD's call order: quiet, then category, then topics.\n\t\tconst filed = messages.map((message) => ({\n\t\t\tid: message.id,\n\t\t\tquiet: this.quiet(message.id),\n\t\t\tcategory: this.category(message.id),\n\t\t\ttopics: [...this.topics(message.id)],\n\t\t}))\n\t\tconst pairs = this.#options.conversation.judgments.judgments().flatMap((judgment) => {\n\t\t\tconst key = parseJSONAs(judgment.id, isArray)\n\t\t\tif (key === undefined) return []\n\t\t\tconst [head, earlier, later] = key\n\t\t\tif ((head !== 'amends' && head !== 'supersedes') || !isString(earlier) || !isString(later))\n\t\t\t\treturn []\n\t\t\tconst before = this.#options.conversation.message(earlier)\n\t\t\tconst after = this.#options.conversation.message(later)\n\t\t\tif (\n\t\t\t\tbefore === undefined ||\n\t\t\t\tafter === undefined ||\n\t\t\t\t(this.#readNoul(this.#buildPair(head, earlier, later)) ?? 0) <\n\t\t\t\t\tthis.#options.thresholds[head]\n\t\t\t)\n\t\t\t\treturn []\n\t\t\treturn head === 'amends' &&\n\t\t\t\t!this.#sharesLiteral(extractLiterals(before.content), extractLiterals(after.content))\n\t\t\t\t? []\n\t\t\t\t: [{ head, earlier, later }]\n\t\t})\n\t\treturn {\n\t\t\tquiet: new Set(filed.filter((one) => one.quiet).map((one) => one.id)),\n\t\t\tcategories: new Map(\n\t\t\t\tfiled.flatMap((one) =>\n\t\t\t\t\tone.category === undefined ? [] : [[one.id, one.category] as const],\n\t\t\t\t),\n\t\t\t),\n\t\t\ttopics: new Map(filed.map((one) => [one.id, one.topics])),\n\t\t\tamendments: this.#sortLaters(pairs, positions),\n\t\t\tsupersessions: this.#sortLaters(\n\t\t\t\tpairs.filter((pair) => pair.head === 'supersedes'),\n\t\t\t\tpositions,\n\t\t\t),\n\t\t}\n\t}\n\n\t// Groups the later ids under each earlier id in first-seen order, each without repeats and in\n\t// conversation order; a stable sort keeps the first-seen order of ids at one position.\n\t#sortLaters(\n\t\tpairs: ReadonlyArray<{ readonly earlier: string; readonly later: string }>,\n\t\tpositions: ReadonlyMap<string, number>,\n\t): ReadonlyMap<string, readonly string[]> {\n\t\treturn new Map(\n\t\t\t[...Map.groupBy(pairs, (pair) => pair.earlier)].map(([earlier, grouped]) => [\n\t\t\t\tearlier,\n\t\t\t\t[...new Set(grouped.map((pair) => pair.later))].sort(\n\t\t\t\t\t(left, right) => (positions.get(left) ?? 0) - (positions.get(right) ?? 0),\n\t\t\t\t),\n\t\t\t]),\n\t\t)\n\t}\n\n\t// Asks every question in order, appending each answer to `results` so a throw still leaves the\n\t// answers completed before it.\n\tasync #gather(\n\t\trequests: ReadonlySet<string>,\n\t\tsignal: AbortSignal,\n\t\tresults: ClassifierResult[],\n\t): Promise<void> {\n\t\tconst messages = this.#options.conversation.messages()\n\t\tconst asked = messages.filter((message) => this.#options.assign(message) === undefined)\n\t\tfor (const message of asked) {\n\t\t\tif (requests.has(message.id)) continue\n\t\t\tif (this.#spent()) await this.#turn(signal)\n\t\t\tresults.push(await this.#ask(this.#buildCategory(message.id), 'category', signal, results))\n\t\t}\n\t\tconst quiets = new Map<string, boolean>()\n\t\tfor (const message of asked) {\n\t\t\tif (!requests.has(message.id) && this.#readQuiet(message.id, quiets)) continue\n\t\t\tfor (const topic of this.#options.topics) {\n\t\t\t\tif (requests.has(message.id) && topic.requested === false) continue\n\t\t\t\tif (this.#spent()) await this.#turn(signal)\n\t\t\t\tresults.push(await this.#ask(this.#buildTopic(message.id, topic), 'topic', signal, results))\n\t\t\t}\n\t\t}\n\t\tconst nears = new Map<string, ReadonlySet<string>>()\n\t\tconst entities = new Map<string, ReadonlySet<string>>()\n\t\tconst literals = new Map<string, LiteralSet>()\n\t\tfor (const later of asked) {\n\t\t\tif (\n\t\t\t\tlater.role !== 'user' ||\n\t\t\t\t(this.#readCategories(later.id)?.probabilities.correction ?? 0) <\n\t\t\t\t\tthis.#options.thresholds.correction\n\t\t\t)\n\t\t\t\tcontinue\n\t\t\tconst near = this.#readNear(later, nears, entities)\n\t\t\tif (near.size === 0) continue\n\t\t\tfor (const earlier of messages) {\n\t\t\t\tif (earlier.id === later.id) break\n\t\t\t\tif (this.#spent()) await this.#turn(signal)\n\t\t\t\tif (!this.#eligible(earlier, later, near, quiets, nears, entities, literals)) continue\n\t\t\t\tconst spec = this.#buildPair('amends', earlier.id, later.id)\n\t\t\t\tresults.push(await this.#ask(spec, 'amends', signal, results))\n\t\t\t\tif ((this.#readNoul(spec) ?? 0) >= this.#options.thresholds.amends)\n\t\t\t\t\tresults.push(\n\t\t\t\t\t\tawait this.#ask(\n\t\t\t\t\t\t\tthis.#buildPair('supersedes', earlier.id, later.id),\n\t\t\t\t\t\t\t'supersedes',\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tresults,\n\t\t\t\t\t\t),\n\t\t\t\t\t)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Reports whether the slice since the last host turn has run its length; with no scheduler no clock is read.\n\t#spent(): boolean {\n\t\treturn this.#options.scheduler !== undefined && performance.now() - this.#slice >= LEDGER_SLICE\n\t}\n\n\t// Hands the host a turn, raced against the signal so an abort ends it, and opens the next slice.\n\tasync #turn(signal: AbortSignal): Promise<void> {\n\t\tconst scheduler = this.#options.scheduler\n\t\tif (scheduler === undefined) return\n\t\tawait raceSignal(scheduler.yield({ signal }), signal)\n\t\tthis.#slice = performance.now()\n\t}\n\n\t#renderState(id: string): string {\n\t\tconst message = this.#options.conversation.message(id)\n\t\treturn `${message?.role ?? 'unknown'}: ${message?.content ?? ''}`\n\t}\n\n\t#buildCategory(id: string): JudgmentInput {\n\t\treturn {\n\t\t\tid: JSON.stringify(['category', id]),\n\t\t\tquestion: {\n\t\t\t\tform: 'choice',\n\t\t\t\t...(this.#options.questions.category.instructions === undefined\n\t\t\t\t\t? {}\n\t\t\t\t\t: { instructions: this.#options.questions.category.instructions }),\n\t\t\t\tcriteria: Object.fromEntries(\n\t\t\t\t\tLEDGER_CATEGORIES.map((category) => [\n\t\t\t\t\t\tcategory,\n\t\t\t\t\t\tthis.#options.questions.category.criteria[category],\n\t\t\t\t\t]),\n\t\t\t\t),\n\t\t\t},\n\t\t\tsources: [id],\n\t\t\tstate: this.#renderState(id),\n\t\t\tmodel: this.#options.judge.model,\n\t\t}\n\t}\n\n\t#buildTopic(id: string, topic: LedgerTopic): JudgmentInput {\n\t\treturn {\n\t\t\tid: JSON.stringify(['topic', id, topic.name]),\n\t\t\tquestion: {\n\t\t\t\tform: 'noul',\n\t\t\t\tinstructions: this.#options.questions.topic,\n\t\t\t\tcriteria: {\n\t\t\t\t\ttrue: `The message concerns ${topic.name}: ${topic.criterion}`,\n\t\t\t\t\tfalse: `The message does not concern ${topic.name}`,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsources: [id],\n\t\t\tstate: this.#renderState(id),\n\t\t\tmodel: this.#options.judge.model,\n\t\t}\n\t}\n\n\t#buildPair(head: 'amends' | 'supersedes', earlier: string, later: string): JudgmentInput {\n\t\treturn {\n\t\t\tid: JSON.stringify([head, earlier, later]),\n\t\t\tquestion: this.#options.questions[head],\n\t\t\tsources: [earlier, later],\n\t\t\tstate: `Earlier message: ${this.#renderState(earlier)}\\nLater message: ${this.#renderState(later)}`,\n\t\t\tmodel: this.#options.judge.model,\n\t\t}\n\t}\n\n\t#read(spec: JudgmentInput): Judgment | undefined {\n\t\tconst recorded = this.#options.conversation.judgments.judgment(spec.id)\n\t\treturn recorded !== undefined &&\n\t\t\tmatchesJudgment(recorded, spec.question, spec.sources, spec.state, this.#options.judge.model)\n\t\t\t? recorded\n\t\t\t: undefined\n\t}\n\n\t#readNoul(spec: JudgmentInput): number | undefined {\n\t\tconst answer = this.#read(spec)?.answer\n\t\treturn answer?.form === 'noul' ? answer.noul : undefined\n\t}\n\n\t#readCategories(id: string): ChoiceAnswer | undefined {\n\t\tconst answer = this.#read(this.#buildCategory(id))?.answer\n\t\treturn answer?.form === 'choice' ? answer : undefined\n\t}\n\n\t#weigh(id: string, categories: readonly LedgerCategory[]): number {\n\t\tconst answer = this.#readCategories(id)\n\t\treturn categories.reduce((sum, category) => sum + (answer?.probabilities[category] ?? 0), 0)\n\t}\n\n\t#collectEntities(message: Message): ReadonlySet<string> {\n\t\tconst argumentsText =\n\t\t\tmessage.calls === undefined\n\t\t\t\t? undefined\n\t\t\t\t: renderJSON(message.calls.map((call) => call.arguments))\n\t\treturn this.#options.entities(\n\t\t\targumentsText === undefined ? message.content : `${message.content} ${argumentsText}`,\n\t\t\tfalse,\n\t\t)\n\t}\n\n\t// Reads a message's registry entities from this call's memo, collecting them on the first read.\n\t#readEntities(message: Message, entities: Map<string, ReadonlySet<string>>): ReadonlySet<string> {\n\t\tconst known = entities.get(message.id)\n\t\tif (known !== undefined) return known\n\t\tconst found = this.#collectEntities(message)\n\t\tentities.set(message.id, found)\n\t\treturn found\n\t}\n\n\t#collectNear(message: Message, entities: Map<string, ReadonlySet<string>>): ReadonlySet<string> {\n\t\treturn new Set([\n\t\t\t...this.#readEntities(message, entities),\n\t\t\t...(this.#options.assign(message) === undefined ? this.topics(message.id) : []),\n\t\t])\n\t}\n\n\t// Reads a message's near set from this call's memo, collecting it on the first read.\n\t#readNear(\n\t\tmessage: Message,\n\t\tnears: Map<string, ReadonlySet<string>>,\n\t\tentities: Map<string, ReadonlySet<string>>,\n\t): ReadonlySet<string> {\n\t\tconst known = nears.get(message.id)\n\t\tif (known !== undefined) return known\n\t\tconst near = this.#collectNear(message, entities)\n\t\tnears.set(message.id, near)\n\t\treturn near\n\t}\n\n\t// Reads a message's literals from this call's memo, extracting them on the first read.\n\t#readLiterals(message: Message, literals: Map<string, LiteralSet>): LiteralSet {\n\t\tconst known = literals.get(message.id)\n\t\tif (known !== undefined) return known\n\t\tconst found = extractLiterals(message.content)\n\t\tliterals.set(message.id, found)\n\t\treturn found\n\t}\n\n\t// Reads a message's quiet flag from this call's memo, reading it on the first request.\n\t#readQuiet(id: string, quiets: Map<string, boolean>): boolean {\n\t\tconst known = quiets.get(id)\n\t\tif (known !== undefined) return known\n\t\tconst quiet = this.quiet(id)\n\t\tquiets.set(id, quiet)\n\t\treturn quiet\n\t}\n\n\t// Reports whether two texts' literals share an id or a number.\n\t#sharesLiteral(own: LiteralSet, other: LiteralSet): boolean {\n\t\treturn (\n\t\t\t[...own.ids].some((id) => other.ids.has(id)) ||\n\t\t\t[...own.numbers].some((number) => other.numbers.has(number))\n\t\t)\n\t}\n\n\t// Reports whether two messages share a registry entity, topics excluded, or a literal of their content.\n\t#sharesEntityOrLiteral(\n\t\tearlier: Message,\n\t\tlater: Message,\n\t\tentities: Map<string, ReadonlySet<string>>,\n\t\tliterals: Map<string, LiteralSet>,\n\t): boolean {\n\t\tconst near = this.#readEntities(later, entities)\n\t\treturn (\n\t\t\t[...this.#readEntities(earlier, entities)].some((entity) => near.has(entity)) ||\n\t\t\tthis.#sharesLiteral(\n\t\t\t\tthis.#readLiterals(earlier, literals),\n\t\t\t\tthis.#readLiterals(later, literals),\n\t\t\t)\n\t\t)\n\t}\n\n\t// Reports whether a correction is compared against an earlier message: the message is not quiet\n\t// and its near set shares a member with the correction's, and under `narrow` the two also share a\n\t// registry entity or a literal. The quiet flag is read first, so a quiet message's near set is\n\t// never read, and with `narrow` unset no literal is read here.\n\t#eligible(\n\t\tearlier: Message,\n\t\tlater: Message,\n\t\tnear: ReadonlySet<string>,\n\t\tquiets: Map<string, boolean>,\n\t\tnears: Map<string, ReadonlySet<string>>,\n\t\tentities: Map<string, ReadonlySet<string>>,\n\t\tliterals: Map<string, LiteralSet>,\n\t): boolean {\n\t\treturn (\n\t\t\t!this.#readQuiet(earlier.id, quiets) &&\n\t\t\t[...this.#readNear(earlier, nears, entities)].some((topic) => near.has(topic)) &&\n\t\t\t(this.#options.correction?.narrow !== true ||\n\t\t\t\tthis.#sharesEntityOrLiteral(earlier, later, entities, literals))\n\t\t)\n\t}\n\n\tasync #ask(\n\t\tspec: JudgmentInput,\n\t\tphase: ClassifierPhase,\n\t\tsignal: AbortSignal,\n\t\tresults: ClassifierResult[],\n\t): Promise<ClassifierResult> {\n\t\tif (this.#read(spec) !== undefined)\n\t\t\treturn { judgments: [spec.id], attempts: [{ key: spec.id, phase, outcome: 'cached' }] }\n\t\tconst fingerprint = JSON.stringify(spec)\n\t\tif (this.#failed.has(fingerprint))\n\t\t\treturn { judgments: [], attempts: [{ key: spec.id, phase, outcome: 'skipped' }] }\n\t\tsignal.throwIfAborted()\n\t\tif (spec.sources.some((id) => this.#options.conversation.message(id) === undefined))\n\t\t\treturn { judgments: [], attempts: [{ key: spec.id, phase, outcome: 'skipped' }] }\n\t\tconst started = performance.now()\n\t\ttry {\n\t\t\tconst [judgment] = await this.#options.conversation.judgments.resolve(\n\t\t\t\tthis.#options.judge,\n\t\t\t\t{ state: spec.state, questions: { [spec.id]: spec.question } },\n\t\t\t\tspec.sources,\n\t\t\t\tsignal,\n\t\t\t)\n\t\t\tconst attempts: readonly ClassifierAttempt[] = [\n\t\t\t\t{ key: spec.id, phase, outcome: 'resolved', duration: performance.now() - started },\n\t\t\t]\n\t\t\treturn judgment === undefined\n\t\t\t\t? { judgments: [], attempts }\n\t\t\t\t: {\n\t\t\t\t\t\tjudgments: [spec.id],\n\t\t\t\t\t\t...(judgment.usage === undefined ? {} : { usage: judgment.usage }),\n\t\t\t\t\t\tattempts,\n\t\t\t\t\t}\n\t\t} catch (error) {\n\t\t\t// A rejection records its attempt in the pushed result alone, so the empty result returned under a live signal repeats none.\n\t\t\tconst attempts: readonly ClassifierAttempt[] = [\n\t\t\t\t{\n\t\t\t\t\tkey: spec.id,\n\t\t\t\t\tphase,\n\t\t\t\t\toutcome: isJudgeAbortError(error) || signal.aborted ? 'aborted' : 'failed',\n\t\t\t\t\tduration: performance.now() - started,\n\t\t\t\t},\n\t\t\t]\n\t\t\tif (isJudgeAbortError(error))\n\t\t\t\tresults.push({\n\t\t\t\t\tjudgments:\n\t\t\t\t\t\tObject.hasOwn(error.partial.answers, spec.id) ||\n\t\t\t\t\t\t(error.partial.refusals !== undefined && Object.hasOwn(error.partial.refusals, spec.id))\n\t\t\t\t\t\t\t? [spec.id]\n\t\t\t\t\t\t\t: [],\n\t\t\t\t\t...(error.partial.usage === undefined ? {} : { usage: error.partial.usage }),\n\t\t\t\t\tattempts,\n\t\t\t\t})\n\t\t\telse results.push({ judgments: [], attempts })\n\t\t\tif (signal.aborted) throw error\n\t\t\tconst failure = isError(error) ? error : new Error(errorToMessage(error))\n\t\t\t// Under the default `strict` this failure rejects the request it belongs to and the next request\n\t\t\t// asks again; a lenient request goes on past it, so a question that cannot succeed is held.\n\t\t\tif (\n\t\t\t\tthis.#options.strict === false &&\n\t\t\t\t((isJudgeError(error) && error.code === 'QUESTION') ||\n\t\t\t\t\tDETERMINISTIC_JUDGE_ERROR.test(renderCauseChain(error)))\n\t\t\t)\n\t\t\t\tthis.#failed.add(fingerprint)\n\t\t\treturn { judgments: [], attempts: [], failure }\n\t\t}\n\t}\n}\n","import type { ThinkingReplay } from '../types.js'\nimport type { GaugeCall, GaugeInterface, GaugeOptions } from './types.js'\nimport { isFiniteNumber } from '@orkestrel/contract'\nimport { estimateMessages } from '../agents/helpers.js'\nimport { LedgerError } from './errors.js'\nimport { isPositiveSafeInteger } from '../validators.js'\nimport { buildRecallMessage, fitSlope, resolvePredict } from './helpers.js'\n\n/**\n * Prices prompts in tokens from a measured scale and overhead cost, and measures the room a request\n * has left.\n *\n * @remarks\n * `observe` rescales from the first call of each finished request with the overhead cost taken out,\n * and keeps that request's calls for the marginal rate and the longest final completion for the reply\n * reserve after subtracting its thinking. `overhead` never changes after construction.\n * {@link GaugeOptions} defines the measured use, reply reserve, and recall-room formulas.\n * {@link LedgerOptions} defines the ledger's plan budget and recall close rule.\n *\n * @example\n * ```ts\n * const gauge = new Gauge({ scale: 1.16, overhead: 498, capacity: 32768 })\n * gauge.room([{ estimate: 1719, prompt: 2498, tools: 3 }], '') // estimate units a recall result can take\n * ```\n */\nexport class Gauge implements GaugeInterface {\n\treadonly #capacity: number\n\treadonly #predict: number\n\treadonly #replay: ThinkingReplay\n\t#history: ReadonlyArray<readonly GaugeCall[]> = []\n\t#scale: number\n\t#overhead: number\n\t#reply = 0\n\n\t/**\n\t * Holds the starting price and the capacity.\n\t *\n\t * @param options - The starting `scale` and `overhead` price and the context `capacity`\n\t * @throws {LedgerError} Thrown when `scale` is not finite and above 0, or `overhead` is not finite and at least 0 (code `'GAUGE'`)\n\t * @throws {LedgerError} Thrown when `capacity` is not a positive safe integer (code `'CAPACITY'`)\n\t * @throws {LedgerError} Thrown when `predict` is not a nonnegative safe integer less than `capacity` (code `'CAPACITY'`)\n\t */\n\tconstructor(options: GaugeOptions) {\n\t\tif (!isFiniteNumber(options.scale) || options.scale <= 0) {\n\t\t\tthrow new LedgerError('GAUGE', 'gauge scale must be finite and greater than 0')\n\t\t}\n\t\tif (!isFiniteNumber(options.overhead) || options.overhead < 0) {\n\t\t\tthrow new LedgerError('GAUGE', 'gauge overhead must be finite and at least 0')\n\t\t}\n\t\tif (!isPositiveSafeInteger(options.capacity)) {\n\t\t\tthrow new LedgerError('CAPACITY', 'gauge capacity must be a positive safe integer')\n\t\t}\n\t\tthis.#scale = options.scale\n\t\tthis.#overhead = options.overhead\n\t\tthis.#capacity = options.capacity\n\t\tthis.#predict = resolvePredict(options.predict, options.capacity)\n\t\tthis.#replay = options.replay ?? 'none'\n\t}\n\n\tget scale(): number {\n\t\treturn this.#scale\n\t}\n\n\tget overhead(): number {\n\t\treturn this.#overhead\n\t}\n\n\t/**\n\t * Reads the marginal rate of prompt tokens per estimate unit.\n\t *\n\t * @remarks\n\t * Returns the scale when the fit is undefined, nonfinite, or not above 0, so a degenerate fit never prices recall room.\n\t *\n\t * @param calls - The calls of the request in progress\n\t * @returns The fitted slope, or the scale when the slope is unusable\n\t */\n\trate(calls: readonly GaugeCall[]): number {\n\t\tconst slope = fitSlope([...this.#history, calls])\n\t\treturn slope !== undefined && isFiniteNumber(slope) && slope > 0 ? slope : this.#scale\n\t}\n\n\tremainder(calls: readonly GaugeCall[]): number {\n\t\tconst call = calls.at(-1)\n\t\tconst used = isFiniteNumber(call?.prompt)\n\t\t\t? call.prompt + (call.completion ?? 0) - (this.#replay === 'none' ? (call.thinking ?? 0) : 0)\n\t\t\t: this.#overhead + this.#scale * (call?.estimate ?? 0)\n\t\treturn Math.max(0, this.#capacity - used)\n\t}\n\n\treserve(calls: readonly GaugeCall[], longest: string): number {\n\t\tconst rate = this.rate(calls)\n\t\t// Before any reply is observed, the longest assistant text stands in for the reply.\n\t\tconst reply =\n\t\t\tthis.#reply > 0\n\t\t\t\t? this.#reply\n\t\t\t\t: rate * estimateMessages([{ id: 'reply', role: 'assistant', content: longest }])\n\t\tconst recall =\n\t\t\trate * estimateMessages([buildRecallMessage(''), { id: 'result', role: 'tool', content: '' }])\n\t\treturn reply + recall\n\t}\n\n\troom(calls: readonly GaugeCall[], longest: string): number {\n\t\treturn Math.max(\n\t\t\t0,\n\t\t\t(this.remainder(calls) - this.#predict - this.reserve(calls, longest)) / 2 / this.rate(calls),\n\t\t)\n\t}\n\n\tobserve(calls: readonly GaugeCall[], reply?: GaugeCall): void {\n\t\tconst first = calls[0]\n\t\tif (first !== undefined && isFiniteNumber(first.prompt) && first.estimate > 0) {\n\t\t\tconst priced = first.prompt - this.#overhead\n\t\t\tif (priced > 0) this.#scale = priced / first.estimate\n\t\t}\n\t\tthis.#history = [...this.#history, [...calls]]\n\t\tif (isFiniteNumber(reply?.completion))\n\t\t\tthis.#reply = Math.max(this.#reply, reply.completion - (reply.thinking ?? 0))\n\t}\n}\n","import type { Selection } from '../contexts/types.js'\nimport type { GaugeCall } from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport { sumUsage } from '../helpers.js'\n\n/**\n * Holds what one pass of a ledger request keeps: the selection its run adopted last, the\n * conversation boundary its calls count additions from, its summed usage, and its gauge calls.\n *\n * @remarks\n * A `LedgerRequest` opens one pass per agent run and exposes only projections of it, so no public\n * signature names this class. The pass appends one final record to its log for each call observed,\n * each usage reported, and each thinking share, in emission order, and derives `calls`, `call`, and\n * `position` from it: a usage or thinking record joins the last call observed before it, and one\n * recorded before the first call joins none.\n */\nexport class LedgerPass {\n\t#log: ReadonlyArray<\n\t\t| { readonly call: GaugeCall; readonly position: number }\n\t\t| { readonly usage: { readonly prompt: number; readonly completion: number } }\n\t\t| { readonly thinking: number }\n\t> = []\n\t#selection: Selection | undefined\n\t#boundary: number\n\t#usage: TokenUsage | undefined\n\n\t/**\n\t * Opens the pass at a conversation boundary.\n\t *\n\t * @param boundary - The conversation length when the pass's run starts\n\t */\n\tconstructor(boundary: number) {\n\t\tthis.#boundary = boundary\n\t}\n\n\t/**\n\t * Returns the selection the pass's run adopted last.\n\t *\n\t * @returns The selection, or undefined before one\n\t */\n\tget selection(): Selection | undefined {\n\t\treturn this.#selection\n\t}\n\n\t/**\n\t * Returns the conversation length the pass's calls count additions from.\n\t *\n\t * @returns The length at the pass's start or at its last adopted selection\n\t */\n\tget boundary(): number {\n\t\treturn this.#boundary\n\t}\n\n\t/**\n\t * Returns the summed usage of the pass's selections and calls.\n\t *\n\t * @returns The summed usage, or undefined while none reported usage\n\t */\n\tget usage(): TokenUsage | undefined {\n\t\treturn this.#usage\n\t}\n\n\t/**\n\t * Returns the call in flight.\n\t *\n\t * @returns The latest call the pass observed, or undefined before one\n\t */\n\tget call(): GaugeCall | undefined {\n\t\treturn this.calls.at(-1)\n\t}\n\n\t/**\n\t * Returns the conversation length when the call in flight started.\n\t *\n\t * @returns The position, or undefined before a call\n\t */\n\tget position(): number | undefined {\n\t\treturn this.#log.flatMap((entry) => ('position' in entry ? [entry.position] : [])).at(-1)\n\t}\n\n\t/**\n\t * Returns the pass's gauge calls in order.\n\t *\n\t * @returns Each observed call with the last usage and thinking recorded after it, before the next call\n\t */\n\tget calls(): readonly GaugeCall[] {\n\t\treturn this.#log.flatMap((entry, index) => {\n\t\t\tif (!('call' in entry)) return []\n\t\t\tconst after = this.#log.slice(index + 1)\n\t\t\tconst next = after.findIndex((one) => 'call' in one)\n\t\t\tconst joined = next === -1 ? after : after.slice(0, next)\n\t\t\tconst usage = joined.flatMap((one) => ('usage' in one ? [one.usage] : [])).at(-1)\n\t\t\tconst thinking = joined.flatMap((one) => ('thinking' in one ? [one.thinking] : [])).at(-1)\n\t\t\tconst billed =\n\t\t\t\tusage === undefined\n\t\t\t\t\t? entry.call\n\t\t\t\t\t: { ...entry.call, prompt: usage.prompt, completion: usage.completion }\n\t\t\treturn [thinking === undefined ? billed : { ...billed, thinking }]\n\t\t})\n\t}\n\n\t/**\n\t * Records the selection the pass's run adopted and adds the usage it spent.\n\t *\n\t * @param selection - The selection the run built its prompt from\n\t * @param boundary - The conversation length when the run adopted it\n\t */\n\tadopt(selection: Selection, boundary: number): void {\n\t\tthis.#selection = selection\n\t\tthis.#boundary = boundary\n\t\tif (selection.usage !== undefined) this.#usage = sumUsage(this.#usage, selection.usage)\n\t}\n\n\t/**\n\t * Starts a call, appending its record to the log.\n\t *\n\t * @param call - The call's estimate and advertised tool count\n\t * @param position - The conversation length when the call starts\n\t */\n\tobserve(call: GaugeCall, position: number): void {\n\t\tthis.#log = [...this.#log, { call, position }]\n\t}\n\n\t/**\n\t * Adds one call's reported usage to the pass's summed usage.\n\t *\n\t * @param usage - The usage the provider reported\n\t */\n\tspend(usage: TokenUsage): void {\n\t\tthis.#usage = sumUsage(this.#usage, usage)\n\t}\n\n\t/**\n\t * Appends a reported usage to the log, which joins the call observed last as its prompt and completion.\n\t *\n\t * @remarks\n\t * The record keeps the prompt and completion values the usage held at this call, so a later change\n\t * to the caller's object never reaches a reading.\n\t *\n\t * @param usage - The usage the provider reported for the call\n\t */\n\tbill(usage: TokenUsage): void {\n\t\tthis.#log = [...this.#log, { usage: { prompt: usage.prompt, completion: usage.completion } }]\n\t}\n\n\t/**\n\t * Appends the thinking share to the log, which joins the call observed last.\n\t *\n\t * @param thinking - The completion tokens attributed to the call's thinking\n\t */\n\tthink(thinking: number): void {\n\t\tthis.#log = [...this.#log, { thinking }]\n\t}\n}\n","import type { Selection } from '../contexts/types.js'\nimport type { ConversationInterface } from '../conversations/types.js'\nimport type { Message } from '../types.js'\nimport type { ClassifierAttempt, GaugeCall, LedgerRequestInterface } from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport { canonicalStringify } from '@orkestrel/contract'\nimport { isNonNegativeSafeInteger } from '../validators.js'\nimport { DEFAULT_RECALL_LIMIT, LEDGER_NOTES } from './constants.js'\nimport { LedgerError } from './errors.js'\nimport { computeThinking } from './helpers.js'\nimport { LedgerPass } from './LedgerPass.js'\n\n/**\n * Holds what one ledger request keeps while it is served: its message, its filing attempts and\n * plan, the gauge calls of its passes, and its repeat and recall stops.\n *\n * @remarks\n * A ledger creates one request per `respond` call and drops it when the call settles. Each pass the\n * request opens holds its own selection, boundary, usage, and calls; the request reads the current\n * pass, the last one opened. {@link LedgerRequestInterface} describes each member.\n *\n * @example\n * ```ts\n * const request = new LedgerRequest('What approval is needed?', 1)\n * request.enter(conversation) // the appended user message\n * request.admit('lookup', { id: 'BW-20931' })\n * request.admit('lookup', { id: 'BW-20931' }) // throws Error(LEDGER_NOTES.repeat)\n * request.recall()\n * request.recall() // throws Error(LEDGER_NOTES.closure): the limit of 1 is spent\n * ```\n */\nexport class LedgerRequest implements LedgerRequestInterface {\n\treadonly #content: string\n\treadonly #limit: number\n\t// The errors this request's run reported through `fault`, so `respond` tells a selection failure from a provider one.\n\treadonly #faults = new Set<unknown>()\n\t#message: Message | undefined\n\t#plan: Selection | undefined\n\t#passes: readonly LedgerPass[] = []\n\t#attempts: readonly ClassifierAttempt[] = []\n\t#admitted: ReadonlySet<string> = new Set()\n\t#listed: ReadonlyMap<string, ReadonlyMap<string, string>> = new Map()\n\t#recalls = 0\n\t#closed = false\n\n\t/**\n\t * Holds the request's content and its recall limit.\n\t *\n\t * @param content - The request text `enter` appends\n\t * @param limit - The `recall` calls the request can make. Default: the `DEFAULT_RECALL_LIMIT` constant\n\t * @throws {LedgerError} Thrown when `limit` is not a nonnegative safe integer (code `'LIMIT'`)\n\t */\n\tconstructor(content: string, limit: number = DEFAULT_RECALL_LIMIT) {\n\t\tif (!isNonNegativeSafeInteger(limit))\n\t\t\tthrow new LedgerError('LIMIT', 'limits must be nonnegative safe integers')\n\t\tthis.#content = content\n\t\tthis.#limit = limit\n\t}\n\n\t/**\n\t * Returns the request message.\n\t *\n\t * @returns The message `enter` appended, or undefined before it\n\t */\n\tget message(): Message | undefined {\n\t\treturn this.#message\n\t}\n\n\t/**\n\t * Returns the selection the request's last successful plan produced.\n\t *\n\t * @returns The planned selection, or undefined before one\n\t */\n\tget plan(): Selection | undefined {\n\t\treturn this.#plan\n\t}\n\n\t/**\n\t * Returns the selection the current pass's run adopted last.\n\t *\n\t * @returns The selection, or undefined before one\n\t */\n\tget selection(): Selection | undefined {\n\t\treturn this.#passes.at(-1)?.selection\n\t}\n\n\t/**\n\t * Returns the conversation length the current pass's calls count additions from.\n\t *\n\t * @returns The length, or undefined before a pass\n\t */\n\tget boundary(): number | undefined {\n\t\treturn this.#passes.at(-1)?.boundary\n\t}\n\n\t/**\n\t * Returns the summed usage of the current pass.\n\t *\n\t * @returns The summed usage, or undefined while none reported usage\n\t */\n\tget usage(): TokenUsage | undefined {\n\t\treturn this.#passes.at(-1)?.usage\n\t}\n\n\t/**\n\t * Returns the gauge calls of every pass.\n\t *\n\t * @returns The calls in order, the call in flight last\n\t */\n\tget calls(): readonly GaugeCall[] {\n\t\treturn this.#passes.flatMap((pass) => pass.calls)\n\t}\n\n\t/**\n\t * Returns the filing attempts of the request's planning selections.\n\t *\n\t * @returns The attempts in asking order\n\t */\n\tget attempts(): readonly ClassifierAttempt[] {\n\t\treturn this.#attempts\n\t}\n\n\tenter(conversation: ConversationInterface): Message {\n\t\tif (this.#message !== undefined) return this.#message\n\t\tthis.#message = conversation.add({ role: 'user', content: this.#content })\n\t\treturn this.#message\n\t}\n\n\topen(boundary: number): void {\n\t\tthis.#passes = [...this.#passes, new LedgerPass(boundary)]\n\t}\n\n\tadopt(selection: Selection, boundary: number): void {\n\t\tthis.#passes.at(-1)?.adopt(selection, boundary)\n\t}\n\n\tobserve(call: GaugeCall, position: number): void {\n\t\tthis.#passes.at(-1)?.observe(call, position)\n\t}\n\n\tspend(usage: TokenUsage): void {\n\t\tthis.#passes.at(-1)?.spend(usage)\n\t\t// The call in flight can belong to an earlier pass until the current pass observes its first call.\n\t\tthis.#passes.findLast((pass) => pass.call !== undefined)?.bill(usage)\n\t}\n\n\tthink(messages: readonly Message[]): void {\n\t\tconst pass = this.#passes.findLast((one) => one.call !== undefined)\n\t\tconst completion = pass?.call?.completion\n\t\tif (pass?.position === undefined || completion === undefined) return\n\t\tconst message = messages.slice(pass.position).find((entry) => entry.role === 'assistant')\n\t\tif (message?.thinking === undefined) return\n\t\tpass.think(computeThinking(message, completion))\n\t}\n\n\tfault(error: unknown): void {\n\t\tthis.#faults.add(error)\n\t}\n\n\tfaulted(error: unknown): boolean {\n\t\treturn this.#faults.has(error)\n\t}\n\n\tfile(attempts: readonly ClassifierAttempt[], plan?: Selection): void {\n\t\tthis.#attempts = [...this.#attempts, ...attempts]\n\t\tif (plan !== undefined) this.#plan = plan\n\t}\n\n\tadmit(name: string, args: Readonly<Record<string, unknown>>): void {\n\t\tconst key = canonicalStringify([name, args])\n\t\tif (key === undefined) throw new Error('tool arguments have no canonical identity')\n\t\tif (this.#admitted.has(key)) throw new Error(LEDGER_NOTES.repeat)\n\t\tthis.#admitted = new Set([...this.#admitted, key])\n\t}\n\n\trecall(): void {\n\t\tif (this.#recalls >= this.#limit) this.#closed = true\n\t\tif (this.#closed) throw new Error(LEDGER_NOTES.closure)\n\t\tthis.#recalls += 1\n\t}\n\n\tclose(): void {\n\t\tthis.#closed = true\n\t}\n\n\tlist(result: string, lines: ReadonlyMap<string, string>): void {\n\t\tthis.#listed = new Map([...this.#listed, [result, lines]])\n\t}\n\n\tlines(result: string): ReadonlyMap<string, string> | undefined {\n\t\treturn this.#listed.get(result)\n\t}\n}\n","import type { Message } from '../types.js'\nimport type { LedgerIndex, LedgerLookupFinding, LedgerRegistry } from './types.js'\nimport { collectToolGroups } from '../helpers.js'\nimport { buildLedgerIndex, collectRegistry } from './helpers.js'\n\n/**\n * Holds what one read of a ledger's conversation yields: the messages, the lookup findings, the\n * tool groups, the registry with its index, and each message's position.\n *\n * @remarks\n * A `Ledger` builds one scan for a selection or a `recall` call from the conversation's messages and\n * the lookup findings it read for them, and reads these values from the scan in place of reading the\n * conversation and the lookups again, so no public signature names this class. The scan computes\n * every value when it is built, never changes one, and reads no lookup itself.\n */\nexport class LedgerScan {\n\treadonly #messages: readonly Message[]\n\treadonly #findings: readonly LedgerLookupFinding[]\n\treadonly #lookups: ReadonlyMap<string, LedgerLookupFinding>\n\treadonly #groups: ReadonlyMap<string, readonly Message[]>\n\treadonly #positions: ReadonlyMap<string, number>\n\treadonly #first: number | undefined\n\treadonly #registry: LedgerRegistry\n\treadonly #index: LedgerIndex\n\n\t/**\n\t * Computes the scan's values from one read of the conversation.\n\t *\n\t * @param messages - The conversation's messages as the read returned them\n\t * @param findings - The lookup findings of those messages, in conversation order\n\t * @param requests - The ids of the ledger's request messages\n\t * @throws {TypeError} Thrown when a finding's owner list or an owner's name list holds a hole\n\t */\n\tconstructor(\n\t\tmessages: readonly Message[],\n\t\tfindings: readonly LedgerLookupFinding[],\n\t\trequests: ReadonlySet<string>,\n\t) {\n\t\tthis.#messages = messages\n\t\tthis.#findings = findings\n\t\tthis.#lookups = new Map(findings.map((finding) => [finding.id, finding]))\n\t\t// Reversed, so a message that two groups held would map to the first, as a search finds it.\n\t\tthis.#groups = new Map(\n\t\t\tcollectToolGroups(messages)\n\t\t\t\t.toReversed()\n\t\t\t\t.flatMap((group) => group.map((message) => [message.id, group] as const)),\n\t\t)\n\t\tthis.#positions = new Map(messages.map((message, at) => [message.id, at]))\n\t\tconst first = messages.findIndex((message) => requests.has(message.id))\n\t\tthis.#first = first < 0 ? undefined : first\n\t\tthis.#registry = collectRegistry(findings)\n\t\tthis.#index = buildLedgerIndex(this.#registry)\n\t}\n\n\t/**\n\t * Returns the messages the scan read.\n\t *\n\t * @returns The conversation's messages in order\n\t */\n\tget messages(): readonly Message[] {\n\t\treturn this.#messages\n\t}\n\n\t/**\n\t * Returns the lookup findings of the scanned messages.\n\t *\n\t * @returns One finding per successful lookup result, in conversation order\n\t */\n\tget findings(): readonly LedgerLookupFinding[] {\n\t\treturn this.#findings\n\t}\n\n\t/**\n\t * Returns the registry the findings name.\n\t *\n\t * @returns The ids and owner names the findings named\n\t */\n\tget registry(): LedgerRegistry {\n\t\treturn this.#registry\n\t}\n\n\t/**\n\t * Returns the registry's matching index.\n\t *\n\t * @returns The index built from the scan's registry\n\t */\n\tget index(): LedgerIndex {\n\t\treturn this.#index\n\t}\n\n\t/**\n\t * Returns the position of the first request message.\n\t *\n\t * @returns The position, or undefined when no scanned message is a request\n\t */\n\tget first(): number | undefined {\n\t\treturn this.#first\n\t}\n\n\t/**\n\t * Reads the lookup finding of a tool message.\n\t *\n\t * @param id - The tool message's id\n\t * @returns The finding, or undefined when the message has no successful lookup finding\n\t */\n\tfinding(id: string): LedgerLookupFinding | undefined {\n\t\treturn this.#lookups.get(id)\n\t}\n\n\t/**\n\t * Reads the tool group that holds a message.\n\t *\n\t * @param id - The message's id\n\t * @returns The group, or undefined when no tool group holds the message\n\t */\n\tgroup(id: string): readonly Message[] | undefined {\n\t\treturn this.#groups.get(id)\n\t}\n\n\t/**\n\t * Reads a message's position among the scanned messages.\n\t *\n\t * @param id - The message's id\n\t * @returns The position, or undefined when the scan holds no message with that id\n\t */\n\tposition(id: string): number | undefined {\n\t\treturn this.#positions.get(id)\n\t}\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\tstringShape,\n} from '@orkestrel/contract'\nimport { conversationSnapshotShape } from '../conversations/shapers.js'\n\n/**\n * Describes a ledger snapshot exactly: an unknown key refuses, so a key a later version adds never reads\n * as absent.\n *\n * @remarks\n * `isLedgerSnapshot` is the door a hydrate reads, and this shape fills a refusal's faults. What the shape\n * cannot state, that each marked id names a message of the snapshot's own conversation, is the guard's\n * alone.\n */\nexport const ledgerSnapshotShape = objectShape({\n\tconversation: conversationSnapshotShape,\n\trequests: arrayShape(stringShape()),\n\tnotes: arrayShape(stringShape()),\n\tfailures: arrayShape(stringShape()),\n\tgauge: optionalShape(objectShape({ scale: numberShape(), overhead: numberShape() })),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { ledgerSnapshotShape } from './shapers.js'\n\n/** Reads the faults of a refused ledger snapshot. */\nexport const ledgerSnapshotContract = createContract(ledgerSnapshotShape)\n","import type { LedgerQuestion, LedgerSnapshot } from './types.js'\nimport { arrayOf, holds, isFiniteNumber, isRecord, isString } from '@orkestrel/contract'\nimport { LEDGER_CATEGORIES } from './constants.js'\nimport { isConversationSnapshot } from '../conversations/validators.js'\nimport { isJudgeEntry, isJudgeQuestion } from '../validators.js'\n\n/**\n * Checks whether a value is finite and lies in the interval (0, 1].\n * @param value - The value to inspect\n * @returns True if the value is a valid ledger fraction; false otherwise\n * @example\n * ```ts\n * isFraction(0.8) // true\n * ```\n */\nexport function isFraction(value: unknown): value is number {\n\treturn isFiniteNumber(value) && value > 0 && value <= 1\n}\n\n/**\n * Checks whether a value holds the wording a ledger files with: a choice `category` question whose\n * criteria describe every category, a non-empty `topic` string, and noul `amends` and `supersedes`\n * questions.\n *\n * @remarks\n * A ledger asks these on every message, so a hole in them would otherwise surface as filing that quietly\n * stays undecided. Total: an unreadable member returns false.\n *\n * @param value - The value to inspect\n * @returns True if the value holds the wording the ledger's questions read; false otherwise\n * @example\n * ```ts\n * isLedgerQuestion(LEDGER_QUESTIONS) // true\n * isLedgerQuestion({ topic: 'Does it concern this?' }) // false\n * ```\n */\nexport function isLedgerQuestion(value: unknown): value is LedgerQuestion {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { category, topic, amends, supersedes } = value\n\t\tif (!isJudgeQuestion(category) || category.form !== 'choice') return false\n\t\tconst { criteria } = category\n\t\treturn (\n\t\t\tLEDGER_CATEGORIES.every((one) => isJudgeEntry(criteria[one])) &&\n\t\t\tisString(topic) &&\n\t\t\ttopic.trim() !== '' &&\n\t\t\tisJudgeQuestion(amends) &&\n\t\t\tamends.form === 'noul' &&\n\t\t\tisJudgeQuestion(supersedes) &&\n\t\t\tsupersedes.form === 'noul'\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a ledger snapshot whose marked ids name messages of its own conversation.\n *\n * @remarks\n * `conversation` must pass `isConversationSnapshot`, and `requests`, `notes`, and `failures` must be\n * dense arrays of strings. Each request and note id must name a `user` message of\n * `conversation.messages` and each failure id a `tool` message, so an id only a section original\n * holds is refused. A `gauge`, when present, must hold a finite `scale` greater than 0 and a finite\n * `overhead` of at least 0. The guard is exact: an unknown member and an own `undefined` `gauge` return\n * false, at the snapshot and in its `gauge`. Unreadable members and untrusted inputs return false. The\n * guard is structural: it admits a conversation holding a stored `system` role\n * or a repeated id, which `requireConversationSnapshot` and the `Conversation` constructor then\n * refuse as the stored-data check a ledger's hydrate runs.\n *\n * @param value - The value to inspect, such as a parsed checkpoint\n * @returns True if the value has the shape of a ledger snapshot whose marked ids name messages of its own conversation; false otherwise\n * @example\n * ```ts\n * const conversation = { id: 'c1', sections: [], messages: [{ id: 'm1', role: 'user', content: 'Check FX-111.' }] }\n * isLedgerSnapshot({ conversation, requests: ['m1'], notes: [], failures: [] }) // true\n * isLedgerSnapshot({ conversation, requests: [], notes: [], failures: ['m1'] }) // false\n * ```\n */\nexport function isLedgerSnapshot(value: unknown): value is LedgerSnapshot {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { conversation, requests, notes, failures, gauge } = value\n\t\tif (Object.keys(value).length !== (gauge === undefined ? 4 : 5)) return false\n\t\tif (!isConversationSnapshot(conversation)) return false\n\t\tconst isIds = arrayOf(isString)\n\t\tif (!isIds(requests) || !isIds(notes) || !isIds(failures)) return false\n\t\tif (gauge !== undefined) {\n\t\t\tif (!isRecord(gauge) || Object.keys(gauge).length !== 2) return false\n\t\t\tconst { scale, overhead } = gauge\n\t\t\tif (!isFiniteNumber(scale) || scale <= 0 || !isFiniteNumber(overhead) || overhead < 0)\n\t\t\t\treturn false\n\t\t}\n\t\tconst roles = new Map(conversation.messages.map((message) => [message.id, message.role]))\n\t\treturn (\n\t\t\trequests.every((id) => roles.get(id) === 'user') &&\n\t\t\tnotes.every((id) => roles.get(id) === 'user') &&\n\t\t\tfailures.every((id) => roles.get(id) === 'tool')\n\t\t)\n\t})\n}\n","import type { AgentInterface, AgentResult } from '../agents/types.js'\nimport type { Selection } from '../contexts/types.js'\nimport type { ConversationInterface } from '../conversations/types.js'\nimport type { ProviderInterface } from '../providers/types.js'\nimport type { Message, ThinkingReplay } from '../types.js'\nimport type {\n\tClassifierResult,\n\tLedgerAmendment,\n\tLedgerCategory,\n\tLedgerGauge,\n\tLedgerInterface,\n\tLedgerLine,\n\tLedgerLookupFinding,\n\tLedgerOptions,\n\tLedgerProjection,\n\tLedgerProjectionInput,\n\tLedgerRecord,\n\tLedgerRenderContext,\n\tLedgerResult,\n\tLedgerShare,\n\tLedgerSnapshot,\n} from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport type { ToolCall, ToolContext, ToolResult } from '@orkestrel/tool'\nimport { createAbort } from '@orkestrel/abort'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tisError,\n\tisFiniteNumber,\n\tisString,\n\tobjectShape,\n\tstringShape,\n} from '@orkestrel/contract'\nimport { createTool, createToolManager } from '@orkestrel/tool'\nimport { createAgent, createAuthority } from '../agents/factories.js'\nimport { AgentError } from '../agents/errors.js'\nimport { estimateMessages } from '../agents/helpers.js'\nimport { createScope } from '../contexts/factories.js'\nimport { createConversationManager } from '../conversations/factories.js'\nimport {\n\tcollectExchanges,\n\tcollectToolGroups,\n\textractLiterals,\n\thasCalls,\n\tstripThinking,\n\tsumUsage,\n} from '../helpers.js'\nimport { isNonNegativeSafeInteger, isPositiveSafeInteger } from '../validators.js'\nimport { Classifier } from './Classifier.js'\nimport { Gauge } from './Gauge.js'\nimport { LedgerRequest } from './LedgerRequest.js'\nimport { LedgerScan } from './LedgerScan.js'\nimport {\n\tDEFAULT_LEDGER_LIMIT,\n\tDEFAULT_LEDGER_SHARE,\n\tLEDGER_NOTES,\n\tLEDGER_OWNER_PREFIX,\n\tLEDGER_RULES_KEY,\n\tLEDGER_SCALE_DRIFT,\n} from './constants.js'\nimport { isLedgerError, LedgerError } from './errors.js'\nimport {\n\tbuildLedgerCut,\n\tbuildLedgerIndex,\n\tbuildLines,\n\tbuildLedgerRenderContext,\n\tbuildRecallMessage,\n\tbuildRequestMessage,\n\tcollectProjectionIds,\n\textractWords,\n\tfindLedgerCut,\n\trenderTopicNames,\n\tscanAmendments,\n\tsplitWords,\n\tbuildRecords,\n\tcollectNames,\n\tcollectRegistry,\n\tcutListing,\n\tlinkOwners,\n\tmatchEntities,\n\trankLedgerCut,\n\tresolvePredict,\n\tfindLedgerCall,\n\tmatchesCutLine,\n\trenderLedgerAnnotation,\n\trenderLedgerPinned,\n\trenderLedgerRecord,\n\trenderLookupCall,\n\trenderStub,\n\tselectRecords,\n\tsplitResultLines,\n\tsplitTopic,\n\tsplitSentences,\n} from './helpers.js'\nimport { ledgerSnapshotContract } from './contracts.js'\nimport { isFraction, isLedgerQuestion, isLedgerSnapshot } from './validators.js'\n\n/**\n * Serves one conversation through classified records, a bounded copy of each request turn that carries them, and a final answer pass.\n * @example\n * ```ts\n * const ledger = new Ledger(provider, options)\n * ledger.conversation.add({ role: 'user', content: 'The refund needs approval.' })\n * const reply = await ledger.respond('What approval is needed?')\n * ```\n */\nexport class Ledger implements LedgerInterface {\n\treadonly #provider: ProviderInterface\n\treadonly #options: LedgerOptions\n\treadonly #replay: ThinkingReplay\n\treadonly #predict: number\n\treadonly #share: LedgerShare\n\treadonly #conversation: ConversationInterface\n\treadonly #agent: AgentInterface\n\treadonly #classifier: Classifier\n\treadonly #requests = new Set<string>()\n\treadonly #annotations = new Set<string>()\n\treadonly #results = new Map<string, ToolResult>()\n\treadonly #pending = new Map<number, ToolResult>()\n\treadonly #live = new Set<LedgerRequest>()\n\treadonly #scans = new Set<LedgerScan>()\n\treadonly #calibrations = new Set<AbortSignal>()\n\t#gauge: Gauge | undefined\n\n\t/**\n\t * Composes the conversation, classifier, tools, and agent.\n\t * @param provider - The provider that serves the conversation\n\t * @param options - The filing policy, capacity, and request bounds\n\t * @throws {LedgerError} Thrown when a threshold, share, capacity, limit, topic, lookup, or gauge is invalid\n\t * @throws {LedgerError} Thrown when `predict` is not a nonnegative safe integer less than `capacity` (code `'CAPACITY'`)\n\t * @throws {LedgerError} Thrown when `questions` fails `isLedgerQuestion` (code `'QUESTION'`)\n\t * @throws {LedgerError} Thrown when `snapshot` is malformed or marks an id its conversation does not hold (code `'SNAPSHOT'`, its faults under `context.faults`)\n\t * @throws {ConversationError} Thrown when `isLedgerSnapshot` admits `snapshot` but its conversation holds a `system` message (code `'ROLE'`) or repeats an id (code `'SNAPSHOT'`)\n\t */\n\tconstructor(provider: ProviderInterface, options: LedgerOptions) {\n\t\tfor (const key of ['category', 'topic', 'correction', 'amends', 'supersedes'] as const) {\n\t\t\tconst value = options.thresholds[key]\n\t\t\tif (!isFraction(value))\n\t\t\t\tthrow new LedgerError('THRESHOLD', 'thresholds must be finite and in (0, 1]')\n\t\t}\n\t\tfor (const value of Object.values(options.share ?? {})) {\n\t\t\tif (!isFraction(value)) throw new LedgerError('SHARE', 'shares must be finite and in (0, 1]')\n\t\t}\n\t\tif (!isPositiveSafeInteger(options.capacity))\n\t\t\tthrow new LedgerError('CAPACITY', 'capacity must be a positive safe integer')\n\t\tthis.#predict = resolvePredict(options.predict, options.capacity)\n\t\tif (options.recall?.limit !== undefined && !isNonNegativeSafeInteger(options.recall.limit))\n\t\t\tthrow new LedgerError('LIMIT', 'a recall limit must be a nonnegative safe integer')\n\t\tif (options.agent?.limit !== undefined && !isPositiveSafeInteger(options.agent.limit))\n\t\t\tthrow new LedgerError('LIMIT', 'an agent limit must be a positive safe integer')\n\t\t// `Array.from` visits a hole in either list as an undefined entry, as iteration does, so the\n\t\t// read of its `name` refuses it; `map` skips it.\n\t\tconst topics = Array.from(options.topics, (topic) => topic.name)\n\t\tif (topics.some((name) => name.trim() === '') || new Set(topics).size !== topics.length)\n\t\t\tthrow new LedgerError('TOPIC', 'topic names must be nonempty and unique')\n\t\tconst lookups = ['recall', ...Array.from(options.lookups ?? [], (lookup) => lookup.tool.name)]\n\t\tif (new Set(lookups).size !== lookups.length)\n\t\t\tthrow new LedgerError('LOOKUP', 'lookup names must be unique and cannot be recall')\n\t\tif (!isLedgerQuestion(options.questions))\n\t\t\tthrow new LedgerError(\n\t\t\t\t'QUESTION',\n\t\t\t\t'questions must hold a choice category question whose criteria describe every category, a non-empty topic string, and noul amends and supersedes questions; pass the LEDGER_QUESTIONS constant for the measured wording',\n\t\t\t)\n\t\tconst snapshot = options.snapshot\n\t\tif (snapshot !== undefined && !isLedgerSnapshot(snapshot)) {\n\t\t\tconst faults = ledgerSnapshotContract.audit(snapshot)\n\t\t\tthrow new LedgerError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`snapshot must be well formed and mark only messages of its conversation; pass what snapshot() returned${faults.length === 0 ? '' : `\\n${describeFaults(faults, ledgerSnapshotContract.schema)}`}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tthis.#provider = provider\n\t\tthis.#replay = provider.replay ?? 'none'\n\t\tthis.#options = options\n\t\tthis.#share = { ...DEFAULT_LEDGER_SHARE, ...options.share }\n\t\tconst gauge = options.gauge ?? snapshot?.gauge\n\t\tif (gauge !== undefined)\n\t\t\tthis.#gauge = new Gauge({\n\t\t\t\t...gauge,\n\t\t\t\tcapacity: options.capacity,\n\t\t\t\tpredict: this.#predict,\n\t\t\t\treplay: this.#replay,\n\t\t\t})\n\t\tconst conversations = createConversationManager()\n\t\tthis.#conversation =\n\t\t\tsnapshot === undefined\n\t\t\t\t? conversations.add()\n\t\t\t\t: conversations.add({ snapshot: snapshot.conversation })\n\t\tconversations.switch(this.#conversation.id)\n\t\tif (snapshot !== undefined) this.#seat(snapshot)\n\t\tthis.#classifier = new Classifier({\n\t\t\tconversation: this.#conversation,\n\t\t\tjudge: options.judge,\n\t\t\tquestions: options.questions,\n\t\t\tthresholds: options.thresholds,\n\t\t\ttopics: options.topics,\n\t\t\tassign: this.#assign.bind(this),\n\t\t\tentities: (text, partial) => {\n\t\t\t\t// The held scan's index serves its selection or recall; without one, this call reads the lookups.\n\t\t\t\tconst [scan] = this.#scans\n\t\t\t\treturn matchEntities(\n\t\t\t\t\tscan?.index ??\n\t\t\t\t\t\tbuildLedgerIndex(collectRegistry(this.#readLookups(this.#conversation.messages()))),\n\t\t\t\t\ttext,\n\t\t\t\t\tpartial,\n\t\t\t\t)\n\t\t\t},\n\t\t\t...(options.correction === undefined ? {} : { correction: options.correction }),\n\t\t\t...(options.agent?.scheduler === undefined ? {} : { scheduler: options.agent.scheduler }),\n\t\t\t...(options.agent?.strict === undefined ? {} : { strict: options.agent.strict }),\n\t\t})\n\t\tconst tools = createToolManager()\n\t\tfor (const lookup of options.lookups ?? []) {\n\t\t\ttools.add(\n\t\t\t\tcreateTool({\n\t\t\t\t\tname: lookup.tool.name,\n\t\t\t\t\t...(lookup.tool.parameters === undefined ? {} : { parameters: lookup.tool.parameters }),\n\t\t\t\t\t...(lookup.tool.description === undefined\n\t\t\t\t\t\t? {}\n\t\t\t\t\t\t: { description: lookup.tool.description }),\n\t\t\t\t\t...(lookup.tool.summary === undefined ? {} : { summary: lookup.tool.summary }),\n\t\t\t\t\t...(lookup.tool.title === undefined ? {} : { title: lookup.tool.title }),\n\t\t\t\t\t...(lookup.tool.annotations === undefined\n\t\t\t\t\t\t? {}\n\t\t\t\t\t\t: { annotations: lookup.tool.annotations }),\n\t\t\t\t\texecute: (args, context) => this.#lookup(lookup.tool.name, args, context),\n\t\t\t\t}),\n\t\t\t)\n\t\t}\n\t\ttools.add(\n\t\t\tcreateTool({\n\t\t\t\tname: 'recall',\n\t\t\t\tdescription: `Recall what the full conversation record holds on a topic: an owner name, an id, or one of the topics ${renderTopicNames(options.topics)}. Returns source lines, newest first.`,\n\t\t\t\tcontract: objectShape({\n\t\t\t\t\ttopic: stringShape({ min: 1, description: 'An owner name, an id, or a topic' }),\n\t\t\t\t}),\n\t\t\t\texecute: (args) => this.#recall(args),\n\t\t\t}),\n\t\t)\n\t\t// The ledger holds no approver, and it allows exactly the names it advertises, so a tool a host\n\t\t// adds to the agent's manager later is denied instead of inheriting those names' trust.\n\t\tthis.#agent = createAgent(provider, {\n\t\t\t...options.agent,\n\t\t\tconversations,\n\t\t\tsystem: options.system,\n\t\t\ttools,\n\t\t\tauthority: createAuthority({\n\t\t\t\trules: [\n\t\t\t\t\t{\n\t\t\t\t\t\tmatch: (context) => lookups.includes(context.call.name),\n\t\t\t\t\t\tzone: 'ledger',\n\t\t\t\t\t\tverdict: 'allow',\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\tfallback: { zone: 'ledger', verdict: 'deny', reason: 'the ledger advertises no such tool' },\n\t\t\t}),\n\t\t\tlimit: options.agent?.limit ?? DEFAULT_LEDGER_LIMIT,\n\t\t\tselect: (conversation, request, signal) => this.#select(conversation, request, signal),\n\t\t})\n\t\tthis.#agent.emitter.on('tool', (_call, result) => {\n\t\t\tthis.#flush()\n\t\t\tthis.#pending.set(this.#conversation.messages().length, result)\n\t\t\tif (!result.success && result.error === LEDGER_NOTES.repeat) {\n\t\t\t\tthis.#current()?.close()\n\t\t\t\tthis.#agent.abort('repeat')\n\t\t\t}\n\t\t})\n\t\tthis.#agent.emitter.on('select', (selection) => {\n\t\t\tif (isLedgerError(selection.fault) && selection.fault.code === 'REQUEST') return\n\t\t\tthis.#current()?.adopt(selection, this.#conversation.messages().length)\n\t\t})\n\t\tthis.#agent.emitter.on('fault', (error) => this.#current()?.fault(error))\n\t\tthis.#agent.emitter.on('turn', () => this.#observeTurn())\n\t\tthis.#agent.emitter.on('usage', (usage) => this.#current()?.spend(usage))\n\t}\n\n\t/**\n\t * Returns the owned request engine.\n\t * @returns The engine serving this conversation\n\t */\n\tget agent(): AgentInterface {\n\t\treturn this.#agent\n\t}\n\t/**\n\t * Returns the owned message history.\n\t * @returns The history filed and served by this ledger\n\t */\n\tget conversation(): ConversationInterface {\n\t\treturn this.#conversation\n\t}\n\t/**\n\t * Returns the stored prompt price.\n\t * @returns The scale and overhead cost, or undefined before calibration\n\t */\n\tget gauge(): LedgerGauge | undefined {\n\t\treturn this.#gauge === undefined\n\t\t\t? undefined\n\t\t\t: { scale: this.#gauge.scale, overhead: this.#gauge.overhead }\n\t}\n\n\t/**\n\t * Measures message scale and tool overhead from provider prompt usage.\n\t * @param signal - The signal bounding both calibration calls; an abort rejects with its reason\n\t * @returns The stored scale and overhead cost\n\t * @throws {LedgerError} Thrown when either call reports no prompt usage, or a prompt usage of 0 or less (code `'GAUGE'`)\n\t * @throws {AgentError} Thrown when a request or calibration is active (code `'CONCURRENCY'`)\n\t */\n\tasync calibrate(signal: AbortSignal): Promise<LedgerGauge> {\n\t\tif (this.#busy()) throw new AgentError('CONCURRENCY', 'a ledger request is already active')\n\t\tthis.#calibrations.add(signal)\n\t\ttry {\n\t\t\treturn await this.#measureGauge(signal)\n\t\t} finally {\n\t\t\tthis.#calibrations.delete(signal)\n\t\t}\n\t}\n\n\t/**\n\t * Serializes the conversation, the marked ids, and the gauge into a snapshot a later ledger hydrates from.\n\t * @returns The snapshot, each id list in conversation order and `gauge` present after the ledger holds one\n\t * @throws {AgentError} Thrown when a request or calibration is active (code `'CONCURRENCY'`)\n\t */\n\tsnapshot(): LedgerSnapshot {\n\t\tif (this.#busy()) throw new AgentError('CONCURRENCY', 'a ledger request is already active')\n\t\tthis.#flush()\n\t\tconst conversation = this.#conversation.snapshot()\n\t\tconst ids = conversation.messages.map((message) => message.id)\n\t\tconst gauge = this.gauge\n\t\treturn {\n\t\t\tconversation,\n\t\t\trequests: ids.filter((id) => this.#requests.has(id)),\n\t\t\tnotes: ids.filter((id) => this.#annotations.has(id)),\n\t\t\tfailures: ids.filter((id) => this.#results.get(id)?.success === false),\n\t\t\t...(gauge === undefined ? {} : { gauge }),\n\t\t}\n\t}\n\n\t/**\n\t * Appends a request and serves it, recovering an unfinished or rejected first pass with one answer pass.\n\t * A calibration call that reports no prompt usage, or a prompt usage of 0 or less, rejects with `LedgerError` code `'GAUGE'`; a provider error during calibration rejects with that error.\n\t * When the answer pass rejects, `respond` rejects with that error, and under the default `strict` a judge failure or another selection failure rejects it too. A rejection belongs to its own call: the ledger stays free, and the next call asks the judge again about whatever is still unanswered.\n\t * @param content - The request text\n\t * @param signal - The caller's cancellation signal; an abort during calibration rejects with its reason\n\t * @returns The final pass, the usage of every pass, and the filing attempts of this request alone\n\t * @throws {AgentError} Thrown when a request or calibration is active (code `'CONCURRENCY'`)\n\t * @throws {LedgerError} Thrown when a calibration call reports no prompt usage, or a prompt usage of 0 or less (code `'GAUGE'`)\n\t * @throws {ContractError} Thrown when `signal` is defined and is not a native `AbortSignal`\n\t */\n\tasync respond(content: string, signal?: AbortSignal): Promise<LedgerResult> {\n\t\tif (this.#busy()) throw new AgentError('CONCURRENCY', 'a ledger request is already active')\n\t\tconst abort = createAbort({ ...(signal === undefined ? {} : { signal }) })\n\t\tconst caller = abort.signal\n\t\tconst request = new LedgerRequest(content, this.#options.recall?.limit)\n\t\tthis.#live.add(request)\n\t\ttry {\n\t\t\tif (this.#gauge === undefined) await this.#measureGauge(caller)\n\t\t\tconst entered = request.enter(this.#conversation)\n\t\t\tthis.#requests.add(entered.id)\n\t\t\t// The answer pass recovers a provider failure in the first pass; the agent's error event carries\n\t\t\t// its cause. A selection failure that reached here rejected under `strict`, and it stands.\n\t\t\tconst first = await this.#generatePass(request, caller, this.#options.think).catch(\n\t\t\t\t(error: unknown): AgentResult => {\n\t\t\t\t\tif (request.faulted(error)) throw error\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: '',\n\t\t\t\t\t\tpartial: true,\n\t\t\t\t\t\t...(request.usage === undefined ? {} : { usage: request.usage }),\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t)\n\t\t\tconst passes = [first]\n\t\t\tif (!caller.aborted && (first.partial || first.content.trim() === '')) {\n\t\t\t\tconst digest = this.#buildDigest(request, entered)\n\t\t\t\tif (digest !== undefined)\n\t\t\t\t\tthis.#annotations.add(this.#conversation.add({ role: 'user', content: digest }).id)\n\t\t\t\tthis.#annotations.add(\n\t\t\t\t\tthis.#conversation.add({ role: 'user', content: LEDGER_NOTES.cue }).id,\n\t\t\t\t)\n\t\t\t\tconst previous = this.#agent.context.scope\n\t\t\t\tthis.#agent.context.apply(\n\t\t\t\t\tcreateScope({\n\t\t\t\t\t\tname: 'answer',\n\t\t\t\t\t\ttools: [],\n\t\t\t\t\t\tselect: async (conversation, message, runSignal) => {\n\t\t\t\t\t\t\tconst selection = await this.#select(conversation, message, runSignal)\n\t\t\t\t\t\t\tif (selection.fault !== undefined) return selection\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t...selection,\n\t\t\t\t\t\t\t\tmessages: selection.messages.filter((one) => one.role !== 'tool' && !hasCalls(one)),\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t\t\ttry {\n\t\t\t\t\tpasses.push(await this.#generatePass(request, caller, false))\n\t\t\t\t} finally {\n\t\t\t\t\tthis.#agent.context.apply(previous)\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst last = passes.at(-1) ?? first\n\t\t\trequest.think(this.#conversation.messages())\n\t\t\tconst calls = request.calls\n\t\t\tthis.#gauge?.observe(\n\t\t\t\tcalls,\n\t\t\t\t!last.partial && last.content.trim() !== '' ? calls.at(-1) : undefined,\n\t\t\t)\n\t\t\tconst usage = passes.reduce<TokenUsage | undefined>(\n\t\t\t\t(total, pass) => (pass.usage === undefined ? total : sumUsage(total, pass.usage)),\n\t\t\t\tundefined,\n\t\t\t)\n\t\t\t// Joined as a `joinThinking` fold would join them: an empty part adds no separator.\n\t\t\tconst parts = passes.flatMap((pass) => (pass.thinking === undefined ? [] : [pass.thinking]))\n\t\t\treturn {\n\t\t\t\tcontent: last.content,\n\t\t\t\tpartial: last.partial,\n\t\t\t\tpasses,\n\t\t\t\tattempts: request.attempts,\n\t\t\t\t...(usage === undefined ? {} : { usage }),\n\t\t\t\t...(parts.length === 0\n\t\t\t\t\t? {}\n\t\t\t\t\t: { thinking: parts.filter((part) => part.length > 0).join('\\n\\n') }),\n\t\t\t\t...(last.stop === undefined ? {} : { stop: last.stop }),\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.#flush()\n\t\t\tthis.#live.delete(request)\n\t\t}\n\t}\n\n\tasync #measureGauge(signal: AbortSignal): Promise<LedgerGauge> {\n\t\ttry {\n\t\t\t// The empty turn stands where the request will, so a workspace's images have a user turn to\n\t\t\t// read, and it leaves with them: calibration prices text.\n\t\t\tconst messages = stripThinking(\n\t\t\t\tthis.#agent.context.build({\n\t\t\t\t\tmessages: [\n\t\t\t\t\t\t...this.#conversation.view(),\n\t\t\t\t\t\t{ id: 'calibration', role: 'user', content: '' },\n\t\t\t\t\t],\n\t\t\t\t\tjudgments: [],\n\t\t\t\t}),\n\t\t\t\tthis.#replay,\n\t\t\t).slice(0, -1)\n\t\t\tconst priced = await this.#provider.generate(\n\t\t\t\tmessages,\n\t\t\t\tsignal,\n\t\t\t\tthis.#agent.context.tools.definitions(),\n\t\t\t\t{ think: false },\n\t\t\t)\n\t\t\tconst bare = await this.#provider.generate(messages, signal, undefined, { think: false })\n\t\t\tif (\n\t\t\t\t!isFiniteNumber(priced.usage?.prompt) ||\n\t\t\t\tpriced.usage.prompt <= 0 ||\n\t\t\t\t!isFiniteNumber(bare.usage?.prompt) ||\n\t\t\t\tbare.usage.prompt <= 0\n\t\t\t)\n\t\t\t\tthrow new LedgerError('GAUGE', 'calibration requires prompt usage from both calls')\n\t\t\tthis.#gauge = new Gauge({\n\t\t\t\tcapacity: this.#options.capacity,\n\t\t\t\tpredict: this.#predict,\n\t\t\t\treplay: this.#replay,\n\t\t\t\tscale: bare.usage.prompt / estimateMessages(messages),\n\t\t\t\toverhead: Math.max(0, priced.usage.prompt - bare.usage.prompt),\n\t\t\t})\n\t\t\treturn { scale: this.#gauge.scale, overhead: this.#gauge.overhead }\n\t\t} catch (error) {\n\t\t\tsignal.throwIfAborted()\n\t\t\tthrow error\n\t\t}\n\t}\n\n\tasync #generatePass(\n\t\trequest: LedgerRequest,\n\t\tsignal: AbortSignal,\n\t\tthink?: boolean,\n\t): Promise<AgentResult> {\n\t\trequest.open(this.#conversation.messages().length)\n\t\treturn await this.#agent.generate({\n\t\t\tsignal,\n\t\t\tconversation: this.#conversation,\n\t\t\t...(think === undefined ? {} : { think }),\n\t\t})\n\t}\n\n\t// The live `respond` call's request after it has entered its message; a run during the call's\n\t// calibration reaches none. A refusal on every add caps the registry at one request.\n\t#current(): LedgerRequest | undefined {\n\t\tconst [request] = this.#live\n\t\treturn request?.message === undefined ? undefined : request\n\t}\n\n\t#busy(): boolean {\n\t\treturn this.#live.size > 0 || this.#calibrations.size > 0\n\t}\n\n\t#flush(): void {\n\t\t// Filing calls this once per message, and reading the messages copies them all.\n\t\tif (this.#pending.size === 0) return\n\t\tconst messages = this.#conversation.messages()\n\t\tfor (const [at, result] of this.#pending) {\n\t\t\tconst message = messages[at]\n\t\t\tif (message?.role === 'tool') {\n\t\t\t\tthis.#results.set(message.id, result)\n\t\t\t\tthis.#pending.delete(at)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Seats the hydrated marks. A failure is rebuilt as the result the agent's `tool` event carried:\n\t// its error is the tool message's content, and its name is the call's, or `tool` when no call\n\t// pairs with it, as a tail stub names it.\n\t#seat(snapshot: LedgerSnapshot): void {\n\t\tfor (const id of snapshot.requests) this.#requests.add(id)\n\t\tfor (const id of snapshot.notes) this.#annotations.add(id)\n\t\tconst messages = this.#conversation.messages()\n\t\tconst groups = collectToolGroups(messages)\n\t\tfor (const message of messages) {\n\t\t\tif (!snapshot.failures.includes(message.id)) continue\n\t\t\tconst group = groups.find((entries) => entries.some((one) => one.id === message.id))\n\t\t\tconst call = group === undefined ? undefined : findLedgerCall(group, message)\n\t\t\tthis.#results.set(message.id, {\n\t\t\t\tsuccess: false,\n\t\t\t\tid: call?.id ?? message.call ?? message.id,\n\t\t\t\tname: call?.name ?? 'tool',\n\t\t\t\terror: message.content,\n\t\t\t})\n\t\t}\n\t}\n\n\t// A held scan already read every lookup, caching each failed read, and holds the positions.\n\t#assign(message: Message): LedgerCategory | undefined {\n\t\tthis.#flush()\n\t\tif (this.#annotations.has(message.id)) return 'chatter'\n\t\tconst [scan] = this.#scans\n\t\tif (message.role === 'tool') {\n\t\t\tif (scan === undefined) this.#readLookups(this.#conversation.messages())\n\t\t\treturn this.#results.get(message.id)?.success === false ? 'chatter' : 'fact'\n\t\t}\n\t\tif (message.role !== 'assistant') return undefined\n\t\tif (hasCalls(message)) return 'chatter'\n\t\tif (scan !== undefined) {\n\t\t\tconst at = scan.position(message.id)\n\t\t\treturn scan.first !== undefined && at !== undefined && at > scan.first ? 'chatter' : undefined\n\t\t}\n\t\tconst messages = this.#conversation.messages()\n\t\tconst first = messages.findIndex((one) => this.#requests.has(one.id))\n\t\treturn first >= 0 && messages.findIndex((one) => one.id === message.id) > first\n\t\t\t? 'chatter'\n\t\t\t: undefined\n\t}\n\n\t#readLookups(messages: readonly Message[]): readonly LedgerLookupFinding[] {\n\t\tthis.#flush()\n\t\treturn collectToolGroups(messages).flatMap((group) =>\n\t\t\tgroup.slice(1).flatMap((message): LedgerLookupFinding[] => {\n\t\t\t\tconst call = findLedgerCall(group, message)\n\t\t\t\tconst lookup = this.#options.lookups?.find((one) => one.tool.name === call?.name)\n\t\t\t\tif (\n\t\t\t\t\tcall === undefined ||\n\t\t\t\t\tlookup === undefined ||\n\t\t\t\t\tthis.#results.get(message.id)?.success === false\n\t\t\t\t)\n\t\t\t\t\treturn []\n\t\t\t\tconst read = attempt(() => lookup.read(call.arguments, message.content))\n\t\t\t\tif (!read.success) {\n\t\t\t\t\t// Caches the failed read, so no later reading calls its handler again.\n\t\t\t\t\tthis.#results.set(message.id, {\n\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\tid: call.id,\n\t\t\t\t\t\tname: call.name,\n\t\t\t\t\t\terror: isError(read.error) ? read.error.message : String(read.error),\n\t\t\t\t\t})\n\t\t\t\t\treturn []\n\t\t\t\t}\n\t\t\t\tconst result = read.value\n\t\t\t\treturn [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: message.id,\n\t\t\t\t\t\tname: call.name,\n\t\t\t\t\t\targuments: call.arguments,\n\t\t\t\t\t\ttext: message.content,\n\t\t\t\t\t\tresult:\n\t\t\t\t\t\t\tresult === undefined\n\t\t\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\t\t\t\tids: [\n\t\t\t\t\t\t\t\t\t\t\t...new Set([\n\t\t\t\t\t\t\t\t\t\t\t\t...result.ids,\n\t\t\t\t\t\t\t\t\t\t\t\t...Object.values(call.arguments)\n\t\t\t\t\t\t\t\t\t\t\t\t\t.filter(isString)\n\t\t\t\t\t\t\t\t\t\t\t\t\t.flatMap((value) => [...extractLiterals(value).ids]),\n\t\t\t\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t}),\n\t\t)\n\t}\n\n\t#project(scan: LedgerScan): {\n\t\treadonly input: LedgerProjectionInput\n\t\treadonly projection: LedgerProjection\n\t} {\n\t\tconst input: LedgerProjectionInput = {\n\t\t\tsystem: this.#options.system,\n\t\t\texclusions: [...this.#requests, ...this.#annotations],\n\t\t\towners: scan.registry.owners,\n\t\t\tmessages: scan.messages,\n\t\t\tfindings: scan.findings,\n\t\t\tentities: new Map(\n\t\t\t\tscan.messages.map((message) => [\n\t\t\t\t\tmessage.id,\n\t\t\t\t\t[...matchEntities(scan.index, message.content, true)],\n\t\t\t\t]),\n\t\t\t),\n\t\t\tclassification: this.#classifier.classification(),\n\t\t}\n\t\treturn { input, projection: buildRecords(input) }\n\t}\n\n\t#projectLines(input: LedgerProjectionInput, id: string) {\n\t\treturn buildLines(\n\t\t\tinput,\n\t\t\tnew Map(input.messages.map((message) => [message.id, message])),\n\t\t\tid,\n\t\t\t[...input.owners.values()].flat(),\n\t\t\tcollectNames(this.#options.system),\n\t\t)\n\t}\n\n\t#renderSource(\n\t\tid: string,\n\t\tsentences: readonly string[],\n\t\tlines: readonly LedgerLine[],\n\t\tcontext: LedgerRenderContext,\n\t\tscan: LedgerScan,\n\t): string {\n\t\tconst message = this.#conversation.message(id)\n\t\tif (message === undefined) return ''\n\t\tconst retained = new Set(lines.map((line) => line.sentence))\n\t\tconst text = sentences.every((_, index) => retained.has(index))\n\t\t\t? message.content\n\t\t\t: lines.map((line) => sentences[line.sentence] ?? '').join(' ')\n\t\tif (text === '') return ''\n\t\tconst finding = scan.finding(id)\n\t\treturn [\n\t\t\t...lines.map((line) => renderLedgerAnnotation(line, context)),\n\t\t\tmessage.role !== 'tool' || finding === undefined\n\t\t\t\t? text\n\t\t\t\t: `${renderLookupCall(finding.name, finding.arguments)}: ${text}`,\n\t\t]\n\t\t\t.filter((part) => part !== '')\n\t\t\t.join('\\n')\n\t}\n\n\t#collectAfter(request: Message): readonly Message[] {\n\t\tconst messages = this.#conversation.messages()\n\t\tconst start = messages.findIndex((message) => message.id === request.id)\n\t\treturn messages\n\t\t\t.slice(start + 1)\n\t\t\t.filter(\n\t\t\t\t(message) =>\n\t\t\t\t\tmessage.role !== 'assistant' || hasCalls(message) || message.content.trim() !== '',\n\t\t\t)\n\t}\n\n\t// A run owns a selection only on the ledger's conversation, by identity, and only for the active\n\t// call's request or a ledger note; any other run is refused over its own conversation's view, so\n\t// the refusal never carries the ledger's history to another thread.\n\tasync #select(\n\t\tconversation: ConversationInterface,\n\t\tmessage: Message,\n\t\tsignal: AbortSignal,\n\t): Promise<Selection> {\n\t\tconst request = this.#current()\n\t\tconst selected = request?.message\n\t\tif (\n\t\t\tconversation !== this.#conversation ||\n\t\t\trequest === undefined ||\n\t\t\tselected === undefined ||\n\t\t\t(message.id !== selected.id && !this.#annotations.has(message.id))\n\t\t)\n\t\t\treturn this.#refuse(\n\t\t\t\tnew LedgerError(\n\t\t\t\t\t'REQUEST',\n\t\t\t\t\t'ledger selection requires a request owned by an active respond call',\n\t\t\t\t),\n\t\t\t\t{ judgments: [] },\n\t\t\t\tconversation,\n\t\t\t)\n\t\tconst plan = request.plan\n\t\tif (this.#annotations.has(message.id) && plan !== undefined)\n\t\t\treturn { messages: [...plan.messages, ...this.#collectAfter(selected)], judgments: [] }\n\t\t// A scan that throws holds no entry, so the handlers and the plan read the conversation and\n\t\t// the lookups themselves and throw where the classification and the plan catch it.\n\t\tconst scanned = attempt(() => this.#scan(this.#conversation.messages()))\n\t\tif (scanned.success) this.#hold(scanned.value)\n\t\ttry {\n\t\t\treturn await this.#classifier.classify(this.#requests, signal).then(\n\t\t\t\t(classified) => this.#file(classified, request, selected, message),\n\t\t\t\t(error: unknown) => this.#refuse(error, { judgments: [] }, this.#conversation),\n\t\t\t)\n\t\t} finally {\n\t\t\tconst [scan] = this.#scans\n\t\t\tif (scan !== undefined) this.#scans.delete(scan)\n\t\t}\n\t}\n\n\t// Reads each lookup result of the messages once and holds the values a selection or a recall reads.\n\t#scan(messages: readonly Message[]): LedgerScan {\n\t\treturn new LedgerScan(messages, this.#readLookups(messages), this.#requests)\n\t}\n\n\t// Adds a scan to the registry, which a refusal on every add caps at one entry.\n\t#hold(scan: LedgerScan): void {\n\t\tif (this.#scans.size > 0) throw new AgentError('CONCURRENCY', 'a ledger scan is already held')\n\t\tthis.#scans.add(scan)\n\t}\n\n\t// Returns the held scan while the conversation keeps its message ids in order; otherwise holds,\n\t// in its place, a scan of the conversation as it reads at this call, so one plan never mixes the two.\n\t#rescan(): LedgerScan {\n\t\tconst messages = this.#conversation.messages()\n\t\tconst [held] = this.#scans\n\t\tif (\n\t\t\theld !== undefined &&\n\t\t\theld.messages.length === messages.length &&\n\t\t\tmessages.every((one, at) => held.messages[at]?.id === one.id)\n\t\t)\n\t\t\treturn held\n\t\tconst scan = this.#scan(messages)\n\t\tif (held !== undefined) this.#scans.delete(held)\n\t\tthis.#hold(scan)\n\t\treturn scan\n\t}\n\n\t// Files a classification's attempts into the request and plans the selection, falling back to\n\t// the conversation view when the filing faulted or planning threw.\n\t#file(\n\t\tclassified: ClassifierResult,\n\t\trequest: LedgerRequest,\n\t\tselected: Message,\n\t\tmessage: Message,\n\t): Selection {\n\t\tconst { attempts, failure, ...filed } = classified\n\t\tconst planned = attempt(() => {\n\t\t\tif (filed.fault !== undefined) throw filed.fault\n\t\t\treturn {\n\t\t\t\t...this.#plan(selected, this.#rescan()),\n\t\t\t\t...filed,\n\t\t\t\t// The filing went on past a judge failure, so the plan stands beside it for a lenient run.\n\t\t\t\t...(failure === undefined ? {} : { fault: failure }),\n\t\t\t}\n\t\t})\n\t\trequest.file(attempts, planned.success ? planned.value : undefined)\n\t\tif (!planned.success) return this.#refuse(planned.error, filed, this.#conversation)\n\t\treturn this.#annotations.has(message.id)\n\t\t\t? {\n\t\t\t\t\t...planned.value,\n\t\t\t\t\tmessages: [...planned.value.messages, ...this.#collectAfter(selected)],\n\t\t\t\t}\n\t\t\t: planned.value\n\t}\n\n\t// Falls back to the given conversation's view with the failure as the selection's fault, keeping\n\t// the filing completed before it.\n\t#refuse(\n\t\terror: unknown,\n\t\tfiled: Omit<ClassifierResult, 'attempts'>,\n\t\tconversation: ConversationInterface,\n\t): Selection {\n\t\treturn {\n\t\t\t...filed,\n\t\t\tmessages: conversation.view(),\n\t\t\tfault: isError(error) ? error : new Error(String(error)),\n\t\t}\n\t}\n\n\t#price(messages: readonly Message[]): number {\n\t\treturn (this.#gauge?.scale ?? 1) * estimateMessages(messages)\n\t}\n\n\t#ruled(source: string, role: Message['role']): boolean {\n\t\tconst category = this.#classifier.category(source)\n\t\tconst unsettled = role === 'user' && !this.#classifier.decisive(source)\n\t\treturn !unsettled && (category === 'rule' || category === 'correction')\n\t}\n\n\t#plan(request: Message, scan: LedgerScan): Pick<Selection, 'messages'> {\n\t\tconst { input, projection } = this.#project(scan)\n\t\tconst registry = scan.registry\n\t\tconst links = linkOwners(input.findings, registry.owners)\n\t\tconst near = new Set([\n\t\t\t...matchEntities(scan.index, request.content, true),\n\t\t\t...this.#classifier.topics(request.id),\n\t\t])\n\t\tconst owners = [...near].flatMap((id) => {\n\t\t\tconst owner = registry.owners.has(id) ? id : links.get(id)\n\t\t\treturn owner === undefined ? [] : [owner]\n\t\t})\n\t\tconst records = selectRecords(projection, { owners, topics: [...near] })\n\t\tconst total =\n\t\t\tMath.max(\n\t\t\t\t0,\n\t\t\t\t(this.#options.capacity - this.#predict) * this.#share.prompt -\n\t\t\t\t\t(this.#gauge?.overhead ?? 0),\n\t\t\t) /\n\t\t\t(1 + LEDGER_SCALE_DRIFT)\n\t\tconst tail = this.#selectTail(request, total * this.#share.tail, input, scan)\n\t\tconst tailIds = new Set(tail.map((message) => message.id))\n\t\tconst scoped = records.some((record) => record.key !== LEDGER_RULES_KEY)\n\t\tconst held = new Set([\n\t\t\t...projection.records\n\t\t\t\t.filter((record) => scoped || record.key === LEDGER_RULES_KEY)\n\t\t\t\t.flatMap((record) => record.members),\n\t\t\t...(records.length > 0\n\t\t\t\t? projection.records.flatMap((record) =>\n\t\t\t\t\t\trecord.members.filter(\n\t\t\t\t\t\t\t(id) =>\n\t\t\t\t\t\t\t\tthis.#conversation.message(id)?.role === 'user' &&\n\t\t\t\t\t\t\t\tthis.#classifier.category(id) === 'rule' &&\n\t\t\t\t\t\t\t\tthis.#classifier.decisive(id),\n\t\t\t\t\t\t),\n\t\t\t\t\t)\n\t\t\t\t: []),\n\t\t])\n\t\tconst texts = input.messages\n\t\t\t.filter((message) => !hasCalls(message))\n\t\t\t.map((message) => ({\n\t\t\t\tmessage,\n\t\t\t\t...extractWords(message),\n\t\t\t}))\n\t\tconst asked = texts.find(({ message }) => message.id === request.id)\n\t\tconst counts = new Map(\n\t\t\t[\n\t\t\t\t...Map.groupBy(\n\t\t\t\t\ttexts.flatMap((text) => [...text.words]),\n\t\t\t\t\t(word) => word,\n\t\t\t\t),\n\t\t\t].map(([word, occurrences]) => [word, occurrences.length]),\n\t\t)\n\t\tconst live = collectProjectionIds(projection)\n\t\tconst units = texts.flatMap(({ message, words, names }, position) => {\n\t\t\tif (\n\t\t\t\t!live.has(message.id) ||\n\t\t\t\theld.has(message.id) ||\n\t\t\t\t(message.role !== 'tool' && tailIds.has(message.id))\n\t\t\t)\n\t\t\t\treturn []\n\t\t\tconst literals = extractLiterals(message.content)\n\t\t\tif (message.role === 'user' && literals.ids.size + literals.numbers.size + names.size === 0)\n\t\t\t\treturn []\n\t\t\tconst category = this.#classifier.category(message.id)\n\t\t\tconst unsettled = message.role === 'user' && !this.#classifier.decisive(message.id)\n\t\t\tconst topics = new Set([\n\t\t\t\t...(input.entities.get(message.id) ?? []),\n\t\t\t\t...this.#classifier.topics(message.id),\n\t\t\t])\n\t\t\tconst ruled = this.#ruled(message.id, message.role)\n\t\t\tconst group = [...topics].some((topic) => near.has(topic))\n\t\t\t\t? 1\n\t\t\t\t: ruled\n\t\t\t\t\t? 2\n\t\t\t\t\t: [...names].some((name) => asked?.names.has(name))\n\t\t\t\t\t\t? 3\n\t\t\t\t\t\t: 4\n\t\t\tif (group === 4) return []\n\t\t\tconst score = [...words]\n\t\t\t\t.filter((word) => asked?.words.has(word))\n\t\t\t\t.reduce((sum, word) => sum + Math.log(texts.length / (counts.get(word) ?? 1)), 0)\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: message.id,\n\t\t\t\t\tlines: this.#projectLines(input, message.id),\n\t\t\t\t\tposition,\n\t\t\t\t\tgroup,\n\t\t\t\t\truled,\n\t\t\t\t\tunsettled: unsettled,\n\t\t\t\t\tscore,\n\t\t\t\t\tcategory,\n\t\t\t\t\tcut: rankLedgerCut(group, unsettled, category),\n\t\t\t\t},\n\t\t\t]\n\t\t})\n\t\tconst ordered = [...units].sort(\n\t\t\t(left, right) =>\n\t\t\t\tleft.group - right.group ||\n\t\t\t\t(left.group === 1 ? Number(left.unsettled) - Number(right.unsettled) : 0) ||\n\t\t\t\t((left.group === 1 && left.unsettled) || left.group === 3 ? right.score - left.score : 0) ||\n\t\t\t\tleft.position - right.position,\n\t\t)\n\t\tconst cuts = [...ordered].sort(\n\t\t\t(left, right) =>\n\t\t\t\tleft.cut - right.cut ||\n\t\t\t\t(left.cut === 0 || left.cut === 1 || left.cut === 4 ? left.score - right.score : 0) ||\n\t\t\t\tright.position - left.position,\n\t\t)\n\t\tconst rules = records.find((record) => record.key === LEDGER_RULES_KEY)\n\t\tconst owned = records.filter((record) => record.key !== LEDGER_RULES_KEY)\n\t\tconst ruledSources = new Set(\n\t\t\t[...new Map(owned.flatMap((record) => record.lines).map((line) => [line.source, line.role]))]\n\t\t\t\t.filter(([source, role]) => this.#ruled(source, role))\n\t\t\t\t.map(([source]) => source),\n\t\t)\n\t\tconst steps = [\n\t\t\t...(rules?.lines.filter((line) => !line.topics.some((topic) => near.has(topic))).reverse() ??\n\t\t\t\t[]),\n\t\t\t...cuts.map((unit) => unit.source),\n\t\t\t...(rules?.lines.filter((line) => line.topics.some((topic) => near.has(topic))).reverse() ??\n\t\t\t\t[]),\n\t\t\t...owned\n\t\t\t\t.toReversed()\n\t\t\t\t.flatMap((record) => [\n\t\t\t\t\t...record.lines.filter((line) => !ruledSources.has(line.source)),\n\t\t\t\t\t...record.lines.filter((line) => ruledSources.has(line.source)),\n\t\t\t\t]),\n\t\t]\n\t\tconst mapped = ordered.map((unit) => ({\n\t\t\tsource: unit.source,\n\t\t\trecord: this.#buildUnitRecord(unit.source, unit.category, input, projection, held),\n\t\t}))\n\t\tconst trimmed = this.#trim(\n\t\t\tsteps,\n\t\t\trecords,\n\t\t\tmapped,\n\t\t\tnew Set(ordered.map((unit) => unit.source)),\n\t\t\trequest,\n\t\t\ttail,\n\t\t\ttotal,\n\t\t\tprojection.amendments,\n\t\t\tscan,\n\t\t)\n\t\tconst shown = new Set([\n\t\t\t...trimmed.included,\n\t\t\t...records.flatMap((record) =>\n\t\t\t\trecord.members.filter((source) => {\n\t\t\t\t\tconst full = record.lines.filter((line) => line.source === source)\n\t\t\t\t\tconst visible =\n\t\t\t\t\t\ttrimmed.records\n\t\t\t\t\t\t\t.find((one) => one.key === record.key)\n\t\t\t\t\t\t\t?.lines.filter((line) => line.source === source) ?? []\n\t\t\t\t\treturn full.length > 0 && full.length === visible.length\n\t\t\t\t}),\n\t\t\t),\n\t\t])\n\t\treturn {\n\t\t\tmessages: [\n\t\t\t\t...tail\n\t\t\t\t\t.slice(0, -1)\n\t\t\t\t\t.map((message) =>\n\t\t\t\t\t\tmessage.role === 'tool' ? this.#renderTailStub(message, scan, shown) : message,\n\t\t\t\t\t),\n\t\t\t\tbuildRequestMessage(request, trimmed.rendered),\n\t\t\t],\n\t\t}\n\t}\n\n\t// Searches the cuts in step order (each step drops the line it is, or the source it names) for one\n\t// whose built head, tail, and request copy fit `total`, and returns the winning probe's render; the\n\t// cut after every step is returned unchecked. A cut past the first fit fits too while the price does\n\t// not rise along the steps, so the search lands on the first fitting cut.\n\t#trim(\n\t\tsteps: ReadonlyArray<LedgerLine | string>,\n\t\trecords: readonly LedgerRecord[],\n\t\tmapped: ReadonlyArray<{ readonly source: string; readonly record: LedgerRecord }>,\n\t\tincluded: ReadonlySet<string>,\n\t\trequest: Message,\n\t\ttail: readonly Message[],\n\t\ttotal: number,\n\t\tamendments: readonly LedgerAmendment[],\n\t\tscan: LedgerScan,\n\t): {\n\t\treadonly rendered: string\n\t\treadonly records: readonly LedgerRecord[]\n\t\treadonly included: ReadonlySet<string>\n\t} {\n\t\tconst built = this.#agent.context.build({ messages: [request], judgments: [] })\n\t\tconst head = built.slice(0, -1)\n\t\tconst priced = built.at(-1) ?? request\n\t\tconst found = findLedgerCut(0, steps.length, (count) => {\n\t\t\tconst cut = buildLedgerCut(steps.slice(0, count), records, included)\n\t\t\tconst rendered = this.#render(cut.records, mapped, cut.included, amendments, scan)\n\t\t\tconst copy = buildRequestMessage(priced, rendered)\n\t\t\treturn this.#price([...head, ...stripThinking([...tail.slice(0, -1), copy], this.#replay)]) <=\n\t\t\t\ttotal\n\t\t\t\t? { ...cut, rendered }\n\t\t\t\t: undefined\n\t\t})\n\t\tif (found !== undefined) return found\n\t\tconst last = buildLedgerCut(steps, records, included)\n\t\treturn {\n\t\t\t...last,\n\t\t\trendered: this.#render(last.records, mapped, last.included, amendments, scan),\n\t\t}\n\t}\n\n\t#buildUnitRecord(\n\t\tsource: string,\n\t\tcategory: LedgerCategory | undefined,\n\t\tinput: LedgerProjectionInput,\n\t\tprojection: LedgerProjection,\n\t\theld: ReadonlySet<string>,\n\t): LedgerRecord {\n\t\treturn {\n\t\t\tkey: category === 'rule' ? LEDGER_RULES_KEY : source,\n\t\t\ttitle: '',\n\t\t\tmembers: [source],\n\t\t\tlines: this.#collectAmended(\n\t\t\t\tinput,\n\t\t\t\tprojection,\n\t\t\t\tsource,\n\t\t\t\tcategory === 'rule'\n\t\t\t\t\t? new Set(projection.records.flatMap((record) => record.members))\n\t\t\t\t\t: held,\n\t\t\t),\n\t\t}\n\t}\n\n\t#collectAmended(\n\t\tinput: LedgerProjectionInput,\n\t\tprojection: LedgerProjection,\n\t\tsource: string,\n\t\theld: ReadonlySet<string>,\n\t): readonly LedgerLine[] {\n\t\tconst live = collectProjectionIds(projection)\n\t\treturn scanAmendments(\n\t\t\tinput.classification.amendments,\n\t\t\tsource,\n\t\t\t(id) => id === source || (!held.has(id) && live.has(id)),\n\t\t).flatMap((id) => this.#projectLines(input, id))\n\t}\n\n\t#render(\n\t\trecords: readonly LedgerRecord[],\n\t\tmapped: ReadonlyArray<{ readonly source: string; readonly record: LedgerRecord }>,\n\t\tincluded: ReadonlySet<string>,\n\t\tamendments: readonly LedgerAmendment[],\n\t\tscan: LedgerScan,\n\t): string {\n\t\tconst entries = mapped\n\t\t\t.filter((unit) => included.has(unit.source))\n\t\t\t.flatMap(({ record: unit }) =>\n\t\t\t\t[...Map.groupBy(unit.lines, (line) => line.source)].map(([source, lines]) => ({\n\t\t\t\t\tunit,\n\t\t\t\t\tsource,\n\t\t\t\t\tlines,\n\t\t\t\t})),\n\t\t\t)\n\t\t// A source renders once, under the first unit that carries it.\n\t\tconst first = new Map(entries.toReversed().map((entry) => [entry.source, entry]))\n\t\tconst unique = entries.filter((entry) => first.get(entry.source) === entry)\n\t\tconst record = records.find(\n\t\t\t(candidate) => candidate.key === LEDGER_RULES_KEY && candidate.lines.length > 0,\n\t\t)\n\t\tconst ownerRecords = records.filter(\n\t\t\t(candidate) => candidate.key !== LEDGER_RULES_KEY && candidate.lines.length > 0,\n\t\t)\n\t\t// The context reads the lines in the order the records emit them, so each source's first\n\t\t// retained line carries its annotation and its reference.\n\t\tconst context = buildLedgerRenderContext(\n\t\t\tthis.#conversation.messages(),\n\t\t\t[\n\t\t\t\t...ownerRecords.flatMap((candidate) => candidate.lines),\n\t\t\t\t...unique\n\t\t\t\t\t.filter((entry) => entry.unit.key !== LEDGER_RULES_KEY)\n\t\t\t\t\t.flatMap((entry) => entry.lines),\n\t\t\t\t...(record?.lines ?? []),\n\t\t\t\t...unique\n\t\t\t\t\t.filter((entry) => entry.unit.key === LEDGER_RULES_KEY)\n\t\t\t\t\t.flatMap((entry) => entry.lines),\n\t\t\t],\n\t\t\tamendments,\n\t\t)\n\t\tconst owners = ownerRecords.map((owner) => renderLedgerPinned(owner, context)).join('\\n\\n')\n\t\tconst rendered = unique.map(({ unit, source, lines }) => {\n\t\t\tconst sentences = splitSentences(this.#conversation.message(source)?.content ?? '')\n\t\t\treturn {\n\t\t\t\trules: unit.key === LEDGER_RULES_KEY,\n\t\t\t\ttexts:\n\t\t\t\t\tunit.key === LEDGER_RULES_KEY && source === unit.members[0]\n\t\t\t\t\t\t? lines.map((line) => this.#renderSource(source, sentences, [line], context, scan))\n\t\t\t\t\t\t: [this.#renderSource(source, sentences, lines, context, scan)],\n\t\t\t}\n\t\t})\n\t\tconst pinned = rendered.filter((one) => !one.rules).flatMap((one) => one.texts)\n\t\tconst rules = rendered.filter((one) => one.rules).flatMap((one) => one.texts)\n\t\tconst body = [owners, pinned.join('\\n')].filter((text) => text !== '').join('\\n\\n')\n\t\treturn [\n\t\t\tbody === '' ? '' : `## Records\\n${body}`,\n\t\t\trecord === undefined\n\t\t\t\t? rules.length === 0\n\t\t\t\t\t? ''\n\t\t\t\t\t: ['## Rules', ...rules].join('\\n')\n\t\t\t\t: [renderLedgerRecord(record, context), ...rules].join('\\n'),\n\t\t]\n\t\t\t.filter((text) => text !== '')\n\t\t\t.join('\\n\\n')\n\t}\n\n\t#selectTail(\n\t\trequest: Message,\n\t\tcap: number,\n\t\tinput: LedgerProjectionInput,\n\t\tscan: LedgerScan,\n\t): readonly Message[] {\n\t\tconst first = input.messages.findIndex((message) => this.#requests.has(message.id))\n\t\tconst seed = input.messages.slice(\n\t\t\t0,\n\t\t\tfirst < 0 ? input.messages.findIndex((message) => message.id === request.id) : first,\n\t\t)\n\t\tconst groups = collectToolGroups(seed).flatMap((group) => {\n\t\t\tconst leader = group[0]\n\t\t\tif (leader === undefined) return []\n\t\t\tconst kept = (leader.calls ?? []).flatMap((call) => {\n\t\t\t\tconst message = group.slice(1).find((result) => findLedgerCall(group, result) === call)\n\t\t\t\tconst result = message === undefined ? undefined : this.#results.get(message.id)\n\t\t\t\treturn message === undefined ||\n\t\t\t\t\t!this.#options.lookups?.some((lookup) => lookup.tool.name === call.name) ||\n\t\t\t\t\t(result?.success === false && result.error === LEDGER_NOTES.repeat)\n\t\t\t\t\t? []\n\t\t\t\t\t: [{ call, message }]\n\t\t\t})\n\t\t\treturn [{ leader, kept }]\n\t\t})\n\t\tconst calls = new Map<string, readonly ToolCall[]>(\n\t\t\tgroups.map(({ leader, kept }) => [leader.id, kept.map(({ call }) => call)]),\n\t\t)\n\t\tconst results = new Set(groups.flatMap(({ kept }) => kept.map(({ message }) => message.id)))\n\t\tconst history = seed\n\t\t\t.filter(\n\t\t\t\t(message) =>\n\t\t\t\t\t!this.#annotations.has(message.id) &&\n\t\t\t\t\t(message.role !== 'tool' || results.has(message.id)) &&\n\t\t\t\t\t!(\n\t\t\t\t\t\tmessage.role === 'assistant' &&\n\t\t\t\t\t\thasCalls(message) &&\n\t\t\t\t\t\t(calls.get(message.id)?.length ?? 0) === 0 &&\n\t\t\t\t\t\tmessage.content === ''\n\t\t\t\t\t),\n\t\t\t)\n\t\t\t.map((message) =>\n\t\t\t\tmessage.role === 'tool'\n\t\t\t\t\t? this.#renderTailStub(message, scan)\n\t\t\t\t\t: calls.has(message.id)\n\t\t\t\t\t\t? (calls.get(message.id)?.length ?? 0) > 0\n\t\t\t\t\t\t\t? { ...message, calls: calls.get(message.id) ?? [] }\n\t\t\t\t\t\t\t: { id: message.id, role: message.role, content: message.content }\n\t\t\t\t\t\t: message,\n\t\t\t)\n\t\t// Newest exchange first: the tail keeps every exchange before the first whose addition passes\n\t\t// `cap`, and each candidate is the kept exchanges in conversation order, then the request.\n\t\tconst exchanges = collectExchanges(history).toReversed()\n\t\tconst stop = exchanges.findIndex(\n\t\t\t(_exchange, index) =>\n\t\t\t\tthis.#price(\n\t\t\t\t\tstripThinking(\n\t\t\t\t\t\t[\n\t\t\t\t\t\t\t...exchanges\n\t\t\t\t\t\t\t\t.slice(0, index + 1)\n\t\t\t\t\t\t\t\t.toReversed()\n\t\t\t\t\t\t\t\t.flat(),\n\t\t\t\t\t\t\trequest,\n\t\t\t\t\t\t],\n\t\t\t\t\t\tthis.#replay,\n\t\t\t\t\t),\n\t\t\t\t) > cap,\n\t\t)\n\t\treturn [\n\t\t\t...exchanges\n\t\t\t\t.slice(0, stop === -1 ? exchanges.length : stop)\n\t\t\t\t.toReversed()\n\t\t\t\t.flat(),\n\t\t\trequest,\n\t\t]\n\t}\n\n\t#renderTailStub(message: Message, scan: LedgerScan, shown?: ReadonlySet<string>): Message {\n\t\tconst group = scan.group(message.id)\n\t\tconst call = group === undefined ? undefined : findLedgerCall(group, message)\n\t\tconst finding = scan.finding(message.id)\n\t\tconst hidden = renderStub(call?.name ?? 'tool', call?.arguments ?? {}, 'hidden')\n\t\tconst visible = renderStub(call?.name ?? 'tool', call?.arguments ?? {}, 'shown')\n\t\treturn {\n\t\t\t...message,\n\t\t\tcontent: renderStub(\n\t\t\t\tcall?.name ?? 'tool',\n\t\t\t\tcall?.arguments ?? {},\n\t\t\t\tthis.#results.get(message.id)?.success === false\n\t\t\t\t\t? 'failed'\n\t\t\t\t\t: finding?.result === undefined\n\t\t\t\t\t\t? 'empty'\n\t\t\t\t\t\t: shown === undefined\n\t\t\t\t\t\t\t? hidden.length > visible.length\n\t\t\t\t\t\t\t\t? 'hidden'\n\t\t\t\t\t\t\t\t: 'shown'\n\t\t\t\t\t\t\t: shown.has(message.id)\n\t\t\t\t\t\t\t\t? 'shown'\n\t\t\t\t\t\t\t\t: 'hidden',\n\t\t\t),\n\t\t}\n\t}\n\n\t#observeTurn(): void {\n\t\tthis.#flush()\n\t\tconst request = this.#current()\n\t\tconst boundary = request?.boundary\n\t\tif (request === undefined || boundary === undefined) return\n\t\tconst additions = this.#conversation.messages().slice(boundary)\n\t\tconst messages = this.#agent.context.build(request.selection)\n\t\tconst names = this.#agent.context.scope?.tools\n\t\trequest.observe(\n\t\t\t{\n\t\t\t\testimate: estimateMessages(stripThinking([...messages, ...additions], this.#replay)),\n\t\t\t\ttools: this.#agent.context.tools\n\t\t\t\t\t.definitions()\n\t\t\t\t\t.filter((tool) => names === undefined || names.includes(tool.name)).length,\n\t\t\t},\n\t\t\tthis.#conversation.messages().length,\n\t\t)\n\t}\n\n\t#findLongest(): string {\n\t\treturn this.#conversation\n\t\t\t.messages()\n\t\t\t.filter((message) => message.role === 'assistant')\n\t\t\t.reduce(\n\t\t\t\t(longest, message) => (message.content.length > longest.length ? message.content : longest),\n\t\t\t\t'',\n\t\t\t)\n\t}\n\n\t#lookup(name: string, args: Readonly<Record<string, unknown>>, context: ToolContext): unknown {\n\t\tthis.#current()?.admit(name, args)\n\t\tconst lookup = this.#options.lookups?.find((one) => one.tool.name === name)\n\t\tif (lookup === undefined) throw new Error(`unknown lookup ${name}`)\n\t\treturn lookup.tool.execute(args, context)\n\t}\n\n\t#recall(args: Readonly<Record<string, unknown>>): string {\n\t\tconst request = this.#current()\n\t\trequest?.think(this.#conversation.messages())\n\t\tconst topic = isString(args.topic) ? args.topic.trim() : ''\n\t\trequest?.admit('recall', { topic })\n\t\tconst gauge = this.#gauge\n\t\tconst calls = request?.calls ?? []\n\t\tif (\n\t\t\tgauge !== undefined &&\n\t\t\tcalls.length > 0 &&\n\t\t\tgauge.remainder(calls) - this.#predict < 2 * gauge.reserve(calls, this.#findLongest())\n\t\t)\n\t\t\trequest?.close()\n\t\trequest?.recall()\n\t\tconst guidance = `an owner name, an id, or one of ${renderTopicNames(this.#options.topics)}`\n\t\tif (topic === '') throw new Error(`recall needs a topic: ${guidance}`)\n\t\tconst room =\n\t\t\t(gauge?.room(calls, this.#findLongest()) ?? 0) - estimateMessages([buildRecallMessage(topic)])\n\t\tconst scan = this.#scan(this.#conversation.messages())\n\t\t// A recall while a selection holds its scan, which only another conversation's run can make,\n\t\t// holds no scan of its own, so the classifier's handlers read the selection's.\n\t\tconst added = this.#scans.size === 0\n\t\tif (added) this.#hold(scan)\n\t\ttry {\n\t\t\tconst { input, projection } = this.#project(scan)\n\t\t\tconst registry = scan.registry\n\t\t\tconst searches = splitTopic(topic).map((part) => {\n\t\t\t\tconst words = splitWords(part)\n\t\t\t\tconst matched = new Set([\n\t\t\t\t\t...[...registry.ids].filter(\n\t\t\t\t\t\t(id) =>\n\t\t\t\t\t\t\textractLiterals(part).ids.has(id) ||\n\t\t\t\t\t\t\twords.every((word) =>\n\t\t\t\t\t\t\t\t`${id} ${(registry.owners.get(id) ?? []).join(' ')}`.toLowerCase().includes(word),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t\t...this.#options.topics\n\t\t\t\t\t\t.filter((one) => words.every((word) => one.name.toLowerCase().includes(word)))\n\t\t\t\t\t\t.map((one) => one.name),\n\t\t\t\t])\n\t\t\t\treturn { words, matched }\n\t\t\t})\n\t\t\tconst start = request?.message === undefined ? -1 : (scan.position(request.message.id) ?? -1)\n\t\t\tconst listable = new Map(\n\t\t\t\tinput.messages\n\t\t\t\t\t.filter(\n\t\t\t\t\t\t(message, at) =>\n\t\t\t\t\t\t\t!this.#requests.has(message.id) && (at < start || message.role === 'tool'),\n\t\t\t\t\t)\n\t\t\t\t\t.map((message) => [message.id, message]),\n\t\t\t)\n\t\t\tconst matched = new Set(searches.flatMap((search) => [...search.matched]))\n\t\t\tconst unmatched = searches.filter(\n\t\t\t\t(search) => search.matched.size === 0 && search.words.length > 0,\n\t\t\t)\n\t\t\t// Newest first; each item's amenders are claimed by it, so an older item never lists them again.\n\t\t\tconst gathered = [...listable.values()].reverse().reduce<{\n\t\t\t\treadonly done: ReadonlySet<string>\n\t\t\t\treadonly listed: readonly string[]\n\t\t\t\treadonly recalled: ReadonlyMap<string, string>\n\t\t\t}>(\n\t\t\t\t(state, message) => {\n\t\t\t\t\tconst finding = scan.finding(message.id)\n\t\t\t\t\tconst topics = new Set([\n\t\t\t\t\t\t...(input.entities.get(message.id) ?? []),\n\t\t\t\t\t\t...this.#classifier.topics(message.id),\n\t\t\t\t\t\t...projection.records\n\t\t\t\t\t\t\t.filter(\n\t\t\t\t\t\t\t\t(record) =>\n\t\t\t\t\t\t\t\t\trecord.key.startsWith(LEDGER_OWNER_PREFIX) && record.members.includes(message.id),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.map((record) => record.key.slice(LEDGER_OWNER_PREFIX.length)),\n\t\t\t\t\t])\n\t\t\t\t\tconst onTopic =\n\t\t\t\t\t\t(message.role === 'tool'\n\t\t\t\t\t\t\t? finding?.result !== undefined\n\t\t\t\t\t\t\t: !hasCalls(message) && !this.#classifier.quiet(message.id)) &&\n\t\t\t\t\t\t[...matched].some((match) => topics.has(match))\n\t\t\t\t\tconst worded =\n\t\t\t\t\t\t(message.role === 'tool' ? finding !== undefined : message.role === 'user') &&\n\t\t\t\t\t\tunmatched.some((search) =>\n\t\t\t\t\t\t\tsearch.words.every((word) => message.content.toLowerCase().includes(word)),\n\t\t\t\t\t\t)\n\t\t\t\t\tif (!onTopic && !worded) return state\n\t\t\t\t\tconst items = scanAmendments(\n\t\t\t\t\t\tinput.classification.amendments,\n\t\t\t\t\t\tmessage.id,\n\t\t\t\t\t\t(candidate) => listable.has(candidate) && !state.done.has(candidate),\n\t\t\t\t\t).flatMap((id) => {\n\t\t\t\t\t\tconst source = listable.get(id)\n\t\t\t\t\t\tif (source === undefined) return []\n\t\t\t\t\t\tconst lookup = scan.finding(id)\n\t\t\t\t\t\tconst text =\n\t\t\t\t\t\t\tlookup === undefined\n\t\t\t\t\t\t\t\t? source.content\n\t\t\t\t\t\t\t\t: `${renderLookupCall(lookup.name, lookup.arguments)}: ${source.content}`\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\t\ttext,\n\t\t\t\t\t\t\t\tstored:\n\t\t\t\t\t\t\t\t\tlookup === undefined\n\t\t\t\t\t\t\t\t\t\t? []\n\t\t\t\t\t\t\t\t\t\t: [\n\t\t\t\t\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t\t\t\t\tsplitResultLines(text)[0] ?? '',\n\t\t\t\t\t\t\t\t\t\t\t\t\tsplitResultLines(source.content)[0] ?? '',\n\t\t\t\t\t\t\t\t\t\t\t\t] as const,\n\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t]\n\t\t\t\t\t})\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdone: new Set([...state.done, ...items.map((item) => item.id)]),\n\t\t\t\t\t\tlisted:\n\t\t\t\t\t\t\titems.length === 0\n\t\t\t\t\t\t\t\t? state.listed\n\t\t\t\t\t\t\t\t: [...state.listed, items.map((item) => item.text).join('\\n')],\n\t\t\t\t\t\trecalled: new Map([...state.recalled, ...items.flatMap((item) => item.stored)]),\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t{ done: new Set(), listed: [], recalled: new Map() },\n\t\t\t)\n\t\t\tconst result =\n\t\t\t\tgathered.listed.length === 0\n\t\t\t\t\t? `nothing on \"${topic}\"; recall ${guidance}`\n\t\t\t\t\t: cutListing(gathered.listed, room)\n\t\t\trequest?.list(result, gathered.recalled)\n\t\t\treturn result\n\t\t} finally {\n\t\t\tif (added) this.#scans.delete(scan)\n\t\t}\n\t}\n\n\t#buildDigest(request: LedgerRequest, entered: Message): string | undefined {\n\t\tthis.#flush()\n\t\t// Each line once, at its first occurrence.\n\t\tconst lines = [\n\t\t\t...new Set(\n\t\t\t\tthis.#collectAfter(entered).flatMap((message) => {\n\t\t\t\t\tconst result = this.#results.get(message.id)\n\t\t\t\t\tif (message.role !== 'tool' || result?.success !== true) return []\n\t\t\t\t\tconst recalled = result.name === 'recall'\n\t\t\t\t\tif (recalled && /^nothing on /.test(message.content)) return []\n\t\t\t\t\tconst stored = recalled ? request.lines(message.content) : undefined\n\t\t\t\t\treturn splitResultLines(message.content)\n\t\t\t\t\t\t.filter((line) => !recalled || !matchesCutLine(line))\n\t\t\t\t\t\t.map((line) => stored?.get(line) ?? line)\n\t\t\t\t}),\n\t\t\t),\n\t\t]\n\t\treturn lines.length === 0 ? undefined : `${LEDGER_NOTES.results}\\n${lines.join('\\n')}`\n\t}\n}\n","import type { ProviderInterface } from '../providers/types.js'\nimport type { LedgerInterface, LedgerOptions } from './types.js'\nimport { Ledger } from './Ledger.js'\n\n/**\n * Creates a conversation ledger after checking its thresholds, allocation, and tool names.\n * @param provider - The provider that answers requests\n * @param options - The judge, projection policy, capacity, and agent bounds\n * @returns The ledger and its owned conversation and agent\n * @throws {LedgerError} Thrown when an option lies outside its documented bounds\n * @example\n * ```ts\n * const ledger = createLedger(provider, options)\n * const reply = await ledger.respond('Check the order.')\n * ```\n */\nexport function createLedger(provider: ProviderInterface, options: LedgerOptions): LedgerInterface {\n\treturn new Ledger(provider, options)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAMA,IAAa,gBAAgB,OAAO,OAAO;CAAC;CAAU;CAAQ;CAAa;AAAM,CAAU;;;;;;AAO3F,IAAa,qBAAqB,OAAO,OAAO;CAAC;CAAQ;CAAa;AAAM,CAAU;;;;;;;;;;;;;ACStF,IAAa,kBAAb,cAAqC,MAAM;;CAE1C,OAAgB;CAChB;CAEA,YAAY,SAAsB,SAAwB;EACzD,MAAM,sBAAsB,OAAO;EACnC,KAAK,OAAO;EACZ,oBAAA,WAAW,OAAO,MAAM,OAAO,IAAI,kCAAkC,GAAG,EAAE,OAAO,KAAK,CAAC;EACvF,KAAK,UAAU;CAChB;AACD;;;;;;;;;;;;AAaA,SAAgB,kBAAkB,OAA0C;CAC3E,QAAA,GAAOA,oBAAAA,MAAAA,OAAY;EAClB,IACC,EACC,iBAAiB,SACjBC,oBAAAA,WAAW,UAAU,KAAK,MAAM,MAAM,aACtCA,oBAAAA,WAAW,SAAS,OAAO,OAAO,IAAI,kCAAkC,CAAC,CAAC,EAAE,UAAU,QAGvF,OAAO;EAER,IAAI,EADSA,oBAAAA,WAAW,QAAQ,KAAK,OAAO,MACtC,MAAS,UAAU,OAAO;EAChC,MAAM,UAAUA,oBAAAA,WAAW,QAAQ,KAAK,OAAO,SAAS;EACxD,IAAI,GAAA,GAAEC,oBAAAA,SAAAA,CAAS,OAAO,MAAA,GAAKC,oBAAAA,WAAAA,CAAW,OAAO,IAAI,OAAO;EACxD,MAAM,UAAUF,oBAAAA,WAAW,QAAQ,KAAK,SAAS,SAAS;EAC1D,MAAM,WAAWA,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K,MAAM,CAAC,CAAC;IAC/E;GACD,GACA;IAAE,sBAAM,IAAI,IAAI;IAAG,QAAQ,CAAC;IAAG,0BAAU,IAAI,IAAI;GAAE,CACpD;GACA,MAAM,SACL,SAAS,OAAO,WAAW,IACxB,eAAe,MAAM,YAAY,aACjC,WAAW,SAAS,QAAQ,IAAI;GACpC,SAAS,KAAK,QAAQ,SAAS,QAAQ;GACvC,OAAO;EACR,UAAU;GACT,IAAI,OAAO,KAAK,OAAO,OAAO,IAAI;EACnC;CACD;CAEA,aAAa,SAAwB,SAAsC;EAC1E,KAAK,OAAO;EAEZ,MAAM,QAAQ,CACb,GAAG,IAAI,IACN,KAAK,cAAc,OAAO,CAAC,CAAC,SAAS,YAAY;GAChD,MAAM,SAAS,KAAK,SAAS,IAAI,QAAQ,EAAE;GAC3C,IAAI,QAAQ,SAAS,UAAU,QAAQ,YAAY,MAAM,OAAO,CAAC;GACjE,MAAM,WAAW,OAAO,SAAS;GACjC,IAAI,YAAY,eAAe,KAAK,QAAQ,OAAO,GAAG,OAAO,CAAC;GAC9D,MAAM,SAAS,WAAW,QAAQ,MAAM,QAAQ,OAAO,IAAI,KAAA;GAC3D,OAAO,iBAAiB,QAAQ,OAAO,CAAC,CACtC,QAAQ,SAAS,CAAC,YAAY,CAAC,eAAe,IAAI,CAAC,CAAC,CACpD,KAAK,SAAS,QAAQ,IAAI,IAAI,KAAK,IAAI;EAC1C,CAAC,CACF,CACD;EACA,OAAO,MAAM,WAAW,IAAI,KAAA,IAAY,GAAG,aAAa,QAAQ,IAAI,MAAM,KAAK,IAAI;CACpF;AACD;;;;;;;;;;;;;;ACn2CA,SAAgB,aAAa,UAA6B,SAAyC;CAClG,OAAO,IAAI,OAAO,UAAU,OAAO;AACpC"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["holds","INTRINSICS","isObject","isFunction","isString","isNumber","arrayOf","objectShape","stringShape","recordShape","jsonShape","literalShape","optionalShape","arrayShape","unionShape","nullableShape","numberShape","createContract","attempt","parseJSON","isString","preview","describeFaults","isFiniteNumber","attempt","isArray","isRecord","isString","isArray","isJSONValue","isRecord","attempt","nullableOf","arrayOf","optionalOf","holds","isFiniteNumber","holds","INTRINSICS","isObject","isFunction","isString","isBoolean","isNumber","arrayOf","objectShape","arrayShape","optionalShape","stringShape","recordShape","jsonShape","booleanShape","unionShape","literalShape","createContract","objectOf","isString","arrayOf","isToolCall","isBudgetAmount","isBoolean","attempt","isRecord","optionalOf","nullableOf","isNumber","boundsOf","isArray","attempt","isObject","isFunction","preview","canonicalStringify","failure","success","boundsOf","isArray","isNumber","isTokenUsage","Timeout","createNDJSONParser","attempt","cloneJSONValue","isRecord","Timeout","parseJSON","describeFaults","preview","attempt","cloneJSONValue","preview","parseJSONAs","holds","INTRINSICS","isObject","isFunction","unionShape","objectShape","stringShape","arrayShape","numberShape","optionalShape","createContract","attempt","isRecord","isString","arrayOf","isToolCall","unionOf","recordOf","isFiniteNumber","isTokenUsage","holds","optionalOf","isObject","isBoolean","isFunction","preview","isRecord","isJSONValue","describeFaults","attempt","isArray","isString","isArray","isRecord","describeFaults","Emitter","isArray","preview","isArray","preview","describeFaults","createMemoryDriver","stringShape","rawShape","createDatabase","holds","INTRINSICS","isObject","isFunction","Emitter","isArray","isBinary","objectShape","stringShape","numberShape","optionalShape","arrayShape","createContract","holds","isRecord","isString","isFiniteNumber","arrayOf","describeFaults","Emitter","isArray","Emitter","isArray","Emitter","isArray","createFile","preview","describeFaults","WorkspaceManager","ToolManager","isTextContent","createMemoryDriver","stringShape","rawShape","createDatabase","holds","INTRINSICS","isObject","isFunction","isString","isNumber","isBoolean","objectShape","stringShape","optionalShape","arrayShape","numberShape","recordShape","jsonShape","unionShape","literalShape","booleanShape","createContract","objectOf","isString","attempt","isObject","isString","isBoolean","isFiniteNumber","parseJSONValue","isBudgetAmount","isBudgetError","preview","canonicalStringify","createContract","schemaToShape","parseJSON","describeFaults","objectOf","isBudget","ContractError","Emitter","attempt","describeFaults","ContractError","preview","createTimeout","createAbort","createTokenMeter","failure","isObject","errorToMessage","success","preview","describeFaults","createTokenBudget","ToolManager","createQueue","createRunner","holds","INTRINSICS","isObject","isFunction","attempt","canonicalStringify","isError","isString","isFiniteNumber","isError","errorToMessage","parseJSONAs","isArray","isString","isFiniteNumber","canonicalStringify","objectShape","stringShape","arrayShape","optionalShape","numberShape","createContract","holds","isRecord","isString","arrayOf","isFiniteNumber","describeFaults","createToolManager","createTool","objectShape","stringShape","createAbort","isFiniteNumber","attempt","isError","isString"],"sources":["../../../src/core/constants.ts","../../../src/core/errors.ts","../../../src/core/shapers.ts","../../../src/core/contracts.ts","../../../src/core/cloners.ts","../../../src/core/helpers.ts","../../../src/core/validators.ts","../../../src/core/providers/constants.ts","../../../src/core/providers/errors.ts","../../../src/core/providers/shapers.ts","../../../src/core/providers/contracts.ts","../../../src/core/providers/validators.ts","../../../src/core/providers/helpers.ts","../../../src/core/providers/RelayStream.ts","../../../src/core/providers/ThinkSplitter.ts","../../../src/core/providers/AgentProvider.ts","../../../src/core/providers/RelayProvider.ts","../../../src/core/providers/AgentJudge.ts","../../../src/core/providers/SystemOneJudge.ts","../../../src/core/providers/factories.ts","../../../src/core/conversations/constants.ts","../../../src/core/conversations/errors.ts","../../../src/core/conversations/shapers.ts","../../../src/core/conversations/contracts.ts","../../../src/core/conversations/validators.ts","../../../src/core/conversations/helpers.ts","../../../src/core/conversations/JudgmentManager.ts","../../../src/core/conversations/Conversation.ts","../../../src/core/conversations/ConversationManager.ts","../../../src/core/conversations/stores/MemoryConversationStore.ts","../../../src/core/conversations/stores/DatabaseConversationStore.ts","../../../src/core/conversations/factories.ts","../../../src/core/contexts/constants.ts","../../../src/core/contexts/errors.ts","../../../src/core/contexts/instructions/Instruction.ts","../../../src/core/contexts/instructions/InstructionManager.ts","../../../src/core/contexts/helpers.ts","../../../src/core/contexts/shapers.ts","../../../src/core/contexts/contracts.ts","../../../src/core/contexts/validators.ts","../../../src/core/contexts/memos/MemoManager.ts","../../../src/core/contexts/scopes/Scope.ts","../../../src/core/contexts/scopes/ScopeManager.ts","../../../src/core/contexts/skills/SkillManager.ts","../../../src/core/contexts/stores/MemoryMemoStore.ts","../../../src/core/contexts/stores/DatabaseMemoStore.ts","../../../src/core/contexts/AgentContext.ts","../../../src/core/contexts/factories.ts","../../../src/core/agents/constants.ts","../../../src/core/agents/errors.ts","../../../src/core/agents/shapers.ts","../../../src/core/agents/contracts.ts","../../../src/core/agents/ChannelCell.ts","../../../src/core/agents/Channel.ts","../../../src/core/agents/validators.ts","../../../src/core/agents/helpers.ts","../../../src/core/agents/Agent.ts","../../../src/core/agents/AgentFanout.ts","../../../src/core/agents/Authority.ts","../../../src/core/agents/Approver.ts","../../../src/core/agents/AgentRegistry.ts","../../../src/core/agents/factories.ts","../../../src/core/ledgers/constants.ts","../../../src/core/ledgers/errors.ts","../../../src/core/ledgers/helpers.ts","../../../src/core/ledgers/Classifier.ts","../../../src/core/ledgers/Gauge.ts","../../../src/core/ledgers/LedgerPass.ts","../../../src/core/ledgers/LedgerRequest.ts","../../../src/core/ledgers/LedgerScan.ts","../../../src/core/ledgers/shapers.ts","../../../src/core/ledgers/contracts.ts","../../../src/core/ledgers/validators.ts","../../../src/core/ledgers/Ledger.ts","../../../src/core/ledgers/factories.ts"],"sourcesContent":["/**\n * Lists the roles any message can play, the built `system` head included, in the order the wire\n * contract names them — the one list the {@link import('./types.js').MessageRole} union derives\n * from, the message guard tests membership against, and the message shape passes to its literal\n * contract.\n */\nexport const MESSAGE_ROLES = Object.freeze(['system', 'user', 'assistant', 'tool'] as const)\n\n/**\n * Lists the roles a stored conversation message can play: every `MESSAGE_ROLES` member except\n * `system`, in wire order — the one list the {@link import('./types.js').ConversationRole} union\n * derives from and a conversation checks each stored or selected role against.\n */\nexport const CONVERSATION_ROLES = Object.freeze(['user', 'assistant', 'tool'] as const)\n","import type { JudgeResult } from './types.js'\nimport {\n\tINTRINSICS,\n\tholds,\n\tisObject,\n\tisFunction,\n\tisNumber,\n\tisString,\n\tarrayOf,\n} from '@orkestrel/contract'\n\n/**\n * Reports a judge call cancelled by the caller's signal or its deadline, carrying the\n * {@link JudgeResult} merged from the calls that completed before the cancel and the\n * machine-readable `code` `'ABORT'`.\n *\n * @remarks\n * `partial` keeps the answers and the usage of every completed call, so usage reported by\n * completed calls is retained; a cancel before the first call carries an empty partial. `cause` holds the failure the\n * cancel superseded when a throw raced the abort, and is undefined when the cancel was the only\n * failure.\n */\nexport class JudgeAbortError extends Error {\n\t/** Names the machine-readable condition — `'ABORT'`: a judge call cancelled mid-flight. */\n\treadonly code = 'ABORT' as const\n\treadonly partial: JudgeResult\n\n\tconstructor(partial: JudgeResult, options?: ErrorOptions) {\n\t\tsuper('judge call aborted', options)\n\t\tthis.name = 'JudgeAbortError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.JudgeAbortError'), { value: this })\n\t\tthis.partial = partial\n\t}\n}\n\n/**\n * Narrows a caught value to a {@link JudgeAbortError} through its identity brand and declared members, so a `catch` can\n * recover its `partial` result.\n *\n * @param value - The caught value\n * @returns True if the value is a {@link JudgeAbortError}; false otherwise\n * @example\n * ```ts\n * isJudgeAbortError(new JudgeAbortError({ model: 'tev1:0.8b', answers: {} })) // true\n * ```\n */\nexport function isJudgeAbortError(value: unknown): value is JudgeAbortError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.JudgeAbortError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'ABORT')) return false\n\t\tconst partial = INTRINSICS.reflect.read(value, 'partial')\n\t\tif (!(isObject(partial) || isFunction(partial))) return false\n\t\tconst answers = INTRINSICS.reflect.read(partial, 'answers')\n\t\tconst refusals = INTRINSICS.reflect.read(partial, 'refusals')\n\t\tconst usage = INTRINSICS.reflect.read(partial, 'usage')\n\t\treturn (\n\t\t\tisString(INTRINSICS.reflect.read(partial, 'model')) &&\n\t\t\t(usage === undefined ||\n\t\t\t\t((isObject(usage) || isFunction(usage)) &&\n\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(usage, key)),\n\t\t\t\t\t))) &&\n\t\t\t(isObject(answers) || isFunction(answers)) &&\n\t\t\tINTRINSICS.values(answers).every((answer: unknown) => {\n\t\t\t\tif (!(isObject(answer) || isFunction(answer))) return false\n\t\t\t\tconst form = INTRINSICS.reflect.read(answer, 'form')\n\t\t\t\tif (form === 'noul') return isNumber(INTRINSICS.reflect.read(answer, 'noul'))\n\t\t\t\tconst probabilities = INTRINSICS.reflect.read(answer, 'probabilities')\n\t\t\t\tif (form === 'score') return arrayOf(isNumber)(probabilities)\n\t\t\t\treturn (\n\t\t\t\t\tform === 'choice' &&\n\t\t\t\t\t(isObject(probabilities) || isFunction(probabilities)) &&\n\t\t\t\t\tINTRINSICS.values(probabilities).every(isNumber)\n\t\t\t\t)\n\t\t\t}) &&\n\t\t\t(refusals === undefined ||\n\t\t\t\t((isObject(refusals) || isFunction(refusals)) &&\n\t\t\t\t\tINTRINSICS.values(refusals).every(\n\t\t\t\t\t\t(refusal: unknown) =>\n\t\t\t\t\t\t\t(isObject(refusal) || isFunction(refusal)) &&\n\t\t\t\t\t\t\tarrayOf(isString)(INTRINSICS.reflect.read(refusal, 'missing')),\n\t\t\t\t\t)))\n\t\t)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport { MESSAGE_ROLES } from './constants.js'\nimport {\n\tarrayShape,\n\tjsonShape,\n\tliteralShape,\n\tnullableShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\trecordShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\n\n/**\n * Describes a tool call's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than the domain: non-JSON arguments are refused.\n * Calls carry only id, name, and arguments; execution context stays local.\n * The guard refuses every extra member; the contract parser drops extra members.\n */\nexport const toolCallShape = objectShape({\n\tid: stringShape(),\n\tname: stringShape(),\n\targuments: recordShape(jsonShape()),\n}) satisfies ContractShape\n\n/**\n * Describes a message input's JSON projection: a {@link messageShape} with no `id`, which the\n * storing conversation mints.\n *\n * @remarks\n * A conversation's `add` checks a hoisted hand guard on every message and reads this shape's `audit`\n * only to fill a refusal's faults. A job's seed messages cross it at `build`. The role literal admits\n * `system`, so the conversation's `ROLE` refusal explains that case.\n */\nexport const messageInputShape = objectShape({\n\trole: literalShape(MESSAGE_ROLES),\n\tcontent: stringShape(),\n\tcalls: optionalShape(arrayShape(toolCallShape)),\n\tcall: optionalShape(stringShape()),\n\timages: optionalShape(arrayShape(stringShape())),\n\tthinking: optionalShape(stringShape()),\n}) satisfies ContractShape\n\n/**\n * Describes a conversation message's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than Message: non-JSON call arguments are refused,\n * and execution context stays local. The guard refuses extra members; the parser drops them.\n */\nexport const messageShape = objectShape({\n\tid: stringShape(),\n\trole: literalShape(MESSAGE_ROLES),\n\tcontent: stringShape(),\n\tcalls: optionalShape(arrayShape(toolCallShape)),\n\tcall: optionalShape(stringShape()),\n\timages: optionalShape(arrayShape(stringShape())),\n\tthinking: optionalShape(stringShape()),\n}) satisfies ContractShape\n\n/**\n * Describes the text or JSON a judge reads: a non-empty string, a non-empty JSON array, or a JSON record.\n *\n * @remarks\n * A shape has no `minProperties`, so this one admits the empty record that `isJudgeEntry` refuses;\n * the guard is the door, and this shape only fills a refusal's faults.\n */\nexport const judgeEntryShape = unionShape(\n\tstringShape({ min: 1 }),\n\tarrayShape(jsonShape(), { min: 1 }),\n\trecordShape(jsonShape()),\n) satisfies ContractShape\n\n/**\n * Describes a judge question: one exact object per form, the schema an LLM-facing wire can publish.\n *\n * @remarks\n * `isJudgeQuestion` is the door; this shape fills a refusal's faults. A choice needing at least 2\n * options is the guard's alone, because a shape has no `minProperties`.\n */\nexport const judgeQuestionShape = unionShape(\n\tobjectShape({\n\t\tform: literalShape(['choice']),\n\t\tinstructions: optionalShape(judgeEntryShape),\n\t\tcriteria: recordShape(nullableShape(judgeEntryShape)),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['score']),\n\t\tinstructions: optionalShape(judgeEntryShape),\n\t\tcriteria: arrayShape(nullableShape(judgeEntryShape), { min: 2 }),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['noul']),\n\t\tinstructions: optionalShape(judgeEntryShape),\n\t\tcriteria: optionalShape(\n\t\t\tobjectShape({\n\t\t\t\ttrue: optionalShape(judgeEntryShape),\n\t\t\t\tfalse: optionalShape(judgeEntryShape),\n\t\t\t}),\n\t\t),\n\t}),\n) satisfies ContractShape\n\n/**\n * Describes a judge answer: one exact object per form, each probability in `[0, 1]`.\n *\n * @remarks\n * The ties an answer holds to its question run at the write doors through `scanJudgmentTies`, which a\n * shape over the answer alone cannot state.\n */\nexport const judgeAnswerShape = unionShape(\n\tobjectShape({\n\t\tform: literalShape(['choice']),\n\t\tprobabilities: recordShape(numberShape({ min: 0, max: 1 })),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['score']),\n\t\tprobabilities: arrayShape(numberShape({ min: 0, max: 1 })),\n\t}),\n\tobjectShape({\n\t\tform: literalShape(['noul']),\n\t\tnoul: numberShape({ min: 0, max: 1 }),\n\t}),\n) satisfies ContractShape\n\n/**\n * Describes a token usage record's JSON wire projection — its `prompt`, `completion`, and `total`\n * counts, shared by the provider result and the agent frame wires.\n *\n * @remarks\n * Each count is a nonnegative number. The guard refuses extra members; the contract parser drops them.\n */\nexport const tokenUsageShape = objectShape({\n\tprompt: numberShape({ min: 0 }),\n\tcompletion: numberShape({ min: 0 }),\n\ttotal: numberShape({ min: 0 }),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { judgeAnswerShape, judgeQuestionShape, messageInputShape, messageShape } from './shapers.js'\n\n/** Validates and projects conversation messages at a JSON wire boundary. */\nexport const messageContract = createContract(messageShape)\n\n/** Reads the faults of a refused judge question. */\nexport const judgeQuestionContract = createContract(judgeQuestionShape)\n\n/** Reads the faults of a refused judge answer. */\nexport const judgeAnswerContract = createContract(judgeAnswerShape)\n\n/** Reads the faults of a refused message input and validates a job's seed messages. */\nexport const messageInputContract = createContract(messageInputShape)\n","import { attempt, parseJSON } from '@orkestrel/contract'\n\n/**\n * Owns a value by serializing it to JSON and parsing the text, so the copy shares nothing with its source.\n *\n * @remarks\n * A value that JSON cannot carry, such as a cycle or a bigint, and a value that serializes to\n * nothing, such as a function, both return undefined, so a guard over the result refuses them.\n * A proxied value serializes through its traps, where a structured clone refuses it.\n *\n * @param value - The value to own\n * @returns The owned JSON copy, or undefined when the value is not JSON\n * @example\n * ```ts\n * copyJSON({ state: 'A ticket.' }) // { state: 'A ticket.' }\n * copyJSON(() => 1) // undefined\n * ```\n */\nexport function copyJSON(value: unknown): unknown {\n\tconst text = attempt(() => JSON.stringify(value))\n\tif (!text.success || typeof text.value !== 'string') return undefined\n\treturn parseJSON(text.value)\n}\n\n/**\n * Owns a value through JSON and freezes every record and list of the copy, leaving the value unchanged.\n *\n * @remarks\n * Returns undefined exactly where `copyJSON` does, a value too deep for `JSON.stringify` among them.\n * A worklist over the owned copy does the freezing, so any depth `JSON.stringify` holds also freezes.\n *\n * @param value - The value to own and freeze\n * @returns The frozen JSON copy, or undefined when the value is not JSON\n * @example\n * ```ts\n * Object.isFrozen(freezeJSON({ state: 'A ticket.' })) // true\n * freezeJSON(() => 1) // undefined\n * ```\n */\nexport function freezeJSON(value: unknown): unknown {\n\tconst copy = copyJSON(value)\n\tif (typeof copy !== 'object' || copy === null) return copy\n\tconst worklist: object[] = [copy]\n\tfor (const node of worklist) {\n\t\tObject.freeze(node)\n\t\tconst children: readonly unknown[] = Object.values(node)\n\t\tfor (const child of children) {\n\t\t\tif (typeof child === 'object' && child !== null) worklist.push(child)\n\t\t}\n\t}\n\treturn copy\n}\n","import type {\n\tJudgeAnswer,\n\tJudgeQuestion,\n\tJudgment,\n\tLiteralSet,\n\tMessage,\n\tThinkingReplay,\n} from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport type { AuditFault, JSONSchema } from '@orkestrel/contract'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tisArray,\n\tisFiniteNumber,\n\tisRecord,\n\tisString,\n\tpreview,\n} from '@orkestrel/contract'\n\n/**\n * Filters a list of members by a {@link import('./contexts/index.js').ScopeInterface} allow-list of keys —\n * `undefined` passes everything, `[]` passes nothing, and a non-empty list passes the listed keys\n * alone, order preserved. The pure, total set-membership primitive the context's build step and\n * the agent loop's tool-advertise step apply a scope through.\n *\n * @remarks\n * Varies by the allow-list's shape, so a `Scope` category expresses \"all /\n * none / only these\":\n * - `undefined` ⇒ no constraint — every member passes (returned unchanged).\n * - `[]` (empty) ⇒ none pass (no key is in an empty set).\n * - a non-empty list ⇒ only members whose `key(member)` is in the list pass.\n *\n * Preserves the members' order. Keys are matched by a `Set` for membership.\n *\n * @typeParam T - The member type being filtered\n * @param allow - The allow-list of keys (`undefined` ⇒ all, `[]` ⇒ none, else only-listed)\n * @param members - The members to filter (returned unchanged when `allow` is `undefined`)\n * @param key - Extracts the key a member is matched on (for example an instruction's `name`)\n * @returns The members that pass the allow-list, in their original order\n *\n * @example\n * ```ts\n * const members = [{ name: 'refunds' }, { name: 'billing' }]\n * filterAllowList(undefined, members, (member) => member.name) // [{ name: 'refunds' }, { name: 'billing' }]\n * filterAllowList([], members, (member) => member.name) // []\n * filterAllowList(['billing'], members, (member) => member.name) // [{ name: 'billing' }]\n * ```\n */\nexport function filterAllowList<T>(\n\tallow: readonly string[] | undefined,\n\tmembers: readonly T[],\n\tkey: (member: T) => string,\n): readonly T[] {\n\tif (allow === undefined) return members\n\tif (allow.length === 0) return []\n\tconst set = new Set(allow)\n\treturn members.filter((member) => set.has(key(member)))\n}\n\n/**\n * Joins the reasoning a run's provider calls separated from the answer — the first call\n * seeds the accumulation, a later call appends blank-line separated so each turn's reasoning\n * stays readable.\n *\n * @remarks\n * Pure and total. `prior` is `undefined` until a call surfaces reasoning, so the first join\n * returns `next` verbatim. Each call's non-empty thinking is recorded on the assistant message\n * that call appends. The joined result also includes thinking from calls that appended no\n * message, such as an aborted call. Only recorded thinking can return to a provider,\n * as its `replay` policy allows. Thinking stays out of `content`.\n *\n * @param prior - The reasoning accumulated so far (`undefined` before the first)\n * @param next - This call's separated reasoning\n * @returns The joined reasoning\n *\n * @example\n * ```ts\n * joinThinking(undefined, 'first') // 'first'\n * joinThinking('', 'x') // 'x'\n * joinThinking('first', 'second') // 'first\\n\\nsecond'\n * ```\n */\nexport function joinThinking(prior: string | undefined, next: string): string {\n\tif (prior === undefined || prior.length === 0) return next\n\treturn next.length === 0 ? prior : `${prior}\\n\\n${next}`\n}\n\n/**\n * Applies a {@link ThinkingReplay} policy to a conversation, returning the messages a provider\n * sends with only the assistant thinking the policy allows.\n *\n * @remarks\n * Pure and total. `'all'` returns the input array itself. `'none'` drops `thinking` from every\n * message that carries it. `'turn'` drops it from every message at or before the last `user`\n * message and keeps it after, the turn in progress; with no `user` message every message counts\n * as inside the turn. A message without `thinking`, or one that keeps it, is the same object;\n * a message that loses it is a copy without that member, so no `undefined` member is written.\n *\n * @param messages - The conversation to project (left unchanged)\n * @param replay - The policy naming which thinking stays\n * @returns The messages with the policy applied\n *\n * @example\n * ```ts\n * const messages = [\n * \t{ id: '1', role: 'user', content: 'Plan the trip' },\n * \t{ id: '2', role: 'assistant', content: 'Booked', thinking: 'Compare fares first' },\n * ]\n * stripThinking(messages, 'none') // [{ id: '1', ... }, { id: '2', role: 'assistant', content: 'Booked' }]\n * stripThinking(messages, 'turn') // the thinking on '2' stays: it follows the last user message\n * ```\n */\nexport function stripThinking(\n\tmessages: readonly Message[],\n\treplay: ThinkingReplay,\n): readonly Message[] {\n\tif (replay === 'all') return messages\n\tconst last =\n\t\treplay === 'none' ? messages.length - 1 : messages.findLastIndex((one) => one.role === 'user')\n\treturn messages.map((message, index) => {\n\t\tif (message.thinking === undefined || index > last) return message\n\t\tconst { thinking: _thinking, ...rest } = message\n\t\treturn rest\n\t})\n}\n\n/**\n * Estimates the context-token footprint of a string — the deterministic `ceil(length / 4)`\n * character heuristic {@link import('./agents/helpers.js').estimateMessages} sums over a conversation's\n * messages (the default context-budget estimator).\n *\n * @remarks\n * Approximates `ceil(length / 4)` (4 characters per token), so the same input always yields the same estimate (no model round-trip).\n * Empty text is `0`. This is a planning heuristic for reasoning about how much a turn's\n * messages cost the next request, not an exact tokenizer count — it never calls a provider,\n * so the agent layer stays provider-agnostic and synchronous where it can be.\n *\n * @param text - The text to estimate (a section summary, a message's content)\n * @returns The estimated token count (`ceil(text.length / 4)`; `0` for empty text)\n *\n * @example\n * ```ts\n * estimateTokens('') // 0\n * estimateTokens('hello') // 2 (ceil(5 / 4))\n * estimateTokens('a'.repeat(40)) // 10\n * ```\n */\nexport function estimateTokens(text: string): number {\n\treturn Math.ceil(text.length / 4)\n}\n\n/**\n * Describes a snapshot a store refuses, naming its id, the first fault, and what a host does about it.\n *\n * @remarks\n * A store's row is the host's table, so the text tells the host to read the row itself and rewrite it,\n * or to delete it, rather than reading a refused row as absent, which would let `open` overwrite it.\n * The id and each fault segment are JSON-encoded and length-capped.\n *\n * @param noun - What the snapshot holds, such as `conversation`\n * @param id - The id the row is keyed by\n * @param faults - The faults of the snapshot's shape\n * @returns The refusal's text\n * @example\n * ```ts\n * describeSnapshotRefusal('conversation', 'c1', [{ reason: 'extra', path: ['summary'] }])\n * // 'conversation \"c1\" holds a snapshot this version refuses (extra key \"summary\" at the root; ...'\n * ```\n */\nexport function describeSnapshotRefusal(\n\tnoun: string,\n\tid: unknown,\n\tfaults: readonly AuditFault[],\n): string {\n\tconst [first] = faults\n\tconst path = first === undefined ? [] : isString(first.path) ? [first.path] : first.path\n\tconst held = preview(id)\n\tconst finding =\n\t\tfirst?.reason === 'extra'\n\t\t\t? `extra key ${preview(path.at(-1))} at ${path.length === 1 ? 'the root' : preview(path.slice(0, -1).join('.'))}`\n\t\t\t: first === undefined\n\t\t\t\t? 'a value its shape cannot name'\n\t\t\t\t: `${first.reason} at ${path.length === 0 ? 'the root' : preview(path.join('.'))}`\n\tconst remedy =\n\t\tfirst?.reason === 'extra' ? 'rewrite it without the key' : 'rewrite it to the declared shape'\n\treturn `${noun} ${held} holds a snapshot this version refuses (${finding}; a snapshot written before this change can carry it); read the row through your own table and ${remedy}, or delete it with store.delete(${held})`\n}\n\n/**\n * Describes why a judge question refuses, in the text both of its doors throw.\n *\n * @remarks\n * The rule travels in the text because the question shape cannot state it: a record has no minimum\n * size, so a single-option choice refuses at the guard with no fault for the list to name.\n *\n * @param id - The question's id\n * @param faults - What the question shape's audit found\n * @param schema - The question shape's JSON Schema\n * @returns The refusal text, with the fault list on the lines after it\n * @example\n * ```ts\n * describeQuestionRefusal('team', [], judgeQuestionContract.schema)\n * // 'question team is malformed; a choice names two or more criteria, a score two or more levels, and a noul only true and false'\n * ```\n */\nexport function describeQuestionRefusal(\n\tid: string,\n\tfaults: readonly AuditFault[],\n\tschema: JSONSchema,\n): string {\n\tconst refusal = `question ${id} is malformed; a choice names two or more criteria, a score two or more levels, and a noul only true and false`\n\treturn faults.length === 0 ? refusal : `${refusal}\\n${describeFaults(faults, schema)}`\n}\n\n/**\n * Scans what a judge's answer must hold to its question: the same form, every probability in\n * `[0, 1]`, a choice's keys equal to the criteria keys, and a score as long as its levels.\n *\n * @remarks\n * These ties run where a judgment is written, at `JudgmentManager.add` and the `AgentJudge` result\n * ingestion. A read of a stored record checks only exact keys, literal forms, and number types,\n * because records written before the ties can miss them.\n *\n * @param question - The question that was asked\n * @param answer - The answer the judge gave\n * @returns Every broken tie as a fault, or an empty list\n * @example\n * ```ts\n * scanJudgmentTies({ form: 'noul' }, { form: 'noul', noul: 2 })\n * // [{ reason: 'constraint', path: ['answer', 'noul'], expected: 'number', constraint: 'max', limit: 1, received: '2' }]\n * ```\n */\nexport function scanJudgmentTies(\n\tquestion: JudgeQuestion,\n\tanswer: JudgeAnswer,\n): readonly AuditFault[] {\n\tif (question.form === 'noul' && answer.form === 'noul') {\n\t\treturn scanProbability(answer.noul, ['answer', 'noul'])\n\t}\n\tif (question.form === 'choice' && answer.form === 'choice') {\n\t\tconst wanted = Object.keys(question.criteria)\n\t\tconst held = Object.keys(answer.probabilities)\n\t\treturn [\n\t\t\t...wanted\n\t\t\t\t.filter((key) => !Object.hasOwn(answer.probabilities, key))\n\t\t\t\t.map((key): AuditFault => ({\n\t\t\t\t\treason: 'missing',\n\t\t\t\t\tpath: ['answer', 'probabilities', key],\n\t\t\t\t\texpected: 'number',\n\t\t\t\t})),\n\t\t\t...held\n\t\t\t\t.filter((key) => !Object.hasOwn(question.criteria, key))\n\t\t\t\t.map((key): AuditFault => ({\n\t\t\t\t\treason: 'extra',\n\t\t\t\t\tpath: ['answer', 'probabilities', key],\n\t\t\t\t})),\n\t\t\t...held.flatMap((key) =>\n\t\t\t\tscanProbability(answer.probabilities[key], ['answer', 'probabilities', key]),\n\t\t\t),\n\t\t]\n\t}\n\tif (question.form === 'score' && answer.form === 'score') {\n\t\tconst levels = question.criteria.length\n\t\tconst count = answer.probabilities.length\n\t\treturn [\n\t\t\t...(count === levels\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\treason: 'constraint',\n\t\t\t\t\t\t\tpath: ['answer', 'probabilities'],\n\t\t\t\t\t\t\texpected: 'array',\n\t\t\t\t\t\t\tconstraint: count < levels ? 'min' : 'max',\n\t\t\t\t\t\t\tlimit: levels,\n\t\t\t\t\t\t\treceived: String(count),\n\t\t\t\t\t\t} satisfies AuditFault,\n\t\t\t\t\t]),\n\t\t\t...answer.probabilities.flatMap((value, index) =>\n\t\t\t\tscanProbability(value, ['answer', 'probabilities', String(index)]),\n\t\t\t),\n\t\t]\n\t}\n\treturn [\n\t\t{\n\t\t\treason: 'type',\n\t\t\tpath: ['answer', 'form'],\n\t\t\texpected: 'literal',\n\t\t\treceived: answer.form,\n\t\t},\n\t]\n}\n\n/**\n * Scans one probability against `[0, 1]`.\n *\n * @param value - The probability\n * @param path - Where it sits\n * @returns A constraint fault when it is out of bounds, or an empty list\n * @example\n * ```ts\n * scanProbability(0.5, ['answer', 'noul']) // []\n * ```\n */\nexport function scanProbability(\n\tvalue: number | undefined,\n\tpath: readonly string[],\n): readonly AuditFault[] {\n\tif (value !== undefined && value >= 0 && value <= 1) return []\n\treturn [\n\t\t{\n\t\t\treason: 'constraint',\n\t\t\tpath,\n\t\t\texpected: 'number',\n\t\t\tconstraint: value !== undefined && value < 0 ? 'min' : 'max',\n\t\t\tlimit: value !== undefined && value < 0 ? 0 : 1,\n\t\t\treceived: String(value),\n\t\t},\n\t]\n}\n\n/**\n * Sanitizes one reported token count into a non-negative integer — a non-finite or\n * non-positive value becomes `0`, and a positive fractional value floors down.\n *\n * @param value - The token count to sanitize\n * @returns The floored count, or `0` when the value is non-finite or non-positive\n * @example\n * ```ts\n * sanitizeToken(12.7) // 12\n * sanitizeToken(-1) // 0\n * sanitizeToken(Number.NaN) // 0\n * ```\n */\nexport function sanitizeToken(value: number): number {\n\treturn isFiniteNumber(value) && value > 0 ? Math.floor(value) : 0\n}\n\n/**\n * Sanitizes a {@link TokenUsage} into non-negative integers — the guard an agent's\n * abort-usage path applies to a provider's partial usage before it is charged against a\n * budget or folded into the run total, because a cancelled stream has no settled result to refuse;\n * a settled result's usage crosses the provider-result door instead.\n *\n * @remarks\n * Per field (`prompt` / `completion` / `total`): a non-finite value (`NaN`, `+Infinity`,\n * `-Infinity`) or a negative value floors to `0`; a fractional value floors to its\n * non-negative integer part. No upper cap is applied. Total — never throws.\n *\n * @param usage - The token usage to sanitize (for example a provider's abort-partial usage)\n * @returns A new {@link TokenUsage} with every field a non-negative integer\n *\n * @example\n * ```ts\n * sanitizeUsage({ prompt: -5, completion: NaN, total: 12.7 }) // { prompt: 0, completion: 0, total: 12 }\n * ```\n */\nexport function sanitizeUsage(usage: TokenUsage): TokenUsage {\n\treturn {\n\t\tprompt: sanitizeToken(usage.prompt),\n\t\tcompletion: sanitizeToken(usage.completion),\n\t\ttotal: sanitizeToken(usage.total),\n\t}\n}\n\n/**\n * Adds two {@link TokenUsage} values field by field — the running total an agent run keeps\n * across its provider calls.\n *\n * @remarks\n * Pure and total: the first call seeds the total (`prior` `undefined` returns `next`\n * unchanged), later calls accumulate. No sanitization happens here — charge a provider's\n * reported usage through {@link sanitizeUsage} first.\n *\n * @param prior - The total so far (`undefined` before the first usage-bearing call)\n * @param next - This call's reported usage\n * @returns The summed usage\n *\n * @example\n * ```ts\n * sumUsage(undefined, { prompt: 2, completion: 1, total: 3 }) // { prompt: 2, completion: 1, total: 3 }\n * sumUsage({ prompt: 2, completion: 1, total: 3 }, { prompt: 1, completion: 1, total: 2 })\n * // { prompt: 3, completion: 2, total: 5 }\n * ```\n */\nexport function sumUsage(prior: TokenUsage | undefined, next: TokenUsage): TokenUsage {\n\tif (prior === undefined) return next\n\treturn {\n\t\tprompt: prior.prompt + next.prompt,\n\t\tcompletion: prior.completion + next.completion,\n\t\ttotal: prior.total + next.total,\n\t}\n}\n\n/**\n * Removes each key through a single-key remover and folds the outcomes, so a batch `remove`\n * reports whether the whole batch applied.\n *\n * @remarks\n * Every key is passed to `remove` even after one is missing, so a present key still takes\n * effect (and emits) when an earlier key was absent.\n *\n * @typeParam K - The key type the remover accepts\n * @param keys - The keys to remove, in order\n * @param remove - Removes one key and returns whether it was present\n * @returns True if every key was present and removed; false otherwise (an empty list returns true)\n *\n * @example\n * ```ts\n * const stored = new Set(['refunds', 'billing'])\n * removeEntries(['refunds', 'escalations'], (key) => stored.delete(key)) // false\n * removeEntries(['billing'], (key) => stored.delete(key)) // true\n * ```\n */\nexport function removeEntries<K>(keys: readonly K[], remove: (key: K) => boolean): boolean {\n\t// `Array.from` passes a hole in `keys` to `remove` as `undefined`, as iteration does; `map` skips it.\n\treturn Array.from(keys, (key) => remove(key)).every((removed) => removed)\n}\n\n/**\n * Collects whole exchanges, joining every exchange spanned by a tool group.\n *\n * @remarks\n * A user message opens an exchange that ends before the next user message. Leading messages\n * form their own exchange. A tool group joins every exchange between its first and last member.\n *\n * @param messages - The messages in prompt order\n * @returns The exchanges in prompt order, with each message retained unchanged\n * @example\n * ```ts\n * collectExchanges([\n * \t{ id: 'greeting', role: 'assistant', content: 'Welcome.' },\n * \t{ id: 'request', role: 'user', content: 'Read the order.' },\n * ]) // a leading exchange and a request exchange\n * ```\n */\nexport function collectExchanges(messages: readonly Message[]): ReadonlyArray<readonly Message[]> {\n\tif (messages.length === 0) return []\n\tconst positions = new Map(messages.map((message, index) => [message, index]))\n\tconst spans = collectToolGroups(messages).map((group) => {\n\t\tconst spanned = group.flatMap((message) => {\n\t\t\tconst position = positions.get(message)\n\t\t\treturn position === undefined ? [] : [position]\n\t\t})\n\t\treturn {\n\t\t\tstart: spanned.reduce((least, position) => Math.min(least, position), messages.length),\n\t\t\tend: spanned.reduce((most, position) => Math.max(most, position), 0),\n\t\t}\n\t})\n\tconst starts = [\n\t\t...new Set([\n\t\t\t0,\n\t\t\t...[...messages.entries()].flatMap(([index, message]) =>\n\t\t\t\tmessage.role === 'user' ? [index] : [],\n\t\t\t),\n\t\t]),\n\t].filter((boundary) => !spans.some((span) => span.start < boundary && boundary <= span.end))\n\treturn starts.map((start, position) =>\n\t\tmessages.slice(start, starts[position + 1] ?? messages.length),\n\t)\n}\n\n/**\n * Checks whether a message carries at least one tool call.\n *\n * @remarks\n * Pure and total. An absent `calls` member and an empty list both return false, so a tool-free\n * assistant turn and a turn whose provider reported an empty call list read alike.\n *\n * @param message - The message to inspect; only its `calls` member is read\n * @returns True if `calls` holds at least one call; false otherwise\n * @example\n * ```ts\n * hasCalls({ calls: [{ id: 'order', name: 'lookup', arguments: {} }] }) // true\n * hasCalls({ calls: [] }) // false\n * hasCalls({}) // false\n * ```\n */\nexport function hasCalls(message: Pick<Message, 'calls'>): boolean {\n\treturn (message.calls?.length ?? 0) > 0\n}\n\n/**\n * Collects each assistant message that carries calls together with the tool messages that answer\n * it, then each run of tool messages that no assistant message owns.\n *\n * @remarks\n * A tool message belongs to the one assistant message whose calls hold its `call` id. Without that\n * unique owner, it belongs to the assistant message that leads its run of tool messages when that\n * leader holds the id, repeats a call id, or the tool message has no `call`. Any other tool message\n * joins the orphan run it sits in. Compaction and the stock selection each keep a group on one side\n * of their cut.\n *\n * @param messages - The messages in prompt order\n * @returns The owned groups in owner order, then the orphan runs, each in prompt order\n * @example\n * ```ts\n * collectToolGroups([\n * \t{ id: 'lookup', role: 'assistant', content: '', calls: [{ id: 'order', name: 'lookup', arguments: {} }] },\n * \t{ id: 'result', role: 'tool', content: 'LH-81660 is late', call: 'order' },\n * ]) // one group holding the lookup call and its result\n * ```\n */\nexport function collectToolGroups(messages: readonly Message[]): ReadonlyArray<readonly Message[]> {\n\tconst leaders = messages.filter((message) => message.role === 'assistant' && hasCalls(message))\n\tconst distinct = new Set(leaders)\n\t// `Array.from` passes a hole in `calls` to the mapper as `undefined`, as iteration does; `map` skips it.\n\tconst owners = Map.groupBy(\n\t\tleaders.flatMap((leader) =>\n\t\t\tArray.from(leader.calls ?? [], (call) => ({ id: call.id, leader })),\n\t\t),\n\t\t(entry) => entry.id,\n\t)\n\tconst starts = [...messages.entries()].flatMap(([index, message]) =>\n\t\tmessage.role === 'tool' ? [] : [index],\n\t)\n\t// A run is one non-tool message, which leads it when it owns calls, and the tool messages after it.\n\tconst runs = [\n\t\t{ leader: undefined, tools: messages.slice(0, starts[0] ?? messages.length) },\n\t\t...starts.map((start, position) => {\n\t\t\tconst message = messages[start]\n\t\t\treturn {\n\t\t\t\tleader: message !== undefined && distinct.has(message) ? message : undefined,\n\t\t\t\ttools: messages.slice(start + 1, starts[position + 1] ?? messages.length),\n\t\t\t}\n\t\t}),\n\t]\n\tconst placed = runs.map(({ leader, tools }) => {\n\t\tconst local = leader?.calls ?? []\n\t\tconst duplicate = new Set(local.map((call) => call.id)).size !== local.length\n\t\treturn tools.map((message) => {\n\t\t\tconst paired =\n\t\t\t\tleader !== undefined &&\n\t\t\t\t(duplicate || message.call === undefined || local.some((call) => call.id === message.call))\n\t\t\tconst claims = message.call === undefined ? undefined : owners.get(message.call)\n\t\t\tconst owner = claims?.length === 1 ? claims[0]?.leader : paired ? leader : undefined\n\t\t\treturn { message, owner }\n\t\t})\n\t})\n\tconst owned = Map.groupBy(\n\t\tplaced.flat().filter((entry) => entry.owner !== undefined),\n\t\t(entry) => entry.owner,\n\t)\n\tconst groups = [...distinct].map((leader) => [\n\t\tleader,\n\t\t...(owned.get(leader) ?? []).map((entry) => entry.message),\n\t])\n\tconst orphans = placed.flatMap((run) => {\n\t\tconst orphan = run.filter((entry) => entry.owner === undefined).map((entry) => entry.message)\n\t\treturn orphan.length > 0 ? [orphan] : []\n\t})\n\treturn [...groups, ...orphans]\n}\n\n/**\n * Renders a value as the JSON text `JSON.stringify` produces, or `undefined` when JSON cannot\n * carry the value.\n *\n * @remarks\n * Pure and total. Where `JSON.stringify` returns text, the result is the same text, property order\n * included; `canonicalStringify` from `@orkestrel/contract` sorts keys instead. A value\n * `JSON.stringify` throws on, such as a `BigInt` or a cycle, and a value it renders as nothing,\n * such as `undefined`, a function, or a symbol, return `undefined`.\n *\n * @param value - The value to render\n * @returns The JSON text, or `undefined` when the value has no JSON text\n * @example\n * ```ts\n * renderJSON({ order: 'LH-81660', late: true }) // '{\"order\":\"LH-81660\",\"late\":true}'\n * renderJSON(10n) // undefined\n * renderJSON(undefined) // undefined\n * ```\n */\nexport function renderJSON(value: unknown): string | undefined {\n\tconst outcome = attempt((): string | undefined => JSON.stringify(value))\n\treturn outcome.success ? outcome.value : undefined\n}\n\n/**\n * Matches two values of JSON data by JSON value, whatever their key order, and reads a value with no JSON text as no match.\n *\n * @remarks\n * True exactly when the two values' JSON texts, as `JSON.stringify` renders them, parse to the\n * same JSON value, and false when either side has no JSON text, a cycle aside. A record member\n * whose value is `undefined`, a function, or a symbol is absent, as JSON renders it, and each\n * member is read as an own member, so a `__proto__` or `constructor` key counts only on a side\n * that holds it. A list compares by position and length. The walk stops at the first difference.\n *\n * Bounded to JSON data. It promises nothing for a cycle, a `toJSON` object, `NaN`, an infinity, or\n * an `undefined`, function, symbol, or hole inside a list.\n *\n * @param left - One value\n * @param right - The other value\n * @returns True if the two values are the same JSON value; false otherwise\n * @example\n * ```ts\n * matchesJSON({ order: 'LH-81660', late: true }, { late: true, order: 'LH-81660' }) // true\n * matchesJSON(['LH-81660', 'LH-81661'], ['LH-81661', 'LH-81660']) // false\n * matchesJSON(undefined, undefined) // false\n * ```\n */\nexport function matchesJSON(left: unknown, right: unknown): boolean {\n\tif (typeof left !== 'object' || left === null) {\n\t\treturn (\n\t\t\tleft === right &&\n\t\t\t(typeof left === 'string' ||\n\t\t\t\ttypeof left === 'number' ||\n\t\t\t\ttypeof left === 'boolean' ||\n\t\t\t\tleft === null)\n\t\t)\n\t}\n\tif (isArray(left)) {\n\t\treturn (\n\t\t\tisArray(right) &&\n\t\t\tleft.length === right.length &&\n\t\t\tleft.every((member, index) => matchesJSON(member, right[index]))\n\t\t)\n\t}\n\tif (!isRecord(left) || !isRecord(right)) return false\n\tconst ours = Object.keys(left).filter(\n\t\t(key) =>\n\t\t\tleft[key] !== undefined && typeof left[key] !== 'function' && typeof left[key] !== 'symbol',\n\t)\n\tconst theirs = Object.keys(right).filter(\n\t\t(key) =>\n\t\t\tright[key] !== undefined &&\n\t\t\ttypeof right[key] !== 'function' &&\n\t\t\ttypeof right[key] !== 'symbol',\n\t)\n\treturn (\n\t\tours.length === theirs.length &&\n\t\tours.every((key) => Object.hasOwn(right, key) && matchesJSON(left[key], right[key]))\n\t)\n}\n\n/**\n * Matches a recorded question by JSON value, the ordered sources, the rendered state, and the judge identity.\n *\n * @param judgment - The recorded judgment to compare\n * @param question - The question to ask\n * @param sources - The ordered source message ids\n * @param state - The rendered state to compare\n * @param model - The configured judge identity\n * @returns True if every identity component matches; false otherwise\n * @example\n * ```ts\n * matchesJudgment(judgment, question, ['message-a'], 'Charged twice', judge.model)\n * ```\n */\nexport function matchesJudgment(\n\tjudgment: Judgment,\n\tquestion: JudgeQuestion,\n\tsources: readonly string[],\n\tstate: string,\n\tmodel: string,\n): boolean {\n\treturn (\n\t\tjudgment.model === model &&\n\t\tjudgment.state === state &&\n\t\tjudgment.sources.length === sources.length &&\n\t\tjudgment.sources.every((id, index) => id === sources[index]) &&\n\t\tmatchesJSON(judgment.question, question)\n\t)\n}\n\n/**\n * Reads the ids and the numbers of a text.\n *\n * @param text - The text to read\n * @returns The uppercased hyphenated ids that hold a digit, and the numbers outside those ids with grouping commas removed\n *\n * @example\n * ```ts\n * const literals = extractLiterals('Order bw-5512 totals 1,200.50')\n * // literals.ids is Set { 'BW-5512' }, literals.numbers is Set { 1200.5 }\n * ```\n */\nexport function extractLiterals(text: string): LiteralSet {\n\tconst idShape = /\\b[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+\\b/g\n\tconst ids = new Set(\n\t\t[...text.matchAll(idShape)].flatMap(([token]) =>\n\t\t\t/\\d/.test(token) ? [token.toUpperCase()] : [],\n\t\t),\n\t)\n\tconst rest = text.replace(idShape, (token) => (/\\d/.test(token) ? ' ' : token))\n\tconst numbers = new Set(\n\t\t[...rest.matchAll(/(?<![\\w.])\\d[\\d,]*(?:\\.\\d+)?/g)].flatMap(([token]) => {\n\t\t\tconst value = Number(token.replace(/,/g, ''))\n\t\t\treturn Number.isFinite(value) ? [value] : []\n\t\t}),\n\t)\n\treturn { ids, numbers }\n}\n","import type { JudgeAnswer, JudgeEntry, JudgeQuestion } from './types.js'\nimport {\n\tarrayOf,\n\tattempt,\n\tholds,\n\tisArray,\n\tisFiniteNumber,\n\tisJSONValue,\n\tisRecord,\n\tisString,\n\tnullableOf,\n\toptionalOf,\n} from '@orkestrel/contract'\n\n/**\n * Checks whether a value is a judge entry: a non-empty string, a non-empty JSON record, or a non-empty\n * JSON array.\n *\n * @remarks\n * Total: a cycle, a non-JSON member, a class instance, and an untrusted input return false. `''`,\n * `[]`, and `{}` give a judge nothing to read, so they return false. `null` is not an entry; a\n * criteria guard admits it where the protocol keeps it.\n *\n * @param value - The unknown entry candidate\n * @returns True if the value is a non-empty string or JSON structure the model can read; false otherwise\n * @example\n * ```ts\n * isJudgeEntry('Customer asks for a refund') // true\n * isJudgeEntry({ ticket: 4182, tags: ['billing'] }) // true\n * isJudgeEntry(null) // false\n * isJudgeEntry({ opened: new Date() }) // false\n * ```\n */\nexport function isJudgeEntry(value: unknown): value is JudgeEntry {\n\tif (isString(value)) return value.length > 0\n\tif (isArray(value)) return isJSONValue(value) && value.length > 0\n\treturn isRecord(value) && isJSONValue(value) && Object.keys(value).length > 0\n}\n\n/**\n * Checks whether a value is a well-formed judge question of the choice, score, or noul form.\n *\n * @remarks\n * Total: an untrusted input returns false. A choice needs at least 2 options and a score at least 2\n * levels, because the confidence formula divides by the candidate count; a description or a level\n * can be `null`. `instructions` and noul `criteria` are omitted when absent and never `null`. The\n * guard is exact on the question's own keys: an unknown member, and an own `undefined` optional,\n * return false, as `judgeQuestionContract` does. Server limits on option, level, and question counts\n * are left to the server.\n *\n * @param value - The unknown question candidate\n * @returns True if the value is a question the judge engine can send; false otherwise\n * @example\n * ```ts\n * isJudgeQuestion({ form: 'choice', criteria: { billing: null, bug: 'Software defect' } }) // true\n * isJudgeQuestion({ form: 'score', criteria: ['Cosmetic', null, 'Blocking'] }) // true\n * isJudgeQuestion({ form: 'noul', instructions: 'Is a refund owed?' }) // true\n * isJudgeQuestion({ form: 'choice', criteria: { billing: null } }) // false\n * ```\n */\nexport function isJudgeQuestion(value: unknown): value is JudgeQuestion {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { form, instructions, criteria } = value\n\t\tif (instructions !== undefined && !isJudgeEntry(instructions)) return false\n\t\tconst owned = 1 + (instructions === undefined ? 0 : 1) + (criteria === undefined ? 0 : 1)\n\t\tif (Object.keys(value).length !== owned) return false\n\t\tif (form === 'choice') {\n\t\t\treturn (\n\t\t\t\tisRecord(criteria) &&\n\t\t\t\tObject.keys(criteria).length >= 2 &&\n\t\t\t\tObject.values(criteria).every(nullableOf(isJudgeEntry))\n\t\t\t)\n\t\t}\n\t\tif (form === 'score') {\n\t\t\treturn arrayOf(nullableOf(isJudgeEntry))(criteria) && criteria.length >= 2\n\t\t}\n\t\tif (form !== 'noul') return false\n\t\tif (criteria === undefined) return true\n\t\tif (!isRecord(criteria)) return false\n\t\tconst held = (criteria.true === undefined ? 0 : 1) + (criteria.false === undefined ? 0 : 1)\n\t\treturn (\n\t\t\tObject.keys(criteria).length === held &&\n\t\t\toptionalOf(isJudgeEntry)(criteria.true) &&\n\t\t\toptionalOf(isJudgeEntry)(criteria.false)\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a value is a stored judge answer of the choice, score, or noul form.\n *\n * @remarks\n * Total: an untrusted input returns false. The guard is exact on the answer's own keys and reads each\n * probability as a finite number, with noul bounded to `[0, 1]`. Whether the answer fits\n * its question belongs to the write. Every\n * judgment of a hydrate reads this guard, so it counts keys by hand rather than through a built one,\n * and `judgeAnswerContract` holds it in lockstep: an answer the contract accepts passes here.\n *\n * @param value - The value to test\n * @returns True if the value has the shape of a {@link JudgeAnswer}; false otherwise\n * @example\n * ```ts\n * isJudgeAnswer({ form: 'noul', noul: 0.9 }) // true\n * isJudgeAnswer({ form: 'choice', probabilities: { billing: 1 } }) // true\n * isJudgeAnswer({ form: 'noul', noul: 0.9, note: 'extra' }) // false\n * ```\n */\nexport function isJudgeAnswer(value: unknown): value is JudgeAnswer {\n\treturn holds(() => {\n\t\tif (!isRecord(value) || Object.keys(value).length !== 2) return false\n\t\tconst { form, noul, probabilities } = value\n\t\tif (form === 'noul') return isFiniteNumber(noul) && noul >= 0 && noul <= 1\n\t\tif (form === 'score') return arrayOf(isFiniteNumber)(probabilities)\n\t\treturn (\n\t\t\tform === 'choice' &&\n\t\t\tisRecord(probabilities) &&\n\t\t\tObject.values(probabilities).every(isFiniteNumber)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a positive safe integer.\n * @param value - The value to inspect\n * @returns True if the value is a positive safe integer; false otherwise\n * @example\n * ```ts\n * isPositiveSafeInteger(4096) // true\n * isPositiveSafeInteger(0) // false\n * ```\n */\nexport function isPositiveSafeInteger(value: unknown): value is number {\n\treturn isFiniteNumber(value) && Number.isSafeInteger(value) && value > 0\n}\n\n/**\n * Checks whether a value is a non-negative safe integer, `-0` included.\n *\n * @remarks\n * Total: a non-number, `NaN`, an infinity, a fraction, a negative number, and an integer past\n * `Number.MAX_SAFE_INTEGER` return false. It differs from `isNonNegativeInteger` in\n * `@orkestrel/contract` at two values: that guard rejects `-0` and admits an unsafe integer such\n * as `2 ** 53`, while this guard admits `-0` and rejects `2 ** 53`.\n *\n * @param value - The unknown count or limit candidate\n * @returns True if the value is a safe integer at or above zero; false otherwise\n * @example\n * ```ts\n * isNonNegativeSafeInteger(0) // true\n * isNonNegativeSafeInteger(-0) // true\n * isNonNegativeSafeInteger(2 ** 53) // false\n * isNonNegativeSafeInteger(1.5) // false\n * ```\n */\nexport function isNonNegativeSafeInteger(value: unknown): value is number {\n\treturn isFiniteNumber(value) && Number.isSafeInteger(value) && value >= 0\n}\n","/**\n * Names the opening tag a {@link import('./ThinkSplitter.js').ThinkSplitter} recognizes as the start of\n * an in-content reasoning span — `'<think>'`, the de-facto wire convention thinking models\n * (qwen3, DeepSeek-R1 family) emit their chain-of-thought under when a daemon renders it inline\n * instead of on a separate wire field. Paired with {@link THINK_CLOSE}.\n */\nexport const THINK_OPEN = '<think>'\n\n/**\n * Names the closing tag that ends a {@link THINK_OPEN} reasoning span — `'</think>'`. A span the\n * stream never closes (the model was cut off mid-reasoning) is treated as thinking to its end,\n * and {@link import('./types.js').ThinkSplitterInterface.flush} settles it.\n */\nexport const THINK_CLOSE = '</think>'\n\n/**\n * Holds the default provider deadline in milliseconds — `120_000`, the wall-clock bound a call\n * runs under when `AgentProviderInput.timeout` is omitted, folded with the caller's signal so\n * whichever trips first cancels the call.\n */\nexport const DEFAULT_PROVIDER_TIMEOUT = 120_000\n\n/**\n * Bounds the decoded error excerpt's input in bytes — `2048`, the leading bytes of a non-OK\n * response body handed to the decoder before the read cancels the remainder, so a `ProviderError`\n * message never carries a longer excerpt.\n */\nexport const MAX_ERROR_BODY_LENGTH = 2048\n\n/**\n * Holds the default relay request limit in bytes — `1_048_576`, the byte budget a relay applies\n * to an inbound body when `RelayOptions.limit` is omitted, refusing a body that reaches it.\n */\nexport const DEFAULT_RELAY_LIMIT = 1_048_576\n\n/**\n * Names the relay's newline-delimited JSON content type — `'application/x-ndjson; charset=utf-8'`,\n * the header a relay response carries beside `cache-control: no-store`.\n */\nexport const RELAY_CONTENT_TYPE = 'application/x-ndjson; charset=utf-8'\n\n/**\n * Names the public message for an unexpected upstream relay failure — `'relay provider failed'`,\n * the fixed text every `error` frame carries, so an upstream failure's own message never reaches\n * the browser.\n */\nexport const RELAY_PROVIDER_MESSAGE = 'relay provider failed'\n\n/**\n * Names the status a relay answers when the `authorize` callback returns anything but `true` or throws —\n * `401`, carried with no body and reaching the browser as a `ProviderError` with the `HTTP` code.\n */\nexport const UNAUTHORIZED_RELAY_STATUS = 401\n\n/**\n * Names the status a relay answers when the upstream provider call cannot be constructed — `502`,\n * carried with no body after `provider.stream` was entered and threw before returning its\n * iterator.\n */\nexport const UPSTREAM_RELAY_STATUS = 502\n\n/**\n * Names the status a relay answers for a body that is missing, unreadable, or rejected by\n * `providerRequestContract` — `400`, carried with no body and reaching the browser as a\n * `ProviderError` with the `HTTP` code.\n */\nexport const INVALID_RELAY_STATUS = 400\n\n/**\n * Names the status a relay answers for a request body at or above its byte budget — `413`,\n * carried with no body and answered for an aborted inbound read as well.\n */\nexport const OVERSIZED_RELAY_STATUS = 413\n\n/**\n * Names the status a relay answers for a page system message or tool definition its policy\n * refuses — `422`, carrying one {@link REFUSED_RELAY_CONTENT_TYPE} line per refused member and\n * reaching the browser as a `ProviderError` with the `HTTP` code.\n */\nexport const REFUSED_RELAY_STATUS = 422\n\n/**\n * Names the content type of a relay's `422` refusal body — `'text/plain; charset=utf-8'`, the\n * header a refusal carries beside `cache-control: no-store`.\n */\nexport const REFUSED_RELAY_CONTENT_TYPE = 'text/plain; charset=utf-8'\n\n/** Names the System One decision endpoint shared by compatible servers. */\nexport const SYSTEM_ONE_PATH = '/v1/systemone'\n","import type {\n\tJudgeErrorCode,\n\tProviderErrorCode,\n\tProviderErrorOptions,\n\tProviderResult,\n} from './types.js'\nimport {\n\tINTRINSICS,\n\tholds,\n\tisObject,\n\tisFunction,\n\tisNumber,\n\tisString,\n\tisBoolean,\n\tarrayOf,\n} from '@orkestrel/contract'\n\n/**\n * Reports a provider stream cancelled mid-flight by its bound signal — thrown by the\n * `stream` method of a {@link ProviderInterface}, carrying the {@link ProviderResult} assembled from\n * whatever streamed before the cancel and the machine-readable `code` `'ABORT'`.\n *\n * @remarks\n * Lets a caller recover the partial content (and any tool calls / usage seen so far)\n * on cancellation: `catch` the throw, narrow with {@link isProviderAbortError}, and\n * read `partial`. `code` is the machine-readable condition (`'ABORT'` — the only one this\n * error reports), so a `catch` branches on it rather than on the message string. `cause`\n * holds the failure the cancel superseded when a throw raced the abort — the wire decoder's\n * {@link ProviderError}, say — and is undefined when the cancel was the only failure.\n */\nexport class ProviderAbortError extends Error {\n\t/** Names the machine-readable condition — `'ABORT'`: a stream cancelled mid-flight. */\n\treadonly code = 'ABORT' as const\n\treadonly partial: ProviderResult\n\n\tconstructor(partial: ProviderResult, options?: ErrorOptions) {\n\t\tsuper('provider stream aborted', options)\n\t\tthis.name = 'ProviderAbortError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ProviderAbortError'), { value: this })\n\t\tthis.partial = partial\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link ProviderAbortError} through its identity brand and declared members, so a\n * `catch` can recover its `partial` result.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link ProviderAbortError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tfor await (const delta of provider.stream(messages, signal)) render(delta)\n * } catch (error) {\n * \tif (isProviderAbortError(error)) keep(error.partial.content) // recover partial\n * }\n * ```\n */\nexport function isProviderAbortError(value: unknown): value is ProviderAbortError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ProviderAbortError'))?.value ===\n\t\t\t\t\tvalue\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'ABORT')) return false\n\t\tconst partial = INTRINSICS.reflect.read(value, 'partial')\n\t\tif (!(isObject(partial) || isFunction(partial))) return false\n\t\tconst usage = INTRINSICS.reflect.read(partial, 'usage')\n\t\tconst tools = INTRINSICS.reflect.read(partial, 'tools')\n\t\treturn (\n\t\t\tisString(INTRINSICS.reflect.read(partial, 'content')) &&\n\t\t\t(INTRINSICS.reflect.read(partial, 'thinking') === undefined ||\n\t\t\t\tisString(INTRINSICS.reflect.read(partial, 'thinking'))) &&\n\t\t\t(INTRINSICS.reflect.read(partial, 'truncated') === undefined ||\n\t\t\t\tisBoolean(INTRINSICS.reflect.read(partial, 'truncated'))) &&\n\t\t\t(usage === undefined ||\n\t\t\t\t((isObject(usage) || isFunction(usage)) &&\n\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(usage, key)),\n\t\t\t\t\t))) &&\n\t\t\t(tools === undefined ||\n\t\t\t\tarrayOf(\n\t\t\t\t\t(tool: unknown) =>\n\t\t\t\t\t\t(isObject(tool) || isFunction(tool)) &&\n\t\t\t\t\t\tisString(INTRINSICS.reflect.read(tool, 'id')) &&\n\t\t\t\t\t\tisString(INTRINSICS.reflect.read(tool, 'name')) &&\n\t\t\t\t\t\t(isObject(INTRINSICS.reflect.read(tool, 'arguments')) ||\n\t\t\t\t\t\t\tisFunction(INTRINSICS.reflect.read(tool, 'arguments'))),\n\t\t\t\t)(tools))\n\t\t)\n\t})\n}\n\n/**\n * Reports a coded provider failure with its HTTP status and underlying cause when available.\n *\n * @remarks\n * The provider base throws this error for a non-OK HTTP response, a missing response\n * body, a stream that ended with no settled result and no terminal record, a result or delta the published contract refuses, and with code `'PROTOCOL'`\n * when the framing seam's `parse` or `finish` throws anything but a provider error, with the throw\n * as `cause`. Concrete wire decoders also use it for malformed records and relayed provider\n * failures.\n * `status` is present only for an `HTTP` failure; other codes leave it undefined.\n */\nexport class ProviderError extends Error {\n\t/** Names the machine-readable condition — `'HTTP'`: a non-OK response, including a relay refusing an oversized request with 413; `'PROTOCOL'`: a missing response body, a malformed wire record, or a strict stream with no settled result; `'PROVIDER'`: an upstream failure carried by a relay error record. */\n\treadonly code: ProviderErrorCode\n\t/** Holds the response status for an HTTP failure, or undefined for other codes. */\n\treadonly status: number | undefined\n\t/** Holds what the refusal found, such as its faults, or undefined when it carries none. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\tconstructor(code: ProviderErrorCode, message: string, options?: ProviderErrorOptions) {\n\t\tsuper(message, options)\n\t\tthis.name = 'ProviderError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ProviderError'), { value: this })\n\t\tthis.code = code\n\t\tthis.status = options?.status\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows a caught value to the provider failure class through its identity brand and declared members.\n *\n * @param value - The caught value\n * @returns True if the value is a provider failure; false otherwise\n * @example\n * ```ts\n * isProviderError(new ProviderError('HTTP', 'unavailable', { status: 503 })) // true\n * ```\n */\nexport function isProviderError(value: unknown): value is ProviderError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ProviderError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'HTTP' || code === 'PROTOCOL' || code === 'PROVIDER')) return false\n\t\tconst status = INTRINSICS.reflect.read(value, 'status')\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn (\n\t\t\t(status === undefined || isNumber(status)) &&\n\t\t\t(context === undefined || isObject(context) || isFunction(context))\n\t\t)\n\t})\n}\n\n/**\n * Reports a coded judge failure with its HTTP status and underlying cause when available.\n *\n * @remarks\n * The judge engine throws this error for a non-OK HTTP response, a missing or unparsable\n * response body, and a request refused before inference. Concrete wire decoders also use it for\n * a response they cannot read and for a wire limit. `status` is present only for an `HTTP`\n * failure; other codes leave it undefined.\n */\nexport class JudgeError extends Error {\n\t/** Names the machine-readable condition — `'HTTP'`: a non-OK response; `'PROTOCOL'`: a missing, unparsable, or unreadable response body; `'QUESTION'`: a request refused before inference. */\n\treadonly code: JudgeErrorCode\n\t/** Holds the response status for an HTTP failure, or undefined for other codes. */\n\treadonly status: number | undefined\n\t/** Holds the values that locate the failure, such as the `faults` of a refused question, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\tconstructor(code: JudgeErrorCode, message: string, options?: ProviderErrorOptions) {\n\t\tsuper(message, options)\n\t\tthis.name = 'JudgeError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.JudgeError'), { value: this })\n\t\tthis.code = code\n\t\tthis.status = options?.status\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows a caught value to the judge failure class through its identity brand and declared members.\n *\n * @param value - The caught value\n * @returns True if the value is a {@link JudgeError}; false otherwise\n * @example\n * ```ts\n * isJudgeError(new JudgeError('HTTP', 'judge error: 429', { status: 429 })) // true\n * ```\n */\nexport function isJudgeError(value: unknown): value is JudgeError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.JudgeError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'HTTP' || code === 'PROTOCOL' || code === 'QUESTION')) return false\n\t\tconst status = INTRINSICS.reflect.read(value, 'status')\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn (\n\t\t\t(status === undefined || isNumber(status)) &&\n\t\t\t(context === undefined || isObject(context) || isFunction(context))\n\t\t)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tbooleanShape,\n\tjsonShape,\n\tliteralShape,\n\tobjectShape,\n\toptionalShape,\n\trecordShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\nimport { messageShape, tokenUsageShape, toolCallShape } from '../shapers.js'\n\n/**\n * Describes a provider request's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than `ProviderRequest`: non-JSON arguments, parameters,\n * or schema members are refused. Execution context stays local; the guard refuses\n * extra members, and the parser drops them.\n */\nexport const providerRequestShape = objectShape({\n\tmessages: arrayShape(messageShape),\n\ttools: optionalShape(\n\t\tarrayShape(\n\t\t\tobjectShape({\n\t\t\t\tname: stringShape(),\n\t\t\t\tdescription: optionalShape(stringShape()),\n\t\t\t\tparameters: optionalShape(recordShape(jsonShape())),\n\t\t\t}),\n\t\t),\n\t),\n\toptions: optionalShape(\n\t\tobjectShape({\n\t\t\tthink: optionalShape(booleanShape()),\n\t\t\tschema: optionalShape(recordShape(jsonShape())),\n\t\t}),\n\t),\n}) satisfies ContractShape\n\n/**\n * Describes a provider result's JSON wire projection.\n *\n * @remarks\n * The wire is strictly narrower than `ProviderResult`: non-JSON call arguments are\n * refused. Execution context stays local; the guard refuses extra members, and the parser drops them.\n */\nexport const providerResultShape = objectShape({\n\tcontent: stringShape(),\n\tthinking: optionalShape(stringShape()),\n\ttools: optionalShape(arrayShape(toolCallShape)),\n\tusage: optionalShape(tokenUsageShape),\n\ttruncated: optionalShape(booleanShape()),\n}) satisfies ContractShape\n\n/**\n * Describes the channel-discriminated JSON relay wire projection.\n *\n * @remarks\n * The wire is strictly narrower than `RelayFrame` through its `result` and `partial`\n * fields: non-JSON arguments are refused. Execution context stays local; the guard\n * refuses extra members, and the parser drops them.\n */\nexport const relayFrameShape = unionShape(\n\tobjectShape({ channel: literalShape(['content', 'thinking']), text: stringShape() }),\n\tobjectShape({ channel: literalShape(['result']), result: providerResultShape }),\n\tobjectShape({ channel: literalShape(['abort']), partial: providerResultShape }),\n\tobjectShape({ channel: literalShape(['error']), message: stringShape() }),\n) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { providerRequestShape, providerResultShape, relayFrameShape } from './shapers.js'\n\n/** Validates and projects provider requests at a JSON wire boundary. */\nexport const providerRequestContract = createContract(providerRequestShape)\n\n/** Validates and projects provider results at a JSON wire boundary. */\nexport const providerResultContract = createContract(providerResultShape)\n\n/** Validates and projects channel-discriminated relay frames at a JSON wire boundary. */\nexport const relayFrameContract = createContract(relayFrameShape)\n","import type { ProviderResult, SystemOneAnswer, SystemOneResponse } from './types.js'\nimport type { Guard } from '@orkestrel/contract'\nimport { isBudgetAmount } from '@orkestrel/budget'\nimport {\n\tarrayOf,\n\tattempt,\n\tboundsOf,\n\tisArray,\n\tisBoolean,\n\tisNumber,\n\tisRecord,\n\tisString,\n\tnullableOf,\n\tobjectOf,\n\toptionalOf,\n} from '@orkestrel/contract'\nimport { isToolCall } from '@orkestrel/tool'\n\n/**\n * Checks whether a value carries the members {@link ProviderResult} publishes: a string `content`, and\n * `thinking`, `tools`, `usage`, and `truncated` absent or of their published types.\n *\n * @remarks\n * A provider is a foreign interface, so the guard is open: unknown members and a class instance pass,\n * and each `usage` count is a finite nonnegative number with no integer check. An unreadable member\n * returns false. {@link import('./helpers.js').requireProviderResult} runs it on an owned copy, the one\n * door a run's result crosses.\n *\n * @param value - The unknown result candidate\n * @returns True if the value satisfies the published result; false otherwise\n *\n * @example\n * ```ts\n * isProviderResult({ content: 'answer', extra: true }) // true\n * isProviderResult({}) // false\n * isProviderResult({ content: 'answer', usage: { prompt: -1, completion: 0, total: 0 } }) // false\n * ```\n */\nexport const isProviderResult: Guard<ProviderResult> = objectOf(\n\t{\n\t\tcontent: isString,\n\t\tthinking: isString,\n\t\ttools: arrayOf(isToolCall),\n\t\tusage: objectOf({ prompt: isBudgetAmount, completion: isBudgetAmount, total: isBudgetAmount }),\n\t\ttruncated: isBoolean,\n\t},\n\t['thinking', 'tools', 'usage', 'truncated'],\n)\n\n/**\n * Checks whether a value is a System One response envelope with optional model and usage.\n *\n * @remarks\n * Answers remain unknown until checked against their questions. Missing and null usage counts\n * are accepted. Extra members are ignored, and unreadable fields return false.\n *\n * @param value - The unknown response candidate\n * @returns True if the envelope fields have their wire types; false otherwise\n * @example\n * ```ts\n * isSystemOneResponse({ answers: {}, usage: { input_tokens: null } }) // true\n * ```\n */\nexport function isSystemOneResponse(value: unknown): value is SystemOneResponse {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { model, answers, usage } = value\n\t\tif (!optionalOf(isString)(model) || !isRecord(answers)) return false\n\t\tif (usage === undefined) return true\n\t\tif (!isRecord(usage)) return false\n\t\treturn (\n\t\t\toptionalOf(nullableOf(isNumber))(usage.input_tokens) &&\n\t\t\toptionalOf(nullableOf(isNumber))(usage.output_tokens)\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a value is a System One answer whose type and distribution the wire can read.\n *\n * @remarks\n * The guard checks `type` and the distribution the wire dereferences: the `noul` number for a\n * noul, the `probabilities` map for a choice, and the `probabilities` map or dense array for a\n * score, each probability a finite number in [0, 1]. The `choice`, `score`, `confidence`, and\n * `legend` members are carried unchecked, because the wire never reads them, so a value this\n * guard accepts can hold any value in those members. Distribution sums are not constrained;\n * values are never normalized. A read that throws returns false.\n *\n * @param value - The unknown answer candidate\n * @returns True if the type and the distribution satisfy the wire contract; false otherwise\n * @example\n * ```ts\n * isSystemOneAnswer({ type: 'noul', noul: 0.9 }) // true\n * isSystemOneAnswer({ type: 'score', probabilities: [0.2, 0.8] }) // true\n * isSystemOneAnswer({ type: 'noul', noul: 1.1 }) // false\n * ```\n */\nexport function isSystemOneAnswer(value: unknown): value is SystemOneAnswer {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { type } = value\n\t\tif (type === 'noul') return boundsOf(0, 1)(value.noul)\n\t\tif (type !== 'choice' && type !== 'score') return false\n\t\tconst { probabilities } = value\n\t\tif (type === 'score' && isArray(probabilities)) {\n\t\t\treturn arrayOf(boundsOf(0, 1))(probabilities)\n\t\t}\n\t\treturn isRecord(probabilities) && Object.values(probabilities).every(boundsOf(0, 1))\n\t})\n\treturn checked.success && checked.value\n}\n","import type {\n\tAnswerMeasure,\n\tFailureRead,\n\tProviderDelta,\n\tProviderOptions,\n\tProviderRequest,\n\tProviderResult,\n\tRelayOptions,\n\tRelayRefusal,\n\tSystemOneAnswer,\n\tSystemOneQuestion,\n\tSystemOneUsage,\n\tTextRead,\n} from './types.js'\nimport type { JudgeAnswer, JudgeQuestion, JudgeResult, Message, Refusal } from '../types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport type { Result } from '@orkestrel/contract'\nimport type { ToolCall, ToolDefinition } from '@orkestrel/tool'\nimport { isTokenUsage } from '@orkestrel/budget'\nimport {\n\tattempt,\n\tboundsOf,\n\tcanonicalStringify,\n\tfailure,\n\tisArray,\n\tisFunction,\n\tisNumber,\n\tisObject,\n\tpreview,\n\tsuccess,\n} from '@orkestrel/contract'\nimport { JudgeError, ProviderError } from './errors.js'\nimport { MAX_ERROR_BODY_LENGTH } from './constants.js'\nimport { isProviderResult } from './validators.js'\nimport { sanitizeUsage, sumUsage } from '../helpers.js'\n\n/**\n * Assembles a provider result with only populated optional fields.\n *\n * @param content - The authoritative answer\n * @param thinking - The joined reasoning\n * @param tools - The accumulated calls\n * @param usage - The reported token usage\n * @param truncated - Whether the model stopped at its own limit, or undefined when the wire did not report it\n * @returns The assembled result\n * @example\n * ```ts\n * buildProviderResult('ok', '', [], undefined, undefined) // { content: 'ok' }\n * ```\n */\nexport function buildProviderResult(\n\tcontent: string,\n\tthinking: string,\n\ttools: readonly ToolCall[],\n\tusage: TokenUsage | undefined,\n\ttruncated: boolean | undefined,\n): ProviderResult {\n\treturn {\n\t\tcontent,\n\t\t...(thinking.length === 0 ? {} : { thinking }),\n\t\t...(tools.length === 0 ? {} : { tools }),\n\t\t...(usage === undefined ? {} : { usage }),\n\t\t...(truncated === undefined ? {} : { truncated }),\n\t}\n}\n\n/**\n * Reads a foreign delta's two members once into an owned copy, the value a run checks and consumes.\n *\n * @remarks\n * A stream checks every delta, so this is a hand read rather than a record guard. Reading each member\n * once means a getter can't pass the check with one value and hand the run another. Unknown members\n * and a class instance pass, and an unreadable member reads as no delta.\n *\n * @param value - The value the stream yielded\n * @returns The owned delta, or undefined when the value is not one\n *\n * @example\n * ```ts\n * readProviderDelta({ channel: 'content', text: 'Hi' }) // { channel: 'content', text: 'Hi' }\n * readProviderDelta({ channel: 'audio', text: 'Hi' }) // undefined\n * ```\n */\nexport function readProviderDelta(value: unknown): ProviderDelta | undefined {\n\tif (typeof value !== 'object' || value === null) return undefined\n\ttry {\n\t\tconst { channel, text }: { readonly channel?: unknown; readonly text?: unknown } = value\n\t\treturn (channel === 'content' || channel === 'thinking') && typeof text === 'string'\n\t\t\t? { channel, text }\n\t\t\t: undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * Owns a provider's settled result: reads each published member of the foreign value once into a fresh\n * record, checks that copy, and returns it, or throws a coded error the run rejects with.\n *\n * @remarks\n * The check is {@link isProviderResult}, so a class instance carrying the published members passes and\n * an unknown member is left behind. `usage` is copied count by count; `tools` keeps the array the\n * provider returned. An absent `usage` stays absent, which the loop bills as its estimate.\n *\n * @param value - The value a provider's stream returned\n * @returns The owned result\n * @throws {ProviderError} Thrown when the value does not carry the published members (code `PROTOCOL`)\n *\n * @example\n * ```ts\n * requireProviderResult({ content: 'answer', extra: true }) // { content: 'answer' }\n * requireProviderResult({}) // throws ProviderError ('PROTOCOL')\n * ```\n */\nexport function requireProviderResult(value: unknown): ProviderResult {\n\tconst owned = attempt(() => {\n\t\tif (!isObject(value) && !isFunction(value)) return undefined\n\t\tconst thinking = Reflect.get(value, 'thinking')\n\t\tconst tools = Reflect.get(value, 'tools')\n\t\tconst usage = Reflect.get(value, 'usage')\n\t\tconst truncated = Reflect.get(value, 'truncated')\n\t\treturn {\n\t\t\tcontent: Reflect.get(value, 'content'),\n\t\t\t...(thinking === undefined ? {} : { thinking }),\n\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t...(usage === undefined\n\t\t\t\t? {}\n\t\t\t\t: {\n\t\t\t\t\t\tusage: isObject(usage)\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tprompt: Reflect.get(usage, 'prompt'),\n\t\t\t\t\t\t\t\t\tcompletion: Reflect.get(usage, 'completion'),\n\t\t\t\t\t\t\t\t\ttotal: Reflect.get(usage, 'total'),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: usage,\n\t\t\t\t\t}),\n\t\t\t...(truncated === undefined ? {} : { truncated }),\n\t\t}\n\t})\n\tif (owned.success && isProviderResult(owned.value)) return owned.value\n\tthrow new ProviderError(\n\t\t'PROTOCOL',\n\t\t`provider error: a provider must return string content, with thinking, tools, usage, and truncated absent or of their published types, and usage counts finite and nonnegative; received ${preview(owned.success ? owned.value : value)}`,\n\t\towned.success ? undefined : { cause: owned.error },\n\t)\n}\n\n/**\n * Cancels a stream reader and releases its lock, swallowing a cancellation failure so the\n * caller's own outcome stands.\n *\n * @param reader - The reader to cancel and release\n * @returns A promise that settles after the lock is released\n *\n * @example\n * ```ts\n * const body = new Response('answer').body\n * if (body !== null) await releaseReader(body.getReader())\n * ```\n */\nexport async function releaseReader(\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n): Promise<void> {\n\ttry {\n\t\tawait reader.cancel()\n\t} catch {\n\t\t// Preserve the caller's outcome when the source refuses cancellation.\n\t} finally {\n\t\treader.releaseLock()\n\t}\n}\n\n/**\n * Arms signal propagation to a byte reader and returns the controller that removes the listener.\n *\n * @param reader - The reader whose pending read an abort releases\n * @param signal - The optional caller signal\n * @returns The listener cleanup controller after any already-aborted cancellation settles\n * @example\n * ```ts\n * const reader = new ReadableStream<Uint8Array>().getReader()\n * const cleanup = await armReaderAbort(reader, new AbortController().signal)\n * cleanup.abort()\n * await releaseReader(reader)\n * ```\n */\nexport async function armReaderAbort(\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n\tsignal: AbortSignal | undefined,\n): Promise<AbortController> {\n\tconst cleanup = new AbortController()\n\tsignal?.addEventListener(\n\t\t'abort',\n\t\t() => {\n\t\t\tvoid reader.cancel(signal.reason).catch(() => {\n\t\t\t\t// Preserve the caller's outcome when source cancellation rejects.\n\t\t\t})\n\t\t},\n\t\t{ once: true, signal: cleanup.signal },\n\t)\n\tif (signal?.aborted) {\n\t\tawait reader.cancel(signal.reason).catch(() => {\n\t\t\t// An already-aborted read retains the same cancellation contract.\n\t\t})\n\t\tcleanup.abort()\n\t}\n\treturn cleanup\n}\n\n/**\n * Reads a bounded HTTP failure excerpt and preserves an unreadable body's cause.\n *\n * @remarks\n * Both HTTP engines, the provider and the judge, translate a non-OK response here and throw\n * their own coded error from the result. When the excerpt is read and `signal` has aborted, the\n * abort is the call's outcome, so the signal's reason is thrown. When the body read fails, the\n * failure is returned as `cause` whether or not `signal` has aborted, so a read that raced the\n * abort stays recorded on the error the caller throws.\n *\n * @param response - The failed HTTP response\n * @param prefix - The error taxonomy prefix, including its trailing colon\n * @param signal - The call's combined caller and deadline signal\n * @returns The status message with any excerpt, the response status, and any body-read failure\n * @throws Thrown when the excerpt is read after `signal` aborts, with the signal's reason\n * @example\n * ```ts\n * await readFailure(new Response('unavailable', { status: 503 }), 'provider error:', new AbortController().signal)\n * // { message: 'provider error: 503 - unavailable', status: 503 }\n * ```\n */\nexport async function readFailure(\n\tresponse: Response,\n\tprefix: string,\n\tsignal: AbortSignal,\n): Promise<FailureRead> {\n\tconst status = response.status\n\tconst body = response.body\n\tif (body === null) {\n\t\tsignal.throwIfAborted()\n\t\treturn { message: `${prefix} ${status}`, status }\n\t}\n\treturn readBodyText(body, MAX_ERROR_BODY_LENGTH, signal).then(\n\t\t(read) => {\n\t\t\tsignal.throwIfAborted()\n\t\t\treturn { message: `${prefix} ${status}${read.text === '' ? '' : ` - ${read.text}`}`, status }\n\t\t},\n\t\t(cause: unknown) => ({\n\t\t\tmessage: `${prefix} ${status} - (error body unavailable)`,\n\t\t\tstatus,\n\t\t\t...(cause === undefined ? {} : { cause }),\n\t\t}),\n\t)\n}\n\n/**\n * Reads a UTF-8 prefix of a byte stream and cancels its remainder.\n *\n * @remarks\n * The `limit` parameter bounds bytes passed to the decoder, including a partial final\n * character. An omitted limit reads to completion. A source may deliver a chunk larger\n * than the remaining limit; its unused bytes are discarded without decoding.\n * The read can overshoot by one source chunk. An abort leaves completion false.\n *\n * @param body - The readable byte stream\n * @param limit - The maximum byte prefix, or undefined for the complete stream\n * @param signal - The optional cancellation bound; abort returns the decoded prefix\n * @returns The decoded prefix and whether EOF occurred within the byte budget\n * @example\n * ```ts\n * import { readBodyText } from '@orkestrel/agent'\n *\n * const body = new Response('answer').body\n * if (body !== null) await readBodyText(body, 3) // { text: 'ans', complete: false }\n * ```\n */\nexport async function readBodyText(\n\tbody: ReadableStream<Uint8Array>,\n\tlimit?: number,\n\tsignal?: AbortSignal,\n): Promise<TextRead> {\n\tconst reader = body.getReader()\n\tconst decoder = new TextDecoder()\n\ttry {\n\t\tconst cleanup = await armReaderAbort(reader, signal)\n\t\ttry {\n\t\t\t// Each step's decoded text and the byte budget left after it: the next read's budget is the\n\t\t\t// latest step's, and the text joins every step.\n\t\t\tconst steps: Array<{ readonly text: string; readonly remaining: number }> = []\n\t\t\tfor (;;) {\n\t\t\t\tconst remaining = steps.at(-1)?.remaining ?? limit ?? Infinity\n\t\t\t\tconst step = remaining > 0 && signal?.aborted !== true ? await reader.read() : undefined\n\t\t\t\tif (step === undefined || signal?.aborted === true || step.done) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttext: steps.map((one) => one.text).join('') + decoder.decode(),\n\t\t\t\t\t\tcomplete: step?.done === true && signal?.aborted !== true,\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst bytes = step.value.subarray(0, remaining)\n\t\t\t\tsteps.push({\n\t\t\t\t\ttext: decoder.decode(bytes, { stream: true }),\n\t\t\t\t\tremaining: remaining - bytes.byteLength,\n\t\t\t\t})\n\t\t\t}\n\t\t} finally {\n\t\t\tcleanup.abort()\n\t\t}\n\t} finally {\n\t\tawait releaseReader(reader)\n\t}\n}\n\n/**\n * Decodes UTF-8 chunks with a final flush and releases the stream on every exit.\n *\n * @param body - The readable byte stream\n * @param signal - The optional cancellation bound; abort ends iteration without further chunks\n * @returns Decoded text chunks, including a held decoder tail\n * @example\n * ```ts\n * const body = new Response('answer').body\n * if (body !== null) for await (const chunk of readChunks(body)) render(chunk)\n * ```\n */\nexport async function* readChunks(\n\tbody: ReadableStream<Uint8Array>,\n\tsignal?: AbortSignal,\n): AsyncGenerator<string> {\n\tconst reader = body.getReader()\n\tconst decoder = new TextDecoder()\n\ttry {\n\t\tconst cleanup = await armReaderAbort(reader, signal)\n\t\ttry {\n\t\t\tif (signal?.aborted) return\n\t\t\tfor (;;) {\n\t\t\t\tconst step = await reader.read()\n\t\t\t\tif (signal?.aborted) return\n\t\t\t\tif (step.done) break\n\t\t\t\tconst chunk = decoder.decode(step.value, { stream: true })\n\t\t\t\tif (chunk.length > 0) yield chunk\n\t\t\t}\n\t\t\tconst tail = decoder.decode()\n\t\t\tif (tail.length > 0) yield tail\n\t\t} finally {\n\t\t\tcleanup.abort()\n\t\t}\n\t} finally {\n\t\tawait releaseReader(reader)\n\t}\n}\n\n/**\n * Derives the winner, its probability, the published confidence, and a score answer's expected\n * level from a judge answer's distribution.\n *\n * @remarks\n * The winner is the first strictly greatest candidate in enumeration order: an option name in\n * criteria order for a fresh answer and in the order its store returned for a restored record,\n * a level index, or `'false'` then `'true'` for a noul, so a noul of exactly 0.5\n * names `'false'`. Confidence follows the TypeSafe formulas, whatever confidence a server sent: a\n * choice reads `(max(p) - 1/n) / (1 - 1/n)`, a noul reads `|2p - 1|` (the choice formula at\n * n = 2), and a score reads `max(0, 1 - spread / even_spread)`, where `spread` sums each level's\n * probability times its distance from the winning level and `even_spread` is the mean distance\n * from the middle level. `score` is the expected level `sum(i * p_i)` and is set only for a score\n * answer.\n *\n * @param answer - The answer whose distribution is read\n * @returns The derived measures\n * @throws JudgeError Thrown when a choice or score answer has fewer than 2 candidates\n * (code `PROTOCOL`), because the confidence formulas divide by the candidate count\n * @example\n * ```ts\n * measureAnswer({ form: 'choice', probabilities: { billing: 0.88, technical: 0.12, sales: 0 } })\n * // { winner: 'billing', probability: 0.88, confidence: 0.82 } to 2 decimals\n * measureAnswer({ form: 'score', probabilities: [0, 0.57, 0.43] })\n * // { winner: '1', probability: 0.57, confidence: 0.355, score: 1.43 } to 3 decimals\n * measureAnswer({ form: 'noul', noul: 0.5 }) // { winner: 'false', probability: 0.5, confidence: 0 }\n * ```\n */\nexport function measureAnswer(answer: JudgeAnswer): AnswerMeasure {\n\tif (answer.form === 'noul') {\n\t\tconst yes = answer.noul > 0.5\n\t\treturn {\n\t\t\twinner: yes ? 'true' : 'false',\n\t\t\tprobability: yes ? answer.noul : 1 - answer.noul,\n\t\t\tconfidence: Math.abs(2 * answer.noul - 1),\n\t\t}\n\t}\n\tconst candidates: ReadonlyArray<readonly [string, number]> =\n\t\tanswer.form === 'choice'\n\t\t\t? Object.entries(answer.probabilities)\n\t\t\t: answer.probabilities.map((probability, level) => [String(level), probability])\n\tconst count = candidates.length\n\tif (count < 2) {\n\t\tthrow new JudgeError('PROTOCOL', 'judge error: an answer needs at least 2 candidates')\n\t}\n\t// The first strictly greatest candidate wins a tie. The spread visits a hole in a score\n\t// distribution as an undefined candidate, which the destructuring refuses; `reduce` skips it.\n\tconst { winner, index, probability } = [...candidates].reduce(\n\t\t(top, [name, candidate], position) =>\n\t\t\tcandidate > top.probability ? { winner: name, index: position, probability: candidate } : top,\n\t\t{ winner: '', index: 0, probability: -Infinity },\n\t)\n\tif (answer.form === 'choice') {\n\t\treturn { winner, probability, confidence: (probability - 1 / count) / (1 - 1 / count) }\n\t}\n\tconst spread = answer.probabilities.reduce(\n\t\t(sum, candidate, level) => sum + candidate * Math.abs(level - index),\n\t\t0,\n\t)\n\tconst even = answer.probabilities.reduce(\n\t\t(sum, _candidate, level) => sum + Math.abs(level - (count - 1) / 2),\n\t\t0,\n\t)\n\tconst score = answer.probabilities.reduce((sum, candidate, level) => sum + level * candidate, 0)\n\treturn { winner, probability, confidence: Math.max(0, 1 - spread / (even / count)), score }\n}\n\n/**\n * Merges the results of a judge request's calls into one result.\n *\n * @remarks\n * Pure and total. Answers and refusals are joined by question id; `refusals` is omitted when no\n * call refused a question. Each call's usage passes through {@link sanitizeUsage} and the totals\n * add through {@link sumUsage}; `usage` is omitted when no call reported one. The model is the\n * first call's, or the given model when the list is empty, as the empty partial of a cancel\n * before the first call requires.\n *\n * @param model - The judge's configured model, reported when no call completed\n * @param results - The completed calls' results in call order\n * @returns The merged result\n * @example\n * ```ts\n * buildJudgeResult('tev1:0.8b', []) // { model: 'tev1:0.8b', answers: {} }\n * buildJudgeResult('jev-latest', [\n * \t{ model: 'jev-1.13.0', answers: { urgent: { form: 'noul', noul: 0.95 } } },\n * \t{ model: 'jev-1.13.0', answers: {}, refusals: { team: { missing: ['sales'] } } },\n * ])\n * // { model: 'jev-1.13.0', answers: { urgent: ... }, refusals: { team: { missing: ['sales'] } } }\n * ```\n */\nexport function buildJudgeResult(model: string, results: readonly JudgeResult[]): JudgeResult {\n\t// The spread visits a hole in `results` as an undefined result, as iteration does; `reduce` skips it.\n\tconst calls = [...results]\n\tconst answers = calls.reduce<Readonly<Record<string, JudgeAnswer>>>(\n\t\t(merged, result) => ({ ...merged, ...result.answers }),\n\t\t{},\n\t)\n\tconst refusals = calls.reduce<Readonly<Record<string, Refusal>>>(\n\t\t(merged, result) => ({ ...merged, ...result.refusals }),\n\t\t{},\n\t)\n\tconst usage = calls.reduce<TokenUsage | undefined>(\n\t\t(total, result) =>\n\t\t\tresult.usage === undefined ? total : sumUsage(total, sanitizeUsage(result.usage)),\n\t\tundefined,\n\t)\n\treturn {\n\t\tmodel: results[0]?.model ?? model,\n\t\tanswers,\n\t\t...(Object.keys(refusals).length === 0 ? {} : { refusals }),\n\t\t...(usage === undefined ? {} : { usage }),\n\t}\n}\n\n/**\n * Builds a request's JSON headers, awaiting the caller's header hook inside the call's\n * cancellation bound.\n *\n * @remarks\n * The headers start with `Content-Type: application/json`; each entry the hook returns is set\n * over them, so the hook overrides the content type only when it returns that header. The hook\n * receives `signal` and races its abort, and the abort listener is removed on every exit. Both\n * HTTP engines, the provider and the judge, read their headers here.\n *\n * @param hook - The caller's header hook, or undefined for the JSON content type alone\n * @param signal - The call's combined caller and deadline signal\n * @returns The request headers\n * @throws Thrown when the signal aborts before the hook settles, with the signal's reason, and\n * when the hook throws or rejects, with the hook's own failure\n * @example\n * ```ts\n * const signal = new AbortController().signal\n * // API_KEY stands for the server's bearer key.\n * const headers = await buildProviderHeaders(() => ({ authorization: 'Bearer API_KEY' }), signal)\n * headers.get('authorization') // 'Bearer API_KEY'\n * headers.get('content-type') // 'application/json'\n * ```\n */\nexport async function buildProviderHeaders(\n\thook: ProviderOptions['headers'],\n\tsignal: AbortSignal,\n): Promise<Headers> {\n\tconst headers = new Headers({ 'Content-Type': 'application/json' })\n\tif (hook === undefined) return headers\n\tconst cleanup = new AbortController()\n\tconst aborted = Promise.withResolvers<never>()\n\tsignal.addEventListener('abort', () => aborted.reject(signal.reason), {\n\t\tonce: true,\n\t\tsignal: cleanup.signal,\n\t})\n\ttry {\n\t\tsignal.throwIfAborted()\n\t\tconst entries = await Promise.race([\n\t\t\tPromise.resolve().then(() => hook(signal)),\n\t\t\taborted.promise,\n\t\t])\n\t\tfor (const [key, value] of Object.entries(entries)) headers.set(key, value)\n\t\treturn headers\n\t} finally {\n\t\tcleanup.abort()\n\t}\n}\n\n/**\n * Projects a provider request onto the declared members the relay wire carries.\n *\n * @remarks\n * Each message keeps `id`, `role`, `content`, and any `calls`, `call`, `images`, and `thinking`;\n * each call keeps only `id`, `name`, and `arguments`, so a local execution context never reaches\n * the wire. Each tool keeps `name` and any `description` and `parameters`, and the options keep\n * any `think` and `schema`. An absent member stays absent. The projection shares every nested\n * value with the input rather than copying it, so the caller snapshots and validates the result;\n * a throwing getter propagates its failure.\n *\n * @param request - The domain conversation and call configuration\n * @returns The request with only the members the relay wire declares\n * @example\n * ```ts\n * requestToRelay({\n * \tmessages: [{ id: 'turn-1', role: 'user', content: 'Track BW-5512' }],\n * \toptions: { think: false },\n * })\n * // { messages: [{ id: 'turn-1', role: 'user', content: 'Track BW-5512' }], options: { think: false } }\n * ```\n */\nexport function requestToRelay(request: ProviderRequest): ProviderRequest {\n\treturn {\n\t\tmessages: request.messages.map((message) => ({\n\t\t\tid: message.id,\n\t\t\trole: message.role,\n\t\t\tcontent: message.content,\n\t\t\t...(message.calls === undefined\n\t\t\t\t? {}\n\t\t\t\t: {\n\t\t\t\t\t\tcalls: message.calls.map((call) => ({\n\t\t\t\t\t\t\tid: call.id,\n\t\t\t\t\t\t\tname: call.name,\n\t\t\t\t\t\t\targuments: call.arguments,\n\t\t\t\t\t\t})),\n\t\t\t\t\t}),\n\t\t\t...(message.call === undefined ? {} : { call: message.call }),\n\t\t\t...(message.images === undefined ? {} : { images: message.images }),\n\t\t\t...(message.thinking === undefined ? {} : { thinking: message.thinking }),\n\t\t})),\n\t\t...(request.tools === undefined\n\t\t\t? {}\n\t\t\t: {\n\t\t\t\t\ttools: request.tools.map((tool) => ({\n\t\t\t\t\t\tname: tool.name,\n\t\t\t\t\t\t...(tool.description === undefined ? {} : { description: tool.description }),\n\t\t\t\t\t\t...(tool.parameters === undefined ? {} : { parameters: tool.parameters }),\n\t\t\t\t\t})),\n\t\t\t\t}),\n\t\t...(request.options === undefined\n\t\t\t? {}\n\t\t\t: {\n\t\t\t\t\toptions: {\n\t\t\t\t\t\t...(request.options.think === undefined ? {} : { think: request.options.think }),\n\t\t\t\t\t\t...(request.options.schema === undefined ? {} : { schema: request.options.schema }),\n\t\t\t\t\t},\n\t\t\t\t}),\n\t}\n}\n\n/**\n * Builds the request a relay forwards under its system turn and tool definition policy, or every\n * refusal that policy finds.\n *\n * @remarks\n * One pass collects every refused member. A page `system` message is refused unless\n * `policy.system.accept` is `true`; under it, only one content-only page message at index 0 is\n * admitted. When `policy.system.text` is defined, the forwarded request starts with one composed\n * system message whose content is that text, joined with `'\\n\\n'` before an admitted page head's\n * content, and the page head is removed; when it is undefined, an admitted page head is forwarded\n * unchanged. A page tool definition of a declared name must equal the declared one after both are\n * projected as {@link requestToRelay} projects them, key order ignored, and the declared copy is\n * forwarded; a definition of an undeclared name is forwarded only when `policy.tools.accept` is\n * `true`; each occurrence of a name after its first is refused. The forwarded tools keep the\n * page's order and stay absent when the page sent none; the messages after the head and the\n * options pass unchanged. The helper is deterministic except the composed head's id, which takes\n * `crypto.randomUUID()`.\n *\n * @param request - The page request the relay contract admitted\n * @param policy - The relay's system turn and tool definition policy\n * @returns The request to forward, or every refusal in message then tool order\n * @throws {ContractError} Thrown when comparing a page definition with the declared one of its\n * name reads a `parameters` member that throws, such as a getter or a `Proxy` trap, which parsed\n * JSON never has\n * @example\n * ```ts\n * const built = buildRelayRequest(\n * \t{ messages: [{ id: 'u1', role: 'user', content: 'Track BW-5512' }] },\n * \t{ system: { text: 'Answer for the shipping desk.' } },\n * )\n * built.success && built.value.messages[0]?.content // 'Answer for the shipping desk.'\n * ```\n */\nexport function buildRelayRequest(\n\trequest: ProviderRequest,\n\tpolicy: Pick<RelayOptions, 'system' | 'tools'>,\n): Result<ProviderRequest, readonly RelayRefusal[]> {\n\tconst refusals: RelayRefusal[] = []\n\tconst admits = policy.system?.accept === true\n\tfor (const [index, message] of request.messages.entries()) {\n\t\tif (message.role !== 'system') continue\n\t\tif (!admits) {\n\t\t\trefusals.push({ reason: 'system', index })\n\t\t\tcontinue\n\t\t}\n\t\tif (\n\t\t\tindex !== 0 ||\n\t\t\tmessage.thinking !== undefined ||\n\t\t\tmessage.calls !== undefined ||\n\t\t\tmessage.call !== undefined ||\n\t\t\tmessage.images !== undefined\n\t\t) {\n\t\t\trefusals.push({ reason: 'head', index })\n\t\t}\n\t}\n\tconst declared = new Map<string, ToolDefinition>()\n\tconst definitions = policy.tools?.definitions ?? []\n\tfor (const tool of requestToRelay({ messages: [], tools: definitions }).tools ?? []) {\n\t\tdeclared.set(tool.name, tool)\n\t}\n\tconst sent =\n\t\trequest.tools === undefined\n\t\t\t? undefined\n\t\t\t: requestToRelay({ messages: [], tools: request.tools }).tools\n\tconst forwarded: ToolDefinition[] = []\n\tconst seen = new Set<string>()\n\tfor (const [index, tool] of (sent ?? []).entries()) {\n\t\tconst server = declared.get(tool.name)\n\t\tif (seen.has(tool.name)) {\n\t\t\trefusals.push({ reason: 'mismatch', index, name: tool.name })\n\t\t} else if (server !== undefined) {\n\t\t\tconst encoded = canonicalStringify(server)\n\t\t\tif (encoded === undefined || encoded !== canonicalStringify(tool)) {\n\t\t\t\trefusals.push({ reason: 'mismatch', index, name: tool.name })\n\t\t\t} else forwarded.push(server)\n\t\t} else if (policy.tools?.accept === true) {\n\t\t\tforwarded.push(tool)\n\t\t} else refusals.push({ reason: 'tool', index, name: tool.name })\n\t\tseen.add(tool.name)\n\t}\n\tif (refusals.length > 0) return failure(refusals)\n\tconst text = policy.system?.text\n\tconst first = request.messages[0]\n\tconst page = first?.role === 'system' ? first : undefined\n\tconst head: Message | undefined =\n\t\ttext === undefined\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\tid: crypto.randomUUID(),\n\t\t\t\t\trole: 'system',\n\t\t\t\t\tcontent: page === undefined ? text : `${text}\\n\\n${page.content}`,\n\t\t\t\t}\n\tconst messages =\n\t\thead === undefined\n\t\t\t? request.messages\n\t\t\t: [head, ...(page === undefined ? request.messages : request.messages.slice(1))]\n\treturn success({\n\t\tmessages,\n\t\t...(sent === undefined ? {} : { tools: forwarded }),\n\t\t...(request.options === undefined ? {} : { options: request.options }),\n\t})\n}\n\n/**\n * Describes one relay refusal as the line its `422` response body carries.\n *\n * @remarks\n * A line holds only the member's key path, the expected shape, the option that admits the member,\n * and, for a tool, the name the page sent as `preview` renders it, so no page content or server\n * text reaches it. `preview` JSON-encodes the name; an encoding longer than 64 characters renders\n * as its opening quote, at most 63 escaped characters that split no escape or surrogate pair, and\n * `…`, so a rendered name holds at most 65 characters. A line names only the tool names the page\n * sent, so the page learns no server tool name it did not send.\n *\n * @param refusal - The refused member and where it sits\n * @returns The refusal line\n * @example\n * ```ts\n * describeRelayRefusal({ reason: 'tool', index: 0, name: 'weather' })\n * // 'tools[0] \"weather\": the relay refuses a tool it does not declare; declare it in tools.definitions, or set tools.accept to admit page tools'\n * ```\n */\nexport function describeRelayRefusal(refusal: RelayRefusal): string {\n\tswitch (refusal.reason) {\n\t\tcase 'system':\n\t\t\treturn `messages[${refusal.index}]: the relay refuses a system message; the server writes the system turn through system.text, and system.accept admits one page system message at index 0`\n\t\tcase 'head':\n\t\t\treturn `messages[${refusal.index}]: the relay admits one system message, at index 0, carrying content only`\n\t\tcase 'tool':\n\t\t\treturn `tools[${refusal.index}] ${preview(refusal.name)}: the relay refuses a tool it does not declare; declare it in tools.definitions, or set tools.accept to admit page tools`\n\t\tcase 'mismatch':\n\t\t\treturn `tools[${refusal.index}] ${preview(refusal.name)}: the definition must equal the relay's declared definition of that name, and a name appears once`\n\t}\n}\n\n/**\n * Projects a judge question onto the System One wire while preserving omitted members.\n *\n * @param question - The domain question to send\n * @returns The wire question with `type` replacing `form`\n * @example\n * ```ts\n * questionToSystemOne({ form: 'noul' }) // { type: 'noul' }\n * ```\n */\nexport function questionToSystemOne(question: JudgeQuestion): SystemOneQuestion {\n\treturn {\n\t\ttype: question.form,\n\t\t...(question.instructions === undefined ? {} : { instructions: question.instructions }),\n\t\t...(question.criteria === undefined ? {} : { criteria: question.criteria }),\n\t}\n}\n\n/**\n * Extracts a System One distribution in question criteria order and drops server measures.\n *\n * @remarks\n * Every requested candidate must have a finite probability in [0, 1]; the helper checks each\n * requested candidate itself and reads no other member. Score maps and arrays project onto the\n * requested levels. Additional candidates are ignored, sums are unconstrained, and probabilities\n * are preserved without normalization.\n *\n * @param answer - The wire answer to decode\n * @param question - The question defining the form and candidate order\n * @returns The domain answer, or undefined for a mismatched form or a requested candidate whose\n * probability is missing, not finite, or outside [0, 1]\n * @example\n * ```ts\n * extractSystemOneAnswer({ type: 'noul', noul: 0.9 }, { form: 'noul' })\n * // { form: 'noul', noul: 0.9 }\n * ```\n */\nexport function extractSystemOneAnswer(\n\tanswer: SystemOneAnswer,\n\tquestion: JudgeQuestion,\n): JudgeAnswer | undefined {\n\tconst bounded = boundsOf(0, 1)\n\tif (answer.type === 'noul' && question.form === 'noul') {\n\t\treturn bounded(answer.noul) ? { form: 'noul', noul: answer.noul } : undefined\n\t}\n\tif (answer.type === 'choice' && question.form === 'choice') {\n\t\tconst labels = Object.keys(question.criteria)\n\t\tconst entries = labels.flatMap((label) => {\n\t\t\tconst probability = Object.hasOwn(answer.probabilities, label)\n\t\t\t\t? answer.probabilities[label]\n\t\t\t\t: undefined\n\t\t\treturn bounded(probability) ? [[label, probability] as const] : []\n\t\t})\n\t\treturn entries.length === labels.length\n\t\t\t? { form: 'choice', probabilities: Object.fromEntries(entries) }\n\t\t\t: undefined\n\t}\n\tif (answer.type !== 'score' || question.form !== 'score') return undefined\n\t// `keys()` visits a hole in the criteria list too, so a hole is a requested level.\n\tconst levels = [...question.criteria.keys()]\n\tconst probabilities = levels.flatMap((level) => {\n\t\tconst probability = Object.hasOwn(answer.probabilities, level)\n\t\t\t? isArray(answer.probabilities)\n\t\t\t\t? answer.probabilities[level]\n\t\t\t\t: answer.probabilities[String(level)]\n\t\t\t: undefined\n\t\treturn bounded(probability) ? [probability] : []\n\t})\n\treturn probabilities.length === levels.length ? { form: 'score', probabilities } : undefined\n}\n\n/**\n * Maps complete System One token counts onto validated token usage.\n *\n * @param usage - The optional wire token counts\n * @returns Token usage, or undefined for missing, null, negative, or non-finite counts\n * @example\n * ```ts\n * extractSystemOneUsage({ input_tokens: 975, output_tokens: 4 })\n * // { prompt: 975, completion: 4, total: 979 }\n * ```\n */\nexport function extractSystemOneUsage(usage: SystemOneUsage | undefined): TokenUsage | undefined {\n\tconst extracted = attempt(() => {\n\t\tif (usage === undefined) return undefined\n\t\tconst { input_tokens: prompt, output_tokens: completion } = usage\n\t\tif (!isNumber(prompt) || !isNumber(completion)) return undefined\n\t\tconst result = { prompt, completion, total: prompt + completion }\n\t\treturn isTokenUsage(result) ? result : undefined\n\t})\n\treturn extracted.success ? extracted.value : undefined\n}\n","import type { ProviderDelta, ProviderResult, RelayFrame, RelayStreamOptions } from './types.js'\nimport type { ThinkingReplay } from '../types.js'\nimport { RELAY_CONTENT_TYPE, RELAY_PROVIDER_MESSAGE } from './constants.js'\nimport { ProviderError, isProviderAbortError } from './errors.js'\nimport { relayFrameContract } from './contracts.js'\nimport { stripThinking } from '../helpers.js'\n\n/**\n * Streams a provider call as validated NDJSON frames under response backpressure.\n *\n * @remarks\n * Cancellation aborts upstream before returning its iterator. Request listeners are\n * released on settlement. Unexpected provider failures carry a fixed public message.\n * An inbound abort leaves the response body neither closed nor errored — a server runtime\n * cancels that body when the client disconnects — so a consumer that aborts the inbound\n * signal itself must cancel the body rather than keep reading it.\n *\n * @example\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { RelayStream } from '@orkestrel/agent'\n *\n * export function respond(provider: ProviderInterface, signal: AbortSignal): Response {\n * \treturn new RelayStream({ provider, request: { messages: [] }, signal }).response\n * }\n * ```\n */\nexport class RelayStream {\n\treadonly #replay: ThinkingReplay\n\treadonly #upstream = new AbortController()\n\treadonly #encoder = new TextEncoder()\n\treadonly #iterator: AsyncGenerator<ProviderDelta, ProviderResult>\n\treadonly #signal: AbortSignal\n\treadonly #listener: () => void\n\treadonly #response: Response\n\t#settled = false\n\n\tconstructor(options: RelayStreamOptions) {\n\t\tthis.#replay = options.provider.replay ?? 'none'\n\t\tthis.#signal = options.signal\n\t\tthis.#listener = this.#abort.bind(this)\n\t\tif (this.#signal.aborted) this.#abortProvider()\n\t\tthis.#iterator = options.provider.stream(\n\t\t\tstripThinking(options.request.messages, this.#replay),\n\t\t\tthis.#upstream.signal,\n\t\t\toptions.request.tools,\n\t\t\toptions.request.options,\n\t\t)\n\t\tthis.#signal.addEventListener('abort', this.#listener, { once: true })\n\t\tthis.#response = new Response(\n\t\t\tnew ReadableStream<Uint8Array>({\n\t\t\t\tpull: this.#pull.bind(this),\n\t\t\t\tcancel: this.#abort.bind(this),\n\t\t\t}),\n\t\t\t{ headers: { 'content-type': RELAY_CONTENT_TYPE, 'cache-control': 'no-store' } },\n\t\t)\n\t}\n\n\t/** Exposes the pull-driven response for the provider call. */\n\tget response(): Response {\n\t\treturn this.#response\n\t}\n\n\tasync #pull(controller: ReadableStreamDefaultController<Uint8Array>): Promise<void> {\n\t\tif (this.#settled) return\n\t\ttry {\n\t\t\tconst step = await this.#iterator.next()\n\t\t\tif (this.#settled) return\n\t\t\tthis.#write(controller, step.done ? { channel: 'result', result: step.value } : step.value)\n\t\t\tif (step.done) this.#finish(controller)\n\t\t} catch (error) {\n\t\t\tif (this.#settled) return\n\t\t\tconst frame: RelayFrame = isProviderAbortError(error)\n\t\t\t\t? { channel: 'abort', partial: error.partial }\n\t\t\t\t: { channel: 'error', message: RELAY_PROVIDER_MESSAGE }\n\t\t\tthis.#write(\n\t\t\t\tcontroller,\n\t\t\t\trelayFrameContract.is(frame)\n\t\t\t\t\t? frame\n\t\t\t\t\t: { channel: 'error', message: RELAY_PROVIDER_MESSAGE },\n\t\t\t)\n\t\t\tthis.#finish(controller)\n\t\t\tthis.#abortProvider()\n\t\t\tawait this.#return()\n\t\t}\n\t}\n\n\tasync #abort(): Promise<void> {\n\t\tif (this.#settled) return\n\t\tthis.#settled = true\n\t\tthis.#abortProvider()\n\t\ttry {\n\t\t\tawait this.#return()\n\t\t} finally {\n\t\t\tthis.#release()\n\t\t}\n\t}\n\n\tasync #return(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.#iterator.return({ content: '' })\n\t\t} catch {\n\t\t\t// A settled response has no error channel for upstream cleanup failures.\n\t\t}\n\t}\n\n\t#write(controller: ReadableStreamDefaultController<Uint8Array>, frame: RelayFrame): void {\n\t\tif (!relayFrameContract.is(frame)) throw new ProviderError('PROTOCOL', 'invalid relay frame')\n\t\tcontroller.enqueue(this.#encoder.encode(`${JSON.stringify(frame)}\\n`))\n\t}\n\n\t#finish(controller: ReadableStreamDefaultController<Uint8Array>): void {\n\t\tthis.#settled = true\n\t\tthis.#release()\n\t\tcontroller.close()\n\t}\n\n\t#abortProvider(): void {\n\t\tthis.#upstream.abort(this.#signal.reason)\n\t}\n\n\t#release(): void {\n\t\tthis.#signal.removeEventListener('abort', this.#listener)\n\t}\n}\n","import type { ThinkSplitterInterface } from './types.js'\nimport { THINK_CLOSE, THINK_OPEN } from './constants.js'\n\n/**\n * Separates `<think>…</think>` spans from raw content deltas with per-stream state: text inside a\n * span accumulates on `thinking`, and text outside it returns as clean content, so a provider\n * yields the answer alone and surfaces the reasoning as\n * {@link import('./types.js').ProviderResult.thinking}. A tag split across deltas is held until\n * disambiguated, `flush()` settles the stream end, and one splitter serves one stream.\n *\n * @remarks\n * - **Cross-chunk tags.** A tag may arrive split across wire deltas (`'<thi'` then\n * `'nk>'`): any suffix of the pending text that is a strict prefix of a tag being\n * scanned for is held back (neither surfaced nor routed) until the next delta — or\n * `flush()` — disambiguates it. A held tag prefix that never completes is real\n * content; a held close-tag prefix inside a span is thinking.\n * - **The implicit leading open (the qwen3-template shape).** Some chat templates\n * pre-seed `<think>` into the prompt scaffold, so the wire stream begins\n * mid-reasoning and only a bare `</think>` appears. Before any tag event, a bare\n * close therefore reclassifies everything surfaced so far (plus the pre-close\n * pending) as thinking — `content` is corrected retroactively (the already-returned\n * prefix cannot be recalled, so `content` is the authoritative accumulation). The\n * rule is one-shot: after any tag event a bare `</think>` is plain text.\n * - **Multiple spans** accumulate onto `thinking` in stream order. A nested-looking\n * `<think>` inside an open span is thinking text (no nesting is tracked — the\n * first `</think>` closes the span), matching how the models emit it.\n * - **Unclosed span at stream end.** `flush()` routes the open span's tail (including\n * any held partial close tag) to `thinking` — a cut-off model was still reasoning.\n * - **One splitter, one stream.** State is per-stream; create a fresh instance per\n * provider call ({@link import('./factories.js').createThinkSplitter}).\n *\n * @example\n * ```ts\n * const splitter = new ThinkSplitter()\n * splitter.split('<thi') // '' (held — ambiguous)\n * splitter.split('nk>plan</think>ok') // 'ok'\n * splitter.thinking // 'plan'\n * splitter.content // 'ok'\n * splitter.flush() // '' (nothing held)\n * ```\n */\nexport class ThinkSplitter implements ThinkSplitterInterface {\n\t// The undecided tail of the stream — text not yet routed to content or thinking\n\t// (at most one partial tag's worth between calls; transiently the whole delta).\n\t#pending = ''\n\t// Whether the scanner is inside an open `<think>` span.\n\t#inside = false\n\t// Whether a tag event has occurred (an explicit open, or the one-shot implicit-open\n\t// close) — before it, a bare `</think>` closes the implicit span a chat template\n\t// pre-seeded; after it, a bare close is plain text.\n\t#opened = false\n\t#content = ''\n\t#thinking = ''\n\n\tget content(): string {\n\t\treturn this.#content\n\t}\n\n\tget thinking(): string {\n\t\treturn this.#thinking\n\t}\n\n\tsplit(delta: string): string {\n\t\tconst out = this.#scan(delta)\n\t\tthis.#content += out\n\t\treturn out\n\t}\n\n\tflush(): string {\n\t\tconst pending = this.#pending\n\t\tthis.#pending = ''\n\t\tif (this.#inside) {\n\t\t\t// An unclosed span at stream end — the tail (held partial close tag included)\n\t\t\t// is thinking: the model was cut off mid-reasoning, never mid-answer.\n\t\t\tthis.#thinking += pending\n\t\t\tthis.#inside = false\n\t\t\treturn ''\n\t\t}\n\t\t// A held partial tag that never completed was real content after all.\n\t\tthis.#content += pending\n\t\treturn pending\n\t}\n\n\t// Drive the state machine over one delta, returning the clean content it surfaces —\n\t// the wrapper folds the return into `#content`, which the implicit-open branch may\n\t// have reset a moment earlier (the reclassification).\n\t#scan(delta: string): string {\n\t\tthis.#pending += delta\n\t\treturn [...this.#surface()].join('')\n\t}\n\n\t// Walk the pending text, yielding each clean content piece in order and routing reasoning to\n\t// `#thinking`; return when the remainder is undecided and held for the next delta. A piece is\n\t// yielded only after `#opened` is set, so the reclassification below never meets one.\n\t*#surface(): Generator<string, void> {\n\t\tfor (;;) {\n\t\t\tif (this.#inside) {\n\t\t\t\tconst close = this.#pending.indexOf(THINK_CLOSE)\n\t\t\t\tif (close === -1) {\n\t\t\t\t\t// No close yet — route all but a possible partial close-tag suffix to\n\t\t\t\t\t// thinking, holding that suffix for the next delta to disambiguate.\n\t\t\t\t\tthis.#thinking += this.#hold([THINK_CLOSE])\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tthis.#thinking += this.#pending.slice(0, close)\n\t\t\t\tthis.#pending = this.#pending.slice(close + THINK_CLOSE.length)\n\t\t\t\tthis.#inside = false\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst open = this.#pending.indexOf(THINK_OPEN)\n\t\t\tif (!this.#opened) {\n\t\t\t\t// The implicit leading open: before any tag event, a bare close (arriving\n\t\t\t\t// ahead of any explicit open) means the stream began inside a pre-seeded\n\t\t\t\t// span — everything surfaced so far was reasoning. Reclassify it.\n\t\t\t\tconst close = this.#pending.indexOf(THINK_CLOSE)\n\t\t\t\tif (close !== -1 && (open === -1 || close < open)) {\n\t\t\t\t\tthis.#thinking += this.#content + this.#pending.slice(0, close)\n\t\t\t\t\tthis.#content = ''\n\t\t\t\t\tthis.#pending = this.#pending.slice(close + THINK_CLOSE.length)\n\t\t\t\t\tthis.#opened = true\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (open === -1) {\n\t\t\t\t// No open tag — surface all but a possible partial tag suffix as content\n\t\t\t\t// (before any tag event a split leading close must not leak either, so the\n\t\t\t\t// close tag's prefixes are held back too).\n\t\t\t\tconst tags = this.#opened ? [THINK_OPEN] : [THINK_OPEN, THINK_CLOSE]\n\t\t\t\tyield this.#hold(tags)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.#opened = true\n\t\t\tyield this.#pending.slice(0, open)\n\t\t\tthis.#pending = this.#pending.slice(open + THINK_OPEN.length)\n\t\t\tthis.#inside = true\n\t\t}\n\t}\n\n\t// Settle the pending text against the given tags' possible partial suffixes: the\n\t// longest ambiguous suffix stays pending for the next delta; everything before it\n\t// is returned for routing.\n\t#hold(tags: readonly string[]): string {\n\t\tconst keep = Math.max(...tags.map((tag) => this.#overlap(tag)))\n\t\tconst cut = this.#pending.length - keep\n\t\tconst settled = this.#pending.slice(0, cut)\n\t\tthis.#pending = this.#pending.slice(cut)\n\t\treturn settled\n\t}\n\n\t// The length of the longest strict prefix of `tag` that suffixes the pending text —\n\t// how many trailing characters are an ambiguous partial tag and must be held back.\n\t#overlap(tag: string): number {\n\t\tconst max = Math.min(this.#pending.length, tag.length - 1)\n\t\treturn (\n\t\t\tArray.from({ length: max }, (_, index) => max - index).find((length) =>\n\t\t\t\tthis.#pending.endsWith(tag.slice(0, length)),\n\t\t\t) ?? 0\n\t\t)\n\t}\n}\n","import type {\n\tAgentProviderInput,\n\tAgentProviderInterface,\n\tProviderDelta,\n\tProviderIncrement,\n\tProviderOptions,\n\tProviderParserInterface,\n\tProviderRequest,\n\tProviderResult,\n\tProviderStreamOptions,\n\tThinkSplitterInterface,\n} from './types.js'\nimport type { Message, ThinkingReplay } from '../types.js'\nimport type { ToolDefinition } from '@orkestrel/tool'\nimport { Timeout } from '@orkestrel/timeout'\nimport { DEFAULT_PROVIDER_TIMEOUT } from './constants.js'\nimport {\n\tisProviderAbortError,\n\tisProviderError,\n\tProviderAbortError,\n\tProviderError,\n} from './errors.js'\nimport { ThinkSplitter } from './ThinkSplitter.js'\nimport { buildProviderResult, readChunks, buildProviderHeaders, readFailure } from './helpers.js'\nimport { joinThinking } from '../helpers.js'\n\n/**\n * Implements bounded HTTP streaming and result assembly behind concrete wire seams.\n *\n * @remarks\n * Every call owns its parser, splitter, deadline, and accumulation. Success bodies\n * have no size limit. A qwen3 implicit-open reclassification corrects the final\n * content while content deltas already yielded cannot be recalled. A subclass fills\n * `name`, `frame`, `encode`, `read`, and `finish`; the constructor takes the `split`\n * and `strict` switches to control reasoning separation and whether a stream must end with a settled\n * `result` or a `done` record, which it must by default.\n * The `replay` input names which stored thinking the agent and relay send back. Default: `'none'`.\n * A throw from the framing seam (`parse`, or `finish`) becomes a `ProviderError` with the\n * `PROTOCOL` code whose `cause` is the throw, unless it is already a `ProviderError` or\n * `ProviderAbortError`, which reaches the caller unchanged. A throw from `read`, `frame`, or\n * `clear` reaches the caller unchanged, and a framing throw that races a cancel is the `cause` of\n * the `ProviderAbortError`.\n *\n * @example Writing a provider for a new wire\n * ```ts\n * import type {\n * \tProviderIncrement,\n * \tProviderOptions,\n * \tProviderParserInterface,\n * \tProviderRequest,\n * } from '@orkestrel/agent'\n * import { AgentProvider } from '@orkestrel/agent'\n *\n * class TextFrame implements ProviderParserInterface<string> {\n * \tparse(chunk: string): readonly string[] {\n * \t\treturn [chunk]\n * \t}\n * \tclear(): void {} // Raw text retains no framing state.\n * }\n *\n * interface TextOptions extends ProviderOptions {\n * \treadonly url: string\n * }\n *\n * class TextProvider extends AgentProvider<string> {\n * \treadonly name = 'text'\n * \tconstructor(options: TextOptions) {\n * \t\t// Raw text has no terminal record, so a stream that stops reads as complete.\n * \t\tsuper({ ...options, path: '/generate', strict: false })\n * \t}\n * \tframe(): ProviderParserInterface<string> {\n * \t\treturn new TextFrame()\n * \t}\n * \tencode(request: ProviderRequest): object {\n * \t\treturn { messages: request.messages }\n * \t}\n * \tread(record: string): ProviderIncrement {\n * \t\treturn { content: record, thinking: '', tools: [] }\n * \t}\n * \tfinish(_parser: ProviderParserInterface<string>): readonly string[] {\n * \t\treturn [] // Raw text retains no records at end of input.\n * \t}\n * }\n * ```\n */\nexport abstract class AgentProvider<\n\tTRecord = Readonly<Record<string, unknown>>,\n> implements AgentProviderInterface<TRecord> {\n\treadonly #id: string\n\treadonly #replay: ThinkingReplay\n\treadonly #url: string\n\treadonly #path: string\n\treadonly #timeout: number\n\treadonly #transport: typeof globalThis.fetch\n\treadonly #headers: ProviderOptions['headers']\n\treadonly #split: boolean\n\treadonly #strict: boolean\n\n\tconstructor(input: AgentProviderInput) {\n\t\tthis.#id = crypto.randomUUID()\n\t\tthis.#replay = input.replay ?? 'none'\n\t\tthis.#url = input.url\n\t\tthis.#path = input.path ?? ''\n\t\tthis.#timeout = input.timeout ?? DEFAULT_PROVIDER_TIMEOUT\n\t\tthis.#transport = input.fetch ?? globalThis.fetch.bind(globalThis)\n\t\tthis.#headers = input.headers\n\t\tthis.#split = input.split ?? true\n\t\tthis.#strict = input.strict ?? true\n\t}\n\n\t/** Identifies the concrete backend. */\n\tabstract readonly name: string\n\n\t/** Exposes the instance's minted UUID. */\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\t/** Names which stored assistant thinking the agent and relay send back. */\n\tget replay(): ThinkingReplay {\n\t\treturn this.#replay\n\t}\n\n\t/** Creates fresh framing state for the call. */\n\tabstract frame(): ProviderParserInterface<TRecord>\n\t/** Projects a domain request onto the concrete protocol's wire body. */\n\tabstract encode(request: ProviderRequest): object\n\t/** Decodes a framed record into a turn increment. */\n\tabstract read(record: TRecord): ProviderIncrement\n\t/** Returns any records retained at end of input. */\n\tabstract finish(parser: ProviderParserInterface<TRecord>): readonly TRecord[]\n\n\t/**\n\t * Generates a complete turn by draining the shared stream engine.\n\t *\n\t * @param messages - The conversation turns\n\t * @param signal - The caller's cancellation bound\n\t * @param tools - The advertised tool definitions\n\t * @param options - The per-call generation configuration\n\t * @returns The terminal stream result\n\t */\n\tasync generate(\n\t\tmessages: readonly Message[],\n\t\tsignal: AbortSignal,\n\t\ttools?: readonly ToolDefinition[],\n\t\toptions?: ProviderStreamOptions,\n\t): Promise<ProviderResult> {\n\t\tconst stream = this.stream(messages, signal, tools, options)\n\t\tfor (;;) {\n\t\t\tconst step = await stream.next()\n\t\t\tif (step.done) return step.value\n\t\t}\n\t}\n\n\t/**\n\t * Streams decoded deltas and returns the authoritative or assembled turn result.\n\t *\n\t * @param messages - The conversation turns\n\t * @param signal - The caller's cancellation bound\n\t * @param tools - The advertised tool definitions\n\t * @param options - The per-call generation configuration\n\t * @returns Content and native thinking deltas followed by the settled result\n\t * @throws ProviderAbortError Thrown when the combined cancellation bound fires\n\t * @throws ProviderError Thrown when the response is non-OK, the response has no body, a wire record\n\t * is malformed or carries an upstream failure, or a strict stream ends without a settled result\n\t */\n\tasync *stream(\n\t\tmessages: readonly Message[],\n\t\tsignal: AbortSignal,\n\t\ttools?: readonly ToolDefinition[],\n\t\toptions?: ProviderStreamOptions,\n\t): AsyncGenerator<ProviderDelta, ProviderResult> {\n\t\tif (signal.aborted) throw new ProviderAbortError({ content: '' })\n\t\tconst timeout = new Timeout({ ms: this.#timeout })\n\t\ttimeout.start()\n\t\tconst combined = AbortSignal.any([timeout.signal, signal])\n\t\ttry {\n\t\t\tconst parser = this.#frame(combined)\n\t\t\ttry {\n\t\t\t\treturn yield* this.#read(parser, messages, tools, options, combined)\n\t\t\t} finally {\n\t\t\t\tparser.clear()\n\t\t\t}\n\t\t} finally {\n\t\t\ttimeout.clear()\n\t\t}\n\t}\n\n\t// Creates the call's framing state; a failure that raced the cancel becomes the empty partial.\n\t#frame(combined: AbortSignal): ProviderParserInterface<TRecord> {\n\t\ttry {\n\t\t\treturn this.frame()\n\t\t} catch (error) {\n\t\t\tthrow this.#translate(error, combined, undefined, [])\n\t\t}\n\t}\n\n\t// Requests, frames, folds, and settles one call. Each decoded increment joins a local list\n\t// before its deltas are exposed, so a cancel during a yield assembles from what the body produced.\n\tasync *#read(\n\t\tparser: ProviderParserInterface<TRecord>,\n\t\tmessages: readonly Message[],\n\t\ttools: readonly ToolDefinition[] | undefined,\n\t\toptions: ProviderStreamOptions | undefined,\n\t\tcombined: AbortSignal,\n\t): AsyncGenerator<ProviderDelta, ProviderResult> {\n\t\tconst splitter = this.#split ? new ThinkSplitter() : undefined\n\t\tconst increments: ProviderIncrement[] = []\n\t\ttry {\n\t\t\tconst response = await this.#request(\n\t\t\t\t{\n\t\t\t\t\tmessages,\n\t\t\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t\t\t...(options === undefined ? {} : { options }),\n\t\t\t\t},\n\t\t\t\tcombined,\n\t\t\t)\n\t\t\tif (response.body === null) {\n\t\t\t\tthrow new ProviderError('PROTOCOL', 'provider error: no response body')\n\t\t\t}\n\t\t\tfor await (const chunk of readChunks(response.body, combined)) {\n\t\t\t\tcombined.throwIfAborted()\n\t\t\t\tfor (const record of this.#parse(parser, chunk, combined)) {\n\t\t\t\t\tconst increment = this.read(record)\n\t\t\t\t\tincrements.push(increment)\n\t\t\t\t\tif (increment.result === undefined) yield* this.#emit(increment, splitter, combined)\n\t\t\t\t\tcombined.throwIfAborted()\n\t\t\t\t\tif (increment.result !== undefined) return increment.result\n\t\t\t\t}\n\t\t\t}\n\t\t\tcombined.throwIfAborted()\n\t\t\tfor (const record of this.#finish(parser, combined)) {\n\t\t\t\tconst increment = this.read(record)\n\t\t\t\tincrements.push(increment)\n\t\t\t\tif (increment.result === undefined) yield* this.#emit(increment, splitter, combined)\n\t\t\t\tcombined.throwIfAborted()\n\t\t\t\tif (increment.result !== undefined) return increment.result\n\t\t\t}\n\t\t\tconst tail = splitter?.flush() ?? ''\n\t\t\tif (tail.length > 0) yield { channel: 'content', text: tail }\n\t\t\tcombined.throwIfAborted()\n\t\t\tif (this.#strict && increments.at(-1)?.done !== true) {\n\t\t\t\tthrow new ProviderError(\n\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t'provider error: the stream ended with no settled result and no terminal record; the response was cut, or a wire with no terminal record passes strict: false',\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn this.#assemble(splitter, increments)\n\t\t} catch (error) {\n\t\t\tthrow this.#translate(error, combined, splitter, increments)\n\t\t}\n\t}\n\n\t// A throw from parse becomes a PROTOCOL ProviderError unless cancelled or already a provider error.\n\t#parse(\n\t\tparser: ProviderParserInterface<TRecord>,\n\t\tchunk: string,\n\t\tcombined: AbortSignal,\n\t): readonly TRecord[] {\n\t\ttry {\n\t\t\treturn parser.parse(chunk)\n\t\t} catch (error) {\n\t\t\tif (combined.aborted || isProviderError(error) || isProviderAbortError(error)) throw error\n\t\t\tthrow new ProviderError('PROTOCOL', 'provider error: malformed wire record', { cause: error })\n\t\t}\n\t}\n\n\t// A throw from finish becomes a PROTOCOL ProviderError unless cancelled or already a provider error.\n\t#finish(parser: ProviderParserInterface<TRecord>, combined: AbortSignal): readonly TRecord[] {\n\t\ttry {\n\t\t\treturn this.finish(parser)\n\t\t} catch (error) {\n\t\t\tif (combined.aborted || isProviderError(error) || isProviderAbortError(error)) throw error\n\t\t\tthrow new ProviderError('PROTOCOL', 'provider error: malformed wire record', { cause: error })\n\t\t}\n\t}\n\n\t// Surfaces one increment's deltas: the split content, then — unless the call was cancelled in\n\t// between — its native thinking.\n\t*#emit(\n\t\tincrement: ProviderIncrement,\n\t\tsplitter: ThinkSplitterInterface | undefined,\n\t\tsignal: AbortSignal,\n\t): Generator<ProviderDelta> {\n\t\tconst content = splitter?.split(increment.content) ?? increment.content\n\t\tif (content.length > 0) yield { channel: 'content', text: content }\n\t\tsignal.throwIfAborted()\n\t\tif (increment.thinking.length > 0) yield { channel: 'thinking', text: increment.thinking }\n\t}\n\n\t// The error a failed call throws: a cancel becomes a ProviderAbortError carrying the partial\n\t// assembled so far (a throw that raced the cancel is its cause); any other failure is itself.\n\t#translate(\n\t\terror: unknown,\n\t\tcombined: AbortSignal,\n\t\tsplitter: ThinkSplitterInterface | undefined,\n\t\tincrements: readonly ProviderIncrement[],\n\t): unknown {\n\t\tif (!combined.aborted) return error\n\t\tsplitter?.flush()\n\t\treturn new ProviderAbortError(\n\t\t\tthis.#assemble(splitter, increments),\n\t\t\terror === combined.reason ? undefined : { cause: error },\n\t\t)\n\t}\n\n\t// Both exits assemble alike, so an aborted call's partial carries what a settled call would. A\n\t// settled-result increment replaces the fold; a `done` increment closes it.\n\t#assemble(\n\t\tsplitter: ThinkSplitterInterface | undefined,\n\t\tincrements: readonly ProviderIncrement[],\n\t): ProviderResult {\n\t\tconst last = increments.at(-1)\n\t\tconst folded = last?.result === undefined ? increments : [last]\n\t\treturn buildProviderResult(\n\t\t\tsplitter?.content ?? folded.map((one) => one.content).join(''),\n\t\t\tjoinThinking(splitter?.thinking, folded.map((one) => one.thinking).join('')),\n\t\t\tfolded.flatMap((one) => [...one.tools]),\n\t\t\tfolded.findLast((one) => one.usage !== undefined)?.usage,\n\t\t\tfolded.findLast((one) => one.truncated !== undefined)?.truncated,\n\t\t)\n\t}\n\n\t// Send one request and translate its HTTP failure into the shared provider taxonomy.\n\tasync #request(request: ProviderRequest, signal: AbortSignal): Promise<Response> {\n\t\tconst headers = await buildProviderHeaders(this.#headers, signal)\n\t\tsignal.throwIfAborted()\n\t\tconst response = await this.#transport(this.#url + this.#path, {\n\t\t\tmethod: 'POST',\n\t\t\theaders,\n\t\t\tbody: JSON.stringify(this.encode(request)),\n\t\t\tsignal,\n\t\t})\n\t\tif (!response.ok) {\n\t\t\tconst { message, ...options } = await readFailure(response, 'provider error:', signal)\n\t\t\tthrow new ProviderError('HTTP', message, options)\n\t\t}\n\t\treturn response\n\t}\n}\n","import type {\n\tProviderIncrement,\n\tProviderParserInterface,\n\tProviderRequest,\n\tRelayProviderOptions,\n} from './types.js'\nimport { attempt, cloneJSONValue } from '@orkestrel/contract'\nimport { createNDJSONParser } from '@orkestrel/ndjson'\nimport { AgentProvider } from './AgentProvider.js'\nimport { ProviderAbortError, ProviderError } from './errors.js'\nimport { requestToRelay } from './helpers.js'\nimport { providerRequestContract, relayFrameContract } from './contracts.js'\n\n/**\n * Carries provider calls over an authenticated NDJSON relay endpoint.\n *\n * @remarks\n * Tool execution context stays local; calls carry only `id`, `name`, and `arguments`.\n * The wire body is an owned snapshot of\n * the projection read through property descriptors, so a serializer reachable only through a\n * `get` trap or a prototype is never consulted; an own function-valued property such as a\n * `toJSON` method is a value outside JSON and is refused before fetching, with the clone's\n * failure as the refusal's `cause`. A remote abort reconstructs a `ProviderAbortError` instance\n * without aborting the\n * local signal; the `Agent` runtime treats that instance as an error unless its own bound\n * signal is aborted.\n * Content is preserved verbatim, including literal thinking tags.\n * A refusal reaches the browser as a `ProviderError` instance with the `HTTP` code and status.\n * Without a `parser` option each response frames through a fresh strict `@orkestrel/ndjson`\n * parser, so a malformed or non-object line rejects the call with a `ProviderError` carrying the\n * `PROTOCOL` code.\n * This is the browser end alone; {@link createRelay} mounts the server end.\n * The example of {@link createRelayProvider} is the browser half of that pair.\n *\n * @example\n * ```ts\n * import type { ProviderResult } from '@orkestrel/agent'\n * import { RelayProvider } from '@orkestrel/agent'\n *\n * export function ask(bearer: string, signal: AbortSignal): Promise<ProviderResult> {\n * \tconst browser = new RelayProvider({\n * \t\turl: 'https://relay.example/relay',\n * \t\theaders: () => ({ authorization: `Bearer ${bearer}` }),\n * \t})\n * \treturn browser.generate([{ id: 'ask', role: 'user', content: 'ping' }], signal)\n * }\n * ```\n */\nexport class RelayProvider extends AgentProvider {\n\treadonly #parser: (() => ProviderParserInterface) | undefined\n\n\tconstructor(options: RelayProviderOptions) {\n\t\tconst { url, timeout, fetch, headers, replay } = options\n\t\tsuper({\n\t\t\turl,\n\t\t\tsplit: false,\n\t\t\tstrict: true,\n\t\t\t...(timeout === undefined ? {} : { timeout }),\n\t\t\t...(fetch === undefined ? {} : { fetch }),\n\t\t\t...(headers === undefined ? {} : { headers }),\n\t\t\t...(replay === undefined ? {} : { replay }),\n\t\t})\n\t\tthis.#parser = options.parser\n\t}\n\n\t/** Identifies the relay backend. */\n\treadonly name = 'relay'\n\n\t/** Creates fresh framing state for each response. */\n\tframe(): ProviderParserInterface {\n\t\treturn this.#parser === undefined ? createNDJSONParser({ strict: true }) : this.#parser()\n\t}\n\n\t/**\n\t * Projects declared request fields and refuses values the JSON wire cannot carry.\n\t *\n\t * @remarks\n\t * The snapshot is taken from property descriptors, so a custom serializer a projected\n\t * value carries is ignored rather than consulted and cannot reach the wire.\n\t *\n\t * @param request - The domain conversation and call configuration\n\t * @returns The validated wire request\n\t * @throws {ProviderError} Thrown when the snapshot cannot be taken — a throwing read or a\n\t * value outside JSON — carrying that failure as its `cause`, and when the snapshot the\n\t * projection produced is not a valid wire request\n\t */\n\tencode(request: ProviderRequest): object {\n\t\tconst snapshot = attempt(() => cloneJSONValue(requestToRelay(request)))\n\t\tif (!snapshot.success) {\n\t\t\tthrow new ProviderError('PROTOCOL', 'relay request is not JSON', { cause: snapshot.error })\n\t\t}\n\t\tif (!providerRequestContract.is(snapshot.value)) {\n\t\t\tthrow new ProviderError('PROTOCOL', 'relay request is not JSON')\n\t\t}\n\t\treturn snapshot.value\n\t}\n\n\t/**\n\t * Validates a relay frame and translates its channel into the shared stream engine.\n\t *\n\t * @param record - The framed wire record\n\t * @returns A delta contribution or authoritative result\n\t * @throws {ProviderAbortError} Thrown when the frame's channel is `abort`, carrying its partial result\n\t * @throws {ProviderError} Thrown when the frame fails validation or its channel is `error`\n\t */\n\tread(record: Readonly<Record<string, unknown>>): ProviderIncrement {\n\t\tif (!relayFrameContract.is(record)) {\n\t\t\tthrow new ProviderError('PROTOCOL', 'invalid relay frame')\n\t\t}\n\t\tswitch (record.channel) {\n\t\t\tcase 'content':\n\t\t\t\treturn { content: record.text, thinking: '', tools: [] }\n\t\t\tcase 'thinking':\n\t\t\t\treturn { content: '', thinking: record.text, tools: [] }\n\t\t\tcase 'result':\n\t\t\t\treturn { content: '', thinking: '', tools: [], result: record.result }\n\t\t\tcase 'abort':\n\t\t\t\tthrow new ProviderAbortError(record.partial)\n\t\t\tcase 'error':\n\t\t\t\tthrow new ProviderError('PROVIDER', record.message)\n\t\t}\n\t}\n\n\t/**\n\t * Recovers an unterminated final frame by completing its NDJSON line.\n\t *\n\t * @param parser - The call's retained framing state\n\t * @returns Records completed by the final newline\n\t */\n\tfinish(parser: ProviderParserInterface): ReadonlyArray<Readonly<Record<string, unknown>>> {\n\t\treturn parser.parse('\\n')\n\t}\n}\n","import type { AgentJudgeInput, AgentJudgeInterface, ProviderOptions } from './types.js'\nimport type { JudgeRequest, JudgeResult } from '../types.js'\nimport { describeFaults, isRecord, parseJSON } from '@orkestrel/contract'\nimport { Timeout } from '@orkestrel/timeout'\nimport { DEFAULT_PROVIDER_TIMEOUT } from './constants.js'\nimport { JudgeError } from './errors.js'\nimport { copyJSON } from '../cloners.js'\nimport { judgeAnswerContract, judgeQuestionContract } from '../contracts.js'\nimport { JudgeAbortError } from '../errors.js'\nimport { describeQuestionRefusal, estimateTokens, scanJudgmentTies } from '../helpers.js'\nimport { buildJudgeResult, buildProviderHeaders, readFailure, readBodyText } from './helpers.js'\nimport { isJudgeEntry, isJudgeQuestion } from '../validators.js'\n\n/**\n * Implements the bounded HTTP calls, validation, and result merging of a judge behind concrete\n * wire seams.\n *\n * @remarks\n * A subclass fills `name`, `encode`, and `read`; the constructor takes the `batch` switch that\n * decides whether one call carries every question or each question gets its own call in key\n * order. `ask` validates the request before any call, builds every body before the first call so\n * a wire limit refuses the request before inference, and runs the calls one after another, each\n * under its own deadline folded with the caller's signal. A cancel throws a `JudgeAbortError`\n * whose `partial` merges the calls that completed. A transport error reaches the caller unchanged. An\n * answer that does not fit its question throws code `PROTOCOL`, and a call whose `read` reports no\n * usage bills the estimate of its response text.\n *\n * @example Writing a judge wire\n * ```ts\n * import type { JudgeRequest, JudgeResult } from '@orkestrel/agent'\n * import { AgentJudge, JudgeError } from '@orkestrel/agent'\n * import { isFiniteNumber, isRecord } from '@orkestrel/contract'\n *\n * // A wire whose server answers one yes/no question per call as { \"yes\": 0.93 }.\n * class YesJudge extends AgentJudge {\n * \treadonly name = 'yes'\n * \tencode(request: JudgeRequest): object {\n * \t\treturn { model: this.model, state: request.state, questions: request.questions }\n * \t}\n * \tread(value: unknown, request: JudgeRequest): JudgeResult {\n * \t\tconst [id] = Object.keys(request.questions)\n * \t\tif (id === undefined || !isRecord(value) || !isFiniteNumber(value.yes)) {\n * \t\t\tthrow new JudgeError('PROTOCOL', 'judge error: unreadable answer')\n * \t\t}\n * \t\treturn { model: this.model, answers: { [id]: { form: 'noul', noul: value.yes } } }\n * \t}\n * }\n *\n * const judge = new YesJudge({\n * \turl: 'http://localhost:8010',\n * \tpath: '/v1/yes',\n * \tmodel: 'yes-1',\n * \tbatch: false,\n * })\n * ```\n */\nexport abstract class AgentJudge implements AgentJudgeInterface {\n\treadonly #id: string\n\treadonly #url: string\n\treadonly #path: string\n\treadonly #model: string\n\treadonly #timeout: number\n\treadonly #transport: typeof globalThis.fetch\n\treadonly #headers: ProviderOptions['headers']\n\treadonly #batch: boolean\n\n\tconstructor(input: AgentJudgeInput) {\n\t\tthis.#id = crypto.randomUUID()\n\t\tthis.#url = input.url\n\t\tthis.#path = input.path ?? ''\n\t\tthis.#model = input.model\n\t\tthis.#timeout = input.timeout ?? DEFAULT_PROVIDER_TIMEOUT\n\t\tthis.#transport = input.fetch ?? globalThis.fetch.bind(globalThis)\n\t\tthis.#headers = input.headers\n\t\tthis.#batch = input.batch ?? true\n\t}\n\n\t/** Identifies the concrete wire. */\n\tabstract readonly name: string\n\n\t/** Exposes the instance's minted UUID. */\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\t/** Exposes the configured model identity. */\n\tget model(): string {\n\t\treturn this.#model\n\t}\n\n\t/** Projects one call's request onto the concrete protocol's wire body. */\n\tabstract encode(request: JudgeRequest): object\n\t/** Decodes one call's parsed response body into the answers for that call's questions. */\n\tabstract read(value: unknown, request: JudgeRequest): JudgeResult\n\n\t/**\n\t * Asks every question of the request about its state and merges the answers of every call.\n\t *\n\t * @param request - The state and the questions keyed by caller id\n\t * @param signal - The caller's cancellation bound\n\t * @returns The merged answers, refusals, and usage of every call\n\t * @throws JudgeAbortError Thrown when the caller's signal or a call's deadline fires, carrying\n\t * the merged result of the completed calls\n\t * @throws JudgeError Thrown when the question map is empty, a question or the state is malformed,\n\t * or the wire refuses the request before any call (code `QUESTION`); when a response is non-OK\n\t * (code `HTTP`); and when a response body is missing or unparsable (code `PROTOCOL`)\n\t */\n\tasync ask(request: JudgeRequest, signal: AbortSignal): Promise<JudgeResult> {\n\t\tif (signal.aborted) throw new JudgeAbortError(buildJudgeResult(this.#model, []))\n\t\t// The request is owned through JSON before validation, so a caller mutating it while a call\n\t\t// is pending cannot make `read` decode against criteria that were never sent, and a proxied\n\t\t// request (a reactive view) is owned where a structured clone would refuse it.\n\t\tconst state = copyJSON(request.state)\n\t\tif (!isJudgeEntry(state)) {\n\t\t\tthrow new JudgeError('QUESTION', 'judge error: state is not a judge entry')\n\t\t}\n\t\tconst questions = isRecord(request.questions) ? Object.entries(request.questions) : []\n\t\tif (questions.length === 0) throw new JudgeError('QUESTION', 'judge error: no questions')\n\t\tconst entries = questions.map(([id, question]) => {\n\t\t\tconst copy = copyJSON(question)\n\t\t\tif (!isJudgeQuestion(copy)) {\n\t\t\t\tconst faults = judgeQuestionContract.audit(copy)\n\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t'QUESTION',\n\t\t\t\t\t`judge error: ${describeQuestionRefusal(id, faults, judgeQuestionContract.schema)}`,\n\t\t\t\t\t{ context: { question: id, faults } },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn [id, copy] as const\n\t\t})\n\t\tconst owned: JudgeRequest = { state, questions: Object.fromEntries(entries) }\n\t\tconst parts: readonly JudgeRequest[] = this.#batch\n\t\t\t? [owned]\n\t\t\t: entries.map(([id, question]) => ({ state, questions: { [id]: question } }))\n\t\tconst calls = parts.map((part) => ({ request: part, body: JSON.stringify(this.encode(part)) }))\n\t\tconst results: JudgeResult[] = []\n\t\tfor (const call of calls) {\n\t\t\tresults.push(await this.#call(call.request, call.body, signal, results))\n\t\t}\n\t\treturn buildJudgeResult(this.#model, results)\n\t}\n\n\tasync #call(\n\t\trequest: JudgeRequest,\n\t\tbody: string,\n\t\tsignal: AbortSignal,\n\t\tcompleted: readonly JudgeResult[],\n\t): Promise<JudgeResult> {\n\t\tconst timeout = new Timeout({ ms: this.#timeout })\n\t\ttimeout.start()\n\t\tconst combined = AbortSignal.any([timeout.signal, signal])\n\t\ttry {\n\t\t\tconst headers = await buildProviderHeaders(this.#headers, combined)\n\t\t\tcombined.throwIfAborted()\n\t\t\tconst response = await this.#transport(this.#url + this.#path, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\theaders,\n\t\t\t\tbody,\n\t\t\t\tsignal: combined,\n\t\t\t})\n\t\t\tif (!response.ok) {\n\t\t\t\tconst { message, ...options } = await readFailure(response, 'judge error:', combined)\n\t\t\t\tthrow new JudgeError('HTTP', message, options)\n\t\t\t}\n\t\t\tif (response.body === null) {\n\t\t\t\tthrow new JudgeError('PROTOCOL', 'judge error: no response body')\n\t\t\t}\n\t\t\tconst text = await readBodyText(response.body, undefined, combined)\n\t\t\tcombined.throwIfAborted()\n\t\t\tconst value = parseJSON(text.text)\n\t\t\tif (value === undefined) throw new JudgeError('PROTOCOL', 'judge error: invalid JSON body')\n\t\t\tconst result = this.read(value, request)\n\t\t\tcombined.throwIfAborted()\n\t\t\tfor (const [id, answer] of Object.entries(result.answers)) {\n\t\t\t\tconst question = Object.hasOwn(request.questions, id) ? request.questions[id] : undefined\n\t\t\t\tconst faults = question === undefined ? [] : scanJudgmentTies(question, answer)\n\t\t\t\tif (faults.length > 0) {\n\t\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t\t`judge error: the answer to question ${id} does not fit it\\n${describeFaults(faults, judgeAnswerContract.schema)}`,\n\t\t\t\t\t\t{ context: { question: id, faults } },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (result.usage !== undefined) return result\n\t\t\t// A call that reports no usage still spent tokens, so it bills its estimate as a provider call does.\n\t\t\tconst estimate = estimateTokens(text.text)\n\t\t\treturn { ...result, usage: { prompt: 0, completion: estimate, total: estimate } }\n\t\t} catch (error) {\n\t\t\tif (combined.aborted) {\n\t\t\t\t// A throw that raced the cancel is the call's real failure, so it is recorded as the cause.\n\t\t\t\tthrow new JudgeAbortError(\n\t\t\t\t\tbuildJudgeResult(this.#model, completed),\n\t\t\t\t\terror === combined.reason ? undefined : { cause: error },\n\t\t\t\t)\n\t\t\t}\n\t\t\tthrow error\n\t\t} finally {\n\t\t\ttimeout.clear()\n\t\t}\n\t}\n}\n","import type { SystemOneJudgeOptions, SystemOneRequest } from './types.js'\nimport type { JudgeRequest, JudgeResult } from '../types.js'\nimport { preview } from '@orkestrel/contract'\nimport { AgentJudge } from './AgentJudge.js'\nimport { SYSTEM_ONE_PATH } from './constants.js'\nimport { extractSystemOneAnswer, extractSystemOneUsage, questionToSystemOne } from './helpers.js'\nimport { isSystemOneAnswer, isSystemOneResponse } from './validators.js'\nimport { JudgeError } from './errors.js'\n\n/**\n * Carries judge questions over the System One protocol and derives answers from server distributions.\n *\n * @remarks\n * The caller supplies the server origin and model. Every question travels in one request.\n * Server measures are ignored; the response model is preserved.\n * `headers` supplies authentication through the shared judge engine.\n * The example readings are those of the response Ollama 0.40.0 returned for that request on 2026-10-07.\n *\n * @example Asking a System One server a choice, a noul, and a score\n * ```ts\n * import { measureAnswer, createSystemOneJudge } from '@orkestrel/agent'\n *\n * const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })\n * const result = await judge.ask(\n * \t{\n * \t\tstate: 'Our checkout has returned 500 errors since 9am. I want a refund for today.',\n * \t\tquestions: {\n * \t\t\tlabel: {\n * \t\t\t\tform: 'choice',\n * \t\t\t\tinstructions: 'Which label fits this ticket?',\n * \t\t\t\tcriteria: { billing: 'Payments and refunds', bug: 'Software errors', account: null },\n * \t\t\t},\n * \t\t\trefund: {\n * \t\t\t\tform: 'noul',\n * \t\t\t\tinstructions: 'Does the customer ask for money back?',\n * \t\t\t\tcriteria: {\n * \t\t\t\t\ttrue: 'The customer asks for a refund or for money back.',\n * \t\t\t\t\tfalse: 'The customer does not ask for money back.',\n * \t\t\t\t},\n * \t\t\t},\n * \t\t\tseverity: {\n * \t\t\t\tform: 'score',\n * \t\t\t\tinstructions: 'How severe is the reported issue?',\n * \t\t\t\tcriteria: ['Cosmetic; no impact', 'Degraded, workaround exists', 'Blocking; no workaround'],\n * \t\t\t},\n * \t\t},\n * \t},\n * \tAbortSignal.timeout(30_000),\n * )\n * const measures = Object.fromEntries(\n * \tObject.entries(result.answers).map(([id, answer]) => [id, measureAnswer(answer)]),\n * )\n * result.model // 'tev1:0.8b' — the model the server named\n * result.usage // { prompt: 975, completion: 4, total: 979 }\n * measures.label // { winner: 'bug', probability: 0.9691, confidence: 0.9536 } to 4 decimals\n * measures.refund // { winner: 'true', probability: 0.9979, confidence: 0.9958 } to 4 decimals\n * measures.severity // { winner: '1', probability: 0.9494, confidence: 0.9241, score: 0.9919 } to 4 decimals\n * ```\n */\nexport class SystemOneJudge extends AgentJudge {\n\tconstructor(options: SystemOneJudgeOptions) {\n\t\tsuper({ ...options, path: SYSTEM_ONE_PATH, batch: true })\n\t}\n\n\t/** Identifies the System One protocol. */\n\treadonly name = 'systemone'\n\n\t/**\n\t * Projects the state and questions onto the System One request with the configured model.\n\t *\n\t * @param request - The state and questions keyed by caller id\n\t * @returns The System One request body\n\t */\n\tencode(request: JudgeRequest): SystemOneRequest {\n\t\treturn {\n\t\t\tstate: request.state,\n\t\t\tmodel: this.model,\n\t\t\tquestions: Object.fromEntries(\n\t\t\t\tObject.entries(request.questions).map(([id, question]) => [\n\t\t\t\t\tid,\n\t\t\t\t\tquestionToSystemOne(question),\n\t\t\t\t]),\n\t\t\t),\n\t\t}\n\t}\n\n\t/**\n\t * Decodes requested System One answers and reports the server model and available usage.\n\t *\n\t * @param value - The parsed response body\n\t * @param request - The questions defining the expected answers\n\t * @returns The decoded distributions, model, and available usage\n\t * @throws JudgeError Thrown when the envelope is malformed or an answer is invalid (code `PROTOCOL`);\n\t * the message names the question id of an invalid answer\n\t */\n\tread(value: unknown, request: JudgeRequest): JudgeResult {\n\t\tif (!isSystemOneResponse(value)) {\n\t\t\tthrow new JudgeError(\n\t\t\t\t'PROTOCOL',\n\t\t\t\t`judge error: invalid System One response envelope; received ${preview(value)}`,\n\t\t\t)\n\t\t}\n\t\tconst entries = Object.entries(request.questions).map(([id, question]) => {\n\t\t\tif (!Object.hasOwn(value.answers, id)) {\n\t\t\t\tthrow new JudgeError('PROTOCOL', `judge error: question ${id} has no System One answer`)\n\t\t\t}\n\t\t\tconst wire = value.answers[id]\n\t\t\tif (!isSystemOneAnswer(wire)) {\n\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t`judge error: question ${id} has an invalid System One answer; received ${preview(wire)}`,\n\t\t\t\t)\n\t\t\t}\n\t\t\tconst answer = extractSystemOneAnswer(wire, question)\n\t\t\tif (answer === undefined) {\n\t\t\t\tthrow new JudgeError(\n\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t`judge error: question ${id} has a mismatched or incomplete System One answer`,\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn [id, answer] as const\n\t\t})\n\t\tconst usage = extractSystemOneUsage(value.usage)\n\t\treturn {\n\t\t\tmodel: value.model ?? this.model,\n\t\t\tanswers: Object.fromEntries(entries),\n\t\t\t...(usage === undefined ? {} : { usage }),\n\t\t}\n\t}\n}\n","import type {\n\tProviderInterface,\n\tRelayHandler,\n\tRelayOptions,\n\tRelayProviderOptions,\n\tRelaySystemOptions,\n\tRelayToolOptions,\n\tSystemOneJudgeOptions,\n\tThinkSplitterInterface,\n} from './types.js'\nimport type { JudgeInterface } from '../types.js'\nimport { attempt, cloneJSONValue, parseJSONAs, preview } from '@orkestrel/contract'\nimport {\n\tDEFAULT_RELAY_LIMIT,\n\tINVALID_RELAY_STATUS,\n\tOVERSIZED_RELAY_STATUS,\n\tREFUSED_RELAY_CONTENT_TYPE,\n\tREFUSED_RELAY_STATUS,\n\tUNAUTHORIZED_RELAY_STATUS,\n\tUPSTREAM_RELAY_STATUS,\n} from './constants.js'\nimport { buildRelayRequest, describeRelayRefusal, readBodyText, requestToRelay } from './helpers.js'\nimport { ProviderError } from './errors.js'\nimport { providerRequestContract } from './contracts.js'\nimport { RelayStream } from './RelayStream.js'\nimport { RelayProvider } from './RelayProvider.js'\nimport { ThinkSplitter } from './ThinkSplitter.js'\nimport { SystemOneJudge } from './SystemOneJudge.js'\n\n/**\n * Creates an authorized relay handler that validates a bounded JSON request before streaming.\n *\n * @remarks\n * Answers with the `401` status when authorization refuses or throws, the `400`\n * status when the body is missing, unreadable, or invalid, the `413` status when\n * the body fills its byte budget or the inbound read is aborted, the `422` status when\n * the request carries a page `system` message or tool definition that `options.system` or\n * `options.tools` does not admit, and the `502` status when the upstream provider call cannot\n * be constructed. A `422` refusal carries one `text/plain` line per refused member, as\n * {@link describeRelayRefusal} writes it, and the page reads at most `MAX_ERROR_BODY_LENGTH`\n * bytes of that body into its error message; every other refusal carries no body. An admitted\n * request is forwarded as {@link buildRelayRequest} builds it, the server's text first, and\n * `limit` counts only the inbound bytes. The handler copies `options.system` and the tool\n * definitions at construction, so a later change to either has no effect.\n *\n * @param options - The upstream provider, authorization decision, optional byte budget, and\n * system turn and tool definition policy\n * @returns A fetch-standard handler suitable for a router\n * @throws {ProviderError} Thrown with the `PROTOCOL` code when `options.tools.definitions` is not\n * JSON or repeats a name\n * @example Mounting the relay on your server\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createRelay } from '@orkestrel/agent'\n * import { createDispatcher } from '@orkestrel/router'\n * import { createServer } from '@orkestrel/server'\n *\n * declare const upstream: ProviderInterface // the server-side provider holding the credential\n * declare const bearer: string\n *\n * const handler = createRelay({\n * \tprovider: upstream,\n * \tauthorize: (request) => request.headers.get('authorization') === `Bearer ${bearer}`,\n * })\n * const dispatcher = createDispatcher({\n * \troutes: [{ method: 'POST', path: '/relay', handler }],\n * })\n *\n * export function serve(request: Request): Promise<Response> {\n * \treturn dispatcher.handle(request, undefined)\n * }\n *\n * const server = createServer({ dispatcher, state: () => undefined })\n * await server.start()\n * process.on('SIGTERM', () => server.stop()) // signal cancellation, drain, then close the listener\n * ```\n */\nexport function createRelay(options: RelayOptions): RelayHandler {\n\tconst { provider, authorize, limit } = options\n\tconst system: RelaySystemOptions = { ...options.system }\n\tconst { definitions, accept } = options.tools ?? {}\n\tconst snapshot = attempt(() =>\n\t\tcloneJSONValue(requestToRelay({ messages: [], tools: definitions ?? [] })),\n\t)\n\tif (!snapshot.success) {\n\t\tthrow new ProviderError('PROTOCOL', 'relay tool definitions are not JSON', {\n\t\t\tcause: snapshot.error,\n\t\t})\n\t}\n\tif (!providerRequestContract.is(snapshot.value)) {\n\t\tthrow new ProviderError('PROTOCOL', 'relay tool definitions are not JSON')\n\t}\n\tconst owned = snapshot.value.tools ?? []\n\tconst names = new Set<string>()\n\tfor (const tool of owned) {\n\t\tif (names.has(tool.name)) {\n\t\t\tthrow new ProviderError(\n\t\t\t\t'PROTOCOL',\n\t\t\t\t`relay tool definitions repeat the name ${preview(tool.name)}`,\n\t\t\t)\n\t\t}\n\t\tnames.add(tool.name)\n\t}\n\tconst tools: RelayToolOptions = {\n\t\t...(definitions === undefined ? {} : { definitions: owned }),\n\t\t...(accept === undefined ? {} : { accept }),\n\t}\n\treturn async (request) => {\n\t\ttry {\n\t\t\tif ((await authorize(request)) !== true) {\n\t\t\t\treturn new Response(undefined, { status: UNAUTHORIZED_RELAY_STATUS })\n\t\t\t}\n\t\t} catch {\n\t\t\treturn new Response(undefined, { status: UNAUTHORIZED_RELAY_STATUS })\n\t\t}\n\t\tif (request.body === null) return new Response(undefined, { status: INVALID_RELAY_STATUS })\n\t\tconst read = await readBodyText(\n\t\t\trequest.body,\n\t\t\tlimit ?? DEFAULT_RELAY_LIMIT,\n\t\t\trequest.signal,\n\t\t).catch(() => undefined)\n\t\tif (read === undefined) {\n\t\t\treturn new Response(undefined, {\n\t\t\t\tstatus: request.signal.aborted ? OVERSIZED_RELAY_STATUS : INVALID_RELAY_STATUS,\n\t\t\t})\n\t\t}\n\t\tif (!read.complete) return new Response(undefined, { status: OVERSIZED_RELAY_STATUS })\n\t\tconst parsed = parseJSONAs(read.text, providerRequestContract.is)\n\t\tif (parsed === undefined) return new Response(undefined, { status: INVALID_RELAY_STATUS })\n\t\tconst built = buildRelayRequest(parsed, { system, tools })\n\t\tif (!built.success) {\n\t\t\treturn new Response(built.error.map(describeRelayRefusal).join('\\n'), {\n\t\t\t\tstatus: REFUSED_RELAY_STATUS,\n\t\t\t\theaders: { 'content-type': REFUSED_RELAY_CONTENT_TYPE, 'cache-control': 'no-store' },\n\t\t\t})\n\t\t}\n\t\ttry {\n\t\t\treturn new RelayStream({ provider, request: built.value, signal: request.signal }).response\n\t\t} catch {\n\t\t\treturn new Response(undefined, { status: UPSTREAM_RELAY_STATUS })\n\t\t}\n\t}\n}\n\n/**\n * Creates a provider that carries calls through a relay endpoint.\n *\n * @remarks\n * This is the browser end alone. {@link createRelay} mounts the server end, and its example\n * is the server half this one pairs with.\n *\n * @param options - The endpoint, optional parser factory, and HTTP call configuration\n * @returns The relay provider behind the shared provider interface; construct\n * {@link RelayProvider} directly to reach its wire seams\n * @example Reaching the relay from the browser\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createRelayProvider } from '@orkestrel/agent'\n * import { createAbort } from '@orkestrel/abort'\n *\n * declare const bearer: string\n * const abort = createAbort()\n * const messages = [{ id: '1', role: 'user', content: 'Say hello.' }] as const\n *\n * const browser: ProviderInterface = createRelayProvider({\n * \turl: 'https://app.example/relay',\n * \theaders: () => ({ authorization: `Bearer ${bearer}` }),\n * })\n * const result = await browser.generate(messages, abort.signal) // a ProviderResult like a local provider's\n * ```\n */\nexport function createRelayProvider(options: RelayProviderOptions): ProviderInterface {\n\treturn new RelayProvider(options)\n}\n\n/**\n * Creates a fresh stream-stateful `<think>` separator — a {@link ThinkSplitterInterface} that\n * splits a thinking model's in-content `<think>…</think>` reasoning spans away from the answer,\n * delta by delta, so a provider yields clean content alone and surfaces the accumulated\n * reasoning as {@link import('./types.js').ProviderResult.thinking}. One splitter serves one\n * stream.\n *\n * @remarks\n * Feed each raw wire delta through `split(delta)` (it returns the clean content to\n * surface — possibly `''` mid-think) and settle the stream end with `flush()` (a held\n * partial open tag that never completed returns as final content; an unclosed think\n * span lands on `thinking`). Tags split across deltas are held back until\n * disambiguated, multiple spans accumulate in order, and a nested-looking `<think>`\n * inside an open span is thinking text. One splitter serves one stream — create\n * a fresh one per provider call.\n *\n * @returns A fresh {@link ThinkSplitterInterface} (state empty, outside any span)\n *\n * @example\n * ```ts\n * import { createThinkSplitter } from '@orkestrel/agent'\n *\n * const splitter = createThinkSplitter()\n * const clean = splitter.split('<think>plan the answer</think>Here it is.')\n * clean // 'Here it is.'\n * splitter.thinking // 'plan the answer'\n * ```\n */\nexport function createThinkSplitter(): ThinkSplitterInterface {\n\treturn new ThinkSplitter()\n}\n\n/**\n * Creates a judge that sends every question through the configured System One server.\n *\n * @param options - The server origin, model, and optional transport, headers, and timeout\n * @returns The configured judge behind its shared interface\n * @example\n * ```ts\n * const judge = createSystemOneJudge({ url: 'http://localhost:11434', model: 'tev1:0.8b' })\n * ```\n */\nexport function createSystemOneJudge(options: SystemOneJudgeOptions): JudgeInterface {\n\treturn new SystemOneJudge(options)\n}\n","/**\n * Sets the default number of recent live messages the `compact()` method of a\n * {@link ConversationInterface} retains verbatim — `0`, so a manual `compact()` keeps no recent tail and folds every\n * exchange before the newest user message into one summarized section. A caller retains a recent\n * tail by passing `keep` (on\n * {@link ConversationOptions}, {@link ConversationManagerOptions}, or per-fold through\n * {@link CompactOptions}), folding at most the older `count - keep` messages, cut back to whole\n * exchanges, and leaving at least the most recent `keep` live for the next turn. Overridable everywhere `keep` is accepted.\n */\nexport const DEFAULT_CONVERSATION_KEEP = 0\n\n/**\n * Names the framing label the `view()` method of a {@link ConversationInterface} prefixes onto the\n * summary of each compacted section so a small model reads it as a condensed recap of earlier turns — the lean\n * `'[Summary of earlier messages] '` marker, never a literal assistant turn to echo or treat as\n * the live answer.\n *\n * @remarks\n * Deliberately a fixed, lean handful of tokens (a short bracketed marker) so the framing adds a\n * bounded `prefix × sections` overhead and never an open-ended blow-up — the\n * {@link ConversationInterface} no-bloat test guard pins exactly that. Kept here (beside\n * {@link DEFAULT_CONVERSATION_KEEP}) as the one tunable framing constant of the conversation\n * layer, so the wording has a single source of truth (the `view()` recap framing is distinct\n * from the cross-conversation provenance marker of `reference()`, which is rendered inline\n * because it interpolates the per-call provenance `label`).\n */\nexport const CONVERSATION_RECAP_PREFIX = '[Summary of earlier messages] '\n","import type { ConversationErrorCode } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport { INTRINSICS, holds, isObject, isFunction } from '@orkestrel/contract'\n\n/**\n * Reports a conversation with no {@link ConversationSummaryHandler} to fold its messages with, a\n * `sections` cap that is not a positive safe integer, a `keep` that is not a nonnegative safe integer, a summarizer digest\n * whose trimmed text is empty, a judgment or judge request that JSON cannot carry, a stored or\n * selected message whose role is not a conversation role, or a malformed snapshot — thrown by the\n * `compact()` and `add` methods of a {@link ConversationInterface}, its construction, or its\n * judgment store, or returned as the `fault` of an agent selection, carrying the machine-readable\n * `code` `'SUMMARIZER' | 'SECTIONS' | 'JUDGMENT' | 'KEEP' | 'SUMMARY' | 'ROLE' | 'MESSAGE' | 'SNAPSHOT' | 'ID'`.\n *\n * @remarks\n * Compaction requires a summarizer (it digests the folded slice into a section summary); a\n * conversation created without one can still store + `view()` its live tail, but a `compact()`\n * is a programmer error and throws this with `'SUMMARIZER'`.\n * A `sections` cap (on {@link import('./types.js').ConversationOptions} /\n * {@link import('./types.js').ConversationManagerOptions} /\n * {@link import('./types.js').CompactOptions}) must be a positive safe integer, and cannot combine with\n * `cumulative` — either is a programmer error and throws this with `'SECTIONS'`. A `keep` on the same options must be a nonnegative\n * safe integer — any other value is a programmer error and throws this with `'KEEP'`. A judgment\n * record or a judge request that JSON cannot carry, or whose copy fails its guard, is a\n * programmer error and throws this with `'JUDGMENT'`, its `context` naming the `question` id\n * where one exists. A summarizer digest whose trimmed text is empty rejects the `compact()` with\n * `'SUMMARY'`. A stored message plays a `ConversationRole`: `add` refuses any other role with\n * `'ROLE'` before it stores any message of the batch, a hydrating snapshot that holds one throws\n * `'ROLE'`, and an agent's selection that holds one becomes a `'ROLE'` fault. `add` refuses an input\n * whose other members fail `isMessageInput` with `'MESSAGE'`, its `context` holding the `faults`,\n * before it stores any message of the batch. A hydrating snapshot that fails\n * `isConversationSnapshot` or repeats an id, and a store row or written snapshot that does, throw\n * `'SNAPSHOT'`; a judgment the guard admits but JSON cannot carry still throws `'JUDGMENT'`. A\n * manager's `add` of an id it already holds throws `'ID'`. Narrow a caught value with\n * {@link isConversationError} and branch on `error.code`.\n */\nexport class ConversationError extends Error {\n\t/** Names the machine-readable condition — `'SUMMARIZER'`: a `compact()` with no summarizer; `'SECTIONS'`: a `sections` cap that is not a positive safe integer, or a cap combined with `cumulative`; `'JUDGMENT'`: a judgment or judge request that is not JSON; `'KEEP'`: a `keep` that is not a nonnegative safe integer; `'SUMMARY'`: a summarizer digest whose trimmed text is empty; `'ROLE'`: a stored or selected message whose role is not a `ConversationRole`; `'MESSAGE'`: a message input whose other members fail `isMessageInput`; `'SNAPSHOT'`: a snapshot that fails `isConversationSnapshot` or repeats an id; `'ID'`: a manager's `add` of an id it already holds. */\n\treadonly code: ConversationErrorCode\n\t/** Holds the values that locate the failure, such as the `question` id of a malformed question, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Creates the error with its code, message, and optional cause and context.\n\t *\n\t * @param code - The machine-readable {@link ConversationErrorCode}\n\t * @param message - The human-readable description\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: ConversationErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'ConversationError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ConversationError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link ConversationError} through its identity brand and declared members, so a\n * `catch` can branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link ConversationError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tawait conversation.compact()\n * } catch (error) {\n * \tif (isConversationError(error) && error.code === 'SUMMARIZER') addSummarizer()\n * }\n * ```\n */\nexport function isConversationError(value: unknown): value is ConversationError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ConversationError'))?.value ===\n\t\t\t\t\tvalue\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (\n\t\t\t!(\n\t\t\t\tcode === 'SUMMARIZER' ||\n\t\t\t\tcode === 'SECTIONS' ||\n\t\t\t\tcode === 'JUDGMENT' ||\n\t\t\t\tcode === 'KEEP' ||\n\t\t\t\tcode === 'SUMMARY' ||\n\t\t\t\tcode === 'ROLE' ||\n\t\t\t\tcode === 'MESSAGE' ||\n\t\t\t\tcode === 'SNAPSHOT' ||\n\t\t\t\tcode === 'ID'\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\nimport { judgeAnswerShape, judgeQuestionShape, messageShape, tokenUsageShape } from '../shapers.js'\n\n/**\n * Describes a stored judgment: exactly one of an answer or a refusal, with its question, sources,\n * state, model, and storage time.\n *\n * @remarks\n * `isJudgment` is the door a hydrate reads and this shape fills a refusal's faults, because hydrate is\n * a per-element path. A lockstep test holds that `is` implies the guard.\n */\nexport const judgmentShape = unionShape(\n\tobjectShape({\n\t\tid: stringShape(),\n\t\tquestion: judgeQuestionShape,\n\t\tmodel: stringShape(),\n\t\tsources: arrayShape(stringShape()),\n\t\tstate: stringShape(),\n\t\ttime: numberShape(),\n\t\tusage: optionalShape(tokenUsageShape),\n\t\tanswer: judgeAnswerShape,\n\t}),\n\tobjectShape({\n\t\tid: stringShape(),\n\t\tquestion: judgeQuestionShape,\n\t\tmodel: stringShape(),\n\t\tsources: arrayShape(stringShape()),\n\t\tstate: stringShape(),\n\t\ttime: numberShape(),\n\t\tusage: optionalShape(tokenUsageShape),\n\t\trefusal: objectShape({ missing: arrayShape(stringShape()) }),\n\t}),\n) satisfies ContractShape\n\n/** Describes a compacted section: its id, its summary, and the originals it folded. */\nexport const sectionShape = objectShape({\n\tid: stringShape(),\n\tsummary: stringShape(),\n\tmessages: arrayShape(messageShape),\n}) satisfies ContractShape\n\n/**\n * Describes a conversation snapshot exactly: an unknown key refuses, so a key a later version adds never\n * reads as absent.\n *\n * @remarks\n * `isConversationSnapshot` is the door a hydrate and a store read, and this shape fills a refusal's\n * faults. A lockstep test holds that `is` implies the guard, and on the snapshot fixtures the converse.\n */\nexport const conversationSnapshotShape = objectShape({\n\tid: stringShape(),\n\tsections: arrayShape(sectionShape),\n\tmessages: arrayShape(messageShape),\n\tjudgments: optionalShape(arrayShape(judgmentShape)),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { conversationSnapshotShape, judgmentShape, sectionShape } from './shapers.js'\n\n/** Reads the faults of a refused judgment. */\nexport const judgmentContract = createContract(judgmentShape)\n\n/** Reads the faults of a refused section. */\nexport const sectionContract = createContract(sectionShape)\n\n/** Reads the faults of a refused conversation snapshot. */\nexport const conversationSnapshotContract = createContract(conversationSnapshotShape)\n","import type { ConversationInterface, ConversationSnapshot, Section } from './types.js'\nimport type { Judgment, Message, MessageInput } from '../types.js'\nimport type { Guard } from '@orkestrel/contract'\nimport { isTokenUsage } from '@orkestrel/budget'\nimport {\n\tarrayOf,\n\tattempt,\n\tholds,\n\tisBoolean,\n\tisFiniteNumber,\n\tisFunction,\n\tisObject,\n\tisRecord,\n\tisString,\n\toptionalOf,\n\trecordOf,\n\tunionOf,\n} from '@orkestrel/contract'\nimport { isToolCall } from '@orkestrel/tool'\nimport { MESSAGE_ROLES } from '../constants.js'\nimport { isJudgeAnswer, isJudgeQuestion } from '../validators.js'\n\n/**\n * Checks whether a value satisfies the domain conversation-message contract.\n *\n * @remarks\n * Roles belong to MessageRole, image elements are strings, `thinking` is a string, and `call` is a string on any\n * role, as the flat Message type admits. Tool arguments may carry non-JSON values, as the\n * domain type permits; the message wire contract is narrower. The guard is exact on the message's own\n * keys: an unknown member, and an own `undefined` optional, return false. Unreadable fields and\n * untrusted inputs return false.\n *\n * @param value - The unknown message candidate\n * @returns True if the domain message fields are valid; false otherwise\n * @example\n * ```ts\n * isMessage({ id: '1', role: 'user', content: 'hi' }) // true\n * isMessage({ id: '1', role: 'other', content: '' }) // false\n * isMessage({ id: '1', role: 'user', content: '', images: [1] }) // false\n * isMessage({ id: '1', role: 'assistant', content: '', thinking: 1 }) // false\n * ```\n */\nexport function isMessage(value: unknown): value is Message {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { id, role, content, calls, call, images, thinking } = value\n\t\tif (!isString(id) || !isString(content)) return false\n\t\tif (!MESSAGE_ROLES.some((one) => one === role)) return false\n\t\tif (calls !== undefined && !arrayOf(isToolCall)(calls)) return false\n\t\tif (call !== undefined && !isString(call)) return false\n\t\tif (images !== undefined && !arrayOf(isString)(images)) return false\n\t\tif (thinking !== undefined && !isString(thinking)) return false\n\t\t// Three members are required, and a key the count misses is an unknown or an own undefined.\n\t\treturn (\n\t\t\tObject.keys(value).length ===\n\t\t\t3 +\n\t\t\t\t(calls === undefined ? 0 : 1) +\n\t\t\t\t(call === undefined ? 0 : 1) +\n\t\t\t\t(images === undefined ? 0 : 1) +\n\t\t\t\t(thinking === undefined ? 0 : 1)\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a value is a message input a conversation can store: a string `content`, and `calls`,\n * `call`, `images`, and `thinking` absent or of their declared types.\n *\n * @remarks\n * A hand guard, because `add` checks every message: it reads the members once and composes no record\n * guard. The role is `requireRole`'s to refuse, so its text can name the role received. Unknown members\n * are left behind by the store. An unreadable member returns false.\n *\n * @param value - The unknown input candidate\n * @returns True if the input's members are valid; false otherwise\n * @example\n * ```ts\n * isMessageInput({ role: 'user', content: 'hi' }) // true\n * isMessageInput({ role: 'user' }) // false\n * ```\n */\nexport function isMessageInput(value: unknown): value is MessageInput {\n\tconst checked = attempt(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { content, calls, call, images, thinking } = value\n\t\treturn (\n\t\t\tisString(content) &&\n\t\t\t(calls === undefined || arrayOf(isToolCall)(calls)) &&\n\t\t\t(call === undefined || isString(call)) &&\n\t\t\t(images === undefined || arrayOf(isString)(images)) &&\n\t\t\t(thinking === undefined || isString(thinking))\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n\n/**\n * Checks whether a stored judgment carries a valid question and exactly one answer or refusal.\n *\n * @remarks\n * Unknown members, malformed members, and unreadable inputs return false, the answer's and the\n * refusal's own included. Answer fields follow the root judge value types; this storage guard does not\n * impose a wire's probability or candidate limits, and the ties an answer holds to its question run\n * where a judgment is written, because records written before them can miss them. The `time`, `noul`,\n * and probability numbers must be finite, because a stored record is owned through JSON, which turns\n * `NaN` and the infinities into `null`; so every record this guard accepts hydrates through a\n * `JudgmentManager`.\n *\n * @param value - The unknown stored record\n * @returns True if the record satisfies the judgment contract; false otherwise\n * @example\n * ```ts\n * isJudgment({ id: 'q', question: { form: 'noul' }, answer: { form: 'noul', noul: 0.9 }, model: 'judge', sources: [], state: 'text', time: 0 }) // true\n * isJudgment({ id: 'q' }) // false\n * ```\n */\nexport const isJudgment: Guard<Judgment> = unionOf(\n\trecordOf(\n\t\t{\n\t\t\tid: isString,\n\t\t\tquestion: isJudgeQuestion,\n\t\t\tmodel: isString,\n\t\t\tsources: arrayOf(isString),\n\t\t\tstate: isString,\n\t\t\ttime: isFiniteNumber,\n\t\t\tusage: isTokenUsage,\n\t\t\tanswer: isJudgeAnswer,\n\t\t},\n\t\t['usage'],\n\t),\n\trecordOf(\n\t\t{\n\t\t\tid: isString,\n\t\t\tquestion: isJudgeQuestion,\n\t\t\tmodel: isString,\n\t\t\tsources: arrayOf(isString),\n\t\t\tstate: isString,\n\t\t\ttime: isFiniteNumber,\n\t\t\tusage: isTokenUsage,\n\t\t\trefusal: recordOf({ missing: arrayOf(isString) }),\n\t\t},\n\t\t['usage'],\n\t),\n)\n\n/**\n * Checks whether an `unknown` is structurally a {@link Section} record — a `string` `id` and\n * `summary` beside a `messages` array of valid {@link Message} records, the per-section step of\n * the {@link isConversationSnapshot} read-boundary narrow. Total, never throwing, and never an\n * assertion.\n *\n * @remarks\n * A total guard (it never throws — untrusted input, a throwing getter included, returns\n * `false`). It checks the shape of the section: a record with a `string` `id`, a `string`\n * `summary`, and a dense `messages` array every element of which is a valid {@link Message}\n * record ({@link isMessage}), and no other member; a sparse `messages` array is refused. Enough to\n * safely impose the {@link Section} type at a storage boundary without a cast.\n *\n * @param value - The value to test (one element of a snapshot's `sections` array)\n * @returns True if `value` has the structural shape of a {@link Section}; false otherwise\n *\n * @example\n * ```ts\n * isSection({ id: 's', summary: 'recap', messages: [{ id: '1', role: 'user', content: 'hi' }] }) // true\n * isSection({ id: 's', summary: 'recap', messages: 'nope' }) // false\n * isSection({ id: 's', messages: [] }) // false (missing summary)\n * ```\n */\nexport function isSection(value: unknown): value is Section {\n\treturn holds(() => {\n\t\tif (!isRecord(value) || Object.keys(value).length !== 3) return false\n\t\tconst { id, summary, messages } = value\n\t\treturn isString(id) && isString(summary) && arrayOf(isMessage)(messages)\n\t})\n}\n\n/**\n * Narrows an `unknown` to a {@link ConversationSnapshot} — a `string` `id` and valid\n * `sections` and `messages` arrays; the total boundary guard for an untrusted snapshot read (a storage row a\n * {@link import('./stores/DatabaseConversationStore.js').DatabaseConversationStore}\n * reads back from its opaque JSON column, a snapshot loaded from disk), never throwing. The exact\n * analogue of {@link import('@orkestrel/workspace').isWorkspaceSnapshot}.\n *\n * @remarks\n * A total guard (it never throws — untrusted input returns `false`). It checks the shape of the\n * snapshot: a `string` `id`, a `sections` array every element of which is a valid\n * {@link Section} ({@link isSection}), a `messages` array every element of which is a\n * valid {@link Message} ({@link isMessage}), and optional valid `judgments` — enough to safely\n * impose the\n * {@link ConversationSnapshot} type at a storage boundary without a cast. The structural twin of\n * {@link import('@orkestrel/workspace').isWorkspaceSnapshot}. The guard is exact: an unknown member, a\n * 0.0.35 snapshot's conversation `summary` included, and an own `undefined` `judgments` return\n * `false`, as a malformed blob (a non-record, a missing / non-string `id`, a non-array `sections` /\n * `messages`, or any malformed element) does, so a\n * {@link import('./stores/DatabaseConversationStore.js').DatabaseConversationStore}\n * read refuses it rather than seating a broken conversation. The guard is structural: it admits a\n * stored `system` role and a repeated id, which `requireConversationSnapshot` and the\n * {@link import('./Conversation.js').Conversation} constructor then refuse as the stored-data\n * check a conversation runs at hydrate.\n *\n * @param value - The value to test (an opaque storage read)\n * @returns True if `value` has the structural shape of a {@link ConversationSnapshot}; false otherwise\n *\n * @example\n * ```ts\n * isConversationSnapshot({ id: 'c1', sections: [], messages: [] }) // true\n * isConversationSnapshot({ id: 'c1', sections: 'nope', messages: [] }) // false\n * isConversationSnapshot({ sections: [], messages: [] }) // false (missing id)\n * ```\n */\nexport function isConversationSnapshot(value: unknown): value is ConversationSnapshot {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { id, sections, messages, judgments } = value\n\t\treturn (\n\t\t\tObject.keys(value).length === (judgments === undefined ? 3 : 4) &&\n\t\t\tisString(id) &&\n\t\t\tarrayOf(isSection)(sections) &&\n\t\t\tarrayOf(isMessage)(messages) &&\n\t\t\toptionalOf(arrayOf(isJudgment))(judgments)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value carries the {@link ConversationInterface} members a run reads: a string\n * `id`, a boolean `summarizable`, and `view`, `add`, `compact`, and `messages` methods.\n *\n * @remarks\n * A total guard: a throwing getter, a revoked `Proxy`, and any other malformed input return false.\n * It checks each member as its declared type and admits unknown members and class instances, so\n * any implementation the interface admits passes; it never calls a method.\n *\n * @param value - The value to test\n * @returns True if `value` carries the conversation members a run reads; false otherwise\n * @example\n * ```ts\n * isConversation(createConversation()) // true\n * isConversation({ id: 'c1' }) // false\n * ```\n */\nexport function isConversation(value: unknown): value is ConversationInterface {\n\tconst checked = attempt(() => {\n\t\tif (!isObject(value)) return false\n\t\treturn (\n\t\t\tisString(Reflect.get(value, 'id')) &&\n\t\t\tisBoolean(Reflect.get(value, 'summarizable')) &&\n\t\t\tisFunction(Reflect.get(value, 'view')) &&\n\t\t\tisFunction(Reflect.get(value, 'add')) &&\n\t\t\tisFunction(Reflect.get(value, 'compact')) &&\n\t\t\tisFunction(Reflect.get(value, 'messages'))\n\t\t)\n\t})\n\treturn checked.success && checked.value\n}\n","import type { ConversationSnapshot, Section } from './types.js'\nimport type {\n\tConversationRole,\n\tJudgeAnswer,\n\tJudgeRequest,\n\tJudgeResult,\n\tJudgment,\n\tJudgmentInput,\n\tMessage,\n\tMessageInput,\n\tRefusal,\n} from '../types.js'\nimport type { ContractInterface } from '@orkestrel/contract'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tisArray,\n\tisJSONValue,\n\tisRecord,\n\tisString,\n\tpreview,\n} from '@orkestrel/contract'\nimport { CONVERSATION_RECAP_PREFIX } from './constants.js'\nimport { ConversationError } from './errors.js'\nimport { isConversationSnapshot, isJudgment, isMessageInput } from './validators.js'\nimport { CONVERSATION_ROLES } from '../constants.js'\nimport { isNonNegativeSafeInteger, isPositiveSafeInteger } from '../validators.js'\n\n/**\n * Reads what a judge result holds for one question, from its own keys alone.\n *\n * @param result - The reported answers and refusals\n * @param id - The question's id\n * @returns The answer and the refusal, each undefined when the result holds none\n * @example\n * ```ts\n * readJudgeOutcome({ model: 'm', answers: {} }, 'q') // { answer: undefined, refusal: undefined }\n * ```\n */\nexport function readJudgeOutcome(\n\tresult: JudgeResult,\n\tid: string,\n): { readonly answer: JudgeAnswer | undefined; readonly refusal: Refusal | undefined } {\n\treturn {\n\t\tanswer: Object.hasOwn(result.answers, id) ? result.answers[id] : undefined,\n\t\trefusal:\n\t\t\tresult.refusals !== undefined && Object.hasOwn(result.refusals, id)\n\t\t\t\t? result.refusals[id]\n\t\t\t\t: undefined,\n\t}\n}\n\n/**\n * Returns after checking a complete judge result answered or refused every question it was asked, or\n * throws a coded error naming those it left out.\n *\n * @remarks\n * A cancelled call's partial is not complete, so `JudgmentManager` reads this on a settled result only,\n * and {@link buildJudgments} stores whatever a partial holds.\n *\n * An undefined value is no answer, as {@link buildJudgments} reads it, so a key alone doesn't count.\n *\n * @param request - The request whose question keys must be answered\n * @param result - The result the judge settled\n * @throws ConversationError Thrown when a question has neither an answer nor a refusal (code `'JUDGMENT'`), naming them under `context.questions`\n * @example\n * ```ts\n * requireJudgeResult({ state: 'text', questions: { a: { form: 'noul' } } }, { model: 'm', answers: {} })\n * // throws ConversationError ('JUDGMENT')\n * ```\n */\nexport function requireJudgeResult(request: JudgeRequest, result: JudgeResult): void {\n\tconst left = Object.keys(request.questions).filter((id) => {\n\t\tconst { answer, refusal } = readJudgeOutcome(result, id)\n\t\treturn answer === undefined && refusal === undefined\n\t})\n\tif (left.length === 0) return\n\tthrow new ConversationError(\n\t\t'JUDGMENT',\n\t\t`conversation error: the judge left ${left.map((id) => preview(id)).join(', ')} neither answered nor refused; a judge must answer or refuse every question it is asked`,\n\t\t{ context: { questions: left } },\n\t)\n}\n\n/**\n * Builds records for answered or refused request keys, attaching usage only for a single question.\n *\n * @param request - The request whose question keys define record order\n * @param result - The reported answers, refusals, model, and usage\n * @param sources - The ordered source message ids\n * @param state - The rendered state read by the judge\n * @param model - The configured judge identity the records carry\n * @returns Inputs for completed question keys in request order\n * @example\n * ```ts\n * buildJudgments(request, result, ['message-a'], 'Charged twice', judge.model)\n * ```\n */\nexport function buildJudgments(\n\trequest: JudgeRequest,\n\tresult: JudgeResult,\n\tsources: readonly string[],\n\tstate: string,\n\tmodel: string,\n): readonly JudgmentInput[] {\n\tconst single = Object.keys(request.questions).length === 1\n\treturn Object.entries(request.questions).flatMap(([id, question]): readonly JudgmentInput[] => {\n\t\tconst { answer, refusal } = readJudgeOutcome(result, id)\n\t\tif (answer === undefined && refusal === undefined) return []\n\t\treturn [\n\t\t\t{\n\t\t\t\tid,\n\t\t\t\tquestion,\n\t\t\t\tmodel,\n\t\t\t\tsources,\n\t\t\t\tstate,\n\t\t\t\t...(answer !== undefined ? { answer } : refusal !== undefined ? { refusal } : {}),\n\t\t\t\t...(single && result.usage !== undefined ? { usage: result.usage } : {}),\n\t\t\t},\n\t\t]\n\t})\n}\n\n/**\n * Builds the raw synthetic summary message for one compacted section — role `'assistant'`, the\n * section's stable `id`, and its `summary` verbatim as content.\n *\n * @remarks\n * Pure and total. This is the unframed form a `sections` cap merge digests when it folds the\n * oldest sections into one (a summary over their section summaries); the recap label is absent\n * from that merge input. A cumulative fold instead re-reads the framed recap — see\n * {@link buildRecapMessage}.\n *\n * @param section - The compacted section to render\n * @returns The synthetic summary message\n *\n * @example\n * ```ts\n * buildSummaryMessage({ id: 's1', summary: 'recap', messages: [] })\n * // { id: 's1', role: 'assistant', content: 'recap' }\n * ```\n */\nexport function buildSummaryMessage(section: Section): Message {\n\treturn { id: section.id, role: 'assistant', content: section.summary }\n}\n\n/**\n * Builds the framed recap message for one compacted section — the same role and stable `id` as\n * {@link buildSummaryMessage}, with the content prefixed by {@link\n * import('./constants.js').CONVERSATION_RECAP_PREFIX}.\n *\n * @remarks\n * Pure and total. The prefix is what makes a small model read the message as a condensed\n * recap of earlier turns rather than a literal assistant turn to echo or answer from. It is a\n * fixed handful of tokens, so a conversation's `view()` stays lean however many sections it\n * carries.\n *\n * @param section - The compacted section to render\n * @returns The framed recap message\n *\n * @example\n * ```ts\n * buildRecapMessage({ id: 's1', summary: 'recap', messages: [] })\n * // { id: 's1', role: 'assistant', content: `${CONVERSATION_RECAP_PREFIX}recap` }\n * ```\n */\nexport function buildRecapMessage(section: Section): Message {\n\treturn {\n\t\tid: section.id,\n\t\trole: 'assistant',\n\t\tcontent: `${CONVERSATION_RECAP_PREFIX}${section.summary}`,\n\t}\n}\n\n/**\n * Returns a stored judgment record after checking it, or throws a coded error.\n *\n * @remarks\n * A stored record is the JSON copy of its input, so a proxied record is accepted and the stored\n * value is exactly what a snapshot can carry. The check is {@link isJudgment}.\n *\n * @param value - The JSON copy of a judgment record or input\n * @returns The value, narrowed to a {@link Judgment}\n * @throws ConversationError Thrown when the value is not a valid judgment record (code `'JUDGMENT'`)\n *\n * @example\n * ```ts\n * requireJudgment(copyJSON(record)) // the record, narrowed\n * requireJudgment({ id: 'refund' }) // throws ConversationError ('JUDGMENT')\n * ```\n */\nexport function requireJudgment(value: unknown): Judgment {\n\tif (!isJudgment(value)) {\n\t\tthrow new ConversationError('JUDGMENT', 'conversation error: a judgment must be a JSON record')\n\t}\n\treturn value\n}\n\n/**\n * Returns a stored message role after checking it is a conversation role, or throws a coded error.\n *\n * @remarks\n * A stored conversation message plays a `ConversationRole`; the `system` message is built per\n * request and never stored. The error message names the received role JSON-encoded and cut to 32\n * characters, or its `typeof` when it is not a string, and for the role `'system'` adds that host\n * text goes in the `system` option or an instruction.\n *\n * @param role - The role to check\n * @returns The role, narrowed to a `ConversationRole`\n * @throws ConversationError Thrown when the role is not a `CONVERSATION_ROLES` member (code `'ROLE'`)\n *\n * @example\n * ```ts\n * requireRole('user') // 'user'\n * requireRole('system') // throws ConversationError ('ROLE')\n * ```\n */\nexport function requireRole(role: unknown): ConversationRole {\n\tconst stored = CONVERSATION_ROLES.find((one) => one === role)\n\tif (stored === undefined) {\n\t\tthrow new ConversationError(\n\t\t\t'ROLE',\n\t\t\t`a stored message role must be one of ${CONVERSATION_ROLES.map((one) => JSON.stringify(one)).join(', ')}; received ${typeof role === 'string' ? JSON.stringify(role.slice(0, 32)) : typeof role}${role === 'system' ? '; host text goes in the system option or an instruction' : ''}`,\n\t\t)\n\t}\n\treturn stored\n}\n\n/**\n * Returns a message input after checking its role and its members, or throws a coded error.\n *\n * @remarks\n * The role goes first, through {@link requireRole}, so its refusal names what it received. The members\n * cross {@link isMessageInput}, and `audit` runs only after it refused, so a stored message costs one\n * guard read. Tool-call arguments must also be JSON values before a message is stored.\n *\n * @param input - The message input to check\n * @param contract - The message input contract, whose `audit` is read only on a refusal\n * @returns The input, narrowed\n * @throws ConversationError Thrown when the role is not a `ConversationRole` (code `'ROLE'`) or a member fails `isMessageInput` (code `'MESSAGE'`), with the faults under `context.faults`\n *\n * @example\n * ```ts\n * requireMessageInput({ role: 'user', content: 'hi' }, messageInputContract) // the input\n * requireMessageInput({ role: 'user' }, messageInputContract) // throws ConversationError ('MESSAGE')\n * ```\n */\nexport function requireMessageInput(\n\tinput: unknown,\n\tcontract: Pick<ContractInterface<unknown>, 'audit' | 'schema'>,\n): MessageInput {\n\trequireRole(isRecord(input) ? input.role : undefined)\n\tif (\n\t\tisMessageInput(input) &&\n\t\t(input.calls === undefined || input.calls.every((call) => isJSONValue(call.arguments)))\n\t)\n\t\treturn input\n\tconst faults = contract.audit(input)\n\tthrow new ConversationError(\n\t\t'MESSAGE',\n\t\t`a message input must carry string content, and calls, call, images, and thinking of their declared types\\n${describeFaults(faults, contract.schema)}`,\n\t\t{ context: { faults } },\n\t)\n}\n\n/**\n * Returns a conversation snapshot after checking its shape, its stored roles, and its ids, or\n * throws a coded error.\n *\n * @remarks\n * The check reads every own member of `value` once and builds a candidate from those reads: each\n * array member becomes a fresh array holding the same element objects, and a member that is not an\n * array stays as read, so the guard refuses it. The candidate must pass `isConversationSnapshot`, which\n * is exact, so an unknown key refuses rather than being dropped; every section message and live message\n * must play a `ConversationRole`; and no id may repeat across section ids, section message ids, and live\n * message ids. The `'ROLE'` refusal of a `system` message names the row a snapshot written before\n * 0.0.35 can hold, and each `'SNAPSHOT'` refusal asks for what `snapshot()` returned. The candidate\n * is returned, never `value`, so the check and the seating share one read. Element objects are kept\n * by reference, so a host is trusted not to edit a seated message object or to back its members\n * with accessors. The contract's `audit` runs only after the guard refused, to fill `context.faults`.\n *\n * @param value - The snapshot to check, typically a stored read\n * @param contract - The snapshot contract, whose `audit` is read only on a refusal\n * @returns The candidate snapshot\n * @throws ConversationError Thrown when the value fails `isConversationSnapshot` (code `'SNAPSHOT'`, its faults under `context.faults`), repeats an id (code `'SNAPSHOT'`), or holds a message whose role is not a `ConversationRole` (code `'ROLE'`)\n *\n * @example\n * ```ts\n * requireConversationSnapshot({ id: 'c1', sections: [], messages: [] }, conversationSnapshotContract) // { id: 'c1', sections: [], messages: [] }\n * requireConversationSnapshot({ id: 'c1', sections: 'nope', messages: [] }, conversationSnapshotContract) // throws ConversationError ('SNAPSHOT')\n * ```\n */\nexport function requireConversationSnapshot(\n\tvalue: unknown,\n\tcontract: Pick<ContractInterface<unknown>, 'audit' | 'schema'>,\n): ConversationSnapshot {\n\tconst candidate = attempt(() => {\n\t\tif (!isRecord(value)) return undefined\n\t\tconst read = { ...value }\n\t\tconst { sections, messages, judgments } = read\n\t\treturn {\n\t\t\t...read,\n\t\t\tsections: isArray(sections) ? Array.from(sections) : sections,\n\t\t\tmessages: isArray(messages) ? Array.from(messages) : messages,\n\t\t\t...(judgments === undefined\n\t\t\t\t? {}\n\t\t\t\t: { judgments: isArray(judgments) ? Array.from(judgments) : judgments }),\n\t\t}\n\t})\n\tif (!candidate.success) {\n\t\tthrow new ConversationError(\n\t\t\t'SNAPSHOT',\n\t\t\t'a conversation snapshot must be readable; pass what snapshot() returned',\n\t\t\t{ cause: candidate.error },\n\t\t)\n\t}\n\tif (!isConversationSnapshot(candidate.value)) {\n\t\tconst faults = contract.audit(candidate.value)\n\t\tthrow new ConversationError(\n\t\t\t'SNAPSHOT',\n\t\t\t`a conversation snapshot must carry a string id, valid sections and messages, valid judgments when present, and no other member; pass what snapshot() returned\\n${describeFaults(faults, contract.schema)}`,\n\t\t\t{ context: { faults } },\n\t\t)\n\t}\n\tconst snapshot = candidate.value\n\tsnapshot.sections\n\t\t.flatMap((section) => section.messages)\n\t\t.concat(snapshot.messages)\n\t\t.forEach((message) => {\n\t\t\tif (message.role === 'system')\n\t\t\t\tthrow new ConversationError(\n\t\t\t\t\t'ROLE',\n\t\t\t\t\t'a conversation snapshot holds a system message; a snapshot written before 0.0.35 can hold a system row; remove it before loading',\n\t\t\t\t)\n\t\t\trequireRole(message.role)\n\t\t})\n\tconst ids = [\n\t\t...snapshot.sections.map((section) => section.id),\n\t\t...snapshot.sections.flatMap((section) => section.messages.map((message) => message.id)),\n\t\t...snapshot.messages.map((message) => message.id),\n\t]\n\t// Built from the reversed pairs, the map keeps each id's first index.\n\tconst pairs = ids.map((id, index): readonly [string, number] => [id, index])\n\tconst firsts = new Map(pairs.toReversed())\n\tconst repeated = ids.find((id, index) => firsts.get(id) !== index)\n\tif (repeated !== undefined) {\n\t\tthrow new ConversationError(\n\t\t\t'SNAPSHOT',\n\t\t\t`a conversation snapshot repeats the id ${JSON.stringify(repeated.slice(0, 64))}; pass what snapshot() returned`,\n\t\t)\n\t}\n\treturn snapshot\n}\n\n/**\n * Returns a `sections` cap after checking it is a positive safe integer, or throws a coded error.\n *\n * @remarks\n * A fraction, positive infinity, and `NaN` are refused with `0` and a negative, because a count of\n * sections is a whole number. An absent cap leaves the sections list unbounded.\n *\n * @param cap - The cap to check, or `undefined` for no cap\n * @returns The cap unchanged, or `undefined` when none was supplied\n * @throws ConversationError Thrown when the cap is not a positive safe integer (code `'SECTIONS'`)\n *\n * @example\n * ```ts\n * requireSectionsCap(2) // 2\n * requireSectionsCap(undefined) // undefined\n * requireSectionsCap(0) // throws ConversationError ('SECTIONS')\n * ```\n */\nexport function requireSectionsCap(cap: number | undefined): number | undefined {\n\tif (cap !== undefined && !isPositiveSafeInteger(cap)) {\n\t\tthrow new ConversationError('SECTIONS', 'a sections cap must be a positive safe integer', {\n\t\t\tcontext: { sections: cap },\n\t\t})\n\t}\n\treturn cap\n}\n\n/**\n * Returns the cumulative switch after checking it does not combine with a `sections` cap, or throws a\n * coded error.\n *\n * @remarks\n * A cumulative conversation keeps one section, so a cap has nothing to bound. The cap checked is the\n * effective one: inherited from a manager, configured on the conversation, or supplied to one\n * `compact()` call.\n *\n * @param cumulative - Whether folds accumulate every prior section into one\n * @param cap - The effective `sections` cap, or `undefined` for none\n * @returns The cumulative switch unchanged\n * @throws ConversationError Thrown when `cumulative` is true and a cap is present (code `'SECTIONS'`)\n *\n * @example\n * ```ts\n * requireCumulative(true, undefined) // true\n * requireCumulative(false, 2) // false\n * requireCumulative(true, 2) // throws ConversationError ('SECTIONS')\n * ```\n */\nexport function requireCumulative(cumulative: boolean, cap: number | undefined): boolean {\n\tif (cumulative && cap !== undefined) {\n\t\tthrow new ConversationError('SECTIONS', 'cumulative cannot combine with a sections cap', {\n\t\t\tcontext: { sections: cap },\n\t\t})\n\t}\n\treturn cumulative\n}\n\n/**\n * Returns a retained-tail size after checking it is a nonnegative safe integer, or throws a coded\n * error.\n *\n * @remarks\n * The check is {@link import('../validators.js').isNonNegativeSafeInteger}, so `0` and `-0`\n * pass while `NaN`, a fraction, a negative, and either infinity are refused. The error's `context`\n * carries the refused value as `keep`.\n *\n * @param keep - The retained-tail size to check\n * @returns The size unchanged\n * @throws ConversationError Thrown when `keep` is not a nonnegative safe integer (code `'KEEP'`)\n *\n * @example\n * ```ts\n * requireKeep(2) // 2\n * requireKeep(1.5) // throws ConversationError ('KEEP')\n * ```\n */\nexport function requireKeep(keep: number): number {\n\tif (!isNonNegativeSafeInteger(keep)) {\n\t\tthrow new ConversationError('KEEP', 'a keep must be a nonnegative safe integer', {\n\t\t\tcontext: { keep },\n\t\t})\n\t}\n\treturn keep\n}\n\n/**\n * Returns a summarizer digest after checking it carries non-whitespace text, or throws a coded error.\n *\n * @remarks\n * A digest whose trimmed text is empty would commit a section that recaps nothing, so it is\n * refused. Any digest with a non-whitespace character passes unchanged, surrounding whitespace\n * included.\n *\n * @param summary - The summarizer digest to check\n * @returns The digest unchanged\n * @throws ConversationError Thrown when the digest is not a string or its trimmed text is empty (code `'SUMMARY'`)\n *\n * @example\n * ```ts\n * requireSummary('recap') // 'recap'\n * requireSummary(' \\n') // throws ConversationError ('SUMMARY')\n * ```\n */\nexport function requireSummary(summary: string): string {\n\tif (!isString(summary) || summary.trim() === '') {\n\t\tthrow new ConversationError('SUMMARY', 'a summarizer digest must carry non-whitespace text')\n\t}\n\treturn summary\n}\n\n/**\n * Settles with a promise, or rejects with the reason of a signal that aborts first, so a caller\n * is released at once even when the work behind the promise ignores the signal.\n *\n * @remarks\n * An absent signal returns `promise` itself. An already-aborted signal rejects with its reason\n * without waiting. The abort listener is removed as soon as `promise` settles. A rejection of\n * `promise` after the abort is handled here and never surfaces as unhandled.\n *\n * @param promise - The work to wait for\n * @param signal - The cancellation signal, or `undefined` for none\n * @returns A promise that settles as `promise` does, or rejects with `signal.reason` on abort\n *\n * @example\n * ```ts\n * const controller = new AbortController()\n * const raced = raceSignal(new Promise<string>(() => {}), controller.signal)\n * controller.abort(new Error('stop'))\n * await raced // rejects with the Error 'stop'\n * ```\n */\nexport function raceSignal<T>(promise: Promise<T>, signal: AbortSignal | undefined): Promise<T> {\n\tif (signal === undefined) return promise\n\treturn new Promise<T>((resolve, reject) => {\n\t\t// Aborting `settled` removes the abort listener after the promise settles.\n\t\tconst settled = new AbortController()\n\t\tif (signal.aborted) reject(signal.reason)\n\t\telse {\n\t\t\tsignal.addEventListener('abort', () => reject(signal.reason), {\n\t\t\t\tonce: true,\n\t\t\t\tsignal: settled.signal,\n\t\t\t})\n\t\t}\n\t\t// Settling an already-rejected race is a no-op, and this handler is what keeps a late\n\t\t// rejection of `promise` from surfacing as unhandled.\n\t\tvoid promise.then(resolve, reject).finally(() => settled.abort())\n\t})\n}\n","import type { JudgmentManagerInterface } from './types.js'\nimport type { JudgeInterface, JudgeRequest, Judgment, JudgmentInput } from '../types.js'\nimport { describeFaults, isArray, isRecord } from '@orkestrel/contract'\nimport { ConversationError } from './errors.js'\nimport { buildJudgments, requireJudgeResult, requireJudgment } from './helpers.js'\nimport { copyJSON, freezeJSON } from '../cloners.js'\nimport { judgeAnswerContract, judgeQuestionContract } from '../contracts.js'\nimport { JudgeAbortError, isJudgeAbortError } from '../errors.js'\nimport {\n\tdescribeQuestionRefusal,\n\tmatchesJudgment,\n\tremoveEntries,\n\tscanJudgmentTies,\n} from '../helpers.js'\nimport { isJudgeEntry, isJudgeQuestion } from '../validators.js'\n\n/**\n * Stores judgments in insertion order and asks a judge only for unmatched question identities.\n *\n * @remarks\n * Adding an existing key replaces it without changing its insertion position. Construction\n * restores recorded times; adding stamps the current epoch milliseconds. Caller input is owned\n * through JSON on arrival, so a proxied record or request is accepted where a structured clone\n * refuses it. Each record is stored deep-frozen, and every read returns that frozen record, so a\n * write to one throws in strict-mode code and a caller that edits a record copies it first.\n *\n * @example\n * ```ts\n * const judgments = new JudgmentManager()\n * const records = await judgments.resolve(judge, request, ['message-a'], signal)\n * ```\n */\nexport class JudgmentManager implements JudgmentManagerInterface {\n\treadonly #judgments: Map<string, Judgment>\n\n\t/**\n\t * Restores records without replacing their storage times.\n\t * @param judgments - The previously stored judgments to restore. Default: an empty list\n\t */\n\tconstructor(judgments: readonly Judgment[] = []) {\n\t\t// `Array.from` visits a hole in `judgments` as an undefined record, which `requireJudgment` refuses; `map` skips it.\n\t\tthis.#judgments = new Map(\n\t\t\tArray.from(judgments, (judgment) => requireJudgment(freezeJSON(judgment))).map((owned) => [\n\t\t\t\towned.id,\n\t\t\t\towned,\n\t\t\t]),\n\t\t)\n\t}\n\n\tget count(): number {\n\t\treturn this.#judgments.size\n\t}\n\n\tadd(input: JudgmentInput): Judgment\n\tadd(inputs: readonly JudgmentInput[]): readonly Judgment[]\n\tadd(input: JudgmentInput | readonly JudgmentInput[]): Judgment | readonly Judgment[] {\n\t\tif (isArray(input)) return input.map((one) => this.#store(one))\n\t\treturn this.#store(input)\n\t}\n\n\tjudgment(id: string): Judgment | undefined {\n\t\treturn this.#judgments.get(id)\n\t}\n\n\tjudgments(): readonly Judgment[] {\n\t\treturn [...this.#judgments.values()]\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) return removeEntries(ids, (id) => this.#judgments.delete(id))\n\t\treturn this.#judgments.delete(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#judgments.clear()\n\t}\n\n\tasync resolve(\n\t\tjudge: JudgeInterface,\n\t\trequest: JudgeRequest,\n\t\tsources: readonly string[],\n\t\tsignal: AbortSignal,\n\t): Promise<readonly Judgment[]> {\n\t\t// The request is owned through JSON before it is compared or sent, as the judge engine owns\n\t\t// it, so a proxied request is accepted and a caller mutating it mid-flight changes nothing.\n\t\tconst state = copyJSON(request.state)\n\t\tif (!isJudgeEntry(state)) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t'conversation error: the judge state is not a judge entry',\n\t\t\t)\n\t\t}\n\t\tconst rendered = typeof state === 'string' ? state : JSON.stringify(state)\n\t\tconst origins = [...sources]\n\t\tconst entries = isRecord(request.questions) ? Object.entries(request.questions) : []\n\t\tif (entries.length === 0) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t'conversation error: a judge request needs at least one question, keyed by id',\n\t\t\t)\n\t\t}\n\t\tconst checked = entries.map(([id, raw]) => {\n\t\t\tconst question = copyJSON(raw)\n\t\t\tif (!isJudgeQuestion(question)) {\n\t\t\t\tconst faults = judgeQuestionContract.audit(question)\n\t\t\t\tthrow new ConversationError(\n\t\t\t\t\t'JUDGMENT',\n\t\t\t\t\t`conversation error: ${describeQuestionRefusal(id, faults, judgeQuestionContract.schema)}`,\n\t\t\t\t\t{ context: { question: id, faults } },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn [id, question] as const\n\t\t})\n\t\tconst matched = new Map(\n\t\t\tchecked.flatMap(([id, question]) => {\n\t\t\t\tconst judgment = this.#judgments.get(id)\n\t\t\t\treturn judgment !== undefined &&\n\t\t\t\t\tmatchesJudgment(judgment, question, origins, rendered, judge.model)\n\t\t\t\t\t? [[id, judgment] as const]\n\t\t\t\t\t: []\n\t\t\t}),\n\t\t)\n\t\tconst pending = checked.filter(([id]) => !matched.has(id))\n\t\tconst added =\n\t\t\tpending.length === 0\n\t\t\t\t? []\n\t\t\t\t: await this.#ask(\n\t\t\t\t\t\tjudge,\n\t\t\t\t\t\t{ state, questions: Object.fromEntries(pending) },\n\t\t\t\t\t\torigins,\n\t\t\t\t\t\trendered,\n\t\t\t\t\t\tsignal,\n\t\t\t\t\t)\n\t\tconst resolved = new Map([\n\t\t\t...matched,\n\t\t\t...added.map((judgment) => [judgment.id, judgment] as const),\n\t\t])\n\t\treturn entries.flatMap(([id]) => {\n\t\t\tconst judgment = resolved.get(id)\n\t\t\treturn judgment === undefined ? [] : [judgment]\n\t\t})\n\t}\n\n\t// Asks the judge for the unmatched questions and stores what it answered. An aborted ask stores\n\t// its partial answers before it rethrows, so finished work survives the cancellation.\n\tasync #ask(\n\t\tjudge: JudgeInterface,\n\t\tunmatched: JudgeRequest,\n\t\torigins: readonly string[],\n\t\trendered: string,\n\t\tsignal: AbortSignal,\n\t): Promise<readonly Judgment[]> {\n\t\tif (signal.aborted) throw new JudgeAbortError({ model: judge.model, answers: {} })\n\t\ttry {\n\t\t\tconst result = await judge.ask(unmatched, signal)\n\t\t\trequireJudgeResult(unmatched, result)\n\t\t\treturn this.add(buildJudgments(unmatched, result, origins, rendered, judge.model))\n\t\t} catch (error) {\n\t\t\tif (isJudgeAbortError(error))\n\t\t\t\tthis.add(buildJudgments(unmatched, error.partial, origins, rendered, judge.model))\n\t\t\tthrow error\n\t\t}\n\t}\n\n\t#store(input: JudgmentInput): Judgment {\n\t\tconst copy = copyJSON(input)\n\t\tif (isRecord(copy) && copy.answer !== undefined && !judgeAnswerContract.is(copy.answer)) {\n\t\t\tconst faults = judgeAnswerContract.audit(copy.answer)\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t`conversation error: a judgment answer must satisfy its shape\\n${describeFaults(faults, judgeAnswerContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tconst judgment = requireJudgment(\n\t\t\tfreezeJSON(isRecord(copy) ? { ...copy, time: Date.now() } : copy),\n\t\t)\n\t\t// The ties run on a write alone, because a record written before them can miss them.\n\t\tconst faults =\n\t\t\tjudgment.answer === undefined ? [] : scanJudgmentTies(judgment.question, judgment.answer)\n\t\tif (faults.length > 0) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'JUDGMENT',\n\t\t\t\t`conversation error: judgment ${judgment.id} answers its question wrongly\\n${describeFaults(faults, judgeAnswerContract.schema)}`,\n\t\t\t\t{ context: { question: judgment.id, faults } },\n\t\t\t)\n\t\t}\n\t\tthis.#judgments.set(judgment.id, judgment)\n\t\treturn judgment\n\t}\n}\n","import type {\n\tCompactOptions,\n\tConversationEventMap,\n\tConversationInterface,\n\tConversationOptions,\n\tConversationReferenceOptions,\n\tConversationSnapshot,\n\tConversationSummaryHandler,\n\tJudgmentManagerInterface,\n\tSection,\n} from './types.js'\nimport type { Message, MessageInput } from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { messageInputContract } from '../contracts.js'\nimport { collectExchanges, removeEntries, stripThinking } from '../helpers.js'\nimport { conversationSnapshotContract } from './contracts.js'\nimport { DEFAULT_CONVERSATION_KEEP } from './constants.js'\nimport { ConversationError } from './errors.js'\nimport {\n\tbuildRecapMessage,\n\tbuildSummaryMessage,\n\traceSignal,\n\trequireConversationSnapshot,\n\trequireMessageInput,\n\trequireKeep,\n\trequireCumulative,\n\trequireSectionsCap,\n\trequireSummary,\n} from './helpers.js'\nimport { JudgmentManager } from './JudgmentManager.js'\n\n/**\n * Represents a conversation — a live uncompacted tail of messages it owns directly above a flat\n * message store, plus compacted, summarized {@link Section} records and a `summarizable` flag,\n * with on-demand `rehydrate` and substring `search`, driven by a provider-agnostic\n * {@link ConversationSummaryHandler} seam so `core` never imports a provider. Observable through\n * its own `emitter`.\n *\n * @remarks\n * - **Live tail + sections.** The conversation owns its live tail directly — `#messages` is an\n * insertion-ordered `Map` of immutable {@link Message} records keyed by their minted id\n * (`add` / `message` / `messages` / `remove` / `clear` / `count`), exactly as a `Workspace`\n * owns its files (no separate per-value manager). `#sections` are the compacted history\n * (oldest → newest), each a summarized slice that retains its originals.\n * - **Stored roles.** Every stored message plays a `ConversationRole`. `add` checks every role of\n * a batch before it stores any and throws `ConversationError` code `'ROLE'` for a `system` or\n * unknown role; the `system` message is built per request and never stored. A hydrating\n * `snapshot` is checked by `requireConversationSnapshot` before any member is seated: a\n * malformed snapshot or a repeated id across section ids, section message ids, and live message\n * ids throws `'SNAPSHOT'`, and a stored `system` message throws `'ROLE'`. The checked\n * snapshot's top-level members are read once, and its section and message objects are seated by\n * reference.\n * - **`view()`.** Each section folds to one synthetic summary message (role `'assistant'` — a\n * prior-context recap — keyed by the stable `id` of the section), then the live messages\n * verbatim. The per-section summaries are the compaction benefit.\n * - **`compact()`.** Folds the oldest `count - keep` live messages into a section (its\n * `summary` from `#summarize`), removes them from the live tail by id, and emits `compact`.\n * The fold stops before the newest user message, so the request a run serves and its turns\n * stay live. An exchange is a user message and every message after it up to the next user\n * message. Leading messages form a separate exchange, retained until the first user exchange\n * can also fold. A cut inside an exchange moves back to its start, so a fold removes whole\n * exchanges. A cut inside an assistant call group, which only a group spanning two exchanges\n * allows, moves before the group, so a tool result never stays live without its call. Returns\n * the section, or `undefined` when nothing folds. A {@link ConversationError} is thrown when no\n * `#summarize` was supplied. Calls run one at a time, a fold commits only when its messages are\n * still the live entries after the summarizer settles, and `options.signal` rejects the call\n * with its reason at once and commits nothing after the abort. A digest that is not a string or\n * whose trimmed text is empty refuses the fold it would commit with `'SUMMARY'`; such a digest for\n * a capped merge refuses the merge and keeps the fold already committed, one section over the cap,\n * which the next sound merge collapses.\n * - **Cumulative.** With `cumulative`, a fold makes one summarizer call over the framed recaps of all\n * prior sections then the slice, and replaces those sections with one that retains their\n * originals then the slice. It emits `compact` once and never `collapse`, commits nothing when\n * the call throws, aborts, or the slice or prior sections changed, and cannot combine with a\n * `sections` cap (`'SECTIONS'`).\n * - **`rehydrate(id)` / `search(query)`.** `rehydrate` returns the full original messages of a\n * known section and emits its `id` through `rehydrate`, even when the list is empty. An unknown\n * id returns `undefined` without an event. The read never reinserts messages. `search` is a case-insensitive\n * substring scan of `content` across all messages (the originals of every section + the live\n * tail).\n * - **Observable.** The owned {@link emitter} ({@link ConversationEventMap}) carries\n * `compact` / `collapse` / `rehydrate`, emitted directly, strictly after the state change;\n * the emitter isolates a listener throw and routes it to its `error` handler (the `error`\n * option), so a buggy observer can never corrupt a compaction.\n *\n * @example\n * ```ts\n * const conversation = new Conversation({ summarize: async (m) => `recap of ${m.length}` })\n * conversation.add([\n * \t{ role: 'user', content: 'Hello' },\n * \t{ role: 'assistant', content: 'Hi there' },\n * \t{ role: 'user', content: 'What did I say?' },\n * ])\n * const section = await conversation.compact() // folds the first two into one summarized section\n * conversation.view() // [<recap of 2>, { role: 'user', content: 'What did I say?' }]\n * ```\n */\nexport class Conversation implements ConversationInterface {\n\treadonly #id: string\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a compaction.\n\treadonly #emitter: Emitter<ConversationEventMap>\n\t// The provider-agnostic summarizer seam — `undefined` ⇒ `compact()` throws (a conversation\n\t// can still store + view a live tail; it cannot fold).\n\treadonly #summarize: ConversationSummaryHandler | undefined\n\t// How many recent live messages a `compact()` retains verbatim (older ones fold).\n\treadonly #keep: number\n\t// The optional cap on the compacted sections list — `undefined` ⇒ unlimited. Enforced\n\t// after appending a fresh `compact()` fold: an overflow folds the oldest sections into one.\n\treadonly #cap: number | undefined\n\t// Whether a fold replaces every prior section with one that digests their recaps and the slice.\n\t// Never combined with a cap.\n\treadonly #cumulative: boolean\n\t// The compacted history, oldest → newest — each summarized slice retains its originals.\n\t// Replaced on every change rather than mutated in place.\n\t#sections: readonly Section[]\n\t// The live uncompacted tail the conversation owns directly — an insertion-ordered Map of\n\t// immutable messages keyed by their minted id.\n\treadonly #messages: Map<string, Message>\n\treadonly #judgments: JudgmentManager\n\t// The folds in flight, one per `compact()` call from its start until it settles. A later call\n\t// waits on every member it finds, so an aborted waiter never releases it ahead of a fold still\n\t// running.\n\treadonly #pending = new Set<Promise<Section | undefined>>()\n\n\tconstructor(options?: ConversationOptions) {\n\t\t// A summarizer is a function and is never serialized, so it comes from options beside the\n\t\t// snapshot. Restoring is silent: nothing was edited, so no event fires. The snapshot is checked\n\t\t// before any member is seated, and every member is seated from the checked read alone.\n\t\tconst supplied = options?.snapshot\n\t\tconst snapshot =\n\t\t\tsupplied === undefined\n\t\t\t\t? undefined\n\t\t\t\t: requireConversationSnapshot(supplied, conversationSnapshotContract)\n\t\tthis.#judgments = new JudgmentManager(snapshot?.judgments)\n\t\tthis.#id = snapshot?.id ?? options?.id ?? crypto.randomUUID()\n\t\tthis.#emitter = new Emitter<ConversationEventMap>(options)\n\t\tthis.#summarize = options?.summarize\n\t\tthis.#keep = requireKeep(options?.keep ?? DEFAULT_CONVERSATION_KEEP)\n\t\tthis.#cap = requireSectionsCap(options?.sections)\n\t\tthis.#cumulative = options?.cumulative ?? false\n\t\trequireCumulative(this.#cumulative, this.#cap)\n\t\tthis.#sections = snapshot === undefined ? [] : [...snapshot.sections]\n\t\tthis.#messages = new Map(snapshot?.messages.map((message) => [message.id, message]))\n\t}\n\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\tget judgments(): JudgmentManagerInterface {\n\t\treturn this.#judgments\n\t}\n\n\tget emitter(): EmitterInterface<ConversationEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget sections(): readonly Section[] {\n\t\treturn [...this.#sections]\n\t}\n\n\tget summarizable(): boolean {\n\t\t// True exactly when a summarizer was supplied — the clean signal the automatic compaction of\n\t\t// the agent loop gates on (a non-summarizable conversation is never auto-compacted, so the\n\t\t// auto path never throws the `SUMMARIZER` ConversationError). A manual `compact()` still throws.\n\t\treturn this.#summarize !== undefined\n\t}\n\n\tget count(): number {\n\t\treturn this.#messages.size\n\t}\n\n\tadd(input: MessageInput): Message\n\tadd(inputs: readonly MessageInput[]): readonly Message[]\n\tadd(input: MessageInput | readonly MessageInput[]): Message | readonly Message[] {\n\t\tif (isArray(input)) {\n\t\t\t// Every input is checked before any element is stored, so a refused batch stores nothing.\n\t\t\tinput.forEach((one) => requireMessageInput(one, messageInputContract))\n\t\t\treturn input.map((one) => this.#create(one))\n\t\t}\n\t\treturn this.#create(requireMessageInput(input, messageInputContract))\n\t}\n\n\tmessage(id: string): Message | undefined {\n\t\treturn this.#messages.get(id)\n\t}\n\n\tmessages(): readonly Message[] {\n\t\treturn [...this.#messages.values()]\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) {\n\t\t\treturn removeEntries(ids, (id) => this.#messages.delete(id))\n\t\t}\n\t\treturn this.#messages.delete(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#messages.clear()\n\t}\n\n\tview(): readonly Message[] {\n\t\t// The recap is framed (a `[Summary of earlier messages]` label prefix) so a small model reads\n\t\t// it as a condensed recap of prior turns, not as a literal assistant turn it must echo / treat\n\t\t// as the latest answer — a lean label (a handful of tokens), proven no-bloat by a test guard.\n\t\treturn [\n\t\t\t...this.#sections.map((section) => buildRecapMessage(section)),\n\t\t\t...this.#messages.values(),\n\t\t]\n\t}\n\n\tasync compact(options?: CompactOptions): Promise<Section | undefined> {\n\t\tconst summarize = this.#summarize\n\t\tif (summarize === undefined) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'SUMMARIZER',\n\t\t\t\t'cannot compact a conversation without a summarizer',\n\t\t\t)\n\t\t}\n\t\tconst cap = requireSectionsCap(options?.sections ?? this.#cap)\n\t\trequireCumulative(this.#cumulative, cap)\n\t\tconst keep = requireKeep(options?.keep ?? this.#keep)\n\t\tconst signal = options?.signal\n\t\tsignal?.throwIfAborted()\n\t\t// Queue behind every earlier fold, captured before this call adds its own. An earlier waiter\n\t\t// that aborts leaves the folds it waited on in the set, so a later call still waits for them.\n\t\tconst previous = this.#pending.size === 0 ? undefined : Promise.allSettled([...this.#pending])\n\t\tconst fold = this.#fold(previous, summarize, cap, keep, signal)\n\t\tthis.#pending.add(fold)\n\t\tvoid fold.then(\n\t\t\t() => this.#pending.delete(fold),\n\t\t\t() => this.#pending.delete(fold),\n\t\t)\n\t\treturn fold\n\t}\n\n\trehydrate(id: string): readonly Message[] | undefined {\n\t\tconst section = this.#sections.find((one) => one.id === id)\n\t\tif (section === undefined) return undefined\n\t\t// Emitted after resolving, because a read has no mutation for a listener to perturb.\n\t\tthis.#emitter.emit('rehydrate', id)\n\t\treturn section.messages\n\t}\n\n\tsearch(query: string): readonly Message[] {\n\t\t// Section originals (oldest → newest) come before the live tail.\n\t\tconst needle = query.toLowerCase()\n\t\tconst all = [\n\t\t\t...this.#sections.flatMap((section) => section.messages),\n\t\t\t...this.#messages.values(),\n\t\t]\n\t\treturn all.filter((message) => message.content.toLowerCase().includes(needle))\n\t}\n\n\treference(options?: ConversationReferenceOptions): string {\n\t\t// The leading marker states the block is not part of the live conversation, so a small\n\t\t// model reads the cherry-picked excerpts as foreign material it attributes to that source,\n\t\t// never as its own latest turns.\n\t\tconst label = options?.label ?? this.#id\n\t\tconst messages = options?.messages ?? []\n\t\t// `Array.from` visits a hole in `messages` as an undefined message, as iteration does; `map` skips it.\n\t\treturn [\n\t\t\t`[Reference — conversation \"${label}\" — NOT part of this conversation]`,\n\t\t\t...(messages.length > 0\n\t\t\t\t? [\n\t\t\t\t\t\t'Relevant messages:',\n\t\t\t\t\t\t...Array.from(messages, (message) => `- ${message.role}: ${message.content}`),\n\t\t\t\t\t]\n\t\t\t\t: []),\n\t\t].join('\\n')\n\t}\n\n\tsnapshot(): ConversationSnapshot {\n\t\t// The summarizer / keep are not serialized — they are live config re-supplied on hydrate\n\t\t// (a ConversationSummaryHandler is a function, not data).\n\t\treturn {\n\t\t\tid: this.#id,\n\t\t\tsections: this.sections,\n\t\t\tmessages: this.messages(),\n\t\t\t...(this.#judgments.count === 0 ? {} : { judgments: this.#judgments.judgments() }),\n\t\t}\n\t}\n\n\t// One serialized fold: wait for the earlier call, read the live tail, summarize, then commit\n\t// only what is unchanged. Every await is raced against the signal and followed by a check, so\n\t// an abort commits nothing after it.\n\tasync #fold(\n\t\tprevious: Promise<unknown> | undefined,\n\t\tsummarize: ConversationSummaryHandler,\n\t\tcap: number | undefined,\n\t\tkeep: number,\n\t\tsignal: AbortSignal | undefined,\n\t): Promise<Section | undefined> {\n\t\tif (previous !== undefined) await raceSignal(previous, signal)\n\t\tsignal?.throwIfAborted()\n\t\tconst live = [...this.#messages.values()]\n\t\t// The newest user message is the request a run serves, so it and its turns stay live.\n\t\tconst newest = live.findLastIndex((message) => message.role === 'user')\n\t\tconst first = live.findIndex((message) => message.role === 'user')\n\t\tconst wanted = Math.min(\n\t\t\tkeep <= 0 ? live.length : live.length - keep,\n\t\t\tnewest === -1 ? live.length : newest,\n\t\t)\n\t\t// A cut inside an exchange moves back to that exchange's start so a whole exchange folds or\n\t\t// stays. At most one exchange strictly contains the cut.\n\t\tconst { aligned } = collectExchanges(live).reduce(\n\t\t\t(cut, exchange) => {\n\t\t\t\tconst end = cut.start + exchange.length\n\t\t\t\treturn { start: end, aligned: cut.start < wanted && wanted < end ? cut.start : cut.aligned }\n\t\t\t},\n\t\t\t{ start: 0, aligned: wanted },\n\t\t)\n\t\t// Retain the leading context until the first user exchange can fold with it.\n\t\tconst fold = first >= 0 && aligned <= first ? 0 : aligned\n\t\tif (fold <= 0) return undefined\n\t\tconst slice = live.slice(0, fold)\n\t\t// A cumulative fold digests every prior recap (framed, oldest first) ahead of the slice in one\n\t\t// call; the originals behind those recaps are never sent.\n\t\tconst prior = this.#cumulative ? this.#sections : []\n\t\tconst request = [...prior.map((one) => buildRecapMessage(one)), ...stripThinking(slice, 'none')]\n\t\tconst summary = await raceSignal(summarize(request, signal), signal)\n\t\tsignal?.throwIfAborted()\n\t\trequireSummary(summary)\n\t\t// A `remove` or `clear` while the summarizer ran changed what this fold covers, so commit\n\t\t// nothing rather than fold messages the caller dropped back into a section.\n\t\tif (!slice.every((message) => this.#messages.get(message.id) === message)) return undefined\n\t\t// The recaps the digest covers must still be the sections held, or the replacement would drop\n\t\t// a section it never summarized.\n\t\tif (\n\t\t\tthis.#cumulative &&\n\t\t\t(this.#sections.length !== prior.length ||\n\t\t\t\t!prior.every((one, index) => this.#sections[index] === one))\n\t\t) {\n\t\t\treturn undefined\n\t\t}\n\t\tconst section: Section = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\tsummary,\n\t\t\tmessages: this.#cumulative ? [...prior.flatMap((one) => one.messages), ...slice] : slice,\n\t\t}\n\t\tfor (const message of slice) this.#messages.delete(message.id)\n\t\tthis.#sections = this.#cumulative ? [section] : [...this.#sections, section]\n\t\t// Preserve the original sections if the merge fails by replacing them only after summarizing.\n\t\tif (cap !== undefined && this.#sections.length > cap) {\n\t\t\tconst overflow = this.#sections.length - cap + 1\n\t\t\tconst folded = this.#sections.slice(0, overflow)\n\t\t\tconst digest = await raceSignal(\n\t\t\t\tsummarize(\n\t\t\t\t\tfolded.map((one) => buildSummaryMessage(one)),\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t\tsignal,\n\t\t\t)\n\t\t\tsignal?.throwIfAborted()\n\t\t\trequireSummary(digest)\n\t\t\t// Only a fold changes `#sections`, and folds run one at a time, so the head holds here;\n\t\t\t// the check keeps a merge from ever replacing sections it did not summarize.\n\t\t\tif (!folded.every((one, index) => this.#sections[index] === one)) return undefined\n\t\t\tconst merged: Section = {\n\t\t\t\tid: crypto.randomUUID(),\n\t\t\t\tsummary: digest,\n\t\t\t\tmessages: folded.flatMap((one) => one.messages),\n\t\t\t}\n\t\t\tthis.#sections = [merged, ...this.#sections.slice(overflow)]\n\t\t\tthis.#emitter.emit('collapse', merged)\n\t\t}\n\t\t// Observe the section last, so a swallowed listener throw can't perturb the fold.\n\t\tthis.#emitter.emit('compact', section)\n\t\treturn section\n\t}\n\n\t// Spread each optional only when present, so an absent optional is never stored as `undefined`.\n\t#create(input: MessageInput): Message {\n\t\tconst message: Message = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\trole: input.role,\n\t\t\tcontent: input.content,\n\t\t\t...(input.calls === undefined\n\t\t\t\t? {}\n\t\t\t\t: {\n\t\t\t\t\t\tcalls: input.calls.map(({ id, name, arguments: arguments_ }) => ({\n\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\tname,\n\t\t\t\t\t\t\targuments: arguments_,\n\t\t\t\t\t\t})),\n\t\t\t\t\t}),\n\t\t\t...(input.call === undefined ? {} : { call: input.call }),\n\t\t\t...(input.images === undefined ? {} : { images: input.images }),\n\t\t\t...(input.thinking === undefined ? {} : { thinking: input.thinking }),\n\t\t}\n\t\tthis.#messages.set(message.id, message)\n\t\treturn message\n\t}\n}\n","import type {\n\tConversationInput,\n\tConversationInterface,\n\tConversationManagerInterface,\n\tConversationManagerOptions,\n\tConversationStoreInterface,\n\tConversationSummaryHandler,\n} from './types.js'\nimport { isArray, preview } from '@orkestrel/contract'\nimport { DEFAULT_CONVERSATION_KEEP } from './constants.js'\nimport { Conversation } from './Conversation.js'\nimport { ConversationError } from './errors.js'\nimport { requireKeep } from './helpers.js'\nimport { removeEntries } from '../helpers.js'\n\n/**\n * Registers {@link Conversation} instances keyed by `id`, in insertion order, with an active pointer —\n * the id-keyed store over the conversation layer, the `active` / `switch` seam the\n * {@link import('../contexts/index.js').AgentContext} renders, and the durable `open` / `save`\n * store seam. Event-free (a registry, like\n * {@link import('@orkestrel/workspace').WorkspaceManager}); the observability lives on each\n * {@link Conversation}.\n *\n * @remarks\n * - **Registry.** Conversations live in an insertion-ordered `Map` keyed by `id`. `add(input?)`\n * mints a {@link Conversation} (its `id` from `input` or `crypto.randomUUID()`), flowing the\n * default `#summarize` / `#keep` of the manager in unless the `input` overrides them, and stores\n * it (an already-present `id` throws a `ConversationError` coded `'ID'`, because replacing a live\n * conversation would orphan its turns). `count` is the map size,\n * `conversation(id)` looks one up, `conversations()` lists them in insertion order.\n * - **Active pointer.** `active` is the active conversation (the conversation an agent run binds\n * when it names none), `undefined` until the first `add` (which auto-activates it — a registry\n * with conversations always has one active). A subsequent `add` leaves `active` unchanged.\n * `switch(id)` re-points `active` to the conversation with `id` and returns it; an unknown `id`\n * returns `undefined` and leaves `active` unchanged (the lenient lookup style — never throws,\n * no added error code).\n * - **Removal.** `remove` drops one by id, or a batch — `true` only when every supplied id was\n * removed; removing the active conversation sets `active` to `undefined`. `clear` empties the registry\n * and sets `active` to `undefined`.\n * - **Event-free.** A purely registry store — no Emitter, no events (each conversation owns\n * its own observable `emitter`).\n *\n * @example\n * ```ts\n * const manager = new ConversationManager({ summarize: async (m) => `recap of ${m.length}` })\n * const conversation = manager.add() // auto-activates — active === conversation\n * manager.add({ id: 'scratch' }) // leaves active unchanged\n * manager.switch('scratch') // re-points active to the 'scratch' conversation\n * manager.count // 2\n * ```\n */\nexport class ConversationManager implements ConversationManagerInterface {\n\treadonly #conversations = new Map<string, ConversationInterface>()\n\t// The active conversation's id — the message source the context renders; `undefined` until the\n\t// first `add` auto-activates one (kept as an id, never a stale reference, so a re-add / removal\n\t// of that id is reflected through the live map lookup in `active`).\n\t#active: string | undefined\n\t// The default summarizer flowed into every conversation `add` creates (a per-`add` override\n\t// wins); a conversation created with neither cannot `compact` (it throws a ConversationError).\n\treadonly #summarize: ConversationSummaryHandler | undefined\n\t// The default retained-tail size flowed into every conversation `add` creates (overridable).\n\treadonly #keep: number\n\t// The default `sections` cap flowed into every conversation `add` creates (overridable); `undefined` ⇒ unlimited.\n\treadonly #sections: number | undefined\n\t// The optional durable store backing `open` / `save`; `undefined` ⇒ registry-only (both lenient).\n\treadonly #store: ConversationStoreInterface | undefined\n\n\tconstructor(options?: ConversationManagerOptions) {\n\t\tthis.#summarize = options?.summarize\n\t\tthis.#keep = requireKeep(options?.keep ?? DEFAULT_CONVERSATION_KEEP)\n\t\tthis.#sections = options?.sections\n\t\tthis.#store = options?.store\n\t}\n\n\tget count(): number {\n\t\treturn this.#conversations.size\n\t}\n\n\tget active(): ConversationInterface | undefined {\n\t\treturn this.#active === undefined ? undefined : this.#conversations.get(this.#active)\n\t}\n\n\tconversation(id: string): ConversationInterface | undefined {\n\t\treturn this.#conversations.get(id)\n\t}\n\n\tconversations(): readonly ConversationInterface[] {\n\t\treturn [...this.#conversations.values()]\n\t}\n\n\tadd(input?: ConversationInput): ConversationInterface {\n\t\tconst held = input?.id ?? input?.snapshot?.id\n\t\tif (held !== undefined && this.#conversations.has(held)) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'ID',\n\t\t\t\t`conversation ${preview(held)} is already live; pick another id, or remove it first`,\n\t\t\t\t{ context: { conversation: held } },\n\t\t\t)\n\t\t}\n\t\t// An optional `snapshot` hydrates through the declared `ConversationOptions.snapshot` seam,\n\t\t// so the live summarize / keep ride beside it in the same options object.\n\t\tconst sections = input?.sections ?? this.#sections\n\t\tconst summarize = input?.summarize ?? this.#summarize\n\t\tconst conversation = new Conversation({\n\t\t\t...(input?.id === undefined ? {} : { id: input.id }),\n\t\t\t...(input?.on === undefined ? {} : { on: input.on }),\n\t\t\t...(input?.error === undefined ? {} : { error: input.error }),\n\t\t\t...(summarize === undefined ? {} : { summarize }),\n\t\t\tkeep: input?.keep ?? this.#keep,\n\t\t\t...(sections === undefined ? {} : { sections }),\n\t\t\t...(input?.cumulative === undefined ? {} : { cumulative: input.cumulative }),\n\t\t\t...(input?.snapshot === undefined ? {} : { snapshot: input.snapshot }),\n\t\t})\n\t\tthis.#conversations.set(conversation.id, conversation)\n\t\t// First conversation auto-activates: a registry that holds conversations always has one active.\n\t\tif (this.#active === undefined) this.#active = conversation.id\n\t\treturn conversation\n\t}\n\n\tswitch(id: string): ConversationInterface | undefined {\n\t\t// Lenient (mirrors `conversation(id)`): an unknown id re-points nothing and returns undefined.\n\t\tconst conversation = this.#conversations.get(id)\n\t\tif (conversation === undefined) return undefined\n\t\tthis.#active = id\n\t\treturn conversation\n\t}\n\n\tasync open(id: string): Promise<ConversationInterface | undefined> {\n\t\tconst existing = this.#conversations.get(id)\n\t\tif (existing !== undefined) {\n\t\t\tthis.#active = id\n\t\t\treturn existing\n\t\t}\n\t\tif (this.#store === undefined) return undefined\n\t\tconst snapshot = await this.#store.get(id)\n\t\t// A conversation registered while the read was pending is live state; the stored snapshot\n\t\t// is older, so it must not overwrite it.\n\t\tconst registered = this.#conversations.get(id)\n\t\tif (registered !== undefined) {\n\t\t\tthis.#active = id\n\t\t\treturn registered\n\t\t}\n\t\tif (snapshot === undefined) return undefined\n\t\t// A store answering with another conversation's snapshot would register and activate the\n\t\t// wrong id, so only a snapshot of `id` hydrates.\n\t\tif (snapshot.id !== id) {\n\t\t\tthrow new ConversationError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`the store answered ${preview(id)} with the snapshot of ${preview(snapshot.id)}; a store returns only the row of the id it is asked`,\n\t\t\t)\n\t\t}\n\t\t// Reuse `add` so the default summarize / keep of the manager flow into the rehydrated conversation.\n\t\tconst conversation = this.add({ snapshot })\n\t\t// Re-point `active` explicitly: `add` only auto-activates the first conversation, so an open\n\t\t// into a non-empty registry must still make the rehydrated one active.\n\t\tthis.#active = conversation.id\n\t\treturn conversation\n\t}\n\n\tasync save(id: string): Promise<boolean> {\n\t\tconst conversation = this.#conversations.get(id)\n\t\tif (this.#store === undefined || conversation === undefined) return false\n\t\tawait this.#store.set(conversation.snapshot())\n\t\treturn true\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) {\n\t\t\t// Each present id still clears `active` when it was the active conversation.\n\t\t\treturn removeEntries(ids, (id) => this.#drop(id))\n\t\t}\n\t\treturn this.#drop(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#conversations.clear()\n\t\t// An emptied registry has no active conversation.\n\t\tthis.#active = undefined\n\t}\n\n\t// Delete one conversation by id; when it was the active one, clear the active pointer (a removed\n\t// conversation can never stay active).\n\t#drop(id: string): boolean {\n\t\tconst removed = this.#conversations.delete(id)\n\t\tif (removed && this.#active === id) this.#active = undefined\n\t\treturn removed\n\t}\n}\n","import type { ConversationSnapshot, ConversationStoreInterface } from '../types.js'\n\n/**\n * Implements the {@link ConversationStoreInterface} in memory — a process-lifetime `Map` of\n * {@link ConversationSnapshot} records keyed by conversation id, the default store\n * {@link import('../factories.js').createMemoryConversationStore} builds and the default\n * backing for `open` / `save`. The exact twin of\n * {@link import('@orkestrel/workspace').MemoryWorkspaceStore}.\n *\n * @remarks\n * A plain `Map<string, ConversationSnapshot>` — the snapshot is already pure,\n * self-contained JSON, so no encoding is needed for the memory tier. Like the\n * {@link import('@orkestrel/workspace').MemoryWorkspaceStore} it twins,\n * there is no idle-TTL and no eviction: a persisted conversation lives until an explicit `delete`. A\n * durable backend (JSON / SQLite / IndexedDB) swaps in through the same interface without touching\n * the {@link import('../ConversationManager.js').ConversationManager} or the\n * {@link import('../Conversation.js').Conversation} — its driver-pluggable twin is\n * {@link import('./DatabaseConversationStore.js').DatabaseConversationStore} (the snapshot as one\n * opaque JSON column).\n *\n * - **`get` resolves the persisted snapshot for an id**, or `undefined` if none is stored.\n * - **`set` inserts / replaces under the snapshot's own `id`** (no separate id param).\n * - **`delete` drops a snapshot by id**; an absent id is a no-op (no throw).\n * - **`get` hands back the stored snapshot object itself**: its messages and sections are shared\n * with the store, so an edit to them changes what the next `get` returns, and its judgments are\n * frozen records, so a write to one throws `TypeError` in strict-mode code; copy a judgment\n * through `copyJSON` to migrate or redact it.\n *\n * The public surface is exactly `get` / `set` / `delete` — no extra members (the method\n * bijection with {@link ConversationStoreInterface}). Hydration is a caller concern: a\n * {@link import('../ConversationManager.js').ConversationManager} reads a snapshot back and rebuilds\n * the live conversation through the `snapshot` option (its `open` / `save`).\n *\n * @example\n * ```ts\n * import { createConversation, createMemoryConversationStore } from '@orkestrel/agent'\n *\n * const store = createMemoryConversationStore()\n * const conversation = createConversation()\n * conversation.add({ role: 'user', content: 'hello' })\n * await store.set(conversation.snapshot()) // persist the conversation\n * const snapshot = await store.get(conversation.id)\n * await store.delete(conversation.id) // drop it\n * ```\n */\nexport class MemoryConversationStore implements ConversationStoreInterface {\n\treadonly #snapshots = new Map<string, ConversationSnapshot>()\n\n\t/**\n\t * Resolves the persisted snapshot for `id`, or `undefined` if none is stored.\n\t *\n\t * @param id - The conversation id to resolve (a {@link ConversationSnapshot.id})\n\t * @returns The persisted snapshot, or `undefined` if absent\n\t */\n\tget(id: string): Promise<ConversationSnapshot | undefined> {\n\t\treturn Promise.resolve(this.#snapshots.get(id))\n\t}\n\n\t/**\n\t * Inserts or replaces a snapshot under its own `snapshot.id` (no separate id param —\n\t * mirroring the `set` method of {@link import('@orkestrel/workspace').WorkspaceStoreInterface}).\n\t *\n\t * @param snapshot - The snapshot to store (keyed by its `id`)\n\t * @returns A promise that resolves after the snapshot is stored\n\t */\n\tset(snapshot: ConversationSnapshot): Promise<void> {\n\t\tthis.#snapshots.set(snapshot.id, snapshot)\n\t\treturn Promise.resolve()\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op (no throw).\n\t *\n\t * @param id - The conversation id to drop\n\t * @returns A promise that resolves after the snapshot is dropped\n\t */\n\tdelete(id: string): Promise<void> {\n\t\t// `Map.delete` of an absent id is a no-op, so no presence check is needed.\n\t\tthis.#snapshots.delete(id)\n\t\treturn Promise.resolve()\n\t}\n}\n","import type {\n\tConversationSnapshot,\n\tConversationSnapshotRow,\n\tConversationStoreInterface,\n} from '../types.js'\nimport type { AuditFault } from '@orkestrel/contract'\nimport type { TableInterface } from '@orkestrel/database'\nimport { describeFaults, preview } from '@orkestrel/contract'\nimport { conversationSnapshotContract } from '../contracts.js'\nimport { ConversationError } from '../errors.js'\nimport { isConversationSnapshot } from '../validators.js'\nimport { describeSnapshotRefusal } from '../../helpers.js'\n\n/**\n * Backs a {@link ConversationStoreInterface} with one table of the `databases` layer — a\n * conversation's durable state is a row holding the snapshot as one opaque JSON column, narrowed\n * back on `get` by {@link import('../validators.js').isConversationSnapshot}, so persistence\n * reduces to keyed point-access (`get` / `set` / `delete`) over a {@link TableInterface}. The\n * driver-pluggable twin of the plain-`Map`\n * {@link import('./MemoryConversationStore.js').MemoryConversationStore}, and the exact twin of\n * {@link import('@orkestrel/workspace').DatabaseWorkspaceStore}.\n *\n * @remarks\n * The store is driver-agnostic: it holds a single {@link TableInterface} whose backend (memory,\n * JSON, SQLite, IndexedDB) is chosen by whoever builds it (the factories), so a JSON / SQLite /\n * IndexedDB backend swaps in without touching the\n * {@link import('../ConversationManager.js').ConversationManager} or the\n * {@link import('../Conversation.js').Conversation} — the same seam as\n * {@link import('@orkestrel/workspace').DatabaseWorkspaceStore}. Default driver: memory\n * ({@link import('../factories.js').createDatabaseConversationStore} passes\n * `createMemoryDriver()`), so it also works in memory out of the box; you opt into the\n * durable plumbing by passing a JSON / SQLite / IndexedDB driver.\n *\n * The {@link ConversationSnapshot} is stored as one opaque JSON column — the table is a row of\n * `{ id; snapshot }` ({@link ConversationSnapshotRow}), the snapshot the whole JSON blob (a\n * `rawShape` column the factory builds) — exactly as `DatabaseWorkspaceStore` stores its snapshot.\n * The snapshot is already a complete, self-contained, pure-JSON payload, so storing it whole is\n * lossless and keeps the row type flat (`snapshot` reads back as `unknown`).\n *\n * - **`set(snapshot)` upserts under the snapshot's own `id`** (no separate id param) — it writes\n * the row `{ id: snapshot.id, snapshot }`.\n * - **`get(id)` resolves the stored snapshot for an id**, narrowing the opaque JSON column back to\n * a {@link ConversationSnapshot} ({@link import('../validators.js').isConversationSnapshot} — the\n * total guard for an untrusted storage read), or `undefined` only when no row is stored. A row whose\n * blob is malformed or names another id throws a `ConversationError` coded `'SNAPSHOT'`, so `open`\n * and `create` stop rather than overwrite it, and `delete` still removes it.\n * - **`delete(id)` drops a snapshot by id**; an absent id is a no-op (no throw).\n *\n * Unlike a session store there is no idle-TTL / eviction — a persisted conversation lives until an\n * explicit `delete`. The public surface is exactly `get` / `set` / `delete` — no extra members (the\n * method bijection with {@link ConversationStoreInterface}). Hydration stays a caller concern: a\n * {@link import('../ConversationManager.js').ConversationManager} reads a snapshot back and rebuilds\n * the live conversation through the `snapshot` option (its `open` / `save`).\n *\n * @example\n * ```ts\n * import { createConversation, createDatabaseConversationStore } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseConversationStore(createMemoryDriver()) // a durable driver swaps in here\n * const conversation = createConversation()\n * conversation.add({ role: 'user', content: 'hello' })\n * await store.set(conversation.snapshot()) // persist the conversation (one JSON column)\n * const snapshot = await store.get(conversation.id)\n * await store.delete(conversation.id) // drop it\n * ```\n */\nexport class DatabaseConversationStore implements ConversationStoreInterface {\n\treadonly #table: TableInterface<ConversationSnapshotRow>\n\n\t/**\n\t * Wraps a table as a conversation store.\n\t *\n\t * @param table - The {@link TableInterface} holding the snapshots — its row is the\n\t * {@link ConversationSnapshotRow} `{ id; snapshot }` shape (the snapshot one opaque JSON column)\n\t */\n\tconstructor(table: TableInterface<ConversationSnapshotRow>) {\n\t\tthis.#table = table\n\t}\n\n\t/**\n\t * Resolves the persisted snapshot for `id`, narrowing the opaque JSON column back to a\n\t * `ConversationSnapshot`.\n\t *\n\t * @remarks\n\t * The column is an untrusted storage read, so a blob that fails\n\t * {@link import('../validators.js').isConversationSnapshot}, or whose own `id` is not `id`, throws\n\t * rather than reading as absent, which would let a later `set` overwrite it.\n\t *\n\t * @param id - The conversation id to resolve (the row key)\n\t * @returns The persisted snapshot, or `undefined` when no row is stored\n\t * @throws {ConversationError} Thrown when the row's blob is malformed or names another id (code `'SNAPSHOT'`), with the faults under `context.faults`\n\t */\n\tasync get(id: string): Promise<ConversationSnapshot | undefined> {\n\t\tconst row = await this.#table.get(id)\n\t\tif (row === undefined) return undefined\n\t\t// The snapshot crosses back as an untrusted storage read (a structured clone / a JSON row),\n\t\t// so narrow the opaque JSON column with the total boundary guard rather than a cast;\n\t\t// `audit` runs only after the guard refused, to fill the faults.\n\t\tconst snapshot = row.snapshot\n\t\tif (isConversationSnapshot(snapshot)) {\n\t\t\tif (snapshot.id === id) return snapshot\n\t\t\tconst faults: readonly AuditFault[] = [\n\t\t\t\t{\n\t\t\t\t\treason: 'type',\n\t\t\t\t\tpath: ['id'],\n\t\t\t\t\texpected: 'literal',\n\t\t\t\t\treceived: preview(snapshot.id),\n\t\t\t\t},\n\t\t\t]\n\t\t\tthrow new ConversationError('SNAPSHOT', describeSnapshotRefusal('conversation', id, faults), {\n\t\t\t\tcontext: { id, faults },\n\t\t\t})\n\t\t}\n\t\tconst faults = conversationSnapshotContract.audit(snapshot)\n\t\tthrow new ConversationError('SNAPSHOT', describeSnapshotRefusal('conversation', id, faults), {\n\t\t\tcontext: { id, faults },\n\t\t})\n\t}\n\n\t/**\n\t * Inserts or replaces under the snapshot's own `id` (no separate id param) — the row is\n\t * `{ id, snapshot }`.\n\t *\n\t * @param snapshot - The snapshot to store, keyed by its `id`\n\t * @returns A promise that resolves after the row is written\n\t * @throws {ConversationError} Thrown before any write when the snapshot fails the guard `get` reads (code `'SNAPSHOT'`), so no row is written that `get` refuses\n\t */\n\tasync set(snapshot: ConversationSnapshot): Promise<void> {\n\t\tif (!isConversationSnapshot(snapshot)) {\n\t\t\tconst unchecked: unknown = snapshot\n\t\t\tconst faults = conversationSnapshotContract.audit(unchecked)\n\t\t\tthrow new ConversationError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`a conversation store writes only what snapshot() returned\\n${describeFaults(faults, conversationSnapshotContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tawait this.#table.set({ id: snapshot.id, snapshot })\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op (no throw).\n\t *\n\t * @param id - The conversation id to drop\n\t * @returns A promise that resolves after the row is gone\n\t */\n\tasync delete(id: string): Promise<void> {\n\t\tawait this.#table.remove(id)\n\t}\n}\n","import type {\n\tConversationInterface,\n\tConversationManagerInterface,\n\tConversationManagerOptions,\n\tConversationOptions,\n\tConversationSnapshotRow,\n\tConversationStoreInterface,\n} from './types.js'\nimport type { DriverInterface, TableInterface } from '@orkestrel/database'\nimport { rawShape, stringShape } from '@orkestrel/contract'\nimport { createDatabase, createMemoryDriver } from '@orkestrel/database'\nimport { Conversation } from './Conversation.js'\nimport { ConversationManager } from './ConversationManager.js'\nimport { MemoryConversationStore } from './stores/MemoryConversationStore.js'\nimport { DatabaseConversationStore } from './stores/DatabaseConversationStore.js'\n\n/**\n * Creates a conversation — a {@link ConversationInterface} grouping messages above a flat\n * message store it owns directly, with compaction into summarized sections, on-demand\n * `rehydrate`, and substring `search`, driven by a provider-agnostic\n * {@link ConversationSummaryHandler} seam.\n *\n * @remarks\n * Append turns through the conversation's own `add` (the live tail it owns); `view()` is the model input\n * (each section as a summary message, then the live tail). `compact()` folds the older live\n * messages into a summarized {@link Section}, whole exchanges at a time — it requires a\n * `summarize` (without one `compact()` throws a `ConversationError`); `keep` retains a recent\n * tail (Default: `DEFAULT_CONVERSATION_KEEP`, which folds up to the newest user message).\n * `rehydrate(id)` / `search(query)` read the retained originals. Observable (`emitter` —\n * `compact` / `collapse` / `rehydrate`), wired\n * through the reserved `on` option; the emitter isolates a listener throw and routes it to\n * its `error` handler (the `error` option), so it can never corrupt a compaction.\n *\n * @param options - Optional `id` / `on` hooks + the `summarize` seam + `keep` + `sections` (see {@link ConversationOptions})\n * @returns A working {@link ConversationInterface}\n *\n * @example Conversations & compaction\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createConversation } from '@orkestrel/agent'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * // The summarizer seam — built from the provider by the runtime; core stays provider-agnostic.\n * // Append the instruction as the FINAL user turn: a chat model emits nothing when the prompt\n * // ends on an assistant turn, so a leading-system instruction is unreliable.\n * const conversation = createConversation({\n * \tsummarize: async (messages, signal) =>\n * \t\t(\n * \t\t\tawait provider.generate(\n * \t\t\t\t[\n * \t\t\t\t\t...messages,\n * \t\t\t\t\t{ id: 's', role: 'user', content: 'Summarize the conversation so far concisely.' },\n * \t\t\t\t],\n * \t\t\t\tsignal ?? AbortSignal.timeout(30_000), // the compact() call's signal, when it passed one\n * \t\t\t)\n * \t\t).content,\n * \tkeep: 2, // retain at least the 2 most recent messages verbatim on each compaction\n * })\n * conversation.add([\n * \t{ role: 'user', content: 'My name is Ada.' },\n * \t{ role: 'assistant', content: 'Nice to meet you, Ada.' },\n * \t{ role: 'user', content: 'Book a table for two at 19:00.' },\n * \t{ role: 'assistant', content: 'Booked for two at 19:00.' },\n * \t{ role: 'user', content: 'What did I say my name was?' },\n * ])\n *\n * const section = await conversation.compact() // folds the first exchange → a summarized section\n * conversation.view() // [<section summary message>, ...the retained recent exchanges] — the model input\n * conversation.search('ada') // case-insensitive across sections' originals + the live tail\n * section && conversation.rehydrate(section.id) // the section's full original messages (a pure read)\n * ```\n */\nexport function createConversation(options?: ConversationOptions): ConversationInterface {\n\treturn new Conversation(options)\n}\n\n/**\n * Creates a conversation registry — a {@link ConversationManagerInterface} holding\n * {@link ConversationInterface} instances keyed by their `id`, in insertion order, with an active pointer:\n * the id-keyed store over the conversation layer plus the `active` / `switch` seam the context\n * renders. `add` auto-activates the first conversation and flows the registry's default\n * `summarize` / `keep` into every conversation it creates.\n *\n * @remarks\n * Starts empty; `add(input?)` mints a {@link ConversationInterface} (its `id` from the input\n * or a random UUID), flowing the manager's default `summarize` / `keep` in unless the input\n * overrides them, and stores it (an already-present `id` throws, code `'ID'`) — and\n * auto-activates the first one (a registry with conversations always has one `active`); a later\n * `add` leaves `active` unchanged. `switch(id)` re-points `active` (an unknown `id` returns\n * `undefined`, leaving `active` unchanged — lenient, never throws); `conversation(id)` /\n * `conversations()` look up; `remove` (one or a batch) reports `true` only when every supplied id\n * was removed and clears `active` if it was a removed one; `clear` empties it and clears `active`.\n * Event-free\n * (each conversation owns its own observable `emitter`). A conversation created with neither a\n * manager default nor a per-`add` `summarize` cannot `compact` (it throws a `ConversationError`).\n *\n * @param options - Optional default `summarize` / `keep` (see {@link ConversationManagerOptions})\n * @returns An empty {@link ConversationManagerInterface}\n *\n * @example\n * ```ts\n * import { createConversationManager } from '@orkestrel/agent'\n *\n * const conversations = createConversationManager({ summarize: async (m) => `recap of ${m.length}` })\n * const chat = conversations.add() // auto-activates — conversations.active === chat\n * chat.add({ role: 'user', content: 'Hello' })\n * ```\n */\nexport function createConversationManager(\n\toptions?: ConversationManagerOptions,\n): ConversationManagerInterface {\n\treturn new ConversationManager(options)\n}\n\n/**\n * Creates the in-memory conversation store — a {@link ConversationStoreInterface} backed by a\n * process-lifetime `Map` of {@link import('./types.js').ConversationSnapshot} records keyed by conversation\n * id, the default backing for the durable {@link ConversationManagerInterface.open} /\n * {@link ConversationManagerInterface.save} seam. The exact twin of\n * {@link import('@orkestrel/workspace').createMemoryWorkspaceStore}.\n *\n * @remarks\n * A plain `Map` (the snapshot is already pure JSON, so no encoding is needed for the memory tier),\n * the structural twin of {@link import('@orkestrel/workspace').createMemoryWorkspaceStore}.\n * `get` / `set` / `delete` are async (the\n * same shape a durable backend fits); unlike a session store there is no idle-TTL / eviction — a\n * persisted conversation lives until an explicit `delete`. Its driver-pluggable twin is\n * {@link createDatabaseConversationStore} (the snapshot as one opaque JSON column over a `databases`\n * table) — for a durable store pass it a JSON / SQLite / IndexedDB driver, and it swaps in without\n * touching the manager or the conversation. Hydration stays a manager concern: read a snapshot back\n * and rebuild the live conversation through the `snapshot` option (re-supplying the live\n * `summarize` / `keep`).\n *\n * @returns A memory-backed {@link ConversationStoreInterface}\n *\n * @example\n * ```ts\n * import { createConversationManager, createMemoryConversationStore } from '@orkestrel/agent'\n *\n * const store = createMemoryConversationStore()\n * const manager = createConversationManager({ store })\n * const conversation = manager.add()\n * conversation.add({ role: 'user', content: 'hello' })\n * await manager.save(conversation.id) // persist the conversation\n * ```\n */\nexport function createMemoryConversationStore(): ConversationStoreInterface {\n\treturn new MemoryConversationStore()\n}\n\n/**\n * Creates a {@link DatabaseConversationStore} over any {@link DriverInterface} — the durable,\n * driver-pluggable backing for the conversation persistence\n * seam, holding each snapshot as one opaque JSON column and standing as the opt-in twin of\n * {@link createMemoryConversationStore}. The exact twin of\n * {@link import('@orkestrel/workspace').createDatabaseWorkspaceStore}.\n *\n * @remarks\n * Builds a one-table database (`conversations`, keyed by `id`) over the supplied driver, the snapshot\n * held as one opaque JSON column — the column map is `{ id; snapshot }` where `snapshot` is a\n * `rawShape` (a JSON blob), exactly as\n * {@link import('@orkestrel/workspace').createDatabaseWorkspaceStore} stores its snapshot. The\n * snapshot is already a complete, self-contained, pure-JSON payload, so storing it whole is lossless\n * and keeps the row type flat (the column reads back as `unknown`, narrowed on `get` by\n * {@link import('./validators.js').isConversationSnapshot}). Default driver:\n * {@link createMemoryDriver}, so the store also works in memory out of the box; pass a server\n * `createJSONDriver` / `createSQLiteDriver` (or a browser IndexedDB driver) for a persistent one —\n * the durability is the driver's job, the store engine is shared. It swaps in behind\n * {@link ConversationStoreInterface} without touching the manager or the conversation.\n *\n * @param driver - The storage backend the snapshots persist to. Default: {@link createMemoryDriver}\n * @returns A {@link ConversationStoreInterface} over the driver\n *\n * @example\n * ```ts\n * import { createConversationManager, createDatabaseConversationStore } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseConversationStore(createMemoryDriver()) // a durable driver swaps in here\n * const manager = createConversationManager({ store })\n * const conversation = manager.add()\n * conversation.add({ role: 'user', content: 'hello' })\n * await manager.save(conversation.id) // persist the conversation (one JSON column)\n * ```\n */\nexport function createDatabaseConversationStore(\n\tdriver: DriverInterface = createMemoryDriver(),\n): ConversationStoreInterface {\n\t// The snapshot is stored as one opaque JSON column (`rawShape`), so the row infers flat —\n\t// `{ id: string; snapshot: unknown }` = `ConversationSnapshotRow` — and the sections/messages\n\t// snapshot shape never forces a contract `Infer`.\n\tconst columns = { id: stringShape(), snapshot: rawShape({}) }\n\tconst database = createDatabase({ driver, tables: { conversations: columns } })\n\tconst table: TableInterface<ConversationSnapshotRow> = database.table('conversations')\n\treturn new DatabaseConversationStore(table)\n}\n","/**\n * Names the section header the `build()` method of {@link import('./AgentContext.js').AgentContext}\n * renders the active workspace's text files under — `'## Workspace'`, the line that opens the\n * workspace section of the user context message that follows the system message.\n *\n * @remarks\n * `build()` owns the workspace render (a `Workspace` / `WorkspaceManager` stays file-focused — no\n * `open` / `format` getters), so this header lives here as the contexts module's one\n * workspace-section framing constant rather than on a manager. Each workspace text file renders\n * beneath it as a fenced `` File: <path>\\n```<language>\\n<text>\\n``` `` block through\n * {@link import('./helpers.js').renderFencedFile}, and the section follows any memos index in the\n * context message, and is never part of the system message. The workspace's image files render no\n * section: their `base64` payload attaches to the last user message.\n */\nexport const WORKSPACE_SECTION_HEADER = '## Workspace'\n\n/**\n * Names the built-in section header a {@link import('./types.js').MemoManagerInterface} renders\n * its index under — `'## Memos'`, the `open` value when no `format.open` overrides it.\n *\n * @remarks\n * Each memo renders beneath it as one `- name: description` line through\n * {@link import('./helpers.js').renderIndexEntry}.\n */\nexport const MEMO_SECTION_HEADER = '## Memos'\n\n/**\n * Names the built-in section header a {@link import('./types.js').SkillManagerInterface} renders\n * its index under — `'## Skills'`, the `open` value when no `format.open` overrides it.\n *\n * @remarks\n * Each skill renders beneath it as one `- name: description` line through\n * {@link import('./helpers.js').renderIndexEntry}.\n */\nexport const SKILL_SECTION_HEADER = '## Skills'\n","import type { ContextErrorCode } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport { INTRINSICS, holds, isObject, isFunction } from '@orkestrel/contract'\n\n/**\n * Reports a context snapshot that `isMemoSnapshot` refuses, thrown by the construction of a\n * {@link import('./memos/MemoManager.js').MemoManager} before it seats any state or by a memo store,\n * or image files a build has no user turn to carry, carrying the machine-readable `code`\n * `'SNAPSHOT' | 'IMAGES'`.\n *\n * @remarks\n * A `snapshot` option crosses a foreign boundary (a client, a store, a file), so the manager copies\n * it through JSON and checks the copy: a value JSON cannot carry, a revision missing a field, a\n * non-finite `time`, a sparse `memos` array, a non-string `id`, or a non-array `memos` is a\n * programmer error and throws this with `'SNAPSHOT'`, its `context` holding the `faults`. A build\n * over a conversation that holds turns and no user turn, with image files in the active workspace,\n * throws `'IMAGES'` rather than dropping them. Narrow a caught value with {@link isContextError} and\n * branch on `error.code`.\n */\nexport class ContextError extends Error {\n\t/** Names the machine-readable condition — `'SNAPSHOT'`: a snapshot that is not a memo snapshot; `'IMAGES'`: image files with no user turn to carry them. */\n\treadonly code: ContextErrorCode\n\t/** Holds the values that locate the failure, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Creates the error with its code, message, and optional cause and context.\n\t *\n\t * @param code - The machine-readable {@link ContextErrorCode}\n\t * @param message - The human-readable description\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: ContextErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'ContextError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.ContextError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link ContextError} through its identity brand and declared members, so a\n * `catch` can branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link ContextError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tnew MemoManager({ snapshot })\n * } catch (error) {\n * \tif (isContextError(error) && error.code === 'SNAPSHOT') discard(snapshot)\n * }\n * ```\n */\nexport function isContextError(value: unknown): value is ContextError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.ContextError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (code !== 'SNAPSHOT' && code !== 'IMAGES') return false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { InstructionInput, InstructionInterface } from '../types.js'\n\n/**\n * Represents an immutable named directive — an {@link InstructionInterface} assembled once from\n * its input (`name` / `content` and an optional `priority`), the `id` minted at\n * construction. Default: `0` for `priority`.\n *\n * @remarks\n * A thin immutable value object (mirroring {@link import('@orkestrel/tool').Tool}): the\n * constructor mints a fresh `id` (`crypto.randomUUID()`), copies the input's `name` /\n * `content`, resolves `priority` to the input's value or `0`, and carries the input's\n * per-item `override` value when supplied; otherwise that property is `undefined`.\n * Never mutated after construction. An\n * {@link import('./InstructionManager.js').InstructionManager} keys it by `name` and\n * renders it (highest `priority` first) under its section header.\n *\n * @example\n * ```ts\n * const instruction = new Instruction({ name: 'tone', content: 'Be concise.', priority: 5 })\n * instruction.priority // 5\n * ```\n */\nexport class Instruction implements InstructionInterface {\n\treadonly id: string\n\treadonly name: string\n\treadonly content: string\n\treadonly priority: number\n\treadonly override?: string\n\n\tconstructor(input: InstructionInput) {\n\t\tthis.id = crypto.randomUUID()\n\t\tthis.name = input.name\n\t\tthis.content = input.content\n\t\tthis.priority = input.priority ?? 0\n\t\tif (input.override !== undefined) this.override = input.override\n\t}\n}\n","import type {\n\tContextSectionFormat,\n\tInstructionInput,\n\tInstructionInterface,\n\tInstructionManagerEventMap,\n\tInstructionManagerInterface,\n\tInstructionManagerOptions,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { removeEntries } from '../../helpers.js'\nimport { Instruction } from './Instruction.js'\n\n/**\n * Registers the immutable {@link Instruction} instances a richer context assembles a directives block\n * from — keyed by `name` so adding an instruction whose name is already stored overwrites it,\n * last write wins, and listed by descending `priority`, carrying the `open` / `render` / `close` build contract and an observable `emitter`.\n *\n * @remarks\n * - **Registry.** Instructions live in an insertion-ordered `Map` keyed by `name`;\n * `add` takes one {@link InstructionInput} or a batch, mints each instruction's\n * `id`, and adding an instruction whose name is already stored overwrites it (last write wins).\n * `count` is the map size, `instruction(name)` looks one up, and `instructions()` lists them sorted by\n * descending `priority` (a stable sort, so equal priorities keep insertion order).\n * - **Build contract (the whole format cascade).** `open` is the section header a context\n * renders the instructions under, `render(instruction)` renders one instruction, and\n * `close` is the line after them. Each resolves the cascade most-specific-first: `render`\n * returns the instruction's {@link InstructionInput.override}, else the\n * `InstructionManagerOptions.format` `render`, else its `content`; `open` returns the\n * options `open`, else the built-in header; `close` returns the options `close`, else\n * `undefined`. A context reads the three and frames the section from them (see\n * {@link import('../AgentContext.js').AgentContext}).\n * - **Removal.** `remove` drops one by name, or a batch — `true` only when every supplied\n * name was removed; `clear` empties the registry.\n * - **Observable.** The owned {@link emitter} ({@link InstructionManagerEventMap})\n * carries `add` (the created instruction) / `remove` (the name) / `clear` for\n * fire-and-forget observers. Every event is emitted directly, strictly after the map\n * mutation completes; the emitter isolates a listener throw and routes it to its `error`\n * handler (the `error` option), so a buggy observer can never corrupt a mutation.\n *\n * @example\n * ```ts\n * const manager = new InstructionManager()\n * manager.add([\n * \t{ name: 'tone', content: 'Be concise.', priority: 1 },\n * \t{ name: 'safety', content: 'Refuse unsafe requests.', priority: 10 },\n * ])\n * manager.instructions().map((one) => one.name) // ['safety', 'tone'] — highest priority first\n * ```\n */\nexport class InstructionManager implements InstructionManagerInterface {\n\treadonly #instructions = new Map<string, InstructionInterface>()\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<InstructionManagerEventMap>\n\treadonly #format: ContextSectionFormat<InstructionInterface> | undefined\n\n\tconstructor(options?: InstructionManagerOptions) {\n\t\tthis.#emitter = new Emitter<InstructionManagerEventMap>(options)\n\t\tthis.#format = options?.format\n\t}\n\n\tget emitter(): EmitterInterface<InstructionManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn this.#instructions.size\n\t}\n\n\tget open(): string {\n\t\treturn this.#format?.open ?? '## Instructions'\n\t}\n\n\tget close(): string | undefined {\n\t\treturn this.#format?.close\n\t}\n\n\tadd(input: InstructionInput): InstructionInterface\n\tadd(inputs: readonly InstructionInput[]): readonly InstructionInterface[]\n\tadd(\n\t\tinput: InstructionInput | readonly InstructionInput[],\n\t): InstructionInterface | readonly InstructionInterface[] {\n\t\tif (isArray(input)) return input.map((one) => this.#create(one))\n\t\treturn this.#create(input)\n\t}\n\n\tinstruction(name: string): InstructionInterface | undefined {\n\t\treturn this.#instructions.get(name)\n\t}\n\n\tinstructions(): readonly InstructionInterface[] {\n\t\t// A stable descending-priority sort — Array.prototype.sort is stable, so equal\n\t\t// priorities keep their insertion order (the map's iteration order).\n\t\treturn [...this.#instructions.values()].sort((a, b) => b.priority - a.priority)\n\t}\n\n\trender(instruction: InstructionInterface): string {\n\t\treturn instruction.override ?? this.#format?.render?.(instruction) ?? instruction.content\n\t}\n\n\tremove(name: string): boolean\n\tremove(names: readonly string[]): boolean\n\tremove(names: string | readonly string[]): boolean {\n\t\tif (isArray(names)) {\n\t\t\t// Each present name still emits.\n\t\t\treturn removeEntries(names, (name) => this.#delete(name))\n\t\t}\n\t\treturn this.#delete(names)\n\t}\n\n\tclear(): void {\n\t\tthis.#instructions.clear()\n\t\t// Observe the cleared registry — after the map emptied, so a swallowed listener\n\t\t// throw can never alter the clear (no payload — `clear` is a pure signal).\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\t// Mint an immutable instruction, store it by name (overwriting a same-name one), and\n\t// emit `add` after the map mutation — so a swallowed listener throw can't perturb it.\n\t#create(input: InstructionInput): InstructionInterface {\n\t\tconst instruction = new Instruction(input)\n\t\tthis.#instructions.set(instruction.name, instruction)\n\t\tthis.#emitter.emit('add', instruction)\n\t\treturn instruction\n\t}\n\n\t// Delete one instruction, emitting `remove` only when one was actually removed (a\n\t// delete of an absent name returns `false` and emits nothing) — after the deletion.\n\t#delete(name: string): boolean {\n\t\tconst removed = this.#instructions.delete(name)\n\t\tif (removed) this.#emitter.emit('remove', name)\n\t\treturn removed\n\t}\n}\n","import type { Memo } from './types.js'\nimport type { Message } from '../types.js'\nimport type { FileInterface } from '@orkestrel/workspace'\nimport { isBinary } from '@orkestrel/workspace'\nimport { ContextError } from './errors.js'\n\n/**\n * Renders a path-addressed text body as a fenced reference block — a `File: <path>` label line\n * over a language-tagged fence, the framing the\n * active-workspace text-file render of {@link import('./AgentContext.js').AgentContext} emits.\n *\n * @remarks\n * Produces `` File: <path>\\n```<language>\\n<content>\\n``` `` — the `File:` label line, then a\n * fenced code block tagged with `language`, the `content` verbatim inside. The fence is one\n * backtick longer than the longest backtick run in `content`, and never shorter than three (the\n * CommonMark rule), so file text holding a triple backtick cannot close the block early and\n * land outside it; content with no run of three or more keeps the three-backtick fence. Pure\n * string assembly, total — never throws. The one fenced-file format string for the whole\n * module — `AgentContext.build()` frames an active workspace's text files with it (each carries\n * its own `language` on its {@link import('@orkestrel/workspace').FileContent} text arm).\n *\n * @param path - The file path shown on the `File:` label line\n * @param language - The fenced-code language tag (for example `'typescript'`)\n * @param content - The file body rendered verbatim inside the fence\n * @returns The fenced reference block\n *\n * @example\n * ```ts\n * import { renderFencedFile } from '@orkestrel/agent'\n *\n * renderFencedFile('src/main.ts', 'typescript', 'const x = 1')\n * // 'File: src/main.ts\\n```typescript\\nconst x = 1\\n```'\n * ```\n */\nexport function renderFencedFile(path: string, language: string, content: string): string {\n\tconst runs = content.match(/`+/g) ?? []\n\tconst longest = runs.reduce((most, run) => Math.max(most, run.length), 0)\n\tconst fence = '`'.repeat(Math.max(3, longest + 1))\n\treturn `File: ${path}\\n${fence}${language}\\n${content}\\n${fence}`\n}\n\n/**\n * Renders one context section — the resolved `open`, each item's rendering, and the resolved\n * `close` when one exists, blank-line joined; `undefined` when the section has no items.\n *\n * @remarks\n * Pure and total. A section with no items renders nothing (`undefined`), so an empty or fully\n * scoped-out manager stays silent — its `open` / `close` appear only when the section has items.\n * An empty or absent slot is dropped before the join, so an `open: ''`, a `close: ''`, an unset\n * `close` (there is no built-in close), and an item rendered to `''` each add no blank line; a\n * section whose every slot is empty renders nothing (`undefined`).\n *\n * @typeParam T - The section item being rendered\n * @param open - The section's resolved leading text\n * @param members - The already scope-filtered items\n * @param render - Renders one item to its prompt text\n * @param close - The section's resolved trailing text, or `undefined` for none\n * @returns The rendered section, or `undefined` when there are no items or every slot is empty\n *\n * @example\n * ```ts\n * renderSection('## Instructions', [{ content: 'Be terse.' }], (one) => one.content, undefined)\n * // '## Instructions\\n\\nBe terse.'\n * renderSection('<rules>', [], (one) => one.content, '</rules>') // undefined (no items)\n * ```\n */\nexport function renderSection<T>(\n\topen: string,\n\tmembers: readonly T[],\n\trender: (member: T) => string,\n\tclose: string | undefined,\n): string | undefined {\n\tif (members.length === 0) return undefined\n\tconst lines = [open, ...members.map(render), close].flatMap((line) =>\n\t\tline === undefined || line === '' ? [] : [line],\n\t)\n\treturn lines.length === 0 ? undefined : lines.join('\\n\\n')\n}\n\n/**\n * Copies a message with image data merged onto its `images` — the message's own images first,\n * then the attached data, keeping every other field and never mutating the original.\n *\n * @remarks\n * Pure and total: the original message is never mutated. Every other field the source message\n * carries (`calls`, `call`, `thinking`) is kept as given, and an absent one stays absent,\n * mirroring the store's present-when-given convention.\n *\n * @param message - The message to copy (left unchanged)\n * @param payloads - The base64 image data to attach\n * @returns A new message carrying the merged `images`\n *\n * @example\n * ```ts\n * // IMAGE_BASE64 stands for the base64 text of one image.\n * attachImages({ id: '1', role: 'user', content: 'Describe' }, ['IMAGE_BASE64'])\n * // { id: '1', role: 'user', content: 'Describe', images: ['IMAGE_BASE64'] }\n * ```\n */\nexport function attachImages(message: Message, payloads: readonly string[]): Message {\n\treturn { ...message, images: [...(message.images ?? []), ...payloads] }\n}\n\n/**\n * Attaches image data to a conversation's last user message — the turn a vision provider reads\n * images off — as a new array with that one message replaced by its carrying copy, and unchanged\n * when there is no data or no user turn.\n *\n * @remarks\n * Pure and total: the conversation and its messages are never mutated, and the returned array\n * replaces exactly the one target message with the copy {@link attachImages} builds. Empty\n * data returns the conversation unchanged. Data with no user message to carry it refuses, an empty\n * conversation included, because leaving the images out would run without them. A hole in a sparse\n * conversation is not a user message.\n *\n * @param conversation - The messages to attach into (left unchanged)\n * @param payloads - The base64 image data to attach\n * @returns The conversation with its last user message replaced by the carrying copy\n * @throws ContextError Thrown when there is data and no user message to carry it (code `'IMAGES'`)\n *\n * @example\n * ```ts\n * attachUserImages([{ id: '1', role: 'user', content: 'Describe' }], ['IMAGE_BASE64'])\n * // [{ id: '1', role: 'user', content: 'Describe', images: ['IMAGE_BASE64'] }]\n * ```\n */\nexport function attachUserImages(\n\tconversation: readonly Message[],\n\tpayloads: readonly string[],\n): readonly Message[] {\n\tif (payloads.length === 0) return conversation\n\tconst target = conversation.findLastIndex(\n\t\t(message: Message | undefined) => message?.role === 'user',\n\t)\n\tif (target === -1) {\n\t\tthrow new ContextError(\n\t\t\t'IMAGES',\n\t\t\t`the active workspace holds ${payloads.length} image file(s) and the conversation holds no user turn to carry them; add a user message or scope the files out`,\n\t\t)\n\t}\n\treturn conversation.map((message, index) =>\n\t\tindex === target ? attachImages(message, payloads) : message,\n\t)\n}\n\n/**\n * Collects the `base64` payload of the image files in a workspace file list — the data an agent\n * context attaches to the last user message.\n *\n * @remarks\n * Pure and total. `isBinary` narrows the tagless content to its binary arm (a total guard,\n * never an assertion), then the MIME prefix gates it to an image, so a text file and a non-image\n * binary (a PDF) are both skipped. Order follows the file list.\n *\n * @param files - The (already scope-filtered) workspace files\n * @returns The `base64` payload of each image file, in file order\n *\n * @example\n * ```ts\n * collectImageData([createFile({ path: 'a.png', content: { base64: 'IMAGE_BASE64', mime: 'image/png' } })])\n * // ['IMAGE_BASE64']\n * ```\n */\nexport function collectImageData(files: readonly FileInterface[]): readonly string[] {\n\t// The spread visits a hole in `files` as an undefined file, as iteration does; `flatMap` skips it.\n\treturn [...files].flatMap((file) =>\n\t\tisBinary(file.content) && file.content.mime.startsWith('image/') ? [file.content.base64] : [],\n\t)\n}\n\n/**\n * Intersects two scope category lists under the \"`undefined` is the universal set\" rule — a\n * fresh copy that can only tighten, and the primitive a scope narrows through.\n *\n * @remarks\n * Pure and total, and it can only tighten: `undefined` ∩ `undefined` is `undefined` (still no\n * constraint); `undefined` ∩ a list is a copy of that list (the `undefined` side imposes\n * nothing); a list ∩ a list keeps the child keys the parent also allows, so a parent-excluded\n * key can never be re-admitted. Every returned list is a fresh copy, so a later mutation of\n * either input cannot leak into the result.\n *\n * @param parent - The parent's allow-list (`undefined` ⇒ no constraint)\n * @param child - The narrowing allow-list (`undefined` ⇒ no constraint)\n * @returns The intersected allow-list, or `undefined` when neither side constrains\n *\n * @example\n * ```ts\n * intersectKeys(['read', 'write'], ['write', 'admin']) // ['write']\n * intersectKeys(undefined, ['read']) // ['read']\n * intersectKeys(undefined, undefined) // undefined\n * ```\n */\nexport function intersectKeys(\n\tparent: readonly string[] | undefined,\n\tchild: readonly string[] | undefined,\n): readonly string[] | undefined {\n\tif (parent === undefined) return child === undefined ? undefined : [...child]\n\tif (child === undefined) return [...parent]\n\tconst allowed = new Set(parent)\n\treturn child.filter((key) => allowed.has(key))\n}\n\n/**\n * Renders one index line for a memo or a skill — `- name: description`, the built-in `render` of\n * the memo and skill managers.\n *\n * @remarks\n * Pure and total. A run of line-break code points in the name or the description renders as one\n * space, so the entry is one line.\n *\n * @param entry - The memo or skill whose `name` and `description` the line shows\n * @returns The index line\n *\n * @example\n * ```ts\n * import { renderIndexEntry } from '@orkestrel/agent'\n *\n * renderIndexEntry({ name: 'tone', description: 'Prefers short replies.' })\n * // '- tone: Prefers short replies.'\n * ```\n */\nexport function renderIndexEntry(entry: Pick<Memo, 'name' | 'description'>): string {\n\treturn `- ${entry.name}: ${entry.description}`.replace(/[\\n\\v\\f\\r\\u0085\\u2028\\u2029]+/g, ' ')\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\tstringShape,\n} from '@orkestrel/contract'\n\n/** Describes a stored memo revision exactly. */\nexport const memoShape = objectShape({\n\tid: stringShape(),\n\tname: stringShape(),\n\tdescription: stringShape(),\n\tcontent: stringShape(),\n\ttime: numberShape(),\n\torigin: optionalShape(\n\t\tobjectShape({\n\t\t\tconversation: stringShape(),\n\t\t\tmessage: optionalShape(stringShape()),\n\t\t}),\n\t),\n}) satisfies ContractShape\n\n/**\n * Describes a stored memo set exactly: an unknown key refuses.\n *\n * @remarks\n * `isMemoSnapshot` is the door a hydrate and a store read, and this shape fills a refusal's faults. A\n * lockstep test holds that `is` implies the guard, and on the snapshot fixtures the converse.\n */\nexport const memoSnapshotShape = objectShape({\n\tid: stringShape(),\n\tmemos: arrayShape(memoShape),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { memoShape, memoSnapshotShape } from './shapers.js'\n\n/** Reads the faults of a refused memo revision. */\nexport const memoContract = createContract(memoShape)\n\n/** Reads the faults of a refused memo snapshot. */\nexport const memoSnapshotContract = createContract(memoSnapshotShape)\n","import type { Memo, MemoOrigin, MemoSnapshot } from './types.js'\nimport { arrayOf, holds, isFiniteNumber, isRecord, isString } from '@orkestrel/contract'\n\n/**\n * Checks whether a value is a memo's origin: a record with a string `conversation` and an optional\n * string `message`.\n *\n * @remarks\n * The guard is exact: an unknown member and an own `undefined` `message` return false. Malformed and\n * unreadable inputs return false.\n *\n * @param value - The value to test\n * @returns True if `value` has the shape of a {@link MemoOrigin}; false otherwise\n * @example\n * ```ts\n * isMemoOrigin({ conversation: 'c1', message: 'm1' }) // true\n * isMemoOrigin({ conversation: 'c1', extra: true }) // false\n * ```\n */\nexport function isMemoOrigin(value: unknown): value is MemoOrigin {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { conversation, message } = value\n\t\treturn (\n\t\t\tisString(conversation) &&\n\t\t\t(message === undefined || isString(message)) &&\n\t\t\tObject.keys(value).length === (message === undefined ? 1 : 2)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a stored memo revision: a record with a string `id`, `name`,\n * `description`, and `content`, a finite `time`, and an optional `origin`.\n *\n * @remarks\n * An `origin`, when present, is a record with a string `conversation` and an optional string\n * `message`. The guard is exact: an unknown member and an own `undefined` optional return false. The\n * `time` must be finite, because a stored revision is owned through JSON, which turns `NaN` and the\n * infinities into `null`. Malformed and unreadable inputs, a throwing getter or a revoked proxy\n * included, return false. Every revision of a hydrate reads this guard, so it counts keys by hand\n * rather than through a built one, and `memoSnapshotContract` holds it in lockstep.\n *\n * @param value - The value to test\n * @returns True if `value` has the shape of a {@link Memo}; false otherwise\n *\n * @example\n * ```ts\n * import { isMemo } from '@orkestrel/agent'\n *\n * isMemo({ id: 'm1', name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.', time: 0 }) // true\n * isMemo({ id: 'm1', name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.', time: Number.NaN }) // false\n * ```\n */\nexport function isMemo(value: unknown): value is Memo {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { id, name, description, content, time, origin } = value\n\t\treturn (\n\t\t\tisString(id) &&\n\t\t\tisString(name) &&\n\t\t\tisString(description) &&\n\t\t\tisString(content) &&\n\t\t\tisFiniteNumber(time) &&\n\t\t\t(origin === undefined || isMemoOrigin(origin)) &&\n\t\t\tObject.keys(value).length === (origin === undefined ? 5 : 6)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a stored memo set: a record with a string `id` and a dense `memos`\n * array of {@link isMemo} values.\n *\n * @remarks\n * The guard is exact: an unknown member returns false, as a sparse `memos` array, a malformed\n * revision, and unreadable inputs, a throwing getter or a revoked proxy included, do.\n *\n * @param value - The value to test (an opaque storage read)\n * @returns True if `value` has the shape of a {@link MemoSnapshot}; false otherwise\n *\n * @example\n * ```ts\n * import { isMemoSnapshot } from '@orkestrel/agent'\n *\n * isMemoSnapshot({ id: 'ada', memos: [] }) // true\n * isMemoSnapshot({ id: 'ada', memos: [{ name: 'tone' }] }) // false\n * ```\n */\nexport function isMemoSnapshot(value: unknown): value is MemoSnapshot {\n\treturn holds(() => {\n\t\tif (!isRecord(value) || Object.keys(value).length !== 2) return false\n\t\tconst { id, memos } = value\n\t\treturn isString(id) && arrayOf(isMemo)(memos)\n\t})\n}\n","import type {\n\tContextSectionFormat,\n\tMemo,\n\tMemoInput,\n\tMemoManagerEventMap,\n\tMemoManagerInterface,\n\tMemoManagerOptions,\n\tMemoSnapshot,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { describeFaults, isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { copyJSON } from '../../cloners.js'\nimport { removeEntries } from '../../helpers.js'\nimport { MEMO_SECTION_HEADER } from '../constants.js'\nimport { ContextError } from '../errors.js'\nimport { renderIndexEntry } from '../helpers.js'\nimport { memoSnapshotContract } from '../contracts.js'\nimport { isMemoSnapshot } from '../validators.js'\n\n/**\n * Registers the revisions of a memo set — durable facts keyed by `name`, where a same-name `add`\n * with changed text appends a revision and the earlier ones stay readable — carrying the `open` /\n * `render` / `close` build contract and an observable `emitter`.\n *\n * @remarks\n * - **Revisions.** Every revision lives in one append-ordered list, so the current revisions,\n * each name's history, and the snapshot order all derive from it. `add` appends a revision with\n * a minted `id` and `time` unless the name's current revision holds the same `description` and\n * `content`; then it returns that revision and emits nothing. `memo(name)` is the name's last\n * revision, `memos()` lists each name's last revision by its first revision, and `count` is the\n * number of names.\n * - **Removal.** `remove` drops every revision of a name, so a re-added name starts a fresh\n * history at the end of `memos()`; `clear` drops every revision.\n * - **Identity.** `id` is the `snapshot.id`, else the `id` option, else a minted UUID. A\n * `snapshot` option is copied through JSON and checked with `isMemoSnapshot`, then seeds the\n * revisions silently; a malformed one throws a `ContextError` with code `'SNAPSHOT'`. `snapshot()`\n * returns owned copies of the revisions, oldest first.\n * - **Build contract.** `open` returns the options `open`, else `'## Memos'`; `render(memo)`\n * returns the options `render`, else `- name: description`; `close` returns the options\n * `close`, else `undefined`.\n * - **Observable.** The owned {@link emitter} ({@link MemoManagerEventMap}) carries `add` (the\n * appended revision) / `remove` (the name) / `clear`, each emitted after the mutation completes;\n * the emitter isolates a listener throw and routes it to its `error` handler.\n *\n * @example\n * ```ts\n * const memos = new MemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * memos.add({ name: 'tone', description: 'Prefers bullet lists.', content: 'Answer in bullets.' })\n * memos.history('tone').length // 2\n * memos.memo('tone')?.description // 'Prefers bullet lists.'\n * ```\n */\nexport class MemoManager implements MemoManagerInterface {\n\treadonly #id: string\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<MemoManagerEventMap>\n\treadonly #format: ContextSectionFormat<Memo> | undefined\n\t// Every revision, oldest first. Replaced on every change rather than mutated in place.\n\t#revisions: readonly Memo[]\n\n\t/**\n\t * Creates the memo set, owning a supplied `snapshot` through a JSON copy.\n\t *\n\t * @param options - The optional {@link MemoManagerOptions}\n\t * @throws {ContextError} Thrown when the `snapshot` option cannot be copied through JSON or its copy is not a memo snapshot (code `'SNAPSHOT'`); nothing is seated and nothing is emitted\n\t */\n\tconstructor(options?: MemoManagerOptions) {\n\t\t// A snapshot is a foreign value: own a JSON copy first so the caller's objects never alias the\n\t\t// state, then refuse a malformed one, or one JSON cannot copy, before anything is seated.\n\t\tconst supplied = options?.snapshot\n\t\tconst owned = supplied === undefined ? undefined : copyJSON(supplied)\n\t\tconst snapshot = isMemoSnapshot(owned) ? owned : undefined\n\t\tif (supplied !== undefined && snapshot === undefined) {\n\t\t\tconst faults = memoSnapshotContract.audit(owned)\n\t\t\tthrow new ContextError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`context error: memo snapshot is malformed; pass what snapshot() returned\\n${describeFaults(faults, memoSnapshotContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\t// Restoring is silent: nothing was edited, so no event fires.\n\t\tthis.#id = snapshot?.id ?? options?.id ?? crypto.randomUUID()\n\t\tthis.#emitter = new Emitter<MemoManagerEventMap>(options)\n\t\tthis.#format = options?.format\n\t\tthis.#revisions = snapshot === undefined ? [] : snapshot.memos\n\t}\n\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\tget emitter(): EmitterInterface<MemoManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn new Set(this.#revisions.map((memo) => memo.name)).size\n\t}\n\n\tget open(): string {\n\t\treturn this.#format?.open ?? MEMO_SECTION_HEADER\n\t}\n\n\tget close(): string | undefined {\n\t\treturn this.#format?.close\n\t}\n\n\tadd(input: MemoInput): Memo\n\tadd(inputs: readonly MemoInput[]): readonly Memo[]\n\tadd(input: MemoInput | readonly MemoInput[]): Memo | readonly Memo[] {\n\t\tif (isArray(input)) return input.map((one) => this.#append(one))\n\t\treturn this.#append(input)\n\t}\n\n\tmemo(name: string): Memo | undefined {\n\t\treturn this.#revisions.findLast((memo) => memo.name === name)\n\t}\n\n\tmemos(): readonly Memo[] {\n\t\t// A Map keeps a key's first insertion position while each later set replaces its value, so\n\t\t// every name lands at its first revision holding its last one.\n\t\treturn [...new Map(this.#revisions.map((memo) => [memo.name, memo])).values()]\n\t}\n\n\thistory(name: string): readonly Memo[] {\n\t\treturn this.#revisions.filter((memo) => memo.name === name)\n\t}\n\n\trender(memo: Memo): string {\n\t\treturn this.#format?.render?.(memo) ?? renderIndexEntry(memo)\n\t}\n\n\tremove(name: string): boolean\n\tremove(names: readonly string[]): boolean\n\tremove(names: string | readonly string[]): boolean {\n\t\tif (isArray(names)) {\n\t\t\t// Each present name still emits.\n\t\t\treturn removeEntries(names, (name) => this.#delete(name))\n\t\t}\n\t\treturn this.#delete(names)\n\t}\n\n\tclear(): void {\n\t\tthis.#revisions = []\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\tsnapshot(): MemoSnapshot {\n\t\t// Owned copies: a returned snapshot never aliases the manager's revisions or their origins.\n\t\treturn {\n\t\t\tid: this.#id,\n\t\t\tmemos: this.#revisions.map((memo) => ({\n\t\t\t\t...memo,\n\t\t\t\t...(memo.origin === undefined ? {} : { origin: { ...memo.origin } }),\n\t\t\t})),\n\t\t}\n\t}\n\n\t// Append a revision unless the name's current one already holds the same text, and emit `add`\n\t// after the list changed, so a swallowed listener throw can't perturb it.\n\t#append(input: MemoInput): Memo {\n\t\tconst current = this.memo(input.name)\n\t\tif (current?.description === input.description && current.content === input.content) {\n\t\t\treturn current\n\t\t}\n\t\tconst memo: Memo = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\tname: input.name,\n\t\t\tdescription: input.description,\n\t\t\tcontent: input.content,\n\t\t\ttime: Date.now(),\n\t\t\t...(input.origin === undefined ? {} : { origin: { ...input.origin } }),\n\t\t}\n\t\tthis.#revisions = [...this.#revisions, memo]\n\t\tthis.#emitter.emit('add', memo)\n\t\treturn memo\n\t}\n\n\t// Drop every revision of one name, emitting `remove` only when one was present — after the\n\t// list changed.\n\t#delete(name: string): boolean {\n\t\tconst kept = this.#revisions.filter((memo) => memo.name !== name)\n\t\tif (kept.length === this.#revisions.length) return false\n\t\tthis.#revisions = kept\n\t\tthis.#emitter.emit('remove', name)\n\t\treturn true\n\t}\n}\n","import type { ScopeFilter, ScopeInput, ScopeInterface, SelectionHandler } from '../types.js'\nimport { intersectKeys } from '../helpers.js'\n\n/**\n * Represents a named, immutable filter over a richer context's items — an optional allow-list per\n * category (`instructions` / `skills` / `memos` / `tools` / `files`), each keyed by that category's\n * identity (an instruction's, a skill's, a memo's, or a tool's `name`, a workspace file's `path`)\n * and read as an allow-list:\n * `undefined` lets everything pass, `[]` lets nothing pass, and a non-empty list passes the listed\n * keys alone. `narrow` composes a tighter child by set intersection.\n *\n * @remarks\n * - **A category list is three-way.** `undefined` ⇒ no constraint on that category (all\n * pass); `[]` ⇒ none pass; a non-empty list ⇒ only the listed keys pass. The build\n * step / loop apply this through `filterAllowList`.\n * - **Immutable.** The `id` is minted at construction; every supplied list is copied in\n * (so a later mutation of the caller's array can't leak in), and the lists are\n * `readonly`. A `Scope` is never mutated after construction — `narrow` returns a new\n * one rather than altering this one.\n * - **`narrow` is set-intersection (immutable composition).** A child scope's visible set\n * per category is the intersection of this scope's list and the config's list — but\n * `undefined` means \"no constraint\", so it acts as the universal set: intersecting\n * `undefined` with a list yields the list, and `undefined` with `undefined` stays\n * `undefined`. Narrowing can only tighten, never widen — a key excluded by a parent\n * can never be re-admitted by a child. The child keeps this scope's `name` and `select` handler.\n *\n * @example\n * ```ts\n * const scope = new Scope({ name: 'reader', tools: ['search', 'read'] })\n * // narrow intersects: tools ∩ ['read', 'write'] = ['read'] (write was never in the parent).\n * const tighter = scope.narrow({ tools: ['read', 'write'] })\n * tighter.tools // ['read']\n * // instructions had no parent constraint (undefined) → the child's list passes through.\n * tighter.narrow({ instructions: ['safety'] }).instructions // ['safety']\n * ```\n */\nexport class Scope implements ScopeInterface {\n\treadonly id: string = crypto.randomUUID()\n\treadonly name: string\n\treadonly instructions?: readonly string[]\n\treadonly skills?: readonly string[]\n\treadonly memos?: readonly string[]\n\treadonly tools?: readonly string[]\n\treadonly files?: readonly string[]\n\treadonly select?: SelectionHandler\n\n\tconstructor(input: ScopeInput) {\n\t\tthis.name = input.name\n\t\tif (input.select !== undefined) this.select = input.select\n\t\t// Copy each supplied list in (a later mutation of the caller's array can't leak in);\n\t\t// an omitted list stays `undefined` — the absence of a constraint.\n\t\tif (input.instructions !== undefined) this.instructions = [...input.instructions]\n\t\tif (input.skills !== undefined) this.skills = [...input.skills]\n\t\tif (input.memos !== undefined) this.memos = [...input.memos]\n\t\tif (input.tools !== undefined) this.tools = [...input.tools]\n\t\tif (input.files !== undefined) this.files = [...input.files]\n\t}\n\n\tnarrow(config: ScopeFilter): ScopeInterface {\n\t\tconst instructions = intersectKeys(this.instructions, config.instructions)\n\t\tconst skills = intersectKeys(this.skills, config.skills)\n\t\tconst memos = intersectKeys(this.memos, config.memos)\n\t\tconst tools = intersectKeys(this.tools, config.tools)\n\t\tconst files = intersectKeys(this.files, config.files)\n\t\treturn new Scope({\n\t\t\tname: this.name,\n\t\t\t...(this.select === undefined ? {} : { select: this.select }),\n\t\t\t...(instructions === undefined ? {} : { instructions }),\n\t\t\t...(skills === undefined ? {} : { skills }),\n\t\t\t...(memos === undefined ? {} : { memos }),\n\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t...(files === undefined ? {} : { files }),\n\t\t})\n\t}\n}\n","import type {\n\tScopeInput,\n\tScopeInterface,\n\tScopeManagerEventMap,\n\tScopeManagerInterface,\n\tScopeManagerOptions,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { removeEntries } from '../../helpers.js'\nimport { Scope } from './Scope.js'\n\n/**\n * Registers the named filters a richer context reuses — immutable {@link Scope} instances keyed by their\n * minted `id`, in insertion order, where `add` always mints and stores rather than overwriting,\n * and an observable `emitter` reports each change.\n *\n * @remarks\n * - **Registry.** Scopes live in an insertion-ordered `Map` keyed by their minted `id`;\n * `add` mints a {@link Scope} from a {@link ScopeInput} (an `id` plus the per-category\n * allow-lists), stores it, and returns it. `count` is the map size, `scope(id)` looks\n * one up, and `scopes()` lists them in insertion order. (Unlike the name-keyed\n * instruction registry, a scope's key is its minted `id`, so two scopes may share a\n * `name`; `add` therefore never overwrites.)\n * - **Removal.** `remove` drops one by id, or a batch — `true` only when every supplied id\n * was removed; `clear` empties the registry.\n * - **Observable.** The owned {@link emitter} ({@link ScopeManagerEventMap}) carries\n * `add` (the added scope) / `remove` (the id) / `clear`. Every event is emitted\n * directly, strictly after the map mutation completes; the emitter isolates a listener\n * throw and routes it to its `error` handler (the `error` option), so a buggy observer can\n * never corrupt a mutation.\n *\n * @example\n * ```ts\n * const manager = new ScopeManager()\n * const reader = manager.add({ name: 'reader', tools: ['search', 'read'] })\n * manager.scope(reader.id) // the same scope\n * manager.count // 1\n * ```\n */\nexport class ScopeManager implements ScopeManagerInterface {\n\treadonly #scopes = new Map<string, ScopeInterface>()\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<ScopeManagerEventMap>\n\n\tconstructor(options?: ScopeManagerOptions) {\n\t\tthis.#emitter = new Emitter<ScopeManagerEventMap>(options)\n\t}\n\n\tget emitter(): EmitterInterface<ScopeManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn this.#scopes.size\n\t}\n\n\tadd(input: ScopeInput): ScopeInterface {\n\t\tconst scope = new Scope(input)\n\t\tthis.#scopes.set(scope.id, scope)\n\t\t// Observe the added scope — after the map set, so a swallowed listener throw can't\n\t\t// perturb the store the caller is about to use.\n\t\tthis.#emitter.emit('add', scope)\n\t\treturn scope\n\t}\n\n\tscope(id: string): ScopeInterface | undefined {\n\t\treturn this.#scopes.get(id)\n\t}\n\n\tscopes(): readonly ScopeInterface[] {\n\t\treturn [...this.#scopes.values()]\n\t}\n\n\tremove(id: string): boolean\n\tremove(ids: readonly string[]): boolean\n\tremove(ids: string | readonly string[]): boolean {\n\t\tif (isArray(ids)) {\n\t\t\t// Each present id still emits.\n\t\t\treturn removeEntries(ids, (id) => this.#delete(id))\n\t\t}\n\t\treturn this.#delete(ids)\n\t}\n\n\tclear(): void {\n\t\tthis.#scopes.clear()\n\t\t// Observe the cleared registry — after the map emptied, so a swallowed listener\n\t\t// throw can never alter the clear (no payload — `clear` is a pure signal).\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\t// Delete one scope, emitting `remove` only when one was actually removed (a delete of\n\t// an absent id returns `false` and emits nothing) — after the deletion.\n\t#delete(id: string): boolean {\n\t\tconst removed = this.#scopes.delete(id)\n\t\tif (removed) this.#emitter.emit('remove', id)\n\t\treturn removed\n\t}\n}\n","import type {\n\tContextSectionFormat,\n\tSkill,\n\tSkillInput,\n\tSkillManagerEventMap,\n\tSkillManagerInterface,\n\tSkillManagerOptions,\n} from '../types.js'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport { isArray } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { createFile } from '@orkestrel/workspace'\nimport { removeEntries } from '../../helpers.js'\nimport { SKILL_SECTION_HEADER } from '../constants.js'\nimport { renderIndexEntry } from '../helpers.js'\n\n/**\n * Registers the skills a host supplies — instruction bundles keyed by `name`, listed in insertion\n * order, where adding a skill whose name is already stored overwrites it, last write wins —\n * carrying the `open` / `render` / `close` build contract and an observable `emitter`.\n *\n * @remarks\n * - **Registry.** Skills live in an insertion-ordered `Map` keyed by `name`; `add` takes one\n * {@link SkillInput} or a batch, mints each skill's `id`, and mints each file with `createFile`\n * from `@orkestrel/workspace`. A same-name `add` replaces the stored skill with a fresh `id` and\n * keeps the name's position. `count` is the map size, `skill(name)` looks one up, and `skills()`\n * lists them in insertion order.\n * - **Removal.** `remove` drops one by name, or a batch — `true` only when every supplied name was\n * removed; `clear` empties the registry.\n * - **Build contract.** `open` returns the options `open`, else `'## Skills'`; `render(skill)`\n * returns the options `render`, else `- name: description`; `close` returns the options\n * `close`, else `undefined`.\n * - **Observable.** The owned {@link emitter} ({@link SkillManagerEventMap}) carries `add` (the\n * created skill) / `remove` (the name) / `clear`, each emitted after the mutation completes; the\n * emitter isolates a listener throw and routes it to its `error` handler.\n *\n * @example\n * ```ts\n * const skills = new SkillManager()\n * const review = skills.add({\n * \tname: 'review',\n * \tdescription: 'Reviews a change for defects.',\n * \tcontent: 'Read the diff, then list each defect with its line.',\n * \tfiles: [{ path: 'checklist.md', content: { text: '- Tests pass', language: 'markdown' } }],\n * })\n * review.files[0]?.path // 'checklist.md'\n * skills.render(review) // '- review: Reviews a change for defects.'\n * ```\n */\nexport class SkillManager implements SkillManagerInterface {\n\treadonly #skills = new Map<string, Skill>()\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a mutation.\n\treadonly #emitter: Emitter<SkillManagerEventMap>\n\treadonly #format: ContextSectionFormat<Skill> | undefined\n\n\tconstructor(options?: SkillManagerOptions) {\n\t\tthis.#emitter = new Emitter<SkillManagerEventMap>(options)\n\t\tthis.#format = options?.format\n\t}\n\n\tget emitter(): EmitterInterface<SkillManagerEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget count(): number {\n\t\treturn this.#skills.size\n\t}\n\n\tget open(): string {\n\t\treturn this.#format?.open ?? SKILL_SECTION_HEADER\n\t}\n\n\tget close(): string | undefined {\n\t\treturn this.#format?.close\n\t}\n\n\tadd(input: SkillInput): Skill\n\tadd(inputs: readonly SkillInput[]): readonly Skill[]\n\tadd(input: SkillInput | readonly SkillInput[]): Skill | readonly Skill[] {\n\t\tif (isArray(input)) return input.map((one) => this.#create(one))\n\t\treturn this.#create(input)\n\t}\n\n\tskill(name: string): Skill | undefined {\n\t\treturn this.#skills.get(name)\n\t}\n\n\tskills(): readonly Skill[] {\n\t\treturn [...this.#skills.values()]\n\t}\n\n\trender(skill: Skill): string {\n\t\treturn this.#format?.render?.(skill) ?? renderIndexEntry(skill)\n\t}\n\n\tremove(name: string): boolean\n\tremove(names: readonly string[]): boolean\n\tremove(names: string | readonly string[]): boolean {\n\t\tif (isArray(names)) {\n\t\t\t// Each present name still emits.\n\t\t\treturn removeEntries(names, (name) => this.#delete(name))\n\t\t}\n\t\treturn this.#delete(names)\n\t}\n\n\tclear(): void {\n\t\tthis.#skills.clear()\n\t\tthis.#emitter.emit('clear')\n\t}\n\n\t// Mint a skill and its files, store it by name (a same-name one is replaced in place), and emit\n\t// `add` after the map changed, so a swallowed listener throw can't perturb it.\n\t#create(input: SkillInput): Skill {\n\t\tconst skill: Skill = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\tname: input.name,\n\t\t\tdescription: input.description,\n\t\t\tcontent: input.content,\n\t\t\tfiles: (input.files ?? []).map((file) => createFile(file)),\n\t\t}\n\t\tthis.#skills.set(skill.name, skill)\n\t\tthis.#emitter.emit('add', skill)\n\t\treturn skill\n\t}\n\n\t// Delete one skill, emitting `remove` only when one was present — after the deletion.\n\t#delete(name: string): boolean {\n\t\tconst removed = this.#skills.delete(name)\n\t\tif (removed) this.#emitter.emit('remove', name)\n\t\treturn removed\n\t}\n}\n","import type { MemoSnapshot, MemoStoreInterface } from '../types.js'\n\n/**\n * Implements the {@link MemoStoreInterface} in memory — a process-lifetime map of\n * {@link MemoSnapshot} records keyed by memo set id, the store\n * {@link import('../factories.js').createMemoryMemoStore} builds.\n *\n * @remarks\n * The snapshot is plain JSON data, so the memory tier needs no encoding. A stored set lives until\n * an explicit `delete`. Its driver-pluggable twin is\n * {@link import('./DatabaseMemoStore.js').DatabaseMemoStore} (the snapshot as one opaque JSON\n * column).\n *\n * This store copies nothing: `get` returns the object `set` received, as a readonly\n * {@link MemoSnapshot}, where the twin reads back the copy its driver made. `createMemoManager`\n * copies a snapshot through JSON, so a restored manager shares no state with this store.\n *\n * - **`get` resolves the stored snapshot for an id**, or `undefined` if none is stored.\n * - **`set` inserts or replaces under the snapshot's own `id`** (no separate id param).\n * - **`delete` drops a snapshot by id**; an absent id is a no-op (no throw).\n *\n * @example\n * ```ts\n * import { createMemoManager, createMemoryMemoStore } from '@orkestrel/agent'\n *\n * const store = createMemoryMemoStore()\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set\n * const snapshot = await store.get('ada')\n * await store.delete('ada') // drop it\n * ```\n */\nexport class MemoryMemoStore implements MemoStoreInterface {\n\t// Every stored set by id. Replaced on every change rather than mutated in place.\n\t#snapshots: ReadonlyMap<string, MemoSnapshot> = new Map()\n\n\t/**\n\t * Resolves the stored snapshot for `id`, or `undefined` if none is stored.\n\t *\n\t * @param id - The memo set id to resolve (a {@link MemoSnapshot.id})\n\t * @returns The stored snapshot, or `undefined` if absent\n\t */\n\tget(id: string): Promise<MemoSnapshot | undefined> {\n\t\treturn Promise.resolve(this.#snapshots.get(id))\n\t}\n\n\t/**\n\t * Inserts or replaces a snapshot under its own `snapshot.id`.\n\t *\n\t * @param snapshot - The snapshot to store, keyed by its `id`\n\t */\n\tset(snapshot: MemoSnapshot): Promise<void> {\n\t\tthis.#snapshots = new Map([...this.#snapshots, [snapshot.id, snapshot]])\n\t\treturn Promise.resolve()\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op.\n\t *\n\t * @param id - The memo set id to drop\n\t */\n\tdelete(id: string): Promise<void> {\n\t\tthis.#snapshots = new Map([...this.#snapshots].filter(([key]) => key !== id))\n\t\treturn Promise.resolve()\n\t}\n}\n","import type { MemoSnapshot, MemoSnapshotRow, MemoStoreInterface } from '../types.js'\nimport type { AuditFault } from '@orkestrel/contract'\nimport type { TableInterface } from '@orkestrel/database'\nimport { describeFaults, preview } from '@orkestrel/contract'\nimport { memoSnapshotContract } from '../contracts.js'\nimport { ContextError } from '../errors.js'\nimport { isMemoSnapshot } from '../validators.js'\nimport { describeSnapshotRefusal } from '../../helpers.js'\n\n/**\n * Backs a {@link MemoStoreInterface} with one table of the `databases` layer — a memo set's durable\n * state is a row holding the snapshot as one opaque JSON column, narrowed back on `get` by\n * {@link import('../validators.js').isMemoSnapshot}.\n *\n * @remarks\n * The store is driver-agnostic: it holds a single {@link TableInterface} whose backend (memory,\n * JSON, SQLite, IndexedDB) is chosen by whoever builds it (the factories), so a JSON / SQLite /\n * IndexedDB backend swaps in without touching the\n * {@link import('../memos/MemoManager.js').MemoManager}. Default driver: memory\n * ({@link import('../factories.js').createDatabaseMemoStore} passes `createMemoryDriver()`), so it\n * also works in memory out of the box; pass a JSON / SQLite / IndexedDB driver for a durable store.\n *\n * The {@link MemoSnapshot} is stored as one opaque JSON column — the table is a row of\n * `{ id; snapshot }` ({@link MemoSnapshotRow}), the snapshot the whole JSON blob (a `rawShape`\n * column the factory builds). The snapshot is already a complete, self-contained, pure-JSON payload,\n * so storing it whole is lossless and keeps the row type flat (`snapshot` reads back as `unknown`).\n *\n * - **`set(snapshot)` upserts under the snapshot's own `id`** (no separate id param) — it writes\n * the row `{ id: snapshot.id, snapshot }`.\n * - **`get(id)` resolves the stored snapshot for an id**, narrowing the opaque JSON column back to\n * a {@link MemoSnapshot} ({@link import('../validators.js').isMemoSnapshot} — the total guard for\n * an untrusted storage read), or `undefined` only when no row is stored. A row whose blob is malformed\n * or names another id throws a `ContextError` coded `'SNAPSHOT'`, so a host never overwrites it\n * unawares, and `delete` still removes it.\n * - **`delete(id)` drops a snapshot by id**; an absent id is a no-op (no throw).\n *\n * @example\n * ```ts\n * import { createDatabaseMemoStore, createMemoManager } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseMemoStore(createMemoryDriver()) // a durable driver swaps in here\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set (one JSON column)\n * const snapshot = await store.get('ada')\n * await store.delete('ada') // drop it\n * ```\n */\nexport class DatabaseMemoStore implements MemoStoreInterface {\n\treadonly #table: TableInterface<MemoSnapshotRow>\n\n\t/**\n\t * Wraps a table as a memo store.\n\t *\n\t * @param table - The {@link TableInterface} holding the snapshots — its row is the\n\t * {@link MemoSnapshotRow} `{ id; snapshot }` shape (the snapshot one opaque JSON column)\n\t */\n\tconstructor(table: TableInterface<MemoSnapshotRow>) {\n\t\tthis.#table = table\n\t}\n\n\t/**\n\t * Resolves the stored snapshot for `id`, narrowing the opaque JSON column back to a\n\t * `MemoSnapshot`.\n\t *\n\t * @remarks\n\t * The column is an untrusted storage read, so a blob that fails\n\t * {@link import('../validators.js').isMemoSnapshot}, or whose own `id` is not `id`, throws rather\n\t * than reading as absent.\n\t *\n\t * @param id - The memo set id to resolve (the row key)\n\t * @returns The stored snapshot, or `undefined` when no row is stored\n\t * @throws {ContextError} Thrown when the row's blob is malformed or names another id (code `'SNAPSHOT'`), with the faults under `context.faults`\n\t */\n\tasync get(id: string): Promise<MemoSnapshot | undefined> {\n\t\tconst row = await this.#table.get(id)\n\t\tif (row === undefined) return undefined\n\t\t// The snapshot crosses back as an untrusted storage read (a structured clone / a JSON row),\n\t\t// so narrow the opaque JSON column with the total boundary guard rather than a cast;\n\t\t// `audit` runs only after the guard refused, to fill the faults.\n\t\tconst snapshot = row.snapshot\n\t\tif (isMemoSnapshot(snapshot)) {\n\t\t\tif (snapshot.id === id) return snapshot\n\t\t\tconst faults: readonly AuditFault[] = [\n\t\t\t\t{\n\t\t\t\t\treason: 'type',\n\t\t\t\t\tpath: ['id'],\n\t\t\t\t\texpected: 'literal',\n\t\t\t\t\treceived: preview(snapshot.id),\n\t\t\t\t},\n\t\t\t]\n\t\t\tthrow new ContextError('SNAPSHOT', describeSnapshotRefusal('memo set', id, faults), {\n\t\t\t\tcontext: { id, faults },\n\t\t\t})\n\t\t}\n\t\tconst faults = memoSnapshotContract.audit(snapshot)\n\t\tthrow new ContextError('SNAPSHOT', describeSnapshotRefusal('memo set', id, faults), {\n\t\t\tcontext: { id, faults },\n\t\t})\n\t}\n\n\t/**\n\t * Inserts or replaces under the snapshot's own `id` (no separate id param) — the row is\n\t * `{ id, snapshot }`.\n\t *\n\t * @param snapshot - The snapshot to store, keyed by its `id`\n\t * @returns A promise that resolves after the row is written\n\t * @throws {ContextError} Thrown before any write when the snapshot fails the guard `get` reads (code `'SNAPSHOT'`), so no row is written that `get` refuses\n\t */\n\tasync set(snapshot: MemoSnapshot): Promise<void> {\n\t\tif (!isMemoSnapshot(snapshot)) {\n\t\t\tconst unchecked: unknown = snapshot\n\t\t\tconst faults = memoSnapshotContract.audit(unchecked)\n\t\t\tthrow new ContextError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`a memo store writes only what snapshot() returned\\n${describeFaults(faults, memoSnapshotContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tawait this.#table.set({ id: snapshot.id, snapshot })\n\t}\n\n\t/**\n\t * Drops a snapshot by id; an absent id is a no-op (no throw).\n\t *\n\t * @param id - The memo set id to drop\n\t * @returns A promise that resolves after the row is gone\n\t */\n\tasync delete(id: string): Promise<void> {\n\t\tawait this.#table.remove(id)\n\t}\n}\n","import type {\n\tAgentContextInterface,\n\tAgentContextOptions,\n\tInstructionManagerInterface,\n\tMemoManagerInterface,\n\tScopeInterface,\n\tSelection,\n\tSelectionHandler,\n\tSkillManagerInterface,\n} from './types.js'\nimport type { ConversationInterface, ConversationManagerInterface } from '../conversations/index.js'\nimport type { Message } from '../types.js'\nimport type { ToolManagerInterface } from '@orkestrel/tool'\nimport type { WorkspaceManagerInterface } from '@orkestrel/workspace'\nimport { ToolManager } from '@orkestrel/tool'\nimport { isTextContent, WorkspaceManager } from '@orkestrel/workspace'\nimport { InstructionManager } from './instructions/InstructionManager.js'\nimport { MemoManager } from './memos/MemoManager.js'\nimport { SkillManager } from './skills/SkillManager.js'\nimport { ConversationError, ConversationManager } from '../conversations/index.js'\nimport { CONVERSATION_ROLES } from '../constants.js'\nimport { filterAllowList } from '../helpers.js'\nimport { attachUserImages, collectImageData, renderFencedFile, renderSection } from './helpers.js'\nimport { WORKSPACE_SECTION_HEADER } from './constants.js'\n\n/**\n * Assembles a provider request from the richer turn context — the optional system prompt, the\n * observable context managers (instructions / skills / memos / workspaces), the\n * {@link ConversationManagerInterface} message source whose active conversation is `messages`, the\n * {@link ToolManagerInterface} registry, and an active {@link ScopeInterface} changed through\n * {@link AgentContextInterface.apply}. `build()` folds the system prompt, the scoped instructions,\n * and the skills index into one system message, the memos index and the active workspace's text into\n * one `user` context message after it, then the conversation, and never reads `tools`.\n *\n * @remarks\n * - **Composition.** `system` is the optional system prompt; `instructions` / `skills` / `memos` /\n * `tools` / `workspaces` / `conversations` are the registries passed in `options` (bring your own),\n * or fresh empty ones when omitted (so `workspaces` is always present); `messages` is the active\n * conversation's live tail (always defined — see the following item). `scope` is the active filter —\n * `undefined` (the default) ⇒ no filtering; change it through `apply(scope)`. The structural\n * `workspaces` / `conversations` registries are fixed at construction; switch their active\n * members through their own `switch(id)` methods.\n * - **The default conversation — the conversation registry's active conversation.** `conversations`\n * is a {@link ConversationManagerInterface}. The constructor adds a default conversation when the\n * manager has no active one, so the dynamic `messages` getter — `this.#conversations.active` — is\n * always defined. `messages` returns the active conversation itself (it owns the live tail + the\n * message verbs directly, satisfying\n * {@link import('../conversations/index.js').MessageManagerInterface} structurally — the same\n * reference, no duplication), and `build()` with no selection folds that conversation's `view()`\n * (its per-section summaries + live tail) as the authoritative message inclusion — the scope does\n * not filter the conversation (it owns inclusion through compaction; scope filters only\n * instructions / skills / memos / tools / workspace files). An agent run binds a conversation at\n * its call, the active one unless the run names another, and builds through a selection over that\n * conversation, so `conversations.switch(id)` sets the next run's default at any moment and never\n * moves a live run; `select(conversation, request, signal)` checks the conversation it is given.\n * - **`build()` — the scoped assembly + the format cascade.** It folds, in order,\n * the system prompt, the scope-filtered instructions (a block: the section's `open` text,\n * each item's rendering, then any `close` text), then the scope-filtered skills index (a block\n * whose entries join with a single newline) into one leading `system` message (prepended only\n * when at least one part exists), then the scope-filtered memos index (the same block) and the\n * active workspace's text files into one `user` context message (present only when the memos or\n * workspace section renders), then\n * appends the active conversation's `view()` (the conversation owns message inclusion through\n * compaction — the scope does not filter the conversation). The instruction manager resolves\n * each `open` / item / `close` most-specific-first: `open` = manager-options-override >\n * built-in; per item = item-override > manager-options-override > built-in; `close` =\n * manager-options-override (no built-in ⇒ no closing line when unset) (see\n * {@link AgentContextInterface.build}). With no override set, each section is its built-in\n * header + items, no closing line. The active workspace's scoped-in image files' `base64` payload is attached to\n * the last user message (a vision provider reads images off a user turn); when no user message\n * exists the attachment is skipped. Built fresh each call (recomputed, never cached), so it\n * always reflects the current managers / messages / scope / active workspace; it never mutates a\n * manager or the stored messages. `build(selection)` adds at most one `system` message of its own\n * and reads the selection's messages as conversation parts without a role check; the select path\n * refuses a selected `system` role as a `'ROLE'` fault over `view()`; a host's direct\n * `build(selection)` is host-trusted, and a conversation a host implements itself whose `view()`\n * holds a `system` message is read as host-trusted too.\n * - **The active workspace, rendered by carrier — the sole document/image context.**\n * `workspaces.active` (when set) has its {@link import('@orkestrel/workspace').FileInterface} values scope-filtered by `scope.files`,\n * then split: text files fold into a dedicated `## Workspace` section (fenced reference blocks)\n * in the context message — a `user` message with a minted id, never stored — and image files'\n * `base64` payload attaches to the last user message of the conversation, never to the context\n * message. The `system` message holds host-authored text alone: the system prompt, the\n * instructions, and the skills index. Active-only — never the other registered workspaces; with no\n * active workspace nothing renders for workspaces. `build()` owns this render (a `Workspace` /\n * `WorkspaceManager` stays file-focused).\n * - **Tools are structural, not in the prompt.** The registry is advertised to the provider\n * through `tools.definitions()` (scope-filtered by the loop), never serialized into the\n * message array — so the output of the `build()` method carries no tool content, scoped or not.\n * - **Event-free context; observable managers.** The context itself owns no Emitter; the\n * context managers each carry their own (the push observation surface).\n *\n * @example\n * ```ts\n * const context = new AgentContext({ system: 'You are concise.' })\n * context.instructions.add({ name: 'tone', content: 'Be terse.' })\n * context.messages.add({ role: 'user', content: 'Hi' })\n * context.build() // [{ role: 'system', content: 'You are concise.\\n\\n## Instructions\\n\\nBe terse.' }, { role: 'user', content: 'Hi' }]\n * ```\n */\nexport class AgentContext implements AgentContextInterface {\n\treadonly #system: string | undefined\n\treadonly #instructions: InstructionManagerInterface\n\treadonly #skills: SkillManagerInterface\n\treadonly #memos: MemoManagerInterface\n\t// The workspace registry whose active workspace `build()` renders by carrier (text files →\n\t// the context message, image files → the last user message). Always present (a fresh empty\n\t// manager when none was supplied). The registry is structural; switch its active workspace\n\t// through `workspaces.switch(id)`. `build()` reads `active` / its `files()` fresh each call.\n\treadonly #workspaces: WorkspaceManagerInterface\n\t// The conversation registry whose active conversation is a run's default: the dynamic `messages`\n\t// getter returns `#conversations.active`, which the constructor seeds when absent, and `build()`\n\t// with no selection folds that conversation's `view()`. The registry is structural; switch its\n\t// active conversation through `conversations.switch(id)`.\n\treadonly #conversations: ConversationManagerInterface\n\treadonly #tools: ToolManagerInterface\n\t// The agent default; the active scope's `select` overrides it at each `select` call.\n\treadonly #select: SelectionHandler | undefined\n\t#scope: ScopeInterface | undefined\n\n\tconstructor(options?: AgentContextOptions) {\n\t\tthis.#system = options?.system\n\t\tthis.#instructions = options?.instructions ?? new InstructionManager()\n\t\tthis.#skills = options?.skills ?? new SkillManager()\n\t\tthis.#memos = options?.memos ?? new MemoManager()\n\t\t// Default to a fresh empty registry so `context.workspaces` is always present (mirroring\n\t\t// the always-present instruction manager); a supplied one is reused. The active workspace\n\t\t// is the sole document/image context.\n\t\tthis.#workspaces = options?.workspaces ?? new WorkspaceManager()\n\t\t// The conversation registry the default conversation flows from — a supplied one is reused,\n\t\t// else a fresh empty one. Seed an active conversation so `context.messages` (the active conversation's\n\t\t// live tail) is always defined: when the manager has none active, call `add()` for a default\n\t\t// (which auto-activates it). NB: `messages` is not captured here — it is computed dynamically\n\t\t// (the getter reads `#conversations.active`), so it always tracks the current active\n\t\t// conversation's live tail, no duplication.\n\t\tthis.#conversations = options?.conversations ?? new ConversationManager()\n\t\tif (this.#conversations.active === undefined) this.#conversations.add()\n\t\tthis.#tools = options?.tools ?? new ToolManager()\n\t\tthis.#select = options?.select\n\t\tthis.#scope = options?.scope\n\t}\n\n\tget system(): string | undefined {\n\t\treturn this.#system\n\t}\n\n\tget instructions(): InstructionManagerInterface {\n\t\treturn this.#instructions\n\t}\n\n\tget skills(): SkillManagerInterface {\n\t\treturn this.#skills\n\t}\n\n\tget memos(): MemoManagerInterface {\n\t\treturn this.#memos\n\t}\n\n\tget workspaces(): WorkspaceManagerInterface {\n\t\treturn this.#workspaces\n\t}\n\n\t// Dynamic — the active conversation itself (it owns its live tail + the message verbs directly,\n\t// like a `Workspace` owns its files), always defined: the constructor adds a default when the\n\t// registry has no active conversation. Computed on every read (never captured), so\n\t// `context.messages` always points at the current active conversation (the same reference — no\n\t// duplication) and follows a `conversations.switch(id)`. It is the default a run binds, never a\n\t// live run's target. The `#ensureConversation()` method re-seats a default if a caller's supplied\n\t// manager was emptied (for example `clear()`), so the getter is total — never undefined.\n\tget messages(): ConversationInterface {\n\t\treturn this.#ensureConversation()\n\t}\n\n\tget conversations(): ConversationManagerInterface {\n\t\treturn this.#conversations\n\t}\n\n\tget tools(): ToolManagerInterface {\n\t\treturn this.#tools\n\t}\n\n\tget scope(): ScopeInterface | undefined {\n\t\treturn this.#scope\n\t}\n\n\tapply(scope: ScopeInterface | undefined): void {\n\t\tthis.#scope = scope\n\t}\n\n\tselect(\n\t\tconversation: ConversationInterface,\n\t\trequest: Message,\n\t\tsignal: AbortSignal,\n\t): Promise<Selection> | undefined {\n\t\t// Returning `undefined` synchronously keeps the loop's default path free of an `await`.\n\t\tconst handler = this.#scope?.select ?? this.#select\n\t\tif (handler === undefined) return undefined\n\t\treturn this.#check(handler, conversation, request, signal)\n\t}\n\n\tbuild(selection?: Selection): readonly Message[] {\n\t\tconst scope = this.#scope\n\t\t// Tools reach the provider structurally. Each manager resolves its format cascade.\n\t\tconst parts: string[] = []\n\t\t// Configured by `=== undefined`, not falsiness — an explicitly supplied '' (or a\n\t\t// whitespace-only) system is opted in and prepended verbatim; a truthiness check would drop it.\n\t\tif (this.#system !== undefined) parts.push(this.#system)\n\t\tconst instructions = filterAllowList(\n\t\t\tscope?.instructions,\n\t\t\tthis.#instructions.instructions(),\n\t\t\t(one) => one.name,\n\t\t)\n\t\tconst instructed = renderSection(\n\t\t\tthis.#instructions.open,\n\t\t\tinstructions,\n\t\t\t(one) => this.#instructions.render(one),\n\t\t\tthis.#instructions.close,\n\t\t)\n\t\tif (instructed !== undefined) parts.push(instructed)\n\t\tconst skills = filterAllowList(scope?.skills, this.#skills.skills(), (one) => one.name)\n\t\tconst indexed = renderSection(\n\t\t\tthis.#skills.open,\n\t\t\tskills.length === 0 ? [] : [skills.map((one) => this.#skills.render(one)).join('\\n')],\n\t\t\t(block) => block,\n\t\t\tthis.#skills.close,\n\t\t)\n\t\tif (indexed !== undefined) parts.push(indexed)\n\t\t// The active workspace is the sole document/image context. `build()` owns this render\n\t\t// because a `Workspace` / `WorkspaceManager` stays file-focused.\n\t\tconst files = filterAllowList(\n\t\t\tscope?.files,\n\t\t\tthis.#workspaces.active?.files() ?? [],\n\t\t\t(one) => one.path,\n\t\t)\n\t\tconst workspaceTexts = files.flatMap((file) =>\n\t\t\tisTextContent(file.content)\n\t\t\t\t? [{ path: file.path, language: file.content.language, text: file.content.text }]\n\t\t\t\t: [],\n\t\t)\n\t\tconst documented = renderSection(\n\t\t\tWORKSPACE_SECTION_HEADER,\n\t\t\tworkspaceTexts,\n\t\t\t(file) => renderFencedFile(file.path, file.language, file.text),\n\t\t\tundefined,\n\t\t)\n\t\tconst memos = filterAllowList(scope?.memos, this.#memos.memos(), (one) => one.name)\n\t\tconst remembered = renderSection(\n\t\t\tthis.#memos.open,\n\t\t\tmemos.length === 0 ? [] : [memos.map((one) => this.#memos.render(one)).join('\\n')],\n\t\t\t(block) => block,\n\t\t\tthis.#memos.close,\n\t\t)\n\t\t// With no selection, the active conversation's `view()` is authoritative (the per-section\n\t\t// summaries + the live tail) — the conversation owns message inclusion through compaction, so the\n\t\t// scope does not filter the conversation here (scope filters only the preceding instructions /\n\t\t// skills / memos / tools / workspace files). The active conversation is always present (the\n\t\t// constructor adds one), with `#ensureConversation()` as a total fallback if a caller emptied its\n\t\t// supplied registry.\n\t\tconst conversation = selection?.messages ?? this.#ensureConversation().view()\n\t\t// A vision provider reads image payloads from a user turn.\n\t\tconst tail = attachUserImages(conversation, collectImageData(files))\n\t\tconst slots = [remembered, documented].filter((slot) => slot !== undefined)\n\t\tconst context: readonly Message[] =\n\t\t\tslots.length === 0\n\t\t\t\t? []\n\t\t\t\t: [{ id: crypto.randomUUID(), role: 'user', content: slots.join('\\n\\n') }]\n\n\t\tif (parts.length === 0) return [...context, ...tail]\n\t\tconst system: Message = {\n\t\t\tid: crypto.randomUUID(),\n\t\t\trole: 'system',\n\t\t\tcontent: parts.join('\\n\\n'),\n\t\t}\n\t\treturn [system, ...context, ...tail]\n\t}\n\n\t// Another run can append to the conversation while the handler awaits, so the view's ids are\n\t// recorded before the call (an async body runs synchronously up to its first `await`) and\n\t// compared after it; a changed view turns the handler's selection into a fault over `view()`.\n\t// Over an unchanged view, a selected message whose role is not a conversation role does the same.\n\tasync #check(\n\t\thandler: SelectionHandler,\n\t\tconversation: ConversationInterface,\n\t\trequest: Message,\n\t\tsignal: AbortSignal,\n\t): Promise<Selection> {\n\t\tconst before = conversation.view().map((message) => message.id)\n\t\tconst selection = await handler(conversation, request, signal)\n\t\tconst after = conversation.view()\n\t\tif (\n\t\t\tafter.length === before.length &&\n\t\t\tafter.every((message, index) => message.id === before[index])\n\t\t) {\n\t\t\tconst refused = selection.messages.find(\n\t\t\t\t(message) => !CONVERSATION_ROLES.some((role) => role === message.role),\n\t\t\t)\n\t\t\tif (refused === undefined) return selection\n\t\t\tconst role =\n\t\t\t\ttypeof refused.role === 'string'\n\t\t\t\t\t? JSON.stringify(refused.role.slice(0, 32))\n\t\t\t\t\t: typeof refused.role\n\t\t\tconst reason = `a selected message role must be one of ${CONVERSATION_ROLES.map((one) => JSON.stringify(one)).join(', ')}; message ${selection.messages.indexOf(refused)} has ${role}`\n\t\t\treturn {\n\t\t\t\tmessages: after,\n\t\t\t\tjudgments: selection.judgments,\n\t\t\t\t...(selection.usage === undefined ? {} : { usage: selection.usage }),\n\t\t\t\tfault: new ConversationError(\n\t\t\t\t\t'ROLE',\n\t\t\t\t\treason,\n\t\t\t\t\tselection.fault === undefined ? undefined : { cause: selection.fault },\n\t\t\t\t),\n\t\t\t}\n\t\t}\n\t\tconst text = `conversation ${conversation.id} changed during selection: ${before.length} messages before, ${after.length} after`\n\t\treturn {\n\t\t\tmessages: after,\n\t\t\tjudgments: selection.judgments,\n\t\t\t...(selection.usage === undefined ? {} : { usage: selection.usage }),\n\t\t\tfault:\n\t\t\t\tselection.fault === undefined\n\t\t\t\t\t? new Error(text)\n\t\t\t\t\t: new Error(text, { cause: selection.fault }),\n\t\t}\n\t}\n\n\t// A supplied registry can be emptied after construction. Re-seat a default so the message\n\t// source stays defined, preferring the active conversation an add observer may have selected.\n\t#ensureConversation(): ConversationInterface {\n\t\tif (this.#conversations.active !== undefined) return this.#conversations.active\n\t\tconst conversation = this.#conversations.add()\n\t\treturn this.#conversations.active ?? conversation\n\t}\n}\n","import type {\n\tAgentContextInterface,\n\tAgentContextOptions,\n\tInstructionInput,\n\tInstructionInterface,\n\tInstructionManagerInterface,\n\tInstructionManagerOptions,\n\tMemoManagerInterface,\n\tMemoManagerOptions,\n\tMemoSnapshotRow,\n\tMemoStoreInterface,\n\tScopeInput,\n\tScopeInterface,\n\tScopeManagerInterface,\n\tScopeManagerOptions,\n\tSkillManagerInterface,\n\tSkillManagerOptions,\n} from './types.js'\nimport type { DriverInterface, TableInterface } from '@orkestrel/database'\nimport { rawShape, stringShape } from '@orkestrel/contract'\nimport { createDatabase, createMemoryDriver } from '@orkestrel/database'\nimport { Instruction } from './instructions/Instruction.js'\nimport { InstructionManager } from './instructions/InstructionManager.js'\nimport { MemoManager } from './memos/MemoManager.js'\nimport { Scope } from './scopes/Scope.js'\nimport { ScopeManager } from './scopes/ScopeManager.js'\nimport { SkillManager } from './skills/SkillManager.js'\nimport { MemoryMemoStore } from './stores/MemoryMemoStore.js'\nimport { DatabaseMemoStore } from './stores/DatabaseMemoStore.js'\nimport { AgentContext } from './AgentContext.js'\n\n/**\n * Creates an instruction — an immutable {@link InstructionInterface} (a named directive)\n * from its `name` / `content` and optional `priority`, the `id` minted at construction.\n *\n * @remarks\n * Only `name` / `content` are required; `priority` orders the instruction in the\n * rendered list of an {@link InstructionManagerInterface} (higher first). Default: `0`.\n * Stored immutable — never mutated after creation.\n *\n * @param input - `name` / `content` (required) and an optional `priority` (see\n * {@link InstructionInput})\n * @returns A working {@link InstructionInterface}\n *\n * @example\n * ```ts\n * import { createInstruction } from '@orkestrel/agent'\n *\n * const instruction = createInstruction({ name: 'tone', content: 'Be concise.', priority: 5 })\n * ```\n */\nexport function createInstruction(input: InstructionInput): InstructionInterface {\n\treturn new Instruction(input)\n}\n\n/**\n * Creates an instruction registry — an {@link InstructionManagerInterface} holding\n * immutable instructions keyed by `name`, listed by descending `priority`.\n *\n * @remarks\n * Starts empty; `add` (one or a batch) mints each `id` and overwrites a same-name\n * instruction (last write wins); `instructions()` lists them sorted by descending\n * `priority` (stable for ties); `open` / `render` / `close` are the build contract a richer\n * context renders an instructions block with; `remove` (one or a batch) reports `true` only\n * when every supplied name was removed; `clear` empties it. Carries an observable `emitter`\n * ({@link import('./types.js').InstructionManagerEventMap}) wired through the reserved `on`\n * option; the emitter isolates a listener throw and routes it to its `error` handler\n * (the `error` option), so it can never corrupt a mutation. An optional `format`\n * override is the manager-options level of the `AgentContext` build cascade (consulted by\n * `open` / `render` / `close`, beating the built-in; a per-item\n * `InstructionInput.override` still beats it).\n *\n * @param options - Optional `on` hooks + a `format` override (see {@link InstructionManagerOptions})\n * @returns An empty {@link InstructionManagerInterface}\n *\n * @example\n * ```ts\n * import { createInstructionManager } from '@orkestrel/agent'\n *\n * const instructions = createInstructionManager()\n * instructions.add({ name: 'tone', content: 'Be concise.', priority: 5 })\n * ```\n */\nexport function createInstructionManager(\n\toptions?: InstructionManagerOptions,\n): InstructionManagerInterface {\n\treturn new InstructionManager(options)\n}\n\n/**\n * Creates a named scope — an immutable {@link ScopeInterface} from its `name` and its\n * per-category allow-lists, the `id` minted at construction.\n *\n * @remarks\n * Each list is three-way: `undefined` ⇒ no constraint on that category (all pass), `[]` ⇒\n * none pass, a non-empty list ⇒ only the listed keys pass. `narrow(config)` composes a\n * tighter child by set-intersection (an `undefined` side imposing no constraint). Stored\n * immutable — never mutated after creation (`narrow` returns a new scope).\n *\n * @param input - `name` (required) and the optional `instructions` / `skills` / `memos` /\n * `tools` / `files` allow-lists (see {@link ScopeInput})\n * @returns A working {@link ScopeInterface}\n *\n * @example\n * ```ts\n * import { createScope } from '@orkestrel/agent'\n *\n * const reader = createScope({ name: 'reader', tools: ['search', 'read'] })\n * reader.narrow({ tools: ['read', 'write'] }).tools // ['read'] — intersection tightens\n * ```\n */\nexport function createScope(input: ScopeInput): ScopeInterface {\n\treturn new Scope(input)\n}\n\n/**\n * Creates a scope registry — a {@link ScopeManagerInterface} holding immutable scopes keyed\n * by their minted `id`, in insertion order.\n *\n * @remarks\n * Starts empty; `add` mints each scope's `id` and stores it (keyed by `id`, so it\n * never overwrites — two scopes may share a `name`); `scopes()` lists them in insertion order;\n * `remove` (one or a batch) reports `true` only when every supplied id was removed; `clear`\n * empties it. Carries an observable `emitter`\n * ({@link import('./types.js').ScopeManagerEventMap}) wired through the reserved `on`\n * option; the emitter isolates a listener throw and routes it to its `error` handler\n * (the `error` option), so it can never corrupt a mutation.\n *\n * @param options - Optional `on` hooks (see {@link ScopeManagerOptions})\n * @returns An empty {@link ScopeManagerInterface}\n *\n * @example\n * ```ts\n * import { createScopeManager } from '@orkestrel/agent'\n *\n * const scopes = createScopeManager()\n * const reader = scopes.add({ name: 'reader', tools: ['search'] })\n * ```\n */\nexport function createScopeManager(options?: ScopeManagerOptions): ScopeManagerInterface {\n\treturn new ScopeManager(options)\n}\n\n/**\n * Creates a memo set — a {@link MemoManagerInterface} holding durable facts keyed by `name`, where\n * a same-name `add` with changed text appends a revision and the earlier ones stay readable.\n *\n * @remarks\n * Starts empty unless a `snapshot` restores revisions, silently. `id` names the set: the\n * `snapshot.id`, else the `id` option, else a minted UUID. Re-adding unchanged text is a no-op that\n * emits nothing; `remove` forgets a memo and its whole history. An optional `format` override is\n * the manager-options level of the build cascade, beating the built-in `'## Memos'` header and the\n * `- name: description` index line.\n *\n * @param options - Optional `id`, `on` hooks, `error` handler, `format` override, and `snapshot`\n * (see {@link MemoManagerOptions})\n * @returns A {@link MemoManagerInterface}\n *\n * @example\n * ```ts\n * import { createMemoManager } from '@orkestrel/agent'\n *\n * const memos = createMemoManager({ id: 'ada' })\n * const tone = memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * memos.render(tone) // '- tone: Prefers short replies.'\n * ```\n */\nexport function createMemoManager(options?: MemoManagerOptions): MemoManagerInterface {\n\treturn new MemoManager(options)\n}\n\n/**\n * Creates the in-memory memo store — a {@link MemoStoreInterface} backed by a process-lifetime map,\n * the default twin of {@link createDatabaseMemoStore}.\n *\n * @remarks\n * The snapshot is already plain JSON data, so the memory tier needs no encoding. `get` / `set` /\n * `delete` are async (the same shape a durable backend fits), and a stored memo set lives until an\n * explicit `delete`. Its driver-pluggable twin is {@link createDatabaseMemoStore} (the snapshot as\n * one opaque JSON column over a `databases` table). Restoring stays a caller concern: read a\n * snapshot back and pass it to `createMemoManager({ snapshot })`.\n *\n * @returns A memory-backed {@link MemoStoreInterface}\n *\n * @example\n * ```ts\n * import { createMemoManager, createMemoryMemoStore } from '@orkestrel/agent'\n *\n * const store = createMemoryMemoStore()\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set\n * const snapshot = await store.get('ada')\n * ```\n */\nexport function createMemoryMemoStore(): MemoStoreInterface {\n\treturn new MemoryMemoStore()\n}\n\n/**\n * Creates a {@link DatabaseMemoStore} over any {@link DriverInterface} — the durable, driver-pluggable\n * backing for a memo set, holding each snapshot as one opaque JSON column in a `memos` table.\n *\n * @remarks\n * Builds a one-table database (`memos`, keyed by `id`) over the supplied driver, the snapshot held\n * as one opaque JSON column — the column map is `{ id; snapshot }` where `snapshot` is a `rawShape`\n * (a JSON blob). The snapshot is already a complete, self-contained, pure-JSON payload, so storing\n * it whole is lossless and keeps the row type flat (the column reads back as `unknown`, narrowed on\n * `get` by {@link import('./validators.js').isMemoSnapshot}). Default driver:\n * {@link createMemoryDriver}, so the store also works in memory out of the box; pass a server\n * `createJSONDriver` / `createSQLiteDriver` (or a browser IndexedDB driver) for a persistent one —\n * the durability is the driver's job, the store engine is shared. It swaps in behind\n * {@link MemoStoreInterface} without touching the manager.\n *\n * @param driver - The storage backend the snapshots persist to. Default: {@link createMemoryDriver}\n * @returns A {@link MemoStoreInterface} over the driver\n *\n * @example\n * ```ts\n * import { createDatabaseMemoStore, createMemoManager } from '@orkestrel/agent'\n * import { createMemoryDriver } from '@orkestrel/database'\n *\n * const store = createDatabaseMemoStore(createMemoryDriver()) // a durable driver swaps in here\n * const memos = createMemoManager({ id: 'ada' })\n * memos.add({ name: 'tone', description: 'Prefers short replies.', content: 'Answer briefly.' })\n * await store.set(memos.snapshot()) // store the memo set (one JSON column)\n * const snapshot = await store.get('ada')\n * ```\n */\nexport function createDatabaseMemoStore(\n\tdriver: DriverInterface = createMemoryDriver(),\n): MemoStoreInterface {\n\t// The snapshot is stored as one opaque JSON column (`rawShape`), so the row infers flat —\n\t// `{ id: string; snapshot: unknown }` = `MemoSnapshotRow` — and the revisions' snapshot shape\n\t// never forces a contract `Infer`.\n\tconst columns = { id: stringShape(), snapshot: rawShape({}) }\n\tconst database = createDatabase({ driver, tables: { memos: columns } })\n\tconst table: TableInterface<MemoSnapshotRow> = database.table('memos')\n\treturn new DatabaseMemoStore(table)\n}\n\n/**\n * Creates a skill registry — a {@link SkillManagerInterface} holding the skills a host supplies,\n * keyed by `name` and listed in insertion order.\n *\n * @remarks\n * Starts empty; `add` (one or a batch) mints each `id` and each file through `createFile`, and\n * overwrites a same-name skill (last write wins, position kept). An optional `format` override is\n * the manager-options level of the build cascade, beating the built-in `'## Skills'` header and the\n * `- name: description` index line.\n *\n * @param options - Optional `on` hooks, `error` handler, and `format` override (see\n * {@link SkillManagerOptions})\n * @returns An empty {@link SkillManagerInterface}\n *\n * @example\n * ```ts\n * import { createSkillManager } from '@orkestrel/agent'\n *\n * const skills = createSkillManager()\n * const review = skills.add({ name: 'review', description: 'Reviews a change.', content: 'Read the diff.' })\n * skills.render(review) // '- review: Reviews a change.'\n * ```\n */\nexport function createSkillManager(options?: SkillManagerOptions): SkillManagerInterface {\n\treturn new SkillManager(options)\n}\n\n/**\n * Creates a richer turn context — an {@link AgentContextInterface} assembling a provider request\n * from the optional system prompt, the instruction registry, the skill registry, the memo set, the\n * workspace registry (the only document channel), the conversation registry that is its `messages`\n * source, the tool registry, and the active scope, which `build()` folds into the next turn's input.\n *\n * @remarks\n * `system` is the optional system prompt; `tools` / `instructions` / `skills` / `memos` /\n * `workspaces` are pre-built managers to reuse (empty ones are created when omitted, so\n * `context.workspaces` is always present); `scope` is the initial active filter (`undefined` ⇒ no\n * filtering, changeable afterwards through `context.apply(...)`). `conversations` is the message\n * source: a supplied manager's active conversation is reused as `messages`, and a default\n * conversation is added only when it has none (a fresh registry with one default conversation is\n * created when omitted). `build()` folds the system prompt, the scoped instructions, and the scoped\n * skills index into one leading `system` message, renders the scoped memos index and the active\n * workspace's scope-filtered text files (fenced) into one `user` context message after it, and\n * appends the active conversation's `view()` (attaching the active workspace's scope-filtered image\n * files' `base64` payload to the last user message), built\n * fresh each call; the scope never filters the conversation. The active workspace is the sole\n * document/image context. Tools are advertised structurally (through\n * `tools.definitions()`, scope-filtered by the loop), never serialized into the prompt.\n *\n * @param options - Optional `system` / `tools` / `instructions` / `skills` / `memos` /\n * `workspaces` / `conversations` / `scope` / `select` (see {@link AgentContextOptions})\n * @returns A working {@link AgentContextInterface}\n *\n * @example\n * ```ts\n * import { createAgentContext } from '@orkestrel/agent'\n *\n * const context = createAgentContext({ system: 'You are concise.' })\n * context.instructions.add({ name: 'tone', content: 'Be terse.' })\n * context.messages.add({ role: 'user', content: 'Hi' })\n * context.build() // [{ role: 'system', content: 'You are concise.\\n\\n## Instructions\\n\\nBe terse.' }, { role: 'user', content: 'Hi' }]\n * ```\n */\nexport function createAgentContext(options?: AgentContextOptions): AgentContextInterface {\n\treturn new AgentContext(options)\n}\n","import type { AuthorityNote } from './types.js'\n\n/**\n * Caps an {@link AgentInterface} turn's tool iterations by default — `10` context → provider →\n * tools cycles before the loop stops, so a model that keeps requesting tools can never loop\n * forever. Overridable per agent through `AgentOptions.limit`.\n */\nexport const DEFAULT_AGENT_LIMIT = 10\n\n/**\n * Holds the model-facing texts the loop writes after a tool name in a denial result — the\n * `denied` note for a deny with no reason, an evaluate throw, an invalid decision, or a tool\n * changed after its decision; the `refused` note for an ask the approver refused; and the\n * `unanswered` note for an ask no approver answered, frozen.\n */\nexport const AUTHORITY_NOTES: AuthorityNote = Object.freeze({\n\tdenied: 'it is not allowed. Do not call it again; answer with what you have.',\n\trefused: 'it was refused. Do not call it again; continue without it.',\n\tunanswered:\n\t\t'it needs approval and none was given. Do not call it again; tell the user it needs approval.',\n})\n\n/**\n * Holds the model-facing text the loop writes after a tool name in the denial result of a call\n * outside the active scope's tool allow-list or whose name no definition advertises this turn.\n */\nexport const SCOPE_NOTE =\n\t'it is not one of the listed tools. Use a listed tool or answer with what you have.'\n\n/**\n * Estimates the role and framing overhead added to each message by {@link estimateMessages}.\n */\nexport const MESSAGE_TOKEN_OVERHEAD = 4\n\n/**\n * Names the coarse, deliberately approximate per-image token cost\n * {@link import('./helpers.js').estimateMessages} charges for each attached image — `512`, because\n * a base64 payload's length is no reliable token proxy.\n *\n * @remarks\n * A base64 image payload's length is not a reliable token proxy (a vision model's actual image\n * token cost depends on resolution / tiling, not byte size), so this is a fixed, coarse\n * per-image estimate rather than a derivation from `image.length` — a planning heuristic, not an\n * exact count.\n */\nexport const IMAGE_TOKEN_ESTIMATE = 512\n","import type { AgentErrorCode, AgentResult } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport {\n\tINTRINSICS,\n\tholds,\n\tisObject,\n\tisFunction,\n\tisNumber,\n\tisString,\n\tisBoolean,\n} from '@orkestrel/contract'\n\n// A real error type, not a sentinel. An agent job treats a partial result\n// (a job committed early from an abort / budget / timeout) as a failure by default — the\n// queue / runner handler throws this so the Queue's retries + a Runner's fail-fast\n// engage. It carries the partial AgentResult so a caller (or a `retries: 0` enqueue that\n// rejects with it) can still inspect what accumulated.\n\n/**\n * Reports an {@link AgentInterface} run that ended {@link AgentResult.partial} under a\n * `partial` policy of `false` (the default) — thrown by an agent-job handler (a\n * `createAgentQueue` / `createAgentRunner` job), carrying the partial {@link AgentResult} so\n * the failure stays inspectable, and the machine-readable `code` `'PARTIAL'`.\n *\n * @remarks\n * A partial result means the agent was cancelled (an external `signal` abort, a queue /\n * runner abort threaded in, a `timeout` deadline, or an exhausted token `budget`) rather\n * than finishing naturally. For a durable job that is a failure by default: throwing this\n * lets the Queue's retries re-run the job and a Runner's fail-fast abort its siblings.\n * Set `partial: true` (see `AgentQueueOptions` / `AgentRunnerOptions`) to treat a\n * partial as success instead, in which case this is never thrown. Narrow a caught value\n * with {@link isAgentJobError} to read `partial`. `code` is the machine-readable condition\n * (`'PARTIAL'` — the only one this error reports), so a `catch` branches on it rather than on\n * the message string.\n */\nexport class AgentJobError extends Error {\n\t/** Names the machine-readable condition — `'PARTIAL'`: a job that ended partial under a disallowing policy. */\n\treadonly code = 'PARTIAL' as const\n\t/** Holds the partial {@link AgentResult} the cancelled job produced. */\n\treadonly partial: AgentResult\n\n\tconstructor(message: string, partial: AgentResult) {\n\t\tsuper(message)\n\t\tthis.name = 'AgentJobError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.AgentJobError'), { value: this })\n\t\tthis.partial = partial\n\t}\n}\n\n/**\n * Narrows an unknown caught value to an {@link AgentJobError} through its identity brand and declared members, so a\n * `catch` can recover its `partial` result.\n *\n * @param value - The value to test (typically a `catch` binding or a rejected enqueue)\n * @returns True if `value` is an {@link AgentJobError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tawait queue.enqueue(job) // retries: 0 → a partial rejects with the error\n * } catch (error) {\n * \tif (isAgentJobError(error)) keep(error.partial.content) // recover the partial content\n * }\n * ```\n */\nexport function isAgentJobError(value: unknown): value is AgentJobError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.AgentJobError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (!(code === 'PARTIAL')) return false\n\t\tconst partial = INTRINSICS.reflect.read(value, 'partial')\n\t\tif (!(isObject(partial) || isFunction(partial))) return false\n\t\tconst usage = INTRINSICS.reflect.read(partial, 'usage')\n\t\tconst stop = INTRINSICS.reflect.read(partial, 'stop')\n\t\tif (stop !== undefined) {\n\t\t\tif (!(isObject(stop) || isFunction(stop))) return false\n\t\t\tconst layer = INTRINSICS.reflect.read(stop, 'layer')\n\t\t\tconst spent = INTRINSICS.reflect.read(stop, 'usage')\n\t\t\tif (\n\t\t\t\t!isString(INTRINSICS.reflect.read(stop, 'id')) ||\n\t\t\t\t!(\n\t\t\t\t\tINTRINSICS.reflect.read(stop, 'estimate') === undefined ||\n\t\t\t\t\tisNumber(INTRINSICS.reflect.read(stop, 'estimate'))\n\t\t\t\t) ||\n\t\t\t\t!(\n\t\t\t\t\tspent === undefined ||\n\t\t\t\t\t((isObject(spent) || isFunction(spent)) &&\n\t\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(spent, key)),\n\t\t\t\t\t\t))\n\t\t\t\t)\n\t\t\t)\n\t\t\t\treturn false\n\t\t\tif (\n\t\t\t\tlayer !== 'model' &&\n\t\t\t\t!(\n\t\t\t\t\t(layer === 'meter' || layer === 'budget') &&\n\t\t\t\t\tisNumber(INTRINSICS.reflect.read(stop, 'max')) &&\n\t\t\t\t\tisNumber(INTRINSICS.reflect.read(stop, 'consumed'))\n\t\t\t\t)\n\t\t\t)\n\t\t\t\treturn false\n\t\t}\n\t\treturn (\n\t\t\tisString(INTRINSICS.reflect.read(partial, 'content')) &&\n\t\t\t(INTRINSICS.reflect.read(partial, 'thinking') === undefined ||\n\t\t\t\tisString(INTRINSICS.reflect.read(partial, 'thinking'))) &&\n\t\t\tisBoolean(INTRINSICS.reflect.read(partial, 'partial')) &&\n\t\t\t(usage === undefined ||\n\t\t\t\t((isObject(usage) || isFunction(usage)) &&\n\t\t\t\t\t['prompt', 'completion', 'total'].every((key) =>\n\t\t\t\t\t\tisNumber(INTRINSICS.reflect.read(usage, key)),\n\t\t\t\t\t)))\n\t\t)\n\t})\n}\n\n// A real error type, not a sentinel. A run that would share an accounting instance or a\n// conversation with a live run would corrupt that accounting or interleave that thread, so\n// `stream()` throws this synchronously, before any state mutation or emit, rather than letting the\n// runs race. Carries a machine-readable `code` so a\n// `catch` branches on `error.code`, mirroring `ConversationError` in `conversations/errors.ts`.\n\n/**\n * Reports an `ask` on an authority that has no approver or that its approver refuses, an authority missing its fallback or a rule's verdict, tools set with no authority, a run sharing accounting or a conversation with a live run, a `limit` that is not a positive safe integer, a name absent from a registry pool, a strict run's answer that fails its schema, or a strict run whose request exceeds its window. Agent construction, the `stream` and `generate` methods, and registry accessors throw synchronously, before returning a handle or promise; a `'WINDOW'` refusal rejects the run's `result` instead, and an `'APPROVER'` refusal rejects the promise the `ask` method of an `Authority` or the `ask` handler of an `Approver` returns. Await the call inside `try`/`catch` to catch it.\n *\n * @remarks\n * `'APPROVER'` reports an `ask` on an `Authority` built with no `ask` handler, or an ask an\n * `Approver` refuses: a call framed while no ask waited for it, a second ask for a waiting call, or\n * arguments it cannot render. The agent loop reads it as an unanswered ask and denies the call. Its\n * `context` names the asked `tool`.\n * `'AUTHORITY'` reports an `Authority` built with no valid `fallback`, a rule with no valid `verdict`\n * (its `context` names the `rule` index), and an agent built with `tools` and no `authority`. Each\n * throws at construction, with the options a host passes to correct it in the message.\n * `'CONCURRENCY'` reports a run started while another run on the same agent is live, when the\n * agent carries a `window` (a shared context budget) or when the run's effective `budget` or\n * `allowance` is an instance a live run holds — the second run's `start()` would replace that\n * instance's signal and race its charges against the first run's — or when a live run is bound to\n * the run's conversation, whose turns the two runs would interleave; its `context` names the\n * `agent` id, and the `conversation` id for a shared conversation. For concurrent runs, use\n * separate agents, or one agent with no `window`, distinct budget and allowance instances per\n * run, and a distinct conversation per run.\n * `'LIMIT'` reports a construction or per-run `limit` that is not a positive safe integer,\n * including one a rehydrated job carries into `build`. `'REGISTRY'` reports a rehydration name\n * absent from its registry pool, on `provider` / `tool` / `authority` / `scheduler` / `build`,\n * whose `context` names the `category` and the `name`, and a job that names `tools` and no\n * `authority` at `build`, whose `context` names the `tools`. `'WINDOW'` reports a `strict` run whose\n * complete provider request is still over the `window` budget, equal to its `max` included, after\n * the one permitted fold or with no fold possible; it settles the run `error` before the provider\n * is called, and its `context` holds the `agent` id, the measured `consumed`, and the window `max`.\n * Narrow a caught value with {@link isAgentError} and branch on `error.code`.\n */\nexport class AgentError extends Error {\n\t/** Names the machine-readable condition — `'APPROVER'`: an `ask` on an authority that has no approver, or one its approver refuses; `'AUTHORITY'`: an authority missing its fallback or a rule's verdict, or tools set with no authority; `'CONCURRENCY'`: a run sharing accounting or a conversation with a live run; `'LIMIT'`: a `limit` that is not a positive safe integer; `'REGISTRY'`: a rehydration name absent from its registry pool; `'WINDOW'`: a strict run whose complete request is still over its window budget. */\n\treadonly code: AgentErrorCode\n\t/** Holds the values that locate the failure, such as the `agent` id of a refused run, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Creates the error with its code, message, and optional cause and context.\n\t *\n\t * @param code - The machine-readable {@link AgentErrorCode}\n\t * @param message - The human-readable description\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: AgentErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'AgentError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.AgentError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to an {@link AgentError} through its identity brand and declared members, so a `catch`\n * can branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is an {@link AgentError}; false otherwise\n *\n * @example\n * ```ts\n * try {\n * \tagent.stream()\n * } catch (error) {\n * \tif (isAgentError(error) && error.code === 'CONCURRENCY') useSeparateAgents()\n * }\n * ```\n */\nexport function isAgentError(value: unknown): value is AgentError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.AgentError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (\n\t\t\t!(\n\t\t\t\tcode === 'APPROVER' ||\n\t\t\t\tcode === 'AUTHORITY' ||\n\t\t\t\tcode === 'CONCURRENCY' ||\n\t\t\t\tcode === 'LIMIT' ||\n\t\t\t\tcode === 'REGISTRY' ||\n\t\t\t\tcode === 'SCHEMA' ||\n\t\t\t\tcode === 'WINDOW'\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tbooleanShape,\n\tjsonShape,\n\tliteralShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\trecordShape,\n\tstringShape,\n\tunionShape,\n} from '@orkestrel/contract'\nimport { messageInputShape, tokenUsageShape } from '../shapers.js'\n\n/**\n * Describes a durable agent job — the record `AgentRegistry.build` checks exactly, so a misspelled key\n * refuses instead of running an agent without it.\n *\n * @remarks\n * `children` holds JSON records, because each child crosses this shape at its own `build`. `limit`\n * reads as a number here and as a positive safe integer at the agent's construction, which owns that\n * refusal.\n */\nexport const agentJobShape = objectShape({\n\tprovider: stringShape(),\n\tmessages: optionalShape(arrayShape(messageInputShape)),\n\tsystem: optionalShape(stringShape()),\n\ttools: optionalShape(arrayShape(stringShape())),\n\tauthority: optionalShape(stringShape()),\n\tscheduler: optionalShape(stringShape()),\n\tlimit: optionalShape(numberShape()),\n\ttimeout: optionalShape(numberShape()),\n\tbudget: optionalShape(numberShape()),\n\tchildren: optionalShape(arrayShape(recordShape(jsonShape()))),\n}) satisfies ContractShape\n\n/**\n * Describes the agent frame wire — one exact object per `AgentFrame` arm, discriminated by\n * `category`.\n *\n * @remarks\n * A `tool` frame carries the tool `name` on success and nothing on failure; an `ask` frame carries\n * the minted `id`, the tool `name`, the decision's `zone`, and the `arguments` text; a `finish` frame\n * carries the result's `content`, `partial`, `usage`, and stop `layer`; an `error` frame carries\n * nothing. The guard refuses every extra member; the contract parser drops extra members.\n */\nexport const agentFrameShape = unionShape(\n\tobjectShape({ category: literalShape(['token']), content: stringShape() }),\n\tobjectShape({ category: literalShape(['think']), content: stringShape() }),\n\tobjectShape({\n\t\tcategory: literalShape(['tool']),\n\t\tsuccess: literalShape([true]),\n\t\tname: stringShape(),\n\t}),\n\tobjectShape({ category: literalShape(['tool']), success: literalShape([false]) }),\n\tobjectShape({\n\t\tcategory: literalShape(['usage']),\n\t\tusage: tokenUsageShape,\n\t\testimate: numberShape(),\n\t}),\n\tobjectShape({\n\t\tcategory: literalShape(['ask']),\n\t\tid: stringShape(),\n\t\tname: stringShape(),\n\t\tzone: stringShape(),\n\t\targuments: stringShape(),\n\t}),\n\tobjectShape({\n\t\tcategory: literalShape(['finish']),\n\t\tcontent: stringShape(),\n\t\tpartial: booleanShape(),\n\t\tusage: optionalShape(tokenUsageShape),\n\t\tlayer: optionalShape(literalShape(['meter', 'budget', 'model'])),\n\t}),\n\tobjectShape({ category: literalShape(['error']) }),\n) satisfies ContractShape\n\n/**\n * Describes the agent prompt wire — the `content` and optional `images` of one user turn, read by\n * the run's `prompt` option and by a page's request body.\n *\n * @remarks\n * The guard refuses every other member, a `role`, an `id`, and a message history included; the\n * contract parser drops extra members.\n */\nexport const agentPromptShape = objectShape({\n\tcontent: stringShape(),\n\timages: optionalShape(arrayShape(stringShape())),\n}) satisfies ContractShape\n\n/**\n * Describes the agent answer wire — the `id` of one `ask` frame and a boolean `approved`.\n *\n * @remarks\n * The guard refuses every extra member and an `approved` that is not a boolean; the contract parser\n * drops extra members.\n */\nexport const agentAnswerShape = objectShape({\n\tid: stringShape(),\n\tapproved: booleanShape(),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { agentAnswerShape, agentFrameShape, agentJobShape, agentPromptShape } from './shapers.js'\n\n/** Validates and projects agent frames at a JSON wire boundary. */\nexport const agentFrameContract = createContract(agentFrameShape)\n\n/** Validates and projects agent prompts at the run's `prompt` option and a page's request body. */\nexport const agentPromptContract = createContract(agentPromptShape)\n\n/** Checks a durable agent job exactly at `AgentRegistry.build`. */\nexport const agentJobContract = createContract(agentJobShape)\n\n/** Validates and projects a page's answers to `ask` frames at a JSON wire boundary. */\nexport const agentAnswerContract = createContract(agentAnswerShape)\n","/**\n * Holds one value a `Channel` buffers and the cell pushed after it.\n *\n * @remarks\n * A `Channel` keeps its unread values as a chain of these cells, from its `#head` cell to its\n * `#tail` cell, so no public signature names this class. The `#next` field is a latch: the\n * `link` method sets it once, and a later call keeps the first cell.\n */\nexport class ChannelCell<T> {\n\treadonly #value: T\n\t#next: ChannelCell<T> | undefined\n\n\t/**\n\t * Holds a value with no next cell.\n\t *\n\t * @param value - The value the cell holds\n\t */\n\tconstructor(value: T) {\n\t\tthis.#value = value\n\t}\n\n\t/**\n\t * Returns the value the cell holds.\n\t *\n\t * @returns The value\n\t */\n\tget value(): T {\n\t\treturn this.#value\n\t}\n\n\t/**\n\t * Returns the cell pushed after this one.\n\t *\n\t * @returns The next cell, or undefined before one is linked\n\t */\n\tget next(): ChannelCell<T> | undefined {\n\t\treturn this.#next\n\t}\n\n\t/**\n\t * Links the cell pushed after this one, once.\n\t *\n\t * @param cell - The cell pushed after this one\n\t */\n\tlink(cell: ChannelCell<T>): void {\n\t\tthis.#next ??= cell\n\t}\n}\n","import type { ChannelInterface } from './types.js'\nimport { ChannelCell } from './ChannelCell.js'\n\n/**\n * Buffers chunks in a minimal unbounded async channel — the eager pump writes them in (`push`)\n * and ends it (`close` / `fail`) regardless of consumption, while a consumer reads them back live\n * through the `drain` async-iterator. Decoupling write from read is what lets a producer make\n * progress without a consumer pulling, and it is why an agent's `result` settles whether or not\n * its `events` are drained.\n *\n * @remarks\n * A waiting `drain` call parks its void resolver in the `#waiting` field. The `push` method links a\n * cell after the `#tail` cell, and the `close` and `fail` methods flag the end; each then fires and\n * clears every parked resolver, so each parked reader wakes, reads the `#head` cell, and either\n * takes the next chunk, returns (on `close`), throws (on `fail`), or parks again when another\n * reader took the chunk first. Taking a chunk reads its value and moves `#head` to the next cell\n * synchronously, so each value reaches exactly one reader, a push and a take each cost constant\n * time, and every taken cell but the newest is released at once; the `#tail` field holds the newest\n * cell until the next push links after it, so a channel retains at most one taken value. A reader\n * suspended at its `yield` expression holds only the value it took, never a cell, so the bound on\n * retained values covers both the channel's fields and a suspended reader.\n * A `return()` issued while a read is parked waits for the next `push`, `close`, or `fail`.\n * The parked read takes a pushed value before that `return()` settles.\n *\n * @example\n * ```ts\n * import { Channel } from '@orkestrel/agent'\n *\n * const channel = new Channel<number>()\n * channel.push(1)\n * channel.close()\n * for await (const value of channel.drain()) {\n * \tvalue // 1\n * }\n * ```\n */\nexport class Channel<T> implements ChannelInterface<T> {\n\t#head: ChannelCell<T> | undefined\n\t#tail: ChannelCell<T> | undefined\n\t#waiting: ReadonlyArray<() => void> = []\n\t#closed = false\n\t#failure: { error: unknown } | undefined\n\n\tpush(value: T): void {\n\t\tconst cell = new ChannelCell(value)\n\t\tthis.#tail?.link(cell)\n\t\tthis.#tail = cell\n\t\tthis.#head ??= cell\n\t\tthis.#signal()\n\t}\n\n\tclose(): void {\n\t\tthis.#closed = true\n\t\tthis.#signal()\n\t}\n\n\tfail(error: unknown): void {\n\t\t// First failure wins; a later close / fail can't override a recorded error.\n\t\tif (this.#failure === undefined) this.#failure = { error }\n\t\tthis.#closed = true\n\t\tthis.#signal()\n\t}\n\n\tasync *drain(): AsyncGenerator<T, void> {\n\t\tfor (;;) {\n\t\t\t// Yield everything buffered before checking for end, so a close / fail that\n\t\t\t// arrives alongside the last chunks still delivers those chunks first.\n\t\t\twhile (this.#head !== undefined) {\n\t\t\t\tconst value = this.#head.value\n\t\t\t\tthis.#head = this.#head.next\n\t\t\t\tyield value\n\t\t\t}\n\t\t\tif (this.#failure !== undefined) throw this.#failure.error\n\t\t\tif (this.#closed) return\n\t\t\tawait this.#park()\n\t\t}\n\t}\n\n\t// Wake every parked reader, not one: with concurrent readers, a single woken reader would\n\t// leave the rest parked past a `close` / `fail`. The list is cleared before firing, so a\n\t// reader that re-parks arms the next wait. A `signal` with no parked reader is a no-op (the\n\t// buffer / flags are already set, so the next `drain` pass reads them without parking).\n\t#signal(): void {\n\t\tconst waiting = this.#waiting\n\t\tthis.#waiting = []\n\t\tfor (const wake of waiting) wake()\n\t}\n\n\t#park(): Promise<void> {\n\t\treturn new Promise<void>((resolve) => {\n\t\t\tthis.#waiting = [...this.#waiting, resolve]\n\t\t})\n\t}\n}\n","import type { AuthorityDecision, AuthorityVerdict } from './types.js'\nimport { isString, objectOf } from '@orkestrel/contract'\n\n/**\n * Checks whether a value is an authority verdict: `allow`, `ask`, or `deny`.\n *\n * @param value - The unknown verdict candidate\n * @returns True if the value names a verdict; false otherwise\n *\n * @example\n * ```ts\n * isAuthorityVerdict('ask') // true\n * isAuthorityVerdict('maybe') // false\n * ```\n */\nexport function isAuthorityVerdict(value: unknown): value is AuthorityVerdict {\n\treturn value === 'allow' || value === 'ask' || value === 'deny'\n}\n\n/**\n * Checks whether a value is an authority decision: an object whose `zone` is a string, whose\n * `verdict` is `allow`, `ask`, or `deny`, and whose `reason` is absent or a string.\n *\n * @remarks\n * Total: each member is read once, unknown members are admitted, and a non-object, a 0.0.34\n * `{ zone, allowed }` decision, an unknown verdict, a throwing getter, and a revoked or throwing\n * `Proxy` return false rather than throw.\n *\n * @param value - The unknown decision candidate\n * @returns True if the value satisfies the decision contract; false otherwise\n *\n * @example\n * ```ts\n * isAuthorityDecision({ zone: 'default', verdict: 'ask', reason: 'confirm' }) // true\n * isAuthorityDecision({ zone: 'default', allowed: true }) // false\n * ```\n */\nexport function isAuthorityDecision(value: unknown): value is AuthorityDecision {\n\treturn objectOf({ zone: isString, verdict: isAuthorityVerdict, reason: isString }, ['reason'])(\n\t\tvalue,\n\t)\n}\n","import type { Message } from '../types.js'\nimport type { ProviderRequest } from '../providers/index.js'\nimport type { ConversationInterface } from '../conversations/index.js'\nimport type {\n\tAgentChunk,\n\tAgentFrame,\n\tAgentInterface,\n\tAgentJobInput,\n\tAgentRegistryInterface,\n\tAgentResult,\n\tAgentRunResult,\n\tAgentStop,\n\tAgentStreamInterface,\n\tAgentTurn,\n\tApproverInterface,\n\tAuthorityDecision,\n\tAuthorityOptions,\n\tAuthorityRule,\n\tMessageMeasure,\n\tRequestMeasure,\n} from './types.js'\nimport type { BudgetInterface, TokenUsage } from '@orkestrel/budget'\nimport type { AuditFault, JSONValue } from '@orkestrel/contract'\nimport type { QueueContext, QueueOptions } from '@orkestrel/queue'\nimport type { ToolCall, ToolDefinition, ToolResult } from '@orkestrel/tool'\nimport { isBudget, isBudgetAmount, isBudgetError } from '@orkestrel/budget'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tcanonicalStringify,\n\tContractError,\n\tcreateContract,\n\tisBoolean,\n\tisFiniteNumber,\n\tisObject,\n\tisString,\n\tobjectOf,\n\tparseJSON,\n\tparseJSONValue,\n\tpreview,\n\tschemaToShape,\n} from '@orkestrel/contract'\nimport { AUTHORITY_NOTES, IMAGE_TOKEN_ESTIMATE, MESSAGE_TOKEN_OVERHEAD } from './constants.js'\nimport { AgentError, AgentJobError } from './errors.js'\nimport { isConversation } from '../conversations/index.js'\nimport { isAuthorityDecision, isAuthorityVerdict } from './validators.js'\nimport { estimateTokens, hasCalls, renderJSON } from '../helpers.js'\nimport { isPositiveSafeInteger } from '../validators.js'\n\n/**\n * Projects an unknown value onto a fresh, exact `JSONValue` representation of an\n * {@link AgentResult} — capturing each structural field once through a total boundary,\n * accepting conforming accessors and inherited properties, preserving fractional usage counts,\n * dropping extras, and resolving `undefined` for a malformed field, a negative or non-finite usage\n * number, a throwing getter, or a malformed or revoked proxy.\n *\n * @remarks\n * This is a total projection that reads untrusted input. Each structural field is captured once\n * through Contract's sanctioned exception boundary, so conforming accessors and inherited\n * properties are supported while a throwing getter or revoked proxy returns `undefined`.\n * Present usage counts must be finite and nonnegative; fractional values are preserved, not\n * normalized. The result is exact `{ content, thinking?, usage?, partial, stop? }`. A present\n * `stop` is read once, then its `layer`, `id`, `max`, `consumed`, `estimate`, and `usage` once each\n * in that order, `max` and `consumed` only on a `meter` or `budget` arm; the arm is rebuilt\n * exactly, omitting absent readings, and a malformed `stop` refuses the whole value. Extra input\n * properties are dropped while a fresh exact plain object is rebuilt and deep-gated through\n * {@link import('@orkestrel/contract').parseJSONValue}.\n *\n * @param value - The unknown value to project\n * @returns A fresh JSON value containing only AgentResult fields, or `undefined` when invalid\n *\n * @example\n * ```ts\n * import { agentResultToJSON } from '@orkestrel/agent'\n *\n * agentResultToJSON({ content: 'done', usage: { prompt: 2, completion: 1, total: 3 }, partial: false })\n * // { content: 'done', usage: { prompt: 2, completion: 1, total: 3 }, partial: false }\n * ```\n */\nexport function agentResultToJSON(value: unknown): JSONValue | undefined {\n\tconst captured = attempt(() => {\n\t\tif (!isObject(value)) return undefined\n\n\t\tconst content = Reflect.get(value, 'content')\n\t\tconst thinking = Reflect.get(value, 'thinking')\n\t\tconst usage = Reflect.get(value, 'usage')\n\t\tconst partial = Reflect.get(value, 'partial')\n\t\tconst stop = Reflect.get(value, 'stop')\n\t\tif (!isString(content) || !isBoolean(partial)) return undefined\n\t\tif (thinking !== undefined && !isString(thinking)) return undefined\n\t\tconst projectedUsage = usage === undefined ? undefined : tokenUsageToJSON(usage)\n\t\tif (usage !== undefined && projectedUsage === undefined) return undefined\n\t\tif (stop !== undefined && !isObject(stop)) return undefined\n\n\t\tconst layer = isObject(stop) ? Reflect.get(stop, 'layer') : undefined\n\t\tconst id = isObject(stop) ? Reflect.get(stop, 'id') : undefined\n\t\tconst ranked = layer === 'meter' || layer === 'budget'\n\t\tconst max = isObject(stop) && ranked ? Reflect.get(stop, 'max') : undefined\n\t\tconst consumed = isObject(stop) && ranked ? Reflect.get(stop, 'consumed') : undefined\n\t\tconst estimate = isObject(stop) ? Reflect.get(stop, 'estimate') : undefined\n\t\tconst spent = isObject(stop) ? Reflect.get(stop, 'usage') : undefined\n\t\tconst projectedSpent = spent === undefined ? undefined : tokenUsageToJSON(spent)\n\t\tconst measured = {\n\t\t\t...(isFiniteNumber(estimate) ? { estimate } : {}),\n\t\t\t...(projectedSpent === undefined ? {} : { usage: projectedSpent }),\n\t\t}\n\t\tconst projectedStop =\n\t\t\t!isString(id) ||\n\t\t\t(estimate !== undefined && !isFiniteNumber(estimate)) ||\n\t\t\t(spent !== undefined && projectedSpent === undefined)\n\t\t\t\t? undefined\n\t\t\t\t: ranked && isFiniteNumber(max) && isFiniteNumber(consumed)\n\t\t\t\t\t? { layer, id, max, consumed, ...measured }\n\t\t\t\t\t: layer === 'model'\n\t\t\t\t\t\t? { layer, id, ...measured }\n\t\t\t\t\t\t: undefined\n\t\tif (stop !== undefined && projectedStop === undefined) return undefined\n\n\t\treturn {\n\t\t\tcontent,\n\t\t\t...(thinking === undefined ? {} : { thinking }),\n\t\t\t...(projectedUsage === undefined ? {} : { usage: projectedUsage }),\n\t\t\tpartial,\n\t\t\t...(projectedStop === undefined ? {} : { stop: projectedStop }),\n\t\t}\n\t})\n\n\treturn captured.success ? parseJSONValue(captured.value) : undefined\n}\n\n/**\n * Projects an unknown value onto a fresh, exact `TokenUsage` — capturing `prompt`, `completion`, and\n * `total` once each through a total boundary, preserving fractional counts, dropping extras, and\n * resolving `undefined` for a non-object, a missing, negative, or non-finite count, a throwing getter,\n * or a malformed or revoked proxy.\n *\n * @remarks\n * The capture runs inside Contract's sanctioned exception boundary, so conforming accessors and\n * inherited properties are read while a throwing getter or a revoked proxy returns `undefined`.\n * Counts are not normalized. A caller that treats an absent usage as valid checks for `undefined`\n * before it projects, because this function refuses `undefined` like any other non-object.\n *\n * @param value - The unknown value to project\n * @returns A fresh `{ prompt, completion, total }`, or `undefined` when the value is not a finite nonnegative usage triple\n *\n * @example\n * ```ts\n * import { tokenUsageToJSON } from '@orkestrel/agent'\n *\n * tokenUsageToJSON({ prompt: 2, completion: 1, total: 1.5, cached: 4 }) // { prompt: 2, completion: 1, total: 1.5 }\n * tokenUsageToJSON({ prompt: 2, completion: -1, total: 1 }) // undefined\n * tokenUsageToJSON({ prompt: 2, completion: Infinity, total: 3 }) // undefined\n * ```\n */\nexport function tokenUsageToJSON(value: unknown): TokenUsage | undefined {\n\tconst captured = attempt(() => {\n\t\tif (!isObject(value)) return undefined\n\t\tconst prompt = Reflect.get(value, 'prompt')\n\t\tconst completion = Reflect.get(value, 'completion')\n\t\tconst total = Reflect.get(value, 'total')\n\t\treturn isBudgetAmount(prompt) && isBudgetAmount(completion) && isBudgetAmount(total)\n\t\t\t? { prompt, completion, total }\n\t\t\t: undefined\n\t})\n\treturn captured.success ? captured.value : undefined\n}\n\n/**\n * Projects one {@link AgentChunk} onto the {@link AgentFrame} a page receives, member by member, or\n * returns `undefined` for an `ask` chunk.\n *\n * @remarks\n * A `tool` chunk projects to `{ category: 'tool', success: true, name }` only when its result\n * succeeded, and to `{ category: 'tool', success: false }` otherwise, so neither the tool's value nor\n * a denial's text crosses. An `ask` chunk returns `undefined`, because only the approver holding the\n * ask knows its id: read its frame through the `frame` method of an {@link ApproverInterface}.\n *\n * @param chunk - The chunk the run yielded\n * @returns The chunk's frame, or `undefined` for an `ask` chunk\n *\n * @example\n * ```ts\n * agentChunkToFrame({ category: 'token', content: 'Hi' }) // { category: 'token', content: 'Hi' }\n * ```\n */\nexport function agentChunkToFrame(chunk: AgentChunk): AgentFrame | undefined {\n\tswitch (chunk.category) {\n\t\tcase 'token':\n\t\t\treturn { category: 'token', content: chunk.content }\n\t\tcase 'think':\n\t\t\treturn { category: 'think', content: chunk.content }\n\t\tcase 'tool':\n\t\t\treturn chunk.result.success === true\n\t\t\t\t? { category: 'tool', success: true, name: chunk.call.name }\n\t\t\t\t: { category: 'tool', success: false }\n\t\tcase 'usage':\n\t\t\treturn {\n\t\t\t\tcategory: 'usage',\n\t\t\t\tusage: {\n\t\t\t\t\tprompt: chunk.usage.prompt,\n\t\t\t\t\tcompletion: chunk.usage.completion,\n\t\t\t\t\ttotal: chunk.usage.total,\n\t\t\t\t},\n\t\t\t\testimate: chunk.estimate,\n\t\t\t}\n\t\tcase 'ask':\n\t\t\treturn undefined\n\t}\n}\n\n/**\n * Projects a settled {@link AgentResult} onto the terminal `finish` frame a page receives.\n *\n * @remarks\n * Keeps `content`, `partial`, `usage` when present, and the stop's `layer` when a layer stopped the\n * run; drops `thinking` and every other stop member.\n *\n * @param result - The run's settled result\n * @returns A fresh `finish` frame\n *\n * @example\n * ```ts\n * agentResultToFrame({ content: 'Done.', partial: false }) // { category: 'finish', content: 'Done.', partial: false }\n * ```\n */\nexport function agentResultToFrame(\n\tresult: AgentResult,\n): Extract<AgentFrame, { readonly category: 'finish' }> {\n\treturn {\n\t\tcategory: 'finish',\n\t\tcontent: result.content,\n\t\tpartial: result.partial,\n\t\t...(result.usage === undefined\n\t\t\t? {}\n\t\t\t: {\n\t\t\t\t\tusage: {\n\t\t\t\t\t\tprompt: result.usage.prompt,\n\t\t\t\t\t\tcompletion: result.usage.completion,\n\t\t\t\t\t\ttotal: result.usage.total,\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t...(result.stop === undefined ? {} : { layer: result.stop.layer }),\n\t}\n}\n\n/**\n * Projects a run's live handle onto the frames a page receives — one frame per projected chunk, in\n * order, then exactly one terminal `finish` or `error`, last.\n *\n * @remarks\n * Reads `events` and `result` once each. An `ask` chunk crosses as the frame `approver` holds for\n * its call, and every other chunk through {@link agentChunkToFrame}; after the drain ends, the\n * settled result crosses through {@link agentResultToFrame}, and any throw ends the frames with a\n * bare `{ category: 'error' }`. It never calls `abort` itself, and drained to the end it yields\n * exactly one terminal frame, last. A consumer's `return()` while it waits at a chunk's frame\n * closes the drain of `events`, whose `return()` aborts the run, and no terminal frame is yielded.\n * When the run aborts while an ask waits and the reader pulls the `ask` chunk afterwards, the\n * approver holds no waiting ask, marks the call, and frames nothing, so no `ask` frame crosses and\n * the frames end `finish` with `partial: true`.\n *\n * @param stream - The run's live handle\n * @param approver - The approver wired as the authority's `ask` handler; with `undefined` every\n * `ask` chunk is dropped, so pass `undefined` only when an in-process handler answers the asks.\n * An `Approver` wired as the handler but passed as `undefined` leaves each ask waiting with no\n * frame until the run `timeout` or an abort.\n * @returns An async generator of the run's frames\n *\n * @example\n * ```ts\n * const approver = createApprover()\n * const authority = createAuthority({\n * \trules,\n * \tfallback: { zone: 'default', verdict: 'deny' },\n * \task: approver.ask,\n * })\n * const agent = createAgent(provider, { tools, authority })\n * for await (const frame of agentStreamToFrames(agent.stream({ prompt }), approver)) {\n * \tsend(JSON.stringify(frame))\n * }\n * ```\n */\nexport async function* agentStreamToFrames(\n\tstream: AgentStreamInterface,\n\tapprover: ApproverInterface | undefined,\n): AsyncGenerator<AgentFrame, void, undefined> {\n\tconst events = stream.events\n\tconst result = stream.result\n\ttry {\n\t\tfor await (const chunk of events) {\n\t\t\tconst frame =\n\t\t\t\tchunk.category === 'ask' ? approver?.frame(chunk.call) : agentChunkToFrame(chunk)\n\t\t\tif (frame !== undefined) yield frame\n\t\t}\n\t\tyield agentResultToFrame(await result)\n\t} catch {\n\t\tyield { category: 'error' }\n\t}\n}\n\n/**\n * Infers which of a run's three failure layers stopped it — the meter allowance, a cost budget, or\n * the model's own token limit — and returns the stop record with the final call's readings, or\n * `undefined` when no layer stopped the run.\n *\n * @remarks\n * The layer that stopped the final provider call first wins. A model-truncated final answer whose\n * call returned unaborted is a `model` stop, even when that call's reconciled usage then exhausted a\n * budget or a cancel then landed before the run settled. Otherwise a `BudgetError` reason is a\n * `meter` stop when the allowance is exhausted, the reason is the allowance signal's own reason, its\n * context carries the allowance's `id` and `max`, and either the allowance is a separate instance or\n * the run had aborted when the call returned; any other `BudgetError` is a `budget` stop. The\n * identity test tells an allowance and a budget that share an `id` apart, and the readings test\n * tells a parent budget linked through the allowance's `signal` option apart from the allowance. An\n * external cancel, a deadline, or an `abort()` reason yields `undefined`. Two inputs still read as\n * the meter's: a charge a caller makes to the run's shared allowance from its own code while a call\n * streams, and a parent with the allowance's own `id` and `max` that aborted before `start()` when\n * the allowance then exhausts.\n *\n * @param reason - The run signal's abort reason (`undefined` while it is unaborted)\n * @param allowance - The run's meter allowance, or `undefined` for a run with no meter layer\n * @param budget - The run's cost budget, or `undefined` for an unbudgeted run\n * @param provider - The provider's trace `id`, named by a `model` stop\n * @param turn - The run's final turn record, or `undefined` when the run took no turn\n * @returns The stop record, or `undefined` when no layer stopped the run\n *\n * @example\n * ```ts\n * const allowance = createTokenBudget({ id: 'meter', max: 2 })\n * allowance.start()\n * allowance.consume({ prompt: 0, completion: 3, total: 3 })\n * inferStop(allowance.signal.reason, allowance, undefined, 'ollama', undefined)\n * // { layer: 'meter', id: 'meter', max: 2, consumed: 3 }\n * ```\n */\nexport function inferStop(\n\treason: unknown,\n\tallowance: BudgetInterface<TokenUsage> | undefined,\n\tbudget: BudgetInterface<TokenUsage> | undefined,\n\tprovider: string,\n\tturn: AgentTurn | undefined,\n): AgentStop | undefined {\n\tconst measured = {\n\t\t...(turn?.estimate === undefined ? {} : { estimate: turn.estimate }),\n\t\t...(turn?.usage === undefined ? {} : { usage: turn.usage }),\n\t}\n\tconst interrupted = turn?.interrupted === true\n\tif (!interrupted && turn?.truncated === true) return { layer: 'model', id: provider, ...measured }\n\tif (!isBudgetError(reason)) return undefined\n\tconst metered =\n\t\tallowance !== undefined &&\n\t\tallowance.exhausted &&\n\t\treason === allowance.signal.reason &&\n\t\treason.context.id === allowance.id &&\n\t\treason.context.max === allowance.max &&\n\t\t(allowance !== budget || interrupted)\n\treturn {\n\t\tlayer: metered ? 'meter' : 'budget',\n\t\tid: reason.context.id,\n\t\tmax: reason.context.max,\n\t\tconsumed: reason.context.consumed,\n\t\t...measured,\n\t}\n}\n\n/**\n * Estimates the context-token footprint of a batch of messages — each message's content plus\n * {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD}, a tool-call JSON estimate, a thinking estimate, and\n * {@link import('./constants.js').IMAGE_TOKEN_ESTIMATE} for each attached image. The {@link estimateRequest} message term\n * for an agent's context budget (the\n * {@link import('./types.js').AgentOptions} `window`), a provider-independent approximation for conforming messages rather than an exact tokenizer count.\n *\n * @remarks\n * Sums, per message, {@link estimateTokens} over its `content` (the `ceil(length / 4)` character\n * heuristic) plus {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD} (a fixed per-message\n * role/framing overhead) plus, when present, {@link estimateTokens} over its JSON-stringified\n * `calls` plus, when present, {@link estimateTokens} over its `thinking` plus `images.length * `{@link import('./constants.js').IMAGE_TOKEN_ESTIMATE} (a coarse,\n * deliberately-approximate per-image cost — a base64 length is not a token proxy). Deterministic\n * and provider-free — the same messages always yield the same estimate, with an empty batch `0`.\n * It is the message term of the request estimate an agent's auto-compaction context budget\n * measures; a caller wanting a sharper count supplies its own `consumer` to `createBudget`\n * instead. A `calls` list JSON cannot carry (a circular or `BigInt`\n * `ToolCall.arguments`) contributes a fixed\n * {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD} (the same per-message overhead scale)\n * instead of an estimate of the (unreachable) serialized length.\n *\n * @param messages - The messages to estimate (a turn's appended assistant + tool messages)\n * @returns The summed estimated token count (`0` when empty)\n *\n * @example\n * ```ts\n * estimateMessages([]) // 0\n * estimateMessages([{ id: '1', role: 'user', content: 'hello' }]) // 6 (2 content + 4 overhead)\n * ```\n */\nexport function estimateMessages(messages: readonly Message[]): number {\n\treturn messages.reduce((sum, message) => {\n\t\tconst measure = measureMessage(message)\n\t\treturn (\n\t\t\tsum + measure.content + measure.uniform + measure.calls + measure.thinking + measure.images\n\t\t)\n\t}, 0)\n}\n\n/**\n * Measures the disjoint estimate components of one message — its content, framing, rendered calls,\n * thinking, and attached images — as the same heuristic units {@link estimateMessages} sums.\n *\n * @remarks\n * The result is a structural estimate, not a tokenizer allocation. `id` and `role` are joins back to\n * the message, not token components, and a provider's template effects and the estimation error are\n * not individually attributed. `content` is `ceil(length / 4)`, `uniform` is\n * {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD}, `calls` is the estimate of the JSON-rendered\n * calls (`0` when the message carries none, the framing overhead when they cannot be rendered),\n * `thinking` is the estimate of the thinking (`0` when absent), and `images` is the attached image\n * count times {@link import('./constants.js').IMAGE_TOKEN_ESTIMATE}. The message is not mutated.\n *\n * @param message - The message to measure\n * @returns The message's identity and its estimate components\n *\n * @example\n * ```ts\n * measureMessage({ id: '1', role: 'user', content: 'hello' })\n * // { id: '1', role: 'user', content: 2, uniform: 4, calls: 0, thinking: 0, images: 0 }\n * ```\n */\nexport function measureMessage(message: Message): MessageMeasure {\n\tconst rendered = hasCalls(message) ? renderJSON(message.calls) : ''\n\tconst calls = rendered === undefined ? MESSAGE_TOKEN_OVERHEAD : estimateTokens(rendered)\n\treturn {\n\t\tid: message.id,\n\t\trole: message.role,\n\t\tcontent: estimateTokens(message.content),\n\t\tuniform: MESSAGE_TOKEN_OVERHEAD,\n\t\tcalls,\n\t\tthinking: message.thinking === undefined ? 0 : estimateTokens(message.thinking),\n\t\timages: (message.images?.length ?? 0) * IMAGE_TOKEN_ESTIMATE,\n\t}\n}\n\n/**\n * Estimates the context-token footprint of a complete provider request — its messages through\n * {@link estimateMessages}, plus the rendered advertised `tools`, plus the rendered\n * `options.schema`. The `consumer` estimator an agent's `window` budget measures every request\n * through, a provider-independent heuristic rather than an exact tokenizer count.\n *\n * @remarks\n * Adds {@link estimateTokens} over the JSON rendering of `tools` and of `options.schema` to the\n * message estimate. An absent or empty `tools` list and an absent or empty `schema` add `0`, so a\n * request that advertises neither equals {@link estimateMessages} of its messages. A value JSON\n * cannot carry contributes a fixed {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD}. The\n * estimate counts JSON characters, not a provider's template: the chat template's own tokens, how\n * a provider encodes tool definitions, and the `think` flag are not counted, and no tokenizer\n * runs. A model's real prompt count can exceed it, so choose the window `max` with headroom\n * measured against the provider.\n *\n * @param request - The {@link ProviderRequest} to estimate: messages, advertised tools, call options\n * @returns The estimated token count (`0` for an empty request)\n *\n * @example\n * ```ts\n * estimateRequest({ messages: [{ id: '1', role: 'user', content: 'hello' }] }) // 6, equal to estimateMessages\n * ```\n */\nexport function estimateRequest(request: ProviderRequest): number {\n\tconst measure = measureRequest(request)\n\treturn measure.messages.reduce(\n\t\t(sum, message) =>\n\t\t\tsum + message.content + message.uniform + message.calls + message.thinking + message.images,\n\t\tmeasure.tools + measure.schema,\n\t)\n}\n\n/**\n * Measures the disjoint estimate components of a complete provider request — each message through\n * {@link measureMessage}, plus the rendered advertised `tools` and `options.schema` — as the same\n * heuristic units {@link estimateRequest} sums.\n *\n * @remarks\n * The result is a structural estimate, not a tokenizer allocation; a provider's template effects, how\n * it encodes tool definitions, the `think` flag, and the estimation error are not individually\n * attributed. `messages` keeps the request's message order, duplicate ids included. `tools` and\n * `schema` are each `0` when absent or empty and the estimate of their JSON rendering otherwise, or\n * the {@link import('./constants.js').MESSAGE_TOKEN_OVERHEAD} when JSON cannot carry them. The request\n * is not mutated.\n *\n * @param request - The {@link ProviderRequest} to measure: messages, advertised tools, call options\n * @returns The per-message components and the tools and schema estimates\n *\n * @example\n * ```ts\n * measureRequest({ messages: [{ id: '1', role: 'user', content: 'hello' }] })\n * // { messages: [{ id: '1', role: 'user', content: 2, uniform: 4, calls: 0, thinking: 0, images: 0 }], tools: 0, schema: 0 }\n * ```\n */\nexport function measureRequest(request: ProviderRequest): RequestMeasure {\n\tconst tools = request.tools !== undefined && request.tools.length > 0 ? request.tools : undefined\n\tconst schema = request.options?.schema\n\tconst [toolsEstimate = 0, schemaEstimate = 0] = [\n\t\ttools,\n\t\tschema !== undefined && Object.keys(schema).length > 0 ? schema : undefined,\n\t].map((part) => {\n\t\tif (part === undefined) return 0\n\t\tconst rendered = renderJSON(part)\n\t\treturn rendered === undefined ? MESSAGE_TOKEN_OVERHEAD : estimateTokens(rendered)\n\t})\n\treturn {\n\t\tmessages: request.messages.map((message) => measureMessage(message)),\n\t\ttools: toolsEstimate,\n\t\tschema: schemaEstimate,\n\t}\n}\n\n/**\n * Runs one rehydrated agent and applies the partial-as-configurable-failure policy — a partial\n * run throws an {@link import('./errors.js').AgentJobError} unless the `partial` policy allows\n * it, and a natural finish resolves. The shared job-handler step `createAgentQueue` and\n * `createAgentRunner` both settle each job through, so the policy can never diverge between\n * them.\n *\n * @remarks\n * A turn that committed partial (a cancel — abort / budget / timeout) is by default a\n * failure, so it throws an {@link import('./errors.js').AgentJobError} carrying the partial\n * (the Queue's retries + a Runner's fail-fast then engage); the `partial` policy resolves\n * it as success instead. A natural finish always resolves with its result.\n *\n * @param agent - The rehydrated {@link AgentInterface} to run to its {@link AgentResult}\n * @param partial - The partial policy. If `true`, a partial result resolves as success; if\n * `false` (the default policy), a partial result throws an {@link AgentJobError}\n * @returns The agent's {@link AgentResult} (a natural finish, or a partial one under the\n * `partial` policy)\n * @throws {AgentJobError} Thrown when the run ended partial and the `partial` policy is `false`\n *\n * @example\n * ```ts\n * const result = await settleAgentJob(registry.build(input, signal), false)\n * ```\n */\nexport async function settleAgentJob(\n\tagent: AgentInterface,\n\tpartial: boolean,\n): Promise<AgentResult> {\n\tconst result = await agent.generate()\n\tif (result.partial && !partial) throw new AgentJobError('agent job ended partial', result)\n\treturn result\n}\n\n/**\n * Handles one queued agent job by rehydrating it through a registry with the queue\n * attempt's signal, then applying the shared partial-result policy.\n *\n * @param registry - The registry that rehydrates the serializable job\n * @param partial - The partial policy. If `true`, a partial result resolves; if `false`, it throws\n * @param input - The serializable agent job\n * @param context - The queue attempt whose signal bounds the agent\n * @returns The settled agent result\n *\n * @example\n * ```ts\n * const result = await handleAgentQueueJob(registry, false, input, context)\n * ```\n */\nexport function handleAgentQueueJob(\n\tregistry: AgentRegistryInterface,\n\tpartial: boolean,\n\tinput: AgentJobInput,\n\tcontext: QueueContext,\n): Promise<AgentResult> {\n\treturn settleAgentJob(registry.build(input, context.signal), partial)\n}\n\n/**\n * Assembles the settled {@link AgentResult} from a run's {@link AgentRunResult} — `thinking`,\n * `usage`, and `stop` are carried only when the run surfaced them, and the loop-internal\n * `exhausted` flag is left out.\n *\n * @remarks\n * Pure and total. An absent optional is omitted rather than stored as `undefined` (the\n * present-when-given convention the message store follows), so a settled result JSON\n * round-trips without an explicit `undefined` field. The keys follow `content`, `partial`,\n * `thinking`, `usage`, then `stop`. `exhausted` is loop bookkeeping and does not reach the public\n * result — the `exhaust` event carries it instead.\n *\n * @param outcome - The run's settled outcome\n * @returns The public {@link AgentResult}\n *\n * @example\n * ```ts\n * assembleResult({ content: 'hi', thinking: undefined, usage: undefined, partial: false, exhausted: false, stop: undefined })\n * // { content: 'hi', partial: false }\n * ```\n */\nexport function assembleResult(outcome: AgentRunResult): AgentResult {\n\treturn {\n\t\tcontent: outcome.content,\n\t\tpartial: outcome.partial,\n\t\t...(outcome.thinking === undefined ? {} : { thinking: outcome.thinking }),\n\t\t...(outcome.usage === undefined ? {} : { usage: outcome.usage }),\n\t\t...(outcome.stop === undefined ? {} : { stop: outcome.stop }),\n\t}\n}\n\n/**\n * Synthesizes the denial {@link ToolResult} a blocked call is fed back with — the call's `id` /\n * `name` keyed back, carrying a denial `error` instead of a value.\n *\n * @remarks\n * Pure and total. The `error` is `<name> did not run: <reason>`, where the model-supplied name\n * is JSON-encoded and capped at `PREVIEW_LIMIT` characters by\n * {@link import('@orkestrel/contract').preview}, and an absent `reason` reads the `denied` note\n * of {@link AUTHORITY_NOTES}. There is no `value`, so the agent loop feeds it back exactly like\n * a tool error and the model can react to it.\n *\n * @param call - The denied {@link ToolCall}\n * @param reason - A rule's `deny` reason or a note, or `undefined` for the `denied` note\n * @returns The failure-arm {@link ToolResult}\n *\n * @example\n * ```ts\n * denyCall({ id: '1', name: 'drop', arguments: {} }, 'read-only mode')\n * // { success: false, id: '1', name: 'drop', error: '\"drop\" did not run: read-only mode' }\n * ```\n */\nexport function denyCall(call: ToolCall, reason: string | undefined): ToolResult {\n\treturn {\n\t\tsuccess: false,\n\t\tid: call.id,\n\t\tname: call.name,\n\t\terror: `${preview(call.name)} did not run: ${reason ?? AUTHORITY_NOTES.denied}`,\n\t}\n}\n\n/**\n * Renders a tool call's arguments as escaped canonical JSON text — sorted keys, with every control,\n * format, line-separator, and paragraph-separator code point written as `\\uXXXX` — or returns\n * `undefined` when they cannot be rendered.\n *\n * @remarks\n * Keeps the semantics of `canonicalStringify` from `@orkestrel/contract`, so `NaN` renders as `null`\n * and an `undefined` member is dropped, as `JSON.stringify` does; the exact round trip through\n * `JSON.parse` is promised for JSON arguments only, which a provider's parsed call carries. A cycle,\n * a throwing getter, or a value JSON cannot encode returns `undefined`. Each escaped code point is\n * written over its UTF-16 code units, so bidi overrides, zero-width characters, U+0085, U+2028,\n * U+2029, U+FEFF, and tag characters appear only encoded. The text carries no length cap.\n *\n * @param value - The call's arguments\n * @returns The rendered text, or `undefined` when the arguments cannot be rendered\n *\n * @example\n * ```ts\n * renderArguments({ path: 'a\\u202Eb', mode: 'r' }) // '{\"mode\":\"r\",\"path\":\"a\\\\u202eb\"}'\n * ```\n */\nexport function renderArguments(value: Readonly<Record<string, unknown>>): string | undefined {\n\tconst rendered = attempt(() => canonicalStringify(value))\n\tif (!rendered.success || rendered.value === undefined) return undefined\n\treturn rendered.value.replace(/[\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}]/gu, (match) =>\n\t\tArray.from(\n\t\t\t{ length: match.length },\n\t\t\t(_, index) => `\\\\u${match.charCodeAt(index).toString(16).padStart(4, '0')}`,\n\t\t).join(''),\n\t)\n}\n\n/**\n * Applies the `consequential` floor to one {@link AuthorityDecision} — an `allow` for a tool\n * whose advertised definition is annotated `consequential` becomes an `ask`.\n *\n * @remarks\n * Pure and total, and monotone: it raises `allow` to `ask` and never loosens a verdict, so an\n * `ask` and a `deny` return with the same verdict. The result is always a fresh decision.\n * An unannotated or absent definition leaves the verdict unchanged.\n *\n * @param decision - The authority's decision on one call\n * @param definition - The advertised definition named by the call, or `undefined` when none is\n * @returns A fresh decision carrying the raised or unchanged verdict\n *\n * @example\n * ```ts\n * const definition = { name: 'write', description: 'Write a file', annotations: { consequential: true } }\n * resolveDecision({ zone: 'default', verdict: 'allow' }, definition) // { zone: 'default', verdict: 'ask' }\n * ```\n */\nexport function resolveDecision(\n\tdecision: AuthorityDecision,\n\tdefinition: ToolDefinition | undefined,\n): AuthorityDecision {\n\tconst raised = decision.verdict === 'allow' && definition?.annotations?.consequential === true\n\treturn { ...decision, verdict: raised ? 'ask' : decision.verdict }\n}\n\n/**\n * Checks a run's final answer against its schema: the content must parse as JSON and satisfy the\n * schema, or the call throws a coded error carrying the faults.\n *\n * @remarks\n * The schema enters through the lenient `schemaToShape`, which widens every keyword it cannot express\n * and never narrows, because a provider constrains its own output and a false refusal would discard a\n * good answer. The check is `is`, because `explain` alone passes what `parse` would repair. Each call\n * compiles once, and a run has one final answer.\n *\n * @param content - The final answer's text\n * @param schema - The run's JSON Schema\n * @throws {AgentError} Thrown when the content is not JSON, misses the schema, or the schema does not compile (code `'SCHEMA'`), with the faults under `context.faults` for the first two\n *\n * @example\n * ```ts\n * requireAnswer('{\"a\":5}', { type: 'object', properties: { a: { type: 'number' } } })\n * requireAnswer('nope', { type: 'object' }) // throws AgentError ('SCHEMA')\n * ```\n */\nexport function requireAnswer(content: string, schema: Readonly<Record<string, unknown>>): void {\n\tconst compiled = attempt(() => createContract(schemaToShape(schema)))\n\tif (!compiled.success) {\n\t\tthrow new AgentError(\n\t\t\t'SCHEMA',\n\t\t\t'the run schema does not compile; pass a JSON Schema built from plain values',\n\t\t\t{ cause: compiled.error },\n\t\t)\n\t}\n\tconst parsed = parseJSON(content)\n\tconst faults: readonly AuditFault[] =\n\t\tparsed === undefined\n\t\t\t? [\n\t\t\t\t\t{\n\t\t\t\t\t\treason: 'type',\n\t\t\t\t\t\tpath: [],\n\t\t\t\t\t\texpected: 'json',\n\t\t\t\t\t\treceived: preview(content),\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t: compiled.value.is(parsed)\n\t\t\t\t? []\n\t\t\t\t: compiled.value.audit(parsed)\n\tif (faults.length > 0) {\n\t\tthrow new AgentError(\n\t\t\t'SCHEMA',\n\t\t\t`the final answer does not satisfy the run schema; pass strict: false to createAgent to take the raw content from a provider that ignores schemas\\n${describeFaults(faults, compiled.value.schema)}`,\n\t\t\t{ context: { faults } },\n\t\t)\n\t}\n}\n\n/**\n * Returns an authority's no-match decision after checking the options carry one, or throws a coded\n * error naming the two fallbacks a host chooses between.\n *\n * @remarks\n * A rules list says nothing about the calls it does not match, so no fallback is assumed: a denylist\n * and an allowlist differ only here. The check is {@link isAuthorityDecision}, a total guard, so a\n * missing options object and a malformed fallback refuse alike.\n *\n * @param options - The authority's options as a host passed them\n * @returns The fallback, narrowed to an {@link AuthorityDecision}\n * @throws {AgentError} Thrown when the options carry no valid `fallback` (code `'AUTHORITY'`)\n *\n * @example\n * ```ts\n * requireFallback({ fallback: { zone: 'default', verdict: 'deny' } }) // { zone: 'default', verdict: 'deny' }\n * ```\n */\nexport function requireFallback(options: AuthorityOptions): AuthorityDecision {\n\tconst fallback = attempt(() => (isObject(options) ? Reflect.get(options, 'fallback') : undefined))\n\tif (fallback.success && isAuthorityDecision(fallback.value)) return fallback.value\n\tthrow new AgentError(\n\t\t'AUTHORITY',\n\t\t\"an authority needs a fallback for the calls no rule matches; pass fallback: { zone: 'default', verdict: 'deny' } for an allowlist, or fallback: { zone: 'default', verdict: 'allow' } for a denylist\",\n\t)\n}\n\n/**\n * Returns an authority's rules after checking each one names a verdict, or throws a coded error naming\n * the first rule that does not.\n *\n * @remarks\n * Absent rules return an empty list. A rule's verdict decides a tool call, so a rule with none is\n * refused rather than read as an allow. The error's `context` carries the rule's index as `rule`.\n *\n * @param rules - The ordered rules, or `undefined` for none\n * @returns The rules unchanged, or an empty list when none were supplied\n * @throws {AgentError} Thrown when a rule has no valid `verdict` (code `'AUTHORITY'`)\n *\n * @example\n * ```ts\n * requireRules([{ match: () => true, zone: 'files', verdict: 'ask' }]).length // 1\n * ```\n */\nexport function requireRules(\n\trules: readonly AuthorityRule[] | undefined,\n): readonly AuthorityRule[] {\n\tif (rules === undefined) return []\n\t// `Array.from` visits a hole as an undefined rule, which the guard refuses; `findIndex` reads it too.\n\tconst refused = Array.from(rules).findIndex(\n\t\t(rule) => !objectOf({ verdict: isAuthorityVerdict })(rule),\n\t)\n\tif (refused !== -1) {\n\t\tthrow new AgentError(\n\t\t\t'AUTHORITY',\n\t\t\t`rule ${refused} has no verdict; set verdict: 'allow' | 'ask' | 'deny'`,\n\t\t\t{ context: { rule: refused } },\n\t\t)\n\t}\n\treturn rules\n}\n\n/**\n * Consumes a reported usage against a budget over what was already charged, so a turn's total\n * draw is never less than the report and nothing is charged twice.\n *\n * @remarks\n * The full `prompt` count is consumed because no earlier charge covers it. Each of `completion`\n * and `total` is consumed less `charged`, floored at 0. An estimate above the report is not\n * refunded. The usage must already be sanitized: a\n * non-finite field yields a non-finite charge, which the installed `Budget` refuses by throwing\n * a `range` `ContractError`. Without a budget the call consumes nothing.\n *\n * @param budget - The budget to consume against, or `undefined` for an unmetered run\n * @param usage - The sanitized usage the provider reported\n * @param charged - The completion tokens already consumed this turn\n *\n * @example\n * ```ts\n * const budget = createBudget<TokenUsage>({ max: 1000, consumer: (usage) => usage.total })\n * chargeUsage(budget, { prompt: 20, completion: 30, total: 50 }, 10)\n * budget.consumed // 40\n * ```\n */\nexport function chargeUsage(\n\tbudget: BudgetInterface<TokenUsage> | undefined,\n\tusage: TokenUsage,\n\tcharged: number,\n): void {\n\tbudget?.consume({\n\t\tprompt: usage.prompt,\n\t\tcompletion: Math.max(0, usage.completion - charged),\n\t\ttotal: Math.max(0, usage.total - charged),\n\t})\n}\n\n/**\n * Returns a tool-iteration cap after checking it is a positive safe integer, or throws a coded error.\n *\n * @remarks\n * The check is {@link import('../validators.js').isPositiveSafeInteger}, so `0`, `-0`, `NaN`, a\n * fraction, a negative, and either infinity are refused, because a run of no turns asks the provider\n * nothing and answers nothing. The error's `context` carries the refused value as `limit`.\n *\n * @param limit - The tool-iteration cap to check\n * @returns The cap unchanged\n * @throws {AgentError} Thrown when `limit` is not a positive safe integer (code `'LIMIT'`)\n *\n * @example\n * ```ts\n * requireLimit(3) // 3\n * requireLimit(0) // throws AgentError ('LIMIT')\n * ```\n */\nexport function requireLimit(limit: number): number {\n\tif (!isPositiveSafeInteger(limit)) {\n\t\tthrow new AgentError(\n\t\t\t'LIMIT',\n\t\t\t'a limit must be a positive safe integer, because a run of no turns answers nothing',\n\t\t\t{ context: { limit } },\n\t\t)\n\t}\n\treturn limit\n}\n\n/**\n * Returns a run's budget or allowance after checking it implements `BudgetInterface`, or throws a\n * coded error before the run starts it.\n *\n * @remarks\n * The check is `isBudget` from `@orkestrel/budget`, a total guard. `undefined` passes, because an\n * absent bound is no bound. The error's `context` carries the option path, the limit\n * `'BudgetInterface'`, and a preview of the refused value.\n *\n * @param value - The effective `budget` or `allowance`, or `undefined`\n * @param key - The option the value came from, named in the error's path\n * @returns The value unchanged\n * @throws {ContractError} Thrown when `value` is defined and does not implement `BudgetInterface` (code `'placement'`)\n *\n * @example\n * ```ts\n * requireBudget(createTokenBudget({ max: 10 }), 'budget') // the budget\n * requireBudget({ consume() {} }, 'allowance') // throws ContractError ('placement')\n * ```\n */\nexport function requireBudget(\n\tvalue: BudgetInterface<TokenUsage> | undefined,\n\tkey: 'budget' | 'allowance',\n): BudgetInterface<TokenUsage> | undefined {\n\tif (value === undefined || isBudget(value)) return value\n\tthrow new ContractError(`Agent: ${key} must implement BudgetInterface`, {\n\t\tcode: 'placement',\n\t\tcontext: { path: ['options', key], limit: 'BudgetInterface', received: preview(value) },\n\t})\n}\n\n/**\n * Returns a run's `conversation` option after checking it implements `ConversationInterface`, or\n * throws a coded error before the run starts.\n *\n * @remarks\n * The check is {@link isConversation}, a total guard. `undefined` passes, because an absent option\n * binds the run to the active conversation. The error's `context` carries the option path, the\n * limit `'ConversationInterface'`, and a preview of the refused value.\n *\n * @param value - The run's `conversation` option, or `undefined`\n * @returns The value unchanged\n * @throws {ContractError} Thrown when `value` is defined and does not implement `ConversationInterface` (code `'placement'`)\n *\n * @example\n * ```ts\n * requireConversation(createConversation()) // the conversation\n * requireConversation({ id: 'c1' }) // throws ContractError ('placement')\n * ```\n */\nexport function requireConversation(value: unknown): ConversationInterface | undefined {\n\tif (value === undefined || isConversation(value)) return value\n\tthrow new ContractError('Agent: conversation must implement ConversationInterface', {\n\t\tcode: 'placement',\n\t\tcontext: {\n\t\t\tpath: ['options', 'conversation'],\n\t\t\tlimit: 'ConversationInterface',\n\t\t\treceived: preview(value),\n\t\t},\n\t})\n}\n\n/**\n * Renders a tool's output as the string content of its tool message, or returns `undefined` when JSON\n * cannot carry it.\n *\n * @remarks\n * A string passes through unchanged and `undefined` renders as `''`. Any other value renders as\n * its `JSON.stringify` text, byte for byte, when JSON can carry it. A value JSON cannot carry (a\n * `BigInt`, a cycle, a symbol, a function, a throwing `toJSON`) returns `undefined`, which\n * {@link settleToolResult} turns into a tool failure.\n *\n * @param value - The tool's output (`ToolSuccess.value`, which may be `undefined`)\n * @returns The message content, or `undefined` when JSON cannot carry the value\n *\n * @example\n * ```ts\n * renderToolValue('sunny') // 'sunny'\n * renderToolValue({ count: 2 }) // '{\"count\":2}'\n * renderToolValue(undefined) // ''\n * renderToolValue(10n) // undefined\n * ```\n */\nexport function renderToolValue(value: unknown): string | undefined {\n\tif (typeof value === 'string') return value\n\tif (value === undefined) return ''\n\treturn renderJSON(value)\n}\n\n/**\n * Settles one dispatched result into the result the loop reports and the content of its tool message,\n * turning a success whose value JSON cannot carry into a tool failure the model reads.\n *\n * @remarks\n * A failure passes through with its `error` as the content. A success renders through\n * {@link renderToolValue}. A success that renders to `undefined` becomes the failure arm with the\n * call's `id` and `name`, whose `error` names the tool and the remedy.\n *\n * @param result - The result the tool manager returned for one call\n * @returns The result to report and the tool message content\n *\n * @example\n * ```ts\n * settleToolResult({ success: true, id: '1', name: 'read', value: { count: 2 } }).content // '{\"count\":2}'\n * settleToolResult({ success: true, id: '1', name: 'read', value: 10n }).outcome.success // false\n * ```\n */\nexport function settleToolResult(result: ToolResult): {\n\treadonly outcome: ToolResult\n\treadonly content: string\n} {\n\tif (!result.success) return { outcome: result, content: result.error }\n\tconst content = renderToolValue(result.value)\n\tif (content !== undefined) return { outcome: result, content }\n\tconst error = `${preview(result.name)} returned a value JSON cannot carry; return strings, numbers, booleans, null, arrays, and plain records`\n\treturn {\n\t\toutcome: { success: false, id: result.id, name: result.name, error },\n\t\tcontent: error,\n\t}\n}\n\n/**\n * Returns a registered value or throws when its name is absent.\n * @param pool - The named values\n * @param category - The registry category named in an error\n * @param name - The requested key\n * @returns The registered value\n * @throws {AgentError} Thrown when the name is absent, with code REGISTRY, the name encoded and capped in its text because a tool refusal reaches a model, and a `context` naming the `category` and the `name`.\n * @example\n * ```ts\n * requireEntry(new Map([['main', provider]]), 'provider', 'main') // provider\n * ```\n */\nexport function requireEntry<T>(pool: ReadonlyMap<string, T>, category: string, name: string): T {\n\tconst value = pool.get(name)\n\tif (value === undefined) {\n\t\tthrow new AgentError('REGISTRY', `unknown ${category}: ${preview(name)}`, {\n\t\t\tcontext: { category, name },\n\t\t})\n\t}\n\treturn value\n}\n\n/**\n * Extracts the present concurrency, retry, and deadline options for an agent job queue.\n * @param options - The queue options to project\n * @returns The present queue options, omitting absent keys\n * @example\n * ```ts\n * extractQueueOptions({ concurrency: 2 }) // { concurrency: 2 }\n * ```\n */\nexport function extractQueueOptions(\n\toptions: Pick<QueueOptions<AgentJobInput, AgentResult>, 'concurrency' | 'retries' | 'timeout'>,\n): Pick<QueueOptions<AgentJobInput, AgentResult>, 'concurrency' | 'retries' | 'timeout'> {\n\tconst { concurrency, retries, timeout } = options\n\treturn {\n\t\t...(concurrency === undefined ? {} : { concurrency }),\n\t\t...(retries === undefined ? {} : { retries }),\n\t\t...(timeout === undefined ? {} : { timeout }),\n\t}\n}\n","import type {\n\tAgentChunk,\n\tAgentEventMap,\n\tAgentInterface,\n\tAgentOptions,\n\tAgentResult,\n\tAgentRunOptions,\n\tAgentStatus,\n\tAgentStreamInterface,\n\tAgentTurn,\n\tAuthorityDecision,\n\tAuthorityInterface,\n\tAgentRunResult,\n} from './types.js'\nimport type { ProviderInterface, ProviderRequest, ProviderResult } from '../providers/index.js'\nimport type { AgentContextInterface, Selection } from '../contexts/index.js'\nimport type { ConversationInterface, Section } from '../conversations/index.js'\nimport type { Message, ThinkingReplay } from '../types.js'\nimport type { AbortInterface } from '@orkestrel/abort'\nimport type { BudgetInterface, MeterInterface, TokenUsage } from '@orkestrel/budget'\nimport type { Result } from '@orkestrel/contract'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport type {\n\tToolCall,\n\tToolDefinition,\n\tToolInterface,\n\tToolManagerInterface,\n\tToolResult,\n} from '@orkestrel/tool'\nimport type { SchedulerInterface } from '@orkestrel/workflow'\nimport type { TimeoutInterface } from '@orkestrel/timeout'\nimport { createAbort } from '@orkestrel/abort'\nimport { createTokenMeter } from '@orkestrel/budget'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tContractError,\n\terrorToMessage,\n\tfailure,\n\tisObject,\n\tpreview,\n\tsuccess,\n} from '@orkestrel/contract'\nimport { createTimeout } from '@orkestrel/timeout'\nimport { Emitter } from '@orkestrel/emitter'\nimport { AgentContext } from '../contexts/index.js'\nimport { raceSignal } from '../conversations/index.js'\nimport { AUTHORITY_NOTES, DEFAULT_AGENT_LIMIT, SCOPE_NOTE } from './constants.js'\nimport { agentPromptContract } from './contracts.js'\nimport { AgentError } from './errors.js'\nimport { Channel } from './Channel.js'\nimport {\n\tassembleResult,\n\tchargeUsage,\n\tdenyCall,\n\tinferStop,\n\trequireAnswer,\n\trequireBudget,\n\trequireConversation,\n\trequireLimit,\n\tresolveDecision,\n\tsettleToolResult,\n} from './helpers.js'\nimport { isAuthorityDecision } from './validators.js'\nimport {\n\testimateTokens,\n\tfilterAllowList,\n\tsanitizeUsage,\n\tstripThinking,\n\tsumUsage,\n} from '../helpers.js'\nimport {\n\tisProviderAbortError,\n\tProviderError,\n\treadProviderDelta,\n\trequireProviderResult,\n} from '../providers/index.js'\n\n/**\n * Composes a {@link ProviderInterface}, an {@link AgentContext}, and a\n * {@link ToolManagerInterface} into a bounded context → provider → tools → repeat turn, exposed\n * as both a one-shot `generate` and a live `stream` that share one private run — bounded by the\n * run `signal`, the `timeout`, and the `budget` folded through `AbortSignal.any`, paced by\n * `scheduler`, with tool iteration capped at `limit`.\n *\n * @remarks\n * - **One loop, two faces.** A single private async generator (`#execute`) drives the\n * whole run. `stream` drives it eagerly into a private {@link Channel} and settles `result`\n * from the outcome the generator returns, through one `.then` — so `result` settles\n * whether or not the live `events` are drained; `generate` awaits that same\n * settled `result` — so the two can never diverge.\n * - **The turn.** `#execute` builds the provider input once, then runs up to `limit` turns,\n * each appending one readonly {@link AgentTurn} record: drive `provider.stream(...)`,\n * yielding each content delta as a `token` chunk and charging its estimate to the\n * `allowance` when one is set; reconcile the turn's usage against the `budget`, before a\n * genuine provider error rejects the run too, and yield a `usage` chunk; if the model\n * requested tools, yield an `ask` chunk per asked call after its approver is entered,\n * append the assistant turn, pass them to the tool manager's `execute`\n * method, yield a `tool` chunk per call, append each tool result message, and continue;\n * otherwise append the final assistant message and stop. The run's content,\n * thinking, usage, and stop derive from the turn records. Each assistant message stores its\n * call's non-empty thinking.\n * Provider calls and prompt estimates apply the provider's `replay` policy to the working\n * array; an absent policy means `'none'`.\n * - **Bounded.** Each run arms one cancel through `createAbort({ signal: AbortSignal.any([\n * …]) })` folding the external `signal`, the `timeout` deadline, and the `budget` and\n * `allowance` signals; `abort()` fires it. Any trip stops the loop and commits a partial result\n * (the `result` promise resolves, never rejects, on a cancel) — only a genuine\n * provider / tool error rejects.\n * - **Paced + capped.** The loop calls the `scheduler` object's `yield` method between turns when given; tool\n * iteration is capped at `limit`.\n * - **Two observation surfaces.** The pull {@link AgentChunk} stream carries per-token\n * deltas (+ usage/ask/tool chunks); the push {@link emitter} ({@link AgentEventMap}) carries\n * lifecycle + usage/tool/allow/ask/deny moments for fire-and-forget observers. Every event is\n * emitted directly, after the relevant state transition / settle; the emitter isolates a\n * listener throw and routes it to its `error` handler (the `error` option), so a buggy\n * observer can never escape into / reorder / corrupt the settle-once loop — observation is\n * purely a side-channel.\n *\n * @example\n * ```ts\n * const agent = new Agent(provider, { system: 'You are concise.' })\n * agent.context.messages.add({ role: 'user', content: 'Say hi.' })\n * const result = await agent.generate()\n * ```\n */\nexport class Agent implements AgentInterface {\n\treadonly #id: string\n\treadonly #provider: ProviderInterface\n\treadonly #replay: ThinkingReplay\n\treadonly #context: AgentContextInterface\n\treadonly #limit: number\n\treadonly #timeoutMs: number | undefined\n\treadonly #budget: BudgetInterface<TokenUsage> | undefined\n\t// The meter layer's allowance: the budget each content delta's estimate is charged against while\n\t// a call streams. `undefined` ⇒ no meter layer; the estimate is still measured for `usage`.\n\treadonly #allowance: BudgetInterface<TokenUsage> | undefined\n\treadonly #scheduler: SchedulerInterface | undefined\n\treadonly #signal: AbortSignal | undefined\n\t// The gate consulted for each admitted call; `undefined` only on an agent built with no `tools`,\n\t// where a tool a host adds later through `context.tools` is denied rather than run.\n\treadonly #authority: AuthorityInterface | undefined\n\t// The context budget that admits each provider request — its `consumer` is a token estimator\n\t// over the complete `ProviderRequest`, its `max` the context window. `#admit` re-measures the\n\t// exact request (replay-filtered messages, advertised tools, per-call options) against it before\n\t// every dispatch; `undefined` ⇒ disabled, and the loop never calls `#admit`. Reset (`clear()`)\n\t// at run entry so no stale `consumed` carries across runs / a conversation switch. Not the hard\n\t// cost `budget` ceiling — when the request reaches its `max` this compacts + continues\n\t// (non-fatal on a summarizer throw, futile-guarded) and never aborts; only `strict` refuses.\n\treadonly #window: BudgetInterface<ProviderRequest> | undefined\n\t// True by default: a summarizer failure during automatic compaction rethrows (after the `fault`\n\t// event) instead of skipping compaction and continuing over-window, a request still over the\n\t// window after the permitted fold is refused with a WINDOW error before dispatch, and an answer\n\t// that fails the run's `schema` is refused with a SCHEMA error.\n\treadonly #strict: boolean\n\t// The push observation surface — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into the loop. No\n\t// `destroy()`: the Agent holds no other teardownable resources, and an `Emitter` owns\n\t// only listener sets (no timers / handles), so it is reclaimed with the agent — there\n\t// is no leak to clear, and adding lifecycle the entity does not otherwise need is avoided.\n\treadonly #emitter: Emitter<AgentEventMap>\n\t// The label the last run settled on. `status` derives the live answer from `#runs`, so an\n\t// overlapping run can never report `done` while another is still in flight.\n\t#settled: AgentStatus = 'idle'\n\t// Every in-flight run's abort handle, mapped to the distinct budget and allowance instances that\n\t// run holds and the conversation it is bound to — a run adds its entry on `stream()` and its\n\t// settle removes it, so `abort()` fires every live run, not only the most recent, and the\n\t// concurrency guards read which instances and conversations are held. Per-run, never a single\n\t// shared slot a later `stream()` could clobber: `generate`/`stream` are reusable and may overlap,\n\t// and each run must cancel independently (its own `stream.abort()` fires its own handle;\n\t// `agent.abort()` fires them all).\n\treadonly #runs = new Map<\n\t\tAbortInterface,\n\t\t{\n\t\t\treadonly budgets: ReadonlyArray<BudgetInterface<TokenUsage>>\n\t\t\treadonly conversation: ConversationInterface\n\t\t}\n\t>()\n\n\tconstructor(provider: ProviderInterface, options?: AgentOptions) {\n\t\tif (options?.tools !== undefined && options.authority === undefined) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'AUTHORITY',\n\t\t\t\t\"tools are set and no authority is; pass authority: createAuthority({ fallback: { zone: 'default', verdict: 'allow' } }) to allow every call, or rules with a deny fallback\",\n\t\t\t)\n\t\t}\n\t\tthis.#limit = requireLimit(options?.limit ?? DEFAULT_AGENT_LIMIT)\n\t\tthis.#id = crypto.randomUUID()\n\t\tthis.#provider = provider\n\t\tthis.#replay = provider.replay ?? 'none'\n\t\tthis.#context = new AgentContext({\n\t\t\t...(options?.system === undefined ? {} : { system: options.system }),\n\t\t\t...(options?.tools === undefined ? {} : { tools: options.tools }),\n\t\t\t...(options?.instructions === undefined ? {} : { instructions: options.instructions }),\n\t\t\t...(options?.skills === undefined ? {} : { skills: options.skills }),\n\t\t\t...(options?.memos === undefined ? {} : { memos: options.memos }),\n\t\t\t...(options?.workspaces === undefined ? {} : { workspaces: options.workspaces }),\n\t\t\t...(options?.scope === undefined ? {} : { scope: options.scope }),\n\t\t\t...(options?.conversations === undefined ? {} : { conversations: options.conversations }),\n\t\t\t...(options?.select === undefined ? {} : { select: options.select }),\n\t\t})\n\t\tthis.#timeoutMs = options?.timeout\n\t\tthis.#budget = options?.budget\n\t\tthis.#allowance = options?.allowance\n\t\tthis.#scheduler = options?.scheduler\n\t\tthis.#signal = options?.signal\n\t\tthis.#authority = options?.authority\n\t\tthis.#window = options?.window\n\t\tthis.#strict = options?.strict ?? true\n\t\tthis.#emitter = new Emitter<AgentEventMap>(options)\n\t}\n\n\tget id(): string {\n\t\treturn this.#id\n\t}\n\n\tget emitter(): EmitterInterface<AgentEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget status(): AgentStatus {\n\t\t// Derived, never stored: a live run outranks any settled label, so overlapping runs can\n\t\t// never report `done` while one is still in flight, and a settled `error` is not erased\n\t\t// by a later run's `done` until that later run itself settles.\n\t\treturn this.#runs.size > 0 ? 'running' : this.#settled\n\t}\n\n\tget context(): AgentContextInterface {\n\t\treturn this.#context\n\t}\n\n\tgenerate(options?: AgentRunOptions): Promise<AgentResult> {\n\t\t// Zero loop logic of its own — start the stream (whose eager drive settles `result`\n\t\t// independently of any consumer) and await that same settled result, so `generate`\n\t\t// and `stream` can never diverge. No manual drain needed: the drive runs regardless.\n\t\treturn this.stream(options).result\n\t}\n\n\tstream(options?: AgentRunOptions): AgentStreamInterface {\n\t\t// The prompt is checked exactly before every other guard, so a refused prompt starts nothing.\n\t\tconst prompt = options?.prompt\n\t\tif (prompt !== undefined && !agentPromptContract.is(prompt)) {\n\t\t\t// A contract error's context declares no faults, so they travel in the text it carries.\n\t\t\tconst faults = attempt(() =>\n\t\t\t\tdescribeFaults(agentPromptContract.audit(prompt), agentPromptContract.schema),\n\t\t\t)\n\t\t\tthrow new ContractError(\n\t\t\t\t`Agent: prompt must be an exact AgentPrompt${faults.success ? `\\n${faults.value}` : ''}`,\n\t\t\t\t{\n\t\t\t\t\tcode: 'placement',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\tpath: ['options', 'prompt'],\n\t\t\t\t\t\tlimit: 'AgentPrompt',\n\t\t\t\t\t\treceived: preview(prompt),\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\t\t// Resolve effective per-run bounds — a per-run override wins, else the construction\n\t\t// default. `limit`, `budget`, and `allowance` also thread into `#execute` (the loop bound,\n\t\t// the reconcile, and the mid-stream meter); each budget is the same instance folded into\n\t\t// `#combineSignals`, so its trip both aborts the run and is the budget the run charges.\n\t\tconst limit = options?.limit === undefined ? this.#limit : requireLimit(options.limit)\n\t\t// Both bounds are checked before the concurrency guard and before either `start()`, so a\n\t\t// refused bound leaves the other untouched and nothing emitted.\n\t\tconst budget = requireBudget(options?.budget ?? this.#budget, 'budget')\n\t\tconst allowance = requireBudget(options?.allowance ?? this.#allowance, 'allowance')\n\t\t// The distinct accounting instances this run holds, cost budget first: an allowance that is\n\t\t// the budget itself is held, started, and folded once.\n\t\tconst held = [budget, allowance].filter(\n\t\t\t(one, index, all): one is BudgetInterface<TokenUsage> =>\n\t\t\t\tone !== undefined && all.indexOf(one) === index,\n\t\t)\n\t\t// Concurrency guard: a `window` is one context budget every run clears and measures, and a\n\t\t// budget instance carries one signal that `start()` replaces — a second live run on either\n\t\t// would race the first run's accounting or orphan its bound. Thrown synchronously, before\n\t\t// any state mutation or emit, so a sequential/awaited caller is never affected and runs\n\t\t// holding distinct budget instances with no `window` still overlap.\n\t\tif (\n\t\t\tthis.#runs.size > 0 &&\n\t\t\t(this.#window !== undefined ||\n\t\t\t\t[...this.#runs.values()].some((live) => live.budgets.some((one) => held.includes(one))))\n\t\t) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'CONCURRENCY',\n\t\t\t\t'a live run on this agent holds the window or this budget or allowance instance; use separate agents or distinct instances per run',\n\t\t\t\t{ context: { agent: this.#id } },\n\t\t\t)\n\t\t}\n\t\t// The run binds its conversation here, at the call: a later `switch`, `open`, `remove`,\n\t\t// `clear`, or same-id `add` moves the pointer, never this run. One live run per conversation —\n\t\t// two would interleave their turns in one thread — refused synchronously, before any `start()`\n\t\t// or emit, like the guard above.\n\t\tconst conversation = requireConversation(options?.conversation) ?? this.#context.messages\n\t\tif ([...this.#runs.values()].some((live) => live.conversation === conversation)) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'CONCURRENCY',\n\t\t\t\t'a live run on this agent holds this conversation; await it or bind the run to another conversation',\n\t\t\t\t{ context: { agent: this.#id, conversation: conversation.id } },\n\t\t\t)\n\t\t}\n\t\t// The deadline is created unarmed: its signal keeps its identity when `start()` arms it, so\n\t\t// it can be composed first and armed only after nothing left can throw.\n\t\tconst timeoutMs = options?.timeout ?? this.#timeoutMs\n\t\tconst timeout = timeoutMs === undefined ? undefined : createTimeout({ ms: timeoutMs })\n\t\t// The prompt joins the conversation from its named members after every guard, before `start()`.\n\t\tif (prompt !== undefined) {\n\t\t\tconversation.add({\n\t\t\t\trole: 'user',\n\t\t\t\tcontent: prompt.content,\n\t\t\t\t...(prompt.images === undefined ? {} : { images: prompt.images }),\n\t\t\t})\n\t\t}\n\t\t// `start()` replaces each held budget's signal, so it precedes the composition that reads it.\n\t\tfor (const one of held) one.start()\n\t\t// Fold every present bound (external signal + a per-run signal + deadline + budget +\n\t\t// allowance) into one cancel the run races against; this run's own `abort()` fires this handle.\n\t\tconst signal = this.#combineSignals(timeout, budget, allowance, options?.signal)\n\t\tconst abort = createAbort(signal === undefined ? {} : { signal })\n\t\ttimeout?.start()\n\t\tthis.#runs.set(abort, { budgets: held, conversation })\n\t\t// Observe the run begin — after the run joins `#runs` (which is what `status` derives\n\t\t// `running` from), so a swallowed listener throw can't perturb the state the drive is\n\t\t// about to change.\n\t\tthis.#emitter.emit('start', this.id)\n\t\tconst channel = new Channel<AgentChunk>()\n\t\tconst settled = Promise.withResolvers<AgentResult>()\n\t\t// Kick off the eager drive synchronously (not lazily on first `events` pull): it drives\n\t\t// `#execute` into the channel, and the run settles from its outcome regardless of whether\n\t\t// anyone drains `events`. The per-run `think` / `schema` preferences ride through to\n\t\t// `provider.stream`; `limit` / `budget` / `allowance` ride through as the effective run bounds.\n\t\tvoid this.#drive(\n\t\t\tabort,\n\t\t\tconversation,\n\t\t\tchannel,\n\t\t\toptions?.think,\n\t\t\toptions?.schema,\n\t\t\tlimit,\n\t\t\tbudget,\n\t\t\tallowance,\n\t\t).then(\n\t\t\t(outcome) => this.#finish(abort, timeout, channel, settled, limit, outcome),\n\t\t\t(error: unknown) => this.#fail(abort, timeout, channel, settled, error),\n\t\t)\n\t\t// An abandoned handle (neither `events` drained nor `result` awaited) must not surface an\n\t\t// unhandledRejection on a genuine error — guard the public result, where the rejection lives\n\t\t// (`#fail` rejects `settled` without re-throwing, so the drive's settle itself resolves).\n\t\t// A caller who awaits `result` still gets the rejection: `.catch` returns a derived promise, it\n\t\t// does not consume the original's rejection.\n\t\tsettled.promise.catch(() => {})\n\t\treturn {\n\t\t\tevents: this.#drainEvents(channel, abort),\n\t\t\tresult: settled.promise,\n\t\t\t// Fire this run's own handle (the closed-over `abort`), never a shared field a\n\t\t\t// later `stream()` could have replaced — so a handle's `abort()` always cancels\n\t\t\t// the run it belongs to, even when runs overlap.\n\t\t\tabort: abort.abort.bind(abort),\n\t\t}\n\t}\n\n\tabort(reason?: unknown): void {\n\t\t// Cancel every in-flight run — iterate a snapshot so a settle-driven `#runs.delete`\n\t\t// (a cancelled run unwinding) can't disturb the walk. Aborting is idempotent, so an\n\t\t// already-finished or already-cancelled handle is a harmless no-op.\n\t\tfor (const abort of [...this.#runs.keys()]) abort.abort(reason)\n\t}\n\n\t// Settles a finished or cancelled run: the deadline clears, the run leaves `#runs` (releasing its\n\t// budget instances), the channel closes, and the assembled result resolves before it is observed.\n\t// A cancel still resolves a partial, so a cancelled run emits `abort` (the cancel reason) then\n\t// `finish`; limit exhaustion is not a cancel and emits `exhaust` (the turn count) instead. Every\n\t// emit follows the resolve, so an isolated listener throw can't reorder the settle.\n\t#finish(\n\t\tabort: AbortInterface,\n\t\ttimeout: TimeoutInterface | undefined,\n\t\tchannel: Channel<AgentChunk>,\n\t\tsettled: PromiseWithResolvers<AgentResult>,\n\t\tlimit: number,\n\t\toutcome: AgentRunResult,\n\t): void {\n\t\ttimeout?.clear()\n\t\tthis.#runs.delete(abort)\n\t\tthis.#settled = 'done'\n\t\tchannel.close()\n\t\tconst result = assembleResult(outcome)\n\t\tsettled.resolve(result)\n\t\tif (outcome.exhausted) this.#emitter.emit('exhaust', limit)\n\t\telse if (outcome.partial) this.#emitter.emit('abort', abort.signal.reason)\n\t\tthis.#emitter.emit('finish', result)\n\t}\n\n\t// Settles a run a genuine (non-cancel) provider / tool error stopped: the channel fails, the\n\t// result rejects, and the failure is observed after the reject.\n\t#fail(\n\t\tabort: AbortInterface,\n\t\ttimeout: TimeoutInterface | undefined,\n\t\tchannel: Channel<AgentChunk>,\n\t\tsettled: PromiseWithResolvers<AgentResult>,\n\t\terror: unknown,\n\t): void {\n\t\ttimeout?.clear()\n\t\tthis.#runs.delete(abort)\n\t\tthis.#settled = 'error'\n\t\tchannel.fail(error)\n\t\tsettled.reject(error)\n\t\tthis.#emitter.emit('error', error)\n\t}\n\n\t// Drives `#execute` by hand, pushing each chunk into the channel, and returns the generator's\n\t// return value; a genuine provider / tool error rejects. `for await` would discard the return.\n\tasync #drive(\n\t\tabort: AbortInterface,\n\t\tconversation: ConversationInterface,\n\t\tchannel: Channel<AgentChunk>,\n\t\tthink: boolean | undefined,\n\t\tschema: Readonly<Record<string, unknown>> | undefined,\n\t\tlimit: number,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t): Promise<AgentRunResult> {\n\t\tconst run = this.#execute(abort, conversation, think, schema, limit, budget, allowance)\n\t\tfor (;;) {\n\t\t\tconst next = await run.next()\n\t\t\tif (next.done === true) return next.value\n\t\t\tchannel.push(next.value)\n\t\t}\n\t}\n\n\t// The live event stream: drain the channel the drive writes into, yielding each `AgentChunk` as it\n\t// is pushed (and throwing if the drive called `fail` on the channel). Its `return()` — fired when\n\t// a consumer exits with `break` early — fires the turn abort, so the run stops promptly: the drive\n\t// then completes the loop with `partial: true`, calls `clear` on the deadline, and settles\n\t// `result` to a non-misleading `{ partial: true }`, leaving `status` no longer `running`.\n\tasync *#drainEvents(\n\t\tchannel: Channel<AgentChunk>,\n\t\tabort: AbortInterface,\n\t): AsyncGenerator<AgentChunk, void> {\n\t\ttry {\n\t\t\tyield* channel.drain()\n\t\t} finally {\n\t\t\t// Early break (consumer stopped pulling): cancel the run so the drive unwinds and\n\t\t\t// settles partial. A natural end reaches here too with the signal already done, so\n\t\t\t// this abort is a harmless no-op then.\n\t\t\tabort.abort()\n\t\t}\n\t}\n\n\t// The core loop, shared by generate + stream. Builds the provider input once, then runs turns up\n\t// to `limit`, appending each turn's readonly record, and continues only while the latest turn\n\t// requested tools. A cancel (the bound abort) ends a turn with a `cancel` record — it never\n\t// throws; only a genuine provider / tool error propagates. Every record is local to one call, so\n\t// concurrent runs share nothing, and the settled `AgentRunResult` derives from the records.\n\t// Each turn runs in this generator, which delegates only the provider stream to `#provide` and the\n\t// ask pass to `#approve`: a delta crosses these two layers before `#drive` pushes it, and no\n\t// per-turn generator settles between a turn's chunks. Every further layer would let a consumer's\n\t// early `return()` cancel the provider a step sooner.\n\tasync *#execute(\n\t\tabort: AbortInterface,\n\t\tconversation: ConversationInterface,\n\t\tthink: boolean | undefined,\n\t\tschema: Readonly<Record<string, unknown>> | undefined,\n\t\tlimit: number,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t): AsyncGenerator<AgentChunk, AgentRunResult> {\n\t\t// Compaction can fold the request into a section, so every select site receives the object\n\t\t// captured at run entry.\n\t\tconst last = conversation.view().at(-1)\n\t\tconst request = last?.role === 'user' ? last : undefined\n\t\t// `#select` returns `undefined` when no handler is set, so the default path awaits nothing\n\t\t// before the first provider request.\n\t\tconst selection = this.#select(conversation, request, abort, budget)\n\t\tconst entry =\n\t\t\tselection === undefined\n\t\t\t\t? { messages: this.#build(conversation), usage: undefined }\n\t\t\t\t: await selection\n\t\tconst tools = this.#context.tools\n\t\t// The per-call options are fixed for the run, so every request carries this one snapshot;\n\t\t// absent when neither preference is set.\n\t\tconst options: ProviderRequest['options'] =\n\t\t\tthink === undefined && schema === undefined\n\t\t\t\t? undefined\n\t\t\t\t: {\n\t\t\t\t\t\t...(think === undefined ? {} : { think }),\n\t\t\t\t\t\t...(schema === undefined ? {} : { schema }),\n\t\t\t\t\t}\n\t\t// Reset `#window` at run entry so no stale `consumed` carries across runs / a conversation\n\t\t// switch.\n\t\tthis.#window?.clear()\n\t\t// One readonly record per turn; the turn index is the list length.\n\t\tconst turns: AgentTurn[] = []\n\t\t// One turn per step: observe it, pace the boundary, admit the request, stream the provider,\n\t\t// reconcile the usage, then dispatch the requested tools or record the final answer. A cancel\n\t\t// appends a `cancel` record, which ends the loop; a genuine error throws. The section before\n\t\t// the provider call adds no `await` on the default path, so the provider's `stream()` is\n\t\t// entered in `agent.stream()`'s own call.\n\t\twhile (turns.length < limit && (turns.at(-1)?.outcome ?? 'tools') === 'tools') {\n\t\t\tconst index = turns.length\n\t\t\tconst base = turns.at(-1)?.messages ?? entry.messages\n\t\t\tconst futile = turns.at(-1)?.futile ?? false\n\t\t\t// The record of a turn a cancel stopped before its provider call.\n\t\t\tconst stopped: AgentTurn = {\n\t\t\t\tcontent: '',\n\t\t\t\tthinking: undefined,\n\t\t\t\tusage: undefined,\n\t\t\t\tspent: undefined,\n\t\t\t\tmessages: base,\n\t\t\t\toutcome: 'cancel',\n\t\t\t\tpartial: true,\n\t\t\t\tfutile,\n\t\t\t\testimate: undefined,\n\t\t\t\tinterrupted: false,\n\t\t\t\ttruncated: false,\n\t\t\t}\n\t\t\t// Observe each turn begin (the turn index). The emitter isolates a listener throw, so it\n\t\t\t// can't perturb the turn that immediately follows.\n\t\t\tthis.#emitter.emit('turn', index)\n\t\t\t// Pace between expensive turns — never before the first. The yield is raced against the run\n\t\t\t// signal, so a scheduler that ignores it still ends on a cancel; a cancel landing at the turn\n\t\t\t// boundary surfaces here as a throw and stops the turn like any cancel, while a genuine\n\t\t\t// scheduler fault propagates.\n\t\t\tconst scheduler = this.#scheduler\n\t\t\tif (index > 0 && scheduler !== undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tawait raceSignal(scheduler.yield({ signal: abort.signal }), abort.signal)\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (!abort.signal.aborted) throw error\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (abort.signal.aborted) {\n\t\t\t\tturns.push(stopped)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// Snapshot the allow-list before listeners can change the active scope this turn.\n\t\t\tconst names = this.#context.scope?.tools?.slice()\n\t\t\tconst advertised = filterAllowList(\n\t\t\t\tnames,\n\t\t\t\ttools.definitions(),\n\t\t\t\t(definition) => definition.name,\n\t\t\t)\n\t\t\t// The instance each advertised name holds at this point: every decision of this turn applies to it.\n\t\t\tconst held = new Map(\n\t\t\t\tadvertised.map((definition) => [definition.name, tools.tool(definition.name)]),\n\t\t\t)\n\t\t\tconst definitions = advertised.length > 0 ? advertised : undefined\n\t\t\t// Admit the request: compose it once from the replay-filtered messages and this turn's tool\n\t\t\t// and option snapshot, measure it against `#window`, and dispatch exactly the request the\n\t\t\t// window last measured (the rebuilt one after a fold). With no `window` it is composed and\n\t\t\t// dispatched with no extra `await`.\n\t\t\tconst composed = this.#compose(base, definitions, options)\n\t\t\tconst admitted =\n\t\t\t\tthis.#window === undefined\n\t\t\t\t\t? { dispatch: composed, messages: base, spent: undefined, futile }\n\t\t\t\t\t: await this.#admit(\n\t\t\t\t\t\t\tthis.#window,\n\t\t\t\t\t\t\tconversation,\n\t\t\t\t\t\t\tcomposed,\n\t\t\t\t\t\t\tbase,\n\t\t\t\t\t\t\trequest,\n\t\t\t\t\t\t\tindex > 0,\n\t\t\t\t\t\t\tfutile,\n\t\t\t\t\t\t\tabort,\n\t\t\t\t\t\t\tbudget,\n\t\t\t\t\t\t)\n\t\t\tif (admitted.dispatch === undefined) {\n\t\t\t\tturns.push({\n\t\t\t\t\t...stopped,\n\t\t\t\t\tmessages: admitted.messages,\n\t\t\t\t\tspent: admitted.spent,\n\t\t\t\t\tfutile: admitted.futile,\n\t\t\t\t})\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// The turn's streamed content, one entry per content delta; the meter charges only when an\n\t\t\t// allowance exists.\n\t\t\tconst streamed: string[] = []\n\t\t\tconst meter =\n\t\t\t\tallowance === undefined\n\t\t\t\t\t? undefined\n\t\t\t\t\t: createTokenMeter({ budget: allowance, estimator: estimateTokens })\n\t\t\tconst provided = yield* this.#provide(admitted.dispatch, abort.signal, streamed, meter)\n\t\t\tconst settled = this.#reconcile(provided, abort, streamed, admitted, meter, budget, allowance)\n\t\t\tif (!provided.success) {\n\t\t\t\tturns.push({ ...settled, outcome: 'cancel', partial: true })\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst result = provided.value\n\t\t\tconst recorded = settled.thinking === undefined ? {} : { thinking: settled.thinking }\n\t\t\tif (settled.usage !== undefined) {\n\t\t\t\t// Observe this turn's usage — the result already exists; emit beside the yield.\n\t\t\t\tthis.#emitter.emit('usage', settled.usage, settled.estimate)\n\t\t\t\tyield { category: 'usage', usage: settled.usage, estimate: settled.estimate }\n\t\t\t}\n\t\t\tif (result.tools !== undefined && result.tools.length > 0) {\n\t\t\t\tconst admittedCalls = filterAllowList(names, result.tools, (call) => call.name)\n\t\t\t\tconst authorization = this.#authorize(result.tools, admittedCalls, advertised, held)\n\t\t\t\t// The ask pass runs only for an asked call, so a turn with no `ask` verdict adds no `await`.\n\t\t\t\tif (authorization.asked.length > 0 && !abort.signal.aborted) {\n\t\t\t\t\tyield* this.#approve(tools, authorization, abort.signal)\n\t\t\t\t}\n\t\t\t\tif (!abort.signal.aborted) this.#confirm(tools, authorization, advertised)\n\t\t\t\tif (abort.signal.aborted) {\n\t\t\t\t\tturns.push({ ...settled, outcome: 'cancel', partial: true })\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst assistant = conversation.add({\n\t\t\t\t\trole: 'assistant',\n\t\t\t\t\tcontent: result.content,\n\t\t\t\t\tcalls: result.tools,\n\t\t\t\t\t...recorded,\n\t\t\t\t})\n\t\t\t\tconst results = await this.#dispatch(\n\t\t\t\t\ttools,\n\t\t\t\t\tresult.tools,\n\t\t\t\t\tabort.signal,\n\t\t\t\t\tauthorization,\n\t\t\t\t\tadvertised,\n\t\t\t\t)\n\t\t\t\t// Each dispatched call's tool message, appended per step of a loop that yields.\n\t\t\t\tconst replies: Message[] = []\n\t\t\t\tfor (const [position, call] of result.tools.entries()) {\n\t\t\t\t\tconst dispatched = results[position]\n\t\t\t\t\tif (dispatched === undefined) continue\n\t\t\t\t\tconst { outcome, content } = settleToolResult(dispatched)\n\t\t\t\t\t// Observe the dispatched tool + its result — beside the `tool` yield (the result\n\t\t\t\t\t// already exists). Carries the same pair the chunk carries.\n\t\t\t\t\tthis.#emitter.emit('tool', call, outcome)\n\t\t\t\t\tyield { category: 'tool', call, result: outcome }\n\t\t\t\t\t// `call` comes from the paired call rather than the result's `id`, so tool message\n\t\t\t\t\t// `position` names `calls[position]` even when a supplied tool manager returns another id.\n\t\t\t\t\treplies.push(conversation.add({ role: 'tool', content, call: call.id }))\n\t\t\t\t}\n\t\t\t\t// The next turn's admission measures and folds the grown prompt, so a final answer never\n\t\t\t\t// pays for a fold. A tools turn's truncation does not end the run, so it is not recorded.\n\t\t\t\tturns.push({\n\t\t\t\t\t...settled,\n\t\t\t\t\tmessages: [...admitted.messages, assistant, ...replies],\n\t\t\t\t\toutcome: 'tools',\n\t\t\t\t\tpartial: true,\n\t\t\t\t})\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// No tools: this turn's content is the final answer. A truncated answer is the model's stop,\n\t\t\t// and a cancel already settles the run, so neither meets the schema.\n\t\t\tif (schema !== undefined && result.truncated !== true && !abort.signal.aborted) {\n\t\t\t\tconst answered = attempt(() => requireAnswer(result.content, schema))\n\t\t\t\tif (!answered.success) {\n\t\t\t\t\tif (this.#strict) throw answered.error\n\t\t\t\t\tthis.#emitter.emit('fault', answered.error)\n\t\t\t\t}\n\t\t\t}\n\t\t\tconversation.add({ role: 'assistant', content: result.content, ...recorded })\n\t\t\tturns.push({\n\t\t\t\t...settled,\n\t\t\t\tcontent: result.content,\n\t\t\t\toutcome: 'answer',\n\t\t\t\tpartial: abort.signal.aborted,\n\t\t\t\ttruncated: result.truncated === true,\n\t\t\t})\n\t\t}\n\t\tconst final = turns.at(-1)\n\t\tconst thinkings = turns.flatMap((turn) => (turn.thinking === undefined ? [] : [turn.thinking]))\n\t\treturn {\n\t\t\tcontent:\n\t\t\t\tfinal?.outcome === 'answer' ? final.content : turns.map((turn) => turn.content).join(''),\n\t\t\tthinking: thinkings.length === 0 ? undefined : thinkings.join('\\n\\n'),\n\t\t\t// Every turn's usage, then the entry selection's, then each rebuild's, in turn order.\n\t\t\tusage: [\n\t\t\t\t...turns.map((turn) => turn.usage),\n\t\t\t\tentry.usage,\n\t\t\t\t...turns.map((turn) => turn.spent),\n\t\t\t].reduce<TokenUsage | undefined>(\n\t\t\t\t(total, one) => (one === undefined ? total : sumUsage(total, one)),\n\t\t\t\tundefined,\n\t\t\t),\n\t\t\tpartial: final?.partial ?? abort.signal.aborted,\n\t\t\t// Every allowed turn requested tools; a cancel during the last one is an `abort`, never an\n\t\t\t// `exhaust`, so the signal is read here.\n\t\t\texhausted: final?.outcome === 'tools' && !abort.signal.aborted,\n\t\t\tstop: inferStop(abort.signal.reason, allowance, budget, this.#provider.id, final),\n\t\t}\n\t}\n\n\t// Settles one provider call into its turn's record fields, charges the cost budget at the one\n\t// charge site, then rethrows a genuine failure. A cancelled call's fields still describe what it\n\t// streamed; the caller adds the ending.\n\t#reconcile(\n\t\tprovided: Result<ProviderResult>,\n\t\tabort: AbortInterface,\n\t\tstreamed: readonly string[],\n\t\tadmitted: Pick<AgentTurn, 'messages' | 'spent' | 'futile'>,\n\t\tmeter: MeterInterface | undefined,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\t// Read before any reconcile: an abort seen here landed while the call ran, and a charge that\n\t\t// trips a bound later in this method cannot turn a genuine failure into a partial.\n\t\tconst interrupted = abort.signal.aborted\n\t\t// The abort partial of an interrupted call is the only carrier of its reasoning and usage; the\n\t\t// streamed deltas already hold its content. A genuine failure contributes no usage.\n\t\tconst partial =\n\t\t\t!provided.success && interrupted && isProviderAbortError(provided.error)\n\t\t\t\t? provided.error.partial\n\t\t\t\t: undefined\n\t\tconst surfaced = provided.success ? provided.value.thinking : partial?.thinking\n\t\tconst thinking = surfaced !== undefined && surfaced.length > 0 ? surfaced : undefined\n\t\tconst content = streamed.join('')\n\t\tconst estimate = estimateTokens(content)\n\t\t// A settled result's usage crossed the provider-result door; only an abort partial is sanitized.\n\t\tconst usage = provided.success\n\t\t\t? provided.value.usage\n\t\t\t: partial?.usage === undefined\n\t\t\t\t? undefined\n\t\t\t\t: sanitizeUsage(partial.usage)\n\t\t// The one charge site for every path, a genuine failure included. The estimate is already on\n\t\t// the cost budget only when the meter charged that same instance; a call that reported no usage\n\t\t// keeps its estimate on the cost budget, unless the meter put it there.\n\t\tconst charged = allowance === budget ? (meter?.charged ?? 0) : 0\n\t\tconst billed =\n\t\t\tusage ??\n\t\t\t(allowance === budget ? undefined : { prompt: 0, completion: estimate, total: estimate })\n\t\tif (billed !== undefined) chargeUsage(budget, billed, charged)\n\t\tif (!provided.success && !interrupted) throw provided.error\n\t\treturn {\n\t\t\tcontent,\n\t\t\tthinking,\n\t\t\tusage,\n\t\t\tspent: admitted.spent,\n\t\t\tmessages: admitted.messages,\n\t\t\tfutile: admitted.futile,\n\t\t\testimate,\n\t\t\tinterrupted,\n\t\t\ttruncated: false,\n\t\t}\n\t}\n\n\t// Composes the exact request one provider dispatch sends: the working messages after the\n\t// provider's replay policy, the advertised tool definitions, and the per-call `think` / `schema`\n\t// options. Keys are omitted when undefined, so the provider receives no tools and no options\n\t// object when there are none. The request owns its messages array, a shallow copy after the\n\t// replay filter, because that filter returns the working array itself for a policy that keeps\n\t// every message. The admission step measures this object and `#provide` sends it, so the\n\t// measured and the dispatched request can never diverge.\n\t#compose(\n\t\tmessages: readonly Message[],\n\t\ttools: ProviderRequest['tools'],\n\t\toptions: ProviderRequest['options'],\n\t): ProviderRequest {\n\t\treturn {\n\t\t\tmessages: [...stripThinking(messages, this.#replay)],\n\t\t\t...(tools === undefined ? {} : { tools }),\n\t\t\t...(options === undefined ? {} : { options }),\n\t\t}\n\t}\n\n\t// Admits one request against the context `window` budget immediately before its provider\n\t// dispatch, so only called when a window is set. `clear()` then consuming the request makes\n\t// `window.consumed` its estimated footprint, and `exhausted` means it has reached `max`. A\n\t// request over the window folds the run's conversation once when it is summarizable and not\n\t// latched futile, then rebuilds through selection, recomposes with the same tool and option\n\t// snapshot, and measures again; the request that measures last is the one dispatched. A\n\t// non-summarizable conversation is measured but never folded, so the auto path never throws the\n\t// `compact()` SUMMARIZER error. A cancel anywhere returns no dispatch so the run settles partial,\n\t// and wins over `strict`; otherwise `strict` refuses a request still over the window with a\n\t// WINDOW error before any dispatch, while `strict: false` sends it.\n\tasync #admit(\n\t\twindow: BudgetInterface<ProviderRequest>,\n\t\tconversation: ConversationInterface,\n\t\tcomposed: ProviderRequest,\n\t\tmessages: readonly Message[],\n\t\trequest: Message | undefined,\n\t\tlatch: boolean,\n\t\tfutile: boolean,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\twindow.clear()\n\t\twindow.consume(composed)\n\t\tif (!window.exhausted) return { dispatch: composed, messages, spent: undefined, futile }\n\t\t// A cancelled run is about to settle, so a fold would spend summarizer time on a request no\n\t\t// provider reads; the next run's first admission folds instead.\n\t\tif (abort.signal.aborted) return { dispatch: undefined, messages, spent: undefined, futile }\n\t\tif (!conversation.summarizable || futile) {\n\t\t\treturn { dispatch: this.#enforce(window, composed), messages, spent: undefined, futile }\n\t\t}\n\t\t// The run signal bounds the fold: `compact()` rejects with its reason on a cancel and\n\t\t// commits nothing, even when the summarizer ignores the signal.\n\t\treturn conversation.compact({ signal: abort.signal }).then(\n\t\t\t(section) =>\n\t\t\t\tthis.#refold(\n\t\t\t\t\tsection,\n\t\t\t\t\twindow,\n\t\t\t\t\tconversation,\n\t\t\t\t\tcomposed,\n\t\t\t\t\tmessages,\n\t\t\t\t\trequest,\n\t\t\t\t\tlatch,\n\t\t\t\t\tabort,\n\t\t\t\t\tbudget,\n\t\t\t\t),\n\t\t\t(error: unknown) => this.#unfold(error, composed, messages, abort),\n\t\t)\n\t}\n\n\t// A fold that settled: a cancel wins; nothing folded latches on a later turn, where the tail had\n\t// its chance to grow, but not on the first; otherwise rebuild through selection (compaction\n\t// changed the view the handler evaluates) and measure the rebuilt request.\n\tasync #refold(\n\t\tsection: Section | undefined,\n\t\twindow: BudgetInterface<ProviderRequest>,\n\t\tconversation: ConversationInterface,\n\t\tcomposed: ProviderRequest,\n\t\tmessages: readonly Message[],\n\t\trequest: Message | undefined,\n\t\tlatch: boolean,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\tif (abort.signal.aborted)\n\t\t\treturn { dispatch: undefined, messages, spent: undefined, futile: false }\n\t\tif (section === undefined) {\n\t\t\treturn {\n\t\t\t\tdispatch: this.#enforce(window, composed),\n\t\t\t\tmessages,\n\t\t\t\tspent: undefined,\n\t\t\t\tfutile: latch,\n\t\t\t}\n\t\t}\n\t\tconst selection = this.#select(conversation, request, abort, budget)\n\t\tconst rebuilt =\n\t\t\tselection === undefined\n\t\t\t\t? { messages: this.#build(conversation), usage: undefined }\n\t\t\t\t: await selection\n\t\tif (abort.signal.aborted) {\n\t\t\treturn {\n\t\t\t\tdispatch: undefined,\n\t\t\t\tmessages: rebuilt.messages,\n\t\t\t\tspent: rebuilt.usage,\n\t\t\t\tfutile: false,\n\t\t\t}\n\t\t}\n\t\tconst dispatch = this.#compose(rebuilt.messages, composed.tools, composed.options)\n\t\twindow.clear()\n\t\twindow.consume(dispatch)\n\t\treturn {\n\t\t\tdispatch: this.#enforce(window, dispatch),\n\t\t\tmessages: rebuilt.messages,\n\t\t\tspent: rebuilt.usage,\n\t\t\tfutile: false,\n\t\t}\n\t}\n\n\t// A fold that rejected: a cancel wins over the failure it caused, as on the select path;\n\t// otherwise surface the summarizer failure observably, rethrow under `strict`, and send the\n\t// over-window request leniently.\n\t#unfold(\n\t\terror: unknown,\n\t\tcomposed: ProviderRequest,\n\t\tmessages: readonly Message[],\n\t\tabort: AbortInterface,\n\t) {\n\t\tif (abort.signal.aborted)\n\t\t\treturn { dispatch: undefined, messages, spent: undefined, futile: false }\n\t\tthis.#emitter.emit('fault', error)\n\t\t// A fault listener can cancel the run synchronously; the cancel then wins over strict, as in #reject.\n\t\tif (abort.signal.aborted)\n\t\t\treturn { dispatch: undefined, messages, spent: undefined, futile: false }\n\t\tif (this.#strict) throw error\n\t\treturn { dispatch: composed, messages, spent: undefined, futile: false }\n\t}\n\n\t// Refuses, under `strict`, a request the window still measures over its max; returns it otherwise.\n\t#enforce(window: BudgetInterface<ProviderRequest>, request: ProviderRequest): ProviderRequest {\n\t\tif (this.#strict && window.exhausted) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'WINDOW',\n\t\t\t\t`the request measures ${window.consumed} against a window of ${window.max} and this run folds no further`,\n\t\t\t\t{ context: { agent: this.#id, consumed: window.consumed, max: window.max } },\n\t\t\t)\n\t\t}\n\t\treturn request\n\t}\n\n\t// One select site: the handler's pending selection settled into the working messages and the\n\t// usage it spent, or `undefined` when there is no request or no handler, so the caller builds from\n\t// `view()` without an await.\n\t#select(\n\t\tconversation: ConversationInterface,\n\t\trequest: Message | undefined,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\tconst pending =\n\t\t\trequest === undefined ? undefined : this.#context.select(conversation, request, abort.signal)\n\t\treturn pending === undefined\n\t\t\t? undefined\n\t\t\t: pending.then(\n\t\t\t\t\t(selection) => this.#adopt(conversation, selection, abort, budget),\n\t\t\t\t\t(error: unknown) => this.#reject(conversation, error, abort),\n\t\t\t\t)\n\t}\n\n\t// A resolved selection: charge its usage before the abort check, because judge calls were spent\n\t// either way and the charge can itself trip the run's bound; then build from it unless a cancel or\n\t// a strict fault intervenes. A cancelled site builds from `view()` and emits no `select`.\n\t#adopt(\n\t\tconversation: ConversationInterface,\n\t\tselection: Selection,\n\t\tabort: AbortInterface,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t) {\n\t\tconst usage = selection.usage === undefined ? undefined : sanitizeUsage(selection.usage)\n\t\tif (usage !== undefined) chargeUsage(budget, usage, 0)\n\t\tif (abort.signal.aborted) return { messages: this.#build(conversation), usage }\n\t\tif (selection.fault !== undefined) {\n\t\t\tthis.#emitter.emit('fault', selection.fault)\n\t\t\tif (abort.signal.aborted) return { messages: this.#build(conversation), usage }\n\t\t\tif (this.#strict) throw selection.fault\n\t\t}\n\t\tconst messages = this.#build(conversation, selection)\n\t\tthis.#emitter.emit('select', selection)\n\t\treturn { messages, usage }\n\t}\n\n\t// A rejected selection: a fault unless cancelled, rethrown under `strict`, then the default build.\n\t#reject(conversation: ConversationInterface, error: unknown, abort: AbortInterface) {\n\t\tif (!abort.signal.aborted) {\n\t\t\tthis.#emitter.emit('fault', error)\n\t\t\t// A `fault` listener can cancel the run synchronously; the cancel then wins over `strict`.\n\t\t\tif (this.#strict && !abort.signal.aborted) throw error\n\t\t}\n\t\treturn { messages: this.#build(conversation), usage: undefined }\n\t}\n\n\t// Every build of a run renders its bound conversation: a selection's messages when one is given,\n\t// else that conversation's `view()`, never the pointer a later `switch` may have moved.\n\t#build(conversation: ConversationInterface, selection?: Selection): readonly Message[] {\n\t\treturn this.#context.build(selection ?? { messages: conversation.view(), judgments: [] })\n\t}\n\n\t// Evaluate policy and emit each verdict before the run's final pre-dispatch abort guard, in call\n\t// order with no `await`: policy callbacks and decision listeners can cancel the run synchronously.\n\t// Only an `allow` verdict enters the allowed map, seated with the instance the turn held when it\n\t// advertised the call; an asked call is queued with that same instance, so a decision applies to\n\t// that instance alone and a tool replaced later is refused.\n\t#authorize(\n\t\tcalls: readonly ToolCall[],\n\t\tadmitted: readonly ToolCall[],\n\t\tadvertised: readonly ToolDefinition[],\n\t\theld: ReadonlyMap<string, ToolInterface | undefined>,\n\t) {\n\t\tconst allowed = new Map<\n\t\t\tnumber,\n\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t>()\n\t\tconst denials = new Map<number, ToolResult>()\n\t\tconst asked: Array<{\n\t\t\treadonly index: number\n\t\t\treadonly call: ToolCall\n\t\t\treadonly decision: AuthorityDecision\n\t\t\treadonly tool: ToolInterface | undefined\n\t\t}> = []\n\t\t// `entries()` visits a hole as `undefined`, as a `for…of` over the calls does.\n\t\tfor (const [index, call] of calls.entries()) {\n\t\t\tconst decided = this.#decide(call, admitted, advertised)\n\t\t\tif (!decided.success) {\n\t\t\t\tdenials.set(index, decided.error)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst decision = decided.value\n\t\t\tif (decision.verdict === 'allow') {\n\t\t\t\tallowed.set(index, { call, tool: held.get(call.name) })\n\t\t\t\tthis.#emitter.emit('allow', call, decision)\n\t\t\t} else if (decision.verdict === 'ask') {\n\t\t\t\tasked.push({ index, call, decision, tool: held.get(call.name) })\n\t\t\t} else {\n\t\t\t\tdenials.set(index, denyCall(call, decision.reason))\n\t\t\t\tthis.#emitter.emit('deny', call, decision.reason, decision)\n\t\t\t}\n\t\t}\n\t\treturn { allowed, denials, asked }\n\t}\n\n\t// Decides one call — scope, then authority, then the `consequential` floor. A scope miss (a call\n\t// the allow-list excludes, or whose name no definition advertises this turn), an evaluate throw,\n\t// and an invalid decision emit `deny` with no decision and fail with the denial; otherwise the\n\t// owned copy of the decision, raised by the advertised definition, succeeds. The scope miss is\n\t// decided before the authority is read, so it never sees a name no definition advertises.\n\t#decide(\n\t\tcall: ToolCall,\n\t\tadmitted: readonly ToolCall[],\n\t\tadvertised: readonly ToolDefinition[],\n\t): Result<AuthorityDecision, ToolResult> {\n\t\tconst definition = admitted.includes(call)\n\t\t\t? advertised.find((advertisement) => advertisement.name === call.name)\n\t\t\t: undefined\n\t\tif (definition === undefined) {\n\t\t\tconst denial = denyCall(call, SCOPE_NOTE)\n\t\t\tthis.#emitter.emit('deny', call, `${call.name} is not in the active scope`, undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\tconst authority = this.#authority\n\t\tif (authority === undefined) {\n\t\t\tconst denial = denyCall(call, undefined)\n\t\t\tthis.#emitter.emit('deny', call, 'no authority is set', undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\t// A security gate must fail closed: if a policy `evaluate` throws, or a member read of its\n\t\t// decision throws, the call is not cleared, so it must not run. Synthesize a denial instead of\n\t\t// letting the throw reject the whole run — the tool stays unexecuted and the model still\n\t\t// receives a denial it can react to, exactly like an explicit `deny`.\n\t\tconst evaluated = attempt(() => {\n\t\t\tconst decision = authority.evaluate({ call })\n\t\t\tif (!isObject(decision)) return undefined\n\t\t\t// The decision is foreign: read each member once into an owned copy, then validate the copy.\n\t\t\tconst { zone, verdict, reason } = decision\n\t\t\tconst copy = { zone, verdict, ...(reason === undefined ? {} : { reason }) }\n\t\t\tif (!isAuthorityDecision(copy)) return undefined\n\t\t\t// The floor reads this turn's advertised definition, so a structural authority cannot skip it.\n\t\t\treturn resolveDecision(copy, definition)\n\t\t})\n\t\tif (!evaluated.success) {\n\t\t\t// The declared `@orkestrel/contract` normalizer, so the `deny` event carries a non-empty\n\t\t\t// explanation: an `Error` with an empty `message` normalizes to real text, and a throw whose\n\t\t\t// stringification itself fails is caught rather than escaping the gate and rejecting the\n\t\t\t// run. The model reads the `denied` note, never the error text.\n\t\t\tconst denial = denyCall(call, undefined)\n\t\t\tthis.#emitter.emit('deny', call, errorToMessage(evaluated.error), undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\tif (evaluated.value === undefined) {\n\t\t\tconst denial = denyCall(call, undefined)\n\t\t\tthis.#emitter.emit('deny', call, 'invalid decision', undefined)\n\t\t\treturn failure(denial)\n\t\t}\n\t\treturn success(evaluated.value)\n\t}\n\n\t// Hands each asked call to the approver in call order, entering handler k+1 only after handler k\n\t// settles, and races each answer against the run signal. Each call's `ask` chunk is yielded after\n\t// its handler is entered and before its answer is awaited, so a reader that sees the chunk finds\n\t// the ask registered. An abort returns at once, so the pending call emits nothing further and no\n\t// later handler is entered; `raceSignal` absorbs a late settle. An approval of a tool that is no\n\t// longer the turn's held instance is denied as unanswered; any other approval seats that instance.\n\tasync *#approve(\n\t\ttools: ToolManagerInterface,\n\t\tauthorization: {\n\t\t\treadonly allowed: Map<\n\t\t\t\tnumber,\n\t\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t\t>\n\t\t\treadonly denials: Map<number, ToolResult>\n\t\t\treadonly asked: ReadonlyArray<{\n\t\t\t\treadonly index: number\n\t\t\t\treadonly call: ToolCall\n\t\t\t\treadonly decision: AuthorityDecision\n\t\t\t\treadonly tool: ToolInterface | undefined\n\t\t\t}>\n\t\t},\n\t\tsignal: AbortSignal,\n\t): AsyncGenerator<AgentChunk, void> {\n\t\tconst authority = this.#authority\n\t\tfor (const { index, call, decision, tool } of authorization.asked) {\n\t\t\tif (signal.aborted) return\n\t\t\tthis.#emitter.emit('ask', call, decision)\n\t\t\t// An `ask` listener can cancel the run before the handler is entered.\n\t\t\tif (signal.aborted) return\n\t\t\t// The approver is foreign: the executor reads it and enters it, so a throwing `ask` getter\n\t\t\t// and a synchronous handler throw both become a rejection, which the failure arm reads as\n\t\t\t// unanswered. An absent approver leaves the ask unanswered with no reason.\n\t\t\tconst answer = new Promise<{\n\t\t\t\treadonly approved: boolean\n\t\t\t\treadonly reason: string | undefined\n\t\t\t\treadonly note: string\n\t\t\t}>((resolve) => {\n\t\t\t\tconst ask = authority?.ask\n\t\t\t\tresolve(\n\t\t\t\t\task === undefined\n\t\t\t\t\t\t? { approved: false, reason: undefined, note: AUTHORITY_NOTES.unanswered }\n\t\t\t\t\t\t: Promise.resolve(ask.call(authority, { call }, decision, signal)).then((value) => ({\n\t\t\t\t\t\t\t\tapproved: value === true,\n\t\t\t\t\t\t\t\treason: undefined,\n\t\t\t\t\t\t\t\tnote: value === true ? AUTHORITY_NOTES.unanswered : AUTHORITY_NOTES.refused,\n\t\t\t\t\t\t\t})),\n\t\t\t\t)\n\t\t\t}).catch((error: unknown) => ({\n\t\t\t\tapproved: false,\n\t\t\t\treason: errorToMessage(error),\n\t\t\t\tnote: AUTHORITY_NOTES.unanswered,\n\t\t\t}))\n\t\t\tyield { category: 'ask', call, decision }\n\t\t\tconst outcome = await raceSignal(answer, signal).catch(() => undefined)\n\t\t\tif (outcome === undefined || signal.aborted) return\n\t\t\tif (outcome.approved && tools.tool(call.name) === tool) {\n\t\t\t\tauthorization.allowed.set(index, { call, tool })\n\t\t\t\tthis.#emitter.emit('allow', call, decision)\n\t\t\t} else {\n\t\t\t\tauthorization.denials.set(index, denyCall(call, outcome.note))\n\t\t\t\tthis.#emitter.emit('deny', call, outcome.reason, decision)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Whether a call's tool is no longer what its decision cleared: the instance the turn held, with the\n\t// `consequential` annotation the turn advertised, which is what the floor read. The compare is by\n\t// identity, so a host that re-adds the same instance still runs. The tool is foreign, so a read that\n\t// throws counts as a change.\n\t#changed(\n\t\ttools: ToolManagerInterface,\n\t\tcall: ToolCall,\n\t\ttool: ToolInterface | undefined,\n\t\tadvertised: readonly ToolDefinition[],\n\t): boolean {\n\t\tconst same = attempt(() => {\n\t\t\tconst current = tools.tool(call.name)\n\t\t\tconst definition = advertised.find((entry) => entry.name === call.name)\n\t\t\treturn (\n\t\t\t\tcurrent === tool &&\n\t\t\t\t(current?.annotations?.consequential === true) ===\n\t\t\t\t\t(definition?.annotations?.consequential === true)\n\t\t\t)\n\t\t})\n\t\treturn !same.success || !same.value\n\t}\n\n\t// Denies each allowed call whose tool changed since its decision, with no `await` and before the\n\t// pre-dispatch abort guard, so a `deny` listener that cancels here stores nothing.\n\t#confirm(\n\t\ttools: ToolManagerInterface,\n\t\tauthorization: {\n\t\t\treadonly allowed: Map<\n\t\t\t\tnumber,\n\t\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t\t>\n\t\t\treadonly denials: Map<number, ToolResult>\n\t\t},\n\t\tadvertised: readonly ToolDefinition[],\n\t): void {\n\t\tfor (const [index, { call, tool }] of authorization.allowed) {\n\t\t\tif (!this.#changed(tools, call, tool, advertised)) continue\n\t\t\tauthorization.allowed.delete(index)\n\t\t\tauthorization.denials.set(index, denyCall(call, AUTHORITY_NOTES.denied))\n\t\t\tthis.#emitter.emit('deny', call, `${call.name} changed after its decision`, undefined)\n\t\t\t// A deny listener can change a tool this walk already read, so walk them all again.\n\t\t\tthis.#confirm(tools, authorization, advertised)\n\t\t\treturn\n\t\t}\n\t}\n\n\t// Carry cancellation into entered handlers, then merge executed results and denials in call\n\t// order. An all-denied turn costs no tool run. The allowed entries are rebuilt in call order,\n\t// because approved calls join the map after the synchronous pass. The manager resolves a tool by\n\t// name when it starts a call, and a handler's synchronous prefix runs before the next start, so\n\t// each call is checked again right before it starts: the check and the manager's lookup read one\n\t// instance, and the manager keeps its own policy for the call.\n\tasync #dispatch(\n\t\ttools: ToolManagerInterface,\n\t\tcalls: readonly ToolCall[],\n\t\tsignal: AbortSignal,\n\t\tauthorization: {\n\t\t\treadonly allowed: ReadonlyMap<\n\t\t\t\tnumber,\n\t\t\t\t{ readonly call: ToolCall; readonly tool: ToolInterface | undefined }\n\t\t\t>\n\t\t\treadonly denials: ReadonlyMap<number, ToolResult>\n\t\t},\n\t\tadvertised: readonly ToolDefinition[],\n\t): Promise<readonly ToolResult[]> {\n\t\tconst { allowed, denials } = authorization\n\t\tconst entries = [...allowed].sort(([left], [right]) => left - right)\n\t\tconst started = entries.map(([, { call, tool }]) => {\n\t\t\tif (!this.#changed(tools, call, tool, advertised)) return tools.execute(call, { signal })\n\t\t\tthis.#emitter.emit('deny', call, `${call.name} changed after its decision`, undefined)\n\t\t\treturn denyCall(call, AUTHORITY_NOTES.denied)\n\t\t})\n\t\tconst executed = started.length > 0 ? await Promise.all(started) : []\n\t\t// Denials and executed results never share an index.\n\t\tconst results = new Map<number, ToolResult>([\n\t\t\t...denials,\n\t\t\t...entries.flatMap(([index], offset) => {\n\t\t\t\tconst result = executed[offset]\n\t\t\t\treturn result === undefined ? [] : [[index, result] as const]\n\t\t\t}),\n\t\t])\n\t\treturn calls.map((call, index) => results.get(index) ?? denyCall(call, undefined))\n\t}\n\n\t// Drive one provider stream turn: read the `channel` of each {@link ProviderDelta} — a `'content'`\n\t// delta is the answer (appended to the turn's `streamed` log, charged through the meter, surfaced\n\t// as a `token` chunk, in that order); a `'thinking'` delta is live reasoning (surfaced as a `think`\n\t// chunk, never metered — reasoning is not answer content) — returning the provider's assembled\n\t// result or its failure as a Result built through the installed `attempt`, so the turn reads a\n\t// provider or meter failure from a value. It sends the composed {@link ProviderRequest}\n\t// unchanged — the messages already carry only the thinking the provider's replay policy permits\n\t// (default `'none'`), and the tools and options are the turn's snapshot, absent when there are\n\t// none. It pulls the provider itself, so no generator layer sits between a delta and the turn.\n\tasync *#provide(\n\t\trequest: ProviderRequest,\n\t\tsignal: AbortSignal,\n\t\tstreamed: string[],\n\t\tmeter: MeterInterface | undefined,\n\t): AsyncGenerator<AgentChunk, Result<ProviderResult>> {\n\t\ttry {\n\t\t\tconst generator = this.#provider.stream(\n\t\t\t\trequest.messages,\n\t\t\t\tsignal,\n\t\t\t\trequest.tools,\n\t\t\t\trequest.options,\n\t\t\t)\n\t\t\tfor (;;) {\n\t\t\t\tconst next = await generator.next()\n\t\t\t\tif (next.done === true) {\n\t\t\t\t\tconst result = next.value\n\t\t\t\t\treturn attempt(() => requireProviderResult(result))\n\t\t\t\t}\n\t\t\t\tconst delta = readProviderDelta(next.value)\n\t\t\t\tif (delta === undefined) {\n\t\t\t\t\t// The run ends here, so the foreign stream is released rather than held to its deadline.\n\t\t\t\t\tvoid generator.return({ content: '' }).catch(() => {})\n\t\t\t\t\tthrow new ProviderError(\n\t\t\t\t\t\t'PROTOCOL',\n\t\t\t\t\t\t`provider error: a delta must carry a channel of 'content' or 'thinking' and string text; received ${preview(next.value)}`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tif (delta.channel === 'content') {\n\t\t\t\t\tstreamed.push(delta.text)\n\t\t\t\t\tmeter?.charge(delta.text)\n\t\t\t\t\tyield { category: 'token', content: delta.text }\n\t\t\t\t} else {\n\t\t\t\t\tyield { category: 'think', content: delta.text }\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\treturn attempt((): ProviderResult => {\n\t\t\t\tthrow error\n\t\t\t})\n\t\t}\n\t}\n\n\t// The parent signal for a run's abort: the external signal, an optional per-run signal\n\t// (composed with, never replacing, the construction `signal`), the deadline, the effective\n\t// budget, and an allowance that is not the budget itself, folded through `AbortSignal.any` —\n\t// or a lone present one, or `undefined` when none. The budget precedes the allowance, so when\n\t// both are exhausted before the run the budget's reason is the run's.\n\t#combineSignals(\n\t\ttimeout: TimeoutInterface | undefined,\n\t\tbudget: BudgetInterface<TokenUsage> | undefined,\n\t\tallowance: BudgetInterface<TokenUsage> | undefined,\n\t\tsignal: AbortSignal | undefined,\n\t): AbortSignal | undefined {\n\t\tconst signals = [\n\t\t\tthis.#signal,\n\t\t\tsignal,\n\t\t\ttimeout?.signal,\n\t\t\tbudget?.signal,\n\t\t\tallowance === budget ? undefined : allowance?.signal,\n\t\t].filter((one): one is AbortSignal => one !== undefined)\n\t\tif (signals.length === 0) return undefined\n\t\tif (signals.length === 1) return signals[0]\n\t\treturn AbortSignal.any(signals)\n\t}\n}\n","import type { AgentJobInput, AgentRegistryInterface, AgentResult } from './types.js'\nimport type { ControllerInterface } from '@orkestrel/workflow'\nimport { settleAgentJob } from './helpers.js'\n\n/**\n * Executes one runner's agent jobs — spawning each unit's declared children on its first attempt\n * only, then rehydrating the unit through a registry with the controller signal and settling it\n * under the shared partial-result policy.\n *\n * @remarks\n * One instance serves one runner, because it records every unit it has fanned out. The runner\n * calls its handler once per attempt with the same unit id, so a retried attempt finds its unit\n * recorded and spawns no child again. Children are fired through the controller without being\n * awaited inline, so a bounded runner frees the parent's slot for them. `createAgentRunner`\n * creates one instance per runner and binds its `execute` method as the runner's handler.\n *\n * @example\n * ```ts\n * import type { AgentJobInput, ProviderInterface } from '@orkestrel/agent'\n * import { AgentFanout, createAgentRegistry } from '@orkestrel/agent'\n * import { createRunner } from '@orkestrel/workflow'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const fanout = new AgentFanout(createAgentRegistry({ providers: { main: provider } }), false)\n * const runner = createRunner({ handler: fanout.execute.bind(fanout), retries: 1 })\n * const child: AgentJobInput = { provider: 'main', messages: [{ role: 'user', content: 'child' }] }\n * const parent: AgentJobInput = { ...child, messages: [{ role: 'user', content: 'parent' }], children: [child] }\n * const results = await runner.execute([parent]) // the parent's result, then the child's\n * ```\n */\nexport class AgentFanout {\n\treadonly #registry: AgentRegistryInterface\n\treadonly #partial: boolean\n\t// The ids of the units whose children this runner already spawned. Copy-on-write, and never\n\t// shrunk: a unit id stays recorded so every later attempt of that unit skips the fan-out.\n\t#spawned: ReadonlySet<string> = new Set<string>()\n\n\t/**\n\t * Creates the fan-out for one runner over a registry and a partial policy.\n\t *\n\t * @param registry - The registry that rehydrates each serializable job\n\t * @param partial - The partial policy. If `true`, a partial result resolves; if `false`, it throws an `AgentJobError`\n\t */\n\tconstructor(registry: AgentRegistryInterface, partial: boolean) {\n\t\tthis.#registry = registry\n\t\tthis.#partial = partial\n\t}\n\n\t/**\n\t * Executes one unit: spawns its declared children unless an earlier attempt of the unit already\n\t * did, then runs the rehydrated agent and settles it under the partial policy.\n\t *\n\t * @param controller - The runner controller for this unit's attempt\n\t * @returns The settled agent result of the unit\n\t * @throws {AgentJobError} Thrown when the run ended partial and the partial policy is `false`\n\t */\n\texecute(controller: ControllerInterface<AgentJobInput, AgentResult>): Promise<AgentResult> {\n\t\tconst children = controller.input.children\n\t\tif (children !== undefined && !this.#spawned.has(controller.id)) {\n\t\t\tthis.#spawned = new Set([...this.#spawned, controller.id])\n\t\t\tfor (const child of children) void controller.spawn(child)\n\t\t}\n\t\treturn settleAgentJob(this.#registry.build(controller.input, controller.signal), this.#partial)\n\t}\n}\n","import type {\n\tAuthorityContext,\n\tAuthorityDecision,\n\tAuthorityHandler,\n\tAuthorityInterface,\n\tAuthorityOptions,\n\tAuthorityRule,\n} from './types.js'\nimport { AgentError } from './errors.js'\nimport { requireFallback, requireRules } from './helpers.js'\n\n/**\n * Gates the agent loop's tool calls — the synchronous policy consulted before each call runs,\n * turning one {@link AuthorityContext} into an {@link AuthorityDecision} by walking the ordered\n * rules first-match-wins and falling back to the required `fallback`, and the approver that answers\n * an `ask` verdict.\n *\n * @remarks\n * - **Refuses at construction.** The constructor throws an `AgentError` coded `'AUTHORITY'` when the\n * `fallback` is missing or malformed, or when a rule has no valid `verdict`, so a policy with a\n * hole in it never runs.\n * - **Ordered, first-match-wins.** `evaluate` walks the configured rules in order and\n * returns the first whose `match(context)` is true as `{ zone, verdict, reason }`.\n * - **Fallback.** When no rule matches, `evaluate` returns the configured `fallback`: a\n * `verdict: 'deny'` fallback makes an allowlist (only matched rules that allow get through), and a\n * `verdict: 'allow'` fallback makes a denylist.\n * - **Consulted before each tool call.** The agent loop calls `evaluate({ call })` for\n * every {@link import('@orkestrel/tool').ToolCall} the model emits; a denied call is fed\n * back to the model as a denial {@link import('@orkestrel/tool').ToolResult} (a `tool`\n * chunk + a tool message) instead of being executed — no tool execution —\n * so the model receives the denial and can react.\n * - **Approver.** `ask` answers an `ask` verdict through the `ask` option's\n * {@link AuthorityHandler}, as a promise for a synchronous or an async handler; a handler\n * throw rejects it. With no handler it rejects with an `AgentError` coded `'APPROVER'`, which\n * the loop reads as an unanswered ask. It never throws synchronously.\n * - **Synchronous.** `evaluate` returns the verdict directly; only `ask` is async.\n * - **Event-free.** A purely functional gate — no Emitter, no events.\n *\n * @example\n * ```ts\n * // A denylist: deny the `delete` tool, allow everything else.\n * const authority = new Authority({\n * \trules: [{ match: (c) => c.call.name === 'delete', zone: 'restricted', verdict: 'deny' }],\n * \tfallback: { zone: 'default', verdict: 'allow' },\n * })\n * authority.evaluate({ call: { id: '1', name: 'delete', arguments: {} } }) // { zone: 'restricted', verdict: 'deny' }\n * authority.evaluate({ call: { id: '2', name: 'add', arguments: {} } }) // { zone: 'default', verdict: 'allow' }\n * ```\n */\nexport class Authority implements AuthorityInterface {\n\treadonly #rules: readonly AuthorityRule[]\n\treadonly #fallback: AuthorityDecision\n\treadonly #handler: AuthorityHandler | undefined\n\n\tconstructor(options: AuthorityOptions) {\n\t\tthis.#fallback = requireFallback(options)\n\t\tthis.#rules = requireRules(options.rules)\n\t\tthis.#handler = options.ask\n\t}\n\n\tevaluate(context: AuthorityContext): AuthorityDecision {\n\t\tfor (const rule of this.#rules) {\n\t\t\tif (rule.match(context))\n\t\t\t\treturn {\n\t\t\t\t\tzone: rule.zone,\n\t\t\t\t\tverdict: rule.verdict,\n\t\t\t\t\t...(rule.reason === undefined ? {} : { reason: rule.reason }),\n\t\t\t\t}\n\t\t}\n\t\treturn this.#fallback\n\t}\n\n\tasync ask(\n\t\tcontext: AuthorityContext,\n\t\tdecision: AuthorityDecision,\n\t\tsignal: AbortSignal,\n\t): Promise<boolean> {\n\t\tif (this.#handler === undefined) {\n\t\t\tthrow new AgentError('APPROVER', 'an asked call needs an approver and none is set', {\n\t\t\t\tcontext: { tool: context.call.name },\n\t\t\t})\n\t\t}\n\t\treturn this.#handler(context, decision, signal)\n\t}\n}\n","import type {\n\tAgentAnswer,\n\tAgentFrame,\n\tApproverInterface,\n\tAuthorityContext,\n\tAuthorityDecision,\n\tAuthorityHandler,\n} from './types.js'\nimport type { ToolCall } from '@orkestrel/tool'\nimport { preview } from '@orkestrel/contract'\nimport { AgentError } from './errors.js'\nimport { renderArguments } from './helpers.js'\n\n/**\n * Holds a server's waiting asks between the run that asked them and the page that answers them — an\n * {@link ApproverInterface} whose `ask` is the authority's handler, keyed by the asked call object.\n *\n * @remarks\n * - **One bound handler.** The constructor binds `ask` once, so `ask: approver.ask` and a\n * destructured `ask` both register on this approver, and every read returns the same function.\n * - **Keyed by the call.** Each waiting ask is found through the call object the run handed the\n * handler, never through `call.id`, which a provider can repeat. The ask mints its own `id`, and\n * its frame carries that `id`, the tool `name`, the decision's `zone`, and the call's `arguments`\n * rendered by {@link renderArguments}.\n * - **Fail closed.** A call framed while no ask waits for it, a second ask for a waiting call, and\n * arguments that cannot be rendered each reject with `AgentError` code `'APPROVER'`, which the\n * loop reads as unanswered. A run abort deletes the waiting ask and resolves it `false`.\n * - **Strict approval.** An answer settles only the ask whose `id` it names, and only\n * `approved: true` approves.\n *\n * @example\n * ```ts\n * const approver = new Approver()\n * const authority = createAuthority({\n * \trules,\n * \tfallback: { zone: 'default', verdict: 'deny' },\n * \task: approver.ask,\n * })\n * approver.answer({ id, approved: true }) // true while that ask waits\n * ```\n */\nexport class Approver implements ApproverInterface {\n\t// The waiting asks, keyed by the call object the run handed the handler; each entry is deleted on\n\t// every path that settles it.\n\treadonly #asks = new Map<\n\t\tToolCall,\n\t\t{\n\t\t\treadonly id: string\n\t\t\treadonly frame: AgentFrame\n\t\t\treadonly resolve: (approved: boolean) => void\n\t\t\treadonly settled: AbortController\n\t\t}\n\t>()\n\t// The calls framed while no ask waited for them; a later ask for one is refused.\n\treadonly #missed = new WeakSet<ToolCall>()\n\t// The handler bound once to this approver, so every read of `ask` returns the same function.\n\treadonly #ask: AuthorityHandler\n\n\tconstructor() {\n\t\tthis.#ask = this.#register.bind(this)\n\t}\n\n\tget ask(): AuthorityHandler {\n\t\treturn this.#ask\n\t}\n\n\tframe(call: ToolCall): AgentFrame | undefined {\n\t\tconst entry = this.#asks.get(call)\n\t\tif (entry === undefined) {\n\t\t\tthis.#missed.add(call)\n\t\t\treturn undefined\n\t\t}\n\t\treturn entry.frame\n\t}\n\n\tanswer(answer: AgentAnswer): boolean {\n\t\tconst found = [...this.#asks].find(([, entry]) => entry.id === answer.id)\n\t\tif (found === undefined) return false\n\t\tconst [call, entry] = found\n\t\tthis.#asks.delete(call)\n\t\tentry.settled.abort()\n\t\tentry.resolve(answer.approved === true)\n\t\treturn true\n\t}\n\n\t// Registers one ask before its first suspension, so a reader that pulls the run's `ask` chunk\n\t// finds it waiting; a run abort deletes it and resolves it `false`.\n\tasync #register(\n\t\tcontext: AuthorityContext,\n\t\tdecision: AuthorityDecision,\n\t\tsignal: AbortSignal,\n\t): Promise<boolean> {\n\t\tconst call = context.call\n\t\tif (signal.aborted) return false\n\t\tif (this.#missed.has(call)) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'APPROVER',\n\t\t\t\t`the ask for ${preview(call.name)} arrived after its chunk was framed; enter the approver synchronously from the authority's ask handler`,\n\t\t\t\t{ context: { tool: preview(call.name) } },\n\t\t\t)\n\t\t}\n\t\tif (this.#asks.has(call)) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'APPROVER',\n\t\t\t\t`an ask for ${preview(call.name)} already waits; ask each call once`,\n\t\t\t\t{ context: { tool: preview(call.name) } },\n\t\t\t)\n\t\t}\n\t\tconst rendered = renderArguments(call.arguments)\n\t\tif (rendered === undefined) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'APPROVER',\n\t\t\t\t`the arguments of ${preview(call.name)} cannot be rendered as JSON; pass JSON arguments`,\n\t\t\t\t{ context: { tool: preview(call.name) } },\n\t\t\t)\n\t\t}\n\t\tconst id = crypto.randomUUID()\n\t\tconst settled = new AbortController()\n\t\tconst pending = Promise.withResolvers<boolean>()\n\t\tthis.#asks.set(call, {\n\t\t\tid,\n\t\t\tframe: { category: 'ask', id, name: call.name, zone: decision.zone, arguments: rendered },\n\t\t\tresolve: pending.resolve,\n\t\t\tsettled,\n\t\t})\n\t\tsignal.addEventListener(\n\t\t\t'abort',\n\t\t\t() => {\n\t\t\t\tthis.#asks.delete(call)\n\t\t\t\tpending.resolve(false)\n\t\t\t},\n\t\t\t{ once: true, signal: settled.signal },\n\t\t)\n\t\treturn pending.promise\n\t}\n}\n","import type {\n\tAgentInterface,\n\tAgentJobInput,\n\tAgentOptions,\n\tAgentRegistryInterface,\n\tAgentRegistryOptions,\n\tAuthorityInterface,\n} from './types.js'\nimport type { ProviderInterface } from '../providers/index.js'\nimport type { ConversationStoreInterface } from '../conversations/index.js'\nimport type { ToolInterface } from '@orkestrel/tool'\nimport type { SchedulerInterface } from '@orkestrel/workflow'\nimport { describeFaults } from '@orkestrel/contract'\nimport { createTokenBudget } from '@orkestrel/budget'\nimport { ToolManager } from '@orkestrel/tool'\nimport { Agent } from './Agent.js'\nimport { agentJobContract } from './contracts.js'\nimport { AgentError } from './errors.js'\nimport { ConversationManager } from '../conversations/index.js'\nimport { requireEntry } from './helpers.js'\n\n/**\n * Makes a durable, JSON-serializable {@link AgentJobInput} runnable — holds the named pools of\n * live, non-serializable pieces (providers, tools, authorities, schedulers), throws on a name\n * absent from its pool, and its `build` method returns a seeded, signal-wired {@link Agent} from a job's names and\n * data.\n *\n * @remarks\n * - **Why it exists.** An `AgentJobInput` is serializable so it can survive a crash in a\n * Queue's store; the live objects it needs (a provider with sockets, tools / rules /\n * schedulers carrying functions) cannot serialize. The registry closes that gap:\n * construct it once with the live pools, then a queue / runner handler calls `build`\n * on each (possibly restored) job to get a ready agent.\n * - **Accessors throw on a miss.** `provider` / `tool` / `authority` / `scheduler` resolve a\n * name against their pool and throw an {@link AgentError} carrying `code: 'REGISTRY'` and\n * the message `unknown <category>: <name>` when it is absent — an unknown name in a\n * rehydrated job is a programmer / config error that must fail loudly at build time, never\n * silently resolve to `undefined` and run an agent missing a dependency.\n * - **`build` rehydrates.** Resolve the job's `provider`; assemble a fresh\n * {@link ToolManager} from the `tools` names, which refuses with `code: 'REGISTRY'` when the job\n * names no `authority`; rebuild the token `budget` from its\n * ceiling (`createTokenBudget({ max })`); resolve the optional `authority` /\n * `scheduler` names; construct the {@link Agent} with `system` / `limit` / `timeout` /\n * the threaded `signal`; seed its context with the job's `messages`; return it. The\n * `signal` is the queue attempt's / runner unit's cancel, so a bounded abort propagates\n * into the agent (which commits a partial — the job's `partial` policy then\n * decides success vs. retry). A job `limit` that is not a positive safe integer throws an\n * {@link AgentError} carrying `code: 'LIMIT'` from the agent's construction.\n * - **Event-free.** A pure resolver — no Emitter, no events.\n *\n * @example\n * ```ts\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const registry = new AgentRegistry({ providers: { main: provider } })\n * const agent = registry.build({ provider: 'main', messages: [{ role: 'user', content: 'Say ok.' }] })\n * const result = await agent.generate()\n * ```\n */\nexport class AgentRegistry implements AgentRegistryInterface {\n\treadonly #providers: ReadonlyMap<string, ProviderInterface>\n\treadonly #tools: ReadonlyMap<string, ToolInterface>\n\treadonly #authorities: ReadonlyMap<string, AuthorityInterface>\n\treadonly #schedulers: ReadonlyMap<string, SchedulerInterface>\n\treadonly #store: ConversationStoreInterface | undefined\n\n\tconstructor(options: AgentRegistryOptions) {\n\t\tthis.#providers = new Map(Object.entries(options.providers))\n\t\tthis.#tools = new Map(Object.entries(options.tools ?? {}))\n\t\tthis.#authorities = new Map(Object.entries(options.authorities ?? {}))\n\t\tthis.#schedulers = new Map(Object.entries(options.schedulers ?? {}))\n\t\tthis.#store = options.store\n\t}\n\n\tprovider(name: string): ProviderInterface {\n\t\treturn requireEntry(this.#providers, 'provider', name)\n\t}\n\n\ttool(name: string): ToolInterface {\n\t\treturn requireEntry(this.#tools, 'tool', name)\n\t}\n\n\tauthority(name: string): AuthorityInterface {\n\t\treturn requireEntry(this.#authorities, 'authority', name)\n\t}\n\n\tscheduler(name: string): SchedulerInterface {\n\t\treturn requireEntry(this.#schedulers, 'scheduler', name)\n\t}\n\n\tbuild(input: AgentJobInput, signal?: AbortSignal): AgentInterface {\n\t\t// A job is an owned record, so it crosses exactly: no key is repaired, and a typo refuses.\n\t\tconst faults = agentJobContract.audit(input)\n\t\tif (faults.length > 0) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'REGISTRY',\n\t\t\t\t`a job must hold only the declared members of AgentJobInput\\n${describeFaults(faults, agentJobContract.schema)}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tconst provider = this.provider(input.provider)\n\t\tconst agent = new Agent(provider, this.#buildOptions(input, signal))\n\t\t// Seed the conversation onto the rehydrated agent's context — the serializable\n\t\t// MessageInputs become stored messages (each id minted by the manager).\n\t\tfor (const message of input.messages ?? []) agent.context.messages.add(message)\n\t\treturn agent\n\t}\n\n\t// Assemble the AgentOptions for one job: a fresh ToolManager loaded from the named\n\t// tools, the rebuilt token budget, the resolved authority / scheduler, plus the data\n\t// fields and the threaded cancel. Optional fields are omitted (not set to `undefined`)\n\t// so the Agent's `?? default` fallbacks behave exactly as for a hand-built agent. When\n\t// the registry carries a conversation store, thread a fresh store-backed\n\t// ConversationManager for this build (a fresh conversation id per build ⇒ no\n\t// collisions in the shared store) — omitted when no store is set, so the shape matches a\n\t// registry with no `store`.\n\t#buildOptions(input: AgentJobInput, signal: AbortSignal | undefined): AgentOptions {\n\t\tconst names = input.tools ?? []\n\t\tif (names.length > 0 && input.authority === undefined) {\n\t\t\tthrow new AgentError(\n\t\t\t\t'REGISTRY',\n\t\t\t\t\"a job that names tools and no authority cannot run them; add authority: 'NAME' to the job and register that NAME under authorities\",\n\t\t\t\t{ context: { tools: names } },\n\t\t\t)\n\t\t}\n\t\tconst budget = input.budget === undefined ? undefined : createTokenBudget({ max: input.budget })\n\t\treturn {\n\t\t\t...(names.length === 0 ? {} : { tools: this.#buildToolManager(names) }),\n\t\t\t...(input.system === undefined ? {} : { system: input.system }),\n\t\t\t...(input.limit === undefined ? {} : { limit: input.limit }),\n\t\t\t...(input.timeout === undefined ? {} : { timeout: input.timeout }),\n\t\t\t...(budget === undefined ? {} : { budget }),\n\t\t\t...(input.authority === undefined ? {} : { authority: this.authority(input.authority) }),\n\t\t\t...(input.scheduler === undefined ? {} : { scheduler: this.scheduler(input.scheduler) }),\n\t\t\t...(this.#store === undefined\n\t\t\t\t? {}\n\t\t\t\t: { conversations: new ConversationManager({ store: this.#store }) }),\n\t\t\t...(signal === undefined ? {} : { signal }),\n\t\t}\n\t}\n\n\t// A fresh tool registry loaded with the named tools (each resolved — an unknown name\n\t// throws). Always a new manager per build, so concurrent jobs never share one.\n\t#buildToolManager(names: readonly string[]): ToolManager {\n\t\tconst manager = new ToolManager()\n\t\tfor (const name of names) manager.add(this.tool(name))\n\t\treturn manager\n\t}\n}\n","import type { ProviderInterface } from '../providers/index.js'\nimport type {\n\tAgentInterface,\n\tAgentJobInput,\n\tAgentOptions,\n\tAgentQueueOptions,\n\tAgentRegistryInterface,\n\tAgentRegistryOptions,\n\tAgentResult,\n\tAgentRunnerOptions,\n\tApproverInterface,\n\tAuthorityInterface,\n\tAuthorityOptions,\n\tChannelInterface,\n} from './types.js'\nimport type { QueueInterface } from '@orkestrel/queue'\nimport type { RunnerInterface } from '@orkestrel/workflow'\nimport { createQueue } from '@orkestrel/queue'\nimport { createRunner } from '@orkestrel/workflow'\nimport { Agent } from './Agent.js'\nimport { AgentFanout } from './AgentFanout.js'\nimport { Channel } from './Channel.js'\nimport { Authority } from './Authority.js'\nimport { Approver } from './Approver.js'\nimport { AgentRegistry } from './AgentRegistry.js'\nimport { extractQueueOptions, handleAgentQueueJob } from './helpers.js'\n\n/**\n * Creates an agent loop — an {@link AgentInterface} composing a\n * {@link ProviderInterface}, its {@link AgentContextInterface}, and a tool registry\n * into a bounded context → provider → tools → repeat turn, exposed as a one-shot\n * `generate` and a live `stream`.\n *\n * @remarks\n * One private loop drives the turn; `generate` awaits the settled result of the same run\n * `stream` exposes and leaves its live chunks undrained, so they can never diverge. Each turn is bounded by one cancel folded from\n * `signal` + `timeout` + `budget` + `allowance` (through `AbortSignal.any`) — any trip (or `abort()`)\n * commits a partial result (the stream's `result` resolves on a cancel, rejects only\n * on a genuine provider / tool error). The `scheduler` paces between turns; tool\n * iteration is capped at `limit`. Default: `DEFAULT_AGENT_LIMIT`. Tools are advertised\n * structurally through `context.tools.definitions()`. Two observation surfaces: the\n * {@link AgentChunk} stream (pull — per-token content, usage, `ask`, and `tool` chunks) and a typed `emitter` (push —\n * lifecycle + `usage` / `tool` / `deny` for fire-and-forget observers).\n *\n * @param provider - The {@link ProviderInterface} the loop drives each turn\n * @param options - Optional `system` / `tools` / `limit` / `timeout` / `budget` /\n * `scheduler` / `signal` (see {@link AgentOptions})\n * @returns A working {@link AgentInterface}\n *\n * @example\n * ```ts\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { createAgent } from '@orkestrel/agent'\n * import { createTokenBudget } from '@orkestrel/budget'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const agent = createAgent(provider, {\n * \tsystem: 'You are concise.',\n * \tbudget: createTokenBudget({ max: 50_000, scope: 'total' }),\n * })\n * agent.context.messages.add({ role: 'user', content: 'Say hi.' })\n *\n * const stream = agent.stream()\n * for await (const chunk of stream.events) {\n * \tif (chunk.category === 'token') process.stdout.write(chunk.content)\n * }\n * const result = await stream.result // { content, usage?, partial }\n * ```\n */\nexport function createAgent(provider: ProviderInterface, options?: AgentOptions): AgentInterface {\n\treturn new Agent(provider, options)\n}\n\n/**\n * Creates an empty unbounded async channel — a {@link ChannelInterface} a producer writes\n * values into (`push`) and ends (`close` / `fail`) regardless of consumption, while a\n * consumer reads them back live through `drain`.\n *\n * @remarks\n * Write and read are decoupled, so the producer never waits for a consumer: an agent's eager\n * pump writes each chunk into one, which is why a run's `result` settles whether or not the\n * live events are drained. A value pushed at an already-parked consumer is delivered, buffered\n * values are yielded before the end is reported, and the first failure wins.\n *\n * @typeParam T - The value type the channel carries\n * @returns A fresh, empty {@link ChannelInterface}\n *\n * @example\n * ```ts\n * import { createChannel } from '@orkestrel/agent'\n *\n * const channel = createChannel<number>()\n * channel.push(1)\n * channel.close()\n * for await (const value of channel.drain()) {\n * \tvalue // 1\n * }\n * ```\n */\nexport function createChannel<T>(): ChannelInterface<T> {\n\treturn new Channel<T>()\n}\n\n/**\n * Creates a policy gate — an {@link AuthorityInterface} the agent loop consults before\n * each tool call runs, evaluating the ordered rules first-match-wins and falling back\n * to the required `fallback` when none match.\n *\n * @remarks\n * `rules` are evaluated in order — the first whose `match` is true decides, and each rule names its\n * `verdict`. When no rule matches, the `fallback` decides: a `verdict: 'deny'` fallback makes an\n * allowlist, and a `verdict: 'allow'` fallback makes a denylist. `ask` answers each `ask` verdict;\n * with none, every ask is refused as unanswered. Wire the result into `createAgent` through\n * `AgentOptions.authority`: a denied call is fed back to the model as a denial `ToolResult` (not\n * executed), so the model can react. Synchronous — `evaluate` returns the verdict directly; only\n * `ask` is async.\n *\n * @param options - The `fallback`, and optional `rules` (ordered) and `ask` (see {@link AuthorityOptions})\n * @returns A working {@link AuthorityInterface}\n * @throws {AgentError} Thrown when the `fallback` is missing or malformed, or a rule has no valid `verdict` (code `'AUTHORITY'`)\n *\n * @example\n * ```ts\n * import { createAgent, createAuthority } from '@orkestrel/agent'\n *\n * // Deny the `delete` tool, allow everything else.\n * const authority = createAuthority({\n * \trules: [{ match: (c) => c.call.name === 'delete', zone: 'restricted', verdict: 'deny' }],\n * \tfallback: { zone: 'default', verdict: 'allow' },\n * })\n * const agent = createAgent(provider, { tools, authority })\n * ```\n */\nexport function createAuthority(options: AuthorityOptions): AuthorityInterface {\n\treturn new Authority(options)\n}\n\n/**\n * Creates an approver — an {@link ApproverInterface} that holds a server's waiting asks between\n * the run that asked them and the page that answers them.\n *\n * @remarks\n * Wire its bound `ask` as the authority's handler, project the run through\n * `agentStreamToFrames(stream, approver)`, and settle each `ask` frame with `answer`. One approver\n * serves one principal: an answer settles only an ask this approver holds.\n *\n * @returns A working {@link ApproverInterface} with no waiting ask\n *\n * @example\n * ```ts\n * const approver = createApprover()\n * const authority = createAuthority({\n * \trules,\n * \tfallback: { zone: 'default', verdict: 'deny' },\n * \task: approver.ask,\n * })\n * const agent = createAgent(provider, { tools, authority })\n * const stream = agent.stream({ prompt: { content: 'Tidy the notes.' } })\n * for await (const frame of agentStreamToFrames(stream, approver)) {\n * \tif (frame.category === 'ask') approver.answer({ id: frame.id, approved: true })\n * }\n * ```\n */\nexport function createApprover(): ApproverInterface {\n\treturn new Approver()\n}\n\n/**\n * Creates a registry of live providers, tools, authorities, and schedulers whose `build` method returns a seeded, signal-wired {@link AgentInterface} from a serializable job.\n *\n * @remarks\n * `providers` is required; `tools` / `authorities` / `schedulers` are optional pools.\n * The accessors (`provider` / `tool` / `authority` / `scheduler`) throw an\n * {@link import('./errors.js').AgentError} carrying `code: 'REGISTRY'` and the message\n * `unknown <category>: <name>` on an unregistered name — a misconfigured or crash-restored\n * job fails loudly rather than running with a missing dependency. `build(input, signal)`\n * resolves the names, rebuilds the token budget from its ceiling, seeds the agent's\n * context with the job's messages, and threads `signal` so a queue / runner abort\n * propagates. This is the bridge that makes durable, serializable agent jobs runnable.\n *\n * @param options - The named pools (see {@link AgentRegistryOptions})\n * @returns A working {@link AgentRegistryInterface}\n *\n * @example\n * ```ts\n * import { createAgentRegistry, createAuthority } from '@orkestrel/agent'\n * import type { ProviderInterface } from '@orkestrel/agent'\n * import { numberShape, objectShape } from '@orkestrel/contract'\n * import { createTool } from '@orkestrel/tool'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * const registry = createAgentRegistry({\n * \tproviders: { main: provider },\n * \ttools: {\n * \t\tadd: createTool({\n * \t\t\tname: 'add',\n * \t\t\tcontract: objectShape({ x: numberShape(), y: numberShape() }),\n * \t\t\texecute: (a) => Number(a.x) + Number(a.y),\n * \t\t}),\n * \t},\n * \tauthorities: { open: createAuthority({ fallback: { zone: 'default', verdict: 'allow' } }) },\n * })\n * const agent = registry.build({\n * \tprovider: 'main',\n * \tmessages: [{ role: 'user', content: 'Hi.' }],\n * \ttools: ['add'],\n * \tauthority: 'open',\n * })\n * ```\n */\nexport function createAgentRegistry(options: AgentRegistryOptions): AgentRegistryInterface {\n\treturn new AgentRegistry(options)\n}\n\n/**\n * Creates a durable, bounded-concurrency agent-job queue — a {@link QueueInterface} over\n * serializable values of {@link AgentJobInput} that composes `createQueue`: each job is rehydrated\n * through the `registry` into a live {@link AgentInterface}, run to its {@link AgentResult},\n * and subjected to the partial-as-configurable-failure policy.\n *\n * @remarks\n * - **Composes the substrate (no new engine).** The handler is the only new logic;\n * bounded `concurrency`, `retries`, the per-attempt `timeout`, and durable persistence\n * through `store` (+ `restore()` after a crash) are all the backing Queue's. `enqueue`\n * returns a per-job promise.\n * - **Durable + serializable.** Because `AgentJobInput` is JSON-serializable, a `store`\n * persists outstanding jobs; `restore()` re-enqueues them and the `registry` rehydrates the\n * live pieces from the names. A job survives a crash only through a store whose rows outlive\n * the process (for example `createDatabaseQueueStore`); a `createMemoryQueueStore` row is\n * gone when the process exits.\n * - **Partial policy.** A partial result throws an\n * {@link import('./errors.js').AgentJobError} by default, so a job cancelled by its\n * attempt deadline / a queue abort retries while attempts remain; `partial: true`\n * resolves the partial as success instead.\n * - **Cancellation threads through.** The handler passes `context.signal` into\n * `registry.build`, so a queue `abort()` or a per-attempt timeout cancels the in-flight\n * agent (which commits a partial → throws → retries / fails per policy).\n *\n * @param options - The `registry`, the `partial` policy, and the substrate knobs\n * (`concurrency` / `retries` / `timeout` / `store`) (see {@link AgentQueueOptions})\n * @returns A {@link QueueInterface} of {@link AgentJobInput} → {@link AgentResult}\n *\n * @example\n * ```ts\n * import type { AgentJobInput, ProviderInterface } from '@orkestrel/agent'\n * import type { QueueStoreInterface } from '@orkestrel/queue'\n * import { createAgentQueue, createAgentRegistry } from '@orkestrel/agent'\n *\n * declare const provider: ProviderInterface // any concrete implementation supplied by the host app\n * declare const store: QueueStoreInterface<AgentJobInput> // a durable store the host supplies\n * const registry = createAgentRegistry({ providers: { main: provider } })\n * const queue = createAgentQueue({ registry, concurrency: 2, retries: 1, store })\n * const result = await queue.enqueue({ provider: 'main', messages: [{ role: 'user', content: 'ok?' }] })\n * ```\n */\nexport function createAgentQueue(\n\toptions: AgentQueueOptions,\n): QueueInterface<AgentJobInput, AgentResult> {\n\tconst { registry, partial = false, store } = options\n\treturn createQueue<AgentJobInput, AgentResult>({\n\t\t...extractQueueOptions(options),\n\t\t...(store === undefined ? {} : { store }),\n\t\thandler: handleAgentQueueJob.bind(undefined, registry, partial),\n\t})\n}\n\n/**\n * Creates an agent-job runner — a {@link RunnerInterface} over serializable\n * values of {@link AgentJobInput} that composes `createRunner` (one-shot, ordered, fail-fast), each unit\n * rehydrated through the `registry` and subjected to the partial policy. The runner also carries\n * sub-agent fan-out: a parent job's handler can call `controller.spawn` with a child job.\n *\n * @remarks\n * - **Composes the substrate (no new engine).** Bounded `concurrency`, `retries`, the\n * per-attempt `timeout`, ordered results, and fail-fast are all the backing Runner's;\n * the handler adds only rehydration + the partial policy.\n * - **Sub-agent fan-out.** Each unit's handler receives a `ControllerInterface` whose\n * `spawn(childJob)` launches a child agent job through the same bounded queue (the\n * child's result joins the run after the declared units, in spawn order). A unit spawns its\n * declared `children` on its first attempt only, so a retried parent never runs a child\n * twice. On a bounded\n * runner, fan out and return — do not await a spawn inline from within the handler (a\n * slot-holding handler awaiting its own spawn can deadlock; see `ControllerInterface`).\n * - **Partial policy + cancellation.** Same as `createAgentQueue`: a partial result\n * throws by default (the run's fail-fast engages), `partial: true` resolves it; the\n * handler threads `controller.signal` into `registry.build`, so a runner abort / a\n * per-attempt timeout cancels the agent.\n *\n * @param options - The `registry`, the `partial` policy, and the substrate knobs\n * (`concurrency` / `retries` / `timeout`) (see {@link AgentRunnerOptions})\n * @returns A {@link RunnerInterface} of {@link AgentJobInput} → {@link AgentResult}\n *\n * @example\n * ```ts\n * import { createAgentRunner, createAgentRegistry } from '@orkestrel/agent'\n *\n * const registry = createAgentRegistry({ providers: { main: provider } })\n * const runner = createAgentRunner({ registry, concurrency: 2 })\n * // Run one job that fans out a child; results hold the parent, then the spawn.\n * const child = { provider: 'main', messages: [{ role: 'user', content: 'child' }] }\n * const parent = { provider: 'main', messages: [{ role: 'user', content: 'parent' }], children: [child] }\n * const results = await runner.execute([parent]) // declared first, then any spawns\n * ```\n */\nexport function createAgentRunner(\n\toptions: AgentRunnerOptions,\n): RunnerInterface<AgentJobInput, AgentResult> {\n\tconst { registry, partial = false } = options\n\tconst fanout = new AgentFanout(registry, partial)\n\treturn createRunner<AgentJobInput, AgentResult>({\n\t\t...extractQueueOptions(options),\n\t\thandler: fanout.execute.bind(fanout),\n\t})\n}\n","import type { LedgerCategory, LedgerNote, LedgerQuestion, LedgerShare } from './types.js'\n\n/**\n * Lists the categories the ledger files a message under, in the order the category question\n * names them — the one list the {@link LedgerCategory} union derives from.\n */\nexport const LEDGER_CATEGORIES = Object.freeze([\n\t'fact',\n\t'rule',\n\t'correction',\n\t'request',\n\t'opinion',\n\t'chatter',\n\t'distractor',\n] as const)\n\n/** Lists the categories whose messages the projection leaves out as quiet. */\nexport const QUIET_CATEGORIES: readonly LedgerCategory[] = Object.freeze(['chatter', 'distractor'])\n\n/** Lists the categories whose messages state what the desk acts on, which the records render. */\nexport const DECISIVE_CATEGORIES: readonly LedgerCategory[] = Object.freeze([\n\t'fact',\n\t'rule',\n\t'correction',\n])\n\n/** Lists the categories that place a message no owner claims on the rules record. */\nexport const PLACED_CATEGORIES: readonly LedgerCategory[] = Object.freeze(['rule', 'correction'])\n\n/** Names the key of the record that holds the rules no owner claims. */\nexport const LEDGER_RULES_KEY = 'rules'\n\n/** Prefixes the key of an owner's record, which the owner's id follows. */\nexport const LEDGER_OWNER_PREFIX = 'owner:'\n\n/** Labels recalled conversation text by its origin. */\nexport const LEDGER_RECALL_LABELS = Object.freeze({\n\trequest: 'Earlier request:',\n\treply: 'Earlier reply:',\n})\n\n/**\n * Supplies the wording of every question the ledger asks its judge.\n *\n * @remarks\n * The wording names a support desk. Fit the cutoffs for the wording and judge you use.\n */\nexport const LEDGER_QUESTIONS: LedgerQuestion = Object.freeze({\n\tcategory: Object.freeze({\n\t\tform: 'choice',\n\t\tinstructions: 'Which category best describes what this support-desk message states?',\n\t\tcriteria: Object.freeze({\n\t\t\tfact: 'States a fact about a customer, an order, an account, the desk, or the day',\n\t\t\trule: 'States a standing rule, policy, or instruction the desk must follow',\n\t\t\tcorrection: 'Corrects, replaces, or withdraws a value or rule stated earlier',\n\t\t\trequest: 'Asks the assistant to do a task or to answer a question',\n\t\t\topinion: 'States a personal view or judgment rather than a fact',\n\t\t\tchatter: 'Talk with the agent that states nothing the desk acts on',\n\t\t\tdistractor: \"A statement about something outside the desk's work\",\n\t\t}),\n\t}),\n\ttopic: 'Does this support-desk message concern the named desk topic?',\n\tamends: Object.freeze({\n\t\tform: 'noul',\n\t\tinstructions:\n\t\t\t'Does the later message replace or withdraw any value or rule the earlier message states?',\n\t\tcriteria: Object.freeze({\n\t\t\ttrue: 'The later message replaces or withdraws at least one value or rule the earlier message states',\n\t\t\tfalse:\n\t\t\t\t'Every value and rule the earlier message states stays in force after the later message',\n\t\t}),\n\t}),\n\tsupersedes: Object.freeze({\n\t\tform: 'noul',\n\t\tinstructions:\n\t\t\t'Does the later message replace or withdraw everything the earlier message states?',\n\t\tcriteria: Object.freeze({\n\t\t\ttrue: 'The later message replaces or withdraws everything the earlier message states',\n\t\t\tfalse: 'Some value or rule the earlier message states stays in force after the later message',\n\t\t}),\n\t}),\n})\n\n/**\n * Supplies the text of each ledger note, worded for a model that answers in its final\n * message.\n */\nexport const LEDGER_NOTES: LedgerNote = Object.freeze({\n\tcue: 'Give your complete answer to the request now, from what you already have.',\n\tresults: 'Your lookups and recalls in this request returned:',\n\trepeat:\n\t\t'You already have this result earlier in this request; give your complete answer now as your final message.',\n\tclosure:\n\t\t'recall is closed for the rest of this request; give your final answer from what you have',\n})\n\n/**\n * Supplies the default prompt and tail shares.\n */\nexport const DEFAULT_LEDGER_SHARE: LedgerShare = Object.freeze({\n\tprompt: 0.7,\n\ttail: 0.35,\n})\n\n/**\n * Caps the tool-iteration turns of a ledger's agent at 8 turns.\n */\nexport const DEFAULT_LEDGER_LIMIT = 8\n\n/**\n * Caps the `recall` calls of one request at 2 calls.\n */\nexport const DEFAULT_RECALL_LIMIT = 2\n\n/**\n * Sets the 5 milliseconds a paced classifier files between host turns.\n *\n * @remarks\n * Time, not a count of steps, so a slice on a slow device is the same 5 milliseconds.\n */\nexport const LEDGER_SLICE = 5\n\n/**\n * Holds back the share of the prompt budget the scale can rise by between calibration and a\n * request's first call.\n *\n * @remarks\n * Across the `ledger-deny` and `ledger-admit` series of 2026-10-08, a\n * request's first-call tokens per estimate unit rose at most from 1.150 at the seed to 1.212, 5.4\n * percent; the ledger divides its prompt budget by 1.06.\n */\nexport const LEDGER_SCALE_DRIFT = 0.06\n\n/**\n * Matches a run of letters and digits, which the ledger reads as one word.\n *\n * @remarks\n * `String.prototype.match` resets the pattern's `lastIndex`, so this shared instance keeps no\n * position between reads.\n */\nexport const LEDGER_WORD = /[\\p{L}\\p{N}]+/gu\n\n/** Matches the logprob decoding failure `invalid or duplicate top logprob token`, which the ledger holds for the ledger's life. */\nexport const DETERMINISTIC_JUDGE_ERROR = /invalid or duplicate top logprob token/\n","import type { LedgerErrorCode } from './types.js'\nimport type { CodedErrorOptions } from '../types.js'\nimport { INTRINSICS, holds, isFunction, isObject } from '@orkestrel/contract'\n\n/**\n * Reports an invalid ledger configuration, failed calibration, or unowned request selection,\n * carrying the machine-readable `code`.\n *\n * @remarks\n * A threshold, share, capacity, limit, topic, lookup, question, or gauge outside its bounds is a\n * programmer error and throws this with the matching {@link LedgerErrorCode}. A calibration call that reports\n * no prompt usage throws this with `'GAUGE'`. A snapshot option that `isLedgerSnapshot` refuses\n * throws this with `'SNAPSHOT'`, its `context` holding the `faults`. Narrow a caught value with {@link isLedgerError} and\n * branch on `error.code`. A selection without an active `respond` call, for a conversation other\n * than the ledger's, or for a request other than that call's request or a ledger note, reports\n * `'REQUEST'` through the selection's fault.\n */\nexport class LedgerError extends Error {\n\t/** Names the machine-readable condition; the {@link LedgerErrorCode} union describes each code. */\n\treadonly code: LedgerErrorCode\n\t/** Holds the values that locate the failure, such as the `faults` of a refused snapshot, or `undefined` when none were supplied. */\n\treadonly context: Readonly<Record<string, unknown>> | undefined\n\n\t/**\n\t * Records the failure condition and its description.\n\t *\n\t * @param code - The machine-readable condition, exposed as `error.code`\n\t * @param message - The human-readable description of the failure\n\t * @param options - The optional {@link CodedErrorOptions}; `cause` reaches `Error.cause` only when present\n\t */\n\tconstructor(code: LedgerErrorCode, message: string, options?: CodedErrorOptions) {\n\t\tsuper(message, options?.cause === undefined ? undefined : { cause: options.cause })\n\t\tthis.name = 'LedgerError'\n\t\tINTRINSICS.define(this, Symbol.for('@orkestrel/agent.LedgerError'), { value: this })\n\t\tthis.code = code\n\t\tthis.context = options?.context\n\t}\n}\n\n/**\n * Narrows an unknown caught value to a {@link LedgerError} through its identity brand and declared members, so a `catch` can\n * branch on its `code`.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns True if `value` is a {@link LedgerError}; false otherwise\n *\n * @example\n * ```ts\n * isLedgerError(new LedgerError('GAUGE', 'calibration reported no prompt usage')) // true\n * ```\n */\nexport function isLedgerError(value: unknown): value is LedgerError {\n\treturn holds(() => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tvalue instanceof Error &&\n\t\t\t\tINTRINSICS.prototype(value) !== Error.prototype &&\n\t\t\t\tINTRINSICS.describe(value, Symbol.for('@orkestrel/agent.LedgerError'))?.value === value\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst code = INTRINSICS.reflect.read(value, 'code')\n\t\tif (\n\t\t\t!(\n\t\t\t\tcode === 'REQUEST' ||\n\t\t\t\tcode === 'THRESHOLD' ||\n\t\t\t\tcode === 'SHARE' ||\n\t\t\t\tcode === 'CAPACITY' ||\n\t\t\t\tcode === 'LIMIT' ||\n\t\t\t\tcode === 'TOPIC' ||\n\t\t\t\tcode === 'LOOKUP' ||\n\t\t\t\tcode === 'GAUGE' ||\n\t\t\t\tcode === 'REFERENCE' ||\n\t\t\t\tcode === 'SNAPSHOT' ||\n\t\t\t\tcode === 'QUESTION'\n\t\t\t)\n\t\t)\n\t\t\treturn false\n\t\tconst context = INTRINSICS.reflect.read(value, 'context')\n\t\treturn context === undefined || isObject(context) || isFunction(context)\n\t})\n}\n","import type { Message } from '../types.js'\nimport type {\n\tGaugeCall,\n\tLedgerAmendment,\n\tLedgerEvidence,\n\tLedgerExchangeSnapshot,\n\tLedgerCategory,\n\tLedgerIndex,\n\tLedgerLine,\n\tLedgerOccurrence,\n\tLedgerLookupFinding,\n\tLedgerLookupState,\n\tLedgerPlanGroup,\n\tLedgerProjection,\n\tLedgerProjectionInput,\n\tLedgerProjectionRequest,\n\tLedgerRecord,\n\tLedgerRegistry,\n\tLedgerRenderContext,\n\tLedgerTopic,\n\tLedgerWordSet,\n\tLedgerReceipt,\n} from './types.js'\nimport type { ToolCall } from '@orkestrel/tool'\nimport { attempt, canonicalStringify, isError, isFiniteNumber, isString } from '@orkestrel/contract'\nimport { estimateMessages } from '../agents/helpers.js'\nimport { extractLiterals, hasCalls, renderJSON } from '../helpers.js'\nimport { isNonNegativeSafeInteger } from '../validators.js'\nimport {\n\tLEDGER_OWNER_PREFIX,\n\tLEDGER_RULES_KEY,\n\tLEDGER_WORD,\n\tPLACED_CATEGORIES,\n} from './constants.js'\nimport { LedgerError } from './errors.js'\n\n/**\n * Digests held messages, lookup call context, stored copies, and sentence receipts using canonical text.\n * This non-cryptographic checksum detects host edits and makes no security claim.\n * @param exchanges - The stored exchange entries\n * @param messages - The conversation holding their messages\n * @returns The hexadecimal checksum, or undefined when a required message is absent or canonical encoding fails\n * @example\n * ```ts\n * digestLedgerWindow([], []) // a deterministic hexadecimal checksum\n * ```\n */\nexport function digestLedgerWindow(\n\texchanges: readonly LedgerExchangeSnapshot[],\n\tmessages: readonly Message[],\n): string | undefined {\n\tconst byId = new Map(messages.map((message) => [message.id, message]))\n\tconst ids = new Set(\n\t\texchanges.flatMap((entry) => [\n\t\t\tentry.request,\n\t\t\tentry.reply,\n\t\t\t...entry.lines.map((line) => line.source),\n\t\t]),\n\t)\n\tconst held = [...ids].map((id) => byId.get(id))\n\tconst callers = messages.filter(\n\t\t(message) => message.role === 'tool' || message.calls !== undefined,\n\t)\n\tconst encoded = attempt(() => {\n\t\tconst values = [...held, ...callers].map((message) => canonicalStringify(message))\n\t\treturn values.some((value) => value === undefined)\n\t\t\t? undefined\n\t\t\t: canonicalStringify([values, exchanges.map((entry) => [entry.copy ?? null, entry.lines])])\n\t})\n\tif (!encoded.success || encoded.value === undefined) return undefined\n\tconst text = encoded.value\n\tconst hash = [...text].reduce(\n\t\t(prior, character) => {\n\t\t\tconst code = character.codePointAt(0) ?? 0\n\t\t\treturn {\n\t\t\t\tfirst: Math.imul(prior.first ^ code, 16777619) >>> 0,\n\t\t\t\tsecond: Math.imul(prior.second ^ code, 16777619) >>> 0,\n\t\t\t}\n\t\t},\n\t\t{ first: 2166136261, second: 3335557771 },\n\t)\n\treturn hash.first.toString(16).padStart(8, '0') + hash.second.toString(16).padStart(8, '0')\n}\n\n/**\n * Collects stored messages eligible to open the recall offer.\n * @remarks\n * Notes and findings without a result do not open the offer. User statements, including quiet\n * statements available through recall's word route, can open it; this helper does not filter results.\n * @param input - The messages, findings, and classification\n * @param requests - The marked request ids\n * @param notes - The annotation ids to exclude\n * @returns User statements, lookup findings with results, marked requests, and nonblank call-free replies\n * @example\n * ```ts\n * collectRecallMessages(input, new Set(), new Set()) // eligible stored messages\n * ```\n */\nexport function collectRecallMessages(\n\tinput: LedgerProjectionInput,\n\trequests: ReadonlySet<string>,\n\tnotes: ReadonlySet<string>,\n): readonly Message[] {\n\tconst findings = new Set(\n\t\tinput.findings.filter((finding) => finding.result !== undefined).map((finding) => finding.id),\n\t)\n\treturn input.messages.filter(\n\t\t(message) =>\n\t\t\t!notes.has(message.id) &&\n\t\t\t(requests.has(message.id) ||\n\t\t\t\t(message.role === 'assistant' && !hasCalls(message) && message.content.trim() !== '') ||\n\t\t\t\t(message.role === 'user' && !hasCalls(message)) ||\n\t\t\t\t(message.role === 'tool' && findings.has(message.id))),\n\t)\n}\n\n/**\n * Checks whether an eligible earlier message is absent from the prompt or incompletely covered.\n * A successful finding is held when a finding with the same canonical identity and text is fully received.\n * @param input - The messages, findings, and classification\n * @param request - The current request id; only messages before it count\n * @param requests - The marked request ids\n * @param notes - The annotation ids to exclude\n * @param messages - The seed tail and held exchange messages; lookup stubs retain their rendered text\n * @param lines - The receipts held by the window and the request copy together\n * @returns True if an eligible earlier message is absent or only partly covered; false otherwise\n * @example\n * ```ts\n * hasHiddenRecall(input, request.id, requests, notes, tail, lines) // whether to offer recall\n * ```\n */\nexport function hasHiddenRecall(\n\tinput: LedgerProjectionInput,\n\trequest: string,\n\trequests: ReadonlySet<string>,\n\tnotes: ReadonlySet<string>,\n\tmessages: readonly Message[],\n\tlines: readonly LedgerReceipt[],\n): boolean {\n\tconst start = input.messages.findIndex((message) => message.id === request)\n\tconst before = new Set(input.messages.slice(0, Math.max(0, start)).map((message) => message.id))\n\tconst present = new Map(messages.map((message) => [message.id, message]))\n\tconst receipts = Map.groupBy(lines, (line) => line.source)\n\treturn collectRecallMessages(input, requests, notes).some((message) => {\n\t\tif (!before.has(message.id)) return false\n\t\tif (requests.has(message.id) || message.role === 'assistant') return !present.has(message.id)\n\t\tif (present.get(message.id)?.content === message.content) return false\n\t\tconst finding = input.findings.find((one) => one.id === message.id)\n\t\tconst copies =\n\t\t\tfinding === undefined\n\t\t\t\t? [message.id]\n\t\t\t\t: input.findings\n\t\t\t\t\t\t.filter((one) => before.has(one.id) && matchesLookup(finding, one))\n\t\t\t\t\t\t.map((one) => one.id)\n\t\treturn !copies.some((id) => {\n\t\t\tif (present.get(id)?.content === message.content) return true\n\t\t\tconst held = new Set((receipts.get(id) ?? []).map((line) => line.sentence))\n\t\t\treturn splitSentences(message.content).every((_, at) => held.has(at))\n\t\t})\n\t})\n}\n\n/**\n * Checks whether two successful findings carry the same text for the same canonical lookup.\n * @param left - The finding to compare\n * @param right - The candidate copy\n * @returns True for the same finding or equal canonical identities and text; unkeyable findings match only themselves\n * @example\n * ```ts\n * matchesLookup(finding, finding) // true when it has a result\n * ```\n */\nexport function matchesLookup(left: LedgerLookupFinding, right: LedgerLookupFinding): boolean {\n\tif (left.result === undefined || right.result === undefined || left.text !== right.text)\n\t\treturn false\n\tif (left.id === right.id) return true\n\tconst identity = identifyLookup(left.name, left.arguments)\n\treturn identity !== undefined && identity === identifyLookup(right.name, right.arguments)\n}\n\n/**\n * Collects the live message ids placed in records or left without a record.\n * @param projection - The projected records and unplaced ids\n * @returns The distinct live message ids\n * @example\n * ```ts\n * collectProjectionIds({ records: [], amendments: [], orphans: ['message-1'] }) // Set { 'message-1' }\n * ```\n */\nexport function collectProjectionIds(projection: LedgerProjection): ReadonlySet<string> {\n\treturn new Set([...projection.orphans, ...projection.records.flatMap((record) => record.members)])\n}\n\n/**\n * Scans admitted amendments in breadth-first order without revisiting a message.\n * @param amendments - The later messages attached to each earlier message\n * @param start - The first message to visit\n * @param admit - The predicate that admits a message and its descendants\n * @returns The admitted message ids in visit order\n * @example\n * ```ts\n * scanAmendments(new Map([['first', ['second']]]), 'first', () => true) // ['first', 'second']\n * ```\n */\nexport function scanAmendments(\n\tamendments: ReadonlyMap<string, readonly string[]>,\n\tstart: string,\n\tadmit: (id: string) => boolean,\n): readonly string[] {\n\tconst queue = [start]\n\tconst seen = new Set<string>()\n\tconst admitted: string[] = []\n\tfor (const id of queue) {\n\t\tif (seen.has(id)) continue\n\t\tseen.add(id)\n\t\tif (!admit(id)) continue\n\t\tadmitted.push(id)\n\t\tfor (const later of amendments.get(id) ?? []) queue.push(later)\n\t}\n\treturn admitted\n}\n\n/**\n * Builds the placeholder assistant recall call used to price recall framing.\n * @param topic - The recall topic to price\n * @returns The assistant message carrying the recall call\n * @example\n * ```ts\n * buildRecallMessage('refunds').calls?.[0]?.arguments // { topic: 'refunds' }\n * ```\n */\nexport function buildRecallMessage(topic: string): Message {\n\treturn {\n\t\tid: 'call',\n\t\trole: 'assistant',\n\t\tcontent: '',\n\t\tcalls: [{ id: 'call_00000000', name: 'recall', arguments: { topic } }],\n\t}\n}\n\n/**\n * Builds the copy of a request turn that carries the rendered records ahead of the request.\n * @param request - The stored request message\n * @param records - The rendered records, or empty text when nothing renders\n * @returns The request with its content set to the records, a `## Request` heading, and the request content, or the request itself when the records are empty\n * @example\n * ```ts\n * buildRequestMessage({ id: 'request', role: 'user', content: 'Check BW-5512.' }, '## Records\\n- Refund issued.').content\n * // '## Records\\n- Refund issued.\\n\\n## Request\\nCheck BW-5512.'\n * ```\n */\nexport function buildRequestMessage(request: Message, records: string): Message {\n\tif (records === '') return request\n\treturn { ...request, content: `${records}\\n\\n## Request\\n${request.content}` }\n}\n\n/**\n * Renders topic names as a comma-separated list in their supplied order.\n * @param topics - The configured desk topics\n * @returns The topic names joined by a comma and a space\n * @example\n * ```ts\n * renderTopicNames([{ name: 'refunds', criterion: 'Refund amounts' }]) // 'refunds'\n * ```\n */\nexport function renderTopicNames(topics: readonly LedgerTopic[]): string {\n\treturn topics.map((topic) => topic.name).join(', ')\n}\n\n/**\n * Renders an error and its causes, stopping after 4 entries.\n * @param error - The thrown value to describe\n * @returns The names and messages joined by a cause separator\n * @example\n * ```ts\n * renderCauseChain(new Error('refused', { cause: 'offline' })) // 'Error: refused <- offline'\n * ```\n */\nexport function renderCauseChain(error: unknown): string {\n\t// The error, then at most three causes: each step extends the chain by the last entry's cause.\n\treturn Array.from({ length: 3 })\n\t\t.reduce<readonly unknown[]>(\n\t\t\t(chain) => {\n\t\t\t\tconst last = chain.at(-1)\n\t\t\t\tconst cause = isError(last) ? last.cause : undefined\n\t\t\t\treturn cause === undefined ? chain : [...chain, cause]\n\t\t\t},\n\t\t\terror === undefined ? [] : [error],\n\t\t)\n\t\t.map((one) => (isError(one) ? `${one.name}: ${one.message}` : String(one)))\n\t\t.join(' <- ')\n}\n\n/**\n * Extracts lowercase words and capitalized names that do not open a sentence.\n * @param message - The message whose text supplies the words\n * @returns The distinct words and non-opening names\n * @example\n * ```ts\n * extractWords({ id: 'note', role: 'user', content: 'Ask Odile.' }).names // Set { 'odile' }\n * ```\n */\nexport function extractWords(message: Message): LedgerWordSet {\n\treturn {\n\t\twords: new Set(message.content.toLowerCase().match(LEDGER_WORD) ?? []),\n\t\tnames: new Set(\n\t\t\t[...message.content.matchAll(/(?<![\\p{L}\\p{N}'-])\\p{Lu}\\p{Ll}+(?![\\p{L}\\p{N}-])/gu)]\n\t\t\t\t.filter((match) => {\n\t\t\t\t\tconst before = message.content.slice(0, match.index).trimEnd()\n\t\t\t\t\treturn before !== '' && !/[.!?:;]$/.test(before)\n\t\t\t\t})\n\t\t\t\t.map((match) => match[0].toLowerCase()),\n\t\t),\n\t}\n}\n\n/**\n * Splits a topic part into lowercase words with surrounding punctuation removed.\n * @param part - The topic part to split\n * @returns The nonempty normalized words\n * @example\n * ```ts\n * splitWords('Refunds, BW-5512!') // ['refunds', 'bw-5512']\n * ```\n */\nexport function splitWords(part: string): readonly string[] {\n\treturn part\n\t\t.split(/\\s+/)\n\t\t.map((word) => word.replace(/^[\\p{P}\\p{S}]+|[\\p{P}\\p{S}]+$/gu, '').toLowerCase())\n\t\t.filter((word) => word !== '')\n}\n\n/**\n * Finds the call that owns a tool message within a collected tool group.\n * @param group - The assistant leader followed by its tool results\n * @param message - The tool result to pair; distinct leader ids pair by result id when any result has one\n * @returns The call matching the result's call id, or its position when the leader repeats an id or every result lacks an id; undefined when unpaired\n * @example\n * ```ts\n * findLedgerCall(group, result)?.arguments\n * ```\n */\nexport function findLedgerCall(group: readonly Message[], message: Message): ToolCall | undefined {\n\tconst [leader, ...results] = group\n\tconst at = results.findIndex((result) => result.id === message.id)\n\tif (message.role !== 'tool' || at < 0) return undefined\n\tconst calls = leader?.calls ?? []\n\tconst duplicate = new Set(calls.map((call) => call.id)).size !== calls.length\n\treturn !duplicate && results.some((result) => result.call !== undefined)\n\t\t? calls.find((call) => call.id === message.call)\n\t\t: calls[at]\n}\n\n/**\n * Ranks a source the records render for removal before the next prompt.\n *\n * @param group - The planning group: 1 for a topic match, 2 for an off-topic rule or correction, or 3 for a name match\n * @param unsettled - If `true`, the source is a user message without a decisive category; if `false`, it is another source\n * @param category - The source's recorded category, or undefined when undecided\n * @returns The ascending removal rank: name matches, unsettled sources, off-topic corrections, off-topic rules, then topic matches\n * @remarks\n * The planner removes lower ranks first. Equal ranks 0, 1, and 4 remove lower scores first;\n * every equal rank then removes later conversation positions first.\n * @example\n * ```ts\n * rankLedgerCut(2, false, 'rule') // 3\n * ```\n */\nexport function rankLedgerCut(\n\tgroup: LedgerPlanGroup,\n\tunsettled: boolean,\n\tcategory: LedgerCategory | undefined,\n): number {\n\treturn group === 3 ? 0 : unsettled ? 1 : group === 2 ? (category === 'rule' ? 3 : 2) : 4\n}\n\n/**\n * Finds the first cut in a range whose probe returns a value, by bisection.\n *\n * @param low - The first cut to try; 0 tries that cut alone before any bisection\n * @param high - The end of the range, which the search never probes\n * @param probe - Reads a cut and returns its value when the cut fits, or undefined when it does not\n * @param found - The value of the earliest fitting cut the search has probed\n * @returns The value of the fitting cut the search lands on, or undefined when none fits\n * @remarks\n * A range of `S` cuts takes at most `ceil(log2(S)) + 1` probes and one probe when cut 0 fits. The\n * cut it lands on is the range's first fitting cut while no cut past a fitting cut stops fitting.\n * @example\n * ```ts\n * findLedgerCut(0, 8, (cut) => (cut >= 3 ? cut : undefined)) // 3\n * ```\n */\nexport function findLedgerCut<T>(\n\tlow: number,\n\thigh: number,\n\tprobe: (cut: number) => T | undefined,\n\tfound?: T,\n): T | undefined {\n\tif (low >= high) return found\n\tif (low === 0) return probe(0) ?? findLedgerCut(1, high, probe)\n\tconst middle = Math.floor((low + high) / 2)\n\tconst value = probe(middle)\n\treturn value === undefined\n\t\t? findLedgerCut(middle + 1, high, probe, found)\n\t\t: findLedgerCut(low, middle, probe, value)\n}\n\n/**\n * Builds the cut the given steps leave: each step drops the line it is, by identity, or the source it names.\n *\n * @param steps - The lines and source ids to drop\n * @param records - The records to drop lines from\n * @param included - The source ids to drop sources from\n * @returns The records without the dropped lines and the sources without the dropped ids\n * @example\n * ```ts\n * buildLedgerCut(['a'], [], new Set(['a', 'b'])).included // Set { 'b' }\n * ```\n */\nexport function buildLedgerCut(\n\tsteps: ReadonlyArray<LedgerLine | string>,\n\trecords: readonly LedgerRecord[],\n\tincluded: ReadonlySet<string>,\n): { readonly records: readonly LedgerRecord[]; readonly included: ReadonlySet<string> } {\n\tconst dropped = new Set(steps)\n\treturn {\n\t\trecords: records.map((record) => ({\n\t\t\t...record,\n\t\t\tlines: record.lines.filter((line) => !dropped.has(line)),\n\t\t})),\n\t\tincluded: new Set([...included].filter((source) => !dropped.has(source))),\n\t}\n}\n\n/**\n * Resolves the generation cap and checks it against the context capacity.\n *\n * @param predict - The optional generation cap in tokens; see {@link LedgerOptions}\n * @param capacity - The context capacity in tokens\n * @returns The validated cap\n * @throws {LedgerError} Thrown when the cap is not a nonnegative safe integer less than capacity (code `'CAPACITY'`)\n * @example\n * ```ts\n * resolvePredict(1024, 4096) // 1024\n * ```\n */\nexport function resolvePredict(predict: number | undefined, capacity: number): number {\n\tconst resolved = predict ?? 0\n\tif (!isNonNegativeSafeInteger(resolved) || resolved >= capacity)\n\t\tthrow new LedgerError(\n\t\t\t'CAPACITY',\n\t\t\t'predict must be a nonnegative safe integer less than capacity',\n\t\t)\n\treturn resolved\n}\n\n/**\n * Computes the completion tokens attributable to a message's thinking.\n *\n * @remarks\n * Weights the completion by thinking characters divided by the combined characters of thinking,\n * content, and JSON-serialized calls. Rounds to the nearest integer and caps at the completion.\n * An empty generation yields 0. The completion must be a finite nonnegative integer token count.\n * Calls that cannot be JSON-serialized contribute 0 characters.\n *\n * @param message - The generated thinking, content, and optional calls\n * @param completion - The tokens reported for the completion\n * @returns The thinking share in tokens, or 0 when no thinking characters exist\n *\n * @example\n * ```ts\n * computeThinking({ thinking: 'plan', content: 'ok' }, 10) // 7\n * ```\n */\nexport function computeThinking(\n\tmessage: Pick<Message, 'thinking' | 'content' | 'calls'>,\n\tcompletion: number,\n): number {\n\tconst thinking = message.thinking?.length ?? 0\n\tif (thinking === 0) return 0\n\tconst calls = message.calls === undefined ? 0 : (renderJSON(message.calls)?.length ?? 0)\n\tconst generated = thinking + message.content.length + calls\n\treturn Math.min(completion, Math.round(completion * (thinking / generated)))\n}\n\n/**\n * Splits a message into its sentences.\n *\n * @remarks\n * A sentence ends at a period, question mark, or exclamation mark followed by a space and a capital\n * letter, a digit, or a quote, so a decimal point, an id, or an amount never splits one.\n *\n * @param text - The message text\n * @returns The trimmed, non-empty sentences in order\n *\n * @example\n * ```ts\n * splitSentences('Refunds over $200 need a manager. Ask Odile.')\n * // ['Refunds over $200 need a manager.', 'Ask Odile.']\n * ```\n */\nexport function splitSentences(text: string): readonly string[] {\n\treturn text\n\t\t.split(/(?<=[.!?])\\s+(?=[\\p{Lu}\\d\"'“])/u)\n\t\t.map((sentence) => sentence.trim())\n\t\t.filter((sentence) => sentence !== '')\n}\n\n/**\n * Collects the capitalized name runs of a text, leaving out the run that opens each sentence.\n *\n * @remarks\n * A sentence opens with a capital whatever its first word is, so its opening run proves nothing.\n * This is the reading the person prefix of {@link buildRecords} rests on.\n *\n * @param text - The text to read\n * @returns The capitalized runs in order, repeats included\n *\n * @example\n * ```ts\n * collectNames('Odile phoned. We asked about Odile Marlow.') // ['Odile Marlow']\n * ```\n */\nexport function collectNames(text: string): readonly string[] {\n\treturn splitSentences(text).flatMap((sentence) =>\n\t\t[...sentence.matchAll(/(?<![\\p{L}\\p{N}'-])\\p{Lu}\\p{Ll}+(?: \\p{Lu}\\p{Ll}+)*(?![\\p{L}\\p{N}-])/gu)]\n\t\t\t.filter((match) => (match.index ?? 0) > 0)\n\t\t\t.map((match) => match[0]),\n\t)\n}\n\n/**\n * Normalizes a lookup argument string to the spelling that lookup identity and owner linking compare.\n *\n * @remarks\n * {@link identifyLookup} and {@link linkOwners} read an argument through this one form, so a finding\n * a later lookup replaces and the owner link of its argument agree on the spelling.\n *\n * @param value - The string argument a lookup call carried\n * @returns The argument trimmed and uppercased\n *\n * @example\n * ```ts\n * normalizeArgument(' bw-5512 ') // 'BW-5512'\n * ```\n */\nexport function normalizeArgument(value: string): string {\n\treturn value.trim().toUpperCase()\n}\n\n/**\n * Splits a tool result into its lines at each line feed, with or without a carriage return before it.\n *\n * @remarks\n * The recall listing keys each recalled line by its first line, and the answer note reads a result\n * line by line, so both read lines through this one split.\n *\n * @param text - The tool result text\n * @returns The lines in order, an empty line included; one empty line for an empty text\n *\n * @example\n * ```ts\n * splitResultLines('Account BW-20931.\\r\\nRefund is $148.50.') // ['Account BW-20931.', 'Refund is $148.50.']\n * ```\n */\nexport function splitResultLines(text: string): readonly string[] {\n\treturn text.split(/\\r\\n|\\n/)\n}\n\n/**\n * Renders a lookup call as its tool name and its JSON arguments.\n *\n * @remarks\n * The arguments keep the text `JSON.stringify` writes, key order included. Arguments JSON cannot\n * carry, such as a `BigInt` or a cycle, render the bare tool name instead of throwing.\n *\n * @param name - The lookup tool name\n * @param args - The arguments the call carried\n * @returns The tool name, a space, and the JSON arguments; the bare tool name when JSON cannot carry them\n *\n * @example\n * ```ts\n * renderLookupCall('lookup_order', { id: 'BW-5512' }) // 'lookup_order {\"id\":\"BW-5512\"}'\n * renderLookupCall('lookup_order', { count: 1n }) // 'lookup_order'\n * ```\n */\nexport function renderLookupCall(name: string, args: Readonly<Record<string, unknown>>): string {\n\tconst rendered = renderJSON(args)\n\treturn rendered === undefined ? name : `${name} ${rendered}`\n}\n\n/**\n * Identifies a lookup finding for projection by its tool name and normalized arguments.\n *\n * @remarks\n * Two calls share an identity whatever the key order of their arguments at any depth. A top-level\n * string argument passes through {@link normalizeArgument} first, so `\"lh-1 \"` and `\"LH-1\"` name one call.\n * The ledger's repeat stop instead compares the tool name and canonical arguments without this\n * string normalization, so those argument spellings remain distinct within a request.\n * An uncanonicalizable call has no identity and neither replaces nor is replaced by another finding.\n *\n * @param name - The tool name\n * @param args - The arguments the call carried\n * @returns The name, a space, and the canonical arguments, or `undefined` when uncanonicalizable\n *\n * @example\n * ```ts\n * identifyLookup('lookup_order', { id: 'bw-5512', opts: { tier: 2, region: 'north' } }) ===\n * \tidentifyLookup('lookup_order', { opts: { region: 'north', tier: 2 }, id: ' BW-5512' }) // true\n * ```\n */\nexport function identifyLookup(\n\tname: string,\n\targs: Readonly<Record<string, unknown>>,\n): string | undefined {\n\tconst normalized = Object.fromEntries(\n\t\tObject.entries(args).map(([key, value]) => [\n\t\t\tkey,\n\t\t\tisString(value) ? normalizeArgument(value) : value,\n\t\t]),\n\t)\n\tconst canonical = canonicalStringify(normalized)\n\treturn canonical === undefined ? undefined : `${name} ${canonical}`\n}\n\n/**\n * Links each id-shaped lookup argument to its owner.\n *\n * @remarks\n * An owner argument links to itself. Any other id-shaped argument links to the single owner id its\n * finding's text names. An empty finding links nothing, and a later finding overwrites an earlier\n * link of the same argument.\n *\n * @param findings - The lookup findings in conversation order\n * @param owners - The owners keyed by id\n * @returns The owner id of each linked argument id; an argument whose text names no owner or several is absent\n *\n * @example\n * ```ts\n * linkOwners(\n * \t[{ id: 'tool-1', name: 'lookup_order', arguments: { id: 'BW-5512' }, text: 'Order BW-5512 for account BW-20931.', result: { ids: [], owners: [] } }],\n * \tnew Map([['BW-20931', ['Brightwater Studio']]]),\n * ) // Map { 'BW-5512' => 'BW-20931' }\n * ```\n */\nexport function linkOwners(\n\tfindings: readonly LedgerLookupFinding[],\n\towners: ReadonlyMap<string, readonly string[]>,\n): ReadonlyMap<string, string> {\n\t// A later pair of the same argument overwrites an earlier one, as the `Map` constructor does. The\n\t// spread visits a hole in `findings` as an undefined finding, as iteration does; `flatMap` skips it.\n\treturn new Map(\n\t\t[...findings].flatMap((finding) => {\n\t\t\tif (finding.result === undefined) return []\n\t\t\tconst named = [...extractLiterals(finding.text).ids].filter((id) => owners.has(id))\n\t\t\tconst [only] = named\n\t\t\treturn Object.values(finding.arguments)\n\t\t\t\t.filter(isString)\n\t\t\t\t.map((value) => normalizeArgument(value))\n\t\t\t\t.filter((subject) => extractLiterals(subject).ids.has(subject))\n\t\t\t\t.flatMap((subject) => {\n\t\t\t\t\tif (owners.has(subject)) return [[subject, subject] as const]\n\t\t\t\t\treturn named.length === 1 && only !== undefined ? [[subject, only] as const] : []\n\t\t\t\t})\n\t\t}),\n\t)\n}\n\n/**\n * Collects the ids and the owner names the lookup findings named.\n *\n * @remarks\n * Every finding counts, a replaced one included, because an id a lookup named stays registered.\n * An empty finding names nothing. An owner name is trimmed, and a name without a letter is dropped.\n *\n * @param findings - The lookup findings in conversation order\n * @returns The registry: every id named, and each owner's names in the order read\n *\n * @example\n * ```ts\n * const registry = collectRegistry([\n * \t{ id: 'tool-1', name: 'lookup_customer', arguments: { account: 'BW-20931' }, text: 'Account BW-20931: Brightwater Studio', result: { ids: ['BW-20931'], owners: [{ id: 'BW-20931', names: ['Brightwater Studio'] }] } },\n * ])\n * // registry.owners is Map { 'BW-20931' => ['Brightwater Studio'] }\n * ```\n */\nexport function collectRegistry(findings: readonly LedgerLookupFinding[]): LedgerRegistry {\n\t// The spread and `Array.from` visit a hole in `findings` or in an owner list as an undefined\n\t// entry, as iteration does; `flatMap` and `map` skip it.\n\tconst results = [...findings].flatMap((finding) =>\n\t\tfinding.result === undefined ? [] : [finding.result],\n\t)\n\treturn {\n\t\tids: new Set(\n\t\t\tresults.flatMap((result) => [\n\t\t\t\t...result.ids,\n\t\t\t\t...Array.from(result.owners, (owner) => owner.id),\n\t\t\t]),\n\t\t),\n\t\towners: new Map(\n\t\t\t[\n\t\t\t\t...Map.groupBy(\n\t\t\t\t\tresults.flatMap((result) => Array.from(result.owners)),\n\t\t\t\t\t(owner) => owner.id,\n\t\t\t\t),\n\t\t\t].map(([id, listed]) => [\n\t\t\t\tid,\n\t\t\t\t[\n\t\t\t\t\t...new Set(\n\t\t\t\t\t\tlisted.flatMap((owner) =>\n\t\t\t\t\t\t\towner.names.map((name) => name.trim()).filter((name) => /\\p{L}/u.test(name)),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t],\n\t\t\t]),\n\t\t),\n\t}\n}\n\n/**\n * Normalizes a run of letters and digits to the one form each of its cases folds to.\n *\n * @remarks\n * The word is one run as {@link LEDGER_WORD} matches it, not a `carriers` word split on whitespace.\n * The lowercase comes first and last so a capital sharp s, a long s, and a Greek final sigma each\n * reach the form of their plain letters.\n *\n * @param word - The run to normalize\n * @returns The run's one form\n *\n * @example\n * ```ts\n * normalizeWord('ΟΔΟΣ') // 'οδος'\n * normalizeWord('ſtudio') // 'studio'\n * ```\n */\nexport function normalizeWord(word: string): string {\n\treturn word.toLowerCase().toUpperCase().toLowerCase()\n}\n\n/**\n * Builds a registry's matching index: each name's owners, each word's carriers, the needles, keys, and ranks.\n *\n * @remarks\n * The index holds the registry's own `ids` set. A needle escapes its text's pattern characters and\n * matches at word edges: a whole name in any case, and a capitalized name word that only one name\n * carries in its own case, so a common word that spells a first name does not. `keys` enters each\n * name under its least-carried run of {@link LEDGER_WORD}, a tie going to its first run, and under\n * each word its word needles hold. `ranks` holds each id's and each name's position, so\n * {@link matchEntities} returns registry order whatever order a text names them in.\n *\n * @param registry - The registry to index\n * @returns The index, which {@link matchEntities} reads without building anything\n * @throws {TypeError} Thrown when an owner's name list holds a hole\n *\n * @example\n * ```ts\n * const index = buildLedgerIndex({ ids: new Set(['BW-20931']), owners: new Map([['BW-20931', ['Brightwater Studio']]]) })\n * // index.aliases is Map { 'Brightwater Studio' => ['BW-20931'] }\n * ```\n */\nexport function buildLedgerIndex(registry: LedgerRegistry): LedgerIndex {\n\t// `Array.from` visits a hole in an owner's names as an undefined name, as iteration does; `map` skips it.\n\tconst aliases = new Map(\n\t\t[\n\t\t\t...Map.groupBy(\n\t\t\t\t[...registry.owners].flatMap(([id, names]) => Array.from(names, (name) => ({ name, id }))),\n\t\t\t\t(entry) => entry.name,\n\t\t\t),\n\t\t].map(([name, entries]) => [name, entries.map((entry) => entry.id)]),\n\t)\n\t// A word's carriers are the distinct names it appears in.\n\tconst carriers = new Map(\n\t\t[\n\t\t\t...Map.groupBy(\n\t\t\t\t[...aliases.keys()].flatMap((name) =>\n\t\t\t\t\t[...new Set(name.split(/\\s+/))].map((word) => ({ word, name })),\n\t\t\t\t),\n\t\t\t\t(entry) => entry.word,\n\t\t\t),\n\t\t].map(([word, entries]) => [word, entries.map((entry) => entry.name)]),\n\t)\n\t// Neither flag set carries `g` or `y`, so a shared needle keeps no `lastIndex` between tests.\n\tconst needles = [...aliases.keys()].flatMap((name) =>\n\t\t[\n\t\t\t{ name, needle: name, whole: true },\n\t\t\t...name\n\t\t\t\t.split(/\\s+/)\n\t\t\t\t.filter((word) => /^\\p{Lu}\\p{L}+$/u.test(word) && carriers.get(word)?.length === 1)\n\t\t\t\t.map((word) => ({ name, needle: word, whole: false })),\n\t\t].map((entry) => ({\n\t\t\t...entry,\n\t\t\tpattern: new RegExp(\n\t\t\t\t`(?<![\\\\p{L}\\\\p{N}])${entry.needle.replace(/[.*+?^$()|{}[\\]\\\\]/g, '\\\\$&')}(?![\\\\p{L}\\\\p{N}])`,\n\t\t\t\tentry.whole ? 'iu' : 'u',\n\t\t\t),\n\t\t})),\n\t)\n\tconst unique = Map.groupBy(\n\t\tneedles.filter((entry) => !entry.whole),\n\t\t(entry) => entry.name,\n\t)\n\tconst runs = new Map(\n\t\t[...aliases.keys()].map((name) => [\n\t\t\tname,\n\t\t\t[...new Set((name.match(LEDGER_WORD) ?? []).map((run) => normalizeWord(run)))],\n\t\t]),\n\t)\n\tconst counts = new Map(\n\t\t[...Map.groupBy([...runs.values()].flat(), (run) => run)].map(([run, names]) => [\n\t\t\trun,\n\t\t\tnames.length,\n\t\t]),\n\t)\n\t// A stable sort gives a tie to the run that comes first in the name.\n\tconst keyed = [...runs].flatMap(([name, held]) => {\n\t\tconst anchor = held\n\t\t\t.toSorted((left, right) => (counts.get(left) ?? 0) - (counts.get(right) ?? 0))\n\t\t\t.slice(0, 1)\n\t\tconst words = (unique.get(name) ?? []).map((entry) => normalizeWord(entry.needle))\n\t\treturn [...new Set([...anchor, ...words])].map((key) => ({ key, name }))\n\t})\n\treturn {\n\t\tids: registry.ids,\n\t\taliases,\n\t\tcarriers,\n\t\tnames: new Map(\n\t\t\tneedles.filter((entry) => entry.whole).map((entry) => [entry.name, entry.pattern]),\n\t\t),\n\t\twords: new Map(\n\t\t\t[...unique].map(([name, entries]) => [name, entries.map((entry) => entry.pattern)]),\n\t\t),\n\t\tkeys: new Map(\n\t\t\t[...Map.groupBy(keyed, (entry) => entry.key)].map(([key, entries]) => [\n\t\t\t\tkey,\n\t\t\t\tentries.map((entry) => entry.name),\n\t\t\t]),\n\t\t),\n\t\tranks: {\n\t\t\tids: new Map([...registry.ids].map((id, at) => [id, at])),\n\t\t\tnames: new Map([...aliases.keys()].map((name, at) => [name, at])),\n\t\t},\n\t}\n}\n\n/**\n * Matches the registry ids and owners a text names.\n *\n * @remarks\n * An id counts when the text holds it as an id token. An owner counts when the text holds one of\n * its names whole. With `partial`, a capitalized name word that only one name carries also names\n * that name's owners, matched case-sensitively so a common word that spells a first name does not.\n * The text's words select candidates through `keys`, and the needles confirm each candidate. A\n * pure-ASCII text takes one lowercase, which equals {@link normalizeWord} on ASCII. Any other text\n * folds run by run, because folding it whole would read a final sigma across a run's edge.\n *\n * The lookup carries two limits. U+0345 folds to iota under the needles' `iu` flags but is no\n * letter, so a text or a name using it in iota's place can pass the needle and miss the key. A name\n * holding no letter or digit run has no key and never matches; {@link collectRegistry} keeps no\n * such name, so only a hand-built registry reaches that limit. The fold was proven on Node 22's\n * Unicode data; a browser's tables are unread.\n *\n * @param index - The registry's index, from {@link buildLedgerIndex}\n * @param text - The text to read\n * @param partial - If `true`, a name word only one name carries also names its owners; if `false`, only a whole name does\n * @returns The registry ids and owner ids the text names\n *\n * @example\n * ```ts\n * const index = buildLedgerIndex({ ids: new Set(['BW-20931']), owners: new Map([['BW-20931', ['Brightwater Studio']]]) })\n * matchEntities(index, 'Ask Brightwater about it', true) // Set { 'BW-20931' }\n * matchEntities(index, 'Ask Brightwater about it', false) // Set {}\n * ```\n */\nexport function matchEntities(\n\tindex: LedgerIndex,\n\ttext: string,\n\tpartial: boolean,\n): ReadonlySet<string> {\n\tconst ids = [...extractLiterals(text).ids]\n\t\t.filter((id) => index.ids.has(id))\n\t\t.toSorted((left, right) => (index.ranks.ids.get(left) ?? 0) - (index.ranks.ids.get(right) ?? 0))\n\tif (index.keys.size === 0) return new Set(ids)\n\tconst runs: readonly string[] = /\\P{ASCII}/u.test(text)\n\t\t? (text.match(LEDGER_WORD) ?? []).map((run) => normalizeWord(run))\n\t\t: (text.toLowerCase().match(LEDGER_WORD) ?? [])\n\tconst names = [...new Set(runs.flatMap((run) => index.keys.get(run) ?? []))]\n\t\t.filter(\n\t\t\t(name) =>\n\t\t\t\tindex.names.get(name)?.test(text) === true ||\n\t\t\t\t(partial && (index.words.get(name) ?? []).some((needle) => needle.test(text))),\n\t\t)\n\t\t.toSorted(\n\t\t\t(left, right) => (index.ranks.names.get(left) ?? 0) - (index.ranks.names.get(right) ?? 0),\n\t\t)\n\treturn new Set([...ids, ...names.flatMap((name) => index.aliases.get(name) ?? [])])\n}\n\n/**\n * Projects the owner records and the rules record from a conversation's messages.\n *\n * @remarks\n * Each line is a sentence of a live message, verbatim except that a sentence that opens with a\n * pronoun opens with its party and a colon. {@link collectLive} retains user messages and the\n * earliest finding in each call identity's last run of identical text, subject to exclusions,\n * quiet classification, and supersessions. An undefined result still replaces the earlier finding.\n *\n * A live message joins the owner records its entities name, directly or through a linked lookup\n * argument. A message that names no owner joins where its earlier side of an amendment pair joins,\n * or on the rules record when it is filed as a rule or a correction, and is an orphan otherwise.\n *\n * Every live sentence is retained. Accepted edges are listed in `amendments`. An amender that is\n * superseded points to its newest live successor, or retains its endpoint when none exists.\n *\n * A sentence that opens with a pronoun takes the party named in the sentence before it. The reading\n * trusts capitals, so a capitalized word that is no person, such as a carrier named mid-sentence,\n * is read as the party. This is a documented limit: the prefix carries\n * the follow-up facts the records exist for.\n *\n * @param input - The messages, findings, entities, and classification to project\n * @returns The records with owner records ordered by their first member and the rules record last, the accepted amendments, and the live message ids no record placed\n *\n * @example\n * ```ts\n * const projection = buildRecords({\n * \tsystem: 'You staff the desk.',\n * \texclusions: [],\n * \towners: new Map([['BW-20931', ['Brightwater Studio']]]),\n * \tmessages: [{ id: 'user-1', role: 'user', content: 'Brightwater Studio asked for a refund.' }],\n * \tfindings: [],\n * \tentities: new Map([['user-1', ['BW-20931']]]),\n * \tclassification: { quiet: new Set(), categories: new Map(), topics: new Map(), amendments: new Map(), supersessions: new Map() },\n * })\n * // projection.records[0].title === 'Brightwater Studio (BW-20931)'\n * ```\n */\nexport function buildRecords(input: LedgerProjectionInput): LedgerProjection {\n\tconst position = new Map(input.messages.map((message, at) => [message.id, at]))\n\tconst byId = new Map(input.messages.map((message) => [message.id, message]))\n\tconst live = collectLive(input)\n\tconst links = linkOwners(input.findings, input.owners)\n\tconst amended = new Map(\n\t\t[\n\t\t\t...Map.groupBy(\n\t\t\t\t[...input.classification.amendments].flatMap(([earlier, laters]) =>\n\t\t\t\t\tlaters.map((later) => ({ earlier, later })),\n\t\t\t\t),\n\t\t\t\t(pair) => pair.later,\n\t\t\t),\n\t\t].map(([later, pairs]) => [later, pairs.map((pair) => pair.earlier)]),\n\t)\n\tconst amendments = collectAmendments(input, byId, live)\n\tconst holders = [...input.owners.values()].flat()\n\tconst system = collectNames(input.system)\n\t// The memo belongs to this projection alone: `placeMember` inserts each placement once.\n\tconst settled = new Map<string, ReadonlySet<string>>()\n\tconst placements = live.map((id) => ({\n\t\tid,\n\t\tkeys: placeMember(input, links, amended, id, new Set(), settled),\n\t}))\n\tconst orphans = placements.filter((placement) => placement.keys.size === 0).map(({ id }) => id)\n\tconst members = Map.groupBy(\n\t\tplacements.flatMap(({ id, keys }) => [...keys].map((key) => ({ key, id }))),\n\t\t(entry) => entry.key,\n\t)\n\tconst records = [...members]\n\t\t.map(([key, entries]) => {\n\t\t\tconst ids = entries.map((entry) => entry.id)\n\t\t\tconst lines = ids.flatMap((id) => buildLines(input, byId, id, holders, system))\n\t\t\tconst owner = key.slice(LEDGER_OWNER_PREFIX.length)\n\t\t\tconst holder = input.owners.get(owner)?.[0]\n\t\t\tconst title =\n\t\t\t\tkey === LEDGER_RULES_KEY ? 'Rules' : holder === undefined ? owner : `${holder} (${owner})`\n\t\t\tconst first = position.get(ids[0] ?? '') ?? 0\n\t\t\treturn {\n\t\t\t\tfirst,\n\t\t\t\trecord: {\n\t\t\t\t\tkey,\n\t\t\t\t\ttitle,\n\t\t\t\t\tmembers: ids,\n\t\t\t\t\tlines,\n\t\t\t\t} satisfies LedgerRecord,\n\t\t\t}\n\t\t})\n\t\t.sort(\n\t\t\t(left, right) =>\n\t\t\t\tNumber(left.record.key === LEDGER_RULES_KEY) -\n\t\t\t\t\tNumber(right.record.key === LEDGER_RULES_KEY) ||\n\t\t\t\tleft.first - right.first ||\n\t\t\t\t(left.record.key < right.record.key ? -1 : left.record.key > right.record.key ? 1 : 0),\n\t\t)\n\t\t.map(({ record }) => record)\n\treturn { records, amendments, orphans }\n}\n\n/**\n * Selects a request's records, collapsing owners with equal ordered member ids under titles joined with `; `.\n *\n * @remarks\n * The view holds the owner records the request names in the order it names them, then the rules\n * record with the lines whose topics meet the request's first, each group in position order. The\n * returned records and lines are copies. Owner records with the same member ids in the same order\n * collapse into the first requested record, keeping its key, members, and lines and joining their\n * titles with `; ` before trimming.\n *\n * @param projection - The output of {@link buildRecords}\n * @param request - The owners and the desk topics the request names\n * @returns The selected records; an owner with no record yields none\n *\n * @example\n * ```ts\n * selectRecords(projection, { owners: ['BW-20931'], topics: ['refunds'] }).map((record) => record.key)\n * // ['owner:BW-20931', 'rules']\n * ```\n */\nexport function selectRecords(\n\tprojection: LedgerProjection,\n\trequest: LedgerProjectionRequest,\n): readonly LedgerRecord[] {\n\tconst topics = new Set(request.topics)\n\tconst rules = projection.records.find((one) => one.key === LEDGER_RULES_KEY)\n\t// Lines that meet a request topic lead; each side keeps its position order. `groupBy` visits a\n\t// hole in `lines` as an undefined line, as iteration does; `filter` skips it.\n\tconst sides = Map.groupBy(\n\t\t(rules?.lines ?? []).filter(() => true),\n\t\t(line) => line.topics.some((topic) => topics.has(topic)),\n\t)\n\tconst owners = [...new Set(request.owners)].flatMap((owner) => {\n\t\tconst record = projection.records.find((one) => one.key === `${LEDGER_OWNER_PREFIX}${owner}`)\n\t\treturn record === undefined ? [] : [record]\n\t})\n\tconst groups = Map.groupBy(owners, (record) => JSON.stringify(record.members))\n\treturn [\n\t\t...[...groups.values()].flatMap(([record, ...rest]) =>\n\t\t\trecord === undefined\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\tstructuredClone({\n\t\t\t\t\t\t\t...record,\n\t\t\t\t\t\t\ttitle: [record.title, ...rest.map((one) => one.title)].join('; '),\n\t\t\t\t\t\t}),\n\t\t\t\t\t],\n\t\t),\n\t\t...(rules === undefined\n\t\t\t? []\n\t\t\t: [\n\t\t\t\t\tstructuredClone({\n\t\t\t\t\t\t...rules,\n\t\t\t\t\t\tlines: [...(sides.get(true) ?? []), ...(sides.get(false) ?? [])],\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t]\n}\n\n/**\n * Renders one record as a heading and one list item per line.\n *\n * @remarks\n * The ledger joins rendered records with one blank line.\n *\n * @param record - A record or view with a `title` and `lines`\n * @param context - Rendering facts for optional accepted amendment annotations\n * @returns `## TITLE` followed by `- LINE` for each line\n *\n * @example\n * ```ts\n * renderLedgerRecord({ title: 'Rules', lines: [{ text: 'Refunds need a manager.', source: 'user-1', sentence: 0, topics: [], role: 'user' }] })\n * // '## Rules\\n- Refunds need a manager.'\n * ```\n */\nexport function renderLedgerRecord(\n\trecord: Pick<LedgerRecord, 'title' | 'lines'>,\n\tcontext?: LedgerRenderContext,\n): string {\n\treturn [\n\t\t`## ${record.title}`,\n\t\t...record.lines.flatMap((line) =>\n\t\t\t[context === undefined ? '' : renderLedgerAnnotation(line, context), `- ${line.text}`].filter(\n\t\t\t\t(text) => text !== '',\n\t\t\t),\n\t\t),\n\t].join('\\n')\n}\n\n/**\n * Renders one owner record under a `###` heading, which the records nest under their one `## Records` heading.\n *\n * @param record - An owner record or view with a `title` and `lines`\n * @param context - Rendering facts for optional accepted amendment annotations\n * @returns `### TITLE` followed by `- LINE` for each line\n *\n * @example\n * ```ts\n * renderLedgerPinned({ title: 'Odile Marlow (OM-30418)', lines: [] }) // '### Odile Marlow (OM-30418)'\n * ```\n */\nexport function renderLedgerPinned(\n\trecord: Pick<LedgerRecord, 'title' | 'lines'>,\n\tcontext?: LedgerRenderContext,\n): string {\n\treturn `#${renderLedgerRecord(record, context)}`\n}\n\n/**\n * Derives complete source identities, first occurrences, and local references from the actual lines of the records.\n * @param messages - Original messages whose indexed sentences establish completeness\n * @param lines - Retained lines in rendering order after deduplication and trimming\n * @param amendments - Accepted direct message amendment edges\n * @returns Immutable rendering facts for this retained population\n *\n * @example\n * ```ts\n * buildLedgerRenderContext([], [], []).complete.size // 0\n * ```\n */\nexport function buildLedgerRenderContext(\n\tmessages: readonly Message[],\n\tlines: readonly LedgerLine[],\n\tamendments: readonly LedgerAmendment[],\n): LedgerRenderContext {\n\tconst identities = new Set(lines.map((line) => `${line.source} ${line.sentence}`))\n\treturn {\n\t\tamendments,\n\t\tcomplete: new Set(\n\t\t\tmessages\n\t\t\t\t.filter((message) => {\n\t\t\t\t\tconst sentences = splitSentences(message.content)\n\t\t\t\t\treturn (\n\t\t\t\t\t\tsentences.length > 0 &&\n\t\t\t\t\t\tsentences.every((_, index) => identities.has(`${message.id} ${index}`))\n\t\t\t\t\t)\n\t\t\t\t})\n\t\t\t\t.map((message) => message.id),\n\t\t),\n\t\tfirst: new Map(lines.toReversed().map((line) => [line.source, line])),\n\t\treferences: new Map(\n\t\t\t[...new Set(lines.map((line) => line.source))].map((source, index) => [\n\t\t\t\tsource,\n\t\t\t\tString(index + 1),\n\t\t\t]),\n\t\t),\n\t}\n}\n\n/**\n * Renders a source's reference and its later changes at the source's first retained line.\n * @param line - The retained occurrence to annotate\n * @param context - Actual complete sources, first occurrences, local references, and accepted amendment edges\n * @returns The source's reference and its later-change notes, or empty text for an unrelated or repeated occurrence\n * @throws {LedgerError} Thrown when the context holds no reference for the annotated source or a fully included amender (code `'REFERENCE'`)\n *\n * @example\n * ```ts\n * renderLedgerAnnotation({ text: 'Unchanged.', source: 'user-1', sentence: 0, topics: [], role: 'user' }, buildLedgerRenderContext([], [], [])) // ''\n * ```\n */\nexport function renderLedgerAnnotation(line: LedgerLine, context: LedgerRenderContext): string {\n\tif (context.first.get(line.source) !== line) return ''\n\tconst amendments = context.amendments.filter((pair) => pair.earlier === line.source)\n\tconst endpoint =\n\t\tcontext.complete.has(line.source) &&\n\t\tcontext.amendments.some((pair) => pair.later === line.source && context.first.has(pair.earlier))\n\tif (amendments.length === 0 && !endpoint) return ''\n\tconst included = amendments.filter((pair) => context.complete.has(pair.later))\n\tif (\n\t\tcontext.references.get(line.source) === undefined ||\n\t\tincluded.some((pair) => context.references.get(pair.later) === undefined)\n\t)\n\t\tthrow new LedgerError('REFERENCE', 'Ledger rendering requires retained source references')\n\treturn [\n\t\t`[${context.references.get(line.source)}]`,\n\t\t...included.map((pair) => ` Changed later by [${context.references.get(pair.later)}].`),\n\t\t...(amendments.some((pair) => !context.complete.has(pair.later))\n\t\t\t? [' A later change is not shown.']\n\t\t\t: []),\n\t].join('')\n}\n\n/**\n * Splits a recall topic at its joints.\n *\n * @remarks\n * The joints are a comma, a semicolon, a slash, and the word `and`. A topic with no joint is\n * returned whole, so a model that joins a name and an id recalls each part alone.\n *\n * @param topic - The topic the model asked for\n * @returns The trimmed, non-empty parts when there are at least 2; otherwise the topic itself\n *\n * @example\n * ```ts\n * splitTopic('BW-5512, Odile Marlow and refunds') // ['BW-5512', 'Odile Marlow', 'refunds']\n * ```\n */\nexport function splitTopic(topic: string): readonly string[] {\n\tconst parts = topic\n\t\t.split(/\\s*[,;/]\\s*|\\s+and\\s+/i)\n\t\t.map((part) => part.trim())\n\t\t.filter((part) => part !== '')\n\treturn parts.length > 1 ? parts : [topic]\n}\n\n/**\n * Cuts entries to a room and names how many it left out.\n *\n * @remarks\n * The cut keeps at least one entry. When it leaves entries out, its last line reads\n * `N older items not shown; name a narrower topic to narrow the recall`, which\n * {@link matchesCutLine} recognizes. Pass `more` when the entries aren't in age order, so the line\n * doesn't claim an omitted entry is older than a kept one.\n *\n * @param entries - The entries in the order they are kept\n * @param room - The estimate units the joined entries can take\n * @param kind - What the cut line calls an omitted entry. Default: `older`\n * @returns The kept entries followed by the cut line when any were left out, joined by newlines\n *\n * @example\n * ```ts\n * cutListing(['first entry', 'second entry'], 1) // 'first entry\\n1 older item not shown; name a narrower topic to narrow the recall'\n * ```\n */\nexport function cutListing(\n\tentries: readonly string[],\n\troom: number,\n\tkind: 'older' | 'more' = 'older',\n): string {\n\t// The cut keeps at least one entry, and stops at the first entry whose addition passes the room.\n\tconst stop = [...entries.keys()].findIndex(\n\t\t(at) =>\n\t\t\tat > 0 &&\n\t\t\testimateMessages([\n\t\t\t\t{\n\t\t\t\t\tid: 'recall',\n\t\t\t\t\trole: 'tool',\n\t\t\t\t\tcontent: entries.slice(0, at + 1).join('\\n'),\n\t\t\t\t},\n\t\t\t]) > room,\n\t)\n\tconst kept = entries.slice(0, stop === -1 ? undefined : stop)\n\tconst left = entries.length - kept.length\n\treturn [\n\t\t...kept,\n\t\t...(left > 0\n\t\t\t? [\n\t\t\t\t\t`${left} ${kind} item${left === 1 ? '' : 's'} not shown; name a narrower topic to narrow the recall`,\n\t\t\t\t]\n\t\t\t: []),\n\t].join('\\n')\n}\n\n/**\n * Checks whether a line is the cut line {@link cutListing} writes.\n *\n * @param line - The line to check\n * @returns True if the line is a cut line; false otherwise\n *\n * @example\n * ```ts\n * matchesCutLine('2 older items not shown; name a narrower topic to narrow the recall') // true\n * ```\n */\nexport function matchesCutLine(line: string): boolean {\n\treturn /^\\d+ (?:older|more) items? not shown; /.test(line)\n}\n\n/**\n * Renders the tail stub of a lookup result from an earlier request.\n *\n * @param name - The lookup tool name\n * @param args - The arguments the call carried; arguments JSON cannot carry leave the call as its bare tool name, per {@link renderLookupCall}\n * @param state - What became of the result: `failed`, `empty`, `shown` in the records, or `hidden` from them\n * @returns The stub text, which carries the call and its state\n *\n * @example\n * ```ts\n * renderStub('lookup_order', { id: 'BW-5512' }, 'hidden')\n * // 'lookup_order {\"id\":\"BW-5512\"}: result not shown; call recall with BW-5512'\n * ```\n */\nexport function renderStub(\n\tname: string,\n\targs: Readonly<Record<string, unknown>>,\n\tstate: LedgerLookupState,\n): string {\n\tconst head = renderLookupCall(name, args)\n\tif (state === 'failed') return `${head}: failed`\n\tif (state === 'empty') return `${head}: no record`\n\tif (state === 'shown') return `${head}: result shown under Records below`\n\tconst id = Object.values(args).find(isString)\n\treturn `${head}: result not shown; call recall with ${(id ?? '').trim() || 'its id'}`\n}\n\n/**\n * Fits the marginal tokens one estimate unit adds within a request.\n *\n * @remarks\n * The fit is the least-squares slope of prompt tokens over estimate, taken within each set of calls\n * that advertised the same number of tools and pooled over every group. The estimate counts no tool\n * schema, so a pooled fit across tool counts would read the dropped schemas as a falling rate.\n *\n * @param groups - The calls of each request\n * @returns The slope, or undefined when no set holds 2 calls with a prompt count and an estimate that differ\n *\n * @example\n * ```ts\n * fitSlope([[{ estimate: 100, prompt: 130, tools: 2 }, { estimate: 200, prompt: 260, tools: 2 }]]) // 1.3\n * ```\n */\nexport function fitSlope(groups: ReadonlyArray<readonly GaugeCall[]>): number | undefined {\n\t// The spread visits a hole in `groups` as an undefined group, as iteration does; `flatMap` skips it.\n\tconst terms = [...groups].flatMap((group) =>\n\t\t[...Map.groupBy(group, (call) => call.tools).values()].flatMap((set) => {\n\t\t\tconst points = set.flatMap((call) =>\n\t\t\t\tisFiniteNumber(call.prompt) && call.estimate > 0\n\t\t\t\t\t? [{ estimate: call.estimate, prompt: call.prompt }]\n\t\t\t\t\t: [],\n\t\t\t)\n\t\t\tif (points.length < 2) return []\n\t\t\tconst meanX = points.reduce((sum, point) => sum + point.estimate, 0) / points.length\n\t\t\tconst meanY = points.reduce((sum, point) => sum + point.prompt, 0) / points.length\n\t\t\treturn points.map((point) => ({\n\t\t\t\tspread: (point.estimate - meanX) ** 2,\n\t\t\t\tproduct: (point.estimate - meanX) * (point.prompt - meanY),\n\t\t\t}))\n\t\t}),\n\t)\n\tconst spread = terms.reduce((sum, term) => sum + term.spread, 0)\n\tconst product = terms.reduce((sum, term) => sum + term.product, 0)\n\treturn spread > 0 ? product / spread : undefined\n}\n\n/**\n * Collects the ids of the live messages in conversation order.\n *\n * @remarks\n * A message is live when it is a user message or the first unsuperseded finding in its canonical\n * call identity's last run of identical text, and the input neither excludes it nor files it quiet\n * or superseded. When the whole run is superseded, its first finding reaches that final filter.\n * Different text or an undefined result replaces the earlier run, so an empty lookup leaves the\n * earlier result out of every record. A thrown lookup leaves the earlier result live. A finding\n * with no canonical identity participates in no replacement.\n *\n * @param input - The messages, findings, and classification to read\n * @returns The live message ids\n *\n * @example\n * ```ts\n * collectLive(input) // ['user-1', 'tool-2']\n * ```\n */\nexport function collectLive(input: LedgerProjectionInput): readonly string[] {\n\tconst { classification } = input\n\tconst excluded = new Set(input.exclusions)\n\tconst position = new Map(input.messages.map((message, at) => [message.id, at]))\n\tconst findings = input.findings.filter((finding) => position.has(finding.id))\n\tconst identities = new Map(\n\t\tfindings.map((finding) => [finding.id, identifyLookup(finding.name, finding.arguments)]),\n\t)\n\tconst current = new Set(\n\t\t[...Map.groupBy(findings, (finding) => identities.get(finding.id) ?? finding).values()].flatMap(\n\t\t\t(group) => {\n\t\t\t\tconst ordered = group.toSorted(\n\t\t\t\t\t(left, right) => (position.get(left.id) ?? -1) - (position.get(right.id) ?? -1),\n\t\t\t\t)\n\t\t\t\tconst last = ordered.at(-1)\n\t\t\t\tif (last?.result === undefined) return []\n\t\t\t\tconst boundary = ordered.findLastIndex(\n\t\t\t\t\t(finding) => finding.result === undefined || finding.text !== last.text,\n\t\t\t\t)\n\t\t\t\tconst first =\n\t\t\t\t\tordered\n\t\t\t\t\t\t.slice(boundary + 1)\n\t\t\t\t\t\t.find((finding) => (classification.supersessions.get(finding.id) ?? []).length === 0) ??\n\t\t\t\t\tordered[boundary + 1]\n\t\t\t\treturn first === undefined ? [] : [first.id]\n\t\t\t},\n\t\t),\n\t)\n\treturn [...new Map(input.messages.map((message) => [message.id, message])).values()]\n\t\t.filter(\n\t\t\t(message) => message.role === 'user' || (message.role === 'tool' && current.has(message.id)),\n\t\t)\n\t\t.map((message) => message.id)\n\t\t.filter(\n\t\t\t(id) =>\n\t\t\t\t!excluded.has(id) &&\n\t\t\t\t!classification.quiet.has(id) &&\n\t\t\t\t(classification.supersessions.get(id) ?? []).length === 0,\n\t\t)\n}\n\n/**\n * Places a message on the record keys it joins.\n *\n * @remarks\n * A message joins the owner records its entities name, directly or through a linked lookup argument.\n * A message that names no owner joins where the earlier side of its amendment pair joins, and a\n * message with no earlier side joins the rules record when it is filed as a rule or a correction.\n * A message that is not live still places, because a correction joins where its earlier side would\n * join.\n *\n * With `settled`, the walk returns a placement the map holds without walking again, and records each\n * placement whose walk, its nested walks included, excluded no earlier side through `seen`. Such a\n * walk reaches no cycle. While every `seen` holds only the path that reached the message, as\n * {@link buildRecords} passes it, that path never holds a message the recorded walk reached, so a\n * fresh walk returns the same keys in the same order. A placement a cycle cut stays unrecorded,\n * because its keys depend on the path that reached it. Sharing one map across the members of one\n * input bounds a dense amendment graph to one walk per message.\n *\n * @param input - The owners, entities, and classification to read\n * @param links - The owner of each linked lookup argument, from {@link linkOwners}\n * @param amended - The earlier sides of each message's amendment pairs\n * @param id - The message id\n * @param seen - The ids already visited, which stops a cycle of amendment pairs\n * @param settled - The placements already walked for the same `input`, `links`, and `amended`; the walk adds to it. Default: no map, so every call walks\n * @returns The record keys, empty when the message is an orphan\n *\n * @example\n * ```ts\n * placeMember(input, new Map(), new Map(), 'user-1', new Set()) // Set { 'owner:BW-20931' }\n * ```\n */\nexport function placeMember(\n\tinput: LedgerProjectionInput,\n\tlinks: ReadonlyMap<string, string>,\n\tamended: ReadonlyMap<string, readonly string[]>,\n\tid: string,\n\tseen: ReadonlySet<string>,\n\tsettled?: Map<string, ReadonlySet<string>>,\n): ReadonlySet<string> {\n\tconst known = settled?.get(id)\n\tif (known !== undefined) return known\n\tconst owners = (input.entities.get(id) ?? []).flatMap((entity) => {\n\t\tconst owner = input.owners.has(entity) ? entity : links.get(entity)\n\t\treturn owner === undefined ? [] : [`${LEDGER_OWNER_PREFIX}${owner}`]\n\t})\n\tconst owned = owners.length > 0\n\tconst sides = owned ? [] : (amended.get(id) ?? []).filter((side) => side !== id)\n\tconst earlier = sides.filter((side) => !seen.has(side))\n\tconst category = input.classification.categories.get(id)\n\tconst ruled =\n\t\t!owned && earlier.length === 0 && category !== undefined && PLACED_CATEGORIES.includes(category)\n\tconst keys = new Set([\n\t\t...owners,\n\t\t...earlier.flatMap((side) => [\n\t\t\t...placeMember(input, links, amended, side, new Set([...seen, id]), settled),\n\t\t]),\n\t\t...(ruled ? [LEDGER_RULES_KEY] : []),\n\t])\n\tif (\n\t\tsettled !== undefined &&\n\t\tearlier.length === sides.length &&\n\t\tearlier.every((side) => settled.has(side))\n\t)\n\t\tsettled.set(id, keys)\n\treturn keys\n}\n\n/**\n * Collects accepted amendments of live messages, following superseded amenders to live successors.\n *\n * @remarks\n * An accepted edge relates an earlier live message to an amender without claiming which sentences\n * changed. A superseded amender points to its newest reachable live successor in conversation order,\n * with cycles guarded and duplicate endpoints removed. With no live successor, its endpoint stays.\n * This never restores superseded text. An excluded or quiet user or tool message has no effect.\n *\n * @param input - The exclusions and classification to read\n * @param byId - The messages by id\n * @param live - The live message ids from {@link collectLive}\n * @returns The distinct accepted edges in conversation order\n *\n * @example\n * ```ts\n * collectAmendments(input, byId, ['user-1', 'user-3']) // [{ earlier: 'user-1', later: 'user-3' }]\n * ```\n */\nexport function collectAmendments(\n\tinput: LedgerProjectionInput,\n\tbyId: ReadonlyMap<string, Message>,\n\tlive: readonly string[],\n): readonly LedgerAmendment[] {\n\tconst { classification } = input\n\tconst excluded = new Set(input.exclusions)\n\tconst living = new Set(live)\n\tconst ordered = [...new Set(input.messages.map((message) => message.id))]\n\tconst positions = new Map(ordered.map((id, index) => [id, index]))\n\tconst effective = new Set([\n\t\t...live,\n\t\t...[...byId.values()]\n\t\t\t.filter(\n\t\t\t\t(message) =>\n\t\t\t\t\tmessage.role !== 'assistant' &&\n\t\t\t\t\t!excluded.has(message.id) &&\n\t\t\t\t\t!classification.quiet.has(message.id) &&\n\t\t\t\t\t(classification.supersessions.get(message.id) ?? []).length > 0,\n\t\t\t)\n\t\t\t.map((message) => message.id),\n\t])\n\treturn ordered\n\t\t.filter((id) => living.has(id))\n\t\t.flatMap((earlier) =>\n\t\t\t[\n\t\t\t\t...new Set(\n\t\t\t\t\t(classification.amendments.get(earlier) ?? [])\n\t\t\t\t\t\t.filter((later) => effective.has(later) && positions.has(later))\n\t\t\t\t\t\t.map((later) => {\n\t\t\t\t\t\t\tconst successors = new Set(\n\t\t\t\t\t\t\t\tscanAmendments(classification.supersessions, later, () => true),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\treturn ordered.findLast((id) => living.has(id) && successors.has(id)) ?? later\n\t\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t]\n\t\t\t\t.sort((a, b) => (positions.get(a) ?? 0) - (positions.get(b) ?? 0))\n\t\t\t\t.map((later) => ({ earlier, later })),\n\t\t)\n}\n\n/**\n * Builds the record lines of one message.\n *\n * @remarks\n * Each original indexed sentence becomes a line. A sentence that opens with a pronoun and follows\n * another sentence takes the last name of the sentence before it that is neither an owner name nor\n * a system name, and its line text opens with that party and a colon.\n *\n * @param input - The classification whose topics the lines carry\n * @param byId - The messages by id\n * @param id - The message id\n * @param holders - The owner names, which never serve as a party\n * @param system - The system text's names, which never serve as a party\n * @returns The lines in sentence order; empty when the message is absent\n *\n * @example\n * ```ts\n * buildLines(input, byId, 'user-1', [], []).map((line) => line.text)\n * // ['Odile Marlow phoned about BW-5512.', 'Odile Marlow: She wants a refund.']\n * ```\n */\nexport function buildLines(\n\tinput: LedgerProjectionInput,\n\tbyId: ReadonlyMap<string, Message>,\n\tid: string,\n\tholders: readonly string[],\n\tsystem: readonly string[],\n): readonly LedgerLine[] {\n\tconst message = byId.get(id)\n\tif (message === undefined) return []\n\tconst sentences = splitSentences(message.content)\n\tconst topics = input.classification.topics.get(id) ?? []\n\treturn sentences.flatMap((sentence, at) => {\n\t\tconst party =\n\t\t\t/^(?:He|She|They|His|Her|Their)(?![\\p{L}\\p{N}])/u.test(sentence) && at > 0\n\t\t\t\t? collectNames(sentences[at - 1] ?? '')\n\t\t\t\t\t\t.filter((name) => !holders.includes(name) && !system.includes(name))\n\t\t\t\t\t\t.at(-1)\n\t\t\t\t: undefined\n\t\tconst line = {\n\t\t\ttext: party === undefined ? sentence : `${party}: ${sentence}`,\n\t\t\tsource: id,\n\t\t\tsentence: at,\n\t\t\ttopics: [...topics],\n\t\t\trole: message.role,\n\t\t}\n\t\treturn [party === undefined ? line : { ...line, party }]\n\t})\n}\n\n/**\n * Encodes a sentence's record placement without comparing rendered text.\n * @param line - The record key, title, source, and zero-based sentence index\n * @returns The serialized placement identity\n * @example\n * ```ts\n * buildLedgerLineKey({ key: 'rules', title: 'Rules', source: 'one', sentence: 0 })\n * ```\n */\nexport function buildLedgerLineKey(\n\tline: Pick<LedgerOccurrence, 'key' | 'title' | 'source' | 'sentence'>,\n): string {\n\treturn JSON.stringify([line.key, line.title, line.source, line.sentence])\n}\n\n/**\n * Collects the record's sentence receipts not already held by a window.\n * @param record - The selected record with its original sentence identities\n * @param held - Placement identities already rendered in the window\n * @returns The remaining sentences with their record placement\n * @example\n * ```ts\n * collectLedgerLines({ key: 'rules', title: 'Rules', members: [], lines: [] }, new Set()) // []\n * ```\n */\nexport function collectLedgerLines(\n\trecord: LedgerRecord,\n\theld: ReadonlySet<string>,\n): readonly LedgerOccurrence[] {\n\treturn record.lines\n\t\t.map((line) => ({ ...line, key: record.key, title: record.title }))\n\t\t.filter((line) => !held.has(buildLedgerLineKey(line)))\n}\n\n/**\n * Selects each marked request's unambiguous final call-free reply.\n * @param messages - Stored conversation messages in order\n * @param requests - Marked request ids delimiting reply intervals\n * @param notes - Ledger notes that separate answer passes\n * @returns Eligible final replies keyed by request id\n * @example\n * ```ts\n * collectLedgerReplies([], new Set(), new Set()).size // 0\n * ```\n */\nexport function collectLedgerReplies(\n\tmessages: readonly Message[],\n\trequests: ReadonlySet<string>,\n\tnotes: ReadonlySet<string>,\n): ReadonlyMap<string, Message> {\n\tconst positions = messages.flatMap((message, at) => (requests.has(message.id) ? [at] : []))\n\treturn new Map(\n\t\tpositions.flatMap((position, index) => {\n\t\t\tconst request = messages[position]\n\t\t\tconst turns = messages.slice(position + 1, positions[index + 1])\n\t\t\tconst reply = turns.findLast((message) => message.role === 'assistant' && !hasCalls(message))\n\t\t\tconst answers = turns.flatMap((message, at) =>\n\t\t\t\tmessage.role === 'assistant' && !hasCalls(message) && message.content.trim() !== ''\n\t\t\t\t\t? [at]\n\t\t\t\t\t: [],\n\t\t\t)\n\t\t\tconst certain = answers.every(\n\t\t\t\t(at, offset) =>\n\t\t\t\t\toffset === 0 ||\n\t\t\t\t\tturns.slice((answers[offset - 1] ?? -1) + 1, at).some((message) => notes.has(message.id)),\n\t\t\t)\n\t\t\treturn request !== undefined && certain && reply !== undefined && reply.content.trim() !== ''\n\t\t\t\t? [[request.id, reply] as const]\n\t\t\t\t: []\n\t\t}),\n\t)\n}\n\n/**\n * Captures source bytes and direct amendment edges for a held window.\n * @param messages - Current stored messages\n * @param sources - Requests, replies, and record sources the window holds\n * @param amendments - Accepted direct amendment edges from the current projection\n * @returns Source and edge identities for stale detection\n * @example\n * ```ts\n * collectLedgerEvidence([], new Set(), []).messages.size // 0\n * ```\n */\nexport function collectLedgerEvidence(\n\tmessages: readonly Message[],\n\tsources: ReadonlySet<string>,\n\tamendments: readonly LedgerAmendment[],\n): LedgerEvidence {\n\treturn {\n\t\tmessages: new Set(\n\t\t\tmessages\n\t\t\t\t.filter((message) => sources.has(message.id))\n\t\t\t\t.map((message) => JSON.stringify(['message', message])),\n\t\t),\n\t\tamendments: new Map(\n\t\t\tamendments\n\t\t\t\t.filter((edge) => sources.has(edge.earlier) || sources.has(edge.later))\n\t\t\t\t.map((edge) => [JSON.stringify(['edge', edge.earlier, edge.later]), { ...edge }]),\n\t\t),\n\t}\n}\n","import type { ChoiceAnswer, Judgment, JudgmentInput, LiteralSet, Message } from '../types.js'\nimport type {\n\tClassifierInterface,\n\tClassifierOptions,\n\tClassifierPhase,\n\tClassifierAttempt,\n\tClassifierResult,\n\tLedgerCategory,\n\tLedgerClassification,\n\tLedgerTopic,\n} from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport { errorToMessage, isArray, isError, isString, parseJSONAs } from '@orkestrel/contract'\nimport { raceSignal } from '../conversations/index.js'\nimport { isJudgeAbortError } from '../errors.js'\nimport { extractLiterals, matchesJudgment, renderJSON, sumUsage } from '../helpers.js'\nimport { isJudgeError } from '../providers/errors.js'\nimport {\n\tDECISIVE_CATEGORIES,\n\tDETERMINISTIC_JUDGE_ERROR,\n\tLEDGER_CATEGORIES,\n\tLEDGER_SLICE,\n\tQUIET_CATEGORIES,\n} from './constants.js'\nimport { renderCauseChain } from './helpers.js'\n\n/**\n * Files messages through the conversation's judgment manager and reads their categories and corrections.\n *\n * @example\n * ```ts\n * const classifier = new Classifier(options)\n * await classifier.classify(new Set(), signal)\n * const classification = classifier.classification()\n * ```\n */\nexport class Classifier implements ClassifierInterface {\n\treadonly #options: ClassifierOptions\n\treadonly #failed = new Set<string>()\n\t#slice = 0\n\n\t/**\n\t * Creates the filing engine with the ledger's handlers and calibrated questions.\n\t * @param options - The conversation, judge, questions, cutoffs, and handlers\n\t */\n\tconstructor(options: ClassifierOptions) {\n\t\tthis.#options = options\n\t}\n\n\t/**\n\t * Asks the judge every question the filing still lacks, in the measured order.\n\t * @param requests - The request ids, which skip category questions and receive only enabled request-topic questions\n\t * @param signal - The caller's signal; an abort returns a fault with completed judgments and usage\n\t * @returns The judgment keys, summed usage, and one attempt per logical question, in asking order; a throw from the assign or entities handler, a scheduler rejection, or a caller abort returns the partial result with `fault`; a judge rejection under a live signal leaves that question undecided and sets `failure`\n\t */\n\tasync classify(requests: ReadonlySet<string>, signal: AbortSignal): Promise<ClassifierResult> {\n\t\tconst results: ClassifierResult[] = []\n\t\tconst fault = await this.#gather(requests, signal, results).then(\n\t\t\t(): Error | undefined => undefined,\n\t\t\t(error: unknown) => (isError(error) ? error : new Error(errorToMessage(error))),\n\t\t)\n\t\tconst usage = results.reduce<TokenUsage | undefined>(\n\t\t\t(total, result) => (result.usage === undefined ? total : sumUsage(total, result.usage)),\n\t\t\tundefined,\n\t\t)\n\t\tconst failure = results.find((result) => result.failure !== undefined)?.failure\n\t\treturn {\n\t\t\tjudgments: results.flatMap((result) => result.judgments),\n\t\t\tattempts: results.flatMap((result) => result.attempts),\n\t\t\t...(fault === undefined ? {} : { fault }),\n\t\t\t...(failure === undefined ? {} : { failure }),\n\t\t\t...(usage === undefined ? {} : { usage }),\n\t\t}\n\t}\n\n\t/**\n\t * Reads the category a message is filed under.\n\t * @param id - The message id\n\t * @returns The assigned category, the recorded category at the cutoff, or `undefined`; a quiet winner suppressed by the user literal floor remains undecided\n\t */\n\tcategory(id: string): LedgerCategory | undefined {\n\t\tconst message = this.#options.conversation.message(id)\n\t\tif (message === undefined) return undefined\n\t\tconst assigned = this.#options.assign(message)\n\t\tif (assigned !== undefined) return assigned\n\t\tconst answer = this.#readCategories(id)\n\t\tconst cutoff = this.#options.thresholds.category\n\t\tconst candidates = LEDGER_CATEGORIES.flatMap((category) => {\n\t\t\tconst probability = answer?.probabilities[category]\n\t\t\treturn probability !== undefined && probability >= cutoff ? [{ category, probability }] : []\n\t\t})\n\t\t// The first category in `LEDGER_CATEGORIES` order with the highest probability wins a tie.\n\t\tconst winner = candidates.reduce<{ category: LedgerCategory; probability: number } | undefined>(\n\t\t\t(best, candidate) =>\n\t\t\t\tbest === undefined || candidate.probability > best.probability ? candidate : best,\n\t\t\tundefined,\n\t\t)?.category\n\t\treturn winner !== undefined && QUIET_CATEGORIES.includes(winner) && !this.quiet(id)\n\t\t\t? undefined\n\t\t\t: winner\n\t}\n\n\t/**\n\t * Reports whether a message files under a quiet category.\n\t * @param id - The message id\n\t * @returns True if the assigned category is quiet or the recorded quiet weight reaches the cutoff, except for a user message carrying an id or number; false otherwise\n\t */\n\tquiet(id: string): boolean {\n\t\tconst message = this.#options.conversation.message(id)\n\t\tif (message === undefined) return false\n\t\tconst assigned = this.#options.assign(message)\n\t\tif (assigned !== undefined) return QUIET_CATEGORIES.includes(assigned)\n\t\tif (this.#weigh(id, QUIET_CATEGORIES) < this.#options.thresholds.category) return false\n\t\tif (message.role !== 'user') return true\n\t\tconst literals = extractLiterals(message.content)\n\t\treturn literals.ids.size + literals.numbers.size === 0\n\t}\n\n\t/**\n\t * Reports whether a message files under a decisive category.\n\t * @param id - The message id\n\t * @returns True if the recorded decisive weight reaches the category cutoff; false otherwise.\n\t */\n\tdecisive(id: string): boolean {\n\t\treturn this.#weigh(id, DECISIVE_CATEGORIES) >= this.#options.thresholds.category\n\t}\n\n\t/**\n\t * Reads the topics a message concerns.\n\t * @param id - The message id\n\t * @returns The names of the topics whose recorded weight reaches the topic cutoff\n\t */\n\ttopics(id: string): ReadonlySet<string> {\n\t\tconst message = this.#options.conversation.message(id)\n\t\t// The spread visits a hole in the topic list as an undefined topic, as iteration does; `filter` skips it.\n\t\treturn new Set(\n\t\t\tmessage === undefined\n\t\t\t\t? []\n\t\t\t\t: [...this.#options.topics]\n\t\t\t\t\t\t.filter(\n\t\t\t\t\t\t\t(topic) =>\n\t\t\t\t\t\t\t\t(this.#readNoul(this.#buildTopic(id, topic)) ?? 0) >=\n\t\t\t\t\t\t\t\tthis.#options.thresholds.topic,\n\t\t\t\t\t\t)\n\t\t\t\t\t\t.map((topic) => topic.name),\n\t\t)\n\t}\n\n\t/**\n\t * Reads the whole filing from the recorded judgments.\n\t * @returns The quiet ids, categories, topics, and the amendment and supersession marks\n\t */\n\tclassification(): LedgerClassification {\n\t\tconst messages = this.#options.conversation.messages()\n\t\tconst positions = new Map(messages.map((message, at) => [message.id, at]))\n\t\t// Each message files in HEAD's call order: quiet, then category, then topics.\n\t\tconst filed = messages.map((message) => ({\n\t\t\tid: message.id,\n\t\t\tquiet: this.quiet(message.id),\n\t\t\tcategory: this.category(message.id),\n\t\t\ttopics: [...this.topics(message.id)],\n\t\t}))\n\t\tconst pairs = this.#options.conversation.judgments.judgments().flatMap((judgment) => {\n\t\t\tconst key = parseJSONAs(judgment.id, isArray)\n\t\t\tif (key === undefined) return []\n\t\t\tconst [head, earlier, later] = key\n\t\t\tif ((head !== 'amends' && head !== 'supersedes') || !isString(earlier) || !isString(later))\n\t\t\t\treturn []\n\t\t\tconst before = this.#options.conversation.message(earlier)\n\t\t\tconst after = this.#options.conversation.message(later)\n\t\t\tif (\n\t\t\t\tbefore === undefined ||\n\t\t\t\tafter === undefined ||\n\t\t\t\t(this.#readNoul(this.#buildPair(head, earlier, later)) ?? 0) <\n\t\t\t\t\tthis.#options.thresholds[head] ||\n\t\t\t\tthis.quiet(later)\n\t\t\t)\n\t\t\t\treturn []\n\t\t\treturn head === 'amends' &&\n\t\t\t\t!this.#sharesLiteral(extractLiterals(before.content), extractLiterals(after.content))\n\t\t\t\t? []\n\t\t\t\t: [{ head, earlier, later }]\n\t\t})\n\t\treturn {\n\t\t\tquiet: new Set(filed.filter((one) => one.quiet).map((one) => one.id)),\n\t\t\tcategories: new Map(\n\t\t\t\tfiled.flatMap((one) =>\n\t\t\t\t\tone.category === undefined ? [] : [[one.id, one.category] as const],\n\t\t\t\t),\n\t\t\t),\n\t\t\ttopics: new Map(filed.map((one) => [one.id, one.topics])),\n\t\t\tamendments: this.#sortLaters(pairs, positions),\n\t\t\tsupersessions: this.#sortLaters(\n\t\t\t\tpairs.filter((pair) => pair.head === 'supersedes'),\n\t\t\t\tpositions,\n\t\t\t),\n\t\t}\n\t}\n\n\t// Groups the later ids under each earlier id in first-seen order, each without repeats and in\n\t// conversation order; a stable sort keeps the first-seen order of ids at one position.\n\t#sortLaters(\n\t\tpairs: ReadonlyArray<{ readonly earlier: string; readonly later: string }>,\n\t\tpositions: ReadonlyMap<string, number>,\n\t): ReadonlyMap<string, readonly string[]> {\n\t\treturn new Map(\n\t\t\t[...Map.groupBy(pairs, (pair) => pair.earlier)].map(([earlier, grouped]) => [\n\t\t\t\tearlier,\n\t\t\t\t[...new Set(grouped.map((pair) => pair.later))].sort(\n\t\t\t\t\t(left, right) => (positions.get(left) ?? 0) - (positions.get(right) ?? 0),\n\t\t\t\t),\n\t\t\t]),\n\t\t)\n\t}\n\n\t// Asks every question in order, appending each answer to `results` so a throw still leaves the\n\t// answers completed before it.\n\tasync #gather(\n\t\trequests: ReadonlySet<string>,\n\t\tsignal: AbortSignal,\n\t\tresults: ClassifierResult[],\n\t): Promise<void> {\n\t\tconst messages = this.#options.conversation.messages()\n\t\tconst asked = messages.filter((message) => this.#options.assign(message) === undefined)\n\t\tfor (const message of asked) {\n\t\t\tif (requests.has(message.id)) continue\n\t\t\tif (this.#spent()) await this.#turn(signal)\n\t\t\tresults.push(await this.#ask(this.#buildCategory(message.id), 'category', signal, results))\n\t\t}\n\t\tconst quiets = new Map<string, boolean>()\n\t\tfor (const message of asked) {\n\t\t\tif (!requests.has(message.id) && this.#readQuiet(message.id, quiets)) continue\n\t\t\tfor (const topic of this.#options.topics) {\n\t\t\t\tif (requests.has(message.id) && topic.requested === false) continue\n\t\t\t\tif (this.#spent()) await this.#turn(signal)\n\t\t\t\tresults.push(await this.#ask(this.#buildTopic(message.id, topic), 'topic', signal, results))\n\t\t\t}\n\t\t}\n\t\tconst nears = new Map<string, ReadonlySet<string>>()\n\t\tconst entities = new Map<string, ReadonlySet<string>>()\n\t\tconst literals = new Map<string, LiteralSet>()\n\t\tfor (const later of asked) {\n\t\t\tif (\n\t\t\t\tlater.role !== 'user' ||\n\t\t\t\t(this.#readCategories(later.id)?.probabilities.correction ?? 0) <\n\t\t\t\t\tthis.#options.thresholds.correction\n\t\t\t)\n\t\t\t\tcontinue\n\t\t\tconst near = this.#readNear(later, nears, entities)\n\t\t\tif (near.size === 0) continue\n\t\t\tfor (const earlier of messages) {\n\t\t\t\tif (earlier.id === later.id) break\n\t\t\t\tif (this.#spent()) await this.#turn(signal)\n\t\t\t\tif (!this.#eligible(earlier, later, near, quiets, nears, entities, literals)) continue\n\t\t\t\tconst spec = this.#buildPair('amends', earlier.id, later.id)\n\t\t\t\tresults.push(await this.#ask(spec, 'amends', signal, results))\n\t\t\t\tif ((this.#readNoul(spec) ?? 0) >= this.#options.thresholds.amends)\n\t\t\t\t\tresults.push(\n\t\t\t\t\t\tawait this.#ask(\n\t\t\t\t\t\t\tthis.#buildPair('supersedes', earlier.id, later.id),\n\t\t\t\t\t\t\t'supersedes',\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tresults,\n\t\t\t\t\t\t),\n\t\t\t\t\t)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Reports whether the slice since the last host turn has run its length; with no scheduler no clock is read.\n\t#spent(): boolean {\n\t\treturn this.#options.scheduler !== undefined && performance.now() - this.#slice >= LEDGER_SLICE\n\t}\n\n\t// Hands the host a turn, raced against the signal so an abort ends it, and opens the next slice.\n\tasync #turn(signal: AbortSignal): Promise<void> {\n\t\tconst scheduler = this.#options.scheduler\n\t\tif (scheduler === undefined) return\n\t\tawait raceSignal(scheduler.yield({ signal }), signal)\n\t\tthis.#slice = performance.now()\n\t}\n\n\t#renderState(id: string): string {\n\t\tconst message = this.#options.conversation.message(id)\n\t\treturn `${message?.role ?? 'unknown'}: ${message?.content ?? ''}`\n\t}\n\n\t#buildCategory(id: string): JudgmentInput {\n\t\treturn {\n\t\t\tid: JSON.stringify(['category', id]),\n\t\t\tquestion: {\n\t\t\t\tform: 'choice',\n\t\t\t\t...(this.#options.questions.category.instructions === undefined\n\t\t\t\t\t? {}\n\t\t\t\t\t: { instructions: this.#options.questions.category.instructions }),\n\t\t\t\tcriteria: Object.fromEntries(\n\t\t\t\t\tLEDGER_CATEGORIES.map((category) => [\n\t\t\t\t\t\tcategory,\n\t\t\t\t\t\tthis.#options.questions.category.criteria[category],\n\t\t\t\t\t]),\n\t\t\t\t),\n\t\t\t},\n\t\t\tsources: [id],\n\t\t\tstate: this.#renderState(id),\n\t\t\tmodel: this.#options.judge.model,\n\t\t}\n\t}\n\n\t#buildTopic(id: string, topic: LedgerTopic): JudgmentInput {\n\t\treturn {\n\t\t\tid: JSON.stringify(['topic', id, topic.name]),\n\t\t\tquestion: {\n\t\t\t\tform: 'noul',\n\t\t\t\tinstructions: this.#options.questions.topic,\n\t\t\t\tcriteria: {\n\t\t\t\t\ttrue: `The message concerns ${topic.name}: ${topic.criterion}`,\n\t\t\t\t\tfalse: `The message does not concern ${topic.name}`,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsources: [id],\n\t\t\tstate: this.#renderState(id),\n\t\t\tmodel: this.#options.judge.model,\n\t\t}\n\t}\n\n\t#buildPair(head: 'amends' | 'supersedes', earlier: string, later: string): JudgmentInput {\n\t\treturn {\n\t\t\tid: JSON.stringify([head, earlier, later]),\n\t\t\tquestion: this.#options.questions[head],\n\t\t\tsources: [earlier, later],\n\t\t\tstate: `Earlier message: ${this.#renderState(earlier)}\\nLater message: ${this.#renderState(later)}`,\n\t\t\tmodel: this.#options.judge.model,\n\t\t}\n\t}\n\n\t#read(spec: JudgmentInput): Judgment | undefined {\n\t\tconst recorded = this.#options.conversation.judgments.judgment(spec.id)\n\t\treturn recorded !== undefined &&\n\t\t\tmatchesJudgment(recorded, spec.question, spec.sources, spec.state, this.#options.judge.model)\n\t\t\t? recorded\n\t\t\t: undefined\n\t}\n\n\t#readNoul(spec: JudgmentInput): number | undefined {\n\t\tconst answer = this.#read(spec)?.answer\n\t\treturn answer?.form === 'noul' ? answer.noul : undefined\n\t}\n\n\t#readCategories(id: string): ChoiceAnswer | undefined {\n\t\tconst answer = this.#read(this.#buildCategory(id))?.answer\n\t\treturn answer?.form === 'choice' ? answer : undefined\n\t}\n\n\t#weigh(id: string, categories: readonly LedgerCategory[]): number {\n\t\tconst answer = this.#readCategories(id)\n\t\treturn categories.reduce((sum, category) => sum + (answer?.probabilities[category] ?? 0), 0)\n\t}\n\n\t#collectEntities(message: Message): ReadonlySet<string> {\n\t\tconst argumentsText =\n\t\t\tmessage.calls === undefined\n\t\t\t\t? undefined\n\t\t\t\t: renderJSON(message.calls.map((call) => call.arguments))\n\t\treturn this.#options.entities(\n\t\t\targumentsText === undefined ? message.content : `${message.content} ${argumentsText}`,\n\t\t\tfalse,\n\t\t)\n\t}\n\n\t// Reads a message's registry entities from this call's memo, collecting them on the first read.\n\t#readEntities(message: Message, entities: Map<string, ReadonlySet<string>>): ReadonlySet<string> {\n\t\tconst known = entities.get(message.id)\n\t\tif (known !== undefined) return known\n\t\tconst found = this.#collectEntities(message)\n\t\tentities.set(message.id, found)\n\t\treturn found\n\t}\n\n\t#collectNear(message: Message, entities: Map<string, ReadonlySet<string>>): ReadonlySet<string> {\n\t\treturn new Set([\n\t\t\t...this.#readEntities(message, entities),\n\t\t\t...(this.#options.assign(message) === undefined ? this.topics(message.id) : []),\n\t\t])\n\t}\n\n\t// Reads a message's near set from this call's memo, collecting it on the first read.\n\t#readNear(\n\t\tmessage: Message,\n\t\tnears: Map<string, ReadonlySet<string>>,\n\t\tentities: Map<string, ReadonlySet<string>>,\n\t): ReadonlySet<string> {\n\t\tconst known = nears.get(message.id)\n\t\tif (known !== undefined) return known\n\t\tconst near = this.#collectNear(message, entities)\n\t\tnears.set(message.id, near)\n\t\treturn near\n\t}\n\n\t// Reads a message's literals from this call's memo, extracting them on the first read.\n\t#readLiterals(message: Message, literals: Map<string, LiteralSet>): LiteralSet {\n\t\tconst known = literals.get(message.id)\n\t\tif (known !== undefined) return known\n\t\tconst found = extractLiterals(message.content)\n\t\tliterals.set(message.id, found)\n\t\treturn found\n\t}\n\n\t// Reads a message's quiet flag from this call's memo, reading it on the first request.\n\t#readQuiet(id: string, quiets: Map<string, boolean>): boolean {\n\t\tconst known = quiets.get(id)\n\t\tif (known !== undefined) return known\n\t\tconst quiet = this.quiet(id)\n\t\tquiets.set(id, quiet)\n\t\treturn quiet\n\t}\n\n\t// Reports whether two texts' literals share an id or a number.\n\t#sharesLiteral(own: LiteralSet, other: LiteralSet): boolean {\n\t\treturn (\n\t\t\t[...own.ids].some((id) => other.ids.has(id)) ||\n\t\t\t[...own.numbers].some((number) => other.numbers.has(number))\n\t\t)\n\t}\n\n\t// Reports whether two messages share a registry entity, topics excluded, or a literal of their content.\n\t#sharesEntityOrLiteral(\n\t\tearlier: Message,\n\t\tlater: Message,\n\t\tentities: Map<string, ReadonlySet<string>>,\n\t\tliterals: Map<string, LiteralSet>,\n\t): boolean {\n\t\tconst near = this.#readEntities(later, entities)\n\t\treturn (\n\t\t\t[...this.#readEntities(earlier, entities)].some((entity) => near.has(entity)) ||\n\t\t\tthis.#sharesLiteral(\n\t\t\t\tthis.#readLiterals(earlier, literals),\n\t\t\t\tthis.#readLiterals(later, literals),\n\t\t\t)\n\t\t)\n\t}\n\n\t// Reports whether a correction is compared against an earlier message: the message is not quiet\n\t// and its near set shares a member with the correction's, and under `narrow` the two also share a\n\t// registry entity or a literal. The quiet flag is read first, so a quiet message's near set is\n\t// never read, and with `narrow` unset no literal is read here.\n\t#eligible(\n\t\tearlier: Message,\n\t\tlater: Message,\n\t\tnear: ReadonlySet<string>,\n\t\tquiets: Map<string, boolean>,\n\t\tnears: Map<string, ReadonlySet<string>>,\n\t\tentities: Map<string, ReadonlySet<string>>,\n\t\tliterals: Map<string, LiteralSet>,\n\t): boolean {\n\t\treturn (\n\t\t\t!this.#readQuiet(earlier.id, quiets) &&\n\t\t\t[...this.#readNear(earlier, nears, entities)].some((topic) => near.has(topic)) &&\n\t\t\t(this.#options.correction?.narrow !== true ||\n\t\t\t\tthis.#sharesEntityOrLiteral(earlier, later, entities, literals))\n\t\t)\n\t}\n\n\tasync #ask(\n\t\tspec: JudgmentInput,\n\t\tphase: ClassifierPhase,\n\t\tsignal: AbortSignal,\n\t\tresults: ClassifierResult[],\n\t): Promise<ClassifierResult> {\n\t\tif (this.#read(spec) !== undefined)\n\t\t\treturn { judgments: [spec.id], attempts: [{ key: spec.id, phase, outcome: 'cached' }] }\n\t\tconst fingerprint = JSON.stringify(spec)\n\t\tif (this.#failed.has(fingerprint))\n\t\t\treturn { judgments: [], attempts: [{ key: spec.id, phase, outcome: 'skipped' }] }\n\t\tsignal.throwIfAborted()\n\t\tif (spec.sources.some((id) => this.#options.conversation.message(id) === undefined))\n\t\t\treturn { judgments: [], attempts: [{ key: spec.id, phase, outcome: 'skipped' }] }\n\t\tconst started = performance.now()\n\t\ttry {\n\t\t\tconst [judgment] = await this.#options.conversation.judgments.resolve(\n\t\t\t\tthis.#options.judge,\n\t\t\t\t{ state: spec.state, questions: { [spec.id]: spec.question } },\n\t\t\t\tspec.sources,\n\t\t\t\tsignal,\n\t\t\t)\n\t\t\tconst attempts: readonly ClassifierAttempt[] = [\n\t\t\t\t{ key: spec.id, phase, outcome: 'resolved', duration: performance.now() - started },\n\t\t\t]\n\t\t\treturn judgment === undefined\n\t\t\t\t? { judgments: [], attempts }\n\t\t\t\t: {\n\t\t\t\t\t\tjudgments: [spec.id],\n\t\t\t\t\t\t...(judgment.usage === undefined ? {} : { usage: judgment.usage }),\n\t\t\t\t\t\tattempts,\n\t\t\t\t\t}\n\t\t} catch (error) {\n\t\t\t// A rejection records its attempt in the pushed result alone, so the empty result returned under a live signal repeats none.\n\t\t\tconst attempts: readonly ClassifierAttempt[] = [\n\t\t\t\t{\n\t\t\t\t\tkey: spec.id,\n\t\t\t\t\tphase,\n\t\t\t\t\toutcome: isJudgeAbortError(error) || signal.aborted ? 'aborted' : 'failed',\n\t\t\t\t\tduration: performance.now() - started,\n\t\t\t\t},\n\t\t\t]\n\t\t\tif (isJudgeAbortError(error))\n\t\t\t\tresults.push({\n\t\t\t\t\tjudgments:\n\t\t\t\t\t\tObject.hasOwn(error.partial.answers, spec.id) ||\n\t\t\t\t\t\t(error.partial.refusals !== undefined && Object.hasOwn(error.partial.refusals, spec.id))\n\t\t\t\t\t\t\t? [spec.id]\n\t\t\t\t\t\t\t: [],\n\t\t\t\t\t...(error.partial.usage === undefined ? {} : { usage: error.partial.usage }),\n\t\t\t\t\tattempts,\n\t\t\t\t})\n\t\t\telse results.push({ judgments: [], attempts })\n\t\t\tif (signal.aborted) throw error\n\t\t\tconst failure = isError(error) ? error : new Error(errorToMessage(error))\n\t\t\t// Under the default `strict` this failure rejects the request it belongs to and the next request\n\t\t\t// asks again; a lenient request goes on past it, so a question that cannot succeed is held.\n\t\t\tif (\n\t\t\t\tthis.#options.strict === false &&\n\t\t\t\t((isJudgeError(error) && error.code === 'QUESTION') ||\n\t\t\t\t\tDETERMINISTIC_JUDGE_ERROR.test(renderCauseChain(error)))\n\t\t\t)\n\t\t\t\tthis.#failed.add(fingerprint)\n\t\t\treturn { judgments: [], attempts: [], failure }\n\t\t}\n\t}\n}\n","import type { ThinkingReplay } from '../types.js'\nimport type { GaugeCall, GaugeInterface, GaugeOptions } from './types.js'\nimport { isFiniteNumber } from '@orkestrel/contract'\nimport { estimateMessages } from '../agents/helpers.js'\nimport { LedgerError } from './errors.js'\nimport { isPositiveSafeInteger } from '../validators.js'\nimport { buildRecallMessage, fitSlope, resolvePredict } from './helpers.js'\n\n/**\n * Prices prompts in tokens from a measured scale and overhead cost, and measures the room a request\n * has left.\n *\n * @remarks\n * `observe` rescales from the first call only when `spared` is absent, zero, or at least `overhead`,\n * with `max(0, overhead - spared)` taken out. A partially spared call keeps the existing scale.\n * It keeps that request's calls for the marginal rate and the longest final completion for the reply\n * reserve after subtracting its thinking. `overhead` never changes after construction.\n * {@link GaugeOptions} defines the measured use, reply reserve, and recall-room formulas.\n * {@link LedgerOptions} defines the ledger's plan budget and recall close rule.\n *\n * @example\n * ```ts\n * const gauge = new Gauge({ scale: 1.16, overhead: 498, capacity: 32768 })\n * gauge.room([{ estimate: 1719, prompt: 2498, tools: 3 }], '') // estimate units a recall result can take\n * ```\n */\nexport class Gauge implements GaugeInterface {\n\treadonly #capacity: number\n\treadonly #predict: number\n\treadonly #replay: ThinkingReplay\n\t#history: ReadonlyArray<readonly GaugeCall[]> = []\n\t#scale: number\n\t#overhead: number\n\t#reply = 0\n\n\t/**\n\t * Holds the starting price and the capacity.\n\t *\n\t * @param options - The starting `scale` and `overhead` price and the context `capacity`\n\t * @throws {LedgerError} Thrown when `scale` is not finite and above 0, or `overhead` is not finite and at least 0 (code `'GAUGE'`)\n\t * @throws {LedgerError} Thrown when `capacity` is not a positive safe integer (code `'CAPACITY'`)\n\t * @throws {LedgerError} Thrown when `predict` is not a nonnegative safe integer less than `capacity` (code `'CAPACITY'`)\n\t */\n\tconstructor(options: GaugeOptions) {\n\t\tif (!isFiniteNumber(options.scale) || options.scale <= 0) {\n\t\t\tthrow new LedgerError('GAUGE', 'gauge scale must be finite and greater than 0')\n\t\t}\n\t\tif (!isFiniteNumber(options.overhead) || options.overhead < 0) {\n\t\t\tthrow new LedgerError('GAUGE', 'gauge overhead must be finite and at least 0')\n\t\t}\n\t\tif (!isPositiveSafeInteger(options.capacity)) {\n\t\t\tthrow new LedgerError('CAPACITY', 'gauge capacity must be a positive safe integer')\n\t\t}\n\t\tthis.#scale = options.scale\n\t\tthis.#overhead = options.overhead\n\t\tthis.#capacity = options.capacity\n\t\tthis.#predict = resolvePredict(options.predict, options.capacity)\n\t\tthis.#replay = options.replay ?? 'none'\n\t}\n\n\tget scale(): number {\n\t\treturn this.#scale\n\t}\n\n\tget overhead(): number {\n\t\treturn this.#overhead\n\t}\n\n\t/**\n\t * Reads the marginal rate of prompt tokens per estimate unit.\n\t *\n\t * @remarks\n\t * Returns the scale when the fit is undefined, nonfinite, or not above 0, so a degenerate fit never prices recall room.\n\t *\n\t * @param calls - The calls of the request in progress\n\t * @returns The fitted slope, or the scale when the slope is unusable\n\t */\n\trate(calls: readonly GaugeCall[]): number {\n\t\tconst slope = fitSlope([...this.#history, calls])\n\t\treturn slope !== undefined && isFiniteNumber(slope) && slope > 0 ? slope : this.#scale\n\t}\n\n\tremainder(calls: readonly GaugeCall[]): number {\n\t\tconst call = calls.at(-1)\n\t\tconst used = isFiniteNumber(call?.prompt)\n\t\t\t? call.prompt + (call.completion ?? 0) - (this.#replay === 'none' ? (call.thinking ?? 0) : 0)\n\t\t\t: Math.max(0, this.#overhead - (call?.spared ?? 0)) + this.#scale * (call?.estimate ?? 0)\n\t\treturn Math.max(0, this.#capacity - used)\n\t}\n\n\treserve(calls: readonly GaugeCall[], longest: string): number {\n\t\tconst rate = this.rate(calls)\n\t\t// Before any reply is observed, the longest assistant text stands in for the reply.\n\t\tconst reply =\n\t\t\tthis.#reply > 0\n\t\t\t\t? this.#reply\n\t\t\t\t: rate * estimateMessages([{ id: 'reply', role: 'assistant', content: longest }])\n\t\tconst recall =\n\t\t\trate * estimateMessages([buildRecallMessage(''), { id: 'result', role: 'tool', content: '' }])\n\t\treturn reply + recall\n\t}\n\n\troom(calls: readonly GaugeCall[], longest: string): number {\n\t\treturn Math.max(\n\t\t\t0,\n\t\t\t(this.remainder(calls) - this.#predict - this.reserve(calls, longest)) / 2 / this.rate(calls),\n\t\t)\n\t}\n\n\tobserve(calls: readonly GaugeCall[], reply?: GaugeCall): void {\n\t\tconst first = calls[0]\n\t\tif (\n\t\t\tfirst !== undefined &&\n\t\t\tisFiniteNumber(first.prompt) &&\n\t\t\tfirst.estimate > 0 &&\n\t\t\t((first.spared ?? 0) === 0 || (first.spared ?? 0) >= this.#overhead)\n\t\t) {\n\t\t\tconst priced = first.prompt - Math.max(0, this.#overhead - (first.spared ?? 0))\n\t\t\tif (priced > 0) this.#scale = priced / first.estimate\n\t\t}\n\t\tthis.#history = [...this.#history, [...calls]]\n\t\tif (isFiniteNumber(reply?.completion))\n\t\t\tthis.#reply = Math.max(this.#reply, reply.completion - (reply.thinking ?? 0))\n\t}\n}\n","import type { Selection } from '../contexts/types.js'\nimport type { GaugeCall } from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport { sumUsage } from '../helpers.js'\n\n/**\n * Holds what one pass of a ledger request keeps: the selection its run adopted last, the\n * conversation boundary its calls count additions from, its summed usage, and its gauge calls.\n *\n * @remarks\n * A `LedgerRequest` opens one pass per agent run and exposes only projections of it, so no public\n * signature names this class. The pass appends one final record to its log for each call observed,\n * each usage reported, and each thinking share, in emission order, and derives `calls`, `call`, and\n * `position` from it: a usage or thinking record joins the last call observed before it, and one\n * recorded before the first call joins none.\n */\nexport class LedgerPass {\n\t#log: ReadonlyArray<\n\t\t| { readonly call: GaugeCall; readonly position: number }\n\t\t| { readonly usage: { readonly prompt: number; readonly completion: number } }\n\t\t| { readonly thinking: number }\n\t> = []\n\t#selection: Selection | undefined\n\t#boundary: number\n\t#usage: TokenUsage | undefined\n\n\t/**\n\t * Opens the pass at a conversation boundary.\n\t *\n\t * @param boundary - The conversation length when the pass's run starts\n\t */\n\tconstructor(boundary: number) {\n\t\tthis.#boundary = boundary\n\t}\n\n\t/**\n\t * Returns the selection the pass's run adopted last.\n\t *\n\t * @returns The selection, or undefined before one\n\t */\n\tget selection(): Selection | undefined {\n\t\treturn this.#selection\n\t}\n\n\t/**\n\t * Returns the conversation length the pass's calls count additions from.\n\t *\n\t * @returns The length at the pass's start or at its last adopted selection\n\t */\n\tget boundary(): number {\n\t\treturn this.#boundary\n\t}\n\n\t/**\n\t * Returns the summed usage of the pass's selections and calls.\n\t *\n\t * @returns The summed usage, or undefined while none reported usage\n\t */\n\tget usage(): TokenUsage | undefined {\n\t\treturn this.#usage\n\t}\n\n\t/**\n\t * Returns the call in flight.\n\t *\n\t * @returns The latest call the pass observed, or undefined before one\n\t */\n\tget call(): GaugeCall | undefined {\n\t\treturn this.calls.at(-1)\n\t}\n\n\t/**\n\t * Returns the conversation length when the call in flight started.\n\t *\n\t * @returns The position, or undefined before a call\n\t */\n\tget position(): number | undefined {\n\t\treturn this.#log.flatMap((entry) => ('position' in entry ? [entry.position] : [])).at(-1)\n\t}\n\n\t/**\n\t * Returns the pass's gauge calls in order.\n\t *\n\t * @returns Each observed call with the last usage and thinking recorded after it, before the next call\n\t */\n\tget calls(): readonly GaugeCall[] {\n\t\treturn this.#log.flatMap((entry, index) => {\n\t\t\tif (!('call' in entry)) return []\n\t\t\tconst after = this.#log.slice(index + 1)\n\t\t\tconst next = after.findIndex((one) => 'call' in one)\n\t\t\tconst joined = next === -1 ? after : after.slice(0, next)\n\t\t\tconst usage = joined.flatMap((one) => ('usage' in one ? [one.usage] : [])).at(-1)\n\t\t\tconst thinking = joined.flatMap((one) => ('thinking' in one ? [one.thinking] : [])).at(-1)\n\t\t\tconst billed =\n\t\t\t\tusage === undefined\n\t\t\t\t\t? entry.call\n\t\t\t\t\t: { ...entry.call, prompt: usage.prompt, completion: usage.completion }\n\t\t\treturn [thinking === undefined ? billed : { ...billed, thinking }]\n\t\t})\n\t}\n\n\t/**\n\t * Records the selection the pass's run adopted and adds the usage it spent.\n\t *\n\t * @param selection - The selection the run built its prompt from\n\t * @param boundary - The conversation length when the run adopted it\n\t */\n\tadopt(selection: Selection, boundary: number): void {\n\t\tthis.#selection = selection\n\t\tthis.#boundary = boundary\n\t\tif (selection.usage !== undefined) this.#usage = sumUsage(this.#usage, selection.usage)\n\t}\n\n\t/**\n\t * Starts a call, appending its record to the log.\n\t *\n\t * @param call - The call's estimate and advertised tool count\n\t * @param position - The conversation length when the call starts\n\t */\n\tobserve(call: GaugeCall, position: number): void {\n\t\tthis.#log = [...this.#log, { call, position }]\n\t}\n\n\t/**\n\t * Adds one call's reported usage to the pass's summed usage.\n\t *\n\t * @param usage - The usage the provider reported\n\t */\n\tspend(usage: TokenUsage): void {\n\t\tthis.#usage = sumUsage(this.#usage, usage)\n\t}\n\n\t/**\n\t * Appends a reported usage to the log, which joins the call observed last as its prompt and completion.\n\t *\n\t * @remarks\n\t * The record keeps the prompt and completion values the usage held at this call, so a later change\n\t * to the caller's object never reaches a reading.\n\t *\n\t * @param usage - The usage the provider reported for the call\n\t */\n\tbill(usage: TokenUsage): void {\n\t\tthis.#log = [...this.#log, { usage: { prompt: usage.prompt, completion: usage.completion } }]\n\t}\n\n\t/**\n\t * Appends the thinking share to the log, which joins the call observed last.\n\t *\n\t * @param thinking - The completion tokens attributed to the call's thinking\n\t */\n\tthink(thinking: number): void {\n\t\tthis.#log = [...this.#log, { thinking }]\n\t}\n}\n","import type { ScopeInterface, Selection } from '../contexts/types.js'\nimport type { ConversationInterface } from '../conversations/types.js'\nimport type { Message } from '../types.js'\nimport type { ClassifierAttempt, GaugeCall, LedgerDraft, LedgerRequestInterface } from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport { canonicalStringify } from '@orkestrel/contract'\nimport { isNonNegativeSafeInteger } from '../validators.js'\nimport { DEFAULT_RECALL_LIMIT, LEDGER_NOTES } from './constants.js'\nimport { LedgerError } from './errors.js'\nimport { computeThinking } from './helpers.js'\nimport { LedgerPass } from './LedgerPass.js'\n\n/**\n * Holds what one ledger request keeps while it is served: its message, its filing attempts and\n * plan, the gauge calls of its passes, and its repeat and recall stops.\n *\n * @remarks\n * A ledger creates one request per `respond` call and drops it when the call settles. Each pass the\n * request opens holds its own selection, boundary, usage, and calls; the request reads the current\n * pass, the last one opened. {@link LedgerRequestInterface} describes each member.\n *\n * @example\n * ```ts\n * const request = new LedgerRequest('What approval is needed?', 1)\n * request.enter(conversation) // the appended user message\n * request.admit('lookup', { id: 'BW-20931' })\n * request.admit('lookup', { id: 'BW-20931' }) // throws Error(LEDGER_NOTES.repeat)\n * request.recall()\n * request.recall() // throws Error(LEDGER_NOTES.closure): the limit of 1 is spent\n * ```\n */\nexport class LedgerRequest implements LedgerRequestInterface {\n\treadonly #content: string\n\treadonly #limit: number\n\t// The errors this request's run reported through `fault`, so `respond` tells a selection failure from a provider one.\n\treadonly #faults = new Set<unknown>()\n\t#message: Message | undefined\n\t#plan: Selection | undefined\n\t#scope: ScopeInterface | undefined\n\t#draft: LedgerDraft | undefined\n\t#sent: LedgerDraft | undefined\n\t#passes: readonly LedgerPass[] = []\n\t#attempts: readonly ClassifierAttempt[] = []\n\t#admitted: ReadonlySet<string> = new Set()\n\t#listed: ReadonlyMap<string, ReadonlyMap<string, string>> = new Map()\n\t#recalls = 0\n\t#closed = false\n\n\t/**\n\t * Holds the request's content and its recall limit.\n\t *\n\t * @param content - The request text `enter` appends\n\t * @param limit - The `recall` calls the request can make. Default: the `DEFAULT_RECALL_LIMIT` constant\n\t * @throws {LedgerError} Thrown when `limit` is not a nonnegative safe integer (code `'LIMIT'`)\n\t */\n\tconstructor(content: string, limit: number = DEFAULT_RECALL_LIMIT) {\n\t\tif (!isNonNegativeSafeInteger(limit))\n\t\t\tthrow new LedgerError('LIMIT', 'limits must be nonnegative safe integers')\n\t\tthis.#content = content\n\t\tthis.#limit = limit\n\t}\n\n\t/**\n\t * Returns the request message.\n\t *\n\t * @returns The message `enter` appended, or undefined before it\n\t */\n\tget message(): Message | undefined {\n\t\treturn this.#message\n\t}\n\n\t/**\n\t * Returns the selection the request's last successful plan produced.\n\t *\n\t * @returns The planned selection, or undefined before one\n\t */\n\tget plan(): Selection | undefined {\n\t\treturn this.#plan\n\t}\n\n\tget scope(): ScopeInterface | undefined {\n\t\treturn this.#scope\n\t}\n\n\tapply(scope: ScopeInterface): void {\n\t\tthis.#scope = scope\n\t}\n\n\t/** Returns the copy and receipts adopted by the last provider turn. */\n\tget sent(): LedgerDraft | undefined {\n\t\treturn this.#sent\n\t}\n\n\t/**\n\t * Returns the selection the current pass's run adopted last.\n\t *\n\t * @returns The selection, or undefined before one\n\t */\n\tget selection(): Selection | undefined {\n\t\treturn this.#passes.at(-1)?.selection\n\t}\n\n\t/**\n\t * Returns the conversation length the current pass's calls count additions from.\n\t *\n\t * @returns The length, or undefined before a pass\n\t */\n\tget boundary(): number | undefined {\n\t\treturn this.#passes.at(-1)?.boundary\n\t}\n\n\t/**\n\t * Returns the summed usage of the current pass.\n\t *\n\t * @returns The summed usage, or undefined while none reported usage\n\t */\n\tget usage(): TokenUsage | undefined {\n\t\treturn this.#passes.at(-1)?.usage\n\t}\n\n\t/**\n\t * Returns the gauge calls of every pass.\n\t *\n\t * @returns The calls in order, the call in flight last\n\t */\n\tget calls(): readonly GaugeCall[] {\n\t\treturn this.#passes.flatMap((pass) => pass.calls)\n\t}\n\n\t/**\n\t * Returns the filing attempts of the request's planning selections.\n\t *\n\t * @returns The attempts in asking order\n\t */\n\tget attempts(): readonly ClassifierAttempt[] {\n\t\treturn this.#attempts\n\t}\n\n\tenter(conversation: ConversationInterface): Message {\n\t\tif (this.#message !== undefined) return this.#message\n\t\tthis.#message = conversation.add({ role: 'user', content: this.#content })\n\t\treturn this.#message\n\t}\n\n\topen(boundary: number): void {\n\t\tthis.#passes = [...this.#passes, new LedgerPass(boundary)]\n\t}\n\n\tadopt(selection: Selection, boundary: number): void {\n\t\tthis.#passes.at(-1)?.adopt(selection, boundary)\n\t}\n\n\tobserve(call: GaugeCall, position: number): void {\n\t\tconst draft = this.#draft\n\t\tthis.#sent =\n\t\t\tdraft !== undefined &&\n\t\t\tthis.selection?.messages.some(\n\t\t\t\t(message) => message.id === draft.copy.id && message.content === draft.copy.content,\n\t\t\t)\n\t\t\t\t? draft\n\t\t\t\t: undefined\n\t\tthis.#passes.at(-1)?.observe(call, position)\n\t}\n\n\tspend(usage: TokenUsage): void {\n\t\tthis.#passes.at(-1)?.spend(usage)\n\t\t// The call in flight can belong to an earlier pass until the current pass observes its first call.\n\t\tthis.#passes.findLast((pass) => pass.call !== undefined)?.bill(usage)\n\t}\n\n\tthink(messages: readonly Message[]): void {\n\t\tconst pass = this.#passes.findLast((one) => one.call !== undefined)\n\t\tconst completion = pass?.call?.completion\n\t\tif (pass?.position === undefined || completion === undefined) return\n\t\tconst message = messages.slice(pass.position).find((entry) => entry.role === 'assistant')\n\t\tif (message?.thinking === undefined) return\n\t\tpass.think(computeThinking(message, completion))\n\t}\n\n\tfault(error: unknown): void {\n\t\tthis.#faults.add(error)\n\t}\n\n\tfaulted(error: unknown): boolean {\n\t\treturn this.#faults.has(error)\n\t}\n\n\tfile(attempts: readonly ClassifierAttempt[], plan?: Selection, draft?: LedgerDraft): void {\n\t\tthis.#attempts = [...this.#attempts, ...attempts]\n\t\tthis.#draft = draft\n\t\tif (plan !== undefined) {\n\t\t\tthis.#plan = plan\n\t\t}\n\t}\n\n\tadmit(name: string, args: Readonly<Record<string, unknown>>): void {\n\t\tconst key = canonicalStringify([name, args])\n\t\tif (key === undefined) throw new Error('tool arguments have no canonical identity')\n\t\tif (this.#admitted.has(key)) throw new Error(LEDGER_NOTES.repeat)\n\t\tthis.#admitted = new Set([...this.#admitted, key])\n\t}\n\n\trecall(): void {\n\t\tif (this.#recalls >= this.#limit) this.#closed = true\n\t\tif (this.#closed) throw new Error(LEDGER_NOTES.closure)\n\t\tthis.#recalls += 1\n\t}\n\n\tclose(): void {\n\t\tthis.#closed = true\n\t}\n\n\tlist(result: string, lines: ReadonlyMap<string, string>): void {\n\t\tthis.#listed = new Map([...this.#listed, [result, lines]])\n\t}\n\n\tlines(result: string): ReadonlyMap<string, string> | undefined {\n\t\treturn this.#listed.get(result)\n\t}\n}\n","import type { Message } from '../types.js'\nimport type { LedgerIndex, LedgerLookupFinding, LedgerRegistry } from './types.js'\nimport { collectToolGroups } from '../helpers.js'\nimport { buildLedgerIndex, collectRegistry } from './helpers.js'\n\n/**\n * Holds what one read of a ledger's conversation yields: the messages, the lookup findings, the\n * tool groups, the registry with its index, and each message's position.\n *\n * @remarks\n * A `Ledger` builds one scan for a selection or a `recall` call from the conversation's messages and\n * the lookup findings it read for them, and reads these values from the scan in place of reading the\n * conversation and the lookups again, so no public signature names this class. The scan computes\n * every value when it is built, never changes one, and reads no lookup itself.\n */\nexport class LedgerScan {\n\treadonly #messages: readonly Message[]\n\treadonly #findings: readonly LedgerLookupFinding[]\n\treadonly #lookups: ReadonlyMap<string, LedgerLookupFinding>\n\treadonly #groups: ReadonlyMap<string, readonly Message[]>\n\treadonly #positions: ReadonlyMap<string, number>\n\treadonly #first: number | undefined\n\treadonly #registry: LedgerRegistry\n\treadonly #index: LedgerIndex\n\n\t/**\n\t * Computes the scan's values from one read of the conversation.\n\t *\n\t * @param messages - The conversation's messages as the read returned them\n\t * @param findings - The lookup findings of those messages, in conversation order\n\t * @param requests - The ids of the ledger's request messages\n\t * @throws {TypeError} Thrown when a finding's owner list or an owner's name list holds a hole\n\t */\n\tconstructor(\n\t\tmessages: readonly Message[],\n\t\tfindings: readonly LedgerLookupFinding[],\n\t\trequests: ReadonlySet<string>,\n\t) {\n\t\tthis.#messages = messages\n\t\tthis.#findings = findings\n\t\tthis.#lookups = new Map(findings.map((finding) => [finding.id, finding]))\n\t\t// Reversed, so a message that two groups held would map to the first, as a search finds it.\n\t\tthis.#groups = new Map(\n\t\t\tcollectToolGroups(messages)\n\t\t\t\t.toReversed()\n\t\t\t\t.flatMap((group) => group.map((message) => [message.id, group] as const)),\n\t\t)\n\t\tthis.#positions = new Map(messages.map((message, at) => [message.id, at]))\n\t\tconst first = messages.findIndex((message) => requests.has(message.id))\n\t\tthis.#first = first < 0 ? undefined : first\n\t\tthis.#registry = collectRegistry(findings)\n\t\tthis.#index = buildLedgerIndex(this.#registry)\n\t}\n\n\t/**\n\t * Returns the messages the scan read.\n\t *\n\t * @returns The conversation's messages in order\n\t */\n\tget messages(): readonly Message[] {\n\t\treturn this.#messages\n\t}\n\n\t/**\n\t * Returns the lookup findings of the scanned messages.\n\t *\n\t * @returns One finding per successful lookup result, in conversation order\n\t */\n\tget findings(): readonly LedgerLookupFinding[] {\n\t\treturn this.#findings\n\t}\n\n\t/**\n\t * Returns the registry the findings name.\n\t *\n\t * @returns The ids and owner names the findings named\n\t */\n\tget registry(): LedgerRegistry {\n\t\treturn this.#registry\n\t}\n\n\t/**\n\t * Returns the registry's matching index.\n\t *\n\t * @returns The index built from the scan's registry\n\t */\n\tget index(): LedgerIndex {\n\t\treturn this.#index\n\t}\n\n\t/**\n\t * Returns the position of the first request message.\n\t *\n\t * @returns The position, or undefined when no scanned message is a request\n\t */\n\tget first(): number | undefined {\n\t\treturn this.#first\n\t}\n\n\t/**\n\t * Reads the lookup finding of a tool message.\n\t *\n\t * @param id - The tool message's id\n\t * @returns The finding, or undefined when the message has no successful lookup finding\n\t */\n\tfinding(id: string): LedgerLookupFinding | undefined {\n\t\treturn this.#lookups.get(id)\n\t}\n\n\t/**\n\t * Reads the tool group that holds a message.\n\t *\n\t * @param id - The message's id\n\t * @returns The group, or undefined when no tool group holds the message\n\t */\n\tgroup(id: string): readonly Message[] | undefined {\n\t\treturn this.#groups.get(id)\n\t}\n\n\t/**\n\t * Reads a message's position among the scanned messages.\n\t *\n\t * @param id - The message's id\n\t * @returns The position, or undefined when the scan holds no message with that id\n\t */\n\tposition(id: string): number | undefined {\n\t\treturn this.#positions.get(id)\n\t}\n}\n","import type { ContractShape } from '@orkestrel/contract'\nimport {\n\tarrayShape,\n\tnumberShape,\n\tobjectShape,\n\toptionalShape,\n\tstringShape,\n} from '@orkestrel/contract'\nimport { conversationSnapshotShape } from '../conversations/shapers.js'\n\n/** Describes the minimal serialized evidence of a nonempty exchange window. */\nexport const ledgerWindowShape = objectShape({\n\tdigest: stringShape(),\n\texchanges: arrayShape(\n\t\tobjectShape({\n\t\t\trequest: stringShape(),\n\t\t\tcopy: optionalShape(stringShape()),\n\t\t\treply: stringShape(),\n\t\t\tlines: arrayShape(\n\t\t\t\tobjectShape({\n\t\t\t\t\tkey: stringShape(),\n\t\t\t\t\ttitle: stringShape(),\n\t\t\t\t\tsource: stringShape(),\n\t\t\t\t\tsentence: numberShape({ integer: true, min: 0, max: Number.MAX_SAFE_INTEGER }),\n\t\t\t\t}),\n\t\t\t),\n\t\t}),\n\t\t{ min: 1 },\n\t),\n\tamendments: arrayShape(objectShape({ earlier: stringShape(), later: stringShape() })),\n}) satisfies ContractShape\n\n/**\n * Describes a ledger snapshot exactly: an unknown key refuses, so a key a later version adds never reads\n * as absent.\n *\n * @remarks\n * `isLedgerSnapshot` is the door a hydrate reads, and this shape fills a refusal's faults. What the shape\n * cannot state, that each marked id names a message of the snapshot's own conversation, is the guard's\n * alone.\n */\nexport const ledgerSnapshotShape = objectShape({\n\tconversation: conversationSnapshotShape,\n\trequests: arrayShape(stringShape()),\n\tnotes: arrayShape(stringShape()),\n\tfailures: arrayShape(stringShape()),\n\tgauge: optionalShape(objectShape({ scale: numberShape(), overhead: numberShape() })),\n\twindow: optionalShape(ledgerWindowShape),\n}) satisfies ContractShape\n","import { createContract } from '@orkestrel/contract'\nimport { ledgerSnapshotShape, ledgerWindowShape } from './shapers.js'\n\n/** Reads structural faults of a serialized exchange window. */\nexport const ledgerWindowContract = createContract(ledgerWindowShape)\n\n/** Reads structural faults of a ledger snapshot, including its optional held exchange window. */\nexport const ledgerSnapshotContract = createContract(ledgerSnapshotShape)\n","import type { LedgerQuestion, LedgerSnapshot, LedgerWindowSnapshot } from './types.js'\nimport { arrayOf, holds, isFiniteNumber, isRecord, isString } from '@orkestrel/contract'\nimport { LEDGER_CATEGORIES } from './constants.js'\nimport { isConversationSnapshot } from '../conversations/validators.js'\nimport { isJudgeEntry, isJudgeQuestion, isNonNegativeSafeInteger } from '../validators.js'\n\n/**\n * Checks whether a value has the exact shape of a nonempty exchange window.\n * @param value - The untrusted window\n * @returns True if the window and its members are well shaped; false otherwise\n * @example\n * ```ts\n * isLedgerWindowSnapshot({ digest: 'checksum', exchanges: [{ request: 'request', reply: 'reply', lines: [] }], amendments: [] }) // true\n * ```\n */\nexport function isLedgerWindowSnapshot(value: unknown): value is LedgerWindowSnapshot {\n\treturn holds(() => {\n\t\tif (!isRecord(value) || Object.keys(value).length !== 3 || !isString(value.digest)) return false\n\t\tconst { exchanges, amendments } = value\n\t\tif (!arrayOf(isRecord)(exchanges) || exchanges.length === 0 || !arrayOf(isRecord)(amendments))\n\t\t\treturn false\n\t\treturn (\n\t\t\texchanges.every((entry) => {\n\t\t\t\tconst { request, reply, copy, lines } = entry\n\t\t\t\treturn (\n\t\t\t\t\tObject.keys(entry).length === 3 + Number(copy !== undefined) &&\n\t\t\t\t\tisString(request) &&\n\t\t\t\t\tisString(reply) &&\n\t\t\t\t\t(copy === undefined || isString(copy)) &&\n\t\t\t\t\tarrayOf(isRecord)(lines) &&\n\t\t\t\t\tlines.every(\n\t\t\t\t\t\t(line) =>\n\t\t\t\t\t\t\tObject.keys(line).length === 4 &&\n\t\t\t\t\t\t\tisString(line.key) &&\n\t\t\t\t\t\t\tisString(line.title) &&\n\t\t\t\t\t\t\tisString(line.source) &&\n\t\t\t\t\t\t\tisNonNegativeSafeInteger(line.sentence),\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t}) &&\n\t\t\tamendments.every(\n\t\t\t\t(edge) => Object.keys(edge).length === 2 && isString(edge.earlier) && isString(edge.later),\n\t\t\t)\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is finite and lies in the interval (0, 1].\n * @param value - The value to inspect\n * @returns True if the value is a valid ledger fraction; false otherwise\n * @example\n * ```ts\n * isFraction(0.8) // true\n * ```\n */\nexport function isFraction(value: unknown): value is number {\n\treturn isFiniteNumber(value) && value > 0 && value <= 1\n}\n\n/**\n * Checks whether a value holds the wording a ledger files with: a choice `category` question whose\n * criteria describe every category, a non-empty `topic` string, and noul `amends` and `supersedes`\n * questions.\n *\n * @remarks\n * A ledger asks these on every message, so a hole in them would otherwise surface as filing that quietly\n * stays undecided. Total: an unreadable member returns false.\n *\n * @param value - The value to inspect\n * @returns True if the value holds the wording the ledger's questions read; false otherwise\n * @example\n * ```ts\n * isLedgerQuestion(LEDGER_QUESTIONS) // true\n * isLedgerQuestion({ topic: 'Does it concern this?' }) // false\n * ```\n */\nexport function isLedgerQuestion(value: unknown): value is LedgerQuestion {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { category, topic, amends, supersedes } = value\n\t\tif (!isJudgeQuestion(category) || category.form !== 'choice') return false\n\t\tconst { criteria } = category\n\t\treturn (\n\t\t\tLEDGER_CATEGORIES.every((one) => isJudgeEntry(criteria[one])) &&\n\t\t\tisString(topic) &&\n\t\t\ttopic.trim() !== '' &&\n\t\t\tisJudgeQuestion(amends) &&\n\t\t\tamends.form === 'noul' &&\n\t\t\tisJudgeQuestion(supersedes) &&\n\t\t\tsupersedes.form === 'noul'\n\t\t)\n\t})\n}\n\n/**\n * Checks whether a value is a ledger snapshot whose marked ids name messages of its own conversation.\n *\n * @remarks\n * `conversation` must pass `isConversationSnapshot`, and `requests`, `notes`, and `failures` must be\n * dense arrays of strings. Each request and note id must name a `user` message of\n * `conversation.messages` and each failure id a `tool` message, so an id only a section original\n * holds is refused. A `gauge`, when present, must hold a finite `scale` greater than 0 and a finite\n * `overhead` of at least 0. The guard is exact: an unknown member and an own `undefined` `gauge` return\n * false, at the snapshot and in its `gauge`. Unreadable members and untrusted inputs return false. The\n * guard is structural: it admits a conversation holding a stored `system` role\n * or a repeated id, which `requireConversationSnapshot` and the `Conversation` constructor then\n * refuse as the stored-data check a ledger's hydrate runs.\n * A present `window` must be nonempty and exactly shaped. This guard does not audit its references\n * or sentence ranges. Hydration discards the window when its digest disagrees or cannot be computed.\n *\n * @param value - The value to inspect, such as a parsed checkpoint\n * @returns True if the value has the shape of a ledger snapshot whose marked ids name messages of its own conversation; false otherwise\n * @example\n * ```ts\n * const conversation = { id: 'c1', sections: [], messages: [{ id: 'm1', role: 'user', content: 'Check FX-111.' }] }\n * isLedgerSnapshot({ conversation, requests: ['m1'], notes: [], failures: [] }) // true\n * isLedgerSnapshot({ conversation, requests: [], notes: [], failures: ['m1'] }) // false\n * ```\n */\nexport function isLedgerSnapshot(value: unknown): value is LedgerSnapshot {\n\treturn holds(() => {\n\t\tif (!isRecord(value)) return false\n\t\tconst { conversation, requests, notes, failures, gauge, window } = value\n\t\tif (\n\t\t\tObject.keys(value).length !==\n\t\t\t4 + Number(gauge !== undefined) + Number(window !== undefined)\n\t\t)\n\t\t\treturn false\n\t\tif (!isConversationSnapshot(conversation)) return false\n\t\tconst isIds = arrayOf(isString)\n\t\tif (!isIds(requests) || !isIds(notes) || !isIds(failures)) return false\n\t\tif (gauge !== undefined) {\n\t\t\tif (!isRecord(gauge) || Object.keys(gauge).length !== 2) return false\n\t\t\tconst { scale, overhead } = gauge\n\t\t\tif (!isFiniteNumber(scale) || scale <= 0 || !isFiniteNumber(overhead) || overhead < 0)\n\t\t\t\treturn false\n\t\t}\n\t\tconst roles = new Map(conversation.messages.map((message) => [message.id, message.role]))\n\t\treturn (\n\t\t\trequests.every((id) => roles.get(id) === 'user') &&\n\t\t\tnotes.every((id) => roles.get(id) === 'user') &&\n\t\t\tfailures.every((id) => roles.get(id) === 'tool') &&\n\t\t\t(window === undefined || isLedgerWindowSnapshot(window))\n\t\t)\n\t})\n}\n","import type { AgentInterface, AgentResult } from '../agents/types.js'\nimport type { Selection } from '../contexts/types.js'\nimport type { ConversationInterface } from '../conversations/types.js'\nimport type { ProviderInterface } from '../providers/types.js'\nimport type { Message, ThinkingReplay } from '../types.js'\nimport type {\n\tClassifierResult,\n\tLedgerDraft,\n\tLedgerExchange,\n\tLedgerEvidence,\n\tLedgerOccurrence,\n\tLedgerRenderResult,\n\tLedgerAmendment,\n\tLedgerCategory,\n\tLedgerGauge,\n\tLedgerInterface,\n\tLedgerLine,\n\tLedgerLookupFinding,\n\tLedgerOptions,\n\tLedgerProjection,\n\tLedgerProjectionInput,\n\tLedgerRecord,\n\tLedgerRenderContext,\n\tLedgerResult,\n\tLedgerShare,\n\tLedgerSnapshot,\n} from './types.js'\nimport type { TokenUsage } from '@orkestrel/budget'\nimport type { ToolCall, ToolContext, ToolDefinition, ToolResult } from '@orkestrel/tool'\nimport { createAbort } from '@orkestrel/abort'\nimport {\n\tdescribeFaults,\n\tattempt,\n\tisError,\n\tisFiniteNumber,\n\tisString,\n\tobjectShape,\n\tstringShape,\n} from '@orkestrel/contract'\nimport { createTool, createToolManager } from '@orkestrel/tool'\nimport { createAgent, createAuthority } from '../agents/factories.js'\nimport { AgentError } from '../agents/errors.js'\nimport { estimateMessages } from '../agents/helpers.js'\nimport { createScope } from '../contexts/factories.js'\nimport { createConversationManager } from '../conversations/factories.js'\nimport {\n\tcollectExchanges,\n\tcollectToolGroups,\n\textractLiterals,\n\testimateTokens,\n\thasCalls,\n\tstripThinking,\n\tsumUsage,\n} from '../helpers.js'\nimport { isNonNegativeSafeInteger, isPositiveSafeInteger } from '../validators.js'\nimport { Classifier } from './Classifier.js'\nimport { Gauge } from './Gauge.js'\nimport { LedgerRequest } from './LedgerRequest.js'\nimport { LedgerScan } from './LedgerScan.js'\nimport {\n\tDEFAULT_LEDGER_LIMIT,\n\tDEFAULT_LEDGER_SHARE,\n\tLEDGER_NOTES,\n\tLEDGER_OWNER_PREFIX,\n\tLEDGER_RECALL_LABELS,\n\tLEDGER_RULES_KEY,\n\tLEDGER_SCALE_DRIFT,\n} from './constants.js'\nimport { isLedgerError, LedgerError } from './errors.js'\nimport {\n\thasHiddenRecall,\n\tmatchesLookup,\n\tdigestLedgerWindow,\n\tbuildLedgerCut,\n\tbuildLedgerLineKey,\n\tcollectLedgerLines,\n\tcollectLedgerReplies,\n\tcollectLedgerEvidence,\n\tbuildLedgerIndex,\n\tbuildLines,\n\tbuildLedgerRenderContext,\n\tbuildRecallMessage,\n\tbuildRequestMessage,\n\tcollectProjectionIds,\n\textractWords,\n\tfindLedgerCut,\n\trenderTopicNames,\n\tscanAmendments,\n\tsplitWords,\n\tbuildRecords,\n\tcollectNames,\n\tcollectRegistry,\n\tcutListing,\n\tlinkOwners,\n\tmatchEntities,\n\trankLedgerCut,\n\tresolvePredict,\n\tfindLedgerCall,\n\tmatchesCutLine,\n\trenderLedgerAnnotation,\n\trenderLedgerPinned,\n\trenderLedgerRecord,\n\trenderLookupCall,\n\trenderStub,\n\tselectRecords,\n\tsplitResultLines,\n\tsplitTopic,\n\tsplitSentences,\n} from './helpers.js'\nimport { ledgerSnapshotContract } from './contracts.js'\nimport { isFraction, isLedgerQuestion, isLedgerSnapshot } from './validators.js'\n\n/**\n * Serves one conversation through classified records, a bounded copy of each request turn that carries them, and a final answer pass.\n * @example\n * ```ts\n * const ledger = new Ledger(provider, options)\n * ledger.conversation.add({ role: 'user', content: 'The refund needs approval.' })\n * const reply = await ledger.respond('What approval is needed?')\n * ```\n */\nexport class Ledger implements LedgerInterface {\n\treadonly #provider: ProviderInterface\n\treadonly #options: LedgerOptions\n\treadonly #replay: ThinkingReplay\n\treadonly #predict: number\n\treadonly #share: LedgerShare\n\treadonly #conversation: ConversationInterface\n\treadonly #agent: AgentInterface\n\treadonly #classifier: Classifier\n\treadonly #requests = new Set<string>()\n\treadonly #annotations = new Set<string>()\n\treadonly #results = new Map<string, ToolResult>()\n\treadonly #pending = new Map<number, ToolResult>()\n\treadonly #live = new Set<LedgerRequest>()\n\treadonly #scans = new Set<LedgerScan>()\n\treadonly #calibrations = new Set<AbortSignal>()\n\t#gauge: Gauge | undefined\n\t#window: readonly LedgerExchange[] = []\n\t#evidence: LedgerEvidence = collectLedgerEvidence([], new Set(), [])\n\n\t/**\n\t * Composes the conversation, classifier, tools, and agent.\n\t * @param provider - The provider that serves the conversation\n\t * @param options - The filing policy, capacity, and request bounds\n\t * @throws {LedgerError} Thrown when a threshold, share, capacity, limit, topic, lookup, or gauge is invalid\n\t * @throws {LedgerError} Thrown when `predict` is not a nonnegative safe integer less than `capacity` (code `'CAPACITY'`)\n\t * @throws {LedgerError} Thrown when `questions` fails `isLedgerQuestion` (code `'QUESTION'`)\n\t * @throws {LedgerError} Thrown when `snapshot` is malformed or marks an id its conversation does not hold (code `'SNAPSHOT'`, its faults under `context.faults`)\n\t * @throws {ConversationError} Thrown when `isLedgerSnapshot` admits `snapshot` but its conversation holds a `system` message (code `'ROLE'`) or repeats an id (code `'SNAPSHOT'`)\n\t */\n\tconstructor(provider: ProviderInterface, options: LedgerOptions) {\n\t\tfor (const key of ['category', 'topic', 'correction', 'amends', 'supersedes'] as const) {\n\t\t\tconst value = options.thresholds[key]\n\t\t\tif (!isFraction(value))\n\t\t\t\tthrow new LedgerError('THRESHOLD', 'thresholds must be finite and in (0, 1]')\n\t\t}\n\t\tfor (const value of Object.values(options.share ?? {})) {\n\t\t\tif (!isFraction(value)) throw new LedgerError('SHARE', 'shares must be finite and in (0, 1]')\n\t\t}\n\t\tif (!isPositiveSafeInteger(options.capacity))\n\t\t\tthrow new LedgerError('CAPACITY', 'capacity must be a positive safe integer')\n\t\tthis.#predict = resolvePredict(options.predict, options.capacity)\n\t\tif (options.recall?.limit !== undefined && !isNonNegativeSafeInteger(options.recall.limit))\n\t\t\tthrow new LedgerError('LIMIT', 'a recall limit must be a nonnegative safe integer')\n\t\tif (options.agent?.limit !== undefined && !isPositiveSafeInteger(options.agent.limit))\n\t\t\tthrow new LedgerError('LIMIT', 'an agent limit must be a positive safe integer')\n\t\t// `Array.from` visits a hole in either list as an undefined entry, as iteration does, so the\n\t\t// read of its `name` refuses it; `map` skips it.\n\t\tconst topics = Array.from(options.topics, (topic) => topic.name)\n\t\tif (topics.some((name) => name.trim() === '') || new Set(topics).size !== topics.length)\n\t\t\tthrow new LedgerError('TOPIC', 'topic names must be nonempty and unique')\n\t\tconst lookups = ['recall', ...Array.from(options.lookups ?? [], (lookup) => lookup.tool.name)]\n\t\tif (new Set(lookups).size !== lookups.length)\n\t\t\tthrow new LedgerError('LOOKUP', 'lookup names must be unique and cannot be recall')\n\t\tif (!isLedgerQuestion(options.questions))\n\t\t\tthrow new LedgerError(\n\t\t\t\t'QUESTION',\n\t\t\t\t'questions must hold a choice category question whose criteria describe every category, a non-empty topic string, and noul amends and supersedes questions; pass the LEDGER_QUESTIONS constant for the measured wording',\n\t\t\t)\n\t\tconst snapshot = options.snapshot\n\t\tif (snapshot !== undefined && !isLedgerSnapshot(snapshot)) {\n\t\t\tconst candidate: unknown = options.snapshot\n\t\t\tconst faults = ledgerSnapshotContract.audit(candidate)\n\t\t\tthrow new LedgerError(\n\t\t\t\t'SNAPSHOT',\n\t\t\t\t`snapshot must be well formed and mark only messages of its conversation; pass what snapshot() returned${faults.length === 0 ? '' : `\\n${describeFaults(faults, ledgerSnapshotContract.schema)}`}`,\n\t\t\t\t{ context: { faults } },\n\t\t\t)\n\t\t}\n\t\tthis.#provider = provider\n\t\tthis.#replay = provider.replay ?? 'none'\n\t\tthis.#options = options\n\t\tthis.#share = { ...DEFAULT_LEDGER_SHARE, ...options.share }\n\t\tconst gauge = options.gauge ?? snapshot?.gauge\n\t\tif (gauge !== undefined)\n\t\t\tthis.#gauge = new Gauge({\n\t\t\t\t...gauge,\n\t\t\t\tcapacity: options.capacity,\n\t\t\t\tpredict: this.#predict,\n\t\t\t\treplay: this.#replay,\n\t\t\t})\n\t\tconst conversations = createConversationManager()\n\t\tthis.#conversation =\n\t\t\tsnapshot === undefined\n\t\t\t\t? conversations.add()\n\t\t\t\t: conversations.add({ snapshot: snapshot.conversation })\n\t\tconversations.switch(this.#conversation.id)\n\t\tif (snapshot !== undefined) this.#seat(snapshot)\n\t\tthis.#classifier = new Classifier({\n\t\t\tconversation: this.#conversation,\n\t\t\tjudge: options.judge,\n\t\t\tquestions: options.questions,\n\t\t\tthresholds: options.thresholds,\n\t\t\ttopics: options.topics,\n\t\t\tassign: this.#assign.bind(this),\n\t\t\tentities: (text, partial) => {\n\t\t\t\t// The held scan's index serves its selection or recall; without one, this call reads the lookups.\n\t\t\t\tconst [scan] = this.#scans\n\t\t\t\treturn matchEntities(\n\t\t\t\t\tscan?.index ??\n\t\t\t\t\t\tbuildLedgerIndex(collectRegistry(this.#readLookups(this.#conversation.messages()))),\n\t\t\t\t\ttext,\n\t\t\t\t\tpartial,\n\t\t\t\t)\n\t\t\t},\n\t\t\t...(options.correction === undefined ? {} : { correction: options.correction }),\n\t\t\t...(options.agent?.scheduler === undefined ? {} : { scheduler: options.agent.scheduler }),\n\t\t\t...(options.agent?.strict === undefined ? {} : { strict: options.agent.strict }),\n\t\t})\n\t\tconst tools = createToolManager()\n\t\tfor (const lookup of options.lookups ?? []) {\n\t\t\ttools.add(\n\t\t\t\tcreateTool({\n\t\t\t\t\tname: lookup.tool.name,\n\t\t\t\t\t...(lookup.tool.parameters === undefined ? {} : { parameters: lookup.tool.parameters }),\n\t\t\t\t\t...(lookup.tool.description === undefined\n\t\t\t\t\t\t? {}\n\t\t\t\t\t\t: { description: lookup.tool.description }),\n\t\t\t\t\t...(lookup.tool.summary === undefined ? {} : { summary: lookup.tool.summary }),\n\t\t\t\t\t...(lookup.tool.title === undefined ? {} : { title: lookup.tool.title }),\n\t\t\t\t\t...(lookup.tool.annotations === undefined\n\t\t\t\t\t\t? {}\n\t\t\t\t\t\t: { annotations: lookup.tool.annotations }),\n\t\t\t\t\texecute: (args, context) => this.#lookup(lookup.tool.name, args, context),\n\t\t\t\t}),\n\t\t\t)\n\t\t}\n\t\ttools.add(\n\t\t\tcreateTool({\n\t\t\t\tname: 'recall',\n\t\t\t\tdescription: `Find something said earlier that isn't in the messages you can see, by an owner name, an id, or one of the topics ${renderTopicNames(options.topics)}. Returns statements and lookup findings first, then earlier requests and replies labeled by origin; newest first within each group.`,\n\t\t\t\tcontract: objectShape({\n\t\t\t\t\ttopic: stringShape({ min: 1, description: 'An owner name, an id, or a topic' }),\n\t\t\t\t}),\n\t\t\t\texecute: (args) => this.#recall(args),\n\t\t\t}),\n\t\t)\n\t\t// The ledger holds no approver, and it allows exactly the names it advertises, so a tool a host\n\t\t// adds to the agent's manager later is denied instead of inheriting those names' trust.\n\t\tthis.#agent = createAgent(provider, {\n\t\t\t...options.agent,\n\t\t\tconversations,\n\t\t\tsystem: options.system,\n\t\t\ttools,\n\t\t\tauthority: createAuthority({\n\t\t\t\trules: [\n\t\t\t\t\t{\n\t\t\t\t\t\tmatch: (context) => lookups.includes(context.call.name),\n\t\t\t\t\t\tzone: 'ledger',\n\t\t\t\t\t\tverdict: 'allow',\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\tfallback: { zone: 'ledger', verdict: 'deny', reason: 'the ledger advertises no such tool' },\n\t\t\t}),\n\t\t\tlimit: options.agent?.limit ?? DEFAULT_LEDGER_LIMIT,\n\t\t\tselect: (conversation, request, signal) => this.#select(conversation, request, signal),\n\t\t})\n\t\tthis.#agent.emitter.on('tool', (_call, result) => {\n\t\t\tthis.#flush()\n\t\t\tthis.#pending.set(this.#conversation.messages().length, result)\n\t\t\tif (!result.success && result.error === LEDGER_NOTES.repeat) {\n\t\t\t\tthis.#current()?.close()\n\t\t\t\tthis.#agent.abort('repeat')\n\t\t\t}\n\t\t})\n\t\tthis.#agent.emitter.on('select', (selection) => {\n\t\t\tif (isLedgerError(selection.fault) && selection.fault.code === 'REQUEST') return\n\t\t\tthis.#current()?.adopt(selection, this.#conversation.messages().length)\n\t\t})\n\t\tthis.#agent.emitter.on('fault', (error) => this.#current()?.fault(error))\n\t\tthis.#agent.emitter.on('turn', () => this.#observeTurn())\n\t\tthis.#agent.emitter.on('usage', (usage) => this.#current()?.spend(usage))\n\t}\n\n\t/**\n\t * Returns the owned request engine.\n\t * @returns The engine serving this conversation\n\t */\n\tget agent(): AgentInterface {\n\t\treturn this.#agent\n\t}\n\t/**\n\t * Returns the owned message history.\n\t * @returns The history filed and served by this ledger\n\t */\n\tget conversation(): ConversationInterface {\n\t\treturn this.#conversation\n\t}\n\t/**\n\t * Returns the stored prompt price.\n\t * @returns The scale and overhead cost, or undefined before calibration\n\t */\n\tget gauge(): LedgerGauge | undefined {\n\t\treturn this.#gauge === undefined\n\t\t\t? undefined\n\t\t\t: { scale: this.#gauge.scale, overhead: this.#gauge.overhead }\n\t}\n\n\t/**\n\t * Measures message scale and tool overhead from provider prompt usage.\n\t * @param signal - The signal bounding both calibration calls; an abort rejects with its reason\n\t * @returns The stored scale and overhead cost\n\t * @throws {LedgerError} Thrown when either call reports no prompt usage, or a prompt usage of 0 or less (code `'GAUGE'`)\n\t * @throws {AgentError} Thrown when a request or calibration is active (code `'CONCURRENCY'`)\n\t */\n\tasync calibrate(signal: AbortSignal): Promise<LedgerGauge> {\n\t\tif (this.#busy()) throw new AgentError('CONCURRENCY', 'a ledger request is already active')\n\t\tthis.#calibrations.add(signal)\n\t\ttry {\n\t\t\treturn await this.#measureGauge(signal)\n\t\t} finally {\n\t\t\tthis.#calibrations.delete(signal)\n\t\t}\n\t}\n\n\t/**\n\t * Serializes the conversation, marked ids, gauge, and completed exchange window for a later ledger.\n\t * @returns The snapshot, each id list in conversation order, with a gauge and window only when held\n\t * @throws {AgentError} Thrown when a request or calibration is active (code `'CONCURRENCY'`)\n\t */\n\tsnapshot(): LedgerSnapshot {\n\t\tif (this.#busy()) throw new AgentError('CONCURRENCY', 'a ledger request is already active')\n\t\tthis.#flush()\n\t\tconst conversation = this.#conversation.snapshot()\n\t\tconst ids = conversation.messages.map((message) => message.id)\n\t\tconst gauge = this.gauge\n\t\tconst window = {\n\t\t\texchanges: this.#window.map((entry) => ({\n\t\t\t\trequest: entry.request.id,\n\t\t\t\t...(entry.request.content === this.#conversation.message(entry.request.id)?.content\n\t\t\t\t\t? {}\n\t\t\t\t\t: { copy: entry.request.content }),\n\t\t\t\treply: entry.reply.id,\n\t\t\t\tlines: entry.lines.map(({ key, title, source, sentence }) => ({\n\t\t\t\t\tkey,\n\t\t\t\t\ttitle,\n\t\t\t\t\tsource,\n\t\t\t\t\tsentence,\n\t\t\t\t})),\n\t\t\t})),\n\t\t\tamendments: [...this.#evidence.amendments.values()].map((edge) => ({ ...edge })),\n\t\t}\n\t\tconst digest =\n\t\t\twindow.exchanges.length === 0\n\t\t\t\t? undefined\n\t\t\t\t: digestLedgerWindow(window.exchanges, conversation.messages)\n\t\treturn {\n\t\t\tconversation,\n\t\t\trequests: ids.filter((id) => this.#requests.has(id)),\n\t\t\tnotes: ids.filter((id) => this.#annotations.has(id)),\n\t\t\tfailures: ids.filter((id) => this.#results.get(id)?.success === false),\n\t\t\t...(gauge === undefined ? {} : { gauge }),\n\t\t\t...(window.exchanges.length === 0 || digest === undefined\n\t\t\t\t? {}\n\t\t\t\t: { window: { ...window, digest } }),\n\t\t}\n\t}\n\n\t/**\n\t * Appends a request and serves it, recovering an unfinished or rejected first pass with one answer pass.\n\t * A calibration call that reports no prompt usage, or a prompt usage of 0 or less, rejects with `LedgerError` code `'GAUGE'`; a provider error during calibration rejects with that error.\n\t * When the answer pass rejects, `respond` rejects with that error, and under the default `strict` a judge failure or another selection failure rejects it too. A rejection belongs to its own call: the ledger stays free, and the next call asks the judge again about whatever is still unanswered.\n\t * @param content - The request text\n\t * @param signal - The caller's cancellation signal; an abort during calibration rejects with its reason\n\t * @returns The final pass, the usage of every pass, and the filing attempts of this request alone\n\t * @throws {AgentError} Thrown when a request or calibration is active (code `'CONCURRENCY'`)\n\t * @throws {LedgerError} Thrown when a calibration call reports no prompt usage, or a prompt usage of 0 or less (code `'GAUGE'`)\n\t * @throws {ContractError} Thrown when `signal` is defined and is not a native `AbortSignal`\n\t */\n\tasync respond(content: string, signal?: AbortSignal): Promise<LedgerResult> {\n\t\tif (this.#busy()) throw new AgentError('CONCURRENCY', 'a ledger request is already active')\n\t\tconst abort = createAbort({ ...(signal === undefined ? {} : { signal }) })\n\t\tconst caller = abort.signal\n\t\tconst request = new LedgerRequest(content, this.#options.recall?.limit)\n\t\tconst scope = this.#agent.context.scope\n\t\tthis.#live.add(request)\n\t\ttry {\n\t\t\tif (this.#gauge === undefined) await this.#measureGauge(caller)\n\t\t\tconst entered = request.enter(this.#conversation)\n\t\t\tthis.#requests.add(entered.id)\n\t\t\t// The answer pass recovers a provider failure in the first pass; the agent's error event carries\n\t\t\t// its cause. A selection failure that reached here rejected under `strict`, and it stands.\n\t\t\tconst first = await this.#generatePass(request, caller, this.#options.think).catch(\n\t\t\t\t(error: unknown): AgentResult => {\n\t\t\t\t\tif (request.faulted(error)) throw error\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: '',\n\t\t\t\t\t\tpartial: true,\n\t\t\t\t\t\t...(request.usage === undefined ? {} : { usage: request.usage }),\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t)\n\t\t\tconst passes = [first]\n\t\t\tif (!caller.aborted && (first.partial || first.content.trim() === '')) {\n\t\t\t\tconst digest = this.#buildDigest(request, entered)\n\t\t\t\tif (digest !== undefined)\n\t\t\t\t\tthis.#annotations.add(this.#conversation.add({ role: 'user', content: digest }).id)\n\t\t\t\tthis.#annotations.add(\n\t\t\t\t\tthis.#conversation.add({ role: 'user', content: LEDGER_NOTES.cue }).id,\n\t\t\t\t)\n\t\t\t\tconst previous = this.#agent.context.scope\n\t\t\t\tconst answer = createScope({\n\t\t\t\t\t...previous,\n\t\t\t\t\tname: 'answer',\n\t\t\t\t\ttools: [],\n\t\t\t\t\tselect: async (conversation, message, runSignal) => {\n\t\t\t\t\t\tconst selection = await this.#select(conversation, message, runSignal)\n\t\t\t\t\t\tif (selection.fault !== undefined) return selection\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...selection,\n\t\t\t\t\t\t\tmessages: selection.messages.filter((one) => one.role !== 'tool' && !hasCalls(one)),\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t\tthis.#agent.context.apply(answer)\n\t\t\t\ttry {\n\t\t\t\t\tpasses.push(await this.#generatePass(request, caller, false))\n\t\t\t\t} finally {\n\t\t\t\t\tif (this.#agent.context.scope === answer) this.#agent.context.apply(previous)\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst last = passes.at(-1) ?? first\n\t\t\trequest.think(this.#conversation.messages())\n\t\t\tconst calls = request.calls\n\t\t\tthis.#gauge?.observe(\n\t\t\t\tcalls,\n\t\t\t\t!last.partial && last.content.trim() !== '' ? calls.at(-1) : undefined,\n\t\t\t)\n\t\t\tconst usage = passes.reduce<TokenUsage | undefined>(\n\t\t\t\t(total, pass) => (pass.usage === undefined ? total : sumUsage(total, pass.usage)),\n\t\t\t\tundefined,\n\t\t\t)\n\t\t\t// Joined as a `joinThinking` fold would join them: an empty part adds no separator.\n\t\t\tconst parts = passes.flatMap((pass) => (pass.thinking === undefined ? [] : [pass.thinking]))\n\t\t\treturn {\n\t\t\t\tcontent: last.content,\n\t\t\t\tpartial: last.partial,\n\t\t\t\tpasses,\n\t\t\t\tattempts: request.attempts,\n\t\t\t\t...(usage === undefined ? {} : { usage }),\n\t\t\t\t...(parts.length === 0\n\t\t\t\t\t? {}\n\t\t\t\t\t: { thinking: parts.filter((part) => part.length > 0).join('\\n\\n') }),\n\t\t\t\t...(last.stop === undefined ? {} : { stop: last.stop }),\n\t\t\t}\n\t\t} finally {\n\t\t\tif (request.scope !== undefined && this.#agent.context.scope === request.scope)\n\t\t\t\tthis.#agent.context.apply(scope)\n\t\t\tthis.#flush()\n\t\t\tthis.#commitExchange(request)\n\t\t\tthis.#live.delete(request)\n\t\t}\n\t}\n\n\tasync #measureGauge(signal: AbortSignal): Promise<LedgerGauge> {\n\t\ttry {\n\t\t\t// The empty turn stands where the request will, so a workspace's images have a user turn to\n\t\t\t// read, and it leaves with them: calibration prices text.\n\t\t\tconst messages = stripThinking(\n\t\t\t\tthis.#agent.context.build({\n\t\t\t\t\tmessages: [\n\t\t\t\t\t\t...this.#conversation.view(),\n\t\t\t\t\t\t{ id: 'calibration', role: 'user', content: '' },\n\t\t\t\t\t],\n\t\t\t\t\tjudgments: [],\n\t\t\t\t}),\n\t\t\t\tthis.#replay,\n\t\t\t).slice(0, -1)\n\t\t\tconst priced = await this.#provider.generate(\n\t\t\t\tmessages,\n\t\t\t\tsignal,\n\t\t\t\tthis.#agent.context.tools.definitions(),\n\t\t\t\t{ think: false },\n\t\t\t)\n\t\t\tconst bare = await this.#provider.generate(messages, signal, undefined, { think: false })\n\t\t\tif (\n\t\t\t\t!isFiniteNumber(priced.usage?.prompt) ||\n\t\t\t\tpriced.usage.prompt <= 0 ||\n\t\t\t\t!isFiniteNumber(bare.usage?.prompt) ||\n\t\t\t\tbare.usage.prompt <= 0\n\t\t\t)\n\t\t\t\tthrow new LedgerError('GAUGE', 'calibration requires prompt usage from both calls')\n\t\t\tthis.#gauge = new Gauge({\n\t\t\t\tcapacity: this.#options.capacity,\n\t\t\t\tpredict: this.#predict,\n\t\t\t\treplay: this.#replay,\n\t\t\t\tscale: bare.usage.prompt / estimateMessages(messages),\n\t\t\t\toverhead: Math.max(0, priced.usage.prompt - bare.usage.prompt),\n\t\t\t})\n\t\t\treturn { scale: this.#gauge.scale, overhead: this.#gauge.overhead }\n\t\t} catch (error) {\n\t\t\tsignal.throwIfAborted()\n\t\t\tthrow error\n\t\t}\n\t}\n\n\tasync #generatePass(\n\t\trequest: LedgerRequest,\n\t\tsignal: AbortSignal,\n\t\tthink?: boolean,\n\t): Promise<AgentResult> {\n\t\trequest.open(this.#conversation.messages().length)\n\t\treturn await this.#agent.generate({\n\t\t\tsignal,\n\t\t\tconversation: this.#conversation,\n\t\t\t...(think === undefined ? {} : { think }),\n\t\t})\n\t}\n\n\t// The live `respond` call's request after it has entered its message; a run during the call's\n\t// calibration reaches none. A refusal on every add caps the registry at one request.\n\t#current(): LedgerRequest | undefined {\n\t\tconst [request] = this.#live\n\t\treturn request?.message === undefined ? undefined : request\n\t}\n\n\t#busy(): boolean {\n\t\treturn this.#live.size > 0 || this.#calibrations.size > 0\n\t}\n\n\t#flush(): void {\n\t\t// Filing calls this once per message, and reading the messages copies them all.\n\t\tif (this.#pending.size === 0) return\n\t\tconst messages = this.#conversation.messages()\n\t\tfor (const [at, result] of this.#pending) {\n\t\t\tconst message = messages[at]\n\t\t\tif (message?.role === 'tool') {\n\t\t\t\tthis.#results.set(message.id, result)\n\t\t\t\tthis.#pending.delete(at)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Seats the hydrated marks. A failure is rebuilt as the result the agent's `tool` event carried:\n\t// its error is the tool message's content, and its name is the call's, or `tool` when no call\n\t// pairs with it, as a tail stub names it.\n\t#seat(snapshot: LedgerSnapshot): void {\n\t\tfor (const id of snapshot.requests) this.#requests.add(id)\n\t\tfor (const id of snapshot.notes) this.#annotations.add(id)\n\t\tconst messages = this.#conversation.messages()\n\t\tif (\n\t\t\tsnapshot.window !== undefined &&\n\t\t\tdigestLedgerWindow(snapshot.window.exchanges, messages) === snapshot.window.digest\n\t\t) {\n\t\t\tconst window = snapshot.window.exchanges.map((entry) => {\n\t\t\t\tconst request = this.#conversation.message(entry.request)\n\t\t\t\tconst reply = this.#conversation.message(entry.reply)\n\t\t\t\tif (request === undefined || reply === undefined)\n\t\t\t\t\tthrow new LedgerError('SNAPSHOT', 'window pair must belong to its conversation')\n\t\t\t\tconst lines = entry.lines.map((line) => {\n\t\t\t\t\tconst source = this.#conversation.message(line.source)\n\t\t\t\t\tif (source === undefined)\n\t\t\t\t\t\tthrow new LedgerError('SNAPSHOT', 'window source must belong to its conversation')\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...line,\n\t\t\t\t\t\ttext: splitSentences(source.content)[line.sentence] ?? '',\n\t\t\t\t\t\ttopics: [],\n\t\t\t\t\t\trole: source.role,\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\treturn {\n\t\t\t\t\trequest: { ...request, content: entry.copy ?? request.content },\n\t\t\t\t\treply,\n\t\t\t\t\tlines,\n\t\t\t\t}\n\t\t\t})\n\t\t\tthis.#window = window\n\t\t\tthis.#evidence = collectLedgerEvidence(\n\t\t\t\tmessages,\n\t\t\t\tnew Set(\n\t\t\t\t\tthis.#window.flatMap((entry) => [\n\t\t\t\t\t\tentry.request.id,\n\t\t\t\t\t\tentry.reply.id,\n\t\t\t\t\t\t...entry.lines.map((line) => line.source),\n\t\t\t\t\t]),\n\t\t\t\t),\n\t\t\t\tsnapshot.window.amendments,\n\t\t\t)\n\t\t}\n\t\tconst groups = collectToolGroups(messages)\n\t\tfor (const message of messages) {\n\t\t\tif (!snapshot.failures.includes(message.id)) continue\n\t\t\tconst group = groups.find((entries) => entries.some((one) => one.id === message.id))\n\t\t\tconst call = group === undefined ? undefined : findLedgerCall(group, message)\n\t\t\tthis.#results.set(message.id, {\n\t\t\t\tsuccess: false,\n\t\t\t\tid: call?.id ?? message.call ?? message.id,\n\t\t\t\tname: call?.name ?? 'tool',\n\t\t\t\terror: message.content,\n\t\t\t})\n\t\t}\n\t}\n\n\t// A held scan already read every lookup, caching each failed read, and holds the positions.\n\t#assign(message: Message): LedgerCategory | undefined {\n\t\tthis.#flush()\n\t\tif (this.#annotations.has(message.id)) return 'chatter'\n\t\tconst [scan] = this.#scans\n\t\tif (message.role === 'tool') {\n\t\t\tif (scan === undefined) this.#readLookups(this.#conversation.messages())\n\t\t\treturn this.#results.get(message.id)?.success === false ? 'chatter' : 'fact'\n\t\t}\n\t\tif (message.role !== 'assistant') return undefined\n\t\tif (hasCalls(message)) return 'chatter'\n\t\tif (scan !== undefined) {\n\t\t\tconst at = scan.position(message.id)\n\t\t\treturn scan.first !== undefined && at !== undefined && at > scan.first ? 'chatter' : undefined\n\t\t}\n\t\tconst messages = this.#conversation.messages()\n\t\tconst first = messages.findIndex((one) => this.#requests.has(one.id))\n\t\treturn first >= 0 && messages.findIndex((one) => one.id === message.id) > first\n\t\t\t? 'chatter'\n\t\t\t: undefined\n\t}\n\n\t#readLookups(messages: readonly Message[]): readonly LedgerLookupFinding[] {\n\t\tthis.#flush()\n\t\treturn collectToolGroups(messages).flatMap((group) =>\n\t\t\tgroup.slice(1).flatMap((message): LedgerLookupFinding[] => {\n\t\t\t\tconst call = findLedgerCall(group, message)\n\t\t\t\tconst lookup = this.#options.lookups?.find((one) => one.tool.name === call?.name)\n\t\t\t\tif (\n\t\t\t\t\tcall === undefined ||\n\t\t\t\t\tlookup === undefined ||\n\t\t\t\t\tthis.#results.get(message.id)?.success === false\n\t\t\t\t)\n\t\t\t\t\treturn []\n\t\t\t\tconst read = attempt(() => lookup.read(call.arguments, message.content))\n\t\t\t\tif (!read.success) {\n\t\t\t\t\t// Caches the failed read, so no later reading calls its handler again.\n\t\t\t\t\tthis.#results.set(message.id, {\n\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\tid: call.id,\n\t\t\t\t\t\tname: call.name,\n\t\t\t\t\t\terror: isError(read.error) ? read.error.message : String(read.error),\n\t\t\t\t\t})\n\t\t\t\t\treturn []\n\t\t\t\t}\n\t\t\t\tconst result = read.value\n\t\t\t\treturn [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: message.id,\n\t\t\t\t\t\tname: call.name,\n\t\t\t\t\t\targuments: call.arguments,\n\t\t\t\t\t\ttext: message.content,\n\t\t\t\t\t\tresult:\n\t\t\t\t\t\t\tresult === undefined\n\t\t\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\t\t\t\tids: [\n\t\t\t\t\t\t\t\t\t\t\t...new Set([\n\t\t\t\t\t\t\t\t\t\t\t\t...result.ids,\n\t\t\t\t\t\t\t\t\t\t\t\t...Object.values(call.arguments)\n\t\t\t\t\t\t\t\t\t\t\t\t\t.filter(isString)\n\t\t\t\t\t\t\t\t\t\t\t\t\t.flatMap((value) => [...extractLiterals(value).ids]),\n\t\t\t\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t}),\n\t\t)\n\t}\n\n\t#project(scan: LedgerScan): {\n\t\treadonly input: LedgerProjectionInput\n\t\treadonly projection: LedgerProjection\n\t} {\n\t\tconst input: LedgerProjectionInput = {\n\t\t\tsystem: this.#options.system,\n\t\t\texclusions: [...this.#requests, ...this.#annotations],\n\t\t\towners: scan.registry.owners,\n\t\t\tmessages: scan.messages,\n\t\t\tfindings: scan.findings,\n\t\t\tentities: new Map(\n\t\t\t\tscan.messages.map((message) => [\n\t\t\t\t\tmessage.id,\n\t\t\t\t\t[...matchEntities(scan.index, message.content, true)],\n\t\t\t\t]),\n\t\t\t),\n\t\t\tclassification: this.#classifier.classification(),\n\t\t}\n\t\treturn { input, projection: buildRecords(input) }\n\t}\n\n\t#projectLines(input: LedgerProjectionInput, id: string) {\n\t\treturn buildLines(\n\t\t\tinput,\n\t\t\tnew Map(input.messages.map((message) => [message.id, message])),\n\t\t\tid,\n\t\t\t[...input.owners.values()].flat(),\n\t\t\tcollectNames(this.#options.system),\n\t\t)\n\t}\n\n\t#renderSource(\n\t\tid: string,\n\t\tsentences: readonly string[],\n\t\tlines: readonly LedgerLine[],\n\t\tcontext: LedgerRenderContext,\n\t\tscan: LedgerScan,\n\t): string {\n\t\tconst message = this.#conversation.message(id)\n\t\tif (message === undefined) return ''\n\t\tconst retained = new Set(lines.map((line) => line.sentence))\n\t\tconst text = sentences.every((_, index) => retained.has(index))\n\t\t\t? message.content\n\t\t\t: lines.map((line) => sentences[line.sentence] ?? '').join(' ')\n\t\tif (text === '') return ''\n\t\tconst finding = scan.finding(id)\n\t\treturn [\n\t\t\t...lines.map((line) => renderLedgerAnnotation(line, context)),\n\t\t\tmessage.role !== 'tool' || finding === undefined\n\t\t\t\t? text\n\t\t\t\t: `${renderLookupCall(finding.name, finding.arguments)}: ${text}`,\n\t\t]\n\t\t\t.filter((part) => part !== '')\n\t\t\t.join('\\n')\n\t}\n\n\t#collectAfter(request: Message): readonly Message[] {\n\t\tconst messages = this.#conversation.messages()\n\t\tconst start = messages.findIndex((message) => message.id === request.id)\n\t\treturn messages\n\t\t\t.slice(start + 1)\n\t\t\t.filter(\n\t\t\t\t(message) =>\n\t\t\t\t\tmessage.role !== 'assistant' || hasCalls(message) || message.content.trim() !== '',\n\t\t\t)\n\t}\n\n\t// A run owns a selection only on the ledger's conversation, by identity, and only for the active\n\t// call's request or a ledger note; any other run is refused over its own conversation's view, so\n\t// the refusal never carries the ledger's history to another thread.\n\tasync #select(\n\t\tconversation: ConversationInterface,\n\t\tmessage: Message,\n\t\tsignal: AbortSignal,\n\t): Promise<Selection> {\n\t\tconst request = this.#current()\n\t\tconst selected = request?.message\n\t\tif (\n\t\t\tconversation !== this.#conversation ||\n\t\t\trequest === undefined ||\n\t\t\tselected === undefined ||\n\t\t\t(message.id !== selected.id && !this.#annotations.has(message.id))\n\t\t)\n\t\t\treturn this.#refuse(\n\t\t\t\tnew LedgerError(\n\t\t\t\t\t'REQUEST',\n\t\t\t\t\t'ledger selection requires a request owned by an active respond call',\n\t\t\t\t),\n\t\t\t\t{ judgments: [] },\n\t\t\t\tconversation,\n\t\t\t)\n\t\tconst plan = request.plan\n\t\tif (this.#annotations.has(message.id) && plan !== undefined)\n\t\t\treturn { messages: [...plan.messages, ...this.#collectAfter(selected)], judgments: [] }\n\t\t// A scan that throws holds no entry, so the handlers and the plan read the conversation and\n\t\t// the lookups themselves and throw where the classification and the plan catch it.\n\t\tconst scanned = attempt(() => this.#scan(this.#conversation.messages()))\n\t\tif (scanned.success) this.#hold(scanned.value)\n\t\ttry {\n\t\t\treturn await this.#classifier.classify(this.#requests, signal).then(\n\t\t\t\t(classified) => this.#file(classified, request, selected, message),\n\t\t\t\t(error: unknown) => this.#refuse(error, { judgments: [] }, this.#conversation),\n\t\t\t)\n\t\t} finally {\n\t\t\tconst [scan] = this.#scans\n\t\t\tif (scan !== undefined) this.#scans.delete(scan)\n\t\t}\n\t}\n\n\t// Reads each lookup result of the messages once and holds the values a selection or a recall reads.\n\t#scan(messages: readonly Message[]): LedgerScan {\n\t\treturn new LedgerScan(messages, this.#readLookups(messages), this.#requests)\n\t}\n\n\t// Adds a scan to the registry, which a refusal on every add caps at one entry.\n\t#hold(scan: LedgerScan): void {\n\t\tif (this.#scans.size > 0) throw new AgentError('CONCURRENCY', 'a ledger scan is already held')\n\t\tthis.#scans.add(scan)\n\t}\n\n\t// Returns the held scan while the conversation keeps its message ids in order; otherwise holds,\n\t// in its place, a scan of the conversation as it reads at this call, so one plan never mixes the two.\n\t#rescan(): LedgerScan {\n\t\tconst messages = this.#conversation.messages()\n\t\tconst [held] = this.#scans\n\t\tif (\n\t\t\theld !== undefined &&\n\t\t\theld.messages.length === messages.length &&\n\t\t\tmessages.every((one, at) => held.messages[at]?.id === one.id)\n\t\t)\n\t\t\treturn held\n\t\tconst scan = this.#scan(messages)\n\t\tif (held !== undefined) this.#scans.delete(held)\n\t\tthis.#hold(scan)\n\t\treturn scan\n\t}\n\n\t// Files a classification's attempts into the request and plans the selection, falling back to\n\t// the conversation view when the filing faulted or planning threw.\n\t#file(\n\t\tclassified: ClassifierResult,\n\t\trequest: LedgerRequest,\n\t\tselected: Message,\n\t\tmessage: Message,\n\t): Selection {\n\t\tconst { attempts, failure, ...filed } = classified\n\t\tconst planned = attempt(() => {\n\t\t\tif (filed.fault !== undefined) throw filed.fault\n\t\t\tconst { draft, ...plan } = this.#plan(selected, this.#rescan())\n\t\t\treturn {\n\t\t\t\tdraft,\n\t\t\t\tselection: {\n\t\t\t\t\t...plan,\n\t\t\t\t\t...filed,\n\t\t\t\t\t// A lenient run keeps the plan beside the filing failure.\n\t\t\t\t\t...(failure === undefined ? {} : { fault: failure }),\n\t\t\t\t},\n\t\t\t}\n\t\t})\n\t\trequest.file(\n\t\t\tattempts,\n\t\t\tplanned.success ? planned.value.selection : undefined,\n\t\t\tplanned.success ? planned.value.draft : undefined,\n\t\t)\n\t\tif (!planned.success) return this.#refuse(planned.error, filed, this.#conversation)\n\t\tconst selection = planned.value.selection\n\t\treturn this.#annotations.has(message.id)\n\t\t\t? {\n\t\t\t\t\t...selection,\n\t\t\t\t\tmessages: [...selection.messages, ...this.#collectAfter(selected)],\n\t\t\t\t}\n\t\t\t: selection\n\t}\n\n\t// Falls back to the given conversation's view with the failure as the selection's fault, keeping\n\t// the filing completed before it.\n\t#refuse(\n\t\terror: unknown,\n\t\tfiled: Omit<ClassifierResult, 'attempts'>,\n\t\tconversation: ConversationInterface,\n\t): Selection {\n\t\tif (conversation === this.#conversation && !(isLedgerError(error) && error.code === 'REQUEST'))\n\t\t\tthis.#current()?.file([])\n\t\treturn {\n\t\t\t...filed,\n\t\t\tmessages: conversation.view(),\n\t\t\tfault: isError(error) ? error : new Error(String(error)),\n\t\t}\n\t}\n\n\t#price(messages: readonly Message[]): number {\n\t\treturn (this.#gauge?.scale ?? 1) * estimateMessages(messages)\n\t}\n\n\t#ruled(source: string, role: Message['role']): boolean {\n\t\tconst category = this.#classifier.category(source)\n\t\tconst unsettled = role === 'user' && !this.#classifier.decisive(source)\n\t\treturn !unsettled && (category === 'rule' || category === 'correction')\n\t}\n\n\t#plan(\n\t\trequest: Message,\n\t\tscan: LedgerScan,\n\t): Pick<Selection, 'messages'> & { readonly draft: LedgerDraft } {\n\t\tconst { input, projection } = this.#project(scan)\n\t\tconst registry = scan.registry\n\t\tconst links = linkOwners(input.findings, registry.owners)\n\t\tconst near = new Set([\n\t\t\t...matchEntities(scan.index, request.content, true),\n\t\t\t...this.#classifier.topics(request.id),\n\t\t])\n\t\tconst owners = [...near].flatMap((id) => {\n\t\t\tconst owner = registry.owners.has(id) ? id : links.get(id)\n\t\t\treturn owner === undefined ? [] : [owner]\n\t\t})\n\t\tconst records = selectRecords(projection, { owners, topics: [...near] })\n\t\tconst total =\n\t\t\tMath.max(\n\t\t\t\t0,\n\t\t\t\t(this.#options.capacity - this.#predict) * this.#share.prompt -\n\t\t\t\t\t(this.#gauge?.overhead ?? 0),\n\t\t\t) /\n\t\t\t(1 + LEDGER_SCALE_DRIFT)\n\t\tconst built = this.#agent.context.build({ messages: [request], judgments: [] })\n\t\tconst tail = this.#selectTail(request, total, input, scan)\n\t\tconst tailIds = new Set(tail.map((message) => message.id))\n\t\tconst scoped = records.some((record) => record.key !== LEDGER_RULES_KEY)\n\t\tconst held = new Set([\n\t\t\t...projection.records\n\t\t\t\t.filter((record) => scoped || record.key === LEDGER_RULES_KEY)\n\t\t\t\t.flatMap((record) => record.members),\n\t\t\t...(records.length > 0\n\t\t\t\t? projection.records.flatMap((record) =>\n\t\t\t\t\t\trecord.members.filter(\n\t\t\t\t\t\t\t(id) =>\n\t\t\t\t\t\t\t\tthis.#conversation.message(id)?.role === 'user' &&\n\t\t\t\t\t\t\t\tthis.#classifier.category(id) === 'rule' &&\n\t\t\t\t\t\t\t\tthis.#classifier.decisive(id),\n\t\t\t\t\t\t),\n\t\t\t\t\t)\n\t\t\t\t: []),\n\t\t])\n\t\tconst texts = input.messages\n\t\t\t.filter((message) => !hasCalls(message))\n\t\t\t.map((message) => ({\n\t\t\t\tmessage,\n\t\t\t\t...extractWords(message),\n\t\t\t}))\n\t\tconst asked = texts.find(({ message }) => message.id === request.id)\n\t\tconst counts = new Map(\n\t\t\t[\n\t\t\t\t...Map.groupBy(\n\t\t\t\t\ttexts.flatMap((text) => [...text.words]),\n\t\t\t\t\t(word) => word,\n\t\t\t\t),\n\t\t\t].map(([word, occurrences]) => [word, occurrences.length]),\n\t\t)\n\t\tconst live = collectProjectionIds(projection)\n\t\tconst units = texts.flatMap(({ message, words, names }, position) => {\n\t\t\tif (\n\t\t\t\t!live.has(message.id) ||\n\t\t\t\theld.has(message.id) ||\n\t\t\t\t(message.role !== 'tool' && tailIds.has(message.id))\n\t\t\t)\n\t\t\t\treturn []\n\t\t\tconst literals = extractLiterals(message.content)\n\t\t\tif (message.role === 'user' && literals.ids.size + literals.numbers.size + names.size === 0)\n\t\t\t\treturn []\n\t\t\tconst category = this.#classifier.category(message.id)\n\t\t\tconst unsettled = message.role === 'user' && !this.#classifier.decisive(message.id)\n\t\t\tconst topics = new Set([\n\t\t\t\t...(input.entities.get(message.id) ?? []),\n\t\t\t\t...this.#classifier.topics(message.id),\n\t\t\t])\n\t\t\tconst ruled = this.#ruled(message.id, message.role)\n\t\t\tconst group = [...topics].some((topic) => near.has(topic))\n\t\t\t\t? 1\n\t\t\t\t: ruled\n\t\t\t\t\t? 2\n\t\t\t\t\t: [...names].some((name) => asked?.names.has(name))\n\t\t\t\t\t\t? 3\n\t\t\t\t\t\t: 4\n\t\t\tif (group === 4) return []\n\t\t\tconst score = [...words]\n\t\t\t\t.filter((word) => asked?.words.has(word))\n\t\t\t\t.reduce((sum, word) => sum + Math.log(texts.length / (counts.get(word) ?? 1)), 0)\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: message.id,\n\t\t\t\t\tlines: this.#projectLines(input, message.id),\n\t\t\t\t\tposition,\n\t\t\t\t\tgroup,\n\t\t\t\t\truled,\n\t\t\t\t\tunsettled: unsettled,\n\t\t\t\t\tscore,\n\t\t\t\t\tcategory,\n\t\t\t\t\tcut: rankLedgerCut(group, unsettled, category),\n\t\t\t\t},\n\t\t\t]\n\t\t})\n\t\tconst ordered = [...units].sort(\n\t\t\t(left, right) =>\n\t\t\t\tleft.group - right.group ||\n\t\t\t\t(left.group === 1 ? Number(left.unsettled) - Number(right.unsettled) : 0) ||\n\t\t\t\t((left.group === 1 && left.unsettled) || left.group === 3 ? right.score - left.score : 0) ||\n\t\t\t\tleft.position - right.position,\n\t\t)\n\t\tconst cuts = [...ordered].sort(\n\t\t\t(left, right) =>\n\t\t\t\tleft.cut - right.cut ||\n\t\t\t\t(left.cut === 0 || left.cut === 1 || left.cut === 4 ? left.score - right.score : 0) ||\n\t\t\t\tright.position - left.position,\n\t\t)\n\t\tconst rules = records.find((record) => record.key === LEDGER_RULES_KEY)\n\t\tconst owned = records.filter((record) => record.key !== LEDGER_RULES_KEY)\n\t\tconst ruledSources = new Set(\n\t\t\t[...new Map(owned.flatMap((record) => record.lines).map((line) => [line.source, line.role]))]\n\t\t\t\t.filter(([source, role]) => this.#ruled(source, role))\n\t\t\t\t.map(([source]) => source),\n\t\t)\n\t\tconst steps = [\n\t\t\t...(rules?.lines.filter((line) => !line.topics.some((topic) => near.has(topic))).reverse() ??\n\t\t\t\t[]),\n\t\t\t...cuts.map((unit) => unit.source),\n\t\t\t...(rules?.lines.filter((line) => line.topics.some((topic) => near.has(topic))).reverse() ??\n\t\t\t\t[]),\n\t\t\t...owned\n\t\t\t\t.toReversed()\n\t\t\t\t.flatMap((record) => [\n\t\t\t\t\t...record.lines.filter((line) => !ruledSources.has(line.source)),\n\t\t\t\t\t...record.lines.filter((line) => ruledSources.has(line.source)),\n\t\t\t\t]),\n\t\t]\n\t\tconst mapped = ordered.map((unit) => ({\n\t\t\tsource: unit.source,\n\t\t\trecord: this.#buildUnitRecord(unit.source, unit.category, input, projection, held),\n\t\t}))\n\t\tconst trimmed = this.#trim(\n\t\t\tsteps,\n\t\t\trecords,\n\t\t\tmapped,\n\t\t\tnew Set(ordered.map((unit) => unit.source)),\n\t\t\tbuilt,\n\t\t\ttail,\n\t\t\ttotal,\n\t\t\tprojection.amendments,\n\t\t\tscan,\n\t\t)\n\t\tconst replies = collectLedgerReplies(scan.messages, this.#requests, this.#annotations)\n\t\tconst sources = new Set(\n\t\t\tthis.#window.flatMap((entry) => [\n\t\t\t\tentry.request.id,\n\t\t\t\tentry.reply.id,\n\t\t\t\t...entry.lines.map((line) => line.source),\n\t\t\t]),\n\t\t)\n\t\tconst evidence = collectLedgerEvidence(scan.messages, sources, projection.amendments)\n\t\tconst prefix = this.#window.flatMap((entry) => entry.lines)\n\t\tconst prior = buildLedgerRenderContext(scan.messages, prefix, projection.amendments)\n\t\tconst combined = buildLedgerRenderContext(\n\t\t\tscan.messages,\n\t\t\t[...prefix, ...trimmed.rendered.lines],\n\t\t\tprojection.amendments,\n\t\t)\n\t\tconst stale =\n\t\t\tevidence.messages.size !== this.#evidence.messages.size ||\n\t\t\t[...evidence.messages].some((receipt) => !this.#evidence.messages.has(receipt)) ||\n\t\t\tevidence.amendments.size !== this.#evidence.amendments.size ||\n\t\t\t[...evidence.amendments.keys()].some((receipt) => !this.#evidence.amendments.has(receipt)) ||\n\t\t\tthis.#window.some(\n\t\t\t\t(entry) =>\n\t\t\t\t\treplies.get(entry.request.id)?.id !== entry.reply.id ||\n\t\t\t\t\tentry.lines.some((line) => !live.has(line.source)),\n\t\t\t) ||\n\t\t\tprojection.amendments.some(\n\t\t\t\t(edge) =>\n\t\t\t\t\t(prior.first.has(edge.earlier) &&\n\t\t\t\t\t\tprior.complete.has(edge.later) !== combined.complete.has(edge.later)) ||\n\t\t\t\t\t(prior.first.has(edge.later) &&\n\t\t\t\t\t\tcombined.complete.has(edge.later) &&\n\t\t\t\t\t\t!prior.first.has(edge.earlier) &&\n\t\t\t\t\t\tcombined.first.has(edge.earlier)),\n\t\t\t)\n\t\tconst last = stale ? this.#window.at(-1) : undefined\n\t\tconst retained =\n\t\t\tlast === undefined || replies.get(last.request.id)?.id !== last.reply.id\n\t\t\t\t? undefined\n\t\t\t\t: this.#conversation.message(last.request.id)\n\t\tif (stale) this.#window = []\n\t\tconst [previous, reply] = this.#window.length === 0 ? this.#selectExchange(request, input) : []\n\t\tconst window: readonly LedgerExchange[] =\n\t\t\tthis.#window.length > 0\n\t\t\t\t? this.#window\n\t\t\t\t: previous !== undefined && reply !== undefined\n\t\t\t\t\t? [{ request: previous, reply, lines: [] }]\n\t\t\t\t\t: retained !== undefined && last !== undefined\n\t\t\t\t\t\t? [{ request: retained, reply: last.reply, lines: [] }]\n\t\t\t\t\t\t: []\n\t\tif (this.#window.length === 0) {\n\t\t\tconst derived = collectLedgerEvidence(\n\t\t\t\tscan.messages,\n\t\t\t\tnew Set(window.flatMap((entry) => [entry.request.id, entry.reply.id])),\n\t\t\t\tprojection.amendments,\n\t\t\t)\n\t\t\tthis.#window = window\n\t\t\tthis.#evidence = derived\n\t\t}\n\t\tconst carried = window.flatMap((entry) => entry.lines)\n\t\tconst delta = this.#render(\n\t\t\ttrimmed.records,\n\t\t\tmapped,\n\t\t\ttrimmed.included,\n\t\t\tprojection.amendments,\n\t\t\tscan,\n\t\t\tnew Set(carried.map(buildLedgerLineKey)),\n\t\t\tcarried,\n\t\t)\n\t\tconst newest = window.at(-1)\n\t\tconst plain = newest === undefined ? undefined : this.#conversation.message(newest.request.id)\n\t\tconst fallback: readonly LedgerExchange[] =\n\t\t\tnewest === undefined || plain === undefined\n\t\t\t\t? []\n\t\t\t\t: [{ request: plain, reply: newest.reply, lines: [] }]\n\t\tconst empty = { window: [], rendered: trimmed.rendered }\n\t\tconst rungs = [\n\t\t\t{ window, rendered: delta },\n\t\t\t{ window: fallback, rendered: trimmed.rendered },\n\t\t\tempty,\n\t\t]\n\t\tconst probes = rungs.map((rung) => [\n\t\t\t...built.slice(0, -1),\n\t\t\t...stripThinking(\n\t\t\t\t[\n\t\t\t\t\t...tail.slice(0, -1),\n\t\t\t\t\t...rung.window.flatMap((entry) => [entry.request, entry.reply]),\n\t\t\t\t\tbuildRequestMessage(built.at(-1) ?? request, rung.rendered.text),\n\t\t\t\t],\n\t\t\t\tthis.#replay,\n\t\t\t),\n\t\t])\n\t\tconst names = this.#agent.context.scope?.tools\n\t\tconst definitions = this.#agent.context.tools.definitions()\n\t\tconst offered = rungs.map((rung) =>\n\t\t\thasHiddenRecall(\n\t\t\t\tinput,\n\t\t\t\trequest.id,\n\t\t\t\tthis.#requests,\n\t\t\t\tthis.#annotations,\n\t\t\t\t[...tail.slice(0, -1), ...rung.window.flatMap((entry) => [entry.request, entry.reply])],\n\t\t\t\t[...rung.window.flatMap((entry) => entry.lines), ...rung.rendered.lines],\n\t\t\t),\n\t\t)\n\t\tconst longest = this.#findLongest()\n\t\tconst closed = probes.map((messages, at) => {\n\t\t\tconst tools = definitions.filter(\n\t\t\t\t(tool) =>\n\t\t\t\t\t(names === undefined || names.includes(tool.name)) &&\n\t\t\t\t\t(tool.name !== 'recall' || offered[at]),\n\t\t\t)\n\t\t\tconst calls = [\n\t\t\t\t{ estimate: estimateMessages(messages), tools: tools.length, spared: this.#spared(tools) },\n\t\t\t]\n\t\t\treturn (\n\t\t\t\toffered[at] === true &&\n\t\t\t\tthis.#gauge !== undefined &&\n\t\t\t\tthis.#gauge.remainder(calls) - this.#predict < 2 * this.#gauge.reserve(calls, longest)\n\t\t\t)\n\t\t})\n\t\t// Admission follows trimming: the exchange can spend spare room, never a record or a seed message.\n\t\tconst prices = probes.map((messages) => this.#price(messages))\n\t\tconst admitted =\n\t\t\trungs.find(\n\t\t\t\t(_rung, at) =>\n\t\t\t\t\t(prices[at] ?? Infinity) <= total && (closed[2] === true || closed[at] === false),\n\t\t\t) ?? empty\n\t\tif (\n\t\t\toffered[rungs.indexOf(admitted)] === false &&\n\t\t\t(names === undefined || names.includes('recall'))\n\t\t) {\n\t\t\tconst tools = definitions.filter((tool) => tool.name !== 'recall').map((tool) => tool.name)\n\t\t\tconst scope = this.#agent.context.scope\n\t\t\tconst narrowed = scope?.narrow({ tools }) ?? createScope({ name: 'ledger', tools })\n\t\t\tthis.#current()?.apply(narrowed)\n\t\t\tthis.#agent.context.apply(narrowed)\n\t\t}\n\t\tconst copy = buildRequestMessage(request, admitted.rendered.text)\n\t\tconst kept = admitted.window.flatMap((entry) => entry.lines)\n\t\tconst origins = new Set([\n\t\t\trequest.id,\n\t\t\t...admitted.window.flatMap((entry) => [entry.request.id, entry.reply.id]),\n\t\t\t...kept.map((line) => line.source),\n\t\t\t...admitted.rendered.lines.map((line) => line.source),\n\t\t])\n\t\tconst draft: LedgerDraft = {\n\t\t\tcopy,\n\t\t\tlines: admitted.rendered.lines,\n\t\t\tevidence: collectLedgerEvidence(scan.messages, origins, projection.amendments),\n\t\t\twindow: admitted.window,\n\t\t}\n\t\tconst shown = buildLedgerRenderContext(\n\t\t\tscan.messages,\n\t\t\t[...kept, ...admitted.rendered.lines],\n\t\t\t[],\n\t\t).complete\n\t\treturn {\n\t\t\tdraft,\n\t\t\tmessages: [\n\t\t\t\t...tail\n\t\t\t\t\t.slice(0, -1)\n\t\t\t\t\t.map((message) =>\n\t\t\t\t\t\tmessage.role === 'tool' ? this.#renderTailStub(message, scan, shown) : message,\n\t\t\t\t\t),\n\t\t\t\t...admitted.window.flatMap((entry) => [entry.request, entry.reply]),\n\t\t\t\tcopy,\n\t\t\t],\n\t\t}\n\t}\n\n\t// Searches the cuts in step order (each step drops the line it is, or the source it names) for one\n\t// whose built head, tail, and request copy fit `total`, and returns the winning probe's render; the\n\t// cut after every step is returned unchecked. A cut past the first fit fits too while the price does\n\t// not rise along the steps, so the search lands on the first fitting cut.\n\t#trim(\n\t\tsteps: ReadonlyArray<LedgerLine | string>,\n\t\trecords: readonly LedgerRecord[],\n\t\tmapped: ReadonlyArray<{ readonly source: string; readonly record: LedgerRecord }>,\n\t\tincluded: ReadonlySet<string>,\n\t\tbuilt: readonly Message[],\n\t\ttail: readonly Message[],\n\t\ttotal: number,\n\t\tamendments: readonly LedgerAmendment[],\n\t\tscan: LedgerScan,\n\t): {\n\t\treadonly rendered: LedgerRenderResult\n\t\treadonly records: readonly LedgerRecord[]\n\t\treadonly included: ReadonlySet<string>\n\t} {\n\t\tconst head = built.slice(0, -1)\n\t\tconst priced = built.at(-1)\n\t\tif (priced === undefined) throw new Error('a built plan holds its request')\n\t\tconst found = findLedgerCut(0, steps.length, (count) => {\n\t\t\tconst cut = buildLedgerCut(steps.slice(0, count), records, included)\n\t\t\tconst rendered = this.#render(cut.records, mapped, cut.included, amendments, scan)\n\t\t\tconst copy = buildRequestMessage(priced, rendered.text)\n\t\t\treturn this.#price([...head, ...stripThinking([...tail.slice(0, -1), copy], this.#replay)]) <=\n\t\t\t\ttotal\n\t\t\t\t? { ...cut, rendered }\n\t\t\t\t: undefined\n\t\t})\n\t\tif (found !== undefined) return found\n\t\tconst last = buildLedgerCut(steps, records, included)\n\t\treturn {\n\t\t\t...last,\n\t\t\trendered: this.#render(last.records, mapped, last.included, amendments, scan),\n\t\t}\n\t}\n\n\t#buildUnitRecord(\n\t\tsource: string,\n\t\tcategory: LedgerCategory | undefined,\n\t\tinput: LedgerProjectionInput,\n\t\tprojection: LedgerProjection,\n\t\theld: ReadonlySet<string>,\n\t): LedgerRecord {\n\t\treturn {\n\t\t\tkey: category === 'rule' ? LEDGER_RULES_KEY : source,\n\t\t\ttitle: '',\n\t\t\tmembers: [source],\n\t\t\tlines: this.#collectAmended(\n\t\t\t\tinput,\n\t\t\t\tprojection,\n\t\t\t\tsource,\n\t\t\t\tcategory === 'rule'\n\t\t\t\t\t? new Set(projection.records.flatMap((record) => record.members))\n\t\t\t\t\t: held,\n\t\t\t),\n\t\t}\n\t}\n\n\t#collectAmended(\n\t\tinput: LedgerProjectionInput,\n\t\tprojection: LedgerProjection,\n\t\tsource: string,\n\t\theld: ReadonlySet<string>,\n\t): readonly LedgerLine[] {\n\t\tconst live = collectProjectionIds(projection)\n\t\treturn scanAmendments(\n\t\t\tinput.classification.amendments,\n\t\t\tsource,\n\t\t\t(id) => id === source || (!held.has(id) && live.has(id)),\n\t\t).flatMap((id) => this.#projectLines(input, id))\n\t}\n\n\t#render(\n\t\trecords: readonly LedgerRecord[],\n\t\tmapped: ReadonlyArray<{ readonly source: string; readonly record: LedgerRecord }>,\n\t\tincluded: ReadonlySet<string>,\n\t\tamendments: readonly LedgerAmendment[],\n\t\tscan: LedgerScan,\n\t\theld: ReadonlySet<string> = new Set(),\n\t\tprefix: readonly LedgerOccurrence[] = [],\n\t): LedgerRenderResult {\n\t\tconst population = records.map((record) => ({\n\t\t\t...record,\n\t\t\tlines: collectLedgerLines(record, held),\n\t\t}))\n\t\tconst entries = mapped\n\t\t\t.filter((unit) => included.has(unit.source))\n\t\t\t.flatMap(({ record: unit }) =>\n\t\t\t\t[...Map.groupBy(collectLedgerLines(unit, new Set()), (line) => line.source)].map(\n\t\t\t\t\t([source, lines]) => ({\n\t\t\t\t\t\tunit,\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\tlines,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t)\n\t\t// A source renders once, under the first unit that carries it.\n\t\tconst first = new Map(entries.toReversed().map((entry) => [entry.source, entry]))\n\t\tconst unique = entries\n\t\t\t.filter((entry) => first.get(entry.source) === entry)\n\t\t\t.map((entry) => ({\n\t\t\t\t...entry,\n\t\t\t\tlines: entry.lines.filter(\n\t\t\t\t\t(line) =>\n\t\t\t\t\t\t!prefix.some(\n\t\t\t\t\t\t\t(prior) => prior.source === line.source && prior.sentence === line.sentence,\n\t\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t}))\n\t\t\t.filter((entry) => entry.lines.length > 0)\n\t\tconst record = population.find(\n\t\t\t(candidate) => candidate.key === LEDGER_RULES_KEY && candidate.lines.length > 0,\n\t\t)\n\t\tconst ownerRecords = population.filter(\n\t\t\t(candidate) => candidate.key !== LEDGER_RULES_KEY && candidate.lines.length > 0,\n\t\t)\n\t\t// The context reads the lines in the order the records emit them, so each source's first\n\t\t// retained line carries its annotation and its reference.\n\t\tconst lines = [\n\t\t\t...ownerRecords.flatMap((candidate) => candidate.lines),\n\t\t\t...unique\n\t\t\t\t.filter((entry) => entry.unit.key !== LEDGER_RULES_KEY)\n\t\t\t\t.flatMap((entry) => entry.lines),\n\t\t\t...(record?.lines ?? []),\n\t\t\t...unique\n\t\t\t\t.filter((entry) => entry.unit.key === LEDGER_RULES_KEY)\n\t\t\t\t.flatMap((entry) => entry.lines),\n\t\t]\n\t\tconst context = buildLedgerRenderContext(scan.messages, [...prefix, ...lines], amendments)\n\t\tconst owners = ownerRecords.map((owner) => renderLedgerPinned(owner, context)).join('\\n\\n')\n\t\tconst rendered = unique.map(({ unit, source, lines: retained }) => {\n\t\t\tconst sentences = splitSentences(this.#conversation.message(source)?.content ?? '')\n\t\t\treturn {\n\t\t\t\trules: unit.key === LEDGER_RULES_KEY,\n\t\t\t\ttexts:\n\t\t\t\t\tunit.key === LEDGER_RULES_KEY && source === unit.members[0]\n\t\t\t\t\t\t? retained.map((line) => this.#renderSource(source, sentences, [line], context, scan))\n\t\t\t\t\t\t: [this.#renderSource(source, sentences, retained, context, scan)],\n\t\t\t}\n\t\t})\n\t\tconst pinned = rendered.filter((one) => !one.rules).flatMap((one) => one.texts)\n\t\tconst rules = rendered.filter((one) => one.rules).flatMap((one) => one.texts)\n\t\tconst body = [owners, pinned.join('\\n')].filter((text) => text !== '').join('\\n\\n')\n\t\tconst text = [\n\t\t\tbody === '' ? '' : `## Records\\n${body}`,\n\t\t\trecord === undefined\n\t\t\t\t? rules.length === 0\n\t\t\t\t\t? ''\n\t\t\t\t\t: ['## Rules', ...rules].join('\\n')\n\t\t\t\t: [renderLedgerRecord(record, context), ...rules].join('\\n'),\n\t\t]\n\t\t\t.filter((part) => part !== '')\n\t\t\t.join('\\n\\n')\n\t\treturn { text, lines }\n\t}\n\n\t#selectTail(\n\t\trequest: Message,\n\t\ttotal: number,\n\t\tinput: LedgerProjectionInput,\n\t\tscan: LedgerScan,\n\t): readonly Message[] {\n\t\tconst cap = total * this.#share.tail\n\t\tconst first = input.messages.findIndex((message) => this.#requests.has(message.id))\n\t\tconst seed = input.messages.slice(\n\t\t\t0,\n\t\t\tfirst < 0 ? input.messages.findIndex((message) => message.id === request.id) : first,\n\t\t)\n\t\tconst groups = collectToolGroups(seed).flatMap((group) => {\n\t\t\tconst leader = group[0]\n\t\t\tif (leader === undefined) return []\n\t\t\tconst kept = (leader.calls ?? []).flatMap((call) => {\n\t\t\t\tconst message = group.slice(1).find((result) => findLedgerCall(group, result) === call)\n\t\t\t\tconst result = message === undefined ? undefined : this.#results.get(message.id)\n\t\t\t\treturn message === undefined ||\n\t\t\t\t\t!this.#options.lookups?.some((lookup) => lookup.tool.name === call.name) ||\n\t\t\t\t\t(result?.success === false && result.error === LEDGER_NOTES.repeat)\n\t\t\t\t\t? []\n\t\t\t\t\t: [{ call, message }]\n\t\t\t})\n\t\t\treturn [{ leader, kept }]\n\t\t})\n\t\tconst calls = new Map<string, readonly ToolCall[]>(\n\t\t\tgroups.map(({ leader, kept }) => [leader.id, kept.map(({ call }) => call)]),\n\t\t)\n\t\tconst results = new Set(groups.flatMap(({ kept }) => kept.map(({ message }) => message.id)))\n\t\tconst history = seed\n\t\t\t.filter(\n\t\t\t\t(message) =>\n\t\t\t\t\t!this.#annotations.has(message.id) &&\n\t\t\t\t\t(message.role !== 'tool' || results.has(message.id)) &&\n\t\t\t\t\t!(\n\t\t\t\t\t\tmessage.role === 'assistant' &&\n\t\t\t\t\t\thasCalls(message) &&\n\t\t\t\t\t\t(calls.get(message.id)?.length ?? 0) === 0 &&\n\t\t\t\t\t\tmessage.content === ''\n\t\t\t\t\t),\n\t\t\t)\n\t\t\t.map((message) =>\n\t\t\t\tmessage.role === 'tool'\n\t\t\t\t\t? this.#renderTailStub(message, scan)\n\t\t\t\t\t: calls.has(message.id)\n\t\t\t\t\t\t? (calls.get(message.id)?.length ?? 0) > 0\n\t\t\t\t\t\t\t? { ...message, calls: calls.get(message.id) ?? [] }\n\t\t\t\t\t\t\t: { id: message.id, role: message.role, content: message.content }\n\t\t\t\t\t\t: message,\n\t\t\t)\n\t\t// Newest exchange first: the tail keeps every exchange before the first whose addition passes\n\t\t// `cap`, and each candidate is the kept exchanges in conversation order, then the request.\n\t\tconst exchanges = collectExchanges(history).toReversed()\n\t\tconst stop = exchanges.findIndex(\n\t\t\t(_exchange, index) =>\n\t\t\t\tthis.#price(\n\t\t\t\t\tstripThinking(\n\t\t\t\t\t\t[\n\t\t\t\t\t\t\t...exchanges\n\t\t\t\t\t\t\t\t.slice(0, index + 1)\n\t\t\t\t\t\t\t\t.toReversed()\n\t\t\t\t\t\t\t\t.flat(),\n\t\t\t\t\t\t\trequest,\n\t\t\t\t\t\t],\n\t\t\t\t\t\tthis.#replay,\n\t\t\t\t\t),\n\t\t\t\t) > cap,\n\t\t)\n\t\treturn [\n\t\t\t...exchanges\n\t\t\t\t.slice(0, stop === -1 ? exchanges.length : stop)\n\t\t\t\t.toReversed()\n\t\t\t\t.flat(),\n\t\t\trequest,\n\t\t]\n\t}\n\n\t#selectExchange(request: Message, input: LedgerProjectionInput): readonly Message[] {\n\t\tconst before = input.messages.slice(\n\t\t\t0,\n\t\t\tinput.messages.findIndex((message) => message.id === request.id),\n\t\t)\n\t\tconst previous = before.findLast((message) => this.#requests.has(message.id))\n\t\tconst reply =\n\t\t\tprevious === undefined\n\t\t\t\t? undefined\n\t\t\t\t: collectLedgerReplies(before, this.#requests, this.#annotations).get(previous.id)\n\t\treturn previous !== undefined && reply !== undefined ? [previous, reply] : []\n\t}\n\n\t#commitExchange(request: LedgerRequest): void {\n\t\tconst draft = request.sent\n\t\tconst message = request.message\n\t\tif (message === undefined) return\n\t\tconst reply = collectLedgerReplies(\n\t\t\tthis.#conversation.messages(),\n\t\t\tthis.#requests,\n\t\t\tthis.#annotations,\n\t\t).get(message.id)\n\t\tif (reply === undefined) return\n\t\tthis.#window = [\n\t\t\t...(draft?.window ?? this.#window),\n\t\t\t{ request: draft?.copy ?? message, reply, lines: draft?.lines ?? [] },\n\t\t]\n\t\tconst evidence = draft?.evidence ?? this.#evidence\n\t\tthis.#evidence = {\n\t\t\tmessages: new Set([\n\t\t\t\t...evidence.messages,\n\t\t\t\t...collectLedgerEvidence([message, reply], new Set([message.id, reply.id]), []).messages,\n\t\t\t]),\n\t\t\tamendments: evidence.amendments,\n\t\t}\n\t}\n\n\t#renderTailStub(message: Message, scan: LedgerScan, shown?: ReadonlySet<string>): Message {\n\t\tconst group = scan.group(message.id)\n\t\tconst call = group === undefined ? undefined : findLedgerCall(group, message)\n\t\tconst finding = scan.finding(message.id)\n\t\tconst hidden = renderStub(call?.name ?? 'tool', call?.arguments ?? {}, 'hidden')\n\t\tconst visible = renderStub(call?.name ?? 'tool', call?.arguments ?? {}, 'shown')\n\t\treturn {\n\t\t\t...message,\n\t\t\tcontent: renderStub(\n\t\t\t\tcall?.name ?? 'tool',\n\t\t\t\tcall?.arguments ?? {},\n\t\t\t\tthis.#results.get(message.id)?.success === false\n\t\t\t\t\t? 'failed'\n\t\t\t\t\t: finding?.result === undefined\n\t\t\t\t\t\t? 'empty'\n\t\t\t\t\t\t: shown === undefined\n\t\t\t\t\t\t\t? hidden.length > visible.length\n\t\t\t\t\t\t\t\t? 'hidden'\n\t\t\t\t\t\t\t\t: 'shown'\n\t\t\t\t\t\t\t: scan.findings.some((other) => shown.has(other.id) && matchesLookup(finding, other))\n\t\t\t\t\t\t\t\t? 'shown'\n\t\t\t\t\t\t\t\t: 'hidden',\n\t\t\t),\n\t\t}\n\t}\n\n\t#observeTurn(): void {\n\t\tthis.#flush()\n\t\tconst request = this.#current()\n\t\tconst boundary = request?.boundary\n\t\tif (request === undefined || boundary === undefined) return\n\t\tconst additions = this.#conversation.messages().slice(boundary)\n\t\tconst messages = this.#agent.context.build(request.selection)\n\t\tconst names = this.#agent.context.scope?.tools\n\t\tconst tools = this.#agent.context.tools\n\t\t\t.definitions()\n\t\t\t.filter((tool) => names === undefined || names.includes(tool.name))\n\t\trequest.observe(\n\t\t\t{\n\t\t\t\testimate: estimateMessages(stripThinking([...messages, ...additions], this.#replay)),\n\t\t\t\ttools: tools.length,\n\t\t\t\tspared: this.#spared(tools),\n\t\t\t},\n\t\t\tthis.#conversation.messages().length,\n\t\t)\n\t}\n\n\t#spared(tools: readonly ToolDefinition[]): number {\n\t\tconst overhead = this.#gauge?.overhead ?? 0\n\t\tif (tools.length === 0) return overhead\n\t\tconst missing = this.#agent.context.tools\n\t\t\t.definitions()\n\t\t\t.filter((tool) => !tools.some((one) => one.name === tool.name))\n\t\treturn missing.length === 0\n\t\t\t? 0\n\t\t\t: Math.min(overhead, estimateTokens(JSON.stringify(missing)) * (this.#gauge?.scale ?? 1))\n\t}\n\n\t#findLongest(): string {\n\t\treturn this.#conversation\n\t\t\t.messages()\n\t\t\t.filter((message) => message.role === 'assistant')\n\t\t\t.reduce(\n\t\t\t\t(longest, message) => (message.content.length > longest.length ? message.content : longest),\n\t\t\t\t'',\n\t\t\t)\n\t}\n\n\t#lookup(name: string, args: Readonly<Record<string, unknown>>, context: ToolContext): unknown {\n\t\tthis.#current()?.admit(name, args)\n\t\tconst lookup = this.#options.lookups?.find((one) => one.tool.name === name)\n\t\tif (lookup === undefined) throw new Error(`unknown lookup ${name}`)\n\t\treturn lookup.tool.execute(args, context)\n\t}\n\n\t#recall(args: Readonly<Record<string, unknown>>): string {\n\t\tconst request = this.#current()\n\t\trequest?.think(this.#conversation.messages())\n\t\tconst topic = isString(args.topic) ? args.topic.trim() : ''\n\t\trequest?.admit('recall', { topic })\n\t\tconst gauge = this.#gauge\n\t\tconst calls = request?.calls ?? []\n\t\tif (\n\t\t\tgauge !== undefined &&\n\t\t\tcalls.length > 0 &&\n\t\t\tgauge.remainder(calls) - this.#predict < 2 * gauge.reserve(calls, this.#findLongest())\n\t\t)\n\t\t\trequest?.close()\n\t\trequest?.recall()\n\t\tconst guidance = `an owner name, an id, or one of ${renderTopicNames(this.#options.topics)}`\n\t\tif (topic === '') throw new Error(`recall needs a topic: ${guidance}`)\n\t\tconst room =\n\t\t\t(gauge?.room(calls, this.#findLongest()) ?? 0) - estimateMessages([buildRecallMessage(topic)])\n\t\tconst scan = this.#scan(this.#conversation.messages())\n\t\t// A recall while a selection holds its scan, which only another conversation's run can make,\n\t\t// holds no scan of its own, so the classifier's handlers read the selection's.\n\t\tconst added = this.#scans.size === 0\n\t\tif (added) this.#hold(scan)\n\t\ttry {\n\t\t\tconst { input, projection } = this.#project(scan)\n\t\t\tconst registry = scan.registry\n\t\t\tconst searches = splitTopic(topic).map((part) => {\n\t\t\t\tconst words = splitWords(part)\n\t\t\t\tconst matched = new Set([\n\t\t\t\t\t...[...registry.ids].filter(\n\t\t\t\t\t\t(id) =>\n\t\t\t\t\t\t\textractLiterals(part).ids.has(id) ||\n\t\t\t\t\t\t\twords.every((word) =>\n\t\t\t\t\t\t\t\t`${id} ${(registry.owners.get(id) ?? []).join(' ')}`.toLowerCase().includes(word),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t\t...this.#options.topics\n\t\t\t\t\t\t.filter((one) => words.every((word) => one.name.toLowerCase().includes(word)))\n\t\t\t\t\t\t.map((one) => one.name),\n\t\t\t\t])\n\t\t\t\treturn { words, matched }\n\t\t\t})\n\t\t\tconst start = request?.message === undefined ? -1 : (scan.position(request.message.id) ?? -1)\n\t\t\tconst listable = new Map(\n\t\t\t\tinput.messages\n\t\t\t\t\t.filter(\n\t\t\t\t\t\t(message, at) =>\n\t\t\t\t\t\t\t!this.#requests.has(message.id) && (at < start || message.role === 'tool'),\n\t\t\t\t\t)\n\t\t\t\t\t.map((message) => [message.id, message]),\n\t\t\t)\n\t\t\tconst matched = new Set(searches.flatMap((search) => [...search.matched]))\n\t\t\tconst unmatched = searches.filter(\n\t\t\t\t(search) => search.matched.size === 0 && search.words.length > 0,\n\t\t\t)\n\t\t\t// Newest first; each item's amenders are claimed by it, so an older item never lists them again.\n\t\t\tconst gathered = [...listable.values()].reverse().reduce<{\n\t\t\t\treadonly done: ReadonlySet<string>\n\t\t\t\treadonly listed: readonly string[]\n\t\t\t\treadonly recalled: ReadonlyMap<string, string>\n\t\t\t}>(\n\t\t\t\t(state, message) => {\n\t\t\t\t\tconst finding = scan.finding(message.id)\n\t\t\t\t\tconst topics = new Set([\n\t\t\t\t\t\t...(input.entities.get(message.id) ?? []),\n\t\t\t\t\t\t...this.#classifier.topics(message.id),\n\t\t\t\t\t\t...projection.records\n\t\t\t\t\t\t\t.filter(\n\t\t\t\t\t\t\t\t(record) =>\n\t\t\t\t\t\t\t\t\trecord.key.startsWith(LEDGER_OWNER_PREFIX) && record.members.includes(message.id),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.map((record) => record.key.slice(LEDGER_OWNER_PREFIX.length)),\n\t\t\t\t\t])\n\t\t\t\t\tconst onTopic =\n\t\t\t\t\t\t(message.role === 'tool'\n\t\t\t\t\t\t\t? finding?.result !== undefined\n\t\t\t\t\t\t\t: !hasCalls(message) && !this.#classifier.quiet(message.id)) &&\n\t\t\t\t\t\t[...matched].some((match) => topics.has(match))\n\t\t\t\t\tconst worded =\n\t\t\t\t\t\t(message.role === 'tool' ? finding !== undefined : message.role === 'user') &&\n\t\t\t\t\t\tunmatched.some((search) =>\n\t\t\t\t\t\t\tsearch.words.every((word) => message.content.toLowerCase().includes(word)),\n\t\t\t\t\t\t)\n\t\t\t\t\tif (!onTopic && !worded) return state\n\t\t\t\t\tconst items = scanAmendments(\n\t\t\t\t\t\tinput.classification.amendments,\n\t\t\t\t\t\tmessage.id,\n\t\t\t\t\t\t(candidate) => listable.has(candidate) && !state.done.has(candidate),\n\t\t\t\t\t).flatMap((id) => {\n\t\t\t\t\t\tconst source = listable.get(id)\n\t\t\t\t\t\tif (source === undefined) return []\n\t\t\t\t\t\tconst lookup = scan.finding(id)\n\t\t\t\t\t\tconst content = source.content.replace(\n\t\t\t\t\t\t\t/(^|[\\n\\v\\f\\r\\u0085\\u2028\\u2029])(?=Earlier (?:request|reply):)/g,\n\t\t\t\t\t\t\t'$1 ',\n\t\t\t\t\t\t)\n\t\t\t\t\t\tconst text =\n\t\t\t\t\t\t\tlookup === undefined\n\t\t\t\t\t\t\t\t? content\n\t\t\t\t\t\t\t\t: `${renderLookupCall(lookup.name, lookup.arguments)}: ${content}`\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\t\ttext,\n\t\t\t\t\t\t\t\tstored:\n\t\t\t\t\t\t\t\t\tlookup === undefined\n\t\t\t\t\t\t\t\t\t\t? []\n\t\t\t\t\t\t\t\t\t\t: [\n\t\t\t\t\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t\t\t\t\tsplitResultLines(text)[0] ?? '',\n\t\t\t\t\t\t\t\t\t\t\t\t\tsplitResultLines(content)[0] ?? '',\n\t\t\t\t\t\t\t\t\t\t\t\t] as const,\n\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t]\n\t\t\t\t\t})\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdone: new Set([...state.done, ...items.map((item) => item.id)]),\n\t\t\t\t\t\tlisted:\n\t\t\t\t\t\t\titems.length === 0\n\t\t\t\t\t\t\t\t? state.listed\n\t\t\t\t\t\t\t\t: [...state.listed, items.map((item) => item.text).join('\\n')],\n\t\t\t\t\t\trecalled: new Map([...state.recalled, ...items.flatMap((item) => item.stored)]),\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t{ done: new Set(), listed: [], recalled: new Map() },\n\t\t\t)\n\t\t\tconst earlier = input.messages\n\t\t\t\t.slice(0, Math.max(0, start))\n\t\t\t\t.reverse()\n\t\t\t\t.flatMap((message) => {\n\t\t\t\t\tif (gathered.done.has(message.id) || this.#annotations.has(message.id)) return []\n\t\t\t\t\tconst label = this.#requests.has(message.id)\n\t\t\t\t\t\t? LEDGER_RECALL_LABELS.request\n\t\t\t\t\t\t: message.role === 'assistant' && !hasCalls(message) && message.content.trim() !== ''\n\t\t\t\t\t\t\t? LEDGER_RECALL_LABELS.reply\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\tif (\n\t\t\t\t\t\tlabel === undefined ||\n\t\t\t\t\t\t!searches.some(\n\t\t\t\t\t\t\t(search) =>\n\t\t\t\t\t\t\t\tsearch.words.length > 0 &&\n\t\t\t\t\t\t\t\tsearch.words.every((word) => message.content.toLowerCase().includes(word)),\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t\t\treturn []\n\t\t\t\t\t// Only a label opens a line here, so a stored line can't read as a reply it isn't.\n\t\t\t\t\treturn [`${label} ${message.content.replace(/[\\n\\v\\f\\r\\u0085\\u2028\\u2029]+/g, '\\n ')}`]\n\t\t\t\t})\n\t\t\tconst listed = [...gathered.listed, ...earlier]\n\t\t\t// Statements list ahead of conversation messages, so an omitted message can be newer than a kept one.\n\t\t\tconst result =\n\t\t\t\tlisted.length === 0\n\t\t\t\t\t? `nothing earlier on \"${topic}\"; answer from the messages you can see`\n\t\t\t\t\t: cutListing(listed, room, earlier.length > 0 ? 'more' : 'older')\n\t\t\trequest?.list(result, gathered.recalled)\n\t\t\treturn result\n\t\t} finally {\n\t\t\tif (added) this.#scans.delete(scan)\n\t\t}\n\t}\n\n\t#buildDigest(request: LedgerRequest, entered: Message): string | undefined {\n\t\tthis.#flush()\n\t\t// Each line once, at its first occurrence.\n\t\tconst lines = [\n\t\t\t...new Set(\n\t\t\t\tthis.#collectAfter(entered).flatMap((message) => {\n\t\t\t\t\tconst result = this.#results.get(message.id)\n\t\t\t\t\tif (message.role !== 'tool' || result?.success !== true) return []\n\t\t\t\t\tconst recalled = result.name === 'recall'\n\t\t\t\t\tif (recalled && /^nothing earlier on /.test(message.content)) return []\n\t\t\t\t\tconst stored = recalled ? request.lines(message.content) : undefined\n\t\t\t\t\treturn splitResultLines(message.content)\n\t\t\t\t\t\t.filter((line) => !recalled || !matchesCutLine(line))\n\t\t\t\t\t\t.map((line) => stored?.get(line) ?? line)\n\t\t\t\t}),\n\t\t\t),\n\t\t]\n\t\treturn lines.length === 0 ? undefined : `${LEDGER_NOTES.results}\\n${lines.join('\\n')}`\n\t}\n}\n","import type { ProviderInterface } from '../providers/types.js'\nimport type { LedgerInterface, LedgerOptions } from './types.js'\nimport { Ledger } from './Ledger.js'\n\n/**\n * Creates a conversation ledger after checking its thresholds, allocation, and tool names.\n * @param provider - The provider that answers requests\n * @param options - The judge, projection policy, capacity, and agent bounds\n * @returns The ledger and its owned conversation and agent\n * @throws {LedgerError} Thrown when an option lies outside its documented bounds\n * @example\n * ```ts\n * const ledger = createLedger(provider, options)\n * const reply = await ledger.respond('Check the order.')\n * ```\n */\nexport function createLedger(provider: ProviderInterface, options: LedgerOptions): LedgerInterface {\n\treturn new Ledger(provider, options)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAMA,IAAa,gBAAgB,OAAO,OAAO;CAAC;CAAU;CAAQ;CAAa;AAAM,CAAU;;;;;;AAO3F,IAAa,qBAAqB,OAAO,OAAO;CAAC;CAAQ;CAAa;AAAM,CAAU;;;;;;;;;;;;;ACStF,IAAa,kBAAb,cAAqC,MAAM;;CAE1C,OAAgB;CAChB;CAEA,YAAY,SAAsB,SAAwB;EACzD,MAAM,sBAAsB,OAAO;EACnC,KAAK,OAAO;EACZ,oBAAA,WAAW,OAAO,MAAM,OAAO,IAAI,kCAAkC,GAAG,EAAE,OAAO,KAAK,CAAC;EACvF,KAAK,UAAU;CAChB;AACD;;;;;;;;;;;;AAaA,SAAgB,kBAAkB,OAA0C;CAC3E,QAAA,GAAOA,oBAAAA,MAAAA,OAAY;EAClB,IACC,EACC,iBAAiB,SACjBC,oBAAAA,WAAW,UAAU,KAAK,MAAM,MAAM,aACtCA,oBAAAA,WAAW,SAAS,OAAO,OAAO,IAAI,kCAAkC,CAAC,CAAC,EAAE,UAAU,QAGvF,OAAO;EAER,IAAI,EADSA,oBAAAA,WAAW,QAAQ,KAAK,OAAO,MACtC,MAAS,UAAU,OAAO;EAChC,MAAM,UAAUA,oBAAAA,WAAW,QAAQ,KAAK,OAAO,SAAS;EACxD,IAAI,GAAA,GAAEC,oBAAAA,SAAAA,CAAS,OAAO,MAAA,GAAKC,oBAAAA,WAAAA,CAAW,OAAO,IAAI,OAAO;EACxD,MAAM,UAAUF,oBAAAA,WAAW,QAAQ,KAAK,SAAS,SAAS;EAC1D,MAAM,WAAWA,oBAAAA,W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