Knowledge Base — implementation plan
Status: Draft
Last updated: 2026-05-21
Audience: Engineers planning or executing the Knowledge Base build.
This plan is the engineering-execution complement to spec.md. The spec is the what; this is the how and when. Phasing matches the canonical "phased delivery" section of the spec.
Tracking
- JIRA Epic:
EW-XXX— Knowledge Base v1 (link to be filled in once the Epic exists). - Child tickets: one per phase below, with sub-tasks per deliverable.
- Spec source of truth: spec.md.
- Decision log: spec.md §26.
Phase 1 — foundation (target: first reviewable PR)
The smallest credible vertical slice that proves the data model, the storage abstraction, and the API contract. No UI in this phase beyond a CRUD-bound API.
Deliverables
- Entities in
packages/agent/src/entities/:work-knowledge-document.entity.tswork-knowledge-upload.entity.tswork-knowledge-tag.entity.tswork-knowledge-citation.entity.tswork-knowledge-chunk.entity.ts(composite PK on(workId, id)— see spec §15.2)
- Migrations under
apps/api/src/migrations/({unix-millis}-{PascalCase}.ts):EnablePgvectorExtension—CREATE EXTENSION IF NOT EXISTS vector;CreateWorkKnowledgeDocuments— including thework_id XOR organization_idCHECK constraintCreateWorkKnowledgeUploadsCreateWorkKnowledgeTagsCreateWorkKnowledgeCitationsCreateWorkKnowledgeChunks— composite PK + ivfflat index on(workId, embedding)AddWorkKbConfigColumn—kb_configsimple-json onworks
- DTOs in
packages/contracts/src/kb/—KbDocumentDto,KbDocumentBodyDto,KbUploadDto,KbSearchHit,KbTreeNode,CitationDto,KbDocumentClassunion. - Service skeleton
packages/agent/src/services/knowledge-base.service.ts:- Constructor wiring (repositories, storage plugin resolver, content-extractor resolver, Git provider).
- Method stubs with JSDoc:
resolveDocuments,createDocument,updateDocument,deleteDocument,ingestUpload,search,rebuildIndex,resolveContext. - No method bodies beyond
throw new NotImplementedException()— bodies land in subsequent PRs.
- REST endpoints in
apps/api/src/works/(the existingWorksControllerper the spec §12 routing convention):GET /api/works/:id/kb/documents+POST /api/works/:id/kb/documentsGET/PATCH/DELETE /api/works/:id/kb/documents/:docId- Lock / unlock / restore endpoints
- Upload endpoints (placeholder — accept multipart but persist only the row, not yet extract)
- Tree, search (lexical-only), tags
- All wired with the existing
WorkMemberGuardand@ApiBearerAuthdecorators
- Tests:
- Entity unit tests covering the
workId XOR organizationIdCHECK constraint, the indexed columns, the cascade rules. - Service unit tests for
resolveContextbudget truncation logic (mocking the repositories). - One API e2e test covering create → list → update → delete on a document.
- Entity unit tests covering the
- Backfill job in
packages/tasks/:- Iterates existing Works, initializes empty
.content/kb/structure in each data repo (idempotent).
- Iterates existing Works, initializes empty
- Documentation:
- Append a "Knowledge Base entities" section to
docs/specs/architecture/database.mdlisting the new entities + the inheritance pattern + the pgvector dependency. - Append a dated entry to whatever the platform's change-log convention is (mirror existing PR entries in
docs/).
- Append a "Knowledge Base entities" section to
Out of Phase 1
- Embedding generation (Phase 2).
- Workbench UI (deferred to a separate web-only PR within Phase 1 — see Phase 1B).
- Extraction pipeline (Phase 1B).
- Org-level inheritance UI (Phase 2).
- Agent runtime integration (Phase 2).
Phase 1B (still in foundation, second PR)
- Workbench MVP: tree + Tiptap editor + drag-and-drop upload + PDF / DOCX / Markdown extraction.
- Ingest pipeline (receive → normalize → extract → materialize → index), as Trigger.dev jobs in
packages/tasks/. - Per-document lock UI.
- Activity log integration.
Phase 2 — agent integration + retrieval
Builds on Phase 1's data model.
Deliverables
- Embedding generation via platform-managed embedding lane.
- Add
embed(input)to AI provider plugin contract inpackages/plugin/. - Implement for
openaiplugin (defaulttext-embedding-3-small). - Trigger.dev job in
packages/tasks/to chunk + embed on document insert / update. - Hybrid chunker (heading-aware + fixed-size fallback).
- Add
- Semantic retrieval wired into
KnowledgeBaseService.search(RRF blend of lexical + semantic). KbPromptFormatterinpackages/agent/src/services/kb-prompt-formatter.ts— produces the standardized context block injected into every pipeline.- Pipeline integration — every pipeline in
packages/plugins/{standard-pipeline,agent-pipeline,claude-code,…}consumesKbContextBundlevia the orchestrator. - AI conversation
@kbmentions + citation rendering. - Agent tools —
kb_search,kb_read,kb_write,kb_lockexposed to agent-pipelines via the existing tool-registration surface. - Org-level legal / style / seo inheritance — entity-level support exists in Phase 1; this phase implements the fan-out Trigger.dev job +
.org/{class}/materialization in the Work's data repo + admin endpoints. - Community-PR and scheduled-regen lock respect —
CommunityPrServiceandScheduledUpdateServiceconsultWorkKnowledgeDocument.lockedandlockModebefore mutating any KB doc. - Tests:
- Service tests for hybrid chunking against fixture documents.
- Eval suite for retrieval-quality regression (lexical-only baseline vs. blended).
- E2E test for an org-level legal doc being inherited and overridden by a Work.
Phase 3 — polish
- Video / audio normalization via ffmpeg (Trigger.dev job).
- Transcription via AI provider
transcribe(file)capability (added to plugin contract, implemented inopenaiandanthropicplugins). - Embedded-app outputs — agents can write
index.html+ assets folders as KB documents; workbench renders them in a sandboxed iframe. output-class agent writes — agents can promote their conversation responses into the KB with full provenance tracking.- MCP exposure —
apps/mcpgains akbnamespace (list / read / search / create / update / upload). - CLI exposure —
apps/cligainsever works kb {list,show,upload,edit,search,sync}commands. - Wikilink resolver + rename rewriter — when a document is renamed or moved, all
[[wikilinks]]and relative-path references across the KB are updated automatically. - Reconciliation job — daily sweep detecting Git ↔ DB drift, surfacing
kb.lock.violationand orphan-storage cases in the workbench. - Full telemetry event set per spec §23.
Dependencies + risk
- pgvector: must be enabled on every platform Postgres deployment before Phase 1 migrations run. Coordinate with the platform-infra deploy runbook.
- OAuth token scope for
github-storage: KB upload commits go through the samegithub-storageplugin path that already commits items. No new token scope needed. - Trigger.dev workers: ingest pipeline + embedding generation + sweeper will add real load. Confirm worker count + concurrency limits before Phase 2.
- Embedding cost: default
text-embedding-3-smallis ~$0.02/M tokens — modest, but at scale (large org with many Works) becomes a real line item. The budget-system integration in §19.3 of the spec must land in Phase 1 so usage is observable from day one.
Definition of done per phase
Each phase ends with:
- All deliverables landed in
develop. - PR review loop completed per workspace NN #14 / #18.
- CI green per NN #19.
- Acceptance criteria from acceptance.md for that phase verified manually + by CI.
- JIRA child ticket transitioned to Done.