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
12 changes: 12 additions & 0 deletions .changeset/36993-knowledge-search-refill-display-title.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/service-knowledge': patch
---

fix(service-knowledge): search refills after the permission filter, and a hit's title is the object's display field

Clause-②: no
Metadata-protocol: none

- **Search refills after the permission filter.** The permission re-check runs after ranking, so a caller who may read few of the matching rows used to get a short or empty answer. For a caller who is not the system identity, each `object` source is now asked for `topK × 5` hits; when the hits that survive the re-check name fewer than `topK` records (several passages of one record count once), each `object` source that answered as many as it was asked is asked once more, for `topK × 20` (the cap). At most two rounds; an adapter that answers fewer than it was asked is not asked again. A `file` / `http` source, and every source for the system identity, is asked for `topK` once. The answer is still at most `topK` hits; how many the re-check removed is logged at `debug`, never returned.
- **A hit's title is the object's display field** (ADR-0161 K5; ADR-0079 `resolveDisplayField`: `nameField`, else the derived title field), not a field that happens to be named `title`. A credential-class or `internal` display field, an empty value, or an object with no display field gives no title; a source over an object with no registered definition still reads `title`. The source admission's field gate asks about the same field: a source whose object's display field the caller may not query is not searched for that caller at all. A declared field named `title` still gates the source too, because documents indexed before this change carry its value until a complete reindex writes them again.
- The title enters `docHash`, so the next complete reindex rewrites the documents whose title changed.
8 changes: 7 additions & 1 deletion content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ kernel.use(new KnowledgeMemoryPlugin());
Retrieval is a plain service call — no AI runtime required. Resolve the `knowledge`
service and call `search`. Hits are re-checked against the caller's
`ExecutionContext` (RLS), sorted by score, and capped at `topK` before they come
back.
back. Because the re-check runs after ranking, for a caller other than the
system identity the service asks each object source for more hits than `topK`,
and asks once more when too few records survive, so a caller who may read few of
the matching records still gets up to `topK` hits
([the protocol](/docs/protocol/knowledge) gives the factors and the cap). A
hit's `title` is the value of the object's display field (`nameField`, else the
derived title field).

```ts
const knowledge = ctx.getService('knowledge');
Expand Down
3 changes: 2 additions & 1 deletion content/docs/permissions/system-context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ One of the largest consumers of the flag;
| 43 | Approval actor / submitter / pending-approver checks bypassed (9 sites) | plugin-approvals | Get: approve, reject, recall, reassign without being a pending approver or the submitter, and a decision or comment that attaches files the actor did not upload (every other caller attaches only files it uploaded, in the request's organization) | `isOverrideActor`, `resolveActor`, `sendBack`, `resubmit`, `reassign`, `remind`, `requestInfo`, `comment`, `assertAttachmentsUploadedByCaller` |
| 44 | Attachment access hooks return early (insert + update + delete, the referenced-file check on insert + update, and the read AST) | service-storage | Lose: attachment visibility scoping | `installAttachmentAccessHooks`, `installAttachmentFileReferenceGate`, `installAttachmentReadVisibility` |
| 45 | Comment access hooks return early (insert + update + delete, and the read AST), and so do the activity and audit-log read gates (their read AST), the activity field redaction and the audit-log field redaction (the rows a read serves), the activity summary's message code (the rows a read serves), and the query guard over both objects' value-bearing columns (the read AST) | plugin-audit | Get: the whole activity row and the whole `sys_audit_log` row, its before/after snapshots included, and the activity row's stored summary code (`metadata.summary_message`), on `find` / `findOne` — the audit writer, each read gate's bounded probe and the activity backfill — and a filter, sort or grouping by those columns. Lose: comment visibility scoping, the narrowing of `sys_activity` and of `sys_audit_log` to rows whose parent record the caller can read, the redaction of a parent field's value from an activity row's text and recorded change and from a ledger row's before/after snapshots, the withholding of an activity row recording an update whose every changed field the reader is withheld, of a fired milestone row whose watched fields it is not all served, and of an unstamped row (on `count` and `aggregate` too, so a total agrees with the rows), the removal of that summary code from the activity row's `metadata` (its params are field values), and the refusal of a query over those columns for a reader withheld a field of the objects it can reach, and the withholding of those columns from such a reader's server-chosen search set (#34700) | `installCommentAccessHooks`, `installCommentReadVisibility`, `installActivityReadVisibility`, `installAuditLogReadVisibility`, `installActivityFieldRedaction`, `installAuditLogFieldRedaction`, `installActivitySummaryLocale`, `installParentFieldQueryGuard`, `withheldSearchColumns` |
| 46 | Knowledge search returns hits unfiltered, and searches every source | service-knowledge | Lose: the permission filter over search results, and the source admission ahead of it — an `object` source over an object the caller's exposure decision refuses (disabled package, `apiEnabled` / `apiMethods` for `search`, a `sys_*` object without `enable.aiReadable: true`), or serving a field the caller may not query, is searched | `applyPermissionFilter`, `admitSources` |
| 46 | Knowledge search returns hits unfiltered, and searches every source | service-knowledge | Lose: the permission filter over search results, and the source admission ahead of it — an `object` source over an object the caller's exposure decision refuses (disabled package, `apiEnabled` / `apiMethods` for `search`, a `sys_*` object without `enable.aiReadable: true`), or serving a field the caller may not query, is searched | `applyPermissionFilter`, `admitSources`, `search` |
| 47 | The inbound-mail read gates return early (the read AST) | service-mailbox | Get: every `sys_mail_message` and `sys_mail_association` row on `find` / `findOne` — the ingest writer and each gate's own pre-scan, which read the association and mailbox rows a decision needs. Lose: the narrowing of a message to callers who can read a record it is associated with or who own its mailbox (so the unassociated pool is restricted), and of an association to callers who can read the record it names (on `count` and `aggregate` too, so a total agrees with the rows) | `gateMiddleware` |
| 47b | The inbound-mail ingest seam: `ingest` admits the system context ALONE, and `rematch` admits it without the organization, owner or `manage_mailboxes` check | service-mailbox | Get: a mail source hands a mailbox its messages, and server code re-matches any mailbox. Lose: nothing a user held — every other caller of `ingest` is refused (`PERMISSION_DENIED`), because an ingested message is shown on every record its correspondents' addresses match and a user who could hand the seam a message could write one in a correspondent's name; every other caller of `rematch` must be in the mailbox's organization and own the mailbox or hold `manage_mailboxes` | `ingest`, `authorizeRematch` |
| 47c | The inbound-mail IMAP sync: `syncMailbox` and `syncAll` admit the system context ALONE | service-mailbox | Get: the sync job reads every mailbox row (its organization, address and IMAP cursor), writes the cursor back, and hands each fetched message to `ingest` under the system context. Lose: nothing a user held — every other caller is refused (`PERMISSION_DENIED`), because a sync stores a mailbox's mail and moves its cursor | `syncMailbox`, `syncAll` |
Expand Down Expand Up @@ -703,6 +703,7 @@ and CI refuses one that differs from what the tree produces.
| `packages/services/service-automation/src/runtime-identity.ts#stampSystemInsertOwner` | 1 | 60 |
| `packages/services/service-knowledge/src/knowledge-service.ts#admitSources` | 1 | 46 |
| `packages/services/service-knowledge/src/knowledge-service.ts#applyPermissionFilter` | 1 | 46 |
| `packages/services/service-knowledge/src/knowledge-service.ts#search` | 1 | 46 |
| `packages/services/service-mailbox/src/ingest/mail-ingest-service.ts#authorizeRematch` | 1 | 47b |
| `packages/services/service-mailbox/src/ingest/mail-ingest-service.ts#ingest` | 1 | 47b |
| `packages/services/service-mailbox/src/mail-read-visibility.ts#gateMiddleware` | 1 | 47 |
Expand Down
34 changes: 30 additions & 4 deletions content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,30 @@ caller's `ExecutionContext` and drops hits the user is not allowed to
read. **A hit that the user could not have queried directly is never
returned.**

