hol-cigar 0.14.2

Open-source context engineering protocol for AI agents: governed retrieval, bounded multi-agent handoffs, MCP, provenance, recoverable effects, and 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
#![allow(clippy::unwrap_used, clippy::indexing_slicing, clippy::panic)]
use super::*;
use crate::broker::{AgentGrantSpec, AgentLimits, SourceOrigin, SourceProvenance};
use crate::{ContextRequest, ContextViewSpec, Document, Utf8ByteCounter};
use std::collections::BTreeSet;
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::{DirBuilderExt, PermissionsExt, symlink};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::Instant;
use tempfile::TempDir;

#[test]
fn bundled_sqlite_includes_database_wal_memory_disclosure_fix() {
    // RUSTSEC-2026-0459: the bundled engine must contain SQLite's DB/WAL
    // page-size mismatch fix. Keep this gate in the shipped persistence feature
    // composition, including platform builds that select a different SQLite VFS.
    assert!(
        rusqlite::version_number() >= 3_054_000,
        "SQLite {} predates the database/WAL memory disclosure fix",
        rusqlite::version()
    );
}

struct TestDirectory {
    _parent: TempDir,
    path: PathBuf,
}
impl TestDirectory {
    fn path(&self) -> &Path {
        &self.path
    }
}
fn private_directory() -> TestDirectory {
    let parent = tempfile::tempdir().unwrap();
    let path = parent.path().join("store");
    #[cfg(unix)]
    fs::DirBuilder::new().mode(0o700).create(&path).unwrap();
    #[cfg(windows)]
    drop(cigar_windows_ipc::PrivateStorageDirectory::open(&path, true).unwrap());
    TestDirectory {
        _parent: parent,
        path,
    }
}

fn options(directory: &TestDirectory) -> BrokerStorageOptions {
    let mut options = BrokerStorageOptions::new(directory.path());
    options.max_checkpoint_bytes = 4 * 1024 * 1024;
    options.max_database_bytes = 16 * 1024 * 1024;
    options.max_journal_records = 3;
    options
}
fn open(options: BrokerStorageOptions) -> Result<(ContextBroker, Store, bool), BrokerError> {
    Store::open(
        "durable-test".into(),
        GraphLimits::default(),
        BrokerLimits::default(),
        options,
    )
}
fn provenance() -> SourceProvenance {
    SourceProvenance {
        authority: "test-host".into(),
        upstream_revision: "one".into(),
        observed_at_ms: 1,
        valid_until_ms: None,
        origin: SourceOrigin::Host,
        derived_from: vec![],
    }
}
fn change(broker: &mut ContextBroker, text: &str) -> Event {
    let expected = broker.host_source_revision("docs").unwrap();
    broker
        .host_replace_source(
            "docs",
            &expected,
            if text.is_empty() {
                vec![]
            } else {
                vec![Document::new("fact", "docs", text)]
            },
            provenance(),
        )
        .unwrap();
    Event {
        operation: Operation::ReplaceSource,
        epoch: broker.epoch().into(),
        expected: BTreeMap::from([("docs".into(), expected)]),
        resulting: BTreeMap::from([("docs".into(), broker.host_source_revision("docs").unwrap())]),
    }
}
fn grant(broker: &mut ContextBroker) -> crate::broker::BrokerCredential {
    broker
        .host_grant(AgentGrantSpec {
            view: ContextViewSpec {
                id: "reader".into(),
                policy_revision: "one".into(),
                allowed_sources: BTreeSet::from(["docs".into()]),
                writable_sources: BTreeSet::new(),
            },
            limits: AgentLimits::default(),
            lease_ms: 60_000,
        })
        .unwrap()
}

#[cfg(windows)]
#[test]
#[allow(clippy::expect_used)] // Named test phases diagnose the redacted public error boundary.
fn windows_sqlite_vfs_preserves_pinned_private_file() {
    // Keep individual phase assertions: the public boundary deliberately redacts OS/SQLite
    // failures to Unavailable, but this fixture must identify which interoperability failed.
    let directory = private_directory();
    let options = options(&directory);
    let (path, created) = PrivatePath::prepare(&options).expect("prepare private NTFS path");
    assert!(created);
    let mut connection = Connection::open_with_flags(
        &path.database,
        OpenFlags::SQLITE_OPEN_READ_WRITE
            | OpenFlags::SQLITE_OPEN_NO_MUTEX
            | OpenFlags::SQLITE_OPEN_NOFOLLOW,
    )
    .expect("open pinned NTFS database through SQLite VFS");
    path.verify(&options).expect("verify after VFS open");
    configure(&connection, &options).expect("configure SQLite safety limits");
    let transaction = connection
        .transaction_with_behavior(TransactionBehavior::Exclusive)
        .expect("acquire SQLite exclusive transaction");
    transaction.execute_batch(META).expect("create metadata");
    transaction.execute_batch(JOURNAL).expect("create journal");
    transaction.commit().expect("commit initialization");
    path.verify(&options).expect("verify after SQLite commit");
    path.sync_creation()
        .expect("flush retained database handle");
}

