@konneal/engine 0.2.164 → 0.2.166

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.
@@ -0,0 +1,16 @@
1
+ import {
2
+ handleAsk
3
+ } from "./chunk-2FME6R6J.js";
4
+ import "./chunk-ECOYO7EX.js";
5
+ import "./chunk-AP73VIHB.js";
6
+ import "./chunk-5AME76UW.js";
7
+ import "./chunk-5V2A3CAH.js";
8
+ import "./chunk-TTEJQ5XA.js";
9
+ import "./chunk-ETLSWSZ7.js";
10
+ import "./chunk-OLO45N6D.js";
11
+ import "./chunk-FQYHT4VC.js";
12
+ import "./chunk-3FYJM7LH.js";
13
+ import "./chunk-LSZTJI7V.js";
14
+ export {
15
+ handleAsk
16
+ };
@@ -32,7 +32,7 @@ import {
32
32
  tableRetyped,
33
33
  toolNote,
34
34
  verdictNote
35
- } from "./chunk-K5WL3DKH.js";
35
+ } from "./chunk-AP73VIHB.js";
36
36
  import {
37
37
  NO_CONTEXT,
38
38
  STAGE_NAMES,
@@ -75,18 +75,18 @@ import {
75
75
  tokenBudget,
76
76
  understandQuery,
77
77
  usageTotal
78
- } from "./chunk-E74MNLBC.js";
78
+ } from "./chunk-5AME76UW.js";
79
79
  import {
80
80
  embed,
81
81
  generateOnce
82
- } from "./chunk-FHRGC6TP.js";
82
+ } from "./chunk-5V2A3CAH.js";
83
83
  import {
84
84
  corsHeaders,
85
85
  err,
86
86
  json,
87
87
  readJson,
88
88
  validateQuery
89
- } from "./chunk-GJATVGBS.js";
89
+ } from "./chunk-TTEJQ5XA.js";
90
90
  import {
91
91
  cacheableAnswer,
92
92
  canonicalRefusal,
@@ -99,7 +99,7 @@ import {
99
99
  requestSalt,
100
100
  resolveRequestScope,
101
101
  standardKeysFrom
102
- } from "./chunk-D6UUAL7I.js";
102
+ } from "./chunk-OLO45N6D.js";
103
103
  import {
104
104
  LIMITS,
105
105
  MODELS,
@@ -110,7 +110,7 @@ import {
110
110
  requestEffort,
111
111
  roleModel,
112
112
  sha256Hex
113
- } from "./chunk-ZD6TYLET.js";
113
+ } from "./chunk-FQYHT4VC.js";
114
114
  import {
115
115
  P
116
116
  } from "./chunk-3FYJM7LH.js";
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  embed,
3
3
  rerank
4
- } from "./chunk-FHRGC6TP.js";
4
+ } from "./chunk-5V2A3CAH.js";
5
5
  import {
6
6
  bubbleConfirmPage,
7
7
  isAllowedBubbleOrigin
8
- } from "./chunk-GJATVGBS.js";
8
+ } from "./chunk-TTEJQ5XA.js";
9
9
  import {
10
10
  DATASETS,
11
11
  LIMITS,
@@ -15,12 +15,13 @@ import {
15
15
  processExpansion,
16
16
  sha256Hex,
17
17
  today
18
- } from "./chunk-ZD6TYLET.js";
18
+ } from "./chunk-FQYHT4VC.js";
19
19
  import {
20
20
  P
21
21
  } from "./chunk-3FYJM7LH.js";
