car-verify 0.25.0

Formal verification for Agent IR — the novel contribution
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Transactional conflict detection for shared program state.
//!
//! Survey "Code as Agent Harness" §4.3 + §5.2.4: scaling from one agent to
//! many turns the shared state into a *harness substrate* that planners,
//! coders, testers, and humans all read and modify. The literature's
//! central gap is that synchronizing artifacts is not enough — agents must
//! agree on *assumptions*. "Each action should declare its read set, write
//! set, assumptions, version dependencies … conflicts should be detected
//! not only at the level of file diffs, but also at the level of plans,
//! retrieved evidence, and latent requirements."
//!
//! CAR is unusually well placed to close this gap: it already has a formal
//! [`car_ir::Action`] with declared effects/dependencies, a versioned
//! [`car_state::StateStore`], and snapshot/rollback. This module adds the
//! missing piece — a checker that, given a proposal plus the current state
//! versions/values, detects three conflict classes and proposes a semantic
//! resolution for each:
//!
//! - **write-write**: two actions write the same key with no ordering
//!   dependency declared between them — a last-writer-wins race.
//! - **read-write**: one action reads a key another writes, unordered — the
//!   reader may observe the pre- or post-write value nondeterministically.
//! - **stale assumption** (belief divergence): an action planned against a
//!   key at a version/value that the shared state has since moved past.

use car_ir::{dependency_edges, Action, ActionProposal, StateAssumption};
use serde::Serialize;
use serde_json::Value;
use std::collections::{HashMap, HashSet};

/// The class of a detected conflict.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ConflictKind {
    /// Two actions write the same key without a declared ordering.
    WriteWrite,
    /// One action reads a key another writes, unordered.
    ReadWrite,
    /// An action's assumption about shared state is stale (version or value
    /// moved since it planned) — belief divergence.
    StaleAssumption,
}

/// Value equality for assumption checks that treats JSON numbers
/// numerically — `1` (int) and `1.0` (float) are equal. Stock serde_json
/// compares `Number` by its internal int/float arm, so a value that
/// round-tripped through a system that floatifies integers would otherwise
/// spuriously read as belief divergence (neo review M3). Falls back to
/// structural equality for everything else (object key order is already
/// order-independent in serde_json `Map`).
fn values_equal(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Number(x), Value::Number(y)) => match (x.as_f64(), y.as_f64()) {
            (Some(fx), Some(fy)) => fx == fy,
            _ => x == y,
        },
        _ => a == b,
    }
}

/// A single detected transactional conflict, with a semantic explanation
/// and a suggested resolution (§5.2.4 calls for "semantic merge, rollback,
/// dependency-aware locking, belief-state reconciliation, conflict
/// explanation, and re-verification after merge").
#[derive(Debug, Clone, Serialize)]
pub struct TransactionConflict {
    pub kind: ConflictKind,
    /// The state key the conflict is about.
    pub key: String,
    /// Action ids involved (writers/readers, or the assuming action).
    pub actions: Vec<String>,
    /// Why this is a conflict, in terms a human or repair agent can act on.
    pub explanation: String,
    /// Suggested resolution strategy.
    pub resolution: String,
}

/// The result of a transactional consistency check over a proposal.
#[derive(Debug, Clone, Serialize)]
pub struct TransactionReport {
    /// True when no conflicts were found.
    pub consistent: bool,
    pub conflicts: Vec<TransactionConflict>,
}

impl TransactionReport {
    pub fn conflicts_of(&self, kind: ConflictKind) -> impl Iterator<Item = &TransactionConflict> {
        self.conflicts.iter().filter(move |c| c.kind == kind)
    }
}

/// Transitive ancestors of each action in the executor's dependency graph.
/// `ancestors[i]` is every action that must complete before `i` runs.
/// Built from [`dependency_edges`] — the *same* edges [`car_ir::build_dag`]
/// sequences on — so the checker's notion of "ordered" is identical to the
/// runtime's by construction (neo review C1: a re-derivation drifts from
/// the DAG and suppresses real races / invents false ones).
fn transitive_ancestors(actions: &[Action]) -> Vec<HashSet<usize>> {
    let direct = dependency_edges(actions);
    let n = actions.len();
    let mut ancestors: Vec<HashSet<usize>> = vec![HashSet::new(); n];
    // Edges only point to lower indices (`writer_idx < i`), so a single
    // ascending pass computes the full closure: i's ancestors are its
    // direct deps plus each direct dep's already-computed ancestors.
    for i in 0..n {
        for &d in &direct[i] {
            ancestors[i].insert(d);
            let d_anc: Vec<usize> = ancestors[d].iter().copied().collect();
            ancestors[i].extend(d_anc);
        }
    }
    ancestors
}

/// Are actions `i` and `j` sequenced by a (transitive) dependency — i.e.
/// is one a DAG ancestor of the other? If so a shared-key access is an
/// ordered step, not a race.
fn ordered(i: usize, j: usize, ancestors: &[HashSet<usize>]) -> bool {
    ancestors[i].contains(&j) || ancestors[j].contains(&i)
}

/// Check a proposal for transactional conflicts against the current shared
/// state. `current_versions` maps key → version (from
/// [`car_state::StateStore::versions`]); `current_state` (optional) maps
/// key → value for value-level assumption checks. Both describe the state
/// the proposal was *not* planned against but will execute against.
pub fn check_transaction(
    proposal: &ActionProposal,
    current_versions: &HashMap<String, u64>,
    current_state: Option<&HashMap<String, Value>>,
) -> TransactionReport {
    let mut conflicts = Vec::new();
    let actions = &proposal.actions;

    // Precompute effective read/write sets once.
    let writes: Vec<HashSet<String>> = actions
        .iter()
        .map(|a| a.effective_write_set().into_iter().collect())
        .collect();
    let reads: Vec<HashSet<String>> = actions
        .iter()
        .map(|a| a.effective_read_set().into_iter().collect())
        .collect();
    let ancestors = transitive_ancestors(actions);

    // --- Intra-proposal write-write and read-write hazards ---
    for i in 0..actions.len() {
        for j in (i + 1)..actions.len() {
            if ordered(i, j, &ancestors) {
                continue; // a transitive dependency sequences these — not a race
            }
            // write-write
            for key in writes[i].intersection(&writes[j]) {
                conflicts.push(TransactionConflict {
                    kind: ConflictKind::WriteWrite,
                    key: key.clone(),
                    actions: vec![actions[i].id.clone(), actions[j].id.clone()],
                    explanation: format!(
                        "actions '{}' and '{}' both write '{}' with no ordering dependency — last-writer-wins is nondeterministic",
                        actions[i].id, actions[j].id, key
                    ),
                    resolution: "declare an ordering (add the writer's key to the other action's state_dependencies), split into distinct keys, or define a semantic merge for this key".to_string(),
                });
            }
            // read-write (both directions)
            for key in reads[i].intersection(&writes[j]) {
                conflicts.push(TransactionConflict {
                    kind: ConflictKind::ReadWrite,
                    key: key.clone(),
                    actions: vec![actions[i].id.clone(), actions[j].id.clone()],
                    explanation: format!(
                        "action '{}' reads '{}' while '{}' writes it, unordered — the read may observe the pre- or post-write value",
                        actions[i].id, key, actions[j].id
                    ),
                    resolution: format!(
                        "sequence the reader after the writer (add '{}' to '{}'.state_dependencies) if the fresh value is intended, or before it if the prior value is",
                        key, actions[i].id
                    ),
                });
            }
            for key in reads[j].intersection(&writes[i]) {
                conflicts.push(TransactionConflict {
                    kind: ConflictKind::ReadWrite,
                    key: key.clone(),
                    actions: vec![actions[j].id.clone(), actions[i].id.clone()],
                    explanation: format!(
                        "action '{}' reads '{}' while '{}' writes it, unordered — the read may observe the pre- or post-write value",
                        actions[j].id, key, actions[i].id
                    ),
                    resolution: format!(
                        "sequence the reader after the writer (add '{}' to '{}'.state_dependencies) if the fresh value is intended, or before it if the prior value is",
                        key, actions[j].id
                    ),
                });
            }
        }
    }

    // --- Stale assumptions (belief divergence vs the real shared state) ---
    for action in actions {
        for assumption in &action.assumptions {
            if let Some(conflict) =
                check_assumption(action, assumption, current_versions, current_state)
            {
                conflicts.push(conflict);
            }
        }
    }

    TransactionReport {
        consistent: conflicts.is_empty(),
        conflicts,
    }
}

