|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +// An adapter whose `search` throws without both an ADR-0112 `code` and `status` (an embedder that |
| 4 | +// cannot be reached, a timeout outside the store) is refused for that source, |
| 5 | +// never answered as zero hits: the caller reads a `refused` entry naming the |
| 6 | +// source, coded SERVICE_UNAVAILABLE (503), under a platform sentence; the |
| 7 | +// thrown message reaches the operator's `error` line only. |
| 8 | + |
| 9 | +import { describe, it, expect, vi } from 'vitest'; |
| 10 | +import { KnowledgeService, SEARCH_FIRST_ROUND_FACTOR } from '../knowledge-service.js'; |
| 11 | +import type { IKnowledgeAdapter } from '@objectstack/spec/contracts'; |
| 12 | +import type { KnowledgeHit, KnowledgeSource, ObjectKnowledgeSource } from '@objectstack/spec/ai'; |
| 13 | + |
| 14 | +const SYSTEM = { positions: [], permissions: [], isSystem: true }; |
| 15 | +const INTERNAL = 'embedder down at http://10.0.0.7:11434 (key sk-secret)'; |
| 16 | + |
| 17 | +function source(name: string, adapter: string): KnowledgeSource { |
| 18 | + return { |
| 19 | + name, |
| 20 | + label: name, |
| 21 | + adapter, |
| 22 | + source: { kind: 'object', object: 'task', content: ['title'], metadataFields: ['status'] } as ObjectKnowledgeSource, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function hit(sourceId: string, record: string, n = 0, score = 0.9): KnowledgeHit { |
| 27 | + return { chunkId: `${sourceId}:${record}#${n}`, documentId: `${sourceId}:${record}`, sourceId, sourceRecordId: record, score, snippet: 'ok' }; |
| 28 | +} |
| 29 | + |
| 30 | +function adapter(id: string, search: IKnowledgeAdapter['search']): IKnowledgeAdapter { |
| 31 | + return { id, upsert: vi.fn(async () => undefined), delete: vi.fn(async () => undefined), search: vi.fn(search) }; |
| 32 | +} |
| 33 | + |
| 34 | +function setup(adapters: IKnowledgeAdapter[]) { |
| 35 | + const logger = { debug: vi.fn(), warn: vi.fn(), error: vi.fn() }; |
| 36 | + const svc = new KnowledgeService({ logger: logger as never }); |
| 37 | + for (const a of adapters) { |
| 38 | + svc.registerAdapter(a.id, a); |
| 39 | + svc.registerSource(source(a.id, a.id)); |
| 40 | + } |
| 41 | + return { svc, logger }; |
| 42 | +} |
| 43 | + |
| 44 | +describe('knowledge search: an adapter throw without a code is refused for its source, never zero hits', () => { |
| 45 | + it('a plain Error from search: one refused entry naming the source, SERVICE_UNAVAILABLE / 503; other sources still served', async () => { |
| 46 | + const { svc, logger } = setup([ |
| 47 | + adapter('failing', async () => { throw new Error(INTERNAL); }), |
| 48 | + adapter('answering', async () => [hit('answering', 'r1')]), |
| 49 | + ]); |
| 50 | + const report = await svc.searchWithRefusals('q', { executionContext: SYSTEM }); |
| 51 | + expect(report.hits.map((h) => h.documentId)).toEqual(['answering:r1']); |
| 52 | + expect(report.refused).toHaveLength(1); |
| 53 | + expect(report.refused[0]).toMatchObject({ sourceId: 'failing', code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 54 | + // The caller's sentence names the source; the thrown text stays with the operator. |
| 55 | + expect(report.refused[0].message).toContain("source 'failing'"); |
| 56 | + expect(report.refused[0].message).not.toContain('10.0.0.7'); |
| 57 | + expect(report.refused[0].message).not.toContain('sk-secret'); |
| 58 | + expect(logger.error.mock.calls.some(([line]) => String(line).includes(INTERNAL))).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('the only source throws uncoded: the search is refused whole, SERVICE_UNAVAILABLE / 503, not answered empty', async () => { |
| 62 | + const { svc } = setup([adapter('failing', async () => { throw new Error(INTERNAL); })]); |
| 63 | + const thrown = await svc.searchWithRefusals('q', { executionContext: SYSTEM }).then( |
| 64 | + () => undefined, |
| 65 | + (e: unknown) => e as { code?: string; status?: number; message: string; refused?: Array<{ sourceId: string; code: string }> }, |
| 66 | + ); |
| 67 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 68 | + expect(thrown?.refused).toEqual([expect.objectContaining({ sourceId: 'failing', code: 'SERVICE_UNAVAILABLE', status: 503 })]); |
| 69 | + expect(thrown?.message).not.toContain('sk-secret'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('every searched source throws uncoded (an embedder outage): refused whole, 503, refused naming each', async () => { |
| 73 | + const { svc } = setup([ |
| 74 | + adapter('first', async () => { throw new Error(INTERNAL); }), |
| 75 | + adapter('second', async () => { throw new Error(INTERNAL); }), |
| 76 | + ]); |
| 77 | + const thrown = await svc.searchWithRefusals('q', { executionContext: SYSTEM }).then( |
| 78 | + () => undefined, |
| 79 | + (e: unknown) => e as { code?: string; status?: number; message: string; refused?: Array<{ sourceId: string; code: string }> }, |
| 80 | + ); |
| 81 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 82 | + expect(thrown?.refused?.map((r) => [r.sourceId, r.code])).toEqual([ |
| 83 | + ['first', 'SERVICE_UNAVAILABLE'], |
| 84 | + ['second', 'SERVICE_UNAVAILABLE'], |
| 85 | + ]); |
| 86 | + expect(thrown?.message).not.toContain('sk-secret'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('an uncoded throw beside a coded refusal, none answering: refused names both, top-level code is the first in source order', async () => { |
| 90 | + const { svc } = setup([ |
| 91 | + adapter('failing', async () => { throw new Error(INTERNAL); }), |
| 92 | + adapter('refusing', async () => { |
| 93 | + throw Object.assign(new Error("Knowledge source 'refusing' needs a rebuild."), { code: 'KNOWLEDGE_REBUILD_REQUIRED', status: 409 }); |
| 94 | + }), |
| 95 | + ]); |
| 96 | + const thrown = await svc.searchWithRefusals('q', { executionContext: SYSTEM }).then( |
| 97 | + () => undefined, |
| 98 | + (e: unknown) => e as { code?: string; status?: number; message: string; refused?: Array<{ sourceId: string; code: string; status: number }> }, |
| 99 | + ); |
| 100 | + expect(thrown).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); |
| 101 | + expect(thrown?.refused?.map((r) => [r.sourceId, r.code, r.status])).toEqual([ |
| 102 | + ['failing', 'SERVICE_UNAVAILABLE', 503], |
| 103 | + ['refusing', 'KNOWLEDGE_REBUILD_REQUIRED', 409], |
| 104 | + ]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('a coded refusal is unchanged: its own code, status and message', async () => { |
| 108 | + const { svc } = setup([ |
| 109 | + adapter('refusing', async () => { |
| 110 | + throw Object.assign(new Error("Knowledge source 'refusing' needs a rebuild."), { code: 'KNOWLEDGE_REBUILD_REQUIRED', status: 409 }); |
| 111 | + }), |
| 112 | + adapter('answering', async () => [hit('answering', 'r1')]), |
| 113 | + ]); |
| 114 | + const report = await svc.searchWithRefusals('q', { executionContext: SYSTEM }); |
| 115 | + expect(report.hits.map((h) => h.documentId)).toEqual(['answering:r1']); |
| 116 | + expect(report.refused).toEqual([ |
| 117 | + { sourceId: 'refusing', code: 'KNOWLEDGE_REBUILD_REQUIRED', status: 409, message: "Knowledge source 'refusing' needs a rebuild." }, |
| 118 | + ]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('a source that matches nothing answers no hits and no refusal', async () => { |
| 122 | + const { svc, logger } = setup([adapter('empty', async () => [])]); |
| 123 | + const report = await svc.searchWithRefusals('q', { executionContext: SYSTEM }); |
| 124 | + expect(report).toEqual({ hits: [], refused: [] }); |
| 125 | + expect(logger.error).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('a source that answers the first round and throws uncoded on the refill: refused once, its first-round hits dropped', async () => { |
| 129 | + const topK = 5; |
| 130 | + let calls = 0; |
| 131 | + const { svc } = setup([ |
| 132 | + // Round one fills its ask with passages of one record, so the record-level search refills it. |
| 133 | + adapter('flaky', async () => { |
| 134 | + calls += 1; |
| 135 | + if (calls > 1) throw new Error(INTERNAL); |
| 136 | + return Array.from({ length: topK * SEARCH_FIRST_ROUND_FACTOR }, (_, n) => hit('flaky', 'r0', n, 1 - n / 1000)); |
| 137 | + }), |
| 138 | + adapter('answering', async () => [hit('answering', 'r9', 0, 0.5)]), |
| 139 | + ]); |
| 140 | + const report = await svc.searchWithRefusals('q', { granularity: 'record', topK, executionContext: SYSTEM }); |
| 141 | + expect(calls).toBe(2); |
| 142 | + expect(report.hits.map((h) => h.sourceId)).toEqual(['answering']); |
| 143 | + expect(report.refused).toEqual([expect.objectContaining({ sourceId: 'flaky', code: 'SERVICE_UNAVAILABLE', status: 503 })]); |
| 144 | + }); |
| 145 | +}); |
0 commit comments