mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* 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
144 lines
5.8 KiB
TypeScript
144 lines
5.8 KiB
TypeScript
import { z } from "zod";
|
||
import { Budget } from "../budget.js";
|
||
import type { AnalysisCapturedOutput } from "../analysisErrorBase.js";
|
||
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
|
||
import {
|
||
DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES,
|
||
MAXIMUM_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES,
|
||
} from "../analysisResourceLimits.js";
|
||
import { analysisSettingLimit } from "../analysisSettingDefinitions.js";
|
||
import type { AnalysisSettingLimit } from "../analysisSettings.js";
|
||
|
||
/** Launch seed for CLI and future REA processes. */
|
||
export const JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES_ENVIRONMENT =
|
||
"REA_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES";
|
||
|
||
/** Reported-limit key that names this allowance across worker boundaries. */
|
||
export const JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT =
|
||
"javascript_member_mutation_alias_state_limit";
|
||
|
||
/**
|
||
* The analysis phase a work allowance refused. Facts that depend on it were
|
||
* not examined; syntax facts gathered before it remain.
|
||
*/
|
||
export const javaScriptRefusedPhaseSchema = z.literal("member-mutations");
|
||
|
||
/** The effective allowance for one operation and where it came from. */
|
||
export interface JavaScriptMemberMutationAllowance {
|
||
readonly aliasStates: number;
|
||
readonly source: NonNullable<AnalysisSettingLimit["source"]>;
|
||
}
|
||
|
||
export const DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALLOWANCE: JavaScriptMemberMutationAllowance =
|
||
{
|
||
aliasStates: DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES,
|
||
source: "default",
|
||
};
|
||
|
||
/** Worker request field: the parent's resolved allowance for one source. */
|
||
export const javaScriptMemberMutationAllowanceSchema = z.strictObject({
|
||
alias_states: z
|
||
.int()
|
||
.min(1)
|
||
.max(MAXIMUM_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES),
|
||
source: z.enum(["default", "setting", "runtime", "request"]),
|
||
});
|
||
type JavaScriptMemberMutationAllowanceField = z.output<
|
||
typeof javaScriptMemberMutationAllowanceSchema
|
||
>;
|
||
|
||
/** Encode an allowance for a worker request. */
|
||
export const javaScriptMemberMutationAllowanceField = (
|
||
allowance: JavaScriptMemberMutationAllowance,
|
||
): JavaScriptMemberMutationAllowanceField => ({
|
||
alias_states: allowance.aliasStates,
|
||
source: allowance.source,
|
||
});
|
||
|
||
/** Decode the allowance a worker request carries. */
|
||
export const javaScriptMemberMutationAllowanceFromField = (
|
||
field: JavaScriptMemberMutationAllowanceField,
|
||
): JavaScriptMemberMutationAllowance => ({
|
||
aliasStates: field.alias_states,
|
||
source: field.source,
|
||
});
|
||
|
||
const recovery = (allowance: JavaScriptMemberMutationAllowance): string => {
|
||
const current = `The effective javascript.member_mutation_alias_states value is ${String(allowance.aliasStates)} alias states (source ${allowance.source}; default ${String(DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES)}, maximum ${String(MAXIMUM_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES)}).`;
|
||
const change =
|
||
allowance.aliasStates >= MAXIMUM_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES
|
||
? "It is already at the maximum; do not increase it further. Select another complete source within the allowance."
|
||
: `Raise it with update_analysis_settings on MCP, or ${JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES_ENVIRONMENT} for CLI or future REA processes, then retry. Each scheduled state retains about 1–2 KiB of worker heap until its query finishes, so raise max_heap_mb with it (about 2 MiB per 1,024 states).`;
|
||
return `${current} ${change} max_heap_mb, analysis_timeout_ms and semantic_precision alone do not admit the same source. Repeating the unchanged request fails the same way.`;
|
||
};
|
||
|
||
/**
|
||
* The refusal for a source whose member-mutation phase exceeded the allowance.
|
||
* Worker parents rebuild it from the allowance they sent, so every adapter
|
||
* reports the same setting, value and recovery.
|
||
*/
|
||
export const javaScriptMemberMutationRefusal = (
|
||
operation: string,
|
||
allowance: JavaScriptMemberMutationAllowance,
|
||
limits: Readonly<Record<string, unknown>> = {},
|
||
capturedOutput?: AnalysisCapturedOutput,
|
||
): AnalysisResourceConstraintError => {
|
||
const remediationAction = recovery(allowance);
|
||
return new AnalysisResourceConstraintError(
|
||
operation,
|
||
"cpu",
|
||
`JavaScript member-mutation analysis scheduled more than ${String(allowance.aliasStates)} alias states for one source`,
|
||
{
|
||
...limits,
|
||
[JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT]: allowance.aliasStates,
|
||
},
|
||
{
|
||
retryable: false,
|
||
remediationAction,
|
||
limit: analysisSettingLimit(
|
||
"javascript.member_mutation_alias_states",
|
||
allowance.aliasStates,
|
||
allowance.source,
|
||
),
|
||
...(capturedOutput === undefined ? {} : { capturedOutput }),
|
||
},
|
||
);
|
||
};
|
||
|
||
/** Whether a thrown value is this allowance's refusal. */
|
||
export const isJavaScriptMemberMutationRefusal = (
|
||
cause: unknown,
|
||
): cause is AnalysisResourceConstraintError =>
|
||
cause instanceof AnalysisResourceConstraintError &&
|
||
reportsJavaScriptMemberMutationRefusal(cause.reportedLimits);
|
||
|
||
/** Whether a worker reported this allowance as the limit it reached. */
|
||
export const reportsJavaScriptMemberMutationRefusal = (
|
||
limits: Readonly<Record<string, unknown>> | null | undefined,
|
||
): boolean =>
|
||
typeof limits?.[JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT] === "number";
|
||
|
||
/**
|
||
* Work meter for one analysis phase. Charging past the allowance refuses the
|
||
* phase before its retained state can exhaust the worker heap; a partial
|
||
* mutation index would let later reads keep stale values.
|
||
*/
|
||
export class JavaScriptSemanticWorkMeter {
|
||
readonly #aliasStates: Budget;
|
||
|
||
constructor(
|
||
readonly allowance: JavaScriptMemberMutationAllowance = DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALLOWANCE,
|
||
) {
|
||
this.#aliasStates = new Budget(allowance.aliasStates);
|
||
}
|
||
|
||
charge(units = 1): void {
|
||
this.#aliasStates.charge(units, () =>
|
||
javaScriptMemberMutationRefusal(
|
||
"analyze_javascript_semantics",
|
||
this.allowance,
|
||
),
|
||
);
|
||
}
|
||
}
|