renor-engine 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/README.md +21 -1
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/dist/BUILD.json +6 -0
  4. package/dist/cli.js +1656 -516
  5. package/package.json +16 -80
  6. package/dist/.tsbuildinfo +0 -1
  7. package/dist/api/apiAuth.js +0 -43
  8. package/dist/api/server.js +0 -174
  9. package/dist/buildIdentity.js +0 -149
  10. package/dist/callPhases.js +0 -82
  11. package/dist/cli-extractors.js +0 -46
  12. package/dist/config.js +0 -65
  13. package/dist/context/composer.js +0 -320
  14. package/dist/context/readHonesty.js +0 -478
  15. package/dist/context/rowStore.js +0 -238
  16. package/dist/conversation/formatter.js +0 -81
  17. package/dist/conversation/parser.js +0 -193
  18. package/dist/conversation/types.js +0 -2
  19. package/dist/conversation/writer.js +0 -42
  20. package/dist/db/client.js +0 -149
  21. package/dist/db/handoffTable.js +0 -31
  22. package/dist/db/indexSalvage.js +0 -101
  23. package/dist/db/migrations.js +0 -670
  24. package/dist/db/readCache.js +0 -109
  25. package/dist/db/schema.js +0 -5
  26. package/dist/db/sqlite.js +0 -396
  27. package/dist/db/statements.js +0 -89
  28. package/dist/digests/digestCache.js +0 -146
  29. package/dist/digests/digests.js +0 -597
  30. package/dist/digests/worker.js +0 -350
  31. package/dist/embeddings/caching.js +0 -337
  32. package/dist/embeddings/coalescing.js +0 -177
  33. package/dist/embeddings/interactive.js +0 -103
  34. package/dist/embeddings/local.js +0 -70
  35. package/dist/embeddings/openai.js +0 -106
  36. package/dist/embeddings/rebuild.js +0 -166
  37. package/dist/embeddings/simdScan.js +0 -260
  38. package/dist/embeddings/space.js +0 -21
  39. package/dist/embeddings/types.js +0 -14
  40. package/dist/embeddings/vectorIndex.js +0 -674
  41. package/dist/embeddings/vertex.js +0 -334
  42. package/dist/eval/artifacts.js +0 -42
  43. package/dist/eval/cacheReport.js +0 -146
  44. package/dist/eval/comparison.js +0 -182
  45. package/dist/eval/context700-candidate.js +0 -145
  46. package/dist/eval/evalCache.js +0 -568
  47. package/dist/eval/hashEmbedding.js +0 -35
  48. package/dist/eval/indexSnapshots.js +0 -549
  49. package/dist/eval/longmemeval.js +0 -56
  50. package/dist/eval/metrics.js +0 -181
  51. package/dist/eval/pageDump.js +0 -67
  52. package/dist/eval/renorbenchBaseline.js +0 -486
  53. package/dist/eval/renorbenchDocuments.js +0 -134
  54. package/dist/eval/renorbenchProduct.js +0 -182
  55. package/dist/eval/runLedger.js +0 -279
  56. package/dist/eval/runManifest.js +0 -485
  57. package/dist/eval/scoring.js +0 -197
  58. package/dist/eval/textTokens.js +0 -17
  59. package/dist/evidence/attach.js +0 -264
  60. package/dist/evidence/background.js +0 -147
  61. package/dist/evidence/evidenceCache.js +0 -182
  62. package/dist/evidence/gate.js +0 -37
  63. package/dist/evidence/keyPool.js +0 -85
  64. package/dist/evidence/ollamaChat.js +0 -84
  65. package/dist/evidence/optin.js +0 -165
  66. package/dist/evidence/options.js +0 -151
  67. package/dist/evidence/secondPass.js +0 -294
  68. package/dist/extraction/classifyCapture.js +0 -34
  69. package/dist/extraction/decision-model.js +0 -174
  70. package/dist/extraction/eventExtractor.js +0 -113
  71. package/dist/extraction/extractionClient.js +0 -122
  72. package/dist/extraction/factExtractor.js +0 -46
  73. package/dist/extraction/factsWritePath.js +0 -179
  74. package/dist/extraction/jevDecisionModel.js +0 -307
  75. package/dist/extraction/preferenceExtractor.js +0 -150
  76. package/dist/extraction/secretGuard.js +0 -180
  77. package/dist/extraction/secretScanner.js +0 -665
  78. package/dist/extraction/typedFacts.js +0 -224
  79. package/dist/extraction/writePathAdjudication.js +0 -110
  80. package/dist/importing/apply.js +0 -672
  81. package/dist/importing/bundle-write.js +0 -80
  82. package/dist/importing/cli.js +0 -408
  83. package/dist/importing/existing.js +0 -51
  84. package/dist/importing/facts.js +0 -121
  85. package/dist/importing/forgetImported.js +0 -79
  86. package/dist/importing/hosted.js +0 -467
  87. package/dist/importing/journalLog.js +0 -96
  88. package/dist/importing/markdown.js +0 -98
  89. package/dist/importing/plan.js +0 -201
  90. package/dist/importing/secretScreen.js +0 -113
  91. package/dist/importing/serialize.js +0 -188
  92. package/dist/importing/source.js +0 -463
  93. package/dist/importing/types.js +0 -6
  94. package/dist/indexer/chunker.js +0 -319
  95. package/dist/indexer/dailyEntries.js +0 -63
  96. package/dist/indexer/hasher.js +0 -10
  97. package/dist/indexer/laneRuns.js +0 -48
  98. package/dist/indexer/o200k.js +0 -481
  99. package/dist/indexer/outsideRoot.js +0 -77
  100. package/dist/indexer/parser.js +0 -170
  101. package/dist/indexer/pipeline.js +0 -988
  102. package/dist/indexer/staleness.js +0 -201
  103. package/dist/indexer/tokenizer.js +0 -77
  104. package/dist/indexer/walker.js +0 -170
  105. package/dist/learning/cli.js +0 -113
  106. package/dist/learning/schema.js +0 -144
  107. package/dist/learning/settings.js +0 -45
  108. package/dist/learning/store.js +0 -267
  109. package/dist/learning/tap.js +0 -401
  110. package/dist/mcp/hive.js +0 -88
  111. package/dist/mcp/requestErrors.js +0 -47
  112. package/dist/mcp/server.js +0 -426
  113. package/dist/memory/agentLease.js +0 -43
  114. package/dist/memory/confinedWrite.js +0 -315
  115. package/dist/memory/controls.js +0 -73
  116. package/dist/memory/dailyNote.js +0 -265
  117. package/dist/memory/dreamer/adjudicate.js +0 -215
  118. package/dist/memory/dreamer/apply.js +0 -120
  119. package/dist/memory/dreamer/budget.js +0 -64
  120. package/dist/memory/dreamer/candidates.js +0 -149
  121. package/dist/memory/dreamer/collection.js +0 -104
  122. package/dist/memory/dreamer/dreamer.js +0 -352
  123. package/dist/memory/dreamer/review.js +0 -111
  124. package/dist/memory/durableAppend.js +0 -57
  125. package/dist/memory/factConflicts.js +0 -205
  126. package/dist/memory/factTerms.js +0 -321
  127. package/dist/memory/handoff.js +0 -363
  128. package/dist/memory/hiveNameHeader.cjs +0 -10
  129. package/dist/memory/inventory.js +0 -55
  130. package/dist/memory/invisibleText.js +0 -20
  131. package/dist/memory/lifecycle.js +0 -2014
  132. package/dist/memory/limits.js +0 -13
  133. package/dist/memory/requests.js +0 -183
  134. package/dist/memory/sourceName.js +0 -276
  135. package/dist/memory/statusFacts.js +0 -61
  136. package/dist/memory/whatsNew.js +0 -158
  137. package/dist/memory/writer.js +0 -176
  138. package/dist/memory/writerConflicts.js +0 -85
  139. package/dist/memorymap/memoryMap.js +0 -1594
  140. package/dist/memorymap/replacedJournal.js +0 -58
  141. package/dist/memorymap/replacements.js +0 -410
  142. package/dist/mvp/activity.js +0 -86
  143. package/dist/mvp/agentMemory.js +0 -1803
  144. package/dist/mvp/answerCheck.js +0 -211
  145. package/dist/mvp/auth.js +0 -531
  146. package/dist/mvp/cli.js +0 -86
  147. package/dist/mvp/client-store.js +0 -462
  148. package/dist/mvp/compileCache.js +0 -242
  149. package/dist/mvp/compileSeed.js +0 -33
  150. package/dist/mvp/config.js +0 -292
  151. package/dist/mvp/connectionStatus.js +0 -80
  152. package/dist/mvp/contextDistinct.js +0 -53
  153. package/dist/mvp/contextProfile.js +0 -87
  154. package/dist/mvp/contextRelevance.js +0 -413
  155. package/dist/mvp/directorySurface.js +0 -68
  156. package/dist/mvp/engine.js +0 -1078
  157. package/dist/mvp/exportAll.js +0 -393
  158. package/dist/mvp/factLanePack.js +0 -77
  159. package/dist/mvp/factMarkers.js +0 -65
  160. package/dist/mvp/factsLane.js +0 -203
  161. package/dist/mvp/hiddenSpans.js +0 -585
  162. package/dist/mvp/history.js +0 -414
  163. package/dist/mvp/hitDates.js +0 -31
  164. package/dist/mvp/hitFloor.js +0 -36
  165. package/dist/mvp/identity.js +0 -18
  166. package/dist/mvp/idle.js +0 -113
  167. package/dist/mvp/log.js +0 -118
  168. package/dist/mvp/memoryDir.js +0 -98
  169. package/dist/mvp/posthogMcp.js +0 -210
  170. package/dist/mvp/readScope.js +0 -104
  171. package/dist/mvp/readUsefulness.js +0 -222
  172. package/dist/mvp/sanitize.js +0 -48
  173. package/dist/mvp/schemaRejections.js +0 -91
  174. package/dist/mvp/scope.js +0 -127
  175. package/dist/mvp/server.js +0 -2712
  176. package/dist/mvp/serverCard.js +0 -38
  177. package/dist/mvp/storedSize.js +0 -139
  178. package/dist/mvp/telemetry.js +0 -474
  179. package/dist/net/poolFetch.js +0 -174
  180. package/dist/recall/actionRecall.js +0 -282
  181. package/dist/recall/autoRecall.js +0 -447
  182. package/dist/recall/searchRecall.js +0 -189
  183. package/dist/retrieval/adviceLines.js +0 -195
  184. package/dist/retrieval/adviceQuestion.js +0 -44
  185. package/dist/retrieval/conversationShape.js +0 -44
  186. package/dist/retrieval/dailyEntryTime.js +0 -55
  187. package/dist/retrieval/entityLookup.js +0 -162
  188. package/dist/retrieval/factsFirst.js +0 -188
  189. package/dist/retrieval/keyword.js +0 -84
  190. package/dist/retrieval/ledgerEventTime.js +0 -51
  191. package/dist/retrieval/ledgerLiveness.js +0 -83
  192. package/dist/retrieval/ledgerPresentation.js +0 -161
  193. package/dist/retrieval/noteIdentity.js +0 -57
  194. package/dist/retrieval/orchestrator.js +0 -1224
  195. package/dist/retrieval/plainWording.js +0 -236
  196. package/dist/retrieval/queryHelpers.js +0 -142
  197. package/dist/retrieval/questionSpan.js +0 -475
  198. package/dist/retrieval/rankingTrace.js +0 -184
  199. package/dist/retrieval/readerEvents.js +0 -690
  200. package/dist/retrieval/readerLayout.js +0 -422
  201. package/dist/retrieval/relativeTimeHints.js +0 -113
  202. package/dist/retrieval/semantic.js +0 -67
  203. package/dist/router/routerCache.js +0 -106
  204. package/dist/router/sessionRouter.js +0 -726
  205. package/dist/sharing/cli.js +0 -181
  206. package/dist/sharing/examples.js +0 -136
  207. package/dist/sharing/fields.js +0 -225
  208. package/dist/sharing/loop.js +0 -80
  209. package/dist/sharing/project.js +0 -327
  210. package/dist/sharing/send.js +0 -466
  211. package/dist/sharing/state.js +0 -222
  212. package/dist/sharing/wording.js +0 -48
  213. package/dist/toolRefusal.js +0 -120
  214. package/dist/watcher/watcher.js +0 -77
  215. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
