Optimize gateway comms reload behavior and strengthen regression coverage (#496)

This commit is contained in:
Lingxuan Zuo
2026-03-15 20:36:48 +08:00
committed by GitHub
Unverified
parent 08960d700f
commit 1dbe4a8466
36 changed files with 1511 additions and 197 deletions

View File

@@ -551,6 +551,7 @@ export function createGatewayHttpTransportInvoker(
if (typeof method !== 'string') {
throw new Error('gateway:rpc requires method string');
}
validateGatewayRpcParams(method, params);
const timeoutMs =
typeof timeoutOverride === 'number' && timeoutOverride > 0
@@ -857,6 +858,7 @@ export function createGatewayWsTransportInvoker(options: GatewayWsTransportOptio
if (typeof method !== 'string') {
throw new Error('gateway:rpc requires method string');
}
validateGatewayRpcParams(method, params);
const requestTimeoutMs =
typeof timeoutOverride === 'number' && timeoutOverride > 0
@@ -887,6 +889,17 @@ export function createGatewayWsTransportInvoker(options: GatewayWsTransportOptio
};
}
function validateGatewayRpcParams(method: string, params: unknown): void {
if (method !== 'config.patch') return;
if (!params || typeof params !== 'object' || Array.isArray(params)) {
throw new Error('gateway:rpc config.patch requires object params');
}
const patch = (params as Record<string, unknown>).patch;
if (!patch || typeof patch !== 'object' || Array.isArray(patch)) {
throw new Error('gateway:rpc config.patch requires object patch');
}
}
let defaultTransportsInitialized = false;
export function initializeDefaultTransports(): void {

View File

@@ -129,6 +129,14 @@ function shouldFallbackToBrowser(message: string): boolean {
|| normalized.includes('window is not defined');
}
function allowLocalhostFallback(): boolean {
try {
return window.localStorage.getItem('clawx:allow-localhost-fallback') === '1';
} catch {
return false;
}
}
export async function hostApiFetch<T>(path: string, init?: RequestInit): Promise<T> {
const startedAt = Date.now();
const method = init?.method || 'GET';
@@ -160,6 +168,17 @@ export async function hostApiFetch<T>(path: string, init?: RequestInit): Promise
if (!shouldFallbackToBrowser(message)) {
throw normalized;
}
if (!allowLocalhostFallback()) {
trackUiEvent('hostapi.fetch_error', {
path,
method,
source: 'ipc-proxy',
durationMs: Date.now() - startedAt,
message: 'localhost fallback blocked by policy',
code: 'CHANNEL_UNAVAILABLE',
});
throw normalized;
}
}
// Browser-only fallback (non-Electron environments).