Documents in a job

Merged
JO
2d ago
+719-88

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

What and why

Non-PDF document uploads currently send bytes to the vector store on the web thread, so a slow provider holds a Puma request. This PR creates a pending FamilyDocument with the upload attached, enqueues an IndexFamilyDocumentJob on embed's lane, and lets the job do the upload. The user sees the existing redirect immediately with a notice that the file will be searchable once indexed.

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–§3e, with the design in PR 3 and the redesign.

What changes

  • Migration (20261202000000): family_documents.uploader_id and account_statements.uploader_id, nullable references to users (on_delete: :nullify), no backfill. They record who uploaded a document or statement, so the indexing jobs can check that person's AI switch again before sending a search copy.
  • Family#upload_document_later(file, uploader:) attaches the upload to a pending row and enqueues IndexFamilyDocumentJob.perform_later; upload_document remains for callers that already hold bytes. If the enqueue raises (Redis), the upload is still reported as uploaded: the pending row is indexed by that job or by the scheduled sweep, which re-sends a pending row no job has claimed after an hour. A queue that refuses the job is a failure, as before.
  • Family#index_document is the shared upload/write path for the new job and existing callers. Rows created pending and later set to processing explicitly enqueue their status poll, since FamilyDocument's hook only fires on rows created processing.
  • IndexFamilyDocumentJob claims the pending row so two deliveries never upload twice, rechecks Family#embedding_allowed_for?(uploader) (the uploader may still use embed with their own switch on; with no recorded uploader, someone in the family still has AI on), and marks terminal failures with mark_error! and a DebugLogEntry.
  • Statements use the same rule: every upload path records the uploader instead of the model reading Current.user, and IndexAccountStatementJob checks statement.uploader. It still accepts the old uploader id argument, for jobs queued before this change.
  • The imports page lists your own uploads whose indexing failed in the last 7 days (a DS::Alert).
  • The attached file is purged only after the status write commits. VectorStore::IndexStatusJob purges after its terminal poll outcome as well, so asynchronously indexed documents do not keep their attachment.
  • The imports controller keeps all availability, size, extension and PDF checks; only the non-PDF upload path and its notice change (English and German).
  • Tests cover the pending row and enqueue, job success and failure, the uploader recheck, purge ordering, the status poll path, the lane assignment, the controller checks and the live e2e upload through the job.

Validation

Review fixes: indexing redelivery (a per-row claim) and stale-pending recovery; cleanup retries (VectorStore::PurgeDocumentFileJob), with stored bytes deleted before Active Storage removes its records. Second editor pass (1f793f7b3): the uploader columns and shared rule above; an enqueue that raises is now one case with a lost job, so the enqueue_error marker is gone; one purge-or-retry for both jobs; the Busy rescue removed (embedding runs in the prompt lane, which waits instead of raising it).

Checks (local, on this branch, after the editor pass): full Rails suite 19,334 runs / 212,887 assertions / 0 failures / 0 errors / 32 skips; focused Minitest for the job, upload path and purge ordering 42 runs / 170 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.

Checks after the second editor pass: full Rails suite 20,348 runs / 0 failures / 0 errors / 32 skips; RuboCop and Brakeman clean.

First editor pass: consolidated the job tests, moved attachment cleanup into FamilyDocument#purge_file_if_settled, and trimmed explanatory comments.

sure-dev verification: uploaded dev-doc.txt through /imports; IndexFamilyDocumentJob indexed it to pgvector, leaving the row ready with a provider file id and no attached bytes. An MCP search_family_files call then found the document's marker text through the worker path.

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/pgvector services, not a hosted provider.

🤖 Generated with Claude Code

https://claude.ai/code/session_016zAWUWiGdNK4xoU3JEzKbB


Summary by cubic

Moves non-PDF document uploads off the web thread so a slow vector store no longer holds a Puma request. The imports controller creates a pending FamilyDocument with the file attached, enqueues IndexFamilyDocumentJob to send the bytes, and redirects with a notice that the file will be searchable once indexed.

IndexFamilyDocumentJob claims the pending row before uploading so a redelivered or recovered job never indexes twice. Recovery (requeue_index_polls) resends pending rows whose claim is missing or stale, covering a lost job and an enqueue that raised; a queue that refuses the job marks the upload failed. The job rechecks embedding availability and records terminal failures with a DebugLogEntry. Attachments are purged only after a terminal status commits, including through VectorStore::IndexStatusJob polls; a failed purge retries via VectorStore::PurgeDocumentFileJob without changing the recorded result.

Who uploaded a document or statement now gates its search copy: family_documents and account_statements get a nullable uploader_id (migration 20261202000000, no backfill), and Family#embedding_allowed_for? decides whether the copy may be made, checked again by the indexing jobs. Statements record their uploader at upload instead of reading Current.user; IndexAccountStatementJob still accepts the legacy uploader id for jobs queued before the change.

Uploads that failed to index appear in a warning alert on the new import page (English and German), limited to the user's own uploads from the last seven days. The controller keeps all existing availability, size, extension, and PDF checks; upload_document remains for callers that already hold bytes.

Summary by CodeRabbit

  • New Features
    • Document uploads are now processed in the background, so you can continue without waiting for indexing to finish.
    • Uploaded files are retained while indexing is in progress and cleaned up when processing reaches a final status.
    • Upload confirmation explains that documents become searchable once indexing is complete.
  • Bug Fixes
    • Oversized documents are rejected with a size-specific alert before indexing begins.
    • Uploads that cannot be queued for processing show a failure alert instead of a success confirmation.