Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript
92.1 KB
Raw Blame

OpenReel Desktop (Electron) — Phases 0–2 Implementation Plan

For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (- [ ]) syntax for tracking.

Goal: Stand up an Electron desktop shell that runs the existing apps/web editor over an app:// protocol, then offload video export to a bundled native FFmpeg sidecar in the Node main process.

Architecture: A new apps/desktop workspace package hosts the Electron main process (Node), a sandboxed contextBridge preload exposing window.openreel.*, and a typed IPC contract. The renderer is the unmodified @openreel/web bundle served over a custom app:// scheme that injects COOP/COEP. Heavy export work moves behind a pluggable EncoderBackend in @openreel/core: WebCodecsBackend (today's inline path, extracted) for the browser, NativeFFmpegBackend (renderer side, talks to a main-process FFmpeg child over a MessageChannelMain port) for desktop.

Tech Stack: Electron (pin current stable ≥30; APIs used are stable: protocol.handle ≥25, MessageChannelMain ≥22, safeStorage ≥15), TypeScript, tsup (bundle main/preload), zod (IPC validation), Vitest (already in repo), bundled static FFmpeg binaries, pnpm workspace.

Spec: docs/superpowers/specs/2026-06-02-electron-desktop-native-offload-design.md


Shared Contracts (defined once, referenced by every task)

These three contracts thread through all phases. Their exact shapes are fixed here so later tasks stay consistent.

A. window.openreel ambient type — apps/web/src/types/global.d.ts

The full bridge surface used across Phases 0–2 (methods are implemented progressively; the type is declared in full in Task 0.6 so later renderer ports type-check without rework):

export {};

export interface OpenReelHardwareInfo {
  cpu: { model: string; physicalCores: number; logicalCores: number };
  memory: { totalBytes: number; freeBytes: number };
  gpus: string[];
  encoders: string[];
  platform: "darwin" | "win32" | "linux";
  arch: string;
}

export interface OpenReelExportStartArgs {
  width: number;
  height: number;
  frameRate: number;
  codec: string;
  format: string;
  bitrateKbps: number;
  outputPath: string;
  totalFrames: number;
  audioSampleRate: number;
  audioChannels: number;
}

export interface OpenReelExportSession {
  jobId: string;
  port: MessagePort;
}

declare global {
  interface Window {
    openreel?: {
      platform: "desktop";
      publicOrigin: string;
      probeHardware(): Promise<OpenReelHardwareInfo>;
      fs: {
        showSaveDialog(opts: {
          defaultPath: string;
          filters: { name: string; extensions: string[] }[];
        }): Promise<string | null>;
        showOpenDialog(opts: {
          filters: { name: string; extensions: string[] }[];
        }): Promise<string | null>;
        readFile(path: string): Promise<string>;
        writeFile(path: string, data: string): Promise<void>;
        openWrite(path: string): Promise<string>;
        writeChunk(handleId: string, data: ArrayBuffer, position: number): Promise<void>;
        closeWrite(handleId: string): Promise<void>;
        abortWrite(handleId: string): Promise<void>;
        revealInFolder(path: string): Promise<void>;
      };
      keychain: {
        get(id: string): Promise<string | null>;
        set(id: string, value: string): Promise<void>;
        delete(id: string): Promise<void>;
      };
      export: {
        start(args: OpenReelExportStartArgs): Promise<OpenReelExportSession>;
        writeAudioWav(jobId: string, wav: ArrayBuffer): Promise<void>;
        cancel(jobId: string): Promise<void>;
      };
    };
  }
}

B. EncoderBackend interface — packages/core/src/export/encoder-backend.ts

import type { VideoExportSettings } from "./types";
import type { Project } from "../timeline/types";

export interface EncoderBackend {
  readonly requiresWebCodecsClamping: boolean;
  readonly needsFrameThrottling: boolean;
  start(
    settings: VideoExportSettings,
    project: Project,
    writableStream?: FileSystemWritableFileStream,
  ): Promise<void>;
  addVideoFrame(frame: ImageBitmap, timestampSec: number, durationSec: number): Promise<void>;
  addAudioBuffer(buffer: AudioBuffer): Promise<void>;
  finalize(): Promise<void>;
  abort(): Promise<void>;
}

export type EncoderBackendFactory = (
  mediabunny: typeof import("mediabunny") | null,
) => EncoderBackend;

Note: verify the exact import paths for VideoExportSettings and Project against packages/core/src/export/types.ts and the timeline types during Task 2.4; adjust the two import type lines to match the real locations.

C. IPC channel contract — apps/desktop/src/shared/ipc-contract.ts

Channel names are centralized; every payload is validated with zod at the main-process boundary. Defined in Task 0.5, extended in Phase 2.


File Structure

New (apps/desktop):

  • apps/desktop/package.json — Electron app manifest + scripts
  • apps/desktop/tsconfig.json, apps/desktop/tsup.config.ts
  • apps/desktop/src/main/index.ts — app lifecycle, BrowserWindow
  • apps/desktop/src/main/protocol.ts — app:// scheme + COOP/COEP
  • apps/desktop/src/main/ipc/index.ts — handler registration
  • apps/desktop/src/main/ipc/hardware.ts — probeHardware
  • apps/desktop/src/main/ipc/fs.ts — dialogs + streamed file writes
  • apps/desktop/src/main/ipc/keychain.ts — safeStorage-backed key store
  • apps/desktop/src/main/sidecar/ffmpeg-path.ts — resolve bundled binary
  • apps/desktop/src/main/sidecar/encoder-probe.ts — probe + select encoder
  • apps/desktop/src/main/sidecar/export-job.ts — spawn ffmpeg, stream frames
  • apps/desktop/src/main/sidecar/cfr.ts — CFR frame-grid mapping (pure)
  • apps/desktop/src/preload/index.ts — contextBridge → window.openreel
  • apps/desktop/src/shared/ipc-contract.ts — channels + zod schemas
  • apps/desktop/resources/bin/ — per-platform FFmpeg binaries + MANIFEST.json
  • apps/desktop/test/ — Vitest specs for pure logic (cfr, encoder-probe, fs-writable)

Modified (apps/web, packages/core):

  • apps/web/vite.config.ts — desktop build mode (base, transformIndexHtml)
  • apps/web/index.html — (via plugin) relative paths + self-hosted fonts on desktop
  • apps/web/src/types/global.d.ts — new ambient window.openreel
  • apps/web/src/services/service-worker.ts — desktop guard in registerServiceWorker
  • apps/web/src/services/project-manager.ts — desktop fs branch
  • apps/web/src/services/secure-storage.ts — desktop keychain branch
  • apps/web/src/components/editor/Toolbar.tsx — desktop save path + native export wiring
  • apps/web/src/main.tsx — wire NativeFFmpegBackend factory when desktop
  • packages/core/src/media/ffmpeg-fallback.ts — local-bundle wasm core
  • packages/core/src/export/encoder-backend.ts — new interface
  • packages/core/src/export/webcodecs-backend.ts — new (extracted inline path)
  • packages/core/src/export/export-engine.ts — delegate to backend
  • apps/web/src/services/native-ffmpeg-backend.ts — new desktop backend

PHASE 0 — Workspace & shell skeleton

Phase exit: an Electron window opens, loads a page over app://, crossOriginIsolated === true, and window.openreel.probeHardware() returns real CPU/RAM/GPU.

Task 0.1: Scaffold the apps/desktop package

