Expand description
§Stigmergy: Emergent Coordination through Shared Data Structures
Stigmergy is a biological concept describing how organisms coordinate their activities through modifications to their shared environment. Ants building trails, termites constructing mounds, and birds flocking all demonstrate stigmergic behavior - simple local actions that produce complex global coordination without central control.
This crate implements a stigmergic system for software, providing:
- Entity-Component Architecture: Entities are unique identifiers that can have multiple components attached, following ECS (Entity Component System) patterns
- Schema Validation: Components are validated against JSON schemas to ensure data integrity and consistency
- PostgreSQL Storage: All entities and components are persisted in PostgreSQL
- HTTP API: RESTful endpoints for managing entities and components
- Configuration Management: System configuration through frontmatter-delimited files
§Core Concepts
§Entities
Entities are 32-byte identifiers encoded as URL-safe base64 strings with an “entity:” prefix. They serve as unique handles in the system and can be randomly generated or explicitly specified.
§Components
Components are typed data structures that can be attached to entities. Each component has a Rust-style type path identifier (e.g., “Position”, “std::collections::HashMap”) and JSON data that must conform to a predefined schema.
§Validation
All component data is validated against JSON schemas that support:
- Primitive types (null, boolean, integer, number, string)
- Complex types (arrays, objects)
- Enumerations and union types via
oneOf - Nested structures with full recursion
§Persistence
All entities, components, and their relationships are stored in PostgreSQL with automatic timestamp tracking for creation and modification events.
§Architecture
The system follows a layered architecture:
┌─────────────────────────────────────────┐
│ HTTP API Layer (Axum routes) │
├─────────────────────────────────────────┤
│ Business Logic (Component operations) │
├─────────────────────────────────────────┤
│ PostgreSQL Database (Persistent storage)│
└─────────────────────────────────────────┘§Usage Examples
§Creating and Managing Entities
// Create a new entity with a specific ID
let entity = Entity::new([1u8; 32]);
let entity_str = entity.to_string(); // "entity:AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE"
// Parse an entity from its string representation
let parsed: Entity = entity_str.parse().unwrap();
assert_eq!(entity, parsed);
// Generate a random entity
let random_entity = Entity::random_url_safe().unwrap();§Component Schema Definition
Component schemas can be created manually or automatically generated using the derive macro:
// Manual schema definition
let component = Component::new("Position").unwrap();
let schema = json!({
"type": "object",
"properties": {
"x": { "type": "number" },
"y": { "type": "number" },
"z": { "type": "number" }
},
"required": ["x", "y"]
});
let definition = ComponentDefinition::new(component, schema);
// Validate component data
let valid_data = json!({"x": 1.0, "y": 2.0, "z": 3.0});
assert!(definition.validate_component_data(&valid_data).is_ok());§Automatic Schema Generation
Use the JsonSchemaDerive macro to automatically generate schemas for structs and enums:
// Define types with automatic schema generation
#[derive(stigmergy_derive::JsonSchema)]
struct Position {
x: f64,
y: f64,
z: Option<f64>,
}
#[derive(stigmergy_derive::JsonSchema)]
enum Status {
Active,
Inactive,
Pending,
}
#[derive(stigmergy_derive::JsonSchema)]
enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
}
// Generate schemas automatically
let position_schema = Position::json_schema();
let status_schema = Status::json_schema();
let shape_schema = Shape::json_schema();
// Unit enums become string enums
assert_eq!(status_schema["type"], "string");
assert!(status_schema["enum"].as_array().unwrap().len() == 3);
// Complex enums use oneOf patterns
assert!(shape_schema["oneOf"].is_array());Modules§
- cli_
utils - Command-line interface utilities for program termination and output formatting.
- commands
- Command-line interface command handlers.
- component_
utils - Component-specific utilities for validation and creation.
- http_
utils - HTTP client utilities for interacting with stigmergy services.
- sql
- PostgreSQL database operations for stigmergy.
Structs§
- Bid
- A complete bid expression with condition and value
- BidParser
- Main parser for bid expressions
- Component
- A component type identifier that follows Rust naming conventions.
- Component
Access - Represents a component and its access mode for a system.
- Component
Definition - A component definition that associates a component type with its JSON schema.
- Component
List Item - A component instance item used in list responses.
- Create
Component Request - Request structure for creating a new component instance.
- Create
Component Response - Response structure for successful component creation.
- Create
Entity Request - Request structure for creating a new entity.
- Create
Entity Response - Response structure for entity creation.
- Create
Invariant Request - Request structure for creating a new invariant.
- Create
Invariant Response - Response structure for invariant creation.
- Create
System From Markdown Request - Request structure for creating a system from markdown content.
- Create
System Response - Response structure for system creation.
- Entity
- A 32-byte entity identifier with URL-safe base64 string representation.
- GetInvariant
Response - Response structure for getting an invariant.
- InvariantID
- A 32-byte invariant identifier with URL-safe base64 string representation.
- Json
Schema Builder - A JSON schema representation that can be used for validation and documentation.
- Position
- Position information for error reporting
- System
- A system represents a Claude Code agent configuration with its associated metadata. Systems are identified by their name.
- System
Config - A parsed system configuration containing metadata and content.
- System
List Item - List item for systems (simplified view).
- System
Name - A strongly-typed system name identifier.
- System
Name Parse Error - Error returned when parsing an invalid system name.
- System
Parser - Parser for system configuration files with frontmatter and markdown content.
- Update
Invariant Request - Request structure for updating an invariant.
Enums§
- Access
Mode - Represents the access mode for a component in a system.
- BidParse
Error - Errors that can occur during bid parsing
- Binary
Operator - Binary operators with precedence information
- Data
Store Error - Errors that can occur during data store operations.
- Entity
Parse Error - Errors that can occur when parsing an Entity from a string.
- Evaluation
Error - Errors that can occur during bid evaluation
- Expression
- A parsed expression that can be evaluated
- InvariantID
Parse Error - Errors that can occur when parsing an InvariantID from a string.
- Parse
Error - Errors that can occur when parsing system configuration files.
- Unary
Operator - Unary operators
- Validation
Error - Errors that can occur during JSON schema validation.
Traits§
- Entity
Resolver - A trait for resolving keys into JSON entities. This allows the evaluation engine to work with normalized or relational data structures.
- Json
Schema - Trait for types that can generate their own JSON Schema
Functions§
- create_
component_ definition_ router - Creates the HTTP router for component definition endpoints.
- create_
component_ instance_ router - Creates an Axum router with component instance management endpoints.
- create_
entity_ router - Creates an Axum router with entity management endpoints.
- create_
invariant_ router - Creates an Axum router with invariant management endpoints.
- create_
system_ router - Creates an Axum router with system management endpoints.
- validate_
value - Validates a JSON value against a JSON schema.