codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Side-effect-free preparation of concrete tool inputs.
//!
//! The turn loop remains the authority orchestrator. This module only makes
//! the input-specific policy decision inspectable and reusable, including a
//! mandatory second preparation after a hook rewrites input.

use std::collections::BTreeSet;
use std::path::PathBuf;

use serde_json::Value;

use crate::mcp::McpPool;
use crate::tools::ToolRegistry;
use crate::tools::spec::{ApprovalRequirement, PreparedToolCall, ResourceClaim, ToolError};

use super::dispatch::{
    mcp_tool_approval_description, mcp_tool_is_parallel_safe, mcp_tool_is_read_only,
};
use super::tool_catalog::{CODE_EXECUTION_TOOL_NAME, JS_EXECUTION_TOOL_NAME, is_tool_search_tool};

#[derive(Debug, Clone, PartialEq)]
pub(super) struct PreparedToolPolicy {
    pub(super) call: PreparedToolCall,
    pub(super) auto_approve: bool,
}

/// Prepare a concrete call without mutating external state.
pub(super) fn prepare_tool_call(
    name: &str,
    input: Value,
    registry: Option<&ToolRegistry>,
    session_auto_approve: bool,
) -> Result<PreparedToolPolicy, ToolError> {
    if McpPool::is_mcp_tool(name) {
        let read_only = mcp_tool_is_read_only(name);
        if !read_only
            && let Some(authority) =
                registry.and_then(|registry| registry.context().tool_authority.as_ref())
        {
            return Err(ToolError::permission_denied(format!(
                "worker '{}' cannot run mutating MCP tool {name}: it has no bounded file target under the machine-readable authority envelope",
                authority.owner
            )));
        }
        return Ok(PreparedToolPolicy {
            call: PreparedToolCall {
                name: name.to_string(),
                input,
                description: mcp_tool_approval_description(name),
                read_only,
                supports_parallel: mcp_tool_is_parallel_safe(name),
                starts_detached: false,
                approval: if read_only {
                    ApprovalRequirement::Auto
                } else {
                    ApprovalRequirement::Suggest
                },
                resources: vec![ResourceClaim::GlobalExclusive],
            },
            auto_approve: session_auto_approve,
        });
    }

    if let Some(registry) = registry
        && let Some(spec) = registry.get(name)
    {
        let mut call = spec.prepare(input, registry.context())?;
        call.resources = registered_resource_claims(name, &call.input, registry.context())?;
        return Ok(PreparedToolPolicy {
            call,
            auto_approve: registry.context().auto_approve,
        });
    }

    if name == CODE_EXECUTION_TOOL_NAME {
        reject_unbounded_execution_under_authority(name, registry)?;
        return Ok(conservative_execution_policy(
            name,
            input,
            "Run model-provided Python code in local execution sandbox",
            session_auto_approve,
        ));
    }

    if name == JS_EXECUTION_TOOL_NAME {
        reject_unbounded_execution_under_authority(name, registry)?;
        return Ok(conservative_execution_policy(
            name,
            input,
            "Run model-provided JavaScript code in local Node.js execution sandbox",
            session_auto_approve,
        ));
    }

    if is_tool_search_tool(name) {
        return Ok(PreparedToolPolicy {
            call: PreparedToolCall {
                name: name.to_string(),
                input,
                description: "Search tool catalog".to_string(),
                read_only: true,
                supports_parallel: false,
                starts_detached: false,
                approval: ApprovalRequirement::Auto,
                resources: Vec::new(),
            },
            auto_approve: session_auto_approve,
        });
    }

    Err(ToolError::not_available(format!(
        "tool '{name}' has no preparation path"
    )))
}

fn reject_unbounded_execution_under_authority(
    name: &str,
    registry: Option<&ToolRegistry>,
) -> Result<(), ToolError> {
    let Some(authority) = registry.and_then(|registry| registry.context().tool_authority.as_ref())
    else {
        return Ok(());
    };
    Err(ToolError::permission_denied(format!(
        "worker '{}' cannot run {name}: arbitrary code execution cannot prove a bounded file target under the machine-readable authority envelope",
        authority.owner
    )))
}

/// Re-run preparation from the rewritten input rather than patching any
/// previously derived field.
pub(super) fn reprepare_tool_call_after_hook(
    name: &str,
    updated_input: Value,
    registry: Option<&ToolRegistry>,
    session_auto_approve: bool,
) -> Result<PreparedToolPolicy, ToolError> {
    prepare_tool_call(name, updated_input, registry, session_auto_approve)
}

fn conservative_execution_policy(
    name: &str,
    input: Value,
    description: &str,
    auto_approve: bool,
) -> PreparedToolPolicy {
    PreparedToolPolicy {
        call: PreparedToolCall {
            name: name.to_string(),
            input,
            description: description.to_string(),
            read_only: false,
            supports_parallel: false,
            starts_detached: false,
            approval: ApprovalRequirement::Suggest,
            resources: vec![ResourceClaim::GlobalExclusive],
        },
        auto_approve,
    }
}

fn registered_resource_claims(
    name: &str,
    input: &Value,
    context: &crate::tools::ToolContext,
) -> Result<Vec<ResourceClaim>, ToolError> {
    let canonical = crate::tools::canonical_action::canonical_action_alias(name, input);
    match canonical {
        "read_file" => path_claim(input, "path", None, context, ResourceClaim::ReadPath),
        "write_file" | "edit_file" => {
            path_claim(input, "path", None, context, ResourceClaim::WritePath)
        }
        "list_dir" | "grep_files" | "file_search" => {
            path_claim(input, "path", Some("."), context, ResourceClaim::ReadTree)
        }
        "apply_patch" => apply_patch_resource_claims(input, context),
        "terminal/run" => Ok(terminal_claim(input, "session", Some("term-1"))),
        "terminal/send" | "terminal/wait" | "terminal/cancel" | "terminal/reset" => {
            Ok(terminal_claim(input, "session", None))
        }
        "exec_shell_wait"
        | "exec_wait"
        | "exec_shell_interact"
        | "exec_interact"
        | "exec_shell_cancel" => Ok(terminal_claim(input, "task_id", None)),
        _ => Ok(global_exclusive_claim()),
    }
}

fn path_claim(
    input: &Value,
    key: &str,
    default: Option<&str>,
    context: &crate::tools::ToolContext,
    build: fn(PathBuf) -> ResourceClaim,
) -> Result<Vec<ResourceClaim>, ToolError> {
    let raw = input
        .get(key)
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|path| !path.is_empty())
        .or(default);
    let Some(raw) = raw else {
        return Ok(global_exclusive_claim());
    };
    Ok(context
        .resolve_path(raw)
        .map_or_else(|_| global_exclusive_claim(), |path| vec![build(path)]))
}

fn apply_patch_resource_claims(
    input: &Value,
    context: &crate::tools::ToolContext,
) -> Result<Vec<ResourceClaim>, ToolError> {
    let Ok(preflight) = crate::tools::apply_patch::preflight_apply_patch(input) else {
        return Ok(global_exclusive_claim());
    };
    if preflight.touched_files.is_empty() {
        return Ok(global_exclusive_claim());
    }

    let mut claims = BTreeSet::new();
    for path in preflight.touched_files {
        let Ok(path) = context.resolve_path(&path) else {
            return Ok(global_exclusive_claim());
        };
        claims.insert(ResourceClaim::WritePath(path));
    }
    Ok(claims.into_iter().collect())
}

fn terminal_claim(input: &Value, key: &str, default: Option<&str>) -> Vec<ResourceClaim> {
    input
        .get(key)
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|id| !id.is_empty())
        .or(default)
        .map_or_else(global_exclusive_claim, |id| {
            vec![ResourceClaim::Terminal(id.to_string())]
        })
}

