Skip to content

Commit 30ec243

Browse files
committed
fix(approvals): "ended" is judged by the flow body the run ran, not today's
The read holds only the flow body armed now. A run that started a subflow, whose flow was later edited to drop it, was judged by the edit and served "the approval ended here". "Ended" now also needs the run to have provably run the armed body: that body is the flow's newest registration (the engine's version history, the very object) and was registered strictly before the run started. An edit, a rollback or a restart's re-registration since the run started leaves "ended" unclaimed. The run's flowVersion is not the witness: an edit need not change the author's number. The contract, changeset and approvals docs say which body is judged; the child-run node list names the two executors that set $parentRunId. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzg3ayWUHg9nLoybejn
1 parent e264c10 commit 30ec243

6 files changed

Lines changed: 113 additions & 27 deletions

File tree

‎.changeset/35863-decided-route-outcome.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The single approval request read (`GET /api/v1/approvals/requests/ID`) serves on
1515

1616
- `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.
1717
- `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.
18-
- `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), its flow 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. `false` means not known to have stopped: the run goes on, is a child run, runs a flow that can start a child run, 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).
18+
- `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).
1919

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

‎content/docs/automation/approvals.mdx‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,12 +1220,15 @@ and is absent when none was opened; `status` is that request's status, or this
12201220
request's own without a `step`; `ended` is `true` only when the record's
12211221
approval is known to have stopped: the request's run is a root run (an approval
12221222
inside a `subflow` or a `map` item runs in a child run, whose end is not the
1223-
approval's), its flow holds no `subflow` or `map` node anywhere (a child run
1224-
files its requests under its own run, so the root run's requests cannot show
1225-
what it decided), and the run has stopped for good. `false` means not known to
1226-
have stopped, including a run that cannot be read. So on a flow that starts a
1227-
child run, a decided request's line names the later steps of its own run but
1228-
never says the approval ended. `outcome` is left out when nothing past
1223+
approval's), the flow body the run ran holds no `subflow` or `map` node
1224+
anywhere (a child run files its requests under its own run, so the root run's
1225+
requests cannot show what it decided), and the run has stopped for good. The
1226+
body the run ran is known only while the flow has not been registered again
1227+
since the run started: after an edit, a rollback or a restart, older runs are
1228+
not judged by today's body, and their requests are not served "ended". `false`
1229+
means not known to have stopped, including a run that cannot be read. So on a
1230+
flow that starts a child run, a decided request's line names the later steps of
1231+
its own run but never says the approval ended. `outcome` is left out when nothing past
12291232
the request is known: no later request and the approval not known to have
12301233
ended, or several later requests pending at once. Every reader of the request
12311234
gets it; it names only the later step and its status, never that request's

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ const OPAQUE_NODE_TYPES = new Set(['subflow', 'map', 'loop']);
5454
* item runs under its own run id, linked upward by `$parentRunId`. A request
5555
* a child run opens is filed under that child's `flow_run_id`, so it is not
5656
* among the run rows the route reads.
57+
*
58+
* ⚠️ A hand list of the engine's child-run starters: the only executors that
59+
* set `$parentRunId` on a context are `subflow`
60+
* (`service-automation/src/builtin/subflow-node.ts`) and `map`
61+
* (`builtin/map-node.ts`); `loop`, `parallel` and `try_catch` run their
62+
* regions inside the same run. A node kind that starts a child run joins this
63+
* set in the same change, or {@link flowHostsChildRuns} under-reports it and
64+
* "ended" is claimed past that child's requests.
5765
*/
5866
const CHILD_RUN_NODE_TYPES = new Set(['subflow', 'map']);
5967

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

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@ export interface ApprovalResumeSurface {
312312
flowSlotKey?(packageId: string | null, name: string): string | undefined;
313313
getFlowSlot?(packageId: string | null, name: string): any | null;
314314
getRunFlow?(run: { flowName: string }): any | null;
315+
/**
316+
* [#35863] A flow name's registrations, oldest first: each body and when it
317+
* was registered. Read by {@link ApprovalService.attachFlowRoute} to tell
318+
* whether a run ran the body armed now (`ranArmedBody`). ⚠️ Declares a
319+
* method `AutomationEngine` ALREADY implements publicly
320+
* (`getFlowVersionHistory`); it widens no engine surface. Optional: absent,
321+
* no request is served "ended".
322+
*/
323+
getFlowVersionHistory?(name: string): ReadonlyArray<{ version?: number; definition: unknown; createdAt: string }>;
315324
/**
316325
* Terminally cancel a suspended run (ADR-0044). Used when a recall lands
317326
* during a revision window — the run is paused at the revise-window node,
@@ -330,7 +339,7 @@ export interface ApprovalResumeSurface {
330339
* `null` — unknown run, evicted log, no durable store, no automation engine —
331340
* as "still alive".
332341
*/
333-
getRun?(runId: string): Promise<{ status?: string; flowName?: string } | null>;
342+
getRun?(runId: string): Promise<{ status?: string; flowName?: string; startedAt?: string } | null>;
334343
/**
335344
* Whether a suspension still exists for `runId` — the pre-flight that keeps
336345
* a decision from being recorded against a run that can never advance
@@ -11686,9 +11695,13 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1168611695
* flow starts a child run anywhere ({@link flowHostsChildRuns}): a request
1168711696
* a child run opened, after this one included, is filed under the child's
1168811697
* run id and is not among the rows read here, so neither "ended here" nor
11689-
* "the last decision" can be known. A request opened before the stamp, a
11690-
* child run's, a run of such a flow, or a run that cannot be read is never
11691-
* "ended": `ended: false` means "not known to have stopped".
11698+
* "the last decision" can be known. "Its flow" is the body the run ran,
11699+
* and the read holds only the body armed NOW, so "ended" is also refused
11700+
* unless the run provably ran that body ({@link ranArmedBody}): a flow
11701+
* edited after the run, to drop its `subflow` say, is not judged by the
11702+
* edit. A request opened before the stamp, a child run's, a run of such a
11703+
* flow, a run not known to have run the armed body, or a run that cannot be
11704+
* read is never "ended": `ended: false` means "not known to have stopped".
1169211705
*/
1169311706
private async attachFlowRoute(
1169411707
row: ApprovalRequestRow,
@@ -11717,28 +11730,55 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1171711730
labels?.stepLabel(flowName, nodeId) ?? (nodesById.get(nodeId)?.label || stored.get(nodeId) || nodeId);
1171811731
const runRoot = parseJson<any>(raw?.node_config_json, undefined)?.__runRoot === true;
1171911732
const ended = row.status !== 'pending' && runRoot && !flowHostsChildRuns(flow)
11720-
&& await this.runHasEnded(row.flow_run_id);
11733+
&& await this.runHasEnded(row.flow_run_id, flowName, flow);
1172111734
const route = flowRoute(row, flow, rows, label, ended);
1172211735
if (route) (row as any).flow_route = route;
1172311736
} catch { /* display-only — never fail the read */ }
1172411737
}
1172511738

