diff --git a/.changeset/34191-skills-async-hook-commit.md b/.changeset/34191-skills-async-hook-commit.md new file mode 100644 index 00000000000..88f948268da --- /dev/null +++ b/.changeset/34191-skills-async-hook-commit.md @@ -0,0 +1,10 @@ +--- +'@objectstack/skills': patch +--- + +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 + +Clause-②: no +Metadata-protocol: none + +`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 sync hook, whose `ctx.api` writes roll back with the write, or record it in the caller, where the write is made. 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 three restatements (the "Update by id" paragraph, which repeated its row of the `ctx.api.object` method table; the sentence above the `body` vs `handler` table, which repeated the table; and the closing sentence of the `findOne` note, which repeated its opening paragraph), so its token ceiling does not move. No schema or runtime change. diff --git a/skills/objectstack-data/evals/hooks-security-seeds-datasources.json b/skills/objectstack-data/evals/hooks-security-seeds-datasources.json index e70e3ce7b60..6bccdabab0c 100644 --- a/skills/objectstack-data/evals/hooks-security-seeds-datasources.json +++ b/skills/objectstack-data/evals/hooks-security-seeds-datasources.json @@ -220,6 +220,26 @@ "must_contain": ["renamedFrom: ['sales_rep']", "value: 'crm_sales_rep'"], "must_not_contain": [] } + }, + { + "id": 23, + "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?", + "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.", + "files": [], + "assertions": { + "must_contain": ["afterInsert", "async: true", "api.write", "commit", "afterFind"], + "must_not_contain": ["handler:", "HookContext"] + } + }, + { + "id": 24, + "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.", + "expected_output": "Redirects: a background (`async: true`) 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. Record the failure in the caller, where the write is made: the caller receives the error and inserts the `payment_failure` row outside the rolled-back transaction. A sync hook is no home for this row either: its own `ctx.api` writes roll back with the write. The `async` flag stays for work that follows a committed insert.", + "files": [], + "assertions": { + "must_contain": ["caller", "roll"], + "must_not_contain": [] + } } ] } diff --git a/skills/objectstack-data/references/data-hooks.md b/skills/objectstack-data/references/data-hooks.md index 95f1acdf951..eab91d43c9a 100644 --- a/skills/objectstack-data/references/data-hooks.md +++ b/skills/objectstack-data/references/data-hooks.md @@ -61,7 +61,7 @@ ObjectStack provides lifecycle events organized by operation type: | **Purpose** | Validation, enrichment, transformation | Side effects, notifications, logging | | **Can modify** | `ctx.input` (mutable) | `ctx.result` (mutable) | | **Can abort** | Yes (throw error → rollback) | A sync after-hook's throw still rolls back the transaction (see error handling) | -| **Transaction** | Within transaction | **Within the transaction** (unless `async: true` — then in the background, after commit) | +| **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 | | **Error handling** | Aborts by default (`onError: 'abort'`) | **Also aborts by default** — `onError` defaults to `'abort'` unconditionally; set `onError: 'log'` on side-effect hooks | --- @@ -214,9 +214,6 @@ CEL has no overload for does **not** degrade to "the hook did not fire" — it ## Sandboxed Hook Bodies (`body`) — What the Sandbox `ctx` Can Call -A hook carries its logic as a **`body`** — the form a metadata-only runtime -executes — or as the deprecated inline **`handler`**. - ### `body` (sandboxed) vs `handler` (inline) — pick one | | **`body`** — sandboxed script | **`handler`** — inline function (deprecated) | @@ -321,12 +318,7 @@ await ctx.api.object('candidate').find({ limit: 1 }); // any row will ``` An unpredicated `find` / `count` is fine — returning or counting every row is an -honest answer. It is `findOne`'s implicit "just one of them" that turns a missing -predicate into a confidently wrong record. - -**Update by id.** `update` reads the primary key out of `data`, so the -single-record form is `update({ id, ...fieldsToChange })` — e.g. -`update({ id: pos, status: 'filled' })`. +honest answer. ### Capabilities — the complete list @@ -821,8 +813,11 @@ cross-object operations; refuse a business rule by a ❌ **DON'T** — do expensive work in `before*` (it blocks the transaction); let a hook re-trigger itself; use `object: '*'` unless you mean every object; throw in -`after*` unless the failure must abort the operation; assume `ctx.session` exists -— system operations carry no user. +`after*` unless the failure must abort the operation; react to a refused or +rolled-back write from an `async: true` hook, which never runs then — use a sync +hook (its `ctx.api` writes roll back with the write) or record it in the caller, +where the write is made; assume `ctx.session` exists — system operations carry +no user. ---