@@ -47,11 +47,17 @@ pub enum Commands {
4747 /// Sidecar socket to read events from.
4848 #[ arg( long) ]
4949 socket : Option < PathBuf > ,
50+ /// Prior run directory (or `metrics.jsonl`) to overlay as a baseline.
51+ #[ arg( long) ]
52+ compare : Option < PathBuf > ,
5053 } ,
5154 /// Tail a run directory's `metrics.jsonl` and render it live.
5255 Watch {
5356 /// Run directory (or a `metrics.jsonl` file directly).
5457 path : PathBuf ,
58+ /// Prior run directory (or `metrics.jsonl`) to overlay as a baseline.
59+ #[ arg( long) ]
60+ compare : Option < PathBuf > ,
5561 } ,
5662 /// Serve the live web dashboard for a run directory or sidecar socket.
5763 Web {
@@ -142,8 +148,12 @@ pub fn run() -> ExitCode {
142148fn dispatch ( command : Commands ) -> ExitCode {
143149 let result = match command {
144150 Commands :: Demo { steps } => cmd_demo ( steps) ,
145- Commands :: Watch { path } => cmd_watch ( & path) ,
146- Commands :: Tui { run_dir, socket } => cmd_tui ( run_dir. as_deref ( ) , socket. as_deref ( ) ) ,
151+ Commands :: Watch { path, compare } => cmd_watch ( & path, compare. as_deref ( ) ) ,
152+ Commands :: Tui {
153+ run_dir,
154+ socket,
155+ compare,
156+ } => cmd_tui ( run_dir. as_deref ( ) , socket. as_deref ( ) , compare. as_deref ( ) ) ,
147157 Commands :: Web {
148158 run_dir,
149159 socket,
@@ -173,20 +183,48 @@ fn dispatch(command: Commands) -> ExitCode {
173183 }
174184}
175185
176- /// Builds the dashboard state (seeded with `labels`) and renders `events` to the
177- /// terminal until the user quits.
186+ /// Builds the dashboard state (seeded with `labels` and an optional comparison
187+ /// `baseline`) and renders `events` to the terminal until the user quits.
178188fn run_dashboard (
179189 events : & Receiver < Event > ,
180190 labels : & [ ( MetricId , String ) ] ,
191+ baseline : Vec < emry_tui:: BaselineSeries > ,
181192) -> Result < ( ) , Box < dyn Error > > {
182193 let label_refs: Vec < ( MetricId , & str ) > = labels
183194 . iter ( )
184195 . map ( |( id, name) | ( * id, name. as_str ( ) ) )
185196 . collect ( ) ;
186- run_terminal ( events, UiState :: with_labels ( & label_refs) ) ?;
197+ let mut state = UiState :: with_labels ( & label_refs) ;
198+ state. set_baseline ( baseline) ;
199+ run_terminal ( events, state) ?;
187200 Ok ( ( ) )
188201}
189202
203+ /// Loads a `--compare` baseline run into the TUI's series, warning (and
204+ /// continuing without an overlay) if it cannot be read.
205+ fn load_compare ( compare : Option < & Path > ) -> Vec < emry_tui:: BaselineSeries > {
206+ let Some ( path) = compare else {
207+ return Vec :: new ( ) ;
208+ } ;
209+ match emry_store:: load_baseline ( path) {
210+ Ok ( series) => series
211+ . into_iter ( )
212+ . map ( |s| emry_tui:: BaselineSeries {
213+ label : s. label ,
214+ steps : s. steps ,
215+ values : s. values ,
216+ } )
217+ . collect ( ) ,
218+ Err ( err) => {
219+ eprintln ! (
220+ "emry: could not load --compare baseline {}: {err}" ,
221+ path. display( )
222+ ) ;
223+ Vec :: new ( )
224+ }
225+ }
226+ }
227+
190228/// `emry demo` — a synthetic run streamed to the live dashboard.
191229fn cmd_demo ( steps : u64 ) -> Result < ( ) , Box < dyn Error > > {
192230 let run_dir = std:: env:: temp_dir ( ) . join ( "emry-demo" ) ;
@@ -219,7 +257,30 @@ fn cmd_demo(steps: u64) -> Result<(), Box<dyn Error>> {
219257 run. finish ( ) . ok ( ) ;
220258 } ) ;
221259
222- run_dashboard ( & events, & labels)
260+ // A synthetic prior run (slower-decaying loss) overlaid as the amber
261+ // comparison baseline, so the demo also exercises `--compare`.
262+ let baseline = demo_baseline ( steps) ;
263+ run_dashboard ( & events, & labels, baseline)
264+ }
265+
266+ /// A synthetic prior-run baseline for the demo's `loss` / `loss_ema` curves.
267+ fn demo_baseline ( steps : u64 ) -> Vec < emry_tui:: BaselineSeries > {
268+ let stride = ( steps / 400 ) . max ( 1 ) ;
269+ let ( mut s, mut v) = ( Vec :: new ( ) , Vec :: new ( ) ) ;
270+ let mut step = 0 ;
271+ while step < steps {
272+ #[ allow( clippy:: cast_precision_loss) ]
273+ let loss = 2.3 / ( 1.0 + step as f64 * 0.0032 ) ;
274+ s. push ( step) ;
275+ v. push ( loss) ;
276+ step += stride;
277+ }
278+ let series = |label : & str | emry_tui:: BaselineSeries {
279+ label : label. to_string ( ) ,
280+ steps : s. clone ( ) ,
281+ values : v. clone ( ) ,
282+ } ;
283+ vec ! [ series( "loss" ) , series( "loss_ema" ) ]
223284}
224285
225286/// `emry watch PATH` — tail a run directory's `metrics.jsonl` into the dashboard.
@@ -228,9 +289,10 @@ fn cmd_demo(steps: u64) -> Result<(), Box<dyn Error>> {
228289/// only after the dashboard starts render with the `m{id}` fallback name until a
229290/// name-table protocol exists (a future `MetricRegistered` event); in practice
230291/// metrics are registered at run start, so they are present in the first poll.
231- fn cmd_watch ( path : & Path ) -> Result < ( ) , Box < dyn Error > > {
292+ fn cmd_watch ( path : & Path , compare : Option < & Path > ) -> Result < ( ) , Box < dyn Error > > {
293+ let baseline = load_compare ( compare) ;
232294 let ( rx, labels, stop, poller) = spawn_run_dir_tailer ( path) ?;
233- let result = run_dashboard ( & rx, & labels) ;
295+ let result = run_dashboard ( & rx, & labels, baseline ) ;
234296 stop. store ( true , Ordering :: Release ) ;
235297 let _ = poller. join ( ) ;
236298 result
@@ -283,19 +345,24 @@ fn spawn_run_dir_tailer(path: &Path) -> Result<RunDirTailer, Box<dyn Error>> {
283345}
284346
285347/// `emry tui` — attach to a run directory (`--run-dir`) or a socket (`--socket`).
286- fn cmd_tui ( run_dir : Option < & Path > , socket : Option < & Path > ) -> Result < ( ) , Box < dyn Error > > {
348+ fn cmd_tui (
349+ run_dir : Option < & Path > ,
350+ socket : Option < & Path > ,
351+ compare : Option < & Path > ,
352+ ) -> Result < ( ) , Box < dyn Error > > {
287353 match ( run_dir, socket) {
288- ( Some ( dir) , _) => cmd_watch ( dir) ,
289- ( None , Some ( sock) ) => cmd_socket_tui ( sock) ,
354+ ( Some ( dir) , _) => cmd_watch ( dir, compare ) ,
355+ ( None , Some ( sock) ) => cmd_socket_tui ( sock, compare ) ,
290356 ( None , None ) => Err ( "specify --run-dir or --socket" . into ( ) ) ,
291357 }
292358}
293359
294360/// Reads framed events from a sidecar socket into the dashboard.
295- fn cmd_socket_tui ( sock : & Path ) -> Result < ( ) , Box < dyn Error > > {
361+ fn cmd_socket_tui ( sock : & Path , compare : Option < & Path > ) -> Result < ( ) , Box < dyn Error > > {
362+ let baseline = load_compare ( compare) ;
296363 let rx = spawn_socket_reader ( sock) ?;
297364 // Socket frames carry MetricIds, not names; labels fall back to `m{id}`.
298- run_dashboard ( & rx, & [ ] )
365+ run_dashboard ( & rx, & [ ] , baseline )
299366}
300367
301368/// Connects to a sidecar socket and drains framed events into a bounded channel
@@ -335,8 +402,20 @@ fn cmd_web(
335402 ( None , Some ( sock) ) => ( spawn_socket_reader ( sock) ?, Vec :: new ( ) ) ,
336403 ( None , None ) => return Err ( "specify --run-dir or --socket" . into ( ) ) ,
337404 } ;
405+ // Load the comparison baseline via the one canonical reader
406+ // (`emry_store::load_baseline`) and map it into the web crate's serializable
407+ // types — same path the TUI overlay uses, so behaviour is consistent.
338408 let baseline = match compare {
339- Some ( path) => Some ( emry_web:: load_baseline ( path) ?) ,
409+ Some ( path) => Some ( emry_web:: Baseline {
410+ metrics : emry_store:: load_baseline ( path) ?
411+ . into_iter ( )
412+ . map ( |s| emry_web:: BaselineSeries {
413+ label : s. label ,
414+ steps : s. steps ,
415+ values : s. values ,
416+ } )
417+ . collect ( ) ,
418+ } ) ,
340419 None => None ,
341420 } ;
342421
@@ -602,7 +681,22 @@ mod tests {
602681 fn watch_requires_a_path ( ) {
603682 assert ! ( parse( & [ "emry" , "watch" ] ) . is_err( ) ) ;
604683 match parse ( & [ "emry" , "watch" , "./logs/run" ] ) . unwrap ( ) {
605- Commands :: Watch { path } => assert_eq ! ( path, PathBuf :: from( "./logs/run" ) ) ,
684+ Commands :: Watch { path, compare } => {
685+ assert_eq ! ( path, PathBuf :: from( "./logs/run" ) ) ;
686+ assert ! ( compare. is_none( ) ) ;
687+ }
688+ other => panic ! ( "unexpected {other:?}" ) ,
689+ }
690+ }
691+
692+ #[ test]
693+ fn watch_and_tui_accept_compare ( ) {
694+ match parse ( & [ "emry" , "watch" , "run" , "--compare" , "old" ] ) . unwrap ( ) {
695+ Commands :: Watch { compare, .. } => assert_eq ! ( compare, Some ( PathBuf :: from( "old" ) ) ) ,
696+ other => panic ! ( "unexpected {other:?}" ) ,
697+ }
698+ match parse ( & [ "emry" , "tui" , "--run-dir" , "run" , "--compare" , "old" ] ) . unwrap ( ) {
699+ Commands :: Tui { compare, .. } => assert_eq ! ( compare, Some ( PathBuf :: from( "old" ) ) ) ,
606700 other => panic ! ( "unexpected {other:?}" ) ,
607701 }
608702 }
@@ -619,7 +713,9 @@ mod tests {
619713 ] )
620714 . unwrap ( )
621715 {
622- Commands :: Tui { run_dir, socket } => {
716+ Commands :: Tui {
717+ run_dir, socket, ..
718+ } => {
623719 assert_eq ! ( run_dir, Some ( PathBuf :: from( "./logs/run" ) ) ) ;
624720 assert_eq ! ( socket, Some ( PathBuf :: from( "/tmp/e.sock" ) ) ) ;
625721 }
@@ -630,7 +726,9 @@ mod tests {
630726 #[ test]
631727 fn tui_flags_are_optional ( ) {
632728 match parse ( & [ "emry" , "tui" ] ) . unwrap ( ) {
633- Commands :: Tui { run_dir, socket } => {
729+ Commands :: Tui {
730+ run_dir, socket, ..
731+ } => {
634732 assert ! ( run_dir. is_none( ) && socket. is_none( ) ) ;
635733 }
636734 other => panic ! ( "unexpected {other:?}" ) ,
@@ -846,7 +944,7 @@ mod tests {
846944 #[ test]
847945 fn tui_with_no_target_errors ( ) {
848946 // Pure validation path (no terminal): both targets absent is an error.
849- assert ! ( cmd_tui( None , None ) . is_err( ) ) ;
947+ assert ! ( cmd_tui( None , None , None ) . is_err( ) ) ;
850948 }
851949
852950 #[ test]
0 commit comments