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
10 changes: 10 additions & 0 deletions .changeset/37432-ai-context-field-unprovisioned.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/lint': patch
---

fix(lint): a context pack is warned when it reads an injected column with no storage on an external object

Clause-②: yes (widening)
Metadata-protocol: none

A new advisory rule, `ai-context-field-unprovisioned` (a `warning`), reports a context pack (`ai_context`) that names a system column the platform injects (`owner_id`, `created_at`, `created_by`, …) on an ADR-0015 `external` object. The name resolves, so `ai-context-field-unknown` stays silent, but the remote database owns the table and the platform provisions no column behind it. The rule judges the names in the root's `fields`, `body.field`, and each `related` entry's `fields`, `where`, `orderBy` and link field. Its message names the cost for each position: an empty value on every record, no body, no rows, or an arbitrary order. A column the author declares on the external object is never reported. The provenance answer is the spec's `unprovisionedInjectedColumns`, which every other unprovisioned-anchor rule reads too. `os explain ai-context-field-unprovisioned` states the cause and the remedy, and `os explain ai-context-field-unknown` now says that injected columns count as defined.
1 change: 1 addition & 0 deletions packages/lint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ export {
validateAiContext,
AI_CONTEXT_OBJECT_UNKNOWN,
AI_CONTEXT_FIELD_UNKNOWN,
AI_CONTEXT_FIELD_UNPROVISIONED,
AI_CONTEXT_RELATED_LINK_UNKNOWN,
AI_CONTEXT_BODY_NOT_LONG_TEXT,
AI_CONTEXT_SORT_FIELD_UNSORTABLE,
Expand Down
40 changes: 36 additions & 4 deletions packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ const DASHBOARD_FILTER_FIELD_NOT_INCLUDED_EXPLANATION: RuleExplanation = {
* What an unprovisioned anchor is — the cause half of every read-side anchor
* id (`dashboard-filter-field-unprovisioned`, `react-chart-field-unprovisioned`,
* `sort-field-unprovisioned`, `searchable-field-unprovisioned`,
* `page-field-unprovisioned`, `flow-template-field-unprovisioned`).
* `page-field-unprovisioned`, `flow-template-field-unprovisioned`,
* `ai-context-field-unprovisioned`).
* The rule files share its verdict clause, `unprovisionedAnchorVerdict()`, or
* its long form, `unprovisionedAnchorCause()`.
*/
Expand All @@ -458,7 +459,8 @@ const UNPROVISIONED_ANCHOR_CAUSE =
* the converted anchor ids (`unprovisionedAnchorFix()`) names without the how:
* `sort-field-unprovisioned`, `searchable-field-unprovisioned`,
* `react-chart-field-unprovisioned`, `dashboard-filter-field-unprovisioned`,
* `page-field-unprovisioned`, `flow-template-field-unprovisioned` and the hook,
* `page-field-unprovisioned`, `flow-template-field-unprovisioned`,
* `ai-context-field-unprovisioned` and the hook,
* action and flow-node write-anchor ids write it out.
*/
const UNPROVISIONED_ANCHOR_REMEDY =
Expand Down Expand Up @@ -5992,8 +5994,37 @@ const AI_CONTEXT_FIELD_UNKNOWN_EXPLANATION: RuleExplanation = {
rule: 'ai-context-field-unknown',
covers: 'why each field a context pack lists must exist on its object',
paragraphs: [
'A pack lists the root\'s `fields`, each related list\'s `fields`, and a `body.field`. A name the object ' +
'does not define is left out of every answer: the assistant reads less about the record than the pack says.',
'A pack lists the root\'s `fields`, each related list\'s `fields`, and a `body.field`; a related list\'s ' +
'`where` and `orderBy` name fields too. A name the object does not define is left out of every answer: ' +
'the assistant reads less about the record than the pack says. In a `where` or `orderBy` the whole list ' +
'is left out, since a filter or an order is never read with one of its fields removed.',
'Defined means declared in the object\'s `fields` or injected by the platform: the system columns it ' +
'adds without the author declaring them (`owner_id`, `created_at`, `created_by` and the rest) are ' +
'fields of the published definition `get_record_context` reads, so a pack naming one is not reported. ' +
'A column the platform does not inject on that object is still unknown: for example `owner_id` ' +
'under `ownership: \'org\'`, or any of them under `systemFields: false`. On an ' +
'`external` object an injected column resolves but has no storage; that is ' +
'`ai-context-field-unprovisioned`.',
AI_CONTEXT_PACK_SILENCE,
],
};

const AI_CONTEXT_FIELD_UNPROVISIONED_EXPLANATION: RuleExplanation = {
rule: 'ai-context-field-unprovisioned',
covers: 'why a context pack reads nothing of an external anchor',
paragraphs: [
UNPROVISIONED_ANCHOR_CAUSE,
'A context pack naming such an anchor reads a column that holds nothing, at the cost the message ' +
'names for its position: in the root\'s or a related list\'s `fields` the answer carries it empty on ' +
'every record; as `body.field` the answer carries no body; in a related list\'s `where`, or as the ' +
'reference field that links the list to its root, the condition can never match a real value (on ' +
'SQLite it degrades to constant-false) and the list reads no rows; in its `orderBy` the order finds ' +
'no column, so the list is read in an arbitrary order and its row cap keeps an arbitrary slice.',
'Only a name that resolved BECAUSE it is injected is judged: an author-declared column of the same ' +
'name is one the author vouches for. A name the object does not define at all is ' +
'`ai-context-field-unknown`. A warning, because the rule cannot see the remote table, only that the ' +
'platform provisions no storage.',
UNPROVISIONED_ANCHOR_REMEDY,
AI_CONTEXT_PACK_SILENCE,
],
};
Expand Down Expand Up @@ -7095,6 +7126,7 @@ export const RULE_EXPLANATIONS: Readonly<Record<string, RuleExplanation>> = Obje
[PERMISSION_RETIRED_LIFECYCLE_RESIDUE_EXPLANATION.rule]: PERMISSION_RETIRED_LIFECYCLE_RESIDUE_EXPLANATION,
[AI_CONTEXT_OBJECT_UNKNOWN_EXPLANATION.rule]: AI_CONTEXT_OBJECT_UNKNOWN_EXPLANATION,
[AI_CONTEXT_FIELD_UNKNOWN_EXPLANATION.rule]: AI_CONTEXT_FIELD_UNKNOWN_EXPLANATION,
[AI_CONTEXT_FIELD_UNPROVISIONED_EXPLANATION.rule]: AI_CONTEXT_FIELD_UNPROVISIONED_EXPLANATION,
[AI_CONTEXT_RELATED_LINK_UNKNOWN_EXPLANATION.rule]: AI_CONTEXT_RELATED_LINK_UNKNOWN_EXPLANATION,
[AI_CONTEXT_BODY_NOT_LONG_TEXT_EXPLANATION.rule]: AI_CONTEXT_BODY_NOT_LONG_TEXT_EXPLANATION,
[AI_CONTEXT_SORT_FIELD_UNSORTABLE_EXPLANATION.rule]: AI_CONTEXT_SORT_FIELD_UNSORTABLE_EXPLANATION,
Expand Down
102 changes: 100 additions & 2 deletions packages/lint/src/validate-ai-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, expect, it } from 'vitest';
import {
AI_CONTEXT_BODY_NOT_LONG_TEXT,
AI_CONTEXT_FIELD_UNKNOWN,
AI_CONTEXT_FIELD_UNPROVISIONED,
AI_CONTEXT_OBJECT_UNKNOWN,
AI_CONTEXT_RELATED_LINK_UNKNOWN,
AI_CONTEXT_SORT_FIELD_UNSORTABLE,
Expand Down Expand Up @@ -166,7 +167,7 @@ describe('validateAiContext — ADR-0160 D1 author defects', () => {
// and `get_record_context` answers both in the root's `fields`, a related
// list's `fields`, its `where` and its `orderBy`. On an `ownership: 'org'`
// object the platform injects no `owner_id` and the tool leaves it out.
// `service-record-context`'s injected-system-columns test pins the
// `service-record-context`'s shared harness (`record-context-tools.test.ts`) pins the
// executor's half over a door serving the same registry-injected shape.
it('an injected system column the object does not declare is no unknown field (owner_id, created_at)', () => {
const stack = {
Expand Down Expand Up @@ -230,10 +231,107 @@ describe('validateAiContext — ADR-0160 D1 author defects', () => {
it('every verdict is a warning, and every rule it emits has an `os explain` entry', () => {
const findings = validateAiContext({ objects, aiContexts: [pack({ fields: ['nope'], body: { field: 'title' } })] });
expect(findings.every((f) => f.severity === 'warning')).toBe(true);
for (const id of [AI_CONTEXT_OBJECT_UNKNOWN, AI_CONTEXT_FIELD_UNKNOWN, AI_CONTEXT_RELATED_LINK_UNKNOWN, AI_CONTEXT_BODY_NOT_LONG_TEXT, AI_CONTEXT_SORT_FIELD_UNSORTABLE]) {
for (const id of [AI_CONTEXT_OBJECT_UNKNOWN, AI_CONTEXT_FIELD_UNKNOWN, AI_CONTEXT_FIELD_UNPROVISIONED, AI_CONTEXT_RELATED_LINK_UNKNOWN, AI_CONTEXT_BODY_NOT_LONG_TEXT, AI_CONTEXT_SORT_FIELD_UNSORTABLE]) {
expect(explainRule(id), id).toBeTruthy();
}
});

it('the `ai-context-field-unknown` explanation counts injected columns as defined, and names the external case', () => {
const text = explainRule(AI_CONTEXT_FIELD_UNKNOWN)?.paragraphs.join(' ') ?? '';
expect(text).toContain('injected');
expect(text).toContain('owner_id');
expect(text).toContain(AI_CONTEXT_FIELD_UNPROVISIONED);
});
});

/**
* [ADR-0015] An `external` object's injected columns resolve — the published
* definition carries them, so `ai-context-field-unknown` stays silent — but
* the platform provisions no column behind them. The provenance answer is the
* spec's (`unprovisionedInjectedColumnsFor`), the one every unprovisioned-anchor
* rule here reads; an author-declared column of the same name is the author's.
*/
describe('validateAiContext — an injected column with no storage on an external object (ADR-0015)', () => {
const external = (names: string[]) =>
objects.map((o) => (names.includes(o.name) ? { ...o, external: { remoteName: `${o.name}s` } } : o));
const injectedPack = pack({
fields: ['title', 'owner_id', 'created_at'],
related: [{
object: 'policy_revision',
field: 'policy',
fields: ['version', 'created_by'],
where: { owner_id: 'usr_1' },
orderBy: [{ field: 'created_at', order: 'desc' }],
}],
});

it('warns at every name the pack reads off an injected column, on the root and a related list', () => {
const findings = validateAiContext({ objects: external(['policy', 'policy_revision']), aiContexts: [injectedPack] });
expect(findings.map((f) => [f.rule, f.path])).toEqual([
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].fields[1]'],
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].fields[2]'],
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].related[0].fields[1]'],
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].related[0].where'],
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].related[0].orderBy[0].field'],
]);
expect(findings.every((f) => f.severity === 'warning')).toBe(true);
// Each finding names its object and column, the shared anchor fix beside it.
const [rootOwner, , childCreator] = findings;
expect(rootOwner?.message).toContain("'owner_id'");
expect(rootOwner?.message).toContain("external object 'policy'");
expect(childCreator?.message).toContain("external object 'policy_revision'");
expect(rootOwner?.hint).toContain('columnMap');
});

it('the same pack on objects that are not external is clean', () => {
expect(validateAiContext({ objects, aiContexts: [injectedPack] })).toEqual([]);
});

it('judges each object by its own provenance: an external child under a local root', () => {
expect(rulesOf({ objects: external(['policy_revision']), aiContexts: [injectedPack] })).toEqual([
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].related[0].fields[1]'],
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].related[0].where'],
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].related[0].orderBy[0].field'],
]);
});

it('a column the author declares on the external object is theirs, and never judged', () => {
const declared = external(['policy']).map((o) =>
o.name === 'policy' ? { ...o, fields: { ...o.fields, owner_id: { type: 'text', label: 'Remote owner' } } } : o);
expect(rulesOf({ objects: declared, aiContexts: [pack({ fields: ['title', 'owner_id'] })] })).toEqual([]);
});

it('a body on an injected column carries no body twice over: not long text, and no storage', () => {
expect(rulesOf({ objects: external(['policy']), aiContexts: [pack({ body: { field: 'created_at' } })] })).toEqual([
[AI_CONTEXT_BODY_NOT_LONG_TEXT, 'aiContexts[0].body.field'],
[AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].body.field'],
]);
});

it('a related list linked through an injected reference of an external child reads no rows', () => {
const withUser = [
...objects,
{ name: 'sys_user', fields: { name: { type: 'text' } } },
{ name: 'task', fields: { subject: { type: 'text' } } },
];
const userPack = { name: 'user_brief', object: 'sys_user', fields: ['name'], related: [{ object: 'task', field: 'owner_id' }] };
const local = validateAiContext({ objects: withUser, aiContexts: [userPack] });
expect(local).toEqual([]);
const externalTask = withUser.map((o) => (o.name === 'task' ? { ...o, external: { remoteName: 'tasks' } } : o));
const [finding, ...rest] = validateAiContext({ objects: externalTask, aiContexts: [userPack] });
expect(rest).toEqual([]);
expect([finding?.rule, finding?.path]).toEqual([AI_CONTEXT_FIELD_UNPROVISIONED, 'aiContexts[0].related[0].field']);
expect(finding?.message).toContain("external object 'task'");
});

it('has an `os explain` entry that states the cause, the cost per position and the remedy', () => {
const entry = explainRule(AI_CONTEXT_FIELD_UNPROVISIONED);
expect(entry?.rule).toBe(AI_CONTEXT_FIELD_UNPROVISIONED);
const text = entry?.paragraphs.join(' ') ?? '';
for (const fact of ['ADR-0015', 'external', '`fields`', '`body.field`', '`where`', '`orderBy`', 'columnMap', AI_CONTEXT_FIELD_UNKNOWN]) {
expect(text, fact).toContain(fact);
}
});
});

/**
Expand Down
Loading
Loading