Skip to content

Commit c19dbcd

Browse files
committed
fix(spec): refuse refusals on a hook with no js body, and a {{ }} hole in a refusal message (#26170)
Contract review FAIL-2: a catalogue beside an in-process `handler` (or an expression body) was saved, its translations demanded by `os i18n`, and never answered. HookSchema now refuses it at `refusals`, naming the remedy. A refusal message is plain text; a `{{ }}` hole is refused by a pattern the published JSON Schema carries too, so a later params slot stays additive. The liveness note no longer claims the build lowers every handler to a body. The changeset names @object-ui/i18n and @object-ui/app-shell, which move. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzfyVQuRsL6SRGuKCem
1 parent e5ea888 commit c19dbcd

9 files changed

Lines changed: 98 additions & 18 deletions

File tree

‎.changeset/26170-hook-refusal-catalogue.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
'@objectstack/objectql': minor
55
'@objectstack/cli': minor
66
'@objectstack/lint': minor
7+
'@object-ui/i18n': patch
8+
'@object-ui/app-shell': patch
79
---
810

911
feat(spec): a hook declares its refusal catalogue, and its body refuses by code, answered in the caller's language
@@ -13,10 +15,11 @@ Metadata-protocol: added
1315

1416
A hook can now declare `refusals`: one entry per SCREAMING_SNAKE code, each with the `message` the user reads and an optional 4xx `status` (default `400`). A sandboxed `body` then refuses by code only: it throws an error carrying a declared `code`, and the body runner answers with the entry. The message is translated through `hooks.<hook>.refusals.<CODE>.message` in the caller's locale, the code reaches the caller's error envelope as `declaredCode` (beside the status's registered `code`), and the status is the entry's. The body's own error text and `status` are not what the caller reads.
1517

16-
- **Spec.** `HookSchema.refusals` (`HookRefusalSchema`: `message`, `status?`). A new `hooks` group on the translation bundle, `hooks.<hook>.refusals.<CODE>.message`, and `hookRefusalMessageKey`. `HookContext.session.locale` declares the caller's locale.
17-
- **Undeclared codes are refused loudly.** A code the body throws literally (`{ code: 'X' }`, or `e.code = 'X'; throw e`) that the catalogue does not declare fails the save at `body.source`. Any other undeclared code, and a throw with no code, is a `500` fault at run time that names the hook and the code. This applies only to a hook that declares `refusals`; a hook without one is answered exactly as before.
18+
- **Spec.** `HookSchema.refusals` (`HookRefusalSchema`: `message`, `status?`). A new `hooks` group on the translation bundle, `hooks.<hook>.refusals.<CODE>.message`, and `hookRefusalMessageKey`. A message is plain text: a `{{ }}` hole is refused at save, so a later parameter slot stays additive. `HookContext.session.locale` declares the caller's locale.
19+
- **Undeclared codes are refused loudly.** A code the body throws literally (`{ code: 'X' }`, or `e.code = 'X'; throw e`) that the catalogue does not declare fails the save at `body.source`. Any other undeclared code, and a throw with no code, is a `500` fault at run time that names the hook and the code. A catalogue on a hook with no sandboxed `js` body (an in-process `handler`, an expression body) is refused at save, since nothing would answer it. All of this applies only to a hook that declares `refusals`; a hook without one is answered exactly as before.
1820
- **Runtime.** The body runner resolves the throw against the catalogue and picks the translation through the `i18n` service, negotiated against the locales it holds. An error a `ctx.api` call raised that the body lets leave unchanged, a crash, and the sandbox's own faults pass through untouched. ObjectQL copies the request's locale onto the hook session.
1921
- **CLI.** `os i18n extract` scaffolds one key per declared entry and `os i18n check` counts them, in a new `hook` coverage bucket.
2022
- **Lint.** A translation keyed to a hook the stack does not define, or to a code its `refusals` does not declare, is reported as `translation-target-unknown`.
23+
- **Console.** The translation preview shows a `hooks` category (en/zh labels), and the translation loader lists `hooks` among the spec groups.
2124

2225
Nothing to do for existing hooks. To use it, declare `refusals` on a sandboxed hook and throw its codes.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ L2 sandboxed JS body — runs inside an isolated VM with declared capabilities
8484

8585
| Property | Type | Required | Description |
8686
| :--- | :--- | :--- | :--- |
87-
| **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; no `{{ }}` holes are filled |
87+
| **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 |
8888
| **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 |
8989

9090

@@ -179,7 +179,7 @@ L2 sandboxed JS body — runs inside an isolated VM with declared capabilities
179179

180180
| Property | Type | Required | Description |
181181
| :--- | :--- | :--- | :--- |
182-
| **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; no `{{ }}` holes are filled |
182+
| **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 |
183183
| **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 |
184184

185185

‎packages/spec/api-surface/data.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@
375375
"GroupByNode (type)",
376376
"GroupByNodeSchema (const)",
377377
"HOOK_REFUSAL_CODE_PATTERN (const)",
378+
"HOOK_REFUSAL_MESSAGE_HOLE_REFUSED (const)",
379+
"HOOK_REFUSAL_MESSAGE_WITHOUT_HOLE (const)",
378380
"Hook (type)",
379381
"HookApi (interface)",
380382
"HookBody (type)",
@@ -907,6 +909,7 @@
907909
"hasNulInLikePattern (function)",
908910
"hookForm (const)",
909911
"hookRefusalCodesThrownIn (function)",
912+
"hookRefusalsWithoutSandboxedBody (function)",
910913
"importMappingEntryTargets (function)",
911914
"indexImportMappingTargets (function)",
912915
"injectedSystemColumnDefs (function)",

‎packages/spec/export-origins/data.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@
366366
"GroupByNode": "src/data/query.zod.ts#GroupByNode (type)",
367367
"GroupByNodeSchema": "src/data/query.zod.ts#GroupByNodeSchema (const)",
368368
"HOOK_REFUSAL_CODE_PATTERN": "src/data/hook-refusal-codes.ts#HOOK_REFUSAL_CODE_PATTERN (const)",
369+
"HOOK_REFUSAL_MESSAGE_HOLE_REFUSED": "src/data/hook-refusal-codes.ts#HOOK_REFUSAL_MESSAGE_HOLE_REFUSED (const)",
370+
"HOOK_REFUSAL_MESSAGE_WITHOUT_HOLE": "src/data/hook-refusal-codes.ts#HOOK_REFUSAL_MESSAGE_WITHOUT_HOLE (const)",
369371
"Hook": "src/data/hook.zod.ts#Hook (type)",
370372
"HookApi": "src/data/hook-api.ts#HookApi (interface)",
371373
"HookBody": "src/data/hook-body.zod.ts#HookBody (type)",
@@ -893,6 +895,7 @@
893895
"hasNulInLikePattern": "src/data/filter.zod.ts#hasNulInLikePattern (function)",
894896
"hookForm": "src/data/hook.form.ts#hookForm (const)",
895897
"hookRefusalCodesThrownIn": "src/data/hook-refusal-codes.ts#hookRefusalCodesThrownIn (function)",
898+
"hookRefusalsWithoutSandboxedBody": "src/data/hook-refusal-codes.ts#hookRefusalsWithoutSandboxedBody (function)",
896899
"importMappingEntryTargets": "src/data/import-mapping-target.ts#importMappingEntryTargets (function)",
897900
"indexImportMappingTargets": "src/data/import-mapping-target.ts#indexImportMappingTargets (function)",
898901
"injectedSystemColumnDefs": "src/data/injected-system-column-provenance.ts#injectedSystemColumnDefs (function)",

‎packages/spec/liveness/hook.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"evidence": "packages/runtime/src/sandbox/hook-refusal-catalogue.ts#resolveHookRefusal (the refusal carries the declared `status`, else 400; the status the body set on its own throw is not read)"
105105
}
106106
},
107-
"note": "[#26170, maintainer ruling 6431311681 letter A] Seeded `live` together with its reader: the key, the body runner that answers it, the translation group and the coverage count land in one change (Prime Directive 10). Read on the sandboxed `body` path only; an in-process `handler` is answered as before (the build lowers an inline handler to a `body`, which is the path a served runtime runs). An error a host call raised and the body let through unchanged, a nested hook's refusal, a crash and a sandbox fault pass through untouched. DRILLED to the entry's two keys: the entry face is closed (`.strict()`)."
107+
"note": "[#26170, maintainer ruling 6431311681 letter A] Seeded `live` together with its reader: the key, the body runner that answers it, the translation group and the coverage count land in one change (Prime Directive 10). Read on the sandboxed `body` path only, so the schema refuses `refusals` on a hook with no `js` body (an in-process `handler`, an expression body): a catalogue nothing would answer is never saved. An error a host call raised and the body let through unchanged, a nested hook's refusal, a crash and a sandbox fault pass through untouched. DRILLED to the entry's two keys: the entry face is closed (`.strict()`)."
108108
},
109109
"label": {
110110
"status": "live",

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,33 @@ export function undeclaredHookRefusalCodeAtSave(code: string, declared: readonly
180180
);
181181
}
182182

