renor-engine 1.1.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 (193) hide show
  1. package/README.md +27 -2
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/dist/BUILD.json +3 -3
  4. package/dist/cli.js +1656 -490
  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 -548
  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/preferenceExtractor.js +0 -150
  75. package/dist/extraction/secretGuard.js +0 -176
  76. package/dist/extraction/secretScanner.js +0 -665
  77. package/dist/extraction/typedFacts.js +0 -224
  78. package/dist/extraction/writePathAdjudication.js +0 -110
  79. package/dist/importing/apply.js +0 -672
  80. package/dist/importing/bundle-write.js +0 -80
  81. package/dist/importing/cli.js +0 -408
  82. package/dist/importing/existing.js +0 -51
  83. package/dist/importing/facts.js +0 -121
  84. package/dist/importing/forgetImported.js +0 -79
  85. package/dist/importing/hosted.js +0 -467
  86. package/dist/importing/journalLog.js +0 -96
  87. package/dist/importing/markdown.js +0 -98
  88. package/dist/importing/plan.js +0 -201
  89. package/dist/importing/secretScreen.js +0 -113
  90. package/dist/importing/serialize.js +0 -188
  91. package/dist/importing/source.js +0 -463
  92. package/dist/importing/types.js +0 -6
  93. package/dist/indexer/chunker.js +0 -319
  94. package/dist/indexer/dailyEntries.js +0 -63
  95. package/dist/indexer/hasher.js +0 -10
  96. package/dist/indexer/laneRuns.js +0 -48
  97. package/dist/indexer/o200k.js +0 -481
  98. package/dist/indexer/outsideRoot.js +0 -77
  99. package/dist/indexer/parser.js +0 -170
  100. package/dist/indexer/pipeline.js +0 -988
  101. package/dist/indexer/staleness.js +0 -201
  102. package/dist/indexer/tokenizer.js +0 -77
  103. package/dist/indexer/walker.js +0 -170
  104. package/dist/mcp/hive.js +0 -88
  105. package/dist/mcp/requestErrors.js +0 -47
  106. package/dist/mcp/server.js +0 -426
  107. package/dist/memory/agentLease.js +0 -43
  108. package/dist/memory/confinedWrite.js +0 -315
  109. package/dist/memory/controls.js +0 -73
  110. package/dist/memory/dailyNote.js +0 -265
  111. package/dist/memory/durableAppend.js +0 -57
  112. package/dist/memory/factConflicts.js +0 -203
  113. package/dist/memory/factTerms.js +0 -321
  114. package/dist/memory/handoff.js +0 -363
  115. package/dist/memory/hiveNameHeader.cjs +0 -10
  116. package/dist/memory/inventory.js +0 -55
  117. package/dist/memory/invisibleText.js +0 -20
  118. package/dist/memory/lifecycle.js +0 -1991
  119. package/dist/memory/limits.js +0 -13
  120. package/dist/memory/requests.js +0 -183
  121. package/dist/memory/sourceName.js +0 -276
  122. package/dist/memory/statusFacts.js +0 -61
  123. package/dist/memory/whatsNew.js +0 -158
  124. package/dist/memory/writer.js +0 -175
  125. package/dist/memory/writerConflicts.js +0 -85
  126. package/dist/memorymap/memoryMap.js +0 -1594
  127. package/dist/memorymap/replacedJournal.js +0 -58
  128. package/dist/memorymap/replacements.js +0 -410
  129. package/dist/mvp/activity.js +0 -86
  130. package/dist/mvp/agentMemory.js +0 -1800
  131. package/dist/mvp/answerCheck.js +0 -211
  132. package/dist/mvp/auth.js +0 -531
  133. package/dist/mvp/cli.js +0 -75
  134. package/dist/mvp/client-store.js +0 -462
  135. package/dist/mvp/compileCache.js +0 -242
  136. package/dist/mvp/compileSeed.js +0 -33
  137. package/dist/mvp/config.js +0 -288
  138. package/dist/mvp/connectionStatus.js +0 -80
  139. package/dist/mvp/contextDistinct.js +0 -53
  140. package/dist/mvp/contextProfile.js +0 -87
  141. package/dist/mvp/contextRelevance.js +0 -413
  142. package/dist/mvp/directorySurface.js +0 -68
  143. package/dist/mvp/engine.js +0 -1078
  144. package/dist/mvp/exportAll.js +0 -374
  145. package/dist/mvp/factLanePack.js +0 -77
  146. package/dist/mvp/factMarkers.js +0 -65
  147. package/dist/mvp/factsLane.js +0 -203
  148. package/dist/mvp/hiddenSpans.js +0 -585
  149. package/dist/mvp/history.js +0 -414
  150. package/dist/mvp/hitDates.js +0 -31
  151. package/dist/mvp/hitFloor.js +0 -36
  152. package/dist/mvp/identity.js +0 -18
  153. package/dist/mvp/idle.js +0 -113
  154. package/dist/mvp/log.js +0 -118
  155. package/dist/mvp/memoryDir.js +0 -98
  156. package/dist/mvp/posthogMcp.js +0 -210
  157. package/dist/mvp/readScope.js +0 -104
  158. package/dist/mvp/readUsefulness.js +0 -222
  159. package/dist/mvp/sanitize.js +0 -48
  160. package/dist/mvp/schemaRejections.js +0 -91
  161. package/dist/mvp/scope.js +0 -127
  162. package/dist/mvp/server.js +0 -2604
  163. package/dist/mvp/serverCard.js +0 -38
  164. package/dist/mvp/storedSize.js +0 -139
  165. package/dist/mvp/telemetry.js +0 -474
  166. package/dist/net/poolFetch.js +0 -174
  167. package/dist/recall/actionRecall.js +0 -282
  168. package/dist/recall/autoRecall.js +0 -447
  169. package/dist/recall/searchRecall.js +0 -189
  170. package/dist/retrieval/adviceLines.js +0 -195
  171. package/dist/retrieval/adviceQuestion.js +0 -44
  172. package/dist/retrieval/conversationShape.js +0 -44
  173. package/dist/retrieval/dailyEntryTime.js +0 -55
  174. package/dist/retrieval/entityLookup.js +0 -162
  175. package/dist/retrieval/factsFirst.js +0 -188
  176. package/dist/retrieval/keyword.js +0 -84
  177. package/dist/retrieval/ledgerEventTime.js +0 -51
  178. package/dist/retrieval/ledgerLiveness.js +0 -83
  179. package/dist/retrieval/ledgerPresentation.js +0 -161
  180. package/dist/retrieval/noteIdentity.js +0 -57
  181. package/dist/retrieval/orchestrator.js +0 -1171
  182. package/dist/retrieval/plainWording.js +0 -236
  183. package/dist/retrieval/queryHelpers.js +0 -142
  184. package/dist/retrieval/questionSpan.js +0 -475
  185. package/dist/retrieval/readerEvents.js +0 -690
  186. package/dist/retrieval/readerLayout.js +0 -422
  187. package/dist/retrieval/relativeTimeHints.js +0 -113
  188. package/dist/retrieval/semantic.js +0 -67
  189. package/dist/router/routerCache.js +0 -106
  190. package/dist/router/sessionRouter.js +0 -726
  191. package/dist/toolRefusal.js +0 -120
  192. package/dist/watcher/watcher.js +0 -77
  193. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
