@mengine/medeo-client 1.0.1-alpha.2 → 1.1.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.
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
- import { A as isEmptyVideoClip, B as bytesToBase64, C as fillMainTrackTimeGaps, D as resolveSpeechOverlapByShiftingVideos, E as resolveAllSpeechOverlaps, F as speedOf, I as partUnionToDraft, L as recordEntries, M as safeDurationMs, N as VIDEO_DOCUMENT_SCHEMA_VERSION, O as syncAggregatedClipsTimePosition, P as effectiveVideoClipDurationMs, R as videoDocumentMirrorSchema, S as arrangeMainTrackSeamlessly, T as recalculateTimelineDuration, _ as videoDocumentSchema, a as createMirrorVideoDocument, b as solveVideoDocument, c as readVideoDocumentFromDraft, d as fromVideoDocument, f as toVideoDocument, g as partUnionSchema, h as validateVideoDocument, i as MirrorVideoDocumentAdapter, j as partDurationMs, k as TIMELINE_SKELETON_DURATION_MS, l as buildSpeechHostMap, m as assertValidVideoDocument, n as createPlainMemoryAdapter, o as createMirrorVideoDocumentAdapter, p as VideoDocumentValidationError, r as generatePartId, s as writeVideoDocumentToDraft, t as PlainMemoryAdapter, u as derivePositionFromAbs, v as ensureLaneTrack, w as reassignSpeechesToVideoClipsByTime, x as cascadeAfterVideoClipChanges, y as findLaneTrack, z as base64ToBytes } from "./document-DgffKwRw.js";
2
- import { addSpeechesInputSchema, addVideoClipsInputSchema, adjustBgmVolumeInputSchema, adjustSpeechVolumeInputSchema, adjustVideoClipDurationInputSchema, adjustVideoClipVolumeInputSchema, changeSpeechScriptInputSchema, changeSpeechVoiceInputSchema, deleteBgmInputSchema, deleteSpeechesInputSchema, deleteVideoClipsInputSchema, moveSpeechesInputSchema, moveVideoClipsInputSchema, replaceVideoClipContentInputSchema, setBgmInputSchema, setCaptionStyleInputSchema, setCaptionVisibilityInputSchema, setVideoClipSpeedShiftInputSchema, t as schemas_exports } from "./schemas.js";
1
+ import { t as __exportAll } from "./chunk-D7D4PA-g.js";
2
+ import { A as partDurationMs, C as reassignSpeechesToVideoClipsByTime, D as syncAggregatedClipsTimePosition, E as resolveSpeechOverlapByShiftingVideos, F as recordEntries, I as videoDocumentMirrorSchema, L as base64ToBytes, M as VIDEO_DOCUMENT_SCHEMA_VERSION, N as effectiveVideoClipDurationMs, O as TIMELINE_SKELETON_DURATION_MS, P as partUnionToDraft, R as bytesToBase64, S as fillMainTrackTimeGaps, T as resolveAllSpeechOverlaps, _ as findLaneTrack, a as buildInitialVideoDocument, b as cascadeAfterVideoClipChanges, c as fromVideoDocument, d as assertValidVideoDocument, f as validateVideoDocument, g as ensureLaneTrack, h as LANE_KINDS_IN_STACK_ORDER, i as readVideoDocumentFromDraft, j as safeDurationMs, k as isEmptyVideoClip, l as toVideoDocument, m as videoDocumentSchema, n as createMirrorVideoDocument, o as buildSpeechHostMap, p as partUnionSchema, r as createMirrorVideoDocumentAdapter, s as derivePositionFromAbs, t as MirrorVideoDocumentAdapter, u as VideoDocumentValidationError, v as laneTrackId, w as recalculateTimelineDuration, x as arrangeMainTrackSeamlessly, y as solveVideoDocument } from "./document-C98vSu7J.js";
3
+ import { z } from "zod";
4
+ import { LoroDoc, VersionVector } from "loro-crdt";
3
5
  import { ClientServerSynchronizer, DocManager } from "@mengine/sync";
4
6
  import { DisposableSet, EventBus, Task } from "@mengine/utils";
5
7
  import { BaseDocStorage, DummyConnection } from "@mengine/storage";
@@ -22,6 +24,33 @@ var MengineHttpRequestError = class extends Error {
22
24
  this.name = "MengineHttpRequestError";
23
25
  }
24
26
  };
