|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Equalities the affinity order (ADR-0161, the dated note of 2026-11-29 |
| 5 | + * under K1) states but no code shares, pinned so that neither side moves |
| 6 | + * alone, and the one bound it states with what that bound covers: |
| 7 | + * |
| 8 | + * - `KNOWLEDGE_AFFINITY_MAX_PLACES` (spec) is the largest `topK` of the |
| 9 | + * `search_knowledge` tool (`SEARCH_KNOWLEDGE_MAX_TOP_K`, this package): the |
| 10 | + * jsdoc and the refusal of `places` name the one as the other. |
| 11 | + * - An affinity entry compares a stored value with a caller fact as a scope |
| 12 | + * term compares it with its ids (`elementsOf` of `knowledge-affinity.ts` |
| 13 | + * against `valueInIds` of `knowledge-search-scope.ts`): strings and numbers |
| 14 | + * as `String(value)`, a list by any element, anything else never. |
| 15 | + * - The `user` fact is the user the one resolver of the caller's identity |
| 16 | + * (`resolveCallerIdentity`, `@objectstack/core`) answers: the order reads it |
| 17 | + * from the context under its own copy of that resolver's guard. |
| 18 | + * - The places bound is one entry's: two `boost` entries on one field add. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, expect, it } from 'vitest'; |
| 22 | +import { resolveCallerIdentity, type CallerRecordReader } from '@objectstack/core'; |
| 23 | +import { KNOWLEDGE_AFFINITY_MAX_PLACES, type KnowledgeAffinityEntry, type KnowledgeHit } from '@objectstack/spec/ai'; |
| 24 | +import { SEARCH_KNOWLEDGE_MAX_TOP_K } from '../knowledge-tools.js'; |
| 25 | +import { elementsOf, orderByAffinity, resolveAffinityFacts } from '../knowledge-affinity.js'; |
| 26 | +import { valueInIds } from '../knowledge-search-scope.js'; |
| 27 | + |
| 28 | +describe('affinity pins — the stated equalities hold', () => { |
| 29 | + it('the places bound of one boost entry is the largest topK of search_knowledge', () => { |
| 30 | + expect(KNOWLEDGE_AFFINITY_MAX_PLACES).toBe(SEARCH_KNOWLEDGE_MAX_TOP_K); |
| 31 | + }); |
| 32 | + |
| 33 | + it('an affinity entry and a scope term read a stored value alike', () => { |
| 34 | + const values: unknown[] = [ |
| 35 | + 'bu_1', '', 7, 0, -0, 7.5, Number.NaN, Number.POSITIVE_INFINITY, true, false, null, undefined, |
| 36 | + {}, { id: 'bu_1' }, [], ['bu_1', 7, null, {}, true], [['bu_1']], [7.5, 'x'], |
| 37 | + ]; |
| 38 | + const ids = ['bu_1', '', '7', '0', '7.5', 'NaN', 'Infinity', 'true', 'false', 'null', 'undefined', '[object Object]', 'x']; |
| 39 | + for (const value of values) { |
| 40 | + for (const id of ids) { |
| 41 | + expect(valueInIds(value, new Set([id])), `${JSON.stringify(value)} against '${id}'`) |
| 42 | + .toBe(elementsOf(value).includes(id)); |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it('the user fact is the user resolveCallerIdentity answers, for every shape of userId', async () => { |
| 48 | + const read: CallerRecordReader = async () => undefined; |
| 49 | + for (const userId of ['u_ada', '', ' ', 7, 0, null, undefined, true, {}, ['u_ada']]) { |
| 50 | + const facts = await resolveAffinityFacts({ userId, isSystem: false }, read, { user: true, businessUnit: false }); |
| 51 | + const identity = await resolveCallerIdentity({ userId }, read); |
| 52 | + expect(facts.user, `userId: ${JSON.stringify(userId)}`).toBe(identity.user?.id); |
| 53 | + } |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('affinity pins — the places bound is one entry\'s', () => { |
| 58 | + const unit = (id: string, owner: string): KnowledgeHit => ({ |
| 59 | + chunkId: `${id}#0`, documentId: id, sourceId: 'S', sourceRecordId: id, score: 0, snippet: id, metadata: { owner }, |
| 60 | + }); |
| 61 | + const boost: KnowledgeAffinityEntry = { field: 'owner', caller: 'user', mode: 'boost', places: KNOWLEDGE_AFFINITY_MAX_PLACES }; |
| 62 | + const placeOfMatch = (entries: KnowledgeAffinityEntry[]): number => { |
| 63 | + const list = Array.from({ length: 45 }, (_, i) => unit(`u${i + 1}`, i === 44 ? 'u_ada' : 'u_lin')); |
| 64 | + const { units } = orderByAffinity(list, (u) => u, () => entries, { user: 'u_ada', businessUnits: [] }); |
| 65 | + return units.findIndex((u) => u.documentId === 'u45'); |
| 66 | + }; |
| 67 | + |
| 68 | + it('two boosts on one field at the bound each: the match passes 40 units, not 20', () => { |
| 69 | + // u45 is number 45. One entry: key 25, it stands before u25 (the key tie goes to the larger sum). |
| 70 | + expect(placeOfMatch([boost])).toBe(24); |
| 71 | + // The same (field, caller) pair twice: key 5, it passes 40 units and stands fifth. |
| 72 | + expect(placeOfMatch([boost, boost])).toBe(4); |
| 73 | + }); |
| 74 | +}); |
0 commit comments