@@ -1,1800 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.STATEMENT_RELEVANCE_FLOOR = exports.NEWEST_ANCHOR_RELEVANCE_FLOOR = exports.CONTEXT_SPLIT_FLAG = exports.NEWEST_SESSION_WEAK_STATEMENTS = exports.NEWEST_SESSION_STATEMENT_WEIGHT = exports.NEWEST_SESSION_STATEMENT_FLOOR = exports.NEWEST_SESSION_PIECES = exports.NEWEST_SESSION_SHARE = exports.FACTS_BUDGET_SHARE = exports.CONTEXT_LEAN_ITEMS_FLAG = exports.PERMISSION_RULE_LIMIT = exports.GOVERNANCE_PREDICATES = exports.TRUNCATION_MARKER = exports.MAX_FACT_OBJECT_LANE_SHARE = exports.FTS_STOP_WORDS = void 0;
7
- exports.factQueryTerms = factQueryTerms;
8
- exports.joinedWords = joinedWords;
9
- exports.wordStems = wordStems;
10
- exports.scoreFactRelevance = scoreFactRelevance;
11
- exports.rankCandidateFacts = rankCandidateFacts;
12
- exports.capFactObject = capFactObject;
13
- exports.keywordOrAll = keywordOrAll;
14
- exports.keywordOrMatch = keywordOrMatch;
15
- exports.searchTextFallbackEnabled = searchTextFallbackEnabled;
16
- exports.keywordVariants = keywordVariants;
17
- exports.isGovernancePredicate = isGovernancePredicate;
18
- exports.permissionQuestion = permissionQuestion;
19
- exports.permissionMeaning = permissionMeaning;
20
- exports.currentLifecycleFacts = currentLifecycleFacts;
21
- exports.nameOnFile = nameOnFile;
22
- exports.factLaneRoom = factLaneRoom;
23
- exports.searchRelevance = searchRelevance;
24
- exports.scoreForReply = scoreForReply;
25
- exports.headingForReply = headingForReply;
26
- exports.contextLeanItemsEnabled = contextLeanItemsEnabled;
27
- exports.leanMemory = leanMemory;
28
- exports.dedupeMemories = dedupeMemories;
29
- exports.budgetSplit = budgetSplit;
30
- exports.replyTokens = replyTokens;
31
- exports.matchBudgetOrder = matchBudgetOrder;
32
- exports.newestMatchAbove = newestMatchAbove;
33
- exports.statementRelevance = statementRelevance;
34
- exports.sessionStatementsFor = sessionStatementsFor;
35
- exports.sessionMatesAbove = sessionMatesAbove;
36
- exports.applyBudget = applyBudget;
37
- exports.buildAgentContext = buildAgentContext;
38
- const path_1 = __importDefault(require("path"));
39
- const tokenizer_1 = require("../indexer/tokenizer");
40
- const sanitize_1 = require("./sanitize");
41
- const hiddenSpans_1 = require("./hiddenSpans");
42
- const engine_1 = require("./engine");
43
- const digests_1 = require("../digests/digests");
44
- const scope_1 = require("./scope");
45
- const contextDistinct_1 = require("./contextDistinct");
46
- const factMarkers_1 = require("./factMarkers");
47
- const sourceName_1 = require("../memory/sourceName");
48
- const orchestrator_1 = require("../retrieval/orchestrator");
49
- const attach_1 = require("../evidence/attach");
50
- const readHonesty_1 = require("../context/readHonesty");
51
- const lifecycle_1 = require("../memory/lifecycle");
52
- const writerConflicts_1 = require("../memory/writerConflicts");
53
- const ledgerLiveness_1 = require("../retrieval/ledgerLiveness");
54
- const ledgerPresentation_1 = require("../retrieval/ledgerPresentation");
55
- const factTerms_1 = require("../memory/factTerms");
56
- const factConflicts_1 = require("../memory/factConflicts");
57
- const readCache_1 = require("../db/readCache");
58
- const statements_1 = require("../db/statements");
59
- const contextRelevance_1 = require("./contextRelevance");
60
- const replacements_1 = require("../memorymap/replacements");
61
- const callPhases_1 = require("../callPhases");
62
- const statusFacts_1 = require("../memory/statusFacts");
63
- exports.FTS_STOP_WORDS = new Set([
64
- 'the', 'a', 'an', 'is', 'are', 'was', 'were', 'what', 'which', 'who', 'where', 'when',
65
- 'why', 'how', 'do', 'does', 'did', 'to', 'for', 'of', 'in', 'on', 'at', 'with', 'and',
66
- 'or', 'my', 'your', 'our', 'their', 'his', 'her', 'it', 'this', 'that', 'about',
67
- // Two-letter words that never name a topic. Not every two-letter word: CI, DB, AI, Go or IP often are the topic.
68
- 'am', 'as', 'be', 'by', 'he', 'if', 'me', 'so', 'up', 'we',
69
- ]);
70
- /**
71
- * The facts lane's stop words: FTS_STOP_WORDS and the other words that never say what a fact is about. Pronouns,
72
- * auxiliaries and modals, negation and what a contraction leaves of it ("don't" is `don`), quantifiers, and the
73
- * adverbs, prepositions and conjunctions of any question. A fact's object is often a sentence that has them, so a
74
- * question found it by them: "Hi, how are you?" returned a finding that says "you can not run all the tests now" (live
75
- * dogfood, 2026-10-03, D7). Words that can be the topic are not here: "us" (the country), "may" (the month), "won" (a
76
- * deal), "down", "off", "out" and "over" (a state), and two-letter topics like CI or DB.
77
- */
78
- const FACT_STOP_WORDS = new Set([
79
- ...exports.FTS_STOP_WORDS,
80
- 'you', 'him', 'she', 'they', 'them', 'its', 'mine', 'yours', 'ours', 'theirs', 'hers', 'these', 'those', 'whom', 'whose',
81
- 'myself', 'yourself', 'yourselves', 'himself', 'herself', 'itself', 'ourselves', 'themselves',
82
- 'can', 'could', 'will', 'would', 'shall', 'should', 'might', 'must', 'have', 'has', 'had', 'having', 'been', 'being', 'doing',
83
- 'not', 'no', 'nor', 'don', 'doesn', 'didn', 'isn', 'aren', 'wasn', 'weren', 'hasn', 'haven', 'hadn', 'couldn', 'wouldn',
84
- 'shouldn', 'mustn', 'needn', 'mightn', 'shan', 'ain', 'll', 're', 've',
85
- 'all', 'any', 'both', 'each', 'every', 'few', 'many', 'much', 'more', 'most', 'other', 'some', 'such', 'same', 'own',
86
- 'now', 'then', 'here', 'there', 'just', 'also', 'very', 'too', 'only', 'again', 'once', 'further',
87
- 'from', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'against', 'between',
88
- 'but', 'because', 'until', 'while', 'than',
89
- ]);
90
- /**
91
- * The words a fact is looked up by for `query`: its atoms (memory/factTerms.ts) without the facts lane's stop words, so
92
- * "in", "you" or "not" never make a fact relevant. The stop words count only when the query has no other word.
93
- */
94
- function factQueryTerms(query) {
95
- const all = (0, factTerms_1.atoms)(query);
96
- const content = all.filter(word => !FACT_STOP_WORDS.has(word));
97
- return content.length > 0 ? content : all;
98
- }
99
- /**
100
- * Two neighbouring words of `query` written as one, the way keys often are: "time zone" is `timezone`, "home lab" `homelab`,
101
- * "Lane J" `lanej`. Single letters and digits following a word are joined as well so relation keys like "lanej" or "v2" match.
102
- */
103
- function joinedWords(query) {
104
- const sequence = (0, factTerms_1.atomSequence)(query);
105
- const joined = [];
106
- for (let i = 1; i < sequence.length; i++) {
107
- // Never a stop word: "to do" is not the status `todo`.
108
- if (!FACT_STOP_WORDS.has(sequence[i - 1]) && !FACT_STOP_WORDS.has(sequence[i]))
109
- joined.push(sequence[i - 1] + sequence[i]);
110
- }
111
- const rawParts = query.toLowerCase().replace(/[^\p{L}\p{N}\p{M}_-]+/gu, ' ').split(/\s+/).map(w => w.trim()).filter(Boolean);
112
- for (let i = 1; i < rawParts.length; i++) {
113
- const prev = rawParts[i - 1];
114
- const curr = rawParts[i];
115
- // A trailing `s` is a possessive, not an initial: "John's plan" is not the key `johns`.
116
- if (prev.length >= 2 && curr.length === 1 && curr !== 's' && !FACT_STOP_WORDS.has(prev) && !FACT_STOP_WORDS.has(curr) && curr !== 'a' && curr !== 'i') {
117
- joined.push(prev + curr);
118
- }
119
- }
120
- return Array.from(new Set(joined));
121
- }
122
- function wordStems(wordList) {
123
- const out = new Set();
124
- const irregularBases = { does: 'do', has: 'have' };
125
- for (const word of wordList) {
126
- out.add(word);
127
- const irregular = irregularBases[word];
128
- if (irregular) {
129
- out.add(irregular);
130
- }
131
- else if (/^[a-z]+ies$/.test(word) && word.length >= 5) {
132
- out.add(`${word.slice(0, -3)}y`);
133
- }
134
- else if (/^[a-z]+(ches|shes|xes|zes|oes|sses)$/.test(word)) {
135
- out.add(word.slice(0, -2));
136
- }
137
- else if (/^[a-z]+s$/.test(word) && word.length >= 4 &&
138
- !word.endsWith('ss') && !word.endsWith('us') && !word.endsWith('is')) {
139
- out.add(word.slice(0, -1));
140
- }
141
- else if (/^[a-z]+ed$/.test(word) && word.length >= 5) {
142
- out.add(word.slice(0, -2));
143
- out.add(word.slice(0, -1));
144
- }
145
- else if (/^[a-z]+ing$/.test(word) && word.length >= 6) {
146
- out.add(word.slice(0, -3));
147
- }
148
- }
149
- return Array.from(out);
150
- }
151
- /**
152
- * The words a query looks facts up by, and the same with two neighbouring words joined: where ranking and scoring both
153
- * start. Ranking scores every candidate against one query, so the words are worked out once, not once per fact.
154
- */
155
- const FACT_SEARCH_TERMS_MAX = 8;
156
- const factSearchTermsHeld = new Map();
157
- function factSearchTerms(query) {
158
- const held = factSearchTermsHeld.get(query);
159
- if (held)
160
- return held;
161
- const terms = [...new Set([...factQueryTerms(query), ...joinedWords(query)])];
162
- if (factSearchTermsHeld.size >= FACT_SEARCH_TERMS_MAX)
163
- factSearchTermsHeld.delete(factSearchTermsHeld.keys().next().value);
164
- factSearchTermsHeld.set(query, terms);
165
- return terms;
166
- }
167
- /**
168
- * Stems of one field of a fact (its subject, predicate, object or saved statement words). The text is the whole input.
169
- * A read ranks a few dozen candidates and then scores each of them again, so a field is stemmed once per read, not twice
170
- * per fact and word; a subject or predicate that many facts share costs one stemming for all of them. A few hundred
171
- * fields are kept: the working set of one read, not of the workspace.
172
- */
173
- const FIELD_STEMS_MAX = 256;
174
- /** A field longer than this is stemmed again each time rather than held: one long object would take the room of dozens. */
175
- const FIELD_STEMS_MAX_CHARS = 1024;
176
- const fieldStems = new Map();
177
- const statementWordStems = new Map();
178
- function stemsOfField(text) {
179
- const held = fieldStems.get(text);
180
- if (held)
181
- return held;
182
- const stems = new Set(wordStems((0, factTerms_1.atoms)(text)));
183
- if (text.length <= FIELD_STEMS_MAX_CHARS) {
184
- if (fieldStems.size >= FIELD_STEMS_MAX)
185
- fieldStems.delete(fieldStems.keys().next().value);
186
- fieldStems.set(text, stems);
187
- }
188
- return stems;
189
- }
190
- /** The stems of the words a fact's saved statement was stored with (`statementTerms`: atoms joined by one space). */
191
- function stemsOfStatementWords(statementWords) {
192
- const text = statementWords ?? '';
193
- const held = statementWordStems.get(text);
194
- if (held)
195
- return held;
196
- const stems = new Set(wordStems(text ? text.split(' ') : []));
197
- if (text.length <= FIELD_STEMS_MAX_CHARS) {
198
- if (statementWordStems.size >= FIELD_STEMS_MAX)
199
- statementWordStems.delete(statementWordStems.keys().next().value);
200
- statementWordStems.set(text, stems);
201
- }
202
- return stems;
203
- }
204
- /**
205
- * Score a fact's relevance to the query: matches in subject and predicate weight highest because they define what
206
- * the fact is about; object matches are normalized by length (BM25 principle: long objects have incidental matches);
207
- * distinct query term coverage scales the score; and facts matching the request's scope or naming the queried scope are preferred.
208
- */
209
- function scoreFactRelevance(fact, query, requestScope) {
210
- const queryTerms = factSearchTerms(query);
211
- if (queryTerms.length === 0)
212
- return 0;
213
- return scoreFactTerms(fact, query, requestScope, queryTerms, queryTerms.map(cachedTermStems));
214
- }
215
- /** `scoreFactRelevance` for a query whose words, and their stems, the caller has already worked out. */
216
- function scoreFactTerms(fact, query, requestScope, queryTerms, termStemList) {
217
- const subjStems = stemsOfField(fact.subject);
218
- const predStems = stemsOfField(fact.predicate);
219
- const objStems = stemsOfField(fact.object ?? '');
220
- const objectText = fact.object ?? '';
221
- const objectWordTokens = objectText.trim() ? objectText.split(/\s+/).length : 0;
222
- let subjectMatches = 0;
223
- let predicateMatches = 0;
224
- let objectMatches = 0;
225
- let distinctMatched = 0;
226
- for (const tStems of termStemList) {
227
- const inSubj = tStems.some(s => subjStems.has(s));
228
- const inPred = tStems.some(s => predStems.has(s));
229
- const inObj = tStems.some(s => objStems.has(s));
230
- if (inSubj)
231
- subjectMatches++;
232
- if (inPred)
233
- predicateMatches++;
234
- if (inObj)
235
- objectMatches++;
236
- if (inSubj || inPred || inObj)
237
- distinctMatched++;
238
- }
239
- const subjectScore = subjectMatches * 4.0;
240
- const predicateScore = predicateMatches * 4.0;
241
- const bothBonus = subjectMatches > 0 && predicateMatches > 0 ? 3.0 : 0.0;
242
- const lengthDamping = 1.0 + (objectWordTokens / 24.0);
243
- const objectScore = (objectMatches * 2.0) / lengthDamping;
244
- const coverage = distinctMatched / queryTerms.length;
245
- const coverageMult = 1.0 + coverage;
246
- let baseScore = (subjectScore + predicateScore + bothBonus + objectScore) * coverageMult;
247
- if (requestScope && fact.scope) {
248
- if ((0, scope_1.savedScopeMatches)(fact.scope, (0, scope_1.normalizeScope)(requestScope))) {
249
- baseScore += 2.0;
250
- }
251
- }
252
- else if (!requestScope && fact.scope) {
253
- const scopeSlug = fact.scope.includes(':') ? fact.scope.slice(fact.scope.indexOf(':') + 1) : fact.scope;
254
- if (scopeSlug && query.toLowerCase().includes(scopeSlug.toLowerCase())) {
255
- baseScore += 1.5;
256
- }
257
- }
258
- return baseScore;
259
- }
260
- function rankCandidateFacts(facts, query, requestScope) {
261
- if (!query.trim() || facts.length <= 1)
262
- return facts;
263
- const queryTerms = factSearchTerms(query);
264
- const termStemList = queryTerms.map(cachedTermStems);
265
- const scored = facts.map(fact => {
266
- const subjStems = stemsOfField(fact.subject);
267
- const predStems = stemsOfField(fact.predicate);
268
- const objStems = stemsOfField(fact.object ?? '');
269
- // The words of the statement the fact was saved with let a fact with a terse object reach the bar that the best fact
270
- // sets from subject, predicate and object alone (bestDistinct below). A statement never raises that bar: it only
271
- // admits its own fact. The order (scoreFactRelevance) does not change.
272
- const stmtStems = stemsOfStatementWords(fact.statementWords);
273
- let ownMatched = 0;
274
- let distinctMatched = 0;
275
- for (const tStems of termStemList) {
276
- if (tStems.some(s => subjStems.has(s) || predStems.has(s) || objStems.has(s)))
277
- ownMatched++;
278
- if (tStems.some(s => subjStems.has(s) || predStems.has(s) || objStems.has(s) || stmtStems.has(s)))
279
- distinctMatched++;
280
- }
281
- return {
282
- fact,
283
- score: queryTerms.length === 0 ? 0 : scoreFactTerms(fact, query, requestScope, queryTerms, termStemList),
284
- ownMatched,
285
- distinctMatched,
286
- };
287
- });
288
- scored.sort((a, b) => {
289
- if (Math.abs(b.score - a.score) > 1e-4) {
290
- return b.score - a.score;
291
- }
292
- if (a.fact.validFrom !== b.fact.validFrom) {
293
- return a.fact.validFrom < b.fact.validFrom ? 1 : -1;
294
- }
295
- return (b.fact.eventId ?? '').localeCompare(a.fact.eventId ?? '');
296
- });
297
- const bestDistinct = Math.max(...scored.map(s => s.ownMatched), 0);
298
- if (bestDistinct <= 0)
299
- return facts;
300
- // A fact that shares fewer than half as many words as the best one is left out, counting the best one by its subject,
301
- // predicate and object words only: when "Why did we consider moving Atlas to Fly.io?" finds the Fly.io fact by three
302
- // words, a fact that only says "Atlas" is not what was asked.
303
- return scored
304
- .filter(item => item.distinctMatched * 2 >= bestDistinct)
305
- .map(item => item.fact);
306
- }
307
- /**
308
- * The maximum share of the facts lane's budget a single fact's object may take.
309
- * Long fact objects (e.g. 2,000-char benchmark outputs from other lanes) are capped to this share so they do
310
- * not crowd out subsequent answering facts in get_context.
311
- */
312
- exports.MAX_FACT_OBJECT_LANE_SHARE = 0.35;
313
- /**
314
- * Appended to a fact object the lane cut, so a reader knows it is a prefix and where the rest is: the agent
315
- * reads the whole fact with `read_memory` or `lookup_entity`.
316
- */
317
- exports.TRUNCATION_MARKER = '… [truncated — read the rest with read_memory or lookup_entity]';
318
- function capFactObject(object, maxTokens) {
319
- if (replyTokens(object) <= maxTokens)
320
- return object;
321
- const budget = Math.max(1, maxTokens - replyTokens(exports.TRUNCATION_MARKER));
322
- const words = object.split(/\s+/);
323
- let low = 0;
324
- let high = words.length;
325
- let best = '';
326
- while (low <= high) {
327
- const mid = Math.floor((low + high) / 2);
328
- const candidate = mid === 0 ? '' : words.slice(0, mid).join(' ') + '…';
329
- if (replyTokens(candidate) <= budget) {
330
- best = candidate;
331
- low = mid + 1;
332
- }
333
- else {
334
- high = mid - 1;
335
- }
336
- }
337
- return (best || (object.slice(0, Math.max(1, budget * 3)) + '…')) + exports.TRUNCATION_MARKER;
338
- }
339
- /** Whether a listed fact's object is `value`, or the prefix of it the lane cut (a cut object carries TRUNCATION_MARKER). */
340
- const objectIs = (shown, value) => shown === value || (shown.includes('…') && value.startsWith(shown.split('…')[0]));
341
- /**
342
- * H11b (RENOR_KEYWORD_OR_ALL=1, default off): the keyword lane issues one FTS expression, the question's content words joined
343
- * by OR (BM25 ranks rare words highest), instead of the variants. Unset, nothing changes. Pre-registered in
344
- * data/renor-goal-better-temporal-2/prereg/H11b-H18-H12.md.
345
- */
346
- function keywordOrAll(env = process.env) {
347
- return (env.RENOR_KEYWORD_OR_ALL ?? '').trim() === '1';
348
- }
349
- /** The FTS expression of H11b: each content word quoted, joined by OR. */
350
- function keywordOrMatch(query) {
351
- const terms = [...new Set((0, factTerms_1.words)(query).filter(word => !exports.FTS_STOP_WORDS.has(word)))];
352
- return terms.map(term => `"${term.replace(/"/g, '""')}"`).join(' OR ');
353
- }
354
- /** H12 (RENOR_SEARCH_TEXT_FALLBACK=1, default off): search_text falls back to an OR over the words when the exact phrase is empty. */
355
- function searchTextFallbackEnabled(env = process.env) {
356
- return (env.RENOR_SEARCH_TEXT_FALLBACK ?? '').trim() === '1';
357
- }
358
- function keywordVariants(query) {
359
- const compact = (0, factTerms_1.words)(query).filter(word => !exports.FTS_STOP_WORDS.has(word));
360
- const variants = [query.trim()];
361
- if (compact.length >= 2)
362
- variants.push(compact.slice(0, 4).join(' '));
363
- for (const word of compact.slice(0, 4))
364
- variants.push(word);
365
- return Array.from(new Set(variants.filter(Boolean))).slice(0, 6);
366
- }
367
- // get_context reads a fact as the user's own standing rule unless it says otherwise, so a fact another agent saved names
368
- // it, and an import is marked untrusted. The reader's own facts stay bare: the reply is budgeted with these fields counted.
369
- const toAgentFact = (fact, reader) => {
370
- const { subject, predicate, object, validFrom, scope } = fact;
371
- return { subject, predicate, object, validFrom, scope, ...(0, sourceName_1.authorMarker)(fact.author, reader) };
372
- };
373
- /**
374
- * The words a predicate holding a standing rule or decision is saved under: what a later question about whether an
375
- * action is allowed must be answered with, and what the action's own words never name. A predicate counts when it is
376
- * one of these or is made of words one of which is (`ui_rule`, `approval_policy`, `deploy constraint`). The facts lane
377
- * looks these up when the question asks for permission (`permissionQuestion`), because the question is about the action
378
- * and shares no subject words with the rule that governs it. The words are read through the term index
379
- * (`findCurrentFacts`), which is bounded by the query, not the table.
380
- *
381
- * A word outside this list is not a standing fact to the permission path, however it is worded (`note`, `status`): the
382
- * list is the contract, and a governing fact saved under another word is found only by the question's own words.
383
- */
384
- exports.GOVERNANCE_PREDICATES = [
385
- 'rule', 'rules', 'policy', 'policies', 'decision', 'decisions', 'guideline', 'guidelines',
386
- 'constraint', 'constraints', 'requirement', 'requirements', 'must', 'convention', 'conventions',
387
- 'principle', 'principles', 'guardrail', 'guardrails', 'permission', 'permissions', 'approval', 'approvals',
388
- ];
389
- /** Whether a fact's predicate holds a standing rule or decision (`GOVERNANCE_PREDICATES`), whole or by one of its words. */
390
- function isGovernancePredicate(predicate) {
391
- const lower = predicate.toLowerCase();
392
- if (exports.GOVERNANCE_PREDICATES.includes(lower))
393
- return true;
394
- return lower.split(/[^\p{L}\p{N}]+/u).some(word => word !== '' && exports.GOVERNANCE_PREDICATES.includes(word));
395
- }
396
- /**
397
- * The most governance facts a permission question adds ahead of the facts its words found: enough for
398
- * the rules that govern an action (there are usually a handful), never enough to crowd out the specific
399
- * fact the question named.
400
- */
401
- exports.PERMISSION_RULE_LIMIT = 3;
402
- /**
403
- * How many governance candidates are considered before the relevance ranking. Governance facts are a
404
- * small share of a workspace, and the term index bounds the read (factTerms.ts), so this is generous
405
- * while keeping the work bounded by the query. A workspace with more governance facts than this is not
406
- * silently mis-ranked: the candidates are ranked by relevance to the question, and the ones sharing its
407
- * words are the ones that matter.
408
- */
409
- const PERMISSION_RULE_CANDIDATES = 1_000;
410
- /**
411
- * The phrases that ask whether an action is allowed or needs approval, as a person words them. The
412
- * subject of such a question is the action ("Can I add a section to the dashboard?"), so it shares no
413
- * words with the rule that governs it; the intent has to be read from the question, not matched to the
414
- * rule. Matched on word boundaries over a lower-cased, punctuation-stripped query, contractions spelled out.
415
- *
416
- * These ask for permission wherever they are: a modal addressed to the speaker, or a need for approval.
417
- */
418
- const PERMISSION_PHRASES = [
419
- 'can i', 'can we', 'could i', 'could we', 'may i', 'may we', 'should i', 'should we',
420
- 'am i allowed', 'are we allowed', 'is it allowed', 'is this allowed', 'is that allowed', 'is that still allowed',
421
- 'need permission', 'needs permission', 'need approval', 'needs approval', 'need an approval',
422
- 'require approval', 'requires approval', 'am i permitted', 'are we permitted', 'is it permitted',
423
- 'need to ask', 'needs to ask', 'have to ask', 'has to ask', 'ask before', 'ask first', 'ask permission',
424
- 'need to check with', 'need to confirm with', 'have to check with',
425
- 'do i have to', 'do we have to',
426
- 'is it ok to', 'is it okay to', 'is it fine to', 'is it acceptable to', 'is that ok', 'is that okay',
427
- 'would it be okay to', 'would it be ok to', 'would it be fine to', 'would it be acceptable to',
428
- ];
429
- /**
430
- * Words that ask for permission only in a question about the present: "Which regions are allowed?" asks, "The
431
- * migration is allowed to run after review." states, and "Where did we check with the vendor?" recalls (delta review
432
- * P3, 2026-10-09).
433
- */
434
- const PERMISSION_WORDS = [
435
- 'allowed', 'permitted', 'permission to', 'permission for',
436
- 'check with', 'confirm with', 'clear it with', 'clear this with', 'okay with', 'ok with',
437
- ];
438
- /** Openings that make a sentence a question without its question mark. */
439
- const QUESTION_OPENINGS = /^(?:is|are|am|do|does|can|could|may|might|should|would|will|must|what|which|who|whom|whose|where|when|why|how)\b/;
440
- /** Openings of a question about what was done, not what may be: "Where did we", "When was it". */
441
- const RECOLLECTION = /^(?:what|which|who|where|when|why|how)\s+(?:did|was|were|had)\b/;
442
- /** Whether `query` asks whether an action is allowed or needs approval. */
443
- function permissionQuestion(query) {
444
- const spelled = query.toLowerCase()
445
- .replace(/[’']/g, "'")
446
- .replace(/\bcan't\b/g, 'can not').replace(/\bcannot\b/g, 'can not').replace(/\bwon't\b/g, 'will not')
447
- .replace(/\b(\p{L}+)n't\b/gu, '$1 not');
448
- const words = spelled.replace(/[^\p{L}\p{N}\s]+/gu, ' ').replace(/\s+/g, ' ').trim();
449
- if (!words)
450
- return false;
451
- // "can not i" / "could not we" ask what "can i" asks: the negation is the asker's doubt, not the action.
452
- const padded = ` ${words.replace(/\b(can|could|may|should|would|do) not (i|we)\b/g, '$1 $2')} `;
453
- if (PERMISSION_PHRASES.some(phrase => padded.includes(` ${phrase} `)))
454
- return true;
455
- const asks = query.trim().endsWith('?') || QUESTION_OPENINGS.test(words);
456
- return asks && !RECOLLECTION.test(words) && PERMISSION_WORDS.some(phrase => padded.includes(` ${phrase} `));
457
- }
458
- function governanceCandidates(db, scope) {
459
- return (0, factTerms_1.findCurrentFacts)(db, {
460
- terms: exports.GOVERNANCE_PREDICATES,
461
- scope,
462
- match: 'any',
463
- limit: PERMISSION_RULE_CANDIDATES,
464
- }).filter(fact => isGovernancePredicate(fact.predicate)); // a note that says "decision" is not one (delta review P3)
465
- }
466
- /**
467
- * The standing facts' closeness in meaning to a permission question (`PermissionMeaning`). `queryVector` is the one
468
- * the read's search by meaning embedded, when it ran; otherwise the question is embedded here, through the same
469
- * provider.
470
- *
471
- * It runs only for an embedding model with a measured bar (CALIBRATED_BARS: today the Vertex model production runs),
472
- * and with that bar. An explicit RENOR_CONTEXT_RELEVANCE_MIN tunes the relevance signal; it is not a calibration for
473
- * this lookup, so any other embedder skips it and the standing facts are found by words alone (Batch 7 follow-up c).
474
- */
475
- async function permissionMeaning(db, engine, query, scope, queryVector) {
476
- const provider = engine.provider;
477
- const bar = engine.modelKey && engine.modelKey in contextRelevance_1.CALIBRATED_BARS ? contextRelevance_1.CALIBRATED_BARS[engine.modelKey] : null;
478
- if (!provider || bar === null || !permissionQuestion(query))
479
- return undefined;
480
- try {
481
- const candidates = governanceCandidates(db, scope === undefined ? undefined : (0, scope_1.normalizeScope)(scope));
482
- if (candidates.length === 0)
483
- return undefined;
484
- const vector = queryVector ?? (provider.embedQuery ? await provider.embedQuery(query) : (await provider.embed([query]))[0]);
485
- if (!vector)
486
- return undefined;
487
- const similarity = await (0, contextRelevance_1.similaritiesTo)(provider, candidates.map(f => (0, contextRelevance_1.renderFactForRelevance)(f.predicate, f.object ?? '')), vector);
488
- return similarity ? { similarity, bar } : undefined;
489
- }
490
- catch {
491
- return undefined;
492
- }
493
- }
494
- /**
495
- * The standing facts a permission question should be answered with, at most `PERMISSION_RULE_LIMIT`:
496
- *
497
- * 1. the ones close in meaning to the question (at or over the calibrated bar, `permissionMeaning`), closest first:
498
- * a rule worded without any of the action's words ("Ship nothing until Rowan signs off" for "Can I deploy?");
499
- * 2. then the ones sharing a word with the question, ranked the way the word-found facts are (`scoreFactRelevance`:
500
- * subject and predicate overlap, a length-damped object, the named-scope bonus), then newest. On a scoped read the
501
- * scope's own name counts as one of the question's words: an agent scoped to project:renor asks "Can I add a
502
- * section to the dashboard?", and the rule about Renor is about its question (delta review P2, 2026-10-09);
503
- * 3. when none of them share a word, the newest.
504
- *
505
- * Without a provider or a bar, or when the backend fails, the list is 2 and 3, as it was.
506
- */
507
- function permissionFacts(db, query, scope, meaning) {
508
- const candidates = governanceCandidates(db, scope);
509
- if (candidates.length === 0)
510
- return [];
511
- const slug = scope?.includes(':') ? scope.slice(scope.indexOf(':') + 1) : scope;
512
- const asked = slug && slug !== 'global' && !query.toLowerCase().includes(slug.toLowerCase()) ? `${query} ${slug}` : query;
513
- const newestFirst = (a, b) => {
514
- if (a.validFrom !== b.validFrom)
515
- return a.validFrom < b.validFrom ? 1 : -1;
516
- return (b.eventId ?? '').localeCompare(a.eventId ?? '');
517
- };
518
- const close = meaning
519
- ? candidates
520
- .map(fact => ({ fact, similarity: meaning.similarity.get((0, contextRelevance_1.renderFactForRelevance)(fact.predicate, fact.object ?? '')) ?? -Infinity }))
521
- .filter(item => Math.round(item.similarity * 1000) / 1000 >= meaning.bar)
522
- .sort((a, b) => (Math.abs(b.similarity - a.similarity) > 1e-6 ? b.similarity - a.similarity : newestFirst(a.fact, b.fact)))
523
- .map(item => item.fact)
524
- : [];
525
- const scored = candidates
526
- .map(fact => ({ fact, score: scoreFactRelevance(fact, asked, scope) }))
527
- .sort((a, b) => (Math.abs(b.score - a.score) > 1e-4 ? b.score - a.score : newestFirst(a.fact, b.fact)));
528
- const related = scored.filter(item => item.score > 0);
529
- const byWords = (related.length > 0 ? related : scored).map(item => item.fact);
530
- const out = [];
531
- for (const fact of [...close, ...byWords]) {
532
- if (out.length === exports.PERMISSION_RULE_LIMIT)
533
- break;
534
- if (!out.includes(fact))
535
- out.push(fact);
536
- }
537
- return out;
538
- }
539
- /**
540
- * The standing facts a permission question put ahead of the facts its words found, for this read: get_context caps a
541
- * long one that leads the page (`capFact`). Marked on the fact itself, so a word-found fact with a standing predicate
542
- * is not taken for one, and a fact the permission path put first is, whatever its predicate (delta review P3).
543
- */
544
- const STANDING = new WeakSet();
545
- /**
546
- * Current lifecycle facts for a query: the facts found by its words (`factQueryTerms`, and two neighbouring words
547
- * joined, so "time zone" finds a `timezone` key), ranked by query relevance and scope preference, then newest. A fact
548
- * that scores less than half as much as the best one is left out. With no query, the newest facts.
549
- *
550
- * A question about whether an action is allowed or needs approval is also about the standing rule or
551
- * decision that governs it, which its words do not name. Those facts are looked up through the term
552
- * index (`permissionFacts`) and put first, ahead of the facts the words found: the rule is the answer,
553
- * and the long other-lane facts that share the action's words used to fill the candidate window before
554
- * the rule was even ranked (2026-10-09). They are ranked by meaning where the embedding path can say, then by
555
- * relevance to the question, not by recency, deduped against the word-found facts, and capped.
556
- */
557
- function currentLifecycleFacts(db, query = '', scope, limit = 12) {
558
- return currentFactMatches(db, query, scope, limit).map(fact => toAgentFact(fact));
559
- }
560
- /** `currentLifecycleFacts` with each fact's ledger event, which get_context reads to list an event once. */
561
- function currentFactMatches(db, query, scope, limit, meaning) {
562
- const scopeNorm = scope === undefined ? undefined : (0, scope_1.normalizeScope)(scope);
563
- if (!query.trim())
564
- return (0, factTerms_1.recentCurrentFacts)(db, scopeNorm, limit);
565
- const found = (0, factTerms_1.findCurrentFacts)(db, {
566
- terms: [...factQueryTerms(query), ...joinedWords(query)],
567
- scope: scopeNorm,
568
- match: 'any',
569
- limit: Math.max(limit * 3, 36),
570
- });
571
- const ranked = rankCandidateFacts(found, query, scopeNorm);
572
- if (!permissionQuestion(query))
573
- return ranked.slice(0, limit);
574
- const standing = permissionFacts(db, query, scopeNorm, meaning);
575
- if (standing.length === 0)
576
- return ranked.slice(0, limit);
577
- const key = (fact) => `${fact.scope ?? 'global'}\u0000${fact.subject}\u0000${fact.predicate}`;
578
- const seen = new Set(ranked.map(key));
579
- const prepended = standing.filter(fact => !seen.has(key(fact)));
580
- for (const fact of prepended)
581
- STANDING.add(fact);
582
- return [...prepended, ...ranked].slice(0, limit);
583
- }
584
- /**
585
- * Whether a name is on file for a read: a current fact or a note (its text, path or front matter) of the scope read has
586
- * its words. Read through the indexes the facts lane and search_text read, so a scoped read never consults another scope.
587
- */
588
- function nameOnFile(db, memoryDir, { scope, includeHistory = false, ledgerRelPath } = {}) {
589
- return name => {
590
- const terms = (0, factTerms_1.atoms)(name);
591
- if (terms.length === 0)
592
- return true; // nothing to look up is no evidence
593
- if ((0, factTerms_1.findCurrentFacts)(db, { terms, scope, match: 'all', limit: 1 }).length > 0)
594
- return true;
595
- const keyword = (0, engine_1.engineSearchText)(db, memoryDir, terms.join(' '), 1, scope, includeHistory, ledgerRelPath);
596
- return !keyword.ok || keyword.results.length > 0;
597
- };
598
- }
599
- /** Whether `hit` is the semantic hit the probe scored best, as it is listed. */
600
- function isTopHit(top, hit) {
601
- return top !== undefined && hit.notePath === top.notePath && (hit.headingPath ?? '') === (top.headingPath || '')
602
- && hit.text.trim() === top.text.trim();
603
- }
604
- /**
605
- * The places `top_k` leaves for facts on a page of `indexHits`: what the index's own hits do not fill, the leading
606
- * evidence summary aside (that one is outside top_k by the tool's own promise). Zero on a page top_k has already
607
- * filled, which is the only case in which a caller cannot list a fact it wants to.
608
- */
609
- function factLaneRoom(indexHits, limit) {
610
- const lead = (0, digests_1.leadingSummaryHits)(indexHits);
611
- return Math.max(0, limit - (indexHits.length - lead));
612
- }
613
- /**
614
- * search_memory's relevance (RENOR_CONTEXT_RELEVANCE, where armed): get_context's verdict over the same read. A
615
- * search adds no hit for the verdict, so "relevant" stands only when the hits hold the match it reports: the closest
616
- * returned note chunk, or the closest fact (as a fact hit or its ledger event). A relevant verdict whose fact the
617
- * hits do not hold is returned with that fact in `listFact` for the caller to list — the same rule get_context applies
618
- * to the fact its own verdict was judged on (`missed`), and for the same reason: the fact lane exists because the
619
- * ledger chunk behind a fact is boilerplate that does not rank (contextRelevance.ts), so on a workspace whose notes
620
- * out-rank that chunk — and whose full page leaves the facts lane no room — the fact was not going to be listed, and
621
- * withholding the verdict there left the agent with no way to tell "the answer is on file" from "cannot judge".
622
- * `limit` is the page's top_k, which decides that case: with room the caller lists the fact, and without it the
623
- * verdict names the fact itself (`relevance.fact`) rather than the page passing top_k. The chunk lane needs no such
624
- * rule: the probe's best chunk is the hit it was taken from, so it is always held.
625
- *
626
- * `hits` are the index's own hits, before the facts lane adds any: a fact the hits already carry is not named twice.
627
- */
628
- function searchRelevance(db, engine, memoryDir, read, probe, hits, limit) {
629
- if (!(0, contextRelevance_1.contextRelevanceEnabled)(read.env))
630
- return {};
631
- const scope = read.scope === undefined ? undefined : (0, scope_1.normalizeScope)(read.scope);
632
- const judged = (0, contextRelevance_1.assessQueryRelevance)({
633
- db, engine, scope, probe, env: read.env,
634
- query: read.query, onFile: nameOnFile(db, memoryDir, { scope, includeHistory: read.includeHistory, ledgerRelPath: read.ledgerRelPath }),
635
- });
636
- if (!judged)
637
- return {};
638
- if (!judged.relevance.relevant)
639
- return { relevance: judged.relevance };
640
- const held = judged.matchedOn === 'chunk'
641
- ? hits.some(hit => isTopHit(probe.topHit, hit))
642
- : (() => {
643
- const event = judged.closestFact?.eventId?.toLowerCase();
644
- return event !== undefined && hits.some(hit => (0, ledgerLiveness_1.ledgerEventIds)(hit).includes(event));
645
- })();
646
- if (held)
647
- return { relevance: judged.relevance };
648
- if (judged.matchedOn !== 'fact' || !judged.closestFact)
649
- return {};
650
- // A page top_k has already filled takes no more hits, so the fact cannot be listed; naming it is what keeps the
651
- // verdict honest without breaking the tool's own bound (factsLane `also`).
652
- if (limit !== undefined && factLaneRoom(hits, limit) === 0) {
653
- const { subject, predicate, object, scope: factScope } = judged.closestFact;
654
- return { relevance: { ...judged.relevance, fact: { subject, predicate, object, scope: (0, scope_1.normalizeScope)(factScope) } } };
655
- }
656
- return { relevance: judged.relevance, listFact: judged.closestFact };
657
- }
658
- /** A note the import tool wrote (importing/hosted.ts: `Imported/<label>/…`), never one of the engine's own notes. */
659
- const IMPORTED_PATH = /^Imported\//;
660
- /**
661
- * A score as a reply lists it: six decimals, whatever else is on the page. Tools tell agents to judge hits by score, so the
662
- * rounding must not tie hits the ranking told apart: four decimals did (0.50004 and 0.50003 are both 0.5), six tie only
663
- * scores closer than 5e-7, and no pair on the 50 search_memory pages of the agent-workflow eval. A hit shows the same score on
664
- * every page.
665
- */
666
- function scoreForReply(score) {
667
- return Math.round(score * 1e6) / 1e6;
668
- }
669
- /** A heading that only repeats the note's own name (a daily note's heading is its date) says nothing the path does not. */
670
- function headingForReply(hit) {
671
- if (typeof hit.notePath !== 'string' || !hit.headingPath)
672
- return hit;
673
- const file = hit.notePath.slice(hit.notePath.lastIndexOf('/') + 1).replace(/\.md$/i, '');
674
- if (hit.headingPath !== file)
675
- return hit;
676
- const { headingPath: _headingPath, ...rest } = hit;
677
- return rest;
678
- }
679
- function normalizeMemory(raw, fallbackReason, reader) {
680
- if (!raw || typeof raw !== 'object')
681
- return null;
682
- const hit = raw;
683
- if (typeof hit.text !== 'string' || !hit.text.trim())
684
- return null;
685
- const reason = typeof hit.reason === 'string' ? hit.reason : fallbackReason;
686
- return {
687
- notePath: typeof hit.notePath === 'string' ? hit.notePath : '',
688
- ...(typeof hit.headingPath === 'string' && hit.headingPath ? { headingPath: hit.headingPath } : {}),
689
- text: hit.text,
690
- ...(typeof hit.score === 'number' ? { score: hit.score } : {}),
691
- reason,
692
- ...(typeof hit.session_date === 'string' ? { session_date: hit.session_date } : {}),
693
- // A fact the retrieval named its author of (a fact an identity question reads) is marked as get_context's facts are.
694
- // A memory that is a ledger event is marked in buildAgentContext, which reads its event's author.
695
- ...(hit.author !== undefined ? (0, sourceName_1.authorMarker)(hit.author, reader) : {}),
696
- // A statement the session expansion added sits in the same memories[] list as matched evidence, so the item says
697
- // in a field a reader reads that the query did not match it (adv hardening, report renor-adv-inj5-1011 proposal 3):
698
- // an explicit boolean, in the marker style the hive tools use for untrusted agent text (memory/whatsNew.ts). The
699
- // search lane is untouched, so a matched memory — and every search_memory hit — keeps the fields it had, byte for
700
- // byte. The marker's tokens are budget tokens like any other item's (applyBudget `cost`).
701
- ...(reason.split('+').includes(orchestrator_1.SESSION_EXPAND_REASON) ? { matched: false } : {}),
702
- ...(IMPORTED_PATH.test(String(hit.notePath ?? '')) ? { origin: 'import' } : {}),
703
- // A daily-log chunk an entry of which claims a reserved source (factMarkers.ts): marked here, before the budget counts it.
704
- ...((0, factMarkers_1.claimsReservedEntrySource)(hit.notePath, hit.text) ? { untrusted: true } : {}),
705
- };
706
- }
707
- exports.CONTEXT_LEAN_ITEMS_FLAG = 'RENOR_CONTEXT_LEAN_ITEMS';
708
- /** get_context's lean memory items (RENOR_CONTEXT_LEAN_ITEMS): off by default, and the reply is then what it always was. */
709
- function contextLeanItemsEnabled(env = process.env) {
710
- const raw = (env[exports.CONTEXT_LEAN_ITEMS_FLAG] ?? '').trim().toLowerCase();
711
- return raw === '1' || raw === 'true' || raw === 'yes' || raw === 'on';
712
- }
713
- /**
714
- * A memory as the lean get_context lists it: its text as stored, the day it was written, and every marker it carries
715
- * (replaced, matched, origin, source, untrusted). The note path, heading, score and reason go: they cost about twice
716
- * the memory's own text (an item was about 107 tokens for 32 of text, docs/evals/results/agent-workflow-20261020), and
717
- * the reply is already in the order they decided. A daily entry's text keeps its own time and writer.
718
- */
719
- function leanMemory(memory) {
720
- const { notePath: _notePath, headingPath: _headingPath, text, score: _score, reason: _reason, session_date, ...markers } = memory;
721
- const day = session_date === undefined ? undefined : /^\d{4}-\d{2}-\d{2}/.exec(session_date)?.[0] ?? session_date;
722
- return { text, ...(day === undefined ? {} : { session_date: day }), ...markers };
723
- }
724
- /** The reasons of a memory the question's own words found (keyword search), as opposed to one found by meaning alone. */
725
- const WORD_MATCH_REASONS = ['fts', 'note-identity'];
726
- function dedupeMemories(memories) {
727
- const at = new Map();
728
- const out = [];
729
- for (const memory of memories) {
730
- const key = `${memory.notePath}\0${memory.headingPath ?? ''}\0${memory.text.trim()}`;
731
- const kept = at.get(key);
732
- if (kept === undefined) {
733
- at.set(key, out.length);
734
- out.push(memory);
735
- }
736
- else if (WORD_MATCH_REASONS.includes(memory.reason) && !out[kept].reason.split('+').includes(memory.reason)) {
737
- // The question's words found a memory that is already listed (by meaning, usually): the one copy keeps saying so,
738
- // where it used to be dropped without a trace, and a caller cannot otherwise tell "close in meaning" from "contains
739
- // what you asked for".
740
- out[kept] = { ...out[kept], reason: `${out[kept].reason}+${memory.reason}` };
741
- }
742
- }
743
- return out;
744
- }
745
- /**
746
- * The most of the budget facts take while memories are waiting; the memories get the rest, and the room they leave
747
- * goes back to the facts. Facts used to be added until the budget was spent, so a dozen facts that each shared one
748
- * word with the question cut the note that answered it.
749
- */
750
- exports.FACTS_BUDGET_SHARE = 0.4;
751
- /**
752
- * The most of the budget the newest match above the relevance bar and the rest of its own session may take, before the
753
- * others are taken by score (applyBudget): the match itself, its other matches above the bar, and the statements the
754
- * session's retrieval expansion added. A session that said something newer about the question said it in more than one
755
- * chunk, and the one that holds the answer is often not its closest: with the reservation alone, a closer chunk of an older
756
- * session took the room and the later statement was cut. A quarter of the budget (the first value, PR 508) left a
757
- * budget of 800 tokens room for one big chunk of the session and no statement beside it; the practice questions were best
758
- * served at 0.6, and worse at 0.5 and 0.4 (docs/evals/results/NEWEST-FACT-POOL-20261011.md).
759
- */
760
- exports.NEWEST_SESSION_SHARE = 0.6;
761
- /**
762
- * The most pieces of the newest session the reservation takes: the match, its other matches and its statements together.
763
- * They are taken by relevance per token, so the share above would otherwise be spent on as many small pieces as fit, and
764
- * four is what the 1,200-token budget holds beside the older evidence without losing a question the cut by score kept.
765
- */
766
- exports.NEWEST_SESSION_PIECES = 4;
767
- /**
768
- * The fewest of the question's content words (`statementRelevance`) a statement of the newest session must hold to be
769
- * a candidate for its reservation. The reservation ranks its candidates by relevance squared per token, so a statement with
770
- * one shared word takes room only when nothing better fits; one with none is unmatched text and never takes it.
771
- */
772
- exports.NEWEST_SESSION_STATEMENT_FLOOR = 1;
773
- /**
774
- * How much more a session statement weighs against a chunk of the same relevance. A statement is a short piece of what
775
- * the user said (the expansion cuts them at 100 tokens); a chunk is 250 to 500 tokens of the answer around it.
776
- */
777
- exports.NEWEST_SESSION_STATEMENT_WEIGHT = 2;
778
- /**
779
- * The most statements that share a single word of the question the reservation takes. One shared word is a coincidence, and the
780
- * deciding statement can be one (a turn that says "I now have 5 of them" shares "bikes"), but a session of many short turns
781
- * about the same subject has several, and each weighs double and costs half a chunk: four of them took the room of the scored
782
- * chunk that held the answer (a held-out question about how many bikes a person owns, after the statement cap went to 120
783
- * tokens). A statement that shares two words, and every match retrieval scored, are not counted.
784
- */
785
- exports.NEWEST_SESSION_WEAK_STATEMENTS = 1;
786
- exports.CONTEXT_SPLIT_FLAG = 'RENOR_CONTEXT_SPLIT';
787
- /**
788
- * How get_context splits its budget between facts, the newest session and the older evidence (RENOR_CONTEXT_SPLIT, off by
789
- * default; unset, or anything but `facts,recent,pieces`, gives the constants above). `facts` is the facts' share while memories
790
- * wait and `recent` the newest session's share, both in (0, 1]; `pieces` is the most pieces of that session, an integer from
791
- * 1 to 12. The older evidence takes what the two leave.
792
- */
793
- function budgetSplit() {
794
- const off = { facts: exports.FACTS_BUDGET_SHARE, recent: exports.NEWEST_SESSION_SHARE, pieces: exports.NEWEST_SESSION_PIECES };
795
- const raw = process.env[exports.CONTEXT_SPLIT_FLAG];
796
- if (!raw)
797
- return off;
798
- const [facts, recent, pieces] = raw.split(',').map(part => Number(part.trim()));
799
- const share = (n) => Number.isFinite(n) && n > 0 && n <= 1;
800
- return share(facts) && share(recent) && Number.isInteger(pieces) && pieces >= 1 && pieces <= 12 ? { facts, recent, pieces } : off;
801
- }
802
- /** The fewest of the question's content words the newest match above the bar must share with it (`newestMatchAbove`). */
803
- exports.NEWEST_ANCHOR_RELEVANCE_FLOOR = 1;
804
- /** Tokens of a reply as the engine counts them: o200k, the chunker's tokenizer, special-token syntax read as text.
805
- * The reply is escaped after fitting (jsonResult), so the budget counts what is actually sent: a memory
806
- * rich in hidden characters must not push the reply past max_tokens once they become \uXXXX escapes. */
807
- function replyTokens(text) {
808
- return (0, tokenizer_1.encodeTokens)((0, sanitize_1.escapeInvisibleControls)(text));
809
- }
810
- /**
811
- * The order the matches are taken into a token budget in, as positions in `memories`: first the summary tables that lead
812
- * the list, when there are any (the evidence and digest tables, `leadingSummaryHits`: they sit outside any order), then the
813
- * matches retrieval scored, best score first, then the matches the words of the request found, as they are listed. A match
814
- * retrieval scored is one whose score is the one that picked the window (cosine plus its boosts), so those compare; keyword
815
- * matches are scored on other scales (WORD_MATCH_REASONS) and are not ranked with them. Equal scores keep their place in the
816
- * list, which makes the order the same on every call, and under "score" (a list already in score order) the order is the
817
- * list's own. What the session expansion added is no match and is not in it.
818
- *
819
- * The budget keeps the longest run of this order that fits (applyBudget), except that a top match too big for the whole budget is
820
- * passed over. The list is laid out newest first, and a budget that kept the longest run of the layout cut the oldest matches
821
- * first however close they were: retrieval ablations, 2026-10-06 (docs/evals/results/CUT-BY-SCORE-20261007.md, which also reads what the cut by score trades away).
822
- */
823
- function matchBudgetOrder(memories) {
824
- const leading = (0, digests_1.leadingSummaryHits)(memories);
825
- const lead = [];
826
- const scored = [];
827
- const found = [];
828
- memories.forEach((memory, i) => {
829
- if (memory.reason === orchestrator_1.SESSION_EXPAND_REASON)
830
- return;
831
- if (i < leading)
832
- lead.push(i);
833
- else if (memory.reason.split('+').every(reason => WORD_MATCH_REASONS.includes(reason)))
834
- found.push(i);
835
- else
836
- scored.push(i);
837
- });
838
- // A score that is not a number ranks last among the scored matches, never first: it cannot displace a match.
839
- const score = (i) => (Number.isFinite(memories[i].score) ? memories[i].score : Number.NEGATIVE_INFINITY);
840
- scored.sort((a, b) => score(b) - score(a) || a - b);
841
- return [...lead, ...scored, ...found];
842
- }
843
- /**
844
- * The newest match above the relevance bar, as a position in `memories` (-1 when there is none): the match a cut by score
845
- * must not take the place of. A cut by score keeps the closest matches, and when two statements are about one thing it
846
- * cannot tell which is current; the newest one that is itself relevant (its raw cosine, `similarities`, is at least `bar`,
847
- * the bar the relevance signal judges by) is the one the reader needs last. A match that is not a semantic hit of this
848
- * read (a keyword match, a statement the expansion added) has no cosine and is never it, and neither is one with no date.
849
- * The newer date wins; of equal dates the earlier in the list, which is the better score.
850
- *
851
- * With the question (`query`), a match must also share NEWEST_ANCHOR_RELEVANCE_FLOOR of its content words to be it: the
852
- * cosine is a measure of meaning, and a newer session about something else (a fashion chat that answers a question about
853
- * a grocery list) can sit above the bar and would take the reservation, and the session's room, from the session
854
- * that answers. A question with no word to share (only numbers and single letters, `statementRelevance` counts neither) leaves the bar alone to decide.
855
- */
856
- /** First similarity for each hit, keyed the way `isTopHit` compares a memory to a hit. */
857
- function similarityByMemory(similarities) {
858
- const byNote = new Map();
859
- for (const hit of similarities) {
860
- const heading = hit.headingPath || '';
861
- const text = hit.text.trim();
862
- let byHeading = byNote.get(hit.notePath);
863
- if (!byHeading)
864
- byNote.set(hit.notePath, byHeading = new Map());
865
- let byText = byHeading.get(heading);
866
- if (!byText)
867
- byHeading.set(heading, byText = new Map());
868
- if (!byText.has(text))
869
- byText.set(text, hit.similarity);
870
- }
871
- return memory => byNote.get(memory.notePath)?.get(memory.headingPath ?? '')?.get(memory.text.trim());
872
- }
873
- function newestMatchAbove(memories, similarities, bar, query) {
874
- let newest = -1;
875
- let newestAt = Number.NEGATIVE_INFINITY;
876
- const asked = query === undefined ? 0 : questionTerms(query).length;
877
- // The cosine rules out almost every memory. Stemming a statement the cosine has already rejected cannot change the pick.
878
- const heardAt = similarityByMemory(similarities);
879
- memories.forEach((memory, i) => {
880
- if (memory.reason === orchestrator_1.SESSION_EXPAND_REASON || !memory.session_date)
881
- return;
882
- const heard = heardAt(memory);
883
- if (heard === undefined || !(heard >= bar))
884
- return;
885
- if (asked > 0 && statementRelevance(memory.text, query) < exports.NEWEST_ANCHOR_RELEVANCE_FLOOR)
886
- return;
887
- const at = (0, orchestrator_1.sessionDateMs)(memory.session_date);
888
- if (at > newestAt) {
889
- newest = i;
890
- newestAt = at;
891
- }
892
- });
893
- return newest;
894
- }
895
- /** The session a match belongs to: its note and its date. A chat session is one note with one date; a ledger or a daily log holds one session per event or day. */
896
- const sessionOf = (memory) => `${memory.notePath}\0${memory.session_date ?? ''}`;
897
- /** The content words of a question: the ones a statement can share with it (`statementRelevance`). */
898
- function questionTerms(query) {
899
- return factQueryTerms(query).filter(term => term.length > 1 && !/^\p{Nd}+$/u.test(term));
900
- }
901
- /**
902
- * How many distinct content words of the question a text holds, as stems: how much a statement says about the
903
- * question that brought its session. The session expansion adds a statement without scoring it, and the budget that
904
- * keeps the newest session's statements can rarely keep all of them: the one that answers the question is usually
905
- * not the first the session said (a header, a turn about something else), so they are taken most relevant first.
906
- * A word of one letter or one made of digits alone never says what a statement is about — "I" is in every statement
907
- * of a chat, and "10 pounds" answers "how much" without the number being what a later question shares — so neither
908
- * is counted.
909
- */
910
- /** Stems of a statement text. The text is the whole input, so a later write of a different statement is a different key. */
911
- const STATEMENT_STEMS_MAX = 4096;
912
- const statementStems = new Map();
913
- const TERM_STEMS_MAX = 1024;
914
- const termStems = new Map();
915
- const QUESTION_TERMS_MAX = 64;
916
- const questionTermCache = new Map();
917
- function cachedQuestionTerms(query) {
918
- const held = questionTermCache.get(query);
919
- if (held)
920
- return held;
921
- const terms = questionTerms(query);
922
- if (questionTermCache.size >= QUESTION_TERMS_MAX)
923
- questionTermCache.delete(questionTermCache.keys().next().value);
924
- questionTermCache.set(query, terms);
925
- return terms;
926
- }
927
- function cachedTermStems(term) {
928
- const held = termStems.get(term);
929
- if (held)
930
- return held;
931
- const stems = wordStems([term]);
932
- if (termStems.size >= TERM_STEMS_MAX)
933
- termStems.delete(termStems.keys().next().value);
934
- termStems.set(term, stems);
935
- return stems;
936
- }
937
- function cachedStatementStems(text) {
938
- const held = statementStems.get(text);
939
- if (held)
940
- return held;
941
- const stems = new Set(wordStems((0, factTerms_1.atoms)(text)));
942
- if (statementStems.size >= STATEMENT_STEMS_MAX)
943
- statementStems.delete(statementStems.keys().next().value);
944
- statementStems.set(text, stems);
945
- return stems;
946
- }
947
- function statementRelevance(text, query) {
948
- const terms = cachedQuestionTerms(query);
949
- if (terms.length === 0)
950
- return 0;
951
- const stems = cachedStatementStems(text);
952
- let held = 0;
953
- for (const term of terms)
954
- if (cachedTermStems(term).some(stem => stems.has(stem)))
955
- held++;
956
- return held;
957
- }
958
- /**
959
- * The least of the question's content words a session statement must hold to be taken with the newest session's
960
- * reservation, before the matches are taken by score (applyBudget `sessionStatements`). One shared word is a
961
- * coincidence: a session header shares a date, a turn about something else shares a topic word, and the statement
962
- * that decides the answer sometimes shares none at all. Two is the least that says a statement is about the
963
- * question, so a statement below it keeps the expansion's own leftover room, in document order, which is where it
964
- * was. Measured on the LongMemEval held-out knowledge-update cases: every statement the reservation delivered held
965
- * two or more of the question's words, and the statements it displaced from other sessions held one or none.
966
- */
967
- exports.STATEMENT_RELEVANCE_FLOOR = 2;
968
- /**
969
- * The statements of the session `memories[at]` belongs to, as positions in `memories`, ranked for the budget's
970
- * reservation: most relevant to the question first (`statementRelevance`), and only from STATEMENT_RELEVANCE_FLOOR, so
971
- * a statement the question shares fewer words with is a coincidence and keeps the expansion's own leftover room. Equal
972
- * relevance keeps the order of the list, which is document order inside the note. A session that said something newer
973
- * about the question said the deciding thing somewhere else, and the statement that holds it is usually not the first
974
- * the session said: a header, a turn about something else, the answer. `floor` is the fewest shared words a statement
975
- * needs: STATEMENT_RELEVANCE_FLOOR unless the caller ranks what it takes by relevance per token, as the budget's
976
- * reservation does (NEWEST_SESSION_STATEMENT_FLOOR).
977
- */
978
- function sessionStatementsFor(memories, at, query, floor = exports.STATEMENT_RELEVANCE_FLOOR) {
979
- if (at < 0 || !query.trim())
980
- return [];
981
- const session = sessionOf(memories[at]);
982
- return memories
983
- .map((memory, i) => ({ memory, i }))
984
- .filter(({ memory, i }) => i !== at && memory.reason === orchestrator_1.SESSION_EXPAND_REASON && sessionOf(memory) === session)
985
- .map(({ memory, i }) => ({ i, relevance: statementRelevance(memory.text, query) }))
986
- .filter(entry => entry.relevance >= floor)
987
- .sort((a, b) => b.relevance - a.relevance)
988
- .map(entry => entry.i);
989
- }
990
- /**
991
- * The other matches of the session `memories[at]` belongs to that are themselves above the relevance bar (as `newestMatchAbove`
992
- * counts), as positions in `memories`, in the order of the list: within a session that is best score first. What a session
993
- * said about the question is rarely one chunk, and the chunk that holds the answer is often not its closest.
994
- */
995
- function sessionMatesAbove(memories, similarities, bar, at) {
996
- if (at < 0)
997
- return [];
998
- const session = sessionOf(memories[at]);
999
- const mates = [];
1000
- memories.forEach((memory, i) => {
1001
- if (i === at || memory.reason === orchestrator_1.SESSION_EXPAND_REASON || !memory.session_date || sessionOf(memory) !== session)
1002
- return;
1003
- const heard = similarities.find(hit => isTopHit(hit, memory));
1004
- if (heard !== undefined && heard.similarity >= bar)
1005
- mates.push(i);
1006
- });
1007
- return mates;
1008
- }
1009
- function applyBudget(facts, factEvents, memories, memoryEvents, allowance, cost, plan = {}) {
1010
- const { keep, recent, relevance } = plan;
1011
- // `keep` comes before the reserved session when nothing is reserved, when it is a fact, or when it says more about the question.
1012
- const keepFirst = keep === undefined || recent === undefined || relevance === undefined || !memories.includes(keep) || relevance(keep) > relevance(recent);
1013
- const budgeted = budgetWith(keepFirst, facts, factEvents, memories, memoryEvents, allowance, cost, plan);
1014
- if (keepFirst)
1015
- return budgeted;
1016
- // The reserved session came first, and the facts took the room before it: if neither the match the verdict was judged on
1017
- // nor `recent` is in the reply, the verdict has nothing to stand on (`stands`) and the reply loses `relevance`. `keep`
1018
- // then takes its room first, and the session takes what is left in the order it always did (`ordered`): the room a held match
1019
- // leaves is too small to spend on a ranking by the words of the question.
1020
- const reserved = memories.indexOf(recent);
1021
- const holdsRecent = budgeted.memories.includes(recent)
1022
- || (reserved >= 0 && memoryEvents[reserved].length > 0 && memoryEvents[reserved].every(id => budgeted.listed.has(id)));
1023
- const holdsKeep = budgeted.memories.includes(keep)
1024
- || (memoryEvents[memories.indexOf(keep)]?.length > 0 && memoryEvents[memories.indexOf(keep)].every(id => budgeted.listed.has(id)));
1025
- return holdsRecent || holdsKeep ? budgeted : budgetWith(true, facts, factEvents, memories, memoryEvents, allowance, cost, plan, true);
1026
- }
1027
- function budgetWith(keepFirst, facts, factEvents, memories, memoryEvents, allowance, cost, { keep, closest, recent, mates = [], sessionStatements = [], relevance } = {}, ordered = false) {
1028
- const added = (memory) => memories[memory].reason === orchestrator_1.SESSION_EXPAND_REASON;
1029
- const factShare = memories.some((_, i) => !added(i)) ? Math.floor(allowance * budgetSplit().facts) : allowance;
1030
- let used = 0;
1031
- let held; // `keep`, once it has its room
1032
- const fits = (item, limit) => {
1033
- if (item === held)
1034
- return true; // its room is taken
1035
- if (used + cost(item) > limit)
1036
- return false;
1037
- used += cost(item);
1038
- return true;
1039
- };
1040
- const listed = new Set();
1041
- const kept = new Set(); // the memories listed, by position
1042
- const repeats = (memory) => memoryEvents[memory].length > 0 && memoryEvents[memory].every(id => listed.has(id));
1043
- // A fact the reply lists: a memory that is its event is not listed again, and gives its room back.
1044
- const list = (event) => {
1045
- if (!event)
1046
- return;
1047
- listed.add(event);
1048
- for (const memory of kept) {
1049
- if (repeats(memory)) {
1050
- kept.delete(memory);
1051
- used -= cost(memories[memory]);
1052
- }
1053
- }
1054
- };
1055
- const placeKeep = () => {
1056
- if (keep === undefined)
1057
- return;
1058
- const heldMemory = memories.indexOf(keep);
1059
- if (heldMemory >= 0 && kept.has(heldMemory))
1060
- held = keep; // the reservation took it, and its room is paid
1061
- else if (fits(keep, allowance)) {
1062
- held = keep;
1063
- const heldFact = facts.indexOf(keep);
1064
- if (heldFact >= 0)
1065
- list(factEvents[heldFact]);
1066
- else if (heldMemory >= 0)
1067
- kept.add(heldMemory);
1068
- else if (keep === closest?.fact)
1069
- list(closest.event);
1070
- }
1071
- };
1072
- if (keepFirst)
1073
- placeKeep();
1074
- // The top-ranked fact that the whole allowance cannot hold is delivered as a marked truncated prefix that fits
1075
- // the room it has, not dropped: a long fact that is itself the answer is the one item the budget must not lose.
1076
- // (The P2-1 cap is a crowding rule and never touches the top fact; this is the no-room case, where the choice is
1077
- // a prefix or nothing.)
1078
- let topFactCut = false;
1079
- let top = facts;
1080
- if (facts.length > 0 && used + cost(facts[0]) > allowance) {
1081
- const first = facts[0];
1082
- const object = first.object ?? '';
1083
- const room = Math.max(1, allowance - used - (cost(first) - replyTokens(object)));
1084
- top = [{ ...first, object: capFactObject(object, room) }, ...facts.slice(1)];
1085
- topFactCut = true;
1086
- }
1087
- let keptFacts = 0;
1088
- // The first fact is the best match, so it may take more than the facts' share.
1089
- while (keptFacts < top.length && fits(top[keptFacts], keptFacts === 0 ? allowance : factShare))
1090
- list(factEvents[keptFacts++]);
1091
- // The newest match above the relevance bar and the rest of its session take their room before the others are taken by score.
1092
- // They are one pool, taken by relevance to the question squared per token (a statement weighing more), at most
1093
- // NEWEST_SESSION_PIECES of them in at most NEWEST_SESSION_SHARE of the budget. One that does not fit that room, or the
1094
- // allowance, is passed over for the next (it must not end the run), so a session's closest chunk can be too big for the budget
1095
- // while what it said elsewhere still fits, and the statement that decides the answer is usually one of the small ones.
1096
- const reserved = recent === undefined ? -1 : memories.indexOf(recent);
1097
- if (reserved >= 0) {
1098
- const split = budgetSplit();
1099
- const room = Math.floor(allowance * split.recent);
1100
- const said = (item) => relevance?.(item) ?? 0;
1101
- const weight = (item) => (item.reason === orchestrator_1.SESSION_EXPAND_REASON ? exports.NEWEST_SESSION_STATEMENT_WEIGHT : 1);
1102
- const value = (item) => (weight(item) * said(item) * said(item)) / Math.max(1, cost(item));
1103
- // `ordered`: the match first, then its other matches, then the statements that share two words of the question, as listed.
1104
- const pool = ordered
1105
- ? [recent, ...mates, ...sessionStatements.filter(statement => said(statement) >= exports.STATEMENT_RELEVANCE_FLOOR)]
1106
- : [recent, ...mates, ...sessionStatements].sort((a, b) => value(b) - value(a));
1107
- // A small match cannot buy a share of the budget in unmatched statements (PR #520): the statements together cost no more
1108
- // than the match itself. The match need not fit, so a large matching chunk can still reserve a short deciding statement.
1109
- const statementRoom = cost(memories[reserved]);
1110
- let statementSpent = 0;
1111
- let spent = 0;
1112
- let taken = 0;
1113
- let weakTaken = 0;
1114
- for (const piece of pool) {
1115
- if (taken >= (ordered ? Infinity : split.pieces))
1116
- break;
1117
- const statement = piece.reason === orchestrator_1.SESSION_EXPAND_REASON;
1118
- const weak = !ordered && statement && said(piece) < 2;
1119
- if (weak && weakTaken >= exports.NEWEST_SESSION_WEAK_STATEMENTS)
1120
- continue;
1121
- const i = memories.indexOf(piece);
1122
- if (ordered && piece === recent) { // the match itself is no part of the session's room
1123
- if (i >= 0 && !kept.has(i) && !repeats(i) && fits(piece, allowance))
1124
- kept.add(i);
1125
- continue;
1126
- }
1127
- if (i < 0 || kept.has(i) || repeats(i) || spent + cost(piece) > room || (statement && statementSpent + cost(piece) > statementRoom) || !fits(piece, allowance))
1128
- continue;
1129
- spent += cost(piece);
1130
- if (statement)
1131
- statementSpent += cost(piece);
1132
- taken++;
1133
- if (weak)
1134
- weakTaken++;
1135
- kept.add(i);
1136
- }
1137
- // Nothing of the session's matches was taken: the match itself is tried alone, as the reservation always did.
1138
- const sessionKept = [...kept].some(i => !added(i) && sessionOf(memories[i]) === sessionOf(memories[reserved]));
1139
- if (!sessionKept && !repeats(reserved) && fits(memories[reserved], allowance))
1140
- kept.add(reserved);
1141
- }
1142
- if (!keepFirst)
1143
- placeKeep();
1144
- // A top-ranked match too big for the whole allowance is passed over, and then every later match that fits is taken, in score
1145
- // order, as the top fact is cut (topFactCut). Before, one long note that topped a generic question (the report's "what does
1146
- // ember need": 1,100 words above an 80-token note) emptied the reply at every budget that could not hold it. A top match that
1147
- // fits the allowance is never passed over: while nothing is passed over, a later match that does not fit ends the run, so a
1148
- // budget that holds the top match keeps the best matches (cut by score), not the next ones that happen to be small. The top
1149
- // match is passed over only when it can never fit: a reply cut back from over budget must not swap it for a smaller one.
1150
- const ranked = matchBudgetOrder(memories);
1151
- const topMatch = ranked.find(i => !repeats(i) && !kept.has(i));
1152
- let passedOver = false;
1153
- for (const i of ranked) {
1154
- if (repeats(i) || kept.has(i))
1155
- continue;
1156
- if (fits(memories[i], allowance)) {
1157
- kept.add(i);
1158
- continue;
1159
- }
1160
- if (i === topMatch && cost(memories[i]) > allowance)
1161
- passedOver = true;
1162
- else if (!passedOver)
1163
- break;
1164
- }
1165
- while (keptFacts < top.length && fits(top[keptFacts], allowance))
1166
- list(factEvents[keptFacts++]);
1167
- // What the expansion added takes what the facts and the matches left, in the order it was laid out.
1168
- for (let i = 0; i < memories.length; i++) {
1169
- if (!added(i))
1170
- continue;
1171
- if (!fits(memories[i], allowance))
1172
- break;
1173
- kept.add(i);
1174
- }
1175
- const shownFacts = top.filter((fact, i) => i < keptFacts || fact === held);
1176
- if (closest !== undefined && (closest.fact === held || (keptFacts === top.length && fits(closest.fact, allowance)))) {
1177
- shownFacts.push(closest.fact);
1178
- list(closest.event);
1179
- }
1180
- const shown = [...kept].sort((a, b) => a - b);
1181
- return {
1182
- facts: shownFacts,
1183
- memories: shown.map(i => memories[i]),
1184
- truncated: topFactCut || shownFacts.length < top.length + (closest === undefined ? 0 : 1) || memories.some((_, i) => !added(i) && !kept.has(i) && !repeats(i)),
1185
- tokens: used,
1186
- listed,
1187
- };
1188
- }
1189
- /**
1190
- * Client-safe note path: never leak a host path through a product surface
1191
- * (the same rule the engine search tools follow). Paths inside the memory dir
1192
- * render relative to it; anything else falls back to the basename.
1193
- */
1194
- function clientSafePath(memoryDir, stored) {
1195
- const resolved = path_1.default.resolve(stored);
1196
- const root = path_1.default.resolve(memoryDir);
1197
- if (resolved === root || resolved.startsWith(root + path_1.default.sep)) {
1198
- return path_1.default.relative(root, resolved) || stored;
1199
- }
1200
- return path_1.default.basename(stored);
1201
- }
1202
- /**
1203
- * The most of max_tokens a correction listed for a replaced memory may take. A correction can be as long as a statement
1204
- * (4,000 characters), and a long item the budget cannot fit stops the run of matches behind it (applyBudget), so a
1205
- * correction the replacement brings into a reply is cut to this share, with the marker that says where to read the rest, as
1206
- * a long fact is.
1207
- */
1208
- const CORRECTION_SHARE = 0.3;
1209
- /**
1210
- * The correction the budget works with in place of each one a replaced memory has: it takes the best score of the memories it
1211
- * corrects, and as a match found only by its words it is ranked with the matches retrieval scored (reason "+replacement"),
1212
- * so a correction that says it in other words than the memory it replaces is not cut before that memory. Returns the
1213
- * corrections as they are budgeted, by the one they stand for, and the correction each replaced memory has.
1214
- */
1215
- function bindCorrections(pairs, maxTokens) {
1216
- const share = Math.max(32, Math.floor(maxTokens * CORRECTION_SHARE));
1217
- const bound = new Map();
1218
- for (const { replaced, correction } of pairs) {
1219
- let item = bound.get(correction);
1220
- if (!item) {
1221
- const wordsOnly = correction.reason.split('+').every(reason => WORD_MATCH_REASONS.includes(reason));
1222
- item = {
1223
- ...correction,
1224
- ...(replyTokens(correction.text) > share ? { text: capFactObject(correction.text, share) } : {}),
1225
- ...(wordsOnly ? { reason: `${correction.reason}+replacement` } : {}),
1226
- };
1227
- }
1228
- const score = Math.max(item.score ?? Number.NEGATIVE_INFINITY, replaced.score ?? Number.NEGATIVE_INFINITY);
1229
- bound.set(correction, Number.isFinite(score) ? { ...item, score } : item);
1230
- }
1231
- const correctionOf = new Map();
1232
- for (const { replaced, correction } of pairs)
1233
- correctionOf.set(replaced, bound.get(correction));
1234
- return { bound, correctionOf };
1235
- }
1236
- const sameMemory = (a, b) => a.notePath === b.notePath && (a.headingPath ?? '') === (b.headingPath ?? '') && a.text === b.text;
1237
- /**
1238
- * A reply never lists a replaced memory before its correction, nor without it: each is moved to follow the correction, and
1239
- * one whose correction the reply does not hold is left out (the reply says it cut something).
1240
- */
1241
- function pinToCorrections(reply, correctionOf) {
1242
- let listed = reply.memories;
1243
- const replacedOf = (memory) => memory.replaced === undefined ? undefined : [...correctionOf].find(([replaced]) => sameMemory(replaced, memory))?.[1];
1244
- let cut = false;
1245
- for (const memory of [...listed]) {
1246
- const correction = replacedOf(memory);
1247
- if (correction === undefined)
1248
- continue;
1249
- const at = listed.findIndex(other => other !== memory && sameMemory(other, correction));
1250
- const from = listed.indexOf(memory);
1251
- if (at < 0) {
1252
- listed = listed.filter(other => other !== memory);
1253
- cut = true;
1254
- }
1255
- else if (at > from) {
1256
- listed = [...listed.slice(0, from), ...listed.slice(from + 1, at + 1), memory, ...listed.slice(at + 1)];
1257
- }
1258
- }
1259
- reply.memories = listed;
1260
- if (cut)
1261
- reply.truncated = true;
1262
- }
1263
- /**
1264
- * What the semantic search's notices (mvp/engine.ts: a failed search, a rebuild) say keyword search still does, and what
1265
- * get_context says instead when its keyword index is missing too: the reply never claims it in one sentence and denies
1266
- * it in the next.
1267
- */
1268
- const KEYWORD_STILL_ANSWERS = [
1269
- ['the saved facts and keyword search do not need it and still answer.', 'the saved facts still answer.'],
1270
- ['Keyword search works meanwhile.', 'The saved facts still answer.'],
1271
- ];
1272
- async function buildAgentContext(db, engine, memoryDir, options = {}) {
1273
- const query = (options.query ?? '').trim();
1274
- const scopeNorm = options.scope === undefined ? undefined : (0, scope_1.normalizeScope)(options.scope);
1275
- const maxTokens = Math.min(Math.max(options.maxTokens ?? 800, 128), 4000);
1276
- let rankedBy;
1277
- const memories = [];
1278
- // What retrieval's session expansion added is no match; it is budgeted after every match (see applyBudget).
1279
- const added = [];
1280
- let unfinishedEvidence;
1281
- let semanticAvailable = false;
1282
- let keywordAvailable = false;
1283
- let notice;
1284
- // A search that could not run (not one that found nothing), named so an empty reply is never mistaken for an empty store.
1285
- const degraded = [];
1286
- let keywordFailed = false;
1287
- const probe = {};
1288
- if (query) {
1289
- // Preserve the uncapped get_context default; an explicit environment cap still wins.
1290
- const semantic = await (0, engine_1.engineSearchMemory)(db, engine, memoryDir, query, 8, scopeNorm, options.evidence, options.includeHistory === true, options.ledgerRelPath, probe, undefined, 0);
1291
- semanticAvailable = semantic.ok;
1292
- notice = semantic.notice; // semantic search is off while the embeddings are rebuilt, or the provider failed it: say so
1293
- if (semantic.error === 'semantic-unavailable' && semantic.notice)
1294
- degraded.push('semantic');
1295
- if (semantic.ok) {
1296
- rankedBy = semantic.rankedBy;
1297
- for (const hit of semantic.hits) {
1298
- const normalized = normalizeMemory(hit, 'semantic', options.reader);
1299
- if (normalized)
1300
- (normalized.reason === orchestrator_1.SESSION_EXPAND_REASON ? added : memories).push(normalized);
1301
- }
1302
- }
1303
- // Bounded-wait evidence (RENOR_EVIDENCE_ASYNC) can answer before the table exists. get_context then says so in the
1304
- // words search_memory uses; with the flag off neither reason occurs and the response is what it was.
1305
- if (semantic.evidence?.reason === 'pending' || semantic.evidence?.reason === 'queue-full' || semantic.evidence?.reason === 'daily-cap') {
1306
- const { state, note } = (0, attach_1.productEvidenceReport)(semantic.evidence);
1307
- unfinishedEvidence = { state, note };
1308
- }
1309
- // The variants find the same chunks of the ledger: each chunk is parsed once for this read.
1310
- const ledgerRecords = new Map();
1311
- (0, readCache_1.inReadSnapshot)(db, () => {
1312
- const orAll = keywordOrAll();
1313
- for (const variant of orAll ? [query.trim()] : keywordVariants(query)) {
1314
- const keyword = (0, engine_1.engineSearchText)(db, memoryDir, variant, 6, scopeNorm, options.includeHistory === true, options.ledgerRelPath, ledgerRecords, orAll ? keywordOrMatch(query) : undefined);
1315
- keywordAvailable = keywordAvailable || keyword.ok;
1316
- if (!keyword.ok) {
1317
- keywordFailed = true;
1318
- continue;
1319
- }
1320
- for (const hit of keyword.results) {
1321
- const normalized = normalizeMemory(hit, 'fts', options.reader);
1322
- if (normalized)
1323
- memories.push(normalized);
1324
- }
1325
- }
1326
- });
1327
- if (keywordFailed && !keywordAvailable) {
1328
- degraded.push('keyword');
1329
- const semanticNotice = KEYWORD_STILL_ANSWERS.reduce((text, [claim, instead]) => text.replace(claim, instead), notice ?? '');
1330
- notice = `${semanticNotice ? `${semanticNotice} ` : ''}Keyword search is unavailable: its index is missing, so nothing here was found by words.`;
1331
- }
1332
- }
1333
- // A permission question also looks for the standing facts by meaning, through the embedding path the search above
1334
- // used (and its query vector, when it ran), when that path is the calibrated (Vertex) model; otherwise, or when it
1335
- // fails, by words alone (permissionMeaning).
1336
- const meaning = query ? await permissionMeaning(db, engine, query, scopeNorm, probe.queryVector) : undefined;
1337
- // Status facts ("PR 648 is not merged") past their freshness window are labelled, or left out (RENOR_STATUS_FRESHNESS, off by default).
1338
- const freshness = (0, statusFacts_1.statusFreshnessMode)(options.env);
1339
- const readAt = Date.now();
1340
- const staleOf = (fact) => freshness === 'off' ? null : (0, statusFacts_1.staleStatusOf)(fact, readAt);
1341
- const facts = (0, readCache_1.inReadSnapshot)(db, () => currentFactMatches(db, query, scopeNorm, query ? 12 : 20, meaning))
1342
- .filter(fact => freshness !== 'hide' || staleOf(fact) === null);
1343
- (0, callPhases_1.markRetrieved)(); // the matches and the facts are in hand: what follows builds the reply (callPhases.ts)
1344
- // After the keyword matches, so a statement a keyword search also found is that match, not the added copy.
1345
- const isLedger = (0, ledgerPresentation_1.ledgerNoteMatcher)({ memoryDir, ledgerRelPath: options.ledgerRelPath ?? lifecycle_1.DEFAULT_LEDGER_REL });
1346
- const deduplicated = dedupeMemories([...memories, ...added]);
1347
- // RENOR_CONTEXT_DISTINCT (off by default): a memory that repeats the reply's facts or another memory is not listed.
1348
- const merged = (0, contextDistinct_1.contextDistinctEnabled)(options.env) ? (0, contextDistinct_1.distinctMemories)(deduplicated, facts, isLedger) : deduplicated;
1349
- // A correction takes the best score of the memories it corrects and is cut to a share of the budget (`bindCorrections`), and
1350
- // the reply keeps them in pairs: a replaced memory is never listed without its correction, nor before it, whatever the
1351
- // budget cut and however the layout sorts (`pinToCorrections`, and `pinned` below). Awaited only when the workspace has a
1352
- // replacement: without one the assembly below stays synchronous, as it always was.
1353
- const pairs = [];
1354
- const arranged = (0, replacements_1.hasReplacements)(memoryDir)
1355
- ? await (0, replacements_1.withReplacements)({ db, memoryDir, ledgerRelPath: options.ledgerRelPath ?? lifecycle_1.DEFAULT_LEDGER_REL, indexOptions: {} }, merged, { pairs })
1356
- : merged;
1357
- const { bound, correctionOf: correctionOfRaw } = pairs.length > 0 ? bindCorrections(pairs, maxTokens) : { bound: new Map(), correctionOf: new Map() };
1358
- const deduped = arranged.map(memory => bound.get(memory) ?? memory);
1359
- const hasMemories = deduped.some(m => m.reason !== orchestrator_1.SESSION_EXPAND_REASON);
1360
- // Relevance signal (RENOR_CONTEXT_RELEVANCE): absent when off. Judged before the budget, and the response holds the
1361
- // match it reports. A fact close in meaning that the words of the request did not find ("Where am I based?" for
1362
- // home_city) stands in when they found none; otherwise it follows the facts they found, in the room left, unless it is
1363
- // that match: then, like a note chunk that is, the budget makes room for it.
1364
- const judged = query && (0, contextRelevance_1.contextRelevanceEnabled)(options.env)
1365
- ? (0, readCache_1.inReadSnapshot)(db, () => (0, contextRelevance_1.assessQueryRelevance)({
1366
- db, engine, scope: scopeNorm, probe, env: options.env, components: options.relevanceComponents,
1367
- query, onFile: nameOnFile(db, memoryDir, { scope: scopeNorm, includeHistory: options.includeHistory === true, ledgerRelPath: options.ledgerRelPath }),
1368
- }))
1369
- : null;
1370
- const closestFact = judged?.closestFact;
1371
- const shownFacts = facts.length === 0 && closestFact ? [closestFact] : facts;
1372
- // The values another writer replaced that stay open against a shown fact (memory/writerConflicts.ts): a fact with one
1373
- // carries its line, and a fact with none is shaped as it always was.
1374
- const disputes = (0, writerConflicts_1.writerConflictLines)(db, shownFacts.map(fact => fact.eventId));
1375
- const shaped = (fact) => {
1376
- const base = toAgentFact(fact, options.reader);
1377
- const line = fact.eventId === null ? undefined : disputes.get(fact.eventId);
1378
- const stale = staleOf(fact);
1379
- const marked = stale === null ? base : { ...base, staleStatus: stale };
1380
- return line === undefined ? marked : { ...marked, writerConflict: line };
1381
- };
1382
- const factLaneBudget = hasMemories ? Math.floor(maxTokens * budgetSplit().facts) : maxTokens;
1383
- const maxFactObjectTokens = Math.max(32, Math.floor(factLaneBudget * exports.MAX_FACT_OBJECT_LANE_SHARE));
1384
- // The cap is a crowding rule: it applies only when the facts would not otherwise fit the lane, and never to the
1385
- // top-ranked fact — a long fact that is itself the answer is delivered whole when the lane has room for it.
1386
- const factCost = (fact) => replyTokens(JSON.stringify(shaped(fact))) + 1;
1387
- const crowding = shownFacts.reduce((sum, fact) => sum + factCost(fact), 0) > factLaneBudget;
1388
- let factObjectCut = false;
1389
- const capFact = (fact, index) => {
1390
- const object = fact.object ?? '';
1391
- // A standing rule the permission lookup put first leads the page, but it is not necessarily the answer to the
1392
- // action: a long one must not eat the lane and displace the fact the question's words found. So it is capped like
1393
- // any non-top fact when another fact shares the page (review P3, 2026-10-09); a lone governance fact is whole.
1394
- const leads = index === 0 && shownFacts.length > 1 && STANDING.has(fact);
1395
- if ((crowding && index > 0 || leads) && replyTokens(object) > maxFactObjectTokens) {
1396
- factObjectCut = true;
1397
- return { ...shaped(fact), object: capFactObject(object, maxFactObjectTokens) };
1398
- }
1399
- return shaped(fact);
1400
- };
1401
- // The closest fact is the top-ranked one when the words found none (shownFacts is [closestFact]); otherwise it
1402
- // follows the facts they found and is capped with them when the lane is crowded.
1403
- const closestIndex = closestFact === undefined ? -1 : shownFacts.indexOf(closestFact);
1404
- const closest = closestFact ? capFact(closestFact, closestIndex >= 0 ? closestIndex : shownFacts.length) : undefined;
1405
- const isClosest = (fact) => closest !== undefined && fact.subject === closest.subject
1406
- && fact.predicate === closest.predicate && fact.scope === closest.scope && fact.validFrom === closest.validFrom
1407
- && objectIs(closest.object, fact.object);
1408
- // A memory of the ledger names no note and no heading (retrieval/ledgerPresentation.ts); the event in its heading is
1409
- // read first, so that a memory that is the event of a listed fact is not listed again. Its heading alone: its text is
1410
- // the user's words now, which may hold a line that looks like a record's heading.
1411
- const candidateFacts = shownFacts.map((fact, index) => capFact(fact, index));
1412
- const factEvents = shownFacts.map(fact => fact.eventId?.toLowerCase() ?? null);
1413
- // The agent-facing text: hidden spans labelled (hiddenSpans.ts). It is taken after every judgment above, which compares
1414
- // the raw text (isTopHit), and before the budget, which counts what the reply will carry.
1415
- // A ledger event that is a saved fact says who saved it, as get_context's facts do: its author is read from the fact.
1416
- const eventAuthors = (0, factMarkers_1.factAuthorsByEvent)(db, deduped.flatMap(memory => (isLedger(memory.notePath) ? (0, ledgerLiveness_1.ledgerEventIds)(memory) : [])));
1417
- const candidateMemories = deduped.map(memory => {
1418
- if (!isLedger(memory.notePath))
1419
- return (0, hiddenSpans_1.labelMemoryText)(memory);
1420
- const plain = normalizeMemory({ ...memory, notePath: '', headingPath: '' }, memory.reason);
1421
- return (0, hiddenSpans_1.labelMemoryText)({ ...plain, ...(0, factMarkers_1.eventMarker)((0, ledgerLiveness_1.ledgerEventIds)(memory), eventAuthors, options.reader) });
1422
- });
1423
- // The pairs were made on the memories as retrieved; the budget and the reply hold the labelled copies, so the pairs are
1424
- // re-keyed to those (a memory with hidden text is a new object with different text).
1425
- const listedAs = new Map(deduped.map((memory, i) => [memory, candidateMemories[i]]));
1426
- const correctionOf = new Map([...correctionOfRaw].map(([replaced, correction]) => [listedAs.get(replaced) ?? replaced, listedAs.get(correction) ?? correction]));
1427
- const memoryEvents = deduped.map(memory => (isLedger(memory.notePath) ? (0, ledgerLiveness_1.ledgerEventIds)({ text: '', headingPath: memory.headingPath }) : []));
1428
- const missed = closest !== undefined && !candidateFacts.some(isClosest)
1429
- ? { fact: closest, event: closestFact?.eventId?.toLowerCase() ?? null }
1430
- : undefined;
1431
- // The match the verdict reports: the closest fact, or the note chunk the semantic search scored best. The probe records
1432
- // that chunk with the paths the memories have before the ledger's are taken off, so it is found among those.
1433
- const topMemory = judged?.matchedOn === 'chunk' ? deduped.findIndex(memory => isTopHit(probe.topHit, memory)) : -1;
1434
- const match = judged?.matchedOn === 'fact'
1435
- ? candidateFacts.find(isClosest) ?? missed?.fact
1436
- : topMemory >= 0
1437
- ? candidateMemories[topMemory]
1438
- : undefined;
1439
- // The newest match above the relevance bar keeps its place in the budget (applyBudget `recent`), where the layout is by
1440
- // date (a read about the past is laid out by score and asks for the older statement) and the model has a bar to judge by,
1441
- // and so does the rest of its session that is above the bar, in NEWEST_SESSION_SHARE of the budget (`mates`), and so do the statements
1442
- // its expansion added to that session (`sessionStatementsFor` ranks them): the session said the deciding thing somewhere
1443
- // else, and a statement is not scored, so the words of the question rank it.
1444
- const bar = (0, contextRelevance_1.resolveRelevanceBar)(engine.modelKey, options.env);
1445
- const newest = rankedBy === 'session_date' && bar !== null ? newestMatchAbove(deduped, probe.similarities ?? [], bar, query) : -1;
1446
- const recent = newest >= 0 ? candidateMemories[newest] : undefined;
1447
- const mates = bar === null ? [] : sessionMatesAbove(deduped, probe.similarities ?? [], bar, newest).map(i => candidateMemories[i]);
1448
- const sessionStatements = sessionStatementsFor(deduped, newest, query, exports.NEWEST_SESSION_STATEMENT_FLOOR).map(i => candidateMemories[i]);
1449
- // Whether the reply `kept` holds it: listed, or a memory that is the event of a fact it lists (that one is said once).
1450
- // A fact the budget truncated to a prefix is still the match it was: same key, and its object is the prefix of the
1451
- // candidate's (`objectIs` reads that direction), so the verdict a truncated top fact answers is still reported.
1452
- const holds = (kept) => match !== undefined && (kept.facts.some(f => f === match || (f.subject === match.subject
1453
- && f.predicate === match.predicate
1454
- && f.scope === match.scope
1455
- && f.validFrom === match.validFrom
1456
- && objectIs(f.object, match.object)))
1457
- || kept.memories.includes(match)
1458
- || (topMemory >= 0 && match === candidateMemories[topMemory] && memoryEvents[topMemory].length > 0
1459
- && memoryEvents[topMemory].every(id => kept.listed.has(id))));
1460
- // The verdict stands when the reply holds its match, or, for a note chunk, the newest match above the same bar that the
1461
- // budget reserved in its place (`recent`): the reply then holds a match as relevant as the one the verdict judged, and the
1462
- // verdict reports no more than that (`relevant`, the closest cosine and the bar). A fact the verdict names stays strict.
1463
- const stands = (kept) => holds(kept) || (topMemory >= 0 && recent !== undefined
1464
- && (kept.memories.includes(recent) || (memoryEvents[newest].length > 0 && memoryEvents[newest].every(id => kept.listed.has(id)))));
1465
- const retrieval = !query
1466
- ? 'facts'
1467
- : semanticAvailable && keywordAvailable
1468
- ? 'hybrid'
1469
- : semanticAvailable
1470
- ? 'semantic'
1471
- : keywordAvailable
1472
- ? 'keyword'
1473
- : 'facts';
1474
- const trailer = (options.trailer && (0, readCache_1.inReadSnapshot)(db, options.trailer)) ?? {};
1475
- // The facts this read could list: the ones its words found, and the closest one when they did not find it.
1476
- const named = missed ? [...candidateFacts, missed.fact] : candidateFacts;
1477
- // The opt-in read-honesty lane (LANE 3, RENOR_READ_HONESTY, off by default), set up once per read when it is armed.
1478
- const honestyLane = (0, readHonesty_1.readHonestyEnabled)(options.env) && Boolean(query);
1479
- // Lean memory items (RENOR_CONTEXT_LEAN_ITEMS, off by default): each memory is costed, and the reply measured, as it is
1480
- // listed (leanMemory), so the same max_tokens holds more of them. The read-honesty lane lists provenance on every item,
1481
- // so it keeps its own items.
1482
- const lean = !honestyLane && contextLeanItemsEnabled(options.env);
1483
- const listedTokens = (body) => replyTokens(JSON.stringify(lean ? { ...body, memories: body.memories.map(leanMemory) } : body));
1484
- const ledgerRel = options.ledgerRelPath ?? lifecycle_1.DEFAULT_LEDGER_REL;
1485
- // Each item the lane lists says where it came from and when it was last verified. A fact get_context lists is a
1486
- // lifecycle fact (memory/factTerms.ts reads `source_path = '_mcp_'` only), so the ledger is its source.
1487
- const factWithProvenance = (f) => ({ ...f, provenance: (0, readHonesty_1.factProvenance)(f.validFrom, ledgerRel) });
1488
- const memoryWithProvenance = (m) => ({
1489
- ...m,
1490
- provenance: (0, readHonesty_1.noteProvenance)(m.notePath, m.session_date ?? null),
1491
- });
1492
- // Conflicts are about the product's OWN stored facts, so their source paths are rendered client-safe too: a product
1493
- // surface never leaks a host path (the same rule engineSearchMemory follows). A conflict across scopes is built again
1494
- // from its rendered sides.
1495
- const safe = (side) => ({ ...side, source: clientSafePath(memoryDir, side.source) });
1496
- const clientSafe = (c) => ((0, readHonesty_1.acrossScopes)(c)
1497
- ? (0, readHonesty_1.conflictAcrossScopes)(c.subject, c.predicate, [c.newer, c.older, ...(c.alsoCurrent ?? [])].map(safe), c.total)
1498
- : {
1499
- ...c,
1500
- newer: { ...c.newer, source: clientSafePath(memoryDir, c.newer.source) },
1501
- older: { ...c.older, source: clientSafePath(memoryDir, c.older.source) },
1502
- statement: c.statement
1503
- .split(c.newer.source).join(clientSafePath(memoryDir, c.newer.source))
1504
- .split(c.older.source).join(clientSafePath(memoryDir, c.older.source)),
1505
- });
1506
- // The lane's contradictions for a reply listing `listed`: about what those facts say, in any scope the read sees, and
1507
- // inside one scope about their subjects and the subjects the query names. Read once per list.
1508
- let lastRead;
1509
- const contradictions = (listed) => {
1510
- if (lastRead?.listed !== listed) {
1511
- const { conflicts } = (0, readHonesty_1.applyReadHonesty)({
1512
- db, query, ledgerRelPath: ledgerRel, scope: scopeNorm, subjects: listed.map(f => f.subject), facts: listed, texts: [], env: options.env,
1513
- });
1514
- lastRead = { listed, found: conflicts.map(clientSafe) };
1515
- }
1516
- return lastRead.found;
1517
- };
1518
- const about = (fact, conflict) => (0, readHonesty_1.questionKey)(fact.subject, fact.predicate) === (0, readHonesty_1.questionKey)(conflict.subject, conflict.predicate);
1519
- // What the lane judges is the read, not the reply: everything it could list, the facts it found and the memories a
1520
- // reply listing all of them would list. "No good answer on file for this" is a claim about the store, so it is the
1521
- // same at every max_tokens: a reply the budget cut down to one fact, or to none, does not get to make it while the
1522
- // store covers the question, and a contradiction the read could name stands it down, as in applyReadHonesty.
1523
- const judgedRead = honestyLane ? (() => {
1524
- const factEventIds = new Set([...factEvents, missed?.event ?? null].filter((id) => id !== null));
1525
- const listable = candidateMemories.filter((_, i) => !(memoryEvents[i].length > 0 && memoryEvents[i].every(id => factEventIds.has(id))));
1526
- return (0, readHonesty_1.applyReadHonesty)({
1527
- db,
1528
- query,
1529
- ledgerRelPath: ledgerRel,
1530
- scope: scopeNorm,
1531
- subjects: named.map(f => f.subject),
1532
- facts: named,
1533
- texts: [...named.map(f => `${f.subject} ${f.predicate} ${f.object}`), ...listable.map(m => m.text)],
1534
- considered: named.length + listable.length,
1535
- env: options.env,
1536
- });
1537
- })() : undefined;
1538
- // A conflict-free superset cannot gain a conflict when facts are cut from it. Keep
1539
- // that proof only for this synchronous assembly and the same committed DB contents.
1540
- const conflictStamp = () => db.inTransaction ? null :
1541
- `${(0, statements_1.prepareOnce)(db, 'PRAGMA data_version').pluck().get()}:${(0, statements_1.prepareOnce)(db, 'SELECT total_changes()').pluck().get()}`;
1542
- let emptyConflictsAt = null;
1543
- // The reply for the facts and memories `kept`. `relevant: true` reports its closest match, so it stands only when the
1544
- // reply holds it (the match can be too big for max_tokens on its own); "not relevant" is about everything on file,
1545
- // whatever is shown.
1546
- const assemble = (kept, verdict = judged && (!judged.relevance.relevant || stands(kept)) ? judged.relevance : undefined) => {
1547
- const body = {
1548
- ok: true,
1549
- query: query || null,
1550
- scope: scopeNorm ?? 'all',
1551
- retrieval,
1552
- facts: kept.facts,
1553
- // The order of the semantic memories (keyword matches, reason "fts" or "note-identity", follow them). Named
1554
- // right above the list it describes: memories[0] is the newest match under "session_date", not the best one.
1555
- ...(rankedBy ? { rankedBy } : {}),
1556
- memories: kept.memories,
1557
- // A fact object the lane cut is a cut the reply says it made, whatever the budget did: the agent reads the
1558
- // rest of that fact with read_memory or lookup_entity (the object itself carries TRUNCATION_MARKER).
1559
- truncated: kept.truncated || factObjectCut,
1560
- maxTokens,
1561
- ...(notice ? { notice } : {}),
1562
- ...(degraded.length > 0 ? { degraded } : {}),
1563
- };
1564
- if (verdict)
1565
- body.relevance = verdict;
1566
- // Multi-agent drift (memory/factConflicts.ts): a returned fact whose predicate has another current value under the
1567
- // same subject in another scope, or under a related name, is named with those values. Absent when nothing
1568
- // disagrees; within the read's scope only, so a scoped read never learns what another scope holds. Counted in
1569
- // max_tokens with the fact that names it. The opt-in read-honesty lane below replaces it with its own `conflicts`
1570
- // when armed.
1571
- const conflicts = kept.facts.length > 0 && emptyConflictsAt !== null && emptyConflictsAt === conflictStamp()
1572
- ? [] : (0, factConflicts_1.namedConflicts)(db, kept.facts, scopeNorm ?? null, options.reader);
1573
- if (conflicts.length > 0)
1574
- body.conflicts = conflicts;
1575
- // LANE 3 read-time honesty (RENOR_READ_HONESTY, default OFF). When off, the
1576
- // response is byte-identical to the shipped contract — no new fields, no new
1577
- // queries. When on: every item says where it came from and when it was last
1578
- // verified, contradictions between current facts are named instead of
1579
- // silently returned as compatible, and a question the stored evidence does
1580
- // not cover gets an explicit "no good answer on file for this".
1581
- if (honestyLane) {
1582
- const listed = body.facts;
1583
- body.facts = listed.map(factWithProvenance);
1584
- body.memories = kept.memories.map(memoryWithProvenance);
1585
- body.provenance = {
1586
- ledger: ledgerRel,
1587
- note: 'lifecycle facts are ledger-backed; note items are sourced from the note path shown on each item.',
1588
- };
1589
- // A contradiction about what a listed fact says is in the reply with it, at its least (#331's cut, readHonesty.ts
1590
- // fitConflicts): the two values that disagree, and how many there are. The fact was charged that much (below), so
1591
- // the two are inside max_tokens together or neither is. The lane's other contradictions, and more sides of these,
1592
- // take the room the items leave (`withLane`, below).
1593
- body.conflicts = (0, readHonesty_1.fitConflicts)(contradictions(listed).filter(c => listed.some(f => about(f, c))), 0);
1594
- body.abstention = judgedRead.abstention;
1595
- }
1596
- if (unfinishedEvidence)
1597
- body.evidence = unfinishedEvidence;
1598
- return Object.assign(body, trailer);
1599
- };
1600
- // The whole reply is bounded by max_tokens, as the engine counts tokens (replyTokens): the echoed query, `relevance`,
1601
- // `conflicts` and every other field count, not the facts and memories alone (live dogfood, 2026-10-03, D9: 19 of 31
1602
- // replies were over, by up to 1.89 times). The items get what the rest of an empty reply leaves, a fact costing the
1603
- // conflicts it is the first to name as well (it brings them into the reply). The reply they make is then measured
1604
- // whole, and while it is over (by how separately counted pieces join), the items get that much less. Each round
1605
- // keeps fewer tokens of items, so it ends; a reply whose fixed part alone is over (an echoed query longer than the
1606
- // budget) holds no fact and no memory.
1607
- //
1608
- // The opt-in read-honesty lane (RENOR_READ_HONESTY, off by default) reads under the same bound, and what a budget cuts
1609
- // is the least of what it says. Its items are counted as it lists them, with their provenance. A fact is charged the
1610
- // contradiction about what it says at its least, the two values that disagree and how many there are, so the two are
1611
- // inside max_tokens together or neither is. What else the lane can add takes the room the items leave (`withLane`).
1612
- // A fact is charged the contradiction it names by its key (subject/predicate/scope), not its object: a fact the budget
1613
- // truncated to a prefix is a new object, and it must carry the same charge as the candidate it was cut from, so a kept
1614
- // fact and the contradiction it brings are still in the reply together or neither is.
1615
- const bringsKey = (item) => 'predicate' in item ? `${item.subject}\u0000${item.predicate}\u0000${item.scope ?? 'global'}` : '';
1616
- const brings = new Map();
1617
- if (!honestyLane) { // that lane names its own contradictions instead
1618
- const beforeConflicts = named.length > 0 ? conflictStamp() : null;
1619
- const allConflicts = (0, factConflicts_1.namedConflicts)(db, named, scopeNorm ?? null, options.reader);
1620
- if (allConflicts.length === 0)
1621
- emptyConflictsAt = beforeConflicts;
1622
- for (const conflict of allConflicts) {
1623
- const first = named.find(f => f.predicate === conflict.predicate
1624
- && conflict.values.some(v => v.subject === f.subject && objectIs(f.object, v.object) && v.scope === f.scope))
1625
- ?? named.find(f => f.predicate === conflict.predicate);
1626
- if (first)
1627
- brings.set(bringsKey(first), (brings.get(bringsKey(first)) ?? 0) + replyTokens(JSON.stringify(conflict)) + 1);
1628
- }
1629
- }
1630
- else {
1631
- // Over every fact the read could list, so the charge does not move as the list is cut; the first fact that says what
1632
- // a contradiction is about pays for it, as the reply renders it (and, like the lane's items, without a comma).
1633
- for (const conflict of judgedRead.conflicts.map(clientSafe)) {
1634
- const first = named.find(f => about(f, conflict));
1635
- if (first)
1636
- brings.set(bringsKey(first), (brings.get(bringsKey(first)) ?? 0) + replyTokens(JSON.stringify((0, readHonesty_1.fitConflicts)([conflict], 0)[0])));
1637
- }
1638
- }
1639
- const costs = new Map();
1640
- const cost = (item) => {
1641
- let size = costs.get(item);
1642
- if (size === undefined) {
1643
- // Counted alone and with its comma, an item is a token or two over what it adds to the reply (`},{"` is one token
1644
- // there): the items rarely add up to more than the reply they make, so a reply is rarely cut a second time. The
1645
- // read-honesty lane counts an item as it lists it, with its provenance, and without the comma: what it adds to the
1646
- // reply, so a fact and the contradiction it brings are listed from the least max_tokens that holds them.
1647
- size = honestyLane
1648
- ? replyTokens(JSON.stringify('predicate' in item ? factWithProvenance(item) : memoryWithProvenance(item))) + (brings.get(bringsKey(item)) ?? 0)
1649
- : replyTokens(JSON.stringify(lean && !('predicate' in item) ? leanMemory(item) : item)) + 1 + (brings.get(bringsKey(item)) ?? 0);
1650
- costs.set(item, size);
1651
- }
1652
- return size;
1653
- };
1654
- // With the lane armed, what the session expansion added (reason SESSION_EXPAND_REASON) is not budgeted with the items.
1655
- // The lane lists the whole reply when it fits, and its contradictions take the room the items leave; counted as items,
1656
- // the statements made too big a whole reply that fit without them (the cut that followed listed one fact of three), and
1657
- // took a contradiction's room. So the lane budgets what it budgets with the expansion off, and the statements take the
1658
- // room its whole reply leaves (`withLane`).
1659
- const statement = (memory) => honestyLane && candidateMemories[memory].reason === orchestrator_1.SESSION_EXPAND_REASON;
1660
- const itemMemories = candidateMemories.filter((_, i) => !statement(i));
1661
- const itemEvents = memoryEvents.filter((_, i) => !statement(i));
1662
- const statements = candidateMemories.filter((_, i) => statement(i)).map(memoryWithProvenance);
1663
- // The facts are cut from their end and the matches by score, but the match the verdict reports takes its room first when
1664
- // that would not list it, and so does the newest match above the relevance bar (`recent`) when it fits.
1665
- const budget = (allowance) => {
1666
- const place = (keep) => pinned(applyBudget(candidateFacts, factEvents, itemMemories, itemEvents, allowance, cost, { keep, closest: missed, recent, mates, sessionStatements, relevance: item => ('text' in item ? statementRelevance(item.text, query) : 0) }), allowance);
1667
- const plain = place();
1668
- return match !== undefined && !holds(plain) ? place(match) : plain;
1669
- };
1670
- // A replaced memory the budget kept has its correction kept with it: the correction is added when the allowance holds it, and
1671
- // when it does not the replaced memory is the one left out (the reply says it cut something).
1672
- const pinned = (kept, allowance) => {
1673
- if (correctionOf.size === 0)
1674
- return kept;
1675
- let memories = kept.memories;
1676
- let tokens = kept.tokens;
1677
- let truncated = kept.truncated;
1678
- for (const memory of kept.memories) {
1679
- const correction = correctionOf.get(memory);
1680
- if (correction === undefined || memories.includes(correction))
1681
- continue;
1682
- if (tokens + cost(correction) <= allowance) {
1683
- memories = [...memories, correction];
1684
- tokens += cost(correction);
1685
- }
1686
- else {
1687
- memories = memories.filter(other => other !== memory);
1688
- tokens -= cost(memory);
1689
- truncated = true;
1690
- }
1691
- }
1692
- if (memories === kept.memories)
1693
- return kept;
1694
- return { ...kept, memories: itemMemories.filter(memory => memories.includes(memory)), tokens, truncated };
1695
- };
1696
- // What the read-honesty lane adds in the room the items leave, measured whole, so it never costs the reply an item: its
1697
- // contradictions that no listed fact brings (inside one scope, about a subject the query names), at their least and in
1698
- // their order while the reply holds them, cut from their end like any list; then more sides of each contradiction
1699
- // across scopes, first contradiction first, which `total` counts while they are not named (readHonesty.ts
1700
- // fitConflicts). A contradiction the reply cannot hold is not named, and the reply says it was cut.
1701
- const withLane = (kept, body) => {
1702
- const fits = (reply) => replyTokens(JSON.stringify(reply)) <= maxTokens;
1703
- const found = contradictions(kept.facts);
1704
- const least = (0, readHonesty_1.fitConflicts)(found, 0);
1705
- const take = found.map(c => kept.facts.some(f => about(f, c))); // in the reply already: their facts paid for them
1706
- for (let i = 0; i < found.length; i++) {
1707
- if (take[i])
1708
- continue;
1709
- take[i] = true;
1710
- if (!fits({ ...body, conflicts: least.filter((_, j) => take[j]) })) {
1711
- take[i] = false;
1712
- break;
1713
- }
1714
- }
1715
- const shown = (0, readHonesty_1.fitConflicts)(found.filter((_, i) => take[i]), conflicts => fits({ ...body, conflicts }));
1716
- const reply = { ...body, conflicts: shown };
1717
- if (shown.length < found.length)
1718
- reply.truncated = true;
1719
- // Then what the session expansion added, after every match in the order it was laid out, while the whole reply holds
1720
- // it: the room the reply leaves with the expansion off. As in the default lane (applyBudget), `truncated` does not
1721
- // count it.
1722
- for (const memory of statements) {
1723
- const memories = [...reply.memories, memory];
1724
- if (!fits({ ...reply, memories }))
1725
- break;
1726
- reply.memories = memories;
1727
- }
1728
- if (kept.facts.length > 0 || kept.memories.length > 0)
1729
- return reply;
1730
- // A reply that lists no item can be over all the same: its fixed part alone, for a read with nothing on file or a
1731
- // budget too small for any item. It drops what the lane adds, the least of it first, until it fits: the provenance
1732
- // legend, which explains items it does not list; how the abstention was reached (the terms it looked for, then its
1733
- // coverage, bar and count); a list of contradictions that names none; and last the abstention itself, which a budget
1734
- // that cannot hold it does not carry, as it does not carry a contradiction, and the reply says it was cut. What is
1735
- // left is the reply with the lane off, over only where that one is (an echoed query longer than the budget).
1736
- const abstention = reply.abstention;
1737
- const cuts = [
1738
- () => { delete reply.provenance; },
1739
- () => {
1740
- if (!abstention)
1741
- return;
1742
- const { terms: _terms, ...reached } = abstention;
1743
- reply.abstention = reached;
1744
- },
1745
- () => { if (abstention)
1746
- reply.abstention = { status: abstention.status, reason: abstention.reason, statement: abstention.statement }; },
1747
- () => { if (shown.length === 0)
1748
- delete reply.conflicts; },
1749
- () => {
1750
- delete reply.abstention;
1751
- if (abstention)
1752
- reply.truncated = true;
1753
- },
1754
- ];
1755
- for (const cut of cuts) {
1756
- if (fits(reply))
1757
- break;
1758
- cut();
1759
- }
1760
- return reply;
1761
- };
1762
- const fixed = () => budget(maxTokens - listedTokens(assemble({ facts: [], memories: [], truncated: false, tokens: 0, listed: new Set() }, judged?.relevance)));
1763
- // With the lane armed, the reply that holds everything the read found is the reply whenever it fits: the budget cuts
1764
- // each list in turn, so short of that it can keep a fact's ledger event where the fact would take its place.
1765
- const whole = honestyLane ? budget(Number.POSITIVE_INFINITY) : undefined;
1766
- let kept = whole ?? fixed();
1767
- let body = assemble(kept);
1768
- if (whole && replyTokens(JSON.stringify(body)) > maxTokens) {
1769
- kept = fixed();
1770
- body = assemble(kept);
1771
- }
1772
- for (let over = listedTokens(body) - maxTokens; over > 0 && kept.tokens > 0; over = listedTokens(body) - maxTokens) {
1773
- kept = budget(kept.tokens - over);
1774
- body = assemble(kept);
1775
- }
1776
- const reply = honestyLane ? withLane(kept, body) : body;
1777
- if (rankedBy === 'session_date') {
1778
- // Selection is finished, including the honesty lane's extra statements. Render those statements with their
1779
- // session: appending a newest update after an older match contradicts the order the reply promises. Summary
1780
- // tables stay outside ranking, and word-only matches follow the dated lane in their original order.
1781
- const listed = reply.memories;
1782
- const leading = (0, digests_1.leadingSummaryHits)(listed);
1783
- const dated = [];
1784
- const keywords = [];
1785
- for (const memory of listed.slice(leading)) {
1786
- (memory.reason.split('+').every(r => WORD_MATCH_REASONS.includes(r)) ? keywords : dated).push(memory);
1787
- }
1788
- reply.memories = [
1789
- ...listed.slice(0, leading),
1790
- ...(0, orchestrator_1.orderHits)(dated.map(item => ({ item, score: item.score ?? 0, session_date: item.session_date })), 'session_date').map(hit => hit.item),
1791
- ...keywords,
1792
- ];
1793
- }
1794
- if (correctionOf.size > 0)
1795
- pinToCorrections(reply, correctionOf);
1796
- // Last, once the order and the pairs are settled on the full items, which say how they were found.
1797
- if (lean)
1798
- reply.memories = reply.memories.map(leanMemory);
1799
- return reply;
1800
- }