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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! Local graph behavior, budget, update, and integrity acceptance tests.
#[cfg(feature = "bpe")]
use hol_cigar::TokenCounter;
use hol_cigar::{
    ContextDelta, ContextError, ContextGraph, ContextRequest, ContextSnapshot, Document, EdgeKind,
    ExcerptMode, GraphLimits, Utf8ByteCounter,
};
use std::collections::BTreeSet;
use std::error::Error;

fn graph() -> Result<ContextGraph, ContextError> {
    ContextGraph::new("test-domain", GraphLimits::default())
}

fn request(query: &str) -> ContextRequest {
    ContextRequest {
        query: query.into(),
        max_tokens: 8192,
        ..ContextRequest::default()
    }
}

fn ids(snapshot: &ContextSnapshot) -> BTreeSet<String> {
    snapshot
        .blocks()
        .iter()
        .flat_map(|b| b.citations.iter().map(|c| c.node_id.clone()))
        .collect()
}

#[test]
fn graph_recovers_transitive_evidence_and_counterclaims() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    for (id, text) in [
        ("retry", "The retry handler uses operation keys."),
        (
            "contract",
            "An operation key MUST be stable across attempts.",
        ),
        ("database", "Operation keys are scoped to a tenant."),
        (
            "incident",
            "The old handler creates a NEW key and may duplicate writes.",
        ),
    ] {
        graph.upsert(Document::new(id, format!("docs/{id}"), text))?;
    }
    graph.link("retry", "contract", EdgeKind::Requires)?;
    graph.link("contract", "database", EdgeKind::Requires)?;
    graph.link("retry", "incident", EdgeKind::Contradicts)?;
    let context = graph.compile(&request("retry"), &Utf8ByteCounter)?;
    assert_eq!(
        ids(&context),
        BTreeSet::from([
            "retry".into(),
            "contract".into(),
            "database".into(),
            "incident".into()
        ])
    );
    assert!(context.render().contains("NEW key"));
    context.verify(&Utf8ByteCounter)?;
    Ok(())
}

#[test]
fn verified_render_retains_exact_text_and_counts_once() -> Result<(), Box<dyn Error>> {
    struct CountingCounter(std::cell::Cell<usize>);
    impl hol_cigar::TokenCounter for CountingCounter {
        fn identity(&self) -> &str {
            "cigar.utf8-bytes.v1"
        }

        fn count(&self, text: &str) -> Result<usize, ContextError> {
            self.0.set(self.0.get() + 1);
            Ok(text.len())
        }
    }

    let mut graph = graph()?;
    graph.upsert(Document::new(
        "source",
        "quoted/café.md",
        "Authorized café 🦀 evidence.\nA \"quoted\" counterclaim stays visible.",
    ))?;
    let snapshot = graph.compile(&request("evidence"), &Utf8ByteCounter)?;
    let counter = CountingCounter(std::cell::Cell::new(0));
    assert_eq!(snapshot.render_verified(&counter)?, snapshot.render());
    assert_eq!(counter.0.get(), 1);
    snapshot.verify(&counter)?;
    assert_eq!(counter.0.get(), 2);
    Ok(())
}

#[test]
fn verified_render_rejects_tampering_with_the_same_errors() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("source", "source.md", "Authorized evidence."))?;
    let snapshot = graph.compile(&request("evidence"), &Utf8ByteCounter)?;
    let original = serde_json::to_value(&snapshot)?;
    for field in ["id", "domain", "tokenizer", "rendered_tokens", "block_text"] {
        let mut changed = original.clone();
        let (pointer, replacement) = match field {
            "id" => ("/id", "0".repeat(64).into()),
            "domain" => ("/domain", "".into()),
            "tokenizer" => ("/tokenizer", "different-tokenizer".into()),
            "rendered_tokens" => ("/stats/rendered_tokens", 0.into()),
            "block_text" => ("/blocks/0/text", "replaced unauthorized text".into()),
            _ => return Err("unrecognized test mutation".into()),
        };
        *changed.pointer_mut(pointer).ok_or("missing test field")? = replacement;
        let invalid: ContextSnapshot = serde_json::from_value(changed)?;
        let expected = invalid.verify(&Utf8ByteCounter).err();
        assert!(expected.is_some());
        assert_eq!(invalid.render_verified(&Utf8ByteCounter).err(), expected);
    }
    Ok(())
}

