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
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
//! The compliance screen (SPEC §12, §13.4): egress register with live state, the audit
//! log, PII findings and pending holds, the retention queue and its dry-run-first apply,
//! the model card, and subject access.

use super::error::{ApiError, ApiResult};
use super::extract::ApiQuery;
use super::notes::{Tree, forget_report, rel_path, render_findings};
use super::wire::{
    AuditPage, AuditParams, AuditRow, DryRun, EgressPath, EgressRegister, ModelCard, NoteRef,
    PiiReport, PiiReportEntry, RetentionApplyReport, RetentionApplyRequest, RetentionEntry,
    RetentionQueue, Searched, Skipped, SubjectAccessHit, SubjectAccessReport, SubjectParams,
};
use super::{ServeState, blocking};
use crate::app::App;
use axum::Json;
use axum::body::Bytes;
use axum::extract::State;
use cyberbrain_core::{Citation, EgressPurpose, NoteId, PiiState, Ring};
use cyberbrain_policy::egress::{Destination, Locality, purpose_name};
use cyberbrain_policy::{AuditEvent, AuditFilter, RetentionStatus};
use serde_json::Value;
use std::sync::Arc;

// ---------------------------------------------------------------------------------------
// egress

/// types.ts `EndpointClass`. The register's own vocabulary has one more class, overlay
/// (`100.64.0.0/10`), which needs its own consent flag exactly like a public address does,
/// so it is shown as public here and the `state` text says which it really is.
pub fn endpoint_class(url: &str) -> &'static str {
    match Destination::parse(url) {
        Ok(d) => match d.literal_locality() {
            Some(Locality::Loopback) => "loopback",
            Some(Locality::Private) => "private",
            Some(Locality::Overlay) | Some(Locality::Public) => "public",
            Some(Locality::NotUnicast) | None => "unresolved",
        },
        Err(_) => "unresolved",
    }
}

fn detail_u64(e: &AuditEvent, key: &str) -> u64 {
    e.detail.get(key).and_then(Value::as_u64).unwrap_or(0)
}

/// The obligation catalogue of the active profile. Straight from the library: the wire
/// shape is the library's `Obligation`, because inventing a second one here is how a field
/// ends up meaning two things.
pub async fn obligations(
    State(st): State<Arc<ServeState>>,
) -> ApiResult<Json<crate::app::ObligationsView>> {
    blocking(move || Ok(Json(st.app.policy_obligations()))).await
}

pub async fn egress(State(st): State<Arc<ServeState>>) -> ApiResult<Json<EgressRegister>> {
    blocking(move || {
        let app = &st.app;
        let cfg = app.config();
        let log = app.policy().audit();
        let all = log.read(&AuditFilter::default())?;
        let since = all
            .first()
            .map(|e| e.ts)
            .unwrap_or_else(jiff::Timestamp::now);
        let refused_total = all.iter().filter(|e| e.action == "policy.refusal").count();
        let mut paths = Vec::new();
        let mut register_text = String::new();
        for entry in app.policy_egress() {
            let name = purpose_name(entry.purpose);
            let prefix = format!("{name}:");
            let permitted: Vec<&AuditEvent> = all
                .iter()
                .filter(|e| e.action == "egress.permitted" && e.subject.starts_with(&prefix))
                .collect();
            let bytes_out_total: u64 = all
                .iter()
                .filter(|e| e.action == "egress.completed" && e.subject.starts_with(&prefix))
                .map(|e| detail_u64(e, "bytes_out"))
                .sum();
            let destination = match entry.purpose {
                EgressPurpose::ModelDownload => cfg
                    .embedding
                    .model_source
                    .clone()
                    .unwrap_or_else(|| "none configured".into()),
                EgressPurpose::LocalInference => cfg.inference.base_url.clone(),
                EgressPurpose::AuditSync | EgressPurpose::NoteSync | EgressPurpose::NoteErasure => {
                    cfg.hub.url.clone().unwrap_or_else(|| "not enrolled".into())
                }
                EgressPurpose::HubEnrolment => {
                    "the address in a fleet invitation, when one is enrolled".to_string()
                }
                // No configured destination, and that is the fact about it: where a
                // terminal connects is decided by whoever is typing in it.
                EgressPurpose::Terminal => "wherever you point it".to_string(),
            };
            register_text.push_str(&format!("{name}|{}|{}\n", entry.data, entry.requires));
            paths.push(EgressPath {
                purpose: entry.purpose,
                description: entry.purpose.describe(),
                destination_class: endpoint_class(&destination),
                destination,
                data: entry.data,
                permitted_by: entry.permitted_by.to_vec(),
                enabled: entry.enabled,
                disabled_reason: (!entry.enabled).then(|| entry.state.clone()),
                last_used: permitted.last().map(|e| e.ts),
                uses_total: permitted.len(),
                bytes_out_total,
                state: entry.state,
                carries_note_content: entry.carries_note_content,
            });
        }
        // blake3 over the compile-time register, through core's own hasher: the statement
        // "purposes not on this list do not exist in the binary" as a checkable token.
        let register_hash = format!(
            "b3:{}",
            Citation::new(Ring::Invariant, NoteId::nil(), 0, &register_text).hex()
        );
        Ok(Json(EgressRegister {
            profile: app.policy().profile(),
            since,
            paths,
            refused_total,
            register_hash,
        }))
    })
    .await
}

