Skip to content

Commit 71265ca

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw
authored
feat(nips,relay,acp): NIP-AM durable encrypted agent turn metrics (kind 44200) (#1441)
Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co>
1 parent 8d64887 commit 71265ca

26 files changed

Lines changed: 3367 additions & 40 deletions

File tree

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

Lines changed: 184 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use tokio::process::{Child, ChildStdin, ChildStdout};
1414
use tokio_util::codec::{FramedRead, LinesCodec, LinesCodecError};
1515

1616
use crate::observer::{ObserverContext, ObserverHandle};
17+
use crate::usage::{TurnUsage, UsageTracker};
1718

1819
/// Maximum allowed size of a single NDJSON line from the agent's stdout.
1920
/// Lines exceeding this limit are rejected to prevent OOM from rogue agents.
@@ -167,6 +168,11 @@ pub struct AcpClient {
167168
/// outside of a goose-native turn — the read loop's steer arm is
168169
/// disabled in that case.
169170
steer_rx: Option<tokio::sync::mpsc::Receiver<crate::pool::SteerRequest>>,
171+
/// Usage tracker — accumulates cumulative token counts from
172+
/// `_goose/unstable/session/update` notifications and computes per-turn
173+
/// deltas. Both goose and buzz-agent emit this notification; goose gates
174+
/// on client capability advertisement, buzz-agent emits unconditionally.
175+
goose_usage: UsageTracker,
170176
}
171177

172178
impl AcpClient {
@@ -258,6 +264,7 @@ impl AcpClient {
258264
observer_context: ObserverContext::default(),
259265
active_run_id: None,
260266
steer_rx: None,
267+
goose_usage: UsageTracker::default(),
261268
})
262269
}
263270

