@konneal/engine 0.2.167 → 0.2.169

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-YWXLXXGH.js";
3
+ } from "./chunk-NTZX65GT.js";
4
4
  import "./chunk-ECOYO7EX.js";
5
5
  import "./chunk-PTY2OSC4.js";
6
- import "./chunk-OD3KBF4I.js";
6
+ import "./chunk-LIZJLRXZ.js";
7
7
  import "./chunk-LU7VIMD7.js";
8
8
  import "./chunk-QY556RJM.js";
9
9
  import "./chunk-ETLSWSZ7.js";
@@ -6,7 +6,7 @@ import {
6
6
  sessionFrom,
7
7
  telemetry,
8
8
  understandQuery
9
- } from "./chunk-OD3KBF4I.js";
9
+ } from "./chunk-LIZJLRXZ.js";
10
10
  import {
11
11
  corsHeaders,
12
12
  err,
@@ -22994,6 +22994,59 @@ Which of these figures best answers the question? Reply with ONLY: {"pick": N} u
22994
22994
  }
22995
22995
  };
22996
22996
 
22997
+ // workers/worker_public/src/stages/figureSeat.ts
22998
+ var figureSeat = {
22999
+ name: "figure-seat",
23000
+ failure: "additive",
23001
+ when: (c) => FIGURE_Q.test(c.query),
23002
+ run: async (c) => {
23003
+ try {
23004
+ const counts = /* @__PURE__ */ new Map();
23005
+ for (const h of c.finalHits.slice(0, 5)) {
23006
+ const dn = String(h.metadata.doc_number ?? "");
23007
+ if (dn) counts.set(dn, (counts.get(dn) ?? 0) + 1);
23008
+ }
23009
+ const docNumber = c.u?.doc_number ?? [...counts.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
23010
+ if (!docNumber) return;
23011
+ const seated = new Set(
23012
+ c.finalHits.filter((h) => h.metadata.block === "figure").map((h) => String(h.metadata.unit_id ?? h.id))
23013
+ );
23014
+ const rows = await portStore(c.env).prepare(
23015
+ `SELECT id, doc_id, docidentifier, clause_anchor, clause_title, doc_number, edition, language, status, corpus, tier, text, unit_id
23016
+ FROM chunks WHERE doc_number = ?1 AND block = 'figure' AND unit_id IS NOT NULL LIMIT 3`
23017
+ ).bind(String(docNumber)).all();
23018
+ const fresh = [];
23019
+ for (const r of rows.results ?? []) {
23020
+ if (seated.has(String(r.unit_id))) continue;
23021
+ fresh.push({
23022
+ id: String(r.id),
23023
+ score: 0,
23024
+ rerank_score: 0,
23025
+ text: String(r.text ?? ""),
23026
+ metadata: {
23027
+ doc_id: String(r.doc_id ?? ""),
23028
+ docidentifier: String(r.docidentifier ?? ""),
23029
+ doc_number: String(r.doc_number ?? docNumber),
23030
+ clause_anchor: r.clause_anchor ? String(r.clause_anchor) : void 0,
23031
+ clause_title: r.clause_title ? String(r.clause_title) : void 0,
23032
+ edition: r.edition ? String(r.edition) : void 0,
23033
+ language: r.language ? String(r.language) : void 0,
23034
+ status: r.status ? String(r.status) : void 0,
23035
+ corpus: r.corpus ? String(r.corpus) : void 0,
23036
+ tier: r.tier ? String(r.tier) : void 0,
23037
+ block: "figure",
23038
+ unit_id: String(r.unit_id)
23039
+ }
23040
+ });
23041
+ }
23042
+ if (!fresh.length) return;
23043
+ c.finalHits = [...fresh, ...c.finalHits];
23044
+ console.log("figure-seat: seated", fresh.length, "figure chunk(s) of doc", docNumber);
23045
+ } catch {
23046
+ }
23047
+ }
23048
+ };
23049
+
22997
23050
  // workers/worker_public/src/stages/termNudge.ts
22998
23051
  var termNudge = {
22999
23052
  name: "term-nudge",
@@ -23492,6 +23545,7 @@ var STAGES = [
23492
23545
  typedPin,
23493
23546
  sectionDescent,
23494
23547
  dedup,
23548
+ figureSeat,
23495
23549
  vlmFigureRerank,
23496
23550
  windowFloor
23497
23551
  ];
@@ -75,7 +75,7 @@ import {
75
75
  tokenBudget,
76
76
  understandQuery,
77
77
  usageTotal
78
- } from "./chunk-OD3KBF4I.js";
78
+ } from "./chunk-LIZJLRXZ.js";
79
79
  import {
80
80
  embed,
81
81
  generateOnce
@@ -2785,12 +2785,21 @@ Answer account questions from these records ONLY: name the record when you use i
2785
2785
  }
2786
2786
  const canonical0 = exciseMisplacedPin(canonicalRefusal(full.replace(/^TOOL [a-z_.]+.*$\n?/gm, "").replace(/^\n+/, ""), refusalScope(answerCites(full))), answerCites(full));
2787
2787
  const c2 = canonical0.includes(refusalAnswer()) ? { text: canonical0, blocks: [], dropped: [] } : await contractV2(env.DB, canonical0, usedHits);
2788
+ let streamedCompletionBlocks = [];
2789
+ if (!canonical0.includes(refusalAnswer())) {
2790
+ try {
2791
+ streamedCompletionBlocks = await completeTables(env.DB, canonical0, usedHits);
2792
+ streamedCompletionBlocks.push(...await completeFigures(env.DB, canonical0, [...c2.blocks, ...streamedCompletionBlocks], usedHits, q2.query));
2793
+ if (streamedCompletionBlocks.length) console.log("streamed completion:", streamedCompletionBlocks.length, "block(s) attached");
2794
+ } catch {
2795
+ }
2796
+ }
2788
2797
  send({
2789
2798
  type: "done",
2790
2799
  model,
2791
2800
  query_hash: queryHash,
2792
2801
  follow_ups: refusalShaped(canonical0, refusalScope(answerCites(full))) ? [] : understanding?.follow_ups ?? [],
2793
- blocks: [...c2.blocks, ...verdictBlock ? [verdictBlock] : [], ...conditionBlock ? [conditionBlock] : [], ...aggregationBlock ? [aggregationBlock] : []],
2802
+ blocks: [...c2.blocks, ...verdictBlock ? [verdictBlock] : [], ...conditionBlock ? [conditionBlock] : [], ...aggregationBlock ? [aggregationBlock] : [], ...streamedCompletionBlocks],
2794
2803
  context_applied: ctxApplied,
2795
2804
  read: readAs(),
2796
2805
  // the evidence view's ground truth: the exact passages this
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  handleSearch
3
- } from "./chunk-KD2AHOOQ.js";
4
- import "./chunk-OD3KBF4I.js";
3
+ } from "./chunk-BFOCWZ7U.js";
4
+ import "./chunk-LIZJLRXZ.js";
5
5
  import "./chunk-LU7VIMD7.js";
6
6
  import "./chunk-QY556RJM.js";
7
7
  import "./chunk-ETLSWSZ7.js";
@@ -4,7 +4,7 @@ import {
4
4
  handleMemories,
5
5
  scoreFaithfulness,
6
6
  scoreJudge
7
- } from "../../chunk-YWXLXXGH.js";
7
+ } from "../../chunk-NTZX65GT.js";
8
8
  import "../../chunk-ECOYO7EX.js";
