Files
ruflo/v3/@claude-flow/cli/scripts/prepare-publish.mjs
T
randRuFlo ee2f94ad07 fix(release): refuse to publish helpers that differ from HEAD; release 3.74.1
@claude-flow/cli@3.74.0 shipped 3.63.4 .claude/helpers. The committed
package stamp (.helpers-version) reads 3.57.1, so hook-spawned daemons
running an older checkout's CLI (3.63.4) saw the worktree's helpers as
stale and rewrote them between prepare and npm publish. verify-helpers
passed because it signs and checks whatever is on disk.

prepare-publish now diffs .claude/helpers against HEAD before signing
and again after verifying, and throws if any helper content changed
(the regenerated manifest and stamp are excluded). Tested: a dirtied
helper is refused, and a live daemon overwrite during a clean run was
caught.

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-11-09 01:06:05 -05:00

102 lines
4.1 KiB
JavaScript

#!/usr/bin/env node
import { cp, mkdir, rm } from 'node:fs/promises';
import { spawnSync } from 'node:child_process';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { stageInternalRuntimeBundles } from '../../../../scripts/stage-internal-runtime-bundles.mjs';
const packageDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const repoRoot = resolve(packageDir, '..', '..', '..');
const pluginsDir = join(packageDir, 'plugins');
// `dist` is intentionally ignored by Git, so publishing without a build can
// create a tarball whose declared entry point does not exist. Build the only
// emitted project reference CLI needs (`@claude-flow/swarm`) and then CLI.
// Do not use `tsc --build`: it recursively rebuilds unrelated optional
// workspace packages whose development-only dependencies are not required to
// package this CLI.
const compiler = join(packageDir, 'node_modules', 'typescript', 'bin', 'tsc');
for (const buildDir of [resolve(packageDir, '..', 'swarm'), packageDir]) {
const build = spawnSync(process.execPath, [compiler], {
cwd: buildDir,
stdio: 'inherit',
});
if (build.error) throw build.error;
if (build.status !== 0) {
throw new Error(`TypeScript release build failed for ${buildDir} with exit code ${build.status ?? 'unknown'}`);
}
}
await stageInternalRuntimeBundles(packageDir);
await cp(join(repoRoot, 'README.md'), join(packageDir, 'README.md'));
await rm(pluginsDir, { recursive: true, force: true });
await mkdir(pluginsDir, { recursive: true });
await cp(
join(repoRoot, 'plugins', 'ruflo-metaharness'),
join(pluginsDir, 'ruflo-metaharness'),
{ recursive: true },
);
// ADR-489: `ruflo tui` loads one bundled file from the CLI's own install
// (plugins/ruflo-console/dist/tui.mjs, see src/commands/tui.ts). Build it from
// the plugin's source with the repo's esbuild and stage only that file, so the
// registry install runs the console and the tarball carries no console source.
const tuiBundle = join(pluginsDir, 'ruflo-console', 'dist', 'tui.mjs');
const tuiBuild = spawnSync(
process.execPath,
[join(repoRoot, 'plugins', 'ruflo-console', 'scripts', 'build-tui.mjs'), '--outfile', tuiBundle],
{ cwd: repoRoot, stdio: 'inherit' },
);
if (tuiBuild.error) throw tuiBuild.error;
if (tuiBuild.status !== 0) {
throw new Error(`build-tui.mjs failed with exit code ${tuiBuild.status ?? 'unknown'}`);
}
// 3.74.0 shipped 3.63.4 helpers: a hook-spawned daemon running an older CLI
// rewrote .claude/helpers between pack prep and publish, and verify-helpers
// passed because it signs whatever is on disk. Refuse unless the helper
// content matches the commit being published (the manifest and version stamp
// are regenerated below, so they are excluded).
assertHelpersMatchHead();
for (const script of [
'generate-catalog-manifest.mjs',
'sign-helpers.mjs',
'verify-helpers.mjs',
]) {
const result = spawnSync(process.execPath, [join(packageDir, 'scripts', script)], {
cwd: packageDir,
stdio: 'inherit',
});
if (result.error) throw result.error;
if (result.status !== 0) {
throw new Error(`${script} failed with exit code ${result.status ?? 'unknown'}`);
}
}
assertHelpersMatchHead();
function assertHelpersMatchHead() {
if (process.env.RUFLO_ALLOW_DIRTY_HELPERS === '1') return;
const diff = spawnSync('git', [
'diff', '--name-only', 'HEAD', '--',
'.claude/helpers',
':(exclude).claude/helpers/helpers.manifest.json',
':(exclude).claude/helpers/.helpers-version',
], { cwd: packageDir, encoding: 'utf8' });
if (diff.error || diff.status !== 0) {
throw new Error(`cannot check .claude/helpers against HEAD: ${diff.error?.message ?? diff.stderr}`);
}
const dirty = diff.stdout.trim();
if (dirty) {
throw new Error(
`.claude/helpers differ from HEAD, refusing to publish:\n${dirty}\n` +
'Something rewrote them (often a daemon or MCP server running an older CLI). ' +
'Stop it, run `git checkout HEAD -- .claude/helpers`, and retry. ' +
'Set RUFLO_ALLOW_DIRTY_HELPERS=1 only for a deliberate local experiment.',
);
}
}