Merge M13.14 agent-interface audit + lazy open (worktree-agent-a5f8967ebd42ce98b)

The branch's commits say M13.14; on main that id is the puppet work, so this arc is M13.21.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-03 17:23:10 -04:00
co-authored by Claude Opus 5.5
38 changed files with 1838 additions and 117 deletions
+65 -7
View File
@@ -25,6 +25,12 @@ const EXT: &str = "ecproj";
/// Auto-save bookkeeping of a session.
#[derive(Clone, Debug, Default)]
pub struct AutoSaveState {
/// Serialise and write on a background thread (frontends with an event loop turn this on;
/// ignored on wasm32). The UI thread then only picks the slot and snapshots the project
/// (an `Arc` clone), so a large project's auto-save never stalls a frame.
pub background: bool,
/// The background write in flight, if any.
pub pending: Option<PendingWrite>,
/// Wall-clock seconds of the last auto-save (or of the first tick).
pub last: Option<f64>,
/// Slot written last in this session.
@@ -37,6 +43,37 @@ pub struct AutoSaveState {
pub sentinel: bool,
}
/// A background auto-save write ([`AutoSaveState::background`]).
#[derive(Clone)]
pub struct PendingWrite {
pub path: PathBuf,
handle: std::sync::Arc<std::sync::Mutex<Option<std::thread::JoinHandle<std::io::Result<()>>>>>,
}
impl std::fmt::Debug for PendingWrite {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PendingWrite").field("path", &self.path).finish()
}
}
impl PendingWrite {
pub fn spawn(path: PathBuf, work: impl FnOnce() -> std::io::Result<()> + Send + 'static) -> std::io::Result<PendingWrite> {
let h = std::thread::Builder::new().name("autosave".into()).spawn(work)?;
Ok(PendingWrite { path, handle: std::sync::Arc::new(std::sync::Mutex::new(Some(h))) })
}
pub fn is_finished(&self) -> bool {
self.handle.lock().map(|h| h.as_ref().is_none_or(|h| h.is_finished())).unwrap_or(true)
}
/// Wait for the write; its result (`Ok` if already collected).
pub fn join(&self) -> std::io::Result<()> {
let h = self.handle.lock().ok().and_then(|mut h| h.take());
match h {
Some(h) => h.join().unwrap_or_else(|_| Err(std::io::Error::other("auto-save thread panicked"))),
None => Ok(()),
}
}
}
/// File-name stem of a project (`/a/Intro.ecproj` → `Intro`; untitled → `Untitled Project`).
pub fn project_stem(path: Option<&str>) -> String {
path.and_then(|p| Path::new(p).file_stem()).map(|s| s.to_string_lossy().to_string()).unwrap_or_else(|| "Untitled Project".into())
@@ -103,20 +140,41 @@ pub fn write_in(
last_slot: Option<u32>,
json: &str,
) -> std::io::Result<(PathBuf, u32)> {
let plan = plan_in(fs, prefs, project, fallback, last_slot)?;
plan.write(fs, json.as_bytes())?;
Ok((plan.path, plan.slot))
}
/// Where the next auto-save goes ([`plan_in`]).
#[derive(Clone, Debug)]
pub struct WritePlan {
pub path: PathBuf,
pub slot: u32,
/// Slots above the (lowered) maximum, removed after the write.
pub stale: Vec<PathBuf>,
}
impl WritePlan {
pub fn write(&self, fs: &dyn FileOps, data: &[u8]) -> std::io::Result<()> {
fs.write(&self.path, data)?;
for p in &self.stale {
let _ = fs.remove(p);
}
Ok(())
}
}
/// Pick the next auto-save slot (without writing anything).
pub fn plan_in(fs: &dyn FileOps, prefs: &Prefs, project: Option<&str>, fallback: Option<&Path>, last_slot: Option<u32>) -> std::io::Result<WritePlan> {
let dir = folder(prefs, project, fallback).ok_or_else(|| std::io::Error::other("no auto-save folder for an untitled project"))?;
let stem = project_stem(project);
let max = prefs.auto_save.max_versions.max(1);
let have = existing_in(fs, &dir, &stem);
let slot = next_slot(&have, last_slot, max);
let path = dir.join(file_name(&stem, slot));
fs.write(&path, json.as_bytes())?;
// Max versions lowered: drop slots above it.
for (n, p, _) in have {
if n > max {
let _ = fs.remove(&p);
}
}
Ok((path, slot))
let stale = have.into_iter().filter(|(n, _, _)| *n > max).map(|(_, p, _)| p).collect();
Ok(WritePlan { path, slot, stale })
}
/// The newest auto-save of a project, if any.
+6 -2
View File
@@ -358,8 +358,12 @@ fn list(_: &mut Session, p: &Value) -> Result<Value> {
let filter = str_p(p, "filter").map(str::to_ascii_lowercase);
let v: Vec<Value> = effectcraft_effects::all()
.into_iter()
.filter(|e| filter.as_ref().is_none_or(|f| e.name.to_ascii_lowercase().contains(f) || e.id.contains(f.as_str())))
.map(|e| json!({"id": e.id, "name": e.name, "category": e.category, "params": e.params.iter().map(|p| json!({"id": p.id, "name": p.name, "default": p.default.to_json()})).collect::<Vec<_>>()}))
.filter(|e| {
filter.as_ref().is_none_or(|f| e.name.to_ascii_lowercase().contains(f) || e.id.contains(f.as_str()) || e.category.to_ascii_lowercase().contains(f))
})
.map(|e| {
json!({"id": e.id, "name": e.name, "category": e.category, "gpu": e.gpu, "float": e.float, "params": e.params.iter().map(|p| json!({"id": p.id, "name": p.name, "default": p.default.to_json()})).collect::<Vec<_>>()})
})
.collect();
Ok(json!(v))
}
+5
View File
@@ -45,6 +45,11 @@ pub(crate) fn open(s: &mut Session, p: &Value) -> Result<Value> {
let proj = Project::from_json(&text)?;
s.replace_project(proj, Some(path.to_string()));
s.note_project_path(path);
// Lazy open: footage is checked in the background (Progress panel), not before the
// project shows.
if s.check_footage_on_open {
crate::footage_check::start(s, None, false)?;
}
Ok(json!({"path": path}))
}
+9
View File
@@ -851,6 +851,15 @@ pub fn specs() -> Vec<CommandSpec> {
reduce
),
cmd!("file.findMissing", "Find Missing", [], None, "{what: footage|effects|fonts}", has_items, find_missing),
cmd!(
"footage.check",
"Check Footage",
[],
None,
"{items?: [id], wait?} — look for every footage file (in the background unless `wait`) and flag missing items",
has_items,
crate::footage_check::command
),
cmd!(
"file.runScript",
"Run Script File...",
+67 -1
View File
@@ -215,7 +215,16 @@ pub fn text_preset_list() -> Vec<(String, String)> {
}
pub fn find(id: &str) -> Option<&'static CommandSpec> {
command_specs().iter().find(|c| c.id == id)
// Every command execution looks its spec up: index the ~2,000 ids once.
static INDEX: OnceLock<std::collections::HashMap<&'static str, usize>> = OnceLock::new();
let index = INDEX.get_or_init(|| {
let mut m = std::collections::HashMap::new();
for (i, c) in command_specs().iter().enumerate() {
m.entry(c.id).or_insert(i);
}
m
});
index.get(id).map(|&i| &command_specs()[i])
}
// ---------- parameter validation (agents) ----------
@@ -258,6 +267,63 @@ pub fn accepted_params(doc: &str) -> Option<Vec<String>> {
None
}
/// Top-level pieces of a params doc (split at depth-0 commas), e.g. `{layer?, path|prop, value}`
/// → `["layer?", "path|prop", "value"]`.
fn doc_pieces(doc: &str) -> Option<Vec<String>> {
let body = doc.trim().strip_prefix('{')?;
let (mut out, mut cur, mut depth) = (Vec::new(), String::new(), 0i32);
for c in body.chars() {
match c {
'{' | '[' | '(' => depth += 1,
']' | ')' => depth -= 1,
'}' if depth == 0 => {
if !cur.trim().is_empty() {
out.push(cur.trim().to_string());
}
return Some(out);
}
'}' => depth -= 1,
',' if depth == 0 => {
if !cur.trim().is_empty() {
out.push(cur.trim().to_string());
}
cur.clear();
continue;
}
_ => {}
}
cur.push(c);
}
None
}
/// The JSON Schema of a command's parameters, derived from its params doc: one property per
/// accepted key (described by its piece of the doc), no others. MCP `describe_command` and
/// `list_commands {schemas: true}` serve it.
pub fn params_schema(spec: &CommandSpec) -> Value {
let mut props = serde_json::Map::new();
let mut required = vec![];
for piece in doc_pieces(spec.params).unwrap_or_default() {
let keys = accepted_params(&format!("{{{piece}}}")).unwrap_or_default();
let alternatives = keys.len() > 1;
for k in keys {
// `key?` is optional; so is any key with alternatives (`path|prop`).
let optional = alternatives || piece.contains(&format!("{k}?"));
if !optional && !required.contains(&k) {
required.push(k.clone());
}
props.insert(k, serde_json::json!({"description": piece}));
}
}
serde_json::json!({
"type": "object",
"description": spec.params,
"properties": props,
"required": required,
"additionalProperties": accepted_params(spec.params).is_none(),
})
}
/// Keys every command understands (comp targeting, gesture merging) and accepted aliases.
const ALWAYS_OK: &[&str] = &["comp", "merge"];
const ALIASES: &[(&str, &str)] = &[("layers", "layer"), ("layer", "layers"), ("time", "frame"), ("frameRate", "fps"), ("properties", "property")];
+45 -2
View File
@@ -9,6 +9,7 @@ use crate::{EngineError, Result, Session, query};
fn list(s: &mut Session, p: &Value) -> Result<Value> {
let filter = str_p(p, "filter").map(str::to_ascii_lowercase);
let only = b_p(p, "enabledOnly").unwrap_or(false);
let schemas = b_p(p, "schemas").unwrap_or(false);
let v: Vec<Value> = super::command_specs()
.iter()
.filter(|c| filter.as_ref().is_none_or(|f| c.id.to_ascii_lowercase().contains(f) || c.label.to_ascii_lowercase().contains(f)))
@@ -17,7 +18,12 @@ fn list(s: &mut Session, p: &Value) -> Result<Value> {
if only && en.is_err() {
return None;
}
Some(json!({"id": c.id, "label": c.label, "menu": c.menu, "shortcut": c.shortcut, "params": c.params, "enabled": en.is_ok(), "why": en.err()}))
let mut o =
json!({"id": c.id, "label": c.label, "menu": c.menu, "shortcut": c.shortcut, "params": c.params, "enabled": en.is_ok(), "why": en.err()});
if schemas {
o["schema"] = super::params_schema(c);
}
Some(o)
})
.collect();
Ok(json!(v))
@@ -151,13 +157,50 @@ fn layer_tree(s: &mut Session, p: &Value) -> Result<Value> {
Ok(o)
}
/// One command in full, with the JSON Schema of its parameters.
fn describe(s: &mut Session, p: &Value) -> Result<Value> {
let id = str_p(p, "command").or(str_p(p, "id")).ok_or_else(|| super::bad("command.describe", "missing `command`"))?;
let c = super::find(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?;
let en = (c.enabled)(s);
Ok(json!({
"id": c.id, "label": c.label, "menu": c.menu, "shortcut": c.shortcut, "params": c.params,
"schema": super::params_schema(c), "undoable": c.journal, "enabled": en.is_ok(), "why": en.err(),
}))
}
/// What this build can do: counts agents use to orient themselves, and the parity summary.
fn capabilities(s: &mut Session, _: &Value) -> Result<Value> {
let fx = effectcraft_effects::registry();
let formats: Vec<Value> = s.exporter.as_ref().map(|e| e.formats().iter().map(|f| json!(format!("{f:?}"))).collect()).unwrap_or_default();
Ok(json!({
"version": env!("CARGO_PKG_VERSION"),
"commands": super::command_specs().len(),
"effects": {"total": fx.len(), "gpu": fx.iter().filter(|e| e.gpu).count(), "float": fx.iter().filter(|e| e.float).count()},
"export": formats,
"import": s.importer.is_some(),
"expressions": s.expr.is_some(),
"scripting": s.script.is_some(),
"gpu": s.accel.is_some(),
"parity": {
"target": "Adobe After Effects 2026",
"summary": PARITY,
"doc": "docs/parity.md",
},
}))
}
/// The headline of `docs/parity.md` (kept in sync by hand at each audit).
const PARITY: &str = "≈94% weighted feature parity (80 done / 12 partial / 0 missing of 92 features); 298 of 298 After Effects 2026 effects present";
fn state(s: &mut Session, _: &Value) -> Result<Value> {
Ok(serde_json::to_value(&s.state).unwrap_or_default())
}
pub fn specs() -> Vec<CommandSpec> {
vec![
query!("command.list", "List Commands", "{filter?, enabledOnly?}", list),
query!("command.list", "List Commands", "{filter?, enabledOnly?, schemas? (add each command's params JSON Schema)}", list),
query!("command.describe", "Describe Command", "{command} → id, label, menu, shortcut, params doc, JSON `schema`, enabled", describe),
query!("app.capabilities", "App Capabilities", "{} → version, command/effect counts, export formats, parity summary", capabilities),
query!("project.summary", "Project Summary", "{}", summary),
query!("comp.info", "Composition Info", "{comp?}", comp_info),
query!("layer.tree", "Layer Property Tree", "{layer?, comp?, depth?, time? (comp s)} → nodes with `path`", layer_tree),
+192
View File
@@ -0,0 +1,192 @@
//! Lazy project open: footage is checked in the background after File ▸ Open instead of while
//! the project loads (M13.14).
//!
//! A project file already carries every footage item's metadata, so opening it never touches
//! the media: decoding starts when a frame is first needed, thumbnails when the Project panel
//! shows one, expressions compile on first evaluation and system fonts are scanned only when a
//! text layer asks for a family that isn't bundled. What open used to skip entirely is noticing
//! that files went missing (or that an item was saved without metadata). `footage.check` does
//! that as a background task listed in the Progress panel ("Checking footage"): it looks for
//! every file (each frame of an image sequence), probes items that have no metadata, and then
//! flags missing items (`missing`, File ▸ Dependencies ▸ Find Missing Footage). The desktop app
//! starts it after every open ([`Session::check_footage_on_open`]); agents and the CLI run it
//! with `wait: true`.
use effectcraft_project::{Footage, FootageKind, ItemId, ItemKind};
use serde_json::{Value, json};
use crate::Session;
/// One item to check.
struct Job {
id: ItemId,
footage: Footage,
}
/// What the check found for one item.
enum Found {
Missing(bool),
Probed(Box<Footage>),
}
fn exists(s: &dyn crate::Services, path: &str) -> bool {
!path.is_empty() && s.exists(path)
}
/// Start the check (all footage, or `items`). `wait` runs it to completion first (always on
/// wasm32); the result is then `{checked, missing, probed}`.
pub fn start(s: &mut Session, items: Option<Vec<ItemId>>, wait: bool) -> crate::Result<Value> {
let jobs: Vec<Job> = s
.project
.items
.values()
.filter(|i| items.as_ref().is_none_or(|v| v.contains(&i.id)))
.filter_map(|i| match &i.kind {
ItemKind::Footage(f) if f.kind != FootageKind::Data => Some(Job { id: i.id, footage: f.clone() }),
_ => None,
})
.collect();
if jobs.is_empty() {
return Ok(json!({"checked": 0, "missing": 0, "probed": 0}));
}
let services = s.services.clone();
let importer = s.importer.clone();
let n = jobs.len();
s.spawn_task("footageCheck", format!("Checking footage ({n} items)"), wait, move |ctl| {
let mut found: Vec<(ItemId, Found)> = Vec::with_capacity(jobs.len());
let total = jobs.len() as u64;
for (k, j) in jobs.into_iter().enumerate() {
if !ctl.progress(k as u64, total) {
return Err(crate::render_queue::CANCELLED.into());
}
let f = &j.footage;
let present = if f.sequence.is_empty() {
exists(services.as_ref(), &f.path)
} else {
// A sequence is there while any of its frames is (missing frames render as
// placeholders, as in After Effects).
exists(services.as_ref(), &f.path) || f.sequence.iter().any(|p| exists(services.as_ref(), p))
};
// Saved without metadata (a hand-written or converted project): probe it now.
let needs_probe = present && f.width == 0 && f.height == 0 && f.kind != FootageKind::Audio;
match importer.as_ref().filter(|_| needs_probe).map(|imp| imp.probe(&f.path)) {
Some(Ok(mut nf)) => {
nf.alpha = f.alpha;
nf.loop_count = f.loop_count;
found.push((j.id, Found::Probed(Box::new(nf))));
}
_ => found.push((j.id, Found::Missing(!present))),
}
}
ctl.progress(total, total);
let apply: crate::jobs::Apply = Box::new(move |s: &mut Session| Ok(apply(s, found)));
Ok(apply)
})
}
/// Write the findings into the project. Not an undo step: it records the state of the disk,
/// not an edit (the same as After Effects marking footage missing at open).
fn apply(s: &mut Session, found: Vec<(ItemId, Found)>) -> Value {
let checked = found.len();
let (mut missing, mut probed, mut changed) = (0, 0, false);
let p = std::sync::Arc::make_mut(&mut s.project);
for (id, f) in found {
let Some(ItemKind::Footage(cur)) = p.item_mut(id).map(|i| &mut i.kind) else { continue };
match f {
Found::Missing(m) => {
missing += usize::from(m);
if cur.missing != m {
cur.missing = m;
changed = true;
}
}
Found::Probed(nf) => {
probed += 1;
*cur = *nf;
changed = true;
}
}
}
if changed {
let dirty = s.is_dirty();
s.bump();
if !dirty {
// Only the disk changed: the project isn't modified.
s.saved_revision = s.revision;
}
s.events.push(crate::Event::PurgeCaches);
}
if missing > 0 {
s.toast(format!("{missing} footage item(s) missing (File ▸ Dependencies ▸ Find Missing Footage)"));
}
json!({"checked": checked, "missing": missing, "probed": probed})
}
pub(crate) fn command(s: &mut Session, p: &Value) -> crate::Result<Value> {
let items = p.get("items").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).map(ItemId).collect());
let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(false);
start(s, items, wait)
}
#[cfg(test)]
mod tests {
use super::*;
fn session_with(paths: &[&str]) -> Session {
let mut s = Session::default();
for (i, p) in paths.iter().enumerate() {
let f = Footage { path: p.to_string(), width: 64, height: 48, has_video: true, ..Default::default() };
std::sync::Arc::make_mut(&mut s.project).add_item(&format!("f{i}"), crate::color::Label::None, None, ItemKind::Footage(f));
}
s
}
#[test]
fn check_flags_missing_footage_without_an_undo_step_or_dirtying() {
let dir = std::env::temp_dir().join(format!("ec-footage-check-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let there = dir.join("there.png");
std::fs::write(&there, b"x").unwrap();
let mut s = session_with(&[there.to_str().unwrap(), "/nonexistent/ec/gone.png"]);
s.saved_revision = s.revision;
let r = s.execute("footage.check", json!({"wait": true})).unwrap();
assert_eq!(r, json!({"checked": 2, "missing": 1, "probed": 0}));
let missing: Vec<bool> = s
.project
.items
.values()
.filter_map(|i| match &i.kind {
ItemKind::Footage(f) => Some(f.missing),
_ => None,
})
.collect();
assert_eq!(missing, [false, true]);
assert!(s.history.undo.is_empty() && !s.is_dirty());
// In the background: listed as a job until polled.
std::fs::remove_file(&there).unwrap();
let r = s.execute("footage.check", json!({})).unwrap();
assert_eq!(r["started"], true);
assert!(s.jobs().iter().any(|j| j.kind == "footageCheck"));
let t0 = std::time::Instant::now();
while !s.tasks.is_empty() && t0.elapsed().as_secs() < 10 {
s.poll_jobs();
std::thread::yield_now();
}
assert!(s.project.items.values().all(|i| matches!(&i.kind, ItemKind::Footage(f) if f.missing)));
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn open_starts_the_check_when_asked() {
let mut s = session_with(&["/nonexistent/ec/a.mov"]);
let path = std::env::temp_dir().join(format!("ec-footage-open-{}.ecproj", std::process::id()));
s.execute("file.saveAs", json!({"path": path.to_string_lossy()})).unwrap();
let mut o = Session { check_footage_on_open: true, ..Default::default() };
o.execute("file.open", json!({"path": path.to_string_lossy()})).unwrap();
assert!(o.jobs().iter().any(|j| j.kind == "footageCheck"), "{:?}", o.jobs());
let mut plain = Session::default();
plain.execute("file.open", json!({"path": path.to_string_lossy()})).unwrap();
assert!(plain.jobs().is_empty());
let _ = std::fs::remove_file(path);
}
}
+66
View File
@@ -13,6 +13,7 @@ pub mod camera_track;
pub mod commands;
pub mod config;
pub mod demo;
pub mod footage_check;
pub mod history;
pub mod jobs;
pub mod learn;
@@ -23,6 +24,7 @@ pub mod media_browser;
pub mod media_cache;
pub mod menus;
pub mod offload;
pub mod perf;
pub mod prefs;
pub mod preview;
pub mod psd_import;
@@ -84,6 +86,10 @@ pub type Result<T> = std::result::Result<T, EngineError>;
pub trait Services: Send + Sync {
fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>>;
fn write_file(&self, path: &str, data: &[u8]) -> std::io::Result<()>;
/// Whether a file exists (the background footage check after open).
fn exists(&self, path: &str) -> bool {
std::path::Path::new(path).exists()
}
}
/// Native filesystem.
@@ -366,8 +372,15 @@ pub struct Session {
pub next_task_id: u64,
/// The running Learn tutorial (Home ▸ Learn), see [`learn`].
pub learn: Option<learn::Progress>,
/// Check footage in the background after File ▸ Open ([`footage_check`]): frontends with an
/// event loop (the desktop app) turn this on; headless sessions run `footage.check` when
/// they want it.
pub check_footage_on_open: bool,
}
/// A thumbnail render ([`Session::thumbnail_job`]): (width, height, RGBA8).
pub type ThumbnailJob = Box<dyn FnOnce() -> Option<(u32, u32, Vec<u8>)> + Send>;
/// A script to run (see [`Session::script`]).
#[derive(Clone, Copy, Debug)]
pub struct ScriptRequest<'a> {
@@ -437,6 +450,7 @@ impl Default for Session {
job_log: vec![],
next_task_id: 1,
learn: None,
check_footage_on_open: false,
}
}
}
@@ -583,6 +597,10 @@ impl Session {
pub fn active_comp(&self) -> Option<&Comp> {
self.project.comp(self.state.active_comp?)
}
/// [`Session::active_comp`] as a shared snapshot (an `Arc` clone, not a deep copy).
pub fn active_comp_arc(&self) -> Option<std::sync::Arc<Comp>> {
self.project.comp_arc(self.state.active_comp?)
}
pub fn active_comp_id(&self) -> Option<ItemId> {
self.state.active_comp.filter(|c| self.project.comp(*c).is_some())
}
@@ -714,6 +732,54 @@ impl Session {
Ok((img.width, img.height, img.to_rgba8_over(c.background)))
}
/// A Project panel thumbnail of an item (a comp at `t`, footage at its first frame), longest
/// side at most `max_side`, as a job that owns everything it reads: frontends run it off the
/// UI thread (a big comp takes far longer than a frame). `None` for items without pixels.
pub fn thumbnail_job(&self, item: ItemId, t: Tick, max_side: u32) -> Option<ThumbnailJob> {
let it = self.project.item(item)?;
let footage = self.footage.clone();
match &it.kind {
effectcraft_project::ItemKind::Comp(c) => {
let (project, expr, cache) = (self.project.clone(), self.expr.clone(), self.layer_cache.clone());
let long = c.width.max(c.height).max(1) as f64;
let scale = if max_side == 0 { 1.0 } else { (max_side as f64 / long).min(1.0) };
let opts = RenderOpts {
scale,
nested_switches: self.prefs.general.switches_affect_nested_comps,
draft_shadows: self.prefs.three_d.realtime_shadows,
..Default::default()
};
let bg = c.background;
Some(Box::new(move || {
let mut r = Renderer::new(&project, footage.as_ref(), opts);
r.expr = expr.as_deref();
r.cache = Some(&cache);
let img = r.comp_frame(item, t);
Some((img.width, img.height, img.to_rgba8_over(bg)))
}))
}
effectcraft_project::ItemKind::Footage(f) if f.has_video => {
let f = f.clone();
Some(Box::new(move || {
let img = footage.frame(item, &f, Tick(0))?;
// Point-sample down to at most `max_side` on the long side.
let step = (img.width.max(img.height) as f32 / max_side.max(1) as f32).ceil().max(1.0) as u32;
let (w, h) = (img.width.div_ceil(step), img.height.div_ceil(step));
let full = img.to_rgba8();
let mut px = Vec::with_capacity((w * h * 4) as usize);
for y in 0..h {
for x in 0..w {
let i = (((y * step) * img.width + x * step) * 4) as usize;
px.extend_from_slice(&full[i..i + 4]);
}
}
Some((w, h, px))
}))
}
_ => None,
}
}
/// [`Session::render_rgba8`], or with `transparent` the frame's own straight alpha (what a
/// render with RGB + Alpha channels writes) instead of compositing over the background.
pub fn render_rgba8_alpha(&self, comp: ItemId, t: Tick, max_side: u32, transparent: bool) -> Result<(u32, u32, Vec<u8>)> {
+393
View File
@@ -0,0 +1,393 @@
//! Everyday-operation performance: a large generated project and the engine-side measurements
//! behind `effectcraft-cli bench --ops` (open, save, auto-save, large edits with undo/redo, first
//! frame). The UI-side numbers (first UI frame, timeline and Project panel draw) live in
//! `effectcraft-ui-egui`'s `bench` module. See `docs/architecture.md` ▸ Performance.
use std::sync::Arc;
use effectcraft_keyframe::Value as KValue;
use effectcraft_project::build::{self, Ids};
use effectcraft_project::{AlphaMode, Comp, Expression, Footage, FootageKind, ItemId, ItemKind, Layer, LayerSource, Project, Solid};
use effectcraft_time::{FrameRate, Tick};
use serde_json::{Value, json};
use crate::Session;
/// What [`large_project`] builds.
#[derive(Clone, Debug)]
pub struct LargeSpec {
/// Compositions (the main comp, a precomp nesting chain and leaf comps).
pub comps: usize,
/// Layers in the main comp (the timeline stress case).
pub main_layers: usize,
/// Layers in every other comp.
pub comp_layers: usize,
/// Footage items: a third image sequences, a third movies, a third stills.
pub footage: usize,
/// Frames per image sequence.
pub sequence_frames: usize,
/// Depth of the precomp nesting chain under the main comp.
pub nest_depth: usize,
/// Every n-th layer gets expressions (0 = none).
pub expression_every: usize,
/// Folder the footage paths point into (the files need not exist).
pub media_dir: String,
}
impl Default for LargeSpec {
/// 200 comps, 5,000 layers in the main comp (plus 10 in each other comp), 300 footage items
/// (100 sequences of 240 frames, 100 movies, 100 stills), 20 levels of precomp nesting and
/// expressions on every 4th layer.
fn default() -> Self {
LargeSpec {
comps: 200,
main_layers: 5000,
comp_layers: 10,
footage: 300,
sequence_frames: 240,
nest_depth: 20,
expression_every: 4,
media_dir: "/nonexistent/effectcraft-bench-media".into(),
}
}
}
impl LargeSpec {
/// A small version for tests.
pub fn small() -> Self {
LargeSpec { comps: 12, main_layers: 300, comp_layers: 4, footage: 30, sequence_frames: 24, nest_depth: 5, expression_every: 3, ..Default::default() }
}
/// Total layers [`large_project`] makes.
pub fn total_layers(&self) -> usize {
self.main_layers + self.comps.saturating_sub(1) * self.comp_layers
}
}
fn base_comp(name: &str) -> Comp {
let mut s = Session::default();
s.execute("comp.new", json!({"name": name, "width": 1920, "height": 1080, "frameRate": 30, "duration": 10})).expect("comp.new");
s.active_comp().expect("comp").clone()
}
fn footage_item(spec: &LargeSpec, i: usize) -> Footage {
let dir = &spec.media_dir;
let base = Footage { width: 1920, height: 1080, pixel_aspect: 1.0, has_video: true, alpha: AlphaMode::Straight, ..Default::default() };
match i % 3 {
0 => {
let frames: Vec<String> = (0..spec.sequence_frames).map(|f| format!("{dir}/seq{i:03}/shot{i:03}_{f:05}.png")).collect();
Footage {
path: frames.first().cloned().unwrap_or_default(),
kind: FootageKind::Sequence,
frame_rate: FrameRate::FPS_30,
duration: Tick::from_seconds_f64(spec.sequence_frames as f64 / 30.0),
codec: "PNG".into(),
sequence: frames,
..base
}
}
1 => Footage {
path: format!("{dir}/clip{i:03}.mp4"),
kind: FootageKind::Video,
frame_rate: FrameRate::FPS_30,
duration: Tick::from_seconds_f64(20.0),
has_audio: true,
codec: "H.264".into(),
..base
},
_ => Footage { path: format!("{dir}/still{i:03}.png"), kind: FootageKind::Still, codec: "PNG".into(), ..base },
}
}
fn set_expr(l: &mut Layer, path: &str, text: &str) {
if let Some(p) = l.props.prop_mut(path) {
p.expr = Some(Expression { text: text.into(), enabled: true });
}
}
/// One layer of a varied mix: solids, footage, text, shapes, nulls, some with an effect, some
/// with expressions, parented to the layer above every 7th.
fn make_layer(p: &mut Project, comp: &Comp, spec: &LargeSpec, n: usize, solid: ItemId, footage: &[ItemId], prev: Option<&Layer>) -> Layer {
let (src, size) = match n % 5 {
0 => (LayerSource::Solid { item: solid }, (1920, 1080)),
1 if !footage.is_empty() => (LayerSource::Footage { item: footage[n % footage.len()] }, (1920, 1080)),
2 => (LayerSource::Text, (0, 0)),
3 => (LayerSource::Shape, (0, 0)),
_ => (LayerSource::Null, (100, 100)),
};
let mut l = build::layer(p, comp, &format!("Layer {n}"), src, size, None);
if n.is_multiple_of(6)
&& let Some(fx) = crate::effects::find("ec.blur.gaussian")
{
let mut next = p.next_id;
let mut g = crate::effects::instantiate(fx, &mut Ids(&mut next), fx.name, [1920.0, 1080.0]);
p.next_id = next;
if let Some(b) = g.prop_mut("blurriness") {
b.value = KValue::Scalar(4.0);
}
if let Some(e) = l.props.sub_mut("effects") {
e.children.push(g.into());
}
}
if spec.expression_every > 0 && n.is_multiple_of(spec.expression_every) {
set_expr(&mut l, "transform/position", "wiggle(2, 20)");
set_expr(&mut l, "transform/rotation", "time * 30 + index");
set_expr(&mut l, "transform/opacity", "50 + 50 * Math.sin(time * 2)");
}
if n.is_multiple_of(7)
&& let Some(prev) = prev
{
l.parent = Some(prev.id);
}
// Stagger the bars so the timeline isn't one block.
let span = comp.duration.0.max(1);
let off = (n as i64 * 7919) % span;
l.in_point = Tick(off / 2);
l
}
/// A large project for performance work (see [`LargeSpec`]). The main comp is named `Main` and
/// is the first comp; the nesting chain is `Nest 1` (inside Main) … `Nest {depth}`.
pub fn large_project(spec: &LargeSpec) -> Project {
let mut p = Project::default();
let template = base_comp("Main");
let media = p.add_item("Media", crate::color::Label::None, None, ItemKind::Folder);
let comps_folder = p.add_item("Comps", crate::color::Label::None, None, ItemKind::Folder);
let footage: Vec<ItemId> = (0..spec.footage)
.map(|i| {
let f = footage_item(spec, i);
let name = std::path::Path::new(&f.path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
p.add_item(&name, crate::color::Label::None, Some(media), ItemKind::Footage(f))
})
.collect();
let solid = p.add_item(
"Bench Solid",
crate::color::Label::None,
Some(media),
ItemKind::Solid(Solid { color: [0.2, 0.4, 0.8], width: 1920, height: 1080, pixel_aspect: 1.0 }),
);
// Leaf comps and the nesting chain are built bottom-up so each precomp layer can point at
// an existing item.
let mut n = 0usize;
let fill = |p: &mut Project, comp: &mut Comp, count: usize, n: &mut usize| {
let mut layers: Vec<Layer> = Vec::with_capacity(count);
for _ in 0..count {
let l = make_layer(p, comp, spec, *n, solid, &footage, layers.last());
*n += 1;
layers.push(l);
}
comp.layers = layers;
};
let others = spec.comps.saturating_sub(1);
let depth = spec.nest_depth.min(others);
// Nesting chain: Nest {depth} (deepest) … Nest 1.
let mut below: Option<ItemId> = None;
for d in (1..=depth).rev() {
let mut c = template.clone();
fill(&mut p, &mut c, spec.comp_layers.saturating_sub(1), &mut n);
if let Some(b) = below {
let l = build::layer(&mut p, &c, &format!("Nest {}", d + 1), LayerSource::Comp { item: b }, (1920, 1080), None);
c.layers.insert(0, l);
}
below = Some(p.add_item(&format!("Nest {d}"), crate::color::Label::None, Some(comps_folder), ItemKind::Comp(Arc::new(c))));
}
let mut leaves = vec![];
for i in depth..others {
let mut c = template.clone();
fill(&mut p, &mut c, spec.comp_layers, &mut n);
leaves.push(p.add_item(&format!("Shot {:03}", i + 1), crate::color::Label::None, Some(comps_folder), ItemKind::Comp(Arc::new(c))));
}
// Main: the chain's top, a few leaf precomps, then the bulk.
let mut main = template;
let mut layers = vec![];
if let Some(b) = below {
layers.push(build::layer(&mut p, &main, "Nest 1", LayerSource::Comp { item: b }, (1920, 1080), None));
}
for (k, leaf) in leaves.iter().take(spec.main_layers.saturating_sub(1) / 10).enumerate() {
layers.push(build::layer(&mut p, &main, &format!("Shot {:03}", k + 1), LayerSource::Comp { item: *leaf }, (1920, 1080), None));
}
while layers.len() < spec.main_layers {
let l = make_layer(&mut p, &main, spec, n, solid, &footage, layers.last());
n += 1;
layers.push(l);
}
main.layers = layers;
p.add_item("Main", crate::color::Label::None, None, ItemKind::Comp(Arc::new(main)));
// `Main` must be the first comp `replace_project` opens: give it the smallest id.
let main_id = p.items.values().find(|i| i.name == "Main").map(|i| i.id).expect("main");
let first = p.items.keys().next().copied().expect("items");
if first != main_id {
let item = p.items.remove(&main_id).expect("main");
let other = p.items.remove(&first).expect("first");
// Swap ids: references to `first` (the Media folder) are the footage/solid parents.
p.items.insert(first, effectcraft_project::Item { id: first, ..item });
p.items.insert(main_id, effectcraft_project::Item { id: main_id, ..other });
for it in p.items.values_mut() {
if it.parent == Some(first) {
it.parent = Some(main_id);
}
}
}
p.fix_next_id();
p
}
/// One measurement.
#[derive(Clone, Debug)]
pub struct Measure {
pub name: &'static str,
pub ms: f64,
pub note: String,
}
impl Measure {
pub fn json(&self) -> Value {
json!({"name": self.name, "ms": (self.ms * 1000.0).round() / 1000.0, "note": self.note})
}
}
/// Time `f` (best of `runs`).
pub fn time_best<T>(runs: usize, mut f: impl FnMut() -> T) -> (f64, T) {
let mut best = f64::INFINITY;
let mut out = None;
for _ in 0..runs.max(1) {
let t0 = web_time::Instant::now();
let r = f();
best = best.min(t0.elapsed().as_secs_f64() * 1000.0);
out = Some(r);
}
(best, out.expect("ran"))
}
/// The engine-side everyday operations on a large project: build, save, open (into a session
/// from `fresh`), first frame, small and large edits with undo/redo, auto-save. Files go into
/// `dir`. Returns the measurements and the opened session (for the UI-side numbers).
pub fn ops_bench(spec: &LargeSpec, dir: &std::path::Path, fresh: &dyn Fn() -> Session) -> (Vec<Measure>, Session) {
let mut out = vec![];
let (ms, project) = time_best(1, || large_project(spec));
let comps = project.comps().count();
let layers: usize = project.comps().map(|(_, c)| c.layers.len()).sum();
out.push(Measure { name: "generate", ms, note: format!("{comps} comps, {layers} layers, {} items", project.items.len()) });
let path = dir.join("bench-large.ecproj");
let path_s = path.to_string_lossy().to_string();
let mut s = fresh();
s.replace_project(project, None);
let (ms, r) = time_best(3, || s.execute("file.saveAs", json!({"path": path_s})).expect("save"));
out.push(Measure { name: "save", ms, note: format!("{} bytes", r["bytes"]) });
// Open: what File ▸ Open does, into a fresh session.
let (ms, mut s) = time_best(3, || {
let mut s = fresh();
s.execute("file.open", json!({"path": path_s})).expect("open");
s
});
out.push(Measure { name: "open", ms, note: format!("active comp {:?}", s.state.active_comp.map(|c| c.0)) });
// Its parts: reading the file and parsing it (the rest is replacing the session's project).
let (ms, text) = time_best(3, || std::fs::read_to_string(&path).unwrap_or_default());
out.push(Measure { name: "openRead", ms, note: format!("{} MB", text.len() / 1_000_000) });
let (ms, _) = time_best(3, || Project::from_json(&text).expect("parse"));
out.push(Measure { name: "openParse", ms, note: "JSON → Project".into() });
let compact = Project::from_json(&text).expect("parse").to_json_compact();
drop(text);
let (ms, _) = time_best(3, || Project::from_json(&compact).expect("parse"));
out.push(Measure { name: "openParseCompact", ms, note: format!("the same as compact JSON ({} MB, as auto-saves are written)", compact.len() / 1_000_000) });
drop(compact);
// First frame of the main comp at 1/4 resolution (the viewer's first request).
let cid = s.active_comp_id().expect("main comp");
let (ms, _) = time_best(1, || {
let opts = crate::render::RenderOpts { scale: 0.25, ..Default::default() };
s.render(cid, Tick::ZERO, opts)
});
out.push(Measure { name: "firstFrame", ms, note: "Main at 1/4, cold".into() });
// A small edit in the big project (one property on one layer): the everyday cost of the
// undo snapshot.
let first = s.active_comp().and_then(|c| c.layers.get(10)).map(|l| l.id.0).expect("layer");
let mut v = 0.0;
let (ms, _) = time_best(20, || {
v += 1.0;
s.execute("prop.set", json!({"layer": first, "path": "transform/opacity", "value": 50.0 + v})).expect("prop.set")
});
out.push(Measure { name: "smallEdit", ms, note: "prop.set on one layer of Main".into() });
let (ms, _) = time_best(5, || {
s.undo();
s.redo();
});
out.push(Measure { name: "smallUndoRedo", ms, note: "one undo + one redo".into() });
// A large edit: every layer of Main at once.
let all: Vec<u64> = s.active_comp().map(|c| c.layers.iter().map(|l| l.id.0).collect()).unwrap_or_default();
let mut on = false;
let (ms, _) = time_best(3, || {
on = !on;
s.execute("layer.setSwitch", json!({"layers": all, "switch": "shy", "value": on})).expect("setSwitch")
});
out.push(Measure { name: "largeEdit", ms, note: format!("shy switch on {} layers", all.len()) });
let (ms, _) = time_best(3, || s.undo());
out.push(Measure { name: "largeUndo", ms, note: String::new() });
let (ms, _) = time_best(3, || s.redo());
out.push(Measure { name: "largeRedo", ms, note: String::new() });
// Auto-save: the full cost and what the UI thread pays.
let auto = dir.join("autosave");
let _ = s.execute("prefs.set", json!({"values": {"autoSave.location": "custom", "autoSave.folder": auto.to_string_lossy()}}));
s.path = Some(path_s.clone());
let (ms, _) = time_best(3, || s.autosave_now().expect("autosave"));
out.push(Measure { name: "autosaveSync", ms, note: "serialise + write on the calling thread".into() });
s.autosave.background = true;
let (ms, _) = time_best(3, || s.autosave_now().expect("autosave"));
out.push(Measure { name: "autosaveUiThread", ms, note: "background auto-save: UI-thread share".into() });
s.autosave.background = false;
let _ = s.autosave_wait();
(out, s)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn large_project_round_trips_and_opens_main() {
let spec = LargeSpec::small();
let p = large_project(&spec);
assert_eq!(p.comps().count(), spec.comps);
let layers: usize = p.comps().map(|(_, c)| c.layers.len()).sum();
assert!(layers >= spec.total_layers() - spec.comps, "{layers}");
let footage = p.items.values().filter(|i| matches!(i.kind, ItemKind::Footage(_))).count();
assert_eq!(footage, spec.footage);
let back = Project::from_json(&p.to_json()).unwrap();
assert_eq!(back, p);
let mut s = Session::default();
s.replace_project(p, None);
assert_eq!(s.active_comp().map(|c| c.layers.len()), Some(spec.main_layers));
// The nesting chain is `nest_depth` deep.
let mut depth = 0;
let mut cur = s.project.items.values().find(|i| i.name == "Nest 1").map(|i| i.id);
while let Some(c) = cur {
depth += 1;
cur = s.project.comp(c).and_then(|c| {
c.layers.iter().find_map(|l| match l.source {
LayerSource::Comp { item } => Some(item),
_ => None,
})
});
}
assert_eq!(depth, spec.nest_depth);
}
#[test]
fn ops_bench_runs_on_a_small_project() {
let dir = std::env::temp_dir().join(format!("ec-ops-bench-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (m, s) = ops_bench(&LargeSpec::small(), &dir, &Session::default);
let names: Vec<&str> = m.iter().map(|m| m.name).collect();
for want in ["generate", "save", "open", "firstFrame", "smallEdit", "largeEdit", "largeUndo", "largeRedo", "autosaveSync", "autosaveUiThread"] {
assert!(names.contains(&want), "{want}");
}
assert!(m.iter().all(|m| m.ms.is_finite() && m.ms >= 0.0));
assert!(s.autosave.last_path.as_deref().is_some_and(|p| std::path::Path::new(p).is_file()));
let _ = std::fs::remove_dir_all(dir);
}
}
+42 -4
View File
@@ -163,7 +163,9 @@ impl Session {
pub(crate) fn note_project_path(&mut self, path: &str) {
self.prefs.push_recent(path);
self.save_prefs();
self.autosave = autosave::AutoSaveState { last: self.autosave.last, sentinel: self.autosave.sentinel, ..Default::default() };
let _ = self.autosave_wait();
self.autosave =
autosave::AutoSaveState { last: self.autosave.last, sentinel: self.autosave.sentinel, background: self.autosave.background, ..Default::default() };
self.update_sentinel();
}
@@ -191,11 +193,34 @@ impl Session {
}
/// Write an auto-save now (whether or not the project is dirty). Returns its path.
///
/// With [`autosave::AutoSaveState::background`] the project is serialised and written on a
/// background thread (the path is returned at once; [`Session::autosave_wait`] waits).
/// Auto-saves are compact JSON (opened like any project); File ▸ Save writes pretty JSON.
pub fn autosave_now(&mut self) -> Result<String> {
let json = self.project.to_json();
// One write at a time: the previous one must land before its slot rotates.
self.autosave_wait()?;
let root = self.default_autosave_root();
let (path, slot) = autosave::write_in(self.file_ops(), &self.prefs, self.path.as_deref(), root.as_deref(), self.autosave.last_slot, &json)
.map_err(|e| EngineError::Other(format!("auto-save failed: {e}")))?;
let fail = |e: std::io::Error| EngineError::Other(format!("auto-save failed: {e}"));
let plan = autosave::plan_in(self.file_ops(), &self.prefs, self.path.as_deref(), root.as_deref(), self.autosave.last_slot).map_err(fail)?;
if self.autosave.background && !cfg!(target_arch = "wasm32") {
let project = self.project.clone();
let config = self.config.clone();
let job = plan.clone();
let pending = autosave::PendingWrite::spawn(plan.path.clone(), move || {
let json = project.to_json_compact();
match &config {
Some(c) => job.write(c.files(), json.as_bytes()),
None => job.write(&crate::config::StdFiles, json.as_bytes()),
}
})
.map_err(fail)?;
self.autosave.pending = Some(pending);
} else {
let json = self.project.to_json_compact();
plan.write(self.file_ops(), json.as_bytes()).map_err(fail)?;
}
let (path, slot) = (plan.path, plan.slot);
let p = path.to_string_lossy().to_string();
self.autosave.last_slot = Some(slot);
self.autosave.saved_revision = Some(self.revision);
@@ -204,9 +229,22 @@ impl Session {
Ok(p)
}
/// Wait for a background auto-save ([`autosave::AutoSaveState::background`]) to land.
pub fn autosave_wait(&mut self) -> Result<()> {
match self.autosave.pending.take() {
Some(w) => w.join().map_err(|e| EngineError::Other(format!("auto-save failed: {e}"))),
None => Ok(()),
}
}
/// Call periodically with wall-clock seconds: auto-saves a dirty project once the interval
/// has passed since the last auto-save (or since the first call). Returns the written path.
pub fn autosave_tick(&mut self, now: f64) -> Option<Result<String>> {
if self.autosave.pending.as_ref().is_some_and(autosave::PendingWrite::is_finished)
&& let Err(e) = self.autosave_wait()
{
return Some(Err(e));
}
let a = &self.prefs.auto_save;
if !a.enabled {
self.autosave.last = Some(now);