Skip to content

Commit 08255e0

Browse files
feat(approvals): the service that owns an approval step can refuse a decision before it is recorded (#39143)
Fixes #39126 Clause-②: yes Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:审批人在拥有该审批步骤的服务明确不允许时去批准、驳回或退回(包括退回次数用尽后自动变成驳回的那一次),现在直接收到「无权」提示和对方给出的原因,申请仍是待审,什么都不会被记下;以前是先记成已决定,再报服务器错误。 - 为什么做:云端市场包审核遇到了这个问题(组织管理员借管理员兜底批准了旧的审核申请),按维护者选定的方案一,让拥有审批流程的服务在记录之前先判。 - 出错会怎样:只有主动登记了这种检查的服务才会受影响,其他审批流程行为不变;检查本身出错时一律拒绝且不写任何东西;可以直接撤回本次改动。 - 要你做什么:无。 ## What changes - **A declared admission, asked before anything is recorded.** `IApprovalService` gains an optional `registerDecisionAdmission({ flow, check })` (`ApprovalDecisionAdmission`, `ApprovalDecisionAdmissionInput`, `ApprovalDecisionRefusal`, `ApprovalDecisionVerb` in `@objectstack/spec/contracts`). The service that owns a flow registers one check per flow name; the approvals service asks it for every decision on an approval step of that flow. - **Where it runs.** `decideNode` asks it last among its refusals and before its first write (the audit row), so every approve and reject reaches it: the REST routes, the action door, the action link, the SLA sweep's auto-decision, a slot holder and the administrator override alike. `sendBack` asks it too, before its audit row, as what it records: `revise`, or `reject` once the step's revision budget is spent (that arm records a `reject` row, sets the request `rejected` and resumes the reject edge, so a rule "anyone may send back, only X may reject" holds there too; the budget read moved above the check, and it is a read). The check receives the request's facts (id, flow, package, node, run, object, record, organization, submitter), the verb, the person the decision would be recorded against, `viaOverride`, the comment and the decider's context. - **Refusal.** A check that answers `{ userMessage, message? }` refuses: the service throws `FORBIDDEN: ...` with `status: 403`, `code: 'FORBIDDEN'` and the check's `userMessage`, the shape the approvals doors already answer as `403 FORBIDDEN` with `userMessage` at the top of the body. Nothing is written: no action row, the request stays `pending` on the same slate, the approver index and the status mirror are untouched, the run stays parked, and no notification goes out. A blank `userMessage` still refuses, without one. - **Fail closed.** Any non-nullish answer that is not a refusal object (`false`, a string) refuses too, without a `userMessage`. A check that throws fails closed: its throw propagates unchanged (a throw declaring its own `status`/`code` answers them at the door, any other a 5xx) and nothing is recorded. - **Registration.** Sync or async. One admission per flow name: a second registration throws and the first stands; the returned function removes only its own registration. A flow with no admission behaves exactly as before. - **Where it lives.** `approval-service.ts` is far past the size where a new concern gets its own module, so the registry and the refusal live in `packages/plugins/plugin-approvals/src/decision-admission.ts`; the service holds one instance, a delegating `registerDecisionAdmission`, and two calls. - **No ADR.** AGENTS.md Prime Directive 13 asks for an ADR when a recorded decision is reversed; this adds a seam and reverses none. ADR-0019-approval-as-flow-node already puts the decision's authorization with the owning service and the resume behind it; the admission runs inside that authorization, before the record. The decision of record is the maintainer ruling 7344644972 on this card (option A), cited where the code implements it. - Generated: `packages/spec/api-surface/contracts.json` and `packages/spec/export-origins/contracts.json` gain the four new exports (`check:generated --fix`, only the two it proved stale). `scripts/engine-double-contract.pinned.json` records the new suite's two engine doubles (`check-engine-double-contract.mjs --write`, 0 lost). - **Contract jsdoc.** `viaOverride` is documented as the arm the decision took, not the decider's standing (an override actor who reassigned the request to themselves decides as a slot holder); a rule about who decides reads `actorId` and `context`. The contract also says recall is not asked. - **Guide.** One paragraph in `content/docs/automation/approvals.mdx` under "The decision" names the seam (not a governed path). - Changeset: `@objectstack/spec` and `@objectstack/plugin-approvals`, patch (`Metadata-protocol: none`). ## Tests `packages/plugins/plugin-approvals/src/decision-admission.test.ts` (14 tests), a real `AutomationEngine` with the real approval node and an owning node that refuses any decider but the platform administrator: - CONTROL, no admission registered: an organization administrator's override approval answers `RESUME_FAILED` and leaves the request `approved`, the owning node never applied and no outcome notice sent. This is the measured defect, unchanged where nothing is registered. - With the admission: the override approval, a slot holder's reject and a send-back are each refused with `code: 'FORBIDDEN'`, `status: 403` and the check's `userMessage`; every stored row is equal before and after, the request is `pending` on its slate, the run is still suspended, no message was emitted and the owning node never ran. The check saw the request's facts, `decision`, `actorId` and `viaOverride: true` for the override. - A send-back past its revision budget, under a check that admits `revise` and refuses `reject`: refused, 403 with the `userMessage`, nothing recorded, the check asked once as `reject`. CONTROL with budget left: the same check admits the send-back, asked as `revise`, and the request is `returned`. - The SLA sweep's `auto_approve` under a refusing check: the check is asked as `approve` with `actorId: null`, `viaOverride: true` (a system context holds no slot) and `context.isSystem`; no decision is recorded, the request is `pending` on its slate, the run is parked, no outcome notice. The sweep's own `escalate` marker, written before it decides, stays, so the next sweep does not ask again (pinned). - An admitted decision (the platform administrator's own slot) is recorded, resumed, applied by the owning node and the submitter is told. - A non-object answer (`false`, `'no'`, `0`) refuses with no `userMessage`, nothing recorded. - An async refusal; a throwing check (its own error object reaches the caller, nothing recorded); a blank `userMessage` (refused, no member); a flow the admission was not registered for is never asked; one owner per flow, malformed registrations refused, the disposer. <details><summary>Ablations (each via scripts/ablation-replace.mjs against the committed source; restore verified by blob hash)</summary> - A1, the `decideNode` call removed: 6 failed / 4 passed. The six are every decide refusal case; each now reads `RESUME_FAILED: the ... decision was recorded on request ...`. Restore: blob `d1126da3f038` equals HEAD, `git status` clean. - A2, the `sendBack` call removed: 1 failed / 9 passed (the send-back case). Restore: blob `d1126da3f038` equals HEAD. - Round 2, at `e0bed8150`: A3, the send-back verb forced back to `revise`: 1 failed / 13 passed, the budget-spent case, which then reads `RESUME_FAILED: the auto-rejection was recorded ...` (the bypass the review named). A1 again with the new suite: 8 failed / 6 passed, the SLA case among them. Both restores: blob `5e578ece1828` equals HEAD, `git status` clean. - The suite imports the service by relative path (source, not `dist/`), so no rebuild leg applies. </details> ## Acceptance notes - **Recall is not asked, and the override reaches it.** On a pending request `recall` admits the submitter, a system context and the administrator override actor. The ruling names `decideNode`, so recall is outside this PR. It still finalises the request `recalled` and resumes the run down the `reject` edge with `decision = 'recall'`. So an organization administrator can still recall a marketplace review through the override, and cloud's finalize refuses after the record: the card's defect shape on a third door. The PM files a card for it; whether the admission gains a `recall` verb is the maintainer's ruling. - **A refused SLA auto-decision is single-shot.** The sweep writes its `escalate` marker (ADR-0042's idempotency row) before it decides, so a request whose auto-decision the check refuses stays pending, overdue, and is not escalated again. This is the existing behaviour for any auto-decision that throws (a required decision output, for one); it is pinned here and not changed. - **Keyed by flow name.** A package-scoped flow (ADR-0150) shares its name with any other package's flow of the same name; the check receives `packageId` and `nodeId` to narrow. A host-registered flow such as the marketplace review carries no package. - **The REST door was not re-driven end to end here.** The throw's shape (`FORBIDDEN:` prefix plus `userMessage`) is the one `rest-approvals-wire-codes.test.ts` already pins as `403 { code: 'FORBIDDEN', error, userMessage }` on the decide route. ## Verification <details><summary>Local runs at the head named in the report</summary> - `pnpm --filter @objectstack/plugin-approvals exec vitest run --maxWorkers=2` at `e0bed8150`: 148 files, 2364 tests passed. - `pnpm --filter @objectstack/plugin-approvals typecheck` (build, scripts and the test program) and `pnpm --filter @objectstack/spec typecheck`: exit 0. - `pnpm --filter @objectstack/spec build && pnpm --filter @objectstack/spec check:generated`: 2 stale before the regeneration commit (api-surface, export-origins), all current after. - `node scripts/pm/dispatch-gates.mjs --commands` at `e0bed8150`, reconciled with `--ran`: 127 derived, 124 run, 3 NOT-MEASURED (`check:dual-build-cjs-loads` and `check:i18n` refuse without a full workspace build; `check:type-check-debt` outruns the local timeout), 0 UNRUN. The os-dev report addendum on the card has each exit line. </details> Read first: the two `decisionAdmissions.admit` calls in `approval-service.ts` (their placement before the first write is the whole fix, and the `sendBack` one picks `reject` past the budget), `packages/plugins/plugin-approvals/src/decision-admission.ts` (the registry and the refusal shape), then `packages/spec/src/contracts/approval-service.ts` (`ApprovalDecisionAdmission`). Two-minute check: `pnpm --filter @objectstack/plugin-approvals exec vitest run src/decision-admission.test.ts`, expect `14 passed`. --- _Generated by [Claude Code](https://claude.ai/code/session_01TsNV8Bsjqmw7aXCo2QuLU1)_ <!-- signed: repo:cloud#3 · zhuangjianguo · session_01TsNV8Bsjqmw7aXCo2QuLU1 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8236b79 commit 08255e0

9 files changed

Lines changed: 828 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@objectstack/spec': patch
3+
'@objectstack/plugin-approvals': patch
4+
---
5+
6+
feat(approvals): the service that owns an approval step can refuse a decision before it is recorded
7+
8+
Clause-②: yes
9+
Metadata-protocol: none
10+
11+
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.
12+
13+
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).

‎content/docs/automation/approvals.mdx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,15 @@ decision still lands. The approver slate, the `sys_approval_action` row and the
917917
status mirror all live on this path; it is the only one that produces a
918918
consistent outcome.
919919

920+
A service that owns a flow's approval steps can refuse a decision **before it is
921+
recorded** by registering a decision admission on the approvals service
922+
(`registerDecisionAdmission({ flow, check })`, `ApprovalDecisionAdmission` in
923+
`@objectstack/spec/contracts`). Every approve, reject and send-back on a step of
924+
that flow is asked first (a send-back past its revision budget is asked as the
925+
`reject` it records); a refusal answers 403 (`FORBIDDEN`) with the check's
926+
`userMessage`, and nothing is recorded — the request stays pending and the run
927+
stays parked. A recall is not asked.
928+
920929
A decision may carry **file attachments** — `attachments: string[]` of `sys_file`
921930
ids — recorded on its audit row (e.g. a signed contract on the approve):
922931

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import type {
4545
ApprovalRequestRow,
4646
ApprovalActionRow,
4747
ApprovalActionAttachment,
48+
ApprovalDecisionAdmission,
4849
ApprovalDecisionInput,
4950
ApprovalDecisionResult,
5051
ApprovalRecallInput,
@@ -68,6 +69,7 @@ import type {
6869
import type { ExecutionContext } from '@objectstack/spec/kernel';
6970
import { FlowInnerRefusalSchema } from '@objectstack/spec/api';
7071
import { RESUME_AUTHORITY_SERVICE } from '@objectstack/spec/contracts';
72+
import { DecisionAdmissions } from './decision-admission.js';
7173
import type { IOwnershipQueueService } from '@objectstack/spec/contracts';
7274
import type { IWorkCalendarService } from '@objectstack/spec/contracts';
7375
import {
@@ -3390,6 +3392,22 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
33903392
this.messaging = messaging;
33913393
}
33923394

3395+
/**
3396+
* [Ruling record 7344644972, option A] The decision admissions the services
3397+
* that own approval steps registered ({@link registerDecisionAdmission}),
3398+
* asked by `decideNode` and `sendBack` before their first write.
3399+
*/
3400+
private readonly decisionAdmissions = new DecisionAdmissions();
3401+
3402+
/**
3403+
* Attach the check the service that owns a flow's approval steps runs
3404+
* before any decision on them is recorded (`IApprovalService`'s
3405+
* `registerDecisionAdmission`; the rules live in `decision-admission.ts`).
3406+
*/
3407+
registerDecisionAdmission(admission: ApprovalDecisionAdmission): () => void {
3408+
return this.decisionAdmissions.register(admission);
3409+
}
3410+
33933411
/**
33943412
* Best-effort notification fan-out — failures only log.
33953413
*
@@ -8869,6 +8887,15 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
88698887
// resume is just as stuck, and catching it here keeps the tally honest.
88708888
await this.assertRunResumable(runId, requestId);
88718889

8890+
// [Ruling record 7344644972, option A] The service that owns this step's
8891+
// flow judges the decision before any of it is written: a refusal answers
8892+
// 403 with its userMessage and leaves the request pending, its approver
8893+
// index unchanged and its run parked. Last of the refusals, for every
8894+
// verb and the override arm alike.
8895+
await this.decisionAdmissions.admit(raw, {
8896+
decision: input.decision, actorId: recordedActor(context), viaOverride, comment: input.comment,
8897+
}, context);
8898+
88728899
// Audit the decision first so the quorum/per_group tally below sees it.
88738900
await this.engine.insert('sys_approval_action', await this.actionRow(raw, {
88748901
id: uid('aact'), request_id: requestId, organization_id: org,
@@ -10392,6 +10419,15 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1039210419
});
1039310420
priorSendBacks = Array.isArray(siblings) ? siblings.length : 0;
1039410421
}
10422+
// [Ruling record 7344644972, option A] As in decideNode: the owning
10423+
// service judges the send-back before anything is written. Asked as what
10424+
// this call will record: a `revise`, or — past the revision budget, where
10425+
// it records a `reject` and resumes the reject edge — a `reject`. The
10426+
// override does not reach this door, so it is never `viaOverride`.
10427+
await this.decisionAdmissions.admit(raw, {
10428+
decision: priorSendBacks >= maxRevisions ? 'reject' : 'revise',
10429+
actorId: recordedActor(context), viaOverride: false, comment: input.comment,
10430+
}, context);
1039510431

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

0 commit comments

Comments
 (0)