Skip to content

The agent that grows with you

fix(desktop): harden Hermes API transport

KHALIDagara1cc76fce3ac7
diff --git a/apps/desktop/electron/hardening.ts b/apps/desktop/electron/hardening.ts
index f8c77f31868..4ea4bc125e2 100644
--- a/apps/desktop/electron/hardening.ts
+++ b/apps/desktop/electron/hardening.ts
@@ -13,7 +13,7 @@ import {
DATA_URL_READ_MIN_MAX_MB
} from '../../shared/src/data-url-read-max'
-const DEFAULT_FETCH_TIMEOUT_MS = 15_000
+const DEFAULT_FETCH_TIMEOUT_MS = 30_000
// Remote file.attach sends one base64 JSON-RPC frame. Cap the dedicated attach
// reader so the payload still fits uvicorn's raised ws_max_size (384 MiB)
// after base64 + framing. Preview stays on the Settings-configurable path.
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 92db4cc289a..e588dde0d48 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -4856,8 +4856,40 @@ function multipartBody(upload) {
return { body, contentType: `multipart/form-data; boundary=${boundary}` }
}
+// Connection-pooled HTTP agents to avoid ECONNRESET from per-call TCP sockets.
+// Each fetchJson / fetchPublicJson / downloadViaTokenToFile call shares these
+// instead of opening a fresh connection every time.
+const HTTP_KEEPALIVE_AGENT = new http.Agent({ keepAlive: true, maxSockets: 50 })
+const HTTPS_KEEPALIVE_AGENT = new https.Agent({ keepAlive: true, maxSockets: 50 })
+
+const RETRYABLE_CODES = new Set(['ECONNRESET', 'ECONNREFUSED', 'EPIPE', 'ERR_NETWORK'])
+
+function isRetryableError(error) {
+ if (!error) return false
+ if (RETRYABLE_CODES.has(error.code)) return true
+ const msg = String(error.message || '')
+ return msg.includes('socket hang up') || msg.includes('read ECONNRESET')
+}
+
+async function withRetry(fn, maxRetries = 2) {
+ let lastError
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
+ try {
+ return await fn()
+ } catch (error) {
+ lastError = error
+ if (attempt < maxRetries && isRetryableError(error)) {
+ await new Promise(r => setTimeout(r, Math.min(200 * Math.pow(2, attempt), 2000)))
+ continue
+ }
+ throw error
+ }
+ }
+ throw lastError
+}
+
function fetchJson(url, token, options: any = {}) {
- return new Promise((resolve, reject) => {
+ return withRetry(() => new Promise((resolve, reject) => {
const { body, contentType } = options.upload
? multipartBody(options.upload)
: {
@@ -4867,6 +4899,7 @@ function fetchJson(url, token, options: any = {}) {
const parsed = new URL(url)
const client = parsed.protocol === 'https:' ? https : http
+ const agent = parsed.protocol === 'https:' ? HTTPS_KEEPALIVE_AGENT : HTTP_KEEPALIVE_AGENT
const timeoutMs = resolveTimeoutMs(options.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS)
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
@@ -4878,6 +4911,7 @@ function fetchJson(url, token, options: any = {}) {
const req = client.request(
parsed,
{
+ agent,
method: options.method || 'GET',
headers: {
...headersForRemoteRequest(url),
@@ -4949,7 +4983,7 @@ function fetchJson(url, token, options: any = {}) {
}
req.end()
- })
+ }))
}
// Token-auth download that streams the response body straight to a
@@ -4976,11 +5010,13 @@ function downloadViaTokenToFile(url, token, ctx, options: any = {}) {
}
const client = parsed.protocol === 'https:' ? https : http
+ const agent = parsed.protocol === 'https:' ? HTTPS_KEEPALIVE_AGENT : HTTP_KEEPALIVE_AGENT
const timeoutMs = resolveTimeoutMs(options.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS)
const req = client.request(
parsed,
{
+ agent,
method: 'GET',
headers: options.bearer ? { Authorization: `Bearer ${options.bearer}` } : { 'X-Hermes-Session-Token': token }
},
@@ -5015,7 +5051,7 @@ function fetchPublicJson(url, options: any = {}) {
// NO ``X-Hermes-Session-Token`` header — used by the auth-mode probe before
// any credentials exist, and any time we must not leak a token to an
// endpoint that doesn't need one.
- return new Promise((resolve, reject) => {
+ return withRetry(() => new Promise((resolve, reject) => {
const body = options.body === undefined ? undefined : Buffer.from(JSON.stringify(options.body))
let parsed
@@ -5028,6 +5064,7 @@ function fetchPublicJson(url, options: any = {}) {
}
const client = parsed.protocol === 'https:' ? https : http
+ const agent = parsed.protocol === 'https:' ? HTTPS_KEEPALIVE_AGENT : HTTP_KEEPALIVE_AGENT
const timeoutMs = resolveTimeoutMs(options.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS)
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
@@ -5039,6 +5076,7 @@ function fetchPublicJson(url, options: any = {}) {
const req = client.request(
parsed,
{
+ agent,
method: options.method || 'GET',
headers: {
...headersForRemoteRequest(url),
@@ -5098,7 +5136,7 @@ function fetchPublicJson(url, options: any = {}) {
}
req.end()
- })
+ }))
}
function mimeTypeForPath(filePath) {