feat(audit): audit runs and attestations are journaled (P8)

A claim that someone checked the books is evidence the CPA handoff rests on,
so it is a journal fact and replays: audit.run (a programme run through a
day at the books' figure head, with the hashes that make it reproducible and
the two filed workpapers), receipt.attested, entry.reviewed and
period.attested. Behind the journal feature audits, schema 20; no figure
moves. The figure head is the last line that bears on a figure: filing a
workpaper, a run or an attestation never makes an audit stale, an entry,
document, close, settings or role change does, so a period is attested only
on an audit that still describes the books. A receipt reading that differs
from the entry is recorded as disagreeing and never counts; whether a claim
counts is derived against the bar when asked, never stored.

The store also reads what a ledger cannot: the chain, the database against
a replay, stored and extracted files, the access log, duplicate files, and
the backups' standing handed in by the server. Fixture old-books/p8_before
was written by the version before this one and opens to its figures.
period.attested was the tests' placeholder for an unknown event type; they
use ledger.not_yet_written now.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HS8WZziDnyxunC23MUxrLp
This commit is contained in:
swaitsandClaude Fable 5.1 committed 2026-10-29 15:54:08 -06:00
1 parent 843bcc421f
commit 6b7dccd1ee
19 files changed
+2479 -18

No files matched your search

