Repository navigation
Commit 8071f35
fix(automation): a record-change flow's run history and notification deliveries land when the triggering write runs in a unit of work (#37742)
<!-- claude-projects-attribution: {"github_login":"os-sales"} -->
_Requested by **Sales** · [project
thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4UuT6upE7mQVRYxkhAeA5UjwM)_
Fixes #37705
Clause-②: yes (widening) — `ObjectQL` is exported from the
`@objectstack/objectql` entry, and it gains two public methods,
`runOutsideOpenUnits(body)` and `runAfterOpenUnitsCommit(body)`;
`@objectstack/core` gains the root export
`wakeAfterOpenUnitsCommit(engine, wake)`; the
`@objectstack/service-automation` interface `SuspendedRunStoreEngine`
gains the matching optional member `runOutsideOpenUnits`, so an engine
that does not supply it still satisfies the interface. No existing
signature or accepted input narrows. `DispatchLoop`'s change is to the
async context its ticks run in, and the dispatchers' change is to when a
wake is sent, not to any API. The level stays `patch` because nothing a
metadata author can write changes (`Metadata-protocol: none`).
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:记录变更触发的流程(例如 hotcrm
的线索分配)跑完后,运行历史和通知投递、活动记录不再丢失;运行历史会在保存返回后片刻落库,而不是和那次保存同时落库。
- 为什么做:hotcrm 移植到框架 19 被这个问题挡住,6 个线索分配测试失败;任何在保存时带钩子的对象上跑流程的应用都会丢运行历史。
- 出错会怎样:最坏是运行历史晚一点出现或重新丢失,业务数据本身不受影响;改动可直接回滚。
- 要你做什么:无。
## What changes
On 19.0.0-next.0 (and on `main` at `0f8e13b0`), a record-change flow
fired by a write whose object carries a metadata `before*` hook, or a
blocking `after*` hook that can abort, runs inside the unit of work the
write door opens for that write (`inAfterAbortUnit`). Two pieces of work
the run starts and does not await then took that unit's transaction
handle while it was still open, and ran their statements after it had
committed, so knex answered "Transaction query already complete":
1. **The terminal run-history write**
(`ObjectStoreSuspendedRunStore.recordTerminal`, fired and forgotten from
`run-history.ts`). Its first `find` on `sys_automation_run` failed, so
the run's history row never landed.
2. **The notification dispatcher's tick**, started by the `notify`
node's outbox wake (`DispatchLoop.wake()`) in the same async context.
Its delivery updates failed, and so did the `sys_activity` audit rows
they fire; the timer each tick sets inherited the same context, so later
ticks did too.
The fix, one mechanism per producer:
- `packages/objectql/src/engine.ts`: new public
`ObjectQL.runOutsideOpenUnits(body)`. It runs work its caller starts and
does not await outside every unit open at the call, through the same
`runOutsideOpenUnits` exit an `async: true` hook's run already takes
(`unit-detach.ts`): out of the ambient entry, with that entry's handle
recorded as detached.
- `packages/services/service-automation/src/suspended-run-store.ts`:
`recordTerminal` runs its whole write (the read, the insert or update,
the retention prune) through the engine's `runOutsideOpenUnits`.
`SuspendedRunStoreEngine` gains it as an optional member, so a double
without units keeps running the write where it is.
- `packages/core/src/dispatch-loop.ts`: `DispatchLoop` (the
notification, HTTP-delivery, database-queue and audit-seal loops) runs
every tick, its due-time read and the timer for the next tick in the
async context it was constructed in, never the context of the caller of
`start()`, `wake()` or `wakeAt()`. Its docblock now states that a loop
is constructed at boot, and names `DbQueueAdapter.start()` reached from
`subscribe()` under `autoStart` (review F-5).
- **The wake waits for the commit (review F-2, option a).** A tick woken
inside a unit now runs outside it, and on a pooled driver (Postgres,
MySQL) it cannot see the outbox or queue row the unit has not committed
yet, so that row would wait for a backed-off tick (up to
`maxIdleIntervalMs`, 30 s, under `soleWriter`). New public
`ObjectQL.runAfterOpenUnitsCommit(body)` (`unit-detach.ts`) runs `body`
outside the units once the ambient unit commits, never when it rolls
back, at once when no unit is open, on the same `UnitCommitQueue` the
write events' `async` runs wait on (#33760).
`wakeAfterOpenUnitsCommit(engine, wake)` (new, `@objectstack/core`)
sends a wake through it, or at once on an engine without it. Wired at
the notification and HTTP outboxes' `onEnqueued`
(`messaging-service-plugin.ts`) and at the DB queue's due `publish` and
`replay` (`db-queue-adapter.ts`). A deferred `publish` keeps `wakeAt` (a
timer hint, no read now). A `ScopedContext` `beginTransaction` trio unit
holds the wake back while an engine call carrying its handle runs
(`publishTrioHandleFor` publishes the handle with the trio's queue); a
wake made outside such a call, by code that holds the trio handle only
explicitly, goes at once and on a pooled driver waits one backed-off
tick (review F-6, accepted). `engine.ts` stays within its line ceiling
(6856 of 6857): the `[DETACHED_HOOK_RUN]` docblock lost one line,
wording only.
Behaviour a reader can see: a run's history row is written beside the
run, not inside the triggering write's unit. It is not rolled back with
that write, and it can land a moment after the write has returned, as it
already could for any run outside a unit (the run-history write was
never awaited). A test that reads a run's history once, straight after
the write that fired it, has to wait for it to land instead.
Branch: `claude/project-thread-a7kaf6`, the name the harness assigned
this card thread, not `claude/issue-37705-*`.
## Tests
- `packages/objectql/src/engine-scoped-transaction-late-write.test.ts`,
new top-level describe `#37705`: a driver that refuses a finished handle
as knex does, and holds a read until after the commit. CONTROL: a read
started bare inside `engine.transaction()` took the unit's handle and
fails with "Transaction query already complete". The same read through
`runOutsideOpenUnits` reaches the driver with no handle and answers. A
unit the detached body opens is its own.
-
`packages/services/service-automation/src/suspended-run-store.test.ts`,
new describe `#37705`: every statement of the terminal write (find,
insert, update, the prune's delete) runs inside `runOutsideOpenUnits`;
an engine without it still gets the row.
- `packages/core/src/dispatch-loop.test.ts`, new describe `#37705`: a
tick, its due-time read and the next timer tick see none of an
`AsyncLocalStorage` store the caller of `wake()`, `start()` or
`wakeAt()` was in. A second describe: `wakeAfterOpenUnitsCommit` hands
the wake to the engine's `runAfterOpenUnitsCommit`, and wakes at once
without it.
- `packages/objectql/src/engine-scoped-transaction-late-write.test.ts`,
second `#37705` describe: inside a unit the signal runs after the commit
(`body done`, `commit`, `signal`); a read it starts reaches the driver
with no handle; a rolled-back unit sends none; with no unit it runs
before the call returns.
- `packages/services/service-queue/src/db-queue-idle-backoff.test.ts`,
new describe `#37705`: under an engine that holds the signal as an open
unit does, a due `publish` runs no tick until the commit and then
delivers within the base interval; a `replay` hands its wake over too.
-
`packages/services/service-messaging/src/plugin-enqueue-wakes-dispatcher.test.ts`,
new describe `#37705` on the composed plugin and a real engine: an
`emit()` inside `objectql.transaction()` hands its wake to
`runAfterOpenUnitsCommit` once, nothing is sent while the unit is open,
and the delivery goes out after the commit.
Reverse verification (fix committed first, then each source file put
back to its `main` body, `engine.ts` by making the method `return
body()`): the two `#37705` cases in each of the three files go red (6 of
6), the control stays green, and restoring HEAD gives `git diff HEAD`
empty. Round 2 at 92c84c3, the same way: `runAfterOpenUnitsCommit`
running at once (2 red: commit order, rollback),
`wakeAfterOpenUnitsCommit` never deferring (1 red), the queue's two
wakes back to `this.wake()` (2 red), the notification outbox's
`onEnqueued` back to a bare wake (1 red); 6 of 6, `git diff HEAD` empty
after each.
<details><summary>Local runs (tree: objectstack-ai/objectstack, head
f94bcab; 62064b5 below)</summary>
- `pnpm --filter @objectstack/core test`: Tests 3797 passed (3797);
`typecheck` exit 0.
- `pnpm --filter @objectstack/objectql test`: Tests 14435 passed
(14435); `typecheck` exit 0.
- `pnpm --filter @objectstack/service-messaging test`: Tests 1108 passed
(1108); `typecheck` exit 0.
- `pnpm --filter @objectstack/service-queue test`: Tests 93 passed (93);
`typecheck` exit 0.
- `pnpm --filter @objectstack/service-automation test`: Tests 4322
passed (4322); `typecheck` exit 0.
- All under `os-verify-lock.sh` (VERDICT command-exit 0), after a `turbo
run build` of the five packages with declarations.
- `dispatch-gates.mjs --commands` at f94bcab derived 79 families;
`--ran` reconciles 79: 78 run, all exit 0
(`check:facade-delegation-only`, `check:query-options-erasure`,
`check:dts-closure` and `check:dual-build-cjs-loads` included), 1 NOT
MEASURED (exit 3, prerequisite not met in this shallow tree:
`check-plugin-teardown-shape --self-test`).
- `check-adr-0087-registration`, `check-changeset-no-major`,
`check-empty-changeset` green.
- At 62064b5 (docs and comment wording only: `webhooks.mdx`, the
`unit-detach.ts` docblock, the changeset): `dispatch-gates --commands`
derives 110 families, the 31 the docs page adds all run and exit 0,
`check:facade-delegation-only` and `check:query-options-erasure` re-ran
green, and `engine-scoped-transaction-late-write.test.ts` re-ran green.
- Full dogfood battery: not owed (no `!` in the title, no `(narrowing)`
arm). Round 1's sweep files were run at aed7894 (table above); round 2
changes when a wake is sent, not what the swept tests read.
</details>
## Read-after-write sweep (seat ask)
What moved: only a run's terminal history row in `sys_automation_run`,
and only for a run fired inside a unit. `listRuns`, `listRunsPage` and
`getRun` answer from the in-memory run log first (`run-history.ts`), so
an in-process read through them sees the run as soon as it finishes, as
before. Delivery rows (and the `sys_activity` rows their updates fire)
for a wake made inside a unit go out once the unit commits, through the
commit-time wake above, on every driver, except a wake made outside an
engine call that carries a trio handle (review F-6); before review F-2
this sentence held only on a single-connection pool. What can now miss
is a test that reads the durable row itself
(`ql.find('sys_automation_run')`, `/data/sys_automation_run`,
`loadTerminal`) once, straight after the write.
Checked against this branch's build (every file green, no edit needed):
| File | Reads | Verdict |
|---|---|---|
|
`packages/qa/dogfood/test/install-local-uninstall-flows-withdrawn.dogfood.test.ts`
| durable rows | already a bounded wait (`settledRunCounts`, 10 s) |
|
`packages/plugins/plugin-approvals/src/save-notice-approval-round-trip.integration.test.ts`
| durable terminal row | already a bounded wait (100 × 20 ms) |
|
`packages/qa/dogfood/test/flow-shipped-name-stored-row-boot.dogfood.test.ts`
| `listRuns` | already a bounded wait (50 × 100 ms) |
|
`packages/qa/dogfood/test/flow-trigger-record-credential-mask.dogfood.test.ts`,
`flow-durable-suspend.dogfood.test.ts` | durable `paused` rows | the
pause write is awaited, unchanged |
| `packages/qa/dogfood/test/flow-name-per-namespace.dogfood.test.ts`,
`automation-stored-reference-package-column.dogfood.test.ts` | rows the
test seeds itself; `getRun` | not a run's terminal write |
| `packages/qa/dogfood/test/flow-condition-scope-keys`,
`flow-door-elevated-start`, `seed-ownership-claim-dispatch`
(`.dogfood.test.ts`) | `listRuns` / resume door | in-memory log |
| `packages/verify/src/action-flow-elevated-door`,
`automation-trigger-elevated-door`, `handle.flows-system-start`,
`handle.public-form-door`, `handle.system-insert-delete`,
`handle.update-doors` (`.test.ts`) | `listRuns` | in-memory log |
|
`packages/plugins/plugin-approvals/src/batch-item-recall-resumes-batch.test.ts`
| `getRun` | in-memory log |
| `packages/runtime/src/domains/automation-run-detail-passthrough`,
`automation-screen-read-gate`, `automation-resume-caller-gate`,
`automation-run-read-permission-gate`,
`automation-write-capability-gate` (`.test.ts`) | stubbed engine | no
store |
|
`packages/plugins/organizations/src/elevated-organization-wall.postures.wall.test.ts`
| flow side effects | green |
| `examples/app-todo/test/task-completion-trigger.test.ts`,
`task-recurrence.test.ts`;
`examples/app-showcase/test/approver-bindings-specimen-unarmed.test.ts`
| `listRuns` | in-memory log |
| `packages/services/service-automation/**` | all | full package suite
green (4322) |
Runs: dogfood 51 + 26, verify 70, runtime 25, organizations 10,
plugin-approvals 3 + 10, example-todo 27, example-showcase 4, all
passed. Found by grepping `packages/` and `examples/` test files for
`sys_automation_run`, `listRuns`, `getRun`, `loadTerminal`, `/runs` and
`sys_activity` next to a record-change trigger.
**hotcrm** (not in this PR): in PR #2820's
`test/flow-record-change.test.ts`, the three `case_escalation` cases at
lines 205, 263 and 787 read the durable row once and must switch to the
file's own `landedRuns` wait (details under Acceptance notes).
## Acceptance notes
- **hotcrm.** hotcrm PR #2820's head `03a2647`, with every
`@objectstack/*` package except `console` swapped for this branch's
build: `test/flow-record-change.test.ts` answers 6 failed / 50 passed on
`main` (`0f8e13b0`, the same 6 as on `19.0.0-next.0`) and 0 "Transaction
query already complete" lines with the fix. Three `case_escalation`
cases (lines 205, 263 and 787 there) then read the run's history once,
straight after the write, and find nothing yet: on `main` the row was
written inside the triggering write's unit and was visible when the
write returned. With those three reads switched to the file's own
`landedRuns` (wait for the row), the file is 56 / 56. That three-line
change belongs to the hotcrm port, not to this PR.
- A framework end-to-end twin of the hotcrm run was not kept: a booted
fixture (record-after-create flow, `update_record`, a metadata `before*`
and blocking `after*` hook, `AuditPlugin`) did not reproduce the race on
SQLite, because nothing there held the history read between its handle
and its statement. The engine-level pin holds that step by construction
instead.
- Accepted deviations from review: F-1, the terminal history row
outlives a rolled-back triggering write. F-7, an outbox or queue table
on a datasource the open unit does not cover commits its row on its own
connection while the wake rides the unit's queue, so a rollback drops
that wake and the row goes out on the next backed-off tick (at most
`maxIdleIntervalMs`); no in-tree composition was measured that way.
- Docs: `content/docs/automation/webhooks.mdx` now says a delivery
`enqueueHttp()` or `redeliverHttp()` writes inside a transaction goes
out at its commit, and not at all on a rollback (review F-9).
- Same class elsewhere: any other work a run or a hook starts inside a
unit and does not await can take the unit's handle the same way. This PR
fixes the two producers the hotcrm run measured;
`ObjectQL.runOutsideOpenUnits` is the exit for the next one.
## Verification
Two-minute check: `pnpm --filter @objectstack/objectql exec vitest run
src/engine-scoped-transaction-late-write.test.ts -t 37705`, which shows
the control failing with "Transaction query already complete" and the
detached read answering.
Read first:
1. `packages/services/service-automation/src/suspended-run-store.ts`
`recordTerminal`: the write leaves the caller's units; it no longer
commits or rolls back with the triggering write.
2. `packages/core/src/dispatch-loop.ts` `loopTickAtHome` / `wakeAt`:
every loop's ticks now run in the context the loop was constructed in;
`wakeAfterOpenUnitsCommit` and the `unit-detach.ts`
`runAfterOpenUnitsCommit` it reaches: a wake made in a unit goes at its
commit.
3. `packages/objectql/src/engine.ts` `runOutsideOpenUnits`: the new
public engine member.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq
<!-- signed: repo:objectstack#2 · os-sales ·
session_01L6bhgX3p4cHBBQ11Sjokrq -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 3ed97b7 commit 8071f35
13 files changed
Lines changed: 543 additions & 18 deletions
File tree
- .changeset
- content/docs/automation
- packages
- core/src
- objectql/src
- services
- service-automation/src
- service-messaging/src
- service-queue/src
| 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 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
495 | 495 | | |
496 | 496 | | |
497 | 497 | | |
498 | | - | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
499 | 501 | | |
500 | 502 | | |
501 | 503 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | | - | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
292 | 293 | | |
293 | 294 | | |
294 | 295 | | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 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 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
35 | 57 | | |
36 | 58 | | |
37 | 59 | | |
| |||
60 | 82 | | |
61 | 83 | | |
62 | 84 | | |
| 85 | + | |
| 86 | + | |
63 | 87 | | |
64 | 88 | | |
65 | 89 | | |
| |||
102 | 126 | | |
103 | 127 | | |
104 | 128 | | |
| 129 | + | |
| 130 | + | |
105 | 131 | | |
106 | 132 | | |
107 | 133 | | |
| |||
144 | 170 | | |
145 | 171 | | |
146 | 172 | | |
147 | | - | |
| 173 | + | |
148 | 174 | | |
149 | 175 | | |
150 | 176 | | |
| |||
166 | 192 | | |
167 | 193 | | |
168 | 194 | | |
169 | | - | |
| 195 | + | |
170 | 196 | | |
171 | 197 | | |
172 | 198 | | |
| |||
186 | 212 | | |
187 | 213 | | |
188 | 214 | | |
189 | | - | |
| 215 | + | |
190 | 216 | | |
191 | 217 | | |
192 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
193 | 230 | | |
194 | 231 | | |
195 | 232 | | |
| |||
305 | 342 | | |
306 | 343 | | |
307 | 344 | | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
308 | 358 | | |
309 | 359 | | |
310 | 360 | | |
| |||
0 commit comments