plexus-substrate 0.2.7

Reference Plexus RPC server with conversation trees and LLM orchestration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Substrate

A Plexus RPC server providing conversation trees and LLM orchestration.

## What is Plexus RPC?

Plexus RPC is a protocol for building services where code IS schema. Services expose JSON schemas at runtime for all methods, enabling zero-drift type-safe client generation and instant streaming. The protocol supports tree-structured namespacing, where plugins organize hierarchically via dot-separated paths (`arbor.tree_create`, `cone.chat`).

Key features:
- **Self-describing**: Query any method's schema at runtime
- **Streaming-first**: All methods return streams by default
- **Tree-structured**: Organize methods in hierarchical namespaces
- **Language-agnostic**: Generate type-safe clients for any language

## Abstract

Substrate is a reference Plexus RPC server implementing conversation tree storage (Arbor), LLM orchestration (Cone), and development tools (ClaudeCode, Bash). It demonstrates the full Plexus RPC architecture: hierarchical plugin structure, runtime schema introspection, streaming by default, and cross-language client generation.

This document describes the Plexus RPC architecture as implemented in Substrate. Server-specific activations (Arbor, Cone, ClaudeCode) are documented separately.

## Architecture

### Layer Structure

```
┌────────────────────────────────────────────────────────────────┐
│                     Plexus RPC Backends                        │
│  (DynamicHub instance, future: remote hubs via URL)           │
├────────────────────────────────────────────────────────────────┤
│                         hub-macro                              │
│  #[hub_methods] #[hub_method(streaming)]                      │
│  → generates method enums, schemas, streaming annotations     │
├────────────────────────────────────────────────────────────────┤
│                         hub-core                               │
│  Activation trait, DynamicHub routing, PluginSchema types     │
│  ChildRouter trait, streaming infrastructure                  │
├────────────────────────────────────────────────────────────────┤
│                         substrate                              │
│  Foundation types (Handle, Value), serialization              │
└────────────────────────────────────────────────────────────────┘
```

### Activation Trait

The unified interface for all Plexus RPC plugins:

```rust
#[async_trait]
pub trait Activation: Send + Sync + 'static {
    type Methods: MethodEnumSchema;

    fn namespace(&self) -> &str;
    fn version(&self) -> &str;
    fn description(&self) -> &str;
    fn methods(&self) -> Vec<&str>;

    async fn call(&self, method: &str, params: Value)
        -> Result<PlexusStream, PlexusError>;

    fn plugin_schema(&self) -> PluginSchema;
}
```

All plugins implement `Activation`. The `plugin_schema()` method returns a JSON Schema describing available methods, parameters, and return types.

### Tree-Structured Namespace

Plexus RPC organizes methods hierarchically via dot-separated paths:

```
plexus
├── arbor.tree_create
├── arbor.node_create_text
├── cone.create
├── cone.chat
├── echo.echo
└── health.check
```

Nested plugins implement `ChildRouter` to delegate calls to children. Plexus RPC supports arbitrary nesting depth.

### Schema System

Every Plexus RPC activation exposes a `schema` method:

```rust
// Query any plugin's schema
substrate.call("echo.schema", {})
substrate.call("arbor.schema", {})
```

Schemas include:
- Method names and descriptions
- Parameter types (JSON Schema)
- Return types (JSON Schema)
- Streaming annotation
- Child plugin summaries (namespace, description, hash)

Child schemas are **not included recursively**. Clients fetch child schemas individually via `{namespace}.schema`, enabling lazy traversal of large plugin trees.

### Hash-Based Versioning

Each method schema has a content hash. Parent hashes incorporate child hashes. The root hash changes when any descendant changes. This enables:

- Cache invalidation
- Client version detection
- Schema drift warnings

### Streaming by Default

All Plexus RPC methods return `PlexusStream`, a stream of `PlexusStreamItem`:

```rust
pub enum PlexusStreamItem {
    Content { metadata, content_type, data },
    Progress { metadata, message, percentage },
    Error { metadata, message, code, recoverable },
    Done { metadata },
}
```

Non-streaming methods emit a single `Content` item followed by `Done`. Streaming methods emit multiple items.

## Implementation Patterns

### Leaf Activation (Macro-Generated)

