@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,123 @@
1
+ /**
2
+ * engram#659 — files in ~/.engram that aren't the user's. engram#723 — a
3
+ * ~/.engram that is the user's, and that its own permissions keep them out
4
+ * of (lockedDir).
5
+ *
6
+ * Run with sudo, a CLI from before engram#653 started the daemon as root
7
+ * and wrote config.json as root. What root makes in the user's ~/.engram is
8
+ * root's, and 0600: the user's own daemon can't open its database or read
9
+ * config.json, and the CLI can't update them. The daemon used to fail on
10
+ * the first such file and exit with an error. launchd and systemd started
11
+ * it again a few seconds later, and again, for good, and nothing the user
12
+ * would look at said why.
13
+ *
14
+ * This finds them and says what they stop, or hold. It never changes an
15
+ * owner: that takes root, and is the user's to run (OWNERSHIP_FIX).
16
+ *
17
+ * Node built-ins only.
18
+ */
19
+ /** Makes everything in ~/.engram the user's again. */
20
+ export declare const OWNERSHIP_FIX = "sudo chown -R \"$(id -un)\" ~/.engram";
21
+ export interface OwnershipCheck {
22
+ platform?: NodeJS.Platform;
23
+ /** The effective uid: null where there is none (Windows). */
24
+ euid?: number | null;
25
+ env?: NodeJS.ProcessEnv;
26
+ /** ~/.engram */
27
+ dir?: string;
28
+ /** Who owns `path`, through a symlink: null when nothing is there, or it can't be looked at. */
29
+ ownerOf?: (path: string) => number | null;
30
+ /** Whether this process has `mode` (R_OK, W_OK, X_OK) on `path`. */
31
+ canAccess?: (path: string, mode: number) => boolean;
32
+ /** The permission bits of `path`, through a symlink: null when nothing is there, or it can't be looked at. */
33
+ modeOf?: (path: string) => number | null;
34
+ }
35
+ export interface ForeignFiles {
36
+ /** ~/.engram itself is another user's, and nothing can be made in it. */
37
+ dir: boolean;
38
+ /** The files in it that are another user's and get in the way, by name. */
39
+ files: string[];
40
+ /** Root owns every one of them: `sudo engram …`, as a rule. */
41
+ root: boolean;
42
+ /** The daemon can't run until they are the user's. */
43
+ stopsDaemon: boolean;
44
+ /**
45
+ * The daemon runs, but sends nothing until they are the user's: it can't
46
+ * read the vault key, so what it captures waits here (engram#695).
47
+ */
48
+ holdsSends: boolean;
49
+ }
50
+ /**
51
+ * What in ~/.engram is another user's and gets in Engram's way, or null
52
+ * when nothing is (or ~/.engram isn't there yet). Goes by access as well as
53
+ * by owner, so a file that only looks like another user's, as on a network
54
+ * home that maps owners wrongly, isn't counted while Engram can use it.
55
+ *
56
+ * Never on Windows, and never as root: root can use any file, and is
57
+ * refused before this where it would matter (engram#653).
58
+ */
59
+ export declare function foreignFiles({ platform, euid, env, dir, ownerOf, canAccess, }?: OwnershipCheck): ForeignFiles | null;
60
+ /** Whether to say "it" or "them" of what `f` found. */
61
+ export declare function foreignAreMany(f: ForeignFiles): boolean;
62
+ /**
63
+ * What is whose, and what that stops. "daemon.db and config.json in
64
+ * ~/.engram belong to root, not to you, so the daemon can't start, and
65
+ * nothing new is captured." `daemon`: whether the daemon runs now, where
66
+ * that is known. One that runs was started before them, and captures until
67
+ * it stops. What stops the daemon is said rather than what holds its sends:
68
+ * a daemon that doesn't run sends nothing either.
69
+ */
70
+ export declare function foreignFilesProblem(f: ForeignFiles, daemon?: "running" | "stopped"): string;
71
+ /** "Make it yours with: sudo chown …". `then`: "Then make it yours …", after another step. */
72
+ export declare function foreignFilesFix(f: ForeignFiles, then?: boolean): string;
73
+ /**
74
+ * ~/.engram is the user's, and its own permissions keep them out of it
75
+ * (engram#723), as `chmod -R 600 ~/.engram` leaves it: every file in it is
76
+ * still theirs and whole.
77
+ */
78
+ export interface LockedDir {
79
+ /** It can't be searched (no x): nothing in it can be opened, or even looked at. */
80
+ search: boolean;
81
+ /** Nothing can be made in it (no w): no socket, PID file, -wal or -shm, and no file replaced. */
82
+ write: boolean;
83
+ /** The command that gives the user back what is missing: "chmod u+x ~/.engram". */
84
+ command: string;
85
+ }
86
+ /**
87
+ * Whether ~/.engram is the user's and its permissions keep them out of it,
88
+ * or null when they don't (or ~/.engram isn't there yet, or is another
89
+ * user's: foreignFiles). A folder that can't be searched hides every file
90
+ * in it, so what fails is each file the CLI and the daemon open, and what
91
+ * they said was about the file: "config.json can't be read", "Fix the
92
+ * file", a chmod of the file, which can't reach it either.
93
+ *
94
+ * Only what the owner's own bits keep from them counts, so the command
95
+ * that is given, a chmod of their own folder, is sure to fix it. An ACL or
96
+ * a read-only disk is something else, and chmod wouldn't help. A folder
97
+ * that only can't be listed (no r) isn't counted: nothing in Engram needs
98
+ * that but the daemon's tidy-up at start, which goes without it. The
99
+ * command adds r back with the rest, though.
100
+ *
101
+ * Never on Windows, and never as root: root can open anything.
102
+ */
103
+ export declare function lockedDir({ platform, euid, dir, ownerOf, canAccess, modeOf, }?: OwnershipCheck): LockedDir | null;
104
+ /**
105
+ * Why `doing` failed, in a ~/.engram that can't be searched (search):
106
+ * "~/.engram's permissions keep you from opening config.json in it". Said
107
+ * instead of what is wrong with the file, which nothing is.
108
+ */
109
+ export declare function lockedDirReason(doing: string): string;
110
+ /**
111
+ * What ~/.engram's permissions keep the user from, and what that stops.
112
+ * "~/.engram is yours, but its permissions keep you from opening the files
113
+ * in it, so the daemon can't start, and nothing new is captured."
114
+ * `daemon`: whether the daemon runs now, where that is known. One that
115
+ * runs was started before, and has what it opened then; one that can't
116
+ * open the files in it any more can't read config.json either, so it holds
117
+ * what it would capture until it can (engram#673), and then goes on from
118
+ * where it stopped.
119
+ */
120
+ export declare function lockedDirProblem(l: LockedDir, daemon?: "running" | "stopped"): string;
121
+ /** "To fix it, run: chmod u+x ~/.engram" */
122
+ export declare function lockedDirFix(l: LockedDir): string;
123
+ //# sourceMappingURL=ownership.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ownership.d.ts","sourceRoot":"","sources":["../../src/daemon/ownership.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAMH,sDAAsD;AACtD,eAAO,MAAM,aAAa,0CAAwC,CAAC;AAmDnE,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC;IAC3B,6DAA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AmF;IACnF,MAAM,EAAE,OAAO,CAAC;IAChB,iGAAiG;IACjG,KAAK,EAAE,OAAO,CAAC;IACf,mFAAmF;IACnF,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,SAAS,CAAC,EACxB,QAA2B,EAC3B,IAAkC,EAClC,GAAgC,EAChC,OAAe,EACf,SAAkB,EAClB,MAAoB,GACrB,GAAE,cAAmB,GAAG,SAAS,GAAG,IAAI,CAmBxC;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,MAAM,CASrF;AAED,4CAA4C;AAC5C,wBAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,GAAG,MAAM,CAEjD"}
@@ -0,0 +1,245 @@
1
+ /**
2
+ * engram#659 — files in ~/.engram that aren't the user's. engram#723 — a
3
+ * ~/.engram that is the user's, and that its own permissions keep them out
4
+ * of (lockedDir).
5
+ *
6
+ * Run with sudo, a CLI from before engram#653 started the daemon as root
7
+ * and wrote config.json as root. What root makes in the user's ~/.engram is
8
+ * root's, and 0600: the user's own daemon can't open its database or read
9
+ * config.json, and the CLI can't update them. The daemon used to fail on
10
+ * the first such file and exit with an error. launchd and systemd started
11
+ * it again a few seconds later, and again, for good, and nothing the user
12
+ * would look at said why.
13
+ *
14
+ * This finds them and says what they stop, or hold. It never changes an
15
+ * owner: that takes root, and is the user's to run (OWNERSHIP_FIX).
16
+ *
17
+ * Node built-ins only.
18
+ */
19
+ import { accessSync, constants, statSync } from "node:fs";
20
+ import { homedir } from "node:os";
21
+ import { join } from "node:path";
22
+ /** Makes everything in ~/.engram the user's again. */
23
+ export const OWNERSHIP_FIX = 'sudo chown -R "$(id -un)" ~/.engram';
24
+ const { R_OK, W_OK, X_OK } = constants;
25
+ /** In the order they are named. */
26
+ const WATCHED = [
27
+ // The queue of captured messages. SQLite opens the database, and the -wal
28
+ // and -shm beside it, to read and write.
29
+ { name: "daemon.db", stops: { mode: R_OK | W_OK } },
30
+ { name: "daemon.db-wal", stops: { mode: R_OK | W_OK } },
31
+ { name: "daemon.db-shm", stops: { mode: R_OK | W_OK } },
32
+ // The API key, and the paths `engram exclude add` keeps out of capture.
33
+ // Even with ENGRAM_API_KEY set: unread, the exclusions would be lost, and
34
+ // the sessions they keep out captured (engram#673).
35
+ { name: "config.json", stops: { mode: R_OK }, warns: W_OK },
36
+ // The vault key. One that can't be read holds sends, rather than let the
37
+ // values of named secrets go out as they are (engram#677). Another
38
+ // user's too (engram#695): it used to stop the daemon, and capture with
39
+ // it. Not read at all while ENGRAM_VAULT_KEY is set.
40
+ { name: "vault-key", holds: { mode: R_OK, unlessEnv: "ENGRAM_VAULT_KEY" }, warns: W_OK },
41
+ // The daemon's output. One that can't be written only costs the log: the
42
+ // daemon runs without it (engram#679). It used to stop every start
43
+ // through `engram start`, launchd and the systemd unit.
44
+ { name: "daemon.log", warns: W_OK },
45
+ // The daemon replaces these, and the CLI reads them.
46
+ { name: "status.json", warns: R_OK | W_OK },
47
+ { name: "daemon.pid", warns: R_OK | W_OK },
48
+ // Another user's socket is never taken for the daemon's, whatever its
49
+ // mode (engram#612), so the daemon couldn't hold the single-daemon lock.
50
+ // It would run beside the daemon that made the socket, if that one still
51
+ // runs, and everything would be captured twice.
52
+ { name: "daemon.sock", stops: { mode: "always" } },
53
+ ];
54
+ function owner(path) {
55
+ try {
56
+ // BigInt fields: daemon.sock is one of these, and a plain stat of a
57
+ // socket throws off how Node loads modules after it (see
58
+ // lstatSocketPath in instance.ts).
59
+ return Number(statSync(path, { bigint: true }).uid);
60
+ }
61
+ catch {
62
+ return null;
63
+ }
64
+ }
65
+ function access(path, mode) {
66
+ try {
67
+ accessSync(path, mode);
68
+ return true;
69
+ }
70
+ catch {
71
+ return false;
72
+ }
73
+ }
74
+ function permissions(path) {
75
+ try {
76
+ return Number(statSync(path, { bigint: true }).mode) & 0o777;
77
+ }
78
+ catch {
79
+ return null;
80
+ }
81
+ }
82
+ /**
83
+ * What in ~/.engram is another user's and gets in Engram's way, or null
84
+ * when nothing is (or ~/.engram isn't there yet). Goes by access as well as
85
+ * by owner, so a file that only looks like another user's, as on a network
86
+ * home that maps owners wrongly, isn't counted while Engram can use it.
87
+ *
88
+ * Never on Windows, and never as root: root can use any file, and is
89
+ * refused before this where it would matter (engram#653).
90
+ */
91
+ export function foreignFiles({ platform = process.platform, euid = process.geteuid?.() ?? null, env = process.env, dir = join(homedir(), ".engram"), ownerOf = owner, canAccess = access, } = {}) {
92
+ if (platform === "win32" || euid === null || euid === 0)
93
+ return null;
94
+ const dirOwner = ownerOf(dir);
95
+ if (dirOwner === null)
96
+ return null;
97
+ const found = {
98
+ dir: false,
99
+ files: [],
100
+ root: true,
101
+ stopsDaemon: false,
102
+ holdsSends: false,
103
+ };
104
+ const counted = (uid) => {
105
+ if (uid !== 0)
106
+ found.root = false;
107
+ };
108
+ // The daemon makes its socket, and SQLite its -wal and -shm, in it.
109
+ if (dirOwner !== euid && !canAccess(dir, R_OK | W_OK | X_OK)) {
110
+ found.dir = true;
111
+ found.stopsDaemon = true;
112
+ counted(dirOwner);
113
+ }
114
+ for (const { name, stops, holds, warns } of WATCHED) {
115
+ const path = join(dir, name);
116
+ const uid = ownerOf(path);
117
+ if (uid === null || uid === euid)
118
+ continue;
119
+ const stopped = stops !== undefined &&
120
+ !(stops.unlessEnv && env[stops.unlessEnv]) &&
121
+ (stops.mode === "always" || !canAccess(path, stops.mode));
122
+ const held = holds !== undefined && !(holds.unlessEnv && env[holds.unlessEnv]) && !canAccess(path, holds.mode);
123
+ const warned = warns !== undefined && !canAccess(path, warns);
124
+ if (!stopped && !held && !warned)
125
+ continue;
126
+ found.files.push(name);
127
+ counted(uid);
128
+ if (stopped)
129
+ found.stopsDaemon = true;
130
+ if (held)
131
+ found.holdsSends = true;
132
+ }
133
+ return found.dir || found.files.length > 0 ? found : null;
134
+ }
135
+ /** "a", "a and b", "a, b and c", "a, b, c and 2 more" */
136
+ function names(list) {
137
+ const shown = list.length > 4 ? [...list.slice(0, 3), `${list.length - 3} more`] : list;
138
+ return shown.length === 1 ? shown[0] : `${shown.slice(0, -1).join(", ")} and ${shown[shown.length - 1]}`;
139
+ }
140
+ /** Whether to say "it" or "them" of what `f` found. */
141
+ export function foreignAreMany(f) {
142
+ return !f.dir && f.files.length > 1;
143
+ }
144
+ /**
145
+ * What is whose, and what that stops. "daemon.db and config.json in
146
+ * ~/.engram belong to root, not to you, so the daemon can't start, and
147
+ * nothing new is captured." `daemon`: whether the daemon runs now, where
148
+ * that is known. One that runs was started before them, and captures until
149
+ * it stops. What stops the daemon is said rather than what holds its sends:
150
+ * a daemon that doesn't run sends nothing either.
151
+ */
152
+ export function foreignFilesProblem(f, daemon) {
153
+ const many = foreignAreMany(f);
154
+ const what = f.dir ? "~/.engram" : `${names(f.files)} in ~/.engram`;
155
+ const whose = `${what} ${many ? "belong" : "belongs"} to ${f.root ? "root" : "another user"}, not to you`;
156
+ if (!f.stopsDaemon) {
157
+ if (f.holdsSends)
158
+ return `${whose}. Until ${many ? "they are" : "it is"} yours, captured messages wait on this machine: nothing is lost.`;
159
+ if (!f.dir && f.files.length === 1 && f.files[0] === "daemon.log")
160
+ return `${whose}, so the daemon can't write its log.`;
161
+ return `${whose}, so Engram can't update ${many ? "them" : "it"}.`;
162
+ }
163
+ if (daemon === "running")
164
+ return `${whose}, so the daemon can't start again once it stops.`;
165
+ if (daemon === "stopped")
166
+ return `${whose}, so the daemon can't start, and nothing new is captured.`;
167
+ return `${whose}, so the daemon can't start.`;
168
+ }
169
+ /** "Make it yours with: sudo chown …". `then`: "Then make it yours …", after another step. */
170
+ export function foreignFilesFix(f, then = false) {
171
+ return `${then ? "Then make" : "Make"} ${foreignAreMany(f) ? "them" : "it"} yours with: ${OWNERSHIP_FIX}`;
172
+ }
173
+ /**
174
+ * Whether ~/.engram is the user's and its permissions keep them out of it,
175
+ * or null when they don't (or ~/.engram isn't there yet, or is another
176
+ * user's: foreignFiles). A folder that can't be searched hides every file
177
+ * in it, so what fails is each file the CLI and the daemon open, and what
178
+ * they said was about the file: "config.json can't be read", "Fix the
179
+ * file", a chmod of the file, which can't reach it either.
180
+ *
181
+ * Only what the owner's own bits keep from them counts, so the command
182
+ * that is given, a chmod of their own folder, is sure to fix it. An ACL or
183
+ * a read-only disk is something else, and chmod wouldn't help. A folder
184
+ * that only can't be listed (no r) isn't counted: nothing in Engram needs
185
+ * that but the daemon's tidy-up at start, which goes without it. The
186
+ * command adds r back with the rest, though.
187
+ *
188
+ * Never on Windows, and never as root: root can open anything.
189
+ */
190
+ export function lockedDir({ platform = process.platform, euid = process.geteuid?.() ?? null, dir = join(homedir(), ".engram"), ownerOf = owner, canAccess = access, modeOf = permissions, } = {}) {
191
+ if (platform === "win32" || euid === null || euid === 0)
192
+ return null;
193
+ if (ownerOf(dir) !== euid)
194
+ return null;
195
+ const mode = modeOf(dir);
196
+ if (mode === null)
197
+ return null;
198
+ const search = (mode & 0o100) === 0 && !canAccess(dir, X_OK);
199
+ const write = (mode & 0o200) === 0 && !canAccess(dir, W_OK);
200
+ if (!search && !write)
201
+ return null;
202
+ const missing = [
203
+ [0o400, "r"],
204
+ [0o200, "w"],
205
+ [0o100, "x"],
206
+ ]
207
+ .filter(([bit]) => (mode & bit) === 0)
208
+ .map(([, letter]) => letter)
209
+ .join("");
210
+ return { search, write, command: `chmod u+${missing} ~/.engram` };
211
+ }
212
+ /**
213
+ * Why `doing` failed, in a ~/.engram that can't be searched (search):
214
+ * "~/.engram's permissions keep you from opening config.json in it". Said
215
+ * instead of what is wrong with the file, which nothing is.
216
+ */
217
+ export function lockedDirReason(doing) {
218
+ return `~/.engram's permissions keep you from ${doing} in it`;
219
+ }
220
+ /**
221
+ * What ~/.engram's permissions keep the user from, and what that stops.
222
+ * "~/.engram is yours, but its permissions keep you from opening the files
223
+ * in it, so the daemon can't start, and nothing new is captured."
224
+ * `daemon`: whether the daemon runs now, where that is known. One that
225
+ * runs was started before, and has what it opened then; one that can't
226
+ * open the files in it any more can't read config.json either, so it holds
227
+ * what it would capture until it can (engram#673), and then goes on from
228
+ * where it stopped.
229
+ */
230
+ export function lockedDirProblem(l, daemon) {
231
+ const kept = `~/.engram is yours, but its permissions keep you from ${l.search ? "opening the files in it" : "making files in it"}`;
232
+ if (daemon === "running") {
233
+ return l.search
234
+ ? `${kept}, so capture is paused, and the daemon can't start again once it stops.`
235
+ : `${kept}, so the daemon can't start again once it stops.`;
236
+ }
237
+ if (daemon === "stopped")
238
+ return `${kept}, so the daemon can't start, and nothing new is captured.`;
239
+ return `${kept}, so the daemon can't start.`;
240
+ }
241
+ /** "To fix it, run: chmod u+x ~/.engram" */
242
+ export function lockedDirFix(l) {
243
+ return `To fix it, run: ${l.command}`;
244
+ }
245
+ //# sourceMappingURL=ownership.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ownership.js","sourceRoot":"","sources":["../../src/daemon/ownership.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,sDAAsD;AACtD,MAAM,CAAC,MAAM,aAAa,GAAG,qCAAqC,CAAC;AAEnE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,SAAS,CAAC;AAmBvC,mCAAmC;AACnC,MAAM,OAAO,GAAc;IACzB,0EAA0E;IAC1E,yCAAyC;IACzC,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,EAAE;IACnD,EAAE,IAAI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AAC;AACpE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,KAAa;IAC3C,OAAO,yCAAyC,KAAK,QAAQ,CAAC;AAChE,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,gBAAgB,CAAC,CAAY,EAAE,MAA8B;IAC3E,MAAM,IAAI,GAAG,yDAAyD,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC,oBAAoB,EAAE,CAAC;IACpI,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,CAAC,CAAC,MAAM;YACb,CAAC,CAAC,GAAG,IAAI,yEAAyE;YAClF,CAAC,CAAC,GAAG,IAAI,kDAAkD,CAAC;IAChE,CAAC;IACD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,GAAG,IAAI,2DAA2D,CAAC;IACpG,OAAO,GAAG,IAAI,8BAA8B,CAAC;AAC/C,CAAC;AAED,4CAA4C;AAC5C,MAAM,UAAU,YAAY,CAAC,CAAY;IACvC,OAAO,mBAAmB,CAAC,CAAC,OAAO,EAAE,CAAC;AACxC,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Where `path` is, as a message names it ("npx's cache"), when a package
3
+ * runner installed it there; null for any other path. POSIX and Windows
4
+ * paths alike.
5
+ */
6
+ export declare function runnerCacheOf(path: string): string | null;
7
+ /**
8
+ * The command that runs the CLI at `path` through the package runner that
9
+ * installed it there ("npx @getengram/cli"); null for any other path.
10
+ */
11
+ export declare function runnerCommandOf(path: string): string | null;
12
+ /** How to install the CLI, so that start at login names files that stay. */
13
+ export declare const INSTALL_CLI = "npm install -g @getengram/cli";
14
+ /**
15
+ * Where this CLI runs from, as a message names it ("npx's cache"), when a
16
+ * package runner installed it there (engram#665); null when it is
17
+ * installed.
18
+ */
19
+ export declare function runnerCacheOfThisCli(): string | null;
20
+ /**
21
+ * `engram <rest>` as the user can run it from here (engram#727): through
22
+ * the package runner this CLI runs from (`npx @getengram/cli stop`), or
23
+ * `engram stop` when it is installed. Start at login (`install`, `start
24
+ * --install`) needs the CLI installed, so from a runner that comes first.
25
+ * With no `rest`, the command alone.
26
+ */
27
+ export declare function engramCommand(rest?: string): string;
28
+ //# sourceMappingURL=package-runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package-runner.d.ts","sourceRoot":"","sources":["../../src/daemon/package-runner.ts"],"names":[],"mappings":"AAkEA;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAEzD;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAE3D;AAED,4EAA4E;AAC5E,eAAO,MAAM,WAAW,kCAAkC,CAAC;AAU3D;;;;GAIG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,GAAG,IAAI,CAEpD;AAKD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,SAAK,GAAG,MAAM,CAM/C"}
@@ -0,0 +1,113 @@
1
+ import { loginStartPath, workerPath } from "./launch.js";
2
+ /**
3
+ * A CLI that a package runner runs (engram#665): `npx @getengram/cli`,
4
+ * `pnpm dlx`, `bunx`. Each installs the package into a cache or a temporary
5
+ * folder and runs it from there, and none promises the folder stays:
6
+ * clearing npm's cache is the usual cure for a stale npx, pnpm makes a new
7
+ * dlx folder after a day, and a temporary folder goes when the system
8
+ * clears it. Start at login written from such a run named the worker in
9
+ * that folder, and once the folder was gone the daemon no longer started at
10
+ * login, without a word.
11
+ *
12
+ * Told by the path the CLI runs from, which is the path start at login
13
+ * would name, and not by the environment: npm sets npm_command=exec for a
14
+ * project's own node_modules/.bin too, and an installed CLI that a script
15
+ * run through npx starts inherits it.
16
+ *
17
+ * Run that way, the CLI isn't on PATH, so what it says names its commands
18
+ * as it was run (engram#727): `npx @getengram/cli stop`, not `engram stop`,
19
+ * which the shell can't find. Each runner runs the package's only bin.
20
+ */
21
+ /**
22
+ * Where a package runner installs a package to run it, how a message names
23
+ * that, and the command that runs the CLI through the runner.
24
+ */
25
+ const RUNNER_CACHES = [
26
+ // npm 7 and later: <npm cache>/_npx/<hash>/node_modules/…; npm 6:
27
+ // _npx/<pid>/lib/node_modules/….
28
+ {
29
+ pattern: /[\\/]_npx[\\/][^\\/]+[\\/](?:lib[\\/])?node_modules[\\/]/,
30
+ where: "npx's cache",
31
+ runs: "npx @getengram/cli",
32
+ },
33
+ // pnpm 9 and later: <pnpm cache>/dlx/<key>/<time>-<pid>/node_modules/…,
34
+ // which <key>/pkg links to.
35
+ {
36
+ pattern: /[\\/]dlx[\\/][0-9a-z]+[\\/](?:[0-9a-f]+-[0-9a-f]+|pkg)[\\/]node_modules[\\/]/,
37
+ where: "pnpm's dlx cache",
38
+ runs: "pnpm dlx @getengram/cli",
39
+ },
40
+ // bunx: <bun's cache or temporary folder>/.bunx-<uid>/<package>/…, or
41
+ // bunx-<uid>-<package>/… in the temporary folder.
42
+ {
43
+ pattern: /[\\/](?:\.bunx-\d+|bunx-\d+-[^\\/]+)[\\/](?:[^\\/]+[\\/])*node_modules[\\/]/,
44
+ where: "bunx's cache",
45
+ runs: "bunx @getengram/cli",
46
+ },
47
+ // pnpm 8: <store>/v3/tmp/dlx-<pid>, removed as the command ends.
48
+ {
49
+ pattern: /[\\/]tmp[\\/]dlx-\d+[\\/](?:[^\\/]+[\\/])*node_modules[\\/]/,
50
+ where: "a temporary folder",
51
+ runs: "pnpm dlx @getengram/cli",
52
+ },
53
+ // yarn dlx: <tmp>/xfs-<hex>/dlx-<pid>, removed as the command ends.
54
+ {
55
+ pattern: /[\\/]xfs-[0-9a-f]+[\\/]dlx-\d+[\\/](?:[^\\/]+[\\/])*node_modules[\\/]/,
56
+ where: "a temporary folder",
57
+ runs: "yarn dlx @getengram/cli",
58
+ },
59
+ ];
60
+ function runnerOf(path) {
61
+ return RUNNER_CACHES.find(({ pattern }) => pattern.test(path)) ?? null;
62
+ }
63
+ /**
64
+ * Where `path` is, as a message names it ("npx's cache"), when a package
65
+ * runner installed it there; null for any other path. POSIX and Windows
66
+ * paths alike.
67
+ */
68
+ export function runnerCacheOf(path) {
69
+ return runnerOf(path)?.where ?? null;
70
+ }
71
+ /**
72
+ * The command that runs the CLI at `path` through the package runner that
73
+ * installed it there ("npx @getengram/cli"); null for any other path.
74
+ */
75
+ export function runnerCommandOf(path) {
76
+ return runnerOf(path)?.runs ?? null;
77
+ }
78
+ /** How to install the CLI, so that start at login names files that stay. */
79
+ export const INSTALL_CLI = "npm install -g @getengram/cli";
80
+ /**
81
+ * The path start at login would name for this CLI: the worker, and on
82
+ * Windows the sign-in entry beside it. Where it is tells how the CLI runs.
83
+ */
84
+ function thisCliPath() {
85
+ return process.platform === "win32" ? loginStartPath() : workerPath();
86
+ }
87
+ /**
88
+ * Where this CLI runs from, as a message names it ("npx's cache"), when a
89
+ * package runner installed it there (engram#665); null when it is
90
+ * installed.
91
+ */
92
+ export function runnerCacheOfThisCli() {
93
+ return runnerCacheOf(thisCliPath());
94
+ }
95
+ /** Commands that turn on start at login, which a runner's run refuses (engram#665). */
96
+ const STARTS_AT_LOGIN = /^(?:install|start --install)$/;
97
+ /**
98
+ * `engram <rest>` as the user can run it from here (engram#727): through
99
+ * the package runner this CLI runs from (`npx @getengram/cli stop`), or
100
+ * `engram stop` when it is installed. Start at login (`install`, `start
101
+ * --install`) needs the CLI installed, so from a runner that comes first.
102
+ * With no `rest`, the command alone.
103
+ */
104
+ export function engramCommand(rest = "") {
105
+ const runs = runnerCommandOf(thisCliPath());
106
+ const command = rest === "" ? "engram" : `engram ${rest}`;
107
+ if (runs === null)
108
+ return command;
109
+ if (STARTS_AT_LOGIN.test(rest))
110
+ return `${INSTALL_CLI} && ${command}`;
111
+ return rest === "" ? runs : `${runs} ${rest}`;
112
+ }
113
+ //# sourceMappingURL=package-runner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package-runner.js","sourceRoot":"","sources":["../../src/daemon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CAAC;AAE3D;;;GAGG;AACH,SAAS,WAAW;IAClB,OAAO,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;AACxE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,aAAa,CAAC,WAAW,EAAE,CAAC,CAAC;AACtC,CAAC;AAED,uFAAuF;AACvF,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAExD;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,IAAI,GAAG,EAAE;IACrC,MAAM,IAAI,GAAG,eAAe,CAAC,WAAW,EAAE,CAAC,CAAC;IAC5C,MAAM,OAAO,GAAG,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,UAAU,IAAI,EAAE,CAAC;IAC1D,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,OAAO,CAAC;IAClC,IAAI,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,GAAG,WAAW,OAAO,OAAO,EAAE,CAAC;IACtE,OAAO,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC;AAChD,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import type { OnMessages, LineParser } from "./types.js";
2
+ export declare const REDACTED_TOOL_OUTPUT = "[output withheld by engram: this command prints a secret]";
2
3
  /**
3
4
  * Parses Claude Code JSONL transcript lines into Engram messages.
4
5
  *
@@ -6,19 +7,63 @@ import type { OnMessages, LineParser } from "./types.js";
6
7
  * response may span multiple lines sharing the same `message.id`. We
7
8
  * accumulate blocks and flush when the message is complete (stop_reason
8
9
  * is set) or a new message begins.
10
+ *
11
+ * One parser reads every transcript of the host, interleaved: a session's
12
+ * subagents and workflows write files of their own next to it. So the
13
+ * accumulator is kept per file, and a message is emitted under the session
14
+ * of its own lines (engram#579). It used to be a single accumulator emitted
15
+ * under whichever session the parser had seen last, from any file — a
16
+ * reply could be filed under another session, or dropped along with it when
17
+ * that session was opted out.
9
18
  */
