Skip to main content

vela_protocol/proposals/
types.rs

1//! Proposal data types: the StateProposal record, agent-run provenance, proof
2//! state, accept/preview/validation reports. Re-exported flat from the parent.
3
4use super::*;
5
6pub const PROPOSAL_SCHEMA: &str = "vela.proposal.v0.1";
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
9pub struct StateProposal {
10    #[serde(default = "default_schema")]
11    pub schema: String,
12    pub id: String,
13    pub kind: String,
14    pub target: StateTarget,
15    pub actor: StateActor,
16    pub created_at: String,
17    /// v0.67: when an agent drafts a proposal long before the
18    /// reviewer accepts it, `drafted_at` records the draft moment.
19    /// `created_at` records the moment the proposal entered the
20    /// canonical store. The throughput dashboard reads against
21    /// `drafted_at` when present, falling back to `created_at`,
22    /// so the "median proposal-to-event latency" surfaces real
23    /// reviewer queue time rather than zero.
24    /// Backward-compatible: pre-v0.67 proposals load with `None`.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub drafted_at: Option<String>,
27    pub reason: String,
28    #[serde(default)]
29    pub payload: Value,
30    #[serde(default)]
31    pub source_refs: Vec<String>,
32    pub status: String,
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    pub reviewed_by: Option<String>,
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    pub reviewed_at: Option<String>,
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub decision_reason: Option<String>,
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub applied_event_id: Option<String>,
41    #[serde(default)]
42    pub caveats: Vec<String>,
43    /// v0.22 (Agent Inbox): when a proposal originates from a scoped
44    /// agent run (e.g. Literature Scout reading a PDF folder), this
45    /// captures the model, the run id, and the wall-clock window.
46    /// The substrate stays dumb — it does not know whether the
47    /// proposer was a human, a Claude run, a GPT run, or a lab
48    /// pipeline; this is informational provenance only, surfaced in
49    /// the Workbench Inbox so reviewers can judge what they're
50    /// looking at. Optional + skip-if-none so existing frontiers
51    /// without proposals serialize byte-identically.
52    #[serde(default, skip_serializing_if = "Option::is_none")]
53    pub agent_run: Option<AgentRun>,
54}
55
56/// Agent provenance attached to a `StateProposal`.
57///
58/// Doctrine: the substrate stays model-agnostic. Agents — Literature
59/// Scout, Notes Compiler, Code Analyst, etc. — sit in the
60/// external producer code and write proposals into
61/// a frontier through the existing protocol. This struct is the
62/// reviewer-facing record of *who proposed what, with what model,
63/// during which run* — never used as access control or trust
64/// assignment.
65#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
66pub struct AgentRun {
67    /// Stable agent name (e.g. "literature-scout"). Pairs with the
68    /// proposal's `actor.id == "agent:literature-scout"`.
69    pub agent: String,
70    /// Model identifier (e.g. "claude-sonnet-4-6"). Free-form so the
71    /// substrate never has to enumerate model names.
72    #[serde(default, skip_serializing_if = "String::is_empty")]
73    pub model: String,
74    /// Run identifier — typically a UUID or short hash. Lets the
75    /// reviewer group multiple proposals that came out of the same
76    /// agent invocation.
77    #[serde(default, skip_serializing_if = "String::is_empty")]
78    pub run_id: String,
79    /// ISO-8601 wall-clock start of the run.
80    #[serde(default, skip_serializing_if = "String::is_empty")]
81    pub started_at: String,
82    /// ISO-8601 wall-clock end. Optional because some agents stream.
83    #[serde(default, skip_serializing_if = "Option::is_none")]
84    pub finished_at: Option<String>,
85    /// Free-form context the reviewer should see — e.g. the input
86    /// folder path, the count of papers processed, the prompt
87    /// version. Kept as a flat string map so it round-trips cleanly
88    /// through canonical JSON without imposing a schema.
89    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
90    pub context: BTreeMap<String, String>,
91    /// v0.49: explicit tool-call traces from this run. Each entry
92    /// records one tool invocation by content-addressable summary
93    /// (tool name + input hash + output hash + duration). Lets a
94    /// reviewer see what the agent actually called without bloating
95    /// the bundle with raw payloads. Optional + skip-if-empty so
96    /// existing frontiers round-trip byte-identically.
97    #[serde(default, skip_serializing_if = "Vec::is_empty")]
98    pub tool_calls: Vec<ToolCallTrace>,
99    /// v0.49: declared permission state for this run. Lists the
100    /// data sources the agent had read access to and the tools it
101    /// could invoke. Reviewers compare this declaration against
102    /// `tool_calls` to spot drift. Optional + skip-if-empty so
103    /// existing frontiers round-trip byte-identically.
104    #[serde(default, skip_serializing_if = "Option::is_none")]
105    pub permissions: Option<PermissionState>,
106}
107
108/// One tool invocation made during an `AgentRun`. Stored as a
109/// content-addressable summary, never the raw payload — keeps the
110/// bundle bounded while preserving "did this happen, with what
111/// inputs, returning what outputs" for reviewer audit. v0.49.
112#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
113pub struct ToolCallTrace {
114    /// Tool identifier (e.g. "pubmed_search", "arxiv_fetch", "compile").
115    pub tool: String,
116    /// SHA-256 hex of the canonical-JSON input. 64-char.
117    #[serde(default, skip_serializing_if = "String::is_empty")]
118    pub input_sha256: String,
119    /// SHA-256 hex of the canonical-JSON output. 64-char. Optional
120    /// for tools whose output is opaque (a side effect, a navigation,
121    /// etc.).
122    #[serde(default, skip_serializing_if = "Option::is_none")]
123    pub output_sha256: Option<String>,
124    /// ISO-8601 wall-clock start of the call.
125    #[serde(default, skip_serializing_if = "String::is_empty")]
126    pub at: String,
127    /// Wall-clock duration in milliseconds.
128    #[serde(default, skip_serializing_if = "Option::is_none")]
129    pub duration_ms: Option<u32>,
130    /// Optional non-error status string (e.g. "ok", "rate_limited",
131    /// "partial"). Kept free-form so a tool layer can emit whatever
132    /// taxonomy it wants without protocol bumps.
133    #[serde(default, skip_serializing_if = "String::is_empty")]
134    pub status: String,
135    /// Optional human-readable error detail when `status` indicates a
136    /// failure. Free-form so tool layers can carry a stack frame, an
137    /// HTTP response body, or a one-line summary — whatever a
138    /// reviewer needs to audit what went wrong without re-running the
139    /// agent. Skipped when empty so successful calls round-trip
140    /// byte-identically.
141    #[serde(default, skip_serializing_if = "String::is_empty")]
142    pub error_message: String,
143}
144
145/// Declared permission boundary for an `AgentRun`. Lists what the
146/// agent could read and which tools it could call. Reviewers can
147/// diff this against `tool_calls` to spot scope creep. v0.49.
148#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
149pub struct PermissionState {
150    /// Data sources the agent had read access to. Free-form URIs:
151    /// `pubmed:`, `dataset:`, `frontier:vfr_…`, `path:./papers/…`.
152    #[serde(default, skip_serializing_if = "Vec::is_empty")]
153    pub data_access: Vec<String>,
154    /// Tool identifiers the agent was allowed to call. Should be the
155    /// allow-list `tool_calls[*].tool` is checked against by the
156    /// runtime.
157    #[serde(default, skip_serializing_if = "Vec::is_empty")]
158    pub tool_access: Vec<String>,
159    /// Optional human-readable note explaining the scope (e.g.
160    /// "read-only access to BBB Flagship; can call pubmed search
161    /// and arxiv fetch only"). Reviewer affordance only.
162    #[serde(default, skip_serializing_if = "String::is_empty")]
163    pub note: String,
164}
165
166#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
167pub struct ProposalSummary {
168    pub total: usize,
169    pub pending_review: usize,
170    pub accepted: usize,
171    pub rejected: usize,
172    pub applied: usize,
173    #[serde(default)]
174    pub withdrawn: usize,
175    #[serde(default)]
176    pub by_kind: BTreeMap<String, usize>,
177    #[serde(default)]
178    pub duplicate_ids: Vec<String>,
179    #[serde(default)]
180    pub invalid_targets: Vec<String>,
181}
182
183#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
184pub struct ProofState {
185    #[serde(default)]
186    pub latest_packet: ProofPacketState,
187    #[serde(default, skip_serializing_if = "Option::is_none")]
188    pub last_event_at_export: Option<String>,
189    #[serde(default, skip_serializing_if = "Option::is_none")]
190    pub stale_reason: Option<String>,
191}
192
193#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
194pub struct ProofPacketState {
195    pub generated_at: Option<String>,
196    pub snapshot_hash: Option<String>,
197    pub event_log_hash: Option<String>,
198    /// Event-set commitment with only `attempt.claimed` coordination leases
199    /// removed. Optional so historical proof-state records remain readable.
200    #[serde(default, skip_serializing_if = "Option::is_none")]
201    pub nonlease_event_log_hash: Option<String>,
202    pub packet_manifest_hash: Option<String>,
203    pub status: String,
204}
205
206impl Default for ProofPacketState {
207    fn default() -> Self {
208        Self {
209            generated_at: None,
210            snapshot_hash: None,
211            event_log_hash: None,
212            nonlease_event_log_hash: None,
213            packet_manifest_hash: None,
214            status: "never_exported".to_string(),
215        }
216    }
217}
218
219#[derive(Debug, Clone)]
220pub struct CreateProposalResult {
221    pub proposal_id: String,
222    pub finding_id: String,
223    pub status: String,
224    pub applied_event_id: Option<String>,
225}
226
227#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
228pub struct ProposalValidationReport {
229    pub ok: bool,
230    pub checked: usize,
231    pub valid: usize,
232    pub invalid: usize,
233    #[serde(default)]
234    pub errors: Vec<String>,
235    #[serde(default)]
236    pub proposal_ids: Vec<String>,
237}
238
239#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
240pub struct ProposalPreview {
241    pub proposal_id: String,
242    pub kind: String,
243    pub target: StateTarget,
244    pub reviewer: String,
245    #[serde(default)]
246    pub changed_findings: Vec<String>,
247    /// Field-level before/after for each changed finding (assertion text, type,
248    /// and confidence), so a reviewer reads what the change asserts, not only a
249    /// count delta. Confidence is a formatted string to keep this struct `Eq`.
250    #[serde(default)]
251    pub changed_finding_details: Vec<ChangedFindingDetail>,
252    #[serde(default)]
253    pub changed_artifacts: Vec<String>,
254    #[serde(default)]
255    pub new_event_ids: Vec<String>,
256    #[serde(default)]
257    pub event_kinds: Vec<String>,
258    pub findings_before: usize,
259    pub findings_after: usize,
260    pub findings_delta: isize,
261    pub artifacts_before: usize,
262    pub artifacts_after: usize,
263    pub artifacts_delta: isize,
264    pub events_before: usize,
265    pub events_after: usize,
266    pub events_delta: isize,
267    pub proof_would_be_stale: bool,
268    pub applied_event_id: String,
269}
270
271/// Field-level before/after for one changed finding. All fields are optional:
272/// a `finding.add` has no `before`, a retract no `after`. Confidence is a
273/// formatted string so the containing `ProposalPreview` can stay `Eq`.
274#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
275pub struct ChangedFindingDetail {
276    pub id: String,
277    #[serde(default, skip_serializing_if = "Option::is_none")]
278    pub assertion_before: Option<String>,
279    #[serde(default, skip_serializing_if = "Option::is_none")]
280    pub assertion_after: Option<String>,
281    #[serde(default, skip_serializing_if = "Option::is_none")]
282    pub assertion_type_before: Option<String>,
283    #[serde(default, skip_serializing_if = "Option::is_none")]
284    pub assertion_type_after: Option<String>,
285    #[serde(default, skip_serializing_if = "Option::is_none")]
286    pub confidence_before: Option<String>,
287    #[serde(default, skip_serializing_if = "Option::is_none")]
288    pub confidence_after: Option<String>,
289}
290
291/// Build the field-level detail for each changed finding by looking it up in the
292/// before and after states. Both lookups can miss (add/retract), so each side is
293/// optional.
294
295#[derive(Debug, Clone)]
296pub struct ProofPacketRecord {
297    pub generated_at: String,
298    pub snapshot_hash: String,
299    pub event_log_hash: String,
300    pub packet_manifest_hash: String,
301}
302
303fn default_schema() -> String {
304    PROPOSAL_SCHEMA.to_string()
305}