airust 0.6.5

Rust-native knowledge and memory engine for AI agents and local LLM applications: lexical search, optional semantic retrieval, versioned memory, source-bound evidence and MCP
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
#![doc = include_str!("../../../docs/LOCAL-MODEL-PROCESSES.md")]

mod pool;
mod protocol;
#[cfg(all(test, unix, not(miri)))]
mod tests;
mod transport;

use super::{local::FastEmbedReranker, RerankError, Reranker};
pub use pool::{PooledReranker, ProcessPool};
use protocol::{Init, Reply, Request};
use std::{
    io::BufReader,
    path::{Path, PathBuf},
    time::Duration,
};

/// Reserved startup role understood by the bundled worker and reference pilot.
pub const WORKER_ARGUMENT: &str = "--airust-reranker-worker";

/// Application-owned executable and per-native-call time bounds. The trusted
/// worker must implement this package's protocol and must not spawn descendants
/// retaining its stdio pipes. This is resource isolation, not a security sandbox.
#[derive(Debug, Clone)]
pub struct ProcessOptions {
    executable: PathBuf,
    startup_timeout: Duration,
    request_timeout: Duration,
    runtime: Option<(u64, String)>,
}

impl ProcessOptions {
    pub fn new(executable: impl Into<PathBuf>) -> Self {
        Self {
            executable: executable.into(),
            startup_timeout: Duration::from_secs(60),
            request_timeout: Duration::from_secs(30),
            runtime: None,
        }
    }

    /// Require the ONNX Runtime library to have exactly this size and lowercase
    /// SHA-256. The verified bytes are copied into the private snapshot
    /// directory and the worker loads that copy, never the caller's path.
    pub fn with_runtime_identity(mut self, bytes: u64, sha256: &str) -> Result<Self, RerankError> {
        if bytes == 0 || bytes > 1 << 30 || !crate::text::Identifier::sha256(sha256) {
            return Err(RerankError::Configuration(
                "runtime identity needs 1 byte to 1 GiB and a lowercase SHA-256",
            ));
        }
        self.runtime = Some((bytes, sha256.to_owned()));
        Ok(self)
    }

    pub fn with_timeouts(
        mut self,
        startup: Duration,
        request: Duration,
    ) -> Result<Self, RerankError> {
        if [startup, request]
            .iter()
            .any(|t| t.is_zero() || *t > Duration::from_secs(120))
        {
            return Err(RerankError::Configuration(
                "worker timeouts must be positive and at most 120 seconds",
            ));
        }
        self.startup_timeout = startup;
        self.request_timeout = request;
        Ok(self)
    }
}

/// One private native-model process and one bounded I/O thread. Dropping the
/// scorer closes its input, waits for normal exit, then kills/reaps if necessary.
/// Parent-owned snapshot directories survive until the worker has stopped.
pub struct ProcessReranker {
    transport: transport::Transport,
    _runtime: Option<tempfile::NamedTempFile>,
    _snapshot: tempfile::TempDir,
    recipe: String,
    next_id: u64,
    timeout: Duration,
}

impl ProcessReranker {
    pub fn multilingual_minilm_v2_from_snapshot(
        options: ProcessOptions,
        model_directory: impl AsRef<Path>,
        runtime_library: impl AsRef<Path>,
        threads: usize,
        snapshot_directory: impl AsRef<Path>,
    ) -> Result<Self, RerankError> {
        Self::load(
            options,
            model_directory.as_ref(),
            runtime_library.as_ref(),
            threads,
            snapshot_directory.as_ref(),
        )
    }

