Skip to content

Commit 9d419f7

Browse files
committed
revert(objectql): split item 4 (the first knowledge_source saved with no service) to its own card
The save-time warn, the reworded boot line and their tests leave this change; row 3a's boot line and behaviour are main's again. The full state is kept on claude/area-o1C-37306-item4 (fa2b538). Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
1 parent fa2b538 commit 9d419f7

6 files changed

Lines changed: 7 additions & 168 deletions

File tree

‎.changeset/37306-knowledge-source-adapter-optional.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
'@objectstack/service-knowledge': minor
55
'@objectstack/cli': minor
66
'@objectstack/verify': minor
7-
'@objectstack/objectql': minor
87
---
98

109
feat(knowledge): a knowledge source needs no `adapter` and no `requires: ['knowledge']` — it binds to the deployment's store, and shipping one mounts the service
@@ -19,8 +18,7 @@ A `knowledge_source` may leave out `adapter` (ADR-0161 K4), and a stack that shi
1918
- **`@objectstack/core`.** `stackItemCapabilities(stack)` answers the tokens a stack's items mount (top level, then each package body), and `resolveServedCapabilities` takes them as `itemTokens`: appended after the declared tokens, read as declared (a provider that cannot be built fails the boot as for `requires`), and reported in a new `byItems` set. `resolveCapabilityArgument('knowledge', …)` now answers `{ deploymentAdapter }`, the adapter id of the store `resolveCapabilityExtras` mounts for the same database.
2019
- **`@objectstack/cli` and `@objectstack/verify`.** `os serve` / `os dev` / `os start` and `bootStack` read the item tokens, and hand the knowledge service the deployment's store (`pgvector` on PostgreSQL, `turso` otherwise).
2120
- **`@objectstack/service-knowledge`.** `KnowledgeServicePlugin` and `KnowledgeService` take a `deploymentAdapter` option: the runtime's mark of the store it mounted. It is a service option, not an adapter-contract member, and `registerAdapter` is unchanged: an adapter application code registers is never the deployment's by registering. A source that names no adapter is placed where it is used (`adapterIdFor`, `adapterFor`), so one registered in code before its store started finds it. With several adapters registered and none the deployment's, such a source is refused at registration (both doors) and at its save, `400 VALIDATION_ERROR`, naming the adapters; registered first and left unplaceable by adapters that came later, it answers the same refusal at index and search, and the boot names it once at `error`. With no adapter registered it answers `503 SERVICE_UNAVAILABLE`, naming the deployment's store when one was marked. The `index_status` action reports the adapter a source is placed on.
22-
- **`@objectstack/objectql`.** The first `knowledge_source` saved at run time on a deployment that mounted no knowledge service is kept and not served: no service is mounted while the process runs, and the boot composes its services before it reads the metadata store. Its save is now said once at `warn`, naming the fix (`requires: ['knowledge']`, then a restart), and the boot line for such items names the same fix.
2321

24-
Unchanged: a source that names an adapter that is not mounted answers `SERVICE_UNAVAILABLE` as before; a host that mounts `KnowledgeServicePlugin` itself keeps its own, gets no store from the runtime and no deployment's mark; without a source or the token nothing is mounted.
22+
Unchanged: a source that names an adapter that is not mounted answers `SERVICE_UNAVAILABLE` as before; a host that mounts `KnowledgeServicePlugin` itself keeps its own, gets no store from the runtime and no deployment's mark; without a source or the token nothing is mounted; an item saved at run time on a deployment that mounted no knowledge service is reported at boot exactly as before.
2523

2624
What to do: nothing. A source that names `adapter: 'turso'` or `adapter: 'pgvector'` keeps working; drop the key to let the deployment choose, and drop `knowledge` from `requires` in an app that ships its sources.

