Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/37099-knowledge-first-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@objectstack/service-knowledge': patch
'@objectstack/cli': patch
---

feat(service-knowledge): a knowledge source that was never built is built after boot, and an administrator can rebuild one with `os knowledge rebuild`

Clause-②: yes
Metadata-protocol: none

- **The first build.** After every boot, `KnowledgeServicePlugin` reads what each `object` source's adapter holds (`indexStatus`, else one entry of `listDocuments`) against the index fingerprint and recipe hash the service computes now (ADR-0161 K7, "What starts a rebuild"), and builds — once, in the background, one source at a time — every source the adapter holds nothing for, holds under another fingerprint, or holds under another recipe hash. A source already built is left alone; a source with `refresh.onRecordChange: false` is left to its external indexer; an adapter with neither `indexStatus` nor `listDocuments` is never built automatically, and the boot log says so once. `KnowledgeServicePlugin({ buildAtBoot: false })` turns it off.
- **One node builds a source.** A build runs under the `cluster` service's lock, key `knowledge:build:SOURCE`, renewed while it runs; a node that gets the lock after another built the source re-reads it and builds nothing, and a lock that does not answer starts no build. With no cluster service the composition is one node. Only a shared cluster lock driver (`redis`) makes this hold across nodes: the default `memory` driver's lock is per process, so with it every node builds.
- **The administrator's rebuild.** The `knowledge` settings namespace gains two actions, gated like every settings write on `manage_platform_settings`: `rebuild` (`{ "source": "ID" }`) starts one complete build in the background, and `index_status` (`{}` or `{ "source": "ID" }`) answers each source's index state and the last build the answering node ran — its progress while it runs, its result (written, unchanged, purged, cannot be indexed) once it ended. `os knowledge rebuild SOURCE` starts a build and follows its progress to its end (`--no-wait` exits 0 once it has started and reports no outcome; otherwise exit 1 when it did not run or did not finish); `os knowledge status [SOURCE]` prints the state.
- **What it costs.** A build sends the content fields of every matching row the index does not already hold unchanged to the embedding provider, which bills for them. The line that starts a build says so, the server logs its progress every ten seconds and its result, and `content/docs/ai/knowledge-rag.mdx` states it. The default `memory` adapter holds nothing after a restart, so with it every boot builds the whole source again, on every node.
- `KnowledgeService` gains `readIndexState`, `rebuildSource`, `buildRecord`, `buildSourcesAtBoot` and `stopBuilds`, and the option `resolveBuildLock`; `IKnowledgeService` is unchanged.
30 changes: 29 additions & 1 deletion content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,35 @@ Known residual: text made mostly of random hex or base64 — UUIDs, sha256 diges

## When the model or the split changes: rebuild