#[test]
fn source_batches_write_one_committed_image_and_restore_no_pending_staging() {
    use super::super::Persistence;
    use crate::broker::protocol::HostCommand;
    let directory = private_directory();
    let mut opts = options(&directory);
    opts.max_journal_records = 16;
    let start = || {
        Persistence::open(
            "durable-test".into(),
            GraphLimits::default(),
            BrokerLimits::default(),
            Some(opts.clone()),
        )
        .unwrap()
    };
    let (mut broker, mut persistence, _) = start();
    let original = change(&mut broker, "old committed evidence");
    persistence.commit(&broker, original).unwrap();
    let source = broker.host_source_revision("docs").unwrap();
    let begin = HostCommand::BeginSourceReplace {
        source: "docs".into(),
        expected: source.clone(),
        provenance: provenance(),
        lease_ms: 60_000,
    };
    assert!(persistence.prepare(&broker, &begin).unwrap().is_none());
    let pending = broker
        .host_begin_source_replace("docs", &source, provenance(), 60_000)
        .unwrap();
    let before = fs::read(&persistence.store.as_ref().unwrap().path.database).unwrap();
    let append = HostCommand::AppendSourceDocuments {
        transaction: pending.clone(),
        documents: vec![Document::new("fact", "docs", "STAGED_NOT_COMMITTED")],
    };
    assert!(persistence.prepare(&broker, &append).unwrap().is_none());
    broker
        .host_append_source_documents(
            &pending,
            vec![Document::new("fact", "docs", "STAGED_NOT_COMMITTED")],
        )
        .unwrap();
    assert_eq!(
        fs::read(&persistence.store.as_ref().unwrap().path.database).unwrap(),
        before
    );
    drop(persistence);
    drop(broker);

    let (mut broker, mut persistence, restored) = start();
    assert!(restored);
    assert_eq!(
        broker.host_commit_source_replace(&pending),
        Err(BrokerError::Stale)
    );
    assert_eq!(
        broker.host_source_revision("docs").unwrap().version,
        source.version
    );
    let credential = grant(&mut broker);
    let request = ContextRequest {
        query: "evidence".into(),
        required: BTreeSet::from(["fact".into()]),
        ..Default::default()
    };
    assert!(
        broker
            .compile(&credential, &request, &Utf8ByteCounter)
            .unwrap()
            .rendered
            .contains("old committed evidence")
    );
    let source = broker.host_source_revision("docs").unwrap();
    let transaction = broker
        .host_begin_source_replace("docs", &source, provenance(), 60_000)
        .unwrap();
    broker
        .host_append_source_documents(
            &transaction,
            vec![Document::new("fact", "docs", "new committed evidence")],
        )
        .unwrap();
    let before: i64 = persistence
        .store
        .as_ref()
        .unwrap()
        .connection
        .query_row("SELECT count(*) FROM broker_journal", [], |row| row.get(0))
        .unwrap();
    let event = persistence
        .prepare(
            &broker,
            &HostCommand::CommitSourceReplace {
                transaction: transaction.clone(),
            },
        )
        .unwrap()
        .unwrap();
    let receipt = broker.host_commit_source_replace(&transaction).unwrap();
    persistence.commit(&broker, event).unwrap();
    let after: i64 = persistence
        .store
        .as_ref()
        .unwrap()
        .connection
        .query_row("SELECT count(*) FROM broker_journal", [], |row| row.get(0))
        .unwrap();
    assert_eq!(after, before + 1);
    drop(persistence);
    drop(broker);
    let (mut broker, _persistence, _) = start();
    assert_eq!(
        broker.host_source_revision("docs").unwrap().version,
        receipt.revision.version
    );
    assert_eq!(
        broker.host_commit_source_replace(&transaction),
        Err(BrokerError::Stale)
    );
    let credential = grant(&mut broker);
    assert!(
        broker
            .compile(&credential, &request, &Utf8ByteCounter)
            .unwrap()
            .rendered
            .contains("new committed evidence")
    );
}

