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/35040-action-body-regexp-literal-token-refused.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
---

fix(lint)!: an action body whose regular-expression literal never closes, or whose flags are not a flag set, is refused, not only warned about

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — no published skill teaches the action-body compile check or regular-expression literals in an action body

<!-- adr-0087: registered action-body-regexp-literal-token-refused -->

**BREAKING** only for an action body that never ran. `action-body-does-not-compile` compiles an L2 action's `body.source` inside `(async (input, ctx) => { … })` with the host's JavaScript engine, and reported every failure whose sentence opens `Invalid regular expression` at `warning`, because the host's V8 and the runtime's QuickJS differ on some patterns. That sentence also opens V8's two token errors: a literal that never closes on its line (`return /abc;`, `return /[/;`, a literal broken by a line terminator: `Invalid regular expression: missing /`) and a flag set that is not one (`/a/gg`, `/a/zz`, `/a/uv`: `Invalid regular expression flags`). QuickJS refuses every one of them, so such a body passed `os validate`, `os build`, `os lint` and the runtime publish door, and every run of the action failed with the coded `500` that names the action and the compile error.

What moves (FROM → TO):

- `action-body-does-not-compile` on an unterminated regular-expression literal or an invalid flag set (`validateActionBodyCompiles`, `@objectstack/lint`): FROM `warning` TO `error` at `os validate`, `os build`, `os lint` and the runtime publish door (`action` and `object` writes, `422 INVALID_METADATA`), with a hint that names the fix. A failure in the literal's pattern (a duplicate named capture group, a modifier group: syntax QuickJS accepts and runs) is still a `warning`.
- `os explain action-body-does-not-compile` and the ADR-0087 entry `action-body-does-not-compile-refused` now say that only a pattern the host refuses is a warning.

The fix: close the literal with `/`, write a `/` inside it as `\/`, or, when the `/` is a division, give it a left-hand operand; write the flags as letters from `dgimsuvy`, each at most once, never `u` with `v`.

No conversion is made (ADR-0087 D3, entry `action-body-regexp-literal-token-refused`): whether a `/` opened a literal or meant a division is the author's choice. Measured first-party exposure: zero.
2 changes: 1 addition & 1 deletion content/docs/automation/hook-bodies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +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`.
- **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`.

Four literal write shapes are recognized, and only these:

Expand Down
6 changes: 3 additions & 3 deletions packages/lint/src/authoring-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2949,9 +2949,9 @@ export const AUTHORING_RULES: readonly AuthoringRule[] = [
// 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
// regular-expression PATTERN the host refuses is the one `warning` arm (the
// two engines' pattern dialects measurably differ; a literal that never
// closes or carries a bad flag set is refused; 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`.
Expand Down
10 changes: 6 additions & 4 deletions packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,10 +1478,12 @@ const ACTION_BODY_DOES_NOT_COMPILE_EXPLANATION: RuleExplanation = {
'local another name. `var input = …` is not a redeclaration and compiles.',
'The rule compiles the body inside the same expression the runtime builds, from the same function, ' +
'with this host\'s JavaScript engine and without running it. The runtime\'s engine is QuickJS. ' +
'The two agree on redeclarations, grammar and strict-mode errors; they measurably differ on regular ' +
'expressions, so a regular-expression literal the host refuses is reported as a warning rather than ' +
'refused. Syntax the host accepts and QuickJS does not is not caught here; at run time such an ' +
'action answers a 500 that names the action and the compile error.',
'The two agree on redeclarations, grammar and strict-mode errors; they measurably differ inside the ' +
'pattern of a regular expression (duplicate named groups, modifier groups), so a pattern the host ' +
'refuses is reported as a warning rather than refused. A literal that never closes on its line, or ' +
'whose flags are not a flag set, is refused like any other compile error: QuickJS refuses it too. ' +
'Syntax the host accepts and QuickJS does not is not caught here; at run time such an action answers ' +
'a 500 that names the action and the compile error.',
],
};

Expand Down
55 changes: 54 additions & 1 deletion packages/lint/src/validate-action-body-compiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('action-body-does-not-compile · the wrapper is the runtime\'s', () =>
expect(validateActionBodyCompiles({ actions })).toEqual([]);
});

