feat(hopper): report bridge progress diagnostics
This commit is contained in:
@@ -65,7 +65,7 @@ const failures = [
|
||||
[
|
||||
75,
|
||||
"hopper_exited_during_startup",
|
||||
"Hopper exited before the REA bridge was ready. Run rea doctor and retry the analysis.",
|
||||
"Hopper exited before the REA bridge was ready. In CI, run rea doctor --provider hopper --detail full --json; if Hopper showed a UI or license prompt, complete it in the runner user session, otherwise close stale Hopper sessions and retry.",
|
||||
],
|
||||
[
|
||||
76,
|
||||
|
||||
@@ -42,7 +42,11 @@ import {
|
||||
type HopperRequestActivity,
|
||||
} from "./HopperRequestQueue.js";
|
||||
import { HopperResponseStream } from "./HopperResponseStream.js";
|
||||
import { responseResult } from "./protocol.js";
|
||||
import {
|
||||
type HopperBridgeEvent,
|
||||
type HopperBridgeMessage,
|
||||
responseResult,
|
||||
} from "./protocol.js";
|
||||
|
||||
export type { HopperServerInfo } from "./HopperSessionValues.js";
|
||||
|
||||
@@ -125,8 +129,7 @@ export class HopperClient {
|
||||
},
|
||||
);
|
||||
this.#responses = new HopperResponseStream({
|
||||
accept: (response) =>
|
||||
this.#requests.accept(response.id, responseResult(response)),
|
||||
accept: (message) => this.#acceptBridgeMessage(message),
|
||||
hasQueued: (id) => this.#requests.hasQueued(id),
|
||||
nextRequestId: () => this.#nextId,
|
||||
abort: (message, cause) => this.#abortProtocol(message, cause),
|
||||
@@ -450,6 +453,32 @@ export class HopperClient {
|
||||
socket.on("close", this.#onSocketClose);
|
||||
}
|
||||
|
||||
#acceptBridgeMessage(message: HopperBridgeMessage): boolean {
|
||||
if (!("event" in message))
|
||||
return this.#requests.accept(message.id, responseResult(message));
|
||||
if (!this.#requests.acceptEvent(message)) return false;
|
||||
this.#emitBridgeDiagnostic(message);
|
||||
return true;
|
||||
}
|
||||
|
||||
#emitBridgeDiagnostic(message: HopperBridgeEvent): void {
|
||||
if (message.event.type !== "diagnostic") return;
|
||||
try {
|
||||
this.#options.onDiagnostic?.({
|
||||
type: "bridge-diagnostic",
|
||||
request_id: message.id,
|
||||
code: message.event.error.code,
|
||||
category: message.event.error.type,
|
||||
message: message.event.error.message,
|
||||
});
|
||||
} catch {
|
||||
this.#logger.warn(
|
||||
{ requestId: message.id },
|
||||
"Hopper bridge diagnostic consumer rejected an event",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#detachSocket(socket: Socket): void {
|
||||
socket.off("data", this.#onSocketData);
|
||||
socket.off("error", this.#onSocketError);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { HopperDiagnosticType } from "../domain/errors.js";
|
||||
import type { ProcessCleanupResult } from "../process/ProcessOwnership.js";
|
||||
import type { ProviderProcessStopResult } from "../process/ProviderProcess.js";
|
||||
import type { BridgeLaunch } from "./BridgeLauncher.js";
|
||||
@@ -6,6 +7,13 @@ import type { BridgeLaunch } from "./BridgeLauncher.js";
|
||||
export type HopperDiagnostic =
|
||||
| { readonly type: "launcher-stderr"; readonly bytes: number }
|
||||
| { readonly type: "launcher-exit"; readonly code: number | null }
|
||||
| {
|
||||
readonly type: "bridge-diagnostic";
|
||||
readonly request_id: number;
|
||||
readonly code: number;
|
||||
readonly category: HopperDiagnosticType;
|
||||
readonly message: string;
|
||||
}
|
||||
| {
|
||||
readonly type: "owned-shutdown";
|
||||
readonly status: ProviderProcessStopResult["status"];
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { performance } from "node:perf_hooks";
|
||||
|
||||
import type { ProgressReporter } from "../application/ProgressReporter.js";
|
||||
import type {
|
||||
ProgressReporter,
|
||||
ProgressUpdate,
|
||||
} from "../application/ProgressReporter.js";
|
||||
import type { HopperBridgeEvent } from "./protocol.js";
|
||||
import {
|
||||
HopperCancelledError,
|
||||
type HopperError,
|
||||
@@ -127,6 +131,28 @@ export class HopperRequestQueue {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Forward one correlated bridge event without releasing the wire request. */
|
||||
acceptEvent(event: HopperBridgeEvent): boolean {
|
||||
const entry = this.#active;
|
||||
if (entry === undefined || entry.id !== event.id) return false;
|
||||
if (event.event.type === "progress" && !entry.callerSettled) {
|
||||
if (event.event.terminal === true && entry.heartbeat !== undefined) {
|
||||
clearInterval(entry.heartbeat);
|
||||
entry.heartbeat = undefined;
|
||||
}
|
||||
this.#reportUpdate(entry, {
|
||||
phase: event.event.phase,
|
||||
completed: event.event.completed,
|
||||
total: event.event.total,
|
||||
message: event.event.message,
|
||||
...(event.event.terminal === undefined
|
||||
? {}
|
||||
: { terminal: event.event.terminal }),
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Fail the active wire request and every request still waiting in the FIFO. */
|
||||
failAll(error: HopperError): void {
|
||||
const active = this.#active;
|
||||
@@ -267,13 +293,21 @@ export class HopperRequestQueue {
|
||||
}
|
||||
|
||||
#report(entry: QueuedRequest, message: string): void {
|
||||
void entry.progress
|
||||
?.report({
|
||||
phase: "hopper_request",
|
||||
completed: 0,
|
||||
total: 1,
|
||||
message,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
this.#reportUpdate(entry, {
|
||||
phase: "hopper_request",
|
||||
completed: 0,
|
||||
total: 1,
|
||||
message,
|
||||
});
|
||||
}
|
||||
|
||||
#reportUpdate(entry: QueuedRequest, update: ProgressUpdate): void {
|
||||
try {
|
||||
void Promise.resolve(entry.progress?.report(update)).catch(
|
||||
() => undefined,
|
||||
);
|
||||
} catch {
|
||||
// Progress observation cannot change the request outcome.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { parseResponseLine, type HopperResponse } from "./protocol.js";
|
||||
import {
|
||||
parseBridgeMessageLine,
|
||||
type HopperBridgeMessage,
|
||||
} from "./protocol.js";
|
||||
|
||||
const MAX_LINE_BYTES = 10 * 1024 * 1024;
|
||||
|
||||
export interface HopperResponseStreamOptions {
|
||||
readonly accept: (response: HopperResponse) => boolean;
|
||||
readonly accept: (message: HopperBridgeMessage) => boolean;
|
||||
readonly hasQueued: (id: number) => boolean;
|
||||
readonly nextRequestId: () => number;
|
||||
readonly abort: (message: string, cause?: Error) => void;
|
||||
@@ -38,7 +41,7 @@ export class HopperResponseStream {
|
||||
}
|
||||
|
||||
#acceptLine(line: string): boolean {
|
||||
const parsed = parseResponseLine(line);
|
||||
const parsed = parseBridgeMessageLine(line);
|
||||
if (!parsed.ok) {
|
||||
this.options.abort(parsed.error.message, parsed.error);
|
||||
return false;
|
||||
|
||||
+75
-26
@@ -4,35 +4,84 @@ import { HopperProtocolError, HopperRemoteError } from "../domain/errors.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
const remoteErrorSchema = z
|
||||
.object({
|
||||
code: z.number().int(),
|
||||
message: z.string().max(512),
|
||||
type: z
|
||||
.enum([
|
||||
"remote",
|
||||
"authorization",
|
||||
"invalid_request",
|
||||
"capability_unavailable",
|
||||
"bridge_exception",
|
||||
])
|
||||
.default("remote"),
|
||||
})
|
||||
.strict();
|
||||
|
||||
const responseSchema = z.union([
|
||||
z.object({
|
||||
id: z.number().int().nonnegative(),
|
||||
result: jsonValueSchema,
|
||||
}),
|
||||
z.object({
|
||||
id: z.number().int().nonnegative(),
|
||||
error: z.object({
|
||||
code: z.number().int(),
|
||||
message: z.string(),
|
||||
type: z
|
||||
.enum([
|
||||
"remote",
|
||||
"authorization",
|
||||
"invalid_request",
|
||||
"capability_unavailable",
|
||||
"bridge_exception",
|
||||
])
|
||||
.default("remote"),
|
||||
}),
|
||||
}),
|
||||
z
|
||||
.object({
|
||||
id: z.number().int().nonnegative(),
|
||||
result: jsonValueSchema,
|
||||
})
|
||||
.strict(),
|
||||
z
|
||||
.object({
|
||||
id: z.number().int().nonnegative(),
|
||||
error: remoteErrorSchema,
|
||||
})
|
||||
.strict(),
|
||||
]);
|
||||
|
||||
export type HopperResponse = z.infer<typeof responseSchema>;
|
||||
const eventSchema = z
|
||||
.object({
|
||||
id: z.number().int().nonnegative(),
|
||||
event: z.discriminatedUnion("type", [
|
||||
z
|
||||
.object({
|
||||
type: z.literal("progress"),
|
||||
phase: z.string().min(1).max(64),
|
||||
completed: z.number().nonnegative().finite(),
|
||||
total: z.number().nonnegative().finite().nullable(),
|
||||
message: z.string().min(1).max(512),
|
||||
terminal: z.boolean().optional(),
|
||||
})
|
||||
.strict(),
|
||||
z
|
||||
.object({
|
||||
type: z.literal("diagnostic"),
|
||||
error: remoteErrorSchema,
|
||||
})
|
||||
.strict(),
|
||||
]),
|
||||
})
|
||||
.strict()
|
||||
.superRefine((message, context) => {
|
||||
if (
|
||||
message.event.type === "progress" &&
|
||||
message.event.total !== null &&
|
||||
message.event.completed > message.event.total
|
||||
) {
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "Progress total cannot be less than completed",
|
||||
path: ["event", "total"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/** Parse one complete Hopper NDJSON response line. */
|
||||
export const parseResponseLine = (
|
||||
const messageSchema = z.union([responseSchema, eventSchema]);
|
||||
|
||||
export type HopperResponse = z.infer<typeof responseSchema>;
|
||||
export type HopperBridgeEvent = z.infer<typeof eventSchema>;
|
||||
export type HopperBridgeMessage = z.infer<typeof messageSchema>;
|
||||
|
||||
/** Parse one complete Hopper NDJSON response or event line. */
|
||||
export const parseBridgeMessageLine = (
|
||||
line: string,
|
||||
): Result<HopperResponse, HopperProtocolError> => {
|
||||
): Result<HopperBridgeMessage, HopperProtocolError> => {
|
||||
let decoded: unknown;
|
||||
try {
|
||||
decoded = JSON.parse(line);
|
||||
@@ -42,12 +91,12 @@ export const parseResponseLine = (
|
||||
);
|
||||
}
|
||||
|
||||
const parsed = responseSchema.safeParse(decoded);
|
||||
const parsed = messageSchema.safeParse(decoded);
|
||||
return parsed.success
|
||||
? ok(parsed.data)
|
||||
: err(
|
||||
new HopperProtocolError(
|
||||
"Hopper returned a response outside the bridge contract",
|
||||
"Hopper returned a message outside the bridge contract",
|
||||
{ cause: parsed.error },
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user