Files
ruflo/plugins/ruflo-mods/scripts/build-learning-loop-helper.mjs
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

66 lines
2.7 KiB
JavaScript

#!/usr/bin/env node
// Bundles the classic-hooks learning loop (ADR-512) into .claude/helpers/learning-loop.cjs, from the mod's own loop
// source (scripts/learning-loop-helper.ts → hooks/loop/transcript.ts), so classic hooks and the mod decide outcomes and
// pick lessons with one implementation. The bundle is committed: helper-refresh copies the package's helpers into
// every project on its next `ruflo` command, and the release signs them. esbuild is a BUILD-time tool only.
// node plugins/ruflo-mods/scripts/build-learning-loop-helper.mjs [--check]
import { readFileSync, writeFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const repo = resolve(root, '..', '..')
const check = process.argv.includes('--check')
const targets = [join(repo, 'v3', '@claude-flow', 'cli', '.claude', 'helpers', 'learning-loop.cjs'), join(repo, '.claude', 'helpers', 'learning-loop.cjs')]
// The repo's own esbuild (npm install at the root), or the one in RUFLO_ESBUILD_DIR (as build-tui.mjs).
let esbuild
for (const base of [join(repo, 'package.json'), join(repo, 'v3', 'package.json'), ...(process.env.RUFLO_ESBUILD_DIR ? [join(process.env.RUFLO_ESBUILD_DIR, 'package.json')] : [])]) {
try {
esbuild = createRequire(base)('esbuild')
break
} catch {
// try the next place
}
}
if (esbuild === undefined) {
console.error('build-learning-loop-helper: esbuild is not installed (npm install at the repo root, or set RUFLO_ESBUILD_DIR).')
process.exit(1)
}
const result = await esbuild.build({
entryPoints: [join(root, 'scripts', 'learning-loop-helper.ts')],
bundle: true,
platform: 'node',
format: 'cjs',
target: 'node20',
minify: false,
legalComments: 'none',
write: false,
absWorkingDir: repo,
logLevel: 'warning',
banner: { js: '// GENERATED by plugins/ruflo-mods/scripts/build-learning-loop-helper.mjs from the ruflo-mods learning loop (ADR-512). Do not edit.' },
})
// esbuild writes the entry's path into a comment; keep the bundle the same on every machine.
const text = result.outputFiles[0].text.replaceAll(repo.replace(/\\/g, '/') + '/', '')
let stale = 0
for (const target of targets) {
let current = null
try {
current = readFileSync(target, 'utf8')
} catch {
// missing
}
if (current === text) continue
if (check) {
console.error(`build-learning-loop-helper: ${target} is stale; run node plugins/ruflo-mods/scripts/build-learning-loop-helper.mjs`)
stale++
} else {
writeFileSync(target, text)
console.log(`build-learning-loop-helper: wrote ${target} (${Math.round(text.length / 1024)} KiB)`)
}
}
process.exit(stale ? 1 : 0)