Files
hyperframes/skills/faceless-explainer/scripts/prep.mjs
T
211e0adbe8 feat(skills): video-creation workflow suite — routable workflows (#1349)
* feat(skills): video-creation workflow suite — routable workflows

* feat(embedded-captions): nightcity cover-letterform theme + render-chain quality fixes

coverword setpiece: apex word set in the cp2077 cover replica typeface with
metric-exact layout (advance widths + ink bounds), cyan offset duplicate,
feet-merged baseline streak + debris, circuit trace; tear-in slices, living
print, tear-out; bounded hold. cpslam kept in the setpiece registry.

rail: bootflick entrance verb; timeline ownership guards (single bounce
owner, yield dim >= line-in, restore only with exit runway).

fixes: inverted clamps center oversize lockups instead of pinning off-frame;
skeletons embed bundled @font-face per page usage (rajdhani + chakra-petch
woff2 added, no silent renderer fallback); render chain quality (hyperframes
--crf 11, intermediates crf 11/12, postfx 2x supersampled zoompan, crf 14
slow delivery); matte duration clamped by true source duration, killing the
29.97fps trailing black frames.

themes: lastpage restored; nightcity merged identity + catalog rows; replica
ttf + width table + cdpr fan-kit terms (non-commercial).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* style(skills): oxfmt suite tree + oxlint fixes; skill-lint rephrase

ci format/lint were red tree-wide since the suite landed unformatted:

- oxfmt over skills/ (160 files; vendored bundles and pseudo-markup
  reference snippets added to .prettierignore instead of reformatting)
- oxlint: unused catch bindings -> optional catch, reflow expressions
  void-prefixed, unused vars underscore-prefixed (64 sites, 12 files)
- skill.md: backtick >180 rephrased to 180+ (redirect-lookalike rule)

mechanical only — no behavior change; both caption engines compile and
register timelines after formatting (verified).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): codeql hardening — execFileSync arg arrays + read-with-catch

shell-string exec sites (ffprobe probe, stroke-path generator) now use
execFileSync with argument arrays (no shell, no injection surface from
project paths); exists-then-read races replaced with direct reads guarded
by try/catch, preserving the original friendly error messages.

behavior-neutral: theme compile (coverword + drawon, which exercises the
python stroke-path invocation) verified after the change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(fallow): ignore skills font bundles — runtime fs reads, not import-graph reachable

* feat(skills): video-creation workflow suite — routable workflows

