From ae72c30e2c4b37b1ab24028d474e8e40502feb46 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Sun, 22 Nov 2026 03:39:31 +0800 Subject: [PATCH] fix(bun): keep budget refusals out of layout selection (#12763) Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> --- src/artifacts/bun/BunModuleGraphMembers.ts | 22 ++++-- .../bun/BunStandaloneArtifactReader.test.ts | 68 +++++++++++++++++++ tests/acceptance/bunStandaloneCli.test.ts | 44 ++++++++++++ tests/boundary/mcp/bunStandaloneMcp.test.ts | 45 ++++++++++++ 4 files changed, 173 insertions(+), 6 deletions(-) diff --git a/src/artifacts/bun/BunModuleGraphMembers.ts b/src/artifacts/bun/BunModuleGraphMembers.ts index 2c727d7485..468c86e9f1 100644 --- a/src/artifacts/bun/BunModuleGraphMembers.ts +++ b/src/artifacts/bun/BunModuleGraphMembers.ts @@ -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 => { 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; }; diff --git a/src/artifacts/bun/BunStandaloneArtifactReader.test.ts b/src/artifacts/bun/BunStandaloneArtifactReader.test.ts index d06a4ca332..681634fc6a 100644 --- a/src/artifacts/bun/BunStandaloneArtifactReader.test.ts +++ b/src/artifacts/bun/BunStandaloneArtifactReader.test.ts @@ -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( diff --git a/tests/acceptance/bunStandaloneCli.test.ts b/tests/acceptance/bunStandaloneCli.test.ts index f2e08b6ed3..962f0ac23a 100644 --- a/tests/acceptance/bunStandaloneCli.test.ts +++ b/tests/acceptance/bunStandaloneCli.test.ts @@ -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 }) => { diff --git a/tests/boundary/mcp/bunStandaloneMcp.test.ts b/tests/boundary/mcp/bunStandaloneMcp.test.ts index 9802e13ec8..a019373761 100644 --- a/tests/boundary/mcp/bunStandaloneMcp.test.ts +++ b/tests/boundary/mcp/bunStandaloneMcp.test.ts @@ -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" }, + }); + } +});