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
19 changes: 19 additions & 0 deletions .changeset/38710-search-term-preview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@objectstack/spec': patch
'@objectstack/service-segmenter': patch
'@objectstack/service-knowledge': patch
'@objectstack/knowledge-turso': patch
'@objectstack/knowledge-pgvector': patch
'@objectstack/platform-objects': patch
---

feat(knowledge): Setup → Search Terms previews how a question is cut, and how many passages each term matches

Clause-②: yes (the contracts `ISegmenterService` and `IKnowledgeAdapter` each gain one optional member)
Metadata-protocol: none

Who sees it: a platform administrator in Setup → Search Terms, checking which words a question is matched by before or after adding a search word.

- **Preview Terms.** The Search Terms list has a Preview Terms action, for an administrator with Manage Platform Settings. It takes a question and, optionally, a knowledge source, and answers the terms a knowledge search would match, the words it drops as stop words, and, for a named source, how many passages of the source each term matches beside the source's passage total. The action is labelled in every platform locale (预览切词 in Simplified Chinese).
- **One cut for search and preview.** `ISegmenterService` gains an optional `cut(query, options?)` answering `{ terms, stopWords }`; the platform segmenter's `keywordTerms` now answers `cut(...).terms`, so the preview shows exactly the terms a search is handed. `SegmenterCut` is exported from `@objectstack/spec/contracts`.
- **Adapter contract.** `IKnowledgeAdapter` gains one optional member, `countKeywordMatches(ctx, terms)` answering `{ passages, matches }` (ADR-0161, the second dated note of 2026-12-05 under K8): the passages of the source's active generation, and per term the passages a keyword search of that term matches, with the same operator. The Turso and pgvector stores implement it; a store without it answers the preview's terms uncounted. The counts are not filtered by record permissions, so `KnowledgeService.previewTerms` answers only a system context or a caller holding `manage_platform_settings` (`options.executionContext`), and refuses any other with `403 PERMISSION_DENIED`. The adapter contract suite (`@objectstack/service-knowledge/testing`) gains `implements.keywordCounts` and its case.
2 changes: 2 additions & 0 deletions content/docs/permissions/system-context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ One of the largest consumers of the flag;
| 45a | The `sys_comment` participant keeper lets a system update write `participants` and `thread_root_id` (#39081, #39142), and reads a new reply's parent as the system | plugin-audit | Get: the platform's own rewrite of a thread's stored participant list and of each reply's stored thread root — the after-write reconcile on a reply, a mention edit, a re-point or a delete, and `os migrate comment-participants --apply` — lands under `preserveAudit` with the row's `updated_at` kept; and the create stamp reads the parent of each new reply (its id, parent, discussion and root) to stamp the reply's root. Lose: nothing a user held — every other update has `participants` and `thread_root_id` dropped before the write (reported in `droppedFields` on an ordinary update, dropped unreported under `preserveAudit`), except the engine's removal of a deleted user from `participants` (`referentialFieldClear`), and a create is stamped by the platform over any value the caller sends; the parent read serves only the stamp, never the caller | `installCommentParticipants` |
| 46 | Knowledge search returns hits unfiltered, and searches every source; a knowledge source is saved without its author being judged | service-knowledge, metadata-core, runtime | 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. Lose at the `/meta` and MCP save doors: the same admission asked for the author before a `knowledge_source` is saved (ADR-0161 K4), so the engine's own write saves a source over an object or a field no author was judged for. Lose at the package duplicate door: the same admission asked for the caller, for each knowledge source the copy would save. Lose at the package move-in door: the same admission asked for the caller, for each selected knowledge source the move saves again under the package | `applyPermissionFilter`, `admitSources`, `searchByKey`, `admitAuthoredSource`, `authoredKnowledgeSourceRefusal`, `refuseDuplicatedKnowledgeSources`, `handlePackagesRequest` |
| 46a | Knowledge search under the system identity is not ordered for the asker: a source's `affinity` entries move nothing | service-knowledge | Get: every hit in the order its store answered, and no read of a user or business unit. Lose: nothing a user held. The system identity is no person, so it has no unit to order by; every other caller's user id and unit chain are read as that caller may read them | `resolveAffinityFacts` |
| 46b | The knowledge term preview admits the system context without `manage_platform_settings` | service-knowledge | Get: `previewTerms` answers a question's terms and, for a named source, how many of its passages each term matches, counted over every passage of the source: the counts are NOT filtered by record permissions. Lose: nothing a user held. Every other caller must hold `manage_platform_settings`, or is refused (`PERMISSION_DENIED`) before the question or the source is read; the `preview_terms` action's handler hands it the caller's own capabilities, never the action door's elevation | `callerRefusal` |
| 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 @@ -720,6 +721,7 @@ and CI refuses one that differs from what the tree produces.
| `packages/services/service-knowledge/src/knowledge-service-admission.ts#admitSources` | 1 | 46 |
| `packages/services/service-knowledge/src/knowledge-service-admission.ts#applyPermissionFilter` | 1 | 46 |
| `packages/services/service-knowledge/src/knowledge-service-search.ts#searchByKey` | 1 | 46 |
| `packages/services/service-knowledge/src/knowledge-term-preview.ts#callerRefusal` | 1 | 46b |
| `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
17 changes: 16 additions & 1 deletion content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,18 @@ words; an empty list means the question has no word worth matching, and the
keyword side then finds nothing. When the member is absent (no segmenter is
running, or the segmenter failed), the adapter cuts the question itself, as
before. A search under `vector` is never handed it.
An administrator keeps those words as records of `sys_search_term`: one at a
time, or many through the object's csv and xlsx import, and downloads them
through its csv and xlsx export; the file an export writes imports again with
a mapping that names its columns. The list's **Preview Terms** action answers
how one question is cut: the terms a search would match, the words dropped as
stop words, and, for a named knowledge source whose adapter implements the
optional `countKeywordMatches`, the passages of its active generation each
term matches with the operator the keyword search uses, beside the source's
passage total (ADR-0161, the second dated note of 2026-12-05 under K8). It
counts only what the adapter holds and reads no passage text; an adapter
without the member answers the terms uncounted. The cut comes from the
segmenter's optional `cut`, the same call the search's terms come from.
`listDocuments` lists the documents the
adapter holds for a source, a page of at most `limit` entries at a time, each
with the `docHash` it was stored with; `nextCursor` is absent once the
Expand Down Expand Up @@ -707,7 +719,10 @@ keeps generations, the switch at the end of a build; and, for an adapter that de
`keywordLeg`, an unspaced Chinese question that ranks the passage holding its figure and words; and,
for an adapter that declares `embedQuery`, that a search handed a function answering its own
`embedQuery` answers as without it and makes no embedding call of its own, and that one answering
a vector of another width is rejected. `implements` declares the optional members, and the suite
a vector of another width is rejected; and, for an adapter that declares `keywordCounts`, that
`countKeywordMatches` counts per term exactly the passages a keyword search of that one term
finds in the source, and that the term preview of a real service counts nothing for one that does
not. `implements` declares the optional members, and the suite
checks the declaration against the adapter. `./testing` is a test-only,
ESM-only entry that imports `vitest`; nothing in the runtime entry reaches it.
`knowledge-memory` runs it as the reference implementation; `knowledge-pgvector`
Expand Down
Loading
Loading