Skip to main content

onetaskgraph_linear/
live_artifact.rs

1//! Retained live-test identities. Recognition establishes ownership syntax only; callers
2//! must independently verify the nominated team before mutating a discovered item.
3use crate::LIVE_ARTIFACT_TITLE_PREFIX;
4
5/// Four document slots and the project that holds filed documents.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum Slot {
8    /// Project-filed document probe.
9    Filed,
10    /// Team-owned document without a project.
11    Loose,
12    /// Metadata and follow-up update probe.
13    FollowUp,
14    /// Binary copy and recopy destination.
15    Assets,
16    /// Retained project, separate from the four document slots.
17    Parent,
18}
19impl Slot {
20    fn name(self) -> &'static str {
21        match self {
22            Self::Filed => "filed",
23            Self::Loose => "loose",
24            Self::FollowUp => "follow-up",
25            Self::Assets => "assets",
26            Self::Parent => "parent",
27        }
28    }
29    fn read(value: &str) -> Option<Self> {
30        match value {
31            "filed" => Some(Self::Filed),
32            "loose" => Some(Self::Loose),
33            "follow-up" => Some(Self::FollowUp),
34            "assets" => Some(Self::Assets),
35            "parent" => Some(Self::Parent),
36            _ => None,
37        }
38    }
39}
40
41/// CI identities are stable; local identities belong to one registered invocation.
42#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43pub struct Scope<'a> {
44    team: &'a str,
45    stamp: Option<&'a str>,
46}
47impl<'a> Scope<'a> {
48    /// Validate the canonical team key of the standing CI set.
49    pub fn ci(team: &'a str) -> Result<Self, String> {
50        if !team_valid(team) {
51            return Err("invalid retained Linear team identity".into());
52        }
53        Ok(Self { team, stamp: None })
54    }
55
56    /// Validate the team key and a canonical, positively vouched local ownership stamp.
57    pub fn local(team: &'a str, stamp: &'a str) -> Result<Self, String> {
58        let scope = Self::ci(team)?;
59        if !stamp_valid(stamp) {
60            return Err("invalid retained Linear local ownership stamp".into());
61        }
62        Ok(Self {
63            stamp: Some(stamp),
64            ..scope
65        })
66    }
67
68    /// The validated canonical nominated team key.
69    #[must_use]
70    pub fn team(self) -> &'a str {
71        self.team
72    }
73
74    /// The validated invocation stamp, absent for the standing CI set.
75    #[must_use]
76    pub fn local_stamp(self) -> Option<&'a str> {
77        self.stamp
78    }
79
80    /// Whether this is the standing CI set.
81    #[must_use]
82    pub fn is_ci(self) -> bool {
83        self.stamp.is_none()
84    }
85}
86
87/// Exact recognized identity; this alone does not verify remote team membership.
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub struct Retained<'a> {
90    /// Who owns this standing set.
91    pub scope: Scope<'a>,
92    /// Which probe or parent this object represents.
93    pub slot: Slot,
94}
95
96fn team_valid(team: &str) -> bool {
97    !team.is_empty()
98        && team
99            .bytes()
100            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == b'_' || c == b'-')
101}
102fn stamp_valid(stamp: &str) -> bool {
103    let parts: Vec<_> = stamp.split('-').collect();
104    parts.len() == 3
105        && parts.iter().all(|part| {
106            !part.starts_with('0')
107                && part.bytes().all(|c| c.is_ascii_digit())
108                && part.parse::<u64>().is_ok_and(|value| value > 0)
109        })
110        && parts[..2].iter().all(|part| part.parse::<u32>().is_ok())
111}
112
113/// Format an identity whose team and stamp were validated at scope construction.
114pub fn retained_title(scope: Scope<'_>, slot: Slot) -> Result<String, String> {
115    let suffix = match scope.local_stamp() {
116        None => format!("ci {} {}", scope.team(), slot.name()),
117        Some(stamp) => format!("local {} {} {stamp}", scope.team(), slot.name()),
118    };
119    Ok(format!("{LIVE_ARTIFACT_TITLE_PREFIX}retained-v1 {suffix}"))
120}
121
122/// Recognize only exact identities. Unknown versions, extra tokens and malformed markers
123/// confer no authority, including authority to clean up an item.
124#[must_use]
125pub fn classify_retained(title: &str) -> Option<Retained<'_>> {
126    let suffix = title
127        .strip_prefix(LIVE_ARTIFACT_TITLE_PREFIX)?
128        .strip_prefix("retained-v1 ")?;
129    let parts: Vec<_> = suffix.split(' ').collect();
130    let (scope, slot) = match parts.as_slice() {
131        ["ci", team, slot] => (Scope::ci(team).ok()?, Slot::read(slot)?),
132        ["local", team, slot, stamp] => (Scope::local(team, stamp).ok()?, Slot::read(slot)?),
133        _ => return None,
134    };
135    Some(Retained { scope, slot })
136}