refactor(domain): charge remaining fixed allowances through Budget (#11319)

* refactor(domain): charge the Interface Builder, NativeAOT, managed and runtime budgets through Budget

* test(browser): show a refused runtime retention charge leaves the room intact

* refactor(domain): charge the remaining fixed allowances through Budget (#11383)

* refactor(domain): charge CUDA metadata and SQLite reply budgets through Budget

CudaMetadataBudget (visits, section reads, temporary index) and the SQLite
OutputBudget kept hand-written allowances whose field names escaped the
budget-arithmetic guard; the SQLite reply budget also charged each part
before checking it. Both now charge Budget, which refuses an oversized
charge without changing the remainder.

The guard now also counts, inside any hand-written ...Budget class, a field
that is adjusted by hand and compared with < or >, whatever its name.
Unchecked tallies (a yield counter, a reservation ledger) stay uncounted.
Firmware arena cursors are write offsets, not allowances: they are renamed
#arenaWriteOffset and leave the guard's scope. Baseline 17 -> 15.

* refactor(domain): move cache, re-priced, checkpointed and ledger retention onto Budget

Budget gains chargeEvicting (evict the oldest cache entries until a charge
fits) and restore (refund everything charged since a used mark, never
re-charging). With them:

- the Bun metadata range cache evicts through chargeEvicting and charges
  physical reads through a second Budget;
- Playwright scenario events refund an event's old cost and try-charge its
  re-priced cost, and admission charges with tryCharge;
- WebNetworkProjectionAccumulator keeps one shared allowance plus retained
  and temporary shares whose checkpoints restore by refunding;
- EvidenceLedger commits its admitted delta (charge, or refund when a
  replacement shrinks) and clears with restore(0);
- the web bundle worker charges an admitted result, and on the completed-
  snapshot path retains facts only when they still fit;
- the Pyc nested code walk charges steps and retained references.

budget-arithmetic 15 -> 4 (JavaScriptAnalysisWorker only).

* build(verify): declare the budget-arithmetic guard's counter for typed tests
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 03:08:28 +08:00
1 parent aba42b47b9
commit 64c119df47
30 files changed
+593 -346

No files matched your search

+18 -16
View File
@@ -1,5 +1,6 @@
import { DatabaseSync } from "node:sqlite";
import { z } from "zod";
import { Budget } from "../domain/budget.js";
import { jsonParts } from "../domain/jsonSerialization.js";
import {
SQLITE_ROW_LIMIT_DEFAULT,
@@ -80,26 +81,30 @@ type SelectedTableFacts = Pick<
readonly orderKey?: RowKeyFacts;
};
const replyExhausted = (): SqliteInspectionFailure =>
new SqliteInspectionFailure(
"output-limit",
"Complete SQLite evidence exceeds the 16 MiB JSON reply budget; no partial success was returned.",
);
class OutputBudget {
#bytes = 1024;
readonly #bytes = new Budget(SQLITE_DATABASE_LIMITS.outputBytes);
constructor() {
// The reply envelope's fixed cost.
this.#bytes.charge(1024, replyExhausted);
}
retain(value: unknown): void {
// Count bounded encoded parts, including escapes, without allocating a
// document-sized string just to decide whether it fits the reply.
this.#bytes += 1;
for (const part of jsonParts(value)) {
this.#bytes += Buffer.byteLength(part);
if (this.#bytes > SQLITE_DATABASE_LIMITS.outputBytes)
throw new SqliteInspectionFailure(
"output-limit",
"Complete SQLite evidence exceeds the 16 MiB JSON reply budget; no partial success was returned.",
);
}
this.#bytes.charge(1, replyExhausted);
for (const part of jsonParts(value))
this.#bytes.charge(Buffer.byteLength(part), replyExhausted);
}
beforeDecode(row: z.output<typeof arrayRow>): void {
let minimumBytes = this.#bytes;
let minimumBytes = 0;
for (const value of row)
if (typeof value === "string") minimumBytes += Buffer.byteLength(value);
if (minimumBytes > SQLITE_DATABASE_LIMITS.outputBytes)
if (minimumBytes > this.#bytes.remaining)
throw new SqliteInspectionFailure(
"output-limit",
"Selected cells exceed the 16 MiB reply budget before text/hex expansion.",
@@ -110,10 +115,7 @@ class OutputBudget {
(total, length) => total + length * 2n,
BigInt(lengths.length * 64),
);
if (
encodedBytes + BigInt(this.#bytes) >
BigInt(SQLITE_DATABASE_LIMITS.outputBytes)
)
if (encodedBytes > BigInt(this.#bytes.remaining))
throw new SqliteInspectionFailure(
"output-limit",
"Selected row byte lengths exceed the 16 MiB reply budget before hex projection.",