descendit 0.0.2

Deterministic structural metrics and loss scoring for Rust code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Aggregation policies for item-level utilities and cross-dimension scalarization.
//!
//! This separates two mathematical concerns:
//!
//! - artifact aggregation: how a dimension combines many item scores
//! - objective scalarization: how the final composite combines dimensions
//!
//! The defaults preserve current behavior, but the policy types make future
//! tail-aware or corpus-aware aggregation explicit.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Policies for aggregating item-level utility scores into a dimension score.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ArtifactAggregation {
    /// Current default: geometric mean in utility space.
    #[default]
    GeometricMeanScore,
    /// Arithmetic mean in utility space.
    ArithmeticMeanScore,
    /// Aggregate on losses using an Lp / power mean.
    ///
    /// This policy is intended for `p >= 1.0`, where larger values punish bad
    /// tails more strongly. Values below `1.0` are clamped to `1.0`.
    PowerMeanLoss { p: f64 },
    /// Aggregate on losses with an average plus tail-risk term.
    ///
    /// Large `tail_weight` values can saturate the resulting score at `0.0`
    /// when the tail is severe enough.
    MeanPlusCvarLoss { alpha: f64, tail_weight: f64 },
    /// Aggregate item losses hierarchically: first within each file using
    /// size-weighted means, then across files using size-weighted means.
    ///
    /// This reduces split/merge gaming by making file-level loss less sensitive
    /// to the raw number of items in that file. The weighting is intentionally
    /// asymmetric: items are weighted by `size_weight(size_i)` within a file,
    /// while files are weighted by `size_weight(sum(size_i))` across files.
    HierarchicalFileWeightedMeanLoss {
        #[serde(default)]
        size_weighting: ArtifactSizeWeighting,
    },
}

/// Policies for combining dimension scores into a final composite.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ObjectiveScalarization {
    /// Current default: geometric mean in utility space.
    #[default]
    GeometricMeanScore,
    /// Arithmetic mean in utility space.
    ArithmeticMeanScore,
}

/// Weighting functions for size-aware artifact aggregation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ArtifactSizeWeighting {
    #[serde(rename = "uniform")]
    UniformWeight,
    #[serde(rename = "linear")]
    LinearWeight,
    #[serde(rename = "sqrt")]
    #[default]
    SqrtWeight,
    #[serde(rename = "log2")]
    Log2Weight,
}

/// One scored item eligible for artifact aggregation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ArtifactAggregationObservation<'a> {
    pub file: &'a str,
    pub score: f64,
    pub size_hint: f64,
}

pub fn aggregate_artifact_scores(policy: ArtifactAggregation, scores: &[f64]) -> f64 {
    if scores.is_empty() {
        return 1.0;
    }

    match policy {
        ArtifactAggregation::GeometricMeanScore => geometric_mean(scores),
        ArtifactAggregation::ArithmeticMeanScore => arithmetic_mean(scores),
        ArtifactAggregation::PowerMeanLoss { p } => {
            let losses: Vec<f64> = scores.iter().map(|score| 1.0 - score).collect();
            let aggregated_loss = power_mean(&losses, p);
            (1.0 - aggregated_loss).clamp(0.0, 1.0)
        }
        ArtifactAggregation::MeanPlusCvarLoss { alpha, tail_weight } => {
            let losses: Vec<f64> = scores.iter().map(|score| 1.0 - score).collect();
            let mean_loss = arithmetic_mean(&losses);
            let tail_loss = cvar_loss(&losses, alpha);
            (1.0 - (mean_loss + tail_weight * tail_loss)).clamp(0.0, 1.0)
        }
        ArtifactAggregation::HierarchicalFileWeightedMeanLoss { .. } => {
            let observations: Vec<_> = scores
                .iter()
                .map(|&score| ArtifactAggregationObservation {
                    file: "<ungrouped>",
                    score,
                    size_hint: 1.0,
                })
                .collect();
            aggregate_artifact_observations(policy, &observations)
        }
    }
}

