fix(providers): bound launcher diagnostics without losing byte counts
Give Ghidra and Hopper an 8 MiB aggregate retained-output budget while continuing to drain their process pipes. Expose total observed bytes at the supervisor boundary and retained bytes plus explicit truncation in provider diagnostics. Redact known transport tokens even when the retained prefix ends inside one, preserving adjacent caller path/query evidence. Existing partial-launch and cleanup ownership remain intact. Validation: 285 tests across 34 focused suites passed; the final cutoff-fixture refinement passed all 31 Ghidra/Hopper diagnostic tests. check:fast and architecture guards passed. Provider fixtures use real child processes; real Ghidra/Hopper applications were not verified.
This commit is contained in:
@@ -202,9 +202,11 @@ describe("provider process output and cleanup primitives", () => {
|
||||
expect(snapshot).toMatchObject({
|
||||
stdout: {
|
||||
bytes: outputBytes,
|
||||
observedBytes: outputBytes,
|
||||
},
|
||||
stderr: {
|
||||
bytes: outputBytes,
|
||||
observedBytes: outputBytes,
|
||||
},
|
||||
exitCode: 23,
|
||||
signal: null,
|
||||
|
||||
@@ -29,6 +29,12 @@ it("bounds capture before diagnostic callbacks while draining both output stream
|
||||
expect(
|
||||
diagnostics.some((event) => event.type === "output" && event.truncated),
|
||||
).toBe(true);
|
||||
const snapshot = supervisor.snapshot();
|
||||
expect(snapshot.stdout.observedBytes).toBe(70_000);
|
||||
expect(snapshot.stderr.observedBytes).toBe(70_000);
|
||||
expect(snapshot.stdout.bytes + snapshot.stderr.bytes).toBeLessThanOrEqual(
|
||||
1024,
|
||||
);
|
||||
for (const stream of ["stdout", "stderr"])
|
||||
expect(
|
||||
diagnostics
|
||||
|
||||
@@ -11,6 +11,7 @@ import type { Logger } from "pino";
|
||||
|
||||
import { ok } from "../../../../src/domain/result.js";
|
||||
import { GhidraClient } from "../../../../src/ghidra/GhidraClient.js";
|
||||
import { GHIDRA_PROCESS_DIAGNOSTIC_BYTES } from "../../../../src/ghidra/GhidraDiagnostics.js";
|
||||
import { GhidraHeadlessLauncher } from "../../../../src/ghidra/GhidraLauncher.js";
|
||||
import type { GhidraDiagnostic } from "../../../../src/ghidra/GhidraClientTypes.js";
|
||||
import type {
|
||||
@@ -49,7 +50,8 @@ type FixtureMode =
|
||||
| "hang_tools"
|
||||
| "exit_tools"
|
||||
| "silent"
|
||||
| "exit";
|
||||
| "exit"
|
||||
| "diagnostic_output";
|
||||
|
||||
class FixtureLauncher implements GhidraLauncher {
|
||||
#notifyStarted: ((child: ChildProcess) => void) | undefined;
|
||||
@@ -458,6 +460,53 @@ describe("GhidraClient cleanup and diagnostics", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("bounds long-lived launcher diagnostics without interrupting the session", async () => {
|
||||
const launcher = new FixtureLauncher("diagnostic_output");
|
||||
const client = clientFor(launcher);
|
||||
await expect(client.start()).resolves.toMatchObject({ ok: true });
|
||||
await expect(
|
||||
client.callTool("list_strings", { document: null }),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(client.diagnostics()).toMatchObject({
|
||||
diagnostic_truncated: true,
|
||||
});
|
||||
});
|
||||
const diagnostics = client.diagnostics();
|
||||
const stdout = diagnostics.stdout as {
|
||||
bytes: number;
|
||||
retained_bytes: number;
|
||||
text: string;
|
||||
};
|
||||
const stderr = diagnostics.stderr as {
|
||||
bytes: number;
|
||||
retained_bytes: number;
|
||||
text: string;
|
||||
};
|
||||
expect(stdout.bytes + stderr.bytes).toBeGreaterThan(
|
||||
GHIDRA_PROCESS_DIAGNOSTIC_BYTES,
|
||||
);
|
||||
expect(stdout.retained_bytes + stderr.retained_bytes).toBeLessThanOrEqual(
|
||||
GHIDRA_PROCESS_DIAGNOSTIC_BYTES,
|
||||
);
|
||||
expect(stdout.bytes).toBeGreaterThanOrEqual(stdout.retained_bytes);
|
||||
expect(stderr.bytes).toBeGreaterThanOrEqual(stderr.retained_bytes);
|
||||
expect(JSON.stringify(diagnostics)).not.toContain(launcher.tokens[0] ?? "");
|
||||
expect(`${stdout.text}${stderr.text}`).toContain("[REDACTED]");
|
||||
expect(
|
||||
stderr.text.endsWith(
|
||||
"source=/tmp/local-evidence.bin?cursor=keep&[REDACTED]",
|
||||
),
|
||||
).toBe(true);
|
||||
|
||||
await client.close();
|
||||
expect(exited(launcher.processes[0])).toBe(true);
|
||||
await expect(access(launcher.runtimeRoots[0] ?? "")).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
});
|
||||
|
||||
it("retains actionable diagnostics without retaining its token", async () => {
|
||||
const launcher = new FixtureLauncher("exit");
|
||||
const events: GhidraDiagnostic[] = [];
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import { access } from "node:fs/promises";
|
||||
|
||||
import { expect, it, onTestFinished, vi } from "vitest";
|
||||
|
||||
import { HopperTimeoutError } from "../../../../src/domain/hopperErrors.js";
|
||||
import {
|
||||
HopperClient,
|
||||
HOPPER_PROCESS_DIAGNOSTIC_BYTES,
|
||||
} from "../../../../src/hopper/HopperClient.js";
|
||||
import { HopperFixtureLauncher } from "./hopperClient.fixture.js";
|
||||
|
||||
it("bounds long-lived launcher output while keeping the bridge usable", async () => {
|
||||
let observedStderrBytes = 0;
|
||||
const diagnosticLauncher = new HopperFixtureLauncher(undefined, "noisy");
|
||||
const client = new HopperClient({
|
||||
launcher: diagnosticLauncher,
|
||||
startupTimeoutMs: 10_000,
|
||||
onDiagnostic(event) {
|
||||
if (event.type === "launcher-stderr") observedStderrBytes += event.bytes;
|
||||
},
|
||||
});
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
});
|
||||
|
||||
await expect(client.start()).resolves.toMatchObject({ ok: true });
|
||||
const session = await client.callTool("echo", { value: "still usable" });
|
||||
expect(session).toEqual({ ok: true, value: { value: "still usable" } });
|
||||
await vi.waitFor(() => {
|
||||
expect(observedStderrBytes).toBeGreaterThan(
|
||||
HOPPER_PROCESS_DIAGNOSTIC_BYTES,
|
||||
);
|
||||
});
|
||||
|
||||
await client.close();
|
||||
const directory = diagnosticLauncher.directories[0] ?? "";
|
||||
await expect(access(directory)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
expect(diagnosticLauncher.processes[0]?.exitCode).not.toBeNull();
|
||||
});
|
||||
|
||||
it("reports observed and retained output when capture cuts a token", async () => {
|
||||
const diagnosticLauncher = new HopperFixtureLauncher(
|
||||
undefined,
|
||||
"noisy_no_socket",
|
||||
);
|
||||
const client = new HopperClient({
|
||||
launcher: diagnosticLauncher,
|
||||
startupTimeoutMs: 1_000,
|
||||
});
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
});
|
||||
|
||||
const started = await client.start();
|
||||
if (started.ok || !(started.error instanceof HopperTimeoutError))
|
||||
throw new Error("Expected the no-socket fixture to time out");
|
||||
const outcome = started.error.launcherOutcome;
|
||||
expect(outcome).toMatchObject({ diagnostic_truncated: true });
|
||||
if (outcome === undefined) throw new Error("Launcher outcome was omitted");
|
||||
expect(outcome.stdout.bytes + outcome.stderr.bytes).toBeGreaterThan(
|
||||
HOPPER_PROCESS_DIAGNOSTIC_BYTES,
|
||||
);
|
||||
expect(
|
||||
outcome.stdout.retained_bytes + outcome.stderr.retained_bytes,
|
||||
).toBeLessThanOrEqual(HOPPER_PROCESS_DIAGNOSTIC_BYTES);
|
||||
expect(outcome.stdout.bytes).toBeGreaterThanOrEqual(
|
||||
outcome.stdout.retained_bytes,
|
||||
);
|
||||
expect(outcome.stderr.bytes).toBeGreaterThanOrEqual(
|
||||
outcome.stderr.retained_bytes,
|
||||
);
|
||||
expect(outcome.stderr.retained_bytes).toBe(HOPPER_PROCESS_DIAGNOSTIC_BYTES);
|
||||
const token = diagnosticLauncher.tokens[0] ?? "";
|
||||
expect(JSON.stringify(outcome)).not.toContain(token);
|
||||
expect(`${outcome.stdout.text}${outcome.stderr.text}`).toContain(
|
||||
"[redacted transport credential]",
|
||||
);
|
||||
expect(
|
||||
outcome.stderr.text.endsWith(
|
||||
"source=/tmp/local-evidence.bin?cursor=keep&[redacted transport credential]",
|
||||
),
|
||||
).toBe(true);
|
||||
|
||||
await client.close();
|
||||
const directory = diagnosticLauncher.directories[0] ?? "";
|
||||
await expect(access(directory)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
expect(diagnosticLauncher.processes[0]?.exitCode).not.toBeNull();
|
||||
});
|
||||
@@ -28,15 +28,20 @@ export class HopperFixtureLauncher implements BridgeLauncher {
|
||||
readonly socketPaths: string[] = [];
|
||||
readonly directories: string[] = [];
|
||||
readonly runIds: string[] = [];
|
||||
readonly tokens: string[] = [];
|
||||
readonly processes: ChildProcess[] = [];
|
||||
readonly requests: FixtureRequest[] = [];
|
||||
|
||||
constructor(readonly tokenOverride?: string) {}
|
||||
constructor(
|
||||
readonly tokenOverride?: string,
|
||||
readonly diagnosticMode: "quiet" | "noisy" | "noisy_no_socket" = "quiet",
|
||||
) {}
|
||||
|
||||
launch(session: BridgeSession) {
|
||||
this.socketPaths.push(session.socketPath);
|
||||
this.directories.push(session.directory);
|
||||
this.runIds.push(session.runId);
|
||||
this.tokens.push(session.token);
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
@@ -44,8 +49,10 @@ export class HopperFixtureLauncher implements BridgeLauncher {
|
||||
session.socketPath,
|
||||
this.tokenOverride ?? session.token,
|
||||
session.runId,
|
||||
"acknowledge",
|
||||
this.diagnosticMode,
|
||||
],
|
||||
{ stdio: ["ignore", "ignore", "pipe", "ipc"] },
|
||||
{ stdio: ["ignore", "pipe", "pipe", "ipc"] },
|
||||
);
|
||||
child.on("message", (value: unknown) => {
|
||||
const request = requestSchema.safeParse(value);
|
||||
|
||||
Vendored
+10
@@ -153,6 +153,16 @@ const handleRequest = (socket, server, request, state) => {
|
||||
|
||||
const handlePing = (socket, request, state) => {
|
||||
state.pings.value += 1;
|
||||
if (state.mode === "diagnostic_output" && state.pings.value === 1) {
|
||||
const retainedTokenPrefixBytes = Math.floor(state.token.length / 2);
|
||||
const preservedMarker = "source=/tmp/local-evidence.bin?cursor=keep&";
|
||||
const fillLength = 8 * 1024 * 1024 - retainedTokenPrefixBytes;
|
||||
const fill = "x".repeat(fillLength - Buffer.byteLength(preservedMarker));
|
||||
process.stderr.write(`${fill}${preservedMarker}`);
|
||||
setImmediate(() => {
|
||||
process.stderr.write(state.token);
|
||||
});
|
||||
}
|
||||
if (state.mode === "malformed") {
|
||||
socket.write("{invalid\n");
|
||||
return;
|
||||
|
||||
Vendored
+23
-3
@@ -1,7 +1,13 @@
|
||||
import { createServer } from "node:net";
|
||||
|
||||
const [socketPath, token, runId, shutdownMode = "acknowledge"] =
|
||||
process.argv.slice(2);
|
||||
const [
|
||||
socketPath,
|
||||
token,
|
||||
runId,
|
||||
shutdownMode = "acknowledge",
|
||||
diagnosticMode = "quiet",
|
||||
] = process.argv.slice(2);
|
||||
let diagnosticOutputEmitted = false;
|
||||
const heldReplies = new Map();
|
||||
if (process.send !== undefined)
|
||||
process.on("message", (message) => {
|
||||
@@ -16,6 +22,18 @@ const shutdownResult = () => ({
|
||||
document_closed: shutdownMode !== "unconfirmed",
|
||||
...(shutdownMode === "cleanup-required" ? { cleanup_required: true } : {}),
|
||||
});
|
||||
const emitDiagnosticOutput = () => {
|
||||
if (diagnosticMode === "quiet" || diagnosticOutputEmitted) return;
|
||||
diagnosticOutputEmitted = true;
|
||||
const retainedTokenPrefixBytes = Math.floor(token.length / 2);
|
||||
const preservedMarker = "source=/tmp/local-evidence.bin?cursor=keep&";
|
||||
const fillLength = 8 * 1024 * 1024 - retainedTokenPrefixBytes;
|
||||
const fill = "x".repeat(fillLength - Buffer.byteLength(preservedMarker));
|
||||
process.stderr.write(`${fill}${preservedMarker}`);
|
||||
setImmediate(() => {
|
||||
process.stderr.write(token);
|
||||
});
|
||||
};
|
||||
const capabilityUnavailableResult = (id) => ({
|
||||
id,
|
||||
error: {
|
||||
@@ -182,6 +200,7 @@ const server = createServer((socket) => {
|
||||
if (request.token !== token) {
|
||||
send({ id: request.id, error: { code: -32001, message: "bad token" } });
|
||||
} else if (request.method === "health") {
|
||||
emitDiagnosticOutput();
|
||||
send(
|
||||
{
|
||||
id: request.id,
|
||||
@@ -249,5 +268,6 @@ const closeServer = () =>
|
||||
if (process.connected) process.disconnect();
|
||||
});
|
||||
|
||||
server.listen(socketPath);
|
||||
if (diagnosticMode === "noisy_no_socket") emitDiagnosticOutput();
|
||||
else server.listen(socketPath);
|
||||
process.on("SIGTERM", closeServer);
|
||||
|
||||
Reference in New Issue
Block a user