Skip to content

Commit 50d4ae8

Browse files
committed
fix(lint): state-machine-no-initial-states judges only machines that run on insert
An update-only state machine (events without insert) never runs on create, so the engine never reads its initialStates; the rule no longer refuses it. The three showcase machines that carried a declaration-only initialStates list to pass the rule drop it. Claude-Session: https://claude.ai/code/session_01L6bhgUZrkMKgGrXXe8zPGq Co-authored-by: Claude <noreply@anthropic.com>
1 parent e35cf03 commit 50d4ae8

10 files changed

Lines changed: 64 additions & 20 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@objectstack/lint': minor
3+
'@objectstack/spec': minor
4+
---
5+
6+
fix(lint): `state-machine-no-initial-states` judges only state machines that run on insert
7+
8+
Clause-②: yes (widening)
9+
Metadata-protocol: added
10+
11+
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.
12+
13+
`@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.

‎content/docs/references/data/validation.mdx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const result = ConditionalValidationSchema.parse(data);
192192
| **type** | `'state_machine'` | ✅ | |
193193
| **field** | `string` | ✅ | State field (e.g. status) |
194194
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
195-
| **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`. |
195+
| **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`. |
196196

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

@@ -313,7 +313,7 @@ const result = ConditionalValidationSchema.parse(data);
313313
| **type** | `'state_machine'` | ✅ | |
314314
| **field** | `string` | ✅ | State field (e.g. status) |
315315
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
316-
| **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`. |
316+
| **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`. |
317317

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

@@ -532,7 +532,7 @@ const result = ConditionalValidationSchema.parse(data);
532532
| **type** | `'state_machine'` | ✅ | |
533533
| **field** | `string` | ✅ | State field (e.g. status) |
534534
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
535-
| **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`. |
535+
| **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`. |
536536

537537

538538
---
@@ -599,7 +599,7 @@ This schema accepts one of the following structures:
599599
| **type** | `'state_machine'` | ✅ | |
600600
| **field** | `string` | ✅ | State field (e.g. status) |
601601
| **transitions** | `Record<string, string[]>` | ✅ | Map of `{ OldState: [AllowedNewStates] }` |
602-
| **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`. |
602+
| **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`. |
603603

604604
---
605605

‎examples/app-showcase/src/data/objects/account.object.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,6 @@ export const Account = ObjectSchema.create({
141141
field: 'status',
142142
// Transitions are validated on update; insert sets the initial state.
143143
events: ['update'] as const,
144-
// The entry state: `prospect`, the field's default and the machine's
145-
// root. This machine runs on updates only, so the server does not check
146-
// it on create; it states the entry the lifecycle assumes.
147-
initialStates: ['prospect'],
148144
message: 'Invalid account lifecycle transition.',
149145
transitions: {
150146
prospect: ['active', 'churned'],

‎examples/app-showcase/src/data/objects/project.object.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ export const Project = ObjectSchema.create({
191191
description: 'Health should change one step at a time; skips are warned, not blocked.',
192192
field: 'health',
193193
events: ['update'] as const,
194-
// A project starts `green` (the field's default). Updates only, so this
195-
// states the entry rather than checking it on create.
196-
initialStates: ['green'],
197194
severity: 'warning' as const,
198195
message: 'Health changed by more than one step — confirm this is intentional.',
199196
transitions: {

‎examples/app-showcase/src/data/objects/task.object.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ export const Task = ObjectSchema.create({
115115
// — an authored message is emitted verbatim unless the bundle carries
116116
// that key, and the bundle wins once it does (#14518).
117117
message: 'Invalid task status transition.',
118-
// A task is created on the board's first two columns: `backlog` (the
119-
// default) or `todo`. Updates only, so this states the entry rather
120-
// than checking it on create.
121-
initialStates: ['backlog', 'todo'],
122118
transitions: {
123119
backlog: ['todo'],
124120
todo: ['in_progress', 'backlog'],

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6338,7 +6338,8 @@ const STATE_MACHINE_NO_INITIAL_STATES_EXPLANATION: RuleExplanation = {
63386338
'empty `initialStates` list is the same as none: the engine reads it as no entry contract.',
63396339
'`initialStates` lists the states a record may be created in, and the server refuses an insert in any ' +
63406340
'other. Seed rows are exempt from the machine on purpose, so curated sample data can start mid-process. ' +
6341-
'A machine with `active: false` is not run, so it is not reported.',
6341+
'A machine with `active: false` is not run, so it is not reported; nor is one whose `events` leave out ' +
6342+
'`insert`, because the engine never runs it on create and would never read its `initialStates`.',
63426343
],
63436344
};
63446345

‎packages/lint/src/validate-inert-object-declarations.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ describe('state-machine-no-initial-states', () => {
4545
expect(validateInertObjectDeclarations({ objects: [task({ status: STATUS }, [machine({ active: false })])] })).toEqual([]);
4646
});
4747

48+
it('is silent on an update-only machine with no `initialStates`: the engine never runs it on create', () => {
49+
expect(validateInertObjectDeclarations({ objects: [task({ status: STATUS }, [machine({ events: ['update'] })])] })).toEqual([]);
50+
expect(validateInertObjectDeclarations({ objects: [task({ status: STATUS }, [machine({ events: [] })])] })).toEqual([]);
51+
});
52+
53+
it('still refuses a machine whose `events` name `insert`, or leave it to the default', () => {
54+
const objects = [
55+
task({ status: STATUS }, [
56+
machine({ name: 'explicit_insert', events: ['insert'] }),
57+
machine({ name: 'both', events: ['update', 'insert'] }),
58+
machine({ name: 'defaulted' }),
59+
]),
60+
];
61+
expect(validateInertObjectDeclarations({ objects }).map((f) => [f.rule, f.path])).toEqual([
62+
[STATE_MACHINE_NO_INITIAL_STATES, 'objects[0].validations[0]'],
63+
[STATE_MACHINE_NO_INITIAL_STATES, 'objects[0].validations[1]'],
64+
[STATE_MACHINE_NO_INITIAL_STATES, 'objects[0].validations[2]'],
65+
]);
66+
});
67+
4868
it('is silent when the machine declares its entry states, and on other validation types', () => {
4969
expect(validateInertObjectDeclarations({
5070
objects: [task({ status: STATUS }, [machine({ initialStates: ['draft'] }), { name: 'r', type: 'script', message: 'm', condition: 'false' }])],
@@ -59,6 +79,12 @@ describe('the runtime publish gate refuses an object write carrying the declarat
5979
expect(verdict.errors.map((f) => f.rule)).toContain(STATE_MACHINE_NO_INITIAL_STATES);
6080
});
6181

82+
it('passes an `object` write whose only machine is update-only', () => {
83+
const item = task({ status: STATUS }, [machine({ events: ['update'] })]);
84+
const verdict = runRuntimeAuthoringRules({ type: 'object', item });
85+
expect(verdict.errors.map((f) => f.rule)).not.toContain(STATE_MACHINE_NO_INITIAL_STATES);
86+
});
87+
6288
it('a required boolean is not refused: the form treats false as present', () => {
6389
const item = task({ is_done: { type: 'boolean', label: 'Done', required: true, defaultValue: true } });
6490
const verdict = runRuntimeAuthoringRules({ type: 'object', item });

‎packages/lint/src/validate-inert-object-declarations.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
* "no entry contract" (`checkStateMachine`), so a record can be CREATED
1111
* already in any state (born `approved`), walking straight past the process
1212
* the machine declares. A rule with `active: false` is skipped, as the engine
13-
* skips it.
13+
* skips it, and so is a machine whose `events` leave out `insert`: the
14+
* engine runs a rule only on the write contexts its `events` list
15+
* (`r.events ?? ['insert', 'update']`, `rule-validator.ts`), so on an
16+
* update-only machine `initialStates` is never read and adding it would be
17+
* metadata the runtime ignores.
1418
*
1519
* It reads the object's own body alone, so the runtime publish gate judges an
1620
* `object` write exactly as the CLI judges the stack.
@@ -30,6 +34,16 @@ export interface InertObjectDeclarationFinding {
3034
hint: string;
3135
}
3236

37+
/**
38+
* Whether the engine runs a rule on insert. An absent `events` takes the
39+
* schema default `['insert', 'update']`; an explicit list runs on exactly the
40+
* contexts it names, so `[]` and `['update']` never run on create.
41+
*/
42+
function runsOnInsert(events: unknown): boolean {
43+
if (events === undefined || events === null) return true;
44+
return Array.isArray(events) && events.includes('insert');
45+
}
46+
3347
/** Flag an entry-unguarded state machine on every object. */
3448
export function validateInertObjectDeclarations(stack: Record<string, unknown>): InertObjectDeclarationFinding[] {
3549
const findings: InertObjectDeclarationFinding[] = [];
@@ -41,6 +55,7 @@ export function validateInertObjectDeclarations(stack: Record<string, unknown>):
4155
const validationList: readonly unknown[] = Array.isArray(obj.validations) ? obj.validations : [];
4256
for (const rule of validations) {
4357
if (rule.type !== 'state_machine' || rule.active === false) continue;
58+
if (!runsOnInsert(rule.events)) continue;
4459
// Absent and empty are the same to the engine: no entry contract.
4560
if (Array.isArray(rule.initialStates) && rule.initialStates.length > 0) continue;
4661
const ruleName = typeof rule.name === 'string' ? rule.name : 'state_machine';

‎packages/spec/src/data/validation.zod.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export const StateMachineValidationSchema = lazySchema(() => strictObject({
194194
type: z.literal('state_machine'),
195195
field: z.string().describe('State field (e.g. status)'),
196196
transitions: z.record(z.string(), z.array(z.string())).describe('Map of { OldState: [AllowedNewStates] }'),
197-
initialStates: z.array(z.string()).optional().describe('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`.'),
197+
initialStates: z.array(z.string()).optional().describe('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`.'),
198198
}));
199199

200200
/**

‎packages/spec/src/migrations/entries/semantic/18.publish-refuses-metadata-the-runtime-ignores.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export const entry: SemanticMigration = {
1414
+ 'a flow node whose `type` no executor in the deployment runs (`flow-node-type-no-executor`, judged at publish '
1515
+ 'against the automation engine\'s sealed node-type vocabulary); an `approval` node whose decision out-edges are '
1616
+ 'not labelled `approve` and `reject` (`flow-approval-decision-unrouted`); an `http` node with no `timeoutMs` '
17-
+ '(`flow-http-timeout-missing`, a warning); an active `state_machine` validation with no or an empty '
18-
+ '`initialStates` (`state-machine-no-initial-states`); a standalone action that declares `locations` but no `objectName` '
17+
+ '(`flow-http-timeout-missing`, a warning); an active `state_machine` validation that runs on insert (its `events` '
18+
+ 'include `insert`, the default) with no or an empty `initialStates` (`state-machine-no-initial-states`); a standalone action that declares `locations` but no `objectName` '
1919
+ '(`action-no-object-binding`)',
2020
replacement:
2121
'Give the node a type the deployment runs (or install the plugin that contributes it); label the approval node\'s '

0 commit comments

Comments
 (0)