@@ -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}
0 commit comments