#[test]
fn private_directory_creation_is_explicit_and_never_creates_missing_parents() {
    let parent = tempfile::tempdir().unwrap();
    let directory = parent.path().join("store");
    let mut options = BrokerStorageOptions::new(&directory);
    assert!(open(options.clone()).is_err());
    assert!(!directory.exists());
    options.create_directory = true;
    let (_, store, restored) = open(options.clone()).unwrap();
    assert!(!restored);
    drop(store);
    let (_, store, restored) = open(options.clone()).unwrap();
    assert!(restored);
    drop(store);
    options.directory = parent.path().join("missing-parent").join("store");
    assert!(open(options).is_err());
    assert!(!parent.path().join("missing-parent").exists());
}

#[test]
fn restart_preserves_commits_and_tombstones_with_bounded_receipts_and_fresh_authority() {
    let directory = private_directory();
    let opts = options(&directory);
    let (mut broker, mut store, restored) = open(opts.clone()).unwrap();
    assert!(!restored);
    let old_credential = grant(&mut broker);
    for text in ["one", "two", "three", "four", ""] {
        let event = change(&mut broker, text);
        store.commit(&broker, &event).unwrap();
    }
    let old = broker.host_source_revision("docs").unwrap();
    assert_eq!(old.version, 5);
    let count: i64 = store
        .connection
        .query_row("SELECT count(*) FROM broker_journal", [], |row| row.get(0))
        .unwrap();
    assert_eq!(count, 3);
    assert!(
        open(opts.clone()).is_err(),
        "a second owner must not acquire the store"
    );
    let file = fs::read(&store.path.database).unwrap();
    assert!(
        !file
            .windows(old_credential.secret.len())
            .any(|v| v == old_credential.secret.as_bytes())
    );
    drop(store);
    let (mut broker, _store, restored) = open(opts).unwrap();
    assert!(restored);
    let current = broker.host_source_revision("docs").unwrap();
    assert_eq!(current.version, old.version);
    assert_ne!(current.epoch, old.epoch);
    assert!(
        broker
            .compile(
                &old_credential,
                &ContextRequest::default(),
                &Utf8ByteCounter
            )
            .is_err()
    );
    assert_eq!(
        broker.host_replace_source("docs", &old, vec![], provenance()),
        Err(BrokerError::Conflict)
    );
    let credential = grant(&mut broker);
    let context = broker
        .compile(
            &credential,
            &ContextRequest {
                query: "evidence".into(),
                ..ContextRequest::default()
            },
            &Utf8ByteCounter,
        )
        .unwrap();
    assert!(!context.rendered.contains("four"));
}

