Repository navigation
fix: preserve Anthropic usage across stream events - #996
rapids-bot[bot] merged 2 commits into
Conversation
Signed-off-by: mnajafian-nv <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Enterprise Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review. 📜 Recent review details⏰ Context from checks skipped due to timeout. (44)
🧰 Additional context used📓 Path-based instructions (29)Tests should cover the behavior promised by the changed API surface, including error paths and cross-request isolation where relevant.⚙️ CodeRabbit configuration file Files:
Review the Rust runtime for async correctness, scope isolation, middleware ordering, and event lifecycle regressions.⚙️ CodeRabbit configuration file Files:
For changes affecting `crates/core`, `crates/adaptive`, or shared Rust runtime semantics, expand validation to the full binding matrix with `validate-change`.📄 CodeRabbit inference engine (.agents/skills/test-rust-core/SKILL.md) Files:
If a language surface changed, always run that language's test target even when Rust core did not change.📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md) Files:
Keep async behavior on the existing tokio-based model.📄 CodeRabbit inference engine (AGENTS.md) Files:
[ ] Do all bindings expose the same logical knobs and semantics?📄 CodeRabbit inference engine (.agents/skills/maintain-observability/SKILL.md) Files:
If any Rust code changed, always run `just test-rust`.📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md) Files:
**Formatting**: `cargo fmt` (rustfmt defaults) **Linting**: `cargo clippy -- -D warnings` -- all warnings are treated as errors📄 CodeRabbit inference engine (CONTRIBUTING.md) Files:
If any Rust code changed, also run `cargo fmt --all`.📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md) Files:
Use `Json = serde_json::Value` in Rust-facing runtime APIs where the existing code expects JSON payloads.📄 CodeRabbit inference engine (AGENTS.md) Files:
[ ] Branch scope is coherent and reviewable [ ] Relevant tests passed under `validate-change` [ ] Docs and examples updated for any public behavior changes [ ] Pull request title follows Conventional Commit style and uses the correct type U...📄 CodeRabbit inference engine (.agents/skills/prepare-pr/SKILL.md) Files:
Format changed files with the language-native formatter before the final lint/test pass.📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md) Files:
Keep NeMo Relay optional Use stable, documented framework or plugin APIs Wrap tool and LLM paths at the correct framework boundary Preserve the framework's original behavior when NeMo Relay is absent Integration uses public framework or plu...📄 CodeRabbit inference engine (.agents/skills/contribute-integration/SKILL.md) Files:
Tool execution callbacks and each execution-intercept `next` continuation return the canonical `ToolExecutionResult { result, annotation }`.📄 CodeRabbit inference engine (.agents/skills/add-middleware/SKILL.md) Files:
For changes in the Rust core, adaptive, dynamic plugin, worker, worker-proto, or types crates, run `cargo fmt --all`, `just test-rust`, and `cargo clippy --workspace --all-targets -- -D warnings` as the default validation sequence.📄 CodeRabbit inference engine (.agents/skills/test-rust-core/SKILL.md) Files:
[ ] `crates/core` or `crates/adaptive` changes ran the full language matrix📄 CodeRabbit inference engine (.agents/skills/prepare-pr/SKILL.md) Files:
For shared-semantics or broad runtime changes in the core or adaptive crates, run `just ci=true test-rust`.📄 CodeRabbit inference engine (.agents/skills/test-rust-core/SKILL.md) Files:
**Core Rust** Implement the behavior first in `crates/core/src/api/` and related core modules such as `crates/core/src/api/runtime/`, `crates/core/src/codec/`, or `crates/core/src/json.rs`.📄 CodeRabbit inference engine (.agents/skills/add-binding-feature/SKILL.md) Files:
Keep SPDX headers on source, docs, scripts, and configuration files.📄 CodeRabbit inference engine (AGENTS.md) Files:
**Validation** Run the validation matrix from the `validate-change` skill for the affected surfaces.📄 CodeRabbit inference engine (.agents/skills/add-binding-feature/SKILL.md) Files:
Use `test-ffi-surface`.📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md) Files:
[ ] Any Rust change ran `just test-rust` [ ] Any Rust change ran `cargo fmt --all` [ ] Any Rust change ran `cargo clippy --workspace --all-targets -- -D warnings`📄 CodeRabbit inference engine (.agents/skills/prepare-pr/SKILL.md) Files:
Follow binding naming conventions: Rust and Python `snake_case`, C FFI exports prefixed `nemo_relay_`, Go `PascalCase` for public APIs, Node.js `camelCase`.📄 CodeRabbit inference engine (AGENTS.md) Files:
[ ] SPDX license header on any new files📄 CodeRabbit inference engine (.agents/skills/add-binding-feature/SKILL.md) Files:
Update docs and examples in the same branch.📄 CodeRabbit inference engine (.agents/skills/maintain-observability/SKILL.md) Files:
If the change touched `crates/core` or shared runtime semantics, also use `validate-change` for broader validation📄 CodeRabbit inference engine (.agents/skills/test-go-binding/SKILL.md) Files:
Run `cargo fmt --all` for all FFI work since it is Rust work Run `just test-rust` to validate FFI changes Run `cargo clippy --workspace --all-targets -- -D warnings` to enforce strict linting on FFI work📄 CodeRabbit inference engine (.agents/skills/test-ffi-surface/SKILL.md) Files:
Run `cargo fmt --all` when Rust files are changed as part of Node work Run `cargo clippy --workspace --all-targets -- -D warnings` when Rust files are changed as part of Node work Run `just test-rust` when Rust files are changed as part of...📄 CodeRabbit inference engine (.agents/skills/test-node-binding/SKILL.md) Files:
When Rust files changed as part of Go work, also run `cargo fmt --all`, `just test-rust`, and `cargo clippy --workspace --all-targets -- -D warnings`📄 CodeRabbit inference engine (.agents/skills/test-go-binding/SKILL.md) Files:
🔇 Additional comments (2)
WalkthroughAnthropic streaming usage accumulation now merges successive usage updates. Initial input and cache fields remain available when later deltas provide only output usage. A unit test validates the finalized aggregate token mapping. ChangesAnthropic usage accumulation
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to Anthropic streaming responses now retain previously reported usage fields when later events provide partial cumulative usage updates. The change is covered for prompt, completion, total, cache-read, and cache-write token counts, with no remaining current-head merge risk identified. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
…pic-stream-usage Signed-off-by: mnajafian-nv <[email protected]>
|
/merge |
#### Overview Preserve Anthropic input and cache token usage when later streaming events contain only partial usage updates. - [x] I confirm this contribution is my own work, or I have the right to submit it under this project's license. - [x] I searched existing issues and open pull requests, and this does not duplicate existing work. #### Details - Merge usage fields across Anthropic `message_start` and `message_delta` events instead of replacing the complete usage object. - Preserve fields omitted from later events while allowing newer cumulative values to replace earlier values. - Add regression coverage for prompt, completion, total, cache-read, and cache-write token counts. - No public API or configuration changes. Validation: - `cargo test -p nemo-relay anthropic_streaming_codec_preserves_usage_across_partial_updates -- --nocapture` - `just test-rust` - `just test-python` - `just test-node` - `just test-go` - `cargo clippy --workspace --all-targets -- -D warnings` - `uv run pre-commit run --all-files` #### Where should the reviewer start? Start with `AnthropicMessagesStreamingState::observe_usage` in `crates/core/src/codec/anthropic.rs`, followed by `anthropic_streaming_codec_preserves_usage_across_partial_updates` in `crates/core/tests/unit/codec/anthropic_tests.rs`. #### Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to) - Relates to NousResearch/hermes-agent#103104 ## Summary by CodeRabbit - **Bug Fixes** - Improved streaming Anthropic usage tracking so usage details from earlier updates are retained when later updates provide only partial information. - Ensured prompt, completion, total, and cache token counts remain accurate after streaming responses finish. - **Tests** - Added coverage for preserving usage fields across successive streaming updates. Authors: - Maryam Najafian (https://github.com/mnajafian-nv) Approvers: - Will Killian (https://github.com/willkill07) URL: NVIDIA#996
…ull (#1778) #### Overview Skips `null` values when merging Anthropic `message_delta` usage into the `message_start` usage, so a streamed call keeps its input and cache token counts when the terminal delta reports them as `null`. - [x] I confirm this contribution is my own work, or I have the right to submit it under this project's license. - [x] I searched existing issues and open pull requests, and this does not duplicate existing work. #### Details - `crates/core/src/codec/anthropic.rs`: `observe_usage` skips `null` values when extending the accumulated usage; non-null values, including `0`, still replace earlier ones. - `crates/core/tests/unit/codec/anthropic_tests.rs`: add `anthropic_streaming_codec_keeps_message_start_usage_when_delta_reports_null`. - Matches Anthropic's SDK accumulator, which copies these counters from `message_delta` only when they are not `None`. - Fields omitted from `message_delta` keep the behavior from #996. - No breaking changes. Tested on macOS, on `release/0.11` at `cf0712e5`: - The new test fails without the fix (`left: Null`, `right: Number(10)`) and passes with it. - `cargo nextest run -p nemo-relay -E 'test(~anthropic)'`: 89 passed. - `just test-rust`: 6733 passed, 4 skipped (with `ulimit -n 4096`, since #1736 is on `main` only). One unrelated ATOF timing test, `unanswered_http_post_requests_record_flush_and_close_timeouts`, was flaky and passed on retry and when run alone. - `cargo fmt --all -- --check` and `cargo clippy -p nemo-relay --all-targets -- -D warnings` pass. - Pre-commit hooks pass, except `npm audit` and `uv audit`, which I skipped; this change touches no lockfiles. Not tested on Linux or Windows; CI covers those. #### Where should the reviewer start? `observe_usage` in `crates/core/src/codec/anthropic.rs`. #### Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to) Closes #1775 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Streaming usage totals now retain previously reported values when later updates provide `null`, while still applying non-null updates. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Sakshi Tapkir <[email protected]>
Overview
Preserve Anthropic input and cache token usage when later streaming events contain only partial usage updates.
Details
message_startandmessage_deltaevents instead of replacing the complete usage object.Validation:
cargo test -p nemo-relay anthropic_streaming_codec_preserves_usage_across_partial_updates -- --nocapturejust test-rustjust test-pythonjust test-nodejust test-gocargo clippy --workspace --all-targets -- -D warningsuv run pre-commit run --all-filesWhere should the reviewer start?
Start with
AnthropicMessagesStreamingState::observe_usageincrates/core/src/codec/anthropic.rs, followed byanthropic_streaming_codec_preserves_usage_across_partial_updatesincrates/core/tests/unit/codec/anthropic_tests.rs.Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)
Summary by CodeRabbit
Bug Fixes
Tests