Skip to content
Skip to Content

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:

ticker.rs
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:

KindFormExample
Pluginp:<ns>/<plugin>p:test/ticker
Contractc:<ns>/<Contract>c:test/Tick
Eventc:<ns>/<Contract>.<Event>c:test/Tick.Tick
Owned columnp:<ns>/<plugin>.<column>p:test/ticker.ticks
Decision typec:<ns>/<Contract>.<decision>c:test/Tick.uniform

official/ and test/ are reserved namespaces.

What the host offers

  • Reading. read for the plugin's own columns; birth_date, sex, alive, death_date, domain_status, territory_unit, age_years and age_months for the kernel columns; view for another contract's state; kin for mother and children.
  • Writing. emit is 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's set_by list.
  • 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, fastrand and getrandom are banned from plugin crates.
  • Hazards. hazard_probability converts a hazard per year into a probability for the step, and competing resolves one owner's competing events with a single draw.
  • Links, requests and deadlines. start_link and end_link for owned link types, request to send a request to another contract, set_deadline to be called back after a delay.
  • Modulation. modulate and modulate_row evaluate 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, powf and the like) is forbidden; use the exact-pinned libm.
  • 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.

On this page

You are reading the docs for 0.1. Keys can change between versions: use the version that matches cohortix --version.