haematite-cli 0.21.12

The haematite command: serve a store, its door and Studio, answer MCP, query with jq, and host the store's assistant seat
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
use super::{
    config::{Place, Seat},
    handshake::Session,
    tools::Identity,
};
use crate::door::assistant_network::{OPCODE_BINARY, OPCODE_CLOSE, accept, encode};
use serde_json::{Value, json};
use sha2::{Digest as _, Sha256};
use tokio::io::{
    AsyncBufRead, AsyncBufReadExt as _, AsyncRead, AsyncReadExt as _, AsyncWrite,
    AsyncWriteExt as _, BufReader,
};
use tokio::net::UnixStream;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;

/// The bound on the door's answer to the upgrade: its head, or its
/// refusal's body.
const HEAD_BYTES: usize = 16_384;

type Reader = Box<dyn AsyncRead + Unpin + Send>;
type Writer = Box<dyn AsyncWrite + Unpin + Send>;

/// How frames travel: newline-delimited on the socket, one binary message
/// each on the network (HAEMNET-003 R2).
enum Wire {
    Lines,
    Messages(Masks),
}

enum Pending {
    Frame(Value),
    Flush(tokio::sync::oneshot::Sender<()>),
}

#[derive(Clone)]
pub(super) struct Output {
    sender: mpsc::Sender<Pending>,
    bound: usize,
}
impl Output {
    pub(super) async fn send_wait(&self, value: Value) -> anyhow::Result<()> {
        anyhow::ensure!(
            serde_json::to_vec(&value)?.len().saturating_add(1) <= self.bound,
            "door_frame_bytes_exceeded"
        );
        self.sender
            .send(Pending::Frame(value))
            .await
            .map_err(|_| anyhow::anyhow!("door_writer_closed"))
    }

    pub async fn flush(&self) -> anyhow::Result<()> {
        let (sent, received) = tokio::sync::oneshot::channel();
        self.sender
            .send(Pending::Flush(sent))
            .await
            .map_err(|_| anyhow::anyhow!("door_writer_closed"))?;
        received
            .await
            .map_err(|_| anyhow::anyhow!("door_flush_failed"))
    }

