Skip to content

feat(knowledge): Setup → Search Terms previews a question's terms, stop words and passage counts (#38710 step 3) - #39796

Draft
objectstack-fleet[bot] wants to merge 14 commits into
mainfrom
claude/project-thread-dgwut1
Draft

objectstack-fleet[bot] wants to merge 14 commits into
mainfrom
claude/project-thread-dgwut1

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Requested by Tesla · project thread

Part of #38710 (step 3, PR 1: the term preview, item 1, and the import/export pins, item 3). Left on the card after this PR: the stop-word candidates (F3, held for a ruling), the dictionary candidates (not shipped: no search log is kept, and none is added), and the BrightRule re-run and cloud#5253 (readings held by others).

Clause-②: yes (two optional contract members)
Metadata-protocol: none

维护者速读

  • 平台管理员在「设置 → 检索词条」列表多了「预览切词」按钮:输入一个问题,就能看到它被切成哪些词、哪些词作为停用词被丢掉,选定知识库时还能看到每个词命中多少段落;普通用户无感。
  • 属于中文知识库检索这条线的「管理员能调」一项,替管理员解决加了词之后看不到效果、不知道该加什么的问题。
  • 出错只影响管理员看到的预览(数字不准,或显示“未统计”),不改任何数据、不影响真实检索,按钮可随版本直接撤回。
  • 请审批这次新增的两处可选扩展接口及其设计记录补注。

