trible 0.42.0

A knowledge graph and meta file system for object stores.
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
use anyhow::Result;
use clap::Parser;
use std::fs::File;
use std::path::{Path, PathBuf};

#[derive(Parser)]
pub enum Command {
    /// Verify pile integrity (blob hash validation + branch commit-chain checks).
    Check {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Exit non-zero at the first detected issue
        #[arg(long)]
        fail_fast: bool,
    },
    /// Locate occurrences of a blob handle in raw pile bytes.
    ///
    /// This is useful when the normal repository graph fails (e.g. a branch
    /// points at a missing blob) and you want to distinguish:
    /// - a missing blob record (0 header matches), vs
    /// - a blob referenced inside other blob payloads (payload refs)
    LocateHash {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Handle to locate (e.g. "blake3:HEX..." or bare 64 hex)
        handle: String,
    },
}

pub fn run(cmd: Command) -> Result<()> {
    match cmd {
        Command::Check { pile, fail_fast } => check(&pile, fail_fast),
        Command::LocateHash { pile, handle } => locate_hash_in_pile(&pile, &handle),
    }
}

fn check(pile_path: &Path, fail_fast: bool) -> Result<()> {
    use triblespace::prelude::blobencodings::{LongString, SimpleArchive};
    use triblespace::prelude::{BlobStore, BlobStoreGet, BranchStore};

    use triblespace_core::id::id_hex;
    use triblespace_core::repo::BlobStoreMeta;
    use triblespace_core::repo::pile::{Pile, ReadError};
    use triblespace_core::trible::TribleSet;
    use triblespace_core::inline::encodings::hash::{Blake3, Handle, Hash};
    use triblespace_core::inline::Inline;

    match Pile::open(pile_path) {
        Ok(mut pile) => {
            let res = (|| -> Result<(), anyhow::Error> {
                let mut any_error = false;
                let reader = pile
                    .reader()
                    .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                // Blob hash validation.
                let mut invalid = 0usize;
                let mut total = 0usize;
                for item in reader.iter() {
                    match item {
                        Ok((handle, blob)) => {
                            total += 1;
                            let expected: triblespace_core::inline::Inline<Hash<Blake3>> =
                                Handle::to_hash(handle);
                            let computed = Hash::<Blake3>::digest(&blob.bytes);
                            if expected != computed {
                                invalid += 1;
                            }
                        }
                        Err(_) => {
                            // Treat iterator errors (validation, missing index) as invalid blobs.
                            total += 1;
                            invalid += 1;
                        }
                    }
                }

                if invalid == 0 {
                    println!("Pile appears healthy");
                } else {
                    println!("Pile corrupt: {invalid} of {total} blobs have incorrect hashes");
                    if fail_fast {
                        anyhow::bail!("invalid blob hashes detected");
                    }
                    any_error = true;
                }

                // Branch integrity diagnostics.
                println!("\nBranches:");
                let _repo_branch_attr: triblespace_core::id::Id =
                    id_hex!("8694CC73AF96A5E1C7635C677D1B928A");
                let repo_head_attr: triblespace_core::id::Id =
                    id_hex!("272FBC56108F336C4D2E17289468C35F");
                let repo_parent_attr: triblespace_core::id::Id =
                    id_hex!("317044B612C690000D798CA660ECFD2A");
                let repo_content_attr: triblespace_core::id::Id =
                    id_hex!("4DD4DDD05CC31734B03ABB4E43188B1F");

                fn verify_chain(
                    reader: &triblespace_core::repo::pile::PileReader,
                    start: Inline<Handle<SimpleArchive>>,
                    repo_parent_attr: triblespace_core::id::Id,
                    repo_content_attr: triblespace_core::id::Id,
                ) -> (usize, Option<String>) {
                    use std::collections::BTreeSet;
                    let mut visited: BTreeSet<String> = BTreeSet::new();
                    let mut stack: Vec<Inline<Handle<SimpleArchive>>> = vec![start];
                    let mut count = 0usize;
                    while let Some(h) = stack.pop() {
                        let hh: Inline<Hash<Blake3>> = Handle::to_hash(h);
                        let hex: String = hh.from_inline();
                        if !visited.insert(hex.clone()) {
                            continue;
                        }
                        match reader.metadata(h) {
                            Ok(None) => {
                                return (count, Some(format!("commit blake3:{hex} missing")));
                            }
                            Ok(Some(_)) => {}
                            Err(e) => {
                                return (
                                    count,
                                    Some(format!("commit blake3:{hex} metadata error: {e:?}")),
                                );
                            }
                        }
                        let meta: TribleSet = match reader.get::<TribleSet, SimpleArchive>(h) {
                            Ok(m) => m,
                            Err(e) => {
                                return (
                                    count,
                                    Some(format!("commit blake3:{hex} decode failed: {e:?}")),
                                )
                            }
                        };
                        let mut content_handle: Option<Inline<Handle<SimpleArchive>>> = None;
                        let mut parents: Vec<Inline<Handle<SimpleArchive>>> = Vec::new();
                        for t in meta.iter() {
                            if t.a() == &repo_content_attr {
                                content_handle = Some(*t.v::<Handle<SimpleArchive>>());
                            } else if t.a() == &repo_parent_attr {
                                parents.push(*t.v::<Handle<SimpleArchive>>());
                            }
                        }
                        // Some commits (for example merge-only commits) intentionally do not carry
                        // a content blob. Only verify content existence when present.
                        if let Some(c) = content_handle {
                            match reader.metadata(c) {
                                Ok(Some(_)) => {}
                                Ok(None) => {
                                    return (
                                        count,
                                        Some(format!("commit blake3:{hex} content blob missing")),
                                    );
                                }
                                Err(e) => {
                                    return (
                                        count,
                                        Some(format!("commit blake3:{hex} metadata error: {e:?}")),
                                    );
                                }
                            }
                        }
                        for p in parents {
                            stack.push(p);
                        }
                        count += 1;
                    }
                    (count, None)
                }

                // Ensure in-memory indices are loaded before enumerating branches.
                pile.refresh()?;
                let iter = pile.branches()?;
                for r in iter {
                    let bid = r?;
                    let meta_handle_opt = pile.head(bid)?;
                    let id_hex = format!("{bid:X}");
                    match meta_handle_opt {
                        None => {
                            println!("- {id_hex}: <no branch metadata head set>");
                        }
                        Some(meta_handle) => {
                            let meta_present = reader.metadata(meta_handle)?.is_some();
                            let mut name_val: Option<String> = None;
                            let mut head_val: Option<Inline<Handle<SimpleArchive>>> = None;
                            let mut meta_err: Option<String> = None;
                            let name_attr = triblespace_core::metadata::name.id();
                            if meta_present {
                                match reader.get::<TribleSet, SimpleArchive>(meta_handle) {
                                    Ok(meta) => {
                                        for t in meta.iter() {
                                            if t.a() == &name_attr {
                                                let h: Inline<Handle<LongString>> = *t.v();
                                                if let Ok(view) =
                                                    reader.get::<triblespace::prelude::View<str>, _>(h)
                                                {
                                                    name_val = Some(view.as_ref().to_string());
                                                }
                                            } else if t.a() == &repo_head_attr {
                                                head_val =
                                                    Some(*t.v::<Handle<SimpleArchive>>());
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        meta_err = Some(format!("decode failed: {e:?}"));
                                    }
                                }
                            }
                            let meta_hash: Inline<Hash<Blake3>> = Handle::to_hash(meta_handle);
                            let meta_hex: String = meta_hash.from_inline();
                            if let Some(n) = name_val.as_ref() {
                                println!(
                                    "- {id_hex} ({n}): meta blake3:{meta_hex} [{}]{}",
                                    if meta_present { "present" } else { "missing" },
                                    meta_err
                                        .as_deref()
                                        .map(|e| format!(" ({e})"))
                                        .unwrap_or_default()
                                );
                            } else {
                                println!(
                                    "- {id_hex}: meta blake3:{meta_hex} [{}]{}",
                                    if meta_present { "present" } else { "missing" },
                                    meta_err
                                        .as_deref()
                                        .map(|e| format!(" ({e})"))
                                        .unwrap_or_default()
                                );
                            }
                            if !meta_present {
                                if fail_fast {
                                    anyhow::bail!("branch metadata blob missing for {id_hex}");
                                }
                                any_error = true;
                                continue;
                            }
                            if meta_err.is_some() {
                                if fail_fast {
                                    anyhow::bail!("branch metadata decode failed for {id_hex}");
                                }
                                any_error = true;
                                continue;
                            }
                            if let Some(head) = head_val {
                                let (count, err) =
                                    verify_chain(&reader, head, repo_parent_attr, repo_content_attr);
                                if let Some(e) = err {
                                    println!("  commit chain error: {e}");
                                    if fail_fast {
                                        anyhow::bail!(e);
                                    }
                                    any_error = true;
                                } else {
                                    println!("  commit chain: {count} commits");
                                }
                            } else {
                                println!("  no head set");
                            }
                        }
                    }
                }

                if any_error {
                    anyhow::bail!("diagnostics reported issues");
                }

                Ok(())
            })();

            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Err(ReadError::IoError(err)) if err.kind() == std::io::ErrorKind::NotFound => {
            anyhow::bail!("pile not found");
        }
        Err(e) => return Err(e.into()),
    }
    Ok(())
}

fn padding_for_blob(blob_size: usize) -> usize {
    // Match `triblespace_core::repo::pile::padding_for_blob` without depending on it.
    (64 - ((64 + blob_size) % 64)) % 64
}

fn locate_hash_in_pile(pile_path: &Path, handle: &str) -> Result<()> {
    use anyhow::Context as _;
    use memchr::memmem::Finder;
    use triblespace_core::blob::Bytes;
    use triblespace_core::id::id_hex;
    use triblespace_core::inline::encodings::hash::Blake3;
    use triblespace_core::inline::encodings::hash::Hash;
    use triblespace_core::inline::Inline;

    let handle = handle.trim();
    let normalized = if !handle.contains(':') && handle.len() == 64 {
        format!("blake3:{handle}")
    } else {
        handle.to_owned()
    };
    let target: Inline<Hash<Blake3>> = crate::cli::util::parse_blob_handle(&normalized)?;
    let needle = target.raw;
    let needle_str: String = target.from_inline();

    let file = File::open(pile_path)
        .with_context(|| format!("open pile {}", pile_path.display()))?;
    let mapped = unsafe { Bytes::map_file(&file)? };
    let bytes: &[u8] = mapped.as_ref();

    // Magic markers copied from `triblespace_core::repo::pile` so we can
    // classify where the handle appears without mutating or indexing the pile.
    let marker_blob = id_hex!("1E08B022FF2F47B6EBACF1D68EB35D96").raw();
    let marker_branch = id_hex!("2BC991A7F5D5D2A3A468C53B0AA03504").raw();
    let marker_branch_tombstone = id_hex!("E888CC787202D2AE4C654BFE9699C430").raw();

    let finder = Finder::new(&needle);
    let mut offset = 0usize;
    let mut blob_header_matches = 0usize;
    let mut branch_header_matches = 0usize;
    let mut payload_matches = 0usize;
    let mut parse_error: Option<String> = None;

    while offset < bytes.len() {
        if offset + 16 > bytes.len() {
            break;
        }
        let magic = &bytes[offset..offset + 16];
        if magic == marker_blob {
            if offset + 64 > bytes.len() {
                parse_error = Some(format!("truncated blob header at byte {offset}"));
                break;
            }
            let length = u64::from_le_bytes(
                bytes[offset + 24..offset + 32]
                    .try_into()
                    .expect("u64 slice"),
            ) as usize;
            let hash_bytes: [u8; 32] = bytes[offset + 32..offset + 64]
                .try_into()
                .expect("hash slice");
            if hash_bytes == needle {
                blob_header_matches += 1;
                println!("blob header match at byte {offset}");
            }

            let payload_start = offset + 64;
            let pad = padding_for_blob(length);
            let record_end = payload_start
                .checked_add(length)
                .and_then(|v| v.checked_add(pad))
                .ok_or_else(|| anyhow::anyhow!("blob record length overflow at byte {offset}"))?;
            if record_end > bytes.len() {
                parse_error = Some(format!(
                    "truncated blob payload at byte {offset} (declared {length} bytes)"
                ));
                break;
            }

            let payload = &bytes[payload_start..payload_start + length];
            if let Some(_) = finder.find(payload) {
                let container_hash = Inline::<Hash<Blake3>>::new(hash_bytes);
                let container_str: String = container_hash.from_inline();
                for pos in finder.find_iter(payload) {
                    payload_matches += 1;
                    let absolute = payload_start + pos;
                    println!("payload reference in {container_str} at byte {absolute}");
                }
            }

            offset = record_end;
        } else if magic == marker_branch {
            if offset + 64 > bytes.len() {
                parse_error = Some(format!("truncated branch header at byte {offset}"));
                break;
            }
            let branch_id: [u8; 16] = bytes[offset + 16..offset + 32]
                .try_into()
                .expect("branch id slice");
            let hash_bytes: [u8; 32] = bytes[offset + 32..offset + 64]
                .try_into()
                .expect("branch hash slice");
            if hash_bytes == needle {
                branch_header_matches += 1;
                let branch_hex = hex::encode(branch_id).to_ascii_uppercase();
                println!("branch head match at byte {offset} (branch_id {branch_hex})");
            }
            offset += 64;
        } else if magic == marker_branch_tombstone {
            if offset + 64 > bytes.len() {
                parse_error = Some(format!("truncated branch tombstone at byte {offset}"));
                break;
            }
            offset += 64;
        } else {
            parse_error = Some(format!("unknown magic marker at byte {offset}"));
            break;
        }
    }

    println!("\nSummary for {needle_str}:");
    println!("  blob headers:   {blob_header_matches}");
    println!("  branch headers: {branch_header_matches}");
    println!("  payload refs:   {payload_matches}");
    if let Some(err) = parse_error {
        println!("  parse stopped:  {err}");
    }
    Ok(())
}