[BUG] Loop mutations can leave multi-task groups partially mutated on failure
Summary
delete_loop and set_loop_enabled can leave a multi-task loop group partially mutated when a task-level storage operation fails partway through the operation.
Both functions iterate over the task IDs in a loop group and perform separate remove_task/update_task writes. Each individual write is protected by its own FileLock, but there is no transaction or rollback covering the full iteration. If a later operation fails, earlier successful writes remain persisted.
I reproduced both cases against HEAD 53200c36e on 2026-09-22. The currently audited supported loop-creation paths produce one task per group, so the multi-task trigger is currently latent rather than reachable through normal loop creation. However, the data model and grouping logic support multiple task IDs per group, and no storage-layer invariant prevents such a group from existing.
Expected vs actual behavior
Expected:
If a task operation fails during delete_loop or set_loop_enabled, the operation should not leave the loop group partially mutated. The group should either remain in its original state or the API should provide an explicit, documented partial-success contract.
Actual:
delete_loop removes tasks one at a time. When a later remove_task call fails, the function returns an error, but tasks removed by earlier successful calls remain deleted. The loop group is therefore left with only some of its original tasks.
set_loop_enabled updates tasks one at a time. When a later update_task call fails, earlier successful updates remain persisted, leaving the group with different enabled states.
In the reproduced partial-enable case, list_loop_summaries reports the group as enabled because LoopSummary.enabled is computed using any(task.enabled for task in tasks), even though another task in the same group remains disabled.
Steps to reproduce
- Construct a temporary task store containing two ScheduledTask rows with the same LOOP_GROUP_ID_PARAM, representing one loop group with two task IDs.
- Call delete_loop while making the second remove_task call return False after the first call has successfully written its deletion.
- Observe that delete_loop returns an error, but the first task has already been removed from the store while the second task remains.
- Repeat the setup with two disabled tasks and call set_loop_enabled(enabled=True), making the second update_task call return False.
- Observe that set_loop_enabled returns an error, but the first task remains enabled while the second remains disabled.
Can you reproduce it consistently?
Yes
How often does it occur?
Every time
Operating system
Windows
Logs and error output
delete_loop:
BEFORE: ['2111cf2431bf', '519155d2f17e']
[call 1] remove_task('2111cf2431bf') -> True [call 2] remove_task('519155d2f17e') -> False
mutation: None error: "loop task '519155d2f17e' could not be removed"
AFTER: ['519155d2f17e']
set_loop_enabled:
BEFORE: [('ba5dab9dd8c7', enabled=False), ('406f9997bc28', enabled=False)]
[call 1] update_task('ba5dab9dd8c7', enabled=True) -> True [call 2] update_task('406f9997bc28', enabled=True) -> False
mutation: None error: "loop 'en-grp' could not be updated"
AFTER: [('ba5dab9dd8c7', enabled=True), ('406f9997bc28', enabled=False)]
Additional context
Verified against HEAD 53200c36e on 2026-09-22.
Relevant code:
- infrastructure/scheduling/scheduler/loops.py: delete_loop
- infrastructure/scheduling/scheduler/loops.py: set_loop_enabled
- infrastructure/scheduling/scheduler/loops.py: _summarize_group
- infrastructure/scheduling/scheduler/storage/task_store.py: remove_task
- infrastructure/scheduling/scheduler/storage/task_store.py: update_task
The reproduction uses a temporary task store and does not modify production repository files.
The multi-task group is constructed directly through the task storage layer because the currently audited supported loop-creation paths produce one task per group. The reproduction demonstrates that the mutation functions do not safely handle a multi-task group if such a state exists.
Both partial-mutation cases were reproduced successfully at the stated HEAD.
Source: Tracer-Cloud/opensre