basis 0.4.3

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
//! What a manifest may say, and what basis refuses to read.
//!
//! Portable: nothing here spawns a process. The subprocess half lives in
//! [`super::super::tool`]'s tests and in `basis/tests/declared_tools.rs`.

use super::*;

fn config(global: Option<PathBuf>) -> ToolsConfig {
    ToolsConfig {
        workspace_file: PathBuf::from(DEFAULT_WORKSPACE_TOOLS_FILE),
        global_dir: global,
    }
}

fn write(path: &Path, body: &str) {
    std::fs::create_dir_all(path.parent().expect("a parent")).expect("create dir");
    std::fs::write(path, body).expect("write file");
}

fn one_tool(name: &str, program: &str) -> String {
    format!(
        r#"{{"schema": 1, "tools": {{
            "{name}": {{
                "description": "does the thing",
                "input_schema": {{"type": "object", "properties": {{}}}},
                "command": ["{program}"]
            }}
        }}}}"#
    )
}

/// Parses `body` against an environment that holds nothing.
fn parsed(body: &str) -> Result<Vec<DeclaredToolSpec>, DeclaredToolError> {
    parse(Path::new("/repo/.basis/tools.json"), body, &|_| None)
}

/// The one declared tool `body` holds, or the reason it was refused.
fn only(body: &str) -> DeclaredToolSpec {
    let mut tools = parsed(body).expect("the manifest parses");
    assert_eq!(tools.len(), 1);
    tools.pop().expect("one tool")
}

#[test]
fn nothing_on_disk_means_no_tools() {
    let tmp = tempfile::tempdir().expect("tempdir");

    assert!(
        load(tmp.path(), &config(None))
            .expect("no file is not an error")
            .is_empty()
    );
}

#[test]
fn a_workspace_manifest_is_found() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let path = tmp.path().join(DEFAULT_WORKSPACE_TOOLS_FILE);
    write(&path, &one_tool("jenkins_job", "./ci/jenkins"));

    let found = discover(tmp.path(), &config(None)).expect("parses");

    assert_eq!(found.len(), 1);
    assert_eq!(found[0].path, path);
    assert_eq!(found[0].scope, ContextScope::Workspace);
    assert_eq!(found[0].tools[0].name, "jenkins_job");
}

#[test]
fn the_workspace_manifest_shadows_the_global_one() {
    // Names are the identity: two declarations under one name are one tool with
    // a precedence question, never two tools.
    let tmp = tempfile::tempdir().expect("tempdir");
    let global = tmp.path().join("global");
    write(
        &tmp.path().join(DEFAULT_WORKSPACE_TOOLS_FILE),
        &one_tool("jenkins_job", "./from-the-workspace"),
    );
    write(
        &global.join(DEFAULT_GLOBAL_TOOLS_FILE),
        &one_tool("jenkins_job", "./from-the-global-file"),
    );

    let tools = load(tmp.path(), &config(Some(global))).expect("parses");

    assert_eq!(tools.len(), 1, "one name is one tool");
    assert_eq!(tools[0].command, vec!["./from-the-workspace".to_string()]);
}

#[test]
fn a_global_tool_survives_alongside_a_workspace_one() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let global = tmp.path().join("global");
    write(
        &tmp.path().join(DEFAULT_WORKSPACE_TOOLS_FILE),
        &one_tool("local_tool", "./a"),
    );
    write(
        &global.join(DEFAULT_GLOBAL_TOOLS_FILE),
        &one_tool("shared_tool", "./b"),
    );

    let tools = load(tmp.path(), &config(Some(global))).expect("parses");

    let names: Vec<&str> = tools.iter().map(|tool| tool.name.as_str()).collect();
    assert_eq!(names, vec!["local_tool", "shared_tool"]);
}

#[test]
fn the_same_file_reached_twice_is_read_once() {
    let tmp = tempfile::tempdir().expect("tempdir");
    write(
        &tmp.path().join(DEFAULT_GLOBAL_TOOLS_FILE),
        &one_tool("shared_tool", "./b"),
    );

    let found = discover(
        tmp.path(),
        &ToolsConfig {
            workspace_file: PathBuf::from(DEFAULT_GLOBAL_TOOLS_FILE),
            global_dir: Some(tmp.path().to_path_buf()),
        },
    )
    .expect("parses");

    assert_eq!(found.len(), 1);
    assert_eq!(found[0].scope, ContextScope::Workspace);
}

#[test]
fn a_directory_where_the_manifest_should_be_is_ignored() {
    let tmp = tempfile::tempdir().expect("tempdir");
    std::fs::create_dir_all(tmp.path().join(DEFAULT_WORKSPACE_TOOLS_FILE))
        .expect("create a directory with the file's name");

    assert!(
        discover(tmp.path(), &config(None))
            .expect("parses")
            .is_empty()
    );
}

