Skip to content

Commit f3233ed

Browse files
committed
feat(acp): emit activity records and checkpoint on stop
Add an opt-in JSONL activity log that records bounded, redacted tool and lifecycle activity from the observer feed on stderr, so prompts, agent text, and secrets never enter the platform stream. The binary now parses CLI arguments before installing logging because the log format depends on the flag. Add an opt-in stop checkpoint that runs buzz-agent-checkpoint save after sessions drain, including when shutdown cancels startup. It validates the receipt and makes shutdown fail when the checkpoint fails. Signed-off-by: Tee8z <[email protected]>
1 parent a6e1121 commit f3233ed

7 files changed

Lines changed: 672 additions & 40 deletions

File tree

‎crates/buzz-acp/TESTING.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ Make sure `pi` and `buzz-pi-acp` are on PATH, then restart Buzz.
2121
cargo test -p buzz-acp
2222
```
2323

24+
The package suite also checks owner-only thread routing, ACP full-access negotiation,
25+
redacted JSONL activity, and checkpoint failure propagation. Activity tests use the
26+
real ACP client with a synthetic adapter and assert that test secrets never enter
27+
the platform stream. Checkpoint tests cover validated receipts, command failures,
28+
invalid and oversized output, missing helpers, and timeouts.
29+
The image repository's `buzz-agent/test-checkpoint-storage.py` also stops the
30+
packaged harness during startup while an adapter writes files. It checks that
31+
recovery preserves the final edit and rejected uploads make shutdown fail.
32+
2433
Managed agent sessions may already export harness options. Clear them when
2534
running the package suite: three CLI parsing tests assert the unset defaults,
2635
and inherited values would change the inputs those tests exercise. Running the

‎crates/buzz-acp/src/activity.rs‎

Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
//! Bounded, content-minimized activity records for platform log collection.
2+
//!
3+
//! The encrypted observer feed retains its original payload. The platform feed
4+
//! records operation metadata and output sizes; prompts, raw results, credentials,
5+
//! and arbitrary adapter-provided titles never enter this stream.
6+
7+
use std::collections::HashMap;
8+
use std::time::Instant;
9+
10+
use serde_json::{json, Value};
11+
use sha2::{Digest, Sha256};
12+
13+
use crate::observer::ObserverEvent;
14+
15+
const MAX_ACTIVE_TOOLS: usize = 256;
16+
17+
pub(crate) struct ActivityRecorder {
18+
source_id: String,
19+
sequence: u64,
20+
tools: HashMap<(String, String), (Instant, Option<String>)>,
21+
}
22+
23+
impl ActivityRecorder {
24+
pub(crate) fn new() -> Self {
25+
Self {
26+
source_id: uuid::Uuid::new_v4().to_string(),
27+
sequence: 0,
28+
tools: HashMap::new(),
29+
}
30+
}
31+
32+
pub(crate) fn record(&mut self, event: &ObserverEvent) -> Option<Value> {
33+
let mut details = json!({});
34+
let kind = match event.kind.as_str() {
35+
"checkpoint_started" | "checkpoint_failed" => event.kind.as_str(),
36+
"checkpoint_saved" => {
37+
details = event.payload.clone();
38+
event.kind.as_str()
39+
}
40+
"harness_started" | "harness_stopped" | "turn_started" | "turn_completed" => {
41+
if event.kind == "harness_stopped" {
42+
details["unfinished_tools"] = self.tools.len().into();
43+
self.tools.clear();
44+
} else if event.kind == "turn_completed" {
45+
let before = self.tools.len();
46+
self.tools.retain(|_, (_, turn)| turn != &event.turn_id);
47+
details["unfinished_tools"] = (before - self.tools.len()).into();
48+
}
49+
event.kind.as_str()
50+
}
51+
"acp_read" if event.payload["method"] == "session/update" => {
52+
let update = &event.payload["params"]["update"];
53+
let update_kind = update["sessionUpdate"].as_str()?;
54+
if !matches!(update_kind, "tool_call" | "tool_call_update") {
55+
return None;
56+
}
57+
let tool_id = opaque_id(update["toolCallId"].as_str().unwrap_or("missing"));
58+
let key = (
59+
opaque_id(session_id(event).unwrap_or("missing")),
60+
tool_id.clone(),
61+
);
62+
details["tool_id"] = tool_id.into();
63+
details["kind"] = update["kind"]
64+
.as_str()
65+
.filter(|value| {
66+
matches!(
67+
*value,
68+
"read"
69+
| "edit"
70+
| "delete"
71+
| "move"
72+
| "search"
73+
| "execute"
74+
| "think"
75+
| "fetch"
76+
| "switch_mode"
77+
)
78+
})
79+
.unwrap_or("other")
80+
.into();
81+
let status = update["status"]
82+
.as_str()
83+
.filter(|value| {
84+
matches!(*value, "pending" | "in_progress" | "completed" | "failed")
85+
})
86+
.unwrap_or("unknown");
87+
details["status"] = status.into();
88+
details["arguments"] = command_summary(&update["rawInput"]);
89+
details["output"] = json!({
90+
"redacted": true,
91+
"bytes": update.get("content").map(|v| v.to_string().len()).unwrap_or(0),
92+
"exit_code": update["rawOutput"]["exitCode"].as_i64(),
93+
});
94+
if update_kind == "tool_call" {
95+
if self.tools.len() < MAX_ACTIVE_TOOLS {
96+
self.tools
97+
.entry(key.clone())
98+
.or_insert_with(|| (Instant::now(), event.turn_id.clone()));
99+
} else {
100+
details["timing_capacity_exceeded"] = true.into();
101+
}
102+
}
103+
if matches!(status, "completed" | "failed") {
104+
if let Some((start, _)) = self.tools.remove(&key) {
105+
details["duration_ms"] = json!(start.elapsed().as_millis());
106+
}
107+
"tool_completed"
108+
} else if update_kind == "tool_call" {
109+
"tool_started"
110+
} else {
111+
"tool_updated"
112+
}
113+
}
114+
_ => return None,
115+
};
116+
self.sequence += 1;
117+
Some(json!({
118+
"schema": "buzz.activity.v1",
119+
"source_id": self.source_id,
120+
"event_id": format!("{}:{}", self.source_id, self.sequence),
121+
"sequence": self.sequence,
122+
"timestamp": event.timestamp,
123+
"event": kind,
124+
"agent_index": event.agent_index,
125+
"channel_id": event.channel_id.as_deref().map(opaque_id),
126+
"acp_session_id": session_id(event).map(opaque_id),
127+
"turn_id": event.turn_id.as_deref().map(opaque_id),
128+
"details": details,
129+
}))
130+
}
131+
}
132+
133+
fn session_id(event: &ObserverEvent) -> Option<&str> {
134+
event
135+
.session_id
136+
.as_deref()
137+
.or_else(|| event.payload["params"]["sessionId"].as_str())
138+
}
139+
140+
fn opaque_id(value: &str) -> String {
141+
hex::encode(Sha256::digest(value.as_bytes()))
142+
}
143+
144+
fn command_summary(input: &Value) -> Value {
145+
// Log only known command names. Arbitrary arguments may contain secrets,
146+
// SQL statements, environment assignments, or embedded shell expansions.
147+
let command = input["command"].as_str().unwrap_or("");
148+
let mut words = command.split_whitespace();
149+
let program = words
150+
.next()
151+
.filter(|value| {
152+
matches!(
153+
*value,
154+
"cargo"
155+
| "git"
156+
| "go"
157+
| "python"
158+
| "python3"
159+
| "node"
160+
| "npm"
161+
| "pnpm"
162+
| "yarn"
163+
| "just"
164+
| "make"
165+
| "via"
166+
| "gh"
167+
| "psql"
168+
| "sqlite3"
169+
)
170+
})
171+
.unwrap_or("other");
172+
let operation = words
173+
.next()
174+
.filter(|value| {
175+
matches!(
176+
*value,
177+
"build"
178+
| "test"
179+
| "check"
180+
| "clippy"
181+
| "fmt"
182+
| "clone"
183+
| "fetch"
184+
| "commit"
185+
| "push"
186+
| "status"
187+
| "diff"
188+
| "install"
189+
| "run"
190+
| "api"
191+
)
192+
})
193+
.unwrap_or("other");
194+
json!({"program": program, "operation": operation, "remaining_arguments": "[redacted]"})
195+
}
196+
197+
#[cfg(test)]
198+
mod tests {
199+
use super::*;
200+
201+
fn event(payload: Value) -> ObserverEvent {
202+
ObserverEvent {
203+
seq: 1,
204+
timestamp: "2026-10-08T12:00:00Z".into(),
205+
kind: "acp_read".into(),
206+
agent_index: Some(0),
207+
channel_id: Some("channel".into()),
208+
session_id: Some("session".into()),
209+
turn_id: Some("turn".into()),
210+
started_at: None,
211+
payload,
212+
}
213+
}
214+
215+
#[test]
216+
fn tool_records_are_correlated_and_exclude_raw_content() {
217+
let mut recorder = ActivityRecorder::new();
218+
let mut e = event(json!({"method":"session/update","params":{"update":{
219+
"sessionUpdate":"tool_call", "toolCallId":"secret-id", "kind":"execute",
220+
"title":"SECRET-TITLE", "status":"in_progress",
221+
"rawInput":{"command":"cargo test SECRET-ARG", "password":"SECRET-PASSWORD"},
222+
"content":[{"text":"SECRET-RESULT"}]
223+
}}}));
224+
let start = recorder.record(&e).unwrap();
225+
e.payload["params"]["update"]["sessionUpdate"] = "tool_call_update".into();
226+
e.payload["params"]["update"]["status"] = "completed".into();
227+
e.payload["params"]["update"]["rawOutput"] = json!({"exitCode":0});
228+
let finish = recorder.record(&e).unwrap();
229+
assert_eq!(start["event"], "tool_started");
230+
assert_eq!(finish["event"], "tool_completed");
231+
assert_eq!(start["details"]["tool_id"], finish["details"]["tool_id"]);
232+
assert_eq!(finish["sequence"], 2);
233+
assert!(finish["details"]["duration_ms"].is_number());
234+
assert_eq!(finish["details"]["output"]["exit_code"], 0);
235+
assert_eq!(start["details"]["arguments"]["program"], "cargo");
236+
for value in [start, finish] {
237+
let serialized = value.to_string();
238+
assert!(!serialized.contains("SECRET"));
239+
assert!(!serialized.contains("secret-id"));
240+
}
241+
}
242+
243+
#[test]
244+
fn prompt_and_agent_text_are_never_platform_records() {
245+
let mut recorder = ActivityRecorder::new();
246+
let mut e = event(json!({"method":"session/update","params":{"update":{
247+
"sessionUpdate":"agent_message_chunk", "content":{"text":"SECRET"}
248+
}}}));
249+
assert!(recorder.record(&e).is_none());
250+
e.kind = "acp_write".into();
251+
e.payload = json!({"method":"session/prompt","params":{"prompt":"SECRET"}});
252+
assert!(recorder.record(&e).is_none());
253+
}
254+
255+
#[test]
256+
fn active_tool_tracking_is_bounded() {
257+
let mut recorder = ActivityRecorder::new();
258+
let mut last = Value::Null;
259+
for id in 0..=MAX_ACTIVE_TOOLS {
260+
last = recorder
261+
.record(&event(
262+
json!({"method":"session/update","params":{"update":{
263+
"sessionUpdate":"tool_call", "toolCallId":id.to_string()
264+
}}}),
265+
))
266+
.unwrap();
267+
}
268+
assert_eq!(recorder.tools.len(), MAX_ACTIVE_TOOLS);
269+
assert_eq!(last["details"]["timing_capacity_exceeded"], true);
270+
}
271+
272+
#[cfg(unix)]
273+
#[derive(Clone)]
274+
struct Capture(std::sync::Arc<std::sync::Mutex<Vec<u8>>>);
275+
276+
#[cfg(unix)]
277+
impl std::io::Write for Capture {
278+
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
279+
self.0.lock().unwrap().extend_from_slice(bytes);
280+
Ok(bytes.len())
281+
}
282+
fn flush(&mut self) -> std::io::Result<()> {
283+
Ok(())
284+
}
285+
}
286+
287+
#[cfg(unix)]
288+
#[tokio::test]
289+
async fn real_acp_read_loop_emits_only_sanitized_activity_when_enabled() {
290+
use tracing::instrument::WithSubscriber;
291+
let output = Capture(Default::default());
292+
let writer = output.clone();
293+
let subscriber = tracing_subscriber::fmt()
294+
.json()
295+
.with_max_level(tracing::Level::INFO)
296+
.with_writer(move || writer.clone())
297+
.finish();
298+
async {
299+
for enabled in [false, true] {
300+
let script = r#"
301+
read -r request
302+
printf '%s\n' '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"fixture","update":{"sessionUpdate":"tool_call","toolCallId":"tool-1","kind":"execute","status":"in_progress","title":"DO-NOT-LOG-TITLE","rawInput":{"command":"cargo test DO-NOT-LOG-ARG"}}}}'
303+
printf '%s\n' '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"fixture","update":{"sessionUpdate":"tool_call_update","toolCallId":"tool-1","status":"completed","content":[{"text":"DO-NOT-LOG-RESULT"}],"rawOutput":{"exitCode":0}}}}'
304+
printf '%s\n' '{"jsonrpc":"2.0","id":0,"result":{"protocolVersion":2,"agentCapabilities":{},"authMethods":[]}}'
305+
"#;
306+
let mut client = crate::acp::AcpClient::spawn(
307+
"bash", &["-c".into(), script.into()], &[], false,
308+
).await.unwrap();
309+
let observer = if enabled {
310+
crate::observer::ObserverHandle::with_activity_log()
311+
} else {
312+
crate::observer::ObserverHandle::in_process()
313+
};
314+
client.set_observer(Some(observer), 0);
315+
client.initialize().await.unwrap();
316+
client.shutdown().await;
317+
}
318+
}.with_subscriber(subscriber).await;
319+
let text = String::from_utf8(output.0.lock().unwrap().clone()).unwrap();
320+
let records: Vec<Value> = text
321+
.lines()
322+
.filter_map(|line| {
323+
let envelope: Value = serde_json::from_str(line).ok()?;
324+
if envelope["fields"]["schema"] != "buzz.activity.v1" {
325+
return None;
326+
}
327+
Some(serde_json::from_str(envelope["fields"]["record"].as_str()?).unwrap())
328+
})
329+
.collect();
330+
assert_eq!(
331+
records.len(),
332+
2,
333+
"only the enabled adapter emits platform records"
334+
);
335+
assert_eq!(records[0]["event"], "tool_started");
336+
assert_eq!(records[1]["event"], "tool_completed");
337+
assert!(!serde_json::to_string(&records)
338+
.unwrap()
339+
.contains("DO-NOT-LOG"));
340+
}
341+
}

0 commit comments

Comments
 (0)