Repository navigation
Commit a6cf988
fix(objectql)!: an action body's write inside ctx.api.transaction keeps its audit rows beside a telemetry datasource (#35102)
<!-- claude-projects-attribution: {"github_login":"os-support-ai"} -->
_Requested by **Yibo Wang** · [project
thread](https://claude.ai/code/project/chan_01BVUyyPie8gHwoPM18iABwB?thread=cmsg_01BVUyyPie8gHwoPM18iABwBLW6dHPW6sSheA32XPKZzPV)_
Fixes #35028
Clause-②: yes (narrowing) — inside a transaction opened by
`ScopedContext`'s discrete begin/commit/rollback trio (the sandbox's
`ctx.api.transaction`), a business write to an object on another
datasource is now refused with `CrossDatasourceTransactionWriteError`
(`ERR_CROSS_DATASOURCE_TRANSACTION_WRITE`, 400 at the action door).
Before, it ran on the transaction's connection, the wrong one.
Metadata-protocol: none
Skills-docs: follow-up #35117 (the `api.transaction` row: one datasource
per body transaction; a write to an object on another datasource is
refused)
> **BREAKING** for an action, hook or job body whose
`ctx.api.transaction` writes an object bound to a non-default
datasource. That write is now refused, naming both datasources, and the
body's transaction rolls back. Fix: keep every object written inside one
`ctx.api.transaction` on one datasource, or split the work per
datasource. Single-datasource deployments see no change. ADR-0087
disposition (in the changeset): not-required
(no-migration-prescription).
## 维护者速读
- 改了什么:沙箱 `ctx.api.transaction` 打开的事务句柄,现在在开启时记下归属的驱动;同源闸门据此判断,审计/动态行写到
telemetry 库自己的连接,跨库业务写被拒。
- 为什么改:有 telemetry 数据源时(每个 `os dev`、带 `OS_TELEMETRY_DB` 的 `os
serve`),动作体事务里的写入丢失审计行和动态行,接口仍回 200。
- 风险与回滚:动作体事务里写另一个数据源上的业务对象,从"写错连接"变为 400 拒绝;单数据源不变。回滚即 revert 本 PR。
- 先看:`packages/objectql/src/engine-scoped-context.ts`(beginTransaction
记录、commit/rollback
清除)、`packages/objectql/src/engine-execute-transactions.ts`(记录表与
enforceTransactionOrigin)、`packages/objectql/src/engine-middleware-and-context.ts`(transactionCoversDriverFor)。
- 两分钟验证:`pnpm --filter @objectstack/objectql exec vitest run
--maxWorkers=2 src/engine-transaction-same-origin.test.ts`,预期 29 个用例全绿。
- 需要你做的:Tier H(改了 `docs/adr/**`),需 os-zhuang 在当前 head 上 APPROVED 审核。
## What changes
- `ScopedContext.beginTransaction` records the opened handle's owner
(the same `TransactionScope` that `transaction(cb)` publishes), keyed by
handle in a WeakMap. `commitTransaction` and `rollbackTransaction` drop
it, also when the driver call throws.
- When `txStore` holds no scope for the handle,
`transactionCoversDriverFor` and `enforceTransactionOrigin` read that
record. So ledger rows (`sys_audit_log`, `sys_activity`) take the
existing #5351 carve-out: written on `telemetry`, outside the body
transaction, on their own connection. A cross-datasource business write
is refused before any hook runs.
- `enforceTransactionOrigin` takes the write's explicit handle
(`options.context.transaction`) as a fourth argument. That puts four
one-line call-site edits outside the claim's file surface: `engine.ts`
(the private wrapper), `engine-insert.ts`, `engine-update.ts` and
`engine-delete-and-aggregate.ts`. Route A needs them, because the gate
cannot otherwise see the handle.
- ADR-0119: an add-only dated note under "The declared LIMIT of the
same-origin gate". It narrows the limit to handles the engine never
opened. The existing paragraph is not rewritten.
- The optional plugin-audit fix-text change was not made. With this fix,
the platform's own trio no longer produces that third cause.
- No `IDataDriver` or `packages/spec` change. The sudo() `'action'`
elevation-mark defect (#35052) is not touched.
- Contract review S1 (fix round): `enforceTransactionOrigin` now
discards an explicit handle the current run was detached from before it
reads the trio record, with the same predicate the structural half uses
(`isClosedUnitHandle`, `engine-driver-options.ts`). An `async: true`
`afterFind` run starts outside every open unit while its `ctx.api` still
carries the trio handle; before this, its cross-datasource business
write was refused although `buildDriverOptions` ran it outside any
transaction. It now lands on its own connection, as it does under
`transaction(cb)`. The ADR-0119 note gains an add-only paragraph
bounding "exactly the 2026-08-06 answers" (the detached run, and review
nit N1: a primitive handle stays unattributed); the changeset gains one
sentence. Nit N2 (a closed trio handle reverts to unattributed rather
than closed) is unchanged here; the seat files its follow-up.
## Why
Reading 6964984927 measured this on `origin/main`. The trio never
publishes into `txStore`, so both same-origin gates stepped aside.
plugin-audit's `api.sudo()` insert carried the body's primary handle to
the telemetry driver, and the statement ran on the primary connection,
which has no `sys_audit_log`. On a Postgres primary, the refused
statement turned COMMIT into a rollback.
## Tests
Pins (seat ruling) and their ablations, each run through
`scripts/ablation-replace.mjs`. Every ablation landed (anchor 1 to 0,
blob changed) and was restored (blob equals HEAD, `git diff HEAD`
empty). The dist legs were proved with `ablation-dist-preflight.mjs`
(`--absent` on mutate, present after the restore rebuild).
| Pin | Where | Ablation | Result |
|---|---|---|---|
| 1 engine: trio ledger rows land on telemetry with no foreign handle;
carve-out noted once; rollback keeps the orphan row |
`engine-transaction-same-origin.test.ts`, new describe "a handle the
ScopedContext trio opened is attributed to its owner (#35028)" | A: drop
the `recordTrioTransactionScope` call | 5 red of 28 |
| 1 runtime: action-body write in tx with plugin-audit on a split engine
keeps sys_audit_log + sys_activity |
`packages/qa/dogfood/test/action-body-transaction-audit-split.dogfood.test.ts`
(bootStack + real AuditPlugin + `telemetry` DriverPlugin, real
`/actions` door) | A on dist (objectql rebuilt) | 3 of 3 red |
| 1 runtime (knex better-sqlite3, os dev primary) |
`packages/runtime/src/sandbox/action-body-transaction-rollup-child.integration.test.ts`,
split case | A on dist | split case red |
| 2 single datasource: trio rides one handle, audit rolls back with it |
new nested describe in the same objectql file; existing :558 case kept,
comment reworded (title unchanged) | C: record a foreign owner | the
single-datasource trio case red (plus 4 split cases) |
| forget on commit / rollback | objectql file | B1 / B2: drop the forget
in commit / rollback | 1 red each |
| 3 cross-datasource business write refused, named | objectql file
(insert, update, delete) + dogfood (`400
ERR_CROSS_DATASOURCE_TRANSACTION_WRITE`, message names the object) | A |
red |
| 4 #34998 stall regression: body tx inserting a roll-up master-detail
child completes inside the wall clock on SQLite | runtime file, single +
split, bound 10 s | D: `transactionCoversDriverFor` returns false
(business writes leave the handle and wait on SQLite's one connection) |
both red at 30 s, with the #34998 signature: "exceeded wall-clock
ceiling of 30000ms while awaiting host calls (after 3597 pump
iterations)" |
| S1 detached `async` `afterFind` run: its cross-datasource business
write under an open trio handle lands, with no handle | objectql file,
"lets an async afterFind run, detached from the open trio handle, write
a business object on another datasource" | E: replace
`!this.isClosedUnitHandle(explicitTransaction)` with `true` | 1 red of
29: `CrossDatasourceTransactionWriteError` received instead of `stored`
|
| 5 live pg: an off-origin ledger write neither aborts the business tx
nor rolls back on commit | runtime file,
`describe.skipIf(!OS_TEST_POSTGRES_URL)`, pg primary + SQLite telemetry
| A on dist | both red: 25P02 "current transaction is aborted" for two
inserts; one insert answered 200 with 0 lines on disk (silent rollback)
|
S1 was measured before it was fixed: the new pin, run on `1ea3deab98`
with no code change, failed 1 of 29 with `FAILED
CrossDatasourceTransactionWriteError: insert of 'ledger' inside
transaction() resolves to datasource 'ledger_db', but the transaction is
open on 'primary'`. With the guard (`fe13609b7b`) the file passes 29 of
29. Ablation E ran through `ablation-replace.mjs` (anchor 1 to 0, blob
`9ed8195b6c32` to `c224861e208a`), went red as above, and was restored
(blob equals HEAD, `git diff HEAD` empty). A first attempt with a
replacement that was a substring of the anchor was refused by the tool
before any test ran. The objectql tests import the source, so no dist
leg applies.
Fix-round suites on head `91960c4f5a`:
- objectql `test`: 610 files / 13733 tests passed (`VERDICT command-exit
0`).
- objectql `typecheck`: exit 0.
Package suites on head `1ea3deab98` (previous round), all run in one
locked chain:
- objectql `test`: 610 files / 13732 tests passed (exit 0).
- objectql `typecheck`: exit 0.
- runtime `typecheck`: exit 0.
- Runtime pins plus the two neighbouring transaction suites, with live
pg: 3 files / 16 tests passed (exit 0).
## Live probe (re-run of the reader's probe, `os dev --fresh` on a probe
app)
"Before" is origin/main's objectql source (`1282a464e9`) rebuilt.
"After" is this branch.
| Layout | tx-1 / tx-2 audit | sys_audit_log rows | sys_activity rows |
`Audit write FAILED` lines |
|---|---|---|---|---|
| before, split (default) | NO AUDIT ROW / NO AUDIT ROW | 5 | 2 | 1 |
| after, split | create / create (+ rollback orphan row) | 8 | 4 | 0 |
| before, `OS_TELEMETRY_DB=0` | create / create | 7 | 4 | 0 |
| after, `OS_TELEMETRY_DB=0` | create / create (rollback leaves none) |
7 | 4 | 0 |
## Acceptance notes
- After the fix, the split layout keeps the rolled-back body
transaction's audit row as an orphan row. This is the 2026-08-06
carve-out ruling applied to the trio; the single-datasource layout still
rolls it back.
- Four mechanical call-site edits are outside the claim's file surface
(listed under What changes).
- The test surface of the query-options erasure ratchet stays at 236:
the new runtime file types its read options instead of using `as any`.
## Verification
Fix-round readings, all taken on head `91960c4f5a` (the final head).
Verified by repo:objectstack#1 · os-warren ·
session_01JQwxbXGvw43spR79ztEvWt. No merge from `main`: `overlap-check`
before each push merged cleanly against `origin/main` and the four PRs
sharing files (#35124, #34978, #34817, #34709), and `main` is not red.
Full build: `pnpm build --concurrency=2` under `os-verify-lock`, 120 of
120 tasks successful, `VERDICT command-exit 0`.
Full dogfood battery (owed: `!` title + `(narrowing)` arm), no shard, no
path filter:
<details><summary>pnpm --filter @objectstack/dogfood test · exit 0 · 480
files / 4075 tests passed</summary>
```text
Test Files 480 passed | 3 skipped (483)
Tests 4075 passed | 68 skipped (4143)
os-verify-lock: VERDICT command-exit 0 · held the lock 1777s (29m37s) · waited 0s
```
</details>
<details><summary>pnpm --filter @objectstack/dogfood typecheck · exit
0</summary>
```text
> tsc --noEmit
os-verify-lock: VERDICT command-exit 0 · held the lock 26s
```
</details>
Derived gate families (`node scripts/pm/dispatch-gates.mjs --commands
--repo objectstack-ai/objectstack`, run after the full build):
<details><summary>82 derived · 82 run · 82 exit 0 · 0 NOT
MEASURED</summary>
```text
Run reconciliation — 82 derived, 82 run, 0 NOT-MEASURED, 0 UNRUN.
✓ dispatch-gates --ran: 82 derived famil(ies) accounted for — 82 run, 0 NOT-MEASURED (a DERIVED zero — all 82 recorded an exit code and none of them is 3).
```
The derivation warned that the local `origin/main` ref was 17 commits
ahead of the merge base and that
`scripts/check-skills-token-ratchet.mjs` changed in that range; that
script is not one of this diff's families.
</details>
<details><summary>check-governed-merges --pr 35102 · exit 3 (GOVERNED,
the expected answer)</summary>
```text
governed-surface predicate: 1 of 12 path(s) hit the register (13 surfaces govern objectstack).
GOVERNED · landing tier: H — the maintainer's hand, or an authorized APPROVED review (GOVERNED_APPROVERS) and then the owning seat lands it.
docs/adr/** ×1 — docs/adr/0119-plugin-reachable-transactions-and-honest-atomic-batch.md
size: 793 changed line(s) (+758 / -35) ≤ 3000 — under the human-merge threshold.
```
</details>
CI on head `91960c4f5a`: `Lint & Repo Gates`, `TypeScript Type Check`,
`Build Core`, `Governed Surface Queue Guard`, `Live E2E UI`, `Test UI`,
`Dogfood Regression Gate` and `Check Changeset` concluded `success`;
`Test Core` 9 of 10 shards `success`. `Test Core (7/10)` failed on one
test this diff does not reach: `plugin-security`
`src/org-admin-assignment-boundary.posture.test.ts:1153`
(orgAdminAuditorDelegate, posture 'isolated'), "Test timed out in
5000ms", the known flake signature tracked on #35111; no plugin-security
path is in this diff. The `Lint & Repo Gates` and `TypeScript Type
Check` reds on `fe13609b7b` were cancellation aggregates: that run was
cancelled at 06:20Z when `91960c4f5a` was pushed, and every sub-lane
reads `cancelled`.
## Still owed (seat)
- The delta review of the S1 fix (contract review 6969574094 asked for
one).
- The follow-up card for review nit N2 (a closed trio handle reverts to
unattributed rather than closed).
- Tier H: os-zhuang's APPROVED review on the current head. No seat
readies, queues or arms this PR before it.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01WGufzfrh8TiPJf1i2kMSoa
<!-- signed: repo:objectstack#1 · os-warren ·
session_01JQwxbXGvw43spR79ztEvWt -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>1 parent 99a0e4f commit a6cf988
12 files changed
Lines changed: 758 additions & 35 deletions
File tree
- .changeset
- docs/adr
- packages
- objectql/src
- qa/dogfood/test
- runtime/src/sandbox
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
310 | 310 | | |
311 | 311 | | |
312 | 312 | | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
313 | 345 | | |
314 | 346 | | |
315 | 347 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
57 | 100 | | |
58 | 101 | | |
59 | 102 | | |
| |||
71 | 114 | | |
72 | 115 | | |
73 | 116 | | |
| 117 | + | |
74 | 118 | | |
75 | 119 | | |
76 | 120 | | |
| |||
247 | 291 | | |
248 | 292 | | |
249 | 293 | | |
250 | | - | |
| 294 | + | |
251 | 295 | | |
252 | 296 | | |
253 | 297 | | |
| 298 | + | |
254 | 299 | | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
259 | 313 | | |
260 | 314 | | |
261 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
341 | 341 | | |
342 | 342 | | |
343 | 343 | | |
344 | | - | |
| 344 | + | |
345 | 345 | | |
346 | 346 | | |
347 | 347 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
415 | 416 | | |
416 | 417 | | |
417 | 418 | | |
418 | | - | |
419 | | - | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
420 | 428 | | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
| 429 | + | |
428 | 430 | | |
429 | 431 | | |
430 | 432 | | |
| |||
437 | 439 | | |
438 | 440 | | |
439 | 441 | | |
440 | | - | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
441 | 446 | | |
442 | 447 | | |
443 | 448 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| |||
334 | 335 | | |
335 | 336 | | |
336 | 337 | | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
337 | 349 | | |
338 | 350 | | |
339 | 351 | | |
340 | 352 | | |
341 | 353 | | |
342 | 354 | | |
343 | 355 | | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
348 | 365 | | |
349 | 366 | | |
350 | 367 | | |
351 | 368 | | |
352 | 369 | | |
353 | 370 | | |
354 | 371 | | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
359 | 381 | | |
360 | 382 | | |
361 | 383 | | |
| |||
0 commit comments