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
@@ -3,7 +3,8 @@ 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.MVP_VERSION_PATH = exports.MVP_MCP_PATH = exports.SEARCH_REFUSAL_REASONS = exports.sanitizeErrorMessage = exports.DIRECTORY_HEADER = exports.DASHBOARD_HEADER = void 0;
6
+ exports.MVP_VERSION_PATH = exports.MVP_MCP_PATH = exports.SEARCH_REFUSAL_REASONS = exports.sanitizeErrorMessage = exports.DIRECTORY_HEADER = exports.DASHBOARD_HEADER = exports.OUTPUT_SCHEMA_BOUND_NOTE = void 0;
7
+ exports.toolOutputSchemasEnabled = toolOutputSchemasEnabled;
7
8
  exports.createMvpApp = createMvpApp;
8
9
  exports.toolError = toolError;
9
10
  exports.startMvpService = startMvpService;
@@ -27,8 +28,10 @@ const limits_1 = require("../memory/limits");
27
28
  const sourceName_1 = require("../memory/sourceName");
28
29
  const secretGuard_1 = require("../extraction/secretGuard");
29
30
  const whatsNew_1 = require("../memory/whatsNew");
31
+ const handoff_1 = require("../memory/handoff");
30
32
  const requests_1 = require("../memory/requests");
31
33
  const history_1 = require("./history");
34
+ const directorySurface_1 = require("./directorySurface");
32
35
  const identity_1 = require("./identity");
33
36
  const serverCard_1 = require("./serverCard");
34
37
  const requestErrors_1 = require("../mcp/requestErrors");
@@ -48,11 +51,18 @@ const hitDates_1 = require("./hitDates");
48
51
  const replacements_1 = require("../memorymap/replacements");
49
52
  const contextRelevance_1 = require("./contextRelevance");
50
53
  const hitFloor_1 = require("./hitFloor");
54
+ const answerCheck_1 = require("./answerCheck");
51
55
  const readerLayout_1 = require("../retrieval/readerLayout");
52
56
  const questionSpan_1 = require("../retrieval/questionSpan");
53
57
  const readerEvents_1 = require("../retrieval/readerEvents");
54
58
  const orchestrator_1 = require("../retrieval/orchestrator");
55
59
  const buildIdentity_1 = require("../buildIdentity");
60
+ const tap_1 = require("../learning/tap");
61
+ const rankingTrace_1 = require("../retrieval/rankingTrace");
62
+ const examples_1 = require("../sharing/examples");
63
+ const fields_1 = require("../sharing/fields");
64
+ const send_1 = require("../sharing/send");
65
+ const state_1 = require("../sharing/state");
56
66
  const hostHeaderValidation_js_1 = require("@modelcontextprotocol/sdk/server/middleware/hostHeaderValidation.js");
57
67
  const apiAuth_1 = require("../api/apiAuth");
58
68
  const memoryDir_1 = require("./memoryDir");
@@ -60,15 +70,19 @@ const hosted_1 = require("../importing/hosted");
60
70
  const exportAll_1 = require("./exportAll");
61
71
  const memoryMap_1 = require("../memorymap/memoryMap");
62
72
  const idle_1 = require("./idle");
73
+ const dreamer_1 = require("../memory/dreamer/dreamer");
63
74
  const activity_1 = require("./activity");
64
75
  const storedSize_1 = require("./storedSize");
65
- const storedSize_2 = require("./storedSize");
66
76
  const sanitize_1 = require("./sanitize");
67
77
  Object.defineProperty(exports, "sanitizeErrorMessage", { enumerable: true, get: function () { return sanitize_1.sanitizeErrorMessage; } });
68
78
  const controls_1 = require("../memory/controls");
69
79
  const hiddenSpans_1 = require("./hiddenSpans");
70
80
  const connectionStatus_1 = require("./connectionStatus");
71
81
  const toolRefusal_1 = require("../toolRefusal");
82
+ const callPhases_1 = require("../callPhases");
83
+ const log_1 = require("./log");
84
+ const contextProfile_1 = require("./contextProfile");
85
+ const log = (0, log_1.createLogger)('engine');
72
86
  /**
73
87
  * Single-user ChatGPT MVP remote MCP service.
74
88
  *
@@ -86,10 +100,6 @@ const toolRefusal_1 = require("../toolRefusal");
86
100
  * serialized, validated, honest receipts, restart replay), so persistence,
87
101
  * correction and restart recovery inherit the Gate1A guarantees.
88
102
  */
89
- /**
90
- * What search_memory says of the digest table, only where write-time digests are switched on (RENOR_DIGESTS), so a
91
- * workspace without them has the description it always had.
92
- */
93
103
  /**
94
104
  * What search_memory says of the event lines, only where RENOR_READER_EVENTS is set to an on value (`1`, `true`, `yes`, `on`).
95
105
  * With it unset the lines still lead the page (the default) and say what they are in their own first words, but the tool list is
@@ -97,6 +107,10 @@ const toolRefusal_1 = require("../toolRefusal");
97
107
  * gateway's server card.
98
108
  */
99
109
  const EVENTS_HITS = ' When the question asks for the order or the date of something the user did, the first hit is a list of the user\'s own statements that match it, each under the day it is about, oldest first, and the excerpts stay in full below it (reason "events"). A question about a duration has no such list.';
110
+ /**
111
+ * What search_memory says of the digest table, only where write-time digests are switched on (RENOR_DIGESTS), so a
112
+ * workspace without them has the description it always had.
113
+ */
100
114
  const DIGEST_HITS = ' This workspace writes a dated digest of each saved note in the background: when the notes matched have digests, one table of dated items drawn from them leads the hits (after any evidence table), outside the rankedBy order and top_k, with reason "digest". It is read from stored digests, no model is called to make it, and a note saved a moment ago may not have its digest yet. Use the table when it holds the value needed, and the excerpts when it does not.';
101
115
  /**
102
116
  * What search_memory says of the duration line, only where RENOR_READER_DURATION is set to an on value (`1`, `true`, `yes`, `on`)
@@ -110,7 +124,6 @@ const SPAN_HITS = ' When the question asks how many days, weeks or months lie be
110
124
  */
111
125
  const TIMELINE_HITS = ' When the question asks what a value is now, the first hit is a timeline of the user\'s own statements of that value, oldest first, the last line marked latest, and the excerpts stay in full below it (reason "timeline"). A question about a duration or an order has no timeline.';
112
126
  const MCP_PATH = '/mcp';