What changes

  • Preview Terms (item 1). sys_search_term declares a list action preview_terms (type: 'script', requiredPermissions: ['manage_platform_settings'], params question and optional knowledge_source, a resultDialog with a table of term and passages, a code-list of dropped stop words, the source's passage total and a note). No ui/** code: the console's generic action door and result dialog render it. The knowledge service plugin registers the handler on the engine; the handler calls KnowledgeService.previewTerms, whose body lives in the new module knowledge-term-preview.ts (the facade file stays at its 1758-line ceiling).
  • One cut (F1 = A). ISegmenterService gains optional cut(query, options?) answering SegmenterCut { terms, stopWords }. The platform segmenter's keywordTerms now returns cut(...).terms, and the knowledge service's KnowledgeSearchTerms.cutFor is the one place both search (termsFor) and the preview read the cut from. There is no second cutting path.
  • Counts (F2 = A). IKnowledgeAdapter gains optional countKeywordMatches(ctx, terms) answering AdapterKeywordCounts { passages, matches }: the passages of the source's active generation, and for each term the passages its keyword match holds for. Turso and pgvector implement it, and each shares one per-term match helper with its own keyword search (keywordMatchSql on Turso: trigram FTS5 phrase for 3+ characters, else LIKE; keywordTermMatch on pgvector: phraseto_tsquery for a word term, else LIKE/ILIKE). The SQL text of both search legs is unchanged. Memory does not implement it, so the preview says "Not counted" and never answers 0. ADR-0161 K8 gets its dated note (committed first, 42574f6), and the adapter contract suite gets implements.keywordCounts with its case (testing-keyword-counts.ts).
  • Import and export (item 3, measured: the generic doors already serve this object). Pins in the existing harness packages/rest/src/import-search-term-csv.test.ts: csv and xlsx export with the same rows, an xlsx export re-imported through the async jobs door record for record (ids included), and a 403 with no file for a caller without the capability. Plus a docs paragraph in content/docs/protocol/knowledge.mdx.
  • Action labels are translated in all 11 hand-written platform bundles (zh-CN 预览切词).

Read first (by risk):

  1. packages/plugins/knowledge-pgvector/src/keyword-counts.ts and the countKeywordMatches hunk in pgvector-adapter.ts: the count must use the search's operator, scoped to source_id and the active generation.
  2. packages/services/service-knowledge/src/knowledge-term-preview.ts, countFor: every way a count can't be given (held source, refused adapter, vector-only, no member, a thrown count) turns into a note, never a 0.
  3. packages/services/service-segmenter/src/segmenter-service.ts, cut: keywordTerms now delegates to it, so a change in the collected stop words must not move any term.

Tests

Head 7039ce8. Every heavy run went through os-verify-lock.sh; each line quotes its VERDICT command-exit.

  • Red first: 42ed22e committed the failing pins before the code (segmenter 4 failed; platform-objects 3 failed / 16 passed; the service-knowledge preview file failed to load, missing module).
  • @objectstack/service-knowledge test: 35 files, 941 passed (exit 0, rerun at head).
  • @objectstack/service-segmenter test: 5 files, 55 passed (exit 0).
  • @objectstack/platform-objects test: 137 files, 2462 passed (exit 0).
  • @objectstack/knowledge-turso test: 8 files, 351 passed (exit 0).
  • @objectstack/knowledge-memory test: 3 files, 72 passed (exit 0).
  • @objectstack/knowledge-pgvector test (no server): 10 passed, 10 live files skipped (exit 0).
  • Live PostgreSQL (postgres 16 + pgvector 0.6.0, OS_EXPECT_LIVE_DIALECT_MATRIX=1): pnpm --filter @objectstack/knowledge-pgvector exec vitest run --maxWorkers=2 live-postgres, 10 files, 119 passed (exit 0, at head). That CI job runs only off pull_request, so this local run is the PR's live evidence.
  • @objectstack/rest src/import-search-term-csv.test.ts: 6 passed (exit 0, at head).
  • Typecheck (turbo, 8 packages including their test-typecheck lanes): 43 tasks successful (exit 0).

Ablation of the new contract cases. Each was an on-disk mutation through scripts/ablation-replace.mjs, restored with blob == HEAD and an empty git diff HEAD. The tests resolve the mutated module from src (adapter src/index.ts; the @objectstack/service-knowledge/testing alias), so no build was involved.

  1. Turso, count ignores the term (WHERE 1): the contract case red twice (:memory: and file:), "'公开招标' is counted as the keyword leg matches it: expected 4 to be 2". Restored.
  2. Turso, count with another operator (case-sensitive GLOB): red, "'alpine' ... expected +0 to be 1". Restored.
  3. Service, a store without the member answered as counted zeros: the non-implementer case red on knowledge-memory, "expected +0 to be undefined". Restored.
  4. pgvector on live PG, count across sources (source_id = $1 OR true): red, "expected 5 to be 4". Restored.

Acceptance notes

  • Item 3 measured before coding: the generic export (GET /data/:object/export) and the async import (POST /data/:object/import/jobs) already serve sys_search_term in csv and xlsx, so item 3 is pins and a docs line.
  • Re-importing an export needs a mapping that names every column the export writes, its system columns (Created At, ID, Term Key, ...) included. The import door renames only the headers a mapping names and passes any other through as a field name, which is then refused INVALID_FIELD. The pin spells that mapping. Not filed: it was observed on the REST door only, and the console import wizard's column mapping was not exercised.
  • The result dialog's rendering in the console was not browser-verified (NOT MEASURED: browser). Its field and column paths are pinned against the handler's real answer in search-terms-38710.test.ts.
  • knowledge-service.ts is now exactly at its facade line ceiling (1758/1758): the next addition there belongs in a sibling module.
  • Outside the claim's listed file surface: the item-3 pin in packages/rest/src/import-search-term-csv.test.ts (named by the order), the regenerated packages/spec/api-surface/contracts.json and export-origins/contracts.json, and the changeset.
  • Diff: 38 files, +1375 / -46 (1421 changed lines, under the ~1500 split line).

Verification

Two minutes: pnpm --filter @objectstack/service-knowledge exec vitest run --maxWorkers=2 src/__tests__/search-terms-38710.test.ts. Expect every case to pass, including the "the term preview" describe (the cut equals search's terms, the counts equal a one-term keyword search, "Not counted" without the member).

Derived gates (dispatch-gates --commands at 7039ce8, 125 families)

All 125 derived families ran locally at 7039ce8 with exit 0. --ran reconciliation: "125 derived famil(ies) accounted for — 125 run, 0 NOT-MEASURED (a DERIVED zero — all 125 recorded an exit code and none of them is 3)". Fixed along the way: check:facade-delegation-only (the facade was 5 lines over its ceiling, so the docblock moved into the module) and check:where-matcher (a capturing callback in the new pin was rewritten as a loop). Local-only families: check:skill-frame-freshness exit 0; check:dev-prereqs exit 1, "the workspace is not built" (32 of 111 packages have no dist, because builds here were targeted). That reading is about this machine, not the diff. Families CI owns (pnpm lint, the full farm) were not run locally.

🤖 Generated with Claude Code

https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW

claude added 10 commits December 5, 2026 06:58
…wordMatches (objectstack#38710 step 3)

Registers one optional adapter member before the code that builds it
(rule 5): per-term keyword-leg match counts and the passage total of a
source's active generation, read by the administrator's term preview.

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…nd countKeywordMatches (objectstack#38710 step 3)

Failing before the code: ISegmenterService.cut (terms equal to
keywordTerms, stop words dropped), the contract suite's
countKeywordMatches case (each term counted as the adapter's own keyword
leg matches it; turso and pgvector declare it), the service's term
preview and its action handler, and sys_search_term's Preview Terms
action declaration.

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…urso and pgvector (objectstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…ut and AdapterKeywordCounts

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…en bundles (objectstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…-import; docs for the term preview (objectstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…ctstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…ntract lives in its module (objectstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…llback (objectstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added size/xl documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 6 package(s): @objectstack/platform-objects, @objectstack/knowledge-pgvector, @objectstack/knowledge-turso, @objectstack/service-knowledge, @objectstack/service-segmenter, @objectstack/spec, touching 64 documentable anchor(s). ⚠️ 3 changed file(s) yielded no anchor (packages/services/service-knowledge/src/index.ts, packages/spec/api-surface/contracts.json, packages/spec/export-origins/contracts.json), so the pages documenting them are NOT COVERED by this run — this is not a clean bill of health for those files.

43 hand-written doc(s) name something this change touched — list omitted above 15 rows. Re-derive on the tree named below: node scripts/docs-audit/affected-docs.mjs --json 425f170b39fe6324d2a490e6e5907b49d749a991.

⛔ 15 release-owned page(s) also affected — read-only, see AGENTS.md Documentation Guardrails.

What this run could not see
  • 3 changed file(s) yielded no anchor (packages/services/service-knowledge/src/index.ts, packages/spec/api-surface/contracts.json, packages/spec/export-origins/contracts.json) — pages documenting those are invisible to this run
  • 2 anchor(s) matched too much of the corpus to be a work list: PERMISSION_DENIED (literal, 56 pages), VALIDATION_ERROR (literal, 40 pages)
  • 11 name(s) were too generic to anchor anything (single lowercase words)
  • the SDK route bridge reached 71 of 255 client-bound route-ledger rows — the other 184 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 184: 0 are remediable by widening that discovery convention (an in-repo file declares the path; the convention did not scan it); 60 are structural — on a ledger where NOT ONE row is declared in-repo, so no discovery change reaches them at any price; 124 are undecided (no in-repo declaration, on a ledger that has other in-repo registrars — absence and an unreadable spelling are not distinguishable here). The rows themselves: node scripts/docs-audit/affected-docs.mjs --bridge-coverage
  • a page that states a rule by its inputs shares no identifier with the emitter that implements the rule, so an emitter-only diff cannot list it — not on this run and not on any run. Measured on fix(driver-sql): emit varchar(maxLength) for a text field a declared index keys on #11430: content/docs/protocol/objectql/types.mdx documents the text-family column mapping by the ObjectQL type names it maps FROM (text / textarea / html) while the diff changed createColumn; it went unlisted, and it was the page that diff falsified, in four places. No shared token exists to detect this on, so a rule your change carries has to be re-read by hand in the pages that restate it.
  • a key NAME is not a key, so the hand re-read the line above prescribes can land on the wrong schema. The same spelling is authorable on one governed type and a [REMOVED] tombstone on another for each of active, aria, joins, objects, template, tools and version (censused on [finding] tools is a key on BOTH AgentSchema (tombstoned, dead) and SkillSchema (live, cloud-attested), so a name-based search attributes skill examples to the agent key — it produced a false stop-the-line alarm on PR #19059 #19093 over the liveness ledger's governed types, top-level keys); nothing in a search result distinguishes the two, so a grep hit on a LIVE example reads as evidence about the DEAD key. Measured on fix(spec): the agent.tools liveness row says dead — it claimed live on a key the schema tombstoned #19059: content/docs/ai/agents.mdx was reported as contradicting the agent.tools tombstone over its tools: example at :161, which is inside the defineSkill({ block opened at :155 — the page was already correct. Settle ownership by PARSING the value against both schemas, never by the name: that literal PASSES SkillSchema, and as an AgentSchema it FAILS at tools with the tombstone prescription. ⛔ These names are not the whole class — a key retired through a .strict() guidance map leaves no tombstone in the walked shape and none of them here (tool.category, live as AIToolDefinition.category).

Coarse fallback — 186 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json 425f170b39fe6324d2a490e6e5907b49d749a991 → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 3e90fdf9a2a608b9874f175b8a02a99a88ae5e5b — the merge of head f17ce11712981c0148b33a42617c70a3c2cca658 into base 425f170b39fe6324d2a490e6e5907b49d749a991, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 3e90fdf9a2a608b9874f175b8a02a99a88ae5e5b && git checkout 3e90fdf9a2a608b9874f175b8a02a99a88ae5e5b
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 425f170b39fe6324d2a490e6e5907b49d749a991 f17ce11712981c0148b33a42617c70a3c2cca658 && git checkout -B drift-repro 425f170b39fe6324d2a490e6e5907b49d749a991 && git merge --no-ff f17ce11712981c0148b33a42617c70a3c2cca658

node scripts/docs-audit/affected-docs.mjs --json 425f170b39fe6324d2a490e6e5907b49d749a991

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

Advisory only, and a precision-first one (#9192): a page is listed because it names a
symbol, wire route or SDK method this diff touched — not because it mentions a changed
package. Each row says which anchor put it there, so a wrong row is reportable rather than
merely annoying. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs 425f170b39fe6324d2a490e6e5907b49d749a991 → pass the list as
args.docs, on the commit named under Which tree this was computed on.

…e system, at the service (objectstack#38710 step 3)

Red first: previewTerms and its action handler answer any caller today.

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
… system, at the service (objectstack#38710 step 3)

The passage counts (IKnowledgeAdapter.countKeywordMatches) are not filtered
by record permissions. KnowledgeService.previewTerms now refuses, before it
reads the question or the source, every caller that is neither a system
context nor holds manage_platform_settings (403 PERMISSION_DENIED), and the
action handler hands it the caller's capabilities, never the action door's
isSystem elevation. Stated in the member's jsdoc, the ADR-0161 K8 note and
the changeset.

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…sSystem it ignores (objectstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>
…ets its census row (objectstack#38710 step 3)

Claude-Session: https://claude.ai/code/session_01L9qibw3sEzqFZcu4JmuGwW
Co-authored-by: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/xl tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants