cyberbrain 0.6.0

Cited, trust-tiered, local-first memory for AI coding agents
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
//! The reconciliation. Two ledgers, both closed explicitly:
//!
//! - **files**: `on_disk` (independent count) = `listed` = mapped + skipped + unmapped
//! - **bytes**: per mapped file, bytes read = bytes in items (the split is lossless)
//! - **items**: `found` = written + unchanged + updated + held + duplicate + collided +
//!   exists + failed
//!
//! Every item that did not become a note is listed by name with its reason. A total is
//! never printed as the difference of two other totals (SPEC §14.3, §14.4).

use cyberbrain_core::{PiiState, Slash};
use serde::Serialize;
use std::fmt::Write as _;
use std::path::PathBuf;

#[derive(Debug, Clone, Serialize)]
pub struct ImportReport {
    pub dry_run: bool,
    #[serde(serialize_with = "cyberbrain_core::path_serde::slash")]
    pub root: PathBuf,
    pub plan: String,
    pub accept_pii: bool,
    pub files: FileLedger,
    pub items: ItemLedger,
    /// Every item, in source order.
    pub records: Vec<ItemRecord>,
    /// Mapped files that could not be read or decoded. Their items are unknown, which
    /// is why this list is separate from the item ledger and fails the run.
    pub file_failures: Vec<FileFailure>,
    /// Notes imported earlier from a mapped file that no current item produced. Not
    /// erased (erasure is the operator's act); listed so the operator knows.
    pub stale: Vec<StaleNote>,
    /// Dry run only: the audit rows a real run would append.
    pub audit_preview: Vec<String>,
    pub elapsed_ms: u128,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct FileLedger {
    /// Second, independent walk of the tree.
    pub on_disk: usize,
    /// What the importer's own walk listed.
    pub listed: usize,
    pub mapped: usize,
    pub skipped: Vec<SkippedFile>,
    pub unmapped: Vec<String>,
    /// Mapped files that were blank and so produced no item. Named, because a file
    /// that yields nothing is the shape of a silent drop even when it is legitimate.
    pub empty: Vec<String>,
    /// Bytes read from a file versus bytes handed on as items. The split is lossless by
    /// construction, so any entry here is an importer bug and fails the accounting.
    pub byte_loss: Vec<ByteLoss>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ByteLoss {
    pub path: String,
    pub bytes_read: usize,
    pub bytes_in_items: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct SkippedFile {
    pub path: String,
    pub rule: usize,
    pub reason: String,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ItemLedger {
    pub found: usize,
    pub written: usize,
    pub unchanged: usize,
    pub updated: usize,
    pub held: usize,
    /// Byte-identical text of another item of this run under the same name; the other
    /// item is the note. Nothing is lost, and it is still named.
    pub duplicate: usize,
    pub collided: usize,
    pub exists: usize,
    pub failed: usize,
}

impl ItemLedger {
    pub fn accounted(&self) -> usize {
        self.written
            + self.unchanged
            + self.updated
            + self.held
            + self.duplicate
            + self.collided
            + self.exists
            + self.failed
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct FileFailure {
    pub path: String,
    pub group: String,
    pub reason: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct StaleNote {
    pub name: String,
    pub ring: u8,
    pub source: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ItemRecord {
    pub file: String,
    pub index: usize,
    pub line: usize,
    pub group: String,
    /// Heading or first line, shortened; what a human would call this item.
    pub label: String,
    pub name: String,
    pub ring: u8,
    pub kind: String,
    pub bytes: usize,
    pub outcome: Outcome,
    /// Things worth knowing that are not failures: a fallback name, reused frontmatter.
    pub notes: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "outcome", rename_all = "kebab-case")]
pub enum Outcome {
    Written {
        path: PathBuf,
        pii: PiiState,
    },
    /// Already in the store with this content, ring and kind.
    Unchanged,
    /// Replaced an earlier import of the same item.
    Updated {
        #[serde(serialize_with = "cyberbrain_core::path_serde::slash")]
        path: PathBuf,
    },
    /// The PII gate held it (SPEC §12.4). `findings` summarises what was found.
    Held {
        findings: String,
    },
    /// Same name and byte-identical text as `of`, which is the note.
    Duplicate {
        of: String,
    },
    /// Another item of this run derived the same name with different text.
    Collision {
        with: Vec<String>,
    },
    /// A note of this name exists and is not the same item.
    Exists {
        why: String,
    },
    Failed {
        error: String,
    },
}

impl ImportReport {
    /// Both ledgers close. False means the importer lost track of something itself.
    pub fn is_balanced(&self) -> bool {
        let f = &self.files;
        f.on_disk == f.listed
            && f.listed == f.mapped + f.skipped.len() + f.unmapped.len()
            && f.byte_loss.is_empty()
            && self.items.found == self.items.accounted()
            && self.items.found == self.records.len()
    }

    /// Items that did not become notes and no rule said to skip.
    pub fn drops(&self) -> usize {
        self.items.collided + self.items.exists + self.items.failed + self.files.unmapped.len()
    }

    /// SPEC §8: 0 clean, 1 something did not make it, 2 the importer's own accounting
    /// failed, 3 only policy holds stand between the source and the store.
    pub fn exit_code(&self) -> i32 {
        if !self.is_balanced() {
            return 2;
        }
        if self.drops() > 0 || !self.file_failures.is_empty() {
            return 1;
        }
        if self.items.held > 0 {
            return 3;
        }
        0
    }

    pub fn render(&self) -> String {
        let mut s = String::new();
        if self.dry_run {
            s.push_str(
                "[dry run] nothing was written; every number below is what a real run would do\n",
            );
        }
        let _ = writeln!(s, "source: {}  (plan: {})", Slash(&self.root), self.plan);

        // ---- files
        let f = &self.files;
        let _ = writeln!(
            s,
            "files: {} on disk, {} listed = {} mapped + {} skipped by rule + {} unmapped",
            f.on_disk,
            f.listed,
            f.mapped,
            f.skipped.len(),
            f.unmapped.len()
        );
        if f.on_disk != f.listed {
            let _ = writeln!(
                s,
                "  !! the two walks disagree ({} vs {}): the importer's listing is not trustworthy",
                f.on_disk, f.listed
            );
        }
        if !f.unmapped.is_empty() {
            s.push_str(
                "  UNMAPPED (no rule matches; add a [[group]] or a [[skip]] with a reason):\n",
            );
            for p in &f.unmapped {
                let _ = writeln!(s, "    {p}");
            }
        }
        // Skipped, grouped by rule; each file named.
        let mut rule = 0usize;
        let mut first = true;
        for sk in &f.skipped {
            if first || sk.rule != rule {
                let _ = writeln!(s, "  skipped by rule #{} ({}):", sk.rule + 1, sk.reason);
                rule = sk.rule;
                first = false;
            }
            let _ = writeln!(s, "    {}", sk.path);
        }
        for bl in &f.byte_loss {
            let _ = writeln!(
                s,
                "  !! {}: {} bytes read but {} bytes reached items; the split lost content (importer bug)",
                bl.path, bl.bytes_read, bl.bytes_in_items
            );
        }
        for e in &f.empty {
            let _ = writeln!(s, "  empty (blank file, no note made): {e}");
        }
        for ff in &self.file_failures {
            let _ = writeln!(
                s,
                "  FILE NOT IMPORTED {} (group {}): {}",
                ff.path, ff.group, ff.reason
            );
        }

        // ---- items
        let it = &self.items;
        let _ = writeln!(
            s,
            "items: {} found = {} written + {} already imported + {} updated + {} held (PII) + {} duplicates + {} collisions + {} exist + {} failed",
            it.found,
            it.written,
            it.unchanged,
            it.updated,
            it.held,
            it.duplicate,
            it.collided,
            it.exists,
            it.failed
        );
        if it.found != it.accounted() {
            let _ = writeln!(
                s,
                "  !! UNBALANCED: {} item(s) found but {} accounted for",
                it.found,
                it.accounted()
            );
        }
        let mut section = |title: &str, pick: &dyn Fn(&ItemRecord) -> Option<String>| {
            let rows: Vec<String> = self
                .records
                .iter()
                .filter_map(|r| {
                    pick(r).map(|why| {
                        format!(
                            "    {} <- {} #{} line {} {:?}: {why}",
                            r.name, r.file, r.index, r.line, r.label
                        )
                    })
                })
                .collect();
            if !rows.is_empty() {
                let _ = writeln!(s, "  {title} ({}):", rows.len());
                for row in rows {
                    s.push_str(&row);
                    s.push('\n');
                }
            }
        };
        section(
            "held by the PII gate; re-run with --accept-pii to write them marked reviewed",
            &|r| match &r.outcome {
                Outcome::Held { findings } => Some(findings.clone()),
                _ => None,
            },
        );
        section(
            "duplicates: identical text under the same name as another item of this run; that item is the note",
            &|r| match &r.outcome {
                Outcome::Duplicate { of } => Some(format!("same bytes as {of}")),
                _ => None,
            },
        );
        section(
            "collisions: more than one item derived this name, nothing written for them; change note_name or set collisions = \"hash\"",
            &|r| match &r.outcome {
                Outcome::Collision { with } => Some(format!("also from {}", with.join(", "))),
                _ => None,
            },
        );
        section(
            "exist: a different note already has this name",
            &|r| match &r.outcome {
                Outcome::Exists { why } => Some(why.clone()),
                _ => None,
            },
        );
        section("failed", &|r| match &r.outcome {
            Outcome::Failed { error } => Some(error.clone()),
            _ => None,
        });
        section("updated", &|r| match &r.outcome {
            Outcome::Updated { path } => Some(cyberbrain_core::slash(path)),
            _ => None,
        });
        let noted: Vec<&ItemRecord> = self
            .records
            .iter()
            .filter(|r| !r.notes.is_empty())
            .collect();
        if !noted.is_empty() {
            let _ = writeln!(s, "  notes ({}):", noted.len());
            for r in noted {
                let _ = writeln!(
                    s,
                    "    {} <- {} #{}: {}",
                    r.name,
                    r.file,
                    r.index,
                    r.notes.join("; ")
                );
            }
        }

        // ---- per file
        s.push_str("per file:\n");
        let mut cur: Option<&str> = None;
        let mut counts = ItemLedger::default();
        let flush = |s: &mut String, file: &str, c: &ItemLedger| {
            let mut parts = vec![format!("{} written", c.written)];
            for (n, what) in [
                (c.unchanged, "already imported"),
                (c.updated, "updated"),
                (c.held, "held"),
                (c.duplicate, "duplicates"),
                (c.collided, "collisions"),
                (c.exists, "exist"),
                (c.failed, "failed"),
            ] {
                if n > 0 {
                    parts.push(format!("{n} {what}"));
                }
            }
            let _ = writeln!(s, "  {file}: {} items -> {}", c.found, parts.join(", "));
        };
        for r in &self.records {
            if cur != Some(r.file.as_str()) {
                if let Some(file) = cur {
                    flush(&mut s, file, &counts);
                }
                cur = Some(r.file.as_str());
                counts = ItemLedger::default();
            }
            counts.found += 1;
            match &r.outcome {
                Outcome::Written { .. } => counts.written += 1,
                Outcome::Unchanged => counts.unchanged += 1,
                Outcome::Updated { .. } => counts.updated += 1,
                Outcome::Held { .. } => counts.held += 1,
                Outcome::Duplicate { .. } => counts.duplicate += 1,
                Outcome::Collision { .. } => counts.collided += 1,
                Outcome::Exists { .. } => counts.exists += 1,
                Outcome::Failed { .. } => counts.failed += 1,
            }
        }
        if let Some(file) = cur {
            flush(&mut s, file, &counts);
        }

        if !self.stale.is_empty() {
            let _ = writeln!(
                s,
                "stale ({}): imported earlier from these files, no longer produced by any item; not erased",
                self.stale.len()
            );
            for st in &self.stale {
                let _ = writeln!(s, "    r{} {} <- {}", st.ring, st.name, st.source);
            }
        }
        if self.dry_run && !self.audit_preview.is_empty() {
            let _ = writeln!(
                s,
                "audit: a real run would append {} row(s)",
                self.audit_preview.len()
            );
        }
        let _ = writeln!(s, "elapsed: {} ms", self.elapsed_ms);

        let code = self.exit_code();
        let verdict = match code {
            0 => "clean: every source item is a note".to_string(),
            1 => format!(
                "NOT CLEAN: {} item(s)/file(s) did not make it and no rule said to skip them (listed above)",
                self.drops() + self.file_failures.len()
            ),
            2 => "ACCOUNTING FAILED: the importer's own ledgers do not close; trust nothing above"
                .to_string(),
            _ => format!(
                "held: {} item(s) wait on the PII gate and nothing else is wrong",
                self.items.held
            ),
        };
        let _ = writeln!(s, "result: {verdict}; exit {code}");
        s
    }
}