areev_loop/substrate.rs
1//! The `OmsSubstrate` trait — the engine's only contact with a store. It is
2//! defined in terms of the OMS Level-2 protocol (CAL text ↔ JSON rows, grain
3//! get/put/supersede) plus curated typed reads the built-in analyzers use.
4//! Areev is the first substrate; the in-repo `ReferenceSubstrate` lets engine
5//! CI run with zero Areev, and doubles as the third-party conformance kit.
6
7use crate::error::Result;
8use crate::model::GrainRecord;
9use serde_json::{Map, Value};
10
11/// Optional substrate capabilities, declared once and matched against each
12/// analyzer manifest's `requires` list. A missing capability degrades an
13/// analyzer to an activation-ladder entry, never a silent no-op (§8).
14#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
15pub struct Capabilities {
16 /// Multiple concurrent heads per entity are tracked and queryable
17 /// (fork surfacing needs this).
18 pub forks: bool,
19 /// A telemetry sidecar records recall/access history.
20 pub telemetry: bool,
21 /// An embedder is installed (upgrades T0 analyzers to T1).
22 pub embeddings: bool,
23 /// Workflow plan grains can be structurally validated
24 /// ([`SubstrateRead::validate_plan`]). Without it a `plan_revision`
25 /// proposal stays advisory: the engine will not stamp an executable edit
26 /// to a plan it cannot have checked for reachability and cycle bounds.
27 pub plans: bool,
28 /// Executable tool code is modelled — the §7.4 blob seam plus
29 /// [`SubstrateRead::tool_evalset`]. Without it a `code_revision` proposal
30 /// stays advisory, because Rule E1's pin cannot be resolved.
31 pub code: bool,
32}
33
34/// Read filters for curated grain reads.
35#[derive(Debug, Clone, Copy)]
36pub struct ReadOpts {
37 /// When true (default), only live (non-superseded) grains are returned.
38 pub live_only: bool,
39 /// When set, only grains created at or after this epoch-ms are returned
40 /// (the incremental watermark scan, §8).
41 pub since_ms: Option<i64>,
42}
43
44impl Default for ReadOpts {
45 fn default() -> Self {
46 ReadOpts {
47 live_only: true,
48 since_ms: None,
49 }
50 }
51}
52
53/// A grain to be written by an apply. `derived_from` and other provenance go
54/// in `fields`; the substrate computes the content address.
55#[derive(Debug, Clone, PartialEq)]
56pub struct GrainSpec {
57 pub grain_type: String,
58 pub namespace: String,
59 pub fields: Map<String, Value>,
60}
61
62impl GrainSpec {
63 pub fn new(grain_type: impl Into<String>, namespace: impl Into<String>) -> Self {
64 GrainSpec {
65 grain_type: grain_type.into(),
66 namespace: namespace.into(),
67 fields: Map::new(),
68 }
69 }
70
71 pub fn with_field(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
72 self.fields.insert(key.into(), value.into());
73 self
74 }
75}
76
77/// One entity holding more than one live head (fork surfacing input).
78#[derive(Debug, Clone, PartialEq)]
79pub struct HeadGroup {
80 /// Entity identity, e.g. `"caller/john"` (namespace-qualified subject).
81 pub entity: String,
82 /// The competing head hashes.
83 pub heads: Vec<String>,
84}
85
86/// A snapshot of the recall-telemetry sidecar's rollups (§8). Telemetry-fed
87/// analyzers (`cold_grains`, `coverage_gap`, `budget_pressure`) read this via
88/// [`crate::analyzer::AnalyzeCtx::telemetry`]. It is an owned snapshot, not a
89/// live handle — analyzers stay read-only and can't reach the sidecar. A
90/// substrate without a sidecar returns `None`, so the analyzer degrades to an
91/// activation-ladder entry rather than firing on absent evidence.
92#[derive(Debug, Clone, Default, PartialEq)]
93pub struct TelemetryView {
94 /// Per-grain recall rollups. A grain **absent** here has never been
95 /// recalled (the `cold_grains` signal).
96 pub access: Vec<GrainAccess>,
97 /// Per-question rollups over free-text recalls (the `coverage_gap` signal).
98 pub queries: Vec<QueryUsage>,
99 /// Assembly-budget pressure rollup (the `budget_pressure` signal).
100 pub budget: BudgetUsage,
101}
102
103/// How often one grain has been surfaced by recall.
104#[derive(Debug, Clone, PartialEq)]
105pub struct GrainAccess {
106 pub hash: String,
107 pub recall_count: i64,
108 pub last_ms: i64,
109}
110
111/// How a recurring recall question has fared.
112#[derive(Debug, Clone, PartialEq)]
113pub struct QueryUsage {
114 /// A short human-readable sample of the query intent.
115 pub sample: String,
116 pub run_count: i64,
117 /// How many of those runs returned nothing — the coverage-gap signal.
118 pub empty_count: i64,
119 pub sum_results: i64,
120 pub last_ms: i64,
121}
122
123/// Assembly-budget pressure rollup.
124#[derive(Debug, Clone, Default, PartialEq)]
125pub struct BudgetUsage {
126 pub sample_count: i64,
127 pub overflow_count: i64,
128}
129
130/// The read-only slice of the substrate. Analyzers receive this (via
131/// `AnalyzeCtx`) and nothing else — the trust floor's "analyzers execute
132/// read-only" is enforced by the type system: a `&dyn SubstrateRead` cannot
133/// reach any mutating method. It is object-safe (no generics) so
134/// `builtin_analyzers()` can hand out `Box<dyn Analyzer>`.
135pub trait SubstrateRead {
136 /// Declared optional capabilities.
137 fn capabilities(&self) -> Capabilities;
138
139 /// Curated read: all grains of one OMS type, optionally namespace-scoped
140 /// and watermark/liveness filtered.
141 fn grains_of_type(
142 &self,
143 grain_type: &str,
144 namespace: Option<&str>,
145 opts: ReadOpts,
146 ) -> Result<Vec<GrainRecord>>;
147
148 /// Fetch one grain by content address.
149 fn grain(&self, hash: &str) -> Result<Option<GrainRecord>>;
150
151 /// Entities with more than one live head. Requires the `forks` capability;
152 /// the default impl reports it missing so non-fork substrates degrade
153 /// cleanly rather than pretend.
154 fn heads(&self, _namespace: Option<&str>) -> Result<Vec<HeadGroup>> {
155 Err(crate::error::Error::CapabilityMissing("forks".into()))
156 }
157
158 /// A snapshot of the recall-telemetry rollups (§8). Requires the
159 /// `telemetry` capability; the default returns `None` so substrates without
160 /// a sidecar degrade cleanly. `namespace` scopes the snapshot when set.
161 fn telemetry(&self, _namespace: Option<&str>) -> Result<Option<TelemetryView>> {
162 Ok(None)
163 }
164
165 /// Structurally validate a candidate Workflow grain body — the same
166 /// checks the runtime would run before executing it (unique and reachable
167 /// nodes, conditions parse, every cycle bounded). The engine calls this
168 /// before it will stamp a `plan_revision` as executable, so a proposal
169 /// that would produce an unrunnable plan never reaches a reviewer as
170 /// something they could apply.
171 ///
172 /// This is deliberately the substrate's job: the engine owns no plan
173 /// grammar, exactly as it owns no CAL grammar (see [`OmsSubstrate::validate_cal`]).
174 /// Requires the `plans` capability; the default reports it missing so a
175 /// substrate that does not model workflows degrades the proposal to
176 /// advisory rather than pretending to have checked it.
177 fn validate_plan(&self, _workflow: &Value) -> Result<()> {
178 Err(crate::error::Error::CapabilityMissing("plans".into()))
179 }
180
181 /// The evalset a code revision of `tool` must be gated against (Rule E1's
182 /// pin). Returns `Ok(None)` when the tool declares none, which makes a
183 /// `code_revision` for it advisory — an unpinnable revision is one no
184 /// gating run could ever satisfy.
185 ///
186 /// Resolved from the substrate and never from the model: a proposer that
187 /// could name its own grader is not gated.
188 fn tool_evalset(&self, _tool: &str) -> Result<Option<String>> {
189 Ok(None)
190 }
191}
192
193/// The full store protocol the engine binds to: reads (via the supertrait)
194/// plus governed writes, CAL, and state persistence. All methods are fallible;
195/// a substrate fault surfaces as [`crate::error::Error::Substrate`].
196pub trait OmsSubstrate: SubstrateRead {
197 /// Append a new grain; returns its content address.
198 fn put_grain(&mut self, spec: &GrainSpec) -> Result<String>;
199
200 /// Supersede `target_hash` with a new grain carrying `justification`;
201 /// returns the new grain's address. Atomic and distinct from put
202 /// (OMS §28.4).
203 fn supersede(
204 &mut self,
205 target_hash: &str,
206 spec: &GrainSpec,
207 justification: &str,
208 ) -> Result<String>;
209
210 /// Index-layer retraction (`verification_status = retracted`) — the
211 /// inverse of an applied ADD, used by rollback. Not destructive (the grain
212 /// stays content-addressed; only the index marks it retracted). The
213 /// default reports it unsupported so substrates opt in.
214 fn retract(&mut self, hash: &str, reason: &str) -> Result<()> {
215 Err(crate::error::Error::Substrate(format!(
216 "retract not supported by this substrate ({hash}: {reason})"
217 )))
218 }
219
220 /// Store an opaque blob (candidate tool CODE, evalset payloads) in the
221 /// substrate's CAS, returning its address. §7.4's blob seam —
222 /// CAPABILITY-GATED: the default refuses, so a loop can only carry code
223 /// on substrates that explicitly opt in. Code enters the substrate only
224 /// through this seam or an authored add — never from a git mirror.
225 fn put_blob(&mut self, bytes: &[u8]) -> Result<String> {
226 let _ = bytes;
227 Err(crate::error::Error::Substrate(
228 "put_blob not supported by this substrate (code-carrying loops \
229 need an opted-in blob seam)"
230 .into(),
231 ))
232 }
233
234 /// Fetch a blob by the address `put_blob` returned. Same capability gate.
235 fn get_blob(&mut self, address: &str) -> Result<Vec<u8>> {
236 Err(crate::error::Error::Substrate(format!(
237 "get_blob not supported by this substrate ({address})"
238 )))
239 }
240
241 /// Execute CAL text, returning result rows as JSON. Used to regenerate
242 /// evidence sets (`evidence_query`) and to apply `proposal_cal`. A
243 /// substrate MAY reject CAL it cannot run with [`Error::CalUnsupported`].
244 ///
245 /// [`Error::CalUnsupported`]: crate::error::Error::CalUnsupported
246 fn execute_cal(&mut self, cal: &str) -> Result<Vec<Value>>;
247
248 /// The CAL that would restore the CURRENT definition named by a
249 /// `DEFINE QUERY` / `DEFINE TEMPLATE` statement — the rollback inverse,
250 /// captured before the definition is replaced.
251 ///
252 /// Returns `Ok(None)` when the substrate cannot produce one, which the
253 /// engine treats as "this definition rewrite is not applicable": a
254 /// definition change with no recorded inverse is one that `ROLLBACK`
255 /// would silently fail to undo, and a rollback that reports success
256 /// without restoring anything is worse than a refusal.
257 ///
258 /// The default implementation returns `None`, so a substrate that does
259 /// not model saved definitions simply cannot execute definition
260 /// rewrites — fail closed, no opt-out needed.
261 fn definition_inverse(&self, _statement: &str) -> Result<Option<String>> {
262 Ok(None)
263 }
264
265 /// Validate a CAL batch without executing it (statement classification,
266 /// destructive-op detection). Delegated to the substrate — the engine
267 /// contains a CAL *writer*, never a parser.
268 fn validate_cal(&self, cal: &str) -> Result<()>;
269
270 /// Load the persisted loop state blob (config + watermarks/cooldowns).
271 /// Returns `Value::Null` when nothing has been stored yet.
272 fn load_state(&self) -> Result<Value>;
273
274 /// Persist the loop state blob (a file-truth, so it travels with the
275 /// file on sync).
276 fn store_state(&mut self, state: &Value) -> Result<()>;
277}