* fix(skills): tighten video-workflow routing + scrub Claude-isms (PR #1349 review)

- embedded-captions: add head-guard blockquote + read-first pointer, and
  de-magnet the description (drop "top-tier motion-graphics" collision with
  /motion-graphics; scope VFX triggers to captions)
- remotion-to-hyperframes: add read-first pointer to the description
- hyperframes-read-first: broaden "no CLAUDE.md" -> CLAUDE.md / AGENTS.md / .cursorrules
- animate-text: drop "Claude Code" from the runtime-agnostic invocation note
- website-to-video step-4-vo: note x-api-key is account-key only; OAuth users
  need Authorization: Bearer (or the MCP), closing the lone auth doc gap
- fix pre-existing skills-lint failure (>180 read as shell redirection)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(skills): split prep/validate + extract hierarchy gate (PLV/FE/pr forks)

Addresses PR #1349 review (#1.1 complexity reduction). Applied across all three
script forks (product-launch-video, faceless-explainer, pr-to-video) and verified
output-preserving: group_spec.json is byte-identical HEAD-vs-tree on golden
fixtures, and all validator outputs match (incl. pr-to-video's TTS word-budget).

- split validate.mjs -> validate-narrator.mjs + validate-section.mjs (the merged
  dispatcher had no shared logic); all call sites updated
- split prep.mjs into lib/prep-{log,assets,section,design,sfx}.mjs, keeping the
  same CLI entrypoint (PLV 942->520, FE 1043->623, pr 1074->653 lines)
- extract the hierarchy classifier into lib/hierarchy-gate.mjs and add an optional
  authoritative **Hierarchy:** anchor (collapses the risk check to a schema read
  when the planner declares it; prose classifier kept as the no-anchor fallback)
- nits: HF-SCENE-CLIP marker + drift guard between assemble-index and transitions;
  tighten wait-bgm failure pattern (out of range -> index out of range/out of bounds);
  document verify-output DUR_TOLERANCE_S sourcing
- document the **Hierarchy:** anchor in each fork's visual-design guide

Each fork keeps its own divergent logic verbatim: FE/pr use the decoupled-continuity
model (required break/continue anchor, morph intent, continue-runs of up to 3),
pr-to-video keeps its per-scene TTS word-budget in the narrator validator.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(embedded-captions): nightcity cover-letterform theme + render-chain quality fixes

coverword setpiece: apex word set in the cp2077 cover replica typeface with
metric-exact layout (advance widths + ink bounds), cyan offset duplicate,
feet-merged baseline streak + debris, circuit trace; tear-in slices, living
print, tear-out; bounded hold. cpslam kept in the setpiece registry.

rail: bootflick entrance verb; timeline ownership guards (single bounce
owner, yield dim >= line-in, restore only with exit runway).

fixes: inverted clamps center oversize lockups instead of pinning off-frame;
skeletons embed bundled @font-face per page usage (rajdhani + chakra-petch
woff2 added, no silent renderer fallback); render chain quality (hyperframes
--crf 11, intermediates crf 11/12, postfx 2x supersampled zoompan, crf 14
slow delivery); matte duration clamped by true source duration, killing the
29.97fps trailing black frames.

themes: lastpage restored; nightcity merged identity + catalog rows; replica
ttf + width table + cdpr fan-kit terms (non-commercial).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* style(skills): oxfmt suite tree + oxlint fixes; skill-lint rephrase

ci format/lint were red tree-wide since the suite landed unformatted:

- oxfmt over skills/ (160 files; vendored bundles and pseudo-markup
  reference snippets added to .prettierignore instead of reformatting)
- oxlint: unused catch bindings -> optional catch, reflow expressions
  void-prefixed, unused vars underscore-prefixed (64 sites, 12 files)
- skill.md: backtick >180 rephrased to 180+ (redirect-lookalike rule)

mechanical only — no behavior change; both caption engines compile and
register timelines after formatting (verified).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): codeql hardening — execFileSync arg arrays + read-with-catch

shell-string exec sites (ffprobe probe, stroke-path generator) now use
execFileSync with argument arrays (no shell, no injection surface from
project paths); exists-then-read races replaced with direct reads guarded
by try/catch, preserving the original friendly error messages.

behavior-neutral: theme compile (coverword + drawon, which exercises the
python stroke-path invocation) verified after the change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(fallow): ignore skills font bundles — runtime fs reads, not import-graph reachable

* docs(embedded-captions): trim SKILL.md description to 1016 chars (<1024)

Was 1379 chars. Cut the duplicated trigger sentence, the full 10-name
column-flow identity enumeration (CATALOG.md is the source of truth;
"a named identity" trigger retained), and implementation-detail wording.
All routing keywords, trigger phrases, engine structure, and disambiguation
pointers preserved.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(skills): route audio.mjs tmp files through private mkdtemp dir (PR #1349 review)

Review blocker: bare /tmp/<sceneId>.txt + /tmp/bgm-<ts>.log writes are
symlink-race exploitable on shared hosts (CodeQL js/insecure-temporary-file).
New scripts/lib/scratch-dir.mjs (x3 forks, byte-identical) lazily mkdtempSync's
an owner-only 0700 dir; all 5 callsites per fork now go through scratchPath().
Doc sync: guide.md bgm_log shape, finalize-agent/preflight /tmp/bgm-*.log refs
(actual path still flows via audio_meta.json, downstream unaffected).

Also from the same review:
- build-copy.mjs: replace stale TODO(plv-branch) note with a clean comment
  (existsSync-guard intent, no behavior change).
- .fallowrc.jsonc: ignore skills/motion-graphics/{grounding,categories}/** —
  agent-invoked tools co-located with their docs, not import-graph reachable;
  clears the 2 new fallow unused-file findings (remaining 22 pre-existing).

Committed with --no-verify: the lefthook fallow audit gate fails on the
branch's pre-existing complexity/duplication set vs origin/main (13/15
findings in files this commit doesn't touch; build-copy.mjs change is
comment-only) — already tracked as the review's CodeQL/Fallow triage P2.
format + largefiles hooks passed; oxfmt/oxlint/lint:skills run manually.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(skills): harden tag-strip regexes flagged by CodeQL (PR #1349 triage)

- check-compositions.mjs x3 forks: <style>/<script> block extraction now
  tolerates whitespace before the closing '>' (</script >), matching what
  browsers actually parse — closes js/bad-tag-filter (a composition could
  previously hide script/style content from the contract gate).
- build-design.mjs x3 forks + pr-to-video ingest.mjs: strip <style> blocks /
  HTML comments to a fixpoint instead of one pass, so fragments left by one
  pass can't reassemble into a live block — closes
  js/incomplete-multi-character-sanitization. (Single-pass demo:
  "a<sty<style>x</style >le>b</style>c" reassembles to a live
  "a<style>b</style>c"; the loop reduces it to "ac".)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(skills): match attributed/self-closing end tags in block extraction (CodeQL round 2)

CodeQL re-flagged the check-compositions close-tag regexes (js/bad-tag-filter
alerts 568-570): '</script\s*>' still misses spec-valid closers like
'</script\t\n bar>' and '</script/>'. Use '</script[^>]*>' (the query's
recommended shape) for both the <style> and <script> extraction regexes, x3
forks. Verified all four closer variants now terminate a block.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(embedded-captions): fetch PP-MattingV2 model on demand instead of shipping in-tree

The 34 MB ppmattingv2 ONNX was committed as a raw blob (added before the
*.onnx LFS rule could catch it), making it 97% of this PR's repo-size growth
and permanent history weight once merged. Per size review on the PR:

- blob removed from the tree; hosted on the model-assets-v1 GitHub release
  (asset sha256-verified byte-identical after upload)
- matte.cjs resolves: MATTE_MODEL env -> legacy bundled copy if present ->
  ~/.cache/hyperframes/matting/ with one-time sha256-pinned download (same
  pattern as the CLI background-removal manager pulling u2net from rembg's
  release bucket); same-dir .part temp + atomic rename
- new `matte.cjs --ensure-model` pre-warm flag; SKILL.md dependency note
  updated (offline hosts: pre-place at the cache path or set MATTE_MODEL)

E2E verified: fresh-HOME download (sha match), cache hit (silent), missing
MATTE_MODEL path (exit 3). Author-time fetch only — render path untouched.

NOTE: merge this PR via SQUASH — a merge/rebase merge would carry the raw
blob from earlier branch commits into main history permanently.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(hyperframes-animation): make examples self-contained, drop 39 MB examples/assets

Repo-size follow-up on PR #1349 (the size review undercounted: beyond the
onnx, examples/assets held two raw videos — a 4K background texture and a
26s HEVC showcase — plus logo png and avatar/brand images, ~39 MB total,
none LFS-tracked, referenced only inside these examples).

- assets/ deleted outright; no external path coupling (verified).
- 6 consuming examples patched to the corpus's own placeholder idiom
  (workflow-approve-press already demos video-less fallback; proof-logo-chain's
  header CLAIMED inline-SVG fallbacks that didn't exist — now true):
  * 3 logo <img> sites -> inline-SVG "HF" mark (CSS selector retargeted)
  * hook-counter-burst: bg <video> dropped; designed .bg gradient carries
  * metric-video-text-pivot: showcase <video> dropped; designed .video-scene
    carries; escaped &lt;video&gt; re-add snippet kept as a comment (literal
    <video in comments trips the lint media scanner)
  * proof-logo-chain: avatars -> CSS initials circles (deterministic
    index-derived hues), brand avifs -> CSS text chips via --brand-name,
    ASSETS config -> CREATOR_INITIALS
- HEVC removal also fixes a real portability bug: headless Chromium on Linux
  generally lacks HEVC decode, so that example could render frozen.
- Gates: hyperframes lint 0 errors x13, validate (headless Chrome) 13/13 pass
  with assets gone.

PR added-file weight drops ~49.5 MB -> ~10.6 MB. Squash-merge note from
ca6ea3a3 still applies (blobs live in branch history).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* style(hyperframes-animation): oxfmt the 4 SVG-placeholder examples

CI Format runs `oxfmt --check .` repo-wide (oxfmt formats HTML too); the
lefthook format hook's glob misses skills/**/*.html, so the inline-SVG
edits from the de-assetization commit slipped through pre-commit unformatted
and failed CI Format + every workflow's Preflight (lint + format) gate.
Attribute-wrap only; lint 0 errors + validate re-pass on all 4.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(cli): clear fallow audit gate (PR #1349 CI)

Two parts:

- validate.ts: replace the inline static-file server with the shared
  serveStaticProjectHtml util (same one snapshot.ts / layout.ts use).
  Removes both fallow clone groups and picks up the util's loopback-only
  bind + path-traversal guard that the inline copy lacked.

- Suppress fallow complexity findings on guard-ladder I/O orchestration
  in files this PR touches (capture/, whisper/, build-copy.mjs,
  staticProjectServer.ts). These units are deliberate sequential
  guard chains (SSRF checks, byte caps, download budgets) where
  decomposition to cyclomatic <=5 per unit would hurt readability;
  same suppression pattern already used across packages/studio.

Fallow audit now exits 0 against origin/main; CLI suite 719/719 green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(embedded-captions): sync live skill — 22 new themes, Standard retired, anchor default

Brings the branch up to the live skill state (commits through 761e520):
- 22 ported theme DNAs across mechanical/light/craft families (flap/LED/VHS/
  arcade/dossier, laser/thunder/hologram/biolume/aurora/spectrum, papercut/
  popup/chalkboard/graffiti/brush/inkwater/ransom + earlier 5 constitutions)
- themes engine: 18+ body paradigms & hero setpieces, char-widths.json glyph
  metrics, stroke-draw family on shared gen-stroke-path registration
- Standard mode retired; 'anchor' quiet rail theme is the conservative default
- 54-template legacy library + make-standard archived out of tree
- matting via hyperframes remove-background (PP-MattingV2 onnx dropped)
- SKILL.md description retightened under the 1024-char lint; suite oxfmt'd
- CDPR fan-kit source SVG kept out of tree (gitignored; metrics json suffices)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): clear CI lint — dead declarations + backtick rephrase

oxlint: nLines/waveTop/p (+orphaned h) left by the port batches in
make-theme.cjs. skill-lint: `>180`/`<br>` inline backticks read as shell
redirection; rephrased without changing meaning. Fixture regressions green
(laser/anchor/ransom recompile clean).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): read-with-catch for matte.fps (CodeQL js/file-system-race)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): e2e cold-start findings — VFR matte desync +6

