Write a plugin
A plugin depends on the SDK alone, declares what it owns and emits, and changes state only through events.
Every demographic behaviour is a plugin, and every plugin depends on cohortix-sdk and on nothing else of the engine. The SDK defines the plugin.toml manifest types, the Plugin trait and the host traits that the kernel implements.
0.1 status
The SDK is versioned 0.y until release 1.0, and each 0.y minor release may break its public API. The full SDK, generated typed accessors and the cross-plugin checks come in later versions.
A minimal plugin
The test plugin test/ticker adds one to a counter for every person at every step, and records it as a Tick event:
use cohortix_sdk::{ColumnDecl, Event, EventDecl, HostError, PhaseHost, Plugin, PluginDecl};
struct Ticker;
impl Plugin for Ticker {
fn decl(&self) -> PluginDecl {
PluginDecl::new("p:test/ticker")
.contract("c:test/Tick")
.phase("ticks")
.owns(ColumnDecl::new("p:test/ticker.ticks", 0.0))
.emits(EventDecl::new("c:test/Tick.Tick").field("ticks"))
}
fn on_phase(&mut self, _phase: &str, host: &mut dyn PhaseHost) -> Result<(), HostError> {
for agent in host.agents() {
let old = host.read(agent, "p:test/ticker.ticks")?;
host.emit(
Event::new("c:test/Tick.Tick", agent)
.field("ticks", old + 1.0)
.set("p:test/ticker.ticks", old + 1.0),
)?;
}
Ok(())
}
}The plugin embeds its manifest with fn manifest(&self) -> Option<EmbeddedManifest> { Some(cohortix_sdk::embed_manifest!()) }, with plugin.toml at the crate root.
Names
Every name is canonical and carries its namespace:
| Kind | Form | Example |
|---|---|---|
| Plugin | p:<ns>/<plugin> | p:test/ticker |
| Contract | c:<ns>/<Contract> | c:test/Tick |
| Event | c:<ns>/<Contract>.<Event> | c:test/Tick.Tick |
| Owned column | p:<ns>/<plugin>.<column> | p:test/ticker.ticks |
| Decision type | c:<ns>/<Contract>.<decision> | c:test/Tick.uniform |
official/ and test/ are reserved namespaces.
What the host offers
- Reading.
readfor the plugin's own columns;birth_date,sex,alive,death_date,domain_status,territory_unit,age_yearsandage_monthsfor the kernel columns;viewfor another contract's state;kinfor mother and children. - Writing.
emitis the only write path. The kernel commits an event only if the plugin owns every column it sets and the event is in that column'sset_bylist. - Randomness.
draw(decision, agent)returns the person's next uniform in [0, 1) under a decision type of the plugin's own contract. There is no other randomness:rand,fastrandandgetrandomare banned from plugin crates. - Hazards.
hazard_probabilityconverts a hazard per year into a probability for the step, andcompetingresolves one owner's competing events with a single draw. - Links, requests and deadlines.
start_linkandend_linkfor owned link types,requestto send a request to another contract,set_deadlineto be called back after a delay. - Modulation.
modulateandmodulate_rowevaluate the plugin's own modulation points, where policies attach.
Listeners override Plugin::on_event, request handlers Plugin::on_request, and deadline owners Plugin::on_deadline.
Rules CI enforces on a plugin
- Transcendental math from
std(exp,ln,powfand the like) is forbidden; use the exact-pinnedlibm. - No dependency on the kernel or on another plugin.
- Any package under
plugins/must carry[lints.clippy] disallowed_methods = "forbid".
Versions and MSRV
The SDK is versioned on its own. A plugin built against SDK x.y works with x.z for z >= y, and deprecations are announced at least one minor release before removal. CI runs cargo-semver-checks on the SDK. The minimum supported Rust version of the SDK is 1.88.