haematite-cli 0.21.3

The haematite command: serve a store, its door and Studio, answer MCP, query with jq, and host the store's assistant seat
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
//! `haematite history-check`: a read-only probe of legacy history files against
//! their service rows (docs/design/HAEM-HISTORY-ROW-PROBE-2026-10-07.md).
//!
//! References are read with `read_refs_dir` and rows with
//! `ReadOnlyDatabase`, never through `Storage::open`, which moves a `KeepAll`
//! store to reclaim and rewrites format-1 records. A store whose writer lock
//! is held is refused: the probe is for copies. Each file is judged by
//! `history::reconcile`, the rule the importer's history phase uses.

use std::fmt::{self, Write as _};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use haematite::branch::history::{self, Difference, Reconciled, Row, SERVICE_BRANCH, ShardRoot};
use haematite::branch::{KeptHead, RefRecord};
use haematite::db::vacuum::{CANONICAL_REFS_DIR, read_refs_dir};
use haematite::db::{DataDirLock, ReadOnlyDatabase};

const USAGE: &str = "usage: haematite history-check <data_dir> [--json] [--show N]";
const DEFAULT_SHOW: usize = 50;

/// Why the probe ran no comparison.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Refusal {
    pub code: &'static str,
    pub message: String,
}

impl Refusal {
    fn new(code: &'static str, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
        }
    }
}

/// What one history file was found to be against its row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Finding {
    Reconciled(Reconciled),
    RowMissing,
    NameCorrupt,
    RowCorrupt(String),
}

impl Finding {
    /// The name the probe prints for this finding.
    #[must_use]
    pub const fn kind(&self) -> &'static str {
        match self {
            Self::Reconciled(Reconciled::Equal) => "equal",
            Self::Reconciled(Reconciled::ShardSetDiffers) => "shard_set_differs",
            Self::Reconciled(Reconciled::RowIsAKeptHead { .. }) => "row_is_a_kept_head",
            Self::Reconciled(Reconciled::RootsDiffer(_)) => "roots_differ",
            Self::RowMissing => "row_missing",
            Self::NameCorrupt => "name_corrupt",
            Self::RowCorrupt(_) => "row_corrupt",
        }
    }

    /// The fields printed after the name, in order.
    #[must_use]
    pub fn fields(&self) -> Vec<(&'static str, String)> {
        let shard = |difference: &Difference| {
            vec![
                ("shard", difference.shard_id.to_string()),
                ("file_head", difference.file_head.to_string()),
                ("row_root", difference.row_root.to_string()),
            ]
        };
        match self {
            Self::Reconciled(Reconciled::RootsDiffer(difference)) => shard(difference),
            Self::Reconciled(Reconciled::RowIsAKeptHead {
                difference,
                kept_index,
            }) => {
                let mut fields = shard(difference);
                fields.push(("kept_index", kept_index.to_string()));
                fields
            }
            Self::RowCorrupt(error) => vec![("error", error.clone())],
            Self::Reconciled(Reconciled::Equal | Reconciled::ShardSetDiffers)
            | Self::RowMissing
            | Self::NameCorrupt => Vec::new(),
        }
    }
}

/// One history file that is not equal to its row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Mismatch {
    pub name: String,
    pub finding: Finding,
}

/// How many history files were found to be each kind.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Counts {
    pub files: usize,
    pub equal: usize,
    pub row_missing: usize,
    pub roots_differ: usize,
    pub shard_set_differs: usize,
    pub row_is_a_kept_head: usize,
    pub name_corrupt: usize,
    pub row_corrupt: usize,
}

impl Counts {
    /// Every count, named as printed, in printed order.
    #[must_use]
    pub const fn fields(&self) -> [(&'static str, usize); 8] {
        [
            ("files", self.files),
            ("equal", self.equal),
            ("row_missing", self.row_missing),
            ("roots_differ", self.roots_differ),
            ("shard_set_differs", self.shard_set_differs),
            ("row_is_a_kept_head", self.row_is_a_kept_head),
            ("name_corrupt", self.name_corrupt),
            ("row_corrupt", self.row_corrupt),
        ]
    }
}

/// The probe's whole answer for one store.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Report {
    pub store: PathBuf,
    pub service_heads: Vec<ShardRoot>,
    pub counts: Counts,
    pub mismatches: Vec<Mismatch>,
}

impl Report {
    fn note(&mut self, name: String, finding: Finding) {
        self.counts.files += 1;
        let count = match &finding {
            Finding::Reconciled(Reconciled::Equal) => {
                self.counts.equal += 1;
                return;
            }
            Finding::Reconciled(Reconciled::ShardSetDiffers) => &mut self.counts.shard_set_differs,
            Finding::Reconciled(Reconciled::RowIsAKeptHead { .. }) => {
                &mut self.counts.row_is_a_kept_head
            }
            Finding::Reconciled(Reconciled::RootsDiffer(_)) => &mut self.counts.roots_differ,
            Finding::RowMissing => &mut self.counts.row_missing,
            Finding::NameCorrupt => &mut self.counts.name_corrupt,
            Finding::RowCorrupt(_) => &mut self.counts.row_corrupt,
        };
        *count += 1;
        self.mismatches.push(Mismatch { name, finding });
    }

    fn service_heads_text(&self) -> String {
        self.service_heads
            .iter()
            .map(|shard| format!("{}:{}", shard.shard_id, shard.root))
            .collect::<Vec<_>>()
            .join(",")
    }

    /// The summary line, at most `show` mismatch lines, and how many more
    /// there are when any are not shown.
    ///
    /// # Errors
    ///
    /// Writing into the text fails.
    pub fn text(&self, show: usize) -> Result<String, fmt::Error> {
        let mut text = String::new();
        write!(
            text,
            "history_check store={} service_heads={}",
            self.store.display(),
            self.service_heads_text()
        )?;
        for (name, count) in self.counts.fields() {
            write!(text, " {name}={count}")?;
        }
        writeln!(text)?;
        for mismatch in self.mismatches.iter().take(show) {
            write!(
                text,
                "mismatch kind={} name={}",
                mismatch.finding.kind(),
                mismatch.name
            )?;
            for (name, value) in mismatch.finding.fields() {
                write!(text, " {name}={value}")?;
            }
            writeln!(text)?;
        }
        let hidden = self.mismatches.len().saturating_sub(show);
        if hidden > 0 {
            writeln!(
                text,
                "\u{2026} and {hidden} more mismatches not shown (use --show)"
            )?;
        }
        Ok(text)
    }

    /// The same fields as [`Self::text`], as one JSON object.
    #[must_use]
    pub fn json(&self, show: usize) -> serde_json::Value {
        let mut value = serde_json::json!({
            "store": self.store.display().to_string(),
            "service_heads": self.service_heads_text(),
        });
        for (name, count) in self.counts.fields() {
            value[name] = count.into();
        }
        value["mismatches"] = self
            .mismatches
            .iter()
            .take(show)
            .map(|mismatch| {
                let mut entry = serde_json::json!({
                    "kind": mismatch.finding.kind(),
                    "name": mismatch.name,
                });
                for (name, field) in mismatch.finding.fields() {
                    entry[name] = field.into();
                }
                entry
            })
            .collect();
        value["not_shown"] = self.mismatches.len().saturating_sub(show).into();
        value
    }

    /// Whether every count but `equal` is zero.
    #[must_use]
    pub const fn all_equal(&self) -> bool {
        self.counts.files == self.counts.equal
    }
}

struct Options {
    data_dir: PathBuf,
    json: bool,
    show: usize,
}

/// Runs `haematite history-check`: exit 0 when every history file equals its
/// row, 1 when any does not, 2 when the probe refused.
pub fn run(args: impl Iterator<Item = String>) -> ExitCode {
    let outcome = parse(args).and_then(|options| {
        let report = check(&options.data_dir)?;
        let rendered = if options.json {
            format!("{:#}\n", report.json(options.show))
        } else {
            report
                .text(options.show)
                .map_err(|error| Refusal::new("history_check_render", error.to_string()))?
        };
        Ok((report.all_equal(), rendered))
    });
    match outcome {
        Ok((all_equal, rendered)) => {
            print!("{rendered}");
            if all_equal {
                ExitCode::SUCCESS
            } else {
                ExitCode::FAILURE
            }
        }
        Err(refusal) => {
            eprintln!("refused: {}: {}", refusal.code, refusal.message);
            ExitCode::from(2)
        }
    }
}

