Skip to content
Closed
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/37767-reconciler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/plugin-approvals': patch
---

feat(approvals): the reconciler, and the door reads every run of an approval (ADR-0162 Execution item 8)

Clause-②: yes (widening)
Metadata-protocol: none

Widened: `ApprovalService` gains `reconcileMarkedApprovals(options?)`, which the plugin runs on the sweep clock beside the escalation, dead-run and stranded-request sweeps; the `getRun` member of the automation surface the plugin declares types the step log's `childRunIds`, the run's `trigger` and `completedAt`, and an optional `{ strict }` read; the plugin's message catalog gains `approvalFlowNotContinuedTitle` and `approvalFlowNotContinuedBody` in all 11 locale packs, sent to the organization's administrators on the topic `approval.flow_not_continued`.

Only for an approval the check stamped when it opened (one with a step that can act on an object that declares `closedStates`):

- **The door reads every run of the approval.** Where it read the run that carries the record and the latest request's own run, it now walks the tree from the run that carries the record, following the child runs each delegating step records: every `subflow` child, and every `map` item run that does not carry another record (an item that is a record carries an approval of its own). One run decides for the whole approval: one that another process is advancing, or that the store could not answer for, makes it cannot tell; one parked at a node a step can follow, in progress; one that ended between two steps, abandoned. So a sibling child whose failure the flow routed, a middle run two levels down that failed, an item run that failed before its step, and a run between two others that is being advanced are no longer read as finished. A failure routed to a pause no step follows is read as abandoned, as on the base. A stop is answered where a request of the approval opened after it. A tree of more than 200 runs is cannot tell.
- **The plugin keeps what it read.** At each door, and as each request opens, the approval's latest request keeps, in its config snapshot, the runs the plugin read, which of them ended (and so are not read again), and a failure between two steps it found in a run that ended. A history the automation engine trims afterwards is not needed again, so an approval whose early child run was trimmed, or whose refused outcome is tried again after its run's history went, is judged as it would have been.
- **A run that stopped, placed by the step last decided.** A run the engine has no row for (its history is gone, or a process stopped while advancing it), and the latest request's own run still parked at the step its decision was recorded on (a process stopped before the run was resumed), are cannot tell for an hour from the approval's marks (the age bound). Past it, they have stopped at the step last decided. Where a later step can follow that step, the approval is abandoned: the record is put back, the submitter is told and submits again, the decisions already made stay on their requests, and the run still parked there is cancelled. At the last step, the outcome is written, the administrators are told that the flow did not go on after that step, and the run's pause is left for an operator.
- **The reconciler.** Each pass reads two windows of at most 200 marked requests (`unjudged_since`): the oldest marks, and the next page of a walk over every marked request by id, so that marks which stay cannot keep the newer ones from being read. It judges each approval among them once, as the door would have: it writes the outcome in the name of the request's recorded decider, abandons the approval, or, where nothing was held back, clears the mark and writes nothing. It ends what no door saw: a run that was cancelled, a parent run cancelled while its child's request was pending, and the stopped runs above. Past the age bound it also reads as stopped where it stood a run that reports paused while its pause is gone, and a run parked at a `subflow` or a `map` whose child run has ended or has no row.
- **A refused write is tried again.** A refused outcome or put-back leaves the marks, and the next pass tries it again. The timeline line and the administrators' notice (keyed per request and status) are left once for one refusal; the same holds for the line an abandoned approval leaves.
- **A deleted record.** Where the approval's record was deleted, the reconciler clears the marks of its requests and writes nothing: no retry and no notice.
- **The stranded-request report** (`inspectStrandedRequests`) leaves a decided request the check stamped to the door and the reconciler, and still reports a returned one.

- **A refusing end is an end.** The door reads a run that reached an end of the flow as having ended there, a refusing end (`outcome: 'refused'`) included, as it already did for a completed run.

Unchanged: an object that declares no `closedStates`; an approval that opened before its object declared the key.
16 changes: 16 additions & 0 deletions .changeset/37767-step-child-runs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/service-automation': patch
---

feat(automation): a delegating step records the child runs it started (ADR-0162 Execution item 8)

