use crate::{
AnswerAssessment, AnswerDraft, AnswerPolicy, ClaimReview, ContextError, ContextGraph,
ContextRequest, ContextSnapshot, Document, SelectionExplanation, SelectionStep, SourceUpdate,
TokenCounter, digest,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
const MAX_VIEWS: usize = 128;
const MAX_SOURCES: usize = 256;
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextViewSpec {
pub id: String,
pub allowed_sources: BTreeSet<String>,
#[serde(default)]
pub writable_sources: BTreeSet<String>,
pub policy_revision: String,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextViewHandle {
pub id: String,
pub generation: u64,
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextView {
id: String,
view: ContextViewHandle,
request: ContextRequest,
scope_id: String,
snapshot: ContextSnapshot,
}
impl std::fmt::Debug for ContextView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ContextView").finish_non_exhaustive()
}
}
impl ContextView {
#[must_use]
pub fn id(&self) -> &str {
&self.id
}
#[must_use]
pub const fn snapshot(&self) -> &ContextSnapshot {
&self.snapshot
}
fn commitment(&self) -> Result<String, ContextError> {
digest(
"cigar.context-view.v1",
&(
&self.view,
&self.request,
&self.scope_id,
self.snapshot.id(),
),
)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextViewAssessment {
pub context_id: String,
pub checked_graph_revision: u64,
pub assessment: AnswerAssessment,
}
struct Entry {
handle: ContextViewHandle,
spec: ContextViewSpec,
}
#[derive(Default)]
pub struct ContextViews {
generation: u64,
entries: BTreeMap<String, Entry>,
}
impl ContextViews {
pub fn define(&mut self, spec: ContextViewSpec) -> Result<ContextViewHandle, ContextError> {
if !crate::graph::valid_id(&spec.id)
|| spec.allowed_sources.len() > MAX_SOURCES
|| !spec.writable_sources.is_subset(&spec.allowed_sources)
|| spec.allowed_sources.iter().any(|source| {
source.is_empty() || source.len() > 2048 || source.chars().any(char::is_control)
})
|| spec.policy_revision.is_empty()
|| spec.policy_revision.len() > 256
{
return Err(ContextError::InvalidInput);
}
if !self.entries.contains_key(&spec.id) && self.entries.len() >= MAX_VIEWS {
return Err(ContextError::LimitExceeded);
}
self.generation = self
.generation
.checked_add(1)
.ok_or(ContextError::LimitExceeded)?;
let handle = ContextViewHandle {
id: spec.id.clone(),
generation: self.generation,
};
self.entries.insert(
spec.id.clone(),
Entry {
handle: handle.clone(),
spec,
},
);
Ok(handle)
}
pub fn revoke(&mut self, id: &str) -> bool {
self.entries.remove(id).is_some()
}
fn entry(&self, handle: &ContextViewHandle) -> Result<&Entry, ContextError> {
self.entries
.get(&handle.id)
.filter(|entry| entry.handle == *handle)
.ok_or(ContextError::RequiredUnavailable)
}
pub fn compile(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
request: &ContextRequest,
tokenizer: &impl TokenCounter,
) -> Result<ContextView, ContextError> {
self.compile_bound(graph, handle, request, tokenizer, None)
}
pub(crate) fn compile_bound(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
request: &ContextRequest,
tokenizer: &impl TokenCounter,
authority: Option<&str>,
) -> Result<ContextView, ContextError> {
self.compile_traced(graph, handle, request, tokenizer, authority, None)
}
#[allow(clippy::too_many_arguments)] fn compile_traced(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
request: &ContextRequest,
tokenizer: &impl TokenCounter,
authority: Option<&str>,
trace: Option<&mut Vec<SelectionStep>>,
) -> Result<ContextView, ContextError> {
let entry = self.entry(handle)?;
let authorized = authorized_nodes(graph, &entry.spec);
let mut scope_id = scope_id(graph, entry, &authorized)?;
if let Some(authority) = authority {
scope_id = digest("cigar.context-view-authority.v1", &(&scope_id, authority))?;
}
let scoped = scoped_request(request, &scope_id, &authorized);
let snapshot = graph.compile_traced(&scoped, tokenizer, trace)?;
let mut context = ContextView {
id: String::new(),
view: handle.clone(),
request: request.clone(),
scope_id,
snapshot,
};
context.id = context.commitment()?;
Ok(context)
}
pub fn replace_source(
&self,
graph: &mut ContextGraph,
handle: &ContextViewHandle,
source: &str,
documents: Vec<Document>,
) -> Result<SourceUpdate, ContextError> {
let entry = self.entry(handle)?;
if !entry.spec.writable_sources.contains(source)
|| documents.iter().any(|doc| {
doc.source != source
|| graph
.documents
.get(&doc.id)
.is_some_and(|old| old.document.source != source)
})
{
return Err(ContextError::RequiredUnavailable);
}
graph.replace_source(source, documents)
}
#[allow(clippy::too_many_arguments)] pub fn check_answer(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
context: &ContextView,
draft: &AnswerDraft,
reviews: &[ClaimReview],
policy: &AnswerPolicy,
tokenizer: &impl TokenCounter,
) -> Result<ContextViewAssessment, ContextError> {
self.revalidate(graph, handle, context, tokenizer)?;
let assessment = graph.check_view_snapshot(&context.snapshot, draft, reviews, policy)?;
Ok(ContextViewAssessment {
context_id: context.id.clone(),
checked_graph_revision: graph.revision(),
assessment,
})
}
pub(crate) fn revalidate(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
context: &ContextView,
tokenizer: &impl TokenCounter,
) -> Result<(), ContextError> {
self.revalidate_bound(graph, handle, context, tokenizer, None)
}
pub(crate) fn revalidate_bound(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
context: &ContextView,
tokenizer: &impl TokenCounter,
authority: Option<&str>,
) -> Result<(), ContextError> {
self.revalidate_traced(graph, handle, context, tokenizer, authority, None)
}
pub fn explain(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
context: &ContextView,
tokenizer: &impl TokenCounter,
) -> Result<SelectionExplanation, ContextError> {
self.explain_bound(graph, handle, context, tokenizer, None)
}
pub(crate) fn explain_bound(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
context: &ContextView,
tokenizer: &impl TokenCounter,
authority: Option<&str>,
) -> Result<SelectionExplanation, ContextError> {
let mut steps = Vec::new();
self.revalidate_traced(
graph,
handle,
context,
tokenizer,
authority,
Some(&mut steps),
)?;
SelectionExplanation::new(
&context.snapshot,
&context.request,
graph.revision(),
tokenizer,
steps,
)
}
#[allow(clippy::too_many_arguments)] fn revalidate_traced(
&self,
graph: &ContextGraph,
handle: &ContextViewHandle,
context: &ContextView,
tokenizer: &impl TokenCounter,
authority: Option<&str>,
trace: Option<&mut Vec<SelectionStep>>,
) -> Result<(), ContextError> {
self.entry(handle)?;
if context.view != *handle {
return Err(ContextError::BaseMismatch);
}
context.snapshot.verify(tokenizer)?;
if context.id != context.commitment()? {
return Err(ContextError::Integrity);
}
let current =
self.compile_traced(graph, handle, &context.request, tokenizer, authority, trace)?;
if context.scope_id != current.scope_id
|| !context.snapshot.same_view_evidence(¤t.snapshot)
{
return Err(ContextError::BaseMismatch);
}
Ok(())
}
}
fn authorized_nodes(graph: &ContextGraph, spec: &ContextViewSpec) -> BTreeSet<String> {
spec.allowed_sources
.iter()
.filter_map(|source| graph.sources.get(source))
.flat_map(|ids| ids.iter().cloned())
.collect()
}
fn scoped_request(
request: &ContextRequest,
scope_id: &str,
authorized: &BTreeSet<String>,
) -> ContextRequest {
let mut scoped = request.clone();
scoped.allowed = Some(
authorized
.iter()
.filter(|id| {
request
.allowed
.as_ref()
.is_none_or(|narrowed| narrowed.contains(*id))
})
.cloned()
.collect(),
);
scoped.policy_revision = scope_id.to_owned();
scoped
}
fn scope_id(
graph: &ContextGraph,
entry: &Entry,
authorized: &BTreeSet<String>,
) -> Result<String, ContextError> {
let documents = graph
.authorized_documents(authorized)
.map(|(id, doc)| (id, &doc.digest))
.collect::<Vec<_>>();
let edges = authorized
.iter()
.filter_map(|id| graph.edges.get(id).map(|edges| (id, edges)))
.collect::<Vec<_>>();
digest(
"cigar.context-view-scope.v1",
&(&graph.domain, &entry.handle, &entry.spec, documents, edges),
)
}