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
13 changes: 13 additions & 0 deletions .changeset/35251-state-machine-update-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
---

fix(lint): `state-machine-no-initial-states` judges only state machines that run on insert

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

The publish-time rule `state-machine-no-initial-states` now refuses an active `state_machine` validation with no (or an empty) `initialStates` only when the machine runs on insert: its `events` include `insert`, or are left to the default `['insert', 'update']`. The engine runs a rule only on the write contexts its `events` name, so a machine declared `events: ['update']` (or `events: []`) never runs on create and never reads `initialStates`. Such a machine is accepted at publish and by `os validate` again, with no `initialStates`; adding one to satisfy the rule produced metadata the runtime ignores. A machine that runs on insert without `initialStates` is still refused, with the same fix.

`@objectstack/spec` changes only the description of a state machine validation's `initialStates`, which now says the rule judges a machine that runs on insert.
18 changes: 18 additions & 0 deletions .changeset/35252-field-group-shadowed-six-highlights.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
'@object-ui/plugin-detail': minor
---

fix(lint): `field-group-shadowed` judges the six highlights the record page draws, from one shared constant

Clause-②: yes (narrowing)
Metadata-protocol: narrowed

The default record page draws the first six of an object's `highlightFields` (after the title field leaves the list) in its header strip and leaves those fields out of the Details body. The `field-group-shadowed` warning judged only the first four, so a field group whose only member sat fifth or sixth in `highlightFields` was empty on the record page and passed `os lint --strict`. It now judges the same six, and its hint says "first 6 highlightFields".

What was accepted before and is warned now: an object whose field group is emptied by its fifth or sixth highlight. Fix it as the hint says: keep a member of that group out of the first six highlights, or drop the group. A list the old count warned is still warned: six hoisted fields include the first four, so no list that passed only under six is refused.

New export: `@objectstack/spec/ui` adds `RECORD_HIGHLIGHTS_MAX` (6), the count both the lint and `@object-ui/plugin-detail`'s record page synthesizer read. `@object-ui/plugin-detail` now takes its own `RECORD_HIGHLIGHTS_MAX` from there; the value and the page are unchanged.

<!-- adr-0087: registered field-group-shadowed-six-highlights -->
8 changes: 4 additions & 4 deletions content/docs/references/data/validation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const result = ConditionalValidationSchema.parse(data);
| **type** | `'state_machine'` | ✅ | |
| **field** | `string` | ✅ | State field (e.g. status) |
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate`. |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate` when its `events` include `insert` (the default). An update-only machine never runs on create, so it needs no `initialStates`. |

### Nested Shape: `ConditionalValidation.then[type='format']`

Expand Down Expand Up @@ -313,7 +313,7 @@ const result = ConditionalValidationSchema.parse(data);
| **type** | `'state_machine'` | ✅ | |
| **field** | `string` | ✅ | State field (e.g. status) |
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate`. |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate` when its `events` include `insert` (the default). An update-only machine never runs on create, so it needs no `initialStates`. |

### Nested Shape: `ConditionalValidation.otherwise[type='format']`

Expand Down Expand Up @@ -532,7 +532,7 @@ const result = ConditionalValidationSchema.parse(data);
| **type** | `'state_machine'` | ✅ | |
| **field** | `string` | ✅ | State field (e.g. status) |
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate`. |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate` when its `events` include `insert` (the default). An update-only machine never runs on create, so it needs no `initialStates`. |


