pollardai 0.2.1

Native Rust runtime for Pollard governed execution trees, shared budgets, and verified replay
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use crate::identity::hex64;
use crate::{
    canonical_bytes, node_id, result_digest_from_text, result_text_and_digest, Error, Result,
};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

pub type LeaseRenewer = Arc<dyn Fn(&str, f64) -> Result<bool> + Send + Sync>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NodeKind {
    Root,
    ModelCall,
    ToolCall,
    Note,
    Refusal,
}

impl NodeKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Root => "root",
            Self::ModelCall => "model_call",
            Self::ToolCall => "tool_call",
            Self::Note => "note",
            Self::Refusal => "refusal",
        }
    }
}

/// Detached owned record. Result integrity is checked against exact result text.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Node {
    pub id: String,
    pub parent: Option<String>,
    pub kind: NodeKind,
    pub attempt: u64,
    pub payload: Value,
    pub result: Option<Value>,
    pub result_text: Option<String>,
    pub result_digest: Option<String>,
    pub meta: Value,
}

impl PartialEq for Node {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.parent == other.parent
            && self.kind == other.kind
            && self.attempt == other.attempt
            && self.payload == other.payload
            && self.result_text == other.result_text
            && self.result_digest == other.result_digest
            && match (&self.result, &other.result) {
                (None, None) => true,
                (Some(left), Some(right)) => crate::identity::result_values_equal(left, right),
                _ => false,
            }
            && crate::identity::result_values_equal(&self.meta, &other.meta)
    }
}

impl Node {
    pub fn make(
        kind: NodeKind,
        parent: Option<&str>,
        attempt: u64,
        payload: Value,
        result: Option<Value>,
        meta: Value,
    ) -> Result<Self> {
        let (result_text, result_digest) = match &result {
            Some(v) => {
                let (t, d) = result_text_and_digest(v)?;
                (Some(t), Some(d))
            }
            None => (None, None),
        };
        let node = Self {
            id: node_id(kind.as_str(), parent, attempt, &payload)?,
            parent: parent.map(str::to_owned),
            kind,
            attempt,
            payload,
            result,
            result_text,
            result_digest,
            meta,
        };
        node.validate()?;
        Ok(node)
    }

    /// Import exact stored text without changing its digest surface.
    #[allow(clippy::too_many_arguments)]
    pub fn from_storage(
        id: String,
        parent: Option<String>,
        kind: NodeKind,
        attempt: u64,
        payload_text: &str,
        result_text: Option<String>,
        result_digest: Option<String>,
        meta_text: &str,
    ) -> Result<Self> {
        let parse =
            |text: &str| serde_json::from_str(text).map_err(|e| Error::Invalid(e.to_string()));
        let result = result_text.as_deref().map(parse).transpose()?;
        let node = Self {
            id,
            parent,
            kind,
            attempt,
            payload: parse(payload_text)?,
            result,
            result_text,
            result_digest,
            meta: parse(meta_text)?,
        };
        node.validate_shape()?;
        Ok(node)
    }

    pub fn expected_id(&self) -> Result<String> {
        node_id(
            self.kind.as_str(),
            self.parent.as_deref(),
            self.attempt,
            &self.payload,
        )
    }

    fn validate_shape(&self) -> Result<()> {
        if !hex64(&self.id) {
            return Err(Error::Integrity(
                "node id must be 64 lowercase hex characters".into(),
            ));
        }
        if self.kind == NodeKind::Root {
            if self.parent.is_some() {
                return Err(Error::Integrity("root nodes cannot have parents".into()));
            }
        } else if !self.parent.as_deref().is_some_and(hex64) {
            return Err(Error::Integrity(
                "non-root nodes require a 64-hex parent".into(),
            ));
        }
        if !self.payload.is_object() || !self.meta.is_object() {
            return Err(Error::Invalid(
                "payload and meta must be JSON objects".into(),
            ));
        }
        canonical_bytes(&self.payload)?;
        crate::identity::validate_finite_json(&self.meta)?;
        if self.result_digest.as_deref().is_some_and(|s| !hex64(s)) {
            return Err(Error::Integrity("invalid result digest".into()));
        }
        Ok(())
    }

