Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/38041-knowledge-source-closure-system-object.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@objectstack/lint': minor
---

fix(lint)!: under a stated package closure, a knowledge source naming a `sys_` object the host does not serve is refused at publish; the no-package refusal is worded for its author

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — the published skill already teaches that a `source.object` naming no object is refused, and names no platform-name arm this change moves

<!-- adr-0087: not-required (already-registered knowledge-source-publish-refuses-missing-references-and-gated-objects) the entry that registered this rule's publish refusal landed with the rule itself; this change extends the same refusal to an absent `sys_` name judged under a stated package closure, which keeps every system object the host serves. No id is added and no key is added, removed or re-shaped. -->

ADR-0161 K4, row 4.

**BREAKING** for the publish of a packaged knowledge source whose `source.object` is a `sys_` name the host does not serve: when the publish gate states the package's closure, such a name, known (`sys_user`) or not, is now a `knowledge-source-object-unknown` error (422). Before, a known one was not judged, and an unknown one was refused with a dependency remedy that cannot help, because the closure already keeps every system object the host serves. FROM a source over such an object TO a source over an object the host serves: name the system object by its full name, or install the package that registers it where this package is installed. A `cloud_` or `ai_` name is not on this arm: the closure keeps such an object only when its definition marks it `isSystem: true`, so its verdict is unchanged. On the command line nothing changes.

