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
23 changes: 23 additions & 0 deletions .changeset/34884-action-body-does-not-compile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
'@objectstack/runtime': patch
---

fix(lint, runtime)!: an action body that cannot compile in its `(async (input, ctx) => { … })` wrapper is refused at validate, build and publish, and at run time answers naming the action and the compile error

Clause-②: no (narrowing) — an L2 action body (`type: 'script'`, `body.language: 'js'`) that does not compile as the body of `(async (input, ctx) => { … })` passed `os validate`, `os build`, `os lint`, `os verify` and the runtime publish door (`action` and `object` writes); it is now refused there as `action-body-does-not-compile` at `error`, except a regular-expression literal the host engine refuses, which is a `warning`. Such a body never ran: every run failed with a SyntaxError before its first statement. `@objectstack/lint` also gains the exports `validateActionBodyCompiles`, `hostCompileFailure`, `ACTION_BODY_DOES_NOT_COMPILE`, `ACTION_BODY_PARAMETERS`, `actionBodyFunctionExpression` and two finding types on `.`, and `actionBodyFunctionExpression` on `./runtime`.
Metadata-protocol: narrowed
Skills-docs: follow-up owed — `skills/objectstack-data/references/data-hooks.md` teaches the action wrap `(async (input, ctx) => { … })` without saying a body must not declare `input` / `ctx` again; this change teaches it in `content/docs/ui/actions.mdx` and `content/docs/automation/hook-bodies.mdx`.

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

**BREAKING** only for an action body that never ran. The runtime compiles an L2 action's `body.source` as the statements of `(async (input, ctx) => { SOURCE })`, so a body that declares a wrapper parameter again (`const input = ctx.input || {}`, `let ctx = …`), or fails to compile there for any other reason, raises a SyntaxError before its first statement. Every authoring door accepted it: the parse check (`action-body-source-unparseable`) reads the source inside a different wrapper, and a parser reports no redeclaration. It deployed clean, and then every run of the action answered `500 INTERNAL_ERROR "Internal server error"` on the `/actions` door and MCP `run_action`. `action-body-does-not-compile` now refuses it at `os validate`, `os build`, `os lint` and the runtime publish door (`422 INVALID_METADATA`), naming the action, the compile error and its line of `body.source`. The publish door runs the rule on `object` writes too, so an object write cannot republish a refused body inline in `actions[]`. Hook and job bodies (`(async (ctx) => { … })`) are not judged. Measured first-party exposure: zero — `os validate` over the example apps reports no finding, and no skill or docs sample carries an action body that redeclares `input` or `ctx`.

**FROM → TO.** FROM `body: { language: 'js', source: 'const input = ctx.input || {}; return input.amount;' }` (accepted at every door, failing every run) TO `source: 'const params = ctx.input || {}; return params.amount;'` (or `return input.amount;`).

**Fix:** read the params as `input` (they are also `ctx.input`), or give the local another name (`const params = ctx.input || {}`); `var input = …` compiles. Fix any other compile error the finding names at the line it names. The semantic entry `action-body-does-not-compile-refused` carries the same notice into the upgrade guide and `os migrate meta`. `os explain action-body-does-not-compile` says what the rule judges and where the host's engine and the runtime's QuickJS differ.

The rule compiles the body inside the expression the runtime builds, from the same function (`actionBodyFunctionExpression`, which the QuickJS runner now builds its wrapper from, byte for byte as before), with the host's JavaScript engine and without running it.

At run time, an action whose body still does not compile (one deployed before this release, or syntax the host engine accepts and QuickJS does not) answers `500 INTERNAL_ERROR` on the `/actions` door and MCP `run_action` with the sentence `Action 'NAME' cannot run: its body does not compile (SyntaxError: …). The action's author must fix its body.source and publish it again.`, where it answered `500 INTERNAL_ERROR "Internal server error"`. The server log line reads `action 'NAME' does not compile: SyntaxError: …`. A body that crashes while running (a `TypeError`, a `SyntaxError` thrown by `JSON.parse`) still answers the sanitised `500`.
1 change: 1 addition & 0 deletions content/docs/api/error-catalog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ value directly (single wrap).
| Type has no server dispatch (`url`/`modal`/`form`/`api`), or a param-contract violation | **400** | `{ success: false, error: { message, code } }` |
| Data engine or automation service unavailable | **503** | `{ success: false, error: { message, code } }` |
| Handler **crashed** (`TypeError`, driver error, sandbox timeout) | **500** | `{ success: false, error: { message, code } }` |
| Sandboxed body **does not compile** in its `(async (input, ctx) => { … })` wrapper | **500** | `{ success: false, error: { message, code: 'INTERNAL_ERROR' } }` — `message` names the action and the compile error |

