fix(capture): retain Electron observations and HAR resource failures

This commit is contained in:
morluto
2026-10-09 06:49:02 +08:00
parent 6c13fc8e0a
commit 44fd18305f
27 changed files with 1462 additions and 145 deletions
+3 -1
View File
@@ -86,9 +86,11 @@ import type {
} from "./process/processCapture.js";
import type { FileOffsetPartialObservation } from "./native/fileOffsetPartialObservation.js";
import type { NativeCallPartialObservation } from "./native/nativeCallPartialObservation.js";
import type { ElectronActivePartialObservation } from "./javascript/electronActiveObservation.js";
/** Provider-neutral evidence collected before a typed analysis failure. */
export type AnalysisPartialObservation =
| PartialProcessCaptureObservation
| NativeCallPartialObservation
| FileOffsetPartialObservation;
| FileOffsetPartialObservation
| ElectronActivePartialObservation;
+3 -1
View File
@@ -96,7 +96,7 @@ export const analysisErrorCategory = (
if (error._tag === "ProcessCaptureError")
return error.userCategory ?? "execution_failure";
if (error instanceof BrowserObservationError)
return browserErrorCategory(error.reason);
return error.userCategory ?? browserErrorCategory(error.reason);
if (
error instanceof HopperRemoteError &&
error.diagnosticType === "invalid_request"
@@ -117,6 +117,8 @@ export const analysisErrorCategory = (
const browserErrorCategory = (
reason: BrowserObservationError["reason"],
): AnalysisErrorProjection["category"] => {
if (reason === "cancelled") return "cancelled";
if (reason === "timeout") return "timeout";
if (reason === "payload_limit") return "truncated";
if (
reason === "target_not_found" ||
+5 -1
View File
@@ -96,7 +96,9 @@ const underlyingErrorCode = (
? "provider_unavailable"
: "capability_unavailable";
if (error instanceof BrowserObservationError)
return browserErrorCode(error.reason);
return error.userCategory === "cancelled"
? "cancelled"
: browserErrorCode(error.reason);
if (error instanceof ArtifactOperationError)
return artifactOperationCode(error);
if (error instanceof EvidenceFileError) return evidenceFileCode(error.reason);
@@ -109,6 +111,8 @@ const underlyingErrorCode = (
const browserErrorCode = (
reason: BrowserObservationError["reason"],
): AnalysisErrorProjection["code"] => {
if (reason === "cancelled") return "cancelled";
if (reason === "timeout") return "provider_timeout";
if (reason === "payload_limit") return "truncated";
if (
reason === "target_not_found" ||
+32 -6
View File
@@ -2,7 +2,10 @@ import type {
BrowserObservationFailureReason,
BrowserObservationOperation,
} from "./browserObservationErrors.js";
import { AnalysisError } from "./analysisErrorBase.js";
import {
AnalysisError,
type AnalysisErrorOptions,
} from "./analysisErrorBase.js";
import { AnalysisCancelledError } from "./analysisErrorCore.js";
/** A bounded passive browser observation failed at its CDP boundary. */
@@ -16,16 +19,39 @@ export class BrowserObservationError extends AnalysisError {
constructor(
readonly operation: BrowserObservationOperation,
readonly reason: BrowserObservationFailureReason,
options?: ErrorOptions & { readonly detail?: string },
options?: AnalysisErrorOptions & { readonly detail?: string },
) {
super(`Browser observation ${operation} failed: ${reason}`, options);
this.userMessage = options?.detail;
this.cleanupIncomplete = reason === "cleanup_failed";
this.userMessage =
options?.detail ?? observationFailureMessage(operation, reason);
this.cleanupIncomplete =
this.cleanup !== undefined || reason === "cleanup_failed";
this.cleanupResources =
reason === "cleanup_failed" ? ["browser_transport"] : [];
this.cleanup?.resources ??
(reason === "cleanup_failed" ? ["browser_transport"] : []);
this.userCategory =
reason === "cancelled" || options?.cause instanceof AnalysisCancelledError
reason === "cancelled" ||
options?.cause instanceof AnalysisCancelledError ||
(options?.cause instanceof AnalysisError &&
options.cause.userCategory === "cancelled")
? "cancelled"
: undefined;
}
}
const observationFailureMessage = (
operation: BrowserObservationOperation,
reason: BrowserObservationFailureReason,
): string | undefined => {
if (reason === "cancelled")
return `${operation} was cancelled. Collected observations and cleanup status are reported when available.`;
if (reason === "timeout")
return `${operation} exceeded its execution deadline. Review the target's responsiveness and retained observations before retrying.`;
if (reason === "endpoint_unreachable")
return `${operation} could not reach the selected loopback debugging endpoint. Verify that the target process is running with debugging enabled and that the endpoint's port matches it, then retry.`;
if (reason === "target_not_found")
return `${operation} could not find the selected target at the debugging endpoint. Refresh target discovery for that endpoint and retry with a returned target ID.`;
if (reason === "disconnected")
return `${operation} lost its debugging connection. Verify that the target process is still running, refresh target discovery, and retry the observation.`;
return undefined;
};
+23
View File
@@ -5,6 +5,7 @@ import {
AnalysisInputError,
AnalysisOutputError,
AnalysisCapabilityUnavailableError,
AnalysisResourceConstraintError,
} from "./analysisErrorCore.js";
import { redactExplicitText } from "./explicitSensitiveValues.js";
import type { JsonValue } from "./jsonValue.js";
@@ -118,6 +119,28 @@ export const redactExplicitFailure = (
: { userMessage: text(error.userMessage) }),
},
);
if (error instanceof AnalysisResourceConstraintError)
return new AnalysisResourceConstraintError(
error.operation,
error.resource,
text(error.reason),
error.reportedLimits === null ? null : diagnostics(error.reportedLimits),
{
cause: error,
...(error.remediationAction === undefined
? {}
: { remediationAction: text(error.remediationAction) }),
...(error.capturedOutput === undefined
? {}
: {
capturedOutput: {
stdout: text(error.capturedOutput.stdout),
stderr: text(error.capturedOutput.stderr),
truncated: error.capturedOutput.truncated,
},
}),
},
);
if (error instanceof ProviderCleanupError)
return new ProviderCleanupError(
error.providerId,
@@ -1,8 +1,8 @@
import { z } from "zod";
import { isAbsoluteLocalPath } from "../localPath.js";
import { isAbsoluteLocalPath, localPathStringSchema } from "../localPath.js";
const pathInputSchema = z.string().trim().min(1).refine(isAbsoluteLocalPath, {
const pathInputSchema = localPathStringSchema.refine(isAbsoluteLocalPath, {
message:
"Electron executable, application, and root paths must be absolute local filesystem paths (for example /Applications/Electron.app/Contents/MacOS/Electron)",
});
@@ -76,7 +76,11 @@ export const electronActiveObservationInputSchema = z.strictObject({
.describe(
"Absolute local filesystem root for application-relative paths; omit to derive it from the application path. Relative paths are rejected.",
),
args: z.array(z.string()).default([]),
args: z
.array(
z.string().regex(/^[^\0]*$/u, "Electron arguments must not contain NUL"),
)
.default([]),
actions: z.array(actionSchema).default([]),
});
export type ElectronActiveObservationInput = z.infer<
@@ -309,3 +313,16 @@ export const electronActiveObservationResultSchema = z.strictObject({
export type ElectronActiveObservationResult = z.infer<
typeof electronActiveObservationResultSchema
>;
/** Observations collected before an Electron capture failed its cleanup boundary. */
export interface ElectronActivePartialObservation {
readonly kind: "electron-active-observation";
readonly capture: Omit<ElectronActiveObservationResult, "application"> & {
readonly application: Omit<
ElectronActiveObservationResult["application"],
"cleanup"
> & {
readonly cleanup: "unverified" | "terminated-owned-process";
};
};
}