fix(evidence): retain completed observations on late cancellation

Validate completed reports before cancellation projection, retain valid
Evidence and captured command diagnostics, and convert raw cleanup partials
at their application owner. Record partial Evidence consistently across MCP
handlers. Keep page inspection separate from unfinished bundle analysis.

Refs #4261
This commit is contained in:
morluto committed 2026-10-16 06:46:25 +08:00
1 parent 131fd5ddcd
commit 3b698645a0
39 files changed
+1897 -418

No files matched your search

@@ -76,18 +76,23 @@ it("cancels before invocation and after an acquired observation", async () => {
return Promise.resolve(ok(binaryLayoutFixture()));
},
});
expect(
await service.inspect(
{ path: "/artifacts/source-owned.elf" },
{ signal: controller.signal },
),
).toMatchObject({
const late = await service.inspect(
{ path: "/artifacts/source-owned.elf" },
{ signal: controller.signal },
);
expect(late).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCancelledError",
capturedOutput: binaryLayoutFixture().diagnostics,
},
});
if (!late.ok) {
expect(parseEvidence(late.error.partialObservation)).toMatchObject({
operation: "inspect_binary_layout",
normalized_result: binaryLayoutFixture(),
});
}
const untouched = new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
@@ -142,5 +147,23 @@ it.each(["different-path", "invalid-digest", "outside-range"])(
capturedOutput: fixture.diagnostics,
},
});
const controller = new AbortController();
const lateInvalid = await new BinaryLayoutService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: BINARY_LAYOUT_TEST_PROVIDER,
inspect: () => {
controller.abort();
return Promise.resolve(ok(fixture));
},
}).inspect(
{ path: "/artifacts/source-owned.elf" },
{ signal: controller.signal },
);
expect(lateInvalid).toMatchObject({
ok: false,
error: { _tag: "AnalysisOutputError" },
});
},
);
@@ -14,6 +14,7 @@ import {
} from "../../domain/native/binaryLayout.js";
import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js";
import { err, ok, type Result } from "../../domain/result.js";
import { completeProviderEvidence } from "../ProviderOperationExecution.js";
const OPERATION = "inspect_binary_layout";
@@ -79,8 +80,6 @@ export class BinaryLayoutService {
const outputOptions = diagnostics.success
? { capturedOutput: diagnostics.data }
: undefined;
if (options?.signal?.aborted)
return err(new AnalysisCancelledError(OPERATION, outputOptions));
const report = binaryLayoutSchema.safeParse(inspected.value);
if (!report.success || report.data.artifact.path !== input.data.path)
return err(
@@ -91,26 +90,31 @@ export class BinaryLayoutService {
),
);
const value = report.data;
return ok(
createEvidence(
{
path: value.artifact.path,
format: "elf",
architecture: "x86_64",
sha256: value.artifact.sha256,
},
this.provider.identity,
{
operation: OPERATION,
parameters: input.data,
result: value,
// The decoded report is the only representation; do not repeat it.
rawResult: null,
confidence: "observed",
limitations: value.limitations,
locations: [{ kind: "artifact-path", path: value.artifact.path }],
},
return completeProviderEvidence(
ok(
createEvidence(
{
path: value.artifact.path,
format: "elf",
architecture: "x86_64",
sha256: value.artifact.sha256,
},
this.provider.identity,
{
operation: OPERATION,
parameters: input.data,
result: value,
// The decoded report is the only representation; do not repeat it.
rawResult: null,
confidence: "observed",
limitations: value.limitations,
locations: [{ kind: "artifact-path", path: value.artifact.path }],
},
),
),
OPERATION,
options,
outputOptions,
);
}
}
@@ -6,6 +6,11 @@ import { inspectPeResourcesInputSchema } from "../../domain/native/peResources.j
import { parsePeResources } from "../../native/pe/PeResourceParser.js";
import { peResourceFixture } from "../../native/pe/PeResources.fixture.js";
import { ok } from "../../domain/result.js";
import { parseEvidence } from "../../domain/evidence.js";
import {
AnalysisCancelledError,
AnalysisOutputError,
} from "../../domain/analysisErrorCore.js";
const observedReport = () => {
const { bytes } = peResourceFixture();
@@ -45,7 +50,7 @@ it.each([
},
);
it("discards an observation if the caller cancels while the provider completes", async () => {
it("retains a completed observation on late cancellation and keeps output errors primary", async () => {
const report = await observedReport();
const controller = new AbortController();
const service = new PeResourcesService({
@@ -55,10 +60,34 @@ it("discards an observation if the caller cancels while the provider completes",
return Promise.resolve(ok(report));
},
});
expect(
await service.inspect(
{ path: report.artifact.path },
{ signal: controller.signal },
),
).toMatchObject({ ok: false, error: { _tag: "AnalysisCancelledError" } });
const late = await service.inspect(
{ path: report.artifact.path },
{ signal: controller.signal },
);
expect(late).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
if (!late.ok) {
expect(parseEvidence(late.error.partialObservation)).toMatchObject({
operation: "inspect_pe_resources",
normalized_result: report,
});
}
const malformed = await observedReport();
malformed.artifact.path = "/other.exe";
const outputController = new AbortController();
const invalid = await new PeResourcesService({
identity: PE_RESOURCES_PROVIDER,
inspect: () => {
outputController.abort();
return Promise.resolve(ok(malformed));
},
}).inspect(
{ path: "/fixtures/example.exe" },
{ signal: outputController.signal },
);
expect(invalid.ok).toBe(false);
if (!invalid.ok) expect(invalid.error).toBeInstanceOf(AnalysisOutputError);
});
@@ -16,6 +16,7 @@ import {
} from "../../domain/native/peResources.js";
import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js";
import { err, ok, type Result } from "../../domain/result.js";
import { completeProviderEvidence } from "../ProviderOperationExecution.js";
/** Shared caller validation, output parsing and Evidence provenance for CLI/MCP. */
export class PeResourcesService {
@@ -51,8 +52,6 @@ export class PeResourcesService {
);
const result = await this.provider.inspect(input.data, options);
if (!result.ok) return result;
if (options?.signal?.aborted)
return err(new AnalysisCancelledError(operation));
const report = peResourcesSchema.safeParse(result.value);
if (
!report.success ||
@@ -70,23 +69,27 @@ export class PeResourcesService {
),
);
const value = report.data;
return ok(
createEvidence(
{
path: value.artifact.path,
format: "pe",
sha256: value.artifact.sha256,
},
this.provider.identity,
{
operation,
parameters: input.data,
result: value,
confidence: "observed",
limitations: value.limitations,
locations: [{ kind: "artifact-path", path: value.artifact.path }],
},
return completeProviderEvidence(
ok(
createEvidence(
{
path: value.artifact.path,
format: "pe",
sha256: value.artifact.sha256,
},
this.provider.identity,
{
operation,
parameters: input.data,
result: value,
confidence: "observed",
limitations: value.limitations,
locations: [{ kind: "artifact-path", path: value.artifact.path }],
},
),
),
operation,
options,
);
}
}
@@ -105,18 +105,23 @@ it("cancels before invocation and after an acquired observation", async () => {
return Promise.resolve(ok(recordedCrashFixture()));
},
});
expect(
await service.inspect(
{ path: "/artifacts/source-owned.core" },
{ signal: controller.signal },
),
).toMatchObject({
const late = await service.inspect(
{ path: "/artifacts/source-owned.core" },
{ signal: controller.signal },
);
expect(late).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCancelledError",
capturedOutput: recordedCrashFixture().diagnostics,
},
});
if (!late.ok) {
expect(parseEvidence(late.error.partialObservation)).toMatchObject({
operation: "inspect_recorded_crash",
normalized_result: recordedCrashFixture(),
});
}
const untouched = new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
@@ -170,5 +175,23 @@ it.each(["different-path", "invalid-digest", "outside-range"])(
capturedOutput: fixture.diagnostics,
},
});
const controller = new AbortController();
const lateInvalid = await new RecordedCrashService({
inspectAndClose(input, options) {
return this.inspect(input, options);
},
identity: RECORDED_CRASH_TEST_PROVIDER,
inspect: () => {
controller.abort();
return Promise.resolve(ok(fixture));
},
}).inspect(
{ path: "/artifacts/source-owned.core" },
{ signal: controller.signal },
);
expect(lateInvalid).toMatchObject({
ok: false,
error: { _tag: "AnalysisOutputError" },
});
},
);
@@ -15,6 +15,7 @@ import {
} from "../../domain/native/recordedCrash.js";
import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js";
import { err, ok, type Result } from "../../domain/result.js";
import { completeProviderEvidence } from "../ProviderOperationExecution.js";
const OPERATION = "inspect_recorded_crash";
@@ -80,8 +81,6 @@ export class RecordedCrashService {
const outputOptions = diagnostics.success
? { capturedOutput: diagnostics.data }
: undefined;
if (options?.signal?.aborted)
return err(new AnalysisCancelledError(OPERATION, outputOptions));
const report = recordedCrashSchema.safeParse(inspected.value);
if (
!report.success ||
@@ -97,29 +96,34 @@ export class RecordedCrashService {
),
);
const value = report.data;
return ok(
createEvidence(
{
path: value.artifact.path,
format: "elf",
architecture:
RECORDED_LINUX_CORE_PROFILES[value.architecture]
.evidenceArchitecture,
sha256: value.artifact.sha256,
},
this.provider.identity,
{
operation: OPERATION,
parameters: input.data,
result: value,
// The decoded report is the only representation; do not repeat it.
rawResult: null,
confidence:
value.debugger.status === "available" ? "derived" : "observed",
limitations: value.limitations,
locations: [{ kind: "artifact-path", path: value.artifact.path }],
},
return completeProviderEvidence(
ok(
createEvidence(
{
path: value.artifact.path,
format: "elf",
architecture:
RECORDED_LINUX_CORE_PROFILES[value.architecture]
.evidenceArchitecture,
sha256: value.artifact.sha256,
},
this.provider.identity,
{
operation: OPERATION,
parameters: input.data,
result: value,
// The decoded report is the only representation; do not repeat it.
rawResult: null,
confidence:
value.debugger.status === "available" ? "derived" : "observed",
limitations: value.limitations,
locations: [{ kind: "artifact-path", path: value.artifact.path }],
},
),
),
OPERATION,
options,
outputOptions,
);
}
}