A rejection and a crash are told apart by the thrown error: a plain
`throw new Error(msg)`, a sandboxed body's deliberate throw, or a
Expand Down
1 change: 1 addition & 0 deletions content/docs/automation/hook-bodies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Static validation around a hook is asymmetric, and it is worth knowing exactly w
- **Checked — write side, literal-only.** Since [#4271](https://github.com/objectstack-ai/objectstack/issues/4271), `body.source` is **parsed** (never executed, never type-checked) and the field names it writes are resolved against the target object's declarations, which include every field an `objectExtensions` entry of the same artifact adds to it. An unknown field raises `hook-body-write-unknown-field` — a **warning** carrying a did-you-mean suggestion, which never blocks a build. Action bodies get the same check on their `ctx.api` writes (`action-body-write-unknown-field`). Both run under `os validate`, `os lint` and `os compile`. The *existence* question is advisory like this because the answer can depend on a package the build cannot see; the separate *writability* question — [writing a `readonly` field through `ctx.api`](#writing-a-readonly-field) — **does** gate, because both halves of that judgement are declared in the stack being checked.
- **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 literal that engine refuses is a warning, because its dialect measurably differs from the runtime's QuickJS. 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`.

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 @@ -887,7 +887,7 @@ os compile --json # JSON output for CI pipelines
→ Normalizing stack definition...
→ Lowering inline handlers...
→ Validating protocol compliance...
→ Running author-time rules (70)...
→ Running author-time rules (71)...
→ 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 @@ -519,6 +519,7 @@ orthogonal to both, and no cell here can carry it; it is written out in
| Print page blocks — a page that declares `print` holds only printable component types (`print-page-block-unprintable`, #22158) | ✓ | ✓ | ✓ | ✓ᵖ |
| 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) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
| 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 @@ -865,7 +866,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 (70)...
→ Running author-time rules (71)...
→ 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 (70)...
→ Running author-time rules (71)...

✗ Author-time rules failed (1 issue)
• stack · action 'resolve_ticket' visible: bare reference `status` — a
Expand Down
5 changes: 4 additions & 1 deletion content/docs/ui/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export default defineStack({

Self-contained logic ships inside the metadata and runs in the server sandbox
— signature `(input, ctx)`, with `ctx.api.object(name)` for data access, a
5-second default timeout, and declared `capabilities`:
5-second default timeout, and declared `capabilities`. `input` and `ctx` are the
wrapper's parameters, so a body must not declare them again: `const input = …`
does not compile, and `os validate` / `os build` / the publish door refuse it
(`action-body-does-not-compile`):

```typescript
export const MarkDoneAction = defineAction({
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 (70)...
→ Running author-time rules (71)...
→ Checking that every required capability has a provider installable in this edition...
→ Checking package docs (ADR-0046)...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ const CRASH_SAMPLES: Readonly<Record<string, string>> = {
...SAMPLES,
'formatErr(err)': 'TypeError: boom',
errStr: 'TypeError: boom',
// An action body that does not compile in its wrapper: the action's name and
// the compiler's sentence (`actionBodyCompileFailure`).
actionName: 'mz_lock_decide',
compilerError: 'SyntaxError: invalid redefinition of parameter name',
};

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

/**
* The one spelling of the wrapper an L2 action 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
* (`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.
*
* 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.
*
* Hook and job bodies run in a different wrapper, `(async (ctx) => { … })`, and
* are not described here.
*
* No imports: the runtime reaches this module through `@objectstack/lint/runtime`.
*/

/** The parameters an action body's wrapper binds, in call order: the params bag, then the context. */
export const ACTION_BODY_PARAMETERS = ['input', 'ctx'] as const;

/**
* The function expression an action body runs as: `(async (input, ctx) => { SOURCE })`.
*
* The source is placed after the opening brace on the expression's FIRST line,
* so line N of the source is line N of the expression; a compile error's line
* number therefore reads directly as a line of `body.source`.
*/
export function actionBodyFunctionExpression(source: string): string {
return `(async (${ACTION_BODY_PARAMETERS.join(', ')}) => { ${source} })`;
}
23 changes: 23 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 {
type MembershipKeyOwners,
} from './validate-membership-key-declarations.js';
import { validateRuleCompilability } from './validate-rule-compilability.js';
import { validateActionBodyCompiles } from './validate-action-body-compiles.js';
import { validateRuleSchemaFormats } from './validate-rule-schema-formats.js';
import { validateActionLocations } from './validate-action-locations.js';
import { lintFlowPatterns } from './lint-flow-patterns.js';
Expand Down Expand Up @@ -2888,6 +2889,28 @@ export const AUTHORING_RULES: readonly AuthoringRule[] = [
runtimeTypes: ['object', 'object_extension'],
run: (stack) => validateRuleCompilability(stack),
},
// An L2 action body that does not compile in the wrapper the runtime runs it
// in (`(async (input, ctx) => { SOURCE })`, built from the same function the
// rule compiles inside). Such a body passed every author-time door and then
// failed every run, on every door; `const input = ctx.input || {}` is the
// shape an AI author writes, a redeclared parameter. Gating: no reading of
// the metadata behaves as written, because the body never starts. A
// regular-expression literal the host refuses is the one `warning` arm (the
// two engines' regular-expression dialects measurably differ; see the
// module). Across the runtime publish gate for `action` writes and for
// `object` writes, whose inline `actions[]` the same walk reads; the
// differential charges a write only with the bodies it adds. The rule loads
// no dependency: compiling goes through the host's `node:vm`.
{
name: 'validateActionBodyCompiles',
tier: 'gating',
input: 'parsed',
commands: ALL,
source: 'packages/lint/src/validate-action-body-compiles.ts',
surfaces: CLI_AND_RUNTIME,
runtimeTypes: ['action', 'object'],
run: (stack) => validateActionBodyCompiles(stack),
},
// #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
11 changes: 11 additions & 0 deletions packages/lint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,17 @@ export type {
ActionBodyWriteExclusion,
} from './validate-action-body-writes.js';

// An action body must compile in the wrapper the runtime runs it in, which the
// runtime builds from `actionBodyFunctionExpression` — the same function the
// rule compiles inside, so the two cannot disagree about the wrapper.
export {
validateActionBodyCompiles,
hostCompileFailure,
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';

// 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
// implicit-field set so all three agree on what is writable without being
Expand Down
Loading
Loading