Repository navigation
Commit 783a569
fix(rest): the knowledge_source save reads a scoped knowledge factory in the request's environment (#38325)
Fixes #38211
Clause-②: yes (widening) — `@objectstack/rest`'s exported `RestServer`
constructor takes an optional trailing
`classifiedKernelServiceProvider`, which makes
`ClassifiedKernelServiceLookup` reachable
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:宿主按环境注册知识服务时,经 REST
保存知识源不再一律报"这个部署没装知识服务":该环境有实例就正常保存,没有实例就说明是该环境没有实例,和内部调度入口的说法一字不差;平台自带的知识服务路径不变。
- 为什么做:属于知识库与检索(AI 知识源)这条线,是上一轮"按范围落空时单独说明原因"在 REST 入口的收尾。
- 出错会怎样:只影响自己按环境注册知识服务的宿主(少见),最坏是保存被拒或报错措辞不对,可以直接撤回。
- 要你做什么:无。
## What changes
**Measured first (the card's What 1).** A real `ObjectKernel` with a
`ServiceLifecycle.SCOPED` `knowledge` factory, the real `RestApiPlugin`
on it (scoping on), `PUT
/api/v1/environments/env_alpha/meta/knowledge_source/faq` through
`RestServer`, on base `f19350b410`:
| Registration | Status | Sentence |
|:---|:---|:---|
| factory serving `env_beta` only (the miss) | `503 SERVICE_UNAVAILABLE`
| "The knowledge source 'faq' was not saved: this deployment runs no
knowledge service, so nothing would load, index or search it. Add
requires: ['knowledge'] to the stack ..." |
| factory serving `env_alpha` (should be admitted) | `503
SERVICE_UNAVAILABLE` | the same no-service sentence; the service was
never asked and nothing was saved |
| plain instance | `200` | saved |
So the premise holds, and the defect is wider than the card says: the
single-kernel provider read the slot with `ctx.getService`, which cannot
build a factory and names no scope, so every author's save on a host
with a scoped factory was refused, including in an environment the
factory serves. On the hosted shape (a kernel manager in front of
environment kernels) the provider read the environment kernel without a
scope id, and the same save was refused `503` as "could not be judged
(the knowledge service could not be resolved: Scope ID required for
scoped service 'knowledge')". That reading comes from the ablation run
below.
**The fix (`@objectstack/rest`).**
- `RestApiPlugin` builds `classifiedKernelServiceProvider`. It takes the
legs of `environmentScopedProvider`: the request environment's kernel,
or this plugin's own kernel for no id or `platform`. The read differs in
two ways. It hands the environment id to `getServiceAsync` as the scope
id, and a plain instance ignores that id. The own-kernel leg also reads
through `getServiceAsync` (as `objectQLOnOwnKernel` does), so a factory
can be built. The provider answers the three non-fault outcomes that the
runtime dispatcher's `classifyService` answers: `resolved`,
`never-registered`, and `no-instance-in-scope`. Any other rejection is
raised again. A `LiteKernel`-shaped host keeps the quiet probe. The
plugin still never falls back to its own occupant for an environment
whose kernel carries none.
- `RestServer.resolveKnowledgeService` reads that provider when it is
given one. It maps `no-instance-in-scope` to
`knowledgeServiceNotInScope(scopeId)` from `@objectstack/metadata-core`,
as `resolveKnowledgeServiceForSave` does in the runtime. So
`authoredKnowledgeSourceRefusal` answers the scope sentence on both
transports. A server built without the provider (hand-built, or a test
double) reads `kernelServiceProvider` exactly as before.
- No other seam changes. `kernelServiceProvider` and every
`environmentScopedProvider` slot are untouched.
**File surface.** The claim named `rest-server.ts`, its tests and a
changeset. The fix also edits `rest-api-plugin.ts`, the composition root
that builds the provider. A `RestServer` has no handle on the kernel, so
it cannot make a scoped read by itself.
## Tests
`packages/rest/src/rest-environment-scoped-providers.test.ts` gains a
new top-level describe in the shared real-kernel harness for these
provider seams: `[ADR-0161 K4] the knowledge_source save reads a scoped
knowledge factory in the request's environment`. Every case boots real
`ObjectKernel`s and the real `createRestApiPlugin()`. The identity is
the real resolution plus the save door's two capabilities.
- Single kernel, factory serving another environment: `503
SERVICE_UNAVAILABLE`, first sentence `The knowledge source 'faq' was not
saved: the knowledge service registered on this deployment serves no
instance for environment 'env_alpha'`. The whole message equals what
`authoredKnowledgeSourceRefusal` answers for
`knowledgeServiceNotInScope('env_alpha')`, which is the value the
dispatcher's doors hand it for the same miss. So both transports give
one sentence for one miss. The message has no "runs no knowledge
service". The service is not asked, and nothing is saved.
- CONTROL: the factory serving this environment admits the save, and a
plain instance admits it on both mounts.
- CONTROL: nothing registered keeps the no-service sentence.
- A request that names no environment, against a scoped registration,
gets `503` with the "could not be judged" sentence (the registry's own
`Scope ID required`).
- Hosted: each environment kernel's own scoped factory is read in that
environment. The miss gets the scope sentence for `env_beta`, and the
served environment is admitted and saved.
<details><summary>Readings</summary>
Head `3620e2bb898` unless marked otherwise.
- The new describe and the slot-lookup ledger, `pnpm --filter
@objectstack/rest exec vitest run --maxWorkers=2
src/rest-api-plugin-slot-lookups.test.ts
src/rest-environment-scoped-providers.test.ts`: `Test Files 2 passed
(2)`, `Tests 164 passed (164)`. The lock printed `VERDICT command-exit
0`.
- `pnpm --filter @objectstack/rest typecheck` passed, including
`check:test-typecheck: OK — @objectstack/rest's test layer compiles
under packages/rest/tsconfig.test.json`.
- The whole package at `e78a8cd8d21` (one commit earlier), `pnpm
--filter @objectstack/rest exec vitest run --maxWorkers=2`: `Test Files
1 failed | 439 passed (440)`, `Tests 1 failed | 9306 passed | 376
skipped`. The one red was `rest-api-plugin-slot-lookups.test.ts >
accounts for every positional argument from the first provider to the
last`, the positional-argument ledger asking for the new argument 24.
`3620e2bb898` adds its `NON_PROVIDERS_IN_SPAN` row and a case that pins
the lookup, and that file is green above. The last commit touches only
that test file.
- Ablation at `d6197a14ace` (the fix commit), through
`scripts/ablation-replace.mjs`. The mutation removed the
`classifiedKernelServiceProvider` argument from the `RestServer`
construction, and the anchor went x1 to x0. Result: `Tests 4 failed | 1
passed`. Every new case went red except the nothing-registered control,
which should be unchanged. The hosted miss read the old `could not be
judged (... Scope ID required for scoped service 'knowledge')`. The
restore matched the HEAD blob, and `git diff HEAD` was empty.
</details>
## Acceptance notes
- **Hosted, factory on the HOST kernel.** The dispatcher's
`classifyService` reads the host kernel first, with the scope id, and
then the request kernel. REST still reads only the request environment's
kernel, by the environment-scoped seam rule: no fall back to the
plugin's own occupant. So on a hosted shape where the scoped `knowledge`
factory sits on the host kernel and not on the environment kernels, the
dispatcher can admit a save that REST answers with the no-service
sentence. This PR does not change which kernels REST reads. Nothing here
registers `knowledge` on a host kernel in that shape. Carrier: none.
Noted, not filed.
## Verification
Read first, by risk:
1. `packages/rest/src/rest-api-plugin.ts`:
`classifiedKernelServiceProvider`, the three-way `read` and the
own-kernel leg. This is the only place the scope id is passed.
2. `packages/rest/src/rest-server.ts`: `resolveKnowledgeService`. The
new branch and the unchanged fallback for a server built without the
provider.
Two-minute check: `pnpm --filter @objectstack/rest exec vitest run
--maxWorkers=2 src/rest-environment-scoped-providers.test.ts -t 'scoped
knowledge factory'` should print five passing tests.
<details><summary>Gates</summary>
`node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` was run on `3620e2bb898` and derived 73
families. Each was run on that head with its exit code captured before
any pipe. `--ran` reconciliation: `73 derived famil(ies) accounted for —
71 run, 2 NOT-MEASURED (2 DERIVED from a recorded exit 3)`.
- 71 exited 0.
- NOT MEASURED: `pnpm check:type-check-debt`. Its re-measure builds
under the shared verify lock and exited 3 waiting on it. A second run
was stopped rather than queued. CI runs it.
- NOT MEASURED: `pnpm check:dual-build-cjs-loads`. PREREQUISITE NOT MET:
it needs a whole-workspace `pnpm build`, and only the rest closure was
built here. CI runs it.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 5f741ed commit 783a569
5 files changed
Lines changed: 341 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
217 | 222 | | |
218 | 223 | | |
219 | 224 | | |
| |||
416 | 421 | | |
417 | 422 | | |
418 | 423 | | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
419 | 437 | | |
420 | 438 | | |
421 | 439 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
576 | 577 | | |
577 | 578 | | |
578 | 579 | | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
579 | 642 | | |
580 | 643 | | |
581 | 644 | | |
| |||
615 | 678 | | |
616 | 679 | | |
617 | 680 | | |
618 | | - | |
| 681 | + | |
619 | 682 | | |
620 | 683 | | |
621 | 684 | | |
| |||
0 commit comments