@alpha2-sh/editor 0.4.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/browser/bridge.d.ts +15 -16
  2. package/dist/browser/bridge.js +396 -312
  3. package/dist/browser/extensions-worker.d.ts +12 -16
  4. package/dist/browser/extensions-worker.js +17 -24
  5. package/dist/browser/hash-worker.d.ts +4 -5
  6. package/dist/browser/hash-worker.js +7 -8
  7. package/dist/browser/jobs.d.ts +17 -19
  8. package/dist/browser/jobs.js +41 -28
  9. package/dist/browser/resources.d.ts +6 -11
  10. package/dist/browser/resources.js +5 -9
  11. package/dist/browser/runtime/assets.d.ts +47 -28
  12. package/dist/browser/runtime/assets.js +129 -45
  13. package/dist/browser/runtime/audio.d.ts +16 -11
  14. package/dist/browser/runtime/audio.js +58 -31
  15. package/dist/browser/runtime/budget.d.ts +4 -5
  16. package/dist/browser/runtime/budget.js +4 -5
  17. package/dist/browser/runtime/caption-fonts.d.ts +3 -7
  18. package/dist/browser/runtime/caption-fonts.js +4 -8
  19. package/dist/browser/runtime/engine.d.ts +39 -44
  20. package/dist/browser/runtime/engine.js +78 -64
  21. package/dist/browser/runtime/export-audio.d.ts +6 -6
  22. package/dist/browser/runtime/export-audio.js +8 -9
  23. package/dist/browser/runtime/exporter.d.ts +33 -66
  24. package/dist/browser/runtime/exporter.js +111 -137
  25. package/dist/browser/runtime/extension-import.d.ts +28 -35
  26. package/dist/browser/runtime/extension-import.js +36 -46
  27. package/dist/browser/runtime/fingerprint.d.ts +11 -14
  28. package/dist/browser/runtime/fingerprint.js +20 -24
  29. package/dist/browser/runtime/generators.d.ts +2 -3
  30. package/dist/browser/runtime/generators.js +5 -7
  31. package/dist/browser/runtime/locks.d.ts +5 -7
  32. package/dist/browser/runtime/locks.js +5 -7
  33. package/dist/browser/runtime/output.d.ts +7 -9
  34. package/dist/browser/runtime/output.js +17 -21
  35. package/dist/browser/runtime/preview-utils.js +10 -6
  36. package/dist/browser/runtime/proxies.d.ts +10 -17
  37. package/dist/browser/runtime/proxies.js +33 -45
  38. package/dist/browser/runtime/proxy-storage.d.ts +15 -19
  39. package/dist/browser/runtime/proxy-storage.js +20 -24
  40. package/dist/browser/runtime/sources.d.ts +23 -31
  41. package/dist/browser/runtime/sources.js +85 -103
  42. package/dist/browser/runtime/staging.d.ts +2 -6
  43. package/dist/browser/runtime/staging.js +7 -11
  44. package/dist/browser/runtime/still.d.ts +2 -6
  45. package/dist/browser/runtime/still.js +6 -12
  46. package/dist/browser/runtime/text.d.ts +14 -0
  47. package/dist/browser/runtime/text.js +27 -0
  48. package/dist/browser/runtime/wasm.d.ts +4 -5
  49. package/dist/browser/runtime/wasm.js +4 -5
  50. package/dist/browser/types.d.ts +233 -238
  51. package/dist/browser/worker.d.ts +8 -9
  52. package/dist/browser/worker.js +50 -21
  53. package/dist/core/captions.d.ts +8 -10
  54. package/dist/core/captions.js +10 -18
  55. package/dist/core/clip-time.d.ts +8 -10
  56. package/dist/core/clip-time.js +12 -15
  57. package/dist/core/commands.d.ts +46 -46
  58. package/dist/core/commands.js +38 -36
  59. package/dist/core/compositions.d.ts +78 -112
  60. package/dist/core/compositions.js +66 -94
  61. package/dist/core/document.d.ts +59 -92
  62. package/dist/core/document.js +64 -90
  63. package/dist/core/keyframes.d.ts +11 -12
  64. package/dist/core/keyframes.js +11 -13
  65. package/dist/core/presets.d.ts +14 -20
  66. package/dist/core/presets.js +12 -18
  67. package/dist/core/session.d.ts +26 -38
  68. package/dist/core/session.js +38 -53
  69. package/dist/core/text.d.ts +2 -3
  70. package/dist/core/text.js +2 -3
  71. package/dist/core/timeline-index.d.ts +2 -3
  72. package/dist/core/timeline-index.js +11 -7
  73. package/dist/core/transitions.js +4 -5
  74. package/dist/core/validated.d.ts +6 -7
  75. package/dist/core/validated.js +8 -11
  76. package/dist/native-client/index.d.ts +31 -32
  77. package/dist/native-client/index.js +40 -41
  78. package/dist/ports/index.d.ts +19 -20
  79. package/dist/react/index.d.ts +5 -6
  80. package/dist/react/index.js +5 -6
  81. package/package.json +4 -4
