tabularium 0.1.7

Markdown-oriented document store library (SQLite + Tantivy)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Higher-level document operations (resolve keys, text slices, search hits).

use std::collections::HashMap;

use regex::Regex;
use tracing::instrument;

use crate::db::entry_kind::EntryKind;
use crate::db::meta::{DocumentMeta, GrepLine, SearchHit, WcStats};
use crate::db::{Database, EntryId, Storage};
use crate::resource_path::{canonical_path_segments, parent_and_final_name};
use crate::validation::{
    escape_chat_heading_label, validate_chat_speaker_id, validate_entity_name,
};
use crate::{Error, Result};

impl<S: Storage> Database<S> {
    /// Note: `NotFound` is normal when probing file-vs-directory (REST `get_or_list`); no `err(Debug)` to avoid ERROR spam.
    #[instrument(skip(self), fields(file_path = %file_path.as_ref()))]
    pub async fn resolve_file_path(&self, file_path: impl AsRef<str> + Send) -> Result<EntryId> {
        let p = file_path.as_ref();
        self.storage.resolve_path(p, Some(EntryKind::File)).await
    }

    #[instrument(skip(self), fields(dir_path = %dir_path.as_ref()), err(Debug))]
    pub async fn resolve_directory_path(
        &self,
        dir_path: impl AsRef<str> + Send,
    ) -> Result<EntryId> {
        self.storage
            .resolve_path(dir_path.as_ref(), Some(EntryKind::Dir))
            .await
    }

    #[instrument(skip(self), fields(file_id = file_id.raw()), err(Debug))]
    pub async fn get_document_meta(&self, file_id: EntryId) -> Result<DocumentMeta> {
        self.storage.get_file_meta(file_id).await
    }

    #[instrument(skip(self), fields(file_id = file_id.raw()), err(Debug))]
    pub async fn cat_document_bundle(&self, file_id: EntryId) -> Result<(DocumentMeta, String)> {
        let meta = self.storage.get_file_meta(file_id).await?;
        let body = self.get_document(file_id).await?;
        Ok((meta, body))
    }

    #[instrument(skip(self, path), err(Debug))]
    pub async fn document_ref_by_path(&self, path: impl AsRef<str> + Send) -> Result<DocumentMeta> {
        let id = self.resolve_file_path(path.as_ref()).await?;
        self.storage.get_file_meta(id).await
    }

