renor-engine 1.2.0 → 1.3.0

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.
Files changed (215) hide show
  1. package/README.md +21 -1
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/dist/BUILD.json +6 -0
  4. package/dist/cli.js +1656 -516
  5. package/package.json +16 -80
  6. package/dist/.tsbuildinfo +0 -1
  7. package/dist/api/apiAuth.js +0 -43
  8. package/dist/api/server.js +0 -174
  9. package/dist/buildIdentity.js +0 -149
  10. package/dist/callPhases.js +0 -82
  11. package/dist/cli-extractors.js +0 -46
  12. package/dist/config.js +0 -65
  13. package/dist/context/composer.js +0 -320
  14. package/dist/context/readHonesty.js +0 -478
  15. package/dist/context/rowStore.js +0 -238
  16. package/dist/conversation/formatter.js +0 -81
  17. package/dist/conversation/parser.js +0 -193
  18. package/dist/conversation/types.js +0 -2
  19. package/dist/conversation/writer.js +0 -42
  20. package/dist/db/client.js +0 -149
  21. package/dist/db/handoffTable.js +0 -31
  22. package/dist/db/indexSalvage.js +0 -101
  23. package/dist/db/migrations.js +0 -670
  24. package/dist/db/readCache.js +0 -109
  25. package/dist/db/schema.js +0 -5
  26. package/dist/db/sqlite.js +0 -396
  27. package/dist/db/statements.js +0 -89
  28. package/dist/digests/digestCache.js +0 -146
  29. package/dist/digests/digests.js +0 -597
  30. package/dist/digests/worker.js +0 -350
  31. package/dist/embeddings/caching.js +0 -337
  32. package/dist/embeddings/coalescing.js +0 -177
  33. package/dist/embeddings/interactive.js +0 -103
  34. package/dist/embeddings/local.js +0 -70
  35. package/dist/embeddings/openai.js +0 -106
  36. package/dist/embeddings/rebuild.js +0 -166
  37. package/dist/embeddings/simdScan.js +0 -260
  38. package/dist/embeddings/space.js +0 -21
  39. package/dist/embeddings/types.js +0 -14
  40. package/dist/embeddings/vectorIndex.js +0 -674
  41. package/dist/embeddings/vertex.js +0 -334
  42. package/dist/eval/artifacts.js +0 -42
  43. package/dist/eval/cacheReport.js +0 -146
  44. package/dist/eval/comparison.js +0 -182
  45. package/dist/eval/context700-candidate.js +0 -145
  46. package/dist/eval/evalCache.js +0 -568
  47. package/dist/eval/hashEmbedding.js +0 -35
  48. package/dist/eval/indexSnapshots.js +0 -549
  49. package/dist/eval/longmemeval.js +0 -56
  50. package/dist/eval/metrics.js +0 -181
  51. package/dist/eval/pageDump.js +0 -67
  52. package/dist/eval/renorbenchBaseline.js +0 -486
  53. package/dist/eval/renorbenchDocuments.js +0 -134
  54. package/dist/eval/renorbenchProduct.js +0 -182
  55. package/dist/eval/runLedger.js +0 -279
  56. package/dist/eval/runManifest.js +0 -485
  57. package/dist/eval/scoring.js +0 -197
  58. package/dist/eval/textTokens.js +0 -17
  59. package/dist/evidence/attach.js +0 -264
  60. package/dist/evidence/background.js +0 -147
  61. package/dist/evidence/evidenceCache.js +0 -182
  62. package/dist/evidence/gate.js +0 -37
  63. package/dist/evidence/keyPool.js +0 -85
  64. package/dist/evidence/ollamaChat.js +0 -84
  65. package/dist/evidence/optin.js +0 -165
  66. package/dist/evidence/options.js +0 -151
  67. package/dist/evidence/secondPass.js +0 -294
  68. package/dist/extraction/classifyCapture.js +0 -34
  69. package/dist/extraction/decision-model.js +0 -174
  70. package/dist/extraction/eventExtractor.js +0 -113
  71. package/dist/extraction/extractionClient.js +0 -122
  72. package/dist/extraction/factExtractor.js +0 -46
  73. package/dist/extraction/factsWritePath.js +0 -179
  74. package/dist/extraction/jevDecisionModel.js +0 -307
  75. package/dist/extraction/preferenceExtractor.js +0 -150
  76. package/dist/extraction/secretGuard.js +0 -180
  77. package/dist/extraction/secretScanner.js +0 -665
  78. package/dist/extraction/typedFacts.js +0 -224
  79. package/dist/extraction/writePathAdjudication.js +0 -110
  80. package/dist/importing/apply.js +0 -672
  81. package/dist/importing/bundle-write.js +0 -80
  82. package/dist/importing/cli.js +0 -408
  83. package/dist/importing/existing.js +0 -51
  84. package/dist/importing/facts.js +0 -121
  85. package/dist/importing/forgetImported.js +0 -79
  86. package/dist/importing/hosted.js +0 -467
  87. package/dist/importing/journalLog.js +0 -96
  88. package/dist/importing/markdown.js +0 -98
  89. package/dist/importing/plan.js +0 -201
  90. package/dist/importing/secretScreen.js +0 -113
  91. package/dist/importing/serialize.js +0 -188
  92. package/dist/importing/source.js +0 -463
  93. package/dist/importing/types.js +0 -6
  94. package/dist/indexer/chunker.js +0 -319
  95. package/dist/indexer/dailyEntries.js +0 -63
  96. package/dist/indexer/hasher.js +0 -10
  97. package/dist/indexer/laneRuns.js +0 -48
  98. package/dist/indexer/o200k.js +0 -481
  99. package/dist/indexer/outsideRoot.js +0 -77
  100. package/dist/indexer/parser.js +0 -170
  101. package/dist/indexer/pipeline.js +0 -988
  102. package/dist/indexer/staleness.js +0 -201
  103. package/dist/indexer/tokenizer.js +0 -77
  104. package/dist/indexer/walker.js +0 -170
  105. package/dist/learning/cli.js +0 -113
  106. package/dist/learning/schema.js +0 -144
  107. package/dist/learning/settings.js +0 -45
  108. package/dist/learning/store.js +0 -267
  109. package/dist/learning/tap.js +0 -401
  110. package/dist/mcp/hive.js +0 -88
  111. package/dist/mcp/requestErrors.js +0 -47
  112. package/dist/mcp/server.js +0 -426
  113. package/dist/memory/agentLease.js +0 -43
  114. package/dist/memory/confinedWrite.js +0 -315
  115. package/dist/memory/controls.js +0 -73
  116. package/dist/memory/dailyNote.js +0 -265
  117. package/dist/memory/dreamer/adjudicate.js +0 -215
  118. package/dist/memory/dreamer/apply.js +0 -120
  119. package/dist/memory/dreamer/budget.js +0 -64
  120. package/dist/memory/dreamer/candidates.js +0 -149
  121. package/dist/memory/dreamer/collection.js +0 -104
  122. package/dist/memory/dreamer/dreamer.js +0 -352
  123. package/dist/memory/dreamer/review.js +0 -111
  124. package/dist/memory/durableAppend.js +0 -57
  125. package/dist/memory/factConflicts.js +0 -205
  126. package/dist/memory/factTerms.js +0 -321
  127. package/dist/memory/handoff.js +0 -363
  128. package/dist/memory/hiveNameHeader.cjs +0 -10
  129. package/dist/memory/inventory.js +0 -55
  130. package/dist/memory/invisibleText.js +0 -20
  131. package/dist/memory/lifecycle.js +0 -2014
  132. package/dist/memory/limits.js +0 -13
  133. package/dist/memory/requests.js +0 -183
  134. package/dist/memory/sourceName.js +0 -276
  135. package/dist/memory/statusFacts.js +0 -61
  136. package/dist/memory/whatsNew.js +0 -158
  137. package/dist/memory/writer.js +0 -176
  138. package/dist/memory/writerConflicts.js +0 -85
  139. package/dist/memorymap/memoryMap.js +0 -1594
  140. package/dist/memorymap/replacedJournal.js +0 -58
  141. package/dist/memorymap/replacements.js +0 -410
  142. package/dist/mvp/activity.js +0 -86
  143. package/dist/mvp/agentMemory.js +0 -1803
  144. package/dist/mvp/answerCheck.js +0 -211
  145. package/dist/mvp/auth.js +0 -531
  146. package/dist/mvp/cli.js +0 -86
  147. package/dist/mvp/client-store.js +0 -462
  148. package/dist/mvp/compileCache.js +0 -242
  149. package/dist/mvp/compileSeed.js +0 -33
  150. package/dist/mvp/config.js +0 -292
  151. package/dist/mvp/connectionStatus.js +0 -80
  152. package/dist/mvp/contextDistinct.js +0 -53
  153. package/dist/mvp/contextProfile.js +0 -87
  154. package/dist/mvp/contextRelevance.js +0 -413
  155. package/dist/mvp/directorySurface.js +0 -68
  156. package/dist/mvp/engine.js +0 -1078
  157. package/dist/mvp/exportAll.js +0 -393
  158. package/dist/mvp/factLanePack.js +0 -77
  159. package/dist/mvp/factMarkers.js +0 -65
  160. package/dist/mvp/factsLane.js +0 -203
  161. package/dist/mvp/hiddenSpans.js +0 -585
  162. package/dist/mvp/history.js +0 -414
  163. package/dist/mvp/hitDates.js +0 -31
  164. package/dist/mvp/hitFloor.js +0 -36
  165. package/dist/mvp/identity.js +0 -18
  166. package/dist/mvp/idle.js +0 -113
  167. package/dist/mvp/log.js +0 -118
  168. package/dist/mvp/memoryDir.js +0 -98
  169. package/dist/mvp/posthogMcp.js +0 -210
  170. package/dist/mvp/readScope.js +0 -104
  171. package/dist/mvp/readUsefulness.js +0 -222
  172. package/dist/mvp/sanitize.js +0 -48
  173. package/dist/mvp/schemaRejections.js +0 -91
  174. package/dist/mvp/scope.js +0 -127
  175. package/dist/mvp/server.js +0 -2712
  176. package/dist/mvp/serverCard.js +0 -38
  177. package/dist/mvp/storedSize.js +0 -139
  178. package/dist/mvp/telemetry.js +0 -474
  179. package/dist/net/poolFetch.js +0 -174
  180. package/dist/recall/actionRecall.js +0 -282
  181. package/dist/recall/autoRecall.js +0 -447
  182. package/dist/recall/searchRecall.js +0 -189
  183. package/dist/retrieval/adviceLines.js +0 -195
  184. package/dist/retrieval/adviceQuestion.js +0 -44
  185. package/dist/retrieval/conversationShape.js +0 -44
  186. package/dist/retrieval/dailyEntryTime.js +0 -55
  187. package/dist/retrieval/entityLookup.js +0 -162
  188. package/dist/retrieval/factsFirst.js +0 -188
  189. package/dist/retrieval/keyword.js +0 -84
  190. package/dist/retrieval/ledgerEventTime.js +0 -51
  191. package/dist/retrieval/ledgerLiveness.js +0 -83
  192. package/dist/retrieval/ledgerPresentation.js +0 -161
  193. package/dist/retrieval/noteIdentity.js +0 -57
  194. package/dist/retrieval/orchestrator.js +0 -1224
  195. package/dist/retrieval/plainWording.js +0 -236
  196. package/dist/retrieval/queryHelpers.js +0 -142
  197. package/dist/retrieval/questionSpan.js +0 -475
  198. package/dist/retrieval/rankingTrace.js +0 -184
  199. package/dist/retrieval/readerEvents.js +0 -690
  200. package/dist/retrieval/readerLayout.js +0 -422
  201. package/dist/retrieval/relativeTimeHints.js +0 -113
  202. package/dist/retrieval/semantic.js +0 -67
  203. package/dist/router/routerCache.js +0 -106
  204. package/dist/router/sessionRouter.js +0 -726
  205. package/dist/sharing/cli.js +0 -181
  206. package/dist/sharing/examples.js +0 -136
  207. package/dist/sharing/fields.js +0 -225
  208. package/dist/sharing/loop.js +0 -80
  209. package/dist/sharing/project.js +0 -327
  210. package/dist/sharing/send.js +0 -466
  211. package/dist/sharing/state.js +0 -222
  212. package/dist/sharing/wording.js +0 -48
  213. package/dist/toolRefusal.js +0 -120
  214. package/dist/watcher/watcher.js +0 -77
  215. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
