From 6dbeb77db7bc96710d3d1586bbdcdd51d4d5630d Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 16:45:33 +0000 Subject: [PATCH 1/2] 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 --- .changeset/34191-skills-async-hook-commit.md | 10 ++++++++++ .../hooks-security-seeds-datasources.json | 20 +++++++++++++++++++ .../objectstack-data/references/data-hooks.md | 14 ++++--------- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 .changeset/34191-skills-async-hook-commit.md diff --git a/.changeset/34191-skills-async-hook-commit.md b/.changeset/34191-skills-async-hook-commit.md new file mode 100644 index 00000000000..34db2d108fa --- /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 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. diff --git a/skills/objectstack-data/evals/hooks-security-seeds-datasources.json b/skills/objectstack-data/evals/hooks-security-seeds-datasources.json index e70e3ce7b60..d1589dfff4f 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`) 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.", + "files": [], + "assertions": { + "must_contain": ["blocking", "commit"], + "must_not_contain": ["async: true"] + } } ] } diff --git a/skills/objectstack-data/references/data-hooks.md b/skills/objectstack-data/references/data-hooks.md index 95f1acdf951..b0b9a1932a3 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) | @@ -324,10 +321,6 @@ An unpredicated `find` / `count` is fine — returning or counting every row is 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' })`. - ### Capabilities — the complete list 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 ❌ **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 +blocking hook); assume `ctx.session` exists — system operations carry no user. --- From 919c3dfb47b9edfc720cf53fb3fee0278cc2681d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 00:21:28 +0000 Subject: [PATCH 2/2] docs(skills): the async-hook DON'T names both homes for reacting to a failed write A sync hook's ctx.api writes roll back with the write, so the sentence also names the caller, where the write is made. Eval 24 requires the caller-side home and no longer forbids naming the flag. Paid by one more same-file restatement cut. Claude-Session: https://claude.ai/code/session_01WGufzgAt23MH7HTGSbYsVo Co-authored-by: Claude --- .changeset/34191-skills-async-hook-commit.md | 2 +- .../evals/hooks-security-seeds-datasources.json | 6 +++--- skills/objectstack-data/references/data-hooks.md | 9 +++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/.changeset/34191-skills-async-hook-commit.md b/.changeset/34191-skills-async-hook-commit.md index 34db2d108fa..88f948268da 100644 --- a/.changeset/34191-skills-async-hook-commit.md +++ b/.changeset/34191-skills-async-hook-commit.md @@ -7,4 +7,4 @@ docs(skills): an `async` after-hook on a write event starts after the commit and 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 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/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 d1589dfff4f..6bccdabab0c 100644 --- a/skills/objectstack-data/evals/hooks-security-seeds-datasources.json +++ b/skills/objectstack-data/evals/hooks-security-seeds-datasources.json @@ -234,11 +234,11 @@ { "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`) 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.", + "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": ["blocking", "commit"], - "must_not_contain": ["async: true"] + "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 b0b9a1932a3..eab91d43c9a 100644 --- a/skills/objectstack-data/references/data-hooks.md +++ b/skills/objectstack-data/references/data-hooks.md @@ -318,8 +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. +honest answer. ### Capabilities — the complete list @@ -815,8 +814,10 @@ 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; react to a refused or -rolled-back write from an `async: true` hook, which never runs then (use a -blocking hook); assume `ctx.session` exists — system operations carry no user. +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. ---