Skip to content

Commit ad7b8cc

Browse files
committed
fix(approvals): a refusing child run reaches the approval its whole tree opened (#38985 S4)
The approval step hands its run's root (rootRunOf) to abandonAtRefusedStep, whose input gains an optional rootRunId; the read's second arm compares root_run_id with that root, so a child run that refuses reaches its parent's and its siblings' requests. Still one keyed read; without a root the run is its own root, as before. The changeset names the new input field and the tree. Claude-Session: https://claude.ai/code/session_01WGufzhKEbEnW8pwZRgK2gJ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 813e804 commit ad7b8cc

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

‎.changeset/38985-ended-run-approval-step.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fix(plugin-approvals): an approval step opens no request on a run that has alrea
77
Clause-②: yes (widening)
88
Metadata-protocol: none
99

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 })`, which the approval step calls once when it refuses, described below. It is meant for the approval step only.
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.
1111

1212
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).
1313

@@ -16,6 +16,6 @@ The approval step now reads its run's log before it opens a request. Where the r
1616
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:
1717

1818
- 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 one of its child runs (a `subflow` body, a `map` item) opened it, 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.
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.
2020

2121
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.

‎packages/plugins/plugin-approvals/src/approval-node.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ export function registerApprovalNode(
231231
try {
232232
await service.abandonAtRefusedStep({
233233
runId: String(runId), nodeId: node.id, object, recordId: String(recordId), runStatus: endedAs,
234+
// The approval is the run's whole tree: its root's request, a parent's or a sibling child's.
235+
rootRunId: automation.rootRunOf?.(context)?.runId ?? null,
234236
});
235237
} catch (err: any) {
236238
(logger?.warn ?? logger?.info)?.call(

‎packages/plugins/plugin-approvals/src/approval-service.ts‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12804,15 +12804,20 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1280412804
* ⛔ No request row is written: the latest request's decision stays as its
1280512805
* approver recorded it.
1280612806
*
12807-
* [#38985 S3] "This run or one of its child runs" is one keyed read: a
12808-
* request whose `flow_run_id` is the run (the run opened it), or whose
12809-
* `root_run_id` is the run (a run of the tree the run is the root of
12810-
* opened it: a `subflow` body or a `map` item). A top-level run is its own
12811-
* root, so a request it opened carries its id in both columns; a child
12807+
* [#38985 S3, S4] "Opened by the approval" is one keyed read: a request
12808+
* whose `flow_run_id` is the run (the run opened it), or whose
12809+
* `root_run_id` is the run's root, `rootRunId` (any run of the run's tree
12810+
* opened it: the root itself, a `subflow` body or a `map` item, the
12811+
* refusing run's parent and its siblings included). A top-level run is its
12812+
* own root, so a request it opened carries its id in both columns; a child
1281212813
* run's request carries the child's id in `flow_run_id` and the top-level
12813-
* run's in `root_run_id` (ADR-0162 Execution item 7, `rootRunOf`). A
12814-
* request opened by an engine that hands no root run leaves `root_run_id`
12815-
* empty and is found by `flow_run_id` alone.
12814+
* run's in `root_run_id` (ADR-0162 Execution item 7, `rootRunOf`). Without
12815+
* `rootRunId` (an engine that hands no root run, or one that could not
12816+
* place the run) the run is taken for its own root.
12817+
*
12818+
* The newest request of the tree is the one judged: a record holds at most
12819+
* one pending request, and none opens while one is pending, so where the
12820+
* tree has a pending request it is the newest, and the answer is `open`.
1281612821
*
1281712822
* Nothing is written, and the answer says why, where:
1281812823
* - `judged`: the approval is stamped and its object closes on its
@@ -12821,19 +12826,23 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1282112826
* back, so writing here would put it back twice;
1282212827
* - `open`: the latest request is still pending or already closed by a
1282312828
* recall or a cancel: the dead-run sweep, or that act, owns it;
12824-
* - `none`: neither the run nor any run of the tree it is the root of
12825-
* opened a request on the record (the refused step would have been the
12826-
* approval's first): nothing of an approval to undo. A refusing run that
12827-
* is itself a child reaches its own requests only, not those of runs
12828-
* beside it under the same root.
12829+
* - `none`: no run of the run's tree opened a request on the record
12830+
* (the refused step would have been the approval's first): nothing of
12831+
* an approval to undo. Outside the read, so also `none`: a request that
12832+
* records no root run (`root_run_id` empty: opened by an engine that
12833+
* hands none) and that another run than this one opened; and, where the
12834+
* run's root is not handed over, a request that another run's root
12835+
* names.
1282912836
*/
1283012837
async abandonAtRefusedStep(input: {
1283112838
runId: string; nodeId: string; object: string; recordId: string; runStatus: string;
12839+
/** The root run of `runId`'s tree (`rootRunOf`). Absent or `null`: `runId` is taken for its own root. */
12840+
rootRunId?: string | null;
1283212841
}): Promise<'abandoned' | 'judged' | 'open' | 'none'> {
1283312842
const rows = await this.engine.find('sys_approval_request', {
1283412843
where: {
1283512844
object_name: input.object, record_id: input.recordId,
12836-
$or: [{ flow_run_id: input.runId }, { root_run_id: input.runId }],
12845+
$or: [{ flow_run_id: input.runId }, { root_run_id: input.rootRunId || input.runId }],
1283712846
},
1283812847
orderBy: [{ field: 'created_at', order: 'desc' }], limit: 1, context: SYSTEM_CTX,
1283912848
});

0 commit comments

Comments
 (0)