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
//! Typestate tokens — the evidence lifecycle, tracked at the type level.
//!
//! Every piece of process evidence in this crate moves through a small,
//! strictly-ordered lifecycle. Each stage is an **empty enum** (uninhabited,
//! zero-cost) used only as a `PhantomData` tag inside
//! [`crate::evidence::Evidence`]. Because the stages are distinct types, an
//! illegal stage transition is not a runtime error — it simply **does not
//! compile**.
//!
//! ## The lifecycle
//!
//! ```text
//! Raw ──parse──▶ Parsed ──admit──▶ Admitted ──▶ {Projected | Exportable | Receipted}
//! │ ▲
//! └────────────── refuse ────────────┴──▶ Refused (terminal: a named law was broken)
//! ```
//!
//! - You may *construct* [`crate::state::Raw`] evidence freely (it is untrusted input).
//! - You may only reach [`crate::state::Admitted`] through an [`crate::admission::Admit`]
//! impl — there is **no** public free conversion `Raw → Admitted`.
//! - [`crate::state::Refused`] is terminal and first-class: it carries a *specific named law*,
//! never a bare "invalid input".
//!
//! These tokens are **structure only**. They mark *where a value is* in the
//! boundary protocol; they never run discovery, conformance, or replay.
//!
//! ## The sealed [`crate::state::EvidenceState`] trait
//!
//! All lifecycle stage tokens implement the [`crate::state::EvidenceState`] sealed trait. This
//! prevents a downstream crate from inventing an arbitrary type and using it as
//! the `State` parameter of [`crate::evidence::Evidence`]. Only the seven
//! canonical stages defined here are valid lifecycle positions.
/// Marker trait carried by every canonical lifecycle stage token.
///
/// This trait is **sealed**: only the seven stage tokens defined in this module
/// implement it. A downstream crate cannot invent its own stage and pass it as
/// the `State` type parameter of [`crate::evidence::Evidence`] — the
/// missing-impl error at compile time is the law-enforcement mechanism.
///
/// Structure-only marker. It does not add methods or runtime cost; it only
/// restricts the set of valid `State` arguments.
///
/// # What this is NOT
///
/// Not a validator, not a capability, not a runtime discriminant. It is a pure
/// compile-time constraint that makes illegal stage positions unrepresentable.
/// Graduate to `wasm4pm` when the *meaning* of a stage needs to be acted upon.
/// Untrusted input as it arrives from the outside world.
///
/// `Raw` is the entry stage: bytes/values just parsed off an external format,
/// not yet judged against any [`crate::witness::Witness`]. A `Raw` value must
/// **never** be exported as if it were admitted (see
/// [`crate::diagnostic::CompatDiagnostic::RawEvidenceExportedAsAdmitted`]).
///
/// Structure-only marker. Graduate the *checking* of raw evidence to `wasm4pm`;
/// here it is merely a lifecycle position.
/// Structurally parsed, but not yet judged at the boundary.
///
/// `Parsed` evidence has a well-formed shape (the format decoder accepted it)
/// but has not been put through admission against a named authority. It is the
/// staging stage between [`Raw`] and [`Admitted`].
/// Admitted across the boundary against a named [`crate::witness::Witness`].
///
/// Reaching `Admitted` means an [`crate::admission::Admit`] impl returned
/// [`crate::admission::Admission`] rather than [`crate::admission::Refusal`].
/// Only `Admitted` evidence is eligible to be projected, exported, or receipted.
/// Terminal refusal: a specific named law was broken at the boundary.
///
/// `Refused` is not an error code — it is a *first-class outcome*. A value in
/// this stage carries the named reason it was refused (e.g.
/// `DanglingEventObjectLink`, `FlatteningLoss`), so the refusal is auditable.
/// Refused evidence cannot be silently coerced back into [`Admitted`].
/// Result of a *named, accounted* lossy projection.
///
/// `Projected` evidence was produced by a [`crate::loss::Project`] impl under an
/// explicit [`crate::loss::LossPolicy`], accompanied by a
/// [`crate::loss::LossReport`]. The projection is therefore on the record:
/// nothing was flattened in secret.
/// Cleared to leave the crate as an external/`wasm4pm` value.
///
/// `Exportable` marks evidence that has been admitted (and possibly projected)
/// and is now allowed to cross back out through an export contract. This stage
/// is the boundary's "exit visa".
/// Sealed inside a provenance-bearing receipt shape.
///
/// `Receipted` evidence has been wrapped in a receipt envelope that records its
/// provenance and the witness it answered to. It is the strongest structural
/// stage in this crate — and the natural hand-off point when graduating to a
/// `wasm4pm` engine that will verify the receipt.
// ── StateTransition markers ───────────────────────────────────────────────────
/// Zero-sized type-level marker asserting that `Raw → Parsed` is the
/// transition at hand.
///
/// Used as a const/type witness when an API must distinguish *which* boundary
/// crossing it is operating on without carrying runtime state.
///
/// Structure-only marker. Does not implement any logic; it names a transition.
;
/// Zero-sized type-level marker asserting that `Parsed → Admitted` is the
/// transition at hand (i.e. the admission gate was passed).
///
/// Structure-only marker.
;
/// Zero-sized type-level marker asserting that `Parsed → Refused` is the
/// transition at hand (i.e. the evidence was declined before full admission).
///
/// Structure-only marker.
;
/// Zero-sized type-level marker asserting that `Admitted → Projected` is the
/// transition at hand (i.e. a named lossy projection was applied).
///
/// Structure-only marker.
;
/// Zero-sized type-level marker asserting that `Admitted → Exportable` is the
/// transition at hand (i.e. the exit-visa was granted directly from admission).
///
/// Structure-only marker.
;
/// Zero-sized type-level marker asserting that `Admitted → Receipted` is the
/// transition at hand (i.e. a receipt envelope was sealed directly on admitted
/// evidence).
///
/// Structure-only marker.
;
/// Zero-sized type-level marker asserting that `Projected → Exportable` is
/// the transition at hand.
///
/// Structure-only marker.
;
/// Zero-sized type-level marker asserting that `Projected → Receipted` is
/// the transition at hand.
///
/// Structure-only marker.
;
/// Zero-sized type-level marker asserting that `Exportable → Receipted` is
/// the transition at hand (i.e. the receipt envelope was sealed on an already
/// export-cleared value).
///
/// Structure-only marker.
;
// ── Projectible ──────────────────────────────────────────────────────────────
/// Sealed marker trait: only lifecycle stages that may legally enter a named,
/// accounted projection implement this trait.
///
/// Under the one-way-door invariant, a value must be [`Admitted`] before it
/// can be projected (see [`crate::loss::Project`]). This trait makes that
/// invariant structural: only `Admitted` and — because a second projection pass
/// is representable in some pipeline shapes — `Projected` implement it.
///
/// A downstream crate cannot add its own stage here; the sealing via
/// `private::Sealed` ensures only the two stages above are valid.
///
/// ## What this is NOT
///
/// Not a runtime capability, not a method table. This is a pure compile-time
/// gate that prevents projecting evidence that was never admitted. Graduate
/// the actual projection logic to `wasm4pm`.
// ── EvidenceState impls ───────────────────────────────────────────────────────
// ── Projectible impls ─────────────────────────────────────────────────────────