Simple plugins with methods, no children. Use `#[hub_methods]`:

```rust
#[derive(Clone)]
pub struct Echo;

#[hub_macro::hub_methods(
    namespace = "echo",
    version = "1.0.0",
    description = "Echo messages back"
)]
impl Echo {
    #[hub_macro::hub_method(
        streaming,
        params(
            message = "The message to echo",
            count = "Number of times to repeat"
        )
    )]
    async fn echo(
        &self,
        message: String,
        count: u32
    ) -> impl Stream<Item = EchoEvent> {
        stream! {
            for _ in 0..count {
                yield EchoEvent::Echo { message: message.clone() };
            }
        }
    }
}
```

The macro generates:
- `EchoMethod` enum with JSON Schema
- `Activation` trait implementation
- Automatic `schema` method dispatch

### Hub Activation (Macro-Generated with Children)

Activations containing other activations (hubs). Add `hub` flag and implement `plugin_children()`:

```rust
#[hub_macro::hub_methods(
    namespace = "solar",
    version = "1.0.0",
    description = "Solar system model",
    hub
)]
impl Solar {
    async fn observe(&self) -> impl Stream<Item = SolarEvent> { /* ... */ }

    pub fn plugin_children(&self) -> Vec<PluginSchema> {
        self.planets.iter()
            .map(|p| p.to_plugin_schema())
            .collect()
    }
}

#[async_trait]
impl ChildRouter for Solar {
    fn router_namespace(&self) -> &str { "solar" }

    async fn router_call(&self, method: &str, params: Value)
        -> Result<PlexusStream, PlexusError>
    {
        Activation::call(self, method, params).await
    }

    async fn get_child(&self, name: &str) -> Option<Box<dyn ChildRouter>> {
        self.planets.iter()
            .find(|p| p.name == name)
            .map(|p| Box::new(p.clone()) as Box<dyn ChildRouter>)
    }
}
```

Register hubs with `register_hub()`:

```rust
let plexus = DynamicHub::new()
    .register(Echo)
    .register_hub(Solar::new());
```

### Dynamic Activation (Hand-Implemented)

When activations are created from runtime data, manually implement `Activation`:

```rust
#[async_trait]
impl Activation for Planet {
    type Methods = PlanetMethod;

    fn namespace(&self) -> &str { &self.name }
    fn version(&self) -> &str { "1.0.0" }
    fn description(&self) -> &str { &self.description }
    fn methods(&self) -> Vec<&str> { vec!["info", "schema"] }

    async fn call(&self, method: &str, params: Value)
        -> Result<PlexusStream, PlexusError>
    {
        match method {
            "info" => Ok(self.info_stream()),
            "schema" => {
                let schema = self.plugin_schema();
                Ok(wrap_stream(futures::stream::once(async { schema })))
            }
            _ => route_to_child(self, method, params).await
        }
    }

    fn plugin_schema(&self) -> PluginSchema {
        PluginSchema {
            plugin_id: self.id,
            namespace: self.name.clone(),
            version: "1.0.0".into(),
            description: self.description.clone(),
            methods: vec![/* method schemas */],
            children: vec![],
            hash: compute_hash(/* ... */),
        }
    }
}
```

Dynamic activations must manually:
- Include `"schema"` in `methods()`
- Handle `"schema"` in `call()`
- Implement `ChildRouter` if they have children

## Code Generation Pipeline

```
   Rust Activation            hub-macro              Runtime Schema
  ┌──────────┐              ┌──────────┐             ┌──────────┐
  │ impl Foo │──────────────│ proc-    │─────────────│ Plugin   │
  │ {        │  #[hub_      │ macro    │  generates  │ Schema   │
  │   fn x() │  methods]    │ expand   │  schema()   │ JSON     │
  │ }        │              │          │  method     │          │
  └──────────┘              └──────────┘             └──────────┘
                            ┌──────────────────────────────────────┐
                            │           Synapse (Haskell)          │
                            │  Parses schema, emits IR             │
                            │  synapse --emit-ir                   │
                            └──────────────────────────────────────┘
                            ┌──────────────────────────────────────┐
                            │  plexus-codegen-typescript (Rust)    │
                            │  Consumes IR, generates TypeScript   │
                            └──────────────────────────────────────┘
                            ┌──────────────────────────────────────┐
                            │       TypeScript Client              │
                            │  Type-safe Plexus RPC calls          │
                            └──────────────────────────────────────┘
```

