claude-mem-lite 6.30.9 → 6.30.11

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.9",
12
+ "version": "6.30.11",
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.9",
3
+ "version": "6.30.11",
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.9",
3
+ "version": "6.30.11",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "claude-mem-lite",
9
- "version": "6.30.9",
9
+ "version": "6.30.11",
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.9",
3
+ "version": "6.30.11",
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
@@ -560,8 +560,24 @@ function prevLine(text, ls, u, floor) {
560
560
  // text a tab is `\t` and a quote may be `\"`.
561
561
  const PREFIX_GRAMMAR_RE =
562
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+-))/;
563
568
  // A grammar is a few characters; testing more of a long left context costs a pass over it per BEGIN.
564
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;
565
581
  // A key line (LONG or CUT) starts with a run of 16+ of these, so a stretch without one holds none.
566
582
  const B64_RUN16_RE = /[A-Za-z0-9+/=]{16}/;
567
583
  // The longest gap the D#168 skip tests; past it the reading runs.
@@ -740,6 +756,49 @@ function cutOffKeyEnd(text, b, be, ctx) {
740
756
  const label = text.slice(b, be);
741
757
  const read = readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, end, ctx.memoOf);
742
758
  const found = read.longs > 0 && keyEvidence(read, label, true) ? read.end : -1;
759
+ // The lines below read again with the shape of `prefix`, whose first character after blanks is
760
+ // `lead`: the reading of the key they hold, or null. `accept`: a further test of that reading.
761
+ const reread = (prefix, lead, accept = null) => {
762
+ const alt = ctx.shapeOf(prefix);
763
+ if (!alt || ctx.endFollows(be)) return null;
764
+ const again = readKeyLines(text, next, u, alt, lead, longs, longest, first, end, ctx.memoOf);
765
+ if (!(again.longs > 0 && again.end > found && keyEvidence(again, label, false))) return null;
766
+ if (accept && !accept(again)) return null;
767
+ ctx.reread = true;
768
+ return again;
769
+ };
770
+ // The lines under a diff context BEGIN (D#614 A1, D#641) read with a diff mark's shape, which takes
771
+ // context lines too: where the key ends, or -1. Only when the lines it takes include a removed or
772
+ // added one: Read output starts its lines with spaces as well, and a reading there took the next
773
+ // line's number for a key's short last line. A removed key that ends with its short last line (or a
774
+ // PGP checksum) is followed by the added key's lines, which the reading stopped at: they are read
775
+ // on, as lines of the same key.
776
+ // What D#641 added (a reading of a BEGIN whose next line has no mark, `admitted` false, and the added
777
+ // key's lines) never spans another private-key BEGIN, which no key in a real diff holds: a header line
778
+ // holding one, swallowed by the span, started no reading of its own on a later pass, where it read on
779
+ // over the first pass's ***PEM_KEY*** line and erased a cut line after it (6 of 400,000 r3mix texts).
780
+ // And what D#641 added reads only inside a unified diff's hunk (ctx.inHunk, from the BEGIN's line
781
+ // start `gl`): an indented cut key in prose read the dash list under it as removed and added key
782
+ // lines, losing it (6.30.10 pre-tag r1 F2).
783
+ const diffRead = (admitted, gl) => {
784
+ // Such a reading ends before the next BEGIN and takes a marked line, so with no marked line before
785
+ // that BEGIN there is none to read (header BEGINs over context lines read the same run again each).
786
+ if (!admitted && !(ctx.inHunk(gl, u) && markedLineIn(text, next, ctx.nextBegin(next), u))) return -1;
787
+ let r = reread(
788
+ '-',
789
+ 45,
790
+ (a) => (admitted || ctx.nextBegin(next) >= a.end) && markedLineIn(text, next, a.end, u),
791
+ );
792
+ if (!r) return -1;
793
+ const alt = ctx.shapeOf('-');
794
+ for (;;) {
795
+ const n = r.shorts > 0 ? nextLine(text, r.end, u).next : -1;
796
+ if (n === -1 || (text[n] !== '-' && text[n] !== '+') || !ctx.inHunk(gl, u)) return r.end;
797
+ const again = readKeyLines(text, n, u, alt, 45, 0, 0, '', -1, ctx.memoOf);
798
+ if (again.longs === 0 || ctx.nextBegin(n) < again.end) return r.end;
799
+ r = again;
800
+ }
801
+ };
743
802
  // D#168: code between the line prefix and the BEGIN (`-KEY = '''`, ` 5\tKEY = """`) or a