// ---------------------------------------------------------------------------------------
// audit

/// types.ts names audit actions in its own vocabulary (`write`, `erase`, ...); the log
/// speaks the policy crate's (`note.write`, `note.erase.completed`, ...). A filter in
/// either vocabulary selects the right rows; the rows themselves carry the real names.
fn action_matches(filter: &str, e: &AuditEvent) -> bool {
    let a = e.action.as_str();
    match filter {
        "write" => a.starts_with("note.write"),
        "erase" => a.starts_with("note.erase"),
        "scan" => a.starts_with("index.") || a == "store.init",
        "model-download" => a.starts_with("egress.") && e.subject.starts_with("model-download:"),
        "inference" => a == "inference.call",
        "policy-refusal" | "egress-refused" => a == "policy.refusal",
        "egress-attempt" => a.starts_with("egress."),
        "retention-apply" => a == "retention.expired",
        "hold-resolved" => a == "note.write.resolved" || a == "note.write.discarded",
        "subject-access" => a == "subject.access",
        other => a == other || a.starts_with(&format!("{other}.")),
    }
}

/// `detail` without the chain block, flattened to one level: nested values become their
/// JSON text, which is what types.ts's `Record<string, scalar>` can carry.
fn flat_detail(e: &AuditEvent) -> serde_json::Map<String, Value> {
    let mut out = serde_json::Map::new();
    if let Value::Object(m) = e.detail_without_chain() {
        for (k, v) in m {
            out.insert(
                k,
                match v {
                    Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => v,
                    other => Value::String(other.to_string()),
                },
            );
        }
    }
    out
}

pub async fn audit(
    State(st): State<Arc<ServeState>>,
    ApiQuery(p): ApiQuery<AuditParams>,
) -> ApiResult<Json<AuditPage>> {
    let limit = p.limit.unwrap_or(100).clamp(1, 1000);
    blocking(move || {
        let log = st.app.policy().audit();
        // `seq` is the row's position in the append-only log. The store's own sequence is
        // AUTOINCREMENT with deletes refused by trigger, so the two are the same number.
        let all = log.read(&AuditFilter::default())?;
        let q = p.q.as_deref().map(str::to_lowercase).filter(|s| !s.is_empty());
        let actor = p.actor.as_deref().filter(|s| !s.is_empty());
        let action = p.action.as_deref().filter(|s| !s.is_empty());
        let mut rows: Vec<AuditRow> = all
            .iter()
            .enumerate()
            .rev()
            .filter(|(_, e)| action.is_none_or(|a| action_matches(a, e)))
            .filter(|(_, e)| actor.is_none_or(|a| e.actor.starts_with(a)))
            .filter(|(_, e)| {
                q.as_deref().is_none_or(|q| {
                    format!("{} {} {} {}", e.actor, e.action, e.subject, e.detail)
                        .to_lowercase()
                        .contains(q)
                })
            })
            .map(|(i, e)| AuditRow {
                seq: i + 1,
                ts: e.ts,
                actor: e.actor.clone(),
                action: e.action.clone(),
                subject: e.subject.clone(),
                detail: flat_detail(e),
            })
            .collect();
        // The CLI's rule (App::policy_audit): an action that matches nothing in a
        // non-empty log is refused with the names that are there, because "no such name"
        // and "that never happened" are opposite answers to the same empty table.
        if rows.is_empty()
            && let Some(a) = action
            && !all.is_empty()
            && q.is_none()
            && actor.is_none()
        {
            let mut present: Vec<&str> = all.iter().map(|e| e.action.as_str()).collect();
            present.sort_unstable();
            present.dedup();
            return Err(ApiError::bad_request(format!(
                "no audit action named {a:?}; the log contains: {}. Refused rather than answered with an empty table: a missing name and a thing that never happened are different answers",
                present.join(", ")
            )));
        }
        let total = rows.len();
        if let Some(b) = p.before {
            rows.retain(|r| r.seq < b);
        }
        let more = rows.len() > limit;
        rows.truncate(limit);
        let next_before = if more { rows.last().map(|r| r.seq) } else { None };
        Ok(Json(AuditPage {
            rows,
            total,
            next_before,
        }))
    })
    .await
}

