rushes 0.10.1 → 0.12.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.
Files changed (41) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +14 -4
  3. package/README.md +14 -8
  4. package/dist/cli/demo.js +3 -2
  5. package/dist/cli/main.js +33 -1
  6. package/dist/core/agentRules.js +7 -0
  7. package/dist/core/balance.js +238 -0
  8. package/dist/core/batches.js +29 -11
  9. package/dist/core/exportNotes.js +4 -2
  10. package/dist/core/films.js +114 -0
  11. package/dist/core/log.js +35 -3
  12. package/dist/core/logEvents.js +7 -7
  13. package/dist/core/notes.js +19 -1
  14. package/dist/core/schema.js +69 -14
  15. package/dist/core/script.js +67 -0
  16. package/dist/core/tabs.js +15 -7
  17. package/dist/core/takes.js +1 -1
  18. package/dist/mcp/tools.js +90 -23
  19. package/dist/server/agent/adapters.js +39 -8
  20. package/dist/server/agent/rules.js +106 -9
  21. package/dist/server/agent/runner.js +135 -35
  22. package/dist/server/agent/setupOnce.js +44 -0
  23. package/dist/server/app.js +348 -65
  24. package/dist/server/assets.js +40 -14
  25. package/dist/server/balance.js +103 -0
  26. package/dist/server/brief.js +9 -7
  27. package/dist/server/files.js +3 -2
  28. package/dist/server/films.js +43 -0
  29. package/dist/server/found.js +2 -1
  30. package/dist/server/logbook.js +28 -2
  31. package/dist/server/plans.js +68 -0
  32. package/dist/server/record.js +31 -13
  33. package/dist/server/start.js +15 -7
  34. package/dist/server/storyboard.js +8 -6
  35. package/dist/version.js +1 -1
  36. package/package.json +1 -1
  37. package/skills/rushes/SKILL.md +4 -4
  38. package/web-dist/assets/{index-BIJlv6zN.css → index-BCYA0Awp.css} +1 -1
  39. package/web-dist/assets/index-LPvsS2vO.js +70 -0
  40. package/web-dist/index.html +2 -2
  41. package/web-dist/assets/index-D430jOsg.js +0 -70
@@ -1,5 +1,8 @@
1
1
  import { readdir, stat } from "node:fs/promises";
2
2
  import { basename, extname, join } from "node:path";
3
+ import { scriptsOf } from "../core/script.js";
4
+ import { filmPlace } from "../core/films.js";
5
+ import { variantFilms } from "../core/schema.js";
3
6
  import { fromManifestPath } from "../core/paths.js";
4
7
  import { GRAB_PATH, SCREENSHOT_PATH, registeredMedia } from "./files.js";
5
8
  import { PROXY_PATH } from "./proxy.js";
@@ -141,14 +144,22 @@ async function dirFileNames(store, dir, exts) {
141
144
  * Hidden files, anything inside `.rushes/`, and anything in a deeper sub-folder never appear,
142
145
  * because this only ever reads one directory level.
143
146
  */
144
- async function discoveredLibraryPaths(store) {
147
+ async function discoveredLibraryPaths(store, project) {
145
148
  // One readdir of the project root serves both doc and caption (§16.2's two root extension
146
149
  // sets), rather than reading the same directory twice over just to apply a different filter.
147
150
  const [rootNames, exportNames] = await Promise.all([dirFileNames(store, "."), dirFileNames(store, "exports")]);
148
151
  const extOf = (n) => extname(n).toLowerCase().slice(1);
149
152
  const doc = rootNames.filter((n) => DOC_EXT.has(extOf(n)));
150
153
  const caption = rootNames.filter((n) => CAPTION_EXT.has(extOf(n)));
151
- return { doc, caption, export: exportNames.map((n) => `exports/${n}`) };
154
+ // §31.3: every film but the first keeps its own brief.md and storyboard.md (and any other doc) in films/<film-id>/.
155
+ const filmDocs = [];
156
+ for (const v of project?.videos.slice(1) ?? []) {
157
+ const place = filmPlace(project, v.id);
158
+ const dir = place.brief.slice(0, place.brief.lastIndexOf("/"));
159
+ for (const n of await dirFileNames(store, dir, DOC_EXT))
160
+ filmDocs.push({ path: `${dir}/${n}`, video: v.id });
161
+ }
162
+ return { doc, caption, export: exportNames.map((n) => `exports/${n}`), filmDocs };
152
163
  }
153
164
  /**
154
165
  * The library assets (doc, image, caption, export, delivery, edit), one kind at a time in
@@ -157,7 +168,7 @@ async function discoveredLibraryPaths(store) {
157
168
  * listed once, with the registered metadata (§16.2).
158
169
  */
159
170
  async function libraryAssets(store, project) {
160
- const discovered = await discoveredLibraryPaths(store);
171
+ const discovered = await discoveredLibraryPaths(store, project);
161
172
  const autoByKind = { doc: discovered.doc, caption: discovered.caption, export: discovered.export };
162
173
  const out = [];
163
174
  for (const kind of LIBRARY_KINDS) {
@@ -166,6 +177,10 @@ async function libraryAssets(store, project) {
166
177
  const registeredPaths = new Set(registered.map((f) => f.file));
167
178
  const auto = (autoByKind[kind] ?? []).filter((path) => !registeredPaths.has(path));
168
179
  out.push(...(await Promise.all(auto.map((path) => fileAsset(store, kind, path, {})))));
180
+ if (kind === "doc") {
181
+ const own = discovered.filmDocs.filter((d) => !registeredPaths.has(d.path));
182
+ out.push(...(await Promise.all(own.map((d) => fileAsset(store, "doc", d.path, { video: d.video })))));
183
+ }
169
184
  }
170
185
  return out;
171
186
  }
@@ -179,6 +194,7 @@ function variantEntries(project, stage) {
179
194
  label: variant.name,
180
195
  meta: Object.keys(variant.meta).length > 0 ? variant.meta : undefined,
181
196
  file: variant.file,
197
+ films: variantFilms(variant),
182
198
  })));
