@ultimat3/ai 31.43.0 → 31.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/package.json +11 -11
  3. package/src/budget.ts +23 -2
package/README.md CHANGED
@@ -153,6 +153,7 @@ default store at `replicas: 6` is six ledgers of twenty million, which is a budg
153
153
  | Wire parameters go through **`providerOptions`, one typed bag per wire** (`PROVIDER_OPTION_KEYS`: `anthropic` `metadata`/`service_tier`; `openai` `top_p`/`frequency_penalty`/`presence_penalty`/`user`/`parallel_tool_calls`/`service_tier`) | an unknown wire, key, type or range is `X_AI_REQUEST_INVALID` before any request — at `llm()` / `agent()` declaration, in the gateway, and in each body builder; each provider sends only its own wire's bag; the bag is in the response cache key |
154
154
  | The repair turn replays the tool call's arguments, never an empty `text` | an answer through the `respond` tool leaves `text` empty, and an empty text block is a 400 — the repair came back as `X_AI_PROVIDER_UNAVAILABLE` |
155
155
  | `reserve()` **debits, then checks** | the in-memory scopes check and debit with no `await` between, the store's through its atomic `take`; a read-then-write let concurrent calls — one request per scope racing one org key — all read the same `spent()` and all pass. `record` reconciles and `release` gives it back |
156
+ | A reconcile writes the **store first**, the in-memory counters only once every write landed | a `BudgetStore.add` that throws while booking a paid call leaves the ledger where `reserve` put it (a write that landed on one scope before the next threw is taken back), so the gateway's release of the whole reservation lands at zero — moving the counters first left them at `actual − 2 × reserved`, negative, a ceiling admitting more than it declares |
156
157
  | The cache key is every tool **whole**, through core's `fingerprint` | every `llm()` tool is named `respond`, so a key over names served an answer shaped for an old `output` schema; a cache `set` that throws is logged, never turned into a failure of a call already paid for |
157
158
  | A refusal is never cached | a cached one keeps serving a classifier decision after the prompt was fixed |
158
159
  | A cache hit reads zero `usage`, `cost` and `costExact`, in the same currency | it is not debited, so a meter summing the stored figures read more than the budget beside it |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/ai",
3
- "version": "31.43.0",
3
+ "version": "31.44.0",
4
4
  "description": "LLM gateway, versioned prompts, evals as tests, embeddings, hybrid vector search, RAG",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -45,17 +45,17 @@
45
45
  "test": "bun test"
46
46
  },
47
47
  "dependencies": {
48
- "@ultimat3/action": "31.43.0",
49
- "@ultimat3/cache": "31.43.0",
50
- "@ultimat3/core": "31.43.0",
51
- "@ultimat3/db": "31.43.0",
52
- "@ultimat3/http": "31.43.0",
53
- "@ultimat3/jobs": "31.43.0",
54
- "@ultimat3/money": "31.43.0",
55
- "@ultimat3/schema": "31.43.0",
56
- "@ultimat3/time": "31.43.0"
48
+ "@ultimat3/action": "31.44.0",
49
+ "@ultimat3/cache": "31.44.0",
50
+ "@ultimat3/core": "31.44.0",
51
+ "@ultimat3/db": "31.44.0",
52
+ "@ultimat3/http": "31.44.0",
53
+ "@ultimat3/jobs": "31.44.0",
54
+ "@ultimat3/money": "31.44.0",
55
+ "@ultimat3/schema": "31.44.0",
56
+ "@ultimat3/time": "31.44.0"
57
57
  },
58
58
  "devDependencies": {
59
- "@ultimat3/policy": "31.43.0"
59
+ "@ultimat3/policy": "31.44.0"
60
60
  }
61
61
  }
package/src/budget.ts CHANGED
@@ -293,10 +293,31 @@ export class BudgetLedger {
293
293
  */
294
294
  private async debit(tokens: number): Promise<void> {
295
295
  if (tokens === 0) return;
296
- this.debitChain(tokens);
296
+ // The STORE first, the in-memory chain only once every write landed — as `reserve` orders it.
297
+ // A write that throws leaves the ledger exactly where it was, so the caller's `release` of the
298
+ // whole reservation brings it to zero; moving the chain first left it at `actual - 2×reserved`
299
+ // (negative), a ceiling admitting more than it declares.
297
300
  // The keys `reserve` took — a scope with a declared ceiling — and no others, so a reconcile
298
301
  // never creates the counter `takeScope` declined to.
299
- for (const key of this.meteredKeys()) await this.store.add(key, tokens);
302
+ const landed: string[] = [];
303
+ try {
304
+ for (const key of this.meteredKeys()) {
305
+ await this.store.add(key, tokens);
306
+ landed.push(key);
307
+ }
308
+ } catch (error) {
309
+ // A write that landed on one scope before the next one threw is taken back, so every scope
310
+ // is left as it was; a store down for the undo too keeps the original throw.
311
+ for (const key of landed) {
312
+ try {
313
+ await this.store.add(key, -tokens);
314
+ } catch {
315
+ // The original throw is the one the caller acts on (it releases and logs it).
316
+ }
317
+ }
318
+ throw error;
319
+ }
320
+ this.debitChain(tokens);
300
321
  }
301
322
 
302
323
  /** The store keys this ledger meters: a key whose scope declares a ceiling. */