Skip to main content

postproject_core/
artifact.rs

1//! Derived knowledge state for activity-produced representations.
2
3use crate::{
4    ActivityId, ActivityKind, DependencyKind, DependencyTarget, RepresentationId, ResourceId,
5};
6
7/// One authored edge in a dependency path captured for an activity input.
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub struct ArtifactDependencyPathSegment {
10    source_representation_id: RepresentationId,
11    dependency_position: u32,
12    source_resource_id: Option<ResourceId>,
13    kind: DependencyKind,
14    target: DependencyTarget,
15    resolved_representation_id: Option<RepresentationId>,
16    authored_reference: String,
17}
18
19impl ArtifactDependencyPathSegment {
20    /// Reconstructs one storage-captured path segment.
21    #[doc(hidden)]
22    #[must_use]
23    pub fn new(
24        source_representation_id: RepresentationId,
25        dependency_position: u32,
26        source_resource_id: Option<ResourceId>,
27        kind: DependencyKind,
28        target: DependencyTarget,
29        resolved_representation_id: Option<RepresentationId>,
30        authored_reference: String,
31    ) -> Self {
32        Self {
33            source_representation_id,
34            dependency_position,
35            source_resource_id,
36            kind,
37            target,
38            resolved_representation_id,
39            authored_reference,
40        }
41    }
42
43    /// Returns the representation containing this edge.
44    #[must_use]
45    pub const fn source_representation_id(&self) -> RepresentationId {
46        self.source_representation_id
47    }
48
49    /// Returns the edge's position in its source dependency observation.
50    #[must_use]
51    pub const fn dependency_position(&self) -> u32 {
52        self.dependency_position
53    }
54
55    /// Returns the optional resource that authored the reference.
56    #[must_use]
57    pub const fn source_resource_id(&self) -> Option<ResourceId> {
58        self.source_resource_id
59    }
60
61    /// Returns the open-world dependency kind.
62    #[must_use]
63    pub const fn kind(&self) -> &DependencyKind {
64        &self.kind
65    }
66
67    /// Returns the floating or pinned target.
68    #[must_use]
69    pub const fn target(&self) -> DependencyTarget {
70        self.target
71    }
72
73    /// Returns the representation selected for a floating target.
74    #[must_use]
75    pub const fn resolved_representation_id(&self) -> Option<RepresentationId> {
76        self.resolved_representation_id
77    }
78
79    /// Returns the exact authored reference text.
80    #[must_use]
81    pub fn authored_reference(&self) -> &str {
82        &self.authored_reference
83    }
84}
85
86/// Why captured dependency evidence cannot prove currentness.
87#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
88#[non_exhaustive]
89pub enum ArtifactDependencyIssue {
90    /// A source dependency observation must be extracted again.
91    NeedsExtraction,
92    /// A floating asset target had no resolved representation.
93    Unresolved,
94    /// Capture reached the fixed dependency-depth bound.
95    DepthTruncated,
96    /// Capture reached the fixed dependency-representation bound.
97    RepresentationsTruncated,
98}
99
100/// Knowledge state of a representation produced by an activity.
101#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
102#[non_exhaustive]
103pub enum ArtifactKnowledgeState {
104    /// Every comparable input and output fingerprint still matches.
105    Current,
106    /// An input changed or an upstream artifact is not current.
107    Stale,
108    /// Stored evidence is insufficient to decide whether the artifact is current.
109    Indeterminate,
110    /// The output itself differs from the fingerprint captured when produced.
111    Diverged,
112}
113
114/// Which side of an activity supplied fingerprint evidence.
115#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
116#[non_exhaustive]
117pub enum ArtifactEdgeKind {
118    /// A representation consumed by the activity.
119    Input,
120    /// A representation produced by the activity.
121    Output,
122}
123
124/// Which explicit traversal bound stopped evaluation.
125#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
126#[non_exhaustive]
127pub enum ArtifactTraversalLimitKind {
128    /// The maximum upstream depth was reached.
129    Depth,
130    /// The maximum number of visited representations was reached.
131    Representations,
132}
133
134/// One user-explainable reason for a non-current knowledge state.
135#[derive(Clone, Debug, Eq, PartialEq)]
136#[non_exhaustive]
137pub enum ArtifactKnowledgeReason {
138    /// No activity records how the target representation was produced.
139    ProducingActivityMissing {
140        /// Representation being evaluated.
141        representation_id: RepresentationId,
142    },
143    /// More than one activity claims to have produced the representation.
144    ProducingActivityAmbiguous {
145        /// Representation being evaluated.
146        representation_id: RepresentationId,
147        /// Number of producing activities found.
148        activity_count: u32,
149    },
150    /// A legacy activity edge has no storage-captured snapshot.
151    SnapshotAbsent {
152        /// Activity owning the edge.
153        activity_id: ActivityId,
154        /// Representation referenced by the edge.
155        representation_id: RepresentationId,
156        /// Whether this was an input or output edge.
157        edge: ArtifactEdgeKind,
158    },
159    /// A legacy activity input has no dependency-closure snapshot.
160    DependencySnapshotAbsent {
161        /// Activity owning the input.
162        activity_id: ActivityId,
163        /// Input representation whose closure was not captured.
164        representation_id: RepresentationId,
165    },
166    /// Captured dependency knowledge was incomplete at activity creation.
167    DependencyKnowledgeIncomplete {
168        /// Activity owning the input.
169        activity_id: ActivityId,
170        /// Direct activity input.
171        input_representation_id: RepresentationId,
172        /// Representation at which capture became indeterminate.
173        subject_representation_id: RepresentationId,
174        /// Captured typed path to the subject or unresolved edge.
175        path: Vec<ArtifactDependencyPathSegment>,
176        /// Specific incomplete-knowledge condition.
177        issue: ArtifactDependencyIssue,
178    },
179    /// A required authored path no longer matches its captured observation.
180    DependencyPathChanged {
181        /// Activity owning the input.
182        activity_id: ActivityId,
183        /// Direct activity input.
184        input_representation_id: RepresentationId,
185        /// Captured path that changed.
186        path: Vec<ArtifactDependencyPathSegment>,
187    },
188    /// Current dependency content differs from the captured fingerprint.
189    DependencyFingerprintChanged {
190        /// Activity owning the input.
191        activity_id: ActivityId,
192        /// Direct activity input.
193        input_representation_id: RepresentationId,
194        /// Changed dependency representation.
195        representation_id: RepresentationId,
196        /// Captured typed path to the dependency.
197        path: Vec<ArtifactDependencyPathSegment>,
198        /// Fingerprint algorithm.
199        algorithm: String,
200        /// Fingerprint format version.
201        version: u16,
202        /// Captured fingerprint bytes.
203        snapshot_value: Vec<u8>,
204        /// Current fingerprint bytes.
205        current_value: Vec<u8>,
206    },
207    /// Dependency content changed and its aggregate fingerprint is not current yet.
208    DependencyFingerprintRecomputationPending {
209        /// Activity owning the input.
210        activity_id: ActivityId,
211        /// Direct activity input.
212        input_representation_id: RepresentationId,
213        /// Dependency representation awaiting recomputation.
214        representation_id: RepresentationId,
215        /// Captured typed path to the dependency.
216        path: Vec<ArtifactDependencyPathSegment>,
217    },
218    /// A dependency lacks comparable captured or current fingerprint evidence.
219    DependencyFingerprintEvidenceMissing {
220        /// Activity owning the input.
221        activity_id: ActivityId,
222        /// Direct activity input.
223        input_representation_id: RepresentationId,
224        /// Dependency representation lacking evidence.
225        representation_id: RepresentationId,
226        /// Captured typed path to the dependency.
227        path: Vec<ArtifactDependencyPathSegment>,
228        /// Algorithm domain, when either side supplied one.
229        algorithm: Option<String>,
230        /// Algorithm version, when either side supplied one.
231        version: Option<u16>,
232        /// Captured value, when present.
233        snapshot_value: Option<Vec<u8>>,
234        /// Current value, when present.
235        current_value: Option<Vec<u8>>,
236    },
237    /// The edge has no comparable fingerprint evidence.
238    FingerprintEvidenceMissing {
239        /// Activity owning the edge.
240        activity_id: ActivityId,
241        /// Representation referenced by the edge.
242        representation_id: RepresentationId,
243        /// Whether this was an input or output edge.
244        edge: ArtifactEdgeKind,
245        /// Algorithm domain, when one side of the comparison had it.
246        algorithm: Option<String>,
247        /// Algorithm version, when one side of the comparison had it.
248        version: Option<u16>,
249        /// Snapshot value, when present.
250        snapshot_value: Option<Vec<u8>>,
251        /// Current value, when present.
252        current_value: Option<Vec<u8>>,
253    },
254    /// Current content differs from the edge snapshot in one fingerprint domain.
255    FingerprintChanged {
256        /// Activity owning the edge.
257        activity_id: ActivityId,
258        /// Representation referenced by the edge.
259        representation_id: RepresentationId,
260        /// Whether this was an input or output edge.
261        edge: ArtifactEdgeKind,
262        /// Fingerprint algorithm.
263        algorithm: String,
264        /// Fingerprint algorithm version.
265        version: u16,
266        /// Value captured on the activity edge.
267        snapshot_value: Vec<u8>,
268        /// Current representation fingerprint value.
269        current_value: Vec<u8>,
270    },
271    /// A resource observation changed and its representation fingerprint has
272    /// not yet been recomputed.
273    FingerprintRecomputationPending {
274        /// Activity owning the edge.
275        activity_id: ActivityId,
276        /// Representation whose aggregate fingerprint is dirty.
277        representation_id: RepresentationId,
278        /// Whether this was an input or output edge.
279        edge: ArtifactEdgeKind,
280    },
281    /// An upstream produced representation is not current.
282    UpstreamNotCurrent {
283        /// Immediate input that depends on the upstream result.
284        representation_id: RepresentationId,
285        /// State computed for that upstream representation.
286        state: ArtifactKnowledgeState,
287    },
288    /// Evaluation stopped at an explicit resource bound.
289    TraversalTruncated {
290        /// Bound that was reached.
291        limit: ArtifactTraversalLimitKind,
292        /// Representation at which traversal stopped.
293        representation_id: RepresentationId,
294    },
295}
296
297/// Explicit bounds for transitive artifact evaluation.
298#[derive(Clone, Copy, Debug, Eq, PartialEq)]
299pub struct ArtifactEvaluationLimits {
300    max_depth: u32,
301    max_representations: u32,
302}
303
304impl ArtifactEvaluationLimits {
305    /// Conservative default suitable for interactive inspection.
306    pub const DEFAULT: Self = Self {
307        max_depth: 64,
308        max_representations: 1_000,
309    };
310
311    /// Creates positive traversal bounds.
312    ///
313    /// # Errors
314    ///
315    /// Returns an invalid-argument error if either bound is zero or if the
316    /// representation bound exceeds 100,000.
317    pub fn new(max_depth: u32, max_representations: u32) -> crate::Result<Self> {
318        if max_depth == 0 || max_representations == 0 || max_representations > 100_000 {
319            return Err(crate::Error::new(
320                crate::ErrorKind::InvalidArgument,
321                "artifact evaluation bounds must be positive and visit at most 100000 representations",
322            ));
323        }
324        Ok(Self {
325            max_depth,
326            max_representations,
327        })
328    }
329
330    /// Maximum number of upstream activity edges followed from the target.
331    #[must_use]
332    pub const fn max_depth(self) -> u32 {
333        self.max_depth
334    }
335
336    /// Maximum number of distinct representations inspected.
337    #[must_use]
338    pub const fn max_representations(self) -> u32 {
339        self.max_representations
340    }
341}
342
343impl Default for ArtifactEvaluationLimits {
344    fn default() -> Self {
345        Self::DEFAULT
346    }
347}
348
349/// Complete, explainable result of evaluating one produced representation.
350#[derive(Clone, Debug, Eq, PartialEq)]
351pub struct ArtifactEvaluation {
352    representation_id: RepresentationId,
353    state: ArtifactKnowledgeState,
354    reasons: Vec<ArtifactKnowledgeReason>,
355    visited_representations: u32,
356    truncated: bool,
357}
358
359impl ArtifactEvaluation {
360    /// Creates a backend-computed evaluation result.
361    #[must_use]
362    pub fn new(
363        representation_id: RepresentationId,
364        state: ArtifactKnowledgeState,
365        reasons: Vec<ArtifactKnowledgeReason>,
366        visited_representations: u32,
367        truncated: bool,
368    ) -> Self {
369        Self {
370            representation_id,
371            state,
372            reasons,
373            visited_representations,
374            truncated,
375        }
376    }
377
378    /// Returns the representation that was evaluated.
379    #[must_use]
380    pub const fn representation_id(&self) -> RepresentationId {
381        self.representation_id
382    }
383
384    /// Returns the derived knowledge state.
385    #[must_use]
386    pub const fn state(&self) -> ArtifactKnowledgeState {
387        self.state
388    }
389
390    /// Returns stable, user-explainable reasons for a non-current result.
391    #[must_use]
392    pub fn reasons(&self) -> &[ArtifactKnowledgeReason] {
393        &self.reasons
394    }
395
396    /// Returns how many distinct representations were inspected.
397    #[must_use]
398    pub const fn visited_representations(&self) -> u32 {
399        self.visited_representations
400    }
401
402    /// Returns whether an explicit traversal bound stopped evaluation.
403    #[must_use]
404    pub const fn is_truncated(&self) -> bool {
405        self.truncated
406    }
407}
408
409/// One missing condition that prevents an artifact from being reproducible.
410#[derive(Clone, Debug, Eq, PartialEq)]
411#[non_exhaustive]
412pub enum ArtifactReproducibilityIssue {
413    /// No activity records how the representation was produced.
414    ProducingActivityMissing,
415    /// More than one activity claims to have produced the representation.
416    ProducingActivityAmbiguous {
417        /// Number of producing activities found.
418        activity_count: u32,
419    },
420    /// The producing activity does not identify the tool that performed it.
421    ToolIdentityMissing {
422        /// Producing activity missing the condition.
423        activity_id: ActivityId,
424    },
425    /// The producing activity has no recorded parameter metadata.
426    ParametersMissing {
427        /// Producing activity missing the condition.
428        activity_id: ActivityId,
429    },
430    /// An input representation referenced by the activity is absent.
431    InputRepresentationMissing {
432        /// Producing activity referencing the input.
433        activity_id: ActivityId,
434        /// Missing input representation.
435        representation_id: RepresentationId,
436    },
437}
438
439/// Structured answer to whether production knowledge can reproduce an artifact.
440#[derive(Clone, Debug, Eq, PartialEq)]
441pub struct ArtifactReproducibilityReport {
442    representation_id: RepresentationId,
443    producing_activity_id: Option<ActivityId>,
444    activity_kind: Option<ActivityKind>,
445    issues: Vec<ArtifactReproducibilityIssue>,
446}
447
448impl ArtifactReproducibilityReport {
449    /// Creates a backend-computed reproducibility report.
450    #[must_use]
451    pub fn new(
452        representation_id: RepresentationId,
453        producing_activity_id: Option<ActivityId>,
454        activity_kind: Option<ActivityKind>,
455        issues: Vec<ArtifactReproducibilityIssue>,
456    ) -> Self {
457        Self {
458            representation_id,
459            producing_activity_id,
460            activity_kind,
461            issues,
462        }
463    }
464
465    /// Returns the artifact representation being described.
466    #[must_use]
467    pub const fn representation_id(&self) -> RepresentationId {
468        self.representation_id
469    }
470
471    /// Returns the single producing activity, when one exists unambiguously.
472    #[must_use]
473    pub const fn producing_activity_id(&self) -> Option<ActivityId> {
474        self.producing_activity_id
475    }
476
477    /// Returns the recorded open-world activity kind, when unambiguous.
478    #[must_use]
479    pub const fn activity_kind(&self) -> Option<&ActivityKind> {
480        self.activity_kind.as_ref()
481    }
482
483    /// Returns every missing reproducibility condition.
484    #[must_use]
485    pub fn issues(&self) -> &[ArtifactReproducibilityIssue] {
486        &self.issues
487    }
488
489    /// Returns whether all reproducibility conditions are recorded.
490    #[must_use]
491    pub fn is_reproducible(&self) -> bool {
492        self.issues.is_empty()
493    }
494}
495
496#[cfg(test)]
497mod tests {
498    use super::*;
499
500    #[test]
501    fn evaluation_limits_are_explicit_and_bounded() {
502        assert!(ArtifactEvaluationLimits::new(0, 1).is_err());
503        assert!(ArtifactEvaluationLimits::new(1, 0).is_err());
504        assert!(ArtifactEvaluationLimits::new(1, 100_001).is_err());
505        let limits = ArtifactEvaluationLimits::new(50, 10_000).expect("valid limits");
506        assert_eq!(limits.max_depth(), 50);
507        assert_eq!(limits.max_representations(), 10_000);
508    }
509}