@konneal/engine 0.2.115 → 0.2.116

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-MUAY4BXY.js";
3
+ } from "./chunk-YVNY4JWP.js";
4
4
  import "./chunk-32UDKBA2.js";
5
- import "./chunk-A6YQYN3O.js";
5
+ import "./chunk-PJ3ET5AY.js";
6
6
  import "./chunk-TH5XGPD5.js";
7
7
  import "./chunk-DJ65EF5G.js";
8
8
  import "./chunk-A3QHHUN5.js";
@@ -22694,15 +22694,34 @@ var federate = {
22694
22694
  };
22695
22695
 
22696
22696
  // workers/worker_public/src/stages/seal.ts
22697
+ async function familyChunks(env, familyNumber, query, keep) {
22698
+ const rows = await portStore(env).prepare(
22699
+ "SELECT id, doc_id, docidentifier, doctype, doc_number, edition, language, clause_anchor, clause_title, status, superseded_by, corpus, tier, text, unit_id, block, bm25(chunks_fts) AS rank FROM chunks_fts JOIN chunks c ON c.rowid = chunks_fts.rowid WHERE chunks_fts MATCH ?1 AND c.doc_number = ?2 ORDER BY rank LIMIT ?3"
22700
+ ).bind(query.replace(/["'^]/g, " ").trim() || familyNumber, familyNumber, keep).all().catch(() => ({ results: [] }));
22701
+ return (rows.results ?? []).map((r) => ({
22702
+ id: String(r.id),
22703
+ score: 0,
22704
+ metadata: r,
22705
+ text: String(r.text ?? "")
22706
+ }));
22707
+ }
22697
22708
  var seal = {
22698
22709
  name: "seal",
22699
22710
  when: (c) => !!c.opts.sealScope || !!c.opts.editionSteer || !!c.opts.editionExclude,
22700
- run: (c) => {
22711
+ run: async (c) => {
22701
22712
  const before = c.hits.length;
22702
22713
  const scope = c.opts.sealScope;
22703
22714
  if (scope) {
22704
22715
  c.hits = c.hits.filter((h) => matchesDocScope(h.metadata, scope.doc_number) && (!scope.edition || h.metadata.edition === scope.edition));
22705
22716
  console.log("context seal:", before, "\u2192", c.hits.length, "candidates within", `doc#${scope.doc_number}${scope.edition ? "@" + scope.edition : ""}`);
22717
+ if (c.hits.length === 0) {
22718
+ const familyNumber = scope.doc_number.includes("-") ? scope.doc_number.split("-")[0] : scope.doc_number;
22719
+ const pulled = (await familyChunks(c.env, familyNumber, c.rq || c.query, 12)).filter((h) => matchesDocScope(h.metadata, scope.doc_number) && (!scope.edition || h.metadata.edition === scope.edition)).slice(0, 8).map((h) => ({ ...h, score: 0.5 }));
22720
+ if (pulled.length) {
22721
+ c.hits = pulled;
22722
+ console.log("context seal: pool empty \u2014 pulled", pulled.length, "family chunks from the corpus for", `doc#${scope.doc_number}`);
22723
+ }
22724
+ }
22706
22725
  return;
22707
22726
  }
22708
22727
  const exclude = c.opts.editionExclude;
@@ -6,7 +6,7 @@ import {
6
6
  sessionFrom,
7
7
  telemetry,
8
8
  understandQuery
9
- } from "./chunk-A6YQYN3O.js";
9
+ } from "./chunk-PJ3ET5AY.js";
10
10
  import {
11
11
  portModelRunner
12
12
  } from "./chunk-TH5XGPD5.js";
@@ -60,7 +60,7 @@ import {
60
60
  tokenBudget,
61
61
  understandQuery,
62
62
  usageTotal
63
- } from "./chunk-A6YQYN3O.js";
63
+ } from "./chunk-PJ3ET5AY.js";
64
64
  import {
65
65
  embed,
66
66
  generateOnce,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  handleSearch
3
- } from "./chunk-ZD7SNNGW.js";
4
- import "./chunk-A6YQYN3O.js";
3
+ } from "./chunk-XWT3T3T3.js";
4
+ import "./chunk-PJ3ET5AY.js";
5
5
  import "./chunk-TH5XGPD5.js";
6
6
  import "./chunk-DJ65EF5G.js";
7
7
  import "./chunk-A3QHHUN5.js";
@@ -4,7 +4,7 @@ import {
4
4
  handleMemories,
5
5
  scoreFaithfulness,
6
6
  scoreJudge
7
- } from "../../chunk-MUAY4BXY.js";
7
+ } from "../../chunk-YVNY4JWP.js";
8
8
  import {
9
9
  TOOLS_REGISTRY,
10
10
  bindModelNode,
@@ -12,7 +12,7 @@ import {
12
12
  } from "../../chunk-32UDKBA2.js";
13
13
  import {
14
14
  handleSearch
15
- } from "../../chunk-ZD7SNNGW.js";
15
+ } from "../../chunk-XWT3T3T3.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-A6YQYN3O.js";
37
+ } from "../../chunk-PJ3ET5AY.js";
38
38
  import {
39
39
  cfBlobs,
40
40
  embed,
@@ -1000,7 +1000,7 @@ async function callToolOnce(env, ctx, _req, tier, key, name, args, t0) {
1000
1000
  const r = await runTool({ ...env, DB: env.DB }, { name, args }, "mcp");
1001
1001
  payload = r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
1002
1002
  } else {
1003
- const res = name === "ask" ? await (await import("../../ask-RLMNWTHA.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-AH4A4SFJ.js")).handleSearch(env, ctx, inner, tier, key);
1003
+ const res = name === "ask" ? await (await import("../../ask-QDV37DDB.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-AXP7F365.js")).handleSearch(env, ctx, inner, tier, key);
1004
1004
  payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
1005
1005
  }
1006
1006
  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.115",
30
+ "version": "0.2.116",
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",
@@ -3,17 +3,53 @@
3
3
  // outside the declared family competes for the window; everything
4
4
  // inside it does.
5
5
  import { matchesDocScope } from "../context.ts";
6
+ import { portStore } from "../env.ts";
7
+ import type { ChunkMeta, Hit } from "../../../shared/chunk";
6
8
  import type { Stage } from "./types.ts";
7
9
 
10
+ /** The sealed family's chunks, from the D1 corpus directly (the l6a
11
+ * law): the declared context's guarantee is that the publication CAN
12
+ * answer — a generic question's global ranking need not contain the
13
+ * family at all, so an emptied pool pulls the family's own text. */
14
+ async function familyChunks(env: any, familyNumber: string, query: string, keep: number): Promise<Hit[]> {
15
+ const rows = await portStore(env).prepare(
16
+ "SELECT id, doc_id, docidentifier, doctype, doc_number, edition, language, clause_anchor, clause_title, status, superseded_by, corpus, tier, text, unit_id, block, bm25(chunks_fts) AS rank FROM chunks_fts JOIN chunks c ON c.rowid = chunks_fts.rowid WHERE chunks_fts MATCH ?1 AND c.doc_number = ?2 ORDER BY rank LIMIT ?3",
17
+ )
18
+ .bind(query.replace(/["'^]/g, " ").trim() || familyNumber, familyNumber, keep)
19
+ .all()
20
+ .catch(() => ({ results: [] }));
21
+ return (rows.results ?? []).map((r: any) => ({
22
+ id: String(r.id),
23
+ score: 0,
24
+ metadata: r as unknown as ChunkMeta,
25
+ text: String(r.text ?? ""),
26
+ }));
27
+ }
28
+
8
29
  export const seal: Stage = {
9
30
  name: "seal",
10
31
  when: (c) => !!c.opts.sealScope || !!c.opts.editionSteer || !!c.opts.editionExclude,
11
- run: (c) => {
32
+ run: async (c) => {
12
33
  const before = c.hits.length;
13
34
  const scope = c.opts.sealScope!;
14
35
  if (scope) {
15
36
  c.hits = c.hits.filter((h) => matchesDocScope(h.metadata, scope.doc_number) && (!scope.edition || h.metadata.edition === scope.edition));
16
37
  console.log("context seal:", before, "→", c.hits.length, "candidates within", `doc#${scope.doc_number}${scope.edition ? "@" + scope.edition : ""}`);
38
+ if (c.hits.length === 0) {
39
+ // the declared-context guarantee: the family's own text enters
40
+ // even when no global ranking carried it — the FTS ranking over
41
+ // the family grain (the part identity steers by docidentifier
42
+ // downstream, the same grain law matchesDocScope enforces)
43
+ const familyNumber = scope.doc_number.includes("-") ? scope.doc_number.split("-")[0] : scope.doc_number;
44
+ const pulled = (await familyChunks(c.env, familyNumber, c.rq || c.query, 12))
45
+ .filter((h) => matchesDocScope(h.metadata, scope.doc_number) && (!scope.edition || h.metadata.edition === scope.edition))
46
+ .slice(0, 8)
47
+ .map((h) => ({ ...h, score: 0.5 }));
48
+ if (pulled.length) {
49
+ c.hits = pulled;
50
+ console.log("context seal: pool empty — pulled", pulled.length, "family chunks from the corpus for", `doc#${scope.doc_number}`);
51
+ }
52
+ }
17
53
  return;
18
54
  }
19
55
  const exclude = c.opts.editionExclude!;