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
10 changes: 10 additions & 0 deletions .changeset/37767-hold-root-run-spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/spec': patch
---

feat(spec): a run's writes name the root run of its tree (`ExecutionContext.flowRootRunId`, `HookContext.provenance.flowRootRunId`, ADR-0162 D5)

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

Widened: `ExecutionContextSchema` and the `provenance` of `HookContextSchema` gain the optional string `flowRootRunId`, beside `flowRunId`: the run id of the root of the run tree the writing run belongs to (the run itself for a root run). The automation engine stamps it on every write a run makes; it is never read from a request. Nothing an author writes changes.
13 changes: 13 additions & 0 deletions .changeset/37767-hold-root-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/core': patch
'@objectstack/objectql': patch
'@objectstack/service-automation': patch
'@objectstack/runtime': patch
---

feat(automation): every write of a run carries the root run of its tree (`flowRootRunId`, ADR-0162 D5)

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

Widened: a data write a run makes (`resolveRunDataContext`, the run identity of `@objectstack/service-automation`) carries `flowRootRunId`, the run id of the root of its run tree, beside `flowRunId`; the engine carries it onto the write's `HookContext.provenance` (`@objectstack/objectql`), the platform system api strips it with the other run marks, a sandboxed body and script see it among the declared provenance members (`@objectstack/runtime`), and it is one of the fields an entry point may not set on an `ExecutionContext` (`assembleExecutionContext`, `@objectstack/core`). The approvals plugin reads it to let every run of an approval through the hold. Absent on a write that is not a run's.
23 changes: 23 additions & 0 deletions .changeset/37767-hold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/plugin-approvals': minor
---

feat(approvals)!: on an object that declares `closedStates`, the document is held for the whole approval (ADR-0162 D5, Execution item 8, the hold)

Clause-②: yes (narrowing)
Metadata-protocol: semantic
Skills-docs: none — `skills/` names no `closedStates` (git grep, 0 hits); its `lockRecord` rows describe the lock while a request is pending, which this does not change, and `content/docs/automation/approvals.mdx` teaches the hold

**BREAKING** for a caller that edits a record of an object that declares `closedStates` while its approval is between two steps, or after its last decision until the approval is over: FROM the edit landing (the record was locked only while a request was pending, and only under a node whose `lockRecord` is not `false`) TO the edit refused `409 RECORD_LOCKED`. The same holds for an automation that runs as a user outside the approval's own flow, such as a record-change flow with `runAs: 'user'` that a decision sets off. The one-line fix: make the edit before submitting or after the approval is over, or make it from the approval's own flow, whose every run passes; for such an automation, run it with `runAs: 'system'`. No metadata changes and no stored metadata is refused.

What the hold is. Every decision on such an approval leaves a mark on its request (`unjudged_since`), cleared once the approval is judged or its next request opens. While a mark stands, the pending lock holds the record and its direct `master_detail` lines as a pending request under a locking node does, whatever field the object closes on and whatever the node's `lockRecord` says. It passes:

- a system context, the approval status mirror among them;
- a write that carries only the mirror field;
- a write by any run of the approval, the record's run or a `subflow` or `map` run under it at any depth, known by the root run (`flowRootRunId`) the automation engine now carries on every write of a run; a hook's write through its own `ctx.api` carries it, a write a hook makes as another caller does not;
- the revise window of a returned request;
- the record's own delete, which lands and ends the approval: it is marked abandoned on its latest request, its marks are cleared, and nothing is written (the reconciler does the same for a record deleted another way).

An approval that opened before its object declared `closedStates` left no marks and is not held. The approvals-family lock read (`getRecordLock`, the `/lock` and `/locks` routes) answers for a pending request only, as published: in a passage it answers `{ "locked": false }`.

<!-- adr-0087: not-required (no-migration-prescription) no key, value or metadata type is removed or renamed: what changes is when a caller's edit of such a record is refused, which no metadata rewrite can migrate -->
64 changes: 62 additions & 2 deletions content/docs/automation/approvals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ never deadlock against itself. The exemption is keyed on run identity rather tha
elevation, so a `runAs:'user'` run stays row-level-security scoped while it
writes — it does not become a system write.