9
9
  import {
10
10
  TOOLS_REGISTRY,
@@ -13,7 +13,7 @@ import {
13
13
  } from "../../chunk-PTY2OSC4.js";
14
14
  import {
15
15
  handleSearch
16
- } from "../../chunk-KD2AHOOQ.js";
16
+ } from "../../chunk-BFOCWZ7U.js";
17
17
  import {
18
18
  buildMessages,
19
19
  citations,
@@ -38,7 +38,7 @@ import {
38
38
  sessionFrom,
39
39
  telemetry,
40
40
  understandQuery
41
- } from "../../chunk-OD3KBF4I.js";
41
+ } from "../../chunk-LIZJLRXZ.js";
42
42
  import {
43
43
  embed,
44
44
  generateOnce
@@ -1125,7 +1125,7 @@ async function callToolOnce(env, ctx, _req, tier, key, name, args, t0) {
1125
1125
  const r = await runTool({ ...env, DB: env.DB }, { name, args }, "mcp");
1126
1126
  payload = r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
1127
1127
  } else {
1128
- const res = name === "ask" ? await (await import("../../ask-LV6WNGYK.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-ZQCAISQV.js")).handleSearch(env, ctx, inner, tier, key);
1128
+ const res = name === "ask" ? await (await import("../../ask-OZO3KHFV.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-Y4Y46Y57.js")).handleSearch(env, ctx, inner, tier, key);
1129
1129
  payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
1130
1130
  }
1131
1131
  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.167",
30
+ "version": "0.2.169",
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",
@@ -1702,7 +1702,19 @@ let convEntities: Array<{ entity: string; kind: string }> = [];
1702
1702
  const c2 = canonical0.includes(refusalAnswer())
1703
1703
  ? { text: canonical0, blocks: [], dropped: [] as string[] }
1704
1704
  : await contractV2(env.DB, canonical0, usedHits);
1705
- send({ type: "done", model, query_hash: queryHash, follow_ups: refusalShaped(canonical0, refusalScope(answerCites(full))) ? [] : (understanding?.follow_ups ?? []), blocks: [...c2.blocks, ...(verdictBlock ? [verdictBlock] : []), ...(conditionBlock ? [conditionBlock] : []), ...(aggregationBlock ? [aggregationBlock] : [])], context_applied: ctxApplied, read: readAs(),
1705
+ // the completions ride the streamed lane too (parity: the UI's
1706
+ // bubble IS this lane — the table/figure blocks never reached it
1707
+ // before): additive D1 reads after the tokens are out, refusal
1708
+ // answers carry nothing to complete
1709
+ let streamedCompletionBlocks: Awaited<ReturnType<typeof completeTables>> = [];
1710
+ if (!canonical0.includes(refusalAnswer())) {
1711
+ try {
1712
+ streamedCompletionBlocks = await completeTables(env.DB, canonical0, usedHits);
1713
+ streamedCompletionBlocks.push(...(await completeFigures(env.DB, canonical0, [...c2.blocks, ...streamedCompletionBlocks], usedHits, q.query)));
1714
+ if (streamedCompletionBlocks.length) console.log("streamed completion:", streamedCompletionBlocks.length, "block(s) attached");
1715
+ } catch { /* additive — the answer stands without the blocks */ }
1716
+ }
1717
+ send({ type: "done", model, query_hash: queryHash, follow_ups: refusalShaped(canonical0, refusalScope(answerCites(full))) ? [] : (understanding?.follow_ups ?? []), blocks: [...c2.blocks, ...(verdictBlock ? [verdictBlock] : []), ...(conditionBlock ? [conditionBlock] : []), ...(aggregationBlock ? [aggregationBlock] : []), ...streamedCompletionBlocks], context_applied: ctxApplied, read: readAs(),
1706
1718
  // the evidence view's ground truth: the exact passages this
1707
1719
  // answer was built from, compact — cache hits carry none,
1708
1720
  // because the cache stores the answer and never the passages
@@ -0,0 +1,69 @@
1
+ // The figure seat (TODO.sota/18.3's completion — the guaranteed place):
2
+ // a figure-SEEKING question deterministically seats the asked document's
3
+ // own figure chunks in the window. No embedding contest, no variance —
4
+ // the question names a figure; the document's figures get their places;
5
+ // the VLM rerank then orders them by image and the seeking completion
6
+ // attaches the block from the producer's payload. The D1 read is one
7
+ // bounded query (doc_number + block, LIMIT 3), additive, ablatable like
8
+ // any stage. Positioned BEFORE the VLM rerank so the rerank sees the
9
+ // seated figures and can order them by image.
10
+ import { FIGURE_Q } from "./vlmFigureRerank.ts";
11
+ import { portStore } from "../env.ts";
12
+ import type { Stage } from "./types.ts";
13
+ import type { Hit } from "../../../shared/chunk";
14
+
15
+ export const figureSeat: Stage = {
16
+ name: "figure-seat",
17
+ failure: "additive",
18
+ when: (c) => FIGURE_Q.test(c.query),
19
+ run: async (c) => {
20
+ try {
21
+ const counts = new Map<string, number>();
22
+ for (const h of c.finalHits.slice(0, 5)) {
23
+ const dn = String((h.metadata as any).doc_number ?? "");
24
+ if (dn) counts.set(dn, (counts.get(dn) ?? 0) + 1);
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;
28
+ const seated = new Set(
29
+ c.finalHits.filter((h: Hit) => (h.metadata as any).block === "figure").map((h: Hit) => String((h.metadata as any).unit_id ?? h.id)),
30
+ );
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();
38
+ const fresh: Hit[] = [];
39
+ for (const r of (rows.results ?? []) as any[]) {
40
+ if (seated.has(String(r.unit_id))) continue;
41
+ fresh.push({
42
+ id: String(r.id),
43
+ score: 0,
44
+ rerank_score: 0,
45
+ text: String(r.text ?? ""),
46
+ metadata: {
47
+ doc_id: String(r.doc_id ?? ""),
48
+ docidentifier: String(r.docidentifier ?? ""),
49
+ doc_number: String(r.doc_number ?? docNumber),
50
+ clause_anchor: r.clause_anchor ? String(r.clause_anchor) : undefined,
51
+ clause_title: r.clause_title ? String(r.clause_title) : undefined,
52
+ edition: r.edition ? String(r.edition) : undefined,
53
+ language: r.language ? String(r.language) : undefined,
54
+ status: r.status ? String(r.status) : undefined,
55
+ corpus: r.corpus ? String(r.corpus) : undefined,
56
+ tier: r.tier ? String(r.tier) : undefined,
57
+ block: "figure",
58
+ unit_id: String(r.unit_id),
59
+ } as any,
60
+ });
61
+ }
62
+ if (!fresh.length) return;
63
+ c.finalHits = [...fresh, ...c.finalHits];
64
+ console.log("figure-seat: seated", fresh.length, "figure chunk(s) of doc", docNumber);
65
+ } catch {
66
+ // additive — the text ranking stands
67
+ }
68
+ },
69
+ };
@@ -34,6 +34,7 @@ import { overviewDemote } from "./overviewDemote.ts";
34
34
  import { familyBoost } from "./familyBoost.ts";
35
35
  import { rerankStage, lexicalRrf } from "./rerank.ts";
36
36
  import { vlmFigureRerank } from "./vlmFigureRerank.ts";
37
+ import { figureSeat } from "./figureSeat.ts";
37
38
  import { termNudge } from "./termNudge.ts";
38
39
  import { conceptSteer } from "./conceptSteer.ts";
39
40
  import { editionSteer } from "./editionSteer.ts";
@@ -76,6 +77,7 @@ export const STAGES: Stage[] = [
76
77
  typedPin,
77
78
  sectionDescent,
78
79
  dedup,
80
+ figureSeat,
79
81
  vlmFigureRerank,
80
82
  windowFloor,
81
83
  ];