@nxuss/lemma 1.9.1 → 1.11.0

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.
Files changed (122) hide show
  1. package/README.md +24 -4
  2. package/dist/cjs/cli/lemma-proxy.d.ts.map +1 -1
  3. package/dist/cjs/cli/lemma-proxy.js +7 -2
  4. package/dist/cjs/cli/lemma-proxy.js.map +1 -1
  5. package/dist/cjs/mcp/tool-profiles.d.ts.map +1 -1
  6. package/dist/cjs/mcp/tool-profiles.js +43 -0
  7. package/dist/cjs/mcp/tool-profiles.js.map +1 -1
  8. package/dist/cjs/mcp/tools.d.ts.map +1 -1
  9. package/dist/cjs/mcp/tools.js +314 -146
  10. package/dist/cjs/mcp/tools.js.map +1 -1
  11. package/dist/cjs/subconscious/TheBrainV2.d.ts +27 -1
  12. package/dist/cjs/subconscious/TheBrainV2.d.ts.map +1 -1
  13. package/dist/cjs/subconscious/TheBrainV2.js +53 -1
  14. package/dist/cjs/subconscious/TheBrainV2.js.map +1 -1
  15. package/dist/cjs/utils/AffectedTests.d.ts +54 -0
  16. package/dist/cjs/utils/AffectedTests.d.ts.map +1 -0
  17. package/dist/cjs/utils/AffectedTests.js +195 -0
  18. package/dist/cjs/utils/AffectedTests.js.map +1 -0
  19. package/dist/cjs/utils/AstSymbolCache.d.ts +29 -0
  20. package/dist/cjs/utils/AstSymbolCache.d.ts.map +1 -0
  21. package/dist/cjs/utils/AstSymbolCache.js +66 -0
  22. package/dist/cjs/utils/AstSymbolCache.js.map +1 -0
  23. package/dist/cjs/utils/ContextSqueezer.d.ts +24 -4
  24. package/dist/cjs/utils/ContextSqueezer.d.ts.map +1 -1
  25. package/dist/cjs/utils/ContextSqueezer.js +32 -3
  26. package/dist/cjs/utils/ContextSqueezer.js.map +1 -1
  27. package/dist/cjs/utils/DepGraph.d.ts +28 -0
  28. package/dist/cjs/utils/DepGraph.d.ts.map +1 -0
  29. package/dist/cjs/utils/DepGraph.js +210 -0
  30. package/dist/cjs/utils/DepGraph.js.map +1 -0
  31. package/dist/cjs/utils/LocalPrefilter.d.ts +61 -0
  32. package/dist/cjs/utils/LocalPrefilter.d.ts.map +1 -0
  33. package/dist/cjs/utils/LocalPrefilter.js +169 -0
  34. package/dist/cjs/utils/LocalPrefilter.js.map +1 -0
  35. package/dist/cjs/utils/OutputDelta.d.ts +60 -0
  36. package/dist/cjs/utils/OutputDelta.d.ts.map +1 -0
  37. package/dist/cjs/utils/OutputDelta.js +226 -0
  38. package/dist/cjs/utils/OutputDelta.js.map +1 -0
  39. package/dist/cjs/utils/OutputLedger.d.ts +60 -0
  40. package/dist/cjs/utils/OutputLedger.d.ts.map +1 -0
  41. package/dist/cjs/utils/OutputLedger.js +134 -0
  42. package/dist/cjs/utils/OutputLedger.js.map +1 -0
  43. package/dist/cjs/utils/ReadWorkspaceCache.d.ts +8 -2
  44. package/dist/cjs/utils/ReadWorkspaceCache.d.ts.map +1 -1
  45. package/dist/cjs/utils/ReadWorkspaceCache.js +82 -4
  46. package/dist/cjs/utils/ReadWorkspaceCache.js.map +1 -1
  47. package/dist/cjs/utils/RoiLedger.d.ts +31 -0
  48. package/dist/cjs/utils/RoiLedger.d.ts.map +1 -0
  49. package/dist/cjs/utils/RoiLedger.js +113 -0
  50. package/dist/cjs/utils/RoiLedger.js.map +1 -0
  51. package/dist/cjs/utils/SharedLedgerStore.d.ts +26 -0
  52. package/dist/cjs/utils/SharedLedgerStore.d.ts.map +1 -0
  53. package/dist/cjs/utils/SharedLedgerStore.js +130 -0
  54. package/dist/cjs/utils/SharedLedgerStore.js.map +1 -0
  55. package/dist/cjs/utils/SpeculativeWarmer.d.ts +24 -0
  56. package/dist/cjs/utils/SpeculativeWarmer.d.ts.map +1 -0
  57. package/dist/cjs/utils/SpeculativeWarmer.js +73 -0
  58. package/dist/cjs/utils/SpeculativeWarmer.js.map +1 -0
  59. package/dist/cjs/utils/reportSavings.d.ts.map +1 -1
  60. package/dist/cjs/utils/reportSavings.js +11 -0
  61. package/dist/cjs/utils/reportSavings.js.map +1 -1
  62. package/dist/esm/cli/lemma-proxy.d.ts.map +1 -1
  63. package/dist/esm/cli/lemma-proxy.js +7 -2
  64. package/dist/esm/cli/lemma-proxy.js.map +1 -1
  65. package/dist/esm/mcp/tool-profiles.d.ts.map +1 -1
  66. package/dist/esm/mcp/tool-profiles.js +43 -0
  67. package/dist/esm/mcp/tool-profiles.js.map +1 -1
  68. package/dist/esm/mcp/tools.d.ts.map +1 -1
  69. package/dist/esm/mcp/tools.js +312 -144
  70. package/dist/esm/mcp/tools.js.map +1 -1
  71. package/dist/esm/subconscious/TheBrainV2.d.ts +27 -1
  72. package/dist/esm/subconscious/TheBrainV2.d.ts.map +1 -1
  73. package/dist/esm/subconscious/TheBrainV2.js +53 -1
  74. package/dist/esm/subconscious/TheBrainV2.js.map +1 -1
  75. package/dist/esm/utils/AffectedTests.d.ts +54 -0
  76. package/dist/esm/utils/AffectedTests.d.ts.map +1 -0
  77. package/dist/esm/utils/AffectedTests.js +186 -0
  78. package/dist/esm/utils/AffectedTests.js.map +1 -0
  79. package/dist/esm/utils/AstSymbolCache.d.ts +29 -0
  80. package/dist/esm/utils/AstSymbolCache.d.ts.map +1 -0
  81. package/dist/esm/utils/AstSymbolCache.js +58 -0
  82. package/dist/esm/utils/AstSymbolCache.js.map +1 -0
  83. package/dist/esm/utils/ContextSqueezer.d.ts +24 -4
  84. package/dist/esm/utils/ContextSqueezer.d.ts.map +1 -1
  85. package/dist/esm/utils/ContextSqueezer.js +32 -3
  86. package/dist/esm/utils/ContextSqueezer.js.map +1 -1
  87. package/dist/esm/utils/DepGraph.d.ts +28 -0
  88. package/dist/esm/utils/DepGraph.d.ts.map +1 -0
  89. package/dist/esm/utils/DepGraph.js +169 -0
  90. package/dist/esm/utils/DepGraph.js.map +1 -0
  91. package/dist/esm/utils/LocalPrefilter.d.ts +61 -0
  92. package/dist/esm/utils/LocalPrefilter.d.ts.map +1 -0
  93. package/dist/esm/utils/LocalPrefilter.js +159 -0
  94. package/dist/esm/utils/LocalPrefilter.js.map +1 -0
  95. package/dist/esm/utils/OutputDelta.d.ts +60 -0
  96. package/dist/esm/utils/OutputDelta.d.ts.map +1 -0
  97. package/dist/esm/utils/OutputDelta.js +221 -0
  98. package/dist/esm/utils/OutputDelta.js.map +1 -0
  99. package/dist/esm/utils/OutputLedger.d.ts +60 -0
  100. package/dist/esm/utils/OutputLedger.d.ts.map +1 -0
  101. package/dist/esm/utils/OutputLedger.js +125 -0
  102. package/dist/esm/utils/OutputLedger.js.map +1 -0
  103. package/dist/esm/utils/ReadWorkspaceCache.d.ts +8 -2
  104. package/dist/esm/utils/ReadWorkspaceCache.d.ts.map +1 -1
  105. package/dist/esm/utils/ReadWorkspaceCache.js +81 -4
  106. package/dist/esm/utils/ReadWorkspaceCache.js.map +1 -1
  107. package/dist/esm/utils/RoiLedger.d.ts +31 -0
  108. package/dist/esm/utils/RoiLedger.d.ts.map +1 -0
  109. package/dist/esm/utils/RoiLedger.js +104 -0
  110. package/dist/esm/utils/RoiLedger.js.map +1 -0
  111. package/dist/esm/utils/SharedLedgerStore.d.ts +26 -0
  112. package/dist/esm/utils/SharedLedgerStore.d.ts.map +1 -0
  113. package/dist/esm/utils/SharedLedgerStore.js +122 -0
  114. package/dist/esm/utils/SharedLedgerStore.js.map +1 -0
  115. package/dist/esm/utils/SpeculativeWarmer.d.ts +24 -0
  116. package/dist/esm/utils/SpeculativeWarmer.d.ts.map +1 -0
  117. package/dist/esm/utils/SpeculativeWarmer.js +66 -0
  118. package/dist/esm/utils/SpeculativeWarmer.js.map +1 -0
  119. package/dist/esm/utils/reportSavings.d.ts.map +1 -1
  120. package/dist/esm/utils/reportSavings.js +11 -0
  121. package/dist/esm/utils/reportSavings.js.map +1 -1
  122. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalPrefilter.js","sourceRoot":"","sources":["../../../src/utils/LocalPrefilter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;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C,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,mBAAmB;IACjC,YAAY,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACtD,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * OutputDelta — re-send only what changed, never the whole thing again.
