@getengram/cli 0.7.0 → 0.7.2

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.
@@ -0,0 +1,108 @@
1
+ import * as zlib from "node:zlib";
2
+ import { readSync } from "node:fs";
3
+ import { decompress as fzstdDecompress } from "fzstd";
4
+ // DeepSeek Harness writes session logs as a concatenation of independent,
5
+ // checksummed Zstandard frames: one holding the header line, then one per
6
+ // durable append batch (engram#800). A frame is the unit of commit — a torn
7
+ // last frame is an append that never completed, and dsh truncates it away
8
+ // on repair — so this reads complete frames only, and every offset it hands
9
+ // out is a frame boundary.
10
+ //
11
+ // Node's one-shot zstdDecompressSync stops after the first frame, so frames
12
+ // are found here by walking their headers (RFC 8878 §3.1.1) without
13
+ // decoding blocks, then decoded one at a time. Each frame is handed on as
14
+ // one unit: its messages are queued together or not at all, so a failed
15
+ // queue write retries the frame without repeating any of it.
16
+ const ZSTD_MAGIC = 0xfd2fb528;
17
+ /** Skippable frames: 0x184D2A50–0x184D2A5F. */
18
+ const SKIPPABLE_MASK = 0xfffffff0;
19
+ const SKIPPABLE_MAGIC = 0x184d2a50;
20
+ const native = zlib.zstdDecompressSync;
21
+ /** Decodes one frame: Node's built-in decoder (Node ≥ 22.15 / 23.8), else
22
+ * fzstd's pure-JS one, so every supported Node can read dsh logs. */
23
+ export const decodeFrame = typeof native === "function"
24
+ ? (frame) => native(frame)
25
+ : (frame) => {
26
+ const out = fzstdDecompress(frame);
27
+ return Buffer.from(out.buffer, out.byteOffset, out.byteLength);
28
+ };
29
+ /**
30
+ * Byte length of the frame starting at `p`, or null when `buf` ends before
31
+ * the frame does (still being written, or torn). Throws when `p` doesn't
32
+ * start a frame: the file isn't a zstd frame stream, or is corrupt.
33
+ */
34
+ export function frameLength(buf, p) {
35
+ const len = buf.length;
36
+ if (p + 4 > len)
37
+ return null;
38
+ const magic = buf.readUInt32LE(p);
39
+ if ((magic & SKIPPABLE_MASK) >>> 0 === SKIPPABLE_MAGIC) {
40
+ if (p + 8 > len)
41
+ return null;
42
+ const total = 8 + buf.readUInt32LE(p + 4);
43
+ return p + total > len ? null : total;
44
+ }
45
+ if (magic !== ZSTD_MAGIC) {
46
+ throw new Error(`no zstd frame at byte ${p}`);
47
+ }
48
+ let q = p + 4;
49
+ if (q >= len)
50
+ return null;
51
+ const fhd = buf[q++];
52
+ if (fhd & 0x08)
53
+ throw new Error(`bad zstd frame header at byte ${p}`);
54
+ const fcsFlag = fhd >> 6;
55
+ const singleSegment = (fhd >> 5) & 1;
56
+ const hasChecksum = (fhd >> 2) & 1;
57
+ const dictIdFlag = fhd & 3;
58
+ q += singleSegment ? 0 : 1; // window descriptor
59
+ q += [0, 1, 2, 4][dictIdFlag];
60
+ q += [singleSegment ? 1 : 0, 2, 4, 8][fcsFlag];
61
+ for (;;) {
62
+ if (q + 3 > len)
63
+ return null;
64
+ const header = buf[q] | (buf[q + 1] << 8) | (buf[q + 2] << 16);
65
+ q += 3;
66
+ const last = header & 1;
67
+ const type = (header >> 1) & 3;
68
+ const size = header >>> 3;
69
+ if (type === 3)
70
+ throw new Error(`bad zstd block at byte ${q - 3}`);
71
+ q += type === 1 ? 1 : size; // RLE blocks carry one byte
72
+ if (q > len)
73
+ return null;
74
+ if (last)
75
+ break;
76
+ }
77
+ q += hasChecksum ? 4 : 0;
78
+ return q > len ? null : q - p;
79
+ }
80
+ /**
81
+ * Every complete frame lying wholly within [start, end) of an open file, as
82
+ * one unit each: its decoded text (lines joined by "\n") at its offset.
83
+ * `end` is just past the last complete frame (`start` when there is none).
84
+ * `start` must be a frame boundary — every offset this returns is one.
85
+ */
86
+ export function readZstdFrames(fd, start, end, decode = decodeFrame) {
87
+ if (end <= start)
88
+ return { lines: [], end: start };
89
+ const buf = Buffer.alloc(end - start);
90
+ const bytes = buf.subarray(0, readSync(fd, buf, 0, buf.length, start));
91
+ const lines = [];
92
+ let p = 0;
93
+ for (;;) {
94
+ const n = frameLength(bytes, p);
95
+ if (n === null)
96
+ break;
97
+ const frame = bytes.subarray(p, p + n);
98
+ // Skippable frames hold no content.
99
+ if (frame.readUInt32LE(0) === ZSTD_MAGIC) {
100
+ const text = decode(frame).toString("utf-8");
101
+ if (text.trim())
102
+ lines.push({ text, offset: start + p });
103
+ }
104
+ p += n;
105
+ }
106
+ return { lines, end: start + p };
107
+ }
108
+ //# sourceMappingURL=zstd-frames.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zstd-frames.js","sourceRoot":"","sources":["../../src/daemon/zstd-frames.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,UAAU,IAAI,eAAe,EAAE,MAAM,OAAO,CAAC;AAGtD,0EAA0E;AAC1E,0EAA0E;AAC1E,4EAA4E;AAC5E,0EAA0E;AAC1E,4EAA4E;AAC5E,2BAA2B;AAC3B,EAAE;AACF,4EAA4E;AAC5E,oEAAoE;AACpE,0EAA0E;AAC1E,wEAAwE;AACxE,6DAA6D;AAE7D,MAAM,UAAU,GAAG,UAAU,CAAC;AAC9B,+CAA+C;AAC/C,MAAM,cAAc,GAAG,UAAU,CAAC;AAClC,MAAM,eAAe,GAAG,UAAU,CAAC;AAInC,MAAM,MAAM,GAAI,IAA2D,CAAC,kBAAkB,CAAC;AAE/F;sEACsE;AACtE,MAAM,CAAC,MAAM,WAAW,GACtB,OAAO,MAAM,KAAK,UAAU;IAC1B,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC;IAC1B,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE;QACR,MAAM,GAAG,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QACnC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;IACjE,CAAC,CAAC;AAER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ENGRAM_API_KEY against the key saved in ~/.engram/config.json
3
+ * (engram#757). Set, it wins over config.json for every command run in that
4
+ * shell and for a daemon started from it, which keeps it for its lifetime.
5
+ * On Windows a user environment variable also reaches the daemon that starts
6
+ * at sign-in. `engram auth login` and `engram login` saved a key to
7
+ * config.json and said nothing when the variable held another one: the
8
+ * saved key went unused, the old one kept being sent and refused, and
9
+ * nothing named the variable as the reason.
10
+ */
11
+ /**
12
+ * ENGRAM_API_KEY when it is set to a key other than `saved`. Empty is
13
+ * unset, as the daemon reads it.
14
+ */
15
+ export declare function envKeyOtherThan(saved: string | undefined): string | undefined;
16
+ /**
17
+ * What to say once `saved` is in config.json while ENGRAM_API_KEY holds
18
+ * another key: that the variable wins, where, and how to use the saved key.
19
+ * Nothing when the variable is unset or holds the same key.
20
+ */
21
+ export declare function envKeyWinsLines(saved: string): string[];
22
+ /** Print envKeyWinsLines for `saved`, if there are any. */
23
+ export declare function warnIfEnvKeyWins(saved: string): void;
24
+ /**
25
+ * The line `engram auth status` and `engram whoami` add when ENGRAM_API_KEY
26
+ * is the key in use and config.json holds another, which is then unused.
27
+ */
28
+ export declare function savedKeyUnusedLine(saved: string | undefined): string | undefined;
29
+ //# sourceMappingURL=env-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env-key.d.ts","sourceRoot":"","sources":["../src/env-key.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAQH;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAG7E;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAevD;AAED,2DAA2D;AAC3D,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAEpD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAGhF"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * ENGRAM_API_KEY against the key saved in ~/.engram/config.json
3
+ * (engram#757). Set, it wins over config.json for every command run in that
4
+ * shell and for a daemon started from it, which keeps it for its lifetime.
5
+ * On Windows a user environment variable also reaches the daemon that starts
6
+ * at sign-in. `engram auth login` and `engram login` saved a key to
7
+ * config.json and said nothing when the variable held another one: the
8
+ * saved key went unused, the old one kept being sent and refused, and
9
+ * nothing named the variable as the reason.
10
+ */
11
+ import { engramCommand } from "./daemon/package-runner.js";
12
+ import { red } from "./output.js";
13
+ /** A key's first characters, as `engram auth status` shows them. */
14
+ const prefix = (key) => `${key.slice(0, 20)}...`;
15
+ /**
16
+ * ENGRAM_API_KEY when it is set to a key other than `saved`. Empty is
17
+ * unset, as the daemon reads it.
18
+ */
19
+ export function envKeyOtherThan(saved) {
20
+ const env = process.env.ENGRAM_API_KEY || undefined;
21
+ return env !== undefined && env !== saved ? env : undefined;
22
+ }
23
+ /**
24
+ * What to say once `saved` is in config.json while ENGRAM_API_KEY holds
25
+ * another key: that the variable wins, where, and how to use the saved key.
26
+ * Nothing when the variable is unset or holds the same key.
27
+ */
28
+ export function envKeyWinsLines(saved) {
29
+ const env = envKeyOtherThan(saved);
30
+ if (env === undefined)
31
+ return [];
32
+ const lines = [
33
+ `ENGRAM_API_KEY is set to another key (${prefix(env)}), and it wins over the one saved:`,
34
+ "commands run in this shell, and a daemon started from it, keep using ENGRAM_API_KEY.",
35
+ `To use the saved key, unset it, then run '${engramCommand("stop")}' and '${engramCommand("start")}'.`,
36
+ ];
37
+ if (process.platform === "win32") {
38
+ lines.push("If it is one of your Windows user environment variables, remove it there too:", "the daemon that starts when you sign in gets those.");
39
+ }
40
+ return lines;
41
+ }
42
+ /** Print envKeyWinsLines for `saved`, if there are any. */
43
+ export function warnIfEnvKeyWins(saved) {
44
+ for (const line of envKeyWinsLines(saved))
45
+ console.log(red(` ${line}`));
46
+ }
47
+ /**
48
+ * The line `engram auth status` and `engram whoami` add when ENGRAM_API_KEY
49
+ * is the key in use and config.json holds another, which is then unused.
50
+ */
51
+ export function savedKeyUnusedLine(saved) {
52
+ if (!saved || envKeyOtherThan(saved) === undefined)
53
+ return undefined;
54
+ return `~/.engram/config.json holds another key (${prefix(saved)}), unused while ENGRAM_API_KEY is set.`;
55
+ }
56
+ //# sourceMappingURL=env-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env-key.js","sourceRoot":"","sources":["../src/env-key.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAElC,oEAAoE;AACpE,MAAM,MAAM,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC;AAEjE;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,KAAyB;IACvD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,SAAS,CAAC;IACpD,OAAO,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC;AAC9D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,KAAa;IAC3C,MAAM,GAAG,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;IACnC,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IACjC,MAAM,KAAK,GAAG;QACZ,yCAAyC,MAAM,CAAC,GAAG,CAAC,oCAAoC;QACxF,sFAAsF;QACtF,6CAA6C,aAAa,CAAC,MAAM,CAAC,UAAU,aAAa,CAAC,OAAO,CAAC,IAAI;KACvG,CAAC;IACF,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QACjC,KAAK,CAAC,IAAI,CACR,+EAA+E,EAC/E,qDAAqD,CACtD,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,KAAK,MAAM,IAAI,IAAI,eAAe,CAAC,KAAK,CAAC;QAAE,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC;AAC3E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAyB;IAC1D,IAAI,CAAC,KAAK,IAAI,eAAe,CAAC,KAAK,CAAC,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IACrE,OAAO,4CAA4C,MAAM,CAAC,KAAK,CAAC,wCAAwC,CAAC;AAC3G,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getengram/cli",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "CLI for Engram — persistent memory for AI agents",
5
5
  "type": "module",
6
6
  "bin": {
@@ -30,6 +30,7 @@
30
30
  "dependencies": {
31
31
  "better-sqlite3": "^12.8.0",
32
32
  "chokidar": "^5.0.0",
33
+ "fzstd": "0.1.1",
33
34
  "@getengram/sdk": "0.2.0"
34
35
  },
35
36
  "devDependencies": {