@konneal/engine 0.2.195 → 0.2.196

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,9 +1,9 @@
1
1
  import {
2
2
  handleAsk
3
- } from "./chunk-GJHB3LY4.js";
3
+ } from "./chunk-FOCGVXCX.js";
4
4
  import "./chunk-IRCDOFP4.js";
5
5
  import "./chunk-2DBXRO4E.js";
6
- import "./chunk-JEPZ7SNP.js";
6
+ import "./chunk-CTSEDLL2.js";
7
7
  import "./chunk-5OSQMWRV.js";
8
8
  import "./chunk-M4OCMIWU.js";
9
9
  import "./chunk-ETLSWSZ7.js";
@@ -23043,15 +23043,20 @@ var figureSeat = {
23043
23043
  const dn = String(h.metadata.doc_number ?? "");
23044
23044
  if (dn) counts.set(dn, (counts.get(dn) ?? 0) + 1);
23045
23045
  }
23046
- const docNumber = c.u?.doc_number ?? [...counts.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
23047
- if (!docNumber) return;
23046
+ const namedDoc = String(c.u?.docidentifier ?? "").trim();
23047
+ const number = String(c.u?.doc_number ?? "");
23048
+ const byIdentifier = /-\d|-[A-Z]\b/.test(namedDoc.replace(/:\d{4}.*$/, ""));
23049
+ const scope = byIdentifier ? namedDoc : number || [...counts.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
23050
+ if (!scope) return;
23048
23051
  const seated = new Set(
23049
23052
  c.finalHits.filter((h) => h.metadata.block === "figure").map((h) => String(h.metadata.unit_id ?? h.id))
23050
23053
  );
23051
- const rows = await portStore(c.env).prepare(
23052
- `SELECT id, doc_id, docidentifier, clause_anchor, clause_title, doc_number, edition, language, status, corpus, tier, text, unit_id
23053
- FROM chunks WHERE doc_number = ?1 AND block = 'figure' AND unit_id IS NOT NULL LIMIT 3`
23054
- ).bind(String(docNumber)).all();
23054
+ const cols = "id, doc_id, docidentifier, clause_anchor, clause_title, doc_number, edition, language, status, corpus, tier, text, unit_id";
23055
+ const store = portStore(c.env);
23056
+ let rows = byIdentifier ? await store.prepare(`SELECT ${cols} FROM chunks WHERE docidentifier LIKE ?1 AND block = 'figure' AND unit_id IS NOT NULL ORDER BY clause_anchor LIMIT 3`).bind(`%${namedDoc}`).all() : await store.prepare(`SELECT ${cols} FROM chunks WHERE doc_number = ?1 AND block = 'figure' AND unit_id IS NOT NULL LIMIT 3`).bind(String(scope)).all();
23057
+ if (byIdentifier && !(rows.results ?? []).length && number) {
23058
+ rows = await store.prepare(`SELECT ${cols} FROM chunks WHERE doc_number = ?1 AND block = 'figure' AND unit_id IS NOT NULL LIMIT 3`).bind(number).all();
23059
+ }
23055
23060
  const fresh = [];
23056
23061
  for (const r of rows.results ?? []) {
23057
23062
  if (seated.has(String(r.unit_id))) continue;
@@ -23063,7 +23068,7 @@ var figureSeat = {
23063
23068
  metadata: {
23064
23069
  doc_id: String(r.doc_id ?? ""),
23065
23070
  docidentifier: String(r.docidentifier ?? ""),
23066
- doc_number: String(r.doc_number ?? docNumber),
23071
+ doc_number: String(r.doc_number ?? number ?? scope),
23067
23072
  clause_anchor: r.clause_anchor ? String(r.clause_anchor) : void 0,
23068
23073
  clause_title: r.clause_title ? String(r.clause_title) : void 0,
23069
23074
  edition: r.edition ? String(r.edition) : void 0,
@@ -23078,7 +23083,7 @@ var figureSeat = {
23078
23083
  }
23079
23084
  if (!fresh.length) return;
23080
23085
  c.finalHits = [...fresh, ...c.finalHits];
23081
- console.log("figure-seat: seated", fresh.length, "figure chunk(s) of doc", docNumber);
23086
+ console.log("figure-seat: seated", fresh.length, "figure chunk(s) of", byIdentifier ? namedDoc : "doc " + scope, byIdentifier ? "(named)" : "");
23082
23087
  } catch {
23083
23088
  }
23084
23089
  }
@@ -6,7 +6,7 @@ import {
6
6
  sessionFrom,
7
7
  telemetry,
8
8
  understandQuery
9
- } from "./chunk-JEPZ7SNP.js";
9
+ } from "./chunk-CTSEDLL2.js";
10
10
  import {
11
11
  corsHeaders,
12
12
  err,
@@ -77,7 +77,7 @@ import {
77
77
  tokenBudget,
78
78
  understandQuery,
79
79
  usageTotal
80
- } from "./chunk-JEPZ7SNP.js";
80
+ } from "./chunk-CTSEDLL2.js";
81
81
  import {
82
82
  CAPACITY_PAUSE_MS,
83
83
  embed,
@@ -519,17 +519,20 @@ async function completeTables(db, answer, used) {
519
519
  }
520
520
  return blocks;
521
521
  }
522
- async function completeFigures(db, answer, alreadyAttached, used = [], query) {
522
+ async function completeFigures(db, answer, alreadyAttached, used = [], query, namedDoc = null) {
523
523
  const unitForm = (answer.match(/u:fig[\w.-]*/g) ?? []).map((x) => x.replace(/[.,;:)]+$/, ""));
524
524
  const bareForm = (answer.match(/\bfig-[\w.-]+\b/g) ?? []).map((x) => x.replace(/[.,;:)]+$/, ""));
525
525
  let mentioned = [.../* @__PURE__ */ new Set([...unitForm, ...bareForm.map((x) => x.startsWith("u:") ? x : "u:" + x)])].slice(0, 6);
526
+ const namedLabel = namedDoc ? namedDoc.split(":")[0].trim().toUpperCase() : "";
527
+ const namedPart = namedLabel && /-\d|-[A-Z]\b/.test(namedLabel) ? namedLabel : "";
528
+ const ofNamedPart = (h) => !namedPart || String(h.metadata.docidentifier ?? "").toUpperCase().startsWith(namedPart);
526
529
  const docOf = /* @__PURE__ */ new Map();
527
530
  const proseNums = new Set(
528
531
  (answer.match(/\bFig(?:ure|\.)s?\s*([0-9]{1,2}[a-z]?)/g) ?? []).map((x) => x.replace(/\bFig(?:ure|\.)s?\s*/i, "").toLowerCase())
529
532
  );
530
533
  if (query && /\bfig(ure|s)?\b/i.test(query)) {
531
534
  for (const h of used.slice(0, 12)) {
532
- if (h.metadata.unit_id && h.metadata.block === "figure") {
535
+ if (h.metadata.unit_id && h.metadata.block === "figure" && ofNamedPart(h)) {
533
536
  mentioned.push(h.metadata.unit_id);
534
537
  if (!docOf.has(h.metadata.unit_id)) docOf.set(h.metadata.unit_id, { unit_id: h.metadata.unit_id, doc_id: h.metadata.doc_id, docidentifier: h.metadata.docidentifier });
535
538
  }
@@ -537,7 +540,7 @@ async function completeFigures(db, answer, alreadyAttached, used = [], query) {
537
540
  mentioned = [...new Set(mentioned)].slice(0, 6);
538
541
  }
539
542
  if (proseNums.size) {
540
- const fams = [...new Set(used.map((h) => h.metadata.docidentifier).filter(Boolean))].slice(0, 3);
543
+ const fams = [...new Set([namedPart, ...used.map((h) => h.metadata.docidentifier).filter(Boolean)].filter(Boolean))].slice(0, 3);
541
544
  for (const fam of fams) {
542
545
  const base = String(fam).replace(/\s*\([A-Z]\)\s*$/, "").split(":")[0].trim();
543
546
  const rows = await publicationUnitRows(db, "figure", `%${base}%`, 24);
@@ -3451,7 +3454,7 @@ ${doc.text}` }
3451
3454
  if (!canonical0.includes(refusalAnswer())) {
3452
3455
  try {
3453
3456
  streamedCompletionBlocks = await completeTables(env.DB, canonical0, usedHits);
3454
- streamedCompletionBlocks.push(...await completeFigures(env.DB, canonical0, [...c2.blocks, ...streamedCompletionBlocks], usedHits, q2.query));
3457
+ streamedCompletionBlocks.push(...await completeFigures(env.DB, canonical0, [...c2.blocks, ...streamedCompletionBlocks], usedHits, q2.query, understanding?.docidentifier ?? null));
3455
3458
  if (streamedCompletionBlocks.length) console.log("streamed completion:", streamedCompletionBlocks.length, "block(s) attached");
3456
3459
  } catch {
3457
3460
  }
@@ -3684,7 +3687,7 @@ ${doc.text}` }
3684
3687
  completionBlocks = await completeTables(env.DB, answer, used);
3685
3688
  if (completionBlocks.length) console.log("contract completion:", completionBlocks.length, "table block(s) attached server-side");
3686
3689
  }
3687
- completionBlocks.push(...await completeFigures(env.DB, answer, [...c2ns.blocks, ...completionBlocks], used, q2.query));
3690
+ completionBlocks.push(...await completeFigures(env.DB, answer, [...c2ns.blocks, ...completionBlocks], used, q2.query, understanding?.docidentifier ?? null));
3688
3691
  if (register?.rows?.length && answer.includes(refusalAnswer())) {
3689
3692
  console.log("blind-answer: the register note carried rows but the answer refused \u2014 regenerating once");
3690
3693
  mark("Re-answering from the register results");
@@ -2,4 +2,4 @@ import type { StoreQuery } from "./ports/store.ts";
2
2
  import { type ResolvedBlock } from "./refs.ts";
3
3
  import type { Hit } from "./pipeline.ts";
4
4
  export declare function completeTables(db: StoreQuery, answer: string, used: Hit[]): Promise<ResolvedBlock[]>;
5
- export declare function completeFigures(db: StoreQuery, answer: string, alreadyAttached: ResolvedBlock[], used?: Hit[], query?: string): Promise<ResolvedBlock[]>;
5
+ export declare function completeFigures(db: StoreQuery, answer: string, alreadyAttached: ResolvedBlock[], used?: Hit[], query?: string, namedDoc?: string | null): Promise<ResolvedBlock[]>;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  handleSearch
3
- } from "./chunk-6HW7R5SI.js";
4
- import "./chunk-JEPZ7SNP.js";
3
+ } from "./chunk-F4VDXM3W.js";
4
+ import "./chunk-CTSEDLL2.js";
5
5
  import "./chunk-5OSQMWRV.js";
6
6
  import "./chunk-M4OCMIWU.js";
7
7
  import "./chunk-ETLSWSZ7.js";
@@ -9,7 +9,7 @@ import {
9
9
  listUserDocuments,
10
10
  scoreFaithfulness,
11
11
  scoreJudge
12
- } from "../../chunk-GJHB3LY4.js";
12
+ } from "../../chunk-FOCGVXCX.js";
13
13
  import "../../chunk-IRCDOFP4.js";
14
14
  import {
15
15
  TOOLS_REGISTRY,
@@ -18,7 +18,7 @@ import {
18
18
  } from "../../chunk-2DBXRO4E.js";
19
19
  import {
20
20
  handleSearch
21
- } from "../../chunk-6HW7R5SI.js";
21
+ } from "../../chunk-F4VDXM3W.js";
22
22
  import {
23
23
  buildMessages,
24
24
  citations,
@@ -43,7 +43,7 @@ import {
43
43
  sessionFrom,
44
44
  telemetry,
45
45
  understandQuery
46
- } from "../../chunk-JEPZ7SNP.js";
46
+ } from "../../chunk-CTSEDLL2.js";
47
47
  import {
48
48
  embed,
49
49
  generateOnce,
@@ -1265,7 +1265,7 @@ async function callToolOnce(env, ctx, _req, tier, key, name, args, t0) {
1265
1265
  const r = await runTool({ ...env, DB: env.DB }, { name, args }, "mcp");
1266
1266
  payload = r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
1267
1267
  } else {
1268
- const res = name === "ask" ? await (await import("../../ask-CDJCSFI4.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-3V6EFKFX.js")).handleSearch(env, ctx, inner, tier, key);
1268
+ const res = name === "ask" ? await (await import("../../ask-PYJFLK5H.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-WEZGZDO2.js")).handleSearch(env, ctx, inner, tier, key);
1269
1269
  payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
1270
1270
  }
1271
1271
  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.195",
30
+ "version": "0.2.196",
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",
@@ -1959,7 +1959,7 @@ let convEntities: Array<{ entity: string; kind: string }> = [];
1959
1959
  if (!canonical0.includes(refusalAnswer())) {
1960
1960
  try {
1961
1961
  streamedCompletionBlocks = await completeTables(env.DB, canonical0, usedHits);
1962
- streamedCompletionBlocks.push(...(await completeFigures(env.DB, canonical0, [...c2.blocks, ...streamedCompletionBlocks], usedHits, q.query)));
1962
+ streamedCompletionBlocks.push(...(await completeFigures(env.DB, canonical0, [...c2.blocks, ...streamedCompletionBlocks], usedHits, q.query, understanding?.docidentifier ?? null)));
1963
1963
  if (streamedCompletionBlocks.length) console.log("streamed completion:", streamedCompletionBlocks.length, "block(s) attached");
1964
1964
  } catch { /* additive — the answer stands without the blocks */ }
1965
1965
  }
@@ -2283,7 +2283,7 @@ let convEntities: Array<{ entity: string; kind: string }> = [];
2283
2283
  }
2284
2284
 
2285
2285
  // figure completion (#172) — see ./completion for the rationale
2286
- completionBlocks.push(...(await completeFigures(env.DB, answer!, [...c2ns.blocks, ...completionBlocks], used, q.query)));
2286
+ completionBlocks.push(...(await completeFigures(env.DB, answer!, [...c2ns.blocks, ...completionBlocks], used, q.query, understanding?.docidentifier ?? null)));
2287
2287
 
2288
2288
  // the blind-answer regeneration (2026-10-03, live on the photo case):
2289
2289
  // the multimodal call can answer AROUND an attached image — a 200 whose
@@ -59,6 +59,7 @@ export async function completeFigures(
59
59
  alreadyAttached: ResolvedBlock[],
60
60
  used: Hit[] = [],
61
61
  query?: string,
62
+ namedDoc: string | null = null,
62
63
  ): Promise<ResolvedBlock[]> {
63
64
  // the model names figures BOTH ways: unit ids (u:fig-3) and bare
64
65
  // producer anchors (fig-2a of D 36) — collect both forms; bare
@@ -67,7 +68,15 @@ export async function completeFigures(
67
68
  const unitForm = (answer.match(/u:fig[\w.-]*/g) ?? []).map((x) => x.replace(/[.,;:)]+$/, ""));
68
69
  const bareForm = (answer.match(/\bfig-[\w.-]+\b/g) ?? []).map((x) => x.replace(/[.,;:)]+$/, ""));
69
70
  let mentioned = [...new Set([...unitForm, ...bareForm.map((x) => (x.startsWith("u:") ? x : "u:" + x))])].slice(0, 6);
70
- // the document behind each mentioned unit — a unit id is shared across publications, so a block resolves within the document that named it
71
+ // the part the question names ("Figure 2 in OIML R 60-2"): every part of
72
+ // a family carries the family's number, so without this the completion
73
+ // attached the sibling part's figures — R 60-1's under a question about
74
+ // R 60-2 (measured 2026-12-05). The part comes from the understanding's
75
+ // normalized reference (the text scan keeps only the family); a
76
+ // family-level or absent reference changes nothing.
77
+ const namedLabel = namedDoc ? namedDoc.split(":")[0].trim().toUpperCase() : "";
78
+ const namedPart = namedLabel && /-\d|-[A-Z]\b/.test(namedLabel) ? namedLabel : "";
79
+ const ofNamedPart = (h: Hit) => !namedPart || String(h.metadata.docidentifier ?? "").toUpperCase().startsWith(namedPart);
71
80
  const docOf = new Map<string, UnitKey>();
72
81
  // natural prose names figures without any identifier at all — "Figure 3
73
82
  // of R 60-2 shows…" (the #172 residual): resolve those by number within
@@ -84,7 +93,7 @@ export async function completeFigures(
84
93
  // a hope that the model names an unnameable id
85
94
  if (query && /\bfig(ure|s)?\b/i.test(query)) {
86
95
  for (const h of used.slice(0, 12)) {
87
- if (h.metadata.unit_id && h.metadata.block === "figure") {
96
+ if (h.metadata.unit_id && h.metadata.block === "figure" && ofNamedPart(h)) {
88
97
  mentioned.push(h.metadata.unit_id);
89
98
  if (!docOf.has(h.metadata.unit_id)) docOf.set(h.metadata.unit_id, { unit_id: h.metadata.unit_id, doc_id: h.metadata.doc_id, docidentifier: h.metadata.docidentifier });
90
99
  }
@@ -92,7 +101,7 @@ export async function completeFigures(
92
101
  mentioned = [...new Set(mentioned)].slice(0, 6);
93
102
  }
94
103
  if (proseNums.size) {
95
- const fams = [...new Set(used.map((h) => h.metadata.docidentifier).filter(Boolean))].slice(0, 3);
104
+ const fams = [...new Set([namedPart, ...used.map((h) => h.metadata.docidentifier).filter(Boolean)].filter(Boolean))].slice(0, 3);
96
105
  for (const fam of fams) {
97
106
  const base = String(fam).replace(/\s*\([A-Z]\)\s*$/, "").split(":")[0].trim();
98
107
  const rows = await publicationUnitRows(db, "figure", `%${base}%`, 24);
@@ -23,18 +23,32 @@ export const figureSeat: Stage = {
23
23
  const dn = String((h.metadata as any).doc_number ?? "");
24
24
  if (dn) counts.set(dn, (counts.get(dn) ?? 0) + 1);
25
25
  }
26
- const docNumber = (c.u as any)?.doc_number ?? [...counts.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
27
- if (!docNumber) return;
26
+ // The asked publication at its own grain: every part of a family
27
+ // carries the family's doc_number (R 60-1, R 60-2 and R 60-A all say
28
+ // "60"), so a seat by number returned three of the family's eight
29
+ // figures regardless of the part the question named — "Figure 2 in
30
+ // OIML R 60-2" seated R 60-1's figures (measured 2026-12-05). A part
31
+ // reference is seated by its own identifier; a family reference keeps
32
+ // the number.
33
+ const namedDoc = String((c.u as any)?.docidentifier ?? "").trim();
34
+ const number = String((c.u as any)?.doc_number ?? "");
35
+ const byIdentifier = /-\d|-[A-Z]\b/.test(namedDoc.replace(/:\d{4}.*$/, ""));
36
+ const scope = byIdentifier ? namedDoc : number || [...counts.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
37
+ if (!scope) return;
28
38
  const seated = new Set(
29
39
  c.finalHits.filter((h: Hit) => (h.metadata as any).block === "figure").map((h: Hit) => String((h.metadata as any).unit_id ?? h.id)),
30
40
  );
31
- const rows = await portStore(c.env)
32
- .prepare(
33
- `SELECT id, doc_id, docidentifier, clause_anchor, clause_title, doc_number, edition, language, status, corpus, tier, text, unit_id
34
- FROM chunks WHERE doc_number = ?1 AND block = 'figure' AND unit_id IS NOT NULL LIMIT 3`,
35
- )
36
- .bind(String(docNumber))
37
- .all();
41
+ const cols =
42
+ "id, doc_id, docidentifier, clause_anchor, clause_title, doc_number, edition, language, status, corpus, tier, text, unit_id";
43
+ const store = portStore(c.env);
44
+ let rows = byIdentifier
45
+ ? await store.prepare(`SELECT ${cols} FROM chunks WHERE docidentifier LIKE ?1 AND block = 'figure' AND unit_id IS NOT NULL ORDER BY clause_anchor LIMIT 3`).bind(`%${namedDoc}`).all()
46
+ : await store.prepare(`SELECT ${cols} FROM chunks WHERE doc_number = ?1 AND block = 'figure' AND unit_id IS NOT NULL LIMIT 3`).bind(String(scope)).all();
47
+ if (byIdentifier && !(rows.results ?? []).length && number) {
48
+ // an identifier spelling the chunks do not carry — the family's
49
+ // figures stand in rather than none
50
+ rows = await store.prepare(`SELECT ${cols} FROM chunks WHERE doc_number = ?1 AND block = 'figure' AND unit_id IS NOT NULL LIMIT 3`).bind(number).all();
51
+ }
38
52
  const fresh: Hit[] = [];
39
53
  for (const r of (rows.results ?? []) as any[]) {
40
54
  if (seated.has(String(r.unit_id))) continue;
@@ -46,7 +60,7 @@ export const figureSeat: Stage = {
46
60
  metadata: {
47
61
  doc_id: String(r.doc_id ?? ""),
48
62
  docidentifier: String(r.docidentifier ?? ""),
49
- doc_number: String(r.doc_number ?? docNumber),
63
+ doc_number: String(r.doc_number ?? number ?? scope),
50
64
  clause_anchor: r.clause_anchor ? String(r.clause_anchor) : undefined,
51
65
  clause_title: r.clause_title ? String(r.clause_title) : undefined,
52
66
  edition: r.edition ? String(r.edition) : undefined,
@@ -61,7 +75,7 @@ export const figureSeat: Stage = {
61
75
  }
62
76
  if (!fresh.length) return;
63
77
  c.finalHits = [...fresh, ...c.finalHits];
64
- console.log("figure-seat: seated", fresh.length, "figure chunk(s) of doc", docNumber);
78
+ console.log("figure-seat: seated", fresh.length, "figure chunk(s) of", byIdentifier ? namedDoc : "doc " + scope, byIdentifier ? "(named)" : "");
65
79
  } catch {
66
80
  // additive — the text ranking stands
67
81
  }