From 83d6c450815ef54bf299b2d3ad1f8697f3ae291d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 12:50:25 +0000 Subject: [PATCH 1/2] feat(service-knowledge)!: an author's knowledge source save is refused while the knowledge service is not enabled A knowledge service the runtime composed and that the boot read as not enabled (no embedder, or the deployment's store not mounted) holds every source. An author's save of a source there is now refused 503 SERVICE_UNAVAILABLE by the service's own verdict, ahead of every gate, naming the half that is missing and the ruled remedy ("save an embedder provider, restart, then save"; "mount the deployment's knowledge store, restart, then save"; both). Every door that saves one answers it: the /meta save on both transports, the MCP draft save and the package move-in of a package-less source. The package duplicate is not refused for it, and the engine's own write is admitted. On a deployment with no knowledge service, the save's refusal also says to save an embedder provider and restart again; the duplicate's sentence is unchanged. Claude-Session: https://claude.ai/code/session_01QsR2jREKp1JAmqK54ADBYS Co-authored-by: Claude --- ...38369-knowledge-save-refused-while-held.md | 26 ++++ content/docs/ai/knowledge-rag.mdx | 3 +- content/docs/protocol/knowledge.mdx | 21 ++- .../knowledge-source-save-admission.test.ts | 7 + .../src/knowledge-source-save-admission.ts | 41 ++++-- ...-write-door-capability-enumeration.test.ts | 20 +++ .../runtime/src/domains/mcp-authoring.test.ts | 22 ++++ .../domains/meta-save-capability-gate.test.ts | 26 ++++ .../packages-knowledge-source-gate.test.ts | 121 ++++++++++++++++++ .../knowledge-settings-contribution.test.ts | 78 +++++++++++ .../src/knowledge-service.ts | 46 +++++++ ...sed-while-knowledge-service-not-enabled.ts | 34 +++++ 12 files changed, 429 insertions(+), 16 deletions(-) create mode 100644 .changeset/38369-knowledge-save-refused-while-held.md create mode 100644 packages/spec/src/migrations/entries/semantic/19.knowledge-source-save-refused-while-knowledge-service-not-enabled.ts diff --git a/.changeset/38369-knowledge-save-refused-while-held.md b/.changeset/38369-knowledge-save-refused-while-held.md new file mode 100644 index 00000000000..96dd92c1fd2 --- /dev/null +++ b/.changeset/38369-knowledge-save-refused-while-held.md @@ -0,0 +1,26 @@ +--- +'@objectstack/service-knowledge': minor +'@objectstack/metadata-core': patch +'@objectstack/spec': patch +--- + +feat(service-knowledge)!: an author's knowledge source save is refused while the knowledge service is not enabled + +Clause-②: no (narrowing) +Metadata-protocol: narrowed + + + +**BREAKING** for an author saving a `knowledge_source` on a self-hosted deployment whose knowledge service the runtime mounts (the stack declares `requires: ['knowledge']` or ships a `knowledgeSources` entry) and which, at boot, has no embedder or no deployment knowledge store mounted. Such a save used to be accepted while every source was held (ADR-0161 K8, the dated note on item 12): kept, listed, and never built, synced or searched. It is now refused where it is made, naming the half that is missing. + +- **`@objectstack/service-knowledge`.** While the boot read the service as not enabled, `KnowledgeService.admitAuthoredSource` refuses an author's save `503` with code `SERVICE_UNAVAILABLE`, ahead of every other gate. With no embedder the remedy is "save an embedder provider, restart, then save the source" (a provider saved without its key names that cause beside it); where the deployment's store is not mounted it says so, that the boot's log names the reason, and "mount the deployment's knowledge store, restart, then save the source"; with both missing it names both. Every door that saves one answers it: `PUT /meta/knowledge_source/:name` on both transports, the MCP `save_metadata_draft` tool, and `POST /packages/:id/move-in` of a package-less source. Nothing is written. The package duplicate (`door: 'duplicate'`) is not refused for it, and the engine's own write (a package install, a seed, a migration) is admitted, so a shipped source still installs and is held. +- **`@objectstack/metadata-core`.** On a deployment that runs no knowledge service, the save's refusal now also says to save an embedder provider after the restart that mounts the service, and to restart again, before saving the source: the service `requires: ['knowledge']` mounts serves only once an embedder is configured. The package duplicate's sentence is unchanged. +- **`@objectstack/spec`.** The upgrade guide carries the `knowledge-source-save-refused-while-knowledge-service-not-enabled` entry. + +Unchanged: a knowledge service a host mounts itself (`KnowledgeServicePlugin` in `plugins[]`, no `runtimeComposed`) and a deployment with both halves admit a save as before; stored sources are not rewritten or refused. + +## FROM → TO + +- An author's source save refused because no embedder is configured → save an embedder provider on the AI settings page (or register an `embedder` service from a plugin), restart, then save the source. +- Refused because the deployment's knowledge store is not mounted → mount the store the boot log names, restart, then save the source. +- Refused because the deployment runs no knowledge service → add `requires: ['knowledge']`, restart, save an embedder provider, restart again, then save the source. diff --git a/content/docs/ai/knowledge-rag.mdx b/content/docs/ai/knowledge-rag.mdx index 88882dc0293..34403f9c681 100644 --- a/content/docs/ai/knowledge-rag.mdx +++ b/content/docs/ai/knowledge-rag.mdx @@ -41,7 +41,8 @@ Which store is mounted depends on the platform database: - **More than one replica** on MySQL, MongoDB or a remote libSQL database: each replica keeps its own local `knowledge.db` and indexes only the record writes that replica itself handles, so replicas answer from indexes that differ. Give them one store: save a shared `libsql://` URL in `turso_url`, or run on PostgreSQL, where the vectors live in the one database. - **An install without the libSQL client**: `@objectstack/knowledge-turso` is an optional dependency of the CLI, because its client is a native module. Where it could not be installed the server warns at boot that the store was not mounted, and sources naming `turso` answer `SERVICE_UNAVAILABLE`. - **Without a source or the token** nothing is mounted: no knowledge service, no store and no Knowledge settings page. `knowledge-memory` is never mounted for you. -- **A source is saved only on a deployment that mounts the knowledge service.** On one without it, saving a source in Studio, the metadata API or MCP is refused `SERVICE_UNAVAILABLE` (503) and nothing is written: nothing there would load, index or search it, and the service is mounted when the server starts, never while it runs. A source saved at run time is not one the app ships, so it does not mount the service either. The refusal names the fix: add `requires: ['knowledge']`, restart, then save the source. A package install, a seed or a migration is not refused; the boot names each source it holds with no service to load it, and the same fix. +- **A source is saved only on a deployment that mounts the knowledge service.** On one without it, saving a source in Studio, the metadata API or MCP is refused `SERVICE_UNAVAILABLE` (503) and nothing is written: nothing there would load, index or search it, and the service is mounted when the server starts, never while it runs. A source saved at run time is not one the app ships, so it does not mount the service either. The refusal names the fix: add `requires: ['knowledge']`, restart, save an embedder provider, restart again, then save the source. A package install, a seed or a migration is not refused; the boot names each source it holds with no service to load it, and the same fix. +- **A source is saved only while the service is enabled.** Where the runtime mounted the service and its sources are held ([Mounted is not enabled](#turning-it-on-requires-knowledge)), saving a source in Studio, the metadata API or MCP, or moving a package-less source into a package, is refused `SERVICE_UNAVAILABLE` (503) and nothing is written. The refusal names what is missing and the fix: save an embedder provider, restart, then save; or, where the boot log says the deployment's store is not mounted, mount it, restart, then save. Duplicating a package is not refused for it, and a package install, a seed or a migration is not either: those sources are held. - **Mounting it yourself** still works: an app whose `plugins` already hold `KnowledgeServicePlugin` keeps its own, and the runtime adds neither a second service nor a store. ## Writing a source diff --git a/content/docs/protocol/knowledge.mdx b/content/docs/protocol/knowledge.mdx index 1b0f8cd3b71..07cd81ffcb8 100644 --- a/content/docs/protocol/knowledge.mdx +++ b/content/docs/protocol/knowledge.mdx @@ -264,9 +264,24 @@ ships, so it does not mount the service either. So on such a deployment every door above refuses an author's save `503 SERVICE_UNAVAILABLE`, and a duplicate that would copy a source is refused the same way; nothing is written. The refusal names the fix: add `requires: ['knowledge']` to the stack, restart, -then save the source. The engine's own writes (a package install, a seed, a -migration) are not refused, and a knowledge service a host mounts itself -answers for its own sources. +save an embedder provider (the service the token mounts is enabled only with +one), restart again, then save the source. The engine's own writes (a package +install, a seed, a migration) are not refused, and a knowledge service a host +mounts itself answers for its own sources. + +**A service that is not enabled saves no source either** (ADR-0161 K8, the +dated note on item 12). While a runtime-mounted service is held, its own +verdict (`admitAuthoredSource`) refuses an author's save `503 +SERVICE_UNAVAILABLE` at every door that saves one — the doors above and +`POST /packages/:id/move-in` of a package-less source — ahead of every gate, +and nothing is written: the source would be held, never built, synced or +searched. The refusal names the half that is missing: with no embedder, "save +an embedder provider, restart, then save"; where the deployment's store is not +mounted, that the boot's log names the reason, and "mount the deployment's +knowledge store, restart, then save"; both when both are missing. A duplicate +is not refused for it, since its copies are held like every other source, and +the engine's own writes are admitted, so a shipped source still installs and +is held. #### One name, one source diff --git a/packages/metadata-core/src/knowledge-source-save-admission.test.ts b/packages/metadata-core/src/knowledge-source-save-admission.test.ts index b809d3c2605..09ef243f7b7 100644 --- a/packages/metadata-core/src/knowledge-source-save-admission.test.ts +++ b/packages/metadata-core/src/knowledge-source-save-admission.test.ts @@ -53,6 +53,11 @@ describe('authoredKnowledgeSourceRefusal — source admission at save', () => { // The reason, the item, and the remedy in its order: the token, a restart, then the save. expect(refusal?.message).toMatch(/^The knowledge source 'faq' was not saved: this deployment runs no knowledge service/); expect(refusal?.message).toMatch(/requires: \['knowledge'\].*restart the server.*then save the source again/); + // [ADR-0161 K8, note on item 12] The service the token mounts serves only with an embedder, + // whose settings it contributes: a provider is saved after that restart, then one more. + expect(refusal?.message).toMatch( + /restart the server, save an embedder provider and restart again, then save the source again: /, + ); } } // An item with no name is still refused, without a name to quote. @@ -125,6 +130,8 @@ describe('authoredKnowledgeSourceRefusal — source admission at save', () => { expect(none).toMatchObject({ status: 503, code: 'SERVICE_UNAVAILABLE' }); expect(none?.message).toMatch(/^The knowledge source 'faq' cannot be copied: this deployment runs no knowledge service/); expect(none?.message).toMatch(/requires: \['knowledge'\].*restart the server, then duplicate the package again: /); + // Unchanged for this door (ADR-0161 K8, note on item 12): a duplicate is not refused while held. + expect(none?.message).not.toContain('embedder'); const scoped = await ask(() => knowledgeServiceNotInScope('northwind')); expect(scoped).toMatchObject({ status: 503, code: 'SERVICE_UNAVAILABLE' }); diff --git a/packages/metadata-core/src/knowledge-source-save-admission.ts b/packages/metadata-core/src/knowledge-source-save-admission.ts index 3f33d1411f5..6cea5fff70b 100644 --- a/packages/metadata-core/src/knowledge-source-save-admission.ts +++ b/packages/metadata-core/src/knowledge-source-save-admission.ts @@ -45,8 +45,9 @@ * boot, from `requires: ['knowledge']` in the stack. So an author's save there * would be kept and never served, and the Studio, REST or MCP author would * never see the boot line that says so. The refusal tells them at the save, - * with the remedy: declare the token, restart, then save - * ({@link noKnowledgeServiceRefusal}). `503 SERVICE_UNAVAILABLE`: the save + * with the remedy: declare the token, restart, save an embedder provider (a + * service the token mounts serves only with one: ADR-0161 K8, the dated note + * on item 12), restart again, then save ({@link noKnowledgeServiceRefusal}). `503 SERVICE_UNAVAILABLE`: the save * needs a service this deployment does not run, the code the knowledge * service answers at index and search for a store that is not mounted. * Nothing was written. @@ -63,6 +64,15 @@ * code. The platform's `KnowledgeServicePlugin` registers a plain instance, * so its own service never reaches this. * + * ## A knowledge service the runtime composed, not enabled at this boot + * + * Refused too, by the service's own verdict + * ({@link KnowledgeSourceSaveAdmitter.admitAuthoredSource}): its sources are + * held, so an author's save would be kept and never served (ADR-0161 K8, the + * dated note on item 12). The refusal names the half that is missing, the + * embedder or the deployment's store, and its remedy. A duplicate is not + * refused for it, and the engine's own write is not asked. + * * ## The door's wording * * The sentences name the save's remedy (save the source again, nothing was @@ -177,16 +187,23 @@ function unavailable(why: string, door: KnowledgeSourceSaveDoor): AuthoredKnowle */ function noKnowledgeServiceRefusal(item: unknown, door: KnowledgeSourceSaveDoor): AuthoredKnowledgeSourceRefusal { const wording = DOOR_WORDING[door]; - return { - status: 503, - code: 'SERVICE_UNAVAILABLE', - message: - `${sourceSubject(item)} ${wording.notDone}: this deployment runs no knowledge service, so nothing would ` + - "load, index or search it. Add requires: ['knowledge'] to the stack (or, on a host that composes its own " + - 'kernel, KnowledgeServicePlugin from @objectstack/service-knowledge and a knowledge store), restart the ' + - `server, then ${wording.retry}: the service is mounted when the server starts, never while it runs.` + - wording.nothingDone, - }; + const prefix = + `${sourceSubject(item)} ${wording.notDone}: this deployment runs no knowledge service, so nothing would ` + + "load, index or search it. Add requires: ['knowledge'] to the stack (or, on a host that composes its own " + + 'kernel, KnowledgeServicePlugin from @objectstack/service-knowledge and a knowledge store), restart the ' + + 'server, '; + // [ADR-0161 K8, the dated note on item 12] A service the stack's + // `requires` mounts is enabled only with an embedder, whose settings that + // service contributes: so a save's remedy also saves a provider and + // restarts once more, or the source would be refused again (held). The + // duplicate's sentence is unchanged: that door is not refused while held. + const steps = + door === 'save' + ? `save an embedder provider and restart again, then ${wording.retry}: the service is mounted when the ` + + 'server starts, never while it runs, and the one the stack requires serves only once an embedder is ' + + 'configured.' + : `then ${wording.retry}: the service is mounted when the server starts, never while it runs.`; + return { status: 503, code: 'SERVICE_UNAVAILABLE', message: prefix + steps + wording.nothingDone }; } /** diff --git a/packages/rest/src/meta-write-door-capability-enumeration.test.ts b/packages/rest/src/meta-write-door-capability-enumeration.test.ts index 44b536cc06e..f27ce0ece9f 100644 --- a/packages/rest/src/meta-write-door-capability-enumeration.test.ts +++ b/packages/rest/src/meta-write-door-capability-enumeration.test.ts @@ -738,6 +738,26 @@ describe('ADR-0161 K4 — the save door asks the knowledge service to admit the } }); + /** + * [ADR-0161 K8, the dated note on item 12: ◆ an author's save] The service's + * verdict while it is not enabled (composed in `@objectstack/service-knowledge`, + * which this package does not depend on; its sentence is pinned there and at the + * runtime's doors): this transport answers its status, code and sentence as sent. + */ + it('a knowledge service read as not enabled: its 503 SERVICE_UNAVAILABLE verdict is answered as sent, and nothing is saved', async () => { + const HELD = { + admitted: false, status: 503, code: 'SERVICE_UNAVAILABLE', + message: 'The knowledge source "faq" was not saved: the knowledge service is not enabled on this deployment: no embedder is ' + + 'configured (no embedder provider is saved). A source saved now would be held, never built, synced or searched. ' + + 'To have it served, save an embedder provider, restart, then save the source. Nothing was written.', + }; + const stack = bootWith(AUTHOR, () => HELD); + const out = await stack.knock(asSource(SAVE)); + expect(out.status).toBe(503); + expect(out.body).toMatchObject({ error: { code: 'SERVICE_UNAVAILABLE', message: HELD.message } }); + expect(stack.calls.saveMetaItem).toBe(0); + }); + it('CONTROL: an admitted source is saved; a registered knowledge service that is not the platform\'s (no admitAuthoredSource) lets the save go on', async () => { const admitted = bootWith(AUTHOR, () => ({ admitted: true })); expect((await admitted.knock(asSource(SAVE))).status).not.toBe(403); diff --git a/packages/runtime/src/domains/mcp-authoring.test.ts b/packages/runtime/src/domains/mcp-authoring.test.ts index 22dbe31248e..3c0dbc0f4cd 100644 --- a/packages/runtime/src/domains/mcp-authoring.test.ts +++ b/packages/runtime/src/domains/mcp-authoring.test.ts @@ -59,6 +59,8 @@ import { type OpenAuthoringReadGateSources, } from './mcp-authoring.js'; import { createInProcessAuthoringRegistry } from '../in-process-authoring.js'; +// [ADR-0161 K8, note on item 12] The platform's service, read as not enabled, for the draft save's held refusal. +import { KnowledgeService } from '@objectstack/service-knowledge'; import { resolveExecutionContext } from '../security/resolve-execution-context.js'; import { bindCatalogFromTables } from '../security/security-catalog.testkit.js'; @@ -1085,6 +1087,26 @@ describe('[ADR-0161 K4] save_metadata_draft asks the knowledge service to admit expect((await call(none.authoring, 'save_metadata_draft', { type: 'flow', name: 'lead_intake', item: FLOW_DRAFT, package: 'com.acme.crm' })).isError).toBe(false); }); + /** + * [ADR-0161 K8, the dated note on item 12: ◆ an author's save, point 4] A + * service the runtime composed and this boot read as not enabled holds its + * sources: the draft save is refused with the service's own sentence, + * naming the missing half and its remedy, and nothing is written. + */ + it('a knowledge service held for want of an embedder: the draft save is refused 503 SERVICE_UNAVAILABLE naming the remedy, and nothing is written', async () => { + const knowledge = new KnowledgeService({ enabledByConfiguration: true }); + knowledge.settleEnablement({ embedder: 'no embedder provider is saved' }); + const { authoring, served } = await authorThroughDomain({ systemPermissions: BOTH, knowledge }); + const refused = await save(authoring); + expect(refused.isError).toBe(true); + expect(refused.body.error).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); + expect(refused.body.error.message).toMatch( + /^The knowledge source "faq" was not saved: the knowledge service is not enabled on this deployment: no embedder is configured \(no embedder provider is saved\)\./, + ); + expect(refused.body.error.message).toContain('To have it served, save an embedder provider, restart, then save the source. Nothing was written.'); + expect(served.protocolCalls.filter((c: any) => c[0] === 'saveMetaItem')).toEqual([]); + }); + it('CONTROL: an admitted source is saved; another type is never asked about; a registered knowledge service that is not the platform\'s judges nothing', async () => { const admitAuthoredSource = vi.fn(async () => ({ admitted: true })); const { authoring, served } = await authorThroughDomain({ systemPermissions: BOTH, knowledge: { admitAuthoredSource } }); diff --git a/packages/runtime/src/domains/meta-save-capability-gate.test.ts b/packages/runtime/src/domains/meta-save-capability-gate.test.ts index 04cdc07fc63..a7ae26d64fa 100644 --- a/packages/runtime/src/domains/meta-save-capability-gate.test.ts +++ b/packages/runtime/src/domains/meta-save-capability-gate.test.ts @@ -42,6 +42,8 @@ import { describe, it, expect, vi } from 'vitest'; import { ObjectKernel, ServiceLifecycle } from '@objectstack/core'; import { HttpDispatcher } from '../http-dispatcher.js'; +// [ADR-0161 K8, note on item 12] The platform's service, read as not enabled, for the save's held refusal. +import { KnowledgeService } from '@objectstack/service-knowledge'; // Paid at collection, not inside a case's clock (check:test-source-alias). import '@objectstack/metadata'; @@ -364,6 +366,30 @@ describe('ADR-0161 K4 — dispatcher PUT asks the knowledge service to admit the expect(none.saveMetaItem).not.toHaveBeenCalled(); }); + /** + * [ADR-0161 K8, the dated note on item 12: ◆ an author's save, point 4] The + * platform's service, read as not enabled at this boot, answers the save's + * verdict itself: refused 503, naming the half that is missing, and nothing + * is saved. Here the deployment's store is the missing half. + */ + it('a knowledge service held because the deployment\'s store is not mounted: refused 503 naming the store and its remedy, and nothing is saved', async () => { + const saveMetaItem = vi.fn(async ({ name }: any) => ({ success: true, name })); + const knowledge = new KnowledgeService({ enabledByConfiguration: true, deploymentAdapter: 'turso' }); + knowledge.settleEnablement({ store: 'turso' }); + const kernel = new ObjectKernel({ gracefulShutdown: false, skipSystemValidation: true }); + kernel.registerService('protocol', { saveMetaItem }); + kernel.registerService('knowledge', knowledge); + const res = await new HttpDispatcher(kernel as any).handleMetadata('/knowledge_source/faq', ctx(AUTHOR), 'PUT', copy(SOURCE)); + expect(res.response?.status).toBe(503); + expect(res.response?.body?.error?.code).toBe('SERVICE_UNAVAILABLE'); + const message: string = res.response?.body?.error?.message; + expect(message).toMatch(/^The knowledge source "faq" was not saved: the knowledge service is not enabled on this deployment: /); + expect(message).toContain("the deployment's knowledge store 'turso' is not mounted, for the reason the boot's log names"); + expect(message).toContain("To have it served, mount the deployment's knowledge store, restart, then save the source. Nothing was written."); + expect(message).not.toContain('embedder'); + expect(saveMetaItem).not.toHaveBeenCalled(); + }); + it('CONTROL: an admitted source is saved; a registered knowledge service that is not the platform\'s (no admitAuthoredSource) lets the save go on', async () => { const admitted = bootWith(() => ({ admitted: true })); expect((await put(admitted, AUTHOR)).response?.status).toBe(200); diff --git a/packages/runtime/src/domains/packages-knowledge-source-gate.test.ts b/packages/runtime/src/domains/packages-knowledge-source-gate.test.ts index 94a76ce887b..17b07e01a3e 100644 --- a/packages/runtime/src/domains/packages-knowledge-source-gate.test.ts +++ b/packages/runtime/src/domains/packages-knowledge-source-gate.test.ts @@ -573,3 +573,124 @@ describe('/packages duplicate and /meta save on a real kernel running KnowledgeS }); } }); + +/** + * [ADR-0161 K8, the dated note on item 12: ◆ an author's save, points 1 and 4] + * A real kernel runs the platform's `KnowledgeServicePlugin` as the runtime + * composes it (`runtimeComposed`), with no embedder: the boot reads the + * service as not enabled and holds its sources. Each door that saves an + * author's source refuses it with the service's sentence, naming the missing + * half and the ruled remedy, and writes nothing: the `/meta` save and the + * package move-in of a package-less source (an author's save, with the save's + * remedy). The package duplicate is not refused for it: its copies are held + * like every other source. The engine's own call is not asked. + */ +describe('a runtime-composed knowledge service held for want of an embedder: the save doors refuse, the duplicate copies', () => { + const ARTICLE = { name: 'article', fields: { id: { type: 'text' }, title: { type: 'text' }, body: { type: 'textarea' } } }; + const SOURCE = { name: 'faq', label: 'FAQ', source: { kind: 'object', object: 'article', content: ['body'] } }; + const HELD = /^The knowledge source "faq" was not saved: the knowledge service is not enabled on this deployment: no embedder is configured \(/; + const REMEDY = 'To have it served, save an embedder provider, restart, then save the source. Nothing was written.'; + + async function bootHeld(runtimeComposed: { declared: boolean } | undefined) { + const s = make({ items: [SOURCE] }); + const saveMetaItem = vi.fn(async ({ name }: any) => ({ success: true, name })); + // The loose source the move-in reads before it moves it. + const getMetaItem = vi.fn(async () => ({ item: JSON.parse(JSON.stringify(SOURCE)) })); + Object.assign(s.protocol, { saveMetaItem, getMetaItem }); + Object.assign(s.kernelServices.objectql, { + getSchema: (name: string) => (name === 'article' ? ARTICLE : undefined), + find: vi.fn(async () => []), + }); + const fields = Object.keys(ARTICLE.fields); + const security = { getQueryableFields: vi.fn(async () => fields), getReadableFields: vi.fn(async () => fields) }; + const doubles: Record = { ...s.kernelServices, security }; + const host: Plugin = { + name: 'test.held-knowledge.host', + version: '1.0.0', + type: 'standard', + async init(ctx: PluginContext) { + for (const [name, service] of Object.entries(doubles)) ctx.registerService(name, service); + }, + } as Plugin; + const kernel = new ObjectKernel({ gracefulShutdown: false, skipSystemValidation: true }); + await kernel.use(host); + await kernel.use(new KnowledgeServicePlugin({ enableEventSync: false, buildAtBoot: false, ...(runtimeComposed ? { runtimeComposed } : {}) })); + await kernel.bootstrap(); + const knowledge = kernel.getService('knowledge'); + const dispatcher = new HttpDispatcher(kernel as any); + const moveIn = (caller: any) => + dispatcher.handlePackages('/pkg-a/move-in', 'POST', { items: [{ type: 'knowledge_source', name: 'faq' }] }, {}, caller); + return { + ...s, kernel, knowledge, saveMetaItem, + save: (caller: any) => dispatcher.handleMetadata('/knowledge_source/faq', caller, 'PUT', JSON.parse(JSON.stringify(SOURCE))), + moveIn, + duplicate: (caller: any) => dispatcher.handlePackages('/pkg-a/duplicate', 'POST', { targetPackageId: 'pkg-b' }, {}, caller), + }; + } + + it('the boot reads the service as held: no embedder', async () => { + const stack = await bootHeld({ declared: false }); + try { + expect(stack.knowledge.heldReading()?.embedder).toBeDefined(); + } finally { + await stack.kernel.shutdown(); + } + }); + + it('PUT /meta/knowledge_source/:name: refused 503 SERVICE_UNAVAILABLE with the ruled remedy, and nothing is saved', async () => { + const stack = await bootHeld({ declared: false }); + try { + const r = await stack.save(authed(BOTH)); + expect(r.response?.status).toBe(503); + expect(r.response?.body?.error?.code).toBe('SERVICE_UNAVAILABLE'); + expect(r.response?.body?.error?.message).toMatch(HELD); + expect(r.response?.body?.error?.message).toContain(REMEDY); + expect(stack.saveMetaItem).not.toHaveBeenCalled(); + } finally { + await stack.kernel.shutdown(); + } + }); + + it('POST /packages/:id/move-in of a package-less source is an author\'s save: refused with the save\'s remedy, and nothing is moved', async () => { + const stack = await bootHeld({ declared: false }); + try { + const r = await stack.moveIn(authed(BOTH)); + expect(r.response?.status).toBe(503); + expect(r.response?.body?.error?.code).toBe('SERVICE_UNAVAILABLE'); + const message: string = r.response?.body?.error?.message; + expect(message.startsWith('Nothing was moved into package "pkg-a": knowledge_source \'faq\' — The knowledge source "faq" was not saved: the knowledge service is not enabled'), message).toBe(true); + expect(message).toContain(REMEDY); + expect(stack.protocol.moveIntoPackage).not.toHaveBeenCalled(); + // The engine's own move is not asked. + expect((await stack.moveIn(system())).response?.status).not.toBe(503); + expect(stack.protocol.moveIntoPackage).toHaveBeenCalledTimes(1); + } finally { + await stack.kernel.shutdown(); + } + }); + + it('POST /packages/:id/duplicate is not refused for the held state: the package is copied', async () => { + const stack = await bootHeld({ declared: false }); + try { + const r = await stack.duplicate(authed(BOTH)); + expect(r.response?.status).not.toBe(503); + expect(r.response?.body?.error?.message ?? '').not.toContain('not enabled'); + expect(stack.protocol.duplicatePackage).toHaveBeenCalled(); + } finally { + await stack.kernel.shutdown(); + } + }); + + it('CONTROL: a knowledge service a host mounts itself (no runtimeComposed) is never held, and the same save and move-in go on', async () => { + const stack = await bootHeld(undefined); + try { + expect(stack.knowledge.heldReading()).toBeUndefined(); + expect((await stack.save(authed(BOTH))).response?.status).toBe(200); + expect(stack.saveMetaItem).toHaveBeenCalledTimes(1); + expect((await stack.moveIn(authed(BOTH))).response?.status).not.toBe(503); + expect(stack.protocol.moveIntoPackage).toHaveBeenCalledTimes(1); + } finally { + await stack.kernel.shutdown(); + } + }); +}); diff --git a/packages/services/service-knowledge/src/__tests__/knowledge-settings-contribution.test.ts b/packages/services/service-knowledge/src/__tests__/knowledge-settings-contribution.test.ts index 2baab52802c..1a391754a48 100644 --- a/packages/services/service-knowledge/src/__tests__/knowledge-settings-contribution.test.ts +++ b/packages/services/service-knowledge/src/__tests__/knowledge-settings-contribution.test.ts @@ -977,6 +977,84 @@ describe('[ADR-0161 K8, note on item 12] a runtime-composed knowledge service is expect(knowledge.knowledgeTools().definitions.map((d) => d.name)).toEqual(['search_knowledge']); }); + /** + * [◆ an author's save, point 4, build note 3] While held, an author's save of + * a source is refused 503 at the service's own verdict, which every save door + * answers, naming the half that is missing and the ruled remedy. The engine's + * own write is admitted (a shipped source installs and is held); the package + * duplicate is not refused for it. + */ + describe("held: an author's save is refused, naming the half that is missing", () => { + const AUTHOR = { userId: 'u_author', positions: [], permissions: [], systemPermissions: [], isSystem: false } as never; + const ITEM = { name: 'notes', label: 'Notes', source: { kind: 'object', object: 'doc', content: ['body'] } }; + type Verdict = { admitted: boolean; status?: number; code?: string; message?: string }; + const admit = (knowledge: KnowledgeService, ctx: unknown = AUTHOR, door?: 'save' | 'duplicate') => + knowledge.admitAuthoredSource(ITEM, ctx as never, door ? { door } : undefined) as Promise; + const HELD = 'the knowledge service is not enabled on this deployment'; + + it('no embedder: 503 SERVICE_UNAVAILABLE with "save an embedder provider, restart, then save"; the engine\'s write is admitted', async () => { + const { knowledge, calls } = await bootComposed({ runtimeComposed: { declared: false } }); + const verdict = await admit(knowledge); + expect(verdict).toMatchObject({ admitted: false, status: 503, code: 'SERVICE_UNAVAILABLE' }); + expect(verdict.message).toMatch(new RegExp(`^The knowledge source "notes" was not saved: ${HELD}: no embedder is configured \\(`)); + expect(verdict.message).toContain('To have it served, save an embedder provider, restart, then save the source. Nothing was written.'); + expect(verdict.message).not.toContain('knowledge store'); + // The engine's own write (a package install, a seed) is admitted, and nothing calls the store. + expect(await admit(knowledge, SYSTEM)).toEqual({ admitted: true }); + // A save with no identity is refused for the held state first: it names the deployment, not the caller. + expect((await admit(knowledge, undefined)).message).toContain(HELD); + expect(calls).toEqual([]); + }); + + it('the package duplicate is not refused for the held state: it goes on to the gates', async () => { + const { knowledge } = await bootComposed({ runtimeComposed: { declared: false } }); + const verdict = await admit(knowledge, AUTHOR, 'duplicate'); + expect(verdict.message ?? '').not.toContain(HELD); + // An explicit `save` door is the save's. + expect((await admit(knowledge, AUTHOR, 'save')).message).toContain(HELD); + }); + + it('a provider saved without its key: the ruled words, with the cause beside them', async () => { + stubProvider(); + const { knowledge } = await bootComposed({ runtimeComposed: { declared: false }, saved: { embedder_provider: 'siliconflow' } }); + const verdict = await admit(knowledge); + expect(verdict).toMatchObject({ status: 503, code: 'SERVICE_UNAVAILABLE' }); + expect(verdict.message).toContain('requires an API key'); + expect(verdict.message).toContain('save an embedder provider, restart, then save the source.'); + }); + + it("the deployment's store not mounted: says so, that the boot's log names the reason, and to mount it", async () => { + const { knowledge } = await bootComposed({ + runtimeComposed: { declared: false }, + host: { embedder: hostEmbedder(), phase: 'init' }, + storeMounted: false, + deploymentAdapter: 'turso', + }); + const verdict = await admit(knowledge); + expect(verdict).toMatchObject({ status: 503, code: 'SERVICE_UNAVAILABLE' }); + expect(verdict.message).toContain("the deployment's knowledge store 'turso' is not mounted, for the reason the boot's log names"); + expect(verdict.message).toContain("To have it served, mount the deployment's knowledge store, restart, then save the source."); + expect(verdict.message).not.toContain('embedder'); + }); + + it('both halves missing: both, in one remedy', async () => { + const { knowledge } = await bootComposed({ runtimeComposed: { declared: false }, storeMounted: false, deploymentAdapter: 'turso' }); + const verdict = await admit(knowledge); + expect(verdict.message).toContain('no embedder is configured ('); + expect(verdict.message).toContain("knowledge store 'turso' is not mounted"); + expect(verdict.message).toContain( + "To have it served, save an embedder provider and mount the deployment's knowledge store, restart, then save the source.", + ); + }); + + it('controls — enabled, and a plugin a host mounts with no embedder: never refused for a held state', async () => { + const enabled = await bootComposed({ runtimeComposed: { declared: true }, host: { embedder: hostEmbedder(), phase: 'kernel:ready' } }); + expect((await admit(enabled.knowledge)).message ?? '').not.toContain(HELD); + const hostMounted = await bootComposed({}); + expect((await admit(hostMounted.knowledge)).message ?? '').not.toContain(HELD); + }); + }); + it('a plugin a host mounts itself, no embedder: as before — not held, the tool handed out, the stand-in refuses at the store', async () => { const { knowledge, heldLines, lines, calls } = await bootComposed({}); expect(knowledge.heldReading()).toBeUndefined(); diff --git a/packages/services/service-knowledge/src/knowledge-service.ts b/packages/services/service-knowledge/src/knowledge-service.ts index 9dcb4cc9cd9..683f46c1a7a 100644 --- a/packages/services/service-knowledge/src/knowledge-service.ts +++ b/packages/services/service-knowledge/src/knowledge-service.ts @@ -4183,6 +4183,15 @@ export class KnowledgeService implements IKnowledgeService { * question, the save is refused `503 SERVICE_UNAVAILABLE`. The system * context is admitted, as it bypasses the gates at search. * + * [ADR-0161 K8, the dated note on item 12] While this boot read a service + * the runtime composed as not enabled ({@link settleEnablement}), an + * author's save is refused `503 SERVICE_UNAVAILABLE` ahead of the identity + * check and every gate, naming each half that is missing and its remedy + * ({@link heldSourceSaveText}): a source saved then would be held and + * never served. The package duplicate is not refused for it (its copies + * are held like every other source), and the engine's own write is + * admitted, so a shipped source still installs and is held. + * * `options.door` words the sentences for the door that asks (the gates and * the status and code are the same at both): `save`, the default, for a * door that saves one item; `duplicate` for the package duplicate, which @@ -4228,6 +4237,15 @@ export class KnowledgeService implements IKnowledgeService { `${named}: whether you may search object '${object}' could not be judged (${why}), and an author must be ` + `allowed to search what their source indexes.${wording.tryAgain}`, ); + // [ADR-0161 K8, the dated note on item 12, ◆ an author's save and point 4] + // A service the runtime composed and that this boot read as not enabled + // holds every source, so an author's save would be kept and never served: + // refused at each door that saves one, naming the half that is missing. + // The package duplicate (`door: 'duplicate'`) is not refused for it, and + // the engine's own write was admitted above. + if (this.held && options?.door !== 'duplicate') { + return refuse(503, 'SERVICE_UNAVAILABLE', `${named}: ${heldSourceSaveText(this.held)}`); + } if (!ctx) return refuse(403, 'PERMISSION_DENIED', `${named}: ${wording.act} carries no identity, and a missing identity is not a grant of authority.`); // [ADR-0161 K4 ◆] A source that names no adapter where this deployment // cannot place one is refused at its save as at its registration, so the @@ -4790,6 +4808,34 @@ export function heldSourcesText(reading: KnowledgeHeldReading): string { ); } +/** + * [ADR-0161 K8, the dated note on item 12, point 4] Why an author's save of a + * `knowledge_source` is refused while the service is not enabled, after the + * source's subject: each half that is missing, and the ruled remedy for it — + * "save an embedder provider, restart, then save" with no embedder, "mount the + * deployment's knowledge store, restart, then save" where the store is not + * mounted (whose reason the boot's log names), both when both are missing. + * The embedder half carries its cause (a provider saved without its key, for + * example) in parentheses, beside the ruled words. + */ +function heldSourceSaveText(reading: KnowledgeHeldReading): string { + const missing: string[] = []; + const remedy: string[] = []; + if (reading.embedder !== undefined) { + missing.push(`no embedder is configured (${reading.embedder})`); + remedy.push('save an embedder provider'); + } + if (reading.store !== undefined) { + missing.push(`the deployment's knowledge store '${reading.store}' is not mounted, for the reason the boot's log names`); + remedy.push("mount the deployment's knowledge store"); + } + return ( + `the knowledge service is not enabled on this deployment: ${missing.join(', and ')}. A source saved now would be ` + + `held, never built, synced or searched. To have it served, ${remedy.join(' and ')}, restart, then save the source. ` + + 'Nothing was written.' + ); +} + /** * [ADR-0161 K4 ◆] The refusal of a source that names no adapter where several * are registered and none is the deployment's: ADR-0112 `VALIDATION_ERROR` diff --git a/packages/spec/src/migrations/entries/semantic/19.knowledge-source-save-refused-while-knowledge-service-not-enabled.ts b/packages/spec/src/migrations/entries/semantic/19.knowledge-source-save-refused-while-knowledge-service-not-enabled.ts new file mode 100644 index 00000000000..eb2609daa8a --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/19.knowledge-source-save-refused-while-knowledge-service-not-enabled.ts @@ -0,0 +1,34 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// The upgrade-guide entry of the save refusal for a `knowledge_source` while +// the knowledge service the runtime composed is not enabled at this boot +// (ADR-0161 K8, the dated note on item 12: an author's save, points 1 and 4). +// Notice only: the schema is unchanged, no key is removed and no D2 conversion +// exists, because the fix is the deployment's configuration, never the item. +export const entry: SemanticMigration = { + id: 'knowledge-source-save-refused-while-knowledge-service-not-enabled', + surface: + 'A `knowledge_source` saved by an author (Studio and REST `PUT /meta/knowledge_source/:name`, the runtime ' + + 'dispatcher\'s `/meta` save, the MCP `save_metadata_draft` tool, and `POST /packages/:id/move-in` of a ' + + 'package-less source) on a self-hosted deployment whose knowledge service the runtime mounts and which, at ' + + 'boot, has no embedder or no deployment knowledge store mounted; and the save on a deployment that runs no ' + + 'knowledge service, whose remedy now also saves an embedder provider', + replacement: + 'Save an embedder provider on the AI settings page (or register an `embedder` service from a plugin) and, where ' + + 'the boot log says the deployment\'s knowledge store is not mounted, mount it; restart the server, then save ' + + 'the source. Where no knowledge service runs: add `requires: [\'knowledge\']` to the stack, restart, save an ' + + 'embedder provider, restart again, then save the source', + reason: + 'Such a save used to be accepted while every source was held: kept, listed, and never built, synced or ' + + 'searched, so the author saw a saved source that nothing served. It is now refused at the save, 503 ' + + 'SERVICE_UNAVAILABLE, naming the half that is missing (the embedder, the deployment\'s store, or both) and the ' + + 'fix; nothing is written. The engine\'s own writes (a package install, a seed, a migration) are not refused, ' + + 'so a shipped source still installs and is held; a package duplicate is not refused for it; a knowledge ' + + 'service a host mounts itself is unchanged; stored items are not rewritten or refused at boot.', + acceptanceCriteria: + 'On a deployment that should serve knowledge sources, the boot logs no line saying the knowledge service is not ' + + 'enabled, and an author\'s source save is accepted as before. On a deployment that should not, no author saves ' + + 'a knowledge_source; one a package brings installs and is held.', +}; From 1ec1c93f1340b34cc4270a8f4dd8e85070833289 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 13:23:22 +0000 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20copy=20the=20claim's=20Clause-?= =?UTF-8?q?=E2=91=A1=20line=20into=20the=20changeset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Claude-Session: https://claude.ai/code/session_01QsR2jREKp1JAmqK54ADBYS Co-authored-by: Claude --- .changeset/38369-knowledge-save-refused-while-held.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/38369-knowledge-save-refused-while-held.md b/.changeset/38369-knowledge-save-refused-while-held.md index 96dd92c1fd2..49338060b9c 100644 --- a/.changeset/38369-knowledge-save-refused-while-held.md +++ b/.changeset/38369-knowledge-save-refused-while-held.md @@ -6,7 +6,7 @@ feat(service-knowledge)!: an author's knowledge source save is refused while the knowledge service is not enabled -Clause-②: no (narrowing) +Clause-②: no (narrowing) — a save door that answered 200 while the service was held now refuses with 503 naming each missing half; no key added Metadata-protocol: narrowed