|
1 | 1 | import { randomUUID } from "node:crypto"; |
2 | 2 | import type { RequestSendObserver } from "../../lib/request-execution-budget"; |
3 | | -import { sharedSpendLedger, type SpendReservationLedger } from "../../lib/spend-reservation-ledger"; |
| 3 | +import { sharedSpendLedger, type SpendReservationLedger, type SpendScopes } from "../../lib/spend-reservation-ledger"; |
4 | 4 | import { SpendLedgerOwnerError } from "../../lib/spend-ledger-owner"; |
5 | 5 | import { markLocalRequestLogRefusal, type RequestLogContext } from "../request-log"; |
6 | | -import { recordWorkflowRefusalEvent, workflowDenialSummary } from "../../lib/workflow-budget"; |
| 6 | +import { recordWorkflowRefusalEvent, workflowDenialSummary, type WorkflowDenial } from "../../lib/workflow-budget"; |
7 | 7 |
|
8 | 8 | /** The terminal usage a request reported, in the only two fields the ledger books. */ |
9 | 9 | export interface TerminalSpendUsage { |
@@ -81,44 +81,44 @@ export function createRequestSpendTracker( |
81 | 81 | // short of its limit forever and refuse nothing. |
82 | 82 | const alreadySent = options?.alreadySent === true; |
83 | 83 | const sendId = randomUUID(); |
84 | | - const decision = ledger().reserve({ |
| 84 | + const bookedLedger = ledger(); |
| 85 | + const scopes: SpendScopes = { |
| 86 | + ...(rootId !== undefined ? { rootId } : {}), |
| 87 | + // The existing privacy-safe log label is aliased again by the ledger. |
| 88 | + ...(logCtx.accountLogLabel !== undefined ? { identityId: logCtx.accountLogLabel } : {}), |
| 89 | + ...(logCtx.provider !== undefined ? { poolId: logCtx.provider } : {}), |
| 90 | + }; |
| 91 | + const policy = bookedLedger.policy; |
| 92 | + const enforced = (scopes.rootId !== undefined && policy.root.maxTokens !== undefined) |
| 93 | + || (scopes.identityId !== undefined && policy.identity.maxTokens !== undefined) |
| 94 | + || (scopes.poolId !== undefined && policy.pool.maxTokens !== undefined); |
| 95 | + const decision = bookedLedger.reserve({ |
85 | 96 | sendId, |
86 | | - scopes: { |
87 | | - ...(rootId !== undefined ? { rootId } : {}), |
88 | | - // Already the privacy-safe label the request log uses, and the ledger aliases it |
89 | | - // again on the way to disk. A raw credential never reaches either. |
90 | | - ...(logCtx.accountLogLabel !== undefined ? { identityId: logCtx.accountLogLabel } : {}), |
91 | | - ...(logCtx.provider !== undefined ? { poolId: logCtx.provider } : {}), |
92 | | - }, |
| 97 | + scopes, |
93 | 98 | inputTokens: logCtx.spendInputEstimateTokens ?? logCtx.usageLogInputTokens ?? 0, |
94 | 99 | outputCeilingTokens: logCtx.spendOutputCeilingTokens ?? 0, |
95 | 100 | ...(alreadySent ? { alreadySent: true } : {}), |
96 | 101 | }); |
97 | 102 | if (!decision.reserved) { |
| 103 | + // An applicable ceiling cannot authorize an unbooked send, including when tracking |
| 104 | + // capacity is full. Unconfigured/nonapplicable requests remain observe-only, and a |
| 105 | + // physical send reported after dispatch cannot be refused retroactively. |
| 106 | + if (alreadySent || !enforced) return true; |
98 | 107 | refusals += 1; |
99 | 108 | const denial = decision.denial; |
100 | | - // A ceiling refuses, and so does a ledger that cannot make the reservation durable |
101 | | - // under one. That second case is the whole reason this store is on disk: admitting a |
102 | | - // send whose record a restart would forget is how an exhausted budget comes back with |
103 | | - // a fresh allowance, and the ledger raises those two denials ONLY when a limit is |
104 | | - // configured -- so an install that configured nothing is still never refused here. |
105 | | - // Capacity and a duplicate send id stay permissive: they say the ledger cannot account |
106 | | - // for this send, which is a degradation to report, not an outage to cause. |
107 | | - if (denial.reason === "reserve-not-durable" || denial.reason === "journal-corrupt") { |
108 | | - return alreadySent; |
109 | | - } |
110 | | - if (denial.reason !== "spend-limit-exceeded") return true; |
111 | | - // This send is refused, and the dispatch path that asked will report an exhausted send |
112 | | - // budget -- from there, that is all it can see. The row is where an operator actually |
113 | | - // looks, so the ceiling is named on it here: a locally assigned code wins in |
114 | | - // addFinalRequestLog, so the request that CROSSED the ceiling reads as a spend refusal |
115 | | - // rather than as the ordinary budget exhaustion it would otherwise be indistinguishable |
116 | | - // from. The event ring gets the same pair so /api/workflow-budget agrees with the row. |
117 | | - const detail = { scope: denial.scope, limit: denial.limit, projected: denial.projected }; |
118 | | - const summary = workflowDenialSummary("workflow-spend-exhausted", detail); |
| 109 | + const reason: WorkflowDenial = denial.reason === "duplicate-send-id" |
| 110 | + ? "workflow-send-replayed" |
| 111 | + : denial.reason === "reserve-not-durable" || denial.reason === "journal-corrupt" |
| 112 | + ? "workflow-spend-undurable" |
| 113 | + : denial.reason === "tracking-capacity-exhausted" |
| 114 | + ? "workflow-tracking-exhausted" |
| 115 | + : "workflow-spend-exhausted"; |
| 116 | + const detail = denial.reason === "spend-limit-exceeded" |
| 117 | + ? { scope: denial.scope, limit: denial.limit, projected: denial.projected } : undefined; |
| 118 | + const summary = workflowDenialSummary(reason, detail); |
119 | 119 | markLocalRequestLogRefusal(logCtx, summary.code); |
120 | 120 | logCtx.errorCode = summary.code; |
121 | | - recordWorkflowRefusalEvent(rootId, "workflow-spend-exhausted", Date.now(), detail); |
| 121 | + recordWorkflowRefusalEvent(rootId, reason, Date.now(), detail); |
122 | 122 | return false; |
123 | 123 | } |
124 | 124 | live.push(sendId); |
|
0 commit comments