#[test]
fn access_is_checked_on_every_graph_hop() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("public", "public.md", "retry"))?;
    graph.upsert(Document::new("private", "private.md", "SECRET_CANARY_ABC"))?;
    graph.link("public", "private", EdgeKind::Supports)?;
    let mut request = request("retry");
    request.allowed = Some(BTreeSet::from(["public".into()]));
    let context = graph.compile(&request, &Utf8ByteCounter)?;
    assert!(!context.render().contains("SECRET_CANARY"));
    graph.link("public", "private", EdgeKind::Requires)?;
    let context = graph.compile(&request, &Utf8ByteCounter)?;
    assert!(context.blocks().is_empty());
    request.required.insert("public".into());
    assert_eq!(
        graph.compile(&request, &Utf8ByteCounter).err(),
        Some(ContextError::RequiredUnavailable)
    );
    Ok(())
}

#[test]
fn withdrawing_dependency_cannot_silently_validate_its_dependent() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("root", "a", "retry"))?;
    graph.upsert(Document::new("dependency", "b", "must retain identity"))?;
    graph.link("root", "dependency", EdgeKind::Requires)?;
    graph.remove("dependency")?;
    let mut request = request("retry");
    request.required.insert("root".into());
    assert_eq!(
        graph.compile(&request, &Utf8ByteCounter).err(),
        Some(ContextError::RequiredUnavailable)
    );
    graph.unlink("root", "dependency", EdgeKind::Requires)?;
    assert_eq!(graph.compile(&request, &Utf8ByteCounter)?.blocks().len(), 1);
    Ok(())
}

#[test]
fn cycles_are_jointly_required_and_bounded() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("a", "a", "alpha"))?;
    graph.upsert(Document::new("b", "b", "beta"))?;
    graph.link("a", "b", EdgeKind::Requires)?;
    graph.link("b", "a", EdgeKind::Requires)?;
    let mut request = request("alpha");
    request.required.insert("a".into());
    assert_eq!(ids(&graph.compile(&request, &Utf8ByteCounter)?).len(), 2);
    request.max_tokens = 10;
    assert_eq!(
        graph.compile(&request, &Utf8ByteCounter).err(),
        Some(ContextError::BudgetUnsatisfiable)
    );
    Ok(())
}

#[test]
fn increments_match_a_fresh_rebuild_and_noop_is_stable() -> Result<(), Box<dyn Error>> {
    let mut incremental = graph()?;
    incremental.upsert(Document::new("a", "src/a", "old obsolete reference"))?;
    incremental.upsert(Document::new("b", "src/b", "retry idempotent"))?;
    let updated = Document::new("a", "src/a", "retry operation identity");
    incremental.upsert(updated.clone())?;
    let revision = incremental.revision();
    assert!(!incremental.upsert(updated.clone())?);
    assert_eq!(revision, incremental.revision());
    let mut rebuilt = graph()?;
    rebuilt.upsert(Document::new("b", "src/b", "retry idempotent"))?;
    rebuilt.upsert(updated)?;
    let query = request("retry operation identity");
    assert_eq!(
        incremental.compile(&query, &Utf8ByteCounter)?.render(),
        rebuilt.compile(&query, &Utf8ByteCounter)?.render()
    );
    assert!(
        incremental
            .compile(&request("obsolete"), &Utf8ByteCounter)?
            .blocks()
            .is_empty()
    );
    Ok(())
}

#[test]
fn insertion_order_does_not_change_context() -> Result<(), Box<dyn Error>> {
    let docs = (0..32)
        .map(|id| {
            Document::new(
                format!("n{id:02}"),
                format!("src/{id}"),
                format!("retry tenant boundary {id}"),
            )
        })
        .collect::<Vec<_>>();
    let mut first = graph()?;
    let mut second = graph()?;
    for doc in &docs {
        first.upsert(doc.clone())?;
    }
    for doc in docs.iter().rev() {
        second.upsert(doc.clone())?;
    }
    assert_eq!(
        first.compile(&request("retry tenant"), &Utf8ByteCounter)?,
        second.compile(&request("retry tenant"), &Utf8ByteCounter)?
    );
    Ok(())
}

#[test]
fn extraction_retains_neighbor_lines_and_exact_provenance() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("a","src/file","heading\nnoise\nbackground\nprior condition\nretry MUST keep identity\nexception detail\nnoise\nfooter"))?;
    let mut query = request("retry");
    query.excerpt_mode = ExcerptMode::QueryWindows;
    let context = graph.compile(&query, &Utf8ByteCounter)?;
    let block = context.blocks().first().ok_or("missing block")?;
    assert_eq!(
        block.text,
        "prior condition\nretry MUST keep identity\nexception detail"
    );
    let citation = block.citations.first().ok_or("missing citation")?;
    assert_eq!((citation.start_line, citation.end_line), (4, 6));
    let mut full = request("retry");
    full.excerpt_mode = ExcerptMode::Full;
    assert!(
        graph
            .compile(&full, &Utf8ByteCounter)?
            .render()
            .contains("footer")
    );
    Ok(())
}

#[test]
fn hard_targets_are_never_left_as_previous_excerpts() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new(
        "a",
        "a",
        "header\nnoise\nnoise\nretry\nnoise\nnoise\nCRITICAL_END",
    ))?;
    graph.upsert(Document::new("b", "b", "boundary operation"))?;
    graph.link("b", "a", EdgeKind::Requires)?;
    let mut query = request("retry boundary");
    query.excerpt_mode = ExcerptMode::QueryWindows;
    let context = graph.compile(&query, &Utf8ByteCounter)?;
    assert!(context.render().contains("CRITICAL_END"));
    Ok(())
}

#[test]
fn exact_duplicate_text_keeps_both_required_citations() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    for id in ["a", "b"] {
        graph.upsert(Document::new(id, format!("src/{id}"), "same evidence"))?;
    }
    let mut request = request("evidence");
    request.required = BTreeSet::from(["a".into(), "b".into()]);
    let context = graph.compile(&request, &Utf8ByteCounter)?;
    assert_eq!(context.blocks().len(), 1);
    assert_eq!(ids(&context).len(), 2);
    context.verify(&Utf8ByteCounter)?;
    Ok(())
}

#[test]
fn deltas_reconstruct_withdrawals_and_reject_wrong_bases_and_policy_changes()
-> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("a", "a", "retry operation"))?;
    graph.upsert(Document::new("b", "b", "tenant boundary"))?;
    let request = request("retry operation tenant boundary");
    let first = graph.compile(&request, &Utf8ByteCounter)?;
    graph.remove("a")?;
    graph.upsert(Document::new("c", "c", "retry new implementation"))?;
    let second = graph.compile(&request, &Utf8ByteCounter)?;
    let delta = second.delta_from(&first, &Utf8ByteCounter)?;
    assert_eq!(delta.apply(&first, &Utf8ByteCounter)?, second);
    assert!(!ids(&second).contains("a"));
    assert_eq!(
        delta.apply(&second, &Utf8ByteCounter).err(),
        Some(ContextError::BaseMismatch)
    );
    let mut changed = request;
    changed.policy_revision = "policy2".into();
    let next = graph.compile(&changed, &Utf8ByteCounter)?;
    assert_eq!(
        next.delta_from(&second, &Utf8ByteCounter).err(),
        Some(ContextError::BaseMismatch)
    );
    Ok(())
}

#[test]
fn corrupted_text_and_extra_delta_blocks_are_rejected() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("a", "a", "retry"))?;
    let first = graph.compile(&request("retry"), &Utf8ByteCounter)?;
    let mut serialized = serde_json::to_string(&first)?;
    serialized = serialized.replace("retry", "CORRUPTED");
    let corrupt: ContextSnapshot = serde_json::from_str(&serialized)?;
    assert_eq!(
        corrupt.verify(&Utf8ByteCounter),
        Err(ContextError::Integrity)
    );
    let unchanged = first.delta_from(&first, &Utf8ByteCounter)?;
    assert_eq!(unchanged.added_blocks(), 0);
    assert_eq!(unchanged.apply(&first, &Utf8ByteCounter)?, first);
    let mut value = serde_json::to_value(&unchanged)?;
    *value.get_mut("added").ok_or("missing added")? = serde_json::to_value(first.blocks())?;
    let extra: ContextDelta = serde_json::from_value(value)?;
    assert_eq!(
        extra.apply(&first, &Utf8ByteCounter).err(),
        Some(ContextError::Integrity)
    );
    Ok(())
}