Files:

  • Create: apps/desktop/package.json

  • Create: apps/desktop/tsconfig.json

  • Create: apps/desktop/tsup.config.ts

  • Create: apps/desktop/src/main/index.ts (placeholder)

  • Modify: pnpm-workspace.yaml (already globs apps/* — verify, no edit if so)

  • Step 1: Create apps/desktop/package.json

{
  "name": "@openreel/desktop",
  "private": true,
  "version": "0.0.0",
  "type": "commonjs",
  "main": "dist/main/index.js",
  "scripts": {
    "build:main": "tsup",
    "dev": "tsup --watch --onSuccess \"electron .\"",
    "start": "electron .",
    "typecheck": "tsc --noEmit",
    "test": "vitest",
    "test:run": "vitest run"
  },
  "dependencies": {
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "electron": "^33.0.0",
    "tsup": "^8.0.0",
    "typescript": "^5.4.5",
    "vitest": "^1.6.0"
  }
}
  • Step 2: Create apps/desktop/tsconfig.json
{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "module": "CommonJS",
    "moduleResolution": "node",
    "outDir": "dist",
    "rootDir": "src",
    "lib": ["ES2022", "DOM"],
    "types": ["node"],
    "noEmit": true,
    "composite": false
  },
  "include": ["src"]
}
  • Step 3: Create apps/desktop/tsup.config.ts
import { defineConfig } from "tsup";

export default defineConfig({
  entry: {
    "main/index": "src/main/index.ts",
    "preload/index": "src/preload/index.ts",
  },
  format: ["cjs"],
  platform: "node",
  target: "node18",
  external: ["electron"],
  clean: true,
  sourcemap: true,
});
  • Step 4: Create placeholder apps/desktop/src/main/index.ts
import { app } from "electron";

app.whenReady().then(() => {
  console.log("[openreel-desktop] main process ready");
});
  • Step 5: Install and verify the workspace resolves

Run: pnpm install Expected: completes; @openreel/desktop appears in the workspace (no error about an unknown package).

  • Step 6: Verify pnpm-workspace.yaml includes apps/*

Run: grep -n "apps/\*" pnpm-workspace.yaml Expected: a match. If absent, add - "apps/*" under packages:.

  • Step 7: Commit
git add apps/desktop/package.json apps/desktop/tsconfig.json apps/desktop/tsup.config.ts apps/desktop/src/main/index.ts pnpm-workspace.yaml
git commit -m "feat(desktop): scaffold @openreel/desktop electron package"

Task 0.2: Main process + secure BrowserWindow

Files:

  • Modify: apps/desktop/src/main/index.ts

  • Create: apps/desktop/src/preload/index.ts (minimal)

  • Step 1: Create minimal preload apps/desktop/src/preload/index.ts

import { contextBridge } from "electron";

contextBridge.exposeInMainWorld("openreel", {
  platform: "desktop",
});
  • Step 2: Replace apps/desktop/src/main/index.ts with the window bootstrap
import { app, BrowserWindow } from "electron";
import path from "node:path";

function createWindow(): void {
  const win = new BrowserWindow({
    width: 1440,
    height: 900,
    minWidth: 1024,
    minHeight: 700,
    backgroundColor: "#0a0a0a",
    webPreferences: {
      preload: path.join(__dirname, "../preload/index.js"),
      contextIsolation: true,
      nodeIntegration: false,
      sandbox: true,
      webSecurity: true,
    },
  });

  win.loadURL("data:text/html,<h1 style='color:white;background:%230a0a0a'>OpenReel Desktop boot OK</h1>");
}

app.whenReady().then(() => {
  createWindow();
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});

minWidth: 1024 satisfies spec §5 #8 — the renderer's MobileBlocker (<768px) never trips.

  • Step 3: Build and launch

Run: pnpm --filter @openreel/desktop build:main && pnpm --filter @openreel/desktop start Expected: a window opens showing "OpenReel Desktop boot OK". Close it; process exits.

  • Step 4: Commit
git add apps/desktop/src/main/index.ts apps/desktop/src/preload/index.ts
git commit -m "feat(desktop): secure BrowserWindow + minimal preload bridge"

Task 0.3: app:// protocol with COOP/COEP injection

Files:

  • Create: apps/desktop/src/main/protocol.ts

  • Modify: apps/desktop/src/main/index.ts

  • Create: apps/desktop/resources/renderer/index.html (temporary isolation probe page)

  • Step 1: Create the isolation probe page apps/desktop/resources/renderer/index.html

<!doctype html>
<html>
  <head><meta charset="utf-8" /><title>probe</title></head>
  <body style="background:#0a0a0a;color:#fff;font-family:sans-serif">
    <h1 id="status">checking…</h1>
    <script type="module">
      const ok = (typeof crossOriginIsolated !== "undefined") && crossOriginIsolated;
      document.getElementById("status").textContent =
        "crossOriginIsolated=" + ok + " platform=" + (window.openreel?.platform ?? "none");
      document.title = ok ? "ISOLATED_OK" : "ISOLATED_FAIL";
    </script>
  </body>
</html>
  • Step 2: Create apps/desktop/src/main/protocol.ts
import { protocol, net } from "electron";
import path from "node:path";
import { pathToFileURL } from "node:url";

const SCHEME = "app";

export function registerAppSchemePrivileges(): void {
  protocol.registerSchemesAsPrivileged([
    {
      scheme: SCHEME,
      privileges: {
        standard: true,
        secure: true,
        supportFetchAPI: true,
        corsEnabled: true,
        stream: true,
      },
    },
  ]);
}

export function handleAppScheme(rendererRoot: string): void {
  protocol.handle(SCHEME, async (request) => {
    const url = new URL(request.url);
    let pathname = decodeURIComponent(url.pathname);
    if (pathname === "/" || pathname === "") pathname = "/index.html";

    const resolved = path.join(rendererRoot, pathname);
    const normalized = path.normalize(resolved);
    if (!normalized.startsWith(path.normalize(rendererRoot))) {
      return new Response("Forbidden", { status: 403 });
    }

    let response: Response;
    try {
      response = await net.fetch(pathToFileURL(normalized).toString());
    } catch {
      response = await net.fetch(pathToFileURL(path.join(rendererRoot, "index.html")).toString());
    }

    const headers = new Headers(response.headers);
    headers.set("Cross-Origin-Opener-Policy", "same-origin");
    headers.set("Cross-Origin-Embedder-Policy", "require-corp");
    headers.set("Cross-Origin-Resource-Policy", "same-origin");
    return new Response(response.body, {
      status: response.status,
      statusText: response.statusText,
      headers,
    });
  });
}

export const APP_ORIGIN = `${SCHEME}://openreel`;
export const APP_INDEX = `${APP_ORIGIN}/index.html`;
  • Step 3: Wire protocol into apps/desktop/src/main/index.ts

Replace the file with:

import { app, BrowserWindow } from "electron";
import path from "node:path";
import { registerAppSchemePrivileges, handleAppScheme, APP_INDEX } from "./protocol";

registerAppSchemePrivileges();

function rendererRoot(): string {
  return path.join(__dirname, "../../resources/renderer");
}

function createWindow(): void {
  const win = new BrowserWindow({
    width: 1440,
    height: 900,
    minWidth: 1024,
    minHeight: 700,
    backgroundColor: "#0a0a0a",
    webPreferences: {
      preload: path.join(__dirname, "../preload/index.js"),
      contextIsolation: true,
      nodeIntegration: false,
      sandbox: true,
      webSecurity: true,
    },
  });
  win.loadURL(APP_INDEX);
}

app.whenReady().then(() => {
  handleAppScheme(rendererRoot());
  createWindow();
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
  • Step 4: Build and launch; verify isolation

Run: pnpm --filter @openreel/desktop build:main && pnpm --filter @openreel/desktop start Expected: the window shows crossOriginIsolated=true platform=desktop. (If it shows false, the COOP/COEP headers are not being applied — do not proceed; debug handleAppScheme before continuing, since every later phase depends on isolation.)

  • Step 5: Commit
git add apps/desktop/src/main/protocol.ts apps/desktop/src/main/index.ts apps/desktop/resources/renderer/index.html
git commit -m "feat(desktop): app:// protocol serving with COOP/COEP injection"

Task 0.4: IPC contract scaffold + zod validation harness

Files:

  • Create: apps/desktop/src/shared/ipc-contract.ts

  • Create: apps/desktop/src/main/ipc/index.ts

  • Create: apps/desktop/test/ipc-contract.test.ts

  • Step 1: Write the failing test apps/desktop/test/ipc-contract.test.ts

import { describe, it, expect } from "vitest";
import { CHANNELS, hardwareInfoSchema } from "../src/shared/ipc-contract";

describe("ipc-contract", () => {
  it("exposes the probeHardware channel name", () => {
    expect(CHANNELS.probeHardware).toBe("openreel:probeHardware");
  });

  it("validates a well-formed hardware info object", () => {
    const ok = hardwareInfoSchema.safeParse({
      cpu: { model: "M3", physicalCores: 8, logicalCores: 8 },
      memory: { totalBytes: 1, freeBytes: 1 },
      gpus: ["Apple M3"],
      encoders: ["h264_videotoolbox"],
      platform: "darwin",
      arch: "arm64",
    });
    expect(ok.success).toBe(true);
  });

  it("rejects a malformed hardware info object", () => {
    const bad = hardwareInfoSchema.safeParse({ cpu: { model: 1 } });
    expect(bad.success).toBe(false);
  });
});
  • Step 2: Run to verify failure

Run: pnpm --filter @openreel/desktop test:run Expected: FAIL — cannot resolve ../src/shared/ipc-contract.

  • Step 3: Create apps/desktop/src/shared/ipc-contract.ts
import { z } from "zod";

export const CHANNELS = {
  probeHardware: "openreel:probeHardware",
  fsShowSaveDialog: "openreel:fs:showSaveDialog",
  fsShowOpenDialog: "openreel:fs:showOpenDialog",
  fsReadFile: "openreel:fs:readFile",
  fsWriteFile: "openreel:fs:writeFile",
  fsOpenWrite: "openreel:fs:openWrite",
  fsWriteChunk: "openreel:fs:writeChunk",
  fsCloseWrite: "openreel:fs:closeWrite",
  fsAbortWrite: "openreel:fs:abortWrite",
  fsRevealInFolder: "openreel:fs:revealInFolder",
  keychainGet: "openreel:keychain:get",
  keychainSet: "openreel:keychain:set",
  keychainDelete: "openreel:keychain:delete",
  exportStart: "openreel:export:start",
  exportWriteAudioWav: "openreel:export:writeAudioWav",
  exportCancel: "openreel:export:cancel",
} as const;

export const hardwareInfoSchema = z.object({
  cpu: z.object({
    model: z.string(),
    physicalCores: z.number().int().nonnegative(),
    logicalCores: z.number().int().nonnegative(),
  }),
  memory: z.object({
    totalBytes: z.number().nonnegative(),
    freeBytes: z.number().nonnegative(),
  }),
  gpus: z.array(z.string()),
  encoders: z.array(z.string()),
  platform: z.enum(["darwin", "win32", "linux"]),
  arch: z.string(),
});

export type HardwareInfo = z.infer<typeof hardwareInfoSchema>;

export const saveDialogArgsSchema = z.object({
  defaultPath: z.string(),
  filters: z.array(z.object({ name: z.string(), extensions: z.array(z.string()) })),
});
export const openDialogArgsSchema = z.object({
  filters: z.array(z.object({ name: z.string(), extensions: z.array(z.string()) })),
});
export const writeFileArgsSchema = z.object({ path: z.string(), data: z.string() });
export const readFileArgsSchema = z.object({ path: z.string() });
  • Step 4: Create handler-registration stub apps/desktop/src/main/ipc/index.ts
import { ipcMain } from "electron";
import { z } from "zod";

export function handle<TArgs, TResult>(
  channel: string,
  schema: z.ZodType<TArgs>,
  fn: (args: TArgs) => Promise<TResult> | TResult,
): void {
  ipcMain.handle(channel, async (_event, raw) => {
    const parsed = schema.safeParse(raw);
    if (!parsed.success) {
      throw new Error(`[ipc] invalid payload for ${channel}: ${parsed.error.message}`);
    }
    return fn(parsed.data);
  });
}
  • Step 5: Run to verify pass

Run: pnpm --filter @openreel/desktop test:run Expected: PASS (3 tests).

  • Step 6: Commit
git add apps/desktop/src/shared/ipc-contract.ts apps/desktop/src/main/ipc/index.ts apps/desktop/test/ipc-contract.test.ts
git commit -m "feat(desktop): typed IPC contract + zod-validated handler harness"

Task 0.5: probeHardware implementation

Files:

  • Create: apps/desktop/src/main/ipc/hardware.ts

  • Create: apps/desktop/test/hardware.test.ts

  • Modify: apps/desktop/src/main/index.ts

  • Step 1: Write the failing test apps/desktop/test/hardware.test.ts

import { describe, it, expect } from "vitest";
import { collectHardwareInfo } from "../src/main/ipc/hardware";
import { hardwareInfoSchema } from "../src/shared/ipc-contract";

describe("collectHardwareInfo", () => {
  it("returns a schema-valid object with real CPU/memory", async () => {
    const info = await collectHardwareInfo();
    expect(hardwareInfoSchema.safeParse(info).success).toBe(true);
    expect(info.cpu.logicalCores).toBeGreaterThan(0);
    expect(info.memory.totalBytes).toBeGreaterThan(0);
  });
});
  • Step 2: Run to verify failure

Run: pnpm --filter @openreel/desktop test:run Expected: FAIL — cannot resolve hardware.

  • Step 3: Create apps/desktop/src/main/ipc/hardware.ts
import os from "node:os";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import type { HardwareInfo } from "../../shared/ipc-contract";

const run = promisify(execFile);

async function probeGpus(): Promise<string[]> {
  try {
    if (process.platform === "darwin") {
      const { stdout } = await run("system_profiler", ["SPDisplaysDataType", "-json"], { timeout: 4000 });
      const json = JSON.parse(stdout) as { SPDisplaysDataType?: { sppci_model?: string }[] };
      return (json.SPDisplaysDataType ?? []).map((g) => g.sppci_model ?? "GPU").filter(Boolean);
    }
    if (process.platform === "win32") {
      const { stdout } = await run("wmic", ["path", "win32_VideoController", "get", "name"], { timeout: 4000 });
      return stdout.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && l !== "Name");
    }
    const { stdout } = await run("sh", ["-c", "lspci | grep -iE 'vga|3d|display' || true"], { timeout: 4000 });
    return stdout.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
  } catch {
    return [];
  }
}

export async function collectHardwareInfo(): Promise<HardwareInfo> {
  const cpus = os.cpus();
  return {
    cpu: {
      model: cpus[0]?.model?.trim() ?? "unknown",
      physicalCores: cpus.length,
      logicalCores: cpus.length,
    },
    memory: { totalBytes: os.totalmem(), freeBytes: os.freemem() },
    gpus: await probeGpus(),
    encoders: [],
    platform: process.platform as HardwareInfo["platform"],
    arch: process.arch,
  };
}

encoders is populated in Task 2.3 (encoder probe). Physical-vs-logical core distinction is approximated as cpus.length for v1; refine later if needed.

  • Step 4: Run to verify pass

Run: pnpm --filter @openreel/desktop test:run Expected: PASS.

  • Step 5: Register the handler in apps/desktop/src/main/index.ts

Add near the top after the existing imports:

import { z } from "zod";
import { handle } from "./ipc";
import { CHANNELS } from "../shared/ipc-contract";
import { collectHardwareInfo } from "./ipc/hardware";

Inside app.whenReady().then(() => { ... }), before createWindow();, add:

  handle(CHANNELS.probeHardware, z.undefined(), () => collectHardwareInfo());
  • Step 6: Commit
git add apps/desktop/src/main/ipc/hardware.ts apps/desktop/test/hardware.test.ts apps/desktop/src/main/index.ts
git commit -m "feat(desktop): probeHardware IPC (CPU/RAM/GPU)"

Task 0.6: Expose probeHardware through preload + ambient type

Files:

  • Modify: apps/desktop/src/preload/index.ts

  • Create: apps/web/src/types/global.d.ts (full ambient type from Contract A)

  • Step 1: Replace apps/desktop/src/preload/index.ts

import { contextBridge, ipcRenderer } from "electron";
import { CHANNELS } from "../shared/ipc-contract";

contextBridge.exposeInMainWorld("openreel", {
  platform: "desktop",
  publicOrigin: "https://app.openreel.video",
  probeHardware: () => ipcRenderer.invoke(CHANNELS.probeHardware, undefined),
  fs: {
    showSaveDialog: (opts: unknown) => ipcRenderer.invoke(CHANNELS.fsShowSaveDialog, opts),
    showOpenDialog: (opts: unknown) => ipcRenderer.invoke(CHANNELS.fsShowOpenDialog, opts),
    readFile: (p: string) => ipcRenderer.invoke(CHANNELS.fsReadFile, { path: p }),
    writeFile: (p: string, data: string) => ipcRenderer.invoke(CHANNELS.fsWriteFile, { path: p, data }),
    openWrite: (p: string) => ipcRenderer.invoke(CHANNELS.fsOpenWrite, { path: p }),
    writeChunk: (handleId: string, data: ArrayBuffer, position: number) =>
      ipcRenderer.invoke(CHANNELS.fsWriteChunk, { handleId, data, position }),
    closeWrite: (handleId: string) => ipcRenderer.invoke(CHANNELS.fsCloseWrite, { handleId }),
    abortWrite: (handleId: string) => ipcRenderer.invoke(CHANNELS.fsAbortWrite, { handleId }),
    revealInFolder: (p: string) => ipcRenderer.invoke(CHANNELS.fsRevealInFolder, { path: p }),
  },
  keychain: {
    get: (id: string) => ipcRenderer.invoke(CHANNELS.keychainGet, { id }),
    set: (id: string, value: string) => ipcRenderer.invoke(CHANNELS.keychainSet, { id, value }),
    delete: (id: string) => ipcRenderer.invoke(CHANNELS.keychainDelete, { id }),
  },
  export: {
    start: (args: unknown) => ipcRenderer.invoke(CHANNELS.exportStart, args),
    writeAudioWav: (jobId: string, wav: ArrayBuffer) =>
      ipcRenderer.invoke(CHANNELS.exportWriteAudioWav, { jobId, wav }),
    cancel: (jobId: string) => ipcRenderer.invoke(CHANNELS.exportCancel, { jobId }),
  },
});

The export.start MessagePort handoff is refined in Task 2.6; this stub returns whatever the main handler sends until then.

  • Step 2: Create apps/web/src/types/global.d.ts

Paste the entire Contract A block from the top of this plan (the export {} + interfaces + declare global { interface Window { openreel?: {...} } }).

  • Step 3: Typecheck both packages

Run: pnpm --filter @openreel/desktop typecheck && pnpm --filter @openreel/web typecheck Expected: PASS (no errors). The web app now knows window.openreel's shape.

  • Step 4: Manual end-to-end probe check

Temporarily append to apps/desktop/resources/renderer/index.html's script:

window.openreel?.probeHardware().then(h => { document.title = "CPU:" + h.cpu.logicalCores; });

Run: pnpm --filter @openreel/desktop build:main && pnpm --filter @openreel/desktop start Expected: window title shows a real core count (e.g. CPU:8). Revert the temporary script line after confirming.

  • Step 5: Commit
git add apps/desktop/src/preload/index.ts apps/web/src/types/global.d.ts apps/desktop/resources/renderer/index.html
git commit -m "feat(desktop): preload bridge + window.openreel ambient types; probeHardware round-trip"

PHASE 1 — Renderer runs in Electron (web parity, no native offload)

Phase exit: import → edit → export an MP4 (via the existing wasm/WebCodecs path) entirely inside the desktop app; project round-trips through save/reload; crossOriginIsolated === true with multithreaded ffmpeg.wasm from locally bundled core.

Task 1.1: Desktop build mode for @openreel/web

Files:

  • Modify: apps/web/vite.config.ts

  • Step 1: Replace apps/web/vite.config.ts with the function form + desktop branch

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";

const isDesktop = process.env.OPENREEL_DESKTOP === "1";

function desktopHtmlPlugin() {
  return {
    name: "openreel-desktop-html",
    transformIndexHtml(html: string) {
      if (!isDesktop) return html;
      let out = html
        .replace(/href="\/favicon\.svg"/g, 'href="./favicon.svg"')
        .replace(/href="\/manifest\.json"/g, 'href="./manifest.json"')
        .replace(/href="\/icons\/icon-192\.png"/g, 'href="./icons/icon-192.png"');
      out = out.replace(
        /<link rel="preconnect"[^>]*>\s*/g,
        "",
      );
      out = out.replace(
        /<link href="https:\/\/fonts\.googleapis\.com[^>]*>\s*/g,
        '<link href="./fonts/google-fonts.css" rel="stylesheet" />',
      );
      return out;
    },
  };
}

