|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A service a host mounts itself with no embedder configured answers a search |
| 5 | + * the way the held path does: ADR-0112 `SERVICE_UNAVAILABLE` (503), with the |
| 6 | + * stand-in embedder's own sentence, which names the `ai.embedder_*` settings to |
| 7 | + * save and the restart. It is never answered as an outage under a platform |
| 8 | + * sentence that withholds the remedy, and never answered empty. |
| 9 | + * |
| 10 | + * The stand-in (`UnconfiguredEmbedder`) codes its refusal at its source, on |
| 11 | + * both of its uses: reading `dimensions` and `embed()`. Two store shapes reach |
| 12 | + * it on a search, and both are pinned here, booted the way a host boots them — |
| 13 | + * `KnowledgeServicePlugin` with no runtime composition, the settings service |
| 14 | + * with nothing saved, and a store plugin that resolves the kernel's |
| 15 | + * `embedder` service at each call: |
| 16 | + * |
| 17 | + * - a store whose `embedding` descriptor reads the embedder's `dimensions` |
| 18 | + * (the vector stores' shape): the refusal surfaces while the search is |
| 19 | + * planned, before any store is asked; |
| 20 | + * - a store that embeds the query inside `search`: the refusal surfaces from |
| 21 | + * the store's search and is carried by the search round. |
| 22 | + */ |
| 23 | + |
| 24 | +import { createHash } from 'node:crypto'; |
| 25 | +import { describe, it, expect } from 'vitest'; |
| 26 | +import { LiteKernel } from '@objectstack/core'; |
| 27 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 28 | +import { EMBEDDER_SERVICE, KNOWLEDGE_SERVICE } from '@objectstack/spec/contracts'; |
| 29 | +import type { KnowledgeSource } from '@objectstack/spec/ai'; |
| 30 | +import type { IEmbedder, IKnowledgeAdapter } from '@objectstack/spec/contracts'; |
| 31 | +import { SettingsServicePlugin, type CryptoAdapter } from '@objectstack/service-settings'; |
| 32 | +import { KnowledgeServicePlugin } from '../knowledge-service-plugin.js'; |
| 33 | +import { KnowledgeService } from '../knowledge-service.js'; |
| 34 | + |
| 35 | +const SYSTEM = { positions: [], permissions: [], isSystem: true }; |
| 36 | + |
| 37 | +/** The settings service stores the embedder key only through an adapter that declares confidentiality. */ |
| 38 | +class ConfidentialTestAdapter implements CryptoAdapter { |
| 39 | + readonly confidential = true; |
| 40 | + async encrypt(plaintext: string): Promise<string> { |
| 41 | + return 'kms:' + Buffer.from(plaintext, 'utf8').toString('base64'); |
| 42 | + } |
| 43 | + async decrypt(ciphertext: string): Promise<string> { |
| 44 | + return Buffer.from(ciphertext.replace(/^kms:/, ''), 'base64').toString('utf8'); |
| 45 | + } |
| 46 | + digest(plaintext: string): string { |
| 47 | + return createHash('sha256').update(plaintext).digest('hex'); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +type StoreShape = 'descriptor' | 'search-only'; |
| 52 | + |
| 53 | +/** A store that calls the kernel's `embedder` service, resolved at each call. */ |
| 54 | +class EmbeddingStorePlugin implements Plugin { |
| 55 | + name = 'test.knowledge.embedding-store'; |
| 56 | + version = '1.0.0'; |
| 57 | + type = 'standard' as const; |
| 58 | + dependencies = ['com.objectstack.service.knowledge']; |
| 59 | + constructor(private readonly shape: StoreShape, private readonly calls: string[]) {} |
| 60 | + async init(): Promise<void> {} |
| 61 | + async start(ctx: PluginContext): Promise<void> { |
| 62 | + const calls = this.calls; |
| 63 | + const embedder = () => ctx.getService<IEmbedder>(EMBEDDER_SERVICE); |
| 64 | + const adapter: IKnowledgeAdapter = { |
| 65 | + id: 'vec', |
| 66 | + async upsert() { |
| 67 | + calls.push('upsert'); |
| 68 | + }, |
| 69 | + async search(query) { |
| 70 | + calls.push('search'); |
| 71 | + await embedder().embed([query]); |
| 72 | + return []; |
| 73 | + }, |
| 74 | + async delete() { |
| 75 | + calls.push('delete'); |
| 76 | + }, |
| 77 | + }; |
| 78 | + if (this.shape === 'descriptor') { |
| 79 | + Object.defineProperty(adapter, 'embedding', { |
| 80 | + get: () => ({ dimensions: embedder().dimensions }), |
| 81 | + enumerable: true, |
| 82 | + }); |
| 83 | + } |
| 84 | + ctx.getService<KnowledgeService>(KNOWLEDGE_SERVICE).registerAdapter('vec', adapter); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const SOURCES: KnowledgeSource[] = [ |
| 89 | + { name: 'docs', label: 'Docs', adapter: 'vec', source: { kind: 'object', object: 'doc', content: ['body'] } }, |
| 90 | +]; |
| 91 | + |
| 92 | +/** A host-mounted service: no `runtimeComposed`, nothing saved under `ai.embedder_*`. */ |
| 93 | +async function bootHostMounted(shape: StoreShape) { |
| 94 | + const calls: string[] = []; |
| 95 | + const kernel = new LiteKernel(); |
| 96 | + kernel.use(new KnowledgeServicePlugin({ |
| 97 | + enableEventSync: false, |
| 98 | + buildAtBoot: false, |
| 99 | + reconcile: false, |
| 100 | + sources: SOURCES, |
| 101 | + })); |
| 102 | + kernel.use(new SettingsServicePlugin({ env: {}, registerRoutes: false, crypto: new ConfidentialTestAdapter() })); |
| 103 | + kernel.use(new EmbeddingStorePlugin(shape, calls)); |
| 104 | + await kernel.bootstrap(); |
| 105 | + const knowledge = kernel.getService<KnowledgeService>(KNOWLEDGE_SERVICE); |
| 106 | + const embedder = kernel.getService<IEmbedder>(EMBEDDER_SERVICE); |
| 107 | + return { kernel, knowledge, embedder, calls }; |
| 108 | +} |
| 109 | + |
| 110 | +type Refusal = { code?: string; status?: number; message: string; refused?: Array<{ sourceId: string; code: string; status: number; message: string }> }; |
| 111 | + |
| 112 | +const refusalOf = (p: Promise<unknown>): Promise<Refusal | undefined> => |
| 113 | + p.then(() => undefined, (e: unknown) => e as Refusal); |
| 114 | + |
| 115 | +/** The stand-in's sentence: nothing was embedded or sent, the settings to save, and the restart. */ |
| 116 | +function expectRemedy(message: string | undefined): void { |
| 117 | + expect(message).toMatch(/^No embedder is configured, so nothing was embedded and nothing was sent: /); |
| 118 | + expect(message).toContain('ai.embedder_provider'); |
| 119 | + expect(message).toContain('restart'); |
| 120 | +} |
| 121 | + |
| 122 | +describe('a host-mounted service with no embedder answers like the held path: 503 SERVICE_UNAVAILABLE naming the setting', () => { |
| 123 | + it('the stand-in is what the host-mounted boot registers, and it is not held', async () => { |
| 124 | + const { knowledge, embedder } = await bootHostMounted('search-only'); |
| 125 | + expect(embedder.id).toBe('unconfigured'); |
| 126 | + expect(knowledge.heldReading()).toBeUndefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('reading the stand-in\'s dimensions is refused coded, with the remedy', async () => { |
| 130 | + const { embedder } = await bootHostMounted('search-only'); |
| 131 | + let thrown: Refusal | undefined; |
| 132 | + try { |
| 133 | + void embedder.dimensions; |
| 134 | + } catch (e) { |
| 135 | + thrown = e as Refusal; |
| 136 | + } |
| 137 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 138 | + expectRemedy(thrown?.message); |
| 139 | + }); |
| 140 | + |
| 141 | + it('embed() on the stand-in is refused coded, with the remedy; an empty batch still answers empty', async () => { |
| 142 | + const { embedder } = await bootHostMounted('search-only'); |
| 143 | + const thrown = await refusalOf(embedder.embed(['hello'])); |
| 144 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 145 | + expectRemedy(thrown?.message); |
| 146 | + await expect(embedder.embed([])).resolves.toEqual([]); |
| 147 | + }); |
| 148 | + |
| 149 | + it('a store that embeds inside search: the search is refused 503 with the stand-in\'s own sentence, not the platform\'s', async () => { |
| 150 | + const { knowledge, calls } = await bootHostMounted('search-only'); |
| 151 | + const thrown = await refusalOf(knowledge.searchWithRefusals('q', { executionContext: SYSTEM })); |
| 152 | + expect(calls).toEqual(['search']); |
| 153 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 154 | + expect(thrown?.refused).toHaveLength(1); |
| 155 | + expect(thrown?.refused?.[0]).toMatchObject({ sourceId: 'docs', code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 156 | + expectRemedy(thrown?.refused?.[0].message); |
| 157 | + expect(thrown?.refused?.[0].message).not.toContain('could not be searched'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('a store whose descriptor reads dimensions: the search is refused 503 with the remedy before any store is asked', async () => { |
| 161 | + const { knowledge, calls } = await bootHostMounted('descriptor'); |
| 162 | + const thrown = await refusalOf(knowledge.searchWithRefusals('q', { executionContext: SYSTEM })); |
| 163 | + expect(calls).toEqual([]); |
| 164 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 165 | + expectRemedy(thrown?.message); |
| 166 | + }); |
| 167 | + |
| 168 | + it('search() (no refusal report) is refused the same way, never answered empty', async () => { |
| 169 | + const { knowledge } = await bootHostMounted('search-only'); |
| 170 | + const thrown = await refusalOf(knowledge.search('q', { executionContext: SYSTEM })); |
| 171 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 172 | + expect(thrown?.message).toContain('ai.embedder_provider'); |
| 173 | + }); |
| 174 | +}); |
0 commit comments