holographic-memory 0.6.4

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
// Copyright 2024-2026 WritersLogic Contributors
// SPDX-License-Identifier: Apache-2.0

//! Operator endpoints: snapshots and restore, compaction, key reload, tenant listing.
//! All need the `admin` scope; the key and tenant-listing endpoints also need an all-tenant key.

use std::io::Write;
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use axum::body::Body;
use axum::extract::{Path as UrlPath, Request, State};
use axum::http::{header, HeaderValue, StatusCode};
use axum::response::Response;
use bytes::Bytes;
use http_body_util::BodyExt;
use tokio::io::AsyncWriteExt;

use crate::auth::Scope;
use crate::error::{ApiError, ApiResult};
use crate::request::*;
use crate::tenants::{copy_store, tenant_from_hex, AppState};
use crate::types::*;

pub async fn create_snapshot(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Admin)?;
    let permit = acquire(&state).await?;
    let info = blocking(permit, move || state.create_snapshot(&tenant)).await?;
    Ok(json_response(
        StatusCode::CREATED,
        &body(&Snapshot::from(&info)),
    ))
}

pub async fn list_snapshots(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Admin)?;
    let permit = acquire(&state).await?;
    let list = blocking(permit, move || state.list_snapshots(&tenant)).await?;
    let snapshots = list.iter().map(Snapshot::from).collect();
    Ok(json_response(
        StatusCode::OK,
        &body(&Snapshots { snapshots }),
    ))
}

pub async fn delete_snapshot(
    State(state): State<Arc<AppState>>,
    UrlPath(id): UrlPath<String>,
    req: Request,
) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Admin)?;
    let permit = acquire(&state).await?;
    blocking(permit, move || state.delete_snapshot(&tenant, &id)).await?;
    Ok(json_response(
        StatusCode::OK,
        &body(&Deleted { deleted: true }),
    ))
}

struct ChannelWriter(tokio::sync::mpsc::Sender<std::io::Result<Bytes>>);

impl Write for ChannelWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0
            .blocking_send(Ok(Bytes::copy_from_slice(buf)))
            .map_err(|_| std::io::Error::new(std::io::ErrorKind::BrokenPipe, "client went away"))?;
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// `GET /api/v1/snapshots/{id}/archive`: the snapshot as an uncompressed tar stream, the
/// off-box backup format accepted by `PUT /api/v1/restore`.
pub async fn download_snapshot(
    State(state): State<Arc<AppState>>,
    UrlPath(id): UrlPath<String>,
    req: Request,
) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Admin)?;
    let permit = acquire(&state).await?;
    let dir = state.snapshot_path(&tenant, &id)?;
    let (tx, rx) = tokio::sync::mpsc::channel(8);
    tokio::task::spawn_blocking(move || {
        let _permit = permit;
        let result = (|| -> std::io::Result<()> {
            let mut builder = tar::Builder::new(std::io::BufWriter::with_capacity(
                256 * 1024,
                ChannelWriter(tx.clone()),
            ));
            builder.mode(tar::HeaderMode::Deterministic);
            builder.follow_symlinks(false);
            builder.append_dir_all(".", &dir)?;
            builder.into_inner()?.flush()
        })();
        if let Err(error) = result {
            // The stream is already underway; make the client see a failed transfer.
            let _ = tx.blocking_send(Err(error));
        }
    });
    let stream = futures_util::stream::unfold(rx, |mut rx| async move {
        rx.recv().await.map(|chunk| (chunk, rx))
    });
    let mut response = Response::new(Body::from_stream(stream));
    let headers = response.headers_mut();
    headers.insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static("application/x-tar"),
    );
    if let Ok(v) = HeaderValue::from_str(&format!("attachment; filename=\"{id}.tar\"")) {
        headers.insert(header::CONTENT_DISPOSITION, v);
    }
    Ok(response)
}

fn verify(dir: &Path) -> Result<(), ApiError> {
    holographic_memory::core::admin::verify_store(dir, None)
        .map(|_| ())
        .map_err(|e| {
            crate::log_event!(Warn, "restore_verification_failed", "detail" => format!("{e:#}"));
            ApiError::bad(
                "the snapshot is not a valid, intact store; the current store is unchanged",
            )
        })
}

/// Make a fresh staging directory next to the data, removed on drop unless taken.
struct Staging(Option<PathBuf>);

impl Staging {
    fn new(state: &AppState) -> Result<Self, ApiError> {
        let root = state.staging_root();
        std::fs::create_dir_all(&root).map_err(ApiError::internal)?;
        let dir = root.join(format!("{:016x}", rand::random::<u64>()));
        Ok(Self(Some(dir)))
    }