    fn load(
        options: ProcessOptions,
        model_directory: &Path,
        runtime_library: &Path,
        threads: usize,
        snapshots: &Path,
    ) -> Result<Self, RerankError> {
        if threads == 0 || threads > 64 {
            return Err(RerankError::Configuration(
                "worker thread count must be 1..64",
            ));
        }
        for path in [
            model_directory,
            runtime_library,
            options.executable.as_path(),
        ] {
            if path.as_os_str().len() > 32 * 1024 {
                return Err(RerankError::Configuration("worker path exceeds 32 KiB"));
            }
        }
        let model_directory = model_directory.canonicalize().map_err(provider)?;
        let runtime_library = runtime_library.canonicalize().map_err(provider)?;
        let snapshot_parent = snapshots.canonicalize().map_err(provider)?;
        let mut builder = tempfile::Builder::new();
        builder.prefix("airust-model-");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            builder.permissions(std::fs::Permissions::from_mode(0o700));
        }
        let snapshot = builder.tempdir_in(snapshot_parent).map_err(provider)?;
        let runtime_copy = match &options.runtime {
            Some((bytes, sha256)) => {
                let suffix = runtime_library
                    .extension()
                    .and_then(|extension| extension.to_str())
                    .map(|extension| format!(".{extension}"))
                    .unwrap_or_default();
                let expected = crate::model_assets::Asset {
                    bytes: *bytes,
                    sha256: sha256.clone(),
                };
                Some(
                    crate::model_assets::snapshot(
                        &runtime_library,
                        &expected,
                        snapshot.path(),
                        &suffix,
                        |_| {},
                    )
                    .map_err(provider)?,
                )
            }
            None => None,
        };
        let runtime_library = runtime_copy
            .as_ref()
            .map_or(runtime_library, |copy| copy.path().to_path_buf());
        let init = Init {
            version: crate::VERSION.into(),
            model_directory,
            runtime_library,
            threads,
            snapshot_directory: snapshot.path().to_path_buf(),
        };
        let frame = serde_json::to_vec(&init).map_err(|_| {
            RerankError::Configuration("worker configuration must have UTF-8 paths")
        })?;
        if frame.len() >= protocol::REQUEST_BYTES {
            return Err(RerankError::Budget("worker configuration"));
        }
        let mut transport = transport::Transport::spawn(&options, frame)?;
        let reply = transport.receive(options.startup_timeout)?;
        let reply = serde_json::from_slice::<Reply>(&reply)
            .map_err(|_| RerankError::Output("invalid worker startup reply"))?;
        let recipe = match reply {
            Reply::Ready {
                version,
                pid,
                recipe,
            } if version == crate::VERSION
                && pid == transport.id()
                && !recipe.is_empty()
                && recipe.len() <= 1024 =>
            {
                recipe
            }
            Reply::Failure {
                id: 0,
                output_error,
                message,
            } => return Err(remote_error(output_error, message)),
            _ => return Err(RerankError::Output("worker startup identity mismatch")),
        };
        Ok(Self {
            transport,
            _runtime: runtime_copy,
            _snapshot: snapshot,
            recipe,
            next_id: 1,
            timeout: options.request_timeout,
        })
    }

    pub fn worker_id(&self) -> u32 {
        self.transport.id()
    }
}

impl Reranker for ProcessReranker {
    fn recipe(&self) -> &str {
        &self.recipe
    }

    fn score(&mut self, query: &str, documents: &[&str]) -> Result<Vec<f64>, RerankError> {
        validate_text(query, documents)?;
        if documents.is_empty() {
            return Ok(Vec::new());
        }
        let id = self.next_id;
        let next_id = id
            .checked_add(1)
            .ok_or(RerankError::Configuration("worker request ID exhausted"))?;
        // Validate bytes before cloning/serialization; JSON escaping has a
        // bounded sixfold expansion inside the separate wire-frame limit.
        let request = Request {
            id,
            query: query.into(),
            documents: documents.iter().map(|s| (*s).into()).collect(),
        };
        let frame = serde_json::to_vec(&request)
            .map_err(|_| RerankError::Output("worker request serialization failed"))?;
        if frame.len() >= protocol::REQUEST_BYTES {
            return Err(RerankError::Budget("worker frame"));
        }
        self.next_id = next_id;
        let frame = self.transport.request(frame, self.timeout)?;
        let reply = serde_json::from_slice::<Reply>(&frame)
            .map_err(|_| RerankError::Output("invalid worker score reply"));
        let result = match reply {
            Ok(Reply::Scores {
                id: returned,
                scores,
            }) if returned == id => {
                let scores: Vec<f64> = scores.into_iter().map(f64::from_bits).collect();
                if scores.len() != documents.len() {
                    Err(RerankError::ScoreCount {
                        expected: documents.len(),
                        actual: scores.len(),
                    })
                } else if let Some(i) = scores.iter().position(|s| !s.is_finite()) {
                    Err(RerankError::NonFiniteScore(i + 1))
                } else {
                    Ok(scores)
                }
            }
            Ok(Reply::Failure {
                id: returned,
                output_error,
                message,
            }) if returned == id => Err(remote_error(output_error, message)),
            _ => Err(RerankError::Output(
                "worker response identity or shape mismatch",
            )),
        };
        if result.is_err() {
            self.transport.abort();
        }
        result
    }
}

fn validate_text(query: &str, documents: &[&str]) -> Result<(), RerankError> {
    if query.len() > 16 * 1024
        || documents.len() > protocol::DOCUMENTS
        || documents.iter().any(|d| d.len() > 64 * 1024)
    {
        return Err(RerankError::Budget("worker input"));
    }
    let total = documents
        .iter()
        .try_fold(query.len(), |sum, d| sum.checked_add(d.len()))
        .ok_or(RerankError::Budget("worker input"))?;
    if total > protocol::TEXT_BYTES {
        return Err(RerankError::Budget("worker input"));
    }
    if query.trim().is_empty() {
        return Err(RerankError::EmptyQuery);
    }
    if documents.iter().any(|d| d.trim().is_empty()) {
        return Err(RerankError::Output("empty document passed to local scorer"));
    }
    Ok(())
}

fn remote_error(output: bool, message: String) -> RerankError {
    if output {
        RerankError::Output("malformed native worker output")
    } else {
        RerankError::Provider(protocol::bounded_message(message))
    }
}

