rushes 0.2.2 → 0.3.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.
@@ -2,14 +2,15 @@ import { Hono } from "hono";
2
2
  import { streamSSE } from "hono/streaming";
3
3
  import { z } from "zod";
4
4
  import { RushesError, InvalidError, NotFoundError } from "../core/errors.js";
5
- import { addFile, addVariant, addVersion, ensureProjectIdOnce, lockPicture, resolveVideo, setShots, shotAt } from "../core/project.js";
5
+ import { addFile, addFormat, addVariant, addVersion, ensureProjectIdOnce, lockPicture, resolveCut, resolveVideo, setShots, shotAt } from "../core/project.js";
6
6
  import { addTake, editSection, setSections } from "../core/script.js";
7
- import { addNote, applyReply, applyUserEdit, filterNotes } from "../core/notes.js";
7
+ import { addNote, applyReply, applyUserEdit, checkNoteFormat, filterNotes } from "../core/notes.js";
8
+ import { FORMAT_ID_RE, MAX_FORMATS, isPictureSize, versionFormats } from "../core/formats.js";
8
9
  import { createBatch, latestBatch } from "../core/batches.js";
9
10
  import { exportFileName, notesMarkdown } from "../core/exportNotes.js";
10
11
  import { tabStates } from "../core/tabs.js";
11
12
  import { fromManifestPath, toManifestPath } from "../core/paths.js";
12
- import { probe } from "../core/media.js";
13
+ import { AUDIO_FORMATS, PROBE_TIMEOUT_MS, VIDEO_EXT, giveUpAfter, probe, probeVideo, videoGaveUp } from "../core/media.js";
13
14
  import { PROXY_PATH, ProxyJobs } from "./proxy.js";
14
15
  import { PeakJobs } from "./peaks.js";
