Skip to content

Commit 99bd6d1

Browse files
fix(plugin-approvals): a locked record's lines read as locked, and deleting the locked record still lands (#36135)
Clause-②: no Metadata-protocol: none ## 维护者速读 - 审批中的单据:它的明细行在「是否锁定」查询里也答「已锁定」,和服务端拒绝修改明细的结果一致;审批中删除整张单据又能成功,明细一起删掉、审批请求自动取消。 - 属于审批锁定的加固,替使用审批的业务用户解决「单据锁了但明细状态不一致」和「带明细的单据审批中删不掉」两个问题。 - 出错的话:要么明细被误报为锁定,要么带明细的单据删不掉;可直接撤回本次合并,只回到合并前的状态。 - 无 ## What changes The rule: **a locked record's lines are locked with it, and deleting the locked record itself still lands.** No node switch is added and no default moves: lines lock whenever the record's approval node locks the record, as `main` already enforces. - **The lock read answers for a line.** `ApprovalService.getRecordLock` (the record-lock read behind `GET /api/v1/approvals/records/:object/:recordId/lock` and `client.approvals.getRecordLock`) now answers `{ locked: true }` for a `master_detail` child row while its master is held by a locking pending approval. It used the record-lock judgement only, so it answered `{ locked: false }` for exactly the rows the detail-row lock refuses with `409 RECORD_LOCKED`. The new `detailRowHeldByLockingApproval` (in `lifecycle-hooks.ts`) asks the hook's own question: the row's master edges, read off its FK columns as SYSTEM, each judged by the one shared lookup (`lockingRequestsForRecords`). The caller-scoped readability anchor is unchanged, so a row the caller cannot read still answers `null`, like a missing id. - **Deleting the locked record lands again, with its lines.** The delete-cancel linkage cancels a record's pending request when the record is deleted (its ruling vetoed "forbid delete while pending"). The detail-row lock refused the `master_detail` cascade legs of that delete, so a locked record with lines could not be deleted (`409 RECORD_LOCKED`). The detail-row lock's `beforeDelete` now lets a cascade leg through on one edge only. That leg carries the engine's declared marker `ctx.referentialCascadeDelete`, and the detail object's single cascade-capable relation must be the `master_detail` edge to the held master, which makes the record being deleted that master. The leg must also run inside an open unit of work: a `ctx.transaction` handle the engine does not report closed. A delete with no open transaction is refused whole with `409 RECORD_LOCKED`, and the record, its lines and its pending request stay. That covers a datasource that declares `supports.transactionsUnsupported`, a cascade across datasources, a record and its lines together on a non-default datasource, and a handle whose unit has already finished. Every other shape is refused too: a junction under two cascading masters, or a line that also has a `lookup` declaring `deleteBehavior: 'cascade'`. So deleting a different record never removes a held record's line, with one exception. When the held record is itself the child of a record whose delete cascades to it (a `master_detail` child, or a `lookup` child with `deleteBehavior: 'cascade'`), deleting that parent removes the held record with its lines and cancels its request. - **Docs.** `content/docs/automation/approvals.mdx` now states that lines are locked with the record (insert / update / delete / reparent, the carried exemptions, `lookup` children untouched). It also states what deleting the record while pending does and when that delete is refused, that server code owning the transaction must not catch the refusal and commit over it, and that the lock read answers for a line. ## Why A page reads the lock to decide whether to offer an edit, so the read must answer the same way the server refuses. And a pending record must stay deletable, because the delete-cancel linkage is the only route left once the record is locked. ## Tests Each new pin was run against the base first (red), then with the fix (green), and each guard was ablated (red) and restored (`git diff HEAD` empty, blob equal to HEAD). - `record-lock-read.integration.test.ts`: new top-level `describe` ("a locked record's lines are locked with it — the record-lock read answers for a detail row too"), 4 cases on the real `ApprovalsServicePlugin.start()` rig, with two line objects added to it. Base: 2 failed / 6 passed (`expected { locked: false } to deeply equal { locked: true }`). Fix: 8 / 8. Ablating the new `|| detailRowHeldByLockingApproval(...)`: 2 failed, restored. - `detail-row-lock.integration.test.ts`, four new top-level `describe`s: - "… deleting the record itself still lands", 5 cases: the held record deletes with its lines; the caller's own line delete is refused; a different record's cascade is refused; two fail-closed shapes, a junction under two masters and `master_detail` plus a cascading lookup, are refused with nothing removed. Base: the delete case failed (refused `RECORD_LOCKED`). Ablating the cascade yield: 1 failed. Ablating the sole-edge narrowing: 1 failed. - "… without a transaction the held record's delete is refused", 3 cases on a sqlite driver that declares `supports.transactionsUnsupported`: a held record with a later `restrict` row, a held record with lines only, and a control in which an unheld record deletes. Base: 2 failed (`DELETE_RESTRICTED`; the delete landed). - "… a closed unit's handle is no transaction", 1 case: a cascade split across two datasources, with a delete carrying the handle of a finished transaction. Base: `DELETE_RESTRICTED`. - "… a held record that is itself a detail row goes with its master", 1 case. - Ablating the transaction check turned the two no-transaction pins red (2 failed). Ablating the open-unit test (a closed unit's handle counted as open) turned the closed-handle pin red (1 failed, `DELETE_RESTRICTED`). Every ablation was restored. - The existing insert / update / delete pins of that file were ablated one hook registration at a time: `beforeInsert` 3 failed, `beforeUpdate` 4 failed, `beforeDelete` 2 failed, each restored. - Whole package at `d2056107`: `pnpm --filter @objectstack/plugin-approvals test`: 126 files / 1822 tests passed. `typecheck` (incl. `check:test-typecheck`) OK. ## Acceptance notes - **Withheld yield.** A locked record whose line object has a second relation that also cascades on delete stays undeletable while pending. So does one whose delete runs outside an open transaction. The engine does not tell a hook which record started a cascade, and outside a transaction a later refusal could not put the lines back. In both cases this refuses rather than risk a held line. The docs page says so. - **Owner of the transaction.** Inside a transaction, the refusal is what keeps the lines. Server code that owns the transaction must not catch the refused delete and commit anyway; the hook cannot prevent that, and the docs state the rule. - **Unchanged.** No node-level switch for the line lock and no change to its default: lines lock whenever the node locks the record. Decided documents are not locked; edits after the decision are not in this PR. - **Pending approvers and the lines (read, measured).** The pending-approver read grant names the master record only. On the real service it answers `["held_sheet_a"]` for the master object and `[]` for its line object. An approver reads lines through the line object's own access: a `controlled_by_parent` detail follows the master's readability (source reading, not run on a kernel), and a `private` detail owned by the submitter is not readable to them. - No console code changes, so no browser check is owed. ## Verification Two-minute check: `pnpm --filter @objectstack/plugin-approvals exec vitest run --maxWorkers=2 src/record-lock-read.integration.test.ts src/detail-row-lock.integration.test.ts` should report 2 files and 29 tests passed. Files to read first, by risk: `packages/plugins/plugin-approvals/src/lifecycle-hooks.ts` (the `cascadeSource` hunk in `bindDetailRowLockHook` and `soleCascadeEdgeOf`), then `inOpenUnit` and `detailRowHeldByLockingApproval` beside it, then `approval-service.ts` `getRecordLock`. <details><summary>Local gates at d205610 — 103 derived, 102 exit 0, 1 NOT MEASURED</summary> Derived with `node scripts/pm/dispatch-gates.mjs --commands` at `d2056107` and reconciled with `--ran` (exit codes recorded): "103 derived, 102 run, 1 NOT-MEASURED, 0 UNRUN". The one NOT MEASURED is `check-plugin-teardown-shape --self-test`, refused on a local prerequisite (exit 3, shallow clone) and left to CI. Every other derived family exited 0, among them `check:query-options-erasure`, `check:nul-bytes`, `check:doc-authoring`, `check:engine-double-contract`, `check:test-source-alias`, `check:system-context-census`, `check:i18n` and `check-empty-changeset`. CI's own check-runs are the verdict for everything else. </details> ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (not touched) - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `pnpm lint` passes. (CI owns the repo-wide run) - [x] 4. One task, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: not touched. - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. --- _Generated by [Claude Code](https://claude.ai/code/session_017xRBcRtGQS7L4qLKpL8stv)_ <!-- signed: security#2 · sunhaolin · session_017xRBcRtGQS7L4qLKpL8stv --> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
1 parent c448c61 commit 99bd6d1

6 files changed

Lines changed: 668 additions & 10 deletions

File tree

‎.changeset/sec2-vv32.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/plugin-approvals': patch
3+
---
4+
5+
fix: a locked record's lines read as locked, and deleting the locked record still removes its lines
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
**Reading the lock answers for a line too.** `IApprovalService.getRecordLock` (and `GET /api/v1/approvals/records/:object/:recordId/lock`) now answers `{ locked: true }` for a `master_detail` child row while the record it belongs to is held by a locking pending approval: the same rows whose insert, update and delete the server refuses with `409 RECORD_LOCKED`. It used to answer `{ locked: false }` for them, so a page could offer an edit the server then refused. A row the caller cannot read still answers as a missing one.
11+
12+
**Deleting a record while its approval is pending removes its lines again.** The delete lands and cancels the pending request, as before the lines were locked; its `master_detail` lines are removed with it. The whole delete is refused `409 RECORD_LOCKED`, and the record, its lines and its request stay, when the line object has a second relation that also cascades on delete, or when the delete does not run inside an open transaction: a datasource that declares `supports.transactionsUnsupported`, a cascade across datasources, a record and its lines together on a datasource other than the default one, or a delete carrying the handle of a transaction that has already finished. Server code that owns the transaction must not catch the refused delete and commit anyway. A line is never removed by deleting a different record, except that when the locked record is itself the child of a record whose delete cascades to it (a `master_detail` child, or a `lookup` child with `deleteBehavior: 'cascade'`), deleting that parent removes the locked record and its lines and cancels its request.

‎content/docs/automation/approvals.mdx‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,41 @@ never deadlock against itself. The exemption is keyed on run identity rather tha
408408
elevation, so a `runAs:'user'` run stays row-level-security scoped while it
409409
writes — it does not become a system write.
410410

411+
**The record's lines are locked with it.** Rows of a `master_detail` child of the
412+
locked record (an expense claim's lines, an opportunity's products) refuse insert,
413+
update and delete with the same `409 RECORD_LOCKED`, and so does moving a row into
414+
or out of the record. The record lock's exemptions carry over: system writes and
415+
the run that opened the request still write the lines. Children joined by a plain
416+
`lookup` are not locked.
417+
418+
**Deleting the record while it is pending** is not refused: the delete lands and
419+
its pending request is cancelled (`status: 'cancelled'`, with `cancel_reason` on
420+
the row). The delete removes the record's `master_detail` lines with it. In the
421+
cases below the server refuses the whole delete with `409 RECORD_LOCKED` instead, and
422+
the record, its lines and its pending request all stay:
423+
424+
- the line object has a second relation that also cascades on delete (a `lookup`
425+
with `deleteBehavior: 'cascade'`, or a second `master_detail`), so the server
426+
cannot tell which record's delete removes the line;
427+
- the delete does not run inside an open transaction. This covers a datasource
428+
that declares `supports.transactionsUnsupported`, a cascade that spans more
429+
than one datasource, a record and its lines that sit together on a datasource
430+
other than the default one (the server opens its cascade transaction on the
431+
default datasource only), and a delete that carries the handle of a
432+
transaction that has already finished (for example from an `async` after-hook).
433+
The lines would be removed one by one, and a later refusal (a `restrict`
434+
reference to the record) would leave the record locked with its lines gone.
435+
436+
Inside a transaction the refusal is what keeps the lines: server code that owns
437+
the transaction must not catch the refused delete and commit anyway, or the
438+
lines are gone while the record stays locked.
439+
440+
Deleting a different record never removes a locked record's line, with one
441+
exception: when the locked record is itself the child of a record whose delete
442+
cascades to it (a `master_detail` child, or a `lookup` child with
443+
`deleteBehavior: 'cascade'`), deleting that parent removes the locked record and
444+
its lines, and cancels its request.
445+
411446
**Reading the lock.** Anyone who can read the record can ask whether it is
412447
locked, including a member who is not on the approval and so is served no
413448
request row:
@@ -423,7 +458,8 @@ which stay behind the object's `approvalsVisibleToReaders` switch. `locked` is
423458
`true` while a pending request's node has `lockRecord` other than `false`. That
424459
is the same check the server applies when it refuses an edit with `409
425460
RECORD_LOCKED`, so a page that reads it never offers an edit the server will
426-
refuse. The record is read as the caller: a record they cannot read answers
461+
refuse. Asked of a line, it answers `true` while the record the line belongs to
462+
is locked. The record is read as the caller: a record they cannot read answers
427463
`404 RECORD_NOT_FOUND`, the same body a record id that does not exist gets. The
428464
SDK call is `client.approvals.getRecordLock(object, recordId)`, and the service
429465
member is `IApprovalService.getRecordLock`. An approvals service without that

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ import {
163163
type FieldVisibilitySource,
164164
} from './payload-redaction.js';
165165
import { APPROVAL_REQUEST_CHILD_OBJECTS, type RequestVisibilitySource } from './request-read-gate.js';
166-
import { nodeConfigLocksRecord, recordHeldByLockingApproval } from './lifecycle-hooks.js';
166+
import { detailRowHeldByLockingApproval, nodeConfigLocksRecord, recordHeldByLockingApproval } from './lifecycle-hooks.js';
167167
import { PendingApproverReadGrantSource, type PendingApproverReadGrants } from './pending-approver-read-grant.js';
168168
// The ADR-0043 pages — the ONE copy of their text, rendered by the plugin's
169169
// self-hosted mount and by `handleActionPage` alike.
@@ -11124,7 +11124,11 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1112411124
* `{ locked }` only: no request id, node, participants or `pending`. Request
1112511125
* content stays behind `approvalsVisibleToReaders` (#8652 ruling 5299823744,
1112611126
* untouched). The judgement is the record-lock hook's own
11127-
* ({@link recordHeldByLockingApproval}), never a second copy of it.
11127+
* ({@link recordHeldByLockingApproval}), never a second copy of it — and,
11128+
* for a master-detail line, the detail-row lock's
11129+
* ({@link detailRowHeldByLockingApproval}): a locked record's lines are
11130+
* locked with it, so the read answers `true` for a line whose write the hook
11131+
* refuses.
1112811132
*/
1112911133
async getRecordLock(
1113011134
object: string,
@@ -11139,7 +11143,9 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1113911143
where: { id }, fields: ['id'], limit: 1, context,
1114011144
});
1114111145
if (!Array.isArray(readable) || readable.length === 0) return null;
11142-
return { locked: await recordHeldByLockingApproval(this.engine, objectName, id) };
11146+
const locked = await recordHeldByLockingApproval(this.engine, objectName, id)
11147+
|| await detailRowHeldByLockingApproval(this.engine, objectName, id);
11148+
return { locked };
1114311149
}
1114411150

1114511151
private async loadRequest(

0 commit comments

Comments
 (0)