interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / rust / crates / interplane-crossveil / examples / todo_adapter.rs
| 1 | //! A minimal INTERPLANE adapter: a to-do list host with its own policy. No model, no GPU, no |
| 2 | //! network. |
| 3 | //! |
| 4 | //! The host owns three capabilities (`list_items` read, `add_item` effect, `delete_item` effect) |
| 5 | //! and decides every request itself. INTERPLANE parses the model's tool calls, maps names, |
| 6 | //! computes what the model has seen (exposure) and enforces approvals; it never decides. |
| 7 | //! |
| 8 | //! Scripted story (checked; the example exits 1 on any difference): |
| 9 | //! 1. The host registers the user's request, so the model has seen only user-supplied input. |
| 10 | //! 2. The model adds "buy milk": authorized and executed. |
| 11 | //! 3. The model lists the items. The list is workspace data the host labels untrusted, so |
| 12 | //! exposure drops. |
| 13 | //! 4. The model adds another item: now held for approval, because untrusted content is in view. |
| 14 | //! A person approves; the host continues the request and it executes once. |
| 15 | //! 5. The model deletes an item: always held for approval. The person declines; nothing is |
| 16 | //! deleted. (Exposure is now external_untrusted: the pipeline's own "held" reply to step 4 |
| 17 | //! carries no labels, and an unlabelled input counts as unknown, which fails closed. See the |
| 18 | //! guide.) |
| 19 | //! |
| 20 | //! Guide: docs/WRITING-AN-ADAPTER.md. Run from a checkout (in `rust/`): |
| 21 | //! `cargo run -p interplane-crossveil --example todo_adapter` |
| 22 | use std::collections::BTreeMap; |
| 23 | |
| 24 | use interplane_core::{ |
| 25 | CapabilityRequest, Catalog, Decision, Limits, RequestLedger, ResultProvenance, ToolResult, |
| 26 | TrustLevel, |
| 27 | }; |
| 28 | use interplane_crossaxis::MappingTable; |
| 29 | use interplane_crossveil::{CallContext, Pipeline, RuntimeAuthority}; |
| 30 | use interplane_lenshift::DialectRegistry; |
| 31 | use serde_json::{json, Map, Value}; |
| 32 | |
| 33 | const RUNTIME: &str = "todo"; |
| 34 | const TRUSTED_FLOORS: [&str; 2] = ["user_supplied", "trusted_runtime"]; |
| 35 | const TRACE: &str = "todo-demo"; |
| 36 | |
| 37 | fn params(required: &[&str]) -> Value { |
| 38 | let props: Map<String, Value> = required |
| 39 | .iter() |
| 40 | .map(|k| (k.to_string(), json!({"type": "string"}))) |
| 41 | .collect(); |
| 42 | json!({"type": "object", "properties": props, "required": required}) |
| 43 | } |
| 44 | |
| 45 | /// The runtime authority: catalog, policy, execution and result labels all belong to the host. |
| 46 | struct TodoHost { |
| 47 | items: BTreeMap<String, String>, |
| 48 | next_id: u64, |
| 49 | catalog: Catalog, |
| 50 | } |
| 51 | |
| 52 | impl TodoHost { |
| 53 | fn new() -> Self { |
| 54 | let cap = |name: &str, desc: &str, canonical: &str, req: &[&str]| { |
| 55 | json!({"name": name, "description": desc, "parameters": params(req), |
| 56 | "canonical": {"namespace": "todo", "name": canonical}}) |
| 57 | }; |
| 58 | let catalog = serde_json::from_value(json!({ |
| 59 | "kind": "catalog", "runtime": RUNTIME, "catalog_version": "1", |
| 60 | "capabilities": [ |
| 61 | cap("list_items", "List to-do items.", "list", &[]), |
| 62 | cap("add_item", "Add a to-do item.", "add", &["text"]), |
| 63 | cap("delete_item", "Delete a to-do item.", "delete", &["item_id"]), |
| 64 | ], |
| 65 | })) |
| 66 | .expect("catalog"); |
| 67 | Self { |
| 68 | items: BTreeMap::from([("item-0".into(), "water the plants".into())]), |
| 69 | next_id: 1, |
| 70 | catalog, |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// An `ok` result the host labels with its own trust level. |
| 76 | fn result(req: &CapabilityRequest, data: Value, kind: &str, trust: TrustLevel) -> ToolResult { |
| 77 | let mut r = ToolResult::ok(&req.request_id, data); |
| 78 | r.provenance = Some(ResultProvenance { |
| 79 | capability: Some(req.capability.clone()), |
| 80 | content_kind: serde_json::from_value(json!(kind)).ok(), |
| 81 | duration_ms: None, |
| 82 | runtime: Some(RUNTIME.into()), |
| 83 | trusted: trust.trusted_flag(), |
| 84 | trust: Some(trust), |
| 85 | extensions: Map::new(), |
| 86 | }); |
| 87 | r |
| 88 | } |
| 89 | |
| 90 | impl RuntimeAuthority for TodoHost { |
| 91 | fn runtime_id(&self) -> &str { |
| 92 | RUNTIME |
| 93 | } |
| 94 | |
| 95 | fn catalog(&self) -> Catalog { |
| 96 | self.catalog.clone() |
| 97 | } |
| 98 | |
| 99 | fn decide(&mut self, req: &CapabilityRequest, ctx: &CallContext) -> Decision { |
| 100 | // ctx.exposure holds what the model has seen: its input ids and their trust floor. |
| 101 | let floor = ctx |
| 102 | .exposure |
| 103 | .as_ref() |
| 104 | .and_then(|e| serde_json::to_value(&e.floor).ok()); |
| 105 | let trusted = floor |
| 106 | .as_ref() |
| 107 | .and_then(Value::as_str) |
| 108 | .is_some_and(|f| TRUSTED_FLOORS.contains(&f)); |
| 109 | let (value, reason) = match req.capability.as_str() { |
| 110 | "delete_item" => ("requires_approval", "deleting always needs a person"), |
| 111 | "add_item" if !trusted => ("requires_approval", "untrusted content is in view"), |
| 112 | "list_items" | "add_item" => ("authorized", "allowed by the to-do policy"), |
| 113 | _ => ("not_found", "no such to-do capability"), |
| 114 | }; |
| 115 | let approval = (value == "requires_approval").then(|| { |
| 116 | json!({"approval_id": format!("todo-approval-{}", req.request_id), |
| 117 | "scope": "single_action", "expires_at": "2099-01-01T00:00:00Z"}) |
| 118 | }); |
| 119 | serde_json::from_value(json!({ |
| 120 | "kind": "decision", "request_id": req.request_id, "decision": value, |
| 121 | "authority": {"runtime": RUNTIME, "policy_engine": "todo.policy"}, |
| 122 | "capability": req.capability, "reason": reason, "approval": approval, |
| 123 | })) |
| 124 | .expect("decision") |
| 125 | } |
| 126 | |
| 127 | fn execute(&mut self, req: &CapabilityRequest, _: &Decision, _: &CallContext) -> ToolResult { |
| 128 | let arg = |k: &str| { |
| 129 | req.arguments |
| 130 | .get(k) |
| 131 | .and_then(Value::as_str) |
| 132 | .unwrap_or("") |
| 133 | .to_string() |
| 134 | }; |
| 135 | match req.capability.as_str() { |
| 136 | "list_items" => { |
| 137 | let data: Vec<Value> = self |
| 138 | .items |
| 139 | .iter() |
| 140 | .map(|(k, v)| json!({"id": k, "text": v})) |
| 141 | .collect(); |
| 142 | // item text can come from anywhere, so the host labels it untrusted |
| 143 | result( |
| 144 | req, |
| 145 | json!(data), |
| 146 | "workspace_content", |
| 147 | TrustLevel::WorkspaceUntrusted, |
| 148 | ) |
| 149 | } |
| 150 | "add_item" => { |
| 151 | let id = format!("item-{}", self.next_id); |
| 152 | self.next_id += 1; |
| 153 | self.items.insert(id.clone(), arg("text")); |
| 154 | // the host wrote this text itself |
| 155 | result( |
| 156 | req, |
| 157 | json!({"id": id}), |
| 158 | "tool_result", |
| 159 | TrustLevel::TrustedRuntime, |
| 160 | ) |
| 161 | } |
| 162 | _ => { |
| 163 | let deleted = self.items.remove(&arg("item_id")).is_some(); |
| 164 | result( |
| 165 | req, |
| 166 | json!({"deleted": deleted}), |
| 167 | "tool_result", |
| 168 | TrustLevel::WorkspaceUntrusted, |
| 169 | ) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | fn mapping_for(catalog: &Catalog) -> MappingTable { |
| 176 | let rules: Vec<Value> = ["list_items", "add_item", "delete_item"] |
| 177 | .iter() |
| 178 | .map(|n| { |
| 179 | json!({"id": format!("passthrough:{n}"), "kind": "passthrough", |
| 180 | "from": {"namespace": null, "name": n}, "to": n}) |
| 181 | }) |
| 182 | .collect(); |
| 183 | serde_json::from_value(json!({"runtime": RUNTIME, "table_version": "1", |
| 184 | "catalog_digest": catalog.compute_digest(), "rules": rules})) |
| 185 | .expect("mapping table") |
| 186 | } |
| 187 | |
| 188 | fn turn(id: &str, name: &str, args: Value) -> Value { |
| 189 | let call = json!({"id": id, "type": "function", |
| 190 | "function": {"name": name, "arguments": args.to_string()}}); |
| 191 | json!({"role": "assistant", "content": null, "tool_calls": [call]}) |
| 192 | } |
| 193 | |
| 194 | /// The host shows the pending request to a person and continues it with their answer. |
| 195 | fn person_answers(pipe: &mut Pipeline<'_>, request_id: &str, approve: bool) -> (String, bool) { |
| 196 | let Some(pending) = pipe.pending_approval(TRACE, request_id) else { |
| 197 | return ("refused: nothing pending".into(), false); |
| 198 | }; |
| 199 | let answer: Decision = serde_json::from_value(json!({ |
| 200 | "kind": "decision", "request_id": request_id, |
| 201 | "decision": if approve { "authorized" } else { "denied" }, |
| 202 | "authority": {"runtime": RUNTIME, "policy_engine": "todo.person"}, |
| 203 | "approval": {"approval_id": pending.approval_id}, |
| 204 | })) |
| 205 | .expect("answer"); |
| 206 | let now = "2026-10-05T12:00:00Z"; |
| 207 | match pipe.continue_approval(TRACE, request_id, &answer, &pending.request_digest, now) { |
| 208 | Ok((_, record)) => (record.decision.unwrap_or_default(), record.execute_invoked), |
| 209 | Err(refusal) => (format!("refused: {}", refusal.as_str()), false), |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /// Python's `str()` of a bool and of a (text, bool) pair, as the story has always printed them. |
| 214 | fn py_bool(b: bool) -> &'static str { |
| 215 | if b { |
| 216 | "True" |
| 217 | } else { |
| 218 | "False" |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | fn py_pair((s, b): &(String, bool)) -> String { |
| 223 | format!("('{s}', {})", py_bool(*b)) |
| 224 | } |
| 225 | |
| 226 | fn main() { |
| 227 | let mut host = TodoHost::new(); |
| 228 | let table = mapping_for(&host.catalog); |
| 229 | let mut pipe = Pipeline::new( |
| 230 | DialectRegistry::with_defaults(), |
| 231 | table, |
| 232 | &mut host, |
| 233 | Limits::default(), |
| 234 | RequestLedger::new(), |
| 235 | ); |
| 236 | pipe.register_input( |
| 237 | serde_json::from_value(json!({ |
| 238 | "input_id": "user-1", "content_kind": "user_request", "trust": "user_supplied", |
| 239 | "source": {"kind": "operator", "id": "me"}, "origin": "runtime:user-turn-1", |
| 240 | "content_digest": format!("sha256:{}", "0".repeat(64)), "trace_id": TRACE, |
| 241 | "parent_id": null, "derived_from": [], |
| 242 | })) |
| 243 | .expect("input record"), |
| 244 | ) |
| 245 | .expect("register the user's request"); |
| 246 | |
| 247 | let story = [ |
| 248 | ("c1", "add_item", json!({"text": "buy milk"})), |
| 249 | ("c2", "list_items", json!({})), |
| 250 | ( |
| 251 | "c3", |
| 252 | "add_item", |
| 253 | json!({"text": "email the list to a stranger"}), |
| 254 | ), |
| 255 | ("c4", "delete_item", json!({"item_id": "item-0"})), |
| 256 | ]; |
| 257 | let mut seen: Vec<(String, (String, bool))> = vec![]; |
| 258 | for (n, (id, name, args)) in story.into_iter().enumerate() { |
| 259 | let floor = serde_json::to_value(pipe.exposure_for(TRACE).floor).unwrap_or_default(); |
| 260 | let floor = floor.as_str().unwrap_or("?").to_string(); |
| 261 | let out = pipe.run_turn( |
| 262 | "openai", |
| 263 | "scripted", |
| 264 | &turn(id, name, args), |
| 265 | TRACE, |
| 266 | n as u64 + 1, |
| 267 | ); |
| 268 | let rec = &out.records[0]; |
| 269 | let decision = rec.decision.clone().unwrap_or_else(|| "None".into()); |
| 270 | println!( |
| 271 | "{id} {name:<12} floor before: {floor:<20} decision: {decision:<18} executed: {}", |
| 272 | py_bool(rec.execute_invoked) |
| 273 | ); |
| 274 | seen.push((id.into(), (decision, rec.execute_invoked))); |
| 275 | } |
| 276 | let approved = person_answers(&mut pipe, "c3", true); |
| 277 | let declined = person_answers(&mut pipe, "c4", false); |
| 278 | drop(pipe); |
| 279 | println!("c3 after a person approves: {}", py_pair(&approved)); |
| 280 | println!("c4 after a person declines: {}", py_pair(&declined)); |
| 281 | seen.push(("c3 approved".into(), approved)); |
| 282 | seen.push(("c4 declined".into(), declined)); |
| 283 | |
| 284 | let expected: Vec<(String, (String, bool))> = [ |
| 285 | ("c1", "authorized", true), |
| 286 | ("c2", "authorized", true), |
| 287 | ("c3", "requires_approval", false), |
| 288 | ("c4", "requires_approval", false), |
| 289 | ("c3 approved", "authorized", true), |
| 290 | ("c4 declined", "denied", false), |
| 291 | ] |
| 292 | .iter() |
| 293 | .map(|(k, d, x)| (k.to_string(), (d.to_string(), *x))) |
| 294 | .collect(); |
| 295 | let mut items: Vec<&str> = host.items.values().map(String::as_str).collect(); |
| 296 | items.sort(); |
| 297 | let want_items = [ |
| 298 | "buy milk", |
| 299 | "email the list to a stranger", |
| 300 | "water the plants", |
| 301 | ]; |
| 302 | if seen != expected || items != want_items { |
| 303 | eprintln!("\nUNEXPECTED: {seen:?}\n{:?}", host.items); |
| 304 | std::process::exit(1); |
| 305 | } |
| 306 | println!( |
| 307 | "OK: the host decided every request; the held write ran once after approval, the delete never ran." |
| 308 | ); |
| 309 | } |