fix(providers): preserve observations and primary cleanup failures

This commit is contained in:
morluto
2026-10-09 21:01:52 +08:00
parent 88640f60fe
commit 0696bde266
18 changed files with 629 additions and 232 deletions
@@ -4,6 +4,9 @@ import {
closeInspectorConnection,
inspectorCleanupError,
} from "./V8InspectorProvider.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { analysisErrorProjectionSchema } from "../contracts/errorSchemas.js";
import { javascriptRuntimeObservationSchema } from "../domain/javascript/javascriptRuntimeObservation.js";
it("returns promptly on cancellation while a V8 socket close is hung", async () => {
const controller = new AbortController();
@@ -32,6 +35,59 @@ it("maps a rejected close to incomplete cleanup and preserves the primary failur
expect(error.cleanupIncomplete).toBe(true);
expect(error.cleanupResources).toEqual(["browser_transport"]);
expect(error.cause).toBeInstanceOf(AggregateError);
if (!(error.cause instanceof AggregateError)) return;
expect(error.cause.errors).toEqual([primary, cleanup]);
expect(error.cause).toMatchObject({ errors: [primary, cleanup] });
});
it("projects a completed runtime observation with the cleanup failure", () => {
const observation = javascriptRuntimeObservationSchema.parse({
runtime: {
product: "Node.js",
protocol_version: "1.3",
v8_version: "12.0",
},
target: {
target_id: "target-1",
protocol_type: "node",
attached: false,
location: { kind: "file", file_path: "/fixture.js" },
runtime_kind: "node",
runtime_kind_authority: "caller-declared-unverified",
},
capture: {
observation_ms: 0,
events_observed: 0,
events_retained: 0,
events_dropped: 0,
metadata_bytes_retained: 0,
truncated: false,
truncation_reasons: [],
},
scripts: {
items: [],
observed_total: 0,
excluded: { unsupported_location: 0, invalid_protocol_value: 0 },
},
execution_contexts: [],
directly_observed: ["Inspector target attached and queried."],
unavailable_without_instrumentation: [],
unknowns: [],
limitations: [],
});
const primary = new Error("observation failed");
const cleanup = new Error("socket close failed");
const error = inspectorCleanupError(primary, cleanup, true, observation);
const cause = error.cause;
expect(cause).toBeInstanceOf(AggregateError);
expect(cause).toMatchObject({ errors: [primary, cleanup] });
const projected = projectAnalysisError(error);
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(true);
expect(projected).toMatchObject({
code: "cleanup_incomplete",
details: {
cleanup_reason: "socket close failed",
resources: ["browser_transport"],
partial_observation: observation,
},
});
});
+26 -2
View File
@@ -13,6 +13,7 @@ import {
} from "../domain/javascript/javascriptRuntimeObservation.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import { BrowserObservationError } from "../domain/browserObservationError.js";
import type { AnalysisPartialObservation } from "../domain/analysisErrorBase.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { type BrowserObservationOperation } from "../domain/browserObservationErrors.js";
import { err, ok, type Result } from "../domain/result.js";
@@ -190,7 +191,18 @@ export class V8InspectorProvider implements JavaScriptRuntimeObservationPort {
}
}
if (cleanupFailed) {
return err(inspectorCleanupError(primaryFailure, cleanupFailure, failed));
return err(
inspectorCleanupError(
primaryFailure,
cleanupFailure,
failed,
outcome?.ok === true
? outcome.value
: outcome?.ok === false
? outcome.error.partialObservation
: undefined,
),
);
}
return (
outcome ??
@@ -232,6 +244,7 @@ export const inspectorCleanupError = (
primaryFailure: unknown,
cleanupFailure: unknown,
hasPrimaryFailure: boolean,
partialObservation?: AnalysisPartialObservation,
): BrowserObservationError => {
const cause = hasPrimaryFailure
? new AggregateError(
@@ -239,10 +252,21 @@ export const inspectorCleanupError = (
"Inspector observation and cleanup both failed",
)
: cleanupFailure;
const cleanupReason =
cleanupFailure instanceof Error
? cleanupFailure.message
: String(cleanupFailure);
return new BrowserObservationError(
"observe_javascript_runtime",
"cleanup_failed",
{ cause },
{
cause,
cleanup: {
reason: cleanupReason,
resources: ["browser_transport"],
},
...(partialObservation === undefined ? {} : { partialObservation }),
},
);
};