@@ -1,222 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReadUsefulness = exports.DEFAULT_USE_WINDOW_MINUTES = void 0;
4
- exports.noteItem = noteItem;
5
- exports.factItem = factItem;
6
- exports.contextItems = contextItems;
7
- exports.searchItems = searchItems;
8
- exports.retrieveItems = retrieveItems;
9
- const node_crypto_1 = require("node:crypto");
10
- const statements_1 = require("../db/statements");
11
- const ledgerLiveness_1 = require("../retrieval/ledgerLiveness");
12
- const telemetry_1 = require("./telemetry");
13
- /** How long after a read a use still counts. Long enough for a multi-step task, short enough to stay one working session. */
14
- exports.DEFAULT_USE_WINDOW_MINUTES = 30;
15
- const MAX_WINDOW_MINUTES = 240;
16
- /** Reads kept to be matched, so a busy workspace cannot grow the tracker without bound. */
17
- const MAX_TRACKED_READS = 300;
18
- /** Ids an event lists; `items` still counts them all. */
19
- const MAX_EVENT_IDS = 20;
20
- const norm = (value) => value.normalize('NFKC').toLowerCase().replace(/\s+/g, ' ').trim();
21
- function noteItem(notePath) {
22
- return { key: `note:${notePath}` };
23
- }
24
- function factItem(subject, predicate) {
25
- return { key: `fact:${norm(subject)}\0${norm(predicate)}`, handles: [`subject:${norm(subject)}`] };
26
- }
27
- /** The note a memory id names (`note:<path>`, `entry:<path>#<hash>`), or the text itself when it names none. */
28
- function noteOfId(id) {
29
- if (id.startsWith('note:'))
30
- return id.slice(5);
31
- if (id.startsWith('entry:')) {
32
- const hashAt = id.lastIndexOf('#');
33
- return hashAt > 6 ? id.slice(6, hashAt) : id.slice(6);
34
- }
35
- return id;
36
- }
37
- function windowMs(env) {
38
- const parsed = Number((env.RENOR_READ_USE_WINDOW_MINUTES ?? '').trim());
39
- const minutes = Number.isFinite(parsed) && parsed >= 1 ? Math.min(MAX_WINDOW_MINUTES, Math.trunc(parsed)) : exports.DEFAULT_USE_WINDOW_MINUTES;
40
- return minutes * 60_000;
41
- }
42
- class ReadUsefulness {
43
- emit;
44
- now;
45
- reads = [];
46
- secret = (0, node_crypto_1.randomBytes)(32);
47
- windowMs;
48
- constructor(emit = telemetry_1.emitTelemetry, now = Date.now, env = process.env) {
49
- this.emit = emit;
50
- this.now = now;
51
- this.windowMs = windowMs(env);
52
- }
53
- opaque(key) {
54
- return (0, node_crypto_1.createHmac)('sha256', this.secret).update(key).digest('hex').slice(0, 8);
55
- }
56
- /** Builds what a reply served and records it. Building reads the database, so it is inside the guard too. */
57
- measure(tool, build) {
58
- try {
59
- const served = build();
60
- this.served(tool, served.items, served.relevant, served.judged);
61
- }
62
- catch {
63
- // Measuring must never break a read.
64
- }
65
- }
66
- /** A reply was served. `items` are its returned items; `relevant` and `judged` as described above. */
67
- served(tool, items, relevant, judged) {
68
- try {
69
- const at = this.now();
70
- const unique = new Map();
71
- for (const item of items)
72
- if (!unique.has(item.key))
73
- unique.set(item.key, item);
74
- const id = (0, node_crypto_1.randomBytes)(4).toString('hex');
75
- this.emit({
76
- event: 'read',
77
- tool,
78
- read: id,
79
- relevant,
80
- judged,
81
- items: unique.size,
82
- ids: [...unique.keys()].slice(0, MAX_EVENT_IDS).map(key => this.opaque(key)),
83
- });
84
- if (!relevant || unique.size === 0)
85
- return;
86
- this.prune(at);
87
- this.reads.push({
88
- id,
89
- tool,
90
- at,
91
- keys: new Set(unique.keys()),
92
- handles: new Set([...unique.values()].flatMap(item => item.handles ?? [])),
93
- used: false,
94
- });
95
- if (this.reads.length > MAX_TRACKED_READS)
96
- this.reads.splice(0, this.reads.length - MAX_TRACKED_READS);
97
- }
98
- catch {
99
- // Measuring must never break a read.
100
- }
101
- }
102
- /** The agent read one memory in full (read_memory). */
103
- readInFull(memoryId) {
104
- this.use('read', read => read.keys.has(`note:${noteOfId(memoryId)}`));
105
- }
106
- /** A save named what it builds on. Each cite is a notePath or memory id a read returned, or a fact's subject. */
107
- cited(cites) {
108
- if (!cites?.length)
109
- return;
110
- const wanted = cites.slice(0, 20).map(cite => cite.trim()).filter(Boolean);
111
- this.use('cite', read => wanted.some(cite => read.keys.has(`note:${noteOfId(cite)}`) || read.handles.has(`subject:${norm(cite)}`)));
112
- }
113
- /** A fact was saved with a new value for a subject and predicate (a correction or an update). */
114
- updated(subject, predicate) {
115
- const key = factItem(subject, predicate).key;
116
- this.use('update', read => read.keys.has(key));
117
- }
118
- use(via, matches) {
119
- try {
120
- const at = this.now();
121
- this.prune(at);
122
- for (const read of this.reads) {
123
- if (read.used || !matches(read))
124
- continue;
125
- read.used = true;
126
- this.emit({
127
- event: 'read_used',
128
- tool: read.tool,
129
- read: read.id,
130
- via,
131
- after_s: Math.max(0, Math.round((at - read.at) / 1000)),
132
- });
133
- }
134
- }
135
- catch {
136
- // Measuring must never break a save.
137
- }
138
- }
139
- prune(at) {
140
- const cutoff = at - this.windowMs;
141
- let drop = 0;
142
- while (drop < this.reads.length && this.reads[drop].at < cutoff)
143
- drop++;
144
- if (drop > 0)
145
- this.reads.splice(0, drop);
146
- }
147
- }
148
- exports.ReadUsefulness = ReadUsefulness;
149
- // ---- What each tool's reply serves ---------------------------------------------------------------------------------
150
- /** get_context: its facts by subject and predicate, its memories by note. A memory of a saved fact names no note. */
151
- function contextItems(body) {
152
- const items = [];
153
- for (const fact of Array.isArray(body.facts) ? body.facts : []) {
154
- if (typeof fact.subject === 'string' && typeof fact.predicate === 'string')
155
- items.push(factItem(fact.subject, fact.predicate));
156
- }
157
- for (const memory of Array.isArray(body.memories) ? body.memories : []) {
158
- if (typeof memory.notePath === 'string' && memory.notePath !== '')
159
- items.push(noteItem(memory.notePath));
160
- else if (typeof memory.text === 'string')
161
- items.push({ key: `text:${memory.text}` });
162
- }
163
- return verdictOf(body.relevance, items);
164
- }
165
- function verdictOf(relevance, items) {
166
- const judged = typeof relevance === 'object' && relevance !== null && typeof relevance.relevant === 'boolean';
167
- return { items, relevant: judged ? relevance.relevant : items.length > 0, judged };
168
- }
169
- /**
170
- * search_memory: its hits as they are before the ledger's markup is taken off, so a fact is named by what it claims. A hit
171
- * of the ledger names the facts of its events (the facts table knows each event's subject and predicate); anything else is
172
- * a note.
173
- */
174
- function searchItems(db, isLedger, hits, relevance) {
175
- const events = new Set();
176
- for (const hit of hits)
177
- if (isLedger(hit.notePath))
178
- for (const id of (0, ledgerLiveness_1.ledgerEventIds)(hit))
179
- events.add(id);
180
- const named = new Map();
181
- if (events.size > 0) {
182
- const ids = [...events];
183
- const rows = (0, statements_1.listStatement)(db, ids.length, marks => `SELECT lower(f.event_id) AS event, e.canonical_name AS subject, f.predicate AS predicate
184
- FROM facts f JOIN entities e ON e.id = f.subject_entity_id
185
- WHERE lower(f.event_id) IN (${marks})`).all(...(0, statements_1.paddedList)(ids));
186
- for (const row of rows)
187
- named.set(row.event, row);
188
- }
189
- const items = [];
190
- for (const hit of hits) {
191
- if (!isLedger(hit.notePath)) {
192
- items.push(hit.notePath ? noteItem(hit.notePath) : { key: `text:${hit.text}` });
193
- continue;
194
- }
195
- const facts = (0, ledgerLiveness_1.ledgerEventIds)(hit).map(id => named.get(id)).filter((fact) => fact !== undefined);
196
- if (facts.length > 0) {
197
- for (const fact of facts)
198
- items.push(factItem(fact.subject, fact.predicate));
199
- continue;
200
- }
201
- // A fact the index has not caught up with is headed `subject > predicate`.
202
- const [subject, predicate] = (hit.headingPath ?? '').split(' > ');
203
- items.push(subject && predicate ? factItem(subject, predicate) : { key: `text:${hit.text}` });
204
- }
205
- return verdictOf(relevance, items);
206
- }
207
- /** retrieve_memory: the facts of the subject it was asked for, by the subject the facts table names them under. */
208
- function retrieveItems(db, facts) {
209
- const ids = [...new Set(facts.map(f => f.subject_entity_id).filter((id) => typeof id === 'number'))];
210
- const names = new Map();
211
- if (ids.length > 0) {
212
- const rows = db.prepare(`SELECT id, canonical_name AS name FROM entities WHERE id IN (${ids.map(() => '?').join(',')})`)
213
- .all(...ids);
214
- for (const row of rows)
215
- names.set(row.id, row.name);
216
- }
217
- const items = facts.flatMap(f => {
218
- const subject = f.subject_entity_id === undefined ? undefined : names.get(f.subject_entity_id);
219
- return subject === undefined ? [] : [factItem(subject, f.predicate)];
220
- });
221
- return { items, relevant: items.length > 0, judged: false };
222
- }
@@ -1,48 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sanitizeErrorMessage = sanitizeErrorMessage;
4
- exports.escapeInvisibleControls = escapeInvisibleControls;
5
- /**
6
- * Privacy-safe error sanitizer: redacts host filesystem paths and bounds message length.
7
- * Preserves error descriptions, status codes, and syscalls without disclosing server directories.
8
- */
9
- function sanitizeErrorMessage(err) {
10
- let message = err instanceof Error ? err.message : String(err);
11
- if (err && typeof err === 'object' && 'path' in err && typeof err.path === 'string' && err.path.length > 0) {
12
- message = message.split(err.path).join('[redacted-path]');
13
- }
14
- // Windows absolute paths: C:\...
15
- const winPath = /(?:^|(?<=[\s(]))(['"`]?)[a-zA-Z]:\\(?:[^\s'"`:,;()\[\]\\]+\\)*[^\s'"`:,;()\[\]]*\1/g;
16
- // Unix absolute paths: starts with / and has at least two segments (e.g. /home/user/vault or /tmp/x)
17
- const unixPath = /(?:^|(?<=[\s(]))(['"`]?)\/(?:[^\s'"`:,;()\[\]/]+\/)+[^\s'"`:,;()\[\]]*\1/g;
18
- return message.replace(winPath, '[redacted-path]').replace(unixPath, '[redacted-path]');
19
- }
20
- /**
21
- * Characters JSON.stringify leaves literal although a reader may treat them as structure rather than
22
- * data: the C1 controls (U+0085 NEXT LINE among them), the Unicode format controls — bidirectional
23
- * overrides and zero-width characters a stored memory can use to reorder or hide text — and the line
24
- * and paragraph separators. A memory is untrusted text; emitted raw, a bidirectional override makes
25
- * the reply read backwards in an agent's transcript and a zero-width or line separator can hide an
26
- * instruction from a human reviewer while the model still tokenizes it.
27
- *
28
- * Escaping them to a plain `\uXXXX` escape on the wire changes no tool's contract: JSON.stringify
29
- * already escapes every C0 control, any JSON parser reads the same string back, so the decoded value
30
- * is byte-identical while the raw reply an agent or a log reads shows the hidden character as an
31
- * escape instead of applying it. Mirrors lifecycle.ts JSON_LEAVES_LITERAL, which does the same for the
32
- * ledger's statement field. Surrogate pairs (astral format characters) are escaped unit by unit.
33
- *
34
- * The categories above are not the whole set a reader cannot see. Variation selectors (Mn: U+FE00–FE0F and the
35
- * supplement U+E0100–E01EF, which spell out bytes an instruction can hide in) and blank fillers (Lo, So or Mn: the Hangul
36
- * fillers, the braille blank, the combining grapheme joiner) are invisible in a transcript yet pass the categories
37
- * above, so they are listed by code point (adv report renor-adv-inj6b-1012, F4). Emoji presentation selectors (U+FE0F
38
- * after a symbol) are escaped too: still lossless after a JSON parse, a few bytes longer in the raw reply.
39
- */
40
- const OUTPUT_INVISIBLE = new RegExp('[\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}'
41
- + '\\u034F\\u115F\\u1160\\u2800\\u3164\\uFE00-\\uFE0F\\uFFA0\\u{E0100}-\\u{E01EF}]', 'gu');
42
- /** A text of printable ASCII alone holds none of the characters above: found by a plain character class, not by the Unicode properties. */
43
- const PRINTABLE_ASCII = /^[\x20-\x7e]*$/;
44
- function escapeInvisibleControls(text) {
45
- if (PRINTABLE_ASCII.test(text))
46
- return text;
47
- return text.replace(OUTPUT_INVISIBLE, ch => Array.from({ length: ch.length }, (_, i) => `\\u${ch.charCodeAt(i).toString(16).padStart(4, '0')}`).join(''));
48
- }
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.countSchemaRejections = countSchemaRejections;
4
- const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
5
- const zod_compat_js_1 = require("@modelcontextprotocol/sdk/server/zod-compat.js");
6
- const telemetry_1 = require("./telemetry");
7
- /**
8
- * Wraps `server` so that each call its tools' schemas refuse is counted as one `tool_rejected` event. `onRefused` hears the
9
- * tool's name for each such call, before the event is written: a refused call is still a call (a session's first call).
10
- */
11
- function countSchemaRejections(server, onRefused) {
12
- const sdk = server;
13
- const validate = sdk.validateToolInput;
14
- if (typeof validate !== 'function')
15
- return;
16
- sdk.validateToolInput = async (tool, args, toolName) => {
17
- try {
18
- return await validate.call(server, tool, args, toolName);
19
- }
20
- catch (err) {
21
- // Inside this method the SDK throws InvalidParams for one thing only: the arguments failed the schema (an unknown or
22
- // disabled tool is refused before it is reached). Anything else is a defect of the schema itself, not a refusal.
23
- if (err?.code === types_js_1.ErrorCode.InvalidParams) {
24
- onRefused?.(toolName);
25
- await recordRejection(tool, args, toolName);
26
- }
27
- throw err;
28
- }
29
- };
30
- }
31
- async function recordRejection(tool, args, toolName) {
32
- // The SDK has refused the call, so it is counted whatever happens next: a failure to say more leaves the kind at `other`.
33
- let described = { kind: 'other' };
34
- try {
35
- described = await describeFailure(tool, args);
36
- }
37
- catch {
38
- // Observability must never change what the caller reads.
39
- }
40
- (0, telemetry_1.emitTelemetry)({ event: 'tool_rejected', tool: toolName, ...described });
41
- }
42
- /**
43
- * Which argument the schema refused first and how, read from the issues the SDK's own parse yields (the same function, on
44
- * the same schema and the same arguments, so the same verdict). Issues follow the schema's own order, so "first" is stable.
45
- */
46
- async function describeFailure(tool, args) {
47
- if (!tool.inputSchema)
48
- return { kind: 'other' };
49
- const parsed = await (0, zod_compat_js_1.safeParseAsync)(tool.inputSchema, args);
50
- if (parsed.success)
51
- return { kind: 'other' };
52
- const issue = firstIssue(parsed.error);
53
- if (!issue)
54
- return { kind: 'other' };
55
- // Only a name the tool's own schema declares can be an argument that failed: a key the caller invented is not one. A declared
56
- // name that is not a plain identifier is left out too, so the call is still counted instead of the whole line being dropped.
57
- const shape = (0, zod_compat_js_1.getObjectShape)(tool.inputSchema);
58
- const head = issue.path[0];
59
- const named = typeof head === 'string' && shape !== undefined && Object.prototype.hasOwnProperty.call(shape, head) && (0, telemetry_1.isSafeArgName)(head);
60
- return { ...(named ? { arg: head } : {}), kind: kindOf(issue, args) };
61
- }
62
- function firstIssue(error) {
63
- const issues = error?.issues;
64
- if (!Array.isArray(issues))
65
- return undefined;
66
- const first = issues[0];
67
- if (!first || typeof first.code !== 'string' || !Array.isArray(first.path))
68
- return undefined;
69
- return { code: first.code, path: first.path };
70
- }
71
- function kindOf(issue, args) {
72
- switch (issue.code) {
73
- // The schema says "invalid_type" both for an absent argument and for one of the wrong type; the call says which.
74
- case 'invalid_type': return valueAt(args, issue.path) === undefined ? 'missing' : 'wrong_type';
75
- case 'too_small': return 'too_small';
76
- case 'too_big': return 'too_big';
77
- case 'invalid_value': return 'invalid_value';
78
- case 'invalid_format': return 'invalid_format';
79
- default: return 'other';
80
- }
81
- }
82
- /** Whatever sits at `path` in what the caller sent. Only ever compared with `undefined`: it is never kept or written. */
83
- function valueAt(root, path) {
84
- let value = root;
85
- for (const key of path) {
86
- if (value === null || typeof value !== 'object')
87
- return undefined;
88
- value = value[key];
89
- }
90
- return value;
91
- }
package/dist/mvp/scope.js DELETED
@@ -1,127 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeScope = normalizeScope;
4
- exports.dailyDirForScope = dailyDirForScope;
5
- exports.notePathMatchesScope = notePathMatchesScope;
6
- exports.scopeSqlPredicate = scopeSqlPredicate;
7
- exports.scopeSpellings = scopeSpellings;
8
- exports.savedScopeMatches = savedScopeMatches;
9
- exports.savedScopeSqlPredicate = savedScopeSqlPredicate;
10
- /**
11
- * Optional memory scope for MVP tools (global vs named project/scope).
12
- *
13
- * Allowed forms:
14
- * - "global" (default)
15
- * - "scope:<slug>" or "project:<slug>"
16
- * Slug: lowercase [a-z0-9-]{1,64} after normalization.
17
- */
18
- const toolRefusal_1 = require("../toolRefusal");
19
- /** The one directory a scope owns. Read by both the JS and the SQL scope rules. */
20
- const SCOPES_ROOT = 'Memory/Scopes/';
21
- const SLUG_RE = /^[a-z0-9-]{1,64}$/;
22
- /** Normalize user/agent scope input. Throws on invalid values. */
23
- function normalizeScope(raw) {
24
- if (raw === undefined || raw === null || raw.trim() === '')
25
- return 'global';
26
- const s = raw.trim().toLowerCase();
27
- if (s === 'global')
28
- return 'global';
29
- let kind = null;
30
- let slug = s;
31
- if (s.startsWith('scope:')) {
32
- kind = 'scope';
33
- slug = s.slice('scope:'.length);
34
- }
35
- else if (s.startsWith('project:')) {
36
- kind = 'project';
37
- slug = s.slice('project:'.length);
38
- }
39
- else {
40
- // Bare slug → scope:<slug>
41
- kind = 'scope';
42
- slug = s;
43
- }
44
- // Normalize slug: spaces/underscores → hyphens; strip invalid chars
45
- slug = slug
46
- .replace(/[_\s]+/g, '-')
47
- .replace(/[^a-z0-9-]/g, '')
48
- .replace(/-+/g, '-')
49
- .replace(/^-|-$/g, '');
50
- if (!SLUG_RE.test(slug)) {
51
- // The refusal names its code for telemetry (a closed set, never built from this input); the message the
52
- // agent reads is unchanged, and it is the agent's own argument that is quoted in it.
53
- throw (0, toolRefusal_1.refusal)('invalid_scope', `invalid scope ${JSON.stringify(raw)}: use "global" or "scope:<slug>" / "project:<slug>" with slug matching [a-z0-9-]{1,64}`);
54
- }
55
- return `${kind}:${slug}`;
56
- }
57
- /** Daily-note directory under the vault for this scope. */
58
- function dailyDirForScope(scope) {
59
- if (scope === 'global')
60
- return 'Memory/Daily';
61
- const slug = scope.includes(':') ? scope.slice(scope.indexOf(':') + 1) : scope;
62
- return `Memory/Scopes/${slug}/Daily`;
63
- }
64
- /** True if a vault-relative note path belongs to the given scope. */
65
- function notePathMatchesScope(notePath, scope) {
66
- const norm = notePath.replace(/\\/g, '/');
67
- if (scope === 'global') {
68
- // Global notes: not under Memory/Scopes/
69
- if (norm.startsWith('Memory/Scopes/') || norm.includes('/Memory/Scopes/'))
70
- return false;
71
- return true;
72
- }
73
- const slug = scope.slice(scope.indexOf(':') + 1);
74
- const prefix = `Memory/Scopes/${slug}/`;
75
- return norm === prefix.slice(0, -1) || norm.startsWith(prefix) || norm.includes('/' + prefix);
76
- }
77
- /**
78
- * The same rule as notePathMatchesScope, as a SQL predicate over a stored path
79
- * column, for the callers that must filter or count rows without materializing
80
- * every path. Each arm below is one arm of that function — the three `dir`
81
- * terms are its equality, startsWith and includes cases — and
82
- * tests/mvp/scope-isolation.test.ts pins them against each other, so a scope
83
- * can never mean two different things depending on whether it was checked in
84
- * SQL or in JS. `dir` is built from a validated slug, so it contains no LIKE
85
- * wildcard. Bind with better-sqlite3's positional params in `params`.
86
- */
87
- function scopeSqlPredicate(scope, column) {
88
- if (scope === 'global') {
89
- return {
90
- sql: `(${column} NOT LIKE ? AND instr(${column}, ?) = 0)`,
91
- params: [`${SCOPES_ROOT}%`, `/${SCOPES_ROOT}`],
92
- };
93
- }
94
- const dir = `${SCOPES_ROOT}${scope.slice(scope.indexOf(':') + 1)}`;
95
- return {
96
- sql: `(instr(${column}, ?) > 0 OR ${column} LIKE ? OR ${column} = ?)`,
97
- params: [`/${dir}/`, `${dir}/%`, dir],
98
- };
99
- }
100
- /**
101
- * The scopes a fact or a ledger event can have been saved in and belong to `scope`: the one rule notes follow, for
102
- * what is not a note. A named scope is its slug. `scope:<slug>`, `project:<slug>` and the bare `<slug>` are spellings
103
- * of one scope: a note saved in any of them goes to the one folder of that slug (`dailyDirForScope`) and is read in
104
- * all three (`notePathMatchesScope`). A fact keeps the spelling it was saved with (the ledger, its digest and the
105
- * `scope` a read shows are what was written), and is read in every spelling of its slug, as its scope's notes are. A
106
- * save corrects its subject and predicate in every spelling too: it closes the current value whatever spelling that
107
- * was saved with (memory/lifecycle.ts, one lifecycle stream per slug).
108
- * Global has one spelling. Distinct, so a caller that reads once per spelling never reads a scope twice.
109
- */
110
- function scopeSpellings(scope) {
111
- if (scope === 'global')
112
- return ['global'];
113
- const slug = scope.slice(scope.indexOf(':') + 1);
114
- return [`scope:${slug}`, `project:${slug}`];
115
- }
116
- /** Whether a fact or ledger event saved in `saved` (a stored, normalized scope) belongs to `scope`. */
117
- function savedScopeMatches(saved, scope) {
118
- return scopeSpellings(scope).includes(saved);
119
- }
120
- /**
121
- * The same rule as savedScopeMatches, as a SQL predicate over a stored scope column (`facts.scope`,
122
- * `fact_terms.scope`). Bind with better-sqlite3's positional params in `params`.
123
- */
124
- function savedScopeSqlPredicate(scope, column) {
125
- const spellings = scopeSpellings(scope);
126
- return { sql: `${column} IN (${spellings.map(() => '?').join(', ')})`, params: spellings };
127
- }