Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions .changeset/34987-hook-job-body-does-not-compile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'@objectstack/lint': minor
'@objectstack/runtime': minor
'@objectstack/spec': minor
---

fix(lint)!: a hook body or job body that does not compile in the wrapper the runtime runs it in is refused

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: follow-up owed — skills/objectstack-data/references/data-hooks.md (its "Sandbox restrictions" section teaches what a body may contain and names no compile check)

<!-- adr-0087: registered hook-job-body-does-not-compile-refused -->

**BREAKING** only for a hook or job body that never ran. The runtime runs an L2 hook body and an L2 job body (`body.language: 'js'`) as the body of `(async (ctx) => { … })`. A source that does not compile there, such as `const ctx = ctx.input || {}` (the parameter declared again) or `let x = 1; let x = 2;`, raised a SyntaxError before its first statement on every run, yet passed `os validate`, `os build`, `os lint` and the runtime publish door: the hook parse check reads the source with a parser, which reports no redeclaration, and a job body had no check at all.

What moves (FROM → TO):

- New gating rule `validateHookAndJobBodyCompiles` (`@objectstack/lint`), rule ids `hook-body-does-not-compile` and `job-body-does-not-compile`: FROM no finding TO `error` at `os validate`, `os build`, `os lint`, and at the runtime publish door for `hook` writes (`422 INVALID_METADATA`). No door publishes a job, so a job body is refused by the three commands. A hook lowered from an inline `handler` is located at `hooks[N].handler`. The severity split is `action-body-does-not-compile`'s, read through the same `judgeBodyCompile`: a regular-expression pattern the host engine refuses is a `warning`, and every other compile failure is an `error`.
- `hookBodyFunctionExpression` and `HOOK_BODY_PARAMETERS` (`@objectstack/lint`, the first also on `@objectstack/lint/runtime`): the one spelling of the `(ctx)` wrapper. The runtime's QuickJS runner now builds the hook and job wrapper from it, byte for byte the wrapper it built before, so the check and the run cannot disagree about it.
- `judgeBodyCompile`, `BodyCompileVerdict` and `BodyCompileShape` (`@objectstack/lint`): the compile-and-classify core both rules share. `action-body-does-not-compile` reads it with unchanged findings.
- `os explain hook-body-does-not-compile` and `os explain job-body-does-not-compile` explain both rules.

The fix: use `ctx` as it is, or give the local another name (`const input = ctx.input || {}` in a hook, `const api = ctx.api` in a job); declare each name once; fix any other compile error at the line of `body.source` the finding names.

