luft-adapters 0.3.2

ACP backend adapter for Luft
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Non-interactive permission decisions for ACP tool requests.
//!
//! v0.1 never blocks on a human: every `session/request_permission` is decided
//! synchronously from the task's [`ToolPolicy`]. The decision logic ([`decide`])
//! is pure and unit-tested; [`extract_inputs`] adapts an ACP request into the
//! pure inputs.

use agent_client_protocol::schema::RequestPermissionRequest;
use luft_core::contract::backend::ToolPolicy;

/// Outcome of an automatic permission decision.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Decision {
    /// Approve (the handler selects the first offered option).
    Approve,
    /// Deny with a human-readable reason.
    Deny(String),
}

/// Request facts extracted from an ACP permission request, decoupled from the
/// ACP schema so [`decide`] stays pure and testable.
#[derive(Debug, Clone, Default)]
pub struct PermissionInputs {
    /// Shell command being requested, if any.
    pub command: Option<String>,
    /// Whether this is a file edit/write request.
    pub is_file_edit: bool,
    /// MCP tool name, if this is an MCP tool request.
    pub mcp_tool: Option<String>,
}

/// Decide whether to approve a tool request given the task's policy.
///
/// With **no policy** we default to approve so a self-contained agent (opencode)
/// can do its work in v0.1. With a policy we honour, in order:
/// deny-list → `accept_edits` → `allow_commands` → `allow_mcp`.
pub fn decide(policy: Option<&ToolPolicy>, input: &PermissionInputs) -> Decision {
    if let Some(tool) = &input.mcp_tool {
        if tool == "structured_output" {
            return Decision::Approve;
        }
    }

    let policy = match policy {
        None => return Decision::Approve,
        Some(p) => p,
    };

    // Deny list wins.
    if let Some(cmd) = &input.command {
        if policy.deny.iter().any(|d| cmd.contains(d.as_str())) {
            tracing::warn!(%cmd, "permission denied: command matches deny list");
            return Decision::Deny(format!("command matches deny list: {cmd}"));
        }
    }

    if input.is_file_edit {
        return if policy.accept_edits {
            tracing::debug!("permission: file edit approved");
            Decision::Approve
        } else {
            tracing::warn!("permission denied: file edit not allowed");
            Decision::Deny("accept_edits=false".into())
        };
    }

    if let Some(cmd) = &input.command {
        return if policy
            .allow_commands
            .iter()
            .any(|p| cmd.starts_with(p.as_str()))
        {
            tracing::debug!(%cmd, "permission: command approved");
            Decision::Approve
        } else {
            tracing::warn!(%cmd, "permission denied: command not in allowlist");
            Decision::Deny(format!("command not in allowlist: {cmd}"))
        };
    }

    if let Some(tool) = &input.mcp_tool {
        return if policy.allow_mcp.iter().any(|n| n == tool) {
            tracing::debug!(%tool, "permission: MCP tool approved");
            Decision::Approve
        } else {
            tracing::warn!(%tool, "permission denied: MCP tool not allowed");
            Decision::Deny(format!("mcp tool not allowed: {tool}"))
        };
    }

    // Unknown request type with a policy present: fail-closed (deny).
    Decision::Deny("unrecognized request type".into())
}

/// Best-effort extraction of [`PermissionInputs`] from an ACP request.
///
/// The ACP request shape is macro-generated; rather than depend on exact nested
/// types we inspect the serialized JSON heuristically. Since the default policy
/// is `None` (→ approve), this only affects policy-constrained runs.
pub fn extract_inputs(req: &RequestPermissionRequest) -> PermissionInputs {
    let v = serde_json::to_value(req).unwrap_or(serde_json::Value::Null);
    parse_inputs_from_json(&v)
}

/// Extract [`PermissionInputs`] from an already-serialized JSON value.
///
/// Extracted for testing — the fallback to [`serde_json::Value::Null`] inside
/// [`extract_inputs`] is covered by calling this with `Null` directly.
fn parse_inputs_from_json(v: &serde_json::Value) -> PermissionInputs {
    let raw = v.to_string();
    let is_file_edit =
        find_str_field(v, "kind").as_deref() == Some("edit") || raw.contains("write_text_file");
    let command = find_str_field(v, "command");
    let mcp_tool = find_str_field(v, "tool")
        .or_else(|| find_str_field(v, "name"))
        .filter(|_n| raw.contains("mcp") || raw.contains("structured_output"));
    PermissionInputs {
        command,
        is_file_edit,
        mcp_tool,
    }
}