183+
/**
184+
* A refusal message with no `{{ … }}` hole. Nothing fills one today (a declared
185+
* message is plain text, translated whole), so a hole is refused at save: a
186+
* later params slot then stays a widening instead of re-reading text authors
187+
* already saved with braces in it. A pattern, not a refinement, so the
188+
* published JSON Schema refuses the same text.
189+
*/
190+
export const HOOK_REFUSAL_MESSAGE_WITHOUT_HOLE = /^(?![\s\S]*\{\{[^{}]*\}\})[\s\S]*$/;
191+
192+
/** The save-time refusal for a `{{ … }}` hole in a refusal message. */
193+
export const HOOK_REFUSAL_MESSAGE_HOLE_REFUSED =
194+
'A hook refusal `message` is plain text and nothing fills a `{{ }}` hole: the caller would read the braces as '
195+
+ 'written. Write the sentence without the hole (a refusal carries no parameters yet).';
196+
197+
/**
198+
* The save-time refusal for a catalogue on a hook with no sandboxed `js` body:
199+
* the catalogue is answered only on that path.
200+
*/
201+
export function hookRefusalsWithoutSandboxedBody(language: string | undefined): string {
202+
const has = language === undefined ? 'no `body`' : `a \`${language}\` body`;
203+
return (
204+
`\`refusals\` is answered only for a sandboxed \`body\` (\`language: 'js'\`), and this hook has ${has}: `
205+
+ 'an in-process `handler` or an expression body would never serve the catalogue, while `os i18n` still '
206+
+ 'demanded its translations. Give the hook a `js` body that throws the declared codes, or drop `refusals`.'
207+
);
208+
}
209+
183210
/**
184211
* The run-time refusal for a body throw the catalogue cannot answer: a code it
185212
* does not declare, or no code at all. Fault text, for the server log and the

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { describe, it, expect } from 'vitest';
22
import { HookSchema, HookRefusalSchema, HookContextSchema } from './hook.zod';
33
import {
44
HOOK_REFUSAL_CODE_PATTERN,
5+
HOOK_REFUSAL_MESSAGE_HOLE_REFUSED,
56
hookRefusalCodesThrownIn,
7+
hookRefusalsWithoutSandboxedBody,
68
undeclaredHookRefusalAtRun,
79
undeclaredHookRefusalCodeAtSave,
810
} from './hook-refusal-codes';
@@ -36,13 +38,40 @@ describe('Hook.refusals — the declared catalogue', () => {
3638
});
3739

3840
it('refuses a code that is not SCREAMING_SNAKE', () => {
39-
const r = HookSchema.safeParse({ ...base, handler: 'h', refusals: { creditLimit: { message: 'x' } } });
41+
const r = HookSchema.safeParse({ ...base, body: body('return;'), refusals: { creditLimit: { message: 'x' } } });
4042
expect(r.success).toBe(false);
4143
expect(r.error!.issues.some((i) => i.path[0] === 'refusals')).toBe(true);
4244
});
4345

4446
it('refuses an empty message', () => {
45-
expect(HookSchema.safeParse({ ...base, handler: 'h', refusals: { NO: { message: ' ' } } }).success).toBe(false);
47+
expect(HookSchema.safeParse({ ...base, body: body('return;'), refusals: { NO: { message: ' ' } } }).success).toBe(false);
48+
});
49+
50+
it.each([
51+
['an in-process `handler`', { handler: 'creditGuard' }, 'no `body`'],
52+
['an expression body', { body: { language: 'expression', source: 'record.amount > 0' } }, 'a `expression` body'],
53+
])('refuses a catalogue on a hook with %s — nothing would answer it', (_label, rest, has) => {
54+
const r = HookSchema.safeParse({ ...base, ...rest, refusals: { CREDIT_LIMIT: { message: 'x' } } });
55+
expect(r.success).toBe(false);
56+
const issue = r.error!.issues.find((i) => i.path.join('.') === 'refusals')!;
57+
expect(issue.message).toBe(hookRefusalsWithoutSandboxedBody(has === 'no `body`' ? undefined : 'expression'));
58+
expect(issue.message).toContain(has);
59+
});
60+
61+
it.each(['Over the limit of {{limit}}.', 'Hi {{ user.name }}, no.', 'Line one.\nOver {{limit}}.'])(
62+
'refuses a `{{ }}` hole in %j — nothing fills it, and a params slot stays additive',
63+
(message) => {
64+
const r = HookSchema.safeParse({ ...base, body: body('return;'), refusals: { CREDIT_LIMIT: { message } } });
65+
expect(r.success).toBe(false);
66+
const issue = r.error!.issues.find((i) => i.path.join('.') === 'refusals.CREDIT_LIMIT.message')!;
67+
expect(issue.message).toBe(HOOK_REFUSAL_MESSAGE_HOLE_REFUSED);
68+
},
69+
);
70+
71+
it('accepts single braces and an unclosed pair as plain text', () => {
72+
for (const message of ['Use {x} here.', 'An open {{ brace.']) {
73+
expect(HookRefusalSchema.safeParse({ message }).success, message).toBe(true);
74+
}
4675
});
4776

4877
it.each([399, 500, 503, 404.5])('refuses status %s — a refusal is a 4xx', (status) => {

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import type { IScopedContext } from '../contracts/scoped-context';
3030
// reads too, so this schema's import graph is unchanged.
3131
import {
3232
HOOK_REFUSAL_CODE_PATTERN,
33+
HOOK_REFUSAL_MESSAGE_HOLE_REFUSED,
34+
HOOK_REFUSAL_MESSAGE_WITHOUT_HOLE,
3335
hookRefusalCodesThrownIn,
36+
hookRefusalsWithoutSandboxedBody,
3437
undeclaredHookRefusalCodeAtSave,
3538
} from './hook-refusal-codes';
3639

@@ -208,19 +211,30 @@ function refuseElevatedIdentityAssignmentBodyWrite(
208211
* author meets the refusal before any write does. One issue per code, at
209212
* `body.source`. A code that arrives any other way is the run-time refusal's.
210213
*
214+
* A catalogue is answered only on the sandboxed `body` path, so `refusals` on
215+
* a hook with no `js` body — an in-process `handler`, or an `expression` body
216+
* that cannot throw a code — is refused at `refusals`: the catalogue would be
217+
* demanded by `os i18n` and served by nobody.
218+
*
211219
* A hook that declares no `refusals` is untouched: its body's throws are
212220
* answered as they always were.
213221
*/
214222

