feat(documents): bridge the statement vault to search and let tools read a file

Merged
HO
8d ago
+1498-23

Third of the stacked PRs split out of #3402, and independent of the others: it is based on main, depends on nothing else in the series, and nothing else depends on it. One additive migration.

Sure had two document stores and only one was searchable

AccountStatement.create_from_prepared_upload! never touched the vector store. A statement uploaded through the web vault or the MCP tool existed only in account_statements and was structurally invisible to search_family_files. Only /imports (PdfImport → ProcessPdfJob) bridged the two.

The user-facing shape of that bug: attach a PDF to the right account, ask the assistant about it, and be told no document had been uploaded. Truthfully from the code's point of view, and falsely from theirs.

Three separate defects, fixed separately.

The bridge

An after_create_commit on AccountStatement, the one point all three producers share, enqueues indexing into the document store.

Both sides take the statement row lock before deciding to index. The existence check alone is check-then-write, and ProcessPdfJob runs the same check on another queue with no ordering between them, so two workers that both read "not indexed" would both upload and leave two documents for one statement. Indexing failures are captured to the debug log and never fail an upload whose bytes are already safely stored.

The message

No documents have been uploaded to the family document store yet

was returned whenever families.vector_store_id was NULL, which means no store was ever created, an entirely different claim from "you uploaded nothing". Both unconfigured cases now degrade to a metadata search over the vault and report the real reason, so the answer becomes "here is what exists, full-text search is unavailable".

That fallback gates on AccountStatement.statement_manager? like every other vault tool. Without it, full-text search merely being unconfigured became a way around the role check.

The contents

Every vault tool returned metadata only, and the bytes are served exclusively to an authorized browser session, so a tool client had no route to them at all.

get_document_text reads a statement through PDF::Reader, already a dependency and already what backs VectorStore::Embeddable. It pages on whole-page boundaries, because a statement cut mid-page reads as a truncated number, and an oversized page is served in chunks through a from_char cursor rather than silently losing its tail. A PDF with no text layer is reported as a scan, with an explicit "there is no OCR here, ask the user", rather than as a document that says nothing; an unparseable file is reported distinctly from a scan.

Access is narrower than the other vault tools: full text is a bigger disclosure than metadata, so it also requires access to the linked account.

Documents carry their owning account

The store is one family-wide index, and this branch started feeding every vault upload into it, so a statement on an account the caller cannot see would otherwise come back through the vector path.

The link is a column rather than a metadata key, because metadata cannot be joined, indexed or constrained. FamilyDocument.readable_by is the scope. The migration backfills account_id from the existing metadata behind a strict UUID pattern and a join on accounts, so malformed input cannot cast-error the migration. ProcessPdfJob stamps the same link, which closes the one leak that predates this work.

Tests

581 tests green across the document, vault and assistant-function suites. Rubocop clean, Brakeman reports no warnings.

Where this sits

main
├── G  Wise SCA key encryption   → #3415  (independent)
├── E  this PR                             (independent, 1 migration)
└── A  base tool context         → #3421
    ├── D  seasonal recurring detection
    └── B  overdraft and loan terms        (2 migrations)
        ├── C  cash-flow projection and scenarios
        └── F  category trends and loan amortization

This one can be reviewed and merged in any order relative to the rest.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added automatic indexing of uploaded account statements for document search.
    • Added preview access to retrieve statement text page by page, including large documents.
    • Added text extraction for PDFs, CSVs, and supported text files, with clear handling for scans and unsupported formats.
    • Added account-aware access controls for safer document search results.
  • Bug Fixes

    • Searches now provide metadata-only results when document search is unavailable.
    • Improved document visibility when statements are linked to or unlinked from accounts.
    • Search results now distinguish query matches from recent statement listings.
    • Improved handling and reporting of unsuccessful document indexing and removal.