Skip to main content

perspt_agent/runtime/
settings.rs

1//! Run settings, model routes, and the run summary for one PSP-9 session.
2
3use perspt_sdk::{ApprovalPolicy, NodeTerminalOutcome};
4
5/// Finite settings for one PSP-9 run.
6#[derive(Debug, Clone)]
7pub struct Psp9RunConfig {
8    pub max_turns: u32,
9    pub max_calls_per_turn: u32,
10    pub rejection_budget: u32,
11    pub rho_gate: f64,
12    pub approval_policy: ApprovalPolicy,
13    /// Explicit reduced-isolation mode for hosts without a registered process
14    /// sandbox, or embedders that isolate the complete Perspt process.
15    pub allow_unisolated_verifiers: bool,
16    pub max_parallel_verifiers: usize,
17    /// Persist signed grant intent across sessions. Disabled by default;
18    /// resume still re-mints fresh, epoch-bound capabilities.
19    pub persistent_grants: bool,
20    /// Explicit opt-in for governed dependency mutation (Gate J). Off by
21    /// default: `MutateDependencies` stays withheld from every grant.
22    pub allow_dependency_mutation: bool,
23    /// Explicit opt-in for delegated subagent spawning (`spawn_agent`).
24    /// Off by default: `SpawnAgent` stays withheld from every grant.
25    pub allow_subagents: bool,
26    /// Concurrent work-graph nodes (Gate P). 1 keeps the single-node path
27    /// verbatim; above 1 the multi-node dispatcher runs and a governed
28    /// architect planning turn may decompose the task.
29    pub max_parallel_nodes: usize,
30    /// Per-model-call wall-clock deadline (seconds). Exceeding it is a
31    /// transport failure that consumes sticky failover.
32    pub turn_deadline_secs: u64,
33    /// Declare the plugin `format` verifier stage as an acceptance sensor
34    /// (`[verification] require_format`). Off by default.
35    pub require_format: bool,
36    /// How project tests contribute to acceptance. Ordinary development uses
37    /// `Evolving`; stronger regression or protected-oracle evidence is
38    /// deliberately opt-in.
39    pub test_policy: perspt_core::TestPolicy,
40    /// Protected acceptance material for `TestPolicy::ExternalOracle`.
41    pub external_oracle: Option<perspt_core::ExternalOracleConfig>,
42    /// Per-stage governed verifier wall-clock limits
43    /// (`[verification] stage_timeout_secs` + per-stage overrides).
44    pub verifier_timeouts: crate::verifier::VerifierTimeouts,
45    /// Definition 6 context reserves and working-set bounds (`[context]`).
46    pub resident: crate::toolloop::ResidentReserves,
47    /// Seven-arm evaluation ablation: drop typed correction packets and
48    /// fall back to the legacy first-direction text. Never set in
49    /// production configurations.
50    pub ablate_correction_packets: bool,
51    /// Seven-arm evaluation ablation: skip live resident-context paging.
52    /// Never set in production configurations.
53    pub ablate_context_paging: bool,
54}
55
56impl Default for Psp9RunConfig {
57    fn default() -> Self {
58        Self {
59            max_turns: 12,
60            max_calls_per_turn: 8,
61            rejection_budget: 4,
62            rho_gate: 0.5,
63            approval_policy: ApprovalPolicy::Ask,
64            allow_unisolated_verifiers: false,
65            max_parallel_verifiers: 4,
66            persistent_grants: false,
67            allow_dependency_mutation: false,
68            allow_subagents: false,
69            max_parallel_nodes: 1,
70            turn_deadline_secs: crate::turn::DEFAULT_TURN_DEADLINE_SECS,
71            require_format: false,
72            test_policy: perspt_core::TestPolicy::Evolving,
73            external_oracle: None,
74            verifier_timeouts: crate::verifier::VerifierTimeouts::default(),
75            resident: crate::toolloop::ResidentReserves::default(),
76            ablate_correction_packets: false,
77            ablate_context_paging: false,
78        }
79    }
80}
81
82#[derive(Debug, Clone)]
83pub struct Psp9RunSummary {
84    pub session_id: String,
85    pub node_id: String,
86    pub outcome: NodeTerminalOutcome,
87    pub turns_used: u32,
88    pub ledger_head: String,
89    pub promoted_paths: Vec<String>,
90}
91
92/// Explicit model-plane routes for one PSP-9 session. Role routes are not
93/// interchangeable: only `fallbacks` may replace the actuator after a recorded
94/// transport failure.
95#[derive(Debug, Clone, Default)]
96pub struct Psp9ModelRoutes {
97    pub primary: Option<String>,
98    pub actuator: Option<String>,
99    pub explorer: Option<String>,
100    pub adjudicator: Option<String>,
101    pub fallbacks: Vec<String>,
102}
103
104/// Fold the file configuration into the run settings: `[models]`
105/// turn deadline, `[verification]` format gating, and `[context]` reserves.
106pub(super) fn apply_config_overrides(
107    mut run_config: Psp9RunConfig,
108    config: &perspt_core::Config,
109) -> Psp9RunConfig {
110    if let Some(secs) = config.models.as_ref().and_then(|m| m.turn_timeout_secs) {
111        run_config.turn_deadline_secs = secs.max(1);
112    }
113    if let Some(verification) = &config.verification {
114        run_config.allow_unisolated_verifiers |= verification.allow_unisolated.unwrap_or(false);
115        run_config.require_format = verification.require_format.unwrap_or(false);
116        run_config.test_policy = verification.test_policy.unwrap_or_default();
117        run_config.external_oracle = verification.external_oracle.clone();
118        let defaults = crate::verifier::VerifierTimeouts::default();
119        run_config.verifier_timeouts = crate::verifier::VerifierTimeouts {
120            default_secs: verification
121                .stage_timeout_secs
122                .unwrap_or(defaults.default_secs),
123            syntax: verification.syntax_timeout_secs,
124            build: verification.build_timeout_secs,
125            test: verification.test_timeout_secs,
126            lint: verification.lint_timeout_secs,
127            format: verification.format_timeout_secs,
128        };
129    }
130    fold_context_overrides(&mut run_config, config);
131    run_config
132}
133
134fn fold_context_overrides(run_config: &mut Psp9RunConfig, config: &perspt_core::Config) {
135    if let Some(context) = &config.context {
136        let defaults = crate::toolloop::ResidentReserves::default();
137        run_config.resident = crate::toolloop::ResidentReserves {
138            paging_enabled: defaults.paging_enabled,
139            output_reserve_tokens: context
140                .output_reserve_tokens
141                .unwrap_or(defaults.output_reserve_tokens),
142            guard_reserve_tokens: context
143                .guard_reserve_tokens
144                .unwrap_or(defaults.guard_reserve_tokens),
145            frame_tokens: context
146                .synopsis_frame_tokens
147                .unwrap_or(defaults.frame_tokens),
148            pinned_tail: context
149                .working_set_turns
150                .map(|turns| turns as usize)
151                .unwrap_or(defaults.pinned_tail),
152        };
153    }
154}
155
156#[cfg(test)]
157mod tests {
158    use super::*;
159
160    #[test]
161    fn verification_timeouts_fold_from_the_config_file() {
162        let mut config = perspt_core::Config::default();
163        config.verification = Some(perspt_core::config::VerificationConfig {
164            stage_timeout_secs: Some(240),
165            test_timeout_secs: Some(600),
166            ..Default::default()
167        });
168        let folded = apply_config_overrides(Psp9RunConfig::default(), &config);
169        assert_eq!(folded.verifier_timeouts.default_secs, 240);
170        assert_eq!(folded.verifier_timeouts.test, Some(600));
171        assert_eq!(folded.verifier_timeouts.build, None);
172        use perspt_core::plugin::VerifierStage;
173        let timeouts = folded.verifier_timeouts;
174        assert_eq!(timeouts.for_stage(Some(VerifierStage::Test)).as_secs(), 600);
175        assert_eq!(
176            timeouts.for_stage(Some(VerifierStage::Build)).as_secs(),
177            240
178        );
179        assert_eq!(timeouts.for_stage(None).as_secs(), 240);
180    }
181
182    #[test]
183    fn default_timeouts_stay_at_180_seconds() {
184        let timeouts = crate::verifier::VerifierTimeouts::default();
185        assert_eq!(
186            timeouts
187                .for_stage(Some(perspt_core::plugin::VerifierStage::Lint))
188                .as_secs(),
189            180
190        );
191    }
192
193    #[test]
194    fn test_policy_folds_from_verification_config() {
195        let mut config = perspt_core::Config::default();
196        config.verification = Some(perspt_core::config::VerificationConfig {
197            test_policy: Some(perspt_core::TestPolicy::BackwardCompatible),
198            ..Default::default()
199        });
200        let folded = apply_config_overrides(Psp9RunConfig::default(), &config);
201        assert_eq!(
202            folded.test_policy,
203            perspt_core::TestPolicy::BackwardCompatible
204        );
205        assert!(folded.external_oracle.is_none());
206    }
207
208    #[test]
209    fn reduced_isolation_requires_an_explicit_config_opt_in() {
210        let strict = apply_config_overrides(Psp9RunConfig::default(), &Default::default());
211        assert!(!strict.allow_unisolated_verifiers);
212
213        let mut config = perspt_core::Config::default();
214        config.verification = Some(perspt_core::config::VerificationConfig {
215            allow_unisolated: Some(true),
216            ..Default::default()
217        });
218        let folded = apply_config_overrides(Psp9RunConfig::default(), &config);
219        assert!(folded.allow_unisolated_verifiers);
220    }
221}