hyperframes icon indicating copy to clipboard operation
hyperframes copied to clipboard

feat(lambda): add Lambda handler, ZIP bundling, and BeginFrame probe

Open jrusso1020 opened this issue 6 months ago • 1 comments

What

Adds a new packages/aws-lambda/ workspace package — the AWS Lambda adapter for HyperFrames distributed rendering. One Lambda handler dispatches on event.Action ∈ {plan, renderChunk, assemble} to the matching OSS primitive from @hyperframes/producer/distributed, plus the ZIP build pipeline and a BeginFrame regression-guard probe.

This is PR 6.1 in the 8-PR Phase 6 stack (see DISTRIBUTED-RENDERING-PLAN.md §11 Phase 6 + §15). Phase 6a (PRs 6.1-6.3) validates the architecture on real AWS; Phase 6b (6.4-6.8) wraps it in the user-facing CLI + CDK construct + docs.

Why

Phase 5 (low-level CLI) was dropped because the real adopter paths are either Temporal (HeyGen, internal) or Lambda (OSS users). For the Lambda path to work, we need a Lambda-deployable artifact that wraps the merged OSS distributed primitives.

The load-bearing architectural assumption is that @sparticuz/chromium's chrome-headless-shell build honours CDP HeadlessExperimental.beginFrame with screenshot: true — the only rendering path the engine knows about post-Phase-1 cleanup. If it didn't, the architecture would change (fall back to bundling our own pinned chrome-headless-shell).

The probe at scripts/probe-beginframe.ts is a permanent regression guard, not a one-shot verification. It runs the probe inside public.ecr.aws/lambda/nodejs:22 — the same image real Lambda uses — and asserts a PNG buffer comes back.

How

Architecture (ZIP deploy, not container image; matches Remotion Lambda's ergonomics without copying their code):

Step Functions state machine
  ├── Plan Lambda          → s3://{bucket}/{planHash}/plan.tar.gz
  ├── Map(N) RenderChunk   → s3://{bucket}/{planHash}/chunks/NNNN.mp4
  └── Assemble Lambda      → s3://{bucket}/{planHash}/output.mp4

dist/handler.zip contents (one Lambda function, three roles):
  handler.mjs                       — esbuild bundle, dispatches on Action
  bin/ffmpeg + bin/ffprobe          — ffmpeg-static + ffprobe-static
  node_modules/@sparticuz/chromium/ — primary Chrome (decompresses to /tmp)
  node_modules/puppeteer-core/      — required for the engine's launch path
  node_modules/tar/                 — pure-JS tar (Lambda image has no tar binary)
  hyperframe.manifest.json          — runtime IIFE manifest the producer's loader resolves
  hyperframe.runtime.iife.js        — same

Handler glue is intentionally thin: parse event → S3 download → call OSS primitive (pure function over local paths) → S3 upload → return small JSON. Result payloads sized for Step Functions history budgets (<200 bytes per chunk per §2.4).

Chrome source is selectable at build time via --source=sparticuz (default) or --source=chrome-headless-shell (fallback). Handler reads HYPERFRAMES_LAMBDA_CHROME_SOURCE env var at boot.

Producer bug fix bundled

While running the eval against the PR 6.3 stack, many-cuts (a fixture with audio) tripped PLAN_HASH_MISMATCH. Diagnosed locally — repros in the producer regardless of Lambda. Root cause: plan() cleans up <planDir>/.plan-work/ AFTER freezePlan() walks the planDir. When the audio mix downloads source mp3s, they land in .plan-work/downloads/ and freezePlan hashes them. Chunk workers see the cleaned planDir (no .plan-work/) so their recomputed hash differs.

Fix: move the .plan-work/ cleanup to BEFORE freezePlan().

Added regression test in plan.test.ts that asserts recomputePlanHashFromPlanDir(planDir) === result.planHash for any plan — catches this whole class of bug going forward.

Sizes (sparticuz source)

  • handler.zip: 124 MB
  • Unzipped: 245 MB (well under the 248 MiB in-house gate, 250 MB Lambda hard cap)
  • ffmpeg 80 MB + ffprobe 62 MB + @sparticuz/chromium 70 MB dominate

ZIP package shape:

  • Lambda Node 22 runtime ships neither tar nor unzip in /usr/bin — we use the tar npm package over node:zlib to handle on-the-wire .tar.gz archives.
  • esbuild ESM's __require shim throws "Dynamic require of path" for any CJS dep that does require('path') at top level (postcss does). Fixed via createRequire banner in the bundle.

Test plan

  • [x] bun run --filter @hyperframes/aws-lambda test — 25 tests pass
  • [x] bun test packages/producer/src/services/distributed/plan.test.ts — 16 tests pass (incl. the new planHash regression test)
  • [x] bunx oxlint + bunx oxfmt --check — clean
  • [x] bun run --cwd packages/aws-lambda build:zip — produces a 124 MB ZIP / 245 MiB unzipped
  • [x] bun run --cwd packages/aws-lambda verify:zip-size — passes
  • [x] Real-AWS eval (via PR 6.3's eval.sh) renders 4 fixtures end-to-end on Lambda; PSNR vs baseline 33-52 dB across all fixtures; speedup 1.44-1.72× at N=4 chunks for compositions ≥ 4s.

🤖 Generated with Claude Code

jrusso1020 avatar May 15 '26 23:05 jrusso1020

This stack of pull requests is managed by Graphite. Learn more about stacking.

jrusso1020 avatar May 15 '26 23:05 jrusso1020