Skip to content

Skalierungs-Add-ons: dedizierter Worker + optionaler HNSW-ANN-Index - #17

Merged
veniplex merged 3 commits into
mainfrom
claude/ai-module-large-data-processing-oueat5
Jul 16, 2026
Merged

veniplex merged 3 commits into
mainfrom
claude/ai-module-large-data-processing-oueat5

Conversation

@veniplex

Copy link
Copy Markdown
Owner

Überblick

Zwei bewusst zurückgestellte Skalierungs-Bausteine aus dem GB-Modul-Vorhaben, jetzt als eigenständige, additive und opt-in Änderungen. Der Default-Pfad bleibt unverändert — ohne neue Env-Variablen/Admin-Aktionen verhält sich die App exakt wie bisher.

Baut auf dem bereits gemergten PR #14 auf (Phasen 0–3-Kern) und ist auf den aktuellen main-Stand gemerged (enthält #15/#16).

Enthaltene Änderungen

A — Dedizierter Worker-Prozess (feat(jobs))

Lange Jobs (Embedding, Zusammenfassung, Generierung) können optional außerhalb des Web-Tiers laufen, damit der Next-Server reaktiv bleibt.

  • src/lib/jobs/index.ts: start() aufgeteilt in startClient() (immer: boss.start + createQueue + schedule, damit send() funktioniert) und registerWorkers(boss) (die boss.work(...)).
  • Neues Env WORKERS_IN_PROCESS (Default true). Bei false registriert der Next-Server keine Worker mehr, sondern reiht nur ein.
  • Neuer Entrypoint src/worker.ts (Client + registerWorkers + Alive-Loop + SIGTERM-Handling), npm-Script worker. src/instrumentation.ts respektiert das Flag.
  • docker-compose.yml: dokumentierter optionaler zweiter Service. pg-boss verteilt Jobs automatisch über konkurrierende Worker — kein Doppel-Processing.

B — Optionaler pgvector HNSW-ANN-Index (feat(retrieval))

Schnelle Vektorsuche statt sequenziellem Cosine-Scan bei GB-Skala. Benötigt eine fixierte Dimension, deshalb eine typisierte Schattenspalte neben der bewusst dimensionslosen Basisspalte.

  • src/lib/ai/ann.ts (neu): annDimensionFor, populateAnn, reindexVectors — baut per Admin-Aktion im Hintergrund eine embedding_hnsw vector(N)-Spalte + HNSW-Index (vector_cosine_ops) für das aktive Embedding-Modell, Backfill aus embedding (Source of Truth). Dimension wird aus den Daten abgeleitet, nicht geraten.
  • src/lib/settings.ts: neuer Setting-Key ai.ann (Status idle/building/ready/failed, Modell, Dimension, Fehler).
  • src/app/[locale]/(app)/admin/actions.ts: startVectorReindex() + getAnnStatus().
  • src/components/admin/ann-index-card.tsx + admin/ai/page.tsx: Admin-UI mit Status und Rebuild-Button (pollt während des Builds).
  • src/lib/ai/rag.ts: hybridSearch nutzt den HNSW-Index nur, wenn annDimensionFor eine Dimension liefert — sonst weiter der dimensionslose Scan (Default). populateAnn füllt die Schattenspalte nach frischem Embedding.
  • src/lib/jobs/index.ts: reindex-vectors-Queue/Worker.

Guards: Alles ist best-effort und additiv. Ein Fehler beim ANN-Aufbau/-Befüllen bricht weder Ingestion noch Suche ab; ohne aufgebauten Index bleibt der bisherige Scan aktiv. Modell-/Dimensionswechsel wird durch sauberen Rebuild abgefangen.

Verifikation

  • npm run typecheck — 0 Fehler
  • npm run lint — sauber
  • npm run test — 81 bestanden (3 skipped)
  • npm run build — erfolgreich
  • Default-Pfad (Flags aus, kein ANN-Rebuild) unverändert.

Hinweise zum produktiven Test

  • A: zweiten Prozess mit npm run worker (bzw. Compose-Service) starten und WORKERS_IN_PROCESS=false im Web-Tier setzen → Verarbeitung läuft im Worker, Web-Tier bleibt reaktiv.
  • B: als Admin unter Admin → KI den Index-Rebuild starten; EXPLAIN zeigt danach HNSW-Nutzung, Suchlatenz vor/nach vergleichbar.

Noch offen (Folge-PRs)

S3-Storage-Treiber (C), Batch-API-Adapter (D), tus-Resumable-Uploads (E) — jeweils als eigener additiver PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_0191JikNRN8Q2HBtf6fpLXmH


Generated by Claude Code

claude added 3 commits July 15, 2026 19:35
Splits pg-boss setup into startClient() (connect + queues + schedules,
run everywhere) and registerWorkers() (the job handlers). The web tier
registers workers in-process by default; set WORKERS_IN_PROCESS=false to
have it only enqueue and run one or more standalone workers instead
(src/worker.ts, `npm run worker`) so long extraction/embedding/generation
jobs don't compete with request handling. pg-boss distributes jobs across
all connected workers. Adds tsx devDependency and a commented worker
service in docker-compose. Default behaviour unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191JikNRN8Q2HBtf6fpLXmH
Adds a fast approximate-nearest-neighbour vector index for large corpora,
opt-in and off by default (the dimensionless sequential scan stays the
default, unchanged).

- src/lib/ai/ann.ts: builds a typed shadow column embedding_hnsw vector(N)
  + HNSW index for the active embedding model, dimension inferred from
  stored data; rebuild handles model/dimension changes cleanly. State
  tracked in a new non-secret `ai.ann` setting.
- Runs as a background job (reindex-vectors queue) triggered by an admin
  action (startVectorReindex); admin AI page shows status + a build button
  (AnnIndexCard), i18n de/en.
- hybridSearch uses the ANN column when ready for the active model, else the
  scan; new material/summary chunks keep the ANN column current via
  populateAnn. All raw SQL is additive and guarded — failure marks the index
  failed and search falls back to the scan.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191JikNRN8Q2HBtf6fpLXmH
@veniplex veniplex added the release-candidate Creates a new release and deployment of docker image, once merged. label Jul 16, 2026
@veniplex
veniplex merged commit e58b1db into main Jul 16, 2026
1 of 2 checks passed
@veniplex
veniplex deleted the claude/ai-module-large-data-processing-oueat5 branch July 16, 2026 14:26
github-actions Bot added a commit that referenced this pull request Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-candidate Creates a new release and deployment of docker image, once merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants