Documents in a job

Closed
JO
9d ago
+546-34

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

  • Family#upload_document_later(file) attaches the upload to a pending row and enqueues IndexFamilyDocumentJob.perform_later; upload_document remains for callers that already hold bytes.
  • 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 rechecks Ai.available?(:embed, family:), marks terminal failures with mark_error! and a DebugLogEntry, and lets Ai::Error::Busy retry through ApplicationJob.
  • 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/failure/busy, 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 and cleanup retries; raised enqueue failures keep staged bytes for recovery. Follow-up: an already-accepted job no longer purges those bytes, a rejected retry enqueue keeps the recovery marker so the next pass can retry, and stored bytes are deleted before Active Storage removes its records.

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.

Editor pass: consolidated the job tests (8 -> 7), collapsed the failure paths into one rescue with raise if e.is_a?(Ai::Error::Busy), moved attachment cleanup into FamilyDocument#purge_file_if_settled, and trimmed explanatory comments. No behaviour outside these simplifications changed.

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.


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 now creates a pending FamilyDocument with the file attached, enqueues IndexFamilyDocumentJob to send the bytes, and redirects immediately with a notice that the file will be searchable once indexed.

IndexFamilyDocumentJob rechecks embed availability, marks terminal failures on the row with a DebugLogEntry, and lets Ai::Error::Busy retry via ApplicationJob. A redelivered job skips re-indexing a settled row. Uploaded attachments are purged only after a terminal status write commits, including through VectorStore::IndexStatusJob polls; a failed purge never overwrites the indexing result and retries via the new VectorStore::PurgeDocumentFileJob. If the queue raises at enqueue time, the bytes stay staged on an error row and FamilyDocument.requeue_index_polls retries the upload. 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.