import type { FrameQuality } from "./scrub-cursor"; import { type DiagnosticsSnapshot, type Warning } from "./diagnostics"; /** * Worker-realm trace recorder. Two fixed-capacity ring buffers capture a rolling * window of what the runtime did: an EVENT ring (paints, scrubs, seeks, and * status transitions, the moments EngineCore drives directly) and a SNAPSHOT * ring (the same DiagnosticsSnapshot references already assembled for the * broadcast). Both overwrite oldest in place, so a long session has a constant, * predictable footprint and the appends are O(1) with no growth or GC churn. * * Stall and decode-spike depth are not separate events: stalls ride the snapshot * ring (realtime.stalls), and per-decode latency rides the scrub stats, so the * event ring stays to the moments the engine itself initiates. * * Allocated on arm, freed on disarm or export, so a disarmed recorder costs * zero memory. assemble() walks both rings ascending into one self-describing * versioned JSON document an agent or a human can read cold: the summary carries * the exact Warning diagnoses the UI shows, so the trace explains itself. * * Pure data. It never touches decode, the sink, or the cursor. */ export declare const TRACE_SCHEMA = "roboflow.videoEngine.trace/2"; /** One captured runtime event. type is the moment ("paint", "scrub", "seek", * "status"); tMs is worker-clock relative to arm; the rest are optional payload * fields the relevant moment fills. All plain data. */ export interface TraceEvent { readonly type: "paint" | "scrub" | "seek" | "status" | "rate"; readonly tMs: number; readonly mediaTimeMs?: number; readonly paintSeq?: number; /** paint: which frame of the source these pixels are, by the engine's frame * table. The snapshot ring samples at 10Hz and paints outrun it, so without * this the frame most paints put up is named nowhere in the trace. */ readonly frameIndex?: number; readonly catchUpMs?: number; /** paint: whether a full decode or the coarse stand-in reached the canvas. * Paints come from sources with materially different pixel quality, so a * trace without this cannot tell a crisp frame from a blurry one. */ readonly quality?: FrameQuality; /** seek/scrub: the target the gesture aimed at, and where it landed. * paint: the position the paint was serving (the engine clock at paint). */ readonly targetMs?: number; readonly landedMs?: number; /** seek: whether it was a key-only navigation seek. */ readonly keyOnly?: boolean; /** status: the new playback status string. */ readonly status?: string; /** rate: the new playback rate. Every paint after it is served by a clock * running at a different slope, which the timestamps alone do not show. */ readonly rate?: number; } /** A broadcast snapshot with the moment it was taken, in the same worker-clock * milliseconds since arm that every event carries, so the two series line up. */ export type TracedSnapshot = DiagnosticsSnapshot & { readonly tMs: number; }; /** Environment facts captured once at export, so a trace read elsewhere carries * the device context that shaped the numbers. */ export interface TraceEnvironment { readonly userAgent: string; readonly webgpuAvailable: boolean; /** Null when nobody measured one: the engine has no window to read it off, so * it knows the ratio only when a host hands it a display box or a canvas. */ readonly devicePixelRatio: number | null; readonly hardwareConcurrency: number; } /** Rolled-up totals for a glanceable read, with the same Warning diagnoses the * UI shows so the trace is self-explaining. effectivePaintFps is measured across * every snapshot pushed since arm, so it covers more than the snapshots the ring * still holds; no snapshot carries such a reading. cacheHitRatePct is null until * something looks the cache up, and cacheLookups is its denominator, so a reader * can tell a cold cache from a failing one. */ export interface TraceSummary { readonly scrubP50Ms: number; readonly scrubP95Ms: number; readonly scrubMaxMs: number; readonly effectivePaintFps: number | null; readonly lateFrames: number; readonly stalls: number; readonly maxCatchUpMs: number; readonly cacheHitRatePct: number | null; readonly cacheLookups: number; readonly exactEvictions: number; readonly warnings: Warning[]; } /** * How much of the capture one ring can still answer for. A capture older than a * ring's capacity keeps only its tail, so durationMs describes the session and * this describes the evidence: coveredMs runs from the oldest entry still held, * and dropped counts what was overwritten to make room. */ export interface TraceRingCoverage { readonly capacity: number; readonly retained: number; readonly dropped: number; readonly oldestTMs: number | null; readonly newestTMs: number | null; readonly coveredMs: number; } /** Per-ring coverage. The two rings fill at different rates, so they reach back * different distances and neither one is "the captured window". */ export interface TraceCoverage { readonly events: TraceRingCoverage; readonly snapshots: TraceRingCoverage; } /** The assembled trace document. Versioned by schema so a reader knows the shape. */ export interface EngineTrace { readonly schema: typeof TRACE_SCHEMA; readonly capturedAt: number; readonly armOriginMs: number; readonly durationMs: number; readonly coverage: TraceCoverage; /** Why the capture stopped early, or null when a reader closed it. */ readonly truncatedReason: string | null; readonly environment: TraceEnvironment; readonly events: TraceEvent[]; readonly snapshots: TracedSnapshot[]; readonly summary: TraceSummary; } export interface TraceRecorderEnvironment { readonly userAgent: string; readonly webgpuAvailable: boolean; readonly devicePixelRatio: number | null; readonly hardwareConcurrency: number; } export declare class TraceRecorder { private readonly env; private readonly now; private readonly events; private readonly snapshots; private readonly armOriginMs; private paintedFrames; private playingMs; private lastSnapshotAtMs; private lastPaints; private lastSnapshotPlaying; private truncatedReason; constructor(env: TraceRecorderEnvironment, now?: () => number, eventCap?: number, snapshotCap?: number); pushEvent(event: TraceEvent): void; /** * Stamps the broadcast snapshot with when it was taken and rings it. The * stamp is the whole point of keeping a series: without it the snapshots are * an unordered pile with no way to line them up against the events, which * carry one, or to tell a freeze from a slow stretch. */ pushSnapshot(snapshot: DiagnosticsSnapshot): void; /** Records that the capture was cut short by something other than a reader * closing it, so an assembled trace says why it ends where it does. The * first reason sticks: it is the one that ended the capture. */ truncate(reason: string): void; /** * Paints between two consecutive snapshots over the wall time between them. * An interval counts only when playback was up at both ends, since one * folded-in pause paints nothing and sinks the rate far below what the user * watched. The paint counter restarts with each play session, so a negative * delta means a new session began and that interval is dropped. */ private accumulatePaintRate; /** Worker-clock milliseconds since arm; the stamp every pushed event uses. */ elapsedMs(): number; assemble(): EngineTrace; private summarize; }