basis-cli 0.4.0

The basis CLI, over the basis SDK and basis-acp: headless runs, an ACP server, and a websocket bridge. Installs as `basis`.
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
//! The bridge, driven by a real ACP client over a real websocket.
//!
//! Everything here is genuine except the model: basis's server, the
//! `agent-client-protocol` client, a TCP socket on loopback, and a real
//! WebSocket handshake between them. That is deliberate — the bridge exists to
//! serve a client basis does not ship, so the thing worth testing is that a
//! client which knows nothing about basis can drive it end to end.
//!
//! Loopback is not "the network" in the sense the project rules forbid: no
//! packet leaves the machine, no name is resolved, and the port is whichever
//! one the OS hands out. Every test is timed out, because the failure modes
//! being guarded against — a handshake that never completes, a dispatch loop
//! that blocks — present as a hang.

use std::{net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};

use agent_client_protocol::{
    Agent, Client, ConnectionTo,
    schema::{
        ProtocolVersion,
        v1::{
            ContentBlock, InitializeRequest, NewSessionRequest, PromptRequest, SessionNotification,
            SessionUpdate, StopReason, TextContent,
        },
    },
};
use basis::{PreparedRun, RunConfig, RunError, run::prepare_with_session};
use basis_acp::{ServeConfig, SessionSource};
use mentra::{
    RuntimePolicy,
    test::{MockRuntime, MockToolCall},
};
use tokio_tungstenite::{
    connect_async,
    tungstenite::{
        Error as WsError,
        client::IntoClientRequest,
        http::{HeaderValue, StatusCode, header},
    },
};

use super::{Bridge, BridgeConfig, websocket_transport};

/// Everything here is local and scripted; exceeding this means something is
/// stuck, which is what these tests exist to catch.
const NOT_STUCK: Duration = Duration::from_secs(10);

const A_PAGE: &str = "http://localhost:5173";

/// Serves sessions over a scripted runtime, so the transport is exercised
/// without a provider.
struct MockSource {
    mock: Arc<MockRuntime>,
    workspace: PathBuf,
}

#[async_trait::async_trait]
impl SessionSource for MockSource {
    async fn create(
        &self,
        _cwd: PathBuf,
        _mcp: Vec<basis::McpServer>,
    ) -> Result<PreparedRun, RunError> {
        let session = self
            .mock
            .runtime()
            .create_session_with_config(
                "test",
                self.mock.model(),
                mentra::agent::AgentConfig {
                    workspace: mentra::agent::WorkspaceConfig {
                        base_dir: self.workspace.clone(),
                        ..Default::default()
                    },
                    ..Default::default()
                },
            )
            .expect("session");

        let config = RunConfig::new(&self.workspace, "").with_context(basis::ContextConfig {
            file_name: "AGENTS.md".to_string(),
            global_dir: None,
            walk_parents: false,
        });

        prepare_with_session(session, &config, "openai", "mock-model")
    }
}

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

/// Starts a bridge on a port the OS picks, and answers with its address.
///
/// Port 0 rather than [`DEFAULT_PORT`](super::DEFAULT_PORT) so tests never collide
/// with each other or with a bridge someone is running.
async fn start(config: BridgeConfig, mock: Arc<MockRuntime>, workspace: PathBuf) -> SocketAddr {
    let bridge = Bridge::bind(config)
        .await
        .expect("a loopback port needs no opt-in");
    let address = bridge.local_addr().expect("the listener is bound");

    tokio::spawn(bridge.serve(ServeConfig::with_source(MockSource { mock, workspace })));

    address
}

fn loopback() -> BridgeConfig {
    BridgeConfig::new(SocketAddr::from(([127, 0, 0, 1], 0)))
}

/// Opens a websocket, optionally claiming to be a page at `origin`.
async fn dial(
    address: SocketAddr,
    origin: Option<&str>,
) -> Result<
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>,
    WsError,
> {
    let mut request = format!("ws://{address}/")
        .into_client_request()
        .expect("a well-formed ws url");

    if let Some(origin) = origin {
        request.headers_mut().insert(
            header::ORIGIN,
            HeaderValue::from_str(origin).expect("a header value"),
        );
    }

    connect_async(request)
        .await
        .map(|(socket, _response)| socket)
}

/// What a client saw over one connection.
#[derive(Default)]
struct Observed {
    updates: Vec<SessionUpdate>,
}

impl Observed {
    fn agent_text(&self) -> String {
        self.updates
            .iter()
            .filter_map(|update| match update {
                SessionUpdate::AgentMessageChunk(chunk) => match &chunk.content {
                    ContentBlock::Text(text) => Some(text.text.as_str()),
                    _ => None,
                },
                _ => None,
            })
            .collect()
    }

