@kolisachint/hoocode-agent 0.5.17 → 0.5.19
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/CHANGELOG.md +247 -0
- package/dist/core/learn/audit.d.ts +136 -0
- package/dist/core/learn/audit.d.ts.map +1 -0
- package/dist/core/learn/audit.js +316 -0
- package/dist/core/learn/audit.js.map +1 -0
- package/dist/core/learn/cache.d.ts +58 -0
- package/dist/core/learn/cache.d.ts.map +1 -0
- package/dist/core/learn/cache.js +132 -0
- package/dist/core/learn/cache.js.map +1 -0
- package/dist/core/learn/cluster.d.ts +78 -0
- package/dist/core/learn/cluster.d.ts.map +1 -0
- package/dist/core/learn/cluster.js +184 -0
- package/dist/core/learn/cluster.js.map +1 -0
- package/dist/core/learn/coverage.d.ts +58 -0
- package/dist/core/learn/coverage.d.ts.map +1 -0
- package/dist/core/learn/coverage.js +144 -0
- package/dist/core/learn/coverage.js.map +1 -0
- package/dist/core/learn/digest.d.ts +13 -0
- package/dist/core/learn/digest.d.ts.map +1 -1
- package/dist/core/learn/digest.js +113 -14
- package/dist/core/learn/digest.js.map +1 -1
- package/dist/core/learn/extract.d.ts +108 -105
- package/dist/core/learn/extract.d.ts.map +1 -1
- package/dist/core/learn/extract.js +308 -447
- package/dist/core/learn/extract.js.map +1 -1
- package/dist/core/learn/mine.d.ts +178 -0
- package/dist/core/learn/mine.d.ts.map +1 -0
- package/dist/core/learn/mine.js +390 -0
- package/dist/core/learn/mine.js.map +1 -0
- package/dist/core/learn/reduce.d.ts +89 -0
- package/dist/core/learn/reduce.d.ts.map +1 -0
- package/dist/core/learn/reduce.js +179 -0
- package/dist/core/learn/reduce.js.map +1 -0
- package/dist/core/learn/state.d.ts +19 -18
- package/dist/core/learn/state.d.ts.map +1 -1
- package/dist/core/learn/state.js +35 -31
- package/dist/core/learn/state.js.map +1 -1
- package/dist/core/settings-defaults.d.ts +1 -1
- package/dist/core/settings-defaults.d.ts.map +1 -1
- package/dist/core/settings-defaults.js +1 -1
- package/dist/core/settings-defaults.js.map +1 -1
- package/dist/core/settings-manager.d.ts +4 -2
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +5 -1
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/settings-types.d.ts +1 -1
- package/dist/core/settings-types.d.ts.map +1 -1
- package/dist/core/settings-types.js.map +1 -1
- package/dist/core/startup-progress.d.ts +12 -7
- package/dist/core/startup-progress.d.ts.map +1 -1
- package/dist/core/startup-progress.js +12 -7
- package/dist/core/startup-progress.js.map +1 -1
- package/dist/extensions/core/learn.d.ts +8 -4
- package/dist/extensions/core/learn.d.ts.map +1 -1
- package/dist/extensions/core/learn.js +292 -56
- package/dist/extensions/core/learn.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +7 -25
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/progress-bar.d.ts +50 -0
- package/dist/modes/interactive/components/progress-bar.d.ts.map +1 -0
- package/dist/modes/interactive/components/progress-bar.js +77 -0
- package/dist/modes/interactive/components/progress-bar.js.map +1 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +1 -1
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +1 -1
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/voice/voice-panel.d.ts +6 -1
- package/dist/modes/interactive/voice/voice-panel.d.ts.map +1 -1
- package/dist/modes/interactive/voice/voice-panel.js +18 -14
- package/dist/modes/interactive/voice/voice-panel.js.map +1 -1
- package/docs/settings.md +9 -6
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package.json +1 -1
- package/package.json +4 -4
- package/dist/core/learn/normalize.d.ts +0 -65
- package/dist/core/learn/normalize.d.ts.map +0 -1
- package/dist/core/learn/normalize.js +0 -245
- package/dist/core/learn/normalize.js.map +0 -1
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The map half of `/learn`: a model reads one session transcript and says what
|
|
3
|
+
* it saw.
|
|
4
|
+
*
|
|
5
|
+
* This replaces the regex gate that used to decide which user turns were worth
|
|
6
|
+
* looking at. That gate was a whitelist of imperative words, so a directive
|
|
7
|
+
* phrased any other way — "we're on bun now", "that's not how our error
|
|
8
|
+
* handling works" — was not ranked low, it was invisible. Recall was traded for
|
|
9
|
+
* a token budget, silently and unrecoverably.
|
|
10
|
+
*
|
|
11
|
+
* The trade here is explicit instead. Every user turn goes to the model
|
|
12
|
+
* verbatim; the budget is enforced by chunking and by a session cap the reader
|
|
13
|
+
* can see, not by a filter they cannot.
|
|
14
|
+
*
|
|
15
|
+
* What the model does *not* do here is name or count. It used to emit a label
|
|
16
|
+
* per occurrence — its own canonical name for what was meant — and the reduce
|
|
17
|
+
* step grouped on exact label equality. That cannot work from inside one
|
|
18
|
+
* session: the model is asked to hit a shared vocabulary it has never seen, and
|
|
19
|
+
* on a real corpus it agreed with itself 3 times out of 188. Naming now happens
|
|
20
|
+
* once, globally, in `cluster.ts`, where every candidate is visible at the same
|
|
21
|
+
* time. Counting stays in `reduce.ts`, where it always belonged.
|
|
22
|
+
*
|
|
23
|
+
* Leaving labels out also makes the cache model-independent. A cached candidate
|
|
24
|
+
* used to carry a label frozen at mining time, so changing the `fast` tier
|
|
25
|
+
* forked the vocabulary permanently: old sessions and new ones named the same
|
|
26
|
+
* thing differently, and neither side reached the repeat threshold.
|
|
27
|
+
*/
|
|
28
|
+
import { completeSimple } from "@kolisachint/hoocode-ai";
|
|
29
|
+
/**
|
|
30
|
+
* Chunking exists to fit a session into a context window, so it is sized from
|
|
31
|
+
* the window rather than from a fixed guess.
|
|
32
|
+
*
|
|
33
|
+
* The guess was costing calls. Rendering already strips assistant prose and
|
|
34
|
+
* truncates tool output, which compresses the two real transcripts in this repo
|
|
35
|
+
* from 0.93 MB and 2.26 MB down to 183 KB and 266 KB — about 47k and 68k
|
|
36
|
+
* tokens. A fixed 120k-character chunk cut those into two and three pieces for
|
|
37
|
+
* no reason: on any model with a 200k window each is comfortably one call.
|
|
38
|
+
*
|
|
39
|
+
* One call per session is also better than a cheaper-looking alternative. A
|
|
40
|
+
* chunk boundary is a blind spot — a failure and the fix that resolved it can
|
|
41
|
+
* land on opposite sides of one — so the fewer boundaries inside a session, the
|
|
42
|
+
* more the model can actually see.
|
|
43
|
+
*/
|
|
44
|
+
const CHUNK_CONTEXT_FRACTION = 0.6;
|
|
45
|
+
/** Rough bytes per token. Deliberately conservative; a wrong guess here costs a wasted call. */
|
|
46
|
+
const CHARS_PER_TOKEN = 4;
|
|
47
|
+
/** Used when a model does not report a usable window. */
|
|
48
|
+
const FALLBACK_CHUNK_CHARS = 120_000;
|
|
49
|
+
/** Never chunk below this, or a small window would shred a transcript into noise. */
|
|
50
|
+
const MIN_CHUNK_CHARS = 40_000;
|
|
51
|
+
/**
|
|
52
|
+
* How much rendered transcript to send per call, given the reading model.
|
|
53
|
+
*
|
|
54
|
+
* Only a fraction of the window is used: the instructions, the response, and
|
|
55
|
+
* tokenizer variance all have to fit alongside, and overshooting costs a
|
|
56
|
+
* context-overflow error rather than a slightly worse answer.
|
|
57
|
+
*/
|
|
58
|
+
export function chunkCharsForModel(model) {
|
|
59
|
+
const window = model.contextWindow;
|
|
60
|
+
if (!Number.isFinite(window) || window <= 0)
|
|
61
|
+
return FALLBACK_CHUNK_CHARS;
|
|
62
|
+
const budgetTokens = window * CHUNK_CONTEXT_FRACTION - MAX_RESPONSE_TOKENS;
|
|
63
|
+
return Math.max(MIN_CHUNK_CHARS, Math.floor(budgetTokens * CHARS_PER_TOKEN));
|
|
64
|
+
}
|
|
65
|
+
/** Error output kept per call. Errors carry the signal; success output is dropped entirely. */
|
|
66
|
+
const TOOL_ERROR_CHARS = 1_500;
|
|
67
|
+
/**
|
|
68
|
+
* Shortest literal run of a slash-command body that identifies a replay.
|
|
69
|
+
*
|
|
70
|
+
* Long enough that a user cannot type it by accident, short enough to survive a
|
|
71
|
+
* template whose placeholders are densely packed.
|
|
72
|
+
*/
|
|
73
|
+
const REPLAY_FINGERPRINT_CHARS = 40;
|
|
74
|
+
/** Response ceiling per chunk. A chunk yielding more than this is noise, not signal. */
|
|
75
|
+
const MAX_RESPONSE_TOKENS = 4_000;
|
|
76
|
+
/** Candidates accepted from a single chunk, as a guard against a runaway response. */
|
|
77
|
+
const MAX_CANDIDATES_PER_CHUNK = 40;
|
|
78
|
+
/**
|
|
79
|
+
* Prefix on the message `/learn` injects. Its own digest is persisted like any
|
|
80
|
+
* other user turn, so without this the next run would mine its own output and
|
|
81
|
+
* every proposal would compound its own count.
|
|
82
|
+
*/
|
|
83
|
+
export const LEARN_DIGEST_MARKER = "[learn-digest]";
|
|
84
|
+
/** Collapse whitespace so a quote survives the wrapping a markdown source imposes on it. */
|
|
85
|
+
function normalizeForMatch(text) {
|
|
86
|
+
return text.replace(/\s+/g, " ").trim().toLowerCase();
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Literal runs from slash-command bodies, used to recognise a replayed expansion.
|
|
90
|
+
*
|
|
91
|
+
* A `user`-type slash command is persisted as an ordinary user message holding
|
|
92
|
+
* the whole template body, with nothing to mark it as machinery. Read back off
|
|
93
|
+
* disk it is indistinguishable from something the user typed — and it is the
|
|
94
|
+
* most repeated text in a real corpus, because running `/pr` thirty times
|
|
95
|
+
* writes the same two thousand characters thirty times. Mining it produces
|
|
96
|
+
* directives the user never stated, at counts that look exactly like organic
|
|
97
|
+
* repetition.
|
|
98
|
+
*
|
|
99
|
+
* Detection is retroactive on purpose. A provenance flag written at turn time
|
|
100
|
+
* would be exact, but it would only help sessions recorded after it shipped,
|
|
101
|
+
* leaving the existing corpus contaminated for months. Matching against the
|
|
102
|
+
* command bodies still on disk fixes the history that already exists. The gap
|
|
103
|
+
* is a template that has since been deleted; that case wants the flag, and is
|
|
104
|
+
* the reason to add one later.
|
|
105
|
+
*/
|
|
106
|
+
export function replayFingerprints(templates) {
|
|
107
|
+
const out = [];
|
|
108
|
+
for (const template of templates) {
|
|
109
|
+
// Split on the placeholders that argument substitution rewrites, leaving the
|
|
110
|
+
// literal text that survives every expansion.
|
|
111
|
+
const segments = template.content.split(/\$(?:\d+|ARGUMENTS|\*)/);
|
|
112
|
+
let longest = "";
|
|
113
|
+
for (const segment of segments) {
|
|
114
|
+
const normalized = normalizeForMatch(segment);
|
|
115
|
+
if (normalized.length > longest.length)
|
|
116
|
+
longest = normalized;
|
|
117
|
+
}
|
|
118
|
+
if (longest.length >= REPLAY_FINGERPRINT_CHARS)
|
|
119
|
+
out.push(longest);
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
/** True when a user turn is the body of a slash command rather than something typed. */
|
|
124
|
+
export function isReplayedTurn(text, fingerprints) {
|
|
125
|
+
if (fingerprints.length === 0)
|
|
126
|
+
return false;
|
|
127
|
+
const normalized = normalizeForMatch(text);
|
|
128
|
+
if (normalized.length < REPLAY_FINGERPRINT_CHARS)
|
|
129
|
+
return false;
|
|
130
|
+
return fingerprints.some((fingerprint) => normalized.includes(fingerprint));
|
|
131
|
+
}
|
|
132
|
+
function textOf(content) {
|
|
133
|
+
if (typeof content === "string")
|
|
134
|
+
return content;
|
|
135
|
+
if (!Array.isArray(content))
|
|
136
|
+
return "";
|
|
137
|
+
return content
|
|
138
|
+
.map((block) => block && typeof block === "object" && block.type === "text"
|
|
139
|
+
? (block.text ?? "")
|
|
140
|
+
: "")
|
|
141
|
+
.join("\n")
|
|
142
|
+
.trim();
|
|
143
|
+
}
|
|
144
|
+
function isToolCall(block) {
|
|
145
|
+
return !!block && typeof block === "object" && block.type === "toolCall";
|
|
146
|
+
}
|
|
147
|
+
/** Compact one tool call's arguments — enough to recognise it, not enough to flood the window. */
|
|
148
|
+
function renderArgs(args) {
|
|
149
|
+
if (!args)
|
|
150
|
+
return "";
|
|
151
|
+
const parts = [];
|
|
152
|
+
for (const [key, value] of Object.entries(args)) {
|
|
153
|
+
if (typeof value === "string") {
|
|
154
|
+
parts.push(`${key}=${value.length > 200 ? `${value.slice(0, 200)}…` : value}`);
|
|
155
|
+
}
|
|
156
|
+
else if (typeof value === "number" || typeof value === "boolean") {
|
|
157
|
+
parts.push(`${key}=${value}`);
|
|
158
|
+
}
|
|
159
|
+
// Objects and arrays are structural detail the miner does not need.
|
|
160
|
+
}
|
|
161
|
+
return parts.join(" ");
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Render a session as plain text for the model.
|
|
165
|
+
*
|
|
166
|
+
* User turns the user actually typed go in whole and unfiltered — any
|
|
167
|
+
* truncation there would quietly reintroduce the recall problem the old regex
|
|
168
|
+
* gate had. What does not go in is text the user's tooling replayed: its own
|
|
169
|
+
* past digests, and slash-command bodies.
|
|
170
|
+
*
|
|
171
|
+
* Assistant prose is dropped: it is the bulk of a transcript and almost none of
|
|
172
|
+
* it is evidence about what the *user* wants. Tool calls are kept, because a
|
|
173
|
+
* failure-then-pass is a fix. Successful tool output is dropped: it is a file
|
|
174
|
+
* or a command's stdout, not a statement by anyone, and feeding it to a miner
|
|
175
|
+
* looking for directives yields lines lifted out of plan files and configs
|
|
176
|
+
* attributed to the user.
|
|
177
|
+
*/
|
|
178
|
+
export function renderTranscript(session, fingerprints = []) {
|
|
179
|
+
const lines = [];
|
|
180
|
+
for (const entry of session.entries) {
|
|
181
|
+
const message = entry.type === "message" ? entry.message : undefined;
|
|
182
|
+
if (!message)
|
|
183
|
+
continue;
|
|
184
|
+
if (message.role === "user") {
|
|
185
|
+
const text = textOf(message.content);
|
|
186
|
+
// Skip the command's own past output, or proposals compound their counts.
|
|
187
|
+
if (!text || text.startsWith(LEARN_DIGEST_MARKER))
|
|
188
|
+
continue;
|
|
189
|
+
if (isReplayedTurn(text, fingerprints))
|
|
190
|
+
continue;
|
|
191
|
+
lines.push(`USER: ${text}`);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (message.role === "assistant") {
|
|
195
|
+
for (const block of (message.content ?? [])) {
|
|
196
|
+
if (!isToolCall(block))
|
|
197
|
+
continue;
|
|
198
|
+
lines.push(`TOOL: ${block.name}(${renderArgs(block.arguments)})`);
|
|
199
|
+
}
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (message.role === "toolResult" && message.isError) {
|
|
203
|
+
const output = textOf(message.content);
|
|
204
|
+
if (!output)
|
|
205
|
+
continue;
|
|
206
|
+
lines.push(`ERROR: ${output.length > TOOL_ERROR_CHARS ? `${output.slice(0, TOOL_ERROR_CHARS)}…` : output}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return lines.join("\n");
|
|
210
|
+
}
|
|
211
|
+
/** Everything the user actually said in a session, normalized, for checking quotes against. */
|
|
212
|
+
export function spokenText(session, fingerprints = []) {
|
|
213
|
+
const parts = [];
|
|
214
|
+
for (const entry of session.entries) {
|
|
215
|
+
const message = entry.type === "message" ? entry.message : undefined;
|
|
216
|
+
if (!message || message.role !== "user")
|
|
217
|
+
continue;
|
|
218
|
+
const text = textOf(message.content);
|
|
219
|
+
if (!text || text.startsWith(LEARN_DIGEST_MARKER))
|
|
220
|
+
continue;
|
|
221
|
+
if (isReplayedTurn(text, fingerprints))
|
|
222
|
+
continue;
|
|
223
|
+
parts.push(text);
|
|
224
|
+
}
|
|
225
|
+
return normalizeForMatch(parts.join("\n"));
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Drop candidates whose quote cannot be found in what the user said.
|
|
229
|
+
*
|
|
230
|
+
* The miner is told to quote verbatim and the digest renders every quote inside
|
|
231
|
+
* quotation marks, but on a real corpus a third of them appear nowhere in the
|
|
232
|
+
* session: paraphrases, merged sentences, and lines lifted out of tool output.
|
|
233
|
+
* A quote that cannot be located is evidence that cannot be shown, and a
|
|
234
|
+
* proposal the reader cannot check is worse than one that was never made.
|
|
235
|
+
*
|
|
236
|
+
* Whitespace is normalized before comparing, because a directive written in a
|
|
237
|
+
* markdown file arrives wrapped across lines and the model unwraps it.
|
|
238
|
+
*/
|
|
239
|
+
export function verifyCandidates(candidates, spoken) {
|
|
240
|
+
// Normalized again rather than trusting the caller: the check is a substring
|
|
241
|
+
// test, and one un-normalized argument would silently reject everything.
|
|
242
|
+
const haystack = normalizeForMatch(spoken);
|
|
243
|
+
if (!haystack)
|
|
244
|
+
return [];
|
|
245
|
+
return candidates.filter((candidate) => {
|
|
246
|
+
// A fix is evidenced by commands and errors, not by something the user said.
|
|
247
|
+
if (candidate.kind === "fix")
|
|
248
|
+
return true;
|
|
249
|
+
return haystack.includes(normalizeForMatch(candidate.text));
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Split rendered text on line boundaries, so a chunk never cuts a user turn in
|
|
254
|
+
* half. A single turn longer than the budget gets its own oversized chunk
|
|
255
|
+
* rather than being split — losing the second half of a long directive is
|
|
256
|
+
* exactly the failure this rewrite exists to remove.
|
|
257
|
+
*/
|
|
258
|
+
export function chunkTranscript(text, chunkChars = FALLBACK_CHUNK_CHARS) {
|
|
259
|
+
if (text.length <= chunkChars)
|
|
260
|
+
return text.length > 0 ? [text] : [];
|
|
261
|
+
const chunks = [];
|
|
262
|
+
let current = [];
|
|
263
|
+
let size = 0;
|
|
264
|
+
for (const line of text.split("\n")) {
|
|
265
|
+
if (size > 0 && size + line.length + 1 > chunkChars) {
|
|
266
|
+
chunks.push(current.join("\n"));
|
|
267
|
+
current = [];
|
|
268
|
+
size = 0;
|
|
269
|
+
}
|
|
270
|
+
current.push(line);
|
|
271
|
+
size += line.length + 1;
|
|
272
|
+
}
|
|
273
|
+
if (current.length > 0)
|
|
274
|
+
chunks.push(current.join("\n"));
|
|
275
|
+
return chunks;
|
|
276
|
+
}
|
|
277
|
+
const MINER_SYSTEM_PROMPT = `You read one coding-session transcript and report durable signals in it.
|
|
278
|
+
|
|
279
|
+
You are the recall stage of a two-stage pipeline. A later stage counts how often each signal recurs ACROSS sessions and decides what is worth writing down. Your job is to notice and name, not to judge importance and not to count — you are seeing one session and cannot know what repeats.
|
|
280
|
+
|
|
281
|
+
Report three kinds of thing.
|
|
282
|
+
|
|
283
|
+
**directive** — the user stating a preference, correction, constraint, or fact about how they want work done. Include these regardless of phrasing. All of these are directives:
|
|
284
|
+
- imperative: "always run the tests before pushing"
|
|
285
|
+
- corrective: "no, that's not how our error handling works"
|
|
286
|
+
- declarative: "we're on bun now", "the API returns snake_case"
|
|
287
|
+
- preference stated once, in passing: "I'd rather see this as a table"
|
|
288
|
+
A directive is how things should be done in general. What to do right now is a **request** — see below — not a directive. When a message contains both, report the directive part here.
|
|
289
|
+
|
|
290
|
+
**fix** — a command that failed and later succeeded, where something in between was the cause. Report the failing command, a short error excerpt, and what changed in between.
|
|
291
|
+
|
|
292
|
+
**request** — the user asking for a piece of work by name: "open a release PR", "run the full check and fix what it finds", "give me a demo of X". Report the request as they phrased it. A request repeated across sessions is a slash command waiting to be written, which is why it is worth reporting even though it is not a rule.
|
|
293
|
+
|
|
294
|
+
A message can contain both a request and a directive — "open a release PR, and remember to stage only your own files" is one of each. Report both, separately.
|
|
295
|
+
|
|
296
|
+
Quote "text" VERBATIM from the transcript. Do not paraphrase, merge two sentences, or tidy the wording: a quote that cannot be found in the session is discarded, because the reader is shown it in quotation marks and has to be able to check it.
|
|
297
|
+
|
|
298
|
+
Do not name or group anything. A later stage sees every session at once and decides what counts as the same point; from inside one session you cannot know.
|
|
299
|
+
|
|
300
|
+
Output STRICT JSON, no markdown fence, no prose:
|
|
301
|
+
{"candidates":[{"kind":"directive","text":"<verbatim quote>","rationale":"<one clause on why it is durable>"}]}
|
|
302
|
+
|
|
303
|
+
For fix items add: "command", "errorExcerpt", "interveningCommands" (array), "editedFiles" (array).
|
|
304
|
+
|
|
305
|
+
Report nothing rather than padding. An empty list is a correct answer for a session that taught nothing: {"candidates":[]}`;
|
|
306
|
+
/**
|
|
307
|
+
* Pull the JSON object out of a model response.
|
|
308
|
+
*
|
|
309
|
+
* Models fence JSON even when told not to, and occasionally prepend a sentence.
|
|
310
|
+
* Scanning for the outermost braces is more forgiving than trusting the format
|
|
311
|
+
* and cheaper than a repair pass — and a chunk whose response cannot be parsed
|
|
312
|
+
* is skipped, never fatal, because one bad chunk should not lose a whole run.
|
|
313
|
+
*/
|
|
314
|
+
export function parseCandidates(response) {
|
|
315
|
+
const start = response.indexOf("{");
|
|
316
|
+
const end = response.lastIndexOf("}");
|
|
317
|
+
if (start < 0 || end <= start)
|
|
318
|
+
return [];
|
|
319
|
+
let parsed;
|
|
320
|
+
try {
|
|
321
|
+
parsed = JSON.parse(response.slice(start, end + 1));
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
return [];
|
|
325
|
+
}
|
|
326
|
+
const raw = parsed?.candidates;
|
|
327
|
+
if (!Array.isArray(raw))
|
|
328
|
+
return [];
|
|
329
|
+
const out = [];
|
|
330
|
+
for (const item of raw.slice(0, MAX_CANDIDATES_PER_CHUNK)) {
|
|
331
|
+
if (!item || typeof item !== "object")
|
|
332
|
+
continue;
|
|
333
|
+
const candidate = item;
|
|
334
|
+
const kind = candidate.kind;
|
|
335
|
+
if (kind !== "directive" && kind !== "fix" && kind !== "request")
|
|
336
|
+
continue;
|
|
337
|
+
const text = typeof candidate.text === "string" ? candidate.text.trim() : "";
|
|
338
|
+
// Nothing to quote back means nothing to show the reader.
|
|
339
|
+
if (!text)
|
|
340
|
+
continue;
|
|
341
|
+
const strings = (value) => Array.isArray(value) ? value.filter((v) => typeof v === "string").slice(0, 12) : undefined;
|
|
342
|
+
out.push({
|
|
343
|
+
kind,
|
|
344
|
+
text,
|
|
345
|
+
rationale: typeof candidate.rationale === "string" ? candidate.rationale.trim() : undefined,
|
|
346
|
+
command: typeof candidate.command === "string" ? candidate.command : undefined,
|
|
347
|
+
errorExcerpt: typeof candidate.errorExcerpt === "string" ? candidate.errorExcerpt.slice(0, 400) : undefined,
|
|
348
|
+
interveningCommands: strings(candidate.interveningCommands),
|
|
349
|
+
editedFiles: strings(candidate.editedFiles),
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
return out;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Build the real miner: one model call per chunk of one session.
|
|
356
|
+
*
|
|
357
|
+
* Chunks are mined sequentially rather than in parallel. A cold-cache run is
|
|
358
|
+
* already the expensive path, and firing every chunk of every session at once
|
|
359
|
+
* is how you trip a provider rate limit on exactly the run that has the most to
|
|
360
|
+
* do.
|
|
361
|
+
*/
|
|
362
|
+
export function createLlmMiner(deps) {
|
|
363
|
+
const chunkChars = chunkCharsForModel(deps.model);
|
|
364
|
+
const fingerprints = deps.replayFingerprints ?? [];
|
|
365
|
+
return async (session, signal) => {
|
|
366
|
+
const chunks = chunkTranscript(renderTranscript(session, fingerprints), chunkChars);
|
|
367
|
+
const candidates = [];
|
|
368
|
+
for (const chunk of chunks) {
|
|
369
|
+
if (signal?.aborted)
|
|
370
|
+
break;
|
|
371
|
+
const response = await completeSimple(deps.model, {
|
|
372
|
+
systemPrompt: MINER_SYSTEM_PROMPT,
|
|
373
|
+
messages: [{ role: "user", content: [{ type: "text", text: chunk }], timestamp: Date.now() }],
|
|
374
|
+
}, { maxTokens: MAX_RESPONSE_TOKENS, signal, apiKey: deps.apiKey, headers: deps.headers });
|
|
375
|
+
if (response.stopReason === "error") {
|
|
376
|
+
throw new Error(response.errorMessage || "miner call failed");
|
|
377
|
+
}
|
|
378
|
+
const text = response.content
|
|
379
|
+
.filter((c) => c.type === "text")
|
|
380
|
+
.map((c) => c.text)
|
|
381
|
+
.join("\n");
|
|
382
|
+
candidates.push(...parseCandidates(text));
|
|
383
|
+
}
|
|
384
|
+
// Verify against the whole session rather than the chunk that produced the
|
|
385
|
+
// candidate: a quote can legitimately straddle a chunk boundary, and a
|
|
386
|
+
// dropped-for-being-unfindable verdict has to mean unfindable anywhere.
|
|
387
|
+
return verifyCandidates(candidates, spokenText(session, fingerprints));
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
//# sourceMappingURL=mine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mine.js","sourceRoot":"","sources":["../../../src/core/learn/mine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAgDzD;;;;;;;;;;;;;;GAcG;AACH,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAEnC,gGAAgG;AAChG,MAAM,eAAe,GAAG,CAAC,CAAC;AAE1B,yDAAyD;AACzD,MAAM,oBAAoB,GAAG,OAAO,CAAC;AAErC,qFAAqF;AACrF,MAAM,eAAe,GAAG,MAAM,CAAC;AAE/B;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAwC,EAAU;IACpF,MAAM,MAAM,GAAG,KAAK,CAAC,aAAa,CAAC;IACnC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC;QAAE,OAAO,oBAAoB,CAAC;IACzE,MAAM,YAAY,GAAG,MAAM,GAAG,sBAAsB,GAAG,mBAAmB,CAAC;IAC3E,OAAO,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,eAAe,CAAC,CAAC,CAAC;AAAA,CAC7E;AAED,+FAA+F;AAC/F,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAE/B;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,EAAE,CAAC;AAEpC,wFAAwF;AACxF,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAElC,sFAAsF;AACtF,MAAM,wBAAwB,GAAG,EAAE,CAAC;AAEpC;;;;GA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AC,IAAI,CAAC,CAAC,CAAC;QAC3C,CAAC;QAED,2EAA2E;QAC3E,uEAAuE;QACvE,wEAAwE;QACxE,OAAO,gBAAgB,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC;IAAA,CACvE,CAAC;AAAA,CACF","sourcesContent":["/**\n * The map half of `/learn`: a model reads one session transcript and says what\n * it saw.\n *\n * This replaces the regex gate that used to decide which user turns were worth\n * looking at. That gate was a whitelist of imperative words, so a directive\n * phrased any other way — \"we're on bun now\", \"that's not how our error\n * handling works\" — was not ranked low, it was invisible. Recall was traded for\n * a token budget, silently and unrecoverably.\n *\n * The trade here is explicit instead. Every user turn goes to the model\n * verbatim; the budget is enforced by chunking and by a session cap the reader\n * can see, not by a filter they cannot.\n *\n * What the model does *not* do here is name or count. It used to emit a label\n * per occurrence — its own canonical name for what was meant — and the reduce\n * step grouped on exact label equality. That cannot work from inside one\n * session: the model is asked to hit a shared vocabulary it has never seen, and\n * on a real corpus it agreed with itself 3 times out of 188. Naming now happens\n * once, globally, in `cluster.ts`, where every candidate is visible at the same\n * time. Counting stays in `reduce.ts`, where it always belonged.\n *\n * Leaving labels out also makes the cache model-independent. A cached candidate\n * used to carry a label frozen at mining time, so changing the `fast` tier\n * forked the vocabulary permanently: old sessions and new ones named the same\n * thing differently, and neither side reached the repeat threshold.\n */\n\nimport type { AgentMessage } from \"@kolisachint/hoocode-agent-core\";\nimport type { Model, TextContent, ToolCall } from \"@kolisachint/hoocode-ai\";\nimport { completeSimple } from \"@kolisachint/hoocode-ai\";\n\n/** What kind of thing the model noticed. */\nexport type CandidateKind = \"directive\" | \"fix\" | \"request\";\n\n/**\n * One occurrence, as reported by the model reading a single session.\n *\n * Deliberately unnamed. What was *meant* is only decidable against everything\n * else that was said, and this stage sees one session, so it reports what it\n * saw and leaves grouping to `cluster.ts`. This is also the shape that goes in\n * the cache, which is why nothing model-specific may live on it.\n */\nexport interface MinedCandidate {\n\tkind: CandidateKind;\n\t/** Verbatim text from the transcript, so the digest can quote rather than paraphrase. */\n\ttext: string;\n\t/** Why this is durable, in the model's words. Shown when a proposal is borderline. */\n\trationale?: string;\n\t/** The failing command, for `fix` candidates. */\n\tcommand?: string;\n\t/** Short error excerpt, for `fix` candidates. */\n\terrorExcerpt?: string;\n\t/** What was done in between, for `fix` candidates. */\n\tinterveningCommands?: string[];\n\t/** Files changed as part of the fix. */\n\teditedFiles?: string[];\n}\n\n/** A candidate once the global naming pass has decided what to call it. */\nexport interface LabelledCandidate extends MinedCandidate {\n\t/** Canonical slug for what was meant. The clustering key. */\n\tlabel: string;\n}\n\n/** A session reduced to what the miner needs: identity, time, and rendered text. */\nexport interface MinableSession {\n\tid: string;\n\ttimestamp: string;\n\tentries: Array<{ type: string; message?: AgentMessage }>;\n}\n\n/**\n * Mines one session. Injectable so the reduce path can be tested without a\n * model, and so a cached result can stand in for a live call.\n */\nexport type Miner = (session: MinableSession, signal?: AbortSignal) => Promise<MinedCandidate[]>;\n\n/**\n * Chunking exists to fit a session into a context window, so it is sized from\n * the window rather than from a fixed guess.\n *\n * The guess was costing calls. Rendering already strips assistant prose and\n * truncates tool output, which compresses the two real transcripts in this repo\n * from 0.93 MB and 2.26 MB down to 183 KB and 266 KB — about 47k and 68k\n * tokens. A fixed 120k-character chunk cut those into two and three pieces for\n * no reason: on any model with a 200k window each is comfortably one call.\n *\n * One call per session is also better than a cheaper-looking alternative. A\n * chunk boundary is a blind spot — a failure and the fix that resolved it can\n * land on opposite sides of one — so the fewer boundaries inside a session, the\n * more the model can actually see.\n */\nconst CHUNK_CONTEXT_FRACTION = 0.6;\n\n/** Rough bytes per token. Deliberately conservative; a wrong guess here costs a wasted call. */\nconst CHARS_PER_TOKEN = 4;\n\n/** Used when a model does not report a usable window. */\nconst FALLBACK_CHUNK_CHARS = 120_000;\n\n/** Never chunk below this, or a small window would shred a transcript into noise. */\nconst MIN_CHUNK_CHARS = 40_000;\n\n/**\n * How much rendered transcript to send per call, given the reading model.\n *\n * Only a fraction of the window is used: the instructions, the response, and\n * tokenizer variance all have to fit alongside, and overshooting costs a\n * context-overflow error rather than a slightly worse answer.\n */\nexport function chunkCharsForModel(model: Pick<Model<any>, \"contextWindow\">): number {\n\tconst window = model.contextWindow;\n\tif (!Number.isFinite(window) || window <= 0) return FALLBACK_CHUNK_CHARS;\n\tconst budgetTokens = window * CHUNK_CONTEXT_FRACTION - MAX_RESPONSE_TOKENS;\n\treturn Math.max(MIN_CHUNK_CHARS, Math.floor(budgetTokens * CHARS_PER_TOKEN));\n}\n\n/** Error output kept per call. Errors carry the signal; success output is dropped entirely. */\nconst TOOL_ERROR_CHARS = 1_500;\n\n/**\n * Shortest literal run of a slash-command body that identifies a replay.\n *\n * Long enough that a user cannot type it by accident, short enough to survive a\n * template whose placeholders are densely packed.\n */\nconst REPLAY_FINGERPRINT_CHARS = 40;\n\n/** Response ceiling per chunk. A chunk yielding more than this is noise, not signal. */\nconst MAX_RESPONSE_TOKENS = 4_000;\n\n/** Candidates accepted from a single chunk, as a guard against a runaway response. */\nconst MAX_CANDIDATES_PER_CHUNK = 40;\n\n/**\n * Prefix on the message `/learn` injects. Its own digest is persisted like any\n * other user turn, so without this the next run would mine its own output and\n * every proposal would compound its own count.\n */\nexport const LEARN_DIGEST_MARKER = \"[learn-digest]\";\n\n/** Collapse whitespace so a quote survives the wrapping a markdown source imposes on it. */\nfunction normalizeForMatch(text: string): string {\n\treturn text.replace(/\\s+/g, \" \").trim().toLowerCase();\n}\n\n/**\n * Literal runs from slash-command bodies, used to recognise a replayed expansion.\n *\n * A `user`-type slash command is persisted as an ordinary user message holding\n * the whole template body, with nothing to mark it as machinery. Read back off\n * disk it is indistinguishable from something the user typed — and it is the\n * most repeated text in a real corpus, because running `/pr` thirty times\n * writes the same two thousand characters thirty times. Mining it produces\n * directives the user never stated, at counts that look exactly like organic\n * repetition.\n *\n * Detection is retroactive on purpose. A provenance flag written at turn time\n * would be exact, but it would only help sessions recorded after it shipped,\n * leaving the existing corpus contaminated for months. Matching against the\n * command bodies still on disk fixes the history that already exists. The gap\n * is a template that has since been deleted; that case wants the flag, and is\n * the reason to add one later.\n */\nexport function replayFingerprints(templates: Array<{ content: string }>): string[] {\n\tconst out: string[] = [];\n\tfor (const template of templates) {\n\t\t// Split on the placeholders that argument substitution rewrites, leaving the\n\t\t// literal text that survives every expansion.\n\t\tconst segments = template.content.split(/\\$(?:\\d+|ARGUMENTS|\\*)/);\n\t\tlet longest = \"\";\n\t\tfor (const segment of segments) {\n\t\t\tconst normalized = normalizeForMatch(segment);\n\t\t\tif (normalized.length > longest.length) longest = normalized;\n\t\t}\n\t\tif (longest.length >= REPLAY_FINGERPRINT_CHARS) out.push(longest);\n\t}\n\treturn out;\n}\n\n/** True when a user turn is the body of a slash command rather than something typed. */\nexport function isReplayedTurn(text: string, fingerprints: string[]): boolean {\n\tif (fingerprints.length === 0) return false;\n\tconst normalized = normalizeForMatch(text);\n\tif (normalized.length < REPLAY_FINGERPRINT_CHARS) return false;\n\treturn fingerprints.some((fingerprint) => normalized.includes(fingerprint));\n}\n\nfunction textOf(content: unknown): string {\n\tif (typeof content === \"string\") return content;\n\tif (!Array.isArray(content)) return \"\";\n\treturn content\n\t\t.map((block) =>\n\t\t\tblock && typeof block === \"object\" && (block as TextContent).type === \"text\"\n\t\t\t\t? ((block as TextContent).text ?? \"\")\n\t\t\t\t: \"\",\n\t\t)\n\t\t.join(\"\\n\")\n\t\t.trim();\n}\n\nfunction isToolCall(block: unknown): block is ToolCall {\n\treturn !!block && typeof block === \"object\" && (block as ToolCall).type === \"toolCall\";\n}\n\n/** Compact one tool call's arguments — enough to recognise it, not enough to flood the window. */\nfunction renderArgs(args: Record<string, unknown> | undefined): string {\n\tif (!args) return \"\";\n\tconst parts: string[] = [];\n\tfor (const [key, value] of Object.entries(args)) {\n\t\tif (typeof value === \"string\") {\n\t\t\tparts.push(`${key}=${value.length > 200 ? `${value.slice(0, 200)}…` : value}`);\n\t\t} else if (typeof value === \"number\" || typeof value === \"boolean\") {\n\t\t\tparts.push(`${key}=${value}`);\n\t\t}\n\t\t// Objects and arrays are structural detail the miner does not need.\n\t}\n\treturn parts.join(\" \");\n}\n\n/**\n * Render a session as plain text for the model.\n *\n * User turns the user actually typed go in whole and unfiltered — any\n * truncation there would quietly reintroduce the recall problem the old regex\n * gate had. What does not go in is text the user's tooling replayed: its own\n * past digests, and slash-command bodies.\n *\n * Assistant prose is dropped: it is the bulk of a transcript and almost none of\n * it is evidence about what the *user* wants. Tool calls are kept, because a\n * failure-then-pass is a fix. Successful tool output is dropped: it is a file\n * or a command's stdout, not a statement by anyone, and feeding it to a miner\n * looking for directives yields lines lifted out of plan files and configs\n * attributed to the user.\n */\nexport function renderTranscript(session: MinableSession, fingerprints: string[] = []): string {\n\tconst lines: string[] = [];\n\n\tfor (const entry of session.entries) {\n\t\tconst message = entry.type === \"message\" ? entry.message : undefined;\n\t\tif (!message) continue;\n\n\t\tif (message.role === \"user\") {\n\t\t\tconst text = textOf(message.content);\n\t\t\t// Skip the command's own past output, or proposals compound their counts.\n\t\t\tif (!text || text.startsWith(LEARN_DIGEST_MARKER)) continue;\n\t\t\tif (isReplayedTurn(text, fingerprints)) continue;\n\t\t\tlines.push(`USER: ${text}`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (message.role === \"assistant\") {\n\t\t\tfor (const block of (message.content ?? []) as unknown[]) {\n\t\t\t\tif (!isToolCall(block)) continue;\n\t\t\t\tlines.push(`TOOL: ${block.name}(${renderArgs(block.arguments as Record<string, unknown>)})`);\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (message.role === \"toolResult\" && message.isError) {\n\t\t\tconst output = textOf(message.content);\n\t\t\tif (!output) continue;\n\t\t\tlines.push(`ERROR: ${output.length > TOOL_ERROR_CHARS ? `${output.slice(0, TOOL_ERROR_CHARS)}…` : output}`);\n\t\t}\n\t}\n\n\treturn lines.join(\"\\n\");\n}\n\n/** Everything the user actually said in a session, normalized, for checking quotes against. */\nexport function spokenText(session: MinableSession, fingerprints: string[] = []): string {\n\tconst parts: string[] = [];\n\tfor (const entry of session.entries) {\n\t\tconst message = entry.type === \"message\" ? entry.message : undefined;\n\t\tif (!message || message.role !== \"user\") continue;\n\t\tconst text = textOf(message.content);\n\t\tif (!text || text.startsWith(LEARN_DIGEST_MARKER)) continue;\n\t\tif (isReplayedTurn(text, fingerprints)) continue;\n\t\tparts.push(text);\n\t}\n\treturn normalizeForMatch(parts.join(\"\\n\"));\n}\n\n/**\n * Drop candidates whose quote cannot be found in what the user said.\n *\n * The miner is told to quote verbatim and the digest renders every quote inside\n * quotation marks, but on a real corpus a third of them appear nowhere in the\n * session: paraphrases, merged sentences, and lines lifted out of tool output.\n * A quote that cannot be located is evidence that cannot be shown, and a\n * proposal the reader cannot check is worse than one that was never made.\n *\n * Whitespace is normalized before comparing, because a directive written in a\n * markdown file arrives wrapped across lines and the model unwraps it.\n */\nexport function verifyCandidates(candidates: MinedCandidate[], spoken: string): MinedCandidate[] {\n\t// Normalized again rather than trusting the caller: the check is a substring\n\t// test, and one un-normalized argument would silently reject everything.\n\tconst haystack = normalizeForMatch(spoken);\n\tif (!haystack) return [];\n\treturn candidates.filter((candidate) => {\n\t\t// A fix is evidenced by commands and errors, not by something the user said.\n\t\tif (candidate.kind === \"fix\") return true;\n\t\treturn haystack.includes(normalizeForMatch(candidate.text));\n\t});\n}\n\n/**\n * Split rendered text on line boundaries, so a chunk never cuts a user turn in\n * half. A single turn longer than the budget gets its own oversized chunk\n * rather than being split — losing the second half of a long directive is\n * exactly the failure this rewrite exists to remove.\n */\nexport function chunkTranscript(text: string, chunkChars = FALLBACK_CHUNK_CHARS): string[] {\n\tif (text.length <= chunkChars) return text.length > 0 ? [text] : [];\n\n\tconst chunks: string[] = [];\n\tlet current: string[] = [];\n\tlet size = 0;\n\tfor (const line of text.split(\"\\n\")) {\n\t\tif (size > 0 && size + line.length + 1 > chunkChars) {\n\t\t\tchunks.push(current.join(\"\\n\"));\n\t\t\tcurrent = [];\n\t\t\tsize = 0;\n\t\t}\n\t\tcurrent.push(line);\n\t\tsize += line.length + 1;\n\t}\n\tif (current.length > 0) chunks.push(current.join(\"\\n\"));\n\treturn chunks;\n}\n\nconst MINER_SYSTEM_PROMPT = `You read one coding-session transcript and report durable signals in it.\n\nYou are the recall stage of a two-stage pipeline. A later stage counts how often each signal recurs ACROSS sessions and decides what is worth writing down. Your job is to notice and name, not to judge importance and not to count — you are seeing one session and cannot know what repeats.\n\nReport three kinds of thing.\n\n**directive** — the user stating a preference, correction, constraint, or fact about how they want work done. Include these regardless of phrasing. All of these are directives:\n- imperative: \"always run the tests before pushing\"\n- corrective: \"no, that's not how our error handling works\"\n- declarative: \"we're on bun now\", \"the API returns snake_case\"\n- preference stated once, in passing: \"I'd rather see this as a table\"\nA directive is how things should be done in general. What to do right now is a **request** — see below — not a directive. When a message contains both, report the directive part here.\n\n**fix** — a command that failed and later succeeded, where something in between was the cause. Report the failing command, a short error excerpt, and what changed in between.\n\n**request** — the user asking for a piece of work by name: \"open a release PR\", \"run the full check and fix what it finds\", \"give me a demo of X\". Report the request as they phrased it. A request repeated across sessions is a slash command waiting to be written, which is why it is worth reporting even though it is not a rule.\n\nA message can contain both a request and a directive — \"open a release PR, and remember to stage only your own files\" is one of each. Report both, separately.\n\nQuote \"text\" VERBATIM from the transcript. Do not paraphrase, merge two sentences, or tidy the wording: a quote that cannot be found in the session is discarded, because the reader is shown it in quotation marks and has to be able to check it.\n\nDo not name or group anything. A later stage sees every session at once and decides what counts as the same point; from inside one session you cannot know.\n\nOutput STRICT JSON, no markdown fence, no prose:\n{\"candidates\":[{\"kind\":\"directive\",\"text\":\"<verbatim quote>\",\"rationale\":\"<one clause on why it is durable>\"}]}\n\nFor fix items add: \"command\", \"errorExcerpt\", \"interveningCommands\" (array), \"editedFiles\" (array).\n\nReport nothing rather than padding. An empty list is a correct answer for a session that taught nothing: {\"candidates\":[]}`;\n\n/**\n * Pull the JSON object out of a model response.\n *\n * Models fence JSON even when told not to, and occasionally prepend a sentence.\n * Scanning for the outermost braces is more forgiving than trusting the format\n * and cheaper than a repair pass — and a chunk whose response cannot be parsed\n * is skipped, never fatal, because one bad chunk should not lose a whole run.\n */\nexport function parseCandidates(response: string): MinedCandidate[] {\n\tconst start = response.indexOf(\"{\");\n\tconst end = response.lastIndexOf(\"}\");\n\tif (start < 0 || end <= start) return [];\n\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(response.slice(start, end + 1));\n\t} catch {\n\t\treturn [];\n\t}\n\n\tconst raw = (parsed as { candidates?: unknown })?.candidates;\n\tif (!Array.isArray(raw)) return [];\n\n\tconst out: MinedCandidate[] = [];\n\tfor (const item of raw.slice(0, MAX_CANDIDATES_PER_CHUNK)) {\n\t\tif (!item || typeof item !== \"object\") continue;\n\t\tconst candidate = item as Record<string, unknown>;\n\t\tconst kind = candidate.kind;\n\t\tif (kind !== \"directive\" && kind !== \"fix\" && kind !== \"request\") continue;\n\n\t\tconst text = typeof candidate.text === \"string\" ? candidate.text.trim() : \"\";\n\t\t// Nothing to quote back means nothing to show the reader.\n\t\tif (!text) continue;\n\n\t\tconst strings = (value: unknown): string[] | undefined =>\n\t\t\tArray.isArray(value) ? value.filter((v): v is string => typeof v === \"string\").slice(0, 12) : undefined;\n\n\t\tout.push({\n\t\t\tkind,\n\t\t\ttext,\n\t\t\trationale: typeof candidate.rationale === \"string\" ? candidate.rationale.trim() : undefined,\n\t\t\tcommand: typeof candidate.command === \"string\" ? candidate.command : undefined,\n\t\t\terrorExcerpt: typeof candidate.errorExcerpt === \"string\" ? candidate.errorExcerpt.slice(0, 400) : undefined,\n\t\t\tinterveningCommands: strings(candidate.interveningCommands),\n\t\t\teditedFiles: strings(candidate.editedFiles),\n\t\t});\n\t}\n\treturn out;\n}\n\nexport interface MinerDeps {\n\tmodel: Model<any>;\n\tapiKey?: string;\n\theaders?: Record<string, string>;\n\t/**\n\t * Literal runs from the slash-command bodies in force, from\n\t * `replayFingerprints`. User turns matching one are machinery replaying\n\t * itself, not the user speaking.\n\t */\n\treplayFingerprints?: string[];\n}\n\n/**\n * Build the real miner: one model call per chunk of one session.\n *\n * Chunks are mined sequentially rather than in parallel. A cold-cache run is\n * already the expensive path, and firing every chunk of every session at once\n * is how you trip a provider rate limit on exactly the run that has the most to\n * do.\n */\nexport function createLlmMiner(deps: MinerDeps): Miner {\n\tconst chunkChars = chunkCharsForModel(deps.model);\n\tconst fingerprints = deps.replayFingerprints ?? [];\n\treturn async (session, signal) => {\n\t\tconst chunks = chunkTranscript(renderTranscript(session, fingerprints), chunkChars);\n\t\tconst candidates: MinedCandidate[] = [];\n\n\t\tfor (const chunk of chunks) {\n\t\t\tif (signal?.aborted) break;\n\n\t\t\tconst response = await completeSimple(\n\t\t\t\tdeps.model,\n\t\t\t\t{\n\t\t\t\t\tsystemPrompt: MINER_SYSTEM_PROMPT,\n\t\t\t\t\tmessages: [{ role: \"user\", content: [{ type: \"text\", text: chunk }], timestamp: Date.now() }],\n\t\t\t\t},\n\t\t\t\t{ maxTokens: MAX_RESPONSE_TOKENS, signal, apiKey: deps.apiKey, headers: deps.headers },\n\t\t\t);\n\n\t\t\tif (response.stopReason === \"error\") {\n\t\t\t\tthrow new Error(response.errorMessage || \"miner call failed\");\n\t\t\t}\n\n\t\t\tconst text = response.content\n\t\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t\t.map((c) => c.text)\n\t\t\t\t.join(\"\\n\");\n\t\t\tcandidates.push(...parseCandidates(text));\n\t\t}\n\n\t\t// Verify against the whole session rather than the chunk that produced the\n\t\t// candidate: a quote can legitimately straddle a chunk boundary, and a\n\t\t// dropped-for-being-unfindable verdict has to mean unfindable anywhere.\n\t\treturn verifyCandidates(candidates, spokenText(session, fingerprints));\n\t};\n}\n"]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reduce half of `/learn`: turn per-session observations into counts.
|
|
3
|
+
*
|
|
4
|
+
* The split with `mine.ts` is the whole design. Deciding that "we're on bun
|
|
5
|
+
* now" and "stop using npm" mean the same thing is semantics, and the model is
|
|
6
|
+
* better at it than any normalizer — so the model does it, by emitting a shared
|
|
7
|
+
* `label`. Deciding that the shared label occurred nine times across five
|
|
8
|
+
* sessions is arithmetic, and arithmetic stays here, because a model asked to
|
|
9
|
+
* count over a long context will be approximately right, and an approximately
|
|
10
|
+
* right number is worse than none when the number is the thing the reader acts
|
|
11
|
+
* on.
|
|
12
|
+
*
|
|
13
|
+
* Nothing in this file filters on content. The only gate is the repeat
|
|
14
|
+
* threshold, which is a dial the user sets and the digest reports, not a
|
|
15
|
+
* whitelist they cannot see.
|
|
16
|
+
*/
|
|
17
|
+
import type { LabelledCandidate } from "./mine.js";
|
|
18
|
+
/** Where a repeated directive already lives, if anywhere. */
|
|
19
|
+
export type DirectiveStatus = "new" | "restated" | "has-skill";
|
|
20
|
+
/** Fields every proposable item shares, so suppression can be applied uniformly. */
|
|
21
|
+
export interface Proposable {
|
|
22
|
+
/** Stable identity across runs — what the state file remembers. */
|
|
23
|
+
key: string;
|
|
24
|
+
/** Newest occurrence in the window, ISO. */
|
|
25
|
+
lastSeen: string;
|
|
26
|
+
}
|
|
27
|
+
export interface DirectiveCluster extends Proposable {
|
|
28
|
+
/** The model's canonical name for what was meant. The clustering key. */
|
|
29
|
+
label: string;
|
|
30
|
+
/** Representative verbatim quote, the longest seen in the cluster. */
|
|
31
|
+
text: string;
|
|
32
|
+
/** Why it is durable, in the model's words. */
|
|
33
|
+
rationale?: string;
|
|
34
|
+
/** Total times said. */
|
|
35
|
+
count: number;
|
|
36
|
+
/** Distinct sessions it was said in — the stronger of the two counts. */
|
|
37
|
+
sessions: number;
|
|
38
|
+
status: DirectiveStatus;
|
|
39
|
+
/** The existing rule line matched, when status is `restated`. */
|
|
40
|
+
existingRule?: string;
|
|
41
|
+
/** The skill that already covers this, when status is `has-skill`. */
|
|
42
|
+
existingSkill?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Shown before and still not written down anywhere — neither as a rule nor as
|
|
45
|
+
* a skill — so the reader saw this proposal and passed on it.
|
|
46
|
+
*/
|
|
47
|
+
previouslyDeclined: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface FixCandidate extends Proposable {
|
|
50
|
+
label: string;
|
|
51
|
+
/** The failing command. */
|
|
52
|
+
command: string;
|
|
53
|
+
/** Short excerpt of the real error text. */
|
|
54
|
+
errorExcerpt: string;
|
|
55
|
+
/** Commands run between the failure and the pass. */
|
|
56
|
+
interveningCommands: string[];
|
|
57
|
+
/** Files edited between the failure and the pass. */
|
|
58
|
+
editedFiles: string[];
|
|
59
|
+
count: number;
|
|
60
|
+
sessions: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A piece of work the user keeps asking for by name.
|
|
64
|
+
*
|
|
65
|
+
* This is the slash-command signal, and it was being thrown away: the miner
|
|
66
|
+
* used to be told not to report task requests at all. But a request repeated
|
|
67
|
+
* across sessions is exactly what a slash command is for, and the evidence for
|
|
68
|
+
* it is stronger than for most rules — the same job, asked for again, in
|
|
69
|
+
* someone's own words.
|
|
70
|
+
*/
|
|
71
|
+
export interface RequestCandidate extends Proposable {
|
|
72
|
+
label: string;
|
|
73
|
+
/** How the user phrased it, so the proposal can quote rather than invent. */
|
|
74
|
+
text: string;
|
|
75
|
+
count: number;
|
|
76
|
+
sessions: number;
|
|
77
|
+
}
|
|
78
|
+
/** One session's mining output, named and tagged with the identity the counts need. */
|
|
79
|
+
export interface MinedSession {
|
|
80
|
+
sessionId: string;
|
|
81
|
+
/** When the session was last written to — the clock suppression runs on. */
|
|
82
|
+
lastActivity: string;
|
|
83
|
+
/** Named by the global clustering pass; grouping here is exact-label arithmetic. */
|
|
84
|
+
candidates: LabelledCandidate[];
|
|
85
|
+
}
|
|
86
|
+
export declare function reduceDirectives(sessions: MinedSession[], minRepeats: number): DirectiveCluster[];
|
|
87
|
+
export declare function reduceFixes(sessions: MinedSession[], minRepeats: number): FixCandidate[];
|
|
88
|
+
export declare function reduceRequests(sessions: MinedSession[], minRepeats: number): RequestCandidate[];
|
|
89
|
+
//# sourceMappingURL=reduce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reduce.d.ts","sourceRoot":"","sources":["../../../src/core/learn/reduce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAkB,MAAM,WAAW,CAAC;AAEnE,6DAA6D;AAC7D,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,UAAU,GAAG,WAAW,CAAC;AAE/D,oFAAoF;AACpF,MAAM,WAAW,UAAU;IAC1B,qEAAmE;IACnE,GAAG,EAAE,MAAM,CAAC;IACZ,4CAA4C;IAC5C,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IACnD,yEAAyE;IACzE,KAAK,EAAE,MAAM,CAAC;IACd,sEAAsE;IACtE,IAAI,EAAE,MAAM,CAAC;IACb,+CAA+C;IAC/C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wBAAwB;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,2EAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,eAAe,CAAC;IACxB,iEAAiE;IACjE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,kBAAkB,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,WAAW,YAAa,SAAQ,UAAU;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,2BAA2B;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,4CAA4C;IAC5C,YAAY,EAAE,MAAM,CAAC;IACrB,qDAAqD;IACrD,mBAAmB,EAAE,MAAM,EAAE,CAAC;IAC9B,qDAAqD;IACrD,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IACnD,KAAK,EAAE,MAAM,CAAC;IACd,6EAA6E;IAC7E,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,uFAAuF;AACvF,MAAM,WAAW,YAAY;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,8EAA4E;IAC5E,YAAY,EAAE,MAAM,CAAC;IACrB,oFAAoF;IACpF,UAAU,EAAE,iBAAiB,EAAE,CAAC;CAChC;AAgID,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,UAAU,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAiBjG;AAED,wBAAgB,WAAW,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,UAAU,EAAE,MAAM,GAAG,YAAY,EAAE,CAexF;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,UAAU,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAY/F","sourcesContent":["/**\n * The reduce half of `/learn`: turn per-session observations into counts.\n *\n * The split with `mine.ts` is the whole design. Deciding that \"we're on bun\n * now\" and \"stop using npm\" mean the same thing is semantics, and the model is\n * better at it than any normalizer — so the model does it, by emitting a shared\n * `label`. Deciding that the shared label occurred nine times across five\n * sessions is arithmetic, and arithmetic stays here, because a model asked to\n * count over a long context will be approximately right, and an approximately\n * right number is worse than none when the number is the thing the reader acts\n * on.\n *\n * Nothing in this file filters on content. The only gate is the repeat\n * threshold, which is a dial the user sets and the digest reports, not a\n * whitelist they cannot see.\n */\n\nimport type { LabelledCandidate, MinedCandidate } from \"./mine.js\";\n\n/** Where a repeated directive already lives, if anywhere. */\nexport type DirectiveStatus = \"new\" | \"restated\" | \"has-skill\";\n\n/** Fields every proposable item shares, so suppression can be applied uniformly. */\nexport interface Proposable {\n\t/** Stable identity across runs — what the state file remembers. */\n\tkey: string;\n\t/** Newest occurrence in the window, ISO. */\n\tlastSeen: string;\n}\n\nexport interface DirectiveCluster extends Proposable {\n\t/** The model's canonical name for what was meant. The clustering key. */\n\tlabel: string;\n\t/** Representative verbatim quote, the longest seen in the cluster. */\n\ttext: string;\n\t/** Why it is durable, in the model's words. */\n\trationale?: string;\n\t/** Total times said. */\n\tcount: number;\n\t/** Distinct sessions it was said in — the stronger of the two counts. */\n\tsessions: number;\n\tstatus: DirectiveStatus;\n\t/** The existing rule line matched, when status is `restated`. */\n\texistingRule?: string;\n\t/** The skill that already covers this, when status is `has-skill`. */\n\texistingSkill?: string;\n\t/**\n\t * Shown before and still not written down anywhere — neither as a rule nor as\n\t * a skill — so the reader saw this proposal and passed on it.\n\t */\n\tpreviouslyDeclined: boolean;\n}\n\nexport interface FixCandidate extends Proposable {\n\tlabel: string;\n\t/** The failing command. */\n\tcommand: string;\n\t/** Short excerpt of the real error text. */\n\terrorExcerpt: string;\n\t/** Commands run between the failure and the pass. */\n\tinterveningCommands: string[];\n\t/** Files edited between the failure and the pass. */\n\teditedFiles: string[];\n\tcount: number;\n\tsessions: number;\n}\n\n/**\n * A piece of work the user keeps asking for by name.\n *\n * This is the slash-command signal, and it was being thrown away: the miner\n * used to be told not to report task requests at all. But a request repeated\n * across sessions is exactly what a slash command is for, and the evidence for\n * it is stronger than for most rules — the same job, asked for again, in\n * someone's own words.\n */\nexport interface RequestCandidate extends Proposable {\n\tlabel: string;\n\t/** How the user phrased it, so the proposal can quote rather than invent. */\n\ttext: string;\n\tcount: number;\n\tsessions: number;\n}\n\n/** One session's mining output, named and tagged with the identity the counts need. */\nexport interface MinedSession {\n\tsessionId: string;\n\t/** When the session was last written to — the clock suppression runs on. */\n\tlastActivity: string;\n\t/** Named by the global clustering pass; grouping here is exact-label arithmetic. */\n\tcandidates: LabelledCandidate[];\n}\n\ninterface Acc {\n\tlabel: string;\n\ttext: string;\n\trationale?: string;\n\tcount: number;\n\tsessions: Set<string>;\n\tlastSeen: string;\n\tsamples: MinedCandidate[];\n}\n\n/**\n * Group every candidate of one kind by its label.\n *\n * `lastSeen` takes the session's last activity rather than anything the model\n * reports: the model is reading a transcript and has no reliable clock, and\n * `lastSeen` drives suppression, where a wrong value silently hides a live\n * signal or resurfaces a dead one. It is deliberately not the session's start\n * either — a long-running session would date today's words to the day it was\n * opened, and suppression would call them old news.\n */\nfunction groupByLabel(sessions: MinedSession[], kind: MinedCandidate[\"kind\"]): Acc[] {\n\tconst acc = new Map<string, Acc>();\n\n\tfor (const session of sessions) {\n\t\tfor (const candidate of session.candidates) {\n\t\t\tif (candidate.kind !== kind) continue;\n\t\t\tconst existing = acc.get(candidate.label);\n\t\t\tif (existing) {\n\t\t\t\texisting.count++;\n\t\t\t\texisting.sessions.add(session.sessionId);\n\t\t\t\tif (session.lastActivity > existing.lastSeen) existing.lastSeen = session.lastActivity;\n\t\t\t\t// Keep the fullest quote: a longer one carries more of the reasoning.\n\t\t\t\tif (candidate.text.length > existing.text.length) existing.text = candidate.text;\n\t\t\t\texisting.rationale ??= candidate.rationale;\n\t\t\t\texisting.samples.push(candidate);\n\t\t\t} else {\n\t\t\t\tacc.set(candidate.label, {\n\t\t\t\t\tlabel: candidate.label,\n\t\t\t\t\ttext: candidate.text,\n\t\t\t\t\trationale: candidate.rationale,\n\t\t\t\t\tcount: 1,\n\t\t\t\t\tsessions: new Set([session.sessionId]),\n\t\t\t\t\tlastSeen: session.lastActivity,\n\t\t\t\t\tsamples: [candidate],\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\treturn [...acc.values()];\n}\n\n/**\n * Merge accumulators whose representative quote is the same sentence.\n *\n * The model labels each session independently and has no way to see what it\n * called the same thing last time, so identical text routinely arrives under\n * two labels — and then reads as two separate proposals, the same words twice.\n * Matching on the quote costs nothing and is the one case where sameness is not\n * a judgement call.\n *\n * This runs before the repeat threshold on purpose: two occurrences split\n * across two labels are each below the bar, and merging them afterwards would\n * mean neither was ever considered.\n *\n * The surviving label is the lexicographically smallest of the merged set, not\n * the first one seen. First-seen follows session order, which changes whenever\n * a session is added — so the merged cluster would silently change its name\n * between runs, the state key with it, and every item the reader had already\n * decided on would come back as new. A merge has to be a pure function of what\n * was merged.\n */\nfunction mergeIdenticalText(entries: Acc[]): Acc[] {\n\tconst byText = new Map<string, Acc>();\n\tconst order: Acc[] = [];\n\n\tfor (const entry of entries) {\n\t\tconst key = entry.text.replace(/\\s+/g, \" \").trim().toLowerCase();\n\t\tconst existing = byText.get(key);\n\t\tif (!existing) {\n\t\t\tbyText.set(key, entry);\n\t\t\torder.push(entry);\n\t\t\tcontinue;\n\t\t}\n\t\texisting.count += entry.count;\n\t\tfor (const session of entry.sessions) existing.sessions.add(session);\n\t\tif (entry.lastSeen > existing.lastSeen) existing.lastSeen = entry.lastSeen;\n\t\texisting.rationale ??= entry.rationale;\n\t\texisting.samples.push(...entry.samples);\n\t\tif (entry.label < existing.label) existing.label = entry.label;\n\t}\n\n\treturn order;\n}\n\n/** Distinct sessions first, then raw count: five sessions beats nine times in one. */\nfunction byEvidence(a: { sessions: number; count: number; label: string }, b: typeof a): number {\n\treturn b.sessions - a.sessions || b.count - a.count || a.label.localeCompare(b.label);\n}\n\n/** Merge repeated string fields across a cluster's samples, preserving order and dropping dupes. */\nfunction mergeStrings(samples: MinedCandidate[], pick: (c: MinedCandidate) => string[] | undefined): string[] {\n\tconst out: string[] = [];\n\tconst seen = new Set<string>();\n\tfor (const sample of samples) {\n\t\tfor (const value of pick(sample) ?? []) {\n\t\t\tif (seen.has(value)) continue;\n\t\t\tseen.add(value);\n\t\t\tout.push(value);\n\t\t}\n\t}\n\treturn out.slice(0, 12);\n}\n\n/**\n * The repeat threshold counts distinct sessions, not occurrences.\n *\n * Saying a thing twice inside one session is the commonest thing in a\n * transcript and usually means the opposite of durable: the agent ignored it\n * the first time, so it was restated. Two *sessions* is a claim about how you\n * work; two lines in one session is a claim about one afternoon.\n */\nfunction repeatedEnough(entry: Acc, minRepeats: number): boolean {\n\treturn entry.sessions.size >= minRepeats;\n}\n\nexport function reduceDirectives(sessions: MinedSession[], minRepeats: number): DirectiveCluster[] {\n\treturn mergeIdenticalText(groupByLabel(sessions, \"directive\"))\n\t\t.filter((entry) => repeatedEnough(entry, minRepeats))\n\t\t.map((entry) => ({\n\t\t\tkey: `directive:${entry.label}`,\n\t\t\tlabel: entry.label,\n\t\t\ttext: entry.text,\n\t\t\trationale: entry.rationale,\n\t\t\tcount: entry.count,\n\t\t\tsessions: entry.sessions.size,\n\t\t\tlastSeen: entry.lastSeen,\n\t\t\t// Coverage is decided later, by a model that can tell a paraphrase from a\n\t\t\t// coincidence. Everything starts `new` and is corrected in place.\n\t\t\tstatus: \"new\" as DirectiveStatus,\n\t\t\tpreviouslyDeclined: false,\n\t\t}))\n\t\t.sort(byEvidence);\n}\n\nexport function reduceFixes(sessions: MinedSession[], minRepeats: number): FixCandidate[] {\n\treturn groupByLabel(sessions, \"fix\")\n\t\t.filter((entry) => repeatedEnough(entry, minRepeats))\n\t\t.map((entry) => ({\n\t\t\tkey: `fix:${entry.label}`,\n\t\t\tlabel: entry.label,\n\t\t\tcommand: entry.samples.find((s) => s.command)?.command ?? entry.text,\n\t\t\terrorExcerpt: entry.samples.find((s) => s.errorExcerpt)?.errorExcerpt ?? \"\",\n\t\t\tinterveningCommands: mergeStrings(entry.samples, (s) => s.interveningCommands),\n\t\t\teditedFiles: mergeStrings(entry.samples, (s) => s.editedFiles),\n\t\t\tcount: entry.count,\n\t\t\tsessions: entry.sessions.size,\n\t\t\tlastSeen: entry.lastSeen,\n\t\t}))\n\t\t.sort(byEvidence);\n}\n\nexport function reduceRequests(sessions: MinedSession[], minRepeats: number): RequestCandidate[] {\n\treturn mergeIdenticalText(groupByLabel(sessions, \"request\"))\n\t\t.filter((entry) => repeatedEnough(entry, minRepeats))\n\t\t.map((entry) => ({\n\t\t\tkey: `request:${entry.label}`,\n\t\t\tlabel: entry.label,\n\t\t\ttext: entry.text,\n\t\t\tcount: entry.count,\n\t\t\tsessions: entry.sessions.size,\n\t\t\tlastSeen: entry.lastSeen,\n\t\t}))\n\t\t.sort(byEvidence);\n}\n"]}
|