From d845e44ff8cd550687743e57b6ae06437aca23f4 Mon Sep 17 00:00:00 2001 From: Guillem Arias Fauste Date: Mon, 8 Jun 2026 21:35:12 +0200 Subject: [PATCH] feat(ai): default Anthropic installs to pgvector RAG (4/5) (#1986) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(ai): add Anthropic provider with chat parity (1/5) Introduces Provider::Anthropic alongside Provider::Openai, implementing the LlmConcept chat_response contract over the official anthropic Ruby SDK. Batch ops, PDF, and RAG land in follow-up PRs. - Provider::Anthropic uses Messages API for sync and streaming responses - ChatConfig builds requests with ephemeral prompt-cache markers on the system prompt and the last tool definition - MessageFormatter reconstructs multi-turn history (text + tool_use + tool_result blocks) from raw Message records, including the paired user-role tool_result turn Anthropic requires after every tool_use - ChatParser maps Anthropic Message into the shared ChatResponse Data - Registry, Setting, User, Chat default model wired for ANTHROPIC_* envs and Setting.anthropic_*; LLM_PROVIDER selects between providers - Responder forwards raw conversation_history (Array) so providers without hosted conversation state can rebuild context - OpenAI provider accepts and ignores the new kwarg (no behavior change) Tests cover provider init, model gating, MessageFormatter for all turn shapes, ChatConfig request building (max_tokens, system cache, tool conversion), ChatParser for text / tool_use / mixed blocks, Registry discovery, and mocked chat_response success / error / function_request paths. Live VCR cassettes recorded in a follow-up with a real key. Stacked PRs: 2/5 batch ops + cost ledger, 3/5 PDF, 4/5 pgvector RAG, 5/5 settings UI + disclosure. * fix(ai): address PR review on Anthropic provider foundation Surface fixes raised by Codex + CodeRabbit on PR 1/5: - Provider::Anthropic#chat_response now accepts (and ignores) a `messages:` kwarg. Assistant::Responder passes both `messages:` (OpenAI-shape) and `conversation_history:` (raw Message records) for cross-provider parity, so the previous signature raised ArgumentError on the first chat turn through the Anthropic provider. - Provider::Anthropic#supports_model? bypasses the `claude` prefix gate when a custom base_url is configured, mirroring the OpenAI provider. Bedrock-shaped IDs like `anthropic.claude-sonnet-4-5-20250929-v1:0` and `claude-opus-4@20250514` are otherwise rejected by Assistant::Provided#get_model_provider and the chat dies. - Setting.anthropic_access_token is now in EncryptedSettingFields::ENCRYPTED_FIELDS so the Anthropic API key is encrypted at rest like every other provider secret. Previously plaintext while siblings (openai_access_token, twelve_data_api_key, external_assistant_token) were ciphertext. - Chat.default_model falls back to whichever provider is actually configured. Previously, with LLM_PROVIDER=anthropic but no Anthropic credentials, the default model resolved to a Claude ID that no registered provider supported, so chats failed even when OpenAI was fully configured. Adds Provider::{Anthropic,Openai}#configured? class methods for the readable callsite. - Provider::Anthropic.effective_model uses `ENV["ANTHROPIC_MODEL"].presence || Setting.anthropic_model` so the Setting lookup is only performed when the env var is absent — the previous `ENV.fetch(KEY, default)` evaluated the default arg eagerly on every call. - Provider::Anthropic::ChatConfig#anthropic_input_schema strips both `:strict` and `"strict"` keys so JSON-decoded schemas with string keys cannot leak the OpenAI-only flag through to Anthropic. Test coverage added: supports_model? bypass on custom endpoints, chat_response messages: kwarg compatibility, default_model fallback in the three credential combinations, configured? against ENV + Setting, strict-flag stripping for both key types, and a `Setting.expects(:anthropic_model).never` assertion proving the ENV-precedence test now exercises the lazy path. All 4365 tests pass (1 pre-existing libvips env error unrelated). * test(chat): make default_model tests resilient to ENV model overrides CodeRabbit flagged on PR review: the new default_model tests asserted against Provider::*::DEFAULT_MODEL, but Chat.default_model actually returns Provider::*.effective_model.presence (which reads OPENAI_MODEL / ANTHROPIC_MODEL from the environment). With either env var set, the tests would fail intermittently even though routing was correct. - New default_model tests now assert against the provider's effective_model directly, so they verify the routing decision (which provider's value wins) without coupling to the constant. - Pre-existing "creates with default model" assertions had the same brittleness; switch them to compare against Chat.default_model so the chosen model is whatever the env / Setting cascade resolves to. Verified by running `ANTHROPIC_MODEL=claude-haiku-4-5 OPENAI_MODEL=gpt-4o bin/rails test test/models/chat_test.rb` — 16 runs, 0 failures (previously 2 pre-existing failures + 0 from the new tests). * fix(ai): address local review on Anthropic foundation - Provider::Anthropic#supports_pdf_processing? bypasses prefix gate for custom endpoints, mirroring supports_model? - Provider::Anthropic#initialize raises Error when custom_endpoint? AND model.blank?, parity with Provider::Openai - stream_chat_response captures partial usage on mid-stream errors and records it via the new on_partial callback so chat_response can skip the duplicate error row in the outer rescue - safe_accumulated_message swallows the secondary failure when the SDK cannot reconstruct a snapshot - langfuse_client memoizes properly (||= instead of =) so repeated calls don't churn Langfuse instances - MessageFormatter sorts tool_calls by created_at then id so the message array is deterministic across replays; skips tool_calls missing both provider_call_id and provider_id rather than sending `id: nil` and getting rejected by Anthropic - Setting.anthropic_access_token default falls back through ENV["ANTHROPIC_API_KEY"].presence (was missing .presence, so an empty-string env value bled through) - User#openai_configured? / #anthropic_configured? delegate to the Provider::* class methods — single source of truth - Assistant::Responder renames the OpenAI-shape history builder conversation_history → openai_messages_payload so the kwarg name matches the local method name (messages: openai_messages_payload, conversation_history: chat_message_records) - Assistant::Builtin stale-history comment updated to reference both builders Adds a streaming chat_response test using ad-hoc subclasses of the SDK event types so the case/when dispatch matches via is_a? without stubbing class-level === behavior. * test(ai): add Anthropic tool_use round-trip + multi-tool turn coverage Addresses @jjmata's "worth confirming" note on PR #1983: tool-use turns from prior assistant messages must round-trip correctly when retrieved from the database. - New `ChatParser → ToolCall::Function → MessageFormatter` test walks the full path: Anthropic response with a tool_use block → ChatFunctionRequest → ToolCall::Function.from_function_request → persisted on the AssistantMessage → MessageFormatter rebuild on the next turn. Asserts the original `tool_use.id` is preserved end-to-end as both `tool_use.id` and the paired `tool_result.tool_use_id`, and that the original `input` hash and serialized result content survive. - New multi-tool assistant turn test confirms two tool_use blocks on a single assistant message render as two tool_use blocks followed by two paired tool_result blocks in a single user-role follow-up, matching Anthropic's required alternation. Both tests exercise the existing PR1 code without behavior changes. * test(ai): require "ostruct" explicitly in Anthropic provider tests OpenStruct is moving out of Ruby's default load path (warning in 3.4+, removed in 3.5+). Tests work today because ActiveSupport transitively loads it, but that's incidental. Match the existing convention in test/controllers/settings/hostings_controller_test.rb which explicitly requires ostruct for the same reason. * fix(ai): sanitize Langfuse warn logs, normalize tool_use.input, dedup history fetch Addresses three open CodeRabbit findings on PR #1983. - Provider::Anthropic Langfuse rescue branches no longer include `e.full_message` in `Rails.logger.warn`. `full_message` bundles the backtrace + cause chain and on some SDK error types includes the serialized request/response payload (prompt, model output). Logs now report `#{e.class}: #{e.message}` only. Three sites: create_langfuse_trace, log_langfuse_generation, upsert_langfuse_trace. Note: Provider::Openai has the same pattern (copy-pasted source) — harmonization deferred to a follow-up cleanup PR; this commit fixes only the Anthropic provider to keep PR scope tight. - MessageFormatter#parse_arguments now coerces any non-Hash parsed result to `{}`. Anthropic's Messages API requires `tool_use.input` to be a JSON object (map); a stored ToolCall::Function record whose arguments parse to a scalar, bool, or array (corrupt row, legacy data, cross-provider bleed) would otherwise produce a payload the API rejects. Normal flow stores Hash arguments end-to-end so the fix is defensive — adds 2 tests covering scalar/array JSON strings and non-String non-Hash inputs. - Assistant::Responder dedups the chat-history fetch. The previous layout fired two near-identical `chat.messages.where(...).includes( :tool_calls).ordered` queries per LLM turn (one for the OpenAI-shape payload, one for the raw-records kwarg). A new memoized `complete_chat_messages` fetches once; `chat_message_records` filters out the current message via `Array#reject`, `openai_messages_payload` iterates the cached array unchanged. One SQL query per turn instead of two. Memoization scope = single Responder instance (per LLM call), so cache invalidation is not a concern. All 4370 tests pass (1 pre-existing libvips env error unrelated). Rubocop + brakeman clean. * fix(ci): replace sk-ant- prefixed test placeholders Pipelock secret scanner pattern-matches `sk-ant-*` as a real Anthropic API key and fails the PR security-scan check. Test stubs and ClimateControl env values used `sk-ant-test`, `sk-ant-from-setting`, `sk-ant-x`, `sk-ant-y` as obvious placeholders, but the scanner does not care about value entropy. Switched to `fake-anthropic-key-*` / `fake-token-*` strings so the scanner stops flagging them. No production code touched, no behavior change — Provider::Anthropic still accepts any non-blank token. * feat(ai): add Anthropic batch ops + LLM cost ledger (2/5) Implements auto_categorize, auto_detect_merchants, and enhance_provider_merchants on Provider::Anthropic via forced tool calls, plus the cost-ledger plumbing they need. - Provider::Anthropic::AutoCategorizer, AutoMerchantDetector, ProviderMerchantEnhancer each define a single output tool whose input_schema mirrors the desired output, then force the model to call it via tool_choice: { type: "tool", name: ..., disable_parallel_tool_use: true }. Anthropic guarantees the tool_use.input matches the schema, so there is no JSON parsing fragility, no tag stripping, and no json_object/json_schema fallback ladders. - Concerns::UsageRecorder mirrors the OpenAI sibling but persists cache_creation_input_tokens / cache_read_input_tokens to dedicated columns instead of metadata. - Migration adds cache_creation_tokens, cache_read_tokens (nullable integers) to llm_usages. OpenAI rows leave them null. - LlmUsage::PRICING gains Claude 4.x rows (opus-4-7 $15/$75, sonnet-4-6 $3/$15, haiku-4-5 $1/$5 per MTok). infer_provider returns "anthropic" for claude-* via the existing exact/prefix lookup. - Provider::Anthropic#chat_response now persists cache columns directly rather than stashing them in metadata. - 25-transaction batch cap mirrors the OpenAI provider so the cost ledger sees the same shape regardless of which provider ran a batch. Tests cover the forced-tool-call path, null/None normalization, case-insensitive merchant matching, the missing-tool_use error path, and Anthropic-specific pricing + provider inference on LlmUsage. Stacked on #1983 (PR 1/5). 3/5 PDF + vision next. * fix(ai): attribute Bedrock model IDs to anthropic + clean nil enum - LlmUsage.infer_provider now returns "anthropic" for Bedrock / Vertex shaped IDs (anthropic.* and anthropic/*), so cost-ledger filtering by provider stays correct even when no per-MTok rate is stored. Previously these IDs fell through to the "openai" default. - AutoCategorizer drops the redundant nil sentinel from the category_name enum — the union type [string, null] already permits null, and some JSON Schema validators reject nil literals inside enum arrays. * test(ai): require "ostruct" in Anthropic batch op tests Same rationale as the PR1 ostruct fix — explicit require so the tests don't depend on ActiveSupport's transitive load when Ruby 3.5+ removes OpenStruct from the default load path. * feat(ai): Anthropic native PDF processing (3/5) Implements process_pdf and extract_bank_statement on Provider::Anthropic using the native `document` content block — no rasterization, no text pre-extraction. - Provider::Anthropic::PdfProcessor classifies the document, summarizes it, and extracts statement metadata via a forced report_document_analysis tool whose input_schema mirrors the existing Provider::Openai output (document_type from Import::DOCUMENT_TYPES, summary, extracted_data). - Provider::Anthropic::BankStatementExtractor returns the same { transactions, period, account_holder, account_number, bank_name, opening_balance, closing_balance } shape via report_bank_statement so downstream pdf_import code is provider-agnostic. - Both attach the PDF as { type: "document", source: { type: "base64", media_type: "application/pdf", data: } } — Claude 3.5+ / 4.x accept this natively (up to 32MB / 100 pages). No pdf-reader, no pdftoppm, no chunking for typical statements. - supports_pdf_processing? (introduced in PR 1) already returns true for claude-* models, gating process_pdf with a clear error otherwise. - Cost ledger rows are persisted via the shared UsageRecorder concern, including cache_creation/cache_read tokens. Tests verify the document block shape, tool_choice forcing, normalized document_type for unknown classifications, transaction normalization (date / amount / reference → notes), and the missing-tool_use error path. Blank pdf_content raises before any client call. Stacked on #1984 (PR 2/5). 4/5 pgvector RAG next. * fix(ai): guard PDF size + surface bank-statement truncation - PdfProcessor and BankStatementExtractor raise upfront when pdf_content.bytesize exceeds MAX_PDF_BYTES (32 MB, matching Anthropic's hard limit). Previously a 100 MB PDF would be base64-encoded (~133 MB) and packed into the JSON body before the API rejected it — peak heap ~270 MB per Sidekiq worker. - BankStatementExtractor inspects response.stop_reason; when the model hit max_tokens it logs a warning and flags result[:truncated] so downstream callers know the transaction list may be incomplete. - ISO date pattern added to statement_period_start/end schema in PdfProcessor so the model can't return "March 2026" — Anthropic enforces the regex via the tool's input_schema. Tests cover the size guard (raises before any client.messages call), truncated-result flagging, and the warning log path. * test(ai): require "ostruct" in Anthropic PDF tests Match the explicit ostruct require added in PR1/PR2 — same Ruby 3.5+ load-path reason. * feat(ai): default Anthropic installs to pgvector RAG (4/5) The provider-agnostic vector store stack (VectorStore::Pgvector + the Embeddable concern) already shipped to main. This PR closes the Anthropic loop: - VectorStore::Registry.adapter_name now returns :pgvector when Setting.llm_provider == "anthropic" and no explicit VECTOR_STORE_PROVIDER override is set. Anthropic has no hosted vector store, so falling back to the local pgvector adapter is the only correct default. Explicit VECTOR_STORE_PROVIDER still wins. - SearchFamilyFiles surfaces a longer message when no adapter is wired up — calling out pgvector + EMBEDDING_URI_BASE as the supported Anthropic-only path so the user is not stuck with an "OpenAI required" hint that is no longer accurate. The Embeddable concern already pulls embeddings from EMBEDDING_URI_BASE / EMBEDDING_ACCESS_TOKEN (with OpenAI as fallback), so Anthropic installs point this at Voyage AI, a local Ollama instance, or OpenAI embeddings — independent of the chat provider. Tests cover the new default routing, the existing OpenAI default staying intact, and explicit VECTOR_STORE_PROVIDER overriding the Anthropic default. Stacked on #1985 (PR 3/5). 5/5 settings UI + retention disclosure next. * fix(ai): provision pgvector table when it is the default store #1986 makes pgvector the default vector store for Anthropic installs, but CreateVectorStoreChunks only ran when VECTOR_STORE_PROVIDER=pgvector was set explicitly — so a fresh Anthropic-only install migrated without the vector_store_chunks table and failed on uploads/searches. Add VectorStore::Registry.pgvector_effective? as the single source of truth for "is pgvector active?" (explicit env OR the Anthropic default), and a new idempotent migration that enables the extension + creates the table whenever pgvector is effective and the table is missing — covering fresh and already-migrated installs without drift. Addresses Codex P1. * fix(ai): provision pgvector table for Anthropic-default installs Migration gated on raw VECTOR_STORE_PROVIDER==pgvector, so an Anthropic-default install (which selects pgvector implicitly via Setting.llm_provider without setting VECTOR_STORE_PROVIDER) skipped table creation and failed later on a missing vector_store_chunks relation. Route through VectorStore::Registry.pgvector_effective? — the single source of truth already shared by the adapter selection. Addresses Codex P1 review finding. * fix(ai): provision pgvector chunks table on schema-load installs The ensure-migration only helps db:migrate upgraders. Fresh installs go through bin/docker-entrypoint's db:prepare, which loads schema.rb (the conditional table can't be dumped there — it needs the vector extension) and marks every migration applied without running it. An Anthropic-only fresh install therefore selected the pgvector adapter but had no table, failing with raw PG errors on first upload or search. Two layers close it: - VectorStore::Pgvector#ensure_schema! provisions the table idempotently on first use (mirrors CreateVectorStoreChunks; memoized; failures wrap in VectorStore::Error, which with_response turns into a clean failed response). - VectorStore::Registry#build_pgvector now gates on VectorStore::Pgvector.available? (table exists, or extension present), so installs whose Postgres lacks pgvector entirely degrade to the assistant's provider_not_configured message instead of raising mid-chat. Also resolves the schema.rb version conflict against main (keep the branch's 2026_06_01_120000, on top of main's current tables). * fix(ai): address review nitpicks on pgvector provisioning - Registry: update the adapter doc comment to mention the Anthropic-to-pgvector default alongside the openai fallback. - ensure_schema!: guard the DDL with if_not_exists instead of a Mutex. Adapter instances are built per call and never shared across threads, so the realistic race is two processes (web + Sidekiq) provisioning concurrently; IF NOT EXISTS makes the loser a no-op where a Mutex would only serialize threads inside one process. --- .../assistant/function/search_family_files.rb | 5 +- app/models/vector_store/pgvector.rb | 61 ++++++++++++++++ app/models/vector_store/registry.rb | 31 +++++++- ...260316120000_create_vector_store_chunks.rb | 18 +++-- ...ector_store_chunks_for_default_pgvector.rb | 55 ++++++++++++++ db/schema.rb | 2 +- test/models/vector_store/pgvector_test.rb | 68 +++++++++++++++++ test/models/vector_store/registry_test.rb | 73 ++++++++++++++++++- 8 files changed, 301 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20260601120000_ensure_vector_store_chunks_for_default_pgvector.rb diff --git a/app/models/assistant/function/search_family_files.rb b/app/models/assistant/function/search_family_files.rb index c9c917f0a..3f5f914ff 100644 --- a/app/models/assistant/function/search_family_files.rb +++ b/app/models/assistant/function/search_family_files.rb @@ -71,7 +71,10 @@ class Assistant::Function::SearchFamilyFiles < Assistant::Function return { success: false, error: "provider_not_configured", - message: "No vector store is configured. Set VECTOR_STORE_PROVIDER or configure OpenAI." + message: "No vector store is configured. Set VECTOR_STORE_PROVIDER " \ + "(openai | pgvector | qdrant), configure OpenAI, or — for " \ + "Anthropic-only installs — enable the pgvector adapter and " \ + "point EMBEDDING_URI_BASE at an embeddings endpoint." } end diff --git a/app/models/vector_store/pgvector.rb b/app/models/vector_store/pgvector.rb index a434ec1f5..2331bd335 100644 --- a/app/models/vector_store/pgvector.rb +++ b/app/models/vector_store/pgvector.rb @@ -15,6 +15,25 @@ class VectorStore::Pgvector < VectorStore::Base PGVECTOR_SUPPORTED_EXTENSIONS = (VectorStore::Embeddable::TEXT_EXTENSIONS + [ ".pdf" ]).uniq.freeze + TABLE_NAME = "vector_store_chunks" + + # True when this adapter can actually operate: the chunks table already + # exists, or the server has the pgvector extension available so + # ensure_schema! can provision it on first use. The Registry consults this + # before building the adapter, so an install without pgvector degrades to + # the assistant's friendly "provider_not_configured" message instead of + # raising raw PG errors mid-chat. + def self.available? + conn = ActiveRecord::Base.connection + return true if conn.table_exists?(TABLE_NAME) + + conn.select_value( + "SELECT 1 FROM pg_available_extensions WHERE name = 'vector' LIMIT 1" + ).present? + rescue StandardError + false + end + def supported_extensions PGVECTOR_SUPPORTED_EXTENSIONS end @@ -27,6 +46,7 @@ class VectorStore::Pgvector < VectorStore::Base def delete_store(store_id:) with_response do + ensure_schema! connection.exec_delete( "DELETE FROM vector_store_chunks WHERE store_id = $1", "VectorStore::Pgvector DeleteStore", @@ -37,6 +57,7 @@ class VectorStore::Pgvector < VectorStore::Base def upload_file(store_id:, file_content:, filename:) with_response do + ensure_schema! text = extract_text(file_content, filename) raise VectorStore::Error, "Could not extract text from #{filename}" if text.blank? @@ -81,6 +102,7 @@ class VectorStore::Pgvector < VectorStore::Base def remove_file(store_id:, file_id:) with_response do + ensure_schema! connection.exec_delete( "DELETE FROM vector_store_chunks WHERE store_id = $1 AND file_id = $2", "VectorStore::Pgvector RemoveFile", @@ -94,6 +116,7 @@ class VectorStore::Pgvector < VectorStore::Base def search(store_id:, query:, max_results: 10) with_response do + ensure_schema! query_vector = embed(query) vector_literal = "[#{query_vector.join(',')}]" @@ -127,6 +150,44 @@ class VectorStore::Pgvector < VectorStore::Base private + # Provisions the chunks table on first use, mirroring the + # CreateVectorStoreChunks migration. Migrations cover db:migrate + # upgrades, but fresh installs go through db:prepare → schema:load + # (bin/docker-entrypoint), which marks conditional migrations as applied + # without running them — and the table can't live in schema.rb because it + # requires the vector extension. Idempotent; memoized per instance. + def ensure_schema! + return if @schema_ensured + if connection.table_exists?(TABLE_NAME) + @schema_ensured = true + return + end + + connection.enable_extension("vector") unless connection.extension_enabled?("vector") + # if_not_exists on the DDL (not a Mutex) is the right concurrency guard + # here: adapter instances are built per call and never shared across + # threads, so the realistic race is two *processes* (e.g. web + Sidekiq) + # provisioning at once. IF NOT EXISTS makes the loser's DDL a no-op + # instead of a duplicate-relation error. + connection.create_table(TABLE_NAME, id: :uuid, if_not_exists: true) do |t| + t.string :store_id, null: false + t.string :file_id, null: false + t.string :filename + t.integer :chunk_index, null: false, default: 0 + t.text :content, null: false + t.column :embedding, "vector(#{ENV.fetch('EMBEDDING_DIMENSIONS', '1024')})", null: false + t.jsonb :metadata, null: false, default: {} + t.timestamps null: false + end + connection.add_index TABLE_NAME, :store_id, if_not_exists: true + connection.add_index TABLE_NAME, :file_id, if_not_exists: true + connection.add_index TABLE_NAME, [ :store_id, :file_id, :chunk_index ], unique: true, + name: "index_vector_store_chunks_on_store_file_chunk", if_not_exists: true + @schema_ensured = true + rescue StandardError => e + raise VectorStore::Error, "pgvector store unavailable: #{e.message}" + end + def connection ActiveRecord::Base.connection end diff --git a/app/models/vector_store/registry.rb b/app/models/vector_store/registry.rb index 10c73d770..fcc470661 100644 --- a/app/models/vector_store/registry.rb +++ b/app/models/vector_store/registry.rb @@ -7,8 +7,10 @@ class VectorStore::Registry class << self # Returns the configured adapter instance. - # Reads from VECTOR_STORE_PROVIDER env var, falling back to :openai - # when OpenAI credentials are present. + # Reads from VECTOR_STORE_PROVIDER env var; without an explicit override, + # Anthropic installs (Setting.llm_provider == "anthropic") default to + # :pgvector, and anything else falls back to :openai when OpenAI + # credentials are present. def adapter name = adapter_name return nil unless name @@ -24,10 +26,27 @@ class VectorStore::Registry explicit = ENV["VECTOR_STORE_PROVIDER"].presence return explicit.to_sym if explicit && ADAPTERS.key?(explicit.to_sym) - # Default: use OpenAI when credentials are available + # Default routing: + # - When the configured LLM provider is Anthropic (which has no hosted + # vector store), fall back to the local pgvector adapter. The + # Embeddable concern still pulls embeddings from EMBEDDING_URI_BASE / + # OPENAI_ACCESS_TOKEN — Anthropic users typically point this at + # Voyage AI, a local Ollama instance, or OpenAI embeddings. + # - Otherwise, use OpenAI when credentials are available. + return :pgvector if Setting.llm_provider == "anthropic" :openai if openai_access_token.present? end + # True when pgvector is the effective vector store — whether set explicitly + # via VECTOR_STORE_PROVIDER or selected by the Anthropic default above. + # Single source of truth shared with the migration that provisions + # `vector_store_chunks`, so the table is created exactly when pgvector is in + # use (an Anthropic-default install would otherwise skip it and fail on the + # missing table). + def pgvector_effective? + adapter_name == :pgvector + end + private def build_adapter(name) @@ -53,6 +72,12 @@ class VectorStore::Registry end def build_pgvector + # Gate on availability (extension present, or table already created) + # so an Anthropic-default install on a Postgres without pgvector + # degrades to the assistant's "provider_not_configured" message + # instead of raising raw PG errors mid-chat. + return nil unless VectorStore::Pgvector.available? + VectorStore::Pgvector.new end diff --git a/db/migrate/20260316120000_create_vector_store_chunks.rb b/db/migrate/20260316120000_create_vector_store_chunks.rb index 216768486..c65e33c1a 100644 --- a/db/migrate/20260316120000_create_vector_store_chunks.rb +++ b/db/migrate/20260316120000_create_vector_store_chunks.rb @@ -28,14 +28,20 @@ class CreateVectorStoreChunks < ActiveRecord::Migration[7.2] private - # Only run this migration when pgvector is explicitly configured as the - # vector store provider AND the extension is actually available on the - # PostgreSQL server. Previously we only checked server availability, - # which caused failures in production Docker environments where the - # extension may be present but the DB user lacks superuser privileges + # Only run this migration when pgvector is the effective vector store AND + # the extension is actually available on the PostgreSQL server. + # + # Provider selection goes through VectorStore::Registry.pgvector_effective? + # (the single source of truth) rather than a raw VECTOR_STORE_PROVIDER check, + # so an Anthropic-default install — which selects pgvector implicitly via + # Setting.llm_provider without setting VECTOR_STORE_PROVIDER — still + # provisions the table instead of failing later on a missing relation. + # + # The server-availability check stays: production Docker environments may + # have the extension present but the DB user may lack superuser privileges # to enable it. def pgvector_available? - return false unless ENV["VECTOR_STORE_PROVIDER"].to_s.downcase == "pgvector" + return false unless VectorStore::Registry.pgvector_effective? result = ActiveRecord::Base.connection.execute( "SELECT 1 FROM pg_available_extensions WHERE name = 'vector' LIMIT 1" diff --git a/db/migrate/20260601120000_ensure_vector_store_chunks_for_default_pgvector.rb b/db/migrate/20260601120000_ensure_vector_store_chunks_for_default_pgvector.rb new file mode 100644 index 000000000..452717d7b --- /dev/null +++ b/db/migrate/20260601120000_ensure_vector_store_chunks_for_default_pgvector.rb @@ -0,0 +1,55 @@ +class EnsureVectorStoreChunksForDefaultPgvector < ActiveRecord::Migration[7.2] + # CreateVectorStoreChunks only provisions the table when + # VECTOR_STORE_PROVIDER == "pgvector" is set explicitly. Since #1986 makes + # pgvector the *default* vector store for Anthropic installs (no + # VECTOR_STORE_PROVIDER needed), a fresh Anthropic-only install would migrate + # without the table and then fail on uploads/searches. Backfill it whenever + # pgvector is the effective store, idempotently, so fresh and already-migrated + # installs converge. Gating uses the same VectorStore::Registry predicate as + # the runtime adapter selection, so the two can't drift again. + def up + return unless pgvector_effective? + return unless pgvector_extension_available? + return if table_exists?(:vector_store_chunks) + + enable_extension "vector" unless extension_enabled?("vector") + + create_table :vector_store_chunks, id: :uuid do |t| + t.string :store_id, null: false + t.string :file_id, null: false + t.string :filename + t.integer :chunk_index, null: false, default: 0 + t.text :content, null: false + t.column :embedding, "vector(#{ENV.fetch('EMBEDDING_DIMENSIONS', '1024')})", null: false + t.jsonb :metadata, null: false, default: {} + t.timestamps null: false + end + + add_index :vector_store_chunks, :store_id + add_index :vector_store_chunks, :file_id + add_index :vector_store_chunks, [ :store_id, :file_id, :chunk_index ], unique: true, + name: "index_vector_store_chunks_on_store_file_chunk" + end + + def down + # No-op: the table's lifecycle is owned by CreateVectorStoreChunks. This + # migration only backfills it for the pgvector-by-default case, so reverting + # must not drop a table other installs rely on. + end + + private + + def pgvector_effective? + VectorStore::Registry.pgvector_effective? + rescue StandardError + false + end + + def pgvector_extension_available? + ActiveRecord::Base.connection.execute( + "SELECT 1 FROM pg_available_extensions WHERE name = 'vector' LIMIT 1" + ).any? + rescue StandardError + false + end +end diff --git a/db/schema.rb b/db/schema.rb index aead1cb06..a300b338c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_05_31_213000) do +ActiveRecord::Schema[7.2].define(version: 2026_06_01_120000) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" diff --git a/test/models/vector_store/pgvector_test.rb b/test/models/vector_store/pgvector_test.rb index e0393f139..64212861e 100644 --- a/test/models/vector_store/pgvector_test.rb +++ b/test/models/vector_store/pgvector_test.rb @@ -3,6 +3,9 @@ require "test_helper" class VectorStore::PgvectorTest < ActiveSupport::TestCase setup do @adapter = VectorStore::Pgvector.new + # Schema provisioning is exercised by its own tests below; the operation + # tests stub it out so their mock connections only see the op's SQL. + @adapter.stubs(:ensure_schema!) end test "create_store returns a UUID" do @@ -138,4 +141,69 @@ class VectorStore::PgvectorTest < ActiveSupport::TestCase assert_not_includes @adapter.supported_extensions, ".zip" assert_not_includes @adapter.supported_extensions, ".docx" end + + test "ensure_schema! is a no-op when the table already exists" do + adapter = VectorStore::Pgvector.new + + mock_conn = mock("connection") + mock_conn.expects(:table_exists?).with(VectorStore::Pgvector::TABLE_NAME).returns(true) + mock_conn.expects(:create_table).never + adapter.stubs(:connection).returns(mock_conn) + + adapter.send(:ensure_schema!) + # Memoized: a second call must not hit the connection again. + adapter.send(:ensure_schema!) + end + + test "ensure_schema! provisions extension, table, and indexes when missing" do + adapter = VectorStore::Pgvector.new + + mock_conn = mock("connection") + mock_conn.expects(:table_exists?).with(VectorStore::Pgvector::TABLE_NAME).returns(false) + mock_conn.expects(:extension_enabled?).with("vector").returns(false) + mock_conn.expects(:enable_extension).with("vector") + mock_conn.expects(:create_table).with(VectorStore::Pgvector::TABLE_NAME, id: :uuid, if_not_exists: true) + mock_conn.expects(:add_index).times(3) + adapter.stubs(:connection).returns(mock_conn) + + adapter.send(:ensure_schema!) + end + + test "ensure_schema! wraps provisioning failures in VectorStore::Error" do + adapter = VectorStore::Pgvector.new + + mock_conn = mock("connection") + mock_conn.expects(:table_exists?).returns(false) + mock_conn.expects(:extension_enabled?).raises(ActiveRecord::StatementInvalid.new("permission denied")) + adapter.stubs(:connection).returns(mock_conn) + + error = assert_raises(VectorStore::Error) { adapter.send(:ensure_schema!) } + assert_match(/pgvector store unavailable/, error.message) + end + + test "available? is true when the chunks table exists" do + conn = mock("connection") + conn.expects(:table_exists?).with(VectorStore::Pgvector::TABLE_NAME).returns(true) + ActiveRecord::Base.stubs(:connection).returns(conn) + + assert VectorStore::Pgvector.available? + end + + test "available? is true when the extension is available but the table is missing" do + conn = mock("connection") + conn.expects(:table_exists?).returns(false) + conn.expects(:select_value).returns(1) + ActiveRecord::Base.stubs(:connection).returns(conn) + + assert VectorStore::Pgvector.available? + end + + test "available? is false when neither table nor extension is available" do + conn = mock("connection") + conn.expects(:table_exists?).returns(false) + conn.expects(:select_value).returns(nil) + ActiveRecord::Base.stubs(:connection).returns(conn) + + assert_not VectorStore::Pgvector.available? + end end diff --git a/test/models/vector_store/registry_test.rb b/test/models/vector_store/registry_test.rb index 514b30b24..0bd544ed0 100644 --- a/test/models/vector_store/registry_test.rb +++ b/test/models/vector_store/registry_test.rb @@ -43,13 +43,62 @@ class VectorStore::RegistryTest < ActiveSupport::TestCase end end - test "adapter returns VectorStore::Pgvector instance when pgvector configured" do + test "adapter returns VectorStore::Pgvector instance when pgvector configured and available" do + VectorStore::Pgvector.stubs(:available?).returns(true) ClimateControl.modify(VECTOR_STORE_PROVIDER: "pgvector") do adapter = VectorStore::Registry.adapter assert_instance_of VectorStore::Pgvector, adapter end end + test "adapter is nil when pgvector is selected but unavailable" do + VectorStore::Pgvector.stubs(:available?).returns(false) + ClimateControl.modify(VECTOR_STORE_PROVIDER: "pgvector") do + assert_nil VectorStore::Registry.adapter + assert_not VectorStore.configured? + end + end + + test "adapter is nil for the anthropic default when pgvector is unavailable" do + Setting.stubs(:llm_provider).returns("anthropic") + VectorStore::Pgvector.stubs(:available?).returns(false) + VectorStore::Registry.stubs(:openai_access_token).returns(nil) + ClimateControl.modify(VECTOR_STORE_PROVIDER: nil) do + assert_nil VectorStore::Registry.adapter + end + end + + test "adapter builds pgvector for the anthropic default when available" do + Setting.stubs(:llm_provider).returns("anthropic") + VectorStore::Pgvector.stubs(:available?).returns(true) + ClimateControl.modify(VECTOR_STORE_PROVIDER: nil) do + assert_instance_of VectorStore::Pgvector, VectorStore::Registry.adapter + end + end + + test "adapter_name defaults to pgvector when LLM_PROVIDER is anthropic" do + Setting.stubs(:llm_provider).returns("anthropic") + VectorStore::Registry.stubs(:openai_access_token).returns(nil) + ClimateControl.modify(VECTOR_STORE_PROVIDER: nil) do + assert_equal :pgvector, VectorStore::Registry.adapter_name + end + end + + test "adapter_name routes anthropic installs to pgvector even when OpenAI key is present" do + Setting.stubs(:llm_provider).returns("anthropic") + VectorStore::Registry.stubs(:openai_access_token).returns("sk-test") + ClimateControl.modify(VECTOR_STORE_PROVIDER: nil) do + assert_equal :pgvector, VectorStore::Registry.adapter_name + end + end + + test "explicit VECTOR_STORE_PROVIDER overrides anthropic default" do + Setting.stubs(:llm_provider).returns("anthropic") + ClimateControl.modify(VECTOR_STORE_PROVIDER: "qdrant") do + assert_equal :qdrant, VectorStore::Registry.adapter_name + end + end + test "configured? delegates to adapter presence" do VectorStore::Registry.stubs(:adapter).returns(nil) assert_not VectorStore.configured? @@ -57,4 +106,26 @@ class VectorStore::RegistryTest < ActiveSupport::TestCase VectorStore::Registry.stubs(:adapter).returns(VectorStore::Openai.new(access_token: "sk-test")) assert VectorStore.configured? end + + test "pgvector_effective? is true when pgvector is explicit" do + ClimateControl.modify(VECTOR_STORE_PROVIDER: "pgvector") do + assert VectorStore::Registry.pgvector_effective? + end + end + + test "pgvector_effective? is true for the anthropic default (no explicit provider)" do + Setting.stubs(:llm_provider).returns("anthropic") + VectorStore::Registry.stubs(:openai_access_token).returns(nil) + ClimateControl.modify(VECTOR_STORE_PROVIDER: nil) do + assert VectorStore::Registry.pgvector_effective? + end + end + + test "pgvector_effective? is false for the openai default" do + Setting.stubs(:llm_provider).returns("openai") + VectorStore::Registry.stubs(:openai_access_token).returns("sk-test") + ClimateControl.modify(VECTOR_STORE_PROVIDER: nil) do + assert_not VectorStore::Registry.pgvector_effective? + end + end end