Skip to content

Commit 813e804

Browse files
committed
test(approvals): a refusing child run reaches its parent's and siblings' approval (red, #38985 S4)
A subflow body fans out and fails; its late approval step refuses. The decided request was opened by the parent, or by a sibling child, so neither lookup arm matches the child's id and the record stays approved. A third cell pins that the put-back answers open, not a write, while the tree's newest request is pending. Claude-Session: https://claude.ai/code/session_01WGufzhKEbEnW8pwZRgK2gJ Co-authored-by: Claude <noreply@anthropic.com>
1 parent a304751 commit 813e804

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

‎packages/plugins/plugin-approvals/src/approval-node-ended-run-38985.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ const FLOWS = [
9191
flow('sub_fork_fail_side', [sub('ask', 'child_first'), mark('fork'), sub('mid', 'child_slow'), mark('note'), step('second', 'approver2')],
9292
[edge('start', 'ask'), edge('ask', 'fork'),
9393
edge('fork', 'mid'), edge('fork', 'note'), edge('note', 'end'), edge('mid', 'second'), ...decided('second', 'end')]),
94+
// [#38985 S4] The fan-out inside a child run: `fork` in a subflow body, `note` fails, `second` comes late.
95+
flow('child_fork_fail', [mark('fork'), sub('mid', 'child_slow'), mark('note'), step('second', 'approver2')],
96+
[edge('start', 'fork'), edge('fork', 'mid'), edge('fork', 'note'), edge('note', 'end'), edge('mid', 'second'), ...decided('second', 'end')]),
97+
// The parent decides `first` itself, then calls that child.
98+
flow('parent_then_child_fork', [step('first', 'approver'), sub('inner', 'child_fork_fail')],
99+
[edge('start', 'first'), ...decided('first', 'inner'), edge('inner', 'end')]),
100+
// One child decides `first`, a sibling child then fans out.
101+
flow('sibling_then_child_fork', [sub('ask', 'child_first'), sub('inner', 'child_fork_fail')],
102+
[edge('start', 'ask'), edge('ask', 'inner'), edge('inner', 'end')]),
94103
// The control with no fan-out: `first`, then `second`.
95104
flow('chain', [step('first', 'approver'), step('second', 'approver2')],
96105
[edge('start', 'first'), ...decided('first', 'second'), ...decided('second', 'end')]),
@@ -272,6 +281,63 @@ describe('[#38985] an approval step opens no request on a run that has already e
272281
expect(row.step_name, 'the row names the request\'s own step, as the sweep\'s; the refused one is in the comment').toBe('first');
273282
});
274283

284+
/** The child run of `child_fork_fail` that started last, once its late `second` has run. */
285+
const refusedChild = async () => {
286+
for (let i = 0; i < 100; i++) {
287+
const runs: any[] = await automation.listRuns('child_fork_fail');
288+
const child = runs.find((r) => r?.steps?.some((x: any) => x.nodeId === 'second'));
289+
if (child) return child;
290+
await sleep(20);
291+
}
292+
throw new Error('the child run never logged `second`');
293+
};
294+
for (const [flowName, who] of [
295+
['parent_then_child_fork', 'the parent opened it'],
296+
['sibling_then_child_fork', 'a sibling child opened it'],
297+
] as const) {
298+
it(`the refusing run is a child, and the decided request is not its own (${who}): the step reaches it, and the record ends recalled`, async () => {
299+
const id = `m_${flowName}`;
300+
await start(flowName, id, 'crm_memo');
301+
expect(await steps(id)).toEqual(['first:pending']);
302+
failNote();
303+
await decideFirst(id);
304+
const firstRaw = (await requestsOn(id))[0];
305+
const first = decidedRow(firstRaw);
306+
expect(first.status).toBe('approved');
307+
const child = await refusedChild();
308+
await sleep(50);
309+
const childId = String(child.id ?? child.runId);
310+
const rootId = String(firstRaw.root_run_id);
311+
expect(childId, 'the run that refuses is not the root').not.toBe(rootId);
312+
expect(String(firstRaw.flow_run_id), 'nor did it open the decided request').not.toBe(childId);
313+
314+
expect((await automation.getRun(childId) as any)?.status).toBe('failed');
315+
expect(await steps(id), 'no request row for `second`').toEqual(['first:approved']);
316+
expect(decidedRow((await requestsOn(id))[0])).toEqual(first);
317+
expect(told('approver2')).toBe(0);
318+
expect(await field(id), 'abandoned, never approved').toBe('recalled');
319+
expect((await timeline(String(first.id))).filter((t) => t.action === 'recall')).toEqual([{
320+
action: 'recall', actor: null,
321+
comment: `owning flow run ${childId} is failed: step 'second' opened no request, so the approval was abandoned and the record put back`,
322+
}]);
323+
});
324+
}
325+
326+
it('the put-back never writes over a live request: the newest request of the tree is pending, so it answers open and writes nothing', async () => {
327+
await start('chain', 'm_live', 'crm_memo');
328+
await decideFirst('m_live');
329+
const [firstRaw, secondRaw] = await requestsOn('m_live');
330+
expect([firstRaw.status, secondRaw.status]).toEqual(['approved', 'pending']);
331+
const rootId = String(secondRaw.root_run_id);
332+
333+
const answer = await svc.abandonAtRefusedStep({
334+
runId: 'run_child_elsewhere', rootRunId: rootId, nodeId: 'late', object: 'crm_memo', recordId: 'm_live', runStatus: 'failed',
335+
});
336+
expect(answer, 'the newest is the pending `second`, not the older decided `first`').toBe('open');
337+
expect(await field('m_live')).toBe('pending');
338+
expect((await timeline(String(firstRaw.id))).filter((t) => t.action === 'recall')).toEqual([]);
339+
});
340+
275341
it('a put-back that fails is said, on the refused step\'s own log line: the record keeps the last decision\'s value', async () => {
276342
(svc as any).abandonAtRefusedStep = async () => { throw new Error('store down'); };
277343
await start('fork_fail_side', 'm_fail', 'crm_memo');

0 commit comments

Comments
 (0)