// ---------------------------------------------------------------------------------------
// pii

pub async fn pii(State(st): State<Arc<ServeState>>) -> ApiResult<Json<PiiReport>> {
    blocking(move || {
        let tree = Tree::load(&st.app)?;
        let scan_enabled = st.app.policy().status().pii_scan_active;
        let mut entries = Vec::new();
        for n in &tree.notes {
            let f = &n.note.front;
            if f.pii == PiiState::None {
                continue;
            }
            let findings = cyberbrain_policy::pii::scan(&n.note.body);
            entries.push(PiiReportEntry {
                note: NoteRef {
                    id: f.id,
                    name: f.name.clone(),
                    ring: f.ring,
                },
                state: f.pii,
                findings: render_findings(&n.note.body, &findings),
                // The state is stamped by the write that set it; the frontmatter keeps no
                // separate review timestamp, so the write's is the nearest true thing.
                reviewed_at: f.pii.was_scanned().then_some(f.updated),
            });
        }
        entries.sort_by(|a, b| {
            let rank = |s: PiiState| match s {
                PiiState::Flagged => 0,
                PiiState::Reviewed => 1,
                PiiState::Unscanned => 2,
                PiiState::None => 3,
            };
            rank(a.state)
                .cmp(&rank(b.state))
                .then(a.note.name.cmp(&b.note.name))
        });
        Ok(Json(PiiReport {
            scan_enabled,
            entries,
            holds: st.holds.list(),
        }))
    })
    .await
}

// ---------------------------------------------------------------------------------------
// retention

fn queue_of(app: &App, apply: bool, dry_run: bool) -> ApiResult<crate::app::RetentionReport> {
    Ok(app.policy_retention(apply, dry_run)?)
}

pub async fn retention(State(st): State<Arc<ServeState>>) -> ApiResult<Json<RetentionQueue>> {
    blocking(move || {
        let r = queue_of(&st.app, false, false)?;
        let entries = r
            .queue
            .items
            .iter()
            .map(|i| {
                let (expires_at, due, invalid) = match &i.status {
                    RetentionStatus::Due { expired_at } => (*expired_at, true, None),
                    RetentionStatus::Pending { expires_at } => (*expires_at, false, None),
                    RetentionStatus::Invalid { reason } => (i.created, false, Some(reason.clone())),
                };
                RetentionEntry {
                    note: NoteRef {
                        id: i.note_id,
                        name: i.name.clone(),
                        ring: i.ring,
                    },
                    retention: i.retention.clone(),
                    expires_at,
                    due,
                    invalid,
                }
            })
            .collect();
        Ok(Json(RetentionQueue {
            entries,
            due: r.queue.due,
            indefinite: r.queue.indefinite,
        }))
    })
    .await
}