On an object that declares `closedStates`, the document is held for the whole
approval, between its steps as well, and every run of the approval writes it:
see [the hold](#hold).

**The record's lines are locked with it.** Rows of a `master_detail` child of the
locked record (an expense claim's lines, an opportunity's products) refuse insert,
update and delete with the same `409 RECORD_LOCKED`, and so does moving a row into
Expand Down Expand Up @@ -1638,6 +1642,12 @@ the step's decision is held back: the request records it, and the field keeps
written as before: `pending` as a request opens, `returned` at a send-back, and
what an abandoned request leaves.

So a node that follows a decision in the flow reads `pending` on the field, not
the decision, and a flow or a trigger that fires when the field becomes
`approved` fires once, when the approval is over, not at each step. A node that
needs a step's decision reads it on the step's outcome, as the flow's own edges
do.

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
Expand Down Expand Up @@ -1769,8 +1779,58 @@ own approval, only whether the run has ended.
Where nothing was held back it clears the mark and writes nothing; where it
concluded that a run had stopped, a line in the server log says so. 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.
sweep while the mark stands. Where the record was deleted, the approval is over:
it is marked abandoned on its latest request, its marks are cleared, and nothing
is written. The delete does that as it lands, below, and the reconciler does it
for a record that went another way.

### The document is held for the whole approval [#hold]

On an object that declares `closedStates`, every approval holds the document from
its submission until it is over: while a request is pending, as on any object,
and also through each passage, from a step's decision until the next request
opens or the approval is over (ADR-0162 D5). A passage holds the record and its
direct `master_detail` lines as a pending request does, whatever field the
object closes on and whatever the step's `lockRecord` says: a step that leaves
the record open while its request is pending (`lockRecord: false`) still holds
it once that request is decided. A caller's edit in a passage is refused
`409 RECORD_LOCKED`, so the document cannot change between the decision on it
and the outcome the approval writes. That caller includes an automation that
runs as a user outside the approval's own flow: a record-change flow with
`runAs: 'user'` that a decision sets off starts a run tree of its own, and its
write to the document is refused the same way. Run such a flow with
`runAs: 'system'`, or make the write from the approval's own flow.

What passes the hold:

- **The approval's own runs.** A write made by any run of the approval, the run
started for the record or a `subflow` or `map` run under it at any depth, a
hook's write through its own `ctx.api` included, lands. A run is known by the
root run it belongs to, which the automation engine carries on every write of
a run (`flowRootRunId`, beside `flowRunId`). A write a hook makes outside that
context, as another caller, is refused.
- **A system context**, the approval status mirror among them.
- **A write that carries only the mirror field.**
- **The revise window** of a returned request, as on any object.
- **The record's own delete.** It lands and ends the approval: the approval is
marked abandoned on its latest request, its marks are cleared, and nothing is
written. A line's delete, or a delete that cascades into a line, is held as
an edit is.

A passage lasts as long as the approval's marks stand (`unjudged_since`, above).
A run that pauses between two steps, at a `wait` or a screen, holds the
document while it waits; so does a process that stops in a passage, until the
reconciler judges the approval: at its next sweep where the runs can be read,
otherwise once the hour above has passed. Where that is too long for the
business, the flow should not pause between two steps on such an object.

An approval that was in flight when its object first declared `closedStates`
left no marks, and is not held between its steps.

The approvals-family lock read (`getRecordLock`, above) answers for a pending
request only: in a passage it answers `{ "locked": false }` while an edit is
refused `409 RECORD_LOCKED`. A page that offers an edit in a passage learns of
the hold from the refusal.

## Why

Expand Down
1 change: 1 addition & 0 deletions content/docs/automation/hook-bodies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ A hook body sees the same server-stamped write provenance an in-process handler
| Member | Present when |
|---|---|
| `ctx.provenance.flowRunId` | The write comes from an automation flow's data node; the value is that flow run's id. |
| `ctx.provenance.flowRootRunId` | Set beside `flowRunId`: the id of the root run of that run's tree, the run itself for a run no other run started. Absent on a run the engine could not place in a tree. |
| `ctx.provenance.attributedUserId` | The write is authorized as the system and credited to a real person (for example, the admin behind an identity-adapter call). |
| `ctx.provenance.performedByClientId` | An AI client acting through the MCP OAuth door performed the write on behalf of the user in `ctx.session.userId`. |
| `ctx.provenance.performedByAgentId` | The in-product AI agent performed the write for the signed-in person in `ctx.session.userId`, whose write it runs as; the value is the agent's name. |
Expand Down
12 changes: 12 additions & 0 deletions content/docs/references/data/data-engine.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const result = BaseEngineOptionsSchema.parse(data);
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -128,6 +129,7 @@ Options for DataEngine.aggregate operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -184,6 +186,7 @@ Options for DataEngine.count operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -241,6 +244,7 @@ Options for DataEngine.delete operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -320,6 +324,7 @@ Options for DataEngine.insert operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -382,6 +387,7 @@ Query options for IDataEngine.find() operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -470,6 +476,7 @@ Options for DataEngine.update operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -547,6 +554,7 @@ QueryAST-aligned options for DataEngine.aggregate operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -636,6 +644,7 @@ QueryAST-aligned options for DataEngine.count operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -707,6 +716,7 @@ QueryAST-aligned options for DataEngine.delete operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -773,6 +783,7 @@ QueryAST-aligned query options for IDataEngine.find() operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down Expand Up @@ -867,6 +878,7 @@ QueryAST-aligned options for DataEngine.update operations
| **rlsMembership** | `Record<string, string[]>` | optional | |
| **isSystem** | `boolean` | optional (default: `false`) | |
| **flowRunId** | `string` | optional | |
| **flowRootRunId** | `string` | optional | |
| **skipTriggers** | `boolean` | optional | |
| **skipAutomations** | `boolean` | optional | |
| **seedReplay** | `boolean` | optional | |
Expand Down
Loading
Loading