---
Expand Down Expand Up @@ -599,7 +599,7 @@ This schema accepts one of the following structures:
| **type** | `'state_machine'` | ✅ | |
| **field** | `string` | ✅ | State field (e.g. status) |
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate`. |
| **initialStates** | `string[]` | optional | States a record may be CREATED in. When set, an INSERT whose state field carries a value outside this list is rejected (server-enforced) — the FSM entry point. `transitions` only governs UPDATE, and a `select` field permits ANY declared option as an initial value, so without this a record could be born mid-flow (e.g. created already `approved`). Omitted or empty, insert is not checked; the author-time rule `state-machine-no-initial-states` refuses an active machine declared that way at publish and in `os validate` when its `events` include `insert` (the default). An update-only machine never runs on create, so it needs no `initialStates`. |

---

Expand Down
4 changes: 0 additions & 4 deletions examples/app-showcase/src/data/objects/account.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ export const Account = ObjectSchema.create({
field: 'status',
// Transitions are validated on update; insert sets the initial state.
events: ['update'] as const,
// The entry state: `prospect`, the field's default and the machine's
// root. This machine runs on updates only, so the server does not check
// it on create; it states the entry the lifecycle assumes.
initialStates: ['prospect'],
message: 'Invalid account lifecycle transition.',
transitions: {
prospect: ['active', 'churned'],
Expand Down
3 changes: 0 additions & 3 deletions examples/app-showcase/src/data/objects/project.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ export const Project = ObjectSchema.create({
description: 'Health should change one step at a time; skips are warned, not blocked.',
field: 'health',
events: ['update'] as const,
// A project starts `green` (the field's default). Updates only, so this
// states the entry rather than checking it on create.
initialStates: ['green'],
severity: 'warning' as const,
message: 'Health changed by more than one step — confirm this is intentional.',
transitions: {
Expand Down
4 changes: 0 additions & 4 deletions examples/app-showcase/src/data/objects/task.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ export const Task = ObjectSchema.create({
// — an authored message is emitted verbatim unless the bundle carries
// that key, and the bundle wins once it does (#14518).
message: 'Invalid task status transition.',
// A task is created on the board's first two columns: `backlog` (the
// default) or `todo`. Updates only, so this states the entry rather
// than checking it on create.
initialStates: ['backlog', 'todo'],
transitions: {
backlog: ['todo'],
todo: ['in_progress', 'backlog'],
Expand Down
3 changes: 2 additions & 1 deletion packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6338,7 +6338,8 @@ const STATE_MACHINE_NO_INITIAL_STATES_EXPLANATION: RuleExplanation = {
'empty `initialStates` list is the same as none: the engine reads it as no entry contract.',
'`initialStates` lists the states a record may be created in, and the server refuses an insert in any ' +
'other. Seed rows are exempt from the machine on purpose, so curated sample data can start mid-process. ' +
'A machine with `active: false` is not run, so it is not reported.',
'A machine with `active: false` is not run, so it is not reported; nor is one whose `events` leave out ' +
'`insert`, because the engine never runs it on create and would never read its `initialStates`.',
],
};

Expand Down
26 changes: 26 additions & 0 deletions packages/lint/src/validate-inert-object-declarations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ describe('state-machine-no-initial-states', () => {
expect(validateInertObjectDeclarations({ objects: [task({ status: STATUS }, [machine({ active: false })])] })).toEqual([]);
});

it('is silent on an update-only machine with no `initialStates`: the engine never runs it on create', () => {
expect(validateInertObjectDeclarations({ objects: [task({ status: STATUS }, [machine({ events: ['update'] })])] })).toEqual([]);
expect(validateInertObjectDeclarations({ objects: [task({ status: STATUS }, [machine({ events: [] })])] })).toEqual([]);
});

it('still refuses a machine whose `events` name `insert`, or leave it to the default', () => {
const objects = [
task({ status: STATUS }, [
machine({ name: 'explicit_insert', events: ['insert'] }),
machine({ name: 'both', events: ['update', 'insert'] }),
machine({ name: 'defaulted' }),
]),
];
expect(validateInertObjectDeclarations({ objects }).map((f) => [f.rule, f.path])).toEqual([
[STATE_MACHINE_NO_INITIAL_STATES, 'objects[0].validations[0]'],
[STATE_MACHINE_NO_INITIAL_STATES, 'objects[0].validations[1]'],
[STATE_MACHINE_NO_INITIAL_STATES, 'objects[0].validations[2]'],
]);
});

it('is silent when the machine declares its entry states, and on other validation types', () => {
expect(validateInertObjectDeclarations({
objects: [task({ status: STATUS }, [machine({ initialStates: ['draft'] }), { name: 'r', type: 'script', message: 'm', condition: 'false' }])],
Expand All @@ -59,6 +79,12 @@ describe('the runtime publish gate refuses an object write carrying the declarat
expect(verdict.errors.map((f) => f.rule)).toContain(STATE_MACHINE_NO_INITIAL_STATES);
});

it('passes an `object` write whose only machine is update-only', () => {
const item = task({ status: STATUS }, [machine({ events: ['update'] })]);
const verdict = runRuntimeAuthoringRules({ type: 'object', item });
expect(verdict.errors.map((f) => f.rule)).not.toContain(STATE_MACHINE_NO_INITIAL_STATES);
});

it('a required boolean is not refused: the form treats false as present', () => {
const item = task({ is_done: { type: 'boolean', label: 'Done', required: true, defaultValue: true } });
const verdict = runRuntimeAuthoringRules({ type: 'object', item });
Expand Down
17 changes: 16 additions & 1 deletion packages/lint/src/validate-inert-object-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
* "no entry contract" (`checkStateMachine`), so a record can be CREATED
* already in any state (born `approved`), walking straight past the process
* the machine declares. A rule with `active: false` is skipped, as the engine
* skips it.
* skips it, and so is a machine whose `events` leave out `insert`: the
* engine runs a rule only on the write contexts its `events` list
* (`r.events ?? ['insert', 'update']`, `rule-validator.ts`), so on an
* update-only machine `initialStates` is never read and adding it would be
* metadata the runtime ignores.
*
* It reads the object's own body alone, so the runtime publish gate judges an
* `object` write exactly as the CLI judges the stack.
Expand All @@ -30,6 +34,16 @@ export interface InertObjectDeclarationFinding {
hint: string;
}

/**
* Whether the engine runs a rule on insert. An absent `events` takes the
* schema default `['insert', 'update']`; an explicit list runs on exactly the
* contexts it names, so `[]` and `['update']` never run on create.
*/
function runsOnInsert(events: unknown): boolean {
if (events === undefined || events === null) return true;
return Array.isArray(events) && events.includes('insert');
}

/** Flag an entry-unguarded state machine on every object. */
export function validateInertObjectDeclarations(stack: Record<string, unknown>): InertObjectDeclarationFinding[] {
const findings: InertObjectDeclarationFinding[] = [];
Expand All @@ -41,6 +55,7 @@ export function validateInertObjectDeclarations(stack: Record<string, unknown>):
const validationList: readonly unknown[] = Array.isArray(obj.validations) ? obj.validations : [];
for (const rule of validations) {
if (rule.type !== 'state_machine' || rule.active === false) continue;
if (!runsOnInsert(rule.events)) continue;
// Absent and empty are the same to the engine: no entry contract.
if (Array.isArray(rule.initialStates) && rule.initialStates.length > 0) continue;
const ruleName = typeof rule.name === 'string' ? rule.name : 'state_machine';
Expand Down
Loading
Loading