@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,427 @@
1
+ /**
2
+ * One capture daemon per user (engram#612).
3
+ *
4
+ * The worker listens on a local socket that only its user can reach:
5
+ * ~/.engram/daemon.sock on macOS and Linux, a named pipe on Windows. A
6
+ * worker that finds it answered exits at once, so a second start (launchd at
7
+ * login beside `engram start`, or two starts at the same moment) can't put
8
+ * two daemons on the same transcripts, each capturing and sending them.
9
+ * "Running" means it answers, and the answer gives the worker's PID and
10
+ * version.
11
+ *
12
+ * The PID file used to be the only record, and only a liveness test checked
13
+ * it: a file left by a daemon that didn't exit cleanly could name a PID that
14
+ * had since gone to another program, which `engram status` then reported as
15
+ * the daemon and `engram stop` signalled.
16
+ *
17
+ * The socket is one-way. A client gets one line of JSON and the connection
18
+ * ends; nothing a client sends is read, so nothing that reaches the socket
19
+ * can make the daemon do anything. On Windows `engram stop` asks the daemon
20
+ * to stop through a file in ~/.engram instead (STOP_FILE, engram#613),
21
+ * which only the user can write.
22
+ *
23
+ * daemon.pid is still written, for CLIs from before this change, and is
24
+ * trusted only for a process confirmed to be the daemon (identifyProcess).
25
+ *
26
+ * A daemon running as root in the user's ~/.engram is never the daemon
27
+ * (engram#680): a worker started as root exits at once, and one already
28
+ * running as root, which `sudo engram start` or `sudo engram install` left
29
+ * before engram#653, keeps any other from starting while it runs.
30
+ *
31
+ * IMPORTANT: Node built-ins only. worker.ts claims the lock before the
32
+ * native-module preflight, so nothing here may load better-sqlite3, directly
33
+ * or through another module (see launch.ts).
34
+ */
35
+ import { type Server } from "node:net";
36
+ import { type SystemJob } from "./launchd.js";
37
+ /** This package's version, which the daemon gives when it answers. */
38
+ export declare const VERSION: string;
39
+ /**
40
+ * How long a start waits for a daemon that is stopping: its shutdown waits
41
+ * up to 7 s for a send in progress and 7 s for the last one.
42
+ */
43
+ export declare const STOPPING_WAIT_MS = 20000;
44
+ export interface Endpoint {
45
+ /** The socket's path, or on Windows the pipe's name. */
46
+ path: string;
47
+ pipe: boolean;
48
+ }
49
+ export type EndpointOrReason = Endpoint | {
50
+ unavailable: string;
51
+ };
52
+ /**
53
+ * ~/.engram/daemon.sock, or on Windows a named pipe. Pipe names are one
54
+ * namespace for the whole machine, so the name carries a hash of this
55
+ * user's ~/.engram.
56
+ *
57
+ * A socket path longer than the platform takes is cut short by Node 20 and
58
+ * refused by later versions. A home folder that long gets no lock: the
59
+ * daemon still runs, with the PID file as its only record (see
60
+ * claimDaemonLock).
61
+ */
62
+ export declare function daemonEndpoint(dir?: string, platform?: NodeJS.Platform): EndpointOrReason;
63
+ /** What the daemon says about itself. */
64
+ export interface DaemonInfo {
65
+ pid: number;
66
+ /** Its package version, when it gave a well-formed one. */
67
+ version?: string;
68
+ startedAt?: string;
69
+ /** Which server answered: tells a daemon's own socket from one bound since. */
70
+ id?: string;
71
+ /** Its modules have loaded and capture is set up: `engram start` waits for this. */
72
+ ready: boolean;
73
+ /** It is shutting down: finishing a send, then exiting. */
74
+ stopping: boolean;
75
+ /** It stops when STOP_FILE asks it to (engram#613): on Windows, from this version on. */
76
+ stopFile: boolean;
77
+ /**
78
+ * It reads the vault key again before sending (engram#693): a key saved
79
+ * while it runs is used without a restart. One from before reads it only
80
+ * at start.
81
+ */
82
+ vaultKeyReread: boolean;
83
+ /**
84
+ * It reads the vault again before its next send when `engram vault set`
85
+ * or `engram vault delete` says the vault changed (engram#701). One from
86
+ * before reads it every 10 minutes.
87
+ */
88
+ vaultChangeNotice: boolean;
89
+ /**
90
+ * Whether it has a vault key (engram#698). false: it has none, so only
91
+ * the built-in patterns are removed from what it sends. true: it has one,
92
+ * or holds sends for one it can't read. Undefined: one from before, or
93
+ * one that hasn't read it yet.
94
+ */
95
+ vaultKey?: boolean;
96
+ /**
97
+ * Where its output goes (engram#720): see daemonOutput. Undefined: one
98
+ * from before, one on Windows, or one that can't tell.
99
+ */
100
+ output?: DaemonOutput;
101
+ }
102
+ /**
103
+ * Where the daemon's output goes (engram#720). Since engram#679, a daemon
104
+ * whose ~/.engram/daemon.log can't be written starts without it: `engram
105
+ * start` drops its output, the systemd unit sends it to the journal, and
106
+ * the launchd job runs from a plist without a log. Once the log could be
107
+ * written again, `engram status` named it as the daemon's log, though the
108
+ * daemon wrote somewhere else until it was started again.
109
+ *
110
+ * - "log": daemon.log, the file at that path now.
111
+ * - "terminal": the terminal it was started in (`engram start --foreground`).
112
+ * - "deleted": a file that is no longer anywhere: the log, deleted or
113
+ * replaced while the daemon ran.
114
+ * - "file": another file.
115
+ * - "none": not a file: nowhere (/dev/null), the systemd journal, a pipe.
116
+ */
117
+ export type DaemonOutput = "log" | "terminal" | "deleted" | "file" | "none";
118
+ /**
119
+ * Where this process's output goes: `fd`, stderr, which the daemon writes
120
+ * its log lines to, and which every start points at the same place as
121
+ * stdout. Undefined on Windows, which isn't asked, and when it can't be
122
+ * told. Read again each time the daemon answers, so a log deleted or
123
+ * replaced while it runs is seen without a timer. BigInt stats, as for the
124
+ * socket (see lstatSocketPath). Exported for tests.
125
+ */
126
+ export declare function daemonOutput(fd?: number, log?: string, platform?: NodeJS.Platform): DaemonOutput | undefined;
127
+ /**
128
+ * The daemon's answer, or null for anything else. What is printed from it
129
+ * (the version) is checked character by character: whatever holds the
130
+ * socket, a terminal never gets control characters from it.
131
+ */
132
+ export declare function parseAnswer(line: string): DaemonInfo | null;
133
+ export type Probe = {
134
+ state: "answered";
135
+ info: DaemonInfo;
136
+ }
137
+ /**
138
+ * Something holds the endpoint but didn't answer in time: a daemon whose
139
+ * event loop is busy (the preflight's `npm rebuild` blocks it for a minute
140
+ * or two), or something that isn't a daemon at all.
141
+ */
142
+ | {
143
+ state: "silent";
144
+ }
145
+ /** Nothing listens. `stale`: a socket file is there, left by a daemon that didn't exit cleanly. */
146
+ | {
147
+ state: "none";
148
+ stale: boolean;
149
+ }
150
+ /** Can't tell (reason): the endpoint can't be used here, or isn't the user's own. */
151
+ | {
152
+ state: "unavailable";
153
+ reason: string;
154
+ };
155
+ /** Ask whatever listens on the daemon's endpoint who it is. Never throws. */
156
+ export declare function probeDaemon(options?: {
157
+ timeoutMs?: number;
158
+ endpoint?: EndpointOrReason;
159
+ }): Promise<Probe>;
160
+ /** Who holds the endpoint, when it isn't this process. */
161
+ export type Taken = {
162
+ why: "socket";
163
+ info: DaemonInfo;
164
+ } | {
165
+ why: "silent";
166
+ } | {
167
+ why: "stopping";
168
+ pid: number;
169
+ } | {
170
+ why: "pidfile";
171
+ pid: number;
172
+ } | {
173
+ why: "unknown";
174
+ tries: number;
175
+ };
176
+ export interface Held {
177
+ server: Server;
178
+ endpoint: Endpoint;
179
+ /** What this server answers with, to tell its socket from one bound since. */
180
+ id: string;
181
+ /** The socket file's identity, on macOS and Linux (0n for a pipe). */
182
+ dev: bigint;
183
+ ino: bigint;
184
+ }
185
+ export type LockResult = {
186
+ state: "held";
187
+ held: Held;
188
+ } | {
189
+ state: "taken";
190
+ taken: Taken;
191
+ } | {
192
+ state: "unavailable";
193
+ reason: string;
194
+ };
195
+ export interface LockOptions {
196
+ log?: (msg: string) => void;
197
+ /** Tests shorten it. */
198
+ settleMs?: number;
199
+ }
200
+ /**
201
+ * The daemon named in the PID file, when it is alive and confirmed to be
202
+ * one (and isn't this process): the user's own, or, `root`, one running as
203
+ * root (engram#680).
204
+ */
205
+ export declare function daemonInPidFile(): {
206
+ pid: number;
207
+ root: boolean;
208
+ } | null;
209
+ /** The user's own daemon, named in the PID file and confirmed (see daemonInPidFile). */
210
+ export declare function confirmedDaemonInPidFile(): number | null;
211
+ /**
212
+ * A daemon running as root for this user (engram#680), which `sudo engram
213
+ * start` or `sudo engram install` started before engram#653: `inFile`, the
214
+ * one the PID file names, or, on macOS, the one `job` runs, the job such an
215
+ * install loaded into launchd's system domain from the user's own plist. A
216
+ * CLI from before this change removed the PID file of a daemon it couldn't
217
+ * signal. Each is looked up unless the caller has, the job only when the
218
+ * PID file names none. Null when none runs.
219
+ */
220
+ export declare function rootDaemonPid(inFile?: {
221
+ pid: number;
222
+ root: boolean;
223
+ } | null, job?: SystemJob | null): number | null;
224
+ /**
225
+ * Take the endpoint for this process, or find out who has it.
226
+ *
227
+ * Binding is the lock: two binds to one path can't both succeed, and a
228
+ * second named pipe of the same name can't be made. A socket file left by a
229
+ * daemon that died (it refuses connections) is removed and the bind tried
230
+ * again. After binding, the socket must still answer as this server once a
231
+ * stale-socket removal racing this one has had time to land.
232
+ */
233
+ export declare function lockEndpoint(endpoint: Endpoint, options?: LockOptions): Promise<LockResult>;
234
+ /**
235
+ * Let the endpoint go. On macOS and Linux the socket file is removed only
236
+ * while it is still this server's, and the server is never closed: closing
237
+ * removes whatever file is at the path by then, by name. The process exits
238
+ * right after, which ends the server without touching the path.
239
+ */
240
+ export declare function unlockEndpoint(held: Held): void;
241
+ /**
242
+ * The PID in daemon.pid: digits only, so "-1" or "0", which would signal
243
+ * every process the user has or the whole process group, never come back.
244
+ */
245
+ export declare function pidInFile(file?: string): number | null;
246
+ /** Remove daemon.pid when it names `pid`, and only then. */
247
+ export declare function removePidFileIf(pid: number, file?: string): void;
248
+ export declare function alive(pid: number): boolean;
249
+ /**
250
+ * What a process that runs `args` as the user `owner` is to the user `me`:
251
+ * their daemon, a daemon running as root (engram#680), or something else.
252
+ * Root's only for a user who isn't root. Pure: the tests pass made-up
253
+ * processes.
254
+ */
255
+ export declare function processKind(owner: number, args: string, me: number | undefined): "daemon" | "root" | "other";
256
+ /**
257
+ * Whether `pid` is this user's daemon: a process of the user's running the
258
+ * worker's script. "root": the worker's script running as root, for a user
259
+ * who isn't (engram#680), which the user can't signal. "unknown" when that
260
+ * can't be found out (`ps` or PowerShell failed, or Windows won't say, as
261
+ * for a process run as administrator), which is never taken for the
262
+ * daemon. A daemon started with `engram start --foreground` runs the CLI's
263
+ * script, and is found through its socket only.
264
+ */
265
+ export declare function identifyProcess(pid: number): "daemon" | "root" | "other" | "unknown";
266
+ /**
267
+ * Every process that runs the worker's script as root, when this user
268
+ * isn't root (engram#680), whatever started it: on macOS, `ps`; on Linux,
269
+ * /proc. None on Windows, or when they can't be listed. Only ever said, in
270
+ * `engram status` and `engram start`: nothing ties one of these to this
271
+ * user, as the PID file or launchd's system domain does.
272
+ */
273
+ export declare function rootWorkerPids(): number[];
274
+ /**
275
+ * What identifies a process on Windows, one PowerShell statement each: its
276
+ * owner, the owner of this PowerShell, which runs as the user, and its
277
+ * command line, as one line of JSON; or "none" when there is no such
278
+ * process. The PID comes in ENGRAM_IDENTIFY_PID, never in the script.
279
+ *
280
+ * The command line comes as its character codes, joined by commas.
281
+ * PowerShell writes to a pipe in the console's code page, which turns what
282
+ * it can't show into something else: on a Japanese system, the second byte
283
+ * of a character can be a backslash, and Windows' "best fit" turns a
284
+ * full-width quote into a plain one. Either would break the JSON, or change
285
+ * what it says. Character codes are plain ASCII, whatever the code page.
286
+ *
287
+ * Cmdlets, operators and casts to plain types only, no .NET calls, so it
288
+ * runs the same where PowerShell is locked down to Constrained Language
289
+ * (AppLocker, WDAC).
290
+ */
291
+ export declare const IDENTIFY_STATEMENTS: readonly string[];
292
+ /** How to run IDENTIFY_STATEMENTS for `pid`. */
293
+ export declare function windowsIdentifyCommand(pid: number, systemRoot?: string): {
294
+ file: string;
295
+ args: string[];
296
+ env: Record<string, string>;
297
+ };
298
+ /**
299
+ * What IDENTIFY_STATEMENTS printed, read: "other" for no such process, or
300
+ * another user's; "daemon" for one of the user's running the worker's
301
+ * script; "unknown" when it says too little, as for a process run as
302
+ * administrator, whose owner or command line Windows doesn't give a process
303
+ * that isn't.
304
+ */
305
+ export declare function windowsIdentity(out: string): "daemon" | "other" | "unknown";
306
+ /**
307
+ * Where `engram stop` asks the daemon to stop on Windows (engram#613).
308
+ * There, process.kill is TerminateProcess: the daemon used to be ended
309
+ * wherever it was, in the middle of a send or of writing its queue. The
310
+ * socket stays one-way, so the request is a file in ~/.engram, which only
311
+ * the user can write: `{"pid": …, "id": …}`, the PID and the id the
312
+ * daemon's answer gave. The daemon looks for it every STOP_POLL_MS and
313
+ * stops as it does for SIGTERM. A daemon the request doesn't name, by both,
314
+ * leaves it alone: a file left from an earlier daemon can name a PID that
315
+ * Windows has given to this one since, but never its id.
316
+ *
317
+ * The supervisor that starts the daemon again after a crash reads it too:
318
+ * a daemon that exits with an error after a request was written was
319
+ * stopped, and isn't started again (see supervise.ts).
320
+ */
321
+ export declare const STOP_FILE: string;
322
+ export interface StopRequest {
323
+ /** The daemon it is for: null when it names none, as from an `engram stop` that found none running. */
324
+ pid: number | null;
325
+ id: string | null;
326
+ /** Which write of the file this is. Every write is a new file (see writePrivateFileSync), so it differs from the last. */
327
+ stamp: string;
328
+ }
329
+ /**
330
+ * The stop request in STOP_FILE, or null when there is none. It must be a
331
+ * small regular file, read without waiting (see readSmallFileSync): a FIFO
332
+ * put there would otherwise hang the daemon on the read.
333
+ */
334
+ export declare function readStopFile(file?: string): StopRequest | null;
335
+ /** Remove STOP_FILE when the request in it is one `matches` takes. */
336
+ export declare function removeStopFileIf(matches: (request: StopRequest) => boolean, file?: string): void;
337
+ export type Claim =
338
+ /** This process is the daemon. */
339
+ {
340
+ state: "held";
341
+ }
342
+ /** Another daemon runs (message), and this one should exit. `retry`: exit as failed, so launchd tries again. */
343
+ | {
344
+ state: "taken";
345
+ message: string;
346
+ retry: boolean;
347
+ }
348
+ /** No lock can be had here (reason): the daemon runs without one, as before engram#612. */
349
+ | {
350
+ state: "unavailable";
351
+ reason: string;
352
+ }
353
+ /**
354
+ * Asked to stop (SIGTERM, SIGINT, SIGHUP) before the lock was decided: it
355
+ * has let go of what it held, and should exit 0 (engram#669).
356
+ */
357
+ | {
358
+ state: "stopped";
359
+ }
360
+ /**
361
+ * Running as root for another user (engram#680), or in a ~/.engram its
362
+ * permissions keep the user out of (engram#723): never the daemon. It
363
+ * took nothing, and should exit 0, so launchd doesn't start it again.
364
+ */
365
+ | {
366
+ state: "refused";
367
+ message: string;
368
+ };
369
+ /**
370
+ * What SIGTERM, SIGINT and SIGHUP do (and SIGBREAK, and a stop request, on
371
+ * Windows): the worker sets its shutdown here once it has one.
372
+ */
373
+ export declare function setStopHandler(fn: () => void): void;
374
+ /** What happens when another daemon has taken the socket over. */
375
+ export declare function setDisplacedHandler(fn: () => void): void;
376
+ /** Answer as ready: modules loaded, capture set up. `engram start` waits for this (waitForWorker). */
377
+ export declare function markReady(): void;
378
+ /** Answer as stopping, so a start waits for this one to finish. */
379
+ export declare function markStopping(): void;
380
+ /**
381
+ * Answer whether it has a vault key (engram#698), once it has read it: see
382
+ * DaemonInfo.vaultKey. It never goes back to none: a key read before is
383
+ * used until a restart, and one that can't be read holds sends.
384
+ */
385
+ export declare function markVaultKey(has: boolean): void;
386
+ /**
387
+ * Every SELF_CHECK_MS: the socket is still this daemon's, and the PID file
388
+ * still names it. A socket removed from under it (a cleanup of ~/.engram)
389
+ * is bound again. One another daemon has bound in its place, and that
390
+ * answers as ready, means that daemon has taken over, and this one stops.
391
+ * Exported for the tests.
392
+ */
393
+ export declare function checkDaemonLock(): Promise<void>;
394
+ /**
395
+ * Stop when STOP_FILE asks this daemon to, by its PID and its id. Every
396
+ * STOP_POLL_MS while it holds the lock, on Windows. Exported for the tests.
397
+ */
398
+ export declare function checkStopRequest(): void;
399
+ /**
400
+ * Become the daemon, or find that one already runs: worker.ts calls this
401
+ * before the preflight, so two starts never run `npm rebuild` at once, and
402
+ * worker-main.ts again for a plist from before 0.4.2 that starts it
403
+ * directly (engram#469). Only the first call does anything. Logs what it
404
+ * found; the caller exits on "taken".
405
+ *
406
+ * Held, the PID file is written, SIGTERM, SIGINT and SIGHUP stop the
407
+ * daemon through setStopHandler, and any exit releases both. On Windows a
408
+ * stop request does too (STOP_FILE), and so does SIGBREAK. One of those
409
+ * signals that comes before the lock is decided stops it once it is: the
410
+ * claim is then "stopped" (engram#669).
411
+ */
412
+ export declare function claimDaemonLock(options: ClaimOptions): Promise<Claim>;
413
+ export interface ClaimOptions {
414
+ log: (msg: string) => void;
415
+ endpoint?: EndpointOrReason;
416
+ settleMs?: number;
417
+ /** Look for stop requests in STOP_FILE: on Windows unless set. The tests set it elsewhere. */
418
+ stopFile?: boolean;
419
+ }
420
+ /**
421
+ * Let the lock go: the PID file first, when it names this process, so a
422
+ * start that finds the socket gone doesn't then find this process, alive
423
+ * for a moment more, named as the daemon. Then the socket. Safe to call
424
+ * more than once, and on every exit.
425
+ */
426
+ export declare function releaseDaemonLock(): void;
427
+ //# sourceMappingURL=instance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instance.d.ts","sourceRoot":"","sources":["../../src/daemon/instance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAMH,OAAO,EAAyB,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AAK9D,OAAO,EAAa,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAOzD,sEAAsE;AACtE,eAAO,MAAM,OAAO,EAAE,MAOlB,CAAC;AAYL;;;GAGG;AACH,eAAO,MAAM,gBAAgB,QAAS,CAAC;AAgDvC,MAAM,WAAW,QAAQ;IACvB,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,OAAO,CAAC;CACf;AAED,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AAOlE;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,GAAE,MAAmB,EAAE,QAAQ,GAAE,MAAM,CAAC,QAA2B,GAAG,gBAAgB,CAYvH;AAQD,yCAAyC;AACzC,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,2DAA2D;IAC3D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,+EAA+E;IAC/E,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,oFAAoF;IACpF,KAAK,EAAE,OAAO,CAAC;IACf,2DAA2D;IAC3D,QAAQ,EAAE,OAAO,CAAC;IAClB,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