mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
feat(javascript): make the member-mutation work allowance a runtime setting (#11181)
* feat(javascript): make the member-mutation work allowance a runtime setting The member-mutation alias-state allowance from #11144 was a fixed constant, so a refused source could not be admitted without a code change. Register javascript.member_mutation_alias_states (default 524288, 1 to 8388608 alias states) with the launch seed REA_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES. The application, web bundle and reference-source boundaries resolve it from the accepted operation snapshot and launch environment, refuse a malformed seed before analysis, and send it to the workers with each source. A refusal names the setting, its effective value, source and range, and the recovery; worker parents rebuild it from the allowance they sent. Refs #8294 * fix(javascript): keep the setting and recovery of a refused web script A web bundle script refused by the member-mutation allowance kept only its reason and reported limits, so the public unknown lost the setting, value, source and recovery. Carry the refusal's structured limit and remediation on the per-script result and its javascript_analysis unknown, grouping scripts only when that recovery agrees. Assert structured limit facts and the owning formatter's recovery instead of fixed wording. * fix(browser): keep only registered setting limits on a refused web script * feat(javascript): keep syntax facts for a source whose member-mutation phase is refused (#11187) * feat(javascript): keep syntax facts for a source whose member-mutation phase is refused A source refused by the member-mutation work allowance during static analysis lost every fact, so the application graph counted it as skipped. Reference-source import failed outright on such a file. Checkpoint the parser, comment and text facts the static owner can still state, with parse status partial, examination not-examined and refused_phase member-mutations, before reporting the refusal; the semantic phase is not started because it indexes the same mutations. Reference-source import keeps the file's declaration-level imports and records that its CommonJS require calls were not examined. Refs #8294 * test(javascript): compare the reference-source refusal with its owning formatter * feat(javascript): keep a refused web script's parse and text facts A captured script whose member-mutation phase was refused lost every fact in analyze_web_bundle, so it was neither counted as parsed nor kept its vendor markers. The analyzer now returns the script as unavailable with partial facts: refused phase, parse status and the inferences read from script text alone. Findings gathered before the refusal are dropped, because they may have read an incomplete mutation index. The worker parent states the refusal and its setting recovery from the allowance it sent, through the same helper the isolated analyzer uses for every unavailable script. The bundle counts the script as parsed, keeps its text inferences, and its javascript_analysis unknown carries refused_phase. Refs #8294 * chore(mcp): record the member-mutation setting in the advertised summary * fix(javascript): rebuild a parked source worker when the member-mutation allowance changes A worker parked on a resource scope was reused when heap, timeout and precision matched, but its requests carry the allowance it was built with. After update_analysis_settings accepted a new allowance, the next analysis on that scope kept the old one. The reuse check now compares the allowance's value and source, so a changed setting gets a worker built with it. The web bundle worker already takes the allowance per script.
This commit is contained in:
1 parent
94c260017a
commit
0f53849160
54 files changed
+1490
-214
No files matched your search
@@ -1,60 +1,140 @@
|
||||
import { z } from "zod";
|
||||
import type { AnalysisCapturedOutput } from "../analysisErrorBase.js";
|
||||
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
|
||||
import {
|
||||
fixedAllowanceOptions,
|
||||
type FixedAllowance,
|
||||
} from "../fixedAllowanceRemediation.js";
|
||||
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";
|
||||
|
||||
/**
|
||||
* Alias states one source's member-mutation phase may schedule. Each state is
|
||||
* retained until its query finishes, at roughly 1–2 KiB, so the allowance stays
|
||||
* well below the default 1 GiB worker heap. Published bundles up to 395 KB
|
||||
* schedule at most about 123,000 states.
|
||||
*/
|
||||
export const JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE = 524_288;
|
||||
/** 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";
|
||||
|
||||
const ALLOWANCE_NAME = "The JavaScript member-mutation work allowance";
|
||||
/**
|
||||
* 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");
|
||||
|
||||
/** Fixed allowance facts for a refusal rebuilt from reported limits. */
|
||||
export const javaScriptSemanticWorkAllowance = (
|
||||
limits: Readonly<Record<string, unknown>> | null | undefined,
|
||||
):
|
||||
| { readonly retryable: false; readonly fixedAllowance: FixedAllowance }
|
||||
| undefined => {
|
||||
const limit = limits?.[JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT];
|
||||
return typeof limit === "number"
|
||||
? fixedAllowanceOptions(ALLOWANCE_NAME, "work", {
|
||||
limit,
|
||||
unit: "alias states",
|
||||
})
|
||||
: undefined;
|
||||
/** 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.`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Work meter for one analysis phase. Charging past the fixed allowance refuses
|
||||
* the phase before its retained state can exhaust the worker heap; a partial
|
||||
* 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 {
|
||||
#used = 0;
|
||||
|
||||
constructor(
|
||||
readonly allowance: number = JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE,
|
||||
readonly allowance: JavaScriptMemberMutationAllowance = DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALLOWANCE,
|
||||
) {}
|
||||
|
||||
charge(units = 1): void {
|
||||
this.#used += units;
|
||||
if (this.#used <= this.allowance) return;
|
||||
const limits = { [JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT]: this.allowance };
|
||||
throw new AnalysisResourceConstraintError(
|
||||
if (this.#used <= this.allowance.aliasStates) return;
|
||||
throw javaScriptMemberMutationRefusal(
|
||||
"analyze_javascript_semantics",
|
||||
"cpu",
|
||||
`JavaScript member-mutation analysis scheduled more than ${String(this.allowance)} alias states for one source`,
|
||||
limits,
|
||||
javaScriptSemanticWorkAllowance(limits),
|
||||
this.allowance,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user