|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0161 K8, the dated note on item 12] The administrator's two knowledge |
| 5 | + * actions say when the service is held. While the boot read a service the |
| 6 | + * runtime composed as not enabled (no embedder, or the deployment's store not |
| 7 | + * mounted): |
| 8 | + * |
| 9 | + * - `index_status` answers what it answers otherwise, plus `enablement` — the |
| 10 | + * state, the reason with its remedy, the missing halves and how many |
| 11 | + * sources are held — and its message leads with the state; |
| 12 | + * - `rebuild` is refused (`ok: false`, which the settings door answers 400) |
| 13 | + * with the service's own refusal of the build, naming the remedy, and no |
| 14 | + * store is called. |
| 15 | + * |
| 16 | + * A service a host mounts itself, and an enabled one, answer as they always |
| 17 | + * did: no `enablement`, the same message. The actions are run the way the |
| 18 | + * settings page and `os knowledge status` / `os knowledge rebuild` reach |
| 19 | + * them: through the settings service's action door. |
| 20 | + * |
| 21 | + * While held, `index_status` answers each source's index as `held` without |
| 22 | + * asking its store: a held service calls no store, and a reading of the |
| 23 | + * store would say a build is due that nothing will run. |
| 24 | + */ |
| 25 | + |
| 26 | +import { createHash } from 'node:crypto'; |
| 27 | +import { describe, it, expect } from 'vitest'; |
| 28 | +import { LiteKernel } from '@objectstack/core'; |
| 29 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 30 | +import { EMBEDDER_SERVICE, KNOWLEDGE_SERVICE } from '@objectstack/spec/contracts'; |
| 31 | +import type { KnowledgeSource } from '@objectstack/spec/ai'; |
| 32 | +import type { IEmbedder, IKnowledgeAdapter } from '@objectstack/spec/contracts'; |
| 33 | +import { SettingsServicePlugin, type CryptoAdapter, type SettingsService } from '@objectstack/service-settings'; |
| 34 | +import { KnowledgeServicePlugin } from '../knowledge-service-plugin.js'; |
| 35 | +import { KnowledgeService } from '../knowledge-service.js'; |
| 36 | +import { knowledgeIndexStatusActionHandler, type KnowledgeIndexStatusEnablement } from '../knowledge-build-actions.js'; |
| 37 | + |
| 38 | +/** The settings service stores the embedder key only through an adapter that declares confidentiality. */ |
| 39 | +class ConfidentialTestAdapter implements CryptoAdapter { |
| 40 | + readonly confidential = true; |
| 41 | + async encrypt(plaintext: string): Promise<string> { |
| 42 | + return 'kms:' + Buffer.from(plaintext, 'utf8').toString('base64'); |
| 43 | + } |
| 44 | + async decrypt(ciphertext: string): Promise<string> { |
| 45 | + return Buffer.from(ciphertext.replace(/^kms:/, ''), 'base64').toString('utf8'); |
| 46 | + } |
| 47 | + digest(plaintext: string): string { |
| 48 | + return createHash('sha256').update(plaintext).digest('hex'); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** The deployment's store (`id`), counting every call made to it. */ |
| 53 | +class CountingStorePlugin implements Plugin { |
| 54 | + name = 'test.knowledge.counting-store'; |
| 55 | + version = '1.0.0'; |
| 56 | + type = 'standard' as const; |
| 57 | + dependencies = ['com.objectstack.service.knowledge']; |
| 58 | + constructor(private readonly storeId: string, private readonly calls: string[]) {} |
| 59 | + async init(): Promise<void> {} |
| 60 | + async start(ctx: PluginContext): Promise<void> { |
| 61 | + const calls = this.calls; |
| 62 | + const adapter: IKnowledgeAdapter = { |
| 63 | + id: this.storeId, |
| 64 | + async upsert() { |
| 65 | + calls.push('upsert'); |
| 66 | + }, |
| 67 | + async search() { |
| 68 | + calls.push('search'); |
| 69 | + return []; |
| 70 | + }, |
| 71 | + async delete() { |
| 72 | + calls.push('delete'); |
| 73 | + }, |
| 74 | + async dropSource() { |
| 75 | + calls.push('dropSource'); |
| 76 | + }, |
| 77 | + }; |
| 78 | + ctx.getService<KnowledgeService>(KNOWLEDGE_SERVICE).registerAdapter(this.storeId, adapter); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** A host's own embedder, registered at `init`. */ |
| 83 | +class HostEmbedderPlugin implements Plugin { |
| 84 | + name = 'test.host-embedder'; |
| 85 | + version = '1.0.0'; |
| 86 | + type = 'standard' as const; |
| 87 | + async init(ctx: PluginContext): Promise<void> { |
| 88 | + const embedder: IEmbedder = { id: 'host-own', dimensions: 2, embed: async (t) => t.map(() => [0.6, 0.8]) }; |
| 89 | + ctx.registerService(EMBEDDER_SERVICE, embedder); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +const SOURCES: KnowledgeSource[] = [ |
| 94 | + { name: 'docs', label: 'Docs', source: { kind: 'object', object: 'doc', content: ['body'] } }, |
| 95 | + { name: 'faq', label: 'FAQ', source: { kind: 'object', object: 'faq', content: ['answer'] } }, |
| 96 | +]; |
| 97 | + |
| 98 | +async function boot(opts: { |
| 99 | + /** Set ⇒ the runtime composed the service (`resolveCapabilityArgument`). */ |
| 100 | + runtimeComposed?: { declared: boolean }; |
| 101 | + /** Whether the deployment's store registers its adapter. @default true */ |
| 102 | + storeMounted?: boolean; |
| 103 | + /** Whether a host plugin registers an embedder. @default false */ |
| 104 | + hostEmbedder?: boolean; |
| 105 | +}) { |
| 106 | + const calls: string[] = []; |
| 107 | + const kernel = new LiteKernel(); |
| 108 | + if (opts.hostEmbedder) kernel.use(new HostEmbedderPlugin()); |
| 109 | + kernel.use(new KnowledgeServicePlugin({ |
| 110 | + enableEventSync: false, |
| 111 | + buildAtBoot: false, |
| 112 | + reconcile: false, |
| 113 | + deploymentAdapter: 'vec', |
| 114 | + ...(opts.runtimeComposed ? { runtimeComposed: opts.runtimeComposed } : {}), |
| 115 | + sources: SOURCES, |
| 116 | + })); |
| 117 | + kernel.use(new SettingsServicePlugin({ env: {}, registerRoutes: false, crypto: new ConfidentialTestAdapter() })); |
| 118 | + if (opts.storeMounted !== false) kernel.use(new CountingStorePlugin('vec', calls)); |
| 119 | + await kernel.bootstrap(); |
| 120 | + const knowledge = kernel.getService<KnowledgeService>(KNOWLEDGE_SERVICE); |
| 121 | + const settings = kernel.getService<SettingsService>('settings'); |
| 122 | + const run = (action: 'index_status' | 'rebuild', payload: unknown) => settings.runAction('knowledge', action, payload); |
| 123 | + return { kernel, knowledge, calls, run }; |
| 124 | +} |
| 125 | + |
| 126 | +const enablementOf = (details: unknown): KnowledgeIndexStatusEnablement | undefined => |
| 127 | + (details as { enablement?: KnowledgeIndexStatusEnablement }).enablement; |
| 128 | + |
| 129 | +describe('[ADR-0161 K8, note on item 12] index_status states a held service: not enabled, why, how many sources', () => { |
| 130 | + it('no embedder: the state, the reason naming the remedy, the embedder half and every held source', async () => { |
| 131 | + const { knowledge, run, calls } = await boot({ runtimeComposed: { declared: false } }); |
| 132 | + expect(knowledge.heldReading()).toBeDefined(); |
| 133 | + |
| 134 | + const answer = await run('index_status', {}); |
| 135 | + expect(answer.ok).toBe(true); |
| 136 | + const enablement = enablementOf(answer.details); |
| 137 | + expect(enablement).toEqual({ |
| 138 | + state: 'not-enabled', |
| 139 | + reason: expect.stringMatching(/^\[knowledge\] the knowledge service is not enabled on this deployment: no embedder is configured/), |
| 140 | + embedder: expect.stringContaining('ai.embedder_provider'), |
| 141 | + heldSources: 2, |
| 142 | + }); |
| 143 | + expect(enablement?.reason).toContain('To enable it, save an embedder provider, then restart.'); |
| 144 | + expect(enablement?.reason).not.toContain('knowledge store'); |
| 145 | + // The message leads with the state and counts the held sources. |
| 146 | + expect(answer.message).toBe(`${enablement?.reason} 2 source(s) held.`); |
| 147 | + // Every source is still listed, as on an enabled service. |
| 148 | + expect((answer.details as { sources: Array<{ id: string }> }).sources.map((s) => s.id)).toEqual(['docs', 'faq']); |
| 149 | + |
| 150 | + // One source named: its entry alone, and the service's held count all the same. |
| 151 | + const one = await run('index_status', { source: 'docs' }); |
| 152 | + expect((one.details as { sources: Array<{ id: string }> }).sources.map((s) => s.id)).toEqual(['docs']); |
| 153 | + expect(enablementOf(one.details)?.heldSources).toBe(2); |
| 154 | + expect(calls).toEqual([]); |
| 155 | + }); |
| 156 | + |
| 157 | + it("the store not mounted as well: both halves, and the reason names the store's remedy", async () => { |
| 158 | + const { run } = await boot({ runtimeComposed: { declared: false }, storeMounted: false }); |
| 159 | + const enablement = enablementOf((await run('index_status', {})).details); |
| 160 | + expect(enablement).toMatchObject({ state: 'not-enabled', store: 'vec', embedder: expect.any(String), heldSources: 2 }); |
| 161 | + expect(enablement?.reason).toContain("the deployment's knowledge store 'vec' is not mounted"); |
| 162 | + expect(enablement?.reason).toContain("save an embedder provider and mount the deployment's knowledge store, then restart."); |
| 163 | + }); |
| 164 | + |
| 165 | + it('a host-mounted service with no embedder, and an enabled runtime-composed one, answer no enablement and the same message', async () => { |
| 166 | + for (const opts of [{}, { runtimeComposed: { declared: false }, hostEmbedder: true }]) { |
| 167 | + const { knowledge, run } = await boot(opts); |
| 168 | + expect(knowledge.heldReading(), JSON.stringify(opts)).toBeUndefined(); |
| 169 | + const answer = await run('index_status', {}); |
| 170 | + expect(answer.ok).toBe(true); |
| 171 | + expect(Object.keys(answer.details as object), JSON.stringify(opts)).not.toContain('enablement'); |
| 172 | + expect(answer.message, JSON.stringify(opts)).toMatch(/^2 source\(s\): \d+ need a build, 0 building, 0 with failed syncs\.$/); |
| 173 | + } |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +describe('[ADR-0161 K8, note on item 12] rebuild is refused on a held service, with the remedy', () => { |
| 178 | + it("refused (ok: false), the service's own sentence naming the remedy, and no store is called", async () => { |
| 179 | + const { run, calls } = await boot({ runtimeComposed: { declared: false } }); |
| 180 | + const answer = await run('rebuild', { source: 'docs' }); |
| 181 | + expect(answer).toMatchObject({ ok: false, severity: 'error', details: { source: 'docs', build: null } }); |
| 182 | + expect(answer.message).toMatch( |
| 183 | + /^Rebuild of source 'docs' is refused: \[knowledge\] the knowledge service is not enabled on this deployment: no embedder is configured/, |
| 184 | + ); |
| 185 | + expect(answer.message).toContain('To enable it, save an embedder provider, then restart.'); |
| 186 | + expect(calls).toEqual([]); |
| 187 | + }); |
| 188 | + |
| 189 | + it('an enabled runtime-composed service is not refused', async () => { |
| 190 | + const { run } = await boot({ runtimeComposed: { declared: false }, hostEmbedder: true }); |
| 191 | + const answer = await run('rebuild', { source: 'docs' }); |
| 192 | + expect(answer.message ?? '').not.toContain('is refused'); |
| 193 | + expect(answer.message ?? '').not.toContain('not enabled'); |
| 194 | + }); |
| 195 | +}); |
| 196 | + |
| 197 | +describe('[ADR-0161 K8, note on item 12] a held index_status asks no store what it holds', () => { |
| 198 | + /** A service with a data engine bound, so its index reading reaches the store; the store counts every call. */ |
| 199 | + function service(held: boolean) { |
| 200 | + const calls: string[] = []; |
| 201 | + const dataEngine = { find: async () => [], findOne: async () => null, count: async () => 0 } as never; |
| 202 | + const knowledge = new KnowledgeService({ dataEngine, deploymentAdapter: 'vec', enabledByConfiguration: true }); |
| 203 | + const adapter: IKnowledgeAdapter = { |
| 204 | + id: 'vec', |
| 205 | + async upsert() { calls.push('upsert'); }, |
| 206 | + async search() { calls.push('search'); return []; }, |
| 207 | + async delete() { calls.push('delete'); }, |
| 208 | + async indexStatus() { |
| 209 | + calls.push('indexStatus'); |
| 210 | + return { fingerprint: 'built-under-another-model', recipeHash: 'r' }; |
| 211 | + }, |
| 212 | + }; |
| 213 | + knowledge.registerAdapter('vec', adapter); |
| 214 | + knowledge.registerSource(SOURCES[0]!); |
| 215 | + knowledge.settleEnablement(held ? { embedder: 'ai.embedder_provider is not saved' } : undefined); |
| 216 | + const status = knowledgeIndexStatusActionHandler(knowledge); |
| 217 | + const read = () => status({ namespace: 'knowledge', actionId: 'index_status', values: {}, payload: {}, ctx: {} } as never); |
| 218 | + return { calls, read }; |
| 219 | + } |
| 220 | + |
| 221 | + it('held, the store mounted: the index reads held, not due, and the store is never called', async () => { |
| 222 | + const { calls, read } = service(true); |
| 223 | + const answer = await read(); |
| 224 | + const [entry] = (answer.details as { sources: Array<{ index: { state: string; due: boolean; detail: string } }> }).sources; |
| 225 | + expect(entry?.index).toMatchObject({ state: 'held', due: false }); |
| 226 | + expect(entry?.index.detail).toMatch(/^held: the knowledge service is not enabled on this deployment/); |
| 227 | + expect(entry?.index.detail).not.toContain('built anew'); |
| 228 | + expect(calls).toEqual([]); |
| 229 | + }); |
| 230 | + |
| 231 | + it('control: enabled, the same store is asked once per source, as it always was', async () => { |
| 232 | + const { calls, read } = service(false); |
| 233 | + const answer = await read(); |
| 234 | + const [entry] = (answer.details as { sources: Array<{ index: { state: string } }> }).sources; |
| 235 | + expect(entry?.index.state).toBe('new-fingerprint'); |
| 236 | + expect(calls).toEqual(['indexStatus']); |
| 237 | + }); |
| 238 | +}); |
0 commit comments