Skip to content

Commit 4031cd5

Browse files
committed
feat(spec)!: read a thrown reasons list only as a direct member, and declare the narrowing it brings
The save reader now takes a `reasons: [` list only when it is a direct member of the thrown object literal, the one key the run reads; a list nested under `params` is the body's own data, as before (control pins on both save checks). Two docblocks now say what the code does: a reason reaches the wire as the label of a rendered line, and a listed reason is read under every code. The changeset declares the narrowing (`Clause-②: yes (narrowing)`, `Metadata-protocol: narrowed`) with its BREAKING banner, the FROM → TO migration for the three sources now refused, and the ADR-0087 semantic entry it registers. It no longer claims `details.refusals` keeps every sentence: the closing line is only on `userMessage`, which `/data` bounds. Claude-Session: https://claude.ai/code/session_01Utp6CXvMJTCqCs1b69pCH7 Co-authored-by: Claude <noreply@anthropic.com>
1 parent a2558d1 commit 4031cd5

7 files changed

Lines changed: 133 additions & 20 deletions

File tree

‎.changeset/36877-refusal-reasons-list.md‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
'@objectstack/types': minor
55
---
66

7-
feat(refusals): one refusal names several declared reasons, each in the person's language
7+
feat(refusals)!: one refusal names several declared reasons, each in the person's language
88

9-
Clause-②: yes
10-
Metadata-protocol: added
9+
Clause-②: yes (narrowing)
10+
Metadata-protocol: narrowed
11+
12+
<!-- adr-0087: registered refusal-reasons-list-literal-judged -->
13+
14+
**BREAKING** accept-set narrowing beside a widening, shipped as `minor`. The widening is the new `reasons` list below. The narrowing is what that list makes of a `reasons` key that was ignored before: three sources that save and run on the previous release are refused now (see the migration at the end).
1115

1216
A hook or a `script` action that declares `refusals` can now refuse with several declared sentences at once, for example when it checks a whole selection and refuses all of it. Under one declared code it throws an ordered list of the code's declared reasons, each with its own `params`, instead of one `reason`:
1317

