Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
eca1eed
wip(spec): hook refusals on handler hooks, reasons and params (#33214)
claude Nov 14, 2026
96c899d
wip(spec): tests and dropped-refinement ledger (#33214)
claude Nov 14, 2026
8e0dccd
wip(runtime): answer hook refusal reasons, params and in-process hand…
claude Nov 14, 2026
5462b60
wip(runtime): tests for reasons, params and the in-process seam (#33214)
claude Nov 14, 2026
2a1d951
wip(lint): count and check hook refusal reasons (#33214)
claude Nov 14, 2026
34d34eb
wip(lint,cli): reason translation targets and coverage pins (#33214)
claude Nov 14, 2026
92ab7f5
test(verify): hook refusal catalogue parity on both bootStack doors (…
claude Nov 14, 2026
21b1092
chore(spec): liveness rows for hook refusal reasons and params (#33214)
claude Nov 14, 2026
8f3978a
chore(spec): regenerate api surface, export origins, declaration map …
claude Nov 14, 2026
ffdc490
test(cli): a lowered handler hook keeps its refusal catalogue (#33214)
claude Nov 14, 2026
ef84ba7
test(cli): read the lowered reason literal with one pattern (#33214)
claude Nov 14, 2026
51b46e8
docs(hooks,i18n): declared refusals on handler hooks, reasons and par…
claude Nov 14, 2026
0123495
Merge remote-tracking branch 'origin/main' into claude/issue-33214-ha…
claude Nov 14, 2026
ec27b08
refactor(spec,runtime): build the refusal answer's code from a local,…
claude Nov 14, 2026
990d461
test(cli,verify): pay the module loads at collection (#33214)
claude Nov 14, 2026
68aca57
test(spec): declare the hook form's refusal reasons/params gap; ledge…
claude Nov 14, 2026
e7714f5
Merge remote-tracking branch 'origin/main' into claude/issue-33214-ha…
claude Nov 15, 2026
7bb3498
chore(spec): regenerate api-surface and export-origins after merging …
claude Nov 15, 2026
90536ec
fix(spec,runtime,lint): read a refusal reason only under a code that …
claude Nov 15, 2026
9337b82
test(lint): hook refusal template compile pins; drop the duplicate em…
claude Nov 15, 2026
3a820ad
docs(changeset): reasons read only under a declared reasons map; para…
claude Nov 15, 2026
7434178
chore(spec): regenerate the hook reference page (#33214)
claude Nov 15, 2026
1c9a1ec
test(lint): classify the hook refusal template pass in the authored-s…
claude Nov 15, 2026
c800d2a
Merge remote-tracking branch 'origin/main' into claude/issue-33214-ha…
claude Nov 15, 2026
da6c538
chore(spec): regenerate export-origins, api-surface and the translati…
claude Nov 15, 2026
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
21 changes: 21 additions & 0 deletions .changeset/33214-handler-hook-refusals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/spec': minor
'@objectstack/runtime': minor
'@objectstack/lint': minor
---

feat(spec,runtime): a hook written as a TypeScript `handler` can declare `refusals`, and a refusal code can hold several declared sentences with parameters

Clause-②: yes
Metadata-protocol: added
Skills-docs: follow-up #33658 — skills/objectstack-data/references/data-hooks.md (the "Declared refusals (`refusals`)" section teaches a sandboxed `body` only, one sentence per code, no params), skills/objectstack-i18n/SKILL.md (the `hooks.<hook>.refusals.<CODE>.message` row has no `reasons` key)

A hook's refusal catalogue (`HookSchema.refusals`) is answered on every path the hook runs on, and an entry can hold more than one sentence (maintainer ruling 6879477573: letter A, S1, params; inside ruling 6431311681 letter A).

- **A `handler` hook may declare `refusals`.** `defineStack`, `os validate`, `os build` and the metadata save door used to refuse `refusals` on a hook with no `js` body. They now accept it on a hook whose `handler` is an inline function or a string reference. An undeclared code that the handler's source throws literally is refused at save, at `handler`. The catalogue is answered on every path: the body `os build` lowers the handler to (as before), and the in-process handler of a source boot (`bootStack(config)`, `os serve` over a host config), plus the function a string `handler` names when the build could not lower it. Both answer with the same envelope. An error a `ctx.api` or `ctx.ql` call raised, which the handler lets through unchanged, passes through as it does on the body path. Still refused: `refusals` on a hook whose code cannot throw a code (an `expression` body, or neither a body nor a handler).
- **`reasons`: several sentences under one wire code.** `refusals.CODE.reasons.REASON.message`, with a snake_case reason. The hook throws `{ code, reason }`. The caller reads the reason's sentence, with the code and the status of its entry, so the wire code and the envelope are unchanged. Under a code that declares `reasons`, a reason it does not declare is refused loudly: at save where the hook's source throws it literally, otherwise at run time as a `500` naming the hook, the code and the reason. Under a code that declares no `reasons`, a thrown `reason` is not read, as before: the entry's own sentence answers. Translated through `hooks.HOOK.refusals.CODE.reasons.REASON.message`. `os i18n extract` and `os i18n check` count each declared reason, and `os validate` refuses a translation key over a reason the code does not declare (`translation-target-unknown`).
- **`params`: names a sentence may fill.** An entry declares `params: ['state']`, and its sentences (its `message` and each reason) may write a double-brace hole over those names. The hook throws `params: { state }`. The runtime fills the holes after it picks the translation, through the formula template engine the flow `end` refusal renders with, and only declared names are in scope. A hole over an undeclared name is refused at save. A hole holding logic, an unknown formatter, an empty hole and a `currency` formatter with no code (a hook refusal carries no default currency) are refused by `os validate`, `os compile` and the publish gate's `hook` write (`@objectstack/lint`). An entry with no `params` stays plain text and every hole in it is refused, as before. The rule is now a refinement (`dropped-refinements.baseline.json` gains `data/HookRefusal`), so the published JSON Schema no longer carries the hole pattern on `message`.

Unchanged: a hook that declares none of the new parts answers byte-for-byte as before (pinned against the base commit's output), including one that throws a `reason` property under a code with no `reasons` (saved and answered as on `main`, pinned on both paths), and a hook that declares no `refusals` is untouched on both paths.

New exports in `@objectstack/spec/data`: `HOOK_REFUSAL_REASON_PATTERN`, `HOOK_REFUSAL_PARAM_PATTERN`, `hookRefusalReasonsThrownIn`, `undeclaredHookRefusalReasonAtSave`, `undeclaredHookRefusalReasonAtRun`. `hookRefusalMessageKey(hook, code, reason?)` takes an optional reason.
69 changes: 69 additions & 0 deletions content/docs/automation/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,75 @@ would move a handler's own `ctx.api` writes *outside* the transaction the write
ran in, which is a worse guarantee than the one documented here.
</Callout>

## Refusing in the user's language — `refusals`

A hook that says "no" to a write throws. A sentence written into the throw
reaches every caller in the language you wrote it in. To refuse in the
caller's language, **declare the refusals** on the hook and throw a declared
code. The runtime answers with the declared sentence, translated in the
caller's locale, and with the declared status:

```typescript
export const planGate = {
name: 'plan_gate',
object: 'production_plan',
events: ['beforeUpdate'],
refusals: {
CREDIT_LIMIT: { message: 'This plan exceeds the credit limit.', status: 409 },
VALIDATION_FAILED: {
message: 'This plan cannot be submitted.',
params: ['state'],
reasons: {
plan_locked: { message: 'The plan is {{ state }} and can no longer change.' },
second_primary: { message: 'This plant already has a primary plan.' },
},
},
},
handler: async (ctx) => {
if (ctx.previous?.locked) {
throw Object.assign(new Error('locked'), {
code: 'VALIDATION_FAILED',
reason: 'plan_locked',
params: { state: ctx.previous.state },
});
}
},
};
```

- **The code is the key.** Each entry of `refusals` is keyed by the
SCREAMING_SNAKE code the hook throws, and the caller's envelope carries that
code. An entry has a `message` and, optionally, a 4xx `status` (400 when
absent).
- **`reasons` — several sentences under one code.** A gate with several
refusals of the same class keeps one wire code and declares one snake_case
reason per sentence. Throw `{ code, reason }`. The caller reads the reason's
sentence, with the code and the status of the entry it sits under.
- **`params` — names a sentence may fill.** An entry that declares `params`
may write `{{ name }}` holes over those names, in its own `message` and in
each reason. Throw the values as `params: { name: value }`. A hole over any
other name is refused when the hook is saved. An entry that declares no
`params` is plain text, and every `{{ }}` hole in it is refused.
- **It works whichever way the hook is written.** A TypeScript `handler` (run
in-process on a source boot such as `bootStack(config)`, and lowered to a
`js` body by `os build`) and a sandboxed `js` body answer the same envelope.
- **Loud on a mistake.** A code the catalogue does not declare, or a reason a
code with `reasons` does not declare, is refused when the hook is saved if
the hook names it literally in a `throw`.
Otherwise it is refused when the hook runs, as a `500` that names the hook
and the code. A throw with no code is refused the same way. An error a
`ctx.api` call raised and the hook let through unchanged, and a crash, pass
through as they always did. Under a code that declares no `reasons`, a
`reason` on the throw is not read: the entry's own sentence answers.
- **Translated by key.** Each sentence has a key the translation bundle
writes, `hooks.<hook>.refusals.<CODE>.message` and
`hooks.<hook>.refusals.<CODE>.reasons.<REASON>.message`; `os i18n extract`
scaffolds them and `os i18n check` counts them. A translation keeps the
entry's `{{ }}` holes. See [Translations](/docs/ui/translations).

A hook that declares no `refusals` refuses exactly as before: the thrown
error's own `code`, `status` and message reach the caller.

## Recipe — refuse overlapping time ranges

Leave and overtime requests share one rule: two live records of the same owner
Expand Down
1 change: 1 addition & 0 deletions content/docs/protocol/kernel/i18n-standard.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ beneath an action entry are judged the same way under either.
| `reports.{name}` | a report's `name`, declared |
| `hooks.{hook}` | a hook's `name`, declared |
| `hooks.{hook}.refusals.{code}` | a code that hook's `refusals` declares |
| `hooks.{hook}.refusals.{code}.reasons.{reason}` | a reason that code declares under `reasons` |
| `flows.{flow}.screens.{node}` | the `id` of a `type: 'screen'` node in that flow, nodes nested in loop, parallel and try/catch regions included |
| `flows.{flow}.screens.{node}.fields.{field}` | a `config.fields[].name` on that screen — an object-form screen (`config.objectName`) declares none, and its input labels live under `objects.{objectName}.fields` |

Expand Down
16 changes: 13 additions & 3 deletions content/docs/references/data/hook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const result = HookSchema.parse(data);
| **timeoutMs** | `number` | optional | Maximum execution time in milliseconds before the hook is aborted |
| **timeout** | `never` | optional | [REMOVED] `hook.timeout` was removed in @objectstack/spec 17 — its unit (milliseconds) lived only in the description, beside a body-level `timeoutMs` and a `retryPolicy.backoffMs` that spell theirs, so the same number read as two conventions on one surface. Rename the key to `timeoutMs`; the value (milliseconds) is unchanged. Run `os migrate meta --from 17` to list the mechanical edits for existing sources; `--write` applies the ones it can prove, and you apply the rest by hand. |
| **onError** | `Enum<'abort' \| 'log'>` | optional (default: `"abort"`) | Error handling strategy. abort: refuse the operation; a blocking hook's abort rolls the write back inside a unit of work, which the write door opens for an after* hook on an object on the default datasource whose driver declares transactions (elsewhere the caller is refused and the row stays stored). log: log the error and continue |
| **refusals** | `Record<string, { message: string; status?: integer }>` | optional | Refusal catalogue keyed by the SCREAMING_SNAKE code the body throws: `{ message, status? }`. The body refuses by code only; the runtime answers with the declared message (translated through hooks.`<hook>`.refusals.`<CODE>`.message in the caller's locale) and status. A code not declared here is refused loudly — at save when the body throws it literally, otherwise at run time |
| **refusals** | `Record<string, { message: string; status?: integer; reasons?: Record<string, object>; params?: string[] }>` | optional | Refusal catalogue keyed by the SCREAMING_SNAKE code the hook throws: `{ message, status?, reasons?, params? }`. The hook (a js body or a handler) refuses by code only, optionally with a declared reason and params; the runtime answers with the declared message (translated through hooks.`<hook>`.refusals.`<CODE>`.message, or ….`<CODE>`.reasons.`<REASON>`.message, in the caller's locale, holes filled from params) and status. A code not declared here, or a reason a code with reasons does not declare, is refused loudly — at save when the hook throws it literally, otherwise at run time; under a code with no reasons a thrown reason is not read |
| **runAs** | `Enum<'system' \| 'user' \| 'inherit'>` | optional (default: `"inherit"`) | Execution identity for the hook's ctx.api data operations: system = elevated (bypasses RLS), user = the triggering user (RLS-respecting), inherit = the context of the write that fired the hook (the pre-runAs behaviour; the default). A hook with no trigger user has no identity to scope to, so under user its ctx.api data operations are REFUSED — declare system to make the elevation explicit. This covers any hook fired by a write that carried no user (an isSystem plugin/service write; a system-elevated flow node). Elevation does not reach identity assignment: a system hook's governed writes (insert, update, delete, transfer, restore, purge) to sys_user_position, sys_user_permission_set, sys_invitation and sys_member are refused with PERMISSION_DENIED, and a system hook whose body names such a write is refused at save; declare user for position holdings and permission-set grants (the ADR-0090 D12 delegated-administration gate then judges the user the hook acts for), and change invitations and memberships through the organization endpoints. Nor does it reach a ledger: a system hook body's update, transfer, delete, purge or restore of an object declared managedBy: 'append-only' is refused with PERMISSION_DENIED; an insert is not judged by this rule. Scope: ctx.api only — condition evaluation, the readonly strip on ctx.input, ctx.session and async are unchanged. |
| **_lock** | `Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>` | optional | Item-level lock — controls overlay & delete (ADR-0010). |
| **_lockReason** | `string` | optional | Human-readable reason shown when a write is refused by _lock. |
Expand Down Expand Up @@ -84,8 +84,10 @@ L2 sandboxed JS body — runs inside an isolated VM with declared capabilities

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **message** | `string` | ✅ | The sentence the user reads when the body throws this code — plain text in the source locale, translated whole through hooks.`<hook>`.refusals.`<CODE>`.message in the caller's locale; a `{{ }}` hole is refused |
| **message** | `string` | ✅ | The sentence the user reads when the hook throws this code — in the source locale, translated whole through hooks.`<hook>`.refusals.`<CODE>`.message in the caller's locale. With no params every `{{ }}` hole is refused; with params a hole is a path over one of them with an optional known formatter, and a hole over another name, a hole holding logic, an unknown formatter, an empty hole and a currency formatter with no code are refused |
| **status** | `integer` | optional | HTTP status the refusal is answered with (4xx; a refusal is never a fault). Absent: 400. A status the body itself sets on the throw is not read — the catalogue decides |
| **reasons** | `Record<string, { message: string }>` | optional | Further sentences under this same code, keyed by the snake_case reason the hook throws beside the code (`{ code, reason }`): `{ message }`. The wire code and status stay this entry's. A reason not declared here is refused loudly. Absent: the code has the one sentence, and a thrown reason is not read |
| **params** | `string[]` | optional | The names this entry's sentences may fill: a `{{ NAME }}` or `{{ NAME \| formatter }}` hole renders the value the hook throws in `params` (`{ code, params: { NAME: value } }`). A hole over any other name, logic, an unknown formatter, an empty hole and a currency formatter with no code are refused. Absent: the sentences are plain text and a `{{ }}` hole is refused |


---
Expand Down Expand Up @@ -182,8 +184,16 @@ L2 sandboxed JS body — runs inside an isolated VM with declared capabilities

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **message** | `string` | ✅ | The sentence the user reads when the body throws this code — plain text in the source locale, translated whole through hooks.`<hook>`.refusals.`<CODE>`.message in the caller's locale; a `{{ }}` hole is refused |
| **message** | `string` | ✅ | The sentence the user reads when the hook throws this code — in the source locale, translated whole through hooks.`<hook>`.refusals.`<CODE>`.message in the caller's locale. With no params every `{{ }}` hole is refused; with params a hole is a path over one of them with an optional known formatter, and a hole over another name, a hole holding logic, an unknown formatter, an empty hole and a currency formatter with no code are refused |
| **status** | `integer` | optional | HTTP status the refusal is answered with (4xx; a refusal is never a fault). Absent: 400. A status the body itself sets on the throw is not read — the catalogue decides |
| **reasons** | `Record<string, { message: string }>` | optional | Further sentences under this same code, keyed by the snake_case reason the hook throws beside the code (`{ code, reason }`): `{ message }`. The wire code and status stay this entry's. A reason not declared here is refused loudly. Absent: the code has the one sentence, and a thrown reason is not read |
| **params** | `string[]` | optional | The names this entry's sentences may fill: a `{{ NAME }}` or `{{ NAME \| formatter }}` hole renders the value the hook throws in `params` (`{ code, params: { NAME: value } }`). A hole over any other name, logic, an unknown formatter, an empty hole and a currency formatter with no code are refused. Absent: the sentences are plain text and a `{{ }}` hole is refused |

### Nested Shape: `HookRefusal.reasons[string]`

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **message** | `string` | ✅ | The sentence the user reads when the hook throws this reason under its code — in the source locale, translated through hooks.`<hook>`.refusals.`<CODE>`.reasons.`<REASON>`.message in the caller's locale; its `{{ }}` holes follow the entry's message rule |


---
Expand Down
8 changes: 4 additions & 4 deletions content/docs/references/system/translation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Translation data for a single object

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **refusals** | `Record<string, { message?: string }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |
| **refusals** | `Record<string, { message?: string; reasons?: Record<string, object> }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |

### Nested Shape: `PlatformTranslationData.positions[string]`

Expand Down Expand Up @@ -475,7 +475,7 @@ Translation data for a single object

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **refusals** | `Record<string, { message?: string }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |
| **refusals** | `Record<string, { message?: string; reasons?: Record<string, object> }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |

### Nested Shape: `ServedTranslationData.positions[string]`

Expand Down Expand Up @@ -716,7 +716,7 @@ Translation data for a single object

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **refusals** | `Record<string, { message?: string }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |
| **refusals** | `Record<string, { message?: string; reasons?: Record<string, object> }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |

### Nested Shape: `TranslationData.positions[string]`

Expand Down Expand Up @@ -926,7 +926,7 @@ Translation data for a single object

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **refusals** | `Record<string, { message?: string }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |
| **refusals** | `Record<string, { message?: string; reasons?: Record<string, object> }>` | optional | Refusal translations keyed by the code the hook's `refusals` declares |

### Nested Shape: `TranslationItem.positions[string]`

Expand Down
Loading
Loading