renor-engine 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/README.md +21 -1
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/dist/BUILD.json +6 -0
  4. package/dist/cli.js +1656 -516
  5. package/package.json +16 -80
  6. package/dist/.tsbuildinfo +0 -1
  7. package/dist/api/apiAuth.js +0 -43
  8. package/dist/api/server.js +0 -174
  9. package/dist/buildIdentity.js +0 -149
  10. package/dist/callPhases.js +0 -82
  11. package/dist/cli-extractors.js +0 -46
  12. package/dist/config.js +0 -65
  13. package/dist/context/composer.js +0 -320
  14. package/dist/context/readHonesty.js +0 -478
  15. package/dist/context/rowStore.js +0 -238
  16. package/dist/conversation/formatter.js +0 -81
  17. package/dist/conversation/parser.js +0 -193
  18. package/dist/conversation/types.js +0 -2
  19. package/dist/conversation/writer.js +0 -42
  20. package/dist/db/client.js +0 -149
  21. package/dist/db/handoffTable.js +0 -31
  22. package/dist/db/indexSalvage.js +0 -101
  23. package/dist/db/migrations.js +0 -670
  24. package/dist/db/readCache.js +0 -109
  25. package/dist/db/schema.js +0 -5
  26. package/dist/db/sqlite.js +0 -396
  27. package/dist/db/statements.js +0 -89
  28. package/dist/digests/digestCache.js +0 -146
  29. package/dist/digests/digests.js +0 -597
  30. package/dist/digests/worker.js +0 -350
  31. package/dist/embeddings/caching.js +0 -337
  32. package/dist/embeddings/coalescing.js +0 -177
  33. package/dist/embeddings/interactive.js +0 -103
  34. package/dist/embeddings/local.js +0 -70
  35. package/dist/embeddings/openai.js +0 -106
  36. package/dist/embeddings/rebuild.js +0 -166
  37. package/dist/embeddings/simdScan.js +0 -260
  38. package/dist/embeddings/space.js +0 -21
  39. package/dist/embeddings/types.js +0 -14
  40. package/dist/embeddings/vectorIndex.js +0 -674
  41. package/dist/embeddings/vertex.js +0 -334
  42. package/dist/eval/artifacts.js +0 -42
  43. package/dist/eval/cacheReport.js +0 -146
  44. package/dist/eval/comparison.js +0 -182
  45. package/dist/eval/context700-candidate.js +0 -145
  46. package/dist/eval/evalCache.js +0 -568
  47. package/dist/eval/hashEmbedding.js +0 -35
  48. package/dist/eval/indexSnapshots.js +0 -549
  49. package/dist/eval/longmemeval.js +0 -56
  50. package/dist/eval/metrics.js +0 -181
  51. package/dist/eval/pageDump.js +0 -67
  52. package/dist/eval/renorbenchBaseline.js +0 -486
  53. package/dist/eval/renorbenchDocuments.js +0 -134
  54. package/dist/eval/renorbenchProduct.js +0 -182
  55. package/dist/eval/runLedger.js +0 -279
  56. package/dist/eval/runManifest.js +0 -485
  57. package/dist/eval/scoring.js +0 -197
  58. package/dist/eval/textTokens.js +0 -17
  59. package/dist/evidence/attach.js +0 -264
  60. package/dist/evidence/background.js +0 -147
  61. package/dist/evidence/evidenceCache.js +0 -182
  62. package/dist/evidence/gate.js +0 -37
  63. package/dist/evidence/keyPool.js +0 -85
  64. package/dist/evidence/ollamaChat.js +0 -84
  65. package/dist/evidence/optin.js +0 -165
  66. package/dist/evidence/options.js +0 -151
  67. package/dist/evidence/secondPass.js +0 -294
  68. package/dist/extraction/classifyCapture.js +0 -34
  69. package/dist/extraction/decision-model.js +0 -174
  70. package/dist/extraction/eventExtractor.js +0 -113
  71. package/dist/extraction/extractionClient.js +0 -122
  72. package/dist/extraction/factExtractor.js +0 -46
  73. package/dist/extraction/factsWritePath.js +0 -179
  74. package/dist/extraction/jevDecisionModel.js +0 -307
  75. package/dist/extraction/preferenceExtractor.js +0 -150
  76. package/dist/extraction/secretGuard.js +0 -180
  77. package/dist/extraction/secretScanner.js +0 -665
  78. package/dist/extraction/typedFacts.js +0 -224
  79. package/dist/extraction/writePathAdjudication.js +0 -110
  80. package/dist/importing/apply.js +0 -672
  81. package/dist/importing/bundle-write.js +0 -80
  82. package/dist/importing/cli.js +0 -408
  83. package/dist/importing/existing.js +0 -51
  84. package/dist/importing/facts.js +0 -121
  85. package/dist/importing/forgetImported.js +0 -79
  86. package/dist/importing/hosted.js +0 -467
  87. package/dist/importing/journalLog.js +0 -96
  88. package/dist/importing/markdown.js +0 -98
  89. package/dist/importing/plan.js +0 -201
  90. package/dist/importing/secretScreen.js +0 -113
  91. package/dist/importing/serialize.js +0 -188
  92. package/dist/importing/source.js +0 -463
  93. package/dist/importing/types.js +0 -6
  94. package/dist/indexer/chunker.js +0 -319
  95. package/dist/indexer/dailyEntries.js +0 -63
  96. package/dist/indexer/hasher.js +0 -10
  97. package/dist/indexer/laneRuns.js +0 -48
  98. package/dist/indexer/o200k.js +0 -481
  99. package/dist/indexer/outsideRoot.js +0 -77
  100. package/dist/indexer/parser.js +0 -170
  101. package/dist/indexer/pipeline.js +0 -988
  102. package/dist/indexer/staleness.js +0 -201
  103. package/dist/indexer/tokenizer.js +0 -77
  104. package/dist/indexer/walker.js +0 -170
  105. package/dist/learning/cli.js +0 -113
  106. package/dist/learning/schema.js +0 -144
  107. package/dist/learning/settings.js +0 -45
  108. package/dist/learning/store.js +0 -267
  109. package/dist/learning/tap.js +0 -401
  110. package/dist/mcp/hive.js +0 -88
  111. package/dist/mcp/requestErrors.js +0 -47
  112. package/dist/mcp/server.js +0 -426
  113. package/dist/memory/agentLease.js +0 -43
  114. package/dist/memory/confinedWrite.js +0 -315
  115. package/dist/memory/controls.js +0 -73
  116. package/dist/memory/dailyNote.js +0 -265
  117. package/dist/memory/dreamer/adjudicate.js +0 -215
  118. package/dist/memory/dreamer/apply.js +0 -120
  119. package/dist/memory/dreamer/budget.js +0 -64
  120. package/dist/memory/dreamer/candidates.js +0 -149
  121. package/dist/memory/dreamer/collection.js +0 -104
  122. package/dist/memory/dreamer/dreamer.js +0 -352
  123. package/dist/memory/dreamer/review.js +0 -111
  124. package/dist/memory/durableAppend.js +0 -57
  125. package/dist/memory/factConflicts.js +0 -205
  126. package/dist/memory/factTerms.js +0 -321
  127. package/dist/memory/handoff.js +0 -363
  128. package/dist/memory/hiveNameHeader.cjs +0 -10
  129. package/dist/memory/inventory.js +0 -55
  130. package/dist/memory/invisibleText.js +0 -20
  131. package/dist/memory/lifecycle.js +0 -2014
  132. package/dist/memory/limits.js +0 -13
  133. package/dist/memory/requests.js +0 -183
  134. package/dist/memory/sourceName.js +0 -276
  135. package/dist/memory/statusFacts.js +0 -61
  136. package/dist/memory/whatsNew.js +0 -158
  137. package/dist/memory/writer.js +0 -176
  138. package/dist/memory/writerConflicts.js +0 -85
  139. package/dist/memorymap/memoryMap.js +0 -1594
  140. package/dist/memorymap/replacedJournal.js +0 -58
  141. package/dist/memorymap/replacements.js +0 -410
  142. package/dist/mvp/activity.js +0 -86
  143. package/dist/mvp/agentMemory.js +0 -1803
  144. package/dist/mvp/answerCheck.js +0 -211
  145. package/dist/mvp/auth.js +0 -531
  146. package/dist/mvp/cli.js +0 -86
  147. package/dist/mvp/client-store.js +0 -462
  148. package/dist/mvp/compileCache.js +0 -242
  149. package/dist/mvp/compileSeed.js +0 -33
  150. package/dist/mvp/config.js +0 -292
  151. package/dist/mvp/connectionStatus.js +0 -80
  152. package/dist/mvp/contextDistinct.js +0 -53
  153. package/dist/mvp/contextProfile.js +0 -87
  154. package/dist/mvp/contextRelevance.js +0 -413
  155. package/dist/mvp/directorySurface.js +0 -68
  156. package/dist/mvp/engine.js +0 -1078
  157. package/dist/mvp/exportAll.js +0 -393
  158. package/dist/mvp/factLanePack.js +0 -77
  159. package/dist/mvp/factMarkers.js +0 -65
  160. package/dist/mvp/factsLane.js +0 -203
  161. package/dist/mvp/hiddenSpans.js +0 -585
  162. package/dist/mvp/history.js +0 -414
  163. package/dist/mvp/hitDates.js +0 -31
  164. package/dist/mvp/hitFloor.js +0 -36
  165. package/dist/mvp/identity.js +0 -18
  166. package/dist/mvp/idle.js +0 -113
  167. package/dist/mvp/log.js +0 -118
  168. package/dist/mvp/memoryDir.js +0 -98
  169. package/dist/mvp/posthogMcp.js +0 -210
  170. package/dist/mvp/readScope.js +0 -104
  171. package/dist/mvp/readUsefulness.js +0 -222
  172. package/dist/mvp/sanitize.js +0 -48
  173. package/dist/mvp/schemaRejections.js +0 -91
  174. package/dist/mvp/scope.js +0 -127
  175. package/dist/mvp/server.js +0 -2712
  176. package/dist/mvp/serverCard.js +0 -38
  177. package/dist/mvp/storedSize.js +0 -139
  178. package/dist/mvp/telemetry.js +0 -474
  179. package/dist/net/poolFetch.js +0 -174
  180. package/dist/recall/actionRecall.js +0 -282
  181. package/dist/recall/autoRecall.js +0 -447
  182. package/dist/recall/searchRecall.js +0 -189
  183. package/dist/retrieval/adviceLines.js +0 -195
  184. package/dist/retrieval/adviceQuestion.js +0 -44
  185. package/dist/retrieval/conversationShape.js +0 -44
  186. package/dist/retrieval/dailyEntryTime.js +0 -55
  187. package/dist/retrieval/entityLookup.js +0 -162
  188. package/dist/retrieval/factsFirst.js +0 -188
  189. package/dist/retrieval/keyword.js +0 -84
  190. package/dist/retrieval/ledgerEventTime.js +0 -51
  191. package/dist/retrieval/ledgerLiveness.js +0 -83
  192. package/dist/retrieval/ledgerPresentation.js +0 -161
  193. package/dist/retrieval/noteIdentity.js +0 -57
  194. package/dist/retrieval/orchestrator.js +0 -1224
  195. package/dist/retrieval/plainWording.js +0 -236
  196. package/dist/retrieval/queryHelpers.js +0 -142
  197. package/dist/retrieval/questionSpan.js +0 -475
  198. package/dist/retrieval/rankingTrace.js +0 -184
  199. package/dist/retrieval/readerEvents.js +0 -690
  200. package/dist/retrieval/readerLayout.js +0 -422
  201. package/dist/retrieval/relativeTimeHints.js +0 -113
  202. package/dist/retrieval/semantic.js +0 -67
  203. package/dist/router/routerCache.js +0 -106
  204. package/dist/router/sessionRouter.js +0 -726
  205. package/dist/sharing/cli.js +0 -181
  206. package/dist/sharing/examples.js +0 -136
  207. package/dist/sharing/fields.js +0 -225
  208. package/dist/sharing/loop.js +0 -80
  209. package/dist/sharing/project.js +0 -327
  210. package/dist/sharing/send.js +0 -466
  211. package/dist/sharing/state.js +0 -222
  212. package/dist/sharing/wording.js +0 -48
  213. package/dist/toolRefusal.js +0 -120
  214. package/dist/watcher/watcher.js +0 -77
  215. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