    /// Append to an existing file, or create it (and parent directories) if absent.
    ///
    /// `force` controls behaviour when the target file already exists:
    /// - `false` (safe default for agents): existing target → [`Error::Duplicate`]; missing target → create.
    ///   The check-then-create is **atomic** at the storage layer (SQLite `UNIQUE(parent_id, name)`),
    ///   so concurrent callers cannot both observe "missing" and both create.
    /// - `true`: append to existing body, or create when missing (legacy upsert behaviour).
    #[instrument(skip(self, path, to_append), fields(force), err(Debug))]
    pub async fn append_document_by_path(
        &self,
        path: impl AsRef<str> + Send,
        to_append: impl AsRef<str> + Send,
        force: bool,
    ) -> Result<()> {
        let path = path.as_ref();
        let (parent, name) = parent_and_final_name(path)?;
        validate_entity_name(&name)?;
        self.storage.ensure_directory_path(&parent).await?;
        let piece = to_append.as_ref();
        if !force {
            self.create_file_in_directory(&parent, &name, piece).await?;
            return Ok(());
        }
        match self.resolve_file_path(path).await {
            Ok(fid) => self.append_document(fid, piece).await,
            Err(Error::NotFound(_)) => {
                self.create_file_in_directory(&parent, &name, piece).await?;
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Append `to_append` only if `marker` does not occur as a **substring** anywhere in the
    /// current UTF-8 body (Rust [`str::contains`]). The document must already exist.
    ///
    /// Uses a per-document async mutex so concurrent callers cannot both observe “marker absent”
    /// and append (TOCTOU). Missing document is [`Error::NotFound`], not `Ok(false)`.
    ///
    /// Returns `true` if bytes were appended, `false` if skipped because the marker was present.
    #[instrument(skip(self, path, marker, to_append), err(Debug))]
    pub async fn append_if_not_contains_by_path(
        &self,
        path: impl AsRef<str> + Send,
        marker: impl AsRef<str> + Send,
        to_append: impl AsRef<str> + Send,
    ) -> Result<bool> {
        let path = path.as_ref();
        let marker = marker.as_ref();
        let to_append = to_append.as_ref();
        canonical_path_segments(path)?;
        if marker.is_empty() {
            return Err(Error::InvalidInput(
                "append_if_not_contains: marker must be non-empty".into(),
            ));
        }
        let fid = self.resolve_file_path(path).await?;
        if to_append.is_empty() {
            return Ok(false);
        }
        let lock = self.doc_append_mutex(fid);
        let _guard = lock.lock().await;
        let current = self.storage.get_file_content(fid).await?;
        if current.contains(marker) {
            return Ok(false);
        }
        self.append_document(fid, to_append).await?;
        Ok(true)
    }

    #[instrument(skip(self, path, from_id, text), err(Debug))]
    pub async fn say_document_by_path(
        &self,
        path: impl AsRef<str> + Send,
        from_id: impl AsRef<str> + Send,
        text: impl AsRef<str> + Send,
    ) -> Result<()> {
        let path = path.as_ref();
        let from_id = from_id.as_ref();
        validate_chat_speaker_id(from_id)?;
        let body = text.as_ref().trim_end_matches(['\r', '\n']);
        let label = escape_chat_heading_label(from_id);
        let fid = match self.resolve_file_path(path).await {
            Ok(id) => id,
            Err(Error::NotFound(_)) => {
                return Err(Error::InvalidInput(format!(
                    "say_document: document does not exist (create it with append_document or put_document first): {path}"
                )));
            }
            Err(e) => return Err(e),
        };
        let current = self.storage.get_file_content(fid).await?;
        let sep = if current.is_empty() || current.ends_with("\n\n") {
            ""
        } else if current.ends_with('\n') {
            "\n"
        } else {
            "\n\n"
        };
        let piece = format!("{sep}## {label}\n\n{body}\n\n");
        self.append_document(fid, &piece).await
    }

    #[instrument(
        skip(self),
        fields(file_id = file_id.raw(), lines),
        err(Debug)
    )]
    pub async fn document_head(&self, file_id: EntryId, lines: u32) -> Result<String> {
        let content = self.get_document(file_id).await?;
        Ok(crate::text_lines::head_logical_lines(&content, lines))
    }

    #[instrument(
        skip(self),
        fields(file_id = file_id.raw(), tail = ?mode),
        err(Debug)
    )]
    pub async fn document_tail(
        &self,
        file_id: EntryId,
        mode: crate::text_lines::TailMode,
    ) -> Result<String> {
        let content = self.get_document(file_id).await?;
        Ok(crate::text_lines::apply_tail_logical_lines(&content, mode))
    }

    #[instrument(
        skip(self),
        fields(file_id = file_id.raw(), start_line, end_line),
        err(Debug)
    )]
    pub async fn document_slice(
        &self,
        file_id: EntryId,
        start_line: u32,
        end_line: u32,
    ) -> Result<String> {
        if start_line == 0 || end_line == 0 || start_line > end_line {
            return Err(Error::InvalidInput("line range invalid".into()));
        }
        let content = self.get_document(file_id).await?;
        Ok(slice_lines(
            &content,
            start_line as usize,
            end_line as usize,
        ))
    }

    #[instrument(skip(self), fields(file_id = file_id.raw()), err(Debug))]
    pub async fn document_wc(&self, file_id: EntryId) -> Result<WcStats> {
        let content = self.get_document(file_id).await?;
        Ok(WcStats::from_content(&content))
    }

    #[instrument(skip(self), fields(file_id = file_id.raw()), err(Debug))]
    pub async fn document_stat(&self, file_id: EntryId) -> Result<(DocumentMeta, String, usize)> {
        let meta = self.storage.get_file_meta(file_id).await?;
        let parent_path = self.storage.canonical_path(meta.parent_id()).await?;
        let content = self.get_document(file_id).await?;
        let lines = content.lines().count();
        Ok((meta, parent_path, lines))
    }

    #[instrument(
        skip(self, pattern),
        fields(file_id = file_id.raw(), pattern_len = pattern.len(), max_matches),
        err(Debug)
    )]
    pub async fn document_grep(
        &self,
        file_id: EntryId,
        pattern: &str,
        max_matches: usize,
        invert_match: bool,
    ) -> Result<Vec<GrepLine>> {
        let re = Regex::new(pattern).map_err(|e| Error::InvalidInput(e.to_string()))?;
        let content = self.get_document(file_id).await?;
        let cap = if max_matches == 0 {
            usize::MAX
        } else {
            max_matches
        };
        let mut out = Vec::new();
        for (i, line) in content.lines().enumerate() {
            let matched = re.is_match(line);
            if matched != invert_match {
                out.push(GrepLine::new(i + 1, line.to_string()));
                if out.len() >= cap {
                    break;
                }
            }
        }
        Ok(out)
    }

    #[instrument(
        skip(self, keywords),
        fields(
            keywords_len = keywords.as_ref().len(),
            directory = ?directory_prefix,
            restrict_document = ?restrict_to_document.map(|id| id.raw()),
            limit,
        ),
        err(Debug)
    )]
    pub async fn search_hits(
        &self,
        keywords: impl AsRef<str>,
        directory_prefix: Option<&str>,
        limit: usize,
        restrict_to_document: Option<EntryId>,
    ) -> Result<Vec<SearchHit>> {
        let norm = directory_prefix.and_then(|p| {
            let t = p.trim();
            if t.is_empty() || t == "/" {
                None
            } else {
                Some(t.trim_end_matches('/'))
            }
        });
        let scored = self
            .search
            .search_scored(keywords.as_ref(), norm, restrict_to_document)
            .await?;
        let take = scored.len().min(limit);
        let scored: Vec<_> = scored.into_iter().take(take).collect();
        let ids: Vec<EntryId> = scored.iter().map(|(id, _)| *id).collect();
        let rows = self.storage.files_display_batch(&ids).await?;
        let mut map: HashMap<i64, (String, String)> = HashMap::new();
        for (id, path, body) in rows {
            map.insert(id.raw(), (path, body));
        }
        let mut hits = Vec::new();
        for (id, score) in scored {
            let Some((path, content)) = map.get(&id.raw()) else {
                continue;
            };
            let (snippet, line_number) = search_snippet_and_line(content, keywords.as_ref());
            hits.push(SearchHit::new(
                id,
                path.clone(),
                snippet,
                score,
                line_number,
            ));
        }
        Ok(hits)
    }

    #[instrument(skip(self, path), err(Debug))]
    pub async fn document_exists_at_path(&self, path: impl AsRef<str> + Send) -> Result<bool> {
        match self.resolve_file_path(path.as_ref()).await {
            Ok(_) => Ok(true),
            Err(Error::NotFound(_)) => Ok(false),
            Err(e) => Err(e),
        }
    }

    /// Create a file at an absolute path (parent directories must exist).
    ///
    /// `force` overwrites an existing file when `true`. With `only_if_revision`, the server asserts the
    /// path names an existing file: missing target → [`Error::NotFound`]; mismatch → [`Error::RevisionMismatch`].
    /// Existing target with `force=false` → [`Error::Duplicate`] (`only_if_revision` not consulted).
    #[instrument(skip(self, path, content), fields(force, only_if = only_if_revision.is_some()), err(Debug))]
    pub async fn create_document_at_path(
        &self,
        path: impl AsRef<str> + Send,
        content: impl AsRef<str> + Send,
        force: bool,
        only_if_revision: Option<&str>,
    ) -> Result<EntryId> {
        let path = path.as_ref();
        canonical_path_segments(path)?;
        let (parent, name) = parent_and_final_name(path)?;
        validate_entity_name(&name)?;
        match self.resolve_file_path(path).await {
            Ok(id) => {
                if !force {
                    return Err(Error::Duplicate("name already exists in directory".into()));
                }
                if let Some(exp) = only_if_revision {
                    self.update_document_if_revision(id, content.as_ref(), exp)
                        .await?;
                } else {
                    self.update_document(id, content.as_ref()).await?;
                }
                Ok(id)
            }
            Err(Error::NotFound(_)) => {
                if only_if_revision.is_some() {
                    return Err(Error::NotFound(path.to_string()));
                }
                self.create_file_in_directory(parent, name, content.as_ref())
                    .await
            }
            Err(e) => Err(e),
        }
    }

    /// Create parent directories as needed, then create or replace file body.
    ///
    /// `force` controls behaviour when the target file already exists:
    /// - `false` (safe default for agents): existing target → [`Error::Duplicate`]; missing target → create.
    ///   The check-then-create is **atomic** at the storage layer (SQLite `UNIQUE(parent_id, name)`),
    ///   so concurrent callers cannot both observe "missing" and both create.
    /// - `true`: replace existing body (legacy upsert behaviour) or create when missing.
    #[instrument(skip(self, path, content), fields(force, only_if = only_if_revision.is_some()), err(Debug))]
    pub async fn put_document_by_path(
        &self,
        path: impl AsRef<str> + Send,
        content: impl AsRef<str> + Send,
        force: bool,
        only_if_revision: Option<&str>,
    ) -> Result<()> {
        let path = path.as_ref();
        canonical_path_segments(path)?;
        let (parent, name) = parent_and_final_name(path)?;
        validate_entity_name(&name)?;
        self.storage.ensure_directory_path(&parent).await?;
        let content = content.as_ref();
        if let Some(exp) = only_if_revision {
            let id = self.resolve_file_path(path).await?;
            self.update_document_if_revision(id, content, exp).await?;
            return Ok(());
        }
        if !force {
            self.create_file_in_directory(parent, name, content).await?;
            return Ok(());
        }
        match self.resolve_file_path(path).await {
            Ok(id) => {
                self.update_document(id, content).await?;
            }
            Err(Error::NotFound(_)) => {
                self.create_file_in_directory(parent, name, content).await?;
            }
            Err(e) => return Err(e),
        }
        Ok(())
    }

    /// Unix-like `cp` within Tabularium virtual paths.
    ///
    /// - File source: copies body to `dst` (overwrites); if `dst` is an existing directory, copies into it.
    /// - Directory source: requires `recursive`; copies full subtree, including empty directories.
    #[instrument(skip(self), fields(src = %src.as_ref(), dst = %dst.as_ref(), recursive), err(Debug))]
    pub async fn cp(
        &self,
        src: impl AsRef<str> + Send,
        dst: impl AsRef<str> + Send,
        recursive: bool,
    ) -> Result<()> {
        fn trim_nonroot_trailing_slash(p: &str) -> &str {
            if p == "/" {
                "/"
            } else {
                p.trim_end_matches('/')
            }
        }

        let src = trim_nonroot_trailing_slash(src.as_ref());
        let dst = trim_nonroot_trailing_slash(dst.as_ref());
        if src.is_empty() || src == "/" {
            return Err(Error::InvalidInput("cp: invalid source path".into()));
        }
        canonical_path_segments(src)?;
        canonical_path_segments(dst)?;

        let dst_is_dir = match self.resolve_directory_path(dst).await {
            Ok(_) => true,
            Err(Error::NotFound(_)) => false,
            Err(e) => return Err(e),
        };

        // Try file first, then directory.
        if let Ok(fid) = self.resolve_file_path(src).await {
            let (_, name) = parent_and_final_name(src)?;
            let dst_file = if dst_is_dir {
                if dst == "/" {
                    format!("/{name}")
                } else {
                    format!("{dst}/{name}")
                }
            } else {
                dst.to_string()
            };
            if dst_file == src {
                return Err(Error::InvalidInput(
                    "cp: cannot copy a file onto itself".into(),
                ));
            }
            let body = self.get_document(fid).await?;
            self.put_document_by_path(&dst_file, &body, true, None)
                .await?;
            return Ok(());
        }

        let _src_did = self.resolve_directory_path(src).await?;
        if !recursive {
            return Err(Error::InvalidInput(format!(
                "cp: {src}: is a directory (use -r)"
            )));
        }
        let (_, src_name) = parent_and_final_name(src)?;
        let dst_root = if dst_is_dir {
            if dst == "/" {
                format!("/{src_name}")
            } else {
                format!("{dst}/{src_name}")
            }
        } else {
            dst.to_string()
        };

        if dst_root == src || dst_root.starts_with(&format!("{src}/")) {
            return Err(Error::InvalidInput(
                "cp: cannot copy a directory into itself".into(),
            ));
        }

        fn join_dir(parent: &str, name: &str) -> String {
            let p = parent.trim_end_matches('/');
            let n = name.trim_start_matches('/');
            if p.is_empty() || p == "/" {
                format!("/{n}")
            } else {
                format!("{p}/{n}")
            }
        }

        let mut stack: Vec<(String, String)> = vec![(src.to_string(), dst_root)];
        while let Some((src_dir, dst_dir)) = stack.pop() {
            self.storage.ensure_directory_path(&dst_dir).await?;
            let entries = self.storage.list_directory(&src_dir).await?;
            for e in entries {
                let src_child = join_dir(&src_dir, e.name());
                let dst_child = join_dir(&dst_dir, e.name());
                if e.kind() == EntryKind::Dir {
                    stack.push((src_child, dst_child));
                } else if e.kind() == EntryKind::File {
                    let fid = self
                        .storage
                        .resolve_path(&src_child, Some(EntryKind::File))
                        .await?;
                    let body = self.get_document(fid).await?;
                    self.put_document_by_path(&dst_child, &body, true, None)
                        .await?;
                }
            }
        }
        Ok(())
    }

    /// Resolve path for RPC: file path must exist as a file.
    #[instrument(skip(self, path))]
    pub async fn resolve_existing_file_path(
        &self,
        path: impl AsRef<str> + Send,
    ) -> Result<EntryId> {
        self.resolve_file_path(path.as_ref()).await
    }
}

