From 494fe0999ae0f7716b9421c04d080516564c029e Mon Sep 17 00:00:00 2001 From: Jens Holdgaard Pedersen Date: Sat, 11 Jul 2026 23:12:51 +0200 Subject: [PATCH 1/4] =?UTF-8?q?feat(bench):=20rfc=200031=20=E2=80=94=20thr?= =?UTF-8?q?ee-point=20selectivity=20curve=20in=20the=20indicative=20run?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Run #6's storage-side result (5.95x on a 1-row answer) is a single point at the extreme-selectivity end, where Ourios's fixed per-query footer/ metadata reads dominate. This extends the indicative run to a curve: the severity pair (L2 family) plus two time-window slices (~100 and ~2000 rows on the picked service, L6 family, reported under f_l6), all measured against ONE container and ONE corpus replay. Window pairs are chosen with clean edges (no shared timestamp at the start, >=2 ns gap past the end) so [a, b) selects identical rows on both systems regardless of range-end inclusivity, and windows containing any zero-time_unix_nano row's observed-fallback timestamp are rejected — both systems would return such a row with DIFFERENT answer timestamps, a guaranteed equivalence mismatch. Locally proven before spending a run: picker edge/poison/centring unit tests, a fixture pass for the timestamp collector, and a fixture-store test that the bare-service DSL + now/window mapping slices exactly [start, end). Equivalence stays asserted per pair; gates stay REPORTED under the provisional §7 margins. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01WQY9wfrfRggqSpMLH8Xj3Y --- .github/workflows/comparative-bench.yml | 2 +- .../ourios-bench/tests/rfc0031_comparative.rs | 540 ++++++++++++++---- 2 files changed, 426 insertions(+), 116 deletions(-) diff --git a/.github/workflows/comparative-bench.yml b/.github/workflows/comparative-bench.yml index 6fd4c4dd9..61f8ae425 100644 --- a/.github/workflows/comparative-bench.yml +++ b/.github/workflows/comparative-bench.yml @@ -79,7 +79,7 @@ jobs: { echo "## RFC 0031 indicative comparative run" echo '```' - grep -A8 '=== RFC 0031 indicative comparative run ===' comparative-run.log \ + grep -A60 '=== RFC 0031 indicative comparative run ===' comparative-run.log \ || echo "(no report block — see the run log)" echo '```' } >> "$GITHUB_STEP_SUMMARY" diff --git a/crates/ourios-bench/tests/rfc0031_comparative.rs b/crates/ourios-bench/tests/rfc0031_comparative.rs index 854ad753b..eb2c617b8 100644 --- a/crates/ourios-bench/tests/rfc0031_comparative.rs +++ b/crates/ourios-bench/tests/rfc0031_comparative.rs @@ -526,17 +526,7 @@ fn pick_selective_pair(corpus_dir: &std::path::Path) -> SelectivePair { let mut per_service: HashMap>> = HashMap::new(); let (mut total, mut min_ts, mut max_ts) = (0u64, u64::MAX, 0u64); - let mut paths: Vec<_> = std::fs::read_dir(corpus_dir) - .expect("read corpus dir") - .filter_map(|e| { - let p = e.expect("dir entry").path(); - (p.extension().and_then(|x| x.to_str()) == Some("jsonl")).then_some(p) - }) - .collect(); - paths.sort(); - assert!(!paths.is_empty(), "no *.jsonl in {}", corpus_dir.display()); - - for path in paths { + for path in corpus_jsonl_paths(corpus_dir) { let file = std::fs::File::open(&path).expect("open corpus file"); for line in std::io::BufReader::new(file).lines() { let line = line.expect("read corpus line"); @@ -620,6 +610,115 @@ fn pick_selective_pair(corpus_dir: &std::path::Path) -> SelectivePair { } } +/// The corpus's `*.jsonl` files, sorted for deterministic scans. +fn corpus_jsonl_paths(corpus_dir: &std::path::Path) -> Vec { + let mut paths: Vec<_> = std::fs::read_dir(corpus_dir) + .expect("read corpus dir") + .filter_map(|e| { + let p = e.expect("dir entry").path(); + (p.extension().and_then(|x| x.to_str()) == Some("jsonl")).then_some(p) + }) + .collect(); + paths.sort(); + assert!(!paths.is_empty(), "no *.jsonl in {}", corpus_dir.display()); + paths +} + +/// Second streaming pass over the corpus, for the selectivity-curve +/// window pairs: every log record of `service`, split into CLEAN +/// timestamps (non-zero `time_unix_nano` — rows both systems answer +/// with identical keys) and POISON timestamps (the observed-fallback +/// effective time of zero-`time_unix_nano` rows: both systems would +/// RETURN such a row if a window covered its fallback time, but with +/// DIFFERENT answer timestamps — Ourios keeps `time_unix_nano = 0`, +/// Loki stamps its stored observed time — so any window containing one +/// is a guaranteed equivalence mismatch). Both sorted ascending. +fn collect_service_timestamps(corpus_dir: &std::path::Path, service: &str) -> (Vec, Vec) { + use std::io::BufRead as _; + + let (mut clean, mut poison) = (Vec::new(), Vec::new()); + for path in corpus_jsonl_paths(corpus_dir) { + let file = std::fs::File::open(&path).expect("open corpus file"); + for line in std::io::BufReader::new(file).lines() { + let line = line.expect("read corpus line"); + if line.trim().is_empty() { + continue; + } + let data: opentelemetry_proto::tonic::logs::v1::LogsData = + serde_json::from_str(&line).expect("parse LogsData line"); + for rl in &data.resource_logs { + let matches_service = rl + .resource + .as_ref() + .and_then(|r| r.attributes.iter().find(|kv| kv.key == "service.name")) + .and_then(|kv| kv.value.as_ref()) + .and_then(|v| v.value.as_ref()) + .is_some_and(|v| { + matches!( + v, + opentelemetry_proto::tonic::common::v1::any_value::Value::StringValue(s) + if s == service + ) + }); + if !matches_service { + continue; + } + for sl in &rl.scope_logs { + for lr in &sl.log_records { + if lr.time_unix_nano != 0 { + clean.push(lr.time_unix_nano); + } else if lr.observed_time_unix_nano != 0 { + poison.push(lr.observed_time_unix_nano); + } + } + } + } + } + } + clean.sort_unstable(); + poison.sort_unstable(); + (clean, poison) +} + +/// Pick a `k`-row time-window slice `[a, b)` of a service's records with +/// CLEAN EDGES, preferring mid-corpus. `clean_ts` and `poison_ts` come +/// sorted from [`collect_service_timestamps`]. A candidate start index +/// `i` is valid iff: +/// +/// - **start edge**: no earlier record shares `clean_ts[i]`, so `a = +/// clean_ts[i]` admits exactly the window's rows; +/// - **end edge**: the next record (if any) sits ≥ 2 ns past the last +/// in-window one, so `b = last + 1` selects the same rows whether a +/// system treats its range end as inclusive or exclusive; +/// - no poison timestamp falls in `[a, b)`. +/// +/// The window then contains exactly `k` rows on both systems. Returns +/// `(a, b)`, or `None` if no valid window exists. +fn pick_window_pair(clean_ts: &[u64], poison_ts: &[u64], k: usize) -> Option<(u64, u64)> { + if k == 0 || clean_ts.len() < k { + return None; + } + let valid = |i: usize| { + let (first, last) = (clean_ts[i], clean_ts[i + k - 1]); + if i > 0 && clean_ts[i - 1] == first { + return false; + } + let Some(b) = last.checked_add(1) else { + return false; + }; + if i + k < clean_ts.len() && clean_ts[i + k] <= b { + return false; + } + let at = poison_ts.partition_point(|&t| t < first); + poison_ts.get(at).is_none_or(|&t| t >= b) + }; + let centre = (clean_ts.len() - k) / 2; + let i = (0..=clean_ts.len() - k) + .filter(|&i| valid(i)) + .min_by_key(|&i| (i.abs_diff(centre), i))?; + Some((clean_ts[i], clean_ts[i + k - 1] + 1)) +} + /// The candidate `(rows, threshold, service, text)` tuples for /// [`pick_selective_pair`]: for every service and every DISTINCT severity /// number T in it (the nested map dedupes them), the rows with @@ -801,21 +900,134 @@ async fn push_corpus_to_loki(http: &reqwest::Client, base: &str, corpus_dir: &st eprintln!("loki ingest complete: {batched} LogsData lines in {pushed} requests"); } -/// The §7 calibration input: the first indicative Ourios-vs-Loki -/// bytes-read comparison on a real corpus (`OURIOS_COMPARATIVE_CORPUS`, -/// fetched by the `comparative-bench` dispatch workflow — the -/// `corpus/otel-demo-v*` releases). +/// One measured query of the indicative run: the equivalent DSL/`LogQL` +/// question, the window both systems answer it over, and the row count +/// both must return exactly. +struct PairSpec { + label: String, + /// The pair's §7 margin for the reported gate (`m_l2` for the + /// severity pair, `f_l6` for the broad time-window slices). + margin: u64, + dsl: String, + logql: String, + /// Loki `query_range` window (nanoseconds, `[start, end)` by the + /// clean-edge construction). + start: u64, + end: u64, + expected_rows: u64, + /// The Ourios querier's window parameters mapping to the same + /// `[start, end)`: `time_window_filter` is `ts ≥ now − window ∧ + /// ts < now`, so `now = end`, `window = end − start`. + now: u64, + window: u64, +} + +/// The run's three pairs — a selectivity curve, not a point. Run #6 +/// measured ONE extreme-selectivity pair (1 row) and found storage-side +/// advantage 5.95×: Ourios's fixed per-query footer/metadata reads +/// dominate when the answer is a single row. The curve tests the +/// amortization prediction — Ourios's fixed cost should wash out as the +/// result grows while Loki's scan grows with it. The severity pair is +/// the L2-family point; the two time-window slices (~100 and ~2000 rows +/// on the same service) are broad scans, so their gates report under +/// the L6 floor factor. +fn build_pair_specs( + pair: &SelectivePair, + clean_ts: &[u64], + poison_ts: &[u64], + corpus_now: u64, + corpus_window: u64, +) -> Vec { + let margins = ourios_bench::ComparativeMargins::default(); + let mut specs = vec![PairSpec { + label: format!( + "severity, L2 family: service={} severity>={} (text={:?})", + pair.service, pair.threshold, pair.text + ), + margin: margins.m_l2, + dsl: format!( + "service == \"{}\" and severity >= {} | limit 5000", + pair.service, pair.threshold + ), + logql: format!( + "{{service_name=\"{}\"}} | severity_text=\"{}\"", + pair.service, pair.text + ), + start: pair.min_ts, + end: pair.max_ts + 1, + expected_rows: pair.rows, + now: corpus_now, + window: corpus_window, + }]; + for k in [100usize, 2000] { + let Some((start, end)) = pick_window_pair(clean_ts, poison_ts, k) else { + panic!( + "no clean {k}-row window on service {} ({} clean rows, {} poison)", + pair.service, + clean_ts.len(), + poison_ts.len(), + ); + }; + specs.push(PairSpec { + label: format!( + "time-window slice, L6 family: service={} k={k}", + pair.service + ), + margin: margins.f_l6, + dsl: format!("service == \"{}\" | limit 5000", pair.service), + logql: format!("{{service_name=\"{}\"}}", pair.service), + start, + end, + expected_rows: k as u64, + now: end, + window: end - start, + }); + } + specs +} + +/// One pair's Loki measurement: poll `query_range` until ingest has +/// caught up to the expected row count (or fail loudly at the deadline). +async fn loki_measure_pair( + http: &reqwest::Client, + base: &str, + spec: &PairSpec, +) -> (Vec, u64, LokiFetchedBytes) { + let deadline = std::time::Instant::now() + Duration::from_secs(300); + loop { + let (lines, bytes, fetched) = + loki_query_with_stats(http, base, &spec.logql, spec.start, spec.end).await; + if lines.len() as u64 >= spec.expected_rows { + break (lines, bytes, fetched); + } + assert!( + std::time::Instant::now() < deadline, + "loki returned {} of {} expected rows for [{}] before timeout", + lines.len(), + spec.expected_rows, + spec.label, + ); + tokio::time::sleep(Duration::from_secs(2)).await; + } +} + +/// The §7 calibration input: the indicative Ourios-vs-Loki bytes-read +/// comparison on a real corpus (`OURIOS_COMPARATIVE_CORPUS`, fetched by +/// the `comparative-bench` dispatch workflow — the `corpus/otel-demo-v*` +/// releases), measured across a three-point selectivity curve +/// ([`build_pair_specs`]) over one container + one corpus replay. /// -/// **Equivalence is asserted; the bytes gate is REPORTED, not asserted** -/// — the §7 margins are provisional until the maintainer freezes them -/// against exactly this run's numbers (RFC 0031 §7). +/// **Equivalence is asserted per pair; the bytes gates are REPORTED, +/// not asserted** — the §7 margins are provisional until the maintainer +/// freezes them against exactly these numbers (RFC 0031 §7). /// -/// Loki runs the stock image config plus two explicit, documented -/// ingest-side deviations (both in LOKI'S favour — the anti-strawman -/// direction): `-validation.reject-old-samples=false` (the frozen captures carry -/// their original timestamps, weeks old by run time) and raised -/// ingest-rate limits so a 2.96 GB replay isn't throttled by dev-scale -/// defaults. The query side stays stock. +/// Loki runs the stock image config plus explicit, documented +/// ingest-side deviations (all in LOKI'S favour — the anti-strawman +/// direction): `-validation.reject-old-samples=false` (the frozen +/// captures carry their original timestamps, weeks old by run time), +/// raised ingest-rate limits so a 2.96 GB replay isn't throttled by +/// dev-scale defaults, and a raised internal gRPC message cap (see the +/// flag comment). The query side stays stock. #[test] #[ignore = "dispatch-only: needs Docker + a corpus via OURIOS_COMPARATIVE_CORPUS (comparative-bench workflow)"] fn rfc0031_indicative_comparative_run() { @@ -824,9 +1036,17 @@ fn rfc0031_indicative_comparative_run() { .expect("set OURIOS_COMPARATIVE_CORPUS to a corpus dir (the dispatch workflow does)"), ); - // Pick the pair, then drive the (locally-proven) Ourios half. let pair = pick_selective_pair(&corpus_dir); eprintln!("pair: {pair:?}"); + let (clean_ts, poison_ts) = collect_service_timestamps(&corpus_dir, &pair.service); + eprintln!( + "service {}: {} clean timestamps, {} zero-time (poison)", + pair.service, + clean_ts.len(), + poison_ts.len(), + ); + + // The (locally-proven) Ourios half, per pair. let bucket = tempfile::TempDir::new().expect("bucket dir"); let built = ourios_bench::build_comparative_store( &corpus_dir, @@ -835,27 +1055,37 @@ fn rfc0031_indicative_comparative_run() { ) .expect("build comparative store"); let tenant = TenantId::new(built.tenant); - let now = built.max_effective_time_unix_nano + 1; - let window = built.max_effective_time_unix_nano - built.min_effective_time_unix_nano + 2; - let dsl = format!( - "service == \"{}\" and severity >= {} | limit 5000", - pair.service, pair.threshold - ); - let ourios = ourios_bench::ourios_query_answer(bucket.path(), &tenant, &dsl, now, window) - .expect("ourios answer"); - assert_eq!( - ourios.lines.len() as u64, - pair.rows, - "Ourios must return exactly the picked pair's rows", - ); + let corpus_now = built.max_effective_time_unix_nano + 1; + let corpus_window = built.max_effective_time_unix_nano - built.min_effective_time_unix_nano + 2; + let specs = build_pair_specs(&pair, &clean_ts, &poison_ts, corpus_now, corpus_window); + let ourios: Vec<_> = specs + .iter() + .map(|spec| { + let answer = ourios_bench::ourios_query_answer( + bucket.path(), + &tenant, + &spec.dsl, + spec.now, + spec.window, + ) + .expect("ourios answer"); + assert_eq!( + answer.lines.len() as u64, + spec.expected_rows, + "Ourios must return exactly [{}]'s expected rows", + spec.label, + ); + answer + }) + .collect(); - // The Loki half: container (stock + documented ingest-side flags), - // full-corpus OTLP replay, the equivalent LogQL. + // The Loki half: one container (stock + documented ingest-side + // flags), ONE full-corpus OTLP replay, all pairs measured against it. let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .expect("tokio runtime"); - let (loki_lines, loki_bytes, loki_fetched) = runtime.block_on(async { + let loki: Vec<_> = runtime.block_on(async { let (_container, base, http) = start_loki(&[ "-validation.reject-old-samples=false", "-distributor.ingestion-rate-limit-mb=512", @@ -876,90 +1106,69 @@ fn rfc0031_indicative_comparative_run() { ]) .await; push_corpus_to_loki(&http, &base, &corpus_dir).await; - - let logql = format!( - "{{service_name=\"{}\"}} | severity_text=\"{}\"", - pair.service, pair.text - ); - let (start, end) = (pair.min_ts, pair.max_ts + 1); - // Poll until ingest catches up to the expected row count. - let deadline = std::time::Instant::now() + Duration::from_secs(300); - loop { - let (lines, bytes, fetched) = - loki_query_with_stats(&http, &base, &logql, start, end).await; - if lines.len() as u64 >= pair.rows { - break (lines, bytes, fetched); - } - assert!( - std::time::Instant::now() < deadline, - "loki returned {} of {} expected rows before timeout", - lines.len(), - pair.rows, - ); - tokio::time::sleep(Duration::from_secs(2)).await; + let mut measured = Vec::with_capacity(specs.len()); + for spec in &specs { + measured.push(loki_measure_pair(&http, &base, spec).await); } + measured }); - // Equivalence gates the measurement (RFC0031.1) — asserted. - let outcome = compare_lines(&ourios.lines, &loki_lines, 8); - assert!( - outcome.is_equal(), - "the two systems' answers must be multiset-identical: {outcome:?}", - ); + // Equivalence gates every measurement (RFC0031.1) — asserted. + for ((spec, ours), (loki_lines, _, _)) in specs.iter().zip(&ourios).zip(&loki) { + let outcome = compare_lines(&ours.lines, loki_lines, 8); + assert!( + outcome.is_equal(), + "the two systems' answers must be multiset-identical on [{}]: {outcome:?}", + spec.label, + ); + } - print_indicative_report( - &corpus_dir, - &pair, - ourios.bytes_read, - loki_bytes, - loki_fetched, - ); + print_indicative_report(&corpus_dir, pair.total_records, &specs, &ourios, &loki); } -/// The indicative run's report block. The bytes gate is REPORTED under -/// the provisional §7 margins, and evaluated PRIMARILY on the -/// conservative Loki figure: `totalBytesProcessed` is decompressed -/// engine-side work, which overstates Loki's storage reads by the chunk -/// compression ratio; the storage-side figure (compressed chunk bytes + -/// memory-served head-chunk bytes) is the apples-to-apples counterpart -/// of Ourios's fetched-compressed-Parquet bytes. Both ratios are printed -/// so the §9 entry can carry the honest pair of numbers. +/// The indicative run's report block, one section per pair. Each gate is +/// REPORTED under the pair's provisional §7 margin, and evaluated +/// PRIMARILY on the conservative Loki figure: `totalBytesProcessed` is +/// decompressed engine-side work, which overstates Loki's storage reads +/// by the chunk compression ratio; the storage-side figure (compressed +/// chunk bytes + memory-served head-chunk bytes) is the apples-to-apples +/// counterpart of Ourios's fetched-compressed-Parquet bytes. Both ratios +/// are printed so the §9 entry can carry the honest pair of numbers. fn print_indicative_report( corpus_dir: &std::path::Path, - pair: &SelectivePair, - ourios_bytes: u64, - loki_processed: u64, - loki_fetched: LokiFetchedBytes, + total_records: u64, + specs: &[PairSpec], + ourios: &[ourios_bench::OuriosAnswer], + loki: &[(Vec, u64, LokiFetchedBytes)], ) { - let margins = ourios_bench::ComparativeMargins::default(); - let loki_storage = loki_fetched.compressed_bytes + loki_fetched.head_chunk_bytes; - let gate_storage = ourios_bench::bytes_must_win(ourios_bytes, loki_storage, margins.m_l2); - let gate_processed = ourios_bench::bytes_must_win(ourios_bytes, loki_processed, margins.m_l2); println!("=== RFC 0031 indicative comparative run ==="); - println!( - "corpus: {} ({} records)", - corpus_dir.display(), - pair.total_records - ); - println!( - "pair (L2 family): service={} severity>={} (text={:?}) rows={}", - pair.service, pair.threshold, pair.text, pair.rows - ); - println!("ourios bytes_read (compressed, fetched) = {ourios_bytes}"); - println!( - "loki storage-side bytes (conservative) = {loki_storage} \ - (compressed={} + head_chunk={})", - loki_fetched.compressed_bytes, loki_fetched.head_chunk_bytes, - ); - println!("loki totalBytesProcessed (decompressed) = {loki_processed}"); - println!( - "gate vs storage-side (PRIMARY, margin {}): {gate_storage:?}", - margins.m_l2 - ); - println!( - "gate vs bytes-processed (context, margin {}): {gate_processed:?}", - margins.m_l2 - ); + println!("corpus: {} ({total_records} records)", corpus_dir.display()); + for ((spec, ours), (_, loki_processed, loki_fetched)) in specs.iter().zip(ourios).zip(loki) { + let loki_storage = loki_fetched.compressed_bytes + loki_fetched.head_chunk_bytes; + let gate_storage = ourios_bench::bytes_must_win(ours.bytes_read, loki_storage, spec.margin); + let gate_processed = + ourios_bench::bytes_must_win(ours.bytes_read, *loki_processed, spec.margin); + println!("--- pair [{}] rows={} ---", spec.label, spec.expected_rows); + println!("dsl: {}", spec.dsl); + println!( + "ourios bytes_read (compressed, fetched) = {}", + ours.bytes_read + ); + println!( + "loki storage-side bytes (conservative) = {loki_storage} \ + (compressed={} + head_chunk={})", + loki_fetched.compressed_bytes, loki_fetched.head_chunk_bytes, + ); + println!("loki totalBytesProcessed (decompressed) = {loki_processed}"); + println!( + "gate vs storage-side (PRIMARY, margin {}): {gate_storage:?}", + spec.margin + ); + println!( + "gate vs bytes-processed (context, margin {}): {gate_processed:?}", + spec.margin + ); + } } #[test] @@ -1028,3 +1237,104 @@ fn pick_selective_pair_generalizes_without_error_rows() { assert_eq!(pair.text, "WARN"); assert_eq!(pair.rows, 1); } + +#[test] +fn pick_window_pair_prefers_a_clean_mid_corpus_window() { + let ts: Vec = (0..100).map(|i| 1_000 + i * 10).collect(); + let (a, b) = pick_window_pair(&ts, &[], 10).expect("every window is clean"); + assert_eq!(a, 1_000 + 45 * 10, "centred start index (100−10)/2 = 45"); + assert_eq!(b, 1_000 + 54 * 10 + 1, "b = last in-window timestamp + 1"); + assert_eq!(ts.iter().filter(|&&t| t >= a && t < b).count(), 10); +} + +#[test] +fn pick_window_pair_edges_are_clean() { + // Duplicates and 1 ns gaps around the centre force the picker off the + // centred window; whatever it returns must satisfy both edge + // invariants and contain exactly k rows. + let ts: Vec = vec![0, 10, 20, 30, 40, 40, 41, 50, 60, 70, 80, 90]; + let k = 3; + let (a, b) = pick_window_pair(&ts, &[], k).expect("clean windows exist"); + assert_eq!(ts.iter().filter(|&&t| t >= a && t < b).count(), k); + assert!( + !ts.contains(&b), + "end-inclusive semantics at b must not admit an extra row" + ); + let first_inside = ts.iter().position(|&t| t >= a).expect("window non-empty"); + assert!( + first_inside == 0 || ts[first_inside - 1] < a, + "no earlier record shares the window's start timestamp" + ); +} + +#[test] +fn pick_window_pair_avoids_poison_timestamps() { + let ts: Vec = (0..100).map(|i| i * 10).collect(); + let poison = vec![455]; + let (a, b) = pick_window_pair(&ts, &poison, 10).expect("clean windows exist off-centre"); + assert!( + !(a..b).contains(&455), + "a zero-time row's fallback timestamp inside the window guarantees \ + an equivalence mismatch" + ); + assert_eq!(ts.iter().filter(|&&t| t >= a && t < b).count(), 10); +} + +#[test] +fn pick_window_pair_none_when_insufficient() { + assert_eq!(pick_window_pair(&[1, 2, 3], &[], 4), None); + assert_eq!(pick_window_pair(&[1, 2, 3], &[], 0), None); +} + +#[test] +fn collect_service_timestamps_reads_the_fixture() { + let records = comparative_fixture(1_000_000); + let corpus = tempfile::TempDir::new().expect("corpus dir"); + std::fs::write( + corpus.path().join("fixture.jsonl"), + fixture_jsonl(&records).expect("fixture jsonl"), + ) + .expect("write corpus"); + + let (clean, poison) = collect_service_timestamps(corpus.path(), FIXTURE_SERVICE); + assert_eq!(clean, vec![1_000_000, 1_001_000, 1_002_000]); + assert!(poison.is_empty(), "the fixture has no zero-time records"); + let (other_clean, other_poison) = collect_service_timestamps(corpus.path(), "no-such-service"); + assert!(other_clean.is_empty() && other_poison.is_empty()); +} + +/// Locally proves the window-pair query shape end to end on the fixture: +/// the bare-service DSL parses, and the `now = end` / `window = end − +/// start` mapping slices exactly `[start, end)` (the querier's +/// `time_window_filter` is `ts ≥ now − window ∧ ts < now`). +#[test] +fn window_pair_dsl_slices_the_fixture() { + let records = comparative_fixture(1_000_000); + let corpus = tempfile::TempDir::new().expect("corpus dir"); + std::fs::write( + corpus.path().join("fixture.jsonl"), + fixture_jsonl(&records).expect("fixture jsonl"), + ) + .expect("write corpus"); + let bucket = tempfile::TempDir::new().expect("bucket dir"); + let built = ourios_bench::build_comparative_store( + corpus.path(), + bucket.path(), + ourios_bench::TxtSeverity::Fixed, + ) + .expect("build comparative store"); + let tenant = TenantId::new(built.tenant); + + // [1_001_000, 1_002_001) → the second and third fixture records. + let (start, end) = (1_001_000u64, 1_002_001u64); + let dsl = format!("service == \"{FIXTURE_SERVICE}\" | limit 5000"); + let answer = ourios_bench::ourios_query_answer(bucket.path(), &tenant, &dsl, end, end - start) + .expect("ourios answer"); + let mut got: Vec = answer + .lines + .iter() + .map(|k| k.timestamp_unix_nanos) + .collect(); + got.sort_unstable(); + assert_eq!(got, vec![1_001_000, 1_002_000]); +} From 830f7931ea235cfd288e0013699f36afc36e424e Mon Sep 17 00:00:00 2001 From: Jens Holdgaard Pedersen Date: Sat, 11 Jul 2026 23:22:14 +0200 Subject: [PATCH 2/4] =?UTF-8?q?docs(bench):=20rfc=200031=20=E2=80=94=20cor?= =?UTF-8?q?rect=20PairSpec=20now/window=20field=20doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The [start, end) mapping only holds for the time-window slices; the severity pair windows the full corpus and lets the predicate select. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01WQY9wfrfRggqSpMLH8Xj3Y --- crates/ourios-bench/tests/rfc0031_comparative.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/ourios-bench/tests/rfc0031_comparative.rs b/crates/ourios-bench/tests/rfc0031_comparative.rs index eb2c617b8..fc712a740 100644 --- a/crates/ourios-bench/tests/rfc0031_comparative.rs +++ b/crates/ourios-bench/tests/rfc0031_comparative.rs @@ -915,9 +915,13 @@ struct PairSpec { start: u64, end: u64, expected_rows: u64, - /// The Ourios querier's window parameters mapping to the same - /// `[start, end)`: `time_window_filter` is `ts ≥ now − window ∧ - /// ts < now`, so `now = end`, `window = end − start`. + /// The Ourios querier's window parameters (`time_window_filter` is + /// `ts ≥ now − window ∧ ts < now`). For the time-window slices these + /// map exactly to `[start, end)` (`now = end`, `window = end − + /// start`); the severity pair instead uses the full-corpus + /// effective-time window — there both sides' windows are supersets + /// of every matching row and the severity predicate does the + /// selecting. now: u64, window: u64, } From b2816e2a5da03a1fe00f21912d440e90d72fac47 Mon Sep 17 00:00:00 2001 From: Jens Holdgaard Pedersen Date: Sat, 11 Jul 2026 23:28:47 +0200 Subject: [PATCH 3/4] =?UTF-8?q?refactor(bench):=20rfc=200031=20=E2=80=94?= =?UTF-8?q?=20checked=5Fadd=20on=20pick=5Fwindow=5Fpair's=20return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The +1 was provably safe (valid() already gated it), but the checked form keeps that safety local if the two sites ever decouple. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01WQY9wfrfRggqSpMLH8Xj3Y --- crates/ourios-bench/tests/rfc0031_comparative.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ourios-bench/tests/rfc0031_comparative.rs b/crates/ourios-bench/tests/rfc0031_comparative.rs index fc712a740..bee464a6a 100644 --- a/crates/ourios-bench/tests/rfc0031_comparative.rs +++ b/crates/ourios-bench/tests/rfc0031_comparative.rs @@ -716,7 +716,7 @@ fn pick_window_pair(clean_ts: &[u64], poison_ts: &[u64], k: usize) -> Option<(u6 let i = (0..=clean_ts.len() - k) .filter(|&i| valid(i)) .min_by_key(|&i| (i.abs_diff(centre), i))?; - Some((clean_ts[i], clean_ts[i + k - 1] + 1)) + Some((clean_ts[i], clean_ts[i + k - 1].checked_add(1)?)) } /// The candidate `(rows, threshold, service, text)` tuples for From 8c884b5e22f4851ea32655d5227423e3242214d9 Mon Sep 17 00:00:00 2001 From: Jens Holdgaard Pedersen Date: Sat, 11 Jul 2026 23:42:43 +0200 Subject: [PATCH 4/4] =?UTF-8?q?fix(bench):=20rfc=200031=20=E2=80=94=20pois?= =?UTF-8?q?on-aware=20pair=20picker=20+=20checked=20window=20arithmetic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copilot caught a real latent hazard: the severity pair queries the full corpus window, but the picker only EXCLUDED zero-time_unix_nano rows from its bands — it never checked whether the picked predicate could select one. A zero-time row with severity >= threshold (DSL side) or carrying the pair's text at any severity (LogQL side) is returned by both systems with different answer timestamps: a deterministic equivalence failure. Runs #5/#6 passed by corpus luck, not by construction. Zero-time rows are now tallied into per-service POISON bands and any candidate whose predicate could select one is disqualified, on both directions of the predicate. Unit-tested directly on hand-built bands. Also: checked_add on the two flagged window-end computations so a corrupted corpus fails loudly instead of wrapping in release mode. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01WQY9wfrfRggqSpMLH8Xj3Y --- .../ourios-bench/tests/rfc0031_comparative.rs | 121 +++++++++++++----- 1 file changed, 91 insertions(+), 30 deletions(-) diff --git a/crates/ourios-bench/tests/rfc0031_comparative.rs b/crates/ourios-bench/tests/rfc0031_comparative.rs index bee464a6a..32c4d5bb2 100644 --- a/crates/ourios-bench/tests/rfc0031_comparative.rs +++ b/crates/ourios-bench/tests/rfc0031_comparative.rs @@ -509,7 +509,10 @@ struct SelectivePair { /// 5000-line query cap, so the complete result fits one page). The /// consistency requirement makes DSL `severity >= T` and `LogQL` /// `severity_text="t"` express the same question, so the equivalence -/// check is meaningful. Generalised from a hardcoded ERROR band because +/// check is meaningful. Zero-`time_unix_nano` rows are tallied +/// separately as POISON bands and disqualify any candidate whose +/// predicate could select them (see [`select_pair_candidates`]). +/// Generalised from a hardcoded ERROR band because /// real captures vary — otel-demo v8 carries no ERROR logs at all (its /// failure flags surface in traces/metrics), only INFO/Information and /// four WARNs. Candidate thresholds are the service's **observed** @@ -522,8 +525,8 @@ fn pick_selective_pair(corpus_dir: &std::path::Path) -> SelectivePair { use std::collections::HashMap; use std::io::BufRead as _; - // service -> severity_number -> severity_text -> row count - let mut per_service: HashMap>> = HashMap::new(); + // service -> (clean bands, poison bands) + let mut per_service: HashMap = HashMap::new(); let (mut total, mut min_ts, mut max_ts) = (0u64, u64::MAX, 0u64); for path in corpus_jsonl_paths(corpus_dir) { @@ -559,23 +562,25 @@ fn pick_selective_pair(corpus_dir: &std::path::Path) -> SelectivePair { for sl in &rl.scope_logs { for lr in &sl.log_records { total += 1; - // Zero-`time_unix_nano` records are excluded from - // candidate bands even though both systems could + // Zero-`time_unix_nano` records go into POISON + // bands, not candidate bands: both systems could // still return them (Ourios windows the RFC 0005 // §3.2 EFFECTIVE timestamp, falling back to // observed; Loki's OTLP ingest falls back to - // observed too): the two sides would answer with - // DIFFERENT timestamps — Ourios's row keeps - // time_unix_nano = 0, Loki stamps its stored - // (observed) time — so their LineKeys can never - // match and any band containing such rows is a - // guaranteed equivalence mismatch. - if lr.time_unix_nano == 0 { - continue; - } - min_ts = min_ts.min(lr.time_unix_nano); - max_ts = max_ts.max(lr.time_unix_nano); - let texts = entry.entry(lr.severity_number).or_default(); + // observed too), but with DIFFERENT answer + // timestamps — Ourios's row keeps time_unix_nano + // = 0, Loki stamps its stored (observed) time — + // so their LineKeys can never match and any + // candidate whose predicate could select such a + // row is a guaranteed equivalence mismatch. + let bands = if lr.time_unix_nano == 0 { + &mut entry.1 + } else { + min_ts = min_ts.min(lr.time_unix_nano); + max_ts = max_ts.max(lr.time_unix_nano); + &mut entry.0 + }; + let texts = bands.entry(lr.severity_number).or_default(); if let Some(count) = texts.get_mut(lr.severity_text.as_str()) { *count += 1; } else { @@ -719,20 +724,26 @@ fn pick_window_pair(clean_ts: &[u64], poison_ts: &[u64], k: usize) -> Option<(u6 Some((clean_ts[i], clean_ts[i + k - 1].checked_add(1)?)) } +/// `severity_number -> severity_text -> row count`, one map per service +/// for clean (non-zero-time) rows and one for poison (zero-time) rows. +type SeverityBands = std::collections::HashMap>; + /// The candidate `(rows, threshold, service, text)` tuples for /// [`pick_selective_pair`]: for every service and every DISTINCT severity -/// number T in it (the nested map dedupes them), the rows with -/// `number ≥ T` are a candidate iff (a) they all share ONE text t, (b) -/// the count is `1..=4000`, and (c) — the reverse direction — NO row with +/// number T in its clean bands (the nested map dedupes them), the rows +/// with `number ≥ T` are a candidate iff (a) they all share ONE text t, +/// (b) the count is `1..=4000`, (c) — the reverse direction — NO row with /// text t sits below T, so `LogQL`'s text filter selects exactly the same -/// rows as the DSL's number threshold. No-service (empty-key) records are -/// scanned — they show in the failure diagnostic — but never form -/// candidates (an empty service can't make a valid DSL/LogQL pair). +/// rows as the DSL's number threshold, and (d) no POISON row could be +/// selected by EITHER side's predicate: none with `number ≥ T` (the DSL +/// side) and none carrying text t at any number (the `LogQL` side) — +/// the severity pair queries the full corpus window, so a selectable +/// zero-time row is a guaranteed equivalence mismatch. No-service +/// (empty-key) records are scanned — they show in the failure diagnostic +/// — but never form candidates (an empty service can't make a valid +/// DSL/LogQL pair). fn select_pair_candidates( - per_service: &std::collections::HashMap< - String, - std::collections::HashMap>, - >, + per_service: &std::collections::HashMap, ) -> Vec<(u64, i32, &String, &String)> { // Both names are interpolated into quoted DSL and LogQL string // literals; a `"` or `\` (legal in OTLP attributes) would break or @@ -745,7 +756,7 @@ fn select_pair_candidates( .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-' | '/' | ' ')) }; let mut candidates = Vec::new(); - for (svc, bands) in per_service { + for (svc, (bands, poison)) in per_service { if !safe(svc) { continue; } @@ -774,6 +785,12 @@ fn select_pair_candidates( // LogQL side would return more than the DSL side. continue; } + if poison + .iter() + .any(|(&n, texts)| n >= threshold || texts.contains_key(text.as_str())) + { + continue; + } candidates.push((rows, threshold, svc, *text)); } } @@ -958,7 +975,10 @@ fn build_pair_specs( pair.service, pair.text ), start: pair.min_ts, - end: pair.max_ts + 1, + end: pair + .max_ts + .checked_add(1) + .expect("corpus max timestamp overflows the Loki window end"), expected_rows: pair.rows, now: corpus_now, window: corpus_window, @@ -1059,7 +1079,10 @@ fn rfc0031_indicative_comparative_run() { ) .expect("build comparative store"); let tenant = TenantId::new(built.tenant); - let corpus_now = built.max_effective_time_unix_nano + 1; + let corpus_now = built + .max_effective_time_unix_nano + .checked_add(1) + .expect("corpus max effective timestamp overflows now"); let corpus_window = built.max_effective_time_unix_nano - built.min_effective_time_unix_nano + 2; let specs = build_pair_specs(&pair, &clean_ts, &poison_ts, corpus_now, corpus_window); let ourios: Vec<_> = specs @@ -1342,3 +1365,41 @@ fn window_pair_dsl_slices_the_fixture() { got.sort_unstable(); assert_eq!(got, vec![1_001_000, 1_002_000]); } + +#[test] +fn select_pair_candidates_rejects_poisoned_bands() { + use std::collections::HashMap; + + let mut bands: SeverityBands = HashMap::new(); + bands.entry(9).or_default().insert("INFO".into(), 5); + bands.entry(13).or_default().insert("WARN".into(), 1); + + let clean: HashMap = + [("svc".to_string(), (bands.clone(), HashMap::new()))].into(); + assert!( + select_pair_candidates(&clean) + .iter() + .any(|&(rows, threshold, _, text)| rows == 1 && threshold == 13 && text == "WARN"), + "without poison the WARN band is a candidate" + ); + + // A zero-time row the DSL side would select (number ≥ threshold)… + let mut poison_high: SeverityBands = HashMap::new(); + poison_high.entry(17).or_default().insert("ERROR".into(), 1); + let poisoned: HashMap = + [("svc".to_string(), (bands.clone(), poison_high))].into(); + assert!( + select_pair_candidates(&poisoned).is_empty(), + "a poison row at number ≥ threshold disqualifies the candidate" + ); + + // …and one the LogQL side would select (same text, below threshold). + let mut poison_text: SeverityBands = HashMap::new(); + poison_text.entry(5).or_default().insert("WARN".into(), 1); + let poisoned_text: HashMap = + [("svc".to_string(), (bands, poison_text))].into(); + assert!( + select_pair_candidates(&poisoned_text).is_empty(), + "a below-threshold poison row carrying the text disqualifies the candidate" + ); +}