Repository navigation
Commit f9fda2a
feat(service-knowledge): documents carry their organization, and search hands the adapter the caller's organizations (#37256)
repo:objectstack#4 · os-justin · session_01QSeKYpEsWWE9Y5YtSsDgqZ
Fixes #37097
repo:objectstack#4 · os-justin · session_01QSeKYkS1d2QkEUgqhneuU1
Clause-②: yes
Metadata-protocol: added
Skills-docs: none — no skill under `skills/**` teaches
`KnowledgeDocument`, `AdapterSearchOptions` or the adapter contract
suite; `skills/objectstack-ai/SKILL.md` teaches `KnowledgeSource`
authoring only. `content/docs/protocol/knowledge.mdx` gains the members
and an "Organization scope" section here.
## 维护者速读
- 多组织部署里,知识检索只在提问人所属组织的文档里排名,别的组织的文档不再挤占前几名;单组织部署无感。
- 属于「知识检索平台化」专题第二阶段:索引文档记下所属组织,检索时由平台按部署的租户形态算出提问人的组织交给存储先过滤。
-
出错时最坏是某些文档在检索里排不上(尚未重建索引的旧文档在多组织形态下要等一次完整重建);能否返回仍由按提问人身份回查记录决定,可直接撤回本改动。
- 无。
## What changes
ADR-0161 K8, item 9, with `knowledge-memory` as its first reader:
1. **`KnowledgeDocument.organizationId?`**
(`packages/spec/src/ai/knowledge-document.zod.ts`). The service sets it
when it assembles a document from a record: the record's
`organization_id` under the `group` and `isolated` postures. It is
absent under `single`, for `file` / `http` sources and for a record with
no organization. `indexDocument` refuses a caller-supplied value, as it
refuses a caller-supplied `docHash`. The TSDoc also says that an adapter
storing `docHash` stores `organizationId` from the same version on.
2. **`AdapterSearchOptions.organizationIds?`**
(`packages/spec/src/contracts/knowledge-adapter.ts`). The service
computes it per `object` source in `knowledge-organization-scope.ts` and
derives nothing itself:
- the posture is the effective one, `classifyAdmissionTenancyPosture`
over the kernel's `tenancy` service. `KnowledgeServicePlugin` wires it
as the new `resolveTenancyPosture` option, next to `resolveSecurity`;
the mount logic is untouched. The wiring is pinned through real kernels
(`LiteKernel` and `ObjectKernel`): walled, no `tenancy` service, and a
`tenancy` service that cannot be read;
- the organization set is `computeTenantLayer0Verdict`
(`@objectstack/core`) for the source's object, over the context's
`tenantId` / `accessible_org_ids`. `isolated` gives the active
organization and `group` the deduplicated membership set. A `deny`
verdict becomes `[]`; `none` (an object outside tenancy, or with no
`organization_id` column) gives no filter;
- no filter under `single`, for the system identity and for `file` /
`http` sources.
The TSDoc spells out the three cases: absent means no filter, a set
means filter before ranking, `[]` means rank nothing (never "no
filter").
3. **`knowledge-memory`** stores `organizationId` and filters by
`organizationIds` before it scores, so `topK` is filled from in-scope
documents only.
4. **The permission re-read is unchanged** and remains the access
decision. Nothing new is logged or returned about how many hits a filter
removed.
5. **`docHash`** covers `organizationId` only when the document has one.
`DOC_HASH_VERSION` stays `v1`, so a document without an organization
hashes byte-for-byte as before (pinned against the old recipe).
6. **A posture that cannot be read** (a `tenancy` service is registered
but unreadable): a search leaves every `object` source unsearched for
that call and logs at `error`; a reindex stops before writing and
returns `ok: false` with the reason. An event sync logs at `warn` with
the reason and leaves the stored document as it was (no upsert, no
delete; pinned).
7. **Contract suite** (`@objectstack/service-knowledge/testing`):
`implements.organizationIds` is required, which is fine because the
suite has not been released yet. It adds adapter-only cases and cases
through a real `KnowledgeService` under `isolated`, `group` and
`single`, plus the upgrade case for documents indexed before this
change.
### PM mechanism assumptions, measured
- **Computed server-side only.** `KnowledgeSearchOptions` has no
organization member, and the service builds `organizationIds` itself. A
caller's opaque `filter: { organizationIds: [...] }` stays inside
`filter` (pinned), and `search_knowledge`'s closed input set refuses an
`organizationIds` argument (pinned). The only callers are the MCP
bridges through `knowledgeTools()`; no REST route reaches `search`.
- **`[]` and absent are different.** `[]` filters to nothing in the
TSDoc, in `knowledge-memory` and in the suite. Under `isolated` with no
active organization, and under `group` with no membership, the adapter
is handed `[]` (pinned).
- **Documents indexed before this change** carry no `organizationId`.
Behind a wall a filtered search excludes them until a complete reindex
writes them again. Their hash changes once the organization is included,
so that reindex does not skip them. Under `single` nothing changes. This
is documented in the TSDoc, the protocol doc and the changeset, and
tested in the contract suite ("a document indexed without its
organization is not ranked behind a wall until a complete reindex writes
it again").
- **`runKnowledgeAdapterContract` was extended**, not a memory-only
test.
## Tests
<details><summary>Local runs on the final head 6e978db (origin/main
d62f9e9 merged; two commits since 3df745d add tests and a TSDoc
sentence)</summary>
- `pnpm --filter @objectstack/knowledge-memory exec vitest run`: Test
Files 2 passed, Tests 29 passed.
- `pnpm --filter @objectstack/service-knowledge exec vitest run`: Test
Files 8 passed, Tests 197 passed.
- `pnpm --filter @objectstack/service-knowledge --filter
@objectstack/knowledge-memory run typecheck`: exit 0 (tsc +
test-typecheck, 0 debt).
- `pnpm --filter @objectstack/spec build && … check:generated`: "All 14
generated artifacts are up to date". Before the merge, one artifact was
stale (`check:docs`) and was regenerated with `--fix`. That diff is the
`organizationId` row and the `docHash` description in
`content/docs/references/ai/knowledge-document.mdx`.
`authorable-surface/ai.json` gained
`ai/KnowledgeDocument:organizationId` from the spec build.
</details>
<details><summary>Derived gate families (dispatch-gates --commands,
117), with --ran reconciliation</summary>
- Run at a8d8915: 113 exit 0. `pnpm check:system-context-census` exited
1 because the new organization scoping held two system-identity
elevation reads with no row on the system-context page. Fixed at
44494ec by moving the scoping under `admitSources`' existing system
bypass (row 46). The system identity's search therefore skips the
organization filter through the one read the page already documents, and
the census reads OK (202 sites, register matches).
- Re-run at 44494ec, all exit 0: system-context-census,
startup-registry-verdict, type-check-coverage, type-check-debt,
test-source-alias, cross-package-test-inputs, undeclared-dep-imports,
issue-citations, optional-error-sink, logger-receiver-detach, nul-bytes,
doc-authoring, engine-double-contract, tenant-chokepoint,
tenant-audit-census, org-identifier.
- NOT MEASURED (exit 3, prerequisite): `check-plugin-teardown-shape.mjs
--self-test` needs a deeper clone (the same exit 3 on the main base
d7f299c); `check:skill-examples` needs `@objectstack/client-react`
built; `check:dual-build-cjs-loads` needs every package's dist. These
are left to CI.
- `dispatch-gates --ran`: 117 derived, 114 run, 3 NOT-MEASURED, 0 UNRUN.
- After 3df745d only a test file and a TSDoc comment changed; the full
set ran in CI at 6e978db (60 success, 10 skipped, 0 failed).
</details>
<details><summary>Ablations (scripts/ablation-replace.mjs; each mutation
proven on disk and restored to the HEAD blob)</summary>
- Memory adapter filter disabled (`organizations` forced `undefined`): 6
contract cases red (the 3 adapter-only filter cases, both service cases
under isolated and group, and the upgrade case). Restored to blob
70dc64b.
- `organizationId` dropped from the hashed body: 2 red (the contract
upgrade case, and the service hash pin). Restored to fe7502a.
- The service stops handing `organizationIds` to the adapter: 4 contract
cases and 5 service cases red. Restored to fe7502a.
- The plugin's `resolveTenancyPosture` wiring replaced by `async () =>
undefined`: 3 service cases red (both walled kernel cases and the
unreadable case); the no-tenancy case stays green. Restored.
</details>
## Acceptance notes
- **K8 item 2's wording.** It lists the hashed parts as "content, title,
metadata". Here the hash also covers `organizationId`, but only when the
document has one. Without that, a complete reindex would skip every
document indexed before this change, and those documents would never
gain their organization behind a wall. Documents without an organization
keep their exact `v1` hash. The contract review (7157191417, M1) asks
for a dated note on ADR-0161 to land first; that note is PR #37263,
merged first as `8032d958f2`.
- The object facts handed to `computeTenantLayer0Verdict` come from the
engine's `getSchema`: `tenancy.enabled: false` or `systemFields.tenant:
false`, and whether an `organization_id` field exists. Unknown schema
means the object is treated as walled. External (federated) objects are
not special-cased, so `externalOrganizationScopeUndeclared` is `false`.
Platform-admin crossing is not applied to ranking (`isPlatformAdmin:
false`). The re-read still decides access either way.
- `recordToDocument` takes the posture as an optional fifth argument, so
existing calls behave as before.
- The system-context page's row 46 ("Knowledge search returns hits
unfiltered, and searches every source") still describes the system
bypass accurately: through `admitSources`, the same bypass now also
skips the organization filter. The row's prose does not mention
organizations. The page is outside this card's file surface, so it is
left unchanged and its owner can choose to add that.
## Verification
Two-minute check: `pnpm --filter @objectstack/knowledge-memory exec
vitest run src/__tests__/contract.test.ts` should report 21 tests
passed, including "isolated: a search from organization A never ranks or
returns B's documents" and "group: …".
Read first:
1.
`packages/services/service-knowledge/src/knowledge-organization-scope.ts`:
the posture-to-organizations mapping (risk: a wrong verdict input widens
or empties the ranking scope).
2. `packages/services/service-knowledge/src/knowledge-service.ts`:
`scopeByOrganization`, plus `computeDocHash` / `recordToDocument` (risk:
the hash change decides what a reindex rewrites).
3. `packages/plugins/knowledge-memory/src/index.ts`, `search` (risk:
filtering after scoring instead of before).
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QSeKYkS1d2QkEUgqhneuU1)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent a7292a5 commit f9fda2a
15 files changed
Lines changed: 899 additions & 44 deletions
File tree
- .changeset
- content/docs
- protocol
- references/ai
- packages
- plugins/knowledge-memory/src
- __tests__
- services/service-knowledge/src
- __tests__
- spec
- authorable-surface
- src
- ai
- contracts
| 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 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
167 | 174 | | |
168 | 175 | | |
169 | 176 | | |
| |||
231 | 238 | | |
232 | 239 | | |
233 | 240 | | |
234 | | - | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
235 | 245 | | |
236 | 246 | | |
237 | 247 | | |
| |||
246 | 256 | | |
247 | 257 | | |
248 | 258 | | |
249 | | - | |
| 259 | + | |
250 | 260 | | |
251 | 261 | | |
252 | 262 | | |
253 | 263 | | |
254 | | - | |
255 | | - | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
256 | 267 | | |
257 | 268 | | |
258 | 269 | | |
| |||
329 | 340 | | |
330 | 341 | | |
331 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
332 | 379 | | |
333 | 380 | | |
334 | 381 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
| 64 | + | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
30 | 37 | | |
31 | 38 | | |
32 | 39 | | |
| |||
92 | 99 | | |
93 | 100 | | |
94 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
95 | 109 | | |
96 | 110 | | |
97 | | - | |
98 | | - | |
| 111 | + | |
| 112 | + | |
99 | 113 | | |
100 | 114 | | |
101 | 115 | | |
| |||
126 | 140 | | |
127 | 141 | | |
128 | 142 | | |
| 143 | + | |
| 144 | + | |
129 | 145 | | |
130 | 146 | | |
131 | 147 | | |
| |||
167 | 183 | | |
168 | 184 | | |
169 | 185 | | |
| 186 | + | |
170 | 187 | | |
171 | 188 | | |
172 | 189 | | |
| |||
0 commit comments