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
//! Claim review is a host trust boundary, not model self-certification.
use crate::{
    ContextError, ContextGraph, ContextRequest, ContextSnapshot, EdgeKind, TokenCounter, digest,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};

/// One factual claim proposed by a model. All displayed factual prose must be represented.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AnswerClaim {
    /// Complete atomic claim, at most 8 KiB. Citation presence does not establish entailment.
    pub text: String,
    /// Selected node IDs, not arbitrary URLs or short prompt handles. Resolve handles first.
    pub citations: BTreeSet<String>,
    /// Elicited probability of correctness in basis points (0..=10000), or unavailable.
    /// This is telemetry only and never makes a claim eligible for release.
    pub confidence_bps: Option<u16>,
}

impl std::fmt::Debug for AnswerClaim {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AnswerClaim")
            .field("bytes", &self.text.len())
            .field("citations", &self.citations.len())
            .finish_non_exhaustive()
    }
}

impl AnswerClaim {
    /// Bind a review to this exact claim, citations, confidence and context snapshot.
    /// A digest binds bytes; it does not authenticate the reviewer.
    pub fn review_key(&self, snapshot_id: &str) -> Result<String, ContextError> {
        self.validate()?;
        if !valid_digest(snapshot_id) {
            return Err(ContextError::InvalidInput);
        }
        digest("cigar.claim-review.v1", &(snapshot_id, self))
    }

    fn validate(&self) -> Result<(), ContextError> {
        if self.text.trim().is_empty()
            || self.text.len() > 8192
            || self.citations.len() > 64
            || self.citations.iter().any(|id| !crate::graph::valid_id(id))
            || self.confidence_bps.is_some_and(|value| value > 10000)
        {
            return Err(ContextError::InvalidInput);
        }
        Ok(())
    }
}

/// A bounded proposed answer. Unchecked free-form prose is deliberately absent.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AnswerDraft {
    /// Exact snapshot supplied to the generator.
    pub snapshot_id: String,
    /// At most 128 atomic claims, with no duplicate review keys.
    pub claims: Vec<AnswerClaim>,
    /// Explicit abstention must contain no claims.
    #[serde(default)]
    pub abstain: bool,
}

impl AnswerDraft {
    /// Return keys for a trusted reviewer to use; this performs no factual assessment.
    pub fn review_keys(&self) -> Result<Vec<String>, ContextError> {
        if !valid_digest(&self.snapshot_id)
            || self.claims.len() > 128
            || (self.abstain && !self.claims.is_empty())
        {
            return Err(ContextError::InvalidInput);
        }
        let keys = self
            .claims
            .iter()
            .map(|claim| claim.review_key(&self.snapshot_id))
            .collect::<Result<Vec<_>, _>>()?;
        if keys.iter().collect::<BTreeSet<_>>().len() != keys.len() {
            return Err(ContextError::InvalidInput);
        }
        Ok(keys)
    }
}

/// Factual judgment supplied by the owning application through a trusted review path.
/// Never deserialize these verdicts from the same untrusted output as the draft.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ClaimVerdict {
    /// Every assertion is supported by the cited selected evidence, with conflicts resolved.
    Supported,
    /// The evidence does not establish the claim (including irrelevant or incomplete citations).
    Unsupported,
    /// The evidence establishes a conflicting claim.
    Contradicted,
    /// The reviewer could not decide. Unknown is never treated as supported.
    Unknown,
}

/// An independently supplied review. The caller authenticates and evaluates its reviewer.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClaimReview {
    /// Key produced by `AnswerClaim::review_key`; stale or unrelated keys are errors.
    pub claim_key: String,
    /// Semantic judgment of the entire cited claim, not just the existence of its citations.
    pub verdict: ClaimVerdict,
    /// Explicit counterclaim nodes considered by the reviewer. Acknowledgment is not resolution;
    /// a supported verdict additionally asserts the reviewer resolved the conflict.
    #[serde(default)]
    pub reviewed_counterevidence: BTreeSet<String>,
}

/// Host-owned answer release policy. Model confidence cannot relax it.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct AnswerPolicy {
    /// Minimum distinct provenance groups per claim, from one to four. Shared locators or
    /// identical complete document text merge groups transitively. Independence beyond this
    /// conservative grouping is the trusted ingestion/reviewer's responsibility.
    pub min_sources: usize,
    /// Threshold for reporting a confident blocked claim, from zero to 10000 inclusive.
    pub high_confidence_bps: u16,
}

impl Default for AnswerPolicy {
    fn default() -> Self {
        Self {
            min_sources: 1,
            high_confidence_bps: 8000,
        }
    }
}