@@ -303,7 +310,16 @@ impl AcpClient {
303310
// on ACP v2 ahead of the upstream ACP RFD. Revisit when that RFD merges.
304311
let params = serde_json::json!({
305312
"protocolVersion": 2,
306-
"clientCapabilities": {},
313+
"clientCapabilities": {
314+
// Signal to goose that we handle `_goose/unstable/session/update`
315+
// notifications. Without this the custom notification is suppressed
316+
// on goose's side and usage data is never emitted.
317+
"_meta": {
318+
"goose": {
319+
"customNotifications": true
320+
}
321+
}
322+
},
307323
"clientInfo": {
308324
"name": "buzz-acp",
309325
"version": env!("CARGO_PKG_VERSION")
@@ -427,6 +443,11 @@ impl AcpClient {
427443
let hard_deadline = tokio::time::Instant::now() + max_duration;
428444
self.current_hard_deadline = Some(hard_deadline);
429445

446+
// Mark the usage tracker as in-flight for this turn BEFORE sending the
447+
// prompt so that any setup notifications recorded earlier are not
448+
// misattributed to this turn.
449+
self.goose_usage.begin_turn(session_id);
450+
430451
self.last_prompt_id = Some(self.next_id);
431452
let id = self.next_id;
432453
self.next_id += 1;
@@ -502,6 +523,20 @@ impl AcpClient {
502523
self.active_run_id.as_deref()
503524
}
504525

526+
/// Consume and return the per-turn usage record computed from the most
527+
/// recent `_goose/unstable/session/update` notification.
528+
///
529+
/// Returns `None` if no usage update arrived since the last call (i.e.
530+
/// the harness did not emit one for this turn, or this is not a goose
531+
/// agent). Must be called at most once per turn; subsequent calls return
532+
/// `None` until the next `usage_update` notification is recorded.
533+
///
534+
/// Intended for consumption by `publish_agent_turn_metric` in `pool.rs` to
535+
/// publish a kind 44200 NIP-AM event.
536+
pub fn take_turn_usage(&mut self) -> Option<TurnUsage> {
537+
self.goose_usage.take()
538+
}
539+
505540
/// Install a per-turn steer request channel for goose-native
506541
/// non-cancelling mid-turn delivery.
507542
///
@@ -840,6 +875,9 @@ impl AcpClient {
840875
"session/update" => {
841876
let _ = self.handle_session_update(&msg);
842877
}
878+
"_goose/unstable/session/update" => {
879+
self.handle_goose_usage_update(&msg);
880+
}
843881
"session/request_permission" => {
844882
self.handle_permission_request(&msg).await?;
845883
}
@@ -1170,6 +1208,9 @@ impl AcpClient {
11701208
idle_deadline = Instant::now() + idle_timeout;
11711209
}
11721210
}
1211+
"_goose/unstable/session/update" => {
1212+
self.handle_goose_usage_update(&msg);
1213+
}
11731214
"session/request_permission" => {
11741215
self.handle_permission_request(&msg).await?;
11751216
}
@@ -1311,6 +1352,46 @@ impl AcpClient {
13111352
}
13121353
}
13131354

1355+
/// Parse a `_goose/unstable/session/update` notification and record the
1356+
/// usage snapshot in the per-session tracker.
1357+
///
1358+
/// Silently ignores malformed or non-`usage_update` variants — the
1359+
/// notification is best-effort observability data, not a protocol
1360+
/// requirement. Failures are logged at debug level.
1361+
fn handle_goose_usage_update(&mut self, msg: &serde_json::Value) {
1362+
use crate::usage::{GooseSessionUpdateNotification, GooseSessionUpdateVariant};
1363+
let params = match msg.get("params") {
1364+
Some(p) => p,
1365+
None => {
1366+
tracing::debug!(
1367+
target: "acp::usage",
1368+
"_goose/unstable/session/update: missing params"
1369+
);
1370+
return;
1371+
}
1372+
};
1373+
match serde_json::from_value::<GooseSessionUpdateNotification>(params.clone()) {
1374+
Ok(notif) => {
1375+
if let GooseSessionUpdateVariant::UsageUpdate(payload) = &notif.update {
1376+
tracing::debug!(
1377+
target: "acp::usage",
1378+
session_id = %notif.session_id,
1379+
input = payload.accumulated_input_tokens,
1380+
output = payload.accumulated_output_tokens,
1381+
"goose usage update"
1382+
);
1383+
self.goose_usage.record(&notif.session_id, payload);
1384+
}
1385+
}
1386+
Err(e) => {
1387+
tracing::debug!(
1388+
target: "acp::usage",
1389+
"_goose/unstable/session/update: deserialization error: {e}"
1390+
);
1391+
}
1392+
}
1393+
}
1394+
13141395
/// Auto-approve a `session/request_permission` request from the agent.
13151396
///
13161397
/// Finds the option with `kind == "allow_once"` and responds with its `optionId`.
@@ -1782,7 +1863,13 @@ mod tests {
17821863
"method": "initialize",
17831864
"params": {
17841865
"protocolVersion": 2,
1785-
"clientCapabilities": {},
1866+
"clientCapabilities": {
1867+
"_meta": {
1868+
"goose": {
1869+
"customNotifications": true
1870+
}
1871+
}
1872+
},
17861873
"clientInfo": {
17871874
"name": "buzz-acp",
17881875
"version": "0.1.0"
@@ -1795,6 +1882,11 @@ mod tests {
17951882
Some("buzz-acp")
17961883
);
17971884
assert!(msg["params"]["clientCapabilities"].is_object());
1885+
assert_eq!(
1886+
msg["params"]["clientCapabilities"]["_meta"]["goose"]["customNotifications"].as_bool(),
1887+
Some(true),
1888+
"goose customNotifications capability must be advertised"
1889+
);
17981890
}
17991891

18001892
#[test]
@@ -2825,4 +2917,94 @@ mod tests {
28252917
other => panic!("expected SteerAck::Success, got {other:?}"),
28262918
}
28272919
}
2920+
2921+
// ── Goose usage notification integration ──────────────────────────────
2922+
2923+
/// Build a `_goose/unstable/session/update` JSON-RPC notification.
2924+
fn goose_usage_update_msg(
2925+
session_id: &str,
2926+
input: u64,
2927+
output: u64,
2928+
cost: Option<f64>,
2929+
) -> serde_json::Value {
2930+
let mut update = serde_json::json!({
2931+
"sessionUpdate": "usage_update",
2932+
"used": input + output,
2933+
"contextLimit": 200000u64,
2934+
"accumulatedInputTokens": input,
2935+
"accumulatedOutputTokens": output,
2936+
});
2937+
if let Some(c) = cost {
2938+
update["accumulatedCost"] = serde_json::json!(c);
2939+
}
2940+
serde_json::json!({
2941+
"jsonrpc": "2.0",
2942+
"method": "_goose/unstable/session/update",
2943+
"params": {
2944+
"sessionId": session_id,
2945+
"update": update
2946+
}
2947+
})
2948+
}
2949+
2950+
#[tokio::test]
2951+
async fn goose_usage_notification_recorded_and_take_returns_usage() {
2952+
let mut client = spawn_inert_client().await;
2953+
assert!(client.take_turn_usage().is_none(), "starts empty");
2954+
2955+
// begin_turn before sending the prompt — mirrors the real call flow.
2956+
client.goose_usage.begin_turn("s1");
2957+
let msg = goose_usage_update_msg("s1", 1000, 200, Some(0.01));
2958+
client.handle_goose_usage_update(&msg);
2959+
2960+
let usage = client
2961+
.take_turn_usage()
2962+
.expect("usage should be present after notification");
2963+
assert_eq!(usage.session_id, "s1");
2964+
assert_eq!(usage.turn_seq, 1);
2965+
assert!(!usage.delta_reliable, "first turn must be unreliable");
2966+
assert_eq!(usage.cumulative_input_tokens, 1000);
2967+
assert_eq!(usage.cumulative_output_tokens, 200);
2968+
assert_eq!(usage.cumulative_cost_usd, Some(0.01));
2969+
2970+
// Second take must be None.
2971+
assert!(
2972+
client.take_turn_usage().is_none(),
2973+
"take after drain is None"
2974+
);
2975+
}
2976+
2977+
#[tokio::test]
2978+
async fn goose_usage_second_turn_delta_reliable() {
2979+
let mut client = spawn_inert_client().await;
2980+
// Turn 1.
2981+
client.goose_usage.begin_turn("s2");
2982+
client.handle_goose_usage_update(&goose_usage_update_msg("s2", 1000, 200, None));
2983+
let _ = client.take_turn_usage();
2984+
// Turn 2.
2985+
client.goose_usage.begin_turn("s2");
2986+
client.handle_goose_usage_update(&goose_usage_update_msg("s2", 1800, 450, None));
2987+
let usage = client.take_turn_usage().expect("turn 2 usage");
2988+
assert!(usage.delta_reliable);
2989+
assert_eq!(usage.turn_input_tokens, Some(800));
2990+
assert_eq!(usage.turn_output_tokens, Some(250));
2991+
}
2992+
2993+
#[tokio::test]
2994+
async fn goose_usage_malformed_notification_does_not_panic() {
2995+
let mut client = spawn_inert_client().await;
2996+
// Missing params entirely.
2997+
let bad = serde_json::json!({"jsonrpc":"2.0","method":"_goose/unstable/session/update"});
2998+
client.handle_goose_usage_update(&bad);
2999+
assert!(client.take_turn_usage().is_none());
3000+
3001+
// params present but wrong shape.
3002+
let bad2 = serde_json::json!({
3003+
"jsonrpc": "2.0",
3004+
"method": "_goose/unstable/session/update",
3005+
"params": { "oops": true }
3006+
});
3007+
client.handle_goose_usage_update(&bad2);
3008+
assert!(client.take_turn_usage().is_none());
3009+
}
28283010
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ fn validate_multiple_event_handling(
541541
Ok(())
542542
}
543543

544-
fn normalize_agent_command_identity(command: &str) -> String {
544+
pub(crate) fn normalize_agent_command_identity(command: &str) -> String {
545545
let normalized = command.trim().replace('\\', "/");
546546
let trimmed = normalized.trim_end_matches('/');
547547
let basename = trimmed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ mod observer;
88
mod pool;
99
mod queue;
1010
mod relay;
11+
mod usage;
12+
13+
pub use usage::TurnUsage;
1114

1215
use std::collections::{HashMap, HashSet};
1316
use std::sync::Arc;
@@ -1402,6 +1405,7 @@ async fn tokio_main() -> Result<()> {
14021405
.as_deref()
14031406
.and_then(|hex| nostr::PublicKey::from_hex(hex).ok()),
14041407
memory_enabled: config.memory_enabled,
1408+
harness_name: crate::config::normalize_agent_command_identity(&config.agent_command),
14051409
});
14061410

14071411
if !config.memory_enabled {

0 commit comments

Comments
 (0)