    pub fn validate(&self) -> Result<()> {
        self.validate_shape()?;
        if self.id != self.expected_id()? {
            return Err(Error::Integrity(
                "node id does not match identity fields".into(),
            ));
        }
        match (&self.result, &self.result_text, &self.result_digest) {
            (None, None, None) => {}
            (Some(result), Some(text), Some(digest)) => {
                if *digest != result_digest_from_text(text) {
                    return Err(Error::Integrity(
                        "result digest does not match stored result".into(),
                    ));
                }
                let parsed: Value =
                    serde_json::from_str(text).map_err(|e| Error::Integrity(e.to_string()))?;
                if !crate::identity::result_values_equal(result, &parsed) {
                    return Err(Error::Integrity(
                        "result differs from stored result text".into(),
                    ));
                }
            }
            _ => {
                return Err(Error::Integrity(
                    "result, result_text and result_digest must be present together".into(),
                ))
            }
        }
        Ok(())
    }
}

/// A store-local cache token distinguishing local writes from independent ones.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StoreRevision {
    /// Mutations through this store instance.
    pub local: u64,
    /// All independently committed mutations, including identities and blobs.
    pub external: u64,
}

/// Optional settlement extension required by live runtimes. Finalization must
/// atomically replace only a pending, resultless node with the same identity.
pub trait RecordingStore: Store {
    fn finalize(&mut self, node: Node) -> Result<()>;
    /// Stage a live call before dispatch. Append-only backends may keep this
    /// transient and publish a single completed record during finalization.
    fn stage_pending(&mut self, node: Node) -> Result<()> {
        self.put(node)
    }
    /// Revision covering every store mutation, or None for externally writable stores.
    fn revision(&self) -> Option<u64> {
        None
    }
    /// Existing settled identity/result bytes cannot change through this backend.
    fn trusted_immutable_identity(&self) -> bool {
        false
    }
    /// A token valid only on this store instance. Returning Some promises that
    /// every committed mutation changes a component and that reads observe the
    /// current token. Local writes preserve settled identity/result bytes; other
    /// changes, including external SQL and triggers, must change `external`.
    /// A single-node write must not modify other cached nodes; such side effects
    /// require changing `external` or returning None instead.
    /// Runtimes recheck tokens after scans and discard caches on any change.
    fn cache_revision(&self) -> Option<StoreRevision> {
        if self.trusted_immutable_identity() {
            self.revision()
                .map(|local| StoreRevision { local, external: 0 })
        } else {
            None
        }
    }
    /// Return a detached, consistent read of the requested node followed by all
    /// ancestors, ending at a root. All payload blobs must share that snapshot.
    /// None means the backend cannot provide a snapshot; runtime verification
    /// then retains its fail-closed behavior under sustained concurrent changes.
    fn ancestry_snapshot(&self, _id: &str) -> Result<Option<Vec<Node>>> {
        Ok(None)
    }
    /// Return a detached, consistent read of the complete subtree rooted here,
    /// with the requested root first and each parent preceding its children.
    /// The root's external ancestry must also be verified in the same snapshot.
    /// Snapshot reads must not leave a transaction open after success or failure.
    fn subtree_snapshot(&self, _root: &str) -> Result<Option<Vec<Node>>> {
        Ok(None)
    }
    fn supports_reservations(&self) -> bool {
        false
    }
    /// None means this backend has no shared-budget arbiter.
    fn reserve_budget(
        &mut self,
        _id: &str,
        _budgets: &[crate::BudgetReservation],
        _windows: &[crate::WindowReservation],
        _lease_seconds: f64,
    ) -> Result<Option<crate::ReservationCheck>> {
        Ok(None)
    }
    fn settle_budget(&mut self, _id: &str, _charges: &BTreeMap<String, Decimal>) -> Result<()> {
        Err(Error::Invalid("store has no shared-budget arbiter".into()))
    }
    fn release_budget(&mut self, _id: &str) -> Result<()> {
        Err(Error::Invalid("store has no shared-budget arbiter".into()))
    }
    /// An independent connection-safe heartbeat, callable while a handler executes.
    fn lease_renewer(&self) -> Option<LeaseRenewer> {
        None
    }
}