215223
/**
216-
* The object-level check that refuses a code a hook's sandboxed body throws
217-
* literally and its `refusals` does not declare. See the block above.
224+
* The object-level check that refuses a catalogue nothing would answer, and a
225+
* code a hook's sandboxed body throws literally that its `refusals` does not
226+
* declare. See the block above.
218227
*/
219228
function refuseUndeclaredLiteralRefusalCode(
220229
hook: { refusals?: Record<string, unknown>; body?: { language?: string; source?: unknown } },
221230
ctx: z.RefinementCtx,
222231
): void {
223-
if (!hook.refusals || hook.body?.language !== 'js' || typeof hook.body.source !== 'string') return;
232+
if (!hook.refusals) return;
233+
if (hook.body?.language !== 'js') {
234+
ctx.addIssue({ code: 'custom', path: ['refusals'], message: hookRefusalsWithoutSandboxedBody(hook.body?.language) });
235+
return;
236+
}
237+
if (typeof hook.body.source !== 'string') return;
224238
const declared = Object.keys(hook.refusals);
225239
for (const code of hookRefusalCodesThrownIn(hook.body.source)) {
226240
if (Object.prototype.hasOwnProperty.call(hook.refusals, code)) continue;
@@ -240,7 +254,7 @@ function refuseUndeclaredLiteralRefusalCode(
240254
* The message is plain text, handed over verbatim in the source locale and
241255
* replaced whole by its translation, `hooks.<hook>.refusals.<CODE>.message` —
242256
* the validation-rule message's posture (`objects.<object>._validations.<rule>.message`):
243-
* an authored refusal has no parameter contract, so no `{{ }}` hole is filled.
257+
* an authored refusal has no parameter contract, so a `{{ }}` hole is refused at save.
244258
*/
245259
export const HookRefusalSchema = lazySchema(() => strictObject(
246260
{
@@ -256,9 +270,9 @@ export const HookRefusalSchema = lazySchema(() => strictObject(
256270
},
257271
},
258272
{
259-
message: z.string().trim().min(1).describe(
273+
message: z.string().trim().min(1).regex(HOOK_REFUSAL_MESSAGE_WITHOUT_HOLE, HOOK_REFUSAL_MESSAGE_HOLE_REFUSED).describe(
260274
'The sentence the user reads when the body throws this code — plain text in the source locale, translated '
261-
+ 'whole through hooks.<hook>.refusals.<CODE>.message in the caller\'s locale; no {{ }} holes are filled',
275+
+ 'whole through hooks.<hook>.refusals.<CODE>.message in the caller\'s locale; a {{ }} hole is refused',
262276
),
263277
status: z.number().int().min(400).max(499).optional().describe(
264278
'HTTP status the refusal is answered with (4xx; a refusal is never a fault). Absent: 400. A status the '
@@ -598,7 +612,7 @@ export const HookSchema = lazySchema(() => strictObject(
598612
*
599613
* Keyed by hook name, not by object: one hook bound to several objects, or
600614
* to `'*'`, owns one catalogue and one translation per code. Read by the
601-
* sandboxed `body` runner; an in-process `handler` is answered as before.
615+
* sandboxed `body` runner only, so it is refused on a hook with no `js` body.
602616
* Absent: the hook's throws are answered as they always were.
603617
*/
604618
refusals: z.record(

‎skills/objectstack-data/references/data-hooks.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,12 @@ body: { language: 'js', source: `
953953
` },
954954
```
955955

956-
- Codes are SCREAMING_SNAKE; the error envelope carries one as `declaredCode`.
957-
- The plain-text message is translated via `hooks.<hook>.refusals.<CODE>.message`
958-
in the caller's locale; the body's own error text and `status` are not used.
956+
- Codes are SCREAMING_SNAKE; the envelope carries one as `declaredCode`.
957+
- Refused at save without a `js` body (e.g. a bare `handler`).
958+
- The message is plain text (a `{{ }}` hole is refused), translated via
959+
`hooks.<hook>.refusals.<CODE>.message`; the body's own text and `status` are unused.
959960
- An undeclared code, or none, is refused: at save if thrown literally, else as
960-
a `500` fault. Unchanged `ctx.api` errors and crashes pass through.
961+
a `500`. Unchanged `ctx.api` errors and crashes pass through.
961962

962963
---
963964

0 commit comments

Comments
 (0)