tirea_contract/runtime/
activity.rs

1//! Activity management trait for external state updates.
2
3use serde_json::Value;
4use std::sync::Arc;
5use tirea_state::Op;
6
7/// Manager for activity state updates.
8///
9/// Implementations keep per-stream activity state and may emit external events.
10pub trait ActivityManager: Send + Sync {
11    /// Get the current activity snapshot for a stream.
12    fn snapshot(&self, stream_id: &str) -> Value;
13
14    /// Handle an activity operation for a stream.
15    fn on_activity_op(&self, stream_id: &str, activity_type: &str, op: &Op);
16}
17
18/// No-op activity manager that silently discards all operations.
19pub struct NoOpActivityManager;
20
21impl ActivityManager for NoOpActivityManager {
22    fn snapshot(&self, _stream_id: &str) -> Value {
23        Value::Object(Default::default())
24    }
25
26    fn on_activity_op(&self, _stream_id: &str, _activity_type: &str, _op: &Op) {}
27}
28
29impl NoOpActivityManager {
30    /// Create a shared no-op activity manager.
31    pub fn arc() -> Arc<dyn ActivityManager> {
32        Arc::new(Self)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use crate::testing::{TestFixture, TestFixtureState};
39    use serde_json::json;
40
41    // Write-through same-ref and cross-ref covered by tool_call::context::tests.
42
43    #[test]
44    fn test_rebuild_state_reflects_write_through() {
45        let doc = json!({"__test_fixture": {"label": null}});
46        let fix = TestFixture::new_with_state(doc);
47        let ctx = fix.ctx_with("call-1", "test");
48
49        // Write via state_of
50        let ctrl = ctx.state_of::<TestFixtureState>();
51        ctrl.set_label(Some("test_value".into()))
52            .expect("failed to set label");
53
54        // updated_state should return the run_doc snapshot which includes the write
55        let rebuilt = fix.updated_state();
56        assert_eq!(
57            rebuilt["__test_fixture"]["label"], "test_value",
58            "updated_state must reflect write-through updates"
59        );
60    }
61}