Skip to content

Commit 0acd17d

Browse files
committed
fix(spec): drop the non-null ctx.text arm; pin the handler optional call; docs name ctx.text?.(
Claude-Session: https://claude.ai/code/session_01L6bhgX6mQpVU8yUysiRAFC Co-authored-by: Claude <noreply@anthropic.com>
1 parent 269566b commit 0acd17d

8 files changed

Lines changed: 71 additions & 58 deletions

File tree

‎.changeset/35512-text-call-shapes.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
'@objectstack/spec': minor
33
---
44

5-
fix(spec): the save door refuses an undeclared text key asked for as `ctx.text?.('k')` or `ctx.text!('k')`, as it already did for `ctx.text('k')`
5+
fix(spec): the save door refuses an undeclared text key asked for as `ctx.text?.('k')`, as it already did for `ctx.text('k')`
66

77
Clause-②: no (narrowing)
88
Metadata-protocol: narrowed
99

10-
**BREAKING** for a hook or a `type: 'script'` action that declares `texts` and whose code asks for a key `texts` does not declare through the optional-call or non-null shape.
10+
**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.
1111

12-
- FROM: `HookSchema` and `ActionSchema` refused a key asked for literally and not declared in `texts` only for the plain call `ctx.text('k')`. `await ctx.text?.('k')`, `ctx.text?.('k')`, `await ctx.text!('k')` and `ctx.text!('k')` with an undeclared key parsed green, and the runtime refused the key only when the hook or action ran.
13-
- TO: each of those shapes is refused at save with the sentence the plain call already gets, at `body.source` (or `handler` for an in-process hook handler). Whitespace around the operator is read too (`ctx.text ?. ('k')`, `ctx.text !('k')`); a `!` on the next line ends the statement and is not a call.
14-
- 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 every shape.
12+
- 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.
13+
- 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.
14+
- 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.
1515

1616
A key that arrives any other way (a variable, a computed name) is still judged when the code runs.
1717

‎content/docs/automation/hooks.mdx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,9 @@ export const leadFollowUp = {
497497
the organization's choice. Catch it if the hook should write anyway.
498498
- **Only declared text is translated.** A string the hook builds itself is
499499
written as it is. A key `texts` does not declare is refused when the hook is
500-
saved if the hook names it literally in `ctx.text('key')`, otherwise when the
501-
hook runs, as a `500` that names the hook and the key.
500+
saved if the hook names it literally in `ctx.text('key')` or
501+
`ctx.text?.('key')`, otherwise when the hook runs, as a `500` that names the
502+
hook and the key.
502503
- **It works whichever way the hook is written** — a TypeScript `handler` and a
503504
sandboxed `js` body get the same `ctx.text`. An `expression` body cannot ask
504505
for one, so `texts` is refused there.

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ function undeclaredRefusalReasonAtRun(
423423
* (parameter 4).
424424
*
425425
* The same two halves as a refusal code: a key the hook's source asks for
426-
* LITERALLY — `ctx.text('key'`, `ctx.text?.('key'` or `ctx.text!('key'` — and
427-
* `texts` does not declare is refused at save ({@link hookTextKeysAskedIn});
428-
* any other key is refused when the hook runs.
426+
* LITERALLY — `ctx.text('key'` or `ctx.text?.('key'` — and `texts` does not
427+
* declare is refused at save ({@link hookTextKeysAskedIn}); any other key is
428+
* refused when the hook runs.
429429
*/
430430

431431
/**
@@ -436,19 +436,22 @@ export const HOOK_TEXT_KEY_PATTERN = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/;
436436

437437
/**
438438
* `ctx.text('key'` — the call a hook's code asks for a declared text with — and
439-
* its optional-call `ctx.text?.('key'` and non-null `ctx.text!('key'` shapes,
440-
* which strict TypeScript reaches for because `HookContext.text` is optional.
441-
* All three ask for the key literally. A `!` on the next line is not the
442-
* non-null shape (it ends the statement), so it is read only on the same line.
439+
* its optional-call shape `ctx.text?.('key'`, which strict TypeScript reaches
440+
* for because `HookContext.text` is optional. Both ask for the key literally.
441+
*
442+
* A non-null `ctx.text!('key'` is not read: it is TypeScript with no
443+
* JavaScript form. A handler's `!` is erased before the save reads
444+
* `Function.prototype.toString()`, so it arrives as the plain call; in a `js`
445+
* body it is a syntax error, and the body never runs.
443446
*/
444447
const TEXT_CALL = new RegExp(
445-
String.raw`(?<![\w$.])ctx\s*\.\s*text(?:\s*\?\.|[ \t]*!)?\s*\(\s*(?<tquote>['"\x60])(?<key>[a-z][a-z0-9]*(?:_[a-z0-9]+)*)\k<tquote>`,
448+
String.raw`(?<![\w$.])ctx\s*\.\s*text(?:\s*\?\.)?\s*\(\s*(?<tquote>['"\x60])(?<key>[a-z][a-z0-9]*(?:_[a-z0-9]+)*)\k<tquote>`,
446449
'g',
447450
);
448451

449452
/**
450-
* The text keys a hook's source asks for LITERALLY — `ctx.text('key', …)`,
451-
* `ctx.text?.('key', …)` or `ctx.text!('key', …)` — in first-seen order:
453+
* The text keys a hook's source asks for LITERALLY — `ctx.text('key', …)` or
454+
* `ctx.text?.('key', …)` — in first-seen order:
452455
* the cases a save can decide. A key that arrives any other way (a variable,
453456
* a computed value) is left to the run-time refusal.
454457
* Comments and longer strings are blanked first, as for a refusal code.

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,11 @@ describe('Hook.texts — a key the source asks for literally is refused at save
105105
expect(hookTextKeysAskedIn(source)).toEqual(['follow_up_subject']);
106106
});
107107

108-
// The optional-call and non-null call shapes are literal asks too: strict
109-
// TypeScript reaches for them once `HookContext.text` is optional.
108+
// The optional-call shape is a literal ask too: strict TypeScript reaches for
109+
// it once `HookContext.text` is optional.
110110
const SHAPES = [
111111
['ctx.text?.(', (k: string) => `ctx.text?.('${k}')`],
112112
['await ctx.text?.(', (k: string) => `await ctx.text?.('${k}', { lead: ctx.input.name })`],
113-
['ctx.text!(', (k: string) => `ctx.text!('${k}')`],
114-
['await ctx.text!(', (k: string) => `await ctx.text!('${k}', { lead: ctx.input.name })`],
115113
] as const;
116114

117115
it.each(SHAPES)('refuses an undeclared key asked for as %s at save, with the same sentence', (_shape, ask) => {
@@ -128,9 +126,20 @@ describe('Hook.texts — a key the source asks for literally is refused at save
128126
expect(hookTextKeysAskedIn(source)).toEqual(['follow_up_subject']);
129127
});
130128

131-
it('reads the shapes with whitespace around the operator; CONTROL: a `!` on the next line ends the statement and is not read', () => {
132-
expect(hookTextKeysAskedIn("ctx.text ?. ('a_key'); ctx.text !('b_key');")).toEqual(['a_key', 'b_key']);
133-
expect(hookTextKeysAskedIn("ctx.text\n!('c_key');")).toEqual([]);
129+
it('reads the optional call with whitespace or a line break around the operator', () => {
130+
expect(hookTextKeysAskedIn("ctx.text ?. ('a_key'); ctx.text\n ?.('b_key');")).toEqual(['a_key', 'b_key']);
131+
});
132+
133+
it('refuses an inline handler asking ctx.text?.() for an undeclared key, at handler (the strict-TypeScript case)', () => {
134+
const handler = async (ctx: any) => { await ctx.text?.('follow_up_title'); };
135+
const r = HookSchema.safeParse({ ...base, handler, texts });
136+
expect(r.success).toBe(false);
137+
const issue = r.error!.issues.find((i) => i.path.join('.') === 'handler')!;
138+
expect(issue.message).toBe(undeclaredHookTextKeyAtSave('follow_up_title', ['follow_up_subject'], 'handler'));
139+
});
140+
141+
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', () => {
142+
expect(hookTextKeysAskedIn("ctx.text!('a_key');")).toEqual([]);
134143
});
135144

136145
it('a hook that declares no texts is not read for ctx.text calls (the run-time fault names it)', () => {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ function refuseUndeclaredLiteralRefusalCode(
337337
/**
338338
* [#34374, ruling 6913660232 parameter 7] The object-level check that refuses
339339
* `texts` nothing could ask for, and a key a hook's source asks for literally
340-
* (`ctx.text('key')`) that `texts` does not declare — the refusal catalogue's
341-
* two halves above, for the sentences a hook writes into a record.
340+
* (`ctx.text('key')` or `ctx.text?.('key')`) that `texts` does not declare —
341+
* the refusal catalogue's two halves above, for the sentences a hook writes
342+
* into a record.
342343
*/
343344
function refuseUndeclaredLiteralTextKey(
344345
hook: {
@@ -816,9 +817,8 @@ export const HookSchema = lazySchema(() => strictObject(
816817
* hook builds itself is written as it is (parameter 4).
817818
*
818819
* A key the hook's source asks for literally that `texts` does not declare
819-
* — `ctx.text('key')`, `ctx.text?.('key')` or `ctx.text!('key')` — is
820-
* refused at save; any other undeclared key is refused when the hook
821-
* runs. Served on every path a hook runs on — a sandboxed `js` body and an
820+
* — `ctx.text('key')` or `ctx.text?.('key')` — is refused at save; any
821+
* other undeclared key is refused when the hook runs. Served on every path a hook runs on — a sandboxed `js` body and an
822822
* in-process `handler` — and refused on a hook whose code cannot ask for one
823823
* (an `expression` body, or neither a body nor a handler). Keyed by hook
824824
* name, not by object. Absent: `ctx.text` has nothing to answer.
@@ -1940,9 +1940,9 @@ export const HookContextSchema = lazySchema(() => z.object({
19401940
*
19411941
* The runtime binds it on every path a hook runs on — a sandboxed `js` body
19421942
* and an in-process `handler` (`buildHookTextSurface`). The save-time check
1943-
* reads the literal spellings `ctx.text('key'`, `ctx.text?.('key'` and
1944-
* `ctx.text!('key'`, so a key asked for through an alias or a computed name
1945-
* is only judged when the hook runs.
1943+
* reads the literal spellings `ctx.text('key'` and `ctx.text?.('key'`, so a
1944+
* key asked for through an alias or a computed name is only judged when the
1945+
* hook runs.
19461946
*
19471947
* Runtime-only, like {@link api} and {@link notice}: a live function, so the
19481948
* RUNTIME schema is `z.unknown()` (JSON Schema `{}`) and the type is a static

‎packages/spec/src/migrations/entries/semantic/18.hook-action-text-key-optional-call-refused.ts‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33
import type { SemanticMigration } from '../../types.js';
44

55
// The D3 entry for the save door's literal `ctx.text` reader reading the
6-
// optional-call `ctx.text?.('key')` and non-null `ctx.text!('key')` shapes
7-
// (ruling 6913660232 parameter 7: a key asked for literally that `texts` does
8-
// not declare is refused at save). It narrows the hook's and the action's
9-
// accept set; no key is removed, so there is no tombstone and no D2
10-
// conversion: which sentence the author meant is a judgement about the hook.
6+
// optional-call shape `ctx.text?.('key')` (ruling 6913660232 parameter 7: a
7+
// key asked for literally that `texts` does not declare is refused at save).
8+
// It narrows the hook's accept set, the released half; the action half
9+
// (`ActionSchema.texts`) ships already narrowed. No key is removed, so there is
10+
// no tombstone and no D2 conversion: which sentence the author meant is a
11+
// judgement about the hook.
1112
export const entry: SemanticMigration = {
1213
id: 'hook-action-text-key-optional-call-refused',
1314
surface:
14-
'hook.body.source / hook.handler and action.body.source on a hook or a type: script action that declares '
15-
+ 'texts: a literal ctx.text?.(KEY) or ctx.text!(KEY) call, awaited or not, whose KEY texts does not declare',
15+
'hook.body.source / hook.handler on a hook that declares texts: a literal optional call ctx.text?.(KEY), '
16+
+ 'awaited or not, whose KEY texts does not declare. A type: script action\'s body.source is read the same '
17+
+ 'way from the release that introduces action texts, so no released action is affected',
1618
replacement:
1719
'Declare the key under texts.KEY with the sentence the record keeps, or ask for one of the declared keys. '
18-
+ 'The call shape is unchanged: ctx.text?.(KEY) and ctx.text!(KEY) stay legal for a declared key.',
20+
+ 'The optional call stays legal for a declared key.',
1921
reason:
20-
'A key a hook or an action asks for literally that its texts does not declare was refused at save only for '
21-
+ 'the plain call ctx.text(KEY). The optional-call and non-null shapes, which strict TypeScript reaches for '
22-
+ 'because HookContext.text is optional, passed the save, and the runtime refused the key only when the code '
23-
+ 'ran, as a fault naming the hook or action and the key. The save now refuses them with the same sentence, '
24-
+ 'at body.source (or handler). A key that arrives any other way (a variable, a computed name) is still '
25-
+ 'judged when the code runs. There is no mechanical rewrite: the code already failed at run time for that '
26-
+ 'key, and whether to declare it or ask for another is a judgement about the sentence.',
22+
'A key a hook asks for literally that its texts does not declare was refused at save only for the plain '
23+
+ 'call ctx.text(KEY). The optional call ctx.text?.(KEY), which strict TypeScript reaches for because '
24+
+ 'HookContext.text is optional, passed the save, and the runtime refused the key only when the hook ran, '
25+
+ 'as a fault naming the hook and the key. The save now refuses it with the same sentence, at body.source '
26+
+ 'or handler. A key that arrives any other way (a variable, a computed name) is still judged when the code '
27+
+ 'runs. There is no mechanical rewrite: the hook already failed at run time for that key, and whether to '
28+
+ 'declare it or ask for another is a judgement about the sentence.',
2729
acceptanceCriteria:
28-
'objectstack validate reports no issue at body.source or handler for any hook or action that declares texts, '
29-
+ 'and every key its code asks for through ctx.text, ctx.text?.( or ctx.text!( is a key of its texts.',
30+
'objectstack validate reports no issue at body.source or handler for any hook that declares texts, and '
31+
+ 'every key its code asks for through ctx.text or the optional call ctx.text?.( is a key of its texts.',
3032
};

‎packages/spec/src/ui/action-texts.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,12 @@ describe('Action.texts — a key the body asks for literally is refused at save
130130
expect(ActionSchema.safeParse({ ...base, body: body(source), texts: TEXTS }).success).toBe(true);
131131
});
132132

133-
// The optional-call and non-null call shapes are literal asks too, read by
134-
// the one key reader the hook door shares.
133+
// The optional-call shape is a literal ask too, read by the one key reader
134+
// the hook door shares. (An action's door reads `body.source` alone: it has
135+
// no handler path.)
135136
const SHAPES = [
136137
['ctx.text?.(', (k: string) => `ctx.text?.('${k}')`],
137138
['await ctx.text?.(', (k: string) => `await ctx.text?.('${k}', { subject: input.subject })`],
138-
['ctx.text!(', (k: string) => `ctx.text!('${k}')`],
139-
['await ctx.text!(', (k: string) => `await ctx.text!('${k}', { subject: input.subject })`],
140139
] as const;
141140

142141
it.each(SHAPES)('refuses an undeclared key asked for as %s at save, with the same sentence', (_shape, ask) => {

‎packages/spec/src/ui/action.zod.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,9 +1573,8 @@ const actionObject = () => strictObject({
15731573
* (parameter 4).
15741574
*
15751575
* A key the body asks for literally that `texts` does not declare —
1576-
* `ctx.text('key')`, `ctx.text?.('key')` or `ctx.text!('key')` — is
1577-
* refused at save; any other undeclared key is refused when the action
1578-
* runs, as a fault naming the action and the key. Served where an action
1576+
* `ctx.text('key')` or `ctx.text?.('key')` — is refused at save; any other
1577+
* undeclared key is refused when the action runs, as a fault naming the action and the key. Served where an action
15791578
* runs code of its own: a `type: 'script'` action's sandboxed `js` body —
15801579
* which is also what `os build` lowers an inline `target` function to.
15811580
* Refused on every other action: another `type` (its `target` is a URL,
@@ -2659,8 +2658,8 @@ export type ActionText = z.input<typeof ActionTextSchema>;
26592658
* [#34374 PR2] The object-level check on an action's `texts`, the twin of
26602659
* `HookSchema`'s: `texts` nothing the action runs could ask for is refused at
26612660
* `texts`, and a key the body's source asks for literally
2662-
* (`ctx.text('key')`) that `texts` does not declare is refused at
2663-
* `body.source`, one issue each. A key that arrives any other way is the
2661+
* (`ctx.text('key')` or `ctx.text?.('key')`) that `texts` does not declare is
2662+
* refused at `body.source`, one issue each. A key that arrives any other way is the
26642663
* run-time refusal's.
26652664
*/
26662665
function refuseUndeclaredLiteralActionTextKey(

0 commit comments

Comments
 (0)