renor-engine 1.0.0 → 1.2.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 (110) hide show
  1. package/LICENSE +202 -661
  2. package/NOTICE +8 -6
  3. package/README.md +13 -8
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/api/server.js +10 -8
  6. package/dist/buildIdentity.js +18 -1
  7. package/dist/callPhases.js +82 -0
  8. package/dist/cli.js +36 -1
  9. package/dist/context/rowStore.js +1 -1
  10. package/dist/conversation/parser.js +40 -36
  11. package/dist/db/client.js +6 -4
  12. package/dist/db/handoffTable.js +31 -0
  13. package/dist/db/indexSalvage.js +24 -4
  14. package/dist/db/migrations.js +30 -2
  15. package/dist/db/sqlite.js +396 -0
  16. package/dist/digests/digestCache.js +6 -2
  17. package/dist/digests/digests.js +3 -3
  18. package/dist/digests/worker.js +3 -1
  19. package/dist/embeddings/caching.js +5 -5
  20. package/dist/embeddings/local.js +5 -3
  21. package/dist/embeddings/openai.js +4 -3
  22. package/dist/embeddings/simdScan.js +3 -1
  23. package/dist/embeddings/vectorIndex.js +0 -1
  24. package/dist/embeddings/vertex.js +123 -13
  25. package/dist/eval/cacheReport.js +7 -0
  26. package/dist/eval/context700-candidate.js +5 -5
  27. package/dist/eval/evalCache.js +46 -3
  28. package/dist/eval/indexSnapshots.js +549 -0
  29. package/dist/eval/pageDump.js +67 -0
  30. package/dist/eval/runManifest.js +6 -0
  31. package/dist/eval/textTokens.js +17 -0
  32. package/dist/evidence/attach.js +0 -3
  33. package/dist/evidence/evidenceCache.js +3 -3
  34. package/dist/evidence/ollamaChat.js +1 -1
  35. package/dist/evidence/options.js +46 -25
  36. package/dist/evidence/secondPass.js +8 -170
  37. package/dist/extraction/factExtractor.js +1 -1
  38. package/dist/extraction/jevDecisionModel.js +307 -0
  39. package/dist/extraction/preferenceExtractor.js +1 -4
  40. package/dist/extraction/secretGuard.js +7 -3
  41. package/dist/extraction/typedFacts.js +1 -1
  42. package/dist/importing/apply.js +42 -14
  43. package/dist/importing/cli.js +9 -8
  44. package/dist/importing/existing.js +2 -0
  45. package/dist/importing/facts.js +0 -2
  46. package/dist/importing/plan.js +0 -5
  47. package/dist/importing/source.js +1 -1
  48. package/dist/indexer/parser.js +4 -1
  49. package/dist/indexer/pipeline.js +99 -279
  50. package/dist/indexer/walker.js +11 -25
  51. package/dist/learning/cli.js +113 -0
  52. package/dist/learning/schema.js +144 -0
  53. package/dist/learning/settings.js +45 -0
  54. package/dist/learning/store.js +267 -0
  55. package/dist/learning/tap.js +401 -0
  56. package/dist/mcp/server.js +36 -6
  57. package/dist/memory/dreamer/adjudicate.js +215 -0
  58. package/dist/memory/dreamer/apply.js +120 -0
  59. package/dist/memory/dreamer/budget.js +64 -0
  60. package/dist/memory/dreamer/candidates.js +149 -0
  61. package/dist/memory/dreamer/collection.js +104 -0
  62. package/dist/memory/dreamer/dreamer.js +352 -0
  63. package/dist/memory/dreamer/review.js +111 -0
  64. package/dist/memory/factConflicts.js +2 -0
  65. package/dist/memory/handoff.js +363 -0
  66. package/dist/memory/lifecycle.js +338 -80
  67. package/dist/memory/sourceName.js +77 -1
  68. package/dist/memory/statusFacts.js +61 -0
  69. package/dist/memory/writer.js +3 -2
  70. package/dist/memorymap/memoryMap.js +39 -13
  71. package/dist/memorymap/replacements.js +8 -3
  72. package/dist/mvp/agentMemory.js +109 -50
  73. package/dist/mvp/answerCheck.js +211 -0
  74. package/dist/mvp/cli.js +14 -1
  75. package/dist/mvp/compileCache.js +3 -10
  76. package/dist/mvp/compileSeed.js +9 -3
  77. package/dist/mvp/config.js +4 -0
  78. package/dist/mvp/contextDistinct.js +53 -0
  79. package/dist/mvp/contextProfile.js +87 -0
  80. package/dist/mvp/directorySurface.js +68 -0
  81. package/dist/mvp/engine.js +39 -26
  82. package/dist/mvp/exportAll.js +36 -2
  83. package/dist/mvp/factMarkers.js +18 -2
  84. package/dist/mvp/history.js +5 -4
  85. package/dist/mvp/log.js +118 -0
  86. package/dist/mvp/server.js +759 -136
  87. package/dist/mvp/serverCard.js +12 -3
  88. package/dist/mvp/telemetry.js +35 -2
  89. package/dist/net/poolFetch.js +174 -0
  90. package/dist/recall/actionRecall.js +282 -0
  91. package/dist/recall/autoRecall.js +447 -0
  92. package/dist/recall/searchRecall.js +189 -0
  93. package/dist/retrieval/entityLookup.js +53 -4
  94. package/dist/retrieval/orchestrator.js +148 -10
  95. package/dist/retrieval/plainWording.js +236 -0
  96. package/dist/retrieval/rankingTrace.js +184 -0
  97. package/dist/retrieval/readerLayout.js +143 -224
  98. package/dist/router/routerCache.js +2 -2
  99. package/dist/sharing/cli.js +181 -0
  100. package/dist/sharing/examples.js +136 -0
  101. package/dist/sharing/fields.js +225 -0
  102. package/dist/sharing/loop.js +80 -0
  103. package/dist/sharing/project.js +327 -0
  104. package/dist/sharing/send.js +466 -0
  105. package/dist/sharing/state.js +222 -0
  106. package/dist/sharing/wording.js +48 -0
  107. package/dist/watcher/watcher.js +5 -3
  108. package/package.json +5 -7
  109. package/dist/BUILD.json +0 -6
  110. package/dist/importing/index.js +0 -29
