Repository navigation
Commit 08255e0
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
- .changeset
- content/docs/automation
- packages
- plugins/plugin-approvals/src
- spec
- api-surface
- export-origins
- src/contracts
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
917 | 917 | | |
918 | 918 | | |
919 | 919 | | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
920 | 929 | | |
921 | 930 | | |
922 | 931 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
| 72 | + | |
71 | 73 | | |
72 | 74 | | |
73 | 75 | | |
| |||
3390 | 3392 | | |
3391 | 3393 | | |
3392 | 3394 | | |
| 3395 | + | |
| 3396 | + | |
| 3397 | + | |
| 3398 | + | |
| 3399 | + | |
| 3400 | + | |
| 3401 | + | |
| 3402 | + | |
| 3403 | + | |
| 3404 | + | |
| 3405 | + | |
| 3406 | + | |
| 3407 | + | |
| 3408 | + | |
| 3409 | + | |
| 3410 | + | |
3393 | 3411 | | |
3394 | 3412 | | |
3395 | 3413 | | |
| |||
8869 | 8887 | | |
8870 | 8888 | | |
8871 | 8889 | | |
| 8890 | + | |
| 8891 | + | |
| 8892 | + | |
| 8893 | + | |
| 8894 | + | |
| 8895 | + | |
| 8896 | + | |
| 8897 | + | |
| 8898 | + | |
8872 | 8899 | | |
8873 | 8900 | | |
8874 | 8901 | | |
| |||
10392 | 10419 | | |
10393 | 10420 | | |
10394 | 10421 | | |
| 10422 | + | |
| 10423 | + | |
| 10424 | + | |
| 10425 | + | |
| 10426 | + | |
| 10427 | + | |
| 10428 | + | |
| 10429 | + | |
| 10430 | + | |
10395 | 10431 | | |
10396 | 10432 | | |
10397 | 10433 | | |
| |||
0 commit comments