dify/knowledge-fs
Jyong b79b8a879b feat(knowledge-fs): record and count every knowledge space query
Workflow KnowledgeFS retrieval nodes ran through `retrieval-tests`, which
never wrote an AnswerTrace or an overview event, so their queries were
missing from the retrieval history and from the space overview; the history
also only ever showed each member their own traces.

- AnswerTrace gains a `source` (retrieval_test | workflow | service_api |
  agent | mcp; migration 0051_answer_trace_source) derived from the
  Capability v2 caller kind. The retrieval-tests route records one trace per
  run (stages, evidence bundle, profile metadata), returns its id as
  `answerTraceId`, and the workflow node's failed-retrieval capture attaches
  to that trace instead of creating a second record.
- The quality trace list exposes and filters by `source`; traces from other
  caller kinds are visible to any current reader of the space, and counts and
  scores fall back to the evidence embedded in the trace when no bundle row
  exists.
- Overview accounting: retrieval-tests and Research tasks now emit
  `query.requested`, and the Research job state machine emits
  `query.completed` / `query.failed` on terminal stages (wired for both the
  in-process gateway and the durable runtime), so query volume, answer rate
  and outcomes include every caller. Activity details keep `source` and
  `taskKind`.
- Console and service trace routes accept a `source` filter; the retrieval
  test page shows a source badge and an all / retrieval tests / workflow
  filter, with translations for every locale.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015zw5G5SX3HmVfnZof6YWAc
2026-09-04 05:48:17 -04:00
..
.codex chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
.github/workflows chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
.harness fix(knowledge-fs): track workflow retrieval outcomes 2026-09-04 16:50:09 +08:00
apps feat(knowledge-fs): record and count every knowledge space query 2026-09-04 05:48:17 -04:00
contracts refactor(knowledge_fs): replace credential service with dataset API key authorization. Remove the API token feature at the space level for knowledgeFs. 2026-08-26 15:40:18 +08:00
design/productized-knowledgefs chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
docs feat(knowledge-fs): add profile-driven multimodal retrieval 2026-09-02 01:01:57 -04:00
infra fix(knowledge-fs): harden research retrieval execution 2026-09-01 13:26:43 -04:00
packages feat(knowledge-fs): record and count every knowledge space query 2026-09-04 05:48:17 -04:00
scripts feat(knowledge-fs): fix CI. 2026-09-04 16:13:13 +08:00
test Refactor codebase and remove obsolete implementations 2026-07-23 01:53:40 -04:00
tools/swagger chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
.dockerignore chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
.gitignore Expand application capabilities and refactor core workflows 2026-07-22 05:09:09 -04:00
biome.json chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
main.py chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
package.json fix(knowledge-fs): upgrade vulnerable fast-uri 2026-09-03 11:33:45 +08:00
pnpm-lock.yaml fix(knowledge-fs): upgrade vulnerable fast-uri 2026-09-03 11:33:45 +08:00
pnpm-workspace.yaml chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
README.md feat(knowledge-fs): harden PDF image materialization 2026-08-18 03:03:18 -04:00
secret-scan-allowlist.json fix(secret-scan): update line numbers for database schema identifiers in allowlist 2026-08-18 09:51:50 +08:00
tsconfig.base.json chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
turbo.json chore: migrate knowledge-fs source tree 2026-07-20 04:54:20 -04:00
upstream-provenance.json Expand application capabilities and refactor core workflows 2026-07-22 05:09:09 -04:00

KnowledgeFS

KnowledgeFS is Dify's backend knowledge runtime. It provides tenant-scoped ingestion, parsing, indexing, retrieval, KnowledgeFS commands, MCP tools, durable jobs, traces, and evaluation APIs.