744
803
  // string opener before it (`'+`, `"-`) made the whole left context the shape, which no line below
745
804
  // starts with, so nothing past the BEGIN line was read and a cut key was stored. Read again with
@@ -773,60 +832,239 @@ function cutOffKeyEnd(text, b, be, ctx) {
773
832
  if (!B64_RUN16_RE.test(gap) && !gap.includes(':')) return found;
774
833
  }
775
834
  }
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)));
835
+ const gl = lineStartPastQuotes(text, ls, u);
836
+ const head = gl === -1 ? '' : text.slice(gl, Math.min(b, gl + PREFIX_GRAMMAR_SCAN));
837
+ const g = gl === -1 ? null : PREFIX_GRAMMAR_RE.exec(head);
802
838
  const gs = g ? gl + g[0].length - g[1].length : -1;
803
839
  let gp = gl;
804
840
  while (gp < b && (text.charCodeAt(gp) === 32 || text.charCodeAt(gp) === 9)) gp++;
805
841
  // Only when the line is more than its grammar before the BEGIN: an opener before it or code after it.
806
842
  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;
843
+ let lead = gs;
844
+ while (text.charCodeAt(lead) === 32 || text.charCodeAt(lead) === 9) lead++;
845
+ const r = reread(g[1], text.charCodeAt(lead));
846
+ if (r) return r.end;
847
+ }
848
+ // D#614 A2: a grep column after a path the grammar refuses; not the column the grammar read
849
+ // already, nor the first reading's own prefix. That prefix is the column whenever nothing stands
850
+ // between it and the BEGIN, except in escaped text after a quote in the path (`it's/`), where the
851
+ // prefix starts past the quote.
852
+ const p = gl === -1 ? null : GREP_ANY_PATH_RE.exec(head);
853
+ if (p && !(g && g[1] === p[1]) && p[1] !== left.replace(/^[ \t]+/, '')) {
854
+ const r = reread(p[1], text.charCodeAt(gl + p[0].length - p[1].length));
855
+ if (r) return r.end;
856
+ }
857
+ // D#614 A1: a unified diff's context line starts with its space, with code after it or none, and
858
+ // the removed and added lines below it with `-` and `+`, which the line prefix (that space, or the
859
+ // code) never matched.
860
+ if (gl !== -1 && isDiffContextLine(text, gl, b)) {
861
+ const r = diffRead(text[next] === '-' || text[next] === '+', gl);
862
+ if (r !== -1) return r;
863
+ }
864
+ } else if (run < 16) {
865
+ // D#641: in a real diff the lines under a context BEGIN can be context lines too: an OpenSSH key's
866
+ // first body line is the same for every key of its type. The reading above took them for the key
867
+ // and stopped at the first removed line, so the removed and added lines were stored.
868
+ const after = nextLine(text, found, u).next;
869
+ if (after !== -1 && (text[after] === '-' || text[after] === '+')) {
870
+ const gl = lineStartPastQuotes(text, ls, u);
871
+ if (gl !== -1 && isDiffContextLine(text, gl, b)) {
872
+ const r = diffRead(false, gl);
873
+ if (r !== -1) return r;
824
874
  }
825
875
  }
826
876
  }
827
877
  return found;
828
878
  }
829
879
 
