waggle-core 0.4.0

Sans-I/O core of waggle: tokens, timestamps, entropy injection. No clock, no I/O, no storage — every effect is a parameter. Compiles to wasm32 unchanged.
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! The attribution manifest: three zones with three mutability rules
//! (design doc `02 §2`) — the immutable core fixed at mint, variants fixed
//! at mint (v1), and versioned mutable sections whose every change is an
//! event first (doc `04 §4`).
//!
//! This module is the *data model*; the sealed selection algorithm over
//! variants lands in CP-2 and lives beside it, never inside it.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::slug::{Channel, Sharer};
use crate::target::{CanonicalUrl, MediaRef, TargetMeta};
use crate::time::Timestamp;
use crate::token::Token;

/// Schema version of the manifest envelope — versioned independently of
/// crate semver; additive changes only (doc `09 §6`).
pub const MANIFEST_SCHEMA_VERSION: u16 = 1;

/// A set of consumer modalities, as a small bitset.
///
/// Match dimensions for variants (doc `06 §2`); `vision`/`audio` are what
/// make multimodal variants ride the ordinary matcher.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct ModalitySet(u8);

impl<'de> Deserialize<'de> for ModalitySet {
    /// Accepts BOTH wire forms: the compact bits (`5`) and the humane
    /// names (`["text", "shell"]`) — hosts and switchboards write names;
    /// the log and the vectors keep bits.
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct V;
        impl<'de> serde::de::Visitor<'de> for V {
            type Value = ModalitySet;
            fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                f.write_str("modality bits (u8) or names ([\"text\", ...])")
            }
            fn visit_u64<E: serde::de::Error>(self, bits: u64) -> Result<ModalitySet, E> {
                Ok(ModalitySet::from_bits_truncate(
                    u8::try_from(bits).map_err(E::custom)?,
                ))
            }
            fn visit_seq<A: serde::de::SeqAccess<'de>>(
                self,
                mut seq: A,
            ) -> Result<ModalitySet, A::Error> {
                let mut set = ModalitySet::empty();
                while let Some(name) = seq.next_element::<String>()? {
                    set = set.with(ModalitySet::from_name(&name).ok_or_else(|| {
                        serde::de::Error::custom(format!(
                            "unknown modality `{name}` — text, browser, shell, vision, audio"
                        ))
                    })?);
                }
                Ok(set)
            }
        }
        deserializer.deserialize_any(V)
    }
}

impl ModalitySet {
    /// Plain text I/O.
    pub const TEXT: Self = Self(1);
    /// Can drive a browser.
    pub const BROWSER: Self = Self(1 << 1);
    /// Can run shell commands.
    pub const SHELL: Self = Self(1 << 2);
    /// Can interpret images.
    pub const VISION: Self = Self(1 << 3);
    /// Can interpret audio.
    pub const AUDIO: Self = Self(1 << 4);

    /// The empty set.
    #[must_use]
    pub const fn empty() -> Self {
        Self(0)
    }

    /// Set union.
    #[must_use]
    pub const fn with(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Does `self` contain every modality in `required`?
    #[must_use]
    pub const fn contains(self, required: Self) -> bool {
        self.0 & required.0 == required.0
    }

    /// A single modality by its wire name.
    #[must_use]
    pub fn from_name(name: &str) -> Option<Self> {
        match name {
            "text" => Some(Self::TEXT),
            "browser" => Some(Self::BROWSER),
            "shell" => Some(Self::SHELL),
            "vision" => Some(Self::VISION),
            "audio" => Some(Self::AUDIO),
            _ => None,
        }
    }

    /// Build from raw bits, ignoring undefined ones — for hosts
    /// deserializing foreign context descriptors (and for exhaustive
    /// testing over the modality space).
    #[must_use]
    pub const fn from_bits_truncate(bits: u8) -> Self {
        Self(bits & 0b1_1111)
    }
}

/// The consumer's execution posture.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Posture {
    /// A human is present now.
    Attended,
    /// No human present (SSH box, background agent).
    Headless,
    /// Automation context (CI, cron) — fail closed on any would-be prompt.
    Ci,
}