/// Find the first string value for `key` anywhere in a JSON tree.
fn find_str_field(v: &serde_json::Value, key: &str) -> Option<String> {
    match v {
        serde_json::Value::Object(map) => {
            if let Some(serde_json::Value::String(s)) = map.get(key) {
                return Some(s.clone());
            }
            map.values().find_map(|child| find_str_field(child, key))
        }
        serde_json::Value::Array(arr) => arr.iter().find_map(|child| find_str_field(child, key)),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn policy(accept_edits: bool, allow: &[&str], deny: &[&str]) -> ToolPolicy {
        ToolPolicy {
            accept_edits,
            allow_commands: allow.iter().map(|s| s.to_string()).collect(),
            allow_mcp: vec![],
            deny: deny.iter().map(|s| s.to_string()).collect(),
        }
    }

    #[test]
    fn no_policy_approves() {
        assert_eq!(
            decide(None, &PermissionInputs::default()),
            Decision::Approve
        );
    }

    #[test]
    fn deny_list_blocks_command() {
        let p = policy(true, &["rm"], &["rm -rf"]);
        let input = PermissionInputs {
            command: Some("rm -rf /".into()),
            ..Default::default()
        };
        assert!(matches!(decide(Some(&p), &input), Decision::Deny(_)));
    }

    #[test]
    fn accept_edits_gate() {
        let edit = PermissionInputs {
            is_file_edit: true,
            ..Default::default()
        };
        assert_eq!(
            decide(Some(&policy(true, &[], &[])), &edit),
            Decision::Approve
        );
        assert!(matches!(
            decide(Some(&policy(false, &[], &[])), &edit),
            Decision::Deny(_)
        ));
    }

    #[test]
    fn allow_commands_prefix() {
        let p = policy(false, &["cargo"], &[]);
        let ok = PermissionInputs {
            command: Some("cargo test".into()),
            ..Default::default()
        };
        let bad = PermissionInputs {
            command: Some("curl evil".into()),
            ..Default::default()
        };
        assert_eq!(decide(Some(&p), &ok), Decision::Approve);
        assert!(matches!(decide(Some(&p), &bad), Decision::Deny(_)));
    }

    // --- structured_output early return (lines 38-41) ---
    #[test]
    fn structured_output_approved() {
        let input = PermissionInputs {
            mcp_tool: Some("structured_output".into()),
            ..Default::default()
        };
        // No policy
        assert_eq!(decide(None, &input), Decision::Approve);
        // With policy that would otherwise deny
        let p = ToolPolicy {
            accept_edits: false,
            allow_commands: vec![],
            allow_mcp: vec![],
            deny: vec![],
        };
        assert_eq!(decide(Some(&p), &input), Decision::Approve);
    }

    // --- MCP tool allowed (lines 77-80) ---
    #[test]
    fn mcp_tool_allowed() {
        let p = ToolPolicy {
            accept_edits: false,
            allow_commands: vec![],
            allow_mcp: vec!["my_tool".into()],
            deny: vec![],
        };
        let input = PermissionInputs {
            mcp_tool: Some("my_tool".into()),
            ..Default::default()
        };
        assert_eq!(decide(Some(&p), &input), Decision::Approve);
    }

    // --- MCP tool denied (lines 77, 82-83) ---
    #[test]
    fn mcp_tool_denied() {
        let p = policy(false, &[], &[]);
        let input = PermissionInputs {
            mcp_tool: Some("unknown_tool".into()),
            ..Default::default()
        };
        assert!(matches!(decide(Some(&p), &input), Decision::Deny(_)));
    }

    // --- catch-all deny (fail-closed) ---
    #[test]
    fn unknown_request_with_policy_denies() {
        let p = policy(false, &[], &[]);
        // Catch-all is fail-closed: unrecognized request types are denied
        // regardless of default_decision.
        assert_eq!(
            decide(Some(&p), &PermissionInputs::default()),
            Decision::Deny("unrecognized request type".into())
        );
    }

    #[test]
    fn unknown_request_with_policy_and_no_rules_denies() {
        // Even with no rules, an unrecognized request type should be denied
        // (fail-closed catch-all).
        let p = policy(false, &[], &[]);
        let input = PermissionInputs {
            ..Default::default()
        };
        let decision = decide(Some(&p), &input);
        assert!(matches!(decision, Decision::Deny(_)));
    }

    // --- find_str_field: nested object (lines 113-119) ---
    #[test]
    fn find_str_field_nested_object() {
        let v: serde_json::Value = serde_json::json!({"outer": {"inner": {"target": "found"}}});
        assert_eq!(find_str_field(&v, "target"), Some("found".into()));
    }

    // --- find_str_field: array (line 121) ---
    #[test]
    fn find_str_field_in_array() {
        let v: serde_json::Value = serde_json::json!([{"k": "a"}, {"k": "b"}]);
        assert_eq!(find_str_field(&v, "k"), Some("a".into()));
    }

    // --- find_str_field: scalar returns None (line 122) ---
    #[test]
    fn find_str_field_scalar() {
        let v: serde_json::Value = serde_json::json!("string");
        assert_eq!(find_str_field(&v, "key"), None);
    }

    // --- find_str_field: key absent (line 119 fallthrough) ---
    #[test]
    fn find_str_field_missing_key() {
        let v: serde_json::Value = serde_json::json!({"a": 1, "b": 2});
        assert_eq!(find_str_field(&v, "missing"), None);
    }

    // --- extract_inputs: command (lines 96-110) ---
    #[test]
    fn extract_inputs_command() {
        let req: RequestPermissionRequest = serde_json::from_value(serde_json::json!({
            "sessionId": "s1",
            "toolCall": {
                "toolCallId": "t1",
                "kind": "execute",
                "rawInput": { "command": "cargo build" }
            },
            "options": [],
            "_meta": null
        }))
        .unwrap();
        let inputs = extract_inputs(&req);
        assert_eq!(inputs.command.as_deref(), Some("cargo build"));
        assert!(!inputs.is_file_edit);
        assert!(inputs.mcp_tool.is_none());
    }

    // --- extract_inputs: file edit via kind=="edit" (lines 99-100) ---
    #[test]
    fn extract_inputs_file_edit_via_kind() {
        let req: RequestPermissionRequest = serde_json::from_value(serde_json::json!({
            "sessionId": "s1",
            "toolCall": {
                "toolCallId": "t1",
                "kind": "edit",
                "rawInput": {}
            },
            "options": [],
            "_meta": null
        }))
        .unwrap();
        let inputs = extract_inputs(&req);
        assert!(inputs.is_file_edit);
        assert_eq!(inputs.command, None);
        assert!(inputs.mcp_tool.is_none());
    }

    // --- extract_inputs: file edit via write_text_file (lines 99-100) ---
    #[test]
    fn extract_inputs_file_edit_via_write_text_file() {
        let req: RequestPermissionRequest = serde_json::from_value(serde_json::json!({
            "sessionId": "s1",
            "toolCall": {
                "toolCallId": "t1",
                "rawInput": { "write_text_file": {} }
            },
            "options": [],
            "_meta": null
        }))
        .unwrap();
        let inputs = extract_inputs(&req);
        assert!(inputs.is_file_edit);
    }

    // --- extract_inputs: MCP tool via "tool" field (lines 102-104) ---
    #[test]
    fn extract_inputs_mcp_tool() {
        let req: RequestPermissionRequest = serde_json::from_value(serde_json::json!({
            "sessionId": "s1",
            "toolCall": {
                "toolCallId": "t1",
                "rawInput": { "tool": "my_mcp_tool" }
            },
            "options": [],
            "_meta": null
        }))
        .unwrap();
        let inputs = extract_inputs(&req);
        assert_eq!(inputs.mcp_tool.as_deref(), Some("my_mcp_tool"));
    }

    // --- extract_inputs: MCP tool via "name" fallback (lines 102-104) ---
    #[test]
    fn extract_inputs_mcp_tool_via_name() {
        let req: RequestPermissionRequest = serde_json::from_value(serde_json::json!({
            "sessionId": "s1",
            "toolCall": {
                "toolCallId": "t1",
                "rawInput": { "name": "mcp_helper" }
            },
            "options": [],
            "_meta": null
        }))
        .unwrap();
        let inputs = extract_inputs(&req);
        assert_eq!(inputs.mcp_tool.as_deref(), Some("mcp_helper"));
    }

    // --- extract_inputs: MCP filter rejects non-mcp (lines 102-104 filter) ---
    #[test]
    fn extract_inputs_no_mcp_without_keyword() {
        let req: RequestPermissionRequest = serde_json::from_value(serde_json::json!({
            "sessionId": "s1",
            "toolCall": {
                "toolCallId": "t1",
                "rawInput": { "tool": "some_tool" }
            },
            "options": [],
            "_meta": null
        }))
        .unwrap();
        let inputs = extract_inputs(&req);
        assert!(inputs.mcp_tool.is_none());
    }

    // --- parse_inputs_from_json with Value::Null (unwrap_or fallback, line 97) ---
    #[test]
    fn parse_inputs_from_json_null_fallback() {
        let inputs = parse_inputs_from_json(&serde_json::Value::Null);
        assert_eq!(inputs.command, None);
        assert!(!inputs.is_file_edit);
        assert_eq!(inputs.mcp_tool, None);
    }

    // --- deny list takes priority over file edit (lines 50-55 vs 57-65) ---
    #[test]
    fn deny_list_wins_over_file_edit() {
        let p = policy(true, &[], &["rm -rf"]);
        let input = PermissionInputs {
            command: Some("rm -rf /".into()),
            is_file_edit: true,
            ..Default::default()
        };
        // deny-list check runs first, so this is Deny even though accept_edits=true
        assert!(matches!(decide(Some(&p), &input), Decision::Deny(_)));
    }

    // --- extract_inputs: both command and mcp_tool (lines 96-110) ---
    #[test]
    fn extract_inputs_command_and_mcp_tool() {
        let req: RequestPermissionRequest = serde_json::from_value(serde_json::json!({
            "sessionId": "s1",
            "toolCall": {
                "toolCallId": "t1",
                "kind": "execute",
                "rawInput": {
                    "command": "cargo build",
                    "tool": "my_mcp",
                    "name": "my_mcp"
                }
            },
            "options": [],
            "_meta": null
        }))
        .unwrap();
        let inputs = extract_inputs(&req);
        assert_eq!(inputs.command.as_deref(), Some("cargo build"));
        assert!(!inputs.is_file_edit);
        // mcp_tool is set because raw contains "mcp"
        assert_eq!(inputs.mcp_tool.as_deref(), Some("my_mcp"));
    }
}