modelrig 0.9.0 → 0.11.0

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.
Files changed (231) hide show
  1. package/CHANGELOG.md +227 -0
  2. package/README.md +51 -27
  3. package/data/PROVENANCE.md +14 -0
  4. package/data/pricing-map.json +26 -0
  5. package/dist/adapters/attempt-meta-map.d.ts +8 -1
  6. package/dist/adapters/attempt-meta-map.d.ts.map +1 -1
  7. package/dist/adapters/attempt-meta-map.js +4 -0
  8. package/dist/adapters/build-adapters.js +2 -2
  9. package/dist/adapters/gemini.d.ts +28 -1
  10. package/dist/adapters/gemini.d.ts.map +1 -1
  11. package/dist/adapters/gemini.js +38 -9
  12. package/dist/adapters/grok.d.ts.map +1 -1
  13. package/dist/adapters/grok.js +53 -4
  14. package/dist/adapters/openai-compat.d.ts +8 -2
  15. package/dist/adapters/openai-compat.d.ts.map +1 -1
  16. package/dist/adapters/openai-compat.js +8 -1
  17. package/dist/adapters/openai-stream.d.ts +10 -2
  18. package/dist/adapters/openai-stream.d.ts.map +1 -1
  19. package/dist/adapters/openai-stream.js +5 -1
  20. package/dist/adapters/openai.d.ts +10 -1
  21. package/dist/adapters/openai.d.ts.map +1 -1
  22. package/dist/adapters/openai.js +57 -7
  23. package/dist/adapters/responses-api.d.ts +61 -0
  24. package/dist/adapters/responses-api.d.ts.map +1 -0
  25. package/dist/adapters/responses-api.js +124 -0
  26. package/dist/artifacts/artifact.d.ts +20 -0
  27. package/dist/artifacts/artifact.d.ts.map +1 -1
  28. package/dist/artifacts/artifact.js +207 -171
  29. package/dist/artifacts/index.d.ts +3 -1
  30. package/dist/artifacts/index.d.ts.map +1 -1
  31. package/dist/artifacts/index.js +8 -1
  32. package/dist/bakeoff/groundedness.d.ts +58 -0
  33. package/dist/bakeoff/groundedness.d.ts.map +1 -0
  34. package/dist/bakeoff/groundedness.js +165 -0
  35. package/dist/bakeoff/quality.d.ts +14 -1
  36. package/dist/bakeoff/quality.d.ts.map +1 -1
  37. package/dist/bakeoff/quality.js +10 -0
  38. package/dist/bakeoff/replay.d.ts.map +1 -1
  39. package/dist/bakeoff/replay.js +4 -0
  40. package/dist/cli.d.ts.map +1 -1
  41. package/dist/cli.js +8 -0
  42. package/dist/config.d.ts +31 -0
  43. package/dist/config.d.ts.map +1 -1
  44. package/dist/config.js +23 -0
  45. package/dist/doctor-cli.d.ts +7 -0
  46. package/dist/doctor-cli.d.ts.map +1 -0
  47. package/dist/doctor-cli.js +68 -0
  48. package/dist/doctor-preflight.d.ts +64 -0
  49. package/dist/doctor-preflight.d.ts.map +1 -0
  50. package/dist/doctor-preflight.js +167 -0
  51. package/dist/feedback/coverage.d.ts.map +1 -1
  52. package/dist/feedback/coverage.js +9 -2
  53. package/dist/feedback/grade.d.ts +8 -1
  54. package/dist/feedback/grade.d.ts.map +1 -1
  55. package/dist/feedback/grade.js +4 -0
  56. package/dist/gateway/http-protocol.d.ts +19 -0
  57. package/dist/gateway/http-protocol.d.ts.map +1 -1
  58. package/dist/gateway/serve-client.d.ts +99 -0
  59. package/dist/gateway/serve-client.d.ts.map +1 -0
  60. package/dist/gateway/serve-client.js +360 -0
  61. package/dist/index.d.ts +31 -3
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +71 -8
  64. package/dist/prompts/manifest.d.ts +128 -0
  65. package/dist/prompts/manifest.d.ts.map +1 -0
  66. package/dist/prompts/manifest.js +214 -0
  67. package/dist/prompts/prompt-fetch.d.ts +84 -0
  68. package/dist/prompts/prompt-fetch.d.ts.map +1 -0
  69. package/dist/prompts/prompt-fetch.js +120 -0
  70. package/dist/prompts/prompt-resolve.d.ts +76 -0
  71. package/dist/prompts/prompt-resolve.d.ts.map +1 -0
  72. package/dist/prompts/prompt-resolve.js +93 -0
  73. package/dist/prompts/slot-resolver.d.ts +91 -0
  74. package/dist/prompts/slot-resolver.d.ts.map +1 -0
  75. package/dist/prompts/slot-resolver.js +135 -0
  76. package/dist/prompts/tools-register.d.ts +60 -0
  77. package/dist/prompts/tools-register.d.ts.map +1 -0
  78. package/dist/prompts/tools-register.js +48 -0
  79. package/dist/prompts/version-hash.d.ts +29 -0
  80. package/dist/prompts/version-hash.d.ts.map +1 -0
  81. package/dist/prompts/version-hash.js +54 -0
  82. package/dist/prompts-export-cli.d.ts +21 -0
  83. package/dist/prompts-export-cli.d.ts.map +1 -0
  84. package/dist/prompts-export-cli.js +130 -0
  85. package/dist/registry/cache-policy.d.ts.map +1 -1
  86. package/dist/registry/cache-policy.js +22 -0
  87. package/dist/registry/pricing-shape.d.ts +38 -8
  88. package/dist/registry/pricing-shape.d.ts.map +1 -1
  89. package/dist/registry/pricing-shape.js +40 -12
  90. package/dist/registry/pricing.d.ts +6 -5
  91. package/dist/registry/pricing.d.ts.map +1 -1
  92. package/dist/registry/pricing.js +9 -5
  93. package/dist/routes/destination-loader.d.ts +16 -0
  94. package/dist/routes/destination-loader.d.ts.map +1 -0
  95. package/dist/routes/destination-loader.js +29 -0
  96. package/dist/routes/draft.d.ts +7 -0
  97. package/dist/routes/draft.d.ts.map +1 -1
  98. package/dist/routes/draft.js +12 -0
  99. package/dist/routes/loader.d.ts.map +1 -1
  100. package/dist/routes/loader.js +27 -54
  101. package/dist/routes/output-loader.d.ts +42 -0
  102. package/dist/routes/output-loader.d.ts.map +1 -0
  103. package/dist/routes/output-loader.js +112 -0
  104. package/dist/routes/prompt-loader.d.ts +49 -0
  105. package/dist/routes/prompt-loader.d.ts.map +1 -0
  106. package/dist/routes/prompt-loader.js +113 -0
  107. package/dist/routes/resolved-bundle.d.ts +19 -0
  108. package/dist/routes/resolved-bundle.d.ts.map +1 -1
  109. package/dist/routes/resolved-bundle.js +24 -0
  110. package/dist/routes/screen-loader.d.ts +1 -1
  111. package/dist/routes/screen-loader.d.ts.map +1 -1
  112. package/dist/routes/screen-loader.js +8 -1
  113. package/dist/run/accept.d.ts +3 -0
  114. package/dist/run/accept.d.ts.map +1 -1
  115. package/dist/run/accept.js +1 -0
  116. package/dist/run/cost.d.ts +31 -2
  117. package/dist/run/cost.d.ts.map +1 -1
  118. package/dist/run/cost.js +57 -2
  119. package/dist/run/destination.d.ts +39 -0
  120. package/dist/run/destination.d.ts.map +1 -0
  121. package/dist/run/destination.js +45 -0
  122. package/dist/run/dispatch.d.ts +25 -3
  123. package/dist/run/dispatch.d.ts.map +1 -1
  124. package/dist/run/dispatch.js +26 -4
  125. package/dist/run/raw-defaults.d.ts.map +1 -1
  126. package/dist/run/raw-defaults.js +6 -1
  127. package/dist/run/raw-finalize.d.ts +96 -0
  128. package/dist/run/raw-finalize.d.ts.map +1 -0
  129. package/dist/run/raw-finalize.js +283 -0
  130. package/dist/run/raw-knobs.d.ts +36 -0
  131. package/dist/run/raw-knobs.d.ts.map +1 -0
  132. package/dist/run/raw-knobs.js +99 -0
  133. package/dist/run/raw-request.d.ts +24 -0
  134. package/dist/run/raw-request.d.ts.map +1 -0
  135. package/dist/run/raw-request.js +69 -0
  136. package/dist/run/raw-schema.d.ts +76 -0
  137. package/dist/run/raw-schema.d.ts.map +1 -0
  138. package/dist/run/raw-schema.js +166 -0
  139. package/dist/run/raw-search-cost.d.ts +49 -0
  140. package/dist/run/raw-search-cost.d.ts.map +1 -0
  141. package/dist/run/raw-search-cost.js +79 -0
  142. package/dist/run/raw-support.d.ts +64 -6
  143. package/dist/run/raw-support.d.ts.map +1 -1
  144. package/dist/run/raw-support.js +84 -11
  145. package/dist/run/raw.d.ts +25 -26
  146. package/dist/run/raw.d.ts.map +1 -1
  147. package/dist/run/raw.js +30 -351
  148. package/dist/run/record-attempt.d.ts +8 -0
  149. package/dist/run/record-attempt.d.ts.map +1 -1
  150. package/dist/run/record-attempt.js +26 -12
  151. package/dist/run/record-call.d.ts +104 -0
  152. package/dist/run/record-call.d.ts.map +1 -0
  153. package/dist/run/record-call.js +277 -0
  154. package/dist/run/repair-attempt.d.ts +68 -0
  155. package/dist/run/repair-attempt.d.ts.map +1 -0
  156. package/dist/run/repair-attempt.js +180 -0
  157. package/dist/run/resolve-destination.d.ts +49 -0
  158. package/dist/run/resolve-destination.d.ts.map +1 -0
  159. package/dist/run/resolve-destination.js +76 -0
  160. package/dist/run/routing-docs.d.ts.map +1 -1
  161. package/dist/run/routing-docs.js +9 -4
  162. package/dist/run/run.d.ts +30 -1
  163. package/dist/run/run.d.ts.map +1 -1
  164. package/dist/run/run.js +75 -148
  165. package/dist/run/telemetry-row.d.ts +92 -0
  166. package/dist/run/telemetry-row.d.ts.map +1 -0
  167. package/dist/run/telemetry-row.js +81 -0
  168. package/dist/run/validate.d.ts +12 -1
  169. package/dist/run/validate.d.ts.map +1 -1
  170. package/dist/run/validate.js +31 -0
  171. package/dist/run/web-search-pricing.d.ts +43 -0
  172. package/dist/run/web-search-pricing.d.ts.map +1 -0
  173. package/dist/run/web-search-pricing.js +56 -0
  174. package/dist/settings/grading-guide.d.ts +27 -1
  175. package/dist/settings/grading-guide.d.ts.map +1 -1
  176. package/dist/status.d.ts +6 -0
  177. package/dist/status.d.ts.map +1 -1
  178. package/dist/status.js +4 -1
  179. package/dist/telemetry/artifact-store.d.ts.map +1 -1
  180. package/dist/telemetry/artifact-store.js +11 -0
  181. package/dist/telemetry/attempt-meta.d.ts +66 -1
  182. package/dist/telemetry/attempt-meta.d.ts.map +1 -1
  183. package/dist/telemetry/attempt-meta.js +27 -1
  184. package/dist/telemetry/exporter-setup.d.ts +2 -0
  185. package/dist/telemetry/exporter-setup.d.ts.map +1 -1
  186. package/dist/telemetry/exporter-setup.js +6 -1
  187. package/dist/telemetry/grade-mappers.d.ts +5 -2
  188. package/dist/telemetry/grade-mappers.d.ts.map +1 -1
  189. package/dist/telemetry/grade-mappers.js +8 -2
  190. package/dist/telemetry/grades-sql.d.ts +4 -1
  191. package/dist/telemetry/grades-sql.d.ts.map +1 -1
  192. package/dist/telemetry/grades-sql.js +26 -3
  193. package/dist/telemetry/ingest-protocol.d.ts +26 -2
  194. package/dist/telemetry/ingest-protocol.d.ts.map +1 -1
  195. package/dist/telemetry/ingest-protocol.js +32 -29
  196. package/dist/telemetry/lanes.d.ts +31 -0
  197. package/dist/telemetry/lanes.d.ts.map +1 -0
  198. package/dist/telemetry/lanes.js +41 -0
  199. package/dist/telemetry/org-identity.d.ts +47 -0
  200. package/dist/telemetry/org-identity.d.ts.map +1 -0
  201. package/dist/telemetry/org-identity.js +100 -0
  202. package/dist/telemetry/route-registration.d.ts +48 -0
  203. package/dist/telemetry/route-registration.d.ts.map +1 -0
  204. package/dist/telemetry/route-registration.js +77 -0
  205. package/dist/telemetry/sinks.d.ts +10 -1
  206. package/dist/telemetry/sinks.d.ts.map +1 -1
  207. package/dist/telemetry/sinks.js +52 -6
  208. package/dist/telemetry/store-migrations.d.ts.map +1 -1
  209. package/dist/telemetry/store-migrations.js +13 -0
  210. package/dist/tenancy/api-key-format.d.ts +16 -1
  211. package/dist/tenancy/api-key-format.d.ts.map +1 -1
  212. package/dist/tenancy/api-key-format.js +17 -0
  213. package/dist/types/provider.d.ts +20 -3
  214. package/dist/types/provider.d.ts.map +1 -1
  215. package/dist/types/provider.js +1 -0
  216. package/dist/types/route.d.ts +76 -0
  217. package/dist/types/route.d.ts.map +1 -1
  218. package/dist/types/telemetry.d.ts +30 -2
  219. package/dist/types/telemetry.d.ts.map +1 -1
  220. package/docs/agent-skill/SKILL.md +75 -5
  221. package/docs/instrumentation.md +98 -36
  222. package/docs/llms.txt +96 -16
  223. package/docs/managed-serving.md +150 -0
  224. package/docs/mcp-oracle.md +75 -2
  225. package/docs/migration-playbook.md +5 -4
  226. package/docs/quickstart.md +169 -83
  227. package/package.json +3 -2
  228. package/registry/registry.json +82 -0
  229. package/dist/feedback/feedback.d.ts +0 -35
  230. package/dist/feedback/feedback.d.ts.map +0 -1
  231. package/dist/feedback/feedback.js +0 -39
