Files
ruflo/plugins/ruflo-mods/hooks/loop/transcript.ts
T
rUvandr 189aceca1d feat(learning): the verified-outcome learning loop on classic hooks, for every ruflo project (ADR-512) (#4604)
* feat(learning): run the verified-outcome learning loop on classic hooks for every ruflo project (ADR-512)

Until now only the opt-in ruflo-mods plugin recorded verified turns and showed lessons. Classic hooks
(every `ruflo init` project) now do the same. They replay the session transcript (`transcript_path`)
through the mod's own record and lesson code, bundled into .claude/helpers/learning-loop.cjs:

- route: records the turns that finished, then prints the lessons that fit the new prompt
- session-end: records the rest (PreCompact leaves the open turn open). This runs before the
  session-id dedup, which otherwise dropped SessionEnd after a PreCompact. It also starts a
  detached `ruflo hooks verified-ingest --quiet`
- one implementation: plugins/ruflo-mods/scripts/build-learning-loop-helper.mjs bundles
  hooks/loop/transcript.ts; the bundle is committed, checked in mod-guidance CI, and is a signed
  critical helper that helper-refresh installs (manifest re-signed)
- never twice: with its loop on, the mod adds `learning-loop` to RUFLO_MODS_OWNS
- opt-out: .claude-flow/config.json learningLoop.enabled=false, or RUFLO_LEARNING_LOOP=0

Tests: mods-learning-loop-classic (replay matches the mod's records, offsets, the hook-handler run
as a process, end to end classic record -> real ingest -> lesson in route output) and real-binary
contract 6 (pinned claude, mock model, no plugin). ruflo-mods 0.14.0.

Co-Authored-By: RuFlo <ruv@ruv.net>

* docs(adr): ADR-512 cites PR #4604

Co-Authored-By: RuFlo <ruv@ruv.net>

* ci: sign and exclude the classic-loop helper and real-binary test where the other helpers are (ADR-512)

smoke-helper-signing-security copies the critical helpers into its fixture; it now copies
learning-loop.cjs too. The ratchet's default run skips real-binary files, which need the
claude binary (mods-real-binary runs them).

Co-Authored-By: RuFlo <ruv@ruv.net>

* test(mods): give the built-CLI tests the time their children get

vitest's 5s default timed out mods-cli-bin on a loaded CI runner and reported only
STACK_TRACE_ERROR (the timeout error carries the stack of the it() call). Each test runs the
built CLI several times with a 120s child timeout.

Co-Authored-By: RuFlo <ruv@ruv.net>

---------

Co-authored-by: r <r@x>
2026-10-30 16:59:46 -04:00

152 lines
7.3 KiB
TypeScript

/**
* The learning loop without the mod (ADR-512). Pure.
*
* Users on classic hooks (`ruflo init`, no `ruflo mods install`) have no in-process `tool.call` event, and their
* settings carry no PostToolUse hook for Bash. What every hook does get is Claude Code's session transcript
* (`transcript_path`), which holds each tool call and its result, including a Bash command's `is_error` on a
* non-zero exit. So the classic `route` and `session-end` hooks replay the turns the transcript gained since they
* last looked through the mod's own `start`/`step`/`line`, and show lessons through the mod's own `pick`. One source
* decides an outcome and picks a lesson on both paths: the bundle `.claude/helpers/learning-loop.cjs` is built from
* this file (`plugins/ruflo-mods/scripts/build-learning-loop-helper.mjs`).
*/
import { utf8Bytes } from '../compact-snapshot/snapshot'
import { lessonBlock, parseLessons, pick } from './lessons'
import { line, start, step } from './record'
import { relativePath } from './taint'
import { classify, verdict } from './verifier'
export { appendLines, LESSONS_PATH, VERIFIED_PATH } from './record'
/** One transcript line, as far as this file reads it. */
type Entry = {
readonly type?: unknown
readonly isSidechain?: unknown
readonly isMeta?: unknown
readonly isCompactSummary?: unknown
readonly timestamp?: unknown
readonly message?: { readonly content?: unknown }
readonly toolUseResult?: unknown
}
type Block = { readonly type?: unknown; readonly text?: unknown; readonly id?: unknown; readonly name?: unknown; readonly input?: unknown; readonly tool_use_id?: unknown; readonly is_error?: unknown }
export type Replay = {
/** Record lines for the turns that finished, oldest first (unknown outcomes included: the ingest counts them). */
readonly lines: string[]
/** Bytes of `text` consumed: up to the start of the turn still open, never into an unfinished last line. */
readonly consumed: number
}
const INTERRUPTED = '[Request interrupted by user'
const blocks = (e: Entry): Block[] => (Array.isArray(e.message?.content) ? (e.message!.content as Block[]) : [])
/** The prompt text of a line the user typed, or null: tool results, meta lines, compaction summaries, interrupts and slash commands are not prompts. */
export function promptOf(e: Entry): string | null {
if (e.type !== 'user' || e.isSidechain === true || e.isMeta === true || e.isCompactSummary === true) return null
const content = e.message?.content
const text = typeof content === 'string'
? content
: Array.isArray(content) && !(content as Block[]).some(b => b.type === 'tool_result')
? (content as Block[]).filter(b => b.type === 'text' && typeof b.text === 'string').map(b => b.text as string).join('\n')
: null
if (text === null) return null
const trimmed = text.trimStart()
if (trimmed === '' || trimmed.startsWith('/') || trimmed.startsWith('<') || trimmed.startsWith(INTERRUPTED)) return null
return text
}
const time = (e: Entry, fallback: number): number => {
const t = typeof e.timestamp === 'string' ? Date.parse(e.timestamp) : NaN
return Number.isFinite(t) ? t : fallback
}
/** The mod's tool-call result shape, rebuilt from a transcript result, for `verdict`. */
function resultOf(block: Block, extra: unknown): { isError: boolean; result: unknown } {
return { isError: block.is_error === true, result: extra && typeof extra === 'object' ? extra : {} }
}
/**
* Replays the turns in `text`, the transcript read from byte `base`. A turn opens at a typed prompt and closes at the
* next one, so the last turn stays open (not consumed) unless `final` (the session ended). A last line without its
* newline is still being written and is never consumed. Lines that do not parse are skipped.
*
* @param agentFor the agent the router names for a prompt (the classic router, so records key like the mod's)
* @param root the project root, to make edited paths project-relative
*/
export function replay(text: string, opts: { base: number; session: string; root: string; final: boolean; agentFor: (prompt: string) => string; now: number }): Replay {
const lines: string[] = []
type Open = { t: ReturnType<typeof start>; aborted: boolean; last: number; at: number }
let open: Open | null = null
const calls = new Map<string, { tool: string; input: Record<string, unknown> }>()
const close = () => {
if (open && open.t.steps.length > 0) lines.push(line(open.t, open.aborted, open.last))
open = null
calls.clear()
}
const raws = text.split('\n')
// The segment after the last newline is either empty or a line still being written.
raws.pop()
let pos = 0
for (const raw of raws) {
const at = pos
pos += utf8Bytes(raw) + 1
if (!raw.trim()) continue
let e: Entry
try {
e = JSON.parse(raw) as Entry
} catch {
continue
}
if (!e || typeof e !== 'object' || e.isSidechain === true) continue
const prompt = promptOf(e)
if (prompt !== null) {
close()
const when = time(e, opts.now)
// The id is the prompt's byte position in this session's transcript, so replaying the same turn twice writes
// the same id, and the ingest ledger (which remembers ids) trains it once.
const id = `vt-c${opts.session.replace(/[^A-Za-z0-9]/g, '').slice(0, 16)}-${(opts.base + at).toString(36)}`
open = { t: start(id, prompt, opts.agentFor(prompt), when, opts.session), aborted: false, last: when, at }
continue
}
const turn = open as Open | null
if (!turn) continue
turn.last = time(e, turn.last)
for (const b of blocks(e)) {
if (e.type === 'assistant' && b.type === 'tool_use' && typeof b.id === 'string' && typeof b.name === 'string') {
calls.set(b.id, { tool: b.name, input: b.input && typeof b.input === 'object' ? (b.input as Record<string, unknown>) : {} })
} else if (e.type === 'user' && b.type === 'tool_result' && typeof b.tool_use_id === 'string') {
const call = calls.get(b.tool_use_id)
if (!call) continue
calls.delete(b.tool_use_id)
const command = call.tool === 'Bash' ? call.input.command : undefined
const verifier = call.tool === 'Bash' ? classify(command) : null
const target = typeof call.input.file_path === 'string' ? call.input.file_path : typeof call.input.notebook_path === 'string' ? call.input.notebook_path : undefined
// A run sent to the background has no exit status yet, whatever its result line says.
const status = verifier && call.input.run_in_background !== true ? verdict(resultOf(b, e.toolUseResult)) : null
step(turn.t, call.tool, b.is_error !== true, verifier, status, target === undefined ? undefined : relativePath(opts.root, target), command)
} else if (e.type === 'user' && b.type === 'text' && typeof b.text === 'string' && b.text.startsWith(INTERRUPTED)) {
turn.aborted = true
}
}
}
const still = open as Open | null
if (opts.final || still === null) {
close()
return { lines, consumed: pos }
}
return { lines, consumed: still.at }
}
/** The lesson block for a prompt from the text of `learned-lessons.json`, or '' (none fits, or the file is bad). */
export function lessonsFor(prompt: string, lessonsText: string): string {
try {
return lessonBlock(pick(parseLessons(lessonsText), prompt)) ?? ''
} catch {
return ''
}
}