diff --git a/frontend/src/lib/capacitor-fetch.js b/frontend/src/lib/capacitor-fetch.js index bc07475..cc7b31a 100644 --- a/frontend/src/lib/capacitor-fetch.js +++ b/frontend/src/lib/capacitor-fetch.js @@ -22,7 +22,10 @@ export async function nativeFetch(url, init = {}) { const request = cap.CapacitorHttp.request({ url, method: init.method || 'GET', headers, data, - responseType: 'text', connectTimeout: 30000, readTimeout: 6 * 60000 + // The read timeout is not the job timeout: that is the AbortSignal's job (see abortOf), + // and coach-local.js derives it from the provider. This only has to be longer than any + // job may legitimately run, so a local model on a laptop is never cut off by the transport. + responseType: 'text', connectTimeout: 30000, readTimeout: 40 * 60000 }) const res = init.signal ? await Promise.race([request, abortOf(init.signal)]) : await request const text = typeof res.data === 'string' ? res.data : res.data == null ? '' : JSON.stringify(res.data) diff --git a/frontend/src/lib/coach-local.js b/frontend/src/lib/coach-local.js index c9c6e36..719bda6 100644 --- a/frontend/src/lib/coach-local.js +++ b/frontend/src/lib/coach-local.js @@ -31,7 +31,11 @@ import { t } from './i18n.js' export const ADAPTERS = { anthropic, openai, gemini, compatible } export const LOCAL_DAILY_CAP = 10 +// Five minutes is right for a cloud API and wrong for a model on somebody's laptop; the +// OpenAI-compatible endpoint is the one that may be local, so it gets the long budget. export const TIMEOUT_MS = 5 * 60000 +export const LOCAL_ENDPOINT_TIMEOUT_MS = 25 * 60000 +export const timeoutFor = provider => (provider === 'compatible' ? LOCAL_ENDPOINT_TIMEOUT_MS : TIMEOUT_MS) const PENDING_DAYS = 14 const HANDLE_LENGTH = 16 @@ -141,7 +145,7 @@ async function run(S, kind, opts, d, adapter) { }) const attempt = await runPipeline({ adapter, cfg: cfgOf(d), kind, payload, - model: d.model || HTTP_PROVIDERS[d.provider].defaultModel, timeoutMs: TIMEOUT_MS, + model: d.model || HTTP_PROVIDERS[d.provider].defaultModel, timeoutMs: timeoutFor(d.provider), invokeOpts: { env: envOf(d, key), fetch: nativeFetch } }) if (!attempt.ok) { diff --git a/frontend/src/lib/coach-local.test.js b/frontend/src/lib/coach-local.test.js index f8c4e98..37d3f47 100644 --- a/frontend/src/lib/coach-local.test.js +++ b/frontend/src/lib/coach-local.test.js @@ -179,3 +179,18 @@ describe('the Coach on a phone with its own key', () => { expect(d.categories).toEqual(['plan', 'training', 'bodyweight', 'profile', 'prefs']) }) }) + +describe('timeouts on the phone', () => { + it('gives a local endpoint the long budget and cloud providers the short one', async () => { + const { timeoutFor, TIMEOUT_MS, LOCAL_ENDPOINT_TIMEOUT_MS } = await import('./coach-local.js') + expect(timeoutFor('compatible')).toBe(LOCAL_ENDPOINT_TIMEOUT_MS) + expect(LOCAL_ENDPOINT_TIMEOUT_MS).toBeGreaterThanOrEqual(20 * 60000) + for (const p of ['anthropic', 'openai', 'gemini']) expect(timeoutFor(p)).toBe(TIMEOUT_MS) + }) + it('the native transport read timeout outlasts the longest job', async () => { + const src = (await import('node:fs')).readFileSync(new URL('./capacitor-fetch.js', import.meta.url), 'utf8') + const m = src.match(/readTimeout:\s*(\d+)\s*\*\s*60000/) + expect(m).toBeTruthy() + expect(+m[1] * 60000).toBeGreaterThan(25 * 60000) + }) +})