Clause-②: yes (widening)
Metadata-protocol: none

Widened: `AutomationEngine` gains the public read `runStartedFor(context)` and an optional second argument to `getRun(runId, { strict })`; a node executor's result (`NodeExecutionResult`) and the exported step type (`StepLogEntry`) gain the optional `childRunIds`, which the engine copies onto the step's log entry (`ExecutionStepLogSchema.childRunIds`, in `@objectstack/spec`).

- **`childRunIds` on a step.** A `subflow` step names the one run it started, on every exit: paused on its child, completed, failed or refused. A `map` step names the item runs that execution of the step started. Absent on a step that started none, and on a child that paused with no run id. A log of more than 200 steps is compacted for history and can drop a region-body step with its link; a top-level step keeps it. `getRun` serves it with the rest of the step log, so a reader can walk a run's tree from its root; before this, a parent's link to its child lived only on the parent's pause (`correlation: 'subflow:<id>'`), and only while the parent was parked.
- **`runStartedFor(context)`.** A run that ends inside `execute` answers no `runId` (the contract sets it on a paused run only). The engine now answers the id of the run the last `execute` call started for that very context object, whatever it ended in. A run is named once it is past the gates that skip it and leaves a history row, or where it fails before its first node and is recorded as failed; `undefined` where no run started (the flow was missing, disabled, had no start node, or its start condition or the re-entry guard skipped it). Under `errorHandling.strategy: 'retry'` it is the last attempt that started a run. The `subflow` and `map` nodes read it for a child that ended inside the call.
- **`getRun(runId, { strict: true })`.** A store that fails is still logged and read as `null` by default. With `strict`, its failure is thrown instead, so a caller that decides on a missing run can tell a run with no row from a store that did not answer.

Unchanged: what a run does, the `runId` the contract returns, and every existing step field.
11 changes: 11 additions & 0 deletions .changeset/37767-step-log-child-runs-spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@objectstack/spec': minor
---

feat(spec): a run's step log names the child runs a step started (`ExecutionStepLogSchema.childRunIds`, ADR-0162 Execution item 8)

Clause-②: yes (widening)
Metadata-protocol: added
Skills-docs: none — `skills/` names none of `ExecutionStepLog`, `childRunIds`, `runStartedFor` or `unjudged_since` (git grep, 0 hits): the step log is a runtime record no skill teaches an author to write

Widened: `ExecutionStepLogSchema` gains the optional `childRunIds: string[]`: the run ids of the child runs the step started, the one run of a `subflow` and, for a `map`, the item runs that execution of the step started. Absent for a step that started none. The engine writes it (`@objectstack/service-automation`); a reader of `getRun` can walk a run's tree from its root with it. Nothing an author writes in a flow changes.
80 changes: 76 additions & 4 deletions content/docs/automation/approvals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,8 @@ not swallowed:
One more ground is the host's, not the author's. An approval any step of which
can act on an object that declares `closedStates`, concerned or not, opens only
on a host that runs the approvals plugin's sweeps, which needs a `job` service.
The sweeps are what release a record held by a dead run, below.
The sweeps are what release a record held by a dead run, and what judge an
approval that no action judged, below.

### An approval that does not mirror onto the closing field

Expand Down Expand Up @@ -1590,9 +1591,14 @@ request stays the rule. These are left to the first request:
Three writers abandon a request: its submitter's recall, of a pending request or
of a returned one in its revise window; an administrator's override of a pending
request, the override arm of the same recall; and the sweep that releases a
record held by a dead run. Each records `recalled` on the request. Where the
object closes on the mirror field at any value, one of its own or a status, an
abandoned request never writes a closed value of that field:
record held by a dead run. Each records `recalled` on the request. The platform
also abandons an approval whose run ended between two steps, below, at the
action that found it or in the reconciler: the requests keep the decisions
already made, a line with no actor on the latest request's timeline says that
the approval did not go on, why, and what the record was put back to, and the
submitter is told, and submits again. Where the object closes on the mirror
field at any value, one of its own or a status, an abandoned request never
writes a closed value of that field:

1. **A closed value the approval did not write is left alone.** Where the field
holds a closed value that no request of this approval on this record had as
Expand Down Expand Up @@ -1624,6 +1630,72 @@ write of empty is refused: the field keeps the value it holds, and the refusal i
logged, and reported as above where the object closes on the field at `approved`
or `rejected`.

### When the outcome lands on the record

Where the object closes on a step's mirror field at `approved` or `rejected`,
the step's decision is held back: the request records it, and the field keeps
`pending` until the approval is over for the record. Every other status is
written as before: `pending` as a request opens, `returned` at a send-back, and
what an abandoned request leaves.

After each action that moves a run of the approval on (a decision, by a person
or by the escalation clock, a send-back, a resubmission, a recall, and the
continuation of a restored run), the platform reads the approval's requests on
the record and every run of the approval: the run that carries the record and,
through the steps that started them, every `subflow` child under it, and every
`map` item run that does not carry another record (an item that is a record has
an approval of its own). It answers one of:

- **In progress.** A request is pending or returned on the record, or a run is
parked at a node between two steps. Nothing is written.
- **Ended between two steps.** A run stopped on a node a step could still follow
(it failed or was cancelled there), a failure the flow routed at the top level
of a run ended it, to an end or to a pause no step follows, or a later step
could not open, and no request of the approval opened after it. The approval
is abandoned, above.
- **Cannot tell.** A run is being advanced by another process, the store did
not answer for a run, or the requests cannot be read. Nothing is written.
- **Finished**, otherwise: every run reached an end of the flow, a refusing end
included, stopped after the last step, or is parked at a pause no step can
follow. The latest request's decision is written, in the name of the person
who decided it.

The platform keeps what it read on the approval's own requests, at each of these
actions and as each request opens: which runs it read, which of them ended, and a
failure between two steps it found in one. A run that ended is not read again,
so a flow run history the platform trims later, as it trims every flow's history,
does not hold the approval.

Two more states cannot tell at first: a run with no history left to read, which
the platform never read to its end (a process stopped while it was advancing
it), and a decided request whose run is still parked at that request's own step
(a process stopped before the run was resumed). An hour after the approval's
marks were set, either has stopped at the step last decided. Where a later step
could follow that step, the approval is abandoned, above, and a run still parked
there is cancelled, so nothing resumes it; the decisions already made stay on
their requests, and the submitter submits again. Where the step was the last,
its decision is written, the organization's administrators are told that the
flow did not go on after that step (the notice `approval.flow_not_continued`),
and the run's pause is left, so that an operator can continue the flow's
remaining nodes or cancel the run.

Every decision on an approval any step of which can act on an object that
declares `closedStates` leaves a mark on its request (`unjudged_since`), also
where nothing is held back. The mark is cleared once the approval is judged, or
when its next request opens. The plugin's sweeps run a reconciler that judges
every approval whose mark still stands, as the action would have: after a run
that was cancelled, a parent run cancelled while its child's request was pending,
or a process that stopped, as above. Each sweep reads the oldest marks and the
next page of a walk over every marked request, so that approvals that stay in
progress do not keep the newer ones from being judged. Once the mark has stood
for an hour, it also reads as stopped where it stood a run that reports paused
while its pause is gone, and a run parked at a `subflow` or a `map` whose child
run has ended or has no history left.
Where nothing was held back it clears the mark and writes nothing. A write that
a rule or a hook refuses is reported as above, once, and tried again on each
sweep while the mark stands. Where the record was deleted, the reconciler clears
the mark and writes nothing.

## Why

Separating *who configures* from *who approves* from *as whom it runs* is the same capability/assignment/requirement decoupling as the rest of authorization (ADR-0066). The `runAs` default of `user` means an approval flow can't silently grant the submitter cross-tenant or cross-owner reach — elevation is opt-in and auditable. That is the safe-by-default posture: the common case respects RLS; the elevated case is explicit.
Expand Down
2 changes: 2 additions & 0 deletions content/docs/references/automation/execution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const result = CheckpointSchema.parse(data);
| **skippedBy** | `{ nodeId: string; edgeId?: string; label?: string }` | optional | The gate that closed, when `status` is `skipped` |
| **warnings** | `string[]` | optional | Advisory warnings from the node executor. A `success` step may carry them — e.g. a write whose requested fields the data engine legally stripped — so a step that partially did not land never reads as a clean success. Absent when the executor raised none |
| **droppedFields** | `{ object: string; fields: string[]; reason: Enum<'readonly' \| 'readonly_when' \| 'primary_key' \| 'computed' \| 'stored_expression'> }[]` | optional | The data engine's own drop events behind those warnings: which caller-supplied fields of which object the write did not store, and why. Absent when the step dropped nothing |
| **childRunIds** | `string[]` | optional | Run ids of the child runs this step started: the one run of a `subflow`, and for a `map` the item runs this execution of the step started. Absent for a step that started none |

### Nested Shape: `ExecutionLog.summary`

Expand Down Expand Up @@ -210,6 +211,7 @@ const result = CheckpointSchema.parse(data);
| **skippedBy** | `{ nodeId: string; edgeId?: string; label?: string }` | optional | The gate that closed, when `status` is `skipped` |
| **warnings** | `string[]` | optional | Advisory warnings from the node executor. A `success` step may carry them — e.g. a write whose requested fields the data engine legally stripped — so a step that partially did not land never reads as a clean success. Absent when the executor raised none |
| **droppedFields** | `{ object: string; fields: string[]; reason: Enum<'readonly' \| 'readonly_when' \| 'primary_key' \| 'computed' \| 'stored_expression'> }[]` | optional | The data engine's own drop events behind those warnings: which caller-supplied fields of which object the write did not store, and why. Absent when the step dropped nothing |
| **childRunIds** | `string[]` | optional | Run ids of the child runs this step started: the one run of a `subflow`, and for a `map` the item runs this execution of the step started. Absent for a step that started none |

### Nested Shape: `ExecutionStepLog.error`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ a row or a number here — fix the code, or the census, and regenerate.

| Measure | Value |
|---|---:|
| Write call sites | 49 |
| Object name statically decidable | 47 |
| Write call sites | 50 |
| Object name statically decidable | 48 |
| Object name chosen at run time | 2 |
| Object name spelled inline | 45 |
| Object name spelled inline | 46 |
| Object name spelled through a `const` | 2 |
| Object name is an `object: string` parameter | 1 |
| Object name is some other run-time expression | 1 |
| Against a tenancy-enabled object | 47 |
| Against a tenancy-enabled object | 48 |
| Against an object declaring tenancy off | 0 |
| Threading a tenant context | 49 |
| Threading a tenant context | 50 |
| Provably carrying none (options read, no context key) | 0 |
| …and decidably tenancy-enabled | 0 |
| Options argument unreadable | 0 |
| …and decidably tenancy-enabled | 0 |
| Threading a decidably elevated (`isSystem`) context | 47 |
| Threading a decidably elevated (`isSystem`) context | 48 |
| Threading a decidably non-elevated context | 0 |
| Threading a context of undecidable elevation | 2 |
| Receiver carried a readable engine type | 49 |
| Receiver carried a readable engine type | 50 |
| Receiver erased, placed by the object NAME | 0 |
| Receiver erased, placed by an `object: string` PARAMETER | 0 |
| Receiver erased, placed by an `UNTYPED_RECEIVERS` row | 0 |
Expand All @@ -54,7 +54,7 @@ a row or a number here — fix the code, or the census, and regenerate.
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `delete` | `sys_approval_approver` | enabled | elevated | 2 |
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `insert` | `sys_approval_approver` | enabled | elevated | 2 |
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `insert` | `sys_approval_request` | enabled | elevated | 1 |
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `update` | `sys_approval_request` | enabled | elevated | 14 |
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `update` | `sys_approval_request` | enabled | elevated | 15 |
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `insert` | `sys_approval_token` | enabled | elevated | 1 |
| `packages/plugins/plugin-approvals/src/approval-service.ts` | `update` | `sys_approval_token` | enabled | elevated | 1 |
| `packages/plugins/plugin-approvals/src/backfill-platform-row-organizations.ts` | `update` | `objectPlan.object` | undecidable | context, elevation undecidable | 1 |
Expand Down
Loading
Loading