tirea-contract 0.5.0

Agent runtime contracts: 8-phase plugin lifecycle, typed tool traits, and state scope system
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
use crate::runtime::inference::response::{InferenceError, LLMResponse, StreamResult};
use crate::runtime::phase::step::StepContext;
use crate::runtime::phase::Phase;
use crate::runtime::phase::{
    ActionSet, AfterInferenceAction, AfterToolExecuteAction, BeforeInferenceAction,
    BeforeToolExecuteAction, LifecycleAction,
};
use crate::runtime::run::config::AgentRunConfig;
use crate::runtime::run::RunIdentity;
use crate::runtime::state::StateScopeRegistry;
use crate::runtime::state::{ScopeContext, StateActionDeserializerRegistry, StateScope, StateSpec};
use crate::runtime::tool_call::{ToolCallResume, ToolResult};
use crate::thread::Message;
use crate::RunPolicy;
use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;
use tirea_state::{get_at_path, parse_path, DocCell, LatticeRegistry, State, TireaResult};

/// Immutable snapshot of step context passed to [`AgentBehavior`] phase hooks.
///
/// The loop builds a `ReadOnlyContext` from the current `StepContext` before
/// each phase hook and passes it by shared reference. Plugins read data from
/// this snapshot and return a typed `ActionSet` describing effects to apply.
pub struct ReadOnlyContext<'a> {
    phase: Phase,
    thread_id: &'a str,
    messages: &'a [Arc<Message>],
    run_config: Arc<AgentRunConfig>,
    run_identity: RunIdentity,
    doc: &'a DocCell,
    llm_response: Option<&'a LLMResponse>,
    tool_name: Option<&'a str>,
    tool_call_id: Option<&'a str>,
    tool_args: Option<&'a Value>,
    tool_result: Option<&'a ToolResult>,
    resume_input: Option<ToolCallResume>,
    scope_ctx: ScopeContext,
    initial_message_count: usize,
}

impl<'a> ReadOnlyContext<'a> {
    /// Backward-compatible constructor. Wraps `RunPolicy` in a default `AgentRunConfig`.
    pub fn new(
        phase: Phase,
        thread_id: &'a str,
        messages: &'a [Arc<Message>],
        run_policy: &RunPolicy,
        doc: &'a DocCell,
    ) -> Self {
        Self::with_run_config(
            phase,
            thread_id,
            messages,
            Arc::new(AgentRunConfig::new(run_policy.clone())),
            doc,
        )
    }

    /// Config-aware constructor for production paths.
    pub fn with_run_config(
        phase: Phase,
        thread_id: &'a str,
        messages: &'a [Arc<Message>],
        run_config: Arc<AgentRunConfig>,
        doc: &'a DocCell,
    ) -> Self {
        Self {
            phase,
            thread_id,
            messages,
            run_config,
            run_identity: RunIdentity::default(),
            doc,
            llm_response: None,
            tool_name: None,
            tool_call_id: None,
            tool_args: None,
            tool_result: None,
            resume_input: None,
            scope_ctx: ScopeContext::run(),
            initial_message_count: 0,
        }
    }

    #[must_use]
    pub fn with_llm_response(mut self, response: &'a LLMResponse) -> Self {
        self.llm_response = Some(response);
        self
    }

    #[must_use]
    pub fn with_tool_info(
        mut self,
        name: &'a str,
        call_id: &'a str,
        args: Option<&'a Value>,
    ) -> Self {
        self.tool_name = Some(name);
        self.tool_call_id = Some(call_id);
        self.tool_args = args;
        self
    }

    #[must_use]
    pub fn with_tool_result(mut self, result: &'a ToolResult) -> Self {
        self.tool_result = Some(result);
        self
    }

    #[must_use]
    pub fn with_resume_input(mut self, resume: ToolCallResume) -> Self {
        self.resume_input = Some(resume);
        self
    }

    #[must_use]
    pub fn with_scope_ctx(mut self, scope_ctx: ScopeContext) -> Self {
        self.scope_ctx = scope_ctx;
        self
    }

    pub fn phase(&self) -> Phase {
        self.phase
    }

    pub fn thread_id(&self) -> &str {
        self.thread_id
    }

    pub fn messages(&self) -> &[Arc<Message>] {
        self.messages
    }

    /// Number of messages that existed before the current run started.
    pub fn initial_message_count(&self) -> usize {
        self.initial_message_count
    }

    /// Layered runtime configuration.
    pub fn run_config(&self) -> &AgentRunConfig {
        &self.run_config
    }

    /// Backward-compatible accessor — delegates to `run_config().policy()`.
    pub fn run_policy(&self) -> &RunPolicy {
        self.run_config.policy()
    }

    pub fn run_identity(&self) -> &RunIdentity {
        &self.run_identity
    }

    pub fn doc(&self) -> &DocCell {
        self.doc
    }

    pub fn response(&self) -> Option<&StreamResult> {
        self.llm_response.and_then(|r| r.outcome.as_ref().ok())
    }