/// Why a claim cannot be released. Multiple failures can apply to one claim.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ClaimIssue {
    /// No citation was proposed.
    Uncited,
    /// A cited node is absent from the current selected context.
    InvalidCitation,
    /// Too few distinct provenance groups remain after collapsing copies and shared sources.
    InsufficientSources,
    /// No trusted review exists for this exact claim and snapshot.
    Unreviewed,
    /// Trusted review found insufficient support.
    Unsupported,
    /// Trusted review found conflicting evidence.
    Contradicted,
    /// Trusted review could not determine support.
    Unknown,
    /// Declared counterevidence in the cited nodes' hard closure was not reviewed.
    UnreviewedCounterevidence,
}

/// Content-free per-claim result in draft order.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClaimAssessment {
    /// Exact key assessed.
    pub claim_key: String,
    /// Number of distinct provenance groups, not a semantic support score.
    pub independent_sources: usize,
    /// Empty only when the complete review contract is satisfied.
    pub issues: Vec<ClaimIssue>,
}

/// All-or-nothing disposition; a single failed claim blocks the whole draft.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnswerDecision {
    /// Every claim passed the trusted review contract.
    Release,
    /// The application must abstain, gather evidence or revise and obtain a new review.
    Abstain,
}

/// Assessment against the graph's current authorization and source state.
/// This is a transient result, not a signed certificate. Recheck after any state/policy change.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AnswerAssessment {
    /// Exact context assessed.
    pub snapshot_id: String,
    /// Exact draft assessed, including explicit abstention.
    pub draft_id: String,
    /// All-or-nothing release decision.
    pub decision: AnswerDecision,
    /// Per-claim failures, without copying claim/source text into operational telemetry.
    pub claims: Vec<ClaimAssessment>,
    /// Failed claims whose supplied confidence reaches the policy threshold.
    /// This is a gate-failure count, not an independently measured hallucination count.
    pub confident_failures: usize,
    /// Claims without elicited confidence. Missing confidence is never imputed.
    pub missing_confidence: usize,
}

impl ContextGraph {
    /// Check a draft using independently supplied trusted reviews and current authorization.
    ///
    /// Recompiles `request` and requires the exact snapshot given to the model. Every citation
    /// must occur in that selected evidence; every claim needs a supported trusted review.
    /// Known hard dependencies/counterclaims are traversed without dropping conflicts. Raising
    /// or lowering confidence never bypasses a failed review. No semantic judge runs here.
    ///
    /// The application must keep `reviews`, `policy` and authorization outside model control,
    /// and display only the assessed claims on `Release`. The runtime cannot establish that a
    /// model decomposed every factual assertion or that a supplied judge is truthful/accurate.
    pub fn check_answer(
        &self,
        request: &ContextRequest,
        draft: &AnswerDraft,
        reviews: &[ClaimReview],
        policy: &AnswerPolicy,
        tokenizer: &impl TokenCounter,
    ) -> Result<AnswerAssessment, ContextError> {
        let (keys, review_map) = prepare_reviews(draft, reviews, policy)?;
        let snapshot = self.compile(request, tokenizer)?;
        self.assess_snapshot(&snapshot, draft, reviews, policy, (keys, review_map))
    }

    // Only call after freshly proving that this snapshot's complete authorized scope,
    // request, evidence and policy still match the current graph. A digest is not authority.
    pub(crate) fn check_view_snapshot(
        &self,
        snapshot: &ContextSnapshot,
        draft: &AnswerDraft,
        reviews: &[ClaimReview],
        policy: &AnswerPolicy,
    ) -> Result<AnswerAssessment, ContextError> {
        let (keys, review_map) = prepare_reviews(draft, reviews, policy)?;
        self.assess_snapshot(snapshot, draft, reviews, policy, (keys, review_map))
    }