#[test]
fn interrupted_transactions_restore_only_the_last_commit() {
    for point in 0..=3 {
        let directory = private_directory();
        let opts = options(&directory);
        let (mut broker, mut store, _) = open(opts.clone()).unwrap();
        let event = change(&mut broker, "old evidence");
        store.commit(&broker, &event).unwrap();
        drop(store);
        let mut child = Command::new(std::env::current_exe().unwrap())
            .args([
                "--exact",
                "worker::broker_worker::persistence::sqlite::tests::crash_child",
                "--ignored",
                "--nocapture",
            ])
            .env("CIGAR_TEST_STORE_DIRECTORY", directory.path())
            .env("CIGAR_TEST_CRASH_POINT", point.to_string())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .unwrap();
        let until = Instant::now() + Duration::from_secs(30);
        while child.try_wait().unwrap().is_none() {
            if Instant::now() >= until {
                let _ = child.kill();
                let _ = child.wait();
                panic!("crash fixture exceeded its process deadline");
            }
            std::thread::sleep(Duration::from_millis(10));
        }
        let output = child.wait_with_output().unwrap();
        assert_eq!(
            output.status.code(),
            Some(91),
            "crash fixture did not run: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        let (mut restored, _store, _) = open(opts).unwrap();
        assert_eq!(
            restored.host_source_revision("docs").unwrap().version,
            if point == 3 { 2 } else { 1 }
        );
        let credential = grant(&mut restored);
        let context = restored
            .compile(
                &credential,
                &ContextRequest {
                    query: "evidence".into(),
                    ..ContextRequest::default()
                },
                &Utf8ByteCounter,
            )
            .unwrap();
        assert_eq!(context.rendered.contains("old evidence"), point != 3);
    }
}

#[test]
#[ignore = "invoked by the bounded crash parent with a dedicated private directory"]
fn crash_child() {
    let directory = std::env::var_os("CIGAR_TEST_STORE_DIRECTORY").unwrap();
    let point: u8 = std::env::var("CIGAR_TEST_CRASH_POINT")
        .unwrap()
        .parse()
        .unwrap();
    let mut opts = BrokerStorageOptions::new(directory);
    opts.max_checkpoint_bytes = 4 * 1024 * 1024;
    opts.max_database_bytes = 16 * 1024 * 1024;
    opts.max_journal_records = 3;
    let (mut broker, mut store, _) = open(opts).unwrap();
    // Force dirty pages to spill before COMMIT, exercising actual hot-journal rollback.
    store
        .connection
        .pragma_update(None, "cache_size", 2)
        .unwrap();
    let event = change(&mut broker, &"new evidence ".repeat(16_000));
    store
        .commit_with_hook(&broker, &event, |boundary| {
            if boundary == point {
                std::process::exit(91);
            }
        })
        .unwrap();
    panic!("fault boundary was not reached");
}

#[test]
fn corruption_and_unknown_schema_are_rejected_without_reset() {
    for sql in [
        "UPDATE broker_meta SET checkpoint=x'00'",
        "UPDATE broker_journal SET previous='corrupt'",
        "UPDATE broker_journal SET event=x'7b7d'",
        "UPDATE broker_journal SET event=zeroblob(32769)",
        "DELETE FROM broker_journal",
        "UPDATE broker_meta SET sequence=99",
        "PRAGMA user_version=2",
        "CREATE TABLE unexpected(data BLOB)",
    ] {
        let directory = private_directory();
        let opts = options(&directory);
        let (mut broker, mut store, _) = open(opts.clone()).unwrap();
        let event = change(&mut broker, "private evidence");
        store.commit(&broker, &event).unwrap();
        drop(store);
        let database = directory.path().join("broker.sqlite3");
        let connection = Connection::open(&database).unwrap();
        connection.execute_batch(sql).unwrap();
        drop(connection);
        let before = fs::read(&database).unwrap();
        assert!(open(opts).is_err());
        assert_eq!(fs::read(&database).unwrap(), before);
    }
}

#[test]
fn exhausted_forged_sequence_is_rejected_without_integer_overflow() {
    let directory = private_directory();
    let opts = options(&directory);
    let (_, store, _) = open(opts.clone()).unwrap();
    drop(store);
    let database = directory.path().join("broker.sqlite3");
    let connection = Connection::open(&database).unwrap();
    let (checkpoint, event): (String, Vec<u8>) = connection
        .query_row(
            "SELECT checkpoint_digest,event FROM broker_journal",
            [],
            |row| Ok((row.get(0)?, row.get(1)?)),
        )
        .unwrap();
    let head = receipt_hash(i64::MAX, ZERO, &checkpoint, &event);
    connection
        .execute(
            "UPDATE broker_meta SET sequence=?1,anchor_sequence=?2,head_digest=?3",
            params![i64::MAX, i64::MAX - 1, head],
        )
        .unwrap();
    connection
        .execute(
            "UPDATE broker_journal SET sequence=?1,digest=?2",
            params![i64::MAX, head],
        )
        .unwrap();
    drop(connection);
    assert!(open(opts).is_err());
}

#[test]
fn maximum_escaped_source_locators_fit_durable_relation_receipts() {
    let directory = private_directory();
    let opts = options(&directory);
    let (mut broker, mut store, _) = open(opts.clone()).unwrap();
    let sources = [
        format!("{}a", "\\".repeat(2047)),
        format!("{}b", "\\".repeat(2047)),
    ];
    for (source, id) in sources.iter().zip(["left", "right"]) {
        let before = broker.host_source_revision(source).unwrap();
        broker
            .host_replace_source(
                source,
                &before,
                vec![Document::new(id, source, "evidence")],
                provenance(),
            )
            .unwrap();
        let event = Event {
            operation: Operation::ReplaceSource,
            epoch: broker.epoch().into(),
            expected: BTreeMap::from([(source.clone(), before)]),
            resulting: BTreeMap::from([(
                source.clone(),
                broker.host_source_revision(source).unwrap(),
            )]),
        };
        store.commit(&broker, &event).unwrap();
    }
    let expected = sources
        .iter()
        .map(|source| (source.clone(), broker.host_source_revision(source).unwrap()))
        .collect();
    let resulting = broker
        .host_set_edge(
            "left",
            "right",
            crate::EdgeKind::Contradicts,
            true,
            &expected,
        )
        .unwrap();
    let event = Event {
        operation: Operation::SetEdge,
        epoch: broker.epoch().into(),
        expected,
        resulting,
    };
    assert!(event_bytes(&event).unwrap().len() > 16 * 1024);
    store.commit(&broker, &event).unwrap();
    drop(store);
    let (broker, _store, _) = open(opts).unwrap();
    for source in sources {
        assert_eq!(broker.host_source_revision(&source).unwrap().version, 2);
    }
}

#[test]
fn rejected_checkpoint_and_disk_bounds_leave_the_previous_durable_evidence() {
    let directory = private_directory();
    let mut opts = options(&directory);
    opts.max_checkpoint_bytes = 4096;
    opts.max_database_bytes = 131_072;
    let (mut broker, mut store, _) = open(opts.clone()).unwrap();
    let event = change(&mut broker, "old");
    store.commit(&broker, &event).unwrap();
    let event = change(&mut broker, &"x".repeat(8192));
    assert_eq!(store.commit(&broker, &event), Err(BrokerError::Quota));
    drop(store);
    let (broker, _store, _) = open(opts).unwrap();
    assert_eq!(broker.host_source_revision("docs").unwrap().version, 1);

    let directory = private_directory();
    let opts = options(&directory);
    let (mut broker, mut store, _) = open(opts.clone()).unwrap();
    let pages: i64 = store
        .connection
        .pragma_query_value(None, "page_count", |row| row.get(0))
        .unwrap();
    store
        .connection
        .pragma_update(None, "max_page_count", pages)
        .unwrap();
    let event = change(&mut broker, &"large evidence".repeat(10_000));
    assert!(store.commit(&broker, &event).is_err());
    drop(store);
    let (broker, _store, _) = open(opts).unwrap();
    assert_eq!(broker.host_source_revision("docs").unwrap().version, 0);
}

#[test]
#[cfg(unix)]
fn unsafe_paths_and_replaced_files_are_rejected_without_chmod_or_following() {
    let directory = private_directory();
    let opts = options(&directory);
    fs::set_permissions(directory.path(), fs::Permissions::from_mode(0o755)).unwrap();
    assert!(open(opts.clone()).is_err());
    assert!(!directory.path().join("broker.sqlite3").exists());
    fs::set_permissions(directory.path(), fs::Permissions::from_mode(0o700)).unwrap();
    let (mut broker, mut store, _) = open(opts.clone()).unwrap();
    let held = directory.path().join("held.sqlite3");
    fs::rename(&store.path.database, &held).unwrap();
    symlink(&held, &store.path.database).unwrap();
    let event = change(&mut broker, "never acknowledged");
    assert!(store.commit(&broker, &event).is_err());
    assert!(open(opts.clone()).is_err());
    drop(store);
    fs::remove_file(directory.path().join("broker.sqlite3")).unwrap();
    fs::hard_link(&held, directory.path().join("broker.sqlite3")).unwrap();
    assert!(open(opts).is_err());

    let directory = private_directory();
    let opts = options(&directory);
    let (_, store, _) = open(opts.clone()).unwrap();
    drop(store);
    let target = directory.path().join("private-target");
    fs::write(&target, b"untouched").unwrap();
    symlink(&target, directory.path().join("broker.sqlite3-journal")).unwrap();
    assert!(open(opts).is_err());
    assert_eq!(fs::read(target).unwrap(), b"untouched");

    let parent = private_directory();
    let child = tempfile::Builder::new()
        .permissions(fs::Permissions::from_mode(0o700))
        .tempdir_in(parent.path())
        .unwrap();
    fs::set_permissions(parent.path(), fs::Permissions::from_mode(0o777)).unwrap();
    assert!(open(BrokerStorageOptions::new(child.path())).is_err());
    assert!(!child.path().join("broker.sqlite3").exists());
    fs::set_permissions(parent.path(), fs::Permissions::from_mode(0o700)).unwrap();
}