Files
ruflo/v3/@claude-flow/cli/scripts/verify-helpers.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

76 lines
3.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Verify .claude/helpers/helpers.manifest.json against the shipped critical
* helpers (ADR-174, issue #2593). Runs in prepublishOnly AFTER sign-helpers.mjs
* so npm publish fails closed if the manifest ever drifts from disk again.
*
* Checks:
* 1. Ed25519 signature over the canonical manifest bytes is valid under
* RUFLO_HELPERS_PUBKEY (same primitive as helper-signing.ts).
* 2. manifest.version matches package.json version.
* 3. Every critical helper on disk hashes to the manifest entry.
*
* Optional arg: path to a helpers dir (e.g. an extracted `npm pack` tarball).
* Defaults to <cli-pkg>/.claude/helpers.
*/
import { readFileSync, existsSync } from 'node:fs';
import { createHash, verify as edVerify } from 'node:crypto';
import { join, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PKG_ROOT = join(__dirname, '..');
const HELPERS_DIR = resolve(process.argv[2] || join(PKG_ROOT, '.claude', 'helpers'));
// Keep in sync with sign-helpers.mjs:CRITICAL and src/init/helper-refresh.ts:CRITICAL_HELPERS.
const CRITICAL = ['auto-memory-hook.mjs', 'hook-handler.cjs', 'intelligence.cjs', 'statusline.cjs', 'router.cjs', 'learning-loop.cjs'];
function die(msg) { console.error(`[verify-helpers] ${msg}`); process.exit(1); }
// #2675 — helper-signing.ts is the sole key source. prepare-publish.mjs and
// stable-npm-release.yml both build before verification, so importing emitted
// runtime code preserves the exact key users will trust after installation.
let RUFLO_HELPERS_PUBKEY;
try {
({ RUFLO_HELPERS_PUBKEY } = await import('../dist/src/init/helper-signing.js'));
} catch {
die('compiled helper-signing.js is unavailable; run `npm run build` before verification');
}
const manifestPath = join(HELPERS_DIR, 'helpers.manifest.json');
if (!existsSync(manifestPath)) die(`missing manifest: ${manifestPath}`);
let signed;
try { signed = JSON.parse(readFileSync(manifestPath, 'utf-8')); }
catch (e) { die(`manifest is not valid JSON: ${e.message}`); }
if (!signed || signed.algorithm !== 'ed25519' || !signed.signature || !signed.manifest?.files) {
die('manifest is malformed (missing algorithm/signature/files)');
}
// Canonical bytes — sorted file keys, MUST match sign-helpers.mjs + helper-signing.ts.
const sortedFiles = {};
for (const k of Object.keys(signed.manifest.files).sort()) sortedFiles[k] = signed.manifest.files[k];
const canonical = Buffer.from(JSON.stringify({ version: signed.manifest.version, files: sortedFiles }), 'utf-8');
if (!edVerify(null, canonical, RUFLO_HELPERS_PUBKEY, Buffer.from(signed.signature, 'base64'))) {
die('Ed25519 signature does not verify against RUFLO_HELPERS_PUBKEY');
}
const pkgVersion = JSON.parse(readFileSync(join(PKG_ROOT, 'package.json'), 'utf-8')).version;
if (signed.manifest.version !== pkgVersion) {
die(`manifest version ${signed.manifest.version} != package.json ${pkgVersion} — re-run sign-helpers.mjs`);
}
for (const name of CRITICAL) {
const p = join(HELPERS_DIR, name);
if (!existsSync(p)) die(`critical helper missing on disk: ${p}`);
const actual = createHash('sha256').update(readFileSync(p)).digest('hex');
const expected = signed.manifest.files[name];
if (!expected) die(`manifest has no entry for ${name}`);
if (actual !== expected) {
die(`hash drift for ${name}\n manifest: ${expected}\n on disk: ${actual}\n → re-run sign-helpers.mjs`);
}
}
console.log(`[verify-helpers] ok — ${CRITICAL.length} helpers match signed manifest @ ${signed.manifest.version}`);