fix(bun): keep budget refusals out of layout selection (#12763)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-21 20:39:31 +01:00
1 parent 44e8e72db1
commit ae72c30e2c
4 files changed
+173 -6

No files matched your search

+16 -6
View File
@@ -118,6 +118,10 @@ export const decodeBunModuleGraph = async (
recordBytes,
failure: await assessLayout(graph, records, recordBytes, limits),
});
// A budget refusal leaves that layout unexamined, not structurally invalid.
// Do not let a smaller module allowance choose another revision's records.
for (const { failure } of assessments)
if (failure?.reason === "limit") throw failure;
const valid = assessments.filter((item) => item.failure === null);
if (valid.length > 1)
throw unsupported(
@@ -207,17 +211,23 @@ const assessLayout = async (
): Promise<ArtifactReaderFailure | null> => {
const { data, entryPointId } = graph;
const count = records.length / recordBytes;
if (count > limits.module_count) return bunModuleCountLimit(count, limits);
for (let index = 0; index < count; index += 1) {
if (entryPointId >= count)
return bunFormatFailure(
`entry_point_id ${String(entryPointId)} is outside the module table`,
);
// An invalid prefix can disprove an over-budget interpretation without
// walking its remaining records. A wholly valid prefix cannot prove it.
for (
let index = 0;
index < Math.min(count, limits.module_count);
index += 1
) {
const record = readRecord(records, index, recordBytes);
if (index % 256 === 0) await setImmediate();
const failure = await recordPointerFailure(data, record, recordBytes);
if (failure !== null) return failure;
}
if (entryPointId >= count)
return bunFormatFailure(
`entry_point_id ${String(entryPointId)} is outside the module table`,
);
if (count > limits.module_count) return bunModuleCountLimit(count, limits);
return null;
};
@@ -47,6 +47,74 @@ it.each(["close", "cancel"] as const)(
},
);
it.each([9, 12, 13])(
"does not use a %i-module budget to resolve an ambiguous Bun layout",
async (moduleCount) => {
const path = join(
await createTestTempDirectory("rea-bun-layout-budget-"),
"app",
);
// 468 bytes admit thirteen 36-byte records and nine 52-byte records.
await writeFile(
path,
elfAppendedBunPayload(
bunModuleGraphPayload({
modules: [{ loader: 0, moduleFormat: 0 }],
tableLength: 468,
}),
),
);
const limits = resolveArtifactReaderLimits({
bun: { module_count: moduleCount },
});
const reader = new BunStandaloneArtifactReader(path, undefined, limits.bun);
try {
await expect(collect(reader.entries())).rejects.toMatchObject({
reason: moduleCount < 13 ? "limit" : "unavailable",
});
expect(reader.formatMetadata()).toBeUndefined();
} finally {
await reader.close();
}
},
);
it("admits a unique nine-module layout at its exact budget", async () => {
const path = join(
await createTestTempDirectory("rea-bun-exact-layout-"),
"app",
);
await writeFile(
path,
elfAppendedBunPayload(
bunModuleGraphPayload({
modules: Array.from({ length: 9 }, (_, index) => ({
name: `module-${index}.js`,
contents: Buffer.from(`source-${index}`),
})),
}),
),
);
const reader = new BunStandaloneArtifactReader(
path,
undefined,
resolveArtifactReaderLimits({ bun: { module_count: 9 } }).bun,
);
try {
const entries = await collect(reader.entries());
expect(entries).toHaveLength(9);
expect(reader.formatMetadata()).toMatchObject({
layout_revision: "bun-1.3.9",
module_count: 9,
});
expect((await buffer(await reader.open(entries[8]!))).toString()).toBe(
"source-8",
);
} finally {
await reader.close();
}
});
it("keeps duplicate and unsafe names distinct while extracting exact bytes", async () => {
const path = join(await createTestTempDirectory("rea-bun-reader-"), "app");
await writeFile(
+44
View File
@@ -20,6 +20,50 @@ import {
artifactExtractionResultSchema,
} from "../../src/domain/artifactGraph.js";
cliTest(
"does not resolve Bun layout ambiguity by lowering the module budget",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-bun-layout-ambiguity-");
const path = join(root, "app");
await writeFile(
path,
elfAppendedBunPayload(
bunModuleGraphPayload({
modules: [{ loader: 0, moduleFormat: 0 }],
tableLength: 468,
}),
),
);
for (const moduleCount of [9, 13]) {
const result = await cli.run({
arguments: [
"inspect-artifact",
path,
"--artifact-limits",
JSON.stringify({ bun: { module_count: moduleCount } }),
"--json",
],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: moduleCount < 13 ? "truncated" : "artifact_operation_failed",
retryable: moduleCount < 13,
details:
moduleCount < 13
? {
reason: "limit",
reader_limit: {
parameter: "artifact_limits.bun.module_count",
effective_value: moduleCount,
required_value: 13,
},
}
: { reason: "unavailable" },
});
}
},
);
cliTest(
"distinguishes a truncated Bun graph from an intact graph over the selected limit",
async ({ cli }) => {
@@ -1,3 +1,5 @@
import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
@@ -61,3 +63,46 @@ it("exposes Bun graph metadata through the existing MCP artifact tool", async ()
await session.close();
}
});
it("keeps a budget-limited Bun layout unresolved through MCP", async () => {
const root = await createTestTempDirectory("rea-bun-layout-mcp-");
const path = join(root, "app");
await writeFile(
path,
elfAppendedBunPayload(
bunModuleGraphPayload({
modules: [{ loader: 0, moduleFormat: 0 }],
tableLength: 468,
}),
),
);
const { client } = await createApplicationMcpHarness({
session: createTestBinarySession(new ArtifactProvider({})),
clientIdentity: { name: "bun-layout-budget", version: "1" },
});
expect(
(await client.callTool({ name: "open_binary", arguments: { path } }))
.isError,
).not.toBe(true);
for (const moduleCount of [9, 13]) {
const reply = await client.callTool({
name: "inspect_artifact",
arguments: { artifact_limits: { bun: { module_count: moduleCount } } },
});
expect(reply.isError).toBe(true);
expect(parseMcpToolError(reply).error).toMatchObject({
code: moduleCount < 13 ? "truncated" : "artifact_operation_failed",
retryable: moduleCount < 13,
details:
moduleCount < 13
? {
reason: "limit",
reader_limit: {
parameter: "artifact_limits.bun.module_count",
required_value: 13,
},
}
: { reason: "unavailable" },
});
}
});