@konneal/engine 0.2.152 → 0.2.155

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.
@@ -1637,7 +1637,7 @@ async function extractNameplate(ai, model, image) {
1637
1637
  return null;
1638
1638
  }
1639
1639
 
1640
- // workers/worker_public/src/ask.ts
1640
+ // workers/worker_public/src/answerGuards.ts
1641
1641
  function toolNarration(t) {
1642
1642
  return /^(?:the\s+)?[a-z][a-z_.]*\.[a-z_]+ tool returned\b/i.test(t.slice(0, 200)) || /tool returned,? for the query\b/i.test(t.slice(0, 300));
1643
1643
  }
@@ -1649,6 +1649,8 @@ function intentPreamble(t) {
1649
1649
  return !/\[\[u:|\[\^?\d+\]|\b\d+\.\d+\b/.test(s);
1650
1650
  }
1651
1651
  var INTENT_PREAMBLE_NOTICE = "Correction notice: your draft announced a plan ('I'll look up \u2026') instead of answering. Do not narrate intent \u2014 answer the question NOW from the retrieved passages: give the values, clause references and citations the question asks for.";
1652
+
1653
+ // workers/worker_public/src/ask.ts
1652
1654
  function userImageDataUrl(body) {
1653
1655
  const img = body?.image;
1654
1656
  if (img == null) return null;
@@ -1844,14 +1846,6 @@ async function handleAsk(env, ctx, req, tier, key) {
1844
1846
  const telemetryMeta = () => ({ durationMs: Date.now() - tStart, keyId: key?.id ?? null, retries: generateRetries });
1845
1847
  const stageTiming = {};
1846
1848
  let generateRetries = 0;
1847
- const readAs = () => understanding ? {
1848
- intent: understanding.intent,
1849
- doc: understanding.docidentifier,
1850
- edition: understanding.edition ?? null,
1851
- term: understanding.term,
1852
- terms: (understanding.defined_terms ?? []).slice(0, 4),
1853
- lang: q?.lang ?? null
1854
- } : void 0;
1855
1849
  const serverTiming = () => Object.entries(stageTiming).map(([k, v]) => `${k};dur=${v}`).concat([`generate-retries;desc=count;dur=${generateRetries ?? 0}`, `total;dur=${Date.now() - tStart}`]).join(", ");
1856
1850
  const body = await readJson(req);
1857
1851
  const q = validateQuery(body);
@@ -1898,10 +1892,40 @@ async function handleAsk(env, ctx, req, tier, key) {
1898
1892
  return err(503, "generation_disabled", "Generation is temporarily paused; search remains available.");
1899
1893
  }
1900
1894
  const agentSteps = [];
1895
+ let live = null;
1896
+ let liveResponse = null;
1901
1897
  const mark = (step) => {
1902
- agentSteps.push(step);
1898
+ if (live) live.send({ type: "agent", step });
1899
+ else agentSteps.push(step);
1903
1900
  console.log(`agent-step: ${step}`);
1904
1901
  };
1902
+ const wantsStream = body?.stream === true || tier === "anon" && body?.stream !== false;
1903
+ if (wantsStream) {
1904
+ const encoder = new TextEncoder();
1905
+ let ctrl = null;
1906
+ const sse = new ReadableStream({
1907
+ start(c) {
1908
+ ctrl = c;
1909
+ }
1910
+ });
1911
+ live = {
1912
+ send: (ev) => {
1913
+ try {
1914
+ ctrl.enqueue(encoder.encode(`data: ${JSON.stringify(ev)}
1915
+
1916
+ `));
1917
+ } catch {
1918
+ }
1919
+ },
1920
+ close: () => {
1921
+ try {
1922
+ ctrl.close();
1923
+ } catch {
1924
+ }
1925
+ }
1926
+ };
1927
+ liveResponse = new Response(sse, { headers: { "content-type": "text/event-stream", "cache-control": "no-cache", "x-accel-buffering": "no", ...corsHeaders(req) } });
1928
+ }
1905
1929
  const service = env.INTERNAL_SERVICE;
1906
1930
  const fedAuth = {
1907
1931
  cookie: req.headers.get("cookie") ?? "",
@@ -1960,941 +1984,948 @@ async function handleAsk(env, ctx, req, tier, key) {
1960
1984
  const gen = await corpusGen(env.CACHE);
1961
1985
  const withImage = effectiveImage || storedImage;
1962
1986
  const cached = fresh || contextual || declaredCtx || draftAct || apiCallIntent || withImage || ablate ? null : await cacheGet(env, gen, ns, q.query, q.lang, salt);
1963
- const wantsStream = body?.stream === true || tier === "anon" && body?.stream !== false;
1964
- if (cached) {
1965
- telemetry(env, ctx, tier, "ask", null, true, (cached.value.answer ?? "").length, cached.value.query_hash, q.lang, "exact", telemetryMeta());
1966
- const cctx = cached.value.context_applied ?? NO_CONTEXT;
1967
- if (wantsStream) {
1968
- return sseResponse([
1969
- { type: "citations", citations: cached.value.citations ?? [], quota, context_applied: cctx, ...cached.value.source_quality ? { source_quality: cached.value.source_quality, confidence_note: cached.value.confidence_note ?? null, ...cached.value.experimental_sources ? { experimental_sources: cached.value.experimental_sources } : {}, ...cached.value.entailment ? { entailment: cached.value.entailment } : {} } : {} },
1970
- { type: "token", v: cached.value.answer ?? "" },
1971
- { type: "done", model: cached.value.model ?? MODELS.member, query_hash: cached.value.query_hash, served_from: "cache", context_applied: cctx }
1972
- ], corsHeaders(req));
1973
- }
1974
- return json({ ...cached.value, cached: true, quota, context_applied: cctx });
1975
- }
1976
- const warmQuery = retrievalQuery(q.query, prev);
1977
- const warmEmbed = embedWarm(env, warmQuery);
1978
- const conversationId = typeof body?.conversation_id === "string" && /^[A-Za-z0-9_-]{8,64}$/.test(body.conversation_id) ? body.conversation_id : null;
1979
- try {
1980
- if (!effectiveImage && !attachIds.length && conversationId && env.DB && member) {
1981
- const prior = await env.DB.prepare(
1982
- "SELECT m.attachment_id AS aid, a.mime, a.r2_key FROM messages m JOIN attachments a ON a.id = m.attachment_id JOIN conversations c ON c.id = m.conversation_id AND c.sub = ?2 WHERE m.conversation_id = ?1 AND m.role = 'user' AND m.attachment_id IS NOT NULL ORDER BY m.created_at DESC LIMIT 1"
1983
- ).bind(conversationId, member.sub).first();
1984
- if (prior) {
1985
- const obj = await env.CHAT_UPLOADS.get(prior.r2_key);
1986
- if (obj) {
1987
- const bytes = new Uint8Array(await obj.arrayBuffer());
1988
- let bin = "";
1989
- for (let i = 0; i < bytes.length; i += 32768) bin += String.fromCharCode(...bytes.subarray(i, i + 32768));
1990
- storedImage = `data:${prior.mime};base64,${btoa(bin)}`;
1991
- }
1987
+ let probeTokensUsed = 0;
1988
+ const pipeline = async (channel, qq) => {
1989
+ const live2 = channel;
1990
+ const q2 = qq;
1991
+ const finishLine = (message) => {
1992
+ if (live2) {
1993
+ live2.send({ type: "token", v: message });
1994
+ live2.send({ type: "done", model: MODELS.member, query_hash: "", context_applied: NO_CONTEXT });
1995
+ live2.close();
1996
+ return null;
1997
+ }
1998
+ return err(503, "serving_busy", message);
1999
+ };
2000
+ const readAs = () => understanding ? {
2001
+ intent: understanding.intent,
2002
+ doc: understanding.docidentifier,
2003
+ edition: understanding.edition ?? null,
2004
+ term: understanding.term,
2005
+ terms: (understanding.defined_terms ?? []).slice(0, 4),
2006
+ lang: q2?.lang ?? null
2007
+ } : void 0;
2008
+ if (cached) {
2009
+ telemetry(env, ctx, tier, "ask", null, true, (cached.value.answer ?? "").length, cached.value.query_hash, q2.lang, "exact", telemetryMeta());
2010
+ const cctx = cached.value.context_applied ?? NO_CONTEXT;
2011
+ if (live2) {
2012
+ live2.send({ type: "citations", citations: cached.value.citations ?? [], quota, context_applied: cctx, ...cached.value.source_quality ? { source_quality: cached.value.source_quality, confidence_note: cached.value.confidence_note ?? null, ...cached.value.experimental_sources ? { experimental_sources: cached.value.experimental_sources } : {}, ...cached.value.entailment ? { entailment: cached.value.entailment } : {} } : {} });
2013
+ live2.send({ type: "token", v: cached.value.answer ?? "" });
2014
+ live2.send({ type: "done", model: cached.value.model ?? MODELS.member, query_hash: cached.value.query_hash, served_from: "cache", context_applied: cctx });
2015
+ live2.close();
2016
+ return null;
1992
2017
  }
2018
+ return json({ ...cached.value, cached: true, quota, context_applied: cctx });
1993
2019
  }
1994
- } catch {
1995
- }
1996
- let convEntities = [];
1997
- if (conversationId) {
2020
+ const warmQuery = retrievalQuery(q2.query, prev);
2021
+ const warmEmbed = embedWarm(env, warmQuery);
2022
+ const conversationId = typeof body?.conversation_id === "string" && /^[A-Za-z0-9_-]{8,64}$/.test(body.conversation_id) ? body.conversation_id : null;
1998
2023
  try {
1999
- const rows = await env.DB.prepare("SELECT entity, kind FROM conversation_entities WHERE conversation_id = ?1 LIMIT 12").bind(conversationId).all();
2000
- convEntities = rows.results ?? [];
2001
- if (convEntities.length) console.log("entity map:", convEntities.length, "entries");
2024
+ if (!effectiveImage && !attachIds.length && conversationId && env.DB && member) {
2025
+ const prior = await env.DB.prepare(
2026
+ "SELECT m.attachment_id AS aid, a.mime, a.r2_key FROM messages m JOIN attachments a ON a.id = m.attachment_id JOIN conversations c ON c.id = m.conversation_id AND c.sub = ?2 WHERE m.conversation_id = ?1 AND m.role = 'user' AND m.attachment_id IS NOT NULL ORDER BY m.created_at DESC LIMIT 1"
2027
+ ).bind(conversationId, member.sub).first();
2028
+ if (prior) {
2029
+ const obj = await env.CHAT_UPLOADS.get(prior.r2_key);
2030
+ if (obj) {
2031
+ const bytes = new Uint8Array(await obj.arrayBuffer());
2032
+ let bin = "";
2033
+ for (let i = 0; i < bytes.length; i += 32768) bin += String.fromCharCode(...bytes.subarray(i, i + 32768));
2034
+ storedImage = `data:${prior.mime};base64,${btoa(bin)}`;
2035
+ }
2036
+ }
2037
+ }
2002
2038
  } catch {
2003
2039
  }
2004
- }
2005
- let understanding = null;
2006
- const answerCites = (t) => /\[[^\]]+\]/.test(t);
2007
- const refusalScope = (cited) => ({ cited, conversational: understanding?.intent === "conversational" });
2008
- let optimisticVec = null;
2009
- let optimisticHits = [];
2010
- const t0 = Date.now();
2011
- if (!cached) {
2012
- const understandingP = understandQuery(portModelRunner(env), roleModel(env, "understand"), q.query, history, convEntities);
2013
- try {
2014
- optimisticVec = await warmEmbed ?? null;
2015
- if (optimisticVec) {
2016
- const ores = await env.VECTORIZE.query(optimisticVec, { topK: LIMITS.retrieveK, returnMetadata: "all" });
2017
- optimisticHits = (ores.matches ?? []).map((m) => ({
2018
- id: m.id,
2019
- score: m.score,
2020
- metadata: m.metadata,
2021
- text: m.metadata?.chunk_text ?? ""
2022
- }));
2040
+ let convEntities = [];
2041
+ if (conversationId) {
2042
+ try {
2043
+ const rows = await env.DB.prepare("SELECT entity, kind FROM conversation_entities WHERE conversation_id = ?1 LIMIT 12").bind(conversationId).all();
2044
+ convEntities = rows.results ?? [];
2045
+ if (convEntities.length) console.log("entity map:", convEntities.length, "entries");
2046
+ } catch {
2023
2047
  }
2024
- } catch {
2025
2048
  }
2026
- understanding = await understandingP;
2027
- stageTiming.understand = Date.now() - t0;
2028
- console.log("stage: understand+optimistic", Date.now() - t0, "ms");
2029
- }
2030
- const docScope = declaredCtx && declaredCtx.kind !== "account" ? await resolveDocScope(env, declaredCtx) : null;
2031
- const named = declaredCtx && declaredCtx.kind !== "account" ? namedDocumentIn(q.query) : null;
2032
- let ctxApplied;
2033
- let declaredScoped = false;
2034
- if (!declaredCtx) {
2035
- ctxApplied = NO_CONTEXT;
2036
- const bare = understanding?.process_intent ? null : namedDocumentIn(q.query);
2037
- if (bare && understanding?.doc_number !== bare.doc_number) {
2038
- understanding = {
2039
- ...understanding ?? syntheticUnderstanding(bare),
2040
- docidentifier: bare.label,
2041
- doc_number: bare.doc_number,
2042
- edition: bare.edition ?? understanding?.edition ?? null
2043
- };
2044
- console.log("question names", bare.label, "\u2014 scoping retrieval from the text");
2045
- }
2046
- } else if (declaredCtx.kind === "account") {
2047
- ctxApplied = appliedContext(declaredCtx, null);
2048
- } else if (docScope && (!named || named.doc_number === docScope.doc_number)) {
2049
- if (understanding?.doc_number && understanding.doc_number !== docScope.doc_number) {
2050
- console.log("context scope: understand's doc#" + understanding.doc_number, "is inferred, not named in the question \u2014 the declared", docScope.label, "scopes");
2051
- }
2052
- understanding = {
2053
- ...understanding ?? syntheticUnderstanding(docScope),
2054
- docidentifier: docScope.label,
2055
- doc_number: docScope.doc_number,
2056
- edition: docScope.edition ?? understanding?.edition ?? null
2057
- };
2058
- ctxApplied = appliedContext(declaredCtx, docScope);
2059
- declaredScoped = true;
2060
- console.log("context scope:", docScope.label, `(${declaredCtx.kind})`);
2061
- } else if (docScope && named) {
2062
- if (understanding?.doc_number !== named.doc_number) {
2049
+ let understanding = null;
2050
+ const answerCites = (t) => /\[[^\]]+\]/.test(t);
2051
+ const refusalScope = (cited) => ({ cited, conversational: understanding?.intent === "conversational" });
2052
+ let optimisticVec = null;
2053
+ let optimisticHits = [];
2054
+ const t0 = Date.now();
2055
+ if (!cached) {
2056
+ const understandingP = understandQuery(portModelRunner(env), roleModel(env, "understand"), q2.query, history, convEntities);
2057
+ try {
2058
+ optimisticVec = await warmEmbed ?? null;
2059
+ if (optimisticVec) {
2060
+ const ores = await env.VECTORIZE.query(optimisticVec, { topK: LIMITS.retrieveK, returnMetadata: "all" });
2061
+ optimisticHits = (ores.matches ?? []).map((m) => ({
2062
+ id: m.id,
2063
+ score: m.score,
2064
+ metadata: m.metadata,
2065
+ text: m.metadata?.chunk_text ?? ""
2066
+ }));
2067
+ }
2068
+ } catch {
2069
+ }
2070
+ understanding = await understandingP;
2071
+ stageTiming.understand = Date.now() - t0;
2072
+ console.log("stage: understand+optimistic", Date.now() - t0, "ms");
2073
+ }
2074
+ const docScope = declaredCtx && declaredCtx.kind !== "account" ? await resolveDocScope(env, declaredCtx) : null;
2075
+ const named = declaredCtx && declaredCtx.kind !== "account" ? namedDocumentIn(q2.query) : null;
2076
+ let ctxApplied;
2077
+ let declaredScoped = false;
2078
+ if (!declaredCtx) {
2079
+ ctxApplied = NO_CONTEXT;
2080
+ const bare = understanding?.process_intent ? null : namedDocumentIn(q2.query);
2081
+ if (bare && understanding?.doc_number !== bare.doc_number) {
2082
+ understanding = {
2083
+ ...understanding ?? syntheticUnderstanding(bare),
2084
+ docidentifier: bare.label,
2085
+ doc_number: bare.doc_number,
2086
+ edition: bare.edition ?? understanding?.edition ?? null
2087
+ };
2088
+ console.log("question names", bare.label, "\u2014 scoping retrieval from the text");
2089
+ }
2090
+ } else if (declaredCtx.kind === "account") {
2091
+ ctxApplied = appliedContext(declaredCtx, null);
2092
+ } else if (docScope && (!named || named.doc_number === docScope.doc_number)) {
2093
+ if (understanding?.doc_number && understanding.doc_number !== docScope.doc_number) {
2094
+ console.log("context scope: understand's doc#" + understanding.doc_number, "is inferred, not named in the question \u2014 the declared", docScope.label, "scopes");
2095
+ }
2063
2096
  understanding = {
2064
- ...understanding ?? syntheticUnderstanding(named),
2065
- docidentifier: named.label,
2066
- doc_number: named.doc_number,
2067
- edition: named.edition ?? null
2097
+ ...understanding ?? syntheticUnderstanding(docScope),
2098
+ docidentifier: docScope.label,
2099
+ doc_number: docScope.doc_number,
2100
+ edition: docScope.edition ?? understanding?.edition ?? null
2068
2101
  };
2102
+ ctxApplied = appliedContext(declaredCtx, docScope);
2103
+ declaredScoped = true;
2104
+ console.log("context scope:", docScope.label, `(${declaredCtx.kind})`);
2105
+ } else if (docScope && named) {
2106
+ if (understanding?.doc_number !== named.doc_number) {
2107
+ understanding = {
2108
+ ...understanding ?? syntheticUnderstanding(named),
2109
+ docidentifier: named.label,
2110
+ doc_number: named.doc_number,
2111
+ edition: named.edition ?? null
2112
+ };
2113
+ }
2114
+ ctxApplied = appliedContext(declaredCtx, null, "question-document-wins");
2115
+ console.log("context scope: the question names", named.label, "\u2014 it wins over the declared", docScope.label);
2116
+ } else if (declaredCtx.doc) {
2117
+ ctxApplied = appliedContext(declaredCtx, null, "document-not-in-corpus");
2118
+ console.log("context scope:", declaredCtx.doc, "not in the corpus \u2014 the general corpus answers");
2119
+ } else {
2120
+ ctxApplied = appliedContext(declaredCtx, null);
2121
+ }
2122
+ if (conversationId && understanding) {
2123
+ const now = Date.now();
2124
+ const ents = [];
2125
+ if (understanding.docidentifier) ents.push([understanding.docidentifier, "document"]);
2126
+ for (const t of understanding.defined_terms) ents.push([t, "term"]);
2127
+ if (ents.length) {
2128
+ const upsert = (e, k) => env.DB.prepare("INSERT OR REPLACE INTO conversation_entities (conversation_id, entity, kind, ts) VALUES (?1, ?2, ?3, ?4)").bind(conversationId, e, k, now).run();
2129
+ ctx.waitUntil(Promise.allSettled(ents.map(([e, k]) => upsert(e, k))));
2130
+ }
2069
2131
  }
2070
- ctxApplied = appliedContext(declaredCtx, null, "question-document-wins");
2071
- console.log("context scope: the question names", named.label, "\u2014 it wins over the declared", docScope.label);
2072
- } else if (declaredCtx.doc) {
2073
- ctxApplied = appliedContext(declaredCtx, null, "document-not-in-corpus");
2074
- console.log("context scope:", declaredCtx.doc, "not in the corpus \u2014 the general corpus answers");
2075
- } else {
2076
- ctxApplied = appliedContext(declaredCtx, null);
2077
- }
2078
- if (conversationId && understanding) {
2079
- const now = Date.now();
2080
- const ents = [];
2081
- if (understanding.docidentifier) ents.push([understanding.docidentifier, "document"]);
2082
- for (const t of understanding.defined_terms) ents.push([t, "term"]);
2083
- if (ents.length) {
2084
- const upsert = (e, k) => env.DB.prepare("INSERT OR REPLACE INTO conversation_entities (conversation_id, entity, kind, ts) VALUES (?1, ?2, ?3, ?4)").bind(conversationId, e, k, now).run();
2085
- ctx.waitUntil(Promise.allSettled(ents.map(([e, k]) => upsert(e, k))));
2086
- }
2087
- }
2088
- console.log("understand:", understanding?.intent ?? "null", understanding?.doc_number ? `doc#${understanding.doc_number}${understanding.edition ? "@" + understanding.edition : ""}` : "nodoc", "|", q.query.slice(0, 50));
2089
- const graphDocNumbers = await graphExpand(env, understanding);
2090
- const eNote = await editionNote(env, understanding);
2091
- let register = await searchRegister(env.DB, q.query);
2092
- if (nameplate?.manufacturer) {
2093
- const bridged = await searchRegister(env.DB, nameplateRegisterQuery(nameplate, q.query), true);
2094
- if (bridged?.rows?.length) {
2095
- register = bridged;
2096
- console.log("nameplate-bridge:", nameplate.manufacturer, nameplate.model ?? "", "\u2192", bridged.rows.length, "register rows");
2097
- }
2098
- }
2099
- console.log("register-search:", JSON.stringify({ shaped: !!register, tokens: register?.tokens ?? null, rows: register?.rows?.length ?? null, first: register?.rows?.[0]?.num ?? null }));
2100
- function queryFamilyForAggregate(query) {
2101
- return queryFamily(query);
2102
- }
2103
- let registerBlock = null;
2104
- let aggregateNoteText;
2105
- const aggIntent = registerAggregateIntent(q.query);
2106
- if (aggIntent) {
2107
- const fam = queryFamilyForAggregate(q.query);
2108
- if (fam) {
2109
- const agg = await registerAggregate(env.DB, fam);
2110
- if (agg) {
2111
- const latestLine = agg.latest ? `The latest certificate in the snapshot is ${agg.latest.num} \u2014 holder ${agg.latest.holder}, model "${agg.latest.model}"${agg.latest.year ? `, issued ${agg.latest.year}` : ""}, status ${agg.latest.status}.` : "No certificate rows are present in the snapshot.";
2112
- const linkLine = agg.latest?.pdf_key ? `
2132
+ console.log("understand:", understanding?.intent ?? "null", understanding?.doc_number ? `doc#${understanding.doc_number}${understanding.edition ? "@" + understanding.edition : ""}` : "nodoc", "|", q2.query.slice(0, 50));
2133
+ const graphDocNumbers = await graphExpand(env, understanding);
2134
+ const eNote = await editionNote(env, understanding);
2135
+ let register = await searchRegister(env.DB, q2.query);
2136
+ if (nameplate?.manufacturer) {
2137
+ mark(`certificates.search \u2014 ${nameplate.manufacturer}${nameplate.model ? ` ${nameplate.model}` : ""}`.slice(0, 140));
2138
+ const bridged = await searchRegister(env.DB, nameplateRegisterQuery(nameplate, q2.query), true);
2139
+ if (bridged?.rows?.length) {
2140
+ register = bridged;
2141
+ console.log("nameplate-bridge:", nameplate.manufacturer, nameplate.model ?? "", "\u2192", bridged.rows.length, "register rows");
2142
+ }
2143
+ }
2144
+ console.log("register-search:", JSON.stringify({ shaped: !!register, tokens: register?.tokens ?? null, rows: register?.rows?.length ?? null, first: register?.rows?.[0]?.num ?? null }));
2145
+ function queryFamilyForAggregate(query) {
2146
+ return queryFamily(query);
2147
+ }
2148
+ let registerBlock = null;
2149
+ let aggregateNoteText;
2150
+ const aggIntent = registerAggregateIntent(q2.query);
2151
+ if (aggIntent) {
2152
+ const fam = queryFamilyForAggregate(q2.query);
2153
+ if (fam) {
2154
+ const agg = await registerAggregate(env.DB, fam);
2155
+ if (agg) {
2156
+ const latestLine = agg.latest ? `The latest certificate in the snapshot is ${agg.latest.num} \u2014 holder ${agg.latest.holder}, model "${agg.latest.model}"${agg.latest.year ? `, issued ${agg.latest.year}` : ""}, status ${agg.latest.status}.` : "No certificate rows are present in the snapshot.";
2157
+ const linkLine = agg.latest?.pdf_key ? `
2113
2158
 
2114
2159
  [The latest certificate's document](${`/cert-pdf/${agg.latest.pdf_key}`}) is on file.` : "";
2115
- const series = agg.latest?.num?.match(/^[A-Za-z]+/)?.[0] ?? "R";
2116
- const answer2 = aggIntent === "count" ? `The ${P().publisher.name} ${series} ${agg.family} family holds **${agg.count} certificate${agg.count === 1 ? "" : "s"}** in the ${P().publisher.name}-CS register snapshot (${agg.valid} valid${agg.suspended ? `, ${agg.suspended} suspended` : ""}).
2160
+ const series = agg.latest?.num?.match(/^[A-Za-z]+/)?.[0] ?? "R";
2161
+ const answer2 = aggIntent === "count" ? `The ${P().publisher.name} ${series} ${agg.family} family holds **${agg.count} certificate${agg.count === 1 ? "" : "s"}** in the ${P().publisher.name}-CS register snapshot (${agg.valid} valid${agg.suspended ? `, ${agg.suspended} suspended` : ""}).
2117
2162
 
2118
2163
  Certificates are issued per recommendation family \u2014 the register carries no edition dimension, so the 2021 you named does not narrow the count. The register is a snapshot rather than live certification status.` : `${latestLine}${linkLine}
2119
2164
 
2120
2165
  Certificates are issued per recommendation family \u2014 the register carries no edition dimension. The register is a snapshot rather than live certification status.`;
2121
- const block = aggregateBlock(agg);
2122
- registerBlock = block;
2123
- aggregateNoteText = aggregateNote(agg);
2124
- const queryHash2 = await sha256Hex(q.query);
2125
- telemetry(env, ctx, tier, "ask", MODELS.member, true, answer2.length, queryHash2, q.lang, void 0, telemetryMeta());
2126
- const tq2 = await chargeAnswerTokens(env, bucketId, void 0, q.query.length, answer2.length);
2127
- return json({
2128
- answer: answer2,
2129
- citations: [{ docidentifier: `${P().publisher.name}-CS certificate register (family ${agg.family})`, corpus: "register" }],
2130
- model: MODELS.member,
2131
- query_hash: queryHash2,
2132
- follow_ups: [],
2133
- blocks: [block],
2134
- context_applied: NO_CONTEXT,
2135
- read: readAs(),
2136
- ...ablate ? { ablate } : {},
2137
- quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview }
2138
- });
2166
+ const block = aggregateBlock(agg);
2167
+ registerBlock = block;
2168
+ aggregateNoteText = aggregateNote(agg);
2169
+ const queryHash2 = await sha256Hex(q2.query);
2170
+ telemetry(env, ctx, tier, "ask", MODELS.member, true, answer2.length, queryHash2, q2.lang, void 0, telemetryMeta());
2171
+ const tq2 = await chargeAnswerTokens(env, bucketId, void 0, q2.query.length, answer2.length);
2172
+ const payload = {
2173
+ answer: answer2,
2174
+ citations: [{ docidentifier: `${P().publisher.name}-CS certificate register (family ${agg.family})`, corpus: "register" }],
2175
+ model: MODELS.member,
2176
+ query_hash: queryHash2,
2177
+ follow_ups: [],
2178
+ blocks: [block],
2179
+ context_applied: NO_CONTEXT,
2180
+ read: readAs(),
2181
+ ...ablate ? { ablate } : {},
2182
+ quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview }
2183
+ };
2184
+ if (live2) {
2185
+ live2.send({ type: "citations", citations: payload.citations, quota: payload.quota, context_applied: NO_CONTEXT });
2186
+ live2.send({ type: "token", v: answer2 });
2187
+ live2.send({ type: "done", model: MODELS.member, query_hash: queryHash2, blocks: [block], follow_ups: [], context_applied: NO_CONTEXT, read: readAs() });
2188
+ live2.send({ type: "quota", quota: payload.quota });
2189
+ live2.close();
2190
+ return null;
2191
+ }
2192
+ return json(payload);
2193
+ }
2139
2194
  }
2140
2195
  }
2141
- }
2142
- const regNote = aggregateNoteText ?? (register ? [registerNote(register.rows), certificateLinks(register.rows)].filter(Boolean).join("\n") : void 0);
2143
- const ops = await operationsLane(env.DB, q.query);
2144
- if (understanding?.intent === "conversational") {
2145
- const queryHash2 = await sha256Hex(q.query);
2146
- const messages2 = [
2147
- { role: "system", content: identityNote(!!member) },
2148
- ...summary ? [{ role: "system", content: `Earlier in this conversation (summarized for continuity):
2196
+ const regNote = aggregateNoteText ?? (register ? [registerNote(register.rows), certificateLinks(register.rows)].filter(Boolean).join("\n") : void 0);
2197
+ const ops = await operationsLane(env.DB, q2.query);
2198
+ if (understanding?.intent === "conversational") {
2199
+ const queryHash2 = await sha256Hex(q2.query);
2200
+ const messages2 = [
2201
+ { role: "system", content: identityNote(!!member) },
2202
+ ...summary ? [{ role: "system", content: `Earlier in this conversation (summarized for continuity):
2149
2203
  ${summary}` }] : [],
2150
- ...keptHistory.slice(-6),
2151
- { role: "user", content: q.query }
2152
- ];
2153
- if (wantsStream) {
2154
- const stream = await generateStream(env, model, messages2, effort);
2155
- if (stream) {
2156
- const encoder = new TextEncoder();
2157
- const sse = new ReadableStream({
2158
- async start(controller) {
2159
- const send = (obj) => controller.enqueue(encoder.encode(`data: ${JSON.stringify(obj)}
2160
-
2161
- `));
2162
- if (readAs()) send({ type: "read", read: readAs() });
2163
- send({ type: "citations", citations: [], context_applied: NO_CONTEXT, quota });
2164
- const stats = {};
2165
- let full = "";
2166
- try {
2167
- for await (const tok of sseTokens(stream, stats)) {
2168
- full += tok;
2169
- send({ type: "token", v: tok });
2170
- }
2171
- } catch {
2204
+ ...keptHistory.slice(-6),
2205
+ { role: "user", content: q2.query }
2206
+ ];
2207
+ if (live2) {
2208
+ if (readAs()) live2.send({ type: "read", read: readAs() });
2209
+ live2.send({ type: "citations", citations: [], context_applied: NO_CONTEXT, quota });
2210
+ const stats = {};
2211
+ let full = "";
2212
+ const stream = await generateStream(env, model, messages2, effort);
2213
+ try {
2214
+ if (stream) {
2215
+ for await (const tok of sseTokens(stream, stats)) {
2216
+ full += tok;
2217
+ live2.send({ type: "token", v: tok });
2172
2218
  }
2173
- const tq2 = await chargeAnswerTokens(env, bucketId, stats, messageChars(messages2), full.length, probeTokensUsed);
2174
- send({ type: "quota", quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2175
- send({ type: "done", model, query_hash: queryHash2, context_applied: NO_CONTEXT, read: readAs() });
2176
- telemetry(env, ctx, tier, "ask", model, true, full.length, queryHash2, q.lang, void 0, telemetryMeta());
2177
- controller.close();
2219
+ } else {
2220
+ live2.send({ type: "token", v: "The generation model is unavailable; please retry." });
2178
2221
  }
2179
- });
2180
- return new Response(sse, {
2181
- headers: { "content-type": "text/event-stream", "cache-control": "no-cache", "x-accel-buffering": "no", ...corsHeaders(req) }
2182
- });
2222
+ } catch {
2223
+ }
2224
+ const tq2 = await chargeAnswerTokens(env, bucketId, stats, messageChars(messages2), full.length, probeTokensUsed);
2225
+ live2.send({ type: "quota", quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2226
+ live2.send({ type: "done", model, query_hash: queryHash2, context_applied: NO_CONTEXT, read: readAs() });
2227
+ telemetry(env, ctx, tier, "ask", model, true, full.length, queryHash2, q2.lang, void 0, telemetryMeta());
2228
+ live2.close();
2229
+ return null;
2183
2230
  }
2184
- }
2185
- let answer2 = await generateOnce(env, model, messages2, effort);
2186
- if (answer2 === null) answer2 = await generateOnce(env, MODELS.fallback, messages2, effort);
2187
- if (answer2 === null) {
2188
- telemetry(env, ctx, tier, "ask", model, false, 0, queryHash2, q.lang, void 0, telemetryMeta());
2189
- return err(502, "generation_failed", "The generation model is unavailable; please retry.");
2190
- }
2191
- telemetry(env, ctx, tier, "ask", model, true, answer2.length, queryHash2, q.lang, void 0, telemetryMeta());
2192
- return json({ answer: answer2, citations: [], model, query_hash: queryHash2, follow_ups: [], context_applied: NO_CONTEXT, read: readAs(), quota });
2193
- }
2194
- if (draftAct) {
2195
- const draftCtxApplied = declaredCtx ? appliedContext(declaredCtx, null) : NO_CONTEXT;
2196
- const queryHash2 = await sha256Hex(q.query);
2197
- const liveCfg = liveDataConfig(env);
2198
- const sessionRaw = rawSessionToken(req);
2199
- let delegation;
2200
- if (!member || !sessionRaw) delegation = { status: "unsigned" };
2201
- else if (!liveCfg) delegation = { status: "not_configured" };
2202
- else {
2203
- const exchanged = await liveTokenFor(env, sessionRaw, member);
2204
- delegation = exchanged.ok ? { status: "ok", token: exchanged.token } : { status: exchanged.reason };
2205
- }
2206
- const verdict = await prepareDraft(env, {
2207
- act: draftAct,
2208
- query: q.query,
2209
- history: keptHistory,
2210
- member,
2211
- delegation,
2212
- platformClientId: liveCfg?.platformClientId,
2213
- model: roleModel(env, "understand")
2214
- });
2215
- console.log("draft act:", draftAct, "\u2192", verdict.status === "draft" ? `draft (${Object.keys(verdict.draft.fields).length} fields)` : `refused (${verdict.reason})`);
2216
- const citations2 = verdict.citation ? [{ ...verdict.citation, corpus: P().publisher.id }] : [];
2217
- const draftPayload = verdict.status === "draft" ? verdict.draft : void 0;
2218
- telemetry(env, ctx, tier, "ask", model, true, verdict.answer.length, queryHash2, q.lang, void 0, telemetryMeta());
2219
- if (wantsStream) {
2220
- return sseResponse(
2221
- [
2222
- ...readAs() ? [{ type: "read", read: readAs() }] : [],
2223
- { type: "citations", citations: citations2, context_applied: draftCtxApplied, ...draftPayload ? { draft: draftPayload } : {}, quota },
2224
- { type: "token", v: verdict.answer },
2225
- { type: "done", model, query_hash: queryHash2, context_applied: draftCtxApplied, read: readAs(), ...ablate ? { ablate } : {} }
2226
- ],
2227
- corsHeaders(req)
2228
- );
2229
- }
2230
- const tq2 = await chargeAnswerTokens(env, bucketId, void 0, q.query.length, verdict.answer.length);
2231
- return json({ answer: verdict.answer, citations: citations2, model, query_hash: queryHash2, follow_ups: [], context_applied: draftCtxApplied, read: readAs(), ...draftPayload ? { draft: draftPayload } : {}, ...ablate ? { ablate } : {}, quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2232
- }
2233
- if (apiCallIntent) {
2234
- const callCtxApplied = declaredCtx ? appliedContext(declaredCtx, null) : NO_CONTEXT;
2235
- const queryHash2 = await sha256Hex(q.query);
2236
- const verdict = await prepareApiCall(env, {
2237
- intent: apiCallIntent,
2238
- query: q.query,
2239
- history: keptHistory,
2240
- member,
2241
- declared: declaredCtx,
2242
- model: roleModel(env, "acts")
2243
- });
2244
- console.log("api_call draft:", apiCallIntent, "\u2192", verdict.status === "draft" ? `draft (${verdict.draft.call.method} ${verdict.draft.call.path})` : `refused (${verdict.reason})`);
2245
- const draftPayload = verdict.status === "draft" ? verdict.draft : void 0;
2246
- telemetry(env, ctx, tier, "ask", model, true, verdict.answer.length, queryHash2, q.lang, void 0, telemetryMeta());
2247
- if (wantsStream) {
2248
- return sseResponse(
2249
- [
2250
- ...readAs() ? [{ type: "read", read: readAs() }] : [],
2251
- { type: "citations", citations: [], context_applied: callCtxApplied, ...draftPayload ? { draft: draftPayload } : {}, quota },
2252
- { type: "token", v: verdict.answer },
2253
- { type: "done", model, query_hash: queryHash2, context_applied: callCtxApplied, read: readAs(), ...ablate ? { ablate } : {} }
2254
- ],
2255
- corsHeaders(req)
2256
- );
2257
- }
2258
- const tq2 = await chargeAnswerTokens(env, bucketId, void 0, q.query.length, verdict.answer.length);
2259
- return json({ answer: verdict.answer, citations: [], model, query_hash: queryHash2, follow_ups: [], context_applied: callCtxApplied, read: readAs(), ...draftPayload ? { draft: draftPayload } : {}, ...ablate ? { ablate } : {}, quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2260
- }
2261
- let liveRecords;
2262
- let accountNote;
2263
- const modelDocHint = named ?? docScope ?? namedDocumentIn(q.query);
2264
- const boundModel = P().publisher.features?.model_plane ? await bindModelNode(env, {
2265
- label: declaredCtx?.label,
2266
- query: q.query,
2267
- standard: standardForDocNumber(modelDocHint?.doc_number),
2268
- standardKeys
2269
- }) ?? await bindConstraintByQuantities(env, q.query, standardKeys) : null;
2270
- if (boundModel) {
2271
- ctxApplied = { ...ctxApplied, model: modelEcho(boundModel) };
2272
- console.log("model plane: bound", boundModel.node_id, `[${boundModel.standard}]`, boundModel.clause?.urn ?? "no-clause", boundModel.gated ? "(gated: license)" : "");
2273
- }
2274
- const modelNote = boundModel && !boundModel.gated ? modelGroundingBlock(boundModel) : void 0;
2275
- let machineVerdict = boundModel && !boundModel.gated && !ablate?.noVerdict ? evaluate(boundModel.content, q.query) : null;
2276
- if (machineVerdict === null && boundModel && !boundModel.gated) {
2277
- try {
2278
- const unitTokens = [...new Set(Object.values(extractParamsWithUnits(q.query, symbolsIn(extractChecks(boundModel.content))).units).map((u) => u.toLowerCase()))];
2279
- if (unitTokens.length) {
2280
- const marks = unitTokens.map(() => "?").join(",");
2281
- const rows = await env.DB.prepare(
2282
- `SELECT lower(coalesce(json_extract(symbols, '$.ascii'), json_extract(symbols, '$.unicode'))) AS sym, dimension_ascii FROM units_db WHERE lower(coalesce(json_extract(symbols, '$.ascii'), json_extract(symbols, '$.unicode'))) IN (${marks})`
2283
- ).bind(...unitTokens).all().catch(() => ({ results: [] }));
2284
- const dimensions = {};
2285
- for (const r of rows.results ?? []) dimensions[String(r.sym)] = String(r.dimension_ascii ?? "");
2286
- machineVerdict = evaluate(boundModel.content, q.query, { dimensions });
2231
+ let answer2 = await generateOnce(env, model, messages2, effort);
2232
+ if (answer2 === null) answer2 = await generateOnce(env, MODELS.fallback, messages2, effort);
2233
+ if (answer2 === null) {
2234
+ telemetry(env, ctx, tier, "ask", model, false, 0, queryHash2, q2.lang, void 0, telemetryMeta());
2235
+ return err(502, "generation_failed", "The generation model is unavailable; please retry.");
2236
+ }
2237
+ telemetry(env, ctx, tier, "ask", model, true, answer2.length, queryHash2, q2.lang, void 0, telemetryMeta());
2238
+ return json({ answer: answer2, citations: [], model, query_hash: queryHash2, follow_ups: [], context_applied: NO_CONTEXT, read: readAs(), quota });
2239
+ }
2240
+ if (draftAct) {
2241
+ const draftCtxApplied = declaredCtx ? appliedContext(declaredCtx, null) : NO_CONTEXT;
2242
+ const queryHash2 = await sha256Hex(q2.query);
2243
+ const liveCfg = liveDataConfig(env);
2244
+ const sessionRaw = rawSessionToken(req);
2245
+ let delegation;
2246
+ if (!member || !sessionRaw) delegation = { status: "unsigned" };
2247
+ else if (!liveCfg) delegation = { status: "not_configured" };
2248
+ else {
2249
+ const exchanged = await liveTokenFor(env, sessionRaw, member);
2250
+ delegation = exchanged.ok ? { status: "ok", token: exchanged.token } : { status: exchanged.reason };
2251
+ }
2252
+ const verdict = await prepareDraft(env, {
2253
+ act: draftAct,
2254
+ query: q2.query,
2255
+ history: keptHistory,
2256
+ member,
2257
+ delegation,
2258
+ platformClientId: liveCfg?.platformClientId,
2259
+ model: roleModel(env, "understand")
2260
+ });
2261
+ console.log("draft act:", draftAct, "\u2192", verdict.status === "draft" ? `draft (${Object.keys(verdict.draft.fields).length} fields)` : `refused (${verdict.reason})`);
2262
+ const citations2 = verdict.citation ? [{ ...verdict.citation, corpus: P().publisher.id }] : [];
2263
+ const draftPayload = verdict.status === "draft" ? verdict.draft : void 0;
2264
+ telemetry(env, ctx, tier, "ask", model, true, verdict.answer.length, queryHash2, q2.lang, void 0, telemetryMeta());
2265
+ if (live2) {
2266
+ if (readAs()) live2.send({ type: "read", read: readAs() });
2267
+ live2.send({ type: "citations", citations: citations2, context_applied: draftCtxApplied, ...draftPayload ? { draft: draftPayload } : {}, quota });
2268
+ live2.send({ type: "token", v: verdict.answer });
2269
+ live2.send({ type: "done", model, query_hash: queryHash2, context_applied: draftCtxApplied, read: readAs(), ...ablate ? { ablate } : {} });
2270
+ live2.close();
2271
+ return null;
2272
+ }
2273
+ const tq2 = await chargeAnswerTokens(env, bucketId, void 0, q2.query.length, verdict.answer.length);
2274
+ return json({ answer: verdict.answer, citations: citations2, model, query_hash: queryHash2, follow_ups: [], context_applied: draftCtxApplied, read: readAs(), ...draftPayload ? { draft: draftPayload } : {}, ...ablate ? { ablate } : {}, quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2275
+ }
2276
+ if (apiCallIntent) {
2277
+ const callCtxApplied = declaredCtx ? appliedContext(declaredCtx, null) : NO_CONTEXT;
2278
+ const queryHash2 = await sha256Hex(q2.query);
2279
+ const verdict = await prepareApiCall(env, {
2280
+ intent: apiCallIntent,
2281
+ query: q2.query,
2282
+ history: keptHistory,
2283
+ member,
2284
+ declared: declaredCtx,
2285
+ model: roleModel(env, "acts")
2286
+ });
2287
+ console.log("api_call draft:", apiCallIntent, "\u2192", verdict.status === "draft" ? `draft (${verdict.draft.call.method} ${verdict.draft.call.path})` : `refused (${verdict.reason})`);
2288
+ const draftPayload = verdict.status === "draft" ? verdict.draft : void 0;
2289
+ telemetry(env, ctx, tier, "ask", model, true, verdict.answer.length, queryHash2, q2.lang, void 0, telemetryMeta());
2290
+ if (live2) {
2291
+ if (readAs()) live2.send({ type: "read", read: readAs() });
2292
+ live2.send({ type: "citations", citations: [], context_applied: callCtxApplied, ...draftPayload ? { draft: draftPayload } : {}, quota });
2293
+ live2.send({ type: "token", v: verdict.answer });
2294
+ live2.send({ type: "done", model, query_hash: queryHash2, context_applied: callCtxApplied, read: readAs(), ...ablate ? { ablate } : {} });
2295
+ live2.close();
2296
+ return null;
2297
+ }
2298
+ const tq2 = await chargeAnswerTokens(env, bucketId, void 0, q2.query.length, verdict.answer.length);
2299
+ return json({ answer: verdict.answer, citations: [], model, query_hash: queryHash2, follow_ups: [], context_applied: callCtxApplied, read: readAs(), ...draftPayload ? { draft: draftPayload } : {}, ...ablate ? { ablate } : {}, quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2300
+ }
2301
+ let liveRecords;
2302
+ let accountNote;
2303
+ const modelDocHint = named ?? docScope ?? namedDocumentIn(q2.query);
2304
+ const boundModel = P().publisher.features?.model_plane ? await bindModelNode(env, {
2305
+ label: declaredCtx?.label,
2306
+ query: q2.query,
2307
+ standard: standardForDocNumber(modelDocHint?.doc_number),
2308
+ standardKeys
2309
+ }) ?? await bindConstraintByQuantities(env, q2.query, standardKeys) : null;
2310
+ if (boundModel) {
2311
+ ctxApplied = { ...ctxApplied, model: modelEcho(boundModel) };
2312
+ console.log("model plane: bound", boundModel.node_id, `[${boundModel.standard}]`, boundModel.clause?.urn ?? "no-clause", boundModel.gated ? "(gated: license)" : "");
2313
+ }
2314
+ const modelNote = boundModel && !boundModel.gated ? modelGroundingBlock(boundModel) : void 0;
2315
+ let machineVerdict = boundModel && !boundModel.gated && !ablate?.noVerdict ? evaluate(boundModel.content, q2.query) : null;
2316
+ if (machineVerdict === null && boundModel && !boundModel.gated) {
2317
+ try {
2318
+ const unitTokens = [...new Set(Object.values(extractParamsWithUnits(q2.query, symbolsIn(extractChecks(boundModel.content))).units).map((u) => u.toLowerCase()))];
2319
+ if (unitTokens.length) {
2320
+ const marks = unitTokens.map(() => "?").join(",");
2321
+ const rows = await env.DB.prepare(
2322
+ `SELECT lower(coalesce(json_extract(symbols, '$.ascii'), json_extract(symbols, '$.unicode'))) AS sym, dimension_ascii FROM units_db WHERE lower(coalesce(json_extract(symbols, '$.ascii'), json_extract(symbols, '$.unicode'))) IN (${marks})`
2323
+ ).bind(...unitTokens).all().catch(() => ({ results: [] }));
2324
+ const dimensions = {};
2325
+ for (const r of rows.results ?? []) dimensions[String(r.sym)] = String(r.dimension_ascii ?? "");
2326
+ machineVerdict = evaluate(boundModel.content, q2.query, { dimensions });
2327
+ }
2328
+ } catch {
2287
2329
  }
2288
- } catch {
2289
2330
  }
2290
- }
2291
- const machineNote = machineVerdict && boundModel ? verdictNote(machineVerdict, boundModel) : void 0;
2292
- const modelNodeRows = async (kind, docNum) => {
2293
- const attempt = (num3) => {
2294
- const sql = num3 ? `SELECT standard, node_id, content FROM model_nodes WHERE kind = '${kind}' AND standard LIKE '%' || ?1` : `SELECT standard, node_id, content FROM model_nodes WHERE kind = '${kind}'`;
2295
- return num3 ? env.DB.prepare(sql).bind(num3) : env.DB.prepare(sql);
2331
+ const machineNote = machineVerdict && boundModel ? verdictNote(machineVerdict, boundModel) : void 0;
2332
+ const modelNodeRows = async (kind, docNum) => {
2333
+ const attempt = (num3) => {
2334
+ const sql = num3 ? `SELECT standard, node_id, content FROM model_nodes WHERE kind = '${kind}' AND standard LIKE '%' || ?1` : `SELECT standard, node_id, content FROM model_nodes WHERE kind = '${kind}'`;
2335
+ return num3 ? env.DB.prepare(sql).bind(num3) : env.DB.prepare(sql);
2336
+ };
2337
+ let rows = await attempt(docNum).all().catch(() => ({ results: [] }));
2338
+ if (!(rows.results ?? []).length && docNum && docNum.includes("-")) {
2339
+ rows = await attempt(docNum.replace(/-\d+$/, "")).all().catch(() => ({ results: [] }));
2340
+ }
2341
+ return rows;
2296
2342
  };
2297
- let rows = await attempt(docNum).all().catch(() => ({ results: [] }));
2298
- if (!(rows.results ?? []).length && docNum && docNum.includes("-")) {
2299
- rows = await attempt(docNum.replace(/-\d+$/, "")).all().catch(() => ({ results: [] }));
2343
+ let conditionVerdict = null;
2344
+ let conditionStandard = null;
2345
+ if (!machineVerdict && !boundModel && !ablate?.noVerdict && P().publisher.features?.model_plane) {
2346
+ const ql = q2.query.toLowerCase();
2347
+ const severityWord = /\b(severity|test|valid|tolerance|condition|within)\b/.test(ql);
2348
+ const stated = quantitiesIn(q2.query);
2349
+ if (severityWord && Object.keys(stated).length >= 1) {
2350
+ const docNum = modelDocHint?.doc_number;
2351
+ const rows = await modelNodeRows("condition_set", docNum);
2352
+ const candidates = (rows.results ?? []).filter((r) => {
2353
+ const entry = licensedEntryForPackage(String(r.standard));
2354
+ return !entry || (standardKeys?.has(entry.key) ?? false);
2355
+ });
2356
+ const v = evaluateConditionSets(
2357
+ candidates.map((r) => ({ node_id: String(r.node_id), content: JSON.parse(String(r.content ?? "{}")) })),
2358
+ q2.query
2359
+ );
2360
+ if (v) {
2361
+ conditionVerdict = v;
2362
+ conditionStandard = String(rows.results?.[0]?.standard ?? "");
2363
+ }
2364
+ }
2300
2365
  }
2301
- return rows;
2302
- };
2303
- let conditionVerdict = null;
2304
- let conditionStandard = null;
2305
- if (!machineVerdict && !boundModel && !ablate?.noVerdict && P().publisher.features?.model_plane) {
2306
- const ql = q.query.toLowerCase();
2307
- const severityWord = /\b(severity|test|valid|tolerance|condition|within)\b/.test(ql);
2308
- const stated = quantitiesIn(q.query);
2309
- if (severityWord && Object.keys(stated).length >= 1) {
2366
+ const conditionNote = conditionVerdict && conditionStandard ? `${conditionVerdict.note} (computed from the ${conditionStandard} condition sets \u2014 machine evaluation, cite the package's clause.)` : void 0;
2367
+ const verdictBlock = machineVerdict ? {
2368
+ unit_id: boundModel.node_id,
2369
+ type: "verdict",
2370
+ docidentifier: `${P().publisher.name} model (${boundModel.standard})`,
2371
+ payload: {
2372
+ verdict: machineVerdict.verdict,
2373
+ on_violation: machineVerdict.on_violation,
2374
+ violation_meaning: machineVerdict.violation_meaning,
2375
+ missing: machineVerdict.missing,
2376
+ checks: machineVerdict.checks
2377
+ }
2378
+ } : null;
2379
+ if (machineVerdict) console.log("verdict engine:", boundModel.node_id, "\u2192", machineVerdict.verdict.toUpperCase(), machineVerdict.missing.length ? `(missing ${machineVerdict.missing.join(",")})` : "");
2380
+ const conditionBlock = conditionVerdict ? {
2381
+ unit_id: conditionVerdict.matched[0] ?? conditionVerdict.nearest.node_id,
2382
+ type: "verdict",
2383
+ docidentifier: `${P().publisher.name} model (${conditionStandard})`,
2384
+ payload: {
2385
+ verdict: conditionVerdict.verdict,
2386
+ missing: [],
2387
+ checks: conditionVerdict.checks.map((c) => ({
2388
+ expression: `${c.quantity_kind} within ${c.band}`,
2389
+ values: { stated: c.stated },
2390
+ result: c.in_band
2391
+ })),
2392
+ ...conditionVerdict.nearest ? { nearest: conditionVerdict.nearest } : { matched: conditionVerdict.matched }
2393
+ }
2394
+ } : null;
2395
+ if (conditionVerdict) console.log("condition engine:", conditionVerdict.matched.join("|") || conditionVerdict.nearest.node_id, "\u2192", conditionVerdict.verdict.toUpperCase());
2396
+ let aggregationVerdict = null;
2397
+ let aggregationStandard = null;
2398
+ if (!machineVerdict && !boundModel && !conditionVerdict && P().publisher.features?.model_plane) {
2310
2399
  const docNum = modelDocHint?.doc_number;
2311
- const rows = await modelNodeRows("condition_set", docNum);
2400
+ const rows = await modelNodeRows("table", docNum);
2312
2401
  const candidates = (rows.results ?? []).filter((r) => {
2313
2402
  const entry = licensedEntryForPackage(String(r.standard));
2314
2403
  return !entry || (standardKeys?.has(entry.key) ?? false);
2315
2404
  });
2316
- const v = evaluateConditionSets(
2405
+ const v = evaluateAggregation(
2317
2406
  candidates.map((r) => ({ node_id: String(r.node_id), content: JSON.parse(String(r.content ?? "{}")) })),
2318
- q.query
2407
+ q2.query
2319
2408
  );
2320
2409
  if (v) {
2321
- conditionVerdict = v;
2322
- conditionStandard = String(rows.results?.[0]?.standard ?? "");
2410
+ aggregationVerdict = v;
2411
+ aggregationStandard = String(rows.results?.[0]?.standard ?? "");
2323
2412
  }
2324
2413
  }
2325
- }
2326
- const conditionNote = conditionVerdict && conditionStandard ? `${conditionVerdict.note} (computed from the ${conditionStandard} condition sets \u2014 machine evaluation, cite the package's clause.)` : void 0;
2327
- const verdictBlock = machineVerdict ? {
2328
- unit_id: boundModel.node_id,
2329
- type: "verdict",
2330
- docidentifier: `${P().publisher.name} model (${boundModel.standard})`,
2331
- payload: {
2332
- verdict: machineVerdict.verdict,
2333
- on_violation: machineVerdict.on_violation,
2334
- violation_meaning: machineVerdict.violation_meaning,
2335
- missing: machineVerdict.missing,
2336
- checks: machineVerdict.checks
2337
- }
2338
- } : null;
2339
- if (machineVerdict) console.log("verdict engine:", boundModel.node_id, "\u2192", machineVerdict.verdict.toUpperCase(), machineVerdict.missing.length ? `(missing ${machineVerdict.missing.join(",")})` : "");
2340
- const conditionBlock = conditionVerdict ? {
2341
- unit_id: conditionVerdict.matched[0] ?? conditionVerdict.nearest.node_id,
2342
- type: "verdict",
2343
- docidentifier: `${P().publisher.name} model (${conditionStandard})`,
2344
- payload: {
2345
- verdict: conditionVerdict.verdict,
2346
- missing: [],
2347
- checks: conditionVerdict.checks.map((c) => ({
2348
- expression: `${c.quantity_kind} within ${c.band}`,
2349
- values: { stated: c.stated },
2350
- result: c.in_band
2351
- })),
2352
- ...conditionVerdict.nearest ? { nearest: conditionVerdict.nearest } : { matched: conditionVerdict.matched }
2353
- }
2354
- } : null;
2355
- if (conditionVerdict) console.log("condition engine:", conditionVerdict.matched.join("|") || conditionVerdict.nearest.node_id, "\u2192", conditionVerdict.verdict.toUpperCase());
2356
- let aggregationVerdict = null;
2357
- let aggregationStandard = null;
2358
- if (!machineVerdict && !boundModel && !conditionVerdict && P().publisher.features?.model_plane) {
2359
- const docNum = modelDocHint?.doc_number;
2360
- const rows = await modelNodeRows("table", docNum);
2361
- const candidates = (rows.results ?? []).filter((r) => {
2362
- const entry = licensedEntryForPackage(String(r.standard));
2363
- return !entry || (standardKeys?.has(entry.key) ?? false);
2364
- });
2365
- const v = evaluateAggregation(
2366
- candidates.map((r) => ({ node_id: String(r.node_id), content: JSON.parse(String(r.content ?? "{}")) })),
2367
- q.query
2368
- );
2369
- if (v) {
2370
- aggregationVerdict = v;
2371
- aggregationStandard = String(rows.results?.[0]?.standard ?? "");
2372
- }
2373
- }
2374
- const aggregationNote = aggregationVerdict ? `${aggregationVerdict.note}${aggregationStandard ? ` (standard ${aggregationStandard}.)` : ""}` : void 0;
2375
- const aggregationBlock = aggregationVerdict ? {
2376
- unit_id: aggregationVerdict.table,
2377
- type: "verdict",
2378
- docidentifier: `${P().publisher.name} model table${aggregationStandard ? ` (${aggregationStandard})` : ""}`,
2379
- payload: {
2380
- check: `${aggregationVerdict.operation}: ${aggregationVerdict.column ?? aggregationVerdict.table_title ?? aggregationVerdict.table} = ${aggregationVerdict.value}${aggregationVerdict.unit ? ` ${aggregationVerdict.unit}` : ""}`,
2381
- meaning: aggregationVerdict.table_title,
2382
- operation: aggregationVerdict.operation,
2383
- value: aggregationVerdict.value,
2384
- unit: aggregationVerdict.unit,
2385
- row: aggregationVerdict.row
2386
- }
2387
- } : null;
2388
- if (aggregationVerdict) console.log("aggregation engine:", aggregationVerdict.operation, aggregationVerdict.table, "\u2192", aggregationVerdict.value);
2389
- let boundaryNote = null;
2390
- let boundaryBoost;
2391
- let licensedDocNumbers;
2392
- let editionBoost;
2393
- try {
2394
- const fam = understanding?.doc ? refCodec().familyOf(understanding.doc) : null;
2395
- if (fam) {
2396
- const row = await env.DB.prepare("SELECT edition FROM documents WHERE family = ?1 AND active = 1 ORDER BY edition DESC LIMIT 1").bind(fam).first().catch(() => null);
2397
- if (row?.edition) editionBoost = String(row.edition);
2414
+ const aggregationNote = aggregationVerdict ? `${aggregationVerdict.note}${aggregationStandard ? ` (standard ${aggregationStandard}.)` : ""}` : void 0;
2415
+ const aggregationBlock = aggregationVerdict ? {
2416
+ unit_id: aggregationVerdict.table,
2417
+ type: "verdict",
2418
+ docidentifier: `${P().publisher.name} model table${aggregationStandard ? ` (${aggregationStandard})` : ""}`,
2419
+ payload: {
2420
+ check: `${aggregationVerdict.operation}: ${aggregationVerdict.column ?? aggregationVerdict.table_title ?? aggregationVerdict.table} = ${aggregationVerdict.value}${aggregationVerdict.unit ? ` ${aggregationVerdict.unit}` : ""}`,
2421
+ meaning: aggregationVerdict.table_title,
2422
+ operation: aggregationVerdict.operation,
2423
+ value: aggregationVerdict.value,
2424
+ unit: aggregationVerdict.unit,
2425
+ row: aggregationVerdict.row
2426
+ }
2427
+ } : null;
2428
+ if (aggregationVerdict) console.log("aggregation engine:", aggregationVerdict.operation, aggregationVerdict.table, "\u2192", aggregationVerdict.value);
2429
+ let boundaryNote = null;
2430
+ let boundaryBoost;
2431
+ let licensedDocNumbers;
2432
+ let editionBoost;
2433
+ try {
2434
+ const fam = understanding?.doc ? refCodec().familyOf(understanding.doc) : null;
2435
+ if (fam) {
2436
+ const row = await env.DB.prepare("SELECT edition FROM documents WHERE family = ?1 AND active = 1 ORDER BY edition DESC LIMIT 1").bind(fam).first().catch(() => null);
2437
+ if (row?.edition) editionBoost = String(row.edition);
2438
+ }
2439
+ } catch {
2398
2440
  }
2399
- } catch {
2400
- }
2401
- const lexicalBoost = [boundaryBoost, editionBoost].filter(Boolean).join(" ") || void 0;
2402
- if (P().sources?.licensed?.length) {
2403
- const match = matchLicensedTopic(q.query, P().sources.licensed);
2404
- if (match && (standardKeys?.has(match.entry.key) ?? false)) {
2405
- licensedDocNumbers = match.entry.package ? [match.entry.package] : void 0;
2406
- } else if (match && !(standardKeys?.has(match.entry.key) ?? false)) {
2407
- const docNum = match.entry.doc_number ?? "";
2408
- boundaryBoost = docNum || void 0;
2409
- let citing = [];
2410
- if (docNum) {
2411
- const rows = await env.DB.prepare(
2412
- "SELECT n.label AS label FROM graph_edges e JOIN graph_nodes n ON e.src = n.id WHERE e.kind = 'cites' AND e.dst LIKE ?1 LIMIT 4"
2413
- ).bind(`%${docNum}%`).all().catch(() => ({ results: [] }));
2414
- citing = (rows.results ?? []).map((r) => String(r.label ?? "").replace(/^doc:/, ""));
2441
+ const lexicalBoost = [boundaryBoost, editionBoost].filter(Boolean).join(" ") || void 0;
2442
+ if (P().sources?.licensed?.length) {
2443
+ const match = matchLicensedTopic(q2.query, P().sources.licensed);
2444
+ if (match && (standardKeys?.has(match.entry.key) ?? false)) {
2445
+ licensedDocNumbers = match.entry.package ? [match.entry.package] : void 0;
2446
+ } else if (match && !(standardKeys?.has(match.entry.key) ?? false)) {
2447
+ const docNum = match.entry.doc_number ?? "";
2448
+ boundaryBoost = docNum || void 0;
2449
+ let citing = [];
2450
+ if (docNum) {
2451
+ const rows = await env.DB.prepare(
2452
+ "SELECT n.label AS label FROM graph_edges e JOIN graph_nodes n ON e.src = n.id WHERE e.kind = 'cites' AND e.dst LIKE ?1 LIMIT 4"
2453
+ ).bind(`%${docNum}%`).all().catch(() => ({ results: [] }));
2454
+ citing = (rows.results ?? []).map((r) => String(r.label ?? "").replace(/^doc:/, ""));
2455
+ }
2456
+ boundaryNote = boundaryNoteText(match, citing);
2415
2457
  }
2416
- boundaryNote = boundaryNoteText(match, citing);
2417
2458
  }
2418
- }
2419
- const routeMode = ablate?.route ?? "adaptive";
2420
- const routed = routeMode === "adaptive" ? routeFor(understanding, q.query) : { route: routeMode, features: [`forced-${routeMode}`] };
2421
- const routeSubset = ablate?.stages ?? (routed.route === "fast" ? fastRouteStages(STAGE_NAMES) : null);
2422
- if (routeSubset) console.log("route:", routed.route, routed.features.join("+"), "\u2014", STAGE_NAMES.length - routeSubset.length, "lanes dropped");
2423
- try {
2424
- const tR = Date.now();
2425
- if (declaredCtx?.kind === "account") {
2426
- const live = await resolveLiveAccount(env, rawSessionToken(req), member);
2427
- if (live.status === "ok") {
2428
- liveRecords = live.records;
2429
- ctxApplied = appliedContext(declaredCtx, null, void 0, {
2430
- read_at: live.readAt,
2431
- stores: live.stores,
2432
- records: live.records.length
2433
- });
2434
- const lines = live.records.map(
2435
- (r) => `- ${r.label} [${[r.status, r.detail].filter(Boolean).join("; ")}] ${r.url}`
2436
- );
2437
- accountNote = `Live account data (read ${live.readAt} from ${P().prompts.vars.account_note_source ?? `the user's own ${P().publisher.product_name} account`} \u2014 exactly what they may see, never more):
2459
+ const routeMode = ablate?.route ?? "adaptive";
2460
+ const routed = routeMode === "adaptive" ? routeFor(understanding, q2.query) : { route: routeMode, features: [`forced-${routeMode}`] };
2461
+ const routeSubset = ablate?.stages ?? (routed.route === "fast" ? fastRouteStages(STAGE_NAMES) : null);
2462
+ if (routeSubset) console.log("route:", routed.route, routed.features.join("+"), "\u2014", STAGE_NAMES.length - routeSubset.length, "lanes dropped");
2463
+ try {
2464
+ const tR = Date.now();
2465
+ if (declaredCtx?.kind === "account") {
2466
+ const live3 = await resolveLiveAccount(env, rawSessionToken(req), member);
2467
+ if (live3.status === "ok") {
2468
+ liveRecords = live3.records;
2469
+ ctxApplied = appliedContext(declaredCtx, null, void 0, {
2470
+ read_at: live3.readAt,
2471
+ stores: live3.stores,
2472
+ records: live3.records.length
2473
+ });
2474
+ const lines = live3.records.map(
2475
+ (r) => `- ${r.label} [${[r.status, r.detail].filter(Boolean).join("; ")}] ${r.url}`
2476
+ );
2477
+ accountNote = `Live account data (read ${live3.readAt} from ${P().prompts.vars.account_note_source ?? `the user's own ${P().publisher.product_name} account`} \u2014 exactly what they may see, never more):
2438
2478
  ` + (lines.length ? lines.join("\n") : "(the account surfaces answered empty)") + `
2439
2479
  Answer account questions from these records ONLY: name the record when you use it, never invent one, and say honestly when they do not hold the answer. The corpus passages still ground the regulatory claims (the requirements, the procedures); the records are the user's own work.`;
2440
- console.log("live data:", live.records.length, "records from", live.stores.join("+") || "none");
2441
- } else {
2442
- const note = live.reason === "sign_in_required" ? "sign-in-required" : live.reason === "window_expired" ? "live-window-expired" : "live-unavailable";
2443
- ctxApplied = appliedContext(declaredCtx, null, note);
2444
- accountNote = live.reason === "sign_in_required" ? "Context note: the user asked with the 'my account' context but is not signed in \u2014 the account data was NOT read; answer from the corpus and say so." : live.reason === "window_expired" ? "Context note: the user's live access window lapsed \u2014 the account data was NOT read; answer from the corpus, say the live read did not happen, and suggest signing in again to refresh it." : "Context note: the live account read was refused or unreachable \u2014 the account data was NOT read; answer from the corpus and say so honestly.";
2445
- console.log("live data: not read \u2014", live.reason);
2480
+ console.log("live data:", live3.records.length, "records from", live3.stores.join("+") || "none");
2481
+ } else {
2482
+ const note = live3.reason === "sign_in_required" ? "sign-in-required" : live3.reason === "window_expired" ? "live-window-expired" : "live-unavailable";
2483
+ ctxApplied = appliedContext(declaredCtx, null, note);
2484
+ accountNote = live3.reason === "sign_in_required" ? "Context note: the user asked with the 'my account' context but is not signed in \u2014 the account data was NOT read; answer from the corpus and say so." : live3.reason === "window_expired" ? "Context note: the user's live access window lapsed \u2014 the account data was NOT read; answer from the corpus, say the live read did not happen, and suggest signing in again to refresh it." : "Context note: the live account read was refused or unreachable \u2014 the account data was NOT read; answer from the corpus and say so honestly.";
2485
+ console.log("live data: not read \u2014", live3.reason);
2486
+ }
2446
2487
  }
2447
- }
2448
- retrieved = await retrieve(env, q.query, {
2449
- prev,
2450
- understanding,
2451
- federate,
2452
- warmEmbed,
2453
- graphDocNumbers,
2454
- sealScope: declaredScoped ? docScope : null,
2455
- optimisticHits,
2456
- optimisticVec,
2457
- datasetScope: narrowed ? corpora : null,
2458
- standardKeys,
2459
- lexicalBoost,
2460
- licensedDocNumbers,
2461
- ...routeSubset ? { stageSubset: routeSubset } : {}
2462
- });
2463
- stageTiming["retrieve-core"] = Date.now() - tR;
2464
- console.log("stage: retrieve", Date.now() - tR, "ms");
2465
- const docScoped = !!understanding?.doc_number;
2466
- const gradePromise = docScoped ? Promise.resolve("skipped-doc-scoped") : (() => {
2467
- const tg = Date.now();
2468
- return gradeRetrieval(env.AI, roleModel(env, "grader"), q.query, retrieved.hits.map((h) => h.text)).catch(() => null).finally(() => stageTiming["grade"] = Date.now() - tg);
2488
+ retrieved = await retrieve(env, q2.query, {
2489
+ prev,
2490
+ understanding,
2491
+ federate,
2492
+ warmEmbed,
2493
+ graphDocNumbers,
2494
+ sealScope: declaredScoped ? docScope : null,
2495
+ optimisticHits,
2496
+ optimisticVec,
2497
+ datasetScope: narrowed ? corpora : null,
2498
+ standardKeys,
2499
+ lexicalBoost,
2500
+ licensedDocNumbers,
2501
+ ...routeSubset ? { stageSubset: routeSubset } : {}
2502
+ });
2503
+ stageTiming["retrieve-core"] = Date.now() - tR;
2504
+ console.log("stage: retrieve", Date.now() - tR, "ms");
2505
+ const docScoped = !!understanding?.doc_number;
2506
+ const gradePromise = docScoped ? Promise.resolve("skipped-doc-scoped") : (() => {
2507
+ const tg = Date.now();
2508
+ return gradeRetrieval(env.AI, roleModel(env, "grader"), q2.query, retrieved.hits.map((h) => h.text)).catch(() => null).finally(() => stageTiming["grade"] = Date.now() - tg);
2509
+ })();
2510
+ if (retrieved.hits.length >= 4 && (member || understanding?.complexity === "complex")) {
2511
+ const tl = Date.now();
2512
+ const reordered = await listwiseRerank(env, MODELS.listwise, understanding?.standalone_query || q2.query, retrieved.hits);
2513
+ stageTiming.listwise = Date.now() - tl;
2514
+ if (reordered) {
2515
+ console.log("listwise: reordered", reordered[0]?.metadata?.docidentifier ?? "?", "to top");
2516
+ retrieved = { ...retrieved, hits: reordered };
2517
+ }
2518
+ }
2519
+ const grade = await gradePromise;
2520
+ stageTiming.retrieve = Date.now() - tR;
2521
+ console.log("stage: grade+listwise", Date.now() - tR, "ms since retrieve start | grade:", grade);
2522
+ if (grade === "weak" && understanding?.docidentifier) {
2523
+ const broaden = `${understanding.standalone_query || q2.query} ${understanding.docidentifier}`.trim();
2524
+ const tc = Date.now();
2525
+ const second = await retrieve(env, q2.query, { prev, understanding, queryOverride: broaden, federate, datasetScope: narrowed ? corpora : null, standardKeys, sealScope: declaredScoped ? docScope : null, lexicalBoost, ...routeSubset ? { stageSubset: routeSubset } : {} });
2526
+ const grade2 = await gradeRetrieval(env.AI, roleModel(env, "grader"), q2.query, second.hits.map((h) => h.text));
2527
+ stageTiming.corrective = Date.now() - tc;
2528
+ if (grade2 === "good") retrieved = second;
2529
+ }
2530
+ } catch (e) {
2531
+ console.log("ask: retrieval failed:", String(e).slice(0, 300));
2532
+ telemetry(env, ctx, tier, "ask", MODELS.embed, false, 0, await sha256Hex(q2.query), q2.lang, void 0, telemetryMeta());
2533
+ return finishLine("Search is briefly busy \u2014 please retry in a moment.");
2534
+ }
2535
+ const { hits } = retrieved;
2536
+ if (hits.length === 0 && !liveRecords?.length && (!boundModel || boundModel.gated)) {
2537
+ const answer2 = licenseBoundaryRefusal(modelDocHint?.doc_number ?? understanding?.doc_number ?? null, standardKeys) ?? refusalAnswer();
2538
+ const out2 = { answer: answer2, citations: [], model, query_hash: await sha256Hex(q2.query), context_applied: ctxApplied, ...ablate ? { ablate } : {}, route: { route: routed.route, features: routed.features } };
2539
+ telemetry(env, ctx, tier, "ask", model, true, answer2.length, out2.query_hash, q2.lang, void 0, telemetryMeta());
2540
+ return json({ ...out2, quota });
2541
+ }
2542
+ const processNote = understanding?.process_intent ? P().retrieval.process_note : void 0;
2543
+ const licenseNote = licenseBoundaryNote(modelDocHint?.doc_number ?? understanding?.doc_number ?? null, standardKeys);
2544
+ const glossaryForNote = (() => {
2545
+ const g = retrieved.glossary ?? [];
2546
+ if (!g.length) return g;
2547
+ const dt = (understanding?.defined_terms ?? []).map((s) => s.toLowerCase());
2548
+ if (!dt.length) return g;
2549
+ const matched = g.filter((x) => dt.some((d) => x.term.toLowerCase().includes(d.split(" ")[0]) || d.includes(x.term.toLowerCase().split(" ")[0])));
2550
+ return matched.length ? matched : g;
2469
2551
  })();
2470
- if (retrieved.hits.length >= 4 && (member || understanding?.complexity === "complex")) {
2471
- const tl = Date.now();
2472
- const reordered = await listwiseRerank(env, MODELS.listwise, understanding?.standalone_query || q.query, retrieved.hits);
2473
- stageTiming.listwise = Date.now() - tl;
2474
- if (reordered) {
2475
- console.log("listwise: reordered", reordered[0]?.metadata?.docidentifier ?? "?", "to top");
2476
- retrieved = { ...retrieved, hits: reordered };
2552
+ const vocabNote = glossaryForNote.length ? "Vocabulary binding \u2014 defined terms in the indexed corpus that may name this question's subject:\n" + glossaryForNote.map((g) => `- ${g.term} (${g.docidentifier}): ${g.definition}`).join("\n") + "\nIf the question describes a symptom or behavior in everyday words, OPEN the answer by naming the matching defined term, quote its definition, and cite its defining publication; keep using that term throughout. Match TIME SCALE carefully: change under a constant load over minutes/hours is creep; change over months/years of use is span stability or durability \u2014 do not call long-term drift creep." : void 0;
2553
+ const opsHits = ops.matched.map((m) => ({
2554
+ id: `ops:${m.anchor}`,
2555
+ score: 0.999,
2556
+ metadata: {
2557
+ doc_id: "smart-ops:catalog",
2558
+ docidentifier: m.docidentifier ?? "Platform API",
2559
+ doctype: "api-operation",
2560
+ doc_number: "",
2561
+ edition: "",
2562
+ language: "en",
2563
+ clause_anchor: m.anchor,
2564
+ clause_title: `Platform operation ${m.anchor}`,
2565
+ tier: "curated",
2566
+ corpus: "smart-ops",
2567
+ text_ref: ""
2568
+ },
2569
+ text: `${m.anchor} \u2014 ${m.text}`
2570
+ }));
2571
+ const { messages, usedHits } = buildMessages(
2572
+ q2.query,
2573
+ [...opsHits, ...hits],
2574
+ q2.lang,
2575
+ keptHistory,
2576
+ // stage-extracted graph facts (GraphRAG) ride the same note channel
2577
+ [ops.note, processNote, eNote, regNote, contextNote(declaredCtx, docScope), accountNote, modelNote, vocabNote, memNote, machineNote, conditionNote, aggregationNote, boundaryNote, licenseNote, ...retrieved.notes ?? []].filter(Boolean).join("\n") || void 0,
2578
+ summary,
2579
+ budget
2580
+ );
2581
+ await attachFigureImages(env, messages, usedHits, q2.query);
2582
+ const pendingTools = [];
2583
+ if (multiTurn && env.DB && !nameplate?.manufacturer) {
2584
+ try {
2585
+ const imageTurn = typeof withImage === "string";
2586
+ if (imageTurn) mark("Reading the photograph to identify the instrument");
2587
+ for (let round = 0; round < 3; round++) {
2588
+ const imageNote = imageTurn && round === 0 ? "A photograph of the instrument is attached after this message. Transcribe the maker, model and any printed certificate number you can read FROM THE PHOTOGRAPH ITSELF, then emit the TOOL line with those tokens as the query \u2014 never invented, and never tokens taken from any register content elsewhere in the context (a register row's holder is not the plate's maker; observed live 2026-10-04)." : "";
2589
+ const continueNote = round > 0 ? "The tool results above are in hand. Answer now without a TOOL line, or issue exactly one more TOOL line if one more query is genuinely needed." : "";
2590
+ const probeMessages = [...messages.slice(0, -1), { role: "user", content: [TOOL_DECLARATION, imageNote, continueNote, q2.query].filter(Boolean).join("\n\n") }];
2591
+ if (imageTurn && round === 0) probeMessages.push({ role: "user", content: [{ type: "image_url", image_url: { url: withImage } }] });
2592
+ const probe = await env.AI.run(model, {
2593
+ messages: probeMessages,
2594
+ max_tokens: 700,
2595
+ reasoning_effort: imageTurn && round === 0 ? "high" : "low"
2596
+ });
2597
+ const probeText = typeof probe?.response === "string" ? probe?.response : probe?.choices?.[0]?.message?.content ?? "";
2598
+ probeTokensUsed += usageTotal(probe?.usage) ?? 0;
2599
+ const nativeCall = Array.isArray(probe?.tool_calls) ? probe.tool_calls[0]?.function : null;
2600
+ const call = nativeCall ? {
2601
+ name: String(nativeCall.name ?? ""),
2602
+ args: (() => {
2603
+ try {
2604
+ return typeof nativeCall.arguments === "string" ? JSON.parse(nativeCall.arguments) : nativeCall.arguments ?? {};
2605
+ } catch {
2606
+ return {};
2607
+ }
2608
+ })()
2609
+ } : parseToolCall(probeText);
2610
+ if (!call || !call.name) break;
2611
+ const result = await runTool(env, call);
2612
+ if (!result) break;
2613
+ mark(`${result.name} \u2014 ${result.query}`.slice(0, 140));
2614
+ messages.push({ role: "system", content: toolNote(result) });
2615
+ pendingTools.push(result);
2616
+ console.log("agent-tool:", result.name, JSON.stringify(result.query), "->", result.output.slice(0, 80));
2617
+ }
2618
+ } catch {
2477
2619
  }
2478
2620
  }
2479
- const grade = await gradePromise;
2480
- stageTiming.retrieve = Date.now() - tR;
2481
- console.log("stage: grade+listwise", Date.now() - tR, "ms since retrieve start | grade:", grade);
2482
- if (grade === "weak" && understanding?.docidentifier) {
2483
- const broaden = `${understanding.standalone_query || q.query} ${understanding.docidentifier}`.trim();
2484
- const tc = Date.now();
2485
- const second = await retrieve(env, q.query, { prev, understanding, queryOverride: broaden, federate, datasetScope: narrowed ? corpora : null, standardKeys, sealScope: declaredScoped ? docScope : null, lexicalBoost, ...routeSubset ? { stageSubset: routeSubset } : {} });
2486
- const grade2 = await gradeRetrieval(env.AI, roleModel(env, "grader"), q.query, second.hits.map((h) => h.text));
2487
- stageTiming.corrective = Date.now() - tc;
2488
- if (grade2 === "good") retrieved = second;
2489
- }
2490
- } catch (e) {
2491
- console.log("ask: retrieval failed:", String(e).slice(0, 300));
2492
- telemetry(env, ctx, tier, "ask", MODELS.embed, false, 0, await sha256Hex(q.query), q.lang, void 0, telemetryMeta());
2493
- return err(503, "retrieval_unavailable", "Search is briefly busy \u2014 please retry in a moment.");
2494
- }
2495
- const { hits } = retrieved;
2496
- if (hits.length === 0 && !liveRecords?.length && (!boundModel || boundModel.gated)) {
2497
- const answer2 = licenseBoundaryRefusal(modelDocHint?.doc_number ?? understanding?.doc_number ?? null, standardKeys) ?? refusalAnswer();
2498
- const out2 = { answer: answer2, citations: [], model, query_hash: await sha256Hex(q.query), context_applied: ctxApplied, ...ablate ? { ablate } : {}, route: { route: routed.route, features: routed.features } };
2499
- telemetry(env, ctx, tier, "ask", model, true, answer2.length, out2.query_hash, q.lang, void 0, telemetryMeta());
2500
- return json({ ...out2, quota });
2501
- }
2502
- const processNote = understanding?.process_intent ? P().retrieval.process_note : void 0;
2503
- const licenseNote = licenseBoundaryNote(modelDocHint?.doc_number ?? understanding?.doc_number ?? null, standardKeys);
2504
- const glossaryForNote = (() => {
2505
- const g = retrieved.glossary ?? [];
2506
- if (!g.length) return g;
2507
- const dt = (understanding?.defined_terms ?? []).map((s) => s.toLowerCase());
2508
- if (!dt.length) return g;
2509
- const matched = g.filter((x) => dt.some((d) => x.term.toLowerCase().includes(d.split(" ")[0]) || d.includes(x.term.toLowerCase().split(" ")[0])));
2510
- return matched.length ? matched : g;
2511
- })();
2512
- const vocabNote = glossaryForNote.length ? "Vocabulary binding \u2014 defined terms in the indexed corpus that may name this question's subject:\n" + glossaryForNote.map((g) => `- ${g.term} (${g.docidentifier}): ${g.definition}`).join("\n") + "\nIf the question describes a symptom or behavior in everyday words, OPEN the answer by naming the matching defined term, quote its definition, and cite its defining publication; keep using that term throughout. Match TIME SCALE carefully: change under a constant load over minutes/hours is creep; change over months/years of use is span stability or durability \u2014 do not call long-term drift creep." : void 0;
2513
- const opsHits = ops.matched.map((m) => ({
2514
- id: `ops:${m.anchor}`,
2515
- score: 0.999,
2516
- metadata: {
2517
- doc_id: "smart-ops:catalog",
2518
- docidentifier: m.docidentifier ?? "Platform API",
2519
- doctype: "api-operation",
2520
- doc_number: "",
2521
- edition: "",
2522
- language: "en",
2523
- clause_anchor: m.anchor,
2524
- clause_title: `Platform operation ${m.anchor}`,
2525
- tier: "curated",
2526
- corpus: "smart-ops",
2527
- text_ref: ""
2528
- },
2529
- text: `${m.anchor} \u2014 ${m.text}`
2530
- }));
2531
- const { messages, usedHits } = buildMessages(
2532
- q.query,
2533
- [...opsHits, ...hits],
2534
- q.lang,
2535
- keptHistory,
2536
- // stage-extracted graph facts (GraphRAG) ride the same note channel
2537
- [ops.note, processNote, eNote, regNote, contextNote(declaredCtx, docScope), accountNote, modelNote, vocabNote, memNote, machineNote, conditionNote, aggregationNote, boundaryNote, licenseNote, ...retrieved.notes ?? []].filter(Boolean).join("\n") || void 0,
2538
- summary,
2539
- budget
2540
- );
2541
- await attachFigureImages(env, messages, usedHits, q.query);
2542
- const pendingTools = [];
2543
- let probeTokensUsed = 0;
2544
- if (multiTurn && env.DB) {
2545
- try {
2546
- const imageTurn = typeof withImage === "string";
2547
- if (imageTurn) mark("Reading the photograph to identify the instrument");
2548
- for (let round = 0; round < 3; round++) {
2549
- const imageNote = imageTurn && round === 0 ? "A photograph of the instrument is attached after this message. Transcribe the maker, model and any printed certificate number you can read FROM THE PHOTOGRAPH ITSELF, then emit the TOOL line with those tokens as the query \u2014 never invented, and never tokens taken from any register content elsewhere in the context (a register row's holder is not the plate's maker; observed live 2026-10-04)." : "";
2550
- const continueNote = round > 0 ? "The tool results above are in hand. Answer now without a TOOL line, or issue exactly one more TOOL line if one more query is genuinely needed." : "";
2551
- const probeMessages = [...messages.slice(0, -1), { role: "user", content: [TOOL_DECLARATION, imageNote, continueNote, q.query].filter(Boolean).join("\n\n") }];
2552
- if (imageTurn && round === 0) probeMessages.push({ role: "user", content: [{ type: "image_url", image_url: { url: withImage } }] });
2553
- const probe = await env.AI.run(model, {
2554
- messages: probeMessages,
2555
- max_tokens: 700,
2556
- reasoning_effort: imageTurn && round === 0 ? "high" : "low"
2557
- });
2558
- const probeText = typeof probe?.response === "string" ? probe?.response : probe?.choices?.[0]?.message?.content ?? "";
2559
- probeTokensUsed += usageTotal(probe?.usage) ?? 0;
2560
- const nativeCall = Array.isArray(probe?.tool_calls) ? probe.tool_calls[0]?.function : null;
2561
- const call = nativeCall ? {
2562
- name: String(nativeCall.name ?? ""),
2563
- args: (() => {
2564
- try {
2565
- return typeof nativeCall.arguments === "string" ? JSON.parse(nativeCall.arguments) : nativeCall.arguments ?? {};
2566
- } catch {
2567
- return {};
2568
- }
2569
- })()
2570
- } : parseToolCall(probeText);
2571
- if (!call || !call.name) break;
2572
- const result = await runTool(env, call);
2573
- if (!result) break;
2574
- mark(`${result.name} \u2014 ${result.query}`.slice(0, 140));
2575
- messages.push({ role: "system", content: toolNote(result) });
2576
- pendingTools.push(result);
2577
- console.log("agent-tool:", result.name, JSON.stringify(result.query), "->", result.output.slice(0, 80));
2621
+ if (withImage) {
2622
+ const wantsMarkings = /\b(mark(ing|ings)?|nameplate|label|inscription|engrav|sticker|plate)\b|certificat/i.test(q2.query);
2623
+ const last = messages[messages.length - 1];
2624
+ const note = wantsMarkings ? "\n\n(The user attached a photo with this question. First transcribe every inscription you can actually read in the image \u2014 names, model references, accuracy classes, numeric values with their units, certificate or approval numbers \u2014 quoting them verbatim. Then answer the question from the numbered passages, citing the requirement each interpretation rests on. If an inscription is unreadable, say so; never invent a marking.)" : "\n\n(The user attached an image with this question; interpret it directly when answering.)";
2625
+ if (typeof last.content === "string") {
2626
+ last.content = last.content + note;
2627
+ } else {
2628
+ const textPart = last.content.find((p) => p.type === "text");
2629
+ if (textPart) textPart.text += note;
2578
2630
  }
2579
- } catch {
2631
+ messages.push({ role: "user", content: [{ type: "image_url", image_url: { url: withImage } }] });
2632
+ console.log("user image attached to generation");
2580
2633
  }
2581
- }
2582
- if (withImage) {
2583
- const wantsMarkings = /\b(mark(ing|ings)?|nameplate|label|inscription|engrav|sticker|plate)\b|certificat/i.test(q.query);
2584
- const last = messages[messages.length - 1];
2585
- const note = wantsMarkings ? "\n\n(The user attached a photo with this question. First transcribe every inscription you can actually read in the image \u2014 names, model references, accuracy classes, numeric values with their units, certificate or approval numbers \u2014 quoting them verbatim. Then answer the question from the numbered passages, citing the requirement each interpretation rests on. If an inscription is unreadable, say so; never invent a marking.)" : "\n\n(The user attached an image with this question; interpret it directly when answering.)";
2586
- if (typeof last.content === "string") {
2587
- last.content = last.content + note;
2588
- } else {
2589
- const textPart = last.content.find((p) => p.type === "text");
2590
- if (textPart) textPart.text += note;
2591
- }
2592
- messages.push({ role: "user", content: [{ type: "image_url", image_url: { url: withImage } }] });
2593
- console.log("user image attached to generation");
2594
- }
2595
- const queryHash = await sha256Hex(q.query);
2596
- const cites = boundModel ? [modelCitation(boundModel), ...citations(usedHits)] : citations(usedHits);
2597
- if (wantsStream) {
2598
- const stream = await generateStream(env, model, messages, routeEffort(routed.route));
2599
- if (stream) {
2600
- const encoder = new TextEncoder();
2601
- const sse = new ReadableStream({
2602
- async start(controller) {
2603
- const send = (obj) => controller.enqueue(encoder.encode(`data: ${JSON.stringify(obj)}
2604
-
2605
- `));
2606
- for (const step of agentSteps) {
2607
- try {
2608
- send({ type: "agent", step });
2609
- } catch {
2610
- }
2611
- }
2612
- for (const t of pendingTools) {
2613
- try {
2614
- send({ type: "tool", name: t.name, query: t.query });
2615
- } catch {
2616
- }
2634
+ const queryHash = await sha256Hex(q2.query);
2635
+ const cites = boundModel ? [modelCitation(boundModel), ...citations(usedHits)] : citations(usedHits);
2636
+ if (live2) {
2637
+ const stream = await generateStream(env, model, messages, routeEffort(routed.route));
2638
+ if (stream) {
2639
+ const send = (obj) => live2.send(obj);
2640
+ for (const step of agentSteps) {
2641
+ try {
2642
+ send({ type: "agent", step });
2643
+ } catch {
2617
2644
  }
2618
- send({ type: "read", read: readAs() });
2619
- const sourceQuality = answerQuality(cites.map((c) => c.quality));
2620
- send({ type: "citations", citations: cites, context_applied: ctxApplied, ...sourceQuality ? { source_quality: sourceQuality, confidence_note: qualityNote(sourceQuality), ...sourceQuality === "ocr" ? { experimental_sources: experimentalSourceLabels(cites) } : {} } : {}, ...liveRecords ? { records: liveRecords } : {}, quota });
2621
- const stats = {};
2622
- let full = "";
2645
+ }
2646
+ for (const t of pendingTools) {
2623
2647
  try {
2624
- let headSettled = false;
2625
- for await (const tok of sseTokens(stream, stats)) {
2626
- full += tok;
2627
- if (headSettled) {
2628
- send({ type: "token", v: tok });
2629
- continue;
2630
- }
2631
- if (full.length < 80) continue;
2632
- headSettled = true;
2633
- if (toolNarration(full) || intentPreamble(full)) {
2634
- console.log(toolNarration(full) ? "tool-narration(stream): regenerating once" : "intent-preamble(stream): regenerating once");
2635
- send({ type: "retry" });
2636
- const regen = await generateStream(env, model, [
2637
- ...messages,
2638
- { role: "system", content: toolNarration(full) ? TOOL_NARRATION_NOTICE : INTENT_PREAMBLE_NOTICE }
2639
- ], routeEffort(routed.route));
2640
- if (regen) {
2641
- full = "";
2642
- for await (const tok2 of sseTokens(regen, stats)) {
2643
- full += tok2;
2644
- send({ type: "token", v: tok2 });
2645
- }
2646
- } else {
2647
- send({ type: "token", v: full });
2648
- }
2649
- break;
2650
- }
2651
- send({ type: "token", v: full });
2652
- }
2653
- if (!headSettled) {
2654
- headSettled = true;
2655
- if (!toolNarration(full) && !intentPreamble(full)) send({ type: "token", v: full });
2656
- }
2648
+ send({ type: "tool", name: t.name, query: t.query });
2657
2649
  } catch {
2658
2650
  }
2659
- const tq2 = await chargeAnswerTokens(env, bucketId, stats, messageChars(messages), full.length, probeTokensUsed);
2660
- send({ type: "quota", quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2661
- if (withImage && register?.rows?.length && full.includes(refusalAnswer())) {
2662
- console.log("blind-answer(stream): regenerating once");
2663
- mark("Re-answering from the register results");
2664
- send({ type: "retry" });
2665
- const regen = await generateStream(env, model, [
2666
- ...messages.slice(0, -1),
2667
- { role: "user", content: "The photograph was attached and the register note lists actual matching certificate rows. Present those rows \u2014 number, holder, model, status, and every document link \u2014 as the answer. Do not claim that nothing was provided." },
2668
- messages[messages.length - 1]
2669
- ], routeEffort(routed.route));
2670
- if (regen) {
2671
- full = "";
2672
- try {
2673
- for await (const tok of sseTokens(regen)) {
2674
- full += tok;
2675
- send({ type: "token", v: tok });
2651
+ }
2652
+ send({ type: "read", read: readAs() });
2653
+ const sourceQuality = answerQuality(cites.map((c) => c.quality));
2654
+ send({ type: "citations", citations: cites, context_applied: ctxApplied, ...sourceQuality ? { source_quality: sourceQuality, confidence_note: qualityNote(sourceQuality), ...sourceQuality === "ocr" ? { experimental_sources: experimentalSourceLabels(cites) } : {} } : {}, ...liveRecords ? { records: liveRecords } : {}, quota });
2655
+ const stats = {};
2656
+ let full = "";
2657
+ try {
2658
+ let headSettled = false;
2659
+ for await (const tok of sseTokens(stream, stats)) {
2660
+ full += tok;
2661
+ if (headSettled) {
2662
+ send({ type: "token", v: tok });
2663
+ continue;
2664
+ }
2665
+ if (full.length < 80) continue;
2666
+ headSettled = true;
2667
+ if (toolNarration(full) || intentPreamble(full)) {
2668
+ console.log(toolNarration(full) ? "tool-narration(stream): regenerating once" : "intent-preamble(stream): regenerating once");
2669
+ send({ type: "retry" });
2670
+ const regen = await generateStream(env, model, [
2671
+ ...messages,
2672
+ { role: "system", content: toolNarration(full) ? TOOL_NARRATION_NOTICE : INTENT_PREAMBLE_NOTICE }
2673
+ ], routeEffort(routed.route));
2674
+ if (regen) {
2675
+ full = "";
2676
+ for await (const tok2 of sseTokens(regen, stats)) {
2677
+ full += tok2;
2678
+ send({ type: "token", v: tok2 });
2676
2679
  }
2677
- } catch {
2680
+ } else {
2681
+ send({ type: "token", v: full });
2678
2682
  }
2683
+ break;
2679
2684
  }
2685
+ send({ type: "token", v: full });
2680
2686
  }
2681
- const canonical0 = exciseMisplacedPin(canonicalRefusal(full.replace(/^TOOL [a-z_.]+.*$\n?/gm, "").replace(/^\n+/, ""), refusalScope(answerCites(full))), answerCites(full));
2682
- const c2 = canonical0.includes(refusalAnswer()) ? { text: canonical0, blocks: [], dropped: [] } : await contractV2(env.DB, canonical0, usedHits);
2683
- send({
2684
- type: "done",
2685
- model,
2686
- query_hash: queryHash,
2687
- follow_ups: refusalShaped(canonical0, refusalScope(answerCites(full))) ? [] : understanding?.follow_ups ?? [],
2688
- blocks: [...c2.blocks, ...verdictBlock ? [verdictBlock] : [], ...conditionBlock ? [conditionBlock] : [], ...aggregationBlock ? [aggregationBlock] : []],
2689
- context_applied: ctxApplied,
2690
- read: readAs(),
2691
- // the evidence view's ground truth: the exact passages this
2692
- // answer was built from, compact — cache hits carry none,
2693
- // because the cache stores the answer and never the passages
2694
- passages: usedHits.slice(0, 8).map((h) => ({ d: h.metadata.docidentifier ?? "", a: h.metadata.clause_anchor ?? "", t: (h.text ?? "").slice(0, h.metadata.block === "table" ? 1400 : 600), b: h.metadata.block === "table" || void 0, ...h.metadata.table_selection ? { s: h.metadata.table_selection } : {} }))
2695
- });
2696
- telemetry(env, ctx, tier, "ask", model, true, c2.text.length, queryHash, q.lang, void 0, telemetryMeta());
2697
- const canonical = c2.text;
2698
- let streamedEntailment = null;
2699
- let streamedNote = "";
2700
- if (THRESHOLDS.entailmentGate && sourceQuality && usedHits.length && !canonical.includes(refusalAnswer())) {
2701
- const raced = await Promise.race([
2702
- scoreFaithfulness(env.AI, roleModel(env, "grader"), canonical, usedHits.map((h) => h.text)),
2703
- new Promise((r) => setTimeout(() => r(null), THRESHOLDS.entailmentBudgetMs))
2704
- ]);
2705
- if (raced) {
2706
- const v = entailmentVerdict(raced.score, THRESHOLDS.entailmentSupportedFloor, THRESHOLDS.entailmentPartialFloor);
2707
- streamedEntailment = { support: v.support, score: Math.round(raced.score * 100) / 100, ...raced.ungrounded_claims?.length ? { ungrounded: raced.ungrounded_claims.slice(0, 3) } : {} };
2708
- streamedNote = v.note;
2709
- if (v.support !== "supported") console.log("entailment(stream):", v.support, raced.score);
2710
- send({ type: "confidence", source_quality: sourceQuality, confidence_note: streamedNote || qualityNote(sourceQuality), ...streamedEntailment ? { entailment: streamedEntailment } : {} });
2711
- }
2687
+ if (!headSettled) {
2688
+ headSettled = true;
2689
+ if (!toolNarration(full) && !intentPreamble(full)) send({ type: "token", v: full });
2712
2690
  }
2713
- const streamedAnchors = checkQuoteAnchors(canonical, usedHits.map((h) => h.text));
2714
- const streamedRetyped = tableRetyped(canonical, usedHits.some((h) => h.metadata.unit_id && h.metadata.block === "table"));
2715
- const streamed = { total: streamedAnchors.total, violations: streamedRetyped ? ["table-retyped"] : streamedAnchors.violations };
2716
- if (streamed.violations.length > 0) {
2717
- console.log("anchors:", streamed.violations.length, "of", streamed.total, "unverified \u2014 not caching");
2691
+ } catch {
2692
+ }
2693
+ const tq2 = await chargeAnswerTokens(env, bucketId, stats, messageChars(messages), full.length, probeTokensUsed);
2694
+ send({ type: "quota", quota: { ...quota, ...tq2, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } });
2695
+ if (withImage && register?.rows?.length && full.includes(refusalAnswer())) {
2696
+ console.log("blind-answer(stream): regenerating once");
2697
+ mark("Re-answering from the register results");
2698
+ send({ type: "retry" });
2699
+ const regen = await generateStream(env, model, [
2700
+ ...messages.slice(0, -1),
2701
+ { role: "user", content: "The photograph was attached and the register note lists actual matching certificate rows. Present those rows \u2014 number, holder, model, status, and every document link \u2014 as the answer. Do not claim that nothing was provided." },
2702
+ messages[messages.length - 1]
2703
+ ], routeEffort(routed.route));
2704
+ if (regen) {
2705
+ full = "";
2706
+ try {
2707
+ for await (const tok of sseTokens(regen)) {
2708
+ full += tok;
2709
+ send({ type: "token", v: tok });
2710
+ }
2711
+ } catch {
2712
+ }
2718
2713
  }
2719
- if (streamed.violations.length === 0 && canonical.length > 0 && !contextual && !declaredCtx && cacheableAnswer(canonical, refusalScope(answerCites(canonical))) && !ablate) {
2720
- ctx.waitUntil(
2721
- env.CACHE.put(exactCacheKey(env.INDEX_VERSION, gen, ns, await sha256Hex(cacheKeyMaterial(q.query, q.lang, salt))), JSON.stringify({ answer: canonical, citations: cites, model, query_hash: queryHash, ...sourceQuality ? { source_quality: sourceQuality, confidence_note: streamedNote || qualityNote(sourceQuality) } : {}, ...streamedEntailment ? { entailment: streamedEntailment } : {} }), { expirationTtl: LIMITS.cacheTtlSec })
2722
- );
2714
+ }
2715
+ const canonical0 = exciseMisplacedPin(canonicalRefusal(full.replace(/^TOOL [a-z_.]+.*$\n?/gm, "").replace(/^\n+/, ""), refusalScope(answerCites(full))), answerCites(full));
2716
+ const c2 = canonical0.includes(refusalAnswer()) ? { text: canonical0, blocks: [], dropped: [] } : await contractV2(env.DB, canonical0, usedHits);
2717
+ send({
2718
+ type: "done",
2719
+ model,
2720
+ query_hash: queryHash,
2721
+ follow_ups: refusalShaped(canonical0, refusalScope(answerCites(full))) ? [] : understanding?.follow_ups ?? [],
2722
+ blocks: [...c2.blocks, ...verdictBlock ? [verdictBlock] : [], ...conditionBlock ? [conditionBlock] : [], ...aggregationBlock ? [aggregationBlock] : []],
2723
+ context_applied: ctxApplied,
2724
+ read: readAs(),
2725
+ // the evidence view's ground truth: the exact passages this
2726
+ // answer was built from, compact — cache hits carry none,
2727
+ // because the cache stores the answer and never the passages
2728
+ passages: usedHits.slice(0, 8).map((h) => ({ d: h.metadata.docidentifier ?? "", a: h.metadata.clause_anchor ?? "", t: (h.text ?? "").slice(0, h.metadata.block === "table" ? 1400 : 600), b: h.metadata.block === "table" || void 0, ...h.metadata.table_selection ? { s: h.metadata.table_selection } : {} }))
2729
+ });
2730
+ telemetry(env, ctx, tier, "ask", model, true, c2.text.length, queryHash, q2.lang, void 0, telemetryMeta());
2731
+ const canonical = c2.text;
2732
+ let streamedEntailment = null;
2733
+ let streamedNote = "";
2734
+ if (THRESHOLDS.entailmentGate && sourceQuality && usedHits.length && !canonical.includes(refusalAnswer())) {
2735
+ const raced = await Promise.race([
2736
+ scoreFaithfulness(env.AI, roleModel(env, "grader"), canonical, usedHits.map((h) => h.text)),
2737
+ new Promise((r) => setTimeout(() => r(null), THRESHOLDS.entailmentBudgetMs))
2738
+ ]);
2739
+ if (raced) {
2740
+ const v = entailmentVerdict(raced.score, THRESHOLDS.entailmentSupportedFloor, THRESHOLDS.entailmentPartialFloor);
2741
+ streamedEntailment = { support: v.support, score: Math.round(raced.score * 100) / 100, ...raced.ungrounded_claims?.length ? { ungrounded: raced.ungrounded_claims.slice(0, 3) } : {} };
2742
+ streamedNote = v.note;
2743
+ if (v.support !== "supported") console.log("entailment(stream):", v.support, raced.score);
2744
+ send({ type: "confidence", source_quality: sourceQuality, confidence_note: streamedNote || qualityNote(sourceQuality), ...streamedEntailment ? { entailment: streamedEntailment } : {} });
2723
2745
  }
2724
- controller.close();
2725
2746
  }
2726
- });
2727
- return new Response(sse, {
2728
- headers: {
2729
- "content-type": "text/event-stream",
2730
- "cache-control": "no-cache",
2731
- "x-accel-buffering": "no",
2732
- ...corsHeaders(req)
2747
+ const streamedAnchors = checkQuoteAnchors(canonical, usedHits.map((h) => h.text));
2748
+ const streamedRetyped = tableRetyped(canonical, usedHits.some((h) => h.metadata.unit_id && h.metadata.block === "table"));
2749
+ const streamed = { total: streamedAnchors.total, violations: streamedRetyped ? ["table-retyped"] : streamedAnchors.violations };
2750
+ if (streamed.violations.length > 0) {
2751
+ console.log("anchors:", streamed.violations.length, "of", streamed.total, "unverified \u2014 not caching");
2752
+ }
2753
+ if (streamed.violations.length === 0 && canonical.length > 0 && !contextual && !declaredCtx && cacheableAnswer(canonical, refusalScope(answerCites(canonical))) && !ablate) {
2754
+ ctx.waitUntil(
2755
+ env.CACHE.put(exactCacheKey(env.INDEX_VERSION, gen, ns, await sha256Hex(cacheKeyMaterial(q2.query, q2.lang, salt))), JSON.stringify({ answer: canonical, citations: cites, model, query_hash: queryHash, ...sourceQuality ? { source_quality: sourceQuality, confidence_note: streamedNote || qualityNote(sourceQuality) } : {}, ...streamedEntailment ? { entailment: streamedEntailment } : {} }), { expirationTtl: LIMITS.cacheTtlSec })
2756
+ );
2733
2757
  }
2758
+ live2.close();
2759
+ } else {
2760
+ live2.send({ type: "token", v: "The generation model is unavailable; please retry." });
2761
+ live2.send({ type: "done", model: MODELS.member, query_hash: "", context_applied: NO_CONTEXT });
2762
+ live2.close();
2763
+ return null;
2764
+ }
2765
+ return null;
2766
+ }
2767
+ const tGen = Date.now();
2768
+ let answer = null;
2769
+ if (ablate?.speculative) {
2770
+ const spec = await speculativeDraftVerify(env, {
2771
+ query: q2.query,
2772
+ hits: usedHits,
2773
+ lang: q2.lang,
2774
+ draftModel: MODELS.member,
2775
+ verifierModel: roleModel(env, "verifier")
2776
+ }).catch((e) => {
2777
+ console.log("speculative: failed, falling through \u2014", String(e).slice(0, 120));
2778
+ return null;
2734
2779
  });
2780
+ if (spec) {
2781
+ answer = spec.answer;
2782
+ stageTiming.speculative = Date.now() - tGen;
2783
+ console.log("speculative:", spec.nDrafts, "drafts,", spec.chosen, "viable \u2014 verified");
2784
+ }
2735
2785
  }
2736
- }
2737
- const tGen = Date.now();
2738
- let answer = null;
2739
- if (ablate?.speculative) {
2740
- const spec = await speculativeDraftVerify(env, {
2741
- query: q.query,
2742
- hits: usedHits,
2743
- lang: q.lang,
2744
- draftModel: MODELS.member,
2745
- verifierModel: roleModel(env, "verifier")
2746
- }).catch((e) => {
2747
- console.log("speculative: failed, falling through \u2014", String(e).slice(0, 120));
2748
- return null;
2749
- });
2750
- if (spec) {
2751
- answer = spec.answer;
2752
- stageTiming.speculative = Date.now() - tGen;
2753
- console.log("speculative:", spec.nDrafts, "drafts,", spec.chosen, "viable \u2014 verified");
2754
- }
2755
- }
2756
- if (!answer) answer = await generateOnce(env, model, messages, routeEffort(routed.route));
2757
- if (answer === null) {
2758
- generateRetries += 1;
2759
- const isFigureAttachMessage = (m) => Array.isArray(m.content) && m.content.some((part) => part?.type === "text" && /^The original image of figure unit /.test(part.text ?? ""));
2760
- const flat = messages.filter((m) => !isFigureAttachMessage(m)).map(
2761
- (m) => typeof m.content === "string" ? m : { ...m, content: m.content.filter((p) => p?.type === "text").map((p) => (p?.text ?? "").replace(/\n?\(The user attached an image with this question; interpret it directly when answering\.\)/, "")).join("\n") }
2762
- );
2763
- answer = await generateOnce(env, MODELS.fallback, flat, routeEffort(routed.route));
2764
- }
2765
- const scrubToolLines = (t) => t.replace(/^TOOL [a-z_.]+.*$\n?/gm, "").replace(/^\n+/, "");
2766
- if (answer) answer = scrubToolLines(answer);
2767
- if (answer) answer = canonicalRefusal(answer, refusalScope(answerCites(answer)));
2768
- if (answer) answer = scrubToolLines(answer);
2769
- if (answer) answer = exciseMisplacedPin(answer, answerCites(answer));
2770
- stageTiming.generate = Date.now() - tGen;
2771
- let used = usedHits;
2772
- if (answer && !answer.includes(refusalAnswer())) {
2773
- const anchors = checkQuoteAnchors(answer, used.map((h) => h.text));
2774
- const hasTableUnit = used.some((h) => h.metadata.unit_id && h.metadata.block === "table");
2775
- const retyped = tableRetyped(answer, hasTableUnit);
2776
- const unreferenced = (() => {
2777
- if (!hasTableUnit || answer.includes("[[u:")) return false;
2778
- const norm2 = (s) => (s.match(/\d[\d ,.]{1,8}\d/g) ?? []).map((x) => x.replace(/[ ,.]/g, ""));
2779
- const nums = norm2(answer);
2780
- if (nums.length < 2) return false;
2781
- const tableNums = new Set(
2782
- norm2(used.filter((h) => h.metadata.unit_id && h.metadata.block === "table").map((h) => h.text).join(" "))
2783
- );
2784
- return nums.filter((n) => tableNums.has(n)).length >= 2;
2785
- })();
2786
- if (anchors.violations.length > 0 || retyped || unreferenced || toolNarration(answer) || intentPreamble(answer)) {
2787
- console.log("contract check:", anchors.violations.length, "anchor violations; tableRetyped:", retyped, "; tableDataUnreferenced:", unreferenced, "; toolNarration:", toolNarration(answer), "; intentPreamble:", intentPreamble(answer), "\u2014 regenerating");
2788
- const tableUnitId = unreferenced ? used.find((h) => h.metadata.unit_id && h.metadata.block === "table")?.metadata.unit_id : void 0;
2789
- const note = toolNarration(answer) ? TOOL_NARRATION_NOTICE : intentPreamble(answer) ? INTENT_PREAMBLE_NOTICE : retyped || unreferenced ? `Correction notice: your draft reproduced a table as markdown or presented a served table's data without its reference. Rewrite the answer: describe the table in prose, cite the clause, and write the reference token [[u:${tableUnitId ?? "<unit id>"}]] exactly where the table belongs. Do not render any table as markdown.` : ANCHOR_CORRECTION_NOTE;
2786
+ if (!answer) answer = await generateOnce(env, model, messages, routeEffort(routed.route));
2787
+ if (answer === null) {
2790
2788
  generateRetries += 1;
2791
- const corrected = await generateOnce(env, model, [...messages, { role: "system", content: note }], routeEffort(routed.route));
2792
- if (corrected) {
2793
- const correctedAnswer = canonicalRefusal(corrected, refusalScope(answerCites(corrected)));
2794
- const retryAnchors = checkQuoteAnchors(correctedAnswer, used.map((h) => h.text));
2795
- const retryRetyped = tableRetyped(correctedAnswer, hasTableUnit);
2796
- if (retryAnchors.violations.length < anchors.violations.length || !retryRetyped && retyped || unreferenced && correctedAnswer.includes("[[u:") || !toolNarration(correctedAnswer) || !intentPreamble(correctedAnswer)) {
2797
- answer = correctedAnswer;
2789
+ const isFigureAttachMessage = (m) => Array.isArray(m.content) && m.content.some((part) => part?.type === "text" && /^The original image of figure unit /.test(part.text ?? ""));
2790
+ const flat = messages.filter((m) => !isFigureAttachMessage(m)).map(
2791
+ (m) => typeof m.content === "string" ? m : { ...m, content: m.content.filter((p) => p?.type === "text").map((p) => (p?.text ?? "").replace(/\n?\(The user attached an image with this question; interpret it directly when answering\.\)/, "")).join("\n") }
2792
+ );
2793
+ answer = await generateOnce(env, MODELS.fallback, flat, routeEffort(routed.route));
2794
+ }
2795
+ if (answer === null) {
2796
+ telemetry(env, ctx, tier, "ask", model, false, 0, queryHash, q2.lang, void 0, telemetryMeta());
2797
+ return err(502, "generation_failed", "The generation model is unavailable; please retry.");
2798
+ }
2799
+ const scrubToolLines = (t) => t.replace(/^TOOL [a-z_.]+.*$\n?/gm, "").replace(/^\n+/, "");
2800
+ if (answer) answer = scrubToolLines(answer);
2801
+ if (answer) answer = canonicalRefusal(answer, refusalScope(answerCites(answer)));
2802
+ if (answer) answer = scrubToolLines(answer);
2803
+ if (answer) answer = exciseMisplacedPin(answer, answerCites(answer));
2804
+ stageTiming.generate = Date.now() - tGen;
2805
+ let used = usedHits;
2806
+ if (answer && !answer.includes(refusalAnswer())) {
2807
+ const anchors = checkQuoteAnchors(answer, used.map((h) => h.text));
2808
+ const hasTableUnit = used.some((h) => h.metadata.unit_id && h.metadata.block === "table");
2809
+ const retyped = tableRetyped(answer, hasTableUnit);
2810
+ const unreferenced = (() => {
2811
+ if (!hasTableUnit || answer.includes("[[u:")) return false;
2812
+ const norm2 = (s) => (s.match(/\d[\d ,.]{1,8}\d/g) ?? []).map((x) => x.replace(/[ ,.]/g, ""));
2813
+ const nums = norm2(answer);
2814
+ if (nums.length < 2) return false;
2815
+ const tableNums = new Set(
2816
+ norm2(used.filter((h) => h.metadata.unit_id && h.metadata.block === "table").map((h) => h.text).join(" "))
2817
+ );
2818
+ return nums.filter((n) => tableNums.has(n)).length >= 2;
2819
+ })();
2820
+ if (anchors.violations.length > 0 || retyped || unreferenced || toolNarration(answer) || intentPreamble(answer)) {
2821
+ console.log("contract check:", anchors.violations.length, "anchor violations; tableRetyped:", retyped, "; tableDataUnreferenced:", unreferenced, "; toolNarration:", toolNarration(answer), "; intentPreamble:", intentPreamble(answer), "\u2014 regenerating");
2822
+ const tableUnitId = unreferenced ? used.find((h) => h.metadata.unit_id && h.metadata.block === "table")?.metadata.unit_id : void 0;
2823
+ const note = toolNarration(answer) ? TOOL_NARRATION_NOTICE : intentPreamble(answer) ? INTENT_PREAMBLE_NOTICE : retyped || unreferenced ? `Correction notice: your draft reproduced a table as markdown or presented a served table's data without its reference. Rewrite the answer: describe the table in prose, cite the clause, and write the reference token [[u:${tableUnitId ?? "<unit id>"}]] exactly where the table belongs. Do not render any table as markdown.` : ANCHOR_CORRECTION_NOTE;
2824
+ generateRetries += 1;
2825
+ const corrected = await generateOnce(env, model, [...messages, { role: "system", content: note }], routeEffort(routed.route));
2826
+ if (corrected) {
2827
+ const correctedAnswer = canonicalRefusal(corrected, refusalScope(answerCites(corrected)));
2828
+ const retryAnchors = checkQuoteAnchors(correctedAnswer, used.map((h) => h.text));
2829
+ const retryRetyped = tableRetyped(correctedAnswer, hasTableUnit);
2830
+ if (retryAnchors.violations.length < anchors.violations.length || !retryRetyped && retyped || unreferenced && correctedAnswer.includes("[[u:") || !toolNarration(correctedAnswer) || !intentPreamble(correctedAnswer)) {
2831
+ answer = correctedAnswer;
2832
+ }
2798
2833
  }
2799
2834
  }
2800
2835
  }
2801
- }
2802
- if (answer && !answer.includes(refusalAnswer())) {
2803
- const reflection = await reflect(env.AI, MODELS.grader, q.query, answer, hits.map((h) => h.text));
2804
- console.log("reflection:", reflection ? reflection.grounded ? "grounded" : "ungrounded" : "null");
2805
- if (reflection && !reflection.grounded && reflection.missing_info) {
2806
- const retryRetrieve = await retrieve(env, q.query, {
2807
- prev,
2808
- understanding: { ...understanding, standalone_query: `${understanding?.standalone_query || q.query} ${reflection.missing_info}` },
2809
- sealScope: declaredScoped ? docScope : null,
2810
- datasetScope: narrowed ? corpora : null,
2811
- standardKeys,
2812
- ...routeSubset ? { stageSubset: routeSubset } : {}
2813
- });
2814
- if (retryRetrieve.hits.length > 0) {
2815
- const { messages: retryMessages, usedHits: retryUsed } = buildMessages(q.query, retryRetrieve.hits, q.lang, keptHistory, void 0, summary, budget);
2816
- const retryAnswer = await generateOnce(env, model, retryMessages, routeEffort(routed.route));
2817
- if (retryAnswer) {
2818
- answer = canonicalRefusal(retryAnswer, refusalScope(answerCites(retryAnswer)));
2819
- used = retryUsed;
2836
+ if (answer && !answer.includes(refusalAnswer())) {
2837
+ const reflection = await reflect(env.AI, MODELS.grader, q2.query, answer, hits.map((h) => h.text));
2838
+ console.log("reflection:", reflection ? reflection.grounded ? "grounded" : "ungrounded" : "null");
2839
+ if (reflection && !reflection.grounded && reflection.missing_info) {
2840
+ const retryRetrieve = await retrieve(env, q2.query, {
2841
+ prev,
2842
+ understanding: { ...understanding, standalone_query: `${understanding?.standalone_query || q2.query} ${reflection.missing_info}` },
2843
+ sealScope: declaredScoped ? docScope : null,
2844
+ datasetScope: narrowed ? corpora : null,
2845
+ standardKeys,
2846
+ ...routeSubset ? { stageSubset: routeSubset ?? void 0 } : {}
2847
+ });
2848
+ if (retryRetrieve.hits.length > 0) {
2849
+ const { messages: retryMessages, usedHits: retryUsed } = buildMessages(q2.query, retryRetrieve.hits, q2.lang, keptHistory, void 0, summary, budget);
2850
+ const retryAnswer = await generateOnce(env, model, retryMessages, routeEffort(routed.route));
2851
+ if (retryAnswer) {
2852
+ answer = canonicalRefusal(retryAnswer, refusalScope(answerCites(retryAnswer)));
2853
+ used = retryUsed;
2854
+ }
2820
2855
  }
2821
2856
  }
2822
2857
  }
2823
- }
2824
- if (answer === null) {
2825
- telemetry(env, ctx, tier, "ask", model, false, 0, queryHash, q.lang, void 0, telemetryMeta());
2826
- return err(502, "generation_failed", "The generation model is unavailable; please retry.");
2827
- }
2828
- const finalCites = boundModel ? [modelCitation(boundModel), ...citations(used)] : citations(used);
2829
- const c2ns = answer.includes(refusalAnswer()) ? { text: answer, blocks: [], dropped: [] } : await contractV2(env.DB, answer, used);
2830
- answer = c2ns.text;
2831
- const finalAnchors = answer.includes(refusalAnswer()) ? { total: 0, violations: [] } : checkQuoteAnchors(answer, used.map((h) => h.text));
2832
- if (finalAnchors.violations.length > 0) {
2833
- console.log("anchors:", finalAnchors.violations.length, "of", finalAnchors.total, "unverified \u2014 not caching");
2834
- }
2835
- let completionBlocks = [];
2836
- if (!answer.includes(refusalAnswer()) && !c2ns.blocks.some((b) => b.type === "table")) {
2837
- completionBlocks = await completeTables(env.DB, answer, used);
2838
- if (completionBlocks.length) console.log("contract completion:", completionBlocks.length, "table block(s) attached server-side");
2839
- }
2840
- completionBlocks.push(...await completeFigures(env.DB, answer, [...c2ns.blocks, ...completionBlocks], used));
2841
- if (withImage && register?.rows?.length && answer.includes(refusalAnswer())) {
2842
- console.log("blind-answer: the register note carried rows but the answer refused \u2014 regenerating once");
2843
- mark("Re-answering from the register results");
2844
- const regen = await generateOnce(env, model, [
2845
- ...messages.slice(0, -1),
2846
- { role: "user", content: "The photograph was attached and the register note lists actual matching certificate rows. Present those rows \u2014 number, holder, model, status, and every document link \u2014 as the answer. Do not claim that nothing was provided." },
2847
- messages[messages.length - 1]
2848
- ], effort);
2849
- if (regen && !regen.includes(refusalAnswer())) {
2850
- answer = canonicalRefusal(regen, refusalScope(answerCites(regen)));
2851
- console.log("blind-answer: regeneration grounded");
2852
- }
2853
- }
2854
- const jsonQuality = answerQuality(finalCites.map((c) => c.quality));
2855
- let entailment = null;
2856
- let entailmentNote = "";
2857
- if (THRESHOLDS.entailmentGate && jsonQuality && used.length && !answer.includes(refusalAnswer())) {
2858
- const raced = await Promise.race([
2859
- scoreFaithfulness(env.AI, roleModel(env, "grader"), answer, used.map((h) => h.text)),
2860
- new Promise((r) => setTimeout(() => r(null), THRESHOLDS.entailmentBudgetMs))
2861
- ]);
2862
- if (raced) {
2863
- const v = entailmentVerdict(raced.score, THRESHOLDS.entailmentSupportedFloor, THRESHOLDS.entailmentPartialFloor);
2864
- entailment = { support: v.support, score: Math.round(raced.score * 100) / 100, ...raced.ungrounded_claims?.length ? { ungrounded: raced.ungrounded_claims.slice(0, 3) } : {} };
2865
- entailmentNote = v.note;
2866
- if (v.support !== "supported") console.log("entailment:", v.support, raced.score, raced.ungrounded_claims?.slice(0, 2));
2867
- }
2868
- }
2869
- const out = { answer, citations: finalCites, ...jsonQuality ? { source_quality: jsonQuality, confidence_note: entailmentNote || qualityNote(jsonQuality), ...entailment ? { entailment } : {}, ...jsonQuality === "ocr" ? { experimental_sources: experimentalSourceLabels(finalCites) } : {} } : {}, model: MODELS.member, query_hash: queryHash, follow_ups: refusalShaped(answer, refusalScope(answerCites(answer))) ? [] : understanding?.follow_ups ?? [], blocks: [...c2ns.blocks, ...verdictBlock ? [verdictBlock] : [], ...conditionBlock ? [conditionBlock] : [], ...aggregationBlock ? [aggregationBlock] : [], ...registerBlock ? [registerBlock] : [], ...completionBlocks], context_applied: ctxApplied, ...liveRecords ? { records: liveRecords } : {}, ...ablate ? { ablate } : {}, route: { route: routed.route, features: routed.features } };
2870
- const cacheable = !contextual && !declaredCtx && !ablate && cacheableAnswer(answer, refusalScope(answerCites(answer))) && finalAnchors.violations.length === 0;
2871
- if (cacheable) {
2872
- const ck = exactCacheKey(env.INDEX_VERSION, gen, ns, await sha256Hex(cacheKeyMaterial(q.query, q.lang, salt)));
2873
- ctx.waitUntil(env.CACHE.put(ck, JSON.stringify(out), { expirationTtl: LIMITS.cacheTtlSec }));
2874
- }
2875
- telemetry(env, ctx, tier, "ask", model, true, answer.length, queryHash, q.lang, void 0, telemetryMeta());
2876
- const contextOut = used.map((h) => ({
2877
- doc_id: h.metadata.doc_id,
2878
- clause_anchor: h.metadata.clause_anchor,
2879
- text: h.text.slice(0, 1200),
2880
- ...h.metadata.table_selection ? { sel: h.metadata.table_selection } : {}
2881
- }));
2882
- const tq = await chargeAnswerTokens(env, bucketId, void 0, messageChars(messages), answer.length, probeTokensUsed);
2883
- return json({ ...out, context: contextOut, read: readAs(), quota: { ...quota, ...tq, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } }, 200, { ...corsHeaders(req), "server-timing": serverTiming() });
2884
- }
2885
- function sseResponse(events, cors) {
2886
- const encoder = new TextEncoder();
2887
- const body = events.map((e) => `data: ${JSON.stringify(e)}
2888
-
2889
- `).join("");
2890
- return new Response(encoder.encode(body), {
2891
- headers: {
2892
- "content-type": "text/event-stream",
2893
- "cache-control": "no-cache",
2894
- "x-accel-buffering": "no",
2895
- ...cors
2858
+ if (answer === null) {
2859
+ telemetry(env, ctx, tier, "ask", model, false, 0, queryHash, q2.lang, void 0, telemetryMeta());
2860
+ return err(502, "generation_failed", "The generation model is unavailable; please retry.");
2896
2861
  }
2897
- });
2862
+ const finalCites = boundModel ? [modelCitation(boundModel), ...citations(used)] : citations(used);
2863
+ const c2ns = answer.includes(refusalAnswer()) ? { text: answer, blocks: [], dropped: [] } : await contractV2(env.DB, answer, used);
2864
+ answer = c2ns.text;
2865
+ const finalAnchors = answer.includes(refusalAnswer()) ? { total: 0, violations: [] } : checkQuoteAnchors(answer, used.map((h) => h.text));
2866
+ if (finalAnchors.violations.length > 0) {
2867
+ console.log("anchors:", finalAnchors.violations.length, "of", finalAnchors.total, "unverified \u2014 not caching");
2868
+ }
2869
+ let completionBlocks = [];
2870
+ if (!answer.includes(refusalAnswer()) && !c2ns.blocks.some((b) => b.type === "table")) {
2871
+ completionBlocks = await completeTables(env.DB, answer, used);
2872
+ if (completionBlocks.length) console.log("contract completion:", completionBlocks.length, "table block(s) attached server-side");
2873
+ }
2874
+ completionBlocks.push(...await completeFigures(env.DB, answer, [...c2ns.blocks, ...completionBlocks], used));
2875
+ if (withImage && register?.rows?.length && answer.includes(refusalAnswer())) {
2876
+ console.log("blind-answer: the register note carried rows but the answer refused \u2014 regenerating once");
2877
+ mark("Re-answering from the register results");
2878
+ const regen = await generateOnce(env, model, [
2879
+ ...messages.slice(0, -1),
2880
+ { role: "user", content: "The photograph was attached and the register note lists actual matching certificate rows. Present those rows \u2014 number, holder, model, status, and every document link \u2014 as the answer. Do not claim that nothing was provided." },
2881
+ messages[messages.length - 1]
2882
+ ], effort);
2883
+ if (regen && !regen.includes(refusalAnswer())) {
2884
+ answer = canonicalRefusal(regen, refusalScope(answerCites(regen)));
2885
+ console.log("blind-answer: regeneration grounded");
2886
+ }
2887
+ }
2888
+ const jsonQuality = answerQuality(finalCites.map((c) => c.quality));
2889
+ let entailment = null;
2890
+ let entailmentNote = "";
2891
+ if (THRESHOLDS.entailmentGate && jsonQuality && used.length && !answer.includes(refusalAnswer())) {
2892
+ const raced = await Promise.race([
2893
+ scoreFaithfulness(env.AI, roleModel(env, "grader"), answer, used.map((h) => h.text)),
2894
+ new Promise((r) => setTimeout(() => r(null), THRESHOLDS.entailmentBudgetMs))
2895
+ ]);
2896
+ if (raced) {
2897
+ const v = entailmentVerdict(raced.score, THRESHOLDS.entailmentSupportedFloor, THRESHOLDS.entailmentPartialFloor);
2898
+ entailment = { support: v.support, score: Math.round(raced.score * 100) / 100, ...raced.ungrounded_claims?.length ? { ungrounded: raced.ungrounded_claims.slice(0, 3) } : {} };
2899
+ entailmentNote = v.note;
2900
+ if (v.support !== "supported") console.log("entailment:", v.support, raced.score, raced.ungrounded_claims?.slice(0, 2));
2901
+ }
2902
+ }
2903
+ const out = { answer, citations: finalCites, ...jsonQuality ? { source_quality: jsonQuality, confidence_note: entailmentNote || qualityNote(jsonQuality), ...entailment ? { entailment } : {}, ...jsonQuality === "ocr" ? { experimental_sources: experimentalSourceLabels(finalCites) } : {} } : {}, model: MODELS.member, query_hash: queryHash, follow_ups: refusalShaped(answer, refusalScope(answerCites(answer))) ? [] : understanding?.follow_ups ?? [], blocks: [...c2ns.blocks, ...verdictBlock ? [verdictBlock] : [], ...conditionBlock ? [conditionBlock] : [], ...aggregationBlock ? [aggregationBlock] : [], ...registerBlock ? [registerBlock] : [], ...completionBlocks], context_applied: ctxApplied, ...liveRecords ? { records: liveRecords } : {}, ...ablate ? { ablate } : {}, route: { route: routed.route, features: routed.features } };
2904
+ const cacheable = !contextual && !declaredCtx && !ablate && cacheableAnswer(answer, refusalScope(answerCites(answer))) && finalAnchors.violations.length === 0;
2905
+ if (cacheable) {
2906
+ const ck = exactCacheKey(env.INDEX_VERSION, gen, ns, await sha256Hex(cacheKeyMaterial(q2.query, q2.lang, salt)));
2907
+ ctx.waitUntil(env.CACHE.put(ck, JSON.stringify(out), { expirationTtl: LIMITS.cacheTtlSec }));
2908
+ }
2909
+ telemetry(env, ctx, tier, "ask", model, true, answer.length, queryHash, q2.lang, void 0, telemetryMeta());
2910
+ const contextOut = used.map((h) => ({
2911
+ doc_id: h.metadata.doc_id,
2912
+ clause_anchor: h.metadata.clause_anchor,
2913
+ text: h.text.slice(0, 1200),
2914
+ ...h.metadata.table_selection ? { sel: h.metadata.table_selection } : {}
2915
+ }));
2916
+ const tq = await chargeAnswerTokens(env, bucketId, void 0, messageChars(messages), answer.length, probeTokensUsed);
2917
+ return json({ ...out, context: contextOut, read: readAs(), quota: { ...quota, ...tq, credits: await creditBudget(env, bucketId), credit_estimate: creditPreview } }, 200, { ...corsHeaders(req), "server-timing": serverTiming() });
2918
+ };
2919
+ if (liveResponse) {
2920
+ void pipeline(live, q).catch(() => {
2921
+ try {
2922
+ live.close();
2923
+ } catch {
2924
+ }
2925
+ });
2926
+ return liveResponse;
2927
+ }
2928
+ return await pipeline(null, q);
2898
2929
  }
2899
2930
 
2900
2931
  export {