Skip to content

Commit db0eefe

Browse files
lifecycle: the purge's dependent-table scope compares parent names case-insensitively
Council pass 6 (gpt-6-astra), reproduced RED-first: PRAGMA foreign_key_list reports the parent as written in the FK clause and SQLite identifiers are case-insensitive, so a child declared REFERENCES SESSIONS(id) was outside the scoped foreign_key_check from 87ea3fa. With foreign keys enforced SQLite's own refusal still caught the orphaning delete; with enforcement off the scoped backstop missed it. Parents and reported names are now compared lower-cased.
1 parent 6bd0237 commit db0eefe

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎packages/agent-session-tools/src/agent_session_tools/context/lifecycle.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,17 @@ def _dependent_tables(conn):
104104
table added by a later migration -- or by the learner -- is covered
105105
without a list to keep in step.
106106
"""
107-
parents = {*records.TABLES, *_PURGED_TABLES}
107+
parents = {name.lower() for name in (*records.TABLES, *_PURGED_TABLES)}
108108
dependents = []
109109
for (table,) in conn.execute(
110110
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
111111
):
112112
quoted = '"' + table.replace('"', '""') + '"'
113+
# ``foreign_key_list`` reports the parent as written in the FK clause,
114+
# and SQLite identifiers are case-insensitive: compare them that way
115+
# (council pass 6 -- ``REFERENCES SESSIONS(id)`` was missed before).
113116
if any(
114-
row[2] in parents
117+
str(row[2]).lower() in parents
115118
for row in conn.execute(f"PRAGMA foreign_key_list({quoted})")
116119
):
117120
dependents.append(table)

‎packages/agent-session-tools/tests/test_lifecycle_purge_orphans.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ def test_session_maint_delete_with_a_pre_existing_orphan_deletes(
186186

187187

188188
class TestAnOrphanThePurgeWouldCreateIsStillRefused:
189+
def test_a_dependent_declared_with_the_parent_in_another_case_is_still_in_scope(
190+
self, tmp_path
191+
):
192+
"""SQLite identifiers are case-insensitive and ``PRAGMA foreign_key_list``
193+
reports the parent as written in the FK clause, so ``REFERENCES
194+
SESSIONS(id)`` named a parent the scoped check compared case-sensitively
195+
and missed (council pass 6). The dependent set must include it."""
196+
from agent_session_tools.context.lifecycle import _dependent_tables
197+
198+
path = _db(tmp_path)
199+
_plant(
200+
path,
201+
_session("held")
202+
+ [
203+
"CREATE TABLE shouty_ref(session_id TEXT NOT NULL REFERENCES SESSIONS(id))",
204+
"INSERT INTO shouty_ref VALUES('held')",
205+
],
206+
)
207+
conn = sqlite3.connect(path)
208+
try:
209+
assert "shouty_ref" in _dependent_tables(conn)
210+
finally:
211+
conn.close()
212+
189213
def test_a_dependent_the_schema_does_not_cascade_names_its_table(self, tmp_path):
190214
"""SQLite's own refusal, re-raised as the same type (the replication
191215
withdrawal path rolls back on IntegrityError) with the table named."""

0 commit comments

Comments
 (0)