Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions bins/uast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ fn output_node(node: &cf_uast::Node, output: &str, format: &str) -> Result<(), S

/// Writes `node.to_map()` through the shared go-compat encoder (DESIGN rule 1).
fn write_node(writer: &mut dyn Write, node: &cf_uast::Node, pretty: bool) -> Result<(), String> {
// Go parity: on an empty source the reference parser returns a NIL node
// and `json.Marshal(nil)` emits `null`. The Rust loader surfaces that
// collapsed root as `Node::default()` (empty type, no children, zero
// positions) — a shape no real parse produces, since every lowered node
// carries a type.
if node.node_type.is_empty() && node.children.is_empty() && node.token.is_empty() {
return writeln!(writer, "null").map_err(|e| e.to_string());
}
let value = node_to_value(node);
cf_textutil::write_json(writer, &value, pretty).map_err(|e| e.to_string())
}
Expand Down
3 changes: 3 additions & 0 deletions crates/cf-alg-mapx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ publish.workspace = true
description = "Generic map and slice helpers (clone, additive merge, deterministic sorted_keys, sort-and-limit, lookup-set, unique)."

[dependencies]
# Frozen Go pdqsort: `sort_and_limit` mirrors the reference `mapx.SortAndLimit`
# (`sort.Slice`), whose unstable tie permutation is part of the output contract.
cf-gosort = { path = "../cf-gosort" }
17 changes: 6 additions & 11 deletions crates/cf-alg-mapx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ where
/// mutated.
///
/// `less(a, b)` returns `true` when `a` should sort before `b`. The sort is
/// stable.
/// **unstable** — it reproduces the reference `sort.Slice` (frozen pdqsort)
/// permutation bit-for-bit, including its tie handling, because report
/// surfaces expose the exact output order of equal elements.
///
/// # Examples
///
Expand All @@ -304,22 +306,15 @@ where
/// assert_eq!(all, Some(vec![3, 2, 1]));
/// ```
#[must_use]
pub fn sort_and_limit<T, F>(items: Option<&[T]>, mut less: F, limit: i64) -> Option<Vec<T>>
pub fn sort_and_limit<T, F>(items: Option<&[T]>, less: F, limit: i64) -> Option<Vec<T>>
where
T: Clone,
F: FnMut(&T, &T) -> bool,
{
let items = items?;
let mut sorted: Vec<T> = items.to_vec();
sorted.sort_by(|a, b| {
if less(a, b) {
std::cmp::Ordering::Less
} else if less(b, a) {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
});
let less = std::cell::RefCell::new(less);
cf_gosort::go_sort_slice(&mut sorted, |a, b| (less.borrow_mut())(a, b));
if limit > 0 && sorted.len() as i64 > limit {
sorted.truncate(limit as usize);
}
Expand Down
18 changes: 12 additions & 6 deletions crates/cf-analyzer-burndown/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,20 @@ impl ComputedMetrics {

let mut m = GoMap::new_struct();
m.push("aggregate", self.aggregate.to_go_value());
// Reference: `global_survival` is a `var`-declared append target — an
// empty-walk run leaves it nil (JSON `null`, YAML `[]`), never `[]`.
m.push(
"global_survival",
GoValue::Array(
self.global_survival
.iter()
.map(SurvivalData::to_go_value)
.collect(),
),
if self.global_survival.is_empty() {
GoValue::NilSlice
} else {
GoValue::Array(
self.global_survival
.iter()
.map(SurvivalData::to_go_value)
.collect(),
)
},
);
m.push("file_survival", nil(&self.file_survival));
m.push("developer_survival", nil(&self.developer_survival));
Expand Down
4 changes: 4 additions & 0 deletions crates/cf-anomaly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ cf-alg-stats = { path = "../cf-alg-stats" }
# the SAME GoValue tree through cf-goyaml once that crate lands; the report
# types expose `to_go_value()` for both paths.
cf-gojson = { path = "../cf-gojson" }
# Bit-exact port of Go's sort.Slice (pdqsort). The anomaly lists are sorted
# with an unstable sort in the reference binary, so tie order on equal
# Z-scores is part of the report contract and must match exactly.
cf-gosort = { path = "../cf-gosort" }

[lints.clippy]
pedantic = "warn"
Expand Down
36 changes: 16 additions & 20 deletions crates/cf-anomaly/src/detect.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Anomaly detection over per-tick and external time-series data.

use std::cmp::Ordering;
use std::collections::BTreeMap;

use cf_alg_stats::mean_std_dev;
use cf_gosort::go_sort_slice;

use crate::model::{ExternalAnomaly, ExternalSummary, RawMetrics, Record, TickMetrics, ZScoreSet};
use crate::zscore::compute_z_scores;
Expand Down Expand Up @@ -67,15 +67,11 @@ pub fn detect_anomalies_from_ticks(
threshold_f,
);

// Sort by max absolute Z-score descending. The reference binary's sort is
// unstable on ties (unspecified relative order); we use a stable sort with
// a strict comparator so behavior is deterministic without altering the
// documented "most extreme first" semantics.
anomalies.sort_by(|a, b| {
b.max_abs_z_score
.partial_cmp(&a.max_abs_z_score)
.unwrap_or(Ordering::Equal)
});
// Sort by max absolute Z-score descending. The reference binary uses
// Go's unstable sort.Slice, so the relative order of ties is fixed by
// pdqsort's deterministic-but-unstable behavior; replicate it exactly
// via cf-gosort rather than a stable sort.
go_sort_slice(&mut anomalies, |a, b| a.max_abs_z_score > b.max_abs_z_score);

anomalies
}
Expand Down Expand Up @@ -201,16 +197,16 @@ pub fn detect_external_anomalies(
/// summary list by `(source, dimension)`, so callers that merge per-analyzer
/// results produce the same deterministic order as the reference binary.
pub fn sort_external_results(anomalies: &mut [ExternalAnomaly], summaries: &mut [ExternalSummary]) {
anomalies.sort_by(|a, b| {
b.z_score
.abs()
.partial_cmp(&a.z_score.abs())
.unwrap_or(Ordering::Equal)
});

summaries.sort_by(|a, b| match a.source.cmp(&b.source) {
Ordering::Equal => a.dimension.cmp(&b.dimension),
other => other,
// Both lists are sorted with Go's unstable sort.Slice in the reference
// binary; tie order must replicate pdqsort exactly.
go_sort_slice(anomalies, |a, b| a.z_score.abs() > b.z_score.abs());

go_sort_slice(summaries, |a, b| {
if a.source != b.source {
a.source < b.source
} else {
a.dimension < b.dimension
}
});
}

Expand Down
38 changes: 31 additions & 7 deletions crates/cf-anomaly/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,20 @@ impl ToGoValue for ComputedMetrics {
fn to_go_value(&self) -> GoValue {
let mut m = GoMap::new_struct();
// The report contract distinguishes an absent list from an empty one:
// an empty anomaly set marshals as `null` (no tick was flagged), while
// an empty time series — which is always materialized — marshals as
// `[]`. Pinned by the differential gate.
m.push("anomalies", records_array_or_null(&self.anomalies));
// with a non-empty time series, an empty anomaly set marshals as
// `null` (the main path's `var`-declared append target: no tick was
// flagged), while an EMPTY time series (zero-commit walk, e.g. a
// `--since` cutoff past every commit) takes the reference empty-input
// early return whose anomaly list is materialized — `[]`. Both cases
// oracle-measured; pinned by the differential gate.
m.push(
"anomalies",
if self.anomalies.is_empty() && self.time_series.is_empty() {
GoValue::Array(Vec::new())
} else {
records_array_or_null(&self.anomalies)
},
);
m.push("time_series", time_series_array(&self.time_series));
m.push("aggregate", self.aggregate.to_go_value());
if !self.external_anomalies.is_empty() {
Expand Down Expand Up @@ -573,12 +583,26 @@ mod tests {
let json = Encoder::marshal().encode_to_string(&cm.to_go_value());
assert!(!json.contains("external_anomalies"), "got: {json}");
assert!(!json.contains("external_summaries"), "got: {json}");
// With no detections `anomalies` renders as `null`, while the
// always-materialized `time_series` renders as `[]` (report contract).
assert!(json.contains(r#""anomalies":null"#), "got: {json}");
// An entirely empty report (zero-commit walk) takes the reference
// empty-input early return: BOTH lists are materialized (`[]`), never
// `null` (oracle-measured; the `null` case needs a non-empty
// time series — see `anomalies_null_with_nonempty_time_series`).
assert!(json.contains(r#""anomalies":[]"#), "got: {json}");
assert!(json.contains(r#""time_series":[]"#), "got: {json}");
}

#[test]
fn anomalies_null_with_nonempty_time_series() {
let cm = ComputedMetrics {
time_series: vec![TimeSeriesEntry::default()],
..ComputedMetrics::default()
};
let json = Encoder::marshal().encode_to_string(&cm.to_go_value());
// Main path (non-empty time series), zero detections: the reference
// `var`-declared append target stays nil => `null`.
assert!(json.contains(r#""anomalies":null"#), "got: {json}");
}

#[test]
fn aggregate_threshold_promotes_float32_to_go_float() {
let agg = AggregateData {
Expand Down
3 changes: 3 additions & 0 deletions crates/cf-clones/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ cf-analyze = { path = "../cf-analyze" }
cf-gojson = { path = "../cf-gojson" }
# CFB1 binary envelope + report accessors/formatters.
cf-reportutil = { path = "../cf-reportutil" }
# Frozen Go pdqsort — the reference's sort.Slice permutation is part of the
# terminal/text output contract (tie order among equal-similarity pairs).
cf-gosort = { path = "../cf-gosort" }

[lints.clippy]
pedantic = "warn"
Expand Down
Loading
Loading