10
19
  export declare class Parser implements LineParser {
20
+ /**
21
+ * Every line names its session, so resuming a file needs no context but
22
+ * the recent tool calls: a `vault get` whose output is still to come.
23
+ * Wide enough to reach back past parallel tool calls with large outputs;
24
+ * past it, the value-exact scrubber (scrubber.ts) still applies.
25
+ */
26
+ readonly resumeContext: {
27
+ headBytes: number;
28
+ tailBytes: number;
29
+ };
30
+ /** The buffered assistant message of each file that has one. */
11
31
  private pending;
12
- private lastSessionId;
13
- private lastMeta;
32
+ /**
33
+ * tool_use ids whose result must not be captured. Bounded: an id is
34
+ * removed once its result is queued, and a session produces a few
35
+ * hundred tool calls at most. Tool ids are unique, so one set serves
36
+ * every file.
37
+ */
38
+ private secretToolIds;
14
39
  private onMessages;
15
40
  constructor(onMessages: OnMessages);
16
- processLine(line: string, filePath: string): void;
17
- /** Flush any remaining accumulated assistant message. */
18
- flush(): void;
41
+ processLine(line: string, filePath: string, offset: number): void;
42
+ /**
43
+ * Replays a line an earlier daemon process already handled — the watcher
44
+ * does this for the lines just before where it resumes a file. Only one
45
+ * thing spans lines across a restart: which tool calls print a secret.
46
+ * Without it, a `vault get` whose output was written after the daemon
47
+ * restarted had that output captured. Never emits.
48
+ */
49
+ restoreLine(line: string, _filePath: string): void;
50
+ bufferedFrom(filePath: string): number | null;
51
+ discard(filePath: string): void;
52
+ /** Emit buffered assistant messages: one file's, or every file's. */
53
+ flush(filePath?: string): void;
19
54
  private processUserLine;
20
55
  private processAssistantLine;
56
+ private trackSecretTool;
21
57
  private flushPending;
22
58
  private extractProjectDir;
23
59
  }