#[test]
fn a_malformed_manifest_is_an_error_not_an_empty_list() {
    // The alternative is a run whose model is missing a capability its
    // instructions assume, discovered only where it needed it.
    let tmp = tempfile::tempdir().expect("tempdir");
    write(&tmp.path().join(DEFAULT_WORKSPACE_TOOLS_FILE), "{not json");

    let error = load(tmp.path(), &config(None)).expect_err("rejected");

    assert!(matches!(error, DeclaredToolError::Parse { .. }), "{error}");
}

#[test]
fn a_future_schema_is_refused_by_name() {
    let error = parsed(r#"{"schema": 99, "tools": {}}"#).expect_err("rejected");

    assert!(matches!(
        error,
        DeclaredToolError::UnsupportedSchema { schema: 99, .. }
    ));
    assert!(error.to_string().contains("99"));
}

#[test]
fn a_manifest_with_no_schema_is_refused() {
    let error = parsed(r#"{"tools": {}}"#).expect_err("rejected");

    assert!(
        matches!(error, DeclaredToolError::NoSchema { .. }),
        "{error}"
    );
}

#[test]
fn a_misspelled_tools_key_is_refused_rather_than_read_as_empty() {
    // `deny_unknown_fields` catches it, so it arrives as a location in the file
    // rather than as a workspace whose tools quietly never appear.
    let error = parsed(r#"{"schema": 1, "tool": {}}"#).expect_err("rejected");

    assert!(matches!(error, DeclaredToolError::Parse { .. }), "{error}");
}

#[test]
fn a_misspelled_field_is_refused_rather_than_silently_defaulted() {
    let error = parsed(
        r#"{"schema": 1, "tools": {"t": {
            "description": "d",
            "input_schema": {"type": "object"},
            "command": ["./x"],
            "timout_ms": 10
        }}}"#,
    )
    .expect_err("rejected");

    assert!(
        matches!(error, DeclaredToolError::Parse { .. }),
        "a tool running on a timeout nobody chose is worse than a refused file: {error}"
    );
}

#[test]
fn every_field_a_tool_cannot_do_without_is_named_when_it_is_missing() {
    let cases = [
        (
            r#"{"input_schema": {"type": "object"}, "command": ["./x"]}"#,
            "description",
        ),
        (
            r#"{"description": "d", "command": ["./x"]}"#,
            "input_schema",
        ),
        (
            r#"{"description": "d", "input_schema": {"type": "object"}}"#,
            "command",
        ),
        (
            r#"{"description": "d", "input_schema": {"type": "object"}, "command": []}"#,
            "command",
        ),
    ];

    for (entry, field) in cases {
        let error = parsed(&format!(r#"{{"schema": 1, "tools": {{"t": {entry}}}}}"#))
            .expect_err("rejected");

        assert!(
            error.to_string().contains(field),
            "{field} was not named in {error}"
        );
    }
}

#[test]
fn a_name_a_provider_would_refuse_is_refused_here_instead() {
    // Otherwise the rejection arrives as an opaque request failure on the first
    // turn, naming neither the file nor the tool.
    for name in ["", "has a space", "emoji-🙂", &"x".repeat(65)] {
        let body = format!(
            r#"{{"schema": 1, "tools": {{"{name}": {{
                "description": "d",
                "input_schema": {{"type": "object"}},
                "command": ["./x"]
            }}}}}}"#
        );

        let error = parsed(&body).expect_err("rejected");
        assert!(
            matches!(error, DeclaredToolError::Invalid { .. }),
            "{name:?} was accepted: {error}"
        );
    }
}

#[test]
fn a_workspace_cannot_declare_a_tool_into_mcps_namespace() {
    let error = parsed(&one_tool("mcp__fs__read", "./x")).expect_err("rejected");

    assert!(
        matches!(error, DeclaredToolError::Invalid { .. }),
        "mentra parses that prefix to find a bridged server: {error}"
    );
}

#[test]
fn a_schema_that_is_not_an_object_is_refused() {
    for schema in [r#""string""#, "[]", r#"{"type": "array"}"#] {
        let body = format!(
            r#"{{"schema": 1, "tools": {{"t": {{
                "description": "d",
                "input_schema": {schema},
                "command": ["./x"]
            }}}}}}"#
        );

        let error = parsed(&body).expect_err("rejected");
        assert!(
            matches!(error, DeclaredToolError::Invalid { .. }),
            "{schema} was accepted: {error}"
        );
    }
}

#[test]
fn a_deadline_that_has_already_passed_is_refused() {
    let body = r#"{"schema": 1, "tools": {"t": {
        "description": "d",
        "input_schema": {"type": "object"},
        "command": ["./x"],
        "timeout_ms": 0
    }}}"#;

    let error = parsed(body).expect_err("rejected");

    assert!(
        matches!(error, DeclaredToolError::Invalid { .. }),
        "{error}"
    );
}

