Add generation harness runtime

This commit is contained in:
2026-06-21 22:31:38 +08:00
parent 7ebdfb2582
commit 459ca9edef
18 changed files with 2846 additions and 419 deletions

View File

@@ -0,0 +1,48 @@
"""Execution control helpers for generation harness workflows."""
from typing import TYPE_CHECKING
from sqlalchemy.ext.asyncio import AsyncSession
from app.services.generation_jobs import finish_generation_job
if TYPE_CHECKING:
from app.db.models import GenerationJob, Story
class GenerationJobCanceledError(Exception):
"""Raised when a running worker job has been canceled by the user."""
class ExecutionControl:
"""Runtime control surface for cancelable generation workflows."""
def __init__(self, db: AsyncSession):
self.db = db
async def stop_if_cancel_requested(
self,
*,
job: "GenerationJob | None",
story: "Story | None" = None,
) -> None:
"""Stop a worker-owned job at the next safe checkpoint after cancellation."""
if job is None:
return
await self.db.refresh(job)
if job.current_step != "cancel_requested":
return
await finish_generation_job(
self.db,
job=job,
story=story,
status="canceled",
current_step="generation_canceled",
error_message="Generation canceled by user.",
message="Generation job was canceled after a user request.",
)
raise GenerationJobCanceledError()