Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .changeset/35863-decided-route-outcome.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/spec': patch
'@objectstack/plugin-approvals': patch
'@object-ui/app-shell': patch
'@object-ui/i18n': patch
'@object-ui/console': patch
---

feat(approvals): a decided request's step line says where the record's approval went next: on to a later step, or ended

Clause-②: yes
Metadata-protocol: none

The single approval request read (`GET /api/v1/approvals/requests/ID`) serves one more fact on a decided request's own route: `flow_route.outcome`, beside `ahead: 'decided'`. Before, a decided route stopped at the request's own step, so an approver reopening a request they had decided could not tell whether the record's approval went on to a later step or ended.

- `outcome.step` is the run's latest request when it is a later one than this request (a request of the same flow run opened after it, on any node, a later round of the same node included; a pending one wins over a decided one). It is absent when no later request was opened in that run; a request a child run opened is not among them.
- `outcome.status` is that latest request's status, or this request's own when `step` is absent. `pending` with a `step` means the approval went on to that step and waits on its decision.
- `outcome.ended` is `true` only when the record's approval is known to have stopped: the request's run is a root run (not a `subflow` body or a `map` item, whose end is not the approval's), the flow body the run ran holds no `subflow` or `map` node anywhere, regions included (a child run files its requests under its own run, so this run's requests cannot show what it decided, before or after this request), and the run reports it stopped for good, with the same terminal set the dead-run sweep uses. The body the run ran is known only when the body armed now is the flow's newest registration and was registered before the run started, so a flow edited, rolled back or re-registered after a run started is never judged by its current body for that run. `false` means not known to have stopped: the run goes on, is a child run, ran a body that can start a child run, ran a body that is not known, cannot be read, or was opened before this release (the approval node now records on each new request whether its run is a root run).

`outcome` is absent when nothing past the request is known: no later request was opened and the approval is not known to have ended, or several later requests are pending at once (parallel steps). A pending request's route never carries it. It is served to every reader of the request and says only where the same approval went: the later step's node id and label and that request's status, never the later request's id, approvers, comments or field values. The `ApprovalRequestRow.flow_route` contract type in `@objectstack/spec/contracts` gains the optional member, and `routeOutcome` in `@objectstack/plugin-approvals` is the rule.

The approval request page (`@object-ui/app-shell`) draws one line under the decided step: "Went on to STEP, waiting for a decision", "Went on to STEP: DECISION", "Went on to STEP, then ended: DECISION", or "The approval ended here". No line is drawn when no outcome is served. `@object-ui/i18n` carries the four new `approvalsInbox` rows in every built-in locale, and the console's request type (`@object-ui/console`) mirrors the new member.

Nothing to migrate: a caller that does not read `outcome` is unaffected.
35 changes: 35 additions & 0 deletions content/docs/automation/approvals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,41 @@ out-edges, a loop back or a `subflow` / `map` / `loop` node, and `ahead` reads
`decided` for a request already decided. The older `flow_steps` member is the
graph's first branch at every decision, not the route; nothing draws it.

A decided request's route also says where the record's approval stands now,
in `outcome`, so its step line can answer "what happened next":

```jsonc
// getRequest, for the manager's request after the manager approved:
"flow_route": {
"steps": [{ "id": "manager_review", "label": "Manager", "state": "done" }],
"ahead": "decided",
"outcome": {
"step": { "id": "finance_only", "label": "Finance" }, // the run's latest, later request
"status": "pending", // it waits on its decision
"ended": false // the run goes on
}
}
```

`step` is the run's latest request opened after this one (a pending one first),
and is absent when none was opened; `status` is that request's status, or this
request's own without a `step`; `ended` is `true` only when the record's
approval is known to have stopped: the request's run is a root run (an approval
inside a `subflow` or a `map` item runs in a child run, whose end is not the
approval's), the flow body the run ran holds no `subflow` or `map` node
anywhere (a child run files its requests under its own run, so the root run's
requests cannot show what it decided), and the run has stopped for good. The
body the run ran is known only while the flow has not been registered again
since the run started: after an edit, a rollback or a restart, older runs are
not judged by today's body, and their requests are not served "ended". `false`
means not known to have stopped, including a run that cannot be read. So on a
flow that starts a child run, a decided request's line names the later steps of
its own run but never says the approval ended. `outcome` is left out when nothing past
the request is known: no later request and the approval not known to have
ended, or several later requests pending at once. Every reader of the request
gets it; it names only the later step and its status, never that request's
approvers, comments or field values.

