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
//! Cache speed must not turn external SQL changes into trusted recordings.
use pollardai::*;
use std::{
    cell::Cell,
    path::PathBuf,
    rc::Rc,
    sync::atomic::{AtomicU64, Ordering},
};

static NEXT: AtomicU64 = AtomicU64::new(0);
struct Database(PathBuf);
impl Database {
    fn new() -> Self {
        Self(std::env::temp_dir().join(format!(
            "pollard-cache-{}-{}.db",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        )))
    }
    fn sql(&self, sql: &str, id: &str) {
        rusqlite::Connection::open(&self.0)
            .unwrap()
            .execute(sql, [id])
            .unwrap();
    }
}
impl Drop for Database {
    fn drop(&mut self) {
        for suffix in ["", "-wal", "-shm"] {
            let _ = std::fs::remove_file(format!("{}{suffix}", self.0.display()));
        }
    }
}
fn payload(index: usize) -> Value {
    json!({"index":index})
}
fn setup(db: &Database, n: usize) -> (String, Vec<String>) {
    let runtime = Runtime::new(SQLiteStore::open(&db.0).unwrap(), ReplayMode::Record);
    let mut run = runtime.run("cache", None, 0).unwrap();
    let ids = (0..n)
        .map(|i| {
            run.model_call(payload(i), CallOptions::default(), |_| {
                Ok(json!({"ok":true}))
            })
            .unwrap()
            .id
        })
        .collect();
    (run.root_id().to_owned(), ids)
}
type Hook = Rc<dyn Fn(&str)>;
struct ObservedStore {
    store: SQLiteStore,
    reads: Rc<Cell<usize>>,
    after_get: Option<Hook>,
    after_patch: Option<Hook>,
    cache_available: Rc<Cell<bool>>,
    snapshots_available: bool,
    ancestry_snapshots: Rc<Cell<usize>>,
    subtree_snapshots: Rc<Cell<usize>>,
    after_subtree_snapshot: Option<Hook>,
}
impl ObservedStore {
    fn new(db: &Database) -> Self {
        Self {
            store: SQLiteStore::open(&db.0).unwrap(),
            reads: Rc::new(Cell::new(0)),
            after_get: None,
            after_patch: None,
            cache_available: Rc::new(Cell::new(true)),
            snapshots_available: true,
            ancestry_snapshots: Rc::new(Cell::new(0)),
            subtree_snapshots: Rc::new(Cell::new(0)),
            after_subtree_snapshot: None,
        }
    }
}
impl Store for ObservedStore {
    fn get(&self, id: &str) -> Result<Node> {
        self.reads.set(self.reads.get() + 1);
        let node = self.store.get(id)?;
        if let Some(hook) = &self.after_get {
            hook(id);
        }
        Ok(node)
    }
    fn put(&mut self, node: Node) -> Result<()> {
        self.store.put(node)
    }
    fn exists(&self, id: &str) -> bool {
        self.store.exists(id)
    }
    fn try_exists(&self, id: &str) -> Result<bool> {
        self.store.try_exists(id)
    }
    fn roots(&self) -> Result<Vec<String>> {
        self.store.roots()
    }
    fn children(&self, id: &str) -> Result<Vec<String>> {
        self.store.children(id)
    }
    fn update_meta(&mut self, id: &str, patch: Value) -> Result<()> {
        self.store.update_meta(id, patch)?;
        if let Some(hook) = &self.after_patch {
            hook(id);
        }
        Ok(())
    }
}
impl RecordingStore for ObservedStore {
    fn ancestry_snapshot(&self, id: &str) -> Result<Option<Vec<Node>>> {
        if !self.snapshots_available {
            return Ok(None);
        }
        self.ancestry_snapshots
            .set(self.ancestry_snapshots.get() + 1);
        self.store.ancestry_snapshot(id)
    }
    fn subtree_snapshot(&self, root: &str) -> Result<Option<Vec<Node>>> {
        if !self.snapshots_available {
            return Ok(None);
        }
        self.subtree_snapshots.set(self.subtree_snapshots.get() + 1);
        let snapshot = self.store.subtree_snapshot(root)?;
        if let Some(hook) = &self.after_subtree_snapshot {
            hook(root);
        }
        Ok(snapshot)
    }
    fn finalize(&mut self, node: Node) -> Result<()> {
        self.store.finalize(node)
    }
    fn cache_revision(&self) -> Option<StoreRevision> {
        self.cache_available
            .get()
            .then(|| self.store.cache_revision())
            .flatten()
    }
}

