feat(hopper): report bridge progress diagnostics
This commit is contained in:
Vendored
+68
-14
@@ -16,6 +16,43 @@ const capabilityUnavailableResult = (id) => ({
|
||||
type: "capability_unavailable",
|
||||
},
|
||||
});
|
||||
const bridgeEventFixtureMessages = (request) => {
|
||||
const progress = {
|
||||
type: "progress",
|
||||
phase: "hopper_bridge",
|
||||
completed: 0,
|
||||
total: 1,
|
||||
message: "Hopper bridge started request",
|
||||
};
|
||||
switch (request.method) {
|
||||
case "wrong_event_id":
|
||||
return [{ id: request.id + 100, event: progress }];
|
||||
case "malformed_event":
|
||||
return [
|
||||
{
|
||||
id: request.id,
|
||||
event: {
|
||||
...progress,
|
||||
completed: 2,
|
||||
message: "invalid progress",
|
||||
},
|
||||
},
|
||||
];
|
||||
case "remote_error": {
|
||||
const error = {
|
||||
code: -32001,
|
||||
message: "safe fake failure",
|
||||
type: "bridge_exception",
|
||||
};
|
||||
return [
|
||||
{ id: request.id, event: { type: "diagnostic", error } },
|
||||
{ id: request.id, error },
|
||||
];
|
||||
}
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
const enhancedFixtureResult = (method) => {
|
||||
switch (method) {
|
||||
case "list_segments":
|
||||
@@ -109,6 +146,33 @@ const enhancedFixtureResult = (method) => {
|
||||
}
|
||||
};
|
||||
|
||||
const sendEchoWithProgress = (send, request) => {
|
||||
send({
|
||||
id: request.id,
|
||||
event: {
|
||||
type: "progress",
|
||||
phase: "hopper_bridge",
|
||||
completed: 0,
|
||||
total: 1,
|
||||
message: "Hopper bridge started request",
|
||||
},
|
||||
});
|
||||
setTimeout(() => {
|
||||
send({
|
||||
id: request.id,
|
||||
event: {
|
||||
type: "progress",
|
||||
phase: "hopper_bridge",
|
||||
completed: 1,
|
||||
total: 1,
|
||||
message: "Hopper bridge completed request",
|
||||
terminal: true,
|
||||
},
|
||||
});
|
||||
send({ id: request.id, result: request.params ?? {} });
|
||||
}, request.params?.delay ?? 0);
|
||||
};
|
||||
|
||||
const server = createServer((socket) => {
|
||||
socket.on("error", () => undefined);
|
||||
socket.setEncoding("utf8");
|
||||
@@ -125,6 +189,7 @@ const server = createServer((socket) => {
|
||||
let newline = buffer.indexOf("\n");
|
||||
while (newline >= 0) {
|
||||
const request = JSON.parse(buffer.slice(0, newline));
|
||||
const bridgeEventMessages = bridgeEventFixtureMessages(request);
|
||||
buffer = buffer.slice(newline + 1);
|
||||
if (request.token !== token) {
|
||||
send({ id: request.id, error: { code: -32001, message: "bad token" } });
|
||||
@@ -152,15 +217,8 @@ const server = createServer((socket) => {
|
||||
socket.write("{not-json}\n");
|
||||
} else if (request.method === "wrong_id") {
|
||||
send({ id: request.id + 100, result: {} });
|
||||
} else if (request.method === "remote_error") {
|
||||
send({
|
||||
id: request.id,
|
||||
error: {
|
||||
code: -32001,
|
||||
message: "safe fake failure",
|
||||
type: "bridge_exception",
|
||||
},
|
||||
});
|
||||
} else if (bridgeEventMessages !== undefined) {
|
||||
for (const message of bridgeEventMessages) send(message);
|
||||
} else if (request.method === "capability_unavailable") {
|
||||
send(capabilityUnavailableResult(request.id));
|
||||
} else if (request.method === "current_document") {
|
||||
@@ -194,11 +252,7 @@ const server = createServer((socket) => {
|
||||
},
|
||||
});
|
||||
} else {
|
||||
const delay = request.params?.delay ?? 0;
|
||||
setTimeout(
|
||||
() => send({ id: request.id, result: request.params ?? {} }),
|
||||
delay,
|
||||
);
|
||||
sendEchoWithProgress(send, request);
|
||||
}
|
||||
newline = buffer.indexOf("\n");
|
||||
}
|
||||
|
||||
+32
@@ -2,7 +2,9 @@
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import sys
|
||||
import threading
|
||||
|
||||
|
||||
class FakeDocument:
|
||||
@@ -74,6 +76,34 @@ def main():
|
||||
"message": str(error),
|
||||
}
|
||||
|
||||
bridge["REA_TOKEN"] = "probe-token"
|
||||
bridge["_dispatch"] = lambda method, params: (
|
||||
(_ for _ in ()).throw(RuntimeError("credential=supersecret"))
|
||||
if method == "fail"
|
||||
else params
|
||||
)
|
||||
server_socket, client_socket = socket.socketpair()
|
||||
worker = threading.Thread(
|
||||
target=bridge["_serve_connection"], args=(server_socket,)
|
||||
)
|
||||
worker.start()
|
||||
client_file = client_socket.makefile("rwb")
|
||||
client_file.write(
|
||||
b'{"id":7,"token":"probe-token","method":"fail","params":{}}\n'
|
||||
)
|
||||
client_file.flush()
|
||||
bridge_messages = [
|
||||
json.loads(client_file.readline().decode("utf-8")) for _ in range(3)
|
||||
]
|
||||
client_file.write(
|
||||
b'{"id":-1,"token":"probe-token","method":"echo","params":{}}\n'
|
||||
)
|
||||
client_file.flush()
|
||||
invalid_id_response = json.loads(client_file.readline().decode("utf-8"))
|
||||
client_file.close()
|
||||
client_socket.close()
|
||||
worker.join(timeout=1)
|
||||
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
@@ -82,6 +112,8 @@ def main():
|
||||
"current_address": current_address,
|
||||
"session_document": selected,
|
||||
"analysis_guard": analysis_guard,
|
||||
"bridge_messages": bridge_messages,
|
||||
"invalid_id_response": invalid_id_response,
|
||||
},
|
||||
sort_keys=True,
|
||||
)
|
||||
|
||||
@@ -24,6 +24,45 @@ const probeResultSchema = z.strictObject({
|
||||
diagnostic_type: z.literal("capability_unavailable"),
|
||||
message: z.string(),
|
||||
}),
|
||||
bridge_messages: z.tuple([
|
||||
z.strictObject({
|
||||
id: z.literal(7),
|
||||
event: z.strictObject({
|
||||
type: z.literal("progress"),
|
||||
phase: z.literal("hopper_bridge"),
|
||||
completed: z.literal(0),
|
||||
total: z.literal(1),
|
||||
message: z.literal("Hopper bridge started request"),
|
||||
}),
|
||||
}),
|
||||
z.strictObject({
|
||||
id: z.literal(7),
|
||||
event: z.strictObject({
|
||||
type: z.literal("diagnostic"),
|
||||
error: z.strictObject({
|
||||
code: z.literal(-32000),
|
||||
message: z.literal("RuntimeError: Hopper bridge operation failed"),
|
||||
type: z.literal("bridge_exception"),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
z.strictObject({
|
||||
id: z.literal(7),
|
||||
error: z.strictObject({
|
||||
code: z.literal(-32000),
|
||||
message: z.literal("RuntimeError: Hopper bridge operation failed"),
|
||||
type: z.literal("bridge_exception"),
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
invalid_id_response: z.strictObject({
|
||||
id: z.literal(0),
|
||||
error: z.strictObject({
|
||||
code: z.literal(-32000),
|
||||
message: z.literal("Invalid Hopper bridge request"),
|
||||
type: z.literal("invalid_request"),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
describe("Hopper API facade", () => {
|
||||
@@ -38,6 +77,7 @@ describe("Hopper API facade", () => {
|
||||
},
|
||||
);
|
||||
const result = probeResultSchema.parse(JSON.parse(stdout));
|
||||
expect(stdout).not.toContain("supersecret");
|
||||
expect(result.analysis_guard.message).toContain(
|
||||
"requires completed Hopper background analysis",
|
||||
);
|
||||
|
||||
@@ -14,6 +14,7 @@ import type {
|
||||
BridgeSession,
|
||||
} from "../src/hopper/BridgeLauncher.js";
|
||||
import { HopperClient } from "../src/hopper/HopperClient.js";
|
||||
import type { HopperDiagnostic } from "../src/hopper/HopperDiagnostics.js";
|
||||
import { LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX } from "../src/hopper/LinuxPrivateDisplayDiagnostic.js";
|
||||
import { providerCleanupFailure } from "../src/hopper/HopperDiagnostics.js";
|
||||
import { cleanupOwnedProcessGroup } from "../src/process/ProcessOwnership.js";
|
||||
@@ -296,6 +297,8 @@ describe("HopperClient", () => {
|
||||
it.each([
|
||||
["malformed", "HopperProtocolError"],
|
||||
["wrong_id", "HopperProtocolError"],
|
||||
["wrong_event_id", "HopperProtocolError"],
|
||||
["malformed_event", "HopperProtocolError"],
|
||||
["remote_error", "HopperRemoteError"],
|
||||
["hang", "HopperTimeoutError"],
|
||||
["exit", "HopperProcessError"],
|
||||
@@ -307,7 +310,15 @@ describe("HopperClient", () => {
|
||||
});
|
||||
|
||||
it("preserves a sanitized bridge exception diagnostic", async () => {
|
||||
const client = await startClient();
|
||||
const diagnostics: HopperDiagnostic[] = [];
|
||||
const client = new HopperClient({
|
||||
launcher: new FixtureLauncher(),
|
||||
requestTimeoutMs: 100,
|
||||
startupTimeoutMs: 1_000,
|
||||
onDiagnostic: (event) => diagnostics.push(event),
|
||||
});
|
||||
clients.push(client);
|
||||
await expect(client.start()).resolves.toMatchObject({ ok: true });
|
||||
const result = await client.callTool("remote_error");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok)
|
||||
@@ -315,6 +326,66 @@ describe("HopperClient", () => {
|
||||
_tag: "HopperRemoteError",
|
||||
diagnosticType: "bridge_exception",
|
||||
});
|
||||
expect(diagnostics).toContainEqual({
|
||||
type: "bridge-diagnostic",
|
||||
request_id: 2,
|
||||
code: -32001,
|
||||
category: "bridge_exception",
|
||||
message: "safe fake failure",
|
||||
});
|
||||
});
|
||||
|
||||
it("forwards correlated Python progress before the terminal response", async () => {
|
||||
const client = await startClient();
|
||||
const updates: Array<{
|
||||
readonly phase: string;
|
||||
readonly completed: number;
|
||||
readonly terminal?: boolean;
|
||||
}> = [];
|
||||
const result = await client.callTool(
|
||||
"echo",
|
||||
{ value: "progress" },
|
||||
{
|
||||
progress: {
|
||||
report: (update) => {
|
||||
updates.push(update);
|
||||
return Promise.resolve();
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual({ ok: true, value: { value: "progress" } });
|
||||
expect(updates).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
phase: "hopper_bridge",
|
||||
completed: 0,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
phase: "hopper_bridge",
|
||||
completed: 1,
|
||||
terminal: true,
|
||||
}),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("isolates synchronous progress observer failures from bridge responses", async () => {
|
||||
const client = await startClient();
|
||||
await expect(
|
||||
client.callTool(
|
||||
"echo",
|
||||
{ value: "alive" },
|
||||
{
|
||||
progress: {
|
||||
report: () => {
|
||||
throw new Error("observer failed");
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
).resolves.toEqual({ ok: true, value: { value: "alive" } });
|
||||
});
|
||||
|
||||
it("projects a missing bridge API as typed capability unavailability", async () => {
|
||||
@@ -460,6 +531,28 @@ describe("HopperClient", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it("projects actionable CI remediation for Hopper UI, license, and lifecycle failures", async () => {
|
||||
const client = new HopperClient({
|
||||
launcher: new ExitingLauncher(75),
|
||||
startupTimeoutMs: 10_000,
|
||||
});
|
||||
clients.push(client);
|
||||
const result = await client.start();
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
|
||||
const projected = projectAnalysisError(result.error);
|
||||
expect(projected.message).toContain(
|
||||
"rea doctor --provider hopper --detail full --json",
|
||||
);
|
||||
expect(projected.message).toContain("UI or license prompt");
|
||||
expect(projected.message).toContain("close stale Hopper sessions");
|
||||
expect(projected.details).toMatchObject({
|
||||
failure_code: "hopper_exited_during_startup",
|
||||
exit_code: 75,
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves the bounded private-display diagnostic from an adapter exit", async () => {
|
||||
const client = new HopperClient({
|
||||
launcher: new DiagnosticExitingLauncher(),
|
||||
|
||||
Reference in New Issue
Block a user