refactor(tools): remove artificial analysis ceilings
This commit is contained in:
+22
-42
@@ -16,10 +16,6 @@ export interface NativeCommandCapture {
|
||||
readonly stderr: string;
|
||||
readonly stdoutBytes: number;
|
||||
readonly stderrBytes: number;
|
||||
/** Successful captures are exhaustive; output-limit exits return a failure. */
|
||||
readonly stdoutTruncated: false;
|
||||
/** Successful captures are exhaustive; output-limit exits return a failure. */
|
||||
readonly stderrTruncated: false;
|
||||
readonly exitCode: number | null;
|
||||
readonly signal: string | null;
|
||||
}
|
||||
@@ -27,13 +23,7 @@ export interface NativeCommandCapture {
|
||||
export class NativeCommandFailure extends Error {
|
||||
constructor(
|
||||
readonly tool: string,
|
||||
readonly reason:
|
||||
| "unavailable"
|
||||
| "cancelled"
|
||||
| "timeout"
|
||||
| "output-limit"
|
||||
| "nonzero-exit"
|
||||
| "io",
|
||||
readonly reason: "unavailable" | "cancelled" | "nonzero-exit" | "io",
|
||||
readonly exitCode: number | null = null,
|
||||
options?: ErrorOptions,
|
||||
) {
|
||||
@@ -41,11 +31,9 @@ export class NativeCommandFailure extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
/** Bounds and cancellation applied to one allowlisted native command. */
|
||||
/** Cancellation and exit handling for one allowlisted native command. */
|
||||
export interface NativeCommandOptions {
|
||||
readonly signal?: AbortSignal;
|
||||
readonly timeoutMs: number;
|
||||
readonly maxOutputBytes: number;
|
||||
readonly acceptNonZero?: boolean;
|
||||
}
|
||||
|
||||
@@ -60,18 +48,16 @@ export interface NativeCommandRunner {
|
||||
/** Resolve one allowlisted native tool to an immutable executable identity. */
|
||||
export type NativeToolResolver = (
|
||||
tool: string,
|
||||
signal?: AbortSignal,
|
||||
) => Promise<Result<ResolvedTool, NativeCommandFailure>>;
|
||||
|
||||
/** Run allowlisted Xcode tools directly with bounded output and no shell. */
|
||||
/** Run allowlisted Xcode tools directly without a shell. */
|
||||
export class XcrunCommandRunner implements NativeCommandRunner {
|
||||
readonly #resolved = new Map<
|
||||
string,
|
||||
Promise<Result<ResolvedTool, NativeCommandFailure>>
|
||||
>();
|
||||
readonly #resolved = new Map<string, ResolvedTool>();
|
||||
|
||||
constructor(
|
||||
private readonly resolveTool: NativeToolResolver = (tool) =>
|
||||
resolveXcrunTool(tool),
|
||||
private readonly resolveTool: NativeToolResolver = (tool, signal) =>
|
||||
resolveXcrunTool(tool, signal),
|
||||
) {}
|
||||
|
||||
run(
|
||||
@@ -83,7 +69,7 @@ export class XcrunCommandRunner implements NativeCommandRunner {
|
||||
return Promise.resolve(
|
||||
err(new NativeCommandFailure(tool, "unavailable")),
|
||||
);
|
||||
return this.#resolve(tool).then(async (resolved) => {
|
||||
return this.#resolve(tool, options.signal).then(async (resolved) => {
|
||||
if (!resolved.ok) return resolved;
|
||||
const captured = await captureProcess(
|
||||
resolved.value.path,
|
||||
@@ -108,14 +94,12 @@ export class XcrunCommandRunner implements NativeCommandRunner {
|
||||
|
||||
async #resolve(
|
||||
tool: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<ResolvedTool, NativeCommandFailure>> {
|
||||
const existing = this.#resolved.get(tool);
|
||||
if (existing !== undefined) return existing;
|
||||
const pending = this.resolveTool(tool);
|
||||
this.#resolved.set(tool, pending);
|
||||
const resolved = await pending;
|
||||
if (!resolved.ok && this.#resolved.get(tool) === pending)
|
||||
this.#resolved.delete(tool);
|
||||
if (existing !== undefined) return ok(existing);
|
||||
const resolved = await this.resolveTool(tool, signal);
|
||||
if (resolved.ok) this.#resolved.set(tool, resolved.value);
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
@@ -142,14 +126,20 @@ const ALLOWED_TOOLS = new Set([
|
||||
|
||||
const resolveXcrunTool = async (
|
||||
tool: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<ResolvedTool, NativeCommandFailure>> => {
|
||||
const found = await captureProcess(
|
||||
"/usr/bin/xcrun",
|
||||
["--find", tool],
|
||||
"xcrun",
|
||||
{ timeoutMs: 5_000, maxOutputBytes: 64 * 1024 },
|
||||
signal === undefined ? {} : { signal },
|
||||
);
|
||||
if (!found.ok || found.value.exitCode !== 0)
|
||||
if (!found.ok) {
|
||||
if (found.error.reason === "cancelled")
|
||||
return err(new NativeCommandFailure(tool, "cancelled"));
|
||||
return err(new NativeCommandFailure(tool, "unavailable"));
|
||||
}
|
||||
if (found.value.exitCode !== 0)
|
||||
return err(new NativeCommandFailure(tool, "unavailable"));
|
||||
const candidate = found.value.stdout.trim();
|
||||
if (!candidate.startsWith("/"))
|
||||
@@ -178,8 +168,6 @@ const captureProcess = (
|
||||
tool: string,
|
||||
options: {
|
||||
readonly signal?: AbortSignal;
|
||||
readonly timeoutMs: number;
|
||||
readonly maxOutputBytes: number;
|
||||
readonly acceptNonZero?: boolean;
|
||||
},
|
||||
): Promise<Result<ProcessCapture, NativeCommandFailure>> =>
|
||||
@@ -210,7 +198,6 @@ const captureProcess = (
|
||||
): void => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timeout);
|
||||
options.signal?.removeEventListener("abort", onAbort);
|
||||
resolve(result);
|
||||
};
|
||||
@@ -220,18 +207,13 @@ const captureProcess = (
|
||||
};
|
||||
const onAbort = (): void => stop("cancelled");
|
||||
options.signal?.addEventListener("abort", onAbort, { once: true });
|
||||
const timeout = setTimeout(() => stop("timeout"), options.timeoutMs);
|
||||
child.stdout.on("data", (chunk: Buffer) => {
|
||||
stdoutBytes += chunk.length;
|
||||
if (stdoutBytes + stderrBytes > options.maxOutputBytes)
|
||||
stop("output-limit");
|
||||
else stdout.push(chunk);
|
||||
stdout.push(chunk);
|
||||
});
|
||||
child.stderr.on("data", (chunk: Buffer) => {
|
||||
stderrBytes += chunk.length;
|
||||
if (stdoutBytes + stderrBytes > options.maxOutputBytes)
|
||||
stop("output-limit");
|
||||
else stderr.push(chunk);
|
||||
stderr.push(chunk);
|
||||
});
|
||||
child.once("error", (cause: unknown) =>
|
||||
finish(err(new NativeCommandFailure(tool, "io", null, { cause }))),
|
||||
@@ -251,8 +233,6 @@ const captureProcess = (
|
||||
stderr: Buffer.concat(stderr).toString("utf8"),
|
||||
stdoutBytes,
|
||||
stderrBytes,
|
||||
stdoutTruncated: false,
|
||||
stderrTruncated: false,
|
||||
exitCode: code,
|
||||
signal,
|
||||
}),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
|
||||
import { realpath, stat } from "node:fs/promises";
|
||||
import { dirname, isAbsolute, resolve } from "node:path";
|
||||
import { stat } from "node:fs/promises";
|
||||
|
||||
import { parse as parseXmlPlist } from "plist";
|
||||
import { z } from "zod";
|
||||
@@ -22,7 +22,6 @@ import {
|
||||
AnalysisCancelledError,
|
||||
AnalysisCapabilityUnavailableError,
|
||||
AnalysisOutputError,
|
||||
AnalysisTimeoutError,
|
||||
ProviderAdapterError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
@@ -57,8 +56,6 @@ export const NATIVE_MACOS_PROVIDER_IDENTITY: ProviderIdentity = Object.freeze({
|
||||
version: null,
|
||||
});
|
||||
const IDENTITY = NATIVE_MACOS_PROVIDER_IDENTITY;
|
||||
const TIMEOUT_MS = 30_000;
|
||||
const OUTPUT_LIMIT = 4 * 1024 * 1024;
|
||||
|
||||
/** Read-only semantic provider composed from Xcode command-line utilities. */
|
||||
export class NativeMacOSProvider implements AnalysisProvider {
|
||||
@@ -306,13 +303,10 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<NativeObservation, AnalysisError>> {
|
||||
const requested = parameters.relative_path;
|
||||
if (typeof requested !== "string")
|
||||
const requested = parameters.path;
|
||||
if (requested !== undefined && typeof requested !== "string")
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
"inspect_plist",
|
||||
"relative_path was not a string",
|
||||
),
|
||||
new AnalysisOutputError("inspect_plist", "path was not a string"),
|
||||
);
|
||||
const plist = await resolvePlistPath(this.target, requested);
|
||||
if (!plist.ok) return plist;
|
||||
@@ -341,7 +335,7 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
? "xml"
|
||||
: "unknown",
|
||||
...parsed,
|
||||
source_path: requested,
|
||||
source_path: plist.value,
|
||||
provenance,
|
||||
limitations: [],
|
||||
});
|
||||
@@ -349,7 +343,7 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
result: jsonValueSchema.parse(result),
|
||||
provenance,
|
||||
limitations: [],
|
||||
locations: [{ kind: "artifact-path", path: requested }],
|
||||
locations: [{ kind: "artifact-path", path: plist.value }],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -386,8 +380,6 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
): Promise<Result<NativeCommandCapture, AnalysisError>> {
|
||||
const captured = await this.runner.run(tool, arguments_, {
|
||||
...(options.signal === undefined ? {} : { signal: options.signal }),
|
||||
timeoutMs: TIMEOUT_MS,
|
||||
maxOutputBytes: OUTPUT_LIMIT,
|
||||
acceptNonZero: options.acceptNonZero ?? false,
|
||||
});
|
||||
if (captured.ok) return captured;
|
||||
@@ -419,8 +411,6 @@ const translateCommandFailure = (
|
||||
);
|
||||
if (failure.reason === "cancelled")
|
||||
return new AnalysisCancelledError(operation);
|
||||
if (failure.reason === "timeout")
|
||||
return new AnalysisTimeoutError(operation, TIMEOUT_MS);
|
||||
return new ProviderAdapterError(IDENTITY.id, operation, { cause: failure });
|
||||
};
|
||||
|
||||
@@ -442,8 +432,6 @@ const invocation = (
|
||||
exit: { code: capture.exitCode, signal: capture.signal },
|
||||
stdout_bytes: capture.stdoutBytes,
|
||||
stderr_bytes: capture.stderrBytes,
|
||||
stdout_truncated: capture.stdoutTruncated,
|
||||
stderr_truncated: capture.stderrTruncated,
|
||||
});
|
||||
|
||||
const parseEntitlements = (output: string): JsonValue | null => {
|
||||
@@ -457,31 +445,22 @@ const parseEntitlements = (output: string): JsonValue | null => {
|
||||
|
||||
const resolvePlistPath = async (
|
||||
target: BinaryTarget,
|
||||
requested: string,
|
||||
requested: string | undefined,
|
||||
): Promise<Result<string, AnalysisError>> => {
|
||||
try {
|
||||
const source = target.sourcePath ?? target.path;
|
||||
if (requested !== undefined && isAbsolute(requested))
|
||||
return ok(resolve(requested));
|
||||
const sourceMetadata = await stat(source);
|
||||
const root = sourceMetadata.isDirectory() ? source : dirname(target.path);
|
||||
const candidate = resolve(root, requested);
|
||||
if (!within(root, candidate))
|
||||
return err(new ProviderAdapterError(IDENTITY.id, "inspect_plist"));
|
||||
const canonicalRoot = await realpath(root);
|
||||
const canonical = await realpath(candidate);
|
||||
if (!within(canonicalRoot, canonical))
|
||||
return err(new ProviderAdapterError(IDENTITY.id, "inspect_plist"));
|
||||
return ok(canonical);
|
||||
return ok(
|
||||
requested === undefined
|
||||
? resolve(root, "Contents/Info.plist")
|
||||
: resolve(root, requested),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new ProviderAdapterError(IDENTITY.id, "inspect_plist", { cause }),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const within = (root: string, candidate: string): boolean => {
|
||||
const child = relative(root, candidate);
|
||||
return (
|
||||
child === "" ||
|
||||
(!child.startsWith(`..${sep}`) && child !== ".." && !isAbsolute(child))
|
||||
);
|
||||
};
|
||||
|
||||
@@ -46,7 +46,7 @@ const OPTIONAL_VTOOL_COMMAND = ["vtool", ["-show-build"]] as const;
|
||||
const VTOOL_UNAVAILABLE_LIMITATION =
|
||||
"vtool is unavailable; build metadata is normalized from otool only.";
|
||||
|
||||
/** Inspect one Mach-O with bounded native commands and normalized output. */
|
||||
/** Inspect one Mach-O with the required native commands and normalized output. */
|
||||
export const inspectNativeMacho = async (
|
||||
context: NativeMachoInspectionContext,
|
||||
): Promise<Result<NativeMachoObservation, AnalysisError>> => {
|
||||
|
||||
Reference in New Issue
Block a user