#[test]
fn hybrid_and_read_only_replay_extend_verified_ancestry_with_linear_reads() {
    let db = Database::new();
    setup(&db, 80);
    for mode in [ReplayMode::Hybrid, ReplayMode::Replay] {
        let mut store = ObservedStore::new(&db);
        if mode == ReplayMode::Replay {
            store.store = SQLiteStore::open_read_only(&db.0).unwrap();
        }
        let reads = store.reads.clone();
        let runtime = Runtime::new(store, mode);
        let mut run = runtime.run("cache", None, 0).unwrap();
        for i in 0..80 {
            run.model_call(payload(i), CallOptions::default(), |_| {
                panic!("replay dispatched")
            })
            .unwrap();
        }
        assert!(
            reads.get() < 80 * 7,
            "{mode:?} repeated ancestor reads: {}",
            reads.get()
        );
        assert_eq!(run.report().unwrap().spent["steps"], 80.0);
        let after_report = reads.get();
        assert_eq!(run.report().unwrap().spent["steps"], 80.0);
        assert_eq!(
            reads.get(),
            after_report,
            "unchanged accounting rescanned the tree"
        );
    }
}

#[test]
fn external_ancestor_and_result_tampering_invalidate_both_replay_modes() {
    for mode in [ReplayMode::Hybrid, ReplayMode::Replay] {
        for column in ["payload", "result"] {
            let db = Database::new();
            let (root, ids) = setup(&db, 2);
            let runtime = Runtime::new(SQLiteStore::open(&db.0).unwrap(), mode);
            let mut run = runtime.run("cache", None, 0).unwrap();
            run.model_call(payload(0), CallOptions::default(), |_| panic!("dispatch"))
                .unwrap();
            if column == "payload" {
                db.sql("UPDATE nodes SET payload='{}' WHERE id=?1", &root);
            } else {
                db.sql("UPDATE nodes SET result='{}' WHERE id=?1", &ids[0]);
            }
            assert!(
                run.model_call(payload(1), CallOptions::default(), |_| panic!(
                    "tampered recording dispatched"
                ))
                .is_err(),
                "{mode:?} accepted changed {column}"
            );
        }
    }
}

#[test]
fn external_metadata_and_sibling_insertions_invalidate_accounting() {
    let db = Database::new();
    let (root, ids) = setup(&db, 2);
    let runtime = Runtime::new(SQLiteStore::open(&db.0).unwrap(), ReplayMode::Record);
    let run = runtime.run("cache", None, 0).unwrap();
    assert_eq!(run.report().unwrap().spent["steps"], 2.0);
    db.sql(
        "UPDATE nodes SET meta='{\"charges\":{\"steps\":7}}' WHERE id=?1",
        &ids[0],
    );
    assert_eq!(run.report().unwrap().spent["steps"], 8.0);
    let mut writer = SQLiteStore::open(&db.0).unwrap();
    writer
        .put(
            Node::make(
                NodeKind::ModelCall,
                Some(&root),
                9,
                json!({"sibling":true}),
                Some(json!({"ok":true})),
                json!({"charges":{"steps":3}}),
            )
            .unwrap(),
        )
        .unwrap();
    assert_eq!(run.report().unwrap().spent["steps"], 11.0);
    assert_eq!(run.report().unwrap().spent["steps"], 11.0);
}

#[test]
fn deletion_of_cached_ancestor_is_not_hidden() {
    let db = Database::new();
    let (_, ids) = setup(&db, 2);
    let runtime = Runtime::new(SQLiteStore::open(&db.0).unwrap(), ReplayMode::Hybrid);
    let mut run = runtime.run("cache", None, 0).unwrap();
    run.model_call(payload(0), CallOptions::default(), |_| panic!("dispatch"))
        .unwrap();
    db.sql("DELETE FROM nodes WHERE id=?1", &ids[0]);
    assert!(run
        .model_call(payload(1), CallOptions::default(), |_| panic!("dispatch"))
        .is_err());
}