‎content/docs/ai/knowledge-rag.mdx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ Which store is mounted depends on the platform database:
4040
- **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.
4141
- **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`.
4242
- **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.
43-
- **The first source saved at run time on a deployment with neither** (in Studio, the metadata API or MCP) is kept and not served: the service is mounted when the server starts, never while it runs, and it is the app's own sources that mount it. The save is logged once at `warn`, and every boot after says it again, naming the fix: add `requires: ['knowledge']` and restart — the next boot loads and builds the source.
4443
- **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.
4544

4645
## Writing a source

‎content/docs/protocol/knowledge.mdx‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,9 @@ starts and follows the type from then on, with no restart:
200200

201201
A deployment that holds a `knowledge_source` item but mounts no knowledge
202202
service says so once at boot, naming the items: nothing loads, indexes or
203-
searches them. Such an item is one the stack does not ship — saved at run time
204-
or installed in a package after the boot — since a shipped one mounts the
205-
service. **The first one saved at run time** on such a deployment is kept and
206-
not served: the service is mounted at boot, never while the process runs, and
207-
the boot composes its services before it reads the metadata store, so a plain
208-
restart does not mount it either. Its save is said once at `warn`, naming the
209-
fix — `requires: ['knowledge']` (or, on a host that composes its own kernel,
210-
`KnowledgeServicePlugin` and a store), then a restart; the next boot loads and
211-
builds it.
203+
searches them until `KnowledgeServicePlugin` and an adapter are mounted. Such
204+
an item is one the stack does not ship — saved at run time or installed in a
205+
package after the boot — since a shipped one mounts the service.
212206

213207
#### Who may author one
214208

‎packages/objectql/src/knowledge-source-unserved.test.ts‎

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { describe, expect, it, vi } from 'vitest';
44
import {
55
UNSERVED_KNOWLEDGE_SOURCES_NAMED,
66
announceUnservedKnowledgeSources,
7-
describeSavedUnservedKnowledgeSource,
87
describeUnservedKnowledgeSources,
9-
watchSavedUnservedKnowledgeSources,
108
} from './knowledge-source-unserved.js';
119

1210
describe('describeUnservedKnowledgeSources — items with no knowledge service (ADR-0161 K4, ADR-0078)', () => {
@@ -22,8 +20,6 @@ describe('describeUnservedKnowledgeSources — items with no knowledge service (
2220
expect(line).toContain('it is loaded, indexed or searched');
2321
expect(line).toContain('@objectstack/service-knowledge');
2422
expect(line).toContain('or remove the item.');
25-
// [ADR-0161 K8 item 12] The stack's own items mount the service, so the remedy for these is the token.
26-
expect(line).toContain("Declare requires: ['knowledge'] in the stack and restart");
2723
});
2824

2925
it(`names at most ${UNSERVED_KNOWLEDGE_SOURCES_NAMED} items, counts the rest, and says a name two packages hold once`, () => {
@@ -85,76 +81,3 @@ describe('announceUnservedKnowledgeSources — once, at warn, never a refusal',
8581
expect(h.warn).toHaveBeenCalledTimes(1);
8682
});
8783
});
88-
89-
/**
90-
* [ADR-0161 row 3b, item 4] The first `knowledge_source` saved at run time on a
91-
* deployment that mounted no knowledge service at boot: no service is mounted
92-
* while the process runs, so the item is kept and not served, and its save is
93-
* told so once, naming what serves it — `requires: ['knowledge']` and a
94-
* restart.
95-
*/
96-
describe('watchSavedUnservedKnowledgeSources — the first item saved at run time with no service', () => {
97-
type Listener = (evt: { type?: string; name?: string; state?: string }) => void;
98-
const host = (services: Record<string, unknown>) => {
99-
const warn = vi.fn();
100-
return {
101-
warn,
102-
host: {
103-
getService: (name: string) => {
104-
if (name in services) return services[name];
105-
throw new Error(`no service ${name}`);
106-
},
107-
logger: { warn },
108-
},
109-
};
110-
};
111-
const signal = () => {
112-
const listeners: Listener[] = [];
113-
const unsubscribe = vi.fn();
114-
return {
115-
listeners,
116-
unsubscribe,
117-
protocol: {
118-
onMetadataMutation: vi.fn((l: Listener) => {
119-
listeners.push(l);
120-
return unsubscribe;
121-
}),
122-
},
123-
emit: (evt: { type?: string; name?: string; state?: string }) => listeners.forEach((l) => l(evt)),
124-
};
125-
};
126-
127-
it('a saved source is said once, at warn: kept, not served, and what serves it after a restart', () => {
128-
const s = signal();
129-
const h = host({ protocol: s.protocol });
130-
const unwatch = watchSavedUnservedKnowledgeSources(h.host);
131-
s.emit({ type: 'knowledge_source', name: 'faq', state: 'active' });
132-
s.emit({ type: 'knowledge_source', name: 'faq', state: 'active' });
133-
expect(h.warn).toHaveBeenCalledTimes(1);
134-
const line = String(h.warn.mock.calls[0][0]);
135-
expect(line).toBe(describeSavedUnservedKnowledgeSource('faq'));
136-
expect(line).toContain("knowledge_source 'faq' was saved");
137-
expect(line).toContain('it is kept and is not loaded, indexed or searched');
138-
expect(line).toContain("Declare requires: ['knowledge'] in the stack and restart");
139-
expect(line).toContain('the next boot then loads and builds it');
140-
unwatch?.();
141-
expect(s.unsubscribe).toHaveBeenCalledOnce();
142-
});
143-
144-
it('says nothing for a draft, a delete or another type', () => {
145-
const s = signal();
146-
const h = host({ protocol: s.protocol });
147-
watchSavedUnservedKnowledgeSources(h.host);
148-
s.emit({ type: 'knowledge_source', name: 'faq', state: 'draft' });
149-
s.emit({ type: 'knowledge_source', name: 'faq', state: 'deleted' });
150-
s.emit({ type: 'object', name: 'faq', state: 'active' });
151-
expect(h.warn).not.toHaveBeenCalled();
152-
});
153-
154-
it('a deployment with a knowledge service watches nothing: the service loads the item itself', () => {
155-
const s = signal();
156-
const h = host({ protocol: s.protocol, knowledge: {} });
157-
expect(watchSavedUnservedKnowledgeSources(h.host)).toBeUndefined();
158-
expect(s.protocol.onMetadataMutation).not.toHaveBeenCalled();
159-
});
160-
});

‎packages/objectql/src/knowledge-source-unserved.ts‎

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
* A statement at `warn`, never a boot refusal: a package may ship a source to
1717
* a deployment that chose not to run retrieval, and its objects, pages and
1818
* flows must keep serving there.
19-
*
20-
* [ADR-0161 K8 item 12] Since a `knowledge_source` the stack ships mounts the
21-
* service by itself, the items this line names are the ones the stack does
22-
* not ship: saved at run time (Studio, the metadata API, MCP) or installed in
23-
* a package after the boot composed its services. A kernel takes no plugin
24-
* once it has booted, and the composition is decided before the metadata
25-
* store is read, so such an item is not served — not now, and not by a plain
26-
* restart either — until the stack asks for the service:
27-
* `requires: ['knowledge']`, then a restart. That is what happens to the
28-
* FIRST item saved at run time on a deployment with no knowledge service
29-
* (ADR-0161 row 3b, item 4): it is kept, and said once at its save, in the
30-
* words {@link describeSavedUnservedKnowledgeSource} gives
31-
* ({@link watchSavedUnservedKnowledgeSources}); every later boot says it again
32-
* here.
3319
*/
3420

3521
import { KNOWLEDGE_SERVICE } from '@objectstack/spec/contracts';
@@ -54,69 +40,12 @@ export function describeUnservedKnowledgeSources(names: readonly string[], servi
5440
return (
5541
`[ObjectQLPlugin] this deployment holds ${count} knowledge_source ${count === 1 ? 'item' : 'items'} ` +
5642
`(${shown}${rest > 0 ? `, and ${rest} more` : ''}), but no '${KNOWLEDGE_SERVICE}' service is registered, so ` +
57-
`${count === 1 ? 'it is' : 'none is'} loaded, indexed or searched. ${UNSERVED_REMEDY} or remove the ` +
43+
`${count === 1 ? 'it is' : 'none is'} loaded, indexed or searched. Add KnowledgeServicePlugin ` +
44+
'(@objectstack/service-knowledge) and a knowledge adapter to the composition, or remove the ' +
5845
`${count === 1 ? 'item' : 'items'}.`
5946
);
6047
}
6148

62-
/**
63-
* What mounts the service for an item the stack does not ship. A source the
64-
* stack ships in `knowledgeSources` mounts it by itself, so the remedy is the
65-
* token (or the plugin, for a host that composes its own kernel).
66-
*/
67-
const UNSERVED_REMEDY =
68-
"Declare requires: ['knowledge'] in the stack and restart — the service and the deployment's store are mounted " +
69-
'at boot, never while the process runs — or, on a host that composes its own kernel, add KnowledgeServicePlugin ' +
70-
'(@objectstack/service-knowledge) and a knowledge store;';
71-
72-
/**
73-
* The statement for one `knowledge_source` saved while no knowledge service is
74-
* mounted (ADR-0161 row 3b, item 4): the item is kept, it is not served, and
75-
* what serves it.
76-
*/
77-
export function describeSavedUnservedKnowledgeSource(name: string): string {
78-
return (
79-
`[ObjectQLPlugin] knowledge_source '${name}' was saved, and no '${KNOWLEDGE_SERVICE}' service is registered on ` +
80-
'this deployment, so it is kept and is not loaded, indexed or searched: a knowledge_source mounts the service ' +
81-
`only when the stack ships it. ${UNSERVED_REMEDY} the next boot then loads and builds it.`
82-
);
83-
}
84-
85-
/** The slice of the metadata protocol {@link watchSavedUnservedKnowledgeSources} reads. */
86-
interface MutationSignal {
87-
onMetadataMutation?(listener: (evt: { type?: string; name?: string; state?: string } | undefined) => void): unknown;
88-
}
89-
90-
/**
91-
* Say {@link describeSavedUnservedKnowledgeSource} at `warn` when a
92-
* `knowledge_source` is saved (not a draft, not a delete) while no knowledge
93-
* service is registered — once per item name for the life of the process.
94-
* Armed at `kernel:bootstrapped`, beside the boot line: every plugin has
95-
* registered its services, and none registers one later. Answers the
96-
* unsubscribe, or `undefined` when there is no protocol or no mutation signal.
97-
*/
98-
export function watchSavedUnservedKnowledgeSources(host: UnservedKnowledgeSourcesHost): (() => void) | undefined {
99-
const service = (name: string): unknown => {
100-
try {
101-
return host.getService(name) ?? undefined;
102-
} catch {
103-
return undefined;
104-
}
105-
};
106-
if (service(KNOWLEDGE_SERVICE) !== undefined) return undefined;
107-
const protocol = service('protocol') as MutationSignal | undefined;
108-
if (typeof protocol?.onMetadataMutation !== 'function') return undefined;
109-
const said = new Set<string>();
110-
const unsubscribe = protocol.onMetadataMutation((evt) => {
111-
if (evt?.type !== KNOWLEDGE_SOURCE_TYPE || evt.state === 'draft' || evt.state === 'deleted') return;
112-
const name = typeof evt.name === 'string' && evt.name !== '' ? evt.name : undefined;
113-
if (name === undefined || said.has(name)) return;
114-
said.add(name);
115-
host.logger?.warn?.(describeSavedUnservedKnowledgeSource(name));
116-
});
117-
return typeof unsubscribe === 'function' ? (unsubscribe as () => void) : undefined;
118-
}
119-
12049
/** What {@link announceUnservedKnowledgeSources} reads of its host. */
12150
export interface UnservedKnowledgeSourcesHost {
12251
/** Resolve a kernel service by name; throws or answers nothing when none is registered. */

‎packages/objectql/src/plugin.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { RecordCurrencyFollowRefusedError } from './record-currency-aggregate-do
4545
// [#22070] The builtin audit stamps are wrapped like a bound hook but registered in code.
4646
import { wrapDeclarativeHook } from './hook-wrappers.js';
4747
import { announceStoredExpressionHoles } from './stored-expression-backfill.js';
48-
import { announceUnservedKnowledgeSources, watchSavedUnservedKnowledgeSources } from './knowledge-source-unserved.js';
48+
import { announceUnservedKnowledgeSources } from './knowledge-source-unserved.js';
4949
import { announceSummaryHoles } from './summary-hole-announce.js';
5050
import { LifecycleService } from './lifecycle/lifecycle-service.js';
5151
import { WORK_CALENDAR_OBJECT, WorkCalendarService, installWorkCalendarHooks } from './work-calendar-service.js';
@@ -1485,10 +1485,6 @@ export class ObjectQLPlugin implements Plugin {
14851485
// service to load them are valid and inert: said once, at warn, never
14861486
// a boot refusal (`knowledge-source-unserved.ts`).
14871487
await announceUnservedKnowledgeSources({ getService: (name) => ctx.getService(name), logger: ctx.logger });
1488-
// [ADR-0161 row 3b, item 4] And the first one saved while the process
1489-
// runs: no service is mounted after boot, so the save is told so.
1490-
const unwatch = watchSavedUnservedKnowledgeSources({ getService: (name) => ctx.getService(name), logger: ctx.logger });
1491-
if (unwatch) this.metadataUnsubscribes.push(unwatch);
14921488
});
14931489
ctx.hook('metadata:reloaded', async (payload?: unknown) => {
14941490
await this.resyncAuthoredHooks(ctx);

0 commit comments

Comments
 (0)