#[test]
fn semantic_seeds_recover_nonlexical_evidence_but_never_grant_access() -> Result<(), Box<dyn Error>>
{
    let mut graph = graph()?;
    graph.upsert(Document::new(
        "a",
        "design",
        "The custodian renews its lease every twenty seconds.",
    ))?;
    let mut query = request("leader heartbeat frequency");
    assert!(graph.compile(&query, &Utf8ByteCounter)?.blocks().is_empty());
    query.semantic_candidates.push("a".into());
    assert_eq!(
        ids(&graph.compile(&query, &Utf8ByteCounter)?),
        BTreeSet::from(["a".into()])
    );
    query.allowed = Some(BTreeSet::new());
    assert!(graph.compile(&query, &Utf8ByteCounter)?.blocks().is_empty());
    query.semantic_candidates.push("a".into());
    assert_eq!(
        graph.compile(&query, &Utf8ByteCounter).err(),
        Some(ContextError::InvalidInput)
    );
    Ok(())
}

#[test]
fn symbol_queries_prefer_definitions_over_short_mentions_and_keep_full_text_by_default()
-> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new(
        "mention",
        "notes",
        "resolve_reference_tokenizer_target fingerprint",
    ))?;
    graph.upsert(Document::new("definition", "src/tokenizer.rs",
        "pub fn resolve_reference_tokenizer_target(target: &Target) {\n    // Validate both the provider and fingerprint.\n    reject_mismatch(target);\n}\n"))?;
    let query = request("resolve_reference_tokenizer_target fingerprint");
    let snapshot = graph.compile(&query, &Utf8ByteCounter)?;
    assert!(ids(&snapshot).contains("definition"));
    assert!(snapshot.render().contains("reject_mismatch(target)"));
    assert_eq!(ContextRequest::default().excerpt_mode, ExcerptMode::Full);
    Ok(())
}

#[test]
fn copied_text_does_not_count_as_independent_corroboration() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    graph.upsert(Document::new("a", "src/a", "retry identity"))?;
    graph.upsert(Document::new("b", "src/b", "retry identity"))?;
    graph.upsert(Document::new(
        "c",
        "src/c",
        "retry identity is asserted by a separate integration test",
    ))?;
    let mut query = request("retry identity");
    query.evidence_per_term = 2;
    let snapshot = graph.compile(&query, &Utf8ByteCounter)?;
    assert_eq!(ids(&snapshot), BTreeSet::from(["a".into(), "c".into()]));
    Ok(())
}

#[test]
fn oversized_optional_closures_do_not_hide_other_usable_evidence() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    for id in ["a", "b", "c", "d", "z"] {
        graph.upsert(Document::new(id, id, "retry"))?;
    }
    graph.link("a", "b", EdgeKind::Requires)?;
    graph.link("b", "c", EdgeKind::Requires)?;
    graph.link("c", "d", EdgeKind::Requires)?;
    let mut query = request("retry");
    query.max_candidates = 2;
    query.max_blocks = 2;
    query.graph_depth = 0;
    let snapshot = graph.compile(&query, &Utf8ByteCounter)?;
    assert!(snapshot.blocks().is_empty());
    assert_eq!(snapshot.stats().limit_rejected_roots, 2);
    query.allowed = Some(BTreeSet::from([
        "a".into(),
        "b".into(),
        "c".into(),
        "d".into(),
        "z".into(),
    ]));
    query.semantic_candidates.push("z".into());
    let snapshot = graph.compile(&query, &Utf8ByteCounter)?;
    assert!(ids(&snapshot).contains("z"));
    query.required.insert("a".into());
    assert_eq!(
        graph.compile(&query, &Utf8ByteCounter).err(),
        Some(ContextError::LimitExceeded)
    );
    Ok(())
}

