paramodel-elements 0.2.0

Paramodel central algebra: parameters, domains, constraints, attributes, values, trials, elements, and the ElementRuntime trait.
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
// Copyright (c) Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! `Trial`, `Assignments`, `TrialMetadata`, and the trial canonical
//! byte encoding.
//!
//! A [`Trial`] is one immutable configuration — an assignment of
//! [`Value`]s across every (element, parameter) coordinate that will
//! run together, plus an identifier and optional metadata.
//! Construction goes through [`Trial::builder`]; [`Assignments::new`]
//! enforces self-consistency on the assignment map.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::attributes::{Labels, Tags};
use crate::ids::TrialId;
use crate::names::{ElementName, NameError, ParameterName};
use crate::value::Value;

// ---------------------------------------------------------------------------
// TrialError — construction-time validation for Trial / Assignments.
// ---------------------------------------------------------------------------

/// Errors from `Trial` / `Assignments` construction.
///
/// Separate from the broader crate `Error` so the construction path
/// can have a narrow, matchable type. Wrapped by `Error::Trial`
/// via `#[from]`.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum TrialError {
    /// A name newtype failed validation.
    #[error(transparent)]
    Name(#[from] NameError),

    /// An element's inner parameter map is empty.
    #[error("element '{element}' has no parameter assignments; an Assignments entry must be non-empty")]
    EmptyElementAssignments {
        /// The offending element name.
        element: String,
    },

    /// A value is keyed under a parameter name that doesn't match its
    /// own `Provenance.parameter`.
    #[error(
        "mis-addressed value: assignment key '{expected}' does not match value.provenance.parameter '{actual}'"
    )]
    MisaddressedValue {
        /// The outer-map key this value was stored under.
        expected: ParameterName,
        /// The name the value's provenance records.
        actual:   ParameterName,
    },
}

type Result<T, E = TrialError> = std::result::Result<T, E>;

// ---------------------------------------------------------------------------
// Assignments.
// ---------------------------------------------------------------------------

/// Two-level map: element name → parameter name → value.
///
/// Construction via [`Self::new`] enforces:
///
/// - For every `(element, param) → value` entry,
///   `value.provenance().parameter == param`. No silently
///   mis-addressed values.
/// - No element appears with an empty inner map.
///
/// `BTreeMap` iteration order is natural, so trial canonical bytes
/// (and anything else that walks the assignments) see stable
/// ordering without extra sorting.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Assignments(BTreeMap<ElementName, BTreeMap<ParameterName, Value>>);

impl Assignments {
    /// Empty assignments map.
    #[must_use]
    pub fn empty() -> Self {
        Self::default()
    }

    /// Construct from a pre-built map, validating invariants.
    pub fn new(map: BTreeMap<ElementName, BTreeMap<ParameterName, Value>>) -> Result<Self> {
        for (element, params) in &map {
            if params.is_empty() {
                return Err(TrialError::EmptyElementAssignments {
                    element: element.as_str().to_owned(),
                });
            }
            for (pname, value) in params {
                if value.parameter() != pname {
                    return Err(TrialError::MisaddressedValue {
                        expected: pname.clone(),
                        actual:   value.parameter().clone(),
                    });
                }
            }
        }
        Ok(Self(map))
    }

    /// Look up one `(element, parameter)` binding.
    #[must_use]
    pub fn get(&self, element: &ElementName, param: &ParameterName) -> Option<&Value> {
        self.0.get(element).and_then(|p| p.get(param))
    }

    /// Borrow the inner parameter map for an element.
    #[must_use]
    pub fn for_element(
        &self,
        element: &ElementName,
    ) -> Option<&BTreeMap<ParameterName, Value>> {
        self.0.get(element)
    }

    /// Sorted iterator over `(element, parameter, value)` triples.
    pub fn iter(&self) -> impl Iterator<Item = (&ElementName, &ParameterName, &Value)> {
        self.0
            .iter()
            .flat_map(|(e, params)| params.iter().map(move |(p, v)| (e, p, v)))
    }

    /// Total assignment count across every element.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.values().map(BTreeMap::len).sum()
    }

    /// `true` when the map has no entries.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Number of distinct elements covered.
    #[must_use]
    pub fn element_count(&self) -> usize {
        self.0.len()
    }

    /// Internal: borrow the raw map (used for canonical bytes).
    pub(crate) const fn as_map(&self) -> &BTreeMap<ElementName, BTreeMap<ParameterName, Value>> {
        &self.0
    }
}

// ---------------------------------------------------------------------------
// TrialMetadata.
// ---------------------------------------------------------------------------

/// Authoring/enumeration hints attached to a trial.
///
/// Every field is advisory; the planner treats an absent value the
/// same as one that was never set.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
pub struct TrialMetadata {
    /// Position in the producing `TrialSet`, when that position is
    /// meaningful for reporting (not for scheduling).
    pub enumeration_index: Option<u32>,