1172611739
/**
11727-
* [#35863] Whether the flow run `runId` has stopped for good: its status is
11728-
* one of {@link TERMINAL_RUN_STATUSES}. `false` when there is no `getRun`,
11729-
* no such run, or the read throws: display-only, so an unreadable run is
11730-
* only ever "not known to have ended", never a failed read.
11740+
* [#35863] Whether the flow run `runId` has stopped for good, having run
11741+
* `flow`: its status is one of {@link TERMINAL_RUN_STATUSES} and
11742+
* {@link ranArmedBody} holds for it. `false` when there is no `getRun`, no
11743+
* such run, or a read throws: display-only, so an unreadable run is only
11744+
* ever "not known to have ended", never a failed read.
1173111745
*/
11732-
private async runHasEnded(runId: string): Promise<boolean> {
11746+
private async runHasEnded(runId: string, flowName: string, flow: unknown): Promise<boolean> {
1173311747
if (typeof this.automation?.getRun !== 'function') return false;
1173411748
try {
11735-
const status = (await this.automation.getRun(runId))?.status;
11736-
return !!status && TERMINAL_RUN_STATUSES.has(status);
11749+
const run = await this.automation.getRun(runId);
11750+
const status = run?.status;
11751+
return !!status && TERMINAL_RUN_STATUSES.has(status) && this.ranArmedBody(run?.startedAt, flowName, flow);
1173711752
} catch {
1173811753
return false;
1173911754
}
1174011755
}
1174111756

11757+
/**
11758+
* [#35863] Whether a run that started at `startedAt` provably ran `flow`,
11759+
* the body of `flowName` armed now: `flow` is the engine's NEWEST
11760+
* registration of the name (`getFlowVersionHistory`, the very object), and
11761+
* was registered strictly before the run started. A run starts on the body
11762+
* armed at its start and resumes pinned to it, so with nothing registered
11763+
* since, that body is `flow`.
11764+
*
11765+
* Every other case is "not known", never a guess: a body registered after
11766+
* the run started (an edit, or the registration a restart makes), a body
11767+
* armed without being the newest registration (a rollback re-arms an older
11768+
* one), no history for the name, or no start time. The run's `flowVersion`
11769+
* is not the witness: it is the author's number and an edit need not change
11770+
* it.
11771+
*/
11772+
private ranArmedBody(startedAt: unknown, flowName: string, flow: unknown): boolean {
11773+
if (typeof startedAt !== 'string' || typeof this.automation?.getFlowVersionHistory !== 'function') return false;
11774+
const history = this.automation.getFlowVersionHistory(flowName);
11775+
const newest = Array.isArray(history) && history.length > 0 ? history[history.length - 1] : undefined;
11776+
if (!newest || newest.definition !== flow) return false;
11777+
const registered = Date.parse(newest.createdAt);
11778+
const started = Date.parse(startedAt);
11779+
return Number.isFinite(registered) && Number.isFinite(started) && registered < started;
11780+
}
11781+
1174211782
async listActions(requestId: string, context: ExecutionContext): Promise<ApprovalActionRow[]> {
1174311783
if (!requestId) return [];
1174411784
// Tenant gate: ensure the caller can see the parent request before

‎packages/plugins/plugin-approvals/src/flow-route.integration.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,36 @@ describe('[#26668] the approvals get serves the route a request takes, not the f
437437
});
438438
});
439439