    fn assess_snapshot(
        &self,
        snapshot: &ContextSnapshot,
        draft: &AnswerDraft,
        reviews: &[ClaimReview],
        policy: &AnswerPolicy,
        prepared: PreparedReviews<'_>,
    ) -> Result<AnswerAssessment, ContextError> {
        let (keys, review_map) = prepared;
        if snapshot.id() != draft.snapshot_id {
            return Err(ContextError::BaseMismatch);
        }
        let selected = snapshot
            .blocks()
            .iter()
            .flat_map(|block| block.citations.iter().map(|citation| &citation.node_id))
            .collect::<BTreeSet<_>>();
        if reviews.iter().any(|review| {
            review
                .reviewed_counterevidence
                .iter()
                .any(|id| !selected.contains(id))
        }) {
            return Err(ContextError::InvalidInput);
        }
        let mut claims = Vec::new();
        let mut confident_failures = 0;
        let mut missing_confidence = 0;
        for (claim, key) in draft.claims.iter().zip(keys) {
            let mut issues = Vec::new();
            if claim.citations.is_empty() {
                issues.push(ClaimIssue::Uncited);
            }
            if claim.citations.iter().any(|id| !selected.contains(id)) {
                issues.push(ClaimIssue::InvalidCitation);
            }
            let cited = claim
                .citations
                .iter()
                .filter(|id| selected.contains(*id))
                .filter_map(|id| self.documents.get(id))
                .collect::<Vec<_>>();
            // Connected components prevent copies, chunks and transitive aliases from voting
            // repeatedly. At most 64 nodes; using exact complete-text digests costs no BPE work.
            let mut groups: Vec<BTreeSet<usize>> = Vec::new();
            for (index, node) in cited.iter().enumerate() {
                let mut group = BTreeSet::from([index]);
                groups.retain(|existing| {
                    if existing.iter().any(|i| {
                        cited.get(*i).is_some_and(|prior| {
                            prior.document.source == node.document.source
                                || prior.text_digest == node.text_digest
                        })
                    }) {
                        group.extend(existing);
                        false
                    } else {
                        true
                    }
                });
                groups.push(group);
            }
            let independent_sources = groups.len();
            if independent_sources < policy.min_sources {
                issues.push(ClaimIssue::InsufficientSources);
            }
            let counterevidence = self.answer_counterevidence(&claim.citations, &selected);
            match review_map.get(&key) {
                None => issues.push(ClaimIssue::Unreviewed),
                Some(review) => {
                    match review.verdict {
                        ClaimVerdict::Supported => {}
                        ClaimVerdict::Unsupported => issues.push(ClaimIssue::Unsupported),
                        ClaimVerdict::Contradicted => issues.push(ClaimIssue::Contradicted),
                        ClaimVerdict::Unknown => issues.push(ClaimIssue::Unknown),
                    }
                    if !counterevidence.is_subset(&review.reviewed_counterevidence) {
                        issues.push(ClaimIssue::UnreviewedCounterevidence);
                    }
                }
            }
            if !issues.is_empty()
                && claim
                    .confidence_bps
                    .is_some_and(|value| value >= policy.high_confidence_bps)
            {
                confident_failures += 1;
            }
            missing_confidence += usize::from(claim.confidence_bps.is_none());
            claims.push(ClaimAssessment {
                claim_key: key,
                independent_sources,
                issues,
            });
        }
        let decision = if !draft.abstain
            && !claims.is_empty()
            && claims.iter().all(|claim| claim.issues.is_empty())
        {
            AnswerDecision::Release
        } else {
            AnswerDecision::Abstain
        };
        Ok(AnswerAssessment {
            snapshot_id: snapshot.id().to_owned(),
            draft_id: digest("cigar.answer-draft.v1", draft)?,
            decision,
            claims,
            confident_failures,
            missing_confidence,
        })
    }

    fn answer_counterevidence(
        &self,
        citations: &BTreeSet<String>,
        selected: &BTreeSet<&String>,
    ) -> BTreeSet<String> {
        let mut seen = BTreeSet::new();
        let mut pending = citations
            .iter()
            .filter(|id| selected.contains(*id))
            .collect::<Vec<_>>();
        let mut counterevidence = BTreeSet::new();
        while let Some(id) = pending.pop() {
            if !seen.insert(id) {
                continue;
            }
            if let Some(edges) = self.edges.get(id) {
                for (kind, target) in edges {
                    if selected.contains(target)
                        && matches!(kind, EdgeKind::Requires | EdgeKind::Contradicts)
                    {
                        if *kind == EdgeKind::Contradicts && !citations.contains(target) {
                            counterevidence.insert(target.clone());
                        }
                        pending.push(target);
                    }
                }
            }
        }
        counterevidence
    }
}

type PreparedReviews<'a> = (Vec<String>, BTreeMap<&'a String, &'a ClaimReview>);

fn prepare_reviews<'a>(
    draft: &AnswerDraft,
    reviews: &'a [ClaimReview],
    policy: &AnswerPolicy,
) -> Result<PreparedReviews<'a>, ContextError> {
    if !(1..=4).contains(&policy.min_sources)
        || policy.high_confidence_bps > 10000
        || reviews.len() > 128
    {
        return Err(ContextError::InvalidInput);
    }
    let keys = draft.review_keys()?;
    let mut review_map = BTreeMap::new();
    for review in reviews {
        if !keys.contains(&review.claim_key)
            || review.reviewed_counterevidence.len() > 4096
            || review
                .reviewed_counterevidence
                .iter()
                .any(|id| !crate::graph::valid_id(id))
            || review_map.insert(&review.claim_key, review).is_some()
        {
            return Err(ContextError::InvalidInput);
        }
    }
    Ok((keys, review_map))
}

fn valid_digest(value: &str) -> bool {
    value.len() == 64
        && value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}