cortext 1.3.1

Rust bindings for the Cortext multimodal memory engine
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
use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::Duration;

use flate2::read::GzDecoder;
use sha2::{Digest, Sha256};
use tar::Archive;

use crate::error::Error;
use crate::library::{library_name_for_tag, platform_tag};

/// Default [augmem/cortext.cpp](https://github.com/augmem/cortext.cpp) release
/// that provides shared natives and model shards.
pub const ASSETS_VERSION: &str = "1.3.0";

const DEFAULT_ASSETS_REPO: &str = "augmem/cortext.cpp";
const ASSETS_MARKER_NAME: &str = ".cortext-assets-ok";
const ASSETS_HTTP_TIMEOUT: Duration = Duration::from_secs(600);
const CHECKSUM_HTTP_TIMEOUT: Duration = Duration::from_secs(60);

static ASSETS: Mutex<Option<PathBuf>> = Mutex::new(None);

fn user_agent() -> String {
    format!("cortext.rs/{}", env!("CARGO_PKG_VERSION"))
}

/// Last [`ensure_assets`] root, or empty if not yet loaded.
pub fn assets_dir() -> Option<PathBuf> {
    ASSETS.lock().ok().and_then(|guard| guard.clone())
}

/// True when a host native + model manifest are already available locally
/// (explicit library path, `CORTEXT_ASSETS_DIR`, or a previously downloaded cache).
pub fn package_assets_ready() -> bool {
    if let Ok(path) = std::env::var("CORTEXT_LIBRARY_PATH") {
        if Path::new(&path).is_file() {
            return true;
        }
    }
    if let Ok(dir) = std::env::var("CORTEXT_ASSETS_DIR") {
        let dir = dir.trim();
        if !dir.is_empty() && validate_assets_root(Path::new(dir)).is_ok() {
            return true;
        }
    }
    if let Ok(root) = default_assets_cache_root(&assets_version()) {
        return validate_assets_root(&root).is_ok();
    }
    false
}

/// Effective assets version: `CORTEXT_ASSETS_VERSION` or [`ASSETS_VERSION`].
pub fn assets_version() -> String {
    std::env::var("CORTEXT_ASSETS_VERSION")
        .ok()
        .map(|v| v.trim().trim_start_matches('v').to_string())
        .filter(|v| !v.is_empty())
        .unwrap_or_else(|| ASSETS_VERSION.to_string())
}

/// Download (if needed) and unpack the shared Cortext release tarball.
///
/// Resolution order:
/// 1. `CORTEXT_ASSETS_DIR` (pre-unpacked tree)
/// 2. User/cache dir for [`assets_version`] (download once, reuse)
pub fn ensure_assets() -> Result<PathBuf, Error> {
    let mut guard = ASSETS
        .lock()
        .map_err(|_| Error::new("assets lock poisoned"))?;
    if let Some(root) = guard.as_ref() {
        if validate_assets_root(root).is_ok() {
            return Ok(root.clone());
        }
        *guard = None;
    }
    let root = ensure_assets_locked()?;
    *guard = Some(root.clone());
    Ok(root)
}

#[cfg(test)]
pub(crate) fn reset_assets_for_test() {
    if let Ok(mut guard) = ASSETS.lock() {
        *guard = None;
    }
}

fn ensure_assets_locked() -> Result<PathBuf, Error> {
    if let Ok(dir) = std::env::var("CORTEXT_ASSETS_DIR") {
        let dir = dir.trim();
        if !dir.is_empty() {
            let abs = std::fs::canonicalize(dir).or_else(|_| {
                let p = PathBuf::from(dir);
                if p.is_absolute() {
                    Ok(p)
                } else {
                    std::env::current_dir().map(|cwd| cwd.join(dir))
                }
            })?;
            validate_assets_root(&abs)
                .map_err(|e| Error::new(format!("CORTEXT_ASSETS_DIR: {e}")))?;
            return Ok(abs);
        }
    }

    let version = assets_version();
    let root = default_assets_cache_root(&version)?;
    let marker = root.join(ASSETS_MARKER_NAME);
    if marker.is_file() && validate_assets_root(&root).is_ok() {
        return Ok(root);
    }
    if root.exists() {
        let _ = fs::remove_dir_all(&root);
    }
    fs::create_dir_all(&root)?;

    let mut url = assets_archive_url(&version);
    if let Ok(override_url) = std::env::var("CORTEXT_ASSETS_URL") {
        let override_url = override_url.trim();
        if !override_url.is_empty() {
            url = override_url.to_string();
        }
    }

    let want_sha = resolve_archive_sha256(&version, &url)?;
    let tmp_root = tempfile_dir(root.parent().unwrap_or(Path::new(".")))?;
    let archive_path = tmp_root.join(format!("cortext-assets-{version}.tar.gz"));
    if let Err(err) = download_file(&url, &archive_path) {
        let _ = fs::remove_dir_all(&tmp_root);
        return Err(Error::new(format!("download cortext assets {url}: {err}")));
    }
    let got = file_sha256(&archive_path)?;
    if !want_sha.is_empty() && !got.eq_ignore_ascii_case(&want_sha) {
        let _ = fs::remove_dir_all(&tmp_root);
        return Err(Error::new(format!(
            "cortext assets checksum mismatch: got {got} want {want_sha}"
        )));
    }

    let extract_root = tmp_root.join("extract");
    fs::create_dir_all(&extract_root)?;
    if let Err(err) = extract_tar_gz(&archive_path, &extract_root) {
        let _ = fs::remove_dir_all(&tmp_root);
        return Err(Error::new(format!("extract cortext assets: {err}")));
    }
    if let Err(err) = validate_assets_root(&extract_root) {
        let _ = fs::remove_dir_all(&tmp_root);
        return Err(err);
    }

    let _ = fs::remove_dir_all(&root);
    if let Err(err) = fs::rename(&extract_root, &root) {
        if let Err(copy_err) = copy_dir(&extract_root, &root) {
            let _ = fs::remove_dir_all(&tmp_root);
            return Err(Error::new(format!(
                "install assets cache: {err} / {copy_err}"
            )));
        }
    }
    let _ = fs::remove_dir_all(&tmp_root);
    fs::write(root.join(ASSETS_MARKER_NAME), format!("{version}\n"))?;
    Ok(root)
}

