@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
package/dist/daemon/status.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
2
3
|
import { homedir } from "node:os";
|
|
3
4
|
import { join } from "node:path";
|
|
5
|
+
import { writePrivateFileSync } from "../private-files.js";
|
|
6
|
+
import { plural } from "../output.js";
|
|
7
|
+
import { lockedDir, lockedDirFix, lockedDirReason } from "./ownership.js";
|
|
8
|
+
import { engramCommand } from "./package-runner.js";
|
|
9
|
+
import { powerShellArgs, windowsPowerShellPath } from "./powershell.js";
|
|
4
10
|
const STATUS_FILE = join(homedir(), ".engram", "status.json");
|
|
5
11
|
const DEFAULT_STATUS = {
|
|
6
12
|
health: "healthy",
|
|
@@ -11,10 +17,23 @@ const DEFAULT_STATUS = {
|
|
|
11
17
|
pending_messages: 0,
|
|
12
18
|
consecutive_failures: 0,
|
|
13
19
|
storage: null,
|
|
20
|
+
capture_error: null,
|
|
21
|
+
signed_out_at: null,
|
|
22
|
+
next_retry_at: null,
|
|
23
|
+
delete_refused: null,
|
|
24
|
+
vault_key: null,
|
|
25
|
+
vault_undecryptable: null,
|
|
26
|
+
vault_key_missing: null,
|
|
14
27
|
updated_at: new Date().toISOString(),
|
|
15
28
|
};
|
|
16
29
|
let currentStatus = { ...DEFAULT_STATUS };
|
|
17
30
|
let lastNotifiedError = null;
|
|
31
|
+
/** Transcripts this process can't queue right now (engram#584). */
|
|
32
|
+
const stalledFiles = new Set();
|
|
33
|
+
/** At most one capture notification per hour: a daemon.db that is locked
|
|
34
|
+
* now and then would otherwise raise one per episode. */
|
|
35
|
+
const CAPTURE_NOTIFY_INTERVAL_MS = 60 * 60_000;
|
|
36
|
+
let lastCaptureNotifyAt = -Infinity;
|
|
18
37
|
export function readStatus() {
|
|
19
38
|
try {
|
|
20
39
|
if (existsSync(STATUS_FILE)) {
|
|
@@ -35,7 +54,9 @@ export function readStatus() {
|
|
|
35
54
|
function writeStatus() {
|
|
36
55
|
currentStatus.updated_at = new Date().toISOString();
|
|
37
56
|
try {
|
|
38
|
-
|
|
57
|
+
// 0600, and replaced in one step (engram#627): a reader never sees
|
|
58
|
+
// half of it. Not flushed to disk: it is rewritten every few seconds.
|
|
59
|
+
writePrivateFileSync(STATUS_FILE, JSON.stringify(currentStatus, null, 2), { durable: false });
|
|
39
60
|
}
|
|
40
61
|
catch {
|
|
41
62
|
// best effort
|
|
@@ -51,28 +72,35 @@ export function recordSuccess(pendingMessages) {
|
|
|
51
72
|
currentStatus.pending_messages = pendingMessages;
|
|
52
73
|
currentStatus.consecutive_failures = 0;
|
|
53
74
|
currentStatus.storage = null;
|
|
75
|
+
currentStatus.next_retry_at = null;
|
|
54
76
|
lastNotifiedError = null;
|
|
55
77
|
writeStatus();
|
|
56
78
|
}
|
|
57
79
|
/**
|
|
58
|
-
* Record a sync failure. Logs once per error type and, for the
|
|
59
|
-
*
|
|
60
|
-
* on first occurrence — the daemon is headless, so a line in
|
|
61
|
-
* not "telling" anyone.
|
|
80
|
+
* Record a sync failure. Logs once per error type and, for the states the
|
|
81
|
+
* user must act on (auth, storage_full, vault_key, vault_mismatch), raises a desktop
|
|
82
|
+
* notification on first occurrence — the daemon is headless, so a line in
|
|
83
|
+
* daemon.log is not "telling" anyone.
|
|
62
84
|
*/
|
|
63
|
-
export function recordFailure(error, errorType, pendingMessages,
|
|
85
|
+
export function recordFailure(error, errorType, pendingMessages, details = {}) {
|
|
86
|
+
const { storage, retryAt } = details;
|
|
64
87
|
currentStatus.consecutive_failures++;
|
|
65
88
|
currentStatus.last_error = error;
|
|
66
89
|
currentStatus.last_error_at = new Date().toISOString();
|
|
67
90
|
currentStatus.error_type = errorType;
|
|
68
91
|
currentStatus.pending_messages = pendingMessages;
|
|
92
|
+
currentStatus.next_retry_at = retryAt == null ? null : new Date(retryAt).toISOString();
|
|
69
93
|
if (errorType === "storage_full" &&
|
|
70
94
|
typeof storage?.used === "number" &&
|
|
71
95
|
typeof storage?.limit === "number") {
|
|
72
96
|
currentStatus.storage = { used: storage.used, limit: storage.limit };
|
|
73
97
|
}
|
|
74
98
|
// Escalate health based on severity and consecutive failures
|
|
75
|
-
if (errorType === "auth" ||
|
|
99
|
+
if (errorType === "auth" ||
|
|
100
|
+
errorType === "billing" ||
|
|
101
|
+
errorType === "storage_full" ||
|
|
102
|
+
errorType === "vault_key" ||
|
|
103
|
+
errorType === "vault_mismatch") {
|
|
76
104
|
currentStatus.health = "error";
|
|
77
105
|
}
|
|
78
106
|
else if (currentStatus.consecutive_failures >= 5) {
|
|
@@ -86,38 +114,410 @@ export function recordFailure(error, errorType, pendingMessages, storage) {
|
|
|
86
114
|
if (errorType !== lastNotifiedError) {
|
|
87
115
|
lastNotifiedError = errorType;
|
|
88
116
|
logWarning(errorType, currentStatus.storage);
|
|
89
|
-
if (errorType === "auth" ||
|
|
90
|
-
|
|
117
|
+
if (errorType === "auth" ||
|
|
118
|
+
errorType === "storage_full" ||
|
|
119
|
+
errorType === "vault_key" ||
|
|
120
|
+
errorType === "vault_mismatch") {
|
|
121
|
+
const storage = currentStatus.storage;
|
|
122
|
+
notifyDesktop(errorType === "storage_full" ? "Engram memory is full" : "Engram needs attention", errorType === "storage_full" && storage
|
|
123
|
+
? `${n(storage.used)}/${n(storage.limit)} messages used. New captures are paused.`
|
|
124
|
+
: syncErrorSummary(errorType, storage).slice(0, 180));
|
|
91
125
|
}
|
|
92
126
|
}
|
|
93
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* When the daemon next tries what failed, between failures (engram#596):
|
|
130
|
+
* one conversation's wait can end, and another's go on, with no failure
|
|
131
|
+
* recorded in between. Written only when it changes.
|
|
132
|
+
*/
|
|
133
|
+
export function recordNextRetry(retryAt) {
|
|
134
|
+
const next = retryAt === null ? null : new Date(retryAt).toISOString();
|
|
135
|
+
if (next === currentStatus.next_retry_at)
|
|
136
|
+
return;
|
|
137
|
+
currentStatus.next_retry_at = next;
|
|
138
|
+
writeStatus();
|
|
139
|
+
}
|
|
140
|
+
/** Where the vault key in use came from (engram#693). Written only when it changes. */
|
|
141
|
+
export function recordVaultKey(source) {
|
|
142
|
+
if (currentStatus.vault_key === source)
|
|
143
|
+
return;
|
|
144
|
+
currentStatus.vault_key = source;
|
|
145
|
+
writeStatus();
|
|
146
|
+
}
|
|
147
|
+
/** This daemon has written vault_key_missing (engram#698). */
|
|
148
|
+
let vaultKeyMissingWritten = false;
|
|
149
|
+
/**
|
|
150
|
+
* How many secrets the account's vault holds, while the daemon has no vault
|
|
151
|
+
* key to remove their values with (engram#698); null when there is nothing
|
|
152
|
+
* to say. Written when it changes, and the first time even when it is null:
|
|
153
|
+
* a count the daemon before left is gone at once.
|
|
154
|
+
*/
|
|
155
|
+
export function recordVaultKeyMissing(secrets) {
|
|
156
|
+
const next = secrets !== null && Number.isSafeInteger(secrets) && secrets > 0 ? { secrets } : null;
|
|
157
|
+
if (vaultKeyMissingWritten && (currentStatus.vault_key_missing?.secrets ?? null) === (next?.secrets ?? null)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
vaultKeyMissingWritten = true;
|
|
161
|
+
currentStatus.vault_key_missing = next;
|
|
162
|
+
writeStatus();
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* What the last complete read of the vault found of secrets the key
|
|
166
|
+
* doesn't decrypt (engram#694); null when each decrypts. Written only when
|
|
167
|
+
* it changes.
|
|
168
|
+
*/
|
|
169
|
+
export function recordVaultUndecryptable(found) {
|
|
170
|
+
const prev = currentStatus.vault_undecryptable;
|
|
171
|
+
const next = found === null || found.secrets <= 0
|
|
172
|
+
? null
|
|
173
|
+
: { secrets: found.secrets, unscrubbed: found.unscrubbed, decrypted: found.decrypted, held: found.held };
|
|
174
|
+
if (prev === next ||
|
|
175
|
+
(prev &&
|
|
176
|
+
next &&
|
|
177
|
+
prev.secrets === next.secrets &&
|
|
178
|
+
prev.unscrubbed === next.unscrubbed &&
|
|
179
|
+
prev.decrypted === next.decrypted &&
|
|
180
|
+
prev.held === next.held)) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
currentStatus.vault_undecryptable = next;
|
|
184
|
+
writeStatus();
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* What the daemon before this one held sends for (engram#694), as its
|
|
188
|
+
* status.json says: the secrets its vault key didn't decrypt, while they
|
|
189
|
+
* wait. Null when nothing waited for them. Read before this daemon's first
|
|
190
|
+
* write replaces it.
|
|
191
|
+
*/
|
|
192
|
+
export function heldVaultMismatch() {
|
|
193
|
+
const found = readStatus().vault_undecryptable;
|
|
194
|
+
if (typeof found !== "object" || found === null)
|
|
195
|
+
return null;
|
|
196
|
+
const { secrets, unscrubbed, decrypted, held } = found;
|
|
197
|
+
if (held !== true)
|
|
198
|
+
return null;
|
|
199
|
+
const count = (n) => (typeof n === "number" && Number.isSafeInteger(n) && n > 0 ? n : 0);
|
|
200
|
+
// Held, at least one couldn't be scrubbed, whatever else it says.
|
|
201
|
+
const atLeastOne = Math.max(1, count(unscrubbed));
|
|
202
|
+
return { secrets: Math.max(atLeastOne, count(secrets)), unscrubbed: atLeastOne, decrypted: count(decrypted) };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The key in use can't delete conversations, and `deletes` opt-out deletes
|
|
206
|
+
* wait for one that can (engram#597). Written only when it changes; none
|
|
207
|
+
* waiting clears it.
|
|
208
|
+
*/
|
|
209
|
+
export function recordDeletesRefused(deletes) {
|
|
210
|
+
const prev = currentStatus.delete_refused;
|
|
211
|
+
if (deletes <= 0) {
|
|
212
|
+
if (!prev)
|
|
213
|
+
return;
|
|
214
|
+
currentStatus.delete_refused = null;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
if (prev?.deletes === deletes)
|
|
218
|
+
return;
|
|
219
|
+
currentStatus.delete_refused = { since: prev?.since ?? new Date().toISOString(), deletes };
|
|
220
|
+
}
|
|
221
|
+
writeStatus();
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* A transcript's new lines couldn't be queued in daemon.db (engram#584),
|
|
225
|
+
* or config.json couldn't say whether its folder is excluded (`kind`
|
|
226
|
+
* "config", engram#673); the watcher holds the file at the failing line
|
|
227
|
+
* and retries it. The first stall of an episode is logged as a warning and
|
|
228
|
+
* raised as a desktop notification: when the disk is full, daemon.log and
|
|
229
|
+
* status.json may not be writable either, and the notification still gets
|
|
230
|
+
* through.
|
|
231
|
+
*/
|
|
232
|
+
export function recordCaptureStall(filePath, message, kind = "queue") {
|
|
233
|
+
const first = stalledFiles.size === 0;
|
|
234
|
+
stalledFiles.add(filePath);
|
|
235
|
+
currentStatus.capture_error = {
|
|
236
|
+
message,
|
|
237
|
+
since: currentStatus.capture_error?.since ?? new Date().toISOString(),
|
|
238
|
+
files: stalledFiles.size,
|
|
239
|
+
...(kind === "config" ? { kind } : {}),
|
|
240
|
+
};
|
|
241
|
+
writeStatus();
|
|
242
|
+
if (!first)
|
|
243
|
+
return;
|
|
244
|
+
// A ~/.engram that can't be searched any more (engram#723): the file is
|
|
245
|
+
// fine, and the folder is what needs the fix. daemon.log, which the
|
|
246
|
+
// daemon opened before, still takes the warning; status.json, which is
|
|
247
|
+
// written anew, can't be, so the desktop is told too.
|
|
248
|
+
const locked = kind === "config" ? lockedDir() : null;
|
|
249
|
+
const summary = locked?.search
|
|
250
|
+
? lockedCaptureSummary(locked, currentStatus.capture_error.files)
|
|
251
|
+
: captureErrorSummary(currentStatus.capture_error);
|
|
252
|
+
console.error(`[engram] WARNING: ${summary}`);
|
|
253
|
+
console.error(`[engram] Run '${engramCommand("status")}' for details.`);
|
|
254
|
+
if (Date.now() - lastCaptureNotifyAt >= CAPTURE_NOTIFY_INTERVAL_MS) {
|
|
255
|
+
lastCaptureNotifyAt = Date.now();
|
|
256
|
+
if (kind === "config") {
|
|
257
|
+
if (locked?.search) {
|
|
258
|
+
notifyDesktop("Engram paused capture", `${lockedDirReason("opening the files")}, so capture is paused. ${lockedDirFix(locked)}. ` +
|
|
259
|
+
`Capture then resumes where it stopped.`);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
// The message is short and names the file; it isn't cut.
|
|
263
|
+
notifyDesktop("Engram paused capture", `${message}, so Engram can't tell which folders you excluded. Fix it, and ` +
|
|
264
|
+
`capture resumes where it stopped.`);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
// The reason is cut short, not the advice after it. The advice covers
|
|
268
|
+
// a locked or read-only daemon.db as well as a full disk.
|
|
269
|
+
const reason = message.length > 60 ? `${message.slice(0, 59)}…` : message;
|
|
270
|
+
notifyDesktop("Engram can't capture", `New messages can't be queued (${reason}). Free up disk space, or check ` +
|
|
271
|
+
`~/.engram/daemon.db. Capture resumes where it stopped.`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/** A stalled transcript was queued again, or there is nothing left to
|
|
275
|
+
* retry (the file is gone). The error clears once no file is waiting.
|
|
276
|
+
*
|
|
277
|
+
* `quiet`: the watcher holding the file stopped (a sign-out or a switch of
|
|
278
|
+
* account, engram#594), so the hold is dropped without claiming capture
|
|
279
|
+
* resumed. The lines were never queued; the next watcher reads them again
|
|
280
|
+
* from the saved offset, and a failure there is a new stall. */
|
|
281
|
+
export function recordCaptureResumed(filePath, options = {}) {
|
|
282
|
+
if (!stalledFiles.delete(filePath))
|
|
283
|
+
return;
|
|
284
|
+
if (stalledFiles.size > 0 && currentStatus.capture_error) {
|
|
285
|
+
currentStatus.capture_error = { ...currentStatus.capture_error, files: stalledFiles.size };
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
currentStatus.capture_error = null;
|
|
289
|
+
if (!options.quiet) {
|
|
290
|
+
console.error("[engram] capture resumed: new messages are being queued again");
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
writeStatus();
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* No API key is saved: capture is paused and nothing is sent (engram#594).
|
|
297
|
+
* Clears any sync error, since a rejected key's warning no longer applies
|
|
298
|
+
* once the key is gone, and the pending count, since nothing is due to go.
|
|
299
|
+
*/
|
|
300
|
+
export function recordSignedOut() {
|
|
301
|
+
clearSyncError();
|
|
302
|
+
currentStatus.delete_refused = null;
|
|
303
|
+
currentStatus.pending_messages = 0;
|
|
304
|
+
currentStatus.signed_out_at = currentStatus.signed_out_at ?? new Date().toISOString();
|
|
305
|
+
writeStatus();
|
|
306
|
+
}
|
|
307
|
+
/** A key is saved: capture and sync run. An error recorded under another
|
|
308
|
+
* key (or none) says nothing about this one, so it is cleared too, and so
|
|
309
|
+
* is another key's refusal to delete (engram#597). */
|
|
310
|
+
export function recordSignedIn() {
|
|
311
|
+
clearSyncError();
|
|
312
|
+
currentStatus.delete_refused = null;
|
|
313
|
+
currentStatus.signed_out_at = null;
|
|
314
|
+
writeStatus();
|
|
315
|
+
}
|
|
316
|
+
function clearSyncError() {
|
|
317
|
+
currentStatus.health = "healthy";
|
|
318
|
+
currentStatus.last_error = null;
|
|
319
|
+
currentStatus.last_error_at = null;
|
|
320
|
+
currentStatus.error_type = null;
|
|
321
|
+
currentStatus.consecutive_failures = 0;
|
|
322
|
+
currentStatus.storage = null;
|
|
323
|
+
currentStatus.next_retry_at = null;
|
|
324
|
+
lastNotifiedError = null;
|
|
325
|
+
}
|
|
94
326
|
/** Update pending count without changing health. */
|
|
95
327
|
export function updatePending(count) {
|
|
96
328
|
currentStatus.pending_messages = count;
|
|
97
329
|
writeStatus();
|
|
98
330
|
}
|
|
99
|
-
function
|
|
100
|
-
if (
|
|
331
|
+
function errorParts(err) {
|
|
332
|
+
if (err !== null && typeof err === "object") {
|
|
333
|
+
const e = err;
|
|
334
|
+
return {
|
|
335
|
+
name: typeof e.name === "string" ? e.name : "",
|
|
336
|
+
code: typeof e.code === "string" ? e.code : "",
|
|
337
|
+
status: typeof e.status === "number" ? e.status : undefined,
|
|
338
|
+
message: typeof e.message === "string" ? e.message : "",
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
return { name: "", code: "", status: undefined, message: String(err ?? "") };
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* The server's error code when the message carries its JSON body: "" for a
|
|
345
|
+
* body without one, null when there is no JSON body at all.
|
|
346
|
+
*/
|
|
347
|
+
function bodyCode(message) {
|
|
348
|
+
const start = message.indexOf("{");
|
|
349
|
+
if (start === -1)
|
|
350
|
+
return null;
|
|
351
|
+
try {
|
|
352
|
+
const body = JSON.parse(message.slice(start));
|
|
353
|
+
if (!body || typeof body !== "object")
|
|
354
|
+
return null;
|
|
355
|
+
const code = body.error;
|
|
356
|
+
return typeof code === "string" ? code : "";
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function classifyCode(code) {
|
|
363
|
+
switch (code) {
|
|
364
|
+
case "storage_full":
|
|
365
|
+
return "storage_full";
|
|
366
|
+
// The key works but may not write: only a different key can fix it.
|
|
367
|
+
case "insufficient_scope":
|
|
368
|
+
return "auth";
|
|
369
|
+
case "rate_limit_exceeded":
|
|
370
|
+
return "rate_limit";
|
|
371
|
+
case "writes_paused":
|
|
372
|
+
return "paused";
|
|
373
|
+
}
|
|
374
|
+
// message_limit_exceeded, indexing_quota_exceeded,
|
|
375
|
+
// conversation_limit_exceeded, …: a plan limit.
|
|
376
|
+
if (code.endsWith("limit_exceeded") || code.endsWith("quota_exceeded"))
|
|
377
|
+
return "billing";
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
const AUTH_PHRASE = /\bunauthori[sz]ed\b|\bforbidden\b|\bauthentication (?:failed|error|required)\b|\binvalid (?:or missing )?api key\b/i;
|
|
381
|
+
/**
|
|
382
|
+
* Which kind of failure an error is, and so how the daemon treats it.
|
|
383
|
+
*
|
|
384
|
+
* Read from the error's structure first (engram#587): the SDK's error type
|
|
385
|
+
* and code, then the server's JSON error code, then the HTTP status. The
|
|
386
|
+
* message text comes last. It used to be all there was, searched for "401",
|
|
387
|
+
* "403", "429", "rate" … and every one of those went wrong somewhere:
|
|
388
|
+
* - the SDK's AuthenticationError reads "Invalid or missing API key", so a
|
|
389
|
+
* rejected key passed as a network error and was retried every 2 s;
|
|
390
|
+
* - a 429 arrives with an empty message, so it passed as a network error;
|
|
391
|
+
* - indexing_quota_exceeded and insufficient_scope matched nothing;
|
|
392
|
+
* - a usage count in a refusal body containing 401 or 403 read as a
|
|
393
|
+
* rejected key, which stopped syncing until the daemon restarted;
|
|
394
|
+
* - "rate" matched "generate" and "separate".
|
|
395
|
+
*
|
|
396
|
+
* Plain strings are still accepted (tests, and any error that isn't the
|
|
397
|
+
* SDK's). A status number in the text counts only outside a JSON body:
|
|
398
|
+
* inside one it is data.
|
|
399
|
+
*/
|
|
400
|
+
export function classifyError(err) {
|
|
401
|
+
const { name, code, status, message } = errorParts(err);
|
|
402
|
+
if (name === "AuthenticationError" || code === "auth_error" || status === 401 || status === 403) {
|
|
101
403
|
return "auth";
|
|
102
404
|
}
|
|
405
|
+
const body = bodyCode(message);
|
|
406
|
+
const byCode = body ? classifyCode(body) : null;
|
|
407
|
+
if (byCode)
|
|
408
|
+
return byCode;
|
|
409
|
+
if (status === 429)
|
|
410
|
+
return "rate_limit";
|
|
411
|
+
if (status === 402)
|
|
412
|
+
return "billing";
|
|
413
|
+
if (status === 413)
|
|
414
|
+
return "too_large";
|
|
415
|
+
if (status !== undefined && status >= 500)
|
|
416
|
+
return "server";
|
|
417
|
+
const head = body === null ? message : message.slice(0, message.indexOf("{"));
|
|
103
418
|
// Must precede the generic billing check: a storage_full body also carries
|
|
104
419
|
// a 402, but it is a different situation — backing off and retrying can
|
|
105
420
|
// never clear it, so it gets its own state and its own copy.
|
|
106
|
-
if (message.includes("storage_full"))
|
|
421
|
+
if (message.includes("storage_full"))
|
|
107
422
|
return "storage_full";
|
|
423
|
+
// A 413 comes with no JSON-RPC body, so the SDK throws "Unexpected
|
|
424
|
+
// response format: Payload Too Large" (Engram's own limit) with no status,
|
|
425
|
+
// or a proxy's page, "413 Request Entity Too Large" (engram#750). It used
|
|
426
|
+
// to read as a network error and be retried unchanged forever. Read from
|
|
427
|
+
// outside a JSON body only: inside one it is data.
|
|
428
|
+
if (/\b413\b|payload too large|request entity too large|content too large/i.test(head)) {
|
|
429
|
+
return "too_large";
|
|
108
430
|
}
|
|
109
|
-
if (
|
|
110
|
-
return "billing";
|
|
111
|
-
}
|
|
112
|
-
if (message.includes("429") || message.includes("rate")) {
|
|
431
|
+
if (/\b429\b/.test(head) || /rate[ _-]?limit|too many requests/i.test(message))
|
|
113
432
|
return "rate_limit";
|
|
114
|
-
|
|
115
|
-
|
|
433
|
+
if (/\b402\b/.test(head) || /(?:limit|quota)_exceeded|\bbilling\b/i.test(message))
|
|
434
|
+
return "billing";
|
|
435
|
+
if (/\b40[13]\b/.test(head) || AUTH_PHRASE.test(message))
|
|
436
|
+
return "auth";
|
|
437
|
+
if (/\b5\d\d\b/.test(head))
|
|
116
438
|
return "server";
|
|
117
|
-
}
|
|
118
439
|
return "network";
|
|
119
440
|
}
|
|
120
|
-
|
|
441
|
+
/**
|
|
442
|
+
* A refusal the server would repeat however long the client waited
|
|
443
|
+
* (engram#588): its own error code for a request it won't serve —
|
|
444
|
+
* insufficient_scope (a key without the read scope can never read the
|
|
445
|
+
* vault), a plan limit, a full memory. Not a rejected key: a new key fixes
|
|
446
|
+
* that, and the daemon picks one up without a restart (engram#587). Not a
|
|
447
|
+
* rate limit, a pause, a 5xx, or a code this doesn't know: those can pass.
|
|
448
|
+
*/
|
|
449
|
+
export function isRefusal(err) {
|
|
450
|
+
const { status, message } = errorParts(err);
|
|
451
|
+
if (status === 429 || (status !== undefined && status >= 500))
|
|
452
|
+
return false;
|
|
453
|
+
const code = bodyCode(message);
|
|
454
|
+
if (!code)
|
|
455
|
+
return false;
|
|
456
|
+
const kind = classifyCode(code);
|
|
457
|
+
return kind === "auth" || kind === "billing" || kind === "storage_full";
|
|
458
|
+
}
|
|
459
|
+
const NOT_FOUND = /^\s*(?:conversation[ _])?not[ _]found\s*\.?\s*$/i;
|
|
460
|
+
/**
|
|
461
|
+
* True only when Engram itself answered that the conversation isn't there:
|
|
462
|
+
* the delete tool's `{"error":"Conversation not found"}`, which the SDK
|
|
463
|
+
* passes on as the message of an error with no HTTP status.
|
|
464
|
+
*
|
|
465
|
+
* Deliberately narrow (engram#587). The opt-out delete treats "not found"
|
|
466
|
+
* as done, so a false match keeps a conversation the user said not to save,
|
|
467
|
+
* and nothing retries it. "getaddrinfo ENOTFOUND" is a DNS failure. An
|
|
468
|
+
* error that carries a status (an SDK NotFoundError included) came from
|
|
469
|
+
* whatever answered at that URL, which need not be Engram: a proxy or the
|
|
470
|
+
* wrong host saying 404 says nothing about the conversation.
|
|
471
|
+
*/
|
|
472
|
+
export function isNotFound(err) {
|
|
473
|
+
const { status, message } = errorParts(err);
|
|
474
|
+
if (status !== undefined)
|
|
475
|
+
return false;
|
|
476
|
+
return NOT_FOUND.test(bodyCode(message) ?? message);
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* True only when Engram's append tool answered that the conversation can't
|
|
480
|
+
* be written to (engram#593): absent, deleted, or not this account's, which
|
|
481
|
+
* the server doesn't tell apart. Its refusal carries the code
|
|
482
|
+
* `conversation_not_found` (engram#592), and the SDK passes that body on as
|
|
483
|
+
* the message of an error with no HTTP status.
|
|
484
|
+
*
|
|
485
|
+
* Narrower than isNotFound on purpose: the daemon stops sending the
|
|
486
|
+
* conversation's messages on this answer, so anything else must stay an
|
|
487
|
+
* ordinary failure that is retried. The bare phrase "Conversation not
|
|
488
|
+
* found" is the delete tool's, not an answer to a send. An error that
|
|
489
|
+
* carries a status, an HTTP 404 included, came from whatever answered at
|
|
490
|
+
* that URL, which need not be Engram (a proxy, the wrong host).
|
|
491
|
+
*/
|
|
492
|
+
export function isConversationGone(err) {
|
|
493
|
+
const { status, message } = errorParts(err);
|
|
494
|
+
if (status !== undefined)
|
|
495
|
+
return false;
|
|
496
|
+
return bodyCode(message) === "conversation_not_found";
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* True only when Engram's append tool refused a batch of an import because
|
|
500
|
+
* it doesn't start where the conversation's import is (engram#751):
|
|
501
|
+
* another import of the same file, or the same batch sent again, got there
|
|
502
|
+
* first. Nothing from the request was stored. Read from the code of the
|
|
503
|
+
* body the SDK passes on, like isConversationGone, and never from an error
|
|
504
|
+
* that carries a status: that came from whatever answered at the URL.
|
|
505
|
+
*/
|
|
506
|
+
export function isImportMoved(err) {
|
|
507
|
+
const { status, message } = errorParts(err);
|
|
508
|
+
if (status !== undefined)
|
|
509
|
+
return false;
|
|
510
|
+
return bodyCode(message) === "import_moved";
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* True when the server accepted the key but refused it a permission it
|
|
514
|
+
* doesn't have: its `insufficient_scope` code. Only a different key can
|
|
515
|
+
* fix that (engram#597). Read from the code, not the text: a message that
|
|
516
|
+
* merely mentions the phrase is not the server's refusal.
|
|
517
|
+
*/
|
|
518
|
+
export function isScopeRefusal(err) {
|
|
519
|
+
return bodyCode(errorParts(err).message) === "insufficient_scope";
|
|
520
|
+
}
|
|
121
521
|
const n = (x) => x.toLocaleString("en-US");
|
|
122
522
|
/**
|
|
123
523
|
* One-line summary of a sync error, in the second person. Shared by the
|
|
@@ -127,7 +527,8 @@ const n = (x) => x.toLocaleString("en-US");
|
|
|
127
527
|
export function syncErrorSummary(errorType, storage) {
|
|
128
528
|
switch (errorType) {
|
|
129
529
|
case "auth":
|
|
130
|
-
return
|
|
530
|
+
return (`Your API key was rejected — run '${engramCommand("auth login")}'. New captures are ` +
|
|
531
|
+
"queued locally and sync as soon as a working key is saved.");
|
|
131
532
|
case "storage_full": {
|
|
132
533
|
const size = storage ? `${n(storage.used)}/${n(storage.limit)} messages` : "its limit";
|
|
133
534
|
return (`Engram's memory is full (${size}). Everything already saved is safe and ` +
|
|
@@ -141,44 +542,168 @@ export function syncErrorSummary(errorType, storage) {
|
|
|
141
542
|
return "Rate limited — messages queued, will retry.";
|
|
142
543
|
case "server":
|
|
143
544
|
return "Server error — messages queued, will retry.";
|
|
545
|
+
case "too_large":
|
|
546
|
+
return ("A message was refused as too large to accept — it stays queued here, " +
|
|
547
|
+
"in order, and is retried. Nothing is lost.");
|
|
548
|
+
case "paused":
|
|
549
|
+
return "Engram is paused for scheduled maintenance — messages queued, will retry.";
|
|
550
|
+
case "vault_key":
|
|
551
|
+
return ("Your vault key (~/.engram/vault-key) can't be read, so messages wait here " +
|
|
552
|
+
"rather than be sent with your vault's secrets in them. Nothing is lost.");
|
|
553
|
+
case "vault_mismatch":
|
|
554
|
+
return ("Your vault holds secrets your vault key doesn't decrypt, so messages wait here " +
|
|
555
|
+
"rather than be sent with those secrets in them. Nothing is lost.");
|
|
144
556
|
default:
|
|
145
|
-
return "Can't reach
|
|
557
|
+
return "Can't reach Engram — messages queued, will retry.";
|
|
146
558
|
}
|
|
147
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* One-line summary of a capture error, in the second person: the daemon
|
|
562
|
+
* log, the desktop notification, `engram status` and the CLI banner.
|
|
563
|
+
*/
|
|
564
|
+
export function captureErrorSummary(c) {
|
|
565
|
+
const waiting = transcriptsWaiting(c.files);
|
|
566
|
+
if (c.kind === "config") {
|
|
567
|
+
return (`Engram can't read the folders you excluded from capture (${c.message}), so ${waiting}. ` +
|
|
568
|
+
`Fix the file, and capture picks up where it stopped: nothing is skipped, ` +
|
|
569
|
+
`and nothing from an excluded folder is captured.`);
|
|
570
|
+
}
|
|
571
|
+
return (`Engram can't queue new messages on this machine (${c.message}), so ${waiting}. ` +
|
|
572
|
+
`Free up disk space, or check that ~/.engram/daemon.db can be ` +
|
|
573
|
+
`written. Capture picks up where it stopped, so nothing is skipped.`);
|
|
574
|
+
}
|
|
575
|
+
/** "1 transcript is waiting", "1,234 transcripts are waiting". */
|
|
576
|
+
function transcriptsWaiting(files) {
|
|
577
|
+
return files === 1 ? "1 transcript is waiting" : `${n(files)} transcripts are waiting`;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* captureErrorSummary's words for config.json, when what keeps it from
|
|
581
|
+
* being read is a ~/.engram that can't be searched (engram#723): the
|
|
582
|
+
* folder and its chmod, not "Fix the file", which nothing is wrong with.
|
|
583
|
+
*/
|
|
584
|
+
function lockedCaptureSummary(locked, files) {
|
|
585
|
+
return (`Engram can't read the folders you excluded from capture: ${lockedDirReason("opening the files")}, ` +
|
|
586
|
+
`so ${transcriptsWaiting(files)}. ${lockedDirFix(locked)}. Capture then picks up where it ` +
|
|
587
|
+
`stopped: nothing is skipped, and nothing from an excluded folder is captured.`);
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* One-line summary of a config.json that can't be read (engram#673), from
|
|
591
|
+
* the file itself, in the second person: the CLI banner and `engram
|
|
592
|
+
* status`. The daemon records it only once a new message waits, and not at
|
|
593
|
+
* all while it isn't running.
|
|
594
|
+
*/
|
|
595
|
+
export function configProblemSummary(message) {
|
|
596
|
+
return (`${message}, so Engram can't tell which folders you excluded from capture. ` +
|
|
597
|
+
`New messages wait until it's fixed; nothing is skipped.`);
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* One-line summary of opt-out deletes the key in use may not make
|
|
601
|
+
* (engram#597), in the second person: `engram status`.
|
|
602
|
+
*/
|
|
603
|
+
export function deleteRefusedSummary(r) {
|
|
604
|
+
const which = `${plural(r.deletes, "conversation")} you opted out of ${r.deletes === 1 ? "is" : "are"}`;
|
|
605
|
+
return (`This API key can't delete conversations, so ${which} waiting to be deleted from ` +
|
|
606
|
+
`Engram. Create a key with the 'delete' permission at getengram.app/dashboard, then ` +
|
|
607
|
+
`run '${engramCommand("auth login <key>")}'.`);
|
|
608
|
+
}
|
|
148
609
|
/** Log a warning to stderr (goes to daemon.log). One line per error type. */
|
|
149
610
|
function logWarning(errorType, storage) {
|
|
150
611
|
console.error(`[engram] WARNING: ${syncErrorSummary(errorType, storage)}`);
|
|
151
|
-
console.error(`[engram] Run '
|
|
612
|
+
console.error(`[engram] Run '${engramCommand("status")}' for details.`);
|
|
613
|
+
}
|
|
614
|
+
/** A notifier that hangs (a PowerShell that never finishes loading, a stuck
|
|
615
|
+
* notification service) is ended, not left running beside the daemon. */
|
|
616
|
+
const NOTIFY_TIMEOUT_MS = 30_000;
|
|
617
|
+
/**
|
|
618
|
+
* The Windows toast (engram#614), one PowerShell statement each. The title
|
|
619
|
+
* and body never become script: they arrive in environment variables and go
|
|
620
|
+
* into the toast as XML text nodes, so quotes, `$(...)`, backticks, `<b>` or
|
|
621
|
+
* `%PATH%` in them are shown as written, never run or parsed. There are no
|
|
622
|
+
* double quotes, which the Windows command line would have to escape.
|
|
623
|
+
*
|
|
624
|
+
* The toast goes out under Windows PowerShell's app ID, which every Windows
|
|
625
|
+
* 10 and 11 has registered, so Windows shows it as from Windows PowerShell.
|
|
626
|
+
*/
|
|
627
|
+
export const TOAST_STATEMENTS = [
|
|
628
|
+
"$ErrorActionPreference = 'Stop'",
|
|
629
|
+
"[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null",
|
|
630
|
+
"[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null",
|
|
631
|
+
"$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)",
|
|
632
|
+
"$text = $template.GetElementsByTagName('text')",
|
|
633
|
+
"$null = $text.Item(0).AppendChild($template.CreateTextNode($env:ENGRAM_NOTIFY_TITLE))",
|
|
634
|
+
"$null = $text.Item(1).AppendChild($template.CreateTextNode($env:ENGRAM_NOTIFY_BODY))",
|
|
635
|
+
"$toast = [Windows.UI.Notifications.ToastNotification]::new($template)",
|
|
636
|
+
"[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe').Show($toast)",
|
|
637
|
+
];
|
|
638
|
+
/** How to show a desktop notification here, or null where there is no way. */
|
|
639
|
+
export function notifyCommand(title, body, platform = process.platform, systemRoot = process.env.SystemRoot || "C:\\Windows") {
|
|
640
|
+
if (platform === "darwin") {
|
|
641
|
+
const esc = (s) => s.replace(/["\\]/g, "\\$&");
|
|
642
|
+
return {
|
|
643
|
+
file: "osascript",
|
|
644
|
+
args: ["-e", `display notification "${esc(body)}" with title "${esc(title)}"`],
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
if (platform === "linux")
|
|
648
|
+
return { file: "notify-send", args: [title, body] };
|
|
649
|
+
if (platform === "win32") {
|
|
650
|
+
return {
|
|
651
|
+
// Windows PowerShell by its full path, never a powershell.exe found on
|
|
652
|
+
// PATH or in the working directory. It ships with Windows, and it can
|
|
653
|
+
// load the Windows notification API, which PowerShell 7 cannot.
|
|
654
|
+
file: windowsPowerShellPath(systemRoot),
|
|
655
|
+
args: powerShellArgs(TOAST_STATEMENTS),
|
|
656
|
+
env: { ENGRAM_NOTIFY_TITLE: title, ENGRAM_NOTIFY_BODY: body },
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
return null;
|
|
152
660
|
}
|
|
153
661
|
/**
|
|
154
|
-
* Best-effort desktop notification. The daemon runs headless under launchd
|
|
155
|
-
* so stderr goes to daemon.log
|
|
156
|
-
*
|
|
662
|
+
* Best-effort desktop notification. The daemon runs headless (under launchd
|
|
663
|
+
* on macOS, started at sign-in on Windows), so stderr goes to daemon.log
|
|
664
|
+
* where nobody reads it — for states that need a human decision, this is
|
|
665
|
+
* the only channel that actually reaches them.
|
|
157
666
|
*
|
|
158
667
|
* Deliberately fire-and-forget and fully swallowed: notification tooling is
|
|
159
668
|
* absent on plenty of systems (headless Linux, minimal containers) and a
|
|
160
|
-
* failure to notify must never disturb syncing.
|
|
669
|
+
* failure to notify must never disturb syncing. `done` is called once it
|
|
670
|
+
* has been shown, or has failed, or there was no way to show it.
|
|
161
671
|
*/
|
|
162
|
-
function notifyDesktop(
|
|
163
|
-
const title = errorType === "storage_full" ? "Engram memory is full" : "Engram needs attention";
|
|
164
|
-
const body = errorType === "storage_full" && storage
|
|
165
|
-
? `${n(storage.used)}/${n(storage.limit)} messages used. New captures are paused.`
|
|
166
|
-
: syncErrorSummary(errorType, storage).slice(0, 180);
|
|
672
|
+
function notifyDesktop(title, body, done = () => { }) {
|
|
167
673
|
try {
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
else if (process.platform === "linux") {
|
|
177
|
-
execFile("notify-send", [title, body], done);
|
|
674
|
+
// A static import (engram#581): this used to be a lazy `require`, which
|
|
675
|
+
// does not exist in this ES-module build. It threw, the catch below
|
|
676
|
+
// swallowed it, and no notification was ever shown.
|
|
677
|
+
const command = notifyCommand(title, body);
|
|
678
|
+
if (!command) {
|
|
679
|
+
done();
|
|
680
|
+
return;
|
|
178
681
|
}
|
|
682
|
+
execFile(command.file, command.args, {
|
|
683
|
+
// The daemon's own environment, plus the title and body on Windows.
|
|
684
|
+
env: command.env && { ...process.env, ...command.env },
|
|
685
|
+
windowsHide: true,
|
|
686
|
+
timeout: NOTIFY_TIMEOUT_MS,
|
|
687
|
+
}, () => done());
|
|
179
688
|
}
|
|
180
689
|
catch {
|
|
181
690
|
// no notification tooling — the log line and the CLI banner still stand
|
|
691
|
+
done();
|
|
182
692
|
}
|
|
183
693
|
}
|
|
694
|
+
/**
|
|
695
|
+
* A desktop notification from a daemon about to exit: settles once it has
|
|
696
|
+
* been shown, or after `ms`. launchd and systemd end whatever a job leaves
|
|
697
|
+
* running when its main process exits, so a notifier started just before
|
|
698
|
+
* the exit, and not waited for, would never be shown.
|
|
699
|
+
*/
|
|
700
|
+
export function notifyDesktopBeforeExit(title, body, ms = 10_000) {
|
|
701
|
+
return new Promise((resolve) => {
|
|
702
|
+
const timer = setTimeout(resolve, ms);
|
|
703
|
+
notifyDesktop(title, body, () => {
|
|
704
|
+
clearTimeout(timer);
|
|
705
|
+
resolve();
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
}
|
|
184
709
|
//# sourceMappingURL=status.js.map
|