Skip to content

Commit 573b29b

Browse files
committed
Merge lane A follow-up (error code, auth start order) into the PR branch
Claude-Session: https://claude.ai/code/session_01ADHkmPVdwjc3wMNdnuWeFT Co-authored-by: Claude <noreply@anthropic.com>
2 parents 65155e4 + fff47a9 commit 573b29b

11 files changed

Lines changed: 164 additions & 12 deletions

‎packages/plugins/plugin-auth/src/auth-plugin.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,18 @@ export class AuthPlugin implements Plugin {
698698
* Organization, and on a kernel that composes the guard (`os serve`) a
699699
* database the guard refuses must be left as the guard found it, so the
700700
* organization is created only once the guard has judged.
701+
*
702+
* `com.objectstack.form-type-switch-boot-guard` — the protocol-19 boot guard
703+
* (`@objectstack/runtime`'s `FORM_TYPE_SWITCH_BOOT_GUARD_PLUGIN_NAME`, spelled
704+
* here for the same reason; ADR-0159 D7), order-if-present and for the same
705+
* property: a database that guard refuses is left as it found it, so nothing
706+
* `start()` writes is written before its verdict.
701707
*/
702-
optionalDependencies = ['com.objectstack.service.settings', 'com.objectstack.organization-ownership-boot-guard'];
708+
optionalDependencies = [
709+
'com.objectstack.service.settings',
710+
'com.objectstack.organization-ownership-boot-guard',
711+
'com.objectstack.form-type-switch-boot-guard',
712+
];
703713

704714

705715
private options: AuthPluginOptions;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* Nothing the auth family writes at start is written before the protocol-19
5+
* boot guard has judged the database (ADR-0159 D7) — declared, not incidental.
6+
*
7+
* `AuthPlugin.start()` and `IdentityObjectsPlugin.start()` write identity rows
8+
* (the Default Organization under `single`, ADR-0131 D3). The guard
9+
* (`@objectstack/runtime`'s `FormTypeSwitchBootGuardPlugin`) judges in its own
10+
* `start()`, and a database it refuses must be left as it found it, so both
11+
* plugins name it in `optionalDependencies`, beside the v18 guard
12+
* (`default-organization-guard-ordering.pin.test.ts`). This file proves the
13+
* declaration is what moves the order: a hostile registry (the writer used
14+
* BEFORE the guard) still starts the guard first, and the same registry with
15+
* the declaration removed does not. The real guard on a real kernel is
16+
* `runtime/src/form-type-switch-boot-guard.kernel.test.ts`.
17+
*
18+
* The guard is a NAME-ONLY stub carrying the real guard's own order-if-present
19+
* edges: this package does not depend on the runtime, and
20+
* `resolvePluginOrder` reads only the `OrderablePlugin` surface.
21+
*/
22+
23+
import { describe, expect, it } from 'vitest';
24+
import { resolvePluginOrder } from '@objectstack/core';
25+
import type { OrderablePlugin } from '@objectstack/core';
26+
import { AuthPlugin } from './auth-plugin.js';
27+
import { IdentityObjectsPlugin } from './identity-objects-plugin.js';
28+
29+
// Pay the first transform of the dist-resolved dependency `auth-plugin`'s
30+
// reach loads through a dynamic `import()` at MODULE LOAD, never inside a
31+
// case's clocked window (`scripts/check-test-source-alias.mjs`).
32+
import '@objectstack/core/egress';
33+
34+
const GUARD_PLUGIN = 'com.objectstack.form-type-switch-boot-guard';
35+
const OWNERSHIP_GUARD_PLUGIN = 'com.objectstack.organization-ownership-boot-guard';
36+
const ENGINE_PLUGIN = 'com.objectstack.engine.objectql';
37+
38+
const writers: Array<[string, () => OrderablePlugin]> = [
39+
['AuthPlugin', () => new AuthPlugin({ secret: 'test-secret-at-least-32-chars-long!!' }) as unknown as OrderablePlugin],
40+
['IdentityObjectsPlugin', () => new IdentityObjectsPlugin() as unknown as OrderablePlugin],
41+
];
42+
43+
/** The guard as the resolver sees it: its name and its own order-if-present edges. */
44+
const guardStub = (): OrderablePlugin => ({
45+
name: GUARD_PLUGIN,
46+
optionalDependencies: [ENGINE_PLUGIN, 'com.objectstack.platform-objects', OWNERSHIP_GUARD_PLUGIN],
47+
});
48+
49+
const orderOf = (...plugins: OrderablePlugin[]): string[] =>
50+
resolvePluginOrder(new Map(plugins.map((p) => [p.name, p]))).map((p) => p.name);
51+
52+
describe.each(writers)('%s writes its identity rows only after the protocol-19 boot guard has judged', (_label, make) => {
53+
it('declares the guard as an OPTIONAL dependency', () => {
54+
const writer = make();
55+
expect(writer.optionalDependencies ?? []).toContain(GUARD_PLUGIN);
56+
expect(writer.dependencies ?? []).not.toContain(GUARD_PLUGIN);
57+
});
58+
59+
it('⭐ the declaration moves the order: used BEFORE the guard, it still starts after it', () => {
60+
const writer = make();
61+
const ordered = orderOf({ name: ENGINE_PLUGIN }, writer, guardStub());
62+
expect(ordered.indexOf(GUARD_PLUGIN)).toBeLessThan(ordered.indexOf(writer.name));
63+
});
64+
65+
it('…and the declaration is what does it: forget it and the writer starts first', () => {
66+
const writer = make();
67+
writer.optionalDependencies = (writer.optionalDependencies ?? []).filter((d) => d !== GUARD_PLUGIN);
68+
const ordered = orderOf({ name: ENGINE_PLUGIN }, writer, guardStub());
69+
expect(ordered.indexOf(writer.name)).toBeLessThan(ordered.indexOf(GUARD_PLUGIN));
70+
});
71+
72+
it('beside the v18 guard: used before both, it starts after both, the older guard judging first', () => {
73+
const writer = make();
74+
const ordered = orderOf(
75+
{ name: ENGINE_PLUGIN },
76+
writer,
77+
guardStub(),
78+
{ name: OWNERSHIP_GUARD_PLUGIN, optionalDependencies: [ENGINE_PLUGIN] },
79+
);
80+
expect(ordered.indexOf(OWNERSHIP_GUARD_PLUGIN)).toBeLessThan(ordered.indexOf(GUARD_PLUGIN));
81+
expect(ordered.indexOf(GUARD_PLUGIN)).toBeLessThan(ordered.indexOf(writer.name));
82+
});
83+
84+
it('order-if-present: a kernel that composes no guard resolves as before', () => {
85+
const writer = make();
86+
expect(orderOf({ name: ENGINE_PLUGIN }, writer)).toContain(writer.name);
87+
});
88+
});

‎packages/plugins/plugin-auth/src/identity-objects-plugin.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,15 @@ export class IdentityObjectsPlugin implements Plugin {
9494
* `auth` service is registered by the time `init()` asks, and the pair is
9595
* refused there. And the v18 boot guard's (ADR-0131 D10 item 5; see
9696
* `AuthPlugin.optionalDependencies`): `start()` creates the Default
97-
* Organization only once the guard has judged the database.
97+
* Organization only once the guard has judged the database. And the
98+
* protocol-19 boot guard's (ADR-0159 D7), for the same property: nothing
99+
* `start()` writes is written before that guard's verdict.
98100
*/
99-
optionalDependencies: string[] = ['com.objectstack.auth', 'com.objectstack.organization-ownership-boot-guard'];
101+
optionalDependencies: string[] = [
102+
'com.objectstack.auth',
103+
'com.objectstack.organization-ownership-boot-guard',
104+
'com.objectstack.form-type-switch-boot-guard',
105+
];
100106
requiresServices: string[] = ['manifest'];
101107

102108
private readonly options: IdentityObjectsPluginOptions;

‎packages/runtime/src/form-type-switch-boot-guard.kernel.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { DriverPlugin } from './driver-plugin.js';
5050
import {
5151
assertFormTypeSwitchGuardNotReplaced,
5252
FORM_TYPE_SWITCH_BOOT_GUARD_PLUGIN_NAME,
53+
FORM_TYPE_SWITCH_BOOT_REFUSAL_CODE,
5354
FORM_TYPE_SWITCH_BOOT_REFUSAL_NAME,
5455
FORM_TYPE_SWITCH_PROTOCOL_MAJOR,
5556
FormTypeSwitchBirthMarkerPlugin,
@@ -190,6 +191,9 @@ describe('the protocol-19 boot refusal on a real kernel (ADR-0159 D7)', () => {
190191
const { kernel, error } = await boot(file);
191192
const refusal = refusalIn(error);
192193
expect(refusal?.name, String(error)).toBe(FORM_TYPE_SWITCH_BOOT_REFUSAL_NAME);
194+
// The registered machine code, by value: what a host discriminates the refusal by.
195+
expect(refusal?.code).toBe('FORM_TYPE_SWITCH_REQUIRED');
196+
expect(FORM_TYPE_SWITCH_BOOT_REFUSAL_CODE).toBe('FORM_TYPE_SWITCH_REQUIRED');
193197
expect(refusal?.reason).toBe('absent');
194198
expect(refusal?.message).toContain(`'${COMMAND}'`);
195199
expect(refusal?.message).toContain(`'${COMMAND} --apply --yes'`);

‎packages/runtime/src/form-type-switch-boot-guard.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ import { resolveRuntimeVersion } from './runtime-version.js';
6060
* schema sync the "created from empty" observation is read off), the platform
6161
* objects (`sys_migration`) and the organization-ownership guard, so a
6262
* database that owes both acts is told the older one first. `AppPlugin`,
63-
* whose `start()` loads the inline seed, names this guard in its
64-
* `optionalDependencies` ({@link FORM_TYPE_SWITCH_BOOT_GUARD_PLUGIN_NAME}),
65-
* and `os serve` composes the guard ahead of the auth family. A `start()`
63+
* whose `start()` loads the inline seed, and `@objectstack/plugin-auth`'s
64+
* `AuthPlugin` and `IdentityObjectsPlugin`, whose `start()` writes identity
65+
* rows, name this guard in their `optionalDependencies`
66+
* ({@link FORM_TYPE_SWITCH_BOOT_GUARD_PLUGIN_NAME}). A `start()`
6667
* that throws stops the kernel before the next plugin starts; the kernel
6768
* hands the refusal on as the `cause` of its own start-failure error, so a
6869
* host that inspects the boot error asks {@link isFormTypeSwitchBootRefusal}
@@ -85,10 +86,14 @@ import { resolveRuntimeVersion } from './runtime-version.js';
8586
*/
8687

8788
/**
88-
* The `name` every refusal carries, and what a host discriminates it by across
89-
* module instances. The refusal is thrown at boot, before any socket opens, so
90-
* it answers no request and carries no `error.code` of the request vocabulary.
89+
* The machine code a host discriminates this refusal by, across module
90+
* instances. Registered in `@objectstack/spec`'s error-code ledger under this
91+
* package; the refusal is thrown at boot, before any socket opens, so no
92+
* request ever carries it.
9193
*/
94+
export const FORM_TYPE_SWITCH_BOOT_REFUSAL_CODE = 'FORM_TYPE_SWITCH_REQUIRED' as const;
95+
96+
/** The `name` every refusal carries: what a log line and a stack trace show. */
9297
export const FORM_TYPE_SWITCH_BOOT_REFUSAL_NAME = 'FormTypeSwitchBootRefusal' as const;
9398

9499
/**
@@ -172,6 +177,7 @@ export type FormTypeSwitchMarkerState = 'absent' | 'unverified' | 'unreadable' |
172177
export type FormTypeSwitchRefusalReason = FormTypeSwitchMarkerState | 'birth-unwritten' | 'guard-replaced';
173178

174179
export class FormTypeSwitchBootRefusal extends Error {
180+
readonly code = FORM_TYPE_SWITCH_BOOT_REFUSAL_CODE;
175181
override readonly name = FORM_TYPE_SWITCH_BOOT_REFUSAL_NAME;
176182
constructor(
177183
message: string,
@@ -184,7 +190,7 @@ export class FormTypeSwitchBootRefusal extends Error {
184190

185191
/** Structural, not `instanceof`: a host may hold two module instances. */
186192
export function isFormTypeSwitchBootRefusal(error: unknown): error is FormTypeSwitchBootRefusal {
187-
return !!error && typeof error === 'object' && (error as { name?: unknown }).name === FORM_TYPE_SWITCH_BOOT_REFUSAL_NAME;
193+
return !!error && typeof error === 'object' && (error as { code?: unknown }).code === FORM_TYPE_SWITCH_BOOT_REFUSAL_CODE;
188194
}
189195

190196
/**

‎packages/runtime/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export {
169169
FORM_TYPE_SWITCH_PROTOCOL_MAJOR,
170170
recordFormTypeSwitchBirth,
171171
FormTypeSwitchBootRefusal,
172+
FORM_TYPE_SWITCH_BOOT_REFUSAL_CODE,
172173
FORM_TYPE_SWITCH_BOOT_REFUSAL_NAME,
173174
assertFormTypeSwitched,
174175
assertFormTypeSwitchGuardNotReplaced,

‎packages/spec/docs/references/api/automation-api.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ const result = AutomationApiErrorCode.parse(data);
419419

420420
| Property | Type | Required | Description |
421421
| :--- | :--- | :--- | :--- |
422-
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +385 more>` | ✅ | The inner refusal's code in its WIRE spelling - the ADR-0112 member a record save of the same write answers in `error.code` (e.g. `UNIQUE_VIOLATION`); the closed set `ApiErrorSchema.code` parses against |
422+
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +386 more>` | ✅ | The inner refusal's code in its WIRE spelling - the ADR-0112 member a record save of the same write answers in `error.code` (e.g. `UNIQUE_VIOLATION`); the closed set `ApiErrorSchema.code` parses against |
423423
| **message** | `string` | ✅ | The inner refusal's own diagnostic text, verbatim, without the node and operation prefixes the run's failure text adds |
424424

425425
### Allowed Values: `FlowInnerRefusal.code`
@@ -612,6 +612,7 @@ const result = AutomationApiErrorCode.parse(data);
612612
* `FORM_ADDRESS_TAKEN`
613613
* `FORM_NOT_FOUND`
614614
* `FORM_RESOLVE_FAILED`
615+
* `FORM_TYPE_SWITCH_REQUIRED`
615616
* `HOOK_ABORTED`
616617
* `HOOK_UNSCOPED_DATA_ACCESS`
617618
* `IMPORT_JOB_CREATE_FAILED`

‎packages/spec/docs/references/api/contract.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const result = ApiErrorSchema.parse(data);
2222

2323
| Property | Type | Required | Description |
2424
| :--- | :--- | :--- | :--- |
25-
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +385 more>` | ✅ | Error code (e.g. VALIDATION_ERROR; StandardErrorCode ∪ the ledger the serving side registers — ERROR_CODE_LEDGER for framework packages) |
25+
| **code** | `Enum<'VALIDATION_ERROR' \| 'INVALID_FIELD' \| 'MISSING_REQUIRED_FIELD' \| 'INVALID_FORMAT' \| 'VALUE_TOO_LONG' \| 'VALUE_TOO_SHORT' \| 'VALUE_OUT_OF_RANGE' \| … +386 more>` | ✅ | Error code (e.g. VALIDATION_ERROR; StandardErrorCode ∪ the ledger the serving side registers — ERROR_CODE_LEDGER for framework packages) |
2626
| **declaredCode** | `string` | optional | The producer-declared code, verbatim, when it is not a member of the closed `code` vocabulary — the open, author-authored channel (app-specific spellings; ADR-0112) |
2727
| **message** | `string` | ✅ | Readable error message |
2828
| **userMessage** | `string` | optional | Producer-marked user-facing refusal text, verbatim, in the request's locale: platform-composed text falls back to the workspace default locale, then `en`; an app-authored refusal falls back to its declared wording. Present exactly when the producer opted in at throw time; consumers render it to end users as received and keep their generic substitution for anything unmarked. Status-agnostic; never replaces `message`. |
@@ -222,6 +222,7 @@ const result = ApiErrorSchema.parse(data);
222222
* `FORM_ADDRESS_TAKEN`
223223
* `FORM_NOT_FOUND`
224224
* `FORM_RESOLVE_FAILED`
225+
* `FORM_TYPE_SWITCH_REQUIRED`
225226
* `HOOK_ABORTED`
226227
* `HOOK_UNSCOPED_DATA_ACCESS`
227228
* `IMPORT_JOB_CREATE_FAILED`

‎packages/spec/docs/references/api/error-code-ledger.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ const result = ErrorCode.parse(data);
389389
* `FORM_ADDRESS_TAKEN`
390390
* `FORM_NOT_FOUND`
391391
* `FORM_RESOLVE_FAILED`
392+
* `FORM_TYPE_SWITCH_REQUIRED`
392393
* `HOOK_ABORTED`
393394
* `HOOK_UNSCOPED_DATA_ACCESS`
394395
* `IMPORT_JOB_CREATE_FAILED`

‎packages/spec/src/api/error-code-ledger.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,25 @@ describe('ErrorCode (standard ∪ registered)', () => {
295295
expect(STANDARD_SYNONYM_WAIVERS.map((w) => w.code)).not.toContain(code);
296296
});
297297

298+
it('accepts the protocol-19 boot refusal\'s code under the runtime, as its own meaning (door: none)', () => {
299+
// ADR-0159 D7, ruling 7303149461: `os serve` refuses a database whose
300+
// protocol-19 switch is not recorded. `@objectstack/runtime`'s
301+
// `FormTypeSwitchBootRefusal` stamps it and its kernel suite asserts this
302+
// exact string by value — pinned here too. Thrown at boot, before any
303+
// socket opens: registered on the "door or no door" rule, like the v18
304+
// organization-ownership refusal beside it.
305+
const code = 'FORM_TYPE_SWITCH_REQUIRED';
306+
expect(ErrorCode.parse(code)).toBe(code);
307+
expect(ERROR_CODE_LEDGER['@objectstack/runtime'], `${code} registered under runtime`).toContain(code);
308+
expect(REGISTERED_ERROR_CODES).toContain(code);
309+
// FORM, TYPE and SWITCH are tokens no standard member carries: not a
310+
// synonym of any standard code, and no waiver.
311+
expect(standardSynonymOf(code), `${code} needs no waiver`).toBeUndefined();
312+
expect(STANDARD_SYNONYM_WAIVERS.map((w) => w.code)).not.toContain(code);
313+
// Not a re-spelling of the v18 refusal: both stay registered, each its own act.
314+
expect(ERROR_CODE_LEDGER['@objectstack/runtime']).toContain('ORGANIZATION_OWNERSHIP_MIGRATION_REQUIRED');
315+
});
316+
298317
it('accepts the package disable door\'s dependent-validation-rule refusal code, registered ahead of its runtime producer', () => {
299318
// ADR-0141 Open item 4, ruling A on #30379: the disable door answers 409
300319
// while an enabled package's validation rule reads a field the package

0 commit comments

Comments
 (0)