fix(process): preserve partial evidence and verify detached cleanup (#1011)
* test(ci): allow transport delivery in source identity regression * fix(process): preserve partial evidence and verify detached cleanup * fix(process): scope Linux token sweeps by stable process identity * fix(process): retain environment permission failure codes * fix(process): wait for signaled processes before verifying cleanup * fix(process): verify sampled detached groups during cleanup * fix(electron): preserve prelaunch identities through cleanup * fix(process): retry growing Darwin process table snapshots * fix(process): retain compatibility with saved v3 capture evidence * fix(process): migrate saved v3 captures during reconstruction verification * fix(process): preserve cancellation during Darwin helper compilation * fix(process): propagate cancellation through ownership baselines * perf(javascript): cache canonical export comparison sort keys * fix(process): cancel provider ownership preparation before spawn * fix(process): isolate native compiler waiter cancellation
This commit is contained in:
@@ -8,6 +8,7 @@ import type { AnalysisClient } from "../../../src/application/AnalysisProvider.j
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { ARTIFACT_COMPARISON_EXAMPLE } from "../../../src/contracts/artifactComparisonExample.js";
|
||||
import { PROCESS_CAPTURE_REFERENCE } from "../../../src/contracts/investigationExamples.js";
|
||||
import { EMPTY_PROCESS_CAPTURE_EXAMPLE } from "../../../src/domain/process/processCaptureExample.js";
|
||||
import { createEvidence } from "../../../src/domain/evidence.js";
|
||||
import { createArtifactInspection } from "../../../src/domain/artifactInspection.js";
|
||||
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
|
||||
@@ -57,6 +58,36 @@ describe("guided prompt completion from live analysis", () => {
|
||||
});
|
||||
|
||||
describe("guided prompt completion from investigation records", () => {
|
||||
it("completes process captures imported in the original v3 format", async () => {
|
||||
const session = createTestBinarySession(() => client([]));
|
||||
const {
|
||||
selected_executable_sha256: _selectedDigest,
|
||||
executable_identity: _identity,
|
||||
executable_sha256,
|
||||
...legacyManifest
|
||||
} = EMPTY_PROCESS_CAPTURE_EXAMPLE.manifest;
|
||||
const legacyCapture = {
|
||||
...EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
||||
manifest: { ...legacyManifest, executable_sha256 },
|
||||
};
|
||||
const evidence = createEvidence(undefined, fixtureProvider, {
|
||||
operation: "capture_process_scenario",
|
||||
parameters: {},
|
||||
result: legacyCapture,
|
||||
confidence: "observed",
|
||||
authority: "controlled-replay",
|
||||
environment: fixtureEnvironment,
|
||||
});
|
||||
expect(session.recordEvidence(evidence).ok).toBe(true);
|
||||
const completion = createPromptCompletionSource(session, session);
|
||||
|
||||
expect(
|
||||
await completion.complete("capture", evidence.evidence_id.slice(0, 8)),
|
||||
).toEqual([evidence.evidence_id]);
|
||||
|
||||
await session.close();
|
||||
});
|
||||
|
||||
it("projects only validated typed identifiers from the evidence ledger", async () => {
|
||||
const session = createTestBinarySession(() => client([]));
|
||||
const invalidCapture = createEvidence(undefined, fixtureProvider, {
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { McpServer } from "@modelcontextprotocol/server";
|
||||
|
||||
import type { ToolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import { err } from "../../../src/domain/result.js";
|
||||
import { ProcessCaptureError } from "../../../src/process/capture/ProcessCaptureError.js";
|
||||
import { resolveProcessResult } from "../../../src/process/capture/ProcessCaptureLifecycle.js";
|
||||
import {
|
||||
toolContract,
|
||||
type ToolContract,
|
||||
} from "../../../src/contracts/toolContracts.js";
|
||||
import { err, ok } from "../../../src/domain/result.js";
|
||||
import { HopperProcessError } from "../../../src/domain/hopperErrors.js";
|
||||
import type { IncompleteProcessCaptureObservations } from "../../../src/domain/process/processCapture.js";
|
||||
import { processScenarioSchema } from "../../../src/domain/process/processScenario.js";
|
||||
import { toCallToolResult } from "../../../src/server/toolResult.js";
|
||||
import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { emptyProcessCapture } from "../../../src/domain/process/processCapture.fixture.js";
|
||||
import type { JsonValue } from "../../../src/domain/jsonValue.js";
|
||||
import { evidenceResultOf } from "../../../src/contracts/toolOutputSchemas.js";
|
||||
import { toolRegistrationOptions } from "../../../src/server/toolRegistrationOptions.js";
|
||||
|
||||
const contract: ToolContract = {
|
||||
name: "provider_neutral_fixture",
|
||||
@@ -36,6 +46,215 @@ const contract: ToolContract = {
|
||||
examples: [{ title: "Example fixture request", input: {} }],
|
||||
};
|
||||
|
||||
describe("completed partial process capture MCP projection", () => {
|
||||
it("preserves partial capture details over the MCP SDK transport", async () => {
|
||||
const executionFailure = new Error("capture ended after a fixture error");
|
||||
const cleanup = {
|
||||
owned_process_group: {
|
||||
state: "unverified" as const,
|
||||
reason: "process ownership token could not be read",
|
||||
},
|
||||
terminal_renderer: { state: "cleaned" as const, reason: null },
|
||||
temporary_root: { state: "cleaned" as const, reason: null },
|
||||
};
|
||||
const capture = {
|
||||
...emptyProcessCapture(),
|
||||
frames: [{ sequence: 0, at_ms: 0, data: "observed output" }],
|
||||
event_journal: [],
|
||||
};
|
||||
|
||||
let failure: ProcessCaptureError | undefined;
|
||||
try {
|
||||
resolveProcessResult(capture, executionFailure, cleanup);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ProcessCaptureError)) throw cause;
|
||||
failure = cause;
|
||||
}
|
||||
expect(failure).toBeDefined();
|
||||
if (failure === undefined)
|
||||
throw new Error("expected cleanup-incomplete result");
|
||||
|
||||
const captureContract = toolContract("capture_process_scenario");
|
||||
const server = new McpServer({
|
||||
name: "process-partial-error",
|
||||
version: "1",
|
||||
});
|
||||
server.registerTool(
|
||||
captureContract.name,
|
||||
toolRegistrationOptions(captureContract),
|
||||
async () => toCallToolResult(err(failure), captureContract),
|
||||
);
|
||||
const client = new Client({
|
||||
name: "process-partial-error-client",
|
||||
version: "1",
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
try {
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
const result = await client.callTool({
|
||||
name: captureContract.name,
|
||||
arguments: captureContract.examples[0]?.input ?? {},
|
||||
});
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.structuredContent).toMatchObject({
|
||||
error: {
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
cleanup_report: cleanup,
|
||||
execution_failure: "capture ended after a fixture error",
|
||||
partial_observation: {
|
||||
capture: {
|
||||
frames: [{ data: "observed output" }],
|
||||
settlement: { cleanup_outcome: "failed" },
|
||||
},
|
||||
execution_failure: "capture ended after a fixture error",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const textContent = result.content.find((block) => block.type === "text");
|
||||
expect(textContent?.type === "text" ? textContent.text : undefined).toBe(
|
||||
JSON.stringify(result.structuredContent),
|
||||
);
|
||||
} finally {
|
||||
await Promise.all([client.close(), server.close()]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("incomplete partial process observations MCP projection", () => {
|
||||
it("preserves normalized raw observations over the MCP SDK transport", async () => {
|
||||
const captureContract = toolContract("capture_process_scenario");
|
||||
const example = captureContract.examples[0];
|
||||
if (example === undefined)
|
||||
throw new Error("missing process scenario example");
|
||||
const scenario = processScenarioSchema.parse({
|
||||
...example.input,
|
||||
normalization: { pids: false },
|
||||
});
|
||||
const rootPid = 45678;
|
||||
const cleanup = {
|
||||
owned_process_group: {
|
||||
state: "unverified" as const,
|
||||
reason: "process ownership token could not be read",
|
||||
},
|
||||
terminal_renderer: { state: "cleaned" as const, reason: null },
|
||||
temporary_root: { state: "cleaned" as const, reason: null },
|
||||
};
|
||||
const observations: IncompleteProcessCaptureObservations = {
|
||||
target_pid: { state: "available", value: rootPid },
|
||||
frames: {
|
||||
state: "available",
|
||||
value: [{ sequence: 0, at_ms: 0, data: "partial output" }],
|
||||
},
|
||||
rendered_frames: {
|
||||
state: "unavailable",
|
||||
reason: "fixture renderer observation unavailable",
|
||||
},
|
||||
interaction_events: { state: "available", value: [] },
|
||||
exit: {
|
||||
state: "unavailable",
|
||||
reason: "fixture terminal exit unavailable",
|
||||
},
|
||||
settlement: {
|
||||
state: "unavailable",
|
||||
reason: "fixture process settlement unavailable",
|
||||
},
|
||||
process_samples: {
|
||||
state: "available",
|
||||
value: [
|
||||
{
|
||||
at_ms: 1,
|
||||
pid: rootPid,
|
||||
parent_pid: 1,
|
||||
command: `fixture ${String(rootPid)}`,
|
||||
process_group_id: rootPid,
|
||||
session_id: rootPid,
|
||||
},
|
||||
],
|
||||
},
|
||||
filesystem_snapshots: {
|
||||
before: { state: "available", value: { files: [], truncated: false } },
|
||||
after: {
|
||||
state: "unavailable",
|
||||
reason: "fixture final snapshot unavailable",
|
||||
},
|
||||
},
|
||||
event_journal: { state: "available", value: [] },
|
||||
manifest: {
|
||||
state: "unavailable",
|
||||
reason: "fixture capture manifest unavailable",
|
||||
},
|
||||
};
|
||||
|
||||
let failure: ProcessCaptureError | undefined;
|
||||
try {
|
||||
resolveProcessResult(
|
||||
undefined,
|
||||
new Error("capture failed before completion"),
|
||||
cleanup,
|
||||
observations,
|
||||
{ scenario, rootPid },
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ProcessCaptureError)) throw cause;
|
||||
failure = cause;
|
||||
}
|
||||
if (failure === undefined)
|
||||
throw new Error("expected cleanup-incomplete result");
|
||||
|
||||
const server = new McpServer({
|
||||
name: "process-partial-observations",
|
||||
version: "1",
|
||||
});
|
||||
server.registerTool(
|
||||
captureContract.name,
|
||||
toolRegistrationOptions(captureContract),
|
||||
async () => toCallToolResult(err(failure), captureContract),
|
||||
);
|
||||
const client = new Client({
|
||||
name: "process-partial-observations-client",
|
||||
version: "1",
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
try {
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
const result = await client.callTool({
|
||||
name: captureContract.name,
|
||||
arguments: example.input,
|
||||
});
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.structuredContent).toMatchObject({
|
||||
error: {
|
||||
details: {
|
||||
partial_observation: {
|
||||
observations: {
|
||||
target_pid: { state: "available", value: rootPid },
|
||||
frames: {
|
||||
state: "available",
|
||||
value: [{ data: "partial output" }],
|
||||
},
|
||||
process_samples: {
|
||||
state: "available",
|
||||
value: [{ pid: rootPid }],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
await Promise.all([client.close(), server.close()]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("tool result projection", () => {
|
||||
it("exposes an actionable adapter code to MCP callers", () => {
|
||||
const result = toCallToolResult(err(new HopperProcessError(76)), contract);
|
||||
|
||||
Reference in New Issue
Block a user