renor-engine 1.2.0 → 1.4.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 (216) hide show
  1. package/README.md +34 -2
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/bin/renor-engine.cjs +38 -0
  4. package/dist/BUILD.json +6 -0
  5. package/dist/cli.js +1674 -516
  6. package/package.json +17 -80
  7. package/dist/.tsbuildinfo +0 -1
  8. package/dist/api/apiAuth.js +0 -43
  9. package/dist/api/server.js +0 -174
  10. package/dist/buildIdentity.js +0 -149
  11. package/dist/callPhases.js +0 -82
  12. package/dist/cli-extractors.js +0 -46
  13. package/dist/config.js +0 -65
  14. package/dist/context/composer.js +0 -320
  15. package/dist/context/readHonesty.js +0 -478
  16. package/dist/context/rowStore.js +0 -238
  17. package/dist/conversation/formatter.js +0 -81
  18. package/dist/conversation/parser.js +0 -193
  19. package/dist/conversation/types.js +0 -2
  20. package/dist/conversation/writer.js +0 -42
  21. package/dist/db/client.js +0 -149
  22. package/dist/db/handoffTable.js +0 -31
  23. package/dist/db/indexSalvage.js +0 -101
  24. package/dist/db/migrations.js +0 -670
  25. package/dist/db/readCache.js +0 -109
  26. package/dist/db/schema.js +0 -5
  27. package/dist/db/sqlite.js +0 -396
  28. package/dist/db/statements.js +0 -89
  29. package/dist/digests/digestCache.js +0 -146
  30. package/dist/digests/digests.js +0 -597
  31. package/dist/digests/worker.js +0 -350
  32. package/dist/embeddings/caching.js +0 -337
  33. package/dist/embeddings/coalescing.js +0 -177
  34. package/dist/embeddings/interactive.js +0 -103
  35. package/dist/embeddings/local.js +0 -70
  36. package/dist/embeddings/openai.js +0 -106
  37. package/dist/embeddings/rebuild.js +0 -166
  38. package/dist/embeddings/simdScan.js +0 -260
  39. package/dist/embeddings/space.js +0 -21
  40. package/dist/embeddings/types.js +0 -14
  41. package/dist/embeddings/vectorIndex.js +0 -674
  42. package/dist/embeddings/vertex.js +0 -334
  43. package/dist/eval/artifacts.js +0 -42
  44. package/dist/eval/cacheReport.js +0 -146
  45. package/dist/eval/comparison.js +0 -182
  46. package/dist/eval/context700-candidate.js +0 -145
  47. package/dist/eval/evalCache.js +0 -568
  48. package/dist/eval/hashEmbedding.js +0 -35
  49. package/dist/eval/indexSnapshots.js +0 -549
  50. package/dist/eval/longmemeval.js +0 -56
  51. package/dist/eval/metrics.js +0 -181
  52. package/dist/eval/pageDump.js +0 -67
  53. package/dist/eval/renorbenchBaseline.js +0 -486
  54. package/dist/eval/renorbenchDocuments.js +0 -134
  55. package/dist/eval/renorbenchProduct.js +0 -182
  56. package/dist/eval/runLedger.js +0 -279
  57. package/dist/eval/runManifest.js +0 -485
  58. package/dist/eval/scoring.js +0 -197
  59. package/dist/eval/textTokens.js +0 -17
  60. package/dist/evidence/attach.js +0 -264
  61. package/dist/evidence/background.js +0 -147
  62. package/dist/evidence/evidenceCache.js +0 -182
  63. package/dist/evidence/gate.js +0 -37
  64. package/dist/evidence/keyPool.js +0 -85
  65. package/dist/evidence/ollamaChat.js +0 -84
  66. package/dist/evidence/optin.js +0 -165
  67. package/dist/evidence/options.js +0 -151
  68. package/dist/evidence/secondPass.js +0 -294
  69. package/dist/extraction/classifyCapture.js +0 -34
  70. package/dist/extraction/decision-model.js +0 -174
  71. package/dist/extraction/eventExtractor.js +0 -113
  72. package/dist/extraction/extractionClient.js +0 -122
  73. package/dist/extraction/factExtractor.js +0 -46
  74. package/dist/extraction/factsWritePath.js +0 -179
  75. package/dist/extraction/jevDecisionModel.js +0 -307
  76. package/dist/extraction/preferenceExtractor.js +0 -150
  77. package/dist/extraction/secretGuard.js +0 -180
  78. package/dist/extraction/secretScanner.js +0 -665
  79. package/dist/extraction/typedFacts.js +0 -224
  80. package/dist/extraction/writePathAdjudication.js +0 -110
  81. package/dist/importing/apply.js +0 -672
  82. package/dist/importing/bundle-write.js +0 -80
  83. package/dist/importing/cli.js +0 -408
  84. package/dist/importing/existing.js +0 -51
  85. package/dist/importing/facts.js +0 -121
  86. package/dist/importing/forgetImported.js +0 -79
  87. package/dist/importing/hosted.js +0 -467
  88. package/dist/importing/journalLog.js +0 -96
  89. package/dist/importing/markdown.js +0 -98
  90. package/dist/importing/plan.js +0 -201
  91. package/dist/importing/secretScreen.js +0 -113
  92. package/dist/importing/serialize.js +0 -188
  93. package/dist/importing/source.js +0 -463
  94. package/dist/importing/types.js +0 -6
  95. package/dist/indexer/chunker.js +0 -319
  96. package/dist/indexer/dailyEntries.js +0 -63
  97. package/dist/indexer/hasher.js +0 -10
  98. package/dist/indexer/laneRuns.js +0 -48
  99. package/dist/indexer/o200k.js +0 -481
  100. package/dist/indexer/outsideRoot.js +0 -77
  101. package/dist/indexer/parser.js +0 -170
  102. package/dist/indexer/pipeline.js +0 -988
  103. package/dist/indexer/staleness.js +0 -201
  104. package/dist/indexer/tokenizer.js +0 -77
  105. package/dist/indexer/walker.js +0 -170
  106. package/dist/learning/cli.js +0 -113
  107. package/dist/learning/schema.js +0 -144
  108. package/dist/learning/settings.js +0 -45
  109. package/dist/learning/store.js +0 -267
  110. package/dist/learning/tap.js +0 -401
  111. package/dist/mcp/hive.js +0 -88
  112. package/dist/mcp/requestErrors.js +0 -47
  113. package/dist/mcp/server.js +0 -426
  114. package/dist/memory/agentLease.js +0 -43
  115. package/dist/memory/confinedWrite.js +0 -315
  116. package/dist/memory/controls.js +0 -73
  117. package/dist/memory/dailyNote.js +0 -265
  118. package/dist/memory/dreamer/adjudicate.js +0 -215
  119. package/dist/memory/dreamer/apply.js +0 -120
  120. package/dist/memory/dreamer/budget.js +0 -64
  121. package/dist/memory/dreamer/candidates.js +0 -149
  122. package/dist/memory/dreamer/collection.js +0 -104
  123. package/dist/memory/dreamer/dreamer.js +0 -352
  124. package/dist/memory/dreamer/review.js +0 -111
  125. package/dist/memory/durableAppend.js +0 -57
  126. package/dist/memory/factConflicts.js +0 -205
  127. package/dist/memory/factTerms.js +0 -321
  128. package/dist/memory/handoff.js +0 -363
  129. package/dist/memory/hiveNameHeader.cjs +0 -10
  130. package/dist/memory/inventory.js +0 -55
  131. package/dist/memory/invisibleText.js +0 -20
  132. package/dist/memory/lifecycle.js +0 -2014
  133. package/dist/memory/limits.js +0 -13
  134. package/dist/memory/requests.js +0 -183
  135. package/dist/memory/sourceName.js +0 -276
  136. package/dist/memory/statusFacts.js +0 -61
  137. package/dist/memory/whatsNew.js +0 -158
  138. package/dist/memory/writer.js +0 -176
  139. package/dist/memory/writerConflicts.js +0 -85
  140. package/dist/memorymap/memoryMap.js +0 -1594
  141. package/dist/memorymap/replacedJournal.js +0 -58
  142. package/dist/memorymap/replacements.js +0 -410
  143. package/dist/mvp/activity.js +0 -86
  144. package/dist/mvp/agentMemory.js +0 -1803
  145. package/dist/mvp/answerCheck.js +0 -211
  146. package/dist/mvp/auth.js +0 -531
  147. package/dist/mvp/cli.js +0 -86
  148. package/dist/mvp/client-store.js +0 -462
  149. package/dist/mvp/compileCache.js +0 -242
  150. package/dist/mvp/compileSeed.js +0 -33
  151. package/dist/mvp/config.js +0 -292
  152. package/dist/mvp/connectionStatus.js +0 -80
  153. package/dist/mvp/contextDistinct.js +0 -53
  154. package/dist/mvp/contextProfile.js +0 -87
  155. package/dist/mvp/contextRelevance.js +0 -413
  156. package/dist/mvp/directorySurface.js +0 -68
  157. package/dist/mvp/engine.js +0 -1078
  158. package/dist/mvp/exportAll.js +0 -393
  159. package/dist/mvp/factLanePack.js +0 -77
  160. package/dist/mvp/factMarkers.js +0 -65
  161. package/dist/mvp/factsLane.js +0 -203
  162. package/dist/mvp/hiddenSpans.js +0 -585
  163. package/dist/mvp/history.js +0 -414
  164. package/dist/mvp/hitDates.js +0 -31
  165. package/dist/mvp/hitFloor.js +0 -36
  166. package/dist/mvp/identity.js +0 -18
  167. package/dist/mvp/idle.js +0 -113
  168. package/dist/mvp/log.js +0 -118
  169. package/dist/mvp/memoryDir.js +0 -98
  170. package/dist/mvp/posthogMcp.js +0 -210
  171. package/dist/mvp/readScope.js +0 -104
  172. package/dist/mvp/readUsefulness.js +0 -222
  173. package/dist/mvp/sanitize.js +0 -48
  174. package/dist/mvp/schemaRejections.js +0 -91
  175. package/dist/mvp/scope.js +0 -127
  176. package/dist/mvp/server.js +0 -2712
  177. package/dist/mvp/serverCard.js +0 -38
  178. package/dist/mvp/storedSize.js +0 -139
  179. package/dist/mvp/telemetry.js +0 -474
  180. package/dist/net/poolFetch.js +0 -174
  181. package/dist/recall/actionRecall.js +0 -282
  182. package/dist/recall/autoRecall.js +0 -447
  183. package/dist/recall/searchRecall.js +0 -189
  184. package/dist/retrieval/adviceLines.js +0 -195
  185. package/dist/retrieval/adviceQuestion.js +0 -44
  186. package/dist/retrieval/conversationShape.js +0 -44
  187. package/dist/retrieval/dailyEntryTime.js +0 -55
  188. package/dist/retrieval/entityLookup.js +0 -162
  189. package/dist/retrieval/factsFirst.js +0 -188
  190. package/dist/retrieval/keyword.js +0 -84
  191. package/dist/retrieval/ledgerEventTime.js +0 -51
  192. package/dist/retrieval/ledgerLiveness.js +0 -83
  193. package/dist/retrieval/ledgerPresentation.js +0 -161
  194. package/dist/retrieval/noteIdentity.js +0 -57
  195. package/dist/retrieval/orchestrator.js +0 -1224
  196. package/dist/retrieval/plainWording.js +0 -236
  197. package/dist/retrieval/queryHelpers.js +0 -142
  198. package/dist/retrieval/questionSpan.js +0 -475
  199. package/dist/retrieval/rankingTrace.js +0 -184
  200. package/dist/retrieval/readerEvents.js +0 -690
  201. package/dist/retrieval/readerLayout.js +0 -422
  202. package/dist/retrieval/relativeTimeHints.js +0 -113
  203. package/dist/retrieval/semantic.js +0 -67
  204. package/dist/router/routerCache.js +0 -106
  205. package/dist/router/sessionRouter.js +0 -726
  206. package/dist/sharing/cli.js +0 -181
  207. package/dist/sharing/examples.js +0 -136
  208. package/dist/sharing/fields.js +0 -225
  209. package/dist/sharing/loop.js +0 -80
  210. package/dist/sharing/project.js +0 -327
  211. package/dist/sharing/send.js +0 -466
  212. package/dist/sharing/state.js +0 -222
  213. package/dist/sharing/wording.js +0 -48
  214. package/dist/toolRefusal.js +0 -120
  215. package/dist/watcher/watcher.js +0 -77
  216. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