No conversion is made (ADR-0087 D3, entry `hook-job-body-does-not-compile-refused`): which name the author meant for the local is a judgement. Measured first-party exposure: zero.
1 change: 1 addition & 0 deletions content/docs/automation/hook-bodies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Static validation around a hook is asymmetric, and it is worth knowing exactly w
- **Checked — writes to a system column the object has no storage for.** Since [#8663](https://github.com/objectstack-ai/objectstack/issues/8663), a write to an injected system column is no longer exempted on the strength of its NAME alone. The registry injects `owner_id` / `organization_id` / the audit family onto an ADR-0015 [`external` object](/docs/data-modeling/external-datasources) exactly as onto a local one, but the remote database owns that schema and no column exists behind them. Writing one raises `hook-body-write-unprovisioned-anchor` (or `action-body-write-unprovisioned-anchor` / `flow-node-write-unprovisioned-anchor` on the other two surfaces) — a **warning** on all three, including the flow-node rule that otherwise gates, because the claim is about a remote schema the build cannot see. A column you **declare** yourself is untouched: on a federated object a declared `owner_id` maps a remote column you vouch for. Why it matters more than an ordinary typo: an undeclared name is refused upstream by the engine's own write-path validator (`INVALID_FIELD`), whereas the injected anchor is in the registered schema and passes it — so it is the one payload key that reaches the remote database raw, where a SQL remote aborts the **whole statement** with an untyped `no such column` and takes the correctly named fields of the same payload with it.
- **Checked — writes that reach nothing at all.** Since [#4345](https://github.com/objectstack-ai/objectstack/issues/4345), an action body assigning to `ctx.record` raises `action-record-write-discarded`, also a warning. This one is **not** a field-resolution question: an action's `ctx.record` is a snapshot the runtime never writes back, so the assignment is discarded whether or not the field is declared — see [Signature conventions](#signature-conventions) below.
- **Checked — an action body compiles in its wrapper.** An action body runs as the body of `(async (input, ctx) => { … })`, so `input` and `ctx` are parameters the body shares a scope with. A body that does not compile there — `const input = ctx.input || {}` redeclares a parameter, a syntax error — raises `action-body-does-not-compile`, an **error** under `os validate`, `os build`, `os lint` and the runtime publish door (`action` and `object` writes), naming the action and the compile error. Read the params as `input`, or name the local differently (`const params = ctx.input || {}`); `var input = …` compiles. The check compiles the body inside the same expression the runtime builds, without running it, with the host's JavaScript engine; a regular-expression pattern that engine refuses is a warning, because the two pattern dialects measurably differ (the runtime's QuickJS accepts duplicate named groups and modifier groups), while a literal that never closes on its line, or whose flags are not a flag set, is an error like any other compile failure. An action whose body still fails to compile at run time answers `500 INTERNAL_ERROR` with a sentence naming the action and the compile error, never a bare `Internal server error`.
- **Checked — a hook or job body compiles in its wrapper.** A hook body and a job body both run as the body of `(async (ctx) => { … })`: a job has no input to hand in, so `ctx` is its one parameter. A body that does not compile there — `const ctx = …` or `let ctx` redeclares the parameter, `let x = 1; let x = 2;` declares a name twice, or any grammar error — raises `hook-body-does-not-compile` or `job-body-does-not-compile`, an **error** under `os validate`, `os build` and `os lint`, naming the hook or job and the compile error; the runtime publish door refuses a `hook` write carrying one (no door publishes a job). Use `ctx` as it is, or give the local another name (`const input = ctx.input || {}`). The check is the action check's, over the hook's own wrapper: the same expression the runtime builds, compiled without running it, with the same warning for a regular-expression pattern the host's engine refuses. The parse check on a hook body (`hook-body-source-unparseable`) uses a parser, which reports no redeclaration, so it passes most bodies this check refuses.

Four literal write shapes are recognized, and only these:

Expand Down
2 changes: 1 addition & 1 deletion content/docs/deployment/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ os compile --json # JSON output for CI pipelines
→ Normalizing stack definition...
→ Lowering inline handlers...
→ Validating protocol compliance...
→ Running author-time rules (73)...
→ Running author-time rules (74)...
→ Checking that every required capability has a provider installable in this edition...
→ Collecting package docs (ADR-0046)... 0 collected
→ Writing artifact...
Expand Down
3 changes: 2 additions & 1 deletion content/docs/deployment/validating-metadata.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ orthogonal to both, and no cell here can carry it; it is written out in
| Page variable declarations — each name once and never blank, a `defaultValue` of its declared type, a `source` naming a component the page holds (#28663, #28725) | ✓ | ✓ | ✓ | ✓ᵖ |
| JSX / React page source parses (ADR-0080/0081) | ✓ | ✓ | ✓ | — |
| Script action bodies compile in the runtime's wrapper — an L2 action's `body.source` that does not compile as the body of `(async (input, ctx) => { … })`, most often `const input = …` redeclaring a parameter; an object write runs it over its inline `actions[]`, so an object write cannot republish a refused body (`action-body-does-not-compile`, #34884) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
| Hook and job bodies compile in the runtime's wrapper — an L2 hook or job `body.source` that does not compile as the body of `(async (ctx) => { … })`, most often `const ctx = …` redeclaring the parameter or one name declared twice; no door publishes a job, so the publish column is the `hook` write alone (`hook-body-does-not-compile`, `job-body-does-not-compile`, #34987) | ✓ | ✓ | ✓ | ✓ʰ |
| Approval-node approvers (ADR-0090 D3) | ✓ | ✓ | ✓ | ✓ᶠ |
| Security posture (ADR-0090 — e.g. every custom object declares `sharingModel`) | ✓ | ✓ | ✓ | ✓ˢᵖᵉᵇᵒ |
| Security vocabulary freeze (ADR-0090 D3 — the reserved word, replaced by `permission_set` / `position` / `business_unit`) | ✓ | ✓ | ✓ | ✓ᵒᵖᵉᵇᵖᵒᵃᵒˣ |
Expand Down Expand Up @@ -866,7 +867,7 @@ A clean run walks the registry and reports timing:
Config: /path/to/support-desk/objectstack.config.ts
Load time: 21ms
→ Validating against ObjectStack Protocol...
→ Running author-time rules (73)...
→ Running author-time rules (74)...
→ Checking that every required capability has a provider installable in this edition...
→ Checking package docs (ADR-0046)...

Expand Down
2 changes: 1 addition & 1 deletion content/docs/getting-started/build-with-claude-code.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ visible: 'status != "resolved"'
◆ Validate
────────────────────────────────────────
→ Validating against ObjectStack Protocol...
→ Running author-time rules (73)...
→ Running author-time rules (74)...

✗ Author-time rules failed (1 issue)
• stack · action 'resolve_ticket' visible: bare reference `status` — a
Expand Down
2 changes: 1 addition & 1 deletion content/docs/ui/react-pages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ objectstack validate
────────────────────────────────────────
→ Loading configuration...
→ Validating against ObjectStack Protocol...
→ Running author-time rules (73)...
→ Running author-time rules (74)...
→ Checking that every required capability has a provider installable in this edition...
→ Checking package docs (ADR-0046)...

Expand Down
45 changes: 30 additions & 15 deletions packages/lint/src/action-body-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* The one spelling of the wrapper an L2 action body runs in.
* The one spelling of each wrapper an L2 body runs in.
*
* The runtime does not run an action's `body.source` as a script of its own: it
* pastes the source into the body of an async arrow and calls that arrow with
* the action's params and its context
* The runtime does not run a body's `source` as a script of its own: it pastes
* the source into the body of an async arrow and calls that arrow
* (`packages/runtime/src/sandbox/quickjs-runner.ts`). So the source's
* statements share a scope with two bindings the author never wrote, and a
* source that is valid on its own can fail to compile there: declaring `input`
* or `ctx` again with `const` / `let` / `class` is a redeclaration of a
* parameter, a SyntaxError raised before any statement runs.
* statements share a scope with bindings the author never wrote, and a source
* that is valid on its own can fail to compile there: declaring a parameter
* again with `const` / `let` / `class` is a redeclaration, a SyntaxError raised
* before any statement runs.
*
* The runtime builds its wrapper from {@link actionBodyFunctionExpression}, and
* the author-time rule (`validate-action-body-compiles.ts`) compiles the source
* inside that same expression, so the check and the run cannot disagree about
* the wrapper. ⛔ Never spell the wrapper a second time anywhere: a copy is a
* second shape the next edit can move one side of.
* There are two wrappers. An action body takes the action's params and its
* context, `(async (input, ctx) => { … })`; a hook body and a job body take the
* context alone, `(async (ctx) => { … })` (a job has no input to hand in, and
* `JobSchema.body` documents `ctx` as its whole surface).
*
* Hook and job bodies run in a different wrapper, `(async (ctx) => { … })`, and
* are not described here.
* The runtime builds each wrapper from {@link actionBodyFunctionExpression} and
* {@link hookBodyFunctionExpression}, and the author-time rules
* (`validate-action-body-compiles.ts`, `validate-hook-body-compiles.ts`)
* compile the source inside those same expressions, so the check and the run
* cannot disagree about a wrapper. ⛔ Never spell a wrapper a second time
* anywhere: a copy is a second shape the next edit can move one side of.
*
* No imports: the runtime reaches this module through `@objectstack/lint/runtime`.
*/
Expand All @@ -37,3 +39,16 @@ export const ACTION_BODY_PARAMETERS = ['input', 'ctx'] as const;
export function actionBodyFunctionExpression(source: string): string {
return `(async (${ACTION_BODY_PARAMETERS.join(', ')}) => { ${source} })`;
}

/** The parameter a hook or job body's wrapper binds: the context. */
export const HOOK_BODY_PARAMETERS = ['ctx'] as const;

/**
* The function expression a hook body and a job body run as: `(async (ctx) => { SOURCE })`.
*
* The same one-line placement as {@link actionBodyFunctionExpression}: line N of
* the source is line N of the expression.
*/
export function hookBodyFunctionExpression(source: string): string {
return `(async (${HOOK_BODY_PARAMETERS.join(', ')}) => { ${source} })`;
}
21 changes: 21 additions & 0 deletions packages/lint/src/authoring-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ import {
} from './validate-membership-key-declarations.js';
import { validateRuleCompilability } from './validate-rule-compilability.js';
import { validateActionBodyCompiles } from './validate-action-body-compiles.js';
import { validateHookAndJobBodyCompiles } from './validate-hook-body-compiles.js';
import { validateRuleSchemaFormats } from './validate-rule-schema-formats.js';
import { validateActionLocations } from './validate-action-locations.js';
import { validateActionObjectBinding } from './validate-action-object-binding.js';
Expand Down Expand Up @@ -2965,6 +2966,26 @@ export const AUTHORING_RULES: readonly AuthoringRule[] = [
runtimeTypes: ['action', 'object'],
run: (stack) => validateActionBodyCompiles(stack),
},
// The same compile check over an L2 hook body and an L2 job body, in the
// wrapper the runtime runs both in (`(async (ctx) => { SOURCE })`, built from
// the same function the rule compiles inside). Gating for the action rule's
// reason: the body never starts. Same severity split, read through the one
// `judgeBodyCompile`; two ids (`hook-body-does-not-compile`,
// `job-body-does-not-compile`). Across the runtime publish gate for `hook`
// writes only: a hook is runtime-creatable, a job is not
// (`allowRuntimeCreate: false`), so no door publishes a job body, and no
// object write carries a hook (`hooks` is not an ObjectSchema key). The rule
// loads no dependency: compiling goes through the host's `node:vm`.
{
name: 'validateHookAndJobBodyCompiles',
tier: 'gating',
input: 'parsed',
commands: ALL,
source: 'packages/lint/src/validate-hook-body-compiles.ts',
surfaces: CLI_AND_RUNTIME,
runtimeTypes: ['hook'],
run: (stack, ctx) => validateHookAndJobBodyCompiles(stack, ctx),
},
// #5178 — the residual half of #5029, which registering `ajv-formats` does
// NOT close: under `strict: false` a MISSPELLED format name (`emial`) is
// logged once and DROPPED, so the rule compiles, ships, runs on every write
Expand Down
24 changes: 22 additions & 2 deletions packages/lint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,30 @@ export type {
export {
validateActionBodyCompiles,
hostCompileFailure,
judgeBodyCompile,
ACTION_BODY_DOES_NOT_COMPILE,
} from './validate-action-body-compiles.js';
export type { ActionBodyCompileFinding, HostCompileFailure } from './validate-action-body-compiles.js';
export { ACTION_BODY_PARAMETERS, actionBodyFunctionExpression } from './action-body-wrapper.js';
export type {
ActionBodyCompileFinding,
HostCompileFailure,
BodyCompileShape,
BodyCompileVerdict,
} from './validate-action-body-compiles.js';
export {
ACTION_BODY_PARAMETERS,
actionBodyFunctionExpression,
HOOK_BODY_PARAMETERS,
hookBodyFunctionExpression,
} from './action-body-wrapper.js';

// The same compile check over hook and job bodies, in their own `(ctx)`
// wrapper, which the runtime builds from `hookBodyFunctionExpression`.
export {
validateHookAndJobBodyCompiles,
HOOK_BODY_DOES_NOT_COMPILE,
JOB_BODY_DOES_NOT_COMPILE,
} from './validate-hook-body-compiles.js';
export type { HookBodyCompileFinding } from './validate-hook-body-compiles.js';

// The same write-set question on the third surface — a flow `update_record`
// node's structural `config.fields`. Shares the hook rule's field index and
Expand Down
Loading
Loading