diff --git a/scripts/lib/owned-mcp-stdio.mjs b/scripts/lib/owned-mcp-stdio.mjs index 8e60631d00..13889ffceb 100644 --- a/scripts/lib/owned-mcp-stdio.mjs +++ b/scripts/lib/owned-mcp-stdio.mjs @@ -16,8 +16,8 @@ export async function createOwnedMcpStdioTransport(options) { // Match the SDK's default maximum buffered stdio message size. maxFrameBytes: 10 * 1024 * 1024, // Unlike the JADX adapter, artifact verifiers do not impose a cumulative - // per-connection output limit; the SDK bounds each buffered message. - maxOperationBytes: Infinity, + // per-connection output limit, so maxOperationBytes is omitted; the SDK + // bounds each buffered message. gracefulInputCloseMs: 2000, maxDiagnosticBytes: 64 * 1024, }); diff --git a/scripts/verify/baselines/budget-arithmetic.json b/scripts/verify/baselines/budget-arithmetic.json index a3866e17b6..9729d605f2 100644 --- a/scripts/verify/baselines/budget-arithmetic.json +++ b/scripts/verify/baselines/budget-arithmetic.json @@ -1,13 +1,6 @@ { - "src/application/environment/environmentStateStore.ts": 1, "src/artifacts/AsarHeader.ts": 1, - "src/domain/javascript/javascriptRouteScan.ts": 1, - "src/domain/python/marshalReader.ts": 1, "src/kuna/KunaJsonStream.ts": 1, - "src/kuna/KunaRunSlots.ts": 1, "src/process/BoundedOutputExcerpt.ts": 1, - "src/process/OwnedMcpStdioTransport.ts": 1, - "src/process/ProviderProcess.ts": 1, - "src/process/capture/ProcessSensitiveOutput.ts": 1, - "src/server/http/NodeHttpRequestOwner.ts": 1 + "src/process/ProviderProcess.ts": 1 } diff --git a/src/application/environment/environmentStateStore.ts b/src/application/environment/environmentStateStore.ts index 45818277a2..b70892b824 100644 --- a/src/application/environment/environmentStateStore.ts +++ b/src/application/environment/environmentStateStore.ts @@ -12,6 +12,7 @@ import { AnalysisInputError, AnalysisResourceConstraintError, } from "../../domain/analysisErrorCore.js"; +import { Budget } from "../../domain/budget.js"; import type { JsonValue } from "../../domain/jsonValue.js"; import { jsonByteLength } from "../../domain/jsonSerialization.js"; import { err, ok, type Result } from "../../domain/result.js"; @@ -133,7 +134,7 @@ export class EnvironmentStateStore { /** Entities served deletes kept for an application that still answers a deleted record by id, oldest first. */ readonly #deleted = new Map(); /** Their UTF-8 bytes, kept within what `data/add` loads by dropping the oldest. */ - #deletedBytes = 0; + readonly #deletedBytes = new Budget(STATE_INTAKE_LIMIT_BYTES); constructor(readonly state: EnvironmentState) { this.#types = new Map( @@ -225,7 +226,7 @@ export class EnvironmentStateStore { this.#knownCollections.clear(); this.#collectionHistoryBytes = 0; this.#deleted.clear(); - this.#deletedBytes = 0; + this.#deletedBytes.restore(0); for (const [type, collections] of this.#typeCollections) for (const collection of collections) this.#rememberCollection(type, collection); @@ -414,15 +415,18 @@ export class EnvironmentStateStore { const earlier = this.#deleted.get(key); if (earlier !== undefined) { this.#deleted.delete(key); - this.#deletedBytes -= jsonByteLength(earlier); - } - this.#deleted.set(key, entity); - this.#deletedBytes += jsonByteLength(entity); - for (const [oldest, dropped] of this.#deleted) { - if (this.#deletedBytes <= STATE_INTAKE_LIMIT_BYTES) break; - this.#deleted.delete(oldest); - this.#deletedBytes -= jsonByteLength(dropped); + this.#deletedBytes.refund(jsonByteLength(earlier)); } + const kept = this.#deletedBytes.chargeEvicting( + jsonByteLength(entity), + () => { + const oldest = this.#deleted.entries().next(); + if (oldest.done === true) return undefined; + this.#deleted.delete(oldest.value[0]); + return jsonByteLength(oldest.value[1]); + }, + ); + if (kept) this.#deleted.set(key, entity); } /** Set a held document's value, unless the state would outgrow what `data/add` loads; a value that is not the Evidence's cites no record. */ diff --git a/src/domain/javascript/javascriptRouteScan.ts b/src/domain/javascript/javascriptRouteScan.ts index ed6f3b5c0b..b9c24a3152 100644 --- a/src/domain/javascript/javascriptRouteScan.ts +++ b/src/domain/javascript/javascriptRouteScan.ts @@ -1,4 +1,5 @@ import * as t from "@babel/types"; +import { Budget } from "../budget.js"; import { jsonValueByteLength } from "../jsonValueByteLength.js"; import type { JsonValue } from "../jsonValue.js"; import { JAVASCRIPT_STATIC_FACT_BYTES } from "./javascriptAnalysisResourceControls.js"; @@ -275,7 +276,8 @@ export class JavaScriptRouteScan { >(); readonly #routeChildren: t.ObjectExpression[] = []; #nextRuntime = false; - #factBytes: number; + /** Coverage metadata, facts and queued work share one allowance. */ + readonly #bytes: Budget; #workBytes = 0; private constructor( @@ -283,7 +285,7 @@ export class JavaScriptRouteScan { readonly onLegacyRoute?: (fact: LegacyRoute) => void, readonly factByteBudget = JAVASCRIPT_STATIC_FACT_BYTES, ) { - this.#factBytes = jsonValueByteLength({ + const metadataBytes = jsonValueByteLength({ candidates: [], unresolved: [], capacity: { @@ -293,13 +295,12 @@ export class JavaScriptRouteScan { accounted_work_bytes: factByteBudget, }, }); - if ( - !Number.isSafeInteger(factByteBudget) || - factByteBudget < this.#factBytes - ) + if (!Number.isSafeInteger(factByteBudget) || factByteBudget < metadataBytes) throw new RangeError( "Route accounting budget must fit its coverage metadata", ); + this.#bytes = new Budget(factByteBudget); + this.#bytes.tryCharge(metadataBytes); } /** Establish navigation data ownership before either adapter interprets syntax. */ @@ -1673,25 +1674,24 @@ export class JavaScriptRouteScan { } } #remaining(): number { - return this.factByteBudget - this.#factBytes - this.#workBytes; + return this.#bytes.remaining; } #capacity(): void { this.#result.capacity ??= { kind: "route-work-or-facts", limit_bytes: this.factByteBudget, - accounted_fact_bytes: this.#factBytes, + accounted_fact_bytes: this.#bytes.used - this.#workBytes, accounted_work_bytes: this.#workBytes, }; } #reserve(kind: "fact" | "work", value: JsonValue): boolean { if (this.#result.capacity !== null) return false; const bytes = jsonValueByteLength(value, this.#remaining()) + 1; - if (bytes > this.#remaining()) { + if (!this.#bytes.tryCharge(bytes)) { this.#capacity(); return false; } - if (kind === "fact") this.#factBytes += bytes; - else this.#workBytes += bytes; + if (kind === "work") this.#workBytes += bytes; return true; } #queue(node: t.Node, value: string | null = null): boolean { diff --git a/src/domain/python/marshalReader.ts b/src/domain/python/marshalReader.ts index 8f0bd17f4a..7a706d3ae3 100644 --- a/src/domain/python/marshalReader.ts +++ b/src/domain/python/marshalReader.ts @@ -1,4 +1,5 @@ import { decodeMarshalUtf8 } from "./marshalUtf8.js"; +import { Budget } from "../budget.js"; import { ParseFailureError } from "../parseFailure.js"; const FLAG_REF = 0x80; @@ -122,14 +123,21 @@ export interface MarshalReaderOptions { */ export abstract class MarshalReader { offset = 0; - objects = 0; depthReached = 0; readonly #refs: (V | undefined)[] = []; + readonly #objects: Budget; constructor( readonly bytes: Buffer, readonly options: MarshalReaderOptions, - ) {} + ) { + this.#objects = new Budget(options.limits.maxObjects); + } + + /** Objects read so far, within `maxObjects`. */ + get objects(): number { + return this.#objects.used; + } /** View a core value as this reader's value type. */ protected abstract wrap(value: MarshalCoreValue): V; @@ -171,9 +179,7 @@ export abstract class MarshalReader { readObject(depth: number): V { const { limits } = this.options; - this.objects += 1; - if (this.objects > limits.maxObjects) - this.limitExceeded({ limit: "objects" }); + if (!this.#objects.tryCharge(1)) this.limitExceeded({ limit: "objects" }); if (depth > limits.maxDepth) this.limitExceeded({ limit: "depth" }); this.depthReached = Math.max(this.depthReached, depth); const raw = this.u8(); diff --git a/src/kuna/KunaRunSlots.ts b/src/kuna/KunaRunSlots.ts index e6a282c70a..aa9adcfb61 100644 --- a/src/kuna/KunaRunSlots.ts +++ b/src/kuna/KunaRunSlots.ts @@ -1,5 +1,7 @@ import { availableParallelism } from "node:os"; +import { Budget } from "../domain/budget.js"; + /** Releases one admitted run's slot; later calls do nothing. */ export type KunaRunRelease = () => void; @@ -13,12 +15,11 @@ export type KunaRunRelease = () => void; * keeps its slot while it cannot prove that the process stopped. */ export class KunaRunSlots { - readonly #capacity: number; - #active = 0; + readonly #slots: Budget; readonly #waiting: (() => void)[] = []; constructor(capacity: number) { - this.#capacity = Math.max(1, capacity); + this.#slots = new Budget(Math.max(1, capacity)); } /** Slots for the cores an export with `jobs` workers leaves on this host. */ @@ -29,14 +30,10 @@ export class KunaRunSlots { /** Wait for a slot; undefined when the signal aborts first. */ acquire(signal: AbortSignal): Promise { if (signal.aborted) return Promise.resolve(undefined); - if (this.#active < this.#capacity) { - this.#active += 1; - return Promise.resolve(this.#release()); - } + if (this.#slots.tryCharge(1)) return Promise.resolve(this.#release()); return new Promise((resolve) => { const grant = () => { signal.removeEventListener("abort", onAbort); - this.#active += 1; resolve(this.#release()); }; const onAbort = () => { @@ -54,8 +51,10 @@ export class KunaRunSlots { return () => { if (released) return; released = true; - this.#active -= 1; - this.#waiting.shift()?.(); + // A waiter takes the released slot over; otherwise it is returned. + const next = this.#waiting.shift(); + if (next === undefined) this.#slots.refund(1); + else next(); }; } } diff --git a/src/process/OwnedMcpStdioTransport.ts b/src/process/OwnedMcpStdioTransport.ts index 435053971b..b437828f54 100644 --- a/src/process/OwnedMcpStdioTransport.ts +++ b/src/process/OwnedMcpStdioTransport.ts @@ -14,6 +14,7 @@ import type { SpawnedOwnedProviderProcess, } from "./ProviderProcess.js"; import type { AnalysisCapturedOutput } from "../domain/analysisErrorBase.js"; +import { Budget } from "../domain/budget.js"; import type { FixedAllowance } from "../domain/fixedAllowanceRemediation.js"; import { ProviderProcessSupervisor, @@ -63,7 +64,8 @@ export class OwnedMcpStdioTransport implements Transport { #supervisor: ProviderProcessSupervisor | undefined; #frameBuffer = Buffer.alloc(0); #frameLength = 0; - #bytes = 0; + /** Output charged to the current operation; stays over once it is exceeded. */ + readonly #bytes: Budget | undefined; #started = false; #closed = false; readonly #cleanup = new AnalysisCleanupLease( @@ -96,7 +98,10 @@ export class OwnedMcpStdioTransport implements Transport { readonly notifyOnExit?: boolean; }, readonly launcher: OwnedMcpLauncher = spawnOwnedProviderProcess, - ) {} + ) { + if (settings.maxOperationBytes !== undefined) + this.#bytes = new Budget(settings.maxOperationBytes); + } /** Acquires exactly one protocol process after the SDK installs callbacks. */ start(): Promise { @@ -225,7 +230,7 @@ export class OwnedMcpStdioTransport implements Transport { /** Scope both the byte budget and diagnostic retention to the next serialized request. */ beginOperation(): void { - this.#bytes = 0; + this.#bytes?.restore(0); this.#supervisor?.resetOutput(); } @@ -354,10 +359,11 @@ export class OwnedMcpStdioTransport implements Transport { }; /** Charge output to the operation budget; false once it is exceeded. */ #countOperationBytes(bytes: number): boolean { - const limit = this.settings.maxOperationBytes; - if (limit === undefined) return true; - this.#bytes += bytes; - if (this.#bytes <= limit) return true; + const budget = this.#bytes; + if (budget === undefined) return true; + budget.chargeOverCommitting(bytes); + if (budget.overCommitted === 0) return true; + const limit = budget.maximum; this.#failLimit( "operation", limit, diff --git a/src/process/capture/ProcessSensitiveOutput.ts b/src/process/capture/ProcessSensitiveOutput.ts index d599d28ed5..a744df28be 100644 --- a/src/process/capture/ProcessSensitiveOutput.ts +++ b/src/process/capture/ProcessSensitiveOutput.ts @@ -1,5 +1,6 @@ import { ProcessAdmissionError } from "../ProcessAdmissionError.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; +import { Budget } from "../../domain/budget.js"; import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; import { err, ok, type Result } from "../../domain/result.js"; import type { ProcessScenario } from "../../domain/process/processScenario.js"; @@ -119,7 +120,7 @@ const visibleUnits = function* (parts: readonly string[]) { /** One run's exact, explicit literals; never shared across captures. */ export class ProcessSensitiveOutput { #index = new Uint32Array(0); - #work = 0; + readonly #work = new Budget(MAX_PROJECTION_WORK); #failure: AnalysisError | undefined; constructor(private readonly prefixes: readonly Prefix[]) {} @@ -140,9 +141,13 @@ export class ProcessSensitiveOutput { } #consume(units: number): void { - this.#work += units; - if (this.#work > MAX_PROJECTION_WORK) - throw capacity("projection_work_units", this.#work, MAX_PROJECTION_WORK); + this.#work.charge(units, () => + capacity( + "projection_work_units", + this.#work.used + units, + MAX_PROJECTION_WORK, + ), + ); } #advance(state: number, code: number): number { diff --git a/src/server/http/NodeHttpRequestOwner.ts b/src/server/http/NodeHttpRequestOwner.ts index 151ed453c4..866f8301e7 100644 --- a/src/server/http/NodeHttpRequestOwner.ts +++ b/src/server/http/NodeHttpRequestOwner.ts @@ -1,6 +1,7 @@ import type { IncomingMessage, ServerResponse } from "node:http"; import { writeWebResponse } from "./nodeHttpBridge.js"; +import { Budget } from "../../domain/budget.js"; import { ProviderCleanupError } from "../../domain/providerCleanupError.js"; const HTTP_RESPONSE_SOURCE = "MCP HTTP response source"; @@ -26,19 +27,20 @@ interface NodeHttpRequestTarget { /** Own admitted exchanges and separately joinable response-source cleanup. */ export class NodeHttpRequestOwner { - #active = 0; + readonly #dispatches: Budget; readonly #exchanges = new Set>(); readonly #bodyCleanups = new Set>(); readonly #cleanupFailures: unknown[] = []; constructor( - private readonly capacity: number, + capacity: number, private readonly target: NodeHttpRequestTarget, ) { if (!Number.isSafeInteger(capacity) || capacity < 1) throw new RangeError( "maxConcurrentDispatches must be a positive safe integer", ); + this.#dispatches = new Budget(capacity); } handle(request: IncomingMessage, response: ServerResponse): Promise { @@ -51,9 +53,9 @@ export class NodeHttpRequestOwner { const refusal = this.#cleanupFailures.length > 0 ? "cleanup" - : this.#active >= this.capacity - ? "capacity" - : undefined; + : this.#dispatches.tryCharge(1) + ? undefined + : "capacity"; if (refusal !== undefined) { return this.#trackExchange(async () => { try { @@ -66,7 +68,6 @@ export class NodeHttpRequestOwner { } }); } - this.#active += 1; let reserved = true; let dispatched = false; let inputDone = false; @@ -77,7 +78,7 @@ export class NodeHttpRequestOwner { const release = (): void => { if (!reserved) return; reserved = false; - this.#active -= 1; + this.#dispatches.refund(1); request.off("end", inputEnded); request.off("close", inputEnded); response.off("close", inputEnded);