Skip to content

feat: add PyO3 bindings support for plugin framework - #67

Closed
tedhabeck wants to merge 14 commits into
devfrom
issue-19
Closed

feat: add PyO3 bindings support for plugin framework #67
tedhabeck wants to merge 14 commits into
devfrom
issue-19

Conversation

@tedhabeck

@tedhabeck tedhabeck commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

This branch implements a complete Rust-based backend for the CPEX plugin framework using PyO3 bindings.

Closes: #19

Changes

🆕 New Files Created (15 files)

Rust Implementation (crates/cpex-python/)

  1. crates/cpex-python/Cargo.toml - PyO3 crate configuration with dependencies
  2. crates/cpex-python/src/lib.rs - PyO3 module definition and exports
  3. crates/cpex-python/src/manager.rs - PyPluginManager implementation with async support
  4. crates/cpex-python/src/payload_registry.rs - Hook-to-payload mapping for 12 hook types
  5. crates/cpex-python/src/conversions.rs - Python ↔ Rust type conversions
  6. crates/cpex-python/src/types/mod.rs - Type module exports
  7. crates/cpex-python/src/types/enums.rs - PyPluginMode, PyOnError enums
  8. crates/cpex-python/src/types/config.rs - PyPluginConfig wrapper
  9. crates/cpex-python/src/types/result.rs - PyPluginResult with allow/deny/modify
  10. crates/cpex-python/src/types/context.rs - PyPluginContext, PyPluginContextTable
  11. crates/cpex-python/src/types/extensions.rs - PyExtensions wrapper
  12. crates/cpex-python/src/types/payload.rs - PyMessagePayload with copy-on-write

Python Layer

  1. cpex/_native.py - Python wrapper importing Rust extension
  2. cpex/_native.pyi - Type stubs (227 lines) for IDE support

Testing & Benchmarking

  1. tests/integration/test_rust_backend_integration.py - 10 integration tests
  2. tests/benchmarks/benchmark_rust_vs_python.py - Performance comparison suite
  3. tests/benchmarks/init.py - Benchmarks package

Documentation

  1. docs/pyo3-bindings-guide.md - Comprehensive guide (502 lines)

✏️ Modified Files (6 files)

  1. Cargo.toml - Added cpex-python to workspace members, added once_cell dependency
  2. pyproject.toml - Added maturin build-system configuration
  3. cpex/init.py - Added backend selection logic (Rust vs Python)
  4. Makefile - Added Python Bindings section to help output
  5. tests/unit/cpex/framework/test_pyo3_payload.py - 16 unit tests for payload types
  6. docs/pyo3-bindings-implementation-summary.md - Updated implementation summary

Key Features Implemented

1. Complete Rust Backend

  • PyPluginManager with async initialize/invoke_hook/shutdown
  • 5-phase pipeline execution (SEQUENTIAL → TRANSFORM → AUDIT → CONCURRENT → FIRE_AND_FORGET)
  • Support for all 12 built-in hooks (8 CMF, 2 identity, 2 delegation)
  • Payload registry with dynamic type resolution
  • Bidirectional Python ↔ Rust conversions via JSON

2. Backend Selection System

  • Automatic detection of Rust extension availability
  • Graceful fallback to pure Python backend
  • Environment variable override (CPEX_BACKEND=rust|python)
  • get_backend_info() function for runtime inspection

3. Type Safety

  • Complete .pyi type stubs (227 lines)
  • Full IDE autocomplete support
  • Type checking with mypy/pyright
  • Comprehensive docstrings

4. Testing & Benchmarking

  • 16 unit tests for payload types (all passing)
  • 10 integration tests (5 passing, 5 skipped pending Python plugin bridge)
  • Performance benchmark suite comparing Rust vs Python backends
  • Automated test execution via make python-test

5. Documentation

  • 502-line comprehensive guide covering:
    • Architecture with diagrams
    • Building and installation
    • Usage examples and API reference
    • Performance considerations
    • Development guide
    • Troubleshooting

6. Build System Integration

  • Maturin-based build system
  • Makefile targets: python-build, python-build-release, python-install, python-test
  • Updated make help with Python Bindings section

Performance Characteristics

Benchmark Results (1000 iterations, no plugins):

  • Rust Backend: ~0.11 ms per invocation
  • Python Backend: ~0.0015 ms per invocation

Note: Rust is currently slower due to FFI overhead with no plugins. Performance benefits will appear with:

  • Rust-native plugins (future work)
  • Complex plugin pipelines
  • High-throughput scenarios

Code Statistics

  • Rust Code: ~2,500 lines across 12 files
  • Python Code: ~400 lines (wrapper + tests)
  • Type Stubs: 227 lines
  • Documentation: 502 lines
  • Tests: 26 tests (16 unit + 10 integration)
  • Benchmarks: 186 lines
  • Total: ~3,800 lines

Build & Usage

# Install dependencies
pip install -e .

# Build Rust extension
make python-install

# Verify Rust backend
python -c "import cpex; print(cpex.BACKEND)"  # Output: rust

# Run tests
make python-test

# Run benchmarks
PYTHONPATH="." python tests/benchmarks/benchmark_rust_vs_python.py

Future Work (Not in This Branch)

  • Python Plugin Bridge: Enable Rust backend to invoke Python plugins
  • Performance Optimization: Replace JSON with MessagePack/bincode
  • Rust Plugin SDK: Native Rust plugin development
  • CI Integration: Add Rust backend tests to CI pipeline
  • Wheel Building: Configure maturin for release builds

