Principle:0xsline OpenChatCut Generation Job Lifecycle
| Knowledge Sources | |
|---|---|
| Domains | AI_Generation, Job_Management, Asynchronous |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
Lifecycle principle that manages asynchronous AI media generation jobs through a queue-based state machine: creation, queued, running, progress tracking, terminal (completed/failed), with persistence, resumption, and retention cleanup.
Description
Generation Job Lifecycle provides the state machine for AI media generation. When a user requests AI-generated media (image, video, voice, music, sound), the request is wrapped in a job that tracks its entire lifecycle:
- Creation: `createGenerationJob` registers the job with a unique operation ID, persists it to the SQLite store, and queues the async task via `queueMicrotask`.
- Queued: The job starts in `queued` status with `progress: 0`.
- Running: The async task function (`GenerationJobTask`) calls the provider, reporting progress via the `update` callback.
- Progress: The job's `status`, `progress`, `phase`, `processedFrames`, `totalFrames`, and `resultUrls` are updated as the provider reports.
- Terminal: The job completes (`completed` with result URLs) or fails (`failed` with error and retryability).
- Persistence: Jobs are persisted to SQLite so they survive process restarts; `resumeRestoredJobs` re-attaches to running provider tasks on restart.
- Retention: Terminal jobs are evicted after their retention period (default 60 minutes) via `cleanOldJobs`.
The lifecycle supports:
- Long-polling: `action: 'wait'` blocks until jobs reach a terminal status or timeout.
- Resume: `action: 'resume'` re-engages with a provider task that may still be running.
- Deduplication: Creating a job with an existing operation ID returns the existing job.
Usage
Use this principle when:
- Building an async generation pipeline that needs crash recovery and progress tracking.
- Managing jobs across multiple AI providers with different latency profiles.
- Providing real-time progress updates to a UI via long-polling.
Theoretical Basis
The job state machine:
# Abstract algorithm (NOT real implementation)
STATES = {'queued', 'running', 'completed', 'failed'}
TERMINAL = {'completed', 'failed'}
def create_job(params, task, options):
job = Job(id=uuid(), status='queued', progress=0, ...)
store.persist(job)
queue_microtask(lambda: run_job(job, task))
return {operationId: job.id, status: 'queued'}
def run_job(job, task, options):
update(job, status='running')
try:
result = task(job.id, update, register_download)
update(job, status='completed', result_urls=result.urls)
except Error as e:
update(job, status='failed', error=e.message, retryable=e.retryable)
def wait(job_ids, timeout):
deadline = now() + timeout
while now() < deadline:
if all(j in TERMINAL for j in get_jobs(job_ids)):
break
sleep(0.25)