    pub fn inference_error(&self) -> Option<&InferenceError> {
        self.llm_response.and_then(|r| r.outcome.as_ref().err())
    }

    pub fn tool_name(&self) -> Option<&str> {
        self.tool_name
    }

    pub fn tool_call_id(&self) -> Option<&str> {
        self.tool_call_id
    }

    pub fn tool_args(&self) -> Option<&Value> {
        self.tool_args
    }

    pub fn tool_result(&self) -> Option<&ToolResult> {
        self.tool_result
    }

    pub fn resume_input(&self) -> Option<&ToolCallResume> {
        self.resume_input.as_ref()
    }

    pub fn snapshot(&self) -> Value {
        self.doc.snapshot()
    }

    pub fn snapshot_of<T: State>(&self) -> TireaResult<T> {
        let val = self.doc.snapshot();
        let at = get_at_path(&val, &parse_path(T::PATH)).unwrap_or(&Value::Null);
        T::from_value(at)
    }

    /// Scope-aware state read for `StateSpec` types.
    pub fn scoped_state_of<T: StateSpec>(&self, scope: StateScope) -> TireaResult<T> {
        let path = self.scope_ctx.resolve_path(scope, T::PATH);
        let val = self.doc.snapshot();
        let at = get_at_path(&val, &parse_path(&path)).unwrap_or(&Value::Null);
        T::from_value(at).or_else(|e| {
            if at.is_null() {
                T::from_value(&Value::Object(Default::default())).map_err(|_| e)
            } else {
                Err(e)
            }
        })
    }

    pub fn scope_ctx(&self) -> &ScopeContext {
        &self.scope_ctx
    }

    #[must_use]
    pub fn with_run_identity(mut self, run_identity: &RunIdentity) -> Self {
        self.run_identity = run_identity.clone();
        self
    }
}

/// Declarative execution ordering constraints for a plugin.
#[derive(Debug, Clone, Default)]
pub struct PluginOrdering {
    /// This plugin's phase hooks execute AFTER the listed plugin IDs.
    pub after: &'static [&'static str],
    /// This plugin's phase hooks execute BEFORE the listed plugin IDs.
    pub before: &'static [&'static str],
}

impl PluginOrdering {
    pub const NONE: Self = Self {
        after: &[],
        before: &[],
    };

    #[must_use]
    pub const fn after(ids: &'static [&'static str]) -> Self {
        Self {
            after: ids,
            before: &[],
        }
    }

    #[must_use]
    pub const fn before(ids: &'static [&'static str]) -> Self {
        Self {
            after: &[],
            before: ids,
        }
    }

    #[must_use]
    pub fn is_constrained(&self) -> bool {
        !self.after.is_empty() || !self.before.is_empty()
    }
}

/// Behavioral abstraction for agent phase hooks.
///
/// Each hook receives an immutable [`ReadOnlyContext`] snapshot and returns a
/// typed [`ActionSet`] describing effects to apply. The loop applies these
/// actions via `match` — no dynamic dispatch, no runtime validation.
///
/// All hook methods have default no-op implementations; plugins only override
/// the phases they care about.
#[async_trait]
pub trait AgentBehavior: Send + Sync {
    fn id(&self) -> &str;

    fn behavior_ids(&self) -> Vec<&str> {
        vec![self.id()]
    }

    /// Declare execution ordering constraints relative to other plugins.
    fn ordering(&self) -> PluginOrdering {
        PluginOrdering::NONE
    }

    /// Self-configuration hook called once during resolve, before the loop starts.
    fn configure(&self, _config: &mut AgentRunConfig) {}

    /// Register lattice (CRDT) paths with the registry.
    fn register_lattice_paths(&self, _registry: &mut LatticeRegistry) {}

    /// Register state scopes with the registry.
    fn register_state_scopes(&self, _registry: &mut StateScopeRegistry) {}

    /// Register state-action deserializers for persisted intent-log replay and recovery.
    fn register_state_action_deserializers(&self, _registry: &mut StateActionDeserializerRegistry) {
    }

    async fn run_start(&self, _ctx: &ReadOnlyContext<'_>) -> ActionSet<LifecycleAction> {
        ActionSet::empty()
    }

    async fn step_start(&self, _ctx: &ReadOnlyContext<'_>) -> ActionSet<LifecycleAction> {
        ActionSet::empty()
    }

    async fn before_inference(
        &self,
        _ctx: &ReadOnlyContext<'_>,
    ) -> ActionSet<BeforeInferenceAction> {
        ActionSet::empty()
    }

    async fn after_inference(&self, _ctx: &ReadOnlyContext<'_>) -> ActionSet<AfterInferenceAction> {
        ActionSet::empty()
    }

    async fn before_tool_execute(
        &self,
        _ctx: &ReadOnlyContext<'_>,
    ) -> ActionSet<BeforeToolExecuteAction> {
        ActionSet::empty()
    }

    async fn after_tool_execute(
        &self,
        _ctx: &ReadOnlyContext<'_>,
    ) -> ActionSet<AfterToolExecuteAction> {
        ActionSet::empty()
    }

