claude-mem-lite 6.30.8 → 6.30.10

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.8",
12
+ "version": "6.30.10",
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.8",
3
+ "version": "6.30.10",
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,19 +1,19 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.30.8",
3
+ "version": "6.30.10",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "claude-mem-lite",
9
- "version": "6.30.8",
9
+ "version": "6.30.10",
10
10
  "os": [
11
11
  "darwin",
12
12
  "linux",
13
13
  "win32"
14
14
  ],
15
15
  "dependencies": {
16
- "@modelcontextprotocol/sdk": "^1.30.0",
16
+ "@modelcontextprotocol/sdk": "^1.32.2",
17
17
  "better-sqlite3": "^13.0.3",
18
18
  "zod": "^4.6.7"
19
19
  },
@@ -26,7 +26,7 @@
26
26
  "acorn": "8.19.0",
27
27
  "eslint": "^10.13.0",
28
28
  "fast-check": "^4.10.2",
29
- "knip": "^6.42.1",
29
+ "knip": "^6.43.0",
30
30
  "picomatch": "^4.0.4",
31
31
  "prettier": "3.9.10",
32
32
  "vitest": "^5.0.3"
@@ -36,21 +36,21 @@
36
36
  }
37
37
  },
38
38
  "node_modules/@hono/node-server": {
39
- "version": "2.0.11",
40
- "resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-2.0.11.tgz",
41
- "integrity": "sha512-bjD221KPLoJTWUwso1J6fGKiTXEUFedG/s0visavY4zakFPkeGURMRNly+FhBHs7T8Dz4qHaZIMX9ZoJHSJtKA==",
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==",
42
42
  "license": "MIT",
43
43
  "engines": {
44
44
  "node": ">=20"
45
45
  },
46
46
  "peerDependencies": {
47
- "hono": "^4"
47
+ "hono": "^4 || ^5.0.0-0"
48
48
  }
49
49
  },
