mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
feat(analysis-view): page retained observe_web_execution observations
A retained observe_web_execution observation could only be read back whole, and its script sources make up nearly all of its size. The new web-execution view pages one retained observation. functions lists coverage rows with their script ID, reported URL and function index, filtered by script_ids or executed_only. requests filters by methods. sources lists script metadata with the hash and UTF-8 and UTF-16 sizes but without the text, and source-text pages one script's text by UTF-16 offset. Every page carries the coverage state and script inventory, so a short page still says whether coverage and sources were available. The summary view reports the window, instrumentation, script inventory, coverage context and counts of coverage scripts, functions, executed functions, requests, sources and captured UTF-16 units. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
1 parent
0aca049128
commit
38536f9fdb
5 files changed
+422
No files matched your search
@@ -2,6 +2,7 @@ import { projectWebSourceLocationView } from "./webSourceLocationView.js";
|
||||
import { projectNativeProcedureReferencesView } from "./nativeProcedureReferencesView.js";
|
||||
import { projectPlaywrightTraceView } from "./playwrightTraceView.js";
|
||||
import { projectWebPageView } from "./webPageView.js";
|
||||
import { projectWebExecutionView } from "./webExecutionView.js";
|
||||
import { projectApiContractView } from "./apiContractView.js";
|
||||
import {
|
||||
isNativeObservationOperation,
|
||||
@@ -247,6 +248,7 @@ const retainedViewProjectors = new Map<string, RetainedViewProjector>([
|
||||
retain("inventory_artifact", projectArtifactInventoryView),
|
||||
retain("analyze_web_bundle", projectWebBundleView),
|
||||
retain("inspect_web_page", projectWebPageView),
|
||||
retain("observe_web_execution", projectWebExecutionView),
|
||||
retain("trace_web_source_location", projectWebSourceLocationView),
|
||||
]);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import { elfDependenciesViewRequestSchema } from "./elfDependenciesView.js";
|
||||
import { javaScriptRoutesViewRequestSchema } from "./javascriptRoutesView.js";
|
||||
import { webBundleViewRequestSchema } from "./webBundleView.js";
|
||||
import { webPageViewRequestSchema } from "./webPageView.js";
|
||||
import { webExecutionViewRequestSchema } from "./webExecutionView.js";
|
||||
import { sourceToBundleViewRequestSchema } from "./sourceToBundleView.js";
|
||||
import { endpointComparisonViewRequestSchema } from "./endpointComparisonView.js";
|
||||
import { featureTraceViewRequestSchema } from "./featureTraceView.js";
|
||||
@@ -104,6 +105,7 @@ export const analysisViewRequestSchema = z.discriminatedUnion("kind", [
|
||||
javaScriptRoutesViewRequestSchema,
|
||||
webBundleViewRequestSchema,
|
||||
webPageViewRequestSchema,
|
||||
webExecutionViewRequestSchema,
|
||||
nativeFunctionMatchingViewRequestSchema,
|
||||
sourceToBundleViewRequestSchema,
|
||||
endpointComparisonViewRequestSchema,
|
||||
|
||||
@@ -300,6 +300,10 @@ import {
|
||||
webPagePageViewResultSchema,
|
||||
webPageSummaryViewResultSchema,
|
||||
} from "./summarySchemas/webPageSummarySchemas.js";
|
||||
import {
|
||||
webExecutionPageViewResultSchema,
|
||||
webExecutionSummaryViewResultSchema,
|
||||
} from "./summarySchemas/webExecutionSummarySchemas.js";
|
||||
import { webNetworkCaptureSummaryViewResultSchema } from "./summarySchemas/webNetworkCaptureSummarySchema.js";
|
||||
import {
|
||||
webSourceLocationMatchesViewResultSchema,
|
||||
@@ -382,6 +386,8 @@ export {
|
||||
webBundleSummaryViewResultSchema,
|
||||
webPagePageViewResultSchema,
|
||||
webPageSummaryViewResultSchema,
|
||||
webExecutionPageViewResultSchema,
|
||||
webExecutionSummaryViewResultSchema,
|
||||
webNetworkCaptureSummaryViewResultSchema,
|
||||
webSourceLocationMatchesViewResultSchema,
|
||||
webSourceLocationSummaryViewResultSchema,
|
||||
@@ -690,6 +696,7 @@ export const analysisViewResultSchema = z.discriminatedUnion("kind", [
|
||||
javaScriptRoutesViewResultSchema,
|
||||
webBundlePageViewResultSchema,
|
||||
webPagePageViewResultSchema,
|
||||
webExecutionPageViewResultSchema,
|
||||
sourceToBundlePageViewResultSchema,
|
||||
endpointComparisonPageViewResultSchema,
|
||||
apiContractPageViewResultSchema,
|
||||
@@ -801,6 +808,7 @@ export const analysisViewResultSchema = z.discriminatedUnion("kind", [
|
||||
batchDecompileSummaryViewResultSchema,
|
||||
webBundleSummaryViewResultSchema,
|
||||
webPageSummaryViewResultSchema,
|
||||
webExecutionSummaryViewResultSchema,
|
||||
nugetNupkgSummaryViewResultSchema,
|
||||
]),
|
||||
z.strictObject({
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { z } from "zod";
|
||||
import { webExecutionObservationSchema } from "../../webExecution.js";
|
||||
import {
|
||||
artifactSchema,
|
||||
summaryViewResultBase,
|
||||
viewResultBaseFields,
|
||||
} from "../analysisViewBase.js";
|
||||
import {
|
||||
webExecutionSummarySchema,
|
||||
webExecutionViewItemsSchema,
|
||||
webExecutionViewRequestSchema,
|
||||
} from "../webExecutionView.js";
|
||||
|
||||
/** Counts and context of one retained observe_web_execution observation. */
|
||||
export const webExecutionSummaryViewResultSchema = z.strictObject({
|
||||
...summaryViewResultBase,
|
||||
artifact: artifactSchema.nullable(),
|
||||
parent_operation: z.literal("observe_web_execution"),
|
||||
target_url: z.string(),
|
||||
summary: webExecutionSummarySchema,
|
||||
});
|
||||
|
||||
/** One page of a retained observation's functions, requests, sources or source text. */
|
||||
export const webExecutionPageViewResultSchema = z.strictObject({
|
||||
...viewResultBaseFields,
|
||||
artifact: artifactSchema.nullable(),
|
||||
kind: z.literal("web-execution"),
|
||||
parent_operation: z.literal("observe_web_execution"),
|
||||
target_url: z.string(),
|
||||
view: webExecutionViewRequestSchema,
|
||||
coverage_context: webExecutionObservationSchema.shape.coverage.omit({
|
||||
scripts: true,
|
||||
}),
|
||||
script_inventory: z.strictObject({
|
||||
main_document_scripts: z.int().nonnegative(),
|
||||
not_reported_scripts: z.int().nonnegative(),
|
||||
coverage_absence: z.literal("unknown"),
|
||||
}),
|
||||
items: webExecutionViewItemsSchema,
|
||||
});
|
||||
@@ -0,0 +1,370 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import {
|
||||
webExecutionObservationSchema,
|
||||
type WebExecutionObservation,
|
||||
} from "../webExecution.js";
|
||||
import { err, ok } from "../result.js";
|
||||
import { analysisViewInputError } from "./analysisViewErrors.js";
|
||||
import {
|
||||
createAnalysisViewResultReader,
|
||||
type AnalysisViewParent,
|
||||
} from "./analysisViewParent.js";
|
||||
import {
|
||||
boundedViewMessages,
|
||||
completeWithinViewCoverage,
|
||||
pageViewCoverage,
|
||||
} from "./analysisViewCoverage.js";
|
||||
import {
|
||||
assertOwnedView,
|
||||
type AdmittedView,
|
||||
type SummaryAnalysisView,
|
||||
} from "./analysisViewShape.js";
|
||||
import {
|
||||
analysisTextPage,
|
||||
analysisTextPageItemSchema,
|
||||
} from "./analysisViewText.js";
|
||||
import { selectViewRows } from "./selectViewRows.js";
|
||||
|
||||
export const WEB_EXECUTION_OPERATION = "observe_web_execution" as const;
|
||||
|
||||
const shape = webExecutionObservationSchema.shape;
|
||||
const coverageScriptSchema = shape.coverage.shape.scripts.element;
|
||||
const functionSchema = coverageScriptSchema.shape.functions.element;
|
||||
const requestSchema = shape.requests.element;
|
||||
const sourceSchema = shape.sources.element;
|
||||
|
||||
const pageShape = {
|
||||
kind: z.literal("web-execution"),
|
||||
offset: z.int().nonnegative().default(0),
|
||||
limit: z.int().positive().max(512).default(64),
|
||||
};
|
||||
const names = z.array(z.string().min(1)).max(64).optional();
|
||||
|
||||
/** Pages of one retained observe_web_execution observation. */
|
||||
export const webExecutionViewRequestSchema = z.discriminatedUnion(
|
||||
"collection",
|
||||
[
|
||||
z.strictObject({
|
||||
...pageShape,
|
||||
collection: z.literal("functions"),
|
||||
script_ids: names.describe("Exact session script IDs"),
|
||||
executed_only: z
|
||||
.boolean()
|
||||
.default(false)
|
||||
.describe("Only functions with at least one range count above zero"),
|
||||
}),
|
||||
z.strictObject({
|
||||
...pageShape,
|
||||
collection: z.literal("requests"),
|
||||
methods: names.describe("Exact request methods, such as GET"),
|
||||
}),
|
||||
z.strictObject({
|
||||
...pageShape,
|
||||
collection: z.literal("sources"),
|
||||
script_ids: names.describe("Exact session script IDs"),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal("web-execution"),
|
||||
collection: z.literal("source-text"),
|
||||
script_id: z.string().min(1),
|
||||
offset: z
|
||||
.int()
|
||||
.nonnegative()
|
||||
.default(0)
|
||||
.describe("UTF-16 code-unit offset into the captured source"),
|
||||
limit: z.int().positive().max(65_536).default(16_384),
|
||||
}),
|
||||
],
|
||||
);
|
||||
type WebExecutionView = z.output<typeof webExecutionViewRequestSchema>;
|
||||
|
||||
/** A function row with the coverage script that reported it. */
|
||||
export const webExecutionFunctionRowSchema = functionSchema.extend({
|
||||
script_id: coverageScriptSchema.shape.script_id,
|
||||
reported_url: coverageScriptSchema.shape.reported_url,
|
||||
function_index: z.int().nonnegative(),
|
||||
});
|
||||
|
||||
/** Source metadata without the captured text, which pages separately. */
|
||||
export const webExecutionSourceRowSchema = sourceSchema
|
||||
.omit({ source: true })
|
||||
.extend({
|
||||
source: z.discriminatedUnion("state", [
|
||||
z.strictObject({
|
||||
state: z.literal("captured"),
|
||||
sha256: z.string(),
|
||||
utf8_bytes: z.int().nonnegative(),
|
||||
utf16_units: z.int().nonnegative(),
|
||||
}),
|
||||
z.strictObject({
|
||||
state: z.enum(["unavailable", "excluded"]),
|
||||
reason: z.string(),
|
||||
}),
|
||||
]),
|
||||
});
|
||||
|
||||
export const webExecutionSummarySchema = z.strictObject({
|
||||
browser: shape.browser,
|
||||
target: shape.target,
|
||||
window: shape.window,
|
||||
instrumentation: shape.instrumentation,
|
||||
script_inventory: shape.script_inventory.omit({
|
||||
not_reported_script_ids: true,
|
||||
}),
|
||||
excluded_requests: shape.excluded_requests,
|
||||
coverage: shape.coverage.omit({ scripts: true }),
|
||||
counts: z.strictObject({
|
||||
coverage_scripts: z.int().nonnegative(),
|
||||
functions: z.int().nonnegative(),
|
||||
executed_functions: z.int().nonnegative(),
|
||||
requests: z.int().nonnegative(),
|
||||
sources: z.int().nonnegative(),
|
||||
not_reported_scripts: z.int().nonnegative(),
|
||||
captured_source_utf16_units: z.int().nonnegative(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const webExecutionViewItemsSchema = z.union([
|
||||
z.array(webExecutionFunctionRowSchema),
|
||||
z.array(requestSchema),
|
||||
z.array(webExecutionSourceRowSchema),
|
||||
z.array(
|
||||
analysisTextPageItemSchema.extend({
|
||||
script_id: z.string(),
|
||||
}),
|
||||
),
|
||||
]);
|
||||
|
||||
const readReport = createAnalysisViewResultReader(
|
||||
webExecutionObservationSchema,
|
||||
);
|
||||
|
||||
type FunctionRow = z.output<typeof webExecutionFunctionRowSchema>;
|
||||
type Source = WebExecutionObservation["sources"][number];
|
||||
|
||||
const executed = (row: { readonly ranges: readonly { count: number }[] }) =>
|
||||
row.ranges.some((range) => range.count > 0);
|
||||
|
||||
const selected = (ids: readonly string[] | undefined) =>
|
||||
ids === undefined ? undefined : new Set(ids);
|
||||
|
||||
/**
|
||||
* One page of function rows in coverage order, with the total that match.
|
||||
* Only returned rows are built, so a small page of a large capture stays small.
|
||||
*/
|
||||
const selectFunctionRows = (
|
||||
report: WebExecutionObservation,
|
||||
view: Extract<WebExecutionView, { collection: "functions" }>,
|
||||
): { rows: FunctionRow[]; total: number } => {
|
||||
const ids = selected(view.script_ids);
|
||||
const rows: FunctionRow[] = [];
|
||||
let total = 0;
|
||||
for (const script of report.coverage.scripts) {
|
||||
if (ids !== undefined && !ids.has(script.script_id)) continue;
|
||||
for (let index = 0; index < script.functions.length; index += 1) {
|
||||
const fn = script.functions[index]!;
|
||||
if (view.executed_only && !executed(fn)) continue;
|
||||
if (total >= view.offset && rows.length < view.limit)
|
||||
rows.push({
|
||||
...fn,
|
||||
script_id: script.script_id,
|
||||
reported_url: script.reported_url,
|
||||
function_index: index,
|
||||
});
|
||||
total++;
|
||||
}
|
||||
}
|
||||
return { rows, total };
|
||||
};
|
||||
|
||||
const sourceRow = (
|
||||
row: Source,
|
||||
): z.output<typeof webExecutionSourceRowSchema> => {
|
||||
const { source, ...metadata } = row;
|
||||
return {
|
||||
...metadata,
|
||||
source:
|
||||
source.state === "captured"
|
||||
? {
|
||||
state: "captured",
|
||||
sha256: source.sha256,
|
||||
utf8_bytes: source.utf8_bytes,
|
||||
utf16_units: source.utf16_units,
|
||||
}
|
||||
: { state: source.state, reason: source.reason },
|
||||
};
|
||||
};
|
||||
|
||||
const summarize = (
|
||||
report: WebExecutionObservation,
|
||||
): z.output<typeof webExecutionSummarySchema> => {
|
||||
let functions = 0;
|
||||
let executedFunctions = 0;
|
||||
for (const script of report.coverage.scripts) {
|
||||
functions += script.functions.length;
|
||||
for (const fn of script.functions) if (executed(fn)) executedFunctions++;
|
||||
}
|
||||
const { scripts: _scripts, ...coverage } = report.coverage;
|
||||
const { not_reported_script_ids: notReported, ...scriptInventory } =
|
||||
report.script_inventory;
|
||||
return {
|
||||
browser: report.browser,
|
||||
target: report.target,
|
||||
window: report.window,
|
||||
instrumentation: report.instrumentation,
|
||||
script_inventory: scriptInventory,
|
||||
excluded_requests: report.excluded_requests,
|
||||
coverage,
|
||||
counts: {
|
||||
coverage_scripts: report.coverage.scripts.length,
|
||||
functions,
|
||||
executed_functions: executedFunctions,
|
||||
requests: report.requests.length,
|
||||
sources: report.sources.length,
|
||||
not_reported_scripts: notReported.length,
|
||||
captured_source_utf16_units: report.sources.reduce(
|
||||
(total, row) =>
|
||||
total +
|
||||
(row.source.state === "captured" ? row.source.utf16_units : 0),
|
||||
0,
|
||||
),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Context every page keeps: the coverage state and script inventory, so a
|
||||
* short page still says whether coverage or sources were available.
|
||||
*/
|
||||
const pageContextOf = (report: WebExecutionObservation) => {
|
||||
const { scripts: _coverageScripts, ...coverageContext } = report.coverage;
|
||||
return {
|
||||
coverage_context: coverageContext,
|
||||
script_inventory: {
|
||||
main_document_scripts: report.script_inventory.main_document_scripts,
|
||||
not_reported_scripts:
|
||||
report.script_inventory.not_reported_script_ids.length,
|
||||
coverage_absence: report.script_inventory.coverage_absence,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/** One UTF-16 page of a captured script source, or why it has none. */
|
||||
const sourceTextPage = (
|
||||
report: WebExecutionObservation,
|
||||
view: Extract<WebExecutionView, { collection: "source-text" }>,
|
||||
) => {
|
||||
const captured = report.sources.find(
|
||||
(row) => row.script_id === view.script_id,
|
||||
)?.source;
|
||||
if (captured?.state !== "captured")
|
||||
return err(
|
||||
analysisViewInputError([
|
||||
{
|
||||
path: ["view", "script_id"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
captured === undefined
|
||||
? "No source with this script_id was observed; list sources first."
|
||||
: `Source text was not captured: ${captured.reason}`,
|
||||
},
|
||||
]),
|
||||
);
|
||||
return analysisTextPage(captured.text, view.offset, view.limit, "Source");
|
||||
};
|
||||
|
||||
/** Project a retained execution observation's summary or one page. */
|
||||
export const projectWebExecutionView = (
|
||||
parent: AnalysisViewParent,
|
||||
admitted: AdmittedView,
|
||||
) => {
|
||||
if (admitted.kind !== "web-execution" && admitted.kind !== "summary")
|
||||
return err(
|
||||
analysisViewInputError([
|
||||
{
|
||||
path: ["view", "kind"],
|
||||
reason: "invalid_value",
|
||||
message: "Select a web-execution or summary view.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
assertOwnedView<SummaryAnalysisView | WebExecutionView>(
|
||||
admitted,
|
||||
"summary",
|
||||
"web-execution",
|
||||
);
|
||||
const parsed = readReport(parent);
|
||||
if (!parsed.ok) return parsed;
|
||||
const report = parsed.value;
|
||||
const shared = {
|
||||
parent_evidence_id: parent.evidenceId,
|
||||
parent_operation: WEB_EXECUTION_OPERATION,
|
||||
parent_digest: parent.evidenceId.slice(3),
|
||||
artifact: parent.artifact ?? null,
|
||||
target_url: report.target.initial_url,
|
||||
limitations: boundedViewMessages(
|
||||
[...parent.limitations, ...report.limitations],
|
||||
parent.evidenceId,
|
||||
"limitations",
|
||||
),
|
||||
unknowns: [
|
||||
"Execution outside the armed window, in workers, or caused by UI actions remains unknown.",
|
||||
],
|
||||
};
|
||||
if (admitted.kind === "summary")
|
||||
return ok({
|
||||
...shared,
|
||||
kind: "summary" as const,
|
||||
view: admitted,
|
||||
summary: summarize(report),
|
||||
coverage: completeWithinViewCoverage(1),
|
||||
});
|
||||
const view = admitted;
|
||||
const pageContext = { ...shared, ...pageContextOf(report) };
|
||||
const page = <Row>(items: Row[], total: number, offset: number) =>
|
||||
ok({
|
||||
...pageContext,
|
||||
kind: "web-execution" as const,
|
||||
view,
|
||||
items,
|
||||
coverage: pageViewCoverage(offset, items.length, total),
|
||||
});
|
||||
switch (view.collection) {
|
||||
case "functions": {
|
||||
const rows = selectFunctionRows(report, view);
|
||||
return page(rows.rows, rows.total, view.offset);
|
||||
}
|
||||
case "requests": {
|
||||
const methods = selected(view.methods);
|
||||
const rows = selectViewRows(
|
||||
report.requests,
|
||||
view,
|
||||
methods === undefined ? undefined : (row) => methods.has(row.method),
|
||||
(row) => row,
|
||||
);
|
||||
return page(rows.rows, rows.total, view.offset);
|
||||
}
|
||||
case "sources": {
|
||||
const ids = selected(view.script_ids);
|
||||
const rows = selectViewRows(
|
||||
report.sources,
|
||||
view,
|
||||
ids === undefined ? undefined : (row) => ids.has(row.script_id),
|
||||
sourceRow,
|
||||
);
|
||||
return page(rows.rows, rows.total, view.offset);
|
||||
}
|
||||
case "source-text": {
|
||||
const text = sourceTextPage(report, view);
|
||||
if (!text.ok) return text;
|
||||
return ok({
|
||||
...pageContext,
|
||||
kind: "web-execution" as const,
|
||||
view,
|
||||
items: [{ ...text.value.item, script_id: view.script_id }],
|
||||
coverage: text.value.coverage,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user