Skip to content
Draft
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
20 changes: 20 additions & 0 deletions .changeset/37467-knowledge-title-arm-retired.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@objectstack/service-knowledge': patch
---

fix(service-knowledge): the knowledge field gate stops asking about a field named `title`; a source whose index could still hold an earlier-rule title is searched only once its index is proven built under the current recipe

Clause-②: yes (narrowing) — narrowed: for a caller other than the system context, a source over an object that declares a field named `title` the index may carry and the source does not serve (the display field is another field, none resolves, or the object's fields cannot be enumerated; the source lists `title` in neither `content` nor `metadataFields`) is now searched only when its adapter's `indexStatus` reports the fingerprint and the recipe hash the service computes now, and the builds that record that hash purge; otherwise the search names it in `refused` as `KNOWLEDGE_REBUILD_REQUIRED` (409), and refuses whole when it was the only source, where it was searched for any caller who may query `title`. That holds until a complete build of the source has completed: on a built-in adapter the boot pass, a source reload or the reconcile runs one, and under `buildAtBoot: false` (no boot or reload build) only the reconcile, where it runs, or `os knowledge rebuild` does. It holds for good, since no build can prove the index, for a source with `refresh.onRecordChange: false` (its builds purge nothing) and on an adapter with no `indexStatus`, no `completeBuild` or no `listDocuments`. Each hit of a proven source is then served only when the record still belongs to the source and the stored `docHash` is the one the current recipe assembles from the record as it reads now (one system read per source per search round); a hit that fails that check, or that the check cannot be made for (the read fails, no record id, no stored hash, a hash that cannot be computed, an unreadable tenancy posture), is dropped, where it was served before. Widened: saving a `knowledge_source` over such an object no longer requires the author to query `title`, which the source never indexes.
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed or renamed, and nothing stored is rewritten: the moved answer is whether a search serves a source whose index is not proven built under the current recipe. The adapter author's fix below is new code on optional IKnowledgeAdapter members, not a rewrite of anything authored or stored, and the operator's fix is a rebuild. -->

**BREAKING** for deployments whose knowledge sources sit over an object that declares a field named `title` the source does not serve:

- **Such a source is refused `KNOWLEDGE_REBUILD_REQUIRED` until a complete build under the current recipe completes.** On a built-in adapter (memory, pgvector, Turso) the boot pass, a source reload or the reconcile builds a source whose index records no recipe hash or another one, and the refusal ends when that build completes. Under `buildAtBoot: false`, which also turns off the build on reload, only the reconcile (where a job service runs it) builds it. Fix: run `os knowledge rebuild <source>`; it also ends the refusal sooner in every other case.
- **A proven source serves a hit only when its stored document matches the record as it reads now.** A document written before an edit the event sync has not caught up with, or left in place by a build's purge, is dropped from the answer until it is rewritten; for a stale or missing record's own document the search starts that repair in the background, once per document until a later search finds it current, and a document under a foreign id is dropped, not repaired. Fix: none needed for a synced source; `os knowledge rebuild <source>` rewrites every document now.
- **An adapter whose search hits carry no `docHash` serves no hit from such a source**, since a hit with no stored hash cannot be checked against its record and is dropped. The built-in adapters (memory, pgvector, Turso) return the stored `docHash` on every hit. Fix: return each hit's stored `docHash` from the adapter's `search` (ADR-0161 K8, item 3).
- **A source with `refresh.onRecordChange: false` stays refused** to every caller but the system context: an external indexer owns its index, its builds purge nothing, so no recorded recipe hash proves it. Fix: let the service sync the source (drop `refresh.onRecordChange: false`) and rebuild it, or search it as the system context.
- **On an adapter with no `indexStatus`, no `completeBuild` or no `listDocuments`, such a source stays refused**, since no build can prove what its index holds. Fix: implement all three on the adapter (`@objectstack/spec/contracts`, `IKnowledgeAdapter`), then rebuild the source.

The field gate (ADR-0090 D10 rule 1) kept asking whether the caller may query a declared `title` field alongside the object's display field, because a document indexed before the title became the display field carries the value of `title`. That arm is retired. A complete build under the current recipe rewrites each document it walks whose title changed (its `docHash` covers the title) and, when it purges, removes every document it did not walk; the recipe hash names the title rule (ADR-0161 K7) and was first recorded by code that already read the display field. So the search asks for that proof per source instead, and only for a source where it matters: one over an object that declares no `title` field, whose display field is `title`, whose `title` is credential-class or `internal`, or that serves `title` itself, is searched as before, with no extra read. A proven source's hits are each checked against the live record, since the proof speaks for the index as its last complete build left it and not for a document written after it. Every other arm of the gate (the display field, each `content` and `metadataFields` member, the fields `where` reads, the object-level gates) is unchanged.
2 changes: 1 addition & 1 deletion content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ Vectors from two embedding models are not comparable, and neither are the passag
A store that keeps **generations** (pgvector and Turso) does not throw the old index away when the fingerprint changes. The rebuild fills a new generation beside it, and the switch happens only when the build is done:

1. **The build writes into the new generation.** The complete reindex writes every document into it. Record changes that arrive while it runs (event sync) carry the new fingerprint, so they also land in the new generation only. Nothing is written into the old one under the new model.
2. **Search during the build is refused, never answered from the old index.** A search of the source answers `KNOWLEDGE_REBUILD_REQUIRED` (409) and says the source *is being rebuilt*. A search over several sources still answers from the others, and `search_knowledge` names this one in `refused`.
2. **Search during the build is refused, never answered from the old index.** A search of the source answers `KNOWLEDGE_REBUILD_REQUIRED` (409) and says the source *is being rebuilt*. A search over several sources still answers from the others, and `search_knowledge` names this one in `refused`. The same code has a second cause, from the knowledge service itself: a source over an object that declares a `title` field the source does not serve is refused until its index is proven built under the current recipe ([the protocol's title rule](/docs/protocol/knowledge)).
3. **At the end, the switch.** Once the walk is done, the purge runs once. Then the new generation becomes the active one, and the old one is deleted. pgvector deletes it in batches of 1,000 rows, so no long lock is held over a large source. If the deletion stops part-way, the source's next build finishes it, and the old rows are never searched meanwhile.
4. **One builder per source.** pgvector takes a PostgreSQL advisory lock on the source when its build begins and holds it until the switch. A second build of the source, from another node or process, is refused `LOCK_CONFLICT` (409) and walks nothing. This is a *session* lock, so the store needs a direct or session-mode PostgreSQL connection. Behind a transaction-mode pooler (PgBouncer transaction pooling, or a hosted provider's pooled URL), the lock does not stay with the store's session. Point the store's `url` at a direct or session-mode endpoint. A release whose session no longer held the lock is logged as a warning.
5. **A build that stops is resumed, not restarted.** The generation under construction is kept, with everything written into it so far. The next build under the same fingerprint takes it up, and skips every document it already holds unchanged, so only the rest is sent to the embedding provider. If the process stops, PostgreSQL frees the lock at once. If the build fails on a process that keeps running, the lock is freed after 15 minutes without a call of the build itself. Record events that keep arriving for the source do not hold it. A build under yet another fingerprint abandons the unfinished generation and deletes it, and so does a build under the active fingerprint, when the change was reverted before the new index was finished.
Expand Down
39 changes: 33 additions & 6 deletions content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,36 @@ credential-class or `internal` field, when the value is empty, and when
the object resolves no display field. A source over an object with no
registered definition reads a field named `title`. Documents indexed
before this rule carry the value of a field named `title` until a complete
reindex writes them again, so the source admission below still asks about a
declared `title` field as well as the display field.
reindex writes them again. So a source over an object that declares a
`title` field the index may carry and the source does not serve (the display
field is another field, none resolves, or the object's fields cannot be
enumerated) is searched for a caller other than the system context only once
its adapter's index status reports the fingerprint and the recipe hash the
service computes now. The recipe hash names the title rule and is recorded
only when a complete build completes, and only a build that purges removes
the documents it did not walk. Until then the search refuses the source
`KNOWLEDGE_REBUILD_REQUIRED` (409), and the boot's build pass, a source
reload, the reconcile or `os knowledge rebuild` builds it. Under
`buildAtBoot: false` (which also turns off the build on reload) nothing but
the reconcile, where it runs, or `os knowledge rebuild` builds it. No build
proves such a source, so it stays refused, when its
`refresh.onRecordChange` is `false` (its builds purge nothing, and the boot
pass and the reconcile leave it alone) or when its adapter has no
`listDocuments`, no `completeBuild` or no index status.

The proof covers the index as its last complete build left it. So each hit of
a proven source is also checked against its record as it reads now, with one
read per source per search round in the system context. A hit is served only
when the record still belongs to the source (its `where` included) and the
document the store holds is the one the current recipe assembles from it (the
same `docHash`). A document the build's purge left in place, or one written
after the build under an earlier recipe, is never served. The search repairs
a stale or missing record's own document in the background, once per document
until a later search finds it current; a document under a foreign id is
dropped and not repaired. A check that cannot be made drops the hit: the
read fails, the hit names no record or carries no hash, the hash cannot be
computed, or the tenancy posture cannot be read. The read decides only whether
a hit is served; nothing it reads is added to the hit.

### 3.3 `IKnowledgeService` (consumed by the ObjectOS AI runtime, REST, Console)

Expand Down Expand Up @@ -904,10 +932,9 @@ searched for a non-system caller, the service asks, once per object per search:
- **System objects** — a `sys_*` object is searched only when it declares
`enable.aiReadable: true` (the AI read tools' posture).
- **Field-level rules** — every field the source serves (each `content`
member; each `metadataFields`
member; the display field the title is read from, and a declared `title`
field), less the credential-class and `internal: true` fields an
index never carries, must be one the caller may **query**, per the `security`
member; each `metadataFields` member; the display field the title is read
from), less the credential-class and `internal: true` fields an index never
carries, must be one the caller may **query**, per the `security`
service's `getQueryableFields`. Without that answer: its `getReadableFields`
less every field declaring a `maskingRule`; with no `security` service at
all: every field except those declaring a `maskingRule`. Queryable, not
Expand Down
Loading
Loading