export default defineConfig({
  base: isDesktop ? "./" : "/",
  plugins: [react(), desktopHtmlPlugin()],
  assetsInclude: ["**/*.wasm"],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
      "@openreel/core": path.resolve(__dirname, "../../packages/core/src"),
    },
  },
  worker: { format: "es" },
  optimizeDeps: {
    exclude: ["@ffmpeg/ffmpeg", "@ffmpeg/util", "@ffmpeg/core", "@ffmpeg/core-mt"],
  },
  build: {
    target: "esnext",
    rollupOptions: {
      output: {
        manualChunks: (id) => {
          if (id.includes("node_modules/react") || id.includes("node_modules/react-dom")) return "react";
          if (id.includes("node_modules/zustand")) return "zustand";
          if (id.includes("node_modules/three")) return "three";
          if (id.includes("node_modules/@radix-ui")) return "radix";
        },
      },
    },
  },
  server: {
    headers: {
      "Cross-Origin-Opener-Policy": "same-origin",
      "Cross-Origin-Embedder-Policy": "require-corp",
    },
  },
  preview: {
    headers: {
      "Cross-Origin-Opener-Policy": "same-origin",
      "Cross-Origin-Embedder-Policy": "require-corp",
    },
  },
});
  • Step 2: Verify the web build is unchanged

