@konneal/engine 0.2.101 → 0.2.103

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.
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  handleAsk
3
- } from "./chunk-DMTRTW2Q.js";
4
- import "./chunk-QNLFNEI4.js";
5
- import "./chunk-Z3R34T7Q.js";
3
+ } from "./chunk-NCZHN6QW.js";
4
+ import "./chunk-ANUHRJ4Z.js";
5
+ import "./chunk-25ECO5M7.js";
6
6
  import "./chunk-KNCRYAX3.js";
7
7
  import "./chunk-DTW4UW2E.js";
8
8
  import "./chunk-A3QHHUN5.js";
@@ -22318,27 +22318,27 @@ async function runStages(stages, c) {
22318
22318
  var dense = {
22319
22319
  name: "dense",
22320
22320
  run: async (c) => {
22321
- const { env, filter, filters, vector, opts, rq, folded } = c;
22321
+ const { env, filters, vector, opts, rq, folded } = c;
22322
22322
  const q = { topK: LIMITS.retrieveK, returnMetadata: "all" };
22323
- if (filter) q.filter = filter;
22324
22323
  const optimistic = opts.optimisticHits ?? [];
22325
22324
  const sameLane = rq === folded;
22326
- if (!filter && sameLane && optimistic.length) {
22325
+ if (!filters?.doc_number && sameLane && optimistic.length) {
22327
22326
  c.matches = optimistic.map((h) => ({ id: h.id, score: h.score, metadata: h.metadata }));
22328
22327
  console.log("optimistic lane: reused", c.matches.length, "dense hits (no re-query)");
22329
22328
  return;
22330
22329
  }
22331
- if (filter) {
22332
- let matches = (await env.VECTORIZE.query(vector, q)).matches ?? [];
22330
+ if (filters?.doc_number) {
22331
+ const allMatches = (await env.VECTORIZE.query(vector, q)).matches ?? [];
22332
+ let matches = allMatches;
22333
+ const before = matches.length;
22334
+ matches = matches.filter((m) => String(m.metadata?.doc_number ?? "") === filters.doc_number);
22335
+ if (filters.edition) matches = matches.filter((m) => !m.metadata?.edition || String(m.metadata.edition) === filters.edition);
22336
+ console.log("dense scope (in code):", before, "\u2192", matches.length, "in doc", filters.doc_number, filters.edition ? `@${filters.edition}` : "");
22333
22337
  if (filters && filters.edition && matches.length < 3) {
22334
- const docOnly = await env.VECTORIZE.query(vector, {
22335
- topK: LIMITS.retrieveK,
22336
- returnMetadata: "all",
22337
- filter: toVectorizeFilter({ doc_number: filters.doc_number })
22338
- });
22339
- if ((docOnly.matches ?? []).length > matches.length) {
22340
- console.log("edition pin dropped:", filters.doc_number, "@", filters.edition, "\u2192", docOnly.matches?.length ?? 0, "doc-scoped hits (edition not in corpus)");
22341
- matches = docOnly.matches ?? [];
22338
+ const docOnly = allMatches.filter((m) => String(m.metadata?.doc_number ?? "") === filters.doc_number);
22339
+ if (docOnly.length > matches.length) {
22340
+ console.log("edition pin dropped:", filters.doc_number, "@", filters.edition, "\u2192", docOnly.length, "doc-scoped hits (edition not in corpus)");
22341
+ matches = docOnly;
22342
22342
  filters.edition = void 0;
22343
22343
  }
22344
22344
  }
@@ -22580,17 +22580,40 @@ var licensedLane = {
22580
22580
  const matches = await portIndex(c.env, "public").getByIds(ids);
22581
22581
  const seenIds = new Set(c.matches.map((m) => m.id));
22582
22582
  let merged = 0;
22583
+ const mergedIds = [];
22583
22584
  for (const m of matches) {
22584
22585
  if (m.metadata && !seenIds.has(m.id)) {
22585
22586
  c.matches.push({ id: m.id, score: THRESHOLDS.licensedLaneScore, metadata: m.metadata });
22586
22587
  seenIds.add(m.id);
22587
22588
  merged++;
22589
+ mergedIds.push(m.id);
22588
22590
  }
22589
22591
  }
22592
+ c.lane["licensed-ids"] = Promise.resolve(mergedIds);
22590
22593
  console.log("licensed lane:", ids.length, "units,", matches.length, "vectors,", merged, "merged");
22591
22594
  }
22592
22595
  };
22593
22596
 
22597
+ // workers/worker_public/src/stages/licensedCover.ts
22598
+ var licensedCover = {
22599
+ name: "licensed-cover",
22600
+ failure: "additive",
22601
+ when: (c) => !!c.opts.licensedDocNumbers?.length && c.finalHits.length > 0,
22602
+ run: async (c) => {
22603
+ const keys = new Set(c.opts.licensedDocNumbers);
22604
+ const present = c.finalHits.some((h) => keys.has(String(h.metadata.standard ?? "")));
22605
+ if (present) return;
22606
+ const ids = (await c.lane["licensed-ids"] ?? []).slice(0, 8);
22607
+ if (!ids.length) return;
22608
+ const matches = await portIndex(c.env, "public").getByIds(ids);
22609
+ const unit = matches.find((m) => m.metadata && keys.has(String(m.metadata.standard ?? "")));
22610
+ if (!unit) return;
22611
+ const hit = toHits([unit])[0];
22612
+ c.finalHits = [...c.finalHits, hit];
22613
+ console.log("licensed cover: seated", unit.id, "\u2014", unit.metadata.docidentifier);
22614
+ }
22615
+ };
22616
+
22594
22617
  // workers/worker_public/src/stages/multiQuery.ts
22595
22618
  var RRF_K = 60;
22596
22619
  var multiQuery = {
@@ -22798,12 +22821,10 @@ var editionCover = {
22798
22821
  let added = 0;
22799
22822
  for (const w of want.slice(0, maxDocs)) {
22800
22823
  try {
22801
- const q = await c.env.VECTORIZE.query(c.vector, {
22802
- topK: 3,
22803
- returnMetadata: "all",
22804
- filter: { $and: [{ docidentifier: { $eq: w.di } }, { edition: { $eq: w.edition } }] }
22805
- });
22806
- const hits = toHits(q.matches ?? []).map((h) => ({ ...h, score: top * THRESHOLDS.editionCoverDiscount }));
22824
+ const rows2 = await c.env.DB.prepare(
22825
+ "SELECT id, doc_id, docidentifier, doctype, doc_number, edition, language, clause_anchor, clause_title, status, superseded_by, corpus, tier, text FROM chunks WHERE docidentifier = ?1 AND edition = ?2 LIMIT 3"
22826
+ ).bind(w.di, w.edition).all();
22827
+ const hits = rowsToHits(rows2.results ?? []).map((h) => ({ ...h, score: top * THRESHOLDS.editionCoverDiscount }));
22807
22828
  c.hits.push(...hits);
22808
22829
  added += hits.length;
22809
22830
  console.log("edition cover:", w.di, w.edition, `+${hits.length}`);
@@ -23223,6 +23244,7 @@ var STAGES = [
23223
23244
  editionSteer,
23224
23245
  propagate,
23225
23246
  diversity,
23247
+ licensedCover,
23226
23248
  typedPin,
23227
23249
  sectionDescent,
23228
23250
  dedup,
@@ -80,170 +80,6 @@ function tableRetyped(text, availableTable) {
80
80
  return /(^|\n)\s*\|[^\n]+\|\s*(\n\s*\|[-: |]+\|\s*)?(\n|$)/.test(text) && (text.match(/\|/g) ?? []).length >= 6;
81
81
  }
82
82
 
83
- // workers/worker_public/src/modelplane.ts
84
- var NODE_RE = /(?:^|[\s("'`])\/(req|conf|term|constraint|characteristic|state-machine|dimension)\/([a-z0-9][a-z0-9_-]*(?:\/[a-z0-9][a-z0-9_-]*)?)(?=[\s)"'`,;:.]|$)/i;
85
- function modelNodeRefIn(text) {
86
- if (!text) return null;
87
- const m = text.match(NODE_RE);
88
- if (!m) return null;
89
- const id = `/${m[1].toLowerCase()}/${m[2]}`;
90
- return id.length <= 120 ? id : null;
91
- }
92
- function standardForDocNumber(docNumber) {
93
- if (!docNumber) return null;
94
- const models = P().sources?.models;
95
- if (!models?.standards?.length || !models?.standard_prefix) return null;
96
- return models.standards.includes(docNumber) ? `${models.standard_prefix}${docNumber}` : null;
97
- }
98
- function licensedEntryForPackage(packageId) {
99
- if (!packageId) return null;
100
- return (P().sources?.licensed ?? []).find((l) => l.package === packageId) ?? null;
101
- }
102
- function licensedEntryForDocNumber(docNumber) {
103
- if (!docNumber) return null;
104
- return (P().sources?.licensed ?? []).find((l) => String(l.doc_number ?? "") === docNumber) ?? null;
105
- }
106
- function licenseBoundaryNote(docNumber, standardKeys) {
107
- const entry = licensedEntryForDocNumber(docNumber);
108
- if (!entry || standardKeys && standardKeys.has(entry.key)) return void 0;
109
- const pointer = P().prompts?.vars?.license_declare_pointer;
110
- return `License boundary \u2014 the question is about ${licenseBoundaryName(entry)}, a licensed publication (entitlement key ${entry.key}). The caller's organization license does not cover its text, so no passage of it was retrieved and NONE of its procedural content (steps, parameters, severities, limits) may be stated, paraphrased or recalled from memory. You MAY answer at the citation level: name the standard and edition, and cite the invoking clause from the PUBLIC passages in context (the Recommendation's own applicability and normative references are public and stay answerable). Then say the organization's license does not cover the standard's text` + (pointer ? ` and point to the declare flow: ${pointer}.` : ".");
111
- }
112
- function licenseBoundaryName(entry) {
113
- const id = entry.doc_number ? ` ${entry.doc_number}` : ` ${entry.package}`;
114
- return `${entry.title ?? "standard"}${entry.edition ? ` (${entry.edition})` : ""} \u2014${id}`;
115
- }
116
- function licenseBoundaryRefusal(docNumber, standardKeys) {
117
- const entry = licensedEntryForDocNumber(docNumber);
118
- if (!entry || standardKeys && standardKeys.has(entry.key)) return void 0;
119
- const pointer = P().prompts?.vars?.license_declare_pointer;
120
- return `${licenseBoundaryName(entry)} is a licensed publication and your organization's license does not cover its text, so I can't quote or summarize its procedure. I can answer at the citation level \u2014 the standard's title and edition, and the clause your Recommendation invokes \u2014 and the public ${P().publisher.name} content in full.` + (pointer ? ` To unlock the full text, an org admin can declare the license under ${pointer}.` : "");
121
- }
122
- async function fetchNode(env, standard, nodeId) {
123
- try {
124
- const row = await env.DB.prepare(
125
- "SELECT standard, node_id, kind, name, clause_doc, clause_ref, content FROM model_nodes WHERE standard = ?1 AND node_id = ?2"
126
- ).bind(standard, nodeId).first();
127
- if (!row) return null;
128
- const content = JSON.parse(String(row.content));
129
- const clause = row.clause_doc && row.clause_ref ? { doc: String(row.clause_doc), ref: String(row.clause_ref), urn: `${row.clause_doc}#clause-${row.clause_ref}` } : row.clause_doc ? { doc: String(row.clause_doc), ref: "", urn: String(row.clause_doc) } : null;
130
- return {
131
- standard: String(row.standard),
132
- node_id: String(row.node_id),
133
- kind: String(row.kind),
134
- name: String(row.name ?? row.node_id),
135
- clause,
136
- content
137
- };
138
- } catch {
139
- return null;
140
- }
141
- }
142
- async function bindModelNode(env, opts) {
143
- const nodeId = modelNodeRefIn(opts.label) ?? modelNodeRefIn(opts.query);
144
- if (!nodeId) return null;
145
- const gate = (node) => {
146
- if (!node) return null;
147
- const entry = licensedEntryForPackage(node.standard);
148
- return entry && !(opts.standardKeys?.has(entry.key) ?? false) ? { ...node, gated: true, content: {} } : node;
149
- };
150
- if (opts.standard) return gate(await fetchNode(env, opts.standard, nodeId));
151
- try {
152
- const rows = await env.DB.prepare(
153
- "SELECT n.standard AS standard FROM model_nodes n LEFT JOIN model_plane_meta m ON m.standard = n.standard WHERE n.node_id = ?1 ORDER BY m.indexed_at DESC, n.standard DESC LIMIT 1"
154
- ).bind(nodeId).all();
155
- const standard = (rows?.results ?? []).map((r) => String(r.standard))[0];
156
- if (standard) return gate(await fetchNode(env, standard, nodeId));
157
- return null;
158
- } catch {
159
- return null;
160
- }
161
- }
162
- function clip(s, n = 500) {
163
- const t = String(s ?? "").trim();
164
- return t.length <= n ? t : t.slice(0, n - 1).trimEnd() + " \u2026";
165
- }
166
- function applicabilityText(app) {
167
- if (!app || typeof app !== "object") return "";
168
- const parts = [];
169
- for (const [dim, cond] of Object.entries(app)) {
170
- if (Array.isArray(cond)) parts.push(`${dim.replace(/_/g, " ")}: ${cond.join(", ")}`);
171
- else if (cond && typeof cond === "object" && Array.isArray(cond.values)) {
172
- parts.push(`${dim.replace(/_/g, " ")} (${cond.match ?? "any"}): ${cond.values.join(", ")}`);
173
- }
174
- }
175
- return parts.join("; ");
176
- }
177
- function modelGroundingBlock(node) {
178
- const c = node.content ?? {};
179
- const lines = [];
180
- lines.push(
181
- P().prompts.vars.model_grounding_intro ?? "Model grounding \u2014 the model plane's own statement:"
182
- );
183
- lines.push(`Node: ${node.node_id} (${node.kind.replace(/_/g, " ")}) \u2014 ${node.name} [${node.standard}]`);
184
- if (node.clause) lines.push(`Provenance: ${node.clause.urn}`);
185
- if (c.statement) lines.push(`Statement: ${clip(c.statement)}`);
186
- if (c.definition) lines.push(`Definition: ${clip(c.definition)}`);
187
- if (c.purpose) lines.push(`Purpose: ${clip(c.purpose)}`);
188
- const limit = c.limit ?? {};
189
- if (limit.expression) lines.push(`Machine limit (the constraint the platform's verdict engine evaluates \u2014 quote it verbatim): ${limit.expression}`);
190
- if (limit.accepts?.verdict) lines.push(`Machine limit: ${limit.accepts.verdict} ${limit.accepts.op} ${limit.accepts.limit} (the canonical acceptance chain)`);
191
- if (c.check) lines.push(`Machine check: ${c.check}`);
192
- if (c.derive) lines.push(`Derivation: ${c.derive}${Array.isArray(c.inputs) ? ` (inputs: ${c.inputs.join(", ")})` : ""}`);
193
- const app = applicabilityText(c.applicability);
194
- const scopeApp = applicabilityText(c.scope_applicability);
195
- if (app || scopeApp) lines.push(`Applicability: ${[scopeApp, app].filter(Boolean).join("; ")}`);
196
- if (Array.isArray(c.binds_to) && c.binds_to.length) lines.push(`Binds to: ${c.binds_to.join(", ")}`);
197
- if (Array.isArray(c.targets) && c.targets.length) lines.push(`Verifies requirements: ${c.targets.join(", ")}`);
198
- if (Array.isArray(c.preconditions) && c.preconditions.length) {
199
- const pcs = c.preconditions.map((p) => `${p.id}: ${clip(p.check ?? (p.state ? `state = ${p.state}` : ""), 120)}`).join("; ");
200
- lines.push(`Run-validity preconditions (a violation voids the run \u2014 invalid, never a fail): ${pcs}`);
201
- }
202
- if (c.acceptance_criteria?.description) lines.push(`Acceptance: ${clip(c.acceptance_criteria.description, 300)}`);
203
- if (c.violation_meaning) lines.push(`Violation meaning (verbatim): ${clip(c.violation_meaning, 300)} \u2014 on violation: ${c.on_violation ?? "invalid"}`);
204
- if (Array.isArray(c.values) && c.values.length) {
205
- lines.push(`Values: ${c.values.map((v) => `${v.id}${v.implies?.length ? ` (implies ${v.implies.join(", ")})` : ""}`).join("; ")}`);
206
- }
207
- if (c.source_discrepancy) {
208
- const sd = c.source_discrepancy;
209
- lines.push(
210
- `DECLARED SOURCE DISCREPANCY \u2014 the model and the text disagree; you MUST surface this and cite both: ${clip(sd.summary, 300)} Sources: ${(sd.sources ?? []).join(" and ")}. The model ${sd.resolution === "follows_clause_x" ? "follows one side" : "records the conflict without resolving it"}: ${clip(sd.rationale, 300)}`
211
- );
212
- }
213
- lines.push(
214
- "Rules for this answer: the machine facts (the constraint, the applicability, the acceptance, the provenance) come from THIS node \u2014 quote the machine limit verbatim, never invent one the node does not carry. If this model content and a prose passage disagree \u2014 including a passage from a different edition \u2014 say so explicitly and cite both (this node and the prose clause)."
215
- );
216
- return lines.join("\n");
217
- }
218
- function modelCitation(node) {
219
- return {
220
- doc_id: `model:${node.standard}`,
221
- docidentifier: `${P().publisher.name} SMART model (${(() => {
222
- const prefix = P().sources?.models?.standard_prefix ?? "";
223
- const letter = prefix.replace(/^.*-/, "").toUpperCase();
224
- return String(node.standard).replace(new RegExp(`^${prefix}`, "i"), `${letter} `);
225
- })()})`,
226
- edition: "",
227
- language: "en",
228
- clause_anchor: node.clause?.ref || "model",
229
- clause_title: `${node.kind.replace(/_/g, " ")} \u2014 ${node.name} (${node.node_id})`,
230
- status: "in-force",
231
- corpus: "smart-model",
232
- quality: "verified",
233
- url: void 0,
234
- snippet: `${node.node_id}${node.clause ? ` \xB7 ${node.clause.urn}` : ""}${node.content?.statement ? ` \u2014 ${clip(node.content.statement, 240)}` : ""}`,
235
- score: 1
236
- };
237
- }
238
- function modelEcho(node) {
239
- return {
240
- node_id: node.node_id.slice(0, 120),
241
- kind: node.kind.slice(0, 40),
242
- standard: node.standard.slice(0, 40),
243
- ...node.clause?.urn ? { clause: node.clause.urn.slice(0, 120) } : {}
244
- };
245
- }
246
-
247
83
  // workers/worker_public/src/verdict.ts
248
84
  function tokenize(src) {
249
85
  const toks = [];
@@ -501,6 +337,205 @@ function verdictNote(v, node) {
501
337
  return lines.join("\n");
502
338
  }
503
339
 
340
+ // workers/worker_public/src/modelplane.ts
341
+ var NODE_RE = /(?:^|[\s("'`])\/(req|conf|term|constraint|characteristic|state-machine|dimension)\/([a-z0-9][a-z0-9_-]*(?:\/[a-z0-9][a-z0-9_-]*)?)(?=[\s)"'`,;:.]|$)/i;
342
+ function modelNodeRefIn(text) {
343
+ if (!text) return null;
344
+ const m = text.match(NODE_RE);
345
+ if (!m) return null;
346
+ const id = `/${m[1].toLowerCase()}/${m[2]}`;
347
+ return id.length <= 120 ? id : null;
348
+ }
349
+ function standardForDocNumber(docNumber) {
350
+ if (!docNumber) return null;
351
+ const models = P().sources?.models;
352
+ if (!models?.standards?.length || !models?.standard_prefix) return null;
353
+ return models.standards.includes(docNumber) ? `${models.standard_prefix}${docNumber}` : null;
354
+ }
355
+ function licensedEntryForPackage(packageId) {
356
+ if (!packageId) return null;
357
+ return (P().sources?.licensed ?? []).find((l) => l.package === packageId) ?? null;
358
+ }
359
+ function licensedEntryForDocNumber(docNumber) {
360
+ if (!docNumber) return null;
361
+ return (P().sources?.licensed ?? []).find((l) => String(l.doc_number ?? "") === docNumber) ?? null;
362
+ }
363
+ function licenseBoundaryNote(docNumber, standardKeys) {
364
+ const entry = licensedEntryForDocNumber(docNumber);
365
+ if (!entry || standardKeys && standardKeys.has(entry.key)) return void 0;
366
+ const pointer = P().prompts?.vars?.license_declare_pointer;
367
+ return `License boundary \u2014 the question is about ${licenseBoundaryName(entry)}, a licensed publication (entitlement key ${entry.key}). The caller's organization license does not cover its text, so no passage of it was retrieved and NONE of its procedural content (steps, parameters, severities, limits) may be stated, paraphrased or recalled from memory. You MAY answer at the citation level: name the standard and edition, and cite the invoking clause from the PUBLIC passages in context (the Recommendation's own applicability and normative references are public and stay answerable). Then say the organization's license does not cover the standard's text` + (pointer ? ` and point to the declare flow: ${pointer}.` : ".");
368
+ }
369
+ function licenseBoundaryName(entry) {
370
+ const id = entry.doc_number ? ` ${entry.doc_number}` : ` ${entry.package}`;
371
+ return `${entry.title ?? "standard"}${entry.edition ? ` (${entry.edition})` : ""} \u2014${id}`;
372
+ }
373
+ function licenseBoundaryRefusal(docNumber, standardKeys) {
374
+ const entry = licensedEntryForDocNumber(docNumber);
375
+ if (!entry || standardKeys && standardKeys.has(entry.key)) return void 0;
376
+ const pointer = P().prompts?.vars?.license_declare_pointer;
377
+ return `${licenseBoundaryName(entry)} is a licensed publication and your organization's license does not cover its text, so I can't quote or summarize its procedure. I can answer at the citation level \u2014 the standard's title and edition, and the clause your Recommendation invokes \u2014 and the public ${P().publisher.name} content in full.` + (pointer ? ` To unlock the full text, an org admin can declare the license under ${pointer}.` : "");
378
+ }
379
+ async function fetchNode(env, standard, nodeId) {
380
+ try {
381
+ const row = await env.DB.prepare(
382
+ "SELECT standard, node_id, kind, name, clause_doc, clause_ref, content FROM model_nodes WHERE standard = ?1 AND node_id = ?2"
383
+ ).bind(standard, nodeId).first();
384
+ if (!row) return null;
385
+ const content = JSON.parse(String(row.content));
386
+ const clause = row.clause_doc && row.clause_ref ? { doc: String(row.clause_doc), ref: String(row.clause_ref), urn: `${row.clause_doc}#clause-${row.clause_ref}` } : row.clause_doc ? { doc: String(row.clause_doc), ref: "", urn: String(row.clause_doc) } : null;
387
+ return {
388
+ standard: String(row.standard),
389
+ node_id: String(row.node_id),
390
+ kind: String(row.kind),
391
+ name: String(row.name ?? row.node_id),
392
+ clause,
393
+ content
394
+ };
395
+ } catch {
396
+ return null;
397
+ }
398
+ }
399
+ async function bindModelNode(env, opts) {
400
+ const nodeId = modelNodeRefIn(opts.label) ?? modelNodeRefIn(opts.query);
401
+ if (!nodeId) return null;
402
+ const gate = (node) => {
403
+ if (!node) return null;
404
+ const entry = licensedEntryForPackage(node.standard);
405
+ return entry && !(opts.standardKeys?.has(entry.key) ?? false) ? { ...node, gated: true, content: {} } : node;
406
+ };
407
+ if (opts.standard) return gate(await fetchNode(env, opts.standard, nodeId));
408
+ try {
409
+ const rows = await env.DB.prepare(
410
+ "SELECT n.standard AS standard FROM model_nodes n LEFT JOIN model_plane_meta m ON m.standard = n.standard WHERE n.node_id = ?1 ORDER BY m.indexed_at DESC, n.standard DESC LIMIT 1"
411
+ ).bind(nodeId).all();
412
+ const standard = (rows?.results ?? []).map((r) => String(r.standard))[0];
413
+ if (standard) return gate(await fetchNode(env, standard, nodeId));
414
+ return null;
415
+ } catch {
416
+ return null;
417
+ }
418
+ }
419
+ async function bindConstraintByQuantities(env, query, standardKeys) {
420
+ const verdictish = /\b(acceptable|valid|violate|violation|within|comply|proposal|severity|conform)\b/i.test(query);
421
+ if (!verdictish) return null;
422
+ try {
423
+ const rows = await env.DB.prepare(
424
+ "SELECT standard, node_id, kind, name, clause_doc, clause_ref, content FROM model_nodes WHERE kind = 'constraint' LIMIT 120"
425
+ ).all();
426
+ const q = query.toLowerCase();
427
+ let best = null;
428
+ for (const r of rows.results ?? []) {
429
+ const checks = extractChecks(JSON.parse(String(r.content ?? "{}")));
430
+ if (!checks.length) continue;
431
+ const leaves = new Set(
432
+ symbolsIn(checks).map((sym) => sym.split(".").pop() ?? sym).filter((leaf) => leaf.length >= 2)
433
+ );
434
+ let matched = 0;
435
+ for (const leaf of leaves) {
436
+ const re = new RegExp(`(?<![a-z_])${leaf.replace(/[^a-z0-9_]/g, "")}(?![a-z_])`, "i");
437
+ if (re.test(q)) matched++;
438
+ }
439
+ if (matched >= 2 && (!best || matched > best.score)) {
440
+ best = { score: matched, standard: String(r.standard), node_id: String(r.node_id) };
441
+ }
442
+ }
443
+ if (!best) return null;
444
+ const gate = (node) => {
445
+ if (!node) return null;
446
+ const entry = licensedEntryForPackage(node.standard);
447
+ return entry && !(standardKeys?.has(entry.key) ?? false) ? { ...node, gated: true, content: {} } : node;
448
+ };
449
+ return gate(await fetchNode(env, best.standard, best.node_id));
450
+ } catch {
451
+ return null;
452
+ }
453
+ }
454
+ function clip(s, n = 500) {
455
+ const t = String(s ?? "").trim();
456
+ return t.length <= n ? t : t.slice(0, n - 1).trimEnd() + " \u2026";
457
+ }
458
+ function applicabilityText(app) {
459
+ if (!app || typeof app !== "object") return "";
460
+ const parts = [];
461
+ for (const [dim, cond] of Object.entries(app)) {
462
+ if (Array.isArray(cond)) parts.push(`${dim.replace(/_/g, " ")}: ${cond.join(", ")}`);
463
+ else if (cond && typeof cond === "object" && Array.isArray(cond.values)) {
464
+ parts.push(`${dim.replace(/_/g, " ")} (${cond.match ?? "any"}): ${cond.values.join(", ")}`);
465
+ }
466
+ }
467
+ return parts.join("; ");
468
+ }
469
+ function modelGroundingBlock(node) {
470
+ const c = node.content ?? {};
471
+ const lines = [];
472
+ lines.push(
473
+ P().prompts.vars.model_grounding_intro ?? "Model grounding \u2014 the model plane's own statement:"
474
+ );
475
+ lines.push(`Node: ${node.node_id} (${node.kind.replace(/_/g, " ")}) \u2014 ${node.name} [${node.standard}]`);
476
+ if (node.clause) lines.push(`Provenance: ${node.clause.urn}`);
477
+ if (c.statement) lines.push(`Statement: ${clip(c.statement)}`);
478
+ if (c.definition) lines.push(`Definition: ${clip(c.definition)}`);
479
+ if (c.purpose) lines.push(`Purpose: ${clip(c.purpose)}`);
480
+ const limit = c.limit ?? {};
481
+ if (limit.expression) lines.push(`Machine limit (the constraint the platform's verdict engine evaluates \u2014 quote it verbatim): ${limit.expression}`);
482
+ if (limit.accepts?.verdict) lines.push(`Machine limit: ${limit.accepts.verdict} ${limit.accepts.op} ${limit.accepts.limit} (the canonical acceptance chain)`);
483
+ if (c.check) lines.push(`Machine check: ${c.check}`);
484
+ if (c.derive) lines.push(`Derivation: ${c.derive}${Array.isArray(c.inputs) ? ` (inputs: ${c.inputs.join(", ")})` : ""}`);
485
+ const app = applicabilityText(c.applicability);
486
+ const scopeApp = applicabilityText(c.scope_applicability);
487
+ if (app || scopeApp) lines.push(`Applicability: ${[scopeApp, app].filter(Boolean).join("; ")}`);
488
+ if (Array.isArray(c.binds_to) && c.binds_to.length) lines.push(`Binds to: ${c.binds_to.join(", ")}`);
489
+ if (Array.isArray(c.targets) && c.targets.length) lines.push(`Verifies requirements: ${c.targets.join(", ")}`);
490
+ if (Array.isArray(c.preconditions) && c.preconditions.length) {
491
+ const pcs = c.preconditions.map((p) => `${p.id}: ${clip(p.check ?? (p.state ? `state = ${p.state}` : ""), 120)}`).join("; ");
492
+ lines.push(`Run-validity preconditions (a violation voids the run \u2014 invalid, never a fail): ${pcs}`);
493
+ }
494
+ if (c.acceptance_criteria?.description) lines.push(`Acceptance: ${clip(c.acceptance_criteria.description, 300)}`);
495
+ if (c.violation_meaning) lines.push(`Violation meaning (verbatim): ${clip(c.violation_meaning, 300)} \u2014 on violation: ${c.on_violation ?? "invalid"}`);
496
+ if (Array.isArray(c.values) && c.values.length) {
497
+ lines.push(`Values: ${c.values.map((v) => `${v.id}${v.implies?.length ? ` (implies ${v.implies.join(", ")})` : ""}`).join("; ")}`);
498
+ }
499
+ if (c.source_discrepancy) {
500
+ const sd = c.source_discrepancy;
501
+ lines.push(
502
+ `DECLARED SOURCE DISCREPANCY \u2014 the model and the text disagree; you MUST surface this and cite both: ${clip(sd.summary, 300)} Sources: ${(sd.sources ?? []).join(" and ")}. The model ${sd.resolution === "follows_clause_x" ? "follows one side" : "records the conflict without resolving it"}: ${clip(sd.rationale, 300)}`
503
+ );
504
+ }
505
+ lines.push(
506
+ "Rules for this answer: the machine facts (the constraint, the applicability, the acceptance, the provenance) come from THIS node \u2014 quote the machine limit verbatim, never invent one the node does not carry. If this model content and a prose passage disagree \u2014 including a passage from a different edition \u2014 say so explicitly and cite both (this node and the prose clause)."
507
+ );
508
+ return lines.join("\n");
509
+ }
510
+ function modelCitation(node) {
511
+ return {
512
+ doc_id: `model:${node.standard}`,
513
+ docidentifier: `${P().publisher.name} SMART model (${(() => {
514
+ const prefix = P().sources?.models?.standard_prefix ?? "";
515
+ const letter = prefix.replace(/^.*-/, "").toUpperCase();
516
+ return String(node.standard).replace(new RegExp(`^${prefix}`, "i"), `${letter} `);
517
+ })()})`,
518
+ edition: "",
519
+ language: "en",
520
+ clause_anchor: node.clause?.ref || "model",
521
+ clause_title: `${node.kind.replace(/_/g, " ")} \u2014 ${node.name} (${node.node_id})`,
522
+ status: "in-force",
523
+ corpus: "smart-model",
524
+ quality: "verified",
525
+ url: void 0,
526
+ snippet: `${node.node_id}${node.clause ? ` \xB7 ${node.clause.urn}` : ""}${node.content?.statement ? ` \u2014 ${clip(node.content.statement, 240)}` : ""}`,
527
+ score: 1
528
+ };
529
+ }
530
+ function modelEcho(node) {
531
+ return {
532
+ node_id: node.node_id.slice(0, 120),
533
+ kind: node.kind.slice(0, 40),
534
+ standard: node.standard.slice(0, 40),
535
+ ...node.clause?.urn ? { clause: node.clause.urn.slice(0, 120) } : {}
536
+ };
537
+ }
538
+
504
539
  // workers/worker_public/src/conditions.ts
505
540
  var NUM = String.raw`-?\d+(?:[.,]\d+)?`;
506
541
  function quantitiesIn(query) {
@@ -1143,19 +1178,20 @@ ${r.output}`;
1143
1178
  }
1144
1179
 
1145
1180
  export {
1181
+ evaluate,
1182
+ verdictNote,
1146
1183
  standardForDocNumber,
1147
1184
  licensedEntryForPackage,
1148
1185
  licenseBoundaryNote,
1149
1186
  licenseBoundaryRefusal,
1150
1187
  bindModelNode,
1188
+ bindConstraintByQuantities,
1151
1189
  modelGroundingBlock,
1152
1190
  modelCitation,
1153
1191
  modelEcho,
1154
1192
  resolveBlocks,
1155
1193
  contractV2,
1156
1194
  tableRetyped,
1157
- evaluate,
1158
- verdictNote,
1159
1195
  quantitiesIn,
1160
1196
  evaluateConditionSets,
1161
1197
  parseToolCall,
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  TOOL_DECLARATION,
3
+ bindConstraintByQuantities,
3
4
  bindModelNode,
4
5
  contractV2,
5
6
  evaluate,
@@ -18,7 +19,7 @@ import {
18
19
  tableRetyped,
19
20
  toolNote,
20
21
  verdictNote
21
- } from "./chunk-QNLFNEI4.js";
22
+ } from "./chunk-ANUHRJ4Z.js";
22
23
  import {
23
24
  NO_CONTEXT,
24
25
  answerQuality,
@@ -56,7 +57,7 @@ import {
56
57
  tokenBudget,
57
58
  understandQuery,
58
59
  usageTotal
59
- } from "./chunk-Z3R34T7Q.js";
60
+ } from "./chunk-25ECO5M7.js";
60
61
  import {
61
62
  embed,
62
63
  generateOnce,
@@ -2059,7 +2060,7 @@ ${summary}` }] : [],
2059
2060
  query: q.query,
2060
2061
  standard: standardForDocNumber(modelDocHint?.doc_number),
2061
2062
  standardKeys
2062
- }) : null;
2063
+ }) ?? await bindConstraintByQuantities(env, q.query, standardKeys) : null;
2063
2064
  if (boundModel) {
2064
2065
  ctxApplied = { ...ctxApplied, model: modelEcho(boundModel) };
2065
2066
  console.log("model plane: bound", boundModel.node_id, `[${boundModel.standard}]`, boundModel.clause?.urn ?? "no-clause", boundModel.gated ? "(gated: license)" : "");
@@ -6,7 +6,7 @@ import {
6
6
  sessionFrom,
7
7
  telemetry,
8
8
  understandQuery
9
- } from "./chunk-Z3R34T7Q.js";
9
+ } from "./chunk-25ECO5M7.js";
10
10
  import {
11
11
  portModelRunner
12
12
  } from "./chunk-KNCRYAX3.js";
package/dist/lexical.d.ts CHANGED
@@ -2,6 +2,7 @@ import type { Hit } from "./pipeline";
2
2
  /** Build a safe FTS5 MATCH query from user text: alphanumeric tokens,
3
3
  * joined with OR so jargon hits don't require full-phrase match. */
4
4
  export declare function ftsMatchQuery(query: string): string | null;
5
+ export declare function rowsToHits(rows: any[]): Hit[];
5
6
  /** Lexical ranking WITHIN a document set (the graph lanes' identity
6
7
  * resolution): BM25 over the corpus's own FTS, restricted to the
7
8
  * documents' doc_number values. This is the working substitute for
@@ -71,6 +71,17 @@ export declare function bindModelNode(env: any, opts: {
71
71
  standard?: string | null;
72
72
  standardKeys?: ReadonlySet<string> | null;
73
73
  }): Promise<BoundModelNode | null>;
74
+ /** A constraint-shaped question binds its constraint: when the question
75
+ * states two or more of a constraint node's own check symbols (the
76
+ * leaf names — d_max, e_max) alongside verdict vocabulary ("is that
77
+ * proposal acceptable"), the node binds by WHAT THE QUESTION STATES,
78
+ * not by whether the question spells a node id. The id grammar stays
79
+ * the primary bind; this is the fallback for the user-shaped question
80
+ * ("E_max 30000 v, testing to D_max 26000 v — acceptable?"), and it
81
+ * is deliberately narrow: constraint kind only (requirements are too
82
+ * numerous and too loosely tied to vocabulary), most-matched-symbols
83
+ * wins, license-gated like every model lane. */
84
+ export declare function bindConstraintByQuantities(env: any, query: string, standardKeys?: ReadonlySet<string> | null): Promise<BoundModelNode | null>;
74
85
  /** The structured grounding block for the prompt — every line is the
75
86
  * node's own declared content (the bundle projection), never a model
76
87
  * paraphrase. The discrepancy block, when the model declares one, is the
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  handleSearch
3
- } from "./chunk-PIENBLFH.js";
4
- import "./chunk-Z3R34T7Q.js";
3
+ } from "./chunk-TFGJ6NYS.js";
4
+ import "./chunk-25ECO5M7.js";
5
5
  import "./chunk-KNCRYAX3.js";
6
6
  import "./chunk-DTW4UW2E.js";
7
7
  import "./chunk-A3QHHUN5.js";
@@ -4,7 +4,7 @@ import {
4
4
  parseToolCall,
5
5
  runTool,
6
6
  toolNote
7
- } from "./chunk-QNLFNEI4.js";
7
+ } from "./chunk-ANUHRJ4Z.js";
8
8
  import "./chunk-KNCRYAX3.js";
9
9
  import "./chunk-U4ERFUOK.js";
10
10
  import "./chunk-BOAITSVV.js";
@@ -4,15 +4,15 @@ import {
4
4
  handleMemories,
5
5
  scoreFaithfulness,
6
6
  scoreJudge
7
- } from "../../chunk-DMTRTW2Q.js";
7
+ } from "../../chunk-NCZHN6QW.js";
8
8
  import {
9
9
  TOOLS_REGISTRY,
10
10
  bindModelNode,
11
11
  modelGroundingBlock
12
- } from "../../chunk-QNLFNEI4.js";
12
+ } from "../../chunk-ANUHRJ4Z.js";
13
13
  import {
14
14
  handleSearch
15
- } from "../../chunk-PIENBLFH.js";
15
+ } from "../../chunk-TFGJ6NYS.js";
16
16
  import {
17
17
  buildMessages,
18
18
  citations,
@@ -34,7 +34,7 @@ import {
34
34
  sessionFrom,
35
35
  telemetry,
36
36
  understandQuery
37
- } from "../../chunk-Z3R34T7Q.js";
37
+ } from "../../chunk-25ECO5M7.js";
38
38
  import {
39
39
  cfBlobs,
40
40
  embed,
@@ -990,13 +990,13 @@ async function callToolOnce(env, ctx, _req, tier, key, name, args, t0) {
990
990
  // stream:false forces the JSON lane (anon defaults to SSE)
991
991
  body: JSON.stringify({ ...args, stream: false })
992
992
  });
993
- const { runTool, TOOLS_REGISTRY: TOOLS_REGISTRY2 } = await import("../../tools-ZNQY6ZMC.js");
993
+ const { runTool, TOOLS_REGISTRY: TOOLS_REGISTRY2 } = await import("../../tools-EXDADNWX.js");
994
994
  let payload;
995
995
  if (TOOLS_REGISTRY2.some((t) => t.name === name && t.audiences.includes("mcp"))) {
996
996
  const r = await runTool({ ...env, DB: env.DB }, { name, args }, "mcp");
997
997
  payload = r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
998
998
  } else {
999
- const res = name === "ask" ? await (await import("../../ask-OVT3F2US.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-FC7IULRV.js")).handleSearch(env, ctx, inner, tier, key);
999
+ const res = name === "ask" ? await (await import("../../ask-M3VRAIJB.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-5SNP5CHL.js")).handleSearch(env, ctx, inner, tier, key);
1000
1000
  payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
1001
1001
  }
1002
1002
  telemetry(env, ctx, tier, `mcp:${name}`, null, !payload?.error, JSON.stringify(payload).length, await sha256Hex(`${name}:${JSON.stringify(args ?? {})}`), void 0, void 0, { durationMs: Date.now() - t0 });
package/package.json CHANGED
@@ -27,7 +27,7 @@
27
27
  "playwright": "^1.62.1",
28
28
  "yaml": "^2.9.1"
29
29
  },
30
- "version": "0.2.101",
30
+ "version": "0.2.103",
31
31
  "description": "The Konneal engine: the publisher-agnostic build pipeline and API plane for standards intelligence (retrieval, answer contract, verdicts, evaluation).",
32
32
  "license": "BSD-3-Clause",
33
33
  "type": "module",
@@ -22,7 +22,7 @@ import { contractV2, tableRetyped } from "./refs";
22
22
  import { completeTables, completeFigures } from "./completion";
23
23
  import { NO_CONTEXT, appliedContext, contextNote, namedDocumentIn, parseContext, resolveDocScope, syntheticUnderstanding } from "./context";
24
24
  import { liveDataConfig, liveTokenFor, opCfg, opTokenMember, resolveLiveAccount, type LiveRecord } from "./livedata";
25
- import { bindModelNode, licenseBoundaryNote, licenseBoundaryRefusal, licensedEntryForPackage, modelCitation, modelEcho, modelGroundingBlock, standardForDocNumber } from "./modelplane";
25
+ import { bindModelNode, bindConstraintByQuantities, licenseBoundaryNote, licenseBoundaryRefusal, licensedEntryForPackage, modelCitation, modelEcho, modelGroundingBlock, standardForDocNumber } from "./modelplane";
26
26
  import { evaluate as machineEvaluate, verdictNote } from "./verdict";
27
27
  import { evaluateConditionSets, quantitiesIn, type ConditionVerdict } from "./conditions";
28
28
  import { evaluateAggregation, type AggregationVerdict } from "./aggregation";
@@ -857,12 +857,12 @@ let convEntities: Array<{ entity: string; kind: string }> = [];
857
857
  // unambiguous across the indexed standards.
858
858
  const modelDocHint = named ?? docScope ?? namedDocumentIn(q.query);
859
859
  const boundModel = P().publisher.features?.model_plane
860
- ? await bindModelNode(env, {
860
+ ? (await bindModelNode(env, {
861
861
  label: declaredCtx?.label,
862
862
  query: q.query,
863
863
  standard: standardForDocNumber(modelDocHint?.doc_number),
864
864
  standardKeys,
865
- })
865
+ })) ?? (await bindConstraintByQuantities(env, q.query, standardKeys))
866
866
  : null;
867
867
  if (boundModel) {
868
868
  ctxApplied = { ...ctxApplied, model: modelEcho(boundModel) };
@@ -53,7 +53,7 @@ function rowMeta(r: any): ChunkMeta {
53
53
  };
54
54
  }
55
55
 
56
- function rowsToHits(rows: any[]): Hit[] {
56
+ export function rowsToHits(rows: any[]): Hit[] {
57
57
  return rows.map((r: any) => {
58
58
  // rank is bm25 (lower better) → convert to positive score that RRF can ignore
59
59
  // (RRF uses rank position, not score). score kept for logging only.
@@ -30,6 +30,7 @@
30
30
  * /conf/<class>/<id>, /term/<id>, /constraint/<id>, /characteristic/<id>,
31
31
  * /state-machine/<id>, /dimension/<id>). */
32
32
  import { P } from "./profile.ts";
33
+ import { extractChecks, symbolsIn } from "./verdict.ts";
33
34
  const NODE_RE = /(?:^|[\s("'`])\/(req|conf|term|constraint|characteristic|state-machine|dimension)\/([a-z0-9][a-z0-9_-]*(?:\/[a-z0-9][a-z0-9_-]*)?)(?=[\s)"'`,;:.]|$)/i;
34
35
 
35
36
  /** The first model-node id a text names (the declared chip label first,
@@ -217,6 +218,60 @@ export async function bindModelNode(
217
218
  }
218
219
  }
219
220
 
221
+ // ── bind by stated quantities (the constraint resolution) ────────────
222
+
223
+ /** A constraint-shaped question binds its constraint: when the question
224
+ * states two or more of a constraint node's own check symbols (the
225
+ * leaf names — d_max, e_max) alongside verdict vocabulary ("is that
226
+ * proposal acceptable"), the node binds by WHAT THE QUESTION STATES,
227
+ * not by whether the question spells a node id. The id grammar stays
228
+ * the primary bind; this is the fallback for the user-shaped question
229
+ * ("E_max 30000 v, testing to D_max 26000 v — acceptable?"), and it
230
+ * is deliberately narrow: constraint kind only (requirements are too
231
+ * numerous and too loosely tied to vocabulary), most-matched-symbols
232
+ * wins, license-gated like every model lane. */
233
+ export async function bindConstraintByQuantities(
234
+ env: any,
235
+ query: string,
236
+ standardKeys?: ReadonlySet<string> | null,
237
+ ): Promise<BoundModelNode | null> {
238
+ const verdictish = /\b(acceptable|valid|violate|violation|within|comply|proposal|severity|conform)\b/i.test(query);
239
+ if (!verdictish) return null;
240
+ try {
241
+ const rows = await env.DB.prepare(
242
+ "SELECT standard, node_id, kind, name, clause_doc, clause_ref, content FROM model_nodes WHERE kind = 'constraint' LIMIT 120",
243
+ ).all();
244
+ const q = query.toLowerCase();
245
+ let best: { score: number; standard: string; node_id: string } | null = null;
246
+ for (const r of (rows.results ?? []) as any[]) {
247
+ const checks = extractChecks(JSON.parse(String(r.content ?? "{}")));
248
+ if (!checks.length) continue;
249
+ const leaves = new Set(
250
+ symbolsIn(checks)
251
+ .map((sym) => sym.split(".").pop() ?? sym)
252
+ .filter((leaf) => leaf.length >= 2),
253
+ );
254
+ let matched = 0;
255
+ for (const leaf of leaves) {
256
+ const re = new RegExp(`(?<![a-z_])${leaf.replace(/[^a-z0-9_]/g, "")}(?![a-z_])`, "i");
257
+ if (re.test(q)) matched++;
258
+ }
259
+ if (matched >= 2 && (!best || matched > best.score)) {
260
+ best = { score: matched, standard: String(r.standard), node_id: String(r.node_id) };
261
+ }
262
+ }
263
+ if (!best) return null;
264
+ const gate = (node: BoundModelNode | null): BoundModelNode | null => {
265
+ if (!node) return null;
266
+ const entry = licensedEntryForPackage(node.standard);
267
+ return entry && !(standardKeys?.has(entry.key) ?? false) ? { ...node, gated: true, content: {} } : node;
268
+ };
269
+ return gate(await fetchNode(env, best.standard, best.node_id));
270
+ } catch {
271
+ return null;
272
+ }
273
+ }
274
+
220
275
  // ── the grounding block (the node's own content, composed) ───────────
221
276
 
222
277
  function clip(s: unknown, n = 500): string {
@@ -4,18 +4,20 @@
4
4
  // saved), the doc/edition-filtered query with its guessed-pin drop and
5
5
  // sparse-filter widen, and the plain unfiltered query.
6
6
  import { LIMITS } from "../config.ts";
7
- import { toVectorizeFilter } from "../selfquery.ts";
8
7
  import type { Stage } from "./types.ts";
9
8
 
10
9
  export const dense: Stage = {
11
10
  name: "dense",
12
11
  run: async (c) => {
13
- const { env, filter, filters, vector, opts, rq, folded } = c;
12
+ const { env, filters, vector, opts, rq, folded } = c;
13
+ // the index's metadata filtering is DEAD on this platform (measured
14
+ // 2026-10-05: filters return empty for values proven present), so
15
+ // the scope never rides the query — the matches filter IN CODE and
16
+ // the sparse widen still covers thin scopes
14
17
  const q: any = { topK: LIMITS.retrieveK, returnMetadata: "all" };
15
- if (filter) q.filter = filter;
16
18
  const optimistic = opts.optimisticHits ?? [];
17
19
  const sameLane = rq === folded; // optimistic vector === this lane's vector
18
- if (!filter && sameLane && optimistic.length) {
20
+ if (!filters?.doc_number && sameLane && optimistic.length) {
19
21
  // Option C fast path: the unfiltered dense query already ran
20
22
  // concurrently with understanding — same vector, same topK, no
21
23
  // filter. Reusing it skips one serial Vectorize round-trip with a
@@ -31,8 +33,13 @@ export const dense: Stage = {
31
33
  // REPLACE an identical query, never dilute a better one.
32
34
  return;
33
35
  }
34
- if (filter) {
35
- let matches: any[] = (await env.VECTORIZE.query(vector, q)).matches ?? [];
36
+ if (filters?.doc_number) {
37
+ const allMatches: any[] = (await env.VECTORIZE.query(vector, q)).matches ?? [];
38
+ let matches: any[] = allMatches;
39
+ const before = matches.length;
40
+ matches = matches.filter((m) => String(m.metadata?.doc_number ?? "") === filters.doc_number);
41
+ if (filters.edition) matches = matches.filter((m) => !m.metadata?.edition || String(m.metadata.edition) === filters.edition);
42
+ console.log("dense scope (in code):", before, "→", matches.length, "in doc", filters.doc_number, filters.edition ? `@${filters.edition}` : "");
36
43
  // an edition pin corroborated by (almost) nothing means the pin
37
44
  // was a guess — understanding emits editions for families it
38
45
  // mixes up (observed: R 76 pinned @2021, an R 60 year; the corpus
@@ -42,15 +49,13 @@ export const dense: Stage = {
42
49
  // the doc-only filter and let family-relative steering rank
43
50
  // editions downstream. A pin the user actually asked for
44
51
  // survives — its edition exists in the corpus.
52
+ // the same guess-drop, in code: a pinned edition that starved the
53
+ // scoped set drops to the doc-only scope
45
54
  if (filters && filters.edition && matches.length < 3) {
46
- const docOnly = await env.VECTORIZE.query(vector, {
47
- topK: LIMITS.retrieveK,
48
- returnMetadata: "all",
49
- filter: toVectorizeFilter({ doc_number: filters.doc_number }),
50
- });
51
- if ((docOnly.matches ?? []).length > matches.length) {
52
- console.log("edition pin dropped:", filters.doc_number, "@", filters.edition, "→", docOnly.matches?.length ?? 0, "doc-scoped hits (edition not in corpus)");
53
- matches = docOnly.matches ?? [];
55
+ const docOnly = allMatches.filter((m) => String(m.metadata?.doc_number ?? "") === filters.doc_number);
56
+ if (docOnly.length > matches.length) {
57
+ console.log("edition pin dropped:", filters.doc_number, "@", filters.edition, "→", docOnly.length, "doc-scoped hits (edition not in corpus)");
58
+ matches = docOnly;
54
59
  filters.edition = undefined;
55
60
  }
56
61
  }
@@ -7,7 +7,8 @@
7
7
  // left every 2021 chunk out). One registry read per query; at most
8
8
  // `maxDocs` documents covered; additive — any failure skips.
9
9
  import { THRESHOLDS } from "../config.ts";
10
- import { toHits, type Stage } from "./types.ts";
10
+ import type { Stage } from "./types.ts";
11
+ import { rowsToHits } from "../lexical.ts";
11
12
 
12
13
  const maxDocs = 2;
13
14
 
@@ -43,12 +44,17 @@ export const editionCover: Stage = {
43
44
  let added = 0;
44
45
  for (const w of want.slice(0, maxDocs)) {
45
46
  try {
46
- const q = await c.env.VECTORIZE.query(c.vector, {
47
- topK: 3,
48
- returnMetadata: "all",
49
- filter: { $and: [{ docidentifier: { $eq: w.di } }, { edition: { $eq: w.edition } }] },
50
- });
51
- const hits = toHits((q as any).matches ?? []).map((h) => ({ ...h, score: top * THRESHOLDS.editionCoverDiscount }));
47
+ // identity resolution, not index filtering: the index's metadata
48
+ // filters are dead on this platform (measured 2026-10-05 — the
49
+ // cover's $and docidentifier/edition query returned empty for
50
+ // its whole life), but the D1 chunks table IS the corpus — the
51
+ // current edition's chunks read directly, text and all
52
+ const rows = await c.env.DB.prepare(
53
+ "SELECT id, doc_id, docidentifier, doctype, doc_number, edition, language, clause_anchor, clause_title, status, superseded_by, corpus, tier, text FROM chunks WHERE docidentifier = ?1 AND edition = ?2 LIMIT 3",
54
+ )
55
+ .bind(w.di, w.edition)
56
+ .all();
57
+ const hits = rowsToHits(rows.results ?? []).map((h) => ({ ...h, score: top * THRESHOLDS.editionCoverDiscount }));
52
58
  c.hits.push(...hits);
53
59
  added += hits.length;
54
60
  console.log("edition cover:", w.di, w.edition, `+${hits.length}`);
@@ -18,6 +18,7 @@ import { glossary } from "./glossary.ts";
18
18
  import { conceptGraph } from "./conceptGraph.ts";
19
19
  import { graphLane } from "./graphLane.ts";
20
20
  import { licensedLane } from "./licensedLane.ts";
21
+ import { licensedCover } from "./licensedCover.ts";
21
22
  import { multiQuery } from "./multiQuery.ts";
22
23
  import { subQuery } from "./subQuery.ts";
23
24
  import { poolOpen } from "./poolOpen.ts";
@@ -68,6 +69,7 @@ export const STAGES: Stage[] = [
68
69
  editionSteer,
69
70
  propagate,
70
71
  diversity,
72
+ licensedCover,
71
73
  typedPin,
72
74
  sectionDescent,
73
75
  dedup,
@@ -0,0 +1,31 @@
1
+ // Licensed cover (the seat): an entitled, topically-matched caller
2
+ // asked about the licensed standard, so the licensed package's own
3
+ // machinery must be ABLE to reach the answer — the public restatements
4
+ // crowd the window and the citation the golden legs assert rides this
5
+ // passage. When the rerank cut drops every licensed unit, the lane's
6
+ // best unit takes one seat at the end of the window (appended after
7
+ // ranking, like the section-descent children — the window floor keeps
8
+ // appended passages by construction). Narrow by law: one unit, only
9
+ // when none is present, only for an armed licensed lane.
10
+ import { portIndex } from "../env.ts";
11
+ import { toHits } from "./types.ts";
12
+ import type { Stage } from "./types.ts";
13
+
14
+ export const licensedCover: Stage = {
15
+ name: "licensed-cover",
16
+ failure: "additive",
17
+ when: (c) => !!c.opts.licensedDocNumbers?.length && c.finalHits.length > 0,
18
+ run: async (c) => {
19
+ const keys = new Set(c.opts.licensedDocNumbers!);
20
+ const present = c.finalHits.some((h) => keys.has(String((h.metadata as any).standard ?? "")));
21
+ if (present) return;
22
+ const ids = ((await (c.lane["licensed-ids"] as Promise<string[]> | undefined)) ?? []).slice(0, 8);
23
+ if (!ids.length) return;
24
+ const matches = await portIndex(c.env, "public").getByIds(ids);
25
+ const unit = matches.find((m) => m.metadata && keys.has(String((m.metadata as any).standard ?? "")));
26
+ if (!unit) return;
27
+ const hit = toHits([unit as any])[0]!;
28
+ c.finalHits = [...c.finalHits, hit];
29
+ console.log("licensed cover: seated", unit.id, "—", (unit.metadata as any).docidentifier);
30
+ },
31
+ };
@@ -44,13 +44,18 @@ export const licensedLane: Stage = {
44
44
  const matches = await portIndex(c.env, "public").getByIds(ids);
45
45
  const seenIds = new Set(c.matches.map((m: any) => m.id));
46
46
  let merged = 0;
47
+ const mergedIds: string[] = [];
47
48
  for (const m of matches) {
48
49
  if (m.metadata && !seenIds.has(m.id)) {
49
50
  c.matches.push({ id: m.id, score: THRESHOLDS.licensedLaneScore, metadata: m.metadata });
50
51
  seenIds.add(m.id);
51
52
  merged++;
53
+ mergedIds.push(m.id);
52
54
  }
53
55
  }
56
+ // the cover stage's stash: the licensed units' ids, so the seat can
57
+ // recover a unit the rerank cut dropped
58
+ c.lane["licensed-ids"] = Promise.resolve(mergedIds);
54
59
  console.log("licensed lane:", ids.length, "units,", matches.length, "vectors,", merged, "merged");
55
60
  },
56
61
  };