* fix(cancellation): preserve caller abort reasons through late finalization Caller aborts that arrived during final provider, cleanup, or protocol handling were dropped or replaced by I/O, cleanup, or success results. Each affected path now samples the caller signal after its owner has settled and returns AnalysisCancelledError carrying the exact signal.reason, keeping any completed partial Evidence or cleanup state. - Web script export: keep the exact reason for an interrupted output write, and honor a cancellation fact recorded before a retried capture close succeeds. - Native value tracing and Hopper regex search: check the signal after the final provider reply or regex worker settles. - Apple asset catalog: stop assetutil with SIGTERM so cancellation settles only after the helper exits. - CDP observation: end an armed window on a fatal protocol failure. - ADB bugreport: prefer cancellation over the staging-removal output error when the caller aborted. - JavaScript recovery: check the signal after provider success. - export-web-scripts CLI: run under command cancellation and forward the signal into the export workflow. - Objdump and Rizin CLI: report cancellation after cleanup completes. - Binary session: keep cancellation primary when previous-client retirement fails after abort. Fixes morluto/rea#6042 Fixes morluto/rea#6043 Fixes morluto/rea#6048 Fixes morluto/rea#6049 Fixes morluto/rea#6057 Fixes morluto/rea#6060 Fixes morluto/rea#6078 Fixes morluto/rea#6080 Fixes morluto/rea#6108 Fixes morluto/rea#6111 Fixes morluto/rea#6115 * fix(cancellation): keep caller reasons through export rollback and commit - Rollback failure after a caller abort now returns AnalysisCancelledError with the exact signal.reason, attaching the rollback failure as cleanup and retaining any published result as partial observation. - A capture close retry that succeeds after abort returns cancellation instead of the stale cleanup diagnostic. - An abort that lands during the final output commit returns cancellation carrying the completed manifest as partial observation. - Apple assetutil stop test: abort and await the run in finally so a timed-out wait does not leave the child running, and skip on Windows where the JavaScript SIGTERM handler cannot run. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 35b247e9-c793-4b8f-979a-807c27112cf5 * fix(cancellation): bind export cancellations to cleanup and caller reasons - A rollback-failure cancellation is bound to the retained cleanup lease, so a later successful close clears cleanup_incomplete instead of reporting a residual directory that no longer exists. - mapExportFailure wraps the caller's own AnalysisCancelledError reason under export_web_scripts rather than returning it as the export's error. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 35b247e9-c793-4b8f-979a-807c27112cf5 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> Copilot-Session: 35b247e9-c793-4b8f-979a-807c27112cf5
398 lines
12 KiB
TypeScript
398 lines
12 KiB
TypeScript
import { expect, it } from "vitest";
|
|
|
|
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
|
import { observeWebSessionInputSchema } from "../../../src/domain/browserSession.js";
|
|
import {
|
|
startFakeCdpBrowser,
|
|
type FakeOptions,
|
|
} from "../../fixtures/fakeCdpBrowser.js";
|
|
import { frameNavigated, trackBrowser } from "./cdpBrowserProvider.support.js";
|
|
|
|
const observeDuringClockSampling = async (
|
|
commandEvents: NonNullable<FakeOptions["commandEvents"]>,
|
|
) => {
|
|
return observeWithFake({
|
|
eventsBeforeReply: ["Performance.getMetrics"],
|
|
commandResult: (command) =>
|
|
command.method === "Performance.getMetrics"
|
|
? { metrics: [{ name: "Timestamp", value: 20 }] }
|
|
: undefined,
|
|
commandEvents,
|
|
});
|
|
};
|
|
|
|
const observeWithFake = async (options: FakeOptions) => {
|
|
const browser = await startFakeCdpBrowser(options);
|
|
trackBrowser(browser);
|
|
const result = await new CdpBrowserProvider().observeSession(
|
|
observeWebSessionInputSchema.parse({
|
|
cdp_endpoint: browser.endpoint,
|
|
allowed_origins: [browser.allowedOrigin],
|
|
target_id: "allowed-page",
|
|
observation_ms: 20,
|
|
}),
|
|
);
|
|
if (!result.ok) throw result.error;
|
|
return { browser, observation: result.value };
|
|
};
|
|
|
|
const laterInWindowRequest = (
|
|
command: Parameters<NonNullable<FakeOptions["commandEvents"]>>[0],
|
|
origin: string,
|
|
) => ({
|
|
method: "Network.requestWillBeSent",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
requestId: "later-in-window-request",
|
|
timestamp: 21,
|
|
type: "Document",
|
|
frameId: "frame-main",
|
|
redirectResponse: { url: `${origin}/prior` },
|
|
request: { url: `${origin}/later` },
|
|
},
|
|
});
|
|
|
|
const expectNoCommittedFrameWithInvalidTimeline = (
|
|
observation: Awaited<ReturnType<typeof observeWithFake>>["observation"],
|
|
) => {
|
|
expect(observation.timeline).not.toContainEqual(
|
|
expect.objectContaining({ type: "navigation_committed" }),
|
|
);
|
|
expect(observation.completeness.excluded).toContainEqual(
|
|
expect.objectContaining({
|
|
section: "timeline",
|
|
reason: "invalid_protocol_value",
|
|
}),
|
|
);
|
|
};
|
|
|
|
it("fails closed on an untimestamped cross-origin main-frame commit during clock sampling", async () => {
|
|
const { observation } = await observeDuringClockSampling(
|
|
(command, origin) => {
|
|
if (command.method !== "Performance.getMetrics") return undefined;
|
|
return [
|
|
frameNavigated(command, {
|
|
id: "frame-main",
|
|
loaderId: "outside-loader",
|
|
url: "https://outside.invalid/path",
|
|
}),
|
|
laterInWindowRequest(command, origin),
|
|
];
|
|
},
|
|
);
|
|
|
|
expect(observation.window.end_reason).toBe("target_left_scope");
|
|
expect(observation.target.final_url).toBeNull();
|
|
expect(observation.timeline).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "navigation_committed",
|
|
loader_id: "outside-loader",
|
|
timestamp: null,
|
|
destination_scope: "outside_policy",
|
|
url: null,
|
|
}),
|
|
);
|
|
expect(observation.timeline).not.toContainEqual(
|
|
expect.objectContaining({ request_id: "later-in-window-request" }),
|
|
);
|
|
});
|
|
|
|
it("keeps an unidentifiable frame commit unknown without inventing a main-frame event", async () => {
|
|
const { observation } = await observeDuringClockSampling((command) =>
|
|
command.method === "Performance.getMetrics"
|
|
? [frameNavigated(command, { url: "https://outside.invalid/unknown" })]
|
|
: undefined,
|
|
);
|
|
|
|
expect(observation.window.end_reason).toBe("window_elapsed");
|
|
expectNoCommittedFrameWithInvalidTimeline(observation);
|
|
});
|
|
|
|
it("marks a frame event with absent parameters as unknown timeline evidence", async () => {
|
|
const { observation } = await observeDuringClockSampling((command) =>
|
|
command.method === "Performance.getMetrics"
|
|
? [
|
|
{
|
|
method: "Page.frameNavigated",
|
|
sessionId: command.sessionId,
|
|
params: undefined,
|
|
},
|
|
]
|
|
: undefined,
|
|
);
|
|
|
|
expectNoCommittedFrameWithInvalidTimeline(observation);
|
|
expect(observation.completeness.unavailable_sections).toContain("timeline");
|
|
});
|
|
|
|
it("records a malformed child identity without treating it as the main frame", async () => {
|
|
const { observation } = await observeDuringClockSampling((command) =>
|
|
command.method === "Performance.getMetrics"
|
|
? [
|
|
frameNavigated(command, {
|
|
id: 3,
|
|
parentId: "frame-main",
|
|
url: "https://outside.invalid/child",
|
|
}),
|
|
]
|
|
: undefined,
|
|
);
|
|
|
|
expect(observation.window.end_reason).toBe("window_elapsed");
|
|
expectNoCommittedFrameWithInvalidTimeline(observation);
|
|
});
|
|
|
|
it("updates the main frame from a pre-arm root replacement without inventing a navigation row", async () => {
|
|
const { observation } = await observeDuringClockSampling((command, origin) =>
|
|
command.method === "Performance.getMetrics"
|
|
? [
|
|
frameNavigated(command, {
|
|
id: "replacement-main",
|
|
loaderId: "replacement-loader",
|
|
url: `${origin}/app?token=frame-secret`,
|
|
}),
|
|
{
|
|
method: "Page.lifecycleEvent",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
frameId: "replacement-main",
|
|
loaderId: "replacement-loader",
|
|
name: "DOMContentLoaded",
|
|
timestamp: 21,
|
|
},
|
|
},
|
|
]
|
|
: undefined,
|
|
);
|
|
|
|
expect(observation.timeline).not.toContainEqual(
|
|
expect.objectContaining({ type: "navigation_committed" }),
|
|
);
|
|
expect(observation.timeline).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "lifecycle",
|
|
frame_id: "replacement-main",
|
|
loader_id: "replacement-loader",
|
|
timestamp: 21,
|
|
detail: "domcontentloaded",
|
|
}),
|
|
);
|
|
expect(observation.limitations).toEqual(
|
|
expect.arrayContaining([
|
|
expect.stringContaining("Buffered events with unknown timing"),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it("fails closed on an untimestamped out-of-scope main-frame request during clock sampling", async () => {
|
|
const { observation } = await observeDuringClockSampling(
|
|
(command, origin) => {
|
|
if (command.method !== "Performance.getMetrics") return undefined;
|
|
return [
|
|
{
|
|
method: "Page.frameRequestedNavigation",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
frameId: "frame-main",
|
|
url: "https://outside.invalid/path",
|
|
reason: "scriptInitiated",
|
|
},
|
|
},
|
|
laterInWindowRequest(command, origin),
|
|
];
|
|
},
|
|
);
|
|
|
|
expect(observation.window.end_reason).toBe("target_left_scope");
|
|
expect(observation.timeline).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "navigation_requested",
|
|
timestamp: null,
|
|
destination_scope: "outside_policy",
|
|
url: null,
|
|
}),
|
|
);
|
|
expect(observation.timeline).not.toContainEqual(
|
|
expect.objectContaining({ request_id: "later-in-window-request" }),
|
|
);
|
|
});
|
|
|
|
it("fails closed on an untimestamped out-of-scope redirect during clock sampling", async () => {
|
|
const { observation } = await observeDuringClockSampling(
|
|
(command, origin) => {
|
|
if (command.method !== "Performance.getMetrics") return undefined;
|
|
return [
|
|
{
|
|
method: "Network.requestWillBeSent",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
requestId: "unknown-time-outside-redirect",
|
|
type: "Document",
|
|
frameId: "frame-main",
|
|
redirectResponse: { url: `${origin}/prior` },
|
|
request: { url: "https://outside.invalid/redirected" },
|
|
},
|
|
},
|
|
{
|
|
method: "Network.requestWillBeSent",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
requestId: "later-in-window-request",
|
|
timestamp: 21,
|
|
type: "Document",
|
|
frameId: "frame-main",
|
|
redirectResponse: { url: `${origin}/prior` },
|
|
request: { url: `${origin}/later` },
|
|
},
|
|
},
|
|
];
|
|
},
|
|
);
|
|
|
|
expect(observation.window.end_reason).toBe("target_left_scope");
|
|
expect(observation.target.final_url).toBeNull();
|
|
expect(observation.timeline).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "redirect",
|
|
request_id: "unknown-time-outside-redirect",
|
|
timestamp: null,
|
|
destination_scope: "outside_policy",
|
|
url: null,
|
|
}),
|
|
);
|
|
expect(observation.timeline).not.toContainEqual(
|
|
expect.objectContaining({ request_id: "later-in-window-request" }),
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"Inspector.targetCrashed",
|
|
"target_terminated",
|
|
"target_terminated",
|
|
{
|
|
method: "Inspector.targetCrashed",
|
|
sessionId: "session-1",
|
|
params: {},
|
|
},
|
|
],
|
|
[
|
|
"Target.detachedFromTarget",
|
|
"session_detached",
|
|
"session_detached",
|
|
{
|
|
method: "Target.detachedFromTarget",
|
|
params: { sessionId: "session-1", targetId: "allowed-page" },
|
|
},
|
|
],
|
|
] as const)(
|
|
"ends on buffered untimestamped %s before collecting later events",
|
|
async (_method, endReason, eventType, terminalEvent) => {
|
|
const { observation } = await observeWithFake({
|
|
eventsBeforeReply: ["Performance.getMetrics"],
|
|
commandResult: (command) =>
|
|
command.method === "Performance.getMetrics"
|
|
? { metrics: [{ name: "Timestamp", value: 20 }] }
|
|
: undefined,
|
|
commandEvents: (command, origin) => {
|
|
if (command.method !== "Performance.getMetrics") return undefined;
|
|
return [
|
|
terminalEvent,
|
|
{
|
|
method: "Network.requestWillBeSent",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
requestId: "after-terminal-event",
|
|
timestamp: 21,
|
|
type: "Document",
|
|
frameId: "frame-main",
|
|
redirectResponse: { url: `${origin}/prior` },
|
|
request: { url: `${origin}/later` },
|
|
},
|
|
},
|
|
];
|
|
},
|
|
});
|
|
|
|
expect(observation.window.end_reason).toBe(endReason);
|
|
expect(observation.timeline).toContainEqual(
|
|
expect.objectContaining({
|
|
type: eventType,
|
|
timestamp: null,
|
|
}),
|
|
);
|
|
expect(observation.timeline).not.toContainEqual(
|
|
expect.objectContaining({ request_id: "after-terminal-event" }),
|
|
);
|
|
expect(observation.limitations).toContain(
|
|
"Timeline rows without a comparable producer timestamp have a null timestamp. Buffered events with unknown timing are retained as context only; they do not establish activity inside the selected observation window.",
|
|
);
|
|
},
|
|
);
|
|
|
|
it("represents absent timeline timestamps as unknown and preserves an observed zero", async () => {
|
|
const { browser, observation } = await observeWithFake({
|
|
eventsBeforeReply: ["Page.setLifecycleEventsEnabled"],
|
|
commandEvents: (command, origin) => {
|
|
if (command.method !== "Page.setLifecycleEventsEnabled") return undefined;
|
|
return [
|
|
{
|
|
method: "Page.navigatedWithinDocument",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
frameId: "frame-main",
|
|
url: `${origin}/without-clock-value`,
|
|
navigationType: "historyApi",
|
|
},
|
|
},
|
|
{
|
|
method: "Network.loadingFailed",
|
|
sessionId: command.sessionId,
|
|
params: {
|
|
requestId: "actual-zero-clock-value",
|
|
frameId: "frame-main",
|
|
errorText: "net::ERR_CONNECTION_REFUSED",
|
|
timestamp: 0,
|
|
},
|
|
},
|
|
];
|
|
},
|
|
});
|
|
|
|
expect(observation.timeline).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({
|
|
type: "same_document_navigation",
|
|
url: `${browser.allowedOrigin}/without-clock-value`,
|
|
timestamp: null,
|
|
}),
|
|
expect.objectContaining({
|
|
type: "load_failed",
|
|
request_id: "actual-zero-clock-value",
|
|
timestamp: 0,
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it("ends an armed observation at a fatal protocol failure without waiting out its window", async () => {
|
|
const browser = await startFakeCdpBrowser({
|
|
malformedEventOnMethod: "Page.setLifecycleEventsEnabled",
|
|
});
|
|
trackBrowser(browser);
|
|
const started = performance.now();
|
|
const result = await new CdpBrowserProvider().observeSession(
|
|
observeWebSessionInputSchema.parse({
|
|
cdp_endpoint: browser.endpoint,
|
|
allowed_origins: [browser.allowedOrigin],
|
|
target_id: "allowed-page",
|
|
observation_ms: 10_000,
|
|
}),
|
|
);
|
|
expect(performance.now() - started).toBeLessThan(5_000);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "BrowserObservationError", reason: "protocol_error" },
|
|
});
|
|
}, 15_000);
|