fix(native): resample tool identity for each invocation

This commit is contained in:
morluto
2026-10-09 06:32:09 +08:00
parent 9fd65ce071
commit 14b19a0b8a
2 changed files with 50 additions and 36 deletions
+4 -17
View File
@@ -83,8 +83,6 @@ export type NativeToolResolver = (
/** Run allowlisted Xcode tools directly without a shell. */
export class XcrunCommandRunner implements NativeCommandRunner {
readonly #resolved = new Map<string, ResolvedTool>();
constructor(
private readonly resolveTool: NativeToolResolver = (tool, signal) =>
resolveXcrunTool(tool, signal),
@@ -99,7 +97,7 @@ export class XcrunCommandRunner implements NativeCommandRunner {
return Promise.resolve(
err(new NativeCommandFailure(tool, "unavailable")),
);
return this.#resolve(tool, options.signal).then(async (resolved) => {
return this.resolveTool(tool, options.signal).then(async (resolved) => {
if (!resolved.ok) return resolved;
const captured = await captureProcess(
resolved.value.path,
@@ -115,26 +113,15 @@ export class XcrunCommandRunner implements NativeCommandRunner {
executableSha256: resolved.value.sha256,
toolVersion: null,
versionReason:
"Tool exposes no uniform stable version flag; executable digest identifies it.",
"Tool exposes no uniform stable version flag; executable SHA-256 is a prelaunch file sample and does not prove atomic OS image binding.",
arguments: [...arguments_],
})
: captured;
});
}
async #resolve(
tool: string,
signal?: AbortSignal,
): Promise<Result<ResolvedTool, NativeCommandFailure>> {
const existing = this.#resolved.get(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;
}
}
/** Immutable executable identity returned by native tool discovery. */
/** Executable path and SHA-256 sampled during one tool resolution. */
export interface ResolvedTool {
readonly path: string;
readonly sha256: string;
@@ -154,7 +141,7 @@ const ALLOWED_TOOLS = new Set([
"vtool",
]);
/** Locate an Xcode tool through `xcrun --find` and pin its executable digest. */
/** Locate an Xcode tool through `xcrun --find` and sample its executable digest. */
export const resolveXcrunTool = async (
tool: string,
signal?: AbortSignal,
@@ -1,4 +1,5 @@
import { mkdir, rm, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, beforeAll, describe, expect, it } from "vitest";
@@ -74,24 +75,6 @@ describe("native macOS provider discovery", () => {
});
});
it("retries a failed native tool resolution and caches only success", async () => {
let resolutions = 0;
const runner = new XcrunCommandRunner((tool) => {
resolutions += 1;
return Promise.resolve(
resolutions === 1
? err(new NativeCommandFailure(tool, "unavailable"))
: ok({ path: "/usr/bin/true", sha256: "a".repeat(64) }),
);
});
const options = {};
expect((await runner.run("file", [], options)).ok).toBe(false);
expect((await runner.run("file", [], options)).ok).toBe(true);
expect((await runner.run("file", [], options)).ok).toBe(true);
expect(resolutions).toBe(2);
});
it.each(["timeout", "output-limit"] as const)(
"preserves %s resolution failures with their captured output and cleanup status",
async (reason) => {
@@ -134,6 +117,50 @@ describe("native macOS provider discovery", () => {
});
describe("native command output collection", () => {
it("retries discovery and resamples the executable after its tool path is replaced", async () => {
directory = await createTestTempDirectory("rea-native-tool-identity-");
const executable = join(directory, "fixture-tool");
const source = (value: string): string =>
`#!/bin/sh\nprintf '%s\\n' '${value}'\n`;
const install = async (value: string): Promise<string> => {
const contents = source(value);
await writeFile(executable, contents, { mode: 0o700 });
await chmod(executable, 0o700);
return createHash("sha256").update(contents).digest("hex");
};
let unavailable = true;
const runner = new XcrunCommandRunner(async (tool) => {
if (unavailable) {
unavailable = false;
return err(new NativeCommandFailure(tool, "unavailable"));
}
const digest = createHash("sha256")
.update(await readFile(executable))
.digest("hex");
return ok({ path: executable, sha256: digest });
});
const originalDigest = await install("original-tool-output");
expect(await runner.run("file", [], {})).toMatchObject({
ok: false,
error: { reason: "unavailable" },
});
const original = await runner.run("file", [], {});
if (!original.ok) throw original.error;
expect(original.value.stdout).toBe("original-tool-output\n");
expect(original.value.executableSha256).toBe(originalDigest);
const replacementDigest = await install("replacement-tool-output");
const replacement = await runner.run("file", [], {});
if (!replacement.ok) throw replacement.error;
expect(replacement.value.stdout).toBe("replacement-tool-output\n");
expect(replacement.value.executableSha256).toBe(replacementDigest);
expect(replacement.value.versionReason).toContain("prelaunch file sample");
expect(replacement.value.versionReason).toContain(
"does not prove atomic OS image binding",
);
});
it("retains the complete native command output", async () => {
const runner = new XcrunCommandRunner(() =>
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),