basis 0.2.0

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
//! Assembly-level tests for the run pipeline.
//!
//! These drive a real mentra [`Runtime`](mentra::Runtime) with a scripted
//! provider (`mentra::test::MockRuntime`), so everything between "prompt in"
//! and "JSONL out" is exercised โ€” session lifecycle, event forwarding, the
//! mapping, and the stream's bookends โ€” with no network call and no cost.
//!
//! This is the harness `docs/p0-groundwork.md` ยง4a records as already shipped
//! in mentra; basis's tests are its first consumer.

use std::path::{Path, PathBuf};

use basis::{
    Bound, CollectingSink, Event, JsonlWriter, RunConfig, RunOutcome, run::prepare_with_session,
};
use mentra::{
    RuntimePolicy,
    test::{MockRuntime, MockToolCall},
};
use serde_json::Value;

/// A workspace with an `AGENTS.md`, so context discovery has something to find.
fn workspace_with_context(body: &str) -> tempfile::TempDir {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(dir.path().join("AGENTS.md"), body).expect("write AGENTS.md");
    dir
}

/// Config pinned to the given workspace, with the parent walk and the global
/// file switched off so a real `AGENTS.md` above the temp dir cannot leak in.
fn config(workspace: &Path, prompt: &str) -> RunConfig {
    RunConfig::new(workspace, prompt).with_context(basis::ContextConfig {
        file_name: "AGENTS.md".to_string(),
        global_dir: None,
        walk_parents: false,
    })
}

fn mock(chunks: &[&str]) -> MockRuntime {
    MockRuntime::builder()
        .model("mock-model", "openai")
        .with_policy(RuntimePolicy::permissive())
        .stream_text(chunks.to_vec())
        .build()
        .expect("mock runtime builds")
}