    fn tool_calls(&self) -> usize {
        self.updates
            .iter()
            .filter(|update| matches!(update, SessionUpdate::ToolCall(_)))
            .count()
    }
}

/// Runs one whole conversation over an open socket: initialize, a session, and
/// every prompt in turn. Answers with the stop reasons and what streamed back.
async fn converse(
    socket: tokio_tungstenite::WebSocketStream<
        tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
    >,
    prompts: Vec<&str>,
) -> (String, Vec<StopReason>, Arc<std::sync::Mutex<Observed>>) {
    let observed = Arc::new(std::sync::Mutex::new(Observed::default()));
    let seen = Arc::clone(&observed);
    let prompts: Vec<String> = prompts.into_iter().map(str::to_string).collect();

    let driven = Client
        .builder()
        .on_receive_notification(
            move |notification: SessionNotification, _connection| {
                let seen = Arc::clone(&seen);
                async move {
                    seen.lock()
                        .expect("not poisoned")
                        .updates
                        .push(notification.update);
                    Ok(())
                }
            },
            agent_client_protocol::on_receive_notification!(),
        )
        .connect_with(
            websocket_transport(socket),
            |connection: ConnectionTo<Agent>| async move {
                connection
                    .send_request(InitializeRequest::new(ProtocolVersion::V1))
                    .block_task()
                    .await?;

                let session = connection
                    .send_request(NewSessionRequest::new(PathBuf::from(".")))
                    .block_task()
                    .await?;

                let mut stop_reasons = Vec::new();
                for prompt in prompts {
                    let response = connection
                        .send_request(PromptRequest::new(
                            session.session_id.clone(),
                            vec![ContentBlock::Text(TextContent::new(prompt))],
                        ))
                        .block_task()
                        .await?;
                    stop_reasons.push(response.stop_reason);
                }

                Ok((session.session_id.0.to_string(), stop_reasons))
            },
        );

    let (session_id, stop_reasons) = tokio::time::timeout(NOT_STUCK, driven)
        .await
        .expect("a conversation over loopback must not hang")
        .expect("the client drives cleanly");

    (session_id, stop_reasons, observed)
}

fn workspace() -> tempfile::TempDir {
    tempfile::tempdir().expect("tempdir")
}

#[tokio::test]
async fn a_client_drives_a_whole_conversation_over_the_socket() {
    let workspace = workspace();
    let mock = Arc::new(text_mock(&["Hel", "lo ", "bridge"]));
    let address = start(loopback(), mock, workspace.path().to_path_buf()).await;

    let socket = dial(address, None).await.expect("the handshake succeeds");
    let (_, stop_reasons, observed) = converse(socket, vec!["say hello"]).await;

    assert_eq!(stop_reasons, vec![StopReason::EndTurn]);
    assert_eq!(
        observed.lock().expect("not poisoned").agent_text(),
        "Hello bridge",
        "every chunk must arrive, in order, as its own frame"
    );
}

#[tokio::test]
async fn a_second_turn_over_one_socket_sees_the_first() {
    let workspace = workspace();
    let mock = Arc::new(
        MockRuntime::builder()
            .model("mock-model", "openai")
            .with_policy(RuntimePolicy::permissive())
            .text("first")
            .text("second")
            .build()
            .expect("mock runtime builds"),
    );
    let address = start(
        loopback(),
        Arc::clone(&mock),
        workspace.path().to_path_buf(),
    )
    .await;

    let socket = dial(address, None).await.expect("the handshake succeeds");
    let (_, stop_reasons, _) = converse(socket, vec!["one", "two"]).await;

    assert_eq!(stop_reasons, vec![StopReason::EndTurn, StopReason::EndTurn]);

    // The conversation is a conversation, not two one-shots: the second
    // request carries the first exchange, exactly as it does over stdio.
    let requests = mock.recorded_requests().await;
    let second = requests.get(1).expect("the second turn was sent");
    assert!(
        second
            .messages
            .iter()
            .any(|message| message.text().contains("one")),
        "the second turn must carry the first"
    );
}

#[tokio::test]
async fn a_tool_round_survives_the_transport() {
    let workspace = workspace();
    let mock = Arc::new(
        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 them")
            .build()
            .expect("mock runtime builds"),
    );
    let address = start(loopback(), mock, workspace.path().to_path_buf()).await;

    let socket = dial(address, None).await.expect("the handshake succeeds");
    let (_, stop_reasons, observed) = converse(socket, vec!["list the files"]).await;

    assert_eq!(stop_reasons, vec![StopReason::EndTurn]);
    assert!(
        observed.lock().expect("not poisoned").tool_calls() > 0,
        "a tool call is a notification, and notifications must reach the client too"
    );
}

