|
| 1 | +/** |
| 2 | + * Immutable routing decision for the capture phase. |
| 3 | + * |
| 4 | + * The orchestrator used to carry the same decision in several mutable booleans |
| 5 | + * (`useStreamingEncode`, `useLayeredComposite`, `forceScreenshot`) plus worker |
| 6 | + * routing state. Keeping those values independently mutable made invalid |
| 7 | + * combinations representable during fallback. A CapturePlan is the single |
| 8 | + * value consumed by capture stages, and `replanAfterFailure` is the only |
| 9 | + * transition between variants. |
| 10 | + */ |
| 11 | + |
| 12 | +export type CapturePlanTarget = Readonly<{ |
| 13 | + kind: "sdr_streaming" | "sdr_disk"; |
| 14 | + workerCount: number; |
| 15 | + forceParallelStream: boolean; |
| 16 | +}>; |
| 17 | + |
| 18 | +export type CaptureRouting = |
| 19 | + | Readonly<{ kind: "default" }> |
| 20 | + | Readonly<{ |
| 21 | + kind: "worker_inversion" | "parallel_router"; |
| 22 | + state: "active" | "reverted"; |
| 23 | + fallback: CapturePlanTarget; |
| 24 | + }>; |
| 25 | + |
| 26 | +interface CapturePlanBase { |
| 27 | + readonly workerCount: number; |
| 28 | + readonly forceScreenshot: boolean; |
| 29 | + readonly forceParallelStream: boolean; |
| 30 | + readonly usePageSideCompositing: boolean; |
| 31 | + readonly hasHdrContent: boolean; |
| 32 | + readonly needsAlpha: boolean; |
| 33 | + readonly routing: CaptureRouting; |
| 34 | +} |
| 35 | + |
| 36 | +export interface SdrStreamingCapturePlan extends CapturePlanBase { |
| 37 | + readonly kind: "sdr_streaming"; |
| 38 | +} |
| 39 | + |
| 40 | +export interface SdrDiskCapturePlan extends CapturePlanBase { |
| 41 | + readonly kind: "sdr_disk"; |
| 42 | + readonly forceParallelStream: false; |
| 43 | +} |
| 44 | + |
| 45 | +export interface HdrLayeredCapturePlan extends CapturePlanBase { |
| 46 | + readonly kind: "hdr_layered"; |
| 47 | + readonly forceScreenshot: true; |
| 48 | + readonly forceParallelStream: false; |
| 49 | +} |
| 50 | + |
| 51 | +export type CapturePlan = SdrStreamingCapturePlan | SdrDiskCapturePlan | HdrLayeredCapturePlan; |
| 52 | + |
| 53 | +export interface CreateCapturePlanInput { |
| 54 | + workerCount: number; |
| 55 | + forceScreenshot: boolean; |
| 56 | + forceParallelStream?: boolean; |
| 57 | + useStreamingEncode: boolean; |
| 58 | + useLayeredComposite: boolean; |
| 59 | + usePageSideCompositing: boolean; |
| 60 | + hasHdrContent: boolean; |
| 61 | + needsAlpha: boolean; |
| 62 | + routing?: CaptureRouting; |
| 63 | +} |
| 64 | + |
| 65 | +export type CapturePlanFailure = |
| 66 | + | Readonly<{ kind: "streaming_unavailable" }> |
| 67 | + | Readonly<{ kind: "draw_element_verification" }> |
| 68 | + | Readonly<{ kind: "capture_failure"; memoryExhaustion: boolean }>; |
| 69 | + |
| 70 | +function assertWorkerCount(workerCount: number): void { |
| 71 | + if (!Number.isInteger(workerCount) || workerCount < 1) { |
| 72 | + throw new Error(`CapturePlan workerCount must be a positive integer; got ${workerCount}`); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function freezeTarget(target: CapturePlanTarget): CapturePlanTarget { |
| 77 | + assertWorkerCount(target.workerCount); |
| 78 | + if (target.kind === "sdr_disk" && target.forceParallelStream) { |
| 79 | + throw new Error("CapturePlan disk fallback cannot force parallel streaming"); |
| 80 | + } |
| 81 | + return Object.freeze({ ...target }); |
| 82 | +} |
| 83 | + |
| 84 | +function freezeRouting(routing: CaptureRouting | undefined): CaptureRouting { |
| 85 | + if (!routing || routing.kind === "default") return Object.freeze({ kind: "default" }); |
| 86 | + return Object.freeze({ ...routing, fallback: freezeTarget(routing.fallback) }); |
| 87 | +} |
| 88 | + |
| 89 | +export function createCapturePlan(input: CreateCapturePlanInput): CapturePlan { |
| 90 | + assertWorkerCount(input.workerCount); |
| 91 | + const base = { |
| 92 | + workerCount: input.workerCount, |
| 93 | + forceScreenshot: input.forceScreenshot || input.usePageSideCompositing, |
| 94 | + forceParallelStream: input.useStreamingEncode ? (input.forceParallelStream ?? false) : false, |
| 95 | + usePageSideCompositing: input.usePageSideCompositing, |
| 96 | + hasHdrContent: input.hasHdrContent, |
| 97 | + needsAlpha: input.needsAlpha, |
| 98 | + routing: freezeRouting(input.routing), |
| 99 | + }; |
| 100 | + |
| 101 | + if (input.useLayeredComposite) { |
| 102 | + return Object.freeze({ |
| 103 | + ...base, |
| 104 | + kind: "hdr_layered", |
| 105 | + forceScreenshot: true, |
| 106 | + forceParallelStream: false, |
| 107 | + }); |
| 108 | + } |
| 109 | + if (input.useStreamingEncode) { |
| 110 | + return Object.freeze({ ...base, kind: "sdr_streaming" }); |
| 111 | + } |
| 112 | + return Object.freeze({ ...base, kind: "sdr_disk", forceParallelStream: false }); |
| 113 | +} |
| 114 | + |
| 115 | +function revertedRouting(routing: CaptureRouting): CaptureRouting { |
| 116 | + if (routing.kind === "default") return routing; |
| 117 | + return freezeRouting({ ...routing, state: "reverted" }); |
| 118 | +} |
| 119 | + |
| 120 | +/** Pure, exhaustive capture fallback transition. The input plan is never mutated. */ |
| 121 | +export function replanAfterFailure(plan: CapturePlan, failure: CapturePlanFailure): CapturePlan { |
| 122 | + if (plan.kind !== "sdr_streaming") { |
| 123 | + throw new Error(`Cannot apply ${failure.kind} to ${plan.kind} capture plan`); |
| 124 | + } |
| 125 | + |
| 126 | + if (failure.kind === "streaming_unavailable") { |
| 127 | + return createCapturePlan({ |
| 128 | + ...plan, |
| 129 | + useStreamingEncode: false, |
| 130 | + useLayeredComposite: false, |
| 131 | + forceParallelStream: false, |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + const fallback = |
| 136 | + plan.routing.kind === "default" |
| 137 | + ? { kind: plan.kind, workerCount: plan.workerCount, forceParallelStream: false } |
| 138 | + : plan.routing.fallback; |
| 139 | + const workerCount = |
| 140 | + failure.kind === "capture_failure" && failure.memoryExhaustion ? 1 : fallback.workerCount; |
| 141 | + return createCapturePlan({ |
| 142 | + ...plan, |
| 143 | + workerCount, |
| 144 | + forceScreenshot: true, |
| 145 | + forceParallelStream: fallback.forceParallelStream, |
| 146 | + useStreamingEncode: fallback.kind === "sdr_streaming", |
| 147 | + useLayeredComposite: false, |
| 148 | + routing: revertedRouting(plan.routing), |
| 149 | + }); |
| 150 | +} |
0 commit comments