Skip to content

Commit 03b9383

Browse files
committed
docs(skills): data-hooks teaches the hook and job js-body compile refusal
Claude-Session: https://claude.ai/code/session_01L6bhgTRf6qBRCqPh4JZaZL Co-authored-by: Claude <noreply@anthropic.com>
1 parent 598a34d commit 03b9383

3 files changed

Lines changed: 39 additions & 17 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@objectstack/skills': patch
3+
---
4+
5+
docs(skills): the hooks skill says a hook or job `js` body that does not compile is refused, and how to fix it
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
objectstack-data's `references/data-hooks.md`, "Sandbox restrictions":
11+
12+
- A new bullet says a `js` source is compiled, without being run, inside the `(async (ctx) => { … })` wrapper the runtime runs it in, where `ctx` is already declared. `const ctx = ctx.input || {};` (or `let ctx`, one name declared twice, any syntax error) is `hook-body-does-not-compile`: an error from `os validate`, `os build`, `os lint` and a runtime hook save (`422 INVALID_METADATA`), naming the hook, the `SyntaxError` and its line. The fix is to use `ctx` as it is or rename the local (`const input = ctx.input || {};`). A job's `js` body runs in the same wrapper and is `job-body-does-not-compile` from the three commands. A regular-expression pattern only the checking engine refuses is a warning.
13+
- Six restatements in the same file were removed to pay for the new text; the file is three lines and five tokens shorter than before.
14+
15+
`evals/hooks-security-seeds-datasources.json` gains one case on the refusal and its fix.

‎skills/objectstack-data/evals/hooks-security-seeds-datasources.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@
326326
"amount_paid: 2000"
327327
]
328328
}
329+
},
330+
{
331+
"id": 33,
332+
"prompt": "`os validate` reports an error on this `beforeInsert` hook on `lead`, and I cannot see why: `body: { language: 'js', source: \"const ctx = ctx.input || {}; if (!ctx.status) ctx.status = 'new';\", capabilities: [] }`. What is wrong, and what should the source be?",
333+
"expected_output": "The `js` source is compiled inside the wrapper the runtime runs it in, `(async (ctx) => { … })`, where `ctx` is already the parameter: `const ctx = …` declares it a second time, a SyntaxError before any statement runs. The finding is `hook-body-does-not-compile`, an error from `os validate`, `os build` and `os lint`, and a runtime save of the hook is refused with `422 INVALID_METADATA`. Fix: use `ctx` as it is (`if (!ctx.input.status) ctx.input.status = 'new';`) or give the local another name (`const input = ctx.input || {};`). Does not suppress the rule, and does not move the logic to a deprecated `handler`.",
334+
"files": [],
335+
"assertions": {
336+
"must_contain": ["hook-body-does-not-compile", "ctx.input.status"],
337+
"must_not_contain": []
338+
}
329339
}
330340
]
331341
}

‎skills/objectstack-data/references/data-hooks.md‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ const myHook = defineHook({
123123

124124
### Key Properties Explained
125125

126-
#### `body` — Implementation
127-
128-
A `source` of statements against `ctx`; a throw aborts the write
129-
([Sandboxed Hook Bodies](#sandboxed-hook-bodies-body--what-the-sandbox-ctx-can-call)).
130-
131126
#### `condition` — Declarative Filtering
132127

133128
Skip the hook if the condition is false. Author it as a **CEL
@@ -155,9 +150,7 @@ made total over the object's **declared** fields (`null` when a declared field i
155150
in neither). So:
156151

157152
- Reference **any** declared field, not just the ones this update touches —
158-
`record.done == true` works on an update that only sets `status`. (This is
159-
unlike `ctx.input` / `ctx.result` inside the body, which stay partial — see
160-
Gotcha 1.)
153+
`record.done == true` works on an update that only sets `status`.
161154
- `record` describes the record's **state**, not the diff. `record.done == true`
162155
fires on every update of an already-done record, not only on the update that
163156
set it. **For the transition, compare against `previous`**:
@@ -344,6 +337,15 @@ The body runs in an isolated QuickJS VM, **not** Node:
344337
a name bound at module scope, e.g. a `const slugify = …` helper sitting next to
345338
the hook. A `body` must be **self-contained**: inline the helper, or keep that
346339
hook as a bundled (deprecated) `handler`.
340+
- **Refused when it does not compile:** a `js` source is compiled, unrun, inside
341+
its `(async (ctx) => { … })` wrapper, where `ctx` is already declared. So
342+
`const ctx = ctx.input || {};` (or `let ctx`, one name declared twice, any
343+
syntax error) is `hook-body-does-not-compile`: an **error** from `os validate`,
344+
`os build`, `os lint` and a runtime hook save (`422 INVALID_METADATA`), naming
345+
the hook, the `SyntaxError` and its line. Fix: use `ctx` as it is, or rename
346+
the local (`const input = ctx.input || {};`). A job's `js` body runs in the
347+
same wrapper: `job-body-does-not-compile`, from the three commands. A regex
348+
pattern only the checking engine refuses is a warning.
347349

348350
> **A deadline stamped from an hours table is calendar hours** — nights, weekends and
349351
> holidays count. Name it `*_CALENDAR_HOURS` (`SLA_CALENDAR_HOURS`, or its camelCase)
@@ -450,8 +452,7 @@ interface HookContext {
450452
// Execution context
451453
session?: {
452454
userId?: string;
453-
organizationId?: string; // Active org — the single blessed name. Matches the
454-
// `organization_id` column + `current_user.organizationId` (RLS).
455+
organizationId?: string; // Active org — the single blessed name.
455456
// The former `tenantId` alias was removed in v16.
456457
// Privilege is judged by the security service,
457458
// never by a session claim.
@@ -660,11 +661,8 @@ const cascadeAccountUpdate = defineHook({
660661

661662
A `body` cannot do this: it writes back only `ctx.input`, and its `ctx.result` is a snapshot.
662663

663-
> For **static** field masking (a field is always hidden/masked for a permission
664-
> set or position), prefer declarative **field-level metadata** (secret/masked
665-
> fields) — it applies on every read path automatically. Use an `afterFind` hook
666-
> only for masking that depends on runtime logic the field metadata can't
667-
> express.
664+
> Static masking is declarative **field-level metadata** (secret/masked fields);
665+
> an `afterFind` hook is only for masking that depends on runtime logic.
668666
669667
```typescript
670668
const maskSensitiveData = defineHook({
@@ -674,7 +672,6 @@ const maskSensitiveData = defineHook({
674672
handler: async (ctx) => {
675673
// Exempt the engine's own elevated reads (`isSystem`) — internal writes
676674
// and self-reads must see the real values.
677-
// ⚠️ Never a session claim (`ctx.session.roles` is gone, ctx table above).
678675
const isElevated = ctx.session?.isSystem === true;
679676

680677
if (!isElevated) {
@@ -777,7 +774,7 @@ the layout both real hook modules in the repo use.
777774

778775
## Best Practices
779776

780-
✅ **DO** — use `before*` for validation and `after*` for side effects; set
777+
✅ **DO** — set
781778
`async: true` for non-critical background work; go through `ctx.api` for
782779
cross-object operations; refuse a business rule by a
783780
[declared code](#declared-refusals-refusals); test hooks in isolation.

0 commit comments

Comments
 (0)