The pipeline is language-agnostic at the IR level. Adding Python support requires implementing a Python backend in `plexus-codegen-python`.

## Accessing Plexus RPC

### WebSocket Transport

```bash
# Start Substrate server
cargo run

# Connect via WebSocket
wscat -c ws://localhost:4444

# Call Plexus RPC methods
{"jsonrpc":"2.0","id":1,"method":"substrate.call","params":{"method":"echo.echo","params":{"message":"hello","count":3}}}

# Get Plexus RPC schemas
{"jsonrpc":"2.0","id":1,"method":"substrate.schema"}
{"jsonrpc":"2.0","id":1,"method":"substrate.call","params":{"method":"arbor.schema"}}
```

### MCP Bridge

Substrate exposes an MCP server that presents Plexus RPC methods as MCP tools using dot notation:

```
echo.echo(message, count)
arbor.tree_create(metadata)
cone.chat(name, prompt)
```

The MCP bridge automatically converts all registered Plexus RPC activation methods into callable MCP tools. Tool names mirror the Plexus RPC namespace structure directly.

### In-Process (Rust)

```rust
use substrate::{DynamicHub, activations::Echo};

let plexus = DynamicHub::new().register(Echo);

let mut stream = substrate.call(
    "echo.echo",
    json!({"message": "test", "count": 1})
).await?;

while let Some(item) = stream.next().await {
    println!("{:?}", item);
}
```

## Current State

| Component                     | Status  | Notes                                       |
|-------------------------------|---------|---------------------------------------------|
| hub-core                      | Stable  | Activation, DynamicHub, ChildRouter, schemas|
| hub-macro                     | Stable  | Streaming attribute works                   |
| synapse                       | Stable  | IR emission complete                        |
| plexus-codegen-typescript     | Partial | Types done, namespace generator pending     |
| Multi-hub (remote references) | Planned | Remote hub references not implemented       |

## Multi-Hub Vision

Current: All activations in-process, single DynamicHub instance.

Future: Plexus RPC hubs reference other hubs as activations via URL.

```
┌─────────────────┐          ┌─────────────────┐
│   Local Hub     │          │   Remote Hub    │
│  ┌───────────┐  │  HTTP/   │  ┌───────────┐  │
│  │ local.*   │  │  SSE     │  │ remote.*  │  │
│  └───────────┘  │◄────────►│  └───────────┘  │
│  ┌───────────┐  │          └─────────────────┘
│  │ remote@url│──┼──────────────────┘
│  │ (proxy)   │  │
│  └───────────┘  │
└─────────────────┘
```

Requirements for multi-hub Plexus RPC:
- Transport envelope for cross-hub calls
- Schema federation (remote schemas appear local)
- Streaming across network boundary
- Authentication/authorization

See `docs/architecture/16679517135570018559_multi-hub-transport-envelope.md`.

## Project Structure

```
src/
├── plexus/              # Re-exports from hub-core
├── activations/         # Substrate-specific Plexus RPC activations
│   ├── arbor/          # Conversation tree storage
│   ├── cone/           # Generic LLM orchestration
│   ├── claudecode/     # Claude Code CLI wrapper
│   ├── bash/           # Shell command execution
│   ├── changelog/      # Change tracking
│   ├── mustache/       # Template rendering
│   ├── echo/           # Example leaf activation
│   ├── health/         # Example minimal activation
│   └── solar/          # Example hub activation
├── mcp_bridge.rs       # MCP protocol adapter for Plexus RPC
└── main.rs             # Plexus RPC server entry point
```

## See Also

- `docs/architecture/16676565123400000000_plexus-rpc-ecosystem-naming.md` - Plexus RPC naming strategy
- `docs/architecture/16679477965835151615_hub-architecture-layering.md` - Detailed Plexus RPC architecture
- `docs/architecture/16679613932789736703_compiler-architecture.md` - Code generation pipeline
- `docs/architecture/16680807091363337727_introspective-rpc-protocol.md` - Plexus RPC protocol design
- `docs/architecture/16680343462706939647_schema-as-membrane.md` - Schema philosophy

## License

MIT