/// One dimension's constraint in a [`MatchExpr`]: unconstrained, or an
/// allow-set. Kept as data — expressiveness grows by adding *dimensions*,
/// never by adding cleverness (doc `06 §2`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Constraint {
    /// Matches anything.
    #[default]
    Any,
    /// Matches when the context's value is one of these.
    OneOf(Vec<String>),
}

/// Which resolver contexts a variant serves. A conjunction over four
/// dimensions; specificity = number of constrained dimensions.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MatchExpr {
    /// Model family constraint (`claude`, `gpt`, `gemini`, …).
    #[serde(default, skip_serializing_if = "constraint_is_any")]
    pub model_family: Constraint,
    /// Harness constraint (`claude-code`, `codex`, …).
    #[serde(default, skip_serializing_if = "constraint_is_any")]
    pub harness: Constraint,
    /// Required modalities (superset match), if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub modalities: Option<ModalitySet>,
    /// Posture constraint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub posture: Option<Vec<Posture>>,
}

#[allow(clippy::trivially_copy_pass_by_ref)] // serde skip_serializing_if signature
fn constraint_is_any(c: &Constraint) -> bool {
    matches!(c, Constraint::Any)
}

impl MatchExpr {
    /// The catch-all: matches every context. Every manifest must carry one
    /// variant with this expression so selection is total (doc `06 §2`).
    #[must_use]
    pub fn any() -> Self {
        Self::default()
    }

    /// True when no dimension is constrained.
    #[must_use]
    pub fn is_catch_all(&self) -> bool {
        matches!(self.model_family, Constraint::Any)
            && matches!(self.harness, Constraint::Any)
            && self.modalities.is_none()
            && self.posture.is_none()
    }

    /// Specificity: how many dimensions are constrained (0–4). Selection
    /// ranks by this; the algorithm itself is CP-2's sealed matcher.
    #[must_use]
    pub fn specificity(&self) -> u8 {
        u8::from(!matches!(self.model_family, Constraint::Any))
            + u8::from(!matches!(self.harness, Constraint::Any))
            + u8::from(self.modalities.is_some())
            + u8::from(self.posture.is_some())
    }
}

/// A variant's body: small content inline, larger content by reference
/// (rev 2.3 — the threshold is automatic at mint, doc `02`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum VariantBody {
    /// Inline content (≤ [`crate::INLINE_THRESHOLD_BYTES`]).
    Inline {
        /// MIME type of `data`.
        content_type: String,
        /// The content itself.
        data: String,
    },
    /// Bytes by reference with integrity.
    Media(MediaRef),
}

/// One projection of the artifact for a class of consumers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Variant {
    /// Which contexts this variant serves.
    #[serde(rename = "match")]
    pub match_expr: MatchExpr,
    /// What those consumers receive.
    pub body: VariantBody,
    /// Advisory freshness window in ms for resolutions served from this
    /// variant (G-3). `None` ⇒ [`crate::DEFAULT_REVALIDATE_MS`]. Short for
    /// sensitive artifacts.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub revalidate_after_ms: Option<u64>,
}

/// A token's lifecycle disposition at a point in time.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Disposition {
    /// Live and servable.
    Active,
    /// Past its `expires_at`.
    Expired,
    /// Withdrawn; tombstoned, never recycled (I-6).
    Revoked {
        /// When it was revoked.
        at: Timestamp,
    },
    /// Replaced by a corrected token; late resolvers follow the pointer.
    Superseded {
        /// The replacement token.
        by: Token,
    },
}