27
+ /**
28
+ * A push the server refused on its merits (`kind: 'rejected'`), as opposed to a
29
+ * transport failure. Carries the server's machine `code` so callers can branch on
30
+ * *why* rather than on an HTTP status:
31
+ *
32
+ * - `missing_dependency` (server answers 409) — retryable: the update depends on
33
+ * ops the server log lacks, so catching up and re-exporting resolves it.
34
+ * - `corrupt_update` (server answers 422) — never valid, retrying cannot help.
35
+ *
36
+ * Distinct from {@link MengineHttpRequestError} (transport/status-level failure)
37
+ * and from a raw `fetch` rejection (network down): the three are separate classes
38
+ * so a caller can tell "the server said no" from "the server never answered".
39
+ */
40
+ var MenginePushRejectedError = class extends Error {
41
+ code;
42
+ serverMessage;
43
+ serverVersion;
44
+ status;
45
+ constructor(code, serverMessage, serverVersion, status) {
46
+ super(`mengine rejected update: ${code}: ${serverMessage}`);
47
+ this.code = code;
48
+ this.serverMessage = serverMessage;
49
+ this.serverVersion = serverVersion;
50
+ this.status = status;
51
+ this.name = "MenginePushRejectedError";
52
+ }
53
+ };
25
54
  var MengineHttpClient = class {
26
55
  options;
27
56
  fetchImpl;
@@ -51,15 +80,34 @@ var MengineHttpClient = class {
51
80
  async audit() {
52
81
  return await this.requestJson("audit");
53
82
  }
54
- async pushUpdate(update, baseVersion = null) {
55
- const response = await this.requestJson("updates", {
56
- method: "POST",
57
- body: JSON.stringify({
58
- update: bytesToBase64(update),
59
- base_version: baseVersion
60
- })
61
- });
62
- if (response.kind === "rejected") throw new MengineHttpRequestError(409, response);
83
+ /**
84
+ * Append one Loro update to the server log.
85
+ *
86
+ * Resolves for both accepted outcomes and hands the verdict back verbatim —
87
+ * `ack` (appended, `update_seq` allocated) and `duplicate` (already known, no
88
+ * row appended). A `duplicate` is NOT an error, but it is also not an ack: it
89
+ * means the bytes contributed nothing, so a caller waiting for its own write to
90
+ * land must be able to tell them apart. Hence the verdict is returned rather
91
+ * than collapsed into `void`.
92
+ *
93
+ * Rejections raise {@link MenginePushRejectedError} carrying the server's
94
+ * machine `code`. The server answers them with 409/422, so the failure arrives
95
+ * as a non-ok response; this method re-reads the parsed body to recover `code` /
96
+ * `server_version` instead of leaving the caller a bare status. Transport-level
97
+ * failures stay {@link MengineHttpRequestError}, and a dead network keeps
98
+ * surfacing as the underlying `fetch` rejection.
99
+ */
100
+ async pushUpdate(update) {
101
+ let response;
102
+ try {
103
+ response = await this.requestJson("updates", {
104
+ method: "POST",
105
+ body: JSON.stringify({ update: bytesToBase64(update) })
106
+ });
107
+ } catch (error) {
108
+ throw asPushRejection(error) ?? error;
109
+ }
110
+ if (response.kind === "rejected") throw new MenginePushRejectedError(response.code, response.message, response.server_version, void 0);
63
111
  return response;
64
112
  }
65
113
  eventsUrl() {
@@ -95,6 +143,20 @@ var MengineHttpClient = class {
95
143
  return `${this.options.httpOrigin.replace(/\/$/, "")}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(this.options.docId)}/${path}`;
96
144
  }
97
145
  };
146
+ /**
147
+ * Recover a typed rejection from a failed request: the server sends `rejected`
148
+ * bodies with a 409/422 status, so they surface as {@link MengineHttpRequestError}
149
+ * whose payload still carries the machine `code`. Returns `undefined` for any
150
+ * other failure so the caller rethrows the original.
151
+ */
152
+ function asPushRejection(error) {
153
+ if (!(error instanceof MengineHttpRequestError)) return void 0;
154
+ const payload = error.payload;
155
+ if (payload == null || typeof payload !== "object") return void 0;
156
+ const body = payload;
157
+ if (body.kind !== "rejected" || typeof body.code !== "string") return void 0;
158
+ return new MenginePushRejectedError(body.code, body.message ?? "", body.server_version, error.status);
159
+ }
98
160
  async function safeReadJson(response) {
99
161
  const text = await response.text();
100
162
  if (text.length === 0) return null;
@@ -156,6 +218,566 @@ function findSseFrameBoundary(buffer) {
156
218
  return Math.min(lf, crlf);
157
219
  }
158
220
  //#endregion
221
+ //#region src/editor/id-gen.ts
222
+ /**
223
+ * Part-id generation, aligned with the online ecosystem.
224
+ *
225
+ * The authoritative online producers — agent-harness (`@harness/shared`
226
+ * `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
227
+ * ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
228
+ * shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
229
+ * previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
230
+ * different encoding — the sole cross-repo id divergence. This module removes it
231
+ * by emitting the same `<prefix>_<ULID>` bytes.
232
+ *
233
+ * The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
234
+ * rather than pulling the `ulid` npm package: the randomness class matches the
235
+ * old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
236
+ * dependency-free for a purely mechanical id string. Part ids only need to be
237
+ * unique and lexicographically time-sortable, which this satisfies.
238
+ */
239
+ /** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
240
+ const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
241
+ const TIME_LEN = 10;
242
+ const RANDOM_LEN = 16;
243
+ function encodeTime(now) {
244
+ let out = "";
245
+ let ms = now;
246
+ for (let i = TIME_LEN - 1; i >= 0; i--) {
247
+ const mod = ms % 32;
248
+ out = CROCKFORD[mod] + out;
249
+ ms = (ms - mod) / 32;
250
+ }
251
+ return out;
252
+ }
253
+ function encodeRandom() {
254
+ let out = "";
255
+ for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
256
+ return out;
257
+ }
258
+ /** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
259
+ function ulid() {
260
+ return encodeTime(Date.now()) + encodeRandom();
261
+ }
262
+ function generatePartId(prefix) {
263
+ return `${prefix}_${ulid()}`;
264
+ }
265
+ //#endregion
266
+ //#region src/editor/schemas/shared.ts
267
+ const clipIdSchema = z.string().min(1).describe("The clip part ID on the timeline");
268
+ const clipIdsSchema = z.array(clipIdSchema).min(1).refine((ids) => new Set(ids).size === ids.length, { message: "Duplicate clip IDs are not allowed" }).describe("List of clip part IDs (no duplicates allowed)");
269
+ const mediaIdSchema = z.string().min(1).describe("The media asset ID");
270
+ const speechIdSchema = z.string().min(1).describe("The speech part ID on the timeline");
271
+ const timelineMsSchema = z.number().int().min(0).describe("Time position in milliseconds on the timeline (>= 0)");
272
+ const positiveMsSchema = z.number().int().positive().describe("Duration in milliseconds (> 0)");
273
+ const volumeSchema = z.number().min(-60).max(20).describe("Volume in decibels (-60.0 to 20.0; 0.0 = original, -60 = mute, +20 = max)");
274
+ const speechIdsSchema = z.array(speechIdSchema).min(1).refine((ids) => new Set(ids).size === ids.length, { message: "Duplicate speech IDs are not allowed" }).describe("List of speech part IDs (no duplicates allowed)");
275
+ const tangentHandleSchema = z.object({
276
+ x: z.number().finite(),
277
+ y: z.number().finite()
278
+ }).describe("Bezier tangent handle (x, y)");
279
+ const speedKeyframeSchema = z.object({
280
+ position: z.number().min(0).max(1),
281
+ rate: z.number().min(0),
282
+ in_tangent: tangentHandleSchema.optional(),
283
+ out_tangent: tangentHandleSchema.optional()
284
+ }).describe("A speed keyframe: normalized position (0..1), rate, optional tangents");
285
+ /**
286
+ * A clip's playback-speed fact, the only thing `SetVideoClipSpeedShift` writes.
287
+ * Mirrors the IDL `SpeedShift`: `category` is `linear` | `curve`, and `config`
288
+ * is a discriminated union — `{ linear: { speed } }` for a constant multiplier
289
+ * (the multiplier projection reads at `config.linear.speed`) or `{ curve: {
290
+ * keyframes } }` for a Bezier-controlled variable speed (RFC 02 / `reference/16`
291
+ * §0). Exactly one of `linear` / `curve` is present.
292
+ */
293
+ const speedShiftSchema = z.object({
294
+ category: z.enum(["linear", "curve"]),
295
+ mode: z.string(),
296
+ config: z.union([z.object({ linear: z.object({ speed: z.number().finite().positive() }) }), z.object({ curve: z.object({ keyframes: z.array(speedKeyframeSchema).min(2) }) })])
297
+ }).describe("Speed shift: linear multiplier or Bezier curve, mirroring the IDL shape");
298
+ const voiceSchema = z.object({
299
+ id: z.string().min(1),
300
+ name: z.string()
301
+ }).describe("TTS voice summary attached to a speech");
302
+ //#endregion
303
+ //#region src/editor/schemas/speech-assets.ts
304
+ /**
305
+ * The materialized TTS result shared by `AddSpeeches` / `ChangeSpeechScript` /
306
+ * `ChangeSpeechVoice` (see `results/phase-4-side-effect-payload-contract.md`
307
+ * §1/§2). The side effect (TTS/ASR + billing) runs upstream; the op receives the
308
+ * stable speech + caption parts and writes them as authoritative facts. No
309
+ * cascade runs on write — the projection derives absolute positions on read.
310
+ *
311
+ * Each speech carries the anchoring fact directly (RFC 02 §4): the host video
312
+ * clip `anchor_part_id` and the `offset_ms` within it. The upstream caller
313
+ * already knows which clip a speech attaches to, so the op writes
314
+ * `{ mode:'anchored', anchorPartId, offsetMs }` verbatim — no write-time
315
+ * host-picking. Captions anchor to their speech via the caption part's
316
+ * `start_ms` (offset within the speech).
317
+ */
318
+ const speechAssetSchema = z.object({
319
+ speech_id: speechIdSchema.describe("The speech part ID (= side-effect speech_parts[].id)"),
320
+ anchor_part_id: clipIdSchema.describe("Host video clip part ID the speech anchors to (RFC 02 §4)"),
321
+ offset_ms: timelineMsSchema.describe("Offset within the host clip (speech.abs = host.abs + offset_ms)"),
322
+ audio_storage_key: z.string().min(1),
323
+ duration_ms: positiveMsSchema,
324
+ audio_script: z.string(),
325
+ volume: volumeSchema,
326
+ voice: voiceSchema,
327
+ origin_speech_id: z.string().min(1),
328
+ caption_ids: z.array(z.string().min(1)).describe("Caption part IDs owned by this speech")
329
+ });
330
+ const captionAssetSchema = z.object({
331
+ caption_id: z.string().min(1).describe("The caption part ID (= side-effect created_caption_parts[].id)"),
332
+ speech_part_id: speechIdSchema.describe("The owning speech part ID"),
333
+ text: z.string(),
334
+ start_ms: timelineMsSchema.describe("Offset within the host speech (caption.abs = speech.abs + start_ms)"),
335
+ duration_ms: positiveMsSchema
336
+ });
337
+ /** A materialized speech-subtree write (speeches + their captions). */
338
+ const speechAssetsSchema = z.object({
339
+ speeches: z.array(speechAssetSchema).min(1).describe("Materialized speech parts to write"),
340
+ captions: z.array(captionAssetSchema).describe("Materialized caption parts owned by the speeches")
341
+ });
342
+ //#endregion
343
+ //#region src/editor/schemas/add-speeches.ts
344
+ /**
345
+ * Add speeches (and their captions). TTS runs upstream; the stable speech /
346
+ * caption parts arrive materialized (see `speech-assets.ts`). The op writes the
347
+ * parts and each speech's `{ mode:'anchored', anchorPartId, offsetMs }` fact
348
+ * verbatim — no write-time host-picking, no cascade (RFC 02 §4). The projection
349
+ * derives absolute positions on read.
350
+ */
351
+ const addSpeechesInputSchema = speechAssetsSchema.describe("Materialized speeches + captions to add");
352
+ //#endregion
353
+ //#region src/editor/schemas/add-video-clips.ts
354
+ /**
355
+ * Add video clips to a track. Each clip's duration facts are separated so a
356
+ * single number is never overloaded (RFC 02 / `reference/16` §0b):
357
+ *
358
+ * - `media_duration_ms` is the source media's intrinsic full length (a resource
359
+ * fact, written to the part);
360
+ * - `play_in` / `play_out` are the optional trim window into that media; when
361
+ * omitted the whole media is used (`play_in=0`, `play_out=media_duration_ms`).
362
+ *
363
+ * The clip's effective timeline duration is derived by the projection from the
364
+ * trim window and `speed_shift` — it is never an input here.
365
+ */
366
+ const addVideoClipsInputSchema = z.object({
367
+ clips: z.array(z.object({
368
+ media_id: mediaIdSchema.describe("The media asset ID for the video clip"),
369
+ start_ms: timelineMsSchema.optional().describe("Absolute start time in milliseconds on the timeline"),
370
+ media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
371
+ play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
372
+ play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)"),
373
+ track_id: z.string().min(1).optional().describe("Target track ID (optional, defaults to main track)")
374
+ })).min(1).describe("List of video clips to create"),
375
+ before_clip_id: z.string().min(1).optional().describe("Insert new clips before this clip ID"),
376
+ after_clip_id: z.string().min(1).optional().describe("Insert new clips after this clip ID")
377
+ }).superRefine((data, ctx) => {
378
+ if (data.before_clip_id != null && data.after_clip_id != null) {
379
+ ctx.addIssue({
380
+ code: z.ZodIssueCode.custom,
381
+ message: "Cannot provide both before_clip_id and after_clip_id"
382
+ });
383
+ return;
384
+ }
385
+ const hasRelative = data.before_clip_id != null || data.after_clip_id != null;
386
+ for (let i = 0; i < data.clips.length; i++) {
387
+ const clip = data.clips[i];
388
+ if (hasRelative && clip.start_ms != null) ctx.addIssue({
389
+ code: z.ZodIssueCode.custom,
390
+ message: `clips[${i}].start_ms must not be provided when using before_clip_id or after_clip_id`,
391
+ path: [
392
+ "clips",
393
+ i,
394
+ "start_ms"
395
+ ]
396
+ });
397
+ if (!hasRelative && clip.start_ms == null) ctx.addIssue({
398
+ code: z.ZodIssueCode.custom,
399
+ message: `clips[${i}].start_ms is required when not using relative positioning`,
400
+ path: [
401
+ "clips",
402
+ i,
403
+ "start_ms"
404
+ ]
405
+ });
406
+ const playIn = clip.play_in ?? 0;
407
+ const playOut = clip.play_out ?? clip.media_duration_ms;
408
+ if (playOut > clip.media_duration_ms) ctx.addIssue({
409
+ code: z.ZodIssueCode.custom,
410
+ message: `clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
411
+ path: [
412
+ "clips",
413
+ i,
414
+ "play_out"
415
+ ]
416
+ });
417
+ if (playIn >= playOut) ctx.addIssue({
418
+ code: z.ZodIssueCode.custom,
419
+ message: `clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
420
+ path: [
421
+ "clips",
422
+ i,
423
+ "play_in"
424
+ ]
425
+ });
426
+ }
427
+ });
428
+ //#endregion
429
+ //#region src/editor/schemas/adjust-bgm-volume.ts
430
+ const adjustBgmVolumeInputSchema = z.object({ bgm: z.array(z.object({
431
+ bgm_id: clipIdSchema.describe("The bgm part ID to adjust volume for"),
432
+ volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
433
+ })).min(1).describe("List of bgm parts with their new volume settings") });
434
+ //#endregion
435
+ //#region src/editor/schemas/adjust-speech-volume.ts
436
+ const adjustSpeechVolumeInputSchema = z.object({ speeches: z.array(z.object({
437
+ speech_id: speechIdSchema.describe("The speech part ID to adjust volume for"),
438
+ volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
439
+ })).min(1).describe("List of speeches with their new volume settings") });
440
+ //#endregion
441
+ //#region src/editor/schemas/adjust-video-clip-duration.ts
442
+ /**
443
+ * Re-trim existing video clips (the user-facing "adjust duration" gesture is a
444
+ * trim of the source window). The new `play_in` / `play_out` are the facts; the
445
+ * effective timeline duration is derived from them and the clip's `speed_shift`,
446
+ * and the change reflows downstream clips, speeches, and the timeline inside the
447
+ * op's transaction (no caller-materialized cascade).
448
+ */
449
+ const adjustVideoClipDurationInputSchema = z.object({ clips: z.array(z.object({
450
+ clip_id: clipIdSchema.describe("The video clip part ID to re-trim"),
451
+ play_in: timelineMsSchema.describe("New trim window start in the source media"),
452
+ play_out: positiveMsSchema.describe("New trim window end in the source media")
453
+ })).min(1).describe("Video clips with their new trim windows") });
454
+ //#endregion
455
+ //#region src/editor/schemas/adjust-video-clip-volume.ts
456
+ const adjustVideoClipVolumeInputSchema = z.object({ clips: z.array(z.object({
457
+ clip_id: clipIdSchema.describe("The video clip part ID to adjust volume for"),
458
+ volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
459
+ })).min(1).describe("List of video clips with their new volume settings") });
460
+ //#endregion
461
+ //#region src/editor/schemas/change-speech.ts
462
+ /**
463
+ * Change a speech's script or voice. Both re-run TTS upstream and return the
464
+ * regenerated speech / caption parts in the same materialized shape as
465
+ * `AddSpeeches` (`speech-assets.ts`); the op upserts them by id (the speech part
466
+ * id is preserved across a re-TTS), re-seats at `start_ms`, and reflows. Old
467
+ * caption parts no longer owned by the speech are removed via `caption_ids`.
468
+ */
469
+ const changeSpeechScriptInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new script)");
470
+ const changeSpeechVoiceInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new voice)");
471
+ //#endregion
472
+ //#region src/editor/schemas/delete-bgm.ts
473
+ /**
474
+ * Remove the document BGM. Pure document edit: clears the bgm lane and removes
475
+ * the bgm part. Takes no input (a document holds at most one bgm); an empty
476
+ * object keeps the op signature uniform with the rest.
477
+ */
478
+ const deleteBgmInputSchema = z.object({}).describe("Remove the document BGM (no parameters)");
479
+ //#endregion
480
+ //#region src/editor/schemas/delete-speeches.ts
481
+ /**
482
+ * Delete speeches with their captions. Pure document edit (no side effect): the
483
+ * op removes each speech part, cascade-deletes the captions it owns (via
484
+ * `caption_ids` / `speech_part_id`), drops their track items, and reflows.
485
+ */
486
+ const deleteSpeechesInputSchema = z.object({ speech_ids: speechIdsSchema.describe("Speech part IDs to delete (their captions cascade-delete)") });
487
+ //#endregion
488
+ //#region src/editor/schemas/delete-video-clips.ts
489
+ /**
490
+ * How a delete handles the anchored subtree (speeches anchored to a deleted clip,
491
+ * and their captions) — a delete-op policy, not a data-model field (reference/17
492
+ * §6). `cascade` (default) removes the subtree; `detach` keeps the direct
493
+ * anchored children, re-pinning them to `absolute` so they stay on the timeline.
494
+ */
495
+ const anchoredDeletePolicySchema = z.enum(["cascade", "detach"]);
496
+ const deleteVideoClipsInputSchema = z.object({
497
+ clip_ids: clipIdsSchema.describe("List of video clip part IDs to delete from the main track"),
498
+ on_anchored: anchoredDeletePolicySchema.optional().describe("How to treat anchored children (default cascade)")
499
+ });
500
+ //#endregion
501
+ //#region src/editor/schemas/move-speeches.ts
502
+ /**
503
+ * Move speeches in time. Pure document edit: the op re-seats each speech at its
504
+ * new absolute `start_ms`; the cascade reassigns it to the host video clip,
505
+ * resolves overlaps, and reflows. Captions follow their speech.
506
+ */
507
+ const moveSpeechesInputSchema = z.object({ speeches: z.array(z.object({
508
+ speech_id: speechIdSchema.describe("The speech part ID to move"),
509
+ new_start_ms: timelineMsSchema.describe("New absolute start time on the timeline")
510
+ })).min(1).describe("Speeches to move to new positions") });
511
+ //#endregion
512
+ //#region src/editor/schemas/move-video-clips-by-anchor.ts
513
+ /**
514
+ * Where the moved block lands on the main track.
515
+ *
516
+ * A discriminated union rather than two optional `before_clip_id` /
517
+ * `after_clip_id` fields (the shape `addVideoClips` had to use, because there the
518
+ * two modes share a whole clip description): here the alternatives carry nothing
519
+ * in common, so making them mutually exclusive *by type* removes three runtime
520
+ * `superRefine` checks that would otherwise have to be written and tested.
521
+ *
522
+ * `track_start` is an explicit member, not the absence of an anchor. The
523
+ * agent-harness mutation this maps from treats "neither anchor given" as
524
+ * "move to the front" (`applyBatchMoveVideoClips` falls back to `insertIndex = 0`),
525
+ * which is a default buried in a tool description. Requiring the caller to name
526
+ * that intent keeps a forgotten field from silently reordering the timeline.
527
+ */
528
+ const moveAnchorSchema = z.discriminatedUnion("position", [
529
+ z.object({
530
+ position: z.literal("before"),
531
+ clip_id: clipIdSchema.describe("The moved block lands immediately before this clip")
532
+ }),
533
+ z.object({
534
+ position: z.literal("after"),
535
+ clip_id: clipIdSchema.describe("The moved block lands immediately after this clip")
536
+ }),
537
+ z.object({ position: z.literal("track_start") })
538
+ ]).describe("Where the moved block lands: before/after a reference clip, or at the head of the track");
539
+ /**
540
+ * What happens to the speeches anchored to the clips being moved.
541
+ *
542
+ * - `follow` keeps each speech anchored where it is, so it travels with its clip
543
+ * to the new position. In the anchored model this is the *no-op* branch: a
544
+ * speech's authoritative fact is `{ anchorPartId, offsetMs }` and its absolute
545
+ * time is derived on read from the host's position, so moving the host moves
546
+ * the speech with no write to the speech at all.
547
+ * - `keep_absolute` preserves each speech's current absolute landing instead, then
548
+ * re-anchors it to whichever clip now covers that time (RFC 02 §9.1/§11.1). This
549
+ * is the branch that costs an extra pass, and the one the FE timeline uses.
550
+ *
551
+ * **Required, with no default**, matching `deleteVideoClips` and
552
+ * `replaceVideoClipSequence`. The two branches decide which picture the user's
553
+ * narration ends up over, which is too consequential to infer from a missing
554
+ * field — and neither branch is "safe enough" to be the implicit one.
555
+ */
556
+ const movedClipAnchoredPolicySchema = z.enum(["follow", "keep_absolute"]);
557
+ /**
558
+ * Reorder a set of main-track clips relative to a reference clip.
559
+ *
560
+ * Distinct from `moveVideoClips`, which positions clips by absolute time
561
+ * (`new_start_ms`) and is what the FE timeline dispatches after a drag. Main-track
562
+ * clips are `sequential`-positioned, so absolute time is not authoritative state
563
+ * there (RFC 02 §4/§7): `moveVideoClips` has to *guess* an index back out of the
564
+ * time it was handed, whereas an anchor already is the ordinal fact being changed.
565
+ * Keeping them separate also isolates blast radius — this method can diverge on
566
+ * speech policy without touching the FE path.
567
+ *
568
+ * `clip_ids` need not be contiguous. They move as one block, keeping their
569
+ * relative order, which is the agent-harness `batch_move_video_clips` contract.
570
+ */
571
+ const moveVideoClipsByAnchorInputSchema = z.object({
572
+ clip_ids: clipIdsSchema.describe("Clips to move as one block, keeping their relative order. Need not be contiguous on the track."),
573
+ anchor: moveAnchorSchema,
574
+ on_anchored: movedClipAnchoredPolicySchema.describe("What happens to speeches anchored to the moved clips (required — see the policy doc)")
575
+ });
576
+ //#endregion
577
+ //#region src/editor/schemas/move-video-clips.ts
578
+ const moveVideoClipsInputSchema = z.object({ clips: z.array(z.object({
579
+ clip_id: clipIdSchema.describe("The video clip part ID to move"),
580
+ new_start_ms: timelineMsSchema.describe("New absolute start time in milliseconds on the timeline"),
581
+ new_track_id: z.string().min(1).optional().describe("Target track ID to move the clip to (optional)")
582
+ })).min(1).describe("List of video clips to move to new positions") });
583
+ //#endregion
584
+ //#region src/editor/schemas/replace-video-clip-content.ts
585
+ /**
586
+ * Replace the media backing existing video clips. The media import runs upstream
587
+ * (Director); its stable result — the new media id, intrinsic length, and the
588
+ * reset trim window — arrives materialized (see
589
+ * `results/phase-4-side-effect-payload-contract.md` §4). Director resets
590
+ * `play_in=0` / `play_out=media_duration_ms` and clears `speed_shift` on
591
+ * replacement. The clip `part_id`s (hence their track items) are unchanged; the
592
+ * editor reflows the main track from the new effective durations.
593
+ */
594
+ const replaceVideoClipContentInputSchema = z.object({ clips: z.array(z.object({
595
+ clip_id: clipIdSchema.describe("Existing video clip part ID to re-point"),
596
+ origin_media_id: mediaIdSchema.describe("The new media asset ID"),
597
+ media_duration_ms: positiveMsSchema.describe("The new media's intrinsic full length"),
598
+ play_in: timelineMsSchema.describe("Trim window start in the new media (usually 0)"),
599
+ play_out: positiveMsSchema.describe("Trim window end in the new media (usually = media_duration_ms)"),
600
+ volume: volumeSchema
601
+ })).min(1).describe("Video clips whose media is being replaced") });
602
+ //#endregion
603
+ //#region src/editor/schemas/replace-video-clip-sequence.ts
604
+ /**
605
+ * What happens to the speeches anchored to the clips being replaced.
606
+ *
607
+ * - `remap` re-anchors each surviving speech to the new clip in the SAME POSITION
608
+ * of the sequence, keeping its offset — old[i]'s children become new[i]'s
609
+ * children. An old clip with no counterpart (fewer new clips than old) has its
610
+ * subtree deleted, because there is nothing left to anchor to.
611
+ * - `cascade` deletes every anchored speech (and its captions) outright, like
612
+ * `deleteVideoClips`.
613
+ *
614
+ * **Required, with no default.** The two branches differ in whether the user's
615
+ * narration survives, and the agent tool that drives this op makes its
616
+ * `preserve_speeches` flag required for that reason. A default here would let a
617
+ * caller that forgot the field silently delete speech.
618
+ */
619
+ const anchoredReplacePolicySchema = z.enum(["remap", "cascade"]);
620
+ /**
621
+ * Replace a contiguous run of main-track clips with a new run.
622
+ *
623
+ * A composite of delete + insert that cannot be expressed as the two ops in
624
+ * sequence, because the anchored speeches have to survive *across* the swap: with
625
+ * `remap` they are re-anchored positionally, which needs both the old and the new
626
+ * ids in the same transaction (ADR 0009 — the cascade stays in the editor, callers
627
+ * never re-wire anchors themselves).
628
+ *
629
+ * Duration facts follow `addVideoClips`: `media_duration_ms` is the source's
630
+ * intrinsic length and the trim window defaults to the whole media. The effective
631
+ * timeline duration is derived by the projection, never an input.
632
+ *
633
+ * `media_id` is optional: omitting it creates a **deliberate empty placeholder
634
+ * clip** (`origin_media_id: ''`) — structure with no picture. This is the only op
635
+ * that can produce one, and it is authoritative state, unlike the gap fillers the
636
+ * read-side solve mints (which never enter the document).
637
+ */
638
+ const replaceVideoClipSequenceInputSchema = z.object({
639
+ old_clip_ids: clipIdsSchema.describe("The clips being replaced: a contiguous main-track run, listed in timeline order"),
640
+ new_clips: z.array(z.object({
641
+ media_id: mediaIdSchema.optional().describe("The replacement media asset ID. Omit to create an empty placeholder clip."),
642
+ media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
643
+ play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
644
+ play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)")
645
+ })).min(1).describe("The replacement clips, in the order they take on the track"),
646
+ on_anchored: anchoredReplacePolicySchema.describe("What happens to speeches anchored to the replaced clips (required — see the policy doc)")
647
+ }).superRefine((data, ctx) => {
648
+ for (let i = 0; i < data.new_clips.length; i++) {
649
+ const clip = data.new_clips[i];
650
+ const playIn = clip.play_in ?? 0;
651
+ const playOut = clip.play_out ?? clip.media_duration_ms;
652
+ if (playOut > clip.media_duration_ms) ctx.addIssue({
653
+ code: z.ZodIssueCode.custom,
654
+ message: `new_clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
655
+ path: [
656
+ "new_clips",
657
+ i,
658
+ "play_out"
659
+ ]
660
+ });
661
+ if (playIn >= playOut) ctx.addIssue({
662
+ code: z.ZodIssueCode.custom,
663
+ message: `new_clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
664
+ path: [
665
+ "new_clips",
666
+ i,
667
+ "play_in"
668
+ ]
669
+ });
670
+ }
671
+ });
672
+ //#endregion
673
+ //#region src/editor/schemas/set-bgm.ts
674
+ /**
675
+ * Set the document BGM. The media's stable result (storage key) arrives
676
+ * materialized from upstream (see
677
+ * `results/phase-4-side-effect-payload-contract.md` §3). The op upserts the bgm
678
+ * part and seats it on the bgm lane; its effective length is always the whole
679
+ * timeline, derived by the projection on read — so there is no `duration_ms`
680
+ * input or fact (RFC 02 / `reference/16` §0b). A `bgm_id` lets the op replace an
681
+ * existing bgm part by id.
682
+ */
683
+ const setBgmInputSchema = z.object({
684
+ bgm_id: z.string().min(1).describe("The bgm part ID to write"),
685
+ audio_storage_key: z.string().min(1),
686
+ origin_media_id: mediaIdSchema,
687
+ volume: volumeSchema
688
+ });
689
+ //#endregion
690
+ //#region src/editor/schemas/set-caption-style.ts
691
+ /**
692
+ * Set the caption visual style. GLOBAL by design: the style applies to every
693
+ * caption part in the document — it carries NO `caption_id`. This mirrors the FE,
694
+ * whose caption-style store (`caption-style.ts:persistCaptionStylePatch`) iterates
695
+ * ALL captions and writes the same normalized style to each; the product has a
696
+ * single document-wide caption style, not per-caption styling.
697
+ *
698
+ * Every field is optional and maps to a `CaptionStyle` attribute (snake_case
699
+ * IDL). A field present in the input is written to every caption; a field ABSENT
700
+ * from the input is left untouched on each caption (the editor merges the patch
701
+ * onto each caption's existing style — this is a value edit, not a full-style
702
+ * replace, so a partial patch such as "recolor only" does not wipe font size).
703
+ *
704
+ * Pure document edit, no cascade — captions keep their positions; only the style
705
+ * sub-map of each caption part changes.
706
+ */
707
+ const setCaptionStyleInputSchema = z.object({
708
+ font_id: z.string().min(1).optional().describe("Font ID referencing a font from the font library"),
709
+ font_size: z.number().positive().optional().describe("Font size in points"),
710
+ font_color: z.string().min(1).optional().describe("Font color as hex string, e.g. \"#FFFFFF\""),
711
+ font_weight: z.number().int().optional().describe("Numeric font weight, e.g. 400 or 700"),
712
+ entrance_animation: z.string().optional().describe("Entrance animation preset ID, e.g. \"fade\" or \"none\""),
713
+ entrance_animation_duration_ms: z.number().min(0).optional().describe("Entrance animation duration in ms"),
714
+ stroke_color: z.string().min(1).optional().describe("Outline/stroke color as hex string, e.g. \"#000000\""),
715
+ stroke_width: z.number().min(0).optional().describe("Outline/stroke width in pixels"),
716
+ position_x: z.number().optional().describe("Caption center X as a fraction (0.0 to 1.0)"),
717
+ position_y: z.number().optional().describe("Caption center Y as a fraction (0.0 to 1.0)")
718
+ }).describe("Document-wide caption style patch (no caption_id; applies to every caption)");
719
+ //#endregion
720
+ //#region src/editor/schemas/set-caption-visibility.ts
721
+ /**
722
+ * Toggle caption visibility (the caption track's `is_hidden` flag). Pure
723
+ * document edit, no cascade — captions keep their positions; only the lane's
724
+ * hidden flag changes.
725
+ */
726
+ const setCaptionVisibilityInputSchema = z.object({ is_hidden: z.boolean().describe("Whether the caption track is hidden") });
727
+ //#endregion
728
+ //#region src/editor/schemas/set-video-clip-speed-shift.ts
729
+ /**
730
+ * Set the playback speed of existing video clips. Per the speed-shift decision
731
+ * (`reference/16` §0): the op writes only the `speed_shift` fact — it does NOT
732
+ * store an effective `duration_ms` (projection derives it from the trim window /
733
+ * speed) and does NOT scale anchored speeches' relative offsets (offsets stay
734
+ * put; the cascade reflows absolute positions). A `null` speed_shift clears the
735
+ * speed back to original (1×).
736
+ */
737
+ const setVideoClipSpeedShiftInputSchema = z.object({ clips: z.array(z.object({
738
+ clip_id: clipIdSchema.describe("The video clip part ID to set speed for"),
739
+ speed_shift: speedShiftSchema.nullable().describe("The new speed setting, or null to reset to 1×")
740
+ })).min(1).describe("Video clips with their new speed settings") });
741
+ //#endregion
742
+ //#region src/editor/schemas/index.ts
743
+ var schemas_exports = /* @__PURE__ */ __exportAll({
744
+ addSpeechesInputSchema: () => addSpeechesInputSchema,
745
+ addVideoClipsInputSchema: () => addVideoClipsInputSchema,
746
+ adjustBgmVolumeInputSchema: () => adjustBgmVolumeInputSchema,
747
+ adjustSpeechVolumeInputSchema: () => adjustSpeechVolumeInputSchema,
748
+ adjustVideoClipDurationInputSchema: () => adjustVideoClipDurationInputSchema,
749
+ adjustVideoClipVolumeInputSchema: () => adjustVideoClipVolumeInputSchema,
750
+ anchoredDeletePolicySchema: () => anchoredDeletePolicySchema,
751
+ anchoredReplacePolicySchema: () => anchoredReplacePolicySchema,
752
+ changeSpeechScriptInputSchema: () => changeSpeechScriptInputSchema,
753
+ changeSpeechVoiceInputSchema: () => changeSpeechVoiceInputSchema,
754
+ clipIdSchema: () => clipIdSchema,
755
+ clipIdsSchema: () => clipIdsSchema,
756
+ deleteBgmInputSchema: () => deleteBgmInputSchema,
757
+ deleteSpeechesInputSchema: () => deleteSpeechesInputSchema,
758
+ deleteVideoClipsInputSchema: () => deleteVideoClipsInputSchema,
759
+ mediaIdSchema: () => mediaIdSchema,
760
+ moveAnchorSchema: () => moveAnchorSchema,
761
+ moveSpeechesInputSchema: () => moveSpeechesInputSchema,
762
+ moveVideoClipsByAnchorInputSchema: () => moveVideoClipsByAnchorInputSchema,
763
+ moveVideoClipsInputSchema: () => moveVideoClipsInputSchema,
764
+ movedClipAnchoredPolicySchema: () => movedClipAnchoredPolicySchema,
765
+ positiveMsSchema: () => positiveMsSchema,
766
+ replaceVideoClipContentInputSchema: () => replaceVideoClipContentInputSchema,
767
+ replaceVideoClipSequenceInputSchema: () => replaceVideoClipSequenceInputSchema,
768
+ setBgmInputSchema: () => setBgmInputSchema,
769
+ setCaptionStyleInputSchema: () => setCaptionStyleInputSchema,
770
+ setCaptionVisibilityInputSchema: () => setCaptionVisibilityInputSchema,
771
+ setVideoClipSpeedShiftInputSchema: () => setVideoClipSpeedShiftInputSchema,
772
+ speechAssetsSchema: () => speechAssetsSchema,
773
+ speechIdSchema: () => speechIdSchema,
774
+ speechIdsSchema: () => speechIdsSchema,
775
+ speedShiftSchema: () => speedShiftSchema,
776
+ timelineMsSchema: () => timelineMsSchema,
777
+ voiceSchema: () => voiceSchema,
778
+ volumeSchema: () => volumeSchema
779
+ });
780
+ //#endregion
159
781
  //#region src/editor/snapshot-utils.ts
