Repository navigation
Commit 11e0441
feat(spec, runtime, lint, core): a hook writes the sentences it declares in the organization's language (ctx.text) (#34545)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9Qsbvr2NRYcFNgPCJ7PDjgK)_
Part of #34374
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
This is the **hook half** of #34374 (ruling 6913660232 option B,
parameter 7; split recorded on the card by `repo:objectstack#2`). The
**action half is left to PR2**: action-written text
(`objects.<obj>._actions.<act>.*` / `globalActions.<act>.*`, e.g.
HotCRM's `send_email` script action writing `'Email: ' + subject`) lands
after #34273 merges, reuses #34273's shared declared-entry factory,
starts from fresh main, and is the PR that closes the card. This PR does
not touch `action.zod.ts`.
## 维护者速读
- 改了什么:钩子可以声明自己写进记录给人读的句子(`texts`),运行时用 `await ctx.text('key', { … })`
取,按**组织语言**给出(Localization 设置有人设过就用它,否则应用的
`i18n.defaultLocale`),从不按触发保存的那个人的语言。
- 为什么改:HotCRM 的线索/工单钩子把「Follow up with qualified lead:
…」这类英文句子写进任务标题,中文、日文组织里大家读到的都是英文;裁定 B 第 7 项要求和已声明的钩子拒绝语同一规则。
- 风险与回滚:纯新增,未声明 `texts` 的钩子行为不变;缺译文写声明原文,不失败;只有 Localization
设置读不出来(非「未注册」)时那次 `ctx.text` 拒绝。回滚即 revert。
- 先看:`packages/runtime/src/sandbox/hook-text.ts`(取语言、选译文、填洞),然后
`packages/spec/src/data/hook.zod.ts` 的 `texts` 与保存期检查。
- 两分钟验证:`pnpm --filter @objectstack/runtime exec vitest run
src/sandbox/hook-text.integration.test.ts`,预期 11
条全过(返工后加了「译文渲染不了回落到声明句」在两条路径上各 1 条)。
- 需要你做的:无,席位落地;动作一半在 PR2(等 #34273 合并)。
## What changes
- **Spec — `HookSchema.texts`** (`packages/spec/src/data/hook.zod.ts`,
`hook-refusal-codes.ts`): a hook declares `texts: { <snake_case key>: {
message, params? } }`. `params` names the `{{ }}` holes, under the same
closed-scope rule a declared refusal's message follows; a text with no
`params` is plain and a hole in it is refused. Save-time checks: `texts`
on an `expression` body or on a hook with neither body nor handler is
refused at `texts`; a `ctx.text('<literal>')` key the hook does not
declare is refused at `body.source` / `handler` (comments and strings
are masked; a computed key is left to the run).
- **Spec — translation face** (`i18n-resolver.ts`,
`translation.zod.ts`): the key is `hooks.<hook>.texts.<key>.message`
(`hookTextMessageKey`); the translation bundle's `hooks.<hook>` group
accepts `texts`.
- **Core — one reading of the organization's language**
(`packages/core/src/organization-locale.ts`):
`readOrganizationTextLocale(getService, organizationId)` — the
Localization setting somebody set (`source !== 'default'`), else the
app's `i18n.defaultLocale`, else nothing. An unregistered Localization
namespace/key (`SETTINGS_UNKNOWN_NAMESPACE` / `SETTINGS_UNKNOWN_KEY`) is
"not set"; any other read failure propagates. The flow record write
(`service-automation`) used its own private copy of exactly this; it now
calls the shared one, answers unchanged.
- **Runtime — `ctx.text(key, params?)`**
(`packages/runtime/src/sandbox/hook-text.ts`, `body-runner.ts`,
`quickjs-runner.ts`, `script-runner.ts`, `app-artifact-handlers.ts`):
translate the template first (through the `i18n` service, negotiated as
refusals are), then fill the holes through the formula template engine
(`renderHookTemplate`, shared with the refusal filler, which keeps its
behaviour). A missing translation answers the declared `message`; a
translation whose holes do not render is dropped for the declared
`message`, filled, with a warning that says so; an undeclared key throws
a `SandboxError` naming the hook and the key. Same answer on a sandboxed
`js` body (installed on the VM's `ctx`) and an in-process `handler` (the
`withInProcessHookTitle` seam).
- **Lint** (`i18n-walk.ts`, `validate-translation-references.ts`,
`validate-hook-refusal-templates.ts`): `os i18n extract` scaffolds
`hooks.<hook>.texts.<key>.message` with the declared sentence;
`objectstack validate` refuses a `hooks.<hook>.texts.<key>` translation
over a key the hook does not declare (nearest key offered). The compile
half of a hook sentence (#33214) now judges `texts.<key>.message` for an
entry with `params` beside the refusal sentences: logic, an unknown
formatter, an empty hole and `currency` with no code are errors at
`objectstack validate`, `compile` and the publish gate's `hook` write.
- **Docs**: `automation/hooks.mdx` (new section),
`automation/hook-bodies.mdx` (`ctx.text` row), `ui/translations.mdx`
(key row).
- **Generated**: dropped-refinements baseline (the new `HookText`
object-level check and the `texts.valueType` sites), liveness rows
(`hook.texts`, `translation.hooks.texts`), and the `check:generated
--fix` outputs (api-surface, export-origins, declaration-map, reference
docs, strictness ledger, liveness counts, and the two generator-owned
`skills/*/references/_index.md` lines that list `HookTextSchema`).
Not changed: no backfill of rows already written (parameter 6); no
`org.locale` root (parameter 8); a string a hook builds itself is
written as it is (parameter 4).
## Tests
- **`packages/runtime/src/sandbox/hook-text.integration.test.ts`**
(new): the HotCRM lead follow-up shape on the real `ObjectQL` +
`SqlDriver` (better-sqlite3) + `QuickJSScriptRunner` + in-memory `i18n`,
on **both** the sandboxed body and the in-process handler path, every
assertion on the stored task row. An en-US caller in a zh-CN app writes
the zh-CN translation, holes filled; a ja-JP Localization setting
somebody set wins and is asked in the write's organization (CONTROL: the
`source: 'default'` placeholder does not count); no translation writes
the declared sentence filled; `SETTINGS_UNKNOWN_KEY` answers the app
default while `SETTINGS_STORE_UNAVAILABLE` rejects the update with no
task written; an undeclared key rejects naming hook and key; a
translation whose holes do not render writes the declared sentence
filled and warns so (CONTROL: a translation that renders, with a known
formatter, is written).
- **`packages/spec/src/data/hook-texts.test.ts`** (new): schema
acceptance, key pattern, hole rules, alias, expression-body / no-code
refusal, handler acceptance, literal undeclared key at `body.source` and
`handler`, masked-comment/string/computed-key CONTROL, run-time message,
translation face, key spelling.
- **`packages/core/src/organization-locale.test.ts`**:
`readOrganizationTextLocale` — set wins in the organization; no settings
/ placeholder → app default; unregistered namespace/key → app default;
other failure rejects; no lookup → nothing.
- **`packages/lint/src/validate-hook-refusal-templates.test.ts`**: a
hook text with `params` — CONTROLs accepted (a path, a known formatter,
`currency:EUR`, plain text); an unknown formatter, logic and an empty
hole refused as errors at `texts.<key>.message`; `currency` with no code
refused naming `currency:EUR`; a text with no `params` not read; a text
and a refusal on one hook each at its own path; part of the stack
expression pass; refused at the publish gate's `hook` write (CONTROL
passes).
- **`packages/lint/src/validate-translation-references.test.ts`**,
**`i18n-walk.test.ts`**: declared text passes; undeclared key refused
with suggestion; key on a hook with no `texts` refused; the walk yields
the key with the declared sentence from the `hook` bucket.
- **Forced test changes**:
`packages/metadata-protocol/src/protocol.meta-types-degenerate-derivation.test.ts`
(`hook` serves 24 top-level properties, was 23);
`packages/spec/src/system/metadata-form-zod-reconciliation.test.ts` (a
root ledger row for `hook.texts`);
`packages/spec/src/type-alias-convention.pin.test.ts` pins
`HookTextSchema` as an isomorphic alias (ADR-0122; no default, no
transform, beside `HookRefusalSchema`) and moves the pin count by one
(857 after merging main, which added two). No other existing test was
changed.
## Acceptance notes
- **Action half → PR2** (after #34273), as above.
- The Studio hook form (`hook.form.ts`) gets no `texts` row in this PR:
adding one needs its labels and help text translated in the eleven
metadata-forms bundles, which is outside this half's file surface. The
form↔Zod reconciliation records the omission as a root ledger row, owed
to a follow-up card. A hook authored in Studio can still carry `texts`
through its source. Filed: #34575.
- `ctx.text` is a pull API rather than a write-time marker because the
HotCRM hooks truncate and compose the sentence (both cut to 255
characters) before writing it.
- New public names, confirmed by the seat: `texts`, `ctx.text`,
`HookTextSchema`, `hookTextMessageKey`. `readOrganizationTextLocale` is
kept because its reading differs from main's
`readOrganizationLocaleSetting` (it is built on it, not beside it); its
JSDoc carries the side-by-side table below.
| the Localization setting answers… | `readOrganizationLocaleSetting` |
`readOrganizationTextLocale` |
|:--|:--|:--|
| set (`source: 'tenant'`, `ja-JP`) | `ja-JP` | `ja-JP` |
| the `source: 'default'` placeholder | `undefined` | the app default
(`zh-CN`) |
| no settings service | `undefined` | the app default (`zh-CN`) |
| `SETTINGS_UNKNOWN_KEY` | rejects | the app default (`zh-CN`) |
| `SETTINGS_STORE_UNAVAILABLE` | rejects | rejects |
Read by a probe at head over both exports (app default `zh-CN`,
organization `org_1`). The flow record write's private copy of the
second column (`service-automation`) is gone; the seed loader keeps the
first.
- Needs a contract-tier review (`Metadata-protocol: added`).
## Verification
Every heavy run went under `os-verify-lock`.
- Head 7b8d110 merges main d57d6ee through
`scripts/pm/os-regen-merge.sh` (merge 5e33619, then the regeneration
commit 7b8d110). `packages/spec/dropped-refinements.baseline.json`
conflicted only in its `measured` header (main 268 / 1116, this PR +1
schema / +6 sites) and was recomputed from the merged body, 269 / 1122;
`authorable-surface/data.json` and
`content/docs/references/data/hook.mdx` were regenerated from the merged
tree (`gen:schema`, `gen:docs`) rather than text-merged. After the
merge: `check:generated` 14/14; `@objectstack/spec` tests 848 files,
27168 passed; `@objectstack/lint` 214 files, 9107 passed; runtime
sandbox + app-artifact-handlers 52 files, 649 passed; spec, lint and
runtime `typecheck` exit 0 (runtime's first run was red on a stale local
`driver-turso` build, green after rebuilding it).
`check-governed-merges.mjs --pr 34545` at 7b8d110: "0 of 48 path(s) hit
the register after 2 generated-artifact lift(s) (13 surfaces govern
objectstack). ✅ NOT governed". Outside the baseline and the generated
docs and surface, this PR's diff against main is line-for-line the one
reviewed at 8929099 (`git diff 31096af 8929099` against `git diff
d57d6ee 7b8d110`, same changed lines).
- Head 8929099 is L1 rework round 1 (review 6937770388), plus a merge
of main 31096af:
- Item 1: `validate-hook-refusal-templates.ts` judges hook texts;
`hook-text.ts` falls back from a translation that does not render to the
declared `message`, filled. `@objectstack/lint`
`validate-hook-refusal-templates.test.ts` 24/24; runtime
`hook-text.integration.test.ts` + hook-refusal suites 49/49.
- Ablations (restored, `git diff` empty after each): the lint pass
skipping `texts` ⇒ 7 of 24 red (every refusal case, the stack pass and
the publish gate; the 4 CONTROLs and the no-`params` case stay green).
The run-time fallback writing the unrendered translation ⇒ 2 of 11 red
(the new case, both paths).
- `HookTextSchema.params`, its JSDoc, `automation/hooks.mdx` and the
changeset now say where each hole is refused (parse, or `os validate` /
`compile` / the publish gate). The liveness row `hook.texts.params` is
re-anchored at `renderHookTemplate`.
- Merge of main 31096af: the alias pin count resolves to 857 (main 854
→ 856, this PR +1), with both history lines; `gen:schema` and `gen:docs`
exit 0; `check:generated` failed `check:liveness` on its first run (the
`hook.texts.params` citation named the replaced filler) and passed 14/14
after the re-anchor above.
- Gates: `dispatch-gates --commands` derived 129 families at 8929099;
all 129 ran: 128 exit 0, and `check:skill-examples` exit 3 on a stale
`@objectstack/client` dist (nothing measured), exit 0 on its rerun after
`pnpm --filter @objectstack/client build`.
- `node scripts/pm/check-governed-merges.mjs --pr 34545` at 8929099, in
a built tree: "governed-surface predicate: 0 of 48 path(s) hit the
register after 2 generated-artifact lift(s) (13 surfaces govern
objectstack). ✅ NOT governed". Both `skills/*/references/_index.md`
lines read as a pure `gen:skill-refs` regeneration on this tree
(#11705).
- `@objectstack/runtime` sandbox + app-artifact-handlers suites: 10
files, 308 passed (the hook-text pin 9/9 again at head).
`@objectstack/core` `organization-locale.test.ts`: 10 passed.
`@objectstack/lint` walk + translation references: 255 passed.
`@objectstack/service-automation`: 286 files, 4076 passed.
`@objectstack/spec` hook texts + hook refusals: 113 passed.
- Typecheck (`tsc --noEmit`) of core, runtime, lint, service-automation:
exit 0. `pnpm --filter @objectstack/spec check:generated`: 14/14 green
after `--fix`; `check:test-typecheck` green.
- **Ablation** (`scripts/ablation-replace.mjs`, restored, blob == HEAD,
`git diff HEAD` empty): replace the translation pick in `hook-text.ts`
with `undefined` ⇒ 6 of 9 integration tests red (every
translated-language pin, both paths); the 3 that stay green are the
missing-translation fallback (both paths) and the undeclared-key
refusal, which read no translation.
- Head df9ac15 merges main 8dd8192.
`packages/spec/dropped-refinements.baseline.json` conflicted only in its
`measured` header (main moved sites 1090 → 1114, this PR 1090 → 1096);
the header was recomputed from the merged body, 267 schemas / 1120
sites, and `authorable-surface/data.json` was regenerated with
`gen:schema`. After the merge: `check:generated` exit 0;
`@objectstack/spec` local tests 848 files, 27148 passed; runtime sandbox
+ app-artifact-handlers 52 files, 647 passed; runtime and
service-automation `typecheck` exit 0 (runtime's first run was red on a
stale local `driver-turso` build, green after rebuilding it).
- Head 70a4381 fixes `Test Core (2/8)` at aae87d7:
`scripts/dropped-refinements.test.ts` checks that the ledger's
`measured` header totals equal its body. The hand-edited rows for
`HookText` and the four hook `texts.valueType` sites were added, but the
header still read 266 schemas / 1090 sites; it now reads 267 / 1096.
`@objectstack/spec` local project: 847 files, 1 failed before the fix
(this test), 27/27 in the file after.
- Head aae87d7 fixes three red CI checks at d4e652a:
- `Type Check · workspace`: the new pin imported `EngineInsertOptions`,
which `@objectstack/spec/data` does not export; it now names
`DataEngineInsertOptions`. `pnpm --filter @objectstack/runtime
typecheck` (with `check:test-typecheck`) exit 0; core, lint and
service-automation `typecheck` exit 0.
- `Test Core (2/8)`: `metadata-form-zod-reconciliation.test.ts` refused
`texts` as accepted by the Zod but not offered by the hook form. A root
ledger row (`omit`, `hook`, `texts`) now records why: the Studio row is
owed to a follow-up (see Acceptance notes). 76/76.
- `Test Core (6/8)`: `protocol.meta-types-degenerate-derivation.test.ts`
pins the top-level property count `/meta/types` serves; `hook` 23 → 24
for `texts`, with its history line. `@objectstack/metadata-protocol`:
355 files, 1 failed before the fix (this pin), 18/18 in the file after.
- Affected gates rerun: `check:query-options-erasure`,
`check:slot-lookup`, `check:test-source-alias`, `check:type-check-debt`
exit 0; `check:generated` exit 1 on a first run beside a concurrent
build and exit 0 on a second run.
- Head d4e652a: the changeset's `Clause-②` line now reads `yes
(widening)`, matching `Metadata-protocol: added` (CI `Check Changeset`
refused `no` beside `added`). `check-empty-changeset` and
`check:changeset-fixed` exit 0.
- Head a3a4bb5 merges main 107d2e8. GitHub read the PR as conflicting
because both sides moved the generated schema counts in
`content/docs/references/index.mdx` (this PR +1 in Data, main +1 in UI),
and GitHub does not use the repo's merge driver for generated artifacts.
The local merge applied that driver. Resolution: `gen:docs` on the
merged tree (1667 schemas: Data 188, UI 177), then `check:generated`
14/14 and `check:docs` exit 0. `check-governed-merges --pr 34545`: NOT
governed, and both `skills/*/references/_index.md` lines are a pure
`gen:skill-refs` regeneration on this tree (#11705).
- Head 7bebb37 (35212cf plus a JSDoc table on
`readOrganizationTextLocale` and the `Skills-docs` line in the
changeset): core `organization-locale.test.ts` 10 passed and `tsc
--noEmit` exit 0; the same 129-family derivation, of which the 26
changeset/docs/core families reran with exit 0. At 35212cf, merged with
main after branching from e8cba8f. Gates: `dispatch-gates --commands`
derived 129 families; all 129 ran with exit 0 at this head, and `--ran`
reports 129 run, 0 NOT-MEASURED, 0 UNRUN. On the first round
`check:query-options-erasure` caught two erased options bags in the new
pin (now typed) and `check:spec-parsed-alias` asked for the
`HookTextSchema` pin (added); the exit-3 prerequisites (shallow clone,
unbuilt `client-react` and other dists) were built and every family
rerun.
---
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01ADHkmNwjZuUekdsr5UH3GP
<!-- signed: repo:objectstack#2 · os-steve ·
session_01ADHkmRXuRn93HphKp7kXEz -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 77d7f52 commit 11e0441
48 files changed
Lines changed: 1492 additions & 120 deletions
File tree
- .changeset
- content/docs
- automation
- references
- api
- data
- system
- ui
- docs/audits/2026-07-unknown-key-strictness-ledger.counts
- packages
- core/src
- lint/src
- metadata-protocol/src
- runtime/src
- sandbox
- services/service-automation/src
- spec
- api-surface
- authorable-surface
- declaration-map
- export-origins
- json-schema.manifest
- liveness
- state-counts
- src
- data
- system
- skills
- objectstack-data/references
- objectstack-platform/references
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
453 | 453 | | |
454 | 454 | | |
455 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
456 | 512 | | |
457 | 513 | | |
458 | 514 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1876 | 1876 | | |
1877 | 1877 | | |
1878 | 1878 | | |
1879 | | - | |
| 1879 | + | |
1880 | 1880 | | |
1881 | 1881 | | |
1882 | 1882 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| |||
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
92 | 100 | | |
93 | 101 | | |
94 | 102 | | |
| |||
201 | 209 | | |
202 | 210 | | |
203 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
0 commit comments