wendkeep 0.66.3 → 0.66.5

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,112 @@
1
+ import {
2
+ closeSync,
3
+ openSync,
4
+ readSync,
5
+ } from 'node:fs';
6
+
7
+ export const DEFAULT_CODEX_META_LINE_BYTES = 4 * 1024 * 1024;
8
+ const READ_CHUNK_BYTES = 64 * 1024;
9
+
10
+ function trimCarriageReturn(buffer) {
11
+ return buffer.length > 0 && buffer[buffer.length - 1] === 0x0d
12
+ ? buffer.subarray(0, buffer.length - 1)
13
+ : buffer;
14
+ }
15
+
16
+ // Codex writes session_meta as the first physical JSONL line. Read only that line: the
17
+ // transcript body can be hundreds of MiB and is irrelevant to identity/discovery.
18
+ export function readFirstJsonlLine(
19
+ path,
20
+ { maxBytes = DEFAULT_CODEX_META_LINE_BYTES } = {},
21
+ ) {
22
+ if (typeof path !== 'string' || !path) {
23
+ return { ok: false, reason: 'INVALID_PATH' };
24
+ }
25
+
26
+ const limit = Number(maxBytes);
27
+ if (!Number.isSafeInteger(limit) || limit <= 0) {
28
+ return { ok: false, reason: 'READ_ERROR' };
29
+ }
30
+
31
+ let fd;
32
+ try {
33
+ fd = openSync(path, 'r');
34
+ const parts = [];
35
+ let totalBytes = 0;
36
+
37
+ while (totalBytes <= limit) {
38
+ // The extra byte distinguishes an exactly-at-limit line followed by LF from a line
39
+ // whose content exceeds the limit.
40
+ const remaining = (limit + 1) - totalBytes;
41
+ const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, remaining));
42
+ const bytesRead = readSync(fd, chunk, 0, chunk.length, null);
43
+
44
+ if (bytesRead === 0) {
45
+ if (totalBytes === 0) return { ok: false, reason: 'EMPTY_FILE' };
46
+ const lineBuffer = trimCarriageReturn(Buffer.concat(parts, totalBytes));
47
+ return {
48
+ ok: true,
49
+ line: lineBuffer.toString('utf8'),
50
+ lineBytes: lineBuffer.length,
51
+ };
52
+ }
53
+
54
+ const bytes = chunk.subarray(0, bytesRead);
55
+ const newlineAt = bytes.indexOf(0x0a);
56
+ if (newlineAt !== -1) {
57
+ if (totalBytes + newlineAt > limit) {
58
+ return { ok: false, reason: 'LINE_TOO_LONG' };
59
+ }
60
+ parts.push(bytes.subarray(0, newlineAt));
61
+ const lineBuffer = trimCarriageReturn(Buffer.concat(parts, totalBytes + newlineAt));
62
+ if (lineBuffer.length === 0) return { ok: false, reason: 'EMPTY_FILE' };
63
+ return {
64
+ ok: true,
65
+ line: lineBuffer.toString('utf8'),
66
+ lineBytes: lineBuffer.length,
67
+ };
68
+ }
69
+
70
+ parts.push(bytes);
71
+ totalBytes += bytesRead;
72
+ if (totalBytes > limit) return { ok: false, reason: 'LINE_TOO_LONG' };
73
+ }
74
+
75
+ return { ok: false, reason: 'LINE_TOO_LONG' };
76
+ } catch {
77
+ return { ok: false, reason: 'READ_ERROR' };
78
+ } finally {
79
+ if (fd !== undefined) {
80
+ try { closeSync(fd); } catch { /* already closed */ }
81
+ }
82
+ }
83
+ }
84
+
85
+ export function readCodexRolloutMeta(
86
+ path,
87
+ { maxLineBytes = DEFAULT_CODEX_META_LINE_BYTES } = {},
88
+ ) {
89
+ const first = readFirstJsonlLine(path, { maxBytes: maxLineBytes });
90
+ if (!first.ok) return first;
91
+
92
+ let event;
93
+ try {
94
+ event = JSON.parse(first.line);
95
+ } catch {
96
+ return { ok: false, reason: 'INVALID_JSON' };
97
+ }
98
+
99
+ if (!event || typeof event !== 'object' || Array.isArray(event)
100
+ || event.type !== 'session_meta') {
101
+ return { ok: false, reason: 'NOT_SESSION_META' };
102
+ }
103
+ if (!event.payload || typeof event.payload !== 'object' || Array.isArray(event.payload)) {
104
+ return { ok: false, reason: 'INVALID_META' };
105
+ }
106
+
107
+ return {
108
+ ok: true,
109
+ meta: event.payload,
110
+ lineBytes: first.lineBytes,
111
+ };
112
+ }