Skip to content

Commit 8bdf018

Browse files
teryltTeryl Taylor
authored andcommitted
feat: CPEX Rust config (#38)
* 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>
1 parent bf13b1e commit 8bdf018

15 files changed

Lines changed: 3371 additions & 101 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ tracing = "0.1"
2929
uuid = { version = "1", features = ["v4"] }
3030
paste = "1"
3131
futures = "0.3"
32+
hashbrown = "0.15"

crates/cpex-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ thiserror = { workspace = true }
2525
tracing = { workspace = true }
2626
uuid = { workspace = true }
2727
futures = { workspace = true }
28+
hashbrown = { workspace = true }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# CPEX Core Examples
2+
3+
## plugin_demo
4+
5+
A complete end-to-end example showing how to build plugins, load config, and invoke hooks with the CPEX runtime.
6+
7+
### What it demonstrates
8+
9+
- **Defining hook types and payloads**`ToolPreInvoke` and `ToolPostInvoke` hooks with a shared `ToolInvokePayload`
10+
- **Building plugins** — three plugins (`IdentityResolver`, `PiiGuard`, `AuditLogger`) implementing `Plugin` + `HookHandler<H>` for different hook types
11+
- **Multi-hook registration** — a single plugin instance (e.g., `IdentityResolver`) registered for multiple hooks (`tool_pre_invoke` and `tool_post_invoke`) via the factory pattern
12+
- **Plugin factories**`PluginFactory` implementations that create plugin instances and wire up typed handler adapters
13+
- **YAML config loading**`plugin_demo.yaml` declares plugins, policy groups, and routing rules
14+
- **Policy groups and tag-based routing** — the `pii` policy group activates `PiiGuard` only for tools tagged with `pii`
15+
- **Route resolution** — exact tool matches, wildcard catch-all, tag-driven plugin selection
16+
- **PluginContext**`global_state` used to pass PII clearance between hooks, `local_state` for per-plugin scratch data
17+
- **BackgroundTasks** — fire-and-forget plugins (`AuditLogger`) spawn background tasks; `wait_for_background_tasks()` awaits them
18+
- **PluginContextTable** — context table threaded from pre-invoke to post-invoke to preserve plugin state
19+
20+
### Running
21+
22+
From the workspace root:
23+
24+
```
25+
cargo run --example plugin_demo
26+
```
27+
28+
### Scenarios
29+
30+
The demo runs five scenarios against three registered plugins:
31+
32+
| Scenario | Tool | User | Outcome |
33+
|----------|------|------|---------|
34+
| 1 | get_compensation | alice (no clearance) | DENIED by pii-guard |
35+
| 2 | get_compensation | alice (with clearance) | ALLOWED, then post-invoke fires |
36+
| 3 | list_departments | bob | ALLOWED (no PII tag, pii-guard skipped) |
37+
| 4 | some_other_tool | charlie | ALLOWED (wildcard route) |
38+
| 5 | list_departments | (empty) | DENIED by identity-resolver |
39+
40+
### Files
41+
42+
- `plugin_demo.rs` — Rust source with plugins, factories, and main
43+
- `plugin_demo.yaml` — YAML config with plugins, policy groups, and routes

0 commit comments

Comments
 (0)