Files
rea/src/application/javascript/ElectronObservationService.test.ts
T
de4579e993 fix(observation): keep incomplete cancellation observations (#5644)
* fix(observation): keep incomplete cancellation observations

Service projection treated a retained observation that failed the completed
schema as a malformed completed result. Cancellation became unreadable_output
and the partial was dropped. Incomplete observations now stay on the owner
error, including the exact abort reason.

* fix(observation): drop non-JSON incomplete observations

Keep a retained partial only when projectAnalysisError can serialize it.
Clear a non-JSON partial and record an AnalysisOutputError as a secondary
failure so cancellation and timeout stay the owner error.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(observation): validate incomplete partials without copying

jsonValueSchema.safeParse cloned a JSON-safe partial that projection
copies again. Use jsonValueValidationIssue so the gate only checks.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(ida): expect non-retryable unresolved procedure lookups

Main stops treating invalid_request as retryable. An unchanged missing
procedure stays invalid_request and is not marked retryable.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(observation): share incomplete-observation assertions

The new non-JSON cases repeated the same owner-error checks and pushed
duplication over the 3.5% limit. One expectation helper keeps those
checks without a second copy.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(observation): keep the assertion helper out of the package

The helper imports Vitest. Rename it to a fixture so the production
build excludes it and dist does not import a dev dependency.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(observation): clear non-JSON partials on cleanup primaries

Projection can return the cleanup primary after retry. Clearing only
the wrapper left that primary's partial in place, so projectAnalysisError
threw. The gate now clears every non-JSON partial in the chain.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(observation): freeze retained incomplete observations

A JSON partial stayed a live provider object, so a later mutation could
make projection throw. The gate now keeps one frozen snapshot and still
drops a non-JSON partial without copying it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* refactor(observation): share web runtime partial retention

Observe and inspect used the same promote-or-keep path. One helper
owns that sequence so the two operations cannot drift.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(observation): snapshot a shared partial once

A cleanup wrapper and its primary can hold the same partial object.
The gate now reuses the first frozen snapshot for that object.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(observation): parse a kept partial once

jsonValueSchema.safeParse validates and copies only a JSON partial.
The gate freezes that copy and no longer walks the same value first.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(observation): share repeated observation fixtures

The incomplete-observation tests repeated the same provider setup and
an empty Electron coverage tail, which pushed duplication over 3.5%.
One coverage fixture and shared assertions bring the tree back under
the threshold.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-10-21 04:17:46 +08:00

261 lines
8.2 KiB
TypeScript

import { expect, it } from "vitest";
import {
AnalysisCancelledError,
AnalysisOutputError,
} from "../../domain/analysisErrorCore.js";
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
import { parseEvidence } from "../../domain/evidence.js";
import {
electronPageInspectionSchema,
electronTargetListSchema,
inspectElectronPageInputSchema,
} from "../../domain/javascript/electronObservation.js";
import { emptyElectronPageCoverage } from "../../domain/javascript/electronPageInspection.fixture.js";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import { err, ok } from "../../domain/result.js";
import type { ElectronObservationPort } from "./ElectronObservationPort.js";
import {
inspectElectronPage,
listElectronTargets,
} from "./ElectronObservationService.js";
import {
expectDroppedPartial,
expectKeptPartial,
expectOwnerError,
} from "../ownerObservationExpectation.fixture.js";
const identity = { id: "electron-fixture", name: "Fixture", version: "1" };
const pageProvider = (failure: AnalysisError): ElectronObservationPort => ({
identity: () => identity,
listTargets: async () =>
err(new AnalysisCancelledError("list_electron_targets")),
inspectPage: async () => err(failure),
});
const input = inspectElectronPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "electron-page",
observation_ms: 0,
});
const inspection = electronPageInspectionSchema.parse({
browser: {
product: "Electron",
protocol_version: "1.3",
revision: "fixture",
user_agent: "Electron fixture",
js_version: "V8 fixture",
},
target: {
target_id: "electron-page",
type: "page",
title: "Fixture",
file_path: "/app/index.html",
attached: false,
},
...emptyElectronPageCoverage(),
});
it("converts a retained raw Electron inspection into partial Evidence", async () => {
const failure = new AnalysisCancelledError("inspect_electron_page", {
partialObservation: {
kind: "electron-page-observation",
inspection,
cleanup: "transport-closed",
},
});
const result = await inspectElectronPage(pageProvider(failure), input);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBe(failure);
expect(parseEvidence(result.error.partialObservation)).toMatchObject({
operation: "inspect_electron_page",
normalized_result: {
...inspection,
limitations: [
"REA closed its CDP transport; this does not imply target shutdown.",
],
},
limitations: expect.arrayContaining([
"REA closed its CDP transport; this does not imply target shutdown.",
]),
});
}
});
it("converts a raw inspection retained by provider cleanup failure", async () => {
const failure = new ProviderCleanupError(
"rea-cdp-electron",
["electron-page"],
{ reason: "target detach failed" },
{
operation: "inspect_electron_page",
partialObservation: {
kind: "electron-page-observation",
inspection,
cleanup: "incomplete",
},
},
);
const result = await inspectElectronPage(pageProvider(failure), input);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBe(failure);
expect(result.error.cleanupIncomplete).toBe(true);
expect(parseEvidence(result.error.partialObservation)).toMatchObject({
operation: "inspect_electron_page",
normalized_result: {
...inspection,
limitations: ["CDP transport cleanup remains incomplete."],
},
limitations: expect.arrayContaining([
"CDP transport cleanup remains incomplete.",
]),
});
}
});
it("returns a typed output error for malformed Electron target output after abort", async () => {
const controller = new AbortController();
const malformed = {
browser: {
product: "Electron",
protocol_version: "1.3",
revision: "fixture",
user_agent: "Electron fixture",
js_version: "V8 fixture",
},
targets: [],
excluded: { unsupported_url: 0, non_page: 0 },
limitations: [() => undefined],
} as unknown as ReturnType<typeof electronTargetListSchema.parse>;
const provider: ElectronObservationPort = {
identity: () => identity,
listTargets: async () => {
controller.abort();
return { ok: true, value: malformed };
},
inspectPage: async () =>
err(new AnalysisCancelledError("inspect_electron_page")),
};
const result = await listElectronTargets(
provider,
{
cdp_endpoint: "http://127.0.0.1:9222",
},
{ signal: controller.signal },
);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error).toBeInstanceOf(AnalysisOutputError);
});
it("retains completed target facts when cancellation arrives before Evidence publication", async () => {
const controller = new AbortController();
const targets = electronTargetListSchema.parse({
browser: inspection.browser,
targets: [],
excluded: { unsupported_url: 0, non_page: 0 },
limitations: [],
});
const provider: ElectronObservationPort = {
identity: () => identity,
listTargets: async () => {
controller.abort();
return ok(targets);
},
inspectPage: async () =>
err(new AnalysisCancelledError("inspect_electron_page")),
};
const result = await listElectronTargets(
provider,
{ cdp_endpoint: input.cdp_endpoint },
{ signal: controller.signal },
);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
expect(parseEvidence(result.error.partialObservation)).toMatchObject({
operation: "list_electron_targets",
normalized_result: targets,
});
}
});
it.each(["list", "inspect"] as const)(
"preserves a pre-abort reason before Electron %s provider work",
async (kind) => {
const reason = new Error(`stop Electron ${kind}`);
const controller = new AbortController();
controller.abort(reason);
let providerCalls = 0;
const provider: ElectronObservationPort = {
identity: () => identity,
listTargets: async () => {
providerCalls += 1;
return err(new AnalysisCancelledError("list_electron_targets"));
},
inspectPage: async () => {
providerCalls += 1;
return err(new AnalysisCancelledError("inspect_electron_page"));
},
};
const result =
kind === "list"
? await listElectronTargets(
provider,
{ cdp_endpoint: input.cdp_endpoint },
{ signal: controller.signal },
)
: await inspectElectronPage(provider, input, {
signal: controller.signal,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
expect(result.error.cause).toBe(reason);
}
expect(providerCalls).toBe(0);
},
);
it("keeps cancellation when the retained Electron inspection is incomplete", async () => {
const reason = { requested: "stop before page inspection completed" };
const partial = {
kind: "electron-page-observation",
inspection: { target_id: "electron-page" },
cleanup: "incomplete",
};
const failure = new AnalysisCancelledError("inspect_electron_page", {
cause: reason,
partialObservation: partial as never,
});
const result = await inspectElectronPage(pageProvider(failure), input);
expectKeptPartial(expectOwnerError(result, failure, reason), partial);
});
it.each([
{ inspection: { requests: Number.NaN }, cleanup: "incomplete" as const },
{
inspection: { scripts: [undefined] },
cleanup: "transport-closed" as const,
},
])(
"drops a non-JSON Electron inspection and keeps cancellation primary",
async (unsafe) => {
const reason = { requested: "stop before page inspection completed" };
const failure = new AnalysisCancelledError("inspect_electron_page", {
cause: reason,
partialObservation: {
kind: "electron-page-observation",
...unsafe,
} as never,
});
const result = await inspectElectronPage(pageProvider(failure), input);
expectDroppedPartial(
expectOwnerError(result, failure, reason),
"Electron provider retained a non-JSON incomplete observation.",
);
},
);