@konneal/engine 0.2.196 → 0.2.197
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.
- package/dist/{ask-PYJFLK5H.js → ask-64WZ7GK3.js} +2 -2
- package/dist/{chunk-FOCGVXCX.js → chunk-EHXWBBWS.js} +1 -1
- package/dist/{chunk-CTSEDLL2.js → chunk-TMIVKBN2.js} +31 -18
- package/dist/{chunk-F4VDXM3W.js → chunk-W2MBNLDL.js} +1 -1
- package/dist/context.d.ts +29 -0
- package/dist/{search-WEZGZDO2.js → search-KDB6QHHQ.js} +2 -2
- package/dist/worker_public/src/index.js +4 -4
- package/docs/spec-pipeline.md +14 -8
- package/package.json +1 -1
- package/workers/worker_public/src/context.ts +37 -0
- package/workers/worker_public/src/pipeline.ts +16 -17
- package/workers/worker_public/src/stages/editionCover.ts +8 -0
- package/workers/worker_public/src/stages/types.ts +1 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
handleAsk
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-EHXWBBWS.js";
|
|
4
4
|
import "./chunk-IRCDOFP4.js";
|
|
5
5
|
import "./chunk-2DBXRO4E.js";
|
|
6
|
-
import "./chunk-
|
|
6
|
+
import "./chunk-TMIVKBN2.js";
|
|
7
7
|
import "./chunk-5OSQMWRV.js";
|
|
8
8
|
import "./chunk-M4OCMIWU.js";
|
|
9
9
|
import "./chunk-ETLSWSZ7.js";
|
|
@@ -21820,6 +21820,21 @@ function refCodec() {
|
|
|
21820
21820
|
return REGISTRY[P().publisher.codec] ?? plainSlug;
|
|
21821
21821
|
}
|
|
21822
21822
|
|
|
21823
|
+
// workers/worker_public/src/selfquery.ts
|
|
21824
|
+
function toVectorizeFilter(f) {
|
|
21825
|
+
if (f.doc_number) {
|
|
21826
|
+
const out = { doc_number: f.doc_number };
|
|
21827
|
+
if (f.edition) out.edition = f.edition;
|
|
21828
|
+
return out;
|
|
21829
|
+
}
|
|
21830
|
+
return void 0;
|
|
21831
|
+
}
|
|
21832
|
+
function standardKeyAllowed(meta, keys) {
|
|
21833
|
+
if (!keys) return true;
|
|
21834
|
+
const k = meta.standard_key;
|
|
21835
|
+
return !k || keys.has(k);
|
|
21836
|
+
}
|
|
21837
|
+
|
|
21823
21838
|
// workers/worker_public/src/context.ts
|
|
21824
21839
|
var NO_CONTEXT = { kind: "none", scoped_to: null };
|
|
21825
21840
|
var MACHINE_STATE_MAX = 60;
|
|
@@ -21872,6 +21887,13 @@ function docScopeFilter(scopeDocNumber) {
|
|
|
21872
21887
|
const base = scopeDocNumber.split("-")[0];
|
|
21873
21888
|
return { doc_number: { $in: base === scopeDocNumber ? [scopeDocNumber] : [scopeDocNumber, base] } };
|
|
21874
21889
|
}
|
|
21890
|
+
function bindLexicalLane(hits, scopes) {
|
|
21891
|
+
const { doc, seal: seal2, standardKeys } = scopes;
|
|
21892
|
+
if (!doc && !seal2 && !standardKeys) return hits;
|
|
21893
|
+
return hits.filter(
|
|
21894
|
+
(h) => (!doc || matchesDocScope(h.metadata, doc)) && (!seal2 || matchesDocScope(h.metadata, seal2.doc_number) && (!seal2.edition || h.metadata.edition === seal2.edition)) && standardKeyAllowed(h.metadata, standardKeys)
|
|
21895
|
+
);
|
|
21896
|
+
}
|
|
21875
21897
|
async function resolveDocScope(env, ctx) {
|
|
21876
21898
|
if (!ctx.doc) return null;
|
|
21877
21899
|
const parsed = parseDocRef(ctx.doc, ctx.edition);
|
|
@@ -22203,21 +22225,6 @@ var dense = {
|
|
|
22203
22225
|
}
|
|
22204
22226
|
};
|
|
22205
22227
|
|
|
22206
|
-
// workers/worker_public/src/selfquery.ts
|
|
22207
|
-
function toVectorizeFilter(f) {
|
|
22208
|
-
if (f.doc_number) {
|
|
22209
|
-
const out = { doc_number: f.doc_number };
|
|
22210
|
-
if (f.edition) out.edition = f.edition;
|
|
22211
|
-
return out;
|
|
22212
|
-
}
|
|
22213
|
-
return void 0;
|
|
22214
|
-
}
|
|
22215
|
-
function standardKeyAllowed(meta, keys) {
|
|
22216
|
-
if (!keys) return true;
|
|
22217
|
-
const k = meta.standard_key;
|
|
22218
|
-
return !k || keys.has(k);
|
|
22219
|
-
}
|
|
22220
|
-
|
|
22221
22228
|
// workers/worker_public/src/stages/citationProbe.ts
|
|
22222
22229
|
var CITE_PATTERN = /\b(?:cite[sd]?|citing|referenc(?:e|es|ed|ing)|list[s]?|quote[sd]?)\b/i;
|
|
22223
22230
|
var REFS_PATTERN = /\b(?:standard|publication|document|normative|bibliograph)/i;
|
|
@@ -22822,8 +22829,10 @@ var editionCover = {
|
|
|
22822
22829
|
failure: "additive",
|
|
22823
22830
|
when: (c) => !c.filters?.edition && c.hits.length > 1,
|
|
22824
22831
|
run: async (c) => {
|
|
22832
|
+
const scope = c.filters?.doc_number;
|
|
22825
22833
|
const poolDocs = /* @__PURE__ */ new Map();
|
|
22826
22834
|
for (const h of c.hits) {
|
|
22835
|
+
if (scope && !matchesDocScope(h.metadata, scope)) continue;
|
|
22827
22836
|
const di = h.metadata.docidentifier;
|
|
22828
22837
|
const ed = h.metadata.edition;
|
|
22829
22838
|
if (!di || !ed) continue;
|
|
@@ -23815,10 +23824,14 @@ async function retrieve(env, query, opts = {}) {
|
|
|
23815
23824
|
const vectorP = rq === folded && opts.optimisticVec ? Promise.resolve(opts.optimisticVec) : rq === folded && opts.warmEmbed ? opts.warmEmbed.then((w) => w ?? embed(portModelRunner(env), MODELS.embed, rq)) : embed(portModelRunner(env), MODELS.embed, rq);
|
|
23816
23825
|
const lexicalP = lexicalPrefilter(env, opts.lexicalBoost ? `${rq} ${opts.lexicalBoost}` : rq).catch(() => []);
|
|
23817
23826
|
const [vector, lexicalHits0] = await Promise.all([vectorP, lexicalP]);
|
|
23818
|
-
const lexicalHits =
|
|
23819
|
-
|
|
23820
|
-
|
|
23827
|
+
const lexicalHits = bindLexicalLane(lexicalHits0, {
|
|
23828
|
+
doc: scope?.doc_number ?? null,
|
|
23829
|
+
seal: opts.sealScope ?? null,
|
|
23830
|
+
standardKeys: opts.standardKeys ?? null
|
|
23831
|
+
});
|
|
23821
23832
|
if (lexicalHits.length) console.log("lexical prefilter:", lexicalHits.length, "hits");
|
|
23833
|
+
if (scope?.doc_number && lexicalHits.length !== lexicalHits0.length)
|
|
23834
|
+
console.log("lexical scope:", lexicalHits0.length, "\u2192", lexicalHits.length, "in doc", scope.doc_number);
|
|
23822
23835
|
const ctx = {
|
|
23823
23836
|
env,
|
|
23824
23837
|
query,
|
package/dist/context.d.ts
CHANGED
|
@@ -136,6 +136,35 @@ export declare function docScopeFilter(scopeDocNumber: string): {
|
|
|
136
136
|
$in: string[];
|
|
137
137
|
};
|
|
138
138
|
};
|
|
139
|
+
/** The scopes that bind the lexical lane at its SOURCE. The lane ranks
|
|
140
|
+
* the whole corpus and enters the pool twice — the union before the
|
|
141
|
+
* rerank and the RRF after it — so no pool-level cut can hold it: every
|
|
142
|
+
* hard scope filters the list here, once, for both entries. The
|
|
143
|
+
* document scope is the dense lane's own (the same grain rule): the
|
|
144
|
+
* cross-encoder re-scores whatever it is handed, so an unscoped union
|
|
145
|
+
* put 23–40 off-document candidates beside the scoped ones and let
|
|
146
|
+
* another publication's overview into a part-scoped window (measured
|
|
147
|
+
* 2026-12-05). Off-document breadth stays the dense lane's widening. */
|
|
148
|
+
export interface LexicalScopes {
|
|
149
|
+
/** the understood document — absent or null means unscoped */
|
|
150
|
+
doc?: string | null;
|
|
151
|
+
/** a declared context's seal: the family, optionally the edition */
|
|
152
|
+
seal?: {
|
|
153
|
+
doc_number: string;
|
|
154
|
+
edition?: string;
|
|
155
|
+
} | null;
|
|
156
|
+
/** the license entitlement set — absent or null means inert */
|
|
157
|
+
standardKeys?: ReadonlySet<string> | null;
|
|
158
|
+
}
|
|
159
|
+
export declare function bindLexicalLane<T extends {
|
|
160
|
+
metadata: LexicalMeta;
|
|
161
|
+
}>(hits: T[], scopes: LexicalScopes): T[];
|
|
162
|
+
interface LexicalMeta {
|
|
163
|
+
doc_number?: string;
|
|
164
|
+
docidentifier?: string;
|
|
165
|
+
edition?: string;
|
|
166
|
+
standard_key?: string;
|
|
167
|
+
}
|
|
139
168
|
export declare function resolveDocScope(env: any, ctx: DeclaredContext): Promise<DocScope | null>;
|
|
140
169
|
export declare function appliedContext(declared: DeclaredContext | null, scope: DocScope | null, note?: AppliedContext["note"], live?: LiveEcho): AppliedContext;
|
|
141
170
|
/** Validate a context_applied object arriving from a client (the
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
listUserDocuments,
|
|
10
10
|
scoreFaithfulness,
|
|
11
11
|
scoreJudge
|
|
12
|
-
} from "../../chunk-
|
|
12
|
+
} from "../../chunk-EHXWBBWS.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-
|
|
21
|
+
} from "../../chunk-W2MBNLDL.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-
|
|
46
|
+
} from "../../chunk-TMIVKBN2.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-
|
|
1268
|
+
const res = name === "ask" ? await (await import("../../ask-64WZ7GK3.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-KDB6QHHQ.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/docs/spec-pipeline.md
CHANGED
|
@@ -12,7 +12,9 @@ result.
|
|
|
12
12
|
- **Context** (`PipelineContext`) — the mutable state every stage reads
|
|
13
13
|
and writes: `env`, `query` (the user's original wording), `rq` (the
|
|
14
14
|
retrieval query), `u` (query understanding, nullable), `filters` /
|
|
15
|
-
`filter` (the
|
|
15
|
+
`filter` (the document/edition scope — the dense lane's filter and,
|
|
16
|
+
through the prelude's `bindLexicalLane`, the lexical lane's too),
|
|
17
|
+
`vector`, `lexicalHits`,
|
|
16
18
|
`matches` (the candidate pool, Vectorize-match shape), `hits` (the
|
|
17
19
|
ranked pool), `finalHits` (the answer window), `glossary`, `opts`.
|
|
18
20
|
- **Guard** (`when`) — a pure read of the context; absent = always runs.
|
|
@@ -55,7 +57,7 @@ candidate lanes → pool open → pool-level merges → refinement → window as
|
|
|
55
57
|
| 6 | `multi-query` | `u.query_variants` | blocking (per-variant catches inside) | REPLACES `matches` | RRF-fuses the primary ranking with each variant's (k=60, top `retrieveK`). |
|
|
56
58
|
| 7 | `sub-query` | `u.complexity === "complex" && u.sub_queries` | blocking (per-sub catches inside) | appends `matches` | Complementary-perspective union at `subQueryDiscount` (no RRF). |
|
|
57
59
|
| 8 | `pool-open` | — | blocking | `hits` | Converts the candidate pool to the `Hit` shape. Last writer of `matches`; every later stage operates on `hits`. |
|
|
58
|
-
| 9 | `lexical-union` | `lexicalHits.length` | blocking | appends `hits` | Full-corpus BM25 hits dense missed (dense metadata wins on id collision). |
|
|
60
|
+
| 9 | `lexical-union` | `lexicalHits.length` | blocking | appends `hits` | Full-corpus BM25 hits dense missed (dense metadata wins on id collision). The list arrives already bound to every hard scope at the source (the prelude) — under a document scope the union adds the scoped document's hits only. |
|
|
59
61
|
| 10 | `federate` | `opts.federate` | additive (callback catches transport) | appends `hits` | Internal ISO/IEC passages at `federateDiscount`. |
|
|
60
62
|
| 11 | `seal` | `opts.sealScope` | blocking | filters `hits` | The declared context's hard cut: nothing outside the declared family reaches rerank. (The lexical lane is sealed at the SOURCE, in the prelude — not here.) |
|
|
61
63
|
| 11b | `license-scope` | `opts.standardKeys` | blocking | filters `hits` | The license entitlement hard cut (TODO.external-refs/08): a chunk carrying a `standard_key` outside the caller's set never reaches rerank; public chunks (no key) always pass. (The lexical lane is bound at the SOURCE, in the prelude — the lexical union AND the post-rerank lexical-rrf consume the bound list. The post-scope single-document fetches — section-descent, typed-pin parent, edition-cover — are doc-scoped, and a document's key is a per-document constant, so they cannot re-admit a dropped key; citation-probe filters its bibliography push by the same predicate.) |
|
|
@@ -65,7 +67,7 @@ candidate lanes → pool open → pool-level merges → refinement → window as
|
|
|
65
67
|
| 15 | `lexical-rrf` | `hits.length > 1 && lexicalHits.length` | blocking | REPLACES `hits` order | RRF fusion with the full-corpus lexical ranking. Runs even when rerank failed (additive semantics preserve this). |
|
|
66
68
|
| 16 | `citation-probe` | citation-question shape + a document named in the TEXT | additive | appends `hits`, appends `notes` | GraphRAG: bibliography-shaped questions get (a) the family's bibliography sections as passages — FTS over the chunk store, deterministic, text fetched from D1 (getByIds returns no text), pushed to `hits` at score 10 — and (b) the graph's `cites` edges for the family as an authoritative per-edition note on the `notes` channel, identifiers codec-normalized. The graph answers the STRUCTURE; the passages ground it verbatim. |
|
|
67
69
|
| 17 | `corpus-scope` | `opts.datasetScope` | blocking | filters `hits` | Dataset scope (the sidebar toggles): drops hits whose corpus the request excludes. LAST of the pool-assembly stages — the lexical union above refills the pool after rerank, so filtering earlier let excluded corpora back in. Corpora the toggle model doesn't name pass untouched. |
|
|
68
|
-
| 18 | `edition-cover` | `!filters.edition && hits.length > 1` | additive | appends `hits` | Registry-driven cover: when a pool holds only stale editions of a document whose ACTIVE edition the documents registry knows, fetch the current edition's chunks and add at `editionCoverDiscount` — steering needs the successor present to demote. |
|
|
70
|
+
| 18 | `edition-cover` | `!filters.edition && hits.length > 1` | additive | appends `hits` | Registry-driven cover: when a pool holds only stale editions of a document whose ACTIVE edition the documents registry knows, fetch the current edition's chunks and add at `editionCoverDiscount` — steering needs the successor present to demote. Under a document scope the pooled documents are the scope's own (`matchesDocScope`): an off-document passage a cross-document lane merged never gets its publication covered. |
|
|
69
71
|
| 19 | `std-ref-nudge` | query names ISO/IEC/ASTM/EN | blocking | `hits[].rerank_score`, sorts | Standard-reference nudge: chunks CARRYING such a citation get `stdRefNudgeSpread × spread` — the citing clause is the answer to "which standard does X invoke", and generic family prose otherwise fills the window (l5a measured). |
|
|
70
72
|
| 20 | `term-nudge` | `u.term` | blocking | `hits[].rerank_score`, sorts | Clause whose head IS the asked term gets `termNudgeSpread × spread` (decisive). |
|
|
71
73
|
| 21 | `concept-steer` | `glossary.length && hits.length > 1` | blocking | `hits[].rerank_score`, sorts | Vocabulary-link families boosted (`conceptSteerSpread`). |
|
|
@@ -131,11 +133,15 @@ The invariants:
|
|
|
131
133
|
## The prelude (not stages, deliberately)
|
|
132
134
|
|
|
133
135
|
`retrieve()` resolves the query fold, runs the dense embed and the
|
|
134
|
-
full-corpus lexical prefilter **in parallel**, and
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
136
|
+
full-corpus lexical prefilter **in parallel**, and binds the lexical
|
|
137
|
+
lane at the source to every hard scope (`bindLexicalLane`: the declared
|
|
138
|
+
context's seal, the license entitlement set, and the understood
|
|
139
|
+
document — the dense lane's own scope, the same grain rule). The lane
|
|
140
|
+
re-enters the pool twice (union before the rerank, RRF after it), so
|
|
141
|
+
the binding must precede both. This parallelism is load-bearing (Option
|
|
142
|
+
C: the optimistic vector may already be resolved; embed and lexical
|
|
143
|
+
must not serialize) — it stays in the composition layer, not in stages,
|
|
144
|
+
so no stage boundary can introduce a serial round-trip on the hot path.
|
|
139
145
|
|
|
140
146
|
## Verification
|
|
141
147
|
|
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.
|
|
30
|
+
"version": "0.2.197",
|
|
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",
|
|
@@ -140,6 +140,7 @@ export function parseContext(body: any): DeclaredContext | null {
|
|
|
140
140
|
|
|
141
141
|
import { refCodec, type DocScope } from "./codecs.ts";
|
|
142
142
|
import { P } from "./profile.ts";
|
|
143
|
+
import { standardKeyAllowed } from "./selfquery.ts";
|
|
143
144
|
export type { DocScope };
|
|
144
145
|
|
|
145
146
|
/** Parse the two reference forms the estate speaks: the URN the SMART
|
|
@@ -203,6 +204,42 @@ export function docScopeFilter(scopeDocNumber: string): { doc_number: { $in: str
|
|
|
203
204
|
return { doc_number: { $in: base === scopeDocNumber ? [scopeDocNumber] : [scopeDocNumber, base] } };
|
|
204
205
|
}
|
|
205
206
|
|
|
207
|
+
/** The scopes that bind the lexical lane at its SOURCE. The lane ranks
|
|
208
|
+
* the whole corpus and enters the pool twice — the union before the
|
|
209
|
+
* rerank and the RRF after it — so no pool-level cut can hold it: every
|
|
210
|
+
* hard scope filters the list here, once, for both entries. The
|
|
211
|
+
* document scope is the dense lane's own (the same grain rule): the
|
|
212
|
+
* cross-encoder re-scores whatever it is handed, so an unscoped union
|
|
213
|
+
* put 23–40 off-document candidates beside the scoped ones and let
|
|
214
|
+
* another publication's overview into a part-scoped window (measured
|
|
215
|
+
* 2026-12-05). Off-document breadth stays the dense lane's widening. */
|
|
216
|
+
export interface LexicalScopes {
|
|
217
|
+
/** the understood document — absent or null means unscoped */
|
|
218
|
+
doc?: string | null;
|
|
219
|
+
/** a declared context's seal: the family, optionally the edition */
|
|
220
|
+
seal?: { doc_number: string; edition?: string } | null;
|
|
221
|
+
/** the license entitlement set — absent or null means inert */
|
|
222
|
+
standardKeys?: ReadonlySet<string> | null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function bindLexicalLane<T extends { metadata: LexicalMeta }>(hits: T[], scopes: LexicalScopes): T[] {
|
|
226
|
+
const { doc, seal, standardKeys } = scopes;
|
|
227
|
+
if (!doc && !seal && !standardKeys) return hits;
|
|
228
|
+
return hits.filter(
|
|
229
|
+
(h) =>
|
|
230
|
+
(!doc || matchesDocScope(h.metadata, doc)) &&
|
|
231
|
+
(!seal || (matchesDocScope(h.metadata, seal.doc_number) && (!seal.edition || h.metadata.edition === seal.edition))) &&
|
|
232
|
+
standardKeyAllowed(h.metadata, standardKeys),
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
interface LexicalMeta {
|
|
237
|
+
doc_number?: string;
|
|
238
|
+
docidentifier?: string;
|
|
239
|
+
edition?: string;
|
|
240
|
+
standard_key?: string;
|
|
241
|
+
}
|
|
242
|
+
|
|
206
243
|
export async function resolveDocScope(env: any, ctx: DeclaredContext): Promise<DocScope | null> {
|
|
207
244
|
if (!ctx.doc) return null;
|
|
208
245
|
const parsed = parseDocRef(ctx.doc, ctx.edition);
|
|
@@ -33,8 +33,8 @@ export function promptVars(extra: Record<string, string> = {}): Record<string, s
|
|
|
33
33
|
export function fill(template: string, vars: Record<string, string>): string {
|
|
34
34
|
return template.replace(/\{\{(\w+)\}\}/g, (_m, k: string) => (k in vars ? vars[k] : ""));
|
|
35
35
|
}
|
|
36
|
-
import { QueryFilters, toVectorizeFilter
|
|
37
|
-
import {
|
|
36
|
+
import { QueryFilters, toVectorizeFilter } from "./selfquery";
|
|
37
|
+
import { bindLexicalLane } from "./context";
|
|
38
38
|
import { hitQuality } from "./quality";
|
|
39
39
|
import { lexicalPrefilter } from "./lexical";
|
|
40
40
|
import { positionOrder } from "./structural";
|
|
@@ -129,22 +129,21 @@ export async function retrieve(
|
|
|
129
129
|
: embed(portModelRunner(env), MODELS.embed, rq);
|
|
130
130
|
const lexicalP = lexicalPrefilter(env, opts.lexicalBoost ? `${rq} ${opts.lexicalBoost}` : rq).catch(() => [] as Hit[]);
|
|
131
131
|
const [vector, lexicalHits0] = await Promise.all([vectorP, lexicalP]);
|
|
132
|
-
//
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
//
|
|
136
|
-
//
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
standardKeyAllowed(h.metadata, opts.standardKeys),
|
|
145
|
-
)
|
|
146
|
-
: lexicalHits0);
|
|
132
|
+
// Every hard scope binds the lexical lane at the SOURCE: the lane
|
|
133
|
+
// re-enters the pool twice (lexical-union pre-rerank and lexical-rrf
|
|
134
|
+
// post-rerank — both consume this list), so the RRF fusion mixes the
|
|
135
|
+
// full-corpus lexical ranking into the final hits past any pool-level
|
|
136
|
+
// cut. Under a seal the lane is the FAMILY's hits only; the license
|
|
137
|
+
// entitlement scope binds it the same way; and the understood document
|
|
138
|
+
// scopes it exactly as it scopes the dense lane (bindLexicalLane).
|
|
139
|
+
const lexicalHits = bindLexicalLane(lexicalHits0, {
|
|
140
|
+
doc: scope?.doc_number ?? null,
|
|
141
|
+
seal: opts.sealScope ?? null,
|
|
142
|
+
standardKeys: opts.standardKeys ?? null,
|
|
143
|
+
});
|
|
147
144
|
if (lexicalHits.length) console.log("lexical prefilter:", lexicalHits.length, "hits");
|
|
145
|
+
if (scope?.doc_number && lexicalHits.length !== lexicalHits0.length)
|
|
146
|
+
console.log("lexical scope:", lexicalHits0.length, "→", lexicalHits.length, "in doc", scope.doc_number);
|
|
148
147
|
|
|
149
148
|
const ctx: PipelineContext = {
|
|
150
149
|
env, query, rq, folded, u, filters, filter, vector, lexicalHits,
|
|
@@ -7,6 +7,7 @@
|
|
|
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 { matchesDocScope } from "../context.ts";
|
|
10
11
|
import type { Stage } from "./types.ts";
|
|
11
12
|
import { rowsToHits } from "../lexical.ts";
|
|
12
13
|
|
|
@@ -20,8 +21,15 @@ export const editionCover: Stage = {
|
|
|
20
21
|
failure: "additive",
|
|
21
22
|
when: (c) => !c.filters?.edition && c.hits.length > 1,
|
|
22
23
|
run: async (c) => {
|
|
24
|
+
const scope = c.filters?.doc_number;
|
|
23
25
|
const poolDocs = new Map<string, Set<string>>();
|
|
24
26
|
for (const h of c.hits) {
|
|
27
|
+
// a document scope binds the cover at the scope's grain: the pool
|
|
28
|
+
// may still hold an off-document passage a cross-document lane
|
|
29
|
+
// merged, and covering THAT publication seated three of its cover
|
|
30
|
+
// chunks at one score right beside the top passage (measured
|
|
31
|
+
// 2026-12-05: a part-scoped figure window led with them)
|
|
32
|
+
if (scope && !matchesDocScope(h.metadata, scope)) continue;
|
|
25
33
|
const di = h.metadata.docidentifier;
|
|
26
34
|
const ed = h.metadata.edition;
|
|
27
35
|
if (!di || !ed) continue;
|
|
@@ -99,7 +99,7 @@ export interface PipelineContext {
|
|
|
99
99
|
filters: QueryFilters | null; // dense-stage may drop a guessed edition pin
|
|
100
100
|
filter: Record<string, string> | null | undefined; // toVectorizeFilter's output
|
|
101
101
|
vector: number[]; // embedding of rq
|
|
102
|
-
lexicalHits: Hit[]; // full-corpus BM25 ranking (
|
|
102
|
+
lexicalHits: Hit[]; // full-corpus BM25 ranking (bound at the source to every hard scope: seal, license, document)
|
|
103
103
|
matches: any[]; // the candidate pool (Vectorize match shape, pre-Hit)
|
|
104
104
|
hits: Hit[]; // the ranked pool from poolOpen onward
|
|
105
105
|
finalHits: Hit[]; // the answer window
|