Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(device): stop claiming a dropped gateway call never ran on the device (#20118)

Arvin Xufce3db87c413
diff --git a/packages/device-gateway-client/src/deviceTransportError.test.ts b/packages/device-gateway-client/src/deviceTransportError.test.ts
index 4d2b654b5e0..0d1bde105b9 100644
--- a/packages/device-gateway-client/src/deviceTransportError.test.ts
+++ b/packages/device-gateway-client/src/deviceTransportError.test.ts
@@ -140,7 +140,13 @@ describe('describeGatewayRequestFailure', () => {
});
it('reports a dead gateway host as an undelivered call', () => {
- const failure = describeGatewayRequestFailure(new TypeError('fetch failed'), 'tool call');
+ // undici reports a refused connect as `fetch failed` with the code on `cause`.
+ const failure = describeGatewayRequestFailure(
+ Object.assign(new TypeError('fetch failed'), {
+ cause: { code: 'ECONNREFUSED', message: 'connect ECONNREFUSED 10.0.0.1:443' },
+ }),
+ 'tool call',
+ );
expect(failure.code).toBe(DeviceTransportErrorCode.GatewayUnreachable);
expect(failure.content).toContain('never ran on the device');
@@ -165,6 +171,53 @@ describe('describeGatewayRequestFailure', () => {
expect(failure.code).toBe(DeviceTransportErrorCode.GatewayUnreachable);
});
+ it('recognises a failed DNS lookup and Bun connect failure as undelivered', () => {
+ expect(
+ describeGatewayRequestFailure(
+ Object.assign(new TypeError('fetch failed'), {
+ cause: { code: 'ENOTFOUND', message: 'getaddrinfo ENOTFOUND gateway.invalid' },
+ }),
+ 'tool call',
+ ).code,
+ ).toBe(DeviceTransportErrorCode.GatewayUnreachable);
+ expect(
+ describeGatewayRequestFailure(
+ new TypeError('Unable to connect. Is the computer able to access the url?'),
+ 'tool call',
+ ).code,
+ ).toBe(DeviceTransportErrorCode.GatewayUnreachable);
+ });
+
+ // The request had already reached the gateway when these connections dropped,
+ // so the gateway may have relayed it and the device may have run it. Saying
+ // "never ran" tells the model to repeat a write that already happened.
+ it.each([
+ ['undici reset (ECONNRESET)', { code: 'ECONNRESET', message: 'read ECONNRESET' }],
+ [
+ 'undici peer close (UND_ERR_SOCKET)',
+ { code: 'UND_ERR_SOCKET', message: 'other side closed' },
+ ],
+ ])('does not claim a %s call never ran', (_label, cause) => {
+ const failure = describeGatewayRequestFailure(
+ Object.assign(new TypeError('fetch failed'), { cause }),
+ 'tool call',
+ );
+
+ expect(failure.code).toBe(DeviceTransportErrorCode.GatewayError);
+ expect(failure.content).not.toContain('never ran');
+ expect(failure.content).toContain('unclear whether the device ran it');
+ });
+
+ it.each([
+ 'socket hang up',
+ 'The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()',
+ ])('does not claim a dropped connection (%s) never ran', (message) => {
+ const failure = describeGatewayRequestFailure(new TypeError(message), 'tool call');
+
+ expect(failure.code).toBe(DeviceTransportErrorCode.GatewayError);
+ expect(failure.content).not.toContain('never ran');
+ });
+
it('does not invent a cause for an unrecognised failure', () => {
// The old copy claimed the gateway was unreachable for every rejection,
// including ones (a malformed response body, say) that prove nothing about
diff --git a/packages/device-gateway-client/src/deviceTransportError.ts b/packages/device-gateway-client/src/deviceTransportError.ts
index 59ca0e73b54..6e233998b3e 100644
--- a/packages/device-gateway-client/src/deviceTransportError.ts
+++ b/packages/device-gateway-client/src/deviceTransportError.ts
@@ -177,20 +177,23 @@ const isTimeoutError = (error: unknown): boolean => {
return name === 'TimeoutError' || name === 'AbortError';
};
-const NETWORK_ERROR_MARKERS = [
- 'fetch failed',
- 'socket hang up',
+// Only failures that happen before the request leaves the server prove the
+// gateway never saw it. A reset or peer close (`ECONNRESET`, undici's
+// `UND_ERR_SOCKET` "other side closed", "socket hang up") can land after the
+// gateway already relayed the call, and the device may have run it β€” those
+// fall through to the "unclear" branch so the model checks state before
+// repeating a write.
+const CONNECT_FAILURE_MARKERS = [
'connection refused',
- 'connection reset',
'econnrefused',
- 'econnreset',
'enotfound',
'eai_again',
- 'etimedout',
- 'network',
+ 'und_err_connect_timeout',
+ // Bun's fetch reports a failed connect with this message and no code.
+ 'unable to connect',
];
-const isNetworkError = (error: unknown, message: string): boolean => {
+const isConnectFailure = (error: unknown, message: string): boolean => {
const code = (error as { cause?: { code?: unknown }; code?: unknown } | null)?.code;
const causeCode = (error as { cause?: { code?: unknown } } | null)?.cause?.code;
const haystack = [message, code, causeCode]
@@ -198,12 +201,13 @@ const isNetworkError = (error: unknown, message: string): boolean => {
.join(' ')
.toLowerCase();
- return NETWORK_ERROR_MARKERS.some((marker) => haystack.includes(marker));
+ return CONNECT_FAILURE_MARKERS.some((marker) => haystack.includes(marker));
};
/**
* Describe a failure that happened before any HTTP status existed β€” the request
- * timed out client-side, or the gateway host could not be reached at all.
+ * timed out client-side, the gateway host could not be reached at all, or the
+ * connection dropped before the response arrived.
*
* A client-side timeout is the same situation as a 504 (the call may have been
* delivered and may still be running), so it carries the same warning.
@@ -222,7 +226,7 @@ export const describeGatewayRequestFailure = (
};
}
- if (isNetworkError(error, message)) {
+ if (isConnectFailure(error, message)) {
return {
code: DeviceTransportErrorCode.GatewayUnreachable,
content: `Could not reach the device gateway to relay this ${operation}, so it never ran on the device. This is a network failure between the server and the gateway. Retry once; if it persists, report it to the user rather than retrying in a loop.`,
diff --git a/packages/device-gateway-client/src/http.test.ts b/packages/device-gateway-client/src/http.test.ts
index 9a332cb6b9d..52b6dc2b3c0 100644
--- a/packages/device-gateway-client/src/http.test.ts
+++ b/packages/device-gateway-client/src/http.test.ts
@@ -383,7 +383,11 @@ describe('GatewayHttpClient', () => {
});
it('describes an unreachable gateway host', async () => {
- vi.mocked(fetch).mockRejectedValue(new TypeError('fetch failed'));
+ vi.mocked(fetch).mockRejectedValue(
+ Object.assign(new TypeError('fetch failed'), {
+ cause: { code: 'ECONNREFUSED', message: 'connect ECONNREFUSED 10.0.0.1:443' },
+ }),
+ );
const result = await client.executeToolCall(
{ userId: 'user-1' },
@@ -395,6 +399,24 @@ describe('GatewayHttpClient', () => {
expect(result.error).toContain('DEVICE_GATEWAY_UNREACHABLE');
});
+ it('does not tell the model a call never ran when the connection dropped after sending', async () => {
+ vi.mocked(fetch).mockRejectedValue(
+ Object.assign(new TypeError('fetch failed'), {
+ cause: { code: 'UND_ERR_SOCKET', message: 'other side closed' },
+ }),
+ );
+
+ const result = await client.executeToolCall(
+ { userId: 'user-1' },
+ { apiName: 'writeFile', arguments: '{}', identifier: 'test' },
+ );
+
+ expect(result.success).toBe(false);
+ expect(result.content).not.toContain('never ran');
+ expect(result.content).toContain('unclear whether the device ran it');
+ expect(result.error).toContain('DEVICE_GATEWAY_ERROR');
+ });
+
it('should pass optional deviceId and timeout', async () => {
mockFetch({
json: vi.fn().mockResolvedValue({ content: 'ok', success: true }),