880
+ /**
881
+ * Whether the line from `gl` to the BEGIN at `b` is a unified diff's context line: led by its space,
882
+ * with code after it or none. Not when the line holds an escaped break of another depth: it is then
883
+ * several lines, the first one led by a space.
884
+ */
885
+ function isDiffContextLine(text, gl, b) {
886
+ return text.charCodeAt(gl) === 32 && !/\\[nr]/.test(text.slice(gl, b));
887
+ }
888
+
889
+ // A unified diff's hunk header and the counts of old and new lines under it (1 when left out).
890
+ const HUNK_HEADER_RE = /@@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? @@/g;
891
+
892
+ /**
893
+ * Whether a line starts at `at` at break depth u: the text's start, a break (at an escape depth not a
894
+ * real one right after a backslash, which nextEscapedLine steps over), or the quote that opens a string
895
+ * at that depth (`"patch":"@@ …` in JSON, where the line is the string's first).
896
+ */
897
+ function startsLineAt(text, at, u) {
898
+ if (at === 0) return true;
899
+ const c = text[at - 1];
900
+ if (c === '\n' || c === '\r') return u === 0 || text[at - 2] !== '\\';
901
+ if (c === 'n' || c === 'r') return isEscBreak(backslashesBefore(text, at - 1, 0), u);
902
+ return (c === '"' || c === "'") && isStringEnd(backslashesBefore(text, at - 1, 0), u);
903
+ }
904
+
905
+ // A break at an escape depth, found natively: a backslash run then `n` or `r` (its length decides the
906
+ // depth), or a real break not right after a backslash (nextEscapedLine steps over the character after a
907
+ // run). The lookbehinds start a match at a run's first backslash only, so each run is read once.
908
+ const ESC_BREAK_TOKEN_RE = /(?<!\\)\\+[nr]|(?<!\\)[\n\r]/g;
909
+ const BREAK_RE = /[\n\r]/g;
910
+
911
+ /**
912
+ * nextLine from a line start `s`, its break found natively. A hunk's lines are read at the depth asked,
913
+ * and in text at several depths a line runs across the others' text: nextEscapedLine stopped at each
914
+ * backslash in it, 33% of the time of hunks at four depths in turn under coverage.
915
+ */
916
+ function hunkNextLine(text, s, u) {
917
+ const re = u === 0 ? BREAK_RE : ESC_BREAK_TOKEN_RE;
918
+ re.lastIndex = s;
919
+ for (let m = re.exec(text); m; m = re.exec(text)) {
920
+ const r = m[0].length - 1;
921
+ if (r === 0 || isEscBreak(r, u)) return nextLine(text, m.index, u);
922
+ }
923
+ return { end: text.length, next: -1 };
924
+ }
925
+
926
+ /**
927
+ * Whether a line starting at `s` is in a unified diff's hunk at break depth u (D#649), from the headers
928
+ * `heads` ({ at, old, add }, ascending). A hunk is the lines below a header that starts a line, as
929
+ * nextLine reads them: a `-` line spends one of the old count, a `+` line one of the new, and a context
930
+ * line (led by a space, or empty: git's diff.suppressBlankEmpty and GNU diff's --suppress-blank-empty
931
+ * leave it so) one of each while both last; any other line ends it (`` comes
932
+ * after one side's last line, so no context line follows it). Only context lines are asked about (a
933
+ * BEGIN's line led by a space), so once a count is spent nothing after it is in the hunk. A line's
934
+ * question goes to the nearest header before it that starts a line at u; a header's text inside a line
935
+ * (the diff of a file that holds one) starts no hunk. A hunk is read from its header only as far as the
936
+ * line asked about, and on from there for a later one, so no line is read twice: reading each hunk
937
+ * whole read the last line of every hunk at one depth across the text at the others, 14.7x benign under
938
+ * coverage (hunks at four depths in turn, 3.9x on v6.30.10).
939
+ */
940
+ function hunkReader(text, heads, u) {
941
+ const own = heads.filter((h) => startsLineAt(text, h.at, u));
942
+ const read = new Map();
943
+ return (s) => {
944
+ let lo = 0;
945
+ let hi = own.length;
946
+ while (lo < hi) {
947
+ const mid = (lo + hi) >> 1;
948
+ if (own[mid].at < s) lo = mid + 1;
949
+ else hi = mid;
950
+ }
951
+ const k = lo - 1;
952
+ if (k < 0) return false;
953
+ let h = read.get(k);
954
+ if (h === undefined) {
955
+ const { at, old, add } = own[k];
956
+ read.set(k, (h = { next: hunkNextLine(text, at, u).next, old, add, starts: new Set() }));
957
+ }
958
+ // Never past the next header: it is at or after s (a later line's question goes to its hunk).
959
+ while (h.next !== -1 && h.next <= s) {
960
+ const at = h.next;
961
+ const line = hunkNextLine(text, at, u);
962
+ const c = line.end === at ? 32 : text.charCodeAt(at);
963
+ if (c === 32 && h.old > 0 && h.add > 0) {
964
+ h.old--;
965
+ h.add--;
966
+ } else if (c === 45) h.old--;
967
+ else if (c === 43) h.add--;
968
+ else {
969
+ h.next = -1;
970
+ break;
971
+ }
972
+ h.starts.add(at);
973
+ h.next = line.next;
974
+ }
975
+ return h.starts.has(s);
976
+ };
977
+ }
978
+
979
+ // A diff mark after a character that can end a line: a break, and at an escape depth an escaped
980
+ // break's letter or (one escape deep) the quote that reopens a string concatenated across lines.
981
+ const MARK_AFTER_BREAK_RE = /[\n\r][-+]/g;
982
+ const MARK_AFTER_ESC_RE = /[\n\r"'nr][-+]/g;
983
+
984
+ /**
985
+ * Whether a line that starts in [from, to) at break depth u (`from` a line start) starts with a diff's
986
+ * `-` or `+`. The marks are found natively and each is checked to start a line as nextLine reads them:
987
+ * reading the lines one by one went through every line of a run under a BEGIN in Read output (each led
988
+ * by a space) on each of the 32 passes a chain of labelled secrets takes, 42-57% of two linearity
989
+ * shapes under coverage.
990
+ */
991
+ function markedLineIn(text, from, to, u) {
992
+ if (from === -1 || from >= to) return false;
993
+ const c0 = text.charCodeAt(from);
994
+ if (c0 === 45 || c0 === 43) return true;
995
+ const re = u === 0 ? MARK_AFTER_BREAK_RE : MARK_AFTER_ESC_RE;
996
+ const seg = to === Infinity ? text.slice(from) : text.slice(from, to);
997
+ re.lastIndex = 0;
998
+ for (let m = re.exec(seg); m; m = re.exec(seg)) {
999
+ re.lastIndex = m.index + 1;
1000
+ const c = from + m.index;
1001
+ const ch = text.charCodeAt(c);
1002
+ // At an escape depth a real break right after a backslash run is that run's next character, which
1003
+ // nextEscapedLine steps over.
1004
+ if (ch === 10 || ch === 13) {
1005
+ if (u === 0 || text.charCodeAt(c - 1) !== 92) return true;
1006
+ } else if (ch === 110 || ch === 114) {
1007
+ if (isEscBreak(backslashesBefore(text, c, from), u)) return true;
1008
+ } else if (u === 1) {
1009
+ // The quote that opens the string after `\n" +` and a break: a line start when an escaped break
1010
+ // at depth 1 starts the concatenation (nextEscapedLine's CONCAT_AFTER_RE).
1011
+ if (opensConcat(text, c, from)) return true;
1012
+ }
1013
+ }
1014
+ return false;
1015
+ }
1016
+
1017
+ /**
1018
+ * Whether the quote at `c` opens the string after a depth-1 escaped break, a quote, an optional `+` and a
1019
+ * break, so that a line starts after it: CONCAT_AFTER_RE (nextEscapedLine) read backward, within `from`.
1020
+ */
1021
+ function opensConcat(text, c, from) {
1022
+ const blanks = (j) => {
1023
+ while (j >= from && (text[j] === ' ' || text[j] === '\t')) j--;
1024
+ return j;
1025
+ };
1026
+ let j = blanks(c - 1);
1027
+ if (j < from) return false;
1028
+ if (text[j] === '\n') j -= j - 1 >= from && text[j - 1] === '\r' ? 2 : 1;
1029
+ else if (text[j] === '\r') j--;
1030
+ else return false;
1031
+ j = blanks(j);
1032
+ if (j >= from && text[j] === '+') j = blanks(j - 1);
1033
+ if (j < from || (text[j] !== '"' && text[j] !== "'")) return false;
1034
+ j--;
1035
+ return j >= from && (text[j] === 'n' || text[j] === 'r') && isEscBreak(backslashesBefore(text, j, from), 1);
1036
+ }
1037
+
1038
+ /**
1039
+ * Where the line that holds the BEGIN starts, from `ls`, the start cutOffKeyEnd's prefix walk found:
1040
+ * that, unless the walk stopped at a single quote in escaped text, or -1. The walk stops at an
1041
+ * unescaped single quote, which JSON never escapes (`-KEY = '''` in a JSON string leaves ''), so look
1042
+ * past single quotes for the start of the line: a break, or a double quote that ends a string at this
1043
+ * depth (the string the line is in), or the start of the text. At most LINE_START_SCAN characters
1044
+ * back; past that, -1.
1045
+ */
1046
+ function lineStartPastQuotes(text, ls, u) {
1047
+ if (!(u > 0 && ls > 0 && text[ls - 1] === "'")) return ls;
1048
+ // Native between candidates: a character by character walk was most of the time of a shape that
1049
+ // repeats one 300-character line (pemEscapedLongCodeBeforeBegin), and now goes the whole line.
1050
+ const lim = Math.max(0, ls - LINE_START_SCAN);
1051
+ for (let j = ls - 1; ;) {
1052
+ LINE_BACK_RE.lastIndex = j;
1053
+ const k = j - LINE_BACK_RE.exec(text)[1].length;
1054
+ if (k === 0) return lim === 0 ? 0 : -1;
1055
+ // text[k - 1] is a break, a double quote or a backslash. A backslash run then `n` or `r` at k is
1056
+ // an escaped break at this depth when the run fits it; the line starts after that letter.
1057
+ const c = text.charCodeAt(k - 1);
1058
+ if (c === 92) {
1059
+ const e = text.charCodeAt(k);
1060
+ if (k + 1 < lim) return -1;
1061
+ if ((e === 110 || e === 114) && isEscBreak(backslashesBefore(text, k, 0), u)) return k + 1;
1062
+ } else if (k < lim) return -1;
1063
+ else if (c === 10 || c === 13 || isStringEnd(backslashesBefore(text, k - 1, 0), u)) return k;
1064
+ j = k - 1;
1065
+ }
1066
+ }
1067
+
830
1068
  /**
831
1069
  * Whether a reading is a key: one base64 line of 40+ characters or a start the way a key encoding
832
1070
  * starts; when `loose`, also armor headers or two lines of 16+. The grammar reading (D#168) is not
@@ -946,7 +1184,7 @@ function readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, en
946
1184
  memo ??= memoOf(shape);
947
1185
  const r = memo.long.get(longKey);
948
1186
  if (r !== undefined)
949
- return { longs: r.longs, longest: r.longest, first: r.first, end: r.end, armors };
1187
+ return { longs: r.longs, longest: r.longest, first: r.first, end: r.end, armors, shorts: r.shorts };
950
1188
  }
951
1189
  if (longs++ === 0) first = core;
952
1190
  longest = Math.max(longest, core.length);
@@ -973,14 +1211,15 @@ function readKeyLines(text, next, u, shape, shapeLead, longs, longest, first, en
973
1211
  break;
974
1212
  }
975
1213
  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 };
1214
+ if (longKey !== null) memo.long.set(longKey, { longs, longest, first, end, shorts });
1215
+ return { longs, longest, first, end, armors, shorts };
978
1216
  }
979
1217
 
980
1218
  // Separate scanners: KEY_BEGIN_RE's lastIndex drives scrubCutOffKeys' loop while these run.
981
1219
  const KEY_BEGIN_SCAN_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
982
1220
  const KEY_END_SCAN_RE = new RegExp(KEY_END_RE.source, 'g');
983
1221
  const KEY_BEGIN_NEXT_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
1222
+ const KEY_BEGIN_INNER_RE = new RegExp(KEY_BEGIN_RE.source, 'g');
984
1223
 
985
1224
  /** Start offsets of every match of the global regex `re` in `text`. */
986
1225
  function matchStarts(re, text) {
@@ -1051,14 +1290,45 @@ function scrubCutOffKeys(text) {
1051
1290
  if (m === undefined) memos.set(id, (m = { blank: new Map(), long: new Map() }));
1052
1291
  return m;
1053
1292
  };
1054
- const ctx = { endFollows, nextBegin, shapeOf, memoOf };
1293
+ // Whether the line starting at `s` (depth u) is in a unified diff's hunk (hunkReader): the headers
1294
+ // found once per call, on first use, and one reader per depth.
1295
+ let heads = null;
1296
+ const hunks = new Map();
1297
+ const inHunk = (s, u) => {
1298
+ if (heads === null) {
1299
+ heads = [];
1300
+ HUNK_HEADER_RE.lastIndex = 0;
1301
+ for (let h; (h = HUNK_HEADER_RE.exec(text));) {
1302
+ heads.push({ at: h.index, old: h[1] === undefined ? 1 : +h[1], add: h[2] === undefined ? 1 : +h[2] });
1303
+ }
1304
+ }
1305
+ let has = hunks.get(u);
1306
+ if (has === undefined) hunks.set(u, (has = hunkReader(text, heads, u)));
1307
+ return has(s);
1308
+ };
1309
+ const ctx = { endFollows, nextBegin, shapeOf, memoOf, inHunk, reread: false };
1055
1310
  KEY_BEGIN_RE.lastIndex = 0;
1056
1311
  let out = '';
1057
1312
  let last = 0;
1058
1313
  let m;
1059
1314
  while ((m = KEY_BEGIN_RE.exec(text))) {
1060
- const end = cutOffKeyEnd(text, m.index, m.index + m[0].length, ctx);
1315
+ ctx.reread = false;
1316
+ let end = cutOffKeyEnd(text, m.index, m.index + m[0].length, ctx);
1061
1317
  if (end === -1) continue;
1318
+ // D#614 A4 (D#633): a span read again with a named grammar's shape can take a header line that
1319
+ // holds another private-key BEGIN for an armor header and end where that BEGIN's own reading goes
1320
+ // on (a 15-character line, a line that is only its prefix, another header). The span covered the
1321
+ // BEGIN, which then started no reading, so the lines past the span were stored; 6.30.8, whose
1322
+ // first reading found nothing there, read the inner BEGIN and erased them. Each private-key BEGIN
1323
+ // inside such a span is read too, and the span ends where the farthest of them ends. Only for these
1324
+ // readings: under a BEGIN with nothing before it the same holds in 6.30.8 (a separate defect).
1325
+ if (ctx.reread) {
1326
+ KEY_BEGIN_INNER_RE.lastIndex = m.index + m[0].length;
1327
+ for (let n; (n = KEY_BEGIN_INNER_RE.exec(text)) && n.index < end;) {
1328
+ const e = cutOffKeyEnd(text, n.index, n.index + n[0].length, ctx);
1329
+ if (e > end) end = e;
1330
+ }
1331
+ }
1062
1332
  out += text.slice(last, m.index) + PEM_MARK;
1063
1333
  last = end;
1064
1334
  KEY_BEGIN_RE.lastIndex = end;