- A knowledge source write that names no package, refused for an object the host does not serve, now tells its author to name an object the organization has or install the package that provides it, instead of a stack and a manifest that author does not have.
- `ai_context`: a `body.field` of `id` is reported as the record id every object carries, not as "a untyped field"; a declared field's type now takes the right article.
- `os explain knowledge-source-object-unknown` and the rule's header state which platform-name arm holds on the command line, under a closure, and against the whole collection.
15 changes: 10 additions & 5 deletions packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6321,7 +6321,8 @@ const AI_CONTEXT_SORT_FIELD_UNSORTABLE_EXPLANATION: RuleExplanation = {

const KNOWLEDGE_SOURCE_DOORS =
'Judged by `os validate`, `os build` and `os lint`, and at the runtime publish gate for a ' +
'`knowledge_source` write (Studio, the metadata API, MCP), with the same rule id and message. An error ' +
'`knowledge_source` write (Studio, the metadata API, MCP), with the same rule id and message, except that an ' +
'object verdict at the gate names the remedy its author has. An error ' +
'refuses the publish (ADR-0161 K4): a source over a missing object has nothing to index, a missing field ' +
'indexes no text for that member, and a gated object is never searched for a caller the service judges.';

Expand All @@ -6337,10 +6338,14 @@ const KNOWLEDGE_SOURCE_OBJECT_UNKNOWN_EXPLANATION: RuleExplanation = {
'registers (`sys_user`) is not judged when the stack does not carry its definition, and a name with a ' +
'platform prefix that no known package registers is a warning, because a third-party package may still ' +
'provide it. At the publish gate with no package stated, the source is judged against every object the ' +
'host serves, so either one is an error there when the host does not serve it. When the publish ' +
'gate states the package\'s closure (the package, the packages its manifest declares in ' +
'`dependencies`, and platform objects), an object outside it is reported with the remedy to declare the ' +
'dependency.',
'host serves, so either one is an error there when the host does not serve it, worded for the ' +
'organization\'s author: name an object the organization has, or install the package that provides it. ' +
'When the publish gate states the package\'s closure (the package, the packages its manifest declares ' +
'in `dependencies`, and platform objects), the closure keeps every `sys_` object the host serves, so a ' +
'`sys_` name the host does not serve, known or not, is an error with no dependency to declare. A known ' +
'platform object of another prefix (`cloud_`, `ai_`) is not judged there, because the closure keeps such ' +
'an object only when its definition marks it a system object; any other object outside the closure is ' +
'reported with the remedy to declare the dependency.',
'On the command line a package the manifest declares in `dependencies` but the artifact does not carry ' +
'is out of sight: its objects cannot be read there. So while the manifest declares such a dependency, ' +
'an object the stack does not define is a warning, which clears when that dependency defines it (the ' +
Expand Down
9 changes: 9 additions & 0 deletions packages/lint/src/validate-ai-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ describe('validateAiContext — ADR-0160 D1 author defects', () => {
expect(rulesOf({ objects, aiContexts: [pack({ body: { field: 'id' } })] })).toEqual([
[AI_CONTEXT_BODY_NOT_LONG_TEXT, 'aiContexts[0].body.field'],
]);
// The text names `id` as the record id, not as a typeless field.
const [finding] = validateAiContext({ objects, aiContexts: [pack({ body: { field: 'id' } })] });
expect(finding!.message).toBe(
"`body.field` names 'id', the record id every object carries; a body is a long-text field "
+ '(textarea, markdown, html, richtext), so the pack carries no body',
);
// CONTROL: a declared field keeps its type in the text, with the article it takes.
const [typed] = validateAiContext({ objects, aiContexts: [pack({ body: { field: 'created_at' } })] });
expect(typed!.message).toMatch(/^`body\.field` names 'created_at', a datetime field; /);
});

it('a column the platform does not inject on the object is still unknown: owner_id under ownership org', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/lint/src/validate-ai-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,19 @@ export function validateAiContext(
hint: `Name a long-text field of '${root}' (check the spelling)`,
});
} else if (!(AI_CONTEXT_BODY_FIELD_TYPES as readonly string[]).includes(str(def?.type))) {
// No definition means a column addressable without one: `id`, which
// the driver provisions on every object, is the only such name.
const type = str(def?.type);
const what = !def
? bodyField === 'id' ? 'the record id every object carries' : 'a column with no declared type'
: type ? `${/^[aeiou]/i.test(type) ? 'an' : 'a'} ${type} field` : 'a field with no declared type';
out.push({
severity: 'warning',
rule: AI_CONTEXT_BODY_NOT_LONG_TEXT,
where: `${at} · body`,
path: `aiContexts[${i}].body.field`,
message:
`\`body.field\` names '${bodyField}', a ${str(def?.type) || 'untyped'} field; a body is a long-text ` +
`\`body.field\` names '${bodyField}', ${what}; a body is a long-text ` +
`field (${AI_CONTEXT_BODY_FIELD_TYPES.join(', ')}), so the pack carries no body`,
hint: `Name a long-text field of '${root}' as the body, or list '${bodyField}' in \`fields\` instead`,
});
Expand Down
72 changes: 70 additions & 2 deletions packages/lint/src/validate-knowledge-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ describe('validateKnowledgeSource on the runtime publish door', () => {
const advised = verdict.advisories.filter((f) => KS_RULE.test(f.rule));
expect((severity === 'error' ? refused : advised).map((f) => f.rule)).toEqual([rule]);
expect((severity === 'error' ? advised : refused)).toEqual([]);
expect([...refused, ...advised].map(face)).toEqual(atValidate(source).map(face));
// A write that names no package is worded for its author, who has no
// stack and no manifest; every other verdict prints the CLI's words.
const sameWords = rule !== KNOWLEDGE_SOURCE_OBJECT_UNKNOWN;
const at = (f: ReturnType<typeof face>) => (sameWords ? f : { rule: f.rule, severity: f.severity, where: f.where });
expect([...refused, ...advised].map(face).map(at)).toEqual(atValidate(source).map(face).map(at));
});
}

Expand Down Expand Up @@ -259,7 +263,7 @@ describe('validateKnowledgeSource on the runtime publish door', () => {
const verdict = runRuntimeAuthoringRules({ type: 'knowledge_source', item, context: { objects } });
expect(verdict.errors.map((f) => [f.rule, f.severity, f.path]), object)
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error', 'knowledgeSources[0].source.object']]);
expect(verdict.errors[0]!.message, object).toContain(`'${object}', which this stack does not define`);
expect(verdict.errors[0]!.message, object).toContain(`'${object}', which is not an object of this organization`);
expect(verdict.advisories.filter((f) => KS_RULE.test(f.rule)), object).toEqual([]);
}
// The CLI arms stay: a prefixed name no known package registers warns, a known platform name is not judged.
Expand All @@ -275,6 +279,70 @@ describe('validateKnowledgeSource on the runtime publish door', () => {
}).errors).toEqual([]);
});