pub fn aggregate_artifact_observations(
    policy: ArtifactAggregation,
    observations: &[ArtifactAggregationObservation<'_>],
) -> f64 {
    if observations.is_empty() {
        return 1.0;
    }

    match policy {
        ArtifactAggregation::GeometricMeanScore
        | ArtifactAggregation::ArithmeticMeanScore
        | ArtifactAggregation::PowerMeanLoss { .. }
        | ArtifactAggregation::MeanPlusCvarLoss { .. } => {
            let scores: Vec<f64> = observations
                .iter()
                .map(|observation| observation.score)
                .collect();
            aggregate_artifact_scores(policy, &scores)
        }
        ArtifactAggregation::HierarchicalFileWeightedMeanLoss { size_weighting } => {
            hierarchical_file_weighted_mean_loss(observations, size_weighting)
        }
    }
}

pub fn scalarize_dimension_scores(policy: ObjectiveScalarization, scores: &[f64]) -> f64 {
    if scores.is_empty() {
        return 1.0;
    }

    match policy {
        ObjectiveScalarization::GeometricMeanScore => geometric_mean(scores),
        ObjectiveScalarization::ArithmeticMeanScore => arithmetic_mean(scores),
    }
}

fn arithmetic_mean(values: &[f64]) -> f64 {
    if values.is_empty() {
        return 1.0;
    }

    values.iter().sum::<f64>() / values.len() as f64
}

fn geometric_mean(values: &[f64]) -> f64 {
    if values.is_empty() {
        return 1.0;
    }

    if values.contains(&0.0) {
        return 0.0;
    }

    let log_sum: f64 = values.iter().map(|value| value.ln()).sum();
    (log_sum / values.len() as f64).exp()
}

fn power_mean(losses: &[f64], p: f64) -> f64 {
    if losses.is_empty() {
        return 0.0;
    }

    let exponent = p.max(1.0);
    let mean_power =
        losses.iter().map(|loss| loss.powf(exponent)).sum::<f64>() / losses.len() as f64;
    mean_power.powf(1.0 / exponent)
}

fn cvar_loss(losses: &[f64], alpha: f64) -> f64 {
    if losses.is_empty() {
        return 0.0;
    }

    let mut sorted = losses.to_vec();
    sorted.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));

    let tail_fraction = alpha.clamp(f64::EPSILON, 1.0);
    let tail_count = ((sorted.len() as f64) * tail_fraction).ceil() as usize;
    let take = tail_count.clamp(1, sorted.len());
    arithmetic_mean(&sorted[..take])
}

