From 6940e960baa948ce416a6873286bdebb4e1af7d4 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Tue, 1 Dec 2026 01:46:00 +0800 Subject: [PATCH] fix(analysis-view): fit devicetree property pages to the response budget (#15269) * fix(analysis-view): fit devicetree property pages to the response budget * test(analysis-view): cover multi-property devicetree pages --- src/domain/analysisView/devicetreeView.ts | 61 +++++++++++-- tests/boundary/mcp/devicetreeMcp.test.ts | 102 ++++++++++++++++++++++ 2 files changed, 154 insertions(+), 9 deletions(-) diff --git a/src/domain/analysisView/devicetreeView.ts b/src/domain/analysisView/devicetreeView.ts index e51431eb4e2..bfa04e0cc02 100644 --- a/src/domain/analysisView/devicetreeView.ts +++ b/src/domain/analysisView/devicetreeView.ts @@ -1,3 +1,8 @@ +import type { JsonValue } from "../jsonValue.js"; +import { + PLACEHOLDER_COVERAGE, + selectByteBoundedViewRows, +} from "./byteBoundedViewRows.js"; import { z } from "zod"; import { devicetreeSchema } from "../firmware/devicetree.js"; import { ok } from "../result.js"; @@ -125,6 +130,49 @@ export const projectDevicetreeView = ( return invalidAnalysisView( "The selected node path is absent from this Evidence.", ); + const base = { + ...shared, + kind: "devicetree" as const, + view, + devicetree: { ...context, alias_count: r.summary.aliases.length }, + }; + if (view.collection === "properties" && node !== undefined) { + const project = ( + property: (typeof node.properties)[number], + source_index: number, + ) => ({ ...property, source_index }); + const page = selectByteBoundedViewRows( + node.properties, + view, + { matches: undefined, project }, + { + parentEvidenceId: parent.evidenceId, + envelope: { + ...base, + items: [], + coverage: PLACEHOLDER_COVERAGE, + } as unknown as JsonValue, + facet: "devicetree property", + }, + ); + // Keep individually deliverable properties available even when they exceed + // the view allowance. The adapter applies its own limit to this singleton. + const first = node.properties[view.offset]; + const items = page.ok + ? page.value.rows + : first === undefined + ? [] + : [project(first, view.offset)]; + return ok({ + ...base, + items, + coverage: pageViewCoverage( + view.offset, + items.length, + node.properties.length, + ), + }); + } // Slice retained rows before allocating projections; exact node selection keeps its original index. const page = (rows: readonly T[], originalIndex?: number) => ({ items: rows.slice(view.offset, view.offset + view.limit).map((row, i) => ({ @@ -146,17 +194,12 @@ export const projectDevicetreeView = ( })), total: selectedNodes.total, } - : view.collection === "properties" - ? page(node?.properties ?? []) - : view.collection === "reservations" - ? page(r.reservations) - : page(r.references); + : view.collection === "reservations" + ? page(r.reservations) + : page(r.references); const { items, total } = selected; return ok({ - ...shared, - kind: "devicetree" as const, - view, - devicetree: { ...context, alias_count: r.summary.aliases.length }, + ...base, items, coverage: pageViewCoverage(view.offset, items.length, total), }); diff --git a/tests/boundary/mcp/devicetreeMcp.test.ts b/tests/boundary/mcp/devicetreeMcp.test.ts index 83e04642648..e54abb5054f 100644 --- a/tests/boundary/mcp/devicetreeMcp.test.ts +++ b/tests/boundary/mcp/devicetreeMcp.test.ts @@ -315,3 +315,105 @@ mcpTest( } }, ); + +for (const { textLength, pageLengths } of [ + { textLength: 1_500_000, pageLengths: [1, 1, 1] }, + { textLength: 200_000, pageLengths: [2, 1] }, +]) { + mcpTest( + `continues retained property pages with ${String(textLength)} characters per property`, + async ({ mcp }) => { + const session = createTestBinarySession(() => { + throw new Error("DTB inspection must not start an engine"); + }); + const client = await mcp.connect(createDtbServer(session)); + try { + const path = join( + await createTestTempDirectory("rea-dtb-large-page-"), + "board.dtb", + ); + const text = '"'.repeat(textLength); + const value = dtString(text); + const names = ["compatible", "model", "status"]; + await writeFile( + path, + devicetreeFixture({ + name: "", + children: [ + { + name: "device", + properties: { compatible: value, model: value, status: value }, + }, + ], + }), + ); + const response = await client.callTool({ + name: "inspect_devicetree", + arguments: { path, detail: "summary", max_file_bytes: 16777216 }, + }); + expect(response.isError).not.toBe(true); + const result = toolContract("inspect_devicetree").outputSchema.parse( + response.structuredContent, + ).normalized_result; + if (!("parent_evidence_id" in result)) + throw new Error("Expected summary"); + await unlink(path); + + let offset = 0; + for (const pageLength of pageLengths) { + const reply = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: result.parent_evidence_id, + }, + view: { + kind: "devicetree", + collection: "properties", + path: "/device", + limit: 3, + offset, + }, + }, + }); + expect( + reply.isError, + JSON.stringify(reply.structuredContent).slice(0, 500), + ).not.toBe(true); + const page = z + .object({ + normalized_result: z.object({ + items: z.array( + z.object({ + name: z.string(), + source_index: z.number(), + derived: z.object({ strings: z.array(z.string()) }), + }), + ), + coverage: z.object({ + total: z.number(), + next_offset: z.number().nullable(), + }), + }), + }) + .parse(reply.structuredContent).normalized_result; + expect(page.items).toHaveLength(pageLength); + for (const [index, item] of page.items.entries()) { + expect(item.name).toBe(names[offset + index]); + expect(item.source_index).toBe(offset + index); + expect(item.derived.strings).toEqual([text]); + } + expect(page.coverage).toEqual({ + total: 3, + next_offset: + offset + pageLength < names.length ? offset + pageLength : null, + }); + offset = page.coverage.next_offset ?? offset; + } + } finally { + await session.close(); + } + }, + ); +}