Repository navigation
Commit adcb9f8
docs(skills): data-hooks teaches when a before* hook's ctx.api writes roll back with a refused save (#34964)
Fixes #34504
Ruling-ref: 6929103019
Clause-②: no
Metadata-protocol: none
## 维护者速读
-
改了什么:数据钩子技能页(`skills/objectstack-data/references/data-hooks.md`)写清楚了一条规则:保存前钩子通过
`ctx.api`
顺手写的记录,和这次保存在同一个事务里,保存被拒就一起撤回。页面同时列出四种不撤回的情况,并新增一条"不要在保存前钩子里往另一个数据源写业务记录"。另加两条评测用例。
- 为什么改:PR 34503 已按裁决 A「整笔保存一起撤回」改了引擎行为,技能页却还只写"Within transaction"。AI
照旧页面写钩子,会写出运行时被拒或被撤回的写入(HotCRM 约 28 个对象带这类钩子)。
- 风险与回滚:只改技能文档和评测,不改运行时。风险在措辞:页面说的必须和引擎一致。另外该页 token 上限只剩
15,所以删了同页四处重复表述来付账,上限没有动。回滚即撤回本 PR。
- 先看:① `data-hooks.md` 的 "Before vs After Hooks" 表 Transaction 行
`before*` 单元格(核心规则);② 同文件 `condition` 一节的 "`after*` hooks dispatch once
per matched row too" 一条(八行压成三行,是四处删减里唯一改写而不是整句删除的);③
`evals/hooks-security-seeds-datasources.json` 第 25、26 条。
- 两分钟验证:运行 `node scripts/check-skills-token-ratchet.mjs`,预期看到
`data-hooks.md is 10117 tokens (ceiling 10122; headroom 5)`,退出码 0。
- 需要你做的:审阅并批准本 PR(`skills/**` 是受管面,Tier H,你批准后席位才落地)。
## What changes
`skills/objectstack-data/references/data-hooks.md`, three places.
**1. "Before vs After Hooks" table, Transaction row, `before*` cell.**
It said `Within transaction`. It now says, verbatim:
> One transaction with the outer write: a refusal anywhere in it (a
validation, a later hook, a driver constraint) rolls back the hook's
`ctx.api` writes; no per-hook opt-out. Exits: a caller's open
transaction decides; a hooked object off the default datasource, or a
default driver with no transactions (Turso's remote transport), opens
none, so those writes stay stored after a refusal; a hook registered in
code (`registerHook`) opens none. MongoDB needs a replica set (read in
code, not measured)
**2. Best Practices, the DON'T list, one new item.** Verbatim:
> write business records to another datasource from a `before*` hook
through `ctx.api` (its transaction refuses that write with
`CrossDatasourceTransactionWriteError`; only an append-only ledger,
`lifecycle.class` `audit` / `telemetry` / `event`, is written outside it
and survives its rollback)
**3. Four restatements removed, to pay for the new text.** The file sat
at 9996 tokens against a ceiling of 10011 (39984 bytes). The two
additions cost 778 bytes, about 195 tokens. No ceiling moved, so the
text is paid for by deleting 795 bytes of restatement in the same file:
| Removed | Bytes | Where the fact still stands |
|:--|--:|:--|
| Intro sentence "Covers the lifecycle events, the sandboxed `body` ...
read masking)." | 219 | The page's own contents list, three lines above
it, and `SKILL.md` line 46, which carries the same summary |
| The `after*` per-row bullet under `condition`, compressed from eight
lines to three | 371 | "Why so few?" note under Hook Lifecycle Events:
"Both phases dispatch once per matched row, each on a
single-record-shaped context whose `input.id` ... names that row and
whose `previous` is that row's pre-image". The kept sentence still says
`record` is the row's real state and that a transition condition means
the same for an id write and a predicate write |
| "and the declarative fields (`condition`, `async`, `retryPolicy`,
`timeoutMs`, `onError`, `priority`) are honoured **only** on this path"
(Registration, method 1) | 137 | Registration, method 2, which says it
more exactly: those semantics "apply only through `defineStack({ hooks
})` or `ql.bindHooks([...])`". `priority` is still shown in method 2's
`registerHook` example |
| "A single `afterFind` subscription covers both `find` and `findOne`."
(pattern 4 callout) | 68 | The event table rows for `beforeFind` /
`afterFind` and the "Why so few?" note ("one subscription covers every
read shape") |
`skills/objectstack-data/evals/hooks-security-seeds-datasources.json`
gains two cases:
- **25 (positive).** A declared `beforeInsert` hook creates a
`timesheet` row, a validation refuses the entry. The answer must say the
row rolls back, that `onError: 'log'` does not keep it, and name the
exits. Graded on `roll`, `default datasource`, `registerHook`.
- **26 (DON'T).** A request to write a business object on another
datasource from a `beforeInsert` hook. The answer must redirect. Graded
on `CrossDatasourceTransactionWriteError`, `default datasource`.
`.changeset/34504-skills-before-hook-unit-of-work.md`: `patch` on
`@objectstack/skills`. The `skills/**` tree ships in that package
(`packages/skills/scripts/sync-catalog.mjs` copies it into `dist`), so
this diff publishes and `skip-changeset` does not apply.
## Why
PR 34503 landed the engine change under ruling 6929103019 on card 34412.
The ruling comment, verbatim:
> Ruling: letter A · maintainer 「A 整笔保存一起撤回」 (the option he chose on the
decision card) · recorded 2026-11-16T20:56Z
> **Route A.** A write on an object that carries a metadata-bound
`before*` hook on the same verb opens the write door's unit, with the
three exits #22782 already has (a caller's open unit decides; an object
off the default driver; a driver with no transactions). A refusal
anywhere in that write then rolls back the hook's `ctx.api` side writes
with it.
The skill page is what AI authors read before they write a hook. It said
"Within transaction" with no condition, and nothing on the exits or on
the cross-datasource refusal.
## What each taught sentence rests on
All readings are from this branch's base, `fff0ad0c18` on `main`.
| Taught | Code | Governing doc |
|:--|:--|:--|
| One transaction with the outer write; a refusal anywhere rolls back
the hook's `ctx.api` writes |
`packages/objectql/src/engine-hooks-and-functions.ts`,
`opensAfterAbortUnit`: the `sideWriting` term opens the unit for a
metadata-bound `before*` hook on the same verb |
`content/docs/automation/hooks.mdx`, unit-of-work table, `before*` row;
`content/docs/api/data-flow.mdx`, `beforeInsert/Update/Delete` row |
| No per-hook opt-out | Same function: neither `async` nor `onError`
narrows `sideWriting` | `hooks.mdx`: "a refusal anywhere in the write
rolls them back, with no per-hook opt-out" |
| Exit: a caller's open transaction decides | Same function, first line:
an open threaded or ambient unit returns `false` | Ruling 6929103019,
first exit |
| Exit: a hooked object off the default datasource opens none | Same
function: `this.getDriver(object) === defaultDriver` (`object` there is
the outer write's object, the one the hook is on) | `hooks.mdx` table:
"Not opened on an object outside the default datasource" |
| Exit: a default driver with no transactions (turso's remote transport)
opens none | Same function: `driverSupportsTransactions(defaultDriver)`;
`packages/drivers/driver-turso/src/turso-driver.ts`:
`transactionsUnsupported: this.transportMode === 'remote'` | `hooks.mdx`
table: "or on a driver with no transactions: there a `before*` hook's
writes stay stored after a refusal" |
| Exit: a hook registered in code (`registerHook`) opens none | Same
function: `sideWriting` requires `entry.meta`, which only a
metadata-bound hook carries | `hooks.mdx` table: "A hook registered in
code with `registerHook` opens no unit" |
| The cross-datasource write is refused with
`CrossDatasourceTransactionWriteError` |
`packages/objectql/src/engine-execute-transactions.ts`,
`enforceTransactionOrigin` | `hooks.mdx`: "a write to an object on
another datasource is refused with
`CrossDatasourceTransactionWriteError`" |
| The append-only ledger carve-out (`audit` / `telemetry` / `event`) |
`packages/objectql/src/engine.ts`, `SYSTEM_LEDGER_LIFECYCLE_CLASSES` |
`hooks.mdx`: "The one exception is an append-only ledger on another
datasource" |
| MongoDB needs a replica set (read in code, not measured) |
`packages/drivers/driver-mongodb/src/mongodb-driver.ts`:
`beginTransaction` always calls `session.startTransaction()`, and the
driver declares no `transactionsUnsupported` | `hooks.mdx`: "on MongoDB,
run it as a replica set, which any unit needs". The hedge is the one PR
34503 carries: nobody ran it on a standalone server |
The stay-stored clause is attached only to the two exits `hooks.mdx`
attaches it to. For a code-registered hook the page says only that it
opens none.
## Tests
- No runtime code changes, so there is no new unit test and no ablation.
- The engine pins that hold the taught behaviour, run on this head
(`486b6feb8f`): `pnpm --filter @objectstack/objectql exec vitest run
--maxWorkers=2 src/engine-after-hook-abort-unit.test.ts` gives `Test
Files 1 passed (1)`, `Tests 21 passed (21)`. That file pins the three
verbs, the caller-opened transaction, the driver with no transactions
and the code-registered hook.
- The two new eval cases parse under the strict reader
(`packages/qa/scenarios/src/skill-evals/cases.ts`,
`loadSkillEvalCatalog` over this tree): 12 bundles, 219 cases, 0
refusals; `objectstack-data` has 46 cases (44 before).
## Acceptance notes
- Contract review round (record 7002617585 on 486b6fe: PASS with S1
should-fix). On 314d9cc (origin/main 35c6331 merged), the
`before*` Transaction cell and eval 25 name the hooked object in the
off-default-datasource exit: "Exits: a caller's open transaction
decides; a hooked object off the default datasource, or a default driver
with no transactions (Turso's remote transport), opens none". The
changeset matches. N4 taken; N1–N3 accepted. Budget after the merge:
`data-hooks.md` 10117 tokens against a ceiling of 10122 (40467 bytes).
Gates on 314d9cc: 47 derived, 45 run with exit 0, 2 not measured
locally (check:dual-build-cjs-loads, check:lean-entry-closure; CI runs
them). Overlap-check clean. The page teaches security-adjacent behaviour
(read masking coverage, the audit ledger row on rollback), so the PR
carries `security`.
- **Line budget (PM-set: at most +10 net lines on `data-hooks.md`).**
Whole file: 890 lines before, 885 after (net -5; +13 / -18). Whole
package, all `SKILL.md` files summed: 4728 lines before, 4728 after (12
files, none touched). Tokens, by `check-skills-token-ratchet`:
`data-hooks.md` 9996 before, 9992 after, ceiling 10011 unchanged;
ratcheted (authored) total 191126 before, 191122 after; eval fixtures
(measured, not ratcheted) 53751 before, 54274 after.
- **The changeset is outside `skills/**`.** The dispatch's file surface
named `skills/**` only and told me to apply the publish rule. The rule
says a diff that ships needs a changeset, and `skills/**` ships in
`@objectstack/skills`.
- **NOT MEASURED: MongoDB on a standalone server.** The page keeps the
"read in code, not measured" hedge. I read the driver's code and did not
run it.
- **The cross-datasource rule was measured with a throwaway probe, not
left in the tree.** No existing pin drives a `before*` hook's own
`ctx.api` write to another datasource (the refusal is pinned for
`transaction()` in `engine-transaction-same-origin.test.ts`). I copied
the pin file's fake engine into a temporary test, ran three cases on
`486b6feb8f` and deleted it (`git status` clean after). Results: a
metadata-bound `beforeInsert` writing a business row to the other
datasource was refused with `CrossDatasourceTransactionWriteError` (code
`ERR_CROSS_DATASOURCE_TRANSACTION_WRITE`), one unit begun and rolled
back, no row on either datasource; the same write from a code-registered
hook opened no unit and was stored; with the target declared
`lifecycle.class: 'audit'`, the row was written outside the unit and was
still there after the refusal.
- **NOT MEASURED: a live eval round.** The `skill-evals` runner calls a
model through a local login and is not a gate. The cases were checked
for shape only.
- **Not taught here, noted.** `hooks.mdx` also says that on PostgreSQL a
failed `ctx.api` statement inside the unit refuses the whole write even
when the hook body catches the error. The card's ask does not name it,
and it is not measured on PostgreSQL. carrier: #34563.
- **Not taught here, noted.** The Capabilities table says
`ctx.api.transaction` commits on return and rolls back on throw. Inside
a unit the write door opened, that transaction joins the outer unit: its
return is no commit, and a throw the body catches leaves its writes in
place (`hooks.mdx`: "A nested `transaction()` joins the outer unit, so
its return is no commit"). The row is outside the card's ask and the
file has 19 tokens of headroom left. Reported to the seat as an
out-of-scope finding.
- **Overlap.** `node scripts/pm/overlap-check.mjs` on `486b6feb8f`:
`VERDICT clean exit 0`, clean against `origin/main` (`fff0ad0c18`), and
no open PR shares a file with this branch.
- This PR touches a governed surface (`skills/**`). It stays a draft,
and it owes an L1 review and a contract-tier L2 review before the
maintainer's approval.
## Verification
Head `486b6feb8f` for every run below.
<details><summary>Derived gate families (47 derived, 43 run, 4 not
measured)</summary>
- `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack`: 47 commands, each run with its exit code
recorded before any pipe.
- `node scripts/pm/dispatch-gates.mjs --ran`: `47 derived famil(ies)
accounted for — 43 run, 4 NOT-MEASURED (4 DERIVED from a recorded exit
3)`. All 43 exit 0.
- NOT MEASURED, reason: each reads every package's built `dist`, which
this docs-only diff cannot move, and a full build was not run locally.
CI runs them: `check:dts-closure`, `check:dual-build-cjs-loads`,
`check:lean-entry-closure`, `check:sourcemap-no-sources-content`.
- `check:doc-formula-expressions` answered exit 3 on its first run
(prerequisite: `@objectstack/formula` and `@objectstack/lint` not
built). After `turbo run build` of those two packages it answered exit
0: `26 record-scoped formula example(s) across 578 files / 1855 TS
blocks judged clean`.
- `check-skills-token-ratchet`: `58 authored bundle file(s) within their
ceilings`.
- `check:skill-links`: `256 relative link(s) in 72 published skill
file(s) resolve; 130 fragment(s) name a real heading`.
- `check-empty-changeset --base origin/main`: `No changeset from the
merge base modified or deleted by this diff`.
- `check-adr-0087-registration --base origin/main`: `this PR adds no
declared-breaking changeset (1 non-breaking changeset(s) seen)`.
- `check:doc-authoring`, `check:corpus-claim-drift`, `check:role-word`,
`check:skill-identifier-liveness`, `check:nul-bytes`,
`check:published-files`, `check:skill-refs`: exit 0.
</details>
<details><summary>Builds and the pin run</summary>
- `bash scripts/pm/os-verify-lock.sh -c 'pnpm exec turbo run build
--concurrency=2 --filter=@objectstack/formula --filter=@objectstack/lint
--filter=@objectstack/skills'`: `Tasks: 6 successful, 6 total`, `VERDICT
command-exit 0`. The `@objectstack/skills` build is `sync-catalog
--self-test` plus the sync.
- `bash scripts/pm/os-verify-lock.sh -c "pnpm --workspace-concurrency=2
--filter '@objectstack/objectql^...' build && pnpm --filter
@objectstack/objectql exec vitest run --maxWorkers=2
src/engine-after-hook-abort-unit.test.ts"`: `Tests 21 passed (21)`,
`VERDICT command-exit 0`.
- `pnpm lint`, the type-check lanes and the rest of the farm: CI.
</details>
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(not touched)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` is CI's run (not run locally).
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: its generated artifacts are
regenerated and committed. (not touched)
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English.
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
---
_Generated by [Claude
Code](https://claude.ai/code/session_012MmDsS4GoHCgYwA2uv3n3C)_
<!-- signed: repo:objectstack#4 · os-lizheng ·
session_012MmDsS4GoHCgYwA2uv3n3C -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 438ff47 commit adcb9f8
3 files changed
Lines changed: 43 additions & 18 deletions
File tree
- .changeset
- skills/objectstack-data
- evals
- references
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
293 | 313 | | |
294 | 314 | | |
295 | 315 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 10 | + | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
| |||
61 | 59 | | |
62 | 60 | | |
63 | 61 | | |
64 | | - | |
| 62 | + | |
65 | 63 | | |
66 | 64 | | |
67 | 65 | | |
| |||
184 | 182 | | |
185 | 183 | | |
186 | 184 | | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
195 | 188 | | |
196 | 189 | | |
197 | 190 | | |
| |||
671 | 664 | | |
672 | 665 | | |
673 | 666 | | |
674 | | - | |
| 667 | + | |
675 | 668 | | |
676 | 669 | | |
677 | 670 | | |
| |||
752 | 745 | | |
753 | 746 | | |
754 | 747 | | |
755 | | - | |
756 | | - | |
757 | | - | |
| 748 | + | |
758 | 749 | | |
759 | 750 | | |
760 | 751 | | |
| |||
791 | 782 | | |
792 | 783 | | |
793 | 784 | | |
794 | | - | |
795 | | - | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
796 | 791 | | |
797 | 792 | | |
798 | 793 | | |
| |||
0 commit comments