22
22
  import {
23
23
  hasLane,
24
+ portBlobs,
24
25
  portIndex,
25
26
  portModelRunner,
26
27
  portStore
@@ -22163,6 +22164,11 @@ var dense = {
22163
22164
  matches = matches.filter((m) => matchesDocScope(m.metadata ?? {}, filters.doc_number));
22164
22165
  if (filters.edition) matches = matches.filter((m) => !m.metadata?.edition || String(m.metadata.edition) === filters.edition);
22165
22166
  console.log("dense scope (in code):", before, "\u2192", matches.length, "in doc", filters.doc_number, filters.edition ? `@${filters.edition}` : "");
22167
+ if (!matches.length) {
22168
+ const wide = await env.VECTORIZE.query(vector, { ...q, topK: THRESHOLDS.denseStarveK });
22169
+ matches = (wide.matches ?? []).filter((m) => matchesDocScope(m.metadata ?? {}, filters.doc_number));
22170
+ console.log("dense scope starvation \u2192 deep re-query:", matches.length, "doc members in the top", THRESHOLDS.denseStarveK);
22171
+ }
22166
22172
  if (filters && filters.edition && matches.length < 3) {
22167
22173
  const docOnly = allMatches.filter((m) => matchesDocScope(m.metadata ?? {}, filters.doc_number));
22168
22174
  if (docOnly.length > matches.length) {
@@ -22921,6 +22927,69 @@ var lexicalRrf = {
22921
22927
  }
22922
22928
  };
22923
22929
 
22930
+ // workers/worker_public/src/stages/vlmFigureRerank.ts
22931
+ var FIGURE_Q = /\bfig(ure|s)?\b/i;
22932
+ function parsePick(text, kept) {
22933
+ const t = text;
22934
+ const s8 = typeof text === "string" ? text : typeof t?.response === "string" ? t.response : t?.choices?.[0]?.message?.content ?? "";
22935
+ const pick = Number(String(s8).match(/"pick"\s*:\s*(\d+)/)?.[1] ?? 0);
22936
+ return pick >= 1 && pick <= kept ? pick : 0;
22937
+ }
22938
+ var vlmFigureRerank = {
22939
+ name: "vlm-figure-rerank",
22940
+ failure: "additive",
22941
+ when: (c) => FIGURE_Q.test(c.query) && c.finalHits.some((h) => h.metadata.block === "figure" && h.metadata.unit_id),
22942
+ run: async (c) => {
22943
+ const figures = c.finalHits.filter((h) => h.metadata.block === "figure" && h.metadata.unit_id).slice(0, 3);
22944
+ if (!figures.length) return;
22945
+ try {
22946
+ const ids = figures.map((h) => String(h.metadata.unit_id));
22947
+ const ph = ids.map((_, i) => `?${i + 1}`).join(",");
22948
+ const rows = await portStore(c.env).prepare(
22949
+ `SELECT unit_id, json_extract(payload,'$.uri') AS uri FROM unit_payloads WHERE unit_id IN (${ph})`
22950
+ ).bind(...ids).all();
22951
+ const uris = new Map((rows.results ?? []).map((r) => [r.unit_id, r.uri]));
22952
+ const content = [
22953
+ {
22954
+ type: "text",
22955
+ text: `Question: ${c.query}
22956
+
22957
+ Which of these figures best answers the question? Reply with ONLY: {"pick": N} using the image's number, or {"pick": 0} if none is relevant.`
22958
+ }
22959
+ ];
22960
+ const kept = [];
22961
+ let n = 0;
22962
+ for (const h of figures) {
22963
+ const uri = String(uris.get(String(h.metadata.unit_id)) ?? "");
22964
+ const m = uri.match(/^\/assets\/(.+)/);
22965
+ if (!m) continue;
22966
+ const obj = await portBlobs(c.env).get(m[1]);
22967
+ if (!obj?.body) continue;
22968
+ const buf = new Uint8Array(await new Response(obj.body).arrayBuffer());
22969
+ let binary = "";
22970
+ for (let i = 0; i < buf.length; i += 8192) binary += String.fromCharCode(...buf.subarray(i, i + 8192));
22971
+ n += 1;
22972
+ content.push({ type: "text", text: `Image ${n}:` });
22973
+ content.push({ type: "image_url", image_url: { url: `data:image/png;base64,${btoa(binary)}` } });
22974
+ kept.push({ h, label: n });
22975
+ }
22976
+ if (!kept.length) return;
22977
+ const raced = await Promise.race([
22978
+ portModelRunner(c.env).run({ model: MODELS.vlmFigure, messages: [{ role: "user", content }], maxTokens: 256, effort: "low" }),
22979
+ new Promise((r) => setTimeout(() => r(null), 15e3))
22980
+ ]);
22981
+ const pick = parsePick(raced?.text ?? raced, kept.length);
22982
+ const chosen = kept.find((k) => k.label === pick);
22983
+ if (!chosen) return;
22984
+ const top = Math.max(...c.finalHits.map((h) => h.rerank_score ?? h.score ?? 0));
22985
+ chosen.h.rerank_score = top + 1;
22986
+ c.finalHits = [chosen.h, ...c.finalHits.filter((h) => h !== chosen.h)];
22987
+ console.log("vlm-figure-rerank: promoted", chosen.h.metadata.unit_id, "over", c.finalHits.length - 1, "text-ranked hits");
22988
+ } catch {
22989
+ }
22990
+ }
22991
+ };
22992
+
22924
22993
  // workers/worker_public/src/stages/termNudge.ts
22925
22994
  var termNudge = {
22926
22995
  name: "term-nudge",
@@ -23419,6 +23488,7 @@ var STAGES = [
23419
23488
  typedPin,
23420
23489
  sectionDescent,
23421
23490
  dedup,
23491
+ vlmFigureRerank,
23422
23492
  windowFloor
23423
23493
  ];
23424
23494
  var STAGE_NAMES = STAGES.map((s8) => s8.name);
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  answerEffort,
3
3
  effortBudget
4
- } from "./chunk-ZD6TYLET.js";
4
+ } from "./chunk-FQYHT4VC.js";
5
5
 
6
6
  // workers/worker_public/src/ai.ts
7
7
  var delay = (ms) => new Promise((r) => setTimeout(r, ms));
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  embed
3
- } from "./chunk-FHRGC6TP.js";
3
+ } from "./chunk-5V2A3CAH.js";
4
4
  import {
5
5
  standardKeysFrom
6
- } from "./chunk-D6UUAL7I.js";
6
+ } from "./chunk-OLO45N6D.js";
7
7
  import {
8
8
  THRESHOLDS
9
- } from "./chunk-ZD6TYLET.js";
9
+ } from "./chunk-FQYHT4VC.js";
10
10
  import {
11
11
  P
12
12
  } from "./chunk-3FYJM7LH.js";