#[test]
fn cached_interned_payload_is_rechecked_after_external_blob_mutation() {
    let db = Database::new();
    let payload = json!({"body":"long".repeat(400)});
    let recording = Runtime::new(SQLiteStore::open(&db.0).unwrap(), ReplayMode::Record);
    let mut run = recording.run("blob", None, 0).unwrap();
    run.model_call(payload.clone(), CallOptions::default(), |_| Ok(json!({})))
        .unwrap();
    run.model_call(json!({"next":true}), CallOptions::default(), |_| {
        Ok(json!({}))
    })
    .unwrap();
    let runtime = Runtime::new(SQLiteStore::open(&db.0).unwrap(), ReplayMode::Hybrid);
    let mut run = runtime.run("blob", None, 0).unwrap();
    run.model_call(payload, CallOptions::default(), |_| panic!("dispatch"))
        .unwrap();
    rusqlite::Connection::open(&db.0)
        .unwrap()
        .execute("UPDATE blobs SET value='changed'", [])
        .unwrap();
    assert!(run
        .model_call(json!({"next":true}), CallOptions::default(), |_| panic!(
            "dispatch"
        ))
        .is_err());
}

fn mutate_once(db: &Database, target_get: String, changed_node: String, sql: &'static str) -> Hook {
    let fired = Cell::new(false);
    let path = db.0.clone();
    Rc::new(move |id| {
        if id == target_get && !fired.replace(true) {
            rusqlite::Connection::open(&path)
                .unwrap()
                .execute(sql, [&changed_node])
                .unwrap();
        }
    })
}

#[test]
fn commit_during_verification_cannot_reuse_a_stale_prefix() {
    let db = Database::new();
    let (root, ids) = setup(&db, 2);
    let mut store = ObservedStore::new(&db);
    store.after_get = Some(mutate_once(
        &db,
        ids[0].clone(),
        root,
        "UPDATE nodes SET payload='{}' WHERE id=?1",
    ));
    let runtime = Runtime::new(store, ReplayMode::Hybrid);
    let mut run = runtime.run("cache", None, 0).unwrap();
    assert!(run
        .model_call(payload(0), CallOptions::default(), |_| panic!("dispatch"))
        .is_err());
}

#[test]
fn commit_during_local_patch_cannot_be_promoted_to_a_trusted_revision() {
    let db = Database::new();
    let (root, ids) = setup(&db, 2);
    let mut store = ObservedStore::new(&db);
    store.after_patch = Some(mutate_once(
        &db,
        ids[0].clone(),
        root,
        "UPDATE nodes SET payload='{}' WHERE id=?1",
    ));
    let runtime = Runtime::new(store, ReplayMode::Hybrid);
    let mut run = runtime.run("cache", None, 0).unwrap();
    run.model_call(payload(0), CallOptions::default(), |_| panic!("dispatch"))
        .unwrap();
    assert!(run
        .model_call(payload(1), CallOptions::default(), |_| panic!("dispatch"))
        .is_err());
}

#[test]
fn accounting_retries_a_scan_crossing_external_commits() {
    let db = Database::new();
    let (root, ids) = setup(&db, 2);
    let mut store = ObservedStore::new(&db);
    store.after_get = Some(mutate_once(
        &db,
        ids[0].clone(),
        root,
        "UPDATE nodes SET meta='{\"charges\":{\"steps\":5}}' WHERE id=?1",
    ));
    let runtime = Runtime::new(store, ReplayMode::Replay);
    let run = runtime.run("cache", None, 0).unwrap();
    assert_eq!(run.report().unwrap().spent["steps"], 7.0);
}

#[test]
fn triggers_disable_incremental_cache_and_uncommitted_changes_do_not() {
    let db = Database::new();
    let (root, _) = setup(&db, 1);
    let store = SQLiteStore::open(&db.0).unwrap();
    let before = store.cache_revision().unwrap();
    let connection = rusqlite::Connection::open(&db.0).unwrap();
    connection.execute_batch("BEGIN IMMEDIATE").unwrap();
    connection
        .execute("UPDATE nodes SET payload='{}' WHERE id=?1", [&root])
        .unwrap();
    assert_eq!(store.cache_revision(), Some(before));
    connection.execute_batch("ROLLBACK").unwrap();
    assert_eq!(store.cache_revision(), Some(before));
    connection
        .execute_batch(
            "CREATE TRIGGER arbitrary_change AFTER UPDATE ON nodes BEGIN UPDATE kv SET v=v; END;",
        )
        .unwrap();
    assert_eq!(store.cache_revision(), None);
    connection
        .execute_batch("DROP TRIGGER arbitrary_change")
        .unwrap();
    assert_ne!(store.cache_revision(), Some(before));
    assert!(store.cache_revision().is_some());
}