@@ -19,11 +23,21 @@ throw { code: 'SUBMIT_REFUSED', params: { count }, reasons: [
1923
```
2024

2125
- Each listed reason's sentence is picked in the caller's locale and its holes are filled from that reason's own `params`, as a single reason's sentence is. The answer's `userMessage` is those sentences joined by a line break, in the order thrown, then the code's own `message` as the last line (its holes are filled from the throw's top-level `params`). The code's status and the code on the wire are unchanged.
22-
- Every door that answers the refusal also carries the sentences structured, as `details.refusals: [{ reason, message }, …]`. The `/data` door bounds `userMessage` at 500 characters as before; `details.refusals` keeps every sentence whole.
23-
- Every listed reason must be one the code declares under `reasons`. One that is not makes the whole throw the loud `500` fault naming the hook or action, the code and the reason. Under a code that declares no `reasons`, every listed reason is undeclared. A literal list in the source (`throw { code, reasons: [{ reason: 'x' }] }`, or `e.reasons = [{ reason: 'x' }]` on an error the body throws) is refused when the hook or action is saved.
26+
- Every door that answers the refusal also carries each listed reason's sentence structured, as `details.refusals: [{ reason, message }, …]`. The `/data` door bounds `userMessage` at 500 characters as before, and `details.refusals` is not bounded, so each listed line stays whole there. The closing line (the code's own `message`) is not on `details.refusals`: on a list long enough that `/data` cuts `userMessage`, the closing line is lost on that door. A client that renders the list reads the lines from `details.refusals`.
27+
- Every listed reason must be one the code declares under `reasons`. One that is not makes the whole throw the loud `500` fault naming the hook or action, the code and the reason. Under a code that declares no `reasons`, every listed reason is undeclared. A literal list in the source (`throw { code, reasons: [{ reason: 'x' }] }`, or `e.reasons = [{ reason: 'x' }]` on an error the body throws) is refused when the hook or action is saved. Only a `reasons` key that is a direct member of the thrown object is a list; one nested deeper (`params: { reasons: [ … ] }`) is the body's own data, as before.
2428
- A throw that carries both a `reason` and a `reasons` list, an empty list, or a `reasons` value that is not a list of `{ reason, params? }` is the same loud fault.
2529
- The sandboxed body and the in-process hook handler read the list the same way and answer byte-identically.
2630

2731
The catalogue entry (`message`, `status`, `reasons`, `params`) and the translation keys do not change, so no `os i18n` key is added. A throw with one `reason`, or none, is answered exactly as before.
2832

2933
New exports: `@objectstack/spec` adds `isUndeclaredThrownReason`, `unreadableHookRefusalListAtRun`, `unreadableActionRefusalListAtRun` and the `HookRefusalThrownReason` and `RefusalListProblem` types (`data`); `hookRefusalReasonsThrownIn` now answers `HookRefusalThrownReason[]` (a listed pair carries `listed: true`). `@objectstack/types` adds `declaredRefusalLines` and the `DeclaredRefusalLine` type. `@objectstack/runtime` adds no export; its `SandboxError` gains an optional `refusals`, the list the doors read.
34+
35+
**Migration (FROM → TO).** Each row was accepted and answered as if the `reasons` key were absent; it is refused now.
36+
37+
| FROM (accepted before, refused now) | Where it is refused | TO |
38+
|:---|:---|:---|
39+
| A literal `throw { code: 'X', reasons: [{ reason: 'y' }] }` where `X` declares no `reasons` (a literal `y` under a code that declares `reasons` without it was already refused) | At save: `HookSchema` / `ActionSchema` at `body.source` (the metadata save door, `defineStack`, `os validate`, compile, an artifact's parse) | Declare `reasons` on `X` with `y` and its `message`, or remove the `reasons` key |
40+
| `e.code = 'X'; e.reasons = [{ reason: 'y' }]; throw e` with `y` undeclared under `X` | At save, as above | Declare `y` under `refusals.X.reasons`, or remove the `e.reasons` assignment |
41+
| A body that throws a declared code with a `reasons` property of its own: an empty list, a value that is not a list of `{ reason, params? }`, a list beside a `reason`, or a list naming an undeclared reason | At run: the loud `500` fault naming the hook or action and the code, where the declared sentence and status answered before | Rename the body's own property (for example to `items`, or put it under `params`), or throw it as the declared list |
42+
43+
One-line fix: a `reasons` key on a thrown refusal is now the declared list, so name only reasons the code declares there, or rename the key.

‎packages/spec/src/data/hook-refusal-codes.ts‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ const CODE_LITERAL = String.raw`(?<quote>['"\x60])(?<code>[A-Z][A-Z0-9]*(?:_[A-Z
4242

4343
/**
4444
* The spelling of a declared refusal reason (#33214): snake_case, a machine
45-
* name like every other key an author declares under a code. A reason never
46-
* reaches the wire — the code does — so it is spelled as a data value, not as
47-
* an error code, and the literal reading can tell the two apart.
45+
* name like every other key an author declares under a code. A client
46+
* branches on the code, not on a reason: a reason reaches the wire only as the
47+
* label of a rendered line (`details.refusals[].reason`, #36877), beside the
48+
* sentence it picked. So it is spelled as a data value, not as an error code,
49+
* and the literal reading can tell the two apart.
4850
*/
4951
export const HOOK_REFUSAL_REASON_PATTERN = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/;
5052

@@ -69,7 +71,8 @@ const REASON_ASSIGNMENT = new RegExp(
6971
/**
7072
* [#36877] `reasons: [` — the opening of the list a `throw`'s object literal
7173
* carries (`{ code, reasons: [{ reason, params? }, …] }`); the reasons inside
72-
* the bracket are the listed ones.
74+
* the bracket are the listed ones. Read only as a direct member of the thrown
75+
* object literal ({@link listedSpans}).
7376
*/
7477
const REASONS_LIST_PROPERTY = /(?<![\w$.])reasons\s*:\s*\[/g;
7578

@@ -193,12 +196,45 @@ function closeOfBracket(masked: string, open: number): number {
193196
return masked.length;
194197
}
195198

196-
/** [#36877] The `[from, to)` spans of every `reasons: [ … ]` list in `text`. */
197-
function listedSpans(text: string): Array<[number, number]> {
199+
/**
200+
* [#36877] The brackets still open at `at` in `text` (a masked source or a
201+
* `throw` statement of one), outermost first, strings skipped.
202+
*/
203+
function openBracketsAt(text: string, at: number): string[] {
204+
const open: string[] = [];
205+
for (let i = 0; i < at && i < text.length; i++) {
206+
const ch = text[i];
207+
if (ch === '"' || ch === "'" || ch === '`') {
208+
i = skipString(text, i);
209+
continue;
210+
}
211+
if (ch === '(' || ch === '[' || ch === '{') open.push(ch);
212+
else if (ch === ')' || ch === ']' || ch === '}') open.pop();
213+
}
214+
return open;
215+
}
216+
217+
/**
218+
* [#36877] The `[from, to)` spans of every `reasons: [ … ]` list in a `throw`
219+
* statement that is a DIRECT member of the thrown object literal — the one key
220+
* the run-time address reads (`e.reasons`). The member's enclosing brackets
221+
* hold exactly one `{`, innermost, and no `[`: `throw { code, reasons: [ … ] }`
222+
* and `throw Object.assign(new Error(), { code, reasons: [ … ] })` qualify;
223+
* `throw { code, params: { reasons: [ … ] } }` does not, because the run never
224+
* answers that nested key as a list.
225+
*/
226+
function listedSpans(statement: string): Array<[number, number]> {
198227
const spans: Array<[number, number]> = [];
199-
for (const m of text.matchAll(REASONS_LIST_PROPERTY)) {
200-
const open = (m.index ?? 0) + m[0].length - 1;
201-
spans.push([open, closeOfBracket(text, open)]);
228+
for (const m of statement.matchAll(REASONS_LIST_PROPERTY)) {
229+
const at = m.index ?? 0;
230+
const enclosing = openBracketsAt(statement, at);
231+
const direct =
232+
enclosing[enclosing.length - 1] === '{' &&
233+
enclosing.filter((ch) => ch === '{').length === 1 &&
234+
!enclosing.includes('[');
235+
if (!direct) continue;
236+
const open = at + m[0].length - 1;
237+
spans.push([open, closeOfBracket(statement, open)]);
202238
}
203239
return spans;
204240
}

‎packages/spec/src/data/hook-refusals.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,18 @@ describe('hookRefusalReasonsThrownIn — the reasons of a thrown list (#36877)',
494494
"throw { code: 'C', reason: 'r', reasons: [{ reason: 'l' }] };",
495495
[{ code: 'C', reason: 'r' }, { code: 'C', reason: 'l', listed: true }],
496496
],
497+
[
498+
// the list as a direct member of the object Object.assign stamps
499+
"throw Object.assign(new Error('x'), { code: 'C', reasons: [{ reason: 'l' }] });",
500+
[{ code: 'C', reason: 'l', listed: true }],
501+
],
502+
[
503+
// CONTROL: a `reasons` key nested under `params` is not the thrown list
504+
// (the run reads `e.reasons` only), so its reason is read as main reads
505+
// any `reason: 'x'` in the statement: a single, unlisted pair
506+
"throw { code: 'C', params: { reasons: [{ reason: 'late' }] } };",
507+
[{ code: 'C', reason: 'late' }],
508+
],
497509
])('reads %j', (source, pairs) => {
498510
expect(hookRefusalReasonsThrownIn(source)).toEqual(pairs);
499511
});
@@ -566,6 +578,15 @@ describe('Hook.refusals — a listed reason is refused at save where the body na
566578
expect(r.error!.issues[0]!.message).toContain('(declared: none)');
567579
});
568580

581+
it('CONTROL: a `reasons` list nested under `params` is not a thrown list, so it saves under a code with no reasons', () => {
582+
const r = HookSchema.safeParse({
583+
...base,
584+
body: body("throw { code: 'PLAIN', params: { reasons: [{ reason: 'late' }] } };"),
585+
refusals,
586+
});
587+
expect(r.success).toBe(true);
588+
});
589+
569590
it('CONTROL: a single reason under a code that declares no reasons still saves, as before', () => {
570591
const r = HookSchema.safeParse({ ...base, body: body("throw { code: 'PLAIN', reason: 'anything' };"), refusals });
571592
expect(r.success).toBe(true);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,11 @@ function refuseHookBodyMailGrant(
254254
* parse — so the author meets the refusal before any write does. One issue per
255255
* code. A code that arrives any other way is the run-time refusal's. The same
256256
* holds for a reason under a code that declares `reasons` (#33214,
257-
* `hookRefusalReasonsThrownIn`); under a code that declares none, a thrown
258-
* `reason` is not read at all, as before.
257+
* `hookRefusalReasonsThrownIn`); under a code that declares none, a single
258+
* thrown `reason` is not read at all, as before. A reason inside a `reasons`
259+
* list (#36877, ruling 7178348458) is read under every code: under one that
260+
* declares no `reasons`, every listed reason is undeclared and the save
261+
* refuses it (`isUndeclaredThrownReason`).
259262
*
260263
* [#33214, ruling 6879477573 letter A] The catalogue is answered on every path
261264
* a hook runs on: a sandboxed `js` body, and an in-process `handler` — an
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import type { SemanticMigration } from '../../types.js';
4+
5+
export const entry: SemanticMigration = {
6+
id: 'refusal-reasons-list-literal-judged',
7+
surface: 'a hook `body.source` or a `script` action `body.source` whose `throw` carries a `reasons` key beside a '
8+
+ 'code its `refusals` catalogue declares: `throw { code, reasons: [ … ] }`, or `e.reasons = [ … ]` on an error '
9+
+ 'the body throws',
10+
replacement: 'name in `reasons` only reasons the code declares under `refusals.<CODE>.reasons`, each as '
11+
+ '`{ reason, params? }` (declare `reasons` on the code, with each sentence, where it has none); or rename a '
12+
+ "`reasons` property the body uses for its own data (for example to `items`, or put it under `params`).",
13+
reason:
14+
'A refusal may now name an ordered list of declared reasons in one throw, `{ code, reasons: [{ reason, params? }, '
15+
+ "… ] }`, each rendered in the caller's locale as one line of `userMessage` and of `details.refusals`. Before, a "
16+
+ '`reasons` key on a thrown refusal was ignored and the code\'s own sentence answered. Now it is read: at save, a '
17+
+ 'literal list naming a reason under a code that declares no `reasons` is refused at `body.source` (a literal '
18+
+ 'reason the code does not declare under its `reasons` was already refused), and so is a literal '
19+
+ '`e.reasons = [ … ]` list on a thrown error naming an undeclared reason. At run, a `reasons` value that is empty, '
20+
+ 'not a list of `{ reason, params? }`, thrown beside a `reason`, or naming an undeclared reason is the loud 500 '
21+
+ 'fault naming the hook or action and the code, where the declared sentence and status answered before. Only a '
22+
+ '`reasons` key that is a direct member of the thrown object is read; a nested one (`params: { reasons }`) is '
23+
+ 'the body\'s own data. There is no mechanical rewrite: whether the key was meant as the list or as the body\'s '
24+
+ 'own property is the author\'s call.',
25+
acceptanceCriteria:
26+
'Every hook and `script` action that declares `refusals` saves (`os validate` reports no undeclared-reason issue '
27+
+ 'at `body.source`), and no run of it answers the 500 refusal-list fault: every thrown `reasons` key is a '
28+
+ 'non-empty list of reasons its code declares, with no `reason` beside it.',
29+
};

‎packages/spec/src/ui/action-refusals.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ describe('Action.refusals — a listed reason is refused at save where the body
306306
expect(issue?.message).toBe(undeclaredActionRefusalReasonAtSave('NOT_DRAFT', 'closed', []));
307307
});
308308

309+
it('CONTROL: a `reasons` list nested under `params` is not a thrown list, so it saves under a code with no reasons', () => {
310+
expect(ActionSchema.safeParse({
311+
...base,
312+
body: body("throw { code: 'NOT_DRAFT', params: { reasons: [{ reason: 'closed' }] } };"),
313+
refusals,
314+
}).success).toBe(true);
315+
});
316+
309317
it('the run-time list fault names the action', () => {
310318
expect(unreadableActionRefusalListAtRun('submit_signoff', 'STAGE_LOCKED', 'empty')).toContain(
311319
"action 'submit_signoff' threw code 'STAGE_LOCKED' with a `reasons` list that names no reason",

‎packages/types/src/data-error-classification.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,10 +611,12 @@ function withDeclaredUserMessage(
611611
* [#36877, ruling 7178348458 term 3] A declared refusal thrown as a `reasons`
612612
* list carries its sentences as `details.refusals` on this door too, riding
613613
* with the `userMessage` that joins them ({@link declaredRefusalLines}, the
614-
* one recogniser the dispatcher's composition reads). Every sentence is kept
615-
* whole: `userMessage` is bounded at {@link CLIENT_MESSAGE_MAX} (#5423), and a
616-
* long list read off the structured member loses no line. `details` is
617-
* merged, so a member another arm set is kept; a body with no list is
614+
* one recogniser the dispatcher's composition reads). `userMessage` is bounded
615+
* at {@link CLIENT_MESSAGE_MAX} (#5423) and the list is not: each listed
616+
* reason's line stays whole on `details.refusals`. The closing line — the
617+
* code's own `message`, the last line of `userMessage` — is not on the list,
618+
* so on a list long enough to cut it, this door carries it nowhere. `details`
619+
* is merged, so a member another arm set is kept; a body with no list is
618620
* returned as it was.
619621
*/
620622
function withDeclaredRefusalLines(error: unknown, body: Record<string, unknown>): Record<string, unknown> {

0 commit comments

Comments
 (0)