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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Write-ahead model context (Gate O): the loop's conversation is a pure
//! fold over recorded conversation deltas.
//!
//! Every context change — seed, message, tool activation, compaction — is
//! prepared as a digest-chained [`ConversationDeltaRecord`], emitted to the
//! ledger, and only then applied through
//! [`ConversationProjection::apply`], the sole writer. "Conversation is
//! rebuilt for each model turn from the immutable ledger" is therefore
//! structurally true: a from-scratch refold over the recorded deltas is the
//! mechanism check, and resume refuses a checkpoint whose conversation is
//! not derivable from the ledger.

use std::collections::BTreeSet;

use anyhow::{Context as _, Result};
use perspt_sdk::{
    Conversation, ConversationDelta, ConversationProjection, ConversationSeeded, LedgerEvent,
    Message, ProviderToolCall,
};

use super::{emit, EventLog, LoopEvent, LoopRecorder};

/// The loop's sole owner of model context and deferred tool activation.
pub(crate) struct LoopContext {
    projection: ConversationProjection,
    /// The birth dependency of each message (parallel to the conversation):
    /// the state the page was created under. Resident assembly stamps pages
    /// from this log instead of relabelling them with the live root, so a
    /// state change invalidates stale optional pages rather than silently
    /// rewriting their provenance (Gate AF, spec :1438-1444).
    birth_deps: Vec<perspt_sdk::prompt::StateDependency>,
    current_dep: perspt_sdk::prompt::StateDependency,
}

impl LoopContext {
    /// Seed a fresh context (system prompt + goal), recording the seed.
    pub(crate) fn seed(
        system: &str,
        goal: &str,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<Self> {
        let mut conversation = Conversation::with_system(system);
        conversation.push_user(goal.to_string());
        Self::from_conversation(conversation, Vec::new(), recorder, log)
    }

    /// Re-enter from a durable checkpoint's conversation clone. The clone
    /// was verified against the ledger fold before this call; re-seeding
    /// records the resumed context as the new fold base.
    pub(crate) fn resume(
        conversation: Conversation,
        restored_activated_tools: Vec<String>,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<Self> {
        Self::from_conversation(conversation, restored_activated_tools, recorder, log)
    }

    fn from_conversation(
        conversation: Conversation,
        activated: Vec<String>,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<Self> {
        let seed = ConversationSeeded::new(conversation).map_err(|e| anyhow::anyhow!("{e}"))?;
        emit(
            recorder,
            log,
            LoopEvent::ConversationSeeded { seed: seed.clone() },
        )?;
        let projection =
            ConversationProjection::from_seed(&seed).map_err(|e| anyhow::anyhow!("{e}"))?;
        // Seed messages (instructions, the task) are session-invariant;
        // the loop switches the current dependency to the live root once
        // the baseline checkpoint exists.
        let initial = perspt_sdk::prompt::StateDependency::SessionInvariant;
        let birth_deps = vec![initial.clone(); projection.conversation().messages().len()];
        let mut context = Self {
            projection,
            birth_deps,
            current_dep: initial,
        };
        for name in activated {
            context.activate_tool(&name, recorder, log)?;
        }
        Ok(context)
    }

    /// Set the dependency stamped onto messages appended from now on —
    /// called at the baseline and after every accepted checkpoint.
    pub(crate) fn set_state_dependency(&mut self, dep: perspt_sdk::prompt::StateDependency) {
        self.current_dep = dep;
    }

    /// Stamp the context after the loop opens it: pages appended from here
    /// depend on the live state (the partial seed root under a continuing
    /// branch, the accepted root otherwise). A resumed projection restores
    /// each page's **recorded birth dependency** when the checkpoint
    /// carried one — never a wholesale relabel with the live root, which
    /// would launder stale pages as fresh (Gate AF) and turn the
    /// session-invariant head into a page the next acceptance kills.
    pub(crate) fn stamp_after_open(
        &mut self,
        was_resumed: bool,
        partial_seed_root: Option<&str>,
        accepted_root: &str,
        restored_birth_deps: &[perspt_sdk::prompt::StateDependency],
    ) {
        let dep = match partial_seed_root {
            Some(root) => perspt_sdk::prompt::StateDependency::PartialCheckpointRoot(root.into()),
            None => perspt_sdk::prompt::StateDependency::AcceptedRoot(accepted_root.into()),
        };
        if !was_resumed {
            self.set_state_dependency(dep);
            return;
        }
        if restored_birth_deps.len() == self.birth_deps.len() {
            // Exact provenance from the durable checkpoint: pages keep the
            // roots they were born under; new pages depend on the live
            // state.
            self.birth_deps = restored_birth_deps.to_vec();
            self.current_dep = dep;
        } else {
            self.restamp_legacy(dep);
        }
    }

    /// Legacy fallback for pre-provenance checkpoints: the instruction and
    /// task head is session-invariant (a later acceptance must never stale
    /// the mandatory head); every remaining restored page is deliberately
    /// stale because its true birth root is unknowable. Relabelling those
    /// pages with the current root would launder stale evidence as fresh.
    /// The one exception is an unresolved tool-call pair: it is pinned
    /// mandatory unconditionally, and a mandatory page whose dependency no
    /// longer holds refuses assembly outright (Gate AF) — so its messages
    /// carry the live dependency instead of an unresumable marker.
    pub(crate) fn restamp_legacy(&mut self, dep: perspt_sdk::prompt::StateDependency) {
        let open_pair = {
            let unresolved: BTreeSet<String> = self
                .projection
                .conversation()
                .unresolved_call_ids()
                .into_iter()
                .collect();
            let messages = self.projection.conversation().messages();
            let mut open_pair = vec![false; messages.len()];
            let mut open_calls: BTreeSet<String> = BTreeSet::new();
            for (index, message) in messages.iter().enumerate() {
                match message {
                    Message::AssistantToolCalls { calls }
                        if calls.iter().any(|call| unresolved.contains(&call.call_id)) =>
                    {
                        open_pair[index] = true;
                        open_calls.extend(calls.iter().map(|call| call.call_id.clone()));
                    }
                    Message::ToolResponse { call_id, .. } if open_calls.contains(call_id) => {
                        open_pair[index] = true;
                    }
                    _ => {}
                }
            }
            open_pair
        };
        for (index, entry) in self.birth_deps.iter_mut().enumerate() {
            *entry = if index < 2 {
                perspt_sdk::prompt::StateDependency::SessionInvariant
            } else if open_pair.get(index).copied().unwrap_or(false) {
                dep.clone()
            } else {
                perspt_sdk::prompt::StateDependency::AcceptedRoot(
                    "legacy-unavailable-provenance".into(),
                )
            };
        }
        self.current_dep = dep;
    }

    /// The birth dependency per message, parallel to `conversation()`.
    pub(crate) fn birth_deps(&self) -> &[perspt_sdk::prompt::StateDependency] {
        &self.birth_deps
    }

    /// Keep the birth log parallel to the projection after a delta (a
    /// compaction may replace messages; replacements carry the current
    /// dependency).
    fn sync_birth_deps(&mut self) {
        let len = self.projection.conversation().messages().len();
        while self.birth_deps.len() < len {
            self.birth_deps.push(self.current_dep.clone());
        }
        self.birth_deps.truncate(len);
    }

    /// Write-ahead by construction: the delta record is ledgered before the
    /// projection applies it.
    fn record(
        &mut self,
        delta: ConversationDelta,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<()> {
        let record = self
            .projection
            .prepare(delta)
            .map_err(|e| anyhow::anyhow!("{e}"))?;
        emit(
            recorder,
            log,
            LoopEvent::ConversationDelta {
                record: record.clone(),
            },
        )?;
        self.projection
            .apply(&record)
            .map_err(|e| anyhow::anyhow!("{e}"))?;
        self.sync_birth_deps();
        Ok(())
    }

    pub(crate) fn push_message(
        &mut self,
        message: Message,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<()> {
        self.record(ConversationDelta::Message { message }, recorder, log)
    }

    pub(crate) fn push_user(
        &mut self,
        content: impl Into<String>,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<()> {
        self.push_message(
            Message::User {
                content: content.into(),
            },
            recorder,
            log,
        )
    }

    pub(crate) fn push_tool_calls(
        &mut self,
        calls: Vec<ProviderToolCall>,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<()> {
        self.push_message(Message::AssistantToolCalls { calls }, recorder, log)
    }

    pub(crate) fn push_tool_response(
        &mut self,
        call_id: impl Into<String>,
        content: impl Into<String>,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<()> {
        self.push_message(
            Message::ToolResponse {
                call_id: call_id.into(),
                content: content.into(),
            },
            recorder,
            log,
        )
    }

    pub(crate) fn activate_tool(
        &mut self,
        name: &str,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<()> {
        if self.projection.activated_tools().contains(name) {
            return Ok(());
        }
        self.record(
            ConversationDelta::ToolActivated { name: name.into() },
            recorder,
            log,
        )
    }

    pub(crate) fn compact(
        &mut self,
        control: String,
        recorder: Option<&dyn LoopRecorder>,
        log: &mut EventLog,
    ) -> Result<()> {
        self.record(ConversationDelta::Compacted { control }, recorder, log)
    }

    pub(crate) fn conversation(&self) -> &Conversation {
        self.projection.conversation()
    }

    pub(crate) fn activated_tools(&self) -> &BTreeSet<String> {
        self.projection.activated_tools()
    }

    pub(crate) fn digest(&self) -> &str {
        self.projection.digest()
    }
}

/// Refold the model context recorded in a session's ledger rows, returning
/// the fold state whose rolling digest matches `target_digest` (deltas may
/// continue past a checkpoint before an interruption). `None` when no fold
/// state reaches the target — either the session predates conversation
/// deltas or the checkpoint is not derivable from the ledger.
pub(crate) fn refold_session_context(
    rows: &[perspt_store::Psp9LedgerRow],
    target_digest: &str,
) -> Result<Option<ConversationProjection>> {
    // Concurrent node loops (and search branches) interleave their
    // conversation events on the one session ledger, so the fold routes
    // by **digest chain** instead of holding a single live projection:
    // collect every seed and delta record, walk the target's parent chain
    // back to its seed, and replay that one chain forward through the
    // sole writer (`apply`, which re-verifies digest continuity). A
    // parallel graph's ledger is then exactly as refoldable as a serial
    // one.
    let mut seeds: std::collections::BTreeMap<String, perspt_sdk::ConversationSeeded> =
        Default::default();
    let mut records: std::collections::BTreeMap<String, perspt_sdk::ConversationDeltaRecord> =
        Default::default();
    for row in rows {
        let Ok(LedgerEvent::Custom { kind, payload }) = serde_json::from_str(&row.event_json)
        else {
            continue;
        };
        if kind != "tool_loop" {
            continue;
        }
        // Authoritative refold: unknown envelope versions fail closed
        // (Gate AD), so a checkpoint can never be validated against a
        // stream this build cannot fully read.
        let decoded = super::envelope::decode_tool_loop(&payload)?;
        match decoded.event {
            LoopEvent::ConversationSeeded { seed } => {
                seeds.insert(seed.digest.clone(), seed);
            }
            LoopEvent::ConversationDelta { record } => {
                records.insert(record.digest.clone(), record);
            }
            _ => {}
        }
    }
    if !seeds.contains_key(target_digest) && !records.contains_key(target_digest) {
        // The session predates conversation deltas, or the checkpoint is
        // not derivable — the caller refuses resume either way.
        return Ok(None);
    }
    let mut chain: Vec<&perspt_sdk::ConversationDeltaRecord> = Vec::new();
    let mut cursor = target_digest.to_string();
    while !seeds.contains_key(&cursor) {
        let record = records.get(&cursor).with_context(|| {
            format!("conversation chain to {target_digest} is broken at {cursor}")
        })?;
        chain.push(record);
        cursor = record.prior_digest.clone();
        anyhow::ensure!(
            chain.len() <= records.len(),
            "conversation delta chain contains a cycle"
        );
    }
    let mut projection =
        ConversationProjection::from_seed(&seeds[&cursor]).map_err(|e| anyhow::anyhow!("{e}"))?;
    for record in chain.iter().rev() {
        projection
            .apply(record)
            .map_err(|e| anyhow::anyhow!("{e}"))?;
    }
    anyhow::ensure!(
        projection.digest() == target_digest,
        "refolded conversation does not reproduce the checkpoint digest"
    );
    Ok(Some(projection))
}

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

    fn rows_from(events: Vec<LoopEvent>) -> Vec<perspt_store::Psp9LedgerRow> {
        events
            .into_iter()
            .enumerate()
            .map(|(sequence, event)| perspt_store::Psp9LedgerRow {
                session_id: "s".into(),
                sequence: sequence as i64,
                event_json: serde_json::to_string(&LedgerEvent::Custom {
                    kind: "tool_loop".into(),
                    payload: serde_json::to_value(&event).unwrap(),
                })
                .unwrap(),
                prev_hash: String::new(),
                hash: String::new(),
            })
            .collect()
    }

    #[test]
    fn ledger_fold_reproduces_the_live_context_exactly() {
        let mut log = EventLog::new(true);
        let mut context = LoopContext::seed("system", "goal", None, &mut log).unwrap();
        context
            .push_tool_calls(
                vec![ProviderToolCall {
                    call_id: "c1".into(),
                    name: "read_file".into(),
                    arguments: serde_json::json!({"path": "a.rs"}),
                }],
                None,
                &mut log,
            )
            .unwrap();
        context
            .push_tool_response("c1", "contents", None, &mut log)
            .unwrap();
        context.activate_tool("lsp_query", None, &mut log).unwrap();
        let digest = context.digest().to_string();

        let rows = rows_from(log.into_events());
        let folded = refold_session_context(&rows, &digest).unwrap().unwrap();
        assert_eq!(folded.conversation(), context.conversation());
        assert_eq!(folded.activated_tools(), context.activated_tools());
    }

    #[test]
    fn a_removed_delta_makes_the_checkpoint_unreachable() {
        let mut log = EventLog::new(true);
        let mut context = LoopContext::seed("system", "goal", None, &mut log).unwrap();
        context.push_user("first", None, &mut log).unwrap();
        context.push_user("second", None, &mut log).unwrap();
        let digest = context.digest().to_string();

        let mut rows = rows_from(log.into_events());
        rows.remove(1);
        assert!(
            refold_session_context(&rows, &digest).is_err(),
            "a delta with a missing parent must fail closed, not skip"
        );
    }

    /// Concurrent node loops interleave conversation events on one
    /// ledger (A seed → B seed → A delta → B delta → A delta). The
    /// digest-chain fold reaches BOTH checkpoints; a single-live-projection
    /// fold would apply A's delta to B's projection and refuse the whole
    /// session.
    #[test]
    fn interleaved_parallel_conversations_refold_by_digest_chain() {
        let mut log_a = EventLog::new(true);
        let mut a = LoopContext::seed("system", "goal A", None, &mut log_a).unwrap();
        let mut log_b = EventLog::new(true);
        let mut b = LoopContext::seed("system", "goal B", None, &mut log_b).unwrap();
        a.push_user("a-one", None, &mut log_a).unwrap();
        b.push_user("b-one", None, &mut log_b).unwrap();
        a.push_user("a-two", None, &mut log_a).unwrap();
        let digest_a = a.digest().to_string();
        let digest_b = b.digest().to_string();

        let events_a = log_a.into_events();
        let events_b = log_b.into_events();
        // Ledger order: A seed, B seed, A delta, B delta, A delta.
        let interleaved = vec![
            events_a[0].clone(),
            events_b[0].clone(),
            events_a[1].clone(),
            events_b[1].clone(),
            events_a[2].clone(),
        ];
        let rows = rows_from(interleaved);

        let folded_a = refold_session_context(&rows, &digest_a)
            .unwrap()
            .expect("A's chain folds");
        assert_eq!(folded_a.conversation(), a.conversation());
        let folded_b = refold_session_context(&rows, &digest_b)
            .unwrap()
            .expect("B's chain folds");
        assert_eq!(folded_b.conversation(), b.conversation());
    }

    #[test]
    fn legacy_resume_never_launders_unknown_pages_as_current() {
        let mut log = EventLog::new(true);
        let mut context = LoopContext::seed("system", "goal", None, &mut log).unwrap();
        context
            .push_user("old observation", None, &mut log)
            .unwrap();
        context.stamp_after_open(true, None, "live-root", &[]);

        assert_eq!(
            &context.birth_deps()[..2],
            &[
                perspt_sdk::prompt::StateDependency::SessionInvariant,
                perspt_sdk::prompt::StateDependency::SessionInvariant,
            ]
        );
        assert_eq!(
            context.birth_deps()[2],
            perspt_sdk::prompt::StateDependency::AcceptedRoot(
                "legacy-unavailable-provenance".into()
            )
        );
        assert_ne!(
            context.birth_deps()[2],
            perspt_sdk::prompt::StateDependency::AcceptedRoot("live-root".into())
        );
    }

    /// A legacy checkpoint with an unresolved tool call must stay
    /// resumable: the unresolved pair is pinned mandatory unconditionally,
    /// and a mandatory page whose dependency never holds would refuse
    /// every assembly (Gate AF) — so exactly those messages carry the live
    /// dependency while other unknown-provenance pages stay demoted.
    #[test]
    fn legacy_resume_keeps_the_unresolved_pair_assemblable() {
        let mut log = EventLog::new(true);
        let mut context = LoopContext::seed("system", "goal", None, &mut log).unwrap();
        context
            .push_user("old observation", None, &mut log)
            .unwrap();
        context
            .push_tool_calls(
                vec![ProviderToolCall {
                    call_id: "c1".into(),
                    name: "read_file".into(),
                    arguments: serde_json::json!({"path": "a.rs"}),
                }],
                None,
                &mut log,
            )
            .unwrap();
        context.stamp_after_open(true, None, "live-root", &[]);

        let deps = context.birth_deps();
        assert_eq!(
            deps[2],
            perspt_sdk::prompt::StateDependency::AcceptedRoot(
                "legacy-unavailable-provenance".into()
            )
        );
        assert_eq!(
            deps[3],
            perspt_sdk::prompt::StateDependency::AcceptedRoot("live-root".into()),
            "the unresolved tool-call page must depend on a state that holds"
        );
    }

    #[test]
    fn compaction_folds_to_the_exact_compacted_context() {
        let mut log = EventLog::new(true);
        let mut context = LoopContext::seed("system", "goal", None, &mut log).unwrap();
        context.push_user("progress", None, &mut log).unwrap();
        context
            .compact("PERSPECTIVE_CONTROL_FRAME_V1\n{}".into(), None, &mut log)
            .unwrap();
        let digest = context.digest().to_string();

        let rows = rows_from(log.into_events());
        let folded = refold_session_context(&rows, &digest).unwrap().unwrap();
        assert_eq!(folded.conversation(), context.conversation());
    }
}