6969 * other module this one imports, so the evaluation order of every module is
7070 * what it was. The published build bundles both files into one, where the
7171 * cycle does not exist.
72+ *
73+ * [#30053 codemod] A PRIVATE method's docblock has since moved here,
74+ * onto its function (`--hand-docs` of `scripts/codemod-split-engine.mjs`),
75+ * and the class keeps none for it; a public or protected method's docblock
76+ * stays on the class, the contract, and its function keeps the placeholder.
7277 */
7378
7479import type { ServiceObject } from '@objectstack/spec/data' ;
@@ -120,7 +125,65 @@ function cascadeReachOf(set: CascadeDeleteSet, object: string, id: unknown): Map
120125 return reach ;
121126}
122127
123- /** The body of `ObjectQL#referenceProbeFilter`; the method's docblock there is its contract. */
128+ /**
129+ * [#9362] The dependents probe's `where`, spelled for the KIND of reference
130+ * field it is aimed at.
131+ *
132+ * A single-valued `lookup` / `master_detail` stores a scalar foreign key, and
133+ * bare equality is the right question about it. A MULTI-VALUED reference
134+ * field stores an ARRAY, and every SQL backend in this repo puts that array
135+ * in a JSON TEXT column. [#18408] Which of the two a field is, is
136+ * {@link declaredMultiValued} — the one predicate the storage side asks
137+ * (#17469) — ⛔ never a raw `field.multiple`: `master_detail` and `tree` are
138+ * outside `MULTI_CAPABLE_TYPES`, so the flag on one of those buys a JSON
139+ * column from nobody, and aiming `$contains` at the scalar column the driver
140+ * really built is the mirror image of the defect below.
141+ *
142+ * Aiming bare equality at a multi-valued column compares
143+ * the whole serialization (`["a","b"]`) against one id, which can never hold;
144+ * `driver-sql` refuses the spelling outright (`INVALID_FILTER` / 400, #7398),
145+ * and that refusal is correct and stays. Until this method existed the probe
146+ * built bare equality for both kinds, so every object pointed at by any
147+ * registered `multiple: true` lookup had its delete refused with a 400 — on
148+ * the stock showcase, `showcase_account`, with an EMPTY dependent table:
149+ * the fault is schema-driven, not data-driven, because the probe runs per
150+ * DECLARED relation.
151+ *
152+ * The multi-value spelling is `$contains`, which is what the refusal itself
153+ * prescribes and the one membership spelling every driver here answers. On a
154+ * column the driver holds declared multi-valued it asks MEMBERSHIP, as
155+ * `FILTER_OPERATORS`' `$contains` docblock (`@objectstack/spec`) declares for
156+ * a JSON-stored column: `driver-sql` (and `driver-sqlite-wasm` and the local
157+ * `driver-turso`, which extend it) compiles a per-dialect membership
158+ * construct, `driver-memory` a mingo `$elemMatch`, and `driver-mongodb` the
159+ * same `$elemMatch`. No public filter surface is widened: `$contains` is
160+ * already declared, and this is the only construction site that changes.
161+ *
162+ * Membership answers EXACTLY on a well-formed slot, but a backend that does
163+ * not hold the declaration (a table it was never told about) answers the
164+ * SUBSTRING reading instead, which is a SUPERSET: with ids `acc_1` and
165+ * `acc_10`, a row holding `acc_10` also matches a probe for `acc_1`. That is
166+ * why the caller still narrows the rows exactly through
167+ * {@link ObjectQL.storedReferenceIncludes} — over-matching here would make
168+ * `cascade` DELETE and `set_null` clear rows that never referenced this
169+ * record, which is worse than the 400 being fixed. The pushdown's only job is
170+ * to keep the probe from reading the whole table.
171+ *
172+ * ⚠️ Membership is array-only on every backend that answers it, so a slot
173+ * holding an off-shape bare scalar (the write door never stores one, it wraps
174+ * a scalar; out-of-band data can) is not matched by the pushdown, and the
175+ * scalar arm of {@link ObjectQL.storedReferenceIncludes} never sees that row.
176+ *
177+ * The `$or` limb covers the other direction — a FALSE NEGATIVE, which on an
178+ * integrity guard is the fail-OPEN that #8895 ruled out. An id needing JSON
179+ * escaping (a quote, a backslash) appears in the serialized text in its
180+ * ESCAPED form, so a backend answering the substring reading over that text
181+ * would miss the row for the raw id; a membership test, and a document or
182+ * in-memory backend, compares the element itself and needs the RAW form. Both
183+ * are asked whenever they differ, and the exact narrowing discards whatever
184+ * the extra limb over-matched. Identical for an ordinary id, which is every
185+ * id this engine generates.
186+ */
124187export function referenceProbeFilter (
125188 fieldName : string ,
126189 fdef : { type ?: unknown ; multiple ?: unknown } ,
@@ -141,7 +204,48 @@ export function referenceProbeFilter(
141204 } ;
142205}
143206
144- /** The body of `ObjectQL#recordReferenceCheckElevation`; the method's docblock there is its contract. */
207+ /**
208+ * [#12166, ruling constraint 3] File the reference-check elevation, BOTH
209+ * halves: `triggeredBy` = the operator who asked for the delete,
210+ * `executedAs: 'system'` = the identity the check actually ran under.
211+ *
212+ * ## Why a record exists at all
213+ *
214+ * The elevation is the whole of what this card changed, and an elevation
215+ * nobody can see afterwards is the thing a security reviewer has no way to
216+ * audit. The ruling names the Salesforce/Dataverse ledger shape for exactly
217+ * this reason: a platform-executed integrity action is attributable to the
218+ * human who triggered it, not laundered into "system did something".
219+ *
220+ * ## Why the LOGGER and not a `sys_audit_log` row
221+ *
222+ * Stated plainly, because a narrow audit edge has to be visible to be honest
223+ * (`read-audit.ts` takes the same posture about its own narrowing):
224+ *
225+ * The elevated operation is a READ, and `plugin-audit`'s read writer declares
226+ * — and pins — that a system-elevated read produces NO row, deliberately:
227+ * those are the platform reading for its own bookkeeping, and recording them
228+ * would bury the human views that ledger exists to make findable. Minting a
229+ * row here would either contradict that declared boundary or require the
230+ * engine to reach into a plugin it does not depend on. The engine's logger is
231+ * the channel it owns, and it is where the reporting deployment read the
232+ * defect in the first place — the issue's evidence is a server log line.
233+ *
234+ * So: this is a LOG record, at `info`, and the honest limit of it is that a
235+ * deployment which does not retain engine logs retains no trace. A durable
236+ * `sys_audit_log` row for the elevation is a follow-up that belongs to
237+ * plugin-audit, which owns that row shape.
238+ *
239+ * `debug` was not an option: a record that is off by default in production is
240+ * not an audit record.
241+ *
242+ * ⛔ The record names the referenced OBJECT and the relation FIELD — declared
243+ * metadata — and never a row id, a field value, or a count. Constraint 2
244+ * governs what the CALLER is told; this is the server-side ledger and could
245+ * defensibly say more, but saying more here is how a count reaches a support
246+ * transcript, and the two surfaces are kept on one rule so neither has to be
247+ * re-audited alone.
248+ */
145249export function recordReferenceCheckElevation (
146250 this : Pick < DeleteCascadeHost , 'logger' > ,
147251 object : string ,
@@ -247,7 +351,35 @@ export function collectCascadeDeleteObjects(
247351 return named ;
248352}
249353
250- /** The body of `ObjectQL#collectCascadeDeleteSet`; the method's docblock there is its contract. */
354+ /**
355+ * [#22305] Phase 1 of {@link cascadeDeleteRelations}: every record a by-id
356+ * delete of `object`/`id` removes, the root included, collected READ-ONLY.
357+ *
358+ * Breadth-first across every relation whose resolved behaviour is
359+ * `cascade`, read through the walk's own two readings — which relations
360+ * point at an object ({@link cascadeRelationBehavior}) and which rows
361+ * reference a record ({@link probeReferencingRows}: system identity, the
362+ * same filter, the same missing-table and multi-value handling) — so the
363+ * two phases cannot disagree about which rows a cascade reaches. A record
364+ * already in the set is not expanded again, so a cycle in the data (a
365+ * self-reference, two objects cascading into each other) terminates.
366+ *
367+ * The cost is one extra probe per CASCADING relation per member, since the
368+ * walk probes those relations again as it deletes. `restrict` and
369+ * `set_null` relations are not read here. Every probe here is an elevated
370+ * read, so it files its #12166 record before it runs, through the
371+ * once-per-record ledger the walk shares ({@link fileReferenceCheckElevation}).
372+ *
373+ * [#24964] …except for {@link inspectCascadeDelete}, which passes
374+ * `fileElevations: false`: that record says "read as system FOR THE DELETE
375+ * of" the record, and an inspection deletes nothing, so filing it there would
376+ * put a delete that never happened in the log. The reads are the same.
377+ *
378+ * [#27004] From a root SET, level by level: the records found at one depth
379+ * are probed together, per object and per relation, through
380+ * {@link probeReferencingRowsOfSet}. The set and its edges are the ones the
381+ * record-by-record walk collected.
382+ */
251383export async function collectCascadeDeleteSet (
252384 this : Pick < DeleteCascadeHost , 'cascadeRelationBehavior' | 'fileReferenceCheckElevation' | 'probeReferencingRowsOfSet' > ,
253385 object : string ,
@@ -312,7 +444,38 @@ export async function collectCascadeDeleteSet(
312444 return set ;
313445}
314446
315- /** The body of `ObjectQL#refuseRestrictsInsideOwnCascade`; the method's docblock there is its contract. */
447+ /**
448+ * [#26718] Before the cascade writes anything: refuse the delete when an
449+ * AUTHORED `restrict` is held by a row the cascade would remove only because
450+ * the record that restrict protects is removed.
451+ *
452+ * The deleting walk judges a `restrict` against rows OUTSIDE the cascade set
453+ * alone (#22305), and a row inside it was exempt whatever put it there. That
454+ * exemption is right for a row the cascade reaches through ANOTHER record (a
455+ * contract and the contact it restricts, both details of the account being
456+ * deleted: the account's delete removes both, and neither is left dangling).
457+ * It was wrong for a row reached THROUGH the very record it protects: a child
458+ * holding a `master_detail` and a `restrict` lookup to the same parent is in
459+ * that parent's set by its own master_detail, so the restrict was never
460+ * asked and the cascade deleted the child — the record the author declared
461+ * undeletable. A grandchild under that parent holding the restrict is the
462+ * same case one level down.
463+ *
464+ * So for every member, every relation whose RESOLVED behaviour is `restrict`
465+ * ({@link cascadeRelationBehavior}, an authored `restrict` on a lookup,
466+ * tree or master_detail) is probed here when its object has a member in the
467+ * reach of that member ({@link cascadeReachOf}); a referencing row in the
468+ * reach refuses with the walk's own `DELETE_RESTRICTED`
469+ * ({@link deleteRestrictedError}). Every restrict is judged before the
470+ * first cascade, so the outcome no longer depends on the order the fields or
471+ * objects are declared in, and nothing has been written when it refuses.
472+ *
473+ * ⛔ The required-lookup escalation (`set_null` on a required field read as
474+ * `restrict`) is not judged here: it restricts because a required reference
475+ * cannot be CLEARED, and a row the cascade deletes is never cleared. It keeps
476+ * the #22305 rule. Neither does this replace the walk's judgement of rows
477+ * outside the set, which is unchanged.
478+ */
316479export async function refuseRestrictsInsideOwnCascade (
317480 this : Pick < DeleteCascadeHost , 'cascadeRelationBehavior' | 'fileReferenceCheckElevation' | 'probeReferencingRowsOfSet' | 'deleteRestrictedError' | 'referenceProbeFilter' > ,
318481 cascadeSet : CascadeDeleteSet ,
@@ -396,7 +559,13 @@ export async function refuseRestrictsInsideOwnCascade(
396559 }
397560}
398561
399- /** The body of `ObjectQL#deleteRestrictedError`; the method's docblock there is its contract. */
562+ /**
563+ * The `DELETE_RESTRICTED` refusal of deleting `object`/`id` because
564+ * `dependents` of `childName` reference it via `fieldName`. One builder for
565+ * the deleting walk ({@link walkReferencingRelations}) and the judgement
566+ * before it ({@link refuseRestrictsInsideOwnCascade}), so the two answer one
567+ * envelope.
568+ */
400569export async function deleteRestrictedError (
401570 this : Pick < DeleteCascadeHost , 'validationMessageContext' | 'dependentCountIsDisclosable' | 'objectDisplayLabel' > ,
402571 args : {
0 commit comments