@@ -1,462 +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.ClientRegistry = exports.ClientRegistryError = exports.PENDING_CLIENT_APPROVAL_TTL_MS = exports.MAX_PERSISTED_CLIENTS = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const path_1 = __importDefault(require("path"));
9
- const crypto_1 = __importDefault(require("crypto"));
10
- const sourceName_1 = require("../memory/sourceName");
11
- /**
12
- * Persistent OAuth client registry (CLIENT-PERSISTENCE fix).
13
- *
14
- * The MVP previously kept the DCR client registry in memory only. ChatGPT
15
- * runs dynamic client registration ONCE per connection and caches the
16
- * returned client_id, so a service restart wiped the registry and every
17
- * later authorization failed with invalid_client — the owner could never
18
- * re-authorize without the client re-registering. This store persists ONLY
19
- * registered OAuth client metadata (client_id, client_secret, redirect
20
- * URIs, grant types) across restarts. Authorization tickets and codes are
21
- * deliberately NOT persisted: they stay short-lived and single-use. Access
22
- * and refresh tokens also stay ephemeral for this bounded fix — after a
23
- * restart the owner must re-run the login once; the durable client
24
- * identity is what makes that re-authorization possible.
25
- *
26
- * Hard requirements implemented here:
27
- * - explicit private path, validated OUTSIDE the memory/history trees at
28
- * config time; the store directory ("root") must be 0700/no group or
29
- * other bits, owned by the running uid, a real directory rather than a
30
- * symlink, and reached through no symlink except a root-owned one that
31
- * group and other cannot write (macOS's /var -> private/var), and then
32
- * only on a path where every directory belongs to root or the running
33
- * uid and group and other cannot write it;
34
- * - store file must be a regular, non-symlink, owner-only (0600) file;
35
- * - versioned, strictly validated schema — corrupt, unreadable, or
36
- * schema-violating existing state FAILS CLOSED (startup error), never
37
- * a silent reset to empty;
38
- * - bounded registry (MAX_REGISTERED_CLIENTS);
39
- * - atomic durable writes: temp file 0600 -> fsync -> rename -> dir fsync;
40
- * registration succeeds only AFTER the record is durably on disk — an
41
- * I/O failure throws and leaves NO in-memory-only acknowledged client;
42
- * - single-process serialization: all mutations are synchronous FS
43
- * operations inside one Node event loop, so concurrent registrations
44
- * cannot interleave reads-modify-writes;
45
- * - nothing here ever logs client secrets or full client IDs.
46
- */
47
- const STORE_SCHEMA = 'mvp-oauth-clients/1';
48
- /** Client store bound (AUTH-H2); a store violating it is corrupt, not trimmed. */
49
- exports.MAX_PERSISTED_CLIENTS = 100;
50
- /** Unapproved dynamic registrations are discarded after this owner-approval window. */
51
- exports.PENDING_CLIENT_APPROVAL_TTL_MS = 10 * 60_000;
52
- /** Accesses trigger expiry cleanup, throttled to avoid rewriting the store more than once a minute. */
53
- const EXPIRED_CLIENT_SWEEP_INTERVAL_MS = 60_000;
54
- /** Operator-facing failure. Messages carry no secrets. */
55
- class ClientRegistryError extends Error {
56
- constructor(message) {
57
- super(message);
58
- this.name = 'ClientRegistryError';
59
- }
60
- }
61
- exports.ClientRegistryError = ClientRegistryError;
62
- function failClosed(reason) {
63
- throw new ClientRegistryError(`OAuth client store is not usable: ${reason}. `
64
- + 'Refusing to start with a missing, corrupted, or unsafe client registry '
65
- + '(fail closed). Fix or remove the store file only after backing it up, '
66
- + 'then let clients register again; removed clients must be re-created by their owners.');
67
- }
68
- /** Running uid; the POSIX-only store refuses to run without one. */
69
- function currentUid() {
70
- const uid = process.getuid?.();
71
- if (uid === undefined)
72
- failClosed('process uid is unavailable on this platform');
73
- return uid;
74
- }
75
- const ROOT_UID = 0;
76
- const GROUP_OR_OTHER_WRITE = 0o022;
77
- const STICKY = 0o1000;
78
- /** Linux follows at most 40 symlinks in one path lookup, macOS 32. */
79
- const MAX_PATH_SYMLINKS = 40;
80
- const SYMLINK_RULE = 'store directory must not contain symlinks, except root-owned ones that group and other cannot write, '
81
- + 'on a path that only root and the service user can change';
82
- /** Why the symlink at `here`, which `found` describes, may not be followed, or null when it may. */
83
- function linkRefusal(here, found) {
84
- if (found.uid !== ROOT_UID)
85
- return 'a symlink on its path is not owned by root';
86
- if (found.mode & GROUP_OR_OTHER_WRITE)
87
- return 'a symlink on its path is writable by group or other';
88
- const leadsTo = fs_1.default.lstatSync(fs_1.default.realpathSync(here));
89
- if (leadsTo.uid !== ROOT_UID)
90
- return 'a symlink on its path leads to a directory that is not owned by root';
91
- if (leadsTo.mode & GROUP_OR_OTHER_WRITE)
92
- return 'a symlink on its path leads to a directory that is writable by group or other';
93
- return null;
94
- }
95
- /**
96
- * Why `dir`, which the path searches for the entry `found`, may not be on it, or null when it may. Whoever can write a
97
- * directory can rename or replace the entries in it, so it is root's or the service user's and closed to group and
98
- * other. A directory they can write is accepted only when it is sticky and root's, for an entry root owns, because
99
- * then only root may rename or remove that entry (/tmp is drwxrwxrwt).
100
- */
101
- function directoryRefusal(dir, found) {
102
- if (dir.uid !== ROOT_UID && dir.uid !== currentUid())
103
- return 'a directory on its path is not owned by root or the service user';
104
- if ((dir.mode & GROUP_OR_OTHER_WRITE) === 0)
105
- return null;
106
- if ((dir.mode & STICKY) !== 0 && dir.uid === ROOT_UID && found.uid === ROOT_UID)
107
- return null;
108
- return 'a directory on its path is writable by group or other';
109
- }
110
- /**
111
- * Why the path to `dir` may not be used, or null when it may. These messages reach the /register response, so none
112
- * names a path.
113
- *
114
- * A path without a symlink is judged as it always was: by the store directory itself, in validateStoreRoot. A symlink on
115
- * the path is refused unless root owns it and group and other cannot write it, and the same holds for the directory it
116
- * leads to: macOS reaches its per-user temp directory through /var, a symlink to private/var. Linux reports every link
117
- * as lrwxrwxrwx, so there this still refuses all of them.
118
- *
119
- * A link is only as safe as the directory it sits in, and its target only as safe as every directory on the way to it:
120
- * whoever can write one of those can rename or replace what was just checked, and a path that was refused outright
121
- * before would be accepted. So the walk follows the path as the kernel does, one component at a time, and judges each
122
- * link and every directory a name is looked up in, those a link leads through included (/private is in no path an
123
- * operator writes, only in the target of /var). What nobody else can change nobody else can swap between this check and
124
- * the write that follows, which is why the write needs no descriptor of the directory (Node has no openat). Every
125
- * directory above the store is walked, and the walk must end where realpath does, or the path is not trusted.
126
- *
127
- * Owner and mode are all this sees. A bind mount looks like the directory it mounts and is judged as that directory,
128
- * but lstat and realpath cannot tell whether a mount sits on the path. Mounting over a directory takes root or owning
129
- * it, and every directory accepted here belongs to root or the service user, so the mount layout is the operator's to
130
- * protect, as root's own links are.
131
- */
132
- function symlinkRefusal(dir) {
133
- const real = fs_1.default.realpathSync(dir);
134
- if (real === path_1.default.resolve(dir))
135
- return null;
136
- // At least one symlink is on the way. Follow the path as the kernel does, one component at a time.
137
- let at = path_1.default.sep;
138
- const absolute = path_1.default.isAbsolute(dir) ? dir : `${process.cwd()}${path_1.default.sep}${dir}`;
139
- let rest = absolute.split(path_1.default.sep);
140
- let links = 0;
141
- while (rest.length > 0) {
142
- const name = rest.shift();
143
- if (name === '' || name === '.')
144
- continue;
145
- if (name === '..') {
146
- at = path_1.default.dirname(at);
147
- continue;
148
- }
149
- const here = path_1.default.join(at, name);
150
- const found = fs_1.default.lstatSync(here);
151
- const isLink = found.isSymbolicLink();
152
- if (isLink && ++links > MAX_PATH_SYMLINKS)
153
- return `${SYMLINK_RULE}; its path crosses too many symlinks`;
154
- const unsafe = (isLink ? linkRefusal(here, found) : null) ?? directoryRefusal(fs_1.default.lstatSync(at), found);
155
- if (unsafe !== null)
156
- return `${SYMLINK_RULE}; ${unsafe}`;
157
- if (!isLink) {
158
- at = here;
159
- continue;
160
- }
161
- const target = fs_1.default.readlinkSync(here);
162
- if (path_1.default.isAbsolute(target))
163
- at = path_1.default.sep;
164
- rest = [...target.split(path_1.default.sep), ...rest];
165
- }
166
- // The walk judged what it went through and the kernel says where the path really goes. If the two differ, the walk did
167
- // not see what the kernel saw (a link changed under it, or a case it does not model), and that is not a path to trust.
168
- return at === real ? null : 'store directory could not be resolved consistently';
169
- }
170
- function validateStoreRoot(dir) {
171
- let dirStat;
172
- try {
173
- dirStat = fs_1.default.lstatSync(dir);
174
- }
175
- catch (err) {
176
- failClosed(`store directory is missing or unreadable (${err.code}); `
177
- + 'the operator must create it as an owner-only (0700) directory before startup');
178
- }
179
- if (!dirStat.isDirectory())
180
- failClosed('store path parent is not a directory');
181
- if (dirStat.uid !== currentUid())
182
- failClosed('store directory is not owned by the service user');
183
- if (dirStat.mode & 0o077)
184
- failClosed('store directory must be owner-only (0700)');
185
- // The directory itself is a real, owner-only directory of the service user. What is left is the path to it. The
186
- // refusal is judged outside the try, so its reason cannot be replaced by the one for a failed lookup.
187
- let refusal;
188
- try {
189
- refusal = symlinkRefusal(dir);
190
- }
191
- catch (err) {
192
- failClosed(`store directory could not be resolved (${err.code ?? 'io-error'})`);
193
- }
194
- if (refusal !== null)
195
- failClosed(refusal);
196
- }
197
- function validateStoreFile(filePath) {
198
- const stat = fs_1.default.lstatSync(filePath); // throws if missing (handled by caller)
199
- if (!stat.isFile())
200
- failClosed('store path is not a regular file (symlinks are rejected)');
201
- if (stat.uid !== currentUid())
202
- failClosed('store file is not owned by the service user');
203
- if (stat.mode & 0o077)
204
- failClosed('store file must be owner-only (0600)');
205
- }
206
- /** Strict schema validation. Any deviation fails closed. */
207
- function parseSnapshot(raw) {
208
- let parsed;
209
- try {
210
- parsed = JSON.parse(raw);
211
- }
212
- catch {
213
- failClosed('store file is not valid JSON');
214
- }
215
- if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
216
- failClosed('store file root must be an object');
217
- }
218
- const top = parsed;
219
- const knownKeys = new Set(['schema', 'clients']);
220
- for (const key of Object.keys(top)) {
221
- if (!knownKeys.has(key))
222
- failClosed(`unknown top-level field '${key}'`);
223
- }
224
- if (top.schema !== STORE_SCHEMA)
225
- failClosed(`unsupported schema version (expected ${STORE_SCHEMA})`);
226
- if (!Array.isArray(top.clients))
227
- failClosed('clients must be an array');
228
- if (top.clients.length > exports.MAX_PERSISTED_CLIENTS)
229
- failClosed('store exceeds the bounded client limit');
230
- const clients = new Map();
231
- for (const entry of top.clients) {
232
- if (typeof entry !== 'object' || entry === null || Array.isArray(entry))
233
- failClosed('client entry must be an object');
234
- const c = entry;
235
- const allowed = new Set(['client_id', 'client_secret', 'client_secret_expires_at', 'client_name', 'redirect_uris', 'grant_types',
236
- 'token_endpoint_auth_method', 'client_id_issued_at', 'scope', 'client_uri', 'logo_uri', 'contacts', 'response_types', 'pending_approval']);
237
- for (const key of Object.keys(c)) {
238
- if (!allowed.has(key))
239
- failClosed(`client entry has unknown field '${key}'`);
240
- }
241
- if (typeof c.client_id !== 'string' || c.client_id.length === 0)
242
- failClosed('client_id must be a non-empty string');
243
- if (clients.has(c.client_id))
244
- failClosed(`duplicate client_id in store`);
245
- if (!Array.isArray(c.redirect_uris) || c.redirect_uris.length === 0
246
- || c.redirect_uris.some(u => typeof u !== 'string' || u.length === 0)) {
247
- failClosed('redirect_uris must be a non-empty array of strings');
248
- }
249
- if (c.client_secret !== undefined && typeof c.client_secret !== 'string')
250
- failClosed('client_secret must be a string when present');
251
- if (c.client_name !== undefined && typeof c.client_name !== 'string')
252
- failClosed('client_name must be a string when present');
253
- if (c.grant_types !== undefined && (!Array.isArray(c.grant_types) || c.grant_types.some(g => typeof g !== 'string'))) {
254
- failClosed('grant_types must be an array of strings when present');
255
- }
256
- if (c.token_endpoint_auth_method !== undefined && typeof c.token_endpoint_auth_method !== 'string') {
257
- failClosed('token_endpoint_auth_method must be a string when present');
258
- }
259
- if (c.client_secret_expires_at !== undefined
260
- && (typeof c.client_secret_expires_at !== 'number' || !Number.isFinite(c.client_secret_expires_at))) {
261
- failClosed('client_secret_expires_at must be a finite number when present');
262
- }
263
- // response_types: the SDK's DCR handler includes it for live clients
264
- // (observed in the live registry written by the ChatGPT connector).
265
- if (c.response_types !== undefined
266
- && (!Array.isArray(c.response_types) || c.response_types.some(r => typeof r !== 'string'))) {
267
- failClosed('response_types must be an array of strings when present');
268
- }
269
- if (typeof c.client_id_issued_at !== 'number' || !Number.isFinite(c.client_id_issued_at)) {
270
- failClosed('client_id_issued_at must be a finite number');
271
- }
272
- if (c.pending_approval !== undefined && typeof c.pending_approval !== 'boolean') {
273
- failClosed('pending_approval must be a boolean when present');
274
- }
275
- clients.set(c.client_id, entry);
276
- }
277
- return clients;
278
- }
279
- class ClientRegistry {
280
- filePath;
281
- clients = new Map();
282
- lastExpiredClientSweepAt = Number.NEGATIVE_INFINITY;
283
- constructor(filePath) {
284
- this.filePath = filePath;
285
- validateStoreRoot(path_1.default.dirname(filePath));
286
- let stat = null;
287
- try {
288
- stat = fs_1.default.lstatSync(filePath);
289
- }
290
- catch (err) {
291
- if (err.code === 'ENOENT') {
292
- // No store yet: a fresh deployment starts with zero clients. The
293
- // first registration creates the file; an EXISTING store that has
294
- // become unreadable/corrupt never takes this path (it fails below).
295
- return;
296
- }
297
- failClosed(`store file could not be inspected (${err.code})`);
298
- }
299
- if (stat === null)
300
- failClosed('store file could not be inspected');
301
- validateStoreFile(filePath);
302
- let raw;
303
- try {
304
- raw = fs_1.default.readFileSync(filePath, 'utf-8');
305
- }
306
- catch (err) {
307
- failClosed(`store file could not be read (${err.code})`);
308
- }
309
- this.clients = parseSnapshot(raw);
310
- }
311
- get size() {
312
- this.sweepExpiredPendingClients();
313
- return this.clients.size;
314
- }
315
- get(clientId) {
316
- this.sweepExpiredPendingClients();
317
- return this.clients.get(clientId);
318
- }
319
- /** Whether the client is registered but not yet approved by the owner. Stores from before approval existed have none. */
320
- isPending(clientId) {
321
- this.sweepExpiredPendingClients();
322
- return this.clients.get(clientId)?.pending_approval === true;
323
- }
324
- /**
325
- * True when an approved client other than `exceptClientId` already holds the hive name `identity`. A registration
326
- * that has not been approved holds no name: anyone can register one, so only the owner's approval claims a name.
327
- */
328
- holdsApprovedHiveIdentity(identity, exceptClientId) {
329
- this.sweepExpiredPendingClients();
330
- for (const client of this.clients.values()) {
331
- if (client.client_id !== exceptClientId && client.pending_approval !== true
332
- && (0, sourceName_1.hiveIdentity)(client.client_name) === identity)
333
- return true;
334
- }
335
- return false;
336
- }
337
- /**
338
- * The hive name of this client. Two approved spellings of one header form are one agent: the earliest
339
- * `client_id_issued_at`, then `client_id`, keeps the name, and the others resolve to no name. Nothing is deleted.
340
- * Unapproved registrations are not holders, so one cannot win the name by registering first.
341
- */
342
- hiveConnectionName(clientId) {
343
- this.sweepExpiredPendingClients();
344
- const client = this.clients.get(clientId);
345
- if (!client)
346
- return undefined;
347
- const identity = (0, sourceName_1.hiveIdentity)(client.client_name);
348
- if (!identity)
349
- return typeof client.client_name === 'string' ? client.client_name.trim() : undefined;
350
- const holders = [...this.clients.values()].filter(other => other.pending_approval !== true && (0, sourceName_1.hiveIdentity)(other.client_name) === identity);
351
- holders.sort((a, b) => {
352
- const issued = (a.client_id_issued_at ?? 0) - (b.client_id_issued_at ?? 0);
353
- if (issued !== 0)
354
- return issued;
355
- return a.client_id < b.client_id ? -1 : a.client_id > b.client_id ? 1 : 0;
356
- });
357
- return holders[0]?.client_id === clientId ? identity : '';
358
- }
359
- /**
360
- * Disk-first registration: persists durably BEFORE the record becomes
361
- * visible. Any I/O failure throws and leaves the registry (and disk)
362
- * unchanged — a client is never acknowledged in memory only.
363
- * The record is pending until approve(): it claims no hive name yet.
364
- */
365
- add(client) {
366
- this.sweepExpiredPendingClients();
367
- if (this.clients.size >= exports.MAX_PERSISTED_CLIENTS) {
368
- throw new ClientRegistryError(`client store bound reached (${exports.MAX_PERSISTED_CLIENTS} clients)`);
369
- }
370
- if (this.clients.has(client.client_id)) {
371
- throw new ClientRegistryError('client_id already exists');
372
- }
373
- validateStoreRoot(path_1.default.dirname(this.filePath));
374
- const next = new Map(this.clients);
375
- next.set(client.client_id, { ...client, pending_approval: true });
376
- this.writeSnapshot(next);
377
- this.clients = next;
378
- }
379
- /**
380
- * The owner approved this client: it becomes a holder of its hive name. Persisted before it takes effect, like add.
381
- */
382
- approve(clientId) {
383
- this.sweepExpiredPendingClients();
384
- const client = this.clients.get(clientId);
385
- if (!client)
386
- throw new ClientRegistryError('client is not registered');
387
- if (client.pending_approval !== true)
388
- return;
389
- validateStoreRoot(path_1.default.dirname(this.filePath));
390
- const next = new Map(this.clients);
391
- const { pending_approval: _pending, ...approved } = client;
392
- next.set(clientId, approved);
393
- this.writeSnapshot(next);
394
- this.clients = next;
395
- }
396
- sweepExpiredPendingClients() {
397
- const now = Date.now();
398
- if (now >= this.lastExpiredClientSweepAt
399
- && now - this.lastExpiredClientSweepAt < EXPIRED_CLIENT_SWEEP_INTERVAL_MS)
400
- return;
401
- this.lastExpiredClientSweepAt = now;
402
- const next = new Map(this.clients);
403
- for (const [clientId, client] of next) {
404
- if (client.pending_approval === true
405
- && (client.client_id_issued_at ?? 0) * 1000 <= now - exports.PENDING_CLIENT_APPROVAL_TTL_MS) {
406
- next.delete(clientId);
407
- }
408
- }
409
- if (next.size === this.clients.size)
410
- return;
411
- // Expiry only removes records nobody can use: a store that cannot be written right now must not break reads or
412
- // sign-in, so the expired records are dropped in memory and the next write (or restart) persists the removal.
413
- try {
414
- validateStoreRoot(path_1.default.dirname(this.filePath));
415
- this.writeSnapshot(next);
416
- }
417
- catch (err) {
418
- if (!(err instanceof ClientRegistryError))
419
- throw err;
420
- }
421
- this.clients = next;
422
- }
423
- writeSnapshot(clients) {
424
- const payload = JSON.stringify({
425
- schema: STORE_SCHEMA,
426
- clients: Array.from(clients.values()),
427
- });
428
- const dir = path_1.default.dirname(this.filePath);
429
- const tmp = path_1.default.join(dir, `.${path_1.default.basename(this.filePath)}.${process.pid}.${crypto_1.default.randomBytes(6).toString('hex')}.tmp`);
430
- let fd;
431
- try {
432
- fd = fs_1.default.openSync(tmp, fs_1.default.constants.O_WRONLY | fs_1.default.constants.O_CREAT | fs_1.default.constants.O_EXCL, 0o600);
433
- fs_1.default.writeFileSync(fd, payload, 'utf-8');
434
- fs_1.default.fsyncSync(fd);
435
- fs_1.default.closeSync(fd);
436
- fd = undefined;
437
- fs_1.default.renameSync(tmp, this.filePath);
438
- // durable directory entry for the renamed store file
439
- const dirFd = fs_1.default.openSync(dir, fs_1.default.constants.O_RDONLY);
440
- try {
441
- fs_1.default.fsyncSync(dirFd);
442
- }
443
- finally {
444
- fs_1.default.closeSync(dirFd);
445
- }
446
- }
447
- catch (err) {
448
- if (fd !== undefined) {
449
- try {
450
- fs_1.default.closeSync(fd);
451
- }
452
- catch { /* best effort */ }
453
- }
454
- try {
455
- fs_1.default.unlinkSync(tmp);
456
- }
457
- catch { /* best effort */ }
458
- throw new ClientRegistryError(`client registration could not be persisted (${err.code ?? 'io-error'}); no client was registered`);
459
- }
460
- }
461
- }
462
- exports.ClientRegistry = ClientRegistry;