interplane.Get Omega
Read in your language

interplane-mv

INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.

Brought by drakestapleton

branch main Branches Tags History

interplane-mv / adapters / aien / tests / pipeline.rs

317 lines · 11554 bytes Raw Blame History
1//! Every test drives the real interplane `Pipeline` with `AienAuthority`.
2use aien_capability::ToolEffects;
3use interplane_adapter_aien::*;
4use interplane_core::*;
5use interplane_crossaxis::MappingTable;
6use interplane_crossveil::{Pipeline, RuntimeAuthority, TurnOutcome};
7use interplane_lenshift::DialectRegistry;
8use serde_json::{json, Value};
9
10fn run(rt: &mut AienAuthority, table: MappingTable, name: &str, args: Value) -> TurnOutcome {
11 let mut p = Pipeline::new(
12 DialectRegistry::with_defaults(),
13 table,
14 rt,
15 Limits::default(),
16 RequestLedger::new(),
17 );
18 let call = json!({"id": "c1", "type": "function", "function": {"name": name, "arguments": args.to_string()}});
19 let turn = json!({"role": "assistant", "content": null, "tool_calls": [call]});
20 p.run_turn("openai", "m", &turn, "t", 0)
21}
22
23fn workspace() -> tempfile::TempDir {
24 let d = tempfile::tempdir().unwrap();
25 std::fs::write(d.path().join("hello.txt"), "hi from aien").unwrap();
26 d
27}
28
29#[test]
30fn allowed_read_is_ok_with_workspace_provenance() {
31 let ws = workspace();
32 let mut rt = AienAuthority::new(ws.path()).unwrap();
33 let table = rt.mapping_table(true);
34 let out = run(&mut rt, table, "read_file", json!({"path": "hello.txt"}));
35 let (rec, res) = (&out.records[0], &out.results[0]);
36 assert_eq!(res.status, ResultStatus::Ok, "{res:?}");
37 assert_eq!(res.data["content"], "hi from aien");
38 assert!(rec.decide_invoked && rec.execute_invoked);
39 let p = res.provenance.as_ref().unwrap();
40 assert_eq!(p.content_kind, Some(ContentKind::WorkspaceContent));
41 assert_eq!(p.trust, Some(TrustLevel::WorkspaceUntrusted));
42 assert_eq!(p.trusted, Some(false));
43 assert_eq!(rt.wire_calls(), 1);
44}
45
46#[test]
47fn list_dir_ok_and_trusted_flag_is_honoured() {
48 let ws = workspace();
49 let mut rt = AienAuthority::new(ws.path())
50 .unwrap()
51 .with_trusted_workspace(true);
52 let table = rt.mapping_table(false);
53 let out = run(&mut rt, table, "list_dir", json!({"path": "."}));
54 let res = &out.results[0];
55 assert_eq!(res.status, ResultStatus::Ok, "{res:?}");
56 assert_eq!(res.data["entries"], json!(["hello.txt"]));
57 let p = res.provenance.as_ref().unwrap();
58 assert_eq!(p.trust, Some(TrustLevel::TrustedRuntime));
59 assert_eq!(p.trusted, Some(true));
60}
61
62#[test]
63fn world_mutation_requires_approval_and_never_executes() {
64 let ws = workspace();
65 let mut rt = AienAuthority::new(ws.path()).unwrap();
66 let table = rt.mapping_table(false);
67 let out = run(
68 &mut rt,
69 table,
70 "write_file",
71 json!({"path": "x.txt", "content": "y"}),
72 );
73 let (rec, res) = (&out.records[0], &out.results[0]);
74 assert_eq!(res.status, ResultStatus::RequiresApproval);
75 assert!(rec.decide_invoked && !rec.execute_invoked);
76 let d = res.decision.as_ref().unwrap();
77 assert!(
78 // No user request registered: the empty ledger is external_untrusted (0.3 cut E4).
79 d.reason
80 .as_deref()
81 .unwrap()
82 .contains("under untrusted exposure (floor below user_supplied)"),
83 "{d:?}"
84 );
85 assert_eq!(d.authority.policy_engine, ENGINE_EFFECT);
86 // The adapter minted a correlation handle; nothing is consumed.
87 assert!(d.approval.is_some());
88 let rs = d.runtime_state.as_ref().unwrap();
89 assert!(rs.values.contains(&"staged=true".to_string()), "{rs:?}");
90 assert!(
91 rs.values
92 .iter()
93 .any(|v| v.starts_with("intent_digest=sha256") || v.starts_with("intent_digest=")),
94 "{rs:?}"
95 );
96 assert_eq!((rt.execute_calls, rt.wire_calls()), (0, 0));
97 assert!(!ws.path().join("x.txt").exists());
98}
99
100#[test]
101fn local_ephemeral_effect_is_authorized_by_aien_and_executes_with_receipt() {
102 let ws = workspace();
103 let mut rt = AienAuthority::new(ws.path())
104 .unwrap()
105 .with_effects("write_file", ToolEffects::LOCAL_EPHEMERAL)
106 .unwrap();
107 let table = rt.mapping_table(true);
108 let out = run(
109 &mut rt,
110 table,
111 "write_file",
112 json!({"path": "scratch.txt", "content": "ephemeral"}),
113 );
114 let (rec, res) = (&out.records[0], &out.results[0]);
115 assert_eq!(res.status, ResultStatus::Ok, "{res:?}");
116 assert!(rec.decide_invoked && rec.execute_invoked);
117 assert_eq!(res.data["output"]["bytes"], 9);
118 assert_eq!(res.data["receipt"]["tool_name"], "write_file");
119 assert!(res.data["receipt"]["effect_id"].as_str().unwrap().len() == 32);
120 assert_eq!(rec.decision.as_deref(), Some("authorized"));
121 let f = ws.path().join("scratch.txt");
122 assert_eq!(std::fs::read_to_string(&f).unwrap(), "ephemeral");
123 assert_eq!(rt.wire_calls(), 1);
124 std::fs::remove_file(f).unwrap();
125}
126
127#[test]
128fn irreversible_effect_is_denied_with_aiens_reason_and_never_executes() {
129 let ws = workspace();
130 let mut rt = AienAuthority::new(ws.path())
131 .unwrap()
132 .with_effects("write_file", ToolEffects::EXTERNAL_IRREVERSIBLE)
133 .unwrap();
134 let table = rt.mapping_table(false);
135 let out = run(
136 &mut rt,
137 table,
138 "write_file",
139 json!({"path": "x.txt", "content": "y"}),
140 );
141 let (rec, res) = (&out.records[0], &out.results[0]);
142 assert_eq!(res.status, ResultStatus::Denied, "{res:?}");
143 assert!(res
144 .error
145 .as_ref()
146 .unwrap()
147 .message
148 .contains("irreversible external effects are intents only"));
149 assert!(!rec.execute_invoked);
150 assert_eq!((rt.execute_calls, rt.wire_calls()), (0, 0));
151 assert!(!ws.path().join("x.txt").exists());
152}
153
154#[test]
155fn effect_replay_does_not_execute_twice() {
156 let ws = workspace();
157 let mut rt = AienAuthority::new(ws.path())
158 .unwrap()
159 .with_effects("write_file", ToolEffects::LOCAL_EPHEMERAL)
160 .unwrap();
161 let table = rt.mapping_table(false);
162 let mut p = Pipeline::new(
163 DialectRegistry::with_defaults(),
164 table,
165 &mut rt,
166 Limits::default(),
167 RequestLedger::new(),
168 );
169 let call = json!({"id": "c1", "type": "function", "function": {"name": "write_file",
170 "arguments": json!({"path": "r.txt", "content": "once"}).to_string()}});
171 let turn = json!({"role": "assistant", "content": null, "tool_calls": [call]});
172 let first = p.run_turn("openai", "m", &turn, "t", 0);
173 let second = p.run_turn("openai", "m", &turn, "t", 0);
174 assert_eq!(first.results[0].status, ResultStatus::Ok);
175 assert_ne!(
176 second.results[0].status,
177 ResultStatus::Ok,
178 "{:?}",
179 second.results[0]
180 );
181 assert!(!second.records[0].execute_invoked);
182 drop(p);
183 assert_eq!(rt.wire_calls(), 1);
184}
185
186#[test]
187fn adapter_source_never_constructs_authorized_effect() {
188 let src = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/src/lib.rs")).unwrap();
189 let code: String = src
190 .lines()
191 .filter(|l| !l.trim_start().starts_with("//"))
192 .collect::<Vec<_>>()
193 .join("\n");
194 for forbidden in [
195 "AuthorizedEffect {",
196 "AuthorizedEffect{",
197 "authorize_for_test",
198 "mint(",
199 "impl EffectAuthority",
200 "AuthorityDecision::Allow",
201 ] {
202 assert!(
203 !code.contains(forbidden),
204 "adapter must not contain `{forbidden}`"
205 );
206 }
207}
208
209#[test]
210fn nonexistent_capability_is_not_found() {
211 let ws = workspace();
212 let mut rt = AienAuthority::new(ws.path()).unwrap();
213 let table = MappingTable::from_value(
214 json!({"runtime": "aien", "table_version": "1", "catalog_digest": null,
215 "rules": [{"id": "passthrough:teleport", "kind": "passthrough",
216 "from": {"namespace": null, "name": "teleport"}, "to": "teleport"}]}),
217 )
218 .unwrap();
219 let out = run(&mut rt, table, "teleport", json!({"to": "mars"}));
220 let rec = &out.records[0];
221 assert_eq!(rec.decision.as_deref(), Some("not_found"));
222 assert_eq!(rec.error_code.as_deref(), Some("capability_not_found"));
223 assert!(!rec.execute_invoked);
224 assert_eq!(rt.wire_calls(), 0);
225}
226
227#[test]
228fn missing_argument_is_invalid_with_pinned_message() {
229 let ws = workspace();
230 let mut rt = AienAuthority::new(ws.path()).unwrap();
231 let table = rt.mapping_table(false);
232 let out = run(&mut rt, table, "read_file", json!({}));
233 let (rec, res) = (&out.records[0], &out.results[0]);
234 assert_eq!(rec.decision.as_deref(), Some("invalid"));
235 assert_eq!(
236 res.error.as_ref().unwrap().message,
237 "missing required argument: path"
238 );
239 assert!(!rec.execute_invoked);
240 assert_eq!(rt.wire_calls(), 0);
241}
242
243#[test]
244fn runtime_failure_is_execution_error() {
245 let ws = workspace();
246 let mut rt = AienAuthority::new(ws.path()).unwrap();
247 let table = rt.mapping_table(false);
248 let out = run(&mut rt, table, "read_file", json!({"path": "missing.txt"}));
249 let (rec, res) = (&out.records[0], &out.results[0]);
250 assert_eq!(res.status, ResultStatus::Error);
251 assert_eq!(rec.error_code.as_deref(), Some("execution_error"));
252 assert!(rec.execute_invoked);
253 assert!(res.error.as_ref().unwrap().message.contains("no such file"));
254}
255
256#[test]
257fn runtime_absent_fails_closed() {
258 let mut rt = AienAuthority::unavailable();
259 let table = rt.mapping_table(false);
260 let out = run(&mut rt, table, "read_file", json!({"path": "hello.txt"}));
261 let (rec, res) = (&out.records[0], &out.results[0]);
262 assert_eq!(res.status, ResultStatus::Denied);
263 assert_eq!(res.error.as_ref().unwrap().message, UNAVAILABLE);
264 assert!(rec.decide_invoked && !rec.execute_invoked);
265 assert_eq!(rt.wire_calls(), 0);
266}
267
268#[test]
269fn escape_from_workspace_is_denied() {
270 let ws = workspace();
271 let mut rt = AienAuthority::new(ws.path()).unwrap();
272 let table = rt.mapping_table(false);
273 for path in ["../etc/passwd", "/etc/passwd"] {
274 let out = run(&mut rt, table.clone(), "read_file", json!({"path": path}));
275 assert_eq!(out.results[0].status, ResultStatus::Denied, "{path}");
276 }
277 assert_eq!(rt.wire_calls(), 0);
278}
279
280#[test]
281fn aien_gate_denies_unlisted_shell_command() {
282 let ws = workspace();
283 let mut rt = AienAuthority::new(ws.path()).unwrap();
284 let table = rt.mapping_table(false);
285 let out = run(&mut rt, table, "bash_eval", json!({"command": "rm -rf /"}));
286 assert_eq!(
287 out.results[0].status,
288 ResultStatus::Denied,
289 "{:?}",
290 out.results[0]
291 );
292 assert_eq!(rt.wire_calls(), 0);
293}
294
295#[test]
296fn catalog_records_both_digests() {
297 let ws = workspace();
298 let rt = AienAuthority::new(ws.path()).unwrap();
299 let c = rt.catalog();
300 let a = &c.extensions["aien"];
301 assert_eq!(a["catalog_digest"], json!(c.catalog_digest));
302 assert_eq!(a["interplane_computed_digest"], json!(c.compute_digest()));
303 assert_eq!(a["digests_equal"], json!(false));
304 assert_eq!(c.capabilities.len(), 4);
305 // A pinned table matches the live catalog; a wrong pin is stale before decide.
306 let stale = MappingTable::from_value(json!({"runtime": "aien", "table_version": "1",
307 "catalog_digest": format!("sha256:{}", "0".repeat(64)),
308 "rules": [{"id": "p", "kind": "passthrough", "from": {"namespace": null, "name": "read_file"}, "to": "read_file"}]}))
309 .unwrap();
310 let mut rt = rt;
311 let out = run(&mut rt, stale, "read_file", json!({"path": "hello.txt"}));
312 assert_eq!(
313 out.records[0].error_code.as_deref(),
314 Some("stale_capability")
315 );
316 assert_eq!(rt.decide_calls, 0);
317}