Something went wrong. Try again.
Personal fork of prime agent. Mirror of: https://mnemosyne.sigilzero.dev/sigilzero/prime-agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255//! Log-source tests over a fixture agent dir, asserted on the composed//! report text the command prints.
use super::*;
fn supervisor_line(ts: &str, msg: &str) -> String { format!( r#"{{"ts":"{ts}","level":"warn","component":"coding-agent.daemon-supervisor","pid":15026,"msg":{}}}"#, serde_json::json!(msg) )}
/// The composed report text for a window over the redirected agent dir.fn report_text(since: &str, until: &str) -> String { let options = IncidentCommandOptions { since: Some(since.to_string()), until: Some(until.to_string()), session: None, }; let now_ms = crate::util_time::now_ms() as i64; incident_report_text(&options, None, now_ms) .expect("the window resolves") .expect("a report")}
/// The env-lock serializes the whole body: one lock covers the env var/// swap AND the reads; the previous value is restored afterwards.fn with_agent_dir(body: impl FnOnce(&Path)) { let _guard = crate::config::env_lock(); let previous = std::env::var_os(crate::config::ENV_AGENT_DIR); let dir = tempfile::TempDir::new().expect("temp dir"); let agent_dir = dir.path().to_path_buf(); std::fs::create_dir_all(agent_dir.join("logs")).expect("logs dir"); std::env::set_var(crate::config::ENV_AGENT_DIR, &agent_dir); body(&agent_dir); match previous { Some(previous) => std::env::set_var(crate::config::ENV_AGENT_DIR, previous), None => std::env::remove_var(crate::config::ENV_AGENT_DIR), }}
#[test]fn tolerates_malformed_lines_and_reports_them_as_skipped() { with_agent_dir(|agent_dir| { let good = supervisor_line( "2026-09-10T20:02:39.764Z", "Supervisor command attach failed: Error: Timed out waiting for daemon worker response to attach", ); let contents = [ "this is not json", r#"{"noTs":true,"msg":"missing ts"}"#, good.as_str(), "", ] .join("\n"); std::fs::write(agent_dir.join("logs/agent.jsonl"), format!("{contents}\n")) .expect("write log"); let text = report_text("2026-09-10T20:00", "2026-09-10T20:30"); assert!(text.contains("command attach failed: timed out waiting for worker response")); assert!(text.contains("3 lines scanned, 1 events in window, 2 unreadable skipped")); assert!(text.contains("Source: ")); assert!(text.contains("agent.jsonl")); });}
/// A torn multi-byte write at the live log's tail must not cost the whole scan (Cursor/// Bugbot: `read_to_string` dropped the file on invalid UTF-8); the torn line fails the/// parse like any non-line.#[test]fn keeps_structured_lines_around_a_torn_multibyte_tail() { with_agent_dir(|agent_dir| { let listening = supervisor_line( "2026-09-10T20:02:39.764Z", "Prime Agent daemon supervisor e14de15c listening on /tmp/prime-agent-501/daemon.sock", ); // The stderr-forward classifier keys on a 12-hex worker id // (STDERR_FORWARD), so the fixture carries one. let crash = supervisor_line( "2026-09-10T20:05:00.000Z", "Session worker 5b1d3aeb91ee stderr: uncaught exception: Error: write EPIPE", ); // A third line cut two bytes into its three-byte U+26A0: the // file is invalid UTF-8 from the write tear onward. let torn_line: &[u8] = br#"{"ts":"2026-09-10T20:06:00.000Z","msg":"daemon restarting "#; let contents: Vec<u8> = [ format!("{listening}\n{crash}\n").as_bytes(), torn_line, &[0xE2, 0x9A], ] .concat(); std::fs::write(agent_dir.join("logs/agent.jsonl"), contents).expect("write log"); let text = report_text("2026-09-10T20:00", "2026-09-10T20:30"); assert!( text.contains("worker 5b1d3aeb91ee crashed: uncaught exception: Error: write EPIPE"), "{text}" ); assert!( text.contains("3 lines scanned, 2 events in window, 1 unreadable skipped"), "{text}" ); });}
/// Unix-only: the decoy is a directory with a future mtime via `File::set_times`,/// which Windows cannot open.#[cfg(unix)]#[test]fn falls_back_to_the_newest_per_daemon_log_when_agent_jsonl_is_absent() { with_agent_dir(|agent_dir| { std::fs::write( agent_dir.join("logs/old-sock.a1b2c3d4.log"), "[2026-09-09T10:00:00.000Z] supervisor: Daemon supervisor startup failed: Error: Lock file is already being held\n", ) .expect("write old log"); std::fs::write( agent_dir.join("logs/daemon.sock.98ed5cb2.log"), [ "[2026-09-10T20:00:05.000Z] supervisor: Daemon supervisor startup failed: Error: Lock file is already being held", "[2026-09-10T20:23:24.945Z] uncaught exception: Error: write EPIPE", "[2026-09-10T20:23:29.521Z] supervisor: Recovered worker 5b1d3aeb91ee without replaying uncertain operations: tool_execution_start, agent_end", ] .join("\n"), ) .expect("write newest log"); // A future-dated DIRECTORY matching the log pattern must not be // picked as the fallback, or it would hide both valid daemon logs // behind a failed read. The newest log's mtime is pinned ahead of // the older one (and behind the decoy) so the pick never rides // write-order timing. let decoy_dir = agent_dir.join("logs/daemon.sock.deadbeef.log"); std::fs::create_dir(&decoy_dir).expect("decoy dir"); let decoy = std::fs::File::open(&decoy_dir).expect("open decoy"); let future = std::time::SystemTime::now() + std::time::Duration::from_secs(60); decoy .set_times(std::fs::FileTimes::new().set_modified(future)) .expect("future mtime"); let newest = std::fs::File::open(agent_dir.join("logs/daemon.sock.98ed5cb2.log")) .expect("open newest log"); newest .set_times( std::fs::FileTimes::new().set_modified( std::time::SystemTime::now() + std::time::Duration::from_secs(30), ), ) .expect("newest mtime"); let text = report_text("2026-09-10T20:00", "2026-09-10T20:30"); assert!(text.contains("daemon.sock.98ed5cb2.log"), "{text}"); assert!(text.contains("supervisor startup blocked: another daemon holds the lock")); assert!( text.contains( "worker 5b1d3aeb91ee recovered; 2 uncertain operations not replayed (agent_end x1, tool_execution_start x1)" ), "{text}" ); });}
#[test]fn falls_back_to_daemon_logs_whose_socket_basename_has_no_sock_suffix() { // A daemon started with `--daemon-socket /tmp/prime-daemon` writes // prime-daemon.<hash>.log, which has no .sock segment to match on. with_agent_dir(|agent_dir| { std::fs::write( agent_dir.join("logs/prime-daemon.a1b2c3d4.log"), "[2026-09-10T20:00:05.000Z] supervisor: Daemon supervisor startup failed: Error: Lock file is already being held\n", ) .expect("write log"); let text = report_text("2026-09-10T20:00", "2026-09-10T20:30"); assert!(text.contains("prime-daemon.a1b2c3d4.log"), "{text}"); assert!(text.contains("supervisor startup blocked: another daemon holds the lock")); });}
#[test]fn falls_back_when_agent_jsonl_is_empty_or_unreadable() { with_agent_dir(|agent_dir| { let daemon_log = [ "[2026-09-10T20:23:24.945Z] uncaught exception: Error: write EPIPE", "[2026-09-10T20:23:29.521Z] supervisor: Recovered worker 5b1d3aeb91ee without replaying uncertain operations: tool_execution_start, agent_end", ] .join("\n"); std::fs::write( agent_dir.join("logs/daemon.sock.98ed5cb2.log"), format!("{daemon_log}\n"), ) .expect("write daemon log"); // Unreadable agent.jsonl: nothing parses. std::fs::write( agent_dir.join("logs/agent.jsonl"), "this is not json\nalso not json\n", ) .expect("write broken log"); let text = report_text("2026-09-10T20:00", "2026-09-10T20:30"); assert!(text.contains("daemon.sock.98ed5cb2.log"), "{text}"); assert!( text.contains("worker crashed: uncaught exception: Error: write EPIPE"), "{text}" ); assert!(text.contains("worker 5b1d3aeb91ee recovered; 2 uncertain operations not replayed")); // Empty agent.jsonl also triggers the fallback. std::fs::write(agent_dir.join("logs/agent.jsonl"), "").expect("truncate log"); let text = report_text("2026-09-10T20:00", "2026-09-10T20:30"); assert!(text.contains("daemon.sock.98ed5cb2.log"), "{text}"); assert!( text.contains("worker crashed: uncaught exception: Error: write EPIPE"), "{text}" ); });}
#[test]fn does_not_fall_back_when_agent_jsonl_parses_but_has_no_in_window_events() { with_agent_dir(|agent_dir| { let out_of_window = supervisor_line( "2026-09-01T10:00:00.000Z", "Supervisor command attach failed: Error: Timed out waiting for daemon worker response to attach", ); std::fs::write( agent_dir.join("logs/agent.jsonl"), format!("{out_of_window}\n"), ) .expect("write log"); std::fs::write( agent_dir.join("logs/daemon.sock.98ed5cb2.log"), "[2026-09-10T20:23:24.945Z] supervisor: Supervisor command attach failed: Error: Timed out waiting for daemon worker response to attach\n", ) .expect("write daemon log"); let text = report_text("2026-09-10T20:00", "2026-09-10T20:30"); assert!(text.contains("agent.jsonl"), "{text}"); assert!(!text.contains("daemon.sock.98ed5cb2.log"), "{text}"); assert!(text.contains("(none)"), "{text}"); });}
#[test]fn reports_missing_logs_clearly() { with_agent_dir(|_agent_dir| { // No logs at all: the command prints the missing-logs message // instead of an empty timeline. let options = IncidentCommandOptions { since: Some("2026-09-10T20:00".to_string()), until: Some("2026-09-10T20:30".to_string()), session: None, }; let now_ms = crate::util_time::now_ms() as i64; assert!(incident_report_text(&options, None, now_ms) .expect("the window resolves") .is_none()); let source = read_incident_log_entries(); assert!(source.entries.is_empty()); assert_eq!(source.scanned_count, 0); assert_eq!(source.source, ""); });}