package/dist/run/raw.d.ts CHANGED
@@ -40,12 +40,20 @@
40
40
  * providers + gemini (anthropic fails closed). The provider×knob support set
41
41
  * is ONE table (`run/raw-support.ts`) the guard AND the published docs matrix
42
42
  * both read — they can never disagree (R5).
43
+ * - R2 (issue #6): `responseSchema` — a JSON Schema forwarded as the
44
+ * provider's NATIVE structured-output directive AND checked client-side with
45
+ * the shared Ajv; a miss is a billed, terminal `content_invalid` (never
46
+ * repaired, never retried). Fail-closed on providers without a native
47
+ * schema path (the same matrix). `meta.schema_sha` on the row = sha256 of
48
+ * the canonical schema, so declared-vs-served drift is queryable.
43
49
  */
44
- import { type RigManifest } from "../routes/manifest";
45
- import type { AdapterRequest, CandidateRef, ProviderId, ReasoningParams, ServiceTier, ToolChoice, ToolDefinition } from "../types/provider";
50
+ import type { RigManifest } from "../routes/manifest";
51
+ import type { ProviderId, ReasoningParams, ServiceTier, ToolChoice, ToolDefinition } from "../types/provider";
46
52
  import type { RunResult, RunTags } from "../types/route";
47
53
  import type { RunStreamHandle } from "./stream";
48
54
  import type { TelemetryStore } from "../types/telemetry";
55
+ export { assertRawKnobsSupported } from "./raw-knobs";
56
+ export { buildRawRequest } from "./raw-request";
49
57
  /** Raw-lane request. `systemPrompt`/`userPrompt` are already flattened from the
50
58
  * OpenAI `messages[]` by the caller; `tags` carries the caller's dimensions
51
59
  * (the server stamps `key_ownership` here). `lane: "raw"` is stamped by this
@@ -121,6 +129,21 @@ export interface RawRunInput {
121
129
  * appears nowhere in the prompt — is the CALLER's prompt responsibility on the
122
130
  * raw lane (documented in the R5 matrix, never injected here). */
123
131
  readonly responseFormat?: "json_object";
132
+ /** R2 (issue #6) — a JSON Schema for the response, on the raw lane. Forwarded
133
+ * as the provider's NATIVE structured-output directive (gemini
134
+ * `responseJsonSchema` + JSON mime; openai/grok `response_format: json_schema`
135
+ * strict; anthropic a forced tool call) via the adapters' existing
136
+ * `outputSchema` path, and the returned text is validated client-side by the
137
+ * SAME Ajv the routed lane uses: a miss is a billed, terminal `content_invalid`
138
+ * row + thrown `RigFailureError` (the raw lane never repairs/retries —
139
+ * `meta.validated` stays false; graduate the step to a route for a ladder).
140
+ * GUARDED: deepseek and the OpenAI-compat hosts (deepinfra/fireworks/zai) have
141
+ * no native schema path on this lane and are rejected pre-dispatch
142
+ * (`assertRawKnobsSupported`), as is a schema that is not a plain object, does
143
+ * not compile, or is declared alongside `responseFormat`. The row's
144
+ * `meta.schema_sha` is `sha256:` of the canonical (sorted-key) schema JSON.
145
+ * Absent ⇒ byte-identical (outputSchema stays null). */
146
+ readonly responseSchema?: object;
124
147
  }