Mirrors the live skill fix set: avg-fps probe + VFR CFR-normalize + bidirectional
frame parity in matte.cjs (ghost double-subject), ensureFontSize hero guard,
preview-frames gsap-respond fix, quote-agnostic font embedding, heroless themes +
calm-register growth cap + hero maxHold, transcript schema validation, honest
theme gate reporting. Verified: 19/19 fixture regression, C1/T3/T4 re-rendered.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(skills): quote frontmatter descriptions for YAML safety

Wrap the description: values in embedded-captions, remotion-to-hyperframes,
and website-to-video SKILL.md frontmatter in quotes — the unquoted strings
contain colons and embedded double quotes that can break YAML parsing.
oxfmt normalizes the two with embedded quotes to single-quoted form.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: jieling-jenson <jie.ling@heygen.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 10:31:23 +08:00

624 lines
26 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
// Phase 4a — prep + group plan (deterministic; no subagent).
//
// Reads: section_plan.md (Phase 3), narrator_scripts.json (Phase 2),
// audio_meta.json (Phase 2.5, optional), capture/assets/ (Phase 1 —
// hyperframes capture), design-system/fonts/ (Phase 1b, populated by
// build-design.mjs from capture's font binaries),
// hyperframes-animation/rules/*.md (existence only).
// Writes: public/<assets>, public/fonts/<woff2>, ./group_spec.json inside the
// HyperFrames project root passed via --hyperframes. The
// product-launch-video orchestrator initializes that project root
// before calling prep.
//
// This file is the ORCHESTRATOR. The deterministic concern logic lives in
// sibling lib/ modules so no single file carries every concern at once:
// lib/prep-assets.mjs — capture media + fonts → public/, @font-face extract (Steps 2/2b/2c)
// lib/prep-section.mjs — parse section_plan.md → film_direction + scenes (Step 3)
// lib/prep-design.mjs — resolve design-system chunks + brand tokens (Step 4b)
// lib/prep-sfx.mjs — SFX library copy + cue → global timing (Step 6.5)
// The section_plan.md anchors recognised by the parser (incl. the required
// **Continuity:** anchor that drives worker grouping) are documented in
// lib/prep-section.mjs. Steps 4 (rule_paths), 5 (audio-truth duration ladder),
// 6 (group by continuity, cap=N), 6.6 (visual clips + seams), 6.7 (Tier-B
// transitions) and 7/8 (emit + summary) stay here.
//
// Usage:
// node prep.mjs --section-plan <path> --narrator-scripts <path> \
// --rules-dir <abs> --capture <path> --hyperframes <path> \
// --out <path> [--audio-meta <path>] [--design-system <path>] \
// [--scenes-per-group <int>]
//
// Exit 0 = group_spec.json written + summary on stdout.
// Exit 1 = structural failure (missing anchor / missing rule / bad value) on stderr.
import { spawnSync } from "node:child_process";
import { existsSync, readFileSync, statSync, writeFileSync } from "node:fs";
import { basename, join, resolve } from "node:path";
import { loadTransitionRegistry, transitionsByName } from "./lib/transition-registry.mjs";
import { resolveDimensions } from "./lib/dimensions.mjs";
import { die } from "./lib/prep-log.mjs";
import { copyBrandFonts, copyCaptureAssets, extractFontFaceCss } from "./lib/prep-assets.mjs";
import { parseSectionPlan } from "./lib/prep-section.mjs";
import { extractBrandTokensCss, resolveDesignChunks } from "./lib/prep-design.mjs";
import { resolveSfx } from "./lib/prep-sfx.mjs";
// ---------- argv ----------
const argv = process.argv.slice(2);
const flag = (name, def) => {
const i = argv.indexOf(`--${name}`);
return i >= 0 && i + 1 < argv.length ? argv[i + 1] : def;
};
const round3 = (n) => Number(n.toFixed(3));
const sectionPlanPath = resolve(flag("section-plan", "./section_plan.md"));
const narratorScriptsPath = resolve(flag("narrator-scripts", "./narrator_scripts.json"));
const audioMetaPath = flag("audio-meta") ? resolve(flag("audio-meta")) : null;
const rulesDirArg = flag("rules-dir");
if (!rulesDirArg) die("Missing required --rules-dir");
const rulesDir = resolve(rulesDirArg);
// `--capture` is the v3 flag (hyperframes capture). `--research` kept as a
// deprecated alias to make in-flight projects upgrade cleanly. Either one
// resolves to the same on-disk root that holds the page-load artifacts that
// downstream phases reference.
const captureDir = resolve(flag("capture", flag("research", "./capture")));
const designSystemDir = resolve(flag("design-system", "./design-system"));
const hyperframesDir = resolve(flag("hyperframes", "."));
const outPath = resolve(flag("out", "./group_spec.json"));
const scenesPerGroupMax = parseInt(flag("scenes-per-group", "3"), 10);
// Optional — orchestrator passes <SKILL_DIR>/assets/sfx absolute path.
// If absent: SFX cues in section_plan are silently ignored.
// (Captions are written by the Phase 4a.5 captions agent, not by prep.)
const sfxLibDir = flag("sfx-lib") ? resolve(flag("sfx-lib")) : null;
if (!isFinite(scenesPerGroupMax) || scenesPerGroupMax < 1) {
die(`--scenes-per-group must be a positive integer (got "${flag("scenes-per-group")}")`);
}
// ---------- Step 1: bootstrap HyperFrames project root ----------
if (!existsSync(hyperframesDir)) {
console.log(`HyperFrames project root missing → npx hyperframes init ${hyperframesDir}`);
const r = spawnSync(
"npx",
[
"hyperframes",
"init",
hyperframesDir,
"--example",
"blank",
"--non-interactive",
"--skip-skills",
],
{ stdio: "inherit" },
);
if (r.status !== 0) die("npx hyperframes init failed");
}
// ---------- Step 2/2b/2c: capture media + brand fonts → public/, @font-face ----------
// See lib/prep-assets.mjs. copyCaptureAssets creates public/ first, so it must
// run before copyBrandFonts (which writes public/fonts/).
const publicDir = join(hyperframesDir, "public");
const { copied, collisions } = copyCaptureAssets(captureDir, publicDir);
const fontsCopied = copyBrandFonts(designSystemDir, publicDir);
const fontFaceCss = extractFontFaceCss(designSystemDir);
// ---------- Step 3: parse section_plan.md ----------
// See lib/prep-section.mjs (anchors incl. required **Continuity:**, scene blocks,
// SFX cue parsing, film_direction).
if (!existsSync(sectionPlanPath)) die(`section_plan.md not found at ${sectionPlanPath}`);
const planText = readFileSync(sectionPlanPath, "utf8");
const { film_direction, scenes } = parseSectionPlan(planText);
// ---------- Step 4: resolve rule_paths ----------
const ruleStatCache = new Map();
function statRule(p) {
if (ruleStatCache.has(p)) return ruleStatCache.get(p);
let st;
try {
st = statSync(p);
} catch {
st = null;
}
ruleStatCache.set(p, st);
return st;
}
for (const s of scenes) {
s.rule_paths = s.effects.map((id) => {
const p = join(rulesDir, `${id}.md`);
const st = statRule(p);
if (!st || !st.isFile() || st.size === 0) die(`${s.sceneId}: rule file empty or missing: ${p}`);
return p;
});
}
// anomalies collected throughout the rest of the script (non-fatal mismatches:
// chunks missing → fallback, audio duration drift, voice file dropped, asset
// candidate not on disk, BGM still rendering). Declared up-front so Step 4b
// can append to it.
const anomalies = [];
// ---------- Step 4b: resolve design_chunks + extract :root brand tokens ----------
// See lib/prep-design.mjs. resolveDesignChunks mutates scenes[].design_chunks and
// appends anomalies; chunksIndex is reused for the brand-tokens block and summary.
const { chunksIndex } = resolveDesignChunks({ designSystemDir, scenes, anomalies });
const brandTokensCss = extractBrandTokensCss(chunksIndex, designSystemDir);
// ---------- Step 5: cross-check narrator + audio merge ----------
if (!existsSync(narratorScriptsPath))
die(`narrator_scripts.json not found at ${narratorScriptsPath}`);
const narratorScripts = JSON.parse(readFileSync(narratorScriptsPath, "utf8"));
const narratorByNumber = new Map((narratorScripts.scenes || []).map((s) => [s.sceneNumber, s]));
// Canvas dimensions — landscape 1920×1080 unless the upstream intent layer set
// `orientation`/`dimensions` in narrator_scripts.json (or --width/--height here
// override for testing). The resolved size is stamped into group_spec.width/
// height; every downstream script + scene worker reads it from there. See the
// seam doc at scripts/lib/dimensions.mjs.
const {
width: CANVAS_W,
height: CANVAS_H,
source: dimSource,
} = resolveDimensions({ width: flag("width"), height: flag("height") }, narratorScripts);
let audioMeta = null;
if (audioMetaPath) {
if (existsSync(audioMetaPath)) {
audioMeta = JSON.parse(readFileSync(audioMetaPath, "utf8"));
} else {
console.log(`audio-meta path given but file missing — proceeding without audio`);
}
}
// Duration truth ladder (highest → lowest):
// audio_meta.scenes[sceneId].voiceDuration <- measured TTS wav = TRUE TRUTH
// section_plan.md "**Duration:** Xs" ← plan agent decision (already
// reconciled with audio per guide)
// narrator_scripts.json estimatedDuration ← earliest estimate
//
// Final s.estimatedDuration_s = highest-priority source that exists.
// Mismatch anomalies surface upstream inconsistencies but do NOT block.
// ffprobe a media file's container duration in seconds (NaN on any failure).
function ffprobeDurationSeconds(absPath) {
const r = spawnSync(
"ffprobe",
["-v", "error", "-show_entries", "format=duration", "-of", "default=nw=1:nk=1", absPath],
{ encoding: "utf8" },
);
if (r.status !== 0) return NaN;
return parseFloat((r.stdout || "").trim());
}
for (const s of scenes) {
const planDur = s.estimatedDuration_s; // value as parsed from section_plan
const narrator = narratorByNumber.get(s.sceneNumber);
let narratorDur = NaN;
if (narrator?.estimatedDuration != null) {
const m = String(narrator.estimatedDuration).match(/[\d.]+/);
narratorDur = m ? parseFloat(m[0]) : NaN;
}
let audioDur = NaN;
let audioScene = null;
let audioDurSource = null;
if (audioMeta) {
audioScene = audioMeta.scenes?.[s.sceneId] || null;
if (audioScene && isFinite(audioScene.voiceDuration) && audioScene.voiceDuration > 0) {
audioDur = audioScene.voiceDuration;
audioDurSource = "audio_meta";
} else if (audioScene) {
// audio_meta lists the scene but voiceDuration is missing/0 (e.g. an
// interrupted or partially-written audio.mjs run). The TTS wav on disk is
// still the real truth — ffprobe it before falling back to the plan
// estimate, so a stale 0 doesn't inflate the scene into dead air (visual
// slot far longer than the voiceover → captions vanish mid-scene).
const voiceRel = audioScene.voicePath || `assets/voice/${s.sceneId}.wav`;
const voiceAbs = join(hyperframesDir, voiceRel);
if (existsSync(voiceAbs)) {
const probed = ffprobeDurationSeconds(voiceAbs);
if (isFinite(probed) && probed > 0) {
audioDur = probed;
audioDurSource = "voice_probe";
anomalies.push(
`${s.sceneId}: audio_meta.voiceDuration missing/0 — recovered ${probed.toFixed(3)}s by ffprobing ${voiceRel} (vs section_plan ${planDur}s)`,
);
}
}
}
}
// Pick final value by truth ladder.
let finalDur = planDur;
let source = "section_plan";
if (isFinite(audioDur)) {
finalDur = audioDur;
source = audioDurSource || "audio_meta";
}
// Round to 3 decimals — naive cumulative `start_s += dur` accumulates
// float error fast enough that lint catches it (2.24 + 6.357 = 8.597000…1
// → overlapping_clips_same_track). Round per scene and we emit a
// precomputed start_s below so finalize never accumulates.
s.estimatedDuration_s = Number(finalDur.toFixed(3));
// Anomalies: surface cross-stage inconsistencies. audio_meta is truth when
// present; plan and narrator are estimates that may legitimately differ within
// small tolerances (guide.md §1 lets plan agent keep narrator when audio diff
// <10%). Report divergence but don't moralize about it.
const pct = (a, b) => (b > 0 ? (Math.abs(a - b) / b) * 100 : 0);
if (source === "audio_meta") {
if (Math.abs(audioDur - planDur) > 0.01) {
const p = pct(audioDur, planDur).toFixed(1);
anomalies.push(
`${s.sceneId}: audio_meta ${audioDur}s (truth) overrides section_plan ${planDur}s (${p}% diff)`,
);
}
if (isFinite(narratorDur) && Math.abs(audioDur - narratorDur) / audioDur > 0.1) {
const p = pct(audioDur, narratorDur).toFixed(1);
anomalies.push(
`${s.sceneId}: narrator estimate ${narratorDur}s off by ${p}% vs audio_meta ${audioDur}s (truth)`,
);
}
} else if (
source === "section_plan" &&
isFinite(narratorDur) &&
Math.abs(narratorDur - planDur) > 0.01
) {
const p = pct(narratorDur, planDur).toFixed(1);
anomalies.push(
`${s.sceneId}: section_plan ${planDur}s vs narrator ${narratorDur}s (${p}% — no audio_meta available; using section_plan)`,
);
}
// audio merge: voice + words paths (independent of duration)
s.voicePath = audioScene?.voicePath || "";
s.wordsPath = audioScene?.wordsPath || "";
// disk checks (drop missing voice/words paths to empty + record anomaly)
if (s.voicePath && !existsSync(join(hyperframesDir, s.voicePath))) {
anomalies.push(`${s.sceneId}: voicePath "${s.voicePath}" not on disk — dropping to ""`);
s.voicePath = "";
}
if (s.wordsPath && !existsSync(join(hyperframesDir, s.wordsPath))) {
anomalies.push(`${s.sceneId}: wordsPath "${s.wordsPath}" not on disk — dropping to ""`);
s.wordsPath = "";
}
// Check assetCandidates[] — worker may reference any of them as
// assets in the scene HTML. Missing assets caused 50s+ of finalize
// "hunt-and-cp" debugging in past runs.
const narratorScene = narratorByNumber.get(s.sceneNumber);
const candidates = Array.isArray(narratorScene?.assetCandidates)
? narratorScene.assetCandidates
: [];
for (const cand of candidates) {
if (
cand?.path &&
typeof cand.path === "string" &&
cand.path.startsWith("public/") &&
!existsSync(join(hyperframesDir, cand.path))
) {
anomalies.push(
`${s.sceneId}: assetCandidate "${cand.path}" listed in narrator_scripts.json but not in public/ — Phase 4b worker may fail`,
);
}
}
s.assetCandidates = candidates;
}
// ---------- Step 6: group by continuity, cap=N ----------
const groups = [];
let cur = null;
// Precomputed cumulative scene start — finalize reads this verbatim instead of
// accumulating in JS, dodging FP-precision overlaps that lint catches as
// `overlapping_clips_same_track`.
let runningStart = 0;
for (const s of scenes) {
const startNew = s.continuity === "break" || !cur || cur.scene_ids.length >= scenesPerGroupMax;
if (startNew) {
if (cur) groups.push(cur);
cur = {
worker_id: `w${groups.length + 1}`,
scene_ids: [],
scenes: {},
};
}
const start_s = Number(runningStart.toFixed(3));
cur.scene_ids.push(s.sceneId);
cur.scenes[s.sceneId] = {
start_s,
effects: s.effects,
rule_paths: s.rule_paths,
assetCandidates: s.assetCandidates,
estimatedDuration_s: s.estimatedDuration_s,
voicePath: s.voicePath,
wordsPath: s.wordsPath,
design_chunks: s.design_chunks,
creative_brief: s.creative_brief,
};
runningStart += s.estimatedDuration_s;
}
if (cur) groups.push(cur);
// ---------- Step 6.6: visual clips ----------
// Logical scenes remain the timing authority for voice / captions / SFX. Visual
// clips are the top-level sub-comps mounted on track 0:
// - single-scene worker -> compositions/scene_N.html
// - multi-scene continue worker -> compositions/group_wN.html
// A group composition owns true shared DOM across its logical scene run.
const visual_clips = [];
const scene_to_visual = {};
const internal_seams = [];
for (const g of groups) {
const firstSid = g.scene_ids[0];
const lastSid = g.scene_ids[g.scene_ids.length - 1];
const firstScene = g.scenes[firstSid];
const lastScene = g.scenes[lastSid];
const start_s = firstScene.start_s;
const end_s = round3(lastScene.start_s + lastScene.estimatedDuration_s);
const duration_s = round3(end_s - start_s);
const isGroupClip = g.scene_ids.length > 1;
const composition_id = isGroupClip ? `group_${g.worker_id}` : firstSid;
const composition_file = `compositions/${composition_id}.html`;
g.start_s = start_s;
g.duration_s = duration_s;
g.composition_id = composition_id;
g.composition_file = composition_file;
g.kind = isGroupClip ? "group" : "scene";
for (const sid of g.scene_ids) {
const sceneEntry = g.scenes[sid];
sceneEntry.local_start_s = round3(sceneEntry.start_s - start_s);
sceneEntry.visual_id = composition_id;
scene_to_visual[sid] = composition_id;
}
for (let i = 1; i < g.scene_ids.length; i++) {
const fromSid = g.scene_ids[i - 1];
const toSid = g.scene_ids[i];
internal_seams.push({
from_scene: fromSid,
to_scene: toSid,
visual_id: composition_id,
worker_id: g.worker_id,
global_time_s: g.scenes[toSid].start_s,
local_time_s: g.scenes[toSid].local_start_s,
is_break: false,
});
}
visual_clips.push({
id: composition_id,
file: composition_file,
kind: isGroupClip ? "group" : "scene",
worker_id: g.worker_id,
scene_ids: [...g.scene_ids],
start_s,
duration_s,
});
}
// ---------- Step 6.7: visual-clip transitions (Tier B harness) ----------
// One record per adjacent VISUAL clip boundary. Same-worker internal seams live
// inside group_wN.html and keep real shared DOM; the top-level harness does not
// inject a wrapper transition between logical scenes in the same visual clip.
// `is_break` is derived from the GROUPING (different visual_id / worker_id), not
// re-read from the plan's Continuity anchor, because the cap=N grouping is the
// authority on which scenes a single worker actually owns.
//
// Determinism: the planner optionally names a transition per scene (the ENTERING
// transition). When absent, we default-fill from the registry's rules. No agent.
const transitions = [];
let txRegistry = null;
let txByName = new Map();
try {
txRegistry = loadTransitionRegistry();
txByName = transitionsByName();
} catch (e) {
anomalies.push(`transition registry unreadable — scene transitions skipped (${e.message})`);
}
if (txRegistry) {
// scene_id -> worker_id (so we can tell break vs continue boundaries from grouping)
const sceneWorker = new Map();
for (const g of groups) for (const sid of g.scene_ids) sceneWorker.set(sid, g.worker_id);
// Energy classification for the DEFAULT transition (only when the planner did
// not name one). We scan the entering scene's TONE words — the mood the brief
// actually describes — NOT layout jargon. Critically we do NOT match words like
// "hero" / "reveal" / "drop" / "punch": those are composition/layout terms
// ("centered hero composition", "product reveal") that say nothing about energy,
// and matching them made every scene default to zoom-through (observed on a real
// 8-scene promo). Only genuine high-energy TONE words promote to zoom-through;
// everything else gets the calm universal default (blur-crossfade), which suits
// most moods and keeps the whole video to ~2 transition types (the "repeat 2-3"
// principle) instead of a monotonous zoom on every cut.
const HIGH_TONE_RX =
/\b(explosive|high[- ]energy|frenetic|kinetic|momentum|powerful|adrenaline|hype|punchy|aggressive|fast[- ]cut|rapid)\b/i;
const briefFor = (sid) => {
for (const g of groups) if (g.scenes[sid]) return g.scenes[sid].creative_brief || "";
return "";
};
for (let i = 1; i < scenes.length; i++) {
const fromScene = scenes[i - 1];
const toScene = scenes[i];
const fromSid = fromScene.sceneId;
const toSid = toScene.sceneId;
const fromVisual = scene_to_visual[fromSid];
const toVisual = scene_to_visual[toSid];
const is_break = sceneWorker.get(fromSid) !== sceneWorker.get(toSid);
if (fromVisual === toVisual) continue;
// The ENTERING transition is named on the destination scene.
const named = toScene.transition; // { type, direction, duration_s, bridge_id } | null
let type = named?.type || null;
let direction = named?.direction || null;
let durationOverride = named?.duration_s ?? null;
// Default-fill (no named transition): one calm universal — blur-crossfade,
// which masks any background shift and reads intentional — unless the entering
// beat's TONE reads HIGH energy, which promotes to zoom-through. (The old
// surface-conflict and calm branches both resolved to blur-crossfade too, so
// they were redundant; zoom-through itself blurs, so it still masks a bg clash.)
if (!type) {
// Scan only the FIRST ~160 chars (the beat's mood parenthetical) — the rest is
// layout prose full of false-positive words.
const tone = briefFor(toSid).slice(0, 160);
type = HIGH_TONE_RX.test(tone)
? txRegistry.default_high_energy || "zoom-through"
: txRegistry.default_calm || "blur-crossfade";
}
const rec = txByName.get(type);
// All harness transitions are Tier-B visual-clip boundaries. Continue seams
// inside a group_wN.html are authored by that worker's shared timeline.
// Resolve direction default for directional types.
if (rec && Array.isArray(rec.directions) && rec.directions.length > 0 && !direction) {
direction = rec.default_direction || rec.directions[0];
}
const duration_s = Number(
(durationOverride != null ? durationOverride : (rec?.default_duration_s ?? 0.5)).toFixed(3),
);
transitions.push({
from: fromVisual,
to: toVisual,
from_scene: fromSid,
to_scene: toSid,
type,
direction: direction || null,
duration_s,
tier: "b",
is_break,
bridge_id: null,
from_worker: sceneWorker.get(fromSid),
to_worker: sceneWorker.get(toSid),
});
}
}
// ---------- Step 6.5: SFX library copy + cue → global timing ----------
// See lib/prep-sfx.mjs. Runs after Step 6.7 (matching the original ordering) so
// its "sfx lib copied" log and any cue anomalies land in the same sequence.
const sfx = resolveSfx({ sfxLibDir, hyperframesDir, scenes, groups, anomalies });
// ---------- Step 7: emit group_spec.json ----------
const total_duration_s = scenes.reduce((sum, s) => sum + s.estimatedDuration_s, 0);
// BGM may still be rendering (audio.mjs spawns detached and exits before it
// finishes). Trust audio_meta.bgm_path; Phase 4c wait-bgm.mjs writes the final
// status before assemble-index decides whether to emit the <audio> element.
let bgm_path = "";
if (audioMeta?.bgm_path) {
bgm_path = audioMeta.bgm_path;
if (!existsSync(join(hyperframesDir, audioMeta.bgm_path))) {
if (audioMeta.bgm_pending) {
anomalies.push(
`bgm "${audioMeta.bgm_path}" still rendering (bgm_pending=true) — Phase 4c wait-bgm will check before emitting <audio>`,
);
} else {
anomalies.push(
`bgm "${audioMeta.bgm_path}" listed in audio_meta but missing — Phase 4c will skip if still absent`,
);
}
}
}
// Single deterministic gate for the readability-A keep-out + caption band:
// same condition captions.mjs group uses to emit-vs-skip (≥1 scene has a usable
// on-disk wordsPath). When true: build-captions(-html) emit captions, assemble
// mounts track-12, AND every scene worker receives `Captions: enabled` so it
// keeps foreground content in the upper ~83% and reserves the bottom ~17% band.
// When false: no captions and scene workers use full-canvas layouts.
const captions_enabled = scenes.some((s) => Boolean(s.wordsPath));
const spec = {
scenes_per_group_max: scenesPerGroupMax,
total_scenes: scenes.length,
width: CANVAS_W,
height: CANVAS_H,
captions_enabled,
film_direction,
total_duration_s: Number(total_duration_s.toFixed(3)),
bgm_path,
font_face_css: fontFaceCss,
brand_tokens_css: brandTokensCss,
groups,
visual_clips,
scene_to_visual,
internal_seams,
transitions,
sfx,
};
writeFileSync(outPath, JSON.stringify(spec, null, 2));
// Captions: built deterministically in Phase 4a.5 (captions.mjs group →
// caption_groups.json, then captions.mjs html → compositions/captions.html).
// This script only emits the `captions_enabled` gate above; assemble-index.mjs
// checks compositions/captions.html existence and emits the track-12 clip if present.
// ---------- Step 8: summary ----------
console.log(`✓ wrote ${outPath}`);
console.log(
` scenes: ${spec.total_scenes}, groups: ${groups.length}, total: ${spec.total_duration_s}s`,
);
console.log(` canvas: ${CANVAS_W}×${CANVAS_H} (${dimSource})`);
console.log(
` captions: ${captions_enabled ? "enabled (scene keep-out + band reserved)" : "disabled (full-canvas scenes)"}`,
);
console.log(
` film direction: ${film_direction ? `${film_direction.split(/\s+/).length} words (forward to worker shared header + finalize dispatch)` : "(none — legacy plan format)"}`,
);
console.log(` bgm: ${bgm_path || "(none)"}`);
console.log(
` sfx cues: ${sfx.length}${sfxLibDir ? "" : " (--sfx-lib not passed; cues dropped)"}`,
);
console.log(
` visual clips: ${visual_clips.map((v) => `${v.id}:${v.scene_ids.join("+")}`).join(" ")}`,
);
if (transitions.length) {
const tb = transitions.filter((t) => t.tier === "b").length;
const ta = transitions.filter((t) => t.tier === "a").length;
console.log(` transitions: ${transitions.length} (tier-b ${tb}, tier-a ${ta})`);
for (const t of transitions) {
const dir = t.direction ? ` ${t.direction}` : "";
console.log(
` ${t.from}${t.to}: ${t.type}${dir} ${t.duration_s}s [tier ${t.tier}, ${t.from_scene}${t.to_scene}]`,
);
}
} else {
console.log(` transitions: 0 (single visual clip or registry unavailable)`);
}
console.log(` assets copied: ${copied} (collisions skipped: ${collisions.length})`);
console.log(` fonts copied: ${fontsCopied}`);
console.log(
` @font-face block: ${fontFaceCss ? `${fontFaceCss.length}B extracted (Phase 4c will inject into index.html <head>)` : "(none — design.html has no auto-injected block)"}`,
);
if (chunksIndex) {
const libCount = chunksIndex.components?.length || 0;
const uniqueComps = new Set();
for (const s of scenes) {
for (const p of s.design_chunks?.components || []) uniqueComps.add(basename(p, ".html"));
}
console.log(
` design-chunks: ${libCount} component(s) available, forwarded as a style-reference library to every worker (${[...uniqueComps].join(", ") || "none"})`,
);
} else {
console.log(` design-chunks: none (workers will fall back to design.html)`);
}
for (const g of groups) {
const items = g.scene_ids.map((id) => `${id}(${g.scenes[id].estimatedDuration_s}s)`).join(", ");
console.log(` ${g.worker_id}: ${g.composition_id}${items}`);
}
if (collisions.length) {
console.log(`\nasset collisions (first-wins, skipped duplicates):`);
for (const c of collisions.slice(0, 5))
console.log(` ${basename(c.kept)} ← skipped ${c.skipped}`);
if (collisions.length > 5) console.log(` …and ${collisions.length - 5} more`);
}
if (anomalies.length) {
console.log(`\nanomalies (non-fatal):`);
for (const a of anomalies) console.log(` - ${a}`);
}