Skip to content

Commit d0bce70

Browse files
fix(plugin-approvals): an approval step opens no request on a run another branch already ended (#39064)
Fixes #38985 Clause-②: yes (widening) Metadata-protocol: none The widening, two additions to `@objectstack/plugin-approvals`: the exported `ApprovalAutomationSurface` gains an optional member, `getRun?: ApprovalResumeSurface['getRun']` (L1 7337799728); and the exported `ApprovalService` gains a public method, `abandonAtRefusedStep({ runId, nodeId, object, recordId, runStatus, rootRunId? })`, meant for the approval step only (seat ruling 7338562363). ## 维护者速读 - 用户能看到什么变化:审批流程在审批后分成多条同时运行的分支、其中一条已经失败时,另一条分支走到下一个审批步骤不再发起新的审批请求,下一位审批人不再收到通知,记录也不会一直挂在"审批中",而是在所有对象上都判作废并退回(recalled),第一位审批人的通过记录保持原样。 - 为什么做:这张孤儿审批卡的第二阶段(插件这一侧),落实你"分支审批中一条分支失败判作废、不判通过"的裁决;原先要等后台清理任务(默认 5 分钟,没有任务服务的环境永远不会)才收回这张多余的请求。 - 出错会怎样:只影响分支并行、且一条分支已让运行结束后,另一条分支才走到的审批步骤;运行中的流程、正在恢复的流程、修复后重新续跑的流程都照旧发起请求,已用测试守住;本改动可直接撤回,撤回后回到"先发起、再由清理任务收回"的旧行为。 - 要你做什么:无。 ## What changes `@objectstack/plugin-approvals` only, phase 2 of this card on the seat's phase-1 pick (the plugin side, 7323394875) and the maintainer's ruling 7329450068 (a failed fan-out is abandoned, never approved). It builds on the fan-out judge from #38988 (PR #39013, merged at `a0ee7d26` and merged into this branch at `36b9c4b5`). **Landing order:** this PR must not land before #39013; that holds now, since #39013 is on main. On a tree without that judge the same refusal leaves `approved` on the record (measured, below), and the two outcome cells of the new test file go red there, so the pins hold the order too. `approval-node.ts`: before it opens a request, the approval step reads its run's log entry (`getRun`, now on `ApprovalAutomationSurface` with the type `ApprovalResumeSurface` already declares). It refuses only where both hold: - the run's status is terminal by `RUN_STATUS_LIVENESS` (the classification the dead-run sweep acts on); - the run logged a step completed after its own `completedAt`, strictly: the engine going on with another branch after a failure (or a refusing `end`) ended the run, the same "late step" the #38691 and #38988 readers use. The refusal is answered the way the node answers its other refusals: `{ success: false, error }`, a failed step naming the run and how it ended, plus an `info` log line. No request row, no notice, no approver index row. The run keeps the status and error it ended with (pinned: `run.error` stays `note failed`). Why the second fact: status alone was the first cut and it broke restored continuations. A repaired strand is resumed again while `getRun` still answers the strand's durable terminal row (a restore records `paused` in the ring, but the terminal row wins once the resume has consumed the pause). Measured with status alone: `restored-approval-continuation.test.ts` PIN 7 and PIN 8 and `stranded-resubmit-second-door.test.ts` MEASUREMENT A and C went red (`Approval node 'a2': its run '…' had already ended failed`). The durable row is a snapshot taken at the end and carries no step after it (probed: the row holds `start, first, fork, note`; the ring entry holds `… note, mid, second`), so with the second fact those replays open their step again. Every other answer opens the step as before: live status (a run being resumed onto the step reads `paused`), unknown status, no entry, no readable end, no `getRun`, a read that throws. ## Tests New file `approval-node-ended-run-38985.test.ts`: real kernel, ObjectQL, automation engine, approval node, driver-sql on sqlite, the card's flow `fork_fail_side` (`first` approved, `fork` to `note` that fails at 30 ms and `mid` that calls a slow child and reaches `second`), on an object that closes on its approval. <details><summary>Red first on main (test commit 8011f64 over base ff491c4)</summary> - both fan-out cells (the door judges / no door, the reconciler judges): `AssertionError: no request row for \`second\`: expected [ 'first:approved', 'second:pending' ] to deeply equal [ 'first:approved' ]` - the three controls green: `Tests 2 failed | 3 passed (5)` </details> <details><summary>The outcome needs #38988's judge (measured before #39013 merged)</summary> - fix on main without the judge: `AssertionError: expected 'approved' to be 'recalled'` on both cells (door: no judgement; reconciler: `judged: { finished: 1 }`). - fix with the judge (a trial merge of #39013's head `b7ecc146`): door and reconciler write `recalled`, the timeline reads `ended failed while its branch at node 'mid', from which a step can still follow, had not finished, between two steps`. </details> <details><summary>Green at 36b9c4b (merged with origin/main a0ee7d2)</summary> - `pnpm --filter @objectstack/plugin-approvals exec vitest run --maxWorkers=2`: `Test Files 147 passed (147)`, `Tests 2327 passed (2327)`, including `restored-approval-continuation`, `stranded-resubmit-second-door`, `resume-failure-refusal`, `recall-strand-discriminator`, `approval-revise`, and the #38691 / #38988 fan-out pins - `pnpm --filter @objectstack/plugin-approvals typecheck`: exit 0 (`check:test-typecheck: OK`) - the new file: 5 passed. Cells: both fan-out cells (no `second` row, `approver2` told 0 times, `run.status` `failed` with its own error, record `recalled`, `releaseDeadRunRequests()` answers `{ scanned: 0, released: 0 }`); control: a running fan-out opens `second` (run reads `paused`, `approver2` told once, record `pending`); control: a flow with no fan-out opens `second`; a run whose `getRun` throws opens the step as before. </details> <details><summary>Ablation at 36b9c4b (approval-node.ts restored from ff491c4 on disk, then back from HEAD)</summary> - ablated: `Tests 2 failed | 3 passed (5)`, `no request row for \`second\`: expected [ 'first:approved', 'second:pending' ] to deeply equal [ 'first:approved' ]` - restored: `Tests 5 passed (5)` - the second fact's own ablation is the status-only cut above: four restored-strand pins red. </details> ## Acceptance notes - Not closed: a branch whose approval step is the first node after the fan-out, where the failure lands before that step opens. Nothing is logged past the end yet, so the step opens as before and the dead-run sweep recalls it. Phase 1 measured this race (`fork_direct`, note failing at 0 ms) reading `failed` once and `paused` twice at open. - Not closed: an approval step inside a child run (a `subflow` on the surviving branch) of a parent that already failed. The step reads its own run, which is live. Reading the root run's status too would also refuse a restored child continuation whose parent's row reads failed, so it was left out. Its reach is not measured. - On an object with no `closedStates` on the approval field, the fan-out judge does not run (its approvals carry no stamp), and the record kept `approved` after `first` at `594e1406`. ~~unchanged here~~ Corrected in Patch round 1 (S1, seat ruling 7338562363): the refusal now puts the record back to `recalled` there too. - An approval step with an authored `fault` out-edge: the refusal is a failed step, so the engine takes that edge on the ended run. It already keeps visiting nodes there. ## Verification Read first: `approval-node.ts` `runEndedAs` (the two facts and the fail-open answers); the refusal in `execute` (where it sits, before the root-run reads and `openNodeRequest`); `approval-node-ended-run-38985.test.ts` the two fan-out cells. Two minutes: `pnpm --filter @objectstack/plugin-approvals exec vitest run src/approval-node-ended-run-38985.test.ts` shows 5 passed; with `approval-node.ts` from main, the two fan-out cells fail on a `second:pending` row. ## Patch round 1: route A2 (seat ruling 7338562363, claim amendment 3 7338578050) Answers S1 of contract review 7338436183 and the L1 delta 7338459623 (an object whose approval field declares no `closedStates` ended `approved` on a failed fan-out). Seat ruling 7338495013 picked route (a), and 7338562363 approved A2: the record ends `recalled` on every object. **What changes** - `approval-service.ts`: new public method `ApprovalService.abandonAtRefusedStep({ runId, nodeId, object, recordId, runStatus })`. The approval step calls it once, when it refuses on an ended run. It reads the latest request for that run and record. If that request is approved or rejected, it runs the abandon path's steps, as the dead-run sweep does: an actor-less `recall` audit row naming the run, its status and the refused step; `mirrorStatusField(… 'recalled' …)`; `clearEarlierMarks`. **It writes no request row**, so the first approver's `approved` row stays as written. - It does nothing in three cases: stamped with an object that closes on its approval field (`judged`: the fan-out judge already recalls, so nothing is written twice); latest request pending or closed (`open`); no request (`none`). The dead-run sweep is unchanged. - `ApprovalService` is exported, so the method is reachable from outside the package. It is declared under `Clause-②: yes (widening)` beside `getRun`, as meant for the approval step only. - A failed put-back is reported on the refused step's own log line (`error`, else `warn`) and appended to the step's error ("The approval could not be put back: …"). The node's `MinimalLogger` now requires `warn` (`error` stays optional), per `check:optional-error-sink`. - Tenant-audit census regenerated (plugin-approvals write call sites 51 → 52). The new insert sets `organization_id` the same way `abandonForDeadRun` does. - Changeset: `recalled` on every object kind (replaces the S1 sentence); N4 added (an authored `fault` out-edge is taken on refusal); the widening sentence names the new method. N3: the test reads the run after the wait. **Tests** - Red first at `685a81f2` (test only, over `594e1406`): 4 failed, 5 passed, `expected 'approved' to be 'recalled'` on the no-closedStates (memo) and closedStates-on-another-field (ticket) cells, door and no-door. - Green at `a52afebe`: 10 pass on the real engine (6 fan-out cells over 3 object kinds × door/no-door, a failed put-back pin, 3 controls: a running fan-out, a chain, `getRun` throwing). - Ablation (new call disabled on disk): the same 4 cells fail with the same assertion; restored, hash checked. - Full plugin-approvals suite under os-verify-lock: 147 files, 2332 tests pass. Typecheck exit 0. **Engine measurement at `a52afebe`** | object | door | requests | first row after refusal | approver 2 told | field after refusal | reconcile | sweep released | end | |---|---|---|---|---|---|---|---|---| | crm_invoice (closes on approval) | door | first:approved | unchanged | 0 | recalled | n/a | 0 | recalled | | crm_invoice | no door | first:approved | unchanged | 0 | pending | abandoned:1 | 0 | recalled | | crm_memo (no closedStates) | door | first:approved | unchanged | 0 | recalled | n/a | 0 | recalled | | crm_memo | no door | first:approved | unchanged | 0 | recalled | nothing | 0 | recalled | | crm_ticket (closedStates on another field) | door | first:approved | unchanged | 0 | recalled | n/a | 0 | recalled | | crm_ticket | no door | first:approved | unchanged | 0 | recalled | ended:1 | 0 | recalled | Seat conditions (7338562363): (1) the first approver's row is unchanged; (2) every object kind ends `recalled`; (3) approver 2 is never asked (no request for `second`, 0 notices); (4) the table above is the measurement on the new head. Nothing is written twice: on invoice the only write is the judge's; on ticket without the door the reconciler's own judge clears only its own mark. **Gates:** local `dispatch-gates` 84 derived, 81 exit 0, 3 NOT MEASURED (`check:dual-build-cjs-loads`, `check:i18n` prerequisite not met; `check:query-options-erasure` hit the local cap, CI success); `--ran` exit 0. CI on `a52afebe`: every check-run succeeded (41 success, 18 skipped). ## Patch round 2: contract delta 7339783716 / L1 delta 7339823196 (S2, S3, N5) origin/main merged first (`00562b95`, no file overlap). **What changes** - **S2, no narrowing.** `MinimalLogger` is back to its shape on main (`info?`, `warn?`, no `error`); Patch round 1's required `warn` is undone. A failed put-back is logged at `warn`, else `info`, and always goes on the refused step's error string ("The approval could not be put back: …"), which the node's docblock names as the guaranteed channel. Emitted declarations (`tsc --emitDeclarationOnly`) at this head against main's sources: `registerApprovalNode(automation, service, logger?: MinimalLogger)` and `MinimalLogger { info?; warn? }` identical; `approval-node.d.ts` differs only by the declared `getRun?:` (and its import); `approval-service.d.ts` only by `abandonAtRefusedStep`. - **S3, child runs reached.** `abandonAtRefusedStep` is still one keyed read: `where { object_name, record_id, $or: [{ flow_run_id: run }, { root_run_id: run }] }`, latest by `created_at`, `limit: 1`. No column, index or engine change. Measured and pinned: a top-level run's request holds the run id in both `flow_run_id` and `root_run_id`; a subflow's request holds the child id in `flow_run_id` and the parent id in `root_run_id`. Boundary, in the `none` docblock: when the refusing run is itself a child (a fan-out inside a `subflow` body or a `map` item), it reaches only its own requests, not sibling runs' under the same root. - **N5.** The `recall` row's `step_name` is the request's own node, as in the sweep's row; the refused node stays in the comment. N6 left as is. - **Changeset:** the put-back reaches requests opened by the run or one of its child runs; a failed put-back is reported on the step's error, and the record keeps the value its last decision wrote. Declaration lines and the widening sentence unchanged. Census unchanged (52). **Tests** - Red first at `1f705eca` (test only): new cell on `crm_memo` (no closedStates), approval inside a subflow, then a parent fan-out with a failing branch: 1 failed, 10 passed, `expected 'approved' to be 'recalled'` (request opened by the child run, run failed, no row for `second`, approver 2 told 0). - Green from `e318c220`: 11 pass; the cell also pins the `recall` row's `step_name` = `first`; every top-level cell pins `flow_run_id` = `root_run_id`. - Ablation (lookup back to `flow_run_id` only): 1 failed, same assertion; restored, hash checked. - Full plugin-approvals suite at `6a1f86f9`: 147 files, 2333 tests pass. Typecheck exit 0. **Gates:** `check:optional-error-sink`, `check:durability-log-level`, `check-tenant-audit-census`, `check-nul-bytes` exit 0; `dispatch-gates` 84 derived, 81 exit 0, 3 NOT MEASURED as before (CI ran query-options-erasure: success); `--ran` exit 0. CI on `6a1f86f9`: 41 success, 18 skipped, 0 failed. ## Patch round 3: contract delta 7341149576 / L1 delta 7341170960 (S4) origin/main merged first (`a3047515`, no PR file touched). **What changes** - **S4.** On refusal, the approval step passes its run's root: `rootRunId: automation.rootRunOf?.(context)?.runId ?? null` (`rootRunOf` is a pure read of the step's context). The declared public method's input gains one optional field: `abandonAtRefusedStep({ runId, nodeId, object, recordId, runStatus, rootRunId? })`; the changeset's widening sentence names it. - **The read** stays one keyed read on the same index: `object_name`, `record_id`, `$or: [{ flow_run_id: run }, { root_run_id: rootRunId || run }]`, newest first, `limit: 1`. A refusing child run now reaches requests opened by its root, its parent and its sibling children. With no root handed over, the run counts as its own root, as before. If the tree's newest request is pending, the answer is `open` and nothing is written (new cell, with an older decided request present). - **`none` docblock** names only what remains outside: a request with no `root_run_id` opened by another run; with no root passed, a request whose root is another run. - **Changeset:** the put-back reaches any run of the approval's tree (the root run, a `subflow` body or a `map` item, so also the parent or a sibling of a run that is itself a child). Census unchanged. **Tests** - Red first at `813e8043` (test only): 3 failed, 11 passed. Two new `crm_memo` cells (a subflow body fans out, a branch fails, a late approval step refuses; the decided request opened by the parent / by a sibling child) fail with `expected 'approved' to be 'recalled'` after every earlier assertion passed; the live-request cell fails with `expected 'none' to be 'open'`. - Green at `ad7b8cc4`: 14 pass; both child cells end `recalled` with one actor-less `recall` row naming the child run, the decided row unchanged, approver 2 told 0. - Ablation (second arm back to the run's own id): the same 3 fail; restored, hash checked. - Full plugin-approvals suite at `ad7b8cc4`: 147 files, 2336 tests pass. Typecheck exit 0. **Gates:** `check:optional-error-sink`, `check:durability-log-level`, `check-tenant-audit-census`, `check-nul-bytes` exit 0; `dispatch-gates` 84 derived, 81 exit 0, 3 NOT MEASURED as before; `--ran` exit 0. CI on `ad7b8cc4`: 41 success, 18 skipped, 0 failed. --- _Generated by [Claude Code](https://claude.ai/code/session_01WGufzhKEbEnW8pwZRgK2gJ)_ <!-- signed: repo:objectstack@os-support-ai · os-support-ai · session_01WGufzhKEbEnW8pwZRgK2gJ --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent f037f1c commit d0bce70

5 files changed

Lines changed: 626 additions & 9 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@objectstack/plugin-approvals': patch
3+
---
4+
5+
fix(plugin-approvals): an approval step opens no request on a run that has already ended
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
The widening, two additions: the exported `ApprovalAutomationSurface` gains an optional member, `getRun?: ApprovalResumeSurface['getRun']`, the run log read the approval step makes before it opens a request (a host that passes an engine without it opens the step as before); and the exported `ApprovalService` gains a public method, `abandonAtRefusedStep({ runId, nodeId, object, recordId, runStatus, rootRunId? })`, which the approval step calls once when it refuses, described below. It is meant for the approval step only.
11+
12+
When a flow's node fans out to branches that run together and one branch fails, the run ends while the other branches keep going. A branch that reached an approval step after that still opened a request there: the step's approver was notified and asked to decide for a run that can never resume, and the record was held `pending` until the dead-run sweep recalled the request (every 5 minutes by default, and never on a host with no job service).
13+
14+
The approval step now reads its run's log before it opens a request. Where the run has already ended (completed, failed, cancelled, timed out, refused or missed) and the log shows a step completed after that end, so another branch went on to this step, it opens no request, sends no notice and leaves nothing pending. The step is logged as failed with a sentence naming the run and how it ended; the run keeps the status and error it ended with. Because the step fails, an authored `fault` out-edge on it is taken, on the run that has already ended.
15+
16+
The record then ends `recalled` (or its value at submit, where its options do not declare `recalled`) on every object, abandoned and never approved, and the approver of the next step is never asked:
17+
18+
- On an object that closes on its approval field, the decision before the fan-out was held back, and the plugin's fan-out judge puts the record back, through the door or the reconciler. The refusal writes nothing more there.
19+
- On any other object, the decision before the fan-out was already written to the field (`approved`, say), and no judge runs. The refusal puts the record back itself, as the dead-run sweep does for an approval whose run died: one `recall` row with no actor on the approval's latest request on the record, whether the run itself or any run of its tree opened it (the root run, a `subflow` body or a `map` item, so also the parent or a sibling of a run that is itself a child), naming the run, how it ended and the step that opened nothing, then the status field set to `recalled`. That latest request keeps the decision its approver recorded; no request row is written. If the put-back fails, the step's error says so, and the record keeps the value its last decision wrote.
20+
21+
A run that is still running, or still being resumed onto the step, opens it as before. So does a run that stranded and was restored and is now resumed again: its log still reads the end it stranded with, but nothing logged after it. A run whose status cannot be read also opens the step: an unreadable run is not taken for an ended one. Where a branch reaches the step before it has logged any step past the run's end (the step is the first node after the fan-out and the failure landed first), the step still opens, and the dead-run sweep recalls the request as before.

‎docs/audits/2026-08-tenant-audit-write-call-sites.counts/plugins/plugin-approvals.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ a row or a number here — fix the code, or the census, and regenerate.
1414

1515
| Measure | Value |
1616
|---|---:|
17-
| Write call sites | 51 |
18-
| Object name statically decidable | 49 |
17+
| Write call sites | 52 |
18+
| Object name statically decidable | 50 |
1919
| Object name chosen at run time | 2 |
20-
| Object name spelled inline | 47 |
20+
| Object name spelled inline | 48 |
2121
| Object name spelled through a `const` | 2 |
2222
| Object name is an `object: string` parameter | 1 |
2323
| Object name is some other run-time expression | 1 |
24-
| Against a tenancy-enabled object | 49 |
24+
| Against a tenancy-enabled object | 50 |
2525
| Against an object declaring tenancy off | 0 |
26-
| Threading a tenant context | 51 |
26+
| Threading a tenant context | 52 |
2727
| Provably carrying none (options read, no context key) | 0 |
2828
| …and decidably tenancy-enabled | 0 |
2929
| Options argument unreadable | 0 |
3030
| …and decidably tenancy-enabled | 0 |
31-
| Threading a decidably elevated (`isSystem`) context | 49 |
31+
| Threading a decidably elevated (`isSystem`) context | 50 |
3232
| Threading a decidably non-elevated context | 0 |
3333
| Threading a context of undecidable elevation | 2 |
34-
| Receiver carried a readable engine type | 51 |
34+
| Receiver carried a readable engine type | 52 |
3535
| Receiver erased, placed by the object NAME | 0 |
3636
| Receiver erased, placed by an `object: string` PARAMETER | 0 |
3737
| Receiver erased, placed by an `UNTYPED_RECEIVERS` row | 0 |
@@ -50,7 +50,7 @@ a row or a number here — fix the code, or the census, and regenerate.
5050
| `packages/plugins/plugin-approvals/src/action-slot-backfill.ts` | `update` | `sys_approval_action` | enabled | elevated | 2 |
5151
| `packages/plugins/plugin-approvals/src/approval-activity-feed.ts` | `insert` | `sys_activity` | enabled | elevated | 1 |
5252
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `update` | `object` | undecidable | context, elevation undecidable | 1 |
53-
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `insert` | `sys_approval_action` | enabled | elevated | 20 |
53+
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `insert` | `sys_approval_action` | enabled | elevated | 21 |
5454
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `delete` | `sys_approval_approver` | enabled | elevated | 2 |
5555
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `insert` | `sys_approval_approver` | enabled | elevated | 2 |
5656
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `insert` | `sys_approval_request` | enabled | elevated | 1 |

0 commit comments

Comments
 (0)