@@ -1,320 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ProvenanceError = exports.DEFAULT_BUDGET_TOKENS = exports.DEFAULT_SECTION_CAP = exports.SECTION_DROP_ORDER = exports.SECTION_ORDER = void 0;
4
- exports.estimateTokens = estimateTokens;
5
- exports.structuredContextEnabled = structuredContextEnabled;
6
- exports.queryScopedRowsEnabled = queryScopedRowsEnabled;
7
- exports.resolveBudgetTokens = resolveBudgetTokens;
8
- exports.filterRowsByQueryTerms = filterRowsByQueryTerms;
9
- exports.composeQueryContext = composeQueryContext;
10
- exports.composeQueryContextFromDb = composeQueryContextFromDb;
11
- const gate_1 = require("../evidence/gate");
12
- const rowStore_1 = require("./rowStore");
13
- /** Declared render order (ADR-0003 §5.1). */
14
- exports.SECTION_ORDER = ['excerpts', 'evidence', 'facts', 'chronicle', 'prefs'];
15
- /** Declared drop order on overflow: reverse of fill priority, excerpts last-standing. */
16
- exports.SECTION_DROP_ORDER = ['prefs', 'chronicle', 'facts', 'evidence'];
17
- exports.DEFAULT_SECTION_CAP = 40;
18
- exports.DEFAULT_BUDGET_TOKENS = 6000;
19
- class ProvenanceError extends Error {
20
- }
21
- exports.ProvenanceError = ProvenanceError;
22
- /** Deterministic token estimate (chars/4, ceil). Never model-dependent. */
23
- function estimateTokens(text) {
24
- if (text.length === 0)
25
- return 0;
26
- return Math.max(1, Math.ceil(text.length / 4));
27
- }
28
- /** RENOR_STRUCTURED_CONTEXT (default OFF). Opt in with 1/true/yes/on. */
29
- function structuredContextEnabled(env = process.env) {
30
- const raw = (env.RENOR_STRUCTURED_CONTEXT ?? '').trim().toLowerCase();
31
- return raw === '1' || raw === 'true' || raw === 'yes' || raw === 'on';
32
- }
33
- /**
34
- * RENOR_QUERY_SCOPED_ROWS (default OFF; C5 rollback flag — never flipped here).
35
- * Opt in with 1/true/yes/on. Enables read-path query-term-overlap filtering of
36
- * structured-row tables before composition (efficiency only; gold-blind).
37
- */
38
- function queryScopedRowsEnabled(env = process.env) {
39
- const raw = (env.RENOR_QUERY_SCOPED_ROWS ?? '').trim().toLowerCase();
40
- return raw === '1' || raw === 'true' || raw === 'yes' || raw === 'on';
41
- }
42
- function resolveBudgetTokens(env = process.env) {
43
- const raw = Number(env.RENOR_CONTEXT_BUDGET_TOKENS);
44
- if (Number.isFinite(raw) && raw > 0)
45
- return Math.floor(raw);
46
- return exports.DEFAULT_BUDGET_TOKENS;
47
- }
48
- function queryTerms(query) {
49
- return Array.from(new Set((0, rowStore_1.splitWords)(query).filter((t) => t.length >= 2)));
50
- }
51
- function rowSearchText(row) {
52
- const p = row.payload;
53
- switch (row.rowKind) {
54
- case 'fact': {
55
- const f = p;
56
- return [f.value, f.unit, f.verb, f.object, f.event].filter(Boolean).join(' ');
57
- }
58
- case 'event': {
59
- const e = p;
60
- return [e.when, e.event, ...(e.entities ?? [])].filter(Boolean).join(' ');
61
- }
62
- case 'preference': {
63
- const pr = p;
64
- return [pr.statement, pr.kind].filter(Boolean).join(' ');
65
- }
66
- }
67
- }
68
- /** Relevance = total query-term occurrences; ties broken by valid_from desc (NULL last), then source_path, then dedupe_key. */
69
- function rankRows(rows, terms) {
70
- const counted = rows.map((row) => {
71
- const text = (0, rowStore_1.matchForm)(rowSearchText(row));
72
- let relevance = 0;
73
- for (const term of terms) {
74
- let idx = text.indexOf(term);
75
- while (idx !== -1) {
76
- relevance += 1;
77
- idx = text.indexOf(term, idx + term.length);
78
- }
79
- }
80
- return { row, relevance };
81
- });
82
- counted.sort((a, b) => {
83
- if (a.relevance !== b.relevance)
84
- return b.relevance - a.relevance;
85
- const aDate = a.row.validFrom;
86
- const bDate = b.row.validFrom;
87
- if (aDate === null && bDate !== null)
88
- return 1; // NULL last
89
- if (bDate === null && aDate !== null)
90
- return -1;
91
- if (aDate !== bDate)
92
- return aDate < bDate ? 1 : -1; // desc
93
- if (a.row.sourcePath !== b.row.sourcePath)
94
- return a.row.sourcePath < b.row.sourcePath ? -1 : 1;
95
- return a.row.dedupeKey < b.row.dedupeKey ? -1 : a.row.dedupeKey > b.row.dedupeKey ? 1 : a.row.id - b.row.id;
96
- });
97
- return counted.map((c) => c.row);
98
- }
99
- function provenanceSuffix(row) {
100
- return ` (source: ${row.sourcePath}${row.validFrom ? `, ${row.validFrom}` : ', date unknown'})`;
101
- }
102
- /**
103
- * C5 query-scoped row filtering (HYP-20260921-query-scoped-rows).
104
- *
105
- * Read-path-only efficiency filter: keep rows whose text shares at least one
106
- * query term. Pure function of (query, rows) — zero model calls, zero access
107
- * to benchmark categories, gold labels, or any flag beyond the caller's
108
- * enabled decision (the flag is resolved OUTSIDE; nothing gold is passable).
109
- *
110
- * Gold-blind aggregate-intent detection reuses the product's EVIDENCE_INTENT
111
- * regex on QUERY TEXT ONLY (an AGGREGATE_INTENT-style cue; widened from the
112
- * eval runner's pattern). On aggregate intent the full facts table is retained
113
- * (H9 precedent: aggregates need the whole table to count/sum); chronicle and
114
- * prefs rows are ALWAYS term-filtered, even for aggregate queries.
115
- *
116
- * Determinism: overlap test is set membership on term sets; the caller (rank)
117
- * supplies the stable composer tie-breaks, so relative order is unchanged.
118
- */
119
- function filterRowsByQueryTerms(kind, query, rows) {
120
- if (kind === 'fact' && (0, gate_1.hasEvidenceIntent)(query))
121
- return rows;
122
- const terms = new Set(queryTerms(query));
123
- if (terms.size === 0)
124
- return rows; // empty query cannot scope: keep full table
125
- return rows.filter((row) => {
126
- const text = (0, rowStore_1.matchForm)(rowSearchText(row));
127
- for (const term of terms) {
128
- if (text.includes(term))
129
- return true;
130
- }
131
- return false;
132
- });
133
- }
134
- function renderRow(row) {
135
- const p = row.payload;
136
- switch (row.rowKind) {
137
- case 'fact': {
138
- const f = p;
139
- const value = [f.value, f.unit].filter(Boolean).join(' ');
140
- return `- ${[f.verb, f.object].filter(Boolean).join(' ')}: ${value}${provenanceSuffix(row)}`;
141
- }
142
- case 'event': {
143
- const e = p;
144
- return `- ${e.when}: ${e.event}${provenanceSuffix(row)}`;
145
- }
146
- case 'preference': {
147
- const pr = p;
148
- return `- ${pr.statement}${provenanceSuffix(row)}`;
149
- }
150
- }
151
- }
152
- const FALLBACKS = {
153
- facts: 'Cite only values present in this table; if the answer is not here, use the excerpts instead.',
154
- chronicle: 'Use only events listed in this table; otherwise rely on the excerpts.',
155
- prefs: 'When the question touches a preference or constraint, apply the matching entry to your answer as a standing rule. If nothing here is relevant, use the excerpts instead.',
156
- };
157
- function renderStructuredSection(name, rows) {
158
- if (rows.length === 0)
159
- return null;
160
- const lines = [`## ${name === 'prefs' ? 'Preferences' : name[0].toUpperCase() + name.slice(1)}`, '', FALLBACKS[name], ''];
161
- for (const row of rows)
162
- lines.push(renderRow(row));
163
- const text = lines.join('\n');
164
- return { name, text, tokens: estimateTokens(text), rowCount: rows.length };
165
- }
166
- function renderEvidence(evidence) {
167
- if (!evidence.items || evidence.items.length === 0)
168
- return null;
169
- const lines = ['## Evidence', ''];
170
- for (const item of evidence.items) {
171
- const parts = [item.item];
172
- if (item.action)
173
- parts.push(`action: ${item.action}`);
174
- if (item.amount)
175
- parts.push(`amount: ${item.amount}`);
176
- if (item.date)
177
- parts.push(`date: ${item.date}`);
178
- lines.push(`- ${parts.join(' | ')}`);
179
- }
180
- const text = lines.join('\n');
181
- return { name: 'evidence', text, tokens: estimateTokens(text), rowCount: evidence.items.length };
182
- }
183
- function renderExcerpts(hits) {
184
- if (hits.length === 0)
185
- return null;
186
- const lines = ['## Excerpts', ''];
187
- for (const hit of hits) {
188
- lines.push(`[${hit.score.toFixed(3)}] ${hit.notePath}${hit.session_date ? ` (${hit.session_date})` : ''}`);
189
- lines.push(hit.text);
190
- lines.push('');
191
- }
192
- // Trim the trailing blank so join behavior is stable.
193
- while (lines.length > 0 && lines[lines.length - 1] === '')
194
- lines.pop();
195
- const text = lines.join('\n');
196
- return { name: 'excerpts', text, tokens: estimateTokens(text), rowCount: hits.length };
197
- }
198
- /** Excerpts overflow: deterministic prefix trim (never reorders, never drops the section head). */
199
- function trimExcerpts(hits, budgetTokens) {
200
- const kept = [];
201
- let used = estimateTokens('## Excerpts');
202
- for (const hit of hits) {
203
- const block = `[${hit.score.toFixed(3)}] ${hit.notePath}${hit.session_date ? ` (${hit.session_date})` : ''}\n${hit.text}`;
204
- const cost = estimateTokens(block) + 1;
205
- if (used + cost > budgetTokens)
206
- break;
207
- kept.push(hit);
208
- used += cost;
209
- }
210
- return { hits: kept, tokens: used };
211
- }
212
- /**
213
- * Compose the bounded, deterministically ordered answer context. Structured
214
- * sections fill first; on total-budget overflow whole sections drop in
215
- * declared reverse order (prefs → chronicle → facts → evidence) — never
216
- * silent row-level truncation inside a table. Excerpts take the remaining
217
- * budget in product retrieval order.
218
- */
219
- function composeQueryContext(input, options = {}) {
220
- const budget = options.budgetTokens ?? resolveBudgetTokens(options.env);
221
- const cap = options.sectionCap ?? exports.DEFAULT_SECTION_CAP;
222
- const { query, hits, evidence } = input;
223
- // Provenance guard: any structured row without source_path + source_chunk_id
224
- // is rejected from composition — fail loudly, never silently strip.
225
- for (const row of input.rows ?? []) {
226
- if (!row.sourcePath || !row.sourcePath.trim()) {
227
- throw new ProvenanceError(`structured row ${row.id} (${row.rowKind}) rejected: missing source_path`);
228
- }
229
- if (!Number.isInteger(row.sourceChunkId) || row.sourceChunkId <= 0) {
230
- throw new ProvenanceError(`structured row ${row.id} (${row.rowKind}) rejected: missing source_chunk_id`);
231
- }
232
- }
233
- // Product-observable join: row ↔ query iff its source note is a retrieval hit.
234
- const hitPaths = new Set(hits.map((h) => h.notePath));
235
- const terms = queryTerms(query);
236
- const scoped = queryScopedRowsEnabled(options.env);
237
- const byKind = (kind) => {
238
- const kindRows = (input.rows ?? []).filter((r) => r.rowKind === kind && hitPaths.has(r.sourcePath));
239
- // C5: term-overlap scope BEFORE ranking/cap; budget is still enforced
240
- // downstream (overflow drop + excerpts trim), so no cap reduction here.
241
- const scopedRows = scoped
242
- ? filterRowsByQueryTerms(kind, query, kindRows)
243
- : kindRows;
244
- return rankRows(scopedRows, terms).slice(0, cap);
245
- };
246
- const factsSection = renderStructuredSection('facts', byKind('fact'));
247
- const chronicleSection = renderStructuredSection('chronicle', byKind('event'));
248
- const prefsSection = renderStructuredSection('prefs', byKind('preference'));
249
- const evidenceSection = evidence ? renderEvidence(evidence) : null;
250
- const excerptsSection = renderExcerpts(hits);
251
- const structured = [factsSection, chronicleSection, prefsSection, evidenceSection];
252
- // Fill structured + excerpts, then drop whole sections in declared reverse
253
- // order until the total fits (excerpts are prefix-trimmed, not dropped).
254
- const included = structured.filter((s) => s !== null);
255
- let excerpts = excerptsSection;
256
- let dropped = [];
257
- const total = () => included.reduce((sum, s) => sum + s.tokens, 0) + (excerpts?.tokens ?? 0);
258
- if (total() > budget) {
259
- for (const name of exports.SECTION_DROP_ORDER) {
260
- if (total() <= budget)
261
- break;
262
- const idx = included.findIndex((s) => s.name === name);
263
- if (idx !== -1) {
264
- included.splice(idx, 1);
265
- dropped.push(name);
266
- }
267
- }
268
- if (excerpts && total() > budget) {
269
- const remaining = budget - included.reduce((sum, s) => sum + s.tokens, 0);
270
- const trimmed = trimExcerpts(hits, Math.max(0, remaining));
271
- excerpts = trimmed.hits.length > 0
272
- ? { name: 'excerpts', text: renderExcerpts(trimmed.hits).text, tokens: trimmed.tokens, rowCount: trimmed.hits.length }
273
- : null;
274
- if (excerpts === null)
275
- dropped.push('excerpts');
276
- }
277
- }
278
- // Render in declared section order.
279
- const ordered = [...included].sort((a, b) => exports.SECTION_ORDER.indexOf(a.name) - exports.SECTION_ORDER.indexOf(b.name));
280
- const parts = [];
281
- if (excerpts)
282
- parts.push(excerpts.text);
283
- for (const s of ordered)
284
- parts.push(s.text);
285
- const sections = [];
286
- for (const name of exports.SECTION_ORDER) {
287
- if (name === 'excerpts') {
288
- if (excerpts) {
289
- sections.push({ name, included: true, rowCount: excerpts.rowCount, tokens: excerpts.tokens, text: excerpts.text });
290
- }
291
- else {
292
- sections.push({ name, included: false, rowCount: 0, tokens: 0, text: '' });
293
- }
294
- continue;
295
- }
296
- const found = ordered.find((s) => s.name === name);
297
- if (found) {
298
- sections.push({ name, included: true, rowCount: found.rowCount, tokens: found.tokens, text: found.text });
299
- }
300
- else {
301
- sections.push({ name, included: false, rowCount: 0, tokens: 0, text: '' });
302
- }
303
- }
304
- const text = parts.join('\n\n');
305
- const includedSections = sections.filter((s) => s.included).map((s) => s.name);
306
- return {
307
- query,
308
- budgetTokens: budget,
309
- usedTokens: parts.reduce((sum, p) => sum + estimateTokens(p), 0),
310
- sections,
311
- includedSections,
312
- droppedSections: dropped,
313
- text,
314
- };
315
- }
316
- /** Convenience wiring helper: read current rows for the query's hits and compose. */
317
- function composeQueryContextFromDb(db, input, options = {}) {
318
- const rows = (0, rowStore_1.readCurrentRowsForNotes)(db, input.hits.map((h) => h.notePath));
319
- return composeQueryContext({ ...input, rows }, options);
320
- }