@melite/models-h3 0.2.2-alpha → 0.2.3-rc.4

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.
package/lib/edit.js ADDED
@@ -0,0 +1,645 @@
1
+ import { contexMaskedTarget, loadVideo, renderMaskedEditChain, resolvePlanTier, sheetRefSlots } from './chainEdit.js';
2
+ import { renderH3ChainUpscale } from './chainUpscale.js';
3
+ import { renderH3Extend } from './extend.js';
4
+ import { REFINE_2X_OOM_FRAMES, REFINE_UPSCALE_MODEL, REGION_REPAIR_GREEN_FRAMES } from './refine.js';
5
+ import { FULL_NOISE_REASON, fullNoiseClause, refuseCompetingSecondPass } from './secondPass.js';
6
+ import { resolveCarryOverlap, entryCarries, isGridLength, gridSnap, GRID_BASE_FRAMES, GRID_MAX_FRAMES, solveWindowComposition } from './timelineContract.js';
7
+ /** SAM3's checkpoint row — repair.ts's own spelling (the family's
8
+ * one tracked-mask door; the card's constant cites the same file). */
9
+ const SAM3_CKPT = 'sam3.1_multiplex_fp16.safetensors';
10
+ /** The near-full-denoise floor of the identity-safety refusal. */
11
+ export const EDIT_IDENTITY_BOIL_FLOOR = 0.8;
12
+ /** NO FALLBACK READS (the silent-error law): a string seat stated
13
+ * wrong-typed REFUSES — absent is the lawful unbound state ('' rides
14
+ * to the seat's own refusal), but a wrong-typed value never silently
15
+ * becomes unbound. */
16
+ const seatString = (p, k) => {
17
+ const v = p[k];
18
+ if (v === undefined)
19
+ return '';
20
+ if (typeof v !== 'string') {
21
+ throw new Error(`h3-timeline edit: '${k}' is stated as ${JSON.stringify(v)} — a text seat carries text; a wrong-typed value never silently reads as unbound`);
22
+ }
23
+ return v;
24
+ };
25
+ /** An optional number seat: absent falls to the documented default;
26
+ * a stated non-number REFUSES — a defaulting fallback would swallow
27
+ * a bypassed door's wrong-typed value as a silent error. */
28
+ const statedNumber = (p, k, d) => {
29
+ const v = p[k];
30
+ if (v === undefined)
31
+ return d;
32
+ if (typeof v !== 'number' || !Number.isFinite(v)) {
33
+ throw new Error(`h3-timeline edit: '${k}' is stated as ${JSON.stringify(v)} — a number seat carries a number (absent falls to ${d}); a wrong-typed value never silently defaults`);
34
+ }
35
+ return v;
36
+ };
37
+ /** THE PROBED SOURCE LENGTH (defect board D-5): the compose door
38
+ * stamps the source clip's probed frame count (`__source_frames`)
39
+ * onto the compose copy of every h3 bag row it can read. Returns
40
+ * the frames REMAINING past this window's start — the widest
41
+ * window the film can actually serve — or undefined when the door
42
+ * could not probe (unresolvable or unreadable): the conservative
43
+ * clip_seconds plan keeps judging, never a silent pass. A
44
+ * wrong-typed stamp refuses like any other seat — the door's
45
+ * number is never guessed around. */
46
+ const probedSourceFrames = (entry, windowStart) => {
47
+ const n = entry.__source_frames;
48
+ if (n === undefined)
49
+ return undefined;
50
+ if (typeof n !== 'number' || !Number.isFinite(n) || n <= 0) {
51
+ throw new Error(`h3-timeline edit: '__source_frames' is stated as ${JSON.stringify(n)} — the compose door's probed-length stamp is a positive frame count; a wrong-typed value never silently reads as unprobed`);
52
+ }
53
+ return Math.max(0, Math.round(n) - Math.round(windowStart));
54
+ };
55
+ /** THE VIDEO-EDIT MATRIX RENDERER: composes one free cell of the
56
+ * mask × reference × denoise matrix on the family's one chain
57
+ * spine. Validates the safety law, derives the dial's own mask and
58
+ * anchors, and hands a body that rides contexMaskedTarget — the
59
+ * stage-1 seam, never a hand-rolled pair.
60
+ * @param entry the edit entry's own seats (edit_prompt, denoise,
61
+ * keep_audio, seed, steps, clip_seconds, overlap_frames,
62
+ * resolution, window_start, window_length, mask_grow, mask_blur,
63
+ * detection_threshold — the card's one bag)
64
+ * @param mask the mask dial's resolved value
65
+ * @param reference the reference dial's resolved value
66
+ * @param p the walked doc params (the lora mod bag + SLA/shift
67
+ * knobs — the family plane this edit renders under)
68
+ * @param facts the family's lane facts (unet off the DECL row)
69
+ * @param g the run's graph builder
70
+ * @param runId the run tree's id (the assemble filename rides it)
71
+ * @throws refuses the identity boil (whole-frame × self × denoise
72
+ * >= 0.8), the incoherent audio drop (partial denoise ×
73
+ * keep_audio false), the off-grid stated window, and the negative
74
+ * window seats — each loud, each naming the dial to state */
75
+ export function renderH3Edit(entry, mask, reference, p, facts, g, runId) {
76
+ const prompt = seatString(entry, 'edit_prompt');
77
+ const refineOn = entry.refine === true;
78
+ // THE SECOND-PASS CARVE-OUTS (blank prompt is lawful under BOTH
79
+ // second-pass postures): the loop posture (chain_run stated)
80
+ // reuses the SOURCE RUN'S own per-scene prompts (the conditioning
81
+ // node's documented posture; a bound prompt overrides every
82
+ // scene's pass-2 text); the same-run switch (refine) asks no
83
+ // ACTION at all — the pass re-renders the edit's own latents
84
+ // self-anchored, so the one-dial posture (§-1) composes with no
85
+ // authored text.
86
+ const chainRun = seatString(entry, 'chain_run');
87
+ if (prompt.trim() === '' && chainRun.trim() === '' && !refineOn) {
88
+ throw new Error(`h3-timeline edit: 'edit_prompt' names no action — the edit needs its text (what the masked region or frame should become; agent-composed, user-confirmed); blank is lawful only under a second pass (refine asks no action — it upscales the edit's own latents)`);
89
+ }
90
+ // THE ONE-DIAL DENOISE DERIVATION (§-1, the operator's
91
+
92
+ // for the refine flow"): unstated denoise under the refine switch
93
+ // derives the design window 0.32 — the 1 default is the fresh
94
+ // edit's full-noise pole, which the switch's own partial law
95
+
96
+ // hand-author). A stated denoise rides (presence is authorship).
97
+ const denoise = statedNumber(entry, 'denoise', refineOn ? 0.32 : 1);
98
+ if (!(denoise > 0 && denoise <= 1)) {
99
+ throw new Error(`h3-timeline edit: denoise ${denoise} — the pole lives (0, 1] (partial holds the unmasked region, 1 re-renders the masked one whole); state the dial inside the band`);
100
+ }
101
+ const keepAudio = entry.keep_audio !== false;
102
+ // ── THE UPSCALE POSTURE (slice 3 of the collapse — the doc's
103
+ // collapse map: "upscale = source: prior run + scale 2x"): a
104
+ // stated chain_run seats the door into the DEFERRED second pass
105
+ // — the prior chain run's checkpointed scenes resumed scene by
106
+ // scene through the requeue loop (CheckpointManager →
107
+ // UpscaleAdapter → … → UpscaleLoopEnd), a DIFFERENT mechanism
108
+ // than the refine switch's same-run handoff (the doc's
109
+ // "duplicate 2x" resolved: one door, the SOURCE names which).
110
+ // The clip dials (mask, anchors, window) are inert here — the
111
+ // loop has no masked target and no source clip; denoise rides
112
+ // the pass-2 window (the loop's own law refuses >= 1 below).
113
+ // ──
114
+ if (chainRun.trim() !== '') {
115
+ // THE ONE-SEAT LAW (the shared second-pass gate): the switch
116
+ // and the stated run are two arms of ONE seat — the shared
117
+ // refusal names it (secondPass.ts owns the law; this door
118
+ // owns the voice).
119
+ refuseCompetingSecondPass({ refine: entry.refine === true, chain_run: chainRun }, 'h3-timeline edit');
120
+ if (entry.continuation === true) {
121
+ throw new Error(`h3-timeline edit: a continuation under an upscale — the continuation generates FRESH seconds off a source clip, the upscale resumes a PRIOR run's checkpointed scenes (no clip rides); state one posture`);
122
+ }
123
+ renderH3ChainUpscale({
124
+ chain_run: chainRun,
125
+ // the loop owns the denoise law on its posture (the
126
+ // placement cut: verbatim-or-absent — undefined rides
127
+ // the 0.24 default inside, a STATED 1 refuses loud at
128
+ // the loop's own law, never at a matrix law above)
129
+ ...(typeof entry.denoise === 'number'
130
+ ? { denoise: entry.denoise }
131
+ : {}),
132
+ steps: statedNumber(entry, 'steps', 20),
133
+ segment_crf: statedNumber(entry, 'segment_crf', 18),
134
+ prompt,
135
+ }, p, facts, g, runId);
136
+ return;
137
+ }
138
+ // ── THE CONTINUATION DIAL (slice 2 of the collapse — the doc's
139
+ // collapse map: "extend = mask=whole-frame + continuation policy,
140
+ // not a separate arm"): continuation ON delegates to the
141
+ // continuation seat — fresh full-denoise seconds appended to the
142
+ // prior's last frame (the forced AddGuide seam), joined into one
143
+ // film. Four posture laws refuse loud before the delegation ──
144
+ if (entry.continuation === true) {
145
+ if (mask.source !== 'whole-frame') {
146
+ throw new Error(`h3-timeline edit: a masked continuation (mask_source '${mask.source}') — extended seconds are NEW content, nothing is held; the continuation rides the blank mask (mask_source whole-frame)`);
147
+ }
148
+ if (reference.mode !== 'self') {
149
+ throw new Error(`h3-timeline edit: an anchored continuation (reference '${reference.mode}') — the continuation's anchors are the prior's own (its last frame as the pixel anchor, the whole clip as the motion identity); reference stays self`);
150
+ }
151
+ if (denoise !== 1) {
152
+ throw new Error(`h3-timeline edit: denoise ${denoise} under a continuation — extended seconds generate FRESH at full denoise (a partial pass would hold pixels that do not exist yet); state denoise 1 or leave continuation off`);
153
+ }
154
+ if (entry.refine === true) {
155
+ throw new Error(`h3-timeline edit: the refine pass under a continuation — the continuation's tail is fresh generation (full noise); a full-noise refine discards the learned latent (the same law that refuses denoise 1 under the switch). Leave refine off`);
156
+ }
157
+ renderH3Extend({
158
+ source_clip: seatString(entry, 'source_clip'),
159
+ extend_prompt: prompt,
160
+ extend_seconds: statedNumber(entry, 'extend_seconds', 2.33),
161
+ seed: statedNumber(entry, 'seed', 0),
162
+ steps: statedNumber(entry, 'steps', 6),
163
+ resolution: resolvePlanTier(entry, p),
164
+ overlap_frames: statedNumber(entry, 'overlap_frames', 39),
165
+ // THE WINDOW ROSTER (D-03 R4b): the per-window prompt seat
166
+ // rides the continuation verbatim — absent stays absent (the
167
+ // one-prompt law), and the roster's own judge (line count,
168
+ // blank lines) lives at the extend door, one law one seat.
169
+ window_prompts: seatString(entry, 'window_prompts'),
170
+ }, p, facts, g, runId);
171
+ return;
172
+ }
173
+ // THE REFINE FULL-NOISE LAW (hoisted above the boil by the
174
+ // placement law — slice 4: the specific refusal owns the cell it
175
+ // names; under the refine switch the full-noise sigma law fires,
176
+ // never the general boil beside it). The clause and reason ride
177
+ // the shared second-pass gate (secondPass.ts) — every arm of the
178
+ // seat refuses the same pole in its own word.
179
+ if (entry.refine === true && denoise >= 1) {
180
+ throw new Error(`h3-timeline edit: denoise ${denoise} with the refine pass on — ${fullNoiseClause('refine')} (${FULL_NOISE_REASON} and the vendored node throws); state the partial window (0.05–0.5) or leave refine off`);
181
+ }
182
+ // THE UNGUIDED FULL RENDER (the doc's one deliberate loss,
183
+ // re-authored as validation — slice 4): a MASKED full-denoise
184
+ // pass anchored to nothing but the source is the identity-swap
185
+ // cell with its anchors unstated — the arm-name safety the peer
186
+ // bags carried, stated as the parameter law that replaces it.
187
+ if (denoise >= 1 && mask.source !== 'whole-frame' && reference.mode === 'self') {
188
+ throw new Error(`h3-timeline edit: denoise ${denoise} on the self anchor with a mask — an unguided full re-render of the masked region (the identity-swap cell with its anchors unstated; the subject drifts with no replacement identity to hold). THE MISSING FIELD IS 'mannequin' — the motion video seated beside 'still' under reference still+mannequin ('still' alone is not a posture: the enum is self | still+mannequin; a stated 'still' seat is read only under still+mannequin), or take the partial window (denoise < 1, the repair posture). With mask_source 'region' a plate is OPTIONAL — region_prompt selects the region by text and the edit prompt redraws inside it; a reference image is required only when the new content must be a specific person or a specific mark`);
189
+ }
190
+ // THE IDENTITY BOIL (F3-17's fold): near-full denoise, no anchor
191
+ // but the source itself, nothing held — that is a style boil with
192
+ // the subject's own prior as its only identity, and the friction
193
+ // audit watched it break identity while reading as an
194
+ // improvement. Name the two lawful doors, refuse the cell.
195
+ if (mask.source === 'whole-frame' && reference.mode === 'self' && denoise >= EDIT_IDENTITY_BOIL_FLOOR) {
196
+ throw new Error(`h3-timeline edit: whole-frame × self reference × denoise ${denoise} — the identity boil (a near-full re-render whose only identity anchor is the source's own prior drifts the subject while reading as an improvement, the F3-17 burn). Anchor it (reference still+mannequin) or use the reshoot door for a true re-render`);
197
+ }
198
+ // THE AUDIO HOLD: a partial pass holds the unmasked region — and
199
+ // the soundtrack is the hold's other half. Dropping it is never a
200
+ // coherent cell; the full pole dials it.
201
+ if (denoise < 1 && !keepAudio) {
202
+ throw new Error(`h3-timeline edit: denoise ${denoise} with keep_audio off — a partial pass holds the unmasked region and its sound (the hold is the point); generated audio rides only the full-denoise pole (state denoise 1 to drop the source soundtrack)`);
203
+ }
204
+ const windowStart = statedNumber(entry, 'window_start', 0);
205
+ const windowLength = statedNumber(entry, 'window_length', 0);
206
+ if (windowStart < 0 || windowLength < 0) {
207
+ throw new Error(`h3-timeline edit: window_start ${windowStart} / window_length ${windowLength} — a window seat never goes negative (frames at the film clock, 24 fps; 0/0 is the whole clip)`);
208
+ }
209
+ // THE INTENT-PHRASED GRID REFUSAL (defect board row 8, the
210
+ // rider on row 4's land): arithmetic voicing ("nearest legal")
211
+ // taught frame arithmetic; the cure the operator asked to READ
212
+ // is carry — leave the window unstated and the film splits
213
+ // itself. The expert override stays open (a legal rung rides).
214
+
215
+ // operator's "2 seconds for the refine flows" is 48 f at the
216
+ // film clock and 48 f CANNOT exist — the engine serves 22+17k
217
+ // frames, so the 2 s class's rung IS 56 f = 2.33 s, the
218
+ // memory-safe ceiling at refine 2x; the decree rounds UP to it,
219
+ // and the refusal must SAY so, never leave "the nearest" as the
220
+ // only number an agent can read).
221
+ if (windowLength > 0 && !isGridLength(Math.round(windowLength))) {
222
+ const nearest = gridSnap(Math.min(GRID_MAX_FRAMES, Math.max(GRID_BASE_FRAMES, Math.round(windowLength))));
223
+ throw new Error(`h3-timeline edit: window_length ${windowLength}f is not on the film's grid — ${nearest}f is the nearest${nearest === 56 && windowLength < 56 ? ' and it IS the ceiling of the 2 s class (48 f cannot exist: the engine serves 22+17k frames, so the "2 seconds" decree rounds UP to the 56 f rung = 2.33 s, the memory-safe window at refine 2x)' : ''}, or leave the window unstated and carry: true (the default) splits the film automatically (never type frame numbers)`);
224
+ }
225
+ // ── THE UPSCALE DIAL (the fourth dial — FRICTION_3's DESIGN
226
+ // PROPOSAL: "I flip a switch for refine, it opens up a submenu
227
+ // for scaling"): refine ON adds the learned second pass to THIS
228
+ // chain — the handoff rides the edit's own latents (the same-run
229
+ // pass, never the saved-clip re-encode). Two laws ride the
230
+ // switch renderer-side: partial denoise only (a full-noise sigma
231
+ // discards the learned upscaled latent and the vendored node
232
+ // throws), and the >=2x OOM ceiling with its two cures — the
233
+ // multiplier band itself is the walk's declared-band judge
234
+ // (F3-07's landed law: the declaration binds, not the tooltip),
235
+ // never re-judged where the card door cannot reach a second
236
+ // opinion.
237
+ // `scale` is INERT when refine is off (resolve default-fills it —
238
+ // the default-fill burn's law: presence is never authorship). ──
239
+ // ── THE UPSCALE DIAL: refine ON adds the learned second pass to
240
+ // THIS chain (see the derivation at the denoise read above) ──
241
+ const scale = statedNumber(entry, 'scale', 2);
242
+ if (refineOn) {
243
+
244
+ // refine 2x press on a 56 f clip refused "a 144 f window" — the
245
+ // unstated window planned the 6 s default and never read the
246
+ // film): the window the 2x pass actually upsizes is the plan's
247
+ // FIRST SEGMENT, never wider than the film itself. The compose
248
+ // door stamps the source's probed frame count (__source_frames)
249
+ // on the compose copy; with it a film shorter than the plan
250
+ // composes unstated (56 f judged, not the blind 144). Without it
251
+ // (an unresolvable or unreadable source) the conservative
252
+ // clip_seconds plan keeps its own voice — never a silent pass.
253
+ // (the window needs NO derivation: the probe-capped plan slice
254
+ // already composes every ≤141 f film unstated — min(144,
255
+ // probed) — and the unprobed belt keeps its conservative voice;
256
+ // the D-5 law owns this seat, walled in carry-split.spec.ts)
257
+
258
+ // 2x ceiling is 141 f and the UNSTATED plan derived 6 s = 144 f,
259
+ // so every unstated-window 2x refine on a film longer than the
260
+ // ceiling refused quoting a window the caller never stated —
261
+ // "a 144 f window at refine 2x is TOO LONG" — while the honest
262
+ // answer for a probeable film is the TILE guard's: the plan
263
+ // tiles, the chain cannot join 1x prefixes to 2x windows, one
264
+ // window per press): when the source IS probed and the caller
265
+ // stated no clip dial, the derived plan slices at the ceiling
266
+ // (141 f = 5.875 s, REFINE_2X_OOM_FRAMES at the film clock —
267
+ // derived from the measured constant, never hand-typed), the
268
+ // per-window judge passes, and the multi-segment guard owns the
269
+ // press with its one-window cure. A STATED clip_seconds keeps
270
+ // the caller's number (the expert's slice judges on its own
271
+ // feet); an UNPROBED source keeps the 6 s conservative voice —
272
+ // the D-5 belt: no stamp, no tiling claim, the slice ceiling
273
+ // speaks.
274
+ const probedEarly = probedSourceFrames(entry, windowStart);
275
+ const clipStated = typeof entry['clip_seconds'] === 'number';
276
+ const derivedClipSeconds = !clipStated && scale >= 2 && probedEarly !== undefined
277
+ ? REFINE_2X_OOM_FRAMES / 24
278
+ : 6;
279
+ const planFrames = Math.round(statedNumber(entry, 'clip_seconds', derivedClipSeconds) * 24);
280
+ const probed = probedEarly;
281
+ const windowFrames = windowLength > 0
282
+ ? Math.round(windowLength)
283
+ : Math.min(planFrames, probed ?? planFrames);
284
+ if (scale >= 2 && windowFrames > REFINE_2X_OOM_FRAMES) {
285
+ throw new Error(`h3-timeline edit: a ${windowFrames} f window at refine ${scale}x is TOO LONG — the 2x pass was measured OOM at ${REFINE_2X_OOM_FRAMES} f (H3 memory grows with the window; whichever sizing mode upsizes, a window that dies is a window too long). Cut the window (window_length / clip_seconds) or lower scale below 2 for a same-size pass` +
286
+ (probed !== undefined && windowFrames === planFrames
287
+ ? ` — the source probed ${probed + Math.round(windowStart)} f and outlives the ${planFrames} f plan slice, so the slice itself judged`
288
+ : ``));
289
+ }
290
+ }
291
+ if (mask.source === 'static-png' && mask.image.trim() === '') {
292
+ throw new Error(`h3-timeline edit: the static-png mask dial names no image — bind ONE staged PNG (the track run's pixel-max union is the recommended bridge); per-frame motion is the region dial's own door`);
293
+ }
294
+ if (mask.source === 'region' && mask.prompt.trim() === '') {
295
+ throw new Error(`h3-timeline edit: the region mask dial names no target — state the free-text SAM3 detection prompt ("hands", "a person", "a face")`);
296
+ }
297
+ if (mask.source === 'video' && mask.video.trim() === '') {
298
+ throw new Error(`h3-timeline edit: the video mask dial names no media — bind ONE staged mask video (per-frame, white = edit; the track card's mask export is the recommended bridge), and it must length-match the clip's own frame count or the run refuses naming both counts`);
299
+ }
300
+ if (reference.mode === 'still+mannequin' && (reference.still.trim() === '' || reference.mannequin.trim() === '')) {
301
+ throw new Error(`h3-timeline edit: the still+mannequin reference dial needs both seats bound (the still anchors appearance, the mannequin carries motion); a self-anchored edit states the self reference`);
302
+ }
303
+ // ── the staged media: the window loader (cap + skip on one VHS
304
+ // seat — frames and audio stay in sync) ──
305
+ const sourceClip = seatString(entry, 'source_clip');
306
+ if (sourceClip.trim() === '') {
307
+ throw new Error(`h3-timeline edit: 'source_clip' names no media — the edit rides a staged clip (ONE leaf: the VHS window loader + the core LoadVideo read it by staged name)`);
308
+ }
309
+ const [srcFrames, srcAudio] = loadVideo(g, sourceClip, Math.round(windowLength), Math.round(windowStart));
310
+ // ── THE CALLS→EDIT SEAM TRANSLATION (audit item 5; board row 4's
311
+ // acceptance: "The film already has windows on the timeline. The
312
+ // edit door should split on those … and join with the same
313
+ // overlap"). When the entry states NO window (0/0 — the default
314
+ // carry press) and the doc's calls[] carry AUTHORED windows (two
315
+ // or more spans), the edit's plan shots ARE the calls' spans:
316
+ // one shot per window at the window's own length, each seam
317
+ // carrying what its call states — transition 'cut' →
318
+ // context_length 0 (the engine's cut preset), any carry
319
+ // transition → the derived AV join (resolveCarryOverlap: 39 under
320
+ // carry, 0 when the entry states carry: false — a keyframe is a
321
+ // gen-side anchor; a masked edit carries, and the shot's id
322
+ // STATES the translation, never a silent downgrade). The shot
323
+ // lengths are the windows' own spans — the engine's plan node
324
+ // judges each against its lattice and refuses loud naming the
325
+ // shot. A single call is the un-split film: the source-sliced
326
+ // default press stands, nothing changes. A stated expert window
327
+ // rules over the calls (the override is the expert's, not
328
+ // derived). ──
329
+ const callsRaw = Array.isArray(p.calls) ? p.calls : [];
330
+ const windows = callsRaw.filter((c) => typeof c === 'object' && c !== null);
331
+ const authored = windowStart === 0 && windowLength === 0 && windows.length >= 2
332
+ && windows.every((c) => {
333
+ const start = Number(c['start_frame']);
334
+ const end = Number(c['end_frame']);
335
+ return Number.isFinite(start) && Number.isFinite(end) && end - start >= 1;
336
+ });
337
+ const carryJoin = resolveCarryOverlap(entry);
338
+ const seamShots = authored
339
+ ? windows.map((c, i) => {
340
+ const span = Math.max(1, Math.round(Number(c['end_frame']) - Number(c['start_frame'])));
341
+ const cut = c['transition'] === 'cut' || !entryCarries(entry);
342
+ return {
343
+ id: `w${i + 1}${cut ? '-cut' : '-carry'}`,
344
+ prompt,
345
+ length: span,
346
+ ...(i === 0 ? {} : { context_length: cut ? 0 : carryJoin }),
347
+ };
348
+ })
349
+ : null;
350
+
351
+ // a single-window edit press ACCEPTED audio_clips rows with no
352
+ // error and no warning — and the A/B measured them inert (round
353
+ // 2, F-236: rows in vs rows out, mouth-curve r 0.995, the same
354
+ // film) — a silent no-op on the one field an agent believes
355
+ // conditions the mouth. The film path already refuses the shape
356
+ // ("audio clips ride the FILM path — this single-window doc has
357
+ // clips but no film to mix over"); the edit path now owns the
358
+ // same truth: a single-window edit (no authored calls) cannot
359
+ // condition on the words — the mouth is re-rendered from source
360
+ // pixels at denoise with no audio rail while keep_audio muxes the
361
+ // soundtrack through. Refuse loud, naming both lawful doors.)
362
+ const clipsRaw = p['audio_clips'];
363
+ const clipsOnEdit = Array.isArray(clipsRaw) ? clipsRaw.length : 0;
364
+ if (clipsOnEdit > 0 && seamShots === null) {
365
+ throw new Error(`h3-timeline edit: audio_clips ride the FILM path — this single-window edit press carries ${clipsOnEdit} row${clipsOnEdit === 1 ? '' : 's'} the pass cannot condition on (measured inert at the window: the mouth re-renders from source pixels at denoise, keep_audio muxes the soundtrack untouched). Add a second call to author a film, or press the edit first and place the line on the film after (a window's mouth is free — the known 2x sync risk)`);
366
+ }
367
+ // ── THE EDIT-CHAIN LATTICE PRE-JUDGE (defect board D-24,
368
+
369
+ // length at 22+17k — "Shot 2 length must be an H3-valid frame
370
+ // length between 5 and 3592 with length % 17 == 5; got 51" —
371
+ // while the FILM door's own grid law documents carried spans as
372
+ // 17k (17, 34, 51 …), so every film-legal carried span authored
373
+ // onto an edit press died at the ENGINE with the SPAN printed
374
+ // where a generated length belongs, and the obvious cure
375
+ // (authoring 22+17k spans at the film door) contradicts the film
376
+ // grid's carried rung: two doors, two lattices, one press. The
377
+ // compose owns the reconciliation: judge every authored shot
378
+ // against the chain's own lattice HERE — zero GPU — and refuse
379
+ // naming BOTH readings (the span, the lawful generation, and
380
+ // which door's grid each belongs to), never let the engine's
381
+ // span-only lecture be the first word. The board's own receipts:
382
+ // 51 AND 17 both refused at the engine — the chain carves no
383
+ // carried window, it judges the handed length verbatim.)
384
+ if (seamShots !== null) {
385
+ for (let i = 0; i < seamShots.length; i++) {
386
+ const shot = seamShots[i];
387
+ const span = shot.length;
388
+ if (!isGridLength(span)) {
389
+ const nearest = gridSnap(Math.min(GRID_MAX_FRAMES, Math.max(GRID_BASE_FRAMES, span)));
390
+ throw new Error(`h3-timeline edit: shot ${i + 1}'s ${span} f span is not a length the edit chain generates — the chain serves 22+17k frames (its lattice judges every shot verbatim: carried or cut, the SPAN is the generated length; ${nearest}f is the nearest). The film grid's carried rungs (17, 34, 51 …) are the FILM door's law, not the edit chain's: retime shot ${i + 1} to a 22+17k span${i > 0 ? ' or declare transition "cut"' : ''}, or press the edit per window (a stated window_length)`);
391
+ }
392
+ }
393
+ }
394
+
395
+ // engine burn `h3_masked_prefix: previous/target video latent
396
+ // geometry differs: (1, 24, 17, 168, 96) vs (1, 24, 17, 84, 48)` —
397
+ // segment 0 refined emits a 2x latent, segment 1's prefix builds
398
+ // at 1x, and the chain divides at 1x to re-join at 2x, which can
399
+ // never match; the first segment renders, the second aborts the
400
+ // whole run AFTER the GPU bill): refine + scale >= 2 over a plan
401
+ // the film splits into 2+ windows refuses HERE, zero GPU. The
402
+ // single-window press is the measured posture (56 f at 768×1344 →
403
+ // 1536×2688, AAC kept, ~3 min, a pre-trimmed clip or a stated
404
+ // window) — refine is an output-space pass; the full-film 2x tile
405
+ // owes the assembler its own upscale-after-assembly door. The
406
+ // guard counts the plan's own two faces: AUTHORED calls (the
407
+ // literal shots) count directly; the source-sliced plan — a film
408
+ // LONGER than its first plan slice (probed total past the slice,
409
+ // exactly the press the engine dies on) — tiles through the D-5
410
+ // probe stamp and counts through the solver (an unprobed source
411
+ // stays the engine's belt — the same posture every probe arm here
412
+ // rides). The plan SLICE fits or the D-5 per-window ceiling has
413
+ // the next word — the wrong cure applied in the wrong order is
414
+ // the silent error a wall must refuse.
415
+ const planFramesGuard = Math.round(statedNumber(entry, 'clip_seconds', 6) * 24);
416
+ if (refineOn && scale >= 2) {
417
+ // A STATED window caps the plan at one segment (the measured
418
+ // single-window press) — the guard only judges plans the film
419
+ // or the calls actually split.
420
+ const probedTotal = probedSourceFrames(entry, windowStart);
421
+ // D-28's companion: a total past the slice tiles AT LEAST twice —
422
+ // when the solver cannot compose the total exactly its own
423
+ // exactness lecture must not shadow the tile truth (a refine
424
+ // press past the ceiling is dead either way; the honest first
425
+ // word is the join burn, never uniform-window arithmetic)
426
+ const tileCount = (total) => {
427
+ try {
428
+ return solveWindowComposition(total, 'edit', carryJoin).windows;
429
+ }
430
+ catch {
431
+ return 2;
432
+ }
433
+ };
434
+ const planWindows = windowLength > 0
435
+ ? 1
436
+ : (seamShots !== null
437
+ ? seamShots.length
438
+ : (probedTotal !== undefined && probedTotal > planFramesGuard
439
+ ? tileCount(probedTotal)
440
+ : null));
441
+ if (planWindows !== null && planWindows >= 2) {
442
+ throw new Error(`h3-timeline edit: refine ${scale}x over a ${planWindows}-window plan cannot tile — the chain joins its segments at 1x geometry while each refined window emits ${scale}x latents (the engine dies mid-run: h3_masked_prefix 'previous/target video latent geometry differs'), so a multi-window refine press burns GPU on the first segment to abort on the second. Refine ONE window per press (a stated window_length, or a pre-trimmed clip — the measured posture: 56 f at 2x, ~3 min, AAC kept); the full-film 2x tile owes the assembler its own upscale-after-assembly door`);
443
+ }
444
+ }
445
+ // ── THE REGION-REPAIR MEMORY BAND (defect board D-32,
446
+
447
+ // anywhere — passes.stage() band-checks only 2x passes — and a
448
+ // per-frame SAM3 mask over a 481 f film OOM-KILLED the engine
449
+ // unit (17 minutes in, melite-comfyui.service degraded, kernel
450
+ // oom), while the SAME shape at 362 f ran green (run-101654771803:
451
+ // region "a person", denoise 0.5, grow 32 / blur 12, whole film,
452
+ // 948 → 1142 median Laplacian). The observed boundary sits
453
+ // between 362 and 481 f and nothing at the door knew it. The
454
+ // region mask is per-frame SAM3 work over the whole clip BEFORE
455
+ // the denoise pass consumes it — its cost rides the FILM LENGTH,
456
+ // not the window the plan derives. THE BAND (conservative, at the
457
+ // measured-green datapoint): a region-masked whole-film press
458
+ // over 362 f refuses at compose naming the window law — zero
459
+ // GPU, zero dead engines. `mask_source: "whole-frame"` (no SAM3)
460
+ // is the lawful whole-film posture at any length; a pre-trimmed
461
+ // clip or a stated window_start + window_length presses the
462
+ // repair per window.) ──
463
+ if (mask.source === 'region' && windowLength === 0) {
464
+ const regionProbed = probedSourceFrames(entry, windowStart);
465
+ if (regionProbed !== undefined && regionProbed > REGION_REPAIR_GREEN_FRAMES) {
466
+ throw new Error(`h3-timeline edit: a whole-film region repair over ${regionProbed} f exceeds the measured SAM3 mask band — 362 f ran green, 481 f OOM-killed the engine unit (the region mask is per-frame SAM3 work over the whole clip before the denoise pass; its cost rides the film length, not the window). Press the repair per window (window_start + window_length — one window per press), pre-trim the clip, or use mask_source "whole-frame" (no SAM3) for a whole-film pass at any length`);
467
+ }
468
+ }
469
+ // ── the mask dial's own derivation (outside the body, the
470
+ // presets' posture: repair's SAM3 chain, swap's staged PNG) ──
471
+ let regionMasks;
472
+ if (mask.source === 'region') {
473
+ const scaled = g.node('ImageScaleToMaxDimension', {
474
+ image: srcFrames, upscale_method: 'area', largest_size: 1008,
475
+ }).out(0);
476
+ const sam = g.node('CheckpointLoaderSimple', { ckpt_name: SAM3_CKPT });
477
+ const regionCond = g.node('CLIPTextEncode', {
478
+ text: mask.prompt, clip: sam.out(1),
479
+ }).out(0);
480
+ const track = g.node('SAM3_VideoTrack', {
481
+ images: scaled, model: sam.out(0), conditioning: regionCond,
482
+ detection_threshold: mask.detectionThreshold,
483
+ max_objects: 0, detect_interval: 1,
484
+ }).out(0);
485
+ regionMasks = g.node('SAM3_TrackToMask', {
486
+ track_data: track, object_indices: '',
487
+ }).out(0);
488
+ }
489
+ // ── the reference dial's own media + the plan's geometry anchor
490
+ // (picture_1: the still when the still anchors identity, the
491
+ // window's own first frame when the source does — repair's
492
+ // truthful-anchor posture) ──
493
+ const stillImg = reference.mode === 'still+mannequin'
494
+ ? g.node('LoadImage', { image: reference.still }).out(0)
495
+ : undefined;
496
+ const [mannequin] = reference.mode === 'still+mannequin'
497
+ ? loadVideo(g, reference.mannequin)
498
+ : [undefined];
499
+ // the plan's geometry anchor: the still when the still anchors
500
+ // identity, the window's own first frame when the source does
501
+ // (repair's truthful-anchor posture) — exactly ONE anchor node is
502
+ // created; a graph seat never serializes an unused node
503
+ const anchorFrame = stillImg === undefined
504
+ ? g.node('ImageFromBatch', {
505
+ image: srcFrames, batch_index: 0, length: 1,
506
+ }).out(0)
507
+ : stillImg;
508
+ renderMaskedEditChain(g, {
509
+ p, facts,
510
+ clipSeconds: statedNumber(entry, 'clip_seconds', 6),
511
+ overlapFrames: resolveCarryOverlap(entry),
512
+ resolution: resolvePlanTier(entry, p),
513
+ seed: statedNumber(entry, 'seed', 0),
514
+ steps: statedNumber(entry, 'steps', 6),
515
+ roles: '',
516
+ sourceClip,
517
+ windowStart,
518
+ picture1: anchorFrame,
519
+ srcFrames, srcAudio,
520
+ // the plan's shot law: AUTHORED shots (the calls' spans + the
521
+ // stated seams) rule as the literal — the plan node must not
522
+ // re-slice the source over them; the un-split film keeps the
523
+ // source-sliced default press (the literal is the ignored
524
+ // fallback, plan_json_input rides)
525
+ planJson: seamShots !== null
526
+ ? JSON.stringify({ shots: seamShots })
527
+ : JSON.stringify({ shots: [{ id: 'edit', prompt }] }),
528
+ planFromSource: seamShots === null,
529
+ denoise,
530
+ ...(refineOn
531
+ ? { upscale: { mode: 'scale by multiplier', scale, modelName: REFINE_UPSCALE_MODEL } }
532
+ : {}),
533
+ assemble: {
534
+ audioSource: keepAudio ? 'source' : 'generated',
535
+ filename: `${runId}/edit`,
536
+ },
537
+ body: ({ cur, clip, vaeVideo, vaeAudio, mutedChainAudio, sliceAudio, refImages }) => {
538
+ // ── the reference dial's r2v: the anchors the conditioning
539
+ // rides (swap's pair, or repair's self seat) ──
540
+ const r2v = g.node('MiniMaxH3ReferenceToVideo', {
541
+ prompt,
542
+ width: cur.out(8), height: cur.out(9), length: cur.out(6),
543
+ ref_image_size: 'match',
544
+ clip, vae: vaeVideo, audio_vae: vaeAudio,
545
+ ...(reference.mode === 'still+mannequin'
546
+ ? {
547
+ 'ref_images.ref_image_0': stillImg,
548
+ 'ref_videos.ref_video_0': mannequin,
549
+ }
550
+ : { 'ref_videos.ref_video_0': srcFrames }),
551
+ 'ref_video_audios.ref_video_audio_0': mutedChainAudio,
552
+ // THE SHEET (P0-1): after the dial's own anchor — slot 1
553
+ // under still+mannequin, slot 0 on the self seat (the
554
+ // repair posture). One grammar, both input slots.
555
+ ...sheetRefSlots(refImages, reference.mode === 'still+mannequin' ? 1 : 0),
556
+ });
557
+ const slice = g.node('H3SourceSlice', {
558
+ source_fps: 24, crop: 'center',
559
+ state: cur.out(0), latent: r2v.out(1),
560
+ audio_vae: vaeAudio, source_frames: srcFrames, source_audio: sliceAudio,
561
+ });
562
+ // ── the mask dial, in-body: the per-scene slice + grow the
563
+ // region tracks demand, the SCALED staged PNG (F3-16 defect
564
+ // 1), or the all-white SolidMask ──
565
+ let editMask;
566
+ if (mask.source === 'region') {
567
+ const sceneMask = g.node('MiniMaxH3ContexLoopMaskSlice', {
568
+ state: cur.out(0), mask: regionMasks, source_fps: 24,
569
+ });
570
+ editMask = g.node('GrowMaskWithBlur', {
571
+ mask: sceneMask.out(0), expand: mask.grow,
572
+ tapered_corners: true, blur_radius: mask.blur,
573
+ }).out(0);
574
+ }
575
+ else if (mask.source === 'static-png') {
576
+ // THE GEOMETRY CURE (F3-16 defect 1): the staged PNG's own
577
+ // shape is the TRACK's shape, not the chain's — resize to
578
+ // the plan geometry in-graph, never trust the file.
579
+ const raw = g.node('LoadImage', { image: mask.image }).out(0);
580
+
581
+ // no engine build owns (the refusal landed at compose time,
582
+ // after the authoring pass) — core `ImageScale` is the
583
+ // exact-fit resize (width/height, never proportion-keeping).
584
+
585
+ // run-894ac56e3c8a): the resize rides the IMAGE plane and
586
+ // the conversion rides LAST — `ImageScale` is IMAGE→IMAGE
587
+ // (boot census), and a MASK socket fed an IMAGE is the
588
+ // prompt-validation death both mask-supplied doors died at
589
+ // the engine after D-16 let them compose. Same two nodes,
590
+ // only the ORDER is the law.
591
+ const fitted = g.node('ImageScale', {
592
+ image: raw, width: cur.out(8), height: cur.out(9),
593
+ upscale_method: 'bilinear', crop: 'disabled',
594
+ }).out(0);
595
+ editMask = g.node('ImageToMask', {
596
+ image: fitted, channel: 'red',
597
+ }).out(0);
598
+ }
599
+ else if (mask.source === 'video') {
600
+ // THE OWN-VIDEO MASK DOOR: the operator's staged per-frame
601
+ // mask (the track card's export or their own greyscale
602
+ // ride). Three laws ride before the slice: decode the red
603
+ // channel per frame; cure the geometry in-graph (F3-16
604
+ // defect 1's law owns this door too — a staged file's shape
605
+ // is never the chain's); then THE LENGTH-MATCH CHECK — the
606
+ // mask's frames must equal the clip's frames AS LOADED
607
+ // (window caps included), refused loud engine-side naming
608
+ // both counts. The check GATES the door: the slice rides
609
+ // the verified timeline, never the raw decode.
610
+ const [maskFrames] = loadVideo(g, mask.video);
611
+ // D-16: `ImageScale` (core, exact-fit) — `ImageResize` is a
612
+ // class no engine build owns. D-21: the resize rides the
613
+ // IMAGE plane, the conversion rides LAST (ImageScale is
614
+ // IMAGE→IMAGE; the mask sockets downstream take MASK).
615
+ const fitted = g.node('ImageScale', {
616
+ image: maskFrames, width: cur.out(8), height: cur.out(9),
617
+ upscale_method: 'bilinear', crop: 'disabled',
618
+ }).out(0);
619
+ const decoded = g.node('ImageToMask', {
620
+ image: fitted, channel: 'red',
621
+ }).out(0);
622
+ const verified = g.node('MiniMaxH3ContexLoopMaskLengthCheck', {
623
+ source_frames: srcFrames, mask: decoded,
624
+ }).out(0);
625
+ const sceneMask = g.node('MiniMaxH3ContexLoopMaskSlice', {
626
+ state: cur.out(0), mask: verified, source_fps: 24,
627
+ });
628
+ editMask = g.node('GrowMaskWithBlur', {
629
+ mask: sceneMask.out(0), expand: mask.grow,
630
+ tapered_corners: true, blur_radius: mask.blur,
631
+ }).out(0);
632
+ }
633
+ else {
634
+ editMask = g.node('SolidMask', {
635
+ value: 1, width: cur.out(8), height: cur.out(9),
636
+ }).out(0);
637
+ }
638
+ const target = contexMaskedTarget(g, { vaeVideo, vaeAudio }, {
639
+ frames: slice.out(0), audio: slice.out(1), mask: editMask, keepAudio,
640
+ });
641
+ return { conditioning: r2v.out(0), targetLatent: target };
642
+ },
643
+ });
644
+ }
645
+ //# sourceMappingURL=edit.js.map