@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.
- package/LICENSE +112 -0
- package/README.md +1 -1
- package/dist/commands/auth.d.ts.map +1 -1
- package/dist/commands/auth.js +38 -11
- package/dist/commands/auth.js.map +1 -1
- package/dist/commands/conversations.d.ts.map +1 -1
- package/dist/commands/conversations.js +8 -2
- package/dist/commands/conversations.js.map +1 -1
- package/dist/commands/exclude.d.ts.map +1 -1
- package/dist/commands/exclude.js +73 -21
- package/dist/commands/exclude.js.map +1 -1
- package/dist/commands/import.d.ts +30 -3
- package/dist/commands/import.d.ts.map +1 -1
- package/dist/commands/import.js +374 -45
- package/dist/commands/import.js.map +1 -1
- package/dist/commands/login.d.ts.map +1 -1
- package/dist/commands/login.js +144 -29
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/search.js +2 -2
- package/dist/commands/search.js.map +1 -1
- package/dist/commands/status.d.ts +16 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/status.js +51 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/store.d.ts.map +1 -1
- package/dist/commands/store.js +19 -3
- package/dist/commands/store.js.map +1 -1
- package/dist/commands/upgrade.d.ts +24 -0
- package/dist/commands/upgrade.d.ts.map +1 -1
- package/dist/commands/upgrade.js +64 -15
- package/dist/commands/upgrade.js.map +1 -1
- package/dist/commands/usage.d.ts +44 -0
- package/dist/commands/usage.d.ts.map +1 -1
- package/dist/commands/usage.js +79 -19
- package/dist/commands/usage.js.map +1 -1
- package/dist/commands/vault.d.ts +11 -2
- package/dist/commands/vault.d.ts.map +1 -1
- package/dist/commands/vault.js +372 -53
- package/dist/commands/vault.js.map +1 -1
- package/dist/config-file.d.ts +74 -0
- package/dist/config-file.d.ts.map +1 -0
- package/dist/config-file.js +152 -0
- package/dist/config-file.js.map +1 -0
- package/dist/config-guard.d.ts +96 -0
- package/dist/config-guard.d.ts.map +1 -0
- package/dist/config-guard.js +184 -0
- package/dist/config-guard.js.map +1 -0
- package/dist/config.d.ts +48 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +101 -12
- package/dist/config.js.map +1 -1
- package/dist/daemon/as-root.d.ts +46 -0
- package/dist/daemon/as-root.d.ts.map +1 -0
- package/dist/daemon/as-root.js +64 -0
- package/dist/daemon/as-root.js.map +1 -0
- package/dist/daemon/backoff.d.ts +26 -0
- package/dist/daemon/backoff.d.ts.map +1 -0
- package/dist/daemon/backoff.js +37 -0
- package/dist/daemon/backoff.js.map +1 -0
- package/dist/daemon/codex-parser.d.ts +38 -4
- package/dist/daemon/codex-parser.d.ts.map +1 -1
- package/dist/daemon/codex-parser.js +111 -23
- package/dist/daemon/codex-parser.js.map +1 -1
- package/dist/daemon/command-path.d.ts +13 -0
- package/dist/daemon/command-path.d.ts.map +1 -0
- package/dist/daemon/command-path.js +35 -0
- package/dist/daemon/command-path.js.map +1 -0
- package/dist/daemon/commands.d.ts +153 -3
- package/dist/daemon/commands.d.ts.map +1 -1
- package/dist/daemon/commands.js +2775 -235
- package/dist/daemon/commands.js.map +1 -1
- package/dist/daemon/db.d.ts +255 -27
- package/dist/daemon/db.d.ts.map +1 -1
- package/dist/daemon/db.js +749 -53
- package/dist/daemon/db.js.map +1 -1
- package/dist/daemon/exclusions.d.ts +23 -3
- package/dist/daemon/exclusions.d.ts.map +1 -1
- package/dist/daemon/exclusions.js +91 -21
- package/dist/daemon/exclusions.js.map +1 -1
- package/dist/daemon/homebrew.d.ts +26 -0
- package/dist/daemon/homebrew.d.ts.map +1 -0
- package/dist/daemon/homebrew.js +43 -0
- package/dist/daemon/homebrew.js.map +1 -0
- package/dist/daemon/instance.d.ts +427 -0
- package/dist/daemon/instance.d.ts.map +1 -0
- package/dist/daemon/instance.js +1201 -0
- package/dist/daemon/instance.js.map +1 -0
- package/dist/daemon/key-source.d.ts +41 -0
- package/dist/daemon/key-source.d.ts.map +1 -0
- package/dist/daemon/key-source.js +89 -0
- package/dist/daemon/key-source.js.map +1 -0
- package/dist/daemon/launch.d.ts +56 -0
- package/dist/daemon/launch.d.ts.map +1 -0
- package/dist/daemon/launch.js +153 -0
- package/dist/daemon/launch.js.map +1 -0
- package/dist/daemon/launchd.d.ts +215 -0
- package/dist/daemon/launchd.d.ts.map +1 -0
- package/dist/daemon/launchd.js +527 -0
- package/dist/daemon/launchd.js.map +1 -0
- package/dist/daemon/login-start.d.ts +2 -0
- package/dist/daemon/login-start.d.ts.map +1 -0
- package/dist/daemon/login-start.js +25 -0
- package/dist/daemon/login-start.js.map +1 -0
- package/dist/daemon/ownership.d.ts +123 -0
- package/dist/daemon/ownership.d.ts.map +1 -0
- package/dist/daemon/ownership.js +245 -0
- package/dist/daemon/ownership.js.map +1 -0
- package/dist/daemon/package-runner.d.ts +28 -0
- package/dist/daemon/package-runner.d.ts.map +1 -0
- package/dist/daemon/package-runner.js +113 -0
- package/dist/daemon/package-runner.js.map +1 -0
- package/dist/daemon/parser.d.ts +50 -5
- package/dist/daemon/parser.d.ts.map +1 -1
- package/dist/daemon/parser.js +224 -47
- package/dist/daemon/parser.js.map +1 -1
- package/dist/daemon/powershell.d.ts +25 -0
- package/dist/daemon/powershell.d.ts.map +1 -0
- package/dist/daemon/powershell.js +30 -0
- package/dist/daemon/powershell.js.map +1 -0
- package/dist/daemon/preflight.d.ts +0 -17
- package/dist/daemon/preflight.d.ts.map +1 -1
- package/dist/daemon/preflight.js +32 -6
- package/dist/daemon/preflight.js.map +1 -1
- package/dist/daemon/prune.d.ts +101 -0
- package/dist/daemon/prune.d.ts.map +1 -0
- package/dist/daemon/prune.js +287 -0
- package/dist/daemon/prune.js.map +1 -0
- package/dist/daemon/queue-error.d.ts +47 -0
- package/dist/daemon/queue-error.d.ts.map +1 -0
- package/dist/daemon/queue-error.js +59 -0
- package/dist/daemon/queue-error.js.map +1 -0
- package/dist/daemon/root-daemon.d.ts +60 -0
- package/dist/daemon/root-daemon.d.ts.map +1 -0
- package/dist/daemon/root-daemon.js +86 -0
- package/dist/daemon/root-daemon.js.map +1 -0
- package/dist/daemon/scoop.d.ts +52 -0
- package/dist/daemon/scoop.d.ts.map +1 -0
- package/dist/daemon/scoop.js +84 -0
- package/dist/daemon/scoop.js.map +1 -0
- package/dist/daemon/scrubber.d.ts +456 -0
- package/dist/daemon/scrubber.d.ts.map +1 -0
- package/dist/daemon/scrubber.js +994 -0
- package/dist/daemon/scrubber.js.map +1 -0
- package/dist/daemon/status.d.ts +306 -11
- package/dist/daemon/status.d.ts.map +1 -1
- package/dist/daemon/status.js +568 -43
- package/dist/daemon/status.js.map +1 -1
- package/dist/daemon/supervise.d.ts +78 -0
- package/dist/daemon/supervise.d.ts.map +1 -0
- package/dist/daemon/supervise.js +121 -0
- package/dist/daemon/supervise.js.map +1 -0
- package/dist/daemon/supervisor.d.ts +18 -0
- package/dist/daemon/supervisor.d.ts.map +1 -0
- package/dist/daemon/supervisor.js +56 -0
- package/dist/daemon/supervisor.js.map +1 -0
- package/dist/daemon/syncer.d.ts +382 -21
- package/dist/daemon/syncer.d.ts.map +1 -1
- package/dist/daemon/syncer.js +1448 -178
- package/dist/daemon/syncer.js.map +1 -1
- package/dist/daemon/systemd.d.ts +109 -0
- package/dist/daemon/systemd.d.ts.map +1 -0
- package/dist/daemon/systemd.js +363 -0
- package/dist/daemon/systemd.js.map +1 -0
- package/dist/daemon/types.d.ts +58 -6
- package/dist/daemon/types.d.ts.map +1 -1
- package/dist/daemon/vault-changed.d.ts +29 -0
- package/dist/daemon/vault-changed.d.ts.map +1 -0
- package/dist/daemon/vault-changed.js +47 -0
- package/dist/daemon/vault-changed.js.map +1 -0
- package/dist/daemon/vault-key-missing.d.ts +70 -0
- package/dist/daemon/vault-key-missing.d.ts.map +1 -0
- package/dist/daemon/vault-key-missing.js +125 -0
- package/dist/daemon/vault-key-missing.js.map +1 -0
- package/dist/daemon/watcher.d.ts +87 -4
- package/dist/daemon/watcher.d.ts.map +1 -1
- package/dist/daemon/watcher.js +286 -36
- package/dist/daemon/watcher.js.map +1 -1
- package/dist/daemon/worker-main.js +363 -51
- package/dist/daemon/worker-main.js.map +1 -1
- package/dist/daemon/worker.js +19 -1
- package/dist/daemon/worker.js.map +1 -1
- package/dist/import-resume.d.ts +163 -0
- package/dist/import-resume.d.ts.map +1 -0
- package/dist/import-resume.js +267 -0
- package/dist/import-resume.js.map +1 -0
- package/dist/index.js +242 -33
- package/dist/index.js.map +1 -1
- package/dist/message-bounds.d.ts +52 -0
- package/dist/message-bounds.d.ts.map +1 -0
- package/dist/message-bounds.js +157 -0
- package/dist/message-bounds.js.map +1 -0
- package/dist/output.d.ts +14 -0
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +33 -5
- package/dist/output.js.map +1 -1
- package/dist/private-files.d.ts +126 -0
- package/dist/private-files.d.ts.map +1 -0
- package/dist/private-files.js +372 -0
- package/dist/private-files.js.map +1 -0
- package/dist/server-url.d.ts +31 -0
- package/dist/server-url.d.ts.map +1 -0
- package/dist/server-url.js +47 -0
- package/dist/server-url.js.map +1 -0
- package/dist/vault-key.d.ts +122 -0
- package/dist/vault-key.d.ts.map +1 -0
- package/dist/vault-key.js +273 -0
- package/dist/vault-key.js.map +1 -0
- package/package.json +11 -12
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clears sent messages out of the local queue (engram#598).
|
|
3
|
+
*
|
|
4
|
+
* daemon.db holds what the watcher reads until the server has it. When the
|
|
5
|
+
* server confirms it has stored a batch, the batch's rows are marked sent,
|
|
6
|
+
* and until now they stayed: the file kept a copy of every message ever
|
|
7
|
+
* captured, growing without end. Nothing reads a sent row again. The
|
|
8
|
+
* transcript it was read from is still where it was, and the server has it.
|
|
9
|
+
*
|
|
10
|
+
* Once an hour (the first time a couple of minutes after the daemon
|
|
11
|
+
* starts), rows sent more than SENT_RETENTION_DAYS days ago are deleted, a
|
|
12
|
+
* few hundred at a time with pauses in between, so capture and sending
|
|
13
|
+
* carry on as usual. A row that hasn't been sent is never deleted, however
|
|
14
|
+
* old it is. The days in between leave time to look into a problem with
|
|
15
|
+
* the local queue while its rows are still there.
|
|
16
|
+
*
|
|
17
|
+
* What is deleted leaves the file as well. secure_delete (see db.ts)
|
|
18
|
+
* writes zeros over it, the space it took goes back to the disk, and each
|
|
19
|
+
* pass ends by emptying the write-ahead log. A daemon.db made before this
|
|
20
|
+
* is rewritten once (DaemonDb.compact), when the disks it writes to have
|
|
21
|
+
* room for that, which also clears what earlier deletes left in its free
|
|
22
|
+
* pages.
|
|
23
|
+
*/
|
|
24
|
+
import type { DaemonDb } from "./db.js";
|
|
25
|
+
/** How long a sent message stays in the local queue. */
|
|
26
|
+
export declare const SENT_RETENTION_DAYS = 30;
|
|
27
|
+
export declare const SENT_RETENTION_MS: number;
|
|
28
|
+
/** The first pass waits this long after the daemon starts, past the busy
|
|
29
|
+
* first reads of the transcripts. */
|
|
30
|
+
export declare const FIRST_PASS_MS: number;
|
|
31
|
+
/** Then a pass every hour. */
|
|
32
|
+
export declare const PASS_INTERVAL_MS: number;
|
|
33
|
+
/** Rows deleted in one transaction. */
|
|
34
|
+
export declare const PRUNE_BATCH = 200;
|
|
35
|
+
/** Free pages given back to the disk in one step (8 MB of 4 KB pages). */
|
|
36
|
+
export declare const RELEASE_STEP_PAGES = 2048;
|
|
37
|
+
/** The pause between batches and steps, so the queue isn't held for long. */
|
|
38
|
+
export declare const STEP_PAUSE_MS = 50;
|
|
39
|
+
/** Compacting that was skipped or failed is tried again a day later. */
|
|
40
|
+
export declare const COMPACT_RETRY_MS: number;
|
|
41
|
+
/** Compacting writes a copy of the live data to a temporary file and
|
|
42
|
+
* again to the write-ahead log. It starts only when each disk it writes
|
|
43
|
+
* to has three times that free, and this much more. */
|
|
44
|
+
export declare const COMPACT_SPARE_BYTES: number;
|
|
45
|
+
/**
|
|
46
|
+
* The directory SQLite writes the temporary copy a VACUUM makes to: the
|
|
47
|
+
* first of these it can use, picked as SQLite itself picks it (os_unix.c,
|
|
48
|
+
* unixTempFileDir; on Windows, GetTempPathW's order). Null when none is.
|
|
49
|
+
*/
|
|
50
|
+
export declare function sqliteTempDir(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): string | null;
|
|
51
|
+
/** Bytes free to this user on the disk that holds `path`; null when that
|
|
52
|
+
* can't be told (fs.statfsSync came in Node 18.15). */
|
|
53
|
+
export declare function freeBytesAt(path: string): number | null;
|
|
54
|
+
export interface PrunerOptions {
|
|
55
|
+
/** Writes a line to the daemon log. */
|
|
56
|
+
log: (msg: string) => void;
|
|
57
|
+
now?: () => number;
|
|
58
|
+
pause?: (ms: number) => Promise<void>;
|
|
59
|
+
freeBytes?: (path: string) => number | null;
|
|
60
|
+
tempDir?: () => string | null;
|
|
61
|
+
}
|
|
62
|
+
export declare class Pruner {
|
|
63
|
+
private readonly db;
|
|
64
|
+
private readonly dbPath;
|
|
65
|
+
private readonly log;
|
|
66
|
+
private readonly now;
|
|
67
|
+
private readonly pause;
|
|
68
|
+
private readonly freeBytes;
|
|
69
|
+
private readonly tempDir;
|
|
70
|
+
private timer;
|
|
71
|
+
private running;
|
|
72
|
+
private stopped;
|
|
73
|
+
/** Compacting isn't tried again before this time. */
|
|
74
|
+
private compactAfter;
|
|
75
|
+
/** The last problem logged: the same one again isn't logged every hour. */
|
|
76
|
+
private lastProblem;
|
|
77
|
+
private problemThisPass;
|
|
78
|
+
constructor(db: DaemonDb, dbPath: string, opts: PrunerOptions);
|
|
79
|
+
start(): void;
|
|
80
|
+
/** No pass starts after this, and a pass in progress stops at its next
|
|
81
|
+
* step without touching the database again: close it after this. */
|
|
82
|
+
stop(): void;
|
|
83
|
+
/** Settles when no pass is running. */
|
|
84
|
+
idle(): Promise<void>;
|
|
85
|
+
private schedule;
|
|
86
|
+
/** One pass: delete the rows that are due, give the space they took
|
|
87
|
+
* back to the disk, and empty the write-ahead log. Never throws. */
|
|
88
|
+
pass(): Promise<void>;
|
|
89
|
+
/** Deletes the sent rows that are due, PRUNE_BATCH at a time, and
|
|
90
|
+
* returns how many went. */
|
|
91
|
+
private prune;
|
|
92
|
+
/** Gives the space that deleted rows took back to the disk. */
|
|
93
|
+
private reclaim;
|
|
94
|
+
private release;
|
|
95
|
+
/** Rewrites a daemon.db made before engram#598, once, when the disks it
|
|
96
|
+
* writes to have room. Tried at most once a day. */
|
|
97
|
+
private compact;
|
|
98
|
+
/** Logs a problem, unless it's the one logged last. */
|
|
99
|
+
private problem;
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=prune.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../../src/daemon/prune.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAKH,OAAO,KAAK,EAAE,QAAQ,EAAa,MAAM,SAAS,CAAC;AAKnD,wDAAwD;AACxD,eAAO,MAAM,mBAAmB,KAAK,CAAC;AACtC,eAAO,MAAM,iBAAiB,QAA+B,CAAC;AAE9D;sCACsC;AACtC,eAAO,MAAM,aAAa,QAAgB,CAAC;AAC3C,8BAA8B;AAC9B,eAAO,MAAM,gBAAgB,QAAiB,CAAC;AAE/C,uCAAuC;AACvC,eAAO,MAAM,WAAW,MAAM,CAAC;AAC/B,0EAA0E;AAC1E,eAAO,MAAM,kBAAkB,OAAO,CAAC;AACvC,6EAA6E;AAC7E,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,wEAAwE;AACxE,eAAO,MAAM,gBAAgB,QAAS,CAAC;AACvC;;wDAEwD;AACxD,eAAO,MAAM,mBAAmB,QAAmB,CAAC;AAEpD;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,GAAG,GAAE,MAAM,CAAC,UAAwB,EACpC,QAAQ,GAAE,MAAM,CAAC,QAA2B,GAC3C,MAAM,GAAG,IAAI,CAef;AAED;wDACwD;AACxD,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CASvD;AAUD,MAAM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;YAeP,OAAO;IAiBrB;yDACqD;IACrD,OAAO,CAAC,OAAO;IAkCf,uDAAuD;IACvD,OAAO,CAAC,OAAO;CAOhB"}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clears sent messages out of the local queue (engram#598).
|
|
3
|
+
*
|
|
4
|
+
* daemon.db holds what the watcher reads until the server has it. When the
|
|
5
|
+
* server confirms it has stored a batch, the batch's rows are marked sent,
|
|
6
|
+
* and until now they stayed: the file kept a copy of every message ever
|
|
7
|
+
* captured, growing without end. Nothing reads a sent row again. The
|
|
8
|
+
* transcript it was read from is still where it was, and the server has it.
|
|
9
|
+
*
|
|
10
|
+
* Once an hour (the first time a couple of minutes after the daemon
|
|
11
|
+
* starts), rows sent more than SENT_RETENTION_DAYS days ago are deleted, a
|
|
12
|
+
* few hundred at a time with pauses in between, so capture and sending
|
|
13
|
+
* carry on as usual. A row that hasn't been sent is never deleted, however
|
|
14
|
+
* old it is. The days in between leave time to look into a problem with
|
|
15
|
+
* the local queue while its rows are still there.
|
|
16
|
+
*
|
|
17
|
+
* What is deleted leaves the file as well. secure_delete (see db.ts)
|
|
18
|
+
* writes zeros over it, the space it took goes back to the disk, and each
|
|
19
|
+
* pass ends by emptying the write-ahead log. A daemon.db made before this
|
|
20
|
+
* is rewritten once (DaemonDb.compact), when the disks it writes to have
|
|
21
|
+
* room for that, which also clears what earlier deletes left in its free
|
|
22
|
+
* pages.
|
|
23
|
+
*/
|
|
24
|
+
import * as fs from "node:fs";
|
|
25
|
+
import { tmpdir } from "node:os";
|
|
26
|
+
import { dirname } from "node:path";
|
|
27
|
+
import { plural } from "../output.js";
|
|
28
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
29
|
+
/** How long a sent message stays in the local queue. */
|
|
30
|
+
export const SENT_RETENTION_DAYS = 30;
|
|
31
|
+
export const SENT_RETENTION_MS = SENT_RETENTION_DAYS * DAY_MS;
|
|
32
|
+
/** The first pass waits this long after the daemon starts, past the busy
|
|
33
|
+
* first reads of the transcripts. */
|
|
34
|
+
export const FIRST_PASS_MS = 2 * 60 * 1000;
|
|
35
|
+
/** Then a pass every hour. */
|
|
36
|
+
export const PASS_INTERVAL_MS = 60 * 60 * 1000;
|
|
37
|
+
/** Rows deleted in one transaction. */
|
|
38
|
+
export const PRUNE_BATCH = 200;
|
|
39
|
+
/** Free pages given back to the disk in one step (8 MB of 4 KB pages). */
|
|
40
|
+
export const RELEASE_STEP_PAGES = 2048;
|
|
41
|
+
/** The pause between batches and steps, so the queue isn't held for long. */
|
|
42
|
+
export const STEP_PAUSE_MS = 50;
|
|
43
|
+
/** Compacting that was skipped or failed is tried again a day later. */
|
|
44
|
+
export const COMPACT_RETRY_MS = DAY_MS;
|
|
45
|
+
/** Compacting writes a copy of the live data to a temporary file and
|
|
46
|
+
* again to the write-ahead log. It starts only when each disk it writes
|
|
47
|
+
* to has three times that free, and this much more. */
|
|
48
|
+
export const COMPACT_SPARE_BYTES = 64 * 1024 * 1024;
|
|
49
|
+
/**
|
|
50
|
+
* The directory SQLite writes the temporary copy a VACUUM makes to: the
|
|
51
|
+
* first of these it can use, picked as SQLite itself picks it (os_unix.c,
|
|
52
|
+
* unixTempFileDir; on Windows, GetTempPathW's order). Null when none is.
|
|
53
|
+
*/
|
|
54
|
+
export function sqliteTempDir(env = process.env, platform = process.platform) {
|
|
55
|
+
if (platform === "win32") {
|
|
56
|
+
return env.TMP || env.TEMP || env.USERPROFILE || tmpdir();
|
|
57
|
+
}
|
|
58
|
+
for (const dir of [env.SQLITE_TMPDIR, env.TMPDIR, "/var/tmp", "/usr/tmp", "/tmp", "."]) {
|
|
59
|
+
if (!dir)
|
|
60
|
+
continue;
|
|
61
|
+
try {
|
|
62
|
+
if (!fs.statSync(dir).isDirectory())
|
|
63
|
+
continue;
|
|
64
|
+
fs.accessSync(dir, fs.constants.W_OK | fs.constants.X_OK);
|
|
65
|
+
return dir;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Not usable: SQLite goes on to the next one.
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
/** Bytes free to this user on the disk that holds `path`; null when that
|
|
74
|
+
* can't be told (fs.statfsSync came in Node 18.15). */
|
|
75
|
+
export function freeBytesAt(path) {
|
|
76
|
+
const statfs = fs.statfsSync;
|
|
77
|
+
if (typeof statfs !== "function")
|
|
78
|
+
return null;
|
|
79
|
+
try {
|
|
80
|
+
const s = statfs(path);
|
|
81
|
+
return Number(s.bavail) * Number(s.bsize);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function errorText(err) {
|
|
88
|
+
return err instanceof Error ? err.message : String(err);
|
|
89
|
+
}
|
|
90
|
+
function mb(bytes) {
|
|
91
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
92
|
+
}
|
|
93
|
+
export class Pruner {
|
|
94
|
+
db;
|
|
95
|
+
dbPath;
|
|
96
|
+
log;
|
|
97
|
+
now;
|
|
98
|
+
pause;
|
|
99
|
+
freeBytes;
|
|
100
|
+
tempDir;
|
|
101
|
+
timer = null;
|
|
102
|
+
running = null;
|
|
103
|
+
stopped = false;
|
|
104
|
+
/** Compacting isn't tried again before this time. */
|
|
105
|
+
compactAfter = 0;
|
|
106
|
+
/** The last problem logged: the same one again isn't logged every hour. */
|
|
107
|
+
lastProblem = null;
|
|
108
|
+
problemThisPass = false;
|
|
109
|
+
constructor(db, dbPath, opts) {
|
|
110
|
+
this.db = db;
|
|
111
|
+
this.dbPath = dbPath;
|
|
112
|
+
this.log = opts.log;
|
|
113
|
+
this.now = opts.now ?? Date.now;
|
|
114
|
+
this.pause = opts.pause ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
115
|
+
this.freeBytes = opts.freeBytes ?? freeBytesAt;
|
|
116
|
+
this.tempDir = opts.tempDir ?? (() => sqliteTempDir());
|
|
117
|
+
}
|
|
118
|
+
start() {
|
|
119
|
+
if (this.stopped || this.timer || this.running)
|
|
120
|
+
return;
|
|
121
|
+
this.schedule(FIRST_PASS_MS);
|
|
122
|
+
}
|
|
123
|
+
/** No pass starts after this, and a pass in progress stops at its next
|
|
124
|
+
* step without touching the database again: close it after this. */
|
|
125
|
+
stop() {
|
|
126
|
+
this.stopped = true;
|
|
127
|
+
if (this.timer)
|
|
128
|
+
clearTimeout(this.timer);
|
|
129
|
+
this.timer = null;
|
|
130
|
+
}
|
|
131
|
+
/** Settles when no pass is running. */
|
|
132
|
+
idle() {
|
|
133
|
+
return this.running ?? Promise.resolve();
|
|
134
|
+
}
|
|
135
|
+
schedule(ms) {
|
|
136
|
+
if (this.stopped)
|
|
137
|
+
return;
|
|
138
|
+
const timer = setTimeout(() => {
|
|
139
|
+
this.timer = null;
|
|
140
|
+
this.running = this.pass()
|
|
141
|
+
.catch((err) => {
|
|
142
|
+
// pass() handles its own errors. Anything else (a log line that
|
|
143
|
+
// can't be written to a full disk) mustn't stop the daemon: an
|
|
144
|
+
// unhandled rejection would.
|
|
145
|
+
try {
|
|
146
|
+
this.log(`clearing sent messages failed: ${errorText(err)}`);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// Nowhere to say it.
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
.finally(() => {
|
|
153
|
+
this.running = null;
|
|
154
|
+
this.schedule(PASS_INTERVAL_MS);
|
|
155
|
+
});
|
|
156
|
+
}, ms);
|
|
157
|
+
// The daemon's watchers keep it running; this timer alone shouldn't.
|
|
158
|
+
timer.unref?.();
|
|
159
|
+
this.timer = timer;
|
|
160
|
+
}
|
|
161
|
+
/** One pass: delete the rows that are due, give the space they took
|
|
162
|
+
* back to the disk, and empty the write-ahead log. Never throws. */
|
|
163
|
+
async pass() {
|
|
164
|
+
this.problemThisPass = false;
|
|
165
|
+
const removed = await this.prune();
|
|
166
|
+
if (removed > 0) {
|
|
167
|
+
this.log(`removed ${plural(removed, "message")} sent over ${SENT_RETENTION_DAYS} days ago from the local queue (the server keeps them)`);
|
|
168
|
+
}
|
|
169
|
+
if (!this.stopped)
|
|
170
|
+
await this.reclaim();
|
|
171
|
+
if (!this.stopped) {
|
|
172
|
+
try {
|
|
173
|
+
// Busy (another process reading daemon.db) is fine: the next one.
|
|
174
|
+
this.db.checkpoint();
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
this.problem("emptying daemon.db's write-ahead log failed", err);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (!this.problemThisPass)
|
|
181
|
+
this.lastProblem = null;
|
|
182
|
+
}
|
|
183
|
+
/** Deletes the sent rows that are due, PRUNE_BATCH at a time, and
|
|
184
|
+
* returns how many went. */
|
|
185
|
+
async prune() {
|
|
186
|
+
// Fixed for the pass: rows sent while it runs are never due, so it ends.
|
|
187
|
+
const before = new Date(this.now() - SENT_RETENTION_MS);
|
|
188
|
+
let removed = 0;
|
|
189
|
+
for (;;) {
|
|
190
|
+
if (this.stopped)
|
|
191
|
+
break;
|
|
192
|
+
let deleted;
|
|
193
|
+
try {
|
|
194
|
+
deleted = this.db.pruneSent(before, PRUNE_BATCH);
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
this.problem("removing sent messages from daemon.db failed", err);
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
removed += deleted;
|
|
201
|
+
if (deleted < PRUNE_BATCH)
|
|
202
|
+
break;
|
|
203
|
+
await this.pause(STEP_PAUSE_MS);
|
|
204
|
+
}
|
|
205
|
+
return removed;
|
|
206
|
+
}
|
|
207
|
+
/** Gives the space that deleted rows took back to the disk. */
|
|
208
|
+
async reclaim() {
|
|
209
|
+
let storage;
|
|
210
|
+
try {
|
|
211
|
+
storage = this.db.storage();
|
|
212
|
+
}
|
|
213
|
+
catch (err) {
|
|
214
|
+
this.problem("reading daemon.db's size failed", err);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (storage.incremental) {
|
|
218
|
+
await this.release(storage.freePages);
|
|
219
|
+
}
|
|
220
|
+
else if (this.now() >= this.compactAfter) {
|
|
221
|
+
this.compact(storage);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async release(freePages) {
|
|
225
|
+
let left = freePages;
|
|
226
|
+
while (left > 0) {
|
|
227
|
+
if (this.stopped)
|
|
228
|
+
return;
|
|
229
|
+
let released;
|
|
230
|
+
try {
|
|
231
|
+
released = this.db.releaseFreePages(RELEASE_STEP_PAGES);
|
|
232
|
+
}
|
|
233
|
+
catch (err) {
|
|
234
|
+
this.problem("giving daemon.db's free space back to the disk failed", err);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (released <= 0)
|
|
238
|
+
return;
|
|
239
|
+
left -= released;
|
|
240
|
+
if (left > 0)
|
|
241
|
+
await this.pause(STEP_PAUSE_MS);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/** Rewrites a daemon.db made before engram#598, once, when the disks it
|
|
245
|
+
* writes to have room. Tried at most once a day. */
|
|
246
|
+
compact(storage) {
|
|
247
|
+
this.compactAfter = this.now() + COMPACT_RETRY_MS;
|
|
248
|
+
const live = (storage.pages - storage.freePages) * storage.pageSize;
|
|
249
|
+
const needed = 3 * live + COMPACT_SPARE_BYTES;
|
|
250
|
+
for (const dir of [dirname(this.dbPath), this.tempDir()]) {
|
|
251
|
+
const free = dir === null ? null : this.freeBytes(dir);
|
|
252
|
+
if (free === null) {
|
|
253
|
+
this.problem(`not compacting daemon.db yet: couldn't tell how much space is free ${dir === null ? "for SQLite's temporary files" : `in ${dir}`}`);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (free < needed) {
|
|
257
|
+
this.problem(`not compacting daemon.db yet: it needs ${mb(needed)} free in ${dir}, which has ${mb(free)}`);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const size = storage.pages * storage.pageSize;
|
|
262
|
+
try {
|
|
263
|
+
this.db.compact();
|
|
264
|
+
}
|
|
265
|
+
catch (err) {
|
|
266
|
+
this.problem("compacting daemon.db failed (it is as it was)", err);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
try {
|
|
270
|
+
const after = this.db.storage();
|
|
271
|
+
this.log(`compacted daemon.db from ${mb(size)} to ${mb(after.pages * after.pageSize)}; space it frees goes back to the disk from now on`);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
this.log("compacted daemon.db; space it frees goes back to the disk from now on");
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** Logs a problem, unless it's the one logged last. */
|
|
278
|
+
problem(what, err) {
|
|
279
|
+
this.problemThisPass = true;
|
|
280
|
+
const text = err === undefined ? what : `${what}: ${errorText(err)}`;
|
|
281
|
+
if (text === this.lastProblem)
|
|
282
|
+
return;
|
|
283
|
+
this.lastProblem = text;
|
|
284
|
+
this.log(text);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
//# sourceMappingURL=prune.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prune.js","sourceRoot":"","sources":["../../src/daemon/prune.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEnC,wDAAwD;AACxD,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,mBAAmB,GAAG,MAAM,CAAC;AAE9D;sCACsC;AACtC,MAAM,CAAC,MAAM,aAAa,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;IACjB,CAAC;CACF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Messages read from a transcript could not be queued in daemon.db
|
|
3
|
+
* (engram#584): the disk is full, an I/O error, a locked or damaged
|
|
4
|
+
* database. Unlike a line the parser can't make sense of, this says nothing
|
|
5
|
+
* about the line itself: the same line queues fine once the disk has room.
|
|
6
|
+
* So the watcher stops at the line, keeps its place, and tries again later,
|
|
7
|
+
* where a bad line is skipped.
|
|
8
|
+
*
|
|
9
|
+
* It used to be logged and dropped: the daemon called the syncer as
|
|
10
|
+
* `onMessages(...).catch(log)`, the watcher had already moved on, and it
|
|
11
|
+
* saved the file's resume offset past the line. The message was never
|
|
12
|
+
* queued, and nothing ever read that line again.
|
|
13
|
+
*/
|
|
14
|
+
export declare class QueueWriteError extends Error {
|
|
15
|
+
/** What daemon.db said, e.g. "database or disk is full". */
|
|
16
|
+
readonly reason: string;
|
|
17
|
+
readonly kind: "queue" | "config";
|
|
18
|
+
constructor(cause: unknown);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* ~/.engram/config.json is there, but which folders it excludes from
|
|
22
|
+
* capture can't be told (engram#673): it can't be read, or isn't settings.
|
|
23
|
+
* Like a failed queue write, this says nothing about the line: the watcher
|
|
24
|
+
* holds the file at it and tries again later. Captured meanwhile, a
|
|
25
|
+
* session in an excluded folder would be queued and sent, and an
|
|
26
|
+
* exclusion can't be taken back once it has.
|
|
27
|
+
*
|
|
28
|
+
* `reason` names the problem and never quotes the file, which holds the
|
|
29
|
+
* API key: it reaches daemon.log, status.json and a notification.
|
|
30
|
+
*/
|
|
31
|
+
export declare class ExclusionsUnreadable extends Error {
|
|
32
|
+
/** What is wrong with the file, e.g. "~/.engram/config.json isn't valid JSON". */
|
|
33
|
+
readonly reason: string;
|
|
34
|
+
readonly kind: "queue" | "config";
|
|
35
|
+
constructor(reason: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Why the watcher holds a transcript at a line rather than read on: the
|
|
39
|
+
* line is fine, but it can't be captured safely yet. Anything else thrown
|
|
40
|
+
* while capturing a line is about the line, and it is skipped.
|
|
41
|
+
*/
|
|
42
|
+
export type CaptureHold = QueueWriteError | ExclusionsUnreadable;
|
|
43
|
+
/** True for an error the watcher holds a transcript for, rather than skip the line. */
|
|
44
|
+
export declare function isCaptureHold(err: unknown): err is CaptureHold;
|
|
45
|
+
/** Runs one daemon.db step of queuing, turning its failure into a QueueWriteError. */
|
|
46
|
+
export declare function queueing<T>(step: () => T): T;
|
|
47
|
+
//# sourceMappingURL=queue-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue-error.d.ts","sourceRoot":"","sources":["../../src/daemon/queue-error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC,4DAA4D;IAC5D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,QAAQ,CAAW;gBAEhC,KAAK,EAAE,OAAO;CAM3B;AAED;;;;;;;;;;GAUG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,kFAAkF;IAClF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,QAAQ,CAAY;gBAEjC,MAAM,EAAE,MAAM;CAK3B;AAED;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,eAAe,GAAG,oBAAoB,CAAC;AAEjE,uFAAuF;AACvF,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,WAAW,CAE9D;AAED,sFAAsF;AACtF,wBAAgB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,CAM5C"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Messages read from a transcript could not be queued in daemon.db
|
|
3
|
+
* (engram#584): the disk is full, an I/O error, a locked or damaged
|
|
4
|
+
* database. Unlike a line the parser can't make sense of, this says nothing
|
|
5
|
+
* about the line itself: the same line queues fine once the disk has room.
|
|
6
|
+
* So the watcher stops at the line, keeps its place, and tries again later,
|
|
7
|
+
* where a bad line is skipped.
|
|
8
|
+
*
|
|
9
|
+
* It used to be logged and dropped: the daemon called the syncer as
|
|
10
|
+
* `onMessages(...).catch(log)`, the watcher had already moved on, and it
|
|
11
|
+
* saved the file's resume offset past the line. The message was never
|
|
12
|
+
* queued, and nothing ever read that line again.
|
|
13
|
+
*/
|
|
14
|
+
export class QueueWriteError extends Error {
|
|
15
|
+
/** What daemon.db said, e.g. "database or disk is full". */
|
|
16
|
+
reason;
|
|
17
|
+
kind = "queue";
|
|
18
|
+
constructor(cause) {
|
|
19
|
+
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
20
|
+
super(`couldn't queue messages in daemon.db: ${reason}`, { cause });
|
|
21
|
+
this.name = "QueueWriteError";
|
|
22
|
+
this.reason = reason;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* ~/.engram/config.json is there, but which folders it excludes from
|
|
27
|
+
* capture can't be told (engram#673): it can't be read, or isn't settings.
|
|
28
|
+
* Like a failed queue write, this says nothing about the line: the watcher
|
|
29
|
+
* holds the file at it and tries again later. Captured meanwhile, a
|
|
30
|
+
* session in an excluded folder would be queued and sent, and an
|
|
31
|
+
* exclusion can't be taken back once it has.
|
|
32
|
+
*
|
|
33
|
+
* `reason` names the problem and never quotes the file, which holds the
|
|
34
|
+
* API key: it reaches daemon.log, status.json and a notification.
|
|
35
|
+
*/
|
|
36
|
+
export class ExclusionsUnreadable extends Error {
|
|
37
|
+
/** What is wrong with the file, e.g. "~/.engram/config.json isn't valid JSON". */
|
|
38
|
+
reason;
|
|
39
|
+
kind = "config";
|
|
40
|
+
constructor(reason) {
|
|
41
|
+
super(`can't tell which folders are excluded from capture: ${reason}`);
|
|
42
|
+
this.name = "ExclusionsUnreadable";
|
|
43
|
+
this.reason = reason;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** True for an error the watcher holds a transcript for, rather than skip the line. */
|
|
47
|
+
export function isCaptureHold(err) {
|
|
48
|
+
return err instanceof QueueWriteError || err instanceof ExclusionsUnreadable;
|
|
49
|
+
}
|
|
50
|
+
/** Runs one daemon.db step of queuing, turning its failure into a QueueWriteError. */
|
|
51
|
+
export function queueing(step) {
|
|
52
|
+
try {
|
|
53
|
+
return step();
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
throw isCaptureHold(err) ? err : new QueueWriteError(err);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=queue-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue-error.js","sourceRoot":"","sources":["../../src/daemon/queue-error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACxC,4DAA4D;IACnD,MAAM,CAAS;IACf,IAAI,GAAuB,OAAO,CAAC;IAE5C,YAAY,KAAc;QACxB,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACtE,KAAK,CAAC,yCAAyC,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACpE,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7C,kFAAkF;IACzE,MAAM,CAAS;IACf,IAAI,GAAuB,QAAQ,CAAC;IAE7C,YAAY,MAAc;QACxB,KAAK,CAAC,uDAAuD,MAAM,EAAE,CAAC,CAAC;QACvE,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AASD,uFAAuF;AACvF,MAAM,UAAU,aAAa,CAAC,GAAY;IACxC,OAAO,GAAG,YAAY,eAAe,IAAI,GAAG,YAAY,oBAAoB,CAAC;AAC/E,CAAC;AAED,sFAAsF;AACtF,MAAM,UAAU,QAAQ,CAAI,IAAa;IACvC,IAAI,CAAC;QACH,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,MAAM,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,eAAe,CAAC,GAAG,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* engram#680 — a daemon left running as root in the user's ~/.engram.
|
|
3
|
+
*
|
|
4
|
+
* Before engram#653, `sudo engram start` started the daemon as root, and
|
|
5
|
+
* `sudo engram install` loaded its launchd job into the system domain,
|
|
6
|
+
* where it runs as root. macOS keeps HOME under sudo, so that daemon
|
|
7
|
+
* captures into the user's ~/.engram and makes files there that their own
|
|
8
|
+
* daemon can't write. The user can't signal it: `engram stop` failed, and
|
|
9
|
+
* a CLI from before this change took its PID file for a dead daemon's and
|
|
10
|
+
* removed it, then started a second daemon beside it.
|
|
11
|
+
*
|
|
12
|
+
* Now its PID file is kept, and `engram status`, `start`, `stop` and
|
|
13
|
+
* `install` say how to stop it. While it runs, no other daemon is started:
|
|
14
|
+
* two would capture the same transcripts (see claimDaemonLock).
|
|
15
|
+
*/
|
|
16
|
+
import { type SystemJob } from "./launchd.js";
|
|
17
|
+
import type { ForeignFiles } from "./ownership.js";
|
|
18
|
+
/** A daemon running as root for this user, and Engram's job in launchd's system domain (macOS). */
|
|
19
|
+
export interface RootDaemon {
|
|
20
|
+
pid: number;
|
|
21
|
+
job: SystemJob | null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The daemon running as root for this user, when one runs: named in the
|
|
25
|
+
* PID file (`inFile`, when the caller has looked), or run by the job a
|
|
26
|
+
* sudo install loaded into launchd's system domain from the user's plist.
|
|
27
|
+
* `job`: that job, when the caller has looked. Never on Windows, which has
|
|
28
|
+
* no root, and where looking means asking PowerShell.
|
|
29
|
+
*/
|
|
30
|
+
export declare function findRootDaemon(inFile?: {
|
|
31
|
+
pid: number;
|
|
32
|
+
root: boolean;
|
|
33
|
+
} | null, job?: SystemJob | null): RootDaemon | null;
|
|
34
|
+
/**
|
|
35
|
+
* How to stop it. Through launchd when its job runs it: KeepAlive starts
|
|
36
|
+
* a daemon again after a crash, and unloading the job stops it for good.
|
|
37
|
+
*/
|
|
38
|
+
export declare function rootStopCommand(root: RootDaemon): string;
|
|
39
|
+
/** What to say of it: the problem, then what to run, in order. */
|
|
40
|
+
export declare function rootDaemonLines(root: RootDaemon): {
|
|
41
|
+
problem: string;
|
|
42
|
+
fixes: string[];
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Engram's job in launchd's system domain, loaded and running no daemon of
|
|
46
|
+
* the user's: KeepAlive or a kickstart would start one as root. Gone at the
|
|
47
|
+
* next restart: the system domain loads only /Library/LaunchDaemons.
|
|
48
|
+
*/
|
|
49
|
+
export declare function systemJobLeft(): string;
|
|
50
|
+
/**
|
|
51
|
+
* Workers running as root that neither the PID file nor launchd ties to
|
|
52
|
+
* this user, besides `known`: only said, never acted on. On macOS, where
|
|
53
|
+
* the daemon never runs as root, any. On Linux only when ~/.engram has
|
|
54
|
+
* files root owns, the mark of one in it: a daemon root runs for itself,
|
|
55
|
+
* in its own home, is no concern of the user's.
|
|
56
|
+
*/
|
|
57
|
+
export declare function untiedRootWorkers(known: readonly (number | null)[], foreign: ForeignFiles | null): number[];
|
|
58
|
+
/** What to say of untied root workers: one line, with the command that stops them. */
|
|
59
|
+
export declare function untiedRootLine(pids: readonly number[]): string;
|
|
60
|
+
//# sourceMappingURL=root-daemon.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"root-daemon.d.ts","sourceRoot":"","sources":["../../src/daemon/root-daemon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAgC,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAC5E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,mGAAmG;AACnG,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;CACvB;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,MAAM,CAAC,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAAG,IAAI,EAC9C,GAAG,CAAC,EAAE,SAAS,GAAG,IAAI,GACrB,UAAU,GAAG,IAAI,CAUnB;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAExD;AAED,kEAAkE;AAClE,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAOtF;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAEtC;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,YAAY,GAAG,IAAI,GAAG,MAAM,EAAE,CAO3G;AAED,sFAAsF;AACtF,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAK9D"}
|