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

+2 -1
View File
@@ -9,6 +9,7 @@ import {
} from "./counted-baseline.mjs";
import { localCompletenessEnumCount } from "./completeness-enum-guard.mjs";
import { countKeyedFirstMatches } from "./keyed-first-match.mjs";
import { budgetArithmeticCount } from "./budget-arithmetic-guard.mjs";
import { localLimitErrorClassCount } from "./limit-class-guard.mjs";
// Architecture guards for the strict-identity / best-effort-inference split.
@@ -367,7 +368,7 @@ checkCounted(
"hand-written budget arithmetic (use Budget from src/domain/budget)",
"budget-arithmetic",
files,
/this\.#?\w*(?:[Rr]emaining|[Rr]etained|[Uu]sed)\w*\s*[-+]=/gu,
budgetArithmeticCount,
["src/domain/budget.ts"],
);
@@ -1,19 +1,3 @@
{
"src/application/investigation/EvidenceLedger.ts": 1,
"src/artifacts/UnityFsArtifactReader.ts": 1,
"src/artifacts/apple/InterfaceBuilderDecodeBudget.ts": 2,
"src/artifacts/bun/BunModuleGraphLayout.ts": 2,
"src/browser/PlaywrightScenarioEvents.ts": 3,
"src/browser/WebBundleAnalysisWorker.ts": 2,
"src/browser/execution/CdpRuntimeSources.ts": 2,
"src/desktop/DesktopContentStore.ts": 1,
"src/domain/analysisView/WebNetworkProjectionAccumulator.ts": 1,
"src/domain/apiContract/parseApiContract.ts": 1,
"src/domain/firmware/hexRecords.ts": 1,
"src/domain/firmware/uf2.ts": 1,
"src/domain/javascript/javascriptSemanticWorkAllowance.ts": 1,
"src/domain/javascript/javascriptSymbolicBindings.ts": 4,
"src/dotnet/ManagedDecodeBudget.ts": 1,
"src/javascript/analysis/JavaScriptAnalysisWorker.ts": 4,
"src/python/PythonPycParser.ts": 2
"src/javascript/analysis/JavaScriptAnalysisWorker.ts": 4
}
@@ -0,0 +1 @@
export function budgetArithmeticCount(content: string): number;
@@ -0,0 +1,45 @@
// Counts hand-written allowance arithmetic that `Budget` (src/domain/budget)
// owns. Two shapes are counted:
//
// 1. A field named for an allowance (`…remaining…`, `…retained…`, `…used…`)
// adjusted with `+=` or `-=`.
// 2. Inside a hand-written `…Budget` class, any field that is both adjusted
// by hand (`+=`, `-=`, `++`, `--`) and compared with `<` or `>`: that is a
// limit check, whatever the field is called (`#bytes`, `#visits`).
//
// Write cursors (`#arenaWriteOffset`) and observation tallies are not
// allowances; outside a `…Budget` class they are not counted unless named as
// one.
const namedAllowance =
/this\.#?\w*(?:[Rr]emaining|[Rr]etained|[Uu]sed)\w*\s*[-+]=/gu;
const budgetClass =
/^(?:export\s+)?class\s+\w*Budget\b[^\n]*\{\n([\s\S]*?)^\}/gmu;
const adjusted = /(?:\+\+|--)this\.(#?\w+)|this\.(#?\w+)\s*(?:[-+]=|\+\+|--)/gu;
const escape = (name) => name.replaceAll("$", "\\$");
// `<` or `>` near the field on one line, excluding `=>` and shifts.
const comparedIn = (body, name) => {
const field = `this\\.${escape(name)}\\b`;
const comparison = "(?<![=<>])[<>](?![<>])";
return new RegExp(
`${field}[^;\\n]*${comparison}|${comparison}[^;\\n]*${field}`,
"u",
).test(body);
};
/** @param {string} content comment-stripped source */
export const budgetArithmeticCount = (content) => {
let count = content.match(namedAllowance)?.length ?? 0;
for (const [, body] of content.matchAll(budgetClass)) {
for (const match of body.matchAll(adjusted)) {
// Already counted as a named allowance.
if (new RegExp(`^${namedAllowance.source}$`, "u").test(match[0]))
continue;
if (comparedIn(body, match[1] ?? match[2])) count += 1;
}
}
return count;
};