Repository navigation
Commit c35318c
feat(knowledge): a knowledge source orders its hits for the person asking (
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Jianguo** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbYGovuXpimWzFab7rEUdRYr)_
Part of #38503
Clause-②: yes (widening)
Metadata-protocol: added
Skills-docs: follow-up #38610
## 维护者速读
-
用户能看到什么变化:知识源可以声明「和提问人相关的内容排前面」:本单位优先、再按上级单位逐级(nearest),或提问人本人负责的内容往前提若干位(boost,最多
20 位);Studio 知识源表单多一个「按提问人排序」填写项。没声明的知识源、系统身份的检索结果与今天完全相同。
- 为什么做:属于知识切分与检索专题,按 ADR-0161 当天的注记落地裁定「A
精简:排序加带出」里的排序一片;它读的是「告诉模型是谁在问」已落地的同一个身份解析。
- 出错会怎样:只调整同一知识源内命中的先后,不改分数、不丢弃也不增加命中,也不触发重建;排错了只是先后不对,删掉该声明即恢复原顺序。
- 要你做什么:无。三组对照按实测记录(44 道单位题 × 3 轮):不告诉身份 56.1%,告诉身份 82.6%,告诉身份并声明
nearest 86.7%;您 14:44Z 已裁定按提问人排序无需对照证明。
## What changes
PR 1 of 2 for the card. This PR lands the key, its parse refusals, its
save-door refusal and its first reader. The `os validate` half of the
target-field refusal is PR 2 (the lint package is held by another card's
queue). The card's three-arm measurement was run on this branch's build
(see Acceptance notes).
- **Spec** — `ObjectKnowledgeSourceSchema` gains `affinity?:
KnowledgeAffinityEntry[]`, each `{ field, caller, mode, places? }`
(`KnowledgeAffinityEntrySchema`, strict; author and `…Parsed` aliases
per ADR-0122). `caller` is `user` or `business_unit`; `mode` is `boost`
or `nearest`. Refused at parse, each with a sentence naming the fix: a
`field` not in `metadataFields`; `mode: 'filter'` (narrowing is the
search scope, who may read a record is sharing) and any other mode; a
caller outside the two; a second `nearest`; `nearest` with `caller:
'user'`; `places` missing on `boost`, present on `nearest`, or outside 1
to 20. The cross-key rules are refinements JSON Schema cannot state,
ledgered in `dropped-refinements.baseline.json` (the entry schema, plus
the two source schemas and four installed-package response schemas that
embed it; header totals updated).
- **The `places` bound: 20** (`KNOWLEDGE_AFFINITY_MAX_PLACES`, stated on
the key). It is the most hits one `search_knowledge` answer carries
(`SEARCH_KNOWLEDGE_MAX_TOP_K`). A boost of `p` lets a match standing at
number `n` in its tier land just behind number `n − p`, so with `p` at
the largest answer a match is brought into an answer from at most one
answer's length below it. A larger number adds nothing an author can see
in an answer and drifts toward "matches first", which the note keeps for
`nearest` only.
- **The resolver moves to `@objectstack/core`** —
`resolveCallerIdentity` (#38363), with its types and
`CALLER_UNIT_CHAIN_MAX_DEPTH`, moved unchanged from
`packages/runtime/src/domains/mcp.ts` into
`packages/core/src/caller-identity.ts`, so the knowledge service (which
does not depend on the runtime) calls the same function
`get_current_user` does. The runtime imports it from core and re-exports
it for its own tests; `get_current_user` behaviour is unchanged and
`mcp-asker-38363.test.ts` still pins it through that re-export. This
settles where the resolver lives (the note's slice line).
- **Reader** — `knowledge-affinity.ts` holds the two halves.
`resolveAffinityFacts` resolves only the facts a declared entry reads:
`user` is the search's own `ctx.userId` and reads no row;
`business_unit` hands the resolver the user id alone, so it reads the
caller's user row for the primary unit and each unit up the chain, by id
as the caller through `dataEngine.find` with the search's
`ExecutionContext` (a unit the caller may not read ends the chain), and
no organization row; the system identity has no facts. `orderByAffinity`
is the note's rule exactly: per source among the places its units hold;
judged on the hit standing for each unit; `nearest` tiers (own unit,
each parent in order, every other unit, a hit with no value among them;
a multi-value lookup takes its nearest element); inside a tier key =
number − sum of matched `places`, ties to the larger sum then the
earlier number; values compared as a scope term compares them; no score
written. In `searchByKey`, `affinityOrderFor` resolves the facts at most
once per search, only when a hit in the list belongs to a source that
declares an entry, and only those its entries read; the order runs after
the score sort and before `slice(0, topK)` (chunk granularity) or inside
`answerByRecord`'s `leads()` before its cut (record granularity, so the
freshness check checks the re-ordered top). The rounds, the refill and
the re-check are untouched. Moves go to the `debug` log only. A unit
read that fails for a reason other than a refusal leaves the units out,
reported once at `warn`, and keeps the `user` fact, so a `boost` by user
still applies (a functional degradation: every hit is still answered).
- **Save door** — `admitAuthoredSource` refuses an entry whose `field`
(listed in `metadataFields`) does not point at `sys_user` (for `user`)
or `sys_business_unit` (for `business_unit`), judged by
`referenceTargetOf`: `400 VALIDATION_ERROR`, `field` set, after the
documentBy arm and before every permission arm
(`affinityTargetRefusalText` in `knowledge-source-admission.ts`).
- **System-identity census** —
`content/docs/permissions/system-context.mdx` gains row 46a: under the
system identity a source's `affinity` moves nothing and no user or unit
is read (`resolveAffinityFacts`, the one `isSystem` read; the register
is regenerated).
- **Hashes** — `affinity` is in neither the index fingerprint nor the
recipe hash; pinned with a control that the recipe hash still reads
`metadataFields`.
- **Form** — the form-coverage gate needs a row: "Order for the asker",
`widget: 'json'`, with its regenerated translation bundles.
- **Docs** — `content/docs/protocol/knowledge.mdx` gains "Ordering for
the asker"; `content/docs/ai/knowledge-rag.mdx` rewrites "sorted by
score, and capped at `topK`", which this code makes untrue for a
declaring source, and gains a short "Ordering hits for the asker". The
`IKnowledgeService.search` jsdoc says the same.
- Regenerated: authorable-surface, api-surface, export-origins,
declaration-map, the reference page and index, the strictness-ledger
counts, the liveness ledger row (drilled to its four child keys) and
counts.
## Tests
-
`packages/services/service-knowledge/src/__tests__/affinity-38503.test.ts`
(new, 24): the order rule on its own (tiers, places, ties, a multi-value
lookup, values compared as strings); through the service: `nearest`
under chunk granularity at `topK` 5 answers p8, p9, p4, p2, p1 with
their original scores; the caller's user row and units read once per
search, as the caller, and no organization row; `boost`, with a
`user`-only source reading no row; no fact read when no hit belongs to a
declaring source; record granularity (the re-ordered top is what the
freshness check sees); a source without `affinity` unchanged with no
identity read; the system identity; a parent unit the caller may not
read (403) ends the chain; a 500 on the unit read leaves the units out
with one `warn` and keeps the user boost; nothing dropped or added;
`affinity` changes neither hash (with a `metadataFields` control); the
save door's target refusals, each with no record read (a `nearest` entry
pointed at a `boost`). Forcing the reader to return no facts turns 6 of
them red; reading every fact regardless of the entries turns 2 red.
- `packages/spec/src/ai/knowledge-source.test.ts`: 10 new parse cases (a
positive control, the bound at 20 and 21, `places` type and presence per
mode, `filter`, an unknown caller, a second `nearest`, a field outside
`metadataFields`, an unknown key).
- Suites: spec 913 files / 28,903 tests and `test:repo` 1,608 tests;
core 158 files / 3,818 and `test:repo` 110 (the #22661 pin included);
service-knowledge 25 files / 830; platform-objects 130 files / 2,218;
runtime `mcp-asker` and `mcp-authoring` 91. Every touched package's
typecheck exits 0. The dogfood battery at f55fdc0: 495 files / 4,187
tests passed, 3 files skipped.
- Gates: `dispatch-gates` derived 127 commands and all 127 ran (`--ran`
reconciles 127 of 127). Two were red and are fixed in this PR
(`check:spec-parsed-alias`, `check:system-context-census`). Three
stopped on a missing prerequisite and pass once it is met
(`check-plugin-teardown-shape --self-test`, `check:skill-examples`,
`check:published-readme-exports`). `check:dual-build-cjs-loads` needs
the whole workspace built and is left to CI.
## Acceptance notes
- **Three-arm measurement, as measured.** BrightRule's eval (07e21ac),
the 44 `unit_scope` questions, 3 reps each (132 records per arm), asked
over MCP against a server started on this branch's build, answered and
judged blind on the container's local plan (the AI gateway key generated
embeddings only). Tier and effort for both the answering and the judging
model are recorded with the run files.
| Arm | Score (2/1/0) | Per rep | Run range | Location recall@1 |
|---|---|---|---|---|
| 1, no identity | 56.1% | 52.3 / 61.4 / 54.5 | 9.1 points | 31.8% |
| 2, told who asks | 82.6% | 84.1 / 79.5 / 84.1 | 4.6 points | 33.3% |
| 3, told + `nearest` on `owning_unit` | 86.7% | 86.4 / 87.5 / 86.4 |
1.1 points | 67.8% |
The English questions moved from arm 2 to arm 3 41.7% → 55.6% (18
records), the Chinese questions 93.8% → 91.9% (105 records). No arm
leaked. Four judge replies (two in arm 2, two in arm 3) did not parse
and were scored by the rules alone. #38515 has not landed, so the
personas are members who already read the units (BrightRule grants
employees that read). Arm 3's `affinity` entry was added in a local
clone only. The maintainer ruled at 2026-11-29T14:44Z that ordering hits
by the asker's relation needs no comparison to justify it, so no
conclusion is drawn from these numbers and nothing gates on them.
- **Not in this PR:** the `os validate` rule for the target field (PR 2,
`packages/lint`); `expand.window` (#38504); `expand.follow` (#38505); a
`filter` mode, date decay, other caller facts and any opt-out argument
(out of the card).
- **Skills-docs:** `skills/objectstack-ai/SKILL.md` documents the source
keys and does not yet teach `affinity`; the follow-up is #38610.
- Ordering by position means a declaring source's answer may no longer
be in descending `score`; this is the note's rule (5) and is pinned.
## Verification
Read first, by risk:
`packages/services/service-knowledge/src/knowledge-affinity.ts#orderByAffinity`
(the rule), `knowledge-service.ts#affinityOrderFor` and the two call
sites in `searchByKey` / `answerByRecord` (where the order sits relative
to the cut), `packages/spec/src/ai/knowledge-source.zod.ts` (the key and
its refusals).
Two-minute check: `pnpm --filter @objectstack/service-knowledge exec
vitest run --maxWorkers=2 src/__tests__/affinity-38503.test.ts` — 24
passed, including a search where the caller's own unit's policies
(ranked 8th and 9th) answer first with their original scores.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QsR2jSArETHHmJzkH5368W)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01QsR2jSArETHHmJzkH5368W
<!-- signed: repo:objectstack#1 · zhuangjianguo ·
session_01QsR2jSArETHHmJzkH5368W -->
---------
Co-authored-by: Claude <noreply@anthropic.com>source.affinity) (#38606)1 parent 1fef2ed commit c35318c
47 files changed
Lines changed: 1601 additions & 197 deletions
File tree
- .changeset
- content/docs
- ai
- permissions
- protocol
- references
- ai
- docs/audits/2026-07-unknown-key-strictness-ledger.counts
- packages
- core/src
- security
- platform-objects/src/apps/translations
- runtime/src/domains
- services/service-knowledge/src
- __tests__
- spec
- api-surface
- authorable-surface
- declaration-map
- export-origins
- json-schema.manifest
- liveness
- state-counts
- src
- ai
- contracts
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
124 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
125 | 126 | | |
126 | 127 | | |
127 | 128 | | |
| |||
180 | 181 | | |
181 | 182 | | |
182 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
183 | 212 | | |
184 | 213 | | |
185 | 214 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
| 193 | + | |
193 | 194 | | |
194 | 195 | | |
195 | 196 | | |
| |||
707 | 708 | | |
708 | 709 | | |
709 | 710 | | |
| 711 | + | |
710 | 712 | | |
711 | 713 | | |
712 | 714 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1013 | 1013 | | |
1014 | 1014 | | |
1015 | 1015 | | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
1016 | 1095 | | |
1017 | 1096 | | |
1018 | 1097 | | |
| |||
0 commit comments