@getengram/cli 0.6.0 → 0.7.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 (208) hide show
  1. package/LICENSE +112 -0
  2. package/README.md +1 -1
  3. package/dist/commands/auth.d.ts.map +1 -1
  4. package/dist/commands/auth.js +38 -11
  5. package/dist/commands/auth.js.map +1 -1
  6. package/dist/commands/conversations.d.ts.map +1 -1
  7. package/dist/commands/conversations.js +8 -2
  8. package/dist/commands/conversations.js.map +1 -1
  9. package/dist/commands/exclude.d.ts.map +1 -1
  10. package/dist/commands/exclude.js +73 -21
  11. package/dist/commands/exclude.js.map +1 -1
  12. package/dist/commands/import.d.ts +30 -3
  13. package/dist/commands/import.d.ts.map +1 -1
  14. package/dist/commands/import.js +374 -45
  15. package/dist/commands/import.js.map +1 -1
  16. package/dist/commands/login.d.ts.map +1 -1
  17. package/dist/commands/login.js +144 -29
  18. package/dist/commands/login.js.map +1 -1
  19. package/dist/commands/search.js +2 -2
  20. package/dist/commands/search.js.map +1 -1
  21. package/dist/commands/status.d.ts +16 -0
  22. package/dist/commands/status.d.ts.map +1 -0
  23. package/dist/commands/status.js +51 -0
  24. package/dist/commands/status.js.map +1 -0
  25. package/dist/commands/store.d.ts.map +1 -1
  26. package/dist/commands/store.js +19 -3
  27. package/dist/commands/store.js.map +1 -1
  28. package/dist/commands/upgrade.d.ts +24 -0
  29. package/dist/commands/upgrade.d.ts.map +1 -1
  30. package/dist/commands/upgrade.js +64 -15
  31. package/dist/commands/upgrade.js.map +1 -1
  32. package/dist/commands/usage.d.ts +44 -0
  33. package/dist/commands/usage.d.ts.map +1 -1
  34. package/dist/commands/usage.js +79 -19
  35. package/dist/commands/usage.js.map +1 -1
  36. package/dist/commands/vault.d.ts +11 -2
  37. package/dist/commands/vault.d.ts.map +1 -1
  38. package/dist/commands/vault.js +372 -53
  39. package/dist/commands/vault.js.map +1 -1
  40. package/dist/config-file.d.ts +74 -0
  41. package/dist/config-file.d.ts.map +1 -0
  42. package/dist/config-file.js +152 -0
  43. package/dist/config-file.js.map +1 -0
  44. package/dist/config-guard.d.ts +96 -0
  45. package/dist/config-guard.d.ts.map +1 -0
  46. package/dist/config-guard.js +184 -0
  47. package/dist/config-guard.js.map +1 -0
  48. package/dist/config.d.ts +48 -4
  49. package/dist/config.d.ts.map +1 -1
  50. package/dist/config.js +101 -12
  51. package/dist/config.js.map +1 -1
  52. package/dist/daemon/as-root.d.ts +46 -0
  53. package/dist/daemon/as-root.d.ts.map +1 -0
  54. package/dist/daemon/as-root.js +64 -0
  55. package/dist/daemon/as-root.js.map +1 -0
  56. package/dist/daemon/backoff.d.ts +26 -0
  57. package/dist/daemon/backoff.d.ts.map +1 -0
  58. package/dist/daemon/backoff.js +37 -0
  59. package/dist/daemon/backoff.js.map +1 -0
  60. package/dist/daemon/codex-parser.d.ts +38 -4
  61. package/dist/daemon/codex-parser.d.ts.map +1 -1
  62. package/dist/daemon/codex-parser.js +111 -23
  63. package/dist/daemon/codex-parser.js.map +1 -1
  64. package/dist/daemon/command-path.d.ts +13 -0
  65. package/dist/daemon/command-path.d.ts.map +1 -0
  66. package/dist/daemon/command-path.js +35 -0
  67. package/dist/daemon/command-path.js.map +1 -0
  68. package/dist/daemon/commands.d.ts +153 -3
  69. package/dist/daemon/commands.d.ts.map +1 -1
  70. package/dist/daemon/commands.js +2775 -235
  71. package/dist/daemon/commands.js.map +1 -1
  72. package/dist/daemon/db.d.ts +255 -27
  73. package/dist/daemon/db.d.ts.map +1 -1
  74. package/dist/daemon/db.js +749 -53
  75. package/dist/daemon/db.js.map +1 -1
  76. package/dist/daemon/exclusions.d.ts +23 -3
  77. package/dist/daemon/exclusions.d.ts.map +1 -1
  78. package/dist/daemon/exclusions.js +91 -21
  79. package/dist/daemon/exclusions.js.map +1 -1
  80. package/dist/daemon/homebrew.d.ts +26 -0
  81. package/dist/daemon/homebrew.d.ts.map +1 -0
  82. package/dist/daemon/homebrew.js +43 -0
  83. package/dist/daemon/homebrew.js.map +1 -0
  84. package/dist/daemon/instance.d.ts +427 -0
  85. package/dist/daemon/instance.d.ts.map +1 -0
  86. package/dist/daemon/instance.js +1201 -0
  87. package/dist/daemon/instance.js.map +1 -0
  88. package/dist/daemon/key-source.d.ts +41 -0
  89. package/dist/daemon/key-source.d.ts.map +1 -0
  90. package/dist/daemon/key-source.js +89 -0
  91. package/dist/daemon/key-source.js.map +1 -0
  92. package/dist/daemon/launch.d.ts +56 -0
  93. package/dist/daemon/launch.d.ts.map +1 -0
  94. package/dist/daemon/launch.js +153 -0
  95. package/dist/daemon/launch.js.map +1 -0
  96. package/dist/daemon/launchd.d.ts +215 -0
  97. package/dist/daemon/launchd.d.ts.map +1 -0
  98. package/dist/daemon/launchd.js +527 -0
  99. package/dist/daemon/launchd.js.map +1 -0
  100. package/dist/daemon/login-start.d.ts +2 -0
  101. package/dist/daemon/login-start.d.ts.map +1 -0
  102. package/dist/daemon/login-start.js +25 -0
  103. package/dist/daemon/login-start.js.map +1 -0
  104. package/dist/daemon/ownership.d.ts +123 -0
  105. package/dist/daemon/ownership.d.ts.map +1 -0
  106. package/dist/daemon/ownership.js +245 -0
  107. package/dist/daemon/ownership.js.map +1 -0
  108. package/dist/daemon/package-runner.d.ts +28 -0
  109. package/dist/daemon/package-runner.d.ts.map +1 -0
  110. package/dist/daemon/package-runner.js +113 -0
  111. package/dist/daemon/package-runner.js.map +1 -0
  112. package/dist/daemon/parser.d.ts +50 -5
  113. package/dist/daemon/parser.d.ts.map +1 -1
  114. package/dist/daemon/parser.js +224 -47
  115. package/dist/daemon/parser.js.map +1 -1
  116. package/dist/daemon/powershell.d.ts +25 -0
  117. package/dist/daemon/powershell.d.ts.map +1 -0
  118. package/dist/daemon/powershell.js +30 -0
  119. package/dist/daemon/powershell.js.map +1 -0
  120. package/dist/daemon/preflight.d.ts +0 -17
  121. package/dist/daemon/preflight.d.ts.map +1 -1
  122. package/dist/daemon/preflight.js +32 -6
  123. package/dist/daemon/preflight.js.map +1 -1
  124. package/dist/daemon/prune.d.ts +101 -0
  125. package/dist/daemon/prune.d.ts.map +1 -0
  126. package/dist/daemon/prune.js +287 -0
  127. package/dist/daemon/prune.js.map +1 -0
  128. package/dist/daemon/queue-error.d.ts +47 -0
  129. package/dist/daemon/queue-error.d.ts.map +1 -0
  130. package/dist/daemon/queue-error.js +59 -0
  131. package/dist/daemon/queue-error.js.map +1 -0
  132. package/dist/daemon/root-daemon.d.ts +60 -0
  133. package/dist/daemon/root-daemon.d.ts.map +1 -0
  134. package/dist/daemon/root-daemon.js +86 -0
  135. package/dist/daemon/root-daemon.js.map +1 -0
  136. package/dist/daemon/scoop.d.ts +52 -0
  137. package/dist/daemon/scoop.d.ts.map +1 -0
  138. package/dist/daemon/scoop.js +84 -0
  139. package/dist/daemon/scoop.js.map +1 -0
  140. package/dist/daemon/scrubber.d.ts +456 -0
  141. package/dist/daemon/scrubber.d.ts.map +1 -0
  142. package/dist/daemon/scrubber.js +994 -0
  143. package/dist/daemon/scrubber.js.map +1 -0
  144. package/dist/daemon/status.d.ts +306 -11
  145. package/dist/daemon/status.d.ts.map +1 -1
  146. package/dist/daemon/status.js +568 -43
  147. package/dist/daemon/status.js.map +1 -1
  148. package/dist/daemon/supervise.d.ts +78 -0
  149. package/dist/daemon/supervise.d.ts.map +1 -0
  150. package/dist/daemon/supervise.js +121 -0
  151. package/dist/daemon/supervise.js.map +1 -0
  152. package/dist/daemon/supervisor.d.ts +18 -0
  153. package/dist/daemon/supervisor.d.ts.map +1 -0
  154. package/dist/daemon/supervisor.js +56 -0
  155. package/dist/daemon/supervisor.js.map +1 -0
  156. package/dist/daemon/syncer.d.ts +382 -21
  157. package/dist/daemon/syncer.d.ts.map +1 -1
  158. package/dist/daemon/syncer.js +1448 -178
  159. package/dist/daemon/syncer.js.map +1 -1
  160. package/dist/daemon/systemd.d.ts +109 -0
  161. package/dist/daemon/systemd.d.ts.map +1 -0
  162. package/dist/daemon/systemd.js +363 -0
  163. package/dist/daemon/systemd.js.map +1 -0
  164. package/dist/daemon/types.d.ts +58 -6
  165. package/dist/daemon/types.d.ts.map +1 -1
  166. package/dist/daemon/vault-changed.d.ts +29 -0
  167. package/dist/daemon/vault-changed.d.ts.map +1 -0
  168. package/dist/daemon/vault-changed.js +47 -0
  169. package/dist/daemon/vault-changed.js.map +1 -0
  170. package/dist/daemon/vault-key-missing.d.ts +70 -0
  171. package/dist/daemon/vault-key-missing.d.ts.map +1 -0
  172. package/dist/daemon/vault-key-missing.js +125 -0
  173. package/dist/daemon/vault-key-missing.js.map +1 -0
  174. package/dist/daemon/watcher.d.ts +87 -4
  175. package/dist/daemon/watcher.d.ts.map +1 -1
  176. package/dist/daemon/watcher.js +286 -36
  177. package/dist/daemon/watcher.js.map +1 -1
  178. package/dist/daemon/worker-main.js +363 -51
  179. package/dist/daemon/worker-main.js.map +1 -1
  180. package/dist/daemon/worker.js +19 -1
  181. package/dist/daemon/worker.js.map +1 -1
  182. package/dist/import-resume.d.ts +163 -0
  183. package/dist/import-resume.d.ts.map +1 -0
  184. package/dist/import-resume.js +267 -0
  185. package/dist/import-resume.js.map +1 -0
  186. package/dist/index.js +242 -33
  187. package/dist/index.js.map +1 -1
  188. package/dist/message-bounds.d.ts +52 -0
  189. package/dist/message-bounds.d.ts.map +1 -0
  190. package/dist/message-bounds.js +157 -0
  191. package/dist/message-bounds.js.map +1 -0
  192. package/dist/output.d.ts +14 -0
  193. package/dist/output.d.ts.map +1 -1
  194. package/dist/output.js +33 -5
  195. package/dist/output.js.map +1 -1
  196. package/dist/private-files.d.ts +126 -0
  197. package/dist/private-files.d.ts.map +1 -0
  198. package/dist/private-files.js +372 -0
  199. package/dist/private-files.js.map +1 -0
  200. package/dist/server-url.d.ts +31 -0
  201. package/dist/server-url.d.ts.map +1 -0
  202. package/dist/server-url.js +47 -0
  203. package/dist/server-url.js.map +1 -0
  204. package/dist/vault-key.d.ts +122 -0
  205. package/dist/vault-key.d.ts.map +1 -0
  206. package/dist/vault-key.js +273 -0
  207. package/dist/vault-key.js.map +1 -0
  208. package/package.json +11 -12
