hol_cigar/explain.rs
1//! Opt-in, current-state selection traces. These are not semantic truth assessments.
2
3use crate::{ContextError, ContextRequest, ContextSnapshot, TokenCounter, digest};
4use serde::Serialize;
5
6/// Signals used by the deterministic selector for an admitted root.
7/// They describe retrieval, not support for a claim or permission to execute a tool.
8#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
9#[serde(rename_all = "snake_case")]
10pub enum SelectionSignal {
11 /// Explicitly required by the context request.
12 Required,
13 /// At least one indexed query term matched this root.
14 LexicalMatch,
15 /// A matched identifier was recognized as a declaration by the lexical index.
16 DeclarationMatch,
17 /// This root was admitted from the caller's ranked semantic IDs.
18 SemanticCandidate,
19 /// This root was admitted by bounded graph expansion.
20 GraphExpansion,
21}
22
23/// One successful selection step, in the order it happened. Only selected IDs appear.
24#[derive(Clone, Eq, PartialEq, Serialize)]
25pub struct SelectionStep {
26 /// Root whose complete hard closure was considered at this step.
27 pub root_id: String,
28 /// Previously unselected IDs added by this step, including required counterevidence.
29 /// Sorted by ID; coalesced equal-text documents retain distinct IDs here.
30 pub added_ids: Vec<String>,
31 /// Retrieval signals, not calibrated confidence or a semantic justification.
32 pub signals: Vec<SelectionSignal>,
33}
34
35impl std::fmt::Debug for SelectionStep {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 f.debug_struct("SelectionStep")
38 .field("added_documents", &self.added_ids.len())
39 .finish_non_exhaustive()
40 }
41}
42
43/// Reconstructed selection trace after current evidence and authority validation.
44/// Contains selected IDs, so keep it within the same disclosure boundary as its context.
45/// No query/source text, rejected IDs, similarity scores or truth probabilities are returned.
46/// This record is neither a signed receipt nor a replacement for answer review.
47#[derive(Clone, Eq, PartialEq, Serialize)]
48pub struct SelectionExplanation {
49 /// Independently versioned explanation shape; the snapshot ABI is unchanged.
50 pub schema: &'static str,
51 /// Exact original snapshot being explained.
52 pub snapshot_id: String,
53 /// Domain-separated commitment to the caller's exact context request.
54 pub request_id: String,
55 /// Current revision at validation. Out-of-scope writes may preserve a view's evidence.
56 pub checked_graph_revision: u64,
57 /// Exact algorithm/vocabulary identity used for context-text token accounting.
58 pub tokenizer: String,
59 /// Successful selection steps only. Unselected evidence is not disclosed.
60 pub steps: Vec<SelectionStep>,
61}
62
63impl std::fmt::Debug for SelectionExplanation {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 f.debug_struct("SelectionExplanation")
66 .field("steps", &self.steps.len())
67 .finish_non_exhaustive()
68 }
69}
70
71impl SelectionExplanation {
72 pub(crate) fn new(
73 snapshot: &ContextSnapshot,
74 request: &ContextRequest,
75 checked_graph_revision: u64,
76 tokenizer: &impl TokenCounter,
77 steps: Vec<SelectionStep>,
78 ) -> Result<Self, ContextError> {
79 Ok(Self {
80 schema: "cigar.context-selection-explanation.v1",
81 snapshot_id: snapshot.id().to_owned(),
82 request_id: digest("cigar.context-explanation-request.v1", request)?,
83 checked_graph_revision,
84 tokenizer: tokenizer.identity().to_owned(),
85 steps,
86 })
87 }
88}