diff --git a/.changeset/35512-text-call-shapes.md b/.changeset/35512-text-call-shapes.md new file mode 100644 index 00000000000..b084e4a66de --- /dev/null +++ b/.changeset/35512-text-call-shapes.md @@ -0,0 +1,18 @@ +--- +'@objectstack/spec': minor +--- + +fix(spec): the save door refuses an undeclared text key asked for as `ctx.text?.('k')`, as it already did for `ctx.text('k')` + +Clause-②: no (narrowing) +Metadata-protocol: narrowed + +**BREAKING** for a hook that declares `texts` and whose code asks for a key `texts` does not declare through the optional call `ctx.text?.('k')`. Action `texts` is read the same way from the release that introduces it, so no released action is affected. + +- FROM: `HookSchema` refused a key asked for literally and not declared in `texts` only for the plain call `ctx.text('k')`. `ctx.text?.('k')` and `await ctx.text?.('k')` with an undeclared key parsed green, and the runtime refused the key only when the hook ran. +- TO: the optional call is refused at save with the sentence the plain call already gets, at `body.source` for a `js` body or at `handler` for an in-process handler. Whitespace or a line break around `?.` is read too. +- Fix: declare the key under `texts.KEY` with the sentence the record keeps, or ask for one of the declared keys. A declared key is accepted in both shapes. + +A key that arrives any other way (a variable, a computed name) is still judged when the code runs. + + diff --git a/content/docs/automation/hooks.mdx b/content/docs/automation/hooks.mdx index eea8c232784..b18e975b6ac 100644 --- a/content/docs/automation/hooks.mdx +++ b/content/docs/automation/hooks.mdx @@ -497,8 +497,9 @@ export const leadFollowUp = { 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. + saved if the hook names it literally in `ctx.text('key')` or + `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. diff --git a/packages/spec/src/data/hook-refusal-codes.ts b/packages/spec/src/data/hook-refusal-codes.ts index 0c2d935587f..c61afafd781 100644 --- a/packages/spec/src/data/hook-refusal-codes.ts +++ b/packages/spec/src/data/hook-refusal-codes.ts @@ -423,8 +423,9 @@ function undeclaredRefusalReasonAtRun( * (parameter 4). * * The same two halves as a refusal code: a key the hook's source asks for - * LITERALLY and `texts` does not declare is refused at save - * ({@link hookTextKeysAskedIn}); any other key is refused when the hook runs. + * LITERALLY — `ctx.text('key'` or `ctx.text?.('key'` — and `texts` does not + * declare is refused at save ({@link hookTextKeysAskedIn}); any other key is + * refused when the hook runs. */ /** @@ -433,16 +434,26 @@ function undeclaredRefusalReasonAtRun( */ export const HOOK_TEXT_KEY_PATTERN = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/; -/** `ctx.text('key'` — the call a hook's code asks for a declared text with. */ +/** + * `ctx.text('key'` — the call a hook's code asks for a declared text with — and + * its optional-call shape `ctx.text?.('key'`, which strict TypeScript reaches + * for because `HookContext.text` is optional. Both ask for the key literally. + * + * A non-null `ctx.text!('key'` is not read: it is TypeScript with no + * JavaScript form. A handler's `!` is erased before the save reads + * `Function.prototype.toString()`, so it arrives as the plain call; in a `js` + * body it is a syntax error, and the body never runs. + */ const TEXT_CALL = new RegExp( - String.raw`(?['"\x60])(?[a-z][a-z0-9]*(?:_[a-z0-9]+)*)\k`, + String.raw`(?['"\x60])(?[a-z][a-z0-9]*(?:_[a-z0-9]+)*)\k`, 'g', ); /** - * The text keys a hook's source asks for LITERALLY — `ctx.text('key', …)` — - * in first-seen order: the cases a save can decide. A key that arrives any - * other way (a variable, a computed value) is left to the run-time refusal. + * The text keys a hook's source asks for LITERALLY — `ctx.text('key', …)` or + * `ctx.text?.('key', …)` — in first-seen order: + * the cases a save can decide. A key that arrives any other way (a variable, + * a computed value) is left to the run-time refusal. * Comments and longer strings are blanked first, as for a refusal code. */ export function hookTextKeysAskedIn(source: string): string[] { diff --git a/packages/spec/src/data/hook-texts.test.ts b/packages/spec/src/data/hook-texts.test.ts index 893fe0b3e19..65c2583c7c4 100644 --- a/packages/spec/src/data/hook-texts.test.ts +++ b/packages/spec/src/data/hook-texts.test.ts @@ -105,6 +105,43 @@ describe('Hook.texts — a key the source asks for literally is refused at save expect(hookTextKeysAskedIn(source)).toEqual(['follow_up_subject']); }); + // The optional-call shape is a literal ask too: strict TypeScript reaches for + // it once `HookContext.text` is optional. + const SHAPES = [ + ['ctx.text?.(', (k: string) => `ctx.text?.('${k}')`], + ['await ctx.text?.(', (k: string) => `await ctx.text?.('${k}', { lead: ctx.input.name })`], + ] as const; + + it.each(SHAPES)('refuses an undeclared key asked for as %s at save, with the same sentence', (_shape, ask) => { + const r = HookSchema.safeParse({ ...base, body: body(`const s = ${ask('follow_up_title')};`), texts }); + expect(r.success).toBe(false); + const issue = r.error!.issues.find((i) => i.path.join('.') === 'body.source')!; + expect(issue.message).toBe(undeclaredHookTextKeyAtSave('follow_up_title', ['follow_up_subject'], 'body.source')); + }); + + it.each(SHAPES)('CONTROL: a declared key asked for as %s, and a computed key in that shape, pass the save', (_shape, ask) => { + const computed = ask('follow_up_subject').replace("'follow_up_subject'", 'k'); + const source = `${ask('follow_up_subject')};\nconst k = pick(); ${computed};`; + expect(HookSchema.safeParse({ ...base, body: body(source), texts }).success).toBe(true); + expect(hookTextKeysAskedIn(source)).toEqual(['follow_up_subject']); + }); + + it('reads the optional call with whitespace or a line break around the operator', () => { + expect(hookTextKeysAskedIn("ctx.text ?. ('a_key'); ctx.text\n ?.('b_key');")).toEqual(['a_key', 'b_key']); + }); + + it('refuses an inline handler asking ctx.text?.() for an undeclared key, at handler (the strict-TypeScript case)', () => { + const handler = async (ctx: any) => { await ctx.text?.('follow_up_title'); }; + const r = HookSchema.safeParse({ ...base, handler, texts }); + expect(r.success).toBe(false); + const issue = r.error!.issues.find((i) => i.path.join('.') === 'handler')!; + expect(issue.message).toBe(undeclaredHookTextKeyAtSave('follow_up_title', ['follow_up_subject'], 'handler')); + }); + + it('does not read a non-null ctx.text!() — TypeScript only: a handler reaches the save with the `!` erased, and a js body holding it never compiles', () => { + expect(hookTextKeysAskedIn("ctx.text!('a_key');")).toEqual([]); + }); + it('a hook that declares no texts is not read for ctx.text calls (the run-time fault names it)', () => { expect(HookSchema.safeParse({ ...base, body: body("await ctx.text('anything');") }).success).toBe(true); expect(undeclaredHookTextAtRun('lead_follow_up', 'anything', [])).toMatch(/hook 'lead_follow_up' asked for text 'anything'.*declared: none/); diff --git a/packages/spec/src/data/hook.zod.ts b/packages/spec/src/data/hook.zod.ts index 49b2f0174c7..22e986432ab 100644 --- a/packages/spec/src/data/hook.zod.ts +++ b/packages/spec/src/data/hook.zod.ts @@ -337,8 +337,9 @@ function refuseUndeclaredLiteralRefusalCode( /** * [#34374, ruling 6913660232 parameter 7] The object-level check that refuses * `texts` nothing could ask for, and a key a hook's source asks for literally - * (`ctx.text('key')`) that `texts` does not declare — the refusal catalogue's - * two halves above, for the sentences a hook writes into a record. + * (`ctx.text('key')` or `ctx.text?.('key')`) that `texts` does not declare — + * the refusal catalogue's two halves above, for the sentences a hook writes + * into a record. */ function refuseUndeclaredLiteralTextKey( hook: { @@ -816,8 +817,8 @@ export const HookSchema = lazySchema(() => strictObject( * hook builds itself is written as it is (parameter 4). * * A key the hook's source asks for literally that `texts` does not declare - * is refused at save; any other undeclared key is refused when the hook - * runs. Served on every path a hook runs on — a sandboxed `js` body and an + * — `ctx.text('key')` or `ctx.text?.('key')` — is refused at save; any + * other undeclared key is refused when the hook runs. Served on every path a hook runs on — a sandboxed `js` body and an * in-process `handler` — and refused on a hook whose code cannot ask for one * (an `expression` body, or neither a body nor a handler). Keyed by hook * name, not by object. Absent: `ctx.text` has nothing to answer. @@ -1939,8 +1940,9 @@ export const HookContextSchema = lazySchema(() => z.object({ * * The runtime binds it on every path a hook runs on — a sandboxed `js` body * and an in-process `handler` (`buildHookTextSurface`). The save-time check - * reads the literal spelling `ctx.text('key'`, so a key asked for through - * an alias or a computed name is only judged when the hook runs. + * reads the literal spellings `ctx.text('key'` and `ctx.text?.('key'`, so a + * key asked for through an alias or a computed name is only judged when the + * hook runs. * * Runtime-only, like {@link api} and {@link notice}: a live function, so the * RUNTIME schema is `z.unknown()` (JSON Schema `{}`) and the type is a static diff --git a/packages/spec/src/migrations/entries/semantic/18.hook-action-text-key-optional-call-refused.ts b/packages/spec/src/migrations/entries/semantic/18.hook-action-text-key-optional-call-refused.ts new file mode 100644 index 00000000000..bde170bccf7 --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/18.hook-action-text-key-optional-call-refused.ts @@ -0,0 +1,32 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// The D3 entry for the save door's literal `ctx.text` reader reading the +// optional-call shape `ctx.text?.('key')` (ruling 6913660232 parameter 7: a +// key asked for literally that `texts` does not declare is refused at save). +// It narrows the hook's accept set, the released half; the action half +// (`ActionSchema.texts`) ships already narrowed. No key is removed, so there is +// no tombstone and no D2 conversion: which sentence the author meant is a +// judgement about the hook. +export const entry: SemanticMigration = { + id: 'hook-action-text-key-optional-call-refused', + surface: + 'hook.body.source / hook.handler on a hook that declares texts: a literal optional call ctx.text?.(KEY), ' + + 'awaited or not, whose KEY texts does not declare. A type: script action\'s body.source is read the same ' + + 'way from the release that introduces action texts, so no released action is affected', + replacement: + 'Declare the key under texts.KEY with the sentence the record keeps, or ask for one of the declared keys. ' + + 'The optional call stays legal for a declared key.', + reason: + 'A key a hook asks for literally that its texts does not declare was refused at save only for the plain ' + + 'call ctx.text(KEY). The optional call ctx.text?.(KEY), which strict TypeScript reaches for because ' + + 'HookContext.text is optional, passed the save, and the runtime refused the key only when the hook ran, ' + + 'as a fault naming the hook and the key. The save now refuses it with the same sentence, at body.source ' + + 'or handler. A key that arrives any other way (a variable, a computed name) is still judged when the code ' + + 'runs. There is no mechanical rewrite: the hook already failed at run time for that key, and whether to ' + + 'declare it or ask for another is a judgement about the sentence.', + acceptanceCriteria: + 'objectstack validate reports no issue at body.source or handler for any hook that declares texts, and ' + + 'every key its code asks for through ctx.text or the optional call ctx.text?.( is a key of its texts.', +}; diff --git a/packages/spec/src/ui/action-texts.test.ts b/packages/spec/src/ui/action-texts.test.ts index 0b430117784..f33373c8143 100644 --- a/packages/spec/src/ui/action-texts.test.ts +++ b/packages/spec/src/ui/action-texts.test.ts @@ -130,6 +130,27 @@ describe('Action.texts — a key the body asks for literally is refused at save expect(ActionSchema.safeParse({ ...base, body: body(source), texts: TEXTS }).success).toBe(true); }); + // The optional-call shape is a literal ask too, read by the one key reader + // the hook door shares. (An action's door reads `body.source` alone: it has + // no handler path.) + const SHAPES = [ + ['ctx.text?.(', (k: string) => `ctx.text?.('${k}')`], + ['await ctx.text?.(', (k: string) => `await ctx.text?.('${k}', { subject: input.subject })`], + ] as const; + + it.each(SHAPES)('refuses an undeclared key asked for as %s at save, with the same sentence', (_shape, ask) => { + const r = ActionSchema.safeParse({ ...base, body: body(`const line = ${ask('email_body')};`), texts: TEXTS }); + expect(r.success).toBe(false); + const issue = issuesOf(r).find((i) => i.path.join('.') === 'body.source')!; + expect(issue.message).toBe(undeclaredActionTextKeyAtSave('email_body', ['email_subject'])); + }); + + it.each(SHAPES)('CONTROL: a declared key asked for as %s, and a computed key in that shape, pass the save', (_shape, ask) => { + const computed = ask('email_subject').replace("'email_subject'", 'k'); + const source = `${ask('email_subject')};\nconst k = pick(); ${computed};`; + expect(ActionSchema.safeParse({ ...base, body: body(source), texts: TEXTS }).success).toBe(true); + }); + it('CONTROL: an action that declares no texts is not read for ctx.text calls (the run-time fault names it)', () => { expect(ActionSchema.safeParse({ ...base, body: body("await ctx.text('anything');") }).success).toBe(true); expect(undeclaredActionTextAtRun('send_email', 'anything', [])).toMatch(/^action 'send_email' asked for text 'anything'.*declared: none/); diff --git a/packages/spec/src/ui/action.zod.ts b/packages/spec/src/ui/action.zod.ts index bd2687980dc..e804106fe59 100644 --- a/packages/spec/src/ui/action.zod.ts +++ b/packages/spec/src/ui/action.zod.ts @@ -1572,9 +1572,9 @@ const actionObject = () => strictObject({ * rejects the call. A string the body builds itself is written as it is * (parameter 4). * - * A key the body asks for literally that `texts` does not declare is - * refused at save; any other undeclared key is refused when the action - * runs, as a fault naming the action and the key. Served where an action + * A key the body asks for literally that `texts` does not declare — + * `ctx.text('key')` or `ctx.text?.('key')` — is refused at save; any other + * undeclared key is refused when the action runs, as a fault naming the action and the key. Served where an action * runs code of its own: a `type: 'script'` action's sandboxed `js` body — * which is also what `os build` lowers an inline `target` function to. * Refused on every other action: another `type` (its `target` is a URL, @@ -2658,8 +2658,8 @@ export type ActionText = z.input; * [#34374 PR2] The object-level check on an action's `texts`, the twin of * `HookSchema`'s: `texts` nothing the action runs could ask for is refused at * `texts`, and a key the body's source asks for literally - * (`ctx.text('key')`) that `texts` does not declare is refused at - * `body.source`, one issue each. A key that arrives any other way is the + * (`ctx.text('key')` or `ctx.text?.('key')`) that `texts` does not declare is + * refused at `body.source`, one issue each. A key that arrives any other way is the * run-time refusal's. */ function refuseUndeclaredLiteralActionTextKey(