it('a write that names no package is refused in words that name neither a stack nor a manifest', () => {
const verdict = runRuntimeAuthoringRules({ type: 'knowledge_source', item: kbSource({ object: 'kb_artcle' }), context: { objects } });
expect(verdict.errors.map((f) => [f.rule, f.severity])).toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error']]);
const [finding] = verdict.errors;
for (const word of [/\bstack\b/i, /manifest/i, /dependencies/]) {
expect(finding!.message).not.toMatch(word);
expect(finding!.hint).not.toMatch(word);
}
expect(finding!.message).toContain('organization');
expect(finding!.hint).toMatch(/install the package/);
// CONTROL: the CLI keeps its stack-and-manifest remedy, which its author can follow.
expect(atValidate(kbSource({ object: 'kb_artcle' }))[0]!.hint).toMatch(/manifest's `dependencies`/);
});

/**
* Limb 1 of the closure (`narrowObjectsToPackageClosure`) keeps every object
* `isSystemObject` accepts, and by name alone that is every `sys_` object:
* under a stated closure an absent `sys_` name is absent from the host.
*/
describe('under a stated closure, an absent sys_ name', () => {
const scope = { packageId: 'app.kb', dependencies: [] as string[] };
const served = [
...objects.map((o) => ({ ...o, _packageId: 'app.kb' })),
{ name: 'sys_help_topic', _packageId: 'com.platform.help', enable: { aiReadable: true }, fields: { body: { type: 'text' } } },
];
const atScoped = (object: string, context = served) => runRuntimeAuthoringRules({
type: 'knowledge_source',
item: kbSource({ object, content: ['body'], metadataFields: [] }),
context: { objects: context },
packageScope: scope,
});

it('is refused, known or not, with no dependency remedy', () => {
for (const object of ['sys_user', 'sys_kb_article']) {
const verdict = atScoped(object);
expect(verdict.errors.map((f) => [f.rule, f.severity, f.path]), object)
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error', 'knowledgeSources[0].source.object']]);
expect(verdict.errors[0]!.message, object).toContain(`'${object}', a system object the host does not serve`);
expect(verdict.errors[0]!.hint, object).not.toMatch(/dependenc/);
expect(verdict.advisories.filter((f) => KS_RULE.test(f.rule)), object).toEqual([]);
}
});

it('CONTROL: a sys_ object the host serves, from a package outside the closure, passes', () => {
expect(atScoped('sys_help_topic').errors).toEqual([]);
});

it('cloud_ and ai_ names are not on that arm: limb 1 keeps them only on their definition\'s isSystem', () => {
const foreign = (extra: Record<string, unknown>) => [
...served,
...['cloud_usage_note', 'ai_memory_note'].map((name) => ({ name, _packageId: 'com.cloud', ...extra, fields: { body: { type: 'text' } } })),
];
for (const object of ['cloud_usage_note', 'ai_memory_note']) {
// Not isSystem: the closure drops it, so the verdict is the closure's, with the dependency remedy.
const dropped = atScoped(object, foreign({}));
expect(dropped.errors.map((f) => f.rule), object).toEqual([KNOWLEDGE_SOURCE_OBJECT_UNKNOWN]);
expect(dropped.errors[0]!.message, object).toContain('outside the closure of package "app.kb"');
expect(dropped.errors[0]!.hint, object).toMatch(/^Declare a dependency on the package that defines/);
// isSystem: true: limb 1 keeps it, and the source is clean.
expect(atScoped(object, foreign({ isSystem: true })).errors, object).toEqual([]);
}
});
});

it('an empty whole collection at the door is no universe, and judges no object', () => {
const stack = { objects: [], knowledgeSources: [kbSource({ object: 'faq_entry' })] };
expect(validateKnowledgeSource(stack, { wholeCollection: true })).toEqual([]);
Expand Down
61 changes: 53 additions & 8 deletions packages/lint/src/validate-knowledge-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@
* A `warning` with the hedge on the CLI when the manifest declares such a
* dependency: the build cannot see that package's objects, and the publish
* door judges the source against its closure, which holds them (the
* `flow-trigger-unknown-object` split). The ladder
* `flow-trigger-unknown-object` split). On the CLI the ladder
* `validateObjectReferences` resolves object names by also applies: a
* platform object a known package registers is not judged when the stack
* does not carry it (its declaration is not in hand), and a
* platform-prefixed name no known package registers is a `warning`; both
* on the CLI and under a closure. Against the whole collection at the door
* either one is absent from every package the host serves, so it is an
* `error` as an unprefixed name is. An empty whole collection at the door
* is no universe, and judges nothing.
* platform-prefixed name no known package registers is a `warning`. Under
* a closure only the first holds, and only for a name the closure may
* drop. The closure keeps every system object the host serves
* (`narrowObjectsToPackageClosure` in `runtime-gate.ts`, limb 1, which asks
* `isSystemObject`), and by name alone that reading keeps every `sys_`
* object, so an absent `sys_` name, known or not, exists nowhere the host
* serves: an `error`, with no dependency remedy. A `cloud_` or `ai_` name
* is not on that arm: limb 1 keeps such an object only when its definition
* says `isSystem: true`, which the name does not tell, so its absence may
* be the closure's own; a known one is not judged, and any other is
* outside the closure. Against the whole collection at the door every
* absent name, platform or not, is absent from every package the host
* serves, so it is an `error`, worded for an author with no stack and no
* manifest (the organization's objects, or a package to install). An
* empty whole collection at the door is no universe, and judges nothing.
* - `knowledge-source-field-unknown` (`error`): a `source.content` or
* `source.metadataFields` member names no field of the object: neither one
* it declares (its own stack's extensions folded in, `objectsAsServed`) nor
Expand Down Expand Up @@ -82,6 +92,7 @@ import { hasPlatformObjectPrefix, isPlatformProvidedObjectName } from '@objectst
import { collectionEntries } from './collection-entries.js';
import { closureDependencyRemedy, objectsAsServed, packagesOf, recordsOf, suggestName } from './object-graph.js';
import { injectedColumnsFor } from './system-fields.js';
import { isSystemObject } from './validate-security-posture.js';

export type KnowledgeSourceSeverity = 'error' | 'warning';

Expand Down Expand Up @@ -213,11 +224,30 @@ export function validateKnowledgeSource(

if (!def) {
if (noUniverse) continue;
// Limb 1 of the closure keeps every system object the host serves, and
// asked by name alone (`isSystemObject`) it keeps every `sys_` one: an
// absent `sys_` name is as final under a closure as against the whole
// collection. A `cloud_` / `ai_` name is kept only on its definition's
// `isSystem: true`, which the name does not tell, so it is not here.
const absentSystemObject = !!closure && isSystemObject({ name: objectName });
// A platform object a known package registers: its declaration is not
// in this stack, so neither its fields nor its gates can be read here.
// Against the whole collection it is in hand, and its absence is final.
if (!whole && isPlatformProvidedObjectName(objectName)) continue;
if (closure) {
if (!whole && !absentSystemObject && isPlatformProvidedObjectName(objectName)) continue;
if (absentSystemObject) {
out.push({
severity: 'error',
rule: KNOWLEDGE_SOURCE_OBJECT_UNKNOWN,
where,
path: objectPath,
message:
`\`source.object\` names '${objectName}', a system object the host does not serve. The closure of ` +
`package "${closure.packageId}" holds every system object the host serves, so no dependency brings it ` +
'in, and the source indexes nothing.' +
suggestName(objectName, objects.keys()),
hint: 'Name a system object the host serves (check the spelling), or another object inside that closure',
});
} else if (closure) {
out.push({
severity: 'error',
rule: KNOWLEDGE_SOURCE_OBJECT_UNKNOWN,
Expand Down Expand Up @@ -257,6 +287,21 @@ export function validateKnowledgeSource(
'Ignore this if a declared dependency defines the object (the publish checks it against the package\'s ' +
'closure); otherwise name an object the stack defines (check the spelling)',
});
} else if (whole) {
// A write that names no package: its author has no stack and no
// manifest, only the organization's objects and the packages it installs.
out.push({
severity: 'error',
rule: KNOWLEDGE_SOURCE_OBJECT_UNKNOWN,
where,
path: objectPath,
message:
`\`source.object\` names '${objectName}', which is not an object of this organization (neither one it ` +
'authored nor one an installed package registers), so the source indexes nothing and no search ever ' +
'returns a hit from it.' +
suggestName(objectName, objects.keys()),
hint: `Name an object the organization has (check the spelling), or install the package that provides '${objectName}' first`,
});
} else {
out.push({
severity: 'error',
Expand Down
Loading