Skip to content

Commit 993cd89

Browse files
committed
feat(knowledge): refuse a knowledge_source save that names an adapter the deployment does not mount
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
1 parent 8d34c44 commit 993cd89

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎packages/services/service-knowledge/src/__tests__/knowledge-source-admission.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ describe('admitAuthoredSource — source admission at save, as the authoring pri
530530
.toMatchObject({ admitted: false, code: 'SERVICE_UNAVAILABLE', status: 503 });
531531

532532
const noEngine = new KnowledgeService({});
533+
noEngine.registerAdapter('memory', makeAdapter());
533534
expect(await noEngine.admitAuthoredSource(item('ticket', ['body']), MANAGER))
534535
.toMatchObject({ admitted: false, code: 'SERVICE_UNAVAILABLE', status: 503 });
535536
});
@@ -543,6 +544,51 @@ describe('admitAuthoredSource — source admission at save, as the authoring pri
543544
}
544545
});
545546

547+
// [ADR-0161 K4] The store a source names must be one this deployment mounts:
548+
// a retired adapter ('ragflow') or a typo is refused at the save, not left to
549+
// the boot's error line and a search's 503.
550+
it('a source naming an adapter the deployment does not mount is refused VALIDATION_ERROR / 400, naming the source, the adapter and the mounted ones', async () => {
551+
const { svc } = setup({ sources: [], security: salaryHiddenFromMembers });
552+
for (const adapter of ['ragflow', 'memroy', '']) {
553+
const verdict = await svc.admitAuthoredSource({ ...item('ticket', ['body']), adapter }, MANAGER);
554+
expect(verdict, adapter).toMatchObject({ admitted: false, code: 'VALIDATION_ERROR', status: 400, object: 'ticket' });
555+
const message = (verdict as { message: string }).message;
556+
expect(message).toContain('knowledge source "authored" was not saved');
557+
expect(message).toContain(`names adapter '${adapter}', which this deployment does not mount (mounted: memory)`);
558+
}
559+
// Refused before the gates are asked: an author the field gate would refuse hears the adapter first.
560+
expect(await svc.admitAuthoredSource({ ...item('ticket', ['body', 'salary_note']), adapter: 'ragflow' }, MEMBER))
561+
.toMatchObject({ admitted: false, code: 'VALIDATION_ERROR', status: 400 });
562+
// The engine's own write (a package install) is not judged at this door.
563+
expect(await svc.admitAuthoredSource({ ...item('ticket', ['body']), adapter: 'ragflow' }, SYSTEM)).toEqual({ admitted: true });
564+
});
565+
566+
it('with no adapter mounted, a named one is refused naming none mounted; with two, both are named', async () => {
567+
const bare = new KnowledgeService({ dataEngine: makeEngine([TICKET]) });
568+
expect(await bare.admitAuthoredSource(item('ticket', ['body']), MANAGER)).toMatchObject({
569+
admitted: false, code: 'VALIDATION_ERROR', status: 400,
570+
message: expect.stringContaining("names adapter 'memory', which this deployment does not mount (mounted: none)"),
571+
});
572+
const { svc } = setup({ sources: [] });
573+
svc.registerAdapter('turso', makeAdapter());
574+
expect(await svc.admitAuthoredSource({ ...item('ticket', ['body']), adapter: 'pgvector' }, MANAGER)).toMatchObject({
575+
admitted: false, code: 'VALIDATION_ERROR', status: 400,
576+
message: expect.stringContaining("(mounted: memory, turso)"),
577+
});
578+
});
579+
580+
it('CONTROL: a source naming a mounted adapter, and one that leaves `adapter` out, are admitted', async () => {
581+
const { svc } = setup({ sources: [] });
582+
svc.registerAdapter('turso', makeAdapter());
583+
expect(await svc.admitAuthoredSource(item('ticket', ['body']), MANAGER)).toEqual({ admitted: true });
584+
expect(await svc.admitAuthoredSource({ ...item('ticket', ['body']), adapter: 'turso' }, MANAGER)).toEqual({ admitted: true });
585+
const placed = new KnowledgeService({ dataEngine: makeEngine([TICKET]), deploymentAdapter: 'turso' });
586+
placed.registerAdapter('turso', makeAdapter());
587+
placed.registerAdapter('memory', makeAdapter());
588+
const { adapter: _omitted, ...omitted } = item('ticket', ['body']);
589+
expect(await placed.admitAuthoredSource(omitted, MANAGER)).toEqual({ admitted: true });
590+
});
591+
546592
it('registers nothing and searches nothing: the verdict is a read', async () => {
547593
const { svc, adapter } = setup({ sources: [], security: salaryHiddenFromMembers });
548594
await svc.admitAuthoredSource(item('ticket', ['body']), MANAGER);

‎packages/services/service-knowledge/src/knowledge-service.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2679,7 +2679,14 @@ export class KnowledgeService implements IKnowledgeService {
26792679
* the protocol's parse refuses it with the schema's own prescription.
26802680
*
26812681
* A refusal is coded (ADR-0112) and names the object, and the field for the
2682-
* field gate: `403 PERMISSION_DENIED`. Fails closed: with no data engine, a
2682+
* field gate: `403 PERMISSION_DENIED`. A source whose `adapter` names no
2683+
* adapter this deployment mounts is refused `400 VALIDATION_ERROR`, naming
2684+
* the source, the adapter and the mounted ones; an omitted `adapter` is not
2685+
* (it is placed by {@link adapterIdFor}). That check is this door's only:
2686+
* a package install (`isSystem`), a source registered in code and
2687+
* `os validate` are not judged here, and for them the boot's reading
2688+
* ({@link reportUnmountedAdapters}) and the `503` at index and search stay
2689+
* the signal. Fails closed: with no data engine, a
26832690
* gate that throws, or a security service that answers neither field
26842691
* question, the save is refused `503 SERVICE_UNAVAILABLE`. The system
26852692
* context is admitted, as it bypasses the gates at search.
@@ -2728,6 +2735,13 @@ export class KnowledgeService implements IKnowledgeService {
27282735
if (authoredAdapter === undefined && this.options.deploymentAdapter === undefined && registeredAdapters.length > 1) {
27292736
return refuse(400, 'VALIDATION_ERROR', `${named}: it ${unplacedSourceText(registeredAdapters)}`);
27302737
}
2738+
// [ADR-0161 K4] A source that names an adapter this deployment does not
2739+
// mount is refused at its save, so the author hears it there and not from
2740+
// the boot log or a search's 503. A save is served after every store's
2741+
// `start()`, so the adapters registered now are the deployment's.
2742+
if (typeof authoredAdapter === 'string' && !registeredAdapters.includes(authoredAdapter)) {
2743+
return refuse(400, 'VALIDATION_ERROR', `${named}: ${this.unmountedAdapterSaveText(authoredAdapter, registeredAdapters)}`);
2744+
}
27312745
const engine = this.options.dataEngine;
27322746
if (!engine) return unavailable('the knowledge service has no data engine');
27332747

@@ -2795,6 +2809,27 @@ export class KnowledgeService implements IKnowledgeService {
27952809
}
27962810
}
27972811

2812+
/**
2813+
* Why a saved source naming `adapter`, which `mounted` does not hold, is
2814+
* refused, and what to write: the rule, then the remedies this deployment
2815+
* can take. Leaving `adapter` out is offered only where an omitted adapter
2816+
* would be placed ({@link adapterIdFor}).
2817+
*/
2818+
private unmountedAdapterSaveText(adapter: string, mounted: readonly string[]): string {
2819+
const deployment = this.options.deploymentAdapter;
2820+
const placeable = (deployment !== undefined && mounted.includes(deployment)) || mounted.length === 1;
2821+
const remedies = [
2822+
...(mounted.length === 0 ? [] : [`name one of ${mounted.map((id) => `'${id}'`).join(', ')} as its \`adapter\``]),
2823+
...(placeable ? ["leave `adapter` out, and the deployment's store holds it"] : []),
2824+
`mount the '${adapter}' store and save it again`,
2825+
];
2826+
return (
2827+
`it names adapter '${adapter}', which this deployment does not mount ` +
2828+
`(mounted: ${mounted.length === 0 ? 'none' : mounted.join(', ')}), and a source's \`adapter\` must name a ` +
2829+
`mounted store. ${remedies.join(', or ').replace(/^./, (c) => c.toUpperCase())}.`
2830+
);
2831+
}
2832+
27982833
/** A gate lookup threw: every source over `object` is refused (fail-closed), reported once at `error`, `warn` as the fallback. */
27992834
private reportGateFailure(object: string, err: Error): void {
28002835
const report =

0 commit comments

Comments
 (0)