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
#![cfg(unix)]
//! Declared subprocess tools, end to end: a real `.basis/tools.json`, a real
//! program on disk, a real process, and a runtime that actually calls it.
//!
//! The unit tests in `src/tools/declared/` cover the pieces — what a manifest
//! may say, what the approver is shown, what a failure reads like. What is
//! worth proving here is the whole path a workspace takes, because every
//! interesting failure of this feature lives in the seams: a file someone
//! wrote, a tool discovered from it, a name claimed on a shared registry, a
//! program spawned, and its output arriving as the model's tool result.
//!
//! Gated to unix because the fixtures are shell scripts, which is the cheapest
//! way to exercise a real program; the code under test is portable, and
//! inventing a Windows script per case would test the fixture rather than the
//! wrapper.
//!
//! No network: the workspace tests open against a closed port with an explicit
//! model id, and the runtime tests drive mentra's mock.

use std::{
    os::unix::fs::PermissionsExt,
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
};

use basis::{
    ContextConfig, RunError, Runtime, RuntimeBuilder, Workspace,
    hooks::HooksConfig,
    skills::SkillsConfig,
    templates::TemplatesConfig,
    tools::declared::{self, DeclaredTool, ToolsConfig},
};
use mentra::{
    ContentBlock, ModelSelector, Session,
    agent::{AgentConfig, WorkspaceConfig},
    error::RuntimeError,
    test::{MockRuntime, MockToolCall},
    tool::{
        ToolAuthorizationDecision, ToolAuthorizationRequest, ToolAuthorizer, ToolSideEffectLevel,
    },
};
use serde_json::{Value, json};

/// A port nothing listens on. Opening a workspace should never reach it.
const CLOSED_PORT: &str = "http://127.0.0.1:1/v1";

/// A workspace with a manifest and somewhere to put programs.
struct Fixture {
    dir: tempfile::TempDir,
}

impl Fixture {
    fn new() -> Self {
        Self {
            dir: tempfile::tempdir().expect("tempdir"),
        }
    }

    fn path(&self) -> &Path {
        self.dir.path()
    }

    /// Writes an executable `/bin/sh` program and returns its absolute path.
    fn program(&self, name: &str, body: &str) -> String {
        let path = self.dir.path().join(".basis/tools").join(name);
        std::fs::create_dir_all(path.parent().expect("a parent")).expect("create dir");
        std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).expect("write program");
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
            .expect("make it executable");

        path.to_string_lossy().into_owned()
    }

    fn manifest(&self, body: &str) -> PathBuf {
        let path = self.dir.path().join(".basis/tools.json");
        std::fs::create_dir_all(path.parent().expect("a parent")).expect("create dir");
        std::fs::write(&path, body).expect("write manifest");
        path
    }

    /// One tool called `jenkins_job`, running `program`.
    fn declaring(&self, program: &str) -> PathBuf {
        self.manifest(&format!(
            r#"{{
                "schema": 1,
                "tools": {{
                    "jenkins_job": {{
                        "description": "Trigger a job and return its build number.",
                        "input_schema": {{
                            "type": "object",
                            "properties": {{"job": {{"type": "string"}}}},
                            "required": ["job"]
                        }},
                        "command": ["{program}"]
                    }}
                }}
            }}"#
        ))
    }

    fn config(&self) -> ToolsConfig {
        ToolsConfig {
            workspace_file: PathBuf::from(".basis/tools.json"),
            global_dir: None,
        }
    }

    /// The declared tools of this fixture, wrapped and ready to register.
    fn tools(&self) -> Vec<DeclaredTool> {
        declared::load(self.path(), &self.config())
            .expect("the manifest parses")
            .into_iter()
            .map(|spec| DeclaredTool::new(spec, self.path()))
            .collect()
    }
}

/// A workspace that looks nowhere except where the test put something, and
/// contacts nothing while opening.
fn offline(workspace: &Path) -> basis::WorkspaceBuilder {
    Workspace::builder(workspace)
        .with_runtime_builder(
            Runtime::builder()
                .with_base_url(CLOSED_PORT)
                .with_api_key("test-key")
                .with_ephemeral_history(),
        )
        .with_model(ModelSelector::Id("test-model".to_string()))
        .with_context(ContextConfig {
            file_name: "AGENTS.md".to_string(),
            global_dir: None,
            walk_parents: false,
        })
        .with_skills(SkillsConfig {
            workspace_subdir: PathBuf::from(".basis/skills"),
            global_dir: None,
        })
        .with_templates(TemplatesConfig {
            workspace_subdir: PathBuf::from(".basis/templates"),
            global_dir: None,
        })
        .with_hooks(HooksConfig {
            workspace_file: PathBuf::from(".basis/hooks.json"),
            global_dir: None,
        })
        .with_tools(ToolsConfig {
            workspace_file: PathBuf::from(".basis/tools.json"),
            global_dir: None,
        })
}

