Repository navigation
fix(automation): a record-change flow's run history and notification deliveries land when the triggering write runs in a unit of work - #37742
Conversation
…ck run outside the unit that started them A record-change flow finishes inside the unit of work the write door opens for the triggering write's after-hooks. Its terminal run-history write is not awaited, so its first read took that unit's handle and ran after the unit had committed: knex answered "Transaction query already complete" and the row never landed. A notify node's outbox wake started the dispatcher's tick in the same unit, and its delivery and audit writes failed the same way. - ObjectQL.runOutsideOpenUnits(body): run work the caller does not await outside every open unit (the exit an async: true hook's run takes). - ObjectStoreSuspendedRunStore.recordTerminal runs its write through it. - DispatchLoop runs every tick in its own async context, never the waker's. Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
…s unit Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
…pin's options The contract for ObjectQL.runOutsideOpenUnits now lives on the function it delegates to in unit-detach.ts, and the #37705 pins pass typed options. Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
📓 Docs Drift CheckThis PR changes 5 package(s): 5 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:
⛔ 2 release-owned page(s) also name something this change touched. These are read-only:
What this run could not see
Coarse fallback — 54 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): Which tree this was computed onThis run read A worktree cut from an older # while this PR is open — GitHub drops the merge commit once it closes
git fetch origin b9e8d9d8c077fba5f19c6f36d407919220445945 && git checkout b9e8d9d8c077fba5f19c6f36d407919220445945
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 247382446d968e3b39635ed416275581f09117f2 62064b52334ae7f6452474523247b1469c32c392 && git checkout -B drift-repro 247382446d968e3b39635ed416275581f09117f2 && git merge --no-ff 62064b52334ae7f6452474523247b1469c32c392
node scripts/docs-audit/affected-docs.mjs --json 247382446d968e3b39635ed416275581f09117f2
|
…lause-② widening (#37705) ObjectQL is exported from the @objectstack/objectql entry, so the new public method widens it, and SuspendedRunStoreEngine gains the matching optional member. The level stays patch (Metadata-protocol: none). Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
|
repo:objectstack#2 · os-sales · session_01L6bhgV6i8jfWwSXjpWJrjA Contract reviewServed-tier: Face: declaration ( ① Derived judgments
② Semver level
③ Boundary flags
Implemented-by: VERDICT: PASS |
…t that wrote its row (#37705) A wake made inside a unit of work now runs its tick outside the unit, and on a pooled driver that tick cannot see the row the unit has not yet committed, so the row waited for a backed-off tick. The wake now goes at the unit's commit through the new ObjectQL.runAfterOpenUnitsCommit, and not at all when the unit rolls back: the notification and HTTP outboxes' onEnqueued and the DB queue's publish and replay route through core's wakeAfterOpenUnitsCommit. The DispatchLoop docblock also states that a loop is constructed at boot. Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
…the boot-construction rule (#37705) Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
|
repo:objectstack#2 · os-sales · session_01L6bhgV6i8jfWwSXjpWJrjA Contract reviewServed-tier: This is the delta round on my round-1 record (7207454847, head Gate line: CI on this head is still running at review time. 13 checks succeeded, 12 were skipped and 23 are in progress, including Test Core 1-10, Build Core, the Type Check and Lint lanes, and Live E2E UI. The one ① Derived judgments
② Semver level
③ Boundary flags
Implemented-by: VERDICT: PASS |
…ommit; tighten the commit-time wake's contract (#37705) webhooks.mdx said an enqueued or redelivered HTTP delivery goes out at once; inside a transaction it now goes at the commit and not at all on a rollback. runAfterOpenUnitsCommit's docblock now states how a body's throw surfaces, that a trio unit holds the wake while an engine call carrying its handle runs, and that nothing in the body is atomic with the write. Claude-Session: https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq Co-authored-by: Claude <noreply@anthropic.com>
|
repo:objectstack#2 · os-sales · session_01L6bhgV6i8jfWwSXjpWJrjA Contract reviewServed-tier: This is delta round 3 on my round-2 record (7208126545, head I read four things:
Gate line: CI on this head is still running at review time. 9 checks have succeeded, 11 were skipped and 21 are in progress, including Build Core, every Type Check and Lint lane, Check Changeset and Live E2E UI. Test Core and Test UI have not started. The run on ① Derived judgments
② Semver level
③ Boundary flags
Implemented-by: VERDICT: PASS |
|
repo:objectstack#2 · os-sales · session_01L6bhgV6i8jfWwSXjpWJrjA 席位核对 — ACCEPT · head 62064b5 · card #37705 · area s2-data · 2026-11-27T03:04ZL1 checklist
Tier: S. Landing: ready + auto-merge by this stroke. |
Requested by Sales · project thread
Fixes #37705
Clause-②: yes (widening) —
ObjectQLis exported from the@objectstack/objectqlentry, and it gains two public methods,runOutsideOpenUnits(body)andrunAfterOpenUnitsCommit(body);@objectstack/coregains the root exportwakeAfterOpenUnitsCommit(engine, wake); the@objectstack/service-automationinterfaceSuspendedRunStoreEnginegains the matching optional memberrunOutsideOpenUnits, 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 stayspatchbecause nothing a metadata author can write changes (Metadata-protocol: none).Metadata-protocol: none
维护者速读
What changes
On 19.0.0-next.0 (and on
mainat0f8e13b0), a record-change flow fired by a write whose object carries a metadatabefore*hook, or a blockingafter*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":ObjectStoreSuspendedRunStore.recordTerminal, fired and forgotten fromrun-history.ts). Its firstfindonsys_automation_runfailed, so the run's history row never landed.notifynode's outbox wake (DispatchLoop.wake()) in the same async context. Its delivery updates failed, and so did thesys_activityaudit 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 publicObjectQL.runOutsideOpenUnits(body). It runs work its caller starts and does not await outside every unit open at the call, through the samerunOutsideOpenUnitsexit anasync: truehook'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:recordTerminalruns its whole write (the read, the insert or update, the retention prune) through the engine'srunOutsideOpenUnits.SuspendedRunStoreEnginegains 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 ofstart(),wake()orwakeAt(). Its docblock now states that a loop is constructed at boot, and namesDbQueueAdapter.start()reached fromsubscribe()underautoStart(review F-5).maxIdleIntervalMs, 30 s, undersoleWriter). New publicObjectQL.runAfterOpenUnitsCommit(body)(unit-detach.ts) runsbodyoutside the units once the ambient unit commits, never when it rolls back, at once when no unit is open, on the sameUnitCommitQueuethe write events'asyncruns wait on (objectql: run async after-hooks on commit of the triggering unit (drop on rollback) #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 duepublishandreplay(db-queue-adapter.ts). A deferredpublishkeepswakeAt(a timer hint, no read now). AScopedContextbeginTransactiontrio unit holds the wake back while an engine call carrying its handle runs (publishTrioHandleForpublishes 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.tsstays 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, notclaude/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 insideengine.transaction()took the unit's handle and fails with "Transaction query already complete". The same read throughrunOutsideOpenUnitsreaches 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 insiderunOutsideOpenUnits; 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 anAsyncLocalStoragestore the caller ofwake(),start()orwakeAt()was in. A second describe:wakeAfterOpenUnitsCommithands the wake to the engine'srunAfterOpenUnitsCommit, and wakes at once without it.packages/objectql/src/engine-scoped-transaction-late-write.test.ts, second#37705describe: 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 duepublishruns no tick until the commit and then delivers within the base interval; areplayhands its wake over too.packages/services/service-messaging/src/plugin-enqueue-wakes-dispatcher.test.ts, new describe#37705on the composed plugin and a real engine: anemit()insideobjectql.transaction()hands its wake torunAfterOpenUnitsCommitonce, 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
mainbody,engine.tsby making the methodreturn body()): the two#37705cases in each of the three files go red (6 of 6), the control stays green, and restoring HEAD givesgit diff HEADempty. Round 2 at 92c84c3, the same way:runAfterOpenUnitsCommitrunning at once (2 red: commit order, rollback),wakeAfterOpenUnitsCommitnever deferring (1 red), the queue's two wakes back tothis.wake()(2 red), the notification outbox'sonEnqueuedback to a bare wake (1 red); 6 of 6,git diff HEADempty after each.Local runs (tree: objectstack-ai/objectstack, head f94bcab; 62064b5 below)
pnpm --filter @objectstack/core test: Tests 3797 passed (3797);typecheckexit 0.pnpm --filter @objectstack/objectql test: Tests 14435 passed (14435);typecheckexit 0.pnpm --filter @objectstack/service-messaging test: Tests 1108 passed (1108);typecheckexit 0.pnpm --filter @objectstack/service-queue test: Tests 93 passed (93);typecheckexit 0.pnpm --filter @objectstack/service-automation test: Tests 4322 passed (4322);typecheckexit 0.os-verify-lock.sh(VERDICT command-exit 0), after aturbo run buildof the five packages with declarations.dispatch-gates.mjs --commandsat f94bcab derived 79 families;--ranreconciles 79: 78 run, all exit 0 (check:facade-delegation-only,check:query-options-erasure,check:dts-closureandcheck:dual-build-cjs-loadsincluded), 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-changesetgreen.webhooks.mdx, theunit-detach.tsdocblock, the changeset):dispatch-gates --commandsderives 110 families, the 31 the docs page adds all run and exit 0,check:facade-delegation-onlyandcheck:query-options-erasurere-ran green, andengine-scoped-transaction-late-write.test.tsre-ran green.!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.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,listRunsPageandgetRunanswer 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 thesys_activityrows 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):
packages/qa/dogfood/test/install-local-uninstall-flows-withdrawn.dogfood.test.tssettledRunCounts, 10 s)packages/plugins/plugin-approvals/src/save-notice-approval-round-trip.integration.test.tspackages/qa/dogfood/test/flow-shipped-name-stored-row-boot.dogfood.test.tslistRunspackages/qa/dogfood/test/flow-trigger-record-credential-mask.dogfood.test.ts,flow-durable-suspend.dogfood.test.tspausedrowspackages/qa/dogfood/test/flow-name-per-namespace.dogfood.test.ts,automation-stored-reference-package-column.dogfood.test.tsgetRunpackages/qa/dogfood/test/flow-condition-scope-keys,flow-door-elevated-start,seed-ownership-claim-dispatch(.dogfood.test.ts)listRuns/ resume doorpackages/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)listRunspackages/plugins/plugin-approvals/src/batch-item-recall-resumes-batch.test.tsgetRunpackages/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)packages/plugins/organizations/src/elevated-organization-wall.postures.wall.test.tsexamples/app-todo/test/task-completion-trigger.test.ts,task-recurrence.test.ts;examples/app-showcase/test/approver-bindings-specimen-unarmed.test.tslistRunspackages/services/service-automation/**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/andexamples/test files forsys_automation_run,listRuns,getRun,loadTerminal,/runsandsys_activitynext to a record-change trigger.hotcrm (not in this PR): in PR #2820's
test/flow-record-change.test.ts, the threecase_escalationcases at lines 205, 263 and 787 read the durable row once and must switch to the file's ownlandedRunswait (details under Acceptance notes).Acceptance notes
03a2647, with every@objectstack/*package exceptconsoleswapped for this branch's build:test/flow-record-change.test.tsanswers 6 failed / 50 passed onmain(0f8e13b0, the same 6 as on19.0.0-next.0) and 0 "Transaction query already complete" lines with the fix. Threecase_escalationcases (lines 205, 263 and 787 there) then read the run's history once, straight after the write, and find nothing yet: onmainthe 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 ownlandedRuns(wait for the row), the file is 56 / 56. That three-line change belongs to the hotcrm port, not to this PR.update_record, a metadatabefore*and blockingafter*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.maxIdleIntervalMs); no in-tree composition was measured that way.content/docs/automation/webhooks.mdxnow says a deliveryenqueueHttp()orredeliverHttp()writes inside a transaction goes out at its commit, and not at all on a rollback (review F-9).ObjectQL.runOutsideOpenUnitsis 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:
packages/services/service-automation/src/suspended-run-store.tsrecordTerminal: the write leaves the caller's units; it no longer commits or rolls back with the triggering write.packages/core/src/dispatch-loop.tsloopTickAtHome/wakeAt: every loop's ticks now run in the context the loop was constructed in;wakeAfterOpenUnitsCommitand theunit-detach.tsrunAfterOpenUnitsCommitit reaches: a wake made in a unit goes at its commit.packages/objectql/src/engine.tsrunOutsideOpenUnits: the new public engine member.Generated by Claude Code
🤖 Generated with Claude Code
https://claude.ai/code/session_01L6bhgX3p4cHBBQ11Sjokrq