3
+ *
4
+ * The problem: a debug loop reads the same file (or runs the same command) six times.
5
+ * Between two of those calls maybe three lines moved, but the model pays for the entire
6
+ * file or the entire 400-line `tsc` dump each time — and because tool results stay in the
7
+ * transcript, it keeps paying for every copy on every subsequent turn.
8
+ *
9
+ * `ReadWorkspaceCache` already suppresses the *identical* case. This module covers the far
10
+ * more common one: it changed, but barely.
11
+ *
12
+ * Two rules this module is built around:
13
+ *
14
+ * 1. The diff is computed against the exact text the model received. Not the file on
15
+ * disk, not the pre-squeeze source — the final emitted string. A diff against
16
+ * anything else describes a document the model never had, which is worse than
17
+ * re-sending the whole file.
18
+ * 2. A delta is only emitted when it is decisively smaller than the full text. Below
19
+ * that margin, reconstructing from a diff is cognitive work with no token payoff, so
20
+ * the full text wins. See `shouldEmitDelta`.
21
+ */
22
+ export interface Hunk {
23
+ /** 1-indexed start line in the previous text. */
24
+ oldStart: number;
25
+ oldLines: number;
26
+ /** 1-indexed start line in the new text. */
27
+ newStart: number;
28
+ newLines: number;
29
+ /** Rendered lines, each prefixed with ' ', '-' or '+'. */
30
+ lines: string[];
31
+ }
32
+ export interface DiffResult {
33
+ hunks: Hunk[];
34
+ added: number;
35
+ removed: number;
36
+ /** True when the two texts are byte-identical. */
37
+ identical: boolean;
38
+ }
39
+ /** Line-level diff between the text the model already has and the text we would send now. */
40
+ export declare function computeDiff(previous: string, next: string): DiffResult | null;
41
+ /** Render hunks as a unified diff body (no ---/+++ file headers; the caller supplies context). */
42
+ export declare function renderDiff(diff: DiffResult): string;
43
+ export interface DeltaDecision {
44
+ emit: boolean;
45
+ /** The rendered diff body, present only when `emit` is true. */
46
+ body?: string;
47
+ added: number;
48
+ removed: number;
49
+ hunkCount: number;
50
+ /** Why a delta was declined — surfaced in logs and receipts, never guessed at. */
51
+ reason?: string;
52
+ }
53
+ /**
54
+ * Decide whether to hand the model a delta instead of the full text.
55
+ *
56
+ * Declining is the safe direction: the caller falls back to sending everything, which is
57
+ * always correct. So every uncertain case here resolves to `emit: false`.
58
+ */
59
+ export declare function shouldEmitDelta(previous: string, next: string): DeltaDecision;
60
+ //# sourceMappingURL=OutputDelta.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OutputDelta.d.ts","sourceRoot":"","sources":["../../../src/utils/OutputDelta.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AA2BH,MAAM,WAAW,IAAI;IACnB,iDAAiD;IACjD,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,0DAA0D;IAC1D,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,kDAAkD;IAClD,SAAS,EAAE,OAAO,CAAC;CACpB;AA4HD,6FAA6F;AAC7F,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAY7E;AAED,kGAAkG;AAClG,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAInD;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,gEAAgE;IAChE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,kFAAkF;IAClF,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,aAAa,CA+B7E"}
@@ -0,0 +1,221 @@
1
+ /**
2
+ * OutputDelta — re-send only what changed, never the whole thing again.
3
+ *
4
+ * The problem: a debug loop reads the same file (or runs the same command) six times.
5
+ * Between two of those calls maybe three lines moved, but the model pays for the entire
6
+ * file or the entire 400-line `tsc` dump each time — and because tool results stay in the
7
+ * transcript, it keeps paying for every copy on every subsequent turn.
8
+ *
9
+ * `ReadWorkspaceCache` already suppresses the *identical* case. This module covers the far
10
+ * more common one: it changed, but barely.
11
+ *
12
+ * Two rules this module is built around:
13
+ *
14
+ * 1. The diff is computed against the exact text the model received. Not the file on
15
+ * disk, not the pre-squeeze source — the final emitted string. A diff against
16
+ * anything else describes a document the model never had, which is worse than
17
+ * re-sending the whole file.
18
+ * 2. A delta is only emitted when it is decisively smaller than the full text. Below
19
+ * that margin, reconstructing from a diff is cognitive work with no token payoff, so
20
+ * the full text wins. See `shouldEmitDelta`.
21
+ */
22
+ /**
23
+ * Lines of unchanged context kept around each changed hunk. Three is the unified-diff
24
+ * convention and is enough to locate a hunk unambiguously in almost all real files.
25
+ */
26
+ const CONTEXT_LINES = 3;
27
+ /**
28
+ * A delta must be at most this fraction of the full text to be worth emitting. At 0.5 a
29
+ * diff that saves only a third of the tokens is rejected: the model still has to
30
+ * mentally apply it, and a near-full-size diff is strictly worse than the real text.
31
+ */
32
+ const DELTA_MAX_RATIO = 0.5;
33
+ /**
34
+ * Below this size the full text is cheap enough that a diff never pays for its own
35
+ * explanation header, let alone the risk of misapplication.
36
+ */
37
+ const DELTA_MIN_CHARS = 1200;
38
+ /**
39
+ * Diffing is O(n*m) in the worst case. Past this line count the DP table costs more CPU
40
+ * (and latency the user waits on) than the tokens it could save, so we decline.
41
+ */
42
+ const MAX_DIFF_LINES = 4000;
43
+ /**
44
+ * Longest-common-subsequence over lines.
45
+ *
46
+ * Real edits touch a handful of lines in a file whose head and tail are untouched, so we
47
+ * strip the common prefix and suffix first. That turns the usual case into a DP over a
48
+ * few dozen lines instead of a few thousand, which is what keeps this affordable enough
49
+ * to run on every read.
50
+ */
51
+ function diffLines(oldLines, newLines) {
52
+ let start = 0;
53
+ while (start < oldLines.length && start < newLines.length && oldLines[start] === newLines[start]) {
54
+ start++;
55
+ }
56
+ let endOld = oldLines.length;
57
+ let endNew = newLines.length;
58
+ while (endOld > start && endNew > start && oldLines[endOld - 1] === newLines[endNew - 1]) {
59
+ endOld--;
60
+ endNew--;
61
+ }
62
+ const a = oldLines.slice(start, endOld);
63
+ const b = newLines.slice(start, endNew);
64
+ const ops = [];
65
+ for (let i = 0; i < start; i++)
66
+ ops.push({ type: "equal", line: oldLines[i] });
67
+ // DP table of LCS lengths. Sized on the trimmed middle, which is why the trimming above
68
+ // matters: without it this allocates (n+1)*(m+1) numbers for the whole file.
69
+ const n = a.length;
70
+ const m = b.length;
71
+ const table = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
72
+ for (let i = n - 1; i >= 0; i--) {
73
+ for (let j = m - 1; j >= 0; j--) {
74
+ table[i][j] = a[i] === b[j] ? table[i + 1][j + 1] + 1 : Math.max(table[i + 1][j], table[i][j + 1]);
75
+ }
76
+ }
77
+ let i = 0;
78
+ let j = 0;
79
+ while (i < n && j < m) {
80
+ if (a[i] === b[j]) {
81
+ ops.push({ type: "equal", line: a[i] });
82
+ i++;
83
+ j++;
84
+ }
85
+ else if (table[i + 1][j] >= table[i][j + 1]) {
86
+ ops.push({ type: "del", line: a[i] });
87
+ i++;
88
+ }
89
+ else {
90
+ ops.push({ type: "ins", line: b[j] });
91
+ j++;
92
+ }
93
+ }
94
+ while (i < n)
95
+ ops.push({ type: "del", line: a[i++] });
96
+ while (j < m)
97
+ ops.push({ type: "ins", line: b[j++] });
98
+ for (let k = endOld; k < oldLines.length; k++)
99
+ ops.push({ type: "equal", line: oldLines[k] });
100
+ return ops;
101
+ }
102
+ /** Group an op stream into unified-diff hunks with `CONTEXT_LINES` of surrounding context. */
103
+ function buildHunks(ops) {
104
+ const changedAt = new Set();
105
+ ops.forEach((op, idx) => {
106
+ if (op.type !== "equal")
107
+ changedAt.add(idx);
108
+ });
109
+ if (changedAt.size === 0)
110
+ return [];
111
+ // Expand each change into a window, then merge windows that touch or overlap so two
112
+ // edits four lines apart render as one hunk rather than two with duplicated context.
113
+ const windows = [];
114
+ for (const idx of Array.from(changedAt).sort((x, y) => x - y)) {
115
+ const lo = Math.max(0, idx - CONTEXT_LINES);
116
+ const hi = Math.min(ops.length - 1, idx + CONTEXT_LINES);
117
+ const last = windows[windows.length - 1];
118
+ if (last && lo <= last[1] + 1)
119
+ last[1] = Math.max(last[1], hi);
120
+ else
121
+ windows.push([lo, hi]);
122
+ }
123
+ // Line counters advance as we scan, so each hunk header reports true positions in both
124
+ // documents rather than positions relative to the hunk before it.
125
+ const hunks = [];
126
+ let oldLine = 1;
127
+ let newLine = 1;
128
+ let cursor = 0;
129
+ for (const [lo, hi] of windows) {
130
+ while (cursor < lo) {
131
+ const op = ops[cursor];
132
+ if (op.type !== "ins")
133
+ oldLine++;
134
+ if (op.type !== "del")
135
+ newLine++;
136
+ cursor++;
137
+ }
138
+ const hunk = { oldStart: oldLine, oldLines: 0, newStart: newLine, newLines: 0, lines: [] };
139
+ for (let idx = lo; idx <= hi; idx++) {
140
+ const op = ops[idx];
141
+ if (op.type === "equal") {
142
+ hunk.lines.push(` ${op.line}`);
143
+ hunk.oldLines++;
144
+ hunk.newLines++;
145
+ oldLine++;
146
+ newLine++;
147
+ }
148
+ else if (op.type === "del") {
149
+ hunk.lines.push(`-${op.line}`);
150
+ hunk.oldLines++;
151
+ oldLine++;
152
+ }
153
+ else {
154
+ hunk.lines.push(`+${op.line}`);
155
+ hunk.newLines++;
156
+ newLine++;
157
+ }
158
+ }
159
+ hunks.push(hunk);
160
+ cursor = hi + 1;
161
+ }
162
+ return hunks;
163
+ }
164
+ /** Line-level diff between the text the model already has and the text we would send now. */
165
+ export function computeDiff(previous, next) {
166
+ if (previous === next)
167
+ return { hunks: [], added: 0, removed: 0, identical: true };
168
+ const oldLines = previous.split("\n");
169
+ const newLines = next.split("\n");
170
+ if (oldLines.length > MAX_DIFF_LINES || newLines.length > MAX_DIFF_LINES)
171
+ return null;
172
+ const ops = diffLines(oldLines, newLines);
173
+ const hunks = buildHunks(ops);
174
+ const added = ops.filter((o) => o.type === "ins").length;
175
+ const removed = ops.filter((o) => o.type === "del").length;
176
+ return { hunks, added, removed, identical: false };
177
+ }
178
+ /** Render hunks as a unified diff body (no ---/+++ file headers; the caller supplies context). */
179
+ export function renderDiff(diff) {
180
+ return diff.hunks
181
+ .map((h) => [`@@ -${h.oldStart},${h.oldLines} +${h.newStart},${h.newLines} @@`, ...h.lines].join("\n"))
182
+ .join("\n");
183
+ }
184
+ /**
185
+ * Decide whether to hand the model a delta instead of the full text.
186
+ *
187
+ * Declining is the safe direction: the caller falls back to sending everything, which is
188
+ * always correct. So every uncertain case here resolves to `emit: false`.
189
+ */
190
+ export function shouldEmitDelta(previous, next) {
191
+ const empty = { emit: false, added: 0, removed: 0, hunkCount: 0 };
192
+ if (next.length < DELTA_MIN_CHARS) {
193
+ return { ...empty, reason: "full text is small enough that a diff saves nothing" };
194
+ }
195
+ const diff = computeDiff(previous, next);
196
+ if (!diff)
197
+ return { ...empty, reason: "too large to diff affordably" };
198
+ if (diff.identical)
199
+ return { ...empty, reason: "identical" };
200
+ if (diff.hunks.length === 0)
201
+ return { ...empty, reason: "no hunks" };
202
+ const body = renderDiff(diff);
203
+ const ratio = body.length / next.length;
204
+ if (ratio > DELTA_MAX_RATIO) {
205
+ return {
206
+ ...empty,
207
+ added: diff.added,
208
+ removed: diff.removed,
209
+ hunkCount: diff.hunks.length,
210
+ reason: `diff is ${Math.round(ratio * 100)}% of the full text — not worth reconstructing`,
211
+ };
212
+ }
213
+ return {
214
+ emit: true,
215
+ body,
216
+ added: diff.added,
217
+ removed: diff.removed,
218
+ hunkCount: diff.hunks.length,
219
+ };
220
+ }
221
+ //# sourceMappingURL=OutputDelta.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OutputDelta.js","sourceRoot":"","sources":["../../../src/utils/OutputDelta.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;GAGG;AACH,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB;;;;GAIG;AACH,MAAM,eAAe,GAAG,GAAG,CAAC;AAE5B;;;GAGG;AACH,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;GAGG;AACH,MAAM,cAAc,GAAG,IAAI,CAAC;AAuB5B;;;;;;;GAOG;AACH,SAAS,SAAS,CAAC,QAAkB,EAAE,QAAkB;IACvD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,OAAO,KAAK,GAAG,QAAQ,CAAC,MAAM,IAAI,KAAK,GAAG,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,KAAK,CAAC,KAAK,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACjG,KAAK,EAAE,CAAC;IACV,CAAC;IACD,IAAI,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;IAC7B,IAAI,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;IAC7B,OAAO,MAAM,GAAG,KAAK,IAAI,MAAM,GAAG,KAAK,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC;QACzF,MAAM,EAAE,CAAC;QACT,MAAM,EAAE,CAAC;IACX,CAAC;IAED,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACxC,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,KA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AAC,CAAC;IAC9B,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IACxC,IAAI,KAAK,GAAG,eAAe,EAAE,CAAC;QAC5B,OAAO;YACL,GAAG,KAAK;YACR,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM;YAC5B,MAAM,EAAE,WAAW,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,+CAA+C;SAC1F,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,IAAI;QACV,IAAI;QACJ,KAAK,EAAE,IAAI,CAAC,KAAK;QACjB,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM;KAC7B,CAAC;AACJ,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * OutputLedger — session memory of what each tool already returned.
3
+ *
4
+ * `ReadWorkspaceCache` solves this for file reads, but reads are not where the weight is.
5
+ * A debug loop runs `npm test` six times and `tsc --noEmit` four; each run dumps hundreds
6
+ * of near-identical lines into the transcript, and every one of those copies is re-sent on
7
+ * every later turn. By the end of a session the same failing stack trace can account for
8
+ * more context than the source files being debugged.
9
+ *
10
+ * So: key on (tool, call signature), remember the exact text emitted, and on a repeat
11
+ * answer with `IDENTICAL` or a diff instead of the payload.
12
+ *
13
+ * The correctness rule is the same one that governs the read cache, and it is absolute:
14
+ * a ledger hit may only suppress text the model demonstrably already received in this
15
+ * session. Anything else — a cold start, an evicted entry, a caller that didn't record —
16
+ * resolves to sending the full output. Under-suppressing costs tokens; over-suppressing
17
+ * costs the user a correct answer.
18
+ */
19
+ /**
20
+ * Build a stable key from the arguments that determine the output.
21
+ *
22
+ * Only pass arguments that actually change the result. Including an incidental field
23
+ * (a verbosity flag that doesn't alter the payload, a timestamp) makes every call a
24
+ * fresh key and silently disables the ledger; including too few makes two genuinely
25
+ * different calls collide, which would suppress output the model never saw. When unsure,
26
+ * include the field — a missed saving is recoverable, a wrong suppression is not.
27
+ */
28
+ export declare function ledgerKey(toolName: string, signature: Record<string, unknown>): string;
29
+ export type LedgerStatus = "new" | "identical" | "changed";
30
+ export interface LedgerResult {
31
+ status: LedgerStatus;
32
+ /** Replacement text to return instead of the payload; null means "send the full output". */
33
+ replacement: string | null;
34
+ /** Characters saved by the replacement, for the savings ledger. 0 when nothing was suppressed. */
35
+ savedChars: number;
36
+ }
37
+ export interface LedgerOptions {
38
+ /**
39
+ * Human-readable description of the call, used in the marker so the model can tell which
40
+ * earlier result it is being pointed at ("npm test", "search for 'foo' in src/").
41
+ */
42
+ label: string;
43
+ /**
44
+ * Set when re-deriving the suppressed output is not free for the caller — a command that
45
+ * must actually run again. Purely informational; it does not change the decision.
46
+ */
47
+ rerunHint?: string;
48
+ }
49
+ /**
50
+ * Check a freshly produced output against what this tool last returned for the same call.
51
+ *
52
+ * Always call `record` afterwards with the text you actually emitted, including when this
53
+ * returns `new` — otherwise the next repeat has no base to compare against.
54
+ */
55
+ export declare function checkOutput(key: string, text: string, options: LedgerOptions): LedgerResult;
56
+ /** Record the text a tool emitted (the full payload, not a marker) as the base for next time. */
57
+ export declare function recordOutput(key: string, text: string): void;
58
+ /** Test seam — the map is process-global and would otherwise leak between test cases. */
59
+ export declare function resetOutputLedger(): void;
60
+ //# sourceMappingURL=OutputLedger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OutputLedger.d.ts","sourceRoot":"","sources":["../../../src/utils/OutputLedger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AA2BH;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAMtF;AAED,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG,WAAW,GAAG,SAAS,CAAC;AAE3D,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,YAAY,CAAC;IACrB,4FAA4F;IAC5F,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,kGAAkG;IAClG,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,YAAY,CAyC3F;AAED,iGAAiG;AACjG,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAyB5D;AAED,yFAAyF;AACzF,wBAAgB,iBAAiB,IAAI,IAAI,CAIxC"}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * OutputLedger — session memory of what each tool already returned.
3
+ *
4
+ * `ReadWorkspaceCache` solves this for file reads, but reads are not where the weight is.
5
+ * A debug loop runs `npm test` six times and `tsc --noEmit` four; each run dumps hundreds
6
+ * of near-identical lines into the transcript, and every one of those copies is re-sent on
7
+ * every later turn. By the end of a session the same failing stack trace can account for
8
+ * more context than the source files being debugged.
9
+ *
10
+ * So: key on (tool, call signature), remember the exact text emitted, and on a repeat
11
+ * answer with `IDENTICAL` or a diff instead of the payload.
12
+ *
13
+ * The correctness rule is the same one that governs the read cache, and it is absolute:
14
+ * a ledger hit may only suppress text the model demonstrably already received in this
15
+ * session. Anything else — a cold start, an evicted entry, a caller that didn't record —
16
+ * resolves to sending the full output. Under-suppressing costs tokens; over-suppressing
17
+ * costs the user a correct answer.
18
+ */
19
+ import crypto from "crypto";
20
+ import { shouldEmitDelta } from "./OutputDelta.js";
21
+ import { getSharedEntry, setSharedEntry } from "./SharedLedgerStore.js";
22
+ const entries = new Map();
23
+ /** Mirrors the read cache's bound: this exists to save tokens, not to grow forever. */
24
+ const MAX_LEDGER_CHARS = 2000000;
25
+ const MAX_ENTRY_CHARS = 400000;
26
+ let ledgerChars = 0;
27
+ let callCounter = 0;
28
+ function sha256(text) {
29
+ return crypto.createHash("sha256").update(text).digest("hex");
30
+ }
31
+ /**
32
+ * Build a stable key from the arguments that determine the output.
33
+ *
34
+ * Only pass arguments that actually change the result. Including an incidental field
35
+ * (a verbosity flag that doesn't alter the payload, a timestamp) makes every call a
36
+ * fresh key and silently disables the ledger; including too few makes two genuinely
37
+ * different calls collide, which would suppress output the model never saw. When unsure,
38
+ * include the field — a missed saving is recoverable, a wrong suppression is not.
39
+ */
40
+ export function ledgerKey(toolName, signature) {
41
+ const normalized = Object.keys(signature)
42
+ .sort()
43
+ .map((k) => `${k}=${JSON.stringify(signature[k]) ?? ""}`)
44
+ .join("&");
45
+ return `${toolName}::${normalized}`;
46
+ }
47
+ /**
48
+ * Check a freshly produced output against what this tool last returned for the same call.
49
+ *
50
+ * Always call `record` afterwards with the text you actually emitted, including when this
51
+ * returns `new` — otherwise the next repeat has no base to compare against.
52
+ */
53
+ export function checkOutput(key, text, options) {
54
+ let previous = entries.get(key);
55
+ // No in-process history for this key — check whether a sibling fork already produced
56
+ // this exact call in the shared conversation this process's tree inherits. Scoped and
57
+ // opt-in via SharedLedgerStore; a plain miss here is silent and free.
58
+ if (!previous) {
59
+ const shared = getSharedEntry(process.cwd(), "output-ledger", key);
60
+ if (shared) {
61
+ try {
62
+ const parsed = JSON.parse(shared);
63
+ previous = { text: parsed.text, hash: parsed.hash, callIndex: parsed.callIndex, at: 0 };
64
+ }
65
+ catch {
66
+ previous = undefined;
67
+ }
68
+ }
69
+ }
70
+ if (!previous)
71
+ return { status: "new", replacement: null, savedChars: 0 };
72
+ const hash = sha256(text);
73
+ if (hash === previous.hash) {
74
+ const marker = [
75
+ `IDENTICAL: ${options.label} produced byte-for-byte the same output as earlier this session (call #${previous.callIndex}).`,
76
+ `Nothing changed. Reuse that result rather than re-reading it here.`,
77
+ ...(options.rerunHint ? [options.rerunHint] : []),
78
+ ].join("\n");
79
+ return { status: "identical", replacement: marker, savedChars: Math.max(0, text.length - marker.length) };
80
+ }
81
+ const delta = shouldEmitDelta(previous.text, text);
82
+ if (!delta.emit)
83
+ return { status: "changed", replacement: null, savedChars: 0 };
84
+ const marker = [
85
+ `CHANGED: ${options.label} — ${delta.hunkCount} hunk(s), +${delta.added}/-${delta.removed} vs. call #${previous.callIndex}.`,
86
+ `Everything else is unchanged from that result. Diff:`,
87
+ ``,
88
+ delta.body,
89
+ ].join("\n");
90
+ // A diff that ended up longer than the payload is a saving of zero, not a negative one;
91
+ // the caller decides what to emit, this only reports what was actually spared.
92
+ return { status: "changed", replacement: marker, savedChars: Math.max(0, text.length - marker.length) };
93
+ }
94
+ /** Record the text a tool emitted (the full payload, not a marker) as the base for next time. */
95
+ export function recordOutput(key, text) {
96
+ const previous = entries.get(key);
97
+ if (previous) {
98
+ ledgerChars -= previous.text.length;
99
+ // `set` on an existing key keeps its original insertion position, so delete first to
100
+ // make a refreshed entry count as recently-written for eviction.
101
+ entries.delete(key);
102
+ }
103
+ if (text.length > MAX_ENTRY_CHARS)
104
+ return;
105
+ callCounter++;
106
+ const hash = sha256(text);
107
+ entries.set(key, { text, hash, callIndex: callCounter, at: Date.now() });
108
+ ledgerChars += text.length;
109
+ setSharedEntry(process.cwd(), "output-ledger", key, JSON.stringify({ text, hash, callIndex: callCounter }));
110
+ while (ledgerChars > MAX_LEDGER_CHARS) {
111
+ const oldest = entries.keys().next();
112
+ if (oldest.done)
113
+ break;
114
+ const dropped = entries.get(oldest.value);
115
+ entries.delete(oldest.value);
116
+ ledgerChars -= dropped.text.length;
117
+ }
118
+ }
119
+ /** Test seam — the map is process-global and would otherwise leak between test cases. */
120
+ export function resetOutputLedger() {
121
+ entries.clear();
122
+ ledgerChars = 0;
123
+ callCounter = 0;
124
+ }
125
+ //# sourceMappingURL=OutputL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AAW,IAAI,IAAI,CAAC,MAAM,CAAC;IAE3B,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,eAAe,EAAE,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC;IAE5G,OAAO,WAAW,GAAG,gBAAgB,EAAE,CAAC;QACtC,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;QACrC,IAAI,MAAM,CAAC,IAAI;YAAE,MAAM;QACvB,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAE,CAAC;QAC3C,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC;IACrC,CAAC;AACH,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,iBAAiB;IAC/B,OAAO,CAAC,KAAK,EAAE,CAAC;IAChB,WAAW,GAAG,CAAC,CAAC;IAChB,WAAW,GAAG,CAAC,CAAC;AAClB,CAAC"}
@@ -15,8 +15,14 @@ export interface ReadDedupCheck {
15
15
  }