160
782
  function isMap(value) {
161
783
  return value instanceof Map;
@@ -260,6 +882,27 @@ var SchemaValidator = class {
260
882
  const knownIds = this.mainTrackIds(doc);
261
883
  for (const clip of parsed.clips) if (!knownIds.has(clip.clip_id)) throw new ValidationError("move_clip_not_found", `clip_id "${clip.clip_id}" not present on main_track`, { clip_id: clip.clip_id });
262
884
  }
885
+ /**
886
+ * The anchor must not be one of the clips being moved. Unlike the schema's
887
+ * mutual exclusions this needs the input read as a whole, so it stays here.
888
+ *
889
+ * Rejected rather than normalized: "put this block before itself" has no
890
+ * defensible outcome — treating it as a no-op hides a caller bug behind a
891
+ * success, and picking any surviving neighbour invents an intent. The legacy
892
+ * `applyBatchMoveVideoClips` omits this check (a self-anchor there silently
893
+ * lands the block at its own pre-move index); director's original move tool
894
+ * did enforce it (`_validate_position`: "不是被移动的clips"), and that is the
895
+ * behaviour worth keeping.
896
+ */
897
+ validateMoveVideoClipsByAnchor(input, doc) {
898
+ const parsed = this.parse(moveVideoClipsByAnchorInputSchema, input, "move_by_anchor_invalid_input");
899
+ const knownIds = this.mainTrackIds(doc);
900
+ for (const clipId of parsed.clip_ids) if (!knownIds.has(clipId)) throw new ValidationError("move_by_anchor_clip_not_found", `clip_id "${clipId}" not present on main_track`, { clip_id: clipId });
901
+ if (parsed.anchor.position === "track_start") return;
902
+ const anchorId = parsed.anchor.clip_id;
903
+ if (!knownIds.has(anchorId)) throw new ValidationError("move_by_anchor_anchor_not_found", `anchor clip_id "${anchorId}" not present on main_track`, { anchor_clip_id: anchorId });
904
+ if (parsed.clip_ids.includes(anchorId)) throw new ValidationError("move_by_anchor_anchor_is_moved", `anchor clip_id "${anchorId}" is itself being moved`, { anchor_clip_id: anchorId });
905
+ }
263
906
  validateDeleteVideoClips(input, doc) {
264
907
  const parsed = this.parse(deleteVideoClipsInputSchema, input, "delete_invalid_input");
265
908
  const knownIds = this.mainTrackIds(doc);
@@ -294,6 +937,26 @@ var SchemaValidator = class {
294
937
  });
295
938
  }
