1#![forbid(unsafe_code)]
16
17use perspt_sdk::{
18 AgentDomainPackage, CorrectionDirection, DomainDetection, DomainId, DomainScope,
19 EnergyComponent, EnergyModel, HardGatePolicy, ResidualClass, ResidualEvent, ResidualSchema,
20 ResidualWeight, SafetyBarrierDisposition, StabilityClaim, VerificationCadence,
21 VerifierSuiteSpec, WorkspaceSnapshot,
22};
23
24#[derive(Debug, Clone, Default)]
26pub struct ResearchDomain;
27
28impl ResearchDomain {
29 pub fn new() -> Self {
30 Self
31 }
32}
33
34impl AgentDomainPackage for ResearchDomain {
35 fn domain_id(&self) -> DomainId {
36 DomainId::new("research")
37 }
38
39 fn detect(&self, workspace: &WorkspaceSnapshot) -> DomainDetection {
40 let mut evidence = Vec::new();
41 for marker in [
42 "references.bib",
43 "refs.bib",
44 "bibliography.bib",
45 "sources.md",
46 ] {
47 if workspace.has_file_named(marker) {
48 evidence.push(format!("found {marker}"));
49 }
50 }
51 let activated = !evidence.is_empty();
52 DomainDetection {
53 domain: self.domain_id(),
54 activated,
55 confidence: if activated { 0.85 } else { 0.0 },
56 evidence,
57 }
58 }
59
60 fn residual_schema(&self, _scope: &DomainScope) -> ResidualSchema {
61 ResidualSchema::new(vec![
65 ResidualClass::TestFailure, ResidualClass::InterfaceMismatch, ResidualClass::ImportGraph, ResidualClass::ContextDrift, ResidualClass::SheafInconsistency, ResidualClass::SensorUnavailable,
71 ])
72 }
73
74 fn energy_model(&self, scope: &DomainScope) -> EnergyModel {
75 use EnergyComponent::*;
76 use ResidualClass::*;
77 let mut model = EnergyModel::new("research", 0.5).with_correction_budget(4);
78 model.residual_weights = vec![
79 ResidualWeight::new(TestFailure, Log, 3.0), ResidualWeight::new(InterfaceMismatch, Str, 2.0),
81 ResidualWeight::new(ImportGraph, Str, 2.0),
82 ResidualWeight::new(ContextDrift, Sheaf, 1.5),
83 ResidualWeight::new(SheafInconsistency, Sheaf, 2.5),
84 ResidualWeight::new(SensorUnavailable, Boot, 1.0),
85 ];
86 model.energy_tolerance = 0.0;
87 model.stability_claim = Some(StabilityClaim::not_claimed(format!(
88 "research scope: {}",
89 scope.label
90 )));
91 model
92 }
93
94 fn correction_directions(&self, residuals: &[ResidualEvent]) -> Vec<CorrectionDirection> {
95 residuals
96 .iter()
97 .filter_map(|r| match r.class {
98 ResidualClass::TestFailure => Some(CorrectionDirection::new(
99 ResidualClass::TestFailure,
100 "support or retract the claim: cite a primary source or weaken the statement",
101 )),
102 ResidualClass::ImportGraph => Some(CorrectionDirection::new(
103 ResidualClass::ImportGraph,
104 "add the missing citation linking the claim to its source",
105 )),
106 ResidualClass::ContextDrift => Some(CorrectionDirection::new(
107 ResidualClass::ContextDrift,
108 "refresh the stale evidence: re-fetch the source and re-validate the quote bounds",
109 )),
110 _ => None,
111 })
112 .collect()
113 }
114
115 fn tool_entries(&self, _scope: &DomainScope) -> Vec<perspt_sdk::ToolEntry> {
116 Vec::new()
120 }
121
122 fn hard_gate_policy(&self, _scope: &DomainScope) -> HardGatePolicy {
123 HardGatePolicy {
124 required_stages: vec!["citation-check".into()],
125 }
126 }
127
128 fn verifier_suite(&self, _scope: &DomainScope) -> VerifierSuiteSpec {
129 VerifierSuiteSpec {
130 stages: vec!["citation-check".into()],
131 cadence: VerificationCadence::default(),
132 }
133 }
134
135 fn safety_barrier(&self, _scope: &DomainScope) -> SafetyBarrierDisposition {
136 SafetyBarrierDisposition::NotClaimed {
137 reason: "the research domain declares no operational unsafe set yet; \
138 chance-safety certification is unavailable"
139 .into(),
140 }
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147 use perspt_sdk::{
148 score_candidate, DomainRegistry, IndependenceRoute, ResidualSeverity, SensorRef,
149 };
150
151 #[test]
152 fn research_domain_detects_bibliography() {
153 let domain = ResearchDomain::new();
154 let ws = WorkspaceSnapshot::new("/paper", vec!["refs.bib".into(), "draft.md".into()]);
155 assert!(domain.detect(&ws).activated);
156 }
157
158 #[test]
159 fn research_energy_model_validates_and_is_not_claimed() {
160 let model = ResearchDomain::new().energy_model(&DomainScope::default());
161 assert!(model.validate().is_ok());
162 assert!(!model.stability_claim.unwrap().claims_floor());
163 }
164
165 #[test]
166 fn research_scores_an_unsupported_claim() {
167 let domain = ResearchDomain::new();
168 let model = domain.energy_model(&DomainScope::default());
169 let claim = ResidualEvent::new(
170 "claim-7",
171 0,
172 ResidualClass::TestFailure,
173 ResidualSeverity::Error,
174 1.0,
175 SensorRef::new("claim-checker", IndependenceRoute::SeparateModel),
176 )
177 .unwrap();
178 let score = score_candidate(&model, &[claim]).unwrap();
180 assert_eq!(score.total, 3.0);
181 }
182
183 #[test]
184 fn registry_admits_research_alongside_other_domains() {
185 let mut registry = DomainRegistry::new();
187 registry.register(std::sync::Arc::new(ResearchDomain::new()));
188 assert_eq!(registry.len(), 1);
189 assert!(registry.by_id(&DomainId::new("research")).is_some());
190 }
191}