Run: pnpm --filter @openreel/web build Expected: builds; dist/index.html still references /assets/* (absolute) and keeps the Google Fonts <link> (web mode untouched).

  • Step 3: Verify the desktop build rewrites paths

Run: OPENREEL_DESKTOP=1 pnpm --filter @openreel/web build Expected: builds; dist/index.html now uses ./assets/*, ./favicon.svg, and a ./fonts/google-fonts.css link with no fonts.googleapis.com reference.

  • Step 4: Commit
git add apps/web/vite.config.ts
git commit -m "feat(web): desktop build mode (base + transformIndexHtml for paths/fonts)"

Task 1.2: Self-host the Google Fonts (§5 #10)

Files:

  • Create: apps/web/public/fonts/google-fonts.css + woff2 files

  • Create: apps/web/scripts/vendor-fonts.mjs

  • Step 1: Create the font-vendoring script apps/web/scripts/vendor-fonts.mjs

import { mkdir, writeFile } from "node:fs/promises";
import path from "node:path";

const FONT_CSS_URL =
  "https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&family=DM+Sans:wght@400;500;700&family=Bebas+Neue&family=Anton&family=Poppins:wght@300;400;500;600;700&display=swap";

const OUT_DIR = path.resolve(process.cwd(), "public/fonts");

async function main() {
  await mkdir(OUT_DIR, { recursive: true });
  const css = await (await fetch(FONT_CSS_URL, {
    headers: { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)" },
  })).text();

  const urls = [...css.matchAll(/url\((https:\/\/[^)]+\.woff2)\)/g)].map((m) => m[1]);
  let rewritten = css;
  for (const url of urls) {
    const file = url.split("/").pop();
    const buf = Buffer.from(await (await fetch(url)).arrayBuffer());
    await writeFile(path.join(OUT_DIR, file), buf);
    rewritten = rewritten.replaceAll(url, `./${file}`);
  }
  await writeFile(path.join(OUT_DIR, "google-fonts.css"), rewritten, "utf8");
  console.log(`vendored ${urls.length} font files`);
}
main();

The font list here is the actually-used subset — verify against apps/web/src font usage (search font-family, initCustomFonts, and the families referenced in index.html) and expand the FONT_CSS_URL family= list to cover every family the editor exposes. Do not vendor all ~60 blindly; include only what the UI references.

  • Step 2: Run the vendoring script

Run: cd apps/web && node scripts/vendor-fonts.mjs && cd ../.. Expected: prints vendored N font files; apps/web/public/fonts/google-fonts.css + woff2 files exist.

  • Step 3: Verify desktop build embeds the local fonts

Run: OPENREEL_DESKTOP=1 pnpm --filter @openreel/web build Expected: dist/fonts/google-fonts.css present; dist/index.html links it; no fonts.googleapis.com in dist/index.html.

  • Step 4: Commit
git add apps/web/scripts/vendor-fonts.mjs apps/web/public/fonts
git commit -m "feat(web): self-host fonts for desktop COEP isolation"

Task 1.3: Bundle the ffmpeg.wasm core locally (§5 #3, Phase-1 part)

Files:

  • Modify: packages/core/src/media/ffmpeg-fallback.ts (lines 134–137 baseURL)

  • Step 1: Replace the unpkg baseURL literals in doLoad()

Find (around lines 134–137):

      const useMultiThread = typeof crossOriginIsolated !== "undefined" && crossOriginIsolated;
      const baseURL = useMultiThread
        ? "https://unpkg.com/@ffmpeg/[email protected]/dist/esm"
        : "https://unpkg.com/@ffmpeg/[email protected]/dist/esm";

Replace with locally-resolved asset URLs (Vite ?url imports resolve to bundled, same-origin assets):

      const useMultiThread = typeof crossOriginIsolated !== "undefined" && crossOriginIsolated;

And add at the top of the file (with the other imports):

import coreUrl from "@ffmpeg/core/dist/esm/ffmpeg-core.js?url";
import coreWasmUrl from "@ffmpeg/core/dist/esm/ffmpeg-core.wasm?url";
import coreMtUrl from "@ffmpeg/core-mt/dist/esm/ffmpeg-core.js?url";
import coreMtWasmUrl from "@ffmpeg/core-mt/dist/esm/ffmpeg-core.wasm?url";
import coreMtWorkerUrl from "@ffmpeg/core-mt/dist/esm/ffmpeg-core.worker.js?url";

Then replace the two toBlobURL blocks (lines ~138–160) so they consume these URLs:

      if (useMultiThread) {
        const [coreURL, wasmURL, workerURL] = await Promise.all([
          toBlobURL(coreMtUrl, "text/javascript"),
          toBlobURL(coreMtWasmUrl, "application/wasm"),
          toBlobURL(coreMtWorkerUrl, "text/javascript"),
        ]);
        await this.ffmpeg.load({ coreURL, wasmURL, workerURL });
      } else {
        const [coreURL, wasmURL] = await Promise.all([
          toBlobURL(coreUrl, "text/javascript"),
          toBlobURL(coreWasmUrl, "application/wasm"),
        ]);
        await this.ffmpeg.load({ coreURL, wasmURL });
      }
  • Step 2: Add @ffmpeg/core + @ffmpeg/core-mt as explicit deps

Run: pnpm --filter @openreel/core add @ffmpeg/[email protected] @ffmpeg/[email protected] Expected: added to packages/core/package.json dependencies.

  • Step 3: Typecheck core

Run: pnpm --filter @openreel/core typecheck Expected: PASS. (If TS complains about ?url imports, add declare module "*?url" { const url: string; export default url; } to a packages/core/src/types/url-imports.d.ts.)

  • Step 4: Verify a build does not reference unpkg

Run: OPENREEL_DESKTOP=1 pnpm --filter @openreel/web build && grep -rc "unpkg.com" apps/web/dist || echo "no unpkg refs" Expected: no unpkg refs (or 0). FFmpeg core assets emitted under dist/assets.

  • Step 5: Commit
git add packages/core/src/media/ffmpeg-fallback.ts packages/core/package.json pnpm-lock.yaml
git commit -m "feat(core): bundle ffmpeg.wasm core locally (drop unpkg CDN)"

Task 1.4: Skip the service worker on desktop (§5 #6)

Files:

  • Modify: apps/web/src/services/service-worker.ts (lines 300–307)

  • Step 1: Add the desktop guard at the top of registerServiceWorker()

Find:

export async function registerServiceWorker(): Promise<ServiceWorkerRegistration | null> {
  // Only register in production or if explicitly enabled
  if (import.meta.env.DEV && !import.meta.env.VITE_ENABLE_SW) {
    return null;
  }

  return serviceWorkerManager.register();
}

Replace with:

export async function registerServiceWorker(): Promise<ServiceWorkerRegistration | null> {
  if (typeof window !== "undefined" && window.openreel?.platform === "desktop") {
    return null;
  }
  // Only register in production or if explicitly enabled
  if (import.meta.env.DEV && !import.meta.env.VITE_ENABLE_SW) {
    return null;
  }

  return serviceWorkerManager.register();
}
  • Step 2: Typecheck

Run: pnpm --filter @openreel/web typecheck Expected: PASS (the ambient type from Task 0.6 makes window.openreel?.platform valid).

  • Step 3: Commit
git add apps/web/src/services/service-worker.ts
git commit -m "feat(web): skip service worker registration on desktop"

Task 1.5: Serve the real web bundle over app:// from the desktop app

Files:

  • Modify: apps/desktop/package.json (build scripts)

  • Modify: apps/desktop/src/main/index.ts (rendererRoot points at web dist)

  • Step 1: Add a renderer-build script to apps/desktop/package.json

Add to scripts:

    "build:renderer": "OPENREEL_DESKTOP=1 pnpm --filter @openreel/web build",
    "build": "pnpm run build:renderer && pnpm run build:main",
    "predev": "pnpm run build:renderer"
  • Step 2: Point rendererRoot() at the web dist in apps/desktop/src/main/index.ts

Replace the rendererRoot() function:

function rendererRoot(): string {
  return app.isPackaged
    ? path.join(process.resourcesPath, "renderer")
    : path.join(__dirname, "../../../web/dist");
}

Packaged builds (Phase 5) copy web/dist → resources/renderer; dev resolves the sibling apps/web/dist. Remove the temporary apps/desktop/resources/renderer/index.html probe page now that the real bundle is served.

  • Step 3: Build the renderer + main and launch

Run: pnpm --filter @openreel/desktop build && pnpm --filter @openreel/desktop start Expected: the OpenReel editor UI loads in the desktop window (welcome screen / editor). No MobileBlocker. Open DevTools (View menu or win.webContents.openDevTools() temporarily) and confirm in the console: crossOriginIsolated === true, and no failed font/worker/wasm requests (COEP smoke — spec §4.3 / Phase 0).

  • Step 4: Commit
git add apps/desktop/package.json apps/desktop/src/main/index.ts
git rm apps/desktop/resources/renderer/index.html
git commit -m "feat(desktop): serve @openreel/web bundle over app://"

Task 1.6: Native filesystem IPC handlers (dialogs + read/write + streamed write)

Files:

  • Create: apps/desktop/src/main/ipc/fs.ts

  • Create: apps/desktop/test/fs-writer.test.ts

  • Modify: apps/desktop/src/main/index.ts (register fs handlers)

  • Step 1: Write the failing test for the streamed writer apps/desktop/test/fs-writer.test.ts

import { describe, it, expect, afterAll } from "vitest";
import { tmpdir } from "node:os";
import path from "node:path";
import { readFile, rm } from "node:fs/promises";
import { FileWriterRegistry } from "../src/main/ipc/fs";

const target = path.join(tmpdir(), `openreel-test-${Date.now()}.bin`);

describe("FileWriterRegistry", () => {
  afterAll(() => rm(target, { force: true }));

  it("writes positioned chunks then finalizes to disk", async () => {
    const reg = new FileWriterRegistry();
    const id = await reg.open(target);
    await reg.writeChunk(id, new TextEncoder().encode("world").buffer, 5);
    await reg.writeChunk(id, new TextEncoder().encode("hello").buffer, 0);
    await reg.close(id);
    expect(await readFile(target, "utf8")).toBe("helloworld");
  });

  it("abort discards the partial file", async () => {
    const reg = new FileWriterRegistry();
    const id = await reg.open(target);
    await reg.writeChunk(id, new TextEncoder().encode("partial").buffer, 0);
    await reg.abort(id);
    await expect(readFile(target)).rejects.toBeDefined();
  });
});
  • Step 2: Run to verify failure

Run: pnpm --filter @openreel/desktop test:run Expected: FAIL — cannot resolve FileWriterRegistry.

  • Step 3: Create apps/desktop/src/main/ipc/fs.ts
import { dialog, shell, BrowserWindow } from "electron";
import { promises as fs } from "node:fs";
import { randomUUID } from "node:crypto";

export class FileWriterRegistry {
  private handles = new Map<string, fs.FileHandle>();
  private paths = new Map<string, string>();

  async open(path: string): Promise<string> {
    const id = randomUUID();
    this.handles.set(id, await fs.open(path, "w"));
    this.paths.set(id, path);
    return id;
  }

  async writeChunk(id: string, data: ArrayBuffer, position: number): Promise<void> {
    const fh = this.handles.get(id);
    if (!fh) throw new Error(`unknown write handle ${id}`);
    await fh.write(new Uint8Array(data), 0, data.byteLength, position);
  }

  async close(id: string): Promise<void> {
    const fh = this.handles.get(id);
    if (!fh) return;
    await fh.close();
    this.handles.delete(id);
    this.paths.delete(id);
  }

  async abort(id: string): Promise<void> {
    const fh = this.handles.get(id);
    const p = this.paths.get(id);
    if (fh) await fh.close();
    this.handles.delete(id);
    this.paths.delete(id);
    if (p) await fs.rm(p, { force: true });
  }
}

export const fileWriters = new FileWriterRegistry();

export async function showSaveDialog(args: {
  defaultPath: string;
  filters: { name: string; extensions: string[] }[];
}): Promise<string | null> {
  const win = BrowserWindow.getFocusedWindow() ?? undefined;
  const res = await dialog.showSaveDialog(win!, {
    defaultPath: args.defaultPath,
    filters: args.filters,
  });
  return res.canceled || !res.filePath ? null : res.filePath;
}

export async function showOpenDialog(args: {
  filters: { name: string; extensions: string[] }[];
}): Promise<string | null> {
  const win = BrowserWindow.getFocusedWindow() ?? undefined;
  const res = await dialog.showOpenDialog(win!, {
    filters: args.filters,
    properties: ["openFile"],
  });
  return res.canceled || res.filePaths.length === 0 ? null : res.filePaths[0];
}

export async function readTextFile(args: { path: string }): Promise<string> {
  return fs.readFile(args.path, "utf8");
}

export async function writeTextFile(args: { path: string; data: string }): Promise<void> {
  await fs.writeFile(args.path, args.data, "utf8");
}

export async function revealInFolder(args: { path: string }): Promise<void> {
  shell.showItemInFolder(args.path);
}
  • Step 4: Run to verify pass

Run: pnpm --filter @openreel/desktop test:run Expected: PASS.

  • Step 5: Register fs handlers in apps/desktop/src/main/index.ts

Add imports:

import {
  fileWriters, showSaveDialog, showOpenDialog, readTextFile, writeTextFile, revealInFolder,
} from "./ipc/fs";
import {
  saveDialogArgsSchema, openDialogArgsSchema, readFileArgsSchema, writeFileArgsSchema,
} from "../shared/ipc-contract";

Inside app.whenReady(), alongside the probeHardware registration:

  handle(CHANNELS.fsShowSaveDialog, saveDialogArgsSchema, showSaveDialog);
  handle(CHANNELS.fsShowOpenDialog, openDialogArgsSchema, showOpenDialog);
  handle(CHANNELS.fsReadFile, readFileArgsSchema, readTextFile);
  handle(CHANNELS.fsWriteFile, writeFileArgsSchema, writeTextFile);
  handle(CHANNELS.fsOpenWrite, z.object({ path: z.string() }), ({ path }) => fileWriters.open(path));
  handle(CHANNELS.fsWriteChunk,
    z.object({ handleId: z.string(), data: z.instanceof(ArrayBuffer), position: z.number() }),
    ({ handleId, data, position }) => fileWriters.writeChunk(handleId, data, position));
  handle(CHANNELS.fsCloseWrite, z.object({ handleId: z.string() }), ({ handleId }) => fileWriters.close(handleId));
  handle(CHANNELS.fsAbortWrite, z.object({ handleId: z.string() }), ({ handleId }) => fileWriters.abort(handleId));
  handle(CHANNELS.fsRevealInFolder, readFileArgsSchema, revealInFolder);
  • Step 6: Commit
git add apps/desktop/src/main/ipc/fs.ts apps/desktop/test/fs-writer.test.ts apps/desktop/src/main/index.ts
git commit -m "feat(desktop): native fs IPC (dialogs, read/write, streamed writer)"

Task 1.7: Renderer fs port — project save/open on desktop (§5 #4)

Files:

  • Modify: apps/web/src/services/project-manager.ts (lines 44, 169, 269, 301, 341, 405, 594)

  • Test: apps/web/src/services/project-manager.test.ts (new or extend existing)

  • Step 1: Write a failing test with a mocked window.openreel

Create apps/web/src/services/project-manager.desktop.test.ts:

import { describe, it, expect, beforeEach, vi } from "vitest";
import { projectManager } from "./project-manager";

const store = new Map<string, string>();

beforeEach(() => {
  store.clear();
  (window as any).openreel = {
    platform: "desktop",
    fs: {
      showSaveDialog: vi.fn(async () => "/tmp/proj.oreel"),
      showOpenDialog: vi.fn(async () => "/tmp/proj.oreel"),
      writeFile: vi.fn(async (p: string, d: string) => { store.set(p, d); }),
      readFile: vi.fn(async (p: string) => store.get(p) ?? ""),
    },
  };
});

const project: any = { id: "p1", name: "Demo", timeline: { duration: 1, tracks: [] } };

describe("ProjectManager desktop fs", () => {
  it("saveProjectAs writes via window.openreel.fs and round-trips", async () => {
    const ok = await projectManager.saveProjectAs(project);
    expect(ok).toBe(true);
    expect((window as any).openreel.fs.writeFile).toHaveBeenCalled();
    const loaded = await projectManager.openProject();
    expect(loaded?.name).toBe("Demo");
  });
});

Confirm the exported singleton name (projectManager) against the bottom of project-manager.ts; adjust the import if it differs.

  • Step 2: Run to verify failure

Run: pnpm --filter @openreel/web test:run project-manager.desktop Expected: FAIL (desktop branch not implemented; likely throws or returns false).

  • Step 3: Widen the file-ref types and add the desktop helper

At the top of project-manager.ts, after the existing interfaces (~line 31), add:

type NativeFileRef = { kind: "native"; path: string };
type ProjectFileRef = FileSystemFileHandle | NativeFileRef;

function isDesktopFs(): boolean {
  return typeof window !== "undefined" && !!window.openreel?.fs;
}
function isNativeRef(ref: unknown): ref is NativeFileRef {
  return !!ref && typeof ref === "object" && (ref as NativeFileRef).kind === "native";
}

Change RecentProject.fileHandle (line 44) to fileHandle?: ProjectFileRef;, the private field (line 169) to private currentFileHandle: ProjectFileRef | null = null;, and getCurrentFileHandle() (line 594) return type to ProjectFileRef | null.

  • Step 4: Add the desktop branch to saveProjectAs (top of the method, line 269)
  async saveProjectAs(project: Project): Promise<boolean> {
    if (isDesktopFs()) {
      const filePath = await window.openreel!.fs.showSaveDialog({
        defaultPath: `${project.name}.oreel`,
        filters: [{ name: "OpenReel Project", extensions: ["oreel", "json"] }],
      });
      if (!filePath) return false;
      await window.openreel!.fs.writeFile(filePath, JSON.stringify(project, null, 2));
      this.currentFileHandle = { kind: "native", path: filePath };
      await this.addToRecent(project, this.currentFileHandle);
      this.emit("projectSaved", { project });
      return true;
    }
    if (!("showSaveFilePicker" in window)) {
      return this.downloadProject(project);
    }
    // ...existing File System Access API path unchanged...
  • Step 5: Add the desktop branch to saveToFileHandle (line 301) for re-saves
  private async saveToFileHandle(project: Project, handle: ProjectFileRef): Promise<boolean> {
    try {
      if (isNativeRef(handle)) {
        await window.openreel!.fs.writeFile(handle.path, JSON.stringify(project, null, 2));
        this.emit("projectSaved", { project });
        return true;
      }
      const writable = await handle.createWritable();
      const data = JSON.stringify(project, null, 2);
      await writable.write(data);
      await writable.close();
      this.emit("projectSaved", { project });
      return true;
    } catch (error) {
      console.error("[ProjectManager] Save to file failed:", error);
      return false;
    }
  }
  • Step 6: Add the desktop branch to openProject (top, line 341)
  async openProject(): Promise<Project | null> {
    if (isDesktopFs()) {
      const filePath = await window.openreel!.fs.showOpenDialog({
        filters: [{ name: "OpenReel Project", extensions: ["oreel", "json"] }],
      });
      if (!filePath) return null;
      const content = await window.openreel!.fs.readFile(filePath);
      const project = JSON.parse(content) as Project;
      this.currentFileHandle = { kind: "native", path: filePath };
      await this.addToRecent(project, this.currentFileHandle);
      this.emit("projectOpened", { project });
      return project;
    }
    if ("showOpenFilePicker" in window) {
      // ...existing path unchanged...
  • Step 7: Add the desktop branch to openRecentProject (line 405)

At the top of the if (recentProject.fileHandle) block, before the permission dance:

      if (isNativeRef(recentProject.fileHandle)) {
        try {
          const content = await window.openreel!.fs.readFile(recentProject.fileHandle.path);
          const project = JSON.parse(content) as Project;
          this.currentFileHandle = recentProject.fileHandle;
          await this.updateRecentTimestamp(recentProject.id);
          this.emit("projectOpened", { project });
          return project;
        } catch (error) {
          console.error("[ProjectManager] Open recent (native) failed:", error);
          await this.removeFromRecent(recentProject.id);
          return null;
        }
      }

Also update addToRecent's fileHandle? parameter type (line 486) to ProjectFileRef.

  • Step 8: Run to verify pass + no web regression

Run: pnpm --filter @openreel/web test:run project-manager Expected: PASS (desktop test + any existing project-manager tests still green).

  • Step 9: Commit
git add apps/web/src/services/project-manager.ts apps/web/src/services/project-manager.desktop.test.ts
git commit -m "feat(web): native project save/open on desktop via window.openreel.fs"

Task 1.8: Phase 1 manual acceptance

Files: none (verification only)

  • Step 1: Build and launch the desktop app

Run: pnpm --filter @openreel/desktop build && pnpm --filter @openreel/desktop start

  • Step 2: Walk the acceptance path

In the running desktop app: import a media file (native dialog), add it to the timeline, scrub/play, save the project (native save dialog → .oreel on disk), reload the project, then export a short MP4 via the existing export (wasm/WebCodecs) path. Confirm the exported file plays.

  • Step 3: Confirm isolation + no console errors

Open DevTools console. Confirm crossOriginIsolated === true and there are no failed network requests (fonts/wasm/workers all same-origin).

  • Step 4: Commit a phase marker (docs only)
git commit --allow-empty -m "chore(desktop): Phase 1 acceptance — editor runs under app:// with native fs"

PHASE 2 — Native export offload

Phase exit: export a 4K, >2-min, ProRes/10-bit project with hardware encode, streamed to disk, faster than the wasm path, with accurate progress; golden-frame parity vs WebCodecs within tolerance; timing/audio fixtures pass.

Task 2.1: MessagePort handoff spike under sandbox: true

Files:

  • Create: apps/desktop/src/main/spike-port.ts (temporary)

  • Modify: apps/desktop/src/main/index.ts (temporary wiring)

  • Modify: apps/desktop/src/preload/index.ts (temporary relay)

  • Step 1: Add a temporary main-side port emitter apps/desktop/src/main/spike-port.ts

import { MessageChannelMain, type WebContents } from "electron";

export function sendSpikePort(wc: WebContents): void {
  const { port1, port2 } = new MessageChannelMain();
  port1.on("message", (e) => {
    const buf = e.data as ArrayBuffer;
    port1.postMessage({ echoedBytes: buf.byteLength });
  });
  port1.start();
  wc.postMessage("openreel:spike-port", null, [port2]);
}
  • Step 2: Temporarily emit the port after the window loads (index.ts)

In createWindow, after win.loadURL(APP_INDEX);:

  win.webContents.on("did-finish-load", () => {
    import("./spike-port").then((m) => m.sendSpikePort(win.webContents));
  });
  • Step 3: Temporarily relay the port in preload (preload/index.ts)
ipcRenderer.on("openreel:spike-port", (event) => {
  const [port] = event.ports;
  (window as any).__spikePort = port;
  port.start();
  const probe = new Uint8Array(1024).buffer;
  port.postMessage(probe, [probe]);
  port.onmessage = (e) => { (window as any).__spikeEcho = e.data; };
});
  • Step 4: Build, launch, confirm the round-trip (capture from MAIN stdout, not DevTools)

RESOLVED (Phase-2.1 spike result): window.__spikeEcho set in the preload lives in the isolated world and is invisible to DevTools/executeJavaScript under contextIsolation — so verify from the MAIN process stdout instead. Finding: the MessageChannelMain port handoff works under sandbox: true, BUT ArrayBuffers are NOT transferable over Electron MessagePorts — a buffer placed in the transfer list detaches on the renderer and arrives as null on main (silent data loss). Sending the buffer as the message value with no transfer list delivers intact bytes by structured-clone copy. The export transport (Tasks 2.6/2.8) is written against the copy model; frame data must never be in a transfer list.

Run a headless launch that logs the received byte count from the main-side port listener (SPIKE_RECV_BYTES=), not a DevTools read. Expected: SPIKE_RECV_BYTES=1024 + SPIKE_IS_ARRAYBUFFER=true (copy path), and null when a transfer list is (wrongly) used.

  • Step 5: Revert the spike wiring and commit the finding

Remove spike-port.ts, the did-finish-load block, and the preload spike relay.

git add -A
git commit -m "chore(desktop): verify MessagePort transferable handoff under sandbox (spike, reverted)"

Task 2.2: Bundle a native FFmpeg binary + path resolver

Files:

  • Create: apps/desktop/resources/bin/MANIFEST.json

  • Create: apps/desktop/src/main/sidecar/ffmpeg-path.ts

  • Create: apps/desktop/test/ffmpeg-path.test.ts

  • Place (manually): apps/desktop/resources/bin/<platform-arch>/ffmpeg[.exe]

  • Step 1: Obtain a static FFmpeg binary for the current dev platform

Download a static FFmpeg with hardware encoders for your dev OS/arch (macOS: a videotoolbox-enabled build; Windows: BtbN gpl build; Linux: BtbN/johnvansickle). Place it at apps/desktop/resources/bin/<darwin-arm64|darwin-x64|win32-x64|linux-x64>/ffmpeg (.exe on Windows). Record version + sha256 in MANIFEST.json:

{
  "ffmpegVersion": "7.1",
  "binaries": {
    "darwin-arm64": { "file": "darwin-arm64/ffmpeg", "sha256": "<fill>" }
  },
  "license": "GPL (separate-process invocation; see LICENSES/FFMPEG.md)"
}

Only the dev platform's binary is needed now; the other targets are added in Phase 5 CI. Do not commit large binaries to git history if the repo forbids it — instead add apps/desktop/resources/bin/**/ffmpeg* to .gitignore and document the download in MANIFEST.json. Confirm the repo's binary policy before committing.

  • Step 2: Write the failing test apps/desktop/test/ffmpeg-path.test.ts