@@ -11,6 +11,10 @@ var MODELS = {
11
11
  // Vision image-parts land with answer contract v2; text answers work now.
12
12
  anon: "@cf/zai-org/glm-5.3-flash",
13
13
  member: "@cf/zai-org/glm-5.3-flash",
14
+ // the VLM figure rerank (TODO.sota/18.3): the newly-landed
15
+ // vision-capable MoE scores figure IMAGES against figure-seeking
16
+ // questions — one bounded call per figure question, ablatable stage
17
+ vlmFigure: "@cf/deepseek-ai/deepseek-v4.1-flash-1017",
14
18
  // hot-path understanding/summarize stays on the cheap Qwen (cost-first lane)
15
19
  understand: "@cf/qwen/qwen3-30b-a3b-fp8",
16
20
  // the api_call act's intent-to-act mapping (TODO.ai-platform/09) — the
@@ -84,6 +88,11 @@ var THRESHOLDS = {
84
88
  // the extractive compression arm (TODO.sota/02 row 5): when ablated
85
89
  // on, each passage body is compressed to this share of its budget
86
90
  compressShare: 0.6,
91
+ // the dense scope's starvation fallback (the R 134-2 figure question):
92
+ // when the in-code doc scope cuts the vector-level top-K to zero, the
93
+ // re-query runs at this depth and scopes in code (the native filter is
94
+ // dead — measured 2026-10-05)
95
+ denseStarveK: 1e3,
87
96
  entailmentGate: true,
88
97
  entailmentBudgetMs: 6e3,
89
98
  entailmentSupportedFloor: 0.9,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  DATASETS,
3
3
  datasetAllowed
4
- } from "./chunk-ZD6TYLET.js";
4
+ } from "./chunk-FQYHT4VC.js";
5
5
  import {
6
6
  P
7
7
  } from "./chunk-3FYJM7LH.js";
