warm-rust 0.6.5

Local-first search engine and agent memory: fast approximate nearest-neighbor and BM25 full-text search, encrypted stores, provable deletion, built on vector-symbolic architectures.
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
// Copyright 2024-2026 WritersLogic Contributors
// SPDX-License-Identifier: Apache-2.0

//! Black-box harness for the `warm-server` integration tests: starts the real binary, talks
//! HTTP/1.1 over TCP, a unix socket or TLS, and reads its structured log.

#![allow(dead_code)]

use std::io::{BufRead, BufReader, Read, Write};
use std::net::{SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::time::Duration;

use serde_json::{json, Value};

pub const DIM: usize = 16;
pub const BIN: &str = env!("CARGO_BIN_EXE_warm-server");

pub enum Endpoint {
    Tcp(SocketAddr),
    #[cfg(unix)]
    Unix(PathBuf),
}

pub struct Server {
    pub child: Child,
    pub endpoint: Endpoint,
    pub data_dir: PathBuf,
    pub stderr_path: PathBuf,
    pub tls_root: Option<Vec<u8>>,
    _scratch: Option<tempfile::TempDir>,
}

pub struct Resp {
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub body: Vec<u8>,
}

impl Resp {
    pub fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(name))
            .map(|(_, v)| v.as_str())
    }

    pub fn json(&self) -> Value {
        serde_json::from_slice(&self.body).unwrap_or(Value::Null)
    }

    pub fn text(&self) -> String {
        String::from_utf8_lossy(&self.body).into_owned()
    }
}

pub struct Request<'a> {
    server: &'a Server,
    method: &'a str,
    path: String,
    headers: Vec<(String, String)>,
    body: Vec<u8>,
}

impl<'a> Request<'a> {
    pub fn header(mut self, name: &str, value: &str) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }

    pub fn bearer(self, token: &str) -> Self {
        let value = format!("Bearer {token}");
        self.header("Authorization", &value)
    }

    pub fn tenant(self, tenant: &str) -> Self {
        self.header("X-Tenant-ID", tenant)
    }

    pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = body.into();
        self
    }

    pub fn json(self, body: &Value) -> Self {
        self.header("Content-Type", "application/json")
            .body(body.to_string().into_bytes())
    }

    pub fn send(self) -> Resp {
        let (server, bytes) = self.encode();
        server.exchange(&bytes)
    }

    /// `None` when the server closed the connection without answering.
    pub fn try_send(self) -> Option<Resp> {
        let (server, bytes) = self.encode();
        let raw = server.exchange_raw(&bytes);
        (!raw.is_empty()).then(|| parse_response(&raw))
    }

    fn encode(self) -> (&'a Server, Vec<u8>) {
        let mut raw = format!(
            "{} {} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nContent-Length: {}\r\n",
            self.method,
            self.path,
            self.body.len()
        );
        for (k, v) in &self.headers {
            raw.push_str(&format!("{k}: {v}\r\n"));
        }
        raw.push_str("\r\n");
        let mut bytes = raw.into_bytes();
        bytes.extend_from_slice(&self.body);
        (self.server, bytes)
    }
}

fn parse_response(raw: &[u8]) -> Resp {
    let split = raw
        .windows(4)
        .position(|w| w == b"\r\n\r\n")
        .unwrap_or_else(|| panic!("no header terminator in {:?}", String::from_utf8_lossy(raw)));
    let head = String::from_utf8_lossy(&raw[..split]).into_owned();
    let mut lines = head.split("\r\n");
    let status = lines
        .next()
        .and_then(|l| l.split(' ').nth(1))
        .and_then(|s| s.parse().ok())
        .unwrap_or_else(|| panic!("bad status line in {head:?}"));
    let headers: Vec<(String, String)> = lines
        .filter_map(|l| l.split_once(':'))
        .map(|(k, v)| (k.trim().to_string(), v.trim().to_string()))
        .collect();
    let mut body = raw[split + 4..].to_vec();
    if headers
        .iter()
        .any(|(k, v)| k.eq_ignore_ascii_case("transfer-encoding") && v.contains("chunked"))
    {
        body = dechunk(&body);
    }
    Resp {
        status,
        headers,
        body,
    }
}

