Bill suggestions with Stop

Merged
JO
2d ago
+1678-705

Replaces #5827, which came from a fork branch and so couldn't join the stack. Its earlier bot review threads are there.

What and why

Smart-fill and smart-configure used to run the model on the web thread: a click held a Puma request while the suggestion was generated, with no way to stop it. This PR turns each request into a persisted RecurringTransaction::Suggestion, runs it through the shared bill_setup generation job on its feature lane, and shows a waiting dialog with a Stop button. The answer is stored on the row and broadcast as a frame whose src reloads #show, so a reload is safe and Stop disappears with the result.

Based on: #5784 (feat/ai-4c-typing-hold), the last part of the sequencer stack. This PR is a plain PR — it is not stacked on #5748 or on any other feature PR, and it will retarget to refactor/ai-plumbing when 4c lands.

Plan: delivery plan §1–§2/S3–S5, with the design in PR 3, the redesign and the Workbench comparison.

What changes

  • Migration: recurring_transaction_suggestions stores the family, user, subject (entry for fill or series for configure), mode, status, answer and a safe failure code. A check constraint requires exactly one subject; the daily DataCleanerJob removes rows older than seven days.
  • RecurringTransaction::Suggestion is the bill_setup requester: it rechecks consent in the job, builds the same evidence and normalization the synchronous suggester used, applies the answer under a row lock, and ignores late writes after Stop. Provider error text is logged for support, never shown; the dialog shows a router reason or a localized generic message.
  • smart_fills and smart_configurations gain create/show/destroy resources. Create reuses a pending row on a double click, enqueues Ai::GenerationRun, and renders a waiting frame. Destroy marks the row cancelled under lock; a finished row cannot be stopped. Show renders stored state, including the value-carrying checkbox form for configure.
  • A small Stimulus controller reloads the result frame once when the cable source connects, covering a broadcast that beat the subscription. Stop lives outside the apply form. English and German strings only.
  • Minitest covers normalization and one-off dates, evidence, consent recheck, cancellation races, safe failures, broadcasts, double clicks, enqueue failure, permissions, and the e2e suggestion path.

Validation

Review fixes: Revoked-access answers suppressed; result messages, covered warning and Back link fixed. A bill suggestion's run now ends after its one step: the requester reads its window, so the job no longer queues another step that asks the model again (#5521 review). Full suite after the fix: 20,046 runs, 0 failures.

Checks (local, on this branch, after the editor pass): full Rails suite 19,328 runs / 212,930 assertions / 0 failures / 0 errors / 32 skips; focused Minitest for the model, controllers, helper, architecture, cleaner and e2e path 237 runs / 1,542 assertions / 0 failures / 0 errors / 1 pre-existing skip; system tests 514 runs / 2,832 assertions / 0 failures / 0 errors. RuboCop, erb_lint, Biome lint and Brakeman are clean. npm run format:check reports the same 77 pre-existing errors as the base commit, none in this diff.

Editor pass: restricted requires_ai :bill_setup to create so #show and Stop stay reachable after availability changes; fixed both show views to render the waiting frame for a pending row; consolidated the model tests (16 -> 11) and merged the controller Stop/guard tests.

sure-dev verification: smart fill completed and stored a bill proposal; a second fill was stopped before its job answered and stayed cancelled with no result; smart configure completed with an "already right" result after matching charges were added. With AI disabled after creation, show still rendered the waiting frame and the job failed with the router reason user_disabled, as designed.

Live provider calls: none. Automated validation used the stubbed transport only; RubyLLM version on this base: 2.2.3 (from Gemfile.lock). The manual dev-pod checks used the pod's local llama.cpp service, not a hosted provider.


Summary by cubic

Moves smart-fill and smart-configure off the web thread: each request becomes a persisted suggestion processed by the bill_setup job, so the dialog waits with a Stop button, reloads show the stored answer, and stopping works even after the feature becomes unavailable. The same async pattern now covers PDF import reading and MCP AI tools, and family document uploads index in the background.

Bill suggestions

  • recurring_transaction_suggestions stores the family, user, subject, mode, status, answer, and a safe failure code; exactly one subject is required, and DataCleanerJob removes rows older than seven days.
  • Smart-fill and smart-configure become create/show/destroy resources; a second click reuses a pending row, and partial unique indexes keep a second pending job from starting.
  • The suggestion and its generation run commit in one transaction, so a lost run marks the row failed instead of leaving a pending row with no job; lookups go through find_or_start!.
  • Stop, apply, and fail are single locked writes; only a pending row can be stopped, and finished rows cannot.
  • Answers and rationales are hidden when the entry's access was revoked since the request.
  • The job reads its one-item window, so a completed run ends instead of re-asking the model; stopping before the job starts ends the run without a model call.
  • The job rechecks consent and normalizes evidence the same way the removed on-thread AiSetupSuggester did; router reasons get their usual sentence, looked up from their locale keys, and provider text is logged, never shown.
  • The bill_setup guard applies only to create, so show and Stop stay reachable if availability changes. The new-bill dialog and bill drawer show a "Turn on AI" button when only the member's switch blocks the feature.
  • A Stimulus controller re-navigates the waiting frame once the cable subscribes, covering a broadcast that beat the subscription.

PDF imports, documents, and MCP AI tools

  • AI reading of a PDF import can be stopped; the import stays, late results are fenced out, and a cancelled status is exposed in the API and UI.
  • Document uploads queue an IndexFamilyDocumentJob; the bytes stay on a pending row until indexing, and failures retry instead of failing the upload.
  • AI-backed tools run through Ai::ToolJob on their feature lane with timeouts and busy responses; retried import_bank_statement calls return the existing derived import without re-extracting.

Summary by CodeRabbit

  • New Features
    • Get AI-powered suggestions for bill settings based on payment history, or recurring transaction details based on an entry.
    • Track analysis progress, stop a pending suggestion, and review proposed bill changes before applying them.
    • Reopen stored suggestions, including completed results, even if AI features later become unavailable.
  • Bug Fixes
    • Failed or stopped analyses leave existing entries and bill settings unchanged and show clearer status messages.
    • Repeated requests reuse an existing pending suggestion instead of starting another analysis.