@@ -0,0 +1,307 @@
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.JevDecisionModel = exports.JEV_INPUT_USD_PER_MTOK = exports.JEV_COOLDOWN_MS = exports.JEV_TIMEOUT_MS = exports.JEV_STORE_MIN = exports.JEV_MODEL = exports.JEV_BASE_URL = void 0;
7
+ exports.resolveJevOptin = resolveJevOptin;
8
+ exports.readJevKey = readJevKey;
9
+ exports.jevDecisionLogPath = jevDecisionLogPath;
10
+ exports.jevDecisionLog = jevDecisionLog;
11
+ exports.saveDecisionModel = saveDecisionModel;
12
+ /**
13
+ * Jev (TypeSafe System One) behind the DecisionModel seam, for the save path of ONE opted-in workspace.
14
+ *
15
+ * Off unless the workspace's own env file says so (owner decision, 21 Oct 2026: "your own workspace only, now"):
16
+ *
17
+ * RENOR_JEV_OPTIN=<workspace slug> must equal MVP_INSTANCE_ID, so an env file copied to another workspace stays off
18
+ * RENOR_JEV_KEY_FILE=<path> the TypeSafe key, by its path (a file, so the env file never holds the secret)
19
+ *
20
+ * Both are needed, and so is MVP_MEMORY_DIR (every decision is recorded there, so a decision that cannot be recorded is not made).
21
+ * Anything else, or a setting that does not parse, is off with one stderr line naming the setting, never a value.
22
+ *
23
+ * What it does, once per text about to be stored, in one request (the two questions are independent):
24
+ *
25
+ * sensitivity a credential or a personal identifier. The scanner (secretGuard.ts) has already answered and a scanner
26
+ * `sensitive` stays final; this adds a second opinion. `store` is allowed only at P(store) >= 0.7, the rule
27
+ * that caught 243/243 planted items at 2/200 false blocks (LEDGER block F). A block refuses the save, as the
28
+ * scanner does, and nothing is redacted.
29
+ * capture gate whether the text is worth saving. RECORDED ONLY: it never drops a save, because a wrong "not worth it"
30
+ * loses a memory silently and the scout measured 93% recall, not 100%.
31
+ *
32
+ * What it never does: it reads nothing, so no read path reaches it; it sends no text that is already stored; it never owns a
33
+ * dedupe or supersede verdict (dedupe_adjudication is the local rules, byte for byte: Jev made 16/55 wrong supersedes on real
34
+ * pairs); and it never blocks or loses a save for any reason of its own: a timeout, an error, a bad reply or a cool-down all
35
+ * fall back to today's local rules (the scanner has already run). Every call is recorded as numbers and labels, never text.
36
+ */
37
+ const node_fs_1 = __importDefault(require("node:fs"));
38
+ const node_path_1 = __importDefault(require("node:path"));
39
+ const decision_model_1 = require("./decision-model");
40
+ exports.JEV_BASE_URL = 'https://api.typesafe.ai';
41
+ exports.JEV_MODEL = 'jev-latest';
42
+ /** P(store) below this blocks (LEDGER block F: 243/243 planted items caught at 2/200 false blocks). */
43
+ exports.JEV_STORE_MIN = 0.7;
44
+ /** A save never waits longer than this for Jev (the scout measured p95 about 0.3 s). */
45
+ exports.JEV_TIMEOUT_MS = 1500;
46
+ /** After a failed call, saves skip Jev for this long, so an outage costs one timeout, not one per save. */
47
+ exports.JEV_COOLDOWN_MS = 30_000;
48
+ /** $0.042 per million input tokens (COST-MODEL lane-Jev arm; output is free). */
49
+ exports.JEV_INPUT_USD_PER_MTOK = 0.042;
50
+ const SENSITIVITY_QUESTION = {
51
+ type: 'choice',
52
+ instructions: 'Decide how to persist this text. "block" = do not store it at all (it contains a '
53
+ + 'secret credential: API key, token, password, private key, or live account '
54
+ + 'identifier). "redact" = the text is useful but contains personal identifiers '
55
+ + '(phone, email, card, government id) that must be masked before storing. '
56
+ + '"store" = no secrets or personal identifiers; safe to store verbatim.',
57
+ criteria: {
58
+ store: 'No secrets or personal identifiers; safe to store verbatim.',
59
+ block: 'Contains a secret credential; must not be stored at all.',
60
+ redact: 'Useful text containing personal identifiers that must be masked first.',
61
+ },
62
+ };
63
+ const CAPTURE_QUESTION = {
64
+ type: 'choice',
65
+ instructions: 'What durable information about the user or their world does this user message state?',
66
+ criteria: {
67
+ none: 'Nothing durable: questions, small talk, opinions, transient requests.',
68
+ fact: 'A stable attribute or circumstance.',
69
+ event: 'Something that happened or is scheduled.',
70
+ preference: 'A liking, dislike, habit or standing choice.',
71
+ commitment: 'A plan or promise for future action.',
72
+ sensitive: 'A credential or private identifier.',
73
+ },
74
+ };
75
+ function asChoice(answer) {
76
+ const a = answer;
77
+ return a && a.type === 'choice' && typeof a.choice === 'string' ? a : null;
78
+ }
79
+ function probability(answer, label) {
80
+ const p = answer.probabilities?.[label];
81
+ if (typeof p === 'number' && p >= 0 && p <= 1)
82
+ return p;
83
+ // No distribution in the reply: the chosen label gets its confidence, every other label gets nothing.
84
+ return answer.choice === label ? Math.max(0, Math.min(1, answer.confidence ?? 0)) : 0;
85
+ }
86
+ class JevFallback extends Error {
87
+ reason;
88
+ status;
89
+ constructor(reason, status) {
90
+ super(reason);
91
+ this.reason = reason;
92
+ this.status = status;
93
+ }
94
+ }
95
+ class JevDecisionModel {
96
+ opts;
97
+ local;
98
+ baseUrl;
99
+ timeoutMs;
100
+ cooldownMs;
101
+ now;
102
+ coolUntil = 0;
103
+ constructor(opts) {
104
+ this.opts = opts;
105
+ this.local = opts.local ?? (0, decision_model_1.defaultDecisionModel)();
106
+ this.baseUrl = (opts.baseUrl ?? exports.JEV_BASE_URL).replace(/\/+$/, '');
107
+ this.timeoutMs = opts.timeoutMs ?? exports.JEV_TIMEOUT_MS;
108
+ this.cooldownMs = opts.cooldownMs ?? exports.JEV_COOLDOWN_MS;
109
+ this.now = opts.now ?? Date.now;
110
+ }
111
+ /** Never throws. Dedupe and note classification are the local rules, whatever Jev would say. */
112
+ async decide(request) {
113
+ // Resolve the local decision before branching so every fallback below has the concrete
114
+ // DecisionResult promised by this adapter, rather than its nested asynchronous form.
115
+ const fallback = await this.local.decide(request);
116
+ if (request.task !== 'capture_gate')
117
+ return fallback;
118
+ const text = request.text;
119
+ if (!text.trim())
120
+ return fallback;
121
+ const started = this.now();
122
+ if (started < this.coolUntil) {
123
+ this.record({ outcome: 'fallback', reason: 'cooldown', latencyMs: 0, inputTokens: 0, chars: text.length });
124
+ return fallback;
125
+ }
126
+ // A decision that cannot be recorded is not made: no text leaves the machine without its local record.
127
+ if (!this.canRecord())
128
+ return fallback;
129
+ try {
130
+ const call = await this.call(text);
131
+ const sensitivity = asChoice(call.answers.sensitivity);
132
+ const capture = asChoice(call.answers.capture_gate);
133
+ if (!sensitivity)
134
+ throw new JevFallback('bad_reply');
135
+ const pStore = probability(sensitivity, 'store');
136
+ const blocked = pStore < exports.JEV_STORE_MIN;
137
+ const inputTokens = call.inputTokens;
138
+ const costUSDCents = (inputTokens * exports.JEV_INPUT_USD_PER_MTOK) / 1_000_000 * 100;
139
+ const latencyMs = this.now() - started;
140
+ this.record({
141
+ outcome: 'applied', latencyMs, inputTokens, costUSDCents, chars: text.length,
142
+ sensitivity: { choice: sensitivity.choice, pStore, confidence: sensitivity.confidence ?? 0, blocked },
143
+ ...(capture ? { capture: { choice: capture.choice, pDurable: 1 - probability(capture, 'none'), confidence: capture.confidence ?? 0 } } : {}),
144
+ });
145
+ if (!blocked)
146
+ return fallback;
147
+ // Refused like a scanner hit: the guard reads `sensitive` and refuses the save. `redact` is a refusal too (nothing is altered).
148
+ const verdict = { task: 'capture_gate', decision: 'sensitive', confidence: 1 - pStore, latencyMs, costUSDCents };
149
+ return verdict;
150
+ }
151
+ catch (err) {
152
+ this.coolUntil = this.now() + this.cooldownMs;
153
+ const known = err instanceof JevFallback ? err : null;
154
+ this.record({
155
+ outcome: 'fallback', reason: known?.reason ?? 'error', ...(known?.status !== undefined ? { status: known.status } : {}),
156
+ latencyMs: this.now() - started, inputTokens: 0, chars: text.length,
157
+ });
158
+ return fallback;
159
+ }
160
+ }
161
+ async call(text) {
162
+ let res;
163
+ try {
164
+ res = await (this.opts.fetchImpl ?? fetch)(`${this.baseUrl}/v1/systemone`, {
165
+ method: 'POST',
166
+ headers: { 'content-type': 'application/json', authorization: `Bearer ${this.opts.apiKey}` },
167
+ body: JSON.stringify({ model: exports.JEV_MODEL, state: text, questions: { sensitivity: SENSITIVITY_QUESTION, capture_gate: CAPTURE_QUESTION } }),
168
+ signal: AbortSignal.timeout(this.timeoutMs),
169
+ });
170
+ }
171
+ catch (err) {
172
+ const name = err instanceof Error ? err.name : '';
173
+ throw new JevFallback(name === 'TimeoutError' || name === 'AbortError' ? 'timeout' : 'error');
174
+ }
175
+ if (!res.ok)
176
+ throw new JevFallback('http', res.status);
177
+ let json;
178
+ try {
179
+ json = (await res.json());
180
+ }
181
+ catch (err) {
182
+ const name = err instanceof Error ? err.name : '';
183
+ throw new JevFallback(name === 'TimeoutError' || name === 'AbortError' ? 'timeout' : 'bad_reply');
184
+ }
185
+ if (!json || typeof json.answers !== 'object' || json.answers === null)
186
+ throw new JevFallback('bad_reply');
187
+ return { answers: json.answers, inputTokens: Number.isFinite(json.usage?.input_tokens) ? Number(json.usage?.input_tokens) : 0 };
188
+ }
189
+ canRecord() {
190
+ try {
191
+ return this.opts.canRecord() === true;
192
+ }
193
+ catch {
194
+ return false;
195
+ }
196
+ }
197
+ record(partial) {
198
+ try {
199
+ this.opts.record({
200
+ v: 1, at: new Date(this.now()).toISOString(), workspace: this.opts.workspace, task: 'save_screen', costUSDCents: 0, ...partial,
201
+ });
202
+ }
203
+ catch { /* a log that cannot be written never costs a save */ }
204
+ }
205
+ }
206
+ exports.JevDecisionModel = JevDecisionModel;
207
+ const toldOff = new Set();
208
+ /** null when the workspace has not opted in or the setting cannot be honoured; says why on stderr, never a value. */
209
+ function resolveJevOptin(env = process.env) {
210
+ const workspace = (env.RENOR_JEV_OPTIN ?? '').trim().toLowerCase();
211
+ if (workspace === '')
212
+ return null;
213
+ const off = (why) => {
214
+ if (!toldOff.has(why)) {
215
+ toldOff.add(why);
216
+ process.stderr.write(`[jev] RENOR_JEV_OPTIN is set but ${why}: the save screen stays off\n`);
217
+ }
218
+ return null;
219
+ };
220
+ if ((env.MVP_INSTANCE_ID ?? '').trim().toLowerCase() !== workspace)
221
+ return off('it does not name this workspace (MVP_INSTANCE_ID)');
222
+ const keyFile = (env.RENOR_JEV_KEY_FILE ?? '').trim();
223
+ if (!node_path_1.default.isAbsolute(keyFile))
224
+ return off('RENOR_JEV_KEY_FILE is not an absolute path');
225
+ const memoryDir = (env.MVP_MEMORY_DIR ?? '').trim();
226
+ if (!node_path_1.default.isAbsolute(memoryDir))
227
+ return off('MVP_MEMORY_DIR is not an absolute path, so decisions could not be recorded');
228
+ return { workspace, keyFile, memoryDir };
229
+ }
230
+ /** The key in the file: one token. Null (with one stderr line naming the path, never the content) when it cannot be used. */
231
+ function readJevKey(keyFile) {
232
+ try {
233
+ const st = node_fs_1.default.statSync(keyFile);
234
+ if (!st.isFile())
235
+ throw new Error('not a regular file');
236
+ // A service credential readable by anyone but its owner is not used (the contract: mode 0600, the tenant's account only).
237
+ if ((st.mode & 0o077) !== 0)
238
+ throw new Error('readable or writable by group or others; chmod 600 it');
239
+ if (typeof process.getuid === 'function' && st.uid !== process.getuid())
240
+ throw new Error('not owned by this account');
241
+ const key = node_fs_1.default.readFileSync(keyFile, 'utf8').trim();
242
+ if (key === '' || /\s/.test(key))
243
+ throw new Error('empty or not a single token');
244
+ return key;
245
+ }
246
+ catch (err) {
247
+ process.stderr.write(`[jev] the key file ${keyFile} cannot be used (${err instanceof Error ? err.message.replace(keyFile, 'it') : 'unreadable'}): the save screen stays off\n`);
248
+ return null;
249
+ }
250
+ }
251
+ /** Where a workspace's Jev decisions are kept: beside its index, which the walker, the watcher and release packing already skip. */
252
+ function jevDecisionLogPath(memoryDir) {
253
+ return node_path_1.default.join(node_path_1.default.resolve(memoryDir), '.memory-index', 'jev-decisions.jsonl');
254
+ }
255
+ /**
256
+ * One JSON line per call, owner-only. `ready` opens the log for append before any text is sent, so a log that cannot be
257
+ * written turns the screen off for that save (Jev is not asked; the local rules decide). Told once on stderr; never reaches the save.
258
+ */
259
+ function jevDecisionLog(memoryDir) {
260
+ const file = jevDecisionLogPath(memoryDir);
261
+ let told = false;
262
+ const tell = (err) => {
263
+ if (told)
264
+ return;
265
+ told = true;
266
+ process.stderr.write(`[jev] a decision could not be recorded (${err instanceof Error ? err.name : 'error'}): Jev is not asked, saves go on\n`);
267
+ };
268
+ return {
269
+ ready: () => {
270
+ try {
271
+ node_fs_1.default.mkdirSync(node_path_1.default.dirname(file), { recursive: true, mode: 0o700 });
272
+ node_fs_1.default.closeSync(node_fs_1.default.openSync(file, 'a', 0o600));
273
+ return true;
274
+ }
275
+ catch (err) {
276
+ tell(err);
277
+ return false;
278
+ }
279
+ },
280
+ write: entry => {
281
+ try {
282
+ node_fs_1.default.appendFileSync(file, `${JSON.stringify(entry)}\n`, { mode: 0o600 });
283
+ }
284
+ catch (err) {
285
+ tell(err);
286
+ }
287
+ },
288
+ };
289
+ }
290
+ let cached;
291
+ /**
292
+ * The model the save path uses, or null (the common case) when this workspace has not opted in. One instance per process, so
293
+ * its cool-down is shared by every save; rebuilt only when the settings change.
294
+ */
295
+ function saveDecisionModel(env = process.env) {
296
+ const optin = resolveJevOptin(env);
297
+ if (!optin)
298
+ return null;
299
+ const signature = `${optin.workspace}\0${optin.keyFile}\0${optin.memoryDir}`;
300
+ if (cached?.signature === signature)
301
+ return cached.model;
302
+ const key = readJevKey(optin.keyFile);
303
+ const log = jevDecisionLog(optin.memoryDir);
304
+ const model = key ? new JevDecisionModel({ workspace: optin.workspace, apiKey: key, record: log.write, canRecord: log.ready }) : null;
305
+ cached = { signature, model };
306
+ return model;
307
+ }
@@ -106,10 +106,7 @@ async function extractPreferences(client, noteTitle, noteBody) {
106
106
  // `statement` are dropped below, so mis-shaped rows can never become false
107
107
  // preferences — empty is the worst case.
108
108
  const firstArray = Object.values(parsed).find((v) => Array.isArray(v) && v.length > 0 && typeof v[0] === 'object' && v[0] !== null);
109
- if (firstArray)
110
- parsed.prefs = firstArray;
111
- else
112
- parsed.prefs = [parsed];
109
+ parsed.prefs = firstArray ?? [parsed];
113
110
  }
