refactor(persistence): leave one guarded delete on ScheduledTaskRepository (#8529)

delete() removed a task after an owner check only. Since the queue
landed, the Gateway deletes through delete_with_queue_cancellation,
which refuses while an occurrence is launching or running and cancels a
queued one. The unguarded method stayed public with no production
caller. Remove it, and cover the remaining delete against real SQLite:
the other-owner, queued and launching cases.
This commit is contained in:
Hyeonsang Cho 2026-11-23 22:20:29 +09:00 committed by GitHub
parent 7287c7945f
commit ea1872e429
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 18 deletions

View File

@ -316,7 +316,12 @@ class ScheduledTaskRepository:
error: str,
now: datetime,
) -> str:
"""Delete a task only before queue execution begins."""
"""Delete a task only before queue execution begins.
This is the repository's only delete. Occurrences carry no foreign key
to their task, so a plain row delete would also remove a task whose
occurrence is already launching or running.
"""
async with self._sf() as session:
task = await self._lock_task(session, task_id)
if task is None or task.user_id != user_id:
@ -431,15 +436,6 @@ class ScheduledTaskRepository:
await session.refresh(row)
return self._row_to_dict(row)
async def delete(self, task_id: str, *, user_id: str) -> bool:
async with self._sf() as session:
row = await session.get(ScheduledTaskRow, task_id)
if row is None or row.user_id != user_id:
return False
await session.delete(row)
await session.commit()
return True
async def claim_due_tasks(
self,
*,

View File

@ -334,7 +334,7 @@ async def test_event_lines_survive_task_deletion(tmp_path):
service_for(tasks, runs)
occurrence_id, run_id = await occurrence(sf, runs)
await complete(tasks, occurrence_id, run_id)
assert await tasks.delete("task", user_id=OWNER) is True
assert await tasks.delete_with_queue_cancellation("task", user_id=OWNER, error="deleted", now=datetime.now(UTC)) == "deleted"
async with http_client(sf) as client:
response = await client.get("/api/threads/origin/scheduled-task-events")
assert response.status_code == 200, response.text

View File

@ -756,6 +756,62 @@ async def test_pause_atomically_cancels_waiting_run_but_rejects_launching_run(tm
await close_engine()
async def test_delete_atomically_cancels_waiting_run_but_rejects_launching_run(tmp_path):
await init_engine_from_config(DatabaseConfig(backend="sqlite", sqlite_dir=str(tmp_path)))
try:
sf = get_session_factory()
assert sf is not None
task_repo = ScheduledTaskRepository(sf)
run_repo = ScheduledTaskRunRepository(sf)
now = datetime.now(UTC)
await _seed_reuse_task(task_repo, task_id="task-delete-queued", now=now)
await run_repo.create(
run_record_id="task-run-delete-queued",
task_id="task-delete-queued",
thread_id="thread-1",
scheduled_for=now,
trigger="scheduled",
status="queued",
)
# Another user's delete finds nothing and leaves the queue alone.
assert await task_repo.delete_with_queue_cancellation("task-delete-queued", user_id="user-2", error="deleted while queued", now=now) == "not_found"
assert await task_repo.get("task-delete-queued", user_id="user-1") is not None
assert (await run_repo.list_by_task("task-delete-queued"))[0]["status"] == "queued"
result = await task_repo.delete_with_queue_cancellation("task-delete-queued", user_id="user-1", error="deleted while queued", now=now)
assert result == "deleted"
assert await task_repo.get("task-delete-queued", user_id="user-1") is None
(cancelled,) = await run_repo.list_by_task("task-delete-queued")
assert (cancelled["status"], cancelled["error"]) == ("interrupted", "deleted while queued")
await _seed_reuse_task(task_repo, task_id="task-delete-launching", now=now)
await run_repo.create(
run_record_id="task-run-delete-launching",
task_id="task-delete-launching",
thread_id="thread-1",
scheduled_for=now,
trigger="scheduled",
status="queued",
)
await run_repo.claim_queued_run(
"task-run-delete-launching",
lease_owner="worker-a",
now=now,
lease_seconds=120,
global_max_concurrent_runs=3,
)
result = await task_repo.delete_with_queue_cancellation("task-delete-launching", user_id="user-1", error="deleted while queued", now=now)
assert result == "executing"
assert await task_repo.get("task-delete-launching", user_id="user-1") is not None
assert (await run_repo.list_by_task("task-delete-launching"))[0]["status"] == "launching"
finally:
await close_engine()
async def test_manual_enqueue_cannot_unpause_a_task_that_cancels_its_waiting_run(tmp_path):
await init_engine_from_config(DatabaseConfig(backend="sqlite", sqlite_dir=str(tmp_path)))
try:

View File

@ -149,13 +149,6 @@ class _Repo:
self.items.pop(task_id, None)
return "deleted"
async def delete(self, task_id: str, *, user_id: str):
item = await self.get(task_id, user_id=user_id)
if item is None:
return False
self.items.pop(task_id, None)
return True
async def list_by_task(self, task_id: str):
return []