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
//! Integration tests for Goal 181–189: compute-storage separation traits.
//!
//! Covers the full stack: StorageBackend / SessionStore / ToolSetProvider
//! as injected into `AgentKernelBuilder`. No real external services required
//! for the base feature set; cloud-runtime gated tests use serialisation
//! unit checks only.
mod v060 {
use recursive::llm::{mock::MockProvider, Completion};
use recursive::storage::{AgentCheckpointState, LocalStorageBackend, NoopSessionStore};
use recursive::tools::policy_sandbox::{PolicyConfig, ShellPolicy};
use recursive::{
AgentKernel, LocalStorageBackend as LibLocalStorage, NoopSessionStore as LibNoop,
PolicyConfig as LibPolicyConfig, PolicyToolSetProvider, ToolSetProvider,
};
use std::sync::Arc;
use tempfile::TempDir;
// ─── helpers ─────────────────────────────────────────────────────────────
fn mock_llm() -> Arc<dyn recursive::LlmProvider> {
Arc::new(MockProvider::new(vec![Completion {
content: "done".into(),
tool_calls: vec![],
finish_reason: Some("stop".into()),
usage: None,
reasoning_content: None,
}]))
}
// ─────────────────────────────────────────────────────────────────────────
// 1. LocalStorageBackend — transcript round-trip via AgentKernelBuilder
// ─────────────────────────────────────────────────────────────────────────
#[tokio::test]
async fn local_storage_backend_round_trip() {
use recursive::storage::StorageBackend;
let dir = TempDir::new().unwrap();
let backend = Arc::new(LocalStorageBackend::new(dir.path().to_path_buf()));
// Write two messages through the backend directly.
use recursive::message::{Message, Role};
let msgs = vec![
Message {
role: Role::User,
content: "ping".into(),
tool_calls: vec![],
tool_call_id: None,
reasoning_content: None,
},
Message {
role: Role::Assistant,
content: "pong".into(),
tool_calls: vec![],
tool_call_id: None,
reasoning_content: None,
},
];
backend
.save_transcript("test-session", &msgs)
.await
.unwrap();
// Load back and verify equality.
let loaded = backend.load_transcript("test-session").await.unwrap();
assert_eq!(loaded.len(), 2);
assert_eq!(loaded[0].content, "ping");
assert_eq!(loaded[1].content, "pong");
}
// ─────────────────────────────────────────────────────────────────────────
// 2. LocalStorageBackend — memory round-trip
// ─────────────────────────────────────────────────────────────────────────
#[tokio::test]
async fn local_storage_memory_round_trip() {
use recursive::storage::StorageBackend;
let dir = TempDir::new().unwrap();
let backend = LocalStorageBackend::new(dir.path().to_path_buf());
backend
.save_memory("user.md", "## preferences\n- concise")
.await
.unwrap();
let val = backend.load_memory("user.md").await.unwrap();
assert_eq!(val.as_deref(), Some("## preferences\n- concise"));
let missing = backend.load_memory("nonexistent.md").await.unwrap();
assert!(missing.is_none());
}
// ─────────────────────────────────────────────────────────────────────────
// 3. NoopSessionStore — save/load never errors
// ─────────────────────────────────────────────────────────────────────────
#[tokio::test]
async fn noop_session_store_save_load_does_not_error() {
use recursive::storage::SessionStore;
let store = NoopSessionStore;
let state = AgentCheckpointState {
step: 3,
transcript_len: 12,
};
// Save should be a no-op and succeed.
store.save_state("sess-abc", &state).await.unwrap();
// Load always returns None.
let loaded = store.load_state("sess-abc").await.unwrap();
assert!(loaded.is_none());
}
// ─────────────────────────────────────────────────────────────────────────
// 4. AgentKernelBuilder — defaults compile and build without panicking
// ─────────────────────────────────────────────────────────────────────────
#[test]
fn kernel_builder_defaults_build_successfully() {
let kernel = AgentKernel::builder()
.llm(mock_llm())
.build()
.expect("builder with defaults should succeed");
// Verify default storage is a LocalStorageBackend (non-null arc).
// We can't easily downcast dyn trait, so just assert it's accessible.
let _ = kernel.storage();
let _ = kernel.session_store();
}
// ─────────────────────────────────────────────────────────────────────────
// 5. AgentKernelBuilder — explicit LocalStorageBackend injection
// ─────────────────────────────────────────────────────────────────────────
#[test]
fn kernel_builder_accepts_explicit_storage_backend() {
let dir = TempDir::new().unwrap();
let backend = Arc::new(LibLocalStorage::new(dir.path().to_path_buf()));
let store = Arc::new(LibNoop);
let _kernel = AgentKernel::builder()
.llm(mock_llm())
.with_storage(backend)
.with_session_store(store)
.build()
.expect("explicit storage + session store should build");
}
// ─────────────────────────────────────────────────────────────────────────
// 6. PolicyToolSetProvider — blocks forbidden shell commands
// ─────────────────────────────────────────────────────────────────────────
#[test]
fn policy_sandbox_blocks_forbidden_command() {
let dir = TempDir::new().unwrap();
let policy = LibPolicyConfig {
shell: ShellPolicy {
deny_patterns: vec!["rm".into()],
},
..Default::default()
};
let provider = PolicyToolSetProvider::new(dir.path().to_path_buf(), 30, vec![], policy);
let registry = provider.build_registry();
let attached = registry.policy().expect("policy should be attached");
assert!(
attached.check_shell("rm -rf /").is_err(),
"rm should be blocked"
);
}
// ─────────────────────────────────────────────────────────────────────────
// 7. PolicyToolSetProvider — allows non-forbidden commands
// ─────────────────────────────────────────────────────────────────────────
#[test]
fn policy_sandbox_allows_permitted_command() {
let dir = TempDir::new().unwrap();
let policy = PolicyConfig {
shell: ShellPolicy {
deny_patterns: vec!["rm".into()],
},
..Default::default()
};
let provider = PolicyToolSetProvider::new(dir.path().to_path_buf(), 30, vec![], policy);
let registry = provider.build_registry();
let attached = registry.policy().expect("policy should be attached");
assert!(
attached.check_shell("ls -la").is_ok(),
"ls should be allowed"
);
assert!(
attached.check_shell("cargo test").is_ok(),
"cargo test should be allowed"
);
}
// ─────────────────────────────────────────────────────────────────────────
// 8. PolicyToolSetProvider restrictive preset blocks dangerous patterns
// ─────────────────────────────────────────────────────────────────────────
#[test]
fn restrictive_policy_preset_blocks_dangerous_commands() {
let dir = TempDir::new().unwrap();
let provider = PolicyToolSetProvider::restrictive(dir.path().to_path_buf(), 30, vec![]);
let registry = provider.build_registry();
let policy = registry.policy().unwrap();
// Patterns from PolicyConfig::default_restrictive
assert!(policy.check_shell("rm -rf /").is_err());
assert!(policy.check_shell("mkfs.ext4 /dev/sda").is_err());
assert!(policy.check_shell("echo foo > /dev/mem").is_err());
// Safe commands should pass
assert!(policy.check_shell("ls -la").is_ok());
assert!(policy.check_shell("cargo build").is_ok());
}
// ─────────────────────────────────────────────────────────────────────────
// 9. AgentKernelBuilder — with_tool_set_provider injects PolicyProvider
// ─────────────────────────────────────────────────────────────────────────
#[test]
fn kernel_builder_with_policy_provider_builds() {
let dir = TempDir::new().unwrap();
let provider = Arc::new(PolicyToolSetProvider::restrictive(
dir.path().to_path_buf(),
30,
vec![],
));
let _kernel = AgentKernel::builder()
.llm(mock_llm())
.with_tool_set_provider(provider)
.build()
.expect("kernel with policy provider should build");
}
// ─────────────────────────────────────────────────────────────────────────
// 10. AgentCheckpointState serialization (no external service needed)
// ─────────────────────────────────────────────────────────────────────────
#[test]
fn checkpoint_state_serde_round_trip() {
let state = AgentCheckpointState {
step: 7,
transcript_len: 42,
};
let json = serde_json::to_string(&state).expect("serialise");
let loaded: AgentCheckpointState = serde_json::from_str(&json).expect("deserialise");
assert_eq!(loaded.step, 7);
assert_eq!(loaded.transcript_len, 42);
}
// ─────────────────────────────────────────────────────────────────────────
// 11. [cloud-runtime] RedisSessionStore — construction succeeds (lazy connect)
// ─────────────────────────────────────────────────────────────────────────
#[cfg(feature = "cloud-runtime")]
#[test]
fn redis_session_store_construction_succeeds() {
use recursive::storage::SessionStore;
use recursive::RedisSessionStore;
use std::time::Duration;
// Deadpool-redis creates the pool lazily: the constructor should succeed
// even if no Redis is running.
let store =
RedisSessionStore::new("redis://127.0.0.1:6379", Duration::from_secs(300), "test:")
.expect("should build from URL");
// We can verify that the store implements SessionStore without I/O.
let _: &dyn SessionStore = &store;
}
// ─────────────────────────────────────────────────────────────────────────
// 12. [cloud-runtime] AgentCheckpointState — JSON round-trip for Redis path
// ─────────────────────────────────────────────────────────────────────────
#[cfg(feature = "cloud-runtime")]
#[test]
fn checkpoint_state_json_is_stable() {
use recursive::storage::AgentCheckpointState;
let state = AgentCheckpointState {
step: 99,
transcript_len: 256,
};
let json = serde_json::to_string(&state).unwrap();
// Verify field names are as expected (Redis stores raw JSON).
assert!(json.contains("\"step\""));
assert!(json.contains("\"transcript_len\""));
let back: AgentCheckpointState = serde_json::from_str(&json).unwrap();
assert_eq!(back.step, 99);
assert_eq!(back.transcript_len, 256);
}
}