fix(evidence): enforce managed identity and cancel bundle reads

Validate managed reconstruction through the shared artifact and MVID scope boundary, including known/unknown identity contradictions. Forward MCP comparison cancellation into both bundle reads and consolidate pre-cancel coverage.

Verified managed graph/reconstruction and filesystem/MCP cancellation workflows. A controlled missing-signal mutation reads 130 chunks instead of the expected one; cancellation leaves the session unchanged and retry succeeds.
This commit is contained in:
morluto
2026-10-12 13:06:30 +08:00
parent 83a4445f44
commit 3bcc68c037
8 changed files with 329 additions and 38 deletions
+2 -1
View File
@@ -36,8 +36,9 @@ type EvidenceWriteFailure =
/** Read and validate an evidence bundle at the caller-supplied path. */
export const readEvidenceBundle = (
path: string,
signal?: AbortSignal,
): Promise<Result<EvidenceBundle, EvidenceReadFailure>> =>
readBundle(path, parseEvidenceBundle);
readBundle(path, parseEvidenceBundle, signal);
/** Read an authenticated immutable snapshot for a subsequent session import. */
export const readImmutableEvidenceBundle = (
@@ -53,6 +53,33 @@ describe("managed decompiler reconstruction import", () => {
).toThrow(/does not match/u);
});
it("rejects static Evidence with a token MVID but no module identity", () => {
const input = exampleInput();
const members = managedMemberInspectionSchema.parse(
input.static_members.normalized_result,
);
if (members.module === null || members.module.mvid === null)
throw new Error("Expected fixture module identity and MVID");
const inconsistentEvidence = createEvidence(
undefined,
input.static_members.provider,
{
operation: "inspect_managed_members",
parameters: {},
result: { ...members, module: null },
rawResult: null,
limitations: members.limitations,
},
);
expect(() =>
importManagedReconstruction({
...input,
static_members: inconsistentEvidence,
}),
).toThrow(/token scope MVID/u);
});
it("rejects reconstruction locks for partial CIL without normalized identity", () => {
const input = exampleInput();
const observed = managedMemberInspectionSchema.parse(
@@ -43,15 +43,14 @@ export const parseManagedInspectionEvidence = <
throw new TypeError(
`Managed Evidence ${operation} (${evidence.evidence_id}) token scope SHA-256 ${scope.requires_artifact_sha256} does not match normalized artifact SHA-256 ${artifactSha256}`,
);
const moduleMvid = result.module?.mvid ?? null;
const scopedMvid = scope?.requires_mvid;
if (
scope?.requires_mvid !== undefined &&
scope.requires_mvid !== null &&
result.module?.mvid !== undefined &&
result.module.mvid !== null &&
scope.requires_mvid.toLowerCase() !== result.module.mvid.toLowerCase()
scopedMvid !== undefined &&
scopedMvid?.toLowerCase() !== moduleMvid?.toLowerCase()
)
throw new TypeError(
`Managed Evidence ${operation} (${evidence.evidence_id}) token scope MVID ${scope.requires_mvid} does not match module MVID ${result.module.mvid}`,
`Managed Evidence ${operation} (${evidence.evidence_id}) token scope MVID ${String(scopedMvid)} does not match module MVID ${String(moduleMvid)}`,
);
return { evidence, result };
};
+11 -11
View File
@@ -3,15 +3,15 @@ import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import { evidenceSchema, parseEvidence } from "../evidence.js";
import { evidenceSchema, type Evidence } from "../evidence.js";
import {
cliMetadataGuidSchema,
managedMemberInspectionSchema,
type ManagedMemberInspection,
} from "./managedArtifact.js";
import type { JsonValue } from "../jsonValue.js";
import { digestSchema } from "../digests.js";
import { prefixedDigestSchema } from "../digests.js";
import { digestSchema, prefixedDigestSchema } from "../digests.js";
import { parseManagedInspectionEvidence } from "./managedInspectionEvidence.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
const tokenSchema = z.string().regex(/^0x[0-9a-f]{8}$/u);
@@ -210,7 +210,7 @@ const buildReconstructionSummary = (
});
const buildStaticObservation = (
staticEvidence: ReturnType<typeof parseEvidence>,
staticEvidence: Evidence,
members: ManagedMemberInspection,
): StaticObservation => ({
evidence_id: staticEvidence.evidence_id,
@@ -238,7 +238,7 @@ const RECONSTRUCTION_LIMITATIONS = [
const buildReconstructionWithoutId = (
input: ManagedReconstructionImportInput,
staticEvidence: ReturnType<typeof parseEvidence>,
staticEvidence: Evidence,
members: ManagedMemberInspection,
methods: readonly ImportedMethod[],
): Omit<ManagedReconstructionImportResult, "reconstruction_id"> => ({
@@ -257,12 +257,12 @@ const buildReconstructionWithoutId = (
export const importManagedReconstruction = (
input: ManagedReconstructionImportInput,
): ManagedReconstructionImportResult => {
const staticEvidence = parseEvidence(input.static_members);
if (staticEvidence.operation !== "inspect_managed_members")
throw new TypeError("Evidence operation is not inspect_managed_members");
const members = managedMemberInspectionSchema.parse(
staticEvidence.normalized_result,
);
const { evidence: staticEvidence, result: members } =
parseManagedInspectionEvidence(
input.static_members,
"inspect_managed_members",
managedMemberInspectionSchema,
);
const methods = input.methods.map((candidate) => {
const observed = findObservedMethod(members, candidate);
if (observed === undefined)
+2 -2
View File
@@ -31,8 +31,8 @@ export const registerBundleComparisonTool = (
context.mcpReq.signal,
async () => {
const [left, right] = await Promise.all([
readEvidenceBundle(input.left_bundle_path),
readEvidenceBundle(input.right_bundle_path),
readEvidenceBundle(input.left_bundle_path, context.mcpReq.signal),
readEvidenceBundle(input.right_bundle_path, context.mcpReq.signal),
]);
if (!left.ok) return server.delivery.toCallToolResult(left, contract);
if (!right.ok)