refactor(mcp): make analysis states unrepresentable (#468)

* refactor(config): parse capability settings into exact states

* refactor(runtime): encode replay and process outcomes exactly

* refactor: make analysis states unrepresentable

* refactor(mcp): register canonical schemas with SDK

* fix: preserve capability and catalog invariants

* fix(build): remove stale compiled modules

* fix(replay): update real verifier policy dependency

* fix(package): verify the complete MCP catalog

* fix(test): supply complete replay reload policy

* refactor(contracts): centralize output schema lookup

* refactor(mcp): delegate protocol boundaries to SDK

* fix(browser): approve real source capture

* fix(mcp): preserve catalog guidance
This commit is contained in:
morluto
2026-08-10 02:28:11 +08:00
committed by GitHub
parent 6901edc05d
commit b8a9da0c0f
271 changed files with 5970 additions and 5699 deletions
@@ -59,9 +59,12 @@ describe("browser runtime configuration", () => {
const result = parseConfig(environment);
if (!result.ok) throw result.error;
expect(result.value.browserScenarioPolicy).toEqual({
enabled: true,
executableRoots: ["/opt/chromium"],
cdpEndpoints: ["http://127.0.0.1:9222"],
status: "enabled",
target: {
access: "launch-or-attach",
executableRoots: ["/opt/chromium"],
cdpEndpoints: ["http://127.0.0.1:9222"],
},
allowedOrigins: ["https://app.example.test"],
allowedEnvironment: ["REA_TEST_PASSWORD"],
});
@@ -89,7 +92,74 @@ describe("browser runtime configuration", () => {
}),
);
});
});
describe("browser scenario policy states", () => {
it("parses each legal browser-scenario access state", () => {
const launch = parseConfig({
REA_BROWSER_SCENARIO_ENABLED: "true",
REA_BROWSER_SCENARIO_EXECUTABLE_ROOTS_JSON: '["/opt/chromium"]',
REA_BROWSER_SCENARIO_ALLOWED_ORIGINS_JSON: '["https://app.example.test"]',
});
expect(launch).toMatchObject({
ok: true,
value: {
browserScenarioPolicy: {
status: "enabled",
target: { access: "launch" },
},
},
});
const attach = parseConfig({
REA_BROWSER_SCENARIO_ENABLED: "true",
REA_BROWSER_SCENARIO_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9222"]',
REA_BROWSER_SCENARIO_ALLOWED_ORIGINS_JSON: '["https://app.example.test"]',
});
expect(attach).toMatchObject({
ok: true,
value: {
browserScenarioPolicy: {
status: "enabled",
target: { access: "attach" },
},
},
});
});
it("rejects enabled browser scenarios without an origin or access path", () => {
expect(
parseConfig({
REA_BROWSER_SCENARIO_ENABLED: "true",
REA_BROWSER_SCENARIO_EXECUTABLE_ROOTS_JSON: '["/opt/chromium"]',
}).ok,
).toBe(false);
expect(
parseConfig({
REA_BROWSER_SCENARIO_ENABLED: "true",
REA_BROWSER_SCENARIO_ALLOWED_ORIGINS_JSON:
'["https://app.example.test"]',
}).ok,
).toBe(false);
});
it("drops inactive browser-scenario settings from the parsed policy", () => {
expect(
parseConfig({
REA_BROWSER_SCENARIO_EXECUTABLE_ROOTS_JSON: '["/ignored/chromium"]',
REA_BROWSER_SCENARIO_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9222"]',
REA_BROWSER_SCENARIO_ALLOWED_ORIGINS_JSON:
'["https://ignored.example.test"]',
REA_BROWSER_SCENARIO_ALLOWED_ENV_JSON: '["IGNORED_SECRET"]',
}),
).toMatchObject({
ok: true,
value: { browserScenarioPolicy: { status: "disabled" } },
});
});
});
describe("browser runtime input scopes", () => {
it.each([
'["http://localhost:9222"]',
'["http://192.168.1.5:9222"]',
@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { parseConfig } from "../../../src/config.js";
describe("capability flags", () => {
it("rejects values outside the exact true/false boundary", () => {
const result = parseConfig({
REA_PROCESS_CAPTURE_ENABLED: "typo",
REA_BROWSER_OBSERVE_ENABLED: "typo",
REA_BROWSER_SCENARIO_ENABLED: "typo",
REA_ELECTRON_OBSERVE_ENABLED: "typo",
REA_ELECTRON_AUTOMATE_ENABLED: "typo",
REA_V8_INSPECTOR_OBSERVE_ENABLED: "typo",
REA_JAVASCRIPT_REPLAY_ENABLED: "typo",
REA_MANAGED_RUNTIME_ENABLED: "typo",
});
expect(result.ok).toBe(false);
});
});
@@ -29,36 +29,26 @@ describe("runtime configuration", () => {
maxDepth: 32,
maxPathBytes: 4_096,
});
expect(result.value.browserObservationEnabled).toBe(false);
expect(result.value.browserCdpEndpoints).toEqual([]);
expect(result.value.browserAllowedOrigins).toEqual([]);
expect(result.value.browserObservationPolicy).toEqual({
status: "disabled",
});
expect(result.value.browserScenarioPolicy).toEqual({
enabled: false,
executableRoots: [],
cdpEndpoints: [],
allowedOrigins: [],
allowedEnvironment: [],
status: "disabled",
});
expect(result.value.electronObservationPolicy).toEqual({
status: "disabled",
});
expect(result.value.electronObservationEnabled).toBe(false);
expect(result.value.electronCdpEndpoints).toEqual([]);
expect(result.value.electronFileRoots).toEqual([]);
expect(result.value.electronAutomationPolicy).toEqual({
enabled: false,
executableRoots: [],
applicationRoots: [],
status: "disabled",
});
expect(result.value.v8InspectorObservationEnabled).toBe(false);
expect(result.value.v8InspectorEndpoints).toEqual([]);
expect(result.value.v8InspectorFileRoots).toEqual([]);
expect(result.value.v8InspectorAllowedOrigins).toEqual([]);
expect(result.value.javascriptReplayPolicy).toMatchObject({
enabled: false,
roots: [],
expect(result.value.v8InspectorObservationPolicy).toEqual({
status: "disabled",
});
expect(result.value.managedRuntimePolicy).toMatchObject({
enabled: false,
roots: [],
executablePath: "/usr/bin/dotnet",
expect(result.value.javascriptReplayPolicy).toEqual({
status: "disabled",
});
expect(result.value.managedRuntimePolicy).toEqual({
status: "disabled",
});
}
});
@@ -161,7 +151,7 @@ describe("active Electron permission configuration", () => {
});
if (!result.ok) throw result.error;
expect(result.value.electronAutomationPolicy).toEqual({
enabled: true,
status: "enabled",
executableRoots: ["/opt/electron"],
applicationRoots: ["/tmp/apps"],
});
@@ -204,6 +194,30 @@ describe("active Electron permission configuration", () => {
}).ok,
).toBe(false);
});
it("requires both root sets only for enabled Electron automation", () => {
expect(
parseConfig({
REA_ELECTRON_AUTOMATE_ENABLED: "true",
REA_ELECTRON_AUTOMATE_APPLICATION_ROOTS_JSON: '["/tmp/apps"]',
}).ok,
).toBe(false);
expect(
parseConfig({
REA_ELECTRON_AUTOMATE_ENABLED: "true",
REA_ELECTRON_AUTOMATE_EXECUTABLE_ROOTS_JSON: '["/opt/electron"]',
}).ok,
).toBe(false);
expect(
parseConfig({
REA_ELECTRON_AUTOMATE_EXECUTABLE_ROOTS_JSON: '["/ignored/bin"]',
REA_ELECTRON_AUTOMATE_APPLICATION_ROOTS_JSON: '["/ignored/app"]',
}),
).toMatchObject({
ok: true,
value: { electronAutomationPolicy: { status: "disabled" } },
});
});
});
describe("runtime permission configuration", () => {
@@ -245,6 +259,11 @@ describe("runtime permission configuration", () => {
REA_JAVASCRIPT_REPLAY_NODE_PATH: "/opt/node/bin/node",
});
if (!result.ok) throw result.error;
expect(result.value.javascriptReplayPolicy).toMatchObject({
status: "enabled",
roots: ["/tmp/extracted-modules"],
nodePath: "/opt/node/bin/node",
});
expect(result.value.permissionCeilings).toContainEqual({
capability: "javascript_replay",
roots: ["/tmp/extracted-modules"],
@@ -267,6 +286,24 @@ describe("runtime permission configuration", () => {
);
});
it("requires a source root when JavaScript replay is enabled", () => {
expect(
parseConfig({ REA_JAVASCRIPT_REPLAY_ENABLED: "true" }),
).toMatchObject({ ok: false });
});
it("does not retain dormant JavaScript replay authority", () => {
const result = parseConfig({
REA_JAVASCRIPT_REPLAY_ENABLED: "false",
REA_JAVASCRIPT_REPLAY_ROOTS_JSON: '["/tmp/extracted-modules"]',
REA_JAVASCRIPT_REPLAY_NODE_PATH: "/opt/node/bin/node",
});
if (!result.ok) throw result.error;
expect(result.value.javascriptReplayPolicy).toEqual({
status: "disabled",
});
});
it("rejects relative JavaScript replay roots and executables", () => {
expect(
parseConfig({ REA_JAVASCRIPT_REPLAY_ROOTS_JSON: '["relative/module"]' })
@@ -287,7 +324,7 @@ describe("runtime target configuration", () => {
});
if (!result.ok) throw result.error;
expect(result.value.managedRuntimePolicy).toEqual({
enabled: true,
status: "enabled",
roots: ["/tmp/managed-targets"],
executablePath: "/opt/dotnet/dotnet",
});
@@ -318,6 +355,23 @@ describe("runtime target configuration", () => {
).toBe(false);
});
it("requires a non-empty root set only for enabled managed runtime correlation", () => {
const enabled = parseConfig({ REA_MANAGED_RUNTIME_ENABLED: "true" });
expect(enabled.ok).toBe(false);
if (!enabled.ok)
expect(enabled.error.message).toContain("at least one absolute root");
expect(
parseConfig({
REA_MANAGED_RUNTIME_ROOTS_JSON: '["/ignored/while-disabled"]',
REA_MANAGED_RUNTIME_EXECUTABLE_PATH: "/ignored/dotnet",
}),
).toMatchObject({
ok: true,
value: { managedRuntimePolicy: { status: "disabled" } },
});
});
it("rejects relative Electron file roots", () => {
expect(
parseConfig({ REA_ELECTRON_FILE_ROOTS_JSON: '["relative/app"]' }).ok,
@@ -344,11 +398,7 @@ describe("runtime target configuration", () => {
logLevel: "info",
artifactNativeMountEnabled: false,
processExecutionPolicy: {
enabled: false,
executableRoots: [],
workingRoots: [],
allowedEnvironment: [],
allowExternalNetwork: false,
status: "disabled",
},
evidenceFilePolicy: {
roots: [],
@@ -0,0 +1,153 @@
import { describe, expect, it } from "vitest";
import { parseConfig } from "../../../src/config.js";
describe("passive browser observation policy", () => {
it("requires both endpoint and origin scopes when enabled", () => {
expect(
parseConfig({
REA_BROWSER_OBSERVE_ENABLED: "true",
REA_BROWSER_ALLOWED_ORIGINS_JSON: '["https://app.example.test"]',
}).ok,
).toBe(false);
expect(
parseConfig({
REA_BROWSER_OBSERVE_ENABLED: "true",
REA_BROWSER_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9222"]',
}).ok,
).toBe(false);
});
it("parses one enabled policy and drops inactive settings", () => {
expect(
parseConfig({
REA_BROWSER_OBSERVE_ENABLED: "true",
REA_BROWSER_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9222"]',
REA_BROWSER_ALLOWED_ORIGINS_JSON: '["https://app.example.test"]',
}),
).toMatchObject({
ok: true,
value: {
browserObservationPolicy: {
status: "enabled",
cdpEndpoints: ["http://127.0.0.1:9222"],
allowedOrigins: ["https://app.example.test"],
},
},
});
expect(
parseConfig({
REA_BROWSER_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9222"]',
REA_BROWSER_ALLOWED_ORIGINS_JSON: '["https://ignored.example.test"]',
}),
).toMatchObject({
ok: true,
value: { browserObservationPolicy: { status: "disabled" } },
});
});
});
describe("passive Electron observation policy", () => {
it("requires both endpoint and file-root scopes when enabled", () => {
expect(
parseConfig({
REA_ELECTRON_OBSERVE_ENABLED: "true",
REA_ELECTRON_FILE_ROOTS_JSON: '["/tmp/electron-app"]',
}).ok,
).toBe(false);
expect(
parseConfig({
REA_ELECTRON_OBSERVE_ENABLED: "true",
REA_ELECTRON_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9223"]',
}).ok,
).toBe(false);
});
it("parses one enabled policy and drops inactive settings", () => {
expect(
parseConfig({
REA_ELECTRON_OBSERVE_ENABLED: "true",
REA_ELECTRON_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9223"]',
REA_ELECTRON_FILE_ROOTS_JSON: '["/tmp/electron-app"]',
}),
).toMatchObject({
ok: true,
value: {
electronObservationPolicy: {
status: "enabled",
cdpEndpoints: ["http://127.0.0.1:9223"],
fileRoots: ["/tmp/electron-app"],
},
},
});
expect(
parseConfig({
REA_ELECTRON_CDP_ENDPOINTS_JSON: '["http://127.0.0.1:9223"]',
REA_ELECTRON_FILE_ROOTS_JSON: '["/ignored/electron-app"]',
}),
).toMatchObject({
ok: true,
value: { electronObservationPolicy: { status: "disabled" } },
});
});
});
describe("passive V8 Inspector observation policy", () => {
const endpoint = {
REA_V8_INSPECTOR_OBSERVE_ENABLED: "true",
REA_V8_INSPECTOR_ENDPOINTS_JSON: '["http://127.0.0.1:9229"]',
} as const;
it("requires an endpoint and at least one target-location scope", () => {
expect(
parseConfig({
REA_V8_INSPECTOR_OBSERVE_ENABLED: "true",
REA_V8_INSPECTOR_FILE_ROOTS_JSON: '["/tmp/node-app"]',
}).ok,
).toBe(false);
expect(parseConfig(endpoint).ok).toBe(false);
});
it.each([
["files", { REA_V8_INSPECTOR_FILE_ROOTS_JSON: '["/tmp/node-app"]' }],
[
"origins",
{
REA_V8_INSPECTOR_ALLOWED_ORIGINS_JSON:
'["https://renderer.example.test"]',
},
],
[
"files-and-origins",
{
REA_V8_INSPECTOR_FILE_ROOTS_JSON: '["/tmp/node-app"]',
REA_V8_INSPECTOR_ALLOWED_ORIGINS_JSON:
'["https://renderer.example.test"]',
},
],
] as const)("parses the %s location variant", (scope, environment) => {
expect(parseConfig({ ...endpoint, ...environment })).toMatchObject({
ok: true,
value: {
v8InspectorObservationPolicy: {
status: "enabled",
locations: { scope },
},
},
});
});
it("drops inactive endpoint and location settings", () => {
expect(
parseConfig({
REA_V8_INSPECTOR_ENDPOINTS_JSON: '["http://127.0.0.1:9229"]',
REA_V8_INSPECTOR_FILE_ROOTS_JSON: '["/ignored/node-app"]',
REA_V8_INSPECTOR_ALLOWED_ORIGINS_JSON:
'["https://ignored.example.test"]',
}),
).toMatchObject({
ok: true,
value: { v8InspectorObservationPolicy: { status: "disabled" } },
});
});
});
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { parseConfig } from "../../../src/config.js";
describe("process capture configuration", () => {
it("parses all authority required by enabled capture", () => {
const result = parseConfig({
REA_PROCESS_CAPTURE_ENABLED: "true",
REA_PROCESS_EXECUTABLE_ROOTS_JSON: '["/opt/tools"]',
REA_PROCESS_WORKING_ROOTS_JSON: '["/tmp/work"]',
REA_PROCESS_ALLOWED_ENV_JSON: '["TOKEN"]',
REA_PROCESS_ALLOW_EXTERNAL_NETWORK: "true",
});
if (!result.ok) throw result.error;
expect(result.value.processExecutionPolicy).toEqual({
status: "enabled",
executableRoots: ["/opt/tools"],
workingRoots: ["/tmp/work"],
allowedEnvironment: ["TOKEN"],
networkAccess: "external",
});
});
it("requires both authority roots when capture is enabled", () => {
expect(parseConfig({ REA_PROCESS_CAPTURE_ENABLED: "true" })).toMatchObject({
ok: false,
});
});
it("does not retain dormant process-capture authority", () => {
const result = parseConfig({
REA_PROCESS_CAPTURE_ENABLED: "false",
REA_PROCESS_EXECUTABLE_ROOTS_JSON: '["/opt/tools"]',
REA_PROCESS_WORKING_ROOTS_JSON: '["/tmp/work"]',
REA_PROCESS_ALLOWED_ENV_JSON: '["TOKEN"]',
REA_PROCESS_ALLOW_EXTERNAL_NETWORK: "true",
});
if (!result.ok) throw result.error;
expect(result.value.processExecutionPolicy).toEqual({
status: "disabled",
});
});
});