@ai-matrx/agents 0.40.0 → 0.41.1

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.
@@ -21,7 +21,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var context_exports = {};
22
22
  __export(context_exports, {
23
23
  CONTEXT_FETCHABLE_ROWS: () => CONTEXT_FETCHABLE_ROWS,
24
- CONTEXT_KEY_ACRONYMS: () => CONTEXT_KEY_ACRONYMS,
25
24
  CONTEXT_ROW_BLOCKS: () => CONTEXT_ROW_BLOCKS,
26
25
  CONTEXT_RULES_FEATURE: () => CONTEXT_RULES_FEATURE,
27
26
  DEFAULT_INLINE_CAP: () => DEFAULT_INLINE_CAP,
@@ -42,7 +41,6 @@ __export(context_exports, {
42
41
  contextValueChars: () => contextValueChars,
43
42
  deliveredFields: () => deliveredFields,
44
43
  formatChars: () => formatChars,
45
- humanizeContextKey: () => humanizeContextKey,
46
44
  isRichEnvelope: () => isRichEnvelope,
47
45
  isServerAuthoritativeKey: () => isServerAuthoritativeKey,
48
46
  isServerResolved: () => isServerResolved,
@@ -61,7 +59,6 @@ __export(context_exports, {
61
59
  withheldKeys: () => withheldKeys
62
60
  });
63
61
  module.exports = __toCommonJS(context_exports);
64
- var import_text_case = require("@ai-matrx/kit/text-case");
65
62
 
66
63
  // context/page-context.ts
67
64
  var PAGE_OFF_WITHHELD = [
@@ -525,8 +522,6 @@ function attachmentContextKey(provider, resourceRef) {
525
522
  }
526
523
  return out;
527
524
  }
528
- var humanizeContextKey = import_text_case.humanizeIdentifier;
529
- var CONTEXT_KEY_ACRONYMS = import_text_case.IDENTIFIER_ACRONYMS;
530
525
  function formatChars(n) {
531
526
  if (n === null || n === void 0 || !Number.isFinite(n)) return "\u2014";
532
527
  const abs = Math.abs(n);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../context/index.ts","../../context/page-context.ts"],"sourcesContent":["/**\n * @ai-matrx/agents/context — the context value rules, headless.\n *\n * Contract: common-docs/systems/scopes-context/context-delivery/RULES.md.\n * Parity corpus: ./rules-corpus.json (run here by rules.test.ts and by aidream's\n * pytest against the Python twin `resolve_context_rule`). Every function here\n * is pure, zero-React, and safe to import on a server.\n *\n * The one flow a client runs per turn:\n * rows = values.map(v => resolveContextRow(v, savedRows, cap)) // what the table shows\n * context = buildContextWire(rows) // what the request sends\n * …on `context_receipt`: compareReceipt(rows, receipt) // screen vs server\n */\n\nimport { IDENTIFIER_ACRONYMS, humanizeIdentifier } from \"@ai-matrx/kit/text-case\";\n\n// ── Constants ───────────────────────────────────────────────────────────────\n\n/** `users.user_surface_state.feature` for the person's saved rules. */\nexport const CONTEXT_RULES_FEATURE = \"context_rules\";\n/** Surface key for values no surface emitted (attachments, server-added values). */\nexport const DEFAULT_SURFACE_KEY = \"_default\";\n/** The server's default inline threshold when no layer sets one. */\nexport const DEFAULT_INLINE_MAX_CHARS = 200;\n/** The default ceiling every resolved limit is clamped to (a server knob). */\nexport const DEFAULT_INLINE_CAP = 50000;\n/** A lazy document's server default (RULES.md §2: \"a small lazy document 6000\"). */\nexport const LAZY_SOURCE_DEFAULT_MAX = 6000;\n\n// ── Types ───────────────────────────────────────────────────────────────────\n\n/** Which layer decided a field — lowest to highest. */\nexport type ContextLayer = \"default\" | \"page\" | \"agent\" | \"you\";\nexport type ContextDelivery = \"inline\" | \"on_request\" | \"off\";\n/**\n * A client row's delivery: the server's three, plus `\"server\"` — an included\n * value the server resolves itself (`isServerResolved`), whose delivery the\n * client does not claim. Never on the wire or in a receipt.\n */\nexport type ContextRowDelivery = ContextDelivery | \"server\";\n\n/** One saved per-person rule (one key on one surface). Both fields optional. */\nexport interface SavedContextRule {\n include?: boolean;\n max_inline_chars?: number;\n}\n\n/** `{ [surface_key]: { [context key]: SavedContextRule } }` — the person's rows. */\nexport type SavedContextRuleRows = Record<string, Record<string, SavedContextRule>>;\n\n/** The agent layer for one key (RULES.md §2). */\nexport interface ContextAgentLayer {\n /** The agent's Context Policy declares this key (or the value is the agent's own seed). */\n declared?: boolean;\n /** The policy's ceiling; only read when `declared`. */\n max_inline_chars?: number | null;\n /** `auto_context_disabled` — closes every undeclared key. */\n kill_switch?: boolean;\n}\n\n/** The page layer for one key: the surface that emitted the value. */\nexport interface ContextSurfaceLayer {\n /** The surface declares this value (a `ui_surface_value` row / manifest entry). */\n declared?: boolean;\n /** `false` drops the value unless the agent declares it. */\n auto_context?: boolean | null;\n /** `inlineUpTo` / envelope `max_inline_chars` / DB column. */\n max_inline_chars?: number | null;\n}\n\n/** The non-person layers for one value — what the table's layer breakdown shows. */\nexport interface ContextRuleLayers {\n /** The server's per-key default limit; `null`/absent = 200. */\n default_max?: number | null;\n agent?: ContextAgentLayer | null;\n surface?: ContextSurfaceLayer | null;\n}\n\n/**\n * Input to `resolveContextRule`. Snake case on purpose: it is the corpus shape\n * and the Python twin's shape. `user` is typed loosely because invalid saved\n * values (non-boolean include, non-integer or negative limit) must be ignored,\n * not trusted.\n */\nexport interface ContextRuleInput extends ContextRuleLayers {\n /** Size in code points; `null` = an unresolved lazy source. */\n chars: number | null;\n user?: { include?: unknown; max_inline_chars?: unknown } | null;\n /** Ceiling; default `DEFAULT_INLINE_CAP`. */\n cap?: number;\n /**\n * A server-authoritative value (`SERVER_AUTHORITATIVE_KEYS`, RULES.md §5a):\n * identity is not page or ambient context, so neither the page's switch nor\n * the agent's or mandate's kill switch closes it — only the person's own rule.\n */\n identity?: boolean;\n}\n\nexport interface ContextRuleResult {\n include: boolean;\n /** Effective limit, after the clamp. */\n max_inline_chars: number;\n delivery: ContextDelivery;\n decided_by: { include: ContextLayer; max_inline_chars: ContextLayer };\n clamped: boolean;\n}\n\n/** One receipt row — RULES.md §5, wire fields exactly. */\nexport interface ContextReceiptRow {\n key: string;\n label: string;\n surface_key: string;\n origin: \"client\" | \"server\" | \"surroundings\" | \"rule\";\n chars: number | null;\n include: boolean;\n max_inline_chars: number;\n delivery: ContextDelivery;\n decided_by: { include: ContextLayer; max_inline_chars: ContextLayer };\n user_rule: { include?: boolean; max_inline_chars?: number } | null;\n clamped: boolean;\n /** The CLIENT itself sent a value the person turned off. */\n client_sent_excluded: boolean;\n /**\n * `\"model\"`: nothing reached a model that reads no context. `\"self_check\"`:\n * the server's post-render safety check stripped it (RULES.md §4/§5) —\n * the row stays, `delivery: \"off\"`.\n */\n blocked_by: ContextBlockedBy | null;\n /**\n * Set when the server did not keep the value under its own key — still a\n * row, never missing (RULES.md §5): `\"expanded\"` (a surroundings envelope\n * became one row per part), `\"directive\"` (a reserved directive the server\n * acted on), `\"renamed\"` (an earlier key spelling, held under the stable\n * key), `\"unaccounted\"` (a server path removed it with no reason — filed as\n * a defect). Absent from an older server.\n */\n consumed_as?: ContextConsumedAs | null;\n /** The keys (each with its own row) that now carry what this value held. */\n consumed_into?: string[];\n /**\n * What the model READ for this value, verbatim from the rendered context\n * block (its own element: the whole value inline, or its on-request entry;\n * for a server-authoritative value, the server's own statement of it).\n * Absent/null when it reached no block (off, consumed) or from an older server.\n */\n delivered?: ContextDeliveredRef | null;\n /** On-request values the server holds as text: what the `context` tool returns (size + hash). */\n on_request?: ContextDeliveredRef | null;\n}\n\n/**\n * What the model read for one value or block, by SIZE and HASH only — never\n * the text (RULES.md §5b: a server-shaped value is fetched on demand, never\n * pushed). The text comes from the viewer (`ContextViewLoader`).\n */\nexport interface ContextDeliveredRef {\n /** Code-point length of the whole text the model read. */\n chars: number;\n /** SHA-256 hex of that text (UTF-8). */\n sha256: string;\n}\n\n/**\n * What to view: a value's `delivered` element, its `on_request` text, a\n * `block`, or what the agent can `fetchable` for a value through a tool (the\n * Organization: its selected scopes' values via `scope_system` — rendered when\n * opened, never on the receipt; `source: \"fetched_now\"`).\n */\nexport type ContextViewKind = \"delivered\" | \"on_request\" | \"block\" | \"fetchable\";\n\nexport interface ContextViewTarget {\n kind: ContextViewKind;\n /** The value's key, or the block's id. */\n key: string;\n}\n\n/**\n * The exact text the model received (a sent turn: `GET /ai/context/delivered`)\n * or will receive (the next turn: `POST /ai/context/preview` with `view`).\n */\nexport interface ContextViewedText {\n kind: ContextViewKind;\n key: string;\n text: string;\n chars: number;\n sha256: string;\n /**\n * `fetched_now`: read at request time — a `fetchable` text (what the agent\n * gets if it asks now) or a lazy `on_request` value the agent never read.\n * `tool_result`: exactly what the `context` tool returned to the agent.\n */\n source: \"preview\" | \"wire\" | \"conversation_prompt\" | \"turn_record\" | \"fetched_now\" | \"tool_result\";\n /**\n * `fetchable` only: `false` when the turn's agent cannot call the tool that\n * returns it (tools off, excluded, a structured-output turn). `text` is then\n * `\"\"`; the screen says so with state, never shows content as fetchable.\n */\n available?: boolean;\n /**\n * A lazy `on_request` value of a SENT turn only (a document, a file family,\n * a scope — the turn never reads one): `true` — the agent called `context`\n * for it and `text` is what the tool returned; `false` — the agent never\n * read it and `text` is what it returns now (the screen says so).\n */\n fetched?: boolean | null;\n}\n\n/** The host's door to the viewer API — called only when a person opens a value. */\nexport type ContextViewLoader = (target: ContextViewTarget) => Promise<ContextViewedText>;\n\n/**\n * The blocks a value's detail shows beneath its own text, because the model\n * received them as part of that value: the Organization is its live statement\n * AND the organization catalog (owner ruling 2026-10-01). Such a block is\n * counted in the value's chars and is not listed again under \"Also sent\".\n */\nexport const CONTEXT_ROW_BLOCKS: Readonly<Record<string, readonly string[]>> = {\n organization: [\"organization_catalog\"],\n};\n\n/**\n * Values whose content the agent READS THROUGH A TOOL rather than receiving it,\n * keyed to that tool: the Organization's selected scopes' values are returned\n * by `scope_system` (by design, never inline). Their detail shows \"Agent can\n * fetch\" — the viewer's `fetchable` text, loaded when opened (RULES.md §5b).\n */\nexport const CONTEXT_FETCHABLE_ROWS: Readonly<Record<string, string>> = {\n organization: \"scope_system\",\n};\n\n/** The receipt blocks shown under `key`'s detail (`CONTEXT_ROW_BLOCKS`). */\nexport function blocksForRow(\n key: string,\n blocks: readonly ContextReceiptBlock[] | null | undefined,\n): ContextReceiptBlock[] {\n const ids = CONTEXT_ROW_BLOCKS[key] ?? [];\n return (blocks ?? []).filter((b) => ids.includes(b.id));\n}\n\n/** The receipt blocks not shown under any value present in `rowKeys`. */\nexport function unclaimedBlocks(\n blocks: readonly ContextReceiptBlock[] | null | undefined,\n rowKeys: Iterable<string>,\n): ContextReceiptBlock[] {\n const claimed = new Set<string>();\n for (const key of rowKeys) for (const id of CONTEXT_ROW_BLOCKS[key] ?? []) claimed.add(id);\n return (blocks ?? []).filter((b) => !claimed.has(b.id));\n}\n\n/** How the server consumed a value it did not keep under its own key (receipt `consumed_as`). */\nexport type ContextConsumedAs = \"expanded\" | \"directive\" | \"renamed\" | \"unaccounted\";\n\n/**\n * The reserved keys the server never keeps under their own name, and what it\n * does with each — the receipt rows say so (`consumed_as`). The same table is\n * the parity corpus's `consumed_key_cases` (aidream `RESERVED_CONSUMED_KEYS`).\n */\nexport const RESERVED_CONSUMED_KEYS: Readonly<Record<string, ContextConsumedAs>> = {\n surface_chain: \"expanded\",\n window_forms: \"expanded\",\n __table_access: \"directive\",\n __google_files: \"directive\",\n};\n\n/**\n * Where the server states each SERVER-AUTHORITATIVE value (RULES.md §5a, owner\n * ruling 2026-10-01: identity is the server's truth, never the page's claim).\n * The server drops the client's copy of these keys and the model reads the\n * server's own value, every turn: `\"active_context\"` — its `<active_context>`\n * block (the conversation's organization, the scopes it admitted);\n * `\"manifest\"` — a server-built value (the authenticated person, the server's\n * clock). A screen never shows the client's copy of one as what will be sent.\n * The same table is the parity corpus's `server_authoritative_keys` (aidream\n * `SERVER_AUTHORITATIVE_KEYS`).\n */\nexport const SERVER_AUTHORITATIVE_KEYS: Readonly<Record<string, \"manifest\" | \"active_context\">> = {\n user: \"manifest\",\n client: \"manifest\",\n organization: \"active_context\",\n active_scopes: \"active_context\",\n};\n\n/** True for a key whose value the server states itself (`SERVER_AUTHORITATIVE_KEYS`). */\nexport function isServerAuthoritativeKey(key: string): boolean {\n return Object.prototype.hasOwnProperty.call(SERVER_AUTHORITATIVE_KEYS, key);\n}\n\n/**\n * One block of server text the model received this turn that is not a single\n * value — the turn's `<active_context>`, earlier-turn context, the sandbox\n * briefing, skills, memory, the organization catalog… (receipt `blocks`,\n * RULES.md §5). `delivered` is its exact text, cut at the receipt's cap.\n */\nexport interface ContextReceiptBlock {\n /** Stable id: the per-turn slot it rode in, or the system-prompt addition's name. */\n id: string;\n /** The id in words (`humanizeContextKey`). */\n label: string;\n /** Size and hash; the text comes from the viewer (`kind: \"block\"`). */\n delivered: ContextDeliveredRef;\n}\n\n/** The per-turn receipt the server streams as `context_receipt` — RULES.md §5. */\nexport interface ContextReceipt {\n version: 1;\n surface: string | null;\n cap: number;\n /** False: this turn's model cannot read context at all (every row `blocked_by: \"model\"`). */\n model_reads_context: boolean;\n /** The person's saved rules could not be read; the turn ran without them. */\n rules_error: string | null;\n rows: ContextReceiptRow[];\n /** Every other block of server text the model received, in render order (absent: older server). */\n blocks?: ContextReceiptBlock[];\n}\n\nexport type ContextRowOrigin = \"page\" | \"attached\" | \"system\";\n\n/** Why the server kept a value from the model on this turn (receipt `blocked_by`). */\nexport type ContextBlockedBy = \"model\" | \"self_check\";\n\n/** A client's resolved row: what its table renders and what `buildContextWire` sends. */\nexport interface ResolvedContextRow extends Omit<ContextRuleResult, \"delivery\"> {\n delivery: ContextRowDelivery;\n /**\n * The server resolves this value (a resource reference, a `*_id` UUID): size\n * and delivery are unknown (`chars: null`, `delivery: \"server\"` when\n * included), and its default limit is not predicted. Include and the\n * person's own rule still resolve normally.\n */\n serverResolved?: boolean;\n /** Display-only: chars / limit / delivery were filled from a receipt (`applyReceiptToRows`). */\n fromReceipt?: boolean;\n /** From a receipt: why the server kept this value from the model this turn. */\n blockedBy?: ContextBlockedBy;\n /**\n * Display-only, from a receipt: what the model read for this value. When set,\n * a detail view shows THIS — never `value`, the client's pre-send copy.\n */\n delivered?: ContextDeliveredRef;\n /** Display-only, from a receipt: what the `context` tool returns for it (size + hash). */\n onRequest?: ContextDeliveredRef;\n /** Display-only, from a receipt: blocks the model received as part of this value. */\n deliveredBlocks?: ContextReceiptBlock[];\n key: string;\n label: string;\n surfaceKey: string;\n origin: ContextRowOrigin;\n /** The raw value, or an already-rich envelope (`{ content, type, … }`). */\n value: unknown;\n chars: number | null;\n /** The saved rule that was applied (merged `_default` + surface row), or null. */\n userRule: SavedContextRule | null;\n description?: string;\n /** Context object type for the wire envelope (`text`, `json`, …). */\n type?: string;\n /** The non-person layers the rule was resolved from. Absent = default only. */\n layers?: ContextRuleLayers;\n}\n\n/** What a client knows about one value before resolution. */\nexport interface ContextRowSource {\n key: string;\n label: string;\n surfaceKey?: string;\n origin: ContextRowOrigin;\n value: unknown;\n /** Override the measured size (e.g. `null` for an unresolved lazy source). */\n chars?: number | null;\n description?: string;\n type?: string;\n layers?: ContextRuleLayers;\n}\n\n// ── Helpers ─────────────────────────────────────────────────────────────────\n\nconst isNum = (v: unknown): v is number => typeof v === \"number\" && Number.isFinite(v);\nconst isValidLimit = (v: unknown): v is number =>\n typeof v === \"number\" && Number.isInteger(v) && v >= 0;\n\n// ── resolveContextRule — RULES.md §2, line for line ─────────────────────────\n\nexport function resolveContextRule(input: ContextRuleInput): ContextRuleResult {\n const agent = input.agent ?? null;\n const surface = input.surface ?? null;\n const user = input.user ?? null;\n const cap = isNum(input.cap) ? input.cap : DEFAULT_INLINE_CAP;\n const agentDeclared = agent?.declared === true;\n\n const identity = input.identity === true;\n\n let include = true;\n let includeBy: ContextLayer = \"default\";\n if (!identity && surface?.declared === true && surface.auto_context === false) {\n if (agentDeclared) {\n include = true;\n includeBy = \"agent\";\n } else {\n include = false;\n includeBy = \"page\";\n }\n }\n if (!identity && agent?.kill_switch === true && !agentDeclared) {\n include = false;\n includeBy = \"agent\";\n }\n if (user && typeof user.include === \"boolean\") {\n include = user.include;\n includeBy = \"you\";\n }\n\n let limit = isNum(input.default_max) ? input.default_max : DEFAULT_INLINE_MAX_CHARS;\n let limitBy: ContextLayer = \"default\";\n const a = agentDeclared && isNum(agent?.max_inline_chars) ? agent.max_inline_chars : null;\n const s = isNum(surface?.max_inline_chars) ? surface.max_inline_chars : null;\n if (a !== null && s !== null) {\n if (s < a) {\n limit = s;\n limitBy = \"page\";\n } else {\n limit = a;\n limitBy = \"agent\";\n }\n } else if (a !== null) {\n limit = a;\n limitBy = \"agent\";\n } else if (s !== null) {\n limit = s;\n limitBy = \"page\";\n }\n if (user && isValidLimit(user.max_inline_chars)) {\n limit = user.max_inline_chars;\n limitBy = \"you\";\n }\n const clamped = limit > cap;\n limit = Math.min(limit, cap);\n\n const chars = input.chars;\n const delivery: ContextDelivery = !include\n ? \"off\"\n : chars === null || chars === undefined || limit === 0 || chars > limit\n ? \"on_request\"\n : \"inline\";\n\n return {\n include,\n max_inline_chars: limit,\n delivery,\n decided_by: { include: includeBy, max_inline_chars: limitBy },\n clamped,\n };\n}\n\n// ── contextValueChars — RULES.md §1 ─────────────────────────────────────────\n\n/** Count Unicode code points (never UTF-16 units: `\"😀\".length` is 2). */\nfunction codePoints(s: string): number {\n let n = 0;\n for (const _ of s) n += 1;\n return n;\n}\n\n/**\n * Size of a value in code points, exactly as the server's `content_as_str()`:\n * a string is itself; anything else is `JSON.stringify(value, null, 2)`\n * (Python `json.dumps(value, ensure_ascii=False, indent=2)`). `undefined` → 0.\n */\nexport function contextValueChars(value: unknown): number {\n if (value === undefined) return 0;\n if (typeof value === \"string\") return codePoints(value);\n let text: string | undefined;\n try {\n text = JSON.stringify(value, null, 2);\n } catch {\n text = String(value);\n }\n return text === undefined ? 0 : codePoints(text);\n}\n\n// ── lookupSavedRule — RULES.md §3 ───────────────────────────────────────────\n\nfunction sanitizeRule(raw: unknown): SavedContextRule {\n const out: SavedContextRule = {};\n if (!raw || typeof raw !== \"object\") return out;\n const r = raw as Record<string, unknown>;\n if (typeof r.include === \"boolean\") out.include = r.include;\n if (isValidLimit(r.max_inline_chars)) out.max_inline_chars = r.max_inline_chars;\n return out;\n}\n\n/**\n * The person's rule for one key on one surface: `_default[key]` merged per\n * field under `rows[surfaceKey][key]`, invalid fields stripped. `null` when\n * nothing valid is saved.\n */\nexport function lookupSavedRule(\n rows: SavedContextRuleRows | null | undefined,\n surfaceKey: string,\n key: string,\n): SavedContextRule | null {\n if (!rows || typeof rows !== \"object\") return null;\n const base = sanitizeRule(rows[DEFAULT_SURFACE_KEY]?.[key]);\n const own = surfaceKey === DEFAULT_SURFACE_KEY ? {} : sanitizeRule(rows[surfaceKey]?.[key]);\n const merged: SavedContextRule = { ...base, ...own };\n return Object.keys(merged).length === 0 ? null : merged;\n}\n\n/**\n * The person's rule for one key, drift-proof (identical to aidream's\n * `lookup_user_rule_for`). While a primary surface P is in play, every key\n * filed under `_default` OR under P resolves to the same per-field merge\n * `{ ..._default[key], ...rows[P][key] }` (P wins) — whichever surface the\n * client's or the server's manifest says \"declares\" the key. A code manifest\n * and the DB manifest can disagree (sync drift); this makes the person's rule\n * land the same on both sides anyway. A surroundings key (any other surface)\n * keeps its own surface: `{ ..._default[key], ...rows[surfaceKey][key] }`.\n * With no primary surface this is exactly `lookupSavedRule`.\n *\n * Saving is unchanged: page values save to P, everything else to `_default`.\n */\nexport function lookupSavedRuleFor(\n rows: SavedContextRuleRows | null | undefined,\n primarySurface: string | null | undefined,\n surfaceKey: string,\n key: string,\n): SavedContextRule | null {\n const primary = primarySurface && primarySurface !== DEFAULT_SURFACE_KEY ? primarySurface : null;\n if (primary && (surfaceKey === DEFAULT_SURFACE_KEY || surfaceKey === primary)) {\n return lookupSavedRule(rows, primary, key);\n }\n return lookupSavedRule(rows, surfaceKey, key);\n}\n\n// ── serverDefaultMax — the server's per-key default, read from the wire value ─\n\n/**\n * The keys that make a dict the rich envelope form — exactly aidream\n * `context_objects._RICH_FORM_KEYS`. A dict with ANY other key is raw content\n * on the server, `content` key or not.\n */\nexport const RICH_ENVELOPE_KEYS: ReadonlySet<string> = new Set([\n \"content\",\n \"mutable\",\n \"persist\",\n \"source\",\n \"type\",\n \"label\",\n \"description\",\n \"max_inline_chars\",\n \"default_max_inline_chars\",\n \"summary_agent_id\",\n]);\n\n/**\n * A UUID as the server's `_uuid` (Python `UUID(value.strip())`) accepts it:\n * hyphens optional, `{…}` braces and `urn:uuid:` prefixes allowed.\n */\nfunction isUuidLike(value: string): boolean {\n const hex = value\n .trim()\n .replace(/urn:/gi, \"\")\n .replace(/uuid:/gi, \"\")\n .replace(/^\\{+|\\}+$/g, \"\")\n .replace(/-/g, \"\");\n return /^[0-9a-f]{32}$/i.test(hex);\n}\n\nfunction isPlainObject(v: unknown): v is Record<string, unknown> {\n return !!v && typeof v === \"object\" && !Array.isArray(v);\n}\n\n/** A canonical resource reference the server promotes (aidream `parse_resource_reference`). */\nfunction isResourceRef(v: Record<string, unknown>): boolean {\n if (v.__kind !== \"resource_ref\") return false;\n const type = v.resource_type || v.type;\n const id = v.resource_id || v.id;\n return (\n typeof type === \"string\" &&\n type.trim() !== \"\" &&\n typeof id === \"string\" &&\n isUuidLike(id)\n );\n}\n\nfunction hasOwn(o: Record<string, unknown>, k: string): boolean {\n return Object.prototype.hasOwnProperty.call(o, k);\n}\n\nfunction onlyRichKeys(o: Record<string, unknown>): boolean {\n return Object.keys(o).every((k) => RICH_ENVELOPE_KEYS.has(k));\n}\n\nfunction hasLazySource(o: Record<string, unknown>): boolean {\n return isPlainObject(o.source) && typeof o.source.kind === \"string\";\n}\n\n/**\n * A rich envelope exactly as the server reads one (aidream\n * `_split_rich_context_value`): a plain object with its own `content` whose\n * every key is in `RICH_ENVELOPE_KEYS`. A record that merely HAS `content`\n * (a note `{ id, title, content }`) is raw content, not an envelope.\n */\nexport function isRichEnvelope(value: unknown): value is Record<string, unknown> {\n return isPlainObject(value) && hasOwn(value, \"content\") && onlyRichKeys(value);\n}\n\n/** The source-only lazy pointer form: rich keys only, no `content`, a `source` with a `kind`. */\nfunction isLazyPointer(value: unknown): value is Record<string, unknown> {\n return isPlainObject(value) && !hasOwn(value, \"content\") && onlyRichKeys(value) && hasLazySource(value);\n}\n\n/**\n * Split one wire value into `{ content, overrides }` exactly as aidream\n * `_split_rich_context_value`: a rich envelope or lazy pointer → its `content`\n * (null when absent) and every other key; anything else → the whole value as\n * content, no overrides.\n */\nexport function splitContextValue(value: unknown): {\n content: unknown;\n overrides: Record<string, unknown>;\n} {\n if (isRichEnvelope(value) || isLazyPointer(value)) {\n const { content = null, ...overrides } = value;\n return { content, overrides };\n }\n return { content: value, overrides: {} };\n}\n\n/**\n * The server's default inline limit for one wire value — the `default` layer\n * the server will use, before page/agent/you. Mirrors aidream\n * `context_rules._server_default_max` over the object `ContextManifest.build`\n * makes from the value (after `normalize_resource_context`):\n *\n * - a rich envelope (every key in the rich-form set, and `content` present or a\n * lazy `source` with a string `kind`) carrying a whole-number, non-negative\n * `default_max_inline_chars` → that;\n * - a top-level resource reference (`__kind: \"resource_ref\"` with a type and a\n * UUID id), or a lazy source whose `extra.resource_type` is set → 0\n * (descriptor-first);\n * - a lazy source (a `source` and null/absent `content`) → 6000;\n * - anything else → null (the server's 200).\n *\n * A resource reference nested as an envelope's `content` is NOT promoted by the\n * server (it reads top-level values only), so it is plain JSON content → null.\n */\nexport function serverDefaultMax(value: unknown): number | null {\n if (!isPlainObject(value)) return null;\n if (isResourceRef(value)) return 0;\n if (!isRichEnvelope(value) && !isLazyPointer(value)) return null;\n if (isValidLimit(value.default_max_inline_chars)) return value.default_max_inline_chars;\n if (hasLazySource(value) && (value.content === null || value.content === undefined)) {\n const src = value.source as Record<string, unknown>;\n const extra = isPlainObject(src.extra) ? src.extra : {};\n return extra.resource_type ? 0 : LAZY_SOURCE_DEFAULT_MAX;\n }\n return null;\n}\n\n// ── resolveContextRow — the glue a client runs per value ────────────────────\n\n/** Resolve one value into the row the table renders and the wire sends. */\nexport function resolveContextRow(\n source: ContextRowSource,\n savedRows: SavedContextRuleRows | null | undefined,\n cap: number = DEFAULT_INLINE_CAP,\n /** The page in play; makes the rule lookup drift-proof (`lookupSavedRuleFor`). */\n primarySurface?: string | null,\n): ResolvedContextRow {\n const surfaceKey = source.surfaceKey ?? DEFAULT_SURFACE_KEY;\n const serverResolved = isServerResolved(source.key, source.value);\n const chars = serverResolved\n ? null\n : source.chars !== undefined\n ? source.chars\n : unresolvedSize(source.value)\n ? null\n : contextValueChars(splitContextValue(source.value).content);\n const userRule = lookupSavedRuleFor(savedRows, primarySurface, surfaceKey, source.key);\n const layers: ContextRuleLayers = { ...(source.layers ?? {}) };\n if (!serverResolved && (layers.default_max === undefined || layers.default_max === null)) {\n const serverDefault = serverDefaultMax(source.value);\n if (serverDefault !== null) layers.default_max = serverDefault;\n }\n // Identity is decided as the server decides its own statement of it (RULES.md §5a): only the\n // person's rule closes it — never the page switch or the agent's / mandate's kill switch.\n const identity = isServerAuthoritativeKey(source.key);\n const result = resolveContextRule({ ...layers, chars, user: userRule, cap, identity });\n const row: ResolvedContextRow = {\n ...result,\n delivery: serverResolved && result.include ? \"server\" : result.delivery,\n key: source.key,\n label: source.label,\n surfaceKey,\n origin: source.origin,\n value: source.value,\n chars,\n userRule,\n };\n if (source.description !== undefined) row.description = source.description;\n if (source.type !== undefined) row.type = source.type;\n if (source.layers !== undefined || layers.default_max !== undefined) row.layers = layers;\n if (serverResolved) row.serverResolved = true;\n return row;\n}\n\n// ── buildContextWire — the ONLY producer of a request's `context` ──────────\n\n/**\n * Size the server cannot know yet: a lazy pointer (or an envelope with a lazy\n * source and null content), or a resource reference it will promote to one.\n */\nfunction unresolvedSize(value: unknown): boolean {\n if (!isPlainObject(value)) return false;\n if (isResourceRef(value)) return true;\n return (\n (isRichEnvelope(value) || isLazyPointer(value)) &&\n hasLazySource(value) &&\n (value.content === null || value.content === undefined)\n );\n}\n\nconst KIND_RESOURCE_REF = \"resource_ref\";\n\n/**\n * Values the server acts on by their top-level shape in `request.context`\n * (aidream `normalize_resource_context` → `parse_resource_reference`, which\n * reads ONLY top-level values):\n *\n * - a resource reference — any object whose `__kind` is `\"resource_ref\"`\n * (its own promote/exclude/overlay policy rides inside it);\n * - a bare UUID string under a conventional `*_id` key (`file_id`,\n * `attached_file_id`, `current_note_id`, …), which the server promotes when\n * the noun is a registered resource type. The registry lives on the server,\n * so every `*_id` key with a UUID passes through; for an unregistered noun\n * the server reads the bare string exactly as it would read it wrapped;\n * - a reserved key the server consumes by shape (`RESERVED_CONSUMED_KEYS`:\n * `surface_chain`, `window_forms`, `__table_access`, `__google_files`) —\n * wrapped, the server reads `{content, label}` instead of its list or grant\n * and drops it.\n *\n * These ship exactly as-is: no label, type, description or limit added.\n */\nexport function isWireDirective(key: string, value: unknown): boolean {\n if (Object.hasOwn(RESERVED_CONSUMED_KEYS, key)) return true;\n if (isPlainObject(value)) return value.__kind === KIND_RESOURCE_REF;\n return (\n typeof value === \"string\" &&\n isIdKey(key) &&\n isUuidLike(value)\n );\n}\n\n/** The server's key rule for promoting a bare id (`_resource_type_for_key`): a `*_id` key. */\nfunction isIdKey(key: string): boolean {\n return key.trim().toLowerCase().endsWith(\"_id\");\n}\n\n/**\n * A value whose size, default limit and delivery the SERVER decides by\n * resolving it — so the client must not predict them:\n *\n * - a top-level resource reference (`__kind: \"resource_ref\"`), bare or as a\n * rich envelope's `content`;\n * - a UUID string under a `*_id` key, bare or as a rich envelope's `content`.\n *\n * The server only promotes an id when its noun is in the resource registry\n * (aidream `_resource_type_for_key`), which the client cannot see, so every\n * `*_id` UUID counts. This is a deliberate superset: on a value the server\n * does not promote, the client only stops predicting — it never makes a wrong\n * promise.\n */\nexport function isServerResolved(key: string, value: unknown): boolean {\n const inner = isRichEnvelope(value) ? value.content : value;\n if (isPlainObject(inner)) return inner.__kind === KIND_RESOURCE_REF;\n return typeof inner === \"string\" && isIdKey(key) && isUuidLike(inner);\n}\n\n/**\n * Build the request's `context` object from the resolved rows. This is the\n * ONLY function that may produce it, so what the table shows and what the\n * request carries cannot drift apart.\n *\n * - A row whose `include` is false is omitted entirely — it never reaches the\n * wire (RULES.md §1: off means off everywhere).\n * - An included row becomes `{ content, type?, label, description?,\n * max_inline_chars? }`. An already-rich envelope (`isRichEnvelope`, e.g. a\n * working document carrying `source`/`mutable`/`persist`, or a lazy pointer)\n * keeps every non-rule field it already has; `label`/`description`/`type`\n * are only filled when missing.\n * - `max_inline_chars` on the envelope is the PAGE layer's value only\n * (`layers.surface.max_inline_chars`, else the envelope's own page value).\n * The server reads the person's saved rules itself every turn (RULES.md §3)\n * and the agent layer from its own policy, so the wire must never carry the\n * person's number or the resolved effective limit dressed up as a page rule\n * — that would make a stale client value outrank the server's fresh read.\n */\nexport function buildContextWire(rows: readonly ResolvedContextRow[]): Record<string, unknown> {\n const wire: Record<string, unknown> = {};\n for (const row of rows) {\n if (!row.include) continue;\n // A row with no value (a receipt-built display row) has nothing to send.\n if (row.value === undefined) continue;\n // Rich: an envelope with `content`, or a source-only lazy pointer. Every\n // non-rule field survives — including `default_max_inline_chars`, the\n // server's per-key default (not a person's or a page's number).\n // A record that merely has `content` is NOT rich: it is wrapped whole, or\n // the server would read it as raw content and drop the envelope fields.\n // A top-level directive the server acts on by SHAPE ships exactly as-is:\n // wrapped, it becomes plain JSON and silently stops resolving.\n if (isWireDirective(row.key, row.value)) {\n wire[row.key] = row.value;\n continue;\n }\n const rich = isRichEnvelope(row.value) || isLazyPointer(row.value);\n const envelope: Record<string, unknown> = rich\n ? { ...(row.value as Record<string, unknown>) }\n : { content: row.value };\n delete envelope.max_inline_chars;\n if (envelope.type === undefined && row.type !== undefined) envelope.type = row.type;\n if (envelope.label === undefined) envelope.label = row.label;\n if (envelope.description === undefined && row.description !== undefined) {\n envelope.description = row.description;\n }\n const pageLimit = row.layers?.surface?.max_inline_chars;\n const ownPageLimit = rich ? (row.value as Record<string, unknown>).max_inline_chars : undefined;\n if (isValidLimit(pageLimit)) envelope.max_inline_chars = pageLimit;\n else if (isValidLimit(ownPageLimit)) envelope.max_inline_chars = ownPageLimit;\n wire[row.key] = envelope;\n }\n return wire;\n}\n\n// ── compareReceipt — RULES.md §6 ────────────────────────────────────────────\n\nexport interface ContextReceiptMismatch {\n key: string;\n field: \"include\" | \"max_inline_chars\" | \"delivery\" | \"user_rule\" | \"missing\";\n expected: unknown;\n actual: unknown;\n /**\n * `\"self_check\"`: a real difference from what was shown — the server's\n * safety check stripped the value before the model call. Say \"removed by\n * the server's safety check\", not \"the screen was wrong\".\n */\n reason?: \"self_check\";\n}\n\nfunction normalizeUserRule(rule: SavedContextRule | null | undefined): SavedContextRule | null {\n const clean = sanitizeRule(rule);\n return Object.keys(clean).length === 0 ? null : clean;\n}\n\nfunction sameRule(a: SavedContextRule | null, b: SavedContextRule | null): boolean {\n if (a === null || b === null) return a === b;\n return a.include === b.include && a.max_inline_chars === b.max_inline_chars;\n}\n\n/**\n * Compare the rows the client resolved (and rendered, and sent) with the\n * receipt the server emitted for the same turn.\n *\n * For each expected row, the receipt row is matched by key + surface key, then\n * by key alone. Compared: `include`, `max_inline_chars`, `delivery` (only when\n * both sides know the size) and `user_rule`. An expected row absent from the\n * receipt is a `missing` mismatch — except a row the client correctly withheld\n * for a reason other than the person's own rule (page `auto_context` off, the\n * agent's kill switch): the client never sent it and the server lists only\n * the person's saved `include: false` rules as `origin: \"rule\"` rows, so its\n * absence is the truth, not a lie. Receipt rows nobody expected are returned\n * as `systemRows` — shown, never hidden. A row the server consumed\n * (`consumed_as`: expanded / directive / renamed) is compared on `include`\n * and `user_rule` only — its parts carry their own rows; an `\"unaccounted\"`\n * row, or an included envelope `\"expanded\"` into no parts, is a `missing`\n * mismatch.\n */\nexport function compareReceipt(\n expected: readonly ResolvedContextRow[],\n receipt: ContextReceipt,\n): { mismatches: ContextReceiptMismatch[]; systemRows: ContextReceiptRow[]; modelReadsContext: boolean } {\n const mismatches: ContextReceiptMismatch[] = [];\n // A turn whose model reads no context delivers nothing by design: every row\n // is `delivery: \"off\"`, `blocked_by: \"model\"`. That is not a lie about the\n // person's rules, so delivery is not compared; include / limit / rule still are.\n const modelReadsContext = receipt.model_reads_context !== false;\n const used = new Set<ContextReceiptRow>();\n const rows = receipt.rows ?? [];\n\n for (const exp of expected) {\n const actual =\n rows.find((r) => !used.has(r) && r.key === exp.key && r.surface_key === exp.surfaceKey) ??\n rows.find((r) => !used.has(r) && r.key === exp.key);\n if (!actual) {\n const withheldByOthers = !exp.include && exp.decided_by.include !== \"you\";\n if (!withheldByOthers) {\n mismatches.push({ key: exp.key, field: \"missing\", expected: exp.delivery, actual: null });\n }\n continue;\n }\n used.add(actual);\n if (actual.include !== exp.include) {\n mismatches.push({ key: exp.key, field: \"include\", expected: exp.include, actual: actual.include });\n }\n // A consumed value (expanded / directive / renamed) was not delivered as\n // itself: its parts carry their own rows, limits and deliveries. Only\n // include and the person's rule are this key's own promise.\n const consumed = Boolean(actual.consumed_as);\n // A server path removed the value with no reason: kept as a row so it is\n // never silently missing, and still a broken promise on this screen.\n if (actual.consumed_as === \"unaccounted\" && exp.include) {\n mismatches.push({ key: exp.key, field: \"missing\", expected: exp.delivery, actual: \"unaccounted\" });\n }\n // An envelope the screen showed as sent that the server expanded into no\n // parts delivered nothing (a window whose fields were all unreadable):\n // \"expanded\" alone would pass it silently.\n if (\n actual.consumed_as === \"expanded\" &&\n actual.include &&\n exp.include &&\n (actual.consumed_into ?? []).length === 0\n ) {\n mismatches.push({ key: exp.key, field: \"missing\", expected: exp.delivery, actual: \"expanded_into_nothing\" });\n }\n // A server-resolved value's limit is only the client's to promise when the\n // person set one; its default-derived limit and delivery are the server's.\n const promisesLimit =\n !consumed && (!exp.serverResolved || exp.userRule?.max_inline_chars !== undefined);\n if (promisesLimit && actual.max_inline_chars !== exp.max_inline_chars) {\n mismatches.push({\n key: exp.key,\n field: \"max_inline_chars\",\n expected: exp.max_inline_chars,\n actual: actual.max_inline_chars,\n });\n }\n // The safety check stripped a value the person included: always a\n // mismatch (server-resolved or not), tagged with its reason.\n const selfChecked = actual.blocked_by === \"self_check\" && exp.include;\n if (selfChecked) {\n mismatches.push({\n key: exp.key,\n field: \"delivery\",\n expected: exp.delivery,\n actual: actual.delivery,\n reason: \"self_check\",\n });\n }\n if (\n !selfChecked &&\n !consumed &&\n modelReadsContext &&\n !exp.serverResolved &&\n exp.delivery !== \"server\" &&\n exp.chars !== null &&\n actual.chars !== null &&\n actual.delivery !== exp.delivery\n ) {\n mismatches.push({ key: exp.key, field: \"delivery\", expected: exp.delivery, actual: actual.delivery });\n }\n const expRule = normalizeUserRule(exp.userRule);\n const actRule = normalizeUserRule(actual.user_rule);\n if (!sameRule(expRule, actRule)) {\n mismatches.push({ key: exp.key, field: \"user_rule\", expected: expRule, actual: actRule });\n }\n }\n\n return { mismatches, systemRows: rows.filter((r) => !used.has(r)), modelReadsContext };\n}\n\n/**\n * For DISPLAY only: fill each server-resolved row's `chars`,\n * `max_inline_chars` and `delivery` from the latest receipt row for the same\n * key (key + surface key, then key), marking it `fromReceipt`. Other rows are\n * returned unchanged. Never compare these rows — `compareReceipt` takes the\n * rows the client resolved before sending.\n */\nexport function applyReceiptToRows(\n rows: readonly ResolvedContextRow[],\n receipt: ContextReceipt | null | undefined,\n): ResolvedContextRow[] {\n const receiptRows = receipt?.rows ?? [];\n return rows.map((row) => {\n const hit =\n receiptRows.find((r) => r.key === row.key && r.surface_key === row.surfaceKey) ??\n receiptRows.find((r) => r.key === row.key);\n if (!hit) return row;\n // What the model read is the server's to report for EVERY value — a value the\n // server replaced or reshaped must never show as the client's copy. A value the\n // server states itself (RULES.md §5a) is measured by what it stated, never by\n // the page's copy.\n const attached = blocksForRow(row.key, receipt?.blocks);\n const stated =\n hit.delivered && isServerAuthoritativeKey(row.key)\n ? hit.delivered.chars + attached.reduce((n, b) => n + b.delivered.chars, 0)\n : null;\n const shown = {\n ...row,\n ...deliveredFields(hit),\n ...(stated !== null ? { chars: stated } : {}),\n ...(attached.length ? { deliveredBlocks: attached } : {}),\n };\n if (!row.serverResolved) return shown;\n return {\n ...shown,\n chars: stated ?? hit.chars,\n max_inline_chars: hit.max_inline_chars,\n delivery: hit.delivery,\n fromReceipt: true,\n ...(hit.blocked_by ? { blockedBy: hit.blocked_by } : {}),\n };\n });\n}\n\n// ── receiptRowToResolved — read-only rows for a sent message ────────────────\n\n/**\n * Turn one receipt row into the row a read-only table renders (a sent\n * message's context, read back from `chat.message.model_context`). The value\n * itself is not in the receipt, so `value` is undefined.\n */\nexport function receiptRowToResolved(\n row: ContextReceiptRow,\n blocks?: readonly ContextReceiptBlock[] | null,\n): ResolvedContextRow {\n const attached = row.delivered ? blocksForRow(row.key, blocks) : [];\n const origin: ContextRowOrigin =\n row.surface_key !== DEFAULT_SURFACE_KEY ? \"page\" : row.origin === \"client\" ? \"attached\" : \"system\";\n return {\n key: row.key,\n label: row.label,\n surfaceKey: row.surface_key,\n origin,\n value: undefined,\n chars:\n row.delivered && isServerAuthoritativeKey(row.key)\n ? row.delivered.chars + attached.reduce((n, b) => n + b.delivered.chars, 0)\n : row.chars,\n userRule: normalizeUserRule(row.user_rule),\n include: row.include,\n max_inline_chars: row.max_inline_chars,\n delivery: row.delivery,\n decided_by: { include: row.decided_by.include, max_inline_chars: row.decided_by.max_inline_chars },\n clamped: row.clamped,\n layers: receiptLayers(row),\n ...(row.blocked_by ? { blockedBy: row.blocked_by } : {}),\n ...deliveredFields(row),\n ...(attached.length ? { deliveredBlocks: attached } : {}),\n };\n}\n\n/** A receipt row's `delivered` / `on_request` as display fields (absent when not sent). */\nexport function deliveredFields(\n row: Pick<ContextReceiptRow, \"delivered\" | \"on_request\">,\n): Pick<ResolvedContextRow, \"delivered\" | \"onRequest\"> {\n return {\n ...(row.delivered ? { delivered: row.delivered } : {}),\n ...(row.on_request ? { onRequest: row.on_request } : {}),\n };\n}\n\n/**\n * Rebuild the non-person layers from what the server applied, so a read-only\n * table shows the applied limit as the inherited value and marks the layer\n * that decided it. The limit goes on the layer named by\n * `decided_by.max_inline_chars` (`you` is carried by `userRule`); include is\n * rebuilt the same way for a page `auto_context` off or the agent's kill switch.\n */\n/**\n * Keys the client has a value for but is NOT sending this turn (excluded by\n * the page, the agent or the person's rule). Clients send this list as the\n * request's `context_withheld`, so the receipt's `origin: \"rule\"` rows are\n * limited to keys actually withheld on this page. Deduplicated, in row order.\n */\nexport function withheldKeys(rows: readonly ResolvedContextRow[]): string[] {\n const out: string[] = [];\n for (const row of rows) {\n if (row.include || row.value === undefined || out.includes(row.key)) continue;\n out.push(row.key);\n }\n return out;\n}\n\nconst DOCUMENT_KEY_PREFIXES = [\"attached_document_\", \"attached_file_\", \"resource_file_\"];\n\n/**\n * Receipt rows the client never sent (`compareReceipt(...).systemRows`:\n * server-seeded attachments, scope seeds, saved off-rules…) as table rows, so\n * a person can see and govern them — the normal Include switch and Inline max\n * apply. Documents (`attached_document_*`, `attached_file_*`,\n * `resource_file_*`) group as \"attached\", everything else as \"system\". The\n * server resolved them (`serverResolved`), values come from the receipt\n * (`fromReceipt`), and `surfaceKey` is the row's own `surface_key` — so a rule\n * saved for one writes to `_default` unless the server filed it under a page.\n * `value` is undefined: `buildContextWire` never sends these rows.\n */\nexport function systemRowsToResolved(systemRows: readonly ContextReceiptRow[]): ResolvedContextRow[] {\n return systemRows.map((receiptRow) => {\n const row = receiptRowToResolved(receiptRow);\n const isDocument = DOCUMENT_KEY_PREFIXES.some((p) => receiptRow.key.startsWith(p));\n return {\n ...row,\n origin: isDocument ? \"attached\" : \"system\",\n serverResolved: true,\n fromReceipt: true,\n };\n });\n}\n\nfunction receiptLayers(row: ContextReceiptRow): ContextRuleLayers {\n const layers: ContextRuleLayers = {};\n const limit = row.max_inline_chars;\n const limitBy = row.decided_by.max_inline_chars;\n const includeBy = row.decided_by.include;\n if (limitBy === \"default\") layers.default_max = limit;\n // Page auto_context off: the page dropped it, or the agent re-admitted it.\n const pageOff = (includeBy === \"page\" && !row.include) || (includeBy === \"agent\" && row.include);\n if (limitBy === \"page\" || pageOff) {\n layers.surface = {\n declared: true,\n auto_context: !pageOff,\n max_inline_chars: limitBy === \"page\" ? limit : null,\n };\n }\n if (limitBy === \"agent\") {\n // An agent limit means the key is declared; a declared key the agent decided to include\n // was re-admitted past a page auto_context off. (The kill switch never closes a declared key.)\n layers.agent = { declared: true, max_inline_chars: limit };\n } else if (includeBy === \"agent\") {\n // Off = the kill switch closing an undeclared key; on = a declaration re-admitting a\n // page auto_context-off value.\n layers.agent = row.include ? { declared: true } : { declared: false, kill_switch: true };\n }\n return layers;\n}\n\n// ── attachmentContextKey — the context key of a conversation attachment ─────\n\n/**\n * The context key the server files a conversation attachment under, derivable\n * by every client from `GET /conversations/{id}/attachments` (aidream\n * `conversation_attachments/briefing.py`; corpus `attachment_key_cases`):\n *\n * `attached_resource_` + encode(`${provider}:${resourceRef}`)\n *\n * where encode keeps ASCII letters, digits and `-`, and writes every other\n * UTF-8 byte as `_` + two lowercase hex digits (`_` itself is `_5f`). Unique\n * across providers, case-preserving (Drive ids), stable.\n */\nexport function attachmentContextKey(provider: string, resourceRef: string): string {\n const bytes = new TextEncoder().encode(`${provider}:${resourceRef}`);\n let out = \"attached_resource_\";\n for (const b of bytes) {\n const keep =\n (b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5a) || (b >= 0x61 && b <= 0x7a) || b === 0x2d;\n out += keep ? String.fromCharCode(b) : `_${b.toString(16).padStart(2, \"0\")}`;\n }\n return out;\n}\n\n// ── humanizeContextKey — transitional name for kit's humanizeIdentifier ─────\n\n/**\n * A context key in words. The ONE humanizer is `@ai-matrx/kit/text-case`\n * `humanizeIdentifier` (corpus `humanize_cases` in `rules-corpus.json`, Python\n * twin aidream `humanize_context_key`); this name stays only until matrx-frontend\n * imports the kit function, then it is deleted.\n */\nexport const humanizeContextKey = humanizeIdentifier;\n/** @see IDENTIFIER_ACRONYMS in `@ai-matrx/kit/text-case`. */\nexport const CONTEXT_KEY_ACRONYMS = IDENTIFIER_ACRONYMS;\n\n// ── Display ─────────────────────────────────────────────────────────────────\n\n/** Compact size for a tabular column: `950`, `18.4k`, `184k`, `1.2M`, `—` for unknown. */\nexport function formatChars(n: number | null | undefined): string {\n if (n === null || n === undefined || !Number.isFinite(n)) return \"—\";\n const abs = Math.abs(n);\n const trim = (s: string) => s.replace(/\\.0$/, \"\");\n if (abs < 1000) return String(Math.round(n));\n if (abs < 100_000) return `${trim((n / 1000).toFixed(1))}k`;\n if (abs < 1_000_000) return `${Math.round(n / 1000)}k`;\n return `${trim((n / 1_000_000).toFixed(1))}M`;\n}\n\n// RULES.md §0 — what a page's own conversation (or a switched-off page) never receives.\nexport * from \"./page-context\";\n","/**\n * What a page's OWN conversation — or a conversation whose page switch is off —\n * never receives (common-docs context-delivery RULES.md §0).\n *\n * Arman, 2026-10-01: the agent in a battle \"cannot and should not know its own\n * conversation id, route and things that identify itself. User, Client and\n * Organization are ok.\" A window or panel chat over the page is NOT the page's\n * own conversation and keeps receiving the page.\n *\n * Clients send the case as the request field `page_context`\n * (`{ mode, withheld? }`); aidream's context gate enforces it at the PAGE layer\n * (`decided_by.include === \"page\"`). The lists and every case are the parity\n * corpus's `page_context_defaults` / `page_context_cases` — aidream\n * `page_context.py` runs the same file.\n */\n\nexport type PageContextMode = \"own\" | \"off\";\n\n/** The request field `page_context`. Omitted = a conversation that follows the page. */\nexport interface PageContextDirective {\n mode: PageContextMode;\n /** The page's declared list; omitted = the default for the mode. */\n withheld?: string[];\n}\n\n/** A switched-off page: everything that IS the page — its surroundings and its route. */\nexport const PAGE_OFF_WITHHELD: readonly string[] = [\n \"route_brief\",\n \"surface_chain\",\n \"window_forms\",\n \"surface_closed\",\n];\n\n/** A page's own conversation: the page, plus what identifies the conversation itself. */\nexport const OWN_CONVERSATION_WITHHELD: readonly string[] = [...PAGE_OFF_WITHHELD, \"conversation\"];\n\n/** Values about the PERSON, never the page — never withheld, whatever a page declares. */\nexport const PERSON_CONTEXT_VALUES: ReadonlySet<string> = new Set([\n \"user\",\n \"client\",\n \"organization\",\n \"project\",\n \"task\",\n \"active_scopes\",\n]);\n\n/** What the gate knows about one value when deciding (mirrors aidream `ValueFacts`). */\nexport interface PageContextValueFacts {\n /** `\"surroundings\"` for a value from the screens around the page. */\n origin?: string;\n /** The page itself declared this value. */\n surfaceDeclared?: boolean;\n /** The surface that emitted it (`\"_default\"` when none). */\n surfaceKey?: string;\n}\n\nexport function pageContextWithheldKeys(page: PageContextDirective): ReadonlySet<string> {\n const listed = page.withheld ?? (page.mode === \"own\" ? OWN_CONVERSATION_WITHHELD : PAGE_OFF_WITHHELD);\n return new Set(listed.filter((key) => !PERSON_CONTEXT_VALUES.has(key)));\n}\n\n/**\n * True when the page rule keeps `key` from this conversation: a listed key, a\n * value from the screens around the page, or a value the page itself declared —\n * never a person value.\n */\nexport function pageContextWithholds(\n page: PageContextDirective | null | undefined,\n key: string,\n facts: PageContextValueFacts = {},\n): boolean {\n if (!page || PERSON_CONTEXT_VALUES.has(key)) return false;\n if (pageContextWithheldKeys(page).has(key)) return true;\n if (facts.origin === \"surroundings\") return true;\n return Boolean(facts.surfaceDeclared && facts.surfaceKey && facts.surfaceKey !== \"_default\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA,uBAAwD;;;ACYjD,IAAM,oBAAuC;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,4BAA+C,CAAC,GAAG,mBAAmB,cAAc;AAG1F,IAAM,wBAA6C,oBAAI,IAAI;AAAA,EAChE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAYM,SAAS,wBAAwB,MAAiD;AACvF,QAAM,SAAS,KAAK,aAAa,KAAK,SAAS,QAAQ,4BAA4B;AACnF,SAAO,IAAI,IAAI,OAAO,OAAO,CAAC,QAAQ,CAAC,sBAAsB,IAAI,GAAG,CAAC,CAAC;AACxE;A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O,OAAO,KAAK,MAAM,CAAC,CAAC;AAC3C,MAAI,MAAM,IAAS,QAAO,GAAG,MAAM,IAAI,KAAM,QAAQ,CAAC,CAAC,CAAC;AACxD,MAAI,MAAM,IAAW,QAAO,GAAG,KAAK,MAAM,IAAI,GAAI,CAAC;AACnD,SAAO,GAAG,MAAM,IAAI,KAAW,QAAQ,CAAC,CAAC,CAAC;AAC5C;","names":[]}
1
+ {"version":3,"sources":["../../context/index.ts","../../context/page-context.ts"],"sourcesContent":["/**\n * @ai-matrx/agents/context — the context value rules, headless.\n *\n * Contract: common-docs/systems/scopes-context/context-delivery/RULES.md.\n * Parity corpus: ./rules-corpus.json (run here by rules.test.ts and by aidream's\n * pytest against the Python twin `resolve_context_rule`). Every function here\n * is pure, zero-React, and safe to import on a server.\n *\n * The one flow a client runs per turn:\n * rows = values.map(v => resolveContextRow(v, savedRows, cap)) // what the table shows\n * context = buildContextWire(rows) // what the request sends\n * …on `context_receipt`: compareReceipt(rows, receipt) // screen vs server\n */\n\n// ── Constants ───────────────────────────────────────────────────────────────\n\n/** `users.user_surface_state.feature` for the person's saved rules. */\nexport const CONTEXT_RULES_FEATURE = \"context_rules\";\n/** Surface key for values no surface emitted (attachments, server-added values). */\nexport const DEFAULT_SURFACE_KEY = \"_default\";\n/** The server's default inline threshold when no layer sets one. */\nexport const DEFAULT_INLINE_MAX_CHARS = 200;\n/** The default ceiling every resolved limit is clamped to (a server knob). */\nexport const DEFAULT_INLINE_CAP = 50000;\n/** A lazy document's server default (RULES.md §2: \"a small lazy document 6000\"). */\nexport const LAZY_SOURCE_DEFAULT_MAX = 6000;\n\n// ── Types ───────────────────────────────────────────────────────────────────\n\n/** Which layer decided a field — lowest to highest. */\nexport type ContextLayer = \"default\" | \"page\" | \"agent\" | \"you\";\nexport type ContextDelivery = \"inline\" | \"on_request\" | \"off\";\n/**\n * A client row's delivery: the server's three, plus `\"server\"` — an included\n * value the server resolves itself (`isServerResolved`), whose delivery the\n * client does not claim. Never on the wire or in a receipt.\n */\nexport type ContextRowDelivery = ContextDelivery | \"server\";\n\n/** One saved per-person rule (one key on one surface). Both fields optional. */\nexport interface SavedContextRule {\n include?: boolean;\n max_inline_chars?: number;\n}\n\n/** `{ [surface_key]: { [context key]: SavedContextRule } }` — the person's rows. */\nexport type SavedContextRuleRows = Record<string, Record<string, SavedContextRule>>;\n\n/** The agent layer for one key (RULES.md §2). */\nexport interface ContextAgentLayer {\n /** The agent's Context Policy declares this key (or the value is the agent's own seed). */\n declared?: boolean;\n /** The policy's ceiling; only read when `declared`. */\n max_inline_chars?: number | null;\n /** `auto_context_disabled` — closes every undeclared key. */\n kill_switch?: boolean;\n}\n\n/** The page layer for one key: the surface that emitted the value. */\nexport interface ContextSurfaceLayer {\n /** The surface declares this value (a `ui_surface_value` row / manifest entry). */\n declared?: boolean;\n /** `false` drops the value unless the agent declares it. */\n auto_context?: boolean | null;\n /** `inlineUpTo` / envelope `max_inline_chars` / DB column. */\n max_inline_chars?: number | null;\n}\n\n/** The non-person layers for one value — what the table's layer breakdown shows. */\nexport interface ContextRuleLayers {\n /** The server's per-key default limit; `null`/absent = 200. */\n default_max?: number | null;\n agent?: ContextAgentLayer | null;\n surface?: ContextSurfaceLayer | null;\n}\n\n/**\n * Input to `resolveContextRule`. Snake case on purpose: it is the corpus shape\n * and the Python twin's shape. `user` is typed loosely because invalid saved\n * values (non-boolean include, non-integer or negative limit) must be ignored,\n * not trusted.\n */\nexport interface ContextRuleInput extends ContextRuleLayers {\n /** Size in code points; `null` = an unresolved lazy source. */\n chars: number | null;\n user?: { include?: unknown; max_inline_chars?: unknown } | null;\n /** Ceiling; default `DEFAULT_INLINE_CAP`. */\n cap?: number;\n /**\n * A server-authoritative value (`SERVER_AUTHORITATIVE_KEYS`, RULES.md §5a):\n * identity is not page or ambient context, so neither the page's switch nor\n * the agent's or mandate's kill switch closes it — only the person's own rule.\n */\n identity?: boolean;\n}\n\nexport interface ContextRuleResult {\n include: boolean;\n /** Effective limit, after the clamp. */\n max_inline_chars: number;\n delivery: ContextDelivery;\n decided_by: { include: ContextLayer; max_inline_chars: ContextLayer };\n clamped: boolean;\n}\n\n/** One receipt row — RULES.md §5, wire fields exactly. */\nexport interface ContextReceiptRow {\n key: string;\n label: string;\n surface_key: string;\n origin: \"client\" | \"server\" | \"surroundings\" | \"rule\";\n chars: number | null;\n include: boolean;\n max_inline_chars: number;\n delivery: ContextDelivery;\n decided_by: { include: ContextLayer; max_inline_chars: ContextLayer };\n user_rule: { include?: boolean; max_inline_chars?: number } | null;\n clamped: boolean;\n /** The CLIENT itself sent a value the person turned off. */\n client_sent_excluded: boolean;\n /**\n * `\"model\"`: nothing reached a model that reads no context. `\"self_check\"`:\n * the server's post-render safety check stripped it (RULES.md §4/§5) —\n * the row stays, `delivery: \"off\"`.\n */\n blocked_by: ContextBlockedBy | null;\n /**\n * Set when the server did not keep the value under its own key — still a\n * row, never missing (RULES.md §5): `\"expanded\"` (a surroundings envelope\n * became one row per part), `\"directive\"` (a reserved directive the server\n * acted on), `\"renamed\"` (an earlier key spelling, held under the stable\n * key), `\"unaccounted\"` (a server path removed it with no reason — filed as\n * a defect). Absent from an older server.\n */\n consumed_as?: ContextConsumedAs | null;\n /** The keys (each with its own row) that now carry what this value held. */\n consumed_into?: string[];\n /**\n * What the model READ for this value, verbatim from the rendered context\n * block (its own element: the whole value inline, or its on-request entry;\n * for a server-authoritative value, the server's own statement of it).\n * Absent/null when it reached no block (off, consumed) or from an older server.\n */\n delivered?: ContextDeliveredRef | null;\n /** On-request values the server holds as text: what the `context` tool returns (size + hash). */\n on_request?: ContextDeliveredRef | null;\n}\n\n/**\n * What the model read for one value or block, by SIZE and HASH only — never\n * the text (RULES.md §5b: a server-shaped value is fetched on demand, never\n * pushed). The text comes from the viewer (`ContextViewLoader`).\n */\nexport interface ContextDeliveredRef {\n /** Code-point length of the whole text the model read. */\n chars: number;\n /** SHA-256 hex of that text (UTF-8). */\n sha256: string;\n}\n\n/**\n * What to view: a value's `delivered` element, its `on_request` text, a\n * `block`, or what the agent can `fetchable` for a value through a tool (the\n * Organization: its selected scopes' values via `scope_system` — rendered when\n * opened, never on the receipt; `source: \"fetched_now\"`).\n */\nexport type ContextViewKind = \"delivered\" | \"on_request\" | \"block\" | \"fetchable\";\n\nexport interface ContextViewTarget {\n kind: ContextViewKind;\n /** The value's key, or the block's id. */\n key: string;\n}\n\n/**\n * The exact text the model received (a sent turn: `GET /ai/context/delivered`)\n * or will receive (the next turn: `POST /ai/context/preview` with `view`).\n */\nexport interface ContextViewedText {\n kind: ContextViewKind;\n key: string;\n text: string;\n chars: number;\n sha256: string;\n /**\n * `fetched_now`: read at request time — a `fetchable` text (what the agent\n * gets if it asks now) or a lazy `on_request` value the agent never read.\n * `tool_result`: exactly what the `context` tool returned to the agent.\n */\n source: \"preview\" | \"wire\" | \"conversation_prompt\" | \"turn_record\" | \"fetched_now\" | \"tool_result\";\n /**\n * `fetchable` only: `false` when the turn's agent cannot call the tool that\n * returns it (tools off, excluded, a structured-output turn). `text` is then\n * `\"\"`; the screen says so with state, never shows content as fetchable.\n */\n available?: boolean;\n /**\n * A lazy `on_request` value of a SENT turn only (a document, a file family,\n * a scope — the turn never reads one): `true` — the agent called `context`\n * for it and `text` is what the tool returned; `false` — the agent never\n * read it and `text` is what it returns now (the screen says so).\n */\n fetched?: boolean | null;\n}\n\n/** The host's door to the viewer API — called only when a person opens a value. */\nexport type ContextViewLoader = (target: ContextViewTarget) => Promise<ContextViewedText>;\n\n/**\n * The blocks a value's detail shows beneath its own text, because the model\n * received them as part of that value: the Organization is its live statement\n * AND the organization catalog (owner ruling 2026-10-01). Such a block is\n * counted in the value's chars and is not listed again under \"Also sent\".\n */\nexport const CONTEXT_ROW_BLOCKS: Readonly<Record<string, readonly string[]>> = {\n organization: [\"organization_catalog\"],\n};\n\n/**\n * Values whose content the agent READS THROUGH A TOOL rather than receiving it,\n * keyed to that tool: the Organization's selected scopes' values are returned\n * by `scope_system` (by design, never inline). Their detail shows \"Agent can\n * fetch\" — the viewer's `fetchable` text, loaded when opened (RULES.md §5b).\n */\nexport const CONTEXT_FETCHABLE_ROWS: Readonly<Record<string, string>> = {\n organization: \"scope_system\",\n};\n\n/** The receipt blocks shown under `key`'s detail (`CONTEXT_ROW_BLOCKS`). */\nexport function blocksForRow(\n key: string,\n blocks: readonly ContextReceiptBlock[] | null | undefined,\n): ContextReceiptBlock[] {\n const ids = CONTEXT_ROW_BLOCKS[key] ?? [];\n return (blocks ?? []).filter((b) => ids.includes(b.id));\n}\n\n/** The receipt blocks not shown under any value present in `rowKeys`. */\nexport function unclaimedBlocks(\n blocks: readonly ContextReceiptBlock[] | null | undefined,\n rowKeys: Iterable<string>,\n): ContextReceiptBlock[] {\n const claimed = new Set<string>();\n for (const key of rowKeys) for (const id of CONTEXT_ROW_BLOCKS[key] ?? []) claimed.add(id);\n return (blocks ?? []).filter((b) => !claimed.has(b.id));\n}\n\n/** How the server consumed a value it did not keep under its own key (receipt `consumed_as`). */\nexport type ContextConsumedAs = \"expanded\" | \"directive\" | \"renamed\" | \"unaccounted\";\n\n/**\n * The reserved keys the server never keeps under their own name, and what it\n * does with each — the receipt rows say so (`consumed_as`). The same table is\n * the parity corpus's `consumed_key_cases` (aidream `RESERVED_CONSUMED_KEYS`).\n */\nexport const RESERVED_CONSUMED_KEYS: Readonly<Record<string, ContextConsumedAs>> = {\n surface_chain: \"expanded\",\n window_forms: \"expanded\",\n __table_access: \"directive\",\n __google_files: \"directive\",\n};\n\n/**\n * Where the server states each SERVER-AUTHORITATIVE value (RULES.md §5a, owner\n * ruling 2026-10-01: identity is the server's truth, never the page's claim).\n * The server drops the client's copy of these keys and the model reads the\n * server's own value, every turn: `\"active_context\"` — its `<active_context>`\n * block (the conversation's organization, the scopes it admitted);\n * `\"manifest\"` — a server-built value (the authenticated person, the server's\n * clock). A screen never shows the client's copy of one as what will be sent.\n * The same table is the parity corpus's `server_authoritative_keys` (aidream\n * `SERVER_AUTHORITATIVE_KEYS`).\n */\nexport const SERVER_AUTHORITATIVE_KEYS: Readonly<Record<string, \"manifest\" | \"active_context\">> = {\n user: \"manifest\",\n client: \"manifest\",\n organization: \"active_context\",\n active_scopes: \"active_context\",\n};\n\n/** True for a key whose value the server states itself (`SERVER_AUTHORITATIVE_KEYS`). */\nexport function isServerAuthoritativeKey(key: string): boolean {\n return Object.prototype.hasOwnProperty.call(SERVER_AUTHORITATIVE_KEYS, key);\n}\n\n/**\n * One block of server text the model received this turn that is not a single\n * value — the turn's `<active_context>`, earlier-turn context, the sandbox\n * briefing, skills, memory, the organization catalog… (receipt `blocks`,\n * RULES.md §5). `delivered` is its exact text, cut at the receipt's cap.\n */\nexport interface ContextReceiptBlock {\n /** Stable id: the per-turn slot it rode in, or the system-prompt addition's name. */\n id: string;\n /** The id in words (`humanizeIdentifier`, `@ai-matrx/kit/text-case`). */\n label: string;\n /** Size and hash; the text comes from the viewer (`kind: \"block\"`). */\n delivered: ContextDeliveredRef;\n}\n\n/** The per-turn receipt the server streams as `context_receipt` — RULES.md §5. */\nexport interface ContextReceipt {\n version: 1;\n surface: string | null;\n cap: number;\n /** False: this turn's model cannot read context at all (every row `blocked_by: \"model\"`). */\n model_reads_context: boolean;\n /** The person's saved rules could not be read; the turn ran without them. */\n rules_error: string | null;\n rows: ContextReceiptRow[];\n /** Every other block of server text the model received, in render order (absent: older server). */\n blocks?: ContextReceiptBlock[];\n}\n\nexport type ContextRowOrigin = \"page\" | \"attached\" | \"system\";\n\n/** Why the server kept a value from the model on this turn (receipt `blocked_by`). */\nexport type ContextBlockedBy = \"model\" | \"self_check\";\n\n/** A client's resolved row: what its table renders and what `buildContextWire` sends. */\nexport interface ResolvedContextRow extends Omit<ContextRuleResult, \"delivery\"> {\n delivery: ContextRowDelivery;\n /**\n * The server resolves this value (a resource reference, a `*_id` UUID): size\n * and delivery are unknown (`chars: null`, `delivery: \"server\"` when\n * included), and its default limit is not predicted. Include and the\n * person's own rule still resolve normally.\n */\n serverResolved?: boolean;\n /** Display-only: chars / limit / delivery were filled from a receipt (`applyReceiptToRows`). */\n fromReceipt?: boolean;\n /** From a receipt: why the server kept this value from the model this turn. */\n blockedBy?: ContextBlockedBy;\n /**\n * Display-only, from a receipt: what the model read for this value. When set,\n * a detail view shows THIS — never `value`, the client's pre-send copy.\n */\n delivered?: ContextDeliveredRef;\n /** Display-only, from a receipt: what the `context` tool returns for it (size + hash). */\n onRequest?: ContextDeliveredRef;\n /** Display-only, from a receipt: blocks the model received as part of this value. */\n deliveredBlocks?: ContextReceiptBlock[];\n key: string;\n label: string;\n surfaceKey: string;\n origin: ContextRowOrigin;\n /** The raw value, or an already-rich envelope (`{ content, type, … }`). */\n value: unknown;\n chars: number | null;\n /** The saved rule that was applied (merged `_default` + surface row), or null. */\n userRule: SavedContextRule | null;\n description?: string;\n /** Context object type for the wire envelope (`text`, `json`, …). */\n type?: string;\n /** The non-person layers the rule was resolved from. Absent = default only. */\n layers?: ContextRuleLayers;\n}\n\n/** What a client knows about one value before resolution. */\nexport interface ContextRowSource {\n key: string;\n label: string;\n surfaceKey?: string;\n origin: ContextRowOrigin;\n value: unknown;\n /** Override the measured size (e.g. `null` for an unresolved lazy source). */\n chars?: number | null;\n description?: string;\n type?: string;\n layers?: ContextRuleLayers;\n}\n\n// ── Helpers ─────────────────────────────────────────────────────────────────\n\nconst isNum = (v: unknown): v is number => typeof v === \"number\" && Number.isFinite(v);\nconst isValidLimit = (v: unknown): v is number =>\n typeof v === \"number\" && Number.isInteger(v) && v >= 0;\n\n// ── resolveContextRule — RULES.md §2, line for line ─────────────────────────\n\nexport function resolveContextRule(input: ContextRuleInput): ContextRuleResult {\n const agent = input.agent ?? null;\n const surface = input.surface ?? null;\n const user = input.user ?? null;\n const cap = isNum(input.cap) ? input.cap : DEFAULT_INLINE_CAP;\n const agentDeclared = agent?.declared === true;\n\n const identity = input.identity === true;\n\n let include = true;\n let includeBy: ContextLayer = \"default\";\n if (!identity && surface?.declared === true && surface.auto_context === false) {\n if (agentDeclared) {\n include = true;\n includeBy = \"agent\";\n } else {\n include = false;\n includeBy = \"page\";\n }\n }\n if (!identity && agent?.kill_switch === true && !agentDeclared) {\n include = false;\n includeBy = \"agent\";\n }\n if (user && typeof user.include === \"boolean\") {\n include = user.include;\n includeBy = \"you\";\n }\n\n let limit = isNum(input.default_max) ? input.default_max : DEFAULT_INLINE_MAX_CHARS;\n let limitBy: ContextLayer = \"default\";\n const a = agentDeclared && isNum(agent?.max_inline_chars) ? agent.max_inline_chars : null;\n const s = isNum(surface?.max_inline_chars) ? surface.max_inline_chars : null;\n if (a !== null && s !== null) {\n if (s < a) {\n limit = s;\n limitBy = \"page\";\n } else {\n limit = a;\n limitBy = \"agent\";\n }\n } else if (a !== null) {\n limit = a;\n limitBy = \"agent\";\n } else if (s !== null) {\n limit = s;\n limitBy = \"page\";\n }\n if (user && isValidLimit(user.max_inline_chars)) {\n limit = user.max_inline_chars;\n limitBy = \"you\";\n }\n const clamped = limit > cap;\n limit = Math.min(limit, cap);\n\n const chars = input.chars;\n const delivery: ContextDelivery = !include\n ? \"off\"\n : chars === null || chars === undefined || limit === 0 || chars > limit\n ? \"on_request\"\n : \"inline\";\n\n return {\n include,\n max_inline_chars: limit,\n delivery,\n decided_by: { include: includeBy, max_inline_chars: limitBy },\n clamped,\n };\n}\n\n// ── contextValueChars — RULES.md §1 ─────────────────────────────────────────\n\n/** Count Unicode code points (never UTF-16 units: `\"😀\".length` is 2). */\nfunction codePoints(s: string): number {\n let n = 0;\n for (const _ of s) n += 1;\n return n;\n}\n\n/**\n * Size of a value in code points, exactly as the server's `content_as_str()`:\n * a string is itself; anything else is `JSON.stringify(value, null, 2)`\n * (Python `json.dumps(value, ensure_ascii=False, indent=2)`). `undefined` → 0.\n */\nexport function contextValueChars(value: unknown): number {\n if (value === undefined) return 0;\n if (typeof value === \"string\") return codePoints(value);\n let text: string | undefined;\n try {\n text = JSON.stringify(value, null, 2);\n } catch {\n text = String(value);\n }\n return text === undefined ? 0 : codePoints(text);\n}\n\n// ── lookupSavedRule — RULES.md §3 ───────────────────────────────────────────\n\nfunction sanitizeRule(raw: unknown): SavedContextRule {\n const out: SavedContextRule = {};\n if (!raw || typeof raw !== \"object\") return out;\n const r = raw as Record<string, unknown>;\n if (typeof r.include === \"boolean\") out.include = r.include;\n if (isValidLimit(r.max_inline_chars)) out.max_inline_chars = r.max_inline_chars;\n return out;\n}\n\n/**\n * The person's rule for one key on one surface: `_default[key]` merged per\n * field under `rows[surfaceKey][key]`, invalid fields stripped. `null` when\n * nothing valid is saved.\n */\nexport function lookupSavedRule(\n rows: SavedContextRuleRows | null | undefined,\n surfaceKey: string,\n key: string,\n): SavedContextRule | null {\n if (!rows || typeof rows !== \"object\") return null;\n const base = sanitizeRule(rows[DEFAULT_SURFACE_KEY]?.[key]);\n const own = surfaceKey === DEFAULT_SURFACE_KEY ? {} : sanitizeRule(rows[surfaceKey]?.[key]);\n const merged: SavedContextRule = { ...base, ...own };\n return Object.keys(merged).length === 0 ? null : merged;\n}\n\n/**\n * The person's rule for one key, drift-proof (identical to aidream's\n * `lookup_user_rule_for`). While a primary surface P is in play, every key\n * filed under `_default` OR under P resolves to the same per-field merge\n * `{ ..._default[key], ...rows[P][key] }` (P wins) — whichever surface the\n * client's or the server's manifest says \"declares\" the key. A code manifest\n * and the DB manifest can disagree (sync drift); this makes the person's rule\n * land the same on both sides anyway. A surroundings key (any other surface)\n * keeps its own surface: `{ ..._default[key], ...rows[surfaceKey][key] }`.\n * With no primary surface this is exactly `lookupSavedRule`.\n *\n * Saving is unchanged: page values save to P, everything else to `_default`.\n */\nexport function lookupSavedRuleFor(\n rows: SavedContextRuleRows | null | undefined,\n primarySurface: string | null | undefined,\n surfaceKey: string,\n key: string,\n): SavedContextRule | null {\n const primary = primarySurface && primarySurface !== DEFAULT_SURFACE_KEY ? primarySurface : null;\n if (primary && (surfaceKey === DEFAULT_SURFACE_KEY || surfaceKey === primary)) {\n return lookupSavedRule(rows, primary, key);\n }\n return lookupSavedRule(rows, surfaceKey, key);\n}\n\n// ── serverDefaultMax — the server's per-key default, read from the wire value ─\n\n/**\n * The keys that make a dict the rich envelope form — exactly aidream\n * `context_objects._RICH_FORM_KEYS`. A dict with ANY other key is raw content\n * on the server, `content` key or not.\n */\nexport const RICH_ENVELOPE_KEYS: ReadonlySet<string> = new Set([\n \"content\",\n \"mutable\",\n \"persist\",\n \"source\",\n \"type\",\n \"label\",\n \"description\",\n \"max_inline_chars\",\n \"default_max_inline_chars\",\n \"summary_agent_id\",\n]);\n\n/**\n * A UUID as the server's `_uuid` (Python `UUID(value.strip())`) accepts it:\n * hyphens optional, `{…}` braces and `urn:uuid:` prefixes allowed.\n */\nfunction isUuidLike(value: string): boolean {\n const hex = value\n .trim()\n .replace(/urn:/gi, \"\")\n .replace(/uuid:/gi, \"\")\n .replace(/^\\{+|\\}+$/g, \"\")\n .replace(/-/g, \"\");\n return /^[0-9a-f]{32}$/i.test(hex);\n}\n\nfunction isPlainObject(v: unknown): v is Record<string, unknown> {\n return !!v && typeof v === \"object\" && !Array.isArray(v);\n}\n\n/** A canonical resource reference the server promotes (aidream `parse_resource_reference`). */\nfunction isResourceRef(v: Record<string, unknown>): boolean {\n if (v.__kind !== \"resource_ref\") return false;\n const type = v.resource_type || v.type;\n const id = v.resource_id || v.id;\n return (\n typeof type === \"string\" &&\n type.trim() !== \"\" &&\n typeof id === \"string\" &&\n isUuidLike(id)\n );\n}\n\nfunction hasOwn(o: Record<string, unknown>, k: string): boolean {\n return Object.prototype.hasOwnProperty.call(o, k);\n}\n\nfunction onlyRichKeys(o: Record<string, unknown>): boolean {\n return Object.keys(o).every((k) => RICH_ENVELOPE_KEYS.has(k));\n}\n\nfunction hasLazySource(o: Record<string, unknown>): boolean {\n return isPlainObject(o.source) && typeof o.source.kind === \"string\";\n}\n\n/**\n * A rich envelope exactly as the server reads one (aidream\n * `_split_rich_context_value`): a plain object with its own `content` whose\n * every key is in `RICH_ENVELOPE_KEYS`. A record that merely HAS `content`\n * (a note `{ id, title, content }`) is raw content, not an envelope.\n */\nexport function isRichEnvelope(value: unknown): value is Record<string, unknown> {\n return isPlainObject(value) && hasOwn(value, \"content\") && onlyRichKeys(value);\n}\n\n/** The source-only lazy pointer form: rich keys only, no `content`, a `source` with a `kind`. */\nfunction isLazyPointer(value: unknown): value is Record<string, unknown> {\n return isPlainObject(value) && !hasOwn(value, \"content\") && onlyRichKeys(value) && hasLazySource(value);\n}\n\n/**\n * Split one wire value into `{ content, overrides }` exactly as aidream\n * `_split_rich_context_value`: a rich envelope or lazy pointer → its `content`\n * (null when absent) and every other key; anything else → the whole value as\n * content, no overrides.\n */\nexport function splitContextValue(value: unknown): {\n content: unknown;\n overrides: Record<string, unknown>;\n} {\n if (isRichEnvelope(value) || isLazyPointer(value)) {\n const { content = null, ...overrides } = value;\n return { content, overrides };\n }\n return { content: value, overrides: {} };\n}\n\n/**\n * The server's default inline limit for one wire value — the `default` layer\n * the server will use, before page/agent/you. Mirrors aidream\n * `context_rules._server_default_max` over the object `ContextManifest.build`\n * makes from the value (after `normalize_resource_context`):\n *\n * - a rich envelope (every key in the rich-form set, and `content` present or a\n * lazy `source` with a string `kind`) carrying a whole-number, non-negative\n * `default_max_inline_chars` → that;\n * - a top-level resource reference (`__kind: \"resource_ref\"` with a type and a\n * UUID id), or a lazy source whose `extra.resource_type` is set → 0\n * (descriptor-first);\n * - a lazy source (a `source` and null/absent `content`) → 6000;\n * - anything else → null (the server's 200).\n *\n * A resource reference nested as an envelope's `content` is NOT promoted by the\n * server (it reads top-level values only), so it is plain JSON content → null.\n */\nexport function serverDefaultMax(value: unknown): number | null {\n if (!isPlainObject(value)) return null;\n if (isResourceRef(value)) return 0;\n if (!isRichEnvelope(value) && !isLazyPointer(value)) return null;\n if (isValidLimit(value.default_max_inline_chars)) return value.default_max_inline_chars;\n if (hasLazySource(value) && (value.content === null || value.content === undefined)) {\n const src = value.source as Record<string, unknown>;\n const extra = isPlainObject(src.extra) ? src.extra : {};\n return extra.resource_type ? 0 : LAZY_SOURCE_DEFAULT_MAX;\n }\n return null;\n}\n\n// ── resolveContextRow — the glue a client runs per value ────────────────────\n\n/** Resolve one value into the row the table renders and the wire sends. */\nexport function resolveContextRow(\n source: ContextRowSource,\n savedRows: SavedContextRuleRows | null | undefined,\n cap: number = DEFAULT_INLINE_CAP,\n /** The page in play; makes the rule lookup drift-proof (`lookupSavedRuleFor`). */\n primarySurface?: string | null,\n): ResolvedContextRow {\n const surfaceKey = source.surfaceKey ?? DEFAULT_SURFACE_KEY;\n const serverResolved = isServerResolved(source.key, source.value);\n const chars = serverResolved\n ? null\n : source.chars !== undefined\n ? source.chars\n : unresolvedSize(source.value)\n ? null\n : contextValueChars(splitContextValue(source.value).content);\n const userRule = lookupSavedRuleFor(savedRows, primarySurface, surfaceKey, source.key);\n const layers: ContextRuleLayers = { ...(source.layers ?? {}) };\n if (!serverResolved && (layers.default_max === undefined || layers.default_max === null)) {\n const serverDefault = serverDefaultMax(source.value);\n if (serverDefault !== null) layers.default_max = serverDefault;\n }\n // Identity is decided as the server decides its own statement of it (RULES.md §5a): only the\n // person's rule closes it — never the page switch or the agent's / mandate's kill switch.\n const identity = isServerAuthoritativeKey(source.key);\n const result = resolveContextRule({ ...layers, chars, user: userRule, cap, identity });\n const row: ResolvedContextRow = {\n ...result,\n delivery: serverResolved && result.include ? \"server\" : result.delivery,\n key: source.key,\n label: source.label,\n surfaceKey,\n origin: source.origin,\n value: source.value,\n chars,\n userRule,\n };\n if (source.description !== undefined) row.description = source.description;\n if (source.type !== undefined) row.type = source.type;\n if (source.layers !== undefined || layers.default_max !== undefined) row.layers = layers;\n if (serverResolved) row.serverResolved = true;\n return row;\n}\n\n// ── buildContextWire — the ONLY producer of a request's `context` ──────────\n\n/**\n * Size the server cannot know yet: a lazy pointer (or an envelope with a lazy\n * source and null content), or a resource reference it will promote to one.\n */\nfunction unresolvedSize(value: unknown): boolean {\n if (!isPlainObject(value)) return false;\n if (isResourceRef(value)) return true;\n return (\n (isRichEnvelope(value) || isLazyPointer(value)) &&\n hasLazySource(value) &&\n (value.content === null || value.content === undefined)\n );\n}\n\nconst KIND_RESOURCE_REF = \"resource_ref\";\n\n/**\n * Values the server acts on by their top-level shape in `request.context`\n * (aidream `normalize_resource_context` → `parse_resource_reference`, which\n * reads ONLY top-level values):\n *\n * - a resource reference — any object whose `__kind` is `\"resource_ref\"`\n * (its own promote/exclude/overlay policy rides inside it);\n * - a bare UUID string under a conventional `*_id` key (`file_id`,\n * `attached_file_id`, `current_note_id`, …), which the server promotes when\n * the noun is a registered resource type. The registry lives on the server,\n * so every `*_id` key with a UUID passes through; for an unregistered noun\n * the server reads the bare string exactly as it would read it wrapped;\n * - a reserved key the server consumes by shape (`RESERVED_CONSUMED_KEYS`:\n * `surface_chain`, `window_forms`, `__table_access`, `__google_files`) —\n * wrapped, the server reads `{content, label}` instead of its list or grant\n * and drops it.\n *\n * These ship exactly as-is: no label, type, description or limit added.\n */\nexport function isWireDirective(key: string, value: unknown): boolean {\n if (Object.hasOwn(RESERVED_CONSUMED_KEYS, key)) return true;\n if (isPlainObject(value)) return value.__kind === KIND_RESOURCE_REF;\n return (\n typeof value === \"string\" &&\n isIdKey(key) &&\n isUuidLike(value)\n );\n}\n\n/** The server's key rule for promoting a bare id (`_resource_type_for_key`): a `*_id` key. */\nfunction isIdKey(key: string): boolean {\n return key.trim().toLowerCase().endsWith(\"_id\");\n}\n\n/**\n * A value whose size, default limit and delivery the SERVER decides by\n * resolving it — so the client must not predict them:\n *\n * - a top-level resource reference (`__kind: \"resource_ref\"`), bare or as a\n * rich envelope's `content`;\n * - a UUID string under a `*_id` key, bare or as a rich envelope's `content`.\n *\n * The server only promotes an id when its noun is in the resource registry\n * (aidream `_resource_type_for_key`), which the client cannot see, so every\n * `*_id` UUID counts. This is a deliberate superset: on a value the server\n * does not promote, the client only stops predicting — it never makes a wrong\n * promise.\n */\nexport function isServerResolved(key: string, value: unknown): boolean {\n const inner = isRichEnvelope(value) ? value.content : value;\n if (isPlainObject(inner)) return inner.__kind === KIND_RESOURCE_REF;\n return typeof inner === \"string\" && isIdKey(key) && isUuidLike(inner);\n}\n\n/**\n * Build the request's `context` object from the resolved rows. This is the\n * ONLY function that may produce it, so what the table shows and what the\n * request carries cannot drift apart.\n *\n * - A row whose `include` is false is omitted entirely — it never reaches the\n * wire (RULES.md §1: off means off everywhere).\n * - An included row becomes `{ content, type?, label, description?,\n * max_inline_chars? }`. An already-rich envelope (`isRichEnvelope`, e.g. a\n * working document carrying `source`/`mutable`/`persist`, or a lazy pointer)\n * keeps every non-rule field it already has; `label`/`description`/`type`\n * are only filled when missing.\n * - `max_inline_chars` on the envelope is the PAGE layer's value only\n * (`layers.surface.max_inline_chars`, else the envelope's own page value).\n * The server reads the person's saved rules itself every turn (RULES.md §3)\n * and the agent layer from its own policy, so the wire must never carry the\n * person's number or the resolved effective limit dressed up as a page rule\n * — that would make a stale client value outrank the server's fresh read.\n */\nexport function buildContextWire(rows: readonly ResolvedContextRow[]): Record<string, unknown> {\n const wire: Record<string, unknown> = {};\n for (const row of rows) {\n if (!row.include) continue;\n // A row with no value (a receipt-built display row) has nothing to send.\n if (row.value === undefined) continue;\n // Rich: an envelope with `content`, or a source-only lazy pointer. Every\n // non-rule field survives — including `default_max_inline_chars`, the\n // server's per-key default (not a person's or a page's number).\n // A record that merely has `content` is NOT rich: it is wrapped whole, or\n // the server would read it as raw content and drop the envelope fields.\n // A top-level directive the server acts on by SHAPE ships exactly as-is:\n // wrapped, it becomes plain JSON and silently stops resolving.\n if (isWireDirective(row.key, row.value)) {\n wire[row.key] = row.value;\n continue;\n }\n const rich = isRichEnvelope(row.value) || isLazyPointer(row.value);\n const envelope: Record<string, unknown> = rich\n ? { ...(row.value as Record<string, unknown>) }\n : { content: row.value };\n delete envelope.max_inline_chars;\n if (envelope.type === undefined && row.type !== undefined) envelope.type = row.type;\n if (envelope.label === undefined) envelope.label = row.label;\n if (envelope.description === undefined && row.description !== undefined) {\n envelope.description = row.description;\n }\n const pageLimit = row.layers?.surface?.max_inline_chars;\n const ownPageLimit = rich ? (row.value as Record<string, unknown>).max_inline_chars : undefined;\n if (isValidLimit(pageLimit)) envelope.max_inline_chars = pageLimit;\n else if (isValidLimit(ownPageLimit)) envelope.max_inline_chars = ownPageLimit;\n wire[row.key] = envelope;\n }\n return wire;\n}\n\n// ── compareReceipt — RULES.md §6 ────────────────────────────────────────────\n\nexport interface ContextReceiptMismatch {\n key: string;\n field: \"include\" | \"max_inline_chars\" | \"delivery\" | \"user_rule\" | \"missing\";\n expected: unknown;\n actual: unknown;\n /**\n * `\"self_check\"`: a real difference from what was shown — the server's\n * safety check stripped the value before the model call. Say \"removed by\n * the server's safety check\", not \"the screen was wrong\".\n */\n reason?: \"self_check\";\n}\n\nfunction normalizeUserRule(rule: SavedContextRule | null | undefined): SavedContextRule | null {\n const clean = sanitizeRule(rule);\n return Object.keys(clean).length === 0 ? null : clean;\n}\n\nfunction sameRule(a: SavedContextRule | null, b: SavedContextRule | null): boolean {\n if (a === null || b === null) return a === b;\n return a.include === b.include && a.max_inline_chars === b.max_inline_chars;\n}\n\n/**\n * Compare the rows the client resolved (and rendered, and sent) with the\n * receipt the server emitted for the same turn.\n *\n * For each expected row, the receipt row is matched by key + surface key, then\n * by key alone. Compared: `include`, `max_inline_chars`, `delivery` (only when\n * both sides know the size) and `user_rule`. An expected row absent from the\n * receipt is a `missing` mismatch — except a row the client correctly withheld\n * for a reason other than the person's own rule (page `auto_context` off, the\n * agent's kill switch): the client never sent it and the server lists only\n * the person's saved `include: false` rules as `origin: \"rule\"` rows, so its\n * absence is the truth, not a lie. Receipt rows nobody expected are returned\n * as `systemRows` — shown, never hidden. A row the server consumed\n * (`consumed_as`: expanded / directive / renamed) is compared on `include`\n * and `user_rule` only — its parts carry their own rows; an `\"unaccounted\"`\n * row, or an included envelope `\"expanded\"` into no parts, is a `missing`\n * mismatch.\n */\nexport function compareReceipt(\n expected: readonly ResolvedContextRow[],\n receipt: ContextReceipt,\n): { mismatches: ContextReceiptMismatch[]; systemRows: ContextReceiptRow[]; modelReadsContext: boolean } {\n const mismatches: ContextReceiptMismatch[] = [];\n // A turn whose model reads no context delivers nothing by design: every row\n // is `delivery: \"off\"`, `blocked_by: \"model\"`. That is not a lie about the\n // person's rules, so delivery is not compared; include / limit / rule still are.\n const modelReadsContext = receipt.model_reads_context !== false;\n const used = new Set<ContextReceiptRow>();\n const rows = receipt.rows ?? [];\n\n for (const exp of expected) {\n const actual =\n rows.find((r) => !used.has(r) && r.key === exp.key && r.surface_key === exp.surfaceKey) ??\n rows.find((r) => !used.has(r) && r.key === exp.key);\n if (!actual) {\n const withheldByOthers = !exp.include && exp.decided_by.include !== \"you\";\n if (!withheldByOthers) {\n mismatches.push({ key: exp.key, field: \"missing\", expected: exp.delivery, actual: null });\n }\n continue;\n }\n used.add(actual);\n if (actual.include !== exp.include) {\n mismatches.push({ key: exp.key, field: \"include\", expected: exp.include, actual: actual.include });\n }\n // A consumed value (expanded / directive / renamed) was not delivered as\n // itself: its parts carry their own rows, limits and deliveries. Only\n // include and the person's rule are this key's own promise.\n const consumed = Boolean(actual.consumed_as);\n // A server path removed the value with no reason: kept as a row so it is\n // never silently missing, and still a broken promise on this screen.\n if (actual.consumed_as === \"unaccounted\" && exp.include) {\n mismatches.push({ key: exp.key, field: \"missing\", expected: exp.delivery, actual: \"unaccounted\" });\n }\n // An envelope the screen showed as sent that the server expanded into no\n // parts delivered nothing (a window whose fields were all unreadable):\n // \"expanded\" alone would pass it silently.\n if (\n actual.consumed_as === \"expanded\" &&\n actual.include &&\n exp.include &&\n (actual.consumed_into ?? []).length === 0\n ) {\n mismatches.push({ key: exp.key, field: \"missing\", expected: exp.delivery, actual: \"expanded_into_nothing\" });\n }\n // A server-resolved value's limit is only the client's to promise when the\n // person set one; its default-derived limit and delivery are the server's.\n const promisesLimit =\n !consumed && (!exp.serverResolved || exp.userRule?.max_inline_chars !== undefined);\n if (promisesLimit && actual.max_inline_chars !== exp.max_inline_chars) {\n mismatches.push({\n key: exp.key,\n field: \"max_inline_chars\",\n expected: exp.max_inline_chars,\n actual: actual.max_inline_chars,\n });\n }\n // The safety check stripped a value the person included: always a\n // mismatch (server-resolved or not), tagged with its reason.\n const selfChecked = actual.blocked_by === \"self_check\" && exp.include;\n if (selfChecked) {\n mismatches.push({\n key: exp.key,\n field: \"delivery\",\n expected: exp.delivery,\n actual: actual.delivery,\n reason: \"self_check\",\n });\n }\n if (\n !selfChecked &&\n !consumed &&\n modelReadsContext &&\n !exp.serverResolved &&\n exp.delivery !== \"server\" &&\n exp.chars !== null &&\n actual.chars !== null &&\n actual.delivery !== exp.delivery\n ) {\n mismatches.push({ key: exp.key, field: \"delivery\", expected: exp.delivery, actual: actual.delivery });\n }\n const expRule = normalizeUserRule(exp.userRule);\n const actRule = normalizeUserRule(actual.user_rule);\n if (!sameRule(expRule, actRule)) {\n mismatches.push({ key: exp.key, field: \"user_rule\", expected: expRule, actual: actRule });\n }\n }\n\n return { mismatches, systemRows: rows.filter((r) => !used.has(r)), modelReadsContext };\n}\n\n/**\n * For DISPLAY only: fill each server-resolved row's `chars`,\n * `max_inline_chars` and `delivery` from the latest receipt row for the same\n * key (key + surface key, then key), marking it `fromReceipt`. Other rows are\n * returned unchanged. Never compare these rows — `compareReceipt` takes the\n * rows the client resolved before sending.\n */\nexport function applyReceiptToRows(\n rows: readonly ResolvedContextRow[],\n receipt: ContextReceipt | null | undefined,\n): ResolvedContextRow[] {\n const receiptRows = receipt?.rows ?? [];\n return rows.map((row) => {\n const hit =\n receiptRows.find((r) => r.key === row.key && r.surface_key === row.surfaceKey) ??\n receiptRows.find((r) => r.key === row.key);\n if (!hit) return row;\n // What the model read is the server's to report for EVERY value — a value the\n // server replaced or reshaped must never show as the client's copy. A value the\n // server states itself (RULES.md §5a) is measured by what it stated, never by\n // the page's copy.\n const attached = blocksForRow(row.key, receipt?.blocks);\n const stated =\n hit.delivered && isServerAuthoritativeKey(row.key)\n ? hit.delivered.chars + attached.reduce((n, b) => n + b.delivered.chars, 0)\n : null;\n const shown = {\n ...row,\n ...deliveredFields(hit),\n ...(stated !== null ? { chars: stated } : {}),\n ...(attached.length ? { deliveredBlocks: attached } : {}),\n };\n if (!row.serverResolved) return shown;\n return {\n ...shown,\n chars: stated ?? hit.chars,\n max_inline_chars: hit.max_inline_chars,\n delivery: hit.delivery,\n fromReceipt: true,\n ...(hit.blocked_by ? { blockedBy: hit.blocked_by } : {}),\n };\n });\n}\n\n// ── receiptRowToResolved — read-only rows for a sent message ────────────────\n\n/**\n * Turn one receipt row into the row a read-only table renders (a sent\n * message's context, read back from `chat.message.model_context`). The value\n * itself is not in the receipt, so `value` is undefined.\n */\nexport function receiptRowToResolved(\n row: ContextReceiptRow,\n blocks?: readonly ContextReceiptBlock[] | null,\n): ResolvedContextRow {\n const attached = row.delivered ? blocksForRow(row.key, blocks) : [];\n const origin: ContextRowOrigin =\n row.surface_key !== DEFAULT_SURFACE_KEY ? \"page\" : row.origin === \"client\" ? \"attached\" : \"system\";\n return {\n key: row.key,\n label: row.label,\n surfaceKey: row.surface_key,\n origin,\n value: undefined,\n chars:\n row.delivered && isServerAuthoritativeKey(row.key)\n ? row.delivered.chars + attached.reduce((n, b) => n + b.delivered.chars, 0)\n : row.chars,\n userRule: normalizeUserRule(row.user_rule),\n include: row.include,\n max_inline_chars: row.max_inline_chars,\n delivery: row.delivery,\n decided_by: { include: row.decided_by.include, max_inline_chars: row.decided_by.max_inline_chars },\n clamped: row.clamped,\n layers: receiptLayers(row),\n ...(row.blocked_by ? { blockedBy: row.blocked_by } : {}),\n ...deliveredFields(row),\n ...(attached.length ? { deliveredBlocks: attached } : {}),\n };\n}\n\n/** A receipt row's `delivered` / `on_request` as display fields (absent when not sent). */\nexport function deliveredFields(\n row: Pick<ContextReceiptRow, \"delivered\" | \"on_request\">,\n): Pick<ResolvedContextRow, \"delivered\" | \"onRequest\"> {\n return {\n ...(row.delivered ? { delivered: row.delivered } : {}),\n ...(row.on_request ? { onRequest: row.on_request } : {}),\n };\n}\n\n/**\n * Rebuild the non-person layers from what the server applied, so a read-only\n * table shows the applied limit as the inherited value and marks the layer\n * that decided it. The limit goes on the layer named by\n * `decided_by.max_inline_chars` (`you` is carried by `userRule`); include is\n * rebuilt the same way for a page `auto_context` off or the agent's kill switch.\n */\n/**\n * Keys the client has a value for but is NOT sending this turn (excluded by\n * the page, the agent or the person's rule). Clients send this list as the\n * request's `context_withheld`, so the receipt's `origin: \"rule\"` rows are\n * limited to keys actually withheld on this page. Deduplicated, in row order.\n */\nexport function withheldKeys(rows: readonly ResolvedContextRow[]): string[] {\n const out: string[] = [];\n for (const row of rows) {\n if (row.include || row.value === undefined || out.includes(row.key)) continue;\n out.push(row.key);\n }\n return out;\n}\n\nconst DOCUMENT_KEY_PREFIXES = [\"attached_document_\", \"attached_file_\", \"resource_file_\"];\n\n/**\n * Receipt rows the client never sent (`compareReceipt(...).systemRows`:\n * server-seeded attachments, scope seeds, saved off-rules…) as table rows, so\n * a person can see and govern them — the normal Include switch and Inline max\n * apply. Documents (`attached_document_*`, `attached_file_*`,\n * `resource_file_*`) group as \"attached\", everything else as \"system\". The\n * server resolved them (`serverResolved`), values come from the receipt\n * (`fromReceipt`), and `surfaceKey` is the row's own `surface_key` — so a rule\n * saved for one writes to `_default` unless the server filed it under a page.\n * `value` is undefined: `buildContextWire` never sends these rows.\n */\nexport function systemRowsToResolved(systemRows: readonly ContextReceiptRow[]): ResolvedContextRow[] {\n return systemRows.map((receiptRow) => {\n const row = receiptRowToResolved(receiptRow);\n const isDocument = DOCUMENT_KEY_PREFIXES.some((p) => receiptRow.key.startsWith(p));\n return {\n ...row,\n origin: isDocument ? \"attached\" : \"system\",\n serverResolved: true,\n fromReceipt: true,\n };\n });\n}\n\nfunction receiptLayers(row: ContextReceiptRow): ContextRuleLayers {\n const layers: ContextRuleLayers = {};\n const limit = row.max_inline_chars;\n const limitBy = row.decided_by.max_inline_chars;\n const includeBy = row.decided_by.include;\n if (limitBy === \"default\") layers.default_max = limit;\n // Page auto_context off: the page dropped it, or the agent re-admitted it.\n const pageOff = (includeBy === \"page\" && !row.include) || (includeBy === \"agent\" && row.include);\n if (limitBy === \"page\" || pageOff) {\n layers.surface = {\n declared: true,\n auto_context: !pageOff,\n max_inline_chars: limitBy === \"page\" ? limit : null,\n };\n }\n if (limitBy === \"agent\") {\n // An agent limit means the key is declared; a declared key the agent decided to include\n // was re-admitted past a page auto_context off. (The kill switch never closes a declared key.)\n layers.agent = { declared: true, max_inline_chars: limit };\n } else if (includeBy === \"agent\") {\n // Off = the kill switch closing an undeclared key; on = a declaration re-admitting a\n // page auto_context-off value.\n layers.agent = row.include ? { declared: true } : { declared: false, kill_switch: true };\n }\n return layers;\n}\n\n// ── attachmentContextKey — the context key of a conversation attachment ─────\n\n/**\n * The context key the server files a conversation attachment under, derivable\n * by every client from `GET /conversations/{id}/attachments` (aidream\n * `conversation_attachments/briefing.py`; corpus `attachment_key_cases`):\n *\n * `attached_resource_` + encode(`${provider}:${resourceRef}`)\n *\n * where encode keeps ASCII letters, digits and `-`, and writes every other\n * UTF-8 byte as `_` + two lowercase hex digits (`_` itself is `_5f`). Unique\n * across providers, case-preserving (Drive ids), stable.\n */\nexport function attachmentContextKey(provider: string, resourceRef: string): string {\n const bytes = new TextEncoder().encode(`${provider}:${resourceRef}`);\n let out = \"attached_resource_\";\n for (const b of bytes) {\n const keep =\n (b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5a) || (b >= 0x61 && b <= 0x7a) || b === 0x2d;\n out += keep ? String.fromCharCode(b) : `_${b.toString(16).padStart(2, \"0\")}`;\n }\n return out;\n}\n\n// ── Display ─────────────────────────────────────────────────────────────────\n\n/** Compact size for a tabular column: `950`, `18.4k`, `184k`, `1.2M`, `—` for unknown. */\nexport function formatChars(n: number | null | undefined): string {\n if (n === null || n === undefined || !Number.isFinite(n)) return \"—\";\n const abs = Math.abs(n);\n const trim = (s: string) => s.replace(/\\.0$/, \"\");\n if (abs < 1000) return String(Math.round(n));\n if (abs < 100_000) return `${trim((n / 1000).toFixed(1))}k`;\n if (abs < 1_000_000) return `${Math.round(n / 1000)}k`;\n return `${trim((n / 1_000_000).toFixed(1))}M`;\n}\n\n// RULES.md §0 — what a page's own conversation (or a switched-off page) never receives.\nexport * from \"./page-context\";\n","/**\n * What a page's OWN conversation — or a conversation whose page switch is off —\n * never receives (common-docs context-delivery RULES.md §0).\n *\n * Arman, 2026-10-01: the agent in a battle \"cannot and should not know its own\n * conversation id, route and things that identify itself. User, Client and\n * Organization are ok.\" A window or panel chat over the page is NOT the page's\n * own conversation and keeps receiving the page.\n *\n * Clients send the case as the request field `page_context`\n * (`{ mode, withheld? }`); aidream's context gate enforces it at the PAGE layer\n * (`decided_by.include === \"page\"`). The lists and every case are the parity\n * corpus's `page_context_defaults` / `page_context_cases` — aidream\n * `page_context.py` runs the same file.\n */\n\nexport type PageContextMode = \"own\" | \"off\";\n\n/** The request field `page_context`. Omitted = a conversation that follows the page. */\nexport interface PageContextDirective {\n mode: PageContextMode;\n /** The page's declared list; omitted = the default for the mode. */\n withheld?: string[];\n}\n\n/** A switched-off page: everything that IS the page — its surroundings and its route. */\nexport const PAGE_OFF_WITHHELD: readonly string[] = [\n \"route_brief\",\n \"surface_chain\",\n \"window_forms\",\n \"surface_closed\",\n];\n\n/** A page's own conversation: the page, plus what identifies the conversation itself. */\nexport const OWN_CONVERSATION_WITHHELD: readonly string[] = [...PAGE_OFF_WITHHELD, \"conversation\"];\n\n/** Values about the PERSON, never the page — never withheld, whatever a page declares. */\nexport const PERSON_CONTEXT_VALUES: ReadonlySet<string> = new Set([\n \"user\",\n \"client\",\n \"organization\",\n \"project\",\n \"task\",\n \"active_scopes\",\n]);\n\n/** What the gate knows about one value when deciding (mirrors aidream `ValueFacts`). */\nexport interface PageContextValueFacts {\n /** `\"surroundings\"` for a value from the screens around the page. */\n origin?: string;\n /** The page itself declared this value. */\n surfaceDeclared?: boolean;\n /** The surface that emitted it (`\"_default\"` when none). */\n surfaceKey?: string;\n}\n\nexport function pageContextWithheldKeys(page: PageContextDirective): ReadonlySet<string> {\n const listed = page.withheld ?? (page.mode === \"own\" ? OWN_CONVERSATION_WITHHELD : PAGE_OFF_WITHHELD);\n return new Set(listed.filter((key) => !PERSON_CONTEXT_VALUES.has(key)));\n}\n\n/**\n * True when the page rule keeps `key` from this conversation: a listed key, a\n * value from the screens around the page, or a value the page itself declared —\n * never a person value.\n */\nexport function pageContextWithholds(\n page: PageContextDirective | null | undefined,\n key: string,\n facts: PageContextValueFacts = {},\n): boolean {\n if (!page || PERSON_CONTEXT_VALUES.has(key)) return false;\n if (pageContextWithheldKeys(page).has(key)) return true;\n if (facts.origin === \"surroundings\") return true;\n return Boolean(facts.surfaceDeclared && facts.surfaceKey && facts.surfaceKey !== \"_default\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC0BO,IAAM,oBAAuC;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,4BAA+C,CAAC,GAAG,mBAAmB,cAAc;AAG1F,IAAM,wBAA6C,oBAAI,IAAI;AAAA,EAChE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAYM,SAAS,wBAAwB,MAAiD;AACvF,QAAM,SAAS,KAAK,aAAa,KAAK,SAAS,QAAQ,4BAA4B;AACnF,SAAO,IAAI,IAAI,OAAO,OAAO,CAAC,QAAQ,CAAC,sBAAsB,IAAI,GAAG,CAAC,CAAC;AACxE;AAOO,SAAS,qBACd,MACA,KACA,QAA+B,CAAC,GACvB;AACT,MAAI,CAAC,Q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CAAC;AACxD,MAAI,MAAM,IAAW,QAAO,GAAG,KAAK,MAAM,IAAI,GAAI,CAAC;AACnD,SAAO,GAAG,MAAM,IAAI,KAAW,QAAQ,CAAC,CAAC,CAAC;AAC5C;","names":[]}
@@ -1,5 +1,3 @@
1
- import { humanizeIdentifier } from '@ai-matrx/kit/text-case';
2
-
3
1
  /**
4
2
  * What a page's OWN conversation — or a conversation whose page switch is off —
5
3
  * never receives (common-docs context-delivery RULES.md §0).
@@ -58,7 +56,6 @@ declare function pageContextWithholds(page: PageContextDirective | null | undefi
58
56
  * context = buildContextWire(rows) // what the request sends
59
57
  * …on `context_receipt`: compareReceipt(rows, receipt) // screen vs server
60
58
  */
61
-
62
59
  /** `users.user_surface_state.feature` for the person's saved rules. */
63
60
  declare const CONTEXT_RULES_FEATURE = "context_rules";
64
61
  /** Surface key for values no surface emitted (attachments, server-added values). */
@@ -295,7 +292,7 @@ declare function isServerAuthoritativeKey(key: string): boolean;
295
292
  interface ContextReceiptBlock {
296
293
  /** Stable id: the per-turn slot it rode in, or the system-prompt addition's name. */
297
294
  id: string;
298
- /** The id in words (`humanizeContextKey`). */
295
+ /** The id in words (`humanizeIdentifier`, `@ai-matrx/kit/text-case`). */
299
296
  label: string;
300
297
  /** Size and hash; the text comes from the viewer (`kind: "block"`). */
301
298
  delivered: ContextDeliveredRef;
@@ -584,16 +581,7 @@ declare function systemRowsToResolved(systemRows: readonly ContextReceiptRow[]):
584
581
  * across providers, case-preserving (Drive ids), stable.
585
582
  */
586
583
  declare function attachmentContextKey(provider: string, resourceRef: string): string;
587
- /**
588
- * A context key in words. The ONE humanizer is `@ai-matrx/kit/text-case`
589
- * `humanizeIdentifier` (corpus `humanize_cases` in `rules-corpus.json`, Python
590
- * twin aidream `humanize_context_key`); this name stays only until matrx-frontend
591
- * imports the kit function, then it is deleted.
592
- */
593
- declare const humanizeContextKey: typeof humanizeIdentifier;
594
- /** @see IDENTIFIER_ACRONYMS in `@ai-matrx/kit/text-case`. */
595
- declare const CONTEXT_KEY_ACRONYMS: readonly string[];
596
584
  /** Compact size for a tabular column: `950`, `18.4k`, `184k`, `1.2M`, `—` for unknown. */
597
585
  declare function formatChars(n: number | null | undefined): string;
598
586
 
599
- export { CONTEXT_FETCHABLE_ROWS, CONTEXT_KEY_ACRONYMS, CONTEXT_ROW_BLOCKS, CONTEXT_RULES_FEATURE, type ContextAgentLayer, type ContextBlockedBy, type ContextConsumedAs, type ContextDeliveredRef, type ContextDelivery, type ContextLayer, type ContextReceipt, type ContextReceiptBlock, type ContextReceiptMismatch, type ContextReceiptRow, type ContextRowDelivery, type ContextRowOrigin, type ContextRowSource, type ContextRuleInput, type ContextRuleLayers, type ContextRuleResult, type ContextSurfaceLayer, type ContextViewKind, type ContextViewLoader, type ContextViewTarget, type ContextViewedText, DEFAULT_INLINE_CAP, DEFAULT_INLINE_MAX_CHARS, DEFAULT_SURFACE_KEY, LAZY_SOURCE_DEFAULT_MAX, OWN_CONVERSATION_WITHHELD, PAGE_OFF_WITHHELD, PERSON_CONTEXT_VALUES, type PageContextDirective, type PageContextMode, type PageContextValueFacts, RESERVED_CONSUMED_KEYS, RICH_ENVELOPE_KEYS, type ResolvedContextRow, SERVER_AUTHORITATIVE_KEYS, type SavedContextRule, type SavedContextRuleRows, applyReceiptToRows, attachmentContextKey, blocksForRow, buildContextWire, compareReceipt, contextValueChars, deliveredFields, formatChars, humanizeContextKey, isRichEnvelope, isServerAuthoritativeKey, isServerResolved, isWireDirective, lookupSavedRule, lookupSavedRuleFor, pageContextWithheldKeys, pageContextWithholds, receiptRowToResolved, resolveContextRow, resolveContextRule, serverDefaultMax, splitContextValue, systemRowsToResolved, unclaimedBlocks, withheldKeys };
587
+ export { CONTEXT_FETCHABLE_ROWS, CONTEXT_ROW_BLOCKS, CONTEXT_RULES_FEATURE, type ContextAgentLayer, type ContextBlockedBy, type ContextConsumedAs, type ContextDeliveredRef, type ContextDelivery, type ContextLayer, type ContextReceipt, type ContextReceiptBlock, type ContextReceiptMismatch, type ContextReceiptRow, type ContextRowDelivery, type ContextRowOrigin, type ContextRowSource, type ContextRuleInput, type ContextRuleLayers, type ContextRuleResult, type ContextSurfaceLayer, type ContextViewKind, type ContextViewLoader, type ContextViewTarget, type ContextViewedText, DEFAULT_INLINE_CAP, DEFAULT_INLINE_MAX_CHARS, DEFAULT_SURFACE_KEY, LAZY_SOURCE_DEFAULT_MAX, OWN_CONVERSATION_WITHHELD, PAGE_OFF_WITHHELD, PERSON_CONTEXT_VALUES, type PageContextDirective, type PageContextMode, type PageContextValueFacts, RESERVED_CONSUMED_KEYS, RICH_ENVELOPE_KEYS, type ResolvedContextRow, SERVER_AUTHORITATIVE_KEYS, type SavedContextRule, type SavedContextRuleRows, applyReceiptToRows, attachmentContextKey, blocksForRow, buildContextWire, compareReceipt, contextValueChars, deliveredFields, formatChars, isRichEnvelope, isServerAuthoritativeKey, isServerResolved, isWireDirective, lookupSavedRule, lookupSavedRuleFor, pageContextWithheldKeys, pageContextWithholds, receiptRowToResolved, resolveContextRow, resolveContextRule, serverDefaultMax, splitContextValue, systemRowsToResolved, unclaimedBlocks, withheldKeys };
@@ -1,5 +1,3 @@
1
- import { humanizeIdentifier } from '@ai-matrx/kit/text-case';
2
-
3
1
  /**
4
2
  * What a page's OWN conversation — or a conversation whose page switch is off —
5
3
  * never receives (common-docs context-delivery RULES.md §0).
@@ -58,7 +56,6 @@ declare function pageContextWithholds(page: PageContextDirective | null | undefi
58
56
  * context = buildContextWire(rows) // what the request sends
59
57
  * …on `context_receipt`: compareReceipt(rows, receipt) // screen vs server
60
58
  */
61
-
62
59
  /** `users.user_surface_state.feature` for the person's saved rules. */
63
60
  declare const CONTEXT_RULES_FEATURE = "context_rules";
64
61
  /** Surface key for values no surface emitted (attachments, server-added values). */
@@ -295,7 +292,7 @@ declare function isServerAuthoritativeKey(key: string): boolean;
295
292
  interface ContextReceiptBlock {
296
293
  /** Stable id: the per-turn slot it rode in, or the system-prompt addition's name. */
297
294
  id: string;
298
- /** The id in words (`humanizeContextKey`). */
295
+ /** The id in words (`humanizeIdentifier`, `@ai-matrx/kit/text-case`). */
299
296
  label: string;
300
297
  /** Size and hash; the text comes from the viewer (`kind: "block"`). */
301
298
  delivered: ContextDeliveredRef;
@@ -584,16 +581,7 @@ declare function systemRowsToResolved(systemRows: readonly ContextReceiptRow[]):
584
581
  * across providers, case-preserving (Drive ids), stable.
585
582
  */
586
583
  declare function attachmentContextKey(provider: string, resourceRef: string): string;
587
- /**
588
- * A context key in words. The ONE humanizer is `@ai-matrx/kit/text-case`
589
- * `humanizeIdentifier` (corpus `humanize_cases` in `rules-corpus.json`, Python
590
- * twin aidream `humanize_context_key`); this name stays only until matrx-frontend
591
- * imports the kit function, then it is deleted.
592
- */
593
- declare const humanizeContextKey: typeof humanizeIdentifier;
594
- /** @see IDENTIFIER_ACRONYMS in `@ai-matrx/kit/text-case`. */
595
- declare const CONTEXT_KEY_ACRONYMS: readonly string[];
596
584
  /** Compact size for a tabular column: `950`, `18.4k`, `184k`, `1.2M`, `—` for unknown. */
597
585
  declare function formatChars(n: number | null | undefined): string;
598
586
 
599
- export { CONTEXT_FETCHABLE_ROWS, CONTEXT_KEY_ACRONYMS, CONTEXT_ROW_BLOCKS, CONTEXT_RULES_FEATURE, type ContextAgentLayer, type ContextBlockedBy, type ContextConsumedAs, type ContextDeliveredRef, type ContextDelivery, type ContextLayer, type ContextReceipt, type ContextReceiptBlock, type ContextReceiptMismatch, type ContextReceiptRow, type ContextRowDelivery, type ContextRowOrigin, type ContextRowSource, type ContextRuleInput, type ContextRuleLayers, type ContextRuleResult, type ContextSurfaceLayer, type ContextViewKind, type ContextViewLoader, type ContextViewTarget, type ContextViewedText, DEFAULT_INLINE_CAP, DEFAULT_INLINE_MAX_CHARS, DEFAULT_SURFACE_KEY, LAZY_SOURCE_DEFAULT_MAX, OWN_CONVERSATION_WITHHELD, PAGE_OFF_WITHHELD, PERSON_CONTEXT_VALUES, type PageContextDirective, type PageContextMode, type PageContextValueFacts, RESERVED_CONSUMED_KEYS, RICH_ENVELOPE_KEYS, type ResolvedContextRow, SERVER_AUTHORITATIVE_KEYS, type SavedContextRule, type SavedContextRuleRows, applyReceiptToRows, attachmentContextKey, blocksForRow, buildContextWire, compareReceipt, contextValueChars, deliveredFields, formatChars, humanizeContextKey, isRichEnvelope, isServerAuthoritativeKey, isServerResolved, isWireDirective, lookupSavedRule, lookupSavedRuleFor, pageContextWithheldKeys, pageContextWithholds, receiptRowToResolved, resolveContextRow, resolveContextRule, serverDefaultMax, splitContextValue, systemRowsToResolved, unclaimedBlocks, withheldKeys };
587
+ export { CONTEXT_FETCHABLE_ROWS, CONTEXT_ROW_BLOCKS, CONTEXT_RULES_FEATURE, type ContextAgentLayer, type ContextBlockedBy, type ContextConsumedAs, type ContextDeliveredRef, type ContextDelivery, type ContextLayer, type ContextReceipt, type ContextReceiptBlock, type ContextReceiptMismatch, type ContextReceiptRow, type ContextRowDelivery, type ContextRowOrigin, type ContextRowSource, type ContextRuleInput, type ContextRuleLayers, type ContextRuleResult, type ContextSurfaceLayer, type ContextViewKind, type ContextViewLoader, type ContextViewTarget, type ContextViewedText, DEFAULT_INLINE_CAP, DEFAULT_INLINE_MAX_CHARS, DEFAULT_SURFACE_KEY, LAZY_SOURCE_DEFAULT_MAX, OWN_CONVERSATION_WITHHELD, PAGE_OFF_WITHHELD, PERSON_CONTEXT_VALUES, type PageContextDirective, type PageContextMode, type PageContextValueFacts, RESERVED_CONSUMED_KEYS, RICH_ENVELOPE_KEYS, type ResolvedContextRow, SERVER_AUTHORITATIVE_KEYS, type SavedContextRule, type SavedContextRuleRows, applyReceiptToRows, attachmentContextKey, blocksForRow, buildContextWire, compareReceipt, contextValueChars, deliveredFields, formatChars, isRichEnvelope, isServerAuthoritativeKey, isServerResolved, isWireDirective, lookupSavedRule, lookupSavedRuleFor, pageContextWithheldKeys, pageContextWithholds, receiptRowToResolved, resolveContextRow, resolveContextRule, serverDefaultMax, splitContextValue, systemRowsToResolved, unclaimedBlocks, withheldKeys };
@@ -1,6 +1,3 @@
1
- // context/index.ts
2
- import { IDENTIFIER_ACRONYMS, humanizeIdentifier } from "@ai-matrx/kit/text-case";
3
-
4
1
  // context/page-context.ts
5
2
  var PAGE_OFF_WITHHELD = [
6
3
  "route_brief",
@@ -463,8 +460,6 @@ function attachmentContextKey(provider, resourceRef) {
463
460
  }
464
461
  return out;
465
462
  }
466
- var humanizeContextKey = humanizeIdentifier;
467
- var CONTEXT_KEY_ACRONYMS = IDENTIFIER_ACRONYMS;
468
463
  function formatChars(n) {
469
464
  if (n === null || n === void 0 || !Number.isFinite(n)) return "\u2014";
470
465
  const abs = Math.abs(n);
@@ -476,7 +471,6 @@ function formatChars(n) {
476
471
  }
477
472
  export {
478
473
  CONTEXT_FETCHABLE_ROWS,
479
- CONTEXT_KEY_ACRONYMS,
480
474
  CONTEXT_ROW_BLOCKS,
481
475
  CONTEXT_RULES_FEATURE,
482
476
  DEFAULT_INLINE_CAP,
@@ -497,7 +491,6 @@ export {
497
491
  contextValueChars,
498
492
  deliveredFields,
499
493
  formatChars,
500
- humanizeContextKey,
501
494
  isRichEnvelope,
502
495
  isServerAuthoritativeKey,
503
496
  isServerResolved,