it('reports a regular-expression literal the host refuses as a warning, never a refusal', () => {
it('reports a regular-expression pattern the host refuses as a warning, never a refusal', () => {
const [f] = validateActionBodyCompiles({ actions: [scriptAction('p', 'return /(/.test(input.s);')] });
expect(f.severity).toBe('warning');
expect(f.message).toContain('regular expression');
Expand Down Expand Up @@ -150,3 +150,56 @@ describe('action-body-does-not-compile · the doors that run it', () => {
expect(compileFindings(r.errors)).toEqual([]);
});
});

describe('action-body-does-not-compile · a regular-expression literal\'s token is refused, only its pattern warns', () => {
// Measured against the runtime's QuickJS (quickjs-emscripten, the engine quickjs-runner.ts runs a body in):
// every case refused here is refused by QuickJS too, and both warning cases compile and run there.
const only = (source: string) => {
const findings = validateActionBodyCompiles({ actions: [scriptAction('p', source)] });
expect(findings, source).toHaveLength(1);
return findings[0];
};

it.each([
['an unterminated literal', 'return /abc;'],
['an unterminated literal whose class never closes', 'return /[/.test(input.s);'],
['a literal the body ends inside', 'const ok = /abc'],
['a literal broken by a line terminator', 'return /ab\nc/.test(input.s);'],
])('refuses %s at error, the way every other compile error is refused', (_label, source) => {
const f = only(source);
expect([f.rule, f.severity, f.path]).toEqual([ACTION_BODY_DOES_NOT_COMPILE, 'error', 'actions[0].body.source']);
});

it.each([
['a repeated flag', 'return /a/gg;'],
['an unknown flag', 'return /a/zz;'],
['`u` with `v`', 'return /a/uv;'],
])('refuses a flag set that is not one, %s, at error', (_label, source) => {
const f = only(source);
expect([f.rule, f.severity]).toEqual([ACTION_BODY_DOES_NOT_COMPILE, 'error']);
});

it.each([
['a duplicate named capture group', 'return /(?<a>x)|(?<a>y)/.test(input.s);'],
['a modifier group', 'return /(?i:a)/.test(input.s);'],
])('still only warns on %s, a pattern the runtime\'s engine accepts', (_label, source) => {
const f = only(source);
expect([f.rule, f.severity]).toEqual([ACTION_BODY_DOES_NOT_COMPILE, 'warning']);
});

it('accepts the flags both engines take', () => {
const actions = ['return /a/dgimsuy;', 'return /[a&&b]/v;'].map((source, i) => scriptAction(`ok${i}`, source));
expect(validateActionBodyCompiles({ actions })).toEqual([]);
});

it('os validate refuses an unterminated literal, and so does the runtime publish door', () => {
const body = 'return /abc;';
const stack = { objects: [ticket()], actions: [scriptAction('p', body)] };
const fs = compileFindings(runAuthoringRules('validate', { normalized: stack, parsed: stack }));
expect(fs.map((f) => [f.severity, f.path])).toEqual([['error', 'actions[0].body.source']]);

const refused = runRuntimeAuthoringRules({ type: 'action', item: scriptAction('p', body), context: { objects: [ticket()] } });
expect(compileFindings(refused.errors)).toHaveLength(1);
expect(compileFindings(refused.advisories)).toEqual([]);
});
});
58 changes: 46 additions & 12 deletions packages/lint/src/validate-action-body-compiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@
* The runtime's engine is QuickJS; this rule's is the host's (V8 on Node). Both
* implement the same early errors, and over a probe of 36 bodies the two agreed
* on every redeclaration, every grammar error, every strict-mode error and every
* misplaced `new.target` / `super`. They disagreed in one place: a regular
* expression literal. Node 22's V8 refuses duplicate named capture groups
* (`/(?<a>x)|(?<a>y)/`), which QuickJS accepts and runs. A regular-expression
* failure is therefore reported as a `warning`: the two dialects are where a
* host refusal measurably does not predict a runtime refusal, and an `error`
* there would refuse a body that runs. Every other compile failure is an
* misplaced `new.target` / `super`. They disagree inside the PATTERN of a
* regular-expression literal: Node 22's V8 refuses duplicate named capture
* groups (`/(?<a>x)|(?<a>y)/`) and modifier groups (`/(?i:a)/`), both of which
* QuickJS accepts and runs. A failure V8 reports against a pattern — its
* sentence names the literal, `Invalid regular expression: /PATTERN/FLAGS:
* REASON` — is therefore a `warning`: that is where a host refusal measurably
* does not predict a runtime refusal, and an `error` there would refuse a body
* that runs.
*
* The literal's own TOKEN is not a dialect question. A literal that never closes
* on its line (`return /abc;` — V8 `Invalid regular expression: missing /`) and
* a flag set that is not one (`/a/gg`, `/a/zz`, `/a/uv` — V8 `Invalid regular
* expression flags`) are refused by QuickJS as well (`unexpected end of regexp`,
* `unexpected line terminator in regexp`, `invalid regular expression flags`),
* and over 55 single characters and every ordered pair of the eight flags the
* two engines agreed on all 119 flag sets. So only the pattern shape takes
* the warning arm, and every other compile failure — these two included — is an
* `error`. The disagreement in the other direction (syntax the host accepts and
* QuickJS does not, such as newer grammar) is a missed finding, never a false
* one; at run time the action then answers a coded `500` naming itself and the
Expand All @@ -59,7 +70,10 @@ import { collectActionBodies } from './validate-action-body-writes.js';
export const ACTION_BODY_DOES_NOT_COMPILE = 'action-body-does-not-compile';

export interface ActionBodyCompileFinding {
/** `error`, except a regular-expression literal the host refuses (`warning`; see the module note). */
/**
* `error`, except a regular-expression PATTERN the host refuses (`warning`; see the module note). A
* literal that never closes, or whose flags are not a flag set, is an `error`.
*/
severity: 'error' | 'warning';
rule: string;
/** e.g. `action "decide_approval_step" › body`. */
Expand Down Expand Up @@ -116,8 +130,20 @@ export function hostCompileFailure(text: string): HostCompileFailure | undefined
}
}

/** The host's sentence for a regular-expression literal it refuses (V8 and SpiderMonkey both open so). */
const REGEXP_FAILURE = /^SyntaxError: Invalid regular expression/;
/**
* V8's sentence for a regular-expression PATTERN it refuses, which names the
* literal: `Invalid regular expression: /PATTERN/FLAGS: REASON`. The one
* `warning` arm. The literal's token errors open with the same three words and
* are not this shape (`Invalid regular expression: missing /`, `Invalid regular
* expression flags`), so they stay `error`, and so does any other sentence.
*/
const REGEXP_PATTERN_FAILURE = /^SyntaxError: Invalid regular expression: \//;

/** V8's sentence for a regular-expression literal that never closes on its line. */
const REGEXP_UNTERMINATED = /^SyntaxError: Invalid regular expression: missing \//;

/** V8's sentence for a regular-expression literal whose flags are not a flag set. */
const REGEXP_FLAGS = /^SyntaxError: Invalid regular expression flags/;

/** A redeclaration the host names (`Identifier 'input' has already been declared`), and the name. */
const REDECLARED = /^SyntaxError: Identifier '([^']+)' has already been declared/;
Expand All @@ -137,7 +163,9 @@ export function validateActionBodyCompiles(stack: Record<string, unknown>): Acti
if (!failure) continue;

