Skip to content
13 changes: 13 additions & 0 deletions .changeset/39126-decision-admission.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/spec': patch
'@objectstack/plugin-approvals': patch
---

feat(approvals): the service that owns an approval step can refuse a decision before it is recorded

Clause-②: yes
Metadata-protocol: none

A service that owns a flow's approval steps registers a check with `approvals.registerDecisionAdmission({ flow, check })` (`ApprovalDecisionAdmission` in `@objectstack/spec/contracts`, an optional member of `IApprovalService`). Every decision on an approval step of that flow is asked of it after the approvals service's own refusals and before anything is written: an approve or a reject through any door (REST, the action door, the action link, the SLA sweep's auto-decision), for a slot holder and for the administrator override alike, and a send-back, asked as what it records: `revise`, or `reject` once the step's revision budget is spent. A recall is not asked. A refusal (`{ userMessage, message? }`) answers `403 FORBIDDEN` with that `userMessage`; nothing is recorded, the request stays pending with its approvers and approver index unchanged, the run stays parked and no one is notified. Any other non-nullish answer refuses too, without a `userMessage`. A check that throws fails closed: its throw reaches the caller and nothing is recorded. An SLA auto-decision the check refuses leaves the request pending; the sweep's single-shot `escalate` marker is already written, so the sweep does not ask again. One admission per flow name; a second registration throws.

Before this, a rule the owning flow enforced in a node after the approval could only refuse after the decision was recorded: the door answered `500 RESUME_FAILED`, the request read decided and the submitter was told nothing. Nothing changes for a flow with no admission registered. To adopt it, register the check where the flow is registered, probing for the member first (an implementation without it still satisfies the contract).
9 changes: 9 additions & 0 deletions content/docs/automation/approvals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,15 @@ decision still lands. The approver slate, the `sys_approval_action` row and the
status mirror all live on this path; it is the only one that produces a
consistent outcome.

A service that owns a flow's approval steps can refuse a decision **before it is
recorded** by registering a decision admission on the approvals service
(`registerDecisionAdmission({ flow, check })`, `ApprovalDecisionAdmission` in
`@objectstack/spec/contracts`). Every approve, reject and send-back on a step of
that flow is asked first (a send-back past its revision budget is asked as the
`reject` it records); a refusal answers 403 (`FORBIDDEN`) with the check's
`userMessage`, and nothing is recorded — the request stays pending and the run
stays parked. A recall is not asked.

A decision may carry **file attachments** — `attachments: string[]` of `sys_file`
ids — recorded on its audit row (e.g. a signed contract on the approve):

Expand Down
36 changes: 36 additions & 0 deletions packages/plugins/plugin-approvals/src/approval-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import type {
ApprovalRequestRow,
ApprovalActionRow,
ApprovalActionAttachment,
ApprovalDecisionAdmission,
ApprovalDecisionInput,
ApprovalDecisionResult,
ApprovalRecallInput,
Expand All @@ -68,6 +69,7 @@ import type {
import type { ExecutionContext } from '@objectstack/spec/kernel';
import { FlowInnerRefusalSchema } from '@objectstack/spec/api';
import { RESUME_AUTHORITY_SERVICE } from '@objectstack/spec/contracts';
import { DecisionAdmissions } from './decision-admission.js';
import type { IOwnershipQueueService } from '@objectstack/spec/contracts';
import type { IWorkCalendarService } from '@objectstack/spec/contracts';
import {
Expand Down Expand Up @@ -3390,6 +3392,22 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
this.messaging = messaging;
}

/**
* [Ruling record 7344644972, option A] The decision admissions the services
* that own approval steps registered ({@link registerDecisionAdmission}),
* asked by `decideNode` and `sendBack` before their first write.
*/
private readonly decisionAdmissions = new DecisionAdmissions();

/**
* Attach the check the service that owns a flow's approval steps runs
* before any decision on them is recorded (`IApprovalService`'s
* `registerDecisionAdmission`; the rules live in `decision-admission.ts`).
*/
registerDecisionAdmission(admission: ApprovalDecisionAdmission): () => void {
return this.decisionAdmissions.register(admission);
}

/**
* Best-effort notification fan-out — failures only log.
*
Expand Down Expand Up @@ -8869,6 +8887,15 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
// resume is just as stuck, and catching it here keeps the tally honest.
await this.assertRunResumable(runId, requestId);

// [Ruling record 7344644972, option A] The service that owns this step's
// flow judges the decision before any of it is written: a refusal answers
// 403 with its userMessage and leaves the request pending, its approver
// index unchanged and its run parked. Last of the refusals, for every
// verb and the override arm alike.
await this.decisionAdmissions.admit(raw, {
decision: input.decision, actorId: recordedActor(context), viaOverride, comment: input.comment,
}, context);

// Audit the decision first so the quorum/per_group tally below sees it.
await this.engine.insert('sys_approval_action', await this.actionRow(raw, {
id: uid('aact'), request_id: requestId, organization_id: org,
Expand Down Expand Up @@ -10392,6 +10419,15 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
});
priorSendBacks = Array.isArray(siblings) ? siblings.length : 0;
}
// [Ruling record 7344644972, option A] As in decideNode: the owning
// service judges the send-back before anything is written. Asked as what
// this call will record: a `revise`, or — past the revision budget, where
// it records a `reject` and resumes the reject edge — a `reject`. The
// override does not reach this door, so it is never `viaOverride`.
await this.decisionAdmissions.admit(raw, {
decision: priorSendBacks >= maxRevisions ? 'reject' : 'revise',
actorId: recordedActor(context), viaOverride: false, comment: input.comment,
}, context);

// Audit the revise intent first (audit-first, like decideNode) — on the
// auto-reject path the trail then reads `revise → reject`, preserving
Expand Down
Loading
Loading