Files
reverse-engineering-anything/tests/process-global/modelHostReadFailure.test.ts
T
morluto efbb66c16a fix(errors)!: report wrong-format, missing-file and missing-Evidence refusals by their cause (#15581)
* fix(errors)!: report wrong-format, missing-file and missing-Evidence refusals by their cause

- inspect_labview_artifact refuses a file with no LabVIEW RSRC data as an
  unrecognized target (code unsupported_target, failure_kind unrecognized),
  as its format siblings do, instead of invalid_request. A PE whose
  resource tree could not be fully read is a malformed PE target.
- inspect_gpu_code_objects, inspect_model_artifact and Apktool reported a
  missing or non-regular selected file through their own copy of the
  selected-file refusal (BinaryTargetError, target_unavailable). They now
  use the shared projectSelectedArtifactFileFailure owner
  (invalid_request, conforming, with its own next step), and the copy is
  removed. The shared owner keeps the verified-symlink recovery the copy
  carried.
- A retained Evidence reference the session does not hold is
  invalid_request / invalid_input instead of evidence_integrity_mismatch.
  Altered or unauthenticated Evidence stays evidence_integrity_mismatch.

BREAKING CHANGE: error codes change for three refusals. A non-LabVIEW
file given to inspect_labview_artifact is unsupported_target (was
invalid_request). A missing or non-regular selected file for
inspect_gpu_code_objects, inspect_model_artifact or Apktool operations is
invalid_request (was target_unavailable). A missing retained Evidence
reference (details.reason "missing") is invalid_request with category
invalid_input (was evidence_integrity_mismatch / integrity_mismatch).
Callers that branch on these codes should match the new values;
details.reason, retry_class and the remediation are unchanged in meaning.

* test(model): expect the shared selected-file refusal for a missing model path

The process-global host read failure test still expected
target_unavailable for path errors on a model file. Those are now the
shared selected-file input refusal.

* fix(resources): name a rejected symlink in the shared selected-file refusal

The GPU and model readers' copy of the selected-file refusal said the
path was a symbolic link; the shared owner reported only ELOOP, so
rejectedSymlinkRecovery failed once they moved to it. Use the rejected
symlink's own message, and keep the errno message for every other
selected-path refusal.

* docs(errors): describe missing references and closure members as the caller's selection

The comment above RESOURCE_SNAPSHOT_NOT_APPLICABLE still said a missing
Evidence reference stays an integrity failure. Missing references,
including retained sources and closure members, are invalid_request; only
altered or unauthenticated Evidence stays an integrity failure.
2026-12-01 17:11:08 -05:00

153 lines
5.2 KiB
TypeScript

import { writeFile, type FileHandle } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, expect, vi } from "vitest";
const fault = vi.hoisted(() => ({
path: "",
stage: "" as "" | "open" | "read",
cause: undefined as Error | undefined,
handles: [] as FileHandle[],
}));
vi.mock("node:fs/promises", async (importOriginal) => {
const original = await importOriginal<typeof import("node:fs/promises")>();
return {
...original,
open: async (...args: Parameters<typeof original.open>) => {
const selected = String(args[0]) === fault.path;
if (selected && fault.stage === "open") throw fault.cause;
const handle = await original.open(...args);
if (selected) {
fault.handles.push(handle);
if (fault.stage === "read")
handle.read = (async () => {
throw fault.cause;
}) as FileHandle["read"];
}
return handle;
},
};
});
import { ModelArtifactProvider } from "../../src/model/ModelArtifactProvider.js";
import { ModelArtifactService } from "../../src/application/model/ModelArtifactService.js";
import { EvidenceMcpServer } from "../../src/server/EvidenceMcpServer.js";
import { registerServiceTools } from "../../src/server/serviceToolBindings.js";
import { createToolResultDelivery } from "../../src/server/toolResult.js";
import { silentLogger } from "../../src/logger.js";
import { ggufFixture, safetensorsFixture } from "../fixtures/model/model.js";
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { mcpTest } from "../support/mcp/mcpFixture.js";
afterEach(() => {
fault.path = "";
fault.stage = "";
fault.cause = undefined;
fault.handles = [];
});
mcpTest.for(["gguf", "safetensors"] as const)(
"reports host I/O failures for a valid %s file as execution failures, not unavailable targets",
async (format, { mcp }) => {
const root = await createTestTempDirectory("rea-model-host-failure-");
const path = join(root, `selected.${format}`);
await writeFile(
path,
format === "gguf" ? ggufFixture().bytes : safetensorsFixture().bytes,
);
const provider = new ModelArtifactProvider();
const server = new EvidenceMcpServer(
{ name: "model-host-failure-test", version: "1" },
{},
undefined,
createToolResultDelivery(undefined),
);
const service = new ModelArtifactService(provider);
registerServiceTools(
server,
{ modelArtifact: service },
{ logger: silentLogger },
);
const client = await mcp.connect(server);
const call = (selected = path) =>
client.callTool({
name: "inspect_model_artifact",
arguments: { path: selected, detail: "complete" },
});
try {
expect((await call()).isError).not.toBe(true);
fault.path = path;
for (const [stage, code] of [
["open", "EMFILE"],
["read", "EIO"],
] as const) {
fault.stage = stage;
fault.cause = Object.assign(
new Error(`controlled ${stage} failure (${code})`),
{ code },
);
const { error } = parseMcpToolError(await call());
// Descriptor pressure is occupied host capacity: the unchanged call
// succeeds once it clears, as the accepted retry below shows.
expect(error).toMatchObject(
code === "EMFILE"
? {
code: "resource_constraint",
retryable: true,
details: {
resource: "capacity",
reported_limits: { path, system_code: code },
},
}
: {
code: "execution_failure",
category: "execution_failure",
retryable: false,
details: {
provider_id: "model-artifact",
diagnostics: {
path,
reason: `controlled ${stage} failure (${code})`,
},
},
},
);
expect(error.message).not.toContain("Check that the path exists");
if (stage === "read") expect(fault.handles.length).toBeGreaterThan(0);
expect(fault.handles.every((handle) => handle.fd === -1)).toBe(true);
fault.stage = "";
expect((await call()).isError).not.toBe(true);
}
// Errors about the selected path itself are the shared selected-file
// refusal: the caller selects another file.
for (const code of [
"ENOENT",
"ENOTDIR",
"ELOOP",
"ENAMETOOLONG",
"EISDIR",
"ENOTFILE",
"ESTALE",
]) {
fault.stage = "open";
fault.cause = Object.assign(new Error(`controlled ${code}`), { code });
const { error } = parseMcpToolError(await call());
expect(error, code).toMatchObject({
code: "invalid_request",
category: "invalid_input",
});
}
fault.stage = "";
const { error } = parseMcpToolError(
await call(join(root, `missing.${format}`)),
);
expect(error).toMatchObject({
code: "invalid_request",
category: "invalid_input",
});
} finally {
await provider.close();
}
},
);