Fix down-mark lockouts and keep main's queued AI jobs running
Fixes found in review before the Milestone 1 landing, on top of #5608. Each fix is its own commit with its tests. More land here until the landing; see the review order.
Down marks no longer lock out a click
Problem. A failure marks its route down for 60 seconds, in Rails.cache, for every family on the instance. Bill setup has one route and a person waiting, so one timeout, 429 or bad key made every family's smart-fill and smart-configure clicks fail with "is marked down" for a minute, without a request. On main, each click tried the provider. Found and tested in Luis's slice review (luis-ibanez/sure#1, 106946038); his test is carried over.
Change.
Ai::Generation#call(waiting: true): when every route is marked down, a call someone waits for tries them anyway, whatever the mark was for. Marks are still written, and a marked route is still skipped while another is up. Bill setup's suggester passes it.- Jobs don't, so they still fail at once on a marked route, and a rate-limited shared key gets a back-off instead of every family's jobs hitting it together.
Ai::Error::MarkedDown(anUnreachable) when every route was skipped for a passing mark and nothing was sent.Ai::GenerationJobwaits it out (the mark's 60 s plus up to 10 s) without spending one of its five tries, up to 10 times, then gives up through the requester, so a rule run ends as failed rather than staying pending. Before, the retries at about 3 s and 18 s landed inside the mark, so only 3 of the 5 tries reached the provider.- In the sequencer stack, 4a (#5756) will take
waitingfrom the lane (interactive, or any call outside a job) and drop the option.
Jobs queued on main keep running after the upgrade
Problem. The plumbing deletes AutoCategorizeJob, AutoDetectMerchantsJob, EnhanceProviderMerchantsJob and WorkerAiHealthCheckJob. One still queued, scheduled or waiting for a retry at upgrade time fails with a NameError. For the two rule jobs, the rule run stays pending for good, because main's job was what completed it. Raised in Luis's review.
Change. Each class stays for one release and hands its arguments to the new path:
- the rule jobs start the
Ai::GenerationRunfor their rule run, without counting the job again (main's runner already did); - merchant enhancement starts a run over every merchant not yet enhanced;
- the health check queues
Ai::ProbeJob.
If the new job can't be queued, the rule jobs try five times, then fail the job on their rule run once (Codex's review), and the health check raises, so it's retried.
An oversized PDF says so
Problem. Under Auto, a scan over the provider's PDF limits ended as no_text_layer or unreadable, though reading the same file as PDF says too_large. A page too large to render was unreadable both ways. Found in Luis's testing.
Change. The file's size is too_large under Auto too. A page's is the new page_too_large, with its own text, since too_large advises reading the file as images. A file that can't be opened stays unreadable.
A model that takes no images is "not supported"
Problem. On a custom server whose model takes no images, System health showed the PDF vision check as failing and the worker as unhealthy, though its text PDFs import. OPENAI_SUPPORTS_PDF_PROCESSING=false isn't the fix: it turns off all PDF import, as on main. Found in Luis's testing.
Change.
- The check is "not supported" when the server's refusal is one Sure recognizes (llama.cpp's and Ollama's "image input is not supported"), and the worker stays healthy. A server that words it differently still shows as failing.
- "Read as: Images" on such a server says the model can't read page images.
docs/hosting/ai.md: such a server needs no setting, and the flag also stops text PDFs.
A server that can't render PDF pages says so
Problem. Without pdftoppm, or when it failed, an import that needed page images said "This file couldn't be opened as a PDF", though the file was fine. main named the missing program. System health had no text for missing_binary (it was stored under main's render_missing_binary), failed or blank_page, and showed a renderer's own timeout as the AI service not responding.
Change. Imports and System health both say missing_binary (install poppler-utils) or render_failed (anything else pdftoppm did). The probe's debug log entry carries pdftoppm's code and message, from Sure's own test PDF. A scan over the provider's limits still says too_large, and a PDF with text is still read as text.
Tests
Each test marked fails before was run against the code before its fix and failed as described.
- Bill setup: after a click fails with
Timeout,RateLimitedorUnauthorized, the next click reaches the provider (fails before: "openai/gpt-4.1-mini is marked down"); another family's click does too (fails before). - Generation: every route marked down for a passing failure raises
MarkedDownwith no request; a waiting call tries every route when all are marked, for any reason; a waiting call still skips a marked route while another is up; a failed waiting call still writes its mark, so a job after it sends nothing. Ai::GenerationJob: with real time between retries, all five tries reach the provider (fails before: 3); a job that only ever finds its routes marked down gives up through the requester with no request.- Jobs queued before the upgrade: each rule job starts its run with the rule run's id and the same transactions, without counting it again, and the rule run ends once and isn't left pending; merchant enhancement starts a run; the health check queues the probes. When the new job can't be queued (refused, or the queue raises), the rule run's job fails once after five tries and its other job is still counted (fails before); the health check raises (fails before).
- PDF reading: a scan over the limits says
too_largeunder Auto (fails before); a page too large to render sayspage_too_large, under Auto and by name (fails before); a file that can't be opened staysunreadable; Images on a server that refuses images saysneeds_vision(fails before). - System health: a refused image is
images_refusedin the probe, "not supported" on the page, and no worker failure (each fails before); the missing-renderer reason has its text (fails before); each other renderer failure isrender_failed, with the detail in the debug log (fails before). - Rendering in an import: each renderer code gives its reason under Auto and Images (fails before); without
pdftoppm, a PDF with text is still read as text.
Validation: bin/rails test, full suite, 19,212 runs, 0 failures (TZ=Etc/UTC). Rubocop clean, Brakeman no warnings.
š¤ Generated with Claude Code
https://claude.ai/code/session_016zAWUUTovNPgC5oNfUHKhd
Summary by cubic
Fixes review findings before the AI plumbing lands: down marks no longer lock bill setup out of the provider, jobs main queued keep running after the upgrade, and PDF, health-check, and renderer failures now say what actually went wrong.
Bug fixes
- A waiting call (a click) tries every route when all are marked down, so one
Timeout, 429, or bad key no longer blocks every family's bill setup for 60 seconds. Jobs still fail at once on a marked route. - New
Ai::Error::MarkedDownsignals that no request was sent because every route was skipped.Ai::GenerationJobwaits the mark out (60ā70 s) without spending one of its five tries, up to 10 times, then gives up through the requester. Before, retries landed inside the mark, so only 3 of 5 tries reached the provider. AutoCategorizeJob,AutoDetectMerchantsJob,EnhanceProviderMerchantsJob, andWorkerAiHealthCheckJobare kept for one release so jobs still queued at upgrade time run. The rule-job shims retry on the error itself and, after the last try, fail the rule run's job once with the real error, not a generic queue failure, so the run isn't left pending forever; without AI set up they complete it asmaindid, success with nothing modified. The merchant-enhancement shim clearsmain's queuing lock so a queued batch actually starts its run. A job whose family is gone is dropped. The hosting guide now says to upgrade through this release rather than draining the queues.- A PDF that is too large, or has a page too large to render, now reports that instead of "no text layer" or "unreadable"; a scan a text-only model refuses keeps
no_text_layer, andRead as Imagessays the model can't read page images. - When pdftoppm is missing or fails, imports now say
missing_binaryorrender_failedinstead of "unreadable", and system health shows the same two; the probe's debug log entry records what pdftoppm did. - System health shows a model that takes no images as not supported rather than failing, so the worker stays healthy, and a missing pdftoppm renderer shows its
missing_binarytext.
Summary by CodeRabbit
- Bug Fixes
- Smart bill setup can now try the AI provider even when all routes are marked unavailable, helping requests reach the provider when it may have recovered.
- AI generation retries after routes are marked unavailable, allowing time for service recovery before reporting a failure.
- A failed bill-setup request is less likely to prevent a later attempt from reaching the provider.