basis 0.4.2

The basis SDK: workspace discovery, run lifecycle, one event stream, and the two seams. No protocol, no transport, no TTY.
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
//! The runner's two adapters, each against the binding it serves.
//!
//! Split out of `runner.rs` only for its size — the file was past the 800-line
//! ceiling with these inline. What is checked here is the runner's own job:
//! that each binding's answers are translated faithfully, and that the chain's
//! rules (which `chain.rs` tests on their own) survive the translation.
//!
//! Gated to unix for the same reason as `exec`'s tests: the subprocess
//! fixtures are `/bin/sh` scripts. The in-process binding needs no shell, and
//! `tests/interception.rs` covers it on every platform.

use std::sync::Mutex;

use serde_json::json;

use super::*;
use crate::hooks::{InterceptorError, OnFailure};

fn call(tool_name: &str) -> HookCall {
    HookCall::new("agent-1", tool_name, "call-1", r#"{"command":"ls"}"#)
}

fn sh(name: &str, script: &str) -> HookSpec {
    HookSpec::new(
        name,
        vec!["/bin/sh".to_string(), "-c".to_string(), script.to_string()],
    )
}

/// A reporter that remembers, so a test can prove a failure was announced.
#[derive(Default, Clone)]
struct Reports(Arc<Mutex<Vec<String>>>);

impl Reports {
    fn install(&self, runner: HookRunner) -> HookRunner {
        let sink = self.0.clone();
        runner.with_reporter(move |message| {
            sink.lock().expect("not poisoned").push(message.to_string())
        })
    }

    fn all(&self) -> Vec<String> {
        self.0.lock().expect("not poisoned").clone()
    }
}

fn decide(hooks: Vec<HookSpec>) -> (HookOutcome, Vec<String>) {
    let reports = Reports::default();
    let runner = reports.install(HookRunner::new(".", hooks));

    let outcome = runner.decide(&call("shell"));

    (outcome, reports.all())
}

fn denied(outcome: HookOutcome) -> String {
    match outcome {
        HookOutcome::Deny(reason) => reason,
        other => panic!("expected a denial, got {other:?}"),
    }
}

/// Answers whatever it was built with, and records that it was asked.
struct Fixed {
    name: &'static str,
    answer: fn(&HookRequest) -> Result<HookOutcome, InterceptorError>,
}

impl Fixed {
    fn new(
        name: &'static str,
        answer: fn(&HookRequest) -> Result<HookOutcome, InterceptorError>,
    ) -> Self {
        Self { name, answer }
    }
}

#[async_trait::async_trait]
impl Interceptor for Fixed {
    fn name(&self) -> &str {
        self.name
    }

    async fn intercept(&self, call: &HookRequest) -> Result<HookOutcome, InterceptorError> {
        (self.answer)(call)
    }
}

fn allows(_call: &HookRequest) -> Result<HookOutcome, InterceptorError> {
    Ok(HookOutcome::Allow)
}

#[test]
fn no_participants_means_no_subprocess_and_no_opinion() {
    let (outcome, reports) = decide(Vec::new());

    assert_eq!(outcome, HookOutcome::Allow);
    assert!(reports.is_empty());
}

#[test]
fn a_hook_that_allows_lets_the_call_through() {
    let (outcome, reports) = decide(vec![sh("ok", r#"echo '{"decision":"allow"}'"#)]);

    assert_eq!(outcome, HookOutcome::Allow);
    assert!(reports.is_empty(), "a working hook is not news");
}

#[test]
fn a_denial_carries_the_hook_name_and_its_reason() {
    let (outcome, _) = decide(vec![sh(
        "guard",
        r#"echo '{"decision":"deny","reason":"not here"}'"#,
    )]);

    assert_eq!(
        outcome,
        HookOutcome::Deny("denied by hook 'guard': not here".to_string())
    );
}

#[test]
fn the_hook_is_told_what_the_tool_wants() {
    // Echoing the request back proves the wire contract reached stdin.
    let (outcome, _) = decide(vec![sh(
        "echoer",
        r#"printf '{"decision":"deny","reason":%s}' "\"$(cat | tr -d '\n' | cut -c1-200)\"" "#,
    )]);

    let reason = denied(outcome);
    assert!(reason.contains("hook_schema"));
    assert!(reason.contains("pre_tool_use"));
    assert!(reason.contains("call-1"));
}

#[test]
fn the_first_refusal_stops_the_chain() {
    let (outcome, _) = decide(vec![
        sh("first", r#"echo '{"decision":"deny","reason":"mine"}'"#),
        sh(
            "second",
            r#"echo '{"decision":"deny","reason":"also mine"}'"#,
        ),
    ]);

    assert_eq!(
        outcome,
        HookOutcome::Deny("denied by hook 'first': mine".to_string()),
        "a hook that already denied makes the next spawn pointless"
    );
}

#[test]
fn a_hook_only_hears_about_the_tools_it_asked_for() {
    let runner = HookRunner::new(
        ".",
        vec![sh("guard", r#"echo '{"decision":"deny"}'"#).with_tools(vec!["files".to_string()])],
    )
    .with_reporter(|_| {});

    assert_eq!(runner.decide(&call("shell")), HookOutcome::Allow);
    assert_eq!(
        runner.decide(&call("files")),
        HookOutcome::Deny("denied by hook 'guard'".to_string())
    );
}

#[test]
fn a_modify_replaces_the_input() {
    let (outcome, reports) = decide(vec![sh(
        "redact",
        r#"echo '{"decision":"modify","input":{"command":"ls"},"reason":"stripped the token"}'"#,
    )]);

    assert_eq!(
        outcome,
        HookOutcome::Modify {
            input: json!({"command": "ls"}),
            reason: Some("hook 'redact': stripped the token".to_string()),
        }
    );
    assert!(reports.is_empty(), "modifying is not a failure");
}

#[test]
fn modifications_compose_in_order() {
    // The second hook answers differently depending on what it was shown,
    // so its output proves it was asked about the rewritten call.
    let (outcome, _) = decide(vec![
        sh(
            "first",
            r#"echo '{"decision":"modify","input":{"command":"once"}}'"#,
        ),
        sh(
            "second",
            r#"
            request=$(cat)
            case "$request" in
                *'"command":"once"'*)
                    echo '{"decision":"modify","input":{"command":"twice"}}' ;;
                *) echo '{"decision":"deny","reason":"saw the original"}' ;;
            esac
            "#,
        ),
    ]);

    let HookOutcome::Modify { input, reason } = outcome else {
        panic!("expected a modification");
    };
    assert_eq!(input, json!({"command": "twice"}));
    assert_eq!(
        reason,
        Some("hook 'first'; hook 'second'".to_string()),
        "every hand that touched the call belongs in the trail"
    );
}

#[test]
fn a_later_hook_can_still_deny_a_modified_call() {
    let (outcome, _) = decide(vec![
        sh(
            "rewriter",
            r#"echo '{"decision":"modify","input":{"command":"sneaky"}}'"#,
        ),
        sh("guard", r#"echo '{"decision":"deny","reason":"still no"}'"#),
    ]);

    assert_eq!(
        outcome,
        HookOutcome::Deny("denied by hook 'guard': still no".to_string()),
        "modify must not be a way past a hook that runs later"
    );
}

#[test]
fn a_replacement_that_is_not_an_object_is_refused() {
    let (outcome, reports) = decide(vec![sh(
        "confused",
        r#"echo '{"decision":"modify","input":"ls -l"}'"#,
    )]);

    assert!(
        denied(outcome).contains("not a JSON object"),
        "running the original would ignore a hook that believed it intervened"
    );
    assert_eq!(reports.len(), 1);
}

#[test]
fn a_broken_hook_denies_by_default_and_says_so() {
    let cases = [
        ("gone", sh("gone", "exit 7"), "exited with code 7"),
        ("silent", sh("silent", "true"), "printed nothing"),
        (
            "babbling",
            sh("babbling", "echo not json"),
            "not a decision",
        ),
        (
            "half-modifying",
            sh("half-modifying", r#"echo '{"decision":"modify"}'"#),
            "not a decision",
        ),
    ];

    for (name, spec, expected) in cases {
        let (outcome, reports) = decide(vec![spec]);

        let reason = denied(outcome);
        assert!(
            reason.contains(expected),
            "{name}: denial must say what broke, got {reason}"
        );
        assert_eq!(reports.len(), 1, "{name}: a broken hook must be announced");
        assert!(reports[0].contains(expected));
    }
}

#[test]
fn a_hooks_stderr_reaches_the_reason() {
    let (outcome, _) = decide(vec![sh("noisy", "echo 'no python' >&2; exit 1")]);

    assert!(denied(outcome).contains("no python"));
}

#[test]
fn a_hanging_hook_denies_rather_than_hanging_the_turn() {
    let (outcome, reports) = decide(vec![
        sh("stuck", "sleep 30").with_timeout(Duration::from_millis(150)),
    ]);

    assert!(denied(outcome).contains("150ms"));
    assert_eq!(reports.len(), 1);
}

#[test]
fn an_observer_can_choose_to_fail_open() {
    let (outcome, reports) = decide(vec![
        sh("logger", "exit 1").with_on_failure(OnFailure::Allow),
        sh("guard", r#"echo '{"decision":"allow"}'"#),
    ]);

    assert_eq!(
        outcome,
        HookOutcome::Allow,
        "fail-open is a per-hook choice, written down rather than inferred"
    );
    assert_eq!(
        reports.len(),
        1,
        "carrying on is not the same as staying quiet"
    );
}

#[test]
fn a_command_is_never_handed_to_a_shell() {
    // `;` is shell syntax; as argv it is just an argument. If basis ever
    // started interpreting the command, `touch` would run.
    let tmp = tempfile::tempdir().expect("tempdir");
    let marker = tmp.path().join("ran");
    let spec = HookSpec::new(
        "argv",
        vec![
            "/bin/echo".to_string(),
            format!("{{\"decision\":\"allow\"}}; touch {}", marker.display()),
        ],
    );

    let (outcome, _) = decide(vec![spec]);

    // The whole line is echoed, so it is not a decision — which is itself
    // the proof that nothing split it on the semicolon.
    assert!(matches!(outcome, HookOutcome::Deny(_)));
    assert!(!marker.exists(), "a hook command must not reach a shell");
}

// -----------------------------------------------------------------------
// The in-process binding, through the same runner
// -----------------------------------------------------------------------

#[tokio::test]
async fn an_interceptor_decides_without_a_hooks_file_in_sight() {
    let runner = HookRunner::new(".", Vec::new())
        .with_interceptor(Fixed::new("guard", |_| {
            Ok(HookOutcome::Deny("nothing today".to_string()))
        }))
        .with_reporter(|_| {});

    assert!(!runner.is_empty(), "a runner with a say is not empty");
    assert_eq!(
        runner.decide_async(&call("shell")).await,
        HookOutcome::Deny("denied by interceptor 'guard': nothing today".to_string())
    );
}

#[tokio::test]
async fn an_interceptor_is_asked_before_a_hook_is_spawned() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let marker = tmp.path().join("hook-ran");
    let runner = HookRunner::new(
        ".",
        vec![sh(
            "repo",
            &format!(
                r#"touch '{}'; echo '{{"decision":"allow"}}'"#,
                marker.display()
            ),
        )],
    )
    .with_interceptor(Fixed::new("host", |_| {
        Ok(HookOutcome::Deny("my program, my rules".to_string()))
    }))
    .with_reporter(|_| {});

    let outcome = runner.decide_async(&call("shell")).await;

    assert!(denied(outcome).contains("my program, my rules"));
    assert!(
        !marker.exists(),
        "the host's own refusal must land before a repository's hook is spawned"
    );
}

#[tokio::test]
async fn interceptors_are_asked_in_registration_order() {
    let runner = HookRunner::new(".", Vec::new())
        .with_interceptor(Fixed::new("first", |_| {
            Ok(HookOutcome::Deny("mine".to_string()))
        }))
        .with_interceptor(Fixed::new("second", |_| {
            Ok(HookOutcome::Deny("also mine".to_string()))
        }))
        .with_reporter(|_| {});

    assert_eq!(
        runner.decide_async(&call("shell")).await,
        HookOutcome::Deny("denied by interceptor 'first': mine".to_string())
    );
}

#[tokio::test]
async fn an_interceptors_rewrite_is_what_a_later_hook_is_asked_about() {
    // The hook answers differently depending on what it was shown, so its
    // output is the only proof that the rewrite reached it.
    let runner = HookRunner::new(
        ".",
        vec![sh(
            "check",
            r#"
            request=$(cat)
            case "$request" in
                *'"command":"rewritten"'*) echo '{"decision":"allow"}' ;;
                *) echo '{"decision":"deny","reason":"saw the original"}' ;;
            esac
            "#,
        )],
    )
    .with_interceptor(Fixed::new("rewriter", |_| {
        Ok(HookOutcome::Modify {
            input: json!({"command": "rewritten"}),
            reason: Some("narrowed".to_string()),
        })
    }))
    .with_reporter(|_| {});

    assert_eq!(
        runner.decide_async(&call("shell")).await,
        HookOutcome::Modify {
            input: json!({"command": "rewritten"}),
            reason: Some("interceptor 'rewriter': narrowed".to_string()),
        }
    );
}

#[tokio::test]
async fn an_interceptor_cannot_smuggle_a_call_past_a_hook() {
    // The no-smuggling property `PreExecutionHooks::run` documents. basis's
    // runner owns the ordering, so basis's runner has to keep it — and it
    // has to keep it across the two bindings, not only within one.
    let runner = HookRunner::new(
        ".",
        vec![sh(
            "guard",
            r#"echo '{"decision":"deny","reason":"no rewrite makes this fine"}'"#,
        )],
    )
    .with_interceptor(Fixed::new("rewriter", |_| {
        Ok(HookOutcome::Modify {
            input: json!({"command": "sneaky"}),
            reason: None,
        })
    }))
    .with_reporter(|_| {});

    let outcome = runner.decide_async(&call("shell")).await;

    assert!(denied(outcome).contains("no rewrite makes this fine"));
}

#[tokio::test]
async fn a_hook_and_an_interceptor_both_appear_in_the_trail() {
    let runner = HookRunner::new(
        ".",
        vec![sh(
            "pin",
            r#"echo '{"decision":"modify","input":{"command":"pinned"},"reason":"pinned the ref"}'"#,
        )],
    )
    .with_interceptor(Fixed::new("redact", |_| {
        Ok(HookOutcome::Modify {
            input: json!({"command": "redacted"}),
            reason: None,
        })
    }))
    .with_reporter(|_| {});

    let HookOutcome::Modify { reason, .. } = runner.decide_async(&call("shell")).await else {
        panic!("expected a modification");
    };
    assert_eq!(
        reason,
        Some("interceptor 'redact'; hook 'pin': pinned the ref".to_string())
    );
}

#[tokio::test]
async fn an_interceptor_that_errors_denies_and_says_so() {
    let reports = Reports::default();
    let runner = reports.install(
        HookRunner::new(".", Vec::new()).with_interceptor(Fixed::new("vault", |_| {
            Err(std::io::Error::other("the vault is unreachable").into())
        })),
    );

    let reason = denied(runner.decide_async(&call("shell")).await);

    assert!(reason.contains("vault"), "got {reason}");
    assert!(reason.contains("the vault is unreachable"), "got {reason}");
    assert_eq!(reports.all().len(), 1, "a broken guard must be announced");
}

#[tokio::test]
async fn an_interceptor_that_panics_denies_rather_than_taking_the_turn() {
    let reports = Reports::default();
    let runner = reports.install(
        HookRunner::new(".", Vec::new())
            .with_interceptor(Fixed::new("confused", |_| panic!("unwrapped a None"))),
    );

    let reason = denied(runner.decide_async(&call("shell")).await);

    assert!(reason.contains("panicked"), "got {reason}");
    assert!(reason.contains("unwrapped a None"), "got {reason}");
    assert_eq!(reports.all().len(), 1);
}

#[tokio::test]
async fn a_synchronous_decision_refuses_rather_than_skipping_an_interceptor() {
    // Silently deciding without a registered guard is the one failure this
    // module is arranged to avoid, so the synchronous path says no.
    let runner = HookRunner::new(".", Vec::new())
        .with_interceptor(Fixed::new("guard", allows))
        .with_reporter(|_| {});

    assert!(denied(runner.decide(&call("shell"))).contains("decide_async"));
}

#[tokio::test]
async fn an_interceptor_may_await_on_a_current_thread_runtime() {
    struct Awaits;

    #[async_trait::async_trait]
    impl Interceptor for Awaits {
        fn name(&self) -> &str {
            "awaits"
        }

        async fn intercept(&self, _call: &HookRequest) -> Result<HookOutcome, InterceptorError> {
            tokio::time::sleep(Duration::from_millis(1)).await;
            Ok(HookOutcome::Deny("after awaiting".to_string()))
        }
    }

    let runner = HookRunner::new(".", Vec::new()).with_interceptor(Awaits);

    assert!(
        denied(runner.decide_async(&call("shell")).await).contains("after awaiting"),
        "an interceptor doing real work must not need a multi-thread runtime"
    );
}

/// Puts a call to the runner the way mentra does.
async fn hook_decision(hooks: Vec<HookSpec>) -> HookDecision {
    HookRunner::new(".", hooks)
        .with_reporter(|_| {})
        .pre_tool_execution(&PreExecutionContext {
            agent_id: "agent-1".to_string(),
            tool_name: "shell".to_string(),
            tool_call_id: "call-1".to_string(),
            input_json: r#"{"command":"ls"}"#.to_string(),
            working_directory: std::path::PathBuf::from("."),
        })
        .await
        .expect("a runner never errors")
}

#[tokio::test]
async fn the_runtime_seam_carries_every_outcome() {
    assert_eq!(
        hook_decision(vec![sh("ok", r#"echo '{"decision":"allow"}'"#)]).await,
        HookDecision::Allow
    );

    assert_eq!(
        hook_decision(vec![sh("no", r#"echo '{"decision":"deny","reason":"x"}'"#)]).await,
        HookDecision::Deny("denied by hook 'no': x".to_string())
    );

    // The replacement crosses back as JSON text, which is mentra's shape.
    assert_eq!(
        hook_decision(vec![sh(
            "rewrite",
            r#"echo '{"decision":"modify","input":{"command":"safe"}}'"#,
        )])
        .await,
        HookDecision::Modify {
            input_json: r#"{"command":"safe"}"#.to_string(),
            reason: Some("hook 'rewrite'".to_string()),
        }
    );
}

/// Both flavors, because a hook waits on a subprocess and where that wait
/// happens has to be right on either. `spawn_blocking` works on both;
/// `block_in_place`, which this used to need, panics on current_thread.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_hook_runs_from_inside_a_multi_thread_runtime() {
    let runner = HookRunner::new(".", vec![sh("ok", r#"echo '{"decision":"allow"}'"#)]);

    assert_eq!(
        runner.decide_async(&call("shell")).await,
        HookOutcome::Allow
    );
}

#[tokio::test(flavor = "current_thread")]
async fn a_hook_runs_from_inside_a_current_thread_runtime() {
    let runner = HookRunner::new(".", vec![sh("ok", r#"echo '{"decision":"allow"}'"#)]);

    assert_eq!(
        runner.decide_async(&call("shell")).await,
        HookOutcome::Allow
    );
}

#[test]
fn the_debug_view_names_both_bindings_without_leaking_the_reporter() {
    let runner = HookRunner::new("/repo", vec![sh("guard", "true")])
        .with_interceptor(Fixed::new("redact", allows));

    let shown = format!("{runner:?}");

    assert!(shown.contains("guard"));
    assert!(shown.contains("redact"));
    assert!(shown.contains("/repo"));
}