#[test]
fn temporarily_unavailable_token_discards_all_cached_accounting() {
    let db = Database::new();
    let (_, ids) = setup(&db, 1);
    let store = ObservedStore::new(&db);
    let available = store.cache_available.clone();
    let reads = store.reads.clone();
    let runtime = Runtime::new(store, ReplayMode::Replay);
    let run = runtime.run("cache", None, 0).unwrap();
    assert_eq!(run.report().unwrap().spent["steps"], 1.0);
    available.set(false);
    db.sql(
        "UPDATE nodes SET meta='{\"charges\":{\"steps\":9}}' WHERE id=?1",
        &ids[0],
    );
    assert_eq!(run.report().unwrap().spent["steps"], 9.0);
    let uncached = reads.get();
    available.set(true);
    assert_eq!(run.report().unwrap().spent["steps"], 9.0);
    assert!(
        reads.get() > uncached,
        "missing token retained an old cache"
    );
}

#[test]
fn externally_replaced_view_disables_cache_even_when_rows_are_valid() {
    let db = Database::new();
    setup(&db, 1);
    let store = SQLiteStore::open(&db.0).unwrap();
    assert!(store.cache_revision().is_some());
    rusqlite::Connection::open(&db.0).unwrap().execute_batch(
        "ALTER TABLE nodes RENAME TO stored_nodes; CREATE VIEW nodes AS SELECT * FROM stored_nodes;"
    ).unwrap();
    assert_eq!(store.cache_revision(), None);
    let runtime = Runtime::new(store, ReplayMode::Replay);
    let mut run = runtime.run("cache", None, 0).unwrap();
    run.model_call(payload(0), CallOptions::default(), |_| panic!("dispatch"))
        .unwrap();
}

#[test]
fn failed_write_invalidates_local_token_without_changing_records() {
    let db = Database::new();
    let (root, _) = setup(&db, 1);
    let mut store = SQLiteStore::open(&db.0).unwrap();
    let before = store.cache_revision().unwrap();
    let orphan = Node::make(
        NodeKind::Note,
        Some(&"f".repeat(64)),
        0,
        json!({"orphan":true}),
        None,
        json!({}),
    )
    .unwrap();
    assert!(store.put(orphan).is_err());
    let after = store.cache_revision().unwrap();
    assert_ne!(before.local, after.local);
    assert_eq!(before.external, after.external);
    assert_eq!(store.walk(&root).unwrap().len(), 2);
    assert!(verify_subtree(&store, &root).ok);
}

fn churn_on_read(db: &Database, enabled: Rc<Cell<bool>>) -> Hook {
    let path = db.0.clone();
    Rc::new(move |_| {
        if enabled.get() {
            rusqlite::Connection::open(&path)
                .unwrap()
                .execute_batch("INSERT INTO kv(k,v) VALUES('snapshot_churn','1') ON CONFLICT(k) DO UPDATE SET v=CAST(CAST(v AS INTEGER)+1 AS TEXT)")
                .unwrap();
        }
    })
}

#[test]
fn sustained_commits_fall_back_to_snapshots_without_caching_historical_data() {
    let db = Database::new();
    let (root, ids) = setup(&db, 3);
    let mut store = ObservedStore::new(&db);
    let enabled = Rc::new(Cell::new(true));
    store.after_get = Some(churn_on_read(&db, enabled.clone()));
    store.after_subtree_snapshot = Some(mutate_once(
        &db,
        root.clone(),
        ids[0].clone(),
        "UPDATE nodes SET meta='{\"charges\":{\"steps\":9}}' WHERE id=?1",
    ));
    let ancestry_snapshots = store.ancestry_snapshots.clone();
    let subtree_snapshots = store.subtree_snapshots.clone();
    let runtime = Runtime::new(store, ReplayMode::Replay);
    let mut run = runtime
        .run(
            "cache",
            Some(Budget {
                depth: Some(4),
                ..Default::default()
            }),
            0,
        )
        .unwrap();
    run.model_call(payload(0), CallOptions::default(), |_| panic!("dispatch"))
        .unwrap();
    assert!(ancestry_snapshots.get() > 0);
    assert_eq!(run.report().unwrap().spent["steps"], 3.0);
    assert_eq!(subtree_snapshots.get(), 1);
    enabled.set(false);
    assert_eq!(run.report().unwrap().spent["steps"], 11.0);
    db.sql("UPDATE nodes SET payload='{}' WHERE id=?1", &root);
    assert!(run
        .model_call(payload(1), CallOptions::default(), |_| panic!(
            "tampered dispatch"
        ))
        .is_err());
}