fn slice_lines(content: &str, start: usize, end: usize) -> String {
    let lines: Vec<&str> = content.lines().collect();
    if start > lines.len() {
        return String::new();
    }
    let end = end.min(lines.len());
    lines[start - 1..end].join("\n")
}

fn floor_utf8_boundary(s: &str, mut i: usize) -> usize {
    i = i.min(s.len());
    while i > 0 && !s.is_char_boundary(i) {
        i -= 1;
    }
    i
}

fn search_snippet_and_line(content: &str, query: &str) -> (String, Option<usize>) {
    let q = query.trim();
    if q.is_empty() {
        return (content.chars().take(200).collect(), None);
    }
    let needle = q.split_whitespace().next().unwrap_or("");
    if needle.is_empty() {
        return (content.chars().take(200).collect(), None);
    }
    let Ok(re) = Regex::new(&format!(r"(?i){}", regex::escape(needle))) else {
        return (content.chars().take(200).collect(), None);
    };
    let Some(m) = re.find(content) else {
        return (content.chars().take(200).collect(), None);
    };
    let pos = m.start();
    let match_end = m.end();
    let line_number = Some(1 + content[..pos].bytes().filter(|&b| b == b'\n').count());
    let start_byte = pos.saturating_sub(40);
    let start = floor_utf8_boundary(content, start_byte);
    let slice = content.get(start..).unwrap_or(content);
    let mut s: String = slice.chars().take(200).collect();
    if start > 0 {
        s.insert(0, '');
    }
    if match_end < content.len() {
        s.push('');
    }
    (s, line_number)
}