Repository navigation
Expand file tree
/
Copy pathacceptance.live-postgres.test.ts
More file actions
290 lines (268 loc) · 14.5 KB
/
Copy pathacceptance.live-postgres.test.ts
File metadata and controls
290 lines (268 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* The acceptance cases of #37100 on a LIVE PostgreSQL with pgvector, beside
* the contract suite: a 3072-wide model, two organizations that never see each
* other's passages, an unchanged passage that is not embedded again, a changed
* fingerprint that is refused and never served until a complete reindex has
* built its generation, a first generation that never adopts rows left behind,
* a passage removed under a concurrent write that is not lost, and the store's
* tables kept out of the connection's own schema. The generation cases of
* #37101 are in `generations.live-postgres.test.ts`.
*/
import { afterAll, describe, expect, it } from 'vitest';
import type { KnowledgeDocument, KnowledgeSource } from '@objectstack/spec/ai';
import type { AdapterContext } from '@objectstack/spec/contracts';
import { KnowledgeService } from '@objectstack/service-knowledge';
import type { ExecutionContext } from '@objectstack/spec/kernel';
import { KNOWLEDGE_REBUILD_REQUIRED, poolFromPg, type PgPoolLike } from '../index.js';
import { FakeEmbedder, PG_URL, liveStore, requireLiveCell } from './live-pg.testkit.js';
requireLiveCell('knowledge-pgvector acceptance');
const store = liveStore('kpg_accept');
const source = (id: string): KnowledgeSource =>
({
id,
label: id,
adapter: 'under_test',
source: { kind: 'object', object: 'article', contentFields: ['body'], metadataFields: ['status'] },
}) as KnowledgeSource;
const FP_A = { fingerprint: 'fp-test:a', recipeHash: 'rh-test:1' };
const FP_B = { fingerprint: 'fp-test:b', recipeHash: 'rh-test:1' };
const ctx = (s: KnowledgeSource, index = FP_A): AdapterContext => ({ source: s, reason: 'manual', index });
function doc(s: KnowledgeSource, n: string, content: string, extra: Partial<KnowledgeDocument> = {}): KnowledgeDocument {
const id = `${s.id}:${n}`;
return {
id,
sourceId: s.id,
sourceRecordId: n,
content,
title: `Title ${n}`,
metadata: { status: 'published' },
chunks: [{ id: `${id}#0`, documentId: id, index: 0, content }],
...extra,
};
}
const SYSTEM = { positions: [], permissions: [], systemPermissions: [], isSystem: true } as unknown as ExecutionContext;
describe.skipIf(!PG_URL)('knowledge-pgvector acceptance on a LIVE PostgreSQL', () => {
afterAll(() => store.dropAll());
it('stores and ranks 3072-wide vectors: an exact scan, no index width limit', async () => {
const embedder = new FakeEmbedder(3072, 'fake-3072');
const adapter = store.adapter({ embedder });
const s = source('wide');
await adapter.upsert([doc(s, '1', 'glacier valley ice'), doc(s, '2', 'harbour crane night')], ctx(s));
const hits = await adapter.search('glacier ice', { source: s, topK: 5, index: { fingerprint: FP_A.fingerprint } });
expect(hits.map((h) => h.documentId)).toEqual([`${s.id}:1`]);
expect(hits[0].score).toBeGreaterThan(0.5);
const width = await store.pool().query<{ t: string }>(
`SELECT format_type(a.atttypid, a.atttypmod) AS t FROM pg_attribute a
WHERE a.attrelid = to_regclass($1) AND a.attname = 'embedding'`,
[`"${(adapter as unknown as { schema: string }).schema}"."knowledge_vec_d3072"`],
);
expect(width.rows[0]?.t).toBe('vector(3072)');
});
it('two organizations never see each other\'s passages: the filter runs in SQL, before ranking', async () => {
const adapter = store.adapter();
const s = source('orgs');
// Organization B's passage would outrank A's for the query.
await adapter.upsert(
[
doc(s, 'a', 'alpine meadow harbour', { organizationId: 'org_a' }),
doc(s, 'b', 'alpine meadow alpine meadow', { organizationId: 'org_b' }),
],
ctx(s),
);
const search = (organizationIds?: string[]) =>
adapter.search('alpine meadow', { source: s, topK: 1, index: { fingerprint: FP_A.fingerprint }, ...(organizationIds ? { organizationIds } : {}) });
expect((await search()).map((h) => h.documentId)).toEqual([`${s.id}:b`]);
expect((await search(['org_a'])).map((h) => h.documentId)).toEqual([`${s.id}:a`]);
expect((await search(['org_b'])).map((h) => h.documentId)).toEqual([`${s.id}:b`]);
expect(await search([])).toEqual([]);
expect(await search(['org_c'])).toEqual([]);
});
it('the metadata filter runs in SQL before ranking, so topK is filled from matching passages', async () => {
const adapter = store.adapter();
const s = source('meta');
await adapter.upsert(
[
doc(s, '1', 'basalt basalt basalt', { metadata: { status: 'draft' } }),
doc(s, '2', 'basalt columns', { metadata: { status: 'published' } }),
],
ctx(s),
);
const hits = await adapter.search('basalt', { source: s, topK: 1, filter: { status: 'published' }, index: { fingerprint: FP_A.fingerprint } });
expect(hits.map((h) => h.documentId)).toEqual([`${s.id}:2`]);
expect(hits[0].metadata).toEqual({ status: 'published' });
});
it('an unchanged passage is not embedded again; its metadata is rewritten', async () => {
const embedder = new FakeEmbedder();
const adapter = store.adapter({ embedder });
const s = source('same');
await adapter.upsert([doc(s, '1', 'copper wire conducts heat')], ctx(s));
expect(embedder.calls).toBe(1);
await adapter.upsert([doc(s, '1', 'copper wire conducts heat', { metadata: { status: 'archived' } })], ctx(s));
expect(embedder.calls).toBe(1);
const [hit] = await adapter.search('copper', { source: s, topK: 1, index: { fingerprint: FP_A.fingerprint } });
expect(hit.metadata).toEqual({ status: 'archived' });
// A changed passage is embedded, and only it (the search above embedded its query).
const before = embedder.calls;
await adapter.upsert(
[{ ...doc(s, '1', 'x'), chunks: [
{ id: `${s.id}:1#0`, documentId: `${s.id}:1`, index: 0, content: 'copper wire conducts heat' },
{ id: `${s.id}:1#1`, documentId: `${s.id}:1`, index: 1, content: 'drawn through dies' },
] }],
ctx(s),
);
expect(embedder.calls).toBe(before + 1);
expect(embedder.texts.slice(-1)).toEqual(['drawn through dies']);
});
it('through KnowledgeService: a second complete reindex of unchanged records embeds nothing', async () => {
const embedder = new FakeEmbedder();
const adapter = store.adapter({ embedder });
const rows = [
{ id: 'r1', body: 'granite quarry', status: 'published' },
{ id: 'r2', body: 'marble quarry', status: 'published' },
];
const svc = new KnowledgeService({ dataEngine: rowsEngine(rows) });
svc.registerAdapter('under_test', adapter);
const s = source('svc_same');
svc.registerSource(s);
expect(await svc.reindexSource(s.id)).toMatchObject({ ok: true, indexed: 2 });
const after = embedder.calls;
expect(await svc.reindexSource(s.id)).toMatchObject({ ok: true, indexed: 0, skipped: 2 });
expect(embedder.calls).toBe(after);
expect((await svc.search('granite', { topK: 3, executionContext: SYSTEM })).map((h) => h.documentId)).toEqual([`${s.id}:r1`]);
});
it('a changed embedding model changes the fingerprint: search is refused by name, and the old rows are not served', async () => {
const embedder = new FakeEmbedder(384, 'model-one');
const adapter = store.adapter({ embedder });
const svc = new KnowledgeService({ dataEngine: rowsEngine([{ id: 'r1', body: 'granite quarry', status: 'published' }]) });
svc.registerAdapter('under_test', adapter);
const s = source('svc_model');
svc.registerSource(s);
await svc.reindexSource(s.id);
expect(await svc.search('granite', { topK: 3, executionContext: SYSTEM })).toHaveLength(1);
embedder.model = 'model-two';
const refused = await svc.search('granite', { topK: 3, executionContext: SYSTEM }).then(
() => undefined,
(e: unknown) => e as { code?: string; status?: number; message?: string },
);
expect(refused).toMatchObject({ code: KNOWLEDGE_REBUILD_REQUIRED, status: 409 });
expect(refused?.message).toContain(s.id);
// The caller is told the source needs a rebuild, never SQL.
expect(refused?.message).not.toContain('DELETE');
// A complete reindex under the new model builds the new generation, and search answers again.
const before = embedder.calls;
expect(await svc.reindexSource(s.id)).toMatchObject({ ok: true, indexed: 1, skipped: 0 });
expect(embedder.calls).toBe(before + 1);
expect(await svc.search('granite', { topK: 3, executionContext: SYSTEM })).toHaveLength(1);
});
it('a first generation never adopts passages left behind: a generation row deleted alone does not carry its vectors over', async () => {
const embedder = new FakeEmbedder(384, 'model-one');
const adapter = store.adapter({ embedder });
const schema = (adapter as unknown as { schema: string }).schema;
const svc = new KnowledgeService({ dataEngine: rowsEngine([{ id: 'r1', body: 'granite quarry', status: 'published' }]) });
svc.registerAdapter('under_test', adapter);
const s = source('svc_leftover');
svc.registerSource(s);
expect(await svc.reindexSource(s.id)).toMatchObject({ ok: true, indexed: 1 });
// Same width, another model; an operator deletes only the generation row.
embedder.model = 'model-two';
await store.pool().query(`DELETE FROM "${schema}"."knowledge_generation" WHERE source_id = $1`, [s.id]);
const before = embedder.calls;
expect(await svc.reindexSource(s.id)).toMatchObject({ ok: true, indexed: 1, skipped: 0 });
// The passage was embedded again under the new model: the old vector was not adopted.
expect(embedder.calls).toBe(before + 1);
expect(embedder.texts.slice(-1)).toEqual(['granite quarry']);
const rows = await store.pool().query<{ n: string }>(
`SELECT count(*) AS n FROM "${schema}"."knowledge_vec_d384" WHERE source_id = $1`,
[s.id],
);
expect(Number(rows.rows[0].n)).toBe(1);
});
it('an unchanged passage removed by a concurrent write before this one commits is embedded and kept, not lost', async () => {
const embedder = new FakeEmbedder();
const base = poolFromPg(store.pool());
let interfere: (() => Promise<void>) | undefined;
const pool: PgPoolLike = {
query: (text, params) => base.query(text, params),
connect: async () => {
// The concurrent write lands between this write's read of the stored
// hashes and its transaction.
const run = interfere;
interfere = undefined;
if (run) await run();
return base.connect();
},
};
const adapter = store.adapter({ embedder, pool });
const schema = (adapter as unknown as { schema: string }).schema;
const s = source('race');
await adapter.upsert([doc(s, '1', 'copper wire conducts heat')], ctx(s));
expect(embedder.calls).toBe(1);
interfere = async () => {
await base.query(`DELETE FROM "${schema}"."knowledge_vec_d384" WHERE source_id = $1`, [s.id]);
};
await adapter.upsert([doc(s, '1', 'copper wire conducts heat', { metadata: { status: 'archived' } })], ctx(s));
expect(embedder.calls).toBe(2);
const [hit] = await adapter.search('copper', { source: s, topK: 1, index: { fingerprint: FP_A.fingerprint } });
expect(hit).toMatchObject({ documentId: `${s.id}:1`, metadata: { status: 'archived' } });
});
it('beginBuild under its own fingerprint opens nothing; a search under another is refused and never served', async () => {
const adapter = store.adapter();
const s = source('build');
await adapter.upsert([doc(s, '1', 'granite quarry')], ctx(s, FP_A));
await expect(adapter.beginBuild(ctx(s, FP_A))).resolves.toBeUndefined();
expect(await adapter.indexStatus(ctx(s))).toEqual({ fingerprint: FP_A.fingerprint });
await adapter.completeBuild(ctx(s, FP_A));
const err = await adapter.search('granite', { source: s, topK: 3, index: { fingerprint: FP_B.fingerprint } }).then(
() => undefined,
(e: unknown) => e as { code?: string; status?: number; message?: string; remedy?: string },
);
expect(err).toMatchObject({ code: KNOWLEDGE_REBUILD_REQUIRED, status: 409, message: expect.stringContaining('needs a rebuild') });
expect(err?.message).not.toContain('DELETE');
// The operator's remedy is a complete reindex, never statements to run by hand.
expect(err?.remedy).toContain('complete reindex');
expect(err?.remedy).not.toMatch(/DELETE FROM|DROP |TRUNCATE/);
expect(await adapter.indexStatus(ctx(s))).toEqual({ fingerprint: FP_A.fingerprint, recipeHash: FP_A.recipeHash });
});
it('keeps its tables in its own schema, never the connection\'s current schema', async () => {
const adapter = store.adapter();
const s = source('where');
await adapter.upsert([doc(s, '1', 'granite quarry')], ctx(s));
const own = (adapter as unknown as { schema: string }).schema;
const tables = await store.pool().query<{ table_schema: string; table_name: string }>(
`SELECT table_schema, table_name FROM information_schema.tables
WHERE table_name LIKE 'knowledge\\_%' AND table_schema IN ($1, current_schema()) ORDER BY table_name`,
[own],
);
expect(tables.rows).toEqual([
{ table_schema: own, table_name: 'knowledge_generation' },
{ table_schema: own, table_name: 'knowledge_vec_d384' },
]);
});
});
/** A data engine over fixed rows, for the service's walk: `find` with a keyset on `id`, and `count`. */
function rowsEngine(rows: Array<Record<string, unknown>>) {
const sorted = [...rows].sort((a, b) => (String(a.id) < String(b.id) ? -1 : String(a.id) > String(b.id) ? 1 : 0));
const matches = (row: Record<string, unknown>, where: Record<string, unknown> | undefined): boolean => {
if (!where) return true;
return Object.entries(where).every(([k, v]) => {
if (k === '$and') return (v as Array<Record<string, unknown>>).every((w) => matches(row, w));
if (v !== null && typeof v === 'object') {
const cond = v as Record<string, unknown>;
if ('$gt' in cond) return String(row[k]) > String(cond.$gt);
if ('$in' in cond) return (cond.$in as unknown[]).includes(row[k]);
throw new Error(`rowsEngine: condition ${JSON.stringify(v)} is not implemented`);
}
return row[k] === v;
});
};
return {
async find(_object: string, query: { where?: Record<string, unknown>; limit?: number }) {
const out = sorted.filter((r) => matches(r, query?.where));
return query?.limit === undefined ? out : out.slice(0, query.limit);
},
async count(_object: string, query: { where?: Record<string, unknown> }) {
return sorted.filter((r) => matches(r, query?.where)).length;
},
} as never;
}