/// The stored, retrievable record behind a token (doc `02 §2`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttributionManifest {
    /// Envelope schema version — pinned, additive-only.
    pub schema: u16,
    /// The token this manifest belongs to.
    pub token: Token,
    /// Permanent identity of the target artifact.
    pub target: CanonicalUrl,
    /// Who minted — attribution, independent of authorship.
    pub sharer: Sharer,
    /// Where this share lives.
    pub channel: Channel,
    /// When it was minted.
    pub minted_at: Timestamp,
    /// Mint-time snapshot of the target (never scraped — I-3).
    #[serde(default, skip_serializing_if = "meta_is_empty")]
    pub meta: TargetMeta,
    /// Parent token when this was minted as a delegation child (lineage).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<Token>,
    /// The artifact's bytes at mint, content-addressed (doc `18 §3`) —
    /// set by snapshot minting, immutable like the rest of the core.
    /// `read`/`search` prefer this over the live target: what you grep is
    /// what was minted, wherever the blobs replicate.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content: Option<MediaRef>,
    /// The projections. Always ≥1; exactly one catch-all guaranteed at mint.
    pub variants: Vec<Variant>,
    /// Capability-URL semantics (CP-11): a private token IS its own
    /// credential — minted long enough to be unguessable, and refused by
    /// public surfaces (unfurls, social renderers). Immutable + signed.
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub private: bool,
    /// The consumption contract, if the author declared one at mint
    /// (doc `19 §4.2`): which regions a consumer must reach. Immutable
    /// core — signed; absent for the (default) contract-free mint, so
    /// pre-contract manifests keep their exact canonical bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contract: Option<crate::Contract>,
    /// The symbol outline blob (doc `20 §3`): structure extracted from
    /// the snapshot at mint (`application/waggle-outline+json`,
    /// content-addressed). A pointer only — the outline is content, not
    /// manifest. Immutable core — signed; absent when no grammar
    /// matched, so outline-free manifests keep their exact bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub outline: Option<MediaRef>,
    /// Author signature over the immutable core (CP-11); set at mint by
    /// hosts that hold an identity. NOT itself part of the signed bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signature: Option<SignatureBlock>,
    /// Mutable-section version — CAS target for lifecycle mutations (C-9).
    pub version: u32,
    /// Cosmetic: campaign label (LWW).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub campaign: Option<String>,
    /// Cosmetic: labels (LWW).
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub labels: BTreeMap<String, String>,
    /// Lifecycle: expiry, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<Timestamp>,
    /// Lifecycle: revocation instant, if revoked.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub revoked_at: Option<Timestamp>,
    /// Lifecycle: replacement pointer, if superseded.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub superseded_by: Option<Token>,
}

fn meta_is_empty(m: &TargetMeta) -> bool {
    *m == TargetMeta::default()
}

/// A detached signature over the manifest's IMMUTABLE core (doc 14
/// CP-11): mutations never touch what was signed, so lifecycle churn
/// never invalidates it. Set by the host at mint; verified by anyone.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SignatureBlock {
    /// Signature algorithm — `ed25519` today.
    pub alg: String,
    /// The signer's verifying key, hex-encoded (32 bytes).
    pub key: String,
    /// The signature, hex-encoded (64 bytes).
    pub sig: String,
}

/// Apply one mutable-section change to a manifest — THE mutation semantic,
/// shared by `ManifestFold`, every backend, and reconstruct so they can
/// never disagree (R-4's precondition). Lifecycle changes bump `version`
/// (the C-9 CAS baseline); cosmetic changes don't. CAS checking is the
/// store's job at its commit point; this function only applies.
pub fn apply_change(
    manifest: &mut AttributionManifest,
    change: &crate::log::Change,
    at: Timestamp,
) {
    use crate::log::Change;
    match change {
        Change::Revoked => {
            manifest.revoked_at = Some(at);
            manifest.version += 1;
        }
        Change::Superseded { by } => {
            manifest.superseded_by = Some(*by);
            manifest.version += 1;
        }
        Change::ExpirySet { expires_at } => {
            manifest.expires_at = *expires_at;
            manifest.version += 1;
        }
        Change::CampaignSet { campaign } => manifest.campaign.clone_from(campaign),
        Change::LabelSet { key, value } => {
            manifest.labels.insert(key.clone(), value.clone());
        }
        Change::LabelUnset { key } => {
            manifest.labels.remove(key);
        }
    }
}

