From a72070f87e8bbe7a096c8a9f07ba763c8698122e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 31 Aug 2026 18:13:22 +0000 Subject: [PATCH 1/2] test(hotpath): add slice coverage contracts for sessions and lcm Co-authored-by: Zack Jackson --- .../tests/hotpath_coverage.rs | 153 ++++++++++++++ .../tracedecay-lcm/tests/hotpath_coverage.rs | 168 +++++++++++++++ .../tests/hotpath_coverage.rs | 187 +++++++++++++++++ .../tests/hotpath_coverage.rs | 197 ++++++++++++++++++ .../tests/hotpath_coverage.rs | 169 +++++++++++++++ .../tests/hotpath_coverage.rs | 173 +++++++++++++++ .../tests/hotpath_coverage.rs | 182 ++++++++++++++++ 7 files changed, 1229 insertions(+) create mode 100644 crates/tracedecay-capture/tests/hotpath_coverage.rs create mode 100644 crates/tracedecay-lcm/tests/hotpath_coverage.rs create mode 100644 crates/tracedecay-session-memory/tests/hotpath_coverage.rs create mode 100644 crates/tracedecay-session-runtime/tests/hotpath_coverage.rs create mode 100644 crates/tracedecay-session-temporal-store/tests/hotpath_coverage.rs create mode 100644 crates/tracedecay-sessions/tests/hotpath_coverage.rs create mode 100644 crates/tracedecay-temporal-query/tests/hotpath_coverage.rs diff --git a/crates/tracedecay-capture/tests/hotpath_coverage.rs b/crates/tracedecay-capture/tests/hotpath_coverage.rs new file mode 100644 index 0000000000..0024a70ea4 --- /dev/null +++ b/crates/tracedecay-capture/tests/hotpath_coverage.rs @@ -0,0 +1,153 @@ +//! Hotpath coverage contract for `tracedecay-capture`. +//! +//! Feature-off (default build): every hotpath macro must be a no-op — no +//! metrics listener on 6770/6771, no report file even when the report +//! environment is set, and `hotpath` must stay out of the crate's default +//! features. +//! +//! Feature-on (`--features hotpath`): a process-boundary guard must capture +//! this crate's measured parse sites in a functions-timing report, proving +//! the instrumentation is real rather than dead configuration. + +use serde_json::json; +use tracedecay_capture::parse_claude_record_v1; +use tracedecay_domain::ClaudeByteRangeV1; + +/// Deterministic, daemon-free workload that reaches this crate's measured +/// parse path (`capture.parse.record` and `capture.parse.record_digest`). +fn run_capture_parse_workload() -> usize { + let record = serde_json::to_vec(&json!({ + "type": "assistant", + "message": { "content": "hotpath coverage fixture" }, + })) + .expect("serialize claude record fixture"); + let range = ClaudeByteRangeV1::new(0, record.len() as u64).expect("valid fixture byte range"); + let parsed = parse_claude_record_v1(&record, range).expect("parse claude record fixture"); + assert_eq!(parsed.encoded_len(), record.len()); + assert_eq!( + parsed.value()["message"]["content"], + "hotpath coverage fixture" + ); + parsed.encoded_len() +} + +/// Collects the entries of one feature array (for example `default`) from +/// this crate's manifest, tolerating multi-line arrays. Returns `None` when +/// the feature is not declared at all. +fn manifest_feature_array(manifest: &str, feature: &str) -> Option { + let mut in_features = false; + let mut collecting = false; + let mut collected = String::new(); + for line in manifest.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_features = trimmed == "[features]"; + continue; + } + if collecting { + collected.push_str(trimmed); + if trimmed.contains(']') { + return Some(collected); + } + continue; + } + if in_features + && let Some(rest) = trimmed.strip_prefix(feature) + && let Some(array) = rest.trim_start().strip_prefix('=') + { + collected.push_str(array.trim()); + if collected.contains(']') { + return Some(collected); + } + collecting = true; + } + } + None +} + +/// The profiling features must remain opt-in: neither `default` nor any +/// production-shaped feature set of this crate may pull in hotpath. +#[test] +fn hotpath_stays_out_of_default_and_production_features() { + let manifest = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")) + .expect("read crate manifest"); + for gate in ["default", "production"] { + if let Some(entries) = manifest_feature_array(&manifest, gate) { + assert!( + !entries.contains("hotpath"), + "feature `{gate}` must never enable hotpath, found: {entries}" + ); + } + } +} + +#[cfg(not(feature = "hotpath"))] +mod feature_off { + use std::net::TcpStream; + use std::path::Path; + + /// With the feature off the macros expand to their primary expression: + /// the workload behaves identically, the report environment is ignored, + /// and no metrics listener appears. + #[test] + fn workload_is_a_no_op_for_profiling() { + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("capture-hotpath-off.json"); + let _ = std::fs::remove_file(&report); + // SAFETY: single-threaded with respect to readers — the feature-off + // build contains no hotpath runtime and nothing else in this test + // binary reads these variables. + unsafe { + std::env::set_var("HOTPATH_OUTPUT_FORMAT", "json"); + std::env::set_var("HOTPATH_OUTPUT_PATH", &report); + } + + assert!(super::run_capture_parse_workload() > 0); + + assert!( + !report.exists(), + "feature-off build must never write a hotpath report" + ); + for port in [6770u16, 6771] { + assert!( + TcpStream::connect(("127.0.0.1", port)).is_err(), + "feature-off build must not expose a hotpath listener on port {port} \ + (a listener here means another process on this machine is serving it)" + ); + } + } +} + +#[cfg(feature = "hotpath")] +mod feature_on { + use std::path::Path; + + /// A guard-scoped run of the same workload must record this crate's + /// measured sites, proving `--features hotpath` produces live + /// instrumentation and not an empty report. + #[test] + fn guard_report_captures_measured_parse_sites() { + // SAFETY: set before the first guard build in this process, which is + // the only reader; the metrics listener must stay off in tests. + unsafe { std::env::set_var("HOTPATH_METRICS_SERVER_OFF", "1") }; + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("capture-hotpath-on.json"); + let _ = std::fs::remove_file(&report); + + { + let _guard = hotpath::HotpathGuardBuilder::new("capture-hotpath-coverage") + .format(hotpath::Format::Json) + .output_path(&report) + .report("functions-timing") + .build(); + assert!(super::run_capture_parse_workload() > 0); + } + + let report_text = + std::fs::read_to_string(&report).expect("feature-on guard drop must write a report"); + for label in ["capture.parse.record", "capture.parse.record_digest"] { + assert!( + report_text.contains(label), + "hotpath report must capture measured site `{label}`: {report_text}" + ); + } + } +} diff --git a/crates/tracedecay-lcm/tests/hotpath_coverage.rs b/crates/tracedecay-lcm/tests/hotpath_coverage.rs new file mode 100644 index 0000000000..24e2c9d784 --- /dev/null +++ b/crates/tracedecay-lcm/tests/hotpath_coverage.rs @@ -0,0 +1,168 @@ +//! Hotpath coverage contract for `tracedecay-lcm`. +//! +//! Feature-off (default build): every hotpath macro must be a no-op — no +//! metrics listener on 6770/6771, no report file even when the report +//! environment is set, and `hotpath` must stay out of the crate's default +//! features. +//! +//! Feature-on (`--features hotpath`): a process-boundary guard must capture +//! this crate's measured security-scan and compression-policy sites in a +//! functions-timing report, proving the instrumentation is real rather than +//! dead configuration. + +use serde_json::json; +use tracedecay_lcm::compression_policy::{ + OverflowRecoveryCapInput, overflow_recovery_assembly_cap, +}; +use tracedecay_lcm::security::{long_base64_run_spans, quarantine_reason}; + +/// Deterministic, daemon-free workload that reaches this crate's measured +/// sites: `sessions.lcm.scan_base64`, `sessions.lcm.scan_repetition`, and +/// `sessions.lcm.overflow_cap`. +fn run_lcm_policy_workload() -> usize { + let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let base64_run = alphabet.repeat(64); + let spans = long_base64_run_spans(&base64_run); + assert!(!spans.is_empty(), "fixture must contain a long base64 run"); + + let repeated = + "same repeated assistant diagnostic segment with very low novelty.\n".repeat(1_200); + assert_eq!( + quarantine_reason("assistant", Some("message"), &repeated), + Some("high_repetition"), + ); + + let cap = overflow_recovery_assembly_cap(OverflowRecoveryCapInput { + current_tokens: Some(8), + max_assembly_tokens: Some(10), + messages: &[json!({ "content": "two tokens" })], + }); + assert!(cap.is_some(), "bounded overflow input must produce a cap"); + + spans.len() +} + +/// Collects the entries of one feature array (for example `default`) from +/// this crate's manifest, tolerating multi-line arrays. Returns `None` when +/// the feature is not declared at all. +fn manifest_feature_array(manifest: &str, feature: &str) -> Option { + let mut in_features = false; + let mut collecting = false; + let mut collected = String::new(); + for line in manifest.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_features = trimmed == "[features]"; + continue; + } + if collecting { + collected.push_str(trimmed); + if trimmed.contains(']') { + return Some(collected); + } + continue; + } + if in_features + && let Some(rest) = trimmed.strip_prefix(feature) + && let Some(array) = rest.trim_start().strip_prefix('=') + { + collected.push_str(array.trim()); + if collected.contains(']') { + return Some(collected); + } + collecting = true; + } + } + None +} + +/// The profiling features must remain opt-in: neither `default` nor any +/// production-shaped feature set of this crate may pull in hotpath. +#[test] +fn hotpath_stays_out_of_default_and_production_features() { + let manifest = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")) + .expect("read crate manifest"); + for gate in ["default", "production"] { + if let Some(entries) = manifest_feature_array(&manifest, gate) { + assert!( + !entries.contains("hotpath"), + "feature `{gate}` must never enable hotpath, found: {entries}" + ); + } + } +} + +#[cfg(not(feature = "hotpath"))] +mod feature_off { + use std::net::TcpStream; + use std::path::Path; + + /// With the feature off the macros expand to their primary expression: + /// the workload behaves identically, the report environment is ignored, + /// and no metrics listener appears. + #[test] + fn workload_is_a_no_op_for_profiling() { + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("lcm-hotpath-off.json"); + let _ = std::fs::remove_file(&report); + // SAFETY: single-threaded with respect to readers — the feature-off + // build contains no hotpath runtime and nothing else in this test + // binary reads these variables. + unsafe { + std::env::set_var("HOTPATH_OUTPUT_FORMAT", "json"); + std::env::set_var("HOTPATH_OUTPUT_PATH", &report); + } + + assert!(super::run_lcm_policy_workload() > 0); + + assert!( + !report.exists(), + "feature-off build must never write a hotpath report" + ); + for port in [6770u16, 6771] { + assert!( + TcpStream::connect(("127.0.0.1", port)).is_err(), + "feature-off build must not expose a hotpath listener on port {port} \ + (a listener here means another process on this machine is serving it)" + ); + } + } +} + +#[cfg(feature = "hotpath")] +mod feature_on { + use std::path::Path; + + /// A guard-scoped run of the same workload must record this crate's + /// measured sites, proving `--features hotpath` produces live + /// instrumentation and not an empty report. + #[test] + fn guard_report_captures_measured_policy_sites() { + // SAFETY: set before the first guard build in this process, which is + // the only reader; the metrics listener must stay off in tests. + unsafe { std::env::set_var("HOTPATH_METRICS_SERVER_OFF", "1") }; + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("lcm-hotpath-on.json"); + let _ = std::fs::remove_file(&report); + + { + let _guard = hotpath::HotpathGuardBuilder::new("lcm-hotpath-coverage") + .format(hotpath::Format::Json) + .output_path(&report) + .report("functions-timing") + .build(); + assert!(super::run_lcm_policy_workload() > 0); + } + + let report_text = + std::fs::read_to_string(&report).expect("feature-on guard drop must write a report"); + for label in [ + "sessions.lcm.scan_base64", + "sessions.lcm.scan_repetition", + "sessions.lcm.overflow_cap", + ] { + assert!( + report_text.contains(label), + "hotpath report must capture measured site `{label}`: {report_text}" + ); + } + } +} diff --git a/crates/tracedecay-session-memory/tests/hotpath_coverage.rs b/crates/tracedecay-session-memory/tests/hotpath_coverage.rs new file mode 100644 index 0000000000..afb8ed37da --- /dev/null +++ b/crates/tracedecay-session-memory/tests/hotpath_coverage.rs @@ -0,0 +1,187 @@ +//! Hotpath coverage contract for `tracedecay-session-memory`. +//! +//! Feature-off (default build): every hotpath macro must be a no-op — no +//! metrics listener on 6770/6771, no report file even when the report +//! environment is set, and `hotpath` must stay out of the crate's default +//! features. +//! +//! Feature-on (`--features hotpath`): a process-boundary guard must capture +//! this crate's measured session-grant and memory-command sites in a +//! functions-timing report, proving the instrumentation is real rather than +//! dead configuration. + +use tracedecay_domain::{FactCategoryV1, FactOwnerV1, ProjectId}; +use tracedecay_session_memory::context::{ + CancellationToken, CapabilityDigest, ConfigurationDigest, PolicyDigest, RequestBudgets, + session_application_grant_digest, +}; +use tracedecay_session_memory::memory::{ProjectMemoryFactAddRequest, automatic_fact_add_command}; + +/// Deterministic, daemon-free workload that reaches this crate's measured +/// sites: `usecases.context.session_grant` and +/// `usecases.memory.automatic.command`. +fn run_session_memory_workload() -> usize { + const DIGEST: [u8; 32] = [0x5a; 32]; + let budgets = + RequestBudgets::new(64, 64 * 1024 * 1024, 10_000).expect("non-zero request budgets"); + let cancellation = CancellationToken::for_application_request("request.hotpath-coverage"); + session_application_grant_digest( + CapabilityDigest::new(DIGEST), + PolicyDigest::new(DIGEST), + ConfigurationDigest::new(DIGEST), + &cancellation, + budgets, + ) + .expect("derive session application grant digest"); + + let owner = FactOwnerV1::Project { + project_id: ProjectId::new("project.memory.hotpath-coverage").expect("valid project id"), + }; + let request = ProjectMemoryFactAddRequest { + content: "canonical hotpath coverage fixture".into(), + category: FactCategoryV1::Project, + source_label: None, + tags: Vec::new(), + entities: Vec::new(), + trust: None, + metadata: serde_json::json!({}), + }; + let command = automatic_fact_add_command( + owner, + request, + "run_01J4A7P5MQ1X9DX2P9BQNQW75T", + "automatic-fact-hotpath-coverage", + None, + ) + .expect("build automatic fact add command"); + assert_eq!( + command.automation_run_id(), + Some("run_01J4A7P5MQ1X9DX2P9BQNQW75T") + ); + + 1 +} + +/// Collects the entries of one feature array (for example `default`) from +/// this crate's manifest, tolerating multi-line arrays. Returns `None` when +/// the feature is not declared at all. +fn manifest_feature_array(manifest: &str, feature: &str) -> Option { + let mut in_features = false; + let mut collecting = false; + let mut collected = String::new(); + for line in manifest.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_features = trimmed == "[features]"; + continue; + } + if collecting { + collected.push_str(trimmed); + if trimmed.contains(']') { + return Some(collected); + } + continue; + } + if in_features + && let Some(rest) = trimmed.strip_prefix(feature) + && let Some(array) = rest.trim_start().strip_prefix('=') + { + collected.push_str(array.trim()); + if collected.contains(']') { + return Some(collected); + } + collecting = true; + } + } + None +} + +/// The profiling features must remain opt-in: neither `default` nor any +/// production-shaped feature set of this crate may pull in hotpath. +#[test] +fn hotpath_stays_out_of_default_and_production_features() { + let manifest = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")) + .expect("read crate manifest"); + for gate in ["default", "production"] { + if let Some(entries) = manifest_feature_array(&manifest, gate) { + assert!( + !entries.contains("hotpath"), + "feature `{gate}` must never enable hotpath, found: {entries}" + ); + } + } +} + +#[cfg(not(feature = "hotpath"))] +mod feature_off { + use std::net::TcpStream; + use std::path::Path; + + /// With the feature off the macros expand to their primary expression: + /// the workload behaves identically, the report environment is ignored, + /// and no metrics listener appears. + #[test] + fn workload_is_a_no_op_for_profiling() { + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("session-memory-hotpath-off.json"); + let _ = std::fs::remove_file(&report); + // SAFETY: single-threaded with respect to readers — the feature-off + // build contains no hotpath runtime and nothing else in this test + // binary reads these variables. + unsafe { + std::env::set_var("HOTPATH_OUTPUT_FORMAT", "json"); + std::env::set_var("HOTPATH_OUTPUT_PATH", &report); + } + + assert!(super::run_session_memory_workload() > 0); + + assert!( + !report.exists(), + "feature-off build must never write a hotpath report" + ); + for port in [6770u16, 6771] { + assert!( + TcpStream::connect(("127.0.0.1", port)).is_err(), + "feature-off build must not expose a hotpath listener on port {port} \ + (a listener here means another process on this machine is serving it)" + ); + } + } +} + +#[cfg(feature = "hotpath")] +mod feature_on { + use std::path::Path; + + /// A guard-scoped run of the same workload must record this crate's + /// measured sites, proving `--features hotpath` produces live + /// instrumentation and not an empty report. + #[test] + fn guard_report_captures_measured_memory_sites() { + // SAFETY: set before the first guard build in this process, which is + // the only reader; the metrics listener must stay off in tests. + unsafe { std::env::set_var("HOTPATH_METRICS_SERVER_OFF", "1") }; + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("session-memory-hotpath-on.json"); + let _ = std::fs::remove_file(&report); + + { + let _guard = hotpath::HotpathGuardBuilder::new("session-memory-hotpath-coverage") + .format(hotpath::Format::Json) + .output_path(&report) + .report("functions-timing") + .build(); + assert!(super::run_session_memory_workload() > 0); + } + + let report_text = + std::fs::read_to_string(&report).expect("feature-on guard drop must write a report"); + for label in [ + "usecases.context.session_grant", + "usecases.memory.automatic.command", + ] { + assert!( + report_text.contains(label), + "hotpath report must capture measured site `{label}`: {report_text}" + ); + } + } +} diff --git a/crates/tracedecay-session-runtime/tests/hotpath_coverage.rs b/crates/tracedecay-session-runtime/tests/hotpath_coverage.rs new file mode 100644 index 0000000000..d0f0a21045 --- /dev/null +++ b/crates/tracedecay-session-runtime/tests/hotpath_coverage.rs @@ -0,0 +1,197 @@ +//! Hotpath coverage contract for `tracedecay-session-runtime`. +//! +//! Feature-off (default build): every hotpath macro must be a no-op — no +//! metrics listener on 6770/6771, no report file even when the report +//! environment is set, and `hotpath` must stay out of the crate's default +//! features. +//! +//! Feature-on (`--features hotpath`): a process-boundary guard must capture +//! this crate's measured mounted-LCM authority path in the report, proving +//! the instrumentation is real rather than dead configuration. The measured +//! sites here are `future = true` spans, so the report must include the +//! futures section as well as functions timing. + +use tracedecay_session_memory::context::{ + ProfileId, ResolvedSessionIdentity, SessionRootId, SessionStoreId, +}; +use tracedecay_session_memory::session::lcm::{LcmAuthorityRequest, LcmStatusQuery}; +use tracedecay_session_runtime::lcm_authority::mount_registered_lcm_authority; + +/// Deterministic, daemon-free workload that reaches this crate's measured +/// sites: `daemon.lcm.mount.execute`, `daemon.lcm.execute`, and +/// `daemon.lcm.status`. Registered-database fixtures come from the global-db +/// test harness; no daemon or socket is involved. +async fn run_mounted_lcm_status_workload() -> usize { + let directory = tempfile::tempdir().expect("create registered db fixture dir"); + let runtime = tracedecay_global_db::tests::harness::RegisteredGlobalDbTestRuntime::profile( + directory.path(), + ) + .await + .expect("open registered profile database"); + let database = runtime.profile_database_arc(); + let shard = database.binding().shard_id.clone(); + let identity = ResolvedSessionIdentity::for_profile( + ProfileId::new(shard.profile_id.as_str()).expect("valid profile id"), + SessionStoreId::new("store.profile.hotpath-coverage").expect("valid store id"), + SessionRootId::new("root.profile.hotpath-coverage").expect("valid root id"), + ); + let mounted = mount_registered_lcm_authority(database, identity, &shard) + .expect("mount registered lcm authority for owning profile identity"); + + let first = mounted + .execute(LcmAuthorityRequest::Status(LcmStatusQuery { + provider: "claude".to_owned(), + session_id: Some("session.hotpath-coverage.first".to_owned()), + deep: false, + })) + .await + .expect("mounted status must be invocable"); + let second = mounted + .execute(LcmAuthorityRequest::Status(LcmStatusQuery { + provider: "claude".to_owned(), + session_id: Some("session.hotpath-coverage.second".to_owned()), + deep: false, + })) + .await + .expect("mounted status must be invocable"); + assert_ne!( + first.receipt.grant_digest, second.receipt.grant_digest, + "each mounted request must mint its own grant digest" + ); + + 2 +} + +fn block_on_workload() -> usize { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("build current-thread runtime") + .block_on(run_mounted_lcm_status_workload()) +} + +/// Collects the entries of one feature array (for example `default`) from +/// this crate's manifest, tolerating multi-line arrays. Returns `None` when +/// the feature is not declared at all. +fn manifest_feature_array(manifest: &str, feature: &str) -> Option { + let mut in_features = false; + let mut collecting = false; + let mut collected = String::new(); + for line in manifest.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_features = trimmed == "[features]"; + continue; + } + if collecting { + collected.push_str(trimmed); + if trimmed.contains(']') { + return Some(collected); + } + continue; + } + if in_features + && let Some(rest) = trimmed.strip_prefix(feature) + && let Some(array) = rest.trim_start().strip_prefix('=') + { + collected.push_str(array.trim()); + if collected.contains(']') { + return Some(collected); + } + collecting = true; + } + } + None +} + +/// The profiling features must remain opt-in: neither `default` nor any +/// production-shaped feature set of this crate may pull in hotpath. +#[test] +fn hotpath_stays_out_of_default_and_production_features() { + let manifest = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")) + .expect("read crate manifest"); + for gate in ["default", "production"] { + if let Some(entries) = manifest_feature_array(&manifest, gate) { + assert!( + !entries.contains("hotpath"), + "feature `{gate}` must never enable hotpath, found: {entries}" + ); + } + } +} + +#[cfg(not(feature = "hotpath"))] +mod feature_off { + use std::net::TcpStream; + use std::path::Path; + + /// With the feature off the macros expand to their primary expression: + /// the workload behaves identically, the report environment is ignored, + /// and no metrics listener appears. + #[test] + fn workload_is_a_no_op_for_profiling() { + let report = + Path::new(env!("CARGO_TARGET_TMPDIR")).join("session-runtime-hotpath-off.json"); + let _ = std::fs::remove_file(&report); + // SAFETY: single-threaded with respect to readers — the feature-off + // build contains no hotpath runtime and nothing else in this test + // binary reads these variables. + unsafe { + std::env::set_var("HOTPATH_OUTPUT_FORMAT", "json"); + std::env::set_var("HOTPATH_OUTPUT_PATH", &report); + } + + assert!(super::block_on_workload() > 0); + + assert!( + !report.exists(), + "feature-off build must never write a hotpath report" + ); + for port in [6770u16, 6771] { + assert!( + TcpStream::connect(("127.0.0.1", port)).is_err(), + "feature-off build must not expose a hotpath listener on port {port} \ + (a listener here means another process on this machine is serving it)" + ); + } + } +} + +#[cfg(feature = "hotpath")] +mod feature_on { + use std::path::Path; + + /// A guard-scoped run of the same workload must record this crate's + /// measured sites, proving `--features hotpath` produces live + /// instrumentation and not an empty report. + #[test] + fn guard_report_captures_measured_lcm_authority_sites() { + // SAFETY: set before the first guard build in this process, which is + // the only reader; the metrics listener must stay off in tests. + unsafe { std::env::set_var("HOTPATH_METRICS_SERVER_OFF", "1") }; + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("session-runtime-hotpath-on.json"); + let _ = std::fs::remove_file(&report); + + { + let _guard = hotpath::HotpathGuardBuilder::new("session-runtime-hotpath-coverage") + .format(hotpath::Format::Json) + .output_path(&report) + .report("functions-timing,futures") + .build(); + assert!(super::block_on_workload() > 0); + } + + let report_text = + std::fs::read_to_string(&report).expect("feature-on guard drop must write a report"); + for label in [ + "daemon.lcm.mount.execute", + "daemon.lcm.execute", + "daemon.lcm.status", + ] { + assert!( + report_text.contains(label), + "hotpath report must capture measured site `{label}`: {report_text}" + ); + } + } +} diff --git a/crates/tracedecay-session-temporal-store/tests/hotpath_coverage.rs b/crates/tracedecay-session-temporal-store/tests/hotpath_coverage.rs new file mode 100644 index 0000000000..205d10da44 --- /dev/null +++ b/crates/tracedecay-session-temporal-store/tests/hotpath_coverage.rs @@ -0,0 +1,169 @@ +//! Hotpath coverage contract for `tracedecay-session-temporal-store`. +//! +//! Feature-off (default build): every hotpath macro must be a no-op — no +//! metrics listener on 6770/6771, no report file even when the report +//! environment is set, and `hotpath` must stay out of the crate's default +//! features. +//! +//! Feature-on (`--features hotpath`): a process-boundary guard must capture +//! this crate's measured hydration-render site in a functions-timing report, +//! proving the instrumentation is real rather than dead configuration. + +use tracedecay_lcm::contracts::{LcmContentRange, LcmContentSlice, LcmExpandResponse}; +use tracedecay_session_temporal_store::render::apply_canonical_content; + +/// Deterministic, daemon-free workload that reaches this crate's measured +/// site `session_temporal.hydrate.render`. +fn run_hydration_render_workload() -> usize { + let expansion = LcmExpandResponse { + kind: "raw_message".to_string(), + content: String::new(), + content_range: LcmContentRange { + offset: 0, + limit: 64, + returned_chars: 0, + total_chars: 0, + truncated: false, + }, + raw_message: None, + raw_message_metadata: None, + summary_node: None, + summary_sources: Vec::new(), + payload_ref: None, + from_current_session: None, + externalized_note: None, + source_pagination: None, + }; + + let rendered = apply_canonical_content( + expansion, + LcmContentSlice { + offset: 0, + limit: 64, + }, + "canonical hotpath coverage content", + ) + .expect("render canonical content slice"); + assert_eq!(rendered.content, "canonical hotpath coverage content"); + rendered.content.len() +} + +/// Collects the entries of one feature array (for example `default`) from +/// this crate's manifest, tolerating multi-line arrays. Returns `None` when +/// the feature is not declared at all. +fn manifest_feature_array(manifest: &str, feature: &str) -> Option { + let mut in_features = false; + let mut collecting = false; + let mut collected = String::new(); + for line in manifest.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_features = trimmed == "[features]"; + continue; + } + if collecting { + collected.push_str(trimmed); + if trimmed.contains(']') { + return Some(collected); + } + continue; + } + if in_features + && let Some(rest) = trimmed.strip_prefix(feature) + && let Some(array) = rest.trim_start().strip_prefix('=') + { + collected.push_str(array.trim()); + if collected.contains(']') { + return Some(collected); + } + collecting = true; + } + } + None +} + +/// The profiling features must remain opt-in: neither `default` nor any +/// production-shaped feature set of this crate may pull in hotpath. +#[test] +fn hotpath_stays_out_of_default_and_production_features() { + let manifest = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")) + .expect("read crate manifest"); + for gate in ["default", "production"] { + if let Some(entries) = manifest_feature_array(&manifest, gate) { + assert!( + !entries.contains("hotpath"), + "feature `{gate}` must never enable hotpath, found: {entries}" + ); + } + } +} + +#[cfg(not(feature = "hotpath"))] +mod feature_off { + use std::net::TcpStream; + use std::path::Path; + + /// With the feature off the macros expand to their primary expression: + /// the workload behaves identically, the report environment is ignored, + /// and no metrics listener appears. + #[test] + fn workload_is_a_no_op_for_profiling() { + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("temporal-store-hotpath-off.json"); + let _ = std::fs::remove_file(&report); + // SAFETY: single-threaded with respect to readers — the feature-off + // build contains no hotpath runtime and nothing else in this test + // binary reads these variables. + unsafe { + std::env::set_var("HOTPATH_OUTPUT_FORMAT", "json"); + std::env::set_var("HOTPATH_OUTPUT_PATH", &report); + } + + assert!(super::run_hydration_render_workload() > 0); + + assert!( + !report.exists(), + "feature-off build must never write a hotpath report" + ); + for port in [6770u16, 6771] { + assert!( + TcpStream::connect(("127.0.0.1", port)).is_err(), + "feature-off build must not expose a hotpath listener on port {port} \ + (a listener here means another process on this machine is serving it)" + ); + } + } +} + +#[cfg(feature = "hotpath")] +mod feature_on { + use std::path::Path; + + /// A guard-scoped run of the same workload must record this crate's + /// measured site, proving `--features hotpath` produces live + /// instrumentation and not an empty report. + #[test] + fn guard_report_captures_measured_render_site() { + // SAFETY: set before the first guard build in this process, which is + // the only reader; the metrics listener must stay off in tests. + unsafe { std::env::set_var("HOTPATH_METRICS_SERVER_OFF", "1") }; + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("temporal-store-hotpath-on.json"); + let _ = std::fs::remove_file(&report); + + { + let _guard = hotpath::HotpathGuardBuilder::new("temporal-store-hotpath-coverage") + .format(hotpath::Format::Json) + .output_path(&report) + .report("functions-timing") + .build(); + assert!(super::run_hydration_render_workload() > 0); + } + + let report_text = + std::fs::read_to_string(&report).expect("feature-on guard drop must write a report"); + assert!( + report_text.contains("session_temporal.hydrate.render"), + "hotpath report must capture measured site `session_temporal.hydrate.render`: \ + {report_text}" + ); + } +} diff --git a/crates/tracedecay-sessions/tests/hotpath_coverage.rs b/crates/tracedecay-sessions/tests/hotpath_coverage.rs new file mode 100644 index 0000000000..dba4caaa33 --- /dev/null +++ b/crates/tracedecay-sessions/tests/hotpath_coverage.rs @@ -0,0 +1,173 @@ +//! Hotpath coverage contract for `tracedecay-sessions`. +//! +//! Feature-off (default build): every hotpath macro must be a no-op — no +//! metrics listener on 6770/6771, no report file even when the report +//! environment is set, and `hotpath` must stay out of the crate's default +//! features. +//! +//! Feature-on (`--features hotpath`): a process-boundary guard must capture +//! this crate's measured content-normalization and transcript-discovery +//! sites in a functions-timing report, proving the instrumentation is real +//! rather than dead configuration. + +use serde_json::json; +use tracedecay_sessions::runtime::shared::content_storage_text_and_tools; +use tracedecay_sessions::runtime::source::{ + TranscriptDiscoveryBounds, collect_files_with_ext_bounded, +}; + +/// Deterministic, daemon-free workload that reaches this crate's measured +/// sites: `sessions.shared.content_storage` and +/// `sessions.source.discover_files`. +fn run_sessions_workload() -> usize { + let content = json!([ + { "type": "text", "text": "hotpath coverage fixture" }, + { "type": "tool_use", "name": "Read", "id": "tool.1", "input": { "path": "a.rs" } }, + ]); + let (text, tools) = content_storage_text_and_tools(&content, None); + assert!(!text.is_empty()); + assert_eq!(tools, vec!["Read".to_string()]); + + let temp = tempfile::tempdir().expect("create discovery fixture dir"); + for ordinal in 0..3 { + std::fs::write( + temp.path().join(format!("session-{ordinal}.jsonl")), + b"{\"type\":\"user\"}\n", + ) + .expect("write discovery fixture file"); + } + let report = collect_files_with_ext_bounded( + temp.path(), + "jsonl", + 1, + TranscriptDiscoveryBounds::from_discovered_units(16), + ); + assert_eq!(report.paths.len(), 3); + assert!(report.truncated.is_none()); + + report.paths.len() +} + +/// Collects the entries of one feature array (for example `default`) from +/// this crate's manifest, tolerating multi-line arrays. Returns `None` when +/// the feature is not declared at all. +fn manifest_feature_array(manifest: &str, feature: &str) -> Option { + let mut in_features = false; + let mut collecting = false; + let mut collected = String::new(); + for line in manifest.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_features = trimmed == "[features]"; + continue; + } + if collecting { + collected.push_str(trimmed); + if trimmed.contains(']') { + return Some(collected); + } + continue; + } + if in_features + && let Some(rest) = trimmed.strip_prefix(feature) + && let Some(array) = rest.trim_start().strip_prefix('=') + { + collected.push_str(array.trim()); + if collected.contains(']') { + return Some(collected); + } + collecting = true; + } + } + None +} + +/// The profiling features must remain opt-in: neither `default` nor any +/// production-shaped feature set of this crate may pull in hotpath. +#[test] +fn hotpath_stays_out_of_default_and_production_features() { + let manifest = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")) + .expect("read crate manifest"); + for gate in ["default", "production"] { + if let Some(entries) = manifest_feature_array(&manifest, gate) { + assert!( + !entries.contains("hotpath"), + "feature `{gate}` must never enable hotpath, found: {entries}" + ); + } + } +} + +#[cfg(not(feature = "hotpath"))] +mod feature_off { + use std::net::TcpStream; + use std::path::Path; + + /// With the feature off the macros expand to their primary expression: + /// the workload behaves identically, the report environment is ignored, + /// and no metrics listener appears. + #[test] + fn workload_is_a_no_op_for_profiling() { + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("sessions-hotpath-off.json"); + let _ = std::fs::remove_file(&report); + // SAFETY: single-threaded with respect to readers — the feature-off + // build contains no hotpath runtime and nothing else in this test + // binary reads these variables. + unsafe { + std::env::set_var("HOTPATH_OUTPUT_FORMAT", "json"); + std::env::set_var("HOTPATH_OUTPUT_PATH", &report); + } + + assert!(super::run_sessions_workload() > 0); + + assert!( + !report.exists(), + "feature-off build must never write a hotpath report" + ); + for port in [6770u16, 6771] { + assert!( + TcpStream::connect(("127.0.0.1", port)).is_err(), + "feature-off build must not expose a hotpath listener on port {port} \ + (a listener here means another process on this machine is serving it)" + ); + } + } +} + +#[cfg(feature = "hotpath")] +mod feature_on { + use std::path::Path; + + /// A guard-scoped run of the same workload must record this crate's + /// measured sites, proving `--features hotpath` produces live + /// instrumentation and not an empty report. + #[test] + fn guard_report_captures_measured_ingest_sites() { + // SAFETY: set before the first guard build in this process, which is + // the only reader; the metrics listener must stay off in tests. + unsafe { std::env::set_var("HOTPATH_METRICS_SERVER_OFF", "1") }; + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("sessions-hotpath-on.json"); + let _ = std::fs::remove_file(&report); + + { + let _guard = hotpath::HotpathGuardBuilder::new("sessions-hotpath-coverage") + .format(hotpath::Format::Json) + .output_path(&report) + .report("functions-timing") + .build(); + assert!(super::run_sessions_workload() > 0); + } + + let report_text = + std::fs::read_to_string(&report).expect("feature-on guard drop must write a report"); + for label in [ + "sessions.shared.content_storage", + "sessions.source.discover_files", + ] { + assert!( + report_text.contains(label), + "hotpath report must capture measured site `{label}`: {report_text}" + ); + } + } +} diff --git a/crates/tracedecay-temporal-query/tests/hotpath_coverage.rs b/crates/tracedecay-temporal-query/tests/hotpath_coverage.rs new file mode 100644 index 0000000000..0469b31a0a --- /dev/null +++ b/crates/tracedecay-temporal-query/tests/hotpath_coverage.rs @@ -0,0 +1,182 @@ +//! Hotpath coverage contract for `tracedecay-temporal-query`. +//! +//! Feature-off (default build): every hotpath macro must be a no-op — no +//! metrics listener on 6770/6771, no report file even when the report +//! environment is set, and `hotpath` must stay out of the crate's default +//! features. +//! +//! Feature-on (`--features hotpath`): a process-boundary guard must capture +//! this crate's measured candidate-planning and ranking sites in a +//! functions-timing report, proving the instrumentation is real rather than +//! dead configuration. + +use tracedecay_domain::RetrievalAnchorId; +use tracedecay_temporal_query::candidates::CandidateChannel; +use tracedecay_temporal_query::plan_temporal_candidates; +use tracedecay_temporal_query::ranking::{DiversityLimits, RankingCandidate, rank_candidates}; + +/// Deterministic, daemon-free workload that reaches this crate's measured +/// sites: `temporal.candidates.plan_scope`, `temporal.candidates.plan_text`, +/// and `temporal.rank`. +fn run_temporal_query_workload() -> usize { + let scope_plan = plan_temporal_candidates("", None, false); + assert!( + scope_plan.contains(CandidateChannel::Scope, ""), + "an empty query must plan a scope sweep" + ); + + let text_plan = plan_temporal_candidates("cargo test 2026-07-18", None, false); + assert!( + !text_plan.clauses().is_empty(), + "a text query must plan candidate clauses" + ); + + let anchor = RetrievalAnchorId::new("anchor.hotpath-coverage").expect("valid anchor id"); + let ranked = rank_candidates( + &[RankingCandidate { + stable_id: "candidate.hotpath-coverage".into(), + anchor_id: anchor, + retriever_record_id: "record.hotpath-coverage".into(), + channel: CandidateChannel::Lexical, + raw_score: 10, + knowledge_at_micros: 1, + logical_message: None, + turn: None, + session: Some("session.hotpath-coverage".into()), + source: Some("store".into()), + evidence_role: Some("message".into()), + exact_ranges: Vec::new(), + participant_generation: 1, + }], + DiversityLimits::unbounded(), + ) + .expect("rank single deterministic candidate"); + assert_eq!(ranked.len(), 1); + + text_plan.clauses().len() +} + +/// Collects the entries of one feature array (for example `default`) from +/// this crate's manifest, tolerating multi-line arrays. Returns `None` when +/// the feature is not declared at all. +fn manifest_feature_array(manifest: &str, feature: &str) -> Option { + let mut in_features = false; + let mut collecting = false; + let mut collected = String::new(); + for line in manifest.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_features = trimmed == "[features]"; + continue; + } + if collecting { + collected.push_str(trimmed); + if trimmed.contains(']') { + return Some(collected); + } + continue; + } + if in_features + && let Some(rest) = trimmed.strip_prefix(feature) + && let Some(array) = rest.trim_start().strip_prefix('=') + { + collected.push_str(array.trim()); + if collected.contains(']') { + return Some(collected); + } + collecting = true; + } + } + None +} + +/// The profiling features must remain opt-in: neither `default` nor any +/// production-shaped feature set of this crate may pull in hotpath. +#[test] +fn hotpath_stays_out_of_default_and_production_features() { + let manifest = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")) + .expect("read crate manifest"); + for gate in ["default", "production"] { + if let Some(entries) = manifest_feature_array(&manifest, gate) { + assert!( + !entries.contains("hotpath"), + "feature `{gate}` must never enable hotpath, found: {entries}" + ); + } + } +} + +#[cfg(not(feature = "hotpath"))] +mod feature_off { + use std::net::TcpStream; + use std::path::Path; + + /// With the feature off the macros expand to their primary expression: + /// the workload behaves identically, the report environment is ignored, + /// and no metrics listener appears. + #[test] + fn workload_is_a_no_op_for_profiling() { + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("temporal-query-hotpath-off.json"); + let _ = std::fs::remove_file(&report); + // SAFETY: single-threaded with respect to readers — the feature-off + // build contains no hotpath runtime and nothing else in this test + // binary reads these variables. + unsafe { + std::env::set_var("HOTPATH_OUTPUT_FORMAT", "json"); + std::env::set_var("HOTPATH_OUTPUT_PATH", &report); + } + + assert!(super::run_temporal_query_workload() > 0); + + assert!( + !report.exists(), + "feature-off build must never write a hotpath report" + ); + for port in [6770u16, 6771] { + assert!( + TcpStream::connect(("127.0.0.1", port)).is_err(), + "feature-off build must not expose a hotpath listener on port {port} \ + (a listener here means another process on this machine is serving it)" + ); + } + } +} + +#[cfg(feature = "hotpath")] +mod feature_on { + use std::path::Path; + + /// A guard-scoped run of the same workload must record this crate's + /// measured sites, proving `--features hotpath` produces live + /// instrumentation and not an empty report. + #[test] + fn guard_report_captures_measured_query_sites() { + // SAFETY: set before the first guard build in this process, which is + // the only reader; the metrics listener must stay off in tests. + unsafe { std::env::set_var("HOTPATH_METRICS_SERVER_OFF", "1") }; + let report = Path::new(env!("CARGO_TARGET_TMPDIR")).join("temporal-query-hotpath-on.json"); + let _ = std::fs::remove_file(&report); + + { + let _guard = hotpath::HotpathGuardBuilder::new("temporal-query-hotpath-coverage") + .format(hotpath::Format::Json) + .output_path(&report) + .report("functions-timing") + .build(); + assert!(super::run_temporal_query_workload() > 0); + } + + let report_text = + std::fs::read_to_string(&report).expect("feature-on guard drop must write a report"); + for label in [ + "temporal.candidates.plan_scope", + "temporal.candidates.plan_text", + "temporal.rank", + ] { + assert!( + report_text.contains(label), + "hotpath report must capture measured site `{label}`: {report_text}" + ); + } + } +} From a1fb1d63742009512a5e018b3ca1769e5ca31686 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 31 Aug 2026 18:13:23 +0000 Subject: [PATCH 2/2] ci(hotpath): run slice tests with hotpath off and on per PR Co-authored-by: Zack Jackson --- .github/workflows/hotpath-slice-tests.yml | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 .github/workflows/hotpath-slice-tests.yml diff --git a/.github/workflows/hotpath-slice-tests.yml b/.github/workflows/hotpath-slice-tests.yml new file mode 100644 index 0000000000..3d4606b3b6 --- /dev/null +++ b/.github/workflows/hotpath-slice-tests.yml @@ -0,0 +1,77 @@ +# Hotpath coverage for the sessions/lcm/capture/temporal-query slice. +# +# Proves this slice's hotpath instrumentation contract on every pull +# request: feature-off builds keep every hotpath macro a no-op (no metrics +# listener, no report file, hotpath absent from default features), while +# `--features hotpath` builds compile, run the same suites, and capture real +# measured sites in a guard report (see each crate's +# `tests/hotpath_coverage.rs`). +# +# This complements `.github/workflows/hotpath-profile.yml` — the index-bench +# profiling lane owned by the query/index slice — and must not replace it. +# Benches for this slice are deliberately deferred; this lane is tests only. +name: hotpath-slice-tests + +on: + pull_request: + workflow_dispatch: + +concurrency: + group: hotpath-slice-tests-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +env: + # Feature-on test binaries must never bind the live metrics listener in CI; + # the coverage tests also set this themselves before building a guard. + HOTPATH_METRICS_SERVER_OFF: "1" + SLICE_PACKAGES: >- + -p tracedecay-sessions + -p tracedecay-session-memory + -p tracedecay-session-runtime + -p tracedecay-session-temporal-store + -p tracedecay-lcm + -p tracedecay-capture + -p tracedecay-temporal-query + +jobs: + slice-tests: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@stable + + - uses: Swatinem/rust-cache@v2 + with: + shared-key: hotpath-slice-tests + cache-on-failure: true + + - name: kache compiler cache + uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin/kache + ~/.cache/kache + key: kache-${{ runner.os }}-${{ hashFiles('**/Cargo.lock') }} + restore-keys: kache-${{ runner.os }}- + - name: Enable kache + shell: bash + run: | + command -v kache >/dev/null 2>&1 || cargo install kache --locked + kache init --no-service + echo "RUSTC_WRAPPER=kache" >> "$GITHUB_ENV" + + # Feature-off first: this is the production shape. The per-crate + # hotpath_coverage tests assert the no-op contract (no listener on + # 6770/6771, no report file, hotpath not in default features). + - name: Slice tests (hotpath feature off) + run: cargo test ${{ env.SLICE_PACKAGES }} --locked + + # Feature-on second: the same suites plus the guard-report tests, which + # fail if the slice's instrumented sites stop being hit. + - name: Slice tests (hotpath feature on) + run: cargo test ${{ env.SLICE_PACKAGES }} --features hotpath --locked