Breaking Changes

None. The implementation is fully backward compatible:

  • Pure Python backend remains the default if Rust extension unavailable
  • All existing APIs unchanged
  • Existing plugins work without modification

Dependencies Added

  • Rust: pyo3 (0.21), pyo3-async-runtimes, once_cell
  • Python: maturin (build-system only, in dev dependencies)

Testing Status

  • Unit Tests: 16/16 passing
  • Integration Tests: 5/10 passing (5 skipped pending Python plugin bridge)
  • Benchmarks: Complete and functional
  • Type Checking: All type stubs validated

Checks

  • make lint passes
  • make test passes

terylt and others added 12 commits May 6, 2026 14:02
* feat: initial revision rust core.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fix: addressed comments in PR. Updated PluginContext to match spec.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

---------

Signed-off-by: Teryl Taylor <terylt@ibm.com>
Co-authored-by: Teryl Taylor <terylt@ibm.com>
* feat: added yaml and routing rule support.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added example code to show how to load manager and plugins.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fixes: updated plugin errors, configs to more match python.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

---------

Signed-off-by: Teryl Taylor <terylt@ibm.com>
Co-authored-by: Teryl Taylor <terylt@ibm.com>
* feat: initial revision rust core.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fix: addressed comments in PR. Updated PluginContext to match spec.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added yaml and routing rule support.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added example code to show how to load manager and plugins.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fixes: updated plugin errors, configs to more match python.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: RUST CMF initial revision.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added invoke named support, added constants, fixed reviewed code.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added owned extensions and did some refactoring.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

---------

Signed-off-by: Teryl Taylor <terylt@ibm.com>
Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
Co-authored-by: Teryl Taylor <terylt@ibm.com>
Co-authored-by: Frederico Araujo <frederico.araujo@ibm.com>
* feat: initial revision rust core.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fix: addressed comments in PR. Updated PluginContext to match spec.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added yaml and routing rule support.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added example code to show how to load manager and plugins.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fixes: updated plugin errors, configs to more match python.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: RUST CMF initial revision.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added invoke named support, added constants, fixed reviewed code.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added owned extensions and did some refactoring.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added cgo and golang bindings, examples and readme.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* address P0/P1/P2 review findings (except #17)

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fix: address remaining P2/P3 review findings + testing gaps

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* docs: add CPEX Go public API spec

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* docs: renamed document

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* feat(cpex-rust): CGO review passes 1-11 + lint cleanup + Makefile targets

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fix: address linting issues, updated makefile to support building examples.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* docs: updated the go spec to reflect recent changes.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

---------

Signed-off-by: Teryl Taylor <terylt@ibm.com>
Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
Co-authored-by: Teryl Taylor <terylt@ibm.com>
Co-authored-by: Frederico Araujo <frederico.araujo@ibm.com>
Co-authored-by: Teryl Taylor <terylt@ibm.com>
Co-authored-by: Teryl Taylor <terylt@ibm.com>
)

Co-authored-by: Teryl Taylor <terylt@ibm.com>
* fix: initial revision APL.

* feat: apl-cpex bridge crate + plugin-registry-driven hook dispatch

* feat: add support for plugin calling in APL routes.

* feat: add more APL plugin support, unified config

* feat: added cedar direct PDP.

* feat: add identity hook and extensions.

* feat: added token delegation hooks and tests.

* feat: added plugin for jwt token identity, oauth and biscuit delegation, cedarling PDP.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* fix: updated identity and delegation to support keycloak. added delegate() function, and identity sections.

* fix: added some sample plugins, added updates to support cedar.

Signed-off-by: Teryl Taylor <terylt@ibm.com>

* feat: added session support, serialize and parallel and full effects capabilities.

* feat: add ffi pre-built .a library

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* chore: add workflow_dispatch target

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* fix: critical and high issues from review.

* feat: add APL FFI and go bindings

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* chore: add musl tools to musl runners

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* fix: potential double free after use bug.

* chore: update Go module paths after repo rename to cpex

* feat: map identity extension into cpex ffi

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* feat: add cpex_invoke_resolved abi

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* fix: has_hook_for handling

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

* chore: update headers

Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>

---------

Signed-off-by: Teryl Taylor <terylt@ibm.com>
Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
Co-authored-by: Frederico Araujo <frederico.araujo@ibm.com>
Signed-off-by: habeck <habeck@us.ibm.com>
Signed-off-by: habeck <habeck@us.ibm.com>
Signed-off-by: habeck <habeck@us.ibm.com>
Signed-off-by: habeck <habeck@us.ibm.com>
…problem on macOS where the linker was trying to resolve Python symbols at build time, which shouldn't happen for PyO3 extension modules.

Signed-off-by: habeck <habeck@us.ibm.com>
@tedhabeck
tedhabeck marked this pull request as ready for review June 11, 2026 13:58
@araujof araujof changed the title Issue 19 feat: add PyO3 bindings support for plugin framework Jun 12, 2026
@araujof araujof self-assigned this Jun 12, 2026
@araujof araujof added this to CPEX Jun 12, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in CPEX Jun 12, 2026
@araujof araujof moved this from Backlog to In review in CPEX Jun 12, 2026
@araujof araujof added this to the 0.2.0 milestone Jun 12, 2026
@tedhabeck tedhabeck closed this Jun 15, 2026
@github-project-automation github-project-automation Bot moved this from In review to Done in CPEX Jun 15, 2026
@araujof
araujof deleted the issue-19 branch July 24, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants