From 103acfbbbbb174f988ace51567805153d25f17eb Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 10:21:50 +0800 Subject: [PATCH] refactor(mcp): share first collection page defaults (#16538) * refactor(mcp): share first collection page defaults * fix(mcp): preserve firmware continuation collections --- src/server/toolResult.ts | 117 +++++++++------------ tests/boundary/mcp/largeToolResult.test.ts | 31 ++++++ 2 files changed, 82 insertions(+), 66 deletions(-) diff --git a/src/server/toolResult.ts b/src/server/toolResult.ts index 3bbe5dc2e57..c89e8b8d7a0 100644 --- a/src/server/toolResult.ts +++ b/src/server/toolResult.ts @@ -81,70 +81,55 @@ const retainedEvidenceContinuation = ( ); }; +const firstPage = (kind: string, collection?: string): JsonValue => ({ + kind, + ...(collection === undefined ? {} : { collection }), + offset: 0, + limit: 64, +}); + /** The first page an owner offers when its complete retained result does not fit. */ -const ownerFirstPage = (evidence: Evidence): JsonValue | undefined => - evidence.operation === "trace_web_source_location" - ? { kind: "source-location", collection: "matches", offset: 0 } - : evidence.operation === "analyze_function" || - evidence.operation === "procedure_pseudo_code" - ? { kind: "native", facet: "pseudocode", offset: 0 } - : evidence.operation === "inspect_native_instruction" - ? { - kind: "native-instruction-references", - collection: "incoming_references", - offset: 0, - limit: 64, - } - : evidence.operation === "trace_application_feature" - ? { kind: "feature-trace", collection: "paths", offset: 0, limit: 64 } - : evidence.operation === "batch_decompile" - ? { - kind: "batch-decompile", - collection: "items", - offset: 0, - limit: 64, - } - : evidence.operation === "inspect_esp_nvs" - ? { - kind: "esp-nvs", - collection: "entries", - offset: 0, - limit: 64, - } - : evidence.operation === "inspect_firmware_mmio" - ? { - kind: "firmware-mmio", - collection: "accesses", - offset: 0, - limit: 64, - } - : evidence.operation === "inspect_firmware_vector_table" - ? { - kind: "firmware-vector-table", - collection: (() => { - const arch = ( - evidence.normalized_result as { - architecture?: unknown; - } | null - )?.architecture; - if (arch === "riscv") return "riscv_slots"; - if (arch === "arm-a-r") return "arm_ar_slots"; - if (arch === "avr") return "avr_slots"; - if (arch === "m68k") return "m68k_entries"; - return "arm_entries"; - })(), - offset: 0, - limit: 64, - } - : evidence.operation === "inspect_labview_artifact" - ? { - kind: "labview-artifact", - collection: "blocks", - offset: 0, - limit: 64, - } - : isNativeInventoryOperation(evidence.operation) - ? { kind: "native-inventory", offset: 0, limit: 64 } - : isNativeObservationOperation(evidence.operation) - ? { kind: "native-observation", offset: 0, limit: 64 } - : undefined; +const ownerFirstPage = (evidence: Evidence): JsonValue | undefined => { + switch (evidence.operation) { + case "trace_web_source_location": + return { kind: "source-location", collection: "matches", offset: 0 }; + case "analyze_function": + case "procedure_pseudo_code": + return { kind: "native", facet: "pseudocode", offset: 0 }; + case "inspect_native_instruction": + return firstPage("native-instruction-references", "incoming_references"); + case "trace_application_feature": + return firstPage("feature-trace", "paths"); + case "batch_decompile": + return firstPage("batch-decompile", "items"); + case "inspect_esp_nvs": + return firstPage("esp-nvs", "entries"); + case "inspect_firmware_mmio": + return firstPage("firmware-mmio", "accesses"); + case "inspect_firmware_vector_table": { + const { architecture } = + (evidence.normalized_result as { architecture?: unknown } | null) ?? {}; + const kind = "firmware-vector-table"; + switch (architecture) { + case "riscv": + return firstPage(kind, "riscv_slots"); + case "arm-a-r": + return firstPage(kind, "arm_ar_slots"); + case "avr": + return firstPage(kind, "avr_slots"); + case "m68k": + return firstPage(kind, "m68k_entries"); + default: + return firstPage(kind, "arm_entries"); + } + } + case "inspect_labview_artifact": + return firstPage("labview-artifact", "blocks"); + default: + if (isNativeInventoryOperation(evidence.operation)) + return firstPage("native-inventory"); + if (isNativeObservationOperation(evidence.operation)) + return firstPage("native-observation"); + return undefined; + } +}; diff --git a/tests/boundary/mcp/largeToolResult.test.ts b/tests/boundary/mcp/largeToolResult.test.ts index a87b39f12e3..9f168457a85 100644 --- a/tests/boundary/mcp/largeToolResult.test.ts +++ b/tests/boundary/mcp/largeToolResult.test.ts @@ -329,6 +329,37 @@ describe("oversized Evidence MCP retention", () => { ); }); + it.each([ + ["accesses", "armv7-m"], + ["arm_entries", "armv7-m"], + ["riscv_slots", "riscv"], + ["arm_ar_slots", "arm-a-r"], + ["avr_slots", "avr"], + ["m68k_entries", "m68k"], + ])( + "pages oversized firmware Evidence via %s (%s)", + (collection, architecture) => { + const mmio = collection === "accesses"; + const operation = mmio + ? "inspect_firmware_mmio" + : "inspect_firmware_vector_table"; + const kind = mmio ? "firmware-mmio" : "firmware-vector-table"; + const evidence = createEvidence(unknownTarget("not-observed"), provider, { + operation, + parameters: {}, + result: { architecture, fixture: "x".repeat(MINIMUM_RESPONSE_BYTES) }, + }); + const retained = createToolResultDelivery( + MINIMUM_RESPONSE_BYTES, + ).toEvidenceToolResult(evidence, toolContract(operation), ok("added")); + expect(retained.isError).toBe(true); + expect(retained.structuredContent).toHaveProperty( + "error.details.reported_limits.continuation.arguments.view", + { kind, collection, limit: 64, offset: 0 }, + ); + }, + ); + it("does not claim retention without a recording acknowledgment", () => { const evidence = createEvidence(unknownTarget("not-observed"), provider, { operation: "procedure_pseudo_code",