@@ -6,24 +6,24 @@ import {
6
6
  sessionFrom,
7
7
  telemetry,
8
8
  understandQuery
9
- } from "./chunk-E74MNLBC.js";
9
+ } from "./chunk-5AME76UW.js";
10
10
  import {
11
11
  corsHeaders,
12
12
  err,
13
13
  json,
14
14
  readJson,
15
15
  validateQuery
16
- } from "./chunk-GJATVGBS.js";
16
+ } from "./chunk-TTEJQ5XA.js";
17
17
  import {
18
18
  entitlementScope,
19
19
  standardKeysFrom
20
- } from "./chunk-D6UUAL7I.js";
20
+ } from "./chunk-OLO45N6D.js";
21
21
  import {
22
22
  LIMITS,
23
23
  MODELS,
24
24
  num,
25
25
  sha256Hex
26
- } from "./chunk-ZD6TYLET.js";
26
+ } from "./chunk-FQYHT4VC.js";
27
27
  import {
28
28
  portModelRunner
29
29
  } from "./chunk-LSZTJI7V.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  LIMITS,
3
3
  sha256Hex
4
- } from "./chunk-ZD6TYLET.js";
4
+ } from "./chunk-FQYHT4VC.js";
5
5
  import {
6
6
  P
7
7
  } from "./chunk-3FYJM7LH.js";
package/dist/config.d.ts CHANGED
@@ -3,6 +3,7 @@ export declare const MODELS: {
3
3
  readonly rerank: "@cf/baai/bge-reranker-base";
4
4
  readonly anon: "@cf/zai-org/glm-5.3-flash";
5
5
  readonly member: "@cf/zai-org/glm-5.3-flash";
6
+ readonly vlmFigure: "@cf/deepseek-ai/deepseek-v4.1-flash-1017";
6
7
  readonly understand: "@cf/qwen/qwen3-30b-a3b-fp8";
7
8
  readonly acts: "@cf/qwen/qwen3-30b-a3b-fp8";
8
9
  readonly fallback: "@cf/qwen/qwen3-30b-a3b-fp8";
@@ -50,6 +51,7 @@ export declare const THRESHOLDS: {
50
51
  * not only the source rung. Fails open (a slow or unparseable check
51
52
  * leaves the source-rung note unchanged). */
52
53
  readonly compressShare: 0.6;
54
+ readonly denseStarveK: 1000;
53
55
  readonly entailmentGate: true;
54
56
  readonly entailmentBudgetMs: 6000;
55
57
  readonly entailmentSupportedFloor: 0.9;
@@ -0,0 +1,14 @@
1
+ import {
2
+ handleSearch
3
+ } from "./chunk-SIT4XSD5.js";
4
+ import "./chunk-5AME76UW.js";
5
+ import "./chunk-5V2A3CAH.js";
6
+ import "./chunk-TTEJQ5XA.js";
7
+ import "./chunk-ETLSWSZ7.js";
8
+ import "./chunk-OLO45N6D.js";
9
+ import "./chunk-FQYHT4VC.js";
10
+ import "./chunk-3FYJM7LH.js";
11
+ import "./chunk-LSZTJI7V.js";
12
+ export {
13
+ handleSearch
14
+ };
@@ -4,10 +4,10 @@ import {
4
4
  parseToolCall,
5
5
  runTool,
6
6
  toolNote
7
- } from "./chunk-K5WL3DKH.js";
8
- import "./chunk-FHRGC6TP.js";
9
- import "./chunk-D6UUAL7I.js";
10
- import "./chunk-ZD6TYLET.js";
7
+ } from "./chunk-AP73VIHB.js";
8
+ import "./chunk-5V2A3CAH.js";
9
+ import "./chunk-OLO45N6D.js";
10
+ import "./chunk-FQYHT4VC.js";
11
11
  import "./chunk-3FYJM7LH.js";
12
12
  import "./chunk-LSZTJI7V.js";
13
13
  export {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  authenticate
3
- } from "../../chunk-GJATVGBS.js";
4
- import "../../chunk-ZD6TYLET.js";
3
+ } from "../../chunk-TTEJQ5XA.js";
4
+ import "../../chunk-FQYHT4VC.js";
5
5
  import {
6
6
  P,
7
7
  setProfile
@@ -16,7 +16,7 @@ import {
16
16
  roleModel,
17
17
  sha256Hex,
18
18
  today
19
- } from "../../chunk-ZD6TYLET.js";
19
+ } from "../../chunk-FQYHT4VC.js";
20
20
  import "../../chunk-3FYJM7LH.js";