+192
View File
@@ -729,6 +729,198 @@ pub fn draft_discarded_of(p: &Value) -> Result<(u64, String), StoreError> {
Ok((u64_of(p, E, "id")?, str_of(p, E, "reason")?))
}
// ---------------------------------------------------------------------------
// Audits and attestations (plan P8, feature `audits`)
// ---------------------------------------------------------------------------
/// A method as journaled: only the parts the client gave.
pub fn method_json(m: &ledger_core::audit::Method) -> Value {
let mut o = Map::new();
put(&mut o, "model", m.model.clone());
put(
&mut o,
"reasoning_effort",
m.reasoning_effort.map(|e| e.key()),
);
put(&mut o, "adversarial", m.adversarial);
put(&mut o, "verifier_agents", m.verifier_agents);
Value::Object(o)
}
/// Read a method. A key or an effort this version doesn't know is newer
/// when a line is at hand (B-27), else corrupt.
fn method_of(
line: Option<&crate::journal::JournalLine>,
m: &Value,
event: &str,
) -> Result<ledger_core::audit::Method, StoreError> {
let o = m.as_object().ok_or_else(|| corrupt(event, "method"))?;
let unknown = |what: String| match line {
Some(l) => crate::format::newer_content(l, what),
None => corrupt(event, "method"),
};
let mut out = ledger_core::audit::Method::default();
for (k, v) in o {
match k.as_str() {
"model" => {
out.model = Some(
v.as_str()
.ok_or_else(|| corrupt(event, "method.model"))?
.to_string(),
)
}
"reasoning_effort" => {
let s = v
.as_str()
.ok_or_else(|| corrupt(event, "method.reasoning_effort"))?;
out.reasoning_effort = Some(
ledger_core::audit::Effort::parse(s)
.ok_or_else(|| unknown(format!("the reasoning effort {s:?}")))?,
);
}
"adversarial" => {
out.adversarial = Some(
v.as_bool()
.ok_or_else(|| corrupt(event, "method.adversarial"))?,
)
}
"verifier_agents" => {
out.verifier_agents = Some(
v.as_u64()
.and_then(|n| u32::try_from(n).ok())
.ok_or_else(|| corrupt(event, "method.verifier_agents"))?,
)
}
other => return Err(unknown(format!("the method key {other:?}"))),
}
}
Ok(out)
}
/// A method read back from the projection.
pub fn method_from_value(m: &Value) -> Result<ledger_core::audit::Method, StoreError> {
method_of(None, m, "attestations")
}
/// `audit.run {id, through, through_seq, scope, program_version, tb_hash,
/// findings_sha256, workpaper_attachment?, workpaper_pdf?}`.
pub fn audit_run(r: &crate::audit::AuditRun) -> Value {
let mut o = Map::new();
o.insert("id".into(), json!(r.id));
o.insert("through".into(), json!(r.through.to_string()));
o.insert("through_seq".into(), json!(r.through_seq));
o.insert("scope".into(), json!(r.scope.key()));
o.insert("program_version".into(), json!(r.program_version));
o.insert("tb_hash".into(), json!(r.tb_hash));
o.insert("findings_sha256".into(), json!(r.findings_sha256));
put(&mut o, "workpaper_attachment", r.workpaper_attachment);
put(&mut o, "workpaper_pdf", r.workpaper_pdf);
Value::Object(o)
}
/// Read an `audit.run`. A scope this version doesn't know is newer.
pub fn audit_run_of(
line: &crate::journal::JournalLine,
) -> Result<crate::audit::AuditRun, StoreError> {
const E: &str = "audit.run";
let p = &line.payload;
let scope_key = str_of(p, E, "scope")?;
let scope = ledger_core::audit::Scope::parse(&scope_key).ok_or_else(|| {
crate::format::newer_content(line, format!("the audit scope {scope_key:?}"))
})?;
Ok(crate::audit::AuditRun {
id: u64_of(p, E, "id")?,
through: date_of(&str_of(p, E, "through")?, E, "through")?,
through_seq: u64_of(p, E, "through_seq")?,
scope,
program_version: str_of(p, E, "program_version")?,
tb_hash: str_of(p, E, "tb_hash")?,
findings_sha256: str_of(p, E, "findings_sha256")?,
workpaper_attachment: opt_u64(p, E, "workpaper_attachment")?,
workpaper_pdf: opt_u64(p, E, "workpaper_pdf")?,
seq: line.seq,
})
}
/// The payload of `receipt.attested`, `entry.reviewed` or `period.attested`.
pub fn attestation(a: &crate::audit::Attestation) -> Value {
use crate::audit::Attested;
let mut o = Map::new();
match &a.what {
Attested::Receipt {
attachment_id,
entry_id,
amount_checked,
date_checked,
disagrees,
} => {
o.insert("attachment_id".into(), json!(attachment_id));
o.insert("entry_id".into(), json!(entry_id));
o.insert("amount_checked".into(), json!(amount_checked.to_string()));
o.insert("date_checked".into(), json!(date_checked.to_string()));
o.insert("disagrees".into(), json!(disagrees));
}
Attested::Entry { entry_id } => {
o.insert("entry_id".into(), json!(entry_id));
}
Attested::Period {
period_id,
audit_run_id,
} => {
o.insert("period_id".into(), json!(period_id));
o.insert("audit_run_id".into(), json!(audit_run_id));
}
}
o.insert("by".into(), json!(a.by));
o.insert("at".into(), json!(a.at));
o.insert("method".into(), method_json(&a.method));
put(&mut o, "note", a.note.clone());
Value::Object(o)
}
/// Read one of the three attestation events by its line.
pub fn attestation_of(
line: &crate::journal::JournalLine,
) -> Result<crate::audit::Attestation, StoreError> {
use crate::audit::Attested;
let p = &line.payload;
let e = line.line_type.as_str();
let what = match e {
"receipt.attested" => Attested::Receipt {
attachment_id: u64_of(p, e, "attachment_id")?,
entry_id: u64_of(p, e, "entry_id")?,
amount_checked: str_of(p, e, "amount_checked")?
.parse()
.map_err(|_| corrupt(e, "amount_checked"))?,
date_checked: date_of(&str_of(p, e, "date_checked")?, e, "date_checked")?,
disagrees: p
.get("disagrees")
.and_then(Value::as_bool)
.ok_or_else(|| corrupt(e, "disagrees"))?,
},
"entry.reviewed" => Attested::Entry {
entry_id: u64_of(p, e, "entry_id")?,
},
"period.attested" => Attested::Period {
period_id: u64_of(p, e, "period_id")?,
audit_run_id: u64_of(p, e, "audit_run_id")?,
},
_ => return Err(crate::format::unknown_type(line)),
};
Ok(crate::audit::Attestation {
seq: line.seq,
by: str_of(p, e, "by")?,
at: str_of(p, e, "at")?,
method: method_of(
Some(line),
p.get("method").ok_or_else(|| corrupt(e, "method"))?,
e,
)?,
note: opt_str(p, e, "note")?,
what,
})
}
#[cfg(test)]
mod tests {
use super::*;