layer0 0.4.0

Protocol traits for composable agentic AI systems
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
//! Phase 2 tests — in-memory implementations prove the traits work.
//! Run with: cargo test --features test-utils --test phase2

#![cfg(feature = "test-utils")]

use layer0::test_utils::{
    EchoOperator, InMemoryStore, LocalEnvironment, LocalOrchestrator, LoggingHook,
};
use layer0::*;
use rust_decimal::Decimal;
use serde_json::json;
use std::sync::Arc;

fn simple_input(msg: &str) -> OperatorInput {
    OperatorInput::new(Content::text(msg), layer0::operator::TriggerType::User)
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// EchoOperator
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

#[tokio::test]
async fn echo_operator_returns_input_as_output() {
    let turn = EchoOperator;
    let input = simple_input("hello echo");
    let output = turn.execute(input).await.unwrap();
    assert_eq!(output.message, Content::text("hello echo"));
    assert_eq!(output.exit_reason, ExitReason::Complete);
}

#[tokio::test]
async fn echo_operator_metadata_is_default() {
    let turn = EchoOperator;
    let input = simple_input("test");
    let output = turn.execute(input).await.unwrap();
    assert_eq!(output.metadata.tokens_in, 0);
    assert_eq!(output.metadata.cost, Decimal::ZERO);
    assert!(output.effects.is_empty());
}

#[tokio::test]
async fn echo_operator_is_usable_as_dyn_operator() {
    let turn: Box<dyn Operator> = Box::new(EchoOperator);
    let input = simple_input("dynamic dispatch");
    let output = turn.execute(input).await.unwrap();
    assert_eq!(output.message, Content::text("dynamic dispatch"));
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// InMemoryStore
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// Helper: cast to &dyn StateStore to avoid ambiguity with blanket StateReader impl
fn as_store(s: &InMemoryStore) -> &dyn StateStore {
    s
}

#[tokio::test]
async fn in_memory_store_write_then_read() {
    let store = InMemoryStore::new();
    let scope = Scope::Global;
    let s = as_store(&store);
    s.write(&scope, "key1", json!("value1")).await.unwrap();
    let val = s.read(&scope, "key1").await.unwrap();
    assert_eq!(val, Some(json!("value1")));
}

#[tokio::test]
async fn in_memory_store_read_missing_returns_none() {
    let store = InMemoryStore::new();
    let val = as_store(&store)
        .read(&Scope::Global, "nonexistent")
        .await
        .unwrap();
    assert_eq!(val, None);
}

#[tokio::test]
async fn in_memory_store_delete() {
    let store = InMemoryStore::new();
    let scope = Scope::Global;
    let s = as_store(&store);
    s.write(&scope, "key1", json!("value1")).await.unwrap();
    s.delete(&scope, "key1").await.unwrap();
    let val = s.read(&scope, "key1").await.unwrap();
    assert_eq!(val, None);
}

#[tokio::test]
async fn in_memory_store_delete_missing_is_noop() {
    let store = InMemoryStore::new();
    // Should not error
    as_store(&store)
        .delete(&Scope::Global, "nonexistent")
        .await
        .unwrap();
}

#[tokio::test]
async fn in_memory_store_list_by_prefix() {
    let store = InMemoryStore::new();
    let scope = Scope::Global;
    let s = as_store(&store);
    s.write(&scope, "notes/a", json!("a")).await.unwrap();
    s.write(&scope, "notes/b", json!("b")).await.unwrap();
    s.write(&scope, "other/c", json!("c")).await.unwrap();

    let mut keys = s.list(&scope, "notes/").await.unwrap();
    keys.sort();
    assert_eq!(keys, vec!["notes/a", "notes/b"]);
}

#[tokio::test]
async fn in_memory_store_list_empty_prefix_returns_all() {
    let store = InMemoryStore::new();
    let scope = Scope::Global;
    let s = as_store(&store);
    s.write(&scope, "a", json!(1)).await.unwrap();
    s.write(&scope, "b", json!(2)).await.unwrap();

    let mut keys = s.list(&scope, "").await.unwrap();
    keys.sort();
    assert_eq!(keys, vec!["a", "b"]);
}

#[tokio::test]
async fn in_memory_store_scopes_are_isolated() {
    let store = InMemoryStore::new();
    let s1 = Scope::Session(SessionId::new("s1"));
    let s2 = Scope::Session(SessionId::new("s2"));
    let s = as_store(&store);

    s.write(&s1, "key", json!("from s1")).await.unwrap();
    s.write(&s2, "key", json!("from s2")).await.unwrap();

    assert_eq!(s.read(&s1, "key").await.unwrap(), Some(json!("from s1")));
    assert_eq!(s.read(&s2, "key").await.unwrap(), Some(json!("from s2")));
}

#[tokio::test]
async fn in_memory_store_overwrite() {
    let store = InMemoryStore::new();
    let scope = Scope::Global;
    let s = as_store(&store);
    s.write(&scope, "key", json!("v1")).await.unwrap();
    s.write(&scope, "key", json!("v2")).await.unwrap();
    assert_eq!(s.read(&scope, "key").await.unwrap(), Some(json!("v2")));
}

#[tokio::test]
async fn in_memory_store_search_returns_empty() {
    // InMemoryStore doesn't support semantic search — returns empty vec
    let store = InMemoryStore::new();
    let results = as_store(&store)
        .search(&Scope::Global, "anything", 10)
        .await
        .unwrap();
    assert!(results.is_empty());
}

#[tokio::test]
async fn in_memory_store_is_usable_as_dyn_state_store() {
    let store: Box<dyn StateStore> = Box::new(InMemoryStore::new());
    store.write(&Scope::Global, "k", json!("v")).await.unwrap();
    assert_eq!(
        store.read(&Scope::Global, "k").await.unwrap(),
        Some(json!("v"))
    );
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LocalEnvironment
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

#[tokio::test]
async fn local_environment_passes_through_to_turn() {
    let env = LocalEnvironment::new(Arc::new(EchoOperator));
    let input = simple_input("through environment");
    let spec = EnvironmentSpec::default();
    let output = env.run(input, &spec).await.unwrap();
    assert_eq!(output.message, Content::text("through environment"));
    assert_eq!(output.exit_reason, ExitReason::Complete);
}

#[tokio::test]
async fn local_environment_is_usable_as_dyn_environment() {
    let env: Box<dyn Environment> = Box::new(LocalEnvironment::new(Arc::new(EchoOperator)));
    let input = simple_input("dynamic env");
    let spec = EnvironmentSpec::default();
    let output = env.run(input, &spec).await.unwrap();
    assert_eq!(output.message, Content::text("dynamic env"));
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LoggingHook
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

#[tokio::test]
async fn logging_hook_registers_all_points() {
    let hook = LoggingHook::new();
    let points = hook.points();
    assert!(points.contains(&HookPoint::PreInference));
    assert!(points.contains(&HookPoint::PostInference));
    assert!(points.contains(&HookPoint::PreToolUse));
    assert!(points.contains(&HookPoint::PostToolUse));
    assert!(points.contains(&HookPoint::ExitCheck));
}

#[tokio::test]
async fn logging_hook_returns_continue_at_every_point() {
    let hook = LoggingHook::new();
    let all_points = [
        HookPoint::PreInference,
        HookPoint::PostInference,
        HookPoint::PreToolUse,
        HookPoint::PostToolUse,
        HookPoint::ExitCheck,
    ];
    for point in all_points {
        let ctx = HookContext::new(point);
        let action = hook.on_event(&ctx).await.unwrap();
        assert!(matches!(action, HookAction::Continue));
    }
}

#[tokio::test]
async fn logging_hook_records_events() {
    let hook = LoggingHook::new();
    let mut ctx = HookContext::new(HookPoint::PreInference);
    ctx.tokens_used = 100;
    ctx.cost = Decimal::new(5, 3);
    ctx.turns_completed = 1;
    ctx.elapsed = DurationMs::from_secs(1);
    hook.on_event(&ctx).await.unwrap();
    let events = hook.events();
    assert_eq!(events.len(), 1);
    assert_eq!(events[0].point, HookPoint::PreInference);
}

#[tokio::test]
async fn logging_hook_is_usable_as_dyn_hook() {
    let hook: Box<dyn Hook> = Box::new(LoggingHook::new());
    assert_eq!(hook.points().len(), 5);
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LocalOrchestrator
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

#[tokio::test]
async fn local_orchestrator_dispatch_to_echo() {
    let mut orch = LocalOrchestrator::new();
    orch.register(AgentId::new("echo"), Arc::new(EchoOperator));
    let input = simple_input("dispatch test");
    let output = orch.dispatch(&AgentId::new("echo"), input).await.unwrap();
    assert_eq!(output.message, Content::text("dispatch test"));
}

#[tokio::test]
async fn local_orchestrator_dispatch_agent_not_found() {
    let orch = LocalOrchestrator::new();
    let input = simple_input("nobody home");
    let result = orch.dispatch(&AgentId::new("missing"), input).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("agent not found"));
}

#[tokio::test]
async fn local_orchestrator_dispatch_many_concurrent() {
    let mut orch = LocalOrchestrator::new();
    orch.register(AgentId::new("a"), Arc::new(EchoOperator));
    orch.register(AgentId::new("b"), Arc::new(EchoOperator));

    let tasks = vec![
        (AgentId::new("a"), simple_input("msg-a")),
        (AgentId::new("b"), simple_input("msg-b")),
    ];

    let results = orch.dispatch_many(tasks).await;
    assert_eq!(results.len(), 2);
    assert_eq!(results[0].as_ref().unwrap().message, Content::text("msg-a"));
    assert_eq!(results[1].as_ref().unwrap().message, Content::text("msg-b"));
}

#[tokio::test]
async fn local_orchestrator_dispatch_many_partial_failure() {
    let mut orch = LocalOrchestrator::new();
    orch.register(AgentId::new("a"), Arc::new(EchoOperator));
    // "b" is not registered

    let tasks = vec![
        (AgentId::new("a"), simple_input("ok")),
        (AgentId::new("b"), simple_input("fail")),
    ];

    let results = orch.dispatch_many(tasks).await;
    assert!(results[0].is_ok());
    assert!(results[1].is_err());
}

#[tokio::test]
async fn local_orchestrator_is_usable_as_dyn_orchestrator() {
    let mut orch = LocalOrchestrator::new();
    orch.register(AgentId::new("echo"), Arc::new(EchoOperator));
    let orch: Box<dyn Orchestrator> = Box::new(orch);
    let output = orch
        .dispatch(&AgentId::new("echo"), simple_input("dyn"))
        .await
        .unwrap();
    assert_eq!(output.message, Content::text("dyn"));
}

#[tokio::test]
async fn orchestrator_signal_accepted() {
    let orch = LocalOrchestrator::new();
    let wf = WorkflowId::new("wf-1");
    let signal = layer0::effect::SignalPayload::new("cancel", json!({"reason": "user request"}));
    // LocalOrchestrator accepts all signals (no-op)
    let result = orch.signal(&wf, signal).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn orchestrator_query_returns_null() {
    let orch = LocalOrchestrator::new();
    let wf = WorkflowId::new("wf-1");
    let query = layer0::orchestrator::QueryPayload::new("status", json!({}));
    // LocalOrchestrator returns Null for all queries (no-op)
    let result = orch.query(&wf, query).await.unwrap();
    assert_eq!(result, serde_json::Value::Null);
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Integration: compose ALL implementations
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

/// Full integration: orchestrator dispatches to two echo agents,
/// results are written to in-memory state, hooks log each step,
/// environment wraps the execution. All through trait interfaces.
#[tokio::test]
async fn integration_compose_all_implementations() {
    // 1. Set up orchestrator with two agents
    let mut orch = LocalOrchestrator::new();
    orch.register(AgentId::new("agent-a"), Arc::new(EchoOperator));
    orch.register(AgentId::new("agent-b"), Arc::new(EchoOperator));

    // 2. Set up state store
    let store = InMemoryStore::new();
    let s = as_store(&store);

    // 3. Set up environment
    let env = LocalEnvironment::new(Arc::new(EchoOperator));

    // 4. Set up hook
    let hook = LoggingHook::new();

    // 5. Dispatch two agents through the orchestrator
    let tasks = vec![
        (AgentId::new("agent-a"), simple_input("task for A")),
        (AgentId::new("agent-b"), simple_input("task for B")),
    ];
    let results = orch.dispatch_many(tasks).await;

    // Verify both succeeded
    assert_eq!(results.len(), 2);
    let output_a = results[0].as_ref().unwrap();
    let output_b = results[1].as_ref().unwrap();
    assert_eq!(output_a.message, Content::text("task for A"));
    assert_eq!(output_b.message, Content::text("task for B"));

    // 6. Write results to state store
    let scope = Scope::Workflow(WorkflowId::new("wf-integration"));
    s.write(&scope, "result/agent-a", json!({"message": "task for A"}))
        .await
        .unwrap();
    s.write(&scope, "result/agent-b", json!({"message": "task for B"}))
        .await
        .unwrap();

    // Verify state was persisted
    let keys = {
        let mut k = s.list(&scope, "result/").await.unwrap();
        k.sort();
        k
    };
    assert_eq!(keys, vec!["result/agent-a", "result/agent-b"]);
    assert_eq!(
        s.read(&scope, "result/agent-a").await.unwrap(),
        Some(json!({"message": "task for A"}))
    );

    // 7. Fire hooks to simulate turn lifecycle observation
    let mut ctx = HookContext::new(HookPoint::PostInference);
    ctx.model_output = Some(Content::text("task for A"));
    ctx.tokens_used = 100;
    ctx.cost = Decimal::new(5, 3);
    ctx.turns_completed = 1;
    ctx.elapsed = DurationMs::from_millis(500);
    let action = hook.on_event(&ctx).await.unwrap();
    assert!(matches!(action, HookAction::Continue));

    let mut ctx2 = HookContext::new(HookPoint::PostInference);
    ctx2.model_output = Some(Content::text("task for B"));
    ctx2.tokens_used = 200;
    ctx2.cost = Decimal::new(10, 3);
    ctx2.turns_completed = 2;
    ctx2.elapsed = DurationMs::from_secs(1);
    hook.on_event(&ctx2).await.unwrap();

    // Verify hooks recorded both events
    let events = hook.events();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].tokens_used, 100);
    assert_eq!(events[1].tokens_used, 200);

    // 8. Run a turn through the environment (passthrough)
    let env_output = env
        .run(simple_input("env-wrapped"), &EnvironmentSpec::default())
        .await
        .unwrap();
    assert_eq!(env_output.message, Content::text("env-wrapped"));
}