fn provider(error: std::io::Error) -> RerankError {
    RerankError::Provider(protocol::bounded_message(error))
}

/// Run the trusted worker role on stdin/stdout. The application must dispatch
/// `WORKER_ARGUMENT` before starting a server or loading any other model.
pub fn run_worker() -> Result<(), RerankError> {
    let mut input = BufReader::new(std::io::stdin().lock());
    let mut output = std::io::stdout().lock();
    let frame = protocol::read_frame(&mut input, protocol::REQUEST_BYTES)
        .map_err(provider)?
        .ok_or(RerankError::Output("worker initialization missing"))?;
    let init: Init = serde_json::from_slice(&frame)
        .map_err(|_| RerankError::Configuration("invalid worker initialization"))?;
    if init.version != crate::VERSION || init.threads == 0 || init.threads > 64 {
        return Err(RerankError::Configuration(
            "worker initialization version or threads mismatch",
        ));
    }
    let model = FastEmbedReranker::multilingual_minilm_v2_from_snapshot(
        init.model_directory,
        init.runtime_library,
        init.threads,
        init.snapshot_directory,
    );
    let mut model = match model {
        Ok(model) => model,
        Err(error) => {
            reply(&mut output, &failure(0, error))?;
            return Ok(());
        }
    };
    reply(
        &mut output,
        &Reply::Ready {
            version: crate::VERSION.into(),
            pid: std::process::id(),
            recipe: model.recipe().into(),
        },
    )?;
    let mut expected = 1u64;
    while let Some(frame) =
        protocol::read_frame(&mut input, protocol::REQUEST_BYTES).map_err(provider)?
    {
        let request: Request = serde_json::from_slice(&frame)
            .map_err(|_| RerankError::Output("invalid worker request"))?;
        if request.id != expected {
            return Err(RerankError::Output("worker request sequence mismatch"));
        }
        expected = expected
            .checked_add(1)
            .ok_or(RerankError::Output("worker request sequence exhausted"))?;
        let documents: Vec<_> = request.documents.iter().map(String::as_str).collect();
        let result = validate_text(&request.query, &documents)
            .and_then(|()| model.score(&request.query, &documents));
        let response = match result {
            Ok(scores) => Reply::Scores {
                id: request.id,
                scores: scores.into_iter().map(f64::to_bits).collect(),
            },
            Err(e) => failure(request.id, e),
        };
        reply(&mut output, &response)?;
    }
    Ok(())
}

fn failure(id: u64, error: RerankError) -> Reply {
    Reply::Failure {
        id,
        output_error: !matches!(error, RerankError::Provider(_)),
        message: protocol::bounded_message(error),
    }
}

fn reply(writer: &mut impl std::io::Write, value: &Reply) -> Result<(), RerankError> {
    let frame = serde_json::to_vec(value)
        .map_err(|_| RerankError::Output("worker output serialization failed"))?;
    if frame.len() >= protocol::RESPONSE_BYTES {
        return Err(RerankError::Budget("worker response"));
    }
    protocol::write_frame(writer, &frame).map_err(provider)
}

#[cfg(test)]
mod validation_tests {
    use super::*;

    #[test]
    fn input_limits_reject_expansion_before_worker_work() {
        assert!(validate_text("query", &[]).is_ok());
        assert_eq!(validate_text("  ", &["text"]), Err(RerankError::EmptyQuery));
        assert!(matches!(
            validate_text("query", &[" "]),
            Err(RerankError::Output(_))
        ));
        let query = "é".repeat(8192);
        assert!(validate_text(&query, &["text"]).is_ok());
        assert!(matches!(
            validate_text(&(query + "é"), &["text"]),
            Err(RerankError::Budget(_))
        ));
        assert!(validate_text("query", &["text"; 128]).is_ok());
        assert!(matches!(
            validate_text("query", &["text"; 129]),
            Err(RerankError::Budget(_))
        ));
        let document = "x".repeat(64 * 1024);
        assert!(validate_text("query", &[document.as_str(); 31]).is_ok());
        assert!(matches!(
            validate_text("query", &[document.as_str(); 32]),
            Err(RerankError::Budget(_))
        ));
        assert!(matches!(
            validate_text("query", &[&(document + "x")]),
            Err(RerankError::Budget(_))
        ));
    }

    #[test]
    fn timeout_bounds_are_checked_without_launching_processes() {
        for timeout in [Duration::ZERO, Duration::from_secs(121)] {
            assert!(ProcessOptions::new("unused")
                .with_timeouts(timeout, Duration::from_secs(1))
                .is_err());
            assert!(ProcessOptions::new("unused")
                .with_timeouts(Duration::from_secs(1), timeout)
                .is_err());
        }
        assert!(ProcessOptions::new("unused")
            .with_timeouts(Duration::from_nanos(1), Duration::from_secs(120))
            .is_ok());
    }
}