15
16
  import { lstat, mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
@@ -17,13 +18,17 @@ import { randomUUID } from "node:crypto";
17
18
  import { existsSync } from "node:fs";
18
19
  import { basename, extname, join } from "node:path";
19
20
  import { fileURLToPath } from "node:url";
20
- import { GRAB_PATH, SCREENSHOT_PATH, contentDisposition, contentType, foundMediaFile, inside, isInlineSafeType, mediaSecurityHeaders, registeredMedia, sendFile, servableFile } from "./files.js";
21
+ import { GRAB_PATH, OUTSIDE_CUT_EXT, OUTSIDE_MEDIA_EXT, SCREENSHOT_PATH, contentDisposition, contentType, cutOnlyFiles, foundMediaFile, inside, isInlineSafeType, mediaSecurityHeaders, registeredMedia, sendFile, servableFile } from "./files.js";
21
22
  import { BRING_IN_MAX, FoundScanner, ffprobeDuration, foundAnnouncer } from "./found.js";
22
23
  import { candidatePaths, listAssets, fpsFor, screenshotName } from "./assets.js";
23
24
  import { osRevealer, osOpener, OPEN_SAFE_EXT } from "./reveal.js";
24
25
  import { LaneStageSchema, SectionStatusSchema, StageSchema, BoxSchema, FileKindSchema, MarkSchema, ProjectIdSchema, ShotSchema, LEVEL_MIN, LEVEL_MAX, LEVEL_STEP } from "../core/schema.js";
26
+ import { LABEL_MAX, oneLineOf } from "../core/labels.js";
27
+ import { byOf, logBookFor } from "./logbook.js";
28
+ import { broughtInEvent, cutEvent, fileEvent, formatEvent, lineEvent, lockEvent, notesSentEvent, picksEvent, repliesEvent, scriptEvent, takeEvent, variantEvent, } from "../core/logEvents.js";
29
+ import { LOG_AREAS, LOG_MAX, changeLogFileName, logText, recentChanges } from "../core/logText.js";
25
30
  import { defaultRunner, measureMix } from "./loudness.js";
26
- export const VERSION = "0.2.2";
31
+ export const VERSION = "0.3.0";
27
32
  async function body(c, schema) {
28
33
  let json;
29
34
  try {
@@ -38,6 +43,10 @@ async function body(c, schema) {
38
43
  return parsed.data;
39
44
  }
40
45
  const t = z.number().nonnegative();
46
+ // §21.3: a format id, "9x16" or "2.39x1". Capped like the schema's own id.
47
+ const formatId = z.string().max(16).regex(FORMAT_ID_RE);
48
+ /** The longest path a format may be registered under (FoundItem and FoundPathsBody use the same). */
49
+ const MAX_PATH = 1024;
41
50
  const NewNoteBody = z.object({
42
51
  stage: StageSchema,
43
52
  video: z.string().nullish(),
@@ -51,6 +60,7 @@ const NewNoteBody = z.object({
51
60
  box: BoxSchema.nullish(),
52
61
  grab: z.string().nullish(),
53
62
  marks: z.array(MarkSchema).max(4).optional(),
63
+ format: formatId.nullish(),
54
64
  });
55
65
  const UserEditBody = z.object({
56
66
  text: z.string().optional(),
@@ -61,6 +71,7 @@ const UserEditBody = z.object({
61
71
  tOut: t.nullable().optional(),
62
72
  marks: z.array(MarkSchema).max(4).optional(),
63
73
  status: z.enum(["todo", "done"]).optional(),
74
+ format: formatId.nullable().optional(),
64
75
  });
65
76
  const RepliesBody = z.object({
66
77
  replies: z
@@ -73,7 +84,27 @@ const RepliesBody = z.object({
73
84
  }))
74
85
  .min(1),
75
86
  });
76
- const VersionBody = z.object({ video: z.string().min(1), file: z.string().min(1), note: z.string().optional() });
87
+ // §22.4: `label` is a short label for the version list (R2: the limit is enforced here, not in the file).
88
+ const VersionBody = z.object({
89
+ video: z.string().min(1),
90
+ file: z.string().min(1),
91
+ note: z.string().optional(),
92
+ // Cleaned to one line first, so padding and invisible characters don't count, then counted in characters.
93
+ label: z
94
+ .string()
95
+ .transform(oneLineOf)
96
+ .refine((s) => Array.from(s).length <= LABEL_MAX, `label is ${LABEL_MAX} characters at most: put the detail in note`)
97
+ .optional(),
98
+ // §21.4: the other shapes of this same cut, registered in the same call. `file` is the main one.
99
+ formats: z.array(z.object({ file: z.string().min(1).max(MAX_PATH) })).max(MAX_FORMATS - 1).optional(),
100
+ });
101
+ // §21.4: `label` here is only a hint for the ratio's name ("2.39:1"), not a version's short label.
102
+ const FormatBody = z.object({
103
+ video: z.string().min(1).max(200).optional(),
104
+ version: z.string().min(1).max(64).optional(),
105
+ file: z.string().min(1).max(MAX_PATH),
106
+ label: z.string().trim().min(1).max(16).optional(),
107
+ });
77
108
  const VariantBody = z.object({
78
109
  stage: LaneStageSchema,
79
110
  lane: z.string().optional(),
@@ -107,6 +138,9 @@ const NoteQuery = z.object({
107
138
  status: z.enum(["todo", "done"]).optional(),
108
139
  batch: z.string().optional(),
109
140
  version: z.string().optional(),
141
+ // §21.4: a format's notes, with the all-format notes unless onlyThisFormat (R14: Picture only).
142
+ format: formatId.optional(),
143
+ onlyThisFormat: z.enum(["true", "false"]).optional(),
110
144
  });
111
145
  // ShotSchema's own limits (name trimmed 1-80 chars, tag at most 24) apply at the boundary; `n` is server-assigned.
112
146
  const ShotsBody = z.object({
@@ -117,7 +151,11 @@ const LockBody = z.object({ version: z.string().nullable() });
117
151
  const MixLoudnessBody = z.object({ lanes: z.array(LaneStageSchema).min(1) });
118
152
  const SettingsBody = z.object({ autoProxy: z.boolean() }).strict();
119
153
  // A time in seconds, 0 to 24 h: an empty value, NaN, Infinity or anything huge is a 400.
120
- const FrameQuery = z.object({ t: z.string().min(1).pipe(z.coerce.number().finite().min(0).max(86400)) });
154
+ const FrameQuery = z.object({
155
+ t: z.string().min(1).pipe(z.coerce.number().finite().min(0).max(86400)),
156
+ // R12: the format whose own file the frame is read from. The cut's primary when left out.
157
+ format: formatId.optional(),
158
+ });
121
159
  const GrabBody = z.object({
122
160
  video: z.string().regex(/^[a-z0-9][a-z0-9-]*$/),
123
161
  version: z.string().regex(/^v\d+$/),
@@ -125,6 +163,8 @@ const GrabBody = z.object({
125
163
  frame: z.number().int().nonnegative().max(10_000_000),
126
164
  /** PNG bytes, base64, with or without a data: prefix. */
127
165
  png: z.string().min(1),
166
+ // §21.5: the format on screen when it isn't the cut's primary. Must be one of the cut's shapes.
167
+ format: formatId.optional(),
128
168
  });
129
169
  const RevealBody = z.union([z.object({ path: z.string().min(1) }), z.object({ project: z.literal(true) })]);
130
170
  const OpenBody = z.object({ path: z.string().min(1) });
@@ -155,6 +195,27 @@ const FoundBringInBody = z.object({
155
195
  files: z.array(FoundItem).min(1).max(BRING_IN_MAX),
156
196
  film: z.string().min(1).max(200).optional(),
157
197
  });
198
+ // §22.7: the Change Log's routes. A line is made one line and cut to 160 characters by the server (R21).
199
+ // Ruling a: a query it can't use is a 400 that says what to send, never a quiet default.
200
+ const LIMIT_WORDS = `limit must be a whole number from 1 to ${LOG_MAX}`;
201
+ /** 2026-10-07, optionally with a time (minutes, seconds, fractions) and a zone (Z or ±hh[:]mm). */
202
+ const ISO_DATE = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?(Z|[+-]\d{2}:?\d{2})?)?$/;
203
+ const LogQuery = z.object({
204
+ limit: z.string({ error: LIMIT_WORDS }).regex(/^\d+$/, LIMIT_WORDS).pipe(z.coerce.number().int(LIMIT_WORDS).min(1, LIMIT_WORDS).max(LOG_MAX, LIMIT_WORDS)).optional(),
205
+ area: z.enum(LOG_AREAS, { error: `area must be one of ${LOG_AREAS.join(", ")}` }).optional(),
206
+ // ISO style only: Date.parse alone would read "0" or "1" as a year (M4).
207
+ since: z
208
+ .string()
209
+ .refine((s) => ISO_DATE.test(s) && Number.isFinite(Date.parse(s)), "since must be a date and time, e.g. 2026-10-07T09:00:00Z")
210
+ .optional(),
211
+ });
212
+ const LogLineBody = z.object({
213
+ text: z.string().max(2000),
214
+ area: z.enum(LOG_AREAS, { error: `area must be one of ${LOG_AREAS.join(", ")}` }).optional(),
215
+ video: z.string().min(1).max(200).nullish(),
216
+ version: z.string().min(1).max(64).nullish(),
217
+ ref: z.string().min(1).max(300).nullish(),
218
+ });
158
219
  // Big enough for a full-quality frame of a 4K original from the frame endpoint (§19.5), which
159
220
  // Picture posts here as the grab: 3840×2160 RGB is 24.9 MB before PNG compression.
160
221
  const MAX_GRAB_BYTES = 64 * 1024 * 1024;
@@ -181,6 +242,57 @@ export function createApp(store, opts = {}) {
181
242
  const jobs = opts.proxyJobs ?? new ProxyJobs(store);
182
243
  const peaks = opts.peakJobs ?? new PeakJobs(store, { run: jobs.run, available: () => jobs.available() });
183
244
  const found = opts.found ?? new FoundScanner({ store, probe: ffprobeDuration, announce: foundAnnouncer(store) });
245
+ // §22: the Change Log, shared with the found scanner (logBookFor keeps one per store).
246
+ const log = logBookFor(store);
247
+ const by = (c) => byOf(c.req.header("x-rushes-project"));
248
+ /** R17: writes exports/change-log-YYYY-MM-DD.md (a same-day export replaces it) and returns its path. Written as Export notes writes. */
249
+ async function exportChangeLog(now) {
250
+ const [project, script] = await Promise.all([store.read("project"), store.read("script")]);
251
+ const md = await log.markdown({ project, script }, now);
252
+ const name = changeLogFileName(now);
253
+ const dir = join(store.root, "exports");
254
+ await mkdir(dir, { recursive: true });
255
+ const tmp = join(dir, `.${name}.${process.pid}.${randomUUID()}.tmp`);
256
+ await writeFile(tmp, md, "utf8");
257
+ await rename(tmp, join(dir, name));
258
+ return `exports/${name}`;
259
+ }
260
+ const prober = opts.formatProbe ?? ((abs, signal) => probeVideo(abs, { signal }));
261
+ const proberTimeout = opts.formatProbeTimeoutMs ?? PROBE_TIMEOUT_MS;
262
+ /**
263
+ * Every read of a render (review I2): given up on like any ffprobe, so a stalled drive never holds
264
+ * a request, and stopped when the server closes or `stop` aborts.
265
+ */
266
+ const readVideo = (abs, stop) => jobs.tracked((closing) => {
267
+ const signal = stop ? AbortSignal.any([closing, stop]) : closing;
268
+ if (signal.aborted)
269
+ return Promise.resolve(videoGaveUp("aborted"));
270
+ return giveUpAfter(prober(abs, signal), { timeout: proberTimeout, signal, gaveUp: videoGaveUp });
271
+ });
272
+ const isPlainFile = (abs) => stat(abs).then((s) => s.isFile(), () => false);
273
+ /**
274
+ * §21.3/§21.6: a render's shape on screen, length and rate, or the refusal in the user's words.
275
+ * Only a plain file on disk reaches the probe, and a format must have a video extension; the
276
+ * cut's own file (`primary`) is judged as it is without formats, by what ffprobe makes of it.
277
+ * The size comes back untouched: addFormat judges it (a size that isn't a picture is its RushesError).
278
+ */
279
+ async function readRender(given, how = {}) {
280
+ const file = toManifestPath(store.root, given);
281
+ const abs = fromManifestPath(store.root, file);
282
+ if (!how.primary && !VIDEO_EXT.has(extname(file).toLowerCase().replace(/^\./, "")))
283
+ throw new RushesError(`${file} isn't a video file.`, 400, "not_video", { path: file });
284
+ if (!(await isPlainFile(abs)))
285
+ throw new RushesError(`File not found: ${file}`, 404, "missing_file", { path: file });
286
+ const r = await readVideo(abs, how.stop);
287
+ if (!r.ok) {
288
+ if (r.code === "no_ffprobe")
289
+ throw new RushesError("Registering a format needs ffprobe to read the ratio. Run `rushes doctor` for how to add it.", 501, "no_ffprobe");
290
+ if (r.code === "not_video")
291
+ throw new RushesError(`${file} isn't a video: ${r.reason}.`, 400, "not_video", { path: file });
292
+ throw new RushesError(`ffprobe couldn't read ${file}: ${r.reason}`, 422, "unreadable", { path: file });
293
+ }
294
+ return { file, width: r.width, height: r.height, duration: r.duration, fps: r.fps };
295
+ }
184
296
  const app = new Hono();
185
297
  // Every SSE client adds a change listener, so lift Node's default limit of ten.
186
298
  store.setMaxListeners(0);
@@ -311,7 +423,8 @@ export function createApp(store, opts = {}) {
311
423
  }
312
424
  if (!viaFound) {
313
425
  // §15.5: a registered file reached through a symlink out of the project is served only if it is media.
314
- const servable = await servableFile(store.root, file);
426
+ // Final review M2: a cut or format that resolves outside the project is served only as video or audio.
427
+ const servable = await servableFile(store.root, file, cutOnlyFiles(project, script).has(path) ? OUTSIDE_CUT_EXT : OUTSIDE_MEDIA_EXT);
315
428
  if (!servable)
316
429
  throw new NotFoundError("media", path);
317
430
  file = servable;
@@ -348,7 +461,16 @@ export function createApp(store, opts = {}) {
348
461
  throw new InvalidError("Frame grab must be a PNG");
349
462
  const project = await store.read("project");
350
463
  const fps = fpsFor(project, b.video, b.version);
351
- const name = screenshotName(b.video, b.version, b.frame, fps);
464
+ // §21.5: a grab of a format is named with its ratio; the format must be one of this cut's. The
465
+ // dashboard names only a non-primary format (Task 2 review M4: an older cut with formats but no
466
+ // stored size has no primary id), so a grab with none is the primary's, which carries its own
467
+ // ratio whenever the cut has other formats and the primary's shape is known.
468
+ const version = project.videos.find((v) => v.id === b.video)?.versions.find((v) => v.id === b.version);
469
+ const shapes = version ? versionFormats(version) : [];
470
+ if (b.format !== undefined && !shapes.some((f) => f.id === b.format))
471
+ throw new NotFoundError("format", b.format);
472
+ const primary = version && version.formats.length > 0 ? shapes.find((f) => f.primary)?.id ?? null : null;
473
+ const name = screenshotName(b.video, b.version, b.frame, fps, b.format ?? primary);
352
474
  const grab = `screenshots/${name}`;
353
475
  await mkdir(join(store.root, "screenshots"), { recursive: true });
354
476
  // Written to a temp name in the same directory, then renamed into place. Writing the
@@ -436,6 +558,7 @@ export function createApp(store, opts = {}) {
436
558
  const b = await body(c, AddFileBody);
437
559
  const file = toManifestPath(store.root, b.file);
438
560
  const { result } = await store.update("project", (p) => addFile(p, { ...b, file }));
561
+ await log.add(() => fileEvent(result), by(c));
439
562
  return c.json(result, 201);
440
563
  });
441
564
  app.post("/api/exports/notes", async (c) => {
@@ -455,8 +578,16 @@ export function createApp(store, opts = {}) {
455
578
  const tmp = join(dir, `.${name}.${process.pid}.${randomUUID()}.tmp`);
456
579
  await writeFile(tmp, md, "utf8");
457
580
  await rename(tmp, join(dir, name));
458
- return c.json({ path: `exports/${name}` }, 201);
581
+ // §22.7: Export notes also writes the change log, newest first, one heading per day. The notes
582
+ // are written by now, so a change log that can't be is left out (null), never a failed export.
583
+ const changeLog = await exportChangeLog(now).catch((e) => {
584
+ console.error(`Rushes: couldn't export the Change Log: ${e.message}`);
585
+ return null;
586
+ });
587
+ return c.json({ path: `exports/${name}`, changeLog }, 201);
459
588
  });
589
+ // §22.8: the drawer's Export as Markdown.
590
+ app.post("/api/exports/change-log", async (c) => c.json({ path: await exportChangeLog(new Date()) }, 201));
460
591
  app.post("/api/shutdown", (c) => {
461
592
  if (opts.onShutdown)
462
593
  setImmediate(opts.onShutdown);
@@ -467,13 +598,15 @@ export function createApp(store, opts = {}) {
467
598
  let streams = 0;
468
599
  app.get("/api/health", async (c) => {
469
600
  const [project, id] = await Promise.all([store.read("project"), getProjectId()]);
470
- const health = { ok: true, app: "rushes", version: VERSION, root: store.root, id, name: project.name };
601
+ // Ruling e: Change Log lines that couldn't be written are counted for `rushes doctor`, only when there are any.
602
+ const health = { ok: true, app: "rushes", version: VERSION, root: store.root, id, name: project.name, ...(log.failures ? { logFailures: log.failures } : {}) };
471
603
  return c.json(c.req.query("test") === "1" ? { ...health, sse: streams } : health);
472
604
  });
473
605
  app.get("/api/state", async (c) => {
474
606
  // §20.6: found is what the last scan left in memory; it never waits on a scan or a probe.
475
- const [project, script, notes, picks, batches, ffmpeg, foundSummary] = await Promise.all([
476
- store.read("project"), store.read("script"), store.read("notes"), store.read("picks"), store.read("batches"), jobs.available(), found.summary(),
607
+ // §22.8: the log's head is read-only, so a state request never writes (R6).
608
+ const [project, script, notes, picks, batches, ffmpeg, foundSummary, logHead] = await Promise.all([
609
+ store.read("project"), store.read("script"), store.read("notes"), store.read("picks"), store.read("batches"), jobs.available(), found.summary(), log.head(),
477
610
  ]);
478
611
  const tabs = tabStates(project, script, notes);
479
612
  // §19.5: why each cut may play badly (or null). Never waits on a probe: an unprobed cut is
@@ -482,7 +615,7 @@ export function createApp(store, opts = {}) {
482
615
  ...v,
483
616
  versions: await Promise.all(v.versions.map(async (ver) => ({ ...ver, proxyNeed: await jobs.needNow(fromManifestPath(store.root, ver.file)) }))),
484
617
  })));
485
- return c.json({ project: { ...project, videos }, script, notes, picks, batches, tabs, proxies: { ffmpeg, jobs: jobs.list() }, found: foundSummary });
618
+ return c.json({ project: { ...project, videos }, script, notes, picks, batches, tabs, proxies: { ffmpeg, jobs: jobs.list() }, found: foundSummary, log: logHead });
486
619
  });
487
620
  // ---- found: the project's other files (§20) ----
488
621
  app.get("/api/found", async (c) => {
@@ -508,6 +641,8 @@ export function createApp(store, opts = {}) {
508
641
  let skipKinds = kinds;
509
642
  if (b.include?.length) {
510
643
  const included = await found.bringIn(b.include, { film: b.film, origin: "include" });
644
+ if (included.added.length)
645
+ await log.add(() => broughtInEvent(included.added, null), by(c));
511
646
  // An included cut becomes the cut the scoring is matched to, and a project with no cut
512
647
  // had nothing to record against: settle again now that the cut exists.
513
648
  if (included.added.some((a) => a.kind === "cut"))
@@ -550,7 +685,10 @@ export function createApp(store, opts = {}) {
550
685
  });
551
686
  app.post("/api/found/bring-in", async (c) => {
552
687
  const b = await body(c, FoundBringInBody);
553
- return c.json(await found.bringIn(b.files, { film: b.film }));
688
+ const r = await found.bringIn(b.files, { film: b.film });
689
+ if (r.added.length)
690
+ await log.add(() => broughtInEvent(r.added, null), by(c));
691
+ return c.json(r);
554
692
  });
555
693
  app.post("/api/found/dismiss", async (c) => {
556
694
  const b = await body(c, FoundPathsBody);
@@ -572,48 +710,120 @@ export function createApp(store, opts = {}) {
572
710
  if (!q.success)
573
711
  throw new InvalidError("Query is invalid", q.error.issues);
574
712
  const { notes } = await store.read("notes");
575
- return c.json({ notes: filterNotes(notes, q.data) });
713
+ return c.json({ notes: filterNotes(notes, { ...q.data, onlyThisFormat: q.data.onlyThisFormat === "true" }) });
576
714
  });
577
715
  app.post("/api/notes", async (c) => {
578
716
  const b = await body(c, NewNoteBody);
579
717
  // The client never sends shot; the server stamps it from the version's shots.
580
718
  let shot = null;
581
- if (b.stage === "picture" && b.video && b.version && b.t != null) {
719
+ if (b.stage === "picture" || b.format != null) {
582
720
  const project = await store.read("project");
583
- const version = project.videos.find((v) => v.id === b.video)?.versions.find((v) => v.id === b.version);
584
- if (version)
585
- shot = shotAt(version.shots, b.t);
721
+ // §21.3: a format must be one of this cut's, and on a cut with formats a box belongs to one.
722
+ checkNoteFormat({ next: { stage: b.stage, video: b.video ?? null, version: b.version ?? null, format: b.format ?? null, box: b.box ?? null } }, project);
723
+ if (b.stage === "picture" && b.video && b.version && b.t != null) {
724
+ const version = project.videos.find((v) => v.id === b.video)?.versions.find((v) => v.id === b.version);
725
+ if (version)
726
+ shot = shotAt(version.shots, b.t);
727
+ }
586
728
  }
587
729
  const { result } = await store.update("notes", (f) => addNote(f, { ...b, shot, by: "user" }));
588
730
  return c.json({ note: result }, 201);
589
731
  });
590
732
  app.patch("/api/notes/:id", async (c) => {
591
733
  const b = await body(c, UserEditBody);
592
- const { result } = await store.update("notes", (f) => applyUserEdit(f, { id: c.req.param("id"), ...b }));
734
+ // §21.3 and R7: a change of format or box is checked against the note's cut as it is now.
735
+ const project = b.format !== undefined || b.box !== undefined ? await store.read("project") : null;
736
+ const check = project ? (next, prev) => checkNoteFormat({ next, prev, boxRedrawn: b.box !== undefined }, project) : undefined;
737
+ const { result } = await store.update("notes", (f) => applyUserEdit(f, { id: c.req.param("id"), ...b }, check));
593
738
  return c.json({ note: result });
594
739
  });
595
740
  app.post("/api/replies", async (c) => {
596
741
  const b = await body(c, RepliesBody);
597
742
  const { result } = await store.update("notes", (f) => b.replies.map((r) => applyReply(f, r)));
743
+ await log.add(() => repliesEvent(result), by(c));
598
744
  return c.json({ notes: result });
599
745
  });
600
746
  // ---- media ----
601
747
  app.post("/api/versions", async (c) => {
602
748
  const b = await body(c, VersionBody);
603
- const file = toManifestPath(store.root, b.file);
749
+ // §21.4 (R11): with formats, every file is read first and one refusal refuses the whole call.
750
+ // The refusal names the first refused file in the order given, never whichever read happened to
751
+ // finish first, so a call always gives the same message (CI run 37724786490). So a refusal stops
752
+ // only the reads after it (review M6): they can't change the answer. The ones before it run on,
753
+ // each bounded by its own give-up, since one of them may be the file to name.
754
+ let shapes = null;
755
+ if (b.formats?.length) {
756
+ const files = [b.file, ...b.formats.map((f) => f.file)];
757
+ const stops = files.map(() => new AbortController());
758
+ const settled = await Promise.allSettled(files.map((f, i) => readRender(f, { primary: i === 0, stop: stops[i].signal }).catch((e) => {
759
+ for (const later of stops.slice(i + 1))
760
+ later.abort();
761
+ throw e;
762
+ })));
763
+ // The first rejection in order is always a refusal of its own: only an earlier one stops a read.
764
+ const refused = settled.find((s) => s.status === "rejected");
765
+ if (refused)
766
+ throw refused.reason;
767
+ shapes = settled.map((s) => s.value);
768
+ const own = shapes[0];
769
+ if (!isPictureSize(own.width, own.height)) {
770
+ throw new RushesError(`${own.file} measures ${own.width}×${own.height}, which isn't a picture size, so Rushes can't tell its formats' shapes from it.`, 422, "no_primary_size", { path: own.file });
771
+ }
772
+ }
773
+ const file = shapes ? shapes[0].file : toManifestPath(store.root, b.file);
604
774
  const abs = fromManifestPath(store.root, file);
605
- // The proxy jobs' probe is ffprobe (tests inject a fake), so the cut's need is read from the same answer.
606
- const info = await jobs.probe(abs);
775
+ // The proxy jobs' probe is ffprobe (tests inject a fake), so the cut's need is read from the same
776
+ // answer. §21.3 (R2): the primary's shape on screen is stored too, when it is a file that can be read.
777
+ const readOwn = async () => {
778
+ if (!(await isPlainFile(abs)))
779
+ return null;
780
+ const r = await readVideo(abs);
781
+ // Final review I1: a file ffprobe reads as another kind of container (a playlist or a GIF
782
+ // named .mp4) is not a cut. Anything else it can't read comes in with no size, as before.
783
+ if (!r.ok && r.container)
784
+ throw new RushesError(`${file} isn't a video: ${r.reason}.`, 400, "not_video", { path: file });
785
+ return r.ok ? r : null;
786
+ };
787
+ const [read, info] = await Promise.all([shapes ? Promise.resolve(shapes[0]) : readOwn(), jobs.probe(abs)]);
788
+ // Only a real picture size is stored; a plain cut without one goes in with no size, as cuts did
789
+ // before formats (a formats call was refused above).
790
+ const size = read && isPictureSize(read.width, read.height) ? read : null;
791
+ const formatWarnings = [];
607
792
  let lockedVersion = null;
608
793
  let autoProxy = false;
794
+ let films = 1;
609
795
  const { result } = await store.update("project", (p) => {
610
- const out = addVersion(p, { video: b.video, file, note: b.note, duration: info.duration, fps: info.fps });
796
+ const out = addVersion(p, {
797
+ video: b.video, file, note: b.note, label: b.label, duration: info.duration ?? read?.duration ?? null, fps: info.fps ?? read?.fps ?? null,
798
+ width: size?.width ?? null, height: size?.height ?? null,
799
+ });
800
+ // Throwing here leaves project.json unwritten: the cut and its formats land together or not at all.
801
+ for (const s of shapes?.slice(1) ?? []) {
802
+ let added;
803
+ try {
804
+ added = addFormat(p, { video: out.video.id, version: out.version.id, ...s });
805
+ }
806
+ catch (e) {
807
+ // Final review M7: the version is new, so the shape it "already has" came from this same
808
+ // call. Name the two files; the version is never created, so never name it.
809
+ if (e instanceof RushesError && e.code === "same_ratio") {
810
+ throw new RushesError(`${s.file} has the same shape as ${e.detail.file} (${e.detail.label}). Register one render of each shape.`, 409, "same_ratio", { path: s.file, with: e.detail.file });
811
+ }
812
+ throw e;
813
+ }
814
+ if (added.warning)
815
+ formatWarnings.push(added.warning);
816
+ }
611
817
  lockedVersion = out.video.lockedVersion;
612
818
  autoProxy = p.autoProxy;
819
+ films = p.videos.length;
613
820
  if (info.fps && p.videos.length === 1 && p.videos[0].versions.length === 1)
614
821
  p.fps = info.fps;
615
822
  return out;
616
823
  });
824
+ // One write, one line (R4): a cut that comes with its other shapes says so on its own line
825
+ // ("v2 and its 9:16 added: …"), rather than a line per shape.
826
+ await log.add(() => cutEvent(films, result.video, result.version, result.version.formats.map((f) => f.label)), by(c));
617
827
  // §19.5: say when the cut is likely to play badly, and start its proxy straight away when the
618
828
  // project asks for that. Without ffmpeg, nothing is offered (needFor is null).
619
829
  // §19.9: its waveform is made in the background; nothing here waits for it.
@@ -626,9 +836,43 @@ export function createApp(store, opts = {}) {
626
836
  if (autoProxy && !jobs.isClosing)
627
837
  proxy.proxyJob = jobs.start(result.video.id, result.version.id);
628
838
  }
839
+ const warnings = formatWarnings.length ? { formatWarnings } : {};
629
840
  if (lockedVersion)
630
- return c.json({ ...result, ...proxy, warning: `Picture is locked at ${lockedVersion}` }, 201);
631
- return c.json({ ...result, ...proxy }, 201);
841
+ return c.json({ ...result, ...proxy, ...warnings, warning: `Picture is locked at ${lockedVersion}` }, 201);
842
+ return c.json({ ...result, ...proxy, ...warnings }, 201);
843
+ });
844
+ // ---- formats (§21.4) ----
845
+ app.post("/api/formats", async (c) => {
846
+ const b = await body(c, FormatBody);
847
+ const render = await readRender(b.file);
848
+ // The cut it joins, pinned now, and its own shape when it has none yet (a cut from before formats).
849
+ const cut = resolveCut(await store.read("project"), b.video, b.version);
850
+ let primarySize = null;
851
+ if (cut.version.width === null || cut.version.height === null) {
852
+ const own = fromManifestPath(store.root, cut.version.file);
853
+ const r = (await isPlainFile(own)) ? await readVideo(own) : null;
854
+ if (r?.ok && !isPictureSize(r.width, r.height)) {
855
+ throw new RushesError(`${cut.version.file} measures ${r.width}×${r.height}, which isn't a picture size, so Rushes can't tell a new shape from it.`, 422, "no_primary_size", { version: cut.version.id });
856
+ }
857
+ if (r?.ok)
858
+ primarySize = { width: r.width, height: r.height };
859
+ }
860
+ // Inside the update, so two registrations of one shape at once can't both land: the second
861
+ // sees the first's format and gets §21.3's sentence.
862
+ let films = 1;
863
+ const { result } = await store.update("project", (p) => {
864
+ films = p.videos.length;
865
+ return addFormat(p, { ...render, video: cut.video.id, version: cut.version.id, label: b.label, primarySize });
866
+ });
867
+ // §22.5: "9:16 added to v2".
868
+ await log.add(() => formatEvent(films, result.video, result.version.id, result.format), by(c));
869
+ return c.json({
870
+ video: { id: result.video.id, name: result.video.name },
871
+ version: result.version,
872
+ format: result.format,
873
+ ...(result.warning ? { warning: result.warning } : {}),
874
+ ...(result.labelNote ? { labelNote: result.labelNote } : {}),
875
+ }, 201);
632
876
  });
633
877
  // ---- proxies (§19.5) ----
634
878
  /** The cut a proxy or frame route names, or a 404. The video may be given by id, slug or name. */
@@ -697,15 +941,21 @@ export function createApp(store, opts = {}) {
697
941
  throw new InvalidError("Query is invalid: t must be a time in seconds", q.error.issues);
698
942
  await jobs.requireFfmpeg();
699
943
  const { project, version } = await cutOf(c.req.param("video"), c.req.param("version"));
700
- const orig = fromManifestPath(store.root, version.file);
944
+ // R12: Grab Frame on a format reads that format's own file. Frames are counted at the cut's fps.
945
+ const view = q.data.format ? versionFormats(version).find((f) => f.id === q.data.format) : undefined;
946
+ if (q.data.format && !view)
947
+ throw new NotFoundError("format", q.data.format);
948
+ const source = view && !view.primary ? view.file : version.file;
949
+ const orig = fromManifestPath(store.root, source);
701
950
  const exists = await stat(orig).then((s) => s.isFile(), () => false);
702
951
  if (!exists)
703
- throw new RushesError("The original file is missing", 404, "missing_file", { path: version.file });
952
+ throw new RushesError("The original file is missing", 404, "missing_file", { path: source });
704
953
  const fps = version.fps ?? project.fps;
705
954
  let frame = Math.round(q.data.t * fps);
706
955
  // A time at or past the end lands on the last frame rather than on nothing.
707
- if (version.duration !== null)
708
- frame = Math.min(frame, Math.max(0, Math.ceil(version.duration * fps - 1e-6) - 1));
956
+ const length = view?.duration ?? version.duration;
957
+ if (length !== null)
958
+ frame = Math.min(frame, Math.max(0, Math.ceil(length * fps - 1e-6) - 1));
709
959
  const png = await jobs.frame(orig, frame / fps);
710
960
  if (!png)
711
961
  throw new RushesError(`ffmpeg couldn't read frame ${frame}`, 422, "no_frame", { frame });
@@ -743,12 +993,22 @@ export function createApp(store, opts = {}) {
743
993
  });
744
994
  app.put("/api/videos/:video/lock", async (c) => {
745
995
  const b = await body(c, LockBody);
746
- const { result } = await store.update("project", (p) => lockPicture(p, c.req.param("video"), b.version));
996
+ let before = null;
997
+ let films = 1;
998
+ const { result } = await store.update("project", (p) => {
999
+ before = resolveVideo(p, c.req.param("video")).lockedVersion;
1000
+ films = p.videos.length;
1001
+ return lockPicture(p, c.req.param("video"), b.version);
1002
+ });
1003
+ // R10: a lock that changes nothing isn't logged.
1004
+ if (result.lockedVersion !== before)
1005
+ await log.add(() => lockEvent(films, result), by(c));
747
1006
  return c.json({ video: result });
748
1007
  });
749
1008
  app.post("/api/variants", async (c) => {
750
1009
  const b = await body(c, VariantBody);
751
1010
  const { result } = await store.update("project", (p) => addVariant(p, { ...b, file: toManifestPath(store.root, b.file) }));
1011
+ await log.add(() => variantEvent(result.lane, result.variant), by(c));
752
1012
  return c.json(result, 201);
753
1013
  });
754
1014
  // ---- script ----
@@ -760,6 +1020,7 @@ export function createApp(store, opts = {}) {
760
1020
  s.wordsPerSecond = b.wordsPerSecond;
761
1021
  return setSections(s, b.sections, { replace: b.replace });
762
1022
  });
1023
+ await log.add(() => scriptEvent(result.length), by(c));
763
1024
  return c.json({ sections: result });
764
1025
  });
765
1026
  app.patch("/api/script/:id", async (c) => {
@@ -770,8 +1031,12 @@ export function createApp(store, opts = {}) {
770
1031
  app.post("/api/script/:id/takes", async (c) => {
771
1032
  const b = await body(c, TakeBody);
772
1033
  const file = toManifestPath(store.root, b.file);
773
- const info = await probe(fromManifestPath(store.root, file));
774
- const { result } = await store.update("script", (s) => addTake(s, c.req.param("id"), { file, duration: info.duration }));
1034
+ const info = await probe(fromManifestPath(store.root, file), { formats: AUDIO_FORMATS });
1035
+ const { data, result } = await store.update("script", (s) => addTake(s, c.req.param("id"), { file, duration: info.duration }));
1036
+ await log.add(() => {
1037
+ const section = data.sections.find((s) => s.id === c.req.param("id"));
1038
+ return takeEvent(section, result, section.takes.findIndex((t) => t.id === result.id) + 1);
1039
+ }, by(c));
775
1040
  return c.json({ take: result }, 201);
776
1041
  });
777
1042
  // ---- picks ----
@@ -787,7 +1052,9 @@ export function createApp(store, opts = {}) {
787
1052
  into[k] = v;
788
1053
  }
789
1054
  };
1055
+ let before = {};
790
1056
  const { data } = await store.update("picks", (p) => {
1057
+ before = { ...p.lanes };
791
1058
  merge(p.lanes, b.lanes);
792
1059
  merge(p.sections, b.sections);
793
1060
  // §19.6: same merge rule as lanes/sections -- a number sets the level, null resets it to 0
@@ -802,6 +1069,16 @@ export function createApp(store, opts = {}) {
802
1069
  p.levels[stage] = v;
803
1070
  }
804
1071
  });
1072
+ // §22.5: a change of what's picked is logged; levels alone aren't (R10).
1073
+ const changed = Object.keys({ ...before, ...data.lanes }).some((k) => before[k] !== data.lanes[k]);
1074
+ if (changed) {
1075
+ // The names come from project.json; if it can't be read now, the pick still stands (ruling e).
1076
+ const project = await store.read("project").catch(() => null);
1077
+ if (project)
1078
+ await log.add(() => picksEvent(project, data.lanes), by(c));
1079
+ else
1080
+ log.failures += 1;
1081
+ }
805
1082
  return c.json(data);
806
1083
  });
807
1084
  // ---- mix loudness (§17.6) ----
@@ -820,7 +1097,15 @@ export function createApp(store, opts = {}) {
820
1097
  const run = batchQueue.catch(() => undefined).then(async () => {
821
1098
  const [project, script, batches] = await Promise.all([store.read("project"), store.read("script"), store.read("batches")]);
822
1099
  const { result } = await store.update("notes", (notes) => createBatch({ project, script, notes, batches }, stage));
1100
+ // R11: the prompt ends with the last five lines of the log, from before this send. A log that
1101
+ // can't be read adds nothing: the notes are already marked as sent, so the batch must be kept.
1102
+ const recent = await Promise.all([store.read("project"), store.read("script")])
1103
+ .then(async ([projectNow, scriptNow]) => recentChanges((await log.view({ limit: 5 }, { project: projectNow, script: scriptNow })).entries))
1104
+ .catch(() => "");
1105
+ if (recent)
1106
+ result.prompt = `${result.prompt}\n\n${recent}`;
823
1107
  await store.update("batches", (f) => { f.batches.push(result); });
1108
+ await log.add(() => notesSentEvent(result), by(c));
824
1109
  return result;
825
1110
  });
826
1111
  batchQueue = run;
@@ -838,6 +1123,57 @@ export function createApp(store, opts = {}) {
838
1123
  sections: script.sections.filter((s) => batch.sectionIds.includes(s.id)),
839
1124
  });
840
1125
  });
1126
+ // ---- the Change Log (§22.7) ----
1127
+ app.get("/api/log", async (c) => {
1128
+ const q = LogQuery.safeParse(c.req.query());
1129
+ if (!q.success)
1130
+ throw new InvalidError(q.error.issues.map((i) => i.message).join("; "), q.error.issues);
1131
+ const [project, script] = await Promise.all([store.read("project"), store.read("script")]);
1132
+ return c.json(await log.view(q.data, { project, script }));
1133
+ });
1134
+ app.post("/api/log", async (c) => {
1135
+ const b = await body(c, LogLineBody);
1136
+ // What's refused is named back on one clean line, never with the control characters it came with.
1137
+ const shown = (s) => Array.from(oneLineOf(s)).slice(0, 80).join("");
1138
+ const text = logText(b.text);
1139
+ if (!text)
1140
+ throw new InvalidError("text is empty: say what happened in one line");
1141
+ let video = null;
1142
+ let version = null;
1143
+ if (b.version && !b.video)
1144
+ throw new InvalidError("version needs video");
1145
+ // Read only when something must be checked against it, so a plain line never depends on project.json (M5).
1146
+ const project = b.video || b.ref ? await store.read("project") : null;
1147
+ if (project && b.video) {
1148
+ let v;
1149
+ try {
1150
+ v = resolveVideo(project, b.video);
1151
+ }
1152
+ catch {
1153
+ throw new NotFoundError("video", shown(b.video));
1154
+ }
1155
+ video = v.id;
1156
+ if (b.version) {
1157
+ if (!v.versions.some((x) => x.id === b.version))
1158
+ throw new NotFoundError("version", shown(b.version));
1159
+ version = b.version;
1160
+ }
1161
+ }
1162
+ // A jump goes only to a variant ("<lane>/<variant>") or a take ("<section>:<take>") that exists.
1163
+ if (project && b.ref) {
1164
+ const script = await store.read("script");
1165
+ const refs = new Set([
1166
+ ...project.lanes.flatMap((l) => l.variants.map((x) => `${l.id}/${x.id}`)),
1167
+ ...script.sections.flatMap((s) => s.takes.map((x) => `${s.id}:${x.id}`)),
1168
+ ]);
1169
+ if (!refs.has(b.ref))
1170
+ throw new NotFoundError("ref", shown(b.ref));
1171
+ }
1172
+ const entry = await log.add(() => lineEvent(text, b.area ?? "project", { video, version, ref: b.ref ?? null }), by(c));
1173
+ if (!entry)
1174
+ throw new RushesError("The Change Log couldn't be written. Run `rushes doctor`.", 500, "log_unwritable");
1175
+ return c.json({ entry }, 201);
1176
+ });
841
1177
  // ---- live updates ----
842
1178
  app.get("/api/events", (c) => streamSSE(c, async (stream) => {
843
1179
  const send = (e) => void stream.writeSSE({ event: "change", data: JSON.stringify(e) });