claude-mem-lite 6.30.12 → 6.30.14

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.
@@ -9,7 +9,7 @@
9
9
  "plugins": [
10
10
  {
11
11
  "name": "claude-mem-lite",
12
- "version": "6.30.12",
12
+ "version": "6.30.14",
13
13
  "source": "./",
14
14
  "homepage": "https://github.com/sdsrss/claude-mem-lite",
15
15
  "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)."
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.30.12",
3
+ "version": "6.30.14",
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
  "author": {
6
6
  "name": "sdsrss"
@@ -67,6 +67,89 @@ function importedObsTitle(toolUse) {
67
67
  return scrubbed.slice(0, prefix.length + IMPORTED_TITLE_DETAIL_MAX);
68
68
  }
69
69
 
70
+ // A private-key marker, BEGIN or END: the key arm of utils.mjs's PRIVATE_MARK_RE.
71
+ const KEY_MARK_RE = /-----(BEGIN|END) [A-Z0-9 ]*PRIVATE KEY/;
72
+ // Every tag lib/private-strip.mjs pairs (`<private>`, `<private …>`, `</private>`), and a little more.
73
+ const PRIVATE_TAG_HINT_RE = /<\/?private[\s>]/i;
74
+ const STORED_PART_MAX = 4000;
75
+
76
+ /**
77
+ * One tool part (a tool_use input or a tool_result content) as it is stored: scrubbed, then cut
78
+ * to STORED_PART_MAX (scrub first, cut second, for the reason importedObsTitle gives). `json` says
79
+ * the part is a parsed value stored as its JSON text; otherwise `value` is the text itself.
80
+ *
81
+ * Two rules come before that scrub (2026-11-02: of 12 shape groups measured, import-jsonl stored 7
82
+ * and stores none with these rules):
83
+ * - A part keeps only what comes before its first private-key marker, in the order JSON writes it,
84
+ * object keys included: the string or key holding the marker is cut there and every later member
85
+ * is left out; a part whose first marker is an END is stored empty, since what precedes an END
86
+ * can be a key's tail. This covers keys the scrubber cannot read, such as the diff of a key that
87
+ * gained a passphrase.
88
+ * - Each string value of a parsed part is scrubbed as text before the JSON wrap. Scrubbing only the
89
+ * JSON text left every string one escape level deep, where the raw-text rules do not hold: a label
90
+ * at an escaped line start, a JSON object inside a string, a key printed one string per line. Not
91
+ * done when a string value holds a `<private>` tag, since a pair can span two strings and only the
92
+ * whole text pairs it. The whole-text scrub still runs: it reads object keys as labels.
93
+ * The JSON text is written by an explicit-stack walk, not by JSON.stringify: a JSON.stringify replacer
94
+ * recursed, so a part nested a few thousand levels deep threw and the whole transcript was lost
95
+ * (v6.30.13 pre-tag review r1), and on Node 22 plain JSON.stringify throws from about 4,200 levels.
96
+ */
97
+ function storedToolPart(value, json) {
98
+ let stopped = false;
99
+ let endFirst = false;
100
+ const cut = (s) => {
101
+ const m = KEY_MARK_RE.exec(s);
102
+ if (!m) return s;
103
+ stopped = true;
104
+ if (m[1] === 'END') endFirst = true;
105
+ return s.slice(0, m.index);
106
+ };
107
+ if (!json) {
108
+ const text = cut(value);
109
+ return endFirst ? '' : scrubSecrets(text).slice(0, STORED_PART_MAX);
110
+ }
111
+ const out = []; // pieces of the JSON text
112
+ const strings = []; // indexes in `out` of string values, still raw; quoted below
113
+ let tagged = false;
114
+ const leaf = (v) => {
115
+ if (typeof v !== 'string') return out.push(JSON.stringify(v));
116
+ const s = cut(v);
117
+ if (PRIVATE_TAG_HINT_RE.test(s)) tagged = true;
118
+ strings.push(out.length);
119
+ out.push(s);
120
+ };
121
+ const open = (v) => {
122
+ const keys = Array.isArray(v) ? null : Object.keys(v);
123
+ out.push(keys ? '{' : '[');
124
+ return { v, keys, i: 0 };
125
+ };
126
+ const stack = value !== null && typeof value === 'object' ? [open(value)] : (leaf(value), []);
127
+ while (stack.length) {
128
+ const f = stack[stack.length - 1];
129
+ if (stopped || f.i >= (f.keys ? f.keys.length : f.v.length)) {
130
+ out.push(f.keys ? '}' : ']');
131
+ stack.pop();
132
+ continue;
133
+ }
134
+ if (f.i > 0) out.push(',');
135
+ const k = f.keys ? f.keys[f.i] : f.i;
136
+ f.i++;
137
+ if (f.keys) {
138
+ out.push(JSON.stringify(cut(k)), ':');
139
+ if (stopped) {
140
+ out.push('""');
141
+ continue;
142
+ }
143
+ }
144
+ const v = f.v[k];
145
+ if (v !== null && typeof v === 'object') stack.push(open(v));
146
+ else leaf(v);
147
+ }
148
+ if (endFirst) return '';
149
+ for (const i of strings) out[i] = JSON.stringify(tagged ? out[i] : scrubSecrets(out[i]));
150
+ return scrubSecrets(out.join('')).slice(0, STORED_PART_MAX);
151
+ }
152
+
70
153
  function dedupKey(parts) {
71
154
  return createHash('sha256').update(parts.join('\x1f')).digest('hex');
72
155
  }
@@ -160,20 +243,21 @@ function importToolPair(db, toolUse, toolResult, project) {
160
243
 
161
244
  const toolName = toolUse.name || 'unknown';
162
245
  const type = TOOL_TO_TYPE[toolName] || 'change';
163
- // Each part is scrubbed BEFORE its 4000-character cap, for the reason importedObsTitle
164
- // gives: a cap that splits a token leaves a prefix too short for the scrubber's pattern.
165
- // The body is therefore kept out of the scrubRecord call below, so it is scrubbed once.
166
- const inputJson = scrubSecrets(
167
- typeof toolUse.input === 'object' ? JSON.stringify(toolUse.input) : String(toolUse.input ?? ''),
168
- ).slice(0, 4000);
246
+ // Each part is scrubbed BEFORE its 4000-character cap (storedToolPart), for the reason
247
+ // importedObsTitle gives: a cap that splits a token leaves a prefix too short for the
248
+ // scrubber's pattern. The body is therefore kept out of the scrubRecord call below.
249
+ const inputObject = typeof toolUse.input === 'object';
250
+ const inputJson = storedToolPart(inputObject ? toolUse.input : String(toolUse.input ?? ''), inputObject);
169
251
  // The cap binds to BOTH shapes: it bound only to the array branch, so a string tool_result
170
252
  // (what Bash and Read return in real transcripts) was stored whole (E2E 2026-10-16). The
171
253
  // whole result is scrubbed before the cut, as the array branch always was: a pre-scrub cut
172
254
  // at twice the cap split a token there, and secrets ahead of it that the scrub shortened
173
255
  // pulled that head inside the 4000 kept (E2E round 2, data R2).
174
- const rawResult =
175
- typeof toolResult?.content === 'string' ? toolResult.content : JSON.stringify(toolResult?.content ?? '');
176
- const resultText = scrubSecrets(rawResult).slice(0, 4000);
256
+ const resultString = typeof toolResult?.content === 'string';
257
+ const resultText = storedToolPart(
258
+ resultString ? toolResult.content : (toolResult?.content ?? ''),
259
+ !resultString,
260
+ );
177
261
 
178
262
  // D#35: this gated on `input.file_path` alone, and `NotebookEdit` carries
179
263
  // `notebook_path` and never `file_path` — so the branch named NotebookEdit
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.30.12",
3
+ "version": "6.30.14",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "claude-mem-lite",
9
- "version": "6.30.12",
9
+ "version": "6.30.14",
10
10
  "os": [
11
11
  "darwin",
12
12
  "linux",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.30.12",
3
+ "version": "6.30.14",
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",
package/secret-scrub.mjs CHANGED
@@ -76,9 +76,18 @@ export const SECRET_PATTERNS = [
76
76
  // that way was stored when the text was in a JSON string. A quoted value with more of the
77
77
  // key in it is erased to its closing quote as before: the key passes miss some of what that erases
78
78
  // (a first body line cut inside its first 40 characters).
79
+ //
80
+ // A VALUE ON A LATER LINE (D#683). The whitespace after a separator may run onto the next lines, so a
81
+ // value written below its label is still erased (`password:` then ` <v>`; prettier's `const X =` then
82
+ // ` "<v>";`), but not when that line opens with a label of its own (`NAME: v`, `NAME=v`, `"name":`).
83
+ // A label with an empty value (`POSTGRES_PASSWORD:` in a compose file, `password =` in an INI section)
84
+ // took the next line's label as its value, and that line's value was stored. Such a line is now read
85
+ // as it would be on its own; text with no line break between a separator and its value scrubs exactly
86
+ // as before. A name over 64 characters is not taken for a label, so the check reads a bounded prefix
87
+ // of the line. The quoted-value, JSON-key and env-name patterns below carry the same rule.
79
88
  // 1a. `=` assignment → ALWAYS scrub (config syntax, never prose):
80
89
  [
81
- /((?:\b|_)(?:password|passwd|passphrase|token|bearer|secret)(?:[*_~`]{1,3})?\s*=(?:[*_~]{1,2}(?=\s))?\s*)(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
90
+ /((?:\b|_)(?:password|passwd|passphrase|token|bearer|secret)(?:[*_~`]{1,3})?\s*=(?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?![*~`]{0,3}[\w.-]{1,64}[*~`]{0,3}[^\S\r\n]*(?:[=:](?!=)|:(?=[\s'"]))[^\S\r\n]*\S)))(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
82
91
  '$1***',
83
92
  ],
84
93
  // 1b. `:` separator, PASSWORD nouns. Position decides how permissive the value
@@ -119,16 +128,16 @@ export const SECRET_PATTERNS = [
119
128
  // Both arms emit `***` (3 chars, under the {6,} floor), so they cannot
120
129
  // double-apply.
121
130
  [
122
- /((?:(?<![A-Za-z][ \t][*_~]{0,3})(?:\b|_)(?:password|passwd|passphrase)(?:[*_~]{1,3})?|(?=_?(?:password|passwd|passphrase)`)(?<![A-Za-z][ \t]`[\w-]{0,40})(?<=`[\w-]{0,40})(?:\b|_)(?:password|passwd|passphrase)`)\s*[::](?:[*_~]{1,2}(?=\s))?\s*)(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
131
+ /((?:(?<![A-Za-z][ \t][*_~]{0,3})(?:\b|_)(?:password|passwd|passphrase)(?:[*_~]{1,3})?|(?=_?(?:password|passwd|passphrase)`)(?<![A-Za-z][ \t]`[\w-]{0,40})(?<=`[\w-]{0,40})(?:\b|_)(?:password|passwd|passphrase)`)\s*[::](?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?![*~`]{0,3}[\w.-]{1,64}[*~`]{0,3}[^\S\r\n]*(?:[=:](?!=)|:(?=[\s'"]))[^\S\r\n]*\S)))(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
123
132
  '$1***',
124
133
  ],
125
134
  [
126
- /((?:\b|_)(?:password|passwd|passphrase)(?:[*_~`]{1,3})?\s*[::](?:[*_~]{1,2}(?=\s))?\s*)(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?![A-Za-z]{1,15}(?=[`*~]*(?:[\s,;'"}\]]|$)))(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
135
+ /((?:\b|_)(?:password|passwd|passphrase)(?:[*_~`]{1,3})?\s*[::](?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?![*~`]{0,3}[\w.-]{1,64}[*~`]{0,3}[^\S\r\n]*(?:[=:](?!=)|:(?=[\s'"]))[^\S\r\n]*\S)))(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?![A-Za-z]{1,15}(?=[`*~]*(?:[\s,;'"}\]]|$)))(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
127
136
  '$1***',
128
137
  ],
129
138
  // 1c. `:` separator, prose-ambiguous nouns → keep the lookbehind ("the token: alicebob"):
130
139
  [
131
- /((?:(?<![A-Za-z][ \t][*_~]{0,3})(?:\b|_)(?:token|bearer|secret)(?:[*_~]{1,3})?|(?=_?(?:token|bearer|secret)`)(?<![A-Za-z][ \t]`[\w-]{0,40})(?<=`[\w-]{0,40})(?:\b|_)(?:token|bearer|secret)`)\s*[::](?:[*_~]{1,2}(?=\s))?\s*)(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
140
+ /((?:(?<![A-Za-z][ \t][*_~]{0,3})(?:\b|_)(?:token|bearer|secret)(?:[*_~]{1,3})?|(?=_?(?:token|bearer|secret)`)(?<![A-Za-z][ \t]`[\w-]{0,40})(?<=`[\w-]{0,40})(?:\b|_)(?:token|bearer|secret)`)\s*[::](?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?![*~`]{0,3}[\w.-]{1,64}[*~`]{0,3}[^\S\r\n]*(?:[=:](?!=)|:(?=[\s'"]))[^\S\r\n]*\S)))(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
132
141
  '$1***',
133
142
  ],
134
143
  // access_token / refresh_token are the canonical OAuth2 field names — they were
@@ -143,7 +152,7 @@ export const SECRET_PATTERNS = [
143
152
  // low-FP decision that `topsecret=` / `access_token_count:` are non-credentials
144
153
  // (#8283 + utils.test.mjs:1089-1100); bare `pwd` is omitted so `PWD=` (a path) survives.
145
154
  [
146
- /((?:\b|_)(?:api[_-]?key|api[_-]?secret|secret[_-]?key|access[_-]?key|private[_-]?key|client[_-]?secret|auth[_-]?token|access[_-]?token|refresh[_-]?token|pgpassword|pgpass|mysql_pwd)(?:[*_~`]{1,3})?\s*[=::](?:[*_~]{1,2}(?=\s))?\s*)(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
155
+ /((?:\b|_)(?:api[_-]?key|api[_-]?secret|secret[_-]?key|access[_-]?key|private[_-]?key|client[_-]?secret|auth[_-]?token|access[_-]?token|refresh[_-]?token|pgpassword|pgpass|mysql_pwd)(?:[*_~`]{1,3})?\s*[=::](?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?![*~`]{0,3}[\w.-]{1,64}[*~`]{0,3}[^\S\r\n]*(?:[=:](?!=)|:(?=[\s'"]))[^\S\r\n]*\S)))(?!process\.env\.)(?!new\s)(?!\w+\()(?!(?:null|undefined|true|false|None|nil|empty|""|''|0)\b)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY|\*\*\*PEM_KEY\*\*\*)[^\s,;'"}\]]){6,}/gi,
147
156
  '$1***',
148
157
  ],
149
158
  // Space-separated credential CLI flag: `--password <value>` (long-form). The KV
@@ -170,21 +179,21 @@ export const SECRET_PATTERNS = [
170
179
  // (mirrors the unquoted 1a/1b split — a quoted value doesn't turn `:` prose
171
180
  // into config, but `<word> password="x"` is still a leak):
172
181
  [
173
- /((?:\b|_)(?:password|passwd|passphrase|token|bearer|secret)(?:[*_~`]{1,3})?\s*=(?:[*_~]{1,2}(?=\s))?\s*)(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
182
+ /((?:\b|_)(?:password|passwd|passphrase|token|bearer|secret)(?:[*_~`]{1,3})?\s*=(?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:])))(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
174
183
  '$1$2***$2',
175
184
  ],
176
185
  [
177
- /((?:\b|_)(?:password|passwd|passphrase)(?:[*_~`]{1,3})?\s*[::](?:[*_~]{1,2}(?=\s))?\s*)(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
186
+ /((?:\b|_)(?:password|passwd|passphrase)(?:[*_~`]{1,3})?\s*[::](?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:])))(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
178
187
  '$1$2***$2',
179
188
  ],
180
189
  [
181
- /((?:(?<![A-Za-z][ \t][*_~]{0,3})(?:\b|_)(?:token|bearer|secret)(?:[*_~]{1,3})?|(?=_?(?:token|bearer|secret)`)(?<![A-Za-z][ \t]`[\w-]{0,40})(?<=`[\w-]{0,40})(?:\b|_)(?:token|bearer|secret)`)\s*[::](?:[*_~]{1,2}(?=\s))?\s*)(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
190
+ /((?:(?<![A-Za-z][ \t][*_~]{0,3})(?:\b|_)(?:token|bearer|secret)(?:[*_~]{1,3})?|(?=_?(?:token|bearer|secret)`)(?<![A-Za-z][ \t]`[\w-]{0,40})(?<=`[\w-]{0,40})(?:\b|_)(?:token|bearer|secret)`)\s*[::](?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:])))(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
182
191
  '$1$2***$2',
183
192
  ],
184
193
  // (b) structured keys + named env vars are unambiguous config even after a word
185
194
  // (`see api_key: "x"` DOES scrub, mirroring the unquoted structured-key path):
186
195
  [
187
- /((?:\b|_)(?:pgpassword|pgpass|mysql_pwd|api[_-]?key|api[_-]?secret|secret[_-]?key|access[_-]?key|private[_-]?key|client[_-]?secret|auth[_-]?token|access[_-]?token|refresh[_-]?token)(?:[*_~`]{1,3})?\s*[=::](?:[*_~]{1,2}(?=\s))?\s*)(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
196
+ /((?:\b|_)(?:pgpassword|pgpass|mysql_pwd|api[_-]?key|api[_-]?secret|secret[_-]?key|access[_-]?key|private[_-]?key|client[_-]?secret|auth[_-]?token|access[_-]?token|refresh[_-]?token)(?:[*_~`]{1,3})?\s*[=::](?:[*_~]{1,2}(?=\s))?(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:])))(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
188
197
  '$1$2***$2',
189
198
  ],
190
199
  // AWS access keys: AKIA (long-term) + ASIA (STS temp) + AROA (role) + AIDA
@@ -256,7 +265,10 @@ export const SECRET_PATTERNS = [
256
265
  // name and the colon, so a `curl -v` / fetch header dump walked straight through. The
257
266
  // scheme word is optional because a raw token as the whole value is just as common;
258
267
  // `authorization` is not a benign key, and over-scrub is the safe direction here.
259
- [/(['"](?:proxy-)?authorization['"]\s*:\s*)(['"])(?:(?:Bearer|Basic|token)\s+)?[^'"]{6,}\2/gi, '$1$2***$2'],
268
+ [
269
+ /(['"](?:proxy-)?authorization['"]\s*:(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:])))(['"])(?:(?:Bearer|Basic|token)\s+)?[^'"]{6,}\2/gi,
270
+ '$1$2***$2',
271
+ ],
260
272
  // R10 P1-6: Azure storage. AccountKey= is the account's master credential and sig= is a
261
273
  // live SAS token; neither had any pattern. `sig` is anchored to a URL query position
262
274
  // (`?`/`&`) rather than a word boundary — bare `\bsig=` eats `const sig=computeX(y)`.
@@ -264,7 +276,7 @@ export const SECRET_PATTERNS = [
264
276
  [/([?&]sig=)[^\s;&'"]{16,}/gi, '$1***'],
265
277
  // Supabase / generic long base64 keys (40+ chars, common in env vars)
266
278
  [
267
- /(\b(?:SUPABASE_KEY|SUPABASE_ANON_KEY|SUPABASE_SERVICE_ROLE_KEY|DATABASE_URL|REDIS_URL)\s*[=:]\s*)[^\s,;'"}\]]+/gi,
279
+ /(\b(?:SUPABASE_KEY|SUPABASE_ANON_KEY|SUPABASE_SERVICE_ROLE_KEY|DATABASE_URL|REDIS_URL)\s*[=:](?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?![*~`]{0,3}[\w.-]{1,64}[*~`]{0,3}[^\S\r\n]*(?:[=:](?!=)|:(?=[\s'"]))[^\S\r\n]*\S)))[^\s,;'"}\]]+/gi,
268
280
  '$1***',
269
281
  ],
270
282
  // Basic auth in URLs (https://user:password@host). ftp/ftps added — file-drop
@@ -305,7 +317,7 @@ export const SECRET_PATTERNS = [
305
317
  // these slip through. Match the value-quoted form explicitly. Length floor
306
318
  // (6) avoids tripping on intentional placeholder shorts ("...", "secret").
307
319
  [
308
- /("(?:password|passwd|token|api[_-]?key|api[_-]?secret|secret[_-]?key|access[_-]?key|access[_-]?token|private[_-]?key|client[_-]?secret|auth[_-]?token|bearer|refresh[_-]?token|session[_-]?id|sessionid)"\s*:\s*")[^"]{6,}(")/gi,
320
+ /("(?:password|passwd|token|api[_-]?key|api[_-]?secret|secret[_-]?key|access[_-]?key|access[_-]?token|private[_-]?key|client[_-]?secret|auth[_-]?token|bearer|refresh[_-]?token|session[_-]?id|sessionid)"\s*:(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:]))")[^"]{6,}(")/gi,
309
321
  '$1***$2',
310
322
  ],
311
323
  // JSON keys with vendor PREFIX/SUFFIX around the core credential noun —
@@ -318,7 +330,7 @@ export const SECRET_PATTERNS = [
318
330
  // one quote, `\w*` ran to the end of a word run and backtracked through every keyword in it,
319
331
  // each rescanning the run — `"` + `secret` x 33k took 4.6 s. 64 is far past any key name.
320
332
  [
321
- /("\w{0,64}(?:password|passwd|secret|api[_-]?key|auth[_-]?token|access[_-]?token|private[_-]?key)\w{0,64}"\s*:\s*")[^"]{6,}(")/gi,
333
+ /("\w{0,64}(?:password|passwd|secret|api[_-]?key|auth[_-]?token|access[_-]?token|private[_-]?key)\w{0,64}"\s*:(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:]))")[^"]{6,}(")/gi,
322
334
  '$1***$2',
323
335
  ],
324
336
  // Quoted-KEY credential values — Python dict reprs `{'api_key': '...'}`, single-quoted
@@ -334,7 +346,7 @@ export const SECRET_PATTERNS = [
334
346
  // `'token_count': 123456`); `passphrase` added here too (double-quoted JSON passphrase is
335
347
  // subsumed by this pattern since `['"]` matches `"`). Over-scrub is the safe direction.
336
348
  [
337
- /(['"](?:\w{0,64}(?:password|passwd|passphrase|secret|api[_-]?key|auth[_-]?token|access[_-]?token|private[_-]?key)\w{0,64}|\w{1,64}_token)['"]\s*:\s*)(['"])[^'"]{6,}\2/gi,
349
+ /(['"](?:\w{0,64}(?:password|passwd|passphrase|secret|api[_-]?key|auth[_-]?token|access[_-]?token|private[_-]?key)\w{0,64}|\w{1,64}_token)['"]\s*:(?:[^\S\r\n]*|[^\S\r\n]*[\r\n]\s*(?!(?:"[^"\r\n]*"|'[^'\r\n]*')[^\S\r\n]*[:=:])))(['"])[^'"]{6,}\2/gi,
338
350
  '$1$2***$2',
339
351
  ],
340
352
  // Session cookies in headers / urlencoded bodies (sessionid=, session_id=, JSESSIONID=, PHPSESSID=).
@@ -1534,9 +1546,8 @@ function scrubKeyTails(text) {
1534
1546
  // this repo has twice decided the text matters more.
1535
1547
  const MAX_SCRUB_PASSES = 32;
1536
1548
 
1537
- export function scrubSecrets(text) {
1538
- if (!text || typeof text !== 'string') return text || '';
1539
- let result = stripPrivate(text);
1549
+ function runPatterns(text) {
1550
+ let result = text;
1540
1551
  for (let pass = 1; ; pass++) {
1541
1552
  const before = result;
1542
1553
  for (const [pattern, replacement] of SECRET_PATTERNS) {
@@ -1546,3 +1557,101 @@ export function scrubSecrets(text) {
1546
1557
  }
1547
1558
  return result;
1548
1559
  }
1560
+
1561
+ // ─── Escaped string literals (D#687, D#693, D#685) ────────────────────────
1562
+ //
1563
+ // A JSON string literal holds its text one escape level deep: a key's quotes are `\"` and a line break
1564
+ // is the two characters `\n`. The patterns above are written for text, so inside a literal a JSON key
1565
+ // never closes on a bare quote, a label never starts a line, and a label's value runs on over the
1566
+ // escaped breaks into the next line's label. What `aws secretsmanager get-secret-value --query
1567
+ // SecretString` prints (`"{\"password\":\"…\"}"`), a `.env` that `kubectl … | jq -c .data` prints as one
1568
+ // string, an escaped JSON literal in a source file or a `.env` value: all were stored. Two attempts that
1569
+ // taught the patterns about escaped line starts (D#663, D#668) were withdrawn at review: a pattern
1570
+ // that starts in a new place carries its value's run-on there.
1571
+ //
1572
+ // So each double-quoted literal on one line whose body holds an escape is decoded (as JSON; an escape
1573
+ // JSON does not know is kept as written) and scrubbed as text, MAX_LITERAL_DEPTH levels deep, and
1574
+ // rewritten, as the scrubbed text encoded as JSON, only when that scrub changed it: a literal it
1575
+ // leaves alone keeps every byte. A literal opens at a quote with an even number of backslashes before
1576
+ // it, and a text that ends inside one has it read to the end. The quote is matched before that count
1577
+ // is checked: Node 22 runs a leading lookbehind at every position, which read a 200,000-character run
1578
+ // of backslashes back from each one (5,108 ms; 0 on Node 26).
1579
+ //
1580
+ // A rewritten literal keeps erased what the patterns erase in its escaped body: their run-on erases
1581
+ // the lines after a label up to a space, and the text reading keeps such a line when it has no label
1582
+ // of its own. Without that, 5-8 values in 20,000 generated texts that v6.30.13 erased were stored
1583
+ // (D#687 differential, 2026-11-02). The patterns then run over the whole text as before. The words
1584
+ // of the body, and of what the patterns leave of it, are read decoded and encoded again: an escape
1585
+ // that decodes to a value-token character (`\/` from PHP's json_encode, `+` from .NET) joins
1586
+ // words in the rewritten literal, and read from the escaped body such a word was new and kept
1587
+ // (266-715 of 20,000 generated `.env` texts, v6.30.14 review r2).
1588
+ //
1589
+ // Bounds: MAX_LITERALS literals are decoded per text and level (real transcripts, 2026-11-02: at most
1590
+ // 133 in one text); with no bound, `"\"` repeated over 500,000 characters took 104x the patterns alone
1591
+ // (415 ms against 4). A rewritten literal can give the patterns a reading they did not have, so a text
1592
+ // that holds a backslash is scrubbed again until it no longer changes, MAX_LITERAL_ROUNDS times at
1593
+ // most; with one round, 23-42 of 20,000 generated texts changed on a second call (scrubSecrets is
1594
+ // idempotent by contract: import-jsonl's dedup and cmdRestore rely on it). A text the first round
1595
+ // leaves as it is is a fixed point already, and is not scrubbed again.
1596
+ const ESCAPED_LITERAL_RE = /"(?<=(?:^|[^\\])(?:\\\\)*")((?:[^"\\\r\n]|\\[^\r\n])*)("|$)/g;
1597
+ const JSON_ESCAPE_RE = /\\(u[0-9a-fA-F]{4}|["\\/bfnrt])/g;
1598
+ const JSON_ESCAPES = { '"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t' };
1599
+ // A value token, and an escape, which is no part of one: `\nAPI_KEY` holds the token `API_KEY`.
1600
+ const VALUE_TOKEN_RE = /[A-Za-z0-9+/=_.-]+/g;
1601
+ const ESCAPE_RE = /\\(?:u[0-9a-fA-F]{4}|.)/g;
1602
+ const MAX_LITERAL_DEPTH = 3;
1603
+ const MAX_LITERALS = 256;
1604
+ const MAX_LITERAL_ROUNDS = 4;
1605
+
1606
+ function decodeLiteral(body) {
1607
+ try {
1608
+ return JSON.parse(`"${body}"`);
1609
+ } catch {
1610
+ return body.replace(JSON_ESCAPE_RE, (_m, e) =>
1611
+ e[0] === 'u' ? String.fromCharCode(parseInt(e.slice(1), 16)) : JSON_ESCAPES[e],
1612
+ );
1613
+ }
1614
+ }
1615
+
1616
+ const maskEscapes = (text) => text.replace(ESCAPE_RE, (e) => ' '.repeat(e.length));
1617
+ const valueTokens = (text) => new Set(maskEscapes(text).match(VALUE_TOKEN_RE));
1618
+
1619
+ function scrubEscapedLiterals(text, depth) {
1620
+ if (depth >= MAX_LITERAL_DEPTH || !text.includes('\\')) return text;
1621
+ let budget = MAX_LITERALS;
1622
+ return text.replace(ESCAPED_LITERAL_RE, (literal, body, close) => {
1623
+ if (budget === 0 || !body.includes('\\')) return literal;
1624
+ budget--;
1625
+ const decoded = decodeLiteral(body);
1626
+ const scrubbed = scrubText(decoded, depth + 1);
1627
+ if (scrubbed === decoded) return literal;
1628
+ const encoded = JSON.stringify(scrubbed).slice(1, -1);
1629
+ const reencode = (s) => JSON.stringify(s).slice(1, -1);
1630
+ const keptByPatterns = valueTokens(reencode(decodeLiteral(runPatterns(body))));
1631
+ const inBody = valueTokens(reencode(decoded));
1632
+ let out = '';
1633
+ let last = 0;
1634
+ for (const { 0: token, index } of maskEscapes(encoded).matchAll(VALUE_TOKEN_RE)) {
1635
+ if (keptByPatterns.has(token) || !inBody.has(token)) continue;
1636
+ out += encoded.slice(last, index) + '***';
1637
+ last = index + token.length;
1638
+ }
1639
+ return `"${out}${encoded.slice(last)}${close}`;
1640
+ });
1641
+ }
1642
+
1643
+ function scrubText(text, depth) {
1644
+ return runPatterns(scrubEscapedLiterals(stripPrivate(text), depth));
1645
+ }
1646
+
1647
+ export function scrubSecrets(text) {
1648
+ if (!text || typeof text !== 'string') return text || '';
1649
+ let result = scrubText(text, 0);
1650
+ if (result === text) return result;
1651
+ for (let round = 1; round < MAX_LITERAL_ROUNDS && result.includes('\\'); round++) {
1652
+ const next = scrubText(result, 0);
1653
+ if (next === result) break;
1654
+ result = next;
1655
+ }
1656
+ return result;
1657
+ }