Skip to content
Draft
28 changes: 28 additions & 0 deletions .changeset/38369-knowledge-held-without-embedder-or-store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'@objectstack/service-knowledge': minor
'@objectstack/core': patch
'@objectstack/mcp': patch
'@objectstack/spec': patch
---

feat(service-knowledge)!: a knowledge service the runtime mounts holds its sources without an embedder or its deployment store

Clause-②: no (narrowing)
Metadata-protocol: narrowed

<!-- adr-0087: registered knowledge-service-held-without-embedder-or-store -->

**BREAKING** for a self-hosted deployment whose knowledge service the runtime mounts (the stack declares `requires: ['knowledge']` or ships a `knowledgeSources` entry) and which, at boot, has no embedder or no deployment knowledge store mounted. Such a deployment used to start its boot build, record sync and reconcile job, list `search_knowledge` on MCP, and answer every build and search with an error from a stand-in embedder; a source on a store the application registered was built and searched with no embedder saved. Mounted is not enabled now (ADR-0161 K8, the dated note on item 12): the service is enabled by configuration, read once at `kernel:bootstrapped`, and without both halves every source is held, whichever store it names.

- **`@objectstack/service-knowledge`.** `KnowledgeServicePlugin` takes `runtimeComposed: { declared }`. When set, the plugin reads the embedder and the deployment store at `kernel:bootstrapped`; without both, `KnowledgeService` holds every source: loaded and listed, never built, synced, reconciled or searched. It hands out no `search_knowledge` definition while the reading is pending or held; `search`, `rebuildSource` and `reindexSource` answer `503` with code `SERVICE_UNAVAILABLE` and a message naming the missing half and the fix; a dropped source keeps its index and no store is called. No lifecycle reap guard registers until the reading is enabled, so a retention sweep deletes rows on time. The boot logs one line: info when only a shipped source mounted the service and nothing was saved, warn when the stack declares `requires: ['knowledge']`, a saved embedder provider builds nothing (a provider saved without its key), or the store is missing. Never a boot refusal.
- **`@objectstack/core`.** The capability composition passes `runtimeComposed: { declared }` with the knowledge service's deployment adapter; `declared` is true only when the stack lists `knowledge` in `requires`.
- **`@objectstack/mcp`.** The stdio transport registers `search_knowledge` at `kernel:listening`, after the reading, only when the service hands out its definition; clients are told by `notifications/tools/list_changed`. A held service is never listed on either transport.
- **`@objectstack/spec`.** The upgrade guide carries the `knowledge-service-held-without-embedder-or-store` entry.

Unchanged: a knowledge service a host mounts itself (`KnowledgeServicePlugin` in `plugins[]`, no `runtimeComposed`) boots, builds and serves as before; a deployment with both halves builds, syncs and serves as before; stored sources are not rewritten or refused.

## FROM → TO

- A deployment with knowledge sources and no embedder → save an embedder provider on the AI settings page (or register an `embedder` service from a plugin), then restart.
- A deployment whose boot says its knowledge store is not mounted → mount that store, then restart.
- An application whose own store embeds without the kernel's embedder → mount `KnowledgeServicePlugin` itself in `plugins[]`.
5 changes: 3 additions & 2 deletions content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Which store is mounted depends on the platform database:
- **Two stores and none the deployment's** (a host that mounts the service and two adapters itself): a source that names no adapter is refused when it is registered or saved — `VALIDATION_ERROR` (400), naming the adapters to choose from.
- **A source naming a store that is not mounted** (a retired store such as `ragflow`, or a typo) is refused when it is saved in Studio, over `PUT /meta/knowledge_source` or through MCP: `VALIDATION_ERROR` (400), naming the source, the adapter and the stores that are mounted. A source that leaves `adapter` out is not refused for it. That save check does not reach a package install, a source registered in code or `os validate`; such a source does not stop the boot: the server logs one error naming the source and the stores that are mounted, and indexing or searching that source answers `SERVICE_UNAVAILABLE` (503) with the same choices; `search_knowledge` lists it in `refused`.
- **The embedder** comes from Settings → AI & Embedder ([below](#configuring-the-embedder-from-settings)).
- **Mounted is not enabled.** A service the runtime mounts this way is enabled by configuration, read once when the server starts: an embedder (one a plugin registered, or a provider saved in Settings → AI & Embedder that builds one) and the deployment's store mounted. Without both, every source is **held**, whichever store it names: it is listed to administrators and never built, synced, reconciled or searched; `search_knowledge` is not listed on MCP (HTTP or stdio); a search or a rebuild answers `SERVICE_UNAVAILABLE` (503) naming what is missing; no reap guard is registered and no store is called, so a retention sweep deletes rows on time. The boot says it once: at `info` when only a shipped source mounted the service and nothing was saved, at `warn` when the stack declares `requires: ['knowledge']`, a saved provider builds no embedder (one saved without its key) or the store is not mounted. The server always starts. To enable it, save an embedder provider (or mount the store the log names), then restart. A service you mount yourself (below) is not held.
- **Another libSQL store** for the `turso` case: save its URL as `turso_url` (and `turso_auth_token` for a managed `libsql://` database) in Settings → Knowledge, then restart. A `file:` URL saved there must name a file inside that same data directory, and a URL carrying a NUL byte or another control character, written out or percent-encoded (`%00`), is refused, as is a token carrying one (a pasted line break counts); either way the store answers `SERVICE_UNAVAILABLE` saying why, and the boot goes on.
- **Where the file is**: `os start --home` and `OS_HOME` move it with the default database, so on a container put `OS_HOME` on a volume and the index survives a redeploy. A database named by URL (`OS_DATABASE_URL`) does not move it.
- **Settings → Knowledge** holds only what the service and its store read: `turso_url` and `turso_auth_token`, which `@objectstack/knowledge-turso` adds to the page where it is the store and reads them, with the "Test connection" check of those two fields (on PostgreSQL the page shows none of the three), and `chunk_target`, the longest passage the splitter cuts. The page names no adapter: a source's store is the one its own `adapter` names, and `os knowledge status` reports it per source. Every search re-checks each hit as the asking user, and no setting turns that off. A PostgreSQL deployment that saved the two Turso keys before the group left the page keeps their rows; the settings service never deletes a stored row. To tidy it up, delete the `knowledge` rows named `turso_url` and `turso_auth_token`, and the `sys_secret` row the `turso_auth_token` row's handle names: without the store no API door clears that key, and the row is a decryptable copy of the token. Nothing requires it.
Expand Down Expand Up @@ -195,7 +196,7 @@ An adapter that reads the scope filters before it ranks, so the `topK` hits all

## The `search_knowledge` tool

An AI client searches through one tool, `search_knowledge`, defined once by the knowledge service. The MCP door (HTTP and stdio) and the in-product agent serve that one definition unchanged, so every client is offered the same arguments and gets the same answer. It searches only sources whose `aiExposed` is not `false`, and reads as the person asking.
An AI client searches through one tool, `search_knowledge`, defined once by the knowledge service. The MCP door (HTTP and stdio) and the in-product agent serve that one definition unchanged, so every client is offered the same arguments and gets the same answer. It searches only sources whose `aiExposed` is not `false`, and reads as the person asking. Where the knowledge service is held (no embedder or no store, [above](#turning-it-on-requires-knowledge)), the tool is not listed at all; on stdio it is registered after the boot reads the service as enabled, and a connected client is told by `notifications/tools/list_changed`.

| Argument | What it does |
|:--|:--|
Expand Down Expand Up @@ -230,7 +231,7 @@ When `KnowledgeServicePlugin` is mounted beside the settings service, Settings
- **A provider is saved and complete** → an `@objectstack/embedder-openai` embedder for that provider. The saved Base URL applies only to the custom and Azure providers; other providers use their preset endpoint. The boot line names the endpoint host. "Complete" is what the **Test embedder** button checks: a provider, an API key (Ollama excepted) and, for a custom or Azure endpoint, a Base URL.
- **Dimensions and batch size are positive integers**, saved as numbers or set as text through an `OS_*` override (`OS_AI_EMBEDDER_DIMENSIONS=256`). A value that is not a positive integer is refused, with a `warn` at boot and the error below on every use — never silently ignored.
- **The batch size is passed only when you saved it.** Left unsaved (the form shows 64), the embedder sends as many inputs per request as the model's documented cap allows — 10 for DashScope `text-embedding-v3`, 2048 for OpenAI `text-embedding-3-*` — and 10 for a model it does not know. A saved batch size is held to the saved model's documented cap (`documentedMaxBatchSize(model)` from `@objectstack/embedder-openai`): a saved 64 on `text-embedding-v3` sends 10. For a model without a documented cap the saved size is sent as saved.
- **Nothing is configured** — no provider saved, `none` saved, or the saved settings incomplete → the server still boots, and every use of the embedder answers an error naming the missing settings: an adapter that embeds refuses indexing and search with it, and nothing is sent anywhere. Nothing is sent to a provider until you have saved one, and the API key is never logged or put in an error.
- **Nothing is configured** — no provider saved, `none` saved, or the saved settings incomplete → the server still boots, and every use of the embedder answers an error naming the missing settings: an adapter that embeds refuses indexing and search with it, and nothing is sent anywhere. Where the runtime mounted the service (`requires: ['knowledge']` or a shipped source), its sources are held instead ([above](#turning-it-on-requires-knowledge)): nothing is built or searched, and `search_knowledge` is not listed. Nothing is sent to a provider until you have saved one, and the API key is never logged or put in an error.

**A saved change takes effect after a restart** — the page says so under the provider field. There is no hot replacement.

Expand Down
16 changes: 15 additions & 1 deletion content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ the knowledge service and the deployment's store, exactly as
in `PLATFORM_CAPABILITY_PROVIDERS`). A host that mounts
`KnowledgeServicePlugin` itself keeps its own, and the runtime adds no store.

**Mounted is not enabled** (ADR-0161 K8, the dated note on item 12). A service
the runtime mounted (`runtimeComposed` on the plugin) reads once, at
`kernel:bootstrapped`, whether an embedder is available and whether the
deployment's store is mounted. Without both, every source is held, whichever
store it names: loaded and listed, never built, synced, reconciled or searched;
no `search_knowledge` definition is handed out; `search`, `rebuildSource` and
`reindexSource` answer `503 SERVICE_UNAVAILABLE` naming the missing half; a
removed source keeps its index, and no store is called. The boot logs one line,
`info` when only a shipped source mounted the service and nothing was saved,
`warn` otherwise, and never refuses to start. A change takes effect at the next
restart. A host-mounted service is not read this way and serves from the start.

#### How a source is loaded

The knowledge service reads the published `knowledge_source` items when it
Expand Down Expand Up @@ -1270,7 +1282,9 @@ handled at the source rather than after the fact
For every object a source projects, the plugin registers an ADR-0057
**reap guard** with the lifecycle service. The sweep hands the guard the
candidate rows before deleting them; the guard deletes each row's document and
returns the ids it confirms. Rows it does not confirm stay put. The guarded
returns the ids it confirms. Rows it does not confirm stay put. A service the runtime mounted registers
no guard until the boot reads it as enabled (above): a held service guards
nothing and calls no store, so the sweep deletes its rows on time. The guarded
set follows the sources: a source added after boot for an object with no
guard gets one when it is loaded, so a reaped row of that object is
de-indexed too.
Expand Down
26 changes: 23 additions & 3 deletions packages/core/src/capability-composition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,30 @@ describe("stackItemCapabilities — a stack's items mount their reader", () => {
for (const input of [pg, file]) {
const { argument } = resolveCapabilityArgument('knowledge', input);
const [store] = resolveCapabilityExtras('knowledge', input);
expect(argument).toEqual({ deploymentAdapter: resolveKnowledgeStore(input).store.adapterId });
expect(argument).toEqual({
deploymentAdapter: resolveKnowledgeStore(input).store.adapterId,
runtimeComposed: { declared: false },
});
expect(store?.pkg).toBe(resolveKnowledgeStore(input).store.pkg);
}
expect(resolveCapabilityArgument('knowledge', pg).argument).toEqual({ deploymentAdapter: 'pgvector' });
expect(resolveCapabilityArgument('knowledge', file).argument).toEqual({ deploymentAdapter: 'turso' });
expect(resolveCapabilityArgument('knowledge', pg).argument).toEqual({
deploymentAdapter: 'pgvector',
runtimeComposed: { declared: false },
});
expect(resolveCapabilityArgument('knowledge', file).argument).toEqual({
deploymentAdapter: 'turso',
runtimeComposed: { declared: false },
});
});

it('[ADR-0161 K8, note on item 12] marks the service runtime-composed, declared only when the stack lists the token', () => {
const database = { driver: 'sqlite', url: 'file:/app/.objectstack/data/objectstack.db' };
const argumentOf = (stack: unknown) =>
resolveCapabilityArgument('knowledge', { stack, packageRoot: '/app', database }).argument as {
runtimeComposed: { declared: boolean };
};
// A shipped `knowledge_source` mounts the service (an item token), and is no request for it.
expect(argumentOf({ knowledgeSources: [{ name: 'faq' }] }).runtimeComposed).toEqual({ declared: false });
expect(argumentOf({ requires: ['knowledge'] }).runtimeComposed).toEqual({ declared: true });
});
});
21 changes: 17 additions & 4 deletions packages/core/src/capability-composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import { PLATFORM_ALWAYS_ON_CAPABILITIES, PLATFORM_CAPABILITY_PROVIDERS } from '
import { readEnvWithDeprecation } from '@objectstack/types';
import { CAPABILITY_PROVIDERS, KNOWLEDGE_STORE_PROVIDERS } from './capability-providers.js';
import type { CapabilityIdentities, KnowledgeStoreSpec } from './capability-providers.js';
import { resolveStackCollection } from './stack-collections.js';
import { resolveStackCollection, stackDeclaredCapabilities } from './stack-collections.js';

type Bag = Record<string, unknown>;

Expand Down Expand Up @@ -256,13 +256,21 @@ function providerReader<A extends unknown[]>(
* and on a configuration the schema refuses, an inline secret included.
* - `storage`: the local adapter at {@link resolveStorageLocalRootEnv}'s root,
* else `.objectstack/data/uploads` ({@link resolveStorageCapabilityArg}).
* - `knowledge`: `{ deploymentAdapter }`, the adapter id of the store
* - `knowledge`: `{ deploymentAdapter, runtimeComposed }` — the adapter id of the store
* {@link resolveKnowledgeStore} chooses and {@link resolveCapabilityExtras}
* mounts beside the service — the deployment's-store mark (ADR-0161 K4): a
* source that names no adapter binds to it. A service option, not an
* adapter-contract member; the store itself is told nothing. Read from the
* same {@link CapabilityArgumentInput.database} as the store's choice, so
* the two agree.
* the two agree. Beside it, `runtimeComposed: { declared }`: the service
* was composed by this rule, not mounted by a host, so it is enabled by
* configuration (ADR-0161 K8, the dated note on item 12): at
* `kernel:bootstrapped` it serves only with an embedder and the
* deployment's store, and holds its sources otherwise. `declared` is
* whether the stack lists `requires: ['knowledge']`
* (`stackDeclaredCapabilities`), which raises the not-enabled boot line from
* `info` to `warn`; a stack that mounts the service only by shipping a
* `knowledge_source` leaves it `false`.
* - any other token: no argument.
*
* A throw is the boot's to report: whether it fails the boot or is logged is
Expand Down Expand Up @@ -295,7 +303,12 @@ export function resolveCapabilityArgument(token: string, input: CapabilityArgume
return { argument: storage.options, localStorageRoot: storage.localRoot };
}
if (token === 'knowledge') {
return { argument: { deploymentAdapter: resolveKnowledgeStore(input).store.adapterId } };
return {
argument: {
deploymentAdapter: resolveKnowledgeStore(input).store.adapterId,
runtimeComposed: { declared: stackDeclaredCapabilities(input.stack).includes('knowledge') },
},
};
}
return { argument: undefined };
}
Expand Down
Loading
Loading