import { describe, it, expect } from "vitest";
import { ffmpegRelativePath } from "../src/main/sidecar/ffmpeg-path";

describe("ffmpegRelativePath", () => {
  it("maps darwin/arm64 to the right subpath", () => {
    expect(ffmpegRelativePath("darwin", "arm64")).toBe("darwin-arm64/ffmpeg");
  });
  it("adds .exe on win32", () => {
    expect(ffmpegRelativePath("win32", "x64")).toBe("win32-x64/ffmpeg.exe");
  });
});
  • Step 2b: Run to verify failure

Run: pnpm --filter @openreel/desktop test:run ffmpeg-path Expected: FAIL — module missing.

  • Step 3: Create apps/desktop/src/main/sidecar/ffmpeg-path.ts
import path from "node:path";
import { app } from "electron";

export function ffmpegRelativePath(platform: NodeJS.Platform, arch: string): string {
  const dir = `${platform}-${arch}`;
  const bin = platform === "win32" ? "ffmpeg.exe" : "ffmpeg";
  return `${dir}/${bin}`;
}

export function resolveFfmpegPath(): string {
  const rel = ffmpegRelativePath(process.platform, process.arch);
  const base = app.isPackaged
    ? path.join(process.resourcesPath, "bin")
    : path.join(__dirname, "../../resources/bin");
  return path.join(base, rel);
}
  • Step 4: Run to verify pass

Run: pnpm --filter @openreel/desktop test:run ffmpeg-path Expected: PASS.

  • Step 5: Commit
git add apps/desktop/resources/bin/MANIFEST.json apps/desktop/src/main/sidecar/ffmpeg-path.ts apps/desktop/test/ffmpeg-path.test.ts
git commit -m "feat(desktop): bundled ffmpeg path resolver + manifest"

Task 2.3: Encoder probe + selection (pure logic, TDD)

Files:

  • Create: apps/desktop/src/main/sidecar/encoder-probe.ts

  • Create: apps/desktop/test/encoder-probe.test.ts

  • Step 1: Write the failing test apps/desktop/test/encoder-probe.test.ts

import { describe, it, expect } from "vitest";
import { parseEncoders, selectEncoder } from "../src/main/sidecar/encoder-probe";

const SAMPLE = `
 V....D h264_videotoolbox    VideoToolbox H.264 Encoder
 V....D hevc_videotoolbox    VideoToolbox HEVC Encoder
 V....D libx264              libx264 H.264
 V....D prores_videotoolbox  Apple ProRes
`;

describe("encoder probe", () => {
  it("parses encoder names from ffmpeg -encoders output", () => {
    const names = parseEncoders(SAMPLE);
    expect(names).toContain("h264_videotoolbox");
    expect(names).toContain("libx264");
  });

  it("selects videotoolbox h264 on darwin when present", () => {
    const names = parseEncoders(SAMPLE);
    expect(selectEncoder("h264", "darwin", names)).toBe("h264_videotoolbox");
  });

  it("falls back to libx264 when no hw encoder present", () => {
    expect(selectEncoder("h264", "linux", ["libx264"])).toBe("libx264");
  });

  it("selects prores_videotoolbox for prores on darwin", () => {
    const names = parseEncoders(SAMPLE);
    expect(selectEncoder("prores", "darwin", names)).toBe("prores_videotoolbox");
  });
});
  • Step 2: Run to verify failure

Run: pnpm --filter @openreel/desktop test:run encoder-probe Expected: FAIL — module missing.

  • Step 3: Create apps/desktop/src/main/sidecar/encoder-probe.ts
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { resolveFfmpegPath } from "./ffmpeg-path";

const run = promisify(execFile);

export function parseEncoders(stdout: string): string[] {
  return stdout
    .split(/\r?\n/)
    .map((line) => line.trim())
    .filter((line) => /^[VAS]\S{5}\s/.test(line))
    .map((line) => line.split(/\s+/)[1])
    .filter(Boolean);
}

type Codec = "h264" | "h265" | "hevc" | "av1" | "prores" | "vp9";