16
16
  /** Check whether this exact (path, view) content was already sent this session. */
17
17
  export declare function checkAlreadySent(resolvedPath: string, view: ReadView, rawContent: string): ReadDedupCheck;
18
+ /**
19
+ * The text last emitted for this (path, view), or null if we have none — because this is
20
+ * the first read, or because the entry was evicted under the size cap. A null here means
21
+ * "send the full file", never "assume it is unchanged".
22
+ */
23
+ export declare function getLastEmitted(resolvedPath: string, view: ReadView): string | null;
18
24
  /** Record that this content was just sent, so future identical reads short-circuit. */
19
- export declare function recordSent(resolvedPath: string, view: ReadView, hash: string): void;
20
- /** Test seam — the map is process-global and would otherwise leak between test cases. */
25
+ export declare function recordSent(resolvedPath: string, view: ReadView, hash: string, emitted?: string): void;
26
+ /** Test seam — the maps are process-global and would otherwise leak between test cases. */
21
27
  export declare function resetReadCache(): void;
22
28
  //# sourceMappingURL=ReadWorkspaceCache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ReadWorkspaceCache.d.ts","sourceRoot":"","sources":["../../../src/utils/ReadWorkspaceCache.ts"],"names":[],"mappings":"AAYA;;;;GAIG;AACH,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAaD,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,OAAO,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,mFAAmF;AACnF,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,GAAG,cAAc,CAKzG;AAED,uFAAuF;AACvF,wBAAgB,UAAU,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAEnF;AAED,yFAAyF;AACzF,wBAAgB,cAAc,IAAI,IAAI,CAErC"}
