mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 11:01:11 +00:00
fix(analysis-view): fit protobuf capture pages to byte budget (#15241)
* fix(analysis-view): fit protobuf capture pages to byte budget * test(analysis-view): exercise oversized protobuf field recovery
This commit is contained in:
1 parent
1f1dd15a92
commit
90777b00a1
3 files changed
+196
-13
No files matched your search
@@ -6,6 +6,7 @@ import {
|
||||
isImmutableJsonSnapshot,
|
||||
} from "../immutableJson.js";
|
||||
import * as webNetworkCapture from "../webNetworkCapture.js";
|
||||
import { ANALYSIS_VIEW_PAGE_BYTES } from "./analysisViewErrors.js";
|
||||
import { projectAnalysisView } from "./analysisView.js";
|
||||
import * as protobufCaptureView from "./protobufCaptureView.js";
|
||||
|
||||
@@ -279,9 +280,9 @@ it("reuses one frozen capture snapshot across repeated webNetworkCapture and pro
|
||||
const originalProject = protobufCaptureView.projectProtobufCapturePage;
|
||||
const protobufSpy = vi
|
||||
.spyOn(protobufCaptureView, "projectProtobufCapturePage")
|
||||
.mockImplementation((parsedReport, view) => {
|
||||
.mockImplementation((parsedReport, view, shared) => {
|
||||
passedReports.push(parsedReport);
|
||||
return originalProject(parsedReport, view);
|
||||
return originalProject(parsedReport, view, shared);
|
||||
});
|
||||
try {
|
||||
const summary = projectAnalysisView(sealedParent, { kind: "summary" });
|
||||
@@ -348,3 +349,146 @@ it("reuses one frozen capture snapshot across repeated webNetworkCapture and pro
|
||||
protobufSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("continues byte-limited protobuf pages across decoded bodies", async () => {
|
||||
const { parent, report } = await capture();
|
||||
const original = report.records[0]!.protobuf![0]!;
|
||||
const payloadBytes = 80_004;
|
||||
const fieldBytes = payloadBytes + 4;
|
||||
const frameBytes = 6 * fieldBytes;
|
||||
const records = Array.from({ length: 3 }, (_, ordinal) => ({
|
||||
...report.records[0]!,
|
||||
ordinal,
|
||||
protobuf: [
|
||||
{
|
||||
...original,
|
||||
decoded_bytes: frameBytes + 5,
|
||||
examined_bytes: frameBytes + 5,
|
||||
frames: [
|
||||
{
|
||||
...original.frames[0]!,
|
||||
bytes: frameBytes,
|
||||
examined_bytes: frameBytes,
|
||||
},
|
||||
],
|
||||
fields: Array.from({ length: 6 }, (_, index) => {
|
||||
const offset = 5 + index * fieldBytes;
|
||||
return {
|
||||
...original.fields[0]!,
|
||||
index,
|
||||
offset,
|
||||
field_id: `offset:${String(offset)}`,
|
||||
bytes: fieldBytes,
|
||||
payload_offset: offset + 4,
|
||||
payload_bytes: payloadBytes,
|
||||
text: `${String(ordinal)}/${String(index)}:${"é".repeat(40_000)}`,
|
||||
};
|
||||
}),
|
||||
},
|
||||
],
|
||||
}));
|
||||
const source = {
|
||||
...parent,
|
||||
normalizedResult: { ...report, records, total_records: records.length },
|
||||
};
|
||||
const read = (offset: number) => {
|
||||
const result = projectAnalysisView(source, {
|
||||
kind: "protobuf",
|
||||
collection: "fields",
|
||||
fields: ["text"],
|
||||
offset,
|
||||
limit: 18,
|
||||
});
|
||||
if (!result.ok) throw result.error;
|
||||
if (result.value.kind !== "protobuf")
|
||||
throw new Error("Expected protobuf page");
|
||||
expect(Buffer.byteLength(JSON.stringify(result.value))).toBeLessThanOrEqual(
|
||||
ANALYSIS_VIEW_PAGE_BYTES,
|
||||
);
|
||||
return result.value;
|
||||
};
|
||||
const first = read(0);
|
||||
expect(first.coverage.total).toBe(18);
|
||||
const next = first.coverage.next_offset;
|
||||
if (next === null) throw new Error("Expected protobuf continuation");
|
||||
expect(next).toBe(first.items.length);
|
||||
expect(next).toBeGreaterThan(6);
|
||||
const second = read(next);
|
||||
expect(second.coverage.next_offset).toBeNull();
|
||||
expect(
|
||||
[...first.items, ...second.items].map((row) =>
|
||||
"text" in row ? row.text : null,
|
||||
),
|
||||
).toEqual(
|
||||
records.flatMap((record) =>
|
||||
record.protobuf[0]!.fields.map((field) => field.text),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("names the parent for an oversized field and permits a narrower projection", async () => {
|
||||
const { parent, report } = await capture();
|
||||
const body = report.records[0]!.protobuf![0]!;
|
||||
const payloadBytes = ANALYSIS_VIEW_PAGE_BYTES;
|
||||
const fieldBytes = payloadBytes + 4;
|
||||
const source = {
|
||||
...parent,
|
||||
normalizedResult: {
|
||||
...report,
|
||||
records: [
|
||||
{
|
||||
...report.records[0]!,
|
||||
protobuf: [
|
||||
{
|
||||
...body,
|
||||
decoded_bytes: fieldBytes + 5,
|
||||
examined_bytes: fieldBytes + 5,
|
||||
frames: [
|
||||
{
|
||||
...body.frames[0]!,
|
||||
bytes: fieldBytes,
|
||||
examined_bytes: fieldBytes,
|
||||
},
|
||||
],
|
||||
fields: [
|
||||
{
|
||||
...body.fields[0]!,
|
||||
bytes: fieldBytes,
|
||||
payload_offset: 9,
|
||||
payload_bytes: payloadBytes,
|
||||
text: "x".repeat(payloadBytes),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const view = {
|
||||
kind: "protobuf",
|
||||
collection: "fields",
|
||||
offset: 0,
|
||||
limit: 1,
|
||||
} as const;
|
||||
expect(
|
||||
projectAnalysisView(source, { ...view, fields: ["text"] }),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
_tag: "AnalysisResourceConstraintError",
|
||||
reportedLimits: {
|
||||
collection: "fields",
|
||||
parent_evidence_id: parent.evidenceId,
|
||||
limit_bytes: ANALYSIS_VIEW_PAGE_BYTES,
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(projectAnalysisView(source, { ...view, fields: [] })).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
items: [{ field_id: "offset:5", record_ordinal: 0 }],
|
||||
coverage: { total: 1, next_offset: null },
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,15 @@ import {
|
||||
} from "../protobufWire.js";
|
||||
import type { WebNetworkCapture } from "../webNetworkCapture.js";
|
||||
import { filterSet } from "./selectViewRows.js";
|
||||
import type { JsonValue } from "../jsonValue.js";
|
||||
import { jsonValueByteLength } from "../jsonValueByteLength.js";
|
||||
import { ok } from "../result.js";
|
||||
import {
|
||||
ANALYSIS_VIEW_PAGE_BYTES,
|
||||
analysisViewPageTooLarge,
|
||||
} from "./analysisViewErrors.js";
|
||||
import { viewPageBytesWithDigest } from "./analysisViewPageBudget.js";
|
||||
import { PLACEHOLDER_COVERAGE } from "./byteBoundedViewRows.js";
|
||||
import { pageViewCoverage } from "./analysisViewCoverage.js";
|
||||
|
||||
const body = {
|
||||
@@ -180,9 +189,12 @@ const selectFields = (
|
||||
};
|
||||
|
||||
/** Pages select owner-parsed facts; neither payloads nor descriptors are decoded again. */
|
||||
export const projectProtobufCapturePage = (
|
||||
export const projectProtobufCapturePage = <
|
||||
Shared extends { readonly parent_evidence_id: string },
|
||||
>(
|
||||
report: WebNetworkCapture,
|
||||
view: Request,
|
||||
shared: Shared,
|
||||
) => {
|
||||
const reusable = isImmutableJsonSnapshot(report);
|
||||
let bodies = reusable ? checkedBodies.get(report) : undefined;
|
||||
@@ -207,6 +219,26 @@ export const projectProtobufCapturePage = (
|
||||
: undefined;
|
||||
let total = 0;
|
||||
const items: Record<string, unknown>[] = [];
|
||||
const envelope = {
|
||||
...shared,
|
||||
kind: "protobuf" as const,
|
||||
view,
|
||||
retained_decoded_bodies: bodies.length,
|
||||
};
|
||||
const tooLarge = () =>
|
||||
analysisViewPageTooLarge(
|
||||
shared.parent_evidence_id,
|
||||
"The selected Protobuf projection cannot fit in one analysis-view page.",
|
||||
{ collection: view.collection },
|
||||
);
|
||||
let pageBytes = viewPageBytesWithDigest({
|
||||
...envelope,
|
||||
items: [],
|
||||
source_status: "complete", // Reserve the longest possible status.
|
||||
coverage: PLACEHOLDER_COVERAGE,
|
||||
} as unknown as JsonValue);
|
||||
if (pageBytes > ANALYSIS_VIEW_PAGE_BYTES) return tooLarge();
|
||||
let byteLimited = false;
|
||||
let sourceStatus: "complete" | "partial" | "unknown" =
|
||||
report.protobuf_coverage?.status === "partial"
|
||||
? "partial"
|
||||
@@ -231,7 +263,7 @@ export const projectProtobufCapturePage = (
|
||||
: selectFields(decoding, selection);
|
||||
const from = Math.max(0, view.offset - total);
|
||||
const to = Math.min(rows.length, view.offset + view.limit - total);
|
||||
for (let index = from; index < to; index++) {
|
||||
for (let index = from; index < to && !byteLimited; index++) {
|
||||
const source = rows[index]!;
|
||||
const row: Record<string, unknown> =
|
||||
view.collection === "bodies"
|
||||
@@ -274,7 +306,6 @@ export const projectProtobufCapturePage = (
|
||||
"schema_enum_values",
|
||||
].includes(key),
|
||||
);
|
||||
const chosen = new Set([...mandatory, ...keys]);
|
||||
const projected: Record<string, unknown> = {
|
||||
record_ordinal: recordOrdinal,
|
||||
body_pointer: decoding.pointer,
|
||||
@@ -284,23 +315,32 @@ export const projectProtobufCapturePage = (
|
||||
};
|
||||
for (const key of keys)
|
||||
if (
|
||||
chosen.has(key) &&
|
||||
Object.hasOwn(row, key) &&
|
||||
!["frames", "fields", "pointer"].includes(key)
|
||||
)
|
||||
projected[key] = row[key];
|
||||
for (const key of mandatory)
|
||||
if (Object.hasOwn(row, key)) projected[key] = row[key];
|
||||
const rowBytes =
|
||||
jsonValueByteLength(
|
||||
projected as JsonValue,
|
||||
ANALYSIS_VIEW_PAGE_BYTES - pageBytes,
|
||||
) + Number(items.length > 0);
|
||||
if (pageBytes + rowBytes > ANALYSIS_VIEW_PAGE_BYTES) {
|
||||
if (items.length === 0) return tooLarge();
|
||||
// Keep counting matches and collecting source coverage across later bodies.
|
||||
byteLimited = true;
|
||||
break;
|
||||
}
|
||||
pageBytes += rowBytes;
|
||||
items.push(projected);
|
||||
}
|
||||
total += rows.length;
|
||||
}
|
||||
return {
|
||||
kind: "protobuf" as const,
|
||||
view,
|
||||
return ok({
|
||||
...envelope,
|
||||
items: protobufViewItemsSchema.parse(items),
|
||||
source_status: sourceStatus,
|
||||
retained_decoded_bodies: bodies.length,
|
||||
coverage: pageViewCoverage(view.offset, items.length, total),
|
||||
};
|
||||
});
|
||||
};
|
||||
@@ -524,9 +524,8 @@ export const projectWebNetworkCaptureView = (
|
||||
],
|
||||
};
|
||||
if (view.kind === "protobuf")
|
||||
return ok({
|
||||
return projectProtobufCapturePage(report, view, {
|
||||
...shared,
|
||||
...projectProtobufCapturePage(report, view),
|
||||
unknowns: [
|
||||
...shared.unknowns,
|
||||
"Decoding covers selected retained bodies only. Exhausting wire fields does not establish schema completeness or business meaning; compressed bodies and unsupported schema facets remain unknown.",
|
||||
|
||||
Reference in new issue
Block a user