Headless automation
Headless automation lets scripts create, inspect, edit, probe, and render FreeCut projects without mounting the editor UI. A Node driver owns the workspace and request lifecycle, while a real Chrome page runs the same browser render and timeline domains used by the application.
System boundary
The headless system joins three boundaries: workspace files, a browser harness, and an automation contract. The Node side reads project and media resources, the browser side performs domain work, and the driver returns files or structured results to the caller.
The system has two main entry points:
| Entry point | Role | Browser lifetime |
|---|---|---|
| headless/agent.mjs | One command for project, media, edit, and render work | Starts and closes a harness for the command |
| headless/serve.mjs | HTTP service for repeated automation requests | Keeps one ready Chrome page and serializes requests |
The browser harness is a dedicated page, not the editor shell. It exposes a small window.freecut boundary for project creation and normalization, editing, media probing, frame capture, layout inspection, and rendering. This keeps automation close to the real domain behavior without requiring React routes or workspace UI state.
The child pages Headless server and rendering and Programmatic editing describe the browser-facing API and editing entry point in more detail.
Headless lifecycle
The lifecycle starts with a workspace and ends with either a durable resource change or an output artifact. A one-shot agent repeats the setup for each command. The service keeps the browser warm but uses the same resource and contract rules.
This diagram shows how an automation request moves between the workspace, Node driver, browser harness, and result.
flowchart LR
C[CLI or HTTP client] --> V[Validate request]
V --> W[Read workspace resources]
W --> Q[Queue browser operation]
Q --> H[Ready Chrome harness]
H --> R[Render, edit, or probe]
R --> N[Normalize and validate result]
N --> P[Persist with revision guard]
N --> O[Return JSON or output file]
P --> O
Workspace resources
The workspace is the durable source for project JSON, media source files, media metadata, project media links, and service state. Project and media IDs are treated as contained resource names, so callers address resources by ID rather than by arbitrary file path through HTTP.
Mutating service work claims an exclusive writer lock for the workspace. Project and media writes use temporary files and atomic replacement. A content hash becomes the resource revision, which lets a caller detect a change made after it read the resource.
References: headless/…/workspace.mjs• L28-71 headless/…/lifecycle-store.mjs• L39-70 headless/…/lifecycle-store.mjs• L163-231 headless/…/lifecycle-store.mjs• L387-435
Browser harness
The harness loads a built headless.html page in Chrome and waits until the FreeCut browser API is ready. The page uses the application’s composition and export domains, so the output follows the same project rules as in-app export while avoiding the editor shell.
Media reaches the page through fetchable URLs. The driver also seeds media metadata into the browser state because edit and render work needs duration, frame rate, dimensions, and codec facts. Large media can be read in byte ranges instead of being copied into the page at once.
References: headless.html• L1-10 src/…/main.ts• L1-16 src/…/seed-media.ts• L4-20 headless/…/render-core.mjs• L120-155 headless/…/render-core.mjs• L435-447
Request completion
A request is complete only after the browser operation and its result handoff complete. A render must produce a downloadable file. A persisted edit or probe must pass normalization and the revision check before the workspace changes. A failed request returns a structured error instead of being reported as a successful browser call.
The warm service closes in stages: it stops accepting work, drains the queue, closes the page and browser, closes local servers, and releases the writer lock. This prevents a new request from entering while the workspace or browser is being torn down.
References: headless/serve.mjs• L262-285 headless/serve.mjs• L384-479 headless/serve.mjs• L712-736 headless/agent.mjs• L72-100 headless/…/lifecycle-store.mjs• L387-415
Rendering and inspection
Headless rendering turns a project or inline project object into a video, audio file, still frame, or layout result. The render path resolves only the media needed by the project or requested time slice, adapts settings to the browser, and reports changes or risks through machine-readable warnings.
Render outputs
The system supports full or ranged video renders and audio-only renders. The warm service also supports a single composited frame for fast visual checks and a layout inspection that returns the calculated item boxes without encoding media.
The render result names the effective settings, not only the requested settings. This matters when the browser cannot encode the requested video codec. Missing media is fatal by default; an explicit permissive mode produces blank or silent gaps with a MISSING_MEDIA warning.
References: headless/serve.mjs• L11-29 headless/render.mjs• L1-31 headless/…/render-core.mjs• L183-216 headless/…/render-core.mjs• L268-336
Render validation
Validation exists to expose errors that an interactive editor user might see but an automated caller would otherwise miss. The harness checks project migration warnings, media source overruns, transition structure, transform-parent links, font readiness, and audio results before or during rendering.
Strict mode turns validation warnings into a failure before output. Normal mode returns warnings with the result so a pipeline can decide whether a degraded artifact is acceptable. GPU effects need a usable WebGPU adapter. Transitions can fall back to Canvas2D, but unsupported transition presentations can become hard cuts and are reported.
References: src/…/validation.ts• L1-8 src/…/validation.ts• L29-35 src/…/validation.ts• L93-98 src/…/main.ts• L364-420 headless/…/render-core.mjs• L240-253
Programmatic editing
Programmatic editing applies a batch of named operations to a project and returns a rebuilt project plus per-operation results. The browser entry point hydrates the real timeline domain, applies the operations in order, and serializes the changed state instead of maintaining a second headless-only edit model.
Operations cover the main timeline changes: items and tracks, clip placement, splits and trims, transitions, keyframes, transforms and transform parents, and registered GPU effects. An addClip operation uses seeded media metadata to create the matching image, audio, or video timeline content. A video with audio can create linked audio content so the edit matches normal media import behavior.
Each operation can have a unique caller ID. Later operations may refer to an ID returned by an earlier successful operation, which lets an automated batch use generated item, track, transition, or effect IDs without guessing them. A failed operation stops the batch and is recorded as a failure rather than silently producing a partial success response.
References: src/…/edit.ts• L1-9 src/…/edit.ts• L61-98 src/…/edit.ts• L123-152 src/…/edit.ts• L641-693 headless/…/contract.mjs• L450-539
Edit persistence
An edit can be a dry computation or a durable workspace change. Non-persisted edits return the proposed project and its base revision. Persisted edits normalize the result and save it only when the caller supplies the current revision or explicitly chooses force mode.
The CLI edit path is also safe by default: it writes only when the caller selects an output path or explicit in-place mode. The lifecycle API adds the same revision guard to project save, update, edit, and persisted media probe actions.
References: headless/agent.mjs• L221-254 headless/edit.mjs• L18-19 headless/edit.mjs• L123-137 headless/…/lifecycle-store.mjs• L163-231 headless/…/contract.mjs• L413-457
Media operations
Media operations keep source files and the metadata needed by the browser in step. Import copies a supported local file into the workspace, probes it through the browser media path, and commits metadata. Probe can inspect an existing source and can optionally persist the refreshed metadata.
The lifecycle HTTP API exposes media listing, lookup, and probe. It does not upload media over HTTP. A client must place media into the workspace through the agent’s local import flow or another trusted workspace process before an edit or render can use it.
Media references are resolved by ID. A render collects media from the active project range, including nested composition content, then serves the source through a range-capable local endpoint. An edit needs metadata for addClip, but it does not need to serve media bytes because it does not render.
References: headless/agent.mjs• L256-297 headless/…/lifecycle-store.mjs• L447-532 headless/…/lifecycle-store.mjs• L534-634 headless/…/workspace.mjs• L74-118
Operation queue
The warm service uses one bounded queue for browser work because the shared page and GPU are not safe targets for concurrent operations. The queue runs one operation at a time, limits waiting work, and applies a whole-operation deadline that includes browser evaluation and render download handling.
When an operation fails or times out, the service recovers the browser page before it starts the next operation. A recovery failure makes the service unavailable rather than allowing later work to use an unknown page state. During shutdown the queue rejects new work and waits for accepted work to drain up to the shutdown deadline.
The queue therefore provides three useful caller signals:
| Signal | Meaning |
|---|---|
QUEUE_FULL | The service has reached its active plus waiting work limit |
OPERATION_TIMEOUT | The current browser operation exceeded its deadline |
SERVICE_SHUTTING_DOWN | The service no longer accepts new work |
References: headless/…/operation-queue.mjs• L10-25 headless/…/operation-queue.mjs• L26-125 headless/…/operation-queue.mjs• L128-142 headless/serve.mjs• L210-221
Checkpointed operations
Checkpointed operations make long edit-and-render work restartable. The system records the request, revision checks, project commit, artifact publication, and final result as durable phases, so a process loss does not force a caller to guess which side effects completed.
Operation records
Each operation has a UUIDv7 operation ID and an idempotency key. The checkpoint stores a canonical request and its hash, then moves through queued, revision or recipe application, project commit, rendering, artifact commit, and a terminal success or failure phase. Unfinished records can be reconciled after restart.
References: headless/…/checkpoint-operations.mjs• L8-21 headless/…/checkpoint-operations.mjs• L354-510 headless/…/checkpoint-operations.mjs• L971-1007
Project and artifact commits
Recipe operations commit the edited project with a receipt bound to the operation, request, recipe, and prior revision. Final renders verify the produced media against the fixed render profile before publication. Artifacts are published only when their bytes match the recorded evidence, which prevents a retry from adopting an unrelated file.
References: headless/…/checkpoint-operations.mjs• L512-559 headless/…/checkpoint-operations.mjs• L620-758 headless/…/checkpoint-operations.mjs• L780-839 headless/…/lifecycle-store.mjs• L238-304
Automation contracts
The automation contract makes requests and results stable for scripts. It has one API version, strict request schemas, bounded input sizes, portable resource IDs, published capabilities, and structured validation fields. The service uses the version and capabilities responses so a client can discover supported operations and render choices instead of relying on hidden browser behavior.
Persisted HTTP create and edit requests accept an idempotency key. The service records the request identity and completed response in workspace state, so a safe retry can replay the result. Reusing the same key with a different route or body is a conflict; a crash-left pending record is reported as indeterminate rather than run again without a caller decision.
Contract tests cover schema and capability parity, every public edit operation, workspace revision and lock behavior, media serving rules, queue recovery, and a full HTTP-plus-CLI lifecycle. The end-to-end lifecycle creates a project, edits it, imports media, and renders an output, which checks the boundaries that unit-level tests cannot join.
References: headless/…/contract.mjs• L3-18 headless/…/contract.mjs• L647-705 headless/…/idempotency.mjs• L75-151 headless/edit-operations.mjs• L409-468 headless/lifecycle-e2e.mjs• L43-176
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the published operation names, operation schemas, capability response, and browser edit dispatcher in parity.
- Keep the lifecycle route list, revision rules, idempotency coverage, and resource save behavior aligned.
- Keep render request aliases, effective codec/container reporting, output extensions, MIME values, and warning codes aligned.
- Keep media ID resolution, metadata seeding, media links, and range-serving behavior aligned for both the agent and warm service.
- Keep queue timeout kinds and recovery behavior aligned with HTTP status and error code mapping.
- Keep checkpoint phases, request and recipe hashes, project receipts, artifact evidence, and restart reconciliation aligned.
- Keep checkpoint output paths contained under
artifacts, and keep final-render media probes aligned with the declared render profile. - If the harness API changes, update both the
window.freecutexposure and every Node caller that evaluates it.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Built harness is absent and no build or dev URL is supplied | The agent or service cannot start the browser harness | Build the harness or provide a reachable harness URL |
| A project uses GPU effects on a software-only or unavailable WebGPU adapter | The render fails before producing output | Run with a usable hardware GPU; treat transition fallback warnings separately |
| A referenced media source is absent | Rendering fails by default; permissive mode produces blank or silent gaps | Import or restore the media, or explicitly accept MISSING_MEDIA |
| A persisted request uses an old revision | The write returns a revision conflict and does not replace the newer resource | Read the resource again and retry with its revision, or use force deliberately |
| Another agent owns the workspace writer lock | A mutating agent or service cannot start | Stop the other writer and allow stale-lock recovery only when the owner is confirmed dead |
| Queue capacity is exhausted or an operation exceeds its deadline | The request returns QUEUE_FULL or OPERATION_TIMEOUT; failed work triggers page recovery | Bound client concurrency and set deadlines that fit the job |
| An idempotency key is reused with a different request | The service returns IDEMPOTENCY_CONFLICT instead of guessing which request to run | Use one stable key for one request body and route |
| A checkpoint record or its request hash is corrupt | The operation cannot be trusted or resumed | Preserve canonical checkpoint records and treat corruption as a hard failure |
| A retry finds an artifact with different bytes or a render profile mismatch | The operation returns an artifact collision or render profile error instead of publishing the file | Keep one output path per operation and verify the final media profile |
| An edit refers to missing content or an invalid transition update | The operation fails and the edit does not become a persisted result | Use prior successful result references and validate IDs and clip handles |
Key files
| File | Why to read it |
|---|---|
| headless/serve.mjs | Shows the warm HTTP service lifecycle, route families, queue use, persistence, and shutdown |
| headless/agent.mjs | Shows the one-shot command lifecycle for project, media, edit, and render actions |
| src/…/main.ts | Defines the browser harness boundary and connects project, media, inspection, and render capabilities |
| headless/…/render-core.mjs | Defines shared render preparation, media resolution, warnings, and output handoff |
| src/…/edit.ts | Defines the programmatic editing boundary and its use of the real timeline actions |
| headless/…/contract.mjs | Defines the versioned schemas, capabilities, routes, limits, and edit operation contract |
| headless/…/lifecycle-store.mjs | Defines durable project and media resources, revisions, atomic writes, checkpoint receipts, and the exclusive writer lock |
| headless/…/operation-queue.mjs | Defines serialization, queue bounds, deadlines, recovery, and shutdown behavior |
| headless/…/checkpoint-operations.mjs | Defines durable operation phases, restart reconciliation, project receipts, artifact evidence, and safe publication |
| headless/edit-operations.mjs | Checks parity and behavior for every public edit operation in a real Chrome harness |
| headless/lifecycle-e2e.mjs | Demonstrates the complete HTTP and CLI project-to-render lifecycle |