#[tokio::test]
async fn a_run_streams_deltas_between_the_bookends() {
    let workspace = workspace_with_context("Always be brief.");
    let mock = mock(&["Hel", "lo ", "world"]);
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(workspace.path(), "say hello");
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");

    let report = prepared
        .execute(CollectingSink::new())
        .await
        .expect("run completes");

    assert!(report.succeeded());
    assert_eq!(report.final_message.as_deref(), Some("Hello world"));

    let events = report.sink.into_events();
    assert!(
        matches!(events.first(), Some(Event::RunStarted { .. })),
        "the stream must open with the header"
    );
    assert!(
        matches!(
            events.last(),
            Some(Event::RunFinished {
                outcome: RunOutcome::Ok,
                ..
            })
        ),
        "the stream must close with the outcome"
    );

    let deltas: String = events
        .iter()
        .filter_map(|event| match event {
            Event::AssistantDelta { text } => Some(text.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(deltas, "Hello world");

    assert!(
        events.iter().any(
            |event| matches!(event, Event::AssistantMessage { text } if text == "Hello world")
        ),
        "the completed message must appear too"
    );
}

#[tokio::test]
async fn the_header_reports_the_context_that_was_loaded() {
    let workspace = workspace_with_context("Prefer small diffs.");
    let mock = mock(&["ok"]);
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(workspace.path(), "go");
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");
    let report = prepared
        .execute(CollectingSink::new())
        .await
        .expect("run completes");

    let Some(Event::RunStarted {
        schema,
        context_files,
        model,
        provider,
        ..
    }) = report.sink.events().first()
    else {
        panic!("expected the header first");
    };

    assert_eq!(*schema, basis::EVENT_SCHEMA_VERSION);
    assert_eq!(model, "mock-model");
    assert_eq!(provider, "openai");
    assert_eq!(context_files.len(), 1);
    assert_eq!(context_files[0].scope, "workspace");

    // Discovery resolves symlinks (on macOS the temp dir is one), so the
    // reported path is the resolved spelling โ€” and the header's `workspace`
    // must agree with it rather than echoing what was typed.
    let resolved = workspace
        .path()
        .canonicalize()
        .expect("canonical workspace");
    assert_eq!(context_files[0].path, resolved.join("AGENTS.md"));

    let Some(Event::RunStarted {
        workspace: reported,
        ..
    }) = report.sink.events().first()
    else {
        unreachable!("already matched above");
    };
    assert_eq!(reported, &resolved);
}

#[tokio::test]
async fn workspace_context_reaches_the_model_as_the_system_prompt() {
    let workspace = workspace_with_context("SENTINEL-RULE: never guess.");
    let mock = mock(&["done"]);
    let session = mock
        .runtime()
        .create_session_with_config(
            "test",
            mock.model(),
            mentra::agent::AgentConfig {
                system: basis::WorkspaceContext::discover_with(
                    workspace.path(),
                    &basis::ContextConfig {
                        file_name: "AGENTS.md".to_string(),
                        global_dir: None,
                        walk_parents: false,
                    },
                )
                .expect("discovery")
                .render(),
                ..Default::default()
            },
        )
        .expect("session");

    let config = config(workspace.path(), "go");
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");
    prepared
        .execute(CollectingSink::new())
        .await
        .expect("run completes");

    let requests = mock.recorded_requests().await;
    let system = requests
        .first()
        .expect("a request was made")
        .system
        .as_deref()
        .expect("a system prompt was sent");

    assert!(
        system.contains("SENTINEL-RULE: never guess."),
        "the workspace's own instructions must reach the model"
    );
}

#[tokio::test]
async fn tool_calls_appear_on_the_stream_with_parsed_input() {
    let workspace = workspace_with_context("rules");
    let mock = MockRuntime::builder()
        .model("mock-model", "openai")
        .with_policy(RuntimePolicy::permissive())
        .tool_calls(vec![MockToolCall::new(
            "files",
            serde_json::json!({"operations": [{"op": "list", "path": "."}]}),
        )])
        .text("listed")
        .build()
        .expect("mock runtime builds");
    let session = mock
        .runtime()
        .create_session_with_config(
            "test",
            mock.model(),
            mentra::agent::AgentConfig {
                workspace: mentra::agent::WorkspaceConfig {
                    base_dir: workspace.path().to_path_buf(),
                    ..Default::default()
                },
                ..Default::default()
            },
        )
        .expect("session");

    let config = config(workspace.path(), "list the files");
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");
    let report = prepared
        .execute(CollectingSink::new())
        .await
        .expect("run completes");

    let events = report.sink.into_events();
    let queued = events
        .iter()
        .find_map(|event| match event {
            Event::ToolQueued {
                tool_name, input, ..
            } => Some((tool_name, input)),
            _ => None,
        })
        .expect("a tool was queued");

    assert_eq!(queued.0, "files");
    assert_eq!(
        queued.1["operations"][0]["op"], "list",
        "tool input must arrive as JSON, not a JSON-encoded string"
    );

    let completed = events
        .iter()
        .find_map(|event| match event {
            Event::ToolCompleted {
                tool_call_id,
                tool_name,
                is_error,
                ..
            } => Some((tool_call_id.as_str(), tool_name.as_str(), *is_error)),
            _ => None,
        })
        .expect("the call must also be reported as completed");

    assert_eq!(completed.0, "tool-1");
    assert_eq!(
        completed.1, "files",
        "completion must name its tool (oops-rs/mentra#9)"
    );
    assert!(!completed.2, "the scripted call should succeed");
}

#[tokio::test]
async fn the_jsonl_rendering_is_one_parseable_object_per_line() {
    let workspace = workspace_with_context("rules");
    let mock = mock(&["multi\nline", " answer"]);
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(workspace.path(), "go");
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");
    let report = prepared
        .execute(JsonlWriter::new(Vec::new()))
        .await
        .expect("run completes");

    let output = String::from_utf8(report.sink.into_inner()).expect("utf-8");
    let lines: Vec<Value> = output
        .lines()
        .map(|line| serde_json::from_str(line).expect("every line parses as json"))
        .collect();

    assert_eq!(lines[0]["type"], "run_started");
    assert_eq!(lines[0]["schema"], basis::EVENT_SCHEMA_VERSION);
    assert_eq!(lines[lines.len() - 1]["type"], "run_finished");
    assert_eq!(lines[lines.len() - 1]["status"], "ok");

    let sequence: Vec<u64> = lines
        .iter()
        .map(|line| line["seq"].as_u64().expect("a sequence number"))
        .collect();
    let expected: Vec<u64> = (0..lines.len() as u64).collect();
    assert_eq!(
        sequence, expected,
        "sequence numbers must be dense and ordered"
    );
}

#[tokio::test]
async fn a_failing_turn_still_closes_the_stream() {
    let workspace = workspace_with_context("rules");
    let mock = MockRuntime::builder()
        .model("mock-model", "openai")
        .with_policy(RuntimePolicy::permissive())
        .failure(mentra::ProviderError::UnsupportedCapability(
            "scripted failure".to_string(),
        ))
        .build()
        .expect("mock runtime builds");
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(workspace.path(), "go");
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");
    let report = prepared
        .execute(CollectingSink::new())
        .await
        .expect("the run itself reports rather than erroring");

    assert!(!report.succeeded());
    assert_eq!(report.final_message, None);

    let events = report.sink.into_events();
    assert!(matches!(events.first(), Some(Event::RunStarted { .. })));
    assert!(
        matches!(
            events.last(),
            Some(Event::RunFinished {
                outcome: RunOutcome::Error { .. },
                ..
            })
        ),
        "a failed turn must still terminate the stream"
    );
}

/// A run whose deadline has already passed, so the bound trips on the first
/// check rather than after a wall-clock wait.
#[tokio::test]
async fn a_tripped_bound_is_reported_as_a_bound_not_just_a_failure() {
    let workspace = workspace_with_context("rules");
    let mock = mock(&["never streamed"]);
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(workspace.path(), "go").with_deadline(std::time::Duration::ZERO);
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");

    let report = prepared
        .execute(CollectingSink::new())
        .await
        .expect("a bound ends the run, it does not break it");

    // Both halves matter. The stream still says the run did not finish, which
    // is what a client reading events needs; and the report says *why*, which
    // is what the exit code needs โ€” ADR-0015 asks a shell script to tell "out
    // of time" from "the provider refused" without parsing a message.
    assert!(!report.succeeded());
    assert_eq!(report.stopped_by, Some(Bound::Deadline));

    assert!(
        matches!(
            report.sink.into_events().last(),
            Some(Event::RunFinished {
                outcome: RunOutcome::Error { .. },
                ..
            })
        ),
        "a bounded run must still terminate the stream"
    );
}

#[tokio::test]
async fn a_healthy_run_ran_into_no_bound() {
    let workspace = workspace_with_context("rules");
    let mock = mock(&["done"]);
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(workspace.path(), "go");
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");

    let report = prepared
        .execute(CollectingSink::new())
        .await
        .expect("run completes");

    assert!(report.succeeded());
    assert_eq!(report.stopped_by, None);
}

#[tokio::test]
async fn an_empty_prompt_is_refused_when_it_would_be_sent() {
    let workspace = workspace_with_context("rules");
    let mock = mock(&["unused"]);
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(workspace.path(), "  \t\n ");

    // Preparing is fine โ€” a session with nothing said yet is what ACP's
    // `session/new` opens. Sending is where the prompt has to be real.
    let mut prepared =
        prepare_with_session(session, &config, "openai", "mock-model").expect("prepared");

    assert!(prepared.execute(CollectingSink::new()).await.is_err());
}

#[tokio::test]
async fn a_missing_workspace_is_refused() {
    let mock = mock(&["unused"]);
    let session = mock
        .runtime()
        .create_session("test", mock.model())
        .expect("session");

    let config = config(&PathBuf::from("/definitely/not/a/real/path"), "go");

    assert!(prepare_with_session(session, &config, "openai", "mock-model").is_err());
}

/// The `.git` carve-out, proven where it matters: through a real runtime, on a
/// tool call the model actually makes.
///
/// A file under `.git/hooks` is a program git runs on the next commit, so an
/// agent that can write one executes code outside anything basis's approval or
/// policy governs. basis denies those paths by default; this asserts the denial
/// reaches the tool rather than living only in a config field.
#[tokio::test]
async fn a_write_into_git_hooks_is_refused() {
    let workspace = workspace_with_context("rules");
    std::fs::create_dir_all(workspace.path().join(".git").join("hooks")).expect("hooks dir");

    let policy = mentra::RuntimePolicy::workspace_bounded(workspace.path())
        .with_denied_write_root(workspace.path().join(".git").join("hooks"));

    let mock = MockRuntime::builder()
        .model("mock-model", "openai")
        .with_policy(policy)
        .tool_calls(vec![MockToolCall::new(
            "files",
            serde_json::json!({
                "operations": [{
                    "op": "create",
                    "path": ".git/hooks/pre-commit",
                    "content": "#!/bin/sh\nexfiltrate\n"
                }]
            }),
        )])
        .text("could not")
        .build()
        .expect("mock runtime builds");

    let session = mock
        .runtime()
        .create_session_with_config(
            "test",
            mock.model(),
            mentra::agent::AgentConfig {
                workspace: mentra::agent::WorkspaceConfig {
                    base_dir: workspace.path().to_path_buf(),
                    ..Default::default()
                },
                ..Default::default()
            },
        )
        .expect("session");

    let config = config(workspace.path(), "install a hook");
    let report = prepare_with_session(session, &config, "openai", "mock-model")
        .expect("prepared")
        .execute(CollectingSink::new())
        .await
        .expect("the run reports rather than erroring");

    let failed = report
        .sink
        .events()
        .iter()
        .find_map(|event| match event {
            Event::ToolCompleted { is_error, .. } => Some(*is_error),
            _ => None,
        })
        .expect("the write was attempted");

    assert!(failed, "writing a git hook must fail");
    assert!(
        !workspace.path().join(".git/hooks/pre-commit").exists(),
        "and must not reach the disk"
    );
}