fix: retain completed observations when later analysis steps fail
This commit is contained in:
@@ -5,7 +5,9 @@ import { expect, it as test, onTestFinished } from "vitest";
|
||||
import { z } from "zod";
|
||||
import { access } from "node:fs/promises";
|
||||
import { ANDROID_TOOL_CONTRACTS } from "../../../src/contracts/android/androidToolContracts.js";
|
||||
import { androidPartialObservationSchema } from "../../../src/domain/android/androidPartialObservation.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { ToolResultDelivery } from "../../../src/server/toolResult.js";
|
||||
import { startMcpTransport } from "../../../src/main/transport.js";
|
||||
@@ -14,6 +16,7 @@ import type { AndroidAnalysisPort } from "../../../src/application/android/Andro
|
||||
import { silentLogger } from "../../../src/logger.js";
|
||||
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
||||
import {
|
||||
createJadxProtocolFixture,
|
||||
verifyJadxFixtureCleanup,
|
||||
@@ -148,6 +151,137 @@ it("publishes and executes all APK contracts with inline Evidence and no active
|
||||
await verifyJadxFixtureCleanup(launches);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{
|
||||
scenario: "failed upstream reply",
|
||||
mode: "tool-error",
|
||||
errorCode: "execution_failure",
|
||||
serverName: "rea-jadx-bridge",
|
||||
serverVersion: "1",
|
||||
engineVersion: "0.7.1",
|
||||
calls: [
|
||||
{ operation: "rea_jvm_status" },
|
||||
{ operation: "load_apk" },
|
||||
{ operation: "get_app_info" },
|
||||
{
|
||||
operation: "get_android_manifest",
|
||||
response: { isError: true },
|
||||
},
|
||||
],
|
||||
containsAmbientEnvironment: false,
|
||||
},
|
||||
{
|
||||
scenario: "transport error after successful replies",
|
||||
mode: "rpc-disconnect",
|
||||
errorCode: "execution_failure",
|
||||
serverName: "rea-jadx-bridge",
|
||||
serverVersion: "1",
|
||||
engineVersion: "0.7.1",
|
||||
calls: [{ operation: "rea_jvm_status" }, { operation: "load_apk" }],
|
||||
containsAmbientEnvironment: true,
|
||||
},
|
||||
{
|
||||
scenario: "empty handshake identity mismatch",
|
||||
mode: "empty-server-identity",
|
||||
errorCode: "capability_unavailable",
|
||||
serverName: "",
|
||||
serverVersion: "",
|
||||
engineVersion: null,
|
||||
calls: [],
|
||||
containsAmbientEnvironment: true,
|
||||
},
|
||||
])(
|
||||
"retains Android partial observations for $scenario through MCP",
|
||||
async ({
|
||||
mode,
|
||||
errorCode,
|
||||
serverName,
|
||||
serverVersion,
|
||||
engineVersion,
|
||||
calls,
|
||||
containsAmbientEnvironment,
|
||||
}) => {
|
||||
const { provider, apk, launches } = await createJadxProtocolFixture(mode);
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("Unexpected native provider acquisition");
|
||||
});
|
||||
const server = createServer(
|
||||
{ kind: "session", session },
|
||||
{ androidAnalysis: provider },
|
||||
);
|
||||
const client = new Client({
|
||||
name: "android-partial-observation-regression",
|
||||
version: "1",
|
||||
});
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
await session.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
|
||||
const response = await client.callTool({
|
||||
name: "inspect_android_package",
|
||||
arguments: { path: apk },
|
||||
});
|
||||
const projected = parseMcpToolError(response).error;
|
||||
expect(projected.code).toBe(errorCode);
|
||||
const details = z.record(z.string(), z.unknown()).parse(projected.details);
|
||||
const partial = androidPartialObservationSchema.parse(
|
||||
details.partial_observation,
|
||||
);
|
||||
expect(partial).toMatchObject({
|
||||
provider_id: "jadx",
|
||||
operation: "inspect_android_package",
|
||||
target: { selected_path: apk, path: apk, format: "apk" },
|
||||
provider_facts: {
|
||||
bridge_name: "rea-jadx-bridge",
|
||||
bridge_version: "1",
|
||||
server_name: serverName,
|
||||
server_version: serverVersion,
|
||||
engine_version: engineVersion,
|
||||
},
|
||||
calls,
|
||||
});
|
||||
if (!containsAmbientEnvironment)
|
||||
expect(JSON.stringify(partial)).not.toContain("JAVA_HOME");
|
||||
await client.close();
|
||||
await server.close();
|
||||
await verifyJadxFixtureCleanup(launches);
|
||||
},
|
||||
);
|
||||
|
||||
it("retains partial JADX replies when owned cleanup also fails", async () => {
|
||||
const { service, apk } = await createJadxProtocolFixture("cleanup-failure");
|
||||
const result = await service.execute("inspect_android_package", {
|
||||
path: apk,
|
||||
});
|
||||
if (result.ok) throw new Error("Expected the fixture tool to fail");
|
||||
|
||||
const projected = projectAnalysisError(result.error);
|
||||
expect(projected.code).toBe("cleanup_incomplete");
|
||||
expect(projected.details).toMatchObject({
|
||||
cleanup: "incomplete",
|
||||
execution_failure: "execution_failure",
|
||||
partial_observation: {
|
||||
provider_id: "jadx",
|
||||
operation: "inspect_android_package",
|
||||
calls: [
|
||||
{ operation: "rea_jvm_status" },
|
||||
{ operation: "load_apk" },
|
||||
{ operation: "get_app_info" },
|
||||
{
|
||||
operation: "get_android_manifest",
|
||||
response: { isError: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("cleans an active engine when the MCP client disconnects", async () => {
|
||||
const { provider, apk, launches } = await createJadxProtocolFixture("stall");
|
||||
const session = createTestBinarySession(() => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { runDirectAnalysis } from "../../src/application/DirectAnalysis.js";
|
||||
import type {
|
||||
@@ -18,7 +19,11 @@ import {
|
||||
readAnalysisSnapshot,
|
||||
writeAnalysisSnapshot,
|
||||
} from "../../src/application/binary/AnalysisSnapshotFiles.js";
|
||||
import { createEvidence, type Evidence } from "../../src/domain/evidence.js";
|
||||
import {
|
||||
createEvidence,
|
||||
parseEvidence,
|
||||
type Evidence,
|
||||
} from "../../src/domain/evidence.js";
|
||||
import { createEvidenceBundle } from "../../src/domain/evidenceBundle.js";
|
||||
import {
|
||||
REA_WORKFLOW_PROVIDER,
|
||||
@@ -646,7 +651,51 @@ describe("snapshot replay provider identity", () => {
|
||||
});
|
||||
});
|
||||
|
||||
const assertEvidenceSurvivesSnapshotSaveFailure = async (): Promise<void> => {
|
||||
const directory = await createTestTempDirectory("rea-workflow-save-error-");
|
||||
const path = join(directory, "fixture.hop");
|
||||
const snapshotPath = join(directory, "missing", "snapshot.json");
|
||||
await writeFile(path, "fixture");
|
||||
const starts: string[] = [];
|
||||
const calls: string[] = [];
|
||||
const provider = makeProvider(starts, calls);
|
||||
const dependencies: DirectAnalysisDependencies = {
|
||||
readConfiguration: () => parseConfig({}),
|
||||
createBinarySession: () => createTestBinarySession(provider),
|
||||
createManagedBinarySession: () => createTestBinarySession(provider),
|
||||
};
|
||||
|
||||
const result = await runDirectAnalysis(
|
||||
dependencies,
|
||||
path,
|
||||
"binary_overview",
|
||||
{},
|
||||
{ snapshotPath },
|
||||
);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
partial_observation: {
|
||||
operation: "binary_overview",
|
||||
subject: { local_path: path },
|
||||
},
|
||||
},
|
||||
});
|
||||
const partial = z
|
||||
.object({
|
||||
details: z.object({ partial_observation: z.unknown() }),
|
||||
})
|
||||
.parse(result).details.partial_observation;
|
||||
expect(parseEvidence(partial).operation).toBe("binary_overview");
|
||||
expect(calls.toSorted()).toEqual(operations.toSorted());
|
||||
};
|
||||
|
||||
describe("workflow snapshot profile and cancellation binding", () => {
|
||||
it(
|
||||
"preserves completed Evidence when CLI snapshot persistence fails",
|
||||
assertEvidenceSurvivesSnapshotSaveFailure,
|
||||
);
|
||||
it("does not replay a valid entry from a different workflow profile", async () => {
|
||||
const directory = await createTestTempDirectory("rea-workflow-profile-");
|
||||
const path = join(directory, "fixture.hop");
|
||||
|
||||
Vendored
+8
-6
@@ -6,8 +6,9 @@ import { z } from "zod";
|
||||
// It is a protocol fixture, not evidence of JADX decompiler support.
|
||||
const mode = process.env.REA_FAKE_JADX_MODE ?? "normal";
|
||||
const server = new McpServer({
|
||||
name: "rea-jadx-bridge",
|
||||
version: mode === "version" ? "99.0.0" : "1",
|
||||
name: mode === "empty-server-identity" ? "" : "rea-jadx-bridge",
|
||||
version:
|
||||
mode === "empty-server-identity" ? "" : mode === "version" ? "99.0.0" : "1",
|
||||
});
|
||||
const classes = [
|
||||
"fixture.Target",
|
||||
@@ -60,16 +61,17 @@ register("load_apk", async ({ path }) => {
|
||||
resources: "full",
|
||||
});
|
||||
});
|
||||
register("get_app_info", () =>
|
||||
json({
|
||||
register("get_app_info", () => {
|
||||
if (mode === "rpc-disconnect") process.exit(17);
|
||||
return json({
|
||||
package: "fixture",
|
||||
version_name: "1",
|
||||
version_code: "1",
|
||||
min_sdk: "26",
|
||||
target_sdk: "33",
|
||||
permissions: ["android.permission.INTERNET"],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
register("get_android_manifest", () => {
|
||||
if (mode === "tool-error" || mode === "cleanup-failure")
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user