@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.
- package/README.md +24 -4
- package/dist/cjs/cli/lemma-proxy.d.ts.map +1 -1
- package/dist/cjs/cli/lemma-proxy.js +7 -2
- package/dist/cjs/cli/lemma-proxy.js.map +1 -1
- package/dist/cjs/mcp/tool-profiles.d.ts.map +1 -1
- package/dist/cjs/mcp/tool-profiles.js +43 -0
- package/dist/cjs/mcp/tool-profiles.js.map +1 -1
- package/dist/cjs/mcp/tools.d.ts.map +1 -1
- package/dist/cjs/mcp/tools.js +314 -146
- package/dist/cjs/mcp/tools.js.map +1 -1
- package/dist/cjs/subconscious/TheBrainV2.d.ts +27 -1
- package/dist/cjs/subconscious/TheBrainV2.d.ts.map +1 -1
- package/dist/cjs/subconscious/TheBrainV2.js +53 -1
- package/dist/cjs/subconscious/TheBrainV2.js.map +1 -1
- package/dist/cjs/utils/AffectedTests.d.ts +54 -0
- package/dist/cjs/utils/AffectedTests.d.ts.map +1 -0
- package/dist/cjs/utils/AffectedTests.js +195 -0
- package/dist/cjs/utils/AffectedTests.js.map +1 -0
- package/dist/cjs/utils/AstSymbolCache.d.ts +29 -0
- package/dist/cjs/utils/AstSymbolCache.d.ts.map +1 -0
- package/dist/cjs/utils/AstSymbolCache.js +66 -0
- package/dist/cjs/utils/AstSymbolCache.js.map +1 -0
- package/dist/cjs/utils/ContextSqueezer.d.ts +24 -4
- package/dist/cjs/utils/ContextSqueezer.d.ts.map +1 -1
- package/dist/cjs/utils/ContextSqueezer.js +32 -3
- package/dist/cjs/utils/ContextSqueezer.js.map +1 -1
- package/dist/cjs/utils/DepGraph.d.ts +28 -0
- package/dist/cjs/utils/DepGraph.d.ts.map +1 -0
- package/dist/cjs/utils/DepGraph.js +210 -0
- package/dist/cjs/utils/DepGraph.js.map +1 -0
- package/dist/cjs/utils/LocalPrefilter.d.ts +61 -0
- package/dist/cjs/utils/LocalPrefilter.d.ts.map +1 -0
- package/dist/cjs/utils/LocalPrefilter.js +169 -0
- package/dist/cjs/utils/LocalPrefilter.js.map +1 -0
- package/dist/cjs/utils/OutputDelta.d.ts +60 -0
- package/dist/cjs/utils/OutputDelta.d.ts.map +1 -0
- package/dist/cjs/utils/OutputDelta.js +226 -0
- package/dist/cjs/utils/OutputDelta.js.map +1 -0
- package/dist/cjs/utils/OutputLedger.d.ts +60 -0
- package/dist/cjs/utils/OutputLedger.d.ts.map +1 -0
- package/dist/cjs/utils/OutputLedger.js +134 -0
- package/dist/cjs/utils/OutputLedger.js.map +1 -0
- package/dist/cjs/utils/ReadWorkspaceCache.d.ts +8 -2
- package/dist/cjs/utils/ReadWorkspaceCache.d.ts.map +1 -1
- package/dist/cjs/utils/ReadWorkspaceCache.js +82 -4
- package/dist/cjs/utils/ReadWorkspaceCache.js.map +1 -1
- package/dist/cjs/utils/RoiLedger.d.ts +31 -0
- package/dist/cjs/utils/RoiLedger.d.ts.map +1 -0
- package/dist/cjs/utils/RoiLedger.js +113 -0
- package/dist/cjs/utils/RoiLedger.js.map +1 -0
- package/dist/cjs/utils/SharedLedgerStore.d.ts +26 -0
- package/dist/cjs/utils/SharedLedgerStore.d.ts.map +1 -0
- package/dist/cjs/utils/SharedLedgerStore.js +130 -0
- package/dist/cjs/utils/SharedLedgerStore.js.map +1 -0
- package/dist/cjs/utils/SpeculativeWarmer.d.ts +24 -0
- package/dist/cjs/utils/SpeculativeWarmer.d.ts.map +1 -0
- package/dist/cjs/utils/SpeculativeWarmer.js +73 -0
- package/dist/cjs/utils/SpeculativeWarmer.js.map +1 -0
- package/dist/cjs/utils/reportSavings.d.ts.map +1 -1
- package/dist/cjs/utils/reportSavings.js +11 -0
- package/dist/cjs/utils/reportSavings.js.map +1 -1
- package/dist/esm/cli/lemma-proxy.d.ts.map +1 -1
- package/dist/esm/cli/lemma-proxy.js +7 -2
- package/dist/esm/cli/lemma-proxy.js.map +1 -1
- package/dist/esm/mcp/tool-profiles.d.ts.map +1 -1
- package/dist/esm/mcp/tool-profiles.js +43 -0
- package/dist/esm/mcp/tool-profiles.js.map +1 -1
- package/dist/esm/mcp/tools.d.ts.map +1 -1
- package/dist/esm/mcp/tools.js +312 -144
- package/dist/esm/mcp/tools.js.map +1 -1
- package/dist/esm/subconscious/TheBrainV2.d.ts +27 -1
- package/dist/esm/subconscious/TheBrainV2.d.ts.map +1 -1
- package/dist/esm/subconscious/TheBrainV2.js +53 -1
- package/dist/esm/subconscious/TheBrainV2.js.map +1 -1
- package/dist/esm/utils/AffectedTests.d.ts +54 -0
- package/dist/esm/utils/AffectedTests.d.ts.map +1 -0
- package/dist/esm/utils/AffectedTests.js +186 -0
- package/dist/esm/utils/AffectedTests.js.map +1 -0
- package/dist/esm/utils/AstSymbolCache.d.ts +29 -0
- package/dist/esm/utils/AstSymbolCache.d.ts.map +1 -0
- package/dist/esm/utils/AstSymbolCache.js +58 -0
- package/dist/esm/utils/AstSymbolCache.js.map +1 -0
- package/dist/esm/utils/ContextSqueezer.d.ts +24 -4
- package/dist/esm/utils/ContextSqueezer.d.ts.map +1 -1
- package/dist/esm/utils/ContextSqueezer.js +32 -3
- package/dist/esm/utils/ContextSqueezer.js.map +1 -1
- package/dist/esm/utils/DepGraph.d.ts +28 -0
- package/dist/esm/utils/DepGraph.d.ts.map +1 -0
- package/dist/esm/utils/DepGraph.js +169 -0
- package/dist/esm/utils/DepGraph.js.map +1 -0
- package/dist/esm/utils/LocalPrefilter.d.ts +61 -0
- package/dist/esm/utils/LocalPrefilter.d.ts.map +1 -0
- package/dist/esm/utils/LocalPrefilter.js +159 -0
- package/dist/esm/utils/LocalPrefilter.js.map +1 -0
- package/dist/esm/utils/OutputDelta.d.ts +60 -0
- package/dist/esm/utils/OutputDelta.d.ts.map +1 -0
- package/dist/esm/utils/OutputDelta.js +221 -0
- package/dist/esm/utils/OutputDelta.js.map +1 -0
- package/dist/esm/utils/OutputLedger.d.ts +60 -0
- package/dist/esm/utils/OutputLedger.d.ts.map +1 -0
- package/dist/esm/utils/OutputLedger.js +125 -0
- package/dist/esm/utils/OutputLedger.js.map +1 -0
- package/dist/esm/utils/ReadWorkspaceCache.d.ts +8 -2
- package/dist/esm/utils/ReadWorkspaceCache.d.ts.map +1 -1
- package/dist/esm/utils/ReadWorkspaceCache.js +81 -4
- package/dist/esm/utils/ReadWorkspaceCache.js.map +1 -1
- package/dist/esm/utils/RoiLedger.d.ts +31 -0
- package/dist/esm/utils/RoiLedger.d.ts.map +1 -0
- package/dist/esm/utils/RoiLedger.js +104 -0
- package/dist/esm/utils/RoiLedger.js.map +1 -0
- package/dist/esm/utils/SharedLedgerStore.d.ts +26 -0
- package/dist/esm/utils/SharedLedgerStore.d.ts.map +1 -0
- package/dist/esm/utils/SharedLedgerStore.js +122 -0
- package/dist/esm/utils/SharedLedgerStore.js.map +1 -0
- package/dist/esm/utils/SpeculativeWarmer.d.ts +24 -0
- package/dist/esm/utils/SpeculativeWarmer.d.ts.map +1 -0
- package/dist/esm/utils/SpeculativeWarmer.js +66 -0
- package/dist/esm/utils/SpeculativeWarmer.js.map +1 -0
- package/dist/esm/utils/reportSavings.d.ts.map +1 -1
- package/dist/esm/utils/reportSavings.js +11 -0
- package/dist/esm/utils/reportSavings.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LocalPrefilter — spend a local model's tokens so the expensive one never sees the noise.
|
|
4
|
+
*
|
|
5
|
+
* A broad `search_workspace` returns 200 matches across 60 files. Four of them matter. The
|
|
6
|
+
* frontier model currently pays for all 200 — in the turn that produced them and in every
|
|
7
|
+
* turn afterwards, because tool results stay in the transcript. A 3B model running on the
|
|
8
|
+
* user's own machine can rank that list for free, in the local sense of free: no API cost,
|
|
9
|
+
* no context cost, no per-token billing.
|
|
10
|
+
*
|
|
11
|
+
* This is the one place in Lemma where a model's judgement decides what the main model
|
|
12
|
+
* sees, so the safety rules are stricter than anywhere else:
|
|
13
|
+
*
|
|
14
|
+
* 1. **It ranks; it does not delete.** The prefilter reorders candidates and the caller
|
|
15
|
+
* shows a prefix of that order. Nothing is claimed to be irrelevant — the output says
|
|
16
|
+
* how many were withheld and how to get them all back.
|
|
17
|
+
* 2. **Fail open, always.** No Ollama, wrong model, timeout, malformed reply, an index out
|
|
18
|
+
* of range — every one of those returns null and the caller sends the unfiltered list.
|
|
19
|
+
* A prefilter that fails closed would silently hide code from the model.
|
|
20
|
+
* 3. **Disclosed at the call site.** The caller must state that a local model did the
|
|
21
|
+
* ranking. A subset presented as if it were the whole search is a lie by omission,
|
|
22
|
+
* and the user cannot audit what they were not told happened.
|
|
23
|
+
*
|
|
24
|
+
* Opt-in: set `LEMMA_LOCAL_PREFILTER=1` (or `mcp.localPrefilter: true` in lemma.config.json).
|
|
25
|
+
* Off by default, because a wrong ranking costs a correct answer and the user should choose
|
|
26
|
+
* to accept that trade.
|
|
27
|
+
*/
|
|
28
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.loadPrefilterConfig = loadPrefilterConfig;
|
|
33
|
+
exports.isLocalModelAvailable = isLocalModelAvailable;
|
|
34
|
+
exports.parseRanking = parseRanking;
|
|
35
|
+
exports.rankCandidates = rankCandidates;
|
|
36
|
+
exports.resetPrefilterProbe = resetPrefilterProbe;
|
|
37
|
+
const fs_1 = __importDefault(require("fs"));
|
|
38
|
+
const path_1 = __importDefault(require("path"));
|
|
39
|
+
const axios_1 = __importDefault(require("axios"));
|
|
40
|
+
const DEFAULT_MODEL = "qwen2.5-coder:3b";
|
|
41
|
+
const PROBE_TIMEOUT_MS = 1500;
|
|
42
|
+
const RANK_TIMEOUT_MS = 12000;
|
|
43
|
+
/**
|
|
44
|
+
* Below this many candidates the local round-trip (latency the user waits on) is not worth
|
|
45
|
+
* it — the full list was already cheap.
|
|
46
|
+
*/
|
|
47
|
+
const MIN_CANDIDATES = 25;
|
|
48
|
+
function loadPrefilterConfig(cwd = process.cwd()) {
|
|
49
|
+
let fileEnabled;
|
|
50
|
+
let fileModel;
|
|
51
|
+
try {
|
|
52
|
+
const raw = JSON.parse(fs_1.default.readFileSync(path_1.default.join(cwd, "lemma.config.json"), "utf8"));
|
|
53
|
+
if (typeof raw?.mcp?.localPrefilter === "boolean")
|
|
54
|
+
fileEnabled = raw.mcp.localPrefilter;
|
|
55
|
+
if (typeof raw?.mcp?.localPrefilterModel === "string")
|
|
56
|
+
fileModel = raw.mcp.localPrefilterModel;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// An unreadable config must never turn a feature on that the user did not ask for.
|
|
60
|
+
}
|
|
61
|
+
const envEnabled = process.env.LEMMA_LOCAL_PREFILTER;
|
|
62
|
+
const enabled = envEnabled === "1" || envEnabled === "true" ? true : envEnabled === "0" || envEnabled === "false" ? false : fileEnabled === true;
|
|
63
|
+
return {
|
|
64
|
+
enabled,
|
|
65
|
+
url: process.env.OLLAMA_URL || process.env.OLLAMA_HOST || "http://localhost:11434",
|
|
66
|
+
model: process.env.LEMMA_LOCAL_PREFILTER_MODEL || fileModel || DEFAULT_MODEL,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Availability is cached for the process: probing on every search would add a round-trip to
|
|
71
|
+
* the very path this is meant to make cheaper. A negative result is cached too — if Ollama
|
|
72
|
+
* isn't running at the start of a session it is almost never running by the end, and
|
|
73
|
+
* re-probing would tax every search with a timeout.
|
|
74
|
+
*/
|
|
75
|
+
let availability = { checked: false, available: false };
|
|
76
|
+
async function isLocalModelAvailable(config) {
|
|
77
|
+
if (!config.enabled)
|
|
78
|
+
return false;
|
|
79
|
+
if (availability.checked)
|
|
80
|
+
return availability.available;
|
|
81
|
+
try {
|
|
82
|
+
const res = await axios_1.default.get(`${config.url}/api/tags`, { timeout: PROBE_TIMEOUT_MS });
|
|
83
|
+
const models = (res.data?.models || []).map((m) => String(m?.name ?? ""));
|
|
84
|
+
// A configured model that isn't pulled would fail on every call; treat that as
|
|
85
|
+
// unavailable rather than discovering it once per search.
|
|
86
|
+
const base = config.model.split(":")[0];
|
|
87
|
+
availability = { checked: true, available: models.some((m) => m === config.model || m.split(":")[0] === base) };
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
availability = { checked: true, available: false };
|
|
91
|
+
}
|
|
92
|
+
return availability.available;
|
|
93
|
+
}
|
|
94
|
+
function buildPrompt(query, candidates) {
|
|
95
|
+
const list = candidates.map((c, i) => `${i}. ${c.summary}`).join("\n");
|
|
96
|
+
return [
|
|
97
|
+
`A developer is searching a codebase for: "${query}"`,
|
|
98
|
+
``,
|
|
99
|
+
`Below are search results, numbered. Rank them by how likely each is to be what the`,
|
|
100
|
+
`developer is looking for. Definitions and primary implementations rank above call`,
|
|
101
|
+
`sites; call sites rank above tests, mocks, generated files and build output.`,
|
|
102
|
+
``,
|
|
103
|
+
list,
|
|
104
|
+
``,
|
|
105
|
+
`Reply with ONLY the numbers, most relevant first, comma-separated. Include every`,
|
|
106
|
+
`number exactly once. No explanation, no other text.`,
|
|
107
|
+
].join("\n");
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Parse the model's reply into an ordering.
|
|
111
|
+
*
|
|
112
|
+
* Strict on purpose. A small model will sometimes emit prose, duplicate an index, or invent
|
|
113
|
+
* one; any of those means we do not actually know what it decided, and guessing at its
|
|
114
|
+
* intent is how the relevant file ends up ranked last. Anything not exactly parseable
|
|
115
|
+
* returns null, and the caller sends everything.
|
|
116
|
+
*/
|
|
117
|
+
function parseRanking(reply, candidates) {
|
|
118
|
+
const numbers = (reply.match(/\d+/g) || []).map((n) => parseInt(n, 10));
|
|
119
|
+
if (numbers.length === 0)
|
|
120
|
+
return null;
|
|
121
|
+
const seen = new Set();
|
|
122
|
+
const order = [];
|
|
123
|
+
for (const n of numbers) {
|
|
124
|
+
if (n < 0 || n >= candidates.length)
|
|
125
|
+
return null; // hallucinated index — reply is untrustworthy
|
|
126
|
+
if (seen.has(n))
|
|
127
|
+
continue; // a repeat is sloppy, not wrong; the first position stands
|
|
128
|
+
seen.add(n);
|
|
129
|
+
order.push(candidates[n].id);
|
|
130
|
+
}
|
|
131
|
+
// Anything the model left out is appended in its original order rather than dropped.
|
|
132
|
+
// Ranking is not filtering — the caller decides what to show, and it must be able to
|
|
133
|
+
// show all of them.
|
|
134
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
135
|
+
if (!seen.has(i))
|
|
136
|
+
order.push(candidates[i].id);
|
|
137
|
+
}
|
|
138
|
+
return order;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Rank candidates with the local model. Returns null whenever ranking did not definitively
|
|
142
|
+
* succeed — the caller must then use its own original order and withhold nothing.
|
|
143
|
+
*/
|
|
144
|
+
async function rankCandidates(query, candidates, config = loadPrefilterConfig()) {
|
|
145
|
+
if (candidates.length < MIN_CANDIDATES)
|
|
146
|
+
return null;
|
|
147
|
+
if (!(await isLocalModelAvailable(config)))
|
|
148
|
+
return null;
|
|
149
|
+
try {
|
|
150
|
+
const res = await axios_1.default.post(`${config.url}/api/generate`, {
|
|
151
|
+
model: config.model,
|
|
152
|
+
prompt: buildPrompt(query, candidates),
|
|
153
|
+
stream: false,
|
|
154
|
+
// Deterministic: the same search should not reorder itself between calls, and
|
|
155
|
+
// creativity is worthless for a ranking task.
|
|
156
|
+
options: { temperature: 0, num_predict: 512 },
|
|
157
|
+
}, { timeout: RANK_TIMEOUT_MS });
|
|
158
|
+
const order = parseRanking(String(res.data?.response ?? ""), candidates);
|
|
159
|
+
return order ? { order, model: config.model } : null;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Test seam — the availability probe is cached for the process lifetime. */
|
|
166
|
+
function resetPrefilterProbe() {
|
|
167
|
+
availability = { checked: false, available: false };
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=LocalPrefilter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalPrefilter.js","sourceRoot":"","sources":["../../../src/utils/LocalPrefilter.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;;;AAsBH,kDAmBC;AAUD,sDAeC;AAuCD,oCAqBC;AAMD,wCA2BC;AAGD,kDAEC;AAlKD,4CAAoB;AACpB,gDAAwB;AACxB,kDAA0B;AAE1B,MAAM,aAAa,GAAG,kBAAkB,CAAC;AACzC,MAAM,gBAAgB,GAAG,IAAK,CAAC;AAC/B,MAAM,eAAe,GAAG,KAAM,CAAC;AAE/B;;;GAGG;AACH,MAAM,cAAc,GAAG,EAAE,CAAC;AAQ1B,SAAgB,mBAAmB,CAAC,MAAc,OAAO,CAAC,GAAG,EAAE;IAC7D,IAAI,WAAgC,CAAC;IACrC,IAAI,SAA6B,CAAC;IAClC,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,cAAI,CAAC,IAAI,CAAC,GAAG,EAAE,mBAAmB,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;QACrF,IAAI,OAAO,GAAG,EAAE,GAAG,EAAE,cAAc,KAAK,SAAS;YAAE,WAAW,GAAG,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC;QACxF,IAAI,OAAO,GAAG,EAAE,GAAG,EAAE,mBAAmB,KAAK,QAAQ;YAAE,SAAS,GAAG,GAAG,CAAC,GAAG,CAAC,mBAAmB,CAAC;IACjG,CAAC;IAAC,MAAM,CAAC;QACP,mFAAmF;IACrF,CAAC;IAED,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC;IACrD,MAAM,OAAO,GAAG,UAAU,KAAK,GAAG,IAAI,UAAU,KAAK,MAAM,CA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AC,IAAI,EAAE,QAAQ,IAAI,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;QACzE,OAAO,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACvD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,6EAA6E;AAC7E,SAAgB,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,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* OutputDelta — re-send only what changed, never the whole thing again.
|
|
4
|
+
*
|
|
5
|
+
* The problem: a debug loop reads the same file (or runs the same command) six times.
|
|
6
|
+
* Between two of those calls maybe three lines moved, but the model pays for the entire
|
|
7
|
+
* file or the entire 400-line `tsc` dump each time — and because tool results stay in the
|
|
8
|
+
* transcript, it keeps paying for every copy on every subsequent turn.
|
|
9
|
+
*
|
|
10
|
+
* `ReadWorkspaceCache` already suppresses the *identical* case. This module covers the far
|
|
11
|
+
* more common one: it changed, but barely.
|
|
12
|
+
*
|
|
13
|
+
* Two rules this module is built around:
|
|
14
|
+
*
|
|
15
|
+
* 1. The diff is computed against the exact text the model received. Not the file on
|
|
16
|
+
* disk, not the pre-squeeze source — the final emitted string. A diff against
|
|
17
|
+
* anything else describes a document the model never had, which is worse than
|
|
18
|
+
* re-sending the whole file.
|
|
19
|
+
* 2. A delta is only emitted when it is decisively smaller than the full text. Below
|
|
20
|
+
* that margin, reconstructing from a diff is cognitive work with no token payoff, so
|
|
21
|
+
* the full text wins. See `shouldEmitDelta`.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.computeDiff = computeDiff;
|
|
25
|
+
exports.renderDiff = renderDiff;
|
|
26
|
+
exports.shouldEmitDelta = shouldEmitDelta;
|
|
27
|
+
/**
|
|
28
|
+
* Lines of unchanged context kept around each changed hunk. Three is the unified-diff
|
|
29
|
+
* convention and is enough to locate a hunk unambiguously in almost all real files.
|
|
30
|
+
*/
|
|
31
|
+
const CONTEXT_LINES = 3;
|
|
32
|
+
/**
|
|
33
|
+
* A delta must be at most this fraction of the full text to be worth emitting. At 0.5 a
|
|
34
|
+
* diff that saves only a third of the tokens is rejected: the model still has to
|
|
35
|
+
* mentally apply it, and a near-full-size diff is strictly worse than the real text.
|
|
36
|
+
*/
|
|
37
|
+
const DELTA_MAX_RATIO = 0.5;
|
|
38
|
+
/**
|
|
39
|
+
* Below this size the full text is cheap enough that a diff never pays for its own
|
|
40
|
+
* explanation header, let alone the risk of misapplication.
|
|
41
|
+
*/
|
|
42
|
+
const DELTA_MIN_CHARS = 1200;
|
|
43
|
+
/**
|
|
44
|
+
* Diffing is O(n*m) in the worst case. Past this line count the DP table costs more CPU
|
|
45
|
+
* (and latency the user waits on) than the tokens it could save, so we decline.
|
|
46
|
+
*/
|
|
47
|
+
const MAX_DIFF_LINES = 4000;
|
|
48
|
+
/**
|
|
49
|
+
* Longest-common-subsequence over lines.
|
|
50
|
+
*
|
|
51
|
+
* Real edits touch a handful of lines in a file whose head and tail are untouched, so we
|
|
52
|
+
* strip the common prefix and suffix first. That turns the usual case into a DP over a
|
|
53
|
+
* few dozen lines instead of a few thousand, which is what keeps this affordable enough
|
|
54
|
+
* to run on every read.
|
|
55
|
+
*/
|
|
56
|
+
function diffLines(oldLines, newLines) {
|
|
57
|
+
let start = 0;
|
|
58
|
+
while (start < oldLines.length && start < newLines.length && oldLines[start] === newLines[start]) {
|
|
59
|
+
start++;
|
|
60
|
+
}
|
|
61
|
+
let endOld = oldLines.length;
|
|
62
|
+
let endNew = newLines.length;
|
|
63
|
+
while (endOld > start && endNew > start && oldLines[endOld - 1] === newLines[endNew - 1]) {
|
|
64
|
+
endOld--;
|
|
65
|
+
endNew--;
|
|
66
|
+
}
|
|
67
|
+
const a = oldLines.slice(start, endOld);
|
|
68
|
+
const b = newLines.slice(start, endNew);
|
|
69
|
+
const ops = [];
|
|
70
|
+
for (let i = 0; i < start; i++)
|
|
71
|
+
ops.push({ type: "equal", line: oldLines[i] });
|
|
72
|
+
// DP table of LCS lengths. Sized on the trimmed middle, which is why the trimming above
|
|
73
|
+
// matters: without it this allocates (n+1)*(m+1) numbers for the whole file.
|
|
74
|
+
const n = a.length;
|
|
75
|
+
const m = b.length;
|
|
76
|
+
const table = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
|
|
77
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
78
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
79
|
+
table[i][j] = a[i] === b[j] ? table[i + 1][j + 1] + 1 : Math.max(table[i + 1][j], table[i][j + 1]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
let i = 0;
|
|
83
|
+
let j = 0;
|
|
84
|
+
while (i < n && j < m) {
|
|
85
|
+
if (a[i] === b[j]) {
|
|
86
|
+
ops.push({ type: "equal", line: a[i] });
|
|
87
|
+
i++;
|
|
88
|
+
j++;
|
|
89
|
+
}
|
|
90
|
+
else if (table[i + 1][j] >= table[i][j + 1]) {
|
|
91
|
+
ops.push({ type: "del", line: a[i] });
|
|
92
|
+
i++;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
ops.push({ type: "ins", line: b[j] });
|
|
96
|
+
j++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
while (i < n)
|
|
100
|
+
ops.push({ type: "del", line: a[i++] });
|
|
101
|
+
while (j < m)
|
|
102
|
+
ops.push({ type: "ins", line: b[j++] });
|
|
103
|
+
for (let k = endOld; k < oldLines.length; k++)
|
|
104
|
+
ops.push({ type: "equal", line: oldLines[k] });
|
|
105
|
+
return ops;
|
|
106
|
+
}
|
|
107
|
+
/** Group an op stream into unified-diff hunks with `CONTEXT_LINES` of surrounding context. */
|
|
108
|
+
function buildHunks(ops) {
|
|
109
|
+
const changedAt = new Set();
|
|
110
|
+
ops.forEach((op, idx) => {
|
|
111
|
+
if (op.type !== "equal")
|
|
112
|
+
changedAt.add(idx);
|
|
113
|
+
});
|
|
114
|
+
if (changedAt.size === 0)
|
|
115
|
+
return [];
|
|
116
|
+
// Expand each change into a window, then merge windows that touch or overlap so two
|
|
117
|
+
// edits four lines apart render as one hunk rather than two with duplicated context.
|
|
118
|
+
const windows = [];
|
|
119
|
+
for (const idx of Array.from(changedAt).sort((x, y) => x - y)) {
|
|
120
|
+
const lo = Math.max(0, idx - CONTEXT_LINES);
|
|
121
|
+
const hi = Math.min(ops.length - 1, idx + CONTEXT_LINES);
|
|
122
|
+
const last = windows[windows.length - 1];
|
|
123
|
+
if (last && lo <= last[1] + 1)
|
|
124
|
+
last[1] = Math.max(last[1], hi);
|
|
125
|
+
else
|
|
126
|
+
windows.push([lo, hi]);
|
|
127
|
+
}
|
|
128
|
+
// Line counters advance as we scan, so each hunk header reports true positions in both
|
|
129
|
+
// documents rather than positions relative to the hunk before it.
|
|
130
|
+
const hunks = [];
|
|
131
|
+
let oldLine = 1;
|
|
132
|
+
let newLine = 1;
|
|
133
|
+
let cursor = 0;
|
|
134
|
+
for (const [lo, hi] of windows) {
|
|
135
|
+
while (cursor < lo) {
|
|
136
|
+
const op = ops[cursor];
|
|
137
|
+
if (op.type !== "ins")
|
|
138
|
+
oldLine++;
|
|
139
|
+
if (op.type !== "del")
|
|
140
|
+
newLine++;
|
|
141
|
+
cursor++;
|
|
142
|
+
}
|
|
143
|
+
const hunk = { oldStart: oldLine, oldLines: 0, newStart: newLine, newLines: 0, lines: [] };
|
|
144
|
+
for (let idx = lo; idx <= hi; idx++) {
|
|
145
|
+
const op = ops[idx];
|
|
146
|
+
if (op.type === "equal") {
|
|
147
|
+
hunk.lines.push(` ${op.line}`);
|
|
148
|
+
hunk.oldLines++;
|
|
149
|
+
hunk.newLines++;
|
|
150
|
+
oldLine++;
|
|
151
|
+
newLine++;
|
|
152
|
+
}
|
|
153
|
+
else if (op.type === "del") {
|
|
154
|
+
hunk.lines.push(`-${op.line}`);
|
|
155
|
+
hunk.oldLines++;
|
|
156
|
+
oldLine++;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
hunk.lines.push(`+${op.line}`);
|
|
160
|
+
hunk.newLines++;
|
|
161
|
+
newLine++;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
hunks.push(hunk);
|
|
165
|
+
cursor = hi + 1;
|
|
166
|
+
}
|
|
167
|
+
return hunks;
|
|
168
|
+
}
|
|
169
|
+
/** Line-level diff between the text the model already has and the text we would send now. */
|
|
170
|
+
function computeDiff(previous, next) {
|
|
171
|
+
if (previous === next)
|
|
172
|
+
return { hunks: [], added: 0, removed: 0, identical: true };
|
|
173
|
+
const oldLines = previous.split("\n");
|
|
174
|
+
const newLines = next.split("\n");
|
|
175
|
+
if (oldLines.length > MAX_DIFF_LINES || newLines.length > MAX_DIFF_LINES)
|
|
176
|
+
return null;
|
|
177
|
+
const ops = diffLines(oldLines, newLines);
|
|
178
|
+
const hunks = buildHunks(ops);
|
|
179
|
+
const added = ops.filter((o) => o.type === "ins").length;
|
|
180
|
+
const removed = ops.filter((o) => o.type === "del").length;
|
|
181
|
+
return { hunks, added, removed, identical: false };
|
|
182
|
+
}
|
|
183
|
+
/** Render hunks as a unified diff body (no ---/+++ file headers; the caller supplies context). */
|
|
184
|
+
function renderDiff(diff) {
|
|
185
|
+
return diff.hunks
|
|
186
|
+
.map((h) => [`@@ -${h.oldStart},${h.oldLines} +${h.newStart},${h.newLines} @@`, ...h.lines].join("\n"))
|
|
187
|
+
.join("\n");
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Decide whether to hand the model a delta instead of the full text.
|
|
191
|
+
*
|
|
192
|
+
* Declining is the safe direction: the caller falls back to sending everything, which is
|
|
193
|
+
* always correct. So every uncertain case here resolves to `emit: false`.
|
|
194
|
+
*/
|
|
195
|
+
function shouldEmitDelta(previous, next) {
|
|
196
|
+
const empty = { emit: false, added: 0, removed: 0, hunkCount: 0 };
|
|
197
|
+
if (next.length < DELTA_MIN_CHARS) {
|
|
198
|
+
return { ...empty, reason: "full text is small enough that a diff saves nothing" };
|
|
199
|
+
}
|
|
200
|
+
const diff = computeDiff(previous, next);
|
|
201
|
+
if (!diff)
|
|
202
|
+
return { ...empty, reason: "too large to diff affordably" };
|
|
203
|
+
if (diff.identical)
|
|
204
|
+
return { ...empty, reason: "identical" };
|
|
205
|
+
if (diff.hunks.length === 0)
|
|
206
|
+
return { ...empty, reason: "no hunks" };
|
|
207
|
+
const body = renderDiff(diff);
|
|
208
|
+
const ratio = body.length / next.length;
|
|
209
|
+
if (ratio > DELTA_MAX_RATIO) {
|
|
210
|
+
return {
|
|
211
|
+
...empty,
|
|
212
|
+
added: diff.added,
|
|
213
|
+
removed: diff.removed,
|
|
214
|
+
hunkCount: diff.hunks.length,
|
|
215
|
+
reason: `diff is ${Math.round(ratio * 100)}% of the full text — not worth reconstructing`,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
emit: true,
|
|
220
|
+
body,
|
|
221
|
+
added: diff.added,
|
|
222
|
+
removed: diff.removed,
|
|
223
|
+
hunkCount: diff.hunks.length,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
//# sourceMappingURL=OutputDelta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OutputDelta.js","sourceRoot":"","sources":["../../../src/utils/OutputDelta.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAyKH,kCAYC;AAGD,gCAIC;AAmBD,0CA+BC;AA5OD;;;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,C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AI,CAAC,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"}
|