/// Check one assumption against the current shared state. An assumption is
/// stale if its pinned version no longer matches the current version, or
/// its pinned value no longer matches the current value (numeric-aware).
fn check_assumption(
    action: &Action,
    assumption: &StateAssumption,
    current_versions: &HashMap<String, u64>,
    current_state: Option<&HashMap<String, Value>>,
) -> Option<TransactionConflict> {
    if let Some(read_version) = assumption.read_version {
        let current = current_versions.get(&assumption.key).copied();
        if current != Some(read_version) {
            return Some(TransactionConflict {
                kind: ConflictKind::StaleAssumption,
                key: assumption.key.clone(),
                actions: vec![action.id.clone()],
                explanation: format!(
                    "action '{}' planned against '{}' at version {} but the shared state is now at {} — the plan is based on a stale read",
                    action.id,
                    assumption.key,
                    read_version,
                    current.map(|v| v.to_string()).unwrap_or_else(|| "absent".to_string())
                ),
                resolution: "re-verify the action against current state and reconcile its belief (re-plan from the new version) before executing".to_string(),
            });
        }
    }
    if let (Some(expected), Some(state)) = (&assumption.expected_value, current_state) {
        let current = state.get(&assumption.key);
        if !current.map(|c| values_equal(c, expected)).unwrap_or(false) {
            return Some(TransactionConflict {
                kind: ConflictKind::StaleAssumption,
                key: assumption.key.clone(),
                actions: vec![action.id.clone()],
                explanation: format!(
                    "action '{}' assumes '{}' == {} but the shared state holds {} — belief divergence",
                    action.id,
                    assumption.key,
                    expected,
                    current.map(|v| v.to_string()).unwrap_or_else(|| "absent".to_string())
                ),
                resolution: "reconcile the assumption against the current value (semantic merge or re-plan); do not execute on the stale belief".to_string(),
            });
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use car_ir::{ActionType, FailureBehavior};

    fn act(id: &str, ty: ActionType) -> Action {
        Action {
            id: id.to_string(),
            action_type: ty,
            tool: None,
            parameters: HashMap::new(),
            preconditions: vec![],
            expected_effects: HashMap::new(),
            state_dependencies: vec![],
            read_set: vec![],
            write_set: vec![],
            assumptions: vec![],
            idempotent: false,
            max_retries: 3,
            failure_behavior: FailureBehavior::Abort,
            timeout_ms: None,
            metadata: HashMap::new(),
        }
    }

    fn prop(actions: Vec<Action>) -> ActionProposal {
        ActionProposal {
            id: "p".to_string(),
            source: "test".to_string(),
            actions,
            timestamp: chrono::Utc::now(),
            context: HashMap::new(),
        }
    }

    #[test]
    fn detects_write_write_race() {
        let mut a = act("a", ActionType::ToolCall);
        a.write_set = vec!["k".to_string()];
        let mut b = act("b", ActionType::ToolCall);
        b.write_set = vec!["k".to_string()];
        let r = check_transaction(&prop(vec![a, b]), &HashMap::new(), None);
        assert!(!r.consistent);
        assert_eq!(r.conflicts_of(ConflictKind::WriteWrite).count(), 1);
    }

    #[test]
    fn declared_ordering_suppresses_write_write() {
        // To suppress a race the actions must be ordered by the *executor*,
        // which sequences on expected_effects/StateWrite (a DAG signal).
        // The ordering must come through a *distinct* key: a writes x and k;
        // b depends on x and writes k -> DAG edge b<-a -> ordered -> the
        // shared write on k is sequenced, not a race.
        let mut a = act("a", ActionType::ToolCall);
        a.expected_effects =
            [("x".to_string(), Value::from(1)), ("k".to_string(), Value::from(1))].into();
        let mut b = act("b", ActionType::ToolCall);
        b.state_dependencies = vec!["x".to_string()];
        b.expected_effects = [("k".to_string(), Value::from(2))].into();
        let r = check_transaction(&prop(vec![a, b]), &HashMap::new(), None);
        assert!(r.consistent, "{:?}", r.conflicts);
    }

    #[test]
    fn write_set_ordering_is_not_a_dag_signal_so_race_stands() {
        // write_set alone does NOT create execution ordering, so declaring
        // a downstream dep on a write_set-only key does not suppress the
        // race — the checker must agree with the executor and still flag it
        // (neo review C1: no false-negative from a non-executor "ordering").
        let mut a = act("a", ActionType::ToolCall);
        a.write_set = vec!["k".to_string()];
        let mut b = act("b", ActionType::ToolCall);
        b.write_set = vec!["k".to_string()];
        b.state_dependencies = vec!["k".to_string()];
        let r = check_transaction(&prop(vec![a, b]), &HashMap::new(), None);
        assert_eq!(r.conflicts_of(ConflictKind::WriteWrite).count(), 1);
    }

    #[test]
    fn transitive_ordering_suppresses_distant_write_write() {
        // A -> C -> B chain via expected_effects/state_dependencies. A and
        // B both write k; they are transitively ordered, so it is NOT a
        // race (neo review C2: ordering must be transitive).
        let mut a = act("a", ActionType::ToolCall);
        a.expected_effects = [("x".to_string(), Value::from(1)), ("k".to_string(), Value::from(1))].into();
        let mut c = act("c", ActionType::ToolCall);
        c.state_dependencies = vec!["x".to_string()];
        c.expected_effects = [("y".to_string(), Value::from(1))].into();
        let mut b = act("b", ActionType::ToolCall);
        b.state_dependencies = vec!["y".to_string()];
        b.expected_effects = [("k".to_string(), Value::from(2))].into();
        let r = check_transaction(&prop(vec![a, c, b]), &HashMap::new(), None);
        assert!(r.consistent, "transitively ordered, not a race: {:?}", r.conflicts);
    }

    #[test]
    fn detects_read_write_hazard() {
        let mut reader = act("reader", ActionType::ToolCall);
        reader.read_set = vec!["k".to_string()];
        let mut writer = act("writer", ActionType::ToolCall);
        writer.write_set = vec!["k".to_string()];
        let r = check_transaction(&prop(vec![reader, writer]), &HashMap::new(), None);
        assert_eq!(r.conflicts_of(ConflictKind::ReadWrite).count(), 1);
    }

    #[test]
    fn detects_stale_version_assumption() {
        let mut a = act("a", ActionType::ToolCall);
        a.assumptions = vec![StateAssumption {
            key: "config".to_string(),
            expected_value: None,
            read_version: Some(3),
        }];
        // Shared state has moved to version 4.
        let versions = [("config".to_string(), 4u64)].into();
        let r = check_transaction(&prop(vec![a]), &versions, None);
        assert!(!r.consistent);
        assert_eq!(r.conflicts_of(ConflictKind::StaleAssumption).count(), 1);
    }

    #[test]
    fn matching_version_assumption_is_consistent() {
        let mut a = act("a", ActionType::ToolCall);
        a.assumptions = vec![StateAssumption {
            key: "config".to_string(),
            expected_value: None,
            read_version: Some(4),
        }];
        let versions = [("config".to_string(), 4u64)].into();
        let r = check_transaction(&prop(vec![a]), &versions, None);
        assert!(r.consistent);
    }

    #[test]
    fn detects_stale_value_assumption() {
        let mut a = act("a", ActionType::ToolCall);
        a.assumptions = vec![StateAssumption {
            key: "mode".to_string(),
            expected_value: Some(Value::from("draft")),
            read_version: None,
        }];
        let state = [("mode".to_string(), Value::from("published"))].into();
        let r = check_transaction(&prop(vec![a]), &HashMap::new(), Some(&state));
        assert_eq!(r.conflicts_of(ConflictKind::StaleAssumption).count(), 1);
    }

    #[test]
    fn numeric_assumption_int_float_equal() {
        // expected 1 (int) vs current 1.0 (float) must NOT flag stale.
        let mut a = act("a", ActionType::ToolCall);
        a.assumptions = vec![StateAssumption {
            key: "n".to_string(),
            expected_value: Some(Value::from(1)),
            read_version: None,
        }];
        let state = [("n".to_string(), serde_json::json!(1.0))].into();
        let r = check_transaction(&prop(vec![a]), &HashMap::new(), Some(&state));
        assert!(r.consistent, "1 == 1.0: {:?}", r.conflicts);
    }

    #[test]
    fn union_write_set_does_not_shadow_expected_effects() {
        // Explicit write_set=["a"] plus expected_effects on "b": both must
        // be in the effective write set, so a race on "b" is caught.
        let mut x = act("x", ActionType::ToolCall);
        x.write_set = vec!["a".to_string()];
        x.expected_effects = [("b".to_string(), Value::from(1))].into();
        let mut y = act("y", ActionType::ToolCall);
        y.write_set = vec!["b".to_string()];
        let r = check_transaction(&prop(vec![x, y]), &HashMap::new(), None);
        assert_eq!(r.conflicts_of(ConflictKind::WriteWrite).count(), 1);
    }

    #[test]
    fn derived_write_set_from_expected_effects() {
        // Two actions that don't set write_set explicitly still conflict
        // via the effective (derived) write set.
        let mut a = act("a", ActionType::ToolCall);
        a.expected_effects = [("k".to_string(), Value::from(1))].into();
        let mut b = act("b", ActionType::ToolCall);
        b.expected_effects = [("k".to_string(), Value::from(2))].into();
        let r = check_transaction(&prop(vec![a, b]), &HashMap::new(), None);
        assert_eq!(r.conflicts_of(ConflictKind::WriteWrite).count(), 1);
    }
}