Files
reverse-engineering-anything/src/domain/analysisView/analysisViewCoverage.ts
T
20b802f165 fix(analysis-view): bound limitations and unknowns per view (#2794)
* fix(analysis-view): bound limitations and unknowns per view

Views of a JavaScript application or binary layout copied every parent
limitation into both limitations and unknowns, so a parent with tens of
thousands of limitations produced views as large as the complete record
and exceeded the MCP response budget even for a one-item page. Each view
now carries at most 64 distinct messages per field plus one line naming
the omitted count and the parent Evidence that retains the full list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(analysis-view): assert exact bounded message notices

Source inspection and targeted formatting only. Tests, builds, lint, typechecks and provider workloads were not run at the user's explicit request.

* fix(analysis-view): preserve coverage facts in bounded messages

Reserve JavaScript completeness and unknown-count facts before bulk limitations. Direct omission recovery to the source parent without assuming session retention. Preserve exact threshold assertions and add unrun exhausted-page/item and unretained-inline MCP regressions. Source inspection and targeted formatting only; tests/checks/builds/provider workloads/CI reruns skipped by explicit user request.

* fix(analysis-view): bound repeated message serialization bytes

* fix(analysis-view): let byte budget bound messages

* test(analysis-view): exercise byte-based message omissions

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-14 03:11:48 +08:00

73 lines
2.6 KiB
TypeScript

import { uniqueSorted } from "../canonicalOrdering.js";
import type { AnalysisViewCoverage } from "./analysisView.js";
/** Coverage for a summary, facet, or single selected object. */
export const completeWithinViewCoverage = (
examined: number,
total: number,
): AnalysisViewCoverage => ({
status: "complete-within-view",
examined,
total,
next_offset: null,
exhausted: true,
});
/** Coverage for a stable page, including an offset past the collection. */
export const pageViewCoverage = (
offset: number,
examined: number,
total: number,
): AnalysisViewCoverage => {
const exhausted = offset >= total || offset + examined >= total;
return {
status: examined === 0 ? "empty" : "page",
examined,
total,
next_offset: exhausted ? null : offset + examined,
exhausted,
};
};
/** Per-field repeated MCP message budget, independent of the selected page. */
export const ANALYSIS_VIEW_MESSAGE_BYTE_LIMIT = 256 * 1024;
// A UTF-16 unit needs at most six JSON bytes, or twelve when that JSON is
// escaped into MCP text. Reserve both forms across three Evidence projections.
// This upper bound rejects giant messages without allocating their JSON copies.
const messageWireBytes = (value: string): number => (value.length * 18 + 6) * 3;
/** Deduplicate and bound messages by count and repeated serialized bytes. */
export const boundedViewMessages = (
values: readonly string[],
parentEvidenceId: string,
field: "limitations" | "unknowns",
required: readonly string[] = [],
): string[] => {
const priority = uniqueSorted(required);
const unique = uniqueSorted([...priority, ...values]);
const notice = (omitted: number): string =>
`${String(omitted)} additional ${field} omitted from this view; consult source parent Evidence ${parentEvidenceId} for the complete list (the caller-supplied record, or its retaining session).`;
const totalBytes = unique.reduce(
(total, value) => total + messageWireBytes(value),
0,
);
if (totalBytes <= ANALYSIS_VIEW_MESSAGE_BYTE_LIMIT) return unique;
// Reserve notice space only after proving the complete source set does not fit.
let remaining =
ANALYSIS_VIEW_MESSAGE_BYTE_LIMIT - messageWireBytes(notice(unique.length));
const retained = new Set<string>();
for (const value of [...priority, ...unique]) {
if (retained.has(value)) continue;
const bytes = messageWireBytes(value);
if (bytes > remaining) continue;
retained.add(value);
remaining -= bytes;
}
const omitted = unique.length - retained.size;
return omitted === 0
? unique
: [...uniqueSorted([...retained]), notice(omitted)];
};