#[tokio::test]
async fn a_page_from_an_unnamed_origin_is_refused() {
    let workspace = workspace();
    let mock = Arc::new(text_mock(&["never reached"]));
    let address = start(loopback(), mock, workspace.path().to_path_buf()).await;

    let refused = tokio::time::timeout(NOT_STUCK, dial(address, Some("https://evil.example")))
        .await
        .expect("a refusal must not hang")
        .expect_err("a page nobody named must not be served");

    // The point is that the *handshake* fails: nothing ACP-shaped is ever
    // exchanged with a page that was not allowed in.
    assert!(
        matches!(&refused, WsError::Http(response) if response.status() == StatusCode::FORBIDDEN),
        "the refusal must say why, in a status a page can see: {refused:?}"
    );
}

#[tokio::test]
async fn a_page_from_a_named_origin_is_served() {
    let workspace = workspace();
    let mock = Arc::new(text_mock(&["hello page"]));
    let address = start(
        loopback().with_origins([A_PAGE.to_string()]),
        mock,
        workspace.path().to_path_buf(),
    )
    .await;

    let socket = dial(address, Some(A_PAGE))
        .await
        .expect("a named origin is admitted");
    let (_, stop_reasons, observed) = converse(socket, vec!["hello"]).await;

    assert_eq!(stop_reasons, vec![StopReason::EndTurn]);
    assert_eq!(
        observed.lock().expect("not poisoned").agent_text(),
        "hello page"
    );
}

#[tokio::test]
async fn a_named_origin_does_not_admit_its_neighbours() {
    let workspace = workspace();
    let mock = Arc::new(text_mock(&["never reached"]));
    let address = start(
        loopback().with_origins([A_PAGE.to_string()]),
        mock,
        workspace.path().to_path_buf(),
    )
    .await;

    for near_miss in ["http://localhost:5174", "https://localhost:5173"] {
        let refused = tokio::time::timeout(NOT_STUCK, dial(address, Some(near_miss)))
            .await
            .expect("a refusal must not hang")
            .expect_err("only the origin that was named is served");

        assert!(
            matches!(&refused, WsError::Http(response) if response.status() == StatusCode::FORBIDDEN),
            "{near_miss} is a different origin: {refused:?}"
        );
    }
}

#[tokio::test]
async fn a_native_client_is_served_even_when_no_page_is() {
    let workspace = workspace();
    let mock = Arc::new(text_mock(&["hello native"]));
    // No allowlist at all — the default. A client sending no `Origin` is not a
    // browser, and refusing it would protect nothing it could not already do.
    let address = start(loopback(), mock, workspace.path().to_path_buf()).await;

    let socket = dial(address, None)
        .await
        .expect("a client with no origin is not a page");
    let (_, stop_reasons, _) = converse(socket, vec!["hello"]).await;

    assert_eq!(stop_reasons, vec![StopReason::EndTurn]);
}

#[tokio::test]
async fn two_connections_are_two_conversations() {
    let workspace = workspace();
    // One scripted turn per client. `text_mock` streams its chunks as a single
    // turn, so two conversations need two `.text(..)` replies, not two chunks.
    let mock = Arc::new(
        MockRuntime::builder()
            .model("mock-model", "openai")
            .with_policy(RuntimePolicy::permissive())
            .text("hello")
            .text("hello again")
            .build()
            .expect("mock runtime builds"),
    );
    let address = start(loopback(), mock, workspace.path().to_path_buf()).await;

    let first = dial(address, None)
        .await
        .expect("the first client connects");
    let (first_session, _, _) = converse(first, vec!["hello"]).await;

    let second = dial(address, None)
        .await
        .expect("a second client connects too");
    let (second_session, _, _) = converse(second, vec!["hello"]).await;

    assert_ne!(
        first_session, second_session,
        "one connection is one conversation, exactly as one stdio process is"
    );
}

#[tokio::test]
async fn a_refused_page_does_not_take_the_bridge_down() {
    let workspace = workspace();
    let mock = Arc::new(text_mock(&["still here"]));
    let address = start(loopback(), mock, workspace.path().to_path_buf()).await;

    let _ = tokio::time::timeout(NOT_STUCK, dial(address, Some("https://evil.example")))
        .await
        .expect("a refusal must not hang")
        .expect_err("the page is refused");

    let socket = dial(address, None)
        .await
        .expect("the bridge is still accepting");
    let (_, stop_reasons, observed) = converse(socket, vec!["hello"]).await;

    assert_eq!(stop_reasons, vec![StopReason::EndTurn]);
    assert_eq!(
        observed.lock().expect("not poisoned").agent_text(),
        "still here",
        "one bad client must not end every other conversation"
    );
}