fn hierarchical_file_weighted_mean_loss(
    observations: &[ArtifactAggregationObservation<'_>],
    size_weighting: ArtifactSizeWeighting,
) -> f64 {
    let mut files: BTreeMap<&str, Vec<ArtifactAggregationObservation<'_>>> = BTreeMap::new();
    for observation in observations {
        files
            .entry(observation.file)
            .or_default()
            .push(*observation);
    }

    let mut file_losses: Vec<f64> = Vec::with_capacity(files.len());
    let mut file_weights: Vec<f64> = Vec::with_capacity(files.len());

    for file_observations in files.values() {
        let item_losses: Vec<f64> = file_observations
            .iter()
            .map(|observation| 1.0 - observation.score)
            .collect();
        let item_weights: Vec<f64> = file_observations
            .iter()
            .map(|observation| size_weight(size_weighting, observation.size_hint))
            .collect();
        let file_loss = weighted_mean(&item_losses, &item_weights);
        let total_size: f64 = file_observations
            .iter()
            .map(|observation| observation.size_hint.max(1.0))
            .sum();

        file_losses.push(file_loss);
        file_weights.push(size_weight(size_weighting, total_size));
    }

    (1.0 - weighted_mean(&file_losses, &file_weights)).clamp(0.0, 1.0)
}

fn weighted_mean(values: &[f64], weights: &[f64]) -> f64 {
    if values.is_empty() {
        return 0.0;
    }

    let total_weight: f64 = weights.iter().sum();
    if total_weight <= f64::EPSILON {
        return arithmetic_mean(values);
    }

    values
        .iter()
        .zip(weights)
        .map(|(value, weight)| value * weight)
        .sum::<f64>()
        / total_weight
}

fn size_weight(size_weighting: ArtifactSizeWeighting, size_hint: f64) -> f64 {
    let size = size_hint.max(1.0);
    match size_weighting {
        ArtifactSizeWeighting::UniformWeight => 1.0,
        ArtifactSizeWeighting::LinearWeight => size,
        ArtifactSizeWeighting::SqrtWeight => size.sqrt(),
        ArtifactSizeWeighting::Log2Weight => size.log2().max(1.0),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_artifact_aggregation_matches_geometric_mean() {
        let scores = [1.0, 0.5];
        let aggregated =
            aggregate_artifact_scores(ArtifactAggregation::GeometricMeanScore, &scores);

        assert!((aggregated - std::f64::consts::FRAC_1_SQRT_2).abs() < 1e-10);
    }

    #[test]
    fn test_power_mean_loss_penalizes_bad_tail() {
        let scores = [1.0, 0.2];
        let arithmetic =
            aggregate_artifact_scores(ArtifactAggregation::ArithmeticMeanScore, &scores);
        let power_mean =
            aggregate_artifact_scores(ArtifactAggregation::PowerMeanLoss { p: 2.0 }, &scores);

        assert!(power_mean < arithmetic);
    }

    #[test]
    fn test_power_mean_loss_clamps_subunit_exponents() {
        let scores = [1.0, 0.2];
        let arithmetic =
            aggregate_artifact_scores(ArtifactAggregation::ArithmeticMeanScore, &scores);
        let power_mean =
            aggregate_artifact_scores(ArtifactAggregation::PowerMeanLoss { p: 0.5 }, &scores);

        assert!((power_mean - arithmetic).abs() < 1e-10);
    }

    #[test]
    fn test_mean_plus_cvar_loss_penalizes_worst_tail() {
        let scores = [1.0, 1.0, 0.1];
        let arithmetic =
            aggregate_artifact_scores(ArtifactAggregation::ArithmeticMeanScore, &scores);
        let tail = aggregate_artifact_scores(
            ArtifactAggregation::MeanPlusCvarLoss {
                alpha: 0.34,
                tail_weight: 0.5,
            },
            &scores,
        );

        assert!(tail < arithmetic);
    }

    #[test]
    fn test_hierarchical_file_weighted_mean_loss_respects_file_grouping() {
        let observations = [
            ArtifactAggregationObservation {
                file: "src/a.rs",
                score: 0.2,
                size_hint: 100.0,
            },
            ArtifactAggregationObservation {
                file: "src/a.rs",
                score: 0.8,
                size_hint: 20.0,
            },
            ArtifactAggregationObservation {
                file: "src/b.rs",
                score: 0.9,
                size_hint: 10.0,
            },
        ];

        let score = aggregate_artifact_observations(
            ArtifactAggregation::HierarchicalFileWeightedMeanLoss {
                size_weighting: ArtifactSizeWeighting::LinearWeight,
            },
            &observations,
        );

        let file_a_loss = ((1.0 - 0.2) * 100.0 + (1.0 - 0.8) * 20.0) / 120.0;
        let file_b_loss = 1.0 - 0.9;
        let expected = 1.0 - ((file_a_loss * 120.0 + file_b_loss * 10.0) / 130.0);

        assert!((score - expected).abs() < 1e-10);
    }

    #[test]
    fn test_hierarchical_file_weighted_mean_loss_can_be_less_gameable_than_plain_mean() {
        let scores = [0.2, 0.8, 0.9];
        let plain = aggregate_artifact_scores(ArtifactAggregation::ArithmeticMeanScore, &scores);
        let hierarchical = aggregate_artifact_observations(
            ArtifactAggregation::HierarchicalFileWeightedMeanLoss {
                size_weighting: ArtifactSizeWeighting::LinearWeight,
            },
            &[
                ArtifactAggregationObservation {
                    file: "src/a.rs",
                    score: 0.2,
                    size_hint: 100.0,
                },
                ArtifactAggregationObservation {
                    file: "src/a.rs",
                    score: 0.8,
                    size_hint: 20.0,
                },
                ArtifactAggregationObservation {
                    file: "src/b.rs",
                    score: 0.9,
                    size_hint: 10.0,
                },
            ],
        );

        assert!(hierarchical < plain);
    }

    #[test]
    fn test_artifact_aggregation_is_monotone_under_regressions() {
        let baseline = [
            ArtifactAggregationObservation {
                file: "src/lib.rs",
                score: 1.0,
                size_hint: 10.0,
            },
            ArtifactAggregationObservation {
                file: "src/lib.rs",
                score: 1.0,
                size_hint: 20.0,
            },
            ArtifactAggregationObservation {
                file: "src/extra.rs",
                score: 1.0,
                size_hint: 5.0,
            },
        ];
        let mild_regression = [
            ArtifactAggregationObservation {
                score: 1.0,
                ..baseline[0]
            },
            ArtifactAggregationObservation {
                score: 0.8,
                ..baseline[1]
            },
            ArtifactAggregationObservation {
                score: 1.0,
                ..baseline[2]
            },
        ];
        let severe_regression = [
            ArtifactAggregationObservation {
                score: 1.0,
                ..baseline[0]
            },
            ArtifactAggregationObservation {
                score: 0.4,
                ..baseline[1]
            },
            ArtifactAggregationObservation {
                score: 1.0,
                ..baseline[2]
            },
        ];

        for policy in [
            ArtifactAggregation::GeometricMeanScore,
            ArtifactAggregation::ArithmeticMeanScore,
            ArtifactAggregation::PowerMeanLoss { p: 2.0 },
            ArtifactAggregation::MeanPlusCvarLoss {
                alpha: 0.34,
                tail_weight: 0.5,
            },
            ArtifactAggregation::HierarchicalFileWeightedMeanLoss {
                size_weighting: ArtifactSizeWeighting::SqrtWeight,
            },
        ] {
            let baseline_score = aggregate_artifact_observations(policy, &baseline);
            let mild_score = aggregate_artifact_observations(policy, &mild_regression);
            let severe_score = aggregate_artifact_observations(policy, &severe_regression);

            assert!(
                baseline_score >= mild_score - 1e-12,
                "{policy:?} should be monotone"
            );
            assert!(
                mild_score >= severe_score - 1e-12,
                "{policy:?} should be monotone"
            );
        }
    }

    #[test]
    fn test_objective_scalarization_is_monotone_under_dimension_regressions() {
        let baseline = [1.0, 1.0, 1.0];
        let mild_regression = [1.0, 0.8, 1.0];
        let severe_regression = [1.0, 0.4, 1.0];

        for policy in [
            ObjectiveScalarization::GeometricMeanScore,
            ObjectiveScalarization::ArithmeticMeanScore,
        ] {
            let baseline_score = scalarize_dimension_scores(policy, &baseline);
            let mild_score = scalarize_dimension_scores(policy, &mild_regression);
            let severe_score = scalarize_dimension_scores(policy, &severe_regression);

            assert!(
                baseline_score >= mild_score - 1e-12,
                "{policy:?} should be monotone"
            );
            assert!(
                mild_score >= severe_score - 1e-12,
                "{policy:?} should be monotone"
            );
        }
    }
}