    fn path(&self) -> &Path {
        self.0.as_deref().expect("not taken")
    }

    fn take(mut self) -> PathBuf {
        self.0.take().expect("not taken")
    }
}

impl Drop for Staging {
    fn drop(&mut self) {
        if let Some(dir) = &self.0 {
            let _ = std::fs::remove_dir_all(dir);
        }
    }
}

/// The spooled restore upload, removed on drop: a client that goes away mid-upload drops the
/// handler, and the partial archive must not stay behind in the staging directory.
struct Upload(PathBuf);

impl Drop for Upload {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.0);
    }
}

/// Make every file and directory under `dir` durable, so the store renamed into place after a
/// crash holds the unpacked bytes rather than whatever reached the disk.
fn sync_tree(dir: &Path) -> std::io::Result<()> {
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let kind = entry.file_type()?;
        if kind.is_dir() {
            sync_tree(&entry.path())?;
        } else if kind.is_file() {
            // Unix fsyncs through any handle (an archive may unpack read-only files); Windows
            // flushes file buffers only through a write handle.
            std::fs::OpenOptions::new()
                .read(cfg!(unix))
                .write(!cfg!(unix))
                .open(entry.path())?
                .sync_all()?;
        }
    }
    crate::vindex::sync_dir(dir)
}

/// Verify the staged store and swap it in for the tenant's; the staging directory is removed
/// on failure. Returns the snapshot taken of the replaced store.
fn install_staged(state: &AppState, tenant: &str, staging: Staging) -> Result<String, ApiError> {
    verify(staging.path())?;
    let staged = staging.take();
    let result = state.restore_staged(tenant, &staged);
    if result.is_err() {
        let _ = std::fs::remove_dir_all(&staged);
    }
    result
}

/// `POST /api/v1/snapshots/{id}/restore`: replace the tenant's store with a snapshot. The
/// store being replaced is kept as snapshot `pre-restore-<time>`.
pub async fn restore_snapshot(
    State(state): State<Arc<AppState>>,
    UrlPath(id): UrlPath<String>,
    req: Request,
) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Admin)?;
    let permit = acquire(&state).await?;
    let previous = blocking(permit, move || {
        let source = state.snapshot_path(&tenant, &id)?;
        let staging = Staging::new(&state)?;
        copy_store(&source, staging.path()).map_err(ApiError::internal)?;
        install_staged(&state, &tenant, staging)
    })
    .await?;
    Ok(json_response(
        StatusCode::OK,
        &body(&Restored {
            restored: true,
            previous_snapshot: previous,
        }),
    ))
}

/// Unpack a tar of regular files and directories into `dst`, refusing links, devices, absolute
/// or parent-relative paths, and oversized archives.
fn unpack_archive(file: std::fs::File, dst: &Path, max_bytes: u64) -> Result<(), ApiError> {
    let invalid = |why: &str| ApiError::bad(format!("invalid snapshot archive: {why}"));
    std::fs::create_dir_all(dst).map_err(ApiError::internal)?;
    let mut archive = tar::Archive::new(file);
    let (mut total, mut count) = (0u64, 0usize);
    for entry in archive.entries().map_err(|_| invalid("not a tar file"))? {
        let mut entry = entry.map_err(|_| invalid("unreadable entry"))?;
        let kind = entry.header().entry_type();
        if !(kind.is_file() || kind.is_dir()) {
            return Err(invalid("only regular files and directories are allowed"));
        }
        let path = entry.path().map_err(|_| invalid("bad path"))?.into_owned();
        if path
            .components()
            .any(|c| !matches!(c, Component::Normal(_) | Component::CurDir))
        {
            return Err(invalid("paths must be relative without \"..\""));
        }
        count += 1;
        total = total.saturating_add(entry.header().size().unwrap_or(0));
        if count > 100_000 || total > max_bytes {
            return Err(invalid("archive is larger than this server accepts"));
        }
        if !entry
            .unpack_in(dst)
            .map_err(|_| invalid("cannot unpack entry"))?
        {
            return Err(invalid("entry escapes the destination"));
        }
    }
    Ok(())
}