50
50
  "node_modules/@modelcontextprotocol/sdk": {
51
- "version": "1.32.1",
52
- "resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.32.1.tgz",
53
- "integrity": "sha512-2DdE+SJDtzLEEWzY1ZjY7Q+VcPhcV1KisD3zI4u0XZyktsjHum1mwbMI+JaulUBi2OZk+KJAi2uPXzxichPkdw==",
51
+ "version": "1.32.2",
52
+ "resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.32.2.tgz",
53
+ "integrity": "sha512-SWsVtH/0vULSQStoZ6xxH2K09Li8fXYTMC64ZeogCdpZ3XSi1eCPOByV1OpuJxuI3OcmHhosDWYyii07tpPsLw==",
54
54
  "license": "MIT",
55
55
  "dependencies": {
56
56
  "@hono/node-server": "^1.19.9 || ^2.0.5",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.30.8",
3
+ "version": "6.30.10",
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",
@@ -236,7 +236,7 @@
236
236
  "README.md"
237
237
  ],
238
238
  "dependencies": {
239
- "@modelcontextprotocol/sdk": "^1.30.0",
239
+ "@modelcontextprotocol/sdk": "^1.32.2",
240
240
  "better-sqlite3": "^13.0.3",
241
241
  "zod": "^4.6.7"
242
242
  },
@@ -254,7 +254,7 @@
254
254
  "acorn": "8.19.0",
255
255
  "eslint": "^10.13.0",
256
256
  "fast-check": "^4.10.2",
257
- "knip": "^6.42.1",
257
+ "knip": "^6.43.0",
258
258
  "picomatch": "^4.0.4",
259
259
  "prettier": "3.9.10",
260
260
  "vitest": "^5.0.3"
package/secret-scrub.mjs CHANGED
@@ -421,6 +421,8 @@ const startsLikeKey = (s) => KEY_MAGIC_RE.test(s) || isDerKeyStart(s);
421
421
  const ARMOR_HEADER_RE =
422
422
  /^(?:Proc-Type|DEK-Info|Content-Domain|Originator-ID-(?:Asymmetric|Symmetric)|Originator-Certificate|Issuer-Certificate|MIC-Info|Key-Info|Recipient-ID-(?:Asymmetric|Symmetric)|CRL|Version|Comment|Hash|Charset|MessageID|X-[A-Za-z0-9-]+)[ \t]*:/i;
423
423
  const MAX_ARMOR_HEADERS = 16;
424
+ // The blank line of a run from which readKeyLines asks its memo where the run ends.
425
+ const BLANK_MEMO_AFTER = 8;
424
426
  // A JS/Python string split across source lines: `…\n" +` then `"…` on the next line. Not a comma:
425
427
  // `'…\n',` then `'…'` is the next element of a list, and its first word is not the key's last line.
426
428
  // One whitespace quantifier on each side of the `+`: `[ \t]*\+?[ \t]*` split a run between two
@@ -549,6 +551,38 @@ function prevLine(text, ls, u, floor) {
549
551
  return { start: i + 1, end: bs };
550
552
  }
551
553
 
554
+ // D#168: the line prefix grammars a BEGIN line can carry with code after them, before the BEGIN:
555
+ // a Read column (` 5\t`, `5→`), a diff mark, a quote, a grep column (`path:5:`, `path:`),
556
+ // after at most one string opener (`'+`, `b'+`, `'''+`). Named forms only, not "what the next line
557
+ // shares with this one": that reading took `//`, `#` and a bare `>` for prefixes and was withdrawn
558
+ // from 6.19.4 for the prose it erased and the keys it let through on a later pass. The diff mark
559
+ // comes before the grep forms, so `-key: '''` is a diff line, not a grep path `-key:`. In escaped
560
+ // text a tab is `\t` and a quote may be `\"`.
561
+ const PREFIX_GRAMMAR_RE =
562
+ /^[ \t]*(?:[rRbBuUfF]{0,2}(?:\\*['"`]){1,3})?([ \t]*\d+(?:\t|\\+t)|\d+→|[-+]|(?:>[ \t]?)+|[^\s'"`:\\]+[:-]\d+[:-]|[^\s'"`:\\]+:)/;
563
+ // D#614 A2: a grep column after any path, up to the line's last `:N:` or `-N-` (a number past the path
564
+ // is the grep's; one inside code before the BEGIN is rare): a backslash, a space, a drive letter or
565
+ // another colon, a quote or a backtick in the path, or a leading `-`, `+` or `>`, which the grammar
566
+ // above reads as a diff mark or a quote.
567
+ const GREP_ANY_PATH_RE = /^[ \t]*(\S.*(?::\d+:|-\d+-))/;
568
+ // A grammar is a few characters; testing more of a long left context costs a pass over it per BEGIN.
569
+ const PREFIX_GRAMMAR_SCAN = 256;
570
+ // How far escaped text is searched back for the start of a BEGIN's line past single quotes (D#614 A3).
571
+ // It was PREFIX_GRAMMAR_SCAN, and a code line of more than 256 characters before its last quote
572
+ // (keyword arguments, a dict literal) left no grammar, so the key under it was stored. Not unbounded:
573
+ // the walks of BEGINs at one escape depth never overlap (each BEGIN's break ends the next one's
574
+ // walk), but those at different depths cross the same run, judging each quote or backslash in it.
575
+ // One BEGIN per depth (1, 2, 4, 8) after 150k characters of escaped quotes read 18x benign under
576
+ // coverage unbounded, 1.2x at this bound.
577
+ const LINE_START_SCAN = 4096;
578
+ // From a position back over the characters that cannot end a line in escaped text: anything but a
579
+ // break, a double quote or a backslash. A lookbehind is matched backward, so this runs natively.
580
+ const LINE_BACK_RE = /(?<=([^\n\r"\\]*))/y;
581
+ // A key line (LONG or CUT) starts with a run of 16+ of these, so a stretch without one holds none.
582
+ const B64_RUN16_RE = /[A-Za-z0-9+/=]{16}/;
583
+ // The longest gap the D#168 skip tests; past it the reading runs.
584
+ const SKIP_GAP_SCAN = 4096;
585
+
552
586
  /** A regex for the line prefix `prefix` has, with digits free and grep's `:`/`-` interchangeable. */
553
587
  function prefixShape(prefix) {
554
588
  const body = prefix.replace(/^[ \t]+/, '');
@@ -654,7 +688,7 @@ function cutRun(core) {
654
688
  * or an identifier of 16-39 characters under a header is prose (delta review P3-5). A complete
655
689
  * block never gets here; the block pattern above takes it first.
656
690
  */
657
- function cutOffKeyEnd(text, b, be) {
691
+ function cutOffKeyEnd(text, b, be, ctx) {
658
692
  // The rest of the BEGIN line: nothing, or base64 chunks, then a break that sets the depth.
659
693
  let i = be;
660
694
  for (
@@ -711,20 +745,181 @@ function cutOffKeyEnd(text, b, be) {
711
745
  // per pass (v6.19.2 pre-tag round-5 review F2).
712
746
  let run = 0;
713
747
  while (run < 16 && b - run > ls && isB64Code(text.charCodeAt(b - run - 1))) run++;
714
- const shape = run >= 16 ? null : prefixShape(text.slice(ls, b));
748
+ const left = text.slice(ls, b);
749
+ const shape = run >= 16 ? null : ctx.shapeOf(left);
715
750
  // The first character of the prefix after its spaces and tabs: a shape's match can only start a
716
751
  // line whose first character after spaces and tabs is this one (a digit, `:` and `-` stand for
717
752
  // more, and none of them begins a blank line below).
718
753
  let pl = ls;
719
754
  while (pl < b && (text.charCodeAt(pl) === 32 || text.charCodeAt(pl) === 9)) pl++;
720
755
  const shapeLead = shape ? text.charCodeAt(pl) : -1;
756
+ const label = text.slice(b, be);
757
+ const read = readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, end, ctx.memoOf);
758
+ const found = read.longs > 0 && keyEvidence(read, label, true) ? read.end : -1;
759
+ // D#168: code between the line prefix and the BEGIN (`-KEY = '''`, ` 5\tKEY = """`) or a
760
+ // string opener before it (`'+`, `"-`) made the whole left context the shape, which no line below
761
+ // starts with, so nothing past the BEGIN line was read and a cut key was stored. Read again with
762
+ // the prefix grammar alone, only when the reading above took nothing below the BEGIN line (its
763
+ // output is unchanged otherwise), and only on stronger evidence than that reading's: two prefixed
764
+ // identifiers under a BEGIN in a diff are a list, not a key. Not when an END follows before the
765
+ // next BEGIN: that is a whole block the block pattern did not take on this pass (a partial BEGIN
766
+ // inside it), which it takes whole once the blocker goes; a ***PEM_KEY*** over its BEGIN would
767
+ // keep every line after the first one that is not base64 (v6.19.4 round-2 F1).
768
+ if (run < 16 && (found === -1 || found <= i)) {
769
+ // Skipped only when it cannot find a key line, never cut short: a read limit at the next header
770
+ // BEGIN stored keys twice (D#169), since a quoted or unclosed BEGIN starts no reading of its own.
771
+ // With no key on the BEGIN line, the first line the reading counts is a key line, which starts
772
+ // with a 16+ base64 run, after blank lines and armor headers only, and a header has a colon. A
773
+ // line holding a private-key BEGIN is neither blank nor a key line (its core holds that text, or
774
+ // starts one `-` into it, and `BEGIN ` ends any run), and no header name holds one (names have
775
+ // no space, the BEGIN has one after `BEGIN`): a header line holding one has its colon before it.
776
+ // So with no run and no colon before the next one (or the end) the reading stops on its line at
777
+ // the latest. Not any `-----BEGIN `: `X-----BEGIN : v` is a header whose name swallows it, read
778
+ // through, and ending the gap there stored the key below it (D#607 F5). Reading every short
779
+ // BEGIN block again (`'+-----BEGIN …` over a bare `+`, 5,400 per pass) took that shape from
780
+ // 4.7x to 10.1x benign under coverage, past the linearity budget on CI.
781
+ // Only over a short gap, since the skip saves a reading and the reading is the answer: with no
782
+ // private-key BEGIN below, the gap is the rest of the text, and testing it on each pass took
783
+ // pemHeaderThenCerts from 75 to 142 ms where the reading stops on its first line.
784
+ if (longs === 0 && next === -1) return found;
785
+ if (longs === 0) {
786
+ const to = ctx.nextBegin(next);
787
+ if (to - next <= SKIP_GAP_SCAN) {
788
+ const gap = text.slice(next, to);
789
+ if (!B64_RUN16_RE.test(gap) && !gap.includes(':')) return found;
790
+ }
791
+ }
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
+ }
828
+ const head = gl === -1 ? '' : text.slice(gl, Math.min(b, gl + PREFIX_GRAMMAR_SCAN));
829
+ const g = gl === -1 ? null : PREFIX_GRAMMAR_RE.exec(head);
830
+ const gs = g ? gl + g[0].length - g[1].length : -1;
831
+ let gp = gl;
832
+ 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
+ // Only when the line is more than its grammar before the BEGIN: an opener before it or code after it.
844
+ if (g && !(gs === gp && gl + g[0].length === b)) {
845
+ let lead = gs;
846
+ while (text.charCodeAt(lead) === 32 || text.charCodeAt(lead) === 9) lead++;
847
+ const r = reread(g[1], text.charCodeAt(lead));
848
+ if (r !== -1) return r;
849
+ }
850
+ // D#614 A2: a grep column after a path the grammar refuses; not the column the grammar read
851
+ // already, nor the first reading's own prefix. That prefix is the column whenever nothing stands
852
+ // between it and the BEGIN, except in escaped text after a quote in the path (`it's/`), where the
853
+ // prefix starts past the quote.
854
+ const p = gl === -1 ? null : GREP_ANY_PATH_RE.exec(head);
855
+ if (p && !(g && g[1] === p[1]) && p[1] !== left.replace(/^[ \t]+/, '')) {
856
+ const r = reread(p[1], text.charCodeAt(gl + p[0].length - p[1].length));
857
+ if (r !== -1) return r;
858
+ }
859
+ // D#614 A1: a unified diff's context line starts with its space, with code after it or none, and
860
+ // 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);
871
+ if (r !== -1) return r;
872
+ }
873
+ }
874
+ return found;
875
+ }
721
876
 
877
+ /**
878
+ * Whether a reading is a key: one base64 line of 40+ characters or a start the way a key encoding
879
+ * starts; when `loose`, also armor headers or two lines of 16+. The grammar reading (D#168) is not
880
+ * loose: armor headers over an identifier (`Proc-Type`, `DEK-Info`, then `loadPrivateKeyFromDisk`)
881
+ * read as a key on 858-899 of ~12,000 keyless fuzz texts per seed, which the reading above keeps.
882
+ */
883
+ function keyEvidence(r, label, loose) {
884
+ // Under an opener whose prefix does not match the diff lines below it (`'+`, `rb'+`,
885
+ // `+KEY = '''`) the line still starts with its `+`; the magic is tested without it (D#167).
886
+ const body = r.first.replace(/^[-+]/, '');
887
+ const magic = startsLikeKey(body) || (label.includes('PGP') && PGP_MAGIC_RE.test(body));
888
+ return (loose && (r.longs >= 2 || r.armors > 0)) || r.longest >= 40 || magic;
889
+ }
890
+
891
+ /**
892
+ * The lines of a cut key below its BEGIN line, read with one line shape, from the state the BEGIN
893
+ * line left (`longs`, `longest`, `first`, `end`: a key pasted onto the BEGIN line counts as one line).
894
+ *
895
+ * `memoOf(shape)` (one scrubCutOffKeys call's) keeps what a reading found past two points,
896
+ * keyed by the line and the depth, of which the rest of the reading is a function (with the shape:
897
+ * its lead is the first character of the prefix the source was built from, and the one test that
898
+ * reads it tells only a quote or `<` from the rest, which the source keeps literally): where
899
+ * a run of blank lines ends, from its BLANK_MEMO_AFTER-th line (the run only sets pendingBlank),
900
+ * and the result from the first counted base64 line on, when nothing was counted before it (from
901
+ * there `armors` is never read, and the state is that line's alone). D#607 F1: 16 header lines
902
+ * that each hold a BEGIN (`-Comment: -----BEGIN …`) under a code BEGIN each read the same run again
903
+ * under the prefix grammar and refused it, 33x benign at 200k chars (186x under coverage). Short
904
+ * runs never reach the memo: a memo of every blank run made two shapes slower (D#500). Headers at
905
+ * different depths still read the run once per depth: a line's end depends on it.
906
+ */
907
+ function readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, end, memoOf) {
908
+ let memo = null;
722
909
  let armors = 0;
723
910
  let shorts = 0;
724
911
  let pendingBlank = false;
912
+ let blanks = 0;
913
+ let blankKey = null;
914
+ let longKey = null;
725
915
  while (next !== -1) {
916
+ const at = next;
726
917
  // Another BEGIN line is never part of this key; stop before reading it whole.
727
- if (!shape && text.startsWith('-----BEGIN ', next)) break;
918
+ if (!shape && text.startsWith('-----BEGIN ', next)) {
919
+ if (blankKey !== null) memo.blank.set(blankKey, at);
920
+ blankKey = null;
921
+ break;
922
+ }
728
923
  const line = nextLine(text, next, u);
729
924
  // Blank lines read here without lineCore, where the runs a BEGIN reads through spent a third of
730
925
  // their time (D#511). Spaces and tabs, at most one quote or backtick, spaces and tabs: blank
@@ -750,11 +945,26 @@ function cutOffKeyEnd(text, b, be) {
750
945
  next = line.next;
751
946
  if (shorts > 0) break;
752
947
  pendingBlank = true;
948
+ if (++blanks === BLANK_MEMO_AFTER) {
949
+ const key = at + ',' + u;
950
+ memo ??= memoOf(shape);
951
+ const to = memo.blank.get(key);
952
+ if (to !== undefined) {
953
+ next = to;
954
+ blanks = 0;
955
+ } else blankKey = key;
956
+ }
753
957
  continue;
754
958
  }
755
959
  const { core, start, end: coreEnd } = lineCore(text, next, line.end, shape);
756
960
  const kind = classify(core);
757
961
  next = line.next;
962
+ // A run ends here (blankKey is set only inside one).
963
+ if (kind !== BLANK && blanks !== 0) {
964
+ blanks = 0;
965
+ if (blankKey !== null) memo.blank.set(blankKey, at);
966
+ blankKey = null;
967
+ }
758
968
  if (kind === END) {
759
969
  if (longs > 0) end = start + KEY_END_LINE_RE.exec(core)[0].length;
760
970
  break;
@@ -762,6 +972,15 @@ function cutOffKeyEnd(text, b, be) {
762
972
  if (kind === BLANK) {
763
973
  if (shorts > 0) break;
764
974
  pendingBlank = true;
975
+ if (++blanks === BLANK_MEMO_AFTER) {
976
+ const key = at + ',' + u;
977
+ memo ??= memoOf(shape);
978
+ const to = memo.blank.get(key);
979
+ if (to !== undefined) {
980
+ next = to;
981
+ blanks = 0;
982
+ } else blankKey = key;
983
+ }
765
984
  continue;
766
985
  }
767
986
  if (longs === 0) {
@@ -769,6 +988,13 @@ function cutOffKeyEnd(text, b, be) {
769
988
  if (kind !== LONG && kind !== CUT) break;
770
989
  }
771
990
  if (kind === LONG && shorts === 0) {
991
+ if (longs === 0) {
992
+ longKey = at + ',' + u;
993
+ memo ??= memoOf(shape);
994
+ const r = memo.long.get(longKey);
995
+ if (r !== undefined)
996
+ return { longs: r.longs, longest: r.longest, first: r.first, end: r.end, armors };
997
+ }
772
998
  if (longs++ === 0) first = core;
773
999
  longest = Math.max(longest, core.length);
774
1000
  end = coreEnd;
@@ -793,23 +1019,109 @@ function cutOffKeyEnd(text, b, be) {
793
1019
  }
794
1020
  break;
795
1021
  }
796
- if (longs === 0) return -1;
797
- // Under an opener whose prefix does not match the diff lines below it (`'+`, `rb'+`,
798
- // `+KEY = '''`) the line still starts with its `+`; the magic is tested without it (D#167).
799
- const body = first.replace(/^[-+]/, '');
800
- const magic = startsLikeKey(body) || (text.slice(b, be).includes('PGP') && PGP_MAGIC_RE.test(body));
801
- return longs >= 2 || armors > 0 || longest >= 40 || magic ? end : -1;
1022
+ 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 };
1025
+ }
1026
+
1027
+ // Separate scanners: KEY_BEGIN_RE's lastIndex drives scrubCutOffKeys' loop while these run.
1028
+ const KEY_BEGIN_SCAN_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
1029
+ const KEY_END_SCAN_RE = new RegExp(KEY_END_RE.source, 'g');
1030
+ const KEY_BEGIN_NEXT_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
1031
+ const KEY_BEGIN_INNER_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
1032
+
1033
+ /** Start offsets of every match of the global regex `re` in `text`. */
1034
+ function matchStarts(re, text) {
1035
+ const starts = [];
1036
+ re.lastIndex = 0;
1037
+ for (let m = re.exec(text); m; m = re.exec(text)) starts.push(m.index);
1038
+ return starts;
1039
+ }
1040
+
1041
+ /** The first value in ascending `arr` at or after `x`, or Infinity. */
1042
+ function firstAtOrAfter(arr, x) {
1043
+ let lo = 0;
1044
+ let hi = arr.length;
1045
+ while (lo < hi) {
1046
+ const mid = (lo + hi) >> 1;
1047
+ if (arr[mid] < x) lo = mid + 1;
1048
+ else hi = mid;
1049
+ }
1050
+ return lo < arr.length ? arr[lo] : Infinity;
802
1051
  }
803
1052
 
804
1053
  function scrubCutOffKeys(text) {
805
1054
  if (!text.includes('PRIVATE KEY')) return text;
1055
+ // Whether an END follows offset `be` before the next BEGIN. Positions found once per call, on
1056
+ // first use, so a text with many BEGINs is not rescanned to its end for each.
1057
+ let begins = null;
1058
+ let ends = null;
1059
+ const endFollows = (be) => {
1060
+ if (begins === null) {
1061
+ begins = matchStarts(KEY_BEGIN_SCAN_RE, text);
1062
+ ends = matchStarts(KEY_END_SCAN_RE, text);
1063
+ }
1064
+ const e = firstAtOrAfter(ends, be);
1065
+ return e !== Infinity && e < firstAtOrAfter(begins, be);
1066
+ };
1067
+ // The first private-key BEGIN at or after line start `x`, or Infinity: the leftmost match from a
1068
+ // start, kept for every x between that start and it (a line start is never inside a match, which
1069
+ // holds no break). Not the positions above: found for every BEGIN on each pass, they cost a shape
1070
+ // whose skip holds before endFollows is asked 14-18% under coverage (a binary search in them, 9.6%).
1071
+ let beginFrom = Infinity;
1072
+ let beginAt = Infinity;
1073
+ const nextBegin = (x) => {
1074
+ if (!(beginFrom <= x && beginAt >= x)) {
1075
+ beginFrom = x;
1076
+ KEY_BEGIN_NEXT_RE.lastIndex = x;
1077
+ const m = KEY_BEGIN_NEXT_RE.exec(text);
1078
+ beginAt = m ? m.index : Infinity;
1079
+ }
1080
+ return beginAt;
1081
+ };
1082
+ // One line-prefix regex per distinct short prefix, for this call only: a text repeating one BEGIN
1083
+ // line built the same regex for each BEGIN, 15-22% of those shapes' time under coverage.
1084
+ const shapes = new Map();
1085
+ const shapeOf = (prefix) => {
1086
+ if (prefix.length > 64) return prefixShape(prefix);
1087
+ let r = shapes.get(prefix);
1088
+ if (r === undefined) shapes.set(prefix, (r = prefixShape(prefix)));
1089
+ return r;
1090
+ };
1091
+ // What a reading found past a point, per line shape, for this call only (readKeyLines). Keyed by
1092
+ // the shape's source, not the regex: prefixes that differ only in digits (Read columns, grep line
1093
+ // numbers) build the same pattern from different strings, and one over 64 characters a new regex
1094
+ // per BEGIN, which kept every header BEGIN of numbered output in a memo of its own (D#607 r2 P2-1).
1095
+ const memos = new Map();
1096
+ const memoOf = (shape) => {
1097
+ const id = shape && shape.source;
1098
+ let m = memos.get(id);
1099
+ if (m === undefined) memos.set(id, (m = { blank: new Map(), long: new Map() }));
1100
+ return m;
1101
+ };
1102
+ const ctx = { endFollows, nextBegin, shapeOf, memoOf, reread: false };
806
1103
  KEY_BEGIN_RE.lastIndex = 0;
807
1104
  let out = '';
808
1105
  let last = 0;
809
1106
  let m;
810
1107
  while ((m = KEY_BEGIN_RE.exec(text))) {
811
- const end = cutOffKeyEnd(text, m.index, m.index + m[0].length);
1108
+ ctx.reread = false;
1109
+ let end = cutOffKeyEnd(text, m.index, m.index + m[0].length, ctx);
812
1110
  if (end === -1) continue;
1111
+ // D#614 A4 (D#633): a span read again with a named grammar's shape can take a header line that
1112
+ // holds another private-key BEGIN for an armor header and end where that BEGIN's own reading goes
1113
+ // on (a 15-character line, a line that is only its prefix, another header). The span covered the
1114
+ // BEGIN, which then started no reading, so the lines past the span were stored; 6.30.8, whose
1115
+ // first reading found nothing there, read the inner BEGIN and erased them. Each private-key BEGIN
1116
+ // inside such a span is read too, and the span ends where the farthest of them ends. Only for these
1117
+ // readings: under a BEGIN with nothing before it the same holds in 6.30.8 (a separate defect).
1118
+ if (ctx.reread) {
1119
+ KEY_BEGIN_INNER_RE.lastIndex = m.index + m[0].length;
1120
+ for (let n; (n = KEY_BEGIN_INNER_RE.exec(text)) && n.index < end;) {
1121
+ const e = cutOffKeyEnd(text, n.index, n.index + n[0].length, ctx);
1122
+ if (e > end) end = e;
1123
+ }
1124
+ }
813
1125
  out += text.slice(last, m.index) + PEM_MARK;
814
1126
  last = end;
815
1127
  KEY_BEGIN_RE.lastIndex = end;