feat(desktop): NIP-AM agent-usage backend — P2 emission/transport/archive + P4a aggregation/D6 - #4000
Open
wpfleger96 wants to merge 1 commit into
Open
feat(desktop): NIP-AM agent-usage backend — P2 emission/transport/archive + P4a aggregation/D6#4000wpfleger96 wants to merge 1 commit into
wpfleger96 wants to merge 1 commit into
Conversation
wpfleger96
force-pushed
the
duncan/agent-usage-backend
branch
from
July 31, 2026 20:53
1613f34 to
12f1f29
Compare
wpfleger96
force-pushed
the
duncan/agent-usage-backend
branch
2 times, most recently
from
August 3, 2026 18:56
635b030 to
39e1a91
Compare
wpfleger96
force-pushed
the
duncan/agent-usage-backend
branch
from
August 4, 2026 01:03
39e1a91 to
66a09a8
Compare
…) + P4a (aggregation/D6) P2 — cache-write + pricing-identity emission, transport, and archive: - buzz-agent: tri-state accumulators (Unseen/Exact/Unknown) for cache-read and cache-write. Kills unwrap_or(0) pattern from lib.rs; absent field is Unknown, not zero, through the full pipeline. - buzz-agent/wire: conditional wire emission for accumulatedCachedInputTokens and new accumulatedCacheWriteTokens; skip when cumulative is Unseen or Unknown. ACP contract documented next to the payload. - buzz-agent/config: pricing_authority() — canonical URL → bare-host registry token (api.anthropic.com, api.openai.com, openrouter.ai). HTTPS only, exact host, default port, required path where applicable, rejects lookalikes, userinfo, query, fragment. - buzz-agent/agent: PricingIdentity turn discipline — identity retained only while all usage in the current turn carries one identical proven identity; mismatch, unproven-usage-bearing response, or unpaired cumulative poisons to absent; a later matching notification does not heal a mixed turn. - buzz-acp/usage: pricing_identity field on TurnUsage and UsageUpdatePayload. - buzz-core/agent_turn_metric: PricingIdentity and cache-write fields. - M3 migration (desktop/src-tauri): adds turn_cache_write_tokens, cumulative_cache_write_tokens, pricing_authority, pricing_model, pricing_cache_class to agent_metric_index. Additive, idempotent, guarded by marker. Fresh-DB schema includes all M3 columns. P4a — aggregation layer (agent_usage.rs): - Extended S-1 ladder to cache-read and cache-write via the same ladder_token path as input/output/total. - derive_fresh_input: checked arithmetic, fail-closed — absent cache fields produce Unknown (not zero), overflow and subset > input both produce incomplete. Aggregated as a UsageField. - D6 comparator: sort_value() = provider total when known, else input+output when both known, else None (unknown-last). Replaces the prior total-only comparator for both agent-level and model-level sort. - Tests split into agent_usage_tests.rs (existing) and agent_usage_p4a_tests.rs (14 new pinned tests: cache ladder, fresh-input fail-closed cases, D6 comparator vector) to stay under the 1000-line ratchet. Co-authored-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Co-authored-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
wpfleger96
force-pushed
the
duncan/agent-usage-backend
branch
from
August 4, 2026 01:26
66a09a8 to
a67062a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Implements Phases 2 and 4a of the Usage v2 plan (plan events
d0268cd0/0e95b035), extending the archive backend to emit, transport, archive, and aggregate both cache categories and billing identity fail-closed.P2 — emission, transport, archive
Tri-state accumulators (
Unseen/Exact/Unknown) for cache-read and cache-write inbuzz-agentturn and session state. Absent field = Unknown (never zero) through the full pipeline. Kills theunwrap_or(0)pattern.Conditional wire emission for
accumulatedCachedInputTokensand newaccumulatedCacheWriteTokens: fields are omitted when the cumulative is Unseen or Unknown. ACP_goose/unstable/session/updatecontract documented next to the payload with tests for all absence/zero variants.PricingIdentitystamping (publisher-side):pricing_authority(): canonical parsed-URL endpoint comparison against the official allowlist — HTTPS only, exact allowlisted host (lookalike-safe), default port, required API base path, rejects userinfo/query/fragment/path-prefix lookalikes. Wire token is the NIP-AM bare-host registry identifier (api.anthropic.com,api.openai.com,openrouter.ai).request_modelafter mesh/auto resolution (noteffective_model_str).M3 migration: adds
turn_cache_write_tokens,cumulative_cache_write_tokens,pricing_authority,pricing_model,pricing_cache_classtoagent_metric_index. Additive, idempotent, guarded by marker. Fresh-DB schema includes all M3 columns.P4a — aggregation layer
Extended S-1 ladder to cache-read and cache-write via the same
ladder_tokenpath as the existing token fields.freshInputTokensderivation: checked arithmetic, fail-closed — absent cache fields produce Unknown (not zero), overflow andcacheRead+cacheWrite > inputboth produceincomplete: true. Aggregated as aUsageField.D6 comparator:
sort_value()= provider total when known, elseinput+outputwhen both known, elseNone(unknown-last). Replaces the prior total-only comparator for both agent-level and model-level sort. Ships a pinned test vector that the TS render layer (P5) must match.Test coverage
buzz-agent: 394 tests (pricing_authority, turn discipline, cache tri-state, wire emission)buzz-acp: 9 tests (identity threading, cache fields)agent_usage_p4a_tests.rs): cache ladder, fresh-input fail-closed cases (missing slice, overflow, subset > input), D6 comparator vectorRelated PRs