Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
164789d
wip(spec): a hook declares the texts it writes into records
claude Nov 16, 2026
e04bbea
wip(runtime): ctx.text answers a hook's declared text in the organiza…
claude Nov 16, 2026
3b5e107
wip(#34374): hook texts — lint walk/orphan, liveness, docs, tests, ch…
claude Nov 16, 2026
a84f06c
wip(#34374): hook texts — lint/core tests, regenerated spec artifacts
claude Nov 16, 2026
c3f6ca9
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 16, 2026
484e3a0
test(#34374): type the engine options in the hook-text pin instead of…
claude Nov 16, 2026
35212cf
test(#34374): pin HookTextSchema as an isomorphic alias (ADR-0122)
claude Nov 16, 2026
7bebb37
docs(#34374): say how readOrganizationTextLocale reads beside readOrg…
claude Nov 17, 2026
0d7755f
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 17, 2026
a3a4bb5
chore(#34374): regenerate the reference index counts after merging main
claude Nov 17, 2026
d4e652a
chore(#34374): declare Clause-② yes (widening) beside Metadata-protoc…
claude Nov 17, 2026
aae87d7
test(#34374): the hook-text pin names DataEngineInsertOptions; hook s…
claude Nov 17, 2026
70a4381
test(#34374): the dropped-refinements ledger header counts the hook-t…
claude Nov 17, 2026
df9ac15
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 17, 2026
5b78c31
fix(#34374): a hook text's holes are compiled like a refusal's, and a…
claude Nov 17, 2026
aa82166
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 17, 2026
8929099
chore(#34374): regenerate the hook reference page and re-anchor texts…
claude Nov 17, 2026
5e33619
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 17, 2026
7b8d110
chore(#34374): regenerate the hook reference page and the authorable …
claude Nov 17, 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
25 changes: 25 additions & 0 deletions .changeset/34374-hook-texts-org-locale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
'@objectstack/spec': minor
'@objectstack/core': minor
'@objectstack/runtime': minor
'@objectstack/lint': minor
'@objectstack/service-automation': patch
---

feat(hooks): a hook writes the sentences it declares in the organization's language

Clause-②: yes (widening) — a hook may now declare `texts` and its code may call `ctx.text`; translation bundles accept `hooks.<hook>.texts.<key>.message`; `@objectstack/core` exports `readOrganizationTextLocale`. Nothing that was accepted before is refused
Metadata-protocol: added
Skills-docs: follow-up #34546

A hook can now declare the sentences it writes into records for people to read, and ask for one by key: `texts: { follow_up_subject: { message: 'Follow up with qualified lead: {{ lead }}', params: ['lead'] } }`, then `await ctx.text('follow_up_subject', { lead })`. The answer is in the organization's language, because everyone in the organization reads the record: the Localization setting (`localization.locale`) when somebody set it, else the app's declared `i18n.defaultLocale`. It is never the language of the person whose save fired the hook. The translation `hooks.<hook>.texts.<key>.message` is picked first, then its `{{ }}` holes are filled from `params`.

- A missing translation, or no language to pick, answers the declared sentence, and the hook goes on.
- A Localization setting that cannot be read rejects the call, so another language is never stored over a choice that may exist. A host that registers no `localization` setting has none set, so the app's declared default answers.
- Only declared text is translated: a string the hook builds itself is written as it is. A key `texts` does not declare is refused when the hook is saved if the hook asks for it literally (`ctx.text('key')`), otherwise when the hook runs, as a fault naming the hook and the key. `texts` on an `expression` body, or on a hook with neither a body nor a handler, is refused. A text with no `params` is plain text, and a `{{ }}` hole in it is refused.
- A text with `params` is compiled like a declared refusal's sentence: a hole over another name is refused when the hook is saved, and `objectstack validate`, `compile` and the publish gate refuse a hole holding logic, an unknown formatter, an empty hole and `{{ amount | currency }}` with no code. A translation whose holes do not render at run time is dropped for the declared sentence, filled.
- `ctx.text` is the same on a sandboxed `js` body and on an in-process `handler`.
- `os i18n extract` scaffolds the key, and the existing coverage check counts it. `objectstack validate` refuses a `hooks.<hook>.texts.<key>` translation over a key the hook does not declare.
- Text is picked when it is written. Records already written are not changed.

`@objectstack/core` exports `readOrganizationTextLocale`: the one reading of the organization's language that every writer of such text shares, with an unregistered Localization setting answered as not set. The flow record write (`@objectstack/service-automation`) now reads through it; its answers are unchanged.
1 change: 1 addition & 0 deletions content/docs/automation/hook-bodies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ The script sees only what the surrounding `ctx` object exposes:
| `ctx.api.object(name).insert\|update\|delete` | Cross-object writes. | `api.write` |
| `ctx.title()` | This record's title — the object's `nameField`, **including when it is a `formula`** (evaluated server-side against the record already in hand: no extra read over the record's own columns, one read per lookup a title such as `record.book.title + ' · ' + record.reader.name` reads through). | none |
| `ctx.title('<lookup field>')` | The related record's title, through a lookup / master_detail / user / tree column. Costs one `findOne` (plus the lookups that record's own title reads through). | `api.read` |
| `ctx.text('<key>', params?)` | A sentence the hook declared under `texts`, in the **organization's** language (translated, holes filled from `params`), for a value it writes into a record. Hook bodies only. See [Writing a sentence people read](/docs/automation/hooks#writing-a-sentence-people-read--texts). | none |
| `ctx.crypto.randomUUID()` | UUID generation. | `crypto.uuid` |
| `ctx.log.{info,warn,error}` | Structured logging. | `log` |
| `ctx.email.send({ to, subject, text })` | Sends mail through the mail service, from the configured sender, as the calling user and under the caller's organization. An action or a job body only: a hook body cannot send mail, because a message cannot be taken back if the write that runs the hook rolls back. A hook whose body is granted `email.send` is refused when it is saved, validated, built or bound, and the Console does not offer the grant on a hook. Send mail on a record change from an action, a job or a flow. See [Email service](/docs/kernel/runtime-services/email-service#from-a-script-body). | `email.send` |
Expand Down
56 changes: 56 additions & 0 deletions content/docs/automation/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,62 @@ export const planGate = {
A hook that declares no `refusals` refuses exactly as before: the thrown
error's own `code`, `status` and message reach the caller.

## Writing a sentence people read — `texts`

A sentence a hook writes into a record — the subject of the follow-up task it
opens, the summary it logs — is read by the whole organization. **Declare it**
on the hook and ask for it by key: the runtime answers it in the
**organization's** language, never the language of the person whose save fired
the hook.

```typescript
export const leadFollowUp = {
name: 'lead_follow_up',
object: 'crm_lead',
events: ['afterUpdate'],
texts: {
follow_up_subject: { message: 'Follow up with qualified lead: {{ lead }}', params: ['lead'] },
},
handler: async (ctx) => {
if (ctx.input.status !== 'qualified' || ctx.previous?.status === 'qualified') return;
const subject = await ctx.text('follow_up_subject', { lead: ctx.input.name });
await ctx.api.object('crm_task').insert({ subject, related_to_lead: ctx.input.id });
},
};
```

- **The organization's language.** The Localization setting when somebody set
it (Setup → Localization), else the app's `i18n.defaultLocale`. A hook a
person's save fires still writes in the organization's language — the record
is data everyone reads, not a message to that person.
- **Translated first, filled after.** `ctx.text` picks
`hooks.<hook>.texts.<key>.message` in that language, then fills the `{{ }}`
holes from the `params` you pass. A text that declares `params` may write
`{{ name }}` holes over those names, with an optional known formatter; one
that declares none is plain text, and a hole in it is refused when the hook
is saved. A hole over another name is refused at save; logic, an unknown
formatter, an empty hole and `{{ amount | currency }}` with no code are
refused by `os validate`, `compile` and the publish gate.
- **A missing translation never fails the hook.** With no translation, or no
language to pick, the declared `message` is written. A translation whose
holes do not render is dropped for the declared `message`, filled.
- **An unreadable Localization setting rejects.** When the settings service
cannot be read, `ctx.text` rejects rather than write another language over
the organization's choice. Catch it if the hook should write anyway.
- **Only declared text is translated.** A string the hook builds itself is
written as it is. A key `texts` does not declare is refused when the hook is
saved if the hook names it literally in `ctx.text('key')`, otherwise when the
hook runs, as a `500` that names the hook and the key.
- **It works whichever way the hook is written** — a TypeScript `handler` and a
sandboxed `js` body get the same `ctx.text`. An `expression` body cannot ask
for one, so `texts` is refused there.
- **Translated by key.** `os i18n extract` scaffolds
`hooks.<hook>.texts.<key>.message` and `os i18n check` counts it. See
[Translations](/docs/ui/translations).

Rows a hook wrote before it declared the text keep their text: nothing is
rewritten.

## Recipe — refuse overlapping time ranges

Leave and overtime requests share one rule: two live records of the same owner
Expand Down
2 changes: 1 addition & 1 deletion content/docs/references/api/protocol.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ The published metadata item body, opaque by ruling (1C). Shape is the item's own
| **reports** | `Record<string, { label?: string; description?: string; blocks?: Record<string, object> }>` | optional | Report translations keyed by report name |
| **pages** | `Record<string, { label?: string; description?: string; title?: string; subtitle?: string; … }>` | optional | Page translations keyed by page name |
| **flows** | `Record<string, { label?: string; successMessage?: string; errorMessage?: string; screens?: Record<string, object>; … }>` | optional | Screen-flow translations keyed by flow name |
| **hooks** | `Record<string, { refusals?: Record<string, object> }>` | optional | Hook translations keyed by hook name |
| **hooks** | `Record<string, { refusals?: Record<string, object>; texts?: Record<string, object> }>` | optional | Hook translations keyed by hook name |
| **positions** | `Record<string, { label?: string; description?: string }>` | optional | Position translations keyed by position name |
| **permissions** | `Record<string, { label?: string; description?: string }>` | optional | Permission-set translations keyed by permission-set name |
| **books** | `Record<string, { label?: string; description?: string; groups?: Record<string, object> }>` | optional | Doc-book translations keyed by book name |
Expand Down
26 changes: 23 additions & 3 deletions content/docs/references/data/hook.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Hook schema — Data Protocol property reference
navTitle: Hook
description: "Hook schemas of the ObjectStack Data Protocol: Hook, HookContext, HookEvent, HookRefusal — each property with its type, default and a TypeScript example."
description: "Hook schemas of the ObjectStack Data Protocol: Hook, HookContext, HookEvent and 2 more — each property with its type, default and a TypeScript example."
---

{/* ⚠️ AUTO-GENERATED — DO NOT EDIT. Run build-docs.ts to regenerate. Hand-written docs live in the module folders under content/docs/. */}
Expand All @@ -13,8 +13,8 @@ description: "Hook schemas of the ObjectStack Data Protocol: Hook, HookContext,
## TypeScript Usage

```typescript
import { HookSchema, HookContextSchema, HookEvent, HookRefusalSchema } from '@objectstack/spec/data';
import type { Hook, HookContext, HookRefusal } from '@objectstack/spec/data';
import { HookSchema, HookContextSchema, HookEvent, HookRefusalSchema, HookTextSchema } from '@objectstack/spec/data';
import type { Hook, HookContext, HookRefusal, HookText } from '@objectstack/spec/data';

// Validate data
const result = HookSchema.parse(data);
Expand Down Expand Up @@ -43,6 +43,7 @@ const result = HookSchema.parse(data);
| **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; 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 |
| **texts** | `Record<string, { message: string; params?: string[] }>` | optional | Texts the hook writes into records, keyed by the snake_case key its code asks for with ctx.text(KEY, params): `{ message, params? }`. ctx.text answers the sentence in the organization's language (the Localization setting when somebody set it, else the app's i18n.defaultLocale), translated through hooks.`<hook>`.texts.`<key>`.message with the declared message as the fallback, its holes filled from params. A key not declared here is refused — at save when the hook asks for it literally, otherwise at run time |
| **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 @@ -89,6 +90,13 @@ L2 sandboxed JS body — runs inside an isolated VM with declared capabilities
| **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: `Hook.texts[string]`

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **message** | `string` | ✅ | The sentence the hook writes into a record — in the source locale, translated whole through hooks.`<hook>`.texts.`<key>`.message in the organization's language when the hook asks for it (ctx.text). With no params every `{{ }}` hole is refused; with params a hole is a path over one of them with an optional known formatter |
| **params** | `string[]` | optional | The names this text may fill: a `{{ NAME }}` or `{{ NAME \| formatter }}` hole renders the value the hook passes in ctx.text(KEY, `{ NAME: value }`). A hole over any other name is refused at parse; a hole holding logic, an unknown formatter, an empty hole and a currency formatter with no code are refused where the text is compiled (os validate, compile, the publish gate). Absent: the sentence is plain text and a `{{ }}` hole is refused |


---

Expand Down Expand Up @@ -201,3 +209,15 @@ L2 sandboxed JS body — runs inside an isolated VM with declared capabilities

---

## HookText

### Properties

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **message** | `string` | ✅ | The sentence the hook writes into a record — in the source locale, translated whole through hooks.`<hook>`.texts.`<key>`.message in the organization's language when the hook asks for it (ctx.text). With no params every `{{ }}` hole is refused; with params a hole is a path over one of them with an optional known formatter |
| **params** | `string[]` | optional | The names this text may fill: a `{{ NAME }}` or `{{ NAME \| formatter }}` hole renders the value the hook passes in ctx.text(KEY, `{ NAME: value }`). A hole over any other name is refused at parse; a hole holding logic, an unknown formatter, an empty hole and a currency formatter with no code are refused where the text is compiled (os validate, compile, the publish gate). Absent: the sentence is plain text and a `{{ }}` hole is refused |


---

Loading
Loading