MCPcopy Create free account
hub / github.com/ScriptedAlchemy/tracedecay / merge_usage_counters

Function merge_usage_counters

src/sessions/codex.rs:935–945  ·  view source on GitHub ↗

Add `add`'s numeric counters field-wise into `existing` (both are usage objects). Used when several flushes land on the same assistant message (e.g. an aborted turn with no reply of its own).

(existing: &mut Value, add: &Value)

Source from the content-addressed store, hash-verified

933/// objects). Used when several flushes land on the same assistant message
934/// (e.g. an aborted turn with no reply of its own).
935pub(crate) fn merge_usage_counters(existing: &mut Value, add: &Value) {
936 let (Some(map), Some(add_map)) = (existing.as_object_mut(), add.as_object()) else {
937 return;
938 };
939 for (key, value) in add_map {
940 if let Some(count) = value.as_i64() {
941 let current = map.get(key).and_then(Value::as_i64).unwrap_or(0);
942 map.insert(key.clone(), Value::from(current.saturating_add(count)));
943 }
944 }
945}
946
947/// Attach the finished turn's summed usage to the most recent assistant
948/// message of the batch (the reply the turn's `token_count` events report

Callers 2

flush_codex_turn_usageFunction · 0.85
flush_turn_usageFunction · 0.85

Calls 1

insertMethod · 0.80

Tested by

no test coverage detected