Skip to content

fix(adapters): bound each date-extraction model call (#42) - #59

Merged
dVelopment merged 3 commits into
mainfrom
feat/42-mailbox-watcher-a-model-call
Nov 7, 2026
Merged

dVelopment merged 3 commits into
mainfrom
feat/42-mailbox-watcher-a-model-call

Conversation

@stibbons-igor-agent

@stibbons-igor-agent stibbons-igor-agent Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Closes #42

A DateExtractor model call that never answers no longer holds the mailbox watcher. Each extract call now has a deadline (default 2 minutes, retries included). A call that runs out of time is aborted, HTTP request included, and fails like any other failed run, so the watcher takes the next mail.

Does the client have a default timeout?

No timeout bounds the whole call. The only one is Bun's socket idle timeout of 5 minutes, and every byte received restarts it.

  • Mastra (@mastra/core@1.80.0, dist/agent-b2Y5GsZg.js): Agent.generate runs the loop's execute (~L34047), which calls model.doGenerate with abortSignal: callAbortSignal (~L34146).
    • That signal comes from createTimeoutAbortSignal({ parentSignal, timeoutMs: timeout?.stepMs }) (~L34126).
    • modelSettings.timeout is undefined unless the caller sets it. In that case createTimeoutAbortSignal (L796) returns the caller's signal as it is, with no timer.
    • The call is retried through p-retry with retries: modelSettings?.maxRetries ?? 2, and hex's StreamErrorRetryProcessor (maxRetries: 2) adds retries of its own.
  • Model router → AI SDK (dist/netlify-CGQcc7B9.js):
    • hex's default provider is built as createAnthropic({ apiKey, baseURL, headers }) (L42185), with no fetch and no timeout.
    • The bundled @ai-sdk/anthropic@3.0.133 doGenerate passes only options.abortSignal to postJsonToApi (L14710–14720).
    • The bundled @ai-sdk/provider-utils@4.0.61 postToApi (L2347) calls globalThis.fetch(url, { …, signal: abortSignal }) and adds no timeout.
  • Bun fetch: see BunFetchRequestInit.timeout (also in bun-types@1.4.4, globals.d.ts L2163). It is a socket idle timeout: "The timer is reset on every byte sent or received". Its default is 5 minutes (BUN_CONFIG_HTTP_IDLE_TIMEOUT, since Set fetch timeout to 5 minutes oven-sh/bun#6217), and the docs say: "This is not a whole-request deadline; use AbortSignal.timeout(ms) for that."
  • So: a provider that keeps the connection open and sends nothing fails only after 5 minutes of silence, and that is per attempt. One that trickles bytes is never stopped. Meanwhile the watcher takes no other mail.

What changed

  • packages/adapters/src/date-extractor.ts: new DateExtractorOptions.timeoutMs, default 120_000.
    • It is enforced with Effect.timeoutOrElse on the Effect.tryPromise around agent.generate, so it covers Mastra's retries and StreamErrorRetryProcessor's.
    • Interrupting tryPromise aborts the signal passed as abortSignal. Mastra checks that signal before retrying, and the provider's fetch receives it.
    • On timeout the call fails with DateExtractionError: reading the Document date of "<file>" with <model> timed out after <N> ms. The error type is unchanged and still carries no cause.
    • Production callers keep the default. There is no new config.
  • The mailbox watcher is unchanged. The timed-out run takes the existing failed-run path: counted in the ledger, retried with backoff, and failed permanently at MAX_FAILED_RUNS.
  • packages/server/test/mock-model.ts: the before hook now wraps doGenerate as well as doStream. agent.generate calls doGenerate, so before this change the hook never ran for the extractor.
  • The README's "Document dates" step now lists "no answer within 2 minutes" as a failing model call.

How it was proven

  • packages/adapters/test/date-extractor.test.ts: a mock model that never answers, timeoutMs: 50. The call fails with the timeout message in under 1 s, after one call, and the model's abort signal is aborted.
  • Same file, the deadline spans retry backoff: the first reply is a retryable 503, the next would succeed, with timeoutMs: 100 and retryDelayMs: 1000. The call fails with the timeout message in under 500 ms (about 100 ms in practice), after one model call. With a per-attempt deadline instead, the test fails: the retry waits out the backoff and succeeds after about 1037 ms.
  • packages/server/test/mailbox-watcher.test.ts: end-to-end with the live dateExtractorLayer over a mock model whose first call hangs.
    • The run fails and is counted (failed run 1 of 5), and its mail stays in the watched folder.
    • The second mail is processed in the same look.
  • Both tests fail on the base commit: they hit their 2 s bounds.
  • Smoke over real HTTP, with a throwaway script that wasn't committed: the live dateExtractorLayer("openai/gpt-5-mini", { timeoutMs: 1500 }), with OPENAI_BASE_URL pointing at a loopback server that accepts the request and never answers.
    • After 1506 ms the call failed with DateExtractionError: reading the Document date of "smoke.pdf" with openai/gpt-5-mini timed out after 1500 ms.
    • The server got 1 request and saw the client abort it. So the abort travels the real path: model router → AI SDK → Bun fetch.
  • bun run check passes on 43cb98615 (format, lint, typecheck, manifests, licenses, tests): 1019 pass, 0 fail.
  • Review: two crew rounds. Round 1 found 4 lows: 3 were fixed (a wider test deadline, source references in the comment, comment wording) and 1 was pushed back. Round 2 found nothing above low. In round 1 the reviewer checked the default-timeout references above by hand against the installed packages.
  • Adversarial review round 1 found 1 medium: no test proved that the deadline covers the wait between retries. Fixed with the test above (test-only, 43cb98615). A recheck found nothing above low.

Action items

  1. Watch the first eval run after merge. The eval harness gets the same 120 s deadline, so a slow reasoning model on a large PDF could now fail there instead of waiting. If that happens, pass a larger timeoutMs in packages/eval-harness (recommended over raising the production default).
  2. Not run on the pinned Bun 1.4.2. bun run check and the smoke ran on Bun 1.4.4, the local system Bun. CI covers 1.4.2.
  3. Follow-up (low, optional): keep the cause of a retried attempt. When a transient error (429/503) is retried and the retry then hangs, the timeout message drops the first attempt's HTTP status. An operator can't tell a rate-limit storm from a silent provider. Recommendation: wait and see whether it comes up in logs.
  4. Follow-up (low, optional): test a stack that ignores abort. Add an adapter test whose model never settles and ignores the signal, and assert that the deadline still holds. It passes today because Effect doesn't wait for the interrupted promise. Recommendation: add it the next time this file is touched.
  5. Open question: it was not observed whether Bun's idle-timeout error counts as retryable. If it does, a silent provider without this deadline could have held a run for up to 9 attempts of 5 minutes each. It does not matter with the deadline in place.

A provider that never answers held the mailbox watcher forever: nothing in Mastra, the AI SDK or Bun's fetch sets a whole-call deadline. dateExtractorLayer gains timeoutMs (default 120 s), enforced with Effect.timeoutOrElse, which interrupts the call and aborts its request; the call fails with a DateExtractionError naming the timeout, and the run fails as any other.
@dVelopment
dVelopment marked this pull request as ready for review November 7, 2026 12:15
@dVelopment
dVelopment self-requested a review November 7, 2026 12:15
@dVelopment
dVelopment merged commit ee0d3a7 into main Nov 7, 2026
5 of 6 checks passed
@dVelopment
dVelopment deleted the feat/42-mailbox-watcher-a-model-call branch November 7, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mailbox watcher: a model call that never returns stops all mail intake

1 participant