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:
@@ -20,8 +20,12 @@ it.each([
|
||||
const stderr =
|
||||
"FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory\r\n";
|
||||
const cause = new OwnedCommandFailure("process", "Decoder failed", {
|
||||
stdout: { text: "", bytes: 0 },
|
||||
stderr: { text: stderr, bytes: Buffer.byteLength(stderr) },
|
||||
stdout: { text: "", bytes: 0, observedBytes: 0 },
|
||||
stderr: {
|
||||
text: stderr,
|
||||
bytes: Buffer.byteLength(stderr),
|
||||
observedBytes: Buffer.byteLength(stderr),
|
||||
},
|
||||
exitCode,
|
||||
signal,
|
||||
});
|
||||
@@ -70,8 +74,12 @@ it.each([
|
||||
format: "har",
|
||||
});
|
||||
const cause = new OwnedCommandFailure("process", "Decoder failed", {
|
||||
stdout: { text: "", bytes: 0 },
|
||||
stderr: { text: stderr, bytes: Buffer.byteLength(stderr) },
|
||||
stdout: { text: "", bytes: 0, observedBytes: 0 },
|
||||
stderr: {
|
||||
text: stderr,
|
||||
bytes: Buffer.byteLength(stderr),
|
||||
observedBytes: Buffer.byteLength(stderr),
|
||||
},
|
||||
exitCode,
|
||||
signal,
|
||||
});
|
||||
@@ -128,8 +136,16 @@ it("retains actual command status and both diagnostic streams when cleanup also
|
||||
"process",
|
||||
"Observed command failure",
|
||||
{
|
||||
stdout: { text: "original provider output", bytes: 24 },
|
||||
stderr: { text: "private-marker provider reason", bytes: 30 },
|
||||
stdout: {
|
||||
text: "original provider output",
|
||||
bytes: 24,
|
||||
observedBytes: 24,
|
||||
},
|
||||
stderr: {
|
||||
text: "private-marker provider reason",
|
||||
bytes: 30,
|
||||
observedBytes: 30,
|
||||
},
|
||||
exitCode: 2,
|
||||
signal: null,
|
||||
},
|
||||
|
||||
@@ -16,8 +16,17 @@ export abstract class HopperError extends AnalysisError {}
|
||||
export type HopperLauncherOutcome = {
|
||||
readonly exit_code: number | null;
|
||||
readonly signal: string | null;
|
||||
readonly stdout: { readonly text: string; readonly bytes: number };
|
||||
readonly stderr: { readonly text: string; readonly bytes: number };
|
||||
/** Total bytes drained from the stream; retained_bytes reports captured bytes. */
|
||||
readonly stdout: {
|
||||
readonly text: string;
|
||||
readonly bytes: number;
|
||||
readonly retained_bytes: number;
|
||||
};
|
||||
readonly stderr: {
|
||||
readonly text: string;
|
||||
readonly bytes: number;
|
||||
readonly retained_bytes: number;
|
||||
};
|
||||
readonly output_closed: boolean;
|
||||
readonly diagnostic_truncated: boolean;
|
||||
};
|
||||
|
||||
@@ -24,7 +24,10 @@ import type {
|
||||
} from "./GhidraClientTypes.js";
|
||||
import type { GhidraInventoryOperation } from "./GhidraInventoryValues.js";
|
||||
import type { GhidraFunctionOperation } from "./GhidraFunctionValues.js";
|
||||
import { createGhidraDiagnostics } from "./GhidraDiagnostics.js";
|
||||
import {
|
||||
createGhidraDiagnostics,
|
||||
GHIDRA_PROCESS_DIAGNOSTIC_BYTES,
|
||||
} from "./GhidraDiagnostics.js";
|
||||
import { GhidraLaunchError, type GhidraLaunch } from "./GhidraLauncher.js";
|
||||
import { GhidraResponseBuffer } from "./GhidraResponseBuffer.js";
|
||||
import { GhidraResponseRouter } from "./GhidraResponseRouter.js";
|
||||
@@ -432,6 +435,7 @@ export class GhidraClient {
|
||||
if (partialLaunch !== undefined) {
|
||||
this.#launch = partialLaunch;
|
||||
this.#process = new ProviderProcessSupervisor(partialLaunch, {
|
||||
maxDiagnosticBytes: GHIDRA_PROCESS_DIAGNOSTIC_BYTES,
|
||||
onDiagnostic: (event) => this.#onProcessDiagnostic(event),
|
||||
});
|
||||
}
|
||||
@@ -444,6 +448,7 @@ export class GhidraClient {
|
||||
}
|
||||
this.#launch = launched.value;
|
||||
this.#process = new ProviderProcessSupervisor(launched.value, {
|
||||
maxDiagnosticBytes: GHIDRA_PROCESS_DIAGNOSTIC_BYTES,
|
||||
onDiagnostic: (event) => this.#onProcessDiagnostic(event),
|
||||
});
|
||||
const connected = await this.#connect(endpoint, deadline);
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { redactCapturedTransportCredential } from "../process/ProviderDiagnosticRedaction.js";
|
||||
import type { ProviderProcessSnapshot } from "../process/ProviderProcess.js";
|
||||
import type { GhidraLaunch } from "./GhidraLauncher.js";
|
||||
import type { GhidraTransportKind } from "./GhidraTransport.js";
|
||||
|
||||
/** Aggregate in-memory retention budget for this provider's launcher output. */
|
||||
export const GHIDRA_PROCESS_DIAGNOSTIC_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
/** Inputs needed to project bounded diagnostics for one Ghidra runtime. */
|
||||
export interface GhidraDiagnosticsOptions {
|
||||
readonly targetPath: string;
|
||||
@@ -31,10 +35,6 @@ export const createGhidraDiagnostics = (
|
||||
Object.keys(options.previous).length > 0
|
||||
)
|
||||
return options.previous;
|
||||
const redacted = (value: string): string =>
|
||||
options.token === undefined
|
||||
? value
|
||||
: value.replaceAll(options.token, "[REDACTED]");
|
||||
return {
|
||||
target_path: options.targetPath,
|
||||
target_sha256: options.targetSha256,
|
||||
@@ -67,16 +67,19 @@ export const createGhidraDiagnostics = (
|
||||
: {
|
||||
exit_code: options.snapshot.exitCode ?? null,
|
||||
exit_signal: options.snapshot.signal ?? null,
|
||||
stdout: boundedStream(options.snapshot.stdout, redacted),
|
||||
stderr: boundedStream(options.snapshot.stderr, redacted),
|
||||
diagnostic_truncated: options.snapshot.diagnosticTruncated === true,
|
||||
stdout: boundedStream(options.snapshot.stdout, options.token),
|
||||
stderr: boundedStream(options.snapshot.stderr, options.token),
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
const boundedStream = (
|
||||
stream: ProviderProcessSnapshot["stdout"],
|
||||
redact: (value: string) => string,
|
||||
token: string | undefined,
|
||||
): JsonValue => ({
|
||||
text: redact(stream.text),
|
||||
bytes: stream.bytes,
|
||||
text: redactCapturedTransportCredential(stream, token, "[REDACTED]"),
|
||||
// Preserve the historical total-byte counter; text is now bounded separately.
|
||||
bytes: stream.observedBytes,
|
||||
retained_bytes: stream.bytes,
|
||||
});
|
||||
|
||||
@@ -35,6 +35,7 @@ import { silentLogger } from "../logger.js";
|
||||
import type { Logger } from "pino";
|
||||
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
|
||||
import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";
|
||||
import { redactCapturedTransportCredential } from "../process/ProviderDiagnosticRedaction.js";
|
||||
import { ProviderRunLineage } from "../process/ProviderRunLineage.js";
|
||||
import {
|
||||
type ProviderProcessDiagnostic,
|
||||
@@ -64,6 +65,9 @@ import {
|
||||
responseResult,
|
||||
} from "./protocol.js";
|
||||
|
||||
/** Aggregate in-memory retention budget for this provider's launcher output. */
|
||||
export const HOPPER_PROCESS_DIAGNOSTIC_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
const SHUTDOWN_TIMEOUT_MS = 30_000;
|
||||
const SESSION_ROOT = process.platform === "darwin" ? "/tmp" : tmpdir();
|
||||
|
||||
@@ -609,15 +613,27 @@ export class HopperClient {
|
||||
const snapshot = this.#resources.processSupervisor?.snapshot();
|
||||
const token = this.#token;
|
||||
if (snapshot === undefined) return undefined;
|
||||
const redact = (text: string): string =>
|
||||
token === undefined
|
||||
? text
|
||||
: text.replaceAll(token, "[redacted transport credential]");
|
||||
return {
|
||||
exit_code: snapshot.exitCode ?? null,
|
||||
signal: snapshot.signal ?? null,
|
||||
stdout: { ...snapshot.stdout, text: redact(snapshot.stdout.text) },
|
||||
stderr: { ...snapshot.stderr, text: redact(snapshot.stderr.text) },
|
||||
stdout: {
|
||||
text: redactCapturedTransportCredential(
|
||||
snapshot.stdout,
|
||||
token,
|
||||
"[redacted transport credential]",
|
||||
),
|
||||
bytes: snapshot.stdout.observedBytes,
|
||||
retained_bytes: snapshot.stdout.bytes,
|
||||
},
|
||||
stderr: {
|
||||
text: redactCapturedTransportCredential(
|
||||
snapshot.stderr,
|
||||
token,
|
||||
"[redacted transport credential]",
|
||||
),
|
||||
bytes: snapshot.stderr.observedBytes,
|
||||
retained_bytes: snapshot.stderr.bytes,
|
||||
},
|
||||
output_closed: outputClosed,
|
||||
diagnostic_truncated: snapshot.diagnosticTruncated === true,
|
||||
};
|
||||
@@ -743,6 +759,7 @@ export class HopperClient {
|
||||
|
||||
#attachLauncher(launch: BridgeLaunch): void {
|
||||
this.#resources.processSupervisor = new ProviderProcessSupervisor(launch, {
|
||||
maxDiagnosticBytes: HOPPER_PROCESS_DIAGNOSTIC_BYTES,
|
||||
onDiagnostic: (event) => this.#onLauncherDiagnostic(launch, event),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { ProviderProcessSnapshot } from "./ProviderProcess.js";
|
||||
|
||||
/** Redact explicit transport credentials from complete and budget-cut output. */
|
||||
export const redactCapturedTransportCredential = (
|
||||
stream: Pick<
|
||||
ProviderProcessSnapshot["stdout"],
|
||||
"text" | "bytes" | "observedBytes"
|
||||
>,
|
||||
token: string | undefined,
|
||||
replacement: string,
|
||||
): string => {
|
||||
if (token === undefined || token.length === 0) return stream.text;
|
||||
|
||||
const { text } = stream;
|
||||
const cutOff = stream.observedBytes > stream.bytes;
|
||||
if (cutOff && !text.endsWith(token)) {
|
||||
const maximumPrefixLength = Math.min(text.length, token.length - 1);
|
||||
for (
|
||||
let prefixLength = maximumPrefixLength;
|
||||
prefixLength > 0;
|
||||
prefixLength -= 1
|
||||
) {
|
||||
if (text.endsWith(token.slice(0, prefixLength)))
|
||||
return `${text
|
||||
.slice(0, text.length - prefixLength)
|
||||
.replaceAll(token, replacement)}${replacement}`;
|
||||
}
|
||||
}
|
||||
|
||||
return text.replaceAll(token, replacement);
|
||||
};
|
||||
@@ -76,11 +76,16 @@ export interface ProviderProcessSnapshot {
|
||||
readonly diagnosticTruncated?: boolean;
|
||||
readonly stdout: {
|
||||
readonly text: string;
|
||||
/** Bytes retained in text, limited by maxDiagnosticBytes when configured. */
|
||||
readonly bytes: number;
|
||||
/** All bytes drained from this stream since construction or resetOutput. */
|
||||
readonly observedBytes: number;
|
||||
};
|
||||
readonly stderr: {
|
||||
readonly text: string;
|
||||
/** Bytes retained in text, limited by maxDiagnosticBytes when configured. */
|
||||
readonly bytes: number;
|
||||
readonly observedBytes: number;
|
||||
};
|
||||
readonly exitCode: number | null | undefined;
|
||||
readonly signal: NodeJS.Signals | null | undefined;
|
||||
@@ -268,8 +273,8 @@ export class ProviderProcessSupervisor {
|
||||
snapshot(): ProviderProcessSnapshot {
|
||||
return {
|
||||
...(this.#diagnosticTruncated ? { diagnosticTruncated: true } : {}),
|
||||
stdout: this.#stdout.snapshot(),
|
||||
stderr: this.#stderr.snapshot(),
|
||||
stdout: this.#stdout.snapshot(this.#stdoutObservedBytes),
|
||||
stderr: this.#stderr.snapshot(this.#stderrObservedBytes),
|
||||
exitCode: this.#exitObservation?.code,
|
||||
signal: this.#exitObservation?.signal,
|
||||
};
|
||||
@@ -488,11 +493,12 @@ class ProcessOutputCapture {
|
||||
this.#chunks.push(Buffer.from(chunk));
|
||||
}
|
||||
|
||||
snapshot(): ProviderProcessSnapshot["stdout"] {
|
||||
snapshot(observedBytes: number): ProviderProcessSnapshot["stdout"] {
|
||||
const text = Buffer.concat(this.#chunks, this.#bytes).toString("utf8");
|
||||
return {
|
||||
text,
|
||||
bytes: this.#bytes,
|
||||
observedBytes,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user