Skip to content

Commit 6dbeb77

Browse files
committed
docs(skills): an async after-hook on a write event starts after the commit and is dropped on rollback; an async afterFind starts at dispatch
The Transaction row of Before vs After Hooks in skills/objectstack-data/references/data-hooks.md is now event-precise, Best Practices gains one DON'T, and two eval cases exercise both. The file pays by dropping two restatements, so its token ceiling does not move. Claude-Session: https://claude.ai/code/session_01WGufzgAt23MH7HTGSbYsVo Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9e6fee1 commit 6dbeb77

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/skills': patch
3+
---
4+
5+
docs(skills): an `async` after-hook on a write event starts after the commit and is dropped on rollback; an `async` `afterFind` starts at dispatch
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`skills/objectstack-data/references/data-hooks.md`, the **Transaction** row of Before vs After Hooks: an `async: true` hook on `afterInsert` / `afterUpdate` / `afterDelete` starts once the outermost transaction commits and never runs if it rolls back, a failed commit included; on `afterFind` it starts as the read is dispatched; outside a transaction it starts at once. The row used to say "in the background, after commit" for every event and did not say the hook is dropped on rollback. Best Practices gains one DON'T: do not react to a refused or rolled-back write from an `async: true` hook, which never runs then; use a blocking hook. Two eval cases exercise it: a background `afterInsert` that must not announce an order that did not survive, and a request for a background hook that records a failed insert. The file pays by dropping two restatements (the "Update by id" paragraph, which repeated its row of the `ctx.api.object` method table, and the sentence above the `body` vs `handler` table, which repeated the table), so its token ceiling does not move. No schema or runtime change.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@
220220
"must_contain": ["renamedFrom: ['sales_rep']", "value: 'crm_sales_rep'"],
221221
"must_not_contain": []
222222
}
223+
},
224+
{
225+
"id": 23,
226+
"prompt": "When an `order` is created, add a `fulfilment_job` row for the warehouse without slowing the save. Orders also arrive in atomic batches that can roll back, and a job must never exist for an order that did not survive. Our `afterFind` read-log hook carries the same flag: does it wait for the commit too?",
227+
"expected_output": "An `afterInsert` hook on `order` with `async: true` and a sandboxed `body: { language: 'js', source, capabilities: ['api.write'] }` that inserts the `fulfilment_job` through `ctx.api`; nothing awaits it, so the save is not slowed. Inside a transaction (the atomic batch) it starts once the outermost transaction commits and never runs if that rolls back, a failed commit included, so no job is written for an order that did not survive; outside a transaction it starts at once. The `afterFind` hook does not wait: a rollback does not undo a read, so with the flag it starts as the read is dispatched, whatever the transaction does next.",
228+
"files": [],
229+
"assertions": {
230+
"must_contain": ["afterInsert", "async: true", "api.write", "commit", "afterFind"],
231+
"must_not_contain": ["handler:", "HookContext"]
232+
}
233+
},
234+
{
235+
"id": 24,
236+
"prompt": "Write a background hook that adds a `payment_failure` row whenever an insert on `payment` is refused or rolled back. It must not slow the save.",
237+
"expected_output": "Redirects: a background (`async`) hook on `afterInsert` cannot do this. Inside a transaction it starts only once the outermost transaction commits and never runs when that rolls back (a failed commit included), so it never sees a refused or rolled-back insert. A reaction to a refused write belongs in a blocking hook, or where the write is made: the caller receives the error and records the `payment_failure` there, since a blocking hook's own `ctx.api` writes roll back with the write. The `async` flag stays for work that follows a committed insert.",
238+
"files": [],
239+
"assertions": {
240+
"must_contain": ["blocking", "commit"],
241+
"must_not_contain": ["async: true"]
242+
}
223243
}
224244
]
225245
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ObjectStack provides lifecycle events organized by operation type:
6161
| **Purpose** | Validation, enrichment, transformation | Side effects, notifications, logging |
6262
| **Can modify** | `ctx.input` (mutable) | `ctx.result` (mutable) |
6363
| **Can abort** | Yes (throw error → rollback) | A sync after-hook's throw still rolls back the transaction (see error handling) |
64-
| **Transaction** | Within transaction | **Within the transaction** (unless `async: true` — then in the background, after commit) |
64+
| **Transaction** | Within transaction | **Within the transaction**. `async: true` on `afterInsert` / `afterUpdate` / `afterDelete`: starts once the outermost transaction commits, never if it rolls back (a failed commit included); on `afterFind`: starts as the read is dispatched; outside a transaction: starts at once |
6565
| **Error handling** | Aborts by default (`onError: 'abort'`) | **Also aborts by default** — `onError` defaults to `'abort'` unconditionally; set `onError: 'log'` on side-effect hooks |
6666

6767
---
@@ -214,9 +214,6 @@ CEL has no overload for does **not** degrade to "the hook did not fire" — it
214214

215215
## Sandboxed Hook Bodies (`body`) — What the Sandbox `ctx` Can Call
216216

217-
A hook carries its logic as a **`body`** — the form a metadata-only runtime
218-
executes — or as the deprecated inline **`handler`**.
219-
220217
### `body` (sandboxed) vs `handler` (inline) — pick one
221218

222219
| | **`body`** — sandboxed script | **`handler`** — inline function (deprecated) |
@@ -324,10 +321,6 @@ An unpredicated `find` / `count` is fine — returning or counting every row is
324321
honest answer. It is `findOne`'s implicit "just one of them" that turns a missing
325322
predicate into a confidently wrong record.
326323

327-
**Update by id.** `update` reads the primary key out of `data`, so the
328-
single-record form is `update({ id, ...fieldsToChange })` — e.g.
329-
`update({ id: pos, status: 'filled' })`.
330-
331324
### Capabilities — the complete list
332325

333326
A body may only touch a `ctx` API it declared in `capabilities`. Calling an
@@ -821,8 +814,9 @@ cross-object operations; refuse a business rule by a
821814

822815
❌ **DON'T** — do expensive work in `before*` (it blocks the transaction); let a
823816
hook re-trigger itself; use `object: '*'` unless you mean every object; throw in
824-
`after*` unless the failure must abort the operation; assume `ctx.session` exists
825-
— system operations carry no user.
817+
`after*` unless the failure must abort the operation; react to a refused or
818+
rolled-back write from an `async: true` hook, which never runs then (use a
819+
blocking hook); assume `ctx.session` exists — system operations carry no user.
826820

827821

828822
---

0 commit comments

Comments
 (0)