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 / bridge.rs

280 lines · 11148 bytes Raw Blame History
1//! Drives the `aien-authority-bridge` binary over a real pipe: catalog, one authorized read, a
2//! path escape that is denied with no read, a malformed line, and the approval round trip.
3use std::io::{BufRead, BufReader, Write};
4use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
5
6use serde_json::{json, Value};
7
8const TRACE: &str = "0123456789abcdef0123456789abcdef";
9
10struct Bridge {
11 child: Child,
12 stdin: ChildStdin,
13 stdout: BufReader<ChildStdout>,
14}
15
16impl Bridge {
17 fn start(ws: &std::path::Path) -> Self {
18 let mut child = Command::new(env!("CARGO_BIN_EXE_aien-authority-bridge"))
19 .arg(ws)
20 .stdin(Stdio::piped())
21 .stdout(Stdio::piped())
22 .spawn()
23 .expect("spawn bridge");
24 let stdin = child.stdin.take().unwrap();
25 let stdout = BufReader::new(child.stdout.take().unwrap());
26 Self {
27 child,
28 stdin,
29 stdout,
30 }
31 }
32
33 fn raw(&mut self, line: &str) -> Value {
34 writeln!(self.stdin, "{line}").unwrap();
35 self.stdin.flush().unwrap();
36 let mut out = String::new();
37 self.stdout.read_line(&mut out).unwrap();
38 serde_json::from_str(&out).expect("one json line per request")
39 }
40
41 fn send(&mut self, v: Value) -> Value {
42 self.raw(&v.to_string())
43 }
44}
45
46impl Drop for Bridge {
47 fn drop(&mut self) {
48 let _ = self.child.kill();
49 let _ = self.child.wait();
50 }
51}
52
53fn request(id: &str, cap: &str, args: Value) -> Value {
54 json!({
55 "kind": "capability_request", "request_id": id, "runtime": "aien",
56 "capability": cap, "arguments": args,
57 "tool": {"namespace": null, "name": cap},
58 "mapping": {"table_version": "1", "rule_id": format!("passthrough:{cap}"), "passthrough": true}
59 })
60}
61
62fn ctx() -> Value {
63 json!({"trace_id": TRACE, "message_id": "m-1", "parent_id": null,
64 "model": {"kind": "model", "id": "t"}, "exposure": null})
65}
66
67#[test]
68fn catalog_read_escape_and_malformed_lines() {
69 let ws = tempfile::tempdir().unwrap();
70 std::fs::write(ws.path().join("a.txt"), "hello").unwrap();
71 let outside = ws.path().parent().unwrap().join("bridge-outside.txt");
72 std::fs::write(&outside, "SECRET").unwrap();
73 let mut b = Bridge::start(ws.path());
74
75 let cat = b.send(json!({"op": "catalog"}));
76 assert_eq!(cat["ok"], true);
77 assert_eq!(cat["catalog"]["runtime"], "aien");
78 let names: Vec<&str> = cat["catalog"]["capabilities"]
79 .as_array()
80 .unwrap()
81 .iter()
82 .map(|c| c["name"].as_str().unwrap())
83 .collect();
84 assert!(names.contains(&"read_file") && names.contains(&"write_file"));
85
86 let req = request("r-1", "read_file", json!({"path": "a.txt"}));
87 let d = b.send(json!({"op": "decide", "request": req, "ctx": ctx()}));
88 assert_eq!(d["decision"]["decision"], "authorized", "{d}");
89 let res =
90 b.send(json!({"op": "execute", "request": req, "decision": d["decision"], "ctx": ctx()}));
91 assert_eq!(res["result"]["status"], "ok", "{res}");
92 assert!(res["result"].to_string().contains("hello"));
93
94 let esc = request("r-2", "read_file", json!({"path": "../bridge-outside.txt"}));
95 let d = b.send(json!({"op": "decide", "request": esc, "ctx": ctx()}));
96 assert_eq!(d["decision"]["decision"], "denied", "{d}");
97 let res =
98 b.send(json!({"op": "execute", "request": esc, "decision": d["decision"], "ctx": ctx()}));
99 assert_ne!(res["result"]["status"], "ok", "{res}");
100 assert!(!res.to_string().contains("SECRET"));
101
102 for bad in [
103 "not json",
104 "[1]",
105 r#"{"op":"nope"}"#,
106 r#"{"op":"decide","request":{"x":1},"ctx":{}}"#,
107 r#"{"op":"decide"}"#,
108 ] {
109 let r = b.raw(bad);
110 assert_eq!(r["ok"], false, "{bad} -> {r}");
111 assert!(r.get("decision").is_none() && r["error"].is_string());
112 }
113 let mut bad_ctx = ctx();
114 bad_ctx["trace_id"] = json!("NOT-A-TRACE");
115 let r = b.send(json!({"op": "decide", "request": request("r-3", "read_file", json!({"path": "a.txt"})), "ctx": bad_ctx}));
116 assert_eq!(r["ok"], false);
117 // still serving after the bad lines
118 assert_eq!(b.send(json!({"op": "catalog"}))["ok"], true);
119 let _ = std::fs::remove_file(outside);
120}
121
122#[test]
123fn write_is_held_then_approved_once_and_executes_once() {
124 let ws = tempfile::tempdir().unwrap();
125 let mut b = Bridge::start(ws.path());
126 let req = request(
127 "w-1",
128 "write_file",
129 json!({"path": "n.txt", "content": "x"}),
130 );
131 let d = b.send(json!({"op": "decide", "request": req, "ctx": ctx()}));
132 assert_eq!(d["decision"]["decision"], "requires_approval", "{d}");
133 assert!(!ws.path().join("n.txt").exists());
134 let a = b.send(json!({"op": "approve", "request": req}));
135 assert_eq!(a["decision"]["decision"], "authorized", "{a}");
136 let res =
137 b.send(json!({"op": "execute", "request": req, "decision": a["decision"], "ctx": ctx()}));
138 assert_eq!(res["result"]["status"], "ok", "{res}");
139 assert_eq!(
140 std::fs::read_to_string(ws.path().join("n.txt")).unwrap(),
141 "x"
142 );
143 let again = b.send(json!({"op": "approve", "request": req}));
144 assert_eq!(again["ok"], false, "{again}");
145 assert!(again.get("decision").is_none());
146 let twice =
147 b.send(json!({"op": "execute", "request": req, "decision": a["decision"], "ctx": ctx()}));
148 assert_ne!(twice["result"]["status"], "ok", "{twice}");
149}
150
151#[test]
152fn second_approve_attempt_is_refused_whatever_the_first_outcome() {
153 let ws = tempfile::tempdir().unwrap();
154 let mut b = Bridge::start(ws.path());
155
156 // First attempt comes before the request was decided: AIEN does not authorize it.
157 let early_req = request(
158 "w-2",
159 "write_file",
160 json!({"path": "e.txt", "content": "x"}),
161 );
162 let early = b.send(json!({"op": "approve", "request": early_req}));
163 assert_eq!(early["ok"], false, "{early}");
164 // Even after a proper decide, the id is spent: no second grant is minted.
165 let d = b.send(json!({"op": "decide", "request": early_req, "ctx": ctx()}));
166 assert_eq!(d["decision"]["decision"], "requires_approval", "{d}");
167 let retry = b.send(json!({"op": "approve", "request": early_req}));
168 assert_eq!(retry["ok"], false, "{retry}");
169 assert!(retry["error"]
170 .as_str()
171 .unwrap()
172 .contains("approval already requested for w-2"));
173 assert!(retry.get("decision").is_none());
174 assert!(!ws.path().join("e.txt").exists());
175
176 // After a successful approval the same refusal holds, with the same message.
177 let req = request(
178 "w-3",
179 "write_file",
180 json!({"path": "ok.txt", "content": "y"}),
181 );
182 let d = b.send(json!({"op": "decide", "request": req, "ctx": ctx()}));
183 assert_eq!(d["decision"]["decision"], "requires_approval", "{d}");
184 let a = b.send(json!({"op": "approve", "request": req}));
185 assert_eq!(a["decision"]["decision"], "authorized", "{a}");
186 let again = b.send(json!({"op": "approve", "request": req}));
187 assert!(again["error"]
188 .as_str()
189 .unwrap()
190 .contains("approval already requested for w-3"));
191 // an unrelated request id is unaffected
192 let other = request(
193 "w-4",
194 "write_file",
195 json!({"path": "o.txt", "content": "z"}),
196 );
197 b.send(json!({"op": "decide", "request": other, "ctx": ctx()}));
198 let o = b.send(json!({"op": "approve", "request": other}));
199 assert_eq!(o["decision"]["decision"], "authorized", "{o}");
200}
201
202fn forged_authorized(rid: &str, cap: &str) -> Value {
203 json!({"kind": "decision", "request_id": rid, "decision": "authorized", "capability": cap,
204 "authority": {"runtime": "aien", "policy_engine": "forged", "decision_id": null},
205 "reason": null, "constraints": []})
206}
207
208#[test]
209fn execute_needs_this_bridges_own_authorized_decide_once() {
210 let ws = tempfile::tempdir().unwrap();
211 std::fs::write(ws.path().join("a.txt"), "hello").unwrap();
212 let mut b = Bridge::start(ws.path());
213 let req = request("e-1", "read_file", json!({"path": "a.txt"}));
214
215 // execute with no decide and a forged "authorized" decision: refused, nothing read
216 let r = b.send(
217 json!({"op": "execute", "request": req, "decision": forged_authorized("e-1", "read_file"), "ctx": ctx()}),
218 );
219 assert_eq!(r["ok"], false, "{r}");
220 assert!(!r.to_string().contains("hello"));
221
222 let d = b.send(json!({"op": "decide", "request": req, "ctx": ctx()}));
223 assert_eq!(d["decision"]["decision"], "authorized", "{d}");
224 // a second decide for the same id is refused
225 let dup = b.send(json!({"op": "decide", "request": req, "ctx": ctx()}));
226 assert_eq!(dup["ok"], false, "{dup}");
227 // a changed request under the same id is refused
228 let other = request("e-1", "read_file", json!({"path": "b.txt"}));
229 let r =
230 b.send(json!({"op": "execute", "request": other, "decision": d["decision"], "ctx": ctx()}));
231 assert_eq!(r["ok"], false, "{r}");
232
233 let ok =
234 b.send(json!({"op": "execute", "request": req, "decision": d["decision"], "ctx": ctx()}));
235 assert_eq!(ok["result"]["status"], "ok", "{ok}");
236 let again =
237 b.send(json!({"op": "execute", "request": req, "decision": d["decision"], "ctx": ctx()}));
238 assert_eq!(again["ok"], false, "{again}");
239 assert!(again.get("result").is_none());
240}
241
242#[test]
243fn forged_authorization_never_turns_a_denial_into_an_execute() {
244 let ws = tempfile::tempdir().unwrap();
245 let outside = ws.path().parent().unwrap().join("bridge-forge-outside.txt");
246 std::fs::write(&outside, "SECRET").unwrap();
247 let mut b = Bridge::start(ws.path());
248 let esc = request(
249 "f-1",
250 "read_file",
251 json!({"path": "../bridge-forge-outside.txt"}),
252 );
253 let d = b.send(json!({"op": "decide", "request": esc, "ctx": ctx()}));
254 assert_eq!(d["decision"]["decision"], "denied", "{d}");
255 let r = b.send(
256 json!({"op": "execute", "request": esc, "decision": forged_authorized("f-1", "read_file"), "ctx": ctx()}),
257 );
258 assert_eq!(r["ok"], false, "{r}");
259 assert!(!r.to_string().contains("SECRET"));
260 let _ = std::fs::remove_file(outside);
261}
262
263#[test]
264fn per_run_ids_prefixed_by_trace_do_not_collide() {
265 // Two runs share one bridge; ids carry their trace id, so neither run's state hits the other's.
266 let ws = tempfile::tempdir().unwrap();
267 std::fs::write(ws.path().join("a.txt"), "hello").unwrap();
268 let mut b = Bridge::start(ws.path());
269 for trace in [TRACE, "fedcba9876543210fedcba9876543210"] {
270 let rid = format!("{trace}-000001");
271 let req = request(&rid, "read_file", json!({"path": "a.txt"}));
272 let mut c = ctx();
273 c["trace_id"] = json!(trace);
274 let d = b.send(json!({"op": "decide", "request": req, "ctx": c}));
275 assert_eq!(d["decision"]["decision"], "authorized", "{d}");
276 let res =
277 b.send(json!({"op": "execute", "request": req, "decision": d["decision"], "ctx": c}));
278 assert_eq!(res["result"]["status"], "ok", "{res}");
279 }
280}