Skip to content

Commit a252590

Browse files
committed
test(runtime): pin the duplicate and save doors' wording of the platform knowledge service's verdict
Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP Co-authored-by: Claude <noreply@anthropic.com>
1 parent f19350b commit a252590

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

‎packages/runtime/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"@objectstack/service-analytics": "workspace:*",
8282
"@objectstack/service-datasource": "workspace:*",
8383
"@objectstack/service-job": "workspace:*",
84+
"@objectstack/service-knowledge": "workspace:*",
8485
"@objectstack/service-messaging": "workspace:*",
8586
"@objectstack/service-package": "workspace:*",
8687
"@objectstack/service-record-context": "workspace:*",

‎packages/runtime/src/domains/packages-knowledge-source-gate.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import '@objectstack/metadata';
2727
import { describe, it, expect, vi } from 'vitest';
2828
import { ObjectKernel, ServiceLifecycle } from '@objectstack/core';
29+
import type { Plugin, PluginContext } from '@objectstack/core';
30+
// The platform's knowledge service, for the doors' wording of its own verdict.
31+
import { KnowledgeServicePlugin, type KnowledgeService } from '@objectstack/service-knowledge';
2932
import { HttpDispatcher } from '../http-dispatcher.js';
3033

3134
const ctx = (executionContext: any): any => ({ request: {}, environmentId: 'platform', executionContext });
@@ -415,3 +418,123 @@ describe('/packages — what the gate leaves alone', () => {
415418
expect(s.registry.installPackage).toHaveBeenCalled();
416419
});
417420
});
421+
422+
/**
423+
* The verdict the platform's own knowledge service answers, through both doors
424+
* that ask it: the package duplicate and the `/meta` save. A real kernel runs
425+
* `KnowledgeServicePlugin` over host doubles (the engine, the security
426+
* service, the metadata protocol), so the sentence each door returns is the
427+
* one `admitAuthoredSource` composes for that door, not a double's.
428+
*
429+
* The duplicate's caller copies a package: never told to save a source, and
430+
* told once, by the door's prefix, that nothing was copied. The `/meta`
431+
* save's sentences are the control, pinned byte for byte.
432+
*/
433+
describe('/packages duplicate and /meta save on a real kernel running KnowledgeServicePlugin: each door words the service\'s own verdict', () => {
434+
const ARTICLE = {
435+
name: 'article',
436+
fields: { id: { type: 'text' }, title: { type: 'text' }, body: { type: 'textarea' }, salary: { type: 'textarea' } },
437+
};
438+
const FIELDS = Object.keys(ARTICLE.fields);
439+
type Arm = 'transient' | 'content' | 'where' | 'title';
440+
const sourceFor = (arm: Arm) => ({
441+
name: 'faq', label: 'FAQ', adapter: 'memory',
442+
source: {
443+
kind: 'object', object: 'article', content: arm === 'content' ? ['body', 'salary'] : ['body'],
444+
...(arm === 'where' ? { where: { salary: { $ne: null } } } : {}),
445+
},
446+
});
447+
/** What the save door answers today, byte for byte. */
448+
const SAVE_VERDICT: Record<Arm, { status: number; code: string; message: string }> = {
449+
transient: {
450+
status: 503, code: 'SERVICE_UNAVAILABLE',
451+
message: 'The knowledge source "faq" was not saved: whether you may search object \'article\' could not be judged '
452+
+ '(registry unavailable), and an author must be allowed to search what their source indexes. Nothing was written; try again.',
453+
},
454+
content: {
455+
status: 403, code: 'PERMISSION_DENIED',
456+
message: 'The knowledge source "faq" was not saved: you may not query field \'salary\' of object \'article\' (it is hidden '
457+
+ 'from you, or served to you masked), and the index would hold its text unmasked. Remove the field from `content` '
458+
+ 'and `metadataFields`, or have someone who may query it save the source.',
459+
},
460+
where: {
461+
status: 403, code: 'PERMISSION_DENIED',
462+
message: 'The knowledge source "faq" was not saved: its `where` reads field \'salary\' of object \'article\', which you may '
463+
+ 'not query (it is hidden from you, or served to you masked), and the source would select its records by that '
464+
+ 'field. Remove the field from `where`, or have someone who may query it save the source.',
465+
},
466+
title: {
467+
status: 403, code: 'PERMISSION_DENIED',
468+
message: 'The knowledge source "faq" was not saved: you may not query field \'title\' of object \'article\' (it is hidden '
469+
+ 'from you, or served to you masked), and the index would hold its text unmasked. The source does not list it: '
470+
+ "it is the object's display field, which every document carries as its title. Have someone who may query it save the source.",
471+
},
472+
};
473+
474+
async function bootPlatformKnowledge(arm: Arm) {
475+
const s = make({ items: [sourceFor(arm)] });
476+
const saveMetaItem = vi.fn(async ({ name }: any) => ({ success: true, name }));
477+
Object.assign(s.protocol, { saveMetaItem });
478+
// The engine the knowledge service reads schemas through; the package registry rides on it.
479+
Object.assign(s.kernelServices.objectql, {
480+
getSchema: (name: string) => {
481+
if (arm === 'transient') throw new Error('registry unavailable');
482+
return name === 'article' ? ARTICLE : undefined;
483+
},
484+
find: vi.fn(async () => []),
485+
});
486+
const hidden = arm === 'title' ? 'title' : arm === 'transient' ? undefined : 'salary';
487+
const security = {
488+
getQueryableFields: vi.fn(async () => FIELDS.filter((f) => f !== hidden)),
489+
getReadableFields: vi.fn(async () => FIELDS),
490+
};
491+
const doubles: Record<string, unknown> = { ...s.kernelServices, security };
492+
const host: Plugin = {
493+
name: 'test.duplicate-door.host',
494+
version: '1.0.0',
495+
type: 'standard',
496+
async init(ctx: PluginContext) {
497+
for (const [name, service] of Object.entries(doubles)) ctx.registerService(name, service);
498+
},
499+
} as Plugin;
500+
const kernel = new ObjectKernel({ gracefulShutdown: false, skipSystemValidation: true });
501+
kernel.use(host);
502+
kernel.use(new KnowledgeServicePlugin({ enableEventSync: false }));
503+
await kernel.bootstrap();
504+
const knowledge = kernel.getService<KnowledgeService>('knowledge');
505+
knowledge.registerAdapter('memory', { id: 'memory', upsert: async () => undefined, delete: async () => undefined, search: async () => [] });
506+
const dispatcher = new HttpDispatcher(kernel as any);
507+
return {
508+
...s, kernel, saveMetaItem,
509+
duplicate: (caller: any) => dispatcher.handlePackages('/pkg-a/duplicate', 'POST', { targetPackageId: 'pkg-b' }, {}, caller),
510+
save: (caller: any) => dispatcher.handleMetadata('/knowledge_source/faq', caller, 'PUT', JSON.parse(JSON.stringify(sourceFor(arm)))),
511+
};
512+
}
513+
514+
for (const arm of ['transient', 'content', 'where', 'title'] as const) {
515+
it(`${arm}: the duplicate is told nothing was copied once and to duplicate the package, never to save; the /meta save's sentence is unchanged`, async () => {
516+
const stack = await bootPlatformKnowledge(arm);
517+
try {
518+
const expected = SAVE_VERDICT[arm];
519+
const r = await stack.duplicate(authed(BOTH));
520+
expect(r.response?.status).toBe(expected.status);
521+
expect(r.response?.body?.error?.code).toBe(expected.code);
522+
const message: string = r.response?.body?.error?.message;
523+
expect(message.startsWith('Duplicating package "pkg-a" is refused, and nothing was copied. The knowledge source "faq" cannot be copied: '), message).toBe(true);
524+
expect(message).not.toMatch(/save the source|save it again|Nothing was written|was not saved|the save /);
525+
expect(message.match(/nothing was copied/gi)).toHaveLength(1);
526+
expect(message).toMatch(arm === 'title' ? /Have someone who may query it duplicate the package\.$/ : /duplicate the package again/);
527+
expect(stack.protocol.duplicatePackage).not.toHaveBeenCalled();
528+
529+
// CONTROL: the same verdict at the /meta save door, byte for byte as before.
530+
const saved = await stack.save(authed(BOTH));
531+
expect(saved.response?.status).toBe(expected.status);
532+
expect(saved.response?.body?.error?.code).toBe(expected.code);
533+
expect(saved.response?.body?.error?.message).toBe(expected.message);
534+
expect(stack.saveMetaItem).not.toHaveBeenCalled();
535+
} finally {
536+
await stack.kernel.shutdown();
537+
}
538+
});
539+
}
540+
});

‎packages/runtime/vitest.config.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ export default defineConfig({
216216
// the #4567 regression (croner rejecting the expression envelope) is
217217
// reproduced by the actual scheduler rather than by a double.
218218
{ find: '@objectstack/service-job', replacement: path.resolve(__dirname, '../services/service-job/src/index.ts') },
219+
// Dev-only: packages-knowledge-source-gate.test.ts drives the package
220+
// duplicate and the `/meta` save over the REAL `KnowledgeServicePlugin`
221+
// (ADR-0161 K4, guard two), so the verdict each door words is the
222+
// service's in the checkout, never a built copy of it. Anchored: the pin
223+
// imports only the root entry.
224+
{
225+
find: /^@objectstack\/service-knowledge$/,
226+
replacement: path.resolve(__dirname, '../services/service-knowledge/src/index.ts'),
227+
},
219228
// Dev-only: src/security/request-execution-context.conformance.test.ts
220229
// drives the REAL settings door (`SettingsServicePlugin`'s context
221230
// resolver) as one row of ruling record 6227928430's conformance test, so

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)