Files
hyperframes/packages/cli/src/commands/compare.ts
T
Miguel ÁngelandClaude Opus 4.8 57b3c78987 feat(media-use): color grading — grade/lut resolve, smart-grade, grade-compare + compare (#2041)
* feat(media-use): color grading — grade/lut resolve, smart-grade, grade-compare CLI

Add color grading to media-use as first-class resolve types plus a faithful
comparison command. All local, offline, deterministic — no model, no GPU.

- resolve -t grade / -t lut: produce a data-color-grading block (or a frozen
  .cube). Look cascade: core preset (no file) -> bundled .cube library ->
  parametric buildCube. Emitted .cube is Rec.709 and validated against core's
  colorLuts constraints (LUT_3D_SIZE <= 64) before it is frozen.
- smart grade (grade --for <media>): ffmpeg signalstats -> adjust suggestion
  (exposure / contrast / white balance), surfaced with the measured evidence on
  stderr as a starting point; never auto-applied.
- hyperframes grade-compare: renders N candidate grades onto a reference frame
  through the real runtime shader into one labeled comparison PNG, so an agent
  picks a look without opening Studio. Prepends an "original" baseline cell by
  default (--no-baseline to omit). Shares the headless-capture pipeline with
  snapshot via capture/captureCompositionFrame.
- media-use SKILL: proactive "media opportunity pass" guidance (grounded
  signal -> offer, ask once, surface don't mutate).

Verified: media-use 116/116, grade-compare 7/7, snapshot 9/9, lint + format
clean, full build green, comparison renders end to end.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* test(cli): narrow grade-compare baseline assertion off unknown-typed grading

Assert the whole cell via toEqual instead of reaching into .grading.preset /
.grading.lut on the unknown-typed field, keeping the test typecheck-clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(media-use): agent-authored LUTs via --params + validate --from cube; never-read-.cube guardrail

- resolve -t lut / -t grade --params '<json>': build a parametric .cube from
  explicit params (bypassing the intent cascade), validate, and freeze in one
  step. --intent becomes the optional description. Lets an agent commit a look
  it computed itself.
- --from <file.cube> now validates the ingested LUT for lut/grade types and
  rejects an invalid/oversized cube (no partial write) — the escape hatch for a
  LUT the agent generated with its own code.
- SKILL.md: hard rule to never read a .cube body into context (~size^3 lines,
  zero legible signal) — inspect via grade-compare (see it) or cube-validate
  (ok/size), read the manifest description for meaning; plus both authoring
  paths and the parametric-vs-film-stock ceiling note.

Verified: media-use 116/116, lint + format clean; smokes — --params builds a
valid frozen cube, grade --params returns a lut block, bad JSON and an oversized
--from cube are both rejected with no stray file.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(cli): grade-compare validates referenced LUTs, warns on no-op cells, caps candidates

Bug-bash follow-ups — grade-compare silently accepted bad input:

- Validate LUT *content*, not just existence: each referenced .cube is parsed
  with core's parseCubeLut (now exported from @hyperframes/core) and rejected
  with a per-cell error ("LUT for \"<label>\" is not a valid .cube: ..."). A
  file that exists but isn't a valid cube no longer renders a silent no-op cell.
- Warn on inactive cells: a grading that normalizes to inactive (e.g. a
  malformed {lut:12345}) emits a stderr warning naming the cell; the
  auto-prepended "original" baseline is intentionally inactive and stays silent.
  stdout remains valid JSON.
- Cap candidates at 16 (excluding baseline): over-cap input renders the first N
  and reports {truncated:true, total:M} on stdout + a stderr note — no silent
  drop, no unbounded giant sheet.

Verified: grade-compare 10/10; non-cube LUT → clear error; {lut:12345} → warning
+ ok; 20 cells → cells=17 truncated total=20; valid runs unchanged. Lint/format
clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(cli): general `hyperframes compare` visual-variant primitive

Generalize grade-compare's "render N variants → one labeled sheet → the agent
looks and picks" loop into a standalone command that works on ANY variation
(font, layout, motion, grade, whole compositions) — the tool never needs to
know what differs.

- `hyperframes compare <path...> [--at <sec>] [--labels a,b,c] [--out] [--cols]
  [--json]`: renders each agent-authored composition variant through the real
  runtime (captureCompositionFrame) and stitches one labeled comparison sheet +
  JSON ({ok, sheet, rendered, variants, truncated?/total?}). 2+ paths required;
  caps at 16 with loud truncation. It presents, it does not judge — choosing is
  the caller's job.
- Factored the shared "render a labeled set → contact sheet" path so compare,
  grade-compare, and snapshot all sit on it (no duplication). grade-compare is
  now the first color-specific specialization of this primitive.
- New pathArgs util + contactSheet test; hyperframes-cli SKILL documents compare
  as the agent's "see your own renders and choose" primitive.

Verified: 26/26 across compare + grade-compare + snapshot + contactSheet (no
regressions); compare renders 3 variants into one visibly-distinct labeled
sheet; 2+-path error path clean; lint/format clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(ci): green the skills CI — skip ffmpeg tests when absent, oxfmt markdown

The "Test: skills" CI job runs bare `node --test` with no ffmpeg on PATH (by
design — skills tests are meant to be node-builtin-only). The grade-analyzer +
smart-grade tests shell to ffmpeg and were failing there with ENOENT. Guard
them to skip when ffmpeg isn't on PATH; they still run locally / where it is.

Also oxfmt README.md + hyperframes/media-use SKILL.md (the whole-repo
`oxfmt --check .` Format job caught markdown left unformatted by the rebase
conflict resolution).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(ci): skip core-conformance test when tsx is unavailable

The "Test: skills" CI job installs no deps, so the normalizeHfColorGrading
conformance test (which imports core's TS via `node --import tsx`) failed there.
Guard it to skip when tsx can't resolve; runs locally / in the deps-installed
Test job. Completes the skills-CI greening (the ffmpeg guards handled the rest).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(cli): escape grade-compare src double-quotes (CodeQL XSS) + Windows-safe compare test

- grade-compare built `<img src="...">` (double-quoted) with the single-quote
  escaper, leaving `"` unescaped — a `"` in the frame path could break out
  (CodeQL: incomplete HTML attribute sanitization). Use escapeXml for src.
- compare label test hard-coded POSIX paths that can't match on Windows; assert
  the derived labels (the subject); path resolution is covered elsewhere.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* refactor(media-use): generate LUT library from params (drop committed .cube files)

The 3 bundled .cube files were 733 lines each (2,199 total) and were themselves
buildCube output — pure repo bloat. Replace with compact per-look params in
luts/index.json, generated on resolve; add an optional `url` for future scanned
LUTs to be CDN-hosted + downloaded on demand (freezeUrl) instead of committed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(media-use): serve library LUTs from CDN on-demand (static.heygen.ai/luts), params fallback

Looks now carry a CDN `url` (hosted at s3://heygen-public/luts → static.heygen.ai/luts/<id>.cube);
resolve downloads + validates + freezes on demand, like bgm/image. `params` stays
as the deterministic offline fallback (--local-only, or if the download fails), so
resolution is never blocked on the network. Provider prefers url, falls back to params.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(media-use): address #2041 review — atomic LUT writes, compare telemetry, follow-ups

- Atomic .cube writes: library provider (url + params) and the parametric
  generator now write to a .tmp path, validate, then rename, so a crash can
  never orphan an invalid .cube at the final path (was validate-after-write).
- track("media_use_resolve") now emits provenance.via (url/params-fallback/params).
- grade-compare + compare: --timeout flag (was hardcoded 5000) and a
  media_use_compare event (cells, truncated, total, render_ready_timed_out);
  openSettledCompositionPage now surfaces the render-ready timeout.
- compare staging skips node_modules/.git; --for gets an upfront existence check.
- Rec.709 luma comment; HYPERFRAMES_ANALYZE_TIMEOUT_MS override; measured note
  uses basename; LUT s3 hosting moved from index.json into luts/README.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 22:20:16 -04:00

414 lines
12 KiB
TypeScript

import { cpSync, existsSync, mkdirSync, mkdtempSync, renameSync, rmSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import { basename, dirname, extname, join } from "node:path";
import { defineCommand } from "citty";
import { createContactSheet } from "../capture/contactSheet.js";
import {
openSettledCompositionPage,
seekCompositionTimeline,
} from "../capture/captureCompositionFrame.js";
import { c } from "../ui/colors.js";
import { normalizeErrorMessage } from "../utils/errorMessage.js";
import { displayPathFromInput, readOptionalString, resolveFromBase } from "../utils/pathArgs.js";
import { trackCompareSheet } from "../telemetry/events.js";
import { serveStaticProjectHtml } from "../utils/staticProjectServer.js";
import { withMeta } from "../utils/updateCheck.js";
import type { Example } from "./_examples.js";
const MAX_COMPARE_VARIANTS = 16;
const MAX_COLUMNS = 4;
const DEFAULT_RENDER_READY_TIMEOUT_MS = 5000;
export interface CompareVariantSpec {
label: string;
inputPath: string;
displayPath: string;
}
export interface PreparedCompareVariant extends CompareVariantSpec {
projectDir: string;
stagedDir?: string;
}
export interface ParsedCompareArgs {
variants: CompareVariantSpec[];
outPath: string;
atSeconds: number;
cols?: number;
json: boolean;
timeoutMs: number;
}
export interface CompareVariantCapResult {
variants: CompareVariantSpec[];
truncated: boolean;
total: number;
}
export interface CompareSuccessPayload {
ok: true;
sheet: string;
variants: { label: string; path: string }[];
rendered: number;
truncated?: true;
total?: number;
}
export const examples: Example[] = [
[
"Compare two agent-authored composition variants",
"hyperframes compare ./variants/a ./variants/b --out compare.png",
],
[
"Compare three variants at a specific timeline time",
"hyperframes compare ./a ./b ./c --at 2.5 --labels classic,bold,quiet --json",
],
];
function defaultLabelForPath(input: string): string {
const name = basename(input);
return extname(name).toLowerCase() === ".html" ? basename(name, extname(name)) : name;
}
function parsePathArgs(args: { _?: readonly unknown[] }): string[] {
return (args._ ?? [])
.filter((value): value is string => typeof value === "string")
.map((value) => value.trim())
.filter(Boolean);
}
function parseLabels(value: unknown, pathCount: number): string[] | undefined {
const raw = readOptionalString(value);
if (!raw) return undefined;
const labels = raw.split(",").map((part) => part.trim());
if (labels.some((label) => label.length === 0)) {
throw new Error("--labels entries must be non-empty");
}
if (labels.length !== pathCount) {
throw new Error(`--labels count (${labels.length}) must match path count (${pathCount})`);
}
return labels;
}
function parseAtSeconds(value: unknown): number {
const raw = readOptionalString(value);
if (!raw) return 0;
const parsed = Number(raw);
if (!Number.isFinite(parsed) || parsed < 0) {
throw new Error("--at must be a non-negative number of seconds");
}
return parsed;
}
function parseColumns(value: unknown): number | undefined {
const raw = readOptionalString(value);
if (!raw) return undefined;
const parsed = Number(raw);
if (!Number.isInteger(parsed) || parsed < 1) {
throw new Error("--cols must be a positive integer");
}
return parsed;
}
function defaultCompareCols(cellCount: number): number {
return Math.max(1, Math.min(MAX_COLUMNS, Math.ceil(Math.sqrt(cellCount))));
}
export function parseCompareArgs(
args: {
_?: readonly unknown[];
labels?: unknown;
out?: unknown;
at?: unknown;
cols?: unknown;
json?: unknown;
timeout?: unknown;
},
cwd = process.cwd(),
): ParsedCompareArgs {
const paths = parsePathArgs(args);
if (paths.length < 2) {
throw new Error("need 2+ paths to compare");
}
const labels = parseLabels(args.labels, paths.length);
const variants = paths.map((input, index) => ({
label: labels?.[index] ?? defaultLabelForPath(input),
inputPath: resolveFromBase(cwd, input),
displayPath: displayPathFromInput(cwd, input),
}));
return {
variants,
outPath: resolveFromBase(cwd, readOptionalString(args.out) ?? "compare.png"),
atSeconds: parseAtSeconds(args.at),
cols: parseColumns(args.cols),
json: args.json === true,
timeoutMs:
Number.parseInt(readOptionalString(args.timeout) ?? "", 10) ||
DEFAULT_RENDER_READY_TIMEOUT_MS,
};
}
export function capCompareVariants(
variants: readonly CompareVariantSpec[],
): CompareVariantCapResult {
const total = variants.length;
if (total <= MAX_COMPARE_VARIANTS) {
return { variants: [...variants], truncated: false, total };
}
console.error(
c.warn(
`Warning: ${total} compare variants exceed the ${MAX_COMPARE_VARIANTS}-variant cap — rendering the first ${MAX_COMPARE_VARIANTS} of ${total}; re-run with fewer variants or split into multiple runs.`,
),
);
return {
variants: variants.slice(0, MAX_COMPARE_VARIANTS),
truncated: true,
total,
};
}
export function buildCompareSuccessPayload(
sheet: string,
variants: readonly CompareVariantSpec[],
capResult: CompareVariantCapResult,
): CompareSuccessPayload {
const payload: CompareSuccessPayload = {
ok: true,
sheet,
variants: variants.map((variant) => ({
label: variant.label,
path: variant.displayPath,
})),
rendered: variants.length,
};
if (capResult.truncated) {
payload.truncated = true;
payload.total = capResult.total;
}
return payload;
}
function inputError(variant: CompareVariantSpec): Error {
return new Error(
`Variant "${variant.label}" is not a composition input (${variant.displayPath}): expected a directory containing index.html or a .html file`,
);
}
function stageHtmlVariant(variant: CompareVariantSpec): PreparedCompareVariant {
const stagedDir = mkdtempSync(join(tmpdir(), "hf-compare-variant-"));
try {
// Copy the composition's sibling files but skip heavy/irrelevant trees — a
// variant sitting next to node_modules or .git shouldn't drag them into tmp.
cpSync(dirname(variant.inputPath), stagedDir, {
recursive: true,
filter: (src) => {
const base = basename(src);
return base !== "node_modules" && base !== ".git";
},
});
const sourceName = basename(variant.inputPath);
if (sourceName !== "index.html") {
renameSync(join(stagedDir, sourceName), join(stagedDir, "index.html"));
}
return {
...variant,
projectDir: stagedDir,
stagedDir,
};
} catch (err) {
rmSync(stagedDir, { recursive: true, force: true });
throw err;
}
}
export function prepareCompareVariantProjects(
variants: readonly CompareVariantSpec[],
): PreparedCompareVariant[] {
const prepared: PreparedCompareVariant[] = [];
try {
for (const variant of variants) {
if (!existsSync(variant.inputPath)) {
throw inputError(variant);
}
const stat = statSync(variant.inputPath);
if (stat.isDirectory() && existsSync(join(variant.inputPath, "index.html"))) {
prepared.push({
...variant,
projectDir: variant.inputPath,
});
continue;
}
if (stat.isFile() && extname(variant.inputPath).toLowerCase() === ".html") {
prepared.push(stageHtmlVariant(variant));
continue;
}
throw inputError(variant);
}
return prepared;
} catch (err) {
for (const variant of prepared) {
if (variant.stagedDir) {
rmSync(variant.stagedDir, { recursive: true, force: true });
}
}
throw err;
}
}
function cleanupPreparedCompareVariants(variants: readonly PreparedCompareVariant[]): void {
for (const variant of variants) {
if (variant.stagedDir) {
rmSync(variant.stagedDir, { recursive: true, force: true });
}
}
}
async function renderCompareVariant(
variant: PreparedCompareVariant,
opts: { atSeconds: number; framePath: string; timeoutMs: number },
): Promise<{ framePath: string; renderReadyTimedOut: boolean }> {
try {
const { bundleToSingleHtml } = await import("@hyperframes/core/compiler");
const html = await bundleToSingleHtml(variant.projectDir);
const server = await serveStaticProjectHtml(variant.projectDir, html);
try {
const {
browser: chromeBrowser,
page,
renderReadyTimedOut,
} = await openSettledCompositionPage(html, server.url, {
renderReadyTimeoutMs: opts.timeoutMs,
renderReadyWarningSuffix: `comparison variant "${variant.label}" may be inaccurate`,
});
try {
if (opts.atSeconds > 0) {
await seekCompositionTimeline(page, opts.atSeconds);
}
await page.screenshot({ path: opts.framePath, type: "png" });
return { framePath: opts.framePath, renderReadyTimedOut };
} finally {
await chromeBrowser.close();
}
} finally {
await server.close();
}
} catch (err) {
throw new Error(
`Render failed for variant "${variant.label}" (${variant.displayPath}): ${normalizeErrorMessage(err)}`,
);
}
}
async function renderCompareSheet(parsed: ParsedCompareArgs): Promise<CompareSuccessPayload> {
const capResult = capCompareVariants(parsed.variants);
const variants = capResult.variants;
const prepared = prepareCompareVariantProjects(variants);
const frameDir = mkdtempSync(join(tmpdir(), "hf-compare-frames-"));
const framePaths: string[] = [];
try {
let renderReadyTimedOut = false;
for (let i = 0; i < prepared.length; i++) {
const variant = prepared[i]!;
const framePath = join(frameDir, `variant-${String(i + 1).padStart(2, "0")}.png`);
const rendered = await renderCompareVariant(variant, {
atSeconds: parsed.atSeconds,
framePath,
timeoutMs: parsed.timeoutMs,
});
framePaths.push(rendered.framePath);
renderReadyTimedOut = renderReadyTimedOut || rendered.renderReadyTimedOut;
}
mkdirSync(dirname(parsed.outPath), { recursive: true });
await createContactSheet(framePaths, parsed.outPath, {
cols: parsed.cols ?? defaultCompareCols(framePaths.length),
maxImages: framePaths.length,
labelMode: "custom",
labels: variants.map((variant) => variant.label),
});
trackCompareSheet({
command: "compare",
cells: variants.length,
truncated: capResult.truncated,
total: capResult.total,
renderReadyTimedOut,
});
return buildCompareSuccessPayload(parsed.outPath, variants, capResult);
} finally {
cleanupPreparedCompareVariants(prepared);
rmSync(frameDir, { recursive: true, force: true });
}
}
function printJson(payload: object): void {
console.log(JSON.stringify(withMeta(payload), null, 2));
}
export default defineCommand({
meta: {
name: "compare",
description: "Render independent composition variants into one labeled comparison sheet",
},
args: {
path: {
type: "positional",
description: "Composition project directory or .html file (pass 2+ paths)",
required: false,
},
at: {
type: "string",
description: "Timeline time in seconds to seek before screenshotting each variant",
},
labels: {
type: "string",
description: "Comma-separated labels matching the variant path count",
},
out: {
type: "string",
description: "Output comparison sheet path (default: ./compare.png)",
},
cols: {
type: "string",
description: "Grid columns (default: sqrt heuristic, capped at 4)",
},
timeout: {
type: "string",
description: "Render-ready timeout in ms per variant (default: 5000)",
},
json: {
type: "boolean",
description: "Output result as JSON",
default: false,
},
},
async run({ args }) {
const jsonRequested = args.json === true;
try {
const parsed = parseCompareArgs(args);
if (!parsed.json) {
console.log(
`${c.accent("◆")} Rendering ${Math.min(parsed.variants.length, MAX_COMPARE_VARIANTS)} of ${parsed.variants.length} composition variants`,
);
}
const payload = await renderCompareSheet(parsed);
if (parsed.json) {
printJson(payload);
} else {
console.log(`\n${c.success("◇")} Comparison sheet saved to ${payload.sheet}`);
}
} catch (err) {
const message = normalizeErrorMessage(err);
if (jsonRequested) {
printJson({ ok: false, error: message });
} else {
console.error(`\n${c.error("✗")} Compare failed: ${message}`);
}
process.exit(1);
}
},
});