fix: remove pgvector dependency from AI knowledge retrieval

This commit is contained in:
2026-07-06 10:36:21 -07:00
parent 0d5093ac6c
commit f74b7f3ca0
9 changed files with 78 additions and 30 deletions

View File

@@ -350,7 +350,7 @@ GOOGLE_GENERATIVE_AI_API_KEY=...
ANTHROPIC_API_KEY=sk-ant-...
```
## Scenario: Elder AI Analysis MVP With LangChain And pgvector
## Scenario: Elder AI Analysis MVP With LangChain And PostgreSQL JSONB Embeddings
### 1. Scope / Trigger
@@ -369,7 +369,7 @@ ANTHROPIC_API_KEY=sk-ant-...
- `DELETE /api/ai/knowledge/[id]`
- DB:
- `ai_knowledge_entries`
- `ai_knowledge_chunks` with `vector(1536)`
- `ai_knowledge_chunks` with `jsonb` `embedding` arrays
- `elder_ai_analyses`
- Runtime env:
- `AI_API_KEY` required for generation and embedding.
@@ -394,6 +394,7 @@ ANTHROPIC_API_KEY=sk-ant-...
- `errorCategory` / `errorReason`: sanitized only for failed rows
- Failed rows must not store prompts, full resident context snapshots, API keys, or raw provider errors.
- Knowledge retrieval may return enabled platform knowledge and enabled active-organization knowledge only.
- Knowledge retrieval first filters enabled platform / active-organization chunks in SQL, then computes cosine similarity in `modules/ai/server/knowledge.ts`; this keeps the MVP deployable on plain PostgreSQL without the `vector` extension.
- Completed elder analysis `resultJson.citations` must be derived from citation IDs actually referenced by `keyFindings[].citationIds` and `recommendations[].citationIds`; do not append unused available citations.
### 4. Validation & Error Matrix
@@ -420,7 +421,7 @@ ANTHROPIC_API_KEY=sk-ant-...
### 6. Tests Required
- Permission seeding assertions for `ai:*` and `knowledge:*` default role grants.
- Knowledge retrieval tests for platform shared, own organization, other organization, and disabled entry filtering.
- Knowledge retrieval tests for platform shared, own organization, other organization, disabled entry filtering, and cosine-score ordering from JSONB embeddings.
- Analysis tests for missing config, schema validation failure, failed-history sanitization, and data-scope redaction.
- Analysis tests for unknown citation rejection and referenced-only citation persistence.
- Route tests for auth/permission failures and standard response shape.

View File

@@ -63,8 +63,8 @@ Tests should mock database/provider boundaries and assert observable contracts,
## Compatibility
No database migration is required for this hardening pass.
No new dependency is required.
Deployment hardening additionally changes the still-unapplied `0007` AI migration from pgvector to JSONB embeddings, so production PostgreSQL 18 Alpine can run the migration without a `vector` extension.
Existing persisted `resultJson.citations` remain readable; new analyses will have stricter citation lists.
## Rollback

View File

@@ -9,7 +9,8 @@
5. Add AI route tests for permission failures, invalid input, service failure propagation, success responses, and audit calls.
6. Improve knowledge UI and analysis dialog failure copy.
7. Clarify `ai:manage` permission description.
8. Run targeted AI tests, then project verification.
8. Align AI knowledge embedding storage to JSONB for production PostgreSQL without pgvector.
9. Run targeted AI tests, then project verification.
## Validation Commands

View File

@@ -6,13 +6,13 @@ Harden the existing elder AI analysis and knowledge retrieval MVP so the critica
## Background
The current MVP already implements LangChain-backed elder AI analysis, pgvector-backed knowledge retrieval, permission-scoped resident context, persisted analysis history, and knowledge management UI.
The current MVP now implements LangChain-backed elder AI analysis, PostgreSQL JSONB-backed knowledge retrieval, permission-scoped resident context, persisted analysis history, and knowledge management UI.
Current evidence from repository inspection:
- AI service files live under `modules/ai/`.
- API routes live under `app/api/ai/`.
- AI tables and vector extension are in `modules/core/server/schema.ts` and `drizzle/0007_purple_puff_adder.sql`.
- AI tables and JSONB embedding storage are in `modules/core/server/schema.ts` and `drizzle/0007_purple_puff_adder.sql`; the production target does not require PostgreSQL `vector` extension support.
- Existing project verification passes: `pnpm lint`, `pnpm type-check`, and `pnpm test`.
- No AI-specific test file currently covers schema validation, permission scoping, history redaction, knowledge organization isolation, disabled knowledge exclusion, or provider failure behavior.
- `normalizeCitations` currently appends allowed citations that the model did not necessarily cite, weakening the evidence chain.