    /// Free-form group label (for related-trial clustering).
    pub group: Option<String>,

    /// Human-readable description of how the trial was generated.
    pub generation_method: Option<String>,

    /// Scheduler-hint priority; higher = earlier when otherwise equal.
    pub priority: Option<i32>,
}

// ---------------------------------------------------------------------------
// Trial.
// ---------------------------------------------------------------------------

/// One immutable trial configuration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, bon::Builder)]
pub struct Trial {
    /// Trial id.
    pub id: TrialId,

    /// The parameter assignments for every element in scope.
    pub assignments: Assignments,

    /// Intrinsic facts (e.g. the compiler-stamped `trial_code` label).
    #[builder(default)]
    pub labels: Labels,

    /// Organisational tags.
    #[builder(default)]
    pub tags: Tags,

    /// Advisory trial metadata.
    pub metadata: Option<TrialMetadata>,
}

/// Canonical encoding tag for a trial. Chosen outside the constraint
/// canonical-byte ranges (`0x10..=0x5A`) and value tag ranges
/// (`0x01..=0x05`) used elsewhere in the workspace.
pub const TRIAL_TAG: u8 = 0x60;

impl Trial {
    /// Stable byte encoding of this trial's *content*. Excludes
    /// `id`, `metadata`, `labels`, and `tags` — those are ancillary
    /// to the trial's identity. See SRD-0006 §Canonical bytes.
    ///
    /// Callers compute a hash at a trust boundary (persistence write,
    /// audit compare) with
    /// `Fingerprint::of(&trial.canonical_bytes())`.
    #[must_use]
    pub fn canonical_bytes(&self) -> Vec<u8> {
        let mut out = Vec::new();
        out.push(TRIAL_TAG);

        let assignments = self.assignments.as_map();
        let n_elements = u32::try_from(assignments.len()).expect("element count fits in u32");
        out.extend_from_slice(&n_elements.to_le_bytes());

        for (element_name, params) in assignments {
            write_str_len_prefixed(&mut out, element_name.as_str());
            let n_params = u32::try_from(params.len()).expect("parameter count fits in u32");
            out.extend_from_slice(&n_params.to_le_bytes());
            for (param_name, value) in params {
                write_str_len_prefixed(&mut out, param_name.as_str());
                // The 32-byte value fingerprint is the pre-hashed
                // content address of the assignment — we inherit
                // per-value tamper-checking for free (SRD-0004 D10).
                out.extend_from_slice(value.fingerprint().as_bytes());
            }
        }

        out
    }
}