114
111
  if (!parsed || typeof parsed !== 'object' || !Array.isArray(parsed.prefs)) {
115
112
  throw new typedFacts_1.ExtractorParseError('extractor-parse-error: preference extraction response is missing a prefs array');
@@ -132,19 +132,23 @@ function refuseUnchecked(field, action) {
132
132
  /**
133
133
  * Throws SecretRefusedError when the guard is on and any of `texts` is sensitive; resolves otherwise.
134
134
  * It fails closed: if the check itself throws, the save is refused. Call it before the first write.
135
+ * `contentModel`, when a save path passes one, only adds a second opinion on the texts that are not `nameOnly`.
135
136
  * With the guard off it returns at once, so callers that must not add a step to the unguarded path
136
137
  * check `secretGuardEnabled()` first and skip the call.
137
138
  */
138
- async function assertStorable(texts, action = 'save') {
139
+ async function assertStorable(texts, action = 'save', contentModel = null) {
139
140
  if (!secretGuardEnabled())
140
141
  return;
141
142
  const model = new SecretGuardDecisionModel();
142
- for (const { field, text } of texts) {
143
+ // A second opinion on content only (jevDecisionModel.ts, one opted-in workspace's saves): the scanner still answers first
144
+ // and a scanner `sensitive` is final, because SecretGuardDecisionModel asks the inner model only about text it did not flag.
145
+ const withContentModel = contentModel ? new SecretGuardDecisionModel(contentModel) : model;
146
+ for (const { field, text, nameOnly } of texts) {
143
147
  if (text === undefined || text === '')
144
148
  continue;
145
149
  let verdict;
146
150
  try {
147
- verdict = await model.decide({ task: 'capture_gate', text });
151
+ verdict = await (nameOnly ? model : withContentModel).decide({ task: 'capture_gate', text });
148
152
  }
149
153
  catch (cause) {
150
154
  refuse(field, 'unavailable', action, cause);
@@ -195,7 +195,7 @@ async function extractNumericFacts(client, noteTitle, noteBody) {
195
195
  continue; // malformed rows are dropped, not fatal
196
196
  const fact = { value, object };
197
197
  for (const key of ['unit', 'verb', 'event']) {
198
- const v = raw[key];
198
+ const v = raw?.[key];
199
199
  if (v !== undefined && v !== null && String(v).trim())
200
200
  fact[key] = String(v).trim();
201
201
  }
@@ -14,6 +14,7 @@ const schema_1 = require("../db/schema");
14
14
  const hasher_1 = require("../indexer/hasher");
15
15
  const vectorIndex_1 = require("../embeddings/vectorIndex");
16
16
  const confinedWrite_1 = require("../memory/confinedWrite");
17
+ const durableAppend_1 = require("../memory/durableAppend");
17
18
  const lifecycle_1 = require("../memory/lifecycle");
18
19
  const secretGuard_1 = require("../extraction/secretGuard");
19
20
  const secretScreen_1 = require("./secretScreen");
@@ -70,6 +71,7 @@ async function applyImportPlan(plan, _bundle, options) {
70
71
  const created = { files: [], dirs: [] };
71
72
  let phase = 'notes';
72
73
  let priorLedger = null;
74
+ let appendedLedger = null;
73
75
  // Set immediately before this invocation's first append. A failed read or parse must
74
76
  // never enter ledger compensation: no ledger bytes have been touched yet.
75
77
  let ledgerTouched = false;
@@ -168,19 +170,28 @@ async function applyImportPlan(plan, _bundle, options) {
168
170
  // directories that are missing now, so a rollback cannot leave an empty tree behind.
169
171
  for (const dir of missingDirsUnder(memoryDir, path_1.default.dirname(ledgerPath)))
170
172
  created.dirs.push(dir);
171
- for (const event of plan.events) {
172
- const eventKey = event.id.toUpperCase();
173
- if (existingLedgerIds.has(eventKey)) {
174
- eventsAlreadyPresent += 1;
175
- continue;
173
+ try {
174
+ for (const event of plan.events) {
175
+ const eventKey = event.id.toUpperCase();
176
+ if (existingLedgerIds.has(eventKey)) {
177
+ eventsAlreadyPresent += 1;
178
+ continue;
179
+ }
180
+ // From this point the durable bytes may change, so and only so can rollback restore
181
+ // the prior snapshot (or remove a ledger this invocation created from an absent path).
182
+ phase = 'ledger';
183
+ ledgerTouched = true;
184
+ await (0, lifecycle_1.appendLedgerEvent)(memoryDir, ledgerRelPath, toLedgerEvent(event));
185
+ existingLedgerIds.add(eventKey);
186
+ eventsAppended += 1;
176
187
  }
177
- // From this point the durable bytes may change, so and only so can rollback restore
178
- // the prior snapshot (or remove a ledger this invocation created from an absent path).
179
- phase = 'ledger';
180
- ledgerTouched = true;
181
- await (0, lifecycle_1.appendLedgerEvent)(memoryDir, ledgerRelPath, toLedgerEvent(event));
182
- existingLedgerIds.add(eventKey);
183
- eventsAppended += 1;
188
+ }
189
+ finally {
190
+ // Capture what this run actually left behind even when an append rejects. The
191
+ // compare-before-restore guard must compare against these bytes, not `null`, after
192
+ // an earlier event in the same plan was appended successfully.
193
+ if (ledgerTouched)
194
+ appendedLedger = readFileOrNull(ledgerPath);
184
195
  }
185
196
  phase = 'index';
186
197
  db = (0, client_1.openDb)(dbPath);
@@ -248,7 +259,7 @@ async function applyImportPlan(plan, _bundle, options) {
248
259
  // did not create — unchanged notes, conflicts, the operator's own files — are never
249
260
  // touched, and pruning is limited to directories that are empty afterwards.
250
261
  const restore = ledgerTouched
251
- ? restoreLedger(ledgerPath, priorLedger)
262
+ ? await restoreLedger(ledgerPath, priorLedger, appendedLedger)
252
263
  : { restored: false };
253
264
  // A ledger that could not be restored still holds its carried events, and those events
254
265
  // are canonical: removing the notes they describe would leave a ledger pointing at
@@ -484,8 +495,25 @@ function readFileOrNull(filePath) {
484
495
  * Returns the OUTCOME rather than swallowing it: a run that could not restore the ledger
485
496
  * still holds the carried events, and must say so instead of claiming a clean rollback.
486
497
  */
487
- function restoreLedger(ledgerPath, prior) {
498
+ function restoreLedger(ledgerPath, prior, appended) {
499
+ // Under the lock every ledger writer takes (appends, and the erasure's rewrite in lifecycle.ts), so no writer can
500
+ // change the ledger between the checks below and the restore.
501
+ return (0, durableAppend_1.withAppendLock)(ledgerPath, async () => restoreLedgerLocked(ledgerPath, prior, appended));
502
+ }
503
+ function restoreLedgerLocked(ledgerPath, prior, appended) {
488
504
  try {
505
+ // Compensation owns only the bytes it appended. If another lifecycle writer changed the
506
+ // ledger afterwards (especially by replacing a fact with an erase tombstone), restoring
507
+ // this import's older snapshot would resurrect that writer's deleted canonical content.
508
+ if (readFileOrNull(ledgerPath) !== appended) {
509
+ return { restored: false, error: 'the ledger changed while this import ran (another save, or an erasure); refusing to overwrite newer canonical memory' };
510
+ }
511
+ // The snapshot was read before the appends, without the lock. Only appends may have happened since: then the snapshot
512
+ // is a prefix of what this run left. Anything else (an erasure rewrote the ledger in between) makes the snapshot stale,
513
+ // and restoring it would write erased content back.
514
+ if (prior !== null && (appended === null || !appended.startsWith(prior))) {
515
+ return { restored: false, error: 'the ledger changed while this import ran (another save, or an erasure); refusing to overwrite newer canonical memory' };
516
+ }
489
517
  if (prior === null) {
490
518
  if (fs_1.default.existsSync(ledgerPath))
491
519
  fs_1.default.unlinkSync(ledgerPath);
@@ -325,6 +325,7 @@ async function run(argv) {
325
325
  warnings,
326
326
  };
327
327
  let exitCode = 0;
328
+ let applied;
328
329
  if (options.apply) {
329
330
  const result = await (0, apply_1.applyImportPlan)(plan, bundle, {
330
331
  memoryDir,
@@ -332,7 +333,7 @@ async function run(argv) {
332
333
  ledgerRelPath: options.ledgerRel,
333
334
  canonicalOnly: options.canonicalOnly,
334
335
  });
335
- summary.applied = result;
336
+ summary.applied = applied = result;
336
337
  summary.counts = {
337
338
  ...summary.counts,
338
339
  eventsAlreadyInDestination: result.eventsAlreadyPresent,
@@ -365,29 +366,29 @@ async function run(argv) {
365
366
  console.log(` ! ${plan.counts.chunksNotLocated} chunk(s) not locatable in the carried bytes; ${plan.counts.chunksSpanInherited} inherited the source span, the rest have an unknown span`);
366
367
  }
367
368
  console.log(` ledger events : ${plan.counts.eventsToAppend} new`
368
- + (options.apply ? ` (${summary.applied.eventsAppended} appended, ${summary.applied.eventsAlreadyPresent} already present)` : ''));
369
+ + (applied ? ` (${applied.eventsAppended} appended, ${applied.eventsAlreadyPresent} already present)` : ''));
369
370
  console.log(` facts : ${plan.counts.factsInBundle} current in source, ${plan.counts.eventsToAppend} carried, `
370
371
  + `${plan.counts.multiValuedFactsDropped} multi-valued dropped, ${plan.counts.factsSkipped} other skips`);
371
372
  for (const skip of plan.skipped.events) {
372
373
  console.log(` - ${skip.count} × ${skip.reason}`);
373
374
  }
374
- if (options.apply) {
375
- const applied = summary.applied;
375
+ if (applied) {
376
+ const dest = applied.destination;
376
377
  console.log(` files : ${applied.notesWritten} written, ${applied.notesUnchanged} already identical, ${applied.notesConflicting} conflicting (never overwritten or indexed)`);
377
- for (const conflict of (applied.conflicts ?? []).slice(0, 10)) {
378
+ for (const conflict of applied.conflicts.slice(0, 10)) {
378
379
  console.log(` ! conflict (left as-is, not indexed): ${conflict}`);
379
380
  }
380
- if ((applied.conflicts ?? []).length > 10) {
381
+ if (applied.conflicts.length > 10) {
381
382
  console.log(` ! …and ${applied.conflicts.length - 10} more`);
382
383
  }
383
384
  console.log(` index rows : ${applied.indexRows.notesInserted} notes, ${applied.indexRows.chunksInserted} chunks, ${applied.indexRows.entitiesInserted} entities inserted`);
384
385
  console.log(` replayed : ${applied.ledgerReplayed} ledger event(s) applied to the index`);
385
- console.log(` workspace now : notes ${applied.destination.notes} · chunks ${applied.destination.chunks} · entities ${applied.destination.entities} · facts ${applied.destination.facts} (${applied.destination.currentFacts} current)`);
386
+ console.log(` workspace now : notes ${dest?.notes} · chunks ${dest?.chunks} · entities ${dest?.entities} · facts ${dest?.facts} (${dest?.currentFacts} current)`);
386
387
  }
387
388
  for (const warning of warnings)
388
389
  console.log(` note : ${warning}`);
389
390
  }
390
- if (options.apply && summary.applied.notesConflicting > 0)
391
+ if (applied && applied.notesConflicting > 0)
391
392
  exitCode = 3;
392
393
  // Like a conflict, a note the guard left out is something the operator asked for that was not imported.
393
394
  const guardSkips = plan.skipped.secretGuard;
@@ -38,6 +38,8 @@ function readExistingStreams(dbPath, ledgerPath) {
38
38
  try {
39
39
  const content = fs_1.default.readFileSync(ledgerPath, 'utf8');
40
40
  for (const event of (0, lifecycle_1.parseLedgerEvents)(content)) {
41
+ if (event.kind === 'erase')
42
+ continue; // an erasure tombstone keeps no subject or predicate: it declares no stream
41
43
  streams.add((0, plan_1.streamKey)(event.subject, event.predicate, event.scope ?? 'global'));
42
44
  }
43
45
  }
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.EVENT_ID_RE = void 0;
7
6
  exports.deterministicEventId = deterministicEventId;
8
7
  exports.toCanonicalInstant = toCanonicalInstant;
9
8
  exports.tryNormalizeScope = tryNormalizeScope;
@@ -29,7 +28,6 @@ const scope_1 = require("../mvp/scope");
29
28
  * here: a validator that drifts looser fails at boot, and one that drifts stricter
30
29
  * silently loses memory.
31
30
  */
32
- exports.EVENT_ID_RE = /^mem_[A-Za-z0-9]+$/;
33
31
  /** Field limits as enforced by `lifecycle.ts` `validateField`. */
34
32
  const FIELD_LIMITS = { subject: 512, predicate: 256, object: 8192, source: 256 };
35
33
  const CONTROL_CHARS = /[\u0000-\u001F\u007F]/;
@@ -4,7 +4,6 @@ exports.IMPORT_ROOT = void 0;
4
4
  exports.streamKey = streamKey;
5
5
  exports.assertImportNamespace = assertImportNamespace;
6
6
  exports.buildImportPlan = buildImportPlan;
7
- exports.contentHash = contentHash;
8
7
  const hasher_1 = require("../indexer/hasher");
9
8
  const queryHelpers_1 = require("../retrieval/queryHelpers");
10
9
  const orchestrator_1 = require("../retrieval/orchestrator");
@@ -200,7 +199,3 @@ function buildImportPlan(bundle, options) {
200
199
  },
201
200
  };
202
201
  }
203
- /** The engine's own note change-detection hash, re-exported for callers. */
204
- function contentHash(content) {
205
- return (0, hasher_1.hashContent)(content);
206
- }
@@ -306,7 +306,7 @@ function readBundleSource(filePath, filter = { include: [], exclude: [] }) {
306
306
  sourcePath: String(record.sourcePath ?? ''),
307
307
  origin: record.origin === 'ledger' ? 'ledger' : 'note',
308
308
  eventId: typeof record.eventId === 'string' ? record.eventId : undefined,
309
- closedEvent: record.closedEvent ?? null,
309
+ closedEvent: (record.closedEvent ?? null), // carried unvalidated, as before
310
310
  ledgerSource: typeof record.ledgerSource === 'string' ? record.ledgerSource : undefined,
311
311
  });
312
312
  continue;
@@ -129,7 +129,10 @@ function parseNoteDetailed(filePath, content) {
129
129
  // the memory map's note view, the evidence pass). A daily note keeps a line of the user's words that would
130
130
  // start like an entry behind a backslash; the words are restored here, so none of them sees the escape.
131
131
  const text = (0, dailyNote_1.isDailyNotePath)(filePath) ? (0, dailyNote_1.decodeDailyNoteText)(content) : content;
132
- const { data: frontmatter, content: rawBody } = (0, gray_matter_1.default)(text, MATTER_OPTIONS);
132
+ const { data, content: rawBody } = (0, gray_matter_1.default)(text, MATTER_OPTIONS);
133
+ // Only a mapping is front matter. YAML also parses a list, a string, a number or a date as a document, and passing
134
+ // one on made the export write a list's items as keys `0`, `1`, ...
135
+ const frontmatter = Object.prototype.toString.call(data) === '[object Object]' ? data : {};
133
136
  assertFrontmatterBounded(frontmatter, content.length);
134
137
  const heading = rawBody.match(/^#\s+(.+)$/m);
135
138
  const title = heading?.[1].trim() ?? path_1.default.basename(filePath, '.md');