113
- /** True when any message is a tools/call that omitted `id`. An explicit `id: null` still has the key. */
114
127
  /**
115
128
  * A request body must be UTF-8. Node decodes an invalid byte as U+FFFD, and the tool would store that, so the
116
129
  * body is refused before it is parsed. The error carries a 400 status, which body-parser keeps.
@@ -123,6 +136,7 @@ function requireUtf8Body(_req, _res, body) {
123
136
  throw Object.assign(new Error('request body is not valid UTF-8; nothing was run'), { status: 400, expose: true });
124
137
  }
125
138
  }
139
+ /** True when any message is a tools/call that omitted `id`. An explicit `id: null` still has the key. */
126
140
  function bodyHasToolsCallNotification(body) {
127
141
  const messages = Array.isArray(body) ? body : [body];
128
142
  return messages.some((message) => {
@@ -199,12 +213,53 @@ function isLocalDirectCall(req) {
199
213
  */
200
214
  const EVIDENCE_WAIT_STATES = ' This deployment waits for the evidence model only a bounded time, so two more states mean that this response alone has no table: "pending" (the extraction did not finish in time; it continues, and once its table is cached the same request over the same notes returns it) and "busy" (as many extractions were already waiting as the deployment allows, so none was started).';
201
215
  const EVIDENCE_DAILY_CAP_STATE = ' This workspace has a daily allowance of evidence-model calls, so one more state means that this response alone has no table: "capped" (the allowance is used up until 00:00 UTC; retrieval is unaffected and the hits are the whole result).';
216
+ /**
217
+ * RENOR_TOOL_OUTPUT_SCHEMAS (default off; `1` / `true` / `yes` / `on` turns it on): every tool lists its `outputSchema` and
218
+ * every answer also carries `structuredContent`. Off, the tools listing and every reply are what they were before the
219
+ * schemas existed. It is off by default because the structured form doubles the wire: each answer then carries its JSON
220
+ * twice (the text block and `structuredContent`), get_context's `max_tokens` bounds the text block alone, and the tools
221
+ * listing grows by the schemas (about +47%). Read once per server, so the listing and the replies always agree.
222
+ */
223
+ function toolOutputSchemasEnabled(env = process.env) {
224
+ const v = (env.RENOR_TOOL_OUTPUT_SCHEMAS ?? '').trim().toLowerCase();
225
+ return v === '1' || v === 'true' || v === 'yes' || v === 'on';
226
+ }
227
+ /** A tool's registration with its outputSchema only when output schemas are on: off, the tool lists none, as before. */
228
+ function withOutputSchemaIf(on, config) {
229
+ if (on || config.outputSchema === undefined)
230
+ return config;
231
+ const { outputSchema: _dropped, ...rest } = config;
232
+ return rest;
233
+ }
234
+ /**
235
+ * With output schemas on, get_context's bound covers its text block, and structuredContent repeats the same JSON: a client
236
+ * that passes both to a model pays about twice the bound (measured on #680: 703 tokens of text, 1,446 on the wire).
237
+ */
238
+ exports.OUTPUT_SCHEMA_BOUND_NOTE = ' With output schemas on, max_tokens bounds the text block; structuredContent carries the same JSON again.';
202
239
  /**
203
240
  * Every tool answers in the same shape: its JSON on one line. Indentation is whitespace an agent pays for in tokens on
204
241
  * every call and reads nothing from; indented, a get_context answer was about a fifth larger.
242
+ *
243
+ * The text block is the JSON with its hidden characters written as \uXXXX escapes (escapeInvisibleControls), so no
244
+ * client is handed one raw in text. With output schemas on (RENOR_TOOL_OUTPUT_SCHEMAS), `structuredContent` is exactly
245
+ * JSON.parse of that text block: the decoded value, as any client that parses the text block reads it. Escaping is a
246
+ * property of JSON text and is never applied to a decoded value, which would turn every newline, tab and emoji joiner
247
+ * of a note into the literal text "\u000a" (review of #680, HIGH 1).
205
248
  */
206
- function jsonResult(body) {
207
- return { content: [{ type: 'text', text: (0, sanitize_1.escapeInvisibleControls)(JSON.stringify(body)) }] };
249
+ function jsonResultOf(body, structured) {
250
+ const text = (0, sanitize_1.escapeInvisibleControls)(JSON.stringify(body));
251
+ return {
252
+ content: [{ type: 'text', text }],
253
+ ...(structured ? { structuredContent: JSON.parse(text) } : {}),
254
+ };
255
+ }
256
+ /**
257
+ * A refusal: the same JSON text a client already reads, as a plain isError result. A refusal carries no
258
+ * structuredContent, since the output schema describes a tool's answer and a refusal is not one (the SDK does not
259
+ * check an isError result against it either).
260
+ */
261
+ function refusalResult(body) {
262
+ return { content: [{ type: 'text', text: (0, sanitize_1.escapeInvisibleControls)(JSON.stringify(body)) }], isError: true };
208
263
  }
209
264
  /**
210
265
  * An optional argument that a client may send as JSON null, which is how some clients spell an argument they leave out:
@@ -238,33 +293,31 @@ const REPLACES_ARG = omittedWhenBlank(zod_1.z.string().max(1024).optional()).des
238
293
  const SCOPE_ARG = omittedWhenBlank(zod_1.z.string().optional()).describe('"global" (default) or project:<slug>, e.g. project:atlas.');
239
294
  /** What a client is told a `source` is: the name of the app or agent saving, never a path (memory/sourceName.ts). */
240
295
  const SOURCE_ARG_DESCRIPTION = 'Name of the app or agent saving this, for example claude-code. Leave it out to use the connecting client. Never a file path or document name.';
241
- const RESERVED_SOURCE_IDENTITIES = new Set(['system', 'developer', 'assistant', 'renor', 'admin', 'owner', 'tool']);
296
+ /**
297
+ * Shared pieces of the tools' declared output schemas (MCP `outputSchema`). Every hosted tool declares the object it
298
+ * already returns: the SDK validates the result's `structuredContent` against it, and clients read that as the
299
+ * structured result. A field a reply does not always carry is optional here, so a declared schema never refuses a
300
+ * reply the tool already produced. Each top level is a loose object (additional properties allowed): a client that
301
+ * validates against the schema it listed keeps accepting replies after a later release adds a field.
302
+ */
303
+ /** An index pass a save or index run reports: rows walked, indexed, failed. */
304
+ const OUT_INDEX_PASS = zod_1.z.object({ total: zod_1.z.number(), indexed: zod_1.z.number(), errors: zod_1.z.number(), failure: zod_1.z.string().optional() }).optional();
305
+ /** The fact a save created: its id (null when it was already stored), its value and when it became valid. */
306
+ const OUT_CREATED_FACT = zod_1.z.object({ factId: zod_1.z.number().nullable(), object: zod_1.z.string(), validFrom: zod_1.z.string() }).nullable().optional();
307
+ /** The fact a superseding save closed. */
308
+ const OUT_CLOSED_FACT = zod_1.z.object({ factId: zod_1.z.number(), object: zod_1.z.string(), validTo: zod_1.z.string() }).nullable().optional();
309
+ /** A current value that may be the same fact under another name or scope (advisory, never merged). */
310
+ const OUT_DUPLICATES = zod_1.z.array(zod_1.z.object({
311
+ subject: zod_1.z.string(), object: zod_1.z.string(), scope: zod_1.z.string(), validFrom: zod_1.z.string(),
312
+ source: zod_1.z.string().optional(), untrusted: zod_1.z.literal(true).optional(),
313
+ })).optional();
314
+ /** What a `replaces` link did: the correction recorded, or why none was made. */
315
+ const OUT_REPLACEMENT = zod_1.z.object({ id: zod_1.z.string(), by: zod_1.z.string() }).optional();
316
+ /** One hit/excerpt a search or read returns; its own keys vary by reason, so only its object shape is fixed. */
317
+ const OUT_HIT = zod_1.z.record(zod_1.z.string(), zod_1.z.unknown());
242
318
  const CALLER_SOURCE_ASCII = /^[A-Za-z0-9 _.:/@-]+$/;
243
319
  /** A User-Agent product keeps its punctuation (cleanSource folds it) but may hold no character outside printable ASCII. */
244
320
  const USER_AGENT_PRODUCT_ASCII = /^[\x20-\x7e]+$/;
245
- const SOURCE_IDENTITY_SEPARATORS = /[ ._-]/g;
246
- /** Return the reserved identity a writer spelling claims, including the import-style prefix form. */
247
- function reservedSourceIdentity(source) {
248
- const lower = source.toLowerCase();
249
- const identity = lower.replace(SOURCE_IDENTITY_SEPARATORS, '');
250
- const colon = lower.indexOf(':');
251
- const authorityPrefix = colon < 0 ? null : lower.slice(0, colon).replace(SOURCE_IDENTITY_SEPARATORS, '');
252
- return RESERVED_SOURCE_IDENTITIES.has(identity)
253
- ? identity
254
- : authorityPrefix !== null && RESERVED_SOURCE_IDENTITIES.has(authorityPrefix) ? authorityPrefix : null;
255
- }
256
- /**
257
- * The reserved identity a writer name would store as, under any spelling a write persists: the typed one, the fact's
258
- * cleanSource and the note's headerSource. Paths, URLs, truncation and punctuation folding cannot manufacture one past this.
259
- */
260
- function reservedSourceIn(source) {
261
- for (const form of [source, (0, sourceName_1.cleanSource)(source), (0, sourceName_1.headerSource)(source)]) {
262
- const reserved = reservedSourceIdentity(form);
263
- if (reserved)
264
- return reserved;
265
- }
266
- return null;
267
- }
268
321
  /** Validate an untrusted writer name and every spelling either writer can persist. */
269
322
  function callerSource(source) {
270
323
  // Check what the caller actually supplied. Normalizing first would let fullwidth and other compatibility characters
@@ -272,7 +325,7 @@ function callerSource(source) {
272
325
  if (!CALLER_SOURCE_ASCII.test(source)) {
273
326
  throw (0, toolRefusal_1.refusal)('invalid_argument', 'writer names must be plain ASCII (letters, digits, spaces, and - _ . : / @ only)');
274
327
  }
275
- const reserved = reservedSourceIn(source);
328
+ const reserved = (0, sourceName_1.reservedCallerSource)(source);
276
329
  if (reserved)
277
330
  throw (0, toolRefusal_1.refusal)('invalid_argument', `source names a reserved identity (${reserved}); use an ordinary app or agent name, or leave source out`);
278
331
  return source;
@@ -281,14 +334,26 @@ function markImportedOrigin(item, pathKey) {
281
334
  return String(item[pathKey] ?? '').startsWith('Imported/') ? { ...item, origin: 'import' } : item;
282
335
  }
283
336
  /**
284
- * The source a hosted write stores. The connection's name (`x-renor-agent` behind the gateway, the
285
- * verified OAuth client's name on self-hosted) as its hive identity, or "unnamed" when that connection
286
- * has no name a header can render — an unnamed connection never takes a name the client typed;
287
- * otherwise the one the client sent when it names someone; otherwise the product in the User-Agent
288
- * (`claude-code/2.1.289` is `claude-code`), so an agent never has to pass one;
289
- * otherwise undefined, which leaves the tool's own default. A source with no letter or digit in it (blank, a bare path,
290
- * punctuation) names no one (memory/sourceName.ts).
337
+ * A memory_map or read_memory memory as a read shows it: imported (origin), and a stored source that is a reserved identity
338
+ * (an entry saved before the rule, or written into a file by hand) marked `untrusted`, a claim and not who saved it.
291
339
  */
340
+ function markStoredMemory(item) {
341
+ const marked = markImportedOrigin(item, 'path');
342
+ return (0, sourceName_1.isReservedSource)(marked.source) && marked.untrusted !== true ? { ...marked, untrusted: true } : marked;
343
+ }
344
+ /**
345
+ * A search_memory or search_text hit as a read shows it: imported (origin), and `untrusted` when it is a daily-log chunk an
346
+ * entry of which claims a reserved source (factMarkers.ts claimsReservedEntrySource), as get_context marks the same chunk.
347
+ */
348
+ function markStoredHit(hit) {
349
+ const marked = markImportedOrigin(hit, 'notePath');
350
+ return marked.untrusted !== true && (0, factMarkers_1.claimsReservedEntrySource)(marked.notePath, marked.text) ? { ...marked, untrusted: true } : marked;
351
+ }
352
+ /** A hit's score as an agent is shown it: six decimals. The 17 digits of a double cost about 4 tokens a hit and say nothing the order does not. */
353
+ function shortScore(hit) {
354
+ return typeof hit.score === 'number' ? { ...hit, score: (0, agentMemory_1.scoreForReply)(hit.score) } : hit;
355
+ }
356
+ /** A request header's first value, when it is a string. */
292
357
  function headerText(headers, name) {
293
358
  const raw = headers?.[name];
294
359
  const value = Array.isArray(raw) ? raw[0] : raw;
@@ -302,21 +367,34 @@ function trustedAgentRaw(extra, config) {
302
367
  return typeof agent === 'string' ? agent : undefined;
303
368
  }
304
369
  /**
305
- * The hive identity of a trusted connection name, or "unnamed". A name that is a reserved identity is no name: a key label
306
- * or a self-hosted OAuth client_name (which the client registers itself) must not make its saves read as `owner` or
307
- * `system`, as a typed source or a User-Agent cannot. A hosted OAuth sign-in is `OAuth-<client>`, which never is one.
370
+ * The hive identity of a trusted connection name, or "unnamed". A name that is a reserved identity is no name, so its saves
371
+ * cannot read as `owner` or `system`, as a typed source or a User-Agent cannot. `clientChosen` is a self-hosted OAuth
372
+ * client_name, which the client registers itself: it is held to the caller rule (a reserved first word, digit look-alikes).
373
+ * A gateway key's name is the owner's, chosen in the dashboard: only an exact reserved identity is refused, so `Developer
374
+ * Laptop` or `Renor CLI` keeps its name. A hosted OAuth sign-in is `OAuth-<client>`, which never is one.
308
375
  */
309
- function trustedIdentity(raw) {
376
+ function trustedIdentity(raw, clientChosen) {
310
377
  const identity = (0, sourceName_1.hiveIdentity)(raw);
311
- return identity === null || reservedSourceIn(identity) !== null ? 'unnamed' : identity;
378
+ if (identity === null)
379
+ return 'unnamed';
380
+ return (clientChosen ? (0, sourceName_1.reservedCallerSource)(identity) : (0, sourceName_1.reservedSourceIn)(identity)) !== null ? 'unnamed' : identity;
312
381
  }
382
+ /**
383
+ * The source a hosted write stores. The connection's name (`x-renor-agent` behind the gateway, the
384
+ * verified OAuth client's name on self-hosted) as its hive identity, or "unnamed" when that connection
385
+ * has no name a header can render — an unnamed connection never takes a name the client typed;
386
+ * otherwise the one the client sent when it names someone; otherwise the product in the User-Agent
387
+ * (`claude-code/2.1.289` is `claude-code`), so an agent never has to pass one;
388
+ * otherwise undefined, which leaves the tool's own default. A source with no letter or digit in it (blank, a bare path,
389
+ * punctuation) names no one (memory/sourceName.ts).
390
+ */
313
391
  function sourceOf(sent, extra, config) {
314
392
  // The gateway sets this from the connection key's name and overwrites a client-sent copy, so on a named
315
393
  // connection it is the author: a `source` the client types cannot write as another agent.
316
394
  // A present name that canonicalizes to no identity (punctuation-only, or `unnamed`) cannot take a typed source.
317
395
  const raw = trustedAgentRaw(extra, config);
318
396
  if (typeof raw === 'string' && raw.trim() !== '')
319
- return trustedIdentity(raw);
397
+ return trustedIdentity(raw, !config.gatewaySecret);
320
398
  // Self-hosted always resolves a name for the verified client (the sign-in middleware sets the key, even
321
399
  // when the stored name renders empty). That connection is unnamed, and an unnamed connection cannot
322
400
  // choose an author, exactly as an unnamed stdio connection cannot.
@@ -331,7 +409,7 @@ function sourceOf(sent, extra, config) {
331
409
  // A reserved product (`system/1.0`, `owner/2 (x)`, `https:system`) or a non-ASCII one (a lookalike such as `SYSTEΜ/1.0`) falls back
332
410
  // to the tool's default instead of storing it.
333
411
  const product = (0, sourceName_1.productFromUserAgent)(extra?.requestInfo?.headers?.['user-agent']);
334
- return product !== undefined && USER_AGENT_PRODUCT_ASCII.test(product) && reservedSourceIn(product) === null ? product : undefined;
412
+ return product !== undefined && USER_AGENT_PRODUCT_ASCII.test(product) && (0, sourceName_1.reservedCallerSource)(product) === null ? product : undefined;
335
413
  }
336
414
  /**
337
415
  * Hosted names come only from the trusted gateway. Self-hosted names come only from
@@ -342,7 +420,7 @@ function agentOf(extra, config) {
342
420
  const raw = trustedAgentRaw(extra, config);
343
421
  if (typeof raw !== 'string' || raw.trim() === '')
344
422
  return 'unnamed';
345
- return trustedIdentity(raw);
423
+ return trustedIdentity(raw, !config.gatewaySecret);
346
424
  }
347
425
  /** Nameless connections must not share a feed, mark or inbox. */
348
426
  function hiveAgentOf(extra, tool, config) {
@@ -377,13 +455,20 @@ function isDirectoryCall(headers, config) {
377
455
  * What a directory client is told about the memory. It says nothing about call order or behaviour: the directory rules forbid
378
456
  * that for other tools. Named agent keys keep the instructions built below.
379
457
  */
380
- const DIRECTORY_INSTRUCTIONS = 'Renor single-owner memory: the owner\'s own saved memory, kept in their private workspace. get_context returns the current facts and the saved memories that match a request. remember saves one statement, and save_memory saves a value with its subject, predicate and object.';
458
+ const DIRECTORY_INSTRUCTIONS = 'Renor single-owner memory: the owner\'s own saved memory, kept in their private workspace. get_context returns the current facts and the saved memories that match a request. remember saves one statement, or a value with its subject, predicate and object.';
459
+ /** A tool's config with its title copied into annotations.title, where the Claude directory reads a display name from. */
460
+ function withAnnotationTitle(config) {
461
+ if (typeof config?.title !== 'string' || config.annotations?.title !== undefined)
462
+ return config;
463
+ return { ...config, annotations: { ...config.annotations, title: config.title } };
464
+ }
381
465
  /**
382
- * The one tool only the dashboard may list or run: it erases retired memories for good. Its registerTool call below spells the name
383
- * as a literal, because the docs binding and the hosted-tool tests read the registry off the literal call sites; if the two ever
384
- * differ, registeredToolEntry throws when an agent's server is built.
466
+ * The tools only the dashboard may list or run: one erases retired memories for good, the other reads and sets the workspace's
467
+ * choice to share learning data. Each registerTool call below spells its name as a literal, because the docs binding and the
468
+ * hosted-tool tests read the registry off the literal call sites; if the two ever differ, registeredToolEntry throws when an
469
+ * agent's server is built.
385
470
  */
386
- const OWNER_ONLY_TOOL = 'delete_retired_memory';
471
+ const OWNER_ONLY_TOOLS = ['delete_retired_memory', 'sharing_settings'];
387
472
  /** The tools agents use to reach each other. A directory client is offered none of them; registeredToolEntry throws if one is renamed. */
388
473
  const HIVE_TOOLS = ['whats_new', 'ask_agent', 'list_requests', 'answer_request', 'close_request'];
389
474
  /**
@@ -401,24 +486,34 @@ function registeredToolEntry(server, name) {
401
486
  * binds one transport per Protocol instance, so SSE sessions each get
402
487
  * their own server (bounded, in-process); JSON mode shares one.
403
488
  */
404
- function buildMcpServer(db, config, engine, idle, digests, usefulness, connection, announceFirst, telemetrySession = null, dashboard = false, directory = false) {
489
+ function buildMcpServer(db, config, engine, idle, digests, usefulness, connection, announceFirst, telemetrySession = null, dashboard = false, directory = false, dreamer = null) {
490
+ // RENOR_TOOL_OUTPUT_SCHEMAS, read once for this server: its tools list an outputSchema exactly when its answers carry
491
+ // structuredContent.
492
+ const withSchemas = toolOutputSchemasEnabled();
493
+ const jsonResult = (body) => jsonResultOf(body, withSchemas);
405
494
  // Which Renor this is, on the first reply of a session and on every error (connectionStatus.ts); nothing in a workspace
406
495
  // that is not hosted. A server that serves no session of its own (the stateless one, or one built for a session this
407
496
  // process forgot) has no first reply to announce on.
408
497
  const announcer = new connectionStatus_1.ConnectionAnnouncer(connection, announceFirst);
409
498
  const evidence = (0, engine_1.resolveEngineEvidence)(config, process.env, idle);
499
+ // RENOR_ANSWER_CHECK (default off): null unless armed and the evidence pass has a key; then a relevant verdict a model
500
+ // reads as unanswered turns false. One per app, so every session shares its answers.
501
+ const answerChecker = (0, answerCheck_1.answerCheckerFor)(config, process.env);
410
502
  // The reading agent's name as a save records it (remember's cleanSource and default), so its own facts carry no source.
411
503
  const readerOf = (extra) => (0, sourceName_1.cleanSource)(sourceOf(undefined, extra, config) ?? '') || 'agent';
412
504
  // Only told to agents while get_context can actually return the signal.
413
505
  const relevanceGuidance = (0, contextRelevance_1.contextRelevanceArmed)(engine) ? contextRelevance_1.CONTEXT_RELEVANCE_GUIDANCE : '';
414
506
  const relevanceKeepsMatch = relevanceGuidance ? contextRelevance_1.CONTEXT_RELEVANCE_KEEPS_MATCH : '';
415
507
  const budgetKeepsNewest = relevanceGuidance ? contextRelevance_1.CONTEXT_BUDGET_KEEPS_NEWEST : ''; // where there is a bar, the budget reads it
416
- const server = new mcp_js_1.McpServer({ name: serverCard_1.SERVER_NAME, version: serverCard_1.SERVER_VERSION }, { instructions: directory ? DIRECTORY_INSTRUCTIONS : 'Renor single-owner memory. At the start of every task, call whats_new before anything else. Open requests addressed to you are first in that reply; answering or closing one records the reply and never runs anything by itself. For substantive user requests, call get_context with the current request before answering so relevant prior context can follow the user across agents. '
508
+ // RENOR_MCP_* (contextProfile.ts, every lever off by default): a benchmark arm's tool subset, descriptions, instructions and
509
+ // reply shape. A directory client keeps the surface the directory rules fix.
510
+ const profile = directory ? {} : (0, contextProfile_1.contextProfileFromEnv)();
511
+ const server = new mcp_js_1.McpServer({ name: serverCard_1.SERVER_NAME, version: serverCard_1.SERVER_VERSION, icons: serverCard_1.SERVER_ICONS }, { instructions: directory ? DIRECTORY_INSTRUCTIONS : profile.instructions ?? ('Renor single-owner memory. At the start of every task, call whats_new before anything else. Open requests addressed to you are first in that reply; answering or closing one records the reply and never runs anything by itself. For substantive user requests, call get_context with the current request before answering so relevant prior context can follow the user across agents. '
417
512
  + 'Call it again before you decide something an earlier session may have settled and before you review another agent\'s work, with that choice or topic as the query: reading once at the start is not enough. '
418
513
  + 'To save anything, call remember with one argument, statement: the thing to remember, in plain words. When the user says to remember or save something, they mean this tool, not your own memory. '
419
514
  + 'Renor is the place for the why, next to your board or tracker, not a copy of it: when you save on your own, save decisions with their reason, root causes and corrections, not status, progress or event logs. '
420
515
  + 'Cite an earlier entry by its subject or task instead of restating it, and save nothing that only repeats one. '
421
- + 'Only for a value that stays current until it changes (a city, a database, a deadline, a preference), also pass subject, predicate and object together, never guessed: saving the same subject, predicate and scope again replaces the value and keeps the old one in history. save_memory is that structured form alone; remember_note appends a line to a day\'s log.' + relevanceGuidance });
516
+ + 'Only for a value that stays current until it changes (a city, a database, a deadline, a preference), also pass subject, predicate and object together, never guessed: saving the same subject, predicate and scope again replaces the value and keeps the old one in history. save_memory is that structured form alone; remember_note appends a line to a day\'s log.' + relevanceGuidance) });
422
517
  // PostHog MCP Analytics: per-server (SSE builds a fresh McpServer per session).
423
518
  (0, posthogMcp_1.instrumentMcpServer)(server);
424
519
  // Only the server of one SSE session is handed a telemetrySession: the JSON-mode server is shared by every client, so
@@ -444,18 +539,38 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
444
539
  function toolResultOk(result) {
445
540
  return !(result && result.isError);
446
541
  }
542
+ // The learning log (RENOR_LEARNING_LOG, src/learning): null unless it is on, and then one record per successful call of the
543
+ // tools it covers. The observer reads the reply before it is decorated and never changes it.
544
+ const learner = (0, tap_1.learningObserver)(config.learning, {
545
+ actor: dashboard ? 'dashboard' : 'agent',
546
+ session: telemetrySession,
547
+ model: () => engine.modelKey ?? null,
548
+ agent: extra => agentOf(extra, config),
549
+ // The owner's choice to share, read at each call: recording turns on when they choose a level and off when they choose off.
550
+ sharing: config.sharingDir === undefined ? undefined : (0, examples_1.sharingHook)(config.sharingDir),
551
+ });
447
552
  /** Privacy-safe: tool name / ok / ms and, on a refusal, its code from the closed set — never args or memory content. */
448
553
  function tracedTool(tool, handler) {
449
554
  // On-demand: a running tool call (a write, an index job) holds the engine awake
450
555
  // even after its client disconnects.
451
- const run = idle ? (...args) => idle.track(() => handler(...args)) : handler;
452
- return async (...args) => {
556
+ const run = idle ? (...args) => idle.track(async () => handler(...args)) : handler;
557
+ const traced = async (...args) => {
453
558
  const started = Date.now();
454
559
  const first = announcer.takeFirst();
455
560
  noteFirstCall(tool);
561
+ // The two reads also say where their time went (callPhases.ts), on this same line; every other tool is as it was.
562
+ const phases = (0, callPhases_1.isPhasedTool)(tool) ? new callPhases_1.CallPhases() : undefined;
563
+ const phasesOf = () => {
564
+ const report = phases?.report();
565
+ return report && Object.keys(report).length > 0 ? { phases: report } : {};
566
+ };
456
567
  try {
457
- const result = await run(...args);
568
+ // The ranker's terms are collected only for the learning log (rankingTrace.ts): with the log off no scope is opened.
569
+ const trace = learner ? new rankingTrace_1.RankingTrace(config.memoryDir) : undefined;
570
+ const invoke = () => (phases ? phases.run(() => run(...args)) : run(...args));
571
+ const result = await (trace ? (0, rankingTrace_1.withRankingTrace)(trace, invoke) : invoke());
458
572
  const ok = toolResultOk(result);
573
+ learner?.observe(tool, args[0], result, ok, Date.now() - started, args[1], trace);
459
574
  (0, telemetry_1.emitTelemetry)({
460
575
  event: 'tool_use',
461
576
  tool,
@@ -464,6 +579,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
464
579
  // error_kind is unchanged (dashboards read it); error_reason is what the refusal was: the code the
465
580
  // refusal site named, or the generic `refused` for one that has no better code in the closed set.
466
581
  ...(ok ? {} : { error_kind: 'other', error_reason: (0, toolRefusal_1.toolRefusalOf)(result) ?? 'refused' }),
582
+ ...phasesOf(),
467
583
  });
468
584
  return announcer.decorate(result, first);
469
585
  }
@@ -475,20 +591,42 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
475
591
  ms: Date.now() - started,
476
592
  error_kind: 'internal',
477
593
  error_reason: 'internal',
594
+ ...phasesOf(),
478
595
  });
479
596
  throw err;
480
597
  }
481
598
  };
599
+ // Same arguments and the same result, awaited: the SDK awaits a tool's result whether or not the handler is async.
600
+ return traced;
601
+ }
602
+ /**
603
+ * A note save's row in write_events, which whats_new and memory_map's events read (delete_retired_memory takes it out
604
+ * again by path and this excerpt). Both note saves write it: remember with only a statement, and remember_note. The
605
+ * line keeps the engine's own default source; only the feed's author falls back to unnamed. Best effort: the note is
606
+ * already on disk, so a failure here must not answer an error the agent would retry into a duplicate line.
607
+ */
608
+ function recordNoteSave(file, text, source, scope) {
609
+ try {
610
+ db.prepare('INSERT INTO write_events (note_path, action, summary, created_at, author, scope) VALUES (?, ?, ?, ?, ?, ?)')
611
+ .run(file, 'note', text.slice(0, 240), new Date().toISOString(), source ?? 'unnamed', scope);
612
+ }
613
+ catch (err) {
614
+ log.error('whats_new_row_failed', { detail: 'the note save was written' }, err);
615
+ }
482
616
  }
483
617
  const registerTool = server.registerTool.bind(server);
484
- server.registerTool = ((name, config, cb) => registerTool(name, config, tracedTool(String(name), cb)));
618
+ // The title also goes into annotations.title: the Claude directory reads a tool's display name from there and flags every tool
619
+ // without one.
620
+ const tracedRegisterTool = (name, config, cb) => registerTool(name, withAnnotationTitle(withOutputSchemaIf(withSchemas, config)), tracedTool(name, cb));
621
+ server.registerTool = tracedRegisterTool;
485
622
  server.registerTool('remember', {
486
623
  title: 'Remember something naturally',
487
624
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: false },
488
625
  description: 'Save something to memory: pass `statement`, in plain words, and nothing else. Do not save passwords, API keys or tokens. Example: remember({"statement": "We moved the Fjord database to SQLite because it needs no server."}). It is stored whole and indexed at once, so get_context finds it right away; the receipt\'s id is what read_memory and retire_memory take. `scope` ("project:<slug>") keeps it with one project. '
489
626
  + 'Only for a value that stays current until it changes (a city, a project\'s database, a deadline, a preference), also pass subject, predicate and object together: "subject": "Fjord", "predicate": "database", "object": "SQLite". Saving the same subject, predicate and scope again replaces the value: the old one leaves current reads and stays in history. A save by a different agent than the one that saved the current value is shown in reads as a CONFLICT line until that agent saves it again (see save_memory). Spell them the same every time. Decisions, events and notes need none of the three: leave them out, never guess them. '
490
627
  + `The same statement for the same fact again changes nothing. A statement over ${limits_1.MAX_STATEMENT_LENGTH} characters is refused, never cut, in either form. Saved on its own as a note, one that is not valid Unicode or holds a control character (U+0000 to U+001F except tab, newline and carriage return, or U+007F) is refused and nothing is written; with subject, predicate and object it is saved as a fact with its control characters escaped in the ledger, and only those three fields refuse them. A fact receipt may list possibleDuplicates (the same predicate under a name one word longer or shorter, or in another scope): nothing is merged; to correct one, save again with its subject and scope. A save is compared within the scope it names.`
491
- + ' To correct one specific earlier note or log entry (not a fact), also pass `replaces`, its id (from memory_map with a query, or an earlier save\'s receipt): that memory is then marked replaced, with a pointer to this save, in every recall, and this save is listed before it. Nothing is deleted; retire_memory on this save undoes it, and the receipt says `replaced` (or `replacesIgnored` when linking would make a loop, as when the save is the same line as the memory it replaces).',
628
+ + ' To correct one specific earlier note or log entry (not a fact), also pass `replaces`, its id (from memory_map with a query, or an earlier save\'s receipt): that memory is then marked replaced, with a pointer to this save, in every recall, and this save is listed before it. Nothing is deleted; retire_memory on this save undoes it, and the receipt says `replaced` (or `replacesIgnored` when linking would make a loop, as when the save is the same line as the memory it replaces). '
629
+ + 'Sent with subject, predicate and object as well, replaces naming a note or entry makes the save that plain-words correction (`factFieldsIgnored`), and replaces naming a fact is dropped while the fact is saved (`replacesIgnored`).',
492
630
  inputSchema: {
493
631
  statement: zod_1.z.string().min(1).max(limits_1.MAX_STATEMENT_LENGTH).describe('The thing to remember, in plain words.'),
494
632
  subject: omittedWhenBlank(zod_1.z.string().optional()).describe('With predicate and object: who or what the value is about, e.g. "Fjord".'),
@@ -500,6 +638,29 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
500
638
  cites: CITES_ARG,
501
639
  replaces: REPLACES_ARG,
502
640
  },
641
+ outputSchema: zod_1.z.looseObject({
642
+ ok: zod_1.z.boolean(),
643
+ mode: zod_1.z.enum(['fact', 'note']),
644
+ statement: zod_1.z.string(),
645
+ action: zod_1.z.enum(['store', 'supersede', 'nochange']).optional(),
646
+ eventId: zod_1.z.string().optional(),
647
+ subject: zod_1.z.string().optional(),
648
+ predicate: zod_1.z.string().optional(),
649
+ closed: OUT_CLOSED_FACT,
650
+ alsoClosed: OUT_CLOSED_FACT,
651
+ created: OUT_CREATED_FACT,
652
+ scope: zod_1.z.string().optional(),
653
+ possibleDuplicates: OUT_DUPLICATES,
654
+ possibleDuplicatesTotal: zod_1.z.number().optional(),
655
+ file: zod_1.z.string().optional(),
656
+ id: zod_1.z.string().optional(),
657
+ replaced: OUT_REPLACEMENT,
658
+ replacesIgnored: zod_1.z.string().optional(),
659
+ indexed: zod_1.z.boolean().optional(),
660
+ index: OUT_INDEX_PASS,
661
+ warning: zod_1.z.string().optional(),
662
+ detail: zod_1.z.string().optional(),
663
+ }),
503
664
  }, async (args, extra) => {
504
665
  try {
505
666
  const structured = [args.subject, args.predicate, args.object];
@@ -507,13 +668,26 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
507
668
  if (present !== 0 && present !== 3) {
508
669
  throw (0, toolRefusal_1.refusal)('invalid_argument', 'remember requires subject, predicate, and object together, or none of them: to save plain words pass only statement');
509
670
  }
510
- if (args.replaces !== undefined) {
511
- if (present === 3) {
512
- throw (0, toolRefusal_1.refusal)('invalid_argument', 'replaces is for a plain-words correction: a fact is corrected by saving it again with the same subject, predicate and scope');
671
+ // A client may send a value's subject, predicate and object together with replaces. One of the two is kept rather than the
672
+ // call refused, so the save lands on the first try: replaces naming a fact is dropped (a fact is corrected by saving it
673
+ // again, which this save does), and replaces naming a note or log entry wins (the save becomes a plain-words correction
674
+ // of it, which is the only way that note is marked replaced). The receipt says which.
675
+ let replaces = args.replaces;
676
+ let asFact = present === 3;
677
+ let adjusted = {};
678
+ if (replaces !== undefined && asFact) {
679
+ if (replaces.startsWith('fact:')) {
680
+ replaces = undefined;
681
+ adjusted = { replacesIgnored: 'replaces named a fact: a fact is corrected by saving it again with the same subject, predicate and scope, which this save did' };
682
+ }
683
+ else {
684
+ asFact = false;
685
+ adjusted = { factFieldsIgnored: 'replaces named a note or log entry, so this save is a plain-words correction of it; subject, predicate and object were not saved as a fact' };
513
686
  }
514
- await (0, replacements_1.assertReplaceable)(mapContext(), args.replaces, (0, scope_1.normalizeScope)(args.scope));
515
687
  }
516
- if (present === 3) {
688
+ if (replaces !== undefined)
689
+ await (0, replacements_1.assertReplaceable)(mapContext(), replaces, (0, scope_1.normalizeScope)(args.scope));
690
+ if (asFact) {
517
691
  const receipt = await (0, lifecycle_1.remember)(db, config.memoryDir, config.ledgerRelPath, {
518
692
  subject: args.subject,
519
693
  predicate: args.predicate,
@@ -535,6 +709,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
535
709
  // FULL_VAULT_CHECK_MS (see engineIndexChanged).
536
710
  let refresh = {};
537
711
  if (receipt.action !== 'nochange') {
712
+ dreamer?.noteFactSave();
538
713
  try {
539
714
  const pass = await (0, engine_1.engineIndexChanged)(db, config, engine, undefined, {
540
715
  interactive: true,
@@ -568,14 +743,17 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
568
743
  scope: (0, scope_1.normalizeScope)(args.scope),
569
744
  ...(duplicates.values.length > 0 ? { possibleDuplicates: duplicates.values } : {}),
570
745
  ...(duplicates.total > duplicates.values.length ? { possibleDuplicatesTotal: duplicates.total } : {}),
746
+ ...adjusted,
571
747
  ...refresh,
572
748
  });
573
749
  }
574
- const note = await (0, engine_1.engineRememberNote)(config, args.statement, undefined, sourceOf(args.source, extra, config) ?? 'mcp:remember', args.scope, args.timezone);
750
+ const source = sourceOf(args.source, extra, config);
751
+ const note = await (0, engine_1.engineRememberNote)(config, args.statement, undefined, source ?? 'mcp:remember', args.scope, args.timezone);
752
+ recordNoteSave(note.file, args.statement, source, note.scope);
575
753
  usefulness.cited(args.cites);
576
- const replaced = args.replaces !== undefined && note.id
754
+ const replaced = replaces !== undefined && note.id
577
755
  // The source the entry was written with (the default of this write, when none), so the link names the same one.
578
- ? await replacementReceipt(args.replaces, note.id, sourceOf(args.source, extra, config) ?? 'mcp:remember')
756
+ ? await replacementReceipt(replaces, note.id, source ?? 'mcp:remember')
579
757
  : {};
580
758
  return jsonResult({
581
759
  ok: true,
@@ -584,6 +762,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
584
762
  file: note.file,
585
763
  scope: note.scope,
586
764
  ...(note.id ? { id: note.id } : {}),
765
+ ...adjusted,
587
766
  ...replaced,
588
767
  ...(await indexSavedNote(db, config, engine, digests, note.file)),
589
768
  });
@@ -595,14 +774,34 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
595
774
  server.registerTool('get_context', {
596
775
  title: 'Get relevant user context',
597
776
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
598
- description: 'Call this before substantive work with the current request. Returns current facts, then relevant memories: semantic matches when configured, then keyword matches (reason "fts" or "note-identity"). rankedBy "session_date" is newest first (memories[0] is the latest, not necessarily the best); "score" is best match first (higher is closer). Keyword scores use another scale: compare within one reason. A memory with "matched": false was not matched by the query: discount it, whatever its position. Each memory has session_date, when it was written. Hidden note text is labelled there: "[hidden HTML comment: ...]". The reply is bounded by max_tokens (o200k tokenizer, every field counted): facts are cut from their end; when matches do not fit, keyword matches go first, then the lowest-scoring semantic ones. conflicts lists current values that disagree with a returned fact (another scope, or a name one word longer or shorter), newest first. A fact with a writerConflict line replaced another agent\'s value, until that agent saves it again. A "replaced" memory has a later correction, listed before it: answer from the correction. Facts an import saved are marked "untrusted": true.'
599
- + relevanceGuidance + relevanceKeepsMatch + budgetKeepsNewest,
777
+ description: 'Call this before substantive work with the current request. Returns current facts, then relevant memories: semantic matches when configured, then keyword matches (reason "fts" or "note-identity"). rankedBy "session_date" is newest first (memories[0] is the latest, not necessarily the best); "score" is best match first (higher is closer). Keyword scores use another scale: compare within one reason. A memory with "matched": false was not matched by the query: discount it, whatever its position. Each memory has session_date, when it was written. Hidden note text is labelled there: "[hidden HTML comment: ...]". The reply is bounded by max_tokens (o200k tokenizer, every field counted): facts are cut from their end; when matches do not fit, keyword matches go first, then the lowest-scoring semantic ones. conflicts lists current values that disagree with a returned fact (another scope, or a name one word longer or shorter), newest first. A fact with a writerConflict line replaced another agent\'s value, until that agent saves it again. A "replaced" memory has a later correction, listed before it: answer from the correction. Imports and reserved sources are marked "untrusted": true.'
778
+ + relevanceGuidance + relevanceKeepsMatch + budgetKeepsNewest + (withSchemas ? exports.OUTPUT_SCHEMA_BOUND_NOTE : ''),
600
779
  inputSchema: {
601
780
  query: zod_1.z.string().max(1000).optional().describe('The current user request or task. Omit only for a compact current-memory profile.'),
602
781
  scope: zod_1.z.string().optional().describe('Optional memory scope to restrict context'),
603
- max_tokens: zod_1.z.number().int().min(128).max(4000).optional().describe('Token budget for the whole reply (o200k tokenizer), default 800'),
782
+ max_tokens: zod_1.z.number().int().min(128).max(4000).optional().describe('Token budget for the whole reply (o200k tokenizer), default 800' + (withSchemas ? exports.OUTPUT_SCHEMA_BOUND_NOTE : '')),
604
783
  include_history: zod_1.z.boolean().optional().describe('Also return superseded (corrected) memory events, labelled as historical. Default false: only current memory.'),
605
784
  },
785
+ outputSchema: zod_1.z.looseObject({
786
+ ok: zod_1.z.boolean(),
787
+ query: zod_1.z.string().nullable().optional(),
788
+ scope: zod_1.z.string().optional(),
789
+ retrieval: zod_1.z.enum(['facts', 'hybrid', 'semantic', 'keyword']).optional(),
790
+ rankedBy: zod_1.z.enum(['session_date', 'score']).optional(),
791
+ facts: zod_1.z.array(OUT_HIT).optional(),
792
+ memories: zod_1.z.array(OUT_HIT).optional(),
793
+ truncated: zod_1.z.boolean().optional(),
794
+ maxTokens: zod_1.z.number().optional(),
795
+ notice: zod_1.z.string().optional(),
796
+ degraded: zod_1.z.array(zod_1.z.string()).optional(),
797
+ relevance: OUT_HIT.optional(),
798
+ conflicts: zod_1.z.array(OUT_HIT).optional(),
799
+ provenance: OUT_HIT.optional(),
800
+ abstention: zod_1.z.unknown().optional(),
801
+ evidence: OUT_HIT.optional(),
802
+ index: OUT_HIT.optional(),
803
+ renor: zod_1.z.string().optional(),
804
+ }),
606
805
  }, async (args, extra) => {
607
806
  try {
608
807
  const semantic = engine.provider !== null && Boolean(args.query?.trim());
@@ -618,8 +817,9 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
618
817
  // Which Renor answered: inside max_tokens with the rest of the trailer.
619
818
  trailer: () => ({ ...(0, factsLane_1.indexNotice)(db, semantic, config, args.scope), renor: (0, identity_1.identityLine)(extra?.requestInfo?.headers) }),
620
819
  });
621
- usefulness.measure('get_context', () => (0, readUsefulness_1.contextItems)(body));
622
- return jsonResult(body);
820
+ const checked = await (0, answerCheck_1.applyAnswerCheck)(body, args.query, answerChecker, body.maxTokens);
821
+ usefulness.measure('get_context', () => (0, readUsefulness_1.contextItems)(checked));
822
+ return jsonResult(checked);
623
823
  }
624
824
  catch (err) {
625
825
  return toolError(err);
@@ -639,6 +839,19 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
639
839
  scope: SCOPE_ARG,
640
840
  cites: CITES_ARG,
641
841
  },
842
+ outputSchema: zod_1.z.looseObject({
843
+ ok: zod_1.z.boolean(),
844
+ action: zod_1.z.enum(['store', 'supersede', 'nochange']),
845
+ eventId: zod_1.z.string(),
846
+ subject: zod_1.z.string(),
847
+ predicate: zod_1.z.string(),
848
+ closed: OUT_CLOSED_FACT,
849
+ alsoClosed: OUT_CLOSED_FACT,
850
+ created: OUT_CREATED_FACT,
851
+ possibleDuplicates: OUT_DUPLICATES,
852
+ possibleDuplicatesTotal: zod_1.z.number().optional(),
853
+ ledger: zod_1.z.string(),
854
+ }),
642
855
  }, async (args, extra) => {
643
856
  try {
644
857
  const { subject, predicate, object } = args;
@@ -664,6 +877,8 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
664
877
  duplicates = (0, factConflicts_1.possibleDuplicates)(db, receipt, receipt.created?.factId ?? null, (0, lifecycle_1.scopeFilterFor)(args.scope), readerOf(extra));
665
878
  }
666
879
  catch { /* no hint */ }
880
+ if (receipt.action !== 'nochange')
881
+ dreamer?.noteFactSave();
667
882
  return jsonResult({
668
883
  ok: receipt.ok,
669
884
  action: receipt.action,
@@ -685,7 +900,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
685
900
  server.registerTool('retrieve_memory', {
686
901
  title: 'Retrieve memories',
687
902
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
688
- description: 'Returns the current value(s) for a subject. Optionally filter by predicate, or include the full superseded history (include_history) or a point-in-time view (as_of). Every fact says whether it is the live value (current:true) or a superseded one, so a history read still shows which value is current, and which scope it belongs to. conflicts, when present, names current values that disagree, as get_context does. A current value that replaced a value another agent saved carries a writerConflict line: the replaced value, its writer and date, which stays until that agent saves this subject and predicate again; read it as an open disagreement, not as the settled answer. An empty result says whether the subject exists: known:false means no such subject (closestSubjects lists near matches), known:true means it exists but the predicate or as_of matched nothing. Calling without a subject returns an error that lists the known subjects. With a scope, subjects, suggestions and existence are all judged within that scope only: a subject saved only in another scope is unknown here.',
903
+ description: 'Returns the current value(s) for a subject. Optionally filter by predicate, or include the full superseded history (include_history) or a point-in-time view (as_of). Every fact says whether it is the live value (current:true) or a superseded one, so a history read still shows which value is current, and which scope it belongs to. conflicts, when present, names current values that disagree, as get_context does. A current value that replaced a value another agent saved carries a writerConflict line: the replaced value, its writer and date, which stays until that agent saves this subject and predicate again; read it as an open disagreement, not as the settled answer. An empty result says whether the subject exists: known:false means no such subject (closestSubjects lists near matches), known:true means it exists but the predicate or as_of matched nothing. A fact an import saved, or one whose stored source is a reserved identity (system, developer, assistant, Renor), is marked "untrusted": true. Calling without a subject returns an error that lists the known subjects. With a scope, subjects, suggestions and existence are all judged within that scope only: a subject saved only in another scope is unknown here.',
689
904
  inputSchema: {
690
905
  // Optional in the schema so a missing value reaches the handler and gets an actionable error
691
906
  // instead of the SDK's raw validation error. A null is a missing value too.
@@ -695,6 +910,13 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
695
910
  as_of: nullAsOmitted(zod_1.z.string().optional()).describe('ISO instant: values valid at that time (with include_history: full history)'),
696
911
  scope: nullAsOmitted(zod_1.z.string().optional()).describe('Memory scope: "global" (default) or scope:<slug> / project:<slug>'),
697
912
  },
913
+ outputSchema: zod_1.z.looseObject({
914
+ subject: zod_1.z.string(),
915
+ facts: zod_1.z.array(OUT_HIT),
916
+ conflicts: zod_1.z.array(OUT_HIT).optional(),
917
+ known: zod_1.z.boolean().optional(),
918
+ closestSubjects: zod_1.z.array(zod_1.z.string()).optional(),
919
+ }),
698
920
  }, async (args, extra) => {
699
921
  try {
700
922
  // The one scope everything below is judged in (none = every scope). Facts, existence, suggestions and
@@ -736,9 +958,10 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
736
958
  validTo: f.valid_to,
737
959
  eventId: f.event_id,
738
960
  ...(typeof f.author === 'string' && f.author !== '' ? { source: f.author } : {}),
739
- // An import's text came from a file, not from the user's own statement: marked the way other untrusted text is.
740
- ...((0, sourceName_1.isImportSource)(f.author) ? { untrusted: true } : {}),
741
- ...(f.valid_to == null && displaced.has(f.event_id) ? { writerConflict: displaced.get(f.event_id) } : {}),
961
+ // An import's text came from a file, not from the user's own statement, and a reserved source is only a claim (one
962
+ // stored before the rule): both marked the way other untrusted text is.
963
+ ...((0, sourceName_1.isImportSource)(f.author) || (0, sourceName_1.isReservedSource)(f.author) ? { untrusted: true } : {}),
964
+ ...(f.valid_to == null && f.event_id !== null && displaced.has(f.event_id) ? { writerConflict: displaced.get(f.event_id) } : {}),
742
965
  }));
743
966
  const body = { subject, facts: rows };
744
967
  // Current values that disagree with another current value of the same predicate under this subject in another
@@ -775,6 +998,23 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
775
998
  section: nullAsOmitted(zod_1.z.enum(exportAll_1.EXPORT_SECTIONS).optional()).describe('Continue with this section only (from the manifest\'s nextSection)'),
776
999
  offset: nullAsOmitted(zod_1.z.number().int().min(0).optional()).describe('The item of that section to continue from (from the manifest\'s nextItem); default 0'),
777
1000
  },
1001
+ outputSchema: zod_1.z.looseObject({
1002
+ file: zod_1.z.string(),
1003
+ format: zod_1.z.string(),
1004
+ formatVersion: zod_1.z.string(),
1005
+ bytes: zod_1.z.number().nullable(),
1006
+ counts: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
1007
+ sections: zod_1.z.array(zod_1.z.object({ name: zod_1.z.string(), items: zod_1.z.number().nullable(), events: zod_1.z.number().nullable().optional() })),
1008
+ notExported: zod_1.z.array(zod_1.z.object({ kind: zod_1.z.string(), what: zod_1.z.string(), reason: zod_1.z.string() })),
1009
+ learningLog: zod_1.z.union([zod_1.z.null(), zod_1.z.object({ file: zod_1.z.string(), data: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()) })]),
1010
+ included: zod_1.z.array(zod_1.z.string()),
1011
+ truncated: zod_1.z.boolean(),
1012
+ nextSection: zod_1.z.string().nullable(),
1013
+ nextItem: zod_1.z.number().optional(),
1014
+ documentChars: zod_1.z.number(),
1015
+ section: zod_1.z.string().optional(),
1016
+ offset: zod_1.z.number().optional(),
1017
+ }),
778
1018
  }, async (args) => {
779
1019
  try {
780
1020
  const { manifest, document } = await (0, exportAll_1.exportEverything)(mapContext(), { section: args.section, offset: args.offset });
@@ -785,6 +1025,9 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
785
1025
  { type: 'text', text: (0, sanitize_1.escapeInvisibleControls)(JSON.stringify(manifest)) },
786
1026
  { type: 'text', text: document },
787
1027
  ],
1028
+ // The structured result (output schemas on) is the manifest: JSON.parse of the first block, the value a client parses
1029
+ // from it. The second block (the portable document) is not structured data.
1030
+ ...(withSchemas ? { structuredContent: JSON.parse((0, sanitize_1.escapeInvisibleControls)(JSON.stringify(manifest))) } : {}),
788
1031
  };
789
1032
  }
790
1033
  catch (err) {
@@ -799,6 +1042,21 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
799
1042
  query: zod_1.z.string().min(1).max(200).describe('Lexical search terms, 1..200 characters'),
800
1043
  limit: zod_1.z.number().int().min(1).max(20).optional().describe('Max results (default 5, max 20)'),
801
1044
  },
1045
+ outputSchema: zod_1.z.looseObject({
1046
+ ok: zod_1.z.boolean(),
1047
+ configured: zod_1.z.boolean(),
1048
+ results: zod_1.z.array(OUT_HIT),
1049
+ query: zod_1.z.string().optional(),
1050
+ total_matches: zod_1.z.number().optional(),
1051
+ returned: zod_1.z.number().optional(),
1052
+ skipped_ineligible: zod_1.z.number().optional(),
1053
+ skipped_too_large: zod_1.z.number().optional(),
1054
+ skipped_over_limit: zod_1.z.number().optional(),
1055
+ note: zod_1.z.string().optional(),
1056
+ error: zod_1.z.string().optional(),
1057
+ detail: zod_1.z.string().optional(),
1058
+ disclaimer: zod_1.z.string(),
1059
+ }),
802
1060
  }, async (args) => {
803
1061
  try {
804
1062
  const response = (0, history_1.searchHistory)(config.historyDir, args.query, args.limit);
@@ -808,17 +1066,14 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
808
1066
  if (err instanceof history_1.HistoryUnavailableError) {
809
1067
  // Bounded, explicit, fail-closed: the configured history is unusable,
810
1068
  // but fact memory is untouched and the service stays up.
811
- return (0, toolRefusal_1.markToolRefusal)({
812
- ...jsonResult({
813
- ok: false,
814
- configured: true,
815
- error: 'history-not-available',
816
- detail: err.message.slice(0, 300),
817
- results: [],
818
- disclaimer: history_1.HISTORY_DISCLAIMER,
819
- }),
820
- isError: true,
821
- }, 'capability_unavailable');
1069
+ return (0, toolRefusal_1.markToolRefusal)(refusalResult({
1070
+ ok: false,
1071
+ configured: true,
1072
+ error: 'history-not-available',
1073
+ detail: err.message.slice(0, 300),
1074
+ results: [],
1075
+ disclaimer: history_1.HISTORY_DISCLAIMER,
1076
+ }), 'capability_unavailable');
822
1077
  }
823
1078
  return toolError(err);
824
1079
  }
@@ -826,13 +1081,26 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
826
1081
  server.registerTool('search_memory', {
827
1082
  title: 'Search memory by meaning (where enabled)',
828
1083
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
829
- description: 'Searches notes by meaning when memory_status reports semantic: true; otherwise it returns no hits and says why (semantic-unavailable or semantic-rebuilding), so use search_text or get_context. rankedBy "session_date" (the default) is newest first, so hits[0] is the latest, not necessarily the best; "score" is best match first. Judge hits by score (higher is closer), not position. One conversation\'s hits on one date come as one hit, opening with its date; a time phrase in it is resolved below, counted from that session. A hit without session_date comes last; a fact\'s hit has no notePath. There is no `evidence` object when this deployment has no evidence model configured and the pass is not switched off: no second pass runs, and the hits are the whole result. When there is one, its items come first, outside rankedBy, with reason "evidence", and its state is "ok", "off", "not-applied" or "failed". Facts saved with remember are added outside rankedBy, within top_k; a writerConflict line on one means it replaced another agent\'s value. index.stale: the semantic index lags the saved facts. A "replaced" hit\'s correction is listed before it (by: its id, source: who saved it).' + (evidence?.options.background ? EVIDENCE_WAIT_STATES : '') + (evidence?.options.dailyCap !== undefined ? EVIDENCE_DAILY_CAP_STATE : '') + (digests ? DIGEST_HITS : '') + ((0, readerLayout_1.readerLayoutEnabled)() && (0, questionSpan_1.readerDurationNamed)() ? SPAN_HITS : '') + ((0, readerEvents_1.readerEventsNamed)() ? EVENTS_HITS : '') + ((0, readerLayout_1.readerTimelineEnabled)() ? TIMELINE_HITS : '') + (relevanceGuidance ? contextRelevance_1.SEARCH_RELEVANCE_GUIDANCE : ''),
1084
+ description: 'Searches notes by meaning when memory_status reports semantic: true; else it returns no hits and says why (semantic-unavailable or semantic-rebuilding), so use search_text or get_context. rankedBy "session_date" (the default) is newest first, so hits[0] is the latest, not necessarily the best; "score" is best match first. Judge hits by score (higher is closer), not position. A conversation\'s hits on one date are one hit, opening with its date (its header alone is no hit); a time phrase in it is resolved below, counted from that session. A hit without session_date comes last; a fact\'s hit has no notePath. There is no `evidence` object when this deployment has no evidence model configured and the pass is not switched off: no second pass runs, and the hits are the whole result. When there is one, its items come first, outside rankedBy, with reason "evidence", and its state is "ok", "off", "not-applied" or "failed". Facts saved with remember are added outside rankedBy, within top_k; a writerConflict line on one means it replaced another agent\'s value. index.stale: the semantic index lags saved facts. A "replaced" hit\'s correction is listed before it (by: its id, source: who saved it).' + (evidence?.options.background ? EVIDENCE_WAIT_STATES : '') + (evidence?.options.dailyCap !== undefined ? EVIDENCE_DAILY_CAP_STATE : '') + (digests ? DIGEST_HITS : '') + ((0, readerLayout_1.readerLayoutEnabled)() && (0, questionSpan_1.readerDurationNamed)() ? SPAN_HITS : '') + ((0, readerEvents_1.readerEventsNamed)() ? EVENTS_HITS : '') + ((0, readerLayout_1.readerTimelineEnabled)() ? TIMELINE_HITS : '') + (relevanceGuidance ? contextRelevance_1.SEARCH_RELEVANCE_GUIDANCE : ''),
830
1085
  inputSchema: {
831
1086
  query: zod_1.z.string().min(1).max(500).describe('Natural language query'),
832
1087
  top_k: zod_1.z.number().int().min(1).max(50).optional().describe('Max matches (default 20), not counting the short statements the session expansion adds to them'),
833
1088
  scope: zod_1.z.string().optional().describe('Memory scope: "global" (default) or scope:<slug> / project:<slug>'),
834
1089
  include_history: zod_1.z.boolean().optional().describe('Also return superseded (corrected) memory events, labelled as historical. Default false: only current memory.'),
835
1090
  },
1091
+ outputSchema: zod_1.z.looseObject({
1092
+ ok: zod_1.z.boolean(),
1093
+ hits: zod_1.z.array(OUT_HIT),
1094
+ error: zod_1.z.string().optional(),
1095
+ notice: zod_1.z.string().optional(),
1096
+ note: zod_1.z.string().optional(),
1097
+ rankedBy: zod_1.z.enum(['session_date', 'score']).optional(),
1098
+ scope: zod_1.z.string().optional(),
1099
+ evidence: OUT_HIT.optional(),
1100
+ index: OUT_HIT.optional(),
1101
+ relevance: OUT_HIT.optional(),
1102
+ floor: zod_1.z.object({ similarity: zod_1.z.number(), withheld: zod_1.z.number() }).optional(),
1103
+ }),
836
1104
  }, async (args, extra) => {
837
1105
  try {
838
1106
  const topK = args.top_k ?? 20;
@@ -851,6 +1119,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
851
1119
  query: args.query, scope: args.scope, includeHistory: args.include_history === true, ledgerRelPath: config.ledgerRelPath,
852
1120
  }, probe, body.hits, topK);
853
1121
  const hits = (0, factsLane_1.withFactHits)(db, config, body.hits, { query: args.query, scope: args.scope, match: 'any', limit: topK, dated: true, reader: readerOf(extra) }, verdict.listFact ? [verdict.listFact] : []);
1122
+ (0, callPhases_1.markRetrieved)(); // the matches and the facts are in hand: what follows builds the reply
854
1123
  // Filter the completed page without backfilling. If the operator raises the floor above the relevance
855
1124
  // bar and removes the closest chunk, withhold its verdict: the unchanged tool guidance promises the match.
856
1125
  const floored = floor === 0 ? null : (0, hitFloor_1.applyHitFloor)(hits, floor);
@@ -868,22 +1137,26 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
868
1137
  if (!filteredVerdict.relevance)
869
1138
  shownVerdict = {};
870
1139
  }
871
- // What the read served is the page the caller is shown, floor included: a hit the floor left out was not served, and
872
- // a page it left empty served nothing, whatever the verdict judged over every hit.
873
- usefulness.measure('search_memory', () => {
874
- const served = (0, readUsefulness_1.searchItems)(db, (0, ledgerPresentation_1.ledgerNoteMatcher)(config), page, shownVerdict.relevance);
875
- return floored && served.items.length === 0 ? { ...served, relevant: false } : served;
876
- });
877
1140
  // The event of a fact is read from the ledger's heading, which is taken off below.
1141
+ const layout = (0, readerLayout_1.readerLayoutPage)(body.rankedBy, (await (0, replacements_1.withReplacements)(mapContext(), (0, ledgerPresentation_1.withoutLedgerMetadata)(page, (0, ledgerPresentation_1.ledgerNoteMatcher)(config)), { limit: Math.max(page.length, topK) })).map(hiddenSpans_1.labelMemoryText), (0, ledgerPresentation_1.ledgerNoteMatcher)(config), { query: args.query, turnsOf: (0, readerEvents_1.readerEventsEnabled)() ? (0, readerEvents_1.noteTurnsLoader)(db) : undefined, scoreTurns: (0, readerEvents_1.readerEventsEnabled)() && (0, readerEvents_1.vectorsMeanSomething)(engine.provider?.identity?.kind) && probe.queryVector && engine.vectorIndex ? (0, readerEvents_1.turnScorer)(engine.vectorIndex, probe.queryVector) : undefined }, laidOut);
878
1142
  result = {
879
1143
  ...body,
880
- hits: (0, readerLayout_1.readerLayoutOf)(body.rankedBy, (await (0, replacements_1.withReplacements)(mapContext(), (0, ledgerPresentation_1.withoutLedgerMetadata)(page, (0, ledgerPresentation_1.ledgerNoteMatcher)(config)), { limit: Math.max(page.length, topK) })).map(hiddenSpans_1.labelMemoryText), (0, ledgerPresentation_1.ledgerNoteMatcher)(config), { query: args.query, turnsOf: (0, readerEvents_1.readerEventsEnabled)() ? (0, readerEvents_1.noteTurnsLoader)(db) : undefined, scoreTurns: (0, readerEvents_1.readerEventsEnabled)() && (0, readerEvents_1.vectorsMeanSomething)(engine.provider?.identity?.kind) && probe.queryVector && engine.vectorIndex ? (0, readerEvents_1.turnScorer)(engine.vectorIndex, probe.queryVector) : undefined }, laidOut).map(hit => markImportedOrigin(hit, 'notePath')),
1144
+ // With RENOR_READER_ORDER=relevance a page of conversations comes best match first (readerLayout.ts), and says so.
1145
+ rankedBy: layout.rankedBy,
1146
+ hits: layout.hits.map(markStoredHit).map(shortScore).map(agentMemory_1.headingForReply),
881
1147
  ...(0, factsLane_1.indexNotice)(db, true, config, args.scope),
882
1148
  ...(shownVerdict.relevance ? { relevance: shownVerdict.relevance } : {}),
883
1149
  ...(floored ? { floor: { similarity: floor, withheld: floored.withheld } } : {}),
884
1150
  };
1151
+ result = await (0, answerCheck_1.applyAnswerCheck)(result, args.query, answerChecker);
1152
+ // What the read served is the page the caller is shown, floor included: a hit the floor left out was not served, and
1153
+ // a page it left empty served nothing, whatever the verdict judged over every hit.
1154
+ usefulness.measure('search_memory', () => {
1155
+ const served = (0, readUsefulness_1.searchItems)(db, (0, ledgerPresentation_1.ledgerNoteMatcher)(config), page, result.relevance);
1156
+ return floored && served.items.length === 0 ? { ...served, relevant: false } : served;
1157
+ });
885
1158
  }
886
- return searchResult(body, result);
1159
+ return searchResult(body, result, withSchemas);
887
1160
  }
888
1161
  catch (err) {
889
1162
  return toolError(err);
@@ -892,7 +1165,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
892
1165
  server.registerTool('search_text', {
893
1166
  title: 'Search memory by keywords',
894
1167
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
895
- description: 'BM25/FTS keyword search over indexed note chunks: the query is matched as one exact phrase, and OR, AND, NEAR and * are plain words, not operators. Makes no call to an embedding service. When the term appears in no chunk text it is matched against note paths and frontmatter too, so a task id, slug or file name finds the note that carries it (reason: "note-identity"). Current facts saved with remember are found at once, before the next index_memory (reason: "fact"), in the places the other hits leave under top_k. A fact hit with a writerConflict line replaced a value another agent saved, and the line stays until that agent saves the subject and predicate again (see get_context). A hit of a remembered fact names no note (empty notePath and headingPath). Every hit carries session_date, when that excerpt was written (the moment a daily-log entry or a remembered fact was saved, otherwise its note\'s date), the same date search_memory and get_context give it: read it before trusting an old hit. A hit with `replaced` ({by, at, source, note?}) was corrected by a later save: the correction is listed before it, and `source` says who saved it.',
1168
+ description: 'BM25/FTS keyword search over indexed note chunks: the query is matched as one exact phrase, and OR, AND, NEAR and * are plain words, not operators. Makes no call to an embedding service. When the term appears in no chunk text it is matched against note paths and frontmatter too, so a task id, slug or file name finds the note that carries it (reason: "note-identity"). Current facts saved with remember are found at once, before the next index_memory (reason: "fact"), in the places the other hits leave under top_k. A fact hit with a writerConflict line replaced a value another agent saved, and the line stays until that agent saves the subject and predicate again (see get_context). A hit of a remembered fact names no note (empty notePath and headingPath). Every hit carries session_date, when that excerpt was written (the moment a daily-log entry or a remembered fact was saved, otherwise its note\'s date), the same date search_memory and get_context give it: read it before trusting an old hit. A hit with `replaced` ({by, at, source, note?}) was corrected by a later save: the correction is listed before it, and `source` says who saved it. A daily-log hit whose entry claims a reserved source (system, developer, assistant, Renor), and a `replaced` whose source is one, is marked "untrusted": true.',
896
1169
  inputSchema: {
897
1170
  // A control character (NUL above all) has no place in words to search for: SQLite answers it with its own error text.
898
1171
  query: zod_1.z.string().min(1).max(200).superRefine((query, ctx) => {
@@ -903,20 +1176,35 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
903
1176
  top_k: zod_1.z.number().int().min(1).max(50).optional().describe('Max hits (default 20)'),
904
1177
  scope: zod_1.z.string().optional().describe('Memory scope: "global" (default) or scope:<slug> / project:<slug>'),
905
1178
  },
1179
+ outputSchema: zod_1.z.looseObject({
1180
+ ok: zod_1.z.boolean(),
1181
+ results: zod_1.z.array(OUT_HIT),
1182
+ error: zod_1.z.string().optional(),
1183
+ detail: zod_1.z.string().optional(),
1184
+ scope: zod_1.z.string().optional(),
1185
+ }),
906
1186
  }, async (args, extra) => {
907
1187
  try {
908
1188
  const topK = args.top_k ?? 20;
909
- const body = (0, engine_1.engineSearchText)(db, config.memoryDir, args.query, topK, args.scope, false, config.ledgerRelPath);
1189
+ const exact = (0, engine_1.engineSearchText)(db, config.memoryDir, args.query, topK, args.scope, false, config.ledgerRelPath);
1190
+ // H12 (RENOR_SEARCH_TEXT_FALLBACK=1, default off): when the exact phrase finds nothing, the words are searched with OR and
1191
+ // the reply says so (`fallback`). A reply that has hits is never changed. Pre-registered: data/renor-goal-better-temporal-2/prereg/H11b-H18-H12.md.
1192
+ const body = (0, agentMemory_1.searchTextFallbackEnabled)() && exact.ok && exact.results.length === 0
1193
+ ? (() => {
1194
+ const any = (0, engine_1.engineSearchText)(db, config.memoryDir, args.query, topK, args.scope, false, config.ledgerRelPath, undefined, (0, agentMemory_1.keywordOrMatch)(args.query));
1195
+ return any.ok && any.results.length > 0 ? { ...any, fallback: 'any-word' } : exact;
1196
+ })()
1197
+ : exact;
910
1198
  if (body.ok)
911
1199
  (0, factMarkers_1.markFactHits)(db, body.results, readerOf(extra), (0, ledgerPresentation_1.ledgerNoteMatcher)(config));
912
1200
  const result = body.ok
913
1201
  ? {
914
1202
  ...body,
915
1203
  // An added correction takes the place of a hit, so the page is no longer than top_k (or than it was, if it was longer).
916
- results: (await (async (dated) => (0, replacements_1.withReplacements)(mapContext(), dated, { limit: Math.max(dated.length, topK) }))(await (0, hitDates_1.withHitDates)(db, config.memoryDir, (0, ledgerPresentation_1.withoutLedgerMetadata)((0, factsLane_1.withFactHits)(db, config, body.results, { query: args.query, scope: args.scope, match: 'all', limit: topK, dated: true, reader: readerOf(extra) }), (0, ledgerPresentation_1.ledgerNoteMatcher)(config))))).map(hit => markImportedOrigin(hit, 'notePath')),
1204
+ results: (await (async (dated) => (0, replacements_1.withReplacements)(mapContext(), dated, { limit: Math.max(dated.length, topK) }))(await (0, hitDates_1.withHitDates)(db, config.memoryDir, (0, ledgerPresentation_1.withoutLedgerMetadata)((0, factsLane_1.withFactHits)(db, config, body.results, { query: args.query, scope: args.scope, match: 'all', limit: topK, dated: true, reader: readerOf(extra) }), (0, ledgerPresentation_1.ledgerNoteMatcher)(config))))).map(markStoredHit),
917
1205
  }
918
1206
  : body;
919
- return searchResult(body, result);
1207
+ return searchResult(body, result, withSchemas);
920
1208
  }
921
1209
  catch (err) {
922
1210
  return toolError(err);
@@ -925,15 +1213,25 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
925
1213
  server.registerTool('lookup_entity', {
926
1214
  title: 'Look up an entity',
927
1215
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
928
- description: 'Find a person/project/concept by name or alias. Does not call embeddings. Matches in order: exact canonical name, frontmatter alias, substring of a canonical name, substring of a source note path — never words inside note text, so a word that appears only inside a saved note is not an entity. A miss returns reason "no-entity-match" plus the entity count and the tool that would find the word. With scope set, the search and that count are restricted to the scope, so "no such entity" and "exists in another scope" return the same answer.',
1216
+ description: 'Find a person/project/concept by name or alias. Does not call embeddings. Matches in order: exact canonical name, frontmatter alias, substring of a canonical name, substring of a source note path — never words inside note text, so a word that appears only inside a saved note is not an entity. A miss returns reason "no-entity-match" plus the entity count and the tool that would find the word. With scope set, the search and that count are restricted to the scope, so "no such entity" and "exists in another scope" return the same answer. The entity of an imported note carries origin: "import".',
929
1217
  inputSchema: {
930
1218
  name: zod_1.z.string().min(1).max(200).describe('Entity name or alias'),
931
1219
  scope: zod_1.z.string().optional().describe('Memory scope: "global" (default) or scope:<slug> / project:<slug>; restricts to entities whose source note belongs to the scope'),
932
1220
  },
1221
+ outputSchema: zod_1.z.looseObject({
1222
+ ok: zod_1.z.boolean(),
1223
+ entity: zod_1.z.union([OUT_HIT, zod_1.z.null()]).optional(),
1224
+ reason: zod_1.z.string().optional(),
1225
+ searched: zod_1.z.string().optional(),
1226
+ entitiesIndexed: zod_1.z.number().optional(),
1227
+ note: zod_1.z.string().optional(),
1228
+ scope: zod_1.z.string().optional(),
1229
+ }),
933
1230
  }, async (args) => {
934
1231
  try {
935
1232
  const body = (0, engine_1.engineLookupEntity)(db, config, args.name, args.scope);
936
- return jsonResult(body);
1233
+ // The entity of an imported note says so, as every read of that note does.
1234
+ return jsonResult(body.entity ? { ...body, entity: markImportedOrigin(body.entity, 'sourceNote') } : body);
937
1235
  }
938
1236
  catch (err) {
939
1237
  return toolError(err);
@@ -953,15 +1251,25 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
953
1251
  cites: CITES_ARG,
954
1252
  replaces: REPLACES_ARG,
955
1253
  },
1254
+ outputSchema: zod_1.z.looseObject({
1255
+ ok: zod_1.z.boolean(),
1256
+ file: zod_1.z.string(),
1257
+ scope: zod_1.z.string(),
1258
+ id: zod_1.z.string().optional(),
1259
+ replaced: OUT_REPLACEMENT,
1260
+ replacesIgnored: zod_1.z.string().optional(),
1261
+ indexed: zod_1.z.boolean().optional(),
1262
+ index: OUT_INDEX_PASS,
1263
+ warning: zod_1.z.string().optional(),
1264
+ detail: zod_1.z.string().optional(),
1265
+ }),
956
1266
  }, async (args, extra) => {
957
1267
  try {
958
1268
  const source = sourceOf(args.source, extra, config);
959
1269
  if (args.replaces !== undefined)
960
1270
  await (0, replacements_1.assertReplaceable)(mapContext(), args.replaces, (0, scope_1.normalizeScope)(args.scope));
961
1271
  const body = await (0, engine_1.engineRememberNote)(config, args.text, args.date, source, args.scope, args.timezone);
962
- // The daily line keeps the engine's own default (mcp); only whats_new's author falls back to unnamed.
963
- db.prepare('INSERT INTO write_events (note_path, action, summary, created_at, author, scope) VALUES (?, ?, ?, ?, ?, ?)')
964
- .run(body.file, 'note', args.text.slice(0, 240), new Date().toISOString(), source ?? 'unnamed', body.scope);
1272
+ recordNoteSave(body.file, args.text, source, body.scope);
965
1273
  usefulness.cited(args.cites);
966
1274
  const replaced = args.replaces !== undefined && body.id
967
1275
  // The source the entry was written with (engineRememberNote's default when none), so the link names the same one.
@@ -978,6 +1286,17 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
978
1286
  annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
979
1287
  description: 'Walks the workspace\'s Markdown notes into the search index (chunks, keyword index, entities). If an embedding provider is configured, stamps new and changed notes with it; the provider must match the existing index dimensions. Run it when search_memory or get_context report index.stale; saved facts are keyword-searchable without it. It does not wait for a slow or refusing embedding service: such files are reported (warning "index-unfinished") and finished in the background.',
980
1288
  inputSchema: {},
1289
+ outputSchema: zod_1.z.looseObject({
1290
+ ok: zod_1.z.boolean(),
1291
+ total: zod_1.z.number(),
1292
+ indexed: zod_1.z.number(),
1293
+ errors: zod_1.z.number(),
1294
+ skippedNonUtf8Names: zod_1.z.array(zod_1.z.string()).optional(),
1295
+ skippedTooLarge: zod_1.z.array(zod_1.z.string()).optional(),
1296
+ failure: zod_1.z.string().optional(),
1297
+ warning: zod_1.z.string().optional(),
1298
+ detail: zod_1.z.string().optional(),
1299
+ }),
981
1300
  }, async () => {
982
1301
  try {
983
1302
  const stats = await (0, engine_1.engineIndex)(db, config, engine, { interactive: true });
@@ -1022,6 +1341,21 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1022
1341
  annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false },
1023
1342
  description: 'Dry run for an agent-led import of notes into Imported/<label>/. The server cannot read your disk: read each file yourself and send its text as files: [{ relativePath, markdown }], relativePath being the path inside the folder you import (notes/idea.md: / separators, no leading / or ./ or ..). Only .md files: save a ChatGPT or Claude memory list, JSON or other text as Markdown first and leave attachments out. Names starting with "." are skipped. Returns counts, conflicts (a different file already at the destination is kept, not overwritten; use a new label to import a changed copy), skipped files with reasons, and an expiring batchId; writes no memory. Max 100 files, 64 KiB/file and 128 KiB per call including paths; for more, call again per group. Up to 8 previews can wait at once: apply one, or wait 15 minutes. Refuses unsafe or oversize inputs. Selection: include/exclude path prefixes. stage: false only checks (no batchId, nothing held). Show preview before apply_import.',
1024
1343
  inputSchema: hosted_1.importPreviewSchema,
1344
+ outputSchema: zod_1.z.looseObject({
1345
+ ok: zod_1.z.boolean(),
1346
+ namespace: zod_1.z.string(),
1347
+ counts: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
1348
+ conflicts: zod_1.z.array(zod_1.z.string()),
1349
+ unchanged: zod_1.z.number(),
1350
+ toWrite: zod_1.z.number(),
1351
+ skipped: zod_1.z.array(OUT_HIT),
1352
+ limits: OUT_HIT,
1353
+ staged: zod_1.z.boolean().optional(),
1354
+ batchId: zod_1.z.string().optional(),
1355
+ expiresAt: zod_1.z.string().optional(),
1356
+ secretGuard: OUT_HIT.optional(),
1357
+ unchangedPaths: zod_1.z.array(zod_1.z.string()).optional(),
1358
+ }),
1025
1359
  }, async (args) => {
1026
1360
  try {
1027
1361
  return jsonResult(await (0, hosted_1.previewImport)(mapContext(), args));
@@ -1043,6 +1377,21 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1043
1377
  annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
1044
1378
  description: 'Apply preview_import batchId within 15 minutes (same tenant process). Additive: preserves existing different files and reports conflicts. Immediately keyword searchable; on a workspace with semantic search the new notes are then embedded in the background (memory_status says semantic: false until they are). No fact extraction. Returns batchId for undo_import; retries are idempotent.',
1045
1379
  inputSchema: hosted_1.importBatchSchema,
1380
+ outputSchema: zod_1.z.looseObject({
1381
+ ok: zod_1.z.boolean(),
1382
+ batchId: zod_1.z.string(),
1383
+ namespace: zod_1.z.string().optional(),
1384
+ notesWritten: zod_1.z.number().optional(),
1385
+ notesUnchanged: zod_1.z.number().optional(),
1386
+ notesConflicting: zod_1.z.number().optional(),
1387
+ conflicts: zod_1.z.array(zod_1.z.string()).optional(),
1388
+ eventsAppended: zod_1.z.number().optional(),
1389
+ eventsAlreadyPresent: zod_1.z.number().optional(),
1390
+ ledgerReplayed: zod_1.z.number().optional(),
1391
+ indexRows: OUT_HIT.optional(),
1392
+ destination: zod_1.z.union([OUT_HIT, zod_1.z.null()]).optional(),
1393
+ recall: zod_1.z.string().optional(),
1394
+ }),
1046
1395
  }, async ({ batchId }, extra) => {
1047
1396
  try {
1048
1397
  return jsonResult(await (0, hosted_1.applyHostedImport)(mapContext(), batchId, storageLimitOf(extra?.requestInfo?.headers)));
@@ -1064,6 +1413,14 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1064
1413
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false },
1065
1414
  description: 'Undo this batch\'s claim on imported notes. A created note stays live while a later batch still asserts the same bytes; undoing the last such batch retires it. Preserves pre-existing files, conflicts and later edits; reports preserved paths and the re-assertion reason. Uses existing retirement journal and trash. Batch receipts persist across restarts and are tenant-local.',
1066
1415
  inputSchema: hosted_1.importBatchSchema,
1416
+ outputSchema: zod_1.z.looseObject({
1417
+ ok: zod_1.z.boolean(),
1418
+ batchId: zod_1.z.string(),
1419
+ retired: zod_1.z.number().optional(),
1420
+ preserved: zod_1.z.array(zod_1.z.string()).optional(),
1421
+ preservedReasons: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
1422
+ alreadyUndone: zod_1.z.boolean().optional(),
1423
+ }),
1067
1424
  }, async ({ batchId }) => {
1068
1425
  try {
1069
1426
  return jsonResult(await (0, hosted_1.undoHostedImport)(mapContext(), batchId));
@@ -1075,7 +1432,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1075
1432
  server.registerTool('memory_map', {
1076
1433
  title: 'Map everything this workspace holds',
1077
1434
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
1078
- description: 'Read-only overview for browsing, not for answering questions: every memory (notes and daily-log entries), the things lifecycle facts are about, topics (tags), lifecycle facts with their full history, grounded links between them (tag, wikilink, literal name mention, fact naming another thing), recent write events, and retired memories. A current fact with a writerConflict line replaced a value another agent saved, and the line stays until that agent saves the subject and predicate again. For context while working, use get_context instead. '
1435
+ description: 'Read-only overview for browsing, not for answering questions: every memory (notes and daily-log entries), the things lifecycle facts are about, topics (tags), lifecycle facts with their full history, grounded links between them (tag, wikilink, literal name mention, fact naming another thing), recent write events, and retired memories. A current fact with a writerConflict line replaced a value another agent saved, and the line stays until that agent saves the subject and predicate again. An imported memory carries origin: "import", and a memory whose stored source is a reserved identity (system, developer, assistant, Renor) is marked "untrusted": true. For context while working, use get_context instead. '
1079
1436
  + `The limit bounds every list (memories, things, facts, links, events, retired), newest first (things by their newest fact; links shared out across tag, wikilink, mention and fact): default ${memoryMap_1.DEFAULT_MAP_LIMIT}, at most ${MAX_LOOKUP_LIMIT}. total gives how many each list holds, and truncated says which lists were cut; to see more, raise limit, or pass query to find one memory. `
1080
1437
  + 'To find one memory to read, retire or restore, pass query (words it contains, e.g. "landlord"), and scope to look in one scope only: only the memories and retired memories that have one of them are returned, those with the rarest of the words first, then the newest, up to limit. A note save (remember without a fact, remember_note) returns the id of what it saved, so forgetting it needs no lookup.',
1081
1438
  inputSchema: {
@@ -1083,6 +1440,22 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1083
1440
  scope: zod_1.z.string().optional().describe('With query only: look in this memory scope ("global", or scope:<slug> / project:<slug>, which name the same folder for notes).'),
1084
1441
  limit: zod_1.z.number().int().min(1).max(MAX_LOOKUP_LIMIT).optional().describe(`At most this many of each list the map shows (memories, things, facts, links, events, retired). Without a query, default ${memoryMap_1.DEFAULT_MAP_LIMIT}. With a query it bounds the memories and retired memories found, default ${DEFAULT_LOOKUP_LIMIT}.`),
1085
1442
  },
1443
+ outputSchema: zod_1.z.looseObject({
1444
+ ok: zod_1.z.boolean(),
1445
+ generatedAt: zod_1.z.string().optional(),
1446
+ limit: zod_1.z.number().optional(),
1447
+ query: zod_1.z.string().optional(),
1448
+ scope: zod_1.z.string().optional(),
1449
+ memories: zod_1.z.array(OUT_HIT).optional(),
1450
+ things: zod_1.z.array(OUT_HIT).optional(),
1451
+ topics: zod_1.z.array(OUT_HIT).optional(),
1452
+ facts: zod_1.z.array(OUT_HIT).optional(),
1453
+ links: zod_1.z.array(OUT_HIT).optional(),
1454
+ events: zod_1.z.array(OUT_HIT).optional(),
1455
+ retired: zod_1.z.array(OUT_HIT).optional(),
1456
+ total: zod_1.z.record(zod_1.z.string(), zod_1.z.number()).optional(),
1457
+ truncated: zod_1.z.record(zod_1.z.string(), zod_1.z.boolean()).optional(),
1458
+ }),
1086
1459
  }, async (args) => {
1087
1460
  try {
1088
1461
  if (args.query === undefined) {
@@ -1091,7 +1464,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1091
1464
  if (args.scope !== undefined)
1092
1465
  throw (0, toolRefusal_1.refusal)('invalid_argument', 'memory_map takes a scope only with a query (words the memories contain): the whole map is not cut by scope');
1093
1466
  const map = await (0, memoryMap_1.buildMemoryMap)(mapContext(), { limit: args.limit });
1094
- return jsonResult({ ...map, memories: map.memories.map(memory => markImportedOrigin(memory, 'path')) });
1467
+ return jsonResult({ ...map, memories: map.memories.map(markStoredMemory) });
1095
1468
  }
1096
1469
  // Read the way the facts lane reads a query: stop words count only when nothing else is left.
1097
1470
  const terms = (0, agentMemory_1.factQueryTerms)(args.query);
@@ -1099,7 +1472,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1099
1472
  throw (0, toolRefusal_1.refusal)('invalid_argument', 'query has no word to look for (two or more letters or digits)');
1100
1473
  const scope = args.scope === undefined ? undefined : (0, scope_1.normalizeScope)(args.scope);
1101
1474
  const found = await (0, memoryMap_1.findMemories)(mapContext(), { query: args.query, terms, limit: args.limit ?? DEFAULT_LOOKUP_LIMIT, scope });
1102
- return jsonResult({ ...found, memories: found.memories.map(memory => markImportedOrigin(memory, 'path')) });
1475
+ return jsonResult({ ...found, memories: found.memories.map(markStoredMemory) });
1103
1476
  }
1104
1477
  catch (err) {
1105
1478
  return toolError(err);
@@ -1108,8 +1481,18 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1108
1481
  server.registerTool('read_memory', {
1109
1482
  title: 'Read one memory in full',
1110
1483
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
1111
- description: 'Returns one memory from memory_map by id: its full text, its note metadata (status, tags, dates), and facts extracted from it. memory.session_date is when it was written, the date get_context and search_memory give the same text (memory.at is the entry\'s moment or the note\'s last write).',
1484
+ description: 'Returns one memory from memory_map by id: its full text, its note metadata (status, tags, dates), and facts extracted from it. memory.session_date is when it was written, the date get_context and search_memory give the same text (memory.at is the entry\'s moment or the note\'s last write). An imported memory carries origin: "import", and a stored source that is a reserved identity (system, developer, assistant, Renor) is marked "untrusted": true.',
1112
1485
  inputSchema: { id: memoryIdSchema },
1486
+ outputSchema: zod_1.z.looseObject({
1487
+ ok: zod_1.z.boolean(),
1488
+ memory: OUT_HIT.optional(),
1489
+ text: zod_1.z.string().optional(),
1490
+ textTruncated: zod_1.z.boolean().optional(),
1491
+ metadata: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
1492
+ facts: zod_1.z.array(OUT_HIT).optional(),
1493
+ replaced: OUT_HIT.optional(),
1494
+ replaces: zod_1.z.array(zod_1.z.string()).optional(),
1495
+ }),
1113
1496
  }, async (args) => {
1114
1497
  try {
1115
1498
  const memory = await (0, memoryMap_1.readMemory)(mapContext(), args.id);
@@ -1120,7 +1503,7 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1120
1503
  const labelled = (0, hiddenSpans_1.labelMemoryText)(memory);
1121
1504
  return jsonResult({
1122
1505
  ...labelled,
1123
- memory: markImportedOrigin(labelled.memory, 'path'),
1506
+ memory: markStoredMemory(labelled.memory),
1124
1507
  ...(replaced ? { replaced } : {}),
1125
1508
  ...(replaces.length > 0 ? { replaces } : {}),
1126
1509
  });
@@ -1134,6 +1517,15 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1134
1517
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false },
1135
1518
  description: 'Takes one memory out of recall without deleting it: a note file moves to the retired area, a daily-log entry moves out of its file, a fact leaves every read, and all leave search immediately. Use only when the owner asks to forget something; the receipt of the save that wrote it, or memory_map with a query (words of that memory), gives its id. restore_memory undoes it. A wrong fact is corrected with remember, not retired; a wrong or outdated note is corrected with remember and replaces. Retiring a correction undoes its replaces: the memory it replaced reads as current again.',
1136
1519
  inputSchema: { id: retirableIdSchema },
1520
+ outputSchema: zod_1.z.looseObject({
1521
+ ok: zod_1.z.boolean(),
1522
+ id: zod_1.z.string(),
1523
+ action: zod_1.z.enum(['retired', 'restored']),
1524
+ title: zod_1.z.string(),
1525
+ at: zod_1.z.string(),
1526
+ eventId: zod_1.z.string().optional(),
1527
+ current: zod_1.z.boolean().optional(),
1528
+ }),
1137
1529
  }, async (args, extra) => {
1138
1530
  try {
1139
1531
  return jsonResult(await (0, memoryMap_1.retireMemory)({ ...mapContext(), source: sourceOf(undefined, extra, config) }, args.id));
@@ -1147,11 +1539,20 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1147
1539
  annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
1148
1540
  description: 'Puts a retired memory back exactly as it was and re-indexes it. Refuses to overwrite a file that now occupies the same path.',
1149
1541
  inputSchema: { id: retirableIdSchema },
1542
+ outputSchema: zod_1.z.looseObject({
1543
+ ok: zod_1.z.boolean(),
1544
+ id: zod_1.z.string(),
1545
+ action: zod_1.z.enum(['retired', 'restored']),
1546
+ title: zod_1.z.string(),
1547
+ at: zod_1.z.string(),
1548
+ eventId: zod_1.z.string().optional(),
1549
+ current: zod_1.z.boolean().optional(),
1550
+ }),
1150
1551
  }, async (args, extra) => {
1151
1552
  try {
1152
1553
  const storageLimit = storageLimitOf(extra?.requestInfo?.headers);
1153
1554
  return jsonResult(await (0, memoryMap_1.restoreMemory)({ ...mapContext(), source: sourceOf(undefined, extra, config) }, args.id, storageLimit === undefined ? {} : { validateAfterWrite: async () => {
1154
- const bytes = await (0, storedSize_2.measureMarkdownBytes)(config.memoryDir);
1555
+ const bytes = await (0, storedSize_1.measureMarkdownBytes)(config.memoryDir);
1155
1556
  if (bytes > storageLimit) {
1156
1557
  throw (0, toolRefusal_1.refusal)('storage_limit', `Renor: this write would need ${bytes.toLocaleString('en-US')} bytes of Markdown storage and would not fit; the storage limit is ${storageLimit.toLocaleString('en-US')} bytes. This memory write was not run.`);
1157
1558
  }
@@ -1174,11 +1575,18 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1174
1575
  server.registerTool('delete_retired_memory', {
1175
1576
  title: 'Permanently delete retired memories (owner only)',
1176
1577
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false },
1177
- description: 'Permanently deletes a retired note or daily-log entry (id: retired:<id> from memory_map.retired), or every retired note and entry (id: "all"; with ids, only those retirement ids). Cannot be undone. A live memory is retired first; a retired fact is kept in the fact ledger.',
1578
+ description: 'Permanently deletes a retired memory (id from memory_map.retired), or every listed retired memory (id: "all"; with ids, only those ids). Cannot be undone. A live memory is retired first.',
1178
1579
  inputSchema: {
1179
- id: zod_1.z.string().min(1).max(64).describe('retired:<id> from memory_map.retired, or "all"'),
1180
- ids: zod_1.z.array(zod_1.z.string().min(1).max(64)).min(1).max(MAX_LOOKUP_LIMIT).optional().describe('With id "all": only these retired:<id> entries (what the owner saw listed)'),
1580
+ id: zod_1.z.string().min(1).max(64).describe('A retired memory id from memory_map.retired, or "all"'),
1581
+ ids: zod_1.z.array(zod_1.z.string().min(1).max(64)).min(1).max(MAX_LOOKUP_LIMIT).optional().describe('With id "all": only these retired memory ids (what the owner saw listed)'),
1181
1582
  },
1583
+ outputSchema: zod_1.z.looseObject({
1584
+ ok: zod_1.z.boolean(),
1585
+ action: zod_1.z.enum(['deleted']),
1586
+ ids: zod_1.z.array(zod_1.z.string()),
1587
+ freedBytes: zod_1.z.number(),
1588
+ at: zod_1.z.string(),
1589
+ }),
1182
1590
  }, async (args, extra) => {
1183
1591
  try {
1184
1592
  if (!isDashboardCall(extra?.requestInfo?.headers, config)) {
@@ -1191,7 +1599,76 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1191
1599
  }
1192
1600
  });
1193
1601
  if (!dashboard)
1194
- registeredToolEntry(server, OWNER_ONLY_TOOL).enabled = false;
1602
+ registeredToolEntry(server, 'delete_retired_memory').enabled = false;
1603
+ // Only for the owner, from the dashboard: the workspace's choice to share learning data with Renor (src/sharing). The consent
1604
+ // record the owner's click writes in the control plane (set_learning_consent) is the proof; THIS is what the tenant obeys, so the
1605
+ // dashboard calls it right after the record is written, and recording and sending start or stop on the next call. It guards
1606
+ // itself exactly as delete_retired_memory does, and for an agent it is neither listed nor callable.
1607
+ server.registerTool('sharing_settings', {
1608
+ title: 'Learning-data sharing choice (owner only)',
1609
+ annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: true },
1610
+ description: 'Reads, sets or deletes this workspace\'s choice to share learning data with Renor. action "get" returns the choice; "set" needs level (off, signals or data) and, for signals or data, adult: true and the notice_version the owner saw; "delete" asks Renor to delete what it holds for this workspace and removes the local sharing files. Off is the default.',
1611
+ inputSchema: {
1612
+ action: zod_1.z.enum(['get', 'set', 'delete']).describe('get, set or delete'),
1613
+ level: zod_1.z.enum(fields_1.SHARING_LEVELS).optional().describe('With set: off, signals or data'),
1614
+ notice_version: zod_1.z.string().min(1).max(80).optional().describe('With set: the id of the words the owner saw'),
1615
+ adult: zod_1.z.boolean().optional().describe('With set to signals or data: the owner confirmed they are 18 or older'),
1616
+ },
1617
+ outputSchema: zod_1.z.looseObject({
1618
+ ok: zod_1.z.boolean(),
1619
+ action: zod_1.z.enum(['get', 'set', 'delete']),
1620
+ level: zod_1.z.enum(fields_1.SHARING_LEVELS),
1621
+ effective: zod_1.z.enum(fields_1.SHARING_LEVELS),
1622
+ changedAt: zod_1.z.string().nullable(),
1623
+ noticeVersion: zod_1.z.string().nullable(),
1624
+ remoteDeleted: zod_1.z.boolean().nullable(),
1625
+ contributorId: zod_1.z.string().nullable(),
1626
+ }),
1627
+ }, async (args, extra) => {
1628
+ try {
1629
+ if (!isDashboardCall(extra?.requestInfo?.headers, config)) {
1630
+ throw (0, toolRefusal_1.refusal)('invalid_argument', 'sharing_settings is only available to the owner, from the Renor dashboard');
1631
+ }
1632
+ const dir = config.sharingDir;
1633
+ if (dir === undefined)
1634
+ throw (0, toolRefusal_1.refusal)('invalid_argument', 'sharing is not configured on this server');
1635
+ let remoteDeleted = null;
1636
+ if (args.action === 'set') {
1637
+ if (args.level === undefined)
1638
+ throw (0, toolRefusal_1.refusal)('invalid_argument', 'set needs level');
1639
+ if (args.level !== 'off' && (args.adult !== true || args.notice_version === undefined)) {
1640
+ throw (0, toolRefusal_1.refusal)('invalid_argument', 'sharing signals or data needs adult: true and the notice_version the owner saw');
1641
+ }
1642
+ const result = await (0, send_1.chooseLevel)({
1643
+ dir, level: args.level, wording: args.notice_version ?? 'dashboard', adult: args.adult === true, source: 'dashboard', hosted: true,
1644
+ });
1645
+ remoteDeleted = result.remoteDeleted;
1646
+ }
1647
+ else if (args.action === 'delete') {
1648
+ const result = await (0, send_1.deleteAllShared)({ dir, hosted: true });
1649
+ remoteDeleted = result.remote.requested ? result.remote.ok : null;
1650
+ }
1651
+ const record = (0, state_1.readConsent)(dir);
1652
+ // The name the control plane ties to this workspace, so the endpoint can check the control plane's consent record before it
1653
+ // stores a row. It is the hash of the sender's secret; made on first use only for a workspace that has chosen to share.
1654
+ const sharing = record !== null && record.level !== 'off' && record.adult;
1655
+ return jsonResult({
1656
+ ok: true,
1657
+ action: args.action,
1658
+ contributorId: sharing ? (0, state_1.contributorId)((0, state_1.ensureState)(dir).secret) : null,
1659
+ level: record?.level ?? 'off',
1660
+ effective: (0, state_1.effectiveLevel)(dir),
1661
+ changedAt: record?.changedAt ?? null,
1662
+ noticeVersion: record?.wording ?? null,
1663
+ remoteDeleted,
1664
+ });
1665
+ }
1666
+ catch (err) {
1667
+ return toolError(err);
1668
+ }
1669
+ });
1670
+ if (!dashboard)
1671
+ registeredToolEntry(server, 'sharing_settings').enabled = false;
1195
1672
  server.registerTool('whats_new', {
1196
1673
  title: "What's new for me",
1197
1674
  // It moves this connection's mark, so it is a writing tool, not read-only: a second call returns less.
@@ -1203,6 +1680,14 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1203
1680
  include_own: zod_1.z.boolean().optional().describe('Also return what this connection wrote. Default false.'),
1204
1681
  cursor: omittedWhenBlank(zod_1.z.string().optional()).describe('The cursor from the previous reply, when that reply was full.'),
1205
1682
  },
1683
+ outputSchema: zod_1.z.looseObject({
1684
+ items: zod_1.z.array(OUT_HIT),
1685
+ as_of: zod_1.z.string(),
1686
+ next_since: zod_1.z.string().nullable(),
1687
+ cursor: zod_1.z.string().nullable(),
1688
+ more_requests: zod_1.z.number().optional(),
1689
+ more_requests_hint: zod_1.z.string().optional(),
1690
+ }),
1206
1691
  }, async (args, extra) => {
1207
1692
  try {
1208
1693
  const agent = hiveAgentOf(extra, 'whats_new', config);
@@ -1218,6 +1703,27 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1218
1703
  return toolError(err);
1219
1704
  }
1220
1705
  });
1706
+ // The handoff note (memory/handoff.ts): published only with RENOR_HANDOFF_NOTE on, so the default tool list is unchanged.
1707
+ if ((0, handoff_1.handoffNoteEnabled)())
1708
+ server.registerTool(handoff_1.HANDOFF_TOOL_NAME, {
1709
+ title: 'Handoff note for this branch',
1710
+ annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: false },
1711
+ description: handoff_1.HANDOFF_TOOL_DESCRIPTION,
1712
+ inputSchema: {
1713
+ action: zod_1.z.enum(['read', 'write']).describe(handoff_1.HANDOFF_ARG_DESCRIPTIONS.action),
1714
+ repo: zod_1.z.string().min(1).max(1000).describe(handoff_1.HANDOFF_ARG_DESCRIPTIONS.repo),
1715
+ branch: zod_1.z.string().min(1).max(1000).describe(handoff_1.HANDOFF_ARG_DESCRIPTIONS.branch),
1716
+ note: omittedWhenBlank(zod_1.z.string().max(handoff_1.HANDOFF_MAX_CHARS * 2).optional()).describe(handoff_1.HANDOFF_ARG_DESCRIPTIONS.note),
1717
+ },
1718
+ outputSchema: zod_1.z.looseObject({ ok: zod_1.z.boolean(), action: zod_1.z.enum(['read', 'write']) }),
1719
+ }, async (args, extra) => {
1720
+ try {
1721
+ return jsonResult(await (0, handoff_1.runHandoffTool)(db, args, agentOf(extra, config)));
1722
+ }
1723
+ catch (err) {
1724
+ return toolError(err);
1725
+ }
1726
+ });
1221
1727
  server.registerTool('ask_agent', {
1222
1728
  title: 'Ask another agent',
1223
1729
  annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false },
@@ -1227,6 +1733,13 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1227
1733
  request: zod_1.z.string().min(1).max(limits_1.MAX_STATEMENT_LENGTH).describe('What you are asking, in plain words.'),
1228
1734
  scope: SCOPE_ARG,
1229
1735
  },
1736
+ outputSchema: zod_1.z.looseObject({
1737
+ id: zod_1.z.string(),
1738
+ status: zod_1.z.enum(['open', 'answered', 'closed']),
1739
+ from_agent: zod_1.z.string(),
1740
+ to_agent: zod_1.z.string(),
1741
+ created_at: zod_1.z.string(),
1742
+ }),
1230
1743
  }, async (args, extra) => {
1231
1744
  try {
1232
1745
  const from = hiveAgentOf(extra, 'ask_agent', config);
@@ -1249,6 +1762,11 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1249
1762
  scope: SCOPE_ARG,
1250
1763
  before: omittedWhenBlank(zod_1.z.string().max(80).optional()).describe('Only requests older than this one: the id of the last request a previous list_requests or whats_new showed.'),
1251
1764
  },
1765
+ outputSchema: zod_1.z.looseObject({
1766
+ requests: zod_1.z.array(OUT_HIT),
1767
+ more_requests: zod_1.z.number().optional(),
1768
+ more_requests_hint: zod_1.z.string().optional(),
1769
+ }),
1252
1770
  }, async (args, extra) => {
1253
1771
  try {
1254
1772
  const agent = hiveAgentOf(extra, 'list_requests', config);
@@ -1271,6 +1789,19 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1271
1789
  id: zod_1.z.string().min(1).max(80).describe('The request id from whats_new or list_requests.'),
1272
1790
  answer: zod_1.z.string().min(1).max(limits_1.MAX_STATEMENT_LENGTH).describe('The answer, in plain words.'),
1273
1791
  },
1792
+ outputSchema: zod_1.z.looseObject({
1793
+ id: zod_1.z.string(),
1794
+ scope: zod_1.z.string().nullable(),
1795
+ from_agent: zod_1.z.string(),
1796
+ to_agent: zod_1.z.string(),
1797
+ question: zod_1.z.string(),
1798
+ answer: zod_1.z.string().nullable(),
1799
+ status: zod_1.z.enum(['open', 'answered', 'closed']),
1800
+ created_at: zod_1.z.string(),
1801
+ answered_at: zod_1.z.string().nullable(),
1802
+ closed_at: zod_1.z.string().nullable(),
1803
+ untrusted: zod_1.z.literal(true),
1804
+ }),
1274
1805
  }, async (args, extra) => {
1275
1806
  try {
1276
1807
  const agent = hiveAgentOf(extra, 'answer_request', config);
@@ -1289,6 +1820,19 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1289
1820
  inputSchema: {
1290
1821
  id: zod_1.z.string().min(1).max(80).describe('The request id.'),
1291
1822
  },
1823
+ outputSchema: zod_1.z.looseObject({
1824
+ id: zod_1.z.string(),
1825
+ scope: zod_1.z.string().nullable(),
1826
+ from_agent: zod_1.z.string(),
1827
+ to_agent: zod_1.z.string(),
1828
+ question: zod_1.z.string(),
1829
+ answer: zod_1.z.string().nullable(),
1830
+ status: zod_1.z.enum(['open', 'answered', 'closed']),
1831
+ created_at: zod_1.z.string(),
1832
+ answered_at: zod_1.z.string().nullable(),
1833
+ closed_at: zod_1.z.string().nullable(),
1834
+ untrusted: zod_1.z.literal(true),
1835
+ }),
1292
1836
  }, async (args, extra) => {
1293
1837
  try {
1294
1838
  const agent = hiveAgentOf(extra, 'close_request', config);
@@ -1309,6 +1853,20 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1309
1853
  annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
1310
1854
  description: 'Counts of what this workspace holds: notes (files; a daily log is one note per day), chunks (index size), entities, facts (superseded values included), and whether semantic search can answer now (semantic): while the embeddings are being built (after a provider is configured or changed, or notes are imported) semantic is false and embeddingRebuild counts the chunks left, and keyword search works meanwhile. The engine\'s own ledger is not counted as a note, entity or fact. With semantic search configured, chunksWithoutVectors counts the chunks search by meaning cannot find yet because they have no vector (a save whose embedding failed: the next save or search embeds them; imported notes not embedded yet). No embeddings API call.',
1311
1855
  inputSchema: {},
1856
+ outputSchema: zod_1.z.looseObject({
1857
+ notes: zod_1.z.number(),
1858
+ chunks: zod_1.z.number(),
1859
+ entities: zod_1.z.number(),
1860
+ facts: zod_1.z.number(),
1861
+ relations: zod_1.z.number(),
1862
+ embeddingDims: zod_1.z.number().nullable(),
1863
+ semantic: zod_1.z.boolean(),
1864
+ embeddingRebuild: zod_1.z.object({ chunksLeft: zod_1.z.number(), chunks: zod_1.z.number() }).optional(),
1865
+ chunksWithoutVectors: zod_1.z.number().optional(),
1866
+ degraded: zod_1.z.object({ keyword: zod_1.z.string().optional(), semantic: zod_1.z.string().optional() }).optional(),
1867
+ renor: zod_1.z.string(),
1868
+ digests: OUT_HIT.optional(),
1869
+ }),
1312
1870
  }, async (_args, extra) => {
1313
1871
  try {
1314
1872
  const body = { ...(0, engine_1.engineStatus)(db, engine, config), renor: (0, identity_1.identityLine)(extra?.requestInfo?.headers) };
@@ -1319,6 +1877,19 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1319
1877
  return toolError(err);
1320
1878
  }
1321
1879
  });
1880
+ // Nor the tools directorySurface.ts keeps from a directory client: disabled the same way, once every one of them is registered.
1881
+ if (directory)
1882
+ for (const name of directorySurface_1.DIRECTORY_HIDDEN_TOOLS)
1883
+ registeredToolEntry(server, name).enabled = false;
1884
+ // The profile's tool subset: the others are disabled in the SDK's registry, so neither listed nor run.
1885
+ if (profile.tools || profile.descriptions) {
1886
+ const registered = Object.keys(server._registeredTools ?? {});
1887
+ (0, contextProfile_1.checkProfileTools)(profile, registered);
1888
+ if (profile.tools)
1889
+ for (const name of registered)
1890
+ if (!profile.tools.has(name))
1891
+ registeredToolEntry(server, name).enabled = false;
1892
+ }
1322
1893
  // Every tool is registered above; the SDK's own handlers (initialize included) are mapped in the same pass.
1323
1894
  (0, requestErrors_1.mapInvalidParams)(server.server);
1324
1895
  // Layer 1 above: the owner-only tool is in the answer only when this request carries the mark. It is decided on the answer, after the
@@ -1331,19 +1902,50 @@ function buildMcpServer(db, config, engine, idle, digests, usefulness, connectio
1331
1902
  listHandlers.set('tools/list', async (request, extra) => {
1332
1903
  const answer = await listTools(request, extra);
1333
1904
  const headers = extra?.requestInfo?.headers;
1905
+ const forDirectory = directory || isDirectoryCall(headers, config);
1334
1906
  const hidden = new Set();
1335
1907
  if (!isDashboardCall(headers, config))
1336
- hidden.add(OWNER_ONLY_TOOL);
1337
- if (directory || isDirectoryCall(headers, config))
1338
- for (const name of HIVE_TOOLS)
1908
+ for (const name of OWNER_ONLY_TOOLS)
1909
+ hidden.add(name);
1910
+ if (forDirectory)
1911
+ for (const name of [...HIVE_TOOLS, ...directorySurface_1.DIRECTORY_HIDDEN_TOOLS])
1339
1912
  hidden.add(name);
1340
- if (hidden.size === 0)
1913
+ if (hidden.size === 0 && !forDirectory && !profile.descriptions)
1341
1914
  return answer;
1342
1915
  // An answer this cannot read is not passed on unfiltered.
1343
1916
  if (!Array.isArray(answer?.tools))
1344
1917
  throw new Error('tools/list answered without a tools array');
1345
- return { ...answer, tools: answer.tools.filter((tool) => !hidden.has(tool.name)) };
1918
+ const tools = answer.tools.filter((tool) => !hidden.has(tool.name));
1919
+ // A directory client reads descriptions that say what a tool does, with no instruction to the model (directorySurface.ts).
1920
+ return {
1921
+ ...answer,
1922
+ tools: forDirectory
1923
+ ? tools.map((tool) => {
1924
+ const own = typeof tool.name === 'string' ? directorySurface_1.DIRECTORY_DESCRIPTIONS[tool.name] : undefined;
1925
+ const description = own && withSchemas && tool.name === 'get_context' ? own + exports.OUTPUT_SCHEMA_BOUND_NOTE : own;
1926
+ return description ? { ...tool, description } : tool;
1927
+ })
1928
+ : profile.descriptions
1929
+ ? tools.map((tool) => {
1930
+ const own = typeof tool.name === 'string' ? profile.descriptions[tool.name] : undefined;
1931
+ return own ? { ...tool, description: own } : tool;
1932
+ })
1933
+ : tools,
1934
+ };
1346
1935
  });
1936
+ // The profile's reply shape: a read's text block as lines (renderLines); structuredContent, where there is one, is unchanged.
1937
+ if (profile.response === 'lines') {
1938
+ const callTool = listHandlers.get('tools/call');
1939
+ if (!callTool)
1940
+ throw new Error('tools/call handler missing');
1941
+ listHandlers.set('tools/call', async (request, extra) => {
1942
+ const result = await callTool(request, extra);
1943
+ const name = request.params?.name;
1944
+ if (typeof name !== 'string' || !contextProfile_1.LINE_TOOLS.has(name) || result?.isError || !Array.isArray(result?.content))
1945
+ return result;
1946
+ return { ...result, content: result.content.map((c, i) => (i === 0 && c.type === 'text' ? { ...c, text: (0, contextProfile_1.renderLines)(c.text) } : c)) };
1947
+ });
1948
+ }
1347
1949
  return server;
1348
1950
  }
1349
1951
  function createMvpApp(config, engineOverride) {
@@ -1353,6 +1955,8 @@ function createMvpApp(config, engineOverride) {
1353
1955
  (0, memoryDir_1.ensureOwnerOnlyMemoryDir)(config.memoryDir);
1354
1956
  (0, memoryDir_1.ensureOwnerOnlyFile)(config.dbPath);
1355
1957
  const db = (0, client_1.openIndexDb)(config.dbPath, (0, lifecycle_1.resolveLedgerPath)(config.memoryDir, config.ledgerRelPath));
1958
+ // Expired handoff notes are deleted on a timer, not only when one is read (memory/handoff.ts). Off: nothing runs.
1959
+ const stopHandoffSweep = (0, handoff_1.handoffNoteEnabled)() ? (0, handoff_1.startHandoffSweep)(db) : null;
1356
1960
  (0, schema_1.runMigrations)(db);
1357
1961
  // Facts saved before the term index existed, or by a release without it, are indexed before the first search.
1358
1962
  (0, factTerms_1.ensureFactTerms)(db);
@@ -1360,7 +1964,7 @@ function createMvpApp(config, engineOverride) {
1360
1964
  // go before anything reads the index, so no note is served from a place it no longer is. Fails the start if it cannot.
1361
1965
  const rowsOutsideRoot = (0, outsideRoot_1.removeRowsOutsideRoot)(db, config.memoryDir).total;
1362
1966
  if (rowsOutsideRoot > 0)
1363
- console.log(`[startup] removed ${rowsOutsideRoot} index row(s) for files outside ${config.memoryDir}; the folder is indexed again before serving`);
1967
+ log.info('startup_rows_outside_root_removed', { rows: rowsOutsideRoot, detail: 'index rows for files outside the memory folder; the folder is indexed again before serving' });
1364
1968
  const pendingPass = (0, outsideRoot_1.reindexPending)(db);
1365
1969
  const engine = (0, engine_1.attachEngine)(db, config, engineOverride);
1366
1970
  // An index whose vectors are in another model's space, or in none anyone can vouch for, is cleared and embedded
@@ -1387,16 +1991,24 @@ function createMvpApp(config, engineOverride) {
1387
1991
  digests?.worker.start();
1388
1992
  const usefulness = new readUsefulness_1.ReadUsefulness();
1389
1993
  const connection = (0, connectionStatus_1.connectionStatusFromEnv)(process.env);
1390
- const server = buildMcpServer(db, config, engine, activity, digests, usefulness, connection, false);
1994
+ // The consolidation pass (RENOR_CONSOLIDATE, default off): null, and nothing else changes, unless it is switched on.
1995
+ const dreamer = (0, dreamer_1.createDreamer)(process.env, {
1996
+ db,
1997
+ vaultPath: config.memoryDir,
1998
+ ledgerRelPath: config.ledgerRelPath,
1999
+ index: () => (0, engine_1.engineIndexChanged)(db, config, engine),
2000
+ hold: run => { activity.track(() => run).catch(() => undefined); },
2001
+ });
2002
+ const server = buildMcpServer(db, config, engine, activity, digests, usefulness, connection, false, null, false, false, dreamer);
1391
2003
  // An index an earlier build wrote holds the engine's own daily-note headers as facts, and a note that has not changed
1392
2004
  // is never rebuilt, so they are removed here, before anything is read. A failure must not keep a workspace from serving.
1393
2005
  try {
1394
2006
  const removed = (0, pipeline_1.removeDailyHeaderFacts)(db);
1395
2007
  if (removed > 0)
1396
- console.log(`[startup] removed ${removed} daily-note header facts an earlier build indexed`);
2008
+ log.info('startup_header_facts_removed', { facts: removed, detail: 'daily-note header facts an earlier build indexed' });
1397
2009
  }
1398
2010
  catch (err) {
1399
- console.error('[startup] removing daily-note header facts failed:', err instanceof Error ? err.message : String(err));
2011
+ log.error('startup_header_cleanup_failed', {}, err);
1400
2012
  }
1401
2013
  const app = (0, express_1.default)();
1402
2014
  // Express's default error page prints the stack, with absolute paths, unless the app is in production mode.
@@ -1404,6 +2016,10 @@ function createMvpApp(config, engineOverride) {
1404
2016
  // No `X-Powered-By: Express`: it names the stack to a scanner and serves no client, and the gateway
1405
2017
  // passes this engine's answers through to mcp.renor.cloud as they are (live-security F5).
1406
2018
  app.disable('x-powered-by');
2019
+ app.use((_req, res, next) => {
2020
+ res.setHeader('X-Content-Type-Options', 'nosniff');
2021
+ next();
2022
+ });
1407
2023
  // ACCESS LOG (diagnostics only): one metadata-only line per HTTP outcome
1408
2024
  // covering every status class, so server-side connector calls (ChatGPT)
1409
2025
  // become visible without any sensitive material. Fields: method, path
@@ -1451,7 +2067,7 @@ function createMvpApp(config, engineOverride) {
1451
2067
  fields.client_prefix = clientPrefix;
1452
2068
  if (closedWithoutFinish)
1453
2069
  fields.outcome = 'closed'; // client abort / mid-stream disconnect
1454
- console.log(JSON.stringify({ ts: new Date().toISOString(), event: 'http_access', ...fields }));
2070
+ log.info('http_access', fields);
1455
2071
  };
1456
2072
  res.on('finish', () => emit(false));
1457
2073
  res.on('close', () => emit(true)); // aborted or disconnected responses
@@ -1576,7 +2192,8 @@ function createMvpApp(config, engineOverride) {
1576
2192
  const namesSeen = new Set();
1577
2193
  const noteConnectionName = (raw) => {
1578
2194
  const name = Array.isArray(raw) ? raw[0] : raw;
1579
- const identity = typeof name === 'string' ? trustedIdentity(name) : 'unnamed';
2195
+ // Already agentOf's answer, which applied the rule its connection's kind needs: only the exact rule is left to apply.
2196
+ const identity = typeof name === 'string' ? trustedIdentity(name, false) : 'unnamed';
1580
2197
  if (identity === 'unnamed' || namesSeen.has(identity))
1581
2198
  return;
1582
2199
  try {
@@ -1799,7 +2416,7 @@ function createMvpApp(config, engineOverride) {
1799
2416
  // calls, isPlatformInitialize) gets no id: it still counts as a connection, but it writes no first call and its mcp_init
1800
2417
  // carries no id for the recall share to join, so the share counts agents only.
1801
2418
  const telemetrySession = (0, telemetry_1.isPlatformInitialize)(req.body) ? null : crypto_1.default.randomBytes(8).toString('hex');
1802
- const sessionServer = buildMcpServer(db, config, engine, activity, digests, usefulness, connection, true, telemetrySession, isDashboardCall(req.headers, config), isDirectoryCall(req.headers, config));
2419
+ const sessionServer = buildMcpServer(db, config, engine, activity, digests, usefulness, connection, true, telemetrySession, isDashboardCall(req.headers, config), isDirectoryCall(req.headers, config), dreamer);
1803
2420
  transport.onclose = () => {
1804
2421
  sseSessions.delete(sessionId);
1805
2422
  (0, telemetry_1.emitTelemetry)({ event: 'session', kind: 'mcp_close' });
@@ -1865,13 +2482,13 @@ function createMvpApp(config, engineOverride) {
1865
2482
  app.use((err, req, res, next) => {
1866
2483
  if (res.headersSent)
1867
2484
  return next(err);
1868
- const status = Number.isInteger(err?.status) && err.status >= 400 && err.status < 600 ? err.status : 500;
2485
+ const status = typeof err?.status === 'number' && Number.isInteger(err.status) && err.status >= 400 && err.status < 600 ? err.status : 500;
1869
2486
  const message = status === 400 ? 'Parse error'
1870
2487
  : status === 413 ? 'Request body too large'
1871
2488
  : status === 415 ? 'Unsupported media type'
1872
2489
  : status >= 500 ? 'Internal error' : 'Invalid Request';
1873
2490
  if (status >= 500)
1874
- console.error('[http] unhandled error:', err);
2491
+ log.error('http_unhandled', { status }, err);
1875
2492
  if (req.path === MCP_PATH) {
1876
2493
  const code = status === 400 ? -32700 : status >= 500 ? -32603 : -32600;
1877
2494
  res.status(status).json({ jsonrpc: '2.0', error: { code, message }, id: null });
@@ -1891,9 +2508,12 @@ function createMvpApp(config, engineOverride) {
1891
2508
  idle,
1892
2509
  digests,
1893
2510
  reindexPending: pendingPass,
2511
+ dreamer,
1894
2512
  close: () => {
1895
2513
  closing ??= (async () => {
1896
2514
  // First: it is background work, and what it has started it finishes before the database closes.
2515
+ dreamer?.stop();
2516
+ await dreamer?.inFlight()?.catch(() => undefined);
1897
2517
  (0, engine_1.runningEmbeddingRebuild)(engine)?.abort();
1898
2518
  (0, engine_1.runningBackgroundIndex)(db)?.abort();
1899
2519
  // The digest worker uses the database too: its sweep in flight ends before the database closes.
@@ -1912,6 +2532,7 @@ function createMvpApp(config, engineOverride) {
1912
2532
  running.abort();
1913
2533
  await running.done;
1914
2534
  }
2535
+ stopHandoffSweep?.();
1915
2536
  db.close();
1916
2537
  // The query cache this process opened (an injected provider is its owner's to close).
1917
2538
  if (engineOverride === undefined)
@@ -2024,9 +2645,10 @@ exports.SEARCH_REFUSAL_REASONS = {
2024
2645
  'fts-query': 'invalid_query',
2025
2646
  };
2026
2647
  /** A search reply: ok as the search answered it, and its refusal code beside it when it did not search. */
2027
- function searchResult(body, result) {
2028
- const response = { ...jsonResult(result), isError: !body.ok };
2029
- return body.ok ? response : (0, toolRefusal_1.markToolRefusal)(response, exports.SEARCH_REFUSAL_REASONS[String(body.error)] ?? 'refused');
2648
+ function searchResult(body, result, structured) {
2649
+ if (body.ok)
2650
+ return { ...jsonResultOf(result, structured), isError: false };
2651
+ return (0, toolRefusal_1.markToolRefusal)(refusalResult(result), exports.SEARCH_REFUSAL_REASONS[String(body.error)] ?? 'refused');
2030
2652
  }
2031
2653
  /**
2032
2654
  * Startup for the MVP service: open the derived index, AWAIT the ledger replay
@@ -2042,6 +2664,7 @@ async function startMvpService(config, engineOverride) {
2042
2664
  // Restart recovery: replay the authoritative ledger before readiness.
2043
2665
  // Start-up is the one place a torn final event is set aside: nothing else is appending to this ledger yet.
2044
2666
  await (0, lifecycle_1.replayLedger)(mvp.db, config.memoryDir, config.ledgerRelPath, { setTornTailAside: true });
2667
+ mvp.dreamer?.start();
2045
2668
  // The notes of a folder that moved were removed from the index with their old paths: they are indexed at their
2046
2669
  // current paths before the service listens, so the first answer has them. The mark is cleared only by a pass with no
2047
2670
  // failed file. A file whose embedding the provider still refuses after its retries counts as failed: it keeps its
@@ -2051,7 +2674,7 @@ async function startMvpService(config, engineOverride) {
2051
2674
  if (pass.errors === 0)
2052
2675
  (0, outsideRoot_1.clearReindexPending)(mvp.db);
2053
2676
  else
2054
- console.error(`[startup] ${pass.errors} file(s) did not index at start; the folder stays owed a pass and the next start runs it again`);
2677
+ log.error('startup_index_incomplete', { failedFiles: pass.errors, detail: 'the next start runs the pass again' });
2055
2678
  }
2056
2679
  // Bind by listening, not by the callback Express gives: `app.listen` routes a bind failure to
2057
2680
  // that same callback as a success, so a promise waiting on it resolves with a server that never