claude-mem-lite 6.35.0 → 6.35.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.
@@ -62,48 +62,60 @@ const MEMDIR_WRITE_RE = /[\\/]\.claude[\\/]projects[\\/][^\\/]+[\\/]memory[\\/][
62
62
  * `save` / `defer add`) in the session transcript. 0 on missing/unreadable.
63
63
  */
64
64
  export function countDeliberatePersistence(transcriptPath) {
65
- // Shares the Stop-time parse (lib/transcript-scan.mjs) rather than doing its own
66
- // read+split+parse. It was the NINTH scanner of the same file and was missed when the
67
- // other eight were collapsed — sitting two lines away from two that were migrated, so
68
- // it was quietly charging a full re-parse (~20-25ms on a 5.7MB transcript) against a
69
- // pass whose whole point was to stop doing that. Missing/unreadable paths still yield
70
- // 0, because readTranscriptEntries returns [] for them.
65
+ // Missing/unreadable paths yield 0, because readTranscriptEntries returns [] for them. The
66
+ // Stop hook does not call this: it folds deliberatePersistenceIn over the appended records
67
+ // (lib/transcript-digest.mjs).
71
68
  let count = 0;
72
- for (const entry of readTranscriptEntries(transcriptPath)) {
73
- if (entry.type !== 'assistant' && entry.message?.role !== 'assistant') continue;
74
- const content = entry.message?.content;
75
- if (!Array.isArray(content)) continue;
76
- for (const block of content) {
77
- if (block.type !== 'tool_use') continue;
78
- if (PERSIST_TOOL_NAMES.has(block.name)) {
79
- count++;
80
- continue;
81
- }
82
- if (block.name === 'Bash' && PERSIST_CLI_RE.test(block.input?.command || '')) {
83
- count++;
84
- continue;
85
- }
86
- if (block.name === 'Skill' && PERSIST_SKILL_RE.test(block.input?.skill || '')) {
87
- count++;
88
- continue;
89
- }
90
- if (
91
- (block.name === 'Write' || block.name === 'Edit') &&
92
- MEMDIR_WRITE_RE.test(block.input?.file_path || '')
93
- )
94
- count++;
69
+ for (const entry of readTranscriptEntries(transcriptPath)) count += deliberatePersistenceIn(entry);
70
+ return count;
71
+ }
72
+
73
+ /**
74
+ * countDeliberatePersistence's rule for ONE transcript record: how many deliberate
75
+ * persistence calls it makes.
76
+ * @param {object} entry one parsed transcript record
77
+ * @returns {number}
78
+ */
79
+ export function deliberatePersistenceIn(entry) {
80
+ if (entry.type !== 'assistant' && entry.message?.role !== 'assistant') return 0;
81
+ const content = entry.message?.content;
82
+ if (!Array.isArray(content)) return 0;
83
+ let count = 0;
84
+ for (const block of content) {
85
+ if (block.type !== 'tool_use') continue;
86
+ if (PERSIST_TOOL_NAMES.has(block.name)) {
87
+ count++;
88
+ continue;
89
+ }
90
+ if (block.name === 'Bash' && PERSIST_CLI_RE.test(block.input?.command || '')) {
91
+ count++;
92
+ continue;
93
+ }
94
+ if (block.name === 'Skill' && PERSIST_SKILL_RE.test(block.input?.skill || '')) {
95
+ count++;
96
+ continue;
95
97
  }
98
+ if (
99
+ (block.name === 'Write' || block.name === 'Edit') &&
100
+ MEMDIR_WRITE_RE.test(block.input?.file_path || '')
101
+ )
102
+ count++;
96
103
  }
97
104
  return count;
98
105
  }
99
106
 
100
107
  /**
101
108
  * The G3 gate: finalization signal present AND zero deliberate persistence.
109
+ * @param {{prompts: string[], persistenceCount?: number, transcriptPath?: string}} input
110
+ * persistenceCount is the session's countDeliberatePersistence. Without it the count is taken
111
+ * from transcriptPath: v6.35.0's hook.mjs passes that, and an update swaps this file first.
102
112
  * @returns {{fire: boolean, signal: string|null}}
103
113
  */
104
- export function detectUnpersistedDecision({ prompts, transcriptPath }) {
114
+ export function detectUnpersistedDecision({ prompts, persistenceCount, transcriptPath }) {
105
115
  const signal = detectFinalization(prompts);
106
116
  if (!signal) return { fire: false, signal: null };
107
- if (countDeliberatePersistence(transcriptPath) > 0) return { fire: false, signal };
117
+ const count =
118
+ typeof persistenceCount === 'number' ? persistenceCount : countDeliberatePersistence(transcriptPath);
119
+ if (count > 0) return { fire: false, signal };
108
120
  return { fire: true, signal };
109
121
  }
@@ -9,7 +9,7 @@
9
9
  // with empty remaining_items. This extractor runs synchronously in
10
10
  // handleStop and gives a deterministic floor.
11
11
 
12
- import { readTranscriptEntries } from './transcript-scan.mjs';
12
+ import { closeSync, constants, fstatSync, openSync, readSync } from 'fs';
13
13
 
14
14
  const ZH_HEADER = /^[\s●*>-]*(剩下的?|剩余|还剩|未完成|下次(?:要做|做|继续)?|待做|未做)\s*[::]?\s*/m;
15
15
 
@@ -107,22 +107,82 @@ const ZH_KEY_IS_NOTDONE = /剩下|剩余|还剩|未完成|下次|待做|未做/;
107
107
  * Returns concatenated text of all text blocks in the last `type='assistant'`
108
108
  * entry, or null if the file is missing/empty/malformed.
109
109
  *
110
+ * Read from the end of the file backwards, a chunk at a time, so the cost is the distance from
111
+ * the end to that entry rather than the size of the transcript: Stop calls this every turn on a
112
+ * host that does not send `last_assistant_message`, and a forward parse of the whole file took
113
+ * 1.6 s on a 475 MB transcript (issue #53). The lines and the answer are the forward parse's:
114
+ * a line that does not parse is skipped, as a partly written last one is.
115
+ *
110
116
  * @param {string} transcriptPath
111
117
  * @returns {string|null}
112
118
  */
113
119
  export function extractTailAssistantText(transcriptPath) {
114
- let last = null;
115
- for (const entry of readTranscriptEntries(transcriptPath)) {
116
- if (entry.type !== 'assistant' || !entry.message) continue;
117
- const content = entry.message.content;
118
- if (!Array.isArray(content)) continue;
119
- const texts = content
120
- .filter((b) => b && b.type === 'text' && typeof b.text === 'string')
121
- .map((b) => b.text);
122
- if (texts.length === 0) continue;
123
- last = texts.join('\n');
120
+ if (!transcriptPath) return null;
121
+ let fd;
122
+ try {
123
+ fd = openSync(transcriptPath, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
124
+ } catch {
125
+ return null;
126
+ }
127
+ try {
128
+ const st = fstatSync(fd);
129
+ if (!st.isFile()) return null;
130
+ let pos = st.size;
131
+ let chunkSize = TAIL_FIRST_CHUNK;
132
+ // The end of the line being assembled: bytes after the last newline examined, in file order.
133
+ let tailPieces = [];
134
+ while (pos > 0) {
135
+ const start = Math.max(0, pos - chunkSize);
136
+ chunkSize = Math.min(chunkSize * 2, TAIL_MAX_CHUNK);
137
+ const chunk = Buffer.alloc(pos - start);
138
+ let got = 0;
139
+ while (got < chunk.length) {
140
+ const n = readSync(fd, chunk, got, chunk.length - got, start + got);
141
+ if (n <= 0) return null; // the file shrank under us
142
+ got += n;
143
+ }
144
+ let j = chunk.length;
145
+ for (;;) {
146
+ const nl = j === 0 ? -1 : chunk.lastIndexOf(0x0a, j - 1);
147
+ if (nl === -1 && start > 0) {
148
+ tailPieces.unshift(chunk.subarray(0, j)); // this line started before the chunk
149
+ break;
150
+ }
151
+ const head = chunk.subarray(nl + 1, j);
152
+ const text = assistantTextOf(tailPieces.length > 0 ? Buffer.concat([head, ...tailPieces]) : head);
153
+ if (text !== null) return text;
154
+ tailPieces = [];
155
+ if (nl === -1) return null; // the first line of the file
156
+ j = nl;
157
+ }
158
+ pos = start;
159
+ }
160
+ return null;
161
+ } catch {
162
+ return null;
163
+ } finally {
164
+ closeSync(fd);
165
+ }
166
+ }
167
+
168
+ const TAIL_FIRST_CHUNK = 64 * 1024;
169
+ const TAIL_MAX_CHUNK = 4 * 1024 * 1024;
170
+
171
+ /** The joined text blocks of one transcript line when it is an assistant entry with any; else null. */
172
+ function assistantTextOf(line) {
173
+ let entry;
174
+ try {
175
+ entry = JSON.parse(line.toString('utf8'));
176
+ } catch {
177
+ return null;
124
178
  }
125
- return last;
179
+ if (!entry || entry.type !== 'assistant' || !entry.message) return null;
180
+ const content = entry.message.content;
181
+ if (!Array.isArray(content)) return null;
182
+ const texts = content
183
+ .filter((b) => b && b.type === 'text' && typeof b.text === 'string')
184
+ .map((b) => b.text);
185
+ return texts.length === 0 ? null : texts.join('\n');
126
186
  }
127
187
 
128
188
  /**
@@ -0,0 +1,350 @@
1
+ // Stop's transcript questions, answered from the bytes appended since the previous Stop.
2
+ //
3
+ // Issue #53. Stop fires once per assistant turn, and it answered its citation questions by
4
+ // parsing the whole transcript: lib/transcript-scan.mjs shares one parse within a process,
5
+ // and above its heap budget (256 MB at most) every scanner parsed for itself. A transcript
6
+ // only grows, so each Stop paid again for every byte the session had written.
7
+ //
8
+ // Here each question is an accumulator folded over records with the scanner's own rule for
9
+ // one record (addCitationsOf, addInjectedBySurfaceOf, ...: one implementation, two callers),
10
+ // and the accumulators are kept in the runtime dir with the byte offset they cover. A Stop
11
+ // parses only what was appended since. It stops taking new lines once those it has taken reach
12
+ // `maxBytes` over all files (a line is taken whole, and reads go in 4 MiB chunks), so a
13
+ // transcript first seen larger than that is caught up over several Stops; until it is,
14
+ // `complete` is false and the caller writes nothing, because a cited set read from part of
15
+ // the session would decide "uncited" for ids the rest of it cites. Catching up needs the next
16
+ // call to resume from this one's state. A call whose state cannot be saved still stops at the
17
+ // budget, which is what keeps Stop inside its hook timeout.
18
+ //
19
+ // The saved state is used only when it still describes the file: same inode, not longer than
20
+ // the file, the same first and last 4 KiB before its offset, written by this release of the
21
+ // code (a release that changes a scanner's rule invalidates every saved state), and holding
22
+ // accumulators of the shapes this code builds. Otherwise the file is read again from its first
23
+ // byte. Each release keeps its state under its own name, so two installed releases running
24
+ // Stop on one session (a plugin registered twice during an update) do not reset each other.
25
+
26
+ import { closeSync, constants, fstatSync, openSync, readFileSync, readSync } from 'fs';
27
+ import { createHash } from 'crypto';
28
+ import { basename, dirname, join } from 'path';
29
+ import { fileURLToPath } from 'url';
30
+ import { atomicWriteFileSync } from './atomic-write.mjs';
31
+ import {
32
+ addCitationsOf,
33
+ addCiteRecallOf,
34
+ addInjectedBySurfaceOf,
35
+ addSubagentPromptIdsOf,
36
+ addUserTypedIdsOf,
37
+ emptySurfaceSets,
38
+ findSubagentTranscripts,
39
+ isMainThreadAssistantText,
40
+ } from './citation-tracker.mjs';
41
+ import { addBugfixShapeOf, addCiteBackSignalsOf } from './cite-back-hint.mjs';
42
+ import { deliberatePersistenceIn } from './persist-reminder.mjs';
43
+
44
+ export const DIGEST_FILE_PREFIX = 'transcript-digest-';
45
+
46
+ // Bytes of new lines one Stop takes, over the transcript and its subagent files together. The
47
+ // issue's reporter measured 1.6 s to parse 475 MB, so this is ~0.2 s of parsing there.
48
+ export const DIGEST_BYTES_PER_STOP = 64 * 1024 * 1024;
49
+
50
+ const DIGEST_FORMAT = 1;
51
+ const READ_CHUNK = 4 * 1024 * 1024;
52
+ const PROBE_BYTES = 4096;
53
+
54
+ /** The main transcript's questions: one accumulator each, named for what hook.mjs asks. */
55
+ const MAIN_KINDS = {
56
+ // extractCitationsFromTranscript(path)
57
+ cited: { init: () => new Set(), step: (s, e) => addCitationsOf(s, e) },
58
+ // extractCitationsFromTranscript(path, { mainOnly: true })
59
+ citedMain: { init: () => new Set(), step: (s, e) => addCitationsOf(s, e, { mainOnly: true }) },
60
+ // extractCitationsFromTranscript(path, { mainOnly: true, includeDismissed: true })
61
+ citedMainAnswered: {
62
+ init: () => new Set(),
63
+ step: (s, e) => addCitationsOf(s, e, { mainOnly: true, includeDismissed: true }),
64
+ },
65
+ // extractInjectedBySurface(path)
66
+ injected: { init: emptySurfaceSets, step: (s, e) => addInjectedBySurfaceOf(s, e) },
67
+ // extractInjectedBySurface(path, { mainOnly: true })
68
+ injectedMain: { init: emptySurfaceSets, step: (s, e) => addInjectedBySurfaceOf(s, e, { mainOnly: true }) },
69
+ // extractUserTypedIds(path)
70
+ userTyped: { init: () => new Set(), step: (s, e) => addUserTypedIdsOf(s, e) },
71
+ // extractCiteBackSignals(path)
72
+ citeBack: { init: () => new Set(), step: (s, e) => addCiteBackSignalsOf(s, e) },
73
+ // computeCiteRecall(path), before citeRecallStats
74
+ citeRecall: {
75
+ init: () => ({ injected: new Set(), cited: new Set() }),
76
+ step: (s, e) => addCiteRecallOf(s, e),
77
+ },
78
+ // countUnsavedBugfixShape(path), before bugfixShapeStats
79
+ bugfixShape: { init: () => ({ nudged: 0, saved: 0 }), step: (s, e) => addBugfixShapeOf(s, e) },
80
+ // hasMainThreadAssistantText(path)
81
+ mainText: {
82
+ init: () => ({ seen: false }),
83
+ step: (s, e) => {
84
+ if (!s.seen && isMainThreadAssistantText(e)) s.seen = true;
85
+ },
86
+ },
87
+ // countDeliberatePersistence(path)
88
+ persistence: {
89
+ init: () => ({ count: 0 }),
90
+ step: (s, e) => {
91
+ s.count += deliberatePersistenceIn(e);
92
+ },
93
+ },
94
+ };
95
+
96
+ /** Each subagent file's questions, as collectSubagentSurface asks them. */
97
+ const SUBAGENT_KINDS = {
98
+ // extractInjectedFromSubagentPrompt(file)
99
+ seen: { init: () => new Set(), step: (s, e) => addSubagentPromptIdsOf(s, e) },
100
+ // extractCitationsFromTranscript(file)
101
+ said: { init: () => new Set(), step: (s, e) => addCitationsOf(s, e) },
102
+ };
103
+
104
+ /**
105
+ * The transcript's answers to Stop's questions, reading only the bytes appended since the state
106
+ * saved in `runtimeDir` (all of them when there is none).
107
+ *
108
+ * @param {string} transcriptPath main transcript (.jsonl); its subagent files are found from it
109
+ * @param {object} [opts]
110
+ * @param {string} [opts.runtimeDir] where the state is kept; without it every call reads from
111
+ * the first byte
112
+ * @param {number} [opts.maxBytes] bytes of new lines this call may take. A test seam; Stop passes nothing.
113
+ * @returns {{complete: boolean, main: object, subagents: Array<{seen: Set<number>, said: Set<number>}>}}
114
+ * `main` has one entry per MAIN_KINDS key. `complete` is false when the budget ran out before
115
+ * every file was read to its end.
116
+ */
117
+ export function digestTranscript(transcriptPath, { runtimeDir, maxBytes = DIGEST_BYTES_PER_STOP } = {}) {
118
+ const code = digestCodeIdentity();
119
+ const statePath = runtimeDir && code ? join(runtimeDir, digestFileName(transcriptPath, code)) : null;
120
+ const saved = statePath ? loadState(statePath, code, transcriptPath) : null;
121
+ const buf = Buffer.allocUnsafe(READ_CHUNK);
122
+ const pass = digestPass(transcriptPath, saved?.files, { left: maxBytes }, buf);
123
+ saveState(statePath, code, transcriptPath, pass.files);
124
+ return { complete: pass.complete, main: pass.files.main.acc, subagents: pass.subagents };
125
+ }
126
+
127
+ /**
128
+ * One read over the transcript and its subagent files, resuming each from its record in `prev`.
129
+ * @returns {{complete: boolean, files: object, subagents: object[]}}
130
+ */
131
+ function digestPass(transcriptPath, prev, budget, buf) {
132
+ const main = advance(transcriptPath, prev?.main, MAIN_KINDS, budget, buf);
133
+ const files = { main: main.rec };
134
+ let complete = main.done;
135
+ const subagents = [];
136
+ for (const p of findSubagentTranscripts(transcriptPath)) {
137
+ const key = `subagents/${basename(p)}`;
138
+ let r;
139
+ try {
140
+ r = advance(p, prev?.[key], SUBAGENT_KINDS, budget, buf);
141
+ } catch {
142
+ // D#854 F10: a record one of the subagent rules cannot read. The file adds nothing and
143
+ // keeps no record, and the main results are still written. v6.35.0 did the same for the
144
+ // whole subagent face.
145
+ continue;
146
+ }
147
+ files[key] = r.rec;
148
+ if (!r.done) complete = false;
149
+ subagents.push(r.rec.acc);
150
+ }
151
+ return { complete, files, subagents };
152
+ }
153
+
154
+ /** Keep `files` for the next call; a state that cannot be written is skipped. */
155
+ function saveState(statePath, code, transcriptPath, files) {
156
+ if (!statePath) return;
157
+ try {
158
+ atomicWriteFileSync(
159
+ statePath,
160
+ JSON.stringify({ v: DIGEST_FORMAT, code, path: transcriptPath, files }, toJson),
161
+ { mode: 0o600 },
162
+ );
163
+ } catch {
164
+ /* this call's progress is not kept */
165
+ }
166
+ }
167
+
168
+ /** The state file's name for one transcript path, as kept by one release of the code (in the runtime dir). */
169
+ export function digestFileName(transcriptPath, code = digestCodeIdentity()) {
170
+ const key = `${code}\n${transcriptPath}`;
171
+ return `${DIGEST_FILE_PREFIX}${createHash('sha256').update(key).digest('hex').slice(0, 16)}.json`;
172
+ }
173
+
174
+ let codeIdentity;
175
+ /** Which code wrote a state: the package version, or null when it cannot be read (then no state is kept). */
176
+ function digestCodeIdentity() {
177
+ if (codeIdentity !== undefined) return codeIdentity;
178
+ try {
179
+ const root = join(dirname(fileURLToPath(import.meta.url)), '..');
180
+ const { version } = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
181
+ codeIdentity = typeof version === 'string' && version ? `${DIGEST_FORMAT}:${version}` : null;
182
+ } catch {
183
+ codeIdentity = null;
184
+ }
185
+ return codeIdentity;
186
+ }
187
+
188
+ function loadState(statePath, code, transcriptPath) {
189
+ let fd;
190
+ try {
191
+ // As for the transcript in advance(): a FIFO at this path would block a plain open (D#854
192
+ // F9). Opened this way it reads as empty, or throws EAGAIN while a writer holds it open.
193
+ fd = openSync(statePath, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
194
+ const s = JSON.parse(readFileSync(fd, 'utf8'), fromJson);
195
+ if (s?.v !== DIGEST_FORMAT || s.code !== code || s.path !== transcriptPath) return null;
196
+ return s && typeof s.files === 'object' ? s : null;
197
+ } catch {
198
+ return null;
199
+ } finally {
200
+ if (fd !== undefined) closeSync(fd);
201
+ }
202
+ }
203
+
204
+ // Sets are kept as {"$set": [...]}: no accumulator holds an object with that one key otherwise.
205
+ function toJson(_k, v) {
206
+ return v instanceof Set ? { $set: [...v] } : v;
207
+ }
208
+ function fromJson(_k, v) {
209
+ return v && typeof v === 'object' && !Array.isArray(v) && Array.isArray(v.$set) ? new Set(v.$set) : v;
210
+ }
211
+
212
+ function freshRecord(kinds, ino) {
213
+ const acc = {};
214
+ for (const k of Object.keys(kinds)) acc[k] = kinds[k].init();
215
+ return { ino, offset: 0, head: '', tail: '', acc };
216
+ }
217
+
218
+ /**
219
+ * Fold the records appended to `path` since `saved` into its accumulators.
220
+ * @returns {{rec: object, done: boolean}} done = every complete line up to the end of the
221
+ * file has been read (a partly written last line is left for the next call)
222
+ */
223
+ function advance(path, saved, kinds, budget, buf) {
224
+ let fd;
225
+ try {
226
+ // O_NONBLOCK: a FIFO at this path would otherwise block the open. It has no effect on a
227
+ // regular file, and a non-file is treated as an empty transcript below.
228
+ fd = openSync(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
229
+ } catch {
230
+ return { rec: freshRecord(kinds, null), done: true }; // missing: no records, as readTranscriptEntries
231
+ }
232
+ try {
233
+ const st = fstatSync(fd);
234
+ if (!st.isFile()) return { rec: freshRecord(kinds, null), done: true };
235
+ const rec = continues(fd, st, saved, kinds) ? saved : freshRecord(kinds, st.ino);
236
+ rec.ino = st.ino;
237
+ const done = readAppended(fd, st.size, rec, kinds, budget, buf);
238
+ rec.head = hashRange(fd, 0, Math.min(PROBE_BYTES, rec.offset));
239
+ rec.tail = hashRange(fd, Math.max(0, rec.offset - PROBE_BYTES), rec.offset);
240
+ return { rec, done };
241
+ } finally {
242
+ closeSync(fd);
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Whether `saved` still describes the first `saved.offset` bytes of the open file. A file now
248
+ * shorter than the offset fails the tail probe: it reads fewer bytes than were hashed.
249
+ */
250
+ function continues(fd, st, saved, kinds) {
251
+ if (!saved || typeof saved !== 'object' || !saved.acc || typeof saved.acc !== 'object') return false;
252
+ if (!Object.keys(kinds).every((k) => sameShape(kinds[k].init(), saved.acc[k]))) return false;
253
+ if (saved.ino !== st.ino || !Number.isInteger(saved.offset) || saved.offset < 0) return false;
254
+ return (
255
+ hashRange(fd, 0, Math.min(PROBE_BYTES, saved.offset)) === saved.head &&
256
+ hashRange(fd, Math.max(0, saved.offset - PROBE_BYTES), saved.offset) === saved.tail
257
+ );
258
+ }
259
+
260
+ /**
261
+ * Whether `v` has the shape of `model`, an accumulator's init(): a Set where it has a Set, an
262
+ * object with its keys where it has an object, its primitive type otherwise. The version check
263
+ * passes a state whose accumulators changed shape under one version string (D#854 F9), and a
264
+ * rule folded into one of those throws on every Stop until the state is collected.
265
+ */
266
+ function sameShape(model, v) {
267
+ if (model instanceof Set) return v instanceof Set;
268
+ if (model !== null && typeof model === 'object') {
269
+ return (
270
+ v !== null &&
271
+ typeof v === 'object' &&
272
+ !(v instanceof Set) &&
273
+ Object.keys(model).every((k) => sameShape(model[k], v[k]))
274
+ );
275
+ }
276
+ return typeof v === typeof model;
277
+ }
278
+
279
+ function hashRange(fd, start, end) {
280
+ const len = end - start;
281
+ const b = Buffer.alloc(len);
282
+ let got = 0;
283
+ while (got < len) {
284
+ const n = readSync(fd, b, got, len - got, start + got);
285
+ if (n <= 0) break;
286
+ got += n;
287
+ }
288
+ return createHash('sha256').update(b.subarray(0, got)).digest('hex');
289
+ }
290
+
291
+ /**
292
+ * Read whole lines from `rec.offset` up to `size`, feeding each record to every accumulator,
293
+ * and move `rec.offset` past each line it consumes. Lines are split on the byte 0x0A, which
294
+ * UTF-8 never uses inside a character, and decoded one at a time.
295
+ * @returns {boolean} whether it reached `size`
296
+ */
297
+ function readAppended(fd, size, rec, kinds, budget, buf) {
298
+ let readPos = rec.offset;
299
+ let pending = []; // pieces of a line that started in an earlier chunk
300
+ let pendingLen = 0;
301
+ while (readPos < size) {
302
+ if (budget.left <= 0) return false;
303
+ const n = readSync(fd, buf, 0, Math.min(buf.length, size - readPos), readPos);
304
+ if (n <= 0) break;
305
+ const view = buf.subarray(0, n);
306
+ let i = 0;
307
+ while (i < n) {
308
+ const nl = view.indexOf(0x0a, i);
309
+ if (nl === -1) {
310
+ pending.push(Buffer.from(view.subarray(i))); // copied: `buf` is reused for the next chunk
311
+ pendingLen += n - i;
312
+ break;
313
+ }
314
+ const line = pendingLen > 0 ? Buffer.concat([...pending, view.subarray(i, nl)]) : view.subarray(i, nl);
315
+ pending = [];
316
+ pendingLen = 0;
317
+ feed(line, kinds, rec.acc);
318
+ const lineEnd = readPos + nl + 1;
319
+ budget.left -= lineEnd - rec.offset;
320
+ rec.offset = lineEnd;
321
+ i = nl + 1;
322
+ if (budget.left <= 0 && rec.offset < size) return false;
323
+ }
324
+ readPos += n;
325
+ }
326
+ // A last line without its newline: a record still being written, or one that is complete.
327
+ // readTranscriptEntries keeps it when it parses and drops it when it does not; the same here,
328
+ // except that a line that does not parse yet is left in place to be read whole next time.
329
+ if (pendingLen > 0 && feed(Buffer.concat(pending), kinds, rec.acc)) {
330
+ budget.left -= size - rec.offset;
331
+ rec.offset = size;
332
+ }
333
+ return true;
334
+ }
335
+
336
+ /**
337
+ * Parse one line and fold the record into every accumulator.
338
+ * @returns {boolean} whether the line held a record
339
+ */
340
+ function feed(line, kinds, acc) {
341
+ let entry;
342
+ try {
343
+ entry = JSON.parse(line.toString('utf8'));
344
+ } catch {
345
+ return false; // blank, or a partly written line: readTranscriptEntries drops these too
346
+ }
347
+ if (!entry || typeof entry !== 'object') return false;
348
+ for (const k of Object.keys(kinds)) kinds[k].step(acc[k], entry);
349
+ return true;
350
+ }
@@ -11,6 +11,10 @@
11
11
  //
12
12
  // This is a memo, not a rewrite: each scanner keeps its own per-entry logic exactly as
13
13
  // it was, and only stops re-reading and re-parsing the file to get at it.
14
+ //
15
+ // Since issue #53 the Stop hook does not read through this module: lib/transcript-digest.mjs
16
+ // folds the same per-entry rules over the bytes appended since the previous Stop. The path
17
+ // scanners (benchmarks, CLI, tests) still parse here.
14
18
  import { readFileSync, existsSync, statSync } from 'fs';
15
19
  import { getHeapStatistics } from 'v8';
16
20
 
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.35.0",
3
+ "version": "6.35.2",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "claude-mem-lite",
9
- "version": "6.35.0",
9
+ "version": "6.35.2",
10
10
  "os": [
11
11
  "darwin",
12
12
  "linux",
@@ -36,9 +36,9 @@
36
36
  }
37
37
  },
38
38
  "node_modules/@hono/node-server": {
39
- "version": "2.1.5",
40
- "resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-2.1.5.tgz",
41
- "integrity": "sha512-MgP/I06GdvsilDspMk/qmRzpk8LOUStP6jFAbSRi40W09DhoyUn+Mj7eyKICPqeYKybOjVFLVIAQzvu1bfIa7Q==",
39
+ "version": "2.1.8",
40
+ "resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-2.1.8.tgz",
41
+ "integrity": "sha512-jdf7FHh6A1lzjpHHQUgxsg2GeER2vMetnAOfeActzcoli6jamlK+NAwL6Dsr5Fl3BS3tYUQzCdtZjU8Qxv7bEA==",
42
42
  "license": "MIT",
43
43
  "engines": {
44
44
  "node": ">=20"
@@ -617,9 +617,9 @@
617
617
  }
618
618
  },
619
619
  "node_modules/hono": {
620
- "version": "4.13.13",
621
- "resolved": "https://registry.npmjs.org/hono/-/hono-4.13.13.tgz",
622
- "integrity": "sha512-CQ46U0ZkAGmbT/4UxdzzGJpacP2IeKgY4a5/tOI9AABbpOMfK739wfDXmv1usCk+3RkKj1hQy4/fjhiwa2xlrA==",
620
+ "version": "4.13.18",
621
+ "resolved": "https://registry.npmjs.org/hono/-/hono-4.13.18.tgz",
622
+ "integrity": "sha512-H9q/E1XizaWstJxUW881dmLxEj+t/TrGGnyxINkm4+sK0qUwsMtFdo2BWI9Vs3cIS19MU6b6iCijoUoEf4f3DA==",
623
623
  "license": "MIT",
624
624
  "engines": {
625
625
  "node": ">=16.9.0"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.35.0",
3
+ "version": "6.35.2",
4
4
  "description": "Persistent long-term memory for Claude Code via MCP — captures coding decisions, bugfixes, and context across sessions. FTS5 BM25 keyword search with episode batching. Single SQLite DB, no external services. A lighter, lower-cost alternative to claude-mem (episode batching + a smaller model; cost savings are an internal estimate, not a measured benchmark).",
5
5
  "type": "module",
6
6
  "packageManager": "npm@10.9.2",
@@ -173,6 +173,7 @@
173
173
  "lib/memory-input-guard.mjs",
174
174
  "lib/fast-summary.mjs",
175
175
  "lib/transcript-scan.mjs",
176
+ "lib/transcript-digest.mjs",
176
177
  "lib/ups-query.mjs",
177
178
  "lib/prompt-admission.mjs",
178
179
  "lib/dedup-constants.mjs",
package/source-files.mjs CHANGED
@@ -253,6 +253,10 @@ export const SOURCE_FILES = [
253
253
  // ERR_MODULE_NOT_FOUND on every Stop.
254
254
  'lib/edge-attribution.mjs',
255
255
  'lib/cite-back-hint.mjs',
256
+ // Issue #53: Stop's transcript questions read from the appended bytes only. Imports
257
+ // citation-tracker, cite-back-hint, persist-reminder and atomic-write (all above);
258
+ // statically imported by hook.mjs.
259
+ 'lib/transcript-digest.mjs',
256
260
  // D#124: Key Context marker write + injection_count bump, shared by
257
261
  // handleSessionStart and handlePreCompact. Statically imported by hook.mjs.
258
262
  'lib/keyctx-marker.mjs',