fn dechunk(mut data: &[u8]) -> Vec<u8> {
    let mut out = Vec::new();
    while let Some(eol) = data.windows(2).position(|w| w == b"\r\n") {
        let size = usize::from_str_radix(
            std::str::from_utf8(&data[..eol])
                .unwrap()
                .split(';')
                .next()
                .unwrap()
                .trim(),
            16,
        )
        .unwrap_or(0);
        data = &data[eol + 2..];
        if size == 0 || data.len() < size {
            break;
        }
        out.extend_from_slice(&data[..size]);
        data = data.get(size + 2..).unwrap_or(&[]);
    }
    out
}

pub trait Stream: Read + Write {}
impl<T: Read + Write> Stream for T {}

impl Server {
    pub fn start(extra: &[&str], envs: &[(&str, &str)]) -> Self {
        let scratch = tempfile::tempdir().unwrap();
        let dir = scratch.path().join("data");
        let mut server = Self::start_in(&dir, extra, envs);
        server._scratch = Some(scratch);
        server
    }

    /// Credential-less loopback server (`--no-auth`).
    pub fn open(extra: &[&str]) -> Self {
        let mut args = vec!["--no-auth"];
        args.extend_from_slice(extra);
        Self::start(&args, &[])
    }

    pub fn with_key(key: &str, extra: &[&str]) -> Self {
        Self::start(extra, &[("WARM_API_KEY", key)])
    }

    pub fn start_in(dir: &Path, extra: &[&str], envs: &[(&str, &str)]) -> Self {
        let stderr_path = dir.with_extension("stderr.log");
        std::fs::create_dir_all(dir.parent().unwrap()).unwrap();
        let stderr = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&stderr_path)
            .unwrap();
        let mut cmd = Command::new(BIN);
        cmd.args(["--bind", "127.0.0.1:0", "--input-dim", &DIM.to_string()])
            .args(["--dim", "2048", "--data-dir"])
            .arg(dir)
            .args(extra)
            .env_remove("WARM_API_KEY")
            .stdout(Stdio::piped())
            .stderr(stderr);
        for (k, v) in envs {
            cmd.env(k, v);
        }
        let mut child = cmd.spawn().unwrap();
        let mut line = String::new();
        BufReader::new(child.stdout.as_mut().unwrap())
            .read_line(&mut line)
            .unwrap();
        let target = line.trim().rsplit(' ').next().unwrap_or("").to_string();
        let endpoint = match target.strip_prefix("unix:") {
            #[cfg(unix)]
            Some(path) => Endpoint::Unix(PathBuf::from(path)),
            _ => Endpoint::Tcp(target.parse().unwrap_or_else(|_| {
                panic!(
                    "server did not start: {line:?}\n{}",
                    std::fs::read_to_string(&stderr_path).unwrap_or_default()
                )
            })),
        };
        let tls_root = extra
            .iter()
            .position(|a| *a == "--tls-cert")
            .and_then(|i| std::fs::read(extra[i + 1]).ok());
        Self {
            child,
            endpoint,
            data_dir: dir.to_path_buf(),
            stderr_path,
            tls_root,
            _scratch: None,
        }
    }

    /// Run the binary expecting it to exit with an error; returns its stderr.
    pub fn refuses(extra: &[&str], envs: &[(&str, &str)]) -> String {
        let scratch = tempfile::tempdir().unwrap();
        let mut cmd = Command::new(BIN);
        cmd.args(["--data-dir"])
            .arg(scratch.path())
            .args(extra)
            .env_remove("WARM_API_KEY");
        for (k, v) in envs {
            cmd.env(k, v);
        }
        let out = cmd.output().unwrap();
        assert!(
            !out.status.success(),
            "server unexpectedly started with {extra:?}"
        );
        String::from_utf8_lossy(&out.stderr).into_owned()
    }

    pub fn connect(&self) -> Box<dyn Stream> {
        let timeout = Some(Duration::from_secs(20));
        match &self.endpoint {
            Endpoint::Tcp(addr) => {
                let tcp = TcpStream::connect(addr).unwrap();
                tcp.set_read_timeout(timeout).unwrap();
                match &self.tls_root {
                    None => Box::new(tcp),
                    Some(pem) => Box::new(tls_stream(tcp, pem)),
                }
            }
            #[cfg(unix)]
            Endpoint::Unix(path) => {
                let s = std::os::unix::net::UnixStream::connect(path).unwrap();
                s.set_read_timeout(timeout).unwrap();
                Box::new(s)
            }
        }
    }

    /// The bytes the server sent back, empty when the connection or handshake failed.
    pub fn exchange_raw(&self, bytes: &[u8]) -> Vec<u8> {
        let mut stream = self.connect();
        // The server may answer and close before reading an oversized body.
        let _ = stream.write_all(bytes);
        let mut raw = Vec::new();
        let _ = stream.read_to_end(&mut raw);
        raw
    }

    pub fn exchange(&self, bytes: &[u8]) -> Resp {
        parse_response(&self.exchange_raw(bytes))
    }

    pub fn req<'a>(&'a self, method: &'a str, path: &str) -> Request<'a> {
        Request {
            server: self,
            method,
            path: path.to_string(),
            headers: Vec::new(),
            body: Vec::new(),
        }
    }

    pub fn addr(&self) -> SocketAddr {
        match &self.endpoint {
            Endpoint::Tcp(a) => *a,
            #[cfg(unix)]
            Endpoint::Unix(_) => panic!("unix endpoint"),
        }
    }

    pub fn pid(&self) -> u32 {
        self.child.id()
    }

    pub fn signal(&self, name: &str) {
        let status = Command::new("kill")
            .args([&format!("-{name}"), &self.child.id().to_string()])
            .status()
            .unwrap();
        assert!(status.success());
    }

    /// SIGTERM and wait; true when the process exited cleanly.
    pub fn stop(&mut self) -> bool {
        self.signal("TERM");
        self.child.wait().unwrap().success()
    }

    pub fn log_lines(&self) -> Vec<String> {
        std::fs::read_to_string(&self.stderr_path)
            .unwrap_or_default()
            .lines()
            .map(str::to_string)
            .collect()
    }

    pub fn log_json(&self) -> Vec<Value> {
        self.log_lines()
            .iter()
            .map(|l| serde_json::from_str(l).unwrap_or_else(|_| panic!("non-JSON log line {l:?}")))
            .collect()
    }
}