pub async fn retention_apply(
    State(st): State<Arc<ServeState>>,
    ApiQuery(dry): ApiQuery<DryRun>,
    body: Bytes,
) -> ApiResult<Json<RetentionApplyReport>> {
    let req: RetentionApplyRequest = if body.iter().all(u8::is_ascii_whitespace) {
        RetentionApplyRequest::default()
    } else {
        serde_json::from_slice(&body)
            .map_err(|e| ApiError::bad_request(format!("request body: {e}")))?
    };
    if let Some(names) = &req.names
        && !names.is_empty()
    {
        // The sweep is one path (SPEC §12.5, §8.2) and it erases what is due, all of it;
        // a subset would need its own path with its own audit shape. Refused rather than
        // quietly widened to everything.
        return Err(ApiError::bad_request(
            "applying retention to a subset (`names`) is not supported: the sweep erases every due note, and a partial sweep is not what the audit row `retention.expired` records; omit `names` to run the sweep, or use DELETE /api/v1/notes/{name} for a single note",
        ));
    }
    blocking(move || {
        let r = queue_of(&st.app, true, dry.is_on())?;
        let root = st.app.root().to_path_buf();
        let mut removed = Vec::new();
        let mut skipped = Vec::new();
        for a in r.applied {
            match a.result {
                Ok(rep) => removed.push(forget_report(rep, rel_path(&root, &a.item.path))),
                Err(reason) => skipped.push(Skipped {
                    name: a.name,
                    reason,
                }),
            }
        }
        for i in &r.queue.items {
            if let RetentionStatus::Invalid { reason } = &i.status {
                skipped.push(Skipped {
                    name: i.name.clone(),
                    reason: format!("retention is invalid and was not applied: {reason}"),
                });
            }
        }
        for u in r.unreadable {
            skipped.push(Skipped {
                name: u.clone(),
                reason: "unreadable note; not evaluated".into(),
            });
        }
        Ok(Json(RetentionApplyReport {
            dry_run: r.dry_run,
            removed,
            skipped,
        }))
    })
    .await
}

// ---------------------------------------------------------------------------------------
// model card

pub async fn model_cards(State(st): State<Arc<ServeState>>) -> ApiResult<Json<Vec<ModelCard>>> {
    blocking(move || {
        let report = st.app.policy_model_card();
        let cards = report
            .cards
            .into_iter()
            .map(|c| {
                let mut limitations = c.limitations.clone();
                limitations.extend(c.notes.iter().cloned());
                ModelCard {
                    role: c.role,
                    name: c.name,
                    source: c.source,
                    license: c.licence.unwrap_or_else(|| "not stated".into()),
                    hash: c.blake3,
                    format: c.format,
                    dim: c.dimension,
                    pooling: c.pooling,
                    bytes: c.size_bytes,
                    intended_use: c.intended_use,
                    limitations: limitations.join("; "),
                    // The hash is verified on every load, but no timestamp of that load is
                    // kept anywhere; `null` rather than "now", which would be a claim.
                    verified_at: None,
                    active: true,
                }
            })
            .collect();
        Ok(Json(cards))
    })
    .await
}

// ---------------------------------------------------------------------------------------
// subject access

pub async fn subject(
    State(st): State<Arc<ServeState>>,
    ApiQuery(p): ApiQuery<SubjectParams>,
) -> ApiResult<Json<SubjectAccessReport>> {
    let q =
        p.q.map(|q| q.trim().to_string())
            .filter(|q| !q.is_empty())
            .ok_or_else(|| ApiError::bad_request("`q` (the identifier) must not be empty"))?;
    blocking(move || {
        let r = st.app.policy_subject(&q)?;
        let tree = Tree::load(&st.app)?;
        let audit_rows = st.app.policy().audit().read(&AuditFilter::default())?.len();
        let mut hits = Vec::new();
        for h in &r.note_hits {
            hits.push(SubjectAccessHit {
                r#where: "block",
                r#ref: h.note_name.clone(),
                citation: Some(h.citation.clone()),
                excerpt: h.excerpt.clone(),
            });
        }
        for a in &r.audit_hits {
            hits.push(SubjectAccessHit {
                r#where: "audit",
                r#ref: format!("audit@{}", a.ts),
                citation: None,
                excerpt: format!(
                    "{} {} {} {}{}",
                    a.ts,
                    a.actor,
                    a.action,
                    a.subject,
                    if a.by_hash {
                        " (earlier request for this identifier)"
                    } else {
                        ""
                    }
                ),
            });
        }
        let mut caveats = r.caveats.clone();
        if r.trimmed > 0 {
            caveats.push(format!(
                "{} block(s) the index proposed did not contain the identifier and were dropped",
                r.trimmed
            ));
        }
        Ok(Json(SubjectAccessReport {
            identifier: r.identifier,
            hits,
            searched: Searched {
                notes: tree.notes.len(),
                blocks: tree.total_blocks(),
                audit_rows,
            },
            caveats,
            response_deadline: r.response_deadline,
        }))
    })
    .await
}