Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xsline OpenChatCut Generation Job Lifecycle

From Leeroopedia
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:

  1. Creation: `createGenerationJob` registers the job with a unique operation ID, persists it to the SQLite store, and queues the async task via `queueMicrotask`.
  2. Queued: The job starts in `queued` status with `progress: 0`.
  3. Running: The async task function (`GenerationJobTask`) calls the provider, reporting progress via the `update` callback.
  4. Progress: The job's `status`, `progress`, `phase`, `processedFrames`, `totalFrames`, and `resultUrls` are updated as the provider reports.
  5. Terminal: The job completes (`completed` with result URLs) or fails (`failed` with error and retryability).
  6. Persistence: Jobs are persisted to SQLite so they survive process restarts; `resumeRestoredJobs` re-attaches to running provider tasks on restart.
  7. 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)

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment