yoagent-state 0.3.0

Durable state and lineage for long-running agents.
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
use crate::{
    ActorRef, ArtifactRef, Decision, DecisionStatus, EvalResult, Event, EventId, EventStore,
    ForkId, ForkSnapshot, Frame, Goal, GoalId, GoalStatus, Graph, GraphDiff, GraphSnapshot,
    Hypothesis, Lineage, ModelCall, NodeId, Observation, PatchId, PatchStatus, ProjectSnapshot,
    RunId, StateError, StateOp, StatePatch, Task, TaskId, TaskStatus, ToolCall, diff_graphs,
    fork_events_at, project_event, replay,
};
use serde_json::json;
use std::sync::Arc;
use tokio::sync::RwLock;

#[derive(Debug)]
pub struct YoAgentState<S: EventStore> {
    store: Arc<S>,
    graph: Arc<RwLock<Graph>>,
    /// The open run, if any: its `RunId` and its `run.started` event id.
    /// Shared across clones — at most one run may be open per state handle
    /// and all its clones (enforced by `record_run_started` /
    /// `record_run_finished`). In-memory only: `load` does not recover an
    /// open run from the log, so a process restarted mid-run must start a
    /// new run before recording chained events.
    current_run: Arc<RwLock<Option<(RunId, EventId)>>>,
}

impl<S: EventStore> Clone for YoAgentState<S> {
    fn clone(&self) -> Self {
        Self {
            store: self.store.clone(),
            graph: self.graph.clone(),
            current_run: self.current_run.clone(),
        }
    }
}

impl<S: EventStore> YoAgentState<S> {
    pub async fn load(store: S) -> Result<Self, StateError> {
        let events = store.scan().await?;
        let graph = replay(&events)?;
        Ok(Self {
            store: Arc::new(store),
            graph: Arc::new(RwLock::new(graph)),
            current_run: Arc::new(RwLock::new(None)),
        })
    }

    pub fn store(&self) -> Arc<S> {
        self.store.clone()
    }

    /// Append one event. Events recorded without an explicit `causation_id`
    /// while a run is open (between `record_run_started` and
    /// `record_run_finished`) are chained to the run's start event. Provided
    /// all activity happens inside runs within a single process lifetime,
    /// this keeps the causation graph rooted at `*.created` / `*.started` —
    /// events recorded with no open run become roots of whatever kind they
    /// are, and the open-run marker is not recovered by `load`.
    pub async fn record_event(&self, mut event: Event) -> Result<EventId, StateError> {
        if event.causation_id.is_none() && event.kind != "run.started" {
            if let Some((_, run_event)) = self.current_run.read().await.clone() {
                event.causation_id = Some(run_event);
            }
        }
        let ids = self.store.append(vec![event.clone()]).await?;
        {
            let mut graph = self.graph.write().await;
            project_event(&mut graph, &event)?;
        }
        Ok(ids[0].clone())
    }

    pub async fn apply_ops(
        &self,
        actor: ActorRef,
        ops: Vec<StateOp>,
    ) -> Result<EventId, StateError> {
        self.apply_ops_caused_by(actor, ops, None).await
    }

    /// Append a `state.ops_applied` event carrying the domain event that caused
    /// it, per the GASP pairing rule: ops that materialize a domain event set
    /// `causation_id` to that event's id, keeping the audit layer and the
    /// folded graph mechanically linked.
    pub async fn apply_ops_caused_by(
        &self,
        actor: ActorRef,
        ops: Vec<StateOp>,
        caused_by: Option<EventId>,
    ) -> Result<EventId, StateError> {
        let mut event = Event::new(actor, crate::STATE_OPS_APPLIED, serde_json::to_value(ops)?);
        event.causation_id = caused_by;
        self.record_event(event).await
    }

    pub async fn propose_patch(&self, patch: StatePatch) -> Result<PatchId, StateError> {
        let patch_id = patch.id.clone();
        let actor = patch.created_by.clone();
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "patch.proposed",
                serde_json::to_value(&patch)?,
            ))
            .await?;

        let patch_node_id = NodeId::new(patch.id.0.clone());
        let mut ops = vec![StateOp::CreateNode {
            id: patch_node_id.clone(),
            kind: "patch".to_string(),
            props: json!({
                "title": patch.title,
                "summary": patch.summary,
                "status": patch.status,
                "base_state_version": patch.base_state_version,
                "preconditions": patch.preconditions,
                "expected_effects": patch.expected_effects,
                "base_project_ref": patch.base_project_ref,
            }),
        }];

        for evidence in patch.evidence {
            ops.push(StateOp::CreateRelation {
                from: patch_node_id.clone(),
                rel: "addresses".to_string(),
                to: evidence,
                props: json!({}),
            });
        }

        for artifact in patch.artifacts {
            ops.push(StateOp::AttachArtifact {
                id: patch_node_id.clone(),
                artifact,
            });
        }

        ops.extend(patch.ops);
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await?;
        Ok(patch_id)
    }

    pub async fn update_patch_status(
        &self,
        patch_id: PatchId,
        status: PatchStatus,
        reason: Option<String>,
    ) -> Result<EventId, StateError> {
        let actor = ActorRef::system("yoagent-state");
        let event = Event::new(
            actor.clone(),
            "patch.status_changed",
            json!({
                "patch_id": patch_id,
                "status": status,
                "reason": reason,
            }),
        );
        let caused_by = self.record_event(event).await?;

        self.apply_ops_caused_by(
            actor,
            vec![StateOp::UpdateNode {
                id: NodeId::new(patch_id.0),
                props: json!({
                    "status": status,
                    "status_reason": reason,
                }),
            }],
            Some(caused_by),
        )
        .await
    }

    pub async fn attach_artifact(
        &self,
        node_id: NodeId,
        artifact: ArtifactRef,
    ) -> Result<EventId, StateError> {
        let actor = ActorRef::system("yoagent-state");
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "artifact.attached",
                json!({
                    "node_id": node_id,
                    "artifact": artifact,
                }),
            ))
            .await?;

        self.apply_ops_caused_by(
            actor,
            vec![StateOp::AttachArtifact {
                id: node_id,
                artifact,
            }],
            Some(caused_by),
        )
        .await
    }

    pub async fn graph(&self) -> GraphSnapshot {
        self.graph.read().await.clone()
    }

    pub async fn get_node(&self, node_id: NodeId) -> Option<crate::Node> {
        self.graph.read().await.get_node(&node_id).cloned()
    }

    pub async fn outgoing(&self, node_id: NodeId, rel: Option<&str>) -> Vec<crate::Relation> {
        self.graph.read().await.outgoing(&node_id, rel)
    }

    pub async fn incoming(&self, node_id: NodeId, rel: Option<&str>) -> Vec<crate::Relation> {
        self.graph.read().await.incoming(&node_id, rel)
    }

    pub async fn related(&self, node_id: NodeId) -> Vec<crate::Relation> {
        self.graph.read().await.related(&node_id)
    }

    pub async fn patches_for_failure(&self, failure_id: NodeId) -> Vec<crate::Node> {
        let graph = self.graph.read().await;
        graph
            .incoming(&failure_id, Some("addresses"))
            .into_iter()
            .filter_map(|rel| graph.get_node(&rel.from).cloned())
            .filter(|node| node.kind == "patch")
            .collect()
    }

    pub async fn evals_for_patch(&self, patch_id: PatchId) -> Vec<crate::Node> {
        let patch_node_id = NodeId::new(patch_id.0);
        let graph = self.graph.read().await;
        graph
            .outgoing(&patch_node_id, Some("validated_by"))
            .into_iter()
            .filter_map(|rel| graph.get_node(&rel.to).cloned())
            .collect()
    }

    pub async fn lineage(&self, node_id: NodeId) -> Lineage {
        let graph = self.graph.read().await;
        Lineage::from_graph(&graph, &node_id)
    }

    pub async fn record_run_started(
        &self,
        actor: ActorRef,
        run_id: RunId,
        task: impl Into<String>,
    ) -> Result<EventId, StateError> {
        if let Some((open_run, _)) = self.current_run.read().await.as_ref() {
            return Err(StateError::Validation(format!(
                "run {open_run} is already open; finish it before starting {run_id}"
            )));
        }
        let task = task.into();
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "run.started",
                json!({ "run_id": run_id, "task": task }),
            ))
            .await?;
        let result = self
            .apply_ops_caused_by(
                actor,
                vec![StateOp::CreateNode {
                    id: NodeId::new(run_id.0.clone()),
                    kind: crate::KIND_RUN.to_string(),
                    props: json!({ "task": task, "status": "started" }),
                }],
                Some(caused_by.clone()),
            )
            .await?;
        // Only a fully-recorded run (domain event + ops pair) opens chaining;
        // a failed ops append must not leave events chaining to a phantom run.
        *self.current_run.write().await = Some((run_id, caused_by));
        Ok(result)
    }

    pub async fn record_run_finished(
        &self,
        actor: ActorRef,
        run_id: RunId,
        outcome: impl Into<String>,
    ) -> Result<EventId, StateError> {
        match self.current_run.read().await.as_ref() {
            Some((open_run, _)) if *open_run == run_id => {}
            Some((open_run, _)) => {
                return Err(StateError::Validation(format!(
                    "cannot finish {run_id}: the open run is {open_run}"
                )));
            }
            None => {
                return Err(StateError::Validation(format!(
                    "cannot finish {run_id}: no run is open"
                )));
            }
        }
        let event_id = self
            .record_event(Event::new(
                actor,
                "run.finished",
                json!({ "run_id": run_id, "outcome": outcome.into() }),
            ))
            .await?;
        *self.current_run.write().await = None;
        Ok(event_id)
    }

    pub async fn record_goal(&self, goal: Goal) -> Result<EventId, StateError> {
        let actor = goal.owner.clone();
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "goal.created",
                serde_json::to_value(&goal)?,
            ))
            .await?;
        self.apply_ops_caused_by(
            actor,
            vec![StateOp::CreateNode {
                id: NodeId::new(goal.id.0),
                kind: crate::KIND_GOAL.to_string(),
                props: json!({
                    "title": goal.title,
                    "summary": goal.summary,
                    "status": goal.status,
                    "owner": goal.owner,
                    "metadata": goal.metadata,
                }),
            }],
            Some(caused_by),
        )
        .await
    }

    pub async fn update_goal_status(
        &self,
        goal_id: GoalId,
        status: GoalStatus,
        reason: Option<String>,
    ) -> Result<EventId, StateError> {
        let actor = ActorRef::system("yoagent-state");
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "goal.status_changed",
                json!({ "goal_id": goal_id, "status": status, "reason": reason }),
            ))
            .await?;
        self.apply_ops_caused_by(
            actor,
            vec![StateOp::UpdateNode {
                id: NodeId::new(goal_id.0),
                props: json!({ "status": status, "status_reason": reason }),
            }],
            Some(caused_by),
        )
        .await
    }

    pub async fn record_task(&self, task: Task) -> Result<EventId, StateError> {
        let actor = task.created_by.clone();
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "task.created",
                serde_json::to_value(&task)?,
            ))
            .await?;
        let task_id = NodeId::new(task.id.0);
        let mut ops = vec![StateOp::CreateNode {
            id: task_id.clone(),
            kind: crate::KIND_TASK.to_string(),
            props: json!({
                "title": task.title,
                "summary": task.summary,
                "status": task.status,
                "metadata": task.metadata,
            }),
        }];
        if let Some(goal) = task.goal {
            ops.push(StateOp::CreateRelation {
                from: task_id,
                rel: crate::REL_SERVES.to_string(),
                to: NodeId::new(goal.0),
                props: json!({}),
            });
        }
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await
    }

    pub async fn update_task_status(
        &self,
        task_id: TaskId,
        status: TaskStatus,
        reason: Option<String>,
    ) -> Result<EventId, StateError> {
        let actor = ActorRef::system("yoagent-state");
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "task.status_changed",
                json!({ "task_id": task_id, "status": status, "reason": reason }),
            ))
            .await?;
        self.apply_ops_caused_by(
            actor,
            vec![StateOp::UpdateNode {
                id: NodeId::new(task_id.0),
                props: json!({ "status": status, "status_reason": reason }),
            }],
            Some(caused_by),
        )
        .await
    }

    pub async fn record_observation(
        &self,
        actor: ActorRef,
        observation: Observation,
    ) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "observation.created",
                serde_json::to_value(&observation)?,
            ))
            .await?;
        let observation_id = NodeId::new(observation.id.0);
        let mut ops = vec![StateOp::CreateNode {
            id: observation_id.clone(),
            kind: crate::KIND_OBSERVATION.to_string(),
            props: json!({
                "title": observation.title,
                "summary": observation.summary,
                "metadata": observation.metadata,
            }),
        }];
        if let Some(run_id) = observation.observed_in {
            ops.push(StateOp::CreateRelation {
                from: observation_id,
                rel: crate::REL_OBSERVES.to_string(),
                to: NodeId::new(run_id.0),
                props: json!({}),
            });
        }
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await
    }

    pub async fn record_hypothesis(
        &self,
        actor: ActorRef,
        hypothesis: Hypothesis,
        explains: Option<NodeId>,
    ) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "hypothesis.created",
                serde_json::to_value(&hypothesis)?,
            ))
            .await?;
        let hypothesis_id = NodeId::new(hypothesis.id.0);
        let mut ops = vec![StateOp::CreateNode {
            id: hypothesis_id.clone(),
            kind: crate::KIND_HYPOTHESIS.to_string(),
            props: json!({
                "title": hypothesis.title,
                "summary": hypothesis.summary,
                "confidence": hypothesis.confidence,
                "metadata": hypothesis.metadata,
            }),
        }];
        if let Some(target) = explains {
            ops.push(StateOp::CreateRelation {
                from: hypothesis_id,
                rel: crate::REL_EXPLAINS.to_string(),
                to: target,
                props: json!({}),
            });
        }
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await
    }

    pub async fn record_failure(
        &self,
        actor: ActorRef,
        failure_id: NodeId,
        title: impl Into<String>,
        summary: impl Into<String>,
    ) -> Result<EventId, StateError> {
        let title = title.into();
        let summary = summary.into();
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "failure.observed",
                json!({
                    "id": failure_id,
                    "failure_id": failure_id,
                    "title": title,
                    "summary": summary,
                }),
            ))
            .await?;
        self.apply_ops_caused_by(
            actor,
            vec![StateOp::CreateNode {
                id: failure_id,
                kind: "failure".to_string(),
                props: json!({ "title": title, "summary": summary }),
            }],
            Some(caused_by),
        )
        .await
    }

    pub async fn record_eval_result(
        &self,
        actor: ActorRef,
        eval_id: NodeId,
        patch_id: PatchId,
        command: impl Into<String>,
        passed: bool,
    ) -> Result<EventId, StateError> {
        let patch_node_id = NodeId::new(patch_id.0);
        self.apply_ops(
            actor,
            vec![
                StateOp::CreateNode {
                    id: eval_id.clone(),
                    kind: "eval".to_string(),
                    props: json!({ "command": command.into(), "passed": passed }),
                },
                StateOp::CreateRelation {
                    from: patch_node_id,
                    rel: "validated_by".to_string(),
                    to: eval_id,
                    props: json!({ "passed": passed }),
                },
            ],
        )
        .await
    }

    pub async fn record_eval(
        &self,
        actor: ActorRef,
        eval: EvalResult,
        patch_id: Option<PatchId>,
    ) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "eval.finished",
                serde_json::to_value(&eval)?,
            ))
            .await?;
        let eval_node_id = NodeId::new(eval.id.0);
        let mut ops = vec![StateOp::CreateNode {
            id: eval_node_id.clone(),
            kind: crate::KIND_EVAL.to_string(),
            props: json!({
                "command": eval.command,
                "status": eval.status,
                "score": eval.score,
                "metadata": eval.metadata,
            }),
        }];
        if let Some(patch_id) = patch_id {
            ops.push(StateOp::CreateRelation {
                from: NodeId::new(patch_id.0),
                rel: crate::REL_VALIDATED_BY.to_string(),
                to: eval_node_id,
                props: json!({}),
            });
        }
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await
    }

    pub async fn record_decision(
        &self,
        actor: ActorRef,
        decision_id: NodeId,
        patch_id: PatchId,
        approved: bool,
        reason: impl Into<String>,
    ) -> Result<EventId, StateError> {
        let patch_node_id = NodeId::new(patch_id.0);
        let rel = if approved {
            "approved_by"
        } else {
            "rejected_by"
        };

        self.apply_ops(
            actor,
            vec![
                StateOp::CreateNode {
                    id: decision_id.clone(),
                    kind: "decision".to_string(),
                    props: json!({ "approved": approved, "reason": reason.into() }),
                },
                StateOp::CreateRelation {
                    from: patch_node_id,
                    rel: rel.to_string(),
                    to: decision_id,
                    props: json!({}),
                },
            ],
        )
        .await
    }

    pub async fn record_decision_node(
        &self,
        actor: ActorRef,
        decision: Decision,
        target: Option<NodeId>,
    ) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "decision.created",
                serde_json::to_value(&decision)?,
            ))
            .await?;
        let decision_node_id = NodeId::new(decision.id.0);
        let mut ops = vec![StateOp::CreateNode {
            id: decision_node_id.clone(),
            kind: crate::KIND_DECISION.to_string(),
            props: json!({
                "status": decision.status,
                "reason": decision.reason,
                "decided_by": decision.decided_by,
                "metadata": decision.metadata,
            }),
        }];
        if let Some(target) = target {
            let rel = match decision.status {
                DecisionStatus::Approved => crate::REL_APPROVED_BY,
                DecisionStatus::Rejected => crate::REL_REJECTED_BY,
                _ => crate::REL_REFERENCES,
            };
            ops.push(StateOp::CreateRelation {
                from: target,
                rel: rel.to_string(),
                to: decision_node_id,
                props: json!({}),
            });
        }
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await
    }

    pub async fn record_project_snapshot(
        &self,
        actor: ActorRef,
        snapshot: ProjectSnapshot,
    ) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "project.snapshot_recorded",
                serde_json::to_value(&snapshot)?,
            ))
            .await?;
        let mut ops = vec![StateOp::CreateNode {
            id: snapshot.id.clone(),
            kind: crate::KIND_PROJECT_SNAPSHOT.to_string(),
            props: json!({
                "project": snapshot.project,
                "metadata": snapshot.metadata,
            }),
        }];
        for artifact in snapshot.artifacts {
            ops.push(StateOp::AttachArtifact {
                id: snapshot.id.clone(),
                artifact,
            });
        }
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await
    }

    pub async fn record_model_call(
        &self,
        actor: ActorRef,
        call: ModelCall,
    ) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "model.called",
                serde_json::to_value(&call)?,
            ))
            .await?;
        self.apply_ops_caused_by(
            actor,
            vec![
                StateOp::CreateNode {
                    id: call.id.clone(),
                    kind: crate::KIND_MODEL_CALL.to_string(),
                    props: json!({
                        "run_id": call.run_id,
                        "model": call.model,
                        "prompt_summary": call.prompt_summary,
                        "output_summary": call.output_summary,
                        "metadata": call.metadata,
                    }),
                },
                StateOp::CreateRelation {
                    from: call.id,
                    rel: crate::REL_PRODUCED_BY.to_string(),
                    to: NodeId::new(call.run_id.0),
                    props: json!({}),
                },
            ],
            Some(caused_by),
        )
        .await
    }

    pub async fn record_tool_call(
        &self,
        actor: ActorRef,
        call: ToolCall,
    ) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "tool.called",
                serde_json::to_value(&call)?,
            ))
            .await?;
        self.apply_ops_caused_by(
            actor,
            vec![
                StateOp::CreateNode {
                    id: call.id.clone(),
                    kind: crate::KIND_TOOL_CALL.to_string(),
                    props: json!({
                        "run_id": call.run_id,
                        "tool": call.tool,
                        "input_summary": call.input_summary,
                        "output_summary": call.output_summary,
                        "success": call.success,
                        "metadata": call.metadata,
                    }),
                },
                StateOp::CreateRelation {
                    from: call.id,
                    rel: crate::REL_PRODUCED_BY.to_string(),
                    to: NodeId::new(call.run_id.0),
                    props: json!({}),
                },
            ],
            Some(caused_by),
        )
        .await
    }

    pub async fn record_frame(&self, actor: ActorRef, frame: Frame) -> Result<EventId, StateError> {
        let caused_by = self
            .record_event(Event::new(
                actor.clone(),
                "frame.created",
                serde_json::to_value(&frame)?,
            ))
            .await?;
        let frame_node_id = NodeId::new(frame.id.0);
        let mut ops = vec![StateOp::CreateNode {
            id: frame_node_id.clone(),
            kind: crate::KIND_FRAME.to_string(),
            props: json!({
                "title": frame.title,
                "summary": frame.summary,
                "metadata": frame.metadata,
            }),
        }];
        if let Some(parent) = frame.parent {
            ops.push(StateOp::CreateRelation {
                from: frame_node_id,
                rel: crate::REL_CONTAINED_IN_FRAME.to_string(),
                to: NodeId::new(parent.0),
                props: json!({}),
            });
        }
        self.apply_ops_caused_by(actor, ops, Some(caused_by)).await
    }

    pub async fn link(
        &self,
        actor: ActorRef,
        from: NodeId,
        rel: impl Into<String>,
        to: NodeId,
    ) -> Result<EventId, StateError> {
        self.apply_ops(
            actor,
            vec![StateOp::CreateRelation {
                from,
                rel: rel.into(),
                to,
                props: json!({}),
            }],
        )
        .await
    }

    pub async fn fork_at_event(
        &self,
        fork_id: ForkId,
        parent_event: Option<EventId>,
    ) -> Result<ForkSnapshot, StateError> {
        let events = self.store.scan().await?;
        fork_events_at(&events, fork_id, parent_event)
    }

    pub async fn diff_with(&self, other: &Graph) -> GraphDiff {
        let current = self.graph().await;
        diff_graphs(&current, other)
    }
}