mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 22:11:12 +00:00
* refactor(mcp): share first collection page defaults * fix(mcp): preserve firmware continuation collections
136 lines
5.0 KiB
TypeScript
136 lines
5.0 KiB
TypeScript
import { isNativeObservationOperation } from "../domain/native/nativeObservation.js";
|
|
import { isNativeInventoryOperation } from "../domain/native/nativeInventory.js";
|
|
import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server";
|
|
import { ToolResultDelivery } from "./ToolResultDelivery.js";
|
|
import { AGENT_RESULT_BUDGET_ENVIRONMENT } from "../domain/agentResultBudget.js";
|
|
import { INSPECT_ANALYSIS_VIEW_OPERATION } from "../domain/analysisView/analysisViewErrors.js";
|
|
import { inspectAnalysisViewInputSchema } from "../domain/analysisView/analysisViewRequest.js";
|
|
import { smallerBatchDecompileSelection } from "../domain/analysisView/batchDecompileView.js";
|
|
import type { Evidence } from "../domain/evidence.js";
|
|
import { ownedJsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
|
|
|
/** Compose native Evidence continuation with the selected, parsed response budget. */
|
|
export const createToolResultDelivery = (
|
|
maximumResponseBytes: number | undefined,
|
|
/** The server's captured launch environment, read only for its budget seed. */
|
|
environment: Readonly<Record<string, string | undefined>> = {},
|
|
): ToolResultDelivery =>
|
|
new ToolResultDelivery(
|
|
maximumResponseBytes ?? STDIO_DEFAULT_MAX_BUFFER_SIZE,
|
|
retainedEvidenceContinuation,
|
|
undefined,
|
|
environment[AGENT_RESULT_BUDGET_ENVIRONMENT],
|
|
);
|
|
|
|
/**
|
|
* Halve a caller-selected page that did not fit. Null means no smaller page
|
|
* exists; undefined means the selection was not a page, so the owner's first
|
|
* page applies.
|
|
*/
|
|
const smallerPage = (
|
|
selected: JsonValue | undefined,
|
|
): JsonValue | null | undefined => {
|
|
if (
|
|
typeof selected !== "object" ||
|
|
selected === null ||
|
|
Array.isArray(selected) ||
|
|
typeof selected.limit !== "number"
|
|
)
|
|
return undefined;
|
|
return selected.limit > 1
|
|
? { ...selected, limit: Math.floor(selected.limit / 2) }
|
|
: null;
|
|
};
|
|
|
|
/** A named, schema-validated call reading one view of one source. */
|
|
const continuationOf = (
|
|
source: JsonValue,
|
|
view: unknown,
|
|
): Readonly<Record<string, JsonValue>> => {
|
|
const input = inspectAnalysisViewInputSchema.safeParse({ source, view });
|
|
if (!input.success) return {};
|
|
return {
|
|
continuation: {
|
|
name: INSPECT_ANALYSIS_VIEW_OPERATION,
|
|
arguments: ownedJsonValueSchema.parse(input.data),
|
|
},
|
|
};
|
|
};
|
|
|
|
/** Offer a next page for analysis whose owner supports paging. */
|
|
const retainedEvidenceContinuation = (
|
|
evidence: Evidence,
|
|
selected?: JsonValue,
|
|
viewSource?: JsonValue,
|
|
): Readonly<Record<string, JsonValue>> => {
|
|
// A view continues from the caller's own source, which need not be retained.
|
|
if (evidence.operation === INSPECT_ANALYSIS_VIEW_OPERATION) {
|
|
const view = smallerBatchDecompileSelection(evidence.normalized_result);
|
|
return view === undefined || viewSource === undefined
|
|
? {}
|
|
: continuationOf(viewSource, view);
|
|
}
|
|
const smaller = smallerPage(selected);
|
|
if (smaller === null) return {};
|
|
const view = smaller ?? ownerFirstPage(evidence);
|
|
return view === undefined
|
|
? {}
|
|
: continuationOf(
|
|
{ kind: "retained-evidence", evidence_id: evidence.evidence_id },
|
|
view,
|
|
);
|
|
};
|
|
|
|
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 => {
|
|
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;
|
|
}
|
|
};
|