@kolisachint/hoocode-agent 0.5.18 → 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 +177 -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.map +1 -1
- package/dist/core/learn/cache.js +14 -2
- package/dist/core/learn/cache.js.map +1 -1
- 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.map +1 -1
- package/dist/core/learn/coverage.js +2 -0
- package/dist/core/learn/coverage.js.map +1 -1
- package/dist/core/learn/digest.d.ts +12 -0
- package/dist/core/learn/digest.d.ts.map +1 -1
- package/dist/core/learn/digest.js +86 -14
- package/dist/core/learn/digest.js.map +1 -1
- package/dist/core/learn/extract.d.ts +39 -4
- package/dist/core/learn/extract.d.ts.map +1 -1
- package/dist/core/learn/extract.js +170 -34
- package/dist/core/learn/extract.js.map +1 -1
- package/dist/core/learn/mine.d.ts +78 -23
- package/dist/core/learn/mine.d.ts.map +1 -1
- package/dist/core/learn/mine.js +142 -37
- package/dist/core/learn/mine.js.map +1 -1
- package/dist/core/learn/reduce.d.ts +19 -8
- package/dist/core/learn/reduce.d.ts.map +1 -1
- package/dist/core/learn/reduce.js +69 -13
- package/dist/core/learn/reduce.js.map +1 -1
- package/dist/core/learn/state.d.ts +11 -18
- package/dist/core/learn/state.d.ts.map +1 -1
- package/dist/core/learn/state.js +23 -34
- 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 +2 -2
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +1 -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/extensions/core/learn.d.ts.map +1 -1
- package/dist/extensions/core/learn.js +128 -73
- package/dist/extensions/core/learn.js.map +1 -1
- 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/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
|
@@ -12,30 +12,33 @@
|
|
|
12
12
|
* verbatim; the budget is enforced by chunking and by a session cap the reader
|
|
13
13
|
* can see, not by a filter they cannot.
|
|
14
14
|
*
|
|
15
|
-
* What the model does *not* do is count. It
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
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.
|
|
21
27
|
*/
|
|
22
28
|
import type { AgentMessage } from "@kolisachint/hoocode-agent-core";
|
|
23
29
|
import type { Model } from "@kolisachint/hoocode-ai";
|
|
24
30
|
/** What kind of thing the model noticed. */
|
|
25
|
-
export type CandidateKind = "directive" | "fix" | "
|
|
31
|
+
export type CandidateKind = "directive" | "fix" | "request";
|
|
26
32
|
/**
|
|
27
33
|
* One occurrence, as reported by the model reading a single session.
|
|
28
34
|
*
|
|
29
|
-
*
|
|
30
|
-
* was
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* that `normalizeDirective`'s lowercase-and-strip-punctuation could never do.
|
|
35
|
+
* Deliberately unnamed. What was *meant* is only decidable against everything
|
|
36
|
+
* else that was said, and this stage sees one session, so it reports what it
|
|
37
|
+
* saw and leaves grouping to `cluster.ts`. This is also the shape that goes in
|
|
38
|
+
* the cache, which is why nothing model-specific may live on it.
|
|
34
39
|
*/
|
|
35
40
|
export interface MinedCandidate {
|
|
36
41
|
kind: CandidateKind;
|
|
37
|
-
/** Canonical slug for what was meant. The clustering key. */
|
|
38
|
-
label: string;
|
|
39
42
|
/** Verbatim text from the transcript, so the digest can quote rather than paraphrase. */
|
|
40
43
|
text: string;
|
|
41
44
|
/** Why this is durable, in the model's words. Shown when a proposal is borderline. */
|
|
@@ -48,8 +51,11 @@ export interface MinedCandidate {
|
|
|
48
51
|
interveningCommands?: string[];
|
|
49
52
|
/** Files changed as part of the fix. */
|
|
50
53
|
editedFiles?: string[];
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
}
|
|
55
|
+
/** A candidate once the global naming pass has decided what to call it. */
|
|
56
|
+
export interface LabelledCandidate extends MinedCandidate {
|
|
57
|
+
/** Canonical slug for what was meant. The clustering key. */
|
|
58
|
+
label: string;
|
|
53
59
|
}
|
|
54
60
|
/** A session reduced to what the miner needs: identity, time, and rendered text. */
|
|
55
61
|
export interface MinableSession {
|
|
@@ -79,17 +85,60 @@ export declare function chunkCharsForModel(model: Pick<Model<any>, "contextWindo
|
|
|
79
85
|
* every proposal would compound its own count.
|
|
80
86
|
*/
|
|
81
87
|
export declare const LEARN_DIGEST_MARKER = "[learn-digest]";
|
|
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 declare function replayFingerprints(templates: Array<{
|
|
107
|
+
content: string;
|
|
108
|
+
}>): string[];
|
|
109
|
+
/** True when a user turn is the body of a slash command rather than something typed. */
|
|
110
|
+
export declare function isReplayedTurn(text: string, fingerprints: string[]): boolean;
|
|
82
111
|
/**
|
|
83
112
|
* Render a session as plain text for the model.
|
|
84
113
|
*
|
|
85
|
-
* User turns go in whole and unfiltered —
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
114
|
+
* User turns the user actually typed go in whole and unfiltered — any
|
|
115
|
+
* truncation there would quietly reintroduce the recall problem the old regex
|
|
116
|
+
* gate had. What does not go in is text the user's tooling replayed: its own
|
|
117
|
+
* past digests, and slash-command bodies.
|
|
118
|
+
*
|
|
119
|
+
* Assistant prose is dropped: it is the bulk of a transcript and almost none of
|
|
120
|
+
* it is evidence about what the *user* wants. Tool calls are kept, because a
|
|
121
|
+
* failure-then-pass is a fix. Successful tool output is dropped: it is a file
|
|
122
|
+
* or a command's stdout, not a statement by anyone, and feeding it to a miner
|
|
123
|
+
* looking for directives yields lines lifted out of plan files and configs
|
|
124
|
+
* attributed to the user.
|
|
125
|
+
*/
|
|
126
|
+
export declare function renderTranscript(session: MinableSession, fingerprints?: string[]): string;
|
|
127
|
+
/** Everything the user actually said in a session, normalized, for checking quotes against. */
|
|
128
|
+
export declare function spokenText(session: MinableSession, fingerprints?: string[]): string;
|
|
129
|
+
/**
|
|
130
|
+
* Drop candidates whose quote cannot be found in what the user said.
|
|
131
|
+
*
|
|
132
|
+
* The miner is told to quote verbatim and the digest renders every quote inside
|
|
133
|
+
* quotation marks, but on a real corpus a third of them appear nowhere in the
|
|
134
|
+
* session: paraphrases, merged sentences, and lines lifted out of tool output.
|
|
135
|
+
* A quote that cannot be located is evidence that cannot be shown, and a
|
|
136
|
+
* proposal the reader cannot check is worse than one that was never made.
|
|
137
|
+
*
|
|
138
|
+
* Whitespace is normalized before comparing, because a directive written in a
|
|
139
|
+
* markdown file arrives wrapped across lines and the model unwraps it.
|
|
91
140
|
*/
|
|
92
|
-
export declare function
|
|
141
|
+
export declare function verifyCandidates(candidates: MinedCandidate[], spoken: string): MinedCandidate[];
|
|
93
142
|
/**
|
|
94
143
|
* Split rendered text on line boundaries, so a chunk never cuts a user turn in
|
|
95
144
|
* half. A single turn longer than the budget gets its own oversized chunk
|
|
@@ -110,6 +159,12 @@ export interface MinerDeps {
|
|
|
110
159
|
model: Model<any>;
|
|
111
160
|
apiKey?: string;
|
|
112
161
|
headers?: Record<string, string>;
|
|
162
|
+
/**
|
|
163
|
+
* Literal runs from the slash-command bodies in force, from
|
|
164
|
+
* `replayFingerprints`. User turns matching one are machinery replaying
|
|
165
|
+
* itself, not the user speaking.
|
|
166
|
+
*/
|
|
167
|
+
replayFingerprints?: string[];
|
|
113
168
|
}
|
|
114
169
|
/**
|
|
115
170
|
* Build the real miner: one model call per chunk of one session.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mine.d.ts","sourceRoot":"","sources":["../../../src/core/learn/mine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AACpE,OAAO,KAAK,EAAE,KAAK,EAAyB,MAAM,yBAAyB,CAAC;AAG5E,4CAA4C;AAC5C,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,KAAK,GAAG,UAAU,CAAC;AAE7D;;;;;;;;GAQG;AACH,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,aAAa,CAAC;IACpB,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,yFAAyF;IACzF,IAAI,EAAE,MAAM,CAAC;IACb,sFAAsF;IACtF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iDAAiD;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,sDAAsD;IACtD,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,wCAAwC;IACxC,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,gDAAgD;IAChD,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,oFAAoF;AACpF,MAAM,WAAW,cAAc;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,YAAY,CAAA;KAAE,CAAC,CAAC;CACzD;AAED;;;GAGG;AACH,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,EAAE,cAAc,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;AA4BjG;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,eAAe,CAAC,GAAG,MAAM,CAKnF;AAYD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,mBAAmB,CAAC;AAkCpD;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,cAAc,GAAG,MAAM,CAiChE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,SAAuB,GAAG,MAAM,EAAE,CAiBzF;AAiCD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,EAAE,CA4ClE;AAED,MAAM,WAAW,SAAS;IACzB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,GAAG,KAAK,CA+BrD","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 is count. It reports occurrences one session at\n * a time, and each one carries a `label` — its own normalization of what was\n * meant. Counting identical labels across sessions is arithmetic, and it stays\n * in code (see `reduce.ts`), for two reasons: models do not count reliably over\n * long contexts, and a session mined in isolation cannot see recurrence anyway.\n * Semantic grouping is the model's job; the number is not.\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\" | \"workflow\";\n\n/**\n * One occurrence, as reported by the model reading a single session.\n *\n * `label` is the load-bearing field. It is the model's canonical name for what\n * was meant — \"use-bun-not-npm\" for all of \"we're on bun now\", \"stop using\n * npm\", and \"pnpm isn't what we use\" — and it is what the reduce step groups\n * on. Getting a stable label out of the model is what buys semantic clustering\n * that `normalizeDirective`'s lowercase-and-strip-punctuation could never do.\n */\nexport interface MinedCandidate {\n\tkind: CandidateKind;\n\t/** Canonical slug for what was meant. The clustering key. */\n\tlabel: string;\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\t/** Tool sequence, for `workflow` candidates. */\n\tsteps?: 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/** Tool output kept per call. Errors carry the signal; success output is mostly noise. */\nconst TOOL_OUTPUT_CHARS = 600;\nconst TOOL_ERROR_CHARS = 1_500;\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\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 go in whole and unfiltered — that is the entire point of this\n * rewrite, and any truncation here would quietly reintroduce the recall problem\n * the regex gate had. Assistant prose is dropped: it is the bulk of a\n * transcript and almost none of it is evidence about what the *user* wants.\n * Tool calls are kept because a repeated sequence is a workflow and a\n * failure-then-pass is a fix, and both are things worth proposing.\n */\nexport function renderTranscript(session: MinableSession): 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\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\") {\n\t\t\tconst output = textOf(message.content);\n\t\t\tif (!output) continue;\n\t\t\tconst limit = message.isError ? TOOL_ERROR_CHARS : TOOL_OUTPUT_CHARS;\n\t\t\tconst label = message.isError ? \"ERROR\" : \"RESULT\";\n\t\t\tlines.push(`${label}: ${output.length > limit ? `${output.slice(0, limit)}…` : output}`);\n\t\t}\n\t}\n\n\treturn lines.join(\"\\n\");\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\"\nDo NOT report task requests (\"add a button to the header\", \"fix the login bug\"). A task is what to do now; a directive is how things should be done in general. When a message contains both, report only the directive part.\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**workflow** — a sequence of three or more tool calls that recurs within this session, or that clearly represents a routine procedure (scaffold a file, then register it, then test it).\n\nFor every item, produce a \"label\": a short kebab-case slug naming what was MEANT, not what was said. The label is how occurrences are grouped across sessions, so two different phrasings of the same underlying point MUST get the same label.\n- \"we're on bun now\" → use-bun-not-npm\n- \"stop using npm install\" → use-bun-not-npm\n- \"pnpm isn't what we use here\" → use-bun-not-npm\nKeep labels general enough to collide when they mean the same thing, specific enough not to collide when they do not. Prefer 2-5 words.\n\nOutput STRICT JSON, no markdown fence, no prose:\n{\"candidates\":[{\"kind\":\"directive\",\"label\":\"use-bun-not-npm\",\"text\":\"<verbatim quote>\",\"rationale\":\"<one clause on why it is durable>\"}]}\n\nFor fix items add: \"command\", \"errorExcerpt\", \"interveningCommands\" (array), \"editedFiles\" (array).\nFor workflow items add: \"steps\" (array of tool names in order).\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 !== \"workflow\") continue;\n\n\t\tconst label = typeof candidate.label === \"string\" ? candidate.label.trim().toLowerCase() : \"\";\n\t\tconst text = typeof candidate.text === \"string\" ? candidate.text.trim() : \"\";\n\t\t// A candidate with no label cannot be grouped, and one with no text cannot\n\t\t// be quoted back — either way there is nothing to show the reader.\n\t\tif (!label || !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\tlabel,\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\tsteps: strings(candidate.steps),\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}\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\treturn async (session, signal) => {\n\t\tconst chunks = chunkTranscript(renderTranscript(session), 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\treturn candidates;\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"mine.d.ts","sourceRoot":"","sources":["../../../src/core/learn/mine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AACpE,OAAO,KAAK,EAAE,KAAK,EAAyB,MAAM,yBAAyB,CAAC;AAG5E,4CAA4C;AAC5C,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,KAAK,GAAG,SAAS,CAAC;AAE5D;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,aAAa,CAAC;IACpB,yFAAyF;IACzF,IAAI,EAAE,MAAM,CAAC;IACb,sFAAsF;IACtF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iDAAiD;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,sDAAsD;IACtD,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,wCAAwC;IACxC,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,2EAA2E;AAC3E,MAAM,WAAW,iBAAkB,SAAQ,cAAc;IACxD,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;CACd;AAED,oFAAoF;AACpF,MAAM,WAAW,cAAc;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,YAAY,CAAA;KAAE,CAAC,CAAC;CACzD;AAED;;;GAGG;AACH,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,EAAE,cAAc,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;AA4BjG;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,eAAe,CAAC,GAAG,MAAM,CAKnF;AAmBD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,mBAAmB,CAAC;AAOpD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,KAAK,CAAC;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,MAAM,EAAE,CAclF;AAED,wFAAwF;AACxF,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,OAAO,CAK5E;AAkCD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,cAAc,EAAE,YAAY,GAAE,MAAM,EAAO,GAAG,MAAM,CAgC7F;AAED,+FAA+F;AAC/F,wBAAgB,UAAU,CAAC,OAAO,EAAE,cAAc,EAAE,YAAY,GAAE,MAAM,EAAO,GAAG,MAAM,CAWvF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,cAAc,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,EAAE,CAU/F;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,SAAuB,GAAG,MAAM,EAAE,CAiBzF;AAgCD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,EAAE,CAwClE;AAED,MAAM,WAAW,SAAS;IACzB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC9B;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,GAAG,KAAK,CAmCrD","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"]}
|
package/dist/core/learn/mine.js
CHANGED
|
@@ -12,12 +12,18 @@
|
|
|
12
12
|
* verbatim; the budget is enforced by chunking and by a session cap the reader
|
|
13
13
|
* can see, not by a filter they cannot.
|
|
14
14
|
*
|
|
15
|
-
* What the model does *not* do is count. It
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
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.
|
|
21
27
|
*/
|
|
22
28
|
import { completeSimple } from "@kolisachint/hoocode-ai";
|
|
23
29
|
/**
|
|
@@ -56,9 +62,15 @@ export function chunkCharsForModel(model) {
|
|
|
56
62
|
const budgetTokens = window * CHUNK_CONTEXT_FRACTION - MAX_RESPONSE_TOKENS;
|
|
57
63
|
return Math.max(MIN_CHUNK_CHARS, Math.floor(budgetTokens * CHARS_PER_TOKEN));
|
|
58
64
|
}
|
|
59
|
-
/**
|
|
60
|
-
const TOOL_OUTPUT_CHARS = 600;
|
|
65
|
+
/** Error output kept per call. Errors carry the signal; success output is dropped entirely. */
|
|
61
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;
|
|
62
74
|
/** Response ceiling per chunk. A chunk yielding more than this is noise, not signal. */
|
|
63
75
|
const MAX_RESPONSE_TOKENS = 4_000;
|
|
64
76
|
/** Candidates accepted from a single chunk, as a guard against a runaway response. */
|
|
@@ -69,6 +81,54 @@ const MAX_CANDIDATES_PER_CHUNK = 40;
|
|
|
69
81
|
* every proposal would compound its own count.
|
|
70
82
|
*/
|
|
71
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
|
+
}
|
|
72
132
|
function textOf(content) {
|
|
73
133
|
if (typeof content === "string")
|
|
74
134
|
return content;
|
|
@@ -103,14 +163,19 @@ function renderArgs(args) {
|
|
|
103
163
|
/**
|
|
104
164
|
* Render a session as plain text for the model.
|
|
105
165
|
*
|
|
106
|
-
* User turns go in whole and unfiltered —
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
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.
|
|
112
177
|
*/
|
|
113
|
-
export function renderTranscript(session) {
|
|
178
|
+
export function renderTranscript(session, fingerprints = []) {
|
|
114
179
|
const lines = [];
|
|
115
180
|
for (const entry of session.entries) {
|
|
116
181
|
const message = entry.type === "message" ? entry.message : undefined;
|
|
@@ -121,6 +186,8 @@ export function renderTranscript(session) {
|
|
|
121
186
|
// Skip the command's own past output, or proposals compound their counts.
|
|
122
187
|
if (!text || text.startsWith(LEARN_DIGEST_MARKER))
|
|
123
188
|
continue;
|
|
189
|
+
if (isReplayedTurn(text, fingerprints))
|
|
190
|
+
continue;
|
|
124
191
|
lines.push(`USER: ${text}`);
|
|
125
192
|
continue;
|
|
126
193
|
}
|
|
@@ -132,17 +199,56 @@ export function renderTranscript(session) {
|
|
|
132
199
|
}
|
|
133
200
|
continue;
|
|
134
201
|
}
|
|
135
|
-
if (message.role === "toolResult") {
|
|
202
|
+
if (message.role === "toolResult" && message.isError) {
|
|
136
203
|
const output = textOf(message.content);
|
|
137
204
|
if (!output)
|
|
138
205
|
continue;
|
|
139
|
-
|
|
140
|
-
const label = message.isError ? "ERROR" : "RESULT";
|
|
141
|
-
lines.push(`${label}: ${output.length > limit ? `${output.slice(0, limit)}…` : output}`);
|
|
206
|
+
lines.push(`ERROR: ${output.length > TOOL_ERROR_CHARS ? `${output.slice(0, TOOL_ERROR_CHARS)}…` : output}`);
|
|
142
207
|
}
|
|
143
208
|
}
|
|
144
209
|
return lines.join("\n");
|
|
145
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
|
+
}
|
|
146
252
|
/**
|
|
147
253
|
* Split rendered text on line boundaries, so a chunk never cuts a user turn in
|
|
148
254
|
* half. A single turn longer than the budget gets its own oversized chunk
|
|
@@ -179,23 +285,22 @@ Report three kinds of thing.
|
|
|
179
285
|
- corrective: "no, that's not how our error handling works"
|
|
180
286
|
- declarative: "we're on bun now", "the API returns snake_case"
|
|
181
287
|
- preference stated once, in passing: "I'd rather see this as a table"
|
|
182
|
-
|
|
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.
|
|
183
289
|
|
|
184
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.
|
|
185
291
|
|
|
186
|
-
**
|
|
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.
|
|
187
297
|
|
|
188
|
-
|
|
189
|
-
- "we're on bun now" → use-bun-not-npm
|
|
190
|
-
- "stop using npm install" → use-bun-not-npm
|
|
191
|
-
- "pnpm isn't what we use here" → use-bun-not-npm
|
|
192
|
-
Keep labels general enough to collide when they mean the same thing, specific enough not to collide when they do not. Prefer 2-5 words.
|
|
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.
|
|
193
299
|
|
|
194
300
|
Output STRICT JSON, no markdown fence, no prose:
|
|
195
|
-
{"candidates":[{"kind":"directive","
|
|
301
|
+
{"candidates":[{"kind":"directive","text":"<verbatim quote>","rationale":"<one clause on why it is durable>"}]}
|
|
196
302
|
|
|
197
303
|
For fix items add: "command", "errorExcerpt", "interveningCommands" (array), "editedFiles" (array).
|
|
198
|
-
For workflow items add: "steps" (array of tool names in order).
|
|
199
304
|
|
|
200
305
|
Report nothing rather than padding. An empty list is a correct answer for a session that taught nothing: {"candidates":[]}`;
|
|
201
306
|
/**
|
|
@@ -227,25 +332,21 @@ export function parseCandidates(response) {
|
|
|
227
332
|
continue;
|
|
228
333
|
const candidate = item;
|
|
229
334
|
const kind = candidate.kind;
|
|
230
|
-
if (kind !== "directive" && kind !== "fix" && kind !== "
|
|
335
|
+
if (kind !== "directive" && kind !== "fix" && kind !== "request")
|
|
231
336
|
continue;
|
|
232
|
-
const label = typeof candidate.label === "string" ? candidate.label.trim().toLowerCase() : "";
|
|
233
337
|
const text = typeof candidate.text === "string" ? candidate.text.trim() : "";
|
|
234
|
-
//
|
|
235
|
-
|
|
236
|
-
if (!label || !text)
|
|
338
|
+
// Nothing to quote back means nothing to show the reader.
|
|
339
|
+
if (!text)
|
|
237
340
|
continue;
|
|
238
341
|
const strings = (value) => Array.isArray(value) ? value.filter((v) => typeof v === "string").slice(0, 12) : undefined;
|
|
239
342
|
out.push({
|
|
240
343
|
kind,
|
|
241
|
-
label,
|
|
242
344
|
text,
|
|
243
345
|
rationale: typeof candidate.rationale === "string" ? candidate.rationale.trim() : undefined,
|
|
244
346
|
command: typeof candidate.command === "string" ? candidate.command : undefined,
|
|
245
347
|
errorExcerpt: typeof candidate.errorExcerpt === "string" ? candidate.errorExcerpt.slice(0, 400) : undefined,
|
|
246
348
|
interveningCommands: strings(candidate.interveningCommands),
|
|
247
349
|
editedFiles: strings(candidate.editedFiles),
|
|
248
|
-
steps: strings(candidate.steps),
|
|
249
350
|
});
|
|
250
351
|
}
|
|
251
352
|
return out;
|
|
@@ -260,8 +361,9 @@ export function parseCandidates(response) {
|
|
|
260
361
|
*/
|
|
261
362
|
export function createLlmMiner(deps) {
|
|
262
363
|
const chunkChars = chunkCharsForModel(deps.model);
|
|
364
|
+
const fingerprints = deps.replayFingerprints ?? [];
|
|
263
365
|
return async (session, signal) => {
|
|
264
|
-
const chunks = chunkTranscript(renderTranscript(session), chunkChars);
|
|
366
|
+
const chunks = chunkTranscript(renderTranscript(session, fingerprints), chunkChars);
|
|
265
367
|
const candidates = [];
|
|
266
368
|
for (const chunk of chunks) {
|
|
267
369
|
if (signal?.aborted)
|
|
@@ -279,7 +381,10 @@ export function createLlmMiner(deps) {
|
|
|
279
381
|
.join("\n");
|
|
280
382
|
candidates.push(...parseCandidates(text));
|
|
281
383
|
}
|
|
282
|
-
|
|
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));
|
|
283
388
|
};
|
|
284
389
|
}
|
|
285
390
|
//# sourceMappingURL=mine.js.map
|