Skip to content

feat(runtime): expose frame-presented request and preview health metrics - #8933

Merged
miguel-heygen merged 7 commits into
mainfrom
feat/sealed-preview-health-metrics
Nov 27, 2026
Merged

miguel-heygen merged 7 commits into
mainfrom
feat/sealed-preview-health-metrics

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

Summary

Expose frame-presentation timing and playback-health metrics from inside the preview runtime so Studio and host applications receive identical readings regardless of frame origin:

  1. frame-presented editing request (packages/core/src/runtime/editingPort.ts, editingAgent.ts, packages/studio/src/player/lib/frameFacts.ts):
    • Adds a read-only frame-presented request that waits for any seeking <video> elements inside the preview document to settle and then awaits two requestAnimationFrame ticks before returning { presented: true }.
    • Video waits settle on seeked, error, abort, emptied, DOM disconnection (via MutationObserver), and agent dispose(), removing all listeners and observers on settlement so no listeners leak.
    • Leaves wait-for-seek unchanged for existing callers.
  2. Audio step p90, missed-frame counts, and motion blur summary in preview-health (packages/core/src/runtime/playbackHealth.ts, init.ts, types.ts):
    • Accepts the active playing, unmuted <audio> element from mediaClipsInWindow (zero per-frame DOM queries) and computes mediaStepP90Ms (positive steps under 500 ms over the 3 s window) alongside over16 (gaps > 25 ms) and over33 (gaps > 41.7 ms).
    • Reads motionBlur (attr, shutter, state, tier, movers) via readMotionBlurSummary only when emitting the 1 s health report (zero per-frame work).
  3. Frame shape counts in preview-facts (packages/core/src/runtime/playbackHealth.ts, init.ts, types.ts):
    • Adds optional frameShape (canvas, svg, filter, tweens) to preview-facts, read only on demand when request-preview-facts is handled (zero per-frame work).
  4. Validated Studio parsers (packages/studio/src/player/store/previewQualitySlice.ts):
    • Validates, bounds, and allowlists every new optional field in parsePlaybackHealth and parsePreviewFacts, dropping out-of-range numbers, non-finite values, oversized strings, and unknown enum values.

Testing

  • fails without the fix: packages/core/src/runtime/editingAgent.test.ts (frame-presented suite failed with unknown-kind), packages/core/src/runtime/playbackHealth.test.ts (emits audio step p90, missed-frame counts, and reads motionBlur only on the 1 s emit failed with expected undefined to be 25), and packages/studio/src/player/store/previewQualitySlice.test.ts (validates, bounds, and allowlists ... and validates and bounds frameShape ... failed with undefined).
  • Red-first unit tests verified failing before the implementation, then passing 3 consecutive runs after:
    • packages/core/src/runtime/editingAgent.test.ts (195 tests) and editingPort.test.ts (13 tests): verifies frame-presented waits for seeked + two animation frames, settles on error/abort/emptied/DOM disconnect, and removes listeners cleanly.
    • packages/core/src/runtime/playbackHealth.test.ts (16 tests): verifies mediaStepP90Ms, over16, over33, readMotionBlurSummary (invoked only on health emits, never per frame), and readPreviewFrameShape.
    • packages/studio/src/player/store/previewQualitySlice.test.ts (8 tests): verifies valid, nullable, and rejected/out-of-bounds payloads for mediaStepP90Ms, over16, over33, motionBlur, and frameShape.
  • Package verification:
    • packages/core: build, typecheck (including typecheck:runtime), lint:runtime-preview-guards, runtime contract/behavior/seek checks, and full vitest suite (354/354 files passed).
    • packages/studio: typecheck, oxlint, oxfmt, and full unit vitest suite (851/851 passed, 1 skipped).
    • packages/engine: typecheck passed; vitest 217/222 passed (5 local MPEG-TS tests failed with SIGSEGV in the local static FFmpeg binary; CI verifies full engine suite).
    • packages/producer: typecheck passed; unit vitest 123/124 passed (same local MPEG-TS static FFmpeg SIGSEGV) and bun unit lane 54/55 passed (videoAudioProbe.test.ts remote-probe download path deferred to CI).

