Portable Run State

Export a durable run's journal as JSON and import it into another process or host with no shared disk

Move an in-progress durable run from one process or machine to another by exporting its journal as a JSON-safe blob (export_run / export_run_json) and importing it into a target journal (import_run / import_run_json).

python
from praisonaiagents import Agent
from praisonaiagents.runtime import (
    RunJournal,
    export_run_json,
    import_run_json,
)

# Worker A: export a paused or interrupted durable run
blob_json = export_run_json(source_journal, run_id="run-42")

# Put blob_json in Redis, a queue, or a database row...

# Worker B (no shared disk): import and resume as usual
resumed_id = import_run_json(target_journal, blob_json)
agent = Agent(name="Worker", instructions="Continue the durable task.")
Diagram

Portable Run State

💾 Worker A
SQLite RunJournal

📦 export_run_json

📨 Redis / Queue / DB

📥 import_run_json

💾 Worker B
SQLite RunJournal

✅ Resume Run

Quick Start

  1. Export a run from a journal
    python
    from praisonaiagents.runtime import export_run, export_run_json
    
    # As a JSON-safe Python dict
    blob = export_run(journal, "run-42")
    
    # Or directly as a JSON string for Redis / HTTP / queues
    text = export_run_json(journal, "run-42")
  2. Import into another journal
    python
    from praisonaiagents.runtime import import_run, import_run_json, PortableRunError
    
    # Import under the original run_id (fails if it already exists)
    run_id = import_run_json(other_journal, text)
    
    # Import under a new run_id, or explicitly replace an existing copy
    cloned_id = import_run(other_journal, blob, run_id="run-42-copy")
    replaced_id = import_run(other_journal, blob, overwrite=True)

How It Works

Diagram
Worker BTarget JournalSource JournalWorker AWorker BTarget JournalSource JournalWorker Aexport_run(journal, run_id)version=1 + run metadata + eventsTransfer JSON blobimport_run(journal, blob)Validate version, run_id, all event kinds before writingrun_id (lifecycle status preserved)

Exporting uses the journal itself as the single source of truth — there is no second state format to keep in sync.

Exported SymbolSignatureDescription
PORTABLE_RUN_VERSIONint (1)Schema version embedded in every exported blob. Mismatched versions raise PortableRunError.
PortableRunErrorRuntimeError, PraisonAIErrorRaised when a run does not exist, JSON is invalid, version mismatches, events are malformed, or target run_id already exists without overwrite=True.
export_run(journal: RunJournal, run_id: str) -> Dict[str, Any]Exports {"version", "run", "events"} for run_id. Raises PortableRunError if run_id is absent.
export_run_json(journal: RunJournal, run_id: str) -> strSerialises export_run(...) with json.dumps(..., default=str).
import_run(journal: RunJournal, blob: Dict[str, Any], *, run_id: Optional[str] = None, overwrite: bool = False) -> strValidates all events up front before writing, restores terminal status/outcome (so completed runs never resurrect as running), and returns the target run_id.
import_run_json(journal: RunJournal, text: str, *, run_id: Optional[str] = None, overwrite: bool = False) -> strParses JSON and delegates to import_run(...).

Best Practices

Avoid overwrite=True unless replacing a known snapshot

By default import_run refuses to import over an existing run_id so two runs’ events are never mixed into one replay index. Pass a fresh run_id= to fork, or overwrite=True (which deletes the destination’s prior events first) when intentionally replacing.

Keep producer and consumer on compatible PORTABLE_RUN_VERSION

Every blob records "version": PORTABLE_RUN_VERSION (currently 1). Incompatible versions fail fast with PortableRunError instead of half-importing.

Treat terminal runs as immutable

If an exported run already had a terminal outcome or non-running status (succeeded, failed, cancelled), import_run closes the imported run with that same status so it is not mistakenly resumed as interrupted work.


Related