@@ -1,8 +1,7 @@
1
1
  /**
2
- * Answers the call posted to `scope` (a dedicated worker's global): `{ type: "call", call, file,
3
- * wasmUrl, ... }` is answered with `{ type: "answered", result }`, or `{ type: "failed", name,
4
- * message }` when the file cannot be read or the provider refuses it. `{ type: "started" }` is
5
- * posted first, once the worker runs.
2
+ * Answers calls posted to `scope` (a dedicated worker's global): `{ type: "call", call, file,
3
+ * wasmUrl, ... }` gets `{ type: "answered", result }`, or `{ type: "failed", name, message }`
4
+ * when the file cannot be read or the provider refuses it. `{ type: "started" }` is posted first.
6
5
  * @param {{ onmessage: ((event: MessageEvent) => void) | null, postMessage(message: unknown): void }} scope
7
6
  */
8
7
  export function installExtensionsWorker(scope: {
@@ -10,13 +9,11 @@ export function installExtensionsWorker(scope: {
10
9
  postMessage(message: unknown): void;
11
10
  }): void;
12
11
  /**
13
- * Runs an extensions call: `request` names the call (`read`, `svg-diagnostics` or
14
- * `exr-first-frame`), the `file` it reads and what else the call takes. With `factory`, the call
15
- * runs in a worker it makes (one running {@link installExtensionsWorker}), which is terminated once
16
- * it answers, when `signal` aborts (rejecting with its reason), or after `timeoutMs` (rejecting as
17
- * `provider_stalled`). A worker that cannot start (its script does not load) leaves the call to
18
- * this thread, as it does without a factory; here the module call cannot be stopped, and `signal`
19
- * is checked between steps.
12
+ * Runs an extensions call (`read`, `svg-diagnostics` or `exr-first-frame`) on `request.file`.
13
+ * With `factory`, the call runs in a new worker running {@link installExtensionsWorker}, terminated
14
+ * once it answers, when `signal` aborts (rejecting with its reason), or after `timeoutMs`
15
+ * (rejecting as `provider_stalled`). Without a factory, or if the worker script fails to load, the
16
+ * call runs on this thread, where it cannot be stopped and `signal` is checked between steps.
20
17
  *
21
18
  * @param {{ factory?: () => Worker, wasmUrl?: string | URL }} module
22
19
  * @param {{ call: string, file: Blob & { name?: string }, [key: string]: unknown }} request
@@ -37,10 +34,9 @@ export function callExtensions({ factory, wasmUrl }: {
37
34
  operation?: string;
38
35
  }): Promise<any>;
39
36
  /**
40
- * How long one call in an extensions worker may run before the worker is terminated and the call
41
- * fails as `provider_stalled`: the 40 s a native host's decode shard may spend inside one provider
42
- * or decoder call (10 s plus the default `ALPHA2_GPU_TIMEOUT_MS`) before the export fails as
43
- * stalled. The provider's budgets refuse a file whose drawing they can count long before this; the
44
- * limit is for work they cannot.
37
+ * How long one extensions worker call may run before the worker is terminated and the call fails as
38
+ * `provider_stalled`. Matches the 40 s a native decode shard may spend in one provider or decoder
39
+ * call (10 s plus the default `ALPHA2_GPU_TIMEOUT_MS`). Provider budgets refuse countable work long
40
+ * before this; the limit covers the rest.
45
41
  */
46
42
  export const EXTENSIONS_CALL_TIMEOUT_MS: 40000;
@@ -1,15 +1,14 @@
1
- // Runs extension provider calls (probe, open, thumbnail) off the page's thread: each is one
2
- // synchronous call that can decode every frame or parse a file whole. A host bundling the
3
- // `@alpha2-sh/editor/extensions-worker` entry passes `runtimeAssets.extensionsWorkerFactory`; each
1
+ // Runs extension provider calls (probe, open, thumbnail) off the page's thread, since each is one
2
+ // synchronous call that may decode every frame or parse a whole file. Hosts bundling the
3
+ // `@alpha2-sh/editor/extensions-worker` entry pass `runtimeAssets.extensionsWorkerFactory`; each
4
4
  // call gets its own worker, terminated on cancel or past `EXTENSIONS_CALL_TIMEOUT_MS`.
5
5
  import { EditorError } from "../core/document.js";
6
6
  import { exrFirstFrame, readProviderFile, svgDiagnostics, } from "./runtime/extension-import.js";
7
7
  /**
8
- * How long one call in an extensions worker may run before the worker is terminated and the call
9
- * fails as `provider_stalled`: the 40 s a native host's decode shard may spend inside one provider
10
- * or decoder call (10 s plus the default `ALPHA2_GPU_TIMEOUT_MS`) before the export fails as
11
- * stalled. The provider's budgets refuse a file whose drawing they can count long before this; the
12
- * limit is for work they cannot.
8
+ * How long one extensions worker call may run before the worker is terminated and the call fails as
9
+ * `provider_stalled`. Matches the 40 s a native decode shard may spend in one provider or decoder
10
+ * call (10 s plus the default `ALPHA2_GPU_TIMEOUT_MS`). Provider budgets refuse countable work long
11
+ * before this; the limit covers the rest.
13
12
  */
14
13
  export const EXTENSIONS_CALL_TIMEOUT_MS = 40_000;
15
14
  /** Loads the provider module (`alpha2/extensions`) from `wasmUrl`, once per realm. */
@@ -17,16 +16,13 @@ async function loadModule(wasmUrl) {
17
16
  const { loadExtensions } = await import("alpha2/extensions");
18
17
  return loadExtensions(wasmUrl);
19
18
  }
20
- /** What each call reads of its file's bytes with the provider module. */
21
19
  const CALLS = {
22
- /** What import reads of a file: `null`, or its provider, report and thumbnail. */
23
20
  read: (extensions, bytes) => readProviderFile(extensions, bytes),
24
21
  /** An SVG's diagnostics with the font stack `fonts` (bytes, in fallback order). */
25
22
  "svg-diagnostics": (extensions, bytes, { provider, fonts }) => svgDiagnostics(extensions, provider, bytes, fonts),
26
23
  /** An OpenEXR sequence's first frame: its thumbnail and report. */
27
24
  "exr-first-frame": (extensions, bytes) => exrFirstFrame(extensions, bytes),
28
25
  };
29
- /** Runs `request` in this realm, checking `signal` between the steps it waits for. */
30
26
  async function run(request, wasmUrl, load, signal) {
31
27
  const call = CALLS[request.call];
32
28
  if (!call)
@@ -38,10 +34,9 @@ async function run(request, wasmUrl, load, signal) {
38
34
  return call(extensions, bytes, request);
39
35
  }
40
36
  /**
41
- * Answers the call posted to `scope` (a dedicated worker's global): `{ type: "call", call, file,
42
- * wasmUrl, ... }` is answered with `{ type: "answered", result }`, or `{ type: "failed", name,
43
- * message }` when the file cannot be read or the provider refuses it. `{ type: "started" }` is
44
- * posted first, once the worker runs.
37
+ * Answers calls posted to `scope` (a dedicated worker's global): `{ type: "call", call, file,
38
+ * wasmUrl, ... }` gets `{ type: "answered", result }`, or `{ type: "failed", name, message }`
39
+ * when the file cannot be read or the provider refuses it. `{ type: "started" }` is posted first.
45
40
  * @param {{ onmessage: ((event: MessageEvent) => void) | null, postMessage(message: unknown): void }} scope
46
41
  */
47
42
  export function installExtensionsWorker(scope) {
@@ -63,7 +58,7 @@ export function installExtensionsWorker(scope) {
63
58
  };
64
59
  scope.postMessage({ type: "started" });
65
60
  }
66
- /** `url` as an absolute URL, which a worker resolves the same as the page. */
61
+ /** `url` as an absolute URL, which a worker resolves like the page. */
67
62
  function absolute(url) {
68
63
  if (url === undefined)
69
64
  return undefined;
@@ -71,13 +66,11 @@ function absolute(url) {
71
66
  return base ? new URL(String(url), base).href : String(url);
72
67
  }
73
68
  /**
74
- * Runs an extensions call: `request` names the call (`read`, `svg-diagnostics` or
75
- * `exr-first-frame`), the `file` it reads and what else the call takes. With `factory`, the call
76
- * runs in a worker it makes (one running {@link installExtensionsWorker}), which is terminated once
77
- * it answers, when `signal` aborts (rejecting with its reason), or after `timeoutMs` (rejecting as
78
- * `provider_stalled`). A worker that cannot start (its script does not load) leaves the call to
79
- * this thread, as it does without a factory; here the module call cannot be stopped, and `signal`
80
- * is checked between steps.
69
+ * Runs an extensions call (`read`, `svg-diagnostics` or `exr-first-frame`) on `request.file`.
70
+ * With `factory`, the call runs in a new worker running {@link installExtensionsWorker}, terminated
71
+ * once it answers, when `signal` aborts (rejecting with its reason), or after `timeoutMs`
72
+ * (rejecting as `provider_stalled`). Without a factory, or if the worker script fails to load, the
73
+ * call runs on this thread, where it cannot be stopped and `signal` is checked between steps.
81
74
  *
82
75
  * @param {{ factory?: () => Worker, wasmUrl?: string | URL }} module
83
76
  * @param {{ call: string, file: Blob & { name?: string }, [key: string]: unknown }} request
@@ -136,7 +129,7 @@ export function callExtensions({ factory, wasmUrl }, request, { signal, timeoutM
136
129
  end();
137
130
  if (started)
138
131
  reject(new Error(`the extensions worker stopped: ${event?.message ?? "an uncaught error"}`));
139
- // The worker never ran: the call is made here instead.
132
+ // The worker never ran, so make the call here.
140
133
  else
141
134
  here().then(resolve, reject);
142
135
  };
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Answers hash requests posted to `scope` (a dedicated worker's global): `{ type: "hash", file }`
3
- * is answered with `{ type: "progress", bytes }` as it is read and `{ type: "hashed", sha256 }`, or
3
+ * gets `{ type: "progress", bytes }` while reading, then `{ type: "hashed", sha256 }`, or
4
4
  * `{ type: "failed", message, name }` when the file cannot be read.
5
5
  * @param {{ onmessage: ((event: MessageEvent) => void) | null, postMessage(message: unknown): void }} scope
6
6
  */
@@ -9,10 +9,9 @@ export function installHashWorker(scope: {
9
9
  postMessage(message: unknown): void;
10
10
  }): void;
11
11
  /**
12
- * The SHA-256 of `file`, as `sha256Blob` gives it, hashed in a worker `factory` makes (one running
13
- * {@link installHashWorker}), which is terminated once it answers or `signal` aborts. A worker that
14
- * cannot start (its script does not load) leaves the hash to this thread's `sha256Blob`, which lets
15
- * other tasks run every few milliseconds.
12
+ * The SHA-256 of `file` hashed in a worker from `factory` (running {@link installHashWorker}),
13
+ * terminated once it answers or `signal` aborts. If the worker script fails to load, hashes on
14
+ * this thread with `sha256Blob` instead.
16
15
  * @param {() => Worker} factory
17
16
  * @param {Blob} file
18
17
  * @param {{ signal?: AbortSignal, onProgress?: (bytes: number) => void }} [options]
@@ -1,10 +1,10 @@
1
1
  // Hashes an imported file off the page's thread (about 5 ms of CPU per megabyte in JavaScript).
2
- // A host bundling the `@alpha2-sh/editor/hash-worker` entry passes
3
- // `runtimeAssets.hashWorkerFactory`; each file gets its own worker, terminated on cancel.
2
+ // Hosts bundling the `@alpha2-sh/editor/hash-worker` entry pass `runtimeAssets.hashWorkerFactory`;
3
+ // each file gets its own worker, terminated on cancel.
4
4
  import { sha256Blob } from "alpha2/assets";
5
5
  /**
6
6
  * Answers hash requests posted to `scope` (a dedicated worker's global): `{ type: "hash", file }`
7
- * is answered with `{ type: "progress", bytes }` as it is read and `{ type: "hashed", sha256 }`, or
7
+ * gets `{ type: "progress", bytes }` while reading, then `{ type: "hashed", sha256 }`, or
8
8
  * `{ type: "failed", message, name }` when the file cannot be read.
9
9
  * @param {{ onmessage: ((event: MessageEvent) => void) | null, postMessage(message: unknown): void }} scope
10
10
  */
@@ -28,10 +28,9 @@ export function installHashWorker(scope) {
28
28
  };
29
29
  }
30
30
  /**
31
- * The SHA-256 of `file`, as `sha256Blob` gives it, hashed in a worker `factory` makes (one running
32
- * {@link installHashWorker}), which is terminated once it answers or `signal` aborts. A worker that
33
- * cannot start (its script does not load) leaves the hash to this thread's `sha256Blob`, which lets
34
- * other tasks run every few milliseconds.
31
+ * The SHA-256 of `file` hashed in a worker from `factory` (running {@link installHashWorker}),
32
+ * terminated once it answers or `signal` aborts. If the worker script fails to load, hashes on
33
+ * this thread with `sha256Blob` instead.
35
34
  * @param {() => Worker} factory
36
35
  * @param {Blob} file
37
36
  * @param {{ signal?: AbortSignal, onProgress?: (bytes: number) => void }} [options]
@@ -79,7 +78,7 @@ export function hashInWorker(factory, file, { signal, onProgress } = {}) {
79
78
  return;
80
79
  event?.preventDefault?.();
81
80
  end();
82
- // The worker never ran: the hash is made here instead.
81
+ // The worker never ran, so hash here.
83
82
  sha256Blob(file, { signal, onProgress }).then(resolve, reject);
84
83
  };
85
84
  worker.postMessage({ type: "hash", file });
@@ -1,17 +1,16 @@
1
1
  import { type ExportFailure } from "alpha2/export-failure";
2
- import { type RetryPolicyOptions, type RetryProfile } from "alpha2/export-retry";
2
+ import { type AttemptRenderer, type AttemptVideoEncoder, type RetryPolicyOptions, type RetryProfile } from "alpha2/export-retry";
3
3
  import type { EditorDocument } from "../core/document.js";
4
4
  import type { ExportBackend, ExportProgress, ExportArtifact } from "../ports/index.js";
5
- /**
6
- * Refuses an export `frameRate` the engine cannot read, before an export starts, for every
7
- * backend: one rate, the same everywhere.
8
- */
5
+ /** Refuses an export `frameRate` the engine cannot read, before any backend starts. */
9
6
  export declare function requireExportFrameRate(frameRate: unknown): void;
10
7
  /** What happened to one attempt of a job. */
11
8
  export interface ExportAttemptRecord {
12
9
  readonly id: string;
13
10
  readonly number: number;
14
11
  readonly profile: RetryProfile;
12
+ readonly renderer: AttemptRenderer;
13
+ readonly videoEncoder: AttemptVideoEncoder;
15
14
  readonly startedAt: number;
16
15
  readonly endedAt?: number;
17
16
  readonly outcome: "running" | "succeeded" | "failed" | "cancelled";
@@ -28,7 +27,8 @@ export interface ExportJobOptions {
28
27
  readonly id?: string;
29
28
  /**
30
29
  * The retry policy (see `alpha2/export-retry`), or `false` for one attempt. Default: three
31
- * attempts, the reference backoff.
30
+ * attempts with the reference backoff. Without `pageGpuLosses`, each decision reads the page's
31
+ * current count (`pageGpuLosses` of `alpha2/gpu-recovery`).
32
32
  */
33
33
  readonly retry?: RetryPolicyOptions | false;
34
34
  /** Waits `ms` or rejects when `signal` aborts; replaceable for tests. */
@@ -37,11 +37,10 @@ export interface ExportJobOptions {
37
37
  /**
38
38
  * One export of a pinned document, run in bounded attempts.
39
39
  *
40
- * Every attempt renders the same document from the start, on a fresh device or host, with the same
41
- * output settings; a failed one is retried only as the retry policy allows, with less in flight
42
- * when the failure is evidence of memory exhaustion. Progress from an attempt that has ended is
43
- * ignored, so a late answer cannot move a newer attempt's progress or change its result. Cancelling
44
- * stops the running attempt and any pending retry; no new attempt starts after it.
40
+ * Every attempt renders the same document from the start on a fresh device or host with the same
41
+ * output settings. A failed attempt is retried only as the retry policy allows, with less in flight
42
+ * after memory exhaustion. Progress from ended attempts is ignored, so a late answer cannot change
43
+ * a newer attempt's progress or result. Cancelling stops the running attempt and any pending retry.
45
44
  */
46
45
  export declare class ExportJob {
47
46
  #private;
@@ -54,29 +53,28 @@ export declare class ExportJob {
54
53
  */
55
54
  readonly result: Promise<ExportArtifact>;
56
55
  /**
57
- * Starts the job at once, on the next microtask.
56
+ * Starts the job on the next microtask.
58
57
  *
59
58
  * @param revision The editor revision `document` was pinned at.
60
59
  * @param backend Runs each attempt; its `dispose` runs once when the job settles.
61
60
  * @param release Called once when the job settles, whatever the outcome.
62
61
  */
63
62
  constructor(revision: number, document: EditorDocument, backend: ExportBackend, release?: () => void, options?: ExportJobOptions);
64
- /** The latest progress, frozen. Reports from an attempt that has ended are not reflected. */
63
+ /** The latest progress, frozen. Reports from ended attempts are not reflected. */
65
64
  get progress(): ExportProgress;
66
65
  /** Why the job failed, classified; undefined unless it failed. */
67
66
  get failure(): ExportFailure | undefined;
68
67
  /** Every attempt so far, oldest first. */
69
68
  get attempts(): readonly ExportAttemptRecord[];
70
69
  /**
71
- * Calls `listener` with the current progress at once and with every later change. Returns
72
- * the unsubscribe function.
70
+ * Calls `listener` with the current progress now and on every change. Returns the unsubscribe
71
+ * function.
73
72
  */
74
73
  subscribe(listener: (value: ExportProgress) => void): () => void;
75
74
  /**
76
- * Stops the running attempt and any pending retry, and resolves once the job has settled. An
77
- * attempt that finished before the job settled has what it made released (a temporary file
78
- * deleted). Resolves without effect on a settled job. Rejects only when cleanup failed
79
- * (`EditorError` `cancel_failed`).
75
+ * Stops the running attempt and any pending retry, and resolves once the job has settled. Output
76
+ * of an attempt that finished before settling is released (a temporary file deleted). No effect
77
+ * on a settled job. Rejects only when cleanup failed (`EditorError` `cancel_failed`).
80
78
  */
81
79
  cancel(): Promise<void>;
82
80
  }
@@ -1,11 +1,9 @@
1
1
  import { classifyBrowserFailure, failureOf, withSecondaryFailure, } from "alpha2/export-failure";
2
2
  import { decideRetry, RETRY_PROFILES, } from "alpha2/export-retry";
3
+ import { pageGpuLosses } from "alpha2/gpu-recovery";
3
4
  import { validFrameRate } from "alpha2/project";
4
5
  import { EditorError } from "../core/document.js";
5
- /**
6
- * Refuses an export `frameRate` the engine cannot read, before an export starts, for every
7
- * backend: one rate, the same everywhere.
8
- */
6
+ /** Refuses an export `frameRate` the engine cannot read, before any backend starts. */
9
7
  export function requireExportFrameRate(frameRate) {
10
8
  if (frameRate !== undefined && !validFrameRate(frameRate))
11
9
  throw new EditorError("invalid_frame_rate", `Export frameRate must be { num, den }, positive 32-bit integers with a frame at least one tick long; received ${JSON.stringify(frameRate)}`, "export");
@@ -13,11 +11,10 @@ export function requireExportFrameRate(frameRate) {
13
11
  /**
14
12
  * One export of a pinned document, run in bounded attempts.
15
13
  *
16
- * Every attempt renders the same document from the start, on a fresh device or host, with the same
17
- * output settings; a failed one is retried only as the retry policy allows, with less in flight
18
- * when the failure is evidence of memory exhaustion. Progress from an attempt that has ended is
19
- * ignored, so a late answer cannot move a newer attempt's progress or change its result. Cancelling
20
- * stops the running attempt and any pending retry; no new attempt starts after it.
14
+ * Every attempt renders the same document from the start on a fresh device or host with the same
15
+ * output settings. A failed attempt is retried only as the retry policy allows, with less in flight
16
+ * after memory exhaustion. Progress from ended attempts is ignored, so a late answer cannot change
17
+ * a newer attempt's progress or result. Cancelling stops the running attempt and any pending retry.
21
18
  */
22
19
  export class ExportJob {
23
20
  revision;
@@ -36,7 +33,7 @@ export class ExportJob {
36
33
  #attempts = [];
37
34
  #current = 0;
38
35
  /**
39
- * Starts the job at once, on the next microtask.
36
+ * Starts the job on the next microtask.
40
37
  *
41
38
  * @param revision The editor revision `document` was pinned at.
42
39
  * @param backend Runs each attempt; its `dispose` runs once when the job settles.
@@ -55,8 +52,8 @@ export class ExportJob {
55
52
  !(cause instanceof EditorError && cause.code === "cancel_failed");
56
53
  this.result = this.#attempt(document, backend, progress, options)
57
54
  .then(async (value) => {
58
- // A cancel wins until the job settles, even over an attempt that already finished: what it
59
- // made is released and the job settles as cancelled.
55
+ // A cancel wins until the job settles, even over a finished attempt, whose output is
56
+ // released.
60
57
  if (signal.aborted) {
61
58
  this.#record(this.#current, {
62
59
  outcome: "cancelled",
@@ -90,7 +87,7 @@ export class ExportJob {
90
87
  await backend.dispose?.({ ok: false, cancelled });
91
88
  }
92
89
  catch (cleanup) {
93
- // A failed job keeps its failure and notes the cleanup beside it.
90
+ // A failed job keeps its failure and notes the cleanup failure beside it.
94
91
  if (cancelled)
95
92
  throw cleanup;
96
93
  if (failure)
@@ -128,6 +125,8 @@ export class ExportJob {
128
125
  const sleep = options.sleep ?? abortableSleep;
129
126
  const history = [];
130
127
  let profile = policy?.profiles?.[0] ?? RETRY_PROFILES[0];
128
+ let renderer = "gpu";
129
+ let videoEncoder = "default";
131
130
  await Promise.resolve();
132
131
  for (let number = 1;; number += 1) {
133
132
  signal.throwIfAborted();
@@ -135,6 +134,8 @@ export class ExportJob {
135
134
  id: `${this.id}/${number}`,
136
135
  number,
137
136
  profile,
137
+ renderer,
138
+ videoEncoder,
138
139
  ...(history.length > 0 ? { previous: history.at(-1).failure } : {}),
139
140
  });
140
141
  this.#current = number;
@@ -142,10 +143,12 @@ export class ExportJob {
142
143
  id: attempt.id,
143
144
  number,
144
145
  profile,
146
+ renderer,
147
+ videoEncoder,
145
148
  startedAt: Date.now(),
146
149
  outcome: "running",
147
150
  });
148
- // Progress from an attempt that has ended belongs to nothing.
151
+ // Progress from an ended attempt is dropped.
149
152
  const report = (value) => {
150
153
  if (this.#current === number && !this.#settled)
151
154
  progress({ ...value, attempt: number });
@@ -160,7 +163,7 @@ export class ExportJob {
160
163
  this.#record(number, { outcome: "cancelled", endedAt: Date.now() });
161
164
  throw cause;
162
165
  }
163
- // The next attempt owns progress from here: a late report from this one is dropped.
166
+ // The next attempt owns progress from here.
164
167
  this.#current = number + 1;
165
168
  const failure = {
166
169
  ...(failureOf(cause) ??
@@ -170,21 +173,31 @@ export class ExportJob {
170
173
  jobId: this.id,
171
174
  attemptId: attempt.id,
172
175
  };
173
- history.push({ failure, profile });
176
+ history.push({ failure, profile, renderer, videoEncoder });
177
+ // Losses the page saw so far, this attempt's included, unless the caller counts them.
174
178
  const decision = policy
175
- ? decideRetry(history, policy)
176
- : { retry: false, reason: "retries are off for this job" };
179
+ ? decideRetry(history, {
180
+ ...policy,
181
+ pageGpuLosses: policy.pageGpuLosses ?? pageGpuLosses.count(),
182
+ })
183
+ : {
184
+ retry: false,
185
+ reason: "retries are off for this job",
186
+ failure,
187
+ };
188
+ // A loss recurring at the same frame is reported as the policy read it, as hung.
189
+ const final = decision.retry ? failure : (decision.failure ?? failure);
177
190
  this.#record(number, {
178
191
  outcome: "failed",
179
192
  endedAt: Date.now(),
180
- failure,
193
+ failure: final,
181
194
  decision: { retry: decision.retry, reason: decision.reason },
182
195
  });
183
196
  if (!decision.retry) {
184
197
  this.#current = number;
185
198
  if (cause instanceof Error)
186
199
  Object.defineProperty(cause, "failure", {
187
- value: failure,
200
+ value: final,
188
201
  configurable: true,
189
202
  });
190
203
  throw cause;
@@ -197,6 +210,8 @@ export class ExportJob {
197
210
  });
198
211
  await sleep(decision.delayMs, signal);
199
212
  profile = decision.profile;
213
+ renderer = decision.renderer;
214
+ videoEncoder = decision.videoEncoder;
200
215
  }
201
216
  }
202
217
  }
@@ -207,7 +222,7 @@ export class ExportJob {
207
222
  ...patch,
208
223
  });
209
224
  }
210
- /** The latest progress, frozen. Reports from an attempt that has ended are not reflected. */
225
+ /** The latest progress, frozen. Reports from ended attempts are not reflected. */
211
226
  get progress() {
212
227
  return this.#progress;
213
228
  }
@@ -220,8 +235,8 @@ export class ExportJob {
220
235
  return [...this.#attempts];
221
236
  }
222
237
  /**
223
- * Calls `listener` with the current progress at once and with every later change. Returns
224
- * the unsubscribe function.
238
+ * Calls `listener` with the current progress now and on every change. Returns the unsubscribe
239
+ * function.
225
240
  */
226
241
  subscribe(listener) {
227
242
  this.#listeners.add(listener);
@@ -229,10 +244,9 @@ export class ExportJob {
229
244
  return () => this.#listeners.delete(listener);
230
245
  }
231
246
  /**
232
- * Stops the running attempt and any pending retry, and resolves once the job has settled. An
233
- * attempt that finished before the job settled has what it made released (a temporary file
234
- * deleted). Resolves without effect on a settled job. Rejects only when cleanup failed
235
- * (`EditorError` `cancel_failed`).
247
+ * Stops the running attempt and any pending retry, and resolves once the job has settled. Output
248
+ * of an attempt that finished before settling is released (a temporary file deleted). No effect
249
+ * on a settled job. Rejects only when cleanup failed (`EditorError` `cancel_failed`).
236
250
  */
237
251
  async cancel() {
238
252
  if (!this.#settled) {
@@ -256,7 +270,6 @@ async function releaseArtifact(artifact) {
256
270
  if ("release" in artifact && typeof artifact.release === "function")
257
271
  await artifact.release();
258
272
  }
259
- /** Waits `ms`, or rejects as soon as `signal` aborts. */
260
273
  function abortableSleep(ms, signal) {
261
274
  return new Promise((resolve, reject) => {
262
275
  const stop = () => {
@@ -2,15 +2,13 @@
2
2
  export interface Lease<T> {
3
3
  readonly value: T;
4
4
  /**
5
- * Drops this reference. Idempotent. The entry's `dispose` runs when its last lease is
6
- * released.
5
+ * Drops this reference. Idempotent. The entry's `dispose` runs when its last lease is released.
7
6
  */
8
7
  release(): void;
9
8
  }
10
9
  /**
11
- * Holds runtime values (decoders, bitmaps, files) that must never enter the project JSON or
12
- * its history, keyed by id and revision and kept alive by explicit leases from owners and
13
- * export jobs.
10
+ * Holds runtime values (decoders, bitmaps, files) that must never enter the project JSON or its
11
+ * history, keyed by id and revision and kept alive by leases from owners and export jobs.
14
12
  */
15
13
  export declare class ResourceRegistry<T> {
16
14
  #private;
@@ -21,14 +19,11 @@ export declare class ResourceRegistry<T> {
21
19
  * Stores `value` under `id` at `revision` and returns its first lease. `dispose` runs once,
22
20
  * when the last lease is released or the registry is disposed.
23
21
  *
24
- * Throws `EditorError` `disposed` after {@link dispose}, `duplicate_resource` if that
25
- * revision is already stored, and `resource_budget` when `limit` entries are held.
22
+ * Throws `EditorError` `disposed` after {@link dispose}, `duplicate_resource` if that revision
23
+ * is already stored, and `resource_budget` when `limit` entries are held.
26
24
  */
27
25
  put(id: string, revision: number, value: T, dispose: () => void): Lease<T>;
28
- /**
29
- * Takes another lease on a stored entry. Throws `EditorError` `missing_asset` if no entry
30
- * exists for `id` at `revision`.
31
- */
26
+ /** Takes another lease on a stored entry. Throws `EditorError` `missing_asset` if absent. */
32
27
  acquire(id: string, revision: number): Lease<T>;
33
28
  /** Entries currently held. */
34
29
  get size(): number;
@@ -1,8 +1,7 @@
1
1
  import { EditorError } from "../core/document.js";
2
2
  /**
3
- * Holds runtime values (decoders, bitmaps, files) that must never enter the project JSON or
4
- * its history, keyed by id and revision and kept alive by explicit leases from owners and
5
- * export jobs.
3
+ * Holds runtime values (decoders, bitmaps, files) that must never enter the project JSON or its
4
+ * history, keyed by id and revision and kept alive by leases from owners and export jobs.
6
5
  */
7
6
  export class ResourceRegistry {
8
7
  limit;
@@ -16,8 +15,8 @@ export class ResourceRegistry {
16
15
  * Stores `value` under `id` at `revision` and returns its first lease. `dispose` runs once,
17
16
  * when the last lease is released or the registry is disposed.
18
17
  *
19
- * Throws `EditorError` `disposed` after {@link dispose}, `duplicate_resource` if that
20
- * revision is already stored, and `resource_budget` when `limit` entries are held.
18
+ * Throws `EditorError` `disposed` after {@link dispose}, `duplicate_resource` if that revision
19
+ * is already stored, and `resource_budget` when `limit` entries are held.
21
20
  */
22
21
  put(id, revision, value, dispose) {
23
22
  if (this.#closed)
@@ -40,10 +39,7 @@ export class ResourceRegistry {
40
39
  });
41
40
  return this.acquire(id, revision);
42
41
  }
43
- /**
44
- * Takes another lease on a stored entry. Throws `EditorError` `missing_asset` if no entry
45
- * exists for `id` at `revision`.
46
- */
42
+ /** Takes another lease on a stored entry. Throws `EditorError` `missing_asset` if absent. */
47
43
  acquire(id, revision) {
48
44
  const key = JSON.stringify([id, revision]);
49
45
  const entry = this.#entries.get(key);