#[test]
fn snapshot_fallback_still_rejects_tampering_and_missing_ancestors() {
    for remove in [false, true] {
        let db = Database::new();
        let (root, _) = setup(&db, 2);
        let mut store = ObservedStore::new(&db);
        let reads = Rc::new(Cell::new(0));
        let calls = reads.clone();
        let path = db.0.clone();
        store.after_get = Some(Rc::new(move |_| {
            calls.set(calls.get() + 1);
            let connection = rusqlite::Connection::open(&path).unwrap();
            connection
                .execute_batch("INSERT INTO kv(k,v) VALUES('snapshot_churn','1') ON CONFLICT(k) DO UPDATE SET v=CAST(CAST(v AS INTEGER)+1 AS TEXT)")
                .unwrap();
            if calls.get() == 3 {
                connection
                    .execute(
                        if remove {
                            "DELETE FROM nodes WHERE id=?1"
                        } else {
                            "UPDATE nodes SET payload='{}' WHERE id=?1"
                        },
                        [&root],
                    )
                    .unwrap();
            }
        }));
        let snapshots = store.ancestry_snapshots.clone();
        let runtime = Runtime::new(store, ReplayMode::Replay);
        assert!(runtime.run("cache", None, 0).is_err());
        assert_eq!(snapshots.get(), 1);
        assert_eq!(reads.get(), 3);
    }
}

#[test]
fn backend_without_snapshots_keeps_failing_closed_under_continuous_changes() {
    let db = Database::new();
    setup(&db, 1);
    let mut store = ObservedStore::new(&db);
    store.snapshots_available = false;
    store.after_get = Some(churn_on_read(&db, Rc::new(Cell::new(true))));
    let runtime = Runtime::new(store, ReplayMode::Replay);
    assert!(matches!(
        runtime.run("cache", None, 0),
        Err(Error::Integrity(_))
    ));
}

#[test]
fn sqlite_snapshot_checks_external_ancestry_and_releases_failed_transactions() {
    let db = Database::new();
    let (root, ids) = setup(&db, 2);
    let store = SQLiteStore::open_read_only(&db.0).unwrap();
    assert_eq!(store.ancestry_snapshot(&ids[1]).unwrap().unwrap().len(), 3);
    assert_eq!(store.subtree_snapshot(&ids[0]).unwrap().unwrap().len(), 2);
    db.sql("UPDATE nodes SET payload='{}' WHERE id=?1", &root);
    assert!(store.subtree_snapshot(&ids[0]).is_err());
    assert!(store.ancestry_snapshot(&ids[1]).is_err());
    assert!(
        store.cache_revision().is_some(),
        "failed snapshot left an open transaction"
    );
}

#[test]
fn depth_budget_uses_verified_snapshot_when_concurrent_commits_prevent_caching() {
    let db = Database::new();
    setup(&db, 2);
    let mut store = ObservedStore::new(&db);
    store.after_get = Some(churn_on_read(&db, Rc::new(Cell::new(true))));
    let snapshots = store.ancestry_snapshots.clone();
    let runtime = Runtime::new(store, ReplayMode::Hybrid);
    let mut run = runtime
        .run(
            "cache",
            Some(Budget {
                depth: Some(2),
                ..Default::default()
            }),
            0,
        )
        .unwrap();
    for i in 0..2 {
        run.model_call(payload(i), CallOptions::default(), |_| {
            panic!("replay dispatched")
        })
        .unwrap();
    }
    let before = snapshots.get();
    assert!(matches!(
        run.model_call(payload(2), CallOptions::default(), |_| panic!("depth refusal dispatched")),
        Err(Error::BudgetExceeded { meter, .. }) if meter == "depth"
    ));
    assert!(snapshots.get() > before);
}