diff --git a/docs/design.md b/docs/design.md index 993dae2..1868882 100644 --- a/docs/design.md +++ b/docs/design.md @@ -328,8 +328,9 @@ operation, and nothing acts on the structure besides showing it. Each request, decision, approver, lease use and result is an event with the request's ID. The broker writes it to the journal as a line of text and to `audit.jsonl` in its state directory as a -line of JSON. `slop-approve log [-n 50] [--json]` shows the recent events through the approve -socket, with each request's full detail. Secret operations record the file and the blob ID of the +line of JSON, keeping what approvers were shown with its structure, so the web app's history shows +it as the request did. `slop-approve log [-n 50] [--json]` shows the recent events through the +approve socket, with each request's full detail. Secret operations record the file and the blob ID of the ciphertext they were sent, never values. ## Operations diff --git a/src/audit.rs b/src/audit.rs index efe87db..7d37c4c 100644 --- a/src/audit.rs +++ b/src/audit.rs @@ -10,6 +10,7 @@ use std::time::SystemTime; use anyhow::Result; use serde::{Deserialize, Serialize}; +use crate::detail::{Detail, Section}; use crate::proto::ApprovedBy; /// One line of the audit file. @@ -40,14 +41,14 @@ pub enum Event { requester: String, op: String, summary: String, - detail: String, + detail: Shown, /// Agent-supplied. label: Option, }, /// What approvers are shown once preparing a request (such as building a deploy) finished. Prepared { id: String, - detail: String, + detail: Shown, }, Approved { id: String, @@ -100,6 +101,39 @@ impl Event { } } +/// What approvers were shown of a request. Records from before the broker kept the structure +/// have its text. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum Shown { + Detail(Detail), + Text(String), +} + +impl Shown { + pub fn to_text(&self) -> String { + match self { + Shown::Detail(detail) => detail.to_text(), + Shown::Text(text) => text.clone(), + } + } + + /// The structure, with old records' text as one text section. + pub fn to_detail(&self) -> Detail { + match self { + Shown::Detail(detail) => detail.clone(), + Shown::Text(text) if text.is_empty() => Detail::default(), + Shown::Text(text) => Detail { fields: vec![], sections: vec![Section::text("Text", text)] }, + } + } +} + +impl From for Shown { + fn from(detail: Detail) -> Shown { + Shown::Detail(detail) + } +} + /// Request IDs are long; approvers type a prefix, and logs show the same. pub fn short(id: &str) -> &str { &id[..id.len().min(6)] @@ -115,7 +149,7 @@ impl fmt::Display for Event { } Event::Request { id, requester, summary, detail, label, .. } => { write!(f, "{} {requester} asks: {summary}", short(id))?; - if let Some(line) = detail.lines().next() { + if let Some(line) = detail.to_text().lines().next() { write!(f, "; {line}")?; } if let Some(label) = label { @@ -125,7 +159,7 @@ impl fmt::Display for Event { } Event::Prepared { id, detail } => { write!(f, "{} ready for approval", short(id))?; - if let Some(line) = detail.lines().next() { + if let Some(line) = detail.to_text().lines().next() { write!(f, "; {line}")?; } Ok(()) @@ -165,7 +199,7 @@ impl Record { let time = self.time.replace('T', " ").trim_end_matches('Z').to_owned(); let mut text = format!("{time} {}\n", self.event); if let Event::Request { detail, .. } | Event::Prepared { detail, .. } = &self.event { - for line in detail.lines().skip(1) { + for line in detail.to_text().lines().skip(1) { text += &format!("{:21}{line}\n", ""); } } @@ -248,7 +282,7 @@ mod tests { requester: "slop-cannon".into(), op: "ssh".into(), summary: "ssh river".into(), - detail: "Command: a\nmore".into(), + detail: Shown::Text("Command: a\nmore".into()), label: Some("why".into()), }, }; @@ -258,4 +292,17 @@ mod tests { \x20 more\n" ); } + + #[test] + fn keeps_what_approvers_saw_and_reads_old_text() { + let detail = Detail::code("Command", "uptime"); + let event = Event::Prepared { id: "a".into(), detail: detail.clone().into() }; + let line = serde_json::to_string(&event).unwrap(); + assert_eq!(serde_json::from_str::(&line).unwrap(), event); + + let old: Event = serde_json::from_str(r#"{"event":"prepared","id":"a","detail":"Command: uptime"}"#).unwrap(); + let Event::Prepared { detail: old, .. } = old else { panic!("{old:?}") }; + assert_eq!(old, Shown::Text("Command: uptime".into())); + assert_eq!(old.to_text(), detail.to_text()); + } } diff --git a/src/detail.rs b/src/detail.rs index d3f09cb..b49d835 100644 --- a/src/detail.rs +++ b/src/detail.rs @@ -1,6 +1,6 @@ //! What approvers are shown of a request: fields, then tables, lists and text. Operations build -//! it; every surface renders it. The web app shows the structure, the others [`Detail::to_text`], -//! which is also what the audit log keeps and passkey confirmations fingerprint. +//! it; every surface renders it. The web app shows the structure (also in the audit history, +//! which keeps it), the others [`Detail::to_text`], which passkey confirmations fingerprint. //! //! The structure is the same for every operation, so surfaces don't need to know any of them. //! All of it can carry agent-written text, which every surface makes printable when it shows it. diff --git a/src/server.rs b/src/server.rs index ab92941..5a65672 100644 --- a/src/server.rs +++ b/src/server.rs @@ -195,7 +195,7 @@ async fn process(broker: &Broker, requester: &str, request: Request, io: &mut Io requester: requester.to_owned(), op: parsed.op.to_string(), summary: parsed.summary.clone(), - detail: parsed.detail.to_text(), + detail: parsed.detail.clone().into(), label: label.clone(), }); @@ -239,7 +239,7 @@ async fn process(broker: &Broker, requester: &str, request: Request, io: &mut Io Ok(gate) => gate, Err(error) => return failed(error), }; - log(Event::Prepared { id: id.clone(), detail: gate.detail.to_text() }); + log(Event::Prepared { id: id.clone(), detail: gate.detail.clone().into() }); let (by, approver) = match authorize(broker, &id, requester, &gate, label.clone(), io).await { Ok(Some(approval)) => approval, @@ -263,7 +263,7 @@ async fn process(broker: &Broker, requester: &str, request: Request, io: &mut Io return failed(error); } - log(Event::Prepared { id: id.clone(), detail: parsed.detail.to_text() }); + log(Event::Prepared { id: id.clone(), detail: parsed.detail.clone().into() }); } let (approved_by, approver) = match authorize(broker, &id, requester, &parsed, label, io).await { diff --git a/src/web/history.rs b/src/web/history.rs index b7cce84..83a5428 100644 --- a/src/web/history.rs +++ b/src/web/history.rs @@ -1,7 +1,8 @@ //! The audit log gathered by request: what was asked, who approved it and how it ended. use std::collections::HashMap; -use crate::audit::{Event, Record}; +use crate::audit::{Event, Record, Shown}; +use crate::detail::Detail; use crate::proto::ApprovedBy; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -165,8 +166,8 @@ fn summarize(id: String, records: Vec) -> History { history } -/// One event in a request's timeline: what happened, and any longer text to show with it. -pub fn describe(event: &Event) -> (String, Option) { +/// One event in a request's timeline: what happened, and what approvers were shown with it. +pub fn describe(event: &Event) -> (String, Option) { match event { Event::Unknown { peer } => (format!("Refused a connection from {peer}"), None), Event::Invalid { error, .. } => (format!("Invalid request: {error}"), None), @@ -175,11 +176,9 @@ pub fn describe(event: &Event) -> (String, Option) { if let Some(label) = label { text += &format!(" (label: {label})"); } - (text, Some(detail.clone()).filter(|d| !d.is_empty())) - } - Event::Prepared { detail, .. } => { - ("Prepared for approval".into(), Some(detail.clone()).filter(|d| !d.is_empty())) + (text, shown(detail)) } + Event::Prepared { detail, .. } => ("Prepared for approval".into(), shown(detail)), Event::Approved { by, approver, step, .. } => { let by = match (by, approver) { (ApprovedBy::Policy, _) => "policy".to_owned(), @@ -204,6 +203,10 @@ pub fn describe(event: &Event) -> (String, Option) { } } +fn shown(detail: &Shown) -> Option { + Some(detail.to_detail()).filter(|d| !d.fields.is_empty() || !d.sections.is_empty()) +} + #[cfg(test)] mod tests { use super::*; @@ -218,7 +221,7 @@ mod tests { requester: "slop-cannon".into(), op: "ssh".into(), summary: format!("ssh {id}"), - detail: "Command: uptime".into(), + detail: Detail::code("Command", "uptime").into(), label: None, } } diff --git a/src/web/page.rs b/src/web/page.rs index 0029f8a..f33d9de 100644 --- a/src/web/page.rs +++ b/src/web/page.rs @@ -67,7 +67,7 @@ fn section_titles(detail: &Detail) -> String { detail.sections.iter().map(Section::title).collect::>().join(", ") } -fn describe(record: &Record) -> (String, Option) { +fn describe(record: &Record) -> (String, Option) { history::describe(&record.event) } @@ -270,4 +270,35 @@ mod tests { assert!(html.contains("needs-passkey")); assert!(html.contains("Needs passkey")); } + + #[test] + fn history_shows_what_approvers_saw() { + use crate::audit::{Event, Record, Shown}; + + let mut detail = Detail::code("Command", "uptime"); + detail.section(Section::diff("Diff", "@@ -1 +1 @@\n-b\n+a")); + let records = vec![ + Record { + time: "2026-11-27T10:00:00Z".into(), + event: Event::Request { + id: "abc".into(), + requester: "slop-cannon".into(), + op: "ssh".into(), + summary: "ssh river".into(), + detail: detail.into(), + label: None, + }, + }, + Record { + time: "2026-11-27T10:00:01Z".into(), + event: Event::Prepared { id: "abc".into(), detail: Shown::Text("Old: text".into()) }, + }, + ]; + let (histories, _) = super::history::gather(records); + + let html = request("broker", "token", "abc", None, Some(&histories[0]), 0).0; + assert!(html.contains("
Command
uptime
"), "{html}"); + assert!(html.contains("+a"), "{html}"); + assert!(html.contains("Details (Text)") && html.contains("
Old: text
"), "{html}"); + } } diff --git a/templates/macros.html b/templates/macros.html index 6b43bab..382ad97 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -80,7 +80,7 @@ {% for record in records %} {% let (text, detail) = self::describe(record) %}
  • {% call time(record.time) %}{% endcall %} {{ text }} -{%- if let Some(detail) = detail %}
    Details
    {{ detail }}
    {% endif %}
  • +{%- if let Some(detail) = detail %}{% call detail(detail, false) %}{% endcall %}{% endif %} {% endfor %} {% endmacro %} diff --git a/tests/broker.rs b/tests/broker.rs index f08f145..67dc51f 100644 --- a/tests/broker.rs +++ b/tests/broker.rs @@ -237,7 +237,7 @@ async fn approvers_see_the_whole_command_and_stdin_that_run() { // So is the audit log. let records = broker.audit.tail(10).unwrap(); let slop_broker::audit::Event::Request { detail: logged, .. } = &records[0].event else { panic!("{records:?}") }; - assert_eq!(*logged, detail); + assert_eq!(logged.to_text(), detail); // Multi-line commands are shown as text rather than a one-line field. let mut client = Client::start(&broker, ssh(&["echo one\necho two"])).await;