fn global_exclusive_claim() -> Vec<ResourceClaim> {
    vec![ResourceClaim::GlobalExclusive]
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use async_trait::async_trait;
    use serde_json::json;
    use tempfile::tempdir;

    use crate::tools::spec::{ToolCapability, ToolContext, ToolResult, ToolSpec};

    use super::*;

    struct InputDependentTool;

    #[async_trait]
    impl ToolSpec for InputDependentTool {
        fn name(&self) -> &str {
            "input_dependent"
        }

        fn description(&self) -> &str {
            "characterization tool"
        }

        fn input_schema(&self) -> Value {
            json!({"type": "object"})
        }

        fn capabilities(&self) -> Vec<ToolCapability> {
            vec![ToolCapability::WritesFiles]
        }

        fn approval_requirement_for(&self, input: &Value) -> ApprovalRequirement {
            if input.get("safe").and_then(Value::as_bool) == Some(true) {
                ApprovalRequirement::Auto
            } else {
                ApprovalRequirement::Required
            }
        }

        fn is_read_only_for(&self, input: &Value) -> bool {
            input.get("safe").and_then(Value::as_bool) == Some(true)
        }

        fn supports_parallel_for(&self, input: &Value) -> bool {
            self.is_read_only_for(input)
        }

        fn starts_detached_for(&self, input: &Value) -> bool {
            input.get("detached").and_then(Value::as_bool) == Some(true)
        }

        async fn execute(
            &self,
            _input: Value,
            _context: &ToolContext,
        ) -> Result<ToolResult, ToolError> {
            unreachable!("preparation must not execute the tool")
        }
    }

    fn registry() -> (tempfile::TempDir, ToolRegistry) {
        let root = tempdir().expect("tempdir");
        let mut context = ToolContext::new(root.path().to_path_buf());
        context.auto_approve = true;
        let mut registry = ToolRegistry::new(context);
        registry.register(Arc::new(InputDependentTool));
        (root, registry)
    }

    #[test]
    fn prepared_policy_matches_existing_input_specific_decisions() {
        let (_root, registry) = registry();
        let spec = registry.get("input_dependent").expect("registered tool");

        for input in [
            json!({"safe": true, "detached": false}),
            json!({"safe": false, "detached": true}),
        ] {
            let prepared =
                prepare_tool_call("input_dependent", input.clone(), Some(&registry), false)
                    .expect("prepare");

            assert_eq!(
                prepared.call.approval,
                spec.approval_requirement_for(&input)
            );
            assert_eq!(prepared.call.read_only, spec.is_read_only_for(&input));
            assert_eq!(
                prepared.call.supports_parallel,
                spec.supports_parallel_for(&input)
            );
            assert_eq!(
                prepared.call.starts_detached,
                spec.starts_detached_for(&input)
            );
            assert!(prepared.auto_approve);
        }
    }

    #[test]
    fn hook_rewrite_discards_every_original_prepared_decision() {
        let (_root, registry) = registry();
        let original = prepare_tool_call(
            "input_dependent",
            json!({"safe": true, "detached": false}),
            Some(&registry),
            false,
        )
        .expect("prepare original");
        let rewritten = reprepare_tool_call_after_hook(
            "input_dependent",
            json!({"safe": false, "detached": true}),
            Some(&registry),
            false,
        )
        .expect("reprepare rewritten input");

        assert_eq!(original.call.approval, ApprovalRequirement::Auto);
        assert!(original.call.read_only);
        assert!(original.call.supports_parallel);
        assert!(!original.call.starts_detached);

        assert_eq!(rewritten.call.approval, ApprovalRequirement::Required);
        assert!(!rewritten.call.read_only);
        assert!(!rewritten.call.supports_parallel);
        assert!(rewritten.call.starts_detached);
        assert_eq!(
            rewritten.call.input,
            json!({"safe": false, "detached": true})
        );
    }

    #[test]
    fn bypass_preparation_preserves_legacy_policy_table() {
        struct Expected {
            name: &'static str,
            approval: ApprovalRequirement,
            read_only: bool,
            supports_parallel: bool,
            global_exclusive: bool,
        }

        for expected in [
            Expected {
                name: "read_mcp_resource",
                approval: ApprovalRequirement::Auto,
                read_only: true,
                supports_parallel: true,
                global_exclusive: true,
            },
            Expected {
                name: "mcp_filesystem_write",
                approval: ApprovalRequirement::Suggest,
                read_only: false,
                supports_parallel: false,
                global_exclusive: true,
            },
            Expected {
                name: CODE_EXECUTION_TOOL_NAME,
                approval: ApprovalRequirement::Suggest,
                read_only: false,
                supports_parallel: false,
                global_exclusive: true,
            },
            Expected {
                name: JS_EXECUTION_TOOL_NAME,
                approval: ApprovalRequirement::Suggest,
                read_only: false,
                supports_parallel: false,
                global_exclusive: true,
            },
            Expected {
                name: "tool_search",
                approval: ApprovalRequirement::Auto,
                read_only: true,
                supports_parallel: false,
                global_exclusive: false,
            },
        ] {
            let prepared = prepare_tool_call(expected.name, json!({}), None, false)
                .unwrap_or_else(|error| panic!("prepare {}: {error}", expected.name));
            assert_eq!(
                prepared.call.approval, expected.approval,
                "{}",
                expected.name
            );
            assert_eq!(
                prepared.call.read_only, expected.read_only,
                "{}",
                expected.name
            );
            assert_eq!(
                prepared.call.supports_parallel, expected.supports_parallel,
                "{}",
                expected.name
            );
            assert_eq!(
                prepared.call.resources == vec![ResourceClaim::GlobalExclusive],
                expected.global_exclusive,
                "{}",
                expected.name
            );
            assert!(!prepared.call.starts_detached, "{}", expected.name);
            assert!(!prepared.auto_approve, "{}", expected.name);
        }
    }

    #[test]
    fn mcp_write_preparation_respects_session_auto_approval() {
        let prepared = prepare_tool_call("mcp_filesystem_write", json!({}), None, true)
            .expect("prepare MCP write tool with session auto-approval");

        assert_eq!(prepared.call.approval, ApprovalRequirement::Suggest);
        assert!(!prepared.call.read_only);
        assert!(!prepared.call.supports_parallel);
        assert_eq!(
            prepared.call.resources,
            vec![ResourceClaim::GlobalExclusive]
        );
        assert!(prepared.auto_approve);
        assert!(!super::super::turn_loop::registered_tool_approval_required(
            &prepared.call.name,
            prepared.call.approval,
            prepared.auto_approve,
        ));
    }

    #[test]
    fn hook_rewrite_reprepares_resource_claims_from_final_input() {
        let root = tempdir().expect("tempdir");
        let context = ToolContext::new(root.path().to_path_buf());
        let original_path = context.resolve_path("before.rs").expect("original path");
        let rewritten_path = context.resolve_path("after.rs").expect("rewritten path");
        let mut registry = ToolRegistry::new(context);
        registry.register(Arc::new(crate::tools::file::ReadFileTool));

        let original = prepare_tool_call(
            "read_file",
            json!({"path": "before.rs"}),
            Some(&registry),
            false,
        )
        .expect("prepare original read");
        let rewritten = reprepare_tool_call_after_hook(
            "read_file",
            json!({"path": "after.rs"}),
            Some(&registry),
            false,
        )
        .expect("reprepare rewritten read");

        assert_eq!(
            original.call.resources,
            vec![ResourceClaim::ReadPath(original_path)]
        );
        assert_eq!(
            rewritten.call.resources,
            vec![ResourceClaim::ReadPath(rewritten_path)]
        );
    }

    #[test]
    fn registered_file_claims_are_canonical_and_input_specific() {
        let root = tempdir().expect("tempdir");
        let context = ToolContext::new(root.path().to_path_buf());
        let exact = context.resolve_path("src/lib.rs").expect("exact path");
        let tree = context.resolve_path("src").expect("tree path");

        assert_eq!(
            registered_resource_claims("read_file", &json!({"path": "src/lib.rs"}), &context,)
                .expect("read claim"),
            vec![ResourceClaim::ReadPath(exact.clone())]
        );
        assert_eq!(
            registered_resource_claims("edit_file", &json!({"path": "src/lib.rs"}), &context,)
                .expect("write claim"),
            vec![ResourceClaim::WritePath(exact)]
        );
        assert_eq!(
            registered_resource_claims("grep_files", &json!({"path": "src"}), &context)
                .expect("tree claim"),
            vec![ResourceClaim::ReadTree(tree)]
        );
        assert_eq!(
            registered_resource_claims("read_file", &json!({"path": "../../outside"}), &context,)
                .expect("path escape must fall back conservatively"),
            vec![ResourceClaim::GlobalExclusive]
        );
    }

    #[cfg(unix)]
    #[test]
    fn symlink_aliases_resolve_to_the_same_file_claim() {
        use std::os::unix::fs::symlink;

        let root = tempdir().expect("tempdir");
        let real_dir = root.path().join("real");
        std::fs::create_dir(&real_dir).expect("create real directory");
        std::fs::write(real_dir.join("lib.rs"), "fn main() {}\n").expect("write real file");
        symlink("real", root.path().join("alias")).expect("create directory symlink");
        let context = ToolContext::new(root.path().to_path_buf());
        let canonical_real = real_dir
            .join("lib.rs")
            .canonicalize()
            .expect("canonical file");

        let read_alias =
            registered_resource_claims("read_file", &json!({"path": "alias/lib.rs"}), &context)
                .expect("alias claim");
        let write_real =
            registered_resource_claims("write_file", &json!({"path": "real/lib.rs"}), &context)
                .expect("real claim");

        assert_eq!(read_alias, vec![ResourceClaim::ReadPath(canonical_real)]);
        assert!(read_alias[0].conflicts_with(&write_real[0]));
    }

    #[test]
    fn apply_patch_claims_every_resolved_target_or_falls_back_global() {
        let root = tempdir().expect("tempdir");
        let context = ToolContext::new(root.path().to_path_buf());
        let a = context.resolve_path("a.rs").expect("a path");
        let b = context.resolve_path("b.rs").expect("b path");

        let claims = registered_resource_claims(
            "apply_patch",
            &json!({
                "replace": [
                    {"path": "b.rs", "content": "b"},
                    {"path": "a.rs", "content": "a"}
                ]
            }),
            &context,
        )
        .expect("patch claims");
        assert_eq!(
            claims,
            vec![ResourceClaim::WritePath(a), ResourceClaim::WritePath(b)]
        );

        assert_eq!(
            registered_resource_claims(
                "apply_patch",
                &json!({"patch": "not a unified diff"}),
                &context,
            )
            .expect("fallback claim"),
            vec![ResourceClaim::GlobalExclusive]
        );
        assert_eq!(
            registered_resource_claims(
                "apply_patch",
                &json!({"replace": [{"path": "../../outside", "content": "nope"}]}),
                &context,
            )
            .expect("escaped target fallback"),
            vec![ResourceClaim::GlobalExclusive]
        );
    }

    #[test]
    fn terminal_and_unknown_tools_keep_conservative_claims() {
        let root = tempdir().expect("tempdir");
        let context = ToolContext::new(root.path().to_path_buf());

        assert_eq!(
            registered_resource_claims("terminal/run", &json!({}), &context)
                .expect("default terminal"),
            vec![ResourceClaim::Terminal("term-1".to_string())]
        );
        assert_eq!(
            registered_resource_claims(
                "exec_shell_interact",
                &json!({"task_id": "task-7"}),
                &context,
            )
            .expect("task terminal"),
            vec![ResourceClaim::Terminal("task-7".to_string())]
        );
        assert_eq!(
            registered_resource_claims("plugin_tool", &json!({}), &context).expect("unknown tool"),
            vec![ResourceClaim::GlobalExclusive]
        );
    }
}