Skip to content

Commit e8b0688

Browse files
committed
test(service-knowledge): pin the plugin's tenancy-posture wiring and the event sync on an unreadable posture; TSDoc on organizationId with docHash
Claude-Session: https://claude.ai/code/session_01QSeKYkS1d2QkEUgqhneuU1 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3df745d commit e8b0688

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎packages/services/service-knowledge/src/__tests__/knowledge-service.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { describe, it, expect, vi } from 'vitest';
4+
import { LiteKernel, ObjectKernel } from '@objectstack/core';
5+
import type { Plugin, PluginContext } from '@objectstack/core';
6+
import { KnowledgeServicePlugin } from '../knowledge-service-plugin.js';
47
import {
58
DOC_HASH_VERSION,
69
KnowledgeService,
@@ -1373,3 +1376,93 @@ describe('KnowledgeService — search hands the adapter the caller\'s organizati
13731376
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('tenancy posture could not be read'));
13741377
});
13751378
});
1379+
1380+
// The plugin's `resolveTenancyPosture` wiring is the only production reader of
1381+
// the posture (ADR-0161 K8, item 9): broken, every deployment reads as
1382+
// `single`, no document is stamped and no filter is handed, silently. So the
1383+
// wiring is driven through real kernels with a `tenancy` service: `LiteKernel`
1384+
// (no async accessor: the sync leg) and `ObjectKernel` (`getServiceAsync`).
1385+
describe('KnowledgeServicePlugin reads the tenancy posture from the kernel (ADR-0161 K8, item 9)', () => {
1386+
const PERSON = {
1387+
userId: 'u1', tenantId: 'org_a', accessible_org_ids: ['org_a'], positions: [], permissions: [], systemPermissions: [], isSystem: false,
1388+
} as never;
1389+
1390+
class FakeHostPlugin implements Plugin {
1391+
name = 'test.fake.host';
1392+
version = '1.0.0';
1393+
type = 'standard' as const;
1394+
constructor(private readonly tenancy: unknown) {}
1395+
async init(ctx: PluginContext): Promise<void> {
1396+
ctx.registerService('objectql', { find: vi.fn(async () => []) });
1397+
if (this.tenancy !== undefined) ctx.registerService('tenancy', this.tenancy);
1398+
}
1399+
}
1400+
1401+
async function boot(Kernel: typeof LiteKernel | typeof ObjectKernel, tenancy: unknown) {
1402+
// `ObjectKernel` refuses to start without a `data` service; this harness composes none.
1403+
const kernel = Kernel === ObjectKernel
1404+
? new ObjectKernel({ skipSystemValidation: true, logger: { level: 'silent' }, gracefulShutdown: false })
1405+
: new LiteKernel();
1406+
await kernel.use(new FakeHostPlugin(tenancy));
1407+
await kernel.use(new KnowledgeServicePlugin({ enableEventSync: false, sources: [objectSource('s1', 'task')] }));
1408+
await kernel.bootstrap();
1409+
const svc = kernel instanceof ObjectKernel
1410+
? await kernel.getServiceAsync<KnowledgeService>('knowledge')
1411+
: kernel.getService<KnowledgeService>('knowledge');
1412+
const adapter = makeAdapter('memory');
1413+
svc.registerAdapter('memory', adapter);
1414+
return { kernel, svc, adapter };
1415+
}
1416+
1417+
it.each([
1418+
['LiteKernel', LiteKernel],
1419+
['ObjectKernel', ObjectKernel],
1420+
] as const)('%s: a walled posture hands the adapter the caller\'s organizations', async (_name, Kernel) => {
1421+
const { kernel, svc, adapter } = await boot(Kernel, { posture: 'isolated' });
1422+
await svc.search('q', { executionContext: PERSON });
1423+
expect(adapter.searchSpy).toHaveBeenCalledOnce();
1424+
expect(adapter.searchSpy.mock.calls[0][1]).toMatchObject({ organizationIds: ['org_a'] });
1425+
await kernel.shutdown();
1426+
});
1427+
1428+
it('no `tenancy` service registered: no wall exists, and no filter is handed', async () => {
1429+
const { kernel, svc, adapter } = await boot(LiteKernel, undefined);
1430+
await svc.search('q', { executionContext: PERSON });
1431+
expect(adapter.searchSpy).toHaveBeenCalledOnce();
1432+
expect(adapter.searchSpy.mock.calls[0][1]).not.toHaveProperty('organizationIds');
1433+
await kernel.shutdown();
1434+
});
1435+
1436+
it('a `tenancy` service that is registered but cannot be read: no object source is searched', async () => {
1437+
const unreadable = Object.defineProperty({}, 'posture', {
1438+
get: () => {
1439+
throw new Error('tenancy store down');
1440+
},
1441+
});
1442+
const { kernel, svc, adapter } = await boot(LiteKernel, unreadable);
1443+
expect(await svc.search('q', { executionContext: PERSON })).toEqual([]);
1444+
expect(adapter.searchSpy).not.toHaveBeenCalled();
1445+
await kernel.shutdown();
1446+
});
1447+
});
1448+
1449+
describe('KnowledgeService — an event sync whose tenancy posture cannot be read (ADR-0161 K8, item 9)', () => {
1450+
it('logs at warn and leaves the stored document as it was: no upsert, no delete', async () => {
1451+
const adapter = makeAdapter('memory');
1452+
const row = { id: 'r1', title: 'A', notes: 'B', status: 'open', organization_id: 'org_a' };
1453+
const warn = vi.fn();
1454+
const svc = new KnowledgeService({
1455+
dataEngine: { find: vi.fn(async () => [row]) } as unknown as IDataEngine,
1456+
logger: { warn },
1457+
resolveTenancyPosture: async () => {
1458+
throw new Error('tenancy store down');
1459+
},
1460+
});
1461+
svc.registerAdapter('memory', adapter);
1462+
svc.registerSource(objectSource('s1', 'task'));
1463+
await svc.handleRecordUpsert('task', row);
1464+
expect(adapter.upsertSpy).not.toHaveBeenCalled();
1465+
expect(adapter.deleteSpy).not.toHaveBeenCalled();
1466+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('tenancy store down'));
1467+
});
1468+
});

‎packages/spec/src/ai/knowledge-document.zod.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ export const KnowledgeDocumentSchema = lazySchema(() => z.object({
5757
* caller-supplied value is refused by `indexDocument`. Stored by the adapter
5858
* and read by its search: when the search is handed
5959
* `AdapterSearchOptions.organizationIds`, only documents whose organization
60-
* is in that set are ranked, so a document without one is not.
60+
* is in that set are ranked, so a document without one is not. An adapter
61+
* that stores `docHash` stores `organizationId` from the same version on:
62+
* the hash already covers the organization, so an adapter that held only the
63+
* hash would have every document skipped by a later complete reindex, and a
64+
* walled search would never rank them.
6165
*/
6266
organizationId: z.string().optional()
6367
.describe('Organization of the record the document was assembled from; set by the knowledge service under a walled tenancy posture'),

0 commit comments

Comments
 (0)