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
@@ -10,6 +10,7 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { createElectronEvidence } from "../../../src/application/ElectronEvidence.js";
import { analyzeJavaScriptApplication } from "../../../src/application/JavaScriptApplicationService.js";
import { reconcileJavaScriptRuntime } from "../../../src/domain/javascriptRuntimeReconciliation.js";
import { inspectElectronPageInputSchema } from "../../../src/domain/electronObservation.js";
import { createWebTextArtifact } from "../../../src/domain/webContentArtifact.js";
import { permissionAuthorityForRoot } from "../../fixtures/permissionAuthority.js";
@@ -233,7 +234,7 @@ const electronRuntimeEvidence = (
const includeWorker = options.includeWorker ?? true;
const targetId = options.targetId ?? "target-main";
const sourceCaptureApproved = options.sourceCaptureApproved ?? true;
const input = {
const input = inspectElectronPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9223",
allowed_file_roots: [root],
target_id: targetId,
@@ -250,7 +251,7 @@ const electronRuntimeEvidence = (
max_script_source_bytes: 1_048_576,
max_total_script_source_bytes: 4_194_304,
},
};
});
return createElectronEvidence(
"inspect_electron_page",
input,
@@ -35,11 +35,11 @@ it("does not follow or disclose symlink targets outside declared roots", async (
filesystem_roots: [root],
}),
{
enabled: true,
status: "enabled",
executableRoots: ["/usr/bin"],
workingRoots: [root],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
},
);
expect(result.ok).toBe(true);
@@ -65,11 +65,7 @@ it("does not launch when policy denies capture", async () => {
working_directory: "/tmp",
});
const result = await captureProcessScenario(scenario, {
enabled: false,
executableRoots: [],
workingRoots: [],
allowedEnvironment: [],
allowExternalNetwork: true,
status: "disabled",
});
expect(result.ok).toBe(false);
if (result.ok) throw new Error("expected policy refusal");
@@ -80,11 +76,11 @@ it("distinguishes timeout from cancellation and cleans both runs", async () => {
const capability = await probeProcessCaptureCapability();
if (!capability.available) return;
const policy: ProcessExecutionPolicy = {
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [dirname(processFixture)],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
};
const timedOut = await captureProcessScenario(
parseProcessScenario({
@@ -143,11 +139,11 @@ it("captures source-owned interactive, resize, Unicode, and signal behavior", as
idle_timeout_ms: 2_000,
}),
{
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [dirname(processFixture)],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
},
);
if (!result.ok) throw result.error;
@@ -177,7 +173,7 @@ it("dispatches scheduled events before a silent PTY produces output", async () =
idle_timeout_ms: 2_000,
}),
{
enabled: true,
status: "enabled",
executableRoots: [
join(dirname(process.execPath), "missing"),
dirname(process.execPath),
@@ -187,7 +183,7 @@ it("dispatches scheduled events before a silent PTY produces output", async () =
dirname(processFixture),
],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
},
);
expect(result.ok).toBe(true);
@@ -214,11 +210,11 @@ it("samples and cleans a source-owned child and grandchild process tree", async
idle_timeout_ms: 2_000,
}),
{
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [dirname(processFixture)],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
},
);
expect(result.ok).toBe(true);
@@ -196,11 +196,11 @@ it("runs the committed multi-source reactive fixture deterministically", async (
);
const run = () =>
captureProcessScenario(createMultiSourceScenario(root, script), {
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [root],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
});
try {
const capability = await probeProcessCaptureCapability();
@@ -207,11 +207,11 @@ it("cleans the temporary root when capture home creation fails", async () => {
prepareProcessCapture(
parseProcessScenario(base),
{
enabled: true,
status: "enabled",
executableRoots: ["/bin"],
workingRoots: ["/tmp"],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
},
undefined,
host,
@@ -375,11 +375,11 @@ it("parses bounded scenarios and rejects unordered events", () => {
it("requires explicit operator approval for host network access", () => {
expect(
authorizeProcessScenario(parseProcessScenario(base), {
enabled: true,
status: "enabled",
executableRoots: ["/bin"],
workingRoots: ["/tmp"],
allowedEnvironment: [],
allowExternalNetwork: false,
networkAccess: "none",
}),
).toEqual({
allowed: false,
@@ -394,11 +394,11 @@ it("refuses paths and environment outside operator policy", () => {
});
expect(
authorizeProcessScenario(scenario, {
enabled: true,
status: "enabled",
executableRoots: ["/bin"],
workingRoots: ["/tmp"],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
}),
).toEqual({
allowed: false,
@@ -287,11 +287,11 @@ it("drives a process from terminal observations and retains the reactive transit
const capability = await probeProcessCaptureCapability();
if (!capability.available) return;
const result = await captureProcessScenario(scenario, {
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [root],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
});
if (!result.ok) throw result.error;
assertCompletedCapture(result.value);
@@ -351,11 +351,11 @@ it("records target loss before post-exit settlement can win the deadline race",
const capability = await probeProcessCaptureCapability();
if (!capability.available) return;
const result = await captureProcessScenario(scenario, {
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [root],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
});
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
@@ -119,11 +119,11 @@ it("runs a login and reconnect replay machine inside process capture", async ()
const capability = await probeProcessCaptureCapability();
if (!capability.available) return;
const capture = await captureProcessScenario(scenario, {
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [root],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
});
expect(capture.ok).toBe(true);
if (!capture.ok) throw capture.error;
@@ -172,11 +172,11 @@ it("captures PTY, filesystem, descendants, HTTP replay, and redacts environment"
].join("\n"),
);
const policy: ProcessExecutionPolicy = {
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [root],
allowedEnvironment: ["SECRET"],
allowExternalNetwork: true,
networkAccess: "external",
};
const scenario = parseProcessScenario({
approved: true,
@@ -132,11 +132,11 @@ it("renders terminal state, records shim invocations, and captures literal check
const result = await captureProcessScenario(
buildTerminalScenario(root, script),
{
enabled: true,
status: "enabled",
executableRoots: [dirname(process.execPath)],
workingRoots: [root],
allowedEnvironment: [],
allowExternalNetwork: true,
networkAccess: "external",
},
);
expect(result.ok).toBe(true);
@@ -85,7 +85,7 @@ describe("provider process runtime and wait primitives", () => {
const waiting = deadline.wait(1_000);
controller.abort();
await expect(waiting).resolves.toBe("aborted");
expect(deadline.cancelled).toBe(true);
expect(deadline.interruption).toBe("cancelled");
expect(getEventListeners(controller.signal, "abort")).toHaveLength(0);
expect(vi.getTimerCount()).toBe(0);
deadline.dispose();
@@ -99,7 +99,7 @@ describe("provider process runtime and wait primitives", () => {
await vi.advanceTimersByTimeAsync(100);
await expect(waiting).resolves.toBe("aborted");
expect(deadline.timedOut).toBe(true);
expect(deadline.interruption).toBe("timeout");
expect(deadline.remainingMs()).toBe(0);
deadline.dispose();
expect(vi.getTimerCount()).toBe(0);
@@ -114,8 +114,7 @@ describe("provider process runtime and wait primitives", () => {
controller.abort();
expect(deadline.signal.reason).toMatchObject({ name: "TimeoutError" });
expect(deadline.timedOut).toBe(true);
expect(deadline.cancelled).toBe(false);
expect(deadline.interruption).toBe("timeout");
expect(getEventListeners(controller.signal, "abort")).toHaveLength(0);
expect(vi.getTimerCount()).toBe(0);
deadline.dispose();