impl AttributionManifest {
    /// Lifecycle disposition at `now`. Precedence: revoked > superseded >
    /// expired > active (a revoked token stays revoked even if also
    /// superseded — revocation is the stronger claim).
    #[must_use]
    pub fn disposition(&self, now: Timestamp) -> Disposition {
        if let Some(at) = self.revoked_at {
            return Disposition::Revoked { at };
        }
        if let Some(by) = self.superseded_by {
            return Disposition::Superseded { by };
        }
        match self.expires_at {
            Some(exp) if exp <= now => Disposition::Expired,
            _ => Disposition::Active,
        }
    }
}

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

    #[test]
    fn modality_set_algebra() {
        let ctx = ModalitySet::TEXT.with(ModalitySet::VISION);
        assert!(ctx.contains(ModalitySet::VISION));
        assert!(ctx.contains(ModalitySet::empty()));
        assert!(!ctx.contains(ModalitySet::BROWSER));
        assert!(!ctx.contains(ModalitySet::VISION.with(ModalitySet::AUDIO)));
    }

    #[test]
    fn catch_all_and_specificity() {
        assert!(MatchExpr::any().is_catch_all());
        assert_eq!(MatchExpr::any().specificity(), 0);
        let m = MatchExpr {
            model_family: Constraint::OneOf(vec!["claude".into()]),
            modalities: Some(ModalitySet::VISION),
            ..MatchExpr::default()
        };
        assert!(!m.is_catch_all());
        assert_eq!(m.specificity(), 2);
    }

    #[test]
    fn disposition_precedence() {
        let now = Timestamp::from_unix_ms(10_000);
        let mut entropy = |buf: &mut [u8]| {
            buf.fill(7);
            Ok(())
        };
        let m = crate::mint(
            crate::MintSpec::new(
                CanonicalUrl::new("file:///tmp/x.md").unwrap(),
                Sharer::new("lead").unwrap(),
                Channel::subagent_general(),
            ),
            &crate::MintOptions::default(),
            &mut entropy,
            now,
        )
        .unwrap();

        assert_eq!(m.disposition(now), Disposition::Active);

        let mut expired = m.clone();
        expired.expires_at = Some(now);
        assert_eq!(expired.disposition(now), Disposition::Expired);
        assert_eq!(
            expired.disposition(Timestamp::from_unix_ms(9_999)),
            Disposition::Active,
            "expiry is exclusive of earlier instants"
        );

        let other = Token::parse("zzz").unwrap();
        let mut superseded = expired.clone();
        superseded.superseded_by = Some(other);
        assert_eq!(
            superseded.disposition(now),
            Disposition::Superseded { by: other }
        );

        let mut revoked = superseded;
        revoked.revoked_at = Some(now);
        assert_eq!(
            revoked.disposition(now),
            Disposition::Revoked { at: now },
            "revocation outranks supersession and expiry"
        );
    }

    #[test]
    fn manifest_serde_roundtrip() {
        let now = Timestamp::from_unix_ms(1);
        let mut entropy = |buf: &mut [u8]| {
            buf.fill(9);
            Ok(())
        };
        let m = crate::mint(
            crate::MintSpec::new(
                CanonicalUrl::new("ws://a/b").unwrap(),
                Sharer::new("lead").unwrap(),
                Channel::new("subagent/pricing").unwrap(),
            )
            .variant(
                MatchExpr {
                    modalities: Some(ModalitySet::VISION),
                    ..MatchExpr::default()
                },
                VariantBody::Inline {
                    content_type: "text/markdown".into(),
                    data: "see the chart".into(),
                },
            ),
            &crate::MintOptions::default(),
            &mut entropy,
            now,
        )
        .unwrap();
        let json = serde_json::to_string(&m).unwrap();
        let back: AttributionManifest = serde_json::from_str(&json).unwrap();
        assert_eq!(back, m);
        assert_eq!(back.schema, MANIFEST_SCHEMA_VERSION);
    }
}