diff --git a/.changeset/35659-approval-reject-requires-reason.md b/.changeset/35659-approval-reject-requires-reason.md new file mode 100644 index 00000000000..6181d2d7a3b --- /dev/null +++ b/.changeset/35659-approval-reject-requires-reason.md @@ -0,0 +1,16 @@ +--- +'@objectstack/plugin-approvals': patch +--- + +fix(approvals): rejecting through the `approval_reject` action requires a reason + +Clause-②: no (narrowing) +Metadata-protocol: none + + + +**BREAKING** for a caller of the `approval_reject` action on `sys_approval_request` that sends no `comment`: the call used to reject the request and is now refused before the approvals route runs. It ships `patch` because it is a non-protocol breaking change (AGENTS.md Post-Task Checklist step 3): the declaration belongs to this plugin, and nothing a metadata author writes changes, so `Metadata-protocol` is `none`. + +`approval_reject` declares its `comment` param `required: true`, so a reject through `approval_reject` always carries its reason. The declared param contract judges the call before the target route runs, and it counts an empty or whitespace-only string as missing: the REST action door (`POST /api/v1/actions/sys_approval_request/approval_reject`) and MCP `run_action` answer `400` with the param contract's refusal naming `comment`, and the console's param dialog holds Confirm until a reason is typed. A reject with a reason runs as before. The action's AI tool description now says the reason is required. The `POST /api/v1/approvals/requests/:id/reject` route itself is unchanged. + +Fix for a caller: send the reason as `params.comment` when you run `approval_reject`. diff --git a/content/docs/automation/approvals.mdx b/content/docs/automation/approvals.mdx index a1aea485675..a3567115849 100644 --- a/content/docs/automation/approvals.mdx +++ b/content/docs/automation/approvals.mdx @@ -934,7 +934,10 @@ renders and executes them wherever the object is surfaced — the **Approvals inbox** included — so a decision (comment, a file-upload for `attachments`, the reassign user-picker) is collected by the generic action dialog with **no per-action UI code**. New decision capabilities ship as backend metadata, not -hand-written buttons. +hand-written buttons. A reject through `approval_reject` carries its reason: the +action declares its `comment` param required, so the action door, MCP `run_action` and the +dialog refuse a reject whose `comment` is missing or blank (`400`, the declared +param contract's `required` refusal). On the record itself, a record with approval requests grows an **Approvals** tab beside Details and Related, with a request-count badge: the current step, decision diff --git a/docs/qa/platform-checklist/areas/approvals.json b/docs/qa/platform-checklist/areas/approvals.json index d8c813c04b2..2495133c2bf 100644 --- a/docs/qa/platform-checklist/areas/approvals.json +++ b/docs/qa/platform-checklist/areas/approvals.json @@ -1018,7 +1018,7 @@ "PATCH a showcase_project budget to e.g. 200000 over /api/v1/data — showcase_budget_approval opens and parks at manager_review (routed to manager = admin), lockRecord:false; the admin is the request's submitter as well as its approver", "open that project's record page — /_console/apps/showcase_app/showcase_project/record/:id, the served record route (the shorthand /apps/showcase_app/showcase_project/:id still lands there: the console's ShorthandRecordRedirect rewrites it); once it settles, screenshot the record's ONE header row at the top of the page (objectstack#35593): the object icon and name, '/', the record's name, its stage tag and an approval tag reading 'Awaiting your approval' (zh-CN 待你审批) on the left; the record counter and prev/next, a divider and the ONE button group on the right. There is no decision band above the page body. The group is DeclaredActionsBar's header face over the pending request row (sys_approval_request's declared record_section actions): for the admin (pending approver and submitter) the approver's leads, Approve (approval_approve, declared variant 'primary' → the group's only filled button) and Reject (approval_reject, outlined in the destructive colour), then one ⋮ (declared-actions-header-menu). Open the ⋮ and screenshot it: Reassign, Send back, Request info, Send reminder, Recall and Reply as menu items, a separator (declared-actions-header-menu-separator), then the record's own actions; each lever's data-testid is declared-action- plus the action name. Resubmit stays hidden (the request is not 'returned'). Clicking the approval tag scrolls to the approvals panel (record-approvals-panel), where the approval detail is drawn until the record's 审批 tab (#35595) takes it. Read the viewer block the bar ran on: on first load the record's approvals arrive inside the record page's one read, POST /api/v1/data/showcase_project/:id/page (useRecordApprovals.ts seedRecordApprovals, objectstack#27627), and GET /api/v1/approvals/requests?object=showcase_project&recordId=:id fires only as the post-decision refresh, so read the pending row from that /page body or send the GET yourself: it answers the pending row with viewer.can_act true and viewer.is_submitter true", "because manager_review is lockRecord:false (the request row reads lock_record: false), confirm the edit affordances remain live on this record (the editable contrast): the header's Edit button is enabled, the inline-edit pencils are present, and the approval band reads 'In approval · editable' (sky, a clock icon; ui/packages/plugin-detail/src/DetailView.tsx, detail.approvalPendingEditable)", - "click Reject (declared-action-approval_reject): assert exactly ONE dialog opens — the param dialog titled by the declared label, Reject, with the declared confirm question as its description ('Reject this request? A rejection is final for every approver.'), carrying the Comment textarea, REQUIRED on the record page (Confirm stays refused while it is empty: the record page asks a rejection for its reason, objectstack#35593, while the declaration keeps it optional for the approvals list), and the Attachments file input inline; there is NO confirm dialog in front of it and no second comment dialog after it (approval_reject declares its question as description and no confirmText, so the runtime chains nothing, #7278; objectui#3126)", + "click Reject (declared-action-approval_reject): assert exactly ONE dialog opens — the param dialog titled by the declared label, Reject, with the declared confirm question as its description ('Reject this request? A rejection is final for every approver.'), carrying the Comment textarea, REQUIRED on the record page (Confirm stays refused while it is empty: the record page asks a rejection for its reason, objectstack#35593, and approval_reject's declaration requires it too, so a missing or blank reason is also refused at the REST action door and over MCP), and the Attachments file input inline; there is NO confirm dialog in front of it and no second comment dialog after it (approval_reject declares its question as description and no confirmText, so the runtime chains nothing, #7278; objectui#3126)", "fill the comment and Confirm; capture the decision POST, the action door POST /api/v1/actions/sys_approval_request/approval_reject, with recordId = the REQUEST's id (never the project's) and the comment under params.comment; the door forwards it in-process to the declared target /api/v1/approvals/requests/:id/reject under the caller's identity (ui/packages/app-shell/src/utils/apiActionDoor.ts; targets in packages/plugins/plugin-approvals/src/sys-approval-request.object.ts); re-read GET /api/v1/approvals/requests/:id (status=rejected) and GET /api/v1/approvals/requests/:id/actions (the reject row carries the comment). The bar re-reads the approvals after the decision (refreshAfter) and draws nothing once no request is pending", "approve path: PATCH another project's budget to e.g. 200000; on its record page click Approve → one dialog (titled Approve, described 'Approve this request? Your approval is recorded, and the request moves on once this step has the approvals it requires.', optional Comment and Attachments) → Confirm → POST /api/v1/actions/sys_approval_request/approval_approve (forwarded to /api/v1/approvals/requests/:id/approve, as step 6) → re-read (status=approved; at or under 500000, needs_exec ends the run at approved)", "lock contrast: PATCH a third project's budget above 500000 (e.g. 600000) and approve its manager_review from the record page as in step 7 — needs_exec routes the run to exec_review (position 'showcase_exec', the admin again; lockRecord:true), a NEW pending request whose row reads lock_record: true; once the page re-reads (the decision's refresh, or a reload), screenshot — the header draws NO Edit button at all while the approval locks the record, and the approval tag in the header says why (objectstack#35593 supersedes framework#3794's disabled Edit for an approval lock; a lock from the record's state keeps Edit rendered and disabled, with its lock tag beside the record's name), the inline-edit pencils are gone (approvalLocked, from resolveApprovalLock over recordLockedByApproval of the pending request → InlineEditProvider canEdit false), and the approval band reads 'Locked for approval' (amber, a lock icon; detail.lockedByApproval)", @@ -1068,7 +1068,7 @@ "objectui packages/app-shell/src/views/DeclaredActionsBar.tsx (blob f27b4ab0efff): each declared action's visible predicate evaluated fail-closed against the row's viewer block, the declared variant mapped (primary → filled, danger → destructive), the override framing (isOverrideDecision in utils/approvalOverride.ts: 'Override' plus the label, amber outline, data-override-decision) and the data-testid declared-action- plus the action name; + hooks/useRecordApprovals.ts (blob b65a91f4a9a4): the record's read, GET /api/v1/approvals/requests?object=…&recordId=… then GET …/requests/:id, with no client-side canDecide; + hooks/useConsoleActionRuntime.tsx: the param dialog takes its title from the action's label and its description from the declared description", "objectui packages/app-shell/src/views/RecordDetailView.approvalDeclaredActions.test.tsx (objectui#3055, blob 9519ae70f743 at the console pin cf153eb2e535: the record page runs sys_approval_request's declared decision actions, and 'folds a declared decision output into the nested `outputs` body' asserts that a decision's comment and declared outputs reach the POST to the request) + packages/plugins/plugin-approvals/src/sys-approval-request.object.test.ts ('the confirm question rides `description`, not `confirmText`, so one decision opens one dialog (#7278)'). Between them they hold what the deleted RecordDetailView.approvalDecisionActions.test.tsx pinned, the decision params and one dialog per decision; objectui 99782f961 deleted that file before both console pins", "examples/app-showcase/src/automation/flows/index.ts#BudgetApprovalFlow (BudgetApprovalFlow — manager_review lockRecord:false / exec_review lockRecord:true on showcase_project)", - "packages/plugins/plugin-approvals/src/sys-approval-request.object.ts#approval_reject (the declared actions: approval_approve 'primary' and approval_reject 'danger', each with optional comment and attachments params and its confirm question as description, never confirmText, #7278; approval_reassign, approval_send_back, approval_request_info, approval_remind, approval_recall, approval_resubmit and approval_comment; all at record_section, every visible predicate over record.viewer, the attachedOnRead block) + packages/plugins/plugin-approvals/src/approval-service.ts#attachViewers (can_act: pending and the caller takes a slot; is_submitter; can_override: pending and isOverrideActor; can_override_approve: can_override and the viewer did not submit the request)", + "packages/plugins/plugin-approvals/src/sys-approval-request.object.ts#approval_reject (the declared actions: approval_approve 'primary' with optional comment and attachments params, and approval_reject 'danger' with a required comment (refused when missing or blank at the REST action door, MCP and the console) and optional attachments, each with its confirm question as description, never confirmText, #7278; approval_reassign, approval_send_back, approval_request_info, approval_remind, approval_recall, approval_resubmit and approval_comment; all at record_section, every visible predicate over record.viewer, the attachedOnRead block) + packages/plugins/plugin-approvals/src/approval-service.ts#attachViewers (can_act: pending and the caller takes a slot; is_submitter; can_override: pending and isOverrideActor; can_override_approve: can_override and the viewer did not submit the request)", "packages/plugins/plugin-approvals/src/request-read-gate.ts (header: the approvals door serves a request to its participants and the override actor, and to a record reader only where the object declares enable.approvalsVisibleToReaders, which no showcase object does) + packages/plugins/plugin-approvals/src/approval-service.ts#refuseUnreadableRequest (every door that acts on a request, decide included, answers a caller who may not READ it REQUEST_NOT_FOUND, exactly as a missing id, answered 404 by packages/rest/src/approvals-routes.ts) + packages/plugins/plugin-approvals/src/approval-service.ts#decide (a reader who takes no slot and holds no override: FORBIDDEN, answered 403)" ], "history": [ diff --git a/packages/plugins/plugin-approvals/src/sys-approval-request.object.test.ts b/packages/plugins/plugin-approvals/src/sys-approval-request.object.test.ts index 4330fc2e3a3..65f9515e09e 100644 --- a/packages/plugins/plugin-approvals/src/sys-approval-request.object.test.ts +++ b/packages/plugins/plugin-approvals/src/sys-approval-request.object.test.ts @@ -24,6 +24,7 @@ import { describe, it, expect } from 'vitest'; import { templateSlotSource } from '@objectstack/spec/automation'; import { isAiReadExposed } from '@objectstack/spec/data'; +import { validateActionParams, type ResolvedActionParam } from '@objectstack/spec/ui'; import { SysApprovalRequest } from './sys-approval-request.object.js'; import { SysApprovalAction } from './sys-approval-action.object.js'; @@ -321,3 +322,35 @@ describe('[#32442, ruling A] the AI read declaration', () => { expect(isAiReadExposed(SysApprovalAction)).toBe(false); }); }); + +/** + * A reject through `approval_reject` always carries its reason. It declares its + * `comment` required, and the declared param contract (ADR-0104 D2) is what + * every action door judges a call by before the target route runs: the REST + * action door, MCP `run_action` and the console's param dialog. This runs the + * spec's own validator over the declaration, resolved the way the runtime + * resolves an inline param, so a missing, empty or whitespace-only reason is + * the `required` refusal and a reason passes. + */ +describe('approval_reject requires a reason', () => { + const resolved = (name: string): ResolvedActionParam[] => + byName(name).params.map((p: any) => ({ + name: p.name, type: p.type, multiple: p.multiple, required: Boolean(p.required ?? false), options: p.options, + })); + + it('refuses a reject with no comment, an empty one or a whitespace-only one', () => { + for (const bag of [{}, { comment: '' }, { comment: ' \n\t ' }, { comment: null }]) { + const issues = validateActionParams(resolved('approval_reject'), bag); + expect(issues.map((i) => [i.param, i.code]), JSON.stringify(bag)).toEqual([['comment', 'required']]); + } + }); + + it('CONTROL: a reject with a reason passes, with or without attachments', () => { + expect(validateActionParams(resolved('approval_reject'), { comment: 'Over budget.' })).toEqual([]); + expect(validateActionParams(resolved('approval_reject'), { comment: 'Over budget.', attachments: ['file_a'] })).toEqual([]); + }); + + it('CONTROL: approve keeps its comment optional', () => { + expect(validateActionParams(resolved('approval_approve'), {})).toEqual([]); + }); +}); diff --git a/packages/plugins/plugin-approvals/src/sys-approval-request.object.ts b/packages/plugins/plugin-approvals/src/sys-approval-request.object.ts index 0aaebee890f..8019a9aee5b 100644 --- a/packages/plugins/plugin-approvals/src/sys-approval-request.object.ts +++ b/packages/plugins/plugin-approvals/src/sys-approval-request.object.ts @@ -698,8 +698,15 @@ export const SysApprovalRequest = ObjectSchema.create({ type: 'api', method: 'POST', target: '/api/v1/approvals/requests/{id}/reject', + // A reject through `approval_reject` always carries its reason + // (maintainer requirement on the record page's Reject). `required: true` + // is enforced by the declared param contract (ADR-0104 D2, + // `validateActionParams`), which counts a blank or whitespace-only + // string as missing: the action door, MCP `run_action` and the console's + // param dialog all refuse a reject without one, before the target route + // is called. The target route itself does not judge the reason. params: [ - { name: 'comment', label: 'Comment', type: 'textarea', required: false }, + { name: 'comment', label: 'Comment', type: 'textarea', required: true }, { name: 'attachments', label: 'Attachments', type: 'file', multiple: true, required: false }, ], visible: @@ -711,7 +718,7 @@ export const SysApprovalRequest = ObjectSchema.create({ ai: { exposed: true, description: - 'Reject a pending approval request (the record: a sys_approval_request id) on behalf of the user, with an optional comment. A rejection is final for every approver. Only a current approver of the request, or an administrator who may override a stuck request, may decide it; anyone else is refused. Ask the human to confirm before calling.', + 'Reject a pending approval request (the record: a sys_approval_request id) on behalf of the user. The param comment, the reason for the rejection, is required: a call without one, or with a blank one, is refused. A rejection is final for every approver. Only a current approver of the request, or an administrator who may override a stuck request, may decide it; anyone else is refused. Ask the human to confirm before calling.', requiresConfirmation: true, }, }, diff --git a/packages/qa/dogfood/test/approval-decide-over-mcp.dogfood.test.ts b/packages/qa/dogfood/test/approval-decide-over-mcp.dogfood.test.ts index cf6d570c8f8..bd3c7340fe4 100644 --- a/packages/qa/dogfood/test/approval-decide-over-mcp.dogfood.test.ts +++ b/packages/qa/dogfood/test/approval-decide-over-mcp.dogfood.test.ts @@ -317,4 +317,34 @@ describe('an approver\'s agent decides a pending approval request over MCP (#324 } await expectUndecided(requestId, 'recall, send-back and resubmit over MCP'); }); + + /** + * A reject through `approval_reject` always carries its reason: it declares its + * `comment` required, so the declared param contract (ADR-0104 D2) refuses a + * missing or blank one at the REST action door and at MCP `run_action`, + * before the confirmation gate and before the approvals route. CONTROL: the + * approver's REST reject with a reason rejects the request as before. + */ + it('⭐ a reject with no reason, or a blank one, is refused at the REST action door and over MCP; with a reason it rejects', async () => { + const requestId = await openRequest('reject-reason'); + const restReject = (params: Record) => + stack.apiAs(approver, 'POST', `/actions/${REQUEST}/approval_reject`, { recordId: requestId, params }); + for (const params of [{}, { comment: '' }, { comment: ' \n\t ' }]) { + const res = await restReject(params); + const body: any = await res.json(); + const label = `REST ${JSON.stringify(params)} → ${JSON.stringify(body)}`; + expect([res.status, body?.error?.code ?? body?.code], label).toEqual([400, 'VALIDATION_ERROR']); + expect(String(body?.error?.message), label).toMatch(/\bcomment\b/); + const viaMcp = await act({ bearer: approver }, 'approval_reject', requestId, false, String(params.comment ?? '')); + expect(viaMcp.isError, viaMcp.text).toBe(true); + expect([viaMcp.payload?.error?.status, viaMcp.payload?.error?.code], viaMcp.text).toEqual([400, 'VALIDATION_ERROR']); + expect(String(viaMcp.payload?.error?.message), viaMcp.text).toMatch(/\bcomment\b/); + } + await expectUndecided(requestId, 'a reject without a reason'); + + const res = await restReject({ comment: 'Over budget.' }); + expect(res.status, await res.clone().text()).toBe(200); + expect((await requestRow(requestId)).status).toBe('rejected'); + expect((await decisionsOn(requestId)).map((r) => [r.action, r.comment])).toEqual([['reject', 'Over budget.']]); + }); });