183
199
  }
184
200
  /**
@@ -188,7 +204,7 @@ function variantEntries(project, stage) {
188
204
  * order. Callers never need to sort this themselves. Every file this returns has been `stat`ed,
189
205
  * in parallel within each group, so a large project doesn't pay for it one file at a time.
190
206
  */
191
- export async function listAssets(store, project, script) {
207
+ export async function listAssets(store, project, script, visuals) {
192
208
  const cutEntries = project.videos.flatMap((video) => [...video.versions].reverse().flatMap((version) => {
193
209
  const views = versionFormats(version);
194
210
  const primary = views.find((f) => f.primary);
@@ -203,15 +219,24 @@ export async function listAssets(store, project, script) {
203
219
  // A take has no name of its own in script.json (just an id and the text it was read against),
204
220
  // so its label is its ordinal within the section -- "Take 1", "Take 2" -- rather than the file
205
221
  // name Download/Save-as still need under `name`.
206
- const takeEntries = script.sections.flatMap((section) => section.takes.map((take, i) => ({ section: section.id, file: take.file, label: `Take ${i + 1}` })));
207
- const [[freshNames, oldNames], cuts, proxies, takes, voice, music, sfx] = await Promise.all([
222
+ // §31.3: each take carries the film whose script it is in (a schema 1 script's are the first film's: no film named).
223
+ const takeEntries = ("sections" in script ? [{ video: null, script }] : scriptsOf(script)).flatMap(({ video, script: own }) => own.sections.flatMap((section) => section.takes.map((take, i) => ({ section: section.id, file: take.file, label: `Take ${i + 1}`, ...(video ? { video } : {}) }))));
224
+ // §31.2.3: each visual's pictures as it stands now (its newest version), every page and ratio: shared by every film.
225
+ const visualEntries = (visuals?.visuals ?? []).flatMap((v) => {
226
+ const now = v.versions[v.versions.length - 1];
227
+ const many = now.pages.length > 1;
228
+ return now.pages.flatMap((page) => page.images.map((img) => ({ file: img.file, label: `${v.name}${many ? ` · page ${page.n}` : ""} · ${labelOfId(img.ratio)}`, version: now.id, width: img.width, height: img.height })));
229
+ });
230
+ const filmsOf = (films) => (films.length === 1 ? { video: films[0] } : films.length > 1 ? { videos: films } : {});
231
+ const [[freshNames, oldNames], cuts, proxies, takes, voice, music, sfx, pictures] = await Promise.all([
208
232
  Promise.all([pngNames(store, SCREENSHOT_DIRS[0][0]), pngNames(store, SCREENSHOT_DIRS[1][0])]),
209
233
  Promise.all(cutEntries.map((e) => fileAsset(store, "cut", e.file, { video: e.video, version: e.version, format: e.format, formatLabel: e.formatLabel, width: e.width, height: e.height }))),
210
234
  Promise.all(proxyEntries.map((e) => fileAsset(store, "proxy", e.proxy.file, { video: e.video, version: e.version, width: e.proxy.width, height: e.proxy.height }))),
211
- Promise.all(takeEntries.map((e) => fileAsset(store, "take", e.file, { section: e.section, label: e.label }))),
212
- Promise.all(variantEntries(project, "voice").map((e) => fileAsset(store, "voice", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta }))),
213
- Promise.all(variantEntries(project, "music").map((e) => fileAsset(store, "music", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta }))),
214
- Promise.all(variantEntries(project, "sfx").map((e) => fileAsset(store, "sfx", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta }))),
235
+ Promise.all(takeEntries.map((e) => fileAsset(store, "take", e.file, { section: e.section, label: e.label, ...("video" in e ? { video: e.video } : {}) }))),
236
+ Promise.all(variantEntries(project, "voice").map((e) => fileAsset(store, "voice", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta, ...filmsOf(e.films) }))),
237
+ Promise.all(variantEntries(project, "music").map((e) => fileAsset(store, "music", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta, ...filmsOf(e.films) }))),
238
+ Promise.all(variantEntries(project, "sfx").map((e) => fileAsset(store, "sfx", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta, ...filmsOf(e.films) }))),
239
+ Promise.all(visualEntries.map((e) => fileAsset(store, "visual", e.file, { label: e.label, version: e.version, width: e.width, height: e.height, shared: true }))),
215
240
  ]);
216
241
  const [fresh, old] = await Promise.all([
217
242
  scanScreenshotDir(store, freshNames, SCREENSHOT_DIRS[0][1], SCREENSHOT_DIRS[0][2], project),
@@ -219,7 +244,7 @@ export async function listAssets(store, project, script) {
219
244
  ]);
220
245
  const screenshots = [...fresh, ...old].sort((a, b) => b.mtimeMs - a.mtimeMs).map((x) => x.asset);
221
246
  const library = await libraryAssets(store, project);
222
- return [...screenshots, ...cuts, ...proxies, ...takes, ...voice, ...music, ...sfx, ...library];
247
+ return [...screenshots, ...cuts, ...proxies, ...takes, ...voice, ...music, ...sfx, ...library, ...pictures];
223
248
  }
224
249
  /**
225
250
  * Every path the project knows, without stat'ing any of them: registered media (cuts, takes,
@@ -230,7 +255,8 @@ export async function listAssets(store, project, script) {
230
255
  * the one file they actually need instead of every asset in the project.
231
256
  */
232
257
  export async function candidatePaths(store, project, script) {
233
- const paths = registeredMedia(project, script);
258
+ // §31.2.3: a visual's pictures are listed in Assets too, so they can be revealed and opened from there.
259
+ const paths = registeredMedia(project, script, await store.read("visuals").catch(() => undefined));
234
260
  const nameLists = await Promise.all(SCREENSHOT_DIRS.map(([dir]) => pngNames(store, dir)));
235
261
  for (let i = 0; i < SCREENSHOT_DIRS.length; i++) {
236
262
  const [, manifestPrefix, safe] = SCREENSHOT_DIRS[i];
@@ -240,8 +266,8 @@ export async function candidatePaths(store, project, script) {
240
266
  paths.add(path);
241
267
  }
242
268
  }
243
- const discovered = await discoveredLibraryPaths(store);
244
- for (const path of [...discovered.doc, ...discovered.caption, ...discovered.export])
269
+ const discovered = await discoveredLibraryPaths(store, project);
270
+ for (const path of [...discovered.doc, ...discovered.caption, ...discovered.export, ...discovered.filmDocs.map((d) => d.path)])
245
271
  paths.add(path);
246
272
  return paths;
247
273
  }
@@ -0,0 +1,103 @@
1
+ // §29: auto-balance on the server. Reads each take in use (voice, music, effects) with ffmpeg's ebur128 -- the full
2
+ // band, and for voice and music the speech band too -- and hands the readings to the house rule (core/balance.ts).
3
+ import { stat } from "node:fs/promises";
4
+ import { existsSync } from "node:fs";
5
+ import { balance, parseFrames, SPEECH_BAND, trimmed } from "../core/balance.js";
6
+ import { AUDIO_FORMATS } from "../core/media.js";
7
+ import { fromManifestPath } from "../core/paths.js";
8
+ import { inUse, inUseVariant } from "../core/picks.js";
9
+ import { defaultRunner, ffmpegAvailable, loudnessArgs, LoudnessTimeoutError, parseEbur128 } from "./loudness.js";
10
+ /** The ffmpeg arguments that read one file's loudness over time, optionally through the speech band. */
11
+ export function readingArgs(file, band) {
12
+ const filter = `${band ? `highpass=f=${SPEECH_BAND.low},lowpass=f=${SPEECH_BAND.high},` : ""}ebur128`;
13
+ return ["-nostats", "-protocol_whitelist", "file", "-format_whitelist", AUDIO_FORMATS, "-i", file, "-af", filter, "-f", "null", "-"];
14
+ }
15
+ /** One ffmpeg run, killed (and a 504) past `timeoutMs`. */
16
+ async function timed(run, args, timeoutMs) {
17
+ const controller = new AbortController();
18
+ let timer;
19
+ const timeout = new Promise((_r, reject) => {
20
+ timer = setTimeout(() => {
21
+ controller.abort();
22
+ reject(new LoudnessTimeoutError(timeoutMs));
23
+ }, timeoutMs);
24
+ });
25
+ try {
26
+ return await Promise.race([run(args, controller.signal), timeout]);
27
+ }
28
+ finally {
29
+ clearTimeout(timer);
30
+ }
31
+ }
32
+ async function read(run, file, band, timeoutMs) {
33
+ const r = await timed(run, readingArgs(file, band), timeoutMs);
34
+ const { integrated } = parseEbur128(r.stderr);
35
+ return { integrated: integrated !== null && Number.isFinite(integrated) ? integrated : null, frames: parseFrames(r.stderr) };
36
+ }
37
+ // One reading per (file, mtime, band), per runner: the same take is read once however often the balance runs.
38
+ const CACHE_LIMIT = 60;
39
+ const caches = new WeakMap();
40
+ async function cachedRead(run, file, band, timeoutMs) {
41
+ let key = null;
42
+ try {
43
+ key = JSON.stringify([file, (await stat(file)).mtimeMs, band]);
44
+ }
45
+ catch {
46
+ key = null;
47
+ }
48
+ let cache = caches.get(run);
49
+ if (!cache)
50
+ caches.set(run, (cache = new Map()));
51
+ if (key !== null) {
52
+ const hit = cache.get(key);
53
+ if (hit)
54
+ return hit;
55
+ }
56
+ const reading = await read(run, file, band, timeoutMs);
57
+ if (key !== null && reading.frames.length > 0) {
58
+ cache.set(key, reading);
59
+ if (cache.size > CACHE_LIMIT)
60
+ cache.delete(cache.keys().next().value);
61
+ }
62
+ return reading;
63
+ }
64
+ /**
65
+ * The house balance for these files. Voice and music are read in the speech band too. Then the balanced mix is measured
66
+ * once, and brought down together if it would peak over BALANCE.peakCeiling.
67
+ */
68
+ export async function balanceFiles(files, run = defaultRunner, timeoutMs = 60_000) {
69
+ const takes = {};
70
+ await Promise.all(Object.entries(files).map(async ([stage, file]) => {
71
+ const banded = stage !== "sfx" && files.voice !== undefined && files.music !== undefined;
72
+ const [full, band] = await Promise.all([cachedRead(run, file, false, timeoutMs), banded ? cachedRead(run, file, true, timeoutMs) : undefined]);
73
+ takes[stage] = band ? { full, band } : { full };
74
+ }));
75
+ const b = balance(takes);
76
+ const inputs = Object.entries(files)
77
+ .filter(([stage]) => b.levels[stage] !== undefined)
78
+ .map(([stage, file]) => ({ file, offset: 0, stage, gainDb: b.levels[stage] }));
79
+ if (inputs.length === 0)
80
+ return b;
81
+ const { truePeak } = parseEbur128((await timed(run, loudnessArgs(inputs), timeoutMs)).stderr);
82
+ return trimmed(b, truePeak);
83
+ }
84
+ /** The take in use for each sound (as Mix plays them), whose file is on disk. */
85
+ export function takesInUse(project, picks, root) {
86
+ const use = inUse(project, picks);
87
+ const files = {};
88
+ for (const stage of ["voice", "music", "sfx"]) {
89
+ const taken = inUseVariant(project, use, stage);
90
+ if (!taken)
91
+ continue;
92
+ const abs = fromManifestPath(root, taken.variant.file);
93
+ if (existsSync(abs))
94
+ files[stage] = abs;
95
+ }
96
+ return files;
97
+ }
98
+ /** The house balance for a film's takes in use; `available: false` without ffmpeg. */
99
+ export async function balanceFilm(project, picks, root, run = defaultRunner, timeoutMs) {
100
+ if (!(await ffmpegAvailable(run)))
101
+ return { available: false };
102
+ return { available: true, ...(await balanceFiles(takesInUse(project, picks, root), run, timeoutMs)) };
103
+ }
@@ -1,17 +1,19 @@
1
1
  import { statusAfterEdit } from "../core/approval.js";
2
2
  import { BRIEF_FILE, hashText, parseBrief, setSectionBody, setStatus } from "../core/brief.js";
3
3
  import { InvalidError, RushesError } from "../core/errors.js";
4
+ import { filmPlace } from "../core/films.js";
4
5
  import { RecordFile } from "./record.js";
5
6
  // §24 step 2: brief.md is the file of record. See RecordFile for how it is kept.
6
7
  export class BriefChangedError extends RushesError {
7
- constructor() {
8
- super("brief.md changed since you loaded it. Showing the file as it is now.", 409, "brief_changed");
8
+ constructor(file = BRIEF_FILE) {
9
+ super(`${file} changed since you loaded it. Showing the file as it is now.`, 409, "brief_changed");
9
10
  }
10
11
  }
11
12
  export const NO_BRIEF = { exists: false, title: "", status: null, hash: "", sections: [], changed: 0 };
12
13
  export class BriefService extends RecordFile {
13
- constructor(store) {
14
- super(store, BRIEF_FILE, "brief", () => new BriefChangedError());
14
+ /** §31.3: `place` is the film's (filmPlace); left out, the root brief.md, the first film's. */
15
+ constructor(store, place = filmPlace({ videos: [] }, null)) {
16
+ super(store, place.brief, "brief", () => new BriefChangedError(place.brief), place.slot);
15
17
  }
16
18
  build(text, state) {
17
19
  if (text === null)
@@ -31,7 +33,7 @@ export class BriefService extends RecordFile {
31
33
  return { exists: true, title: yours.title, status: yours.status, hash, sections, changed: sections.filter((s) => s.changed).length };
32
34
  }
33
35
  view() {
34
- return this.run(async () => this.build(await this.readText(), await this.store.read("brief")));
36
+ return this.run(async () => this.build(await this.readText(), await this.state()));
35
37
  }
36
38
  /** Saves one section. A change to an approved brief's words reopens it: the status line goes back to draft. */
37
39
  setSection(index, heading, text, expectHash) {
@@ -50,7 +52,7 @@ export class BriefService extends RecordFile {
50
52
  next = setStatus(next, "draft");
51
53
  if (next !== prev)
52
54
  await this.write(prev, next);
53
- return this.build(next, await this.store.read("brief"));
55
+ return this.build(next, await this.state());
54
56
  });
55
57
  }
56
58
  /** Approves or reopens the brief by setting its status line. Only the dashboard calls this. */
@@ -60,7 +62,7 @@ export class BriefService extends RecordFile {
60
62
  const next = setStatus(prev, status);
61
63
  if (next !== prev)
62
64
  await this.write(prev, next);
63
- return this.build(next, await this.store.read("brief"));
65
+ return this.build(next, await this.state());
64
66
  });
65
67
  }
66
68
  }
@@ -2,6 +2,7 @@ import { createReadStream } from "node:fs";
2
2
  import { lstat, realpath, stat } from "node:fs/promises";
3
3
  import { extname, isAbsolute, join, normalize, sep } from "node:path";
4
4
  import { Readable } from "node:stream";
5
+ import { sectionsOf } from "../core/script.js";
5
6
  import { cueFiles } from "../core/cues.js";
6
7
  import { VIDEO_EXT } from "../core/media.js";
7
8
  import { PROXY_PATH } from "./proxy.js";
@@ -86,7 +87,7 @@ export function registeredMedia(project, script, visuals) {
86
87
  // §23: each cue's sample, when it is an audio file; served only because it is registered.
87
88
  for (const f of cueFiles(project))
88
89
  files.add(f);
89
- for (const s of script.sections)
90
+ for (const s of sectionsOf(script))
90
91
  for (const t of s.takes)
91
92
  files.add(t.file);
92
93
  for (const f of project.files)
@@ -143,7 +144,7 @@ export function cutOnlyFiles(project, script) {
143
144
  for (const l of project.lanes)
144
145
  for (const variant of l.variants)
145
146
  other.add(variant.file);
146
- for (const s of script.sections)
147
+ for (const s of sectionsOf(script))
147
148
  for (const t of s.takes)
148
149
  other.add(t.file);
149
150
  for (const f of project.files)
@@ -0,0 +1,43 @@
1
+ // §31.4: the one-time move. On opening a project with two or more films (and whenever a project gains its second
2
+ // film), whatever is on no film becomes the first film's, and one Activity line says what moved and where. It is
3
+ // idempotent: a second run finds nothing on no film and writes nothing. Visuals are never touched.
4
+ import { moveToFirstFilm, movedAnything, movedText } from "../core/films.js";
5
+ import { logBookFor } from "./logbook.js";
6
+ const runs = new WeakMap();
7
+ /** Runs the move once at a time per store. Null when there was nothing to do (fewer than two films, or nothing on no film). */
8
+ export function moveToFirstFilmOnce(store, log = logBookFor(store)) {
9
+ const prev = runs.get(store) ?? Promise.resolve(null);
10
+ const run = prev.catch(() => null).then(() => move(store, log));
11
+ runs.set(store, run);
12
+ return run;
13
+ }
14
+ async function move(store, log) {
15
+ const project = await store.read("project");
16
+ if (project.videos.length < 2)
17
+ return null;
18
+ const film = { id: project.videos[0].id, name: project.videos[0].name };
19
+ const count = project.videos.length;
20
+ // Each file is changed in its own write, and only when something in it is on no film, so a second run writes nothing.
21
+ const [notes, script] = await Promise.all([store.read("notes"), store.read("script")]);
22
+ const total = { film, notes: 0, takes: 0, files: 0, sections: 0, script: false };
23
+ if (notes.notes.some((n) => !n.video && n.stage !== "visuals")) {
24
+ const { result } = await store.update("notes", (d) => moveToFirstFilm({ notes: d }, film, count));
25
+ total.notes = result.notes;
26
+ }
27
+ if (project.lanes.some((l) => l.variants.some((v) => !v.videos?.length && v.video === undefined)) || project.files.some((f) => !f.video)) {
28
+ const { result } = await store.update("project", (d) => (d.videos[0]?.id === film.id ? moveToFirstFilm({ project: d }, film, d.videos.length) : total));
29
+ total.takes = result.takes;
30
+ total.files = result.files;
31
+ }
32
+ // An empty schema 1 script has nothing to move: it is written as schema 2 by its first change (core/script.ts).
33
+ if (script.schema === 1 && script.sections.length > 0) {
34
+ const { result } = await store.update("script", (d) => moveToFirstFilm({ script: d }, film, count));
35
+ total.sections = result.sections;
36
+ total.script = result.script;
37
+ }
38
+ if (!movedAnything(total))
39
+ return null;
40
+ // A project line (no film), so every film's Activity says where the things that were on no film went.
41
+ await log.add({ area: "project", kind: "film", merge: "once", text: movedText(total), video: null }, "rushes");
42
+ return total;
43
+ }
@@ -7,6 +7,7 @@ import { slugify } from "../core/ids.js";
7
7
  import { AUDIO_FORMATS, probe as ffprobe } from "../core/media.js";
8
8
  import { fromManifestPath, toManifestPath } from "../core/paths.js";
9
9
  import { addFile, addVariant, addVersion, inPlay, resolveVideo } from "../core/project.js";
10
+ import { sectionsOf } from "../core/script.js";
10
11
  import { cueFiles } from "../core/cues.js";
11
12
  import { logBookFor } from "./logbook.js";
12
13
  import { broughtInEvent, cutName, filmEvent } from "../core/logEvents.js";
@@ -251,7 +252,7 @@ export function registeredPaths(project, script) {
251
252
  out.push(f);
252
253
  for (const f of project.files)
253
254
  out.push(f.file);
254
- for (const s of script.sections)
255
+ for (const s of sectionsOf(script))
255
256
  for (const t of s.takes)
256
257
  out.push(t.file);
257
258
  return out;
@@ -4,7 +4,8 @@
4
4
  import { rename, stat } from "node:fs/promises";
5
5
  import { basename } from "node:path";
6
6
  import { CorruptFileError } from "../core/errors.js";
7
- import { appendEvent, backfillLog, logView } from "../core/log.js";
7
+ import { appendEvent, backfillLog, lineOnFilm, logView } from "../core/log.js";
8
+ import { allSections } from "../core/script.js";
8
9
  import { LOG_MAX, logMarkdown } from "../core/logText.js";
9
10
  /** R8: the dashboard sends its project id with every request (web/src/api.ts); MCP, the CLI and curl don't. */
10
11
  export function byOf(projectHeader) {
@@ -91,7 +92,7 @@ export class LogBook {
91
92
  refuseNewer(f);
92
93
  if (f.backfilled)
93
94
  throw UNCHANGED;
94
- backfillLog(f, { project, batches, script }, backfillBefore(f, this.since));
95
+ backfillLog(f, { project, batches, script: { sections: allSections(script) } }, backfillBefore(f, this.since));
95
96
  })
96
97
  .catch((e) => {
97
98
  if (e !== UNCHANGED)
@@ -200,6 +201,31 @@ export class LogBook {
200
201
  return null;
201
202
  }
202
203
  }
204
+ /**
205
+ * §31.5: each film's head, for the Activity dot on that film: its newest line among those Activity shows there
206
+ * (lineOnFilm), so a line about another film never lights it. `total` stays the whole log's. Read-only, like head.
207
+ */
208
+ async filmHeads(films) {
209
+ try {
210
+ const f = await this.read(false);
211
+ const out = {};
212
+ for (const v of films.videos) {
213
+ let mark = null;
214
+ for (let i = f.entries.length - 1; i >= 0; i--) {
215
+ const e = f.entries[i];
216
+ if (!lineOnFilm(e, v.id, films))
217
+ continue;
218
+ mark = `${e.id}@${e.at}`;
219
+ break;
220
+ }
221
+ out[v.id] = { rev: f.rev, mark, total: f.entries.length };
222
+ }
223
+ return out;
224
+ }
225
+ catch {
226
+ return {};
227
+ }
228
+ }
203
229
  /** §22.7: the whole log as Markdown, what Export writes. */
204
230
  async markdown(ctx, now) {
205
231
  const v = await this.view({ limit: LOG_MAX }, ctx);
@@ -0,0 +1,68 @@
1
+ // §31.3: every film has its own brief.md and storyboard.md. This keeps one service per film for each of them, made
2
+ // when first asked for, and (once started) watches every film's file for changes made outside the dashboard.
3
+ import { filmPlace } from "../core/films.js";
4
+ import { BriefService } from "./brief.js";
5
+ import { StoryboardService } from "./storyboard.js";
6
+ export class FilmRecords {
7
+ store;
8
+ make;
9
+ /** One service per film, keyed by its baseline's slot ("" for the first film, or the project with no film yet). */
10
+ services = new Map();
11
+ stops = new Map();
12
+ pollMs = null;
13
+ unlisten = null;
14
+ /** `root`: the service to use for the root file (the first film's), when one is already made. */
15
+ constructor(store, make, root) {
16
+ this.store = store;
17
+ this.make = make;
18
+ if (root)
19
+ this.services.set("", root);
20
+ }
21
+ /** The service for a film's file at `place`. Watched from the moment it is made, once start() has been called. */
22
+ at(place) {
23
+ const key = place.slot ?? "";
24
+ let s = this.services.get(key);
25
+ if (!s) {
26
+ s = this.make(place);
27
+ this.services.set(key, s);
28
+ }
29
+ if (this.pollMs !== null && !this.stops.has(key))
30
+ this.stops.set(key, s.start(this.pollMs));
31
+ return s;
32
+ }
33
+ /** The service for a film (an id); none named, the first film's (or, with no film yet, the project's). */
34
+ async of(video) {
35
+ return this.at(filmPlace(await this.store.read("project"), video));
36
+ }
37
+ /** The first film's, or with no film yet the project's: the root file. */
38
+ get root() {
39
+ return this.at(filmPlace({ videos: [] }, null));
40
+ }
41
+ /** Every film's service, the first film's first. */
42
+ async all() {
43
+ const project = await this.store.read("project").catch(() => null);
44
+ const places = project && project.videos.length > 0 ? project.videos.map((v) => filmPlace(project, v.id)) : [filmPlace({ videos: [] }, null)];
45
+ return places.map((p) => this.at(p));
46
+ }
47
+ /** Watches every film's file, and a film's as soon as it is made. Returns a stop function. */
48
+ start(pollMs = 500) {
49
+ this.pollMs = pollMs;
50
+ void this.all().catch(() => undefined);
51
+ const onChange = (e) => {
52
+ if (e.file === "project")
53
+ void this.all().catch(() => undefined);
54
+ };
55
+ this.store.on("change", onChange);
56
+ this.unlisten = () => this.store.off("change", onChange);
57
+ return () => {
58
+ this.pollMs = null;
59
+ this.unlisten?.();
60
+ this.unlisten = null;
61
+ for (const stop of this.stops.values())
62
+ stop();
63
+ this.stops.clear();
64
+ };
65
+ }
66
+ }
67
+ export const briefRecords = (store, root) => new FilmRecords(store, (place) => new BriefService(store, place), root);
68
+ export const storyboardRecords = (store, root) => new FilmRecords(store, (place) => new StoryboardService(store, place), root);
@@ -1,11 +1,13 @@
1
1
  import { constants } from "node:fs";
2
2
  import { lstat, open, rename, writeFile } from "node:fs/promises";
3
- import { join } from "node:path";
3
+ import { basename, dirname, join } from "node:path";
4
4
  import { hashText } from "../core/brief.js";
5
5
  import { InvalidError, NotFoundError } from "../core/errors.js";
6
6
  // §24 steps 2 and 3: brief.md and storyboard.md are files of record. The agent writes them with its own tools; the
7
7
  // dashboard writes them through a service built on this. Rushes keeps only `.rushes/<key>.json` for each: the agent's
8
8
  // last text (the "Current" column) and the hash it last saw, so it can tell the dashboard's writes from anyone else's.
9
+ // §31.3: each film has its own: the first film's are the root file and the top-level baseline, every other film's are
10
+ // films/<film-id>/<name> and the baseline kept under its id (core/films.ts filmPlace names them).
9
11
  /** A record is a page or two. Anything bigger than this is some other file, and is not read. */
10
12
  export const MAX_RECORD_BYTES = 1_000_000;
11
13
  export class RecordFile {
@@ -13,19 +15,33 @@ export class RecordFile {
13
15
  file;
14
16
  key;
15
17
  changed;
18
+ slot;
16
19
  path;
17
20
  queue = Promise.resolve();
18
21
  /**
19
- * @param file the file's name in the project folder's root
22
+ * @param file the file's path in the project folder, forward slashes ("brief.md", "films/five-views/brief.md")
20
23
  * @param key which `.rushes/*.json` holds its baseline and hash
21
24
  * @param changed the error a save gets when the file changed since the page loaded
25
+ * @param slot where in that file: null for the top-level fields (the first film's), else a film's id under `films`
22
26
  */
23
- constructor(store, file, key, changed) {
27
+ constructor(store, file, key, changed, slot = null) {
24
28
  this.store = store;
25
29
  this.file = file;
26
30
  this.key = key;
27
31
  this.changed = changed;
28
- this.path = join(store.root, file);
32
+ this.slot = slot;
33
+ this.path = join(store.root, ...file.split("/"));
34
+ }
35
+ /** This file's baseline and hash, as `.rushes/<key>.json` holds them now. */
36
+ async state() {
37
+ const d = await this.store.read(this.key);
38
+ return this.slot === null ? d : (d.films?.[this.slot] ?? { baseline: "", seen: "" });
39
+ }
40
+ /** The place in a draft of `.rushes/<key>.json` this file's baseline and hash are kept, made when missing. */
41
+ own(d) {
42
+ if (this.slot === null)
43
+ return d;
44
+ return ((d.films ??= {})[this.slot] ??= { baseline: "", seen: "" });
29
45
  }
30
46
  /** One operation at a time, so a save and a look at the file can never interleave. */
31
47
  run(fn) {
@@ -84,11 +100,12 @@ export class RecordFile {
84
100
  if (text === null)
85
101
  return;
86
102
  const hash = hashText(text);
87
- if ((await this.store.read(this.key)).seen === hash)
103
+ if ((await this.state()).seen === hash)
88
104
  return;
89
105
  await this.store.update(this.key, (d) => {
90
- d.baseline = text;
91
- d.seen = hash;
106
+ const o = this.own(d);
107
+ o.baseline = text;
108
+ o.seen = hash;
92
109
  });
93
110
  });
94
111
  }
@@ -97,23 +114,24 @@ export class RecordFile {
97
114
  const again = await this.readText();
98
115
  if (again === null || hashText(again) !== hashText(prev))
99
116
  throw this.changed();
100
- const tmp = join(this.store.root, `.${this.file}.${process.pid}.${Date.now()}.tmp`);
117
+ const tmp = join(dirname(this.path), `.${basename(this.path)}.${process.pid}.${Date.now()}.tmp`);
101
118
  await writeFile(tmp, next, "utf8");
102
119
  await rename(tmp, this.path);
103
120
  const prevHash = hashText(prev);
104
121
  const nextHash = hashText(next);
105
122
  // If Rushes had not yet seen `prev`, it is the agent's version: keep it as the baseline before the edit lands.
106
123
  await this.store.update(this.key, (d) => {
107
- if (d.seen !== prevHash)
108
- d.baseline = prev;
109
- d.seen = nextHash;
124
+ const o = this.own(d);
125
+ if (o.seen !== prevHash)
126
+ o.baseline = prev;
127
+ o.seen = nextHash;
110
128
  });
111
129
  }
112
130
  /** The file's text, once the page has proved it saw it: 404 with no file, 409 when it has changed since. */
113
131
  async current(expectHash) {
114
132
  const text = await this.readText();
115
133
  if (text === null)
116
- throw new NotFoundError(this.file === "brief.md" ? "Brief" : "Storyboard", this.file);
134
+ throw new NotFoundError(this.key === "brief" ? "Brief" : "Storyboard", this.file);
117
135
  if (hashText(text) !== expectHash)
118
136
  throw this.changed();
119
137
  if (!(await this.isUtf8()))
@@ -138,7 +156,7 @@ export class RecordFile {
138
156
  // seen, which announces it, and which makes the same text coming back (git stash pop, say) a change to announce too.
139
157
  if (hadFile && (await this.run(() => this.readText())) === null) {
140
158
  await this.store.update(this.key, (d) => {
141
- d.seen = "";
159
+ this.own(d).seen = "";
142
160
  });
143
161
  }
144
162
  }
@@ -11,8 +11,9 @@ import { ensureProjectIdOnce } from "../core/project.js";
11
11
  import { createApp } from "./app.js";
12
12
  import { removeLock, writeLock } from "./lock.js";
13
13
  import { watchStore } from "./watch.js";
14
- import { BriefService } from "./brief.js";
15
- import { StoryboardService } from "./storyboard.js";
14
+ import { briefRecords, storyboardRecords } from "./plans.js";
15
+ import { moveToFirstFilmOnce } from "./films.js";
16
+ import { adoptAgentDefaultsOnce } from "./agent/setupOnce.js";
16
17
  import { ProxyJobs, removePartials } from "./proxy.js";
17
18
  import { PeakJobs, removeOrphanPeaks, removePeakTemps } from "./peaks.js";
18
19
  import { FoundScanner, ffprobeDuration, foundAnnouncer } from "./found.js";
@@ -120,15 +121,18 @@ export async function startServer(rootDir, opts = {}) {
120
121
  const peakJobs = new PeakJobs(store, { available: () => proxyJobs.available(), ...opts.peaks });
121
122
  // §20: the project's other files. Its first scan starts once this server owns the project (below).
122
123
  const found = new FoundScanner({ store, probe: opts.found?.probe ?? ffprobeDuration, announce: foundAnnouncer(store), limits: opts.found?.limits });
123
- const brief = new BriefService(store);
124
- const storyboard = new StoryboardService(store);
124
+ // §31.3: every film's brief.md and storyboard.md.
125
+ const briefs = briefRecords(store);
126
+ const storyboards = storyboardRecords(store);
125
127
  // §28: one agent runner per server, on the root it serves, made before the app so every route has it.
126
128
  // Browser tests drive the fake agent through RUSHES_TEST_AGENT (ignored outside tests).
127
129
  const agentOpts = opts.agent ?? testAgentOptions();
128
130
  const runner = new AgentRunner(store, agentOpts);
129
131
  const appOpts = {
130
- brief, storyboard, webDir: opts.webDir, onShutdown: () => void close(), reveal: opts.reveal, proxyJobs, peakJobs, found, formatProbe: opts.formats?.probe,
132
+ briefs, storyboards, webDir: opts.webDir, onShutdown: () => void close(), reveal: opts.reveal, proxyJobs, peakJobs, found, formatProbe: opts.formats?.probe,
131
133
  agent: runner, detect: agentOpts?.detect, notify: opts.notify,
134
+ // §29: on for a real server; RUSHES_AUTO_BALANCE=0 turns it off (the browser tests, whose fixtures are test tones).
135
+ autoBalance: opts.autoBalance ?? process.env.RUSHES_AUTO_BALANCE !== "0",
132
136
  };
133
137
  const app = createApp(store, appOpts);
134
138
  const listener = getRequestListener(app.fetch);
@@ -183,8 +187,12 @@ export async function startServer(rootDir, opts = {}) {
183
187
  appOpts.projectId = id;
184
188
  const url = `http://${host}:${port}`;
185
189
  const stopWatching = await watchStore(store);
186
- const stopBrief = brief.start();
187
- const stopStoryboard = storyboard.start();
190
+ // §31.4: what was on no film in a project with several becomes the first film's, once, before anything is shown.
191
+ await moveToFirstFilmOnce(store).catch((e) => console.error(`Rushes: couldn't move what was on no film: ${e.message}`));
192
+ // §32: a project with no Send to AI rules of its own yet starts from the Mac-wide default.
193
+ await adoptAgentDefaultsOnce(store);
194
+ const stopBrief = briefs.start();
195
+ const stopStoryboard = storyboards.start();
188
196
  // §20.1: a first look through the folder on open. Never awaited, so it never delays start-up;
189
197
  // the tabs hear a change when it lands.
190
198
  // Then §20.4's current set comes in (a no-op without a cut, or when it's already in).