Approval **notifications deep-link into the request**: `notify()` rewrites the
inbox action URL to `/system/approvals?request=<id>`, so clicking the bell opens
that request's drawer directly instead of a generic list.
Expand Down
16 changes: 16 additions & 0 deletions packages/plugins/plugin-approvals/src/approval-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ export function registerApprovalNode(
// The node object this run executes: a step naming the platform
// administrators opens only when it belongs to the armed body.
node,
// [#35863] Whether this run is the record's whole approval run or a
// child of another (a `subflow` body or a `map` item: the engine
// links those upward by `$parentRunId`). A child run's end is not
// the end of the record's approval, so a decided route may say
// "ended" only for a root run's request.
runRoot: !hasParentRun(context),
}, {
...SYSTEM_CTX,
userId: context?.userId,
Expand Down Expand Up @@ -256,3 +262,13 @@ export function registerApprovalNode(

logger?.info?.('[approvals] approval node executor registered');
}

/**
* [#35863] Whether `context` is a child run's: the engine starts a `subflow`
* body or a `map` item with the parent's run id as `$parentRunId`. Read the
* way the engine's own readers read it (a non-empty string).
*/
function hasParentRun(context: unknown): boolean {
const parent = (context as { $parentRunId?: unknown } | undefined)?.$parentRunId;
return typeof parent === 'string' && parent !== '';
}
105 changes: 103 additions & 2 deletions packages/plugins/plugin-approvals/src/approval-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
* the real engine and a real run, is pinned in `flow-route.integration.test.ts`;
* these are the cases a real run reaches rarely or slowly: rounds, other runs,
* outcomes that are not a pass, and each kind of branch the walk refuses to
* decide.
* decide. [#35863] Then where a decided request's approval stands now
* (`routeOutcome`): what the run's later rows and its end say, and the cases
* where they say nothing; and which flows can start a child run, whose
* requests those rows never hold (`flowHostsChildRuns`).
*/

import { describe, it, expect } from 'vitest';
import { flowRoute, passedSteps, stepsAhead, type RouteFlowGraph } from './approval-route.js';
import { flowHostsChildRuns, flowRoute, passedSteps, routeOutcome, stepsAhead, type RouteFlowGraph } from './approval-route.js';

const label = (id: string) => id.toUpperCase();
const row = (flow_node_id: string, status: string, created_at: string, id = `${flow_node_id}@${created_at}`) =>
Expand Down Expand Up @@ -146,3 +149,101 @@ describe('flowRoute — the whole route', () => {
expect(flowRoute({ id: 'x', status: 'pending' }, LINE, runRows, label)).toBeUndefined();
});
});

