Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): report a control-plane hop no process answered as control_plane_unavailable
Alfredo Lopez6f3a53026679
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 5a8e071e0..d36460e4b 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.443",+ "version": "0.1.444","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.443",+ "version": "0.1.444","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 15efdd75c..f850f608b 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.443",+ "version": "0.1.444","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/llm/codexLlmProxyClient.ts b/mcp-host/src/llm/codexLlmProxyClient.tsindex 46fdb3b4e..2059a1919 100644--- a/mcp-host/src/llm/codexLlmProxyClient.ts+++ b/mcp-host/src/llm/codexLlmProxyClient.ts@@ -2,6 +2,7 @@ import {buildCodexProxyEnvelope,parseCodexCompletionRequest,} from '@clerum/llm-provider-attempt-contract'+import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'import { retryAfterMs } from './retryAfter'export const CODEX_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/codex/completions'@@ -117,15 +118,29 @@ export class CodexLlmProxyClient {}const jwt = this.options.readPlatformJwt()const fetchFn = this.options.fetchFn ?? fetch- const response = await fetchFn(this.options.runtimeUrl, {- method: 'POST',- headers: {- authorization: `Bearer ${jwt}`,- 'content-type': 'application/json',- },- body: JSON.stringify(body),- signal: input.signal,- })+ let response: Response+ try {+ response = await fetchFn(this.options.runtimeUrl, {+ method: 'POST',+ headers: {+ authorization: `Bearer ${jwt}`,+ 'content-type': 'application/json',+ },+ body: JSON.stringify(body),+ signal: input.signal,+ })+ } catch (err) {+ // No live proxy process received the request (G1-7, #720). Every other+ // rejection, the caller's abort included, is rethrown unchanged; a+ // failure while the stream is read is never relabelled here.+ if (isConnectPhaseFailure(err, input.signal)) {+ throw new CodexProxyError(+ 'control_plane_unavailable',+ `proxy could not be reached (${fetchCauseCode(err)})`+ )+ }+ throw err+ }if (!response.ok) {if (response.status === 401 && retryOnUnauthorized && this.options.refreshOnUnauthorized) {await this.options.refreshOnUnauthorized()diff --git a/mcp-host/src/llm/controlPlaneReachability.ts b/mcp-host/src/llm/controlPlaneReachability.tsnew file mode 100644index 000000000..81ae6ddb0--- /dev/null+++ b/mcp-host/src/llm/controlPlaneReachability.ts@@ -0,0 +1,33 @@+// G1-7 (#720): the fetch failures that prove no live control-plane process+// received the request. Each one fails before a connection exists (refused,+// DNS, unreachable network, connect timeout), which is what a Service with no+// ready endpoint produces during a cluster restart. A reset, a read timeout or+// a failure after the response started may have reached a live process, so+// they are not in this set. The proxies keep the same set in their own+// `controlApiClient.ts` (#799 tracks the duplication).+const CONNECT_PHASE_CODES: ReadonlySet<string> = new Set([+ 'ECONNREFUSED',+ 'ENOTFOUND',+ 'EAI_AGAIN',+ 'EHOSTUNREACH',+ 'ENETUNREACH',+ 'UND_ERR_CONNECT_TIMEOUT',+])++// The code undici puts on a failed fetch's cause. Only a code-shaped string is+// returned, so nothing else from the error can reach a message or a log.+export function fetchCauseCode(err: unknown): string | undefined {+ if (!(err instanceof Error)) return undefined+ const cause: unknown = (err as { cause?: unknown }).cause+ if (typeof cause !== 'object' || cause === null) return undefined+ const code: unknown = (cause as { code?: unknown }).code+ return typeof code === 'string' && /^[A-Z][A-Z0-9_]{0,63}$/.test(code) ? code : undefined+}++// True only for a fetch rejection the caller did not cause: once the caller's+// signal aborted, the rejection belongs to the abort whatever its code says.+export function isConnectPhaseFailure(err: unknown, signal: AbortSignal | undefined): boolean {+ if (signal?.aborted || !(err instanceof TypeError)) return false+ const code = fetchCauseCode(err)+ return code !== undefined && CONNECT_PHASE_CODES.has(code)+}diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.tsindex 9075e689f..62fa8dc27 100644--- a/mcp-host/src/llm/grokLlmProxyClient.ts+++ b/mcp-host/src/llm/grokLlmProxyClient.ts@@ -1,3 +1,4 @@+import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'import { retryAfterMs } from './retryAfter'export const GROK_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/grok/completions'@@ -99,20 +100,34 @@ export class GrokLlmProxyClient {): Promise<GrokProxyStreamResult> {const jwt = this.options.readPlatformJwt()const fetchFn = this.options.fetchFn ?? fetch- const response = await fetchFn(this.options.runtimeUrl, {- method: 'POST',- headers: {- authorization: `Bearer ${jwt}`,- 'content-type': 'application/json',- },- body: JSON.stringify({- executionTicket: input.executionTicket,- requestHash: input.requestHash,- request: input.request,- ...(input.deadlineMs !== undefined ? { deadlineMs: input.deadlineMs } : {}),- }),- signal: input.signal,- })+ let response: Response+ try {+ response = await fetchFn(this.options.runtimeUrl, {+ method: 'POST',+ headers: {+ authorization: `Bearer ${jwt}`,+ 'content-type': 'application/json',+ },+ body: JSON.stringify({+ executionTicket: input.executionTicket,+ requestHash: input.requestHash,+ request: input.request,+ ...(input.deadlineMs !== undefined ? { deadlineMs: input.deadlineMs } : {}),+ }),+ signal: input.signal,+ })+ } catch (err) {+ // No live proxy process received the request (G1-7, #720). Every other+ // rejection, the caller's abort included, is rethrown unchanged; a+ // failure while the stream is read is never relabelled here.+ if (isConnectPhaseFailure(err, input.signal)) {+ throw new GrokProxyError(+ 'control_plane_unavailable',+ `proxy could not be reached (${fetchCauseCode(err)})`+ )+ }+ throw err+ }if (!response.ok) {if (response.status === 401 && retryOnUnauthorized && this.options.refreshOnUnauthorized) {await this.options.refreshOnUnauthorized()diff --git a/mcp-host/src/llm/providerAttemptAuthorizer.ts b/mcp-host/src/llm/providerAttemptAuthorizer.tsindex ef54f4dbc..f5e9630f8 100644--- a/mcp-host/src/llm/providerAttemptAuthorizer.ts+++ b/mcp-host/src/llm/providerAttemptAuthorizer.ts@@ -4,6 +4,7 @@ import {parseAuthorizeAttemptResponse,requestBodyLimitBytes,} from '@clerum/llm-provider-attempt-contract'+import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'export const AUTHORIZE_PATH = '/api/v1/mcp-host/llm/provider-attempts/authorize'@@ -108,15 +109,28 @@ export class ProviderAttemptAuthorizer {throw new CodexAuthorizeError('no_grant', 'platform JWT is missing')}const fetchFn = this.options.fetchFn ?? fetch- const response = await fetchFn(this.options.authorizeUrl, {- method: 'POST',- headers: {- authorization: `Bearer ${jwt}`,- 'content-type': 'application/json',- },- body: serialized,- ...(signal ? { signal } : {}),- })+ let response: Response+ try {+ response = await fetchFn(this.options.authorizeUrl, {+ method: 'POST',+ headers: {+ authorization: `Bearer ${jwt}`,+ 'content-type': 'application/json',+ },+ body: serialized,+ ...(signal ? { signal } : {}),+ })+ } catch (err) {+ // No live gateway process received the request (G1-7, #720). Every+ // other rejection, the caller's abort included, is rethrown unchanged.+ if (isConnectPhaseFailure(err, signal)) {+ throw new CodexAuthorizeError(+ 'control_plane_unavailable',+ `authorize could not reach the control plane (${fetchCauseCode(err)})`+ )+ }+ throw err+ }const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>if (!response.ok) {if (response.status === 401 && retryOnUnauthorized && this.options.refreshOnUnauthorized) {