125
148
  export interface RawRunDeps {
126
149
  readonly telemetry: TelemetryStore;
@@ -133,30 +156,6 @@ export interface RawRunDeps {
133
156
  * `routesDir: null` runRaw-only rig) ⇒ the warning is a no-op. */
134
157
  readonly manifest?: RigManifest | null;
135
158
  }
136
- /**
137
- * CE-2/CE-9 fail-closed guard (spec §3 F1/F3, CE-9 R1/R2/R4). A DECLARED
138
- * directive is NEVER silently dropped: a `grounding`/`responseFormat` on a
139
- * provider that cannot honor it, a `reasoning` on a provider with NO effort
140
- * control (deepseek/anthropic), or a `grounding.mode` other than the
141
- * v1-supported `"native"` throws `RigFailureError` `invariant_violation` (the
142
- * existing TERMINAL class; no new FailureClass member) BEFORE any adapter is
143
- * built, any provider call is made, or any telemetry row is written.
144
- *
145
- * The support facts come from `RAW_KNOB_SUPPORT` (run/raw-support.ts) — the ONE
146
- * table the published docs matrix also reads, so the guard and the docs can
147
- * never disagree (R5). What the guard does NOT reject (and why):
148
- * - reasoning on openai/deepinfra/fireworks is FORWARDED verbatim
149
- * (`kind: "forward"`) and the provider enforces — a rejection is classified
150
- * teachably by the adapter (no per-model allowlist, R1).
151
- * - reasoning on grok is a DISCLOSED coarsening (`kind: "coarsen"`).
152
- * - reasoning on gemini is model-gated (`kind: "model-gated"`) — the adapter
153
- * rejects a non-thinking model (`geminiReasoningUnsupported`).
154
- * Exported so the guard is unit-testable without a network. Idempotent and
155
- * side-effect-free.
156
- */
157
- export declare function assertRawKnobsSupported(input: RawRunInput): void;
158
- /** Pure raw-lane request shaping — exported for unit tests (no network). */
159
- export declare function buildRawRequest(candidate: CandidateRef, input: RawRunInput): AdapterRequest;
160
159
  /** Buffered raw dispatch (spec §4.2). Records telemetry, returns the RunResult,
161
160
  * or throws `RigFailureError` on an adapter failure. */
