refactor(domain): charge seven remaining hand-written allowances through Budget (#11419)

* refactor(domain): charge seven remaining hand-written allowances through Budget

Projection work, Kuna run slots, HTTP dispatch slots, marshal object counts, kept deleted environment entities, MCP operation output and route-scan facts and work now charge Budget instead of adjusting a field and comparing it with a limit. The budget-arithmetic baseline drops from 11 to 4; the four left are a scan cursor, a token string and two output tallies that admit then trim, which are not allowances.

* fix(process): omit the verifier transport's operation byte limit instead of passing Infinity
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 04:19:46 +08:00
1 parent 62d6cfac92
commit 80486e2d34
9 files changed
+78 -64

No files matched your search

+2 -2
View File
@@ -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,
});
@@ -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
}
@@ -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<string, StateEntity>();
/** 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. */
+11 -11
View File
@@ -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 {
+11 -5
View File
@@ -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<V extends { readonly kind: string }> {
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>): V;
@@ -171,9 +179,7 @@ export abstract class MarshalReader<V extends { readonly kind: string }> {
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();
+9 -10
View File
@@ -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<KunaRunRelease | undefined> {
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();
};
}
}
+13 -7
View File
@@ -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<void> {
@@ -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,
@@ -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 {
+8 -7
View File
@@ -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<Promise<void>>();
readonly #bodyCleanups = new Set<Promise<void>>();
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<void> {
@@ -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);