Files
ruflo/plugins/ruflo-mods/hooks/loop/lessons.ts
T
rUvandr 243bc57f0b feat(learning): lessons say where the fix goes; reworded tasks learn together (ADR-511) (#4603)
* feat(learning): lessons say where the fix goes; reworded tasks learn together (ADR-511)

The §7 evaluation (#4581) showed three limits on what the verified-outcome
loop can learn:

- a lesson named an agent, nothing the model can act on;
- tasks naming different files merged into one lesson;
- the same task worded differently never learned (15 reworded verified
  successes left 15 patterns at one success each, zero lessons).

Changes:
- mods: a verified turn records its screened edited source files; a lesson
  names the three its passing fixes edited most and leads with them; pick()
  weighs rare keywords (IDF) and drops matches far below the best.
  ruflo-mods 0.13.0.
- cli: files are counted per pattern (successes only) into learned-lessons.json;
  SONA takes them as pattern anchors (never keywords), so reworded tasks that
  fixed the same file reinforce one pattern; tasks naming disjoint paths never
  merge. Lesson building moves to verified-lessons.ts.
- eval: the harness gains --ablate, --test-index, --concurrency, --lessons,
  --save-lessons, --eval-sample, --calibrate and --max-turns;
  scripts/eval-learning-loop-corpus.mjs writes a validated 15+30-task corpus.

Proved offline (tests): 15 reworded prompts form 5 lessons, up from 0; the
first run's prompts form 5 lessons, up from 1; the right lesson ranks first
for 30 of 30 eval prompts, with 1.07 lessons shown per prompt.

Paid pilots (haiku, about $6.80) are directional and did not reach the gate:
- 8-turn cap: off 2/10, on 4/10, lessons without files 1/10;
- uncapped: 6/6 vs 6/6, cost within 1%.
ADR-511 §5 has the details.

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

* docs(adr): ADR-511 cites PR #4603

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

---------

Co-authored-by: r <r@x>
2026-10-30 12:12:42 -04:00

117 lines
6.4 KiB
TypeScript

/**
* Verified lessons for the prompt (#4581, ADR-502). Pure.
*
* `.claude-flow/data/learned-lessons.json` is written by the CLI ingest from
* SONA patterns that verified outcomes trained. It is a project file, so it
* is untrusted: every field is validated to a fixed shape, and a block that
* fails the secret or injection screen is dropped whole.
*/
import { scan } from '../guidance/screen'
import { lessonFile } from './record'
/** The SONA routing gate; a file cannot lower it. */
export const GATE = 0.6
/** Lessons added to one prompt, at most. */
export const MAX_PER_PROMPT = 3
const MAX_LESSONS = 50
/** ADR-511: files one lesson names, at most. */
export const MAX_FILES_SHOWN = 3
export type Lesson = {
readonly id: string
readonly kind: 'lesson' | 'caution'
readonly agent: string
readonly keywords: readonly string[]
readonly confidence: number
readonly successes: number
readonly failures: number
readonly evidence: string
/** ADR-511: files the passing fixes edited, most often first; empty for a caution or an older file. */
readonly files: readonly string[]
}
export type Lessons = { readonly gate: number; readonly lessons: readonly Lesson[]; readonly belowGate: number }
const WORD = /^[a-z0-9][a-z0-9 _./-]{0,40}$/
const AGENT = /^[a-z][a-z0-9-]{0,40}$/
/** The CLI cites a lesson by a hash of its SONA pattern key (`lessonId` in verified-outcomes.ts), never the key itself. */
const ID = /^L-[a-f0-9]{10}$/
const EVIDENCE = /^[A-Za-z0-9 ._:/=@+-]{1,80}$/
const count = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) && v >= 0 ? Math.min(Math.floor(v), 1_000_000) : 0)
/** The file's text, validated. Throws on JSON that is not an object (cachedFile reports it as unreadable). */
export function parseLessons(text: string): Lessons {
const raw = JSON.parse(text) as { version?: unknown; gate?: unknown; lessons?: unknown; belowGate?: unknown }
if (!raw || typeof raw !== 'object' || raw.version !== 1) throw new Error('learned-lessons.json: not version 1')
const gate = typeof raw.gate === 'number' && raw.gate >= GATE && raw.gate < 1 ? raw.gate : GATE
const lessons: Lesson[] = []
for (const item of Array.isArray(raw.lessons) ? raw.lessons.slice(0, MAX_LESSONS) : []) {
if (!item || typeof item !== 'object') continue
const l = item as Record<string, unknown>
const confidence = typeof l.confidence === 'number' && l.confidence >= 0 && l.confidence <= 1 ? l.confidence : -1
const keywords = Array.isArray(l.keywords) ? l.keywords.filter((k): k is string => typeof k === 'string' && WORD.test(k)).slice(0, 10) : []
if (typeof l.id !== 'string' || !ID.test(l.id) || typeof l.agent !== 'string' || !AGENT.test(l.agent) || keywords.length === 0 || confidence < 0) continue
if (l.kind !== 'lesson' && l.kind !== 'caution') continue
// A lesson below the gate never reaches a prompt, whatever the file says.
if (l.kind === 'lesson' && confidence < gate) continue
const evidence = typeof l.evidence === 'string' && EVIDENCE.test(l.evidence) ? l.evidence : ''
// ADR-511: the same screen the recorder applies; a path that fails it is dropped, never shown.
const files = l.kind === 'lesson' && Array.isArray(l.files) ? l.files.filter((f): f is string => typeof f === 'string' && lessonFile(f)).slice(0, MAX_FILES_SHOWN) : []
lessons.push({ id: l.id, kind: l.kind, agent: l.agent, keywords, confidence, successes: count(l.successes), failures: count(l.failures), evidence, files })
}
return { gate, lessons, belowGate: count(raw.belowGate) }
}
const words = (text: string) => new Set(text.toLowerCase().split(/[\s\-_.,;:!?'"()\[\]{}]+/).filter(Boolean))
/** Whether the prompt says a keyword: as a word, or (for one with a space or slash) as a phrase. */
const says = (said: Set<string>, lower: string, keyword: string) => said.has(keyword) || (/[ /]/.test(keyword) && lower.includes(keyword))
/** ADR-511: a lesson scoring under this share of the best match is left out (it matched on words every task says). */
export const MIN_SHARE_OF_BEST = 0.5
/**
* The lessons that share a keyword with the prompt, best first: the matched
* share of the lesson's keywords times its confidence. ADR-511: each keyword
* is weighted by how rare it is among the lessons (inverse document
* frequency), so words every lesson has ("test", "fix") barely count. At most
* three, and none far below the best.
*/
export function pick(file: Lessons, prompt: string): Lesson[] {
const said = words(prompt)
const lower = prompt.toLowerCase()
const df = new Map<string, number>()
for (const l of file.lessons) for (const k of new Set(l.keywords)) df.set(k, (df.get(k) ?? 0) + 1)
const weight = (k: string) => Math.log((file.lessons.length + 1) / (df.get(k) ?? 1))
const sum = (ks: readonly string[]) => ks.reduce((a, k) => a + weight(k), 0)
const scored = file.lessons
.map(l => {
const matched = l.keywords.filter(k => says(said, lower, k))
return { l, overlap: matched.length, score: (sum(matched) / sum(l.keywords)) * l.confidence }
})
.filter(x => x.overlap > 0)
.sort((a, b) => b.score - a.score)
const best = scored[0]?.score ?? 0
return scored
.filter(x => x.score >= best * MIN_SHARE_OF_BEST)
.slice(0, MAX_PER_PROMPT)
.map(x => x.l)
}
/** The context block for the model; null when nothing applies or the block fails the screen. */
export function lessonBlock(chosen: readonly Lesson[]): string | null {
if (chosen.length === 0) return null
const lines = chosen.map(l => {
const tag = `[${l.kind} ${l.id} conf ${l.confidence.toFixed(2)}, ${l.successes} passed / ${l.failures} failed]`
const check = l.evidence ? ` \`${l.evidence}\`` : ' verification'
if (l.kind === 'caution') return `- ${tag} tasks about ${l.keywords.join(', ')} failed${check} ${l.failures} times: check that first`
// ADR-511: a lesson that knows where the passing fixes went leads with it; the model skims a keyword list.
if (l.files.length) return `- ${tag} verified fixes for tasks like this (${l.keywords.join(', ')}) edited ${l.files.map(f => `\`${f}\``).join(', ')}: look there first. They passed${check}.`
return `- ${tag} tasks about ${l.keywords.join(', ')} went to ${l.agent} and passed${check}`
})
const block = ['[RUFLO_LESSONS] From this project\'s own test/build exit status (never a model\'s claim of success). Advisory only:', ...lines].join('\n')
const found = scan(block)
return found.secrets.length || found.injection.length ? null : block
}