fn assets_archive_url(version: &str) -> String {
    format!(
        "https://github.com/{DEFAULT_ASSETS_REPO}/releases/download/v{version}/cortext-assets-{version}.tar.gz"
    )
}

fn assets_checksum_url(version: &str) -> String {
    format!("{}.sha256", assets_archive_url(version))
}

fn resolve_archive_sha256(version: &str, archive_url: &str) -> Result<String, Error> {
    if let Ok(v) = std::env::var("CORTEXT_ASSETS_SHA256") {
        let v = v.trim().to_ascii_lowercase();
        if !v.is_empty() {
            return Ok(v);
        }
    }
    let mut sum_url = format!("{archive_url}.sha256");
    if archive_url == assets_archive_url(version) {
        sum_url = assets_checksum_url(version);
    }
    let body = http_get_bytes(&sum_url)
        .map_err(|e| Error::new(format!("fetch assets checksum {sum_url}: {e}")))?;
    parse_sha256_sidecar(&body, &sum_url)
}

pub(crate) fn parse_sha256_sidecar(body: &[u8], source: &str) -> Result<String, Error> {
    let line = String::from_utf8_lossy(body);
    let line = line.trim();
    if line.is_empty() {
        return Err(Error::new(format!("empty assets checksum from {source}")));
    }
    let sum = line
        .split_whitespace()
        .next()
        .unwrap_or("")
        .to_ascii_lowercase();
    if sum.len() != 64 {
        return Err(Error::new(format!(
            "invalid assets sha256 length from {source}: {sum:?}"
        )));
    }
    Ok(sum)
}

fn default_assets_cache_root(version: &str) -> Result<PathBuf, Error> {
    if let Ok(dir) = std::env::var("CORTEXT_ASSET_CACHE_DIR") {
        let dir = dir.trim();
        if !dir.is_empty() {
            return Ok(Path::new(dir).join(version));
        }
    }
    Ok(default_user_cache_root()?.join("assets").join(version))
}

pub(crate) fn default_user_cache_root() -> Result<PathBuf, Error> {
    let home = dirs_home()?;
    let root = if cfg!(target_os = "macos") {
        home.join("Library/Caches/augmem/cortext")
    } else if cfg!(target_os = "windows") {
        let base = std::env::var("LOCALAPPDATA")
            .ok()
            .filter(|s| !s.is_empty())
            .map(PathBuf::from)
            .unwrap_or_else(|| home.join("AppData/Local"));
        base.join("augmem").join("cortext")
    } else {
        let base = std::env::var("XDG_CACHE_HOME")
            .ok()
            .filter(|s| !s.is_empty())
            .map(PathBuf::from)
            .unwrap_or_else(|| home.join(".cache"));
        base.join("augmem").join("cortext")
    };
    Ok(root)
}

fn dirs_home() -> Result<PathBuf, Error> {
    std::env::var("HOME")
        .ok()
        .filter(|s| !s.is_empty())
        .or_else(|| std::env::var("USERPROFILE").ok().filter(|s| !s.is_empty()))
        .map(PathBuf::from)
        .ok_or_else(|| Error::new("could not resolve home directory"))
}

pub(crate) fn validate_assets_root(root: &Path) -> Result<(), Error> {
    let native_manifest = root.join("native").join("manifest.json");
    if !native_manifest.is_file() {
        return Err(Error::new(format!(
            "missing native/manifest.json under {}",
            root.display()
        )));
    }
    let tag = platform_tag();
    let lib = native_library_from_assets(root);
    if !lib.is_file() {
        return Err(Error::new(format!(
            "missing native library for {tag} at {}",
            lib.display()
        )));
    }
    let model_manifest = root.join("models").join("manifest.json");
    if !model_manifest.is_file() {
        return Err(Error::new(format!(
            "missing models/manifest.json under {}",
            root.display()
        )));
    }
    Ok(())
}

