Stop a PDF import

Closed
JO
9d ago
+403-17

What and why

An AI PDF import could not be stopped: once ProcessPdfJob started, the only "cancel" action marked work lost as failed, and a late model answer could still write rows and send the next-steps email. This PR adds a cooperative Stop action. It marks the import cancelled, checks that status at every step of the job, and fences late answers out with the existing reread-claim guard. A request already sent keeps its usage row; its result is simply not applied.

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/P1–P2, with the design in PR 3 and the redesign.

What changes

  • Import gains a cancelled status (no migration: the column is already a string). The existing lost-work cancel action and force_fail! are unchanged.
  • DELETE /pdf_imports/:pdf_import_id/ai_processing stops only a viewable, importing PDF import in the family with statement-write permission. The import page hides the button unless PdfImport#stoppable_by? agrees with those checks.
  • ProcessPdfJob checks cancellation before read, upload, extract, row generation, email and final write, and rescues Ai::Error::Cancelled without recording a failure. Its existing token/status claim rejects late writes.
  • The import page shows Stop while processing and a stopped state afterwards; the index gets a cancelled badge. English and German strings only.
  • cancelled is exposed in the imports API schemas and status filter, and _status_detail.json.jbuilder treats it as terminal. The rswag documentation is regenerated.
  • Minitest covers the endpoint, permission and status fences, job checkpoints, late answers and usage rows, API filtering, and the UI states.

Validation

Review fixes: Statement-less imports now require statement-manager permission to stop.

Checks (local, on this branch, after the editor pass): full Rails suite 19,327 runs / 212,904 assertions / 0 failures / 0 errors / 32 skips; focused Minitest for the stop endpoint, job checkpoints and API coverage 246 runs / 1,049 assertions / 0 failures / 0 errors; 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; the rswag OpenAPI document was regenerated.

Editor pass: removed four redundant cancelled fences and the pass-through Ai::Error::Cancelled rescue from ProcessPdfJob, merged the controller UI states into one test, reduced the job stop tests from 4 to 3, and dropped one implementation-only status test. The permission split between controller and view stays: the controller must distinguish "not processing" from "not authorised".

sure-dev verification: uploaded sample_bank_statement.pdf, saw Stop while the import was importing, submitted the ai_processing Delete form, and got cancelled plus the "Processing stopped" page. A later check confirmed the in-flight AI call added no summary or transactions.

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 check used the pod's local llama.cpp service, not a hosted provider.


Summary by cubic

Uploads family documents through a background indexing job and adds a Stop action so a running PDF AI import can be cancelled.

  • Document uploads keep their bytes on a pending row, queue an embedding job, and purge the attachment only after a terminal status commits. Failed enqueues stay recoverable via FamilyDocument.requeue_index_polls, and failed purges retry without re-indexing.
  • Stop marks an importing import cancelled, checks that status at every job step, and fences late model answers out via the existing claim guard, so a finished request keeps its usage row but its result is never applied.
  • Stopped imports show a stopped state and a cancelled badge; the API exposes cancelled as a status and treats it as terminal. Statement-less imports require statement-manager permission to stop.
  • No migration needed: cancelled is a new Import status value, not a schema change.

Summary by CodeRabbit

  • New Features
    • You can stop an in-progress PDF import when you have permission to manage its statement. Pending imports cannot be stopped.
    • Stopped imports display a stopped status on the import screen and in the import list. The imports API also reports stopped status and supports filtering by it.
  • Bug Fixes
    • Results received after an import is stopped no longer update the import or trigger follow-up actions. Stopped imports remain stopped rather than being marked as failed.