fn write_str_len_prefixed(out: &mut Vec<u8>, s: &str) {
    let bytes = s.as_bytes();
    let len = u32::try_from(bytes.len()).expect("string length fits in u32");
    out.extend_from_slice(&len.to_le_bytes());
    out.extend_from_slice(bytes);
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use crate::{Fingerprint, IntegerValue, LabelKey, LabelValue, Value};
    use ulid::Ulid;

    use super::*;

    fn ename(s: &str) -> ElementName {
        ElementName::new(s).unwrap()
    }
    fn pname(s: &str) -> ParameterName {
        ParameterName::new(s).unwrap()
    }
    fn tid(n: u64) -> TrialId {
        TrialId::from_ulid(Ulid::from_parts(n, 1))
    }

    fn one_element_assignments(
        elem:  &str,
        param: &str,
        value: i64,
    ) -> Assignments {
        let mut inner = BTreeMap::new();
        inner.insert(pname(param), Value::integer(pname(param), value, None));
        let mut outer = BTreeMap::new();
        outer.insert(ename(elem), inner);
        Assignments::new(outer).unwrap()
    }

    // ---------- Assignments ----------

    #[test]
    fn assignments_rejects_empty_inner_map() {
        let mut outer = BTreeMap::new();
        outer.insert(ename("db"), BTreeMap::new());
        let err = Assignments::new(outer).unwrap_err();
        assert!(matches!(err, TrialError::EmptyElementAssignments { .. }));
    }

    #[test]
    fn assignments_rejects_misaddressed_value() {
        // Key says "threads" but value's provenance says "connections".
        let mut inner = BTreeMap::new();
        inner.insert(
            pname("threads"),
            Value::integer(pname("connections"), 8, None),
        );
        let mut outer = BTreeMap::new();
        outer.insert(ename("db"), inner);
        let err = Assignments::new(outer).unwrap_err();
        assert!(matches!(err, TrialError::MisaddressedValue { .. }));
    }

    #[test]
    fn assignments_iter_visits_every_triple() {
        let mut inner = BTreeMap::new();
        inner.insert(pname("a"), Value::integer(pname("a"), 1, None));
        inner.insert(pname("b"), Value::integer(pname("b"), 2, None));
        let mut outer = BTreeMap::new();
        outer.insert(ename("x"), inner);
        let a = Assignments::new(outer).unwrap();
        let triples: Vec<(&str, &str, i64)> = a
            .iter()
            .map(|(e, p, v)| (e.as_str(), p.as_str(), v.as_integer().unwrap()))
            .collect();
        assert_eq!(triples, vec![("x", "a", 1), ("x", "b", 2)]);
    }

    #[test]
    fn assignments_len_sums_across_elements() {
        let mut inner1 = BTreeMap::new();
        inner1.insert(pname("a"), Value::integer(pname("a"), 1, None));
        let mut inner2 = BTreeMap::new();
        inner2.insert(pname("a"), Value::integer(pname("a"), 1, None));
        inner2.insert(pname("b"), Value::integer(pname("b"), 2, None));
        let mut outer = BTreeMap::new();
        outer.insert(ename("x"), inner1);
        outer.insert(ename("y"), inner2);
        let a = Assignments::new(outer).unwrap();
        assert_eq!(a.len(), 3);
        assert_eq!(a.element_count(), 2);
    }

    #[test]
    fn assignments_serde_roundtrip() {
        let a = one_element_assignments("db", "threads", 8);
        let json = serde_json::to_string(&a).unwrap();
        let back: Assignments = serde_json::from_str(&json).unwrap();
        assert_eq!(a, back);
    }

    // ---------- Trial ----------

    #[test]
    fn trial_builds_and_accesses_its_fields() {
        let t = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 8))
            .build();
        assert_eq!(t.id, tid(1));
        assert_eq!(t.assignments.len(), 1);
        assert!(t.labels.is_empty());
        assert!(t.metadata.is_none());
    }

    #[test]
    fn trial_metadata_builder() {
        let md = TrialMetadata::builder()
            .enumeration_index(3)
            .group("baseline".to_owned())
            .priority(10)
            .build();
        assert_eq!(md.enumeration_index, Some(3));
        assert_eq!(md.group.as_deref(), Some("baseline"));
        assert_eq!(md.priority, Some(10));
    }

    #[test]
    fn trial_serde_roundtrip() {
        let t = Trial::builder()
            .id(tid(42))
            .assignments(one_element_assignments("db", "threads", 8))
            .build();
        let json = serde_json::to_string(&t).unwrap();
        let back: Trial = serde_json::from_str(&json).unwrap();
        assert_eq!(t, back);
    }

    // ---------- canonical_bytes ----------

    #[test]
    fn canonical_bytes_deterministic() {
        let a = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 8))
            .build();
        let b = Trial::builder()
            .id(tid(2)) // different id
            .assignments(one_element_assignments("db", "threads", 8))
            .build();
        // Different ids; canonical bytes must match (id is excluded).
        assert_eq!(a.canonical_bytes(), b.canonical_bytes());
    }

    #[test]
    fn canonical_bytes_distinguish_values() {
        let a = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 8))
            .build();
        let b = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 16))
            .build();
        assert_ne!(a.canonical_bytes(), b.canonical_bytes());
    }

    #[test]
    fn canonical_bytes_distinguish_element_names() {
        let a = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 8))
            .build();
        let b = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("cache", "threads", 8))
            .build();
        assert_ne!(a.canonical_bytes(), b.canonical_bytes());
    }

    #[test]
    fn canonical_bytes_excludes_labels_and_metadata() {
        let mut labels = Labels::new();
        labels.insert(
            LabelKey::new("trial_code").unwrap(),
            LabelValue::new("0x0001").unwrap(),
        );
        let with_labels = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 8))
            .labels(labels)
            .metadata(
                TrialMetadata::builder()
                    .enumeration_index(7)
                    .build(),
            )
            .build();
        let plain = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 8))
            .build();
        assert_eq!(with_labels.canonical_bytes(), plain.canonical_bytes());
    }

    #[test]
    fn canonical_bytes_match_hand_built_layout() {
        let t = Trial::builder()
            .id(tid(1))
            .assignments(one_element_assignments("db", "threads", 42))
            .build();
        let got = t.canonical_bytes();

        // Re-create the expected layout independently so a future
        // refactor can't silently change the wire form.
        let mut expected = vec![TRIAL_TAG];
        expected.extend_from_slice(&1u32.to_le_bytes()); // 1 element
        let elem = "db";
        expected.extend_from_slice(&u32::try_from(elem.len()).unwrap().to_le_bytes());
        expected.extend_from_slice(elem.as_bytes());
        expected.extend_from_slice(&1u32.to_le_bytes()); // 1 param
        let param = "threads";
        expected.extend_from_slice(&u32::try_from(param.len()).unwrap().to_le_bytes());
        expected.extend_from_slice(param.as_bytes());
        let fp = IntegerValue::fingerprint_of(&pname("threads"), 42);
        expected.extend_from_slice(fp.as_bytes());
        assert_eq!(got, expected);
        // And the derived hash is a deterministic function of the
        // canonical bytes, as documented.
        let _ = Fingerprint::of(&got);
    }
}