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

112 lines
5.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Sign the critical helpers → .claude/helpers/helpers.manifest.json (ADR-174).
*
* Run at publish time WHENEVER a critical helper changes. NEVER commit the
* private key. The public half is baked into src/init/helper-signing.ts
* (RUFLO_HELPERS_PUBKEY).
*
* Private-key resolution (first that is set wins):
* 1. GCP Secret Manager (PREFERRED for CI/publish):
* RUFLO_HELPERS_SIGNING_SECRET=<secret-name> (e.g. ruflo-helpers-signing-key)
* RUFLO_HELPERS_SIGNING_PROJECT=<gcp-project> (optional; defaults to the
* active gcloud project)
* Fetched via `gcloud secrets versions access latest`.
* 2. RUFLO_HELPERS_SIGNING_KEY=<pem-file-path> (local / air-gapped)
* 3. ~/.ruflo/helpers-signing.key (dev default)
*
* Stdin fallback (the PEM never appears in shell output or argv):
* gcloud secrets versions access latest --secret=... | node scripts/sign-helpers.mjs --stdin-key
*
* Usage: RUFLO_HELPERS_SIGNING_SECRET=ruflo-helpers-signing-key node scripts/sign-helpers.mjs
*/
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
import { createHash, createPrivateKey, sign as edSign } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { homedir } from 'node:os';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PKG_ROOT = join(__dirname, '..');
const HELPERS_DIR = join(PKG_ROOT, '.claude', 'helpers');
// Keep in sync with 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 loadPrivateKey() {
if (process.argv.includes('--stdin-key')) {
if (process.stdin.isTTY) {
console.error(
'[sign-helpers] --stdin-key requires piped stdin; refusing interactive key entry. ' +
'Pipe Secret Manager output directly or use RUFLO_HELPERS_SIGNING_KEY=<pem-file-path>.',
);
process.exit(1);
}
const key = readFileSync(0, 'utf-8');
if (!key.trim()) {
console.error('[sign-helpers] --stdin-key received empty input');
process.exit(1);
}
return key;
}
const secret = process.env.RUFLO_HELPERS_SIGNING_SECRET;
if (secret) {
const args = ['secrets', 'versions', 'access', 'latest', '--secret', secret];
const project = process.env.RUFLO_HELPERS_SIGNING_PROJECT;
if (project) args.push('--project', project);
try {
const gcloudBin = process.platform === 'win32' ? 'gcloud.cmd' : 'gcloud';
// stdio: key on stdout (captured), stderr captured too — NOT inherited.
// Inheriting would forward gcloud's stderr verbatim into whatever log
// is capturing this process's output (CI, terminal); this key material
// is sensitive enough that we never want an uncontrolled passthrough,
// even though gcloud's normal error paths don't echo secret payloads.
return execFileSync(gcloudBin, args, { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'] });
} catch {
console.error(`[sign-helpers] failed to read GCP secret '${secret}'. Is gcloud authed? (gcloud auth login)`);
process.exit(1);
}
}
const keyPath = process.env.RUFLO_HELPERS_SIGNING_KEY || join(homedir(), '.ruflo', 'helpers-signing.key');
if (!existsSync(keyPath)) {
console.error(
`[sign-helpers] no signing key. Set RUFLO_HELPERS_SIGNING_SECRET (GCP) ` +
`or RUFLO_HELPERS_SIGNING_KEY (PEM path); tried ${keyPath}.`,
);
process.exit(1);
}
return readFileSync(keyPath, 'utf-8');
}
const privateKeyPem = loadPrivateKey();
try {
const key = createPrivateKey(privateKeyPem);
if (key.asymmetricKeyType !== 'ed25519') throw new Error('wrong-key-type');
} catch {
// Never include the PEM or the crypto parser's input in diagnostics.
console.error('[sign-helpers] signing key is not a valid Ed25519 private key');
process.exit(1);
}
const version = JSON.parse(readFileSync(join(PKG_ROOT, 'package.json'), 'utf-8')).version;
const files = {};
for (const name of CRITICAL) {
const p = join(HELPERS_DIR, name);
if (!existsSync(p)) { console.error(`[sign-helpers] missing helper: ${p}`); process.exit(1); }
files[name] = createHash('sha256').update(readFileSync(p)).digest('hex');
}
const manifest = { version, files };
// Canonical bytes: sorted file keys — MUST match helper-signing.ts.
const sortedFiles = {};
for (const k of Object.keys(manifest.files).sort()) sortedFiles[k] = manifest.files[k];
const canonical = Buffer.from(JSON.stringify({ version: manifest.version, files: sortedFiles }), 'utf-8');
const signature = edSign(null, canonical, privateKeyPem).toString('base64');
const signed = { manifest, signature, algorithm: 'ed25519' };
const outPath = join(HELPERS_DIR, 'helpers.manifest.json');
writeFileSync(outPath, JSON.stringify(signed, null, 2) + '\n', 'utf-8');
console.log(`[sign-helpers] signed ${CRITICAL.length} helpers → ${outPath}`);
for (const [n, h] of Object.entries(files)) console.log(` ${n}: ${h.slice(0, 16)}…`);