agentleFS
MicrosoftKnown publishermicrosoft/duroxide-python/.github/copilot-instructions.md

duroxide-python

Copilot instructions34 starsChanged 57 days agoMITView on GitHub

🚨 STOP: DO NOT git commit OR git push WITHOUT EXPLICIT USER PERMISSION 🚨 Always ask before committing or pushing. Never assume. When making changes to this SDK, always check ../duroxide-node for symmetry: 1. Bug fixes: If you fix a bug here, apply the same fix in duroxide-node (and vice versa) 2. API parity: Every public API in one SDK must have an equivalent in the other (adjusted for language idioms β€” snake_case in Python, camelCase in JS) 3. Test…

  • Reads credentials
  • Commits and pushes

The file

We split this copilot-instructions.md into the rules it gives an agent

6 rules. About 0 are common, 3 are distinctive, and 3 are very specific to this project.

  • β‰ˆ0

    Common Β· estimate

    Many files like this have a rule of this kind.

  • β‰ˆ3

    Distinctive Β· estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When making changes to this SDK, always check ../duroxide-node for symmetry:

    line 9
  2. Distinctive:

    Bug fixes: If you fix a bug here, apply the same fix in duroxide-node (and vice versa)

    line 10
  3. Distinctive:

    API parity: Every public API in one SDK must have an equivalent in the other (adjusted for language idioms β€” snake_case in Python, …

    line 11
  4. Distinctive:

    Test parity: Every test in one SDK must have a corresponding test in the other

    line 12
  5. Very specific:

    To add a new task type: Add to both context.py AND types.rs, add execution in handlers.rs.

    line 150
  6. Very specific:

    Use SqliteProvider.in_memory() only for SQLite smoketest

    line 214

β‰ˆ β€œCommon” is an estimate: at least 1 in 5 of the 1,644 Copilot instructions files we read have a rule of the same kind, not the same words. β€œVery specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
copilot-instructions.md

duroxide-python β€” Copilot Instructions

🚨 STOP: DO NOT git commit OR git push WITHOUT EXPLICIT USER PERMISSION 🚨

Always ask before committing or pushing. Never assume.

⚠️ SDK Symmetry β€” duroxide-python & duroxide-node MUST stay in sync

When making changes to this SDK, always check ../duroxide-node for symmetry:

  1. Bug fixes: If you fix a bug here, apply the same fix in duroxide-node (and vice versa)
  2. API parity: Every public API in one SDK must have an equivalent in the other (adjusted for language idioms β€” snake_case in Python, camelCase in JS)
  3. Test parity: Every test in one SDK must have a corresponding test in the other
  4. Feature parity: New duroxide features exposed in one SDK must be exposed in both
  5. Rust handler code (src/handlers.rs): Both SDKs share the same interop architecture β€” execute_task, make_select_future, make_join_future should be kept structurally identical

Project Overview

duroxide-python is a Python SDK for the duroxide durable execution runtime. It wraps the Rust duroxide library via PyO3/maturin, providing a generator-based API for writing deterministic, replayable workflows.

This is a thin binding layer β€” the replay engine, state management, providers, and observability live in the duroxide Rust crate (peer folder ../duroxide). This project only bridges Python ↔ Rust.

Architecture

Python (generators)  ←→  PyO3 bridge  ←→  duroxide (Rust core)
                          ↕
                    tokio runtime (global)
  • Orchestrations: Python generators that yield scheduling commands (dicts) to Rust
  • Activities: Regular Python functions called by Rust via block_in_place + Python::with_gil()
  • Providers: PostgreSQL (duroxide-pg) or SQLite β€” configured at startup
  • Tracing: Delegates to Rust tracing β€” controlled by RUST_LOG env var

Key Files

Rust Source (src/)

FileRole
lib.rsPyO3 module entry, #[pyfunction] exports
types.rsScheduledTask enum (Python→Rust protocol)
handlers.rsCore interop: orchestration loop, activity handler, execute_task, select/join
runtime.rsPyRuntime β€” global tokio runtime, start/shutdown
client.rsPyClient β€” all client methods with py.allow_threads()
provider.rsPySqliteProvider
pg_provider.rsPyPostgresProvider

Python Source (python/duroxide/)

FileRole
__init__.pyPublic API: SqliteProvider, PostgresProvider, Client, Runtime, decorators
context.pyOrchestrationContext, ActivityContext (yield scheduling commands)
driver.pyGenerator driver: create_generator, next_step, dispose_generator

Tests (tests/)

FileTestsSchema
test_e2e.py27duroxide_python_e2e
test_races.py7duroxide_python_races
test_admin_api.py14duroxide_python_admin
scenarios/test_toygres.py6duroxide_python_toygres

Build & Test

cd duroxide-python
source .venv/bin/activate

# Build (ALWAYS use maturin, never bare cargo build)
maturin develop                    # debug build + install into venv
maturin develop --release          # release build

# Lint
cargo clippy --all-targets         # must pass with zero warnings

# Test (requires DATABASE_URL in .env for PG tests)
pytest -v                          # all 54 tests
pytest tests/test_e2e.py -v        # e2e only
pytest -s                          # show Rust tracing output

# Logging
RUST_LOG=info pytest -s            # see orchestration/activity traces

⚠️ GIL Deadlock β€” The #1 Pitfall

Every Rust method that calls TOKIO_RT.block_on() MUST release the GIL first:

fn my_method(&self, py: Python<'_>, ...) -> PyResult<...> {
    py.allow_threads(|| {
        TOKIO_RT.block_on(async { ... })
            .map_err(|e| format!("{e}"))
    })
    .map_err(PyRuntimeError::new_err)
}

Without py.allow_threads(), Python holds the GIL while blocking on tokio. Orchestration handlers on tokio threads need the GIL β†’ deadlock. See pyo3-interop skill for full details.

Interop Model

Orchestrations: Generator Protocol

Python generators yield scheduling commands as plain dicts. The Rust handler loop:

  1. Calls create_generator(payload) β†’ Python creates generator, calls gen.send(None) β†’ first task
  2. Loops: execute_task() in Rust β†’ next_step(result) β†’ Python calls gen.send(value) β†’ next task
  3. Generator returns (StopIteration) β†’ orchestration complete

Activities: Synchronous Call

Rust calls Python activity functions synchronously via block_in_place + Python::with_gil(). Activities are regular def functions (not generators, not async).

Tracing: Global Context Maps

static ORCHESTRATION_CTXS: LazyLock<Mutex<HashMap<String, OrchestrationContext>>>
static ACTIVITY_CTXS: LazyLock<Mutex<HashMap<String, ActivityContext>>>

Python ctx.trace_info() β†’ PyO3 function β†’ looks up Rust context by key β†’ delegates to ctx.trace().

Both maps are keyed by an atomic token that is new for every invocation (act-{n}, orch-{n}), never by instance_id: two replays of one instance can be alive in the same process, and each must only reach its own context. Rust passes the orchestration token in ctxInfo["_ctxToken"].

ScheduledTask Protocol

Python OrchestrationContext methods return dicts with a "type" key. Rust deserializes to ScheduledTask enum:

# Python side (context.py)
def schedule_activity(self, name, input):
    return {"type": "activity", "name": name, "input": json.dumps(input)}
// Rust side (types.rs)
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum ScheduledTask {
    Activity { name: String, input: String },
    Timer { delay_ms: u64 },
    // ... etc
}

To add a new task type: Add to both context.py AND types.rs, add execution in handlers.rs.

Key Patterns

Error Handling Across py.allow_threads

PyErr is not Send. Map errors inside, convert outside:

py.allow_threads(|| {
    TOKIO_RT.block_on(async { ... }).map_err(|e| format!("{e}"))
})
.map_err(PyRuntimeError::new_err)

Provider Polymorphism

PyO3 doesn't support trait objects directly. Detect provider type in Python:

if getattr(provider, "_type", None) == "postgres":
    self._native = PyRuntime.from_postgres(provider._native, options)
else:
    self._native = PyRuntime.from_sqlite(provider._native, options)

Activity Client Access

ctx.get_client() calls activity_get_client(token) PyO3 function β†’ looks up ActivityContext in ACTIVITY_CTXS map β†’ calls ctx.get_client() β†’ wraps result as PyClient. Activities can use this to start orchestrations, raise events, etc.

Metrics Snapshot

runtime.metrics_snapshot() returns a dict with 17 counters (orch starts/completions/failures, activity results, dispatcher stats, provider errors). Returns None if observability is not enabled.

Observability Options

PyRuntimeOptions includes log_format, log_level, service_name, service_version which map to duroxide::ObservabilityConfig.

Non-Generator Orchestrations

driver.py handles functions that return without yielding (checked via isinstance(gen, types.GeneratorType)). These are treated as immediate completion.

PyO3 Object Mutability

#[pyclass(get_all)] fields are read-only from Python. Use Python wrapper objects (e.g., OrchestrationResult) instead of mutating PyO3 objects.

Determinism Rules

Orchestration code must be deterministic β€” same input + history = same sequence of yields:

βœ… Safe❌ Breaks Replay
yield ctx.utc_now()time.time()
yield ctx.new_guid()uuid.uuid4()
ctx.trace_info()print()
yield ctx.schedule_timer(ms)time.sleep()
Pure computationI/O, HTTP, DB, randomness

Testing Conventions

  • PostgreSQL tests use schema isolation (one schema per test file)
  • python-dotenv loads DATABASE_URL from .env
  • PyRuntimeOptions(dispatcher_poll_interval_ms=50) for fast test dispatch
  • runtime.shutdown(100) β€” short timeout, it waits the full duration
  • Use SqliteProvider.in_memory() only for SQLite smoketest
  • worker_lock_timeout_ms=2000 for tests needing fast cancellation detection

Dependencies

  • Rust: duroxide (core), duroxide-pg (PG provider), sqlx, tokio, pyo3, tracing
  • Python: maturin (build), pytest (test), python-dotenv (test config)
  • Build: maturin develop (not cargo build) β€” produces the .so/.dylib Python can import
  • Cargo dependencies: Release manifests must use crates.io versions for duroxide and duroxide-pg; remove temporary local [patch.crates-io] sections or inline path = overrides before release validation.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.