/// `PUT /api/v1/restore` with a tar body (as produced by the archive endpoint): replace the
/// tenant's store. The upload is size-limited by `--max-restore-bytes`, verified, then swapped in.
pub async fn restore_upload(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Admin)?;
    let permit = acquire(&state).await?;
    let staging = Staging::new(&state)?;
    let upload = Upload(staging.path().with_extension("tar"));
    let max = state.cfg.max_restore_bytes;
    let idle = Duration::from_secs(state.cfg.body_timeout_seconds);
    let spooled: Result<(), ApiError> = async {
        if let Some(parent) = upload.0.parent() {
            tokio::fs::create_dir_all(parent)
                .await
                .map_err(ApiError::internal)?;
        }
        let mut file = tokio::fs::File::create(&upload.0)
            .await
            .map_err(ApiError::internal)?;
        let mut body = req.into_body();
        let mut total = 0u64;
        loop {
            let frame = tokio::time::timeout(idle, body.frame())
                .await
                .map_err(|_| {
                    ApiError::new(
                        StatusCode::REQUEST_TIMEOUT,
                        "request_timeout",
                        "upload timed out",
                    )
                })?;
            match frame {
                None => break,
                Some(Err(_)) => return Err(ApiError::bad("request body could not be read")),
                Some(Ok(frame)) => {
                    if let Ok(data) = frame.into_data() {
                        total += data.len() as u64;
                        if total > max {
                            return Err(ApiError::new(
                                StatusCode::PAYLOAD_TOO_LARGE,
                                "payload_too_large",
                                format!("archive exceeds {max} bytes"),
                            ));
                        }
                        file.write_all(&data).await.map_err(ApiError::internal)?;
                    }
                }
            }
        }
        file.sync_all().await.map_err(ApiError::internal)?;
        Ok(())
    }
    .await;
    spooled?;
    // The blocking task owns the upload from here, so a client leaving mid-restore cannot
    // remove the archive while it is being unpacked.
    let previous = blocking(permit, move || {
        let file = std::fs::File::open(&upload.0).map_err(ApiError::internal)?;
        unpack_archive(file, staging.path(), max)?;
        drop(upload);
        sync_tree(staging.path()).map_err(ApiError::internal)?;
        install_staged(&state, &tenant, staging)
    })
    .await?;
    Ok(json_response(
        StatusCode::OK,
        &body(&Restored {
            restored: true,
            previous_snapshot: previous,
        }),
    ))
}

pub async fn compaction_status(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Read)?;
    let permit = acquire(&state).await?;
    let interval = state.cfg.compaction_interval_seconds;
    let status = blocking(permit, move || state.compaction_status(&tenant)).await?;
    Ok(json_response(
        StatusCode::OK,
        &body(&CompactionStatusBody {
            scheduled_interval_seconds: interval,
            status: CompactionView::from(&status),
        }),
    ))
}

pub async fn run_compaction(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let (_, tenant) = admit(&state, &req, Scope::Admin)?;
    let permit = acquire(&state).await?;
    let status = blocking(permit, move || state.compact(&tenant, "manual")).await?;
    Ok(json_response(
        StatusCode::OK,
        &body(&CompactionRun {
            status: CompactionView::from(&status),
        }),
    ))
}

pub async fn reload_keys(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let principal = principal(&req)?;
    principal.require(Scope::Admin)?;
    principal.require_global()?;
    let keys = state.auth.reload().map_err(|e| {
        ApiError::bad(format!(
            "keys not reloaded, previous keys stay in force: {e:#}"
        ))
    })?;
    crate::log_event!(Info, "keys_reloaded", "keys" => keys);
    Ok(json_response(StatusCode::OK, &body(&KeysReloaded { keys })))
}

pub async fn reload_tls(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let principal = principal(&req)?;
    principal.require(Scope::Admin)?;
    principal.require_global()?;
    let Some(tls) = state.tls.get() else {
        return Err(ApiError::bad("TLS is not enabled on this server"));
    };
    tls.reload().map_err(|e| {
        ApiError::bad(format!(
            "certificate not reloaded, the previous one stays in use: {e:#}"
        ))
    })?;
    crate::log_event!(Info, "tls_reloaded");
    Ok(json_response(
        StatusCode::OK,
        &body(&TlsReloaded { reloaded: true }),
    ))
}

pub async fn list_tenants(State(state): State<Arc<AppState>>, req: Request) -> ApiResult {
    let principal = principal(&req)?;
    principal.require(Scope::Admin)?;
    principal.require_global()?;
    let root = state.cfg.data_dir.join("tenants");
    let mut tenants = Vec::new();
    if let Ok(entries) = std::fs::read_dir(&root) {
        for entry in entries.flatten() {
            let name = entry.file_name().to_string_lossy().into_owned();
            tenants.extend(tenant_from_hex(&name));
        }
    }
    tenants.sort();
    Ok(json_response(
        StatusCode::OK,
        &body(&Tenants {
            open: state.open_tenant_count(),
            tenants,
        }),
    ))
}