@getengram/cli 0.5.0 → 0.6.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/dist/commands/exclude.d.ts +4 -0
- package/dist/commands/exclude.d.ts.map +1 -0
- package/dist/commands/exclude.js +53 -0
- package/dist/commands/exclude.js.map +1 -0
- package/dist/commands/import.d.ts +1 -12
- package/dist/commands/import.d.ts.map +1 -1
- package/dist/commands/import.js +4 -21
- package/dist/commands/import.js.map +1 -1
- package/dist/commands/usage.d.ts.map +1 -1
- package/dist/commands/usage.js +13 -2
- package/dist/commands/usage.js.map +1 -1
- package/dist/commands/vault.d.ts.map +1 -1
- package/dist/commands/vault.js +41 -3
- package/dist/commands/vault.js.map +1 -1
- package/dist/config.d.ts +4 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/daemon/db.d.ts +12 -0
- package/dist/daemon/db.d.ts.map +1 -1
- package/dist/daemon/db.js +44 -0
- package/dist/daemon/db.js.map +1 -1
- package/dist/daemon/exclusions.d.ts +14 -0
- package/dist/daemon/exclusions.d.ts.map +1 -0
- package/dist/daemon/exclusions.js +119 -0
- package/dist/daemon/exclusions.js.map +1 -0
- package/dist/daemon/status.d.ts +30 -3
- package/dist/daemon/status.d.ts.map +1 -1
- package/dist/daemon/status.js +92 -15
- package/dist/daemon/status.js.map +1 -1
- package/dist/daemon/syncer.d.ts +25 -1
- package/dist/daemon/syncer.d.ts.map +1 -1
- package/dist/daemon/syncer.js +136 -7
- package/dist/daemon/syncer.js.map +1 -1
- package/dist/daemon/worker-main.js +5 -0
- package/dist/daemon/worker-main.js.map +1 -1
- package/dist/index.js +74 -3
- package/dist/index.js.map +1 -1
- package/dist/storage-error.d.ts +23 -0
- package/dist/storage-error.d.ts.map +1 -0
- package/dist/storage-error.js +32 -0
- package/dist/storage-error.js.map +1 -0
- package/package.json +11 -10
- package/LICENSE +0 -112
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { statSync, existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join, dirname, resolve, sep } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
// Capture exclusions (engram#462): two ways to keep a session out of Engram.
|
|
5
|
+
//
|
|
6
|
+
// 1. Path exclusions — `engram exclude add <path>` stores absolute paths in
|
|
7
|
+
// ~/.engram/config.json; any session whose cwd is at or under an excluded
|
|
8
|
+
// path is never queued. A committable `.engramignore` marker file at a
|
|
9
|
+
// repo root does the same for everyone who clones the repo.
|
|
10
|
+
//
|
|
11
|
+
// 2. In-chat opt-out — saying "don't save this to engram" (or `engram:ignore`)
|
|
12
|
+
// in a Claude/Codex session stops capture for that session AND erases what
|
|
13
|
+
// was already captured, locally and server-side (see syncer.ts).
|
|
14
|
+
//
|
|
15
|
+
// Both checks run in the daemon's message funnel (syncer.onMessages), so an
|
|
16
|
+
// excluded session is dropped before anything touches the queue or network.
|
|
17
|
+
const CONFIG_FILE = join(homedir(), ".engram", "config.json");
|
|
18
|
+
export const IGNORE_MARKER = ".engramignore";
|
|
19
|
+
// ── Config-based path exclusions (hot-reloaded) ──
|
|
20
|
+
// The daemon is long-lived; `engram exclude add` must take effect without a
|
|
21
|
+
// restart. Re-read config.json only when its mtime changes — a stat per
|
|
22
|
+
// message batch is negligible.
|
|
23
|
+
let cachedPaths = [];
|
|
24
|
+
let cachedMtimeMs = -1;
|
|
25
|
+
/** Normalize for prefix comparison: absolute, no trailing separator. */
|
|
26
|
+
export function normalizeExcludePath(p) {
|
|
27
|
+
const abs = resolve(p);
|
|
28
|
+
return abs.endsWith(sep) ? abs.slice(0, -sep.length) : abs;
|
|
29
|
+
}
|
|
30
|
+
export function loadExcludePaths(configFile = CONFIG_FILE) {
|
|
31
|
+
try {
|
|
32
|
+
const mtimeMs = statSync(configFile).mtimeMs;
|
|
33
|
+
if (mtimeMs !== cachedMtimeMs) {
|
|
34
|
+
const raw = JSON.parse(readFileSync(configFile, "utf-8"));
|
|
35
|
+
cachedPaths = (raw.excludePaths ?? []).map(normalizeExcludePath);
|
|
36
|
+
cachedMtimeMs = mtimeMs;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
cachedPaths = [];
|
|
41
|
+
cachedMtimeMs = -1;
|
|
42
|
+
}
|
|
43
|
+
return cachedPaths;
|
|
44
|
+
}
|
|
45
|
+
/** True when `cwd` is at or under `excluded` (never a sibling-prefix match:
|
|
46
|
+
* /a/repo does NOT match /a/repo2). */
|
|
47
|
+
export function isUnder(cwd, excluded) {
|
|
48
|
+
const c = normalizeExcludePath(cwd);
|
|
49
|
+
return c === excluded || c.startsWith(excluded + sep);
|
|
50
|
+
}
|
|
51
|
+
// ── .engramignore marker files ──
|
|
52
|
+
// Walking to the filesystem root per batch would be wasteful; sessions keep a
|
|
53
|
+
// stable cwd, so cache the verdict per directory. The daemon is restarted
|
|
54
|
+
// rarely — a freshly added marker applies to new sessions immediately via the
|
|
55
|
+
// negative-TTL below.
|
|
56
|
+
const markerCache = new Map();
|
|
57
|
+
const NEGATIVE_TTL_MS = 60_000; // re-check "no marker" dirs once a minute
|
|
58
|
+
export function hasIgnoreMarker(cwd, nowMs = Date.now()) {
|
|
59
|
+
const start = normalizeExcludePath(cwd);
|
|
60
|
+
const cached = markerCache.get(start);
|
|
61
|
+
if (cached && (cached.found || nowMs - cached.at < NEGATIVE_TTL_MS)) {
|
|
62
|
+
return cached.found;
|
|
63
|
+
}
|
|
64
|
+
let dir = start;
|
|
65
|
+
let found = false;
|
|
66
|
+
// Walk up to the filesystem root looking for the marker.
|
|
67
|
+
for (;;) {
|
|
68
|
+
if (existsSync(join(dir, IGNORE_MARKER))) {
|
|
69
|
+
found = true;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
const parent = dirname(dir);
|
|
73
|
+
if (parent === dir)
|
|
74
|
+
break;
|
|
75
|
+
dir = parent;
|
|
76
|
+
}
|
|
77
|
+
markerCache.set(start, { found, at: nowMs });
|
|
78
|
+
return found;
|
|
79
|
+
}
|
|
80
|
+
/** Test hook: reset module caches. */
|
|
81
|
+
export function resetExclusionCaches() {
|
|
82
|
+
markerCache.clear();
|
|
83
|
+
cachedPaths = [];
|
|
84
|
+
cachedMtimeMs = -1;
|
|
85
|
+
}
|
|
86
|
+
/** Full path check: config exclusions + .engramignore markers. */
|
|
87
|
+
export function isPathExcluded(cwd, configFile = CONFIG_FILE) {
|
|
88
|
+
if (!cwd)
|
|
89
|
+
return false;
|
|
90
|
+
const paths = loadExcludePaths(configFile);
|
|
91
|
+
if (paths.some((p) => isUnder(cwd, p)))
|
|
92
|
+
return true;
|
|
93
|
+
return hasIgnoreMarker(cwd);
|
|
94
|
+
}
|
|
95
|
+
// ── In-chat opt-out phrase detection ──
|
|
96
|
+
// Only USER-role messages are scanned — the assistant merely discussing this
|
|
97
|
+
// feature ("you can say 'don't save this to engram'") must not trigger it.
|
|
98
|
+
// Three shapes, all requiring the word "engram" so ordinary "don't log this"
|
|
99
|
+
// chatter can't disable capture:
|
|
100
|
+
// 1. explicit marker: engram:ignore | engram: off | engram skip
|
|
101
|
+
// 2. negation → verb → engram: "don't save this to engram",
|
|
102
|
+
// "please do not sync this conversation to engram", "stop capturing to engram"
|
|
103
|
+
// 3. engram → negation → verb: "engram, don't record this",
|
|
104
|
+
// "engram should not save this session"
|
|
105
|
+
const VERB = "(?:sav\\w*|record\\w*|captur\\w*|sync\\w*|remember\\w*|log\\w*|track\\w*|stor\\w*|upload\\w*)";
|
|
106
|
+
const NEG = "(?:don'?t|do\\s+not|please\\s+don'?t|please\\s+do\\s+not|stop|never|should\\s+not|shouldn'?t)";
|
|
107
|
+
const SPAN = "[^.!?\\n]{0,80}?";
|
|
108
|
+
const OPT_OUT_PATTERNS = [
|
|
109
|
+
/\bengram\s*[:,]?\s*(?:ignore|off|skip|exclude|opt[- ]?out)\b/i,
|
|
110
|
+
new RegExp(`\\b${NEG}\\b${SPAN}\\b${VERB}\\b${SPAN}\\bengram\\b`, "i"),
|
|
111
|
+
new RegExp(`\\bengram\\b${SPAN}\\b${NEG}\\b${SPAN}\\b${VERB}\\b`, "i"),
|
|
112
|
+
];
|
|
113
|
+
export function detectOptOut(content) {
|
|
114
|
+
// Transcript messages can be enormous (tool dumps); an opt-out is something
|
|
115
|
+
// a person typed, so only scan a sane prefix.
|
|
116
|
+
const head = content.length > 4096 ? content.slice(0, 4096) : content;
|
|
117
|
+
return OPT_OUT_PATTERNS.some((re) => re.test(head));
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=exclusions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exclusions.js","sourceRoot":"","sources":["../../src/daemon/exclusions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACxD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,6EAA6E;AAC7E,EAAE;AACF,4EAA4E;AAC5E,6EAA6E;AAC7E,0EAA0E;AAC1E,+DAA+D;AAC/D,EAAE;AACF,+EAA+E;AAC/E,8EAA8E;AAC9E,oEAAoE;AACpE,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAE5E,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;AAC9D,MAAM,CAAC,MAAM,aAAa,GAAG,eAAe,CAAC;AAE7C,oDAAoD;AAEpD,4EAA4E;AAC5E,wEAAwE;AACxE,+BAA+B;AAC/B,IAAI,WAAW,GAAa,EAAE,CAAC;AAC/B,IAAI,aAAa,GAAG,CAAC,CAAC,CAAC;AAEvB,wEAAwE;AACxE,MAAM,UAAU,oBAAoB,CAAC,CAAS;IAC5C,MAAM,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;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O,eAAe,CAAC,GAAG,CAAC,CAAC;AAC9B,CAAC;AAED,yCAAyC;AAEzC,6EAA6E;AAC7E,2EAA2E;AAC3E,6EAA6E;AAC7E,iCAAiC;AACjC,mEAAmE;AACnE,8DAA8D;AAC9D,oFAAoF;AACpF,8DAA8D;AAC9D,6CAA6C;AAC7C,MAAM,IAAI,GAAG,+FAA+F,CAAC;AAC7G,MAAM,GAAG,GAAG,+FAA+F,CAAC;AAC5G,MAAM,IAAI,GAAG,kBAAkB,CAAC;AAEhC,MAAM,gBAAgB,GAAa;IACjC,+DAA+D;IAC/D,IAAI,MAAM,CAAC,MAAM,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,cAAc,EAAE,GAAG,CAAC;IACtE,IAAI,MAAM,CAAC,eAAe,IAAI,MAAM,GAAG,MAAM,IAAI,MAAM,IAAI,KAAK,EAAE,GAAG,CAAC;CACvE,CAAC;AAEF,MAAM,UAAU,YAAY,CAAC,OAAe;IAC1C,4EAA4E;IAC5E,8CAA8C;IAC9C,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IACtE,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;AACtD,CAAC"}
|
package/dist/daemon/status.d.ts
CHANGED
|
@@ -1,21 +1,48 @@
|
|
|
1
1
|
export type SyncHealth = "healthy" | "warning" | "error";
|
|
2
|
+
export type SyncErrorType = "auth"
|
|
3
|
+
/** Lifetime memory is full. Distinct from `billing`: retrying will never
|
|
4
|
+
* succeed on its own — it needs an upgrade or deleted conversations. */
|
|
5
|
+
| "storage_full" | "billing" | "rate_limit" | "network" | "server";
|
|
2
6
|
export interface SyncStatus {
|
|
3
7
|
health: SyncHealth;
|
|
4
8
|
last_sync_at: string | null;
|
|
5
9
|
last_error_at: string | null;
|
|
6
10
|
last_error: string | null;
|
|
7
|
-
error_type:
|
|
11
|
+
error_type: SyncErrorType | null;
|
|
8
12
|
pending_messages: number;
|
|
9
13
|
consecutive_failures: number;
|
|
14
|
+
/** Populated on a storage_full error so the numbers can be shown without
|
|
15
|
+
* a network round trip (the CLI may be offline when it surfaces them). */
|
|
16
|
+
storage: {
|
|
17
|
+
used: number;
|
|
18
|
+
limit: number;
|
|
19
|
+
} | null;
|
|
10
20
|
updated_at: string;
|
|
11
21
|
}
|
|
12
22
|
export declare function readStatus(): SyncStatus;
|
|
13
23
|
/** Record a successful sync. */
|
|
14
24
|
export declare function recordSuccess(pendingMessages: number): void;
|
|
15
|
-
/**
|
|
16
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Record a sync failure. Logs once per error type and, for the two states
|
|
27
|
+
* the user must act on (auth, storage_full), raises a desktop notification
|
|
28
|
+
* on first occurrence — the daemon is headless, so a line in daemon.log is
|
|
29
|
+
* not "telling" anyone.
|
|
30
|
+
*/
|
|
31
|
+
export declare function recordFailure(error: string, errorType: SyncStatus["error_type"], pendingMessages: number, storage?: {
|
|
32
|
+
used?: number;
|
|
33
|
+
limit?: number;
|
|
34
|
+
} | null): void;
|
|
17
35
|
/** Update pending count without changing health. */
|
|
18
36
|
export declare function updatePending(count: number): void;
|
|
19
37
|
declare function classifyError(message: string): SyncStatus["error_type"];
|
|
20
38
|
export { classifyError };
|
|
39
|
+
/**
|
|
40
|
+
* One-line summary of a sync error, in the second person. Shared by the
|
|
41
|
+
* daemon log, the desktop notification, and the banner the CLI prints on
|
|
42
|
+
* the next interactive command.
|
|
43
|
+
*/
|
|
44
|
+
export declare function syncErrorSummary(errorType: SyncStatus["error_type"], storage?: {
|
|
45
|
+
used: number;
|
|
46
|
+
limit: number;
|
|
47
|
+
} | null): string;
|
|
21
48
|
//# sourceMappingURL=status.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../../src/daemon/status.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,CAAC;AAEzD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,UAAU,CAAC;IACnB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../../src/daemon/status.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,CAAC;AAEzD,MAAM,MAAM,aAAa,GACrB,MAAM;AACR;yEACyE;GACvE,cAAc,GACd,SAAS,GACT,YAAY,GACZ,SAAS,GACT,QAAQ,CAAC;AAEb,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,UAAU,CAAC;IACnB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,UAAU,EAAE,aAAa,GAAG,IAAI,CAAC;IACjC,gBAAgB,EAAE,MAAM,CAAC;IACzB,oBAAoB,EAAE,MAAM,CAAC;IAC7B;+EAC2E;IAC3E,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAChD,UAAU,EAAE,MAAM,CAAC;CACpB;AAiBD,wBAAgB,UAAU,IAAI,UAAU,CAevC;AAWD,gCAAgC;AAChC,wBAAgB,aAAa,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,CAW3D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,UAAU,CAAC,YAAY,CAAC,EACnC,eAAe,EAAE,MAAM,EACvB,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,GACjD,IAAI,CAiCN;AAED,oDAAoD;AACpD,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAGjD;AAED,iBAAS,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAAC,YAAY,CAAC,CAoBhE;AAED,OAAO,EAAE,aAAa,EAAE,CAAC;AAIzB;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,UAAU,CAAC,YAAY,CAAC,EACnC,OAAO,CAAC,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,GAC/C,MAAM,CAsBR"}
|
package/dist/daemon/status.js
CHANGED
|
@@ -10,6 +10,7 @@ const DEFAULT_STATUS = {
|
|
|
10
10
|
error_type: null,
|
|
11
11
|
pending_messages: 0,
|
|
12
12
|
consecutive_failures: 0,
|
|
13
|
+
storage: null,
|
|
13
14
|
updated_at: new Date().toISOString(),
|
|
14
15
|
};
|
|
15
16
|
let currentStatus = { ...DEFAULT_STATUS };
|
|
@@ -17,7 +18,13 @@ let lastNotifiedError = null;
|
|
|
17
18
|
export function readStatus() {
|
|
18
19
|
try {
|
|
19
20
|
if (existsSync(STATUS_FILE)) {
|
|
20
|
-
|
|
21
|
+
// Merged over the defaults: a status.json written by an older CLI
|
|
22
|
+
// predates newer fields, and callers should get null rather than
|
|
23
|
+
// undefined for them.
|
|
24
|
+
return {
|
|
25
|
+
...DEFAULT_STATUS,
|
|
26
|
+
...JSON.parse(readFileSync(STATUS_FILE, "utf-8")),
|
|
27
|
+
};
|
|
21
28
|
}
|
|
22
29
|
}
|
|
23
30
|
catch {
|
|
@@ -43,18 +50,29 @@ export function recordSuccess(pendingMessages) {
|
|
|
43
50
|
currentStatus.error_type = null;
|
|
44
51
|
currentStatus.pending_messages = pendingMessages;
|
|
45
52
|
currentStatus.consecutive_failures = 0;
|
|
53
|
+
currentStatus.storage = null;
|
|
46
54
|
lastNotifiedError = null;
|
|
47
55
|
writeStatus();
|
|
48
56
|
}
|
|
49
|
-
/**
|
|
50
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Record a sync failure. Logs once per error type and, for the two states
|
|
59
|
+
* the user must act on (auth, storage_full), raises a desktop notification
|
|
60
|
+
* on first occurrence — the daemon is headless, so a line in daemon.log is
|
|
61
|
+
* not "telling" anyone.
|
|
62
|
+
*/
|
|
63
|
+
export function recordFailure(error, errorType, pendingMessages, storage) {
|
|
51
64
|
currentStatus.consecutive_failures++;
|
|
52
65
|
currentStatus.last_error = error;
|
|
53
66
|
currentStatus.last_error_at = new Date().toISOString();
|
|
54
67
|
currentStatus.error_type = errorType;
|
|
55
68
|
currentStatus.pending_messages = pendingMessages;
|
|
69
|
+
if (errorType === "storage_full" &&
|
|
70
|
+
typeof storage?.used === "number" &&
|
|
71
|
+
typeof storage?.limit === "number") {
|
|
72
|
+
currentStatus.storage = { used: storage.used, limit: storage.limit };
|
|
73
|
+
}
|
|
56
74
|
// Escalate health based on severity and consecutive failures
|
|
57
|
-
if (errorType === "auth" || errorType === "billing") {
|
|
75
|
+
if (errorType === "auth" || errorType === "billing" || errorType === "storage_full") {
|
|
58
76
|
currentStatus.health = "error";
|
|
59
77
|
}
|
|
60
78
|
else if (currentStatus.consecutive_failures >= 5) {
|
|
@@ -67,7 +85,10 @@ export function recordFailure(error, errorType, pendingMessages) {
|
|
|
67
85
|
// Log once per error type (not per unique message) to avoid spam
|
|
68
86
|
if (errorType !== lastNotifiedError) {
|
|
69
87
|
lastNotifiedError = errorType;
|
|
70
|
-
logWarning(errorType);
|
|
88
|
+
logWarning(errorType, currentStatus.storage);
|
|
89
|
+
if (errorType === "auth" || errorType === "storage_full") {
|
|
90
|
+
notifyDesktop(errorType, currentStatus.storage);
|
|
91
|
+
}
|
|
71
92
|
}
|
|
72
93
|
}
|
|
73
94
|
/** Update pending count without changing health. */
|
|
@@ -79,6 +100,12 @@ function classifyError(message) {
|
|
|
79
100
|
if (message.includes("401") || message.includes("403") || message.includes("Authentication")) {
|
|
80
101
|
return "auth";
|
|
81
102
|
}
|
|
103
|
+
// Must precede the generic billing check: a storage_full body also carries
|
|
104
|
+
// a 402, but it is a different situation — backing off and retrying can
|
|
105
|
+
// never clear it, so it gets its own state and its own copy.
|
|
106
|
+
if (message.includes("storage_full")) {
|
|
107
|
+
return "storage_full";
|
|
108
|
+
}
|
|
82
109
|
if (message.includes("402") || message.includes("limit_exceeded") || message.includes("billing")) {
|
|
83
110
|
return "billing";
|
|
84
111
|
}
|
|
@@ -91,17 +118,67 @@ function classifyError(message) {
|
|
|
91
118
|
return "network";
|
|
92
119
|
}
|
|
93
120
|
export { classifyError };
|
|
121
|
+
const n = (x) => x.toLocaleString("en-US");
|
|
122
|
+
/**
|
|
123
|
+
* One-line summary of a sync error, in the second person. Shared by the
|
|
124
|
+
* daemon log, the desktop notification, and the banner the CLI prints on
|
|
125
|
+
* the next interactive command.
|
|
126
|
+
*/
|
|
127
|
+
export function syncErrorSummary(errorType, storage) {
|
|
128
|
+
switch (errorType) {
|
|
129
|
+
case "auth":
|
|
130
|
+
return "Authentication failed — run 'engram auth login'.";
|
|
131
|
+
case "storage_full": {
|
|
132
|
+
const size = storage ? `${n(storage.used)}/${n(storage.limit)} messages` : "its limit";
|
|
133
|
+
return (`Engram's memory is full (${size}). Everything already saved is safe and ` +
|
|
134
|
+
`searchable — but new captures are queued locally and will NOT be saved ` +
|
|
135
|
+
`until you free space. Upgrade ($9/mo for 1,000,000 messages) or delete ` +
|
|
136
|
+
`old conversations: https://getengram.app/pricing`);
|
|
137
|
+
}
|
|
138
|
+
case "billing":
|
|
139
|
+
return "Plan limit reached — upgrade at https://getengram.app/pricing. Messages are queued locally and will retry with backoff.";
|
|
140
|
+
case "rate_limit":
|
|
141
|
+
return "Rate limited — messages queued, will retry.";
|
|
142
|
+
case "server":
|
|
143
|
+
return "Server error — messages queued, will retry.";
|
|
144
|
+
default:
|
|
145
|
+
return "Can't reach servers — messages queued locally.";
|
|
146
|
+
}
|
|
147
|
+
}
|
|
94
148
|
/** Log a warning to stderr (goes to daemon.log). One line per error type. */
|
|
95
|
-
function logWarning(errorType) {
|
|
96
|
-
|
|
97
|
-
auth: "Authentication failed — run 'engram auth login'",
|
|
98
|
-
billing: "Plan limit reached — upgrade at https://getengram.app/pricing. Messages are queued locally and will retry with backoff.",
|
|
99
|
-
rate_limit: "Rate limited — messages queued, will retry",
|
|
100
|
-
network: "Can't reach servers — messages queued locally",
|
|
101
|
-
server: "Server error — messages queued, will retry",
|
|
102
|
-
};
|
|
103
|
-
const msg = warnings[errorType ?? "network"] ?? "Unknown sync error";
|
|
104
|
-
console.error(`[engram] WARNING: ${msg}`);
|
|
149
|
+
function logWarning(errorType, storage) {
|
|
150
|
+
console.error(`[engram] WARNING: ${syncErrorSummary(errorType, storage)}`);
|
|
105
151
|
console.error(`[engram] Run 'engram status' for details.`);
|
|
106
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Best-effort desktop notification. The daemon runs headless under launchd,
|
|
155
|
+
* so stderr goes to daemon.log where nobody reads it — for states that need
|
|
156
|
+
* a human decision, this is the only channel that actually reaches them.
|
|
157
|
+
*
|
|
158
|
+
* Deliberately fire-and-forget and fully swallowed: notification tooling is
|
|
159
|
+
* absent on plenty of systems (headless Linux, minimal containers) and a
|
|
160
|
+
* failure to notify must never disturb syncing.
|
|
161
|
+
*/
|
|
162
|
+
function notifyDesktop(errorType, storage) {
|
|
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);
|
|
167
|
+
try {
|
|
168
|
+
// Lazy require: keeps the module import-light for the daemon bootstrap,
|
|
169
|
+
// which deliberately avoids pulling in anything heavy at startup.
|
|
170
|
+
const { execFile } = require("node:child_process");
|
|
171
|
+
const done = () => { };
|
|
172
|
+
if (process.platform === "darwin") {
|
|
173
|
+
const esc = (s) => s.replace(/["\\]/g, "\\$&");
|
|
174
|
+
execFile("osascript", ["-e", `display notification "${esc(body)}" with title "${esc(title)}"`], done);
|
|
175
|
+
}
|
|
176
|
+
else if (process.platform === "linux") {
|
|
177
|
+
execFile("notify-send", [title, body], done);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// no notification tooling — the log line and the CLI banner still stand
|
|
182
|
+
}
|
|
183
|
+
}
|
|
107
184
|
//# sourceMappingURL=status.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"status.js","sourceRoot":"","sources":["../../src/daemon/status.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"status.js","sourceRoot":"","sources":["../../src/daemon/status.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;AA4B9D,MAAM,cAAc,GAAe;IACjC,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AS,KAAK,cAAc,IAAI,OAAO;QACrC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,0CAA0C;QAClF,CAAC,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACzD,IAAI,CAAC;QACH,wEAAwE;QACxE,kEAAkE;QAClE,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAwC,CAAC;QAC1F,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;QACtB,IAAI,OAAO,CAAC,QAAQ,KAAK,QAAQ,EAAE,CAAC;YAClC,MAAM,GAAG,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;YACvD,QAAQ,CACN,WAAW,EACX,CAAC,IAAI,EAAE,yBAAyB,GAAG,CAAC,IAAI,CAAC,iBAAiB,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EACxE,IAAI,CACL,CAAC;QACJ,CAAC;aAAM,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;YACxC,QAAQ,CAAC,aAAa,EAAE,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,wEAAwE;IAC1E,CAAC;AACH,CAAC"}
|
package/dist/daemon/syncer.d.ts
CHANGED
|
@@ -24,14 +24,38 @@ export declare class Syncer {
|
|
|
24
24
|
* to avoid blocking the parser on network calls.
|
|
25
25
|
*/
|
|
26
26
|
onMessages(sessionId: string, meta: SessionMeta, messages: ParsedMessage[]): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* The user said not to save this session. Semantics are "this session was
|
|
29
|
+
* never captured": stop future capture, wipe the local queue, and delete
|
|
30
|
+
* whatever already reached the server. The remote delete is persisted in
|
|
31
|
+
* excluded_sessions.delete_pending and retried from the flush loop until
|
|
32
|
+
* it succeeds — offline opt-out must still stick.
|
|
33
|
+
*/
|
|
34
|
+
private excludeSession;
|
|
35
|
+
/** Delete server-side conversations for opted-out sessions; keeps
|
|
36
|
+
* delete_pending until the server confirms. */
|
|
37
|
+
private retryRemoteDeletes;
|
|
27
38
|
startFlushLoop(): void;
|
|
28
39
|
stopFlushLoop(): void;
|
|
29
40
|
/** Flush all pending messages to the API. */
|
|
30
41
|
flush(): Promise<void>;
|
|
31
42
|
/** Create conversations for sessions that were deferred during parsing. */
|
|
32
43
|
private createPendingConversations;
|
|
44
|
+
/** Per-conversation reduced batch size after a failed send (halves down to
|
|
45
|
+
* 1 so even a single pathological message eventually goes alone; restored
|
|
46
|
+
* to the full batch on the next success). */
|
|
47
|
+
private batchOverride;
|
|
33
48
|
private flushConversation;
|
|
34
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Enter billing backoff — escalates each time: 5min → 15min → 1hr.
|
|
51
|
+
*
|
|
52
|
+
* A full memory is separated out here. It arrives as a 402 like any other
|
|
53
|
+
* billing error, but retrying can never clear it on its own: it needs an
|
|
54
|
+
* upgrade or deleted conversations. It gets its own status state (so the
|
|
55
|
+
* CLI can say "memory is full" rather than "plan limit reached") and its
|
|
56
|
+
* used/limit numbers are carried through, so the banner can name them
|
|
57
|
+
* even when the machine is offline.
|
|
58
|
+
*/
|
|
35
59
|
private enterBillingBackoff;
|
|
36
60
|
/** Reset billing backoff after a successful sync. */
|
|
37
61
|
private resetBillingBackoff;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syncer.d.ts","sourceRoot":"","sources":["../../src/daemon/syncer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"syncer.d.ts","sourceRoot":"","sources":["../../src/daemon/syncer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAInC,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAc,MAAM,YAAY,CAAC;AAqBzE;;;;GAIG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,EAAE,CAAW;IACrB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAA+C;IAC5D,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,UAAU,CAAS;IAE3B,4BAA4B;IAC5B,OAAO,CAAC,mBAAmB,CAAa;IACxC,OAAO,CAAC,kBAAkB,CAAa;IAEvC,0EAA0E;IAC1E,OAAO,CAAC,eAAe,CAGnB;gBAEQ,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM;IAKxC;;;;OAIG;IACG,UAAU,CACd,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,WAAW,EACjB,QAAQ,EAAE,aAAa,EAAE,GACxB,OAAO,CAAC,IAAI,CAAC;IAiChB;;;;;;OAMG;YACW,cAAc;IAoB5B;oDACgD;YAClC,kBAAkB;IAuBhC,cAAc,IAAI,IAAI;IAItB,aAAa,IAAI,IAAI;IAOrB,6CAA6C;IACvC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAkD5B,2EAA2E;YAC7D,0BAA0B;IAyDxC;;kDAE8C;IAC9C,OAAO,CAAC,aAAa,CAA6B;YAEpC,iBAAiB;IA+D/B;;;;;;;;;OASG;IACH,OAAO,CAAC,mBAAmB;IA2B3B,qDAAqD;IACrD,OAAO,CAAC,mBAAmB;YAQb,kBAAkB;CA+BjC"}
|
package/dist/daemon/syncer.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { recordSuccess, recordFailure, classifyError } from "./status.js";
|
|
2
|
-
|
|
2
|
+
import { parseStorageFullError } from "../storage-error.js";
|
|
3
|
+
import { isPathExcluded, detectOptOut } from "./exclusions.js";
|
|
4
|
+
// Claude Code transcript messages can be enormous (tool output dumps), and
|
|
5
|
+
// the server does per-message work on append (R2 write, chunk, embed, index).
|
|
6
|
+
// 200-message batches reliably exceeded the request timeout and wedged the
|
|
7
|
+
// queue in a retry loop — same batch, same timeout, forever. Small batches
|
|
8
|
+
// with a byte budget keep every request comfortably inside the timeout; the
|
|
9
|
+
// halve-on-failure fallback (see flushConversation) resolves any batch that
|
|
10
|
+
// still can't make it through, down to one message at a time.
|
|
11
|
+
const BATCH_SIZE = 25;
|
|
12
|
+
const BATCH_BYTE_BUDGET = 256 * 1024;
|
|
3
13
|
const FLUSH_INTERVAL_MS = 2_000;
|
|
4
14
|
const CONCURRENCY = 5;
|
|
5
15
|
/** Backoff schedule for billing/limit errors: 5min → 15min → 1hr → 1hr ... */
|
|
@@ -34,6 +44,23 @@ export class Syncer {
|
|
|
34
44
|
* to avoid blocking the parser on network calls.
|
|
35
45
|
*/
|
|
36
46
|
async onMessages(sessionId, meta, messages) {
|
|
47
|
+
// Capture exclusions (engram#462) — checked here, the single funnel, so
|
|
48
|
+
// an excluded session never touches the queue or the network.
|
|
49
|
+
// A session the user already opted out of stays out.
|
|
50
|
+
if (this.db.isSessionExcluded(sessionId))
|
|
51
|
+
return;
|
|
52
|
+
// Repo/directory exclusions: `engram exclude add <path>` or a
|
|
53
|
+
// .engramignore marker at the repo root. Checked per batch (cheap,
|
|
54
|
+
// cached) so a freshly added exclusion applies without a restart.
|
|
55
|
+
if (isPathExcluded(meta.cwd))
|
|
56
|
+
return;
|
|
57
|
+
// In-chat opt-out: a USER message saying "don't save this to engram"
|
|
58
|
+
// (or `engram:ignore`) excludes the whole session — including what was
|
|
59
|
+
// already captured.
|
|
60
|
+
if (messages.some((m) => m.role === "user" && detectOptOut(m.content))) {
|
|
61
|
+
await this.excludeSession(sessionId, "in-chat opt-out");
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
37
64
|
// Check if we already have a mapping
|
|
38
65
|
let conversationId = this.db.getConversationId(sessionId);
|
|
39
66
|
if (!conversationId) {
|
|
@@ -44,6 +71,47 @@ export class Syncer {
|
|
|
44
71
|
}
|
|
45
72
|
this.db.enqueue(conversationId, messages);
|
|
46
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* The user said not to save this session. Semantics are "this session was
|
|
76
|
+
* never captured": stop future capture, wipe the local queue, and delete
|
|
77
|
+
* whatever already reached the server. The remote delete is persisted in
|
|
78
|
+
* excluded_sessions.delete_pending and retried from the flush loop until
|
|
79
|
+
* it succeeds — offline opt-out must still stick.
|
|
80
|
+
*/
|
|
81
|
+
async excludeSession(sessionId, reason) {
|
|
82
|
+
const conversationId = this.db.getConversationId(sessionId);
|
|
83
|
+
this.db.markSessionExcluded(sessionId, reason, conversationId ?? undefined);
|
|
84
|
+
this.pendingSessions.delete(sessionId);
|
|
85
|
+
const dropped = this.db.deleteAllForConversation(`pending:${sessionId}`) +
|
|
86
|
+
(conversationId ? this.db.deleteAllForConversation(conversationId) : 0);
|
|
87
|
+
console.log(`[engram] session ${sessionId} excluded (${reason}) — ` +
|
|
88
|
+
`${dropped} queued message(s) dropped` +
|
|
89
|
+
(conversationId ? ", deleting synced copy" : ""));
|
|
90
|
+
if (conversationId) {
|
|
91
|
+
await this.retryRemoteDeletes();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Delete server-side conversations for opted-out sessions; keeps
|
|
95
|
+
* delete_pending until the server confirms. */
|
|
96
|
+
async retryRemoteDeletes() {
|
|
97
|
+
for (const row of this.db.getPendingRemoteDeletes()) {
|
|
98
|
+
try {
|
|
99
|
+
await this.client.deleteConversation(row.delete_pending);
|
|
100
|
+
this.db.clearPendingRemoteDelete(row.session_id);
|
|
101
|
+
console.log(`[engram] deleted synced conversation ${row.delete_pending} for opted-out session`);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
105
|
+
// "Not found" means it's already gone (or was never fully created) —
|
|
106
|
+
// the promise is kept either way.
|
|
107
|
+
if (msg.includes("404") || /not.?found/i.test(msg)) {
|
|
108
|
+
this.db.clearPendingRemoteDelete(row.session_id);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
console.error(`[engram] opt-out delete failed for ${row.delete_pending} (will retry): ${msg}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
47
115
|
startFlushLoop() {
|
|
48
116
|
this.timer = setInterval(() => this.flush(), FLUSH_INTERVAL_MS);
|
|
49
117
|
}
|
|
@@ -62,6 +130,9 @@ export class Syncer {
|
|
|
62
130
|
return;
|
|
63
131
|
this.flushing = true;
|
|
64
132
|
try {
|
|
133
|
+
// Step 0: Honor opt-outs whose server-side delete hasn't landed yet
|
|
134
|
+
// (e.g. the machine was offline when the user said "don't save this").
|
|
135
|
+
await this.retryRemoteDeletes();
|
|
65
136
|
// Step 1: Create any pending conversations
|
|
66
137
|
await this.createPendingConversations();
|
|
67
138
|
// Step 2: Flush messages for all conversations (with concurrency)
|
|
@@ -110,6 +181,16 @@ export class Syncer {
|
|
|
110
181
|
if (this.db.getConversationId(sessionId))
|
|
111
182
|
return;
|
|
112
183
|
const conversationId = await this.createConversation(sessionId, meta);
|
|
184
|
+
// Opt-out race (engram#462): the user may have said "don't save
|
|
185
|
+
// this" while the create was in flight — excludeSession saw no
|
|
186
|
+
// conversation id yet, so it couldn't schedule the remote delete.
|
|
187
|
+
// Schedule it now; the flush loop's retryRemoteDeletes handles it.
|
|
188
|
+
if (this.db.isSessionExcluded(sessionId)) {
|
|
189
|
+
this.db.markSessionExcluded(sessionId, "in-chat opt-out", conversationId);
|
|
190
|
+
this.db.deleteAllForConversation(`pending:${sessionId}`);
|
|
191
|
+
this.db.deleteAllForConversation(conversationId);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
113
194
|
// Migrate pending messages from pending:sessionId → real conversationId
|
|
114
195
|
this.db.remapPending(`pending:${sessionId}`, conversationId);
|
|
115
196
|
}));
|
|
@@ -123,7 +204,7 @@ export class Syncer {
|
|
|
123
204
|
return;
|
|
124
205
|
}
|
|
125
206
|
const errType = classifyError(msg);
|
|
126
|
-
if (errType === "billing") {
|
|
207
|
+
if (errType === "billing" || errType === "storage_full") {
|
|
127
208
|
this.enterBillingBackoff(msg);
|
|
128
209
|
return;
|
|
129
210
|
}
|
|
@@ -133,15 +214,35 @@ export class Syncer {
|
|
|
133
214
|
}
|
|
134
215
|
}
|
|
135
216
|
}
|
|
217
|
+
/** Per-conversation reduced batch size after a failed send (halves down to
|
|
218
|
+
* 1 so even a single pathological message eventually goes alone; restored
|
|
219
|
+
* to the full batch on the next success). */
|
|
220
|
+
batchOverride = new Map();
|
|
136
221
|
async flushConversation(conversationId) {
|
|
137
222
|
while (true) {
|
|
138
|
-
const
|
|
223
|
+
const limit = this.batchOverride.get(conversationId) ?? BATCH_SIZE;
|
|
224
|
+
let rows = this.db.dequeue(conversationId, limit);
|
|
139
225
|
if (rows.length === 0)
|
|
140
226
|
break;
|
|
227
|
+
// Byte budget: transcript messages vary wildly in size — trim the batch
|
|
228
|
+
// so its total content stays inside the budget. Always send at least
|
|
229
|
+
// one row, however large (the server accepts it; it just needs time).
|
|
230
|
+
let bytes = 0;
|
|
231
|
+
let take = 0;
|
|
232
|
+
for (const r of rows) {
|
|
233
|
+
bytes += r.content.length;
|
|
234
|
+
take++;
|
|
235
|
+
if (bytes > BATCH_BYTE_BUDGET && take > 1) {
|
|
236
|
+
take--;
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
rows = rows.slice(0, take);
|
|
141
241
|
const messages = rows.map(rowToMessage);
|
|
142
242
|
try {
|
|
143
243
|
await this.client.store({ conversationId, messages });
|
|
144
244
|
this.db.markSent(rows.map((r) => r.id));
|
|
245
|
+
this.batchOverride.delete(conversationId);
|
|
145
246
|
}
|
|
146
247
|
catch (err) {
|
|
147
248
|
const msg = err instanceof Error ? err.message : String(err);
|
|
@@ -154,23 +255,51 @@ export class Syncer {
|
|
|
154
255
|
}
|
|
155
256
|
// Billing / limit errors — exponential backoff
|
|
156
257
|
const errType = classifyError(msg);
|
|
157
|
-
if (errType === "billing") {
|
|
258
|
+
if (errType === "billing" || errType === "storage_full") {
|
|
158
259
|
this.enterBillingBackoff(msg);
|
|
159
260
|
return;
|
|
160
261
|
}
|
|
161
|
-
// Rate limit or network — leave in queue for next cycle
|
|
162
|
-
|
|
262
|
+
// Rate limit or network/timeout — leave in queue for next cycle, but
|
|
263
|
+
// halve this conversation's batch so an oversized batch self-resolves
|
|
264
|
+
// instead of retrying the identical payload forever.
|
|
265
|
+
if (rows.length > 1) {
|
|
266
|
+
const next = Math.max(1, Math.floor(rows.length / 2));
|
|
267
|
+
this.batchOverride.set(conversationId, next);
|
|
268
|
+
console.error(`[engram] send failed (will retry with batch ${next}): ${msg}`);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
console.error(`[engram] send failed (will retry): ${msg}`);
|
|
272
|
+
}
|
|
163
273
|
recordFailure(msg, errType, this.db.getPendingCount());
|
|
164
274
|
return;
|
|
165
275
|
}
|
|
166
276
|
}
|
|
167
277
|
}
|
|
168
|
-
/**
|
|
278
|
+
/**
|
|
279
|
+
* Enter billing backoff — escalates each time: 5min → 15min → 1hr.
|
|
280
|
+
*
|
|
281
|
+
* A full memory is separated out here. It arrives as a 402 like any other
|
|
282
|
+
* billing error, but retrying can never clear it on its own: it needs an
|
|
283
|
+
* upgrade or deleted conversations. It gets its own status state (so the
|
|
284
|
+
* CLI can say "memory is full" rather than "plan limit reached") and its
|
|
285
|
+
* used/limit numbers are carried through, so the banner can name them
|
|
286
|
+
* even when the machine is offline.
|
|
287
|
+
*/
|
|
169
288
|
enterBillingBackoff(msg) {
|
|
170
289
|
const delay = BILLING_BACKOFF_MS[Math.min(this.billingBackoffStep, BILLING_BACKOFF_MS.length - 1)];
|
|
171
290
|
this.billingBackoffUntil = Date.now() + delay;
|
|
172
291
|
this.billingBackoffStep++;
|
|
292
|
+
const storageFull = parseStorageFullError(msg);
|
|
173
293
|
const mins = Math.round(delay / 60_000);
|
|
294
|
+
if (storageFull) {
|
|
295
|
+
console.error(`[engram] memory full (${storageFull.used ?? "?"}/${storageFull.limit ?? "?"}), ` +
|
|
296
|
+
`pausing sync ${mins}m — new captures stay queued until space is freed`);
|
|
297
|
+
recordFailure(msg, "storage_full", this.db.getPendingCount(), {
|
|
298
|
+
used: storageFull.used,
|
|
299
|
+
limit: storageFull.limit,
|
|
300
|
+
});
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
174
303
|
console.error(`[engram] billing limit hit, backing off ${mins}m (attempt ${this.billingBackoffStep}): ${msg}`);
|
|
175
304
|
recordFailure(msg, "billing", this.db.getPendingCount());
|
|
176
305
|
}
|