const PREFERENCE: Record<string, Partial<Record<NodeJS.Platform | "any", string[]>>> = {
  h264: {
    darwin: ["h264_videotoolbox"],
    win32: ["h264_nvenc", "h264_qsv", "h264_amf"],
    linux: ["h264_nvenc", "h264_vaapi"],
    any: ["libx264"],
  },
  hevc: {
    darwin: ["hevc_videotoolbox"],
    win32: ["hevc_nvenc", "hevc_qsv", "hevc_amf"],
    linux: ["hevc_nvenc", "hevc_vaapi"],
    any: ["libx265"],
  },
  av1: {
    win32: ["av1_nvenc", "av1_qsv", "av1_amf"],
    linux: ["av1_nvenc", "av1_vaapi"],
    any: ["libsvtav1"],
  },
  prores: {
    darwin: ["prores_videotoolbox", "prores_ks"],
    any: ["prores_ks"],
  },
};

export function selectEncoder(codec: string, platform: NodeJS.Platform, available: string[]): string | null {
  const key = codec === "h265" ? "hevc" : codec;
  const pref = PREFERENCE[key];
  if (!pref) return null;
  const candidates = [...(pref[platform] ?? []), ...(pref.any ?? [])];
  return candidates.find((c) => available.includes(c)) ?? null;
}

export async function probeEncoders(): Promise<string[]> {
  try {
    const { stdout } = await run(resolveFfmpegPath(), ["-hide_banner", "-encoders"], { maxBuffer: 4 * 1024 * 1024 });
    return parseEncoders(stdout);
  } catch {
    return [];
  }
}
  • Step 4: Run to verify pass

Run: pnpm --filter @openreel/desktop test:run encoder-probe Expected: PASS (4 tests).

  • Step 5: Wire probed encoders into collectHardwareInfo

In apps/desktop/src/main/ipc/hardware.ts, import probeEncoders and set encoders: await probeEncoders() instead of encoders: [].

  • Step 6: Commit
git add apps/desktop/src/main/sidecar/encoder-probe.ts apps/desktop/test/encoder-probe.test.ts apps/desktop/src/main/ipc/hardware.ts
git commit -m "feat(desktop): ffmpeg encoder probe + per-platform selection"

Task 2.4: CFR frame-grid mapping (pure logic, TDD)

Files:

  • Create: apps/desktop/src/main/sidecar/cfr.ts

  • Create: apps/desktop/test/cfr.test.ts

  • Step 1: Write the failing test apps/desktop/test/cfr.test.ts

import { describe, it, expect } from "vitest";
import { frameIndexForTimestamp, totalGridFrames } from "../src/main/sidecar/cfr";

describe("CFR mapping", () => {
  it("maps timestamps to a 30fps grid", () => {
    expect(frameIndexForTimestamp(0, 30)).toBe(0);
    expect(frameIndexForTimestamp(0.0166, 30)).toBe(0);
    expect(frameIndexForTimestamp(0.0334, 30)).toBe(1);
    expect(frameIndexForTimestamp(1.0, 30)).toBe(30);
  });

  it("computes total grid frames from duration", () => {
    expect(totalGridFrames(2.0, 30)).toBe(60);
    expect(totalGridFrames(2.0167, 30)).toBe(61);
  });
});
  • Step 2: Run to verify failure

Run: pnpm --filter @openreel/desktop test:run cfr Expected: FAIL — module missing.

  • Step 3: Create apps/desktop/src/main/sidecar/cfr.ts
export function frameIndexForTimestamp(timestampSec: number, fps: number): number {
  return Math.round(timestampSec * fps);
}

export function totalGridFrames(durationSec: number, fps: number): number {
  return Math.ceil(durationSec * fps);
}

export class CfrWriter {
  private lastWrittenIndex = -1;
  constructor(private readonly fps: number, private readonly write: (frame: Uint8Array) => Promise<void>) {}

  async push(frame: Uint8Array, timestampSec: number): Promise<void> {
    const targetIndex = frameIndexForTimestamp(timestampSec, this.fps);
    if (targetIndex <= this.lastWrittenIndex) return;
    for (let i = this.lastWrittenIndex + 1; i < targetIndex; i++) {
      await this.write(frame);
    }
    await this.write(frame);
    this.lastWrittenIndex = targetIndex;
  }
}
  • Step 4: Run to verify pass

Run: pnpm --filter @openreel/desktop test:run cfr Expected: PASS.

  • Step 5: Commit
git add apps/desktop/src/main/sidecar/cfr.ts apps/desktop/test/cfr.test.ts
git commit -m "feat(desktop): CFR frame-grid mapping (dup/drop to constant fps)"

Task 2.5: Export job — spawn ffmpeg, stream raw frames, parse progress

Files:

  • Create: apps/desktop/src/main/sidecar/export-job.ts

  • Create: apps/desktop/test/progress-parse.test.ts

  • Step 1: Write the failing test for progress parsing apps/desktop/test/progress-parse.test.ts

import { describe, it, expect } from "vitest";
import { parseProgressBlock } from "../src/main/sidecar/export-job";

describe("parseProgressBlock", () => {
  it("extracts frame number from ffmpeg -progress output", () => {
    const block = "frame=42\nfps=30\nbitrate=...\nprogress=continue\n";
    expect(parseProgressBlock(block)?.frame).toBe(42);
  });
  it("detects end", () => {
    expect(parseProgressBlock("frame=100\nprogress=end\n")?.done).toBe(true);
  });
});
  • Step 2: Run to verify failure

Run: pnpm --filter @openreel/desktop test:run progress-parse Expected: FAIL — module missing.

  • Step 3: Create apps/desktop/src/main/sidecar/export-job.ts
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
import { resolveFfmpegPath } from "./ffmpeg-path";

export interface ExportArgs {
  width: number;
  height: number;
  frameRate: number;
  encoder: string;
  bitrateKbps: number;
  outputPath: string;
  audioWavPath: string;
  format: string;
}

export function parseProgressBlock(block: string): { frame: number; done: boolean } | null {
  const frameMatch = block.match(/frame=(\d+)/);
  if (!frameMatch) return null;
  return { frame: Number(frameMatch[1]), done: /progress=end/.test(block) };
}

export function buildFfmpegArgs(a: ExportArgs): string[] {
  return [
    "-y",
    "-f", "rawvideo",
    "-pix_fmt", "rgba",
    "-s", `${a.width}x${a.height}`,
    "-r", String(a.frameRate),
    "-i", "pipe:0",
    "-i", a.audioWavPath,
    "-c:v", a.encoder,
    "-b:v", `${a.bitrateKbps}k`,
    "-pix_fmt", "yuv420p",
    "-c:a", "aac",
    "-shortest",
    "-progress", "pipe:2",
    a.outputPath,
  ];
}

export class ExportJob {
  private proc: ChildProcessWithoutNullStreams | null = null;
  private progressBuf = "";

  constructor(
    private readonly args: ExportArgs,
    private readonly onFrame: (frame: number) => void,
    private readonly onDone: () => void,
    private readonly onError: (msg: string) => void,
  ) {}

  start(): void {
    this.proc = spawn(resolveFfmpegPath(), buildFfmpegArgs(this.args));
    this.proc.stderr.on("data", (chunk: Buffer) => {
      this.progressBuf += chunk.toString();
      const blocks = this.progressBuf.split(/progress=(continue|end)\n/);
      // keep tail; emit on each complete block
      const parsed = parseProgressBlock(this.progressBuf);
      if (parsed) this.onFrame(parsed.frame);
    });
    this.proc.on("error", (e) => this.onError(e.message));
    this.proc.on("close", (code) => {
      if (code === 0) this.onDone();
      else this.onError(`ffmpeg exited with code ${code}`);
    });
  }

  async writeFrame(frame: Uint8Array): Promise<void> {
    if (!this.proc) throw new Error("job not started");
    if (!this.proc.stdin.write(frame)) {
      await new Promise<void>((resolve) => this.proc!.stdin.once("drain", resolve));
    }
  }

  endInput(): void {
    this.proc?.stdin.end();
  }

  cancel(): void {
    this.proc?.kill("SIGKILL");
  }
}

Backpressure is handled by honoring stdin.write()'s return value + drain (credit-based at the OS-pipe level). The renderer's MessagePort credits (Task 2.7) gate further upstream.

  • Step 4: Run to verify pass

Run: pnpm --filter @openreel/desktop test:run progress-parse Expected: PASS.

  • Step 5: Commit
git add apps/desktop/src/main/sidecar/export-job.ts apps/desktop/test/progress-parse.test.ts
git commit -m "feat(desktop): ffmpeg export job (rawvideo stdin + audio input + progress)"

Task 2.6: Export IPC — port handoff + audio WAV + job lifecycle

Files:

  • Create: apps/desktop/src/main/ipc/export.ts

  • Modify: apps/desktop/src/main/index.ts

  • Modify: apps/desktop/src/preload/index.ts

  • Step 1: Create apps/desktop/src/main/ipc/export.ts

import { MessageChannelMain, type WebContents } from "electron";
import path from "node:path";
import os from "node:os";
import { promises as fs } from "node:fs";
import { randomUUID } from "node:crypto";
import { ExportJob, type ExportArgs } from "../sidecar/export-job";
import { CfrWriter } from "../sidecar/cfr";
import { selectEncoder, probeEncoders } from "../sidecar/encoder-probe";

interface StartArgs {
  width: number; height: number; frameRate: number;
  codec: string; format: string; bitrateKbps: number;
  outputPath: string; totalFrames: number;
  audioSampleRate: number; audioChannels: number;
}

const INITIAL_CREDITS = 8;

interface JobEntry {
  job: ExportJob | null;
  cfr: CfrWriter | null;
  port: import("electron").MessagePortMain;
  exportArgs: ExportArgs;
  audioWavPath: string;
  started: boolean;
}

const jobs = new Map<string, JobEntry>();

function cleanupJob(jobId: string): void {
  const entry = jobs.get(jobId);
  if (!entry) return;
  void fs.rm(entry.audioWavPath, { force: true }).catch(() => {});
  jobs.delete(jobId);
}

// CORRECTED (vs the original draft): ffmpeg is NOT spawned here. `-i audio.wav` is opened at
// spawn, so an empty placeholder WAV would make ffmpeg error. Per spec §6.4 the audio file must
// be COMPLETE before spawn — so startExport only wires the transport; writeAudioWav spawns.
export async function startExport(wc: WebContents, args: StartArgs): Promise<{ jobId: string }> {
  const jobId = randomUUID();
  const encoders = await probeEncoders();
  const encoder = selectEncoder(args.codec, process.platform, encoders) ?? "libx264";
  const audioWavPath = path.join(os.tmpdir(), `openreel-${jobId}.wav`);

  const exportArgs: ExportArgs = {
    width: args.width, height: args.height, frameRate: args.frameRate,
    encoder, bitrateKbps: args.bitrateKbps, outputPath: args.outputPath,
    audioWavPath, format: args.format,
  };

  const { port1, port2 } = new MessageChannelMain();
  const entry: JobEntry = { job: null, cfr: null, port: port1, exportArgs, audioWavPath, started: false };

  port1.on("message", async (e) => {
    const msg = e.data as { type: string; ts?: number; buffer?: ArrayBuffer };
    if (msg.type === "frame" && msg.buffer) {
      if (!entry.cfr) return; // frames are gated on the post-spawn credit grant; ignore strays
      try {
        await entry.cfr.push(new Uint8Array(msg.buffer), msg.ts ?? 0);
        port1.postMessage({ type: "credit", credits: 1 });
      } catch (err) {
        port1.postMessage({ type: "error", message: err instanceof Error ? err.message : String(err) });
      }
    } else if (msg.type === "finish") {
      entry.job?.endInput();
    }
  });
  port1.start();

  // ONLY the port is in the transfer list — NEVER a frame buffer (Phase-2.1 spike: ArrayBuffers
  // in a transfer list silently deliver null). Frame bytes arrive by structured-clone copy.
  wc.postMessage("openreel:export-port", { jobId }, [port2]);
  jobs.set(jobId, entry);
  return { jobId };
}