/// Backends return detached owned records. Mutations occur only via methods.
pub trait Store {
    /// Snapshot compatible append-only log bytes, when the backend exposes one.
    fn operation_log(&self) -> Result<Vec<u8>> {
        Err(Error::Invalid(
            "backend does not expose an operation log".into(),
        ))
    }
    fn put(&mut self, node: Node) -> Result<()>;
    fn get(&self, id: &str) -> Result<Node>;
    fn exists(&self, id: &str) -> bool;
    /// Fallible existence query. Runtime dispatch must use this method so a
    /// remote connection failure cannot be mistaken for a missing recording.
    fn try_exists(&self, id: &str) -> Result<bool> {
        Ok(self.exists(id))
    }
    fn children(&self, id: &str) -> Result<Vec<String>>;
    fn update_meta(&mut self, id: &str, patch: Value) -> Result<()>;
    fn roots(&self) -> Result<Vec<String>>;
    /// Import collision checks and writes share one transaction on native stores.
    fn import_nodes(&mut self, nodes: Vec<Node>) -> Result<(usize, usize)> {
        crate::merge::import_prepared(self, nodes)
    }
    /// Merge destination reads and writes share one transaction on SQLite.
    fn merge_nodes(&mut self, nodes: Vec<Node>, replay: bool) -> Result<crate::MergeReport> {
        crate::merge::merge_prepared(self, nodes, replay)
    }
    /// Apply a prevalidated batch. Backends may override to provide atomicity.
    fn apply_batch(&mut self, nodes: Vec<Node>, patches: Vec<(String, Value)>) -> Result<()> {
        for node in nodes {
            self.put(node)?;
        }
        for (id, patch) in patches {
            self.update_meta(&id, patch)?;
        }
        Ok(())
    }
    fn drop_nodes(&mut self, _ids: &BTreeSet<String>) -> Result<()> {
        Err(Error::Invalid(
            "backend does not support garbage collection".into(),
        ))
    }
    fn compact(&mut self) -> Result<usize> {
        Err(Error::Invalid("backend does not support compaction".into()))
    }
    fn walk(&self, root: &str) -> Result<Vec<Node>> {
        let mut pending = vec![root.to_owned()];
        let mut seen = BTreeSet::new();
        let mut nodes = Vec::new();
        while let Some(id) = pending.pop() {
            if !seen.insert(id.clone()) {
                return Err(Error::Integrity(
                    "cycle or duplicate in store traversal".into(),
                ));
            }
            nodes.push(self.get(&id)?);
            pending.extend(self.children(&id)?.into_iter().rev());
        }
        Ok(nodes)
    }
}

#[derive(Debug, Clone, Default)]
pub struct MemoryStore {
    nodes: BTreeMap<String, Node>,
    pending: BTreeSet<String>,
    children: BTreeMap<String, BTreeSet<(String, String)>>,
    revision: u64,
}

impl MemoryStore {
    pub fn new() -> Self {
        Self::default()
    }
}

