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