KnowledgeFS is not an independently deployable product. It must run with the Dify API:

  • Dify owns model and datasource plugin credentials.
  • Dify creates model and datasource plugin instances and performs plugin invocation.
  • Dify owns physical object storage through its configured STORAGE_TYPE.
  • KnowledgeFS reaches those capabilities only through the authenticated Dify inner API.
  • KNOWLEDGE_INTEGRATED_MODE_ENABLED is a Workspace rollout/cutover gate; it never selects a different runtime or credential owner.

Runtime architecture

Dify API
  ├─ model manager / plugin daemon
  ├─ datasource plugins
  ├─ unified object storage
  └─ authenticated inner API
          │
          ▼
KnowledgeFS API
  ├─ document compilation and retrieval
  ├─ KnowledgeFS / MCP command surfaces
  ├─ PostgreSQL repositories and durable jobs
  └─ optional Unstructured parser dependency

Main directories:

apps/api/                              KnowledgeFS backend entrypoint
packages/api/                          Hono gateway, repositories, retrieval, jobs, auth
packages/adapters/                     Database, Dify storage, cache, and queue adapters
packages/dify-model-runtime-client/    Bounded Dify model inner-API client
packages/dify-datasource-runtime-client/
                                       Bounded Dify datasource inner-API client
packages/core/                         Shared contracts and schemas
packages/database/                     Schema catalog and SQL migrations
packages/compute/                      Bounded pure TypeScript compute
packages/parsers/                      Native and Unstructured parser adapters
infra/local/                           Developer harness; requires a running Dify API
infra/kubernetes/                      Inert Dify integration baseline

The repository still contains reusable lower-level adapters and an optional local Admin test harness. They are development assets, not alternative production deployment modes.

Required production configuration

The canonical Dify Compose service loads docker/envs/core-services/knowledge-fs.env.example. Operator-owned inputs are limited to:

  • DATABASE_URL
  • DIFY_INNER_API_URL and DIFY_INNER_API_KEY, injected by Dify Compose
  • UNSTRUCTURED_API_URL and optional UNSTRUCTURED_API_KEY
  • bounded Poppler PDF image rasterization settings (the API image includes pdftoppm)
  • KnowledgeFS capability/JWKS and document-compilation rollout settings

Do not configure storage-provider credentials, model-provider keys, datasource credentials, or a direct Plugin Daemon endpoint in KnowledgeFS. The Dify inner key must match INNER_API_KEY_FOR_PLUGIN.

See production deployment and the operator manual.

Development

Prerequisites:

  • Node.js 22+
  • pnpm 10.33.0 through Corepack
  • Docker
  • A reachable Dify API

Install dependencies:

corepack enable
pnpm install
cp infra/local/.env.example infra/local/.env

Set DIFY_INNER_API_URL and DIFY_INNER_API_KEY in the ignored local env, then start the local database and parser:

pnpm dev:infra
pnpm local:db:migrate

Run the backend from source:

pnpm dev:api

For the optional local Admin test harness:

pnpm --filter @knowledge/admin dev

Run the bounded local smoke after Dify and the local processes are available:

pnpm local:happy-path

It validates health, workspace bootstrap, Markdown upload, parse artifacts, and query evidence. Use LOCAL_SMOKE_ADMIN_BASE when the Admin harness is not on its default port. Other useful forms:

LOCAL_SMOKE_RUN_MIGRATIONS=1 pnpm local:happy-path
pnpm local:happy-path:durable
pnpm local:happy-path:api

The durable smoke requires database health and Dify-backed object-storage health. The API-only smoke skips the Admin BFF.

See the local developer guide for details.

Validation

pnpm typecheck
pnpm test
pnpm lint:backend
pnpm openapi:export:test
pnpm db:migrations:check
pnpm compose:middleware:test
pnpm compose:apps:test
pnpm dify:compose:config
git diff --check

Build the backend production bundle or image:

pnpm --filter @knowledge/api-app build:prod
pnpm docker:api:build
pnpm docker:api:bundle-smoke

The isolated image smoke proves the bundle can boot and remains unhealthy while Dify is absent; a Dify Compose/Kubernetes smoke is required to validate the real inner API, storage, database, models, and datasources.

API and design references