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S,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"]}