21
21
  export {
22
22
  DATASETS,
@@ -4,16 +4,16 @@ import {
4
4
  handleMemories,
5
5
  scoreFaithfulness,
6
6
  scoreJudge
7
- } from "../../chunk-GVCOGEJC.js";
7
+ } from "../../chunk-2FME6R6J.js";
8
8
  import "../../chunk-ECOYO7EX.js";
9
9
  import {
10
10
  TOOLS_REGISTRY,
11
11
  bindModelNode,
12
12
  modelGroundingBlock
13
- } from "../../chunk-K5WL3DKH.js";
13
+ } from "../../chunk-AP73VIHB.js";
14
14
  import {
15
15
  handleSearch
16
- } from "../../chunk-K7UGBTBZ.js";
16
+ } from "../../chunk-SIT4XSD5.js";
17
17
  import {
18
18
  buildMessages,
19
19
  citations,
@@ -38,11 +38,11 @@ import {
38
38
  sessionFrom,
39
39
  telemetry,
40
40
  understandQuery
41
- } from "../../chunk-E74MNLBC.js";
41
+ } from "../../chunk-5AME76UW.js";
42
42
  import {
43
43
  embed,
44
44
  generateOnce
45
- } from "../../chunk-FHRGC6TP.js";
45
+ } from "../../chunk-5V2A3CAH.js";
46
46
  import {
47
47
  authenticate,
48
48
  corsHeaders,
@@ -51,14 +51,14 @@ import {
51
51
  readJson,
52
52
  validateQuery,
53
53
  withCors
54
- } from "../../chunk-GJATVGBS.js";
54
+ } from "../../chunk-TTEJQ5XA.js";
55
55
  import {
56
56
  canonicalRefusal
57
57
  } from "../../chunk-ETLSWSZ7.js";
58
58
  import {
59
59
  entitlementScope,
60
60
  standardKeysFrom
61
- } from "../../chunk-D6UUAL7I.js";
61
+ } from "../../chunk-OLO45N6D.js";
62
62
  import {
63
63
  LIMITS,
64
64
  MODELS,
@@ -69,7 +69,7 @@ import {
69
69
  roleModel,
70
70
  sha256Hex,
71
71
  today
72
- } from "../../chunk-ZD6TYLET.js";
72
+ } from "../../chunk-FQYHT4VC.js";
73
73
  import {
74
74
  P,
75
75
  setProfile
@@ -1119,13 +1119,13 @@ async function callToolOnce(env, ctx, _req, tier, key, name, args, t0) {
1119
1119
  // stream:false forces the JSON lane (anon defaults to SSE)
1120
1120
  body: JSON.stringify({ ...args, stream: false })
1121
1121
  });
1122
- const { runTool, TOOLS_REGISTRY: TOOLS_REGISTRY2 } = await import("../../tools-JZY6GSYC.js");
1122
+ const { runTool, TOOLS_REGISTRY: TOOLS_REGISTRY2 } = await import("../../tools-PR7PFAV6.js");
1123
1123
  let payload;