162
161
  export declare function runRaw(deps: RawRunDeps, input: RawRunInput): Promise<RunResult>;
@@ -1 +1 @@
1
- {"version":3,"file":"raw.d.ts","sourceRoot":"","sources":["../../src/run/raw.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAwBH,OAAO,EAAiC,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACrF,OAAO,KAAK,EACV,cAAc,EAGd,YAAY,EAGZ,UAAU,EACV,eAAe,EACf,WAAW,EACX,UAAU,EACV,cAAc,EACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAW,SAAS,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAGlE,OAAO,KAAK,EAAkB,eAAe,EAAE,MAAM,UAAU,CAAC;AAChE,OAAO,KAAK,EAA+B,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEtF;;;sCAGsC;AACtC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;iFAC6E;IAC7E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;IACxB;;;;;iFAK6E;IAC7E,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC;;;6EAGyE;IACzE,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,Q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ed). The provider×knob support set
42
42
  * is ONE table (`run/raw-support.ts`) the guard AND the published docs matrix
43
43
  * both read — they can never disagree (R5).
44
+ * - R2 (issue #6): `responseSchema` — a JSON Schema forwarded as the
45
+ * provider's NATIVE structured-output directive AND checked client-side with
46
+ * the shared Ajv; a miss is a billed, terminal `content_invalid` (never
47
+ * repaired, never retried). Fail-closed on providers without a native
48
+ * schema path (the same matrix). `meta.schema_sha` on the row = sha256 of
49
+ * the canonical schema, so declared-vs-served drift is queryable.
44
50
  */
45
51
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.assertRawKnobsSupported = assertRawKnobsSupported;
47
- exports.buildRawRequest = buildRawRequest;
52
+ exports.buildRawRequest = exports.assertRawKnobsSupported = void 0;
48
53
  exports.runRaw = runRaw;
49
54
  exports.runRawStream = runRawStream;
50
- const node_crypto_1 = require("node:crypto");
51
55
  const anthropic_1 = require("../adapters/anthropic");
52
56
  const deepseek_1 = require("../adapters/deepseek");
53
57
  const gemini_1 = require("../adapters/gemini");
54
58
  const grok_1 = require("../adapters/grok");
55
59
  const openai_1 = require("../adapters/openai");
56
60
  const openai_compat_1 = require("../adapters/openai-compat");
57
- const cost_1 = require("./cost");
58
- const raw_defaults_1 = require("./raw-defaults");
59
61
  const resolve_1 = require("./resolve");
60
- const raw_support_1 = require("./raw-support");
61
- const failures_1 = require("../types/failures");
62
- const run_context_1 = require("../artifacts/run-context");
63
- const manifest_1 = require("../routes/manifest");
64
- const attempt_meta_1 = require("../telemetry/attempt-meta");
65
- /**
66
- * R1 — resolve the effective raw-lane timeout. An explicit `timeoutMs` always
67
- * wins; absent, the fallback is TIER-AWARE (`RAW_TIMEOUT_DEFAULT_MS`), because a
68
- * passthrough should not be shorter-fused than the tier it advertises (a flex
69
- * grounded prompt needs minutes, not the old 120 s). No pre-dispatch rejection —
70
- * a short explicit timeout is a choice, and the teaching arrives on the first
71
- * `timeout` failure (`rawTimeoutFixHint`), which is when the caller is looking.
72
- */
73
- function resolveRawTimeoutMs(input) {
74
- return input.timeoutMs ?? raw_defaults_1.RAW_TIMEOUT_DEFAULT_MS[input.serviceTier ?? "standard"];
75
- }
76
- /** R1 — the teaching `fixHint` attached to a `timeout` failure on the raw lane:
77
- * it names the elapsed budget, the tier, that tier's advertised target, and the
78
- * two fixes (raise `timeoutMs`, or drop to `standard`). Built from
79
- * `RAW_TIMEOUT_DEFAULT_MS` so the default it quotes can never drift. */
80
- function rawTimeoutFixHint(input, tier) {
81
- const deadlineSec = resolveRawTimeoutMs(input) / 1000;
82
- const budgetMin = raw_defaults_1.RAW_TIMEOUT_DEFAULT_MS[tier] / 60000;
83
- const defaultSec = raw_defaults_1.RAW_TIMEOUT_DEFAULT_MS[tier] / 1000;
84
- // Only flex has a cheaper-latency tier to drop to; at standard/priority the
85
- // only fix is a longer deadline (never tell a caller to switch to the tier
86
- // they are already on).
87
- const tail = tier === "flex" ? ' or use serviceTier: "standard"' : "";
88
- return `timed out at the ${deadlineSec}s deadline at tier ${tier} — ${tier} targets up to ${budgetMin} min; raise timeoutMs (default ${defaultSec}s)${tail}`;
89
- }
62
+ // U4 extractions (behavior-preserving) — the knob guard, request shaper, and
63
+ // finalize/telemetry live in their own modules to keep raw.ts under the LOC cap.
64
+ // buildRawRequest / assertRawKnobsSupported are re-exported below so existing
65
+ // importers (`import { … } from "./raw"`) are unchanged.
66
+ const raw_knobs_1 = require("./raw-knobs");
67
+ const raw_request_1 = require("./raw-request");
68
+ const raw_finalize_1 = require("./raw-finalize");
69
+ var raw_knobs_2 = require("./raw-knobs");
70
+ Object.defineProperty(exports, "assertRawKnobsSupported", { enumerable: true, get: function () { return raw_knobs_2.assertRawKnobsSupported; } });
71
+ var raw_request_2 = require("./raw-request");
72
+ Object.defineProperty(exports, "buildRawRequest", { enumerable: true, get: function () { return raw_request_2.buildRawRequest; } });
90
73
  /** Conservative compat-host capability baseline — parity with createRig's
91
74
  * COMPAT_STATIC. Inconsequential for raw calls (outputSchema is always null, so
92
75
  * no JSON shaping fires), kept aligned so a raw and a routed compat call shape
93
76
  * identically if a schema is ever added. */
94
77
  const RAW_COMPAT_PROFILE = new Set(["json_mode"]);
95
78
  /** Construct a THROWAWAY adapter bound to the caller's BYOK key for one call.
96
- * Each adapter builds its provider client fresh inside `call()`/`stream()`, so
97
- * a per-call instance carries no shared state and leaks no key. Exhaustive over
98
- * ProviderId — a new provider forces a compile error here. */
79
+ * Each builds its provider client fresh inside `call()`/`stream()` (no shared
80
+ * state, no key leak). Exhaustive over ProviderId — a new provider fails here. */
99
81
  function buildRawAdapter(provider, apiKey) {
100
82
  switch (provider) {
101
83
  case "openai":
@@ -109,7 +91,8 @@ function buildRawAdapter(provider, apiKey) {
109
91
  case "deepseek":
110
92
  return (0, deepseek_1.createDeepSeekAdapter)({ apiKey });
111
93
  case "deepinfra":
112
- case "fireworks": {
94
+ case "fireworks":
95
+ case "zai": { // B4a: Z.ai joins the OpenAI-compat host family (verbatim model id)
113
96
  const host = openai_compat_1.OPENAI_COMPAT_HOSTS[provider];
114
97
  return (0, openai_compat_1.createOpenAICompatAdapter)({
115
98
  providerId: provider,
@@ -121,324 +104,20 @@ function buildRawAdapter(provider, apiKey) {
121
104
  }
122
105
  }
123
106
  }
124
- /**
125
- * CE-2/CE-9 fail-closed guard (spec §3 F1/F3, CE-9 R1/R2/R4). A DECLARED
126
- * directive is NEVER silently dropped: a `grounding`/`responseFormat` on a
127
- * provider that cannot honor it, a `reasoning` on a provider with NO effort
128
- * control (deepseek/anthropic), or a `grounding.mode` other than the
129
- * v1-supported `"native"` throws `RigFailureError` `invariant_violation` (the
130
- * existing TERMINAL class; no new FailureClass member) BEFORE any adapter is
131
- * built, any provider call is made, or any telemetry row is written.
132
- *
133
- * The support facts come from `RAW_KNOB_SUPPORT` (run/raw-support.ts) — the ONE
134
- * table the published docs matrix also reads, so the guard and the docs can
135
- * never disagree (R5). What the guard does NOT reject (and why):
136
- * - reasoning on openai/deepinfra/fireworks is FORWARDED verbatim
137
- * (`kind: "forward"`) and the provider enforces — a rejection is classified
138
- * teachably by the adapter (no per-model allowlist, R1).
139
- * - reasoning on grok is a DISCLOSED coarsening (`kind: "coarsen"`).
140
- * - reasoning on gemini is model-gated (`kind: "model-gated"`) — the adapter
141
- * rejects a non-thinking model (`geminiReasoningUnsupported`).
142
- * Exported so the guard is unit-testable without a network. Idempotent and
143
- * side-effect-free.
144
- */
145
- function assertRawKnobsSupported(input) {
146
- const support = raw_support_1.RAW_KNOB_SUPPORT[input.provider];
147
- const groundingMode = input.grounding?.mode;
148
- if (groundingMode !== undefined) {
149
- if (groundingMode !== "native") {
150
- throw new failures_1.RigFailureError({
151
- class: "invariant_violation",
152
- message: `unsupported raw-lane grounding mode "${groundingMode}"`,
153
- provider: input.provider,
154
- model: input.model,
155
- fixHint: 'only { mode: "native" } grounding is supported on the raw lane in v1',
156
- });
157
- }
158
- if (!support.grounding.supported) {
159
- throw new failures_1.RigFailureError({
160
- class: "invariant_violation",
161
- message: `native grounding on the raw lane is not supported for provider "${input.provider}"`,
162
- provider: input.provider,
163
- model: input.model,
164
- fixHint: `native grounding on the raw lane is supported on ${(0, raw_support_1.groundingSupportedProviders)().join(", ")} only; remove grounding or switch provider`,
165
- });
166
- }
167
- }
168
- if (input.reasoning !== undefined && support.reasoning.kind === "fail-closed") {
169
- throw new failures_1.RigFailureError({
170
- class: "invariant_violation",
171
- message: `reasoning control on the raw lane is not supported for provider "${input.provider}"`,
172
- provider: input.provider,
173
- model: input.model,
174
- // Provider-specific teaching straight from the matrix (deepseek →
175
- // deepseek-reasoner; anthropic → extended thinking unwired).
176
- fixHint: support.reasoning.fixHint ??
177
- "reasoning control is not available for this provider on the raw lane; remove reasoning",
178
- });
179
- }
180
- if (input.responseFormat !== undefined && !support.responseFormat.supported) {
181
- throw new failures_1.RigFailureError({
182
- class: "invariant_violation",
183
- message: `responseFormat on the raw lane is not supported for provider "${input.provider}"`,
184
- provider: input.provider,
185
- model: input.model,
186
- fixHint: `responseFormat: "json_object" on the raw lane is supported on ${(0, raw_support_1.responseFormatSupportedProviders)().join(", ")} only; remove responseFormat or switch provider`,
187
- });
188
- }
189
- }
190
- /** Pure raw-lane request shaping — exported for unit tests (no network). */
191
- function buildRawRequest(candidate, input) {
192
- // CE-6: forward temperature/topP through AdapterRequest.sampling ONLY when the
193
- // caller declared at least one — absent, no `sampling` key is added and the
194
- // request is byte-identical to pre-CE-6. maxOutputTokens keeps its top-level
195
- // channel (the adapter resolves sampling.maxOutputTokens ?? maxOutputTokens).
196
- const sampling = input.temperature !== undefined || input.topP !== undefined
197
- ? {
198
- ...(input.temperature !== undefined ? { temperature: input.temperature } : {}),
199
- ...(input.topP !== undefined ? { topP: input.topP } : {}),
200
- }
201
- : undefined;
202
- return {
203
- candidate,
204
- systemPrompt: input.systemPrompt,
205
- userPrompt: input.userPrompt,
206
- outputSchema: null, // raw lane never validates — the product distinction
207
- // CE-2 F2: forward the DECLARED tier (default "standard"); the meta/row
208
- // honesty fix in finalizeRaw reports this same value, never a hardcode.
209
- requestedTier: input.serviceTier ?? "standard",
210
- // R1: tier-aware default (RAW_TIMEOUT_DEFAULT_MS); explicit timeoutMs wins.
211
- timeoutMs: resolveRawTimeoutMs(input),
212
- ...(input.maxOutputTokens !== undefined ? { maxOutputTokens: input.maxOutputTokens } : {}),
213
- ...(sampling !== undefined ? { sampling } : {}),
214
- // CE-2 F1: `{ mode: "native" }` → the adapter's native-grounding directive.
215
- // Any other mode / unsupported provider never reaches here — the guard
216
- // (assertRawKnobsSupported) rejects it pre-dispatch. Absent ⇒ no key added.
217
- ...(input.grounding?.mode === "native" ? { grounding: true } : {}),
218
- // CE-2 F3: forward reasoning verbatim; absent ⇒ no key added (byte-identical).
219
- ...(input.reasoning !== undefined ? { reasoning: input.reasoning } : {}),
220
- // CE-9 R4: forward the json_object directive verbatim; absent ⇒ no key added
221
- // (byte-identical). An unsupported provider (anthropic) never reaches here —
222
- // the guard (assertRawKnobsSupported) rejects it pre-dispatch.
223
- ...(input.responseFormat !== undefined ? { responseFormat: input.responseFormat } : {}),
224
- ...(input.tools !== undefined ? { tools: input.tools } : {}),
225
- ...(input.toolChoice !== undefined ? { toolChoice: input.toolChoice } : {}),
226
- ...(input.cache !== undefined ? { cache: input.cache } : {}),
227
- };
228
- }
229
- /** Write the metered raw-lane telemetry row. Mirrors the run loop's minimal
230
- * `recordSearchCostRow` shape: only load-bearing fields set, route = the
231
- * `provider/model` handle the caller used, lane + key_ownership riding in
232
- * `tags_json`. The exporter mirrors it on its normal cadence — no lane column,
233
- * no schema change (telemetry map, 2026-08-10). */
234
- function recordRawRow(telemetry, p) {
235
- const row = {
236
- id: p.id,
237
- ts: new Date().toISOString(),
238
- route: p.rawModelId,
239
- routeVersion: 0,
240
- provider: p.provider,
241
- model: p.model,
242
- requestedTier: p.requestedTier,
243
- servedTier: p.usage?.servedTier ?? "standard",
244
- tokensIn: p.usage?.tokensIn ?? 0,
245
- tokensOut: p.usage?.tokensOut ?? 0,
246
- tokensCached: p.usage?.tokensCached ?? 0,
247
- tokensCacheWrite: p.usage?.tokensCacheWrite ?? 0,
248
- tokensCacheWrite1h: p.usage?.tokensCacheWrite1h ?? 0,
249
- costEstimateUsd: p.costUsd,
250
- latencyMs: p.latencyMs,
251
- ttfbMs: p.usage?.ttfbMs ?? null,
252
- failureClass: p.failureClass,
253
- attemptsJson: "{}",
254
- // Rung tag (R1 review, F1): the raw lane is a single primary dispatch. Raw
255
- // rows carry `lane: "raw"` and are excluded from route rollups, but every row
256
- // gets a rung for a uniform contract.
257
- tagsJson: JSON.stringify({ ...p.tags, rung: "primary" }),
258
- fingerprintCell: null,
259
- servedVariant: null,
260
- repairedBy: null,
261
- pricingMissing: p.pricingMissing,
262
- // CE-1: a customer cache handle (if supplied) stamps `cache_key`; the raw
263
- // lane runs no WS-C stamping, so `prefix_fingerprint` is always null and the
264
- // provenance rule reads any non-null key here as `customer`.
265
- cacheKey: p.cacheKey ?? null,
266
- prefixFingerprint: null,
267
- // Ladder key (spec §2.1 R0): raw lane = one row per call, seq 1, index 0.
268
- callId: p.callId,
269
- attemptSeq: 1,
270
- candidateIndex: 0,
271
- meta: p.meta,
272
- };
273
- telemetry.record(row);
274
- }
275
- /**
276
- * Read the ambient run context at the ENTRY of a raw call (spec §4) and, if
277
- * present, resolve the step key `input.stepKey ?? tags.step ?? provider/model#n`
278
- * (validated) — so `finalizeRaw` records a ground-truth step exactly like the
279
- * routed lane. Also runs the required-dimension hygiene warning (spec §4),
280
- * unconditionally, as the routed lane does. Fail-open: no context ⇒ null and no
281
- * step is ever recorded; the dispatch is byte-identical to pre-G1.
282
- */
283
- function prepareRawStep(deps, input) {
284
- const runCtx = (0, run_context_1.getActiveRunContext)();
285
- // Warn on the EFFECTIVE tags: an auto-stamped run_id (RC-1) is not missing.
286
- (0, manifest_1.warnMissingRequiredDimensions)(deps.manifest ?? null, (0, run_context_1.withRunIdTag)(input.tags ?? {}, runCtx));
287
- if (runCtx === null)
288
- return null;
289
- const rawModelId = `${input.provider}/${input.model}`;
290
- const stepKey = (0, run_context_1.resolveStepKey)(input.stepKey, input.tags?.step, () => runCtx.nextStepKey(rawModelId));
291
- return { runCtx, stepKey, startedAtIso: new Date().toISOString() };
292
- }
293
- /**
294
- * Turn an `AdapterResult` into a RunResult, recording exactly one telemetry row.
295
- * Shared by the buffered and streaming paths so cost/telemetry are byte-identical.
296
- * On a failure result: record the (possibly billed) row, then throw
297
- * `RigFailureError` — the server maps it to a status exactly like the slug lane.
298
- *
299
- * G1: when a run context was captured at entry (`step`), a ground-truth step is
300
- * emitted after the telemetry row on BOTH branches (success and failure) —
301
- * fail-open, so a buffer error there never affects the returned result. No
302
- * artifact CONTENT is written on the raw lane (spec F3): steps are metadata.
303
- */
304
- function finalizeRaw(deps, input, result, latencyMs, step) {
305
- const unpricedRatePerTok = (deps.unpricedRateUsdPerMTok ?? cost_1.DEFAULT_UNPRICED_RATE_USD_PER_MTOK) / 1000000;
306
- const rawModelId = `${input.provider}/${input.model}`;
307
- // One call_id for this raw invocation — shared by the (single) recorded row and
308
- // the returned meta (spec §2.1 R0).
309
- const callId = (0, node_crypto_1.randomUUID)();
310
- // CE-2 F2: the declared tier is reported truthfully on the row AND the meta.
311
- const requestedTier = input.serviceTier ?? "standard";
312
- // RC-1: `run_id` from the ambient run (caller's value wins); lane is SDK-owned
313
- // and cannot be overridden by a caller-supplied tag.
314
- // GS-1: `tags.step` from the ambient step key (step.stepKey = caller's stepKey
315
- // ?? tags.step ?? the auto provider/model#n key) when a run is active and the
316
- // caller set none — spread FIRST so a caller value wins, and omitted entirely
317
- // outside a run context so the row stays byte-identical (parity with RC-1).
318
- const stepTag = step !== null ? { step: step.stepKey } : {};
319
- const tags = { ...stepTag, ...(0, run_context_1.withRunIdTag)(input.tags ?? {}, step?.runCtx ?? null), lane: "raw" };
320
- // WI-1 (spec §2, §6.3 lane parity): the attempt envelope, built by the SAME
321
- // producer + rendered-request canonicalization the routed lane uses. The raw
322
- // lane never sends an output schema (unvalidated by design), so schema = null.
323
- const declaredToolNames = input.tools?.map((t) => t.function.name);
324
- const rawMeta = (usage) => (0, attempt_meta_1.summarizeAttemptMeta)({
325
- ...(result.meta !== undefined ? { adapterMeta: result.meta } : {}),
326
- renderedRequest: (0, attempt_meta_1.renderRequestForHash)({ system: input.systemPrompt, user: input.userPrompt, schema: null }),
327
- outputText: result.ok ? result.text : null,
328
- ...(declaredToolNames !== undefined ? { declaredToolNames } : {}),
329
- provider: input.provider,
330
- model: input.model,
331
- ...(usage !== undefined ? { usage } : {}),
332
- });
333
- // G1: mirror this raw inference into a ground-truth step (parity with the
334
- // routed lane's record-attempt). task = tags.step ?? provider/model (spec F1).
335
- // Fail-open is handled inside emitGroundTruthStep (buffer errors are swallowed).
336
- const emitStep = (inferenceId, failureClass) => {
337
- if (step === null)
338
- return;
339
- step.runCtx.emitGroundTruthStep({
340
- stepKey: step.stepKey,
341
- task: input.tags?.step ?? rawModelId,
342
- inferenceId,
343
- startedAt: step.startedAtIso,
344
- endedAt: new Date().toISOString(),
345
- failureClass,
346
- });
347
- };
348
- if (!result.ok) {
349
- let costUsd = 0;
350
- let pricingMissing = false;
351
- if (result.usage !== undefined) {
352
- const settled = (0, cost_1.attemptCost)(input.provider, input.model, result.usage, unpricedRatePerTok);
353
- costUsd = settled.costUsd;
354
- pricingMissing = settled.pricingMissing;
355
- }
356
- const failId = (0, node_crypto_1.randomUUID)();
357
- recordRawRow(deps.telemetry, {
358
- id: failId,
359
- rawModelId,
360
- provider: input.provider,
361
- model: input.model,
362
- ...(result.usage !== undefined ? { usage: result.usage } : {}),
363
- costUsd,
364
- latencyMs,
365
- requestedTier,
366
- failureClass: result.failure.class,
367
- tags,
368
- pricingMissing,
369
- callId,
370
- meta: rawMeta(result.usage),
371
- ...(input.cache !== undefined ? { cacheKey: input.cache.key } : {}),
372
- });
373
- emitStep(failId, result.failure.class);
374
- // R1: a raw-lane timeout is the #245 footgun — attach the tier-aware teaching
375
- // hint (the customer is looking at THIS error). Any other failure surfaces
376
- // unchanged. Overrides any adapter-set hint: the tier + budget is the fix.
377
- if (result.failure.class === "timeout") {
378
- throw new failures_1.RigFailureError({
379
- ...result.failure,
380
- fixHint: rawTimeoutFixHint(input, requestedTier),
381
- });
382
- }
383
- throw new failures_1.RigFailureError(result.failure);
384
- }
385
- const { costUsd, pricingMissing } = (0, cost_1.attemptCost)(input.provider, input.model, result.usage, unpricedRatePerTok);
386
- const id = (0, node_crypto_1.randomUUID)();
387
- recordRawRow(deps.telemetry, {
388
- id,
389
- rawModelId,
390
- provider: input.provider,
391
- model: input.model,
392
- usage: result.usage,
393
- costUsd,
394
- latencyMs,
395
- requestedTier,
396
- failureClass: null,
397
- tags,
398
- pricingMissing,
399
- callId,
400
- meta: rawMeta(result.usage),
401
- ...(input.cache !== undefined ? { cacheKey: input.cache.key } : {}),
402
- });
403
- emitStep(id, null);
404
- const toolCalls = result.toolCalls;
405
- const isToolTurn = toolCalls !== undefined && toolCalls.length > 0;
406
- const meta = {
407
- inferenceId: id,
408
- route: rawModelId,
409
- routeVersion: 0,
410
- provider: input.provider,
411
- model: input.model,
412
- requestedTier,
413
- servedTier: result.usage.servedTier,
414
- tokensIn: result.usage.tokensIn,
415
- tokensOut: result.usage.tokensOut,
416
- tokensCached: result.usage.tokensCached,
417
- costEstimateUsd: costUsd,
418
- latencyMs,
419
- validated: false, // raw is NEVER validated — the central product claim
420
- attemptsByClass: {},
421
- servedVariant: null,
422
- repairedBy: null,
423
- callId,
424
- ...(isToolTurn ? { toolCalls } : {}),
425
- };
426
- return { output: isToolTurn ? null : result.text, meta };
427
- }
428
107
  /** Buffered raw dispatch (spec §4.2). Records telemetry, returns the RunResult,
429
108
  * or throws `RigFailureError` on an adapter failure. */
430
109
  async function runRaw(deps, input) {
431
110
  // CE-2 F1/F3: fail closed on an unsupported declared directive BEFORE building
432
111
  // an adapter — no meter, no provider call, no telemetry row.
433
- assertRawKnobsSupported(input);
112
+ (0, raw_knobs_1.assertRawKnobsSupported)(input);
434
113
  // G1: bind the ambient run context at ENTRY (ALS boundary) so a raw call inside
435
114
  // a run.start records a ground-truth step (spec §4). Null ⇒ uninstrumented.
436
- const step = prepareRawStep(deps, input);
115
+ const step = (0, raw_finalize_1.prepareRawStep)(deps, input);
437
116
  const adapter = buildRawAdapter(input.provider, input.apiKey);
438
117
  const candidate = (0, resolve_1.mintRawCandidate)(input.provider, input.model);
439
118
  const started = Date.now();
440
- const result = await adapter.call(buildRawRequest(candidate, input));
441
- return finalizeRaw(deps, input, result, Date.now() - started, step);
119
+ const result = await adapter.call((0, raw_request_1.buildRawRequest)(candidate, input));
120
+ return (0, raw_finalize_1.finalizeRaw)(deps, input, result, Date.now() - started, step);
442
121
  }
443
122
  function streamEndedFailure(input) {
444
123
  return {
@@ -463,7 +142,7 @@ function runRawStream(deps, input) {
463
142
  // G1: read the ambient run context HERE — at the synchronous entry, the ALS
464
143
  // boundary — NOT inside the async generator below (which defers to drain time,
465
144
  // possibly on a different async context, where getActiveRunContext would miss).
466
- const step = prepareRawStep(deps, input);
145
+ const step = (0, raw_finalize_1.prepareRawStep)(deps, input);
467
146
  let settle;
468
147
  let fail;
469
148
  const result = new Promise((res, rej) => {
@@ -478,10 +157,10 @@ function runRawStream(deps, input) {
478
157
  // CE-2 F1/F3: fail closed on an unsupported declared directive BEFORE any
479
158
  // adapter/dispatch — surfaced through the handle (result rejects, the
480
159
  // events iterator throws), so no meter, no provider call, no telemetry row.
481
- assertRawKnobsSupported(input);
160
+ (0, raw_knobs_1.assertRawKnobsSupported)(input);
482
161
  const adapter = buildRawAdapter(input.provider, input.apiKey);
483
162
  const candidate = (0, resolve_1.mintRawCandidate)(input.provider, input.model);
484
- const req = buildRawRequest(candidate, input);
163
+ const req = (0, raw_request_1.buildRawRequest)(candidate, input);
485
164
  const started = Date.now();
486
165
  let adapterResult;
487
166
  if (adapter.stream !== undefined) {
@@ -500,7 +179,7 @@ function runRawStream(deps, input) {
500
179
  else {
501
180
  adapterResult = await adapter.call(req);
502
181
  }
503
- const runResult = finalizeRaw(deps, input, adapterResult, Date.now() - started, step);
182
+ const runResult = (0, raw_finalize_1.finalizeRaw)(deps, input, adapterResult, Date.now() - started, step);
504
183
  settle(runResult);
505
184
  yield { type: "final", result: runResult };
506
185
  }
@@ -10,6 +10,7 @@
10
10
  * in-flight E4 / capture-write work (plan coordination note).
11
11
  */
12
12
  import type { AttemptTelemetry } from "./dispatch";
13
+ import type { Destination } from "./destination";
13
14
  import type { ActiveRunContext } from "../artifacts/run-context";
14
15
  import type { RetryLedger } from "./retry";
15
16
  import type { RouteBundle, RunOptions, RunTags } from "../types/route";
@@ -42,6 +43,13 @@ export interface RecordAttemptDeps {
42
43
  * `meta.arm` so the A/B readout groups `inferences` by arm. Absent ⇒ no `arm`
43
44
  * key on `meta` (byte-identical to a non-experiment run). */
44
45
  readonly arm?: string;
46
+ /** Unified Route U3 (ruling D-2) — the run's RESOLVED serving destination. The
47
+ * row's `tagsJson` stamps `destination` through the shared `destinationTag`
48
+ * helper ONLY for a NON-default value: `direct` (the default, absent ⇒
49
+ * byte-identical) adds NO tag, so every `direct` routed row stays byte-identical
50
+ * to pre-U2; a `modelrig-served` row carries `destination:"modelrig-served"`
51
+ * (legible managed-served attribution). Absent ⇒ `direct`. */
52
+ readonly destination?: Destination;
45
53
  }
46
54
  /**
47
55
  * Classify a recorded row's ladder rung (R1 review, F1). `primaryDispatched`
@@ -1 +1 @@
1
- {"version":3,"file":"record-attempt.d.ts","sourceRoot":"","sources":["../../src/run/record-attempt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACvE,OAAO,KAAK,EAAgB,aAAa,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAGtF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAGpD,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,SAAS,EAAE,cAAc,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B;;;gDAG4C;IAC5C,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IACtC,kFAAkF;IAClF,QAAQ,CAAC,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IACzC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC;;qFAEiF;IACjF,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,CA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