export async function writeAudioWav(args: { jobId: string; wav: ArrayBuffer }): Promise<void> {
  const entry = jobs.get(args.jobId);
  if (!entry) throw new Error(`unknown export job ${args.jobId}`);
  await fs.writeFile(entry.audioWavPath, Buffer.from(args.wav)); // complete WAV on disk FIRST
  if (entry.started) return;

  entry.job = new ExportJob(
    entry.exportArgs,
    (frame) => entry.port.postMessage({ type: "progress", frame }),
    () => { entry.port.postMessage({ type: "done" }); cleanupJob(args.jobId); },
    (msg) => { entry.port.postMessage({ type: "error", message: msg }); cleanupJob(args.jobId); },
  );
  entry.cfr = new CfrWriter(entry.exportArgs.frameRate, async (f) => { await entry.job!.writeFrame(f); });
  entry.job.start(); // ffmpeg spawns now — both inputs valid
  entry.started = true;
  entry.port.postMessage({ type: "credit", credits: INITIAL_CREDITS }); // unblock the renderer
}

export async function cancelExport(args: { jobId: string }): Promise<void> {
  const entry = jobs.get(args.jobId);
  if (!entry) return;
  entry.job?.cancel();
  try { await fs.rm(entry.audioWavPath, { force: true }); } catch {}
  jobs.delete(args.jobId);
}

Sequencing (spec §6.4, CORRECTED): ffmpeg spawns inside writeAudioWav, AFTER the complete WAV is on disk — never with an empty placeholder. The renderer (Task 2.8) starts with 0 credits, MUST call export.writeAudioWav (a silent WAV if the project has no audio) BEFORE any frame, and only streams frames after the post-spawn INITIAL_CREDITS grant. done/error clean up the temp WAV + job entry (no session leak).

  • Step 2: Register export handlers + port relay

In apps/desktop/src/main/index.ts:

import { startExport, writeAudioWav, cancelExport } from "./ipc/export";

Inside app.whenReady():

  ipcMain.handle(CHANNELS.exportStart, (e, raw) => startExport(e.sender, raw));
  handle(CHANNELS.exportWriteAudioWav,
    z.object({ jobId: z.string(), wav: z.instanceof(ArrayBuffer) }), writeAudioWav);
  handle(CHANNELS.exportCancel, z.object({ jobId: z.string() }), cancelExport);

(Add import { ipcMain } from "electron"; if not present.)

  • Step 3: Relay the export port in preload

In apps/desktop/src/preload/index.ts, replace the export.start line so it resolves with the relayed port:

  export: {
    start: (args: unknown) =>
      new Promise((resolve) => {
        ipcRenderer.once("openreel:export-port", (event, meta) => {
          const [port] = event.ports;
          resolve({ jobId: meta.jobId, port });
        });
        ipcRenderer.invoke(CHANNELS.exportStart, args);
      }),
    writeAudioWav: (jobId: string, wav: ArrayBuffer) =>
      ipcRenderer.invoke(CHANNELS.exportWriteAudioWav, { jobId, wav }),
    cancel: (jobId: string) => ipcRenderer.invoke(CHANNELS.exportCancel, { jobId }),
  },
  • Step 4: Typecheck + build

Run: pnpm --filter @openreel/desktop typecheck && pnpm --filter @openreel/desktop build:main Expected: PASS.

  • Step 5: Commit
git add apps/desktop/src/main/ipc/export.ts apps/desktop/src/main/index.ts apps/desktop/src/preload/index.ts
git commit -m "feat(desktop): native export IPC (port handoff, audio wav, job lifecycle)"

Task 2.7: EncoderBackend interface + extract WebCodecsBackend (packages/core)

Files:

  • Create: packages/core/src/export/encoder-backend.ts (Contract B)

  • Create: packages/core/src/export/webcodecs-backend.ts

  • Modify: packages/core/src/export/export-engine.ts

  • Step 1: Create packages/core/src/export/encoder-backend.ts

Paste Contract B (the EncoderBackend interface + EncoderBackendFactory type). Verify the import type paths for VideoExportSettings/Project resolve; fix if needed.

  • Step 2: Extract the inline MediaBunny path into webcodecs-backend.ts

Create packages/core/src/export/webcodecs-backend.ts implementing EncoderBackend. Move the encode/mux logic currently inline in export-engine.ts exportVideo (the Output/VideoSampleSource/AudioBufferSource construction at ~311–395, the per-frame new VideoSample(...) + videoSource.add() at ~455–462, and videoSource.close() + output.finalize() + writableStream.close() at ~484–502) into:

import type { EncoderBackend } from "./encoder-backend";
import type { VideoExportSettings } from "./types";
import type { Project } from "../timeline/types";

export class WebCodecsBackend implements EncoderBackend {
  readonly requiresWebCodecsClamping = true;
  readonly needsFrameThrottling = true;

  private output: any = null;
  private videoSource: any = null;
  private audioSource: any = null;
  private writable?: FileSystemWritableFileStream;

  constructor(private readonly mediabunny: typeof import("mediabunny")) {}

  async start(settings: VideoExportSettings, project: Project, writableStream?: FileSystemWritableFileStream): Promise<void> {
    this.writable = writableStream;
    // ...moved verbatim from export-engine.ts lines 311-395 (Output/StreamTarget/
    //    Mp4OutputFormat selection, videoCodec/audioCodec resolution, VideoSampleSource
    //    with hardwareAcceleration:'prefer-software', addVideoTrack/addAudioTrack,
    //    setMetadataTags, await output.start()). Assign to this.output/videoSource/audioSource.
  }

  async addVideoFrame(frame: ImageBitmap, timestampSec: number, durationSec: number): Promise<void> {
    const { VideoSample } = this.mediabunny;
    const sample = new VideoSample(frame, { timestamp: timestampSec, duration: durationSec });
    await this.videoSource.add(sample);
    sample.close();
    frame.close();
  }

  async addAudioBuffer(buffer: AudioBuffer): Promise<void> {
    // ...moved from the existing AudioBufferSource.add path in export-engine.ts...
  }

  async finalize(): Promise<void> {
    this.videoSource.close();
    await this.output.finalize();
    await this.writable?.close();
  }

  async abort(): Promise<void> {
    try { await this.writable?.abort(); } catch {}
  }
}

Move the exact code from export-engine.ts into these methods — do not paraphrase. The hardwareAcceleration:'prefer-software' option and the MediaBunny destructuring belong here, not in the engine.

  • Step 3: Refactor export-engine.ts to delegate to a backend

  • Add a backend-factory field + setter near the singleton (line ~1437):

let encoderBackendFactory: EncoderBackendFactory | null = null;
export function setEncoderBackendFactory(factory: EncoderBackendFactory | null): void {
  encoderBackendFactory = factory;
}
  • In exportVideo, after settings merge/clamp, select the backend:
    const backend = encoderBackendFactory
      ? encoderBackendFactory(this.mediabunny)
      : new WebCodecsBackend(this.mediabunny!);
  • Guard the prores→h264 normalization (lines 239–244) and the resolution/fps clamping (lines 249–268) with if (backend.requiresWebCodecsClamping).

  • Replace the inline Output/videoSource construction with await backend.start(fullSettings, project, writableStream);.

  • Replace the per-frame new VideoSample(...)/videoSource.add() with await backend.addVideoFrame(frameImage, time, 1 / fullSettings.frameRate);.

  • Guard the every-5-frames GC/yield (lines 464–473) with if (backend.needsFrameThrottling).

  • Replace videoSource.close() + output.finalize() + writableStream.close() with await backend.finalize();.

  • On the error/abort path, call await backend.abort();.

  • Keep exportVideo's signature and yielded ExportProgress shape unchanged (Toolbar.tsx depends on it).

  • CRITICAL ORDERING (native backend, per spec §6.4 + Task 2.6 deferred-spawn): for NativeFFmpegBackend, backend.addAudioBuffer(...) MUST be called before the first backend.addVideoFrame(...). The native backend's addAudioBuffer writes the WAV (which is what triggers the main-process ffmpeg spawn + the initial credit grant); frames block on credits until then. So in exportVideo, ensure the audio mixdown + addAudioBuffer happens before the video render loop when the backend needs it. Expose this via a backend flag (e.g. audioBeforeVideo: boolean — true for native, false for WebCodecs which can interleave) OR simply always mix+send audio first. Additionally, the project may have no audio — in that case the native backend must still send a silent WAV (a zero-filled buffer of the timeline duration) before frames, or ffmpeg never spawns and the export hangs at 0 credits. Handle "no audio → silent WAV" in the NativeFFmpegBackend (Task 2.8) or ensure exportVideo always passes an AudioBuffer (silent if empty).

  • Step 4: Run the core export tests + typecheck

Run: pnpm --filter @openreel/core typecheck && pnpm --filter @openreel/core test:run Expected: PASS. If there are existing export tests, they must stay green (the default WebCodecsBackend path is behavior-preserving).

  • Step 5: Commit
git add packages/core/src/export/encoder-backend.ts packages/core/src/export/webcodecs-backend.ts packages/core/src/export/export-engine.ts
git commit -m "refactor(core): pluggable EncoderBackend; extract WebCodecsBackend from exportVideo"

Task 2.8: NativeFFmpegBackend (renderer) + wire factory on desktop

Files:

  • Create: apps/web/src/services/native-ffmpeg-backend.ts

  • Modify: apps/web/src/main.tsx

  • Step 1: Create apps/web/src/services/native-ffmpeg-backend.ts

import type { EncoderBackend } from "@openreel/core";
import type { VideoExportSettings } from "@openreel/core";

export class NativeFFmpegBackend implements EncoderBackend {
  readonly requiresWebCodecsClamping = false;
  readonly needsFrameThrottling = false;

  private jobId: string | null = null;
  private port: MessagePort | null = null;
  private credits = 0;
  private done: Promise<void> | null = null;
  private width = 0;
  private height = 0;
  private outputPath = "";

  constructor(private readonly resolveOutputPath: () => string) {}

  async start(settings: VideoExportSettings): Promise<void> {
    this.width = settings.width;
    this.height = settings.height;
    this.outputPath = this.resolveOutputPath();
    const session = await window.openreel!.export.start({
      width: settings.width,
      height: settings.height,
      frameRate: settings.frameRate,
      codec: settings.codec,
      format: settings.format,
      bitrateKbps: settings.bitrate ?? 25000,
      outputPath: this.outputPath,
      totalFrames: 0,
      audioSampleRate: settings.audioSettings.sampleRate ?? 48000,
      audioChannels: 2,
    });
    this.jobId = session.jobId;
    this.port = session.port;
    this.credits = 0; // start at 0; main grants INITIAL_CREDITS only AFTER it spawns ffmpeg (post writeAudioWav)
    this.done = new Promise<void>((resolve, reject) => {
      this.port!.onmessage = (e) => {
        const m = e.data as { type: string; credits?: number; message?: string };
        if (m.type === "credit") this.credits += m.credits ?? 0; // credits are DELTAS (main: +8 initial, +1 per consumed frame)
        else if (m.type === "done") resolve();
        else if (m.type === "error") reject(new Error(m.message));
      };
    });
    this.port.start();
  }

  async addAudioBuffer(buffer: AudioBuffer): Promise<void> {
    const wav = audioBufferToWav(buffer);
    await window.openreel!.export.writeAudioWav(this.jobId!, wav);
  }

  async addVideoFrame(frame: ImageBitmap, timestampSec: number): Promise<void> {
    const canvas = new OffscreenCanvas(this.width, this.height);
    const ctx = canvas.getContext("2d")!;
    ctx.drawImage(frame, 0, 0);
    const rgba = ctx.getImageData(0, 0, this.width, this.height).data.buffer;
    frame.close();
    while (this.credits <= 0) await new Promise((r) => setTimeout(r, 1));
    this.credits -= 1;
    // CRITICAL (Phase-2.1 spike): send the buffer as the message VALUE with NO transfer list.
    // Electron MessagePorts only transfer MessagePortMain handles, not ArrayBuffers — putting
    // `rgba` in a transfer list silently delivers `null` to main (data loss). Bytes travel by
    // structured-clone copy. Do NOT add a transfer-list argument here.
    this.port!.postMessage({ type: "frame", ts: timestampSec, buffer: rgba });
  }