1124
1124
  if (TOOLS_REGISTRY2.some((t) => t.name === name && t.audiences.includes("mcp"))) {
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-B7XXKYZP.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-IXNVNOOY.js")).handleSearch(env, ctx, inner, tier, key);
1128
+ const res = name === "ask" ? await (await import("../../ask-XJOOMAXU.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-6KM4P2E5.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 });
@@ -4,8 +4,8 @@ import {
4
4
  requestSalt,
5
5
  resolveRequestScope,
6
6
  standardKeysFrom
7
- } from "../../chunk-D6UUAL7I.js";
8
- import "../../chunk-ZD6TYLET.js";
7
+ } from "../../chunk-OLO45N6D.js";
8
+ import "../../chunk-FQYHT4VC.js";
9
9
  import "../../chunk-3FYJM7LH.js";
10
10
  export {
11
11
  entitlementScope,
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.164",
30
+ "version": "0.2.166",
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",
@@ -7,6 +7,10 @@ export const MODELS = {
7
7
  // Vision image-parts land with answer contract v2; text answers work now.
8
8
  anon: "@cf/zai-org/glm-5.3-flash",
9
9
  member: "@cf/zai-org/glm-5.3-flash",
10
+ // the VLM figure rerank (TODO.sota/18.3): the newly-landed
11
+ // vision-capable MoE scores figure IMAGES against figure-seeking
12
+ // questions — one bounded call per figure question, ablatable stage
13
+ vlmFigure: "@cf/deepseek-ai/deepseek-v4.1-flash-1017",
10
14
  // hot-path understanding/summarize stays on the cheap Qwen (cost-first lane)
11
15
  understand: "@cf/qwen/qwen3-30b-a3b-fp8",
12
16
  // the api_call act's intent-to-act mapping (TODO.ai-platform/09) — the
@@ -81,6 +85,11 @@ export const THRESHOLDS = {
81
85
  // the extractive compression arm (TODO.sota/02 row 5): when ablated
82
86
  // on, each passage body is compressed to this share of its budget
83
87
  compressShare: 0.6,
88
+ // the dense scope's starvation fallback (the R 134-2 figure question):
89
+ // when the in-code doc scope cuts the vector-level top-K to zero, the
90
+ // re-query runs at this depth and scopes in code (the native filter is
91
+ // dead — measured 2026-10-05)
92
+ denseStarveK: 1000,
84
93
  entailmentGate: true,
85
94
  entailmentBudgetMs: 6000,
86
95
  entailmentSupportedFloor: 0.9,
@@ -3,7 +3,7 @@
3
3
  // query, no filter — bit-for-bit same candidates, one serial round-trip
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
- import { LIMITS } from "../config.ts";
6
+ import { LIMITS, THRESHOLDS } from "../config.ts";
7
7
  import { matchesDocScope } from "../context.ts";
8
8
  import type { Stage } from "./types.ts";
9
9
 
@@ -41,6 +41,20 @@ export const dense: Stage = {
41
41
  matches = matches.filter((m) => matchesDocScope(m.metadata ?? {}, filters.doc_number!));
42
42
  if (filters.edition) matches = matches.filter((m) => !m.metadata?.edition || String(m.metadata.edition) === filters.edition);
43
43
  console.log("dense scope (in code):", before, "→", matches.length, "in doc", filters.doc_number, filters.edition ? `@${filters.edition}` : "");
44
+ // the in-code starvation fallback (observed live: the R 134-2
45
+ // figure question — the vector-level top-K held ZERO members of
46
+ // the asked document, so the scope cut to nothing and the answer
47
+ // ran on other lanes' leftovers): re-query at a deep topK and
48
+ // scope in code — the NATIVE metadata filter is dead on this
49
+ // platform (measured 2026-10-05: filters return empty for values
50
+ // proven present), so the document's own nearest neighbors are
51
+ // reachable only through a wider net. Fires only when the scope
52
+ // starves to zero — zero behavior when the scope matches.
53
+ if (!matches.length) {
54
+ const wide: any = await env.VECTORIZE.query(vector, { ...q, topK: THRESHOLDS.denseStarveK });
55
+ matches = (wide.matches ?? []).filter((m: any) => matchesDocScope(m.metadata ?? {}, filters.doc_number!));
56
+ console.log("dense scope starvation → deep re-query:", matches.length, "doc members in the top", THRESHOLDS.denseStarveK);
57
+ }
44
58
  // an edition pin corroborated by (almost) nothing means the pin
45
59
  // was a guess — understanding emits editions for families it
46
60
  // mixes up (observed: R 76 pinned @2021, an R 60 year; the corpus
@@ -33,6 +33,7 @@ import { stdRefNudge } from "./stdRefNudge.ts";
33
33
  import { overviewDemote } from "./overviewDemote.ts";
34
34
  import { familyBoost } from "./familyBoost.ts";
35
35
  import { rerankStage, lexicalRrf } from "./rerank.ts";
36
+ import { vlmFigureRerank } from "./vlmFigureRerank.ts";
36
37
  import { termNudge } from "./termNudge.ts";
37
38
  import { conceptSteer } from "./conceptSteer.ts";
38
39
  import { editionSteer } from "./editionSteer.ts";
@@ -75,6 +76,7 @@ export const STAGES: Stage[] = [
75
76
  typedPin,
76
77
  sectionDescent,
77
78
  dedup,
79
+ vlmFigureRerank,
78
80
  windowFloor,
79
81
  ];
80
82
 
@@ -0,0 +1,80 @@
1
+ // VLM figure rerank (TODO.sota/18.3 — the hedge that sees past
2
+ // embeddings): a figure-SEEKING question's figure candidates are scored
3
+ // by a vision model on their actual images — the MKO figure chunks'
4
+ // text is thin (a breadcrumb line + a caption), so text ranking loses
5
+ // them to sibling tables no matter what the caption-augmented embed
6
+ // carries. One bounded vision call, the winner promoted to the front of
7
+ // the window with a rerank score above the incumbent top; every failure
8
+ // mode is additive (the text ranking stands). Off via the registry
9
+ // projection like any stage (ablate_stages drops it).
10
+ import { MODELS } from "../config.ts";
11
+ import { portBlobs, portModelRunner, portStore } from "../env.ts";
12
+ import type { Stage } from "./types.ts";
13
+ import type { Hit } from "../../../shared/chunk";
14
+
15
+ export const FIGURE_Q = /\bfig(ure|s)?\b/i;
16
+
17
+ export function parsePick(text: unknown, kept: number): number {
18
+ const t = text as any;
19
+ const s = typeof text === "string" ? text : typeof t?.response === "string" ? t.response : t?.choices?.[0]?.message?.content ?? "";
20
+ const pick = Number(String(s).match(/"pick"\s*:\s*(\d+)/)?.[1] ?? 0);
21
+ return pick >= 1 && pick <= kept ? pick : 0;
22
+ }
23
+
24
+ export const vlmFigureRerank: Stage = {
25
+ name: "vlm-figure-rerank",
26
+ failure: "additive",
27
+ when: (c) => FIGURE_Q.test(c.query) && c.finalHits.some((h: Hit) => (h.metadata as any).block === "figure" && (h.metadata as any).unit_id),
28
+ run: async (c) => {
29
+ const figures = c.finalHits.filter((h: Hit) => (h.metadata as any).block === "figure" && (h.metadata as any).unit_id).slice(0, 3);
30
+ if (!figures.length) return;
31
+ try {
32
+ const ids = figures.map((h: Hit) => String((h.metadata as any).unit_id));
33
+ const ph = ids.map((_, i) => `?${i + 1}`).join(",");
34
+ const rows = await portStore(c.env).prepare(
35
+ `SELECT unit_id, json_extract(payload,'$.uri') AS uri FROM unit_payloads WHERE unit_id IN (${ph})`,
36
+ )
37
+ .bind(...ids)
38
+ .all();
39
+ const uris = new Map(((rows.results ?? []) as any[]).map((r: any) => [r.unit_id as string, r.uri as string | null]));
40
+ // the multimodal gotcha (2026-09-09): image parts ride a SHORT
41
+ // message — the question and the labels, nothing else
42
+ const content: any[] = [
43
+ {
44
+ type: "text",
45
+ text: `Question: ${c.query}\n\nWhich of these figures best answers the question? Reply with ONLY: {"pick": N} using the image's number, or {"pick": 0} if none is relevant.`,
46
+ },
47
+ ];
48
+ const kept: { h: Hit; label: number }[] = [];
49
+ let n = 0;
50
+ for (const h of figures) {
51
+ const uri = String(uris.get(String((h.metadata as any).unit_id)) ?? "");
52
+ const m = uri.match(/^\/assets\/(.+)/);
53
+ if (!m) continue;
54
+ const obj = await portBlobs(c.env).get(m[1]);
55
+ if (!obj?.body) continue;
56
+ const buf = new Uint8Array(await new Response(obj.body).arrayBuffer());
57
+ let binary = "";
58
+ for (let i = 0; i < buf.length; i += 8192) binary += String.fromCharCode(...buf.subarray(i, i + 8192));
59
+ n += 1;
60
+ content.push({ type: "text", text: `Image ${n}:` });
61
+ content.push({ type: "image_url", image_url: { url: `data:image/png;base64,${btoa(binary)}` } });
62
+ kept.push({ h, label: n });
63
+ }
64
+ if (!kept.length) return;
65
+ const raced: any = await Promise.race([
66
+ portModelRunner(c.env).run({ model: MODELS.vlmFigure, messages: [{ role: "user", content }], maxTokens: 256, effort: "low" }),
67
+ new Promise<null>((r) => setTimeout(() => r(null), 15000)),
68
+ ]);
69
+ const pick = parsePick(raced?.text ?? raced, kept.length);
70
+ const chosen = kept.find((k) => k.label === pick);
71
+ if (!chosen) return;
72
+ const top = Math.max(...c.finalHits.map((h: Hit) => h.rerank_score ?? h.score ?? 0));
73
+ chosen.h.rerank_score = top + 1;
74
+ c.finalHits = [chosen.h, ...c.finalHits.filter((h: Hit) => h !== chosen.h)];
75
+ console.log("vlm-figure-rerank: promoted", (chosen.h.metadata as any).unit_id, "over", c.finalHits.length - 1, "text-ranked hits");
76
+ } catch {
77
+ // additive — the text ranking stands
78
+ }
79
+ },
80
+ };
@@ -1,16 +0,0 @@
1
- import {
2
- handleAsk
3
- } from "./chunk-GVCOGEJC.js";
4
- import "./chunk-ECOYO7EX.js";
5
- import "./chunk-K5WL3DKH.js";
6
- import "./chunk-E74MNLBC.js";
7
- import "./chunk-FHRGC6TP.js";
8
- import "./chunk-GJATVGBS.js";
9
- import "./chunk-ETLSWSZ7.js";
10
- import "./chunk-D6UUAL7I.js";
11
- import "./chunk-ZD6TYLET.js";
12
- import "./chunk-3FYJM7LH.js";
13
- import "./chunk-LSZTJI7V.js";
14
- export {
15
- handleAsk
16
- };
@@ -1,14 +0,0 @@
1
- import {
2
- handleSearch
3
- } from "./chunk-K7UGBTBZ.js";
4
- import "./chunk-E74MNLBC.js";
5
- import "./chunk-FHRGC6TP.js";
6
- import "./chunk-GJATVGBS.js";
7
- import "./chunk-ETLSWSZ7.js";
8
- import "./chunk-D6UUAL7I.js";
9
- import "./chunk-ZD6TYLET.js";
10
- import "./chunk-3FYJM7LH.js";
11
- import "./chunk-LSZTJI7V.js";
12
- export {
13
- handleSearch
14
- };