Vectors from two embedding models are not comparable, and neither are the passages of two splits. So every source's index is built under one **index fingerprint** — the embedding model and dimensions the adapter reports, the resolved split, and the content list — which the knowledge service computes and carries on every write and every search, and the adapter checks against the index it holds. Change any of them and the source's stored index is **refused** (`KNOWLEDGE_REBUILD_REQUIRED`, 409) until a complete reindex rebuilds it; it never answers with vectors of the old model ranked against the new one. A search over several sources still answers from the others, and the `search_knowledge` tool names each refused source in `refused`. Changing `where` or `metadataFields` changes no vector: a complete reindex repairs it in place. The full rules are in [the protocol's fingerprint section](/docs/protocol/knowledge#36-the-index-fingerprint).
Vectors from two embedding models are not comparable, and neither are the passages of two splits. So every source's index is built under one **index fingerprint** — the embedding model and dimensions the adapter reports, the resolved split, and the content list — which the knowledge service computes and carries on every write and every search, and the adapter checks against the index it holds. Change any of them and the source's stored index is **refused** (`KNOWLEDGE_REBUILD_REQUIRED`, 409) until a complete reindex rebuilds it — which the next boot starts by itself, or `os knowledge rebuild` (below) does now; it never answers with vectors of the old model ranked against the new one. A search over several sources still answers from the others, and the `search_knowledge` tool names each refused source in `refused`. Changing `where` or `metadataFields` changes no vector: a complete reindex repairs it in place. The full rules are in [the protocol's fingerprint section](/docs/protocol/knowledge#36-the-index-fingerprint).

## The first build, and rebuilding a source

Event sync keeps an index in step with rows as they are written; it never indexes the rows a source matched before it was mounted. So after every boot the knowledge service reads what each source's adapter holds and builds, once, in the background, every `object` source that needs it — the boot never waits for it:

| What the adapter holds | At boot |
|:--|:--|
| nothing for the source (or, for an adapter without an index status, no document) | built |
| an index under another fingerprint (embedding model, dimensions or split) | built anew, beside the old index where the store keeps generations |
| the same fingerprint, but the source's `where`, `metadataFields`, title rule or withheld fields changed | rebuilt in place; unchanged documents are skipped |
| the same fingerprint and recipe | left alone |

A source with `refresh.onRecordChange: false` is left to its external indexer, and an adapter that reports neither an index status nor its documents is never built automatically: the boot log says so, and you rebuild it by hand. With several server nodes sharing a cluster lock driver (`OS_CLUSTER_DRIVER=redis`, [Cluster](/docs/kernel/cluster)), only one builds a source: the build runs under the `cluster` service's lock, and a node that finds the source built when it gets the lock builds nothing. `KnowledgeServicePlugin({ buildAtBoot: false })` turns the boot builds off.

<Callout type="warn">
**A build costs money.** A complete build reads every row the source's `where` matches and sends the listed content fields of each one the index does not already hold unchanged to your embedding provider, which bills for them. The first build of a large source, or a rebuild after you change the embedding model or the split, sends all of it. The boot log line that starts a build says so.

The default `memory` adapter holds nothing after a restart, so with it **every boot is a first build**: each boot sends the whole source to the embedding provider again, on every node, because each process keeps its own index. Use a persistent adapter (pgvector, Turso, RAGFlow) for a source you do not want re-embedded on each restart. And "only one node builds a source" holds only with a shared cluster lock driver: the default `memory` cluster driver's lock is a per-process mutex, so with it every node builds the source itself.
</Callout>

To rebuild a source yourself, as an administrator (an account with `manage_platform_settings`):

```bash
os knowledge status # each source: built, never built or due a rebuild, and the last build
os knowledge rebuild support_articles # start a complete build and follow its progress
```

`os knowledge rebuild` starts the build on the server and prints its progress — rows read, documents written, unchanged, purged, and those that cannot be indexed — until it ends; `--no-wait` returns once it has started. Both commands call the `knowledge` settings actions `rebuild` and `index_status` (`POST /api/settings/knowledge/rebuild` with `{ "source": "ID" }`). The server logs each build's start, its progress every ten seconds, and its result. A build's progress lives on the node that runs it: behind a load balancer, read it from that node.

## Self-hosted vector store: PostgreSQL + pgvector

Expand Down
56 changes: 56 additions & 0 deletions content/docs/deployment/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4003,6 +4003,62 @@ the grant, use the environment's console: **Account → Connect an AI Assistant*
| `-s, --server <url>` | `OS_CLOUD_URL` | Control-plane URL. Default `https://cloud.objectos.ai`, or the URL recorded by `os cloud login` |
| `-t, --token <key>` | `OS_CLOUD_API_KEY` | Bearer token, used only to find the environment; `$OS_TOKEN` and `~/.objectstack/cloud.json` are the fallbacks |

### Knowledge

The administrator's view of the server's [knowledge sources](/docs/ai/knowledge-rag#the-first-build-and-rebuilding-a-source):
what each source's index holds, and one complete rebuild of a source. Both
commands call the `knowledge` settings actions (`POST /api/settings/knowledge/rebuild`
and `/index_status`), which answer only an account with
`manage_platform_settings`; any other caller is refused with `403` and is told
nothing about the sources.

| Command | Description |
|---------|-------------|
| `os knowledge status` | Each source's index state (built, never built, due a rebuild) and the last build the answering node ran |
| `os knowledge rebuild` | Start one complete build of a source on the server and follow its progress |

#### `os knowledge status`

```bash
os knowledge status # every source
os knowledge status support_articles # one source
```

Prints, per source, its adapter and kind, its index state against what the
server expects (and whether a build is due), and the last build the node that
answered ran: its progress while it runs, its counts once it ended. A build's
record lives on the node that ran it, so behind a load balancer the node that
answers may not be the one building; the index state is read from the store and
is the same on every node. Exits `1` with the server's refusal text when the
action is refused.

#### `os knowledge rebuild`

```bash
os knowledge rebuild support_articles # start and follow to the end
os knowledge rebuild support_articles --no-wait # start, and return at once
```

**A build costs money:** every row the source's `where` matches is read, and the
content fields of each one the index does not already hold unchanged are sent to
the embedding provider, which bills for them. The build runs on the server in
the background; the command reads `index_status` every `--interval` seconds and
prints the build's progress until it ends. It exits `0` when the build finished,
and `1` when the server refused the rebuild (an unknown or `file` source, a
build already running on that node), when the build did not run or did not
finish, or when the progress was answered by another node than the one
building (the build continues there). `--no-wait` exits `0` once the build has
started and reports no outcome: the build can still end without running
(another node holds the source's lock) or fail, which only `os knowledge status`
then shows.

| Flag | Commands | Env equivalent | Purpose |
|---|---|---|---|
| `-u, --url <url>` | both | `OS_CLOUD_URL` | Server URL (default `http://localhost:3000`) |
| `-t, --token <token>` | both | `OS_TOKEN` | Bearer token of an administrator |
| `--no-wait` | `rebuild` | — | Return once the build has started; its outcome is not reported |
| `--interval <s>` | `rebuild` | — | Seconds between two progress reads (default `2`) |

### Skills

The published skills catalog (`@objectstack/skills`, see [AI Skills](/docs/ai/skills))
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
"i18n": {
"description": "Scaffold translation skeletons from a stack, and check translation coverage"
},
"knowledge": {
"description": "Rebuild a knowledge source's index on the server and read each source's index state and last build"
},
"mcp": {
"description": "Connect an MCP client to your environment's MCP server"
},
Expand Down
Loading
Loading