  async finalize(): Promise<void> {
    this.port!.postMessage({ type: "finish" });
    await this.done;
  }

  async abort(): Promise<void> {
    if (this.jobId) await window.openreel!.export.cancel(this.jobId);
  }
}

function audioBufferToWav(buffer: AudioBuffer): ArrayBuffer {
  const numChannels = buffer.numberOfChannels;
  const sampleRate = buffer.sampleRate;
  const length = buffer.length * numChannels * 2 + 44;
  const out = new DataView(new ArrayBuffer(length));
  const writeString = (off: number, s: string) => { for (let i = 0; i < s.length; i++) out.setUint8(off + i, s.charCodeAt(i)); };
  writeString(0, "RIFF"); out.setUint32(4, length - 8, true); writeString(8, "WAVE");
  writeString(12, "fmt "); out.setUint32(16, 16, true); out.setUint16(20, 1, true);
  out.setUint16(22, numChannels, true); out.setUint32(24, sampleRate, true);
  out.setUint32(28, sampleRate * numChannels * 2, true); out.setUint16(32, numChannels * 2, true);
  out.setUint16(34, 16, true); writeString(36, "data"); out.setUint32(40, length - 44, true);
  let off = 44;
  for (let i = 0; i < buffer.length; i++) {
    for (let ch = 0; ch < numChannels; ch++) {
      const s = Math.max(-1, Math.min(1, buffer.getChannelData(ch)[i]));
      out.setInt16(off, s < 0 ? s * 0x8000 : s * 0x7fff, true);
      off += 2;
    }
  }
  return out.buffer;
}

Confirm EncoderBackend and VideoExportSettings are exported from @openreel/core's src/index.ts; if not, add the exports as part of this task. The audioBufferToWav helper produces the WAV the main process declares as the -i audio.wav input.

  • Step 2: Wire the factory on desktop in apps/web/src/main.tsx

After the imports, before ReactDOM.createRoot(...):

import { setEncoderBackendFactory } from "@openreel/core";
import { NativeFFmpegBackend } from "./services/native-ffmpeg-backend";

if (typeof window !== "undefined" && window.openreel?.platform === "desktop") {
  setEncoderBackendFactory(() => new NativeFFmpegBackend(() => (window as any).__openreelExportPath));
}

__openreelExportPath is set by Toolbar before export (Task 2.9) from the native save dialog, so the backend writes to the user's chosen path.

  • Step 3: Typecheck

Run: pnpm --filter @openreel/web typecheck Expected: PASS.

  • Step 4: Commit
git add apps/web/src/services/native-ffmpeg-backend.ts apps/web/src/main.tsx
git commit -m "feat(web): NativeFFmpegBackend + desktop encoder-backend wiring"

Task 2.9: Toolbar desktop save path → native export (§ Toolbar seam)

Files:

  • Modify: apps/web/src/components/editor/Toolbar.tsx (showSavePicker ~249, wav fork ~373)

  • Step 1: Add the desktop branch to the top of showSavePicker (line 249)

Immediately after computing mime (after line ~256), before the if ("showSaveFilePicker" in window) check:

    if (typeof window.openreel?.fs?.showSaveDialog === "function") {
      const chosen = await window.openreel.fs.showSaveDialog({
        defaultPath: filename,
        filters: [{ name: "Media file", extensions: [ext] }],
      });
      if (!chosen) {
        const err = new DOMException("User cancelled", "AbortError");
        throw err;
      }
      (window as any).__openreelExportPath = chosen;
      const handleId = await window.openreel.fs.openWrite(chosen);
      return {
        seek: () => Promise.resolve(),
        async write(data: unknown) {
          let buf: ArrayBuffer; let pos = 0;
          if (data instanceof ArrayBuffer) buf = data;
          else if (ArrayBuffer.isView(data)) buf = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
          else return;
          await window.openreel!.fs.writeChunk(handleId, buf, pos);
          pos += buf.byteLength;
        },
        close: () => window.openreel!.fs.closeWrite(handleId),
        abort: () => window.openreel!.fs.abortWrite(handleId),
        truncate: () => Promise.resolve(),
      } as unknown as FileSystemWritableFileStream;
    }

Note: for the native export path the file is written by FFmpeg in the main process, not by this writable — NativeFFmpegBackend.finalize() produces the file at __openreelExportPath. This writable still satisfies the wav/non-native export paths. The cursor handling here is simplified; for the wav pipeTo path it writes sequentially. If positioned writes are needed for the wav muxer, track a cursor like the RAM shim (Toolbar.tsx:304–316).

  • Step 2: Fix the wav result fork (line ~373)

Change if ("showSaveFilePicker" in window) to also accept desktop:

            if ("showSaveFilePicker" in window || window.openreel?.platform === "desktop") {
              await finalResult.blob.stream().pipeTo(writable as unknown as WritableStream<Uint8Array>);
            } else {
  • Step 3: Typecheck

Run: pnpm --filter @openreel/web typecheck Expected: PASS.

  • Step 4: Commit
git add apps/web/src/components/editor/Toolbar.tsx
git commit -m "feat(web): desktop save dialog + native export output path"

Task 2.10: Native export integration test (real ffmpeg)

Files:

  • Create: apps/desktop/test/export-integration.test.ts

  • Step 1: Write an integration test driving a real ffmpeg encode

import { describe, it, expect, afterAll } from "vitest";
import { tmpdir } from "node:os";
import path from "node:path";
import { promises as fs } from "node:fs";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { ExportJob, buildFfmpegArgs } from "../src/main/sidecar/export-job";
import { resolveFfmpegPath } from "../src/main/sidecar/ffmpeg-path";

const run = promisify(execFile);
const out = path.join(tmpdir(), `openreel-export-${Date.now()}.mp4`);
const wav = path.join(tmpdir(), `openreel-export-${Date.now()}.wav`);

describe("native export integration", () => {
  afterAll(async () => { await fs.rm(out, { force: true }); await fs.rm(wav, { force: true }); });

  it("encodes 30 solid RGBA frames + silent audio to a playable mp4", async () => {
    // 1s of silence wav
    await run(resolveFfmpegPath(), ["-y", "-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo", "-t", "1", wav]);

    const W = 320, H = 240, FPS = 30;
    const frame = new Uint8Array(W * H * 4).fill(128);
    await new Promise<void>((resolve, reject) => {
      const job = new ExportJob(
        { width: W, height: H, frameRate: FPS, encoder: "libx264", bitrateKbps: 2000, outputPath: out, audioWavPath: wav, format: "mp4" },
        () => {}, resolve, reject,
      );
      job.start();
      (async () => {
        for (let i = 0; i < FPS; i++) await job.writeFrame(frame);
        job.endInput();
      })();
    });

    const { stdout } = await run(resolveFfmpegPath(), ["-i", out, "-hide_banner"], {}).catch((e) => ({ stdout: String(e.stderr ?? e) }));
    expect(stdout).toMatch(/Video:/);
  }, 60000);
});

Requires libx264 in the bundled ffmpeg (GPL build) — if you bundled an LGPL build, change encoder to an available one (h264_videotoolbox on macOS). This test is the throughput/correctness anchor for §6.4.

  • Step 2: Run the integration test

Run: pnpm --filter @openreel/desktop test:run export-integration Expected: PASS — ffprobe/-i reports a Video: stream in the output mp4.

  • Step 3: Commit
git add apps/desktop/test/export-integration.test.ts
git commit -m "test(desktop): native ffmpeg export integration (frames+audio→mp4)"

Task 2.11: Phase 2 manual acceptance + golden-frame parity

Files: none (verification) + optional apps/desktop/test/parity.md checklist

  • Step 1: Build and launch

Run: pnpm --filter @openreel/desktop build && pnpm --filter @openreel/desktop start

  • Step 2: Export a demanding project

Build/open a project that is 4K, > 2 minutes, and request ProRes (or H.265 10-bit). Export. Confirm: (a) the native save dialog appears; (b) the file is written directly to the chosen path (no in-RAM blob); (c) the resolution/fps are not clamped to 1080p/30 (spec §6.2 caps lifted in desktop mode); (d) progress advances; (e) the output opens in a player and matches the preview.

  • Step 3: Compare against the web path (parity)

Export the same short project (a) in the desktop native path and (b) in a browser (WebCodecs path). Decode a few frames from each (e.g. ffmpeg -i out.mp4 -vf "select=eq(n\,30)" -vframes 1 frame.png) and confirm visual equivalence within tolerance. Record results in apps/desktop/test/parity.md.

  • Step 4: Commit the phase marker
git add apps/desktop/test/parity.md
git commit -m "chore(desktop): Phase 2 acceptance — native hardware export with parity"

Self-Review

Spec coverage (Phases 0–2 sections of the spec):

  • §4.1 monorepo apps/desktop → Task 0.1 ✓
  • §4.2 security model (contextIsolation/sandbox/nodeIntegration/zod) → Tasks 0.2, 0.4 ✓
  • §4.3 app:// + COOP/COEP + scheme flags + COEP asset smoke → Tasks 0.3, 1.5 (Step 3) ✓
  • §4.4 IPC contract (request/response, jobs, streams/MessagePort) → Tasks 0.4, 2.6 ✓
  • §5 #1 vite desktop mode → 1.1 ✓; #3 local wasm core → 1.3 ✓; #4 fs bridge → 1.6/1.7 ✓; #6 SW skip → 1.4 ✓; #8 window size → 0.2 ✓; #10 fonts → 1.2 ✓. (#2 api-proxy, #5 capture, #7 share-link, #9 keychain are Phase 3/4 — out of this plan's 0–2 scope, intentionally deferred.)
  • §6.1 ffmpeg bundling → 2.2 ✓; §6.2 encoder matrix + lifted caps → 2.3 + 2.7(Step 3) ✓; §6.3 job orchestration → 2.5/2.6 ✓; §6.4 backend, MessagePort, buffer transport, CFR, audio-temp-file, sequencing → 2.1/2.4/2.5/2.6/2.7/2.8/2.9 ✓; §6.7 hardware probe → 0.5/2.3 ✓.
  • §11 testing (IPC zod, native parity, export timing, sidecar jobs) → 0.4, 2.3, 2.4, 2.10, 2.11 ✓.

Known deferrals (correct for a 0–2 plan): keychain/BYOK (§6.8, Phase 4), API-proxy desktop branch (§5 #2, Phase 4), native capture (§6.6, Phase 3), native transcode/proxy/decode (§6.5, Phase 3), share-link origin (§5 #7, Phase 4), packaging/signing/auto-update/CI (§8/§9, Phase 5). The window.openreel ambient type (Task 0.6) already declares keychain/export so those phases need no type rework.

Placeholder scan: The only deliberately-narrative steps are 2.7 Step 2/3 ("move verbatim from export-engine.ts" / guard specific line ranges) — these reference exact line numbers and the precise blocks to relocate rather than re-pasting ~190 lines of existing code; the surrounding method skeletons and all new logic are fully specified. Acceptable because the source is in-repo and quoting it whole would invite drift.

Type consistency: window.openreel shape (Contract A) is identical across preload (0.6), service-worker (1.4), project-manager (1.7), native backend (2.8), Toolbar (2.9). EncoderBackend (Contract B) members (requiresWebCodecsClamping, needsFrameThrottling, start/addVideoFrame/addAudioBuffer/finalize/abort) match between core interface (2.7), WebCodecsBackend (2.7), and NativeFFmpegBackend (2.8). IPC CHANNELS keys are defined once (0.4) and reused everywhere. Export message protocol (frame/credit/finish/progress/done/error) matches between main (2.6) and renderer backend (2.8).


Execution Handoff

Plan complete and saved to docs/superpowers/plans/2026-06-02-electron-desktop-phases-0-2.md. Two execution options:

1. Subagent-Driven (recommended) — I dispatch a fresh subagent per task, review between tasks, fast iteration.

2. Inline Execution — Execute tasks in this session using executing-plans, batch execution with checkpoints.

Which approach?