#[test]
fn an_undeclared_side_effect_still_reaches_the_approver() {
    // The fail-closed rule this whole binding turns on: the mildest thing a
    // program can truthfully say about itself is that it is a program.
    let spec = only(&one_tool("t", "./x"));

    assert_eq!(spec.side_effect, SideEffect::Process);
    assert!(crate::approval::is_consequential(spec.side_effect.level()));
}

#[test]
fn neither_side_effect_can_be_waved_through_as_a_read() {
    for side_effect in [SideEffect::Process, SideEffect::External] {
        assert!(
            crate::approval::is_consequential(side_effect.level()),
            "{side_effect:?} skipped the approver"
        );
    }
}

#[test]
fn a_tool_that_leaves_the_machine_can_say_so() {
    let body = r#"{"schema": 1, "tools": {"t": {
        "description": "d",
        "input_schema": {"type": "object"},
        "command": ["./x"],
        "side_effect": "external"
    }}}"#;

    assert_eq!(only(body).side_effect, SideEffect::External);
    assert_eq!(
        only(body).side_effect.level(),
        ToolSideEffectLevel::External
    );
}

#[test]
fn a_credential_reaches_the_program_through_the_environment() {
    // The whole reason `env` and `${VAR}` exist: the token is what the tool
    // needs and the last thing that should be in a file people commit.
    let body = r#"{"schema": 1, "tools": {"t": {
        "description": "d",
        "input_schema": {"type": "object"},
        "command": ["./x", "--host", "${CI_HOST}"],
        "env": {"CI_TOKEN": "${CI_TOKEN}"}
    }}}"#;

    let tools = parse(
        Path::new("/repo/.basis/tools.json"),
        body,
        &|name| match name {
            "CI_HOST" => Some("ci.example".to_string()),
            "CI_TOKEN" => Some("secret-value".to_string()),
            _ => None,
        },
    )
    .expect("both are set");

    assert_eq!(tools[0].command[2], "ci.example");
    assert_eq!(
        tools[0].env,
        vec![("CI_TOKEN".to_string(), "secret-value".to_string())]
    );
}

#[test]
fn an_unset_variable_names_the_field_and_never_the_value() {
    let body = r#"{"schema": 1, "tools": {"t": {
        "description": "d",
        "input_schema": {"type": "object"},
        "command": ["./x"],
        "env": {"CI_TOKEN": "${CI_TOKEN}"}
    }}}"#;

    let error = parsed(body).expect_err("CI_TOKEN is unset");

    let message = error.to_string();
    assert!(message.contains("env.CI_TOKEN"), "{message}");
    assert!(message.contains("`t`"), "the tool must be named: {message}");
}

#[test]
fn a_specs_environment_is_not_printed() {
    // `DeclaredToolSpec` is held by a registered tool and reachable from a
    // host's `{:?}`, and by the time it exists the `${VAR}` is the real value.
    let body = r#"{"schema": 1, "tools": {"t": {
        "description": "d",
        "input_schema": {"type": "object"},
        "command": ["./deploy", "--fast"],
        "env": {"CI_TOKEN": "${CI_TOKEN}"}
    }}}"#;

    let spec = parse(Path::new("/repo/.basis/tools.json"), body, &|_| {
        Some("secret-value".to_string())
    })
    .expect("parses")
    .pop()
    .expect("one tool");

    let printed = format!("{spec:?}");

    assert!(!printed.contains("secret-value"));
    assert!(printed.contains("redacted"));
    assert!(
        printed.contains("CI_TOKEN"),
        "the variable's name is what makes a misconfiguration fixable"
    );
    assert!(
        printed.contains("deploy") && printed.contains("--fast"),
        "the command and its arguments are how a spawn is debugged"
    );
}

#[test]
fn a_tool_runs_at_the_workspace_root_unless_it_says_otherwise() {
    let root = Path::new("/repo");
    let plain = only(&one_tool("t", "./x"));

    assert_eq!(plain.working_directory(root), PathBuf::from("/repo"));

    let nested = only(
        r#"{"schema": 1, "tools": {"t": {
            "description": "d",
            "input_schema": {"type": "object"},
            "command": ["./x"],
            "cwd": "services/ci"
        }}}"#,
    );

    assert_eq!(
        nested.working_directory(root),
        PathBuf::from("/repo/services/ci")
    );
}

#[test]
fn omitted_fields_take_the_documented_defaults() {
    let spec = only(&one_tool("t", "./x"));

    assert_eq!(spec.timeout(), DEFAULT_TOOL_TIMEOUT);
    assert_eq!(spec.side_effect, SideEffect::Process);
    assert_eq!(spec.cwd, None);
    assert!(spec.env.is_empty());
}

#[test]
fn defaults_declare_no_tools_of_their_own() {
    assert_eq!(
        ToolsConfig::default().workspace_file,
        PathBuf::from(".basis/tools.json")
    );
}