1
+ {"version":3,"file":"ReadWorkspaceCache.d.ts","sourceRoot":"","sources":["../../../src/utils/ReadWorkspaceCache.ts"],"names":[],"mappings":"AAiCA;;;;GAIG;AACH,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAaD,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,OAAO,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAiBD,mFAAmF;AACnF,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,GAAG,cAAc,CAOzG;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,CAKlF;AAED,uFAAuF;AACvF,wBAAgB,UAAU,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAsCrG;AAED,2FAA2F;AAC3F,wBAAgB,cAAc,IAAI,IAAI,CAIrC"}
@@ -1,9 +1,26 @@
1
1
  import crypto from "crypto";
2
+ import { getSharedEntry, setSharedEntry } from "./SharedLedgerStore.js";
2
3
  // Session-scoped (lives for the MCP server process lifetime, which is normally the
3
4
  // whole conversation). Tracks the content hash we last actually sent back to the
4
5
  // model for a given (filePath, view) pair, so a second identical read doesn't
5
6
  // resend content the model already has in its own context.
6
7
  const lastSent = new Map();
8
+ /**
9
+ * The exact text emitted for each key, kept so a later read of a *changed* file can be
10
+ * answered with a diff instead of the whole file again. Only what the model actually
11
+ * received is stored — diffing against anything else would describe a document it never
12
+ * had.
13
+ */
14
+ const lastEmitted = new Map();
15
+ /**
16
+ * Total characters held in `lastEmitted`. This cache exists to save tokens, so it must
17
+ * not grow without bound in a long session; past the cap the least-recently-written
18
+ * entries are dropped and those reads simply fall back to sending the full file.
19
+ */
20
+ const MAX_EMITTED_CHARS = 2000000;
21
+ /** A single entry larger than this is never stored — one huge file would evict everything else. */
22
+ const MAX_ENTRY_CHARS = 400000;
23
+ let emittedChars = 0;
7
24
  function sha256(content) {
8
25
  return crypto.createHash("sha256").update(content).digest("hex");
9
26
  }
