perspt-agent 0.6.6

SRBN Orchestrator and Agent logic for Perspt
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
//! Durable event recording for the PSP-9 runtime.

use super::*;

/// Durable sink for all live PSP-9 events.
pub struct Psp9Recorder {
    pub(crate) session_id: String,
    pub(crate) store: Arc<SessionStore>,
    ledger: Mutex<Ledger>,
    event_sender: Option<perspt_core::events::channel::EventSender>,
}

impl std::fmt::Debug for Psp9Recorder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Psp9Recorder")
            .field("session_id", &self.session_id)
            .field("head", &self.head())
            .finish_non_exhaustive()
    }
}

impl Psp9Recorder {
    pub(crate) fn create(
        session_id: &str,
        task: &str,
        working_dir: &Path,
        database_path: Option<&Path>,
        shared_store: Option<Arc<SessionStore>>,
        event_sender: Option<perspt_core::events::channel::EventSender>,
    ) -> Result<Self> {
        // An embedder-supplied handle wins: sharing one connection with a
        // dashboard avoids a second live handle on the same database file.
        let store = match (shared_store, database_path) {
            (Some(store), _) => store,
            (None, Some(path)) => Arc::new(SessionStore::open(path).with_context(|| {
                format!(
                    "opening session database {}; if it is corrupted (for example a \
                         poisoned WAL), run `perspt db repair {}`",
                    path.display(),
                    path.display()
                )
            })?),
            (None, None) => Arc::new(SessionStore::new()?),
        };
        store.create_session(&SessionRecord {
            session_id: session_id.into(),
            task: task.into(),
            working_dir: working_dir.display().to_string(),
            merkle_root: None,
            detected_toolchain: None,
            status: "RUNNING_PSP9".into(),
        })?;
        store.initialize_authority_epoch(session_id, 0)?;
        Ok(Self {
            session_id: session_id.into(),
            store,
            ledger: Mutex::new(Ledger::new()),
            event_sender,
        })
    }

    /// Attach to an existing session for mid-loop resume: rebuild the
    /// in-memory chain from the durable rows (verifying it in the process),
    /// so subsequent appends extend the same ledger head.
    pub(crate) fn attach(
        session_id: &str,
        database_path: Option<&Path>,
        shared_store: Option<Arc<SessionStore>>,
        event_sender: Option<perspt_core::events::channel::EventSender>,
    ) -> Result<Self> {
        let store = match (shared_store, database_path) {
            (Some(store), _) => store,
            (None, Some(path)) => Arc::new(SessionStore::open(path).with_context(|| {
                format!(
                    "opening session database {}; if it is corrupted (for example a \
                         poisoned WAL), run `perspt db repair {}`",
                    path.display(),
                    path.display()
                )
            })?),
            (None, None) => Arc::new(SessionStore::new()?),
        };
        let rows = store.get_psp9_events(session_id)?;
        anyhow::ensure!(!rows.is_empty(), "session has no durable ledger to resume");
        let mut ledger = Ledger::new();
        for row in &rows {
            let event: LedgerEvent = serde_json::from_str(&row.event_json)?;
            let head = ledger
                .append(event)
                .map_err(|e| anyhow::anyhow!(e.to_string()))?;
            anyhow::ensure!(
                head == row.hash,
                "ledger hash mismatch at sequence {}; refusing to resume",
                row.sequence
            );
        }
        store.update_session_status(session_id, "RUNNING_PSP9")?;
        Ok(Self {
            session_id: session_id.into(),
            store,
            ledger: Mutex::new(ledger),
            event_sender,
        })
    }

    pub fn record_custom(&self, kind: &str, payload: serde_json::Value) -> Result<()> {
        self.append(LedgerEvent::Custom {
            kind: kind.into(),
            payload,
        })
    }

    /// Write-ahead append: stage the record, persist it durably, and only
    /// then extend the in-memory chain. Staging avoids the previous
    /// clone-the-whole-ledger transaction, which made session recording
    /// O(n²) in events.
    pub(crate) fn append(&self, event: LedgerEvent) -> Result<()> {
        let mut guard = self.ledger.lock().unwrap();
        let record = guard
            .stage(event)
            .map_err(|e| anyhow::anyhow!(e.to_string()))?;
        self.store.record_psp9_event(&Psp9LedgerRow {
            session_id: self.session_id.clone(),
            sequence: record.sequence as i64,
            event_json: serde_json::to_string(&record.event)?,
            prev_hash: record.prev_hash.clone(),
            hash: record.hash.clone(),
        })?;
        guard
            .commit_staged(record)
            .map_err(|e| anyhow::anyhow!(e.to_string()))?;
        Ok(())
    }

    pub fn head(&self) -> String {
        self.ledger.lock().unwrap().head()
    }

    pub(crate) fn finish(&self, status: &str) -> Result<()> {
        self.store.update_session_status(&self.session_id, status)
    }

    pub(crate) fn authority_epoch(&self) -> Result<u64> {
        self.store.authority_epoch(&self.session_id)
    }

    pub(crate) fn record_grant_policy(
        &self,
        policy: &GrantPolicy,
        signed: Option<&perspt_sdk::SignedGrantPolicy>,
    ) -> Result<()> {
        let durable = signed
            .map(serde_json::to_string)
            .transpose()?
            .unwrap_or_else(|| serde_json::to_string(policy).expect("serializable grant policy"));
        self.store
            .record_grant_policy(&self.session_id, &policy.policy_id, &durable)?;
        self.record_custom(
            "grant_policy",
            serde_json::json!({
                "policy": policy,
                "signed": signed.is_some(),
            }),
        )
    }

    pub(crate) fn record_external_intent(
        &self,
        key: &str,
        intent: &serde_json::Value,
    ) -> Result<()> {
        let bytes = serde_json::to_vec(intent)?;
        self.store.record_external_effect_intent(
            &self.session_id,
            key,
            &perspt_sdk::ledger::content_hash(&bytes),
            &String::from_utf8(bytes)?,
        )?;
        self.record_custom("external_effect_intent", intent.clone())
    }

    pub(crate) fn complete_external_effect(
        &self,
        key: &str,
        result: &serde_json::Value,
    ) -> Result<()> {
        self.store.complete_external_effect(
            &self.session_id,
            key,
            &serde_json::to_string(result)?,
        )?;
        self.record_custom("external_effect_completed", result.clone())
    }
}

impl LoopRecorder for Psp9Recorder {
    fn external_intent(&self, key: &str, intent: &serde_json::Value) -> Result<()> {
        self.record_external_intent(key, intent)
    }

    fn external_result(&self, key: &str, result: &serde_json::Value) -> Result<()> {
        self.complete_external_effect(key, result)
    }

    fn record(&self, event: &LoopEvent) -> Result<()> {
        // PSP-10 Gate AD: every post-cutover runtime row is written once,
        // wrapped in the versioned envelope.
        let envelope = crate::toolloop::LoopEventEnvelopeV1::new(event.clone());
        self.record_custom("tool_loop", serde_json::to_value(&envelope)?)?;
        if let LoopEvent::ContextCheckpointCreated { checkpoint } = event {
            self.store.record_psp9_checkpoint(
                &self.session_id,
                &checkpoint.covered_event_root,
                &serde_json::to_string(checkpoint)?,
            )?;
        }
        if let LoopEvent::DurableCandidateCheckpoint {
            candidate_id,
            state_root,
            control,
            conversation,
            birth_deps,
            canonical_scope,
            files,
        } = event
        {
            // The durable resume point: `perspt resume` rebuilds the
            // accepted candidate from these rows.
            self.store.record_psp9_checkpoint(
                &self.session_id,
                state_root,
                &serde_json::to_string(&serde_json::json!({
                    "kind": "candidate",
                    "candidate_id": candidate_id,
                    "state_root": state_root,
                    "control": control,
                    "conversation": conversation,
                    "birth_deps": birth_deps,
                    "canonical_scope": canonical_scope,
                    "files": files,
                }))?,
            )?;
        }
        if let Some(sender) = &self.event_sender {
            // Structured surfaces first: the TUI's energy panel consumes the
            // typed event, not the narration line.
            if let LoopEvent::CandidateMeasured {
                node_id, energy, ..
            } = event
            {
                let _ = sender.send(perspt_core::AgentEvent::EnergyUpdated {
                    node_id: node_id.clone(),
                    energy: *energy as f32,
                });
            }
            let message = narration(event);
            if let Some(message) = message {
                let _ = sender.send(perspt_core::AgentEvent::Log(message));
            }
        }
        Ok(())
    }

    fn record_artifact(&self, content: &[u8], media_type: &str) -> Result<String> {
        let handle = perspt_sdk::ledger::content_hash(content);
        self.store.put_psp9_artifact(&handle, content, media_type)?;
        Ok(handle)
    }

    fn fetch_artifact(&self, handle: &str) -> Result<Option<Vec<u8>>> {
        self.store.get_psp9_artifact(handle)
    }
}

/// The one-line human narration for a loop event, if any.
fn narration(event: &LoopEvent) -> Option<String> {
    match event {
        LoopEvent::CandidateMeasured {
            node_id,
            generation,
            energy,
            hard_pass,
            residuals,
            ..
        } => Some(format!(
            "Measured {node_id} generation {generation}: V={energy:.3}, \
                 hard_pass={hard_pass}, residuals={}",
            residuals.len()
        )),
        LoopEvent::GateDecisionRecorded {
            node_id,
            generation,
            decision,
            ..
        } => Some(format!(
            "Gate {node_id} generation {generation}: {decision:?}"
        )),
        LoopEvent::EffectDenied {
            call_id, reason, ..
        } => Some(format!("Effect {call_id} denied: {reason}")),
        LoopEvent::EffectApplied {
            call_id, mutated, ..
        } => Some(format!(
            "Effect {call_id} applied to candidate (mutated={mutated})"
        )),
        LoopEvent::RouteFailover {
            from_model,
            to_model,
            cause,
        } => Some(format!(
            "Route failover {from_model} -> {to_model}: {cause}"
        )),
        LoopEvent::ContextCheckpointCreated { checkpoint } => Some(format!(
            "Context checkpoint {} covers events {}..{}",
            &checkpoint.covered_event_root[..12.min(checkpoint.covered_event_root.len())],
            checkpoint.covered_from,
            checkpoint.covered_to
        )),
        LoopEvent::RecoveryContained { reason, .. } => {
            Some(format!("Recovery contained the node: {reason}"))
        }
        _ => None,
    }
}

impl super::Psp9AgentRuntime {
    pub(super) fn record_route_capabilities(&self, recorder: &Psp9Recorder) -> Result<()> {
        for (position, model) in std::iter::once(&self.model)
            .chain(self.fallback_models.iter())
            .enumerate()
        {
            let capabilities = self.transport.capabilities(model);
            let mut degradations = Vec::new();
            if !capabilities.strict_schema {
                degradations.push("strict_schema:local_validation");
            }
            if !capabilities.parallel_tool_calls {
                degradations.push("parallel_tool_calls:sequential_execution");
            }
            if !capabilities.streaming_tool_calls {
                degradations.push("streaming_tool_calls:turn_granular_progress");
            }
            if !capabilities.prompt_caching {
                degradations.push("prompt_caching:cache_cold_accounting");
            }
            if !capabilities.structured_output {
                degradations.push("structured_output:local_parse_and_validation");
            }
            recorder.record_custom(
                "provider_capability_evidence",
                serde_json::json!({
                    "model": model,
                    "route_position": position,
                    "source": "declared",
                    "capabilities": capabilities,
                    "degradations": degradations,
                }),
            )?;
        }
        Ok(())
    }
}

impl Psp9Recorder {
    /// Ledger one compiled prompt program exactly (PSP-10 Gate Z): route,
    /// dialect, ordered section provenance, budget-dropped sections, the
    /// tool-surface hash, and the program digest.
    pub(crate) fn record_prompt_program(
        &self,
        program: &perspt_sdk::prompt::CompiledPromptProgram,
    ) -> Result<()> {
        let sections: Vec<&perspt_sdk::prompt::SectionProvenance> = program
            .messages
            .iter()
            .flat_map(|message| message.sections.iter())
            .collect();
        self.record_custom(
            "prompt_program_compiled",
            serde_json::json!({
                "stage": program.stage.dir_name(),
                "route": program.route,
                "dialect": program.dialect,
                "sections": sections,
                "dropped_sections": program.dropped_sections,
                "tool_spec_hash": program.tool_spec_hash,
                "digest": program.program_digest,
            }),
        )
    }

    /// Ledger one model call's exact prompt binding: both program digests
    /// and the invocation digest (Gate Z, one record per call).
    pub(crate) fn record_prompt_invocation(
        &self,
        actor: &str,
        turn: u32,
        invocation: &perspt_sdk::prompt::CompiledPromptInvocation,
    ) -> Result<()> {
        self.record_custom(
            "prompt_program_invoked",
            serde_json::json!({
                "actor": actor,
                "turn": turn,
                "platform_digest": invocation.platform.program_digest,
                "domain_digest": invocation.domain.program_digest,
                "invocation_digest": invocation.invocation_digest,
                "tool_spec_hash": invocation.platform.tool_spec_hash,
            }),
        )
    }
}