fn parse(mut args: impl Iterator<Item = String>) -> Result<Options, Refusal> {
    let usage =
        |message: String| Refusal::new("history_check_usage", format!("{message}\n{USAGE}"));
    let mut data_dir = None;
    let mut json = false;
    let mut show = DEFAULT_SHOW;
    while let Some(argument) = args.next() {
        match argument.as_str() {
            "--json" => json = true,
            "--show" => {
                let value = args
                    .next()
                    .ok_or_else(|| usage("--show needs a count".to_owned()))?;
                show = value
                    .parse()
                    .map_err(|_| usage(format!("--show {value:?} is not a count")))?;
            }
            flag if flag.starts_with("--") => {
                return Err(usage(format!("unknown flag {flag:?}")));
            }
            path if data_dir.is_none() => data_dir = Some(PathBuf::from(path)),
            other => return Err(usage(format!("unexpected argument {other:?}"))),
        }
    }
    Ok(Options {
        data_dir: data_dir.ok_or_else(|| usage("a data directory is needed".to_owned()))?,
        json,
        show,
    })
}

/// Compares every legacy history file in `data_dir` with its service row,
/// writing nothing.
///
/// # Errors
///
/// A live writer holds the store, or its references, service record or
/// rows cannot be read.
pub fn check(data_dir: &Path) -> Result<Report, Refusal> {
    match DataDirLock::is_held(data_dir) {
        Ok(false) => {}
        Ok(true) => {
            return Err(Refusal::new(
                "history_check_store_in_use",
                format!(
                    "{} has a live writer; run the check on a copy",
                    data_dir.display()
                ),
            ));
        }
        Err(error) => {
            return Err(Refusal::new(
                "history_check_lock_unreadable",
                error.to_string(),
            ));
        }
    }
    let scan = read_refs_dir(&data_dir.join(CANONICAL_REFS_DIR))
        .map_err(|error| Refusal::new("history_check_refs_unreadable", error.to_string()))?;
    let service = scan
        .records
        .iter()
        .find(|record| record.name == SERVICE_BRANCH)
        .ok_or_else(|| Refusal::new("history_check_not_served", "no service record"))?;
    let reader = ReadOnlyDatabase::open(data_dir)
        .map_err(|error| Refusal::new("history_check_store_unreadable", error.to_string()))?;
    let mut files = scan
        .records
        .iter()
        .filter(|record| record.name.starts_with(history::FILE_PREFIX))
        .collect::<Vec<_>>();
    files.sort_by(|left, right| left.name.cmp(&right.name));
    let mut report = Report {
        store: data_dir.to_path_buf(),
        service_heads: heads(service),
        counts: Counts::default(),
        mismatches: Vec::new(),
    };
    for record in files {
        let kept = scan
            .kept
            .get(&record.name)
            .map(Vec::as_slice)
            .unwrap_or_default();
        let finding = judge(&reader, &report.service_heads, record, kept)?;
        report.note(record.name.clone(), finding);
    }
    Ok(report)
}

fn judge(
    reader: &ReadOnlyDatabase,
    service: &[ShardRoot],
    record: &RefRecord,
    kept: &[KeptHead],
) -> Result<Finding, Refusal> {
    let Some(key) = history::file_key(&record.name) else {
        return Ok(Finding::NameCorrupt);
    };
    let encoded = key.encode();
    let shard_id = reader.shard_for(&encoded);
    let root = service
        .iter()
        .find(|shard| shard.shard_id == shard_id)
        .ok_or_else(|| {
            Refusal::new(
                "history_check_service_shard_missing",
                format!("the service record has no root for shard {shard_id}"),
            )
        })?
        .root;
    let value = reader.get_at(shard_id, root, &encoded).map_err(|error| {
        Refusal::new(
            "history_check_row_unreadable",
            format!("{}: {error}", record.name),
        )
    })?;
    let Some(value) = value else {
        return Ok(Finding::RowMissing);
    };
    let row = match Row::decode(&encoded, &value) {
        Ok(row) => row,
        Err(error) => return Ok(Finding::RowCorrupt(error.to_string())),
    };
    let versions = kept
        .iter()
        .map(|version| {
            record
                .shards
                .iter()
                .zip(&version.heads)
                .map(|(shard, root)| ShardRoot {
                    shard_id: shard.shard_id,
                    root: *root,
                })
                .collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();
    Ok(Finding::Reconciled(history::reconcile(
        &row,
        &heads(record),
        &versions,
    )))
}

fn heads(record: &RefRecord) -> Vec<ShardRoot> {
    record
        .shards
        .iter()
        .map(|shard| ShardRoot {
            shard_id: shard.shard_id,
            root: shard.head,
        })
        .collect()
}