Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .changeset/38369-knowledge-save-refused-while-held.md
Original file line number Diff line number Diff line change
@@ -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) — 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

<!-- adr-0087: registered knowledge-source-save-refused-while-knowledge-service-not-enabled -->

**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.
3 changes: 2 additions & 1 deletion content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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' });
Expand Down
41 changes: 29 additions & 12 deletions packages/metadata-core/src/knowledge-source-save-admission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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 };
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/rest/src/meta-write-door-capability-enumeration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions packages/runtime/src/domains/mcp-authoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 } });
Expand Down
Loading
Loading