The re-check runs after ranking, so the service asks for more than it
answers. For a caller who is not the system identity, each `object`
source is asked for `topK × 5` hits. When the hits that survive the
re-check name fewer than `topK` records (several passages of one record
count once), every `object` source that answered as many hits as it was
asked is asked once more, for `topK × 20`. That is the cap: a caller who may
read fewer than `topK` of the rows within the cap gets fewer than `topK`
hits. An
adapter that answers fewer than it was asked (one that caps `topK`) is not
asked again. A `file` or `http` source is asked for `topK` once, and so is
every source for the system identity. The answer is at most `topK` hits.
How many hits the re-check removed is logged at `debug`, never returned to
the caller.

A hit's `title` is the value of the object's display field (ADR-0079
`resolveDisplayField`: `nameField`, else the derived title field), set
when the document is built. It is empty when that field is a
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.

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

```ts
Expand Down Expand Up @@ -379,8 +403,8 @@ hashes as before, and the reindex skips it as before.
### Source admission: exposure and field-level rules

A passage is built at index time, under a system context, from every
`contentFields` member of its source, the record `title` and the source's
`metadataFields`. A row re-check alone would therefore serve a caller the text
`contentFields` member of its source, the object's display field (the
title) and the source's `metadataFields`. A row re-check alone would therefore serve a caller the text
of a field a field-level rule hides from them. So before an `object` source is
searched for a non-system caller, the service asks, once per object per search:

Expand All @@ -394,7 +418,8 @@ searched for a non-system caller, the service asks, once per object per search:
`enable.aiReadable: true` (the AI read tools' posture).
- **Field-level rules** — every field the source serves (each `contentFields`
member, `'*'` meaning every field the object declares except `id`; each `metadataFields`
member; `title`), less the credential-class and `internal: true` fields an
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`
service's `getQueryableFields`. Without that answer: its `getReadableFields`
less every field declaring a `maskingRule`; with no `security` service at
Expand Down Expand Up @@ -444,7 +469,8 @@ when the source has no `where`).
the event's `after`, so a write result that omits a field (a projected
result, a formula the write did not return) cannot leave the document
short. The same field rules as a reindex apply: the fields the source lists
and the record `title`, never a credential-class or `internal` field.
and the object's display field as the title, never a credential-class or
`internal` field.
- **No row comes back** — the row does not match (a draft, or a row that
stopped matching), and its document is deleted. Deleting an absent document
changes nothing.
Expand Down
Loading
Loading