pub(crate) fn native_library_from_assets(root: &Path) -> PathBuf {
    let tag = platform_tag();
    root.join("native")
        .join(&tag)
        .join(library_name_for_tag(&tag))
}

pub(crate) fn models_root_from_assets(root: &Path) -> PathBuf {
    root.join("models")
}

fn download_file(url: &str, dest: &Path) -> Result<(), Error> {
    if let Some(parent) = dest.parent() {
        fs::create_dir_all(parent)?;
    }
    let tmp = dest.with_extension("partial");
    let _ = fs::remove_file(&tmp);
    let resp = ureq::get(url)
        .set("User-Agent", &user_agent())
        .timeout(ASSETS_HTTP_TIMEOUT)
        .call()?;
    if resp.status() != 200 {
        return Err(Error::new(format!("HTTP {}", resp.status())));
    }
    let mut reader = resp.into_reader();
    let mut file = File::create(&tmp)?;
    io::copy(&mut reader, &mut file)?;
    file.flush()?;
    fs::rename(&tmp, dest)?;
    Ok(())
}

fn http_get_bytes(url: &str) -> Result<Vec<u8>, Error> {
    let resp = ureq::get(url)
        .set("User-Agent", &user_agent())
        .timeout(CHECKSUM_HTTP_TIMEOUT)
        .call()?;
    if resp.status() != 200 {
        return Err(Error::new(format!("HTTP {}", resp.status())));
    }
    let mut buf = Vec::new();
    resp.into_reader().take(1 << 20).read_to_end(&mut buf)?;
    Ok(buf)
}

fn file_sha256(path: &Path) -> Result<String, Error> {
    let mut file = File::open(path)?;
    let mut hasher = Sha256::new();
    io::copy(&mut file, &mut hasher)?;
    Ok(format!("{:x}", hasher.finalize()))
}

fn extract_tar_gz(archive: &Path, dest: &Path) -> Result<(), Error> {
    let file = File::open(archive)?;
    let gz = GzDecoder::new(file);
    let mut archive = Archive::new(gz);
    let dest_abs = fs::canonicalize(dest).unwrap_or_else(|_| dest.to_path_buf());
    for entry in archive.entries()? {
        let mut entry = entry?;
        let path = entry.path()?.into_owned();
        let name = path.as_os_str();
        if name.is_empty() {
            continue;
        }
        let target = dest_abs.join(&path);
        if !target.starts_with(&dest_abs) {
            return Err(Error::new(format!(
                "tar entry escapes destination: {}",
                path.display()
            )));
        }
        let header = entry.header().clone();
        if header.entry_type().is_dir() {
            fs::create_dir_all(&target)?;
            continue;
        }
        if !header.entry_type().is_file() {
            continue;
        }
        if let Some(parent) = target.parent() {
            fs::create_dir_all(parent)?;
        }
        let mut out = File::create(&target)?;
        io::copy(&mut entry, &mut out)?;
    }
    Ok(())
}

fn tempfile_dir(parent: &Path) -> Result<PathBuf, Error> {
    fs::create_dir_all(parent)?;
    for i in 0..100 {
        let candidate = parent.join(format!(".cortext-assets-{i}-{}", std::process::id()));
        if fs::create_dir(&candidate).is_ok() {
            return Ok(candidate);
        }
    }
    Err(Error::new("could not create temporary assets directory"))
}

fn copy_dir(src: &Path, dst: &Path) -> io::Result<()> {
    fs::create_dir_all(dst)?;
    for entry in walkdir(src)? {
        let rel = entry.strip_prefix(src).unwrap_or(&entry);
        let target = dst.join(rel);
        if entry.is_dir() {
            fs::create_dir_all(&target)?;
        } else if entry.is_file() {
            if let Some(parent) = target.parent() {
                fs::create_dir_all(parent)?;
            }
            fs::copy(&entry, &target)?;
        }
    }
    Ok(())
}

fn walkdir(root: &Path) -> io::Result<Vec<PathBuf>> {
    let mut out = Vec::new();
    fn rec(path: &Path, out: &mut Vec<PathBuf>) -> io::Result<()> {
        out.push(path.to_path_buf());
        if path.is_dir() {
            for entry in fs::read_dir(path)? {
                rec(&entry?.path(), out)?;
            }
        }
        Ok(())
    }
    rec(root, &mut out)?;
    Ok(out)
}

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

    #[test]
    fn parse_sha256_sidecar_accepts_gnu_and_bare() {
        let hex = "a".repeat(64);
        assert_eq!(
            parse_sha256_sidecar(
                format!("{hex}  cortext-assets-1.3.0.tar.gz\n").as_bytes(),
                "t"
            )
            .unwrap(),
            hex
        );
        assert_eq!(parse_sha256_sidecar(hex.as_bytes(), "t").unwrap(), hex);
    }

    #[test]
    fn parse_sha256_sidecar_rejects_short() {
        assert!(parse_sha256_sidecar(b"deadbeef", "t").is_err());
    }

    #[test]
    fn assets_version_default() {
        assert_eq!(ASSETS_VERSION, "1.3.0");
    }
}