fix(native): bound retained observations and preserve failure evidence
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
@@ -6,7 +7,10 @@ import { expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { nativeCallObservationResultSchema } from "../../../src/domain/native/nativeCallObservation.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
|
||||
import { nativeCallPartialObservationSchema } from "../../../src/domain/native/nativeCallPartialObservation.js";
|
||||
import { err, ok } from "../../../src/domain/result.js";
|
||||
import { ProviderAdapterError } from "../../../src/domain/providerAdapterError.js";
|
||||
import type { NativeCallTracer } from "../../../src/native/LldbCallTracer.js";
|
||||
import { NativeMacOSProvider } from "../../../src/native/NativeMacOSProvider.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
@@ -130,3 +134,169 @@ it("routes observe_native_calls through MCP with schema-checked input and output
|
||||
await server.close();
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
["cancelled", "cancelled", undefined],
|
||||
[
|
||||
"tracer failure with cleanup uncertainty",
|
||||
"cleanup_incomplete",
|
||||
{
|
||||
reason: "target process termination could not be verified",
|
||||
resources: ["native-target:4242"],
|
||||
},
|
||||
],
|
||||
] as const)(
|
||||
"preserves typed native partial observations over MCP after %s",
|
||||
async (_label, expectedCode, cleanup) => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-native-calls-partial-mcp-",
|
||||
);
|
||||
const path = join(directory, "Tool");
|
||||
const selectedBytes = machoHeader();
|
||||
const sha256 = createHash("sha256").update(selectedBytes).digest("hex");
|
||||
await writeFile(path, selectedBytes);
|
||||
const launchArguments = ["--fixture", "selected argument"];
|
||||
const launchEnvironment = { REA_NATIVE_FIXTURE: "selected value" };
|
||||
const workingDirectory = directory;
|
||||
const partialObservation = nativeCallPartialObservationSchema.parse({
|
||||
kind: "native-call-observation",
|
||||
target: {
|
||||
path,
|
||||
sha256,
|
||||
architecture: "arm64",
|
||||
arguments: launchArguments,
|
||||
environment: launchEnvironment,
|
||||
working_directory: workingDirectory,
|
||||
},
|
||||
process: {
|
||||
pid: 4242,
|
||||
stdout: {
|
||||
text: "partial target output",
|
||||
bytes: 21,
|
||||
truncated: true,
|
||||
complete: false,
|
||||
},
|
||||
stderr: {
|
||||
text: "debugger diagnostic",
|
||||
bytes: 19,
|
||||
truncated: false,
|
||||
complete: false,
|
||||
},
|
||||
other_stops: ["signal SIGSTOP"],
|
||||
},
|
||||
debugger: { version: "lldb-fixture" },
|
||||
events: [
|
||||
{
|
||||
sequence: 0,
|
||||
elapsed_ms: 4,
|
||||
thread_id: 3,
|
||||
breakpoint_index: 0,
|
||||
load_address: "0x1000",
|
||||
file_address: "0x1000",
|
||||
module: "Tool",
|
||||
module_path: path,
|
||||
symbol: "open",
|
||||
receiver_class: null,
|
||||
selector: null,
|
||||
registers: [{ name: "x0", value: "0x2" }],
|
||||
backtrace: [],
|
||||
},
|
||||
],
|
||||
coverage: {
|
||||
status: "partial",
|
||||
reason: cleanup === undefined ? "cancelled" : "cleanup-failure",
|
||||
},
|
||||
limitations: ["Observation ended before the requested window completed."],
|
||||
});
|
||||
const tracer: NativeCallTracer = {
|
||||
trace: () =>
|
||||
Promise.resolve(
|
||||
err(
|
||||
cleanup === undefined
|
||||
? new AnalysisCancelledError("observe_native_calls", {
|
||||
partialObservation,
|
||||
})
|
||||
: new ProviderAdapterError(
|
||||
"native-macos",
|
||||
"observe_native_calls",
|
||||
{
|
||||
cleanup,
|
||||
partialObservation,
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
};
|
||||
const session = createTestBinarySession(
|
||||
new NativeMacOSProvider(new NativeFixtureRunner(), "darwin", tracer),
|
||||
);
|
||||
const server = createServer(session, session);
|
||||
const client = new Client({
|
||||
name: "native-calls-partial-mcp-test",
|
||||
version: "1",
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
try {
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path },
|
||||
});
|
||||
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
|
||||
|
||||
const called = await client.callTool({
|
||||
name: "observe_native_calls",
|
||||
arguments: {
|
||||
breakpoints: [{ kind: "function", name: "open" }],
|
||||
arguments: launchArguments,
|
||||
environment: launchEnvironment,
|
||||
working_directory: workingDirectory,
|
||||
duration_ms: 1000,
|
||||
},
|
||||
});
|
||||
|
||||
expect(called.isError).toBe(true);
|
||||
expect(called.structuredContent).toMatchObject({
|
||||
error: {
|
||||
code: expectedCode,
|
||||
details: {
|
||||
...(cleanup === undefined
|
||||
? { cleanup: "complete" }
|
||||
: {
|
||||
execution_failure: "execution_failure",
|
||||
cleanup: "incomplete",
|
||||
cleanup_reason: cleanup.reason,
|
||||
}),
|
||||
partial_observation: {
|
||||
kind: "native-call-observation",
|
||||
target: {
|
||||
path,
|
||||
sha256,
|
||||
architecture: "arm64",
|
||||
arguments: launchArguments,
|
||||
environment: launchEnvironment,
|
||||
working_directory: workingDirectory,
|
||||
},
|
||||
debugger: { version: "lldb-fixture" },
|
||||
process: {
|
||||
pid: 4242,
|
||||
stdout: {
|
||||
text: "partial target output",
|
||||
bytes: 21,
|
||||
truncated: true,
|
||||
complete: false,
|
||||
},
|
||||
},
|
||||
events: [{ sequence: 0, symbol: "open" }],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
await client.close();
|
||||
await server.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { nativeCallPartialObservationSchema } from "../../../../src/domain/native/nativeCallPartialObservation.js";
|
||||
import {
|
||||
LLDB_OBSERVATION_JOURNAL_MAX_BYTES,
|
||||
parseLldbObservationJournal,
|
||||
projectLldbPartialObservation,
|
||||
readBoundedPrefix,
|
||||
} from "../../../../src/native/LldbRetainedObservations.js";
|
||||
|
||||
const event = {
|
||||
sequence: 0,
|
||||
elapsed_ms: 1,
|
||||
thread_id: 2,
|
||||
breakpoint_index: 0,
|
||||
load_address: "0x1000",
|
||||
file_address: "0x1000",
|
||||
module: "fixture",
|
||||
module_path: "/tmp/fixture",
|
||||
symbol: "main",
|
||||
receiver_class: null,
|
||||
selector: null,
|
||||
registers: [],
|
||||
backtrace: [],
|
||||
};
|
||||
|
||||
const observation = (
|
||||
reason: "cancelled" | "tracer-failure" | "cleanup-failure",
|
||||
journalText: string,
|
||||
) => {
|
||||
const parsed = parseLldbObservationJournal(Buffer.from(journalText));
|
||||
return nativeCallPartialObservationSchema.parse(
|
||||
projectLldbPartialObservation({
|
||||
target: {
|
||||
path: "/tmp/fixture",
|
||||
sha256: "a".repeat(64),
|
||||
architecture: "arm64",
|
||||
arguments: ["--selected"],
|
||||
environment: { SELECTED_VALUE: "retained" },
|
||||
working_directory: "/tmp",
|
||||
},
|
||||
pid: 42,
|
||||
stdout: { text: "prefix", bytes: 6, truncated: true, complete: false },
|
||||
stderr: null,
|
||||
version: null,
|
||||
journal: parsed,
|
||||
reason,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
describe("LLDB retained observations", () => {
|
||||
it.each([
|
||||
["cancellation", "cancelled"],
|
||||
["bridge failure", "tracer-failure"],
|
||||
["cleanup failure", "cleanup-failure"],
|
||||
] as const)("retains admitted events after %s", (_case, reason) => {
|
||||
const journal = `${JSON.stringify({ kind: "event", event })}\n`;
|
||||
const partial = observation(reason, journal);
|
||||
expect(partial.events).toEqual([event]);
|
||||
expect(partial.coverage).toEqual({ status: "partial", reason });
|
||||
expect(partial.process.stdout).toMatchObject({
|
||||
text: "prefix",
|
||||
bytes: 6,
|
||||
complete: false,
|
||||
truncated: true,
|
||||
});
|
||||
expect(partial.process.stderr).toBeNull();
|
||||
expect(partial.limitations).toContain(
|
||||
"The target stderr capture was unavailable.",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps valid rows around malformed data and ignores a torn final row", () => {
|
||||
const journal = [
|
||||
JSON.stringify({ kind: "event", event }),
|
||||
"{bad-json}",
|
||||
JSON.stringify({ kind: "other-stop", reason: "signal SIGTERM" }),
|
||||
'{"kind":"event","event":',
|
||||
].join("\n");
|
||||
const partial = observation("tracer-failure", journal);
|
||||
expect(partial.events).toEqual([event]);
|
||||
expect(partial.process.other_stops).toEqual(["signal SIGTERM"]);
|
||||
expect(partial.limitations).toContain(
|
||||
"1 observation journal row(s) were malformed or invalid and ignored (first at row 2).",
|
||||
);
|
||||
expect(partial.limitations).toContain(
|
||||
"The final observation journal record was incomplete and was ignored.",
|
||||
);
|
||||
});
|
||||
|
||||
it("bounds malformed-row diagnostics for an 8 MiB journal", () => {
|
||||
const valid = JSON.stringify({ kind: "event", event });
|
||||
const malformed = '{"kind":"unknown"}\n';
|
||||
const malformedRows = Math.floor(
|
||||
(LLDB_OBSERVATION_JOURNAL_MAX_BYTES - valid.length * 2 - 2) /
|
||||
malformed.length,
|
||||
);
|
||||
const journal = `${valid}\n${malformed.repeat(malformedRows)}${valid}\n`;
|
||||
expect(Buffer.byteLength(journal)).toBeLessThanOrEqual(
|
||||
LLDB_OBSERVATION_JOURNAL_MAX_BYTES,
|
||||
);
|
||||
const partial = observation("tracer-failure", journal);
|
||||
expect(partial.events).toEqual([event, event]);
|
||||
expect(partial.limitations.length).toBeLessThanOrEqual(6);
|
||||
expect(
|
||||
partial.limitations.find((limitation) =>
|
||||
limitation.includes("malformed"),
|
||||
),
|
||||
).toMatch(
|
||||
/^\d+ observation journal row\(s\) were malformed or invalid and ignored \(first at row 2\)\.$/u,
|
||||
);
|
||||
});
|
||||
|
||||
it("continues a bounded prefix read after short reads", async () => {
|
||||
const source = Buffer.from("retained prefix");
|
||||
let position = 0;
|
||||
const result = await readBoundedPrefix(
|
||||
source.length,
|
||||
async (buffer, offset, length, requestedPosition) => {
|
||||
expect(requestedPosition).toBe(position);
|
||||
const count = Math.min(2, length, source.length - position);
|
||||
source.copy(buffer, offset, position, position + count);
|
||||
position += count;
|
||||
return count;
|
||||
},
|
||||
);
|
||||
expect(result.bytesRead).toBe(source.length);
|
||||
expect(result.buffer.toString("utf8", 0, result.bytesRead)).toBe(
|
||||
"retained prefix",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -212,6 +212,53 @@ describe("observe_native_calls projection: projects completed observations", ()
|
||||
});
|
||||
|
||||
describe("observe_native_calls projection: reports partial observation coverage", () => {
|
||||
it("retains normal exit and location counts when breakpoint metadata is bounded", async () => {
|
||||
const observed = await observe(
|
||||
new FixtureTracer(
|
||||
ok({
|
||||
debugger: {
|
||||
path: "/usr/bin/lldb",
|
||||
sha256: "a".repeat(64),
|
||||
version: null,
|
||||
},
|
||||
run: run({
|
||||
breakpoint_locations_truncated: true,
|
||||
breakpoints: [
|
||||
{ index: 0, location_count: 64, locations: [LOCATION] },
|
||||
{ index: 1, location_count: 64, locations: [] },
|
||||
],
|
||||
}),
|
||||
stdout: { text: "", bytes: 0, truncated: false, complete: true },
|
||||
stderr: { text: "", bytes: 0, truncated: false, complete: true },
|
||||
terminated: true,
|
||||
}),
|
||||
),
|
||||
{ breakpoints: BREAKPOINTS },
|
||||
);
|
||||
if (!observed.ok) throw observed.error;
|
||||
const result = nativeCallObservationResultSchema.parse(
|
||||
observed.value.result,
|
||||
);
|
||||
expect(result.process).toMatchObject({
|
||||
outcome: "exited",
|
||||
exit_status: 0,
|
||||
terminated: true,
|
||||
});
|
||||
expect(
|
||||
result.breakpoints.map(({ location_count }) => location_count),
|
||||
).toEqual([64, 64]);
|
||||
expect(result.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
breakpoint_locations_truncated: true,
|
||||
resource_limit_reached: false,
|
||||
unresolved_breakpoints: [],
|
||||
});
|
||||
expect(result.limitations).toContain(
|
||||
"Resolved breakpoint locations exceeded the aggregate 8 MiB metadata budget; omitted locations are counted in location_count. This metadata limit does not change the process outcome.",
|
||||
);
|
||||
expect(result.limitations.join(" ")).not.toContain("process was killed");
|
||||
});
|
||||
|
||||
it("marks event limits, unresolved breakpoints and unconfirmed exits", async () => {
|
||||
const tracer = new FixtureTracer(
|
||||
ok({
|
||||
|
||||
Reference in New Issue
Block a user