@@ -17,19 +34,79 @@ function cacheKey(resolvedPath, view) {
17
34
  ];
18
35
  return parts.join("::");
19
36
  }
37
+ function getSharedRead(resolvedPath, view) {
38
+ const raw = getSharedEntry(process.cwd(), "read-cache", cacheKey(resolvedPath, view));
39
+ if (!raw)
40
+ return null;
41
+ try {
42
+ return JSON.parse(raw);
43
+ }
44
+ catch {
45
+ return null;
46
+ }
47
+ }
20
48
  /** Check whether this exact (path, view) content was already sent this session. */
21
49
  export function checkAlreadySent(resolvedPath, view, rawContent) {
22
50
  const hash = sha256(rawContent);
23
51
  const key = cacheKey(resolvedPath, view);
24
- const unchanged = lastSent.get(key) === hash;
52
+ // In-process history wins; a sibling fork's shared entry only counts when this process
53
+ // has never seen the key itself — same "opt-in, scoped, fail-open" contract as OutputLedger.
54
+ const unchanged = lastSent.has(key) ? lastSent.get(key) === hash : getSharedRead(resolvedPath, view)?.hash === hash;
25
55
  return { unchanged, hash };
26
56
  }
57
+ /**
58
+ * The text last emitted for this (path, view), or null if we have none — because this is
59
+ * the first read, or because the entry was evicted under the size cap. A null here means
60
+ * "send the full file", never "assume it is unchanged".
61
+ */
62
+ export function getLastEmitted(resolvedPath, view) {
63
+ const key = cacheKey(resolvedPath, view);
64
+ const local = lastEmitted.get(key);
65
+ if (local !== undefined)
66
+ return local;
67
+ return getSharedRead(resolvedPath, view)?.emitted ?? null;
68
+ }
27
69
  /** Record that this content was just sent, so future identical reads short-circuit. */