60
+ /**
61
+ * Project name from a Claude Code transcript path:
62
+ * ~/.claude/projects/-Users-op-code-engram/session.jsonl → "engram"
63
+ * C:\Users\op\.claude\projects\C--Users-op-code-engram\session.jsonl → "engram"
64
+ * (the last segment of the hyphen-encoded project directory). Either
65
+ * separator: split on "/" alone, every Windows conversation was filed under
66
+ * "unknown" (engram#602).
67
+ */
68
+ export declare function claudeProjectDir(filePath: string): string;
24
69
  //# sourceMappingURL=parser.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/daemon/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA8B,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AA2CrF;;;;;;;GAOG;AACH,qBAAa,MAAO,YAAW,UAAU;IACvC,OAAO,CAAC,OAAO,CAAiC;IAChD,OAAO,CAAC,aAAa,CAAuB;IAC5C,OAAO,CAAC,QAAQ,CAA4B;IAC5C,OAAO,CAAC,UAAU,CAAa;gBAEnB,UAAU,EAAE,UAAU;IAIlC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAuCjD,yDAAyD;IACzD,KAAK,IAAI,IAAI;IAIb,OAAO,CAAC,eAAe;IA0CvB,OAAO,CAAC,oBAAoB;IA4C5B,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,iBAAiB;CAa1B"}
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/daemon/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA8B,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AA6FrF,eAAO,MAAM,oBAAoB,8DAC4B,CAAC;AA6B9D;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,MAAO,YAAW,UAAU;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,aAAa;;;MAAgD;IAEtE,gEAAgE;IAChE,OAAO,CAAC,OAAO,CAAuC;IAEtD;;;;;OAKG;IACH,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,UAAU,CAAa;gBAEnB,UAAU,EAAE,UAAU;IAIlC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,M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