impl Store for MemoryStore {
    fn put(&mut self, node: Node) -> Result<()> {
        node.validate()?;
        if let Some(parent) = &node.parent {
            if !self.nodes.contains_key(parent) {
                return Err(Error::NotFound(parent.clone()));
            }
        }
        if let Some(existing) = self.nodes.get_mut(&node.id) {
            if existing.parent != node.parent
                || existing.kind != node.kind
                || existing.attempt != node.attempt
                || existing.payload != node.payload
            {
                return Err(Error::Integrity("node identity collision".into()));
            }
            if node.result_text.is_some() && node.result_text != existing.result_text {
                let conflicts = existing
                    .meta
                    .as_object_mut()
                    .expect("validated meta")
                    .entry("result_conflicts")
                    .or_insert_with(|| json!([]));
                conflicts
                    .as_array_mut()
                    .ok_or_else(|| Error::Integrity("invalid result_conflicts metadata".into()))?
                    .push(json!({"result_digest":node.result_digest,"result":node.result}));
                self.revision = self.revision.wrapping_add(1);
            }
            return Ok(());
        }
        if node.meta.get("state").and_then(Value::as_str) == Some("pending")
            && node.result_text.is_none()
        {
            self.pending.insert(node.id.clone());
        }
        if let Some(parent) = &node.parent {
            self.children
                .entry(parent.clone())
                .or_default()
                .insert((node.kind.as_str().to_owned(), node.id.clone()));
        }
        self.nodes.insert(node.id.clone(), node);
        self.revision = self.revision.wrapping_add(1);
        Ok(())
    }
    fn get(&self, id: &str) -> Result<Node> {
        self.nodes
            .get(id)
            .cloned()
            .ok_or_else(|| Error::NotFound(id.to_owned()))
    }
    fn exists(&self, id: &str) -> bool {
        self.nodes.contains_key(id)
    }
    fn children(&self, id: &str) -> Result<Vec<String>> {
        Ok(self
            .children
            .get(id)
            .into_iter()
            .flatten()
            .map(|(_, id)| id.clone())
            .collect())
    }
    fn update_meta(&mut self, id: &str, patch: Value) -> Result<()> {
        let patch = patch
            .as_object()
            .ok_or_else(|| Error::Invalid("meta patch must be object".into()))?;
        let node = self
            .nodes
            .get_mut(id)
            .ok_or_else(|| Error::NotFound(id.to_owned()))?;
        node.meta
            .as_object_mut()
            .expect("validated meta")
            .extend(patch.clone());
        self.revision = self.revision.wrapping_add(1);
        Ok(())
    }
    fn roots(&self) -> Result<Vec<String>> {
        let mut roots: Vec<_> = self.nodes.values().filter(|n| n.parent.is_none()).collect();
        roots.sort_by_key(|n| {
            (
                n.payload.get("run").and_then(Value::as_str).unwrap_or(""),
                n.id.as_str(),
            )
        });
        Ok(roots.into_iter().map(|n| n.id.clone()).collect())
    }
    fn apply_batch(&mut self, nodes: Vec<Node>, patches: Vec<(String, Value)>) -> Result<()> {
        let mut staged = self.clone();
        for node in nodes {
            staged.put(node)?;
        }
        for (id, patch) in patches {
            staged.update_meta(&id, patch)?;
        }
        *self = staged;
        Ok(())
    }
    fn drop_nodes(&mut self, ids: &BTreeSet<String>) -> Result<()> {
        if self
            .nodes
            .values()
            .any(|n| !ids.contains(&n.id) && n.parent.as_ref().is_some_and(|p| ids.contains(p)))
        {
            return Err(Error::Integrity(
                "garbage collection would orphan a child".into(),
            ));
        }
        self.nodes.retain(|id, _| !ids.contains(id));
        self.pending.retain(|id| !ids.contains(id));
        self.children.retain(|id, _| !ids.contains(id));
        for children in self.children.values_mut() {
            children.retain(|(_, id)| !ids.contains(id));
        }
        self.revision = self.revision.wrapping_add(1);
        Ok(())
    }
    fn compact(&mut self) -> Result<usize> {
        Ok(0)
    }
}