    async fn step_end(&self, _ctx: &ReadOnlyContext<'_>) -> ActionSet<LifecycleAction> {
        ActionSet::empty()
    }

    async fn run_end(&self, _ctx: &ReadOnlyContext<'_>) -> ActionSet<LifecycleAction> {
        ActionSet::empty()
    }
}

/// A no-op behavior that returns empty action sets for all hooks.
pub struct NoOpBehavior;

#[async_trait]
impl AgentBehavior for NoOpBehavior {
    fn id(&self) -> &str {
        "noop"
    }
}

/// Build a [`ReadOnlyContext`] from typed step fields and doc state.
pub fn build_read_only_context_from_step<'a>(
    phase: Phase,
    step: &'a StepContext<'a>,
    doc: &'a DocCell,
) -> ReadOnlyContext<'a> {
    let mut ctx = ReadOnlyContext::new(
        phase,
        step.thread_id(),
        step.messages(),
        step.run_policy(),
        doc,
    )
    .with_run_identity(step.ctx().run_identity());
    ctx.initial_message_count = step.initial_message_count();
    if let Some(llm) = step.llm_response.as_ref() {
        ctx = ctx.with_llm_response(llm);
    }
    if let Some(gate) = step.gate.as_ref() {
        ctx = ctx.with_tool_info(&gate.name, &gate.id, Some(&gate.args));
        if let Some(result) = gate.result.as_ref() {
            ctx = ctx.with_tool_result(result);
        }
        if matches!(phase, Phase::BeforeToolExecute | Phase::AfterToolExecute) {
            ctx = ctx.with_scope_ctx(ScopeContext::for_call(&gate.id));
        }
    }
    if phase == Phase::BeforeToolExecute {
        if let Some(call_id) = step.tool_call_id() {
            if let Ok(Some(resume)) = step.ctx().resume_input_for(call_id) {
                ctx = ctx.with_resume_input(resume);
            }
        }
    }
    ctx
}

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

    #[tokio::test]
    async fn default_agent_all_phases_noop() {
        let agent = NoOpBehavior;
        let config = RunPolicy::new();
        let doc = DocCell::new(json!({}));
        let ctx = ReadOnlyContext::new(Phase::RunStart, "t1", &[], &config, &doc);

        let actions = agent.run_start(&ctx).await;
        assert!(actions.is_empty());

        let ctx = ReadOnlyContext::new(Phase::BeforeInference, "t1", &[], &config, &doc);
        let actions = agent.before_inference(&ctx).await;
        assert!(actions.is_empty());
    }

    #[tokio::test]
    async fn agent_returns_actions() {
        struct ContextBehavior;

        #[async_trait]
        impl AgentBehavior for ContextBehavior {
            fn id(&self) -> &str {
                "ctx"
            }
            async fn before_inference(
                &self,
                _ctx: &ReadOnlyContext<'_>,
            ) -> ActionSet<BeforeInferenceAction> {
                ActionSet::single(BeforeInferenceAction::AddContextMessage(
                    crate::runtime::inference::ContextMessage {
                        key: "from_agent".into(),
                        role: crate::thread::Role::System,
                        content: "from agent".into(),
                        visibility: crate::thread::Visibility::Internal,
                        cooldown_turns: 0,
                        target: Default::default(),
                        consume_after_emit: false,
                    },
                ))
            }
        }

        let agent = ContextBehavior;
        let config = RunPolicy::new();
        let doc = DocCell::new(json!({}));
        let ctx = ReadOnlyContext::new(Phase::BeforeInference, "t1", &[], &config, &doc);

        let actions = agent.before_inference(&ctx).await;
        assert_eq!(actions.len(), 1);
    }

    #[tokio::test]
    async fn read_only_context_accessors() {
        let config = RunPolicy::new();
        let doc = DocCell::new(json!({"key": "val"}));
        let ctx = ReadOnlyContext::new(Phase::AfterToolExecute, "thread_42", &[], &config, &doc);

        assert_eq!(ctx.phase(), Phase::AfterToolExecute);
        assert_eq!(ctx.thread_id(), "thread_42");
        assert!(ctx.messages().is_empty());
        assert!(ctx.tool_name().is_none());
        assert!(ctx.tool_result().is_none());
        assert!(ctx.response().is_none());
        assert!(ctx.resume_input().is_none());

        let snapshot = ctx.snapshot();
        assert_eq!(snapshot["key"], "val");
    }

    #[tokio::test]
    async fn read_only_context_with_tool_info() {
        let config = RunPolicy::new();
        let doc = DocCell::new(json!({}));
        let args = json!({"x": 1});
        let ctx = ReadOnlyContext::new(Phase::BeforeToolExecute, "t1", &[], &config, &doc)
            .with_tool_info("my_tool", "call_1", Some(&args));

        assert_eq!(ctx.tool_name(), Some("my_tool"));
        assert_eq!(ctx.tool_call_id(), Some("call_1"));
        assert_eq!(ctx.tool_args().unwrap()["x"], 1);
    }
}