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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Graph staging and the global integration gate (PSP-10 system 22,
//! Gate AA).
//!
//! Two nodes may pass separately and fail together. Node winners enter a
//! content-addressed staging root instead of the user workspace; disjoint
//! winners merge deterministically; one global verifier gate runs the full
//! domain suite and the configured test-evidence policy on the combined
//! state; and
//! only a hard-passing integration root is promoted, atomically, through
//! the hardened path. Failure restores the prior staging root — the user
//! workspace never contains one winner without the rest of its verified
//! root.

use std::collections::{BTreeMap, BTreeSet};

use anyhow::Result;
use perspt_sdk::canon::CanonicalEncoder;

use super::{Psp9AgentRuntime, Psp9Recorder};
use crate::candidate::{CandidateWorkspace, CodingCandidateMeasurer};
use crate::toolloop::{CandidateMeasurer, EffectExecutor, SeedFile};

/// One staged node winner.
pub(super) struct StagedWinner {
    pub state_root: String,
    pub files: Vec<SeedFile>,
}

/// The content-addressed staging root: node winners keyed by node id.
#[derive(Default)]
pub(super) struct StagingRoot {
    pub contributions: BTreeMap<String, StagedWinner>,
}

impl StagingRoot {
    pub fn is_empty(&self) -> bool {
        self.contributions.is_empty()
    }

    /// The staging root's content address over its contributions.
    pub fn digest(&self) -> String {
        let mut encoder = CanonicalEncoder::new(b"perspt-staging-v1");
        for (node_id, winner) in &self.contributions {
            encoder.text(node_id).text(&winner.state_root);
            for file in &winner.files {
                encoder.text(&file.path).field(
                    file.content
                        .as_deref()
                        .map(perspt_sdk::ledger::content_hash)
                        .unwrap_or_default()
                        .as_bytes(),
                );
            }
        }
        encoder.digest()
    }

    /// Dependency-aware conflict detection: two nodes writing the same
    /// path with different content conflict only when they are
    /// **topologically incomparable** (true siblings). A downstream node
    /// legitimately refines its predecessor's file — edge order defines
    /// precedence, so that pair is a refinement, never a divergence.
    pub fn conflict(&self, graph: &perspt_sdk::WorkGraphRevision) -> Option<String> {
        let ancestors: BTreeMap<&str, BTreeSet<String>> = self
            .contributions
            .keys()
            .map(|node_id| (node_id.as_str(), transitive_predecessors(graph, node_id)))
            .collect();
        let comparable = |a: &str, b: &str| {
            ancestors.get(a).is_some_and(|set| set.contains(b))
                || ancestors.get(b).is_some_and(|set| set.contains(a))
        };
        type Writers<'a> = BTreeMap<&'a str, Vec<(&'a str, Option<&'a [u8]>)>>;
        // The full writer set per path: the pairwise rule must compare
        // every unordered pair, not each writer against one retained
        // owner — a superseded upstream write can still diverge from an
        // incomparable sibling's.
        let mut writers: Writers = BTreeMap::new();
        for (node_id, winner) in &self.contributions {
            for file in &winner.files {
                writers
                    .entry(file.path.as_str())
                    .or_default()
                    .push((node_id, file.content.as_deref()));
            }
        }
        for (path, entries) in &writers {
            for (index, (first, first_content)) in entries.iter().enumerate() {
                for (second, second_content) in &entries[index + 1..] {
                    if first_content != second_content && !comparable(first, second) {
                        return Some(format!(
                            "path {path} written divergently by {first} and {second}"
                        ));
                    }
                }
            }
        }
        None
    }

    /// The merged file set restricted to `contributors`, in topological
    /// order so a downstream contribution overwrites its predecessor's
    /// (deterministic downstream precedence); path order within.
    pub fn merged_files_for(
        &self,
        graph: &perspt_sdk::WorkGraphRevision,
        contributors: &BTreeSet<String>,
    ) -> Vec<SeedFile> {
        let topo: BTreeMap<&str, usize> = graph
            .validation
            .topo_order
            .iter()
            .enumerate()
            .map(|(index, node_id)| (node_id.as_str(), index))
            .collect();
        let mut ordered: Vec<&String> = self
            .contributions
            .keys()
            .filter(|node_id| contributors.contains(*node_id))
            .collect();
        ordered.sort_by_key(|node_id| topo.get(node_id.as_str()).copied().unwrap_or(usize::MAX));
        let mut merged: BTreeMap<String, SeedFile> = BTreeMap::new();
        for node_id in ordered {
            for file in &self.contributions[node_id].files {
                merged.insert(file.path.clone(), file.clone());
            }
        }
        merged.into_values().collect()
    }

    /// The full merged set (the global integration gate's combined state).
    pub fn merged_files(&self, graph: &perspt_sdk::WorkGraphRevision) -> Vec<SeedFile> {
        self.merged_files_for(graph, &self.contributions.keys().cloned().collect())
    }
}

/// Rebuild the staging root from the durable ledger: the newest
/// `staging_root_updated` event per node, its files rehydrated from the
/// content-addressed artifact store. Fails closed on a staged
/// contribution that predates durable, generation-bound staging (no
/// `files` or `generation` field) — an unreconstructible or ambiguously
/// bound staging root must refuse resume, never promote a partial one
/// (Gate AA).
pub(super) fn fold_staging(
    recorder: &Psp9Recorder,
    session_id: &str,
    graph: &perspt_sdk::WorkGraphRevision,
) -> Result<StagingRoot> {
    use anyhow::Context;
    let mut staging = StagingRoot::default();
    let mut pre_generation: BTreeSet<String> = BTreeSet::new();
    for row in recorder.store.get_psp9_events(session_id)? {
        let Ok(perspt_sdk::LedgerEvent::Custom { kind, payload }) =
            serde_json::from_str::<perspt_sdk::LedgerEvent>(&row.event_json)
        else {
            continue;
        };
        if kind != "staging_root_updated" {
            continue;
        }
        let node_id = payload
            .get("node_id")
            .and_then(|value| value.as_str())
            .context("staged contribution names no node")?;
        let Some(node) = graph.node(node_id) else {
            continue;
        };
        let generation = match payload.get("generation").and_then(|value| value.as_u64()) {
            Some(generation) => {
                pre_generation.remove(node_id);
                generation
            }
            None => {
                // Fatal only if no later, generation-bound row for the same
                // node supersedes this one (newest-wins, fail-closed).
                pre_generation.insert(node_id.to_string());
                staging.contributions.remove(node_id);
                continue;
            }
        };
        if generation != u64::from(node.generation) {
            // A refined/replaced node must never inherit a winner produced
            // by its earlier generation. The current generation re-runs.
            continue;
        }
        let files_value = payload.get("files").with_context(|| {
            format!(
                "staged contribution for {node_id} predates durable staging; \
                 the staging root cannot be reconstructed"
            )
        })?;
        let handles: Vec<crate::toolloop::DurableSeedFile> =
            serde_json::from_value(files_value.clone())?;
        let state_root = payload
            .get("state_root")
            .and_then(|value| value.as_str())
            .context("staged contribution carries no state root")?
            .to_string();
        staging.contributions.insert(
            node_id.to_string(),
            StagedWinner {
                state_root,
                files: super::node::load_seed_files(recorder, handles)?,
            },
        );
    }
    anyhow::ensure!(
        pre_generation.is_empty(),
        "staged contributions for {pre_generation:?} predate generation-bound staging; \
         the staging root cannot be reconstructed safely"
    );
    Ok(staging)
}

/// The transitive dependency closure of one node over the graph's edges.
pub(super) fn transitive_predecessors(
    graph: &perspt_sdk::WorkGraphRevision,
    node_id: &str,
) -> BTreeSet<String> {
    let mut wanted = BTreeSet::new();
    let mut frontier = vec![node_id.to_string()];
    while let Some(current) = frontier.pop() {
        for dependency in graph.dependencies_of(&current) {
            if wanted.insert(dependency.to_string()) {
                frontier.push(dependency.to_string());
            }
        }
    }
    wanted
}

impl Psp9AgentRuntime {
    /// Run the global integration gate over the staging root (Gate AA):
    /// realize the combined state in one eager-copy integration workspace,
    /// run the full suite and configured test-evidence policy, and promote
    /// atomically only on a global hard pass. `None` means integration failed
    /// and the prior root is restored (nothing reached the user workspace).
    pub(super) async fn run_integration_gate(
        &self,
        recorder: &Psp9Recorder,
        session_id: &str,
        graph: &perspt_sdk::WorkGraphRevision,
        staging: &StagingRoot,
    ) -> Result<Option<Vec<String>>> {
        let staging_digest = staging.digest();
        if let Some(conflict) = staging.conflict(graph) {
            recorder.record_custom(
                "integration_failed",
                serde_json::json!({
                    "staging_root": staging_digest,
                    "reason": format!("merge conflict: {conflict}"),
                }),
            )?;
            return Ok(None);
        }
        // Realize the combined state in an isolated integration workspace.
        let exclusions = self.candidate_exclusions();
        let mut workspace = CandidateWorkspace::create_filtered(
            &self.working_dir,
            "integration",
            0,
            &graph.revision_id,
            self.config.allow_unisolated_verifiers,
            &exclusions,
        )?;
        workspace.set_tool_handlers(self.tool_handlers.clone());
        workspace.set_verifier_timeouts(self.config.verifier_timeouts);
        workspace.restore_exported(&staging.merged_files(graph))?;
        let measured = CodingCandidateMeasurer::new(&workspace, "integration", 0)
            .with_domain(self.domain.clone())
            .with_max_parallel(self.config.max_parallel_verifiers)
            .with_require_format(self.config.require_format)
            .with_test_policy(self.config.test_policy, self.config.external_oracle.clone())
            .measure()
            .await?;
        recorder.record_custom(
            "integration_measured",
            serde_json::json!({
                "staging_root": staging_digest,
                "energy": measured.energy,
                "hard_pass": measured.hard_pass,
                "residuals": measured.residuals.len(),
            }),
        )?;
        if !measured.hard_pass {
            recorder.record_custom(
                "integration_failed",
                serde_json::json!({
                    "staging_root": staging_digest,
                    "reason": "combined state failed the global verifier gate",
                }),
            )?;
            return Ok(None);
        }
        self.promote_integration_root(recorder, session_id, graph, &workspace, &staging_digest)
            .await
    }

    /// Atomic promotion of the verified integration root through the
    /// hardened path, under the ordinary grant/kernel discipline.
    async fn promote_integration_root(
        &self,
        recorder: &Psp9Recorder,
        session_id: &str,
        graph: &perspt_sdk::WorkGraphRevision,
        workspace: &CandidateWorkspace,
        staging_digest: &str,
    ) -> Result<Option<Vec<String>>> {
        let assembly = {
            let external_entries = self.external_tool_entries(recorder).await?;
            self.assemble_node(
                recorder,
                session_id,
                graph,
                "integration",
                0,
                &external_entries,
            )?
        };
        let kernel_state =
            super::node::loop_kernel_state(&assembly.grant_policy, &graph.revision_id);
        let promoted = self
            .promote_certified(
                recorder,
                workspace,
                "integration",
                &assembly.grant_policy,
                &assembly.capability,
                &assembly.contract,
                &assembly.barrier,
                &kernel_state,
                &assembly.calibration,
            )
            .await?;
        match promoted {
            Some(paths) => {
                recorder.record_custom(
                    "integration_promoted",
                    serde_json::json!({
                        "staging_root": staging_digest,
                        "paths": paths,
                    }),
                )?;
                Ok(Some(paths))
            }
            None => {
                recorder.record_custom(
                    "integration_failed",
                    serde_json::json!({
                        "staging_root": staging_digest,
                        "reason": "promotion was not certified; prior root restored",
                    }),
                )?;
                Ok(None)
            }
        }
    }

    /// The seed one downstream node forks from: only its **transitive
    /// predecessors'** staged contributions (system 22) — an unrelated
    /// sibling's state never leaks in — realized once so the seed's state
    /// root is verifiable. The seed is inherited: it restores without
    /// entering the node's promotable set.
    pub(super) async fn staging_seed(
        &self,
        graph: &perspt_sdk::WorkGraphRevision,
        staging: &StagingRoot,
        node_id: &str,
    ) -> Result<Option<super::node::CandidateSeed>> {
        let predecessors = transitive_predecessors(graph, node_id);
        let contributors: BTreeSet<String> = staging
            .contributions
            .keys()
            .filter(|contributor| predecessors.contains(*contributor))
            .cloned()
            .collect();
        if contributors.is_empty() {
            return Ok(None);
        }
        let exclusions = self.candidate_exclusions();
        let scratch = CandidateWorkspace::create_filtered(
            &self.working_dir,
            "staging-seed",
            0,
            &graph.revision_id,
            self.config.allow_unisolated_verifiers,
            &exclusions,
        )?;
        let files = staging.merged_files_for(graph, &contributors);
        scratch.restore_seeded(&files)?;
        let checkpoint = scratch.checkpoint(&[]).await?;
        Ok(Some(super::node::CandidateSeed {
            expected_state_root: checkpoint.witness.state_root,
            canonical_scope: checkpoint.witness.canonical_scope.clone(),
            files,
            conversation: perspt_sdk::Conversation::default(),
            birth_deps: Vec::new(),
            activated_tools: Vec::new(),
            inherited: true,
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::toolloop::SeedFile;

    fn graph_a_to_b_with_sibling_z() -> perspt_sdk::WorkGraphRevision {
        let node = |id: &str| {
            perspt_sdk::WorkNode::new(id, format!("goal {id}"), perspt_sdk::NodeClass::Implement)
        };
        perspt_sdk::WorkGraphRevision::build(
            1,
            None,
            perspt_sdk::GraphRevisionReason::InitialPlan,
            vec![node("a"), node("b"), node("z")],
            vec![perspt_sdk::WorkEdge::new(
                "a",
                "b",
                perspt_sdk::EdgeKind::RequiresArtifact,
            )],
        )
        .unwrap()
    }

    fn winner(content: &[u8]) -> StagedWinner {
        StagedWinner {
            state_root: format!("root-{}", perspt_sdk::ledger::content_hash(content)),
            files: vec![SeedFile {
                path: "src/shared.rs".into(),
                content: Some(content.to_vec()),
                source_preimage: None,
            }],
        }
    }

    /// The pairwise rule over the full writer set: with `a → b` and an
    /// unrelated sibling `z`, `a` writing X, `b` refining it to Y, and `z`
    /// independently writing Y must conflict on the incomparable divergent
    /// pair (a, z) — even though the retained-owner walk (a/b comparable,
    /// then b/z equal-content) sees nothing.
    #[test]
    fn a_superseded_upstream_write_still_conflicts_with_a_divergent_sibling() {
        let graph = graph_a_to_b_with_sibling_z();
        let mut staging = StagingRoot::default();
        staging.contributions.insert("a".into(), winner(b"X"));
        staging.contributions.insert("b".into(), winner(b"Y"));
        staging.contributions.insert("z".into(), winner(b"Y"));
        let conflict = staging.conflict(&graph).expect("a and z diverge");
        assert!(
            conflict.contains("a") && conflict.contains("z"),
            "the incomparable divergent pair is named: {conflict}"
        );
    }

    /// Refinement along an edge stays conflict-free, and an agreeing
    /// sibling does not manufacture one.
    #[test]
    fn comparable_refinement_and_agreeing_siblings_do_not_conflict() {
        let graph = graph_a_to_b_with_sibling_z();
        let mut staging = StagingRoot::default();
        staging.contributions.insert("a".into(), winner(b"X"));
        staging.contributions.insert("b".into(), winner(b"Y"));
        assert!(staging.conflict(&graph).is_none(), "a → b is a refinement");
        staging.contributions.insert("z".into(), winner(b"X"));
        let conflict = staging.conflict(&graph).expect("b and z diverge");
        assert!(conflict.contains("b") && conflict.contains("z"));
    }

    fn staging_row(generation: Option<u32>) -> serde_json::Value {
        let mut payload = serde_json::json!({
            "node_id": "a",
            "state_root": "root-a",
            "files": [],
        });
        if let Some(generation) = generation {
            payload["generation"] = generation.into();
        }
        payload
    }

    /// A pre-generation staging row superseded by a newer, generation-bound
    /// row for the same node must not refuse the fold (newest wins); a
    /// node whose newest row predates generation binding still fails
    /// closed.
    #[test]
    fn a_superseded_pre_generation_staging_row_folds_by_the_newer_row() {
        let database = tempfile::tempdir().unwrap();
        let recorder = Psp9Recorder::create(
            "fold-staging-test",
            "task",
            std::path::Path::new("."),
            Some(&database.path().join("fold-staging.db")),
            None,
            None,
        )
        .unwrap();
        let graph = graph_a_to_b_with_sibling_z();
        recorder
            .record_custom("staging_root_updated", staging_row(None))
            .unwrap();
        let refusal = match fold_staging(&recorder, "fold-staging-test", &graph) {
            Err(error) => error.to_string(),
            Ok(_) => panic!("an unsuperseded pre-generation row must refuse the fold"),
        };
        assert!(refusal.contains("generation-bound"), "{refusal}");
        recorder
            .record_custom("staging_root_updated", staging_row(Some(0)))
            .unwrap();
        let staging = fold_staging(&recorder, "fold-staging-test", &graph).unwrap();
        assert_eq!(
            staging
                .contributions
                .get("a")
                .map(|w| w.state_root.as_str()),
            Some("root-a"),
            "the newer generation-bound row supersedes the legacy one"
        );
    }
}