impl RecordingStore for MemoryStore {
    fn finalize(&mut self, node: Node) -> Result<()> {
        node.validate()?;
        let old = self
            .nodes
            .get(&node.id)
            .ok_or_else(|| Error::NotFound(node.id.clone()))?;
        if !self.pending.contains(&node.id)
            || old.meta.get("state").and_then(Value::as_str) != Some("pending")
            || old.result_text.is_some()
            || old.parent != node.parent
            || old.kind != node.kind
            || old.attempt != node.attempt
            || old.payload != node.payload
        {
            return Err(Error::Integrity(
                "only the same pending identity may be finalized".into(),
            ));
        }
        if !["completed", "failed"]
            .contains(&node.meta.get("state").and_then(Value::as_str).unwrap_or(""))
        {
            return Err(Error::Integrity(
                "finalized state must be completed or failed".into(),
            ));
        }
        self.pending.remove(&node.id);
        self.nodes.insert(node.id.clone(), node);
        self.revision = self.revision.wrapping_add(1);
        Ok(())
    }
    fn revision(&self) -> Option<u64> {
        Some(self.revision)
    }
    fn trusted_immutable_identity(&self) -> bool {
        true
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VerifyFinding {
    pub node_id: String,
    pub message: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VerifyReport {
    pub ok: bool,
    pub findings: Vec<VerifyFinding>,
}

/// Verify a node and all ancestors, including lookup keys and stored result text.
pub fn verify<S: Store + ?Sized>(store: &S, id: &str) -> VerifyReport {
    let mut findings = Vec::new();
    let mut seen = BTreeSet::new();
    let mut current = Some(id.to_owned());
    while let Some(id) = current {
        if !seen.insert(id.clone()) {
            findings.push(VerifyFinding {
                node_id: id,
                message: "cycle detected in ancestry".into(),
            });
            break;
        }
        match store.get(&id) {
            Ok(node) => {
                if node.id != id {
                    findings.push(VerifyFinding {
                        node_id: id.clone(),
                        message: "lookup key differs from node id".into(),
                    });
                }
                if let Err(e) = node.validate() {
                    findings.push(VerifyFinding {
                        node_id: id,
                        message: e.to_string(),
                    });
                }
                current = node.parent;
            }
            Err(e) => {
                findings.push(VerifyFinding {
                    node_id: id,
                    message: e.to_string(),
                });
                break;
            }
        }
    }
    VerifyReport {
        ok: findings.is_empty(),
        findings,
    }
}

/// Verify a subtree and its external ancestry, including child-index consistency.
/// The Python-compatible `verify` function intentionally checks ancestry only.
pub fn verify_subtree<S: Store + ?Sized>(store: &S, root: &str) -> VerifyReport {
    let mut report = verify(store, root);
    let mut pending = vec![(root.to_owned(), None)];
    let mut seen = BTreeSet::new();
    while let Some((id, expected_parent)) = pending.pop() {
        if !seen.insert(id.clone()) {
            report.findings.push(VerifyFinding {
                node_id: id,
                message: "cycle or duplicate in subtree".into(),
            });
            continue;
        }
        match store.get(&id) {
            Ok(node) => {
                if node.id != id
                    || expected_parent
                        .as_ref()
                        .is_some_and(|p| node.parent.as_ref() != Some(p))
                {
                    report.findings.push(VerifyFinding {
                        node_id: id.clone(),
                        message: "child index disagrees with stored identity".into(),
                    });
                }
                if id != root {
                    if let Err(e) = node.validate() {
                        report.findings.push(VerifyFinding {
                            node_id: id.clone(),
                            message: e.to_string(),
                        });
                    }
                }
                match store.children(&id) {
                    Ok(children) => pending.extend(
                        children
                            .into_iter()
                            .rev()
                            .map(|child| (child, Some(id.clone()))),
                    ),
                    Err(e) => report.findings.push(VerifyFinding {
                        node_id: id,
                        message: e.to_string(),
                    }),
                }
            }
            Err(e) => report.findings.push(VerifyFinding {
                node_id: id,
                message: e.to_string(),
            }),
        }
    }
    report.ok = report.findings.is_empty();
    report
}