Repository navigation
[ISSUE #8386] Report unavailable AI token usage outcomes instead of silently omitting accounting - #8539
[ISSUE #8386] Report unavailable AI token usage outcomes instead of silently omitting accounting#8539konlue wants to merge 2 commits into
Conversation
…d of silently omitting accounting The response usage observer now emits exactly one AiUsage outcome per response through the accounting consumer: a reported completion count (a known zero stays reported), or an explicitly unavailable usage with the shared apache#7955 vocabulary — LIMIT_EXCEEDED for the 64 KiB buffer, SSE-event and gzip budgets, INVALID for malformed JSON and non-integral or negative counts, UNKNOWN for missing usage and observations that end before any usage arrives. Only reported counts debit the existing Redis counter exactly once per request; every other outcome produces a single bounded WARN diagnostic naming the status only, with no response content, credentials or caller labels. No extra body subscription is introduced, forwarded bytes stay untouched, and reservation-enabled consumers are documented never to settle or release reserved consumption on an unavailable outcome. Tests pin each outcome through the response decorator/accounting boundary with below/at/above-bound JSON fixtures, oversized SSE events, missing usage, malformed counts, cancellation and gzip exhaustion, red against the previous silent omission.
No caller remains after the outcome-typed finishOutcome() replaced the reported/empty view; keeping the one-shot delegate would let a stale caller consume the single finish and turn the response's real outcome into UNKNOWN.
Aias00
left a comment
There was a problem hiding this comment.
Stream Agent analysis
Reviewed the complete captured diff for the AI token-limiter accounting change. The extractor now emits exactly one explicit AiUsage outcome per response (REPORTED / UNKNOWN / INVALID / LIMIT_EXCEEDED) via finishOutcome(), and the decorator's accounting consumer debits Redis only for REPORTED counts, replacing the previous silent OptionalLong.empty() omission.
Evidence: ["Complete diff (complete_diff=true) reviewed across all four changed files: AiTokenLimiterPlugin.java plus the three test suites; no diff pages unread.","Accounting seam verified: finishResponse calls usageRecorder.accept(extractor.finishOutcome()) once; applyUsageOutcome debits the unchanged Redis Lua counter only for REPORTED with non-null tokens and emits one bounded status-only WARN otherwise; forwarded body bytes asserted unchanged.","CI metadata: 35 checks, passed=true, diagnosis CI_PASSED, no failed checks. No local tests executed. Supplementary resource_read/list/search for non-diff source failed (FULL_DIFF attachment, no base/head source IDs); not needed for coverage."]
Next steps: []
Verdict: PASS; publication: APPROVE (EXPLICIT_PASS)
Findings: []
Scope: captured GitHub evidence only; no local tests were executed.
Task: 94524c64-d1ed-4894-9218-f98dbe175bd2 Commit: 262e0f3badf79bd36e04753a779deef4fbca9b71
Fixes #8386.
The token limiter's response observer accounted usage only when extraction succeeded: an oversized payload, malformed JSON, a missing usage object or a cancelled observation all ended in
OptionalLong.empty()and the accounting consumer was never invoked — a silent omission indistinguishable from "nothing to account". This PR makes every response yield exactly one explicit outcome, reusing the sharedAiUsagecontract fromshenyu-common(#7955) rather than a second ledger: the extractor's bounded parser keeps its 64 KiB budget and now records a fixed reason for every unavailable outcome —LIMIT_EXCEEDEDwhen the nonstreaming buffer, an SSE event or the gzip inflation budget is exhausted,INVALIDfor malformed JSON, non-object usage and non-integral or negative token counts, andUNKNOWNfor responses that report no usage at all or that end before any usage arrives (cancellations included). A known zero stays a reported zero.What changed
ResponseUsageExtractorcarries a failure reason and exposesfinishOutcome()returning theAiUsagerecord;finish()remains as a thin reported/empty view. The decorator's accounting seam is oneConsumer<AiUsage>invoked exactly once per response from the existingdoFinallyhook, so there is no duplicate debit and no extra body subscription, and forwarded bytes are untouched. The plugin's consumer debits the existing Redis counter through the unchanged Lua script only forREPORTEDcounts; every other outcome produces a single boundedWARNdiagnostic naming the status only — no response content, no credentials, no high-cardinality caller labels. Reservation-enabled consumers must not settle or release reserved consumption on an unavailable outcome (never an assumed zero); that reconciliation stays with the reservation tasks, and the seam's javadoc documents it.Tests
AiTokenLimiterPluginTestdrives the decorator/accounting boundary, not just the parser: below/at/above-bound JSON fixtures separateREPORTEDfrom an explicitLIMIT_EXCEEDEDwhile the bound and the forwarded bytes stay intact; an oversized SSE event and gzip budget exhaustion carry the same size-limit reason; missing usage reportsUNKNOWN(previously silent), malformed counts reportINVALID, and a cancelled observation with no captured usage reportsUNKNOWNwhile usage captured before cancellation stays recorded; a consumer unit test verifies only reported counts debit the counter. All outcomes are red against the previous silent-omit behavior, and the existing gzip-boundary, chunking, zero-vs-unknown and cancellation coverage is preserved on the new outcome vocabulary. Module results on the current master baseline:shenyu-plugin-ai-token-limiter66 tests (was 44) — all green; checkstyle clean.