impl Drop for Server {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
        #[cfg(unix)]
        if let Endpoint::Unix(path) = &self.endpoint {
            let _ = std::fs::remove_file(path);
        }
    }
}

fn tls_stream(tcp: TcpStream, root_pem: &[u8]) -> impl Read + Write {
    use rustls::pki_types::pem::PemObject;
    use rustls::pki_types::{CertificateDer, ServerName};
    let mut roots = rustls::RootCertStore::empty();
    for cert in CertificateDer::pem_slice_iter(root_pem) {
        roots.add(cert.unwrap()).unwrap();
    }
    let config = rustls::ClientConfig::builder_with_provider(Arc::new(
        rustls::crypto::ring::default_provider(),
    ))
    .with_safe_default_protocol_versions()
    .unwrap()
    .with_root_certificates(roots)
    .with_no_client_auth();
    let conn =
        rustls::ClientConnection::new(Arc::new(config), ServerName::try_from("localhost").unwrap())
            .unwrap();
    rustls::StreamOwned::new(conn, tcp)
}

/// Deterministic pseudo-random embedding for `seed`.
pub fn embedding(seed: u64) -> Vec<f64> {
    let mut state = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ 0xD1B5_4A32_D192_ED03;
    (0..DIM)
        .map(|_| {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            (state % 2000) as f64 / 1000.0 - 1.0
        })
        .collect()
}

pub fn doc(id: &str, seed: u64) -> Value {
    json!({"id": id, "text": format!("document {id}"), "vector": embedding(seed), "metadata": {"seed": seed}})
}

/// Create a key with the binary's own `keygen` and return its token.
pub fn keygen(file: &Path, id: &str, tenants: &str, scopes: &str) -> String {
    let out = Command::new(BIN)
        .args([
            "keygen",
            "--id",
            id,
            "--tenants",
            tenants,
            "--scopes",
            scopes,
            "--keys-file",
        ])
        .arg(file)
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8(out.stdout).unwrap().trim().to_string()
}