Skip to content

Commit 08e707e

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
feat(acp,buzz-agent): thread model name through NIP-AM kind 44200 emit path (#1564)
Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 04a9d10 commit 08e707e

3 files changed

Lines changed: 83 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2743,7 +2743,7 @@ async fn publish_agent_turn_metric(
27432743
let timestamp = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
27442744
let payload = AgentTurnMetricPayload {
27452745
harness: ctx.harness_name.clone(),
2746-
model: None,
2746+
model: usage.model.clone(),
27472747
channel_id: channel_id.map(|id| id.to_string()),
27482748
session_id: Some(usage.session_id.clone()),
27492749
turn_id: Some(turn_id.to_string()),
@@ -3965,6 +3965,7 @@ mod tests {
39653965
cumulative_input_tokens: 100,
39663966
cumulative_output_tokens: 50,
39673967
cumulative_cost_usd: None,
3968+
model: None,
39683969
};
39693970
// owner_pubkey = None → early return, no panic.
39703971
publish_agent_turn_metric(
@@ -3996,6 +3997,7 @@ mod tests {
39963997
cumulative_input_tokens: 200,
39973998
cumulative_output_tokens: 80,
39983999
cumulative_cost_usd: Some(0.001),
4000+
model: None,
39994001
};
40004002
// Will try to publish and fail (no real relay) but must not panic.
40014003
publish_agent_turn_metric(
@@ -4028,6 +4030,7 @@ mod tests {
40284030
cumulative_input_tokens: 150,
40294031
cumulative_output_tokens: 70,
40304032
cumulative_cost_usd: None,
4033+
model: None,
40314034
};
40324035
// Must not panic; HTTP submit will fail (no real relay) — that's fine.
40334036
publish_agent_turn_metric(
@@ -4060,6 +4063,7 @@ mod tests {
40604063
cumulative_input_tokens: 400,
40614064
cumulative_output_tokens: 100,
40624065
cumulative_cost_usd: None,
4066+
model: None,
40634067
};
40644068
// Will try to publish (encrypt succeeds) and fail HTTP (no relay) — must not panic.
40654069
publish_agent_turn_metric(

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pub(crate) struct UsageUpdatePayload {
8686
pub accumulated_input_tokens: u64,
8787
pub accumulated_output_tokens: u64,
8888
pub accumulated_cost: Option<f64>,
89+
/// Effective model id for this turn. Optional — goose payloads that
90+
/// predate this field deserialize cleanly as `None`.
91+
#[serde(default)]
92+
pub model: Option<String>,
8993
}
9094

9195
/// Per-session normalization state: the last cumulative snapshot we saw.
@@ -130,6 +134,9 @@ pub struct TurnUsage {
130134
pub cumulative_output_tokens: u64,
131135
/// Session-cumulative estimated cost in USD; `None` if goose did not report it.
132136
pub cumulative_cost_usd: Option<f64>,
137+
/// Effective model id for this turn (maps to NIP-AM `model`). `None` if the
138+
/// harness did not include the model in its usage notification.
139+
pub model: Option<String>,
133140
}
134141

135142
/// Tracks per-session cumulative usage state across turns.
@@ -262,6 +269,7 @@ impl UsageTracker {
262269
cumulative_input_tokens: current_input,
263270
cumulative_output_tokens: current_output,
264271
cumulative_cost_usd: current_cost,
272+
model: payload.model.clone(),
265273
});
266274
} else if self.in_flight_session.is_none() {
267275
// Not in-flight at all: advance the committed baseline so the next
@@ -322,6 +330,7 @@ mod tests {
322330
accumulated_input_tokens: input,
323331
accumulated_output_tokens: output,
324332
accumulated_cost: cost,
333+
model: None,
325334
}
326335
}
327336

@@ -332,6 +341,7 @@ mod tests {
332341
accumulated_input_tokens: input,
333342
accumulated_output_tokens: output,
334343
accumulated_cost: cost,
344+
model: None,
335345
}
336346
}
337347

@@ -812,4 +822,71 @@ mod tests {
812822
"take() without any record() must return None (pre-response cancel path)"
813823
);
814824
}
825+
826+
// ── model field threading ────────────────────────────────────────────────
827+
828+
fn payload_with_model(
829+
input: u64,
830+
output: u64,
831+
cost: Option<f64>,
832+
model: Option<&str>,
833+
) -> UsageUpdatePayload {
834+
UsageUpdatePayload {
835+
used: input + output,
836+
context_limit: 200_000,
837+
accumulated_input_tokens: input,
838+
accumulated_output_tokens: output,
839+
accumulated_cost: cost,
840+
model: model.map(str::to_string),
841+
}
842+
}
843+
844+
#[test]
845+
fn model_threads_from_payload_to_turn_usage() {
846+
// When a `usage_update` payload includes a `model` field, TurnUsage
847+
// must carry it through so pool.rs can populate the 44200 payload.
848+
let mut tracker = UsageTracker::default();
849+
let p = payload_with_model(1000, 200, None, Some("claude-sonnet-4-5"));
850+
tracker.begin_turn("sess-model");
851+
tracker.record("sess-model", &p);
852+
let usage = tracker.take().expect("pending");
853+
assert_eq!(
854+
usage.model.as_deref(),
855+
Some("claude-sonnet-4-5"),
856+
"model must pass through record() → pending → take()"
857+
);
858+
}
859+
860+
#[test]
861+
fn model_none_when_payload_omits_model_field() {
862+
// Goose payloads that predate the `model` field must deserialize cleanly
863+
// and produce TurnUsage with model = None (no deserialization error,
864+
// no panic — goose-parity / fail-soft contract).
865+
let json = r#"{
866+
"sessionUpdate": "usage_update",
867+
"accumulatedInputTokens": 500,
868+
"accumulatedOutputTokens": 100,
869+
"accumulatedCost": 0.005
870+
}"#;
871+
let variant: GooseSessionUpdateVariant =
872+
serde_json::from_str(json).expect("must deserialize without model field");
873+
let payload = match variant {
874+
GooseSessionUpdateVariant::UsageUpdate(p) => p,
875+
_ => panic!("expected UsageUpdate variant"),
876+
};
877+
assert!(
878+
payload.model.is_none(),
879+
"model must be None when absent from wire payload"
880+
);
881+
882+
// And it should produce a TurnUsage with model = None.
883+
let mut tracker = UsageTracker::default();
884+
tracker.begin_turn("sess-goose-compat");
885+
tracker.record("sess-goose-compat", &payload);
886+
let usage = tracker.take().expect("pending");
887+
assert!(
888+
usage.model.is_none(),
889+
"TurnUsage.model must be None when payload omits the field"
890+
);
891+
}
815892
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
728728
"contextLimit": 0u64,
729729
"accumulatedInputTokens": accumulated_in,
730730
"accumulatedOutputTokens": accumulated_out,
731+
"model": effective_model_str,
731732
}),
732733
),
733734
)

0 commit comments

Comments
 (0)