claude-mem-lite 6.30.10 → 6.30.12

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.10",
12
+ "version": "6.30.12",
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.10",
3
+ "version": "6.30.12",
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"
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.30.10",
3
+ "version": "6.30.12",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "claude-mem-lite",
9
- "version": "6.30.10",
9
+ "version": "6.30.12",
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.10",
3
+ "version": "6.30.12",
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
@@ -60,9 +60,25 @@ export const SECRET_PATTERNS = [
60
60
  // v3.61.0 already had to undo exactly that. `| password | <v> |` table rows stay open for a
61
61
  // different reason: those 831k lines held 1 such row and none with a credential-shaped
62
62
  // value, so a table pattern's false-positive rate cannot be measured here.
63
+ //
64
+ // A KEY'S BEGIN IS NOT A VALUE (D#657). These patterns run before the private key ones below, and
65
+ // a value took the `-----BEGIN` of a key written right after its label (`PRIVATE_KEY=-----BEGIN …`,
66
+ // or `private_key = <<EOT\n-----BEGIN …` in a JSON string, where the escaped break is no space):
67
+ // the key passes then saw no BEGIN and stored every line of a cut-off key. So a value stops before a
68
+ // private key's BEGIN, and the label patterns' values also before `***PEM_KEY***`, or the next pass
69
+ // took that mark and the lines after it up to a space (6.30.11 kept them: it erased such a key from
70
+ // its END).
71
+ // The `--password` flag stops at the BEGIN only: with the mark stop as well, a line that the reading
72
+ // of a cut-off key left after its mark, which the flag's value on the next pass erased in 6.30.11,
73
+ // was kept.
74
+ // A quoted value is left to the key passes when it holds the BEGIN line and nothing else (`'…-----\n'
75
+ // +`: a key concatenated one string per line, Node's util.inspect of an object); a whole key printed
76
+ // that way was stored when the text was in a JSON string. A quoted value with more of the
77
+ // key in it is erased to its closing quote as before: the key passes miss some of what that erases
78
+ // (a first body line cut inside its first 40 characters).
63
79
  // 1a. `=` assignment → ALWAYS scrub (config syntax, never prose):
64
80
  [
65
- /((?:\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)[^\s,;'"}\]]{6,}/gi,
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,
66
82
  '$1***',
67
83
  ],
68
84
  // 1b. `:` separator, PASSWORD nouns. Position decides how permissive the value
@@ -103,16 +119,16 @@ export const SECRET_PATTERNS = [
103
119
  // Both arms emit `***` (3 chars, under the {6,} floor), so they cannot
104
120
  // double-apply.
105
121
  [
106
- /((?:(?<![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)[^\s,;'"}\]]{6,}/gi,
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,
107
123
  '$1***',
108
124
  ],
109
125
  [
110
- /((?:\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,;'"}\]]|$)))[^\s,;'"}\]]{6,}/gi,
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,
111
127
  '$1***',
112
128
  ],
113
129
  // 1c. `:` separator, prose-ambiguous nouns → keep the lookbehind ("the token: alicebob"):
114
130
  [
115
- /((?:(?<![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)[^\s,;'"}\]]{6,}/gi,
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,
116
132
  '$1***',
117
133
  ],
118
134
  // access_token / refresh_token are the canonical OAuth2 field names — they were
@@ -127,7 +143,7 @@ export const SECRET_PATTERNS = [
127
143
  // low-FP decision that `topsecret=` / `access_token_count:` are non-credentials
128
144
  // (#8283 + utils.test.mjs:1089-1100); bare `pwd` is omitted so `PWD=` (a path) survives.
129
145
  [
130
- /((?:\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)[^\s,;'"}\]]{6,}/gi,
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,
131
147
  '$1***',
132
148
  ],
133
149
  // Space-separated credential CLI flag: `--password <value>` (long-form). The KV
@@ -138,7 +154,10 @@ export const SECRET_PATTERNS = [
138
154
  // `gh auth login --token …` and `op … --secret …` are the shapes that actually appear in
139
155
  // Bash output. The {6,} floor is what keeps `--token abc` (a placeholder in a usage line)
140
156
  // out, so do not lower it.
141
- [/(--(?:password|passwd|token|api[-_]?key|secret)[=\s]+)(?!-)[^\s'"]{6,}/gi, '$1***'],
157
+ [
158
+ /(--(?:password|passwd|token|api[-_]?key|secret)[=\s]+)(?!-)(?:(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY)[^\s'"]){6,}/gi,
159
+ '$1***',
160
+ ],
142
161
  // Bare-key QUOTED values — `api_key="..."`, `password: '...'`. The unquoted KV
143
162
  // patterns above stop at `'`/`"` (excluded from their value class), so a quoted
144
163
  // value matched 0 chars and slipped through. Consumes the opening quote, the value,
@@ -151,21 +170,21 @@ export const SECRET_PATTERNS = [
151
170
  // (mirrors the unquoted 1a/1b split — a quoted value doesn't turn `:` prose
152
171
  // into config, but `<word> password="x"` is still a leak):
153
172
  [
154
- /((?:\b|_)(?:password|passwd|passphrase|token|bearer|secret)(?:[*_~`]{1,3})?\s*=(?:[*_~]{1,2}(?=\s))?\s*)(['"])[^'"]{6,}\2/gi,
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,
155
174
  '$1$2***$2',
156
175
  ],
157
176
  [
158
- /((?:\b|_)(?:password|passwd|passphrase)(?:[*_~`]{1,3})?\s*[::](?:[*_~]{1,2}(?=\s))?\s*)(['"])[^'"]{6,}\2/gi,
177
+ /((?:\b|_)(?:password|passwd|passphrase)(?:[*_~`]{1,3})?\s*[::](?:[*_~]{1,2}(?=\s))?\s*)(['"])(?!-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:\\+[nr])*\2)[^'"]{6,}\2/gi,
159
178
  '$1$2***$2',
160
179
  ],
161
180
  [
162
- /((?:(?<![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*)(['"])[^'"]{6,}\2/gi,
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,
163
182
  '$1$2***$2',
164
183
  ],
165
184
  // (b) structured keys + named env vars are unambiguous config even after a word
166
185
  // (`see api_key: "x"` DOES scrub, mirroring the unquoted structured-key path):
167
186
  [
168
- /((?:\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*)(['"])[^'"]{6,}\2/gi,
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,
169
188
  '$1$2***$2',
170
189
  ],
171
190
  // AWS access keys: AKIA (long-term) + ASIA (STS temp) + AROA (role) + AIDA
@@ -756,6 +775,49 @@ function cutOffKeyEnd(text, b, be, ctx) {
756
775
  const label = text.slice(b, be);
757
776
  const read = readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, end, ctx.memoOf);
758
777
  const found = read.longs > 0 && keyEvidence(read, label, true) ? read.end : -1;
778
+ // The lines below read again with the shape of `prefix`, whose first character after blanks is
779
+ // `lead`: the reading of the key they hold, or null. `accept`: a further test of that reading.
780
+ const reread = (prefix, lead, accept = null) => {
781
+ const alt = ctx.shapeOf(prefix);
782
+ if (!alt || ctx.endFollows(be)) return null;
783
+ const again = readKeyLines(text, next, u, alt, lead, longs, longest, first, end, ctx.memoOf);
784
+ if (!(again.longs > 0 && again.end > found && keyEvidence(again, label, false))) return null;
785
+ if (accept && !accept(again)) return null;
786
+ ctx.reread = true;
787
+ return again;
788
+ };
789
+ // The lines under a diff context BEGIN (D#614 A1, D#641) read with a diff mark's shape, which takes
790
+ // context lines too: where the key ends, or -1. Only when the lines it takes include a removed or
791
+ // added one: Read output starts its lines with spaces as well, and a reading there took the next
792
+ // line's number for a key's short last line. A removed key that ends with its short last line (or a
793
+ // PGP checksum) is followed by the added key's lines, which the reading stopped at: they are read
794
+ // on, as lines of the same key.
795
+ // What D#641 added (a reading of a BEGIN whose next line has no mark, `admitted` false, and the added
796
+ // key's lines) never spans another private-key BEGIN, which no key in a real diff holds: a header line
797
+ // holding one, swallowed by the span, started no reading of its own on a later pass, where it read on
798
+ // over the first pass's ***PEM_KEY*** line and erased a cut line after it (6 of 400,000 r3mix texts).
799
+ // And what D#641 added reads only inside a unified diff's hunk (ctx.inHunk, from the BEGIN's line
800
+ // start `gl`): an indented cut key in prose read the dash list under it as removed and added key
801
+ // lines, losing it (6.30.10 pre-tag r1 F2).
802
+ const diffRead = (admitted, gl) => {
803
+ // Such a reading ends before the next BEGIN and takes a marked line, so with no marked line before
804
+ // that BEGIN there is none to read (header BEGINs over context lines read the same run again each).
805
+ if (!admitted && !(ctx.inHunk(gl, u) && markedLineIn(text, next, ctx.nextBegin(next), u))) return -1;
806
+ let r = reread(
807
+ '-',
808
+ 45,
809
+ (a) => (admitted || ctx.nextBegin(next) >= a.end) && markedLineIn(text, next, a.end, u),
810
+ );
811
+ if (!r) return -1;
812
+ const alt = ctx.shapeOf('-');
813
+ for (;;) {
814
+ const n = r.shorts > 0 ? nextLine(text, r.end, u).next : -1;
815
+ if (n === -1 || (text[n] !== '-' && text[n] !== '+') || !ctx.inHunk(gl, u)) return r.end;
816
+ const again = readKeyLines(text, n, u, alt, 45, 0, 0, '', -1, ctx.memoOf);
817
+ if (again.longs === 0 || ctx.nextBegin(n) < again.end) return r.end;
818
+ r = again;
819
+ }
820
+ };
759
821
  // D#168: code between the line prefix and the BEGIN (`-KEY = '''`, ` 5\tKEY = """`) or a
760
822
  // string opener before it (`'+`, `"-`) made the whole left context the shape, which no line below
761
823
  // starts with, so nothing past the BEGIN line was read and a cut key was stored. Read again with
@@ -789,63 +851,18 @@ function cutOffKeyEnd(text, b, be, ctx) {
789
851
  if (!B64_RUN16_RE.test(gap) && !gap.includes(':')) return found;
790
852
  }
791
853
  }
792
- // The grammar starts the line. In escaped text the prefix above stops at an unescaped single
793
- // quote, which JSON never escapes (`-KEY = '''` in a JSON string leaves ''), so look past single
794
- // quotes for the start of the line: a break, or a double quote that ends a string at this depth
795
- // (the string the line is in), or the start of the text. At most LINE_START_SCAN characters back;
796
- // past that, no grammar.
797
- let gl = ls;
798
- if (u > 0 && ls > 0 && text[ls - 1] === "'") {
799
- gl = -1;
800
- // Native between candidates: a character by character walk was most of the time of a shape
801
- // that repeats one 300-character line (pemEscapedLongCodeBeforeBegin), and now goes the whole line.
802
- const lim = Math.max(0, ls - LINE_START_SCAN);
803
- for (let j = ls - 1; ;) {
804
- LINE_BACK_RE.lastIndex = j;
805
- const k = j - LINE_BACK_RE.exec(text)[1].length;
806
- if (k === 0) {
807
- if (lim === 0) gl = 0;
808
- break;
809
- }
810
- // text[k - 1] is a break, a double quote or a backslash. A backslash run then `n` or `r` at k
811
- // is an escaped break at this depth when the run fits it; the line starts after that letter.
812
- const c = text.charCodeAt(k - 1);
813
- if (c === 92) {
814
- const e = text.charCodeAt(k);
815
- if (k + 1 < lim) break;
816
- if ((e === 110 || e === 114) && isEscBreak(backslashesBefore(text, k, 0), u)) {
817
- gl = k + 1;
818
- break;
819
- }
820
- } else if (k < lim) break;
821
- else if (c === 10 || c === 13 || isStringEnd(backslashesBefore(text, k - 1, 0), u)) {
822
- gl = k;
823
- break;
824
- }
825
- j = k - 1;
826
- }
827
- }
854
+ const gl = lineStartPastQuotes(text, ls, u);
828
855
  const head = gl === -1 ? '' : text.slice(gl, Math.min(b, gl + PREFIX_GRAMMAR_SCAN));
829
856
  const g = gl === -1 ? null : PREFIX_GRAMMAR_RE.exec(head);
830
857
  const gs = g ? gl + g[0].length - g[1].length : -1;
831
858
  let gp = gl;
832
859
  while (gp < b && (text.charCodeAt(gp) === 32 || text.charCodeAt(gp) === 9)) gp++;
833
- // The lines below read again with the shape of `prefix`, whose first character after blanks is
834
- // `lead`: where the key they hold ends, or -1.
835
- const reread = (prefix, lead) => {
836
- const alt = ctx.shapeOf(prefix);
837
- if (!alt || ctx.endFollows(be)) return -1;
838
- const again = readKeyLines(text, next, u, alt, lead, longs, longest, first, end, ctx.memoOf);
839
- if (!(again.longs > 0 && again.end > found && keyEvidence(again, label, false))) return -1;
840
- ctx.reread = true;
841
- return again.end;
842
- };
843
860
  // Only when the line is more than its grammar before the BEGIN: an opener before it or code after it.
844
861
  if (g && !(gs === gp && gl + g[0].length === b)) {
845
862
  let lead = gs;
846
863
  while (text.charCodeAt(lead) === 32 || text.charCodeAt(lead) === 9) lead++;
847
864
  const r = reread(g[1], text.charCodeAt(lead));
848
- if (r !== -1) return r;
865
+ if (r) return r.end;
849
866
  }
850
867
  // D#614 A2: a grep column after a path the grammar refuses; not the column the grammar read
851
868
  // already, nor the first reading's own prefix. That prefix is the column whenever nothing stands
@@ -854,26 +871,219 @@ function cutOffKeyEnd(text, b, be, ctx) {
854
871
  const p = gl === -1 ? null : GREP_ANY_PATH_RE.exec(head);
855
872
  if (p && !(g && g[1] === p[1]) && p[1] !== left.replace(/^[ \t]+/, '')) {
856
873
  const r = reread(p[1], text.charCodeAt(gl + p[0].length - p[1].length));
857
- if (r !== -1) return r;
874
+ if (r) return r.end;
858
875
  }
859
876
  // D#614 A1: a unified diff's context line starts with its space, with code after it or none, and
860
877
  // the removed and added lines below it with `-` and `+`, which the line prefix (that space, or the
861
- // code) never matched. They are read with a diff mark's shape, which takes a context line too.
862
- // Only when the next line starts with a mark: Read output starts its lines with spaces as well,
863
- // and a reading there took the next line's number for a key's short last line. Nor when the line
864
- // holds an escaped break of another depth: it is then several lines, the first one led by a space.
865
- if (
866
- text.charCodeAt(gl) === 32 &&
867
- (text[next] === '-' || text[next] === '+') &&
868
- !/\\[nr]/.test(text.slice(gl, b))
869
- ) {
870
- const r = reread('-', 45);
878
+ // code) never matched.
879
+ if (gl !== -1 && isDiffContextLine(text, gl, b)) {
880
+ const r = diffRead(text[next] === '-' || text[next] === '+', gl);
871
881
  if (r !== -1) return r;
872
882
  }
883
+ } else if (run < 16) {
884
+ // D#641: in a real diff the lines under a context BEGIN can be context lines too: an OpenSSH key's
885
+ // first body line is the same for every key of its type. The reading above took them for the key
886
+ // and stopped at the first removed line, so the removed and added lines were stored.
887
+ const after = nextLine(text, found, u).next;
888
+ if (after !== -1 && (text[after] === '-' || text[after] === '+')) {
889
+ const gl = lineStartPastQuotes(text, ls, u);
890
+ if (gl !== -1 && isDiffContextLine(text, gl, b)) {
891
+ const r = diffRead(false, gl);
892
+ if (r !== -1) return r;
893
+ }
894
+ }
873
895
  }
874
896
  return found;
875
897
  }
876
898
 
899
+ /**
900
+ * Whether the line from `gl` to the BEGIN at `b` is a unified diff's context line: led by its space,
901
+ * with code after it or none. Not when the line holds an escaped break of another depth: it is then
902
+ * several lines, the first one led by a space.
903
+ */
904
+ function isDiffContextLine(text, gl, b) {
905
+ return text.charCodeAt(gl) === 32 && !/\\[nr]/.test(text.slice(gl, b));
906
+ }
907
+
908
+ // A unified diff's hunk header and the counts of old and new lines under it (1 when left out).
909
+ const HUNK_HEADER_RE = /@@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? @@/g;
910
+
911
+ /**
912
+ * Whether a line starts at `at` at break depth u: the text's start, a break (at an escape depth not a
913
+ * real one right after a backslash, which nextEscapedLine steps over), or the quote that opens a string
914
+ * at that depth (`"patch":"@@ …` in JSON, where the line is the string's first).
915
+ */
916
+ function startsLineAt(text, at, u) {
917
+ if (at === 0) return true;
918
+ const c = text[at - 1];
919
+ if (c === '\n' || c === '\r') return u === 0 || text[at - 2] !== '\\';
920
+ if (c === 'n' || c === 'r') return isEscBreak(backslashesBefore(text, at - 1, 0), u);
921
+ return (c === '"' || c === "'") && isStringEnd(backslashesBefore(text, at - 1, 0), u);
922
+ }
923
+
924
+ // A break at an escape depth, found natively: a backslash run then `n` or `r` (its length decides the
925
+ // depth), or a real break not right after a backslash (nextEscapedLine steps over the character after a
926
+ // run). The lookbehinds start a match at a run's first backslash only, so each run is read once.
927
+ const ESC_BREAK_TOKEN_RE = /(?<!\\)\\+[nr]|(?<!\\)[\n\r]/g;
928
+ const BREAK_RE = /[\n\r]/g;
929
+
930
+ /**
931
+ * nextLine from a line start `s`, its break found natively. A hunk's lines are read at the depth asked,
932
+ * and in text at several depths a line runs across the others' text: nextEscapedLine stopped at each
933
+ * backslash in it, 33% of the time of hunks at four depths in turn under coverage.
934
+ */
935
+ function hunkNextLine(text, s, u) {
936
+ const re = u === 0 ? BREAK_RE : ESC_BREAK_TOKEN_RE;
937
+ re.lastIndex = s;
938
+ for (let m = re.exec(text); m; m = re.exec(text)) {
939
+ const r = m[0].length - 1;
940
+ if (r === 0 || isEscBreak(r, u)) return nextLine(text, m.index, u);
941
+ }
942
+ return { end: text.length, next: -1 };
943
+ }
944
+
945
+ /**
946
+ * Whether a line starting at `s` is in a unified diff's hunk at break depth u (D#649), from the headers
947
+ * `heads` ({ at, old, add }, ascending). A hunk is the lines below a header that starts a line, as
948
+ * nextLine reads them: a `-` line spends one of the old count, a `+` line one of the new, and a context
949
+ * line (led by a space, or empty: git's diff.suppressBlankEmpty and GNU diff's --suppress-blank-empty
950
+ * leave it so) one of each while both last; any other line ends it (`` comes
951
+ * after one side's last line, so no context line follows it). Only context lines are asked about (a
952
+ * BEGIN's line led by a space), so once a count is spent nothing after it is in the hunk. A line's
953
+ * question goes to the nearest header before it that starts a line at u; a header's text inside a line
954
+ * (the diff of a file that holds one) starts no hunk. A hunk is read from its header only as far as the
955
+ * line asked about, and on from there for a later one, so no line is read twice: reading each hunk
956
+ * whole read the last line of every hunk at one depth across the text at the others, 14.7x benign under
957
+ * coverage (hunks at four depths in turn, 3.9x on v6.30.10).
958
+ */
959
+ function hunkReader(text, heads, u) {
960
+ const own = heads.filter((h) => startsLineAt(text, h.at, u));
961
+ const read = new Map();
962
+ return (s) => {
963
+ let lo = 0;
964
+ let hi = own.length;
965
+ while (lo < hi) {
966
+ const mid = (lo + hi) >> 1;
967
+ if (own[mid].at < s) lo = mid + 1;
968
+ else hi = mid;
969
+ }
970
+ const k = lo - 1;
971
+ if (k < 0) return false;
972
+ let h = read.get(k);
973
+ if (h === undefined) {
974
+ const { at, old, add } = own[k];
975
+ read.set(k, (h = { next: hunkNextLine(text, at, u).next, old, add, starts: new Set() }));
976
+ }
977
+ // Never past the next header: it is at or after s (a later line's question goes to its hunk).
978
+ while (h.next !== -1 && h.next <= s) {
979
+ const at = h.next;
980
+ const line = hunkNextLine(text, at, u);
981
+ const c = line.end === at ? 32 : text.charCodeAt(at);
982
+ if (c === 32 && h.old > 0 && h.add > 0) {
983
+ h.old--;
984
+ h.add--;
985
+ } else if (c === 45) h.old--;
986
+ else if (c === 43) h.add--;
987
+ else {
988
+ h.next = -1;
989
+ break;
990
+ }
991
+ h.starts.add(at);
992
+ h.next = line.next;
993
+ }
994
+ return h.starts.has(s);
995
+ };
996
+ }
997
+
998
+ // A diff mark after a character that can end a line: a break, and at an escape depth an escaped
999
+ // break's letter or (one escape deep) the quote that reopens a string concatenated across lines.
1000
+ const MARK_AFTER_BREAK_RE = /[\n\r][-+]/g;
1001
+ const MARK_AFTER_ESC_RE = /[\n\r"'nr][-+]/g;
1002
+
1003
+ /**
1004
+ * Whether a line that starts in [from, to) at break depth u (`from` a line start) starts with a diff's
1005
+ * `-` or `+`. The marks are found natively and each is checked to start a line as nextLine reads them:
1006
+ * reading the lines one by one went through every line of a run under a BEGIN in Read output (each led
1007
+ * by a space) on each of the 32 passes a chain of labelled secrets takes, 42-57% of two linearity
1008
+ * shapes under coverage.
1009
+ */
1010
+ function markedLineIn(text, from, to, u) {
1011
+ if (from === -1 || from >= to) return false;
1012
+ const c0 = text.charCodeAt(from);
1013
+ if (c0 === 45 || c0 === 43) return true;
1014
+ const re = u === 0 ? MARK_AFTER_BREAK_RE : MARK_AFTER_ESC_RE;
1015
+ const seg = to === Infinity ? text.slice(from) : text.slice(from, to);
1016
+ re.lastIndex = 0;
1017
+ for (let m = re.exec(seg); m; m = re.exec(seg)) {
1018
+ re.lastIndex = m.index + 1;
1019
+ const c = from + m.index;
1020
+ const ch = text.charCodeAt(c);
1021
+ // At an escape depth a real break right after a backslash run is that run's next character, which
1022
+ // nextEscapedLine steps over.
1023
+ if (ch === 10 || ch === 13) {
1024
+ if (u === 0 || text.charCodeAt(c - 1) !== 92) return true;
1025
+ } else if (ch === 110 || ch === 114) {
1026
+ if (isEscBreak(backslashesBefore(text, c, from), u)) return true;
1027
+ } else if (u === 1) {
1028
+ // The quote that opens the string after `\n" +` and a break: a line start when an escaped break
1029
+ // at depth 1 starts the concatenation (nextEscapedLine's CONCAT_AFTER_RE).
1030
+ if (opensConcat(text, c, from)) return true;
1031
+ }
1032
+ }
1033
+ return false;
1034
+ }
1035
+
1036
+ /**
1037
+ * Whether the quote at `c` opens the string after a depth-1 escaped break, a quote, an optional `+` and a
1038
+ * break, so that a line starts after it: CONCAT_AFTER_RE (nextEscapedLine) read backward, within `from`.
1039
+ */
1040
+ function opensConcat(text, c, from) {
1041
+ const blanks = (j) => {
1042
+ while (j >= from && (text[j] === ' ' || text[j] === '\t')) j--;
1043
+ return j;
1044
+ };
1045
+ let j = blanks(c - 1);
1046
+ if (j < from) return false;
1047
+ if (text[j] === '\n') j -= j - 1 >= from && text[j - 1] === '\r' ? 2 : 1;
1048
+ else if (text[j] === '\r') j--;
1049
+ else return false;
1050
+ j = blanks(j);
1051
+ if (j >= from && text[j] === '+') j = blanks(j - 1);
1052
+ if (j < from || (text[j] !== '"' && text[j] !== "'")) return false;
1053
+ j--;
1054
+ return j >= from && (text[j] === 'n' || text[j] === 'r') && isEscBreak(backslashesBefore(text, j, from), 1);
1055
+ }
1056
+
1057
+ /**
1058
+ * Where the line that holds the BEGIN starts, from `ls`, the start cutOffKeyEnd's prefix walk found:
1059
+ * that, unless the walk stopped at a single quote in escaped text, or -1. The walk stops at an
1060
+ * unescaped single quote, which JSON never escapes (`-KEY = '''` in a JSON string leaves ''), so look
1061
+ * past single quotes for the start of the line: a break, or a double quote that ends a string at this
1062
+ * depth (the string the line is in), or the start of the text. At most LINE_START_SCAN characters
1063
+ * back; past that, -1.
1064
+ */
1065
+ function lineStartPastQuotes(text, ls, u) {
1066
+ if (!(u > 0 && ls > 0 && text[ls - 1] === "'")) return ls;
1067
+ // Native between candidates: a character by character walk was most of the time of a shape that
1068
+ // repeats one 300-character line (pemEscapedLongCodeBeforeBegin), and now goes the whole line.
1069
+ const lim = Math.max(0, ls - LINE_START_SCAN);
1070
+ for (let j = ls - 1; ;) {
1071
+ LINE_BACK_RE.lastIndex = j;
1072
+ const k = j - LINE_BACK_RE.exec(text)[1].length;
1073
+ if (k === 0) return lim === 0 ? 0 : -1;
1074
+ // text[k - 1] is a break, a double quote or a backslash. A backslash run then `n` or `r` at k is
1075
+ // an escaped break at this depth when the run fits it; the line starts after that letter.
1076
+ const c = text.charCodeAt(k - 1);
1077
+ if (c === 92) {
1078
+ const e = text.charCodeAt(k);
1079
+ if (k + 1 < lim) return -1;
1080
+ if ((e === 110 || e === 114) && isEscBreak(backslashesBefore(text, k, 0), u)) return k + 1;
1081
+ } else if (k < lim) return -1;
1082
+ else if (c === 10 || c === 13 || isStringEnd(backslashesBefore(text, k - 1, 0), u)) return k;
1083
+ j = k - 1;
1084
+ }
1085
+ }
1086
+
877
1087
  /**
878
1088
  * Whether a reading is a key: one base64 line of 40+ characters or a start the way a key encoding
879
1089
  * starts; when `loose`, also armor headers or two lines of 16+. The grammar reading (D#168) is not
@@ -993,7 +1203,7 @@ function readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, en
993
1203
  memo ??= memoOf(shape);
994
1204
  const r = memo.long.get(longKey);
995
1205
  if (r !== undefined)
996
- return { longs: r.longs, longest: r.longest, first: r.first, end: r.end, armors };
1206
+ return { longs: r.longs, longest: r.longest, first: r.first, end: r.end, armors, shorts: r.shorts };
997
1207
  }
998
1208
  if (longs++ === 0) first = core;
999
1209
  longest = Math.max(longest, core.length);
@@ -1020,8 +1230,8 @@ function readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, en
1020
1230
  break;
1021
1231
  }
1022
1232
  if (blankKey !== null) memo.blank.set(blankKey, -1);
1023
- if (longKey !== null) memo.long.set(longKey, { longs, longest, first, end });
1024
- return { longs, longest, first, end, armors };
1233
+ if (longKey !== null) memo.long.set(longKey, { longs, longest, first, end, shorts });
1234
+ return { longs, longest, first, end, armors, shorts };
1025
1235
  }
1026
1236
 
1027
1237
  // Separate scanners: KEY_BEGIN_RE's lastIndex drives scrubCutOffKeys' loop while these run.
@@ -1099,7 +1309,23 @@ function scrubCutOffKeys(text) {
1099
1309
  if (m === undefined) memos.set(id, (m = { blank: new Map(), long: new Map() }));
1100
1310
  return m;
1101
1311
  };
1102
- const ctx = { endFollows, nextBegin, shapeOf, memoOf, reread: false };
1312
+ // Whether the line starting at `s` (depth u) is in a unified diff's hunk (hunkReader): the headers
1313
+ // found once per call, on first use, and one reader per depth.
1314
+ let heads = null;
1315
+ const hunks = new Map();
1316
+ const inHunk = (s, u) => {
1317
+ if (heads === null) {
1318
+ heads = [];
1319
+ HUNK_HEADER_RE.lastIndex = 0;
1320
+ for (let h; (h = HUNK_HEADER_RE.exec(text));) {
1321
+ heads.push({ at: h.index, old: h[1] === undefined ? 1 : +h[1], add: h[2] === undefined ? 1 : +h[2] });
1322
+ }
1323
+ }
1324
+ let has = hunks.get(u);
1325
+ if (has === undefined) hunks.set(u, (has = hunkReader(text, heads, u)));
1326
+ return has(s);
1327
+ };
1328
+ const ctx = { endFollows, nextBegin, shapeOf, memoOf, inHunk, reread: false };
1103
1329
  KEY_BEGIN_RE.lastIndex = 0;
1104
1330
  let out = '';
1105
1331
  let last = 0;