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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! The check operation and derived check state (agent-trust plan 14).
//!
//! `record_check` is the engine-recorded act of verification: it
//! appends one [`crate::check::CheckRecord`] — verdict, method note,
//! the entity's `content_hash` at check time, and plan-13 provenance
//! (actor, client, declared role) — to the workspace check ledger.
//! Checking mutates nothing: no entity write, no mem commit, no
//! `content_hash` change. That non-mutation is load-bearing — it is
//! what makes check-staleness derivable by hash comparison.
//!
//! `entity_check_state` derives never-checked | checked-ok |
//! check-failed | check-stale from the newest record against the
//! entity's current hash; the derivation lives in
//! [`crate::check::derive_state`] so surfaces and health share one
//! implementation.
use crate::check::{CheckKind, CheckLedger, CheckRecord, CheckState, Verdict, derive_state};
use crate::vcs::{Actor, ClientId};
use super::{Engine, error::EngineError};
impl Engine {
/// Record a check of one entity. Refuses typed on unknown mem
/// (quarantine included), unknown entity, read-only mounts, and
/// on any persistence failure (`CHECK_NOT_RECORDED`) — recording
/// is never best-effort, because a caller who believes an
/// unrecorded check landed is the exact dishonesty this tier
/// removes. The declared role rides engine session state
/// ([`Engine::set_role`]), same as every mutation.
///
/// `kind` selects the closed check-kind vocabulary. A
/// `conformance` record is bound to the mem's schema pin, stamped
/// HERE from the mount — never caller-supplied, so a verdict
/// cannot claim a prose version the caller never read; a mem with
/// no pin refuses (`INVALID_INPUT`), because a semantic judgment
/// against no schema binds to nothing.
#[allow(clippy::too_many_arguments)] // the record's own fields, no natural grouping
pub fn record_check(
&mut self,
mem_name: &str,
entity_id: &str,
verdict: Verdict,
kind: CheckKind,
method: Option<&str>,
actor: Actor,
client: Option<&ClientId>,
) -> Result<CheckRecord, EngineError> {
let mount_idx = self
.mounts
.iter()
.position(|m| m.mount.mem == mem_name)
.ok_or_else(|| self.unknown_mem_error(mem_name))?;
if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
return Err(EngineError::ReadOnlyMount(mem_name.to_string()));
}
let schema_ref = match kind {
CheckKind::Verification => None,
CheckKind::Conformance => Some(
self.mounts[mount_idx]
.mount
.schema
.as_ref()
.map(|s| s.as_display())
.ok_or_else(|| {
EngineError::InvalidInput(format!(
"a conformance check binds to the mem's schema pin, and mem \
`{mem_name}` declares none"
))
})?,
),
};
let entity_hash = self
.store
.all_entities()
.find(|e| e.mem == mem_name && e.id.0 == entity_id)
.map(|e| e.content_hash.clone())
.ok_or_else(|| EngineError::NotFound {
id: entity_id.to_string(),
})?;
let Some(root) = self.workspace_root() else {
return Err(EngineError::CheckNotRecorded {
reason: "engine has no workspace root — no durable check store".to_string(),
});
};
let ledger = CheckLedger::for_workspace(root);
let record = CheckRecord {
ts: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
entity: entity_id.to_string(),
verdict: verdict.as_str().to_string(),
method: method
.map(str::trim)
.filter(|m| !m.is_empty())
.map(str::to_string),
entity_hash,
actor: actor.as_trailer().to_string(),
client: client.map(|c| format!("{}@{}", c.name, c.version)),
role: self
.current_role()
.as_trailer()
.unwrap_or("unspecified")
.to_string(),
// The caller-declared identity rides engine session state
// ([`Engine::set_identity`]), same as the role — absence
// records as absence (plan 15).
identity: self.current_identity().map(str::to_string),
// Verification records omit the kind entirely, so a
// kind-omitted caller's ledger lines stay byte-identical
// to the pre-kind shape.
kind: match kind {
CheckKind::Verification => None,
CheckKind::Conformance => Some(kind.as_str().to_string()),
},
schema_ref,
};
ledger
.record(&record)
.map_err(|e| EngineError::CheckNotRecorded {
reason: format!("ledger append failed: {e}"),
})?;
Ok(record)
}
/// A [`crate::ops::health::CheckStateProvider`]-shaped closure over
/// this engine's check ledger — the `transition_requires_checks`
/// constraint's window into derived verification state. One ledger
/// handle per closure; an engine without a workspace root derives
/// every entity as `never_checked`, so a declared gate refuses
/// honestly rather than passing unverified.
pub(crate) fn check_state_provider(
&self,
) -> impl Fn(&crate::entity::Entity) -> CheckState + '_ {
let ledger = self.workspace_root().map(CheckLedger::for_workspace);
move |entity: &crate::entity::Entity| match &ledger {
None => CheckState::NeverChecked,
Some(ledger) => derive_state(
ledger
.latest_for_kind(&entity.id.0, CheckKind::Verification)
.as_ref(),
&entity.content_hash,
),
}
}
/// Derive one entity's check state and newest check record.
/// Refuses typed on unknown mem/entity; an engine with no
/// workspace root has no check store and honestly derives
/// `never_checked` (no recorded checks exist).
pub fn entity_check_state(
&self,
mem_name: &str,
entity_id: &str,
) -> Result<(CheckState, Option<CheckRecord>), EngineError> {
self.find_mount(mem_name)?;
let current_hash = self
.store
.all_entities()
.find(|e| e.mem == mem_name && e.id.0 == entity_id)
.map(|e| e.content_hash.clone())
.ok_or_else(|| EngineError::NotFound {
id: entity_id.to_string(),
})?;
let latest = self
.workspace_root()
.map(CheckLedger::for_workspace)
.and_then(|l| l.latest_for_kind(entity_id, CheckKind::Verification));
Ok((derive_state(latest.as_ref(), ¤t_hash), latest))
}
/// Derive one entity's `conformance` state and newest conformance
/// record: hash staleness plus pin staleness (a re-pinned or
/// unpinned mem stales the verdict — the prose it judged against
/// is no longer the prose in force). Same refusals as
/// [`Self::entity_check_state`].
pub fn entity_conformance_state(
&self,
mem_name: &str,
entity_id: &str,
) -> Result<(CheckState, Option<CheckRecord>), EngineError> {
let mount = self.find_mount(mem_name)?;
let current_pin = mount.mount.schema.as_ref().map(|s| s.as_display());
let current_hash = self
.store
.all_entities()
.find(|e| e.mem == mem_name && e.id.0 == entity_id)
.map(|e| e.content_hash.clone())
.ok_or_else(|| EngineError::NotFound {
id: entity_id.to_string(),
})?;
let latest = self
.workspace_root()
.map(CheckLedger::for_workspace)
.and_then(|l| l.latest_for_kind(entity_id, CheckKind::Conformance));
Ok((
crate::check::derive_state_pinned(
latest.as_ref(),
¤t_hash,
current_pin.as_deref(),
),
latest,
))
}
}
#[cfg(test)]
mod tests {
use crate::check::{CheckKind, Verdict};
use crate::vcs::Actor;
use crate::workspace::MountCapability;
/// A conformance check binds to the mem's schema pin; a mem that
/// declares none cannot accept one. Today that refusal arrives as
/// the quarantine gate (an unpinned mem quarantines at boot and
/// serves nothing), which fires before the pin guard inside
/// `record_check`; the guard's own `INVALID_INPUT` remains as
/// defense in depth for any future backend that serves unpinned.
#[test]
fn conformance_refuses_without_a_schema_pin() {
let tmp = tempfile::TempDir::new().unwrap();
std::fs::write(
tmp.path().join("anything.md"),
"---\nid: anything\ntitle: Anything\ntype: note\n---\n\nBody.\n",
)
.unwrap();
let mut mount = crate::engine::test_helpers::folder_mount("m", tmp.path().to_path_buf());
mount.schema = None;
let mut engine = crate::Engine::from_mounts(vec![(
mount,
Box::new(crate::storage::FilesystemMemWriter::new(
tmp.path().to_path_buf(),
)) as Box<dyn crate::backend::MemBackend>,
)])
.unwrap();
let err = engine
.record_check(
"m",
"m--anything",
Verdict::Ok,
CheckKind::Conformance,
None,
Actor::Cli,
None,
)
.unwrap_err();
assert_eq!(err.code(), "MEM_QUARANTINED");
}
/// Criterion 5 complement: a read-only mount refuses a check
/// typed (`READ_ONLY_MOUNT`) — capability gating runs before the
/// entity lookup, same as every mutation-shaped guard.
#[test]
fn check_refuses_read_only_mounts_typed() {
let tmp = tempfile::TempDir::new().unwrap();
let mut mount = crate::engine::test_helpers::folder_mount("ro", tmp.path().to_path_buf());
mount.capability = MountCapability::ReadOnly;
let mut engine = crate::Engine::from_mounts(vec![(
mount,
Box::new(crate::storage::FilesystemMemWriter::new(
tmp.path().to_path_buf(),
)) as Box<dyn crate::backend::MemBackend>,
)])
.unwrap();
let err = engine
.record_check(
"ro",
"ro--anything",
Verdict::Ok,
crate::check::CheckKind::Verification,
None,
Actor::Cli,
None,
)
.unwrap_err();
assert_eq!(err.code(), "READ_ONLY_MOUNT");
}
}