From 3000886814a9db75149cd1374a1a3a6b562a9467 Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Thu, 5 Nov 2026 16:26:23 +0000 Subject: [PATCH 01/24] test(logs): pin how each log-tail reader behaves today Three modules read the last lines of a log their own way: the ComfyUI log view (whole file, sixteen-bit decode, ANSI strip, lossy text), the services text module (streamed, fails on invalid text) and the rocmd MCP service_logs tool (whole file into one string, fails on invalid text), while the seek-bounded routine in rocm-core serves only the engines. Add characterisation tests that pass on the unmodified code and document the differences before they are removed: a large file (the whole-file readers return a line far outside any tail window; the shared routine does not), a sixteen-bit little-endian log with ANSI colour codes, invalid eight-bit text, a missing file, a limit of zero, fewer lines than the limit, a final line without a trailing newline, and CRLF endings. Test-only; no behaviour change. Signed-off-by: Jussi Elo --- apps/rocm/src/comfyui.rs | 120 ++++++++++++++++++++++++++ apps/rocm/src/services_text.rs | 135 +++++++++++++++++++++++++++++ apps/rocmd/src/mcp.rs | 140 +++++++++++++++++++++++++++++++ crates/rocm-core/src/log_tail.rs | 62 ++++++++++++++ 4 files changed, 457 insertions(+) diff --git a/apps/rocm/src/comfyui.rs b/apps/rocm/src/comfyui.rs index 2d68ad6b81..f57db65f0e 100644 --- a/apps/rocm/src/comfyui.rs +++ b/apps/rocm/src/comfyui.rs @@ -997,6 +997,126 @@ mod tests { Ok(()) } + fn tail_fixture(name: &str, bytes: &[u8]) -> Result { + let paths = test_paths(name); + let dir = app_root(&paths).join("logs"); + fs::create_dir_all(&dir)?; + let path = dir.join("start-fixture.log"); + fs::write(&path, bytes)?; + Ok(path) + } + + fn utf16_le(text: &str, bom: bool) -> Vec { + let mut bytes = if bom { vec![0xff, 0xfe] } else { Vec::new() }; + for unit in text.encode_utf16() { + bytes.extend_from_slice(&unit.to_le_bytes()); + } + bytes + } + + /// 5 MiB: a marked first line, 5120 lines of 1 KiB, a marked last line. + fn large_log_with_head_marker() -> Vec { + let mut bytes = b"HEAD-MARKER\n".to_vec(); + let filler = format!("{}\n", "x".repeat(1023)); + for _ in 0..5120 { + bytes.extend_from_slice(filler.as_bytes()); + } + bytes.extend_from_slice(b"TAIL-MARKER\n"); + bytes + } + + #[test] + fn read_tail_lines_a_large_file_is_read_whole_today() -> Result<()> { + // Characterisation of the whole-file read: a line far outside any + // tail window is still returned when the limit is large enough. + let log = tail_fixture("comfyui-tail-large", &large_log_with_head_marker())?; + + let lines = read_tail_lines(&log, 10_000)?; + + assert_eq!(lines.first().map(String::as_str), Some("HEAD-MARKER")); + assert_eq!(lines.last().map(String::as_str), Some("TAIL-MARKER")); + assert_eq!(lines.len(), 5122); + Ok(()) + } + + #[test] + fn read_tail_lines_decodes_a_bomless_utf16_log_and_strips_ansi() -> Result<()> { + let text = "\u{1b}[32m[INFO]\u{1b}[0m one\r\n\u{1b}[1;31mtwo\u{1b}[0m\r\n"; + let log = tail_fixture("comfyui-tail-utf16-nobom", &utf16_le(text, false))?; + + let lines = read_tail_lines(&log, 5)?; + + assert_eq!(lines, vec!["[INFO] one", "two"]); + Ok(()) + } + + #[test] + fn read_tail_lines_strips_ansi_from_an_eight_bit_log() -> Result<()> { + let log = tail_fixture("comfyui-tail-ansi-utf8", b"\x1b[32mgreen\x1b[0m done\n")?; + + assert_eq!(read_tail_lines(&log, 5)?, vec!["green done"]); + Ok(()) + } + + #[test] + fn read_tail_lines_decodes_invalid_eight_bit_text_lossily() -> Result<()> { + let log = tail_fixture("comfyui-tail-invalid", b"ok\nbad\xffbyte\nlast\n")?; + + assert_eq!( + read_tail_lines(&log, 5)?, + vec!["ok", "bad\u{FFFD}byte", "last"] + ); + Ok(()) + } + + #[test] + fn read_tail_lines_missing_file_is_a_not_found_error() { + let paths = test_paths("comfyui-tail-missing"); + let missing = app_root(&paths).join("logs").join("absent.log"); + + let error = read_tail_lines(&missing, 5).expect_err("a missing log must not read"); + + let io_error = error + .downcast_ref::() + .expect("the bare io error is returned, without a path label"); + assert_eq!(io_error.kind(), io::ErrorKind::NotFound); + // The same applies to limit 0: the file is opened before the limit is + // looked at. + assert!(read_tail_lines(&missing, 0).is_err()); + } + + #[test] + fn read_tail_lines_limit_zero_returns_nothing_for_an_existing_log() -> Result<()> { + let log = tail_fixture("comfyui-tail-zero", b"a\nb\n")?; + + assert!(read_tail_lines(&log, 0)?.is_empty()); + Ok(()) + } + + #[test] + fn read_tail_lines_returns_every_line_when_fewer_than_the_limit() -> Result<()> { + let log = tail_fixture("comfyui-tail-few", b"a\nb\n")?; + + assert_eq!(read_tail_lines(&log, 50)?, vec!["a", "b"]); + Ok(()) + } + + #[test] + fn read_tail_lines_keeps_a_final_line_without_a_trailing_newline() -> Result<()> { + let log = tail_fixture("comfyui-tail-no-newline", b"a\nb\nc")?; + + assert_eq!(read_tail_lines(&log, 2)?, vec!["b", "c"]); + Ok(()) + } + + #[test] + fn read_tail_lines_strips_crlf_line_endings() -> Result<()> { + let log = tail_fixture("comfyui-tail-crlf", b"a\r\nb\r\n")?; + + assert_eq!(read_tail_lines(&log, 5)?, vec!["a", "b"]); + Ok(()) + } + #[test] fn unmanaged_running_url_reports_reachable_loopback_endpoint() -> Result<()> { let listener = TcpListener::bind("127.0.0.1:0")?; diff --git a/apps/rocm/src/services_text.rs b/apps/rocm/src/services_text.rs index abc8998950..5d50956f66 100644 --- a/apps/rocm/src/services_text.rs +++ b/apps/rocm/src/services_text.rs @@ -1596,6 +1596,141 @@ pub(crate) mod tests { test_paths, }; + fn tail_fixture(name: &str, bytes: &[u8]) -> Result { + let (root, _paths) = test_paths(name); + fs::create_dir_all(&root)?; + let path = root.join("service.log"); + fs::write(&path, bytes)?; + Ok(path) + } + + /// 5 MiB: a marked first line, 5120 lines of 1 KiB, a marked last line. + fn large_log_with_head_marker() -> Vec { + let mut bytes = b"HEAD-MARKER\n".to_vec(); + let filler = format!("{}\n", "x".repeat(1023)); + for _ in 0..5120 { + bytes.extend_from_slice(filler.as_bytes()); + } + bytes.extend_from_slice(b"TAIL-MARKER\n"); + bytes + } + + #[test] + fn read_tail_lines_a_large_file_is_read_whole_today() -> Result<()> { + // Characterisation of the whole-file stream: a line far outside any + // tail window is still returned when the limit is large enough. + let log = tail_fixture("services-tail-large", &large_log_with_head_marker())?; + + let lines = read_tail_lines(&log, 10_000, "service log")?; + + assert_eq!(lines.first().map(String::as_str), Some("HEAD-MARKER")); + assert_eq!(lines.last().map(String::as_str), Some("TAIL-MARKER")); + assert_eq!(lines.len(), 5122); + Ok(()) + } + + #[test] + fn read_tail_lines_fails_on_a_sixteen_bit_log_today() -> Result<()> { + let mut bytes = vec![0xff, 0xfe]; + for unit in "[INFO] one\r\n".encode_utf16() { + bytes.extend_from_slice(&unit.to_le_bytes()); + } + let log = tail_fixture("services-tail-utf16", &bytes)?; + + let error = read_tail_lines(&log, 5, "service log") + .expect_err("a sixteen-bit log is not valid eight-bit text"); + + assert_eq!( + error.to_string(), + format!("failed to read service log {}", log.display()) + ); + Ok(()) + } + + #[test] + fn read_tail_lines_keeps_ansi_colour_codes() -> Result<()> { + let log = tail_fixture("services-tail-ansi", b"\x1b[32mgreen\x1b[0m done\n")?; + + assert_eq!( + read_tail_lines(&log, 5, "service log")?, + vec!["\u{1b}[32mgreen\u{1b}[0m done"] + ); + Ok(()) + } + + #[test] + fn read_tail_lines_fails_on_invalid_eight_bit_text_today() -> Result<()> { + let log = tail_fixture("services-tail-invalid", b"ok\nbad\xffbyte\nlast\n")?; + + let error = read_tail_lines(&log, 5, "service log") + .expect_err("invalid UTF-8 aborts the whole read"); + + assert_eq!( + error.to_string(), + format!("failed to read service log {}", log.display()) + ); + // read_optional_tail_lines turns that error into one placeholder line + // instead of any of the readable lines. + assert_eq!( + read_optional_tail_lines(&log, 5, "service log"), + vec![format!( + "", + log.display() + )] + ); + Ok(()) + } + + #[test] + fn read_tail_lines_missing_file_is_a_labelled_open_error() { + let (root, _paths) = test_paths("services-tail-missing"); + let missing = root.join("absent.log"); + + let error = + read_tail_lines(&missing, 5, "service log").expect_err("a missing log must not read"); + + assert_eq!( + error.to_string(), + format!("failed to open service log {}", missing.display()) + ); + // read_optional_tail_lines guards on is_file first and stays quiet. + assert!(read_optional_tail_lines(&missing, 5, "service log").is_empty()); + } + + #[test] + fn read_tail_lines_limit_zero_returns_nothing_without_opening_the_file() { + let (root, _paths) = test_paths("services-tail-zero"); + + let lines = read_tail_lines(&root.join("absent.log"), 0, "service log") + .expect("limit 0 returns before the file is opened"); + + assert!(lines.is_empty()); + } + + #[test] + fn read_tail_lines_returns_every_line_when_fewer_than_the_limit() -> Result<()> { + let log = tail_fixture("services-tail-few", b"a\nb\n")?; + + assert_eq!(read_tail_lines(&log, 50, "service log")?, vec!["a", "b"]); + Ok(()) + } + + #[test] + fn read_tail_lines_keeps_a_final_line_without_a_trailing_newline() -> Result<()> { + let log = tail_fixture("services-tail-no-newline", b"a\nb\nc")?; + + assert_eq!(read_tail_lines(&log, 2, "service log")?, vec!["b", "c"]); + Ok(()) + } + + #[test] + fn read_tail_lines_strips_crlf_line_endings() -> Result<()> { + let log = tail_fixture("services-tail-crlf", b"a\r\nb\r\n")?; + + assert_eq!(read_tail_lines(&log, 5, "service log")?, vec!["a", "b"]); + Ok(()) + } + #[test] fn lemonade_stop_unloads_selected_model_over_http() -> Result<()> { use std::io::{Read, Write}; diff --git a/apps/rocmd/src/mcp.rs b/apps/rocmd/src/mcp.rs index e7b0b81de8..7fde8b705c 100644 --- a/apps/rocmd/src/mcp.rs +++ b/apps/rocmd/src/mcp.rs @@ -950,6 +950,146 @@ mod tests { Ok(()) } + fn tail_fixture(label: &str, bytes: &[u8]) -> Result { + let path = unique_test_path(&format!( + "rocmd-tail-{label}-{}.log", + crate::test_support::unique_suffix() + )); + fs::write(&path, bytes)?; + Ok(path) + } + + /// 5 MiB: a marked first line, 5120 lines of 1 KiB, a marked last line. + fn large_log_with_head_marker() -> Vec { + let mut bytes = b"HEAD-MARKER\n".to_vec(); + let filler = format!("{}\n", "x".repeat(1023)); + for _ in 0..5120 { + bytes.extend_from_slice(filler.as_bytes()); + } + bytes.extend_from_slice(b"TAIL-MARKER\n"); + bytes + } + + #[test] + fn read_tail_lines_a_large_file_is_read_whole_today() -> Result<()> { + // Characterisation of the whole-file read: a line far outside any + // tail window is still returned when the limit is large enough. + let path = tail_fixture("large", &large_log_with_head_marker())?; + + let tail = read_tail_lines(&path, 10_000)?; + fs::remove_file(&path)?; + + assert!(tail.starts_with("HEAD-MARKER\n"), "head line is returned"); + assert!(tail.ends_with("\nTAIL-MARKER")); + assert_eq!(tail.lines().count(), 5122); + Ok(()) + } + + #[test] + fn read_tail_lines_fails_on_a_sixteen_bit_log_today() -> Result<()> { + let mut bytes = vec![0xff, 0xfe]; + for unit in "[INFO] one\r\n".encode_utf16() { + bytes.extend_from_slice(&unit.to_le_bytes()); + } + let path = tail_fixture("utf16", &bytes)?; + + let error = read_tail_lines(&path, 5).expect_err("not valid eight-bit text"); + fs::remove_file(&path)?; + + assert_eq!( + error.to_string(), + format!("failed to read {}", path.display()) + ); + Ok(()) + } + + #[test] + fn read_tail_lines_keeps_ansi_colour_codes() -> Result<()> { + let path = tail_fixture("ansi", b"\x1b[32mgreen\x1b[0m done\n")?; + + let tail = read_tail_lines(&path, 5)?; + fs::remove_file(&path)?; + + assert_eq!(tail, "\u{1b}[32mgreen\u{1b}[0m done"); + Ok(()) + } + + #[test] + fn read_tail_lines_fails_on_invalid_eight_bit_text_today() -> Result<()> { + let path = tail_fixture("invalid", b"ok\nbad\xffbyte\nlast\n")?; + + let error = read_tail_lines(&path, 5).expect_err("invalid UTF-8 aborts the read"); + fs::remove_file(&path)?; + + assert_eq!( + error.to_string(), + format!("failed to read {}", path.display()) + ); + Ok(()) + } + + #[test] + fn read_tail_lines_missing_file_is_a_labelled_read_error() { + let path = unique_test_path(&format!( + "rocmd-tail-missing-{}.log", + crate::test_support::unique_suffix() + )); + + let error = read_tail_lines(&path, 5).expect_err("a missing log must not read"); + + assert_eq!( + error.to_string(), + format!("failed to read {}", path.display()) + ); + } + + #[test] + fn read_tail_lines_limit_zero_returns_every_line_today() -> Result<()> { + // A latent bug rather than a contract: with limit 0 the window never + // evicts, so the whole file comes back. The tool handler clamps the + // limit to 1..=500, so no caller reaches it. + let path = tail_fixture("zero", b"a\nb\nc\n")?; + + let tail = read_tail_lines(&path, 0)?; + fs::remove_file(&path)?; + + assert_eq!(tail, "a\nb\nc"); + Ok(()) + } + + #[test] + fn read_tail_lines_returns_every_line_when_fewer_than_the_limit() -> Result<()> { + let path = tail_fixture("few", b"a\nb\n")?; + + let tail = read_tail_lines(&path, 50)?; + fs::remove_file(&path)?; + + assert_eq!(tail, "a\nb"); + Ok(()) + } + + #[test] + fn read_tail_lines_keeps_a_final_line_without_a_trailing_newline() -> Result<()> { + let path = tail_fixture("no-newline", b"a\nb\nc")?; + + let tail = read_tail_lines(&path, 2)?; + fs::remove_file(&path)?; + + assert_eq!(tail, "b\nc"); + Ok(()) + } + + #[test] + fn read_tail_lines_strips_crlf_line_endings() -> Result<()> { + let path = tail_fixture("crlf", b"a\r\nb\r\n")?; + + let tail = read_tail_lines(&path, 5)?; + fs::remove_file(&path)?; + + assert_eq!(tail, "a\nb"); + Ok(()) + } + fn mcp_stop_server(paths: &AppPaths, service_id: &str) -> Result { handle_mcp_tool_call( paths, diff --git a/crates/rocm-core/src/log_tail.rs b/crates/rocm-core/src/log_tail.rs index 9ab1fb93f9..c5667a795c 100644 --- a/crates/rocm-core/src/log_tail.rs +++ b/crates/rocm-core/src/log_tail.rs @@ -219,6 +219,68 @@ mod tests { Ok(()) } + #[test] + fn tail_lines_on_a_large_file_returns_only_whole_lines_from_the_window() -> Result<()> { + // 1 MiB of 1 KiB lines under a 64 KiB budget, with a marked first and + // last line. The first line lies far outside the window, so a read + // that returned it was not bounded; the window starts mid-line (the + // budget is not a multiple of the line length), so a leading fragment + // that was kept would be shorter than a whole line. + let mut content = String::from("HEAD-MARKER\n"); + let filler = format!("{}\n", "x".repeat(1023)); + for _ in 0..1024 { + content.push_str(&filler); + } + content.push_str("TAIL-MARKER\n"); + let path = + std::env::temp_dir().join(format!("rocm-core-tail-large-{}.log", unique_suffix())); + fs::write(&path, &content)?; + + let lines = tail_lines(&path, usize::MAX, 64 * 1024 + 7)?; + fs::remove_file(&path).ok(); + + assert!(!lines.iter().any(|line| line == "HEAD-MARKER")); + assert_eq!(lines.last().map(String::as_str), Some("TAIL-MARKER")); + assert!(lines.len() <= 65, "got {} lines", lines.len()); + for line in &lines[..lines.len() - 1] { + assert_eq!(line.len(), 1023, "a partial leading fragment was kept"); + } + Ok(()) + } + + #[test] + fn tail_lines_missing_file_is_an_open_error_naming_the_path() { + let path = Path::new("/does/not/exist/rocm-core-tail-missing.log"); + + let error = tail_lines(path, 5, TEST_MAX_TAIL_READ).expect_err("missing file"); + + assert_eq!( + error.to_string(), + format!("failed to open {}", path.display()) + ); + } + + #[test] + fn tail_lines_returns_every_line_when_fewer_than_the_limit() -> Result<()> { + let path = std::env::temp_dir().join(format!("rocm-core-tail-few-{}.log", unique_suffix())); + fs::write(&path, "a\nb\n")?; + let lines = tail_lines(&path, 50, TEST_MAX_TAIL_READ)?; + fs::remove_file(&path).ok(); + assert_eq!(lines, vec!["a".to_owned(), "b".to_owned()]); + Ok(()) + } + + #[test] + fn tail_lines_keeps_a_final_line_without_a_trailing_newline() -> Result<()> { + let path = + std::env::temp_dir().join(format!("rocm-core-tail-no-nl-{}.log", unique_suffix())); + fs::write(&path, "a\nb\nc")?; + let lines = tail_lines(&path, 2, TEST_MAX_TAIL_READ)?; + fs::remove_file(&path).ok(); + assert_eq!(lines, vec!["b".to_owned(), "c".to_owned()]); + Ok(()) + } + #[test] fn tail_lines_limit_zero_returns_empty_without_opening_file() -> Result<()> { let path = Path::new("/does/not/exist/rocm-core-tail-limit-zero.log"); From fce592c430476c749f9ee9043ffe1c49339d31ca Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Thu, 5 Nov 2026 16:49:49 +0000 Subject: [PATCH 02/24] fix(logs): read every log tail through one bounded, lossy routine The ComfyUI log view, the services text module and the rocmd MCP service_logs tool each read a log tail their own way, while the seek-bounded rocm_core::tail_lines served only the engines. Converge all of them on it. rocm_core::log_tail gains tail_lines_decoded, taking a LogDecode: PLAIN (UTF-8, what the engines and the service logs use) or CONSOLE (UTF-16 little-endian detected by byte order mark or zero-byte pattern, ANSI sequences stripped; what the ComfyUI log view used). The UTF-16 path keeps the seek-boundary handling: the window starts on a character boundary and a partial first line is dropped. strip_ansi_sequences is exported for the assistant-prose note in tool_policy. A shared LOG_VIEW_MAX_TAIL_READ budget (4 MiB) is what the three log views pass. The private readers are deleted; each caller keeps a thin adapter that picks a LogDecode and its own error label. User-visible differences: - A log larger than 4 MiB is read from its last 4 MiB, not whole (all three). - Invalid UTF-8 no longer fails the view: `rocm services logs` and `rocm logs` showed one "" placeholder line and the MCP service_logs tool returned an error; both now show the readable text with U+FFFD for the bad bytes. - Service logs and the MCP tool still read UTF-8 and keep ANSI sequences; a UTF-16 service log is now shown as lossy text rather than as an error. - ComfyUI logs: ANSI stripping is per line, so an unterminated escape sequence ends at its line instead of swallowing the lines after it. - Error wording: a missing log is "failed to read