440+
it('[#35863] a flow edited after the run is not judged by the edit: the run\'s own body hosted a subflow', async () => {
441+
// The run's body: the manager, then legal in a child run. Registered under its own name so the edit below touches no other pin.
442+
const name = 'contract_edited_after_run';
443+
automation.registerFlow(name, { ...REVIEWED_THEN_LEGAL, name } as any);
444+
await new Promise((r) => setTimeout(r, 5)); // registered strictly before the run starts
445+
const manager = await submit(name, {});
446+
const legal = await approve(manager, 'u_mgr');
447+
expect(legal.flow_run_id, 'the legal step runs in a child run').not.toBe(manager.flow_run_id);
448+
await reject(legal, 'u_legal');
449+
expect((await automation.getRun(manager.flow_run_id))?.status, 'the root run has ended').toBe('completed');
450+
expect('outcome' in (await get(manager)).flow_route).toBe(false);
451+
452+
// The flow is edited to drop its subflow, under the SAME version number: the body armed now hosts no child run.
453+
const { nodes, edges } = REVIEWED_THEN_LEGAL;
454+
automation.registerFlow(name, {
455+
...REVIEWED_THEN_LEGAL, name,
456+
nodes: nodes.filter((n) => n.id !== 'legal'),
457+
edges: [...edges.filter((e) => e.source !== 'legal' && e.target !== 'legal'), { id: 'r5', source: 'manager_review', target: 'done', label: 'approve' }],
458+
} as any);
459+
// Still no outcome: the run ran the body with the subflow, and legal's rejection was the last decision.
460+
const row = await get(manager);
461+
expect(row.flow_route).toEqual({ steps: [{ id: 'manager_review', label: 'Manager', state: 'done' }], ahead: 'decided' });
462+
463+
// A run started on the edited body does end at the manager: "ended" is judged by the body a run ran.
464+
await new Promise((r) => setTimeout(r, 5));
465+
const next = await submit(name, {});
466+
await approve(next, 'u_mgr');
467+
expect((await get(next)).flow_route.outcome).toEqual({ status: 'approved', ended: true });
468+
});
469+
440470
it('[#35863] a request whose snapshot does not say its run is a root run is never served "ended"', async () => {
441471
const manager = await submit(LINEAR.name, {});
442472
const director = await approve(manager, 'u_mgr');

‎packages/spec/src/contracts/approval-service.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,19 @@ export interface ApprovalRequestRow {
409409
* - `ended` — `true` only when the record's approval is known to have
410410
* stopped: the request's run is a root run (not a `subflow` body or a
411411
* `map` item, whose end is not the approval's: the parent run goes on),
412-
* its flow holds no `subflow` or `map` node anywhere, its regions
413-
* included (a child run started from it may have opened requests,
414-
* after this one too, that the run's own requests do not show), and
415-
* the run has stopped for good. When `status` is not `pending`, it is
416-
* then the last decision the record's approval took. `false` means
417-
* "not known to have stopped": the run is still going, is a child run,
418-
* runs a flow that can start a child run, was opened before the
419-
* platform recorded which runs are root runs, or cannot be read.
412+
* the flow body the run ran holds no `subflow` or `map` node anywhere,
413+
* its regions included (a child run started from it may have opened
414+
* requests, after this one too, that the run's own requests do not
415+
* show), and the run has stopped for good. The body the run ran is
416+
* known only when the body armed now is the flow's newest registration
417+
* and was registered before the run started; a flow edited, rolled back
418+
* or re-registered since the run started is not judged by its current
419+
* body. When `status` is not `pending`, it is then the last decision the
420+
* record's approval took. `false` means "not known to have stopped":
421+
* the run is still going, is a child run, ran a body that can start a
422+
* child run, ran a body that is not known (its flow was registered
423+
* again since it started), was opened before the platform recorded
424+
* which runs are root runs, or cannot be read.
420425
*
421426
* Absent when nothing past this request is known: no later request was
422427
* opened and the approval is not known to have ended, or several later

0 commit comments

Comments
 (0)