describe('[#35863] routeOutcome — where the record\'s approval stands after a decided request', () => {
const decided = (status: string, created_at = at(2)) => ({ id: `a@${created_at}`, status, created_at });

it('a later request pending: the approval went on to that step and waits, whatever the run says', () => {
const rows = [row('a', 'approved', at(2)), row('b', 'pending', at(3))];
expect(routeOutcome(decided('approved'), rows, false, label)).toEqual({
step: { id: 'b', label: 'B' }, status: 'pending', ended: false,
});
});

it('the last step decided and the run ended: the approval ended with that step\'s decision', () => {
for (const status of ['approved', 'rejected'] as const) {
const rows = [row('a', 'approved', at(2)), row('b', status, at(3))];
expect(routeOutcome(decided('approved'), rows, true, label), status).toEqual({
step: { id: 'b', label: 'B' }, status, ended: true,
});
}
});

it('no later request and the run ended: the approval ended at this request, with its own decision', () => {
for (const status of ['approved', 'rejected', 'recalled', 'cancelled'] as const) {
expect(routeOutcome(decided(status), [row('a', status, at(2))], true, label), status).toEqual({ status, ended: true });
}
});

it('no later request and the run not ended: nothing is known past this request, so no outcome', () => {
expect(routeOutcome(decided('returned'), [row('a', 'returned', at(2))], false, label)).toBeUndefined();
expect(routeOutcome(decided('approved'), [], false, label)).toBeUndefined();
});

it('a later round of the same node counts: a returned request resubmitted waits at its own step again', () => {
const rows = [row('a', 'returned', at(2)), row('a', 'pending', at(3))];
expect(routeOutcome(decided('returned'), rows, false, label)).toEqual({
step: { id: 'a', label: 'A' }, status: 'pending', ended: false,
});
});

it('a pending later request wins over a decided one opened after it; else the latest decided one', () => {
const rows = [row('b', 'pending', at(3)), row('c', 'rejected', at(4))];
expect(routeOutcome(decided('approved'), rows, false, label)?.step?.id).toBe('b');
const decidedOnly = [row('c', 'approved', at(4)), row('b', 'approved', at(3))];
expect(routeOutcome(decided('approved'), decidedOnly, true, label)?.step?.id).toBe('c');
});

it('several later requests pending at once (parallel steps): no single step, no outcome', () => {
const rows = [row('b', 'pending', at(3)), row('c', 'pending', at(3))];
expect(routeOutcome(decided('approved'), rows, false, label)).toBeUndefined();
});

it('earlier rows, the request itself, and rows with no node, time or status are not later requests', () => {
const rows = [
row('z', 'pending', at(1)),
{ id: 'a@' + at(2), flow_node_id: 'a', status: 'pending', created_at: at(5) },
{ id: 'n1', status: 'pending', created_at: at(3) },
{ id: 'n2', flow_node_id: 'b', status: 'pending' },
{ id: 'n3', flow_node_id: 'b', created_at: at(3) },
];
expect(routeOutcome(decided('approved'), rows, false, label)).toBeUndefined();
expect(routeOutcome({ id: 'x', status: 'approved' }, [row('b', 'pending', at(3))], false, label)).toBeUndefined();
});

it('flowRoute carries it on a decided request only, and only when one is known', () => {
const rows = [row('a', 'approved', at(2)), row('b', 'pending', at(3))];
expect(flowRoute({ id: 'a@' + at(2), flow_node_id: 'a', created_at: at(2), status: 'approved' }, LINE, rows, label)).toEqual({
steps: [{ id: 'a', label: 'A', state: 'done' }],
ahead: 'decided',
outcome: { step: { id: 'b', label: 'B' }, status: 'pending', ended: false },
});
// A pending request is never handed an outcome, even with the run's rows and an ended run.
const pending = flowRoute({ id: 'b@' + at(3), flow_node_id: 'b', created_at: at(3), status: 'pending' }, LINE, rows, label, true);
expect(pending && 'outcome' in pending).toBe(false);
});
});

describe('[#35863] flowHostsChildRuns — a flow whose run may file requests under a child run', () => {
const node = (id: string, type: string, config?: Record<string, unknown>) => ({ id, type, label: id, ...(config ? { config } : {}) });
const region = (...nodes: unknown[]) => ({ nodes, edges: [] });

it('a subflow or a map node at the top level', () => {
expect(flowHostsChildRuns({ nodes: [node('s', 'start'), node('legal', 'subflow'), node('e', 'end')] })).toBe(true);
expect(flowHostsChildRuns({ nodes: [node('s', 'start'), node('each', 'map'), node('e', 'end')] })).toBe(true);
});

it('a subflow or a map node inside a container\'s region, at any depth', () => {
const inLoop = { nodes: [node('l', 'loop', { body: region(node('legal', 'subflow')) })] } as RouteFlowGraph;
const inBranch = { nodes: [node('p', 'parallel', { branches: [region(node('a', 'approval')), region(node('each', 'map'))] })] } as RouteFlowGraph;
const inCatch = { nodes: [node('t', 'try_catch', { try: region(node('a', 'approval')), catch: region(node('legal', 'subflow')) })] } as RouteFlowGraph;
const nested = { nodes: [node('l', 'loop', { body: region(node('t', 'try_catch', { try: region(node('legal', 'subflow')) })) })] } as RouteFlowGraph;
for (const flow of [inLoop, inBranch, inCatch, nested]) expect(flowHostsChildRuns(flow)).toBe(true);
});

it('no such node anywhere: approvals, gateways and in-run containers only', () => {
expect(flowHostsChildRuns({ nodes: [node('s', 'start'), node('a', 'approval'), node('d', 'decision'), node('e', 'end')] })).toBe(false);
expect(flowHostsChildRuns({ nodes: [node('l', 'loop', { body: region(node('a', 'approval')) })] } as RouteFlowGraph)).toBe(false);
expect(flowHostsChildRuns({})).toBe(false);
});
});
Loading
Loading