#[test]
fn whitespace_only_documents_are_rejected_before_index_mutation() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    for text in ["", "\n", " \t\r\n"] {
        assert_eq!(
            graph.upsert(Document::new("a", "retry", text)),
            Err(ContextError::InvalidInput)
        );
    }
    assert_eq!(graph.revision(), 0);
    assert!(graph.is_empty());
    Ok(())
}

#[test]
fn chunked_sources_preserve_unicode_line_terminators_and_original_offsets()
-> Result<(), Box<dyn Error>> {
    let document = Document::new(
        "file",
        "src/retry.rs",
        "header\r\nprior\r\nretry café\r\nexception 日本語\r\nfooter\r\n",
    );
    let chunks = document.chunks(3, 1)?;
    assert_eq!(chunks.len(), 2);
    assert_eq!(
        chunks.first().ok_or("first chunk")?.text,
        "header\r\nprior\r\nretry café\r\n"
    );
    assert_eq!(chunks.get(1).ok_or("second chunk")?.start_line, 3);
    let mut graph = graph()?;
    for doc in &chunks {
        graph.upsert(doc.clone())?;
    }
    let mut query = request("日本語");
    query.excerpt_mode = ExcerptMode::Full;
    let snapshot = graph.compile(&query, &Utf8ByteCounter)?;
    let block = snapshot.blocks().first().ok_or("block")?;
    let citation = block.citations.first().ok_or("citation")?;
    assert_eq!((citation.start_line, citation.end_line), (3, 5));
    assert_eq!(block.text, "retry café\r\nexception 日本語\r\nfooter\r\n");
    assert_eq!(
        document.chunks(1, 1).err(),
        Some(ContextError::InvalidInput)
    );
    let mut overflowing = Document::new("overflow", "source", "first\nsecond");
    overflowing.start_line = usize::MAX;
    assert_eq!(graph.upsert(overflowing), Err(ContextError::LimitExceeded));
    Ok(())
}

#[test]
fn generated_graphs_preserve_order_budget_authorization_and_hard_closure()
-> Result<(), Box<dyn Error>> {
    for seed in 0_usize..128 {
        let mut first = graph()?;
        let mut second = graph()?;
        let docs = (0..24).map(|i| Document::new(format!("n{i:02}"), format!("source/{i}"),
            format!("concept{}\nprior condition\nidentity MUST remain stable for {i}\nexception {seed}", (i+seed)%7)))
            .collect::<Vec<_>>();
        for doc in &docs {
            first.upsert(doc.clone())?;
        }
        for doc in docs.iter().rev() {
            second.upsert(doc.clone())?;
        }
        let edges = (0..24)
            .filter_map(|i| {
                let target = (i * 7 + seed) % 24;
                (target != i).then(|| {
                    (
                        format!("n{i:02}"),
                        format!("n{target:02}"),
                        if i % 3 == 0 {
                            EdgeKind::Requires
                        } else {
                            EdgeKind::Supports
                        },
                    )
                })
            })
            .collect::<Vec<_>>();
        for (a, b, kind) in &edges {
            first.link(a, b, *kind)?;
        }
        for (a, b, kind) in edges.iter().rev() {
            second.link(a, b, *kind)?;
        }
        let query = ContextRequest {
            max_tokens: 100 + seed * 17,
            query: format!("concept{} identity", seed % 7),
            allowed: Some(
                docs.iter()
                    .enumerate()
                    .filter(|(i, _)| (i + seed) % 5 != 0)
                    .map(|(_, d)| d.id.clone())
                    .collect(),
            ),
            ..ContextRequest::default()
        };
        let snapshot = first.compile(&query, &Utf8ByteCounter)?;
        assert_eq!(snapshot, second.compile(&query, &Utf8ByteCounter)?);
        snapshot.verify(&Utf8ByteCounter)?;
        let selected = ids(&snapshot);
        assert!(
            query
                .allowed
                .as_ref()
                .is_some_and(|allowed| selected.is_subset(allowed))
        );
        for (from, to, kind) in &edges {
            if *kind == EdgeKind::Requires && selected.contains(from) {
                assert!(selected.contains(to));
                let doc = docs
                    .iter()
                    .find(|doc| &doc.id == to)
                    .ok_or("missing target")?;
                assert!(snapshot.blocks().iter().any(|block| block.text == doc.text));
            }
        }
    }
    Ok(())
}