@@ -0,0 +1,372 @@
1
+ /**
2
+ * ~/.engram holds the API key, the vault key, and the daemon's queue of
3
+ * captured transcript text. On macOS and Linux it is the user's alone
4
+ * (engram#627): the directory is 0700 and the files in it 0600, as Claude
5
+ * Code keeps the transcripts themselves. It used to get the defaults, 0755
6
+ * and 0644 under the usual umask, so other accounts on the machine could
7
+ * read the daemon's copy of transcripts they couldn't read at the source.
8
+ *
9
+ * Windows is left alone: files under the user profile inherit its ACL,
10
+ * which is private already. Writes there are still atomic.
11
+ *
12
+ * IMPORTANT: Node built-ins only. launch.ts uses this module, and the
13
+ * Windows sign-in entry runs launch.ts before the daemon's native-module
14
+ * preflight (see launch.ts).
15
+ */
16
+ import { randomBytes } from "node:crypto";
17
+ import { accessSync, chmodSync, chownSync, closeSync, constants, fchownSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
18
+ import { basename, dirname, join } from "node:path";
19
+ export const PRIVATE_DIR_MODE = 0o700;
20
+ export const PRIVATE_FILE_MODE = 0o600;
21
+ /** The group and other permission bits, which private clears. */
22
+ const SHARED_BITS = 0o077;
23
+ /** `mode` with the group and other bits cleared, and the owner's kept. */
24
+ function unshared(mode) {
25
+ return mode & 0o7777 & ~SHARED_BITS;
26
+ }
27
+ function posix() {
28
+ return process.platform !== "win32";
29
+ }
30
+ function errCode(err) {
31
+ return err?.code;
32
+ }
33
+ /**
34
+ * Whether this process owns `st`. Only what the user owns is changed: root
35
+ * could change anyone's file, but another user's stays as it is.
36
+ */
37
+ function ours(st) {
38
+ return Number(st.uid) === process.geteuid?.();
39
+ }
40
+ /**
41
+ * Root writing into another user's ~/.engram (sudo keeps HOME on some
42
+ * systems): what it creates there goes to the owner of the directory it is
43
+ * created in. Otherwise the user's own CLI and daemon couldn't read a
44
+ * config.json that `sudo engram …` had written. An in-place write left the
45
+ * file's owner as it was; the rename that replaced it would not.
46
+ */
47
+ function ownerFor(dir) {
48
+ if (!posix() || process.geteuid?.() !== 0)
49
+ return null;
50
+ const st = statSync(dir);
51
+ return st.uid === 0 ? null : { uid: st.uid, gid: st.gid };
52
+ }
53
+ /**
54
+ * Make sure `dir` exists and is private: created 0700, or, when it is
55
+ * already there and the user's own, its group and other bits cleared, so an
56
+ * install from before engram#627 is covered too. Returns what was done.
57
+ *
58
+ * Only for a directory that is Engram's alone (~/.engram), never one that
59
+ * other things share.
60
+ */
61
+ export function ensurePrivateDirSync(dir) {
62
+ const created = mkdirSync(dir, { recursive: true, mode: PRIVATE_DIR_MODE }) !== undefined;
63
+ if (!posix())
64
+ return created ? "created" : "private";
65
+ if (created) {
66
+ const owner = ownerFor(dirname(dir));
67
+ if (owner)
68
+ chownSync(dir, owner.uid, owner.gid);
69
+ }
70
+ const st = statSync(dir);
71
+ if ((st.mode & SHARED_BITS) === 0)
72
+ return created ? "created" : "private";
73
+ if (!ours(st))
74
+ return "not-ours";
75
+ chmodSync(dir, unshared(st.mode));
76
+ return created ? "created" : "changed";
77
+ }
78
+ /**
79
+ * Clear the group and other bits of the file at `path` when it is the
80
+ * user's own. Best effort, and never throws: a mode that can't be changed
81
+ * (a filesystem without them) mustn't stop capture.
82
+ *
83
+ * Path-based on purpose. Opening and closing a SQLite database file here
84
+ * would drop the locks this process holds on it through SQLite.
85
+ */
86
+ export function restrictSync(path) {
87
+ if (!posix())
88
+ return "private";
89
+ try {
90
+ const st = statSync(path);
91
+ if ((st.mode & SHARED_BITS) === 0)
92
+ return "private";
93
+ if (!ours(st))
94
+ return "not-ours";
95
+ chmodSync(path, unshared(st.mode));
96
+ return "changed";
97
+ }
98
+ catch (err) {
99
+ return errCode(err) === "ENOENT" ? "missing" : "failed";
100
+ }
101
+ }
102
+ /**
103
+ * Create an empty file at `path`, 0600, unless something is there already.
104
+ * For daemon.db: SQLite gives its -wal and -shm files the database file's
105
+ * mode, and treats an empty file as an empty database.
106
+ */
107
+ export function createPrivateFileSync(path) {
108
+ try {
109
+ closeSync(openSync(path, "wx", PRIVATE_FILE_MODE));
110
+ }
111
+ catch (err) {
112
+ if (errCode(err) !== "EEXIST")
113
+ throw err;
114
+ }
115
+ }
116
+ /** `<name>.<pid>.<8 hex digits>.tmp`: one per write, so two writers never share one. */
117
+ function tempName(name) {
118
+ return `${name}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
119
+ }
120
+ /** A temp file of tempName's, and the name of the file it was for. */
121
+ const TEMP_FILE = /^(.+)\.\d+\.[0-9a-f]{8}\.tmp$/;
122
+ /**
123
+ * Waits before each retry of a rename Windows refused because another
124
+ * program has the old file open without sharing it (an antivirus scan, an
125
+ * editor). About 300 ms in all.
126
+ */
127
+ const WINDOWS_RENAME_RETRY_MS = [10, 20, 40, 80, 160];
128
+ function sleepSync(ms) {
129
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
130
+ }
131
+ function renameRetryingSync(from, to) {
132
+ for (let attempt = 0;; attempt++) {
133
+ try {
134
+ renameSync(from, to);
135
+ return;
136
+ }
137
+ catch (err) {
138
+ const code = errCode(err);
139
+ const busy = code === "EPERM" || code === "EACCES" || code === "EBUSY";
140
+ if (posix() || !busy || attempt >= WINDOWS_RENAME_RETRY_MS.length)
141
+ throw err;
142
+ sleepSync(WINDOWS_RENAME_RETRY_MS[attempt]);
143
+ }
144
+ }
145
+ }
146
+ /** Best effort: makes a rename survive a power cut. Not every filesystem allows it. */
147
+ function syncDirSync(dir) {
148
+ let fd;
149
+ try {
150
+ fd = openSync(dir, "r");
151
+ fsyncSync(fd);
152
+ }
153
+ catch {
154
+ // the rename stands either way
155
+ }
156
+ finally {
157
+ if (fd !== undefined) {
158
+ try {
159
+ closeSync(fd);
160
+ }
161
+ catch {
162
+ // nothing left to do
163
+ }
164
+ }
165
+ }
166
+ }
167
+ /**
168
+ * Replace `file` with `data` in one step (engram#627): `data` goes to a
169
+ * 0600 temp file beside it, which is then renamed over the old one. A
170
+ * reader sees the old content or the new, never part of either, and a
171
+ * crash or a full disk in the middle leaves the old file whole.
172
+ * config.json used to be rewritten in place, so a write cut short left it
173
+ * truncated and lost the API key and baseUrl.
174
+ *
175
+ * A symlink is written through, to the file it points at. A file this
176
+ * process couldn't have written in place isn't replaced either (EACCES,
177
+ * as an in-place write would have thrown): the rename needs only the
178
+ * directory's permission.
179
+ */
180
+ export function writePrivateFileSync(file, data, options = {}) {
181
+ const durable = options.durable ?? true;
182
+ let target = file;
183
+ try {
184
+ target = realpathSync(file);
185
+ }
186
+ catch (err) {
187
+ if (errCode(err) !== "ENOENT")
188
+ throw err;
189
+ }
190
+ try {
191
+ accessSync(target, constants.W_OK);
192
+ }
193
+ catch (err) {
194
+ if (errCode(err) !== "ENOENT")
195
+ throw err;
196
+ }
197
+ const dir = dirname(target);
198
+ const tmp = join(dir, tempName(basename(target)));
199
+ // "wx": a name that is somehow taken already fails here, before anything
200
+ // that would remove a file this call didn't create.
201
+ const fd = openSync(tmp, "wx", PRIVATE_FILE_MODE);
202
+ let renamed = false;
203
+ try {
204
+ try {
205
+ const owner = ownerFor(dir);
206
+ if (owner)
207
+ fchownSync(fd, owner.uid, owner.gid);
208
+ writeFileSync(fd, data);
209
+ if (durable)
210
+ fsyncSync(fd);
211
+ }
212
+ finally {
213
+ closeSync(fd);
214
+ }
215
+ renameRetryingSync(tmp, target);
216
+ renamed = true;
217
+ }
218
+ finally {
219
+ if (!renamed) {
220
+ try {
221
+ unlinkSync(tmp);
222
+ }
223
+ catch {
224
+ // already gone
225
+ }
226
+ }
227
+ }
228
+ if (durable && posix())
229
+ syncDirSync(dir);
230
+ }
231
+ /**
232
+ * Throw what writePrivateFileSync(file, …) would for a file, or a folder,
233
+ * this process can't write, but write nothing (engram#717). For a command
234
+ * that does what can't be undone before it saves: `engram login` makes a
235
+ * new API key, and a save that then fails loses it. A full disk is found
236
+ * only by writing.
237
+ */
238
+ export function checkWritableSync(file) {
239
+ let target = file;
240
+ try {
241
+ target = realpathSync(file);
242
+ }
243
+ catch (err) {
244
+ if (errCode(err) !== "ENOENT")
245
+ throw err;
246
+ }
247
+ try {
248
+ accessSync(target, constants.W_OK);
249
+ }
250
+ catch (err) {
251
+ if (errCode(err) !== "ENOENT")
252
+ throw err;
253
+ }
254
+ // The new file is made in its folder, and a folder that isn't there yet
255
+ // in the nearest one that is (ensurePrivateDirSync).
256
+ for (let dir = dirname(target);; dir = dirname(dir)) {
257
+ try {
258
+ accessSync(dir, constants.W_OK | constants.X_OK);
259
+ return;
260
+ }
261
+ catch (err) {
262
+ if (errCode(err) !== "ENOENT" || dirname(dir) === dir)
263
+ throw err;
264
+ }
265
+ }
266
+ }
267
+ /**
268
+ * A small file read without waiting: null when there is none, it can't be
269
+ * read, or it isn't a regular file of at most `maxBytes`. It is opened
270
+ * without blocking and checked before it is read: a FIFO put in its place
271
+ * would otherwise hang the reader. BigInt fstat: a plain stat fills the
272
+ * buffer Node's module loader reads (see lstatSocketPath in
273
+ * daemon/instance.ts).
274
+ */
275
+ export function readSmallFileSync(file, maxBytes) {
276
+ let fd;
277
+ try {
278
+ // O_NONBLOCK: undefined on Windows, which has no FIFOs in the file system.
279
+ fd = openSync(file, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
280
+ }
281
+ catch {
282
+ return null;
283
+ }
284
+ try {
285
+ const st = fstatSync(fd, { bigint: true });
286
+ if (!st.isFile() || st.size > BigInt(maxBytes))
287
+ return null;
288
+ const buf = Buffer.alloc(maxBytes + 1);
289
+ const read = readSync(fd, buf, 0, buf.length, 0);
290
+ return { data: buf.subarray(0, read), stamp: `${st.dev}:${st.ino}:${st.mtimeNs}:${st.size}` };
291
+ }
292
+ catch {
293
+ return null;
294
+ }
295
+ finally {
296
+ closeSync(fd);
297
+ }
298
+ }
299
+ /** Only these temp files are ever removed: the rest of ~/.engram is left as it is. */
300
+ const TEMP_FILES_FOR = new Set(["config.json", "status.json", "daemon.pid", "daemon.stop", "vault-changed"]);
301
+ /**
302
+ * How old a leftover temp file must be before it is removed. A write in
303
+ * progress, such as the CLI saving config.json as the daemon starts, has
304
+ * its temp file for a few milliseconds.
305
+ */
306
+ export const STALE_TEMP_MS = 10 * 60_000;
307
+ /**
308
+ * At the daemon's start (engram#627): `dir` made private, and every
309
+ * regular file directly in it set to 0600, so an install from before this
310
+ * fix is covered as well as a new one. Symlinks and subdirectories are left
311
+ * alone, so nothing outside `dir` changes. Temp files that an interrupted
312
+ * write of config.json, status.json, daemon.pid, daemon.stop or
313
+ * vault-changed left behind are removed once they are STALE_TEMP_MS old.
314
+ *
315
+ * Throws only when `dir` can't be created or looked at. One that can't be
316
+ * listed, as a `chmod u-r` leaves it, has its files left as they are, and
317
+ * `unlisted` says why (engram#723): the daemon needs to open its files, not
318
+ * to list them, so that is no reason for it not to start.
319
+ */
320
+ export function makeDirPrivateSync(dir, now = Date.now()) {
321
+ const report = { dir: ensurePrivateDirSync(dir), changed: [], skipped: [], removed: [] };
322
+ let names;
323
+ try {
324
+ names = readdirSync(dir);
325
+ }
326
+ catch (err) {
327
+ report.unlisted = err.code ?? "unknown error";
328
+ return report;
329
+ }
330
+ for (const name of names) {
331
+ const path = join(dir, name);
332
+ let st;
333
+ try {
334
+ // BigInt fields: the daemon's socket is in ~/.engram, and a plain
335
+ // stat of a socket throws off how Node loads modules after it (see
336
+ // lstatSocketPath in daemon/instance.ts).
337
+ st = lstatSync(path, { bigint: true });
338
+ }
339
+ catch {
340
+ continue; // gone since the listing
341
+ }
342
+ if (!st.isFile())
343
+ continue;
344
+ const mode = Number(st.mode);
345
+ const temp = TEMP_FILE.exec(name);
346
+ if (temp && TEMP_FILES_FOR.has(temp[1]) && now - Number(st.mtimeMs) >= STALE_TEMP_MS) {
347
+ try {
348
+ unlinkSync(path);
349
+ report.removed.push(name);
350
+ continue;
351
+ }
352
+ catch {
353
+ // restricted below, like any other file
354
+ }
355
+ }
356
+ if (!posix() || (mode & SHARED_BITS) === 0)
357
+ continue;
358
+ if (!ours(st)) {
359
+ report.skipped.push(name);
360
+ continue;
361
+ }
362
+ try {
363
+ chmodSync(path, unshared(mode));
364
+ report.changed.push(name);
365
+ }
366
+ catch {
367
+ report.skipped.push(name);
368
+ }
369
+ }
370
+ return report;
371
+ }
372
+ //# sourceMappingURL=private-files.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"private-files.js","sourceRoot":"","sources":["../src/private-files.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACL,UAAU,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,UAAU,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,QAAQ,EACR,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,UAAU,EACV,aAAa,GAEd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEpD,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAC;AAEvC,iEAAiE;AACjE,MAAM,WAAW,GAAG,KAAK,CAAC;AAE1B,0EAA0E;AAC1E,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,IAAI,GAAG,MAAM,GAAG,CAAC,WAAW,CAAC;AACtC,CAAC;AAED,SAAS,KAAK;IACZ,OAAO,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC;AACtC,CAAC;AAED,SAAS,OAAO,CAAC,GAAY;IAC3B,OAAQ,GAAoC,EAAE,IAAI,CAAC;AACrD,CAAC;AAED;;;GAGG;AACH,SAAS,IAAI,CAAC,EAA4B;IACxC,OAAO,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;AAChD,CAAC;AAED;;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ClC,IAAI,IAAI,IAAI,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,GAAG,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,aAAa,EAAE,CAAC;YACrF,IAAI,CAAC;gBACH,UAAU,CAAC,IAAI,CAAC,CAAC;gBACjB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC1B,SAAS;YACX,CAAC;YAAC,MAAM,CAAC;gBACP,wCAAwC;YAC1C,CAAC;QACH,CAAC;QACD,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC,KAAK,CAAC;YAAE,SAAS;QACrD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;YACd,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC1B,SAAS;QACX,CAAC;QACD,IAAI,CAAC;YACH,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;YAChC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The Engram server's address, and when two addresses name the same server
3
+ * (engram#685). A module of its own, without config.json's code, so the
4
+ * daemon's modules and their tests can use it whatever they load or mock.
5
+ */
6
+ /** The server used when neither ENGRAM_BASE_URL nor config.json names one. */
7
+ export declare const DEFAULT_BASE_URL = "https://mcp.getengram.app";
8
+ /**
9
+ * One spelling of a server address: scheme and host in lower case, without
10
+ * a default port, a trailing slash, credentials, a query or a fragment. Two
11
+ * addresses with the same spelling here are the same server. Something that
12
+ * isn't a URL is only trimmed.
13
+ */
14
+ export declare function normalizeServer(server: string): string;
15
+ /** Whether `server` is the default server, however it is spelled. */
16
+ export declare function isDefaultServer(server: string): boolean;
17
+ /** Whether `a` and `b` are the same server, however each is spelled. */
18
+ export declare function sameServer(a: string, b: string): boolean;
19
+ /**
20
+ * The server the CLI and the daemon talk to: ENGRAM_BASE_URL, then baseUrl
21
+ * in config.json, then the default (docs/cli.md). Everything that sends a
22
+ * key, checks one or signs in for one resolves it here (engram#625).
23
+ * `engram auth login` checked keys against the default server whatever was
24
+ * configured, and the account commands skipped config.json, so a key could
25
+ * be checked or minted on one server, used on another, and sent to a server
26
+ * the user hadn't configured.
27
+ */
28
+ export declare function resolveBaseUrl(config: {
29
+ baseUrl?: unknown;
30
+ }): string;
31
+ //# sourceMappingURL=server-url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-url.d.ts","sourceRoot":"","sources":["../src/server-url.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,8EAA8E;AAC9E,eAAO,MAAM,gBAAgB,8BAA8B,CAAC;AAE5D;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAQtD;AAED,qEAAqE;AACrE,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEvD;AAED,wEAAwE;AACxE,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAExD;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,MAAM,CAMpE"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The Engram server's address, and when two addresses name the same server
3
+ * (engram#685). A module of its own, without config.json's code, so the
4
+ * daemon's modules and their tests can use it whatever they load or mock.
5
+ */
6
+ /** The server used when neither ENGRAM_BASE_URL nor config.json names one. */
7
+ export const DEFAULT_BASE_URL = "https://mcp.getengram.app";
8
+ /**
9
+ * One spelling of a server address: scheme and host in lower case, without
10
+ * a default port, a trailing slash, credentials, a query or a fragment. Two
11
+ * addresses with the same spelling here are the same server. Something that
12
+ * isn't a URL is only trimmed.
13
+ */
14
+ export function normalizeServer(server) {
15
+ const trimmed = server.trim();
16
+ try {
17
+ const url = new URL(trimmed);
18
+ return `${url.protocol}//${url.host}${url.pathname.replace(/\/+$/, "")}`;
19
+ }
20
+ catch {
21
+ return trimmed.replace(/\/+$/, "");
22
+ }
23
+ }
24
+ /** Whether `server` is the default server, however it is spelled. */
25
+ export function isDefaultServer(server) {
26
+ return normalizeServer(server) === normalizeServer(DEFAULT_BASE_URL);
27
+ }
28
+ /** Whether `a` and `b` are the same server, however each is spelled. */
29
+ export function sameServer(a, b) {
30
+ return normalizeServer(a) === normalizeServer(b);
31
+ }
32
+ /**
33
+ * The server the CLI and the daemon talk to: ENGRAM_BASE_URL, then baseUrl
34
+ * in config.json, then the default (docs/cli.md). Everything that sends a
35
+ * key, checks one or signs in for one resolves it here (engram#625).
36
+ * `engram auth login` checked keys against the default server whatever was
37
+ * configured, and the account commands skipped config.json, so a key could
38
+ * be checked or minted on one server, used on another, and sent to a server
39
+ * the user hadn't configured.
40
+ */
41
+ export function resolveBaseUrl(config) {
42
+ const configured = typeof config.baseUrl === "string" && config.baseUrl.trim() !== "" ? config.baseUrl.trim() : undefined;
43
+ // An empty ENGRAM_BASE_URL names no server, as an empty baseUrl doesn't.
44
+ const env = process.env.ENGRAM_BASE_URL?.trim() || undefined;
45
+ return (env ?? configured ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
46
+ }
47
+ //# sourceMappingURL=server-url.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-url.js","sourceRoot":"","sources":["../src/server-url.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,8EAA8E;AAC9E,MAAM,CAAC,MAAM,gBAAgB,GAAG,2BAA2B,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CA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+ * CLI and the daemon.
4
+ *
5
+ * The vault key encrypts the secrets in the user's vault. With it, `engram
6
+ * store` encrypts the secrets it finds before sending, and the daemon
7
+ * scrubs the vault's values out of what it captures (scrubber.ts). A key
8
+ * file that was there but couldn't be read used to count as no key: both
9
+ * went on without it, and sent those secrets as they were, and nothing
10
+ * said why. Now only a file that isn't there means no key. One that can't
11
+ * be read stops what needs it: the daemon holds sends until it can be
12
+ * read, and a command that would send content refuses.
13
+ *
14
+ * IMPORTANT: Node built-ins only, with config-file.js and
15
+ * daemon/ownership.js, which are too. The daemon reads the key with this.
16
+ */
17
+ export declare const VAULT_KEY_FILE: string;
18
+ /**
19
+ * What is wrong with the file, which decides what fixes it. "gone": the
20
+ * daemon held sends for a key it couldn't read, and now the file isn't
21
+ * there (see scrubber.ts). Only the daemon says it: anywhere else, a file
22
+ * that isn't there is no key.
23
+ */
24
+ export type VaultKeyProblem = "permission" | "folder" | "link" | "empty" | "gone" | "other";
25
+ /**
26
+ * ~/.engram/vault-key is there, but no key can be read from it. The
27
+ * message names the problem and never quotes the file.
28
+ */
29
+ export declare class VaultKeyUnreadable extends Error {
30
+ /** What is wrong, worded to follow "vault-key", e.g. "is empty". */
31
+ readonly problem: string;
32
+ readonly kind: VaultKeyProblem;
33
+ constructor(problem: string, kind: VaultKeyProblem);
34
+ }
35
+ /**
36
+ * The vault key reads, but decrypts none of the vault's secrets
37
+ * (engram#694): they were saved with another key. The daemon can't scrub
38
+ * them from what it captures, so it holds sends until they decrypt or are
39
+ * deleted. `secrets`: how many don't decrypt. Never a name, nor a value.
40
+ */
41
+ export declare class VaultKeyMismatch extends Error {
42
+ /** The secrets it doesn't decrypt. */
43
+ readonly secrets: number;
44
+ /** The secrets it does: some, once sends wait already (engram#694). */
45
+ readonly decrypted: number;
46
+ constructor(secrets: number, decrypted?: number);
47
+ }
48
+ /**
49
+ * `err`, from reading the key, as a VaultKeyUnreadable. Reading it throws
50
+ * nothing else; whatever else might is a key that can't be read too, so the
51
+ * daemon holds on it rather than send without the key.
52
+ */
53
+ export declare function asVaultKeyProblem(err: unknown): VaultKeyUnreadable;
54
+ /**
55
+ * The key in ~/.engram/vault-key, or undefined when there is no such file.
56
+ * Throws VaultKeyUnreadable when the file is there but no key can be read
57
+ * from it. An empty file is one: a key that was there may have been lost,
58
+ * and deleting the file is how to say there is no key.
59
+ */
60
+ export declare function readVaultKeyFile(): Promise<string | undefined>;
61
+ /**
62
+ * The vault key: ENGRAM_VAULT_KEY when it is set, which wins over the file;
63
+ * else readVaultKeyFile's.
64
+ */
65
+ export declare function readVaultKey(): Promise<string | undefined>;
66
+ /**
67
+ * What ~/.engram/vault-key holds now: a key, nothing ("none": it isn't
68
+ * there), or why no key can be read from it. The file alone: the daemon
69
+ * reads it, and doesn't have the ENGRAM_VAULT_KEY a shell may set.
70
+ */
71
+ export declare function vaultKeyFileState(): Promise<"key" | "none" | VaultKeyUnreadable>;
72
+ /** `key` is a vault key: 32 bytes, base64-encoded, as the SDK takes one. */
73
+ export declare function isVaultKey(key: string): boolean;
74
+ /**
75
+ * Why `key` isn't a vault key, as `vault status` says it, or null when it
76
+ * is one: it isn't base64 the SDK reads, or it holds too many or too few
77
+ * bytes.
78
+ */
79
+ export declare function whyNotVaultKey(key: string): string | null;
80
+ /**
81
+ * A vault key's fingerprint (engram#699): the first 8 hex characters of
82
+ * SHA-256 over its 32 bytes. It tells two keys apart and shows none of
83
+ * either. `vault status` and `vault keygen` print it, and what they print
84
+ * may be captured, as the start of the key was. One key written two ways
85
+ * (a line's end, padding) has one fingerprint. Null when `key` isn't a
86
+ * vault key.
87
+ */
88
+ export declare function vaultKeyFingerprint(key: string): string | null;
89
+ /**
90
+ * The same key: the same bytes, however written (a line's end, padding).
91
+ * Two that aren't base64 are the same when their text is.
92
+ */
93
+ export declare function sameVaultKey(a: string, b: string): boolean;
94
+ /**
95
+ * The ways `key` may be written (engram#693): as given, trimmed; and, for a
96
+ * vault key, base64 with its padding and without it, which the CLI and the
97
+ * SDK take alike. The daemon scrubs each from what it captures. Longest
98
+ * first is how to replace them: the padded spelling whole, before the
99
+ * unpadded one, which it holds, could leave its "=" behind.
100
+ */
101
+ export declare function vaultKeySpellings(key: string): string[];
102
+ /**
103
+ * ENGRAM_VAULT_KEY, which commands run in a shell use, against
104
+ * ~/.engram/vault-key, which a daemon started at login reads: it may not
105
+ * get a shell's variables (engram#690). "unset": ENGRAM_VAULT_KEY isn't
106
+ * set. "invalid": it doesn't hold a vault key. "same": the file holds its
107
+ * key. "none": there is no file. "different": the file holds another key.
108
+ * Or why no key can be read from the file.
109
+ */
110
+ export type EnvVaultKeyState = "unset" | "invalid" | "same" | "none" | "different" | VaultKeyUnreadable;
111
+ export declare function envVaultKeyState(): Promise<EnvVaultKeyState>;
112
+ /**
113
+ * ENGRAM_VAULT_KEY is set, and ~/.engram/vault-key holds it: a daemon
114
+ * started without the variable, as launchd starts one, has the key too
115
+ * (engram#690). Written the same, not only the same key: the daemon also
116
+ * scrubs the key's own text from what it captures, and should scrub the
117
+ * text set here. Read synchronously, to choose how to start the daemon.
118
+ */
119
+ export declare function envVaultKeySaved(): boolean;
120
+ /** How to fix it, in a line. */
121
+ export declare function vaultKeyFix(problem: VaultKeyUnreadable): string;
122
+ //# sourceMappingURL=vault-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault-key.d.ts","sourceRoot":"","sources":["../src/vault-key.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAWH,eAAO,MAAM,cAAc,QAA0C,CAAC;AAEtE;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC;AAE5F;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;gBAEnB,OAAO,EAA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