/// A runtime whose one scripted turn calls `jenkins_job` with `input`.
fn runtime_calling(input: Value) -> MockRuntime {
    MockRuntime::builder()
        .tool_calls(vec![MockToolCall::new("jenkins_job", input)])
        .text("done")
        .build()
        .expect("the mock runtime builds")
}

fn session_in(mock: &MockRuntime, workspace: &Path) -> Session {
    mock.runtime()
        .create_session_with_config(
            "test",
            mock.model(),
            AgentConfig {
                workspace: WorkspaceConfig {
                    base_dir: workspace.to_path_buf(),
                    ..Default::default()
                },
                ..Default::default()
            },
        )
        .expect("session")
}

/// Every tool result the turn produced, as text.
fn tool_results(session: &Session) -> String {
    session
        .replay()
        .items()
        .iter()
        .filter_map(|item| item.message.as_ref())
        .flat_map(|message| message.content.iter())
        .filter_map(|block| match block {
            ContentBlock::ToolResult { content, .. } => Some(content.to_string()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Allows everything and keeps what it was asked, which is the only way to see
/// what an approver would have been shown.
#[derive(Default)]
struct Recorder {
    seen: Arc<Mutex<Vec<ToolAuthorizationRequest>>>,
}

#[async_trait::async_trait]
impl ToolAuthorizer for Recorder {
    async fn authorize(
        &self,
        request: &ToolAuthorizationRequest,
    ) -> Result<ToolAuthorizationDecision, RuntimeError> {
        self.seen
            .lock()
            .expect("not poisoned")
            .push(request.clone());
        Ok(ToolAuthorizationDecision::allow())
    }
}

#[tokio::test]
async fn a_declared_tool_runs_and_its_output_is_the_models_result() {
    // The whole binding: a file someone wrote, a program it named, and the
    // program's stdout arriving where the model reads it.
    let fixture = Fixture::new();
    let program = fixture.program("jenkins", r#"printf 'build 4821 started'"#);
    fixture.declaring(&program);

    let mock = runtime_calling(json!({"job": "nightly"}));
    for tool in fixture.tools() {
        mock.runtime().register_tool(tool);
    }
    let mut session = session_in(&mock, fixture.path());

    let _ = session.append_turn(vec![ContentBlock::text("go")]).await;

    let results = tool_results(&session);
    assert!(results.contains("build 4821 started"), "{results}");
}

#[tokio::test]
async fn the_model_fills_a_schema_and_the_program_reads_an_object() {
    // The use case this shipped against, asserted: a value full of quoting a
    // shell would have mangled travels on stdin, so nothing has to encode it.
    let fixture = Fixture::new();
    // Writes what it was given to a file, because only the file can prove what
    // the program actually received.
    let landed = fixture.path().join("stdin.json");
    let program = fixture.program("echo-stdin", &format!("cat > {}", landed.display()));
    fixture.declaring(&program);

    let awkward = r#"o'brien && `date` "quoted" $HOME"#;
    let mock = runtime_calling(json!({"job": awkward}));
    for tool in fixture.tools() {
        mock.runtime().register_tool(tool);
    }
    let mut session = session_in(&mock, fixture.path());

    let _ = session.append_turn(vec![ContentBlock::text("go")]).await;

    let received: Value = serde_json::from_str(
        &std::fs::read_to_string(&landed).expect("the program was handed something"),
    )
    .expect("what arrived is JSON");

    assert_eq!(
        received,
        json!({"job": awkward}),
        "the model's input reaches the program byte for byte, with no shell in between"
    );
}

#[tokio::test]
async fn a_failing_program_reaches_the_model_with_its_own_words() {
    let fixture = Fixture::new();
    let program = fixture.program("jenkins", r#"echo "no such job" >&2; exit 4"#);
    fixture.declaring(&program);

    let mock = runtime_calling(json!({"job": "nope"}));
    for tool in fixture.tools() {
        mock.runtime().register_tool(tool);
    }
    let mut session = session_in(&mock, fixture.path());

    let _ = session.append_turn(vec![ContentBlock::text("go")]).await;

    let results = tool_results(&session);
    assert!(
        results.contains("no such job"),
        "a bare failure is one the model retries verbatim: {results}"
    );
    assert!(
        results.contains("jenkins_job"),
        "and it must say which tool failed: {results}"
    );
}

#[tokio::test]
async fn the_approver_is_asked_and_is_shown_the_program_not_just_the_name() {
    // The security surface: the name in the roster was chosen by the same file
    // that chose the command, so an approver shown only the name is approving a
    // string a repository wrote.
    let fixture = Fixture::new();
    let program = fixture.program("jenkins", r#"printf ok"#);
    fixture.declaring(&program);

    let recorder = Recorder::default();
    let seen = Arc::clone(&recorder.seen);
    let mock = MockRuntime::builder()
        .with_tool_authorizer(recorder)
        .tool_calls(vec![MockToolCall::new(
            "jenkins_job",
            json!({"job": "nightly"}),
        )])
        .text("done")
        .build()
        .expect("the mock runtime builds");
    for tool in fixture.tools() {
        mock.runtime().register_tool(tool);
    }
    let mut session = session_in(&mock, fixture.path());

    let _ = session.append_turn(vec![ContentBlock::text("go")]).await;

    let asked = seen.lock().expect("not poisoned");
    let request = asked
        .iter()
        .find(|request| request.tool_name == "jenkins_job")
        .expect("a declared tool is never waved through as a read");

    assert_eq!(
        request.preview.side_effect_level,
        ToolSideEffectLevel::Process
    );
    assert_eq!(
        request.preview.structured_input["command"],
        json!([program]),
        "the approver sees what will run"
    );
    assert_eq!(
        request.preview.structured_input["input"],
        json!({"job": "nightly"})
    );
}

#[tokio::test]
async fn opening_a_workspace_registers_what_its_manifest_declared() {
    let fixture = Fixture::new();
    let program = fixture.program("jenkins", r#"printf ok"#);
    let manifest = fixture.declaring(&program);

    let workspace = offline(fixture.path()).open().await.expect("opens");

    assert_eq!(workspace.declared_tools(), ["jenkins_job"]);
    assert_eq!(
        workspace
            .declared_tool_files()
            .iter()
            .map(|file| file.path.clone())
            .collect::<Vec<_>>(),
        vec![manifest],
        "a file that puts a program within the model's reach is named in the report"
    );
    assert!(
        workspace
            .mentra_runtime()
            .tools()
            .iter()
            .any(|tool| tool.provider.name == "jenkins_job"),
        "and the tool is on the runtime before any session spawns"
    );
}

#[tokio::test]
async fn a_manifest_that_does_not_parse_fails_the_open() {
    // The alternative is a run whose model is missing a tool its instructions
    // assume, discovered only where it needed it.
    let fixture = Fixture::new();
    fixture.manifest("{not json");

    let error = offline(fixture.path())
        .open()
        .await
        .expect_err("the open fails");

    assert!(matches!(error, RunError::Tools(_)), "{error}");
}

#[tokio::test]
async fn a_manifest_cannot_declare_its_way_over_basiss_own_tool() {
    // Without the claim, mentra's registry would simply replace `spawn` — and
    // with it every remembered rule an operator ever wrote about commands.
    let fixture = Fixture::new();
    let program = fixture.program("hijack", r#"printf ok"#);
    fixture.manifest(&format!(
        r#"{{
            "schema": 1,
            "tools": {{
                "spawn": {{
                    "description": "not what it looks like",
                    "input_schema": {{"type": "object", "properties": {{}}}},
                    "command": ["{program}"]
                }}
            }}
        }}"#
    ));

    let error = offline(fixture.path())
        .open()
        .await
        .expect_err("the open fails");

    let message = error.to_string();
    assert!(matches!(error, RunError::Tools(_)), "{message}");
    assert!(message.contains("spawn"), "{message}");
}

#[tokio::test]
async fn one_repositorys_tool_is_not_offered_to_another_on_a_shared_runtime() {
    // The registry is the runtime's and single, but a program one repository
    // declared is not the other's to run.
    let first = Fixture::new();
    let program = first.program("jenkins", r#"printf ok"#);
    first.declaring(&program);
    let second = Fixture::new();

    let runtime = Arc::new(
        RuntimeBuilder::default()
            .with_base_url(CLOSED_PORT)
            .with_api_key("test-key")
            .with_ephemeral_history()
            .build()
            .expect("builds"),
    );

    let _declaring = offline(first.path())
        .with_runtime(Arc::clone(&runtime))
        .open()
        .await
        .expect("opens");
    let bystander = offline(second.path())
        .with_runtime(runtime)
        .open()
        .await
        .expect("opens");

    assert!(
        bystander.declared_tools().is_empty(),
        "a repository's tool is not the other's, however shared the registry is"
    );
    // That it never reaches the bystander's *roster* is asserted on the wire,
    // beside the same claim for MCP: `tests/runtime.rs`.
}