#[test]
fn withdrawn_edge_retention_is_globally_bounded_and_explicitly_reclaimable()
-> Result<(), Box<dyn Error>> {
    let mut graph = ContextGraph::new(
        "churn",
        GraphLimits {
            max_edges: 2,
            ..GraphLimits::default()
        },
    )?;
    for id in ["a", "b", "c"] {
        graph.upsert(Document::new(id, id, "retry"))?;
    }
    graph.link("a", "b", EdgeKind::Contradicts)?;
    graph.remove("b")?;
    let revision = graph.revision();
    assert_eq!(
        graph.link("a", "c", EdgeKind::Requires),
        Err(ContextError::LimitExceeded)
    );
    assert_eq!(graph.revision(), revision);
    assert!(graph.unlink("a", "b", EdgeKind::Contradicts)?);
    assert!(graph.link("a", "c", EdgeKind::Contradicts)?);
    Ok(())
}

#[test]
fn failed_mutations_are_atomic() -> Result<(), Box<dyn Error>> {
    let mut graph = ContextGraph::new(
        "local",
        GraphLimits {
            max_document_bytes: 20,
            max_edges_per_document: 1,
            ..GraphLimits::default()
        },
    )?;
    for id in ["a", "b", "c"] {
        graph.upsert(Document::new(id, id, "retry"))?;
    }
    graph.link("a", "b", EdgeKind::Requires)?;
    let revision = graph.revision();
    assert_eq!(
        graph.link("c", "a", EdgeKind::Contradicts),
        Err(ContextError::LimitExceeded)
    );
    assert_eq!(
        graph.upsert(Document::new("a", "a", "this string exceeds twenty bytes")),
        Err(ContextError::LimitExceeded)
    );
    assert_eq!(revision, graph.revision());
    assert!(!graph.unlink("c", "a", EdgeKind::Contradicts)?);
    Ok(())
}

#[test]
fn special_text_is_quoted_and_debug_does_not_dump_documents() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    let doc = Document::new(
        "a",
        "source\"quote",
        "retry <|endoftext|> \"SECRET_CONTENT\" \\ boundary",
    );
    assert!(!format!("{doc:?}").contains("SECRET_CONTENT"));
    graph.upsert(doc)?;
    let context = graph.compile(&request("retry"), &Utf8ByteCounter)?;
    let line: serde_json::Value = serde_json::from_str(&context.render())?;
    assert!(
        line.get("text")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|text| text.contains("SECRET_CONTENT"))
    );
    assert!(!format!("{context:?}").contains("SECRET_CONTENT"));
    Ok(())
}

#[test]
fn budgets_include_framing_and_reserve_for_every_success() -> Result<(), Box<dyn Error>> {
    let mut graph = graph()?;
    for id in 0..20 {
        graph.upsert(Document::new(
            format!("d{id}"),
            format!("file{id}"),
            format!("retry boundary evidence {id}"),
        ))?;
    }
    for budget in 1..512 {
        let mut query = request("retry boundary evidence");
        query.max_tokens = budget + 5;
        query.reserve_tokens = 5;
        let snapshot = graph.compile(&query, &Utf8ByteCounter)?;
        assert!(snapshot.render().len() <= budget);
        snapshot.verify(&Utf8ByteCounter)?;
    }
    Ok(())
}

#[cfg(feature = "bpe")]
#[test]
fn bpe_counts_actual_rendered_text_and_treats_special_tokens_as_data() -> Result<(), Box<dyn Error>>
{
    let tokenizer = hol_cigar::O200kTokenizer::new()?;
    let mut graph = graph()?;
    graph.upsert(Document::new("a", "a", "retry café 日本語 <|endoftext|>"))?;
    for max_tokens in 1..80 {
        let query = ContextRequest {
            max_tokens,
            ..request("retry")
        };
        let snapshot = graph.compile(&query, &tokenizer)?;
        assert_eq!(
            snapshot.stats().rendered_tokens,
            tokenizer.count(&snapshot.render())?
        );
        assert!(snapshot.stats().rendered_tokens <= max_tokens);
        snapshot.verify(&tokenizer)?;
    }
    Ok(())
}