28
- export function recordSent(resolvedPath, view, hash) {
29
- lastSent.set(cacheKey(resolvedPath, view), hash);
70
+ export function recordSent(resolvedPath, view, hash, emitted) {
71
+ const key = cacheKey(resolvedPath, view);
72
+ lastSent.set(key, hash);
73
+ // Mirrors the local size cap: an oversized entry is skipped locally too, so keeping it
74
+ // out of the shared file avoids bloating something every sibling process reads on every hit.
75
+ if (emitted === undefined || emitted.length <= MAX_ENTRY_CHARS) {
76
+ setSharedEntry(process.cwd(), "read-cache", key, JSON.stringify({ hash, emitted }));
77
+ }
78
+ if (emitted === undefined)
79
+ return;
80
+ const previous = lastEmitted.get(key);
81
+ if (previous !== undefined) {
82
+ emittedChars -= previous.length;
83
+ // Map keeps insertion order, and `set` on an existing key does not move it. Deleting
84
+ // first is what makes a refreshed entry count as recently-written for eviction.
85
+ lastEmitted.delete(key);
86
+ }
87
+ if (emitted.length > MAX_ENTRY_CHARS) {
88
+ // Too big to keep. Drop any stale copy rather than leaving a diff base that no longer
89
+ // matches what the model holds.
90
+ lastEmitted.delete(key);
91
+ return;
92
+ }
93
+ lastEmitted.set(key, emitted);
94
+ emittedChars += emitted.length;
95
+ // Map iteration is insertion-ordered, and re-inserting on every write above moves a
96
+ // refreshed key to the back — so evicting from the front drops the least-recently-written.
97
+ while (emittedChars > MAX_EMITTED_CHARS) {
98
+ const oldest = lastEmitted.keys().next();
99
+ if (oldest.done)
100
+ break;
101
+ const dropped = lastEmitted.get(oldest.value);
102
+ lastEmitted.delete(oldest.value);
103
+ emittedChars -= dropped.length;
104
+ }
30
105
  }
31
- /** Test seam — the map is process-global and would otherwise leak between test cases. */
106
+ /** Test seam — the maps are process-global and would otherwise leak between test cases. */
32
107
  export function resetReadCache() {
33
108
  lastSent.clear();
109
+ lastEmitted.clear();
110
+ emittedChars = 0;
34
111
  }
35
112
  //# sourceMappingURL=ReadWorkspaceCache.js.map