@@ -1,158 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WHATS_NEW_EXCERPT_CHARS = exports.WHATS_NEW_REQUEST_LIMIT = exports.WHATS_NEW_LIMIT = void 0;
4
- exports.readWhatsNew = readWhatsNew;
5
- const scope_1 = require("../mvp/scope");
6
- const requests_1 = require("./requests");
7
- /** How many items one call returns. A full page comes with a cursor for the rest. */
8
- exports.WHATS_NEW_LIMIT = 50;
9
- const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
10
- /**
11
- * At most this many open requests lead a reply (the newest); the rest are counted (`more_requests`). list_requests
12
- * returns the same page: the rest come up as these are answered or closed.
13
- */
14
- exports.WHATS_NEW_REQUEST_LIMIT = requests_1.REQUEST_PAGE_LIMIT;
15
- /** One feed item carries this many characters of another agent's text. list_requests still returns the full question. */
16
- exports.WHATS_NEW_EXCERPT_CHARS = 240;
17
- function excerpt(text) {
18
- if (text === null)
19
- return null;
20
- let end = 0;
21
- let count = 0;
22
- for (const point of text) {
23
- if (count === exports.WHATS_NEW_EXCERPT_CHARS)
24
- break;
25
- end += point.length;
26
- count++;
27
- }
28
- return text.slice(0, end);
29
- }
30
- // A sixth part, "peek", marks a window opened with an explicit `since`: its pages never move the mark either.
31
- function cursorOf(floorAt, floorId, beforeAt, beforeId, asOf, peek) {
32
- const parts = [floorAt, String(floorId), beforeAt, String(beforeId), asOf];
33
- if (peek)
34
- parts.push('peek');
35
- return parts.join('|');
36
- }
37
- function parseCursor(cursor) {
38
- const parts = cursor.split('|');
39
- if ((parts.length !== 5 && !(parts.length === 6 && parts[5] === 'peek')) || !parts[0] || !parts[2] || !parts[4]) {
40
- throw new Error('whats_new cursor is not one this workspace issued');
41
- }
42
- const floorId = Number(parts[1]);
43
- const beforeId = Number(parts[3]);
44
- if (!Number.isInteger(floorId) || !Number.isInteger(beforeId)) {
45
- throw new Error('whats_new cursor is not one this workspace issued');
46
- }
47
- return { floorAt: parts[0], floorId, beforeAt: parts[2], beforeId, asOf: parts[4], peek: parts.length === 6 };
48
- }
49
- /**
50
- * What was saved after this agent's mark, newest first. The mark moves to `as_of` only when the
51
- * page was not full, so a bounded reply cannot skip the rest. An explicit `since` is a peek: it
52
- * never moves the mark, so writes between the mark and `since` are still shown by the next plain call. This agent's own rows are left out
53
- * unless `includeOwn`. The first call, with no mark, starts seven days back.
54
- */
55
- function readWhatsNew(db, args) {
56
- const limit = args.limit ?? exports.WHATS_NEW_LIMIT;
57
- const now = (args.now ?? new Date()).toISOString();
58
- // One scope has several spellings (atlas, scope:atlas, project:atlas): read all of them and keep one mark.
59
- const scope = args.scope !== undefined && args.scope.trim() !== '' ? (0, scope_1.normalizeScope)(args.scope) : null;
60
- const scopeKey = scope ? (0, scope_1.scopeSpellings)(scope)[0] : '';
61
- const page = args.cursor ? parseCursor(args.cursor) : null;
62
- let floorAt;
63
- let floorId;
64
- let asOf;
65
- let beforeAt = null;
66
- let beforeId = null;
67
- let peek = false;
68
- if (page) {
69
- peek = page.peek;
70
- floorAt = page.floorAt;
71
- floorId = page.floorId;
72
- asOf = page.asOf;
73
- beforeAt = page.beforeAt;
74
- beforeId = page.beforeId;
75
- }
76
- else if (args.since) {
77
- peek = true;
78
- floorAt = args.since;
79
- floorId = -1;
80
- asOf = now;
81
- }
82
- else {
83
- const stored = db.prepare('SELECT last_at, last_id FROM agent_cursors WHERE agent = ? AND scope = ?').get(args.agent, scopeKey);
84
- if (stored) {
85
- floorAt = stored.last_at;
86
- floorId = stored.last_id;
87
- }
88
- else {
89
- floorAt = new Date(Date.parse(now) - WEEK_MS).toISOString();
90
- floorId = -1;
91
- }
92
- asOf = now;
93
- }
94
- const params = [floorAt, floorAt, floorId, asOf];
95
- let beforeSql = '';
96
- if (beforeAt !== null && beforeId !== null) {
97
- beforeSql = ' AND (created_at < ? OR (created_at = ? AND id < ?))';
98
- params.push(beforeAt, beforeAt, beforeId);
99
- }
100
- let ownSql = '';
101
- if (!args.includeOwn) {
102
- ownSql = ' AND (author IS NULL OR author != ?)';
103
- params.push(args.agent);
104
- }
105
- let scopeSql = '';
106
- if (scope) {
107
- // A row written without a scope (before migration 19, or a note retire/restore) is global.
108
- const saved = (0, scope_1.savedScopeSqlPredicate)(scope, "COALESCE(scope, 'global')");
109
- scopeSql = ` AND ${saved.sql}`;
110
- params.push(...saved.params);
111
- }
112
- params.push(limit + 1);
113
- const rows = db.prepare(`SELECT id, action, summary, created_at, author, scope FROM write_events
114
- WHERE (created_at > ? OR (created_at = ? AND id > ?))
115
- AND created_at < ?
116
- ${beforeSql}${ownSql}${scopeSql}
117
- ORDER BY created_at DESC, id DESC
118
- LIMIT ?`).all(...params);
119
- const more = rows.length > limit;
120
- const kept = more ? rows.slice(0, limit) : rows;
121
- const logged = kept.map((row) => {
122
- const source = row.author && row.author !== '' ? row.author : 'unnamed';
123
- const other = Boolean(row.author && row.author !== '' && row.author !== args.agent);
124
- return {
125
- kind: row.action,
126
- at: row.created_at,
127
- source,
128
- scope: row.scope,
129
- summary: excerpt(row.summary),
130
- ...(other ? { untrusted: true } : {}),
131
- };
132
- });
133
- // Open requests stay at the front of the first page until they are answered or closed.
134
- // A later cursor page does not repeat them.
135
- const waiting = args.cursor ? { requests: [] } : (0, requests_1.listRequestsPage)(db, { agent: args.agent, status: 'open', scope: args.scope });
136
- const open = waiting.requests.map((request) => ({
137
- kind: 'request',
138
- at: request.created_at,
139
- source: request.from_agent,
140
- scope: request.scope,
141
- summary: excerpt(request.question),
142
- requestId: request.id,
143
- untrusted: true,
144
- }));
145
- const extra = waiting.more_requests
146
- ? { more_requests: waiting.more_requests, more_requests_hint: waiting.more_requests_hint }
147
- : {};
148
- const items = [...open, ...logged];
149
- if (more) {
150
- const last = kept[kept.length - 1];
151
- return { items, as_of: asOf, next_since: null, cursor: cursorOf(floorAt, floorId, last.created_at, last.id, asOf, peek), ...extra };
152
- }
153
- if (peek)
154
- return { items, as_of: asOf, next_since: asOf, cursor: null, ...extra };
155
- db.prepare(`INSERT INTO agent_cursors (agent, scope, last_at, last_id) VALUES (?, ?, ?, ?)
156
- ON CONFLICT(agent, scope) DO UPDATE SET last_at = excluded.last_at, last_id = excluded.last_id`).run(args.agent, scopeKey, asOf, -1);
157
- return { items, as_of: asOf, next_since: asOf, cursor: null, ...extra };
158
- }
@@ -1,176 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.withDailyFileLock = withDailyFileLock;
7
- exports.entryClock = entryClock;
8
- exports.appendDailyMemory = appendDailyMemory;
9
- exports.appendDailyEntry = appendDailyEntry;
10
- const path_1 = __importDefault(require("path"));
11
- const limits_1 = require("./limits");
12
- const invisibleText_1 = require("./invisibleText");
13
- const dailyNote_1 = require("./dailyNote");
14
- const sourceName_1 = require("./sourceName");
15
- const secretGuard_1 = require("../extraction/secretGuard");
16
- const jevDecisionModel_1 = require("../extraction/jevDecisionModel");
17
- const toolRefusal_1 = require("../toolRefusal");
18
- const controls_1 = require("./controls");
19
- const confinedWrite_1 = require("./confinedWrite");
20
- /**
21
- * A note entry is the user's own words, so it is kept whole: nothing is trimmed, stripped or cut. What
22
- * cannot be kept is refused instead, which makes a save that succeeds a save that stored every
23
- * character, and a refused save one that wrote nothing.
24
- */
25
- function checkedNoteText(text) {
26
- if (typeof text !== 'string')
27
- throw (0, toolRefusal_1.refusal)('invalid_argument', 'the note must be a string');
28
- if (text.length > limits_1.MAX_STATEMENT_LENGTH) {
29
- throw (0, toolRefusal_1.refusal)('field_too_long', `the note exceeds the maximum length of ${limits_1.MAX_STATEMENT_LENGTH} (got ${text.length}); nothing was written`);
30
- }
31
- // An unpaired surrogate is not text: UTF-8 cannot hold it, so writing it would store U+FFFD instead.
32
- if (/\p{Cs}/u.test(text)) {
33
- throw (0, toolRefusal_1.refusal)('invalid_text', 'the note contains an unpaired UTF-16 surrogate, which cannot be stored as text; nothing was written');
34
- }
35
- // A control character is not text either, and the filesystem and SQLite both accept one, so a note
36
- // holding NUL would be written, indexed and served as if it were ordinary words. Refused instead
37
- // (memory/controls.ts), so a save that succeeds is a save that stored nothing but the words.
38
- (0, controls_1.assertNoUnstorableControls)('the note', text);
39
- if ((0, invisibleText_1.isBlankOrInvisible)(text))
40
- throw (0, toolRefusal_1.refusal)('invalid_argument', 'the note is blank (only whitespace or invisible characters); nothing was written');
41
- return text;
42
- }
43
- /**
44
- * The forms of a source the secret guard judges: the one the header stores (headerSource: letters, digits and hyphens,
45
- * the other characters written as hyphens) and the same name with every other character deleted, which is how the header
46
- * stored it before. Spacing a key apart does not stop it being a key: the stored form joins the pieces with hyphens, and
47
- * the deleted form joins them whole.
48
- */
49
- function sourceFormsForGuard(source) {
50
- if (typeof source !== 'string')
51
- return [];
52
- return [(0, sourceName_1.headerSource)(source), (0, sourceName_1.cleanSource)(source).replace(/[^\p{L}\p{N}-]/gu, '')];
53
- }
54
- function isValidCalendarDate(date) {
55
- const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date);
56
- if (!match)
57
- return false;
58
- const year = Number(match[1]);
59
- const month = Number(match[2]);
60
- const day = Number(match[3]);
61
- const parsed = new Date(Date.UTC(year, month - 1, day));
62
- return parsed.getUTCFullYear() === year
63
- && parsed.getUTCMonth() === month - 1
64
- && parsed.getUTCDate() === day;
65
- }
66
- /**
67
- * In-process mutual exclusion per daily file. Every writer of a daily file (append here, and the
68
- * memory map's retire/restore, which rewrites one) runs inside this, so a rewrite can never drop
69
- * an append that landed between its read and its write. One tenant runs one engine process.
70
- */
71
- const dailyFileLocks = new Map();
72
- function withDailyFileLock(filePath, fn) {
73
- const key = path_1.default.resolve(filePath);
74
- const prev = dailyFileLocks.get(key) ?? Promise.resolve();
75
- const run = prev.then(fn, fn);
76
- const settled = run.then(() => undefined, () => undefined);
77
- dailyFileLocks.set(key, settled);
78
- void settled.then(() => {
79
- if (dailyFileLocks.get(key) === settled)
80
- dailyFileLocks.delete(key);
81
- });
82
- return run;
83
- }
84
- /** A fixed offset as a client may give it: `-03:00`, `-0300`, or `Z`. */
85
- const FIXED_OFFSET_RE = /^(?:Z|([+-])(\d{2}):?(\d{2}))$/;
86
- const invalidTimeZone = (timeZone) => new Error(`timezone ${JSON.stringify(timeZone.slice(0, 64))} is not a time zone: give an IANA name such as "America/Sao_Paulo" or a UTC offset such as "-03:00"; nothing was written`);
87
- /**
88
- * The day and time to stamp an entry with, read once from `now`. In UTC unless the client names the user's time zone,
89
- * an IANA name ("America/Sao_Paulo") or a fixed offset ("-03:00"): the entry is then filed under the user's own day at
90
- * their own time, so a note said at 21:00 in São Paulo belongs to that evening, not to the next UTC day. A name that is
91
- * neither is refused rather than read as UTC, so nothing is filed under a day the user did not mean.
92
- */
93
- function entryClock(now, timeZone) {
94
- if (timeZone === undefined) {
95
- const iso = now.toISOString();
96
- return { date: iso.slice(0, 10), time: iso.slice(11, 19) };
97
- }
98
- let minutes;
99
- const fixed = FIXED_OFFSET_RE.exec(timeZone.trim());
100
- if (fixed) {
101
- minutes = fixed[1] ? (fixed[1] === '-' ? -1 : 1) * (Number(fixed[2]) * 60 + Number(fixed[3])) : 0;
102
- if (Number(fixed[3] ?? 0) > 59 || Math.abs(minutes) > 14 * 60)
103
- throw invalidTimeZone(timeZone);
104
- }
105
- else {
106
- let named;
107
- try {
108
- named = new Intl.DateTimeFormat('en-US', { timeZone: timeZone.trim(), timeZoneName: 'longOffset' })
109
- .formatToParts(now).find(p => p.type === 'timeZoneName')?.value;
110
- }
111
- catch {
112
- throw invalidTimeZone(timeZone);
113
- }
114
- // `GMT-03:00`, or `GMT` alone at UTC itself.
115
- const m = named === undefined ? null : /^GMT(?:([+-])(\d{2}):(\d{2}))?$/.exec(named);
116
- if (!m)
117
- throw invalidTimeZone(timeZone);
118
- minutes = m[1] ? (m[1] === '-' ? -1 : 1) * (Number(m[2]) * 60 + Number(m[3])) : 0;
119
- }
120
- const iso = new Date(now.getTime() + minutes * 60_000).toISOString();
121
- if (minutes === 0)
122
- return { date: iso.slice(0, 10), time: iso.slice(11, 19) };
123
- const abs = Math.abs(minutes);
124
- const offset = `${minutes < 0 ? '-' : '+'}${String(Math.floor(abs / 60)).padStart(2, '0')}:${String(abs % 60).padStart(2, '0')}`;
125
- return { date: iso.slice(0, 10), time: iso.slice(11, 19), offset };
126
- }
127
- /**
128
- * Appends one entry to the daily note of `date`. `clock` stamps it (its time, and its offset when not UTC); without
129
- * one it is stamped with the current time in UTC. Returns the file's path.
130
- */
131
- async function appendDailyMemory(vaultPath, date, entry, memoryDir = 'Memory/Daily', clock) {
132
- return (await appendDailyEntry(vaultPath, date, entry, memoryDir, clock)).filePath;
133
- }
134
- /**
135
- * `appendDailyMemory`, saying what it wrote: the file, and the entry exactly as it went in (without the line break
136
- * that separates it from the one above), which is what the memory map's id for it is made from.
137
- */
138
- async function appendDailyEntry(vaultPath, date, entry, memoryDir = 'Memory/Daily', clock) {
139
- if (!isValidCalendarDate(date)) {
140
- throw new Error('date must be a valid YYYY-MM-DD calendar date');
141
- }
142
- // The header must read back as an entry, or the entry would be folded into the one above it.
143
- if (clock && (!/^\d{2}:\d{2}:\d{2}$/.test(clock.time) || (clock.offset !== undefined && !/^[+-]\d{2}:\d{2}$/.test(clock.offset)))) {
144
- throw new Error('entry time must be HH:MM:SS, with an offset of the form +HH:MM; nothing was written');
145
- }
146
- const text = checkedNoteText(entry.text);
147
- // RENOR_SECRET_GUARD (default ON): refuse a note that looks like it carries a secret before anything is
148
- // written, the directory included. The source is scanned as it will be stored, and joined: removing characters
149
- // can join the pieces of a key that were spaced apart (sourceFormsForGuard).
150
- if ((0, secretGuard_1.secretGuardEnabled)()) {
151
- await (0, secretGuard_1.assertStorable)([
152
- { field: 'note text', text },
153
- ...sourceFormsForGuard(entry.source).map(form => ({ field: 'note source', text: form, nameOnly: true })),
154
- ], 'save', (0, jevDecisionModel_1.saveDecisionModel)());
155
- }
156
- const dir = path_1.default.join(vaultPath, memoryDir);
157
- const filePath = path_1.default.join(dir, `${date}.md`);
158
- const resolvedPath = path_1.default.resolve(filePath);
159
- const resolvedBase = path_1.default.resolve(dir);
160
- if (!resolvedPath.startsWith(resolvedBase)) {
161
- throw new Error('Invalid date: path traversal detected');
162
- }
163
- // The source's name, in the alphabet a header keeps: letters, digits and hyphens only (sourceName.ts)
164
- const sanitizedSource = (0, sourceName_1.headerSource)(entry.source);
165
- const time = clock?.time ?? new Date().toISOString().substring(11, 19);
166
- // The user's words go in as said, except that a later line which would start like an entry is kept behind a
167
- // backslash so it cannot forge one, and the entry's header then says so (see dailyNote.ts). Both are
168
- // reversible, and every reader undoes them. A file outside the daily-note layout is written as said.
169
- const written = (0, dailyNote_1.formatEntry)(time, sanitizedSource, text, (0, dailyNote_1.isDailyNotePath)(filePath), clock?.offset);
170
- const line = `\n${written}`;
171
- // Appended in place, or created with its frontmatter, through confinedWrite: a link, a hard link or a FIFO planted in the
172
- // vault is refused, never followed, and a failed save leaves the note as it was (see appendConfined).
173
- const relFile = path_1.default.relative(path_1.default.resolve(vaultPath), resolvedPath);
174
- await withDailyFileLock(filePath, () => (0, confinedWrite_1.appendConfined)(vaultPath, relFile, line, (0, dailyNote_1.engineDailyHeader)(date)));
175
- return { filePath, entry: written };
176
- }
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.UNNAMED_WRITER = void 0;
4
- exports.writerOf = writerOf;
5
- exports.displacementsAfter = displacementsAfter;
6
- exports.hasOpenDisplacementBy = hasOpenDisplacementBy;
7
- exports.writerConflictLine = writerConflictLine;
8
- exports.writerConflictLines = writerConflictLines;
9
- const statements_1 = require("../db/statements");
10
- /**
11
- * Writer conflicts: when a writer replaces a value another writer saved, both values stay visible, marked, until the
12
- * writer that lost the value saves the same subject and predicate again.
13
- *
14
- * The writer of a fact is the identity the gateway (or the OAuth client) authenticated for the connection that saved it
15
- * (`agentOf` in mvp/server.ts). It is never the `source` the caller typed, which says nothing a caller cannot choose. A
16
- * fact with no writer recorded (written before this table, or by a path that names none) was saved by its source, so its
17
- * writer is its source, or `unnamed` when it has none.
18
- *
19
- * Each fact a write leaves current carries the displacements still open on it, in `fact_writers` keyed by its event:
20
- * - a save by the same writer keeps every open displacement (the other writer has not confirmed yet);
21
- * - a save by another writer clears the displacements that writer owns, whichever value it saves: that is its
22
- * confirmation. A save by a third writer clears nothing it does not own, so the other displacements stay open;
23
- * - it opens a displacement for the value it replaces (the value, its writer and its date), unless that value is the
24
- * one that displaced the writer's own displacement (it answered its counter-claim) or the two values are the same text.
25
- * Replay derives the same rows from the ledger. A live index has no rows for facts saved before migration 26, so those
26
- * have no displacements on it; a rebuild from the ledger derives rows for them, so a rebuilt index can show a line the
27
- * live one does not. A fact with no row has no displacements.
28
- */
29
- /** The writer a write has when it has no authenticated identity and no source. */
30
- exports.UNNAMED_WRITER = 'unnamed';
31
- /** The writer a fact event recorded, and the displacements open on it. */
32
- function writerOf(db, eventId, source) {
33
- const row = eventId === null ? undefined : (0, statements_1.prepareOnce)(db, 'SELECT writer, displaced FROM fact_writers WHERE event_id = ?').get(eventId);
34
- if (row)
35
- return { writer: row.writer, displaced: JSON.parse(row.displaced) };
36
- return { writer: source || exports.UNNAMED_WRITER, displaced: [] };
37
- }
38
- /** Two values agree when they are the same text once composed and trimmed. */
39
- const sameValue = (a, b) => a.normalize('NFC').trim() === b.normalize('NFC').trim();
40
- /**
41
- * The displacements a save leaves open on the fact it creates: `write` is that save (its event, writer and value) and
42
- * `closed` the fact it replaces, null when it replaces none.
43
- */
44
- function displacementsAfter(db, closed, write) {
45
- if (closed === null)
46
- return [];
47
- const before = writerOf(db, closed.eventId, closed.source);
48
- if (before.writer === write.writer)
49
- return before.displaced;
50
- const rest = before.displaced.filter(d => d.writer !== write.writer);
51
- const confirms = before.displaced.some(d => d.writer === write.writer && d.byEvent === closed.eventId);
52
- if (confirms || sameValue(closed.object, write.object))
53
- return rest;
54
- return [...rest, { writer: before.writer, object: closed.object, validFrom: closed.validFrom, byEvent: write.eventId }];
55
- }
56
- /** Whether `writer` has a displacement open on the fact `eventId`, which its next save of the subject and predicate confirms. */
57
- function hasOpenDisplacementBy(db, eventId, writer) {
58
- return writerOf(db, eventId, null).displaced.some(d => d.writer === writer);
59
- }
60
- /**
61
- * One line a read shows beside a current value that replaced another writer's value: the values it displaced, each with
62
- * its writer and date, and what clears them. Plain words, so an agent reads it as it reads the rest of the reply.
63
- */
64
- function writerConflictLine(open) {
65
- const values = open.map(d => `${JSON.stringify(d.object)} by ${d.writer}, ${d.validFrom.slice(0, 10)}`).join('; ');
66
- return `CONFLICT: ${values}. Replaced by a different writer; it stays a conflict until that writer saves this subject and predicate again.`;
67
- }
68
- /**
69
- * The conflict line of each current fact (by event id) that has displacements open, read in one query. A fact with none
70
- * has no entry, so a reply about it is what it was before.
71
- */
72
- function writerConflictLines(db, eventIds) {
73
- const ids = [...new Set(eventIds.filter((id) => typeof id === 'string'))];
74
- const lines = new Map();
75
- if (ids.length === 0)
76
- return lines;
77
- const rows = (0, statements_1.prepareOnce)(db, 'SELECT event_id, displaced FROM fact_writers WHERE event_id IN (SELECT value FROM json_each(?))')
78
- .all(JSON.stringify(ids));
79
- for (const row of rows) {
80
- const open = JSON.parse(row.displaced);
81
- if (open.length > 0)
82
- lines.set(row.event_id, writerConflictLine(open));
83
- }
84
- return lines;
85
- }