Skip to content

fix: allow null value for used field in UsageUpdate#605

Open
sternelee wants to merge 1 commit intoagentclientprotocol:mainfrom
sternelee:main
Open

fix: allow null value for used field in UsageUpdate#605
sternelee wants to merge 1 commit intoagentclientprotocol:mainfrom
sternelee:main

Conversation

@sternelee
Copy link

The used field in UsageUpdate can be null when the agent doesn't provide token usage information. Changed from u64 to Option<u64> to handle this case gracefully.

Fixes decode error: "invalid type: null, expected u64"

The `used` field in UsageUpdate can be null when the agent doesn't
provide token usage information. Changed from `u64` to `Option<u64>`
to handle this case gracefully.

Fixes decode error: "invalid type: null, expected u64"

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 1, 2026 02:53
@sternelee sternelee requested a review from a team as a code owner March 1, 2026 02:53
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the unstable session usage update model to tolerate agents sending null for token usage, preventing deserialization failures when usage data is missing.

Changes:

  • Change UsageUpdate.used from u64 to Option<u64> to accept null/missing values.
  • Update UsageUpdate::new to accept impl IntoOption<u64> and convert to Option<u64>.
  • Add skip_serializing_if = "Option::is_none" on used to omit it when None.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 252 to 275
@@ -268,9 +269,9 @@ pub struct UsageUpdate {
#[cfg(feature = "unstable_session_usage")]
impl UsageUpdate {
#[must_use]
pub fn new(used: u64, size: u64) -> Self {
pub fn new(used: impl IntoOption<u64>, size: u64) -> Self {
Self {
used,
used: used.into_option(),
size,
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes deserialization of used: null, but there’s no test coverage for UsageUpdate’s (de)serialization behavior. Please add unit tests (under #[cfg(feature = "unstable_session_usage")]) that cover at least: {used: null} -> used == None, {} (missing used) behavior if supported, and serialization behavior when used is None vs Some (especially with skip_serializing_if).

Copilot uses AI. Check for mistakes.
Comment on lines +253 to +254
#[serde(skip_serializing_if = "Option::is_none")]
pub used: Option<u64>,
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By changing used from u64 to Option<u64>, the generated unstable JSON Schema for UsageUpdate will change (nullable + no longer required). Please regenerate and commit the updated schema/schema.unstable.json (and any generated docs/metadata that depend on it) so the repository’s published schema stays in sync with the Rust types.

Suggested change
#[serde(skip_serializing_if = "Option::is_none")]
pub used: Option<u64>,
pub used: u64,

Copilot uses AI. Check for mistakes.
@@ -250,7 +250,8 @@ impl SessionInfoUpdate {
#[non_exhaustive]
pub struct UsageUpdate {
/// Tokens currently in context.
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used is now optional, but the doc comment still reads like it’s always present. Please update the field docs to clarify that used may be omitted/null when the agent doesn’t provide token usage information (and what None means semantically).

Suggested change
/// Tokens currently in context.
/// Tokens currently in context.
///
/// This field is optional and may be omitted/`null` if the agent does not provide
/// token usage information for this update. When `None`, the token usage is
/// unknown, not necessarily zero.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants