claude-mem-lite 6.30.8 → 6.30.9

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.9",
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.9",
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.9",
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.9",
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.9",
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,22 @@ 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
+ // A grammar is a few characters; testing more of a long left context costs a pass over it per BEGIN.
564
+ const PREFIX_GRAMMAR_SCAN = 256;
565
+ // A key line (LONG or CUT) starts with a run of 16+ of these, so a stretch without one holds none.
566
+ const B64_RUN16_RE = /[A-Za-z0-9+/=]{16}/;
567
+ // The longest gap the D#168 skip tests; past it the reading runs.
568
+ const SKIP_GAP_SCAN = 4096;
569
+
552
570
  /** A regex for the line prefix `prefix` has, with digits free and grep's `:`/`-` interchangeable. */
553
571
  function prefixShape(prefix) {
554
572
  const body = prefix.replace(/^[ \t]+/, '');
@@ -654,7 +672,7 @@ function cutRun(core) {
654
672
  * or an identifier of 16-39 characters under a header is prose (delta review P3-5). A complete
655
673
  * block never gets here; the block pattern above takes it first.
656
674
  */
657
- function cutOffKeyEnd(text, b, be) {
675
+ function cutOffKeyEnd(text, b, be, ctx) {
658
676
  // The rest of the BEGIN line: nothing, or base64 chunks, then a break that sets the depth.
659
677
  let i = be;
660
678
  for (
@@ -711,20 +729,150 @@ function cutOffKeyEnd(text, b, be) {
711
729
  // per pass (v6.19.2 pre-tag round-5 review F2).
712
730
  let run = 0;
713
731
  while (run < 16 && b - run > ls && isB64Code(text.charCodeAt(b - run - 1))) run++;
714
- const shape = run >= 16 ? null : prefixShape(text.slice(ls, b));
732
+ const left = text.slice(ls, b);
733
+ const shape = run >= 16 ? null : ctx.shapeOf(left);
715
734
  // The first character of the prefix after its spaces and tabs: a shape's match can only start a
716
735
  // line whose first character after spaces and tabs is this one (a digit, `:` and `-` stand for
717
736
  // more, and none of them begins a blank line below).
718
737
  let pl = ls;
719
738
  while (pl < b && (text.charCodeAt(pl) === 32 || text.charCodeAt(pl) === 9)) pl++;
720
739
  const shapeLead = shape ? text.charCodeAt(pl) : -1;
740
+ const label = text.slice(b, be);
741
+ const read = readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, end, ctx.memoOf);
742
+ const found = read.longs > 0 && keyEvidence(read, label, true) ? read.end : -1;
743
+ // D#168: code between the line prefix and the BEGIN (`-KEY = '''`, ` 5\tKEY = """`) or a
744
+ // string opener before it (`'+`, `"-`) made the whole left context the shape, which no line below
745
+ // starts with, so nothing past the BEGIN line was read and a cut key was stored. Read again with
746
+ // the prefix grammar alone, only when the reading above took nothing below the BEGIN line (its
747
+ // output is unchanged otherwise), and only on stronger evidence than that reading's: two prefixed
748
+ // identifiers under a BEGIN in a diff are a list, not a key. Not when an END follows before the
749
+ // next BEGIN: that is a whole block the block pattern did not take on this pass (a partial BEGIN
750
+ // inside it), which it takes whole once the blocker goes; a ***PEM_KEY*** over its BEGIN would
751
+ // keep every line after the first one that is not base64 (v6.19.4 round-2 F1).
752
+ if (run < 16 && (found === -1 || found <= i)) {
753
+ // Skipped only when it cannot find a key line, never cut short: a read limit at the next header
754
+ // BEGIN stored keys twice (D#169), since a quoted or unclosed BEGIN starts no reading of its own.
755
+ // With no key on the BEGIN line, the first line the reading counts is a key line, which starts
756
+ // with a 16+ base64 run, after blank lines and armor headers only, and a header has a colon. A
757
+ // line holding a private-key BEGIN is neither blank nor a key line (its core holds that text, or
758
+ // starts one `-` into it, and `BEGIN ` ends any run), and no header name holds one (names have
759
+ // no space, the BEGIN has one after `BEGIN`): a header line holding one has its colon before it.
760
+ // So with no run and no colon before the next one (or the end) the reading stops on its line at
761
+ // the latest. Not any `-----BEGIN `: `X-----BEGIN : v` is a header whose name swallows it, read
762
+ // through, and ending the gap there stored the key below it (D#607 F5). Reading every short
763
+ // BEGIN block again (`'+-----BEGIN …` over a bare `+`, 5,400 per pass) took that shape from
764
+ // 4.7x to 10.1x benign under coverage, past the linearity budget on CI.
765
+ // Only over a short gap, since the skip saves a reading and the reading is the answer: with no
766
+ // private-key BEGIN below, the gap is the rest of the text, and testing it on each pass took
767
+ // pemHeaderThenCerts from 75 to 142 ms where the reading stops on its first line.
768
+ if (longs === 0 && next === -1) return found;
769
+ if (longs === 0) {
770
+ const to = ctx.nextBegin(next);
771
+ if (to - next <= SKIP_GAP_SCAN) {
772
+ const gap = text.slice(next, to);
773
+ if (!B64_RUN16_RE.test(gap) && !gap.includes(':')) return found;
774
+ }
775
+ }
776
+ // The grammar starts the line. In escaped text the prefix above stops at an unescaped single
777
+ // quote, which JSON never escapes (`-KEY = '''` in a JSON string leaves ''), so look past single
778
+ // quotes for the start of the line: a break, or a double quote that ends a string at this depth
779
+ // (the string the line is in). At most PREFIX_GRAMMAR_SCAN characters back; past that, no grammar.
780
+ let gl = ls;
781
+ if (u > 0 && ls > 0 && text[ls - 1] === "'") {
782
+ gl = -1;
783
+ for (let j = ls - 1, lim = Math.max(0, ls - PREFIX_GRAMMAR_SCAN); j >= lim; j--) {
784
+ if (j === 0) {
785
+ gl = 0;
786
+ break;
787
+ }
788
+ const ch = text[j - 1];
789
+ if (
790
+ ch === '\n' ||
791
+ ch === '\r' ||
792
+ ((ch === 'n' || ch === 'r') && isEscBreak(backslashesBefore(text, j - 1, 0), u)) ||
793
+ (ch === '"' && isStringEnd(backslashesBefore(text, j - 1, 0), u))
794
+ ) {
795
+ gl = j;
796
+ break;
797
+ }
798
+ }
799
+ }
800
+ const g =
801
+ gl === -1 ? null : PREFIX_GRAMMAR_RE.exec(text.slice(gl, Math.min(b, gl + PREFIX_GRAMMAR_SCAN)));
802
+ const gs = g ? gl + g[0].length - g[1].length : -1;
803
+ let gp = gl;
804
+ while (gp < b && (text.charCodeAt(gp) === 32 || text.charCodeAt(gp) === 9)) gp++;
805
+ // Only when the line is more than its grammar before the BEGIN: an opener before it or code after it.
806
+ if (g && !(gs === gp && gl + g[0].length === b)) {
807
+ const alt = ctx.shapeOf(g[1]);
808
+ if (alt && !ctx.endFollows(be)) {
809
+ let lead = gs;
810
+ while (text.charCodeAt(lead) === 32 || text.charCodeAt(lead) === 9) lead++;
811
+ const again = readKeyLines(
812
+ text,
813
+ next,
814
+ u,
815
+ alt,
816
+ text.charCodeAt(lead),
817
+ longs,
818
+ longest,
819
+ first,
820
+ end,
821
+ ctx.memoOf,
822
+ );
823
+ if (again.longs > 0 && again.end > found && keyEvidence(again, label, false)) return again.end;
824
+ }
825
+ }
826
+ }
827
+ return found;
828
+ }
829
+
830
+ /**
831
+ * Whether a reading is a key: one base64 line of 40+ characters or a start the way a key encoding
832
+ * starts; when `loose`, also armor headers or two lines of 16+. The grammar reading (D#168) is not
833
+ * loose: armor headers over an identifier (`Proc-Type`, `DEK-Info`, then `loadPrivateKeyFromDisk`)
834
+ * read as a key on 858-899 of ~12,000 keyless fuzz texts per seed, which the reading above keeps.
835
+ */
836
+ function keyEvidence(r, label, loose) {
837
+ // Under an opener whose prefix does not match the diff lines below it (`'+`, `rb'+`,
838
+ // `+KEY = '''`) the line still starts with its `+`; the magic is tested without it (D#167).
839
+ const body = r.first.replace(/^[-+]/, '');
840
+ const magic = startsLikeKey(body) || (label.includes('PGP') && PGP_MAGIC_RE.test(body));
841
+ return (loose && (r.longs >= 2 || r.armors > 0)) || r.longest >= 40 || magic;
842
+ }
721
843
 
844
+ /**
845
+ * The lines of a cut key below its BEGIN line, read with one line shape, from the state the BEGIN
846
+ * line left (`longs`, `longest`, `first`, `end`: a key pasted onto the BEGIN line counts as one line).
847
+ *
848
+ * `memoOf(shape)` (one scrubCutOffKeys call's) keeps what a reading found past two points,
849
+ * keyed by the line and the depth, of which the rest of the reading is a function (with the shape:
850
+ * its lead is the first character of the prefix the source was built from, and the one test that
851
+ * reads it tells only a quote or `<` from the rest, which the source keeps literally): where
852
+ * a run of blank lines ends, from its BLANK_MEMO_AFTER-th line (the run only sets pendingBlank),
853
+ * and the result from the first counted base64 line on, when nothing was counted before it (from
854
+ * there `armors` is never read, and the state is that line's alone). D#607 F1: 16 header lines
855
+ * that each hold a BEGIN (`-Comment: -----BEGIN …`) under a code BEGIN each read the same run again
856
+ * under the prefix grammar and refused it, 33x benign at 200k chars (186x under coverage). Short
857
+ * runs never reach the memo: a memo of every blank run made two shapes slower (D#500). Headers at
858
+ * different depths still read the run once per depth: a line's end depends on it.
859
+ */
860
+ function readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, end, memoOf) {
861
+ let memo = null;
722
862
  let armors = 0;
723
863
  let shorts = 0;
724
864
  let pendingBlank = false;
865
+ let blanks = 0;
866
+ let blankKey = null;
867
+ let longKey = null;
725
868
  while (next !== -1) {
869
+ const at = next;
726
870
  // Another BEGIN line is never part of this key; stop before reading it whole.
727
- if (!shape && text.startsWith('-----BEGIN ', next)) break;
871
+ if (!shape && text.startsWith('-----BEGIN ', next)) {
872
+ if (blankKey !== null) memo.blank.set(blankKey, at);
873
+ blankKey = null;
874
+ break;
875
+ }
728
876
  const line = nextLine(text, next, u);
729
877
  // Blank lines read here without lineCore, where the runs a BEGIN reads through spent a third of
730
878
  // their time (D#511). Spaces and tabs, at most one quote or backtick, spaces and tabs: blank
@@ -750,11 +898,26 @@ function cutOffKeyEnd(text, b, be) {
750
898
  next = line.next;
751
899
  if (shorts > 0) break;
752
900
  pendingBlank = true;
901
+ if (++blanks === BLANK_MEMO_AFTER) {
902
+ const key = at + ',' + u;
903
+ memo ??= memoOf(shape);
904
+ const to = memo.blank.get(key);
905
+ if (to !== undefined) {
906
+ next = to;
907
+ blanks = 0;
908
+ } else blankKey = key;
909
+ }
753
910
  continue;
754
911
  }
755
912
  const { core, start, end: coreEnd } = lineCore(text, next, line.end, shape);
756
913
  const kind = classify(core);
757
914
  next = line.next;
915
+ // A run ends here (blankKey is set only inside one).
916
+ if (kind !== BLANK && blanks !== 0) {
917
+ blanks = 0;
918
+ if (blankKey !== null) memo.blank.set(blankKey, at);
919
+ blankKey = null;
920
+ }
758
921
  if (kind === END) {
759
922
  if (longs > 0) end = start + KEY_END_LINE_RE.exec(core)[0].length;
760
923
  break;
@@ -762,6 +925,15 @@ function cutOffKeyEnd(text, b, be) {
762
925
  if (kind === BLANK) {
763
926
  if (shorts > 0) break;
764
927
  pendingBlank = true;
928
+ if (++blanks === BLANK_MEMO_AFTER) {
929
+ const key = at + ',' + u;
930
+ memo ??= memoOf(shape);
931
+ const to = memo.blank.get(key);
932
+ if (to !== undefined) {
933
+ next = to;
934
+ blanks = 0;
935
+ } else blankKey = key;
936
+ }
765
937
  continue;
766
938
  }
767
939
  if (longs === 0) {
@@ -769,6 +941,13 @@ function cutOffKeyEnd(text, b, be) {
769
941
  if (kind !== LONG && kind !== CUT) break;
770
942
  }
771
943
  if (kind === LONG && shorts === 0) {
944
+ if (longs === 0) {
945
+ longKey = at + ',' + u;
946
+ memo ??= memoOf(shape);
947
+ const r = memo.long.get(longKey);
948
+ if (r !== undefined)
949
+ return { longs: r.longs, longest: r.longest, first: r.first, end: r.end, armors };
950
+ }
772
951
  if (longs++ === 0) first = core;
773
952
  longest = Math.max(longest, core.length);
774
953
  end = coreEnd;
@@ -793,22 +972,92 @@ function cutOffKeyEnd(text, b, be) {
793
972
  }
794
973
  break;
795
974
  }
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;
975
+ if (blankKey !== null) memo.blank.set(blankKey, -1);
976
+ if (longKey !== null) memo.long.set(longKey, { longs, longest, first, end });
977
+ return { longs, longest, first, end, armors };
978
+ }
979
+
980
+ // Separate scanners: KEY_BEGIN_RE's lastIndex drives scrubCutOffKeys' loop while these run.
981
+ const KEY_BEGIN_SCAN_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
982
+ const KEY_END_SCAN_RE = new RegExp(KEY_END_RE.source, 'g');
983
+ const KEY_BEGIN_NEXT_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
984
+
985
+ /** Start offsets of every match of the global regex `re` in `text`. */
986
+ function matchStarts(re, text) {
987
+ const starts = [];
988
+ re.lastIndex = 0;
989
+ for (let m = re.exec(text); m; m = re.exec(text)) starts.push(m.index);
990
+ return starts;
991
+ }
992
+
993
+ /** The first value in ascending `arr` at or after `x`, or Infinity. */
994
+ function firstAtOrAfter(arr, x) {
995
+ let lo = 0;
996
+ let hi = arr.length;
997
+ while (lo < hi) {
998
+ const mid = (lo + hi) >> 1;
999
+ if (arr[mid] < x) lo = mid + 1;
1000
+ else hi = mid;
1001
+ }
1002
+ return lo < arr.length ? arr[lo] : Infinity;
802
1003
  }
803
1004
 
804
1005
  function scrubCutOffKeys(text) {
805
1006
  if (!text.includes('PRIVATE KEY')) return text;
1007
+ // Whether an END follows offset `be` before the next BEGIN. Positions found once per call, on
1008
+ // first use, so a text with many BEGINs is not rescanned to its end for each.
1009
+ let begins = null;
1010
+ let ends = null;
1011
+ const endFollows = (be) => {
1012
+ if (begins === null) {
1013
+ begins = matchStarts(KEY_BEGIN_SCAN_RE, text);
1014
+ ends = matchStarts(KEY_END_SCAN_RE, text);
1015
+ }
1016
+ const e = firstAtOrAfter(ends, be);
1017
+ return e !== Infinity && e < firstAtOrAfter(begins, be);
1018
+ };
1019
+ // The first private-key BEGIN at or after line start `x`, or Infinity: the leftmost match from a
1020
+ // start, kept for every x between that start and it (a line start is never inside a match, which
1021
+ // holds no break). Not the positions above: found for every BEGIN on each pass, they cost a shape
1022
+ // whose skip holds before endFollows is asked 14-18% under coverage (a binary search in them, 9.6%).
1023
+ let beginFrom = Infinity;
1024
+ let beginAt = Infinity;
1025
+ const nextBegin = (x) => {
1026
+ if (!(beginFrom <= x && beginAt >= x)) {
1027
+ beginFrom = x;
1028
+ KEY_BEGIN_NEXT_RE.lastIndex = x;
1029
+ const m = KEY_BEGIN_NEXT_RE.exec(text);
1030
+ beginAt = m ? m.index : Infinity;
1031
+ }
1032
+ return beginAt;
1033
+ };
1034
+ // One line-prefix regex per distinct short prefix, for this call only: a text repeating one BEGIN
1035
+ // line built the same regex for each BEGIN, 15-22% of those shapes' time under coverage.
1036
+ const shapes = new Map();
1037
+ const shapeOf = (prefix) => {
1038
+ if (prefix.length > 64) return prefixShape(prefix);
1039
+ let r = shapes.get(prefix);
1040
+ if (r === undefined) shapes.set(prefix, (r = prefixShape(prefix)));
1041
+ return r;
1042
+ };
1043
+ // What a reading found past a point, per line shape, for this call only (readKeyLines). Keyed by
1044
+ // the shape's source, not the regex: prefixes that differ only in digits (Read columns, grep line
1045
+ // numbers) build the same pattern from different strings, and one over 64 characters a new regex
1046
+ // per BEGIN, which kept every header BEGIN of numbered output in a memo of its own (D#607 r2 P2-1).
1047
+ const memos = new Map();
1048
+ const memoOf = (shape) => {
1049
+ const id = shape && shape.source;
1050
+ let m = memos.get(id);
1051
+ if (m === undefined) memos.set(id, (m = { blank: new Map(), long: new Map() }));
1052
+ return m;
1053
+ };
1054
+ const ctx = { endFollows, nextBegin, shapeOf, memoOf };
806
1055
  KEY_BEGIN_RE.lastIndex = 0;
807
1056
  let out = '';
808
1057
  let last = 0;
809
1058
  let m;
810
1059
  while ((m = KEY_BEGIN_RE.exec(text))) {
811
- const end = cutOffKeyEnd(text, m.index, m.index + m[0].length);
1060
+ const end = cutOffKeyEnd(text, m.index, m.index + m[0].length, ctx);
812
1061
  if (end === -1) continue;
813
1062
  out += text.slice(last, m.index) + PEM_MARK;
814
1063
  last = end;