From bef7d2c75a22cffc09e520a9364c18f024e6147a Mon Sep 17 00:00:00 2001 From: Sarah Fortune Date: Wed, 17 Sep 2025 07:13:02 +0000 Subject: [PATCH] Clean up cline-core (#6255) Move all the logic for waiting for the host bridge into `hostbridge-client.ts` Add some comments Use try/finally, remove unused param, log the error message when the health check fails. --- src/standalone/cline-core.ts | 11 +++----- src/standalone/hostbridge-client.ts | 39 ++++++++++++++--------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/standalone/cline-core.ts b/src/standalone/cline-core.ts index 9bdb91da..b921e672 100644 --- a/src/standalone/cline-core.ts +++ b/src/standalone/cline-core.ts @@ -16,14 +16,9 @@ import { EXTENSION_DIR, extensionContext } from "./vscode-context" async function main() { log("\n\n\nStarting cline-core service...\n\n\n") - try { - await waitForHostBridgeReady() - log("HostBridge is serving; continuing startup") - } catch (err) { - log(`ERROR: HostBridge error: ${String(err)}`) - process.exit(1) - } + await waitForHostBridgeReady() + // The host bridge should be available before creating the host provider because it depends on the host bridge. setupHostProvider() // Set up global error handlers to prevent process crashes @@ -31,6 +26,7 @@ async function main() { const webviewProvider = await initialize(extensionContext) + // Enable the localhost HTTP server that handles auth redirects. AuthHandler.getInstance().setEnabled(true) startProtobusService(webviewProvider.controller) @@ -46,6 +42,7 @@ function setupHostProvider() { const getCallbackUrl = (): Promise => { return AuthHandler.getInstance().getCallbackUrl() } + // cline-core expects the binaries to be unpacked in the directory where it is running. const getBinaryLocation = async (name: string): Promise => path.join(process.cwd(), name) HostProvider.initialize( diff --git a/src/standalone/hostbridge-client.ts b/src/standalone/hostbridge-client.ts index a6ac84e1..d0fa2ca2 100644 --- a/src/standalone/hostbridge-client.ts +++ b/src/standalone/hostbridge-client.ts @@ -5,42 +5,41 @@ import { log } from "./utils" export const HOSTBRIDGE_PORT = 26041 -export async function waitForHostBridgeReady(timeoutMs = 60000, intervalMs = 500, address?: string): Promise { +export async function waitForHostBridgeReady(timeoutMs = 60000, intervalMs = 500): Promise { + const address = process.env.HOST_BRIDGE_ADDRESS || `localhost:${HOSTBRIDGE_PORT}` const client = createHealthClient(address) const deadline = Date.now() + timeoutMs - while (Date.now() < deadline) { - // eslint-disable-next-line no-await-in-loop - const ok = await checkHealthOnce(client) - if (ok) { - try { - client.close?.() - } catch {} - return - } - log("Waiting for hostbridge to be ready...") - // eslint-disable-next-line no-await-in-loop - await new Promise((r) => setTimeout(r, intervalMs)) - } try { - client.close?.() - } catch {} - throw new Error("HostBridge health check timed out") + while (Date.now() < deadline) { + const ok = await checkHealthOnce(client) + if (ok) { + log(`HostBridge serving at ${address}; continuing startup`) + return + } + log("Waiting for hostbridge to be ready...") + await new Promise((r) => setTimeout(r, intervalMs)) + } + } finally { + client.close() + } + log("HostBridge health check timed out") + process.exit(1) } // Client-side health check for the hostbridge service (kept at bottom for clarity) const SERVING_STATUS = 1 -function createHealthClient(address?: string) { +function createHealthClient(address: string) { const healthDef = protoLoader.loadSync(health.protoPath) const grpcObj = grpc.loadPackageDefinition(healthDef) as unknown as any const Health = grpcObj.grpc.health.v1.Health - const target = address || process.env.HOST_BRIDGE_ADDRESS || `localhost:${HOSTBRIDGE_PORT}` - return new Health(target, grpc.credentials.createInsecure()) + return new Health(address, grpc.credentials.createInsecure()) } async function checkHealthOnce(client: any): Promise { return new Promise((resolve) => { client.check({ service: "" }, (err: unknown, resp: any) => { if (err) { + console.debug(err.toString()) return resolve(false) } return resolve(resp?.status === SERVING_STATUS)