No visible change

Pure runtime editing-port, playback-health reporting, and Studio store parser changes (.ts only, no UI, CSS, or DOM rendering changes).

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 273 (base branch 271), smooth 67 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Quarantined, measured but not gated (0)

…ealth-metrics

# Conflicts:
#	packages/core/src/runtime/editingAgent.test.ts
#	packages/core/src/runtime/editingAgent.ts
#	packages/core/src/runtime/editingPort.test.ts
#	packages/core/src/runtime/editingPort.ts
…ealth-metrics

# Conflicts:
#	packages/core/src/runtime/editingAgent.ts

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Bound the frame-side lifetime of frame-presented. packages/core/src/runtime/editingAgent.ts:184–213 installs a fresh set of seek listeners and a document-wide observer per seeking video per request. The caller timeout in editingPort.ts:977–981 settles only the host promise. A seek that stays pending leaves the frame-side listeners and observers alive, and subsequent requests add more. Disposal also settles the seek waits into the two-RAF stage, whose callbacks are outside cleanup. Use a bounded, cancellable wait across both stages, release listeners and observers and cancel RAF callbacks on teardown, and report presented: false if the wait expires before presentation. Please cover cleanup for a seek that never settles and disposal with an animation frame pending.

The required checks are green or skipped as expected. This finding is from source review.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Fixed in b7489b7. frame-presented is now one bounded, cancellable wait per request across both stages (packages/core/src/runtime/editingAgent.ts, waitForFramePresented):

  • One deadline per request, 1 s under the caller's default timeout (EDITING_REQUEST_TIMEOUT_MS - 1_000, on the wall clock). On expiry it answers presented: false.
  • Every exit (settled and painted, expired, or agent disposed) runs the same cleanup: seek listeners removed, the observer disconnected, the timer cleared, and the pending animation frame cancelled.
  • One observer per request for all its seeking videos, not one per video, so a request that never settles can't leave anything behind and later requests can't pile up.
  • Dispose expires every pending wait, so the two-frame stage no longer runs past teardown.

Tests (both fail on 218cce9): "answers presented false and lets go of the video when its seek never settles" (all four listeners removed, observer disconnected) and "cancels a pending animation frame when the agent is disposed". The existing frame-presented tests still pass; editingAgent, editingPort and playbackHealth tests are 226/226, 3 runs; core tsc --noEmit clean.

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new request cleans up its timer, video listeners, observer, and pending animation frame on completion, deadline, or agent disposal. The deadline returns presented: false. All 197 targeted editing-agent tests passed locally, and the new cancellation test failed with the previous source. All applicable required checks passed.

— tai (automated review)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 27, 2026
…ealth-metrics

# Conflicts:
#	packages/core/src/runtime/editingAgent.test.ts
#	packages/core/src/runtime/editingAgent.ts
#	packages/core/src/runtime/editingPort.test.ts
#	packages/core/src/runtime/editingPort.ts

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge preserves both parents’ request kinds, parameter parsers, handlers, and teardown calls. The bounded frame wait and its tests are intact. All applicable required checks passed at this head.

— tai (automated review)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 27, 2026
…ealth-metrics

# Conflicts:
#	packages/core/src/runtime/editingAgent.ts

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge retains main’s canvas-key handler verbatim and preserves the frame-presented registration, bounded wait, and teardown. All applicable required checks passed on this head.

— tai (automated review)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 27, 2026
Merged via the queue into main with commit 2192cf6 Nov 27, 2026
103 checks passed
@miguel-heygen
miguel-heygen deleted the feat/sealed-preview-health-metrics branch November 27, 2026 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants