Files
raea/runtime/encode/src/progress.rs
shinyobjectz 57f3ef8db3 Windows: canonicalize goes through dunce and the home folder through user_home (all but five crates)
canonicalize() through std gives \\?\C:\... on Windows: a path Git for Windows can neither read nor lock, Rolldown can't resolve, and which shares no root with the C:\... git and everything else print. It broke a new workspace's git config (fixed in apps/workspace earlier today). Here the calls in the apps (CapCut, check, direction, engines, fingerprint, game, interchange, judge, solid, sound, targets, tools), the bundler, the CLI, the modules loader, the native shim's realpath, the page's motion, io (assets, gen, home, shell), runtime/bundle/lib, js, track and world, the systems (fingerprint-use, footage, index, ml, page-net), the bridge bench's examples and the sdk/ui test tools use dunce::canonicalize, which gives C:\... wherever a path can be said so and is std's own call on the Mac and Linux.

Reads of the home folder took HOME alone, which Windows doesn't set for an app opened from the Start menu or Explorer, so the taste file, the settings file, the inbox, the npm store, the caches (sounds, splats, goo, ingest, speech, Core ML, the portable perceive models), the node shim's os.homedir() and CapCut's drafts were looked for under whatever folder raea ran in. raea_io_home::user_home() reads HOME, then USERPROFILE (Git for Windows' order; an empty one counts as unset), and models(), caches(), logs(), env_path() and offline() use it, as do the places that had their own fallback.

The caches that had their own copy of "this variable, else this folder under home" (splats, goo, ingest, the npm store, Core ML's models) now call raea_io_home::env_path and models(), and raea_io_home::real canonicalizes a path or keeps it as given, so tools/dupes.max comes down to 289 exact and 58 near. tools/skills.sh reads a variable from env_path("RAEA_...", ".raea/...") as it did from env::var, so files.md keeps RAEA_NPM_HOME on ~/.raea/npm (every generated reference is unchanged).

Five crates hold other sessions' uncommitted edits (engine/page/stage, engine/render, runtime/bundle, runtime/export, runtime/live): their few calls, and tools/windows-paths.sh, the check that keeps both at zero, land once those are committed.

Checked: cargo check --all-targets of every crate changed, and of sdk/ui/conformance and sdk/ui/parity. On a Windows 11 PC and on macOS the unit tests of raea-io-home (user_home's order and an empty HOME, now in caches_logs_and_the_home_folder_follow_the_platform), raea-app-check, raea-app-workspace, raea-app-registry and raea-core-facts pass (5, 27, 10, 7, 17).
2026-11-17 10:38:33 -08:00

114 lines
4.6 KiB
Rust

//! What a render is doing while it runs, for whatever shows it: the raea app's chat draws a meter from it (the
//! developer, 11-16: "in the chat ui having something that if its rendering we can see a progress meter"). One small
//! JSON file per render, `<renders>/<pid>-<n>.json`:
//!
//! `{"pid", "out", "cwd", "frames", "done", "started", "updated"}` (times in ms since the epoch)
//!
//! It's written when the render starts, as its frames finish (at most four times a second, and at the last frame),
//! and taken away when the render ends, however it ends; one left by a process that died is a reader's to pass over
//! (its pid is gone). `<renders>` is $RAEA_RENDERS, else ~/.raea/renders; `RAEA_RENDERS=off` writes none.
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
static SEQ: AtomicUsize = AtomicUsize::new(0);
const EVERY: Duration = Duration::from_millis(250);
/// A render's progress, kept on disk while it lives.
pub struct Progress {
file: Option<PathBuf>,
out: String,
cwd: String,
frames: usize,
done: usize,
started: u64,
last: Option<Instant>,
}
fn dir() -> Option<PathBuf> {
let named = std::env::var_os("RAEA_RENDERS").filter(|v| !v.is_empty());
if named.as_deref() == Some(std::ffi::OsStr::new("off")) {
return None;
}
// unset: ~/.raea/renders, the home being USERPROFILE on Windows, which has no HOME
match (named, raea_io_home::user_home()) {
(Some(d), _) => Some(PathBuf::from(d)),
(None, Some(home)) => Some(Path::new(&home).join(".raea/renders")),
(None, None) => None,
}
}
fn now_ms() -> u64 {
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0)
}
impl Progress {
/// A render of `frames` frames to `out`, starting.
pub fn start(out: &Path, frames: usize) -> Self {
Self::start_in(dir(), out, frames)
}
fn start_in(dir: Option<PathBuf>, out: &Path, frames: usize) -> Self {
let file = dir.and_then(|d| std::fs::create_dir_all(&d).ok().map(|_| d.join(format!("{}-{}.json", std::process::id(), SEQ.fetch_add(1, Ordering::Relaxed)))));
let out = std::path::absolute(out).unwrap_or_else(|_| out.to_path_buf()).display().to_string();
let cwd = std::env::current_dir().map(|d| d.display().to_string()).unwrap_or_default();
let mut p = Progress { file, out, cwd, frames, done: 0, started: now_ms(), last: None };
p.write();
p
}
/// One more frame done.
pub fn frame(&mut self) {
self.done += 1;
if self.done >= self.frames || self.last.is_none_or(|t| t.elapsed() >= EVERY) {
self.write();
}
}
fn write(&mut self) {
let Some(f) = &self.file else { return };
self.last = Some(Instant::now());
let body = serde_json::json!({"pid": std::process::id(), "out": self.out, "cwd": self.cwd, "frames": self.frames, "done": self.done, "started": self.started, "updated": now_ms()});
// written whole beside it, then put in its place: a reader never sees half of one
let part = f.with_extension("json.part");
if std::fs::write(&part, body.to_string()).is_ok() {
let _ = std::fs::rename(&part, f);
}
}
}
impl Drop for Progress {
fn drop(&mut self) {
if let Some(f) = &self.file {
let _ = std::fs::remove_file(f);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn written_as_frames_finish_and_gone_when_the_render_ends() {
let d = std::env::temp_dir().join(format!("raea-renders-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
let read = || -> serde_json::Value {
let f = std::fs::read_dir(&d).unwrap().filter_map(|e| e.ok()).map(|e| e.path()).find(|p| p.extension().is_some_and(|x| x == "json")).unwrap();
serde_json::from_str(&std::fs::read_to_string(f).unwrap()).unwrap()
};
let mut p = Progress::start_in(Some(d.clone()), Path::new("film.mp4"), 3);
let v = read();
assert_eq!((v["frames"].as_u64(), v["done"].as_u64(), v["pid"].as_u64()), (Some(3), Some(0), Some(std::process::id() as u64)));
assert!(v["out"].as_str().unwrap().ends_with("film.mp4") && Path::new(v["out"].as_str().unwrap()).is_absolute());
p.frame();
p.frame();
p.frame();
// the last frame is always written, however soon after the one before
assert_eq!(read()["done"].as_u64(), Some(3));
drop(p);
assert_eq!(std::fs::read_dir(&d).unwrap().count(), 0);
let _ = std::fs::remove_dir_all(&d);
}
}