    pub fn send(&self, value: Value) -> anyhow::Result<()> {
        anyhow::ensure!(
            serde_json::to_vec(&value)?.len().saturating_add(1) <= self.bound,
            "door_frame_bytes_exceeded"
        );
        match self.sender.try_send(Pending::Frame(value)) {
            Ok(()) => Ok(()),
            Err(mpsc::error::TrySendError::Full(frame)) => self
                .sender
                .blocking_send(frame)
                .map_err(|_| anyhow::anyhow!("door_writer_closed")),
            Err(mpsc::error::TrySendError::Closed(_)) => anyhow::bail!("door_writer_closed"),
        }
    }
}
pub(super) struct Link {
    pub person: String,
    pub input: mpsc::Receiver<anyhow::Result<Option<Value>>>,
    pub output: Output,
    pub reader: JoinHandle<()>,
    pub writer: JoinHandle<anyhow::Result<()>>,
}
impl Link {
    pub async fn connect(
        seat: &Seat,
        identity: &Identity,
        session: &Session,
    ) -> anyhow::Result<Self> {
        let trust = match &seat.place {
            Place::Local { .. } => None,
            Place::Network { .. } => {
                Some(crate::client::network::Trust::system().map_err(anyhow::Error::msg)?)
            }
        };
        Self::connect_with(seat, identity, session, trust.as_ref()).await
    }
    /// [`Self::connect`] with the roots a network door's certificate is
    /// verified against: the system's, or a test's own authority.
    pub async fn connect_with(
        seat: &Seat,
        identity: &Identity,
        session: &Session,
        trust: Option<&crate::client::network::Trust>,
    ) -> anyhow::Result<Self> {
        let (reader, mut writer, wire): (BufReader<Reader>, Writer, Wire) = match &seat.place {
            Place::Local { door_socket, .. } => {
                let (reader, writer) = local(door_socket).await?;
                (BufReader::new(reader), writer, Wire::Lines)
            }
            Place::Network { door_url, .. } => {
                let trust = trust.ok_or_else(|| anyhow::anyhow!("assistant_door_trust_absent"))?;
                let timeout = std::time::Duration::from_millis(seat.harness.session_timeout_ms);
                let (reader, writer, masks) = network(door_url, trust, timeout).await?;
                (reader, writer, Wire::Messages(masks))
            }
        };
        let bound = seat.harness.acp_frame_bytes;
        let (outgoing, mut pending) = mpsc::channel::<Pending>(seat.harness.write_queue);
        let messages = matches!(wire, Wire::Messages(_));
        let writer = tokio::spawn(async move {
            let mut wire = wire;
            while let Some(value) = pending.recv().await {
                let value = match value {
                    Pending::Frame(value) => value,
                    Pending::Flush(receipt) => {
                        writer.flush().await?;
                        receipt
                            .send(())
                            .map_err(|()| anyhow::anyhow!("door_flush_receiver_closed"))?;
                        continue;
                    }
                };
                let mut bytes = serde_json::to_vec(&value)?;
                match &mut wire {
                    Wire::Lines => bytes.push(b'\n'),
                    Wire::Messages(masks) => {
                        bytes = encode(OPCODE_BINARY, &bytes, Some(masks.mask()))?;
                    }
                }
                writer.write_all(&bytes).await?;
                writer.flush().await?;
            }
            Ok(())
        });
        let (events, input) = mpsc::channel(1);
        let reader = tokio::spawn(async move {
            let mut reader = reader;
            loop {
                let value = if messages {
                    read_message(&mut reader, bound).await
                } else {
                    read(&mut reader, bound).await
                };
                let ended = !matches!(value, Ok(Some(_)));
                if events.send(value).await.is_err() || ended {
                    return;
                }
            }
        });
        let link = Self {
            person: String::new(),
            input,
            output: Output {
                sender: outgoing,
                bound,
            },
            reader,
            writer,
        };
        link.output.send_wait(json!({"hello":{"agent":identity.agent,"pass":identity.pass,"store":seat.store,
            "resumable":session.resumable,"harness":{"id":seat.harness.id,"protocol":session.version.number(),
                "version":session.info["version"].as_str().unwrap_or("not-advertised")}}})).await?;
        Ok(link)
    }
    #[cfg(test)]
    pub async fn open(seat: &Seat, identity: &Identity, session: &Session) -> anyhow::Result<Self> {
        let mut link = Self::connect(seat, identity, session).await?;
        link.welcome(seat, identity).await?;
        Ok(link)
    }
    pub async fn welcome(&mut self, seat: &Seat, identity: &Identity) -> anyhow::Result<()> {
        let welcome = self
            .input
            .recv()
            .await
            .ok_or_else(|| anyhow::anyhow!("assistant_door_closed"))??
            .ok_or_else(|| anyhow::anyhow!("assistant_door_closed"))?;
        if let Some(refused) = welcome.get("refused") {
            let code = refused["code"]
                .as_str()
                .filter(|code| {
                    !code.is_empty()
                        && code.len() <= 96
                        && !code.contains(&identity.pass)
                        && code
                            .bytes()
                            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
                })
                .unwrap_or("unnamed_refusal");
            anyhow::bail!("assistant_door_refused: {code}");
        }
        let person = welcome["welcome"]["person"]
            .as_str()
            .filter(|person| !person.is_empty() && person.len() <= seat.harness.content_bytes)
            .ok_or_else(|| anyhow::anyhow!("assistant_door_welcome_invalid"))?;
        person.clone_into(&mut self.person);
        Ok(())
    }
}
impl Drop for Link {
    fn drop(&mut self) {
        self.reader.abort();
        self.writer.abort();
    }
}
/// The door's assistant socket, checked as it always was: a socket owned by
/// this user, mode 0600, whose peer is this user too.
async fn local(door_socket: &std::path::Path) -> anyhow::Result<(Reader, Writer)> {
    use std::os::unix::fs::{FileTypeExt as _, MetadataExt as _, PermissionsExt as _};
    let metadata = std::fs::symlink_metadata(door_socket)?;
    anyhow::ensure!(
        metadata.file_type().is_socket()
            && metadata.permissions().mode() & 0o777 == 0o600
            && metadata.uid() == rustix::process::geteuid().as_raw(),
        "assistant_socket_requires_owner_and_mode_0600"
    );
    let stream = UnixStream::connect(door_socket).await?;
    anyhow::ensure!(
        stream.peer_cred()?.uid() == rustix::process::geteuid().as_raw(),
        "assistant_socket_owner_differs"
    );
    let (reader, writer) = stream.into_split();
    Ok((Box::new(reader), Box::new(writer)))
}

/// Masks and the handshake's key, each derived from 32 random bytes read
/// once, so a peer cannot predict them (RFC 6455 section 10.3) and no frame
/// reads the random source again.
struct Masks {
    seed: [u8; 32],
    next: u64,
}
impl Masks {
    fn new() -> anyhow::Result<Self> {
        use std::io::Read as _;
        let mut seed = [0_u8; 32];
        std::fs::File::open("/dev/urandom")
            .and_then(|mut source| source.read_exact(&mut seed))
            .map_err(|_| anyhow::anyhow!("assistant_random_unavailable"))?;
        Ok(Self { seed, next: 0 })
    }
    fn take(&mut self) -> [u8; 32] {
        let digest = Sha256::new()
            .chain_update(self.seed)
            .chain_update(self.next.to_be_bytes())
            .finalize();
        self.next = self.next.wrapping_add(1);
        let mut bytes = [0_u8; 32];
        bytes.copy_from_slice(&digest);
        bytes
    }
    fn mask(&mut self) -> [u8; 4] {
        let bytes = self.take();
        [bytes[0], bytes[1], bytes[2], bytes[3]]
    }
    fn key(&mut self) -> String {
        use base64::Engine as _;
        base64::engine::general_purpose::STANDARD.encode(&self.take()[..16])
    }
}

/// Reach the door at its origin over TLS verified against `trust`, and
/// upgrade `GET /assistant` to a WebSocket; the reader is left at the first
/// message.
async fn network(
    door_url: &url::Url,
    trust: &crate::client::network::Trust,
    timeout: std::time::Duration,
) -> anyhow::Result<(BufReader<Reader>, Writer, Masks)> {
    let host = door_url
        .host_str()
        .ok_or_else(|| anyhow::anyhow!("seat_door_url_invalid"))?;
    let port = door_url
        .port_or_known_default()
        .ok_or_else(|| anyhow::anyhow!("seat_door_url_invalid"))?;
    let bare = host.trim_start_matches('[').trim_end_matches(']');
    let name =
        crate::client::network::name(bare).map_err(|_| anyhow::anyhow!("seat_door_url_invalid"))?;
    let connector = tokio_rustls::TlsConnector::from(trust.client_config());
    let reached = tokio::time::timeout(timeout, async {
        let tcp = tokio::net::TcpStream::connect((bare, port)).await?;
        connector.connect(name, tcp).await
    })
    .await
    .map_err(|_| anyhow::anyhow!("assistant_door_unreached: timed out"))?
    .map_err(|error| anyhow::anyhow!("assistant_door_unreached: {}", error.kind()))?;
    let (reader, mut writer) = tokio::io::split(reached);
    let mut masks = Masks::new()?;
    let key = masks.key();
    let origin = door_url.origin().ascii_serialization();
    let authority = &door_url[url::Position::BeforeHost..url::Position::AfterPort];
    writer
        .write_all(
            format!(
                "GET /assistant HTTP/1.1\r\nHost: {authority}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\nOrigin: {origin}\r\n\r\n"
            )
            .as_bytes(),
        )
        .await?;
    writer.flush().await?;
    let mut reader: BufReader<Reader> = BufReader::new(Box::new(reader));
    tokio::time::timeout(timeout, upgraded(&mut reader, &accept(&key)))
        .await
        .map_err(|_| anyhow::anyhow!("assistant_door_upgrade_unanswered"))??;
    Ok((reader, Box::new(writer), masks))
}

/// A 101 carrying the accept value the key asks for; any other answer is
/// the door's refusal, named by its code when its body is the door's JSON.
async fn upgraded(reader: &mut BufReader<Reader>, expected: &str) -> anyhow::Result<()> {
    let mut head = Vec::new();
    while !head.ends_with(b"\r\n\r\n") {
        anyhow::ensure!(
            head.len() < HEAD_BYTES,
            "assistant_door_upgrade_head_too_long"
        );
        head.push(reader.read_u8().await?);
    }
    let head = String::from_utf8(head)
        .map_err(|_| anyhow::anyhow!("assistant_door_upgrade_head_invalid"))?;
    let mut lines = head.split("\r\n");
    let status = lines
        .next()
        .and_then(|line| line.split(' ').nth(1))
        .and_then(|code| code.parse::<u16>().ok())
        .ok_or_else(|| anyhow::anyhow!("assistant_door_upgrade_head_invalid"))?;
    let headers: Vec<(String, &str)> = lines
        .filter_map(|line| line.split_once(':'))
        .map(|(name, value)| (name.trim().to_ascii_lowercase(), value.trim()))
        .collect();
    let header = |name: &str| {
        headers
            .iter()
            .find(|(named, _)| named == name)
            .map(|(_, value)| *value)
    };
    if status == 101 {
        anyhow::ensure!(
            header("upgrade").is_some_and(|value| value.eq_ignore_ascii_case("websocket"))
                && header("sec-websocket-accept") == Some(expected),
            "assistant_door_upgrade_invalid"
        );
        return Ok(());
    }
    let length = header("content-length")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|length| *length <= HEAD_BYTES)
        .unwrap_or(0);
    let mut body = vec![0_u8; length];
    reader.read_exact(&mut body).await?;
    let code = serde_json::from_slice::<Value>(&body)
        .ok()
        .and_then(|value| value["code"].as_str().map(str::to_owned))
        .filter(|code| {
            !code.is_empty()
                && code.len() <= 96
                && code
                    .bytes()
                    .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
        });
    match code {
        Some(code) => anyhow::bail!("assistant_door_refused: {code}"),
        None => anyhow::bail!("assistant_door_upgrade_refused: status {status}"),
    }
}

/// One door message as a frame: `None` at the door's close or the end of
/// the stream. A door frame is never masked, fragmented or text, and its
/// payload with its newline fits `bound`, as on the socket.
async fn read_message(
    reader: &mut BufReader<Reader>,
    bound: usize,
) -> anyhow::Result<Option<Value>> {
    let mut head = [0_u8; 2];
    if reader.read(&mut head[..1]).await? == 0 {
        return Ok(None);
    }
    reader.read_exact(&mut head[1..]).await?;
    anyhow::ensure!(
        head[0] & 0x70 == 0 && head[1] & 0x80 == 0,
        "door_frame_invalid"
    );
    let length = match head[1] & 0x7F {
        126 => u64::from(reader.read_u16().await?),
        127 => reader.read_u64().await?,
        short => u64::from(short),
    };
    let length = usize::try_from(length)
        .ok()
        .filter(|length| *length < bound)
        .ok_or_else(|| anyhow::anyhow!("door_frame_bytes_exceeded"))?;
    let mut payload = vec![0_u8; length];
    reader.read_exact(&mut payload).await?;
    match (head[0] & 0x80 != 0, head[0] & 0x0F) {
        (_, OPCODE_CLOSE) => Ok(None),
        (true, OPCODE_BINARY) => {
            let value: Value = serde_json::from_slice(&payload)
                .map_err(|_| anyhow::anyhow!("door_frame_invalid_json"))?;
            anyhow::ensure!(value.is_object(), "door_frame_requires_object");
            Ok(Some(value))
        }
        _ => anyhow::bail!("door_frame_invalid"),
    }
}

async fn read<R: AsyncBufRead + Unpin>(
    reader: &mut R,
    bound: usize,
) -> anyhow::Result<Option<Value>> {
    let mut line = Vec::new();
    loop {
        let buffer = reader.fill_buf().await?;
        if buffer.is_empty() {
            anyhow::ensure!(line.is_empty(), "door_frame_missing_newline");
            return Ok(None);
        }
        let count = buffer
            .iter()
            .position(|byte| *byte == b'\n')
            .map_or(buffer.len(), |index| index + 1);
        anyhow::ensure!(
            count <= bound.saturating_sub(line.len()),
            "door_frame_bytes_exceeded"
        );
        let ended = buffer[count - 1] == b'\n';
        line.extend_from_slice(&buffer[..count]);
        reader.consume(count);
        if ended {
            break;
        }
    }
    let value: Value =
        serde_json::from_slice(&line).map_err(|_| anyhow::anyhow!("door_frame_invalid_json"))?;
    anyhow::ensure!(value.is_object(), "door_frame_requires_object");
    Ok(Some(value))
}

#[cfg(test)]
mod queue_tests {
    use super::{Output, Pending};
    use serde_json::json;
    use std::{future::Future as _, task::Poll};

    #[tokio::test]
    async fn round4_door_queue_waits_for_capacity_and_names_a_closed_writer() -> anyhow::Result<()>
    {
        let (sender, mut received) = tokio::sync::mpsc::channel(1);
        let output = Output {
            sender,
            bound: 4096,
        };
        output.send_wait(json!({"item":1})).await?;
        let waiting = output.send_wait(json!({"item":2}));
        tokio::pin!(waiting);
        let pending =
            std::future::poll_fn(|cx| Poll::Ready(waiting.as_mut().poll(cx).is_pending())).await;
        assert!(pending, "full door queue did not wait");
        assert!(matches!(received.recv().await,Some(Pending::Frame(value)) if value["item"]==1));
        waiting.await?;
        assert!(matches!(received.recv().await,Some(Pending::Frame(value)) if value["item"]==2));
        drop(received);
        let error = output
            .send_wait(json!({"item":3}))
            .await
            .err()
            .ok_or_else(|| anyhow::anyhow!("closed writer accepted frame"))?;
        assert_eq!(error.to_string(), "door_writer_closed");
        Ok(())
    }
}