Skip to content

Commit 74ef4e4

Browse files
committed
fix(objectql): a cascade dependent the collected set never held is deleted by id, keeping its own recycle-bin entry
The batch takes only the members the collect phase gathered. A row a hook of the same cascade wrote after the set was collected goes by id, as the root of its own cascade, as before the batch: a predicate delete opens no recycle-bin entry and the root's entry snapshotted only the members. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jUg7BKfRBcTPyMxbLh
1 parent 321a866 commit 74ef4e4

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎packages/objectql/src/engine-cascade-planning.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,9 @@ export class CascadePlanningFacet {
14371437
* - the batch's caller-scoped WRITE read misses a child it was asked for
14381438
* (`CascadeBatchNarrowed`, thrown before any hook or write): a by-id
14391439
* delete of that child refuses where a predicate one would skip it.
1440+
* And a dependent the collect phase never held (not in `cascadeSet.members`)
1441+
* always goes by id, whatever its chunk does: it is the root of its own
1442+
* cascade, with its own recycle-bin entry, as before.
14401443
* In each case the by-id path gives today's answer, and a refusal rolls the
14411444
* whole cascade back (ruling 6893104201 point 2). So one cascade can show
14421445
* both shapes.
@@ -1450,17 +1453,22 @@ async function deleteCascadedChildren(host: Pick<CascadePlanningHost, 'cascadeIs
14501453
// [#22305] An earlier chunk's own cascade may have entered a member.
14511454
const chunk = dependents.slice(i, i + batchSize).filter((depId) => !cascadeSetHas(cascadeSet.entered, childName, depId));
14521455
if (chunk.length === 0) continue;
1453-
if (batchable && await readDoorReturnsChunk(host, childName, chunk, context)) {
1456+
// Only the set's collected MEMBERS are batched. A dependent the probe
1457+
// found that the collect phase never held (a row a hook of this cascade
1458+
// wrote after the set was collected) is deleted by id below, as the root
1459+
// of its own cascade, so it keeps its own recycle-bin entry: a predicate
1460+
// delete opens none, and the root's entry snapshotted only the members.
1461+
const members = chunk.filter((depId) => cascadeSetHas(cascadeSet.members, childName, depId));
1462+
if (batchable && members.length > 0 && await readDoorReturnsChunk(host, childName, members, context)) {
14541463
try {
14551464
await host.cascadeIssuedDeletes.run(
1456-
{ object: childName, ids: new Set(chunk.map((depId) => String(depId))), claimed: false },
1457-
() => host.delete(childName, { where: { id: { $in: chunk } }, multi: true, context } as any),
1465+
{ object: childName, ids: new Set(members.map((depId) => String(depId))), claimed: false },
1466+
() => host.delete(childName, { where: { id: { $in: members } }, multi: true, context } as any),
14581467
);
14591468
// The predicate arm walks a child's own relations only when one
14601469
// points at it; entered either way, so a second path to the same row
14611470
// never deletes it again.
1462-
for (const depId of chunk) cascadeSetAdd(cascadeSet.entered, childName, depId);
1463-
continue;
1471+
for (const depId of members) cascadeSetAdd(cascadeSet.entered, childName, depId);
14641472
} catch (e) {
14651473
if (!(e instanceof CascadeBatchNarrowed)) throw e;
14661474
}

‎packages/objectql/src/recycle-bin/capture.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,35 @@ describe("an organization's delete takes its entries with it (ruling Q4)", () =>
534534
});
535535
});
536536

537+
/**
538+
* A `cascade` dependent the walk's probe finds but the collect phase never
539+
* held (a row a hook of this same cascade wrote after the set was collected)
540+
* is no member of the root's entry. It is deleted by id, as the root of its
541+
* own cascade, so it keeps an entry of its own; a cascade batch (one predicate
542+
* delete per chunk, which opens no entry) takes only the collected members.
543+
*/
544+
describe('a cascade dependent written after the set was collected keeps its own entry', () => {
545+
it('is deleted by id with an entry of its own, while the collected members stay rows of the root entry', async () => {
546+
const { engine, driver } = await makeEngine();
547+
seedAccountTree(driver);
548+
// The contacts are batched; their before phase runs ahead of their own
549+
// walk, which probes the notes: n9 lands between the collect and that probe.
550+
engine.on('beforeDelete', 'contact', (ctx: any) => {
551+
if (ctx.input?.id === 'c1') driver.seed('note', { id: 'n9', body: 'late', contact: 'c1' });
552+
});
553+
554+
await engine.delete('account', { where: { id: 'acc1' }, ...USER } as any);
555+
556+
expect(driver.rowsOf('note')).toHaveLength(0);
557+
const entries = entriesOf(driver);
558+
expect(entries.map((e) => `${String(e.object_name)}/${String(e.record_id)}`).sort()).toEqual(['account/acc1', 'note/n9']);
559+
const root = entries.find((e) => e.object_name === 'account')!;
560+
expect((root.snapshot as any).rows.map((r: any) => `${r.object}/${r.id}`)).toEqual([
561+
'account/acc1', 'contact/c1', 'contact/c2', 'note/n1',
562+
]);
563+
});
564+
});
565+
537566
describe('cascadeMembersInOrder', () => {
538567
it('lists the root first, then breadth-first along the cascading edges, then any member no edge reached', () => {
539568
const set: CascadeDeleteSet = {

0 commit comments

Comments
 (0)