Model/repositories/tasks/subtask_repository.py
Jun-te Kim b1ff711260 perf(modelling_e2e): batch SubTask bookkeeping to stop per-property writes
Even after batching the data writes, the handler still wrote to the DB per
property through the orchestrator's SubTask bookkeeping: create + start +
complete each self-committed, and _cascade re-listed every sibling and re-saved
the parent on every transition — ~5 writes per property plus an O(N^2) cascade.

- TaskOrchestrator.run_subtasks: create all children in one INSERT, run each
  (failures isolated per child), then persist all terminal states in one bulk
  save and cascade the parent once. Children go WAITING -> terminal; the
  transient IN_PROGRESS row is never written.
- SubTaskRepository.create_many / save_many (bulk INSERT / bulk fetch + update).
- _cascade short-circuits when the Task is already FAILED (terminal) — skips the
  sibling roll-up entirely.
- modelling_e2e handler fans out via run_subtasks instead of per-property
  create_child_subtask + run_subtask.

Per N-property batch the SubTask bookkeeping drops from ~5N writes + an O(N^2)
cascade to ~2 writes + 1 cascade.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 19:26:42 +00:00

31 lines
945 B
Python

from abc import ABC, abstractmethod
from uuid import UUID
from domain.tasks.subtasks import SubTask
class SubTaskRepository(ABC):
@abstractmethod
def create(self, subtask: SubTask) -> SubTask: ...
@abstractmethod
def create_many(self, subtasks: list[SubTask]) -> None:
"""Persist many SubTasks in one round-trip (one INSERT + one commit) —
the batch form of ``create`` for callers that fan a Task out into many
children at once."""
...
@abstractmethod
def get(self, subtask_id: UUID) -> SubTask: ...
@abstractmethod
def save(self, subtask: SubTask) -> None: ...
@abstractmethod
def save_many(self, subtasks: list[SubTask]) -> None:
"""Persist updates to many SubTasks in one round-trip (one bulk fetch +
one commit) — the batch form of ``save``."""
...
@abstractmethod
def list_by_task(self, task_id: UUID) -> list[SubTask]: ...