const at = failure.line !== undefined ? `, line ${failure.line}` : '';
const regexp = REGEXP_FAILURE.test(failure.error);
const regexp = REGEXP_PATTERN_FAILURE.test(failure.error);
const unterminated = REGEXP_UNTERMINATED.test(failure.error);
const flags = REGEXP_FLAGS.test(failure.error);
const redeclared = REDECLARED.exec(failure.error)?.[1];
const parameter = redeclared !== undefined && (ACTION_BODY_PARAMETERS as readonly string[]).includes(redeclared)
? redeclared
Expand All @@ -159,8 +187,14 @@ export function validateActionBodyCompiles(stack: Record<string, unknown>): Acti
`\`const params = ctx.input || {}\``
: regexp
? 'Rewrite the regular expression in a form both engines accept, or build it with new RegExp(…) at run time'
: `Fix body.source so it compiles as the statements of an async function whose parameters are ` +
`${ACTION_BODY_PARAMETERS.map((p) => `\`${p}\``).join(' and ')}`,
: unterminated
? 'A `/` opens a regular-expression literal that never closes on its line: close it with `/`, write a ' +
'`/` inside it as `\\/`, or, if the `/` is a division, give it a left-hand operand'
: flags
? 'A regular-expression literal\'s flags are letters from `dgimsuvy`, each at most once, and never ' +
'`u` with `v`'
: `Fix body.source so it compiles as the statements of an async function whose parameters are ` +
`${ACTION_BODY_PARAMETERS.map((p) => `\`${p}\``).join(' and ')}`,
});
}

Expand Down
Loading
Loading