296
939
  }
940
+ /**
941
+ * `old_clip_ids` must name a **contiguous run of the main track, in track
942
+ * order**. A sparse or reordered selection is rejected rather than normalized:
943
+ * a sparse selection has no single stretch to swap, so the insert index, the
944
+ * positional speech remap, and the resulting order would each need a different
945
+ * arbitrary choice. Mirrors the agent tool's own contract ("must be a
946
+ * contiguous main-track sequence listed in timeline order").
947
+ */
948
+ validateReplaceVideoClipSequence(input, doc) {
949
+ const parsed = this.parse(replaceVideoClipSequenceInputSchema, input, "replace_sequence_invalid_input");
950
+ const items = readMainTrackItems(doc.snapshot());
951
+ const indexById = new Map(items.map((it, index) => [it.part_id, index]));
952
+ const indices = [];
953
+ for (const clipId of parsed.old_clip_ids) {
954
+ const index = indexById.get(clipId);
955
+ if (index == null) throw new ValidationError("replace_sequence_clip_not_found", `clip_id "${clipId}" not present on main_track`, { clip_id: clipId });
956
+ indices.push(index);
957
+ }
958
+ for (let i = 1; i < indices.length; i++) if (indices[i] !== (indices[i - 1] ?? 0) + 1) throw new ValidationError("replace_sequence_not_contiguous", `old_clip_ids must be a contiguous main-track run in track order, got indices [${indices.join(", ")}]`, { indices });
959
+ }
297
960
  validateAdjustVideoClipDuration(input, doc) {
298
961
  const parsed = this.parse(adjustVideoClipDurationInputSchema, input, "adjust_duration_invalid_input");
299
962
  const snapshot = doc.snapshot();
@@ -463,11 +1126,9 @@ function readPartLibrary(draft) {
463
1126
  var SemanticEditor = class {
464
1127
  doc;
465
1128
  validator;
466
- idFactory;
467
- constructor(doc, validator = new SchemaValidator(), idFactory = generatePartId) {
1129
+ constructor(doc, validator = new SchemaValidator()) {
468
1130
  this.doc = doc;
469
1131
  this.validator = validator;
470
- this.idFactory = idFactory;
471
1132
  }
472
1133
  async moveVideoClips(input, options) {
473
1134
  this.validator.validateMoveVideoClips(input, this.doc);
@@ -486,6 +1147,44 @@ var SemanticEditor = class {
486
1147
  reparentSpeechesAfterMainTrackChange(draft, beforeRanges);
487
1148
  }, audit("MoveVideoClips", input, options));
488
1149
  }
1150
+ /**
1151
+ * Reorder main-track clips relative to an anchor clip, moving them as one block.
1152
+ *
1153
+ * The ordinal sibling of `moveVideoClips`: main-track clips are
1154
+ * `sequential`-positioned, so their order is the authoritative fact and absolute
1155
+ * time is derived on read (RFC 02 §4/§7). A caller that already knows "put these
1156
+ * after that one" should say so, instead of computing a timeline offset that this
1157
+ * editor would only have to resolve back into an index.
1158
+ *
1159
+ * `on_anchored` decides the speech treatment and is required. Note the asymmetry
1160
+ * in cost: `follow` writes nothing to the speeches (their `{ anchorPartId,
1161
+ * offsetMs }` facts stay valid and the derived absolute time moves with the host),
1162
+ * while `keep_absolute` runs the extra re-parent pass that preserves each
1163
+ * speech's absolute landing. `moveVideoClips` is permanently `keep_absolute`,
1164
+ * matching the FE timeline it serves.
1165
+ */
1166
+ async moveVideoClipsByAnchor(input, options) {
1167
+ this.validator.validateMoveVideoClipsByAnchor(input, this.doc);
1168
+ this.doc.transact((draft) => {
1169
+ const items = mainTrackItems(draft);
1170
+ if (items == null) return;
1171
+ const beforeRanges = mainTrackRanges(draft);
1172
+ const targets = new Set(input.clip_ids);
1173
+ const movedIndices = [];
1174
+ for (let i = 0; i < items.length; i++) {
1175
+ const partId = items[i]?.part_id;
1176
+ if (partId != null && targets.has(partId)) movedIndices.push(i);
1177
+ }
1178
+ if (movedIndices.length === 0) return;
1179
+ const insertIndex = anchorInsertIndex(items, input.anchor);
1180
+ const moved = movedIndices.map((index) => items[index]);
1181
+ for (let i = movedIndices.length - 1; i >= 0; i--) items.splice(movedIndices[i], 1);
1182
+ const removedBefore = movedIndices.filter((index) => index < insertIndex).length;
1183
+ items.splice(insertIndex - removedBefore, 0, ...moved);
1184
+ if (input.on_anchored === "keep_absolute") reparentSpeechesAfterMainTrackChange(draft, beforeRanges);
1185
+ else refreshAnchoredFallbackAbs(draft);
1186
+ }, audit("MoveVideoClipsByAnchor", input, options));
1187
+ }
489
1188
  async deleteVideoClips(input, options) {
490
1189
  this.validator.validateDeleteVideoClips(input, this.doc);
491
1190
  const targets = new Set(input.clip_ids);
@@ -509,7 +1208,7 @@ var SemanticEditor = class {
509
1208
  track.items ??= [];
510
1209
  let at = insertIndex;
511
1210
  for (const clip of input.clips) {
512
- const partId = this.idFactory("clip");
1211
+ const partId = generatePartId("clip");
513
1212
  setPart(draft, partId, { video_clip: {
514
1213
  id: partId,
515
1214
  kind: "video_clip",
@@ -527,6 +1226,65 @@ var SemanticEditor = class {
527
1226
  }
528
1227
  }, audit("AddVideoClips", input, options));
529
1228
  }
1229
+ /**
1230
+ * Swap a contiguous run of main-track clips for a new run, in one transaction.
1231
+ *
1232
+ * Composite by necessity, not convenience: with `on_anchored: 'remap'` each
1233
+ * surviving speech is re-anchored to the new clip in the **same position of the
1234
+ * sequence** (old[i] → new[i]), which needs both id sets live at once. Splitting
1235
+ * it into `deleteVideoClips` + `addVideoClips` would leave the caller holding the
1236
+ * anchor re-wiring — the cascade-in-the-caller mistake ADR 0009 retires.
1237
+ *
1238
+ * Positional pairing, not by count: when there are fewer new clips than old, the
1239
+ * unmatched old clips have no counterpart, so their anchored subtrees are deleted
1240
+ * (there is nothing to anchor to). When there are more new clips than old, the
1241
+ * extra ones simply arrive with no children.
1242
+ *
1243
+ * The new clips are inserted where the run started, so surrounding order is
1244
+ * preserved. Every position is derived on read (RFC 02 §7) — this writes only the
1245
+ * facts: track order, the trim windows, and the surviving anchors.
1246
+ */
1247
+ async replaceVideoClipSequence(input, options) {
1248
+ this.validator.validateReplaceVideoClipSequence(input, this.doc);
1249
+ this.doc.transact((draft) => {
1250
+ const track = mainTrackRow(draft);
1251
+ if (track?.items == null) return;
1252
+ const targets = new Set(input.old_clip_ids);
1253
+ const insertIndex = track.items.findIndex((item) => item?.part_id != null && targets.has(item.part_id));
1254
+ const at = insertIndex < 0 ? track.items.length : insertIndex;
1255
+ const newItems = [];
1256
+ const newClipIds = [];
1257
+ for (const clip of input.new_clips) {
1258
+ const partId = generatePartId("clip");
1259
+ setPart(draft, partId, { video_clip: {
1260
+ id: partId,
1261
+ kind: "video_clip",
1262
+ play_in: clip.play_in ?? 0,
1263
+ play_out: clip.play_out ?? clip.media_duration_ms,
1264
+ volume: 0,
1265
+ origin_media_id: clip.media_id ?? ""
1266
+ } });
1267
+ newItems.push({
1268
+ part_id: partId,
1269
+ time_position: { mode: "sequential" },
1270
+ fallback_abs_ms: void 0
1271
+ });
1272
+ newClipIds.push(partId);
1273
+ }
1274
+ if (input.on_anchored === "remap") for (let i = 0; i < input.old_clip_ids.length; i++) {
1275
+ const newClipId = newClipIds[i];
1276
+ if (newClipId == null) {
1277
+ deleteAnchoredSubtree(draft, input.old_clip_ids[i]);
1278
+ continue;
1279
+ }
1280
+ reanchorAnchoredChildren(draft, input.old_clip_ids[i], newClipId);
1281
+ }
1282
+ else for (const clipId of targets) deleteAnchoredSubtree(draft, clipId);
1283
+ track.items = track.items.filter((item) => item?.part_id == null || !targets.has(item.part_id));
1284
+ for (const clipId of targets) deletePart(draft, clipId);
1285
+ track.items.splice(at, 0, ...newItems);
1286
+ }, audit("ReplaceVideoClipSequence", input, options));
1287
+ }
530
1288
  async adjustVideoClipVolume(input, options) {
531
1289
  this.validator.validateAdjustVideoClipVolume(input, this.doc);
532
1290
  this.doc.transact((draft) => {
@@ -686,12 +1444,23 @@ var SemanticEditor = class {
686
1444
  /**
687
1445
  * Set the document BGM. The media's stable result arrives materialized.
688
1446
  *
689
- * KNOWN GAP (non-blocking): when the BGM comes from the public library, the
690
- * Director must also register project-level stock media ownership. That is a
691
- * separate "register-only, no draft write" side effect Director does not yet
692
- * expose; until it does, a public-library BGM set here will not auto-appear in
693
- * the project media library. The document edit itself is complete and correct.
694
- * See `docs/projects/medeo-integration/results/phase-4-op-side-effect-classification.md`.
1447
+ * KNOWN GAP (non-blocking, FE callers only): a public-library BGM also needs
1448
+ * project-level media ownership registered, or it plays but never appears in
1449
+ * the project's media library. This editor deliberately does not do it — the
1450
+ * document edit is complete and correct, and ownership is a side effect owned
1451
+ * by whoever holds the authoritative media data (memota), not by a CRDT write.
1452
+ *
1453
+ * Who is affected, as of 2026-08-13: the agent harness registers it on BOTH
1454
+ * its legacy and mengine paths (shared `resolveMutation` calls memota
1455
+ * `attachMedia` directly), and FE's legacy REST path gets it from Director.
1456
+ * Only FE's mengine path is missing it. FE cannot call memota directly:
1457
+ * `attachMedia` is exposed on memota's internal contract only, so the fix is a
1458
+ * Director proxy route — NOT a new Director business endpoint, since Director
1459
+ * stopped owning media ownership entirely (`a2951355`, 2026-08-05).
1460
+ *
1461
+ * Tracked as a Phase 7 gate (it must land before "new documents default to
1462
+ * mengine" makes public-library BGM a routine operation). See
1463
+ * `docs/projects/medeo-integration/results/phase-6-m4-legacy-refresh-isolation.md`.
695
1464
  */
696
1465
  async setBgm(input, options) {
697
1466
  this.validator.validateSetBgm(input, this.doc);
@@ -829,7 +1598,8 @@ function audit(kind, payload, options) {
829
1598
  return {
830
1599
  kind,
831
1600
  payload,
832
- intent: options?.intent ?? null
1601
+ intent: options?.intent ?? null,
1602
+ ...options?.actor ? { actor: options.actor } : {}
833
1603
  };
834
1604
  }
835
1605
  function mainTrackRow(draft) {
@@ -851,6 +1621,23 @@ function mainTrackItems(draft) {
851
1621
  track.items ??= [];
852
1622
  return track.items;
853
1623
  }
1624
+ /**
1625
+ * Resolve a `MoveAnchor` into an insertion slot in the **pre-extraction** item
1626
+ * list; the caller adjusts for items removed ahead of it.
1627
+ *
1628
+ * A missing anchor cannot reach here — `validateMoveVideoClipsByAnchor` rejects it
1629
+ * loudly first. This deliberately differs from `computeAddInsertIndex`, which
1630
+ * falls back to appending: adding clips with a stale anchor still has an obvious
1631
+ * intent (put them somewhere), while moving to a slot that no longer exists does
1632
+ * not. The `-1` guard stays as a defence-in-depth for a caller that bypassed
1633
+ * validation, and appending is the least destructive reading.
1634
+ */
1635
+ function anchorInsertIndex(items, anchor) {
1636
+ if (anchor.position === "track_start") return 0;
1637
+ const index = items.findIndex((item) => item?.part_id === anchor.clip_id);
1638
+ if (index < 0) return items.length;
1639
+ return anchor.position === "before" ? index : index + 1;
1640
+ }
854
1641
  function moveItem(items, from, to) {
855
1642
  if (from === to) return;
856
1643
  const [moved] = items.splice(from, 1);
@@ -883,9 +1670,16 @@ function normalizeSpeedShift(input) {
883
1670
  function writeSpeechAssets(draft, assets) {
884
1671
  const captionById = new Map(assets.captions.map((c) => [c.caption_id, c]));
885
1672
  const speechTrack = ensureLaneTrack(draft, "speech");
886
- const captionTrack = ensureLaneTrack(draft, "caption");
887
1673
  speechTrack.items ??= [];
888
- captionTrack.items ??= [];
1674
+ let captionItems;
1675
+ const ensureCaptionItems = () => {
1676
+ if (captionItems == null) {
1677
+ const track = ensureLaneTrack(draft, "caption");
1678
+ track.items ??= [];
1679
+ captionItems = track.items;
1680
+ }
1681
+ return captionItems;
1682
+ };
889
1683
  const ranges = mainTrackRanges(draft);
890
1684
  const hostStartMs = (anchorPartId) => ranges.find((r) => r.partId === anchorPartId)?.startMs;
891
1685
  for (const speech of assets.speeches) {
@@ -894,8 +1688,9 @@ function writeSpeechAssets(draft, assets) {
894
1688
  const removedCaptionIds = priorCaptionIds.filter((id) => id != null && !nextCaptionIds.has(id));
895
1689
  for (const captionId of removedCaptionIds) deletePart(draft, captionId);
896
1690
  if (removedCaptionIds.length > 0) {
1691
+ const lane = findLaneTrack(draft, "caption");
897
1692
  const removed = new Set(removedCaptionIds);
898
- captionTrack.items = captionTrack.items.filter((it) => it?.part_id == null || !removed.has(it.part_id));
1693
+ if (lane?.items != null) lane.items = lane.items.filter((it) => it?.part_id == null || !removed.has(it.part_id));
899
1694
  }
900
1695
  const priorSpeech = draft.part_library?.[speech.speech_id]?.speech;
901
1696
  setPart(draft, speech.speech_id, { speech: {
@@ -927,7 +1722,7 @@ function writeSpeechAssets(draft, assets) {
927
1722
  start_ms: caption.start_ms
928
1723
  } });
929
1724
  const captionAbs = speechAbs == null ? void 0 : speechAbs + caption.start_ms;
930
- placeRelative(captionTrack.items, captionId, speech.speech_id, caption.start_ms, captionAbs);
1725
+ placeRelative(ensureCaptionItems(), captionId, speech.speech_id, caption.start_ms, captionAbs);
931
1726
  }
932
1727
  }
933
1728
  }
@@ -1043,120 +1838,54 @@ function detachAnchoredChildren(draft, clipId) {
1043
1838
  item.fallback_abs_ms = void 0;
1044
1839
  }
1045
1840
  }
1046
- /** Delete every speech (with its captions) anchored to the given video clip (RFC 02 §9.2). */
1047
- function deleteAnchoredSubtree(draft, clipId) {
1048
- const anchored = (findLaneTrack(draft, "speech")?.items ?? []).filter((it) => it?.time_position?.mode === "anchored" && it.time_position.anchorPartId === clipId).map((it) => it.part_id).filter((id) => id != null);
1049
- for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
1050
- }
1051
- //#endregion
1052
- //#region src/editor/journal.ts
1053
- /**
1054
- * Wire a plain-memory adapter + editor that share a recording id factory, so
1055
- * every mutating transact lands in `journal` with ordered `generated_ids`.
1056
- */
1057
- function createEditSandbox(document, options) {
1058
- const adapter = createPlainMemoryAdapter(document, options);
1059
- return {
1060
- adapter,
1061
- editor: new SemanticEditor(adapter, new SchemaValidator(), adapter.idFactory),
1062
- journal: adapter.journal
1063
- };
1064
- }
1065
1841
  /**
1066
- * Re-drive `doc` from a recorded journal, forcing each entry's `generated_ids`
1067
- * through a queue-backed id factory. Never mints fresh ids: an empty queue on
1068
- * demand throws `unrecorded id`; leftover ids after an entry throws
1069
- * `unconsumed ids`. Legacy entries without `generated_ids` are treated as `[]`.
1842
+ * Re-anchor the direct anchored children of `fromClipId` onto `toClipId`, keeping
1843
+ * each child's `offsetMs` unchanged.
1844
+ *
1845
+ * "Same offset into the replacement" is the whole point: a speech that started
1846
+ * 100ms into the clip it narrated still starts 100ms into the clip that replaced
1847
+ * it, whatever the two clips' durations are. Absolute time is NOT preserved here
1848
+ * (that is `moveVideoClips`' rule, where the clip stays and the timeline reflows
1849
+ * around it) — the clip itself is being swapped out, so following it is what keeps
1850
+ * narration attached to the picture it describes.
1851
+ *
1852
+ * `fallback_abs_ms` is deliberately left alone: it is the orphan-recovery snapshot
1853
+ * (RFC 02 §11.1), refreshed by the projection on the next read. Writing a guess
1854
+ * here would make this op compete with the follow-the-clock refresh for the same
1855
+ * LWW unit.
1856
+ *
1857
+ * Only direct children move: captions anchor to their speech, so they follow it
1858
+ * without a rewrite.
1070
1859
  */
1071
- async function replayJournal(doc, journal) {
1072
- const queue = [];
1073
- const idFactory = (_prefix) => {
1074
- const id = queue.shift();
1075
- if (id == null) throw new Error("unrecorded id");
1076
- return id;
1077
- };
1078
- const editor = new SemanticEditor(doc, new SchemaValidator(), idFactory);
1079
- for (const entry of journal) {
1080
- queue.push(...entry.generated_ids ?? []);
1081
- await dispatchEntry(editor, entry);
1082
- if (queue.length > 0) throw new Error("unconsumed ids");
1860
+ function reanchorAnchoredChildren(draft, fromClipId, toClipId) {
1861
+ const speechTrack = findLaneTrack(draft, "speech");
1862
+ if (speechTrack?.items == null) return;
1863
+ for (const item of speechTrack.items) {
1864
+ const timePosition = item?.time_position;
1865
+ if (timePosition?.mode !== "anchored" || timePosition.anchorPartId !== fromClipId) continue;
1866
+ item.time_position = {
1867
+ ...timePosition,
1868
+ anchorPartId: toClipId
1869
+ };
1083
1870
  }
1084
1871
  }
1085
- /** Dispatch one journal entry to the matching editor method; payload is passed through. */
1086
- async function dispatchEntry(editor, entry) {
1087
- const payload = entry.payload;
1088
- const options = entry.intent != null ? { intent: entry.intent } : void 0;
1089
- switch (entry.kind) {
1090
- case "MoveVideoClips":
1091
- await editor.moveVideoClips(payload, options);
1092
- return;
1093
- case "DeleteVideoClips":
1094
- await editor.deleteVideoClips(payload, options);
1095
- return;
1096
- case "AddVideoClips":
1097
- await editor.addVideoClips(payload, options);
1098
- return;
1099
- case "AdjustVideoClipVolume":
1100
- await editor.adjustVideoClipVolume(payload, options);
1101
- return;
1102
- case "SetVideoClipSpeedShift":
1103
- await editor.setVideoClipSpeedShift(payload, options);
1104
- return;
1105
- case "ReplaceVideoClipContent":
1106
- await editor.replaceVideoClipContent(payload, options);
1107
- return;
1108
- case "AdjustVideoClipDuration":
1109
- await editor.adjustVideoClipDuration(payload, options);
1110
- return;
1111
- case "AddSpeeches":
1112
- await editor.addSpeeches(payload, options);
1113
- return;
1114
- case "DeleteSpeeches":
1115
- await editor.deleteSpeeches(payload, options);
1116
- return;
1117
- case "MoveSpeeches":
1118
- await editor.moveSpeeches(payload, options);
1119
- return;
1120
- case "ChangeSpeechScript":
1121
- await editor.changeSpeechScript(payload, options);
1122
- return;
1123
- case "ChangeSpeechVoice":
1124
- await editor.changeSpeechVoice(payload, options);
1125
- return;
1126
- case "AdjustSpeechVolume":
1127
- await editor.adjustSpeechVolume(payload, options);
1128
- return;
1129
- case "SetCaptionVisibility":
1130
- await editor.setCaptionVisibility(payload, options);
1131
- return;
1132
- case "SetCaptionStyle":
1133
- await editor.setCaptionStyle(payload, options);
1134
- return;
1135
- case "SetBgm":
1136
- await editor.setBgm(payload, options);
1137
- return;
1138
- case "DeleteBgm":
1139
- await editor.deleteBgm(payload, options);
1140
- return;
1141
- case "AdjustBgmVolume":
1142
- await editor.adjustBgmVolume(payload, options);
1143
- return;
1144
- default: {
1145
- const _exhaustive = entry.kind;
1146
- throw new Error(`replayJournal: unsupported kind ${String(_exhaustive)}`);
1147
- }
1148
- }
1872
+ /** Delete every speech (with its captions) anchored to the given video clip (RFC 02 §9.2). */
1873
+ function deleteAnchoredSubtree(draft, clipId) {
1874
+ const anchored = (findLaneTrack(draft, "speech")?.items ?? []).filter((it) => it?.time_position?.mode === "anchored" && it.time_position.anchorPartId === clipId).map((it) => it.part_id).filter((id) => id != null);
1875
+ for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
1149
1876
  }
1150
1877
  //#endregion
1151
1878
  //#region src/editor/types.ts
1152
1879
  /** Runtime list of the frozen, implemented kinds (for guards / introspection). */
1153
1880
  const IMPLEMENTED_SEMANTIC_OP_KINDS = [
1154
1881
  "MoveVideoClips",
1882
+ "MoveVideoClipsByAnchor",
1155
1883
  "DeleteVideoClips",
1156
1884
  "AddVideoClips",
1157
1885
  "AdjustVideoClipVolume",
1158
1886
  "SetVideoClipSpeedShift",
1159
1887
  "ReplaceVideoClipContent",
1888
+ "ReplaceVideoClipSequence",
1160
1889
  "AdjustVideoClipDuration",
1161
1890
  "AddSpeeches",
1162
1891
  "DeleteSpeeches",
@@ -1174,6 +1903,288 @@ function isImplementedSemanticOpKind(kind) {
1174
1903
  return IMPLEMENTED_SEMANTIC_OP_KINDS.includes(kind);
1175
1904
  }
1176
1905
  //#endregion
1906
+ //#region src/manual-sync/doc-version-mark.ts
1907
+ /**
1908
+ * Encode a {@link DocVersionMark} for storage or transport.
1909
+ *
1910
+ * The mark stays opaque across the round trip — the string is not a version
1911
+ * number and must not be compared, ordered, or parsed. Its only use is
1912
+ * {@link decodeDocVersionMark} followed by `hasChangedSince`.
1913
+ *
1914
+ * Callers that persist this should know the encoded length grows with the number
1915
+ * of peers that have ever written to the document (one counter each), and the FE
1916
+ * mints a fresh peer per page load. Still small in practice (a few hundred bytes
1917
+ * for dozens of peers), but it grows with document age rather than size; version
1918
+ * vector compaction is deferred to a later phase.
1919
+ */
1920
+ function encodeDocVersionMark(mark) {
1921
+ return bytesToBase64(mark.encoded);
1922
+ }
1923
+ /**
1924
+ * Rebuild a mark from {@link encodeDocVersionMark}'s output.
1925
+ *
1926
+ * Returns `undefined` for input this did not produce (a legacy integer version,
1927
+ * a truncated value, an empty string). That is the honest answer — "I cannot
1928
+ * establish what you last saw" — and callers should treat it as "no baseline"
1929
+ * rather than as "unchanged". Decoding does not validate the bytes as a version
1930
+ * vector; `hasChangedSince` reports "changed" for an undecodable mark, which is
1931
+ * the conservative direction.
1932
+ */
1933
+ function decodeDocVersionMark(encoded) {
1934
+ if (encoded === "") return void 0;
1935
+ try {
1936
+ return {
1937
+ __brand: "mengine-doc-version-mark",
1938
+ encoded: base64ToBytes(encoded)
1939
+ };
1940
+ } catch {
1941
+ return;
1942
+ }
1943
+ }
1944
+ //#endregion
1945
+ //#region src/manual-sync/version-coverage.ts
1946
+ /**
1947
+ * Version-vector coverage: "does `outer` contain everything in `inner`?"
1948
+ *
1949
+ * This one predicate answers three different questions in the manual-sync document, which
1950
+ * is why it is factored out rather than inlined three times:
1951
+ *
1952
+ * | question | call |
1953
+ * | ------------------------------------- | ------------------------------- |
1954
+ * | is there anything left to push? | `covers(watermark, localOplog)` |
1955
+ * | did someone else write concurrently? | `covers(localOplog, serverVV)` |
1956
+ * | has the doc moved since I last read? | `covers(seenVersion, localOplog)`|
1957
+ *
1958
+ * `VersionVector.compare` cannot be used for any of them: it returns `undefined`
1959
+ * for concurrent vectors, and concurrency is the NORMAL case here — the server
1960
+ * routinely holds peers the local doc has never seen, and after a collaborative
1961
+ * merge the local doc holds ops the watermark predates. Treating "concurrent" as
1962
+ * "not covered" is right for some of these and wrong for others, so the per-peer
1963
+ * counter check is the only formulation that stays correct for all three.
1964
+ *
1965
+ * Equality must NOT be used as a substitute either: once collaboration happens
1966
+ * the watermark legitimately *leads* the local doc (it carries other peers'
1967
+ * counters), so an equality test reports "still has ops to push" forever.
1968
+ */
1969
+ function covers(outer, inner) {
1970
+ for (const [peer, counter] of inner.toJSON()) if ((outer.get(peer) ?? 0) < counter) return false;
1971
+ return true;
1972
+ }
1973
+ //#endregion
1974
+ //#region src/manual-sync/manual-sync-doc.ts
1975
+ /**
1976
+ * Agent-facing document with explicit `pull()` / `push()` over one Loro
1977
+ * document, with no background sync (ADR 0015 D1–D4).
1978
+ *
1979
+ * It deliberately does NOT reuse `MengineDocSession`'s stack. That stack —
1980
+ * SSE + local `DocStorage` + `ClientServerSynchronizer` + `DocManager` — is
1981
+ * correct for a browser editor and actively wrong here:
1982
+ *
1983
+ * - Its retry backoff lands *outside* the tool-call lifetime, so a write can
1984
+ * settle seconds after the tool already told the LLM what happened.
1985
+ * - It has no durable local queue in the harness, so pending pushes die with the
1986
+ * process — that is lost data, reported as success.
1987
+ * - Agent semantics require the document to change only at points the agent can
1988
+ * name. If it converged on its own between tool calls, "what state was this
1989
+ * decision based on" would be unanswerable, and a remote change could land
1990
+ * mid-edit.
1991
+ *
1992
+ * What replaces the whole background job queue is one variable: the **watermark**,
1993
+ * the version the server has confirmed. `push()` exports `{mode:'update', from:
1994
+ * watermark}` and only advances it on a confirmed verdict, so a failed push is
1995
+ * retried implicitly — the next push carries both the failed ops and any new
1996
+ * ones, in one blob. No queue, no timer, no retry bookkeeping.
1997
+ *
1998
+ * Two non-obvious properties of that watermark, both verified against a real
1999
+ * server in the ADR 0015 spike:
2000
+ *
2001
+ * - It can legitimately *lead* the local document (it carries other peers'
2002
+ * counters). Exporting `from` a leading watermark does not error; the blob
2003
+ * correctly contains only the local peer's new ops. So a collaborative merge
2004
+ * does not force a pull before pushing.
2005
+ * - Therefore every emptiness/coverage test must be one-directional containment,
2006
+ * never equality. See {@link covers}.
2007
+ *
2008
+ * Lifecycle: one `LoroDoc` per document, shared across agent loops with a
2009
+ * refcount held by the caller's session registry. Rebuilding the doc per tool
2010
+ * call would mint a new peer each time and permanently inflate the document's
2011
+ * version vector for every future reader.
2012
+ *
2013
+ * Not thread-safe by design and it does not need to be: harness tool calls run
2014
+ * serially (`execToolCalls` is a `for` + `await`).
2015
+ */
2016
+ var ManualSyncDoc = class ManualSyncDoc {
2017
+ client;
2018
+ doc;
2019
+ adapter;
2020
+ editor;
2021
+ /**
2022
+ * The version the server is known to hold. Starts empty (nothing confirmed)
2023
+ * and only ever moves forward on a verdict that proves the server took our ops.
2024
+ */
2025
+ watermark = new VersionVector(/* @__PURE__ */ new Map());
2026
+ constructor(client, doc) {
2027
+ this.client = client;
2028
+ this.doc = doc;
2029
+ this.adapter = new MirrorVideoDocumentAdapter(doc);
2030
+ this.editor = new SemanticEditor(this.adapter);
2031
+ }
2032
+ /**
2033
+ * Open an existing server document.
2034
+ *
2035
+ * Fetches the snapshot up front rather than starting empty and converging: the
2036
+ * agent's first act is to read the document, so there is no useful state before
2037
+ * the snapshot lands. This also fails fast and loudly on a document that does
2038
+ * not exist, instead of `MengineDocSession.waitForContent()`'s behavior of
2039
+ * waiting forever for content that will never arrive.
2040
+ */
2041
+ static async open(options) {
2042
+ const doc = new LoroDoc();
2043
+ if (options.peerId != null) doc.setPeerId(options.peerId);
2044
+ const response = await options.client.fetchSnapshot();
2045
+ doc.import(base64ToBytes(response.snapshot));
2046
+ const manualSyncDoc = new ManualSyncDoc(options.client, doc);
2047
+ manualSyncDoc.watermark = manualSyncDoc.serverVVFrom(response.version.server_vv) ?? doc.oplogVersion();
2048
+ return manualSyncDoc;
2049
+ }
2050
+ /** Current document read model (authoritative shape). */
2051
+ snapshot() {
2052
+ return this.adapter.snapshot();
2053
+ }
2054
+ /**
2055
+ * The Loro peer this document writes as.
2056
+ *
2057
+ * Exposed because the peer is an externally-meaningful fact, not an internal
2058
+ * detail: it is the identity every op this document emits is attributed to, and
2059
+ * callers mint it under rules of their own (the harness reserves a range so a
2060
+ * peer id alone says "Agent wrote this"). Being able to read it back means those
2061
+ * rules can be verified against the live document rather than against whatever
2062
+ * was passed to the constructor.
2063
+ */
2064
+ editorPeerId() {
2065
+ return this.doc.peerIdStr;
2066
+ }
2067
+ /** Current document projected to the legacy `VideoDraft` read shape. */
2068
+ draft() {
2069
+ return fromVideoDocument(this.adapter.snapshot());
2070
+ }
2071
+ /**
2072
+ * Mark the document state the caller has just observed, for a later
2073
+ * {@link hasChangedSince}.
2074
+ *
2075
+ * This pair replaces the legacy integer-version comparison that
2076
+ * `DraftVersionDetector` used to tell the LLM "the draft was modified
2077
+ * externally, reload before editing". Under mengine `meta.version` never
2078
+ * changes, so that detector would go permanently silent; comparing version
2079
+ * vectors restores the same capability.
2080
+ *
2081
+ * Same capability, not a stronger one: like the legacy detector, this only
2082
+ * reports what changed between two moments the caller chose to sample.
2083
+ */
2084
+ versionMark() {
2085
+ return {
2086
+ __brand: "mengine-doc-version-mark",
2087
+ encoded: this.doc.oplogVersion().encode()
2088
+ };
2089
+ }
2090
+ /**
2091
+ * Has the document moved since `mark` was taken?
2092
+ *
2093
+ * Reports any advance, whoever caused it — including this document's own edits.
2094
+ * The caller decides what is interesting: a detector sampling once per turn is
2095
+ * asking "did anything happen", and its own edits legitimately count.
2096
+ */
2097
+ hasChangedSince(mark) {
2098
+ let previous;
2099
+ try {
2100
+ previous = VersionVector.decode(mark.encoded);
2101
+ } catch {
2102
+ return true;
2103
+ }
2104
+ return !covers(previous, this.doc.oplogVersion());
2105
+ }
2106
+ /**
2107
+ * Fetch and merge everything the server has that this document lacks.
2108
+ *
2109
+ * Must run *before* the editor on each tool call. `SemanticEditor` validates
2110
+ * against the local document, so editing a stale one validates against a world
2111
+ * that no longer exists: the ADR 0015 spike confirmed that without pull-first
2112
+ * an edit to a clip another writer had already deleted passes validation and is
2113
+ * accepted by the server. Pulling afterwards cannot undo that.
2114
+ *
2115
+ * A failure is returned, not thrown — a transient network blip must not make
2116
+ * the tool unusable (M2/A ruling; legacy tolerates read failures the same way).
2117
+ * The caller proceeds on a possibly-stale document knowingly.
2118
+ */
2119
+ async pull() {
2120
+ const before = this.doc.oplogVersion();
2121
+ try {
2122
+ const update = base64ToBytes((await this.client.sync(before.encode())).update);
2123
+ if (update.byteLength > 0) this.doc.import(update);
2124
+ return {
2125
+ ok: true,
2126
+ changed: !covers(before, this.doc.oplogVersion())
2127
+ };
2128
+ } catch (error) {
2129
+ return {
2130
+ ok: false,
2131
+ reason: "failed",
2132
+ error: error instanceof Error ? error : new Error(String(error))
2133
+ };
2134
+ }
2135
+ }
2136
+ /**
2137
+ * Push every local op the server has not confirmed, and report the verdict.
2138
+ *
2139
+ * The return value is the durability answer a tool needs before claiming
2140
+ * success: only `ack` / `duplicate` mean the server holds the ops. This is why
2141
+ * this class exists rather than an ack-waiter — with a direct call, "did it
2142
+ * land" is simply the result.
2143
+ *
2144
+ * `duplicate` counts as durable: the bytes added nothing *because* the server
2145
+ * already had them.
2146
+ */
2147
+ async push() {
2148
+ const localVersion = this.doc.oplogVersion();
2149
+ if (covers(this.watermark, localVersion)) return {
2150
+ kind: "nothing_to_push",
2151
+ collaborated: false
2152
+ };
2153
+ const update = this.doc.export({
2154
+ mode: "update",
2155
+ from: this.watermark
2156
+ });
2157
+ try {
2158
+ const response = await this.client.pushUpdate(update);
2159
+ const serverVV = this.serverVVFrom(response.version?.server_vv);
2160
+ if (serverVV != null) this.watermark = serverVV;
2161
+ return {
2162
+ kind: response.kind,
2163
+ updateSeq: response.update_seq ?? void 0,
2164
+ collaborated: serverVV != null && !covers(localVersion, serverVV)
2165
+ };
2166
+ } catch (error) {
2167
+ const failure = error instanceof Error ? error : new Error(String(error));
2168
+ const rejected = error instanceof MenginePushRejectedError;
2169
+ return {
2170
+ kind: rejected ? "rejected" : "failed",
2171
+ collaborated: false,
2172
+ code: rejected ? error.code : void 0,
2173
+ error: failure
2174
+ };
2175
+ }
2176
+ }
2177
+ /** Decode a wire `server_vv`, tolerating absence/corruption (never throws). */
2178
+ serverVVFrom(serverVV) {
2179
+ if (serverVV == null || serverVV === "") return void 0;
2180
+ try {
2181
+ return VersionVector.decode(base64ToBytes(serverVV));
2182
+ } catch {
2183
+ return;
2184
+ }
2185
+ }
2186
+ };
2187
+ //#endregion
1177
2188
  //#region src/storage/medeo-http-doc-storage.ts
1178
2189
  /**
1179
2190
  * SSE-backed {@link Connection} for the mengine-server document stream.
@@ -1336,10 +2347,49 @@ var MedeoHttpDocStorage = class {
1336
2347
  version: base64ToBytes(response.server_vv)
1337
2348
  };
1338
2349
  }
2350
+ /**
2351
+ * Forward one update to the server.
2352
+ *
2353
+ * The `DocStorage` contract returns `void`, so the server's verdict cannot be
2354
+ * the return value — it is published on {@link subscribePushOutcome} instead,
2355
+ * for BOTH outcomes and failures. That channel is what makes the push
2356
+ * observable; previously the verdict was read and dropped, so a `duplicate`
2357
+ * (bytes contributed nothing) was indistinguishable from a successful write.
2358
+ *
2359
+ * The error is still rethrown after being published: the synchronizer treats a
2360
+ * throw as "retry this cycle", and swallowing it here would strand the update.
2361
+ * Publishing is therefore additive observability, not error handling.
2362
+ */
1339
2363
  async pushDocUpdate(update, _origin) {
1340
2364
  this.assertDocId(update.docId);
1341
2365
  if (this.isReadonly || update.data.byteLength === 0) return;
1342
- await this.client.pushUpdate(update.data);
2366
+ try {
2367
+ const response = await this.client.pushUpdate(update.data);
2368
+ this.events.emit("pushOutcome", {
2369
+ kind: response.kind,
2370
+ updateSeq: response.update_seq ?? void 0,
2371
+ serverVV: decodeServerVV(response.version?.server_vv)
2372
+ });
2373
+ } catch (error) {
2374
+ const failure = error instanceof Error ? error : new Error(String(error));
2375
+ const rejected = error instanceof MenginePushRejectedError;
2376
+ this.events.emit("pushOutcome", {
2377
+ kind: rejected ? "rejected" : "failed",
2378
+ code: rejected ? error.code : void 0,
2379
+ error: failure
2380
+ });
2381
+ throw error;
2382
+ }
2383
+ }
2384
+ /**
2385
+ * Observe the server's verdict for every pushed update, including failures.
2386
+ *
2387
+ * This is the loud channel the `void`-returning `DocStorage.pushDocUpdate`
2388
+ * cannot express. Consumers that need "did my write land" (the agent's
2389
+ * tool-level ack wait) subscribe here.
2390
+ */
2391
+ subscribePushOutcome(callback) {
2392
+ return this.events.on("pushOutcome", callback);
1343
2393
  }
1344
2394
  async deleteDoc(_docId) {
1345
2395
  throw new Error("MedeoHttpDocStorage does not support deleteDoc");
@@ -1360,6 +2410,19 @@ var MedeoHttpDocStorage = class {
1360
2410
  });
1361
2411
  }
1362
2412
  };
2413
+ /**
2414
+ * Decode the wire `server_vv` into raw bytes, tolerating absence. A server that
2415
+ * omits the version block still yields a usable outcome (the kind is the useful
2416
+ * part); only the version-coverage wait degrades, so this must not throw.
2417
+ */
2418
+ function decodeServerVV(serverVV) {
2419
+ if (serverVV == null || serverVV === "") return void 0;
2420
+ try {
2421
+ return base64ToBytes(serverVV);
2422
+ } catch {
2423
+ return;
2424
+ }
2425
+ }
1363
2426
  //#endregion
1364
2427
  //#region src/storage/memory-doc-storage.ts
1365
2428
  /**
@@ -1445,6 +2508,36 @@ var MemoryDocStorage = class extends BaseDocStorage {
1445
2508
  //#endregion
1446
2509
  //#region src/session/mengine-doc-session.ts
1447
2510
  /**
2511
+ * Raised by {@link MengineDocSession.waitForServerAck} when the local edits it
2512
+ * was asked to confirm were not acknowledged as durable.
2513
+ *
2514
+ * Named `...AckFailed`, not `...AckTimeout`: two of the three `reason` values are
2515
+ * not timeouts, and the earlier name made callers reach for a retry-after-delay
2516
+ * that is wrong for `rejected`.
2517
+ *
2518
+ * The distinction the caller needs is "was it written": if this throws, treat the
2519
+ * write as NOT durable. `reason` says which failure it was, and `code` carries the
2520
+ * server's machine code when the push was rejected on its merits:
2521
+ *
2522
+ * - `timeout` — no verdict within the deadline. Ambiguous by nature: the push may
2523
+ * still land later. The caller should surface it as unconfirmed, not as "failed".
2524
+ * - `rejected` — the server refused. `missing_dependency` is retryable after
2525
+ * catch-up; `corrupt_update` never is.
2526
+ * - `failed` — transport/network failure; the server's answer is unknown.
2527
+ */
2528
+ var MengineAckFailedError = class extends Error {
2529
+ reason;
2530
+ code;
2531
+ cause;
2532
+ constructor(reason, code, cause, message) {
2533
+ super(message);
2534
+ this.reason = reason;
2535
+ this.code = code;
2536
+ this.cause = cause;
2537
+ this.name = "MengineAckFailedError";
2538
+ }
2539
+ };
2540
+ /**
1448
2541
  * A live editing session for one Medeo document — the single entry point clients
1449
2542
  * (FE draft driver, Agent, embeds) use to open, edit, and observe a document.
1450
2543
  *
@@ -1478,6 +2571,12 @@ var MengineDocSession = class {
1478
2571
  adapterValue = null;
1479
2572
  editorValue = null;
1480
2573
  started = false;
2574
+ /**
2575
+ * Latest server oplog version seen on a push outcome. Lets `waitForServerAck`
2576
+ * return without waiting when the server is already known to cover the local
2577
+ * doc (e.g. nothing was edited since the last confirmed push).
2578
+ */
2579
+ serverVVValue = null;
1481
2580
  constructor(options) {
1482
2581
  this.options = options;
1483
2582
  this.docId = options.docId;
@@ -1491,32 +2590,74 @@ var MengineDocSession = class {
1491
2590
  this.manager = new DocManager(this.local, this.synchronizer);
1492
2591
  }
1493
2592
  /**
1494
- * The editor for local edits. Each op method validates, writes, and commits
1495
- * itself as one SemanticOp (single commit carrying its audit message), so
1496
- * callers just call `session.editor.someOp(...)` there is no separate commit
1497
- * step. The committed change drives DocManager's local-update push.
2593
+ * Observe every push verdict, including `duplicate` and failures.
2594
+ *
2595
+ * Exposed so a host can log/meter the four outcomes (rfc/05 §3 asks for
2596
+ * explicit ack/rejected semantics). Most callers want the higher-level
2597
+ * {@link waitForServerAck} instead.
1498
2598
  */
1499
- get editor() {
1500
- if (this.editorValue == null) throw new Error("mengine doc session is not started");
1501
- return this.editorValue;
2599
+ subscribePushOutcome(callback) {
2600
+ return this.server.subscribePushOutcome(callback);
1502
2601
  }
1503
2602
  /**
1504
- * Opaque version token of the local oplog (base64 `VersionVector.encode`).
1505
- * Equality-comparable only: equal means no observed change (local or
1506
- * remote-arrived) since the token was taken. Throws when not started.
2603
+ * Resolve once the server has durably accepted the local document state as of
2604
+ * *now* the capability an Agent tool needs to answer "did my edit land?"
2605
+ * before reporting success.
2606
+ *
2607
+ * Call it right after the editor ops whose durability matters. It snapshots the
2608
+ * local doc's current version and resolves on the first push outcome whose
2609
+ * `serverVV` covers that version, i.e. the server log is at least as advanced as
2610
+ * the local doc was when this was called.
2611
+ *
2612
+ * Version coverage, not byte identity, is the acceptance test — for two reasons:
2613
+ * the ops may reach the server inside a merged blob rather than as the exact
2614
+ * bytes a push job carried, and a `duplicate` verdict then correctly satisfies
2615
+ * the wait (the bytes appended nothing *because* the server already had them).
2616
+ * A `rejected` / `failed` outcome rejects immediately with that reason rather
2617
+ * than burning the whole timeout, since neither will resolve by waiting.
2618
+ *
2619
+ * Returns early when the server is already known to be current, so a caller
2620
+ * with nothing outstanding does not block.
2621
+ *
2622
+ * @throws {MengineAckFailedError} on timeout, rejection, or transport failure.
1507
2623
  */
1508
- version() {
2624
+ async waitForServerAck(options = {}) {
1509
2625
  if (this.adapterValue == null) throw new Error("mengine doc session is not started");
1510
- return bytesToBase64(this.adapterValue.doc.oplogVersion().encode());
2626
+ const timeoutMs = options.timeoutMs ?? 15e3;
2627
+ const target = this.manager.connectDoc(this.docId).version();
2628
+ if (this.serverVVValue != null && serverCovers(this.serverVVValue, target)) return;
2629
+ await new Promise((resolve, reject) => {
2630
+ let off = () => {};
2631
+ let done = false;
2632
+ const settle = (error) => {
2633
+ if (done) return;
2634
+ done = true;
2635
+ clearTimeout(timer);
2636
+ off();
2637
+ if (error == null) resolve();
2638
+ else reject(error);
2639
+ };
2640
+ const timer = setTimeout(() => {
2641
+ settle(new MengineAckFailedError("timeout", void 0, void 0, `mengine did not acknowledge the update within ${timeoutMs}ms`));
2642
+ }, timeoutMs);
2643
+ off = this.server.subscribePushOutcome((outcome) => {
2644
+ if (outcome.kind === "rejected" || outcome.kind === "failed") {
2645
+ settle(new MengineAckFailedError(outcome.kind, outcome.code, outcome.error, outcome.error?.message ?? `mengine push ${outcome.kind}`));
2646
+ return;
2647
+ }
2648
+ if (outcome.serverVV != null && serverCovers(outcome.serverVV, target)) settle(void 0);
2649
+ });
2650
+ });
1511
2651
  }
1512
2652
  /**
1513
- * The live document adapter, exposed for journal replay (commit channel).
1514
- * Replaying through it still goes SemanticEditor Loro mengine-server
1515
- * no write bypass. Typed by the narrow interface on purpose.
2653
+ * The editor for local edits. Each op method validates, writes, and commits
2654
+ * itself as one SemanticOp (single commit carrying its audit message), so
2655
+ * callers just call `session.editor.someOp(...)` there is no separate commit
2656
+ * step. The committed change drives DocManager's local-update push.
1516
2657
  */
1517
- get documentAdapter() {
1518
- if (this.adapterValue == null) throw new Error("mengine doc session is not started");
1519
- return this.adapterValue;
2658
+ get editor() {
2659
+ if (this.editorValue == null) throw new Error("mengine doc session is not started");
2660
+ return this.editorValue;
1520
2661
  }
1521
2662
  /** Current document snapshot (read model). */
1522
2663
  snapshot() {
@@ -1555,6 +2696,9 @@ var MengineDocSession = class {
1555
2696
  });
1556
2697
  this.disposables.add(unsubscribe);
1557
2698
  this.disposables.add(this.manager.onDocStateChange(this.docId, (state) => this.events.emit("state", state)));
2699
+ this.disposables.add(this.server.subscribePushOutcome((outcome) => {
2700
+ if (outcome.serverVV != null) this.serverVVValue = outcome.serverVV;
2701
+ }));
1558
2702
  await this.waitForContent();
1559
2703
  return this.snapshot();
1560
2704
  }
@@ -1603,5 +2747,26 @@ var MengineDocSession = class {
1603
2747
  }
1604
2748
  }
1605
2749
  };
2750
+ /**
2751
+ * True when the *encoded* server version `serverVV` includes everything in
2752
+ * `target`. Thin decode wrapper over the shared {@link covers} predicate — an
2753
+ * undecodable blob is treated as "does not cover", so a corrupt server response
2754
+ * leaves a waiter waiting rather than falsely acking it.
2755
+ *
2756
+ * The coverage rule itself (per-peer counters, never `VersionVector.compare`,
2757
+ * never equality) lives in `manual-sync/version-coverage.ts` with the reasoning.
2758
+ * An earlier revision reimplemented it here with a near-identical comment block;
2759
+ * two copies of one rule is exactly the duplication this project has been bitten
2760
+ * by repeatedly.
2761
+ */
2762
+ function serverCovers(serverVV, target) {
2763
+ let server;
2764
+ try {
2765
+ server = VersionVector.decode(serverVV);
2766
+ } catch {
2767
+ return false;
2768
+ }
2769
+ return covers(server, target);
2770
+ }
1606
2771
  //#endregion
1607
- export { IMPLEMENTED_SEMANTIC_OP_KINDS, MedeoHttpDocStorage, MemoryDocStorage, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MirrorVideoDocumentAdapter, PlainMemoryAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createEditSandbox, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, createPlainMemoryAdapter, derivePositionFromAbs, effectiveVideoClipDurationMs, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, replayJournal, resolveAllSpeechOverlaps, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, speedOf, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema, writeVideoDocumentToDraft };
2772
+ export { IMPLEMENTED_SEMANTIC_OP_KINDS, LANE_KINDS_IN_STACK_ORDER, ManualSyncDoc, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, MirrorVideoDocumentAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, decodeDocVersionMark, derivePositionFromAbs, encodeDocVersionMark, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, laneTrackId, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, resolveAllSpeechOverlaps, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema };