bug(pedo): rejected deletes leave committed cascades and NULLIFY changes
Describe the bug
ObjectStore.delete() in chapter5/permission-embedded-data-objects/pedo/core/store.py can raise ReferentialIntegrityError after permanently deleting children and clearing references on other objects. The caller receives a rejected deletion, the requested parent still exists, but part of its graph has already been changed.
At main commit c3352738f4b6fe42fe34e3cf6a79bcb424a133b8, delete() calls _handle_delete_cascades() at line 307 before _check_restrict_references() at line 310. Recursive child deletes and NULLIFY writes each open and commit separate connections. A later restriction or failure cannot roll those writes back.
To Reproduce
The regression test uses the actual ObjectStore.delete() API and storage helpers. A small SQLite adapter translates PostgreSQL placeholders and JSON operators and supplies real SQL transactions; it does not replace the cascade, permission, reference, or persistence logic. No PostgreSQL server is required for this reproduction.
Run from a fresh checkout:
git clone https://github.com/bojieli/ai-agent-book.git pedo-atomic-repro
cd pedo-atomic-repro
git checkout c3352738f4b6fe42fe34e3cf6a79bcb424a133b8
python -m pip install pytest psycopg2-binary
# Obtain only the regression test; keep ObjectStore on the main baseline.
git fetch https://github.com/dakjdakd/ai-agent-book.git fix/pedo-atomic-delete
python -c "import pathlib, subprocess; pathlib.Path('tests/test_ch5_pedo_atomic_delete_repro.py').write_bytes(subprocess.check_output(['git', 'show', '0ccd26340822b531264aea85ce8971dbdd845ed7:tests/test_ch5_pedo_atomic_delete.py']))"
python -m pytest tests/test_ch5_pedo_atomic_delete_repro.py -k 'failed_delete and root_restrict' -qThe test seeds this graph:
| Object | Relationship |
|---|---|
p |
Parent being deleted |
a, b |
Children with parent_id="p" |
n |
NULLIFY reference to p, stored in both content.target_id and references.target |
r |
RESTRICT reference to p |
It then calls store.delete("p", AccessContext("owner")) and checks that the exception leaves the original rows intact.
Observed during verification on the main baseline:
ReferentialIntegrityError: Cannot delete p: referenced by 1 blocker objects via target (RESTRICT)
After the exception:
- p and r still exist.
- a and b have been deleted.
- n.content["target_id"] is null and n.references no longer contains "target".
- The assertion comparing the database to its original state fails.The regression suite also covers a later child's restriction or permission denial, a final parent write error, and reaction publication before the whole operation commits.
Expected behavior
A rejected deletion should preserve the whole graph: the parent, its children, and existing references should remain unchanged. A successful deletion should commit the complete cascade and NULLIFY updates before publishing any after_delete reactions.
The experiment's README describes the pipeline as permission checks and validators, persistence and referential-integrity mechanics, then asynchronous reactions. A reaction for a partially completed deletion must not become observable before the overall operation succeeds.
Technical details / Root cause
In the baseline store.py:
delete()at lines 285–318 runs cascades, checks RESTRICT references, deletes the requested object, and queues its reaction._handle_delete_cascades()at lines 427–456 callsself.delete()for children and CASCADE references, and_update_object()for NULLIFY references._update_object()at lines 606–616 and_delete_object()at lines 618–622 each open a new connection and explicitly commit it.- Each recursive
delete()also calls_queue_reactions()before the outer deletion has completed.
Successful leaf deletions conceal the problem because there is no later step to reject. A parent restriction or a later child failure exposes the separate commits. Moving the parent's RESTRICT check earlier would handle one ordering case, but would still leave earlier child deletes committed when a subsequent child or SQL write fails.
Impact
The offline reproduction verifies permanent child deletion and reference loss after a rejected parent deletion. Related regression cases verify that an earlier child is lost when a later child rejects deletion. The existing reaction call path also publishes child deletion events before the outer operation is known to succeed.
Related implementation / Existing behavior
- #686 introduced the dynamic software data-boundary experiment containing PEDO.
- #791 added the live security evaluation module and tests; it does not make the core store's recursive deletion transactional.
- #1067 corrected timeout exception handling in
pedo/eval/benchmark_spec_errors_v2.py; it does not changeObjectStore.delete()or its persistence helpers.
Scope
This issue concerns atomicity of hierarchy and reference-based deletion, NULLIFY writes, and the timing of deletion reactions. Tenant matching, ownership decisions, and validation of updated references are separate concerns. The related tenant-boundary PR #1161 touches the same store module but changes permission decisions rather than transaction boundaries.
Environment
- Baseline: main at
c3352738f4b6fe42fe34e3cf6a79bcb424a133b8. - Fix branch:
fix/pedo-atomic-delete, commit0ccd26340822b531264aea85ce8971dbdd845ed7. - Local runtime: Python 3.11.7 on Windows.
- Observed backend: SQLite executing the production storage helpers through the adapter described above.
- The production backend is PostgreSQL. Three optional PostgreSQL regression cases are included, but were not run locally; no PostgreSQL result is claimed here.
Source: bojieli/ai-agent-book