Make Odin Rust dependencies deployable

This commit is contained in:
Meta Vens 2026-06-11 08:20:25 +01:00
commit 616f047842
50 changed files with 8227 additions and 9 deletions

View file

@ -6,6 +6,11 @@ members = [
"crates/odin-core",
]
resolver = "3"
exclude = [
"vendor/cultcache-rs",
"vendor/cultmesh-rs",
"vendor/cultnet-rs",
]
[workspace.package]
edition = "2024"

View file

@ -11,5 +11,5 @@ path = "src/main.rs"
[dependencies]
anyhow = "1"
cultmesh-rs = { path = "E:/Projects/EpiphanyAgent/vendor/cultmesh-rs" }
cultmesh-rs = { path = "../../vendor/cultmesh-rs" }
odin-core = { path = "../odin-core" }

View file

@ -11,5 +11,5 @@ path = "src/main.rs"
[dependencies]
anyhow = "1"
cultmesh-rs = { path = "E:/Projects/EpiphanyAgent/vendor/cultmesh-rs" }
cultmesh-rs = { path = "../../vendor/cultmesh-rs" }
odin-core = { path = "../odin-core" }

View file

@ -11,10 +11,10 @@ path = "src/lib.rs"
[dependencies]
anyhow = "1"
cultcache-rs = { path = "E:/Projects/EpiphanyAgent/vendor/cultcache-rs" }
cultmesh-rs = { path = "E:/Projects/EpiphanyAgent/vendor/cultmesh-rs" }
cultcache-rs = { path = "../../vendor/cultcache-rs" }
cultmesh-rs = { path = "../../vendor/cultmesh-rs" }
serde_json = "1"
cultnet-rs = { path = "E:/Projects/EpiphanyAgent/vendor/cultnet-rs" }
cultnet-rs = { path = "../../vendor/cultnet-rs" }
serde = { version = "1", features = ["derive"] }
[dev-dependencies]

View file

@ -30,20 +30,37 @@ Get-CimInstance Win32_Process |
ForEach-Object { taskkill.exe /PID `$_.ProcessId /T /F | Out-Null }
`$muninnDir = Split-Path -Parent "$MuninnExe"
`$launcher = Join-Path `$muninnDir "activate-raven-av-srt.cmd"
`$psLauncher = Join-Path `$muninnDir "activate-raven-av-srt.ps1"
`$pidPath = Join-Path "$LogRoot" "muninn-activate.pid"
`$obsArgs = ""
if (-not [bool]::Parse("$($NoObsTarget.IsPresent)") -and -not ("$TargetHost" -eq "$ObsTargetHost" -and $Port -eq $ObsPort)) {
`$obsArgs = " --obs-target-host ""$ObsTargetHost"" --obs-port $ObsPort"
} else {
`$obsArgs = " --no-obs-target"
}
`$arguments = @("activate", "--store", "$StorePath", "--host", "raven", "--stream", "muninn.raven.av.srt", "--target-host", "$TargetHost", "--port", "$Port")
if (`$obsArgs -like " --no-obs-target") {
`$arguments += "--no-obs-target"
} else {
`$arguments += @("--obs-target-host", "$ObsTargetHost", "--obs-port", "$ObsPort")
}
`$arguments += @("--audio-device", "$AudioDevice", "--ffmpeg", "$Ffmpeg", "--loopback-script", "$LoopbackScript", "--log-root", "$LogRoot")
`$encodedArguments = (`$arguments | ConvertTo-Json -Compress)
`$psLines = @(
"`$ErrorActionPreference = ""Stop""",
"`$arguments = '$encodedArguments' | ConvertFrom-Json",
"`$process = Start-Process -FilePath ""$MuninnExe"" -ArgumentList `$arguments -WorkingDirectory ""`$muninnDir"" -WindowStyle Hidden -PassThru -RedirectStandardOutput ""$LogRoot\muninn-activate.out.log"" -RedirectStandardError ""$LogRoot\muninn-activate.err.log""",
"`$process.Id | Set-Content -Encoding ASCII -LiteralPath ""`$pidPath"""
)
Set-Content -LiteralPath `$psLauncher -Value `$psLines -Encoding ASCII
`$lines = @(
"@echo off",
"cd /d ""`$muninnDir""",
"""$MuninnExe"" activate --store ""$StorePath"" --host raven --stream muninn.raven.av.srt --target-host ""$TargetHost"" --port $Port`$obsArgs --audio-device ""$AudioDevice"" --ffmpeg ""$Ffmpeg"" --loopback-script ""$LoopbackScript"" --log-root ""$LogRoot"" 1>>""$LogRoot\muninn-activate.out.log"" 2>>""$LogRoot\muninn-activate.err.log"""
"powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File ""`$psLauncher"""
)
Set-Content -LiteralPath `$launcher -Value `$lines -Encoding ASCII
cmd /c "schtasks /Delete /TN GameCult-Muninn-Activate /F 2>NUL"
cmd /c schtasks /Create /TN GameCult-Muninn-Activate /SC ONCE /ST 23:59 /TR `$launcher /IT /RL HIGHEST /F
cmd /c schtasks /Create /TN GameCult-Muninn-Activate /SC ONCE /ST 23:59 /TR `$launcher /RL HIGHEST /F
cmd /c schtasks /Run /TN GameCult-Muninn-Activate
"@

View file

@ -22,14 +22,22 @@ New-Item -ItemType Directory -Force -Path "$LogRoot" | Out-Null
New-Item -ItemType Directory -Force -Path (Split-Path -Parent "$StorePath") | Out-Null
`$muninnDir = Split-Path -Parent "$MuninnExe"
`$launcher = Join-Path `$muninnDir "start-muninn-serve.cmd"
`$psLauncher = Join-Path `$muninnDir "start-muninn-serve.ps1"
`$pidPath = Join-Path "$LogRoot" "muninn-serve.pid"
`$psLines = @(
"`$ErrorActionPreference = ""Stop""",
"`$process = Start-Process -FilePath ""$MuninnExe"" -ArgumentList @(""serve"", ""--store"", ""$StorePath"", ""--log-root"", ""$LogRoot"", ""--host"", ""raven"", ""--interval-seconds"", ""15"") -WorkingDirectory ""`$muninnDir"" -WindowStyle Hidden -PassThru -RedirectStandardOutput ""$LogRoot\muninn-serve.out.log"" -RedirectStandardError ""$LogRoot\muninn-serve.err.log""",
"`$process.Id | Set-Content -Encoding ASCII -LiteralPath ""`$pidPath"""
)
Set-Content -LiteralPath `$psLauncher -Value `$psLines -Encoding ASCII
`$lines = @(
"@echo off",
"cd /d ""`$muninnDir""",
"""$MuninnExe"" serve --store ""$StorePath"" --log-root ""$LogRoot"" --host raven --interval-seconds 15 1>>""$LogRoot\muninn-serve.out.log"" 2>>""$LogRoot\muninn-serve.err.log"""
"powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File ""`$psLauncher"""
)
Set-Content -LiteralPath `$launcher -Value `$lines -Encoding ASCII
cmd /c "schtasks /Delete /TN GameCult-Muninn /F 2>NUL"
cmd /c schtasks /Create /TN GameCult-Muninn /SC ONCE /ST 23:59 /TR `$launcher /IT /RL HIGHEST /F
cmd /c schtasks /Create /TN GameCult-Muninn /SC ONCE /ST 23:59 /TR `$launcher /RL HIGHEST /F
cmd /c schtasks /Run /TN GameCult-Muninn
"@

2
vendor/cultcache-rs/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

27
vendor/cultcache-rs/Cargo.toml vendored Normal file
View file

@ -0,0 +1,27 @@
[package]
name = "cultcache-rs"
version = "0.1.0"
edition = "2024"
publish = false
[workspace]
members = ["cultcache-rs-derive"]
[lib]
name = "cultcache_rs"
path = "src/lib.rs"
[dependencies]
anyhow = "1"
chrono = { version = "0.4", features = ["serde"] }
cultcache-rs-derive = { path = "cultcache-rs-derive" }
fs2 = "0.4"
rmp-serde = "1.3"
serde = { version = "1", features = ["derive"] }
serde_bytes = "0.11"
serde_json = "1"
uuid = { version = "1", features = ["v4"] }
[dev-dependencies]
pretty_assertions = "1.4"
tempfile = "3"

332
vendor/cultcache-rs/README.md vendored Normal file
View file

@ -0,0 +1,332 @@
# CultCache Rust
`cultcache-rs` is the Rust port of the useful part of GameCult's CultCache:
a polymorphism-aware in-memory cache that remembers through pluggable backing
stores.
Consumer code should think in domain types:
```rust
let player = cache.get_required::<PlayerData>("player:meta")?;
cache.put("player:meta", &player)?;
```
The persistence layer should deal with envelopes, routing, MessagePack payload
bytes, and schema identity. Application code should not paw at loose JSON files
like a sad little bureaucrat with a clipboard.
## What It Is
CultCache is a domain cache with persistence adapters.
- `CultCache` is the query and mutation surface.
- Domain structs implement `DatabaseEntry`.
- Entries are stored behind a `type::key` identity, so multiple entry types
can share a logical key without colliding.
- Each payload is encoded directly from the known `DatabaseEntry` type into
MessagePack bytes.
- Backing stores are adapters, not the public data model.
- Writes persist to the resolved backing store before the in-memory cache is
updated.
- Type-specific backing stores beat generic backing stores.
- `SingleFileMessagePackBackingStore` is the first concrete store and guards
file access with a sidecar lock file.
This is not an ORM, not a database, and not distributed consensus in a novelty
hat. If multiple processes write the same backing file, use an external lock or
a coordinator.
## Current API
```rust
use cultcache_rs::{CultCache, DatabaseEntry, SingleFileMessagePackBackingStore};
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "settings")]
struct Settings {
#[cultcache(key = 0)]
theme: String,
#[cultcache(key = 1, default)]
retries: u32,
}
let mut cache = CultCache::new();
cache.register_entry_type::<Settings>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new("cache.cc"));
cache.pull_all_backing_stores()?;
cache.put("app", &Settings {
theme: "ash".to_string(),
retries: 3,
})?;
let settings = cache.get_required::<Settings>("app")?;
# Ok::<(), anyhow::Error>(())
```
For a closed set of entries, generate a registry instead of repeating yourself:
```rust
use cultcache_rs::cultcache_registry;
cultcache_registry!(GameCultEntries {
Settings,
PlayerData,
});
let mut cache = CultCache::new();
cache.register_registry(GameCultEntries)?;
```
The important part is that callers retrieve domain values directly:
```rust
let settings: Settings = cache.get_required("app")?;
let all_settings: Vec<Settings> = cache.get_all()?;
```
The cache is the ergonomic surface. The backing store is the memory prosthetic.
## How This Maps From Original CultCache
The original C# CultCache relies on runtime reflection:
- cacheable models inherit from `DatabaseEntry`
- the cache scans child classes at startup
- `Get<T>(id)` returns a `T`
- optional `INamedEntry` enables name lookup
- optional field/property indexes are registered by member name
- backing stores pull/push/delete `DatabaseEntry` values
Rust does not have C#-style assembly scanning or runtime subclass discovery.
The closest honest Rust translation is:
- domain structs implement a marker trait, `DatabaseEntry`
- the derive macro generates a positional array formatter from explicit integer
field slots
- the envelope carries the polymorphic type discriminator
- unknown persisted type ids fail closed instead of constructing arbitrary
runtime types
- a generated registry should eventually replace repetitive manual
registration
Manual `register_entry_type::<T>()` is still accepted, but entry identity no
longer needs a hand-written impl: `#[derive(DatabaseEntry)]` declares it beside
the domain type. `cultcache_registry!` provides the current generated resolver
surface for a closed entry set.
## Intended Rust Ergonomics
The target user-facing shape should be:
```rust
#[derive(Clone, DatabaseEntry)]
#[cultcache(type = "player")]
pub struct PlayerData {
#[cultcache(key = 0)]
pub id: String,
#[cultcache(key = 1)]
pub name: String,
#[cultcache(key = 2)]
pub faction: String,
}
let mut cache = CultCache::builder()
.with_generated_entries(game_entries())
.with_generic_store(SingleFileMessagePackBackingStore::new("cache.cc"))
.build()?;
cache.pull_all_backing_stores()?;
let player = cache.get_required::<PlayerData>("player:ari")?;
```
The current `cultcache_registry!` macro is deliberately simple. The remaining
ergonomic endpoint is automatic inventory or build-script generation so users
do not have to maintain the registry list by hand.
The registry is the Rust equivalent of C# reflection and MessagePack resolver
setup. It should know:
- entry type id
- generated MessagePack/serde formatter path
- Rust type name / schema name
- optional store domain route
- optional name key extractor
- optional secondary indexes
- optional global singleton marker
In other words: code generation should restore the original CultCache feeling
without pretending Rust has runtime reflection hiding under the floorboards.
## Current Surface
- `CultCache::new`
- `register_entry_type::<T>`
- `add_generic_backing_store`
- `add_backing_store`
- `pull_all_backing_stores`
- `get::<T>`
- `get_required::<T>`
- `get_envelope::<T>`
- `get_required_envelope::<T>`
- `get_all::<T>`
- `put::<T>`
- `put_envelope::<T>`
- `update::<T>`
- `delete::<T>`
- `snapshot`
- `SingleFileMessagePackBackingStore`
## Backing Store Routing
Generic store:
```rust
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new("cache.cc"));
```
Type-specific store:
```rust
cache.add_backing_store(
SingleFileMessagePackBackingStore::new("players.cc"),
["player"],
);
```
When writing a `PlayerData`, the cache checks type-specific stores first. If none
match, it writes to the first generic store. Later matching stores are mirrors.
This mirrors the C# behavior:
- specific domain stores own their domain
- the first generic store is the primary generic write target
- later generic stores mirror writes
- this is not multi-master
## Persistence Semantics
`put` persists before mutating the in-memory cache. If persistence fails, the
cache does not pretend the write succeeded.
The envelope is MessagePack, and the payload inside each envelope is also raw
MessagePack bytes encoded from the registered concrete `DatabaseEntry` type.
That avoids the old bootstrap path where payloads were normalized through
`serde_json::Value`.
For bit-compatible neighbors, that also enables a real fast lane:
- `get_envelope::<T>()` exports the canonical persisted bytes for a typed entry
- `put_envelope::<T>()` ingests the same envelope into another cache instance
without re-encoding the payload first
It still decodes once for validation and typed reads. We are not pretending one
`Vec<u8>` became metaphysically zero-copy because we believed in it harder. But
the stupid decode/re-encode loop is gone.
The `DatabaseEntry` derive does not trust Rust source field order as the durable
schema. Every persisted member must declare a stable integer slot:
```rust
#[derive(Clone, DatabaseEntry)]
#[cultcache(type = "player")]
pub struct PlayerData {
#[cultcache(key = 0)]
pub id: String,
#[cultcache(key = 1)]
pub name: String,
#[cultcache(key = 2, default)]
pub level: u32,
}
```
The derive emits a tuple/array formatter. Gaps serialize as nil. Missing fields
marked `default` use `Default::default()` when older payloads are read. Deleted
field slots should stay reserved until an explicit store migration rewrites the
data.
The single-file MessagePack store rewrites an atomic snapshot. That is a sane
starting point for small typed state surfaces, settings, Epiphany agent memory,
heartbeat state, and other compact control-plane data. Large corpora should use
a sharded store or a real database instead of asking one file to become a
warehouse and then acting wounded when physics invoices us.
The single-file store uses a sidecar lock file for shared reads and exclusive
writes. That protects ordinary multi-process access to the same file. It is
still not a multi-master replication protocol, and it is not a substitute for a
coordinator when higher-level write ordering matters.
## Near-Term Ergonomic Improvements
1. **Derive macro**
- `#[derive(DatabaseEntry)]`
- `#[cultcache(type = "...")]`
- `#[cultcache(key = N)]` on every persisted field
- tuple/array formatter generation landed
- optional `#[cultcache(name)]`, `#[cultcache(index)]`, and
`#[cultcache(global)]` are still future work
2. **Generated registry**
- a `CultCacheRegistry` trait
- `cache.register_registry(GameCultEntries)`
- basic macro registry landed
- automatic inventory/build-script generation and store routes are future work
3. **Name and index lookups**
- `cache.get_by_name::<T>("Potion")`
- `cache.get_by_index::<T>("faction", "Lucent")`
- generated extractors instead of reflection strings
4. **Schema/version metadata**
- schema version in the envelope or payload metadata
- explicit migration hooks
- refusal on unknown persisted entry types unless a migration/resolver is
installed
5. **Projection helpers**
- JSON projection for review and git diffs
- vector stripping / large-field elision for agent memory surfaces
## Why Not Skip Registration Immediately?
Rust can only deserialize polymorphic data into concrete types if something
maps the persisted `type` discriminator to the Rust type. In C#, reflection and
MessagePack resolvers can discover a closed `DatabaseEntry` inheritance tree at
runtime and route it through known formatters. In Rust, that map has to come from
somewhere:
- explicit registration
- a generated registry
- a macro inventory crate
- a hand-written resolver
Explicit registration is the simplest honest implementation. Generated registry
is the ergonomic destination. Hand-written resolver tables are the punishment
we give ourselves if we get lazy.
## Security Model
CultCache should never parse arbitrary structured data into arbitrary runtime
types. It only accepts persisted envelopes whose `type` discriminator resolves
to a known `DatabaseEntry` type registered in the cache or generated registry.
That is the Rust version of the original trick:
- C# refuses to store objects outside the `DatabaseEntry` root type.
- Runtime-visible `DatabaseEntry` subclasses get dedicated formatters.
- Unknown or unregistered types are rejected.
- MessagePack payload bytes deserialize through known concrete formatters, not
an open-world type loader or generic structured-value bridge.
The derive/registry work should preserve that shape. Its job is to remove
manual registration ceremony, not to loosen the closed-world boundary.
## License
Private GameCult infrastructure for now. Public packaging can wait until the
API grows enough teeth to deserve strangers.

View file

@ -0,0 +1,13 @@
[package]
name = "cultcache-rs-derive"
version = "0.1.0"
edition = "2024"
publish = false
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["derive", "parsing"] }

View file

@ -0,0 +1,243 @@
use proc_macro::TokenStream;
use quote::format_ident;
use quote::quote;
use syn::Data;
use syn::DeriveInput;
use syn::Fields;
use syn::LitInt;
use syn::LitStr;
use syn::Type;
use syn::parse_macro_input;
#[proc_macro_derive(DatabaseEntry, attributes(cultcache))]
pub fn derive_database_entry(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let ident = input.ident.clone();
let mut entry_type: Option<LitStr> = None;
let mut schema_name: Option<LitStr> = None;
for attribute in input
.attrs
.iter()
.filter(|attr| attr.path().is_ident("cultcache"))
{
let parse_result = attribute.parse_nested_meta(|meta| {
if meta.path.is_ident("type") {
entry_type = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("schema") {
schema_name = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(meta.error("unsupported cultcache attribute"))
}
});
if let Err(error) = parse_result {
return error.to_compile_error().into();
}
}
let Some(entry_type) = entry_type else {
return syn::Error::new_spanned(
ident,
"DatabaseEntry derive requires #[cultcache(type = \"...\")]",
)
.to_compile_error()
.into();
};
let schema_name = schema_name.unwrap_or_else(|| LitStr::new(&ident.to_string(), ident.span()));
let fields = match input.data {
Data::Struct(data) => match data.fields {
Fields::Named(fields) => fields.named,
_ => {
return syn::Error::new_spanned(
ident,
"DatabaseEntry derive requires a named struct",
)
.to_compile_error()
.into();
}
},
_ => {
return syn::Error::new_spanned(ident, "DatabaseEntry derive requires a struct")
.to_compile_error()
.into();
}
};
let mut slots: Vec<CultCacheSlot> = Vec::new();
for field in fields {
let Some(field_ident) = field.ident else {
return syn::Error::new_spanned(field.ty, "DatabaseEntry derive requires named fields")
.to_compile_error()
.into();
};
let mut key: Option<usize> = None;
let mut default = false;
for attribute in field
.attrs
.iter()
.filter(|attr| attr.path().is_ident("cultcache"))
{
let parse_result = attribute.parse_nested_meta(|meta| {
if meta.path.is_ident("key") {
let value: LitInt = meta.value()?.parse()?;
key = Some(value.base10_parse()?);
Ok(())
} else if meta.path.is_ident("default") {
default = true;
Ok(())
} else {
Err(meta.error("unsupported field cultcache attribute"))
}
});
if let Err(error) = parse_result {
return error.to_compile_error().into();
}
}
let Some(key) = key else {
return syn::Error::new_spanned(
field_ident,
"DatabaseEntry fields require #[cultcache(key = N)]",
)
.to_compile_error()
.into();
};
if slots.iter().any(|slot| slot.key == key) {
return syn::Error::new_spanned(
field_ident,
format!("duplicate DatabaseEntry slot {key}"),
)
.to_compile_error()
.into();
}
slots.push(CultCacheSlot {
key,
ident: field_ident,
ty: field.ty,
default,
});
}
slots.sort_by_key(|slot| slot.key);
let max_slot = slots.iter().map(|slot| slot.key).max().unwrap_or(0);
let serialize_elements = (0..=max_slot).map(|slot| {
if let Some(field) = slots.iter().find(|field| field.key == slot) {
let field_ident = &field.ident;
quote! {
tuple.serialize_element(&self.#field_ident)?;
}
} else {
quote! {
tuple.serialize_element(&())?;
}
}
});
let option_declarations = slots.iter().map(|field| {
let variable = format_ident!("slot_{}", field.key);
let ty = &field.ty;
quote! {
let mut #variable: ::std::option::Option<#ty> = ::std::option::Option::None;
}
});
let deserialize_match_arms = (0..=max_slot).map(|slot| {
if let Some(field) = slots.iter().find(|field| field.key == slot) {
let variable = format_ident!("slot_{}", field.key);
let ty = &field.ty;
quote! {
#slot => {
#variable = seq.next_element::<#ty>()?;
}
}
} else {
quote! {
#slot => {
let _ = seq.next_element::<::serde::de::IgnoredAny>()?;
}
}
}
});
let build_fields = slots.iter().map(|field| {
let field_ident = &field.ident;
let variable = format_ident!("slot_{}", field.key);
if field.default {
quote! {
#field_ident: #variable.unwrap_or_default()
}
} else {
let field_name = field_ident.to_string();
quote! {
#field_ident: #variable.ok_or_else(|| ::serde::de::Error::missing_field(#field_name))?
}
}
});
quote! {
impl ::cultcache_rs::DatabaseEntry for #ident {
const TYPE: &'static str = #entry_type;
const SCHEMA_NAME: &'static str = #schema_name;
}
impl ::serde::Serialize for #ident {
fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
use ::serde::ser::SerializeTuple;
let mut tuple = serializer.serialize_tuple(#max_slot + 1)?;
#(#serialize_elements)*
tuple.end()
}
}
impl<'de> ::serde::Deserialize<'de> for #ident {
fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
struct CultCacheVisitor;
impl<'de> ::serde::de::Visitor<'de> for CultCacheVisitor {
type Value = #ident;
fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
formatter.write_str(concat!("a CultCache array for ", stringify!(#ident)))
}
fn visit_seq<A>(self, mut seq: A) -> ::std::result::Result<Self::Value, A::Error>
where
A: ::serde::de::SeqAccess<'de>,
{
#(#option_declarations)*
let mut index = 0usize;
while index <= #max_slot {
match index {
#(#deserialize_match_arms)*
_ => unreachable!(),
}
index += 1;
}
while let ::std::option::Option::Some(_) = seq.next_element::<::serde::de::IgnoredAny>()? {}
Ok(#ident {
#(#build_fields),*
})
}
}
deserializer.deserialize_tuple(#max_slot + 1, CultCacheVisitor)
}
}
}
.into()
}
struct CultCacheSlot {
key: usize,
ident: syn::Ident,
ty: Type,
default: bool,
}

View file

@ -0,0 +1,121 @@
use anyhow::Result;
use anyhow::anyhow;
use cultcache_rs::CultCache;
use cultcache_rs::DatabaseEntry;
use cultcache_rs::SingleFileMessagePackBackingStore;
use serde_json::json;
use std::collections::BTreeMap;
const INTEROP_TYPE: &str = "cultcache.interop-note";
const INTEROP_SCHEMA_VERSION: &str = "cultcache.interop_note.v1";
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "cultcache.interop-note", schema = "CultCacheInteropNote")]
struct CultCacheInteropNote {
#[cultcache(key = 0)]
schema_version: String,
#[cultcache(key = 1)]
document_id: String,
#[cultcache(key = 2)]
author_runtime_id: String,
#[cultcache(key = 3)]
title: String,
#[cultcache(key = 4)]
body: String,
#[cultcache(key = 5, default)]
tags: Vec<String>,
}
fn main() -> Result<()> {
let mut args = std::env::args().skip(1);
let mode = args
.next()
.ok_or_else(|| anyhow!("expected mode: write | read"))?;
let options = parse_args(args.collect());
let file = require_arg(&options, "file")?;
match mode.as_str() {
"write" => write_note(file, require_arg(&options, "runtime-id")?)?,
"read" => read_note(file)?,
_ => return Err(anyhow!("unknown mode {mode}")),
}
Ok(())
}
fn write_note(file: &str, runtime_id: &str) -> Result<()> {
let mut cache = build_cache(file)?;
cache.pull_all_backing_stores()?;
let note = CultCacheInteropNote {
schema_version: INTEROP_SCHEMA_VERSION.to_string(),
document_id: format!("note:{runtime_id}"),
author_runtime_id: runtime_id.to_string(),
title: format!("{runtime_id} wrote a CultCache note"),
body: "The v1 store format is the contract.".to_string(),
tags: vec![
runtime_id.to_string(),
"rust".to_string(),
"interop".to_string(),
],
};
cache.put(&note.document_id, &note)?;
print_note(&note)
}
fn read_note(file: &str) -> Result<()> {
let mut cache = build_cache(file)?;
cache.pull_all_backing_stores()?;
let notes = cache.get_all::<CultCacheInteropNote>()?;
let note = notes
.first()
.ok_or_else(|| anyhow!("no {INTEROP_TYPE} records found"))?;
print_note(note)
}
fn build_cache(file: &str) -> Result<CultCache> {
let mut cache = CultCache::new();
cache.register_entry_type::<CultCacheInteropNote>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(file));
Ok(cache)
}
fn print_note(note: &CultCacheInteropNote) -> Result<()> {
println!(
"{}",
serde_json::to_string(&json!({
"schemaVersion": note.schema_version,
"documentId": note.document_id,
"authorRuntimeId": note.author_runtime_id,
"title": note.title,
"body": note.body,
"tags": note.tags,
}))?
);
Ok(())
}
fn parse_args(args: Vec<String>) -> BTreeMap<String, String> {
let mut parsed = BTreeMap::new();
let mut index = 0;
while index < args.len() {
let token = &args[index];
if !token.starts_with("--") {
index += 1;
continue;
}
let name = token.trim_start_matches("--").to_string();
let value = args
.get(index + 1)
.cloned()
.unwrap_or_else(|| panic!("missing value for --{name}"));
parsed.insert(name, value);
index += 2;
}
parsed
}
fn require_arg<'a>(options: &'a BTreeMap<String, String>, name: &str) -> Result<&'a str> {
options
.get(name)
.map(String::as_str)
.ok_or_else(|| anyhow!("missing required argument --{name}"))
}

884
vendor/cultcache-rs/src/lib.rs vendored Normal file
View file

@ -0,0 +1,884 @@
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs;
use std::fs::File;
use std::fs::OpenOptions;
use std::path::Path;
use std::path::PathBuf;
extern crate self as cultcache_rs;
pub use cultcache_rs_derive::DatabaseEntry;
pub trait DatabaseEntry: Serialize + DeserializeOwned + Clone + Send + 'static {
const TYPE: &'static str;
const SCHEMA_NAME: &'static str = "DatabaseEntry";
}
pub trait CultCacheRegistry {
fn register_entries(&self, cache: &mut CultCache) -> Result<()>;
}
#[macro_export]
macro_rules! cultcache_registry {
($name:ident { $($entry:ty),* $(,)? }) => {
#[derive(Clone, Copy, Debug, Default)]
pub struct $name;
impl $crate::CultCacheRegistry for $name {
fn register_entries(&self, cache: &mut $crate::CultCache) -> ::anyhow::Result<()> {
$(
cache.register_entry_type::<$entry>()?;
)*
Ok(())
}
}
};
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CultCacheEnvelope {
pub key: String,
#[serde(rename = "type")]
pub r#type: String,
#[serde(with = "serde_bytes")]
pub payload: Vec<u8>,
pub stored_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub schema_id: Option<String>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct PersistedStoreSnapshot(
String,
Vec<PersistedSchemaCatalogEntry>,
Vec<PersistedRecord>,
);
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct PersistedSchemaCatalogEntry(
String,
String,
String,
String,
String,
Vec<String>,
Vec<PersistedSchemaCatalogMember>,
);
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct PersistedSchemaCatalogMember(
u32,
String,
String,
bool,
bool,
Option<String>,
bool,
Option<String>,
);
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct PersistedRecord(
String,
String,
String,
#[serde(with = "serde_bytes")] Vec<u8>,
);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PushAllOptions {
pub soft: bool,
}
pub trait CacheBackingStore: Send {
fn pull_all(&self) -> Result<Vec<CultCacheEnvelope>>;
fn push(&mut self, entry: &CultCacheEnvelope) -> Result<()>;
fn delete(&mut self, entry: &CultCacheEnvelope) -> Result<()>;
fn push_all(&mut self, entries: &[CultCacheEnvelope], _options: PushAllOptions) -> Result<()> {
let existing = self.pull_all()?;
for entry in existing {
self.delete(&entry)?;
}
for entry in entries {
self.push(entry)?;
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SingleFileMessagePackBackingStore {
path: PathBuf,
}
impl SingleFileMessagePackBackingStore {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self { path: path.into() }
}
pub fn path(&self) -> &Path {
&self.path
}
fn read_all_unlocked(&self) -> Result<Vec<CultCacheEnvelope>> {
if !self.path.exists() {
return Ok(Vec::new());
}
let bytes = fs::read(&self.path)
.with_context(|| format!("failed to read {}", self.path.display()))?;
if bytes.is_empty() {
return Ok(Vec::new());
}
decode_store_snapshot(&bytes)
.or_else(|_| rmp_serde::from_slice(&bytes))
.with_context(|| format!("failed to decode MessagePack {}", self.path.display()))
}
fn write_all_unlocked(&self, entries: &[CultCacheEnvelope]) -> Result<()> {
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent)
.with_context(|| format!("failed to create {}", parent.display()))?;
}
let bytes = rmp_serde::to_vec(&encode_store_snapshot(entries))
.context("failed to encode MessagePack")?;
let tmp_path = temporary_path_for(&self.path);
fs::write(&tmp_path, bytes)
.with_context(|| format!("failed to write {}", tmp_path.display()))?;
if self.path.exists() {
fs::remove_file(&self.path)
.with_context(|| format!("failed to replace {}", self.path.display()))?;
}
fs::rename(&tmp_path, &self.path).with_context(|| {
format!(
"failed to move {} to {}",
tmp_path.display(),
self.path.display()
)
})?;
Ok(())
}
fn with_shared_lock<T>(&self, action: impl FnOnce() -> Result<T>) -> Result<T> {
let lock = self.open_lock_file()?;
fs2::FileExt::lock_shared(&lock)
.with_context(|| format!("failed to lock {}", self.lock_path().display()))?;
let result = action();
fs2::FileExt::unlock(&lock)
.with_context(|| format!("failed to unlock {}", self.lock_path().display()))?;
result
}
fn with_exclusive_lock<T>(&self, action: impl FnOnce() -> Result<T>) -> Result<T> {
let lock = self.open_lock_file()?;
fs2::FileExt::lock_exclusive(&lock)
.with_context(|| format!("failed to lock {}", self.lock_path().display()))?;
let result = action();
fs2::FileExt::unlock(&lock)
.with_context(|| format!("failed to unlock {}", self.lock_path().display()))?;
result
}
fn open_lock_file(&self) -> Result<File> {
let lock_path = self.lock_path();
if let Some(parent) = lock_path.parent() {
fs::create_dir_all(parent)
.with_context(|| format!("failed to create {}", parent.display()))?;
}
OpenOptions::new()
.create(true)
.read(true)
.truncate(false)
.write(true)
.open(&lock_path)
.with_context(|| format!("failed to open {}", lock_path.display()))
}
fn lock_path(&self) -> PathBuf {
let mut lock_name = self
.path
.file_name()
.map(|value| value.to_os_string())
.unwrap_or_else(|| "cultcache.cc".into());
lock_name.push(".lock");
self.path.with_file_name(lock_name)
}
}
impl CacheBackingStore for SingleFileMessagePackBackingStore {
fn pull_all(&self) -> Result<Vec<CultCacheEnvelope>> {
self.with_shared_lock(|| self.read_all_unlocked())
}
fn push(&mut self, entry: &CultCacheEnvelope) -> Result<()> {
self.with_exclusive_lock(|| {
let mut entries = self.read_all_unlocked()?;
entries.retain(|candidate| entry_id(candidate) != entry_id(entry));
entries.push(entry.clone());
entries.sort_by_key(entry_id);
self.write_all_unlocked(&entries)
})
}
fn delete(&mut self, entry: &CultCacheEnvelope) -> Result<()> {
self.with_exclusive_lock(|| {
let mut entries = self.read_all_unlocked()?;
entries.retain(|candidate| entry_id(candidate) != entry_id(entry));
self.write_all_unlocked(&entries)
})
}
fn push_all(&mut self, entries: &[CultCacheEnvelope], _options: PushAllOptions) -> Result<()> {
self.with_exclusive_lock(|| {
let mut entries = entries.to_vec();
entries.sort_by_key(entry_id);
self.write_all_unlocked(&entries)
})
}
}
struct CultCacheStoreRegistration {
store: Box<dyn CacheBackingStore>,
types: BTreeSet<String>,
}
pub struct CultCache {
definitions: BTreeMap<String, &'static str>,
entries: BTreeMap<String, CultCacheEnvelope>,
stores: Vec<CultCacheStoreRegistration>,
}
impl CultCache {
pub fn new() -> Self {
Self {
definitions: BTreeMap::new(),
entries: BTreeMap::new(),
stores: Vec::new(),
}
}
pub fn register_entry_type<T: DatabaseEntry>(&mut self) -> Result<()> {
if T::TYPE.trim().is_empty() {
return Err(anyhow!(
"CultCache entry types must declare a non-empty type"
));
}
if let Some(existing_schema) = self.definitions.get(T::TYPE)
&& *existing_schema != T::SCHEMA_NAME
{
return Err(anyhow!(
"CultCache already has a different definition registered for type {:?}",
T::TYPE
));
}
self.definitions.insert(T::TYPE.to_string(), T::SCHEMA_NAME);
Ok(())
}
pub fn register_document_type<T: DatabaseEntry>(&mut self) -> Result<()> {
self.register_entry_type::<T>()
}
pub fn register_registry<R: CultCacheRegistry>(&mut self, registry: R) -> Result<&mut Self> {
registry.register_entries(self)?;
Ok(self)
}
pub fn add_backing_store(
&mut self,
store: impl CacheBackingStore + 'static,
types: impl IntoIterator<Item = impl Into<String>>,
) {
self.stores.push(CultCacheStoreRegistration {
store: Box::new(store),
types: types.into_iter().map(Into::into).collect(),
});
}
pub fn add_generic_backing_store(&mut self, store: impl CacheBackingStore + 'static) {
self.add_backing_store(store, Vec::<String>::new());
}
pub fn pull_all_backing_stores(&mut self) -> Result<()> {
self.entries.clear();
let known_types: BTreeSet<String> = self.definitions.keys().cloned().collect();
for registration in &mut self.stores {
for entry in registration.store.pull_all()? {
if !known_types.contains(&entry.r#type) {
return Err(anyhow!(
"No schema is registered for persisted entry type {:?}",
entry.r#type
));
}
self.entries.insert(entry_id(&entry), entry);
}
}
Ok(())
}
pub fn get<T: DatabaseEntry>(&self, key: &str) -> Result<Option<T>> {
self.require_entry_type::<T>()?;
let Some(entry) = self.entries.get(&entry_id_parts(T::TYPE, key)) else {
return Ok(None);
};
let payload = rmp_serde::from_slice(&entry.payload).with_context(|| {
format!(
"failed to decode CultCache entry {:?} at key {:?} as {}",
T::TYPE,
key,
T::SCHEMA_NAME
)
})?;
Ok(Some(payload))
}
pub fn get_required<T: DatabaseEntry>(&self, key: &str) -> Result<T> {
self.get::<T>(key)?
.ok_or_else(|| anyhow!("CultCache has no {:?} entry at key {:?}", T::TYPE, key))
}
pub fn get_envelope<T: DatabaseEntry>(&self, key: &str) -> Result<Option<CultCacheEnvelope>> {
self.require_entry_type::<T>()?;
Ok(self.entries.get(&entry_id_parts(T::TYPE, key)).cloned())
}
pub fn get_required_envelope<T: DatabaseEntry>(&self, key: &str) -> Result<CultCacheEnvelope> {
self.get_envelope::<T>(key)?
.ok_or_else(|| anyhow!("CultCache has no {:?} envelope at key {:?}", T::TYPE, key))
}
pub fn get_all<T: DatabaseEntry>(&self) -> Result<Vec<T>> {
self.require_entry_type::<T>()?;
let mut values = Vec::new();
for entry in self.entries.values() {
if entry.r#type != T::TYPE {
continue;
}
values.push(rmp_serde::from_slice(&entry.payload).with_context(|| {
format!(
"failed to decode CultCache entry {:?} at key {:?} as {}",
T::TYPE,
entry.key,
T::SCHEMA_NAME
)
})?);
}
Ok(values)
}
pub fn put<T: DatabaseEntry>(&mut self, key: impl Into<String>, value: &T) -> Result<T> {
self.require_entry_type::<T>()?;
let key = key.into();
let payload = rmp_serde::to_vec(value).with_context(|| {
format!(
"failed to encode CultCache entry {:?} at key {:?} as {}",
T::TYPE,
key,
T::SCHEMA_NAME
)
})?;
let parsed: T = rmp_serde::from_slice(&payload).with_context(|| {
format!(
"failed to validate CultCache entry {:?} at key {:?} as {}",
T::TYPE,
key,
T::SCHEMA_NAME
)
})?;
let entry = CultCacheEnvelope {
key: key.clone(),
r#type: T::TYPE.to_string(),
payload,
stored_at: now_utc_second(),
schema_id: Some(T::TYPE.to_string()),
};
let route = self.resolve_route_indices(T::TYPE);
let Some(primary_index) = route.first().copied() else {
return Err(anyhow!(
"No backing store is registered for entry type {:?}",
T::TYPE
));
};
self.stores[primary_index].store.push(&entry)?;
for mirror_index in route.iter().skip(1).copied() {
self.stores[mirror_index].store.push(&entry)?;
}
self.entries.insert(entry_id(&entry), entry);
Ok(parsed)
}
pub fn put_envelope<T: DatabaseEntry>(&mut self, entry: CultCacheEnvelope) -> Result<T> {
self.require_entry_type::<T>()?;
if entry.r#type != T::TYPE {
return Err(anyhow!(
"CultCache envelope type {:?} does not match registered Rust type {:?}",
entry.r#type,
T::TYPE
));
}
if entry.key.trim().is_empty() {
return Err(anyhow!(
"CultCache envelope keys for type {:?} must be non-empty",
T::TYPE
));
}
if entry.stored_at.trim().is_empty() {
return Err(anyhow!(
"CultCache envelope stored_at for type {:?} must be non-empty",
T::TYPE
));
}
let parsed: T = rmp_serde::from_slice(&entry.payload).with_context(|| {
format!(
"failed to validate CultCache envelope {:?} at key {:?} as {}",
T::TYPE,
entry.key,
T::SCHEMA_NAME
)
})?;
let route = self.resolve_route_indices(T::TYPE);
let Some(primary_index) = route.first().copied() else {
return Err(anyhow!(
"No backing store is registered for entry type {:?}",
T::TYPE
));
};
self.stores[primary_index].store.push(&entry)?;
for mirror_index in route.iter().skip(1).copied() {
self.stores[mirror_index].store.push(&entry)?;
}
self.entries.insert(entry_id(&entry), entry);
Ok(parsed)
}
pub fn update<T, F>(&mut self, key: &str, updater: F) -> Result<T>
where
T: DatabaseEntry,
F: FnOnce(Option<T>) -> T,
{
let current = self.get::<T>(key)?;
self.put::<T>(key.to_string(), &updater(current))
}
pub fn delete<T: DatabaseEntry>(&mut self, key: &str) -> Result<bool> {
self.require_entry_type::<T>()?;
let id = entry_id_parts(T::TYPE, key);
let Some(entry) = self.entries.get(&id).cloned() else {
return Ok(false);
};
let route = self.resolve_route_indices(T::TYPE);
let Some(primary_index) = route.first().copied() else {
return Err(anyhow!(
"No backing store is registered for entry type {:?}",
T::TYPE
));
};
self.stores[primary_index].store.delete(&entry)?;
for mirror_index in route.iter().skip(1).copied() {
self.stores[mirror_index].store.delete(&entry)?;
}
self.entries.remove(&id);
Ok(true)
}
pub fn snapshot(&self) -> Vec<CultCacheEnvelope> {
self.entries.values().cloned().collect()
}
fn require_entry_type<T: DatabaseEntry>(&self) -> Result<()> {
match self.definitions.get(T::TYPE) {
Some(schema_name) if *schema_name == T::SCHEMA_NAME => Ok(()),
_ => Err(anyhow!(
"CultCache entry type {:?} is not registered on this cache instance",
T::TYPE
)),
}
}
fn resolve_route_indices(&self, type_id: &str) -> Vec<usize> {
let type_specific: Vec<usize> = self
.stores
.iter()
.enumerate()
.filter_map(|(index, registration)| {
registration.types.contains(type_id).then_some(index)
})
.collect();
if !type_specific.is_empty() {
return type_specific;
}
self.stores
.iter()
.enumerate()
.filter_map(|(index, registration)| registration.types.is_empty().then_some(index))
.collect()
}
}
impl Default for CultCache {
fn default() -> Self {
Self::new()
}
}
fn entry_id(entry: &CultCacheEnvelope) -> String {
entry_id_parts(&entry.r#type, &entry.key)
}
fn entry_id_parts(r#type: &str, key: &str) -> String {
format!("{type}::{key}", type = r#type)
}
fn now_utc_second() -> String {
chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}
fn encode_store_snapshot(entries: &[CultCacheEnvelope]) -> PersistedStoreSnapshot {
let mut schema_names = BTreeSet::<String>::new();
for entry in entries {
schema_names.insert(
entry
.schema_id
.clone()
.unwrap_or_else(|| entry.r#type.clone()),
);
}
let catalog = schema_names
.into_iter()
.map(|schema_id| {
PersistedSchemaCatalogEntry(
schema_id.clone(),
schema_id.clone(),
format!("{schema_id}.v1"),
schema_id.clone(),
format!(
"{{\"schemaName\":\"{}\",\"schemaVersion\":\"{}.v1\",\"members\":[]}}",
escape_json_string(&schema_id),
escape_json_string(&schema_id)
),
vec![schema_id],
Vec::new(),
)
})
.collect();
let records = entries
.iter()
.map(|entry| {
PersistedRecord(
entry.key.clone(),
entry
.schema_id
.clone()
.unwrap_or_else(|| entry.r#type.clone()),
entry.stored_at.clone(),
entry.payload.clone(),
)
})
.collect();
PersistedStoreSnapshot("cultcache.store.v1".to_string(), catalog, records)
}
fn decode_store_snapshot(bytes: &[u8]) -> Result<Vec<CultCacheEnvelope>> {
let snapshot: PersistedStoreSnapshot =
rmp_serde::from_slice(bytes).context("failed to decode CultCache v1 snapshot")?;
if snapshot.0 != "cultcache.store.v1" {
return Err(anyhow!("unsupported CultCache snapshot {}", snapshot.0));
}
let catalog = snapshot
.1
.into_iter()
.map(|entry| (entry.0, entry.1))
.collect::<BTreeMap<_, _>>();
snapshot
.2
.into_iter()
.map(|record| {
let r#type = catalog.get(&record.1).cloned().ok_or_else(|| {
anyhow!(
"CultCache record {:?} references missing schema {:?}",
record.0,
record.1
)
})?;
Ok(CultCacheEnvelope {
key: record.0,
r#type,
stored_at: record.2,
payload: record.3,
schema_id: Some(record.1),
})
})
.collect()
}
fn escape_json_string(value: &str) -> String {
value.replace('\\', "\\\\").replace('"', "\\\"")
}
fn temporary_path_for(path: &Path) -> PathBuf {
let mut file_name = path
.file_name()
.map(|value| value.to_os_string())
.unwrap_or_else(|| "cultcache.cc".into());
file_name.push(format!(".{}.tmp", uuid::Uuid::new_v4()));
path.with_file_name(file_name)
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "settings")]
struct Settings {
#[cultcache(key = 0)]
theme: String,
#[cultcache(key = 1, default)]
retries: u32,
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "note")]
struct Note {
#[cultcache(key = 0)]
title: String,
#[cultcache(key = 1)]
body: String,
}
cultcache_registry!(TestEntries { Settings, Note });
#[test]
fn familiar_cultcache_flow_persists_and_reloads_typed_documents() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("cache.cc");
let settings = Settings {
theme: "ash".to_string(),
retries: 3,
};
let mut cache = CultCache::new();
cache.register_entry_type::<Settings>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path));
cache.pull_all_backing_stores()?;
cache.put("app", &settings)?;
assert_eq!(cache.get_required::<Settings>("app")?, settings);
let mut reloaded = CultCache::new();
reloaded.register_entry_type::<Settings>()?;
reloaded.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path));
reloaded.pull_all_backing_stores()?;
assert_eq!(reloaded.get_required::<Settings>("app")?, settings);
Ok(())
}
#[test]
fn entry_identity_is_polymorphic_by_type_and_key() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("cache.cc");
let mut cache = CultCache::new();
cache.register_registry(TestEntries)?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path));
cache.put(
"shared",
&Settings {
theme: "green".to_string(),
retries: 1,
},
)?;
cache.put(
"shared",
&Note {
title: "same key".to_string(),
body: "different type".to_string(),
},
)?;
assert_eq!(cache.snapshot().len(), 2);
assert_eq!(cache.get_required::<Note>("shared")?.title, "same key");
assert_eq!(cache.get_required::<Settings>("shared")?.theme, "green");
Ok(())
}
#[test]
fn type_specific_store_routes_before_generic_store() -> Result<()> {
let temp = tempfile::tempdir()?;
let generic_path = temp.path().join("generic.cc");
let settings_path = temp.path().join("settings.cc");
let mut cache = CultCache::new();
cache.register_entry_type::<Settings>()?;
cache.register_entry_type::<Note>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&generic_path));
cache.add_backing_store(
SingleFileMessagePackBackingStore::new(&settings_path),
["settings"],
);
cache.put(
"app",
&Settings {
theme: "ash".to_string(),
retries: 3,
},
)?;
cache.put(
"memo",
&Note {
title: "hello".to_string(),
body: "world".to_string(),
},
)?;
let generic_entries = SingleFileMessagePackBackingStore::new(&generic_path).pull_all()?;
let settings_entries = SingleFileMessagePackBackingStore::new(&settings_path).pull_all()?;
assert_eq!(generic_entries[0].r#type, "note");
assert_eq!(settings_entries[0].r#type, "settings");
Ok(())
}
#[test]
fn update_and_delete_follow_the_cache_api() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("cache.cc");
let mut cache = CultCache::new();
cache.register_entry_type::<Settings>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path));
let updated = cache.update::<Settings, _>("app", |current| {
let mut current = current.unwrap_or(Settings {
theme: "ash".to_string(),
retries: 0,
});
current.retries += 1;
current
})?;
assert_eq!(updated.retries, 1);
assert!(cache.delete::<Settings>("app")?);
assert!(cache.get::<Settings>("app")?.is_none());
Ok(())
}
#[test]
fn pull_rejects_unregistered_persisted_entry_type() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("cache.cc");
let mut store = SingleFileMessagePackBackingStore::new(&store_path);
store.push(&CultCacheEnvelope {
key: "unknown".to_string(),
r#type: "unregistered".to_string(),
payload: rmp_serde::to_vec(&1_u8)?,
stored_at: now_utc_second(),
schema_id: Some("unregistered".to_string()),
})?;
let mut cache = CultCache::new();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path));
let error = cache.pull_all_backing_stores().unwrap_err();
assert!(
error
.to_string()
.contains("No schema is registered for persisted entry type")
);
Ok(())
}
#[test]
fn payload_is_binary_messagepack_not_json_value() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("cache.cc");
let mut cache = CultCache::new();
cache.register_entry_type::<Settings>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path));
cache.put(
"app",
&Settings {
theme: "ash".to_string(),
retries: 3,
},
)?;
let entry = cache.snapshot().remove(0);
let decoded: Settings = rmp_serde::from_slice(&entry.payload)?;
assert_eq!(decoded.theme, "ash");
assert!(!entry.payload.is_empty());
Ok(())
}
#[test]
fn corrupted_payload_fails_during_typed_retrieval() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("cache.cc");
let mut store = SingleFileMessagePackBackingStore::new(&store_path);
store.push(&CultCacheEnvelope {
key: "app".to_string(),
r#type: "settings".to_string(),
payload: vec![0xc1],
stored_at: now_utc_second(),
schema_id: Some("settings".to_string()),
})?;
let mut cache = CultCache::new();
cache.register_entry_type::<Settings>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path));
cache.pull_all_backing_stores()?;
let error = cache.get_required::<Settings>("app").unwrap_err();
assert!(
error
.to_string()
.contains("failed to decode CultCache entry")
);
Ok(())
}
#[test]
fn put_envelope_reuses_existing_messagepack_payload() -> Result<()> {
let temp = tempfile::tempdir()?;
let origin_store = temp.path().join("origin.cc");
let target_store = temp.path().join("target.cc");
let mut origin = CultCache::new();
origin.register_entry_type::<Settings>()?;
origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store));
origin.put(
"app",
&Settings {
theme: "ash".to_string(),
retries: 3,
},
)?;
let envelope = origin.get_required_envelope::<Settings>("app")?;
let mut target = CultCache::new();
target.register_entry_type::<Settings>()?;
target.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&target_store));
let applied = target.put_envelope::<Settings>(envelope.clone())?;
assert_eq!(
applied,
Settings {
theme: "ash".to_string(),
retries: 3,
}
);
assert_eq!(target.get_required::<Settings>("app")?, applied);
assert_eq!(
target.get_required_envelope::<Settings>("app")?.payload,
envelope.payload
);
Ok(())
}
}

1231
vendor/cultmesh-rs/Cargo.lock generated vendored Normal file

File diff suppressed because it is too large Load diff

19
vendor/cultmesh-rs/Cargo.toml vendored Normal file
View file

@ -0,0 +1,19 @@
[package]
name = "cultmesh-rs"
version = "0.1.0"
edition = "2024"
publish = false
[lib]
name = "cultmesh_rs"
path = "src/lib.rs"
[dependencies]
anyhow = "1"
cultcache-rs = { path = "../cultcache-rs" }
cultnet-rs = { path = "../cultnet-rs" }
serde = { version = "1", features = ["derive"] }
[dev-dependencies]
pretty_assertions = "1.4.1"
tempfile = "3"

212
vendor/cultmesh-rs/src/lib.rs vendored Normal file
View file

@ -0,0 +1,212 @@
use anyhow::Result;
use cultcache_rs::CultCache;
use cultcache_rs::DatabaseEntry;
use cultcache_rs::SingleFileMessagePackBackingStore;
use cultnet_rs::CultNetDocumentRegistry;
use std::path::Path;
pub trait CultMeshDocumentSet: Clone + Send + Sync + 'static {
fn register_cache(&self, cache: &mut CultCache) -> Result<()>;
fn register_documents(&self, registry: &mut CultNetDocumentRegistry) -> Result<()>;
}
#[macro_export]
macro_rules! cultmesh_documents {
($name:ident { $($entry:ty => $schema_version:expr),* $(,)? }) => {
#[derive(Clone, Copy, Debug, Default)]
pub struct $name;
impl $crate::CultMeshDocumentSet for $name {
fn register_cache(
&self,
cache: &mut cultcache_rs::CultCache,
) -> ::anyhow::Result<()> {
$(
cache.register_entry_type::<$entry>()?;
)*
Ok(())
}
fn register_documents(
&self,
registry: &mut cultnet_rs::CultNetDocumentRegistry,
) -> ::anyhow::Result<()> {
$(
registry.register(
cultnet_rs::CultNetDocumentBinding::for_entry::<$entry>(
$schema_version.to_string(),
),
);
)*
Ok(())
}
}
};
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CultMeshNodeOptions {
pub runtime_id: String,
pub pull_on_start: bool,
}
impl Default for CultMeshNodeOptions {
fn default() -> Self {
Self {
runtime_id: "cultmesh-local".to_string(),
pull_on_start: true,
}
}
}
pub struct CultMeshNode {
runtime_id: String,
cache: CultCache,
documents: CultNetDocumentRegistry,
}
impl CultMeshNode {
pub fn runtime_id(&self) -> &str {
&self.runtime_id
}
pub fn cache(&self) -> &CultCache {
&self.cache
}
pub fn documents(&self) -> &CultNetDocumentRegistry {
&self.documents
}
pub fn get<T: DatabaseEntry>(&self, key: &str) -> Result<Option<T>> {
self.cache.get::<T>(key)
}
pub fn get_required<T: DatabaseEntry>(&self, key: &str) -> Result<T> {
self.cache.get_required::<T>(key)
}
pub fn put<T: DatabaseEntry>(&mut self, key: impl Into<String>, value: &T) -> Result<T> {
self.cache.put(key, value)
}
pub fn delete<T: DatabaseEntry>(&mut self, key: &str) -> Result<bool> {
self.cache.delete::<T>(key)
}
pub fn flush(&mut self) -> Result<()> {
Ok(())
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct CultMesh;
impl CultMesh {
pub fn create_node<D>(
store_path: impl AsRef<Path>,
documents: D,
options: CultMeshNodeOptions,
) -> Result<CultMeshNode>
where
D: CultMeshDocumentSet,
{
let mut cache = CultCache::new();
documents.register_cache(&mut cache)?;
cache
.add_generic_backing_store(SingleFileMessagePackBackingStore::new(store_path.as_ref()));
if options.pull_on_start {
cache.pull_all_backing_stores()?;
}
let mut registry = CultNetDocumentRegistry::new();
documents.register_documents(&mut registry)?;
Ok(CultMeshNode {
runtime_id: options.runtime_id,
cache,
documents: registry,
})
}
pub fn start_node<D>(
store_path: impl AsRef<Path>,
documents: D,
options: CultMeshNodeOptions,
) -> Result<CultMeshNode>
where
D: CultMeshDocumentSet,
{
Self::create_node(store_path, documents, options)
}
}
pub fn create_node<D>(
store_path: impl AsRef<Path>,
documents: D,
options: CultMeshNodeOptions,
) -> Result<CultMeshNode>
where
D: CultMeshDocumentSet,
{
CultMesh::create_node(store_path, documents, options)
}
pub fn start_node<D>(
store_path: impl AsRef<Path>,
documents: D,
options: CultMeshNodeOptions,
) -> Result<CultMeshNode>
where
D: CultMeshDocumentSet,
{
CultMesh::start_node(store_path, documents, options)
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[derive(Clone, Debug, PartialEq, Eq, cultcache_rs::DatabaseEntry)]
#[cultcache(type = "cultmesh.test.note", schema = "CultMeshTestNote")]
struct Note {
#[cultcache(key = 0)]
body: String,
}
cultmesh_documents!(TestDocuments {
Note => "cultmesh.test.note.v0",
});
#[test]
fn node_round_trips_registered_documents() -> Result<()> {
let temp = tempfile::tempdir()?;
let store_path = temp.path().join("cultmesh.cc");
let note = Note {
body: "blessed circuit".to_string(),
};
let mut node = CultMesh::create_node(
&store_path,
TestDocuments,
CultMeshNodeOptions {
runtime_id: "test-runtime".to_string(),
..CultMeshNodeOptions::default()
},
)?;
assert_eq!(node.runtime_id(), "test-runtime");
node.put("note", &note)?;
node.flush()?;
let reloaded = CultMesh::create_node(&store_path, TestDocuments, Default::default())?;
assert_eq!(reloaded.get_required::<Note>("note")?, note);
assert!(
reloaded
.documents()
.binding_by_schema_id("cultmesh.test.note.v0")
.is_some()
);
Ok(())
}
}

2
vendor/cultnet-rs/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

32
vendor/cultnet-rs/Cargo.toml vendored Normal file
View file

@ -0,0 +1,32 @@
[package]
name = "cultnet-rs"
version = "0.1.0"
edition = "2024"
publish = false
license = "MIT"
description = "CultNet-style typed MessagePack messaging, direct-pipe framing, security helpers, and CultCache replication for Rust runtimes."
[lib]
name = "cultnet_rs"
path = "src/lib.rs"
[dependencies]
aes-gcm = "0.10"
anyhow = "1"
base64 = "0.22"
chrono = { version = "0.4", features = ["serde"] }
cultcache-rs = { path = "../cultcache-rs" }
hmac = "0.12"
rand = "0.9"
rmp-serde = "1.3"
rmpv = { version = "1.3", features = ["with-serde"] }
serde = { version = "1", features = ["derive"] }
serde_bytes = "0.11"
serde_json = "1"
sha2 = "0.10"
socket2 = "0.5"
uuid = { version = "1", features = ["v4"] }
[dev-dependencies]
pretty_assertions = "1.4"
tempfile = "3"

79
vendor/cultnet-rs/README.md vendored Normal file
View file

@ -0,0 +1,79 @@
# CultNet RS
`cultnet-rs` is the Rust sibling of `cultnet-ts`: typed MessagePack messages,
4-byte length-prefixed direct-pipe framing, CultLib-shaped security helpers, and
CultCache replication without making callers paw through raw envelopes like we
lost a bet.
The contract is intentionally boring:
- `cultnet.schema.v0` sends schema-versioned MessagePack objects.
- `gamecult.networking.v0` maps the legacy C# union shape explicitly.
- schema discovery uses explicit catalog request/response messages; no inbound
auto-detect sludge.
- document put/delete/snapshot messages move typed CultCache entries.
- raw document put/snapshot messages preserve canonical MessagePack payload
bytes for bit-compatible neighbors.
- hello messages advertise `supportedMutationContracts`, so callers discover
which document types are read-only, which accept typed intents, which
authority owns the mutation, and which receipt documents prove the outcome.
- payloads are decoded through registered Rust types before entering the cache.
This crate is not an HTTP wrapper. It is the wire vocabulary Epiphany,
Ghostlight, VoidBot, and the rest of the swarm can share.
The public API surface is the schema plus its mutation contracts. A runtime does
not expose a pile of bespoke verbs and hope everyone remembers the ritual; it
advertises typed documents, typed intents, and typed receipts. Polite machines
knock on the contract before touching the furniture.
## Receipts
```powershell
cargo test
```
The initial tests prove:
- CultLib-compatible AES-GCM string encryption and HMAC session tokens
- 4-byte big-endian MessagePack framing
- schema-versioned message round trips
- legacy `gamecult.networking.v0` login mapping
- schema discovery catalog responses with canonical JSON schema hashes
- CultCache snapshot replication through registered typed documents
- raw snapshot replication that preserves the original payload bytes
- document mutation contract advertisement through hello frames and registries
## Schema Discovery
`cultnet-rs` now ships a built-in schema registry for the shared swarm contract
surface:
- core wire messages
- legacy `gamecult.networking.v0` auth/sample payloads
- schema catalog request/response messages
- the canonical `ghostlight.agent-state` document payload schema
Use `builtin_schema_registry()` when you want the standard catalog, or register
your own closed-world schema set with `CultNetSchemaRegistry`. Discovery stays
explicit on purpose: peers advertise only the contracts they were compiled to
understand, the same way CultCache only consumes the document types you
registered instead of pretending polymorphism is a public park.
## Local Fast Lane
`cultnet-rs` now mirrors the raw replication seam from `cultnet-ts`:
- `cultnet.document_put_raw.v0`
- `cultnet.snapshot_response_raw.v0`
Those messages carry the exact persisted MessagePack payload bytes from
CultCache along with the typed envelope metadata. Combined with
`CultCache::put_envelope::<T>()`, that lets a bit-compatible neighbor ingest the
document without re-encoding the payload first.
That still is not zero-copy in the religious sense. Frames allocate, bytes move,
and the receiving cache still decodes once to keep typed reads and validation
honest. The win is narrower and realer: identical payload bytes stop getting
decoded into generic sludge and then encoded right back into the same bytes for
no reason.

View file

@ -0,0 +1,19 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-delete.schema.json",
"title": "CultNet Document Delete Message",
"type": "object",
"required": [
"schemaVersion",
"messageId",
"schemaId",
"recordKey"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.document_delete.v0" },
"messageId": { "type": "string", "minLength": 1 },
"schemaId": { "type": "string", "minLength": 1 },
"recordKey": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,40 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-mutation-contract.schema.json",
"title": "CultNet Document Mutation Contract",
"type": "object",
"required": [
"documentType",
"operations",
"authority"
],
"additionalProperties": false,
"properties": {
"documentType": { "type": "string", "minLength": 1 },
"payloadSchemaVersion": { "type": "string", "minLength": 1 },
"operations": {
"type": "array",
"minItems": 1,
"items": {
"type": "string",
"enum": ["snapshot", "documentPut", "documentDelete", "intentSubmit", "receiptWatch"]
}
},
"authority": {
"type": "string",
"enum": ["readOnly", "localUser", "coordinator", "runtime", "denied"]
},
"intentDocumentTypes": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"receiptDocumentTypes": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"notes": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
}
}
}

View file

@ -0,0 +1,19 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-put-raw.schema.json",
"title": "CultNet Raw Document Put Message",
"type": "object",
"required": [
"schemaVersion",
"messageId",
"document"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.document_put_raw.v0" },
"messageId": { "type": "string", "minLength": 1 },
"document": {
"$ref": "./cultnet.raw-document-record.schema.json"
}
}
}

View file

@ -0,0 +1,19 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-put.schema.json",
"title": "CultNet Document Put Message",
"type": "object",
"required": [
"schemaVersion",
"messageId",
"document"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.document_put.v0" },
"messageId": { "type": "string", "minLength": 1 },
"document": {
"$ref": "./cultnet.document-record.schema.json"
}
}
}

View file

@ -0,0 +1,26 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-record.schema.json",
"title": "CultNet Document Record",
"type": "object",
"required": [
"schemaId",
"recordKey",
"storedAt",
"payload"
],
"additionalProperties": false,
"properties": {
"schemaId": { "type": "string", "minLength": 1 },
"recordKey": { "type": "string", "minLength": 1 },
"storedAt": { "type": "string", "minLength": 1 },
"payload": {},
"sourceRuntimeId": { "type": "string", "minLength": 1 },
"sourceAgentId": { "type": "string", "minLength": 1 },
"sourceRole": { "type": "string", "minLength": 1 },
"tags": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
}
}
}

View file

@ -0,0 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.error.schema.json",
"title": "CultNet Error Message",
"type": "object",
"required": [
"schemaVersion",
"error"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.error.v0" },
"error": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,35 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.hello.schema.json",
"title": "CultNet Hello Message",
"type": "object",
"required": [
"schemaVersion",
"runtimeId",
"runtimeKind"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.hello.v0" },
"runtimeId": { "type": "string", "minLength": 1 },
"runtimeKind": { "type": "string", "minLength": 1 },
"agentId": { "type": "string", "minLength": 1 },
"role": { "type": "string", "minLength": 1 },
"displayName": { "type": "string", "minLength": 1 },
"supportedDocumentTypes": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"supportedMutationContracts": {
"type": "array",
"items": {
"$ref": "./cultnet.document-mutation-contract.schema.json"
}
},
"supportedMessageVersions": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"supportsSchemaCatalog": { "type": "boolean" }
}
}

View file

@ -0,0 +1,17 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.login-success.schema.json",
"title": "CultNet Login Success Message",
"type": "object",
"required": [
"schemaVersion",
"nonce",
"session"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.login_success.v0" },
"nonce": { "type": "string", "minLength": 1 },
"session": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,19 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.login.schema.json",
"title": "CultNet Login Message",
"type": "object",
"required": [
"schemaVersion",
"nonce",
"auth",
"password"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.login.v0" },
"nonce": { "type": "string", "minLength": 1 },
"auth": { "type": "string", "minLength": 1 },
"password": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,32 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.raw-document-record.schema.json",
"title": "CultNet Raw Document Record",
"type": "object",
"required": [
"schemaId",
"recordKey",
"storedAt",
"payloadEncoding",
"payload"
],
"additionalProperties": false,
"properties": {
"schemaId": { "type": "string", "minLength": 1 },
"recordKey": { "type": "string", "minLength": 1 },
"storedAt": { "type": "string", "minLength": 1 },
"payloadEncoding": { "const": "messagepack" },
"payload": {
"type": "string",
"contentEncoding": "base64",
"x-cult-runtime-type": "bytes"
},
"sourceRuntimeId": { "type": "string", "minLength": 1 },
"sourceAgentId": { "type": "string", "minLength": 1 },
"sourceRole": { "type": "string", "minLength": 1 },
"tags": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
}
}
}

View file

@ -0,0 +1,21 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.register.schema.json",
"title": "CultNet Register Message",
"type": "object",
"required": [
"schemaVersion",
"nonce",
"email",
"password",
"name"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.register.v0" },
"nonce": { "type": "string", "minLength": 1 },
"email": { "type": "string", "minLength": 1 },
"password": { "type": "string", "minLength": 1 },
"name": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.sample-change-name.schema.json",
"title": "CultNet Sample Change Name Message",
"type": "object",
"required": [
"schemaVersion",
"name"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.sample.change_name.v0" },
"name": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.sample-chat.schema.json",
"title": "CultNet Sample Chat Message",
"type": "object",
"required": [
"schemaVersion",
"text"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.sample.chat.v0" },
"text": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,31 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.schema-catalog-request.schema.json",
"title": "CultNet Schema Catalog Request Message",
"type": "object",
"required": [
"schemaVersion",
"messageId"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.schema_catalog_request.v0" },
"messageId": { "type": "string", "minLength": 1 },
"includeSchemaJson": { "type": "boolean" },
"schemaIds": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"kinds": {
"type": "array",
"items": {
"type": "string",
"enum": [
"wire_message",
"document_payload",
"shared_contract"
]
}
}
}
}

View file

@ -0,0 +1,22 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.schema-catalog-response.schema.json",
"title": "CultNet Schema Catalog Response Message",
"type": "object",
"required": [
"schemaVersion",
"messageId",
"schemas"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.schema_catalog_response.v0" },
"messageId": { "type": "string", "minLength": 1 },
"schemas": {
"type": "array",
"items": {
"$ref": "./cultnet.schema-descriptor.schema.json"
}
}
}
}

View file

@ -0,0 +1,39 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.schema-descriptor.schema.json",
"title": "CultNet Schema Descriptor",
"type": "object",
"required": [
"schemaId",
"kind",
"wireContracts",
"contentHash"
],
"additionalProperties": false,
"properties": {
"schemaId": { "type": "string", "minLength": 1 },
"kind": {
"type": "string",
"enum": [
"wire_message",
"document_payload",
"shared_contract"
]
},
"schemaVersion": { "type": "string", "minLength": 1 },
"documentType": { "type": "string", "minLength": 1 },
"title": { "type": "string", "minLength": 1 },
"wireContracts": {
"type": "array",
"items": {
"type": "string",
"enum": [
"cultnet.schema.v0",
"gamecult.networking.v0"
]
}
},
"contentHash": { "type": "string", "minLength": 1 },
"schemaJson": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,23 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.snapshot-request.schema.json",
"title": "CultNet Snapshot Request Message",
"type": "object",
"required": [
"schemaVersion",
"messageId"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.snapshot_request.v0" },
"messageId": { "type": "string", "minLength": 1 },
"schemaIds": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"recordKeys": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
}
}
}

View file

@ -0,0 +1,22 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.snapshot-response-raw.schema.json",
"title": "CultNet Raw Snapshot Response Message",
"type": "object",
"required": [
"schemaVersion",
"messageId",
"documents"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.snapshot_response_raw.v0" },
"messageId": { "type": "string", "minLength": 1 },
"documents": {
"type": "array",
"items": {
"$ref": "./cultnet.raw-document-record.schema.json"
}
}
}
}

View file

@ -0,0 +1,22 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.snapshot-response.schema.json",
"title": "CultNet Snapshot Response Message",
"type": "object",
"required": [
"schemaVersion",
"messageId",
"documents"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.snapshot_response.v0" },
"messageId": { "type": "string", "minLength": 1 },
"documents": {
"type": "array",
"items": {
"$ref": "./cultnet.document-record.schema.json"
}
}
}
}

View file

@ -0,0 +1,17 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.verify.schema.json",
"title": "CultNet Verify Message",
"type": "object",
"required": [
"schemaVersion",
"nonce",
"session"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.verify.v0" },
"nonce": { "type": "string", "minLength": 1 },
"session": { "type": "string", "minLength": 1 }
}
}

View file

@ -0,0 +1,845 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/Ghostlight/schemas/agent-state.schema.json",
"title": "Ghostlight Agent State Schema",
"description": "First canonical contract for Ghostlight agent, perception, relationship, event, scene, and dialogue-context state.",
"type": "object",
"required": [
"schema_version",
"world",
"agents",
"relationships",
"events",
"scenes"
],
"additionalProperties": false,
"properties": {
"schema_version": {
"const": "ghostlight.agent_state.v0"
},
"world": {
"$ref": "#/$defs/world_state"
},
"agents": {
"type": "array",
"items": {
"$ref": "#/$defs/agent"
}
},
"relationships": {
"type": "array",
"items": {
"$ref": "#/$defs/relationship"
}
},
"events": {
"type": "array",
"items": {
"$ref": "#/$defs/event"
}
},
"scenes": {
"type": "array",
"items": {
"$ref": "#/$defs/scene"
}
}
},
"$defs": {
"world_state": {
"type": "object",
"required": [
"world_id",
"setting",
"time",
"canon_context"
],
"additionalProperties": false,
"properties": {
"world_id": {
"type": "string"
},
"setting": {
"type": "string"
},
"time": {
"type": "object",
"required": [
"label"
],
"additionalProperties": true,
"properties": {
"label": {
"type": "string"
}
}
},
"canon_context": {
"type": "array",
"items": {
"type": "string"
}
},
"ambient_pressures": {
"type": "array",
"items": {
"$ref": "#/$defs/pressure"
}
}
}
},
"agent": {
"type": "object",
"required": [
"agent_id",
"identity",
"canonical_state",
"goals",
"memories",
"perceived_state_overlays"
],
"additionalProperties": false,
"properties": {
"agent_id": {
"type": "string"
},
"identity": {
"$ref": "#/$defs/identity"
},
"canonical_state": {
"$ref": "#/$defs/canonical_state"
},
"goals": {
"type": "array",
"items": {
"$ref": "#/$defs/goal"
}
},
"memories": {
"$ref": "#/$defs/memory_bundle"
},
"perceived_state_overlays": {
"type": "array",
"items": {
"$ref": "#/$defs/perceived_state_overlay"
}
}
}
},
"identity": {
"type": "object",
"required": [
"name",
"roles",
"origin",
"public_description"
],
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"roles": {
"type": "array",
"items": {
"type": "string"
}
},
"origin": {
"type": "string"
},
"public_description": {
"type": "string"
},
"private_notes": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"canonical_state": {
"type": "object",
"required": [
"underlying_organization",
"stable_dispositions",
"behavioral_dimensions",
"presentation_strategy",
"voice_style",
"situational_state",
"values"
],
"additionalProperties": false,
"properties": {
"underlying_organization": {
"$ref": "#/$defs/variable_map"
},
"stable_dispositions": {
"$ref": "#/$defs/variable_map"
},
"behavioral_dimensions": {
"$ref": "#/$defs/variable_map"
},
"presentation_strategy": {
"$ref": "#/$defs/variable_map"
},
"situational_state": {
"$ref": "#/$defs/variable_map"
},
"values": {
"type": "array",
"items": {
"$ref": "#/$defs/value_commitment"
}
},
"voice_style": {
"$ref": "#/$defs/variable_map"
}
}
},
"variable_map": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/state_variable"
}
},
"state_variable": {
"type": "object",
"required": [
"mean",
"plasticity",
"current_activation"
],
"additionalProperties": false,
"properties": {
"mean": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"plasticity": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"current_activation": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"evidence": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"value_commitment": {
"type": "object",
"required": [
"value_id",
"label",
"priority",
"unforgivable_if_betrayed"
],
"additionalProperties": false,
"properties": {
"value_id": {
"type": "string"
},
"label": {
"type": "string"
},
"priority": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"unforgivable_if_betrayed": {
"type": "boolean"
}
}
},
"goal": {
"type": "object",
"required": [
"goal_id",
"description",
"scope",
"priority",
"emotional_stake",
"status"
],
"additionalProperties": false,
"properties": {
"goal_id": {
"type": "string"
},
"description": {
"type": "string"
},
"scope": {
"enum": [
"immediate",
"scene",
"case",
"arc",
"life"
]
},
"priority": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"emotional_stake": {
"type": "string"
},
"blockers": {
"type": "array",
"items": {
"type": "string"
}
},
"status": {
"enum": [
"active",
"blocked",
"dormant",
"resolved",
"abandoned"
]
}
}
},
"memory_bundle": {
"type": "object",
"required": [
"episodic",
"semantic",
"relationship_summaries"
],
"additionalProperties": false,
"properties": {
"episodic": {
"type": "array",
"items": {
"$ref": "#/$defs/memory"
}
},
"semantic": {
"type": "array",
"items": {
"$ref": "#/$defs/memory"
}
},
"relationship_summaries": {
"type": "array",
"items": {
"$ref": "#/$defs/memory"
}
}
}
},
"memory": {
"type": "object",
"required": [
"memory_id",
"summary",
"salience",
"confidence"
],
"additionalProperties": false,
"properties": {
"memory_id": {
"type": "string"
},
"summary": {
"type": "string"
},
"salience": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"confidence": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"linked_event_ids": {
"type": "array",
"items": {
"type": "string"
}
},
"linked_relationship_id": {
"type": "string"
}
}
},
"perceived_state_overlay": {
"type": "object",
"required": [
"observer_agent_id",
"target_agent_id",
"perceived_dimensions",
"beliefs",
"distortions"
],
"additionalProperties": false,
"properties": {
"observer_agent_id": {
"type": "string"
},
"target_agent_id": {
"type": "string"
},
"perceived_dimensions": {
"$ref": "#/$defs/inferred_variable_map"
},
"beliefs": {
"type": "array",
"items": {
"$ref": "#/$defs/belief"
}
},
"distortions": {
"type": "array",
"items": {
"enum": [
"hostile_attribution",
"false_reassurance",
"overread_warmth",
"underread_threat",
"false_mask_detection",
"missed_mask_detection",
"status_injury",
"threat_amplification",
"institutional_suspicion_transfer",
"care_underread",
"mask_overfocus"
]
}
}
}
},
"belief": {
"type": "object",
"required": [
"belief_id",
"claim",
"confidence"
],
"additionalProperties": false,
"properties": {
"belief_id": {
"type": "string"
},
"claim": {
"type": "string"
},
"confidence": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"subject_id": {
"type": "string"
},
"claim_type": {
"enum": [
"fact",
"identity_inference",
"motive_inference",
"prediction",
"norm",
"value_read",
"relationship_read"
]
},
"evidence_event_ids": {
"type": "array",
"items": {
"type": "string"
}
},
"visibility": {
"enum": [
"private",
"shared",
"public",
"rumor"
]
},
"emotional_charge": {
"type": "number",
"minimum": 0,
"maximum": 1
}
}
},
"relationship": {
"type": "object",
"required": [
"relationship_id",
"source_id",
"target_id",
"stance",
"summary"
],
"additionalProperties": false,
"properties": {
"relationship_id": {
"type": "string"
},
"source_id": {
"type": "string"
},
"target_id": {
"type": "string"
},
"stance": {
"$ref": "#/$defs/variable_map"
},
"summary": {
"type": "string"
},
"unresolved_incident_ids": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"event": {
"type": "object",
"required": [
"event_id",
"kind",
"summary",
"participants",
"pressure_tags"
],
"additionalProperties": false,
"properties": {
"event_id": {
"type": "string"
},
"kind": {
"enum": [
"dialogue",
"action",
"memory",
"relationship_shift",
"scene_outcome",
"author_note"
]
},
"summary": {
"type": "string"
},
"participants": {
"type": "array",
"items": {
"type": "string"
}
},
"pressure_tags": {
"type": "array",
"items": {
"type": "string"
}
},
"observed_exchange": {
"type": "array",
"items": {
"$ref": "#/$defs/event_exchange_turn"
}
},
"private_interpretations": {
"type": "array",
"items": {
"$ref": "#/$defs/event_private_interpretation"
}
},
"event_effects": {
"type": "array",
"items": {
"$ref": "#/$defs/event_effect"
}
}
}
},
"scene": {
"type": "object",
"required": [
"scene_id",
"location",
"participants",
"public_stakes",
"hidden_stakes",
"dialogue_context_packs"
],
"additionalProperties": false,
"properties": {
"scene_id": {
"type": "string"
},
"location": {
"type": "string"
},
"participants": {
"type": "array",
"items": {
"type": "string"
}
},
"public_stakes": {
"type": "array",
"items": {
"type": "string"
}
},
"hidden_stakes": {
"type": "array",
"items": {
"type": "string"
}
},
"dialogue_context_packs": {
"type": "array",
"items": {
"$ref": "#/$defs/dialogue_context_pack"
}
}
}
},
"dialogue_context_pack": {
"type": "object",
"required": [
"speaker_agent_id",
"listener_ids",
"speaker_local_truth",
"speaker_beliefs",
"active_memories",
"active_goals",
"presentation_constraints"
],
"additionalProperties": false,
"properties": {
"speaker_agent_id": {
"type": "string"
},
"listener_ids": {
"type": "array",
"items": {
"type": "string"
}
},
"speaker_local_truth": {
"type": "array",
"items": {
"type": "string"
}
},
"speaker_beliefs": {
"type": "array",
"items": {
"type": "string"
}
},
"active_memories": {
"type": "array",
"items": {
"type": "string"
}
},
"active_goals": {
"type": "array",
"items": {
"type": "string"
}
},
"presentation_constraints": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"pressure": {
"type": "object",
"required": [
"pressure_id",
"label",
"intensity"
],
"additionalProperties": false,
"properties": {
"pressure_id": {
"type": "string"
},
"label": {
"type": "string"
},
"intensity": {
"type": "number",
"minimum": 0,
"maximum": 1
}
}
},
"inferred_variable_map": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/inferred_state_variable"
}
},
"inferred_state_variable": {
"type": "object",
"required": [
"observed_activation",
"attributed_disposition",
"confidence"
],
"additionalProperties": false,
"properties": {
"observed_activation": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"attributed_disposition": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"confidence": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"evidence": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"event_exchange_turn": {
"type": "object",
"required": [
"speaker_id",
"utterance_summary",
"dialogue_function"
],
"additionalProperties": false,
"properties": {
"speaker_id": {
"type": "string"
},
"utterance_summary": {
"type": "string"
},
"dialogue_function": {
"type": "string"
},
"observable_cues": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"event_private_interpretation": {
"type": "object",
"required": [
"agent_id",
"interpretation",
"confidence"
],
"additionalProperties": false,
"properties": {
"agent_id": {
"type": "string"
},
"interpretation": {
"type": "string"
},
"attributed_motive": {
"type": "string"
},
"appraisal_tags": {
"type": "array",
"items": {
"type": "string"
}
},
"confidence": {
"type": "number",
"minimum": 0,
"maximum": 1
}
}
},
"event_effect": {
"type": "object",
"required": [
"agent_id",
"summary"
],
"additionalProperties": false,
"properties": {
"agent_id": {
"type": "string"
},
"summary": {
"type": "string"
},
"state_deltas": {
"type": "array",
"items": {
"$ref": "#/$defs/state_delta"
}
},
"new_belief_ids": {
"type": "array",
"items": {
"type": "string"
}
},
"memory_update_ids": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"state_delta": {
"type": "object",
"required": [
"path",
"delta"
],
"additionalProperties": false,
"properties": {
"path": {
"type": "string"
},
"delta": {
"type": "number",
"minimum": -1,
"maximum": 1
}
}
}
}
}

View file

@ -0,0 +1,967 @@
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use cultcache_rs::CultCache;
use cultcache_rs::DatabaseEntry;
use cultcache_rs::SingleFileMessagePackBackingStore;
use cultnet_rs::CultNetDocumentBinding;
use cultnet_rs::CultNetDocumentMutationContract;
use cultnet_rs::CultNetDocumentOperation;
use cultnet_rs::CultNetDocumentPutOptions;
use cultnet_rs::CultNetDocumentRegistry;
use cultnet_rs::CultNetMessage;
use cultnet_rs::CultNetMutationAuthority;
use cultnet_rs::CultNetSchemaKind;
use cultnet_rs::CultNetSchemaRegistration;
use cultnet_rs::CultNetSchemaRegistry;
use cultnet_rs::CultNetWireContract;
use cultnet_rs::builtin_schema_registry;
use cultnet_rs::decode_cultnet_message_from_slice;
use cultnet_rs::encode_cultnet_message_to_vec;
use cultnet_rs::encode_frame;
use serde::Deserialize;
use serde::Serialize;
use socket2::Domain;
use socket2::Protocol;
use socket2::Socket;
use socket2::Type;
use std::collections::BTreeMap;
use std::fs;
use std::io::Read;
use std::io::Write;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::net::SocketAddrV4;
use std::net::TcpListener;
use std::net::TcpStream;
use std::net::UdpSocket;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
const INTEROP_DOCUMENT_TYPE: &str = "cultnet.interop-note";
const INTEROP_SCHEMA_VERSION: &str = "cultnet.interop_note.v0";
const MUTATION_INTENT_TYPE: &str = "cultnet.interop-note-mutation-intent";
const MUTATION_INTENT_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-note-mutation-intent.schema.json";
const MUTATION_INTENT_SCHEMA_VERSION: &str = "cultnet.interop_note_mutation_intent.v0";
const MUTATION_RECEIPT_TYPE: &str = "cultnet.interop-note-mutation-receipt";
const MUTATION_RECEIPT_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-note-mutation-receipt.schema.json";
const MUTATION_RECEIPT_SCHEMA_VERSION: &str = "cultnet.interop_note_mutation_receipt.v0";
const FIRE_COMMAND_TYPE: &str = "cultnet.interop-fire-weapon-command";
const FIRE_COMMAND_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-fire-weapon-command.schema.json";
const FIRE_COMMAND_SCHEMA_VERSION: &str = "cultnet.interop_fire_weapon_command.v0";
const FIRE_RECEIPT_TYPE: &str = "cultnet.interop-fire-weapon-receipt";
const FIRE_RECEIPT_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-fire-weapon-receipt.schema.json";
const FIRE_RECEIPT_SCHEMA_VERSION: &str = "cultnet.interop_fire_weapon_receipt.v0";
const DISCOVERY_ANNOUNCE_SCHEMA_VERSION: &str = "cultnet.discovery_announce.v0";
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "cultnet.interop-note", schema = "CultNetInteropNote")]
struct CultNetInteropNote {
#[cultcache(key = 0)]
schema_version: String,
#[cultcache(key = 1)]
document_id: String,
#[cultcache(key = 2)]
author_runtime_id: String,
#[cultcache(key = 3)]
title: String,
#[cultcache(key = 4)]
body: String,
#[cultcache(key = 5)]
tags: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(
type = "cultnet.interop-note-mutation-intent",
schema = "CultNetInteropMutationIntent"
)]
struct CultNetInteropMutationIntent {
#[cultcache(key = 0)]
schema_version: String,
#[cultcache(key = 1)]
intent_id: String,
#[cultcache(key = 2)]
target_document_id: String,
#[cultcache(key = 3)]
append_body: String,
#[cultcache(key = 4)]
append_tag: String,
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(
type = "cultnet.interop-note-mutation-receipt",
schema = "CultNetInteropMutationReceipt"
)]
struct CultNetInteropMutationReceipt {
#[cultcache(key = 0)]
schema_version: String,
#[cultcache(key = 1)]
intent_id: String,
#[cultcache(key = 2)]
accepted: bool,
#[cultcache(key = 3)]
document_id: String,
#[cultcache(key = 4)]
body: String,
#[cultcache(key = 5)]
tags: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(
type = "cultnet.interop-fire-weapon-command",
schema = "CultNetInteropFireCommand"
)]
struct CultNetInteropFireCommand {
#[cultcache(key = 0)]
schema_version: String,
#[cultcache(key = 1)]
command_id: String,
#[cultcache(key = 2)]
character_id: String,
#[cultcache(key = 3)]
weapon_id: String,
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(
type = "cultnet.interop-fire-weapon-receipt",
schema = "CultNetInteropFireReceipt"
)]
struct CultNetInteropFireReceipt {
#[cultcache(key = 0)]
schema_version: String,
#[cultcache(key = 1)]
command_id: String,
#[cultcache(key = 2)]
accepted: bool,
#[cultcache(key = 3)]
character_id: String,
#[cultcache(key = 4)]
weapon_id: String,
#[cultcache(key = 5)]
shots_fired: u32,
#[cultcache(key = 6)]
ammo_remaining: u32,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "schemaVersion", rename_all = "camelCase")]
enum DiscoveryMessage {
#[serde(rename = "cultnet.discovery_probe.v0", rename_all = "camelCase")]
Probe {
message_id: String,
requester_runtime_id: String,
},
#[serde(rename = "cultnet.discovery_announce.v0", rename_all = "camelCase")]
Announce {
message_id: String,
runtime_id: String,
runtime_kind: String,
display_name: String,
agent_id: Option<String>,
tcp_host: String,
tcp_port: u16,
wire_contract: String,
supported_document_types: Vec<String>,
supports_schema_catalog: bool,
},
}
#[derive(Clone, Debug)]
struct PeerConfig {
runtime_id: String,
runtime_kind: String,
display_name: String,
agent_id: String,
bind_host: String,
advertise_host: String,
tcp_port: u16,
discovery_port: u16,
discovery_group: Ipv4Addr,
schema_path: String,
}
#[derive(Clone, Debug)]
struct DialConfig {
runtime_id: String,
runtime_kind: String,
display_name: String,
agent_id: String,
target_host: String,
target_port: u16,
schema_path: String,
}
fn main() -> Result<()> {
let mut args = std::env::args().skip(1);
let mode = args
.next()
.ok_or_else(|| anyhow!("expected mode: serve | probe | dial"))?;
let options = parse_args(args.collect());
match mode.as_str() {
"serve" => serve(parse_peer_config(&options)?)?,
"probe" => probe(&options)?,
"dial" => dial(parse_dial_config(&options)?)?,
_ => return Err(anyhow!("unknown mode {mode}")),
}
Ok(())
}
fn serve(config: PeerConfig) -> Result<()> {
let schema_registration = load_schema_registration(&config.schema_path)?;
let mut schema_registry = builtin_schema_registry()?;
schema_registry.register(schema_registration.clone())?;
let mut cache = CultCache::new();
cache.register_entry_type::<CultNetInteropNote>()?;
register_capability_entry_types(&mut cache)?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(runtime_store_path(
&config.runtime_id,
)));
cache.pull_all_backing_stores()?;
let note = build_note(&config.runtime_id, &config.display_name);
cache.put(&note.document_id, &note)?;
let mut document_registry = CultNetDocumentRegistry::new();
register_capability_bindings(&mut document_registry, &schema_registration.schema_id);
let cache = Arc::new(Mutex::new(cache));
let document_registry = Arc::new(document_registry);
let schema_registry = Arc::new(schema_registry);
let config = Arc::new(config);
start_udp_discovery_server(config.clone())?;
start_tcp_server(config.clone(), cache, document_registry, schema_registry)?;
print_json(&serde_json::json!({
"status": "ready",
"mode": "serve",
"runtimeId": config.runtime_id,
"runtimeKind": config.runtime_kind,
"tcpPort": config.tcp_port,
"discoveryPort": config.discovery_port,
"discoveryGroup": config.discovery_group.to_string(),
}))?;
loop {
thread::sleep(Duration::from_secs(3600));
}
}
fn probe(options: &BTreeMap<String, String>) -> Result<()> {
let runtime_id = require_arg(options, "runtime-id")?.to_string();
let discovery_port = parse_u16_arg(options, "discovery-port")?;
let discovery_group = parse_ipv4_arg(options, "discovery-group")?;
let timeout_ms = parse_u64_arg(options, "timeout-ms").unwrap_or(1_500);
let message_id = format!("{runtime_id}-{}", chrono::Utc::now().timestamp_millis());
let socket = create_discovery_socket(0, false)?;
socket.set_read_timeout(Some(Duration::from_millis(timeout_ms)))?;
let probe_message = DiscoveryMessage::Probe {
message_id: message_id.clone(),
requester_runtime_id: runtime_id.clone(),
};
let payload = rmp_serde::to_vec_named(&probe_message)?;
socket.send_to(
&payload,
SocketAddr::V4(SocketAddrV4::new(discovery_group, discovery_port)),
)?;
let mut buffer = vec![0_u8; 4096];
let mut found = BTreeMap::<String, serde_json::Value>::new();
loop {
match socket.recv_from(&mut buffer) {
Ok((len, _)) => {
if let Ok(DiscoveryMessage::Announce {
message_id: response_message_id,
runtime_id,
runtime_kind,
display_name,
agent_id,
tcp_host,
tcp_port,
wire_contract,
supported_document_types,
supports_schema_catalog,
}) = rmp_serde::from_slice::<DiscoveryMessage>(&buffer[..len])
{
if response_message_id == message_id {
found.insert(
runtime_id.clone(),
serde_json::json!({
"schemaVersion": DISCOVERY_ANNOUNCE_SCHEMA_VERSION,
"messageId": response_message_id,
"runtimeId": runtime_id,
"runtimeKind": runtime_kind,
"displayName": display_name,
"agentId": agent_id,
"tcpHost": tcp_host,
"tcpPort": tcp_port,
"wireContract": wire_contract,
"supportedDocumentTypes": supported_document_types,
"supportsSchemaCatalog": supports_schema_catalog,
}),
);
}
}
}
Err(error)
if error.kind() == std::io::ErrorKind::WouldBlock
|| error.kind() == std::io::ErrorKind::TimedOut =>
{
break;
}
Err(error) => return Err(error.into()),
}
}
print_json(&serde_json::json!({
"mode": "probe",
"runtimeId": runtime_id,
"peers": found.into_values().collect::<Vec<_>>(),
}))?;
Ok(())
}
fn dial(config: DialConfig) -> Result<()> {
let schema_registration = load_schema_registration(&config.schema_path)?;
let mut cache = CultCache::new();
cache.register_entry_type::<CultNetInteropNote>()?;
register_capability_entry_types(&mut cache)?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(runtime_store_path(
&format!("{}-dial", config.runtime_id),
)));
cache.pull_all_backing_stores()?;
let mut document_registry = CultNetDocumentRegistry::new();
register_capability_bindings(&mut document_registry, &schema_registration.schema_id);
let mut stream = TcpStream::connect((config.target_host.as_str(), config.target_port))
.with_context(|| {
format!(
"failed to connect to {}:{}",
config.target_host, config.target_port
)
})?;
send_message(
&mut stream,
&CultNetMessage::Hello {
runtime_id: config.runtime_id.clone(),
runtime_kind: config.runtime_kind.clone(),
agent_id: Some(config.agent_id.clone()),
role: None,
display_name: Some(config.display_name.clone()),
supported_document_types: Some(vec![INTEROP_DOCUMENT_TYPE.to_string()]),
supported_mutation_contracts: Some(interaction_contracts()),
supported_message_versions: Some(vec![INTEROP_SCHEMA_VERSION.to_string()]),
supports_schema_catalog: Some(true),
},
)?;
let remote_hello = read_message(&mut stream)?;
let remote_runtime_id = match &remote_hello {
CultNetMessage::Hello { runtime_id, .. } => runtime_id.clone(),
other => return Err(anyhow!("expected hello response, got {other:?}")),
};
send_message(
&mut stream,
&CultNetMessage::SchemaCatalogRequest {
message_id: format!("{}-catalog", config.runtime_id),
include_schema_json: Some(true),
schema_ids: None,
kinds: None,
},
)?;
let catalog_response = read_message(&mut stream)?;
let has_interop_schema = match &catalog_response {
CultNetMessage::SchemaCatalogResponse { schemas, .. } => schemas.iter().any(|schema| {
schema.schema_id == schema_registration.schema_id
&& schema.document_type.as_deref() == Some(INTEROP_DOCUMENT_TYPE)
}),
other => return Err(anyhow!("expected catalog response, got {other:?}")),
};
send_message(
&mut stream,
&CultNetMessage::SnapshotRequest {
message_id: format!("{}-snapshot", config.runtime_id),
schema_ids: Some(vec![schema_registration.schema_id.clone()]),
record_keys: None,
},
)?;
let snapshot_response = read_message(&mut stream)?;
let applied = document_registry
.apply_raw_snapshot_response::<CultNetInteropNote>(&mut cache, &snapshot_response)?;
let note = applied
.into_iter()
.find(|candidate| candidate.author_runtime_id == remote_runtime_id)
.ok_or_else(|| anyhow!("did not receive an interop note from {remote_runtime_id}"))?;
print_json(&serde_json::json!({
"mode": "dial",
"runtimeId": config.runtime_id,
"targetHost": config.target_host,
"targetPort": config.target_port,
"remoteHello": {
"schemaVersion": "cultnet.hello.v0",
"runtimeId": remote_runtime_id,
},
"hasInteropSchema": has_interop_schema,
"retrievedNote": {
"schemaVersion": note.schema_version,
"documentId": note.document_id,
"authorRuntimeId": note.author_runtime_id,
"title": note.title,
"body": note.body,
"tags": note.tags,
},
"mutatedNote": mutate_remote_note(&mut stream, &mut cache, &document_registry, &schema_registration.schema_id, &config.runtime_id, &note)?,
"fireReceipt": fire_remote_weapon(&mut stream, &mut cache, &document_registry, &config.runtime_id, &remote_runtime_id)?,
}))?;
Ok(())
}
fn mutate_remote_note(
stream: &mut TcpStream,
cache: &mut CultCache,
document_registry: &CultNetDocumentRegistry,
note_schema_id: &str,
runtime_id: &str,
note: &CultNetInteropNote,
) -> Result<serde_json::Value> {
let intent = CultNetInteropMutationIntent {
schema_version: MUTATION_INTENT_SCHEMA_VERSION.to_string(),
intent_id: format!("{runtime_id}-decorate"),
target_document_id: note.document_id.clone(),
append_body: format!(" Decorated by {runtime_id}."),
append_tag: format!("decorated:{runtime_id}"),
};
let message = document_registry.create_raw_document_put_message(
format!("{runtime_id}-decorate-put"),
intent.intent_id.clone(),
&intent,
CultNetDocumentPutOptions::default(),
)?;
send_message(stream, &message)?;
let receipt_message = read_message(stream)?;
let _receipt = document_registry
.apply_raw_document_put_message::<CultNetInteropMutationReceipt>(cache, &receipt_message)?;
let mutated_message = read_message(stream)?;
let mutated = document_registry
.apply_raw_document_put_message::<CultNetInteropNote>(cache, &mutated_message)?;
if mutated_message_schema_id(&mutated_message) != Some(note_schema_id) {
return Err(anyhow!("mutation response used the wrong schema id"));
}
Ok(serde_json::json!({
"schemaVersion": mutated.schema_version,
"documentId": mutated.document_id,
"authorRuntimeId": mutated.author_runtime_id,
"title": mutated.title,
"body": mutated.body,
"tags": mutated.tags,
}))
}
fn fire_remote_weapon(
stream: &mut TcpStream,
cache: &mut CultCache,
document_registry: &CultNetDocumentRegistry,
runtime_id: &str,
remote_runtime_id: &str,
) -> Result<serde_json::Value> {
let command = CultNetInteropFireCommand {
schema_version: FIRE_COMMAND_SCHEMA_VERSION.to_string(),
command_id: format!("{runtime_id}-fire"),
character_id: remote_runtime_id.to_string(),
weapon_id: "interop-rifle".to_string(),
};
let message = document_registry.create_raw_document_put_message(
format!("{runtime_id}-fire-put"),
command.command_id.clone(),
&command,
CultNetDocumentPutOptions::default(),
)?;
send_message(stream, &message)?;
let receipt_message = read_message(stream)?;
let receipt = document_registry
.apply_raw_document_put_message::<CultNetInteropFireReceipt>(cache, &receipt_message)?;
Ok(serde_json::json!({
"schemaVersion": receipt.schema_version,
"commandId": receipt.command_id,
"accepted": receipt.accepted,
"characterId": receipt.character_id,
"weaponId": receipt.weapon_id,
"shotsFired": receipt.shots_fired,
"ammoRemaining": receipt.ammo_remaining,
}))
}
fn start_udp_discovery_server(config: Arc<PeerConfig>) -> Result<()> {
let socket = create_discovery_socket(config.discovery_port, true)?;
socket.join_multicast_v4(&config.discovery_group, &Ipv4Addr::UNSPECIFIED)?;
socket.set_read_timeout(Some(Duration::from_millis(250)))?;
thread::spawn(move || {
let mut buffer = vec![0_u8; 4096];
loop {
match socket.recv_from(&mut buffer) {
Ok((len, remote)) => {
if let Ok(DiscoveryMessage::Probe {
message_id,
requester_runtime_id: _,
}) = rmp_serde::from_slice::<DiscoveryMessage>(&buffer[..len])
{
let announce = DiscoveryMessage::Announce {
message_id,
runtime_id: config.runtime_id.clone(),
runtime_kind: config.runtime_kind.clone(),
display_name: config.display_name.clone(),
agent_id: Some(config.agent_id.clone()),
tcp_host: config.advertise_host.clone(),
tcp_port: config.tcp_port,
wire_contract: "cultnet.schema.v0".to_string(),
supported_document_types: vec![INTEROP_DOCUMENT_TYPE.to_string()],
supports_schema_catalog: true,
};
if let Ok(payload) = rmp_serde::to_vec_named(&announce) {
let _ = socket.send_to(&payload, remote);
}
}
}
Err(error)
if error.kind() == std::io::ErrorKind::WouldBlock
|| error.kind() == std::io::ErrorKind::TimedOut => {}
Err(_) => break,
}
}
});
Ok(())
}
fn start_tcp_server(
config: Arc<PeerConfig>,
cache: Arc<Mutex<CultCache>>,
document_registry: Arc<CultNetDocumentRegistry>,
schema_registry: Arc<CultNetSchemaRegistry>,
) -> Result<()> {
let listener =
TcpListener::bind((config.bind_host.as_str(), config.tcp_port)).with_context(|| {
format!(
"failed to bind TCP listener on {}:{}",
config.bind_host, config.tcp_port
)
})?;
listener.set_nonblocking(false)?;
thread::spawn(move || {
for stream in listener.incoming() {
let Ok(stream) = stream else {
continue;
};
let config = config.clone();
let cache = cache.clone();
let document_registry = document_registry.clone();
let schema_registry = schema_registry.clone();
thread::spawn(move || {
if let Err(error) =
handle_connection(stream, config, cache, document_registry, schema_registry)
{
eprintln!("{error:#}");
}
});
}
});
Ok(())
}
fn handle_connection(
mut stream: TcpStream,
config: Arc<PeerConfig>,
cache: Arc<Mutex<CultCache>>,
document_registry: Arc<CultNetDocumentRegistry>,
schema_registry: Arc<CultNetSchemaRegistry>,
) -> Result<()> {
loop {
let message = match read_message(&mut stream) {
Ok(message) => message,
Err(error) if is_eof_like(&error) => break,
Err(error) => return Err(error),
};
match message {
CultNetMessage::Hello { .. } => {
send_message(
&mut stream,
&CultNetMessage::Hello {
runtime_id: config.runtime_id.clone(),
runtime_kind: config.runtime_kind.clone(),
agent_id: Some(config.agent_id.clone()),
role: None,
display_name: Some(config.display_name.clone()),
supported_document_types: Some(vec![INTEROP_DOCUMENT_TYPE.to_string()]),
supported_mutation_contracts: Some(interaction_contracts()),
supported_message_versions: Some(vec![INTEROP_SCHEMA_VERSION.to_string()]),
supports_schema_catalog: Some(true),
},
)?;
}
request @ CultNetMessage::SchemaCatalogRequest { .. } => {
let response = schema_registry.create_catalog_response(&request)?;
send_message(&mut stream, &response)?;
}
CultNetMessage::SnapshotRequest {
message_id,
schema_ids,
record_keys,
} => {
let mut response = document_registry.create_raw_snapshot_response(
&cache.lock().expect("cache poisoned"),
message_id,
schema_ids.as_deref(),
record_keys.as_deref(),
)?;
if let CultNetMessage::SnapshotResponseRaw { documents, .. } = &mut response {
for document in documents.iter_mut() {
document.source_runtime_id = Some(config.runtime_id.clone());
document.source_agent_id = Some(config.agent_id.clone());
document.source_role = Some("peer".to_string());
document.tags =
Some(vec!["interop".to_string(), config.runtime_id.clone()]);
}
}
send_message(&mut stream, &response)?;
}
message @ CultNetMessage::DocumentPutRaw { .. } => {
handle_raw_put(&mut stream, &config, &cache, &document_registry, &message)?;
}
_ => {}
}
}
Ok(())
}
fn handle_raw_put(
stream: &mut TcpStream,
config: &PeerConfig,
cache: &Arc<Mutex<CultCache>>,
document_registry: &CultNetDocumentRegistry,
message: &CultNetMessage,
) -> Result<()> {
let CultNetMessage::DocumentPutRaw { document, .. } = message else {
return Ok(());
};
if document.schema_id == MUTATION_INTENT_SCHEMA_ID {
let mut cache = cache.lock().expect("cache poisoned");
let intent = document_registry
.apply_raw_document_put_message::<CultNetInteropMutationIntent>(&mut cache, message)?;
let mut note = cache.get_required::<CultNetInteropNote>(&intent.target_document_id)?;
note.body = format!("{}{}", note.body, intent.append_body);
note.tags.push(intent.append_tag);
cache.put(&note.document_id, &note)?;
let receipt = CultNetInteropMutationReceipt {
schema_version: MUTATION_RECEIPT_SCHEMA_VERSION.to_string(),
intent_id: intent.intent_id.clone(),
accepted: true,
document_id: note.document_id.clone(),
body: note.body.clone(),
tags: note.tags.clone(),
};
let options = response_options(config, "mutation");
let receipt_message = document_registry.create_raw_document_put_message(
format!("{}-mutation-receipt", config.runtime_id),
receipt.intent_id.clone(),
&receipt,
options.clone(),
)?;
let note_message = document_registry.create_raw_document_put_message(
format!("{}-mutated-note", config.runtime_id),
note.document_id.clone(),
&note,
options,
)?;
send_message(stream, &receipt_message)?;
send_message(stream, &note_message)?;
} else if document.schema_id == FIRE_COMMAND_SCHEMA_ID {
let mut cache = cache.lock().expect("cache poisoned");
let command = document_registry
.apply_raw_document_put_message::<CultNetInteropFireCommand>(&mut cache, message)?;
let receipt = CultNetInteropFireReceipt {
schema_version: FIRE_RECEIPT_SCHEMA_VERSION.to_string(),
command_id: command.command_id,
accepted: true,
character_id: command.character_id,
weapon_id: command.weapon_id,
shots_fired: 1,
ammo_remaining: 29,
};
let receipt_message = document_registry.create_raw_document_put_message(
format!("{}-fire-receipt", config.runtime_id),
receipt.command_id.clone(),
&receipt,
response_options(config, "side-effect"),
)?;
send_message(stream, &receipt_message)?;
}
Ok(())
}
fn load_schema_registration(schema_path: &str) -> Result<CultNetSchemaRegistration> {
let schema_json = fs::read_to_string(schema_path)
.with_context(|| format!("failed to read schema {}", schema_path))?;
let parsed: serde_json::Value = serde_json::from_str(&schema_json)
.with_context(|| format!("failed to parse schema {}", schema_path))?;
let schema_id = parsed
.get("$id")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| anyhow!("schema {} is missing $id", schema_path))?
.to_string();
let title = parsed
.get("title")
.and_then(serde_json::Value::as_str)
.map(ToString::to_string);
Ok(CultNetSchemaRegistration {
schema_id,
kind: CultNetSchemaKind::DocumentPayload,
wire_contracts: vec![CultNetWireContract::CultNetSchemaV0],
schema_version: Some(INTEROP_SCHEMA_VERSION.to_string()),
document_type: Some(INTEROP_DOCUMENT_TYPE.to_string()),
title,
schema_json: Some(schema_json),
})
}
fn register_capability_entry_types(cache: &mut CultCache) -> Result<()> {
cache.register_entry_type::<CultNetInteropMutationIntent>()?;
cache.register_entry_type::<CultNetInteropMutationReceipt>()?;
cache.register_entry_type::<CultNetInteropFireCommand>()?;
cache.register_entry_type::<CultNetInteropFireReceipt>()?;
Ok(())
}
fn register_capability_bindings(
document_registry: &mut CultNetDocumentRegistry,
note_schema_id: &str,
) {
document_registry
.register(CultNetDocumentBinding::for_entry_with_schema_id::<
CultNetInteropNote,
>(
note_schema_id.to_string(),
INTEROP_SCHEMA_VERSION.to_string(),
))
.register(CultNetDocumentBinding::for_entry_with_schema_id::<
CultNetInteropMutationIntent,
>(
MUTATION_INTENT_SCHEMA_ID.to_string(),
MUTATION_INTENT_SCHEMA_VERSION.to_string(),
))
.register(CultNetDocumentBinding::for_entry_with_schema_id::<
CultNetInteropMutationReceipt,
>(
MUTATION_RECEIPT_SCHEMA_ID.to_string(),
MUTATION_RECEIPT_SCHEMA_VERSION.to_string(),
))
.register(CultNetDocumentBinding::for_entry_with_schema_id::<
CultNetInteropFireCommand,
>(
FIRE_COMMAND_SCHEMA_ID.to_string(),
FIRE_COMMAND_SCHEMA_VERSION.to_string(),
))
.register(CultNetDocumentBinding::for_entry_with_schema_id::<
CultNetInteropFireReceipt,
>(
FIRE_RECEIPT_SCHEMA_ID.to_string(),
FIRE_RECEIPT_SCHEMA_VERSION.to_string(),
));
}
fn interaction_contracts() -> Vec<CultNetDocumentMutationContract> {
vec![CultNetDocumentMutationContract {
document_type: INTEROP_DOCUMENT_TYPE.to_string(),
payload_schema_version: Some(INTEROP_SCHEMA_VERSION.to_string()),
operations: vec![
CultNetDocumentOperation::Snapshot,
CultNetDocumentOperation::DocumentPut,
CultNetDocumentOperation::IntentSubmit,
CultNetDocumentOperation::ReceiptWatch,
],
authority: CultNetMutationAuthority::Runtime,
intent_document_types: Some(vec![
MUTATION_INTENT_TYPE.to_string(),
FIRE_COMMAND_TYPE.to_string(),
]),
receipt_document_types: Some(vec![
MUTATION_RECEIPT_TYPE.to_string(),
FIRE_RECEIPT_TYPE.to_string(),
]),
notes: None,
}]
}
fn response_options(config: &PeerConfig, tag: &str) -> CultNetDocumentPutOptions {
CultNetDocumentPutOptions {
source_runtime_id: Some(config.runtime_id.clone()),
source_agent_id: Some(config.agent_id.clone()),
source_role: Some("peer".to_string()),
tags: Some(vec![tag.to_string(), config.runtime_id.clone()]),
..CultNetDocumentPutOptions::default()
}
}
fn mutated_message_schema_id(message: &CultNetMessage) -> Option<&str> {
match message {
CultNetMessage::DocumentPutRaw { document, .. } => Some(document.schema_id.as_str()),
_ => None,
}
}
fn build_note(runtime_id: &str, display_name: &str) -> CultNetInteropNote {
CultNetInteropNote {
schema_version: INTEROP_SCHEMA_VERSION.to_string(),
document_id: format!("note:{runtime_id}"),
author_runtime_id: runtime_id.to_string(),
title: format!("{display_name} keeps a little note"),
body: format!(
"{runtime_id} can move CultNet state without begging the gods for translation."
),
tags: vec![
runtime_id.to_string(),
"interop".to_string(),
"cultnet".to_string(),
],
}
}
fn send_message(stream: &mut TcpStream, message: &CultNetMessage) -> Result<()> {
let payload = encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?;
let frame = encode_frame(&payload)?;
stream.write_all(&frame)?;
stream.flush()?;
Ok(())
}
fn read_message(stream: &mut TcpStream) -> Result<CultNetMessage> {
let mut header = [0_u8; 4];
stream.read_exact(&mut header)?;
let payload_len = u32::from_be_bytes(header) as usize;
let mut payload = vec![0_u8; payload_len];
stream.read_exact(&mut payload)?;
decode_cultnet_message_from_slice(&payload, CultNetWireContract::CultNetSchemaV0)
}
fn is_eof_like(error: &anyhow::Error) -> bool {
error
.downcast_ref::<std::io::Error>()
.is_some_and(|io| io.kind() == std::io::ErrorKind::UnexpectedEof)
}
fn create_discovery_socket(port: u16, _join_group: bool) -> Result<UdpSocket> {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?;
socket.set_reuse_address(true)?;
socket.bind(&SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port).into())?;
let socket = UdpSocket::from(socket);
socket.set_multicast_loop_v4(true)?;
socket.set_multicast_ttl_v4(1)?;
socket.set_nonblocking(false)?;
Ok(socket)
}
fn parse_peer_config(options: &BTreeMap<String, String>) -> Result<PeerConfig> {
Ok(PeerConfig {
runtime_id: require_arg(options, "runtime-id")?.to_string(),
runtime_kind: require_arg(options, "runtime-kind")?.to_string(),
display_name: require_arg(options, "display-name")?.to_string(),
agent_id: require_arg(options, "agent-id")?.to_string(),
bind_host: options
.get("bind-host")
.cloned()
.unwrap_or_else(|| "127.0.0.1".to_string()),
advertise_host: require_arg(options, "advertise-host")?.to_string(),
tcp_port: parse_u16_arg(options, "tcp-port")?,
discovery_port: parse_u16_arg(options, "discovery-port")?,
discovery_group: parse_ipv4_arg(options, "discovery-group")?,
schema_path: require_arg(options, "schema-path")?.to_string(),
})
}
fn parse_dial_config(options: &BTreeMap<String, String>) -> Result<DialConfig> {
Ok(DialConfig {
runtime_id: require_arg(options, "runtime-id")?.to_string(),
runtime_kind: require_arg(options, "runtime-kind")?.to_string(),
display_name: require_arg(options, "display-name")?.to_string(),
agent_id: require_arg(options, "agent-id")?.to_string(),
target_host: require_arg(options, "target-host")?.to_string(),
target_port: parse_u16_arg(options, "target-port")?,
schema_path: require_arg(options, "schema-path")?.to_string(),
})
}
fn parse_args(args: Vec<String>) -> BTreeMap<String, String> {
let mut parsed = BTreeMap::new();
let mut index = 0;
while index < args.len() {
let token = &args[index];
if !token.starts_with("--") {
index += 1;
continue;
}
let name = token.trim_start_matches("--").to_string();
let value = args
.get(index + 1)
.cloned()
.unwrap_or_else(|| panic!("missing value for --{name}"));
parsed.insert(name, value);
index += 2;
}
parsed
}
fn require_arg<'a>(options: &'a BTreeMap<String, String>, name: &str) -> Result<&'a str> {
options
.get(name)
.map(String::as_str)
.ok_or_else(|| anyhow!("missing required argument --{name}"))
}
fn parse_u16_arg(options: &BTreeMap<String, String>, name: &str) -> Result<u16> {
require_arg(options, name)?
.parse::<u16>()
.with_context(|| format!("argument --{name} must be a u16"))
}
fn parse_u64_arg(options: &BTreeMap<String, String>, name: &str) -> Option<u64> {
options
.get(name)
.map(|value| value.parse::<u64>().expect("u64 arg"))
}
fn parse_ipv4_arg(options: &BTreeMap<String, String>, name: &str) -> Result<Ipv4Addr> {
require_arg(options, name)?
.parse::<Ipv4Addr>()
.with_context(|| format!("argument --{name} must be an IPv4 address"))
}
fn runtime_store_path(runtime_id: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!("cultnet-rs-interop-{runtime_id}.cc"))
}
fn print_json(value: &serde_json::Value) -> Result<()> {
println!("{}", serde_json::to_string(value)?);
Ok(())
}

1155
vendor/cultnet-rs/src/contracts.rs vendored Normal file

File diff suppressed because it is too large Load diff

49
vendor/cultnet-rs/src/framing.rs vendored Normal file
View file

@ -0,0 +1,49 @@
use anyhow::Result;
use anyhow::anyhow;
pub const FRAME_HEADER_BYTES: usize = 4;
pub fn encode_frame(payload: &[u8]) -> Result<Vec<u8>> {
let len: u32 = payload
.len()
.try_into()
.map_err(|_| anyhow!("CultNet frame payload is too large"))?;
let mut frame = Vec::with_capacity(FRAME_HEADER_BYTES + payload.len());
frame.extend_from_slice(&len.to_be_bytes());
frame.extend_from_slice(payload);
Ok(frame)
}
#[derive(Clone, Debug, Default)]
pub struct LengthPrefixedMessageFramer {
buffer: Vec<u8>,
}
impl LengthPrefixedMessageFramer {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, chunk: impl AsRef<[u8]>) -> Vec<Vec<u8>> {
self.buffer.extend_from_slice(chunk.as_ref());
let mut frames = Vec::new();
loop {
if self.buffer.len() < FRAME_HEADER_BYTES {
break;
}
let payload_len = u32::from_be_bytes([
self.buffer[0],
self.buffer[1],
self.buffer[2],
self.buffer[3],
]) as usize;
let total_len = FRAME_HEADER_BYTES + payload_len;
if self.buffer.len() < total_len {
break;
}
frames.push(self.buffer[FRAME_HEADER_BYTES..total_len].to_vec());
self.buffer.drain(0..total_len);
}
frames
}
}

11
vendor/cultnet-rs/src/lib.rs vendored Normal file
View file

@ -0,0 +1,11 @@
mod contracts;
mod framing;
mod replication;
mod schema_discovery;
mod security;
pub use contracts::*;
pub use framing::*;
pub use replication::*;
pub use schema_discovery::*;
pub use security::*;

440
vendor/cultnet-rs/src/replication.rs vendored Normal file
View file

@ -0,0 +1,440 @@
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use cultcache_rs::CultCache;
use cultcache_rs::CultCacheEnvelope;
use cultcache_rs::DatabaseEntry;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use crate::CultNetDocumentMutationContract;
use crate::CultNetDocumentRecord;
use crate::CultNetMessage;
use crate::CultNetRawDocumentRecord;
use crate::CultNetRawPayloadEncoding;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CultNetDocumentPutOptions {
pub stored_at: Option<String>,
pub source_runtime_id: Option<String>,
pub source_agent_id: Option<String>,
pub source_role: Option<String>,
pub tags: Option<Vec<String>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CultNetDocumentBinding {
pub schema_id: String,
pub document_type: String,
pub mutation_contract: Option<CultNetDocumentMutationContract>,
pub payload_schema_version: Option<String>,
}
impl CultNetDocumentBinding {
pub fn for_entry<T: DatabaseEntry>(payload_schema_version: impl Into<Option<String>>) -> Self {
Self {
document_type: T::TYPE.to_string(),
schema_id: T::TYPE.to_string(),
mutation_contract: None,
payload_schema_version: payload_schema_version.into(),
}
}
pub fn for_entry_with_schema_id<T: DatabaseEntry>(
schema_id: impl Into<String>,
payload_schema_version: impl Into<Option<String>>,
) -> Self {
Self {
document_type: T::TYPE.to_string(),
schema_id: schema_id.into(),
mutation_contract: None,
payload_schema_version: payload_schema_version.into(),
}
}
pub fn with_mutation_contract(mut self, contract: CultNetDocumentMutationContract) -> Self {
self.mutation_contract = Some(contract);
self
}
}
#[derive(Clone, Debug, Default)]
pub struct CultNetDocumentRegistry {
bindings_by_type: BTreeMap<String, CultNetDocumentBinding>,
bindings_by_schema_id: BTreeMap<String, CultNetDocumentBinding>,
}
impl CultNetDocumentRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn register(&mut self, binding: CultNetDocumentBinding) -> &mut Self {
self.bindings_by_schema_id
.insert(binding.schema_id.clone(), binding.clone());
self.bindings_by_type
.insert(binding.document_type.clone(), binding);
self
}
pub fn binding(&self, document_type: &str) -> Option<&CultNetDocumentBinding> {
self.bindings_by_type.get(document_type)
}
pub fn binding_by_schema_id(&self, schema_id: &str) -> Option<&CultNetDocumentBinding> {
self.bindings_by_schema_id.get(schema_id)
}
pub fn mutation_contracts(&self) -> Vec<CultNetDocumentMutationContract> {
self.bindings_by_type
.values()
.filter_map(|binding| binding.mutation_contract.clone())
.collect()
}
pub fn create_document_put_message<T>(
&self,
message_id: impl Into<String>,
record_key: impl Into<String>,
value: &T,
options: CultNetDocumentPutOptions,
) -> Result<CultNetMessage>
where
T: DatabaseEntry + Serialize + DeserializeOwned,
{
let binding = self.require_binding(T::TYPE)?;
let payload = serde_json::to_value(round_trip_typed(value)?)?;
Ok(CultNetMessage::DocumentPut {
message_id: message_id.into(),
document: CultNetDocumentRecord {
schema_id: binding.schema_id.clone(),
record_key: record_key.into(),
stored_at: options.stored_at.unwrap_or_else(now_utc_second),
payload,
source_runtime_id: options.source_runtime_id,
source_agent_id: options.source_agent_id,
source_role: options.source_role,
tags: options.tags,
},
})
}
pub fn create_document_delete_message(
&self,
message_id: impl Into<String>,
schema_id: impl Into<String>,
record_key: impl Into<String>,
) -> CultNetMessage {
CultNetMessage::DocumentDelete {
message_id: message_id.into(),
schema_id: schema_id.into(),
record_key: record_key.into(),
}
}
pub fn create_raw_document_put_message_from_envelope(
&self,
message_id: impl Into<String>,
envelope: &CultCacheEnvelope,
) -> Result<CultNetMessage> {
Ok(CultNetMessage::DocumentPutRaw {
message_id: message_id.into(),
document: self.raw_document_record_from_envelope(envelope)?,
})
}
pub fn create_raw_document_put_message<T>(
&self,
message_id: impl Into<String>,
record_key: impl Into<String>,
value: &T,
options: CultNetDocumentPutOptions,
) -> Result<CultNetMessage>
where
T: DatabaseEntry + Serialize,
{
let binding = self.require_binding(T::TYPE)?;
Ok(CultNetMessage::DocumentPutRaw {
message_id: message_id.into(),
document: CultNetRawDocumentRecord {
schema_id: binding.schema_id.clone(),
record_key: record_key.into(),
stored_at: options.stored_at.unwrap_or_else(now_utc_second),
payload_encoding: CultNetRawPayloadEncoding::Messagepack,
payload: rmp_serde::to_vec(value)?,
source_runtime_id: options.source_runtime_id,
source_agent_id: options.source_agent_id,
source_role: options.source_role,
tags: options.tags,
},
})
}
pub fn create_snapshot_response(
&self,
cache: &CultCache,
message_id: impl Into<String>,
schema_ids: Option<&[String]>,
record_keys: Option<&[String]>,
) -> Result<CultNetMessage> {
let requested_schema_ids = schema_ids.map(|items| items.iter().collect::<BTreeSet<_>>());
let requested_record_keys = record_keys.map(|items| items.iter().collect::<BTreeSet<_>>());
let mut documents = Vec::new();
for envelope in cache.snapshot() {
let binding = self.require_binding(&envelope.r#type)?;
if requested_schema_ids
.as_ref()
.is_some_and(|ids| !ids.contains(&binding.schema_id))
{
continue;
}
if requested_record_keys
.as_ref()
.is_some_and(|keys| !keys.contains(&envelope.key))
{
continue;
}
documents.push(self.document_record_from_envelope(&envelope)?);
}
Ok(CultNetMessage::SnapshotResponse {
message_id: message_id.into(),
documents,
})
}
pub fn create_raw_snapshot_response(
&self,
cache: &CultCache,
message_id: impl Into<String>,
schema_ids: Option<&[String]>,
record_keys: Option<&[String]>,
) -> Result<CultNetMessage> {
let requested_schema_ids = schema_ids.map(|items| items.iter().collect::<BTreeSet<_>>());
let requested_record_keys = record_keys.map(|items| items.iter().collect::<BTreeSet<_>>());
let mut documents = Vec::new();
for envelope in cache.snapshot() {
let binding = self.require_binding(&envelope.r#type)?;
if requested_schema_ids
.as_ref()
.is_some_and(|ids| !ids.contains(&binding.schema_id))
{
continue;
}
if requested_record_keys
.as_ref()
.is_some_and(|keys| !keys.contains(&envelope.key))
{
continue;
}
documents.push(self.raw_document_record_from_envelope(&envelope)?);
}
Ok(CultNetMessage::SnapshotResponseRaw {
message_id: message_id.into(),
documents,
})
}
pub fn apply_document_put_message<T>(
&self,
cache: &mut CultCache,
message: &CultNetMessage,
) -> Result<T>
where
T: DatabaseEntry + Serialize + DeserializeOwned,
{
let CultNetMessage::DocumentPut { document, .. } = message else {
return Err(anyhow!("expected cultnet.document_put.v0"));
};
let binding = self.require_binding(T::TYPE)?;
if document.schema_id != binding.schema_id {
return Err(anyhow!(
"schema id {:?} does not match registered Rust type {:?} schema {:?}",
document.schema_id,
T::TYPE,
binding.schema_id
));
}
let value: T = serde_json::from_value(document.payload.clone()).with_context(|| {
format!(
"failed to decode CultNet payload schema {:?} as {}",
binding.schema_id,
T::SCHEMA_NAME
)
})?;
cache.put(&document.record_key, &value)
}
pub fn apply_document_delete_message<T>(
&self,
cache: &mut CultCache,
message: &CultNetMessage,
) -> Result<bool>
where
T: DatabaseEntry,
{
let CultNetMessage::DocumentDelete {
schema_id,
record_key,
..
} = message
else {
return Err(anyhow!("expected cultnet.document_delete.v0"));
};
let binding = self.require_binding(T::TYPE)?;
if schema_id != &binding.schema_id {
return Err(anyhow!(
"schema id {:?} does not match registered Rust type {:?} schema {:?}",
schema_id,
T::TYPE,
binding.schema_id
));
}
cache.delete::<T>(record_key)
}
pub fn apply_raw_document_put_message<T>(
&self,
cache: &mut CultCache,
message: &CultNetMessage,
) -> Result<T>
where
T: DatabaseEntry + Serialize + DeserializeOwned,
{
let CultNetMessage::DocumentPutRaw { document, .. } = message else {
return Err(anyhow!("expected cultnet.document_put_raw.v0"));
};
let binding = self.require_binding(T::TYPE)?;
if document.schema_id != binding.schema_id {
return Err(anyhow!(
"schema id {:?} does not match registered Rust type {:?} schema {:?}",
document.schema_id,
T::TYPE,
binding.schema_id
));
}
cache.put_envelope::<T>(CultCacheEnvelope {
key: document.record_key.clone(),
r#type: binding.document_type.clone(),
payload: document.payload.clone(),
stored_at: document.stored_at.clone(),
schema_id: Some(binding.schema_id.clone()),
})
}
pub fn apply_snapshot_response<T>(
&self,
cache: &mut CultCache,
response: &CultNetMessage,
) -> Result<Vec<T>>
where
T: DatabaseEntry + Serialize + DeserializeOwned,
{
let CultNetMessage::SnapshotResponse { documents, .. } = response else {
return Err(anyhow!("expected cultnet.snapshot_response.v0"));
};
let mut applied = Vec::new();
let binding = self.require_binding(T::TYPE)?;
for document in documents {
if document.schema_id != binding.schema_id {
continue;
}
applied.push(self.apply_document_put_message::<T>(
cache,
&CultNetMessage::DocumentPut {
message_id: "snapshot-apply".to_string(),
document: document.clone(),
},
)?);
}
Ok(applied)
}
pub fn apply_raw_snapshot_response<T>(
&self,
cache: &mut CultCache,
response: &CultNetMessage,
) -> Result<Vec<T>>
where
T: DatabaseEntry + Serialize + DeserializeOwned,
{
let CultNetMessage::SnapshotResponseRaw { documents, .. } = response else {
return Err(anyhow!("expected cultnet.snapshot_response_raw.v0"));
};
let mut applied = Vec::new();
let binding = self.require_binding(T::TYPE)?;
for document in documents {
if document.schema_id != binding.schema_id {
continue;
}
applied.push(self.apply_raw_document_put_message::<T>(
cache,
&CultNetMessage::DocumentPutRaw {
message_id: "snapshot-apply-raw".to_string(),
document: document.clone(),
},
)?);
}
Ok(applied)
}
fn document_record_from_envelope(
&self,
envelope: &CultCacheEnvelope,
) -> Result<CultNetDocumentRecord<Value>> {
let binding = self.require_binding(&envelope.r#type)?;
let payload: Value = rmp_serde::from_slice(&envelope.payload).with_context(|| {
format!(
"failed to decode CultCache envelope {:?} at {:?} as generic CultNet payload",
envelope.r#type, envelope.key
)
})?;
Ok(CultNetDocumentRecord {
schema_id: binding.schema_id.clone(),
record_key: envelope.key.clone(),
stored_at: envelope.stored_at.clone(),
payload,
source_runtime_id: None,
source_agent_id: None,
source_role: None,
tags: None,
})
}
fn raw_document_record_from_envelope(
&self,
envelope: &CultCacheEnvelope,
) -> Result<CultNetRawDocumentRecord> {
let binding = self.require_binding(&envelope.r#type)?;
Ok(CultNetRawDocumentRecord {
schema_id: binding.schema_id.clone(),
record_key: envelope.key.clone(),
stored_at: envelope.stored_at.clone(),
payload_encoding: CultNetRawPayloadEncoding::Messagepack,
payload: envelope.payload.clone(),
source_runtime_id: None,
source_agent_id: None,
source_role: None,
tags: None,
})
}
fn require_binding(&self, document_type: &str) -> Result<&CultNetDocumentBinding> {
self.binding(document_type).ok_or_else(|| {
anyhow!("No CultNet document binding is registered for {document_type:?}")
})
}
}
fn round_trip_typed<T>(value: &T) -> Result<T>
where
T: Serialize + DeserializeOwned,
{
Ok(rmp_serde::from_slice(&rmp_serde::to_vec(value)?)?)
}
fn now_utc_second() -> String {
chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}

View file

@ -0,0 +1,389 @@
use anyhow::Result;
use anyhow::anyhow;
use serde_json::Value;
use sha2::Digest;
use sha2::Sha256;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use crate::CultNetMessage;
use crate::CultNetSchemaDescriptor;
use crate::CultNetSchemaKind;
use crate::CultNetWireContract;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CultNetSchemaRegistration {
pub schema_id: String,
pub kind: CultNetSchemaKind,
pub wire_contracts: Vec<CultNetWireContract>,
pub schema_version: Option<String>,
pub document_type: Option<String>,
pub title: Option<String>,
pub schema_json: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CultNetSchemaCatalogOptions {
pub include_schema_json: bool,
pub schema_ids: Option<Vec<String>>,
pub kinds: Option<Vec<CultNetSchemaKind>>,
}
#[derive(Clone, Debug, Default)]
pub struct CultNetSchemaRegistry {
entries: BTreeMap<String, CultNetSchemaDescriptor>,
}
impl CultNetSchemaRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn register(&mut self, registration: CultNetSchemaRegistration) -> Result<()> {
if registration.schema_id.trim().is_empty() {
return Err(anyhow!("schema_id must be non-empty"));
}
if registration.wire_contracts.is_empty() {
return Err(anyhow!("wire_contracts must contain at least one entry"));
}
let content_hash = if let Some(schema_json) = registration.schema_json.as_deref() {
canonical_schema_hash(schema_json)?
} else {
return Err(anyhow!(
"schema_json is required so discovery can advertise canonical hashes"
));
};
self.entries.insert(
registration.schema_id.clone(),
CultNetSchemaDescriptor {
schema_id: registration.schema_id,
kind: registration.kind,
schema_version: registration.schema_version,
document_type: registration.document_type,
title: registration.title,
wire_contracts: registration.wire_contracts,
content_hash,
schema_json: registration.schema_json,
},
);
Ok(())
}
pub fn get(
&self,
schema_id: &str,
include_schema_json: bool,
) -> Option<CultNetSchemaDescriptor> {
self.entries
.get(schema_id)
.map(|entry| sanitize_descriptor(entry, include_schema_json))
}
pub fn list(&self, options: &CultNetSchemaCatalogOptions) -> Vec<CultNetSchemaDescriptor> {
let requested_ids = options
.schema_ids
.as_ref()
.map(|values| values.iter().cloned().collect::<BTreeSet<_>>());
let requested_kinds = options
.kinds
.as_ref()
.map(|values| values.iter().copied().collect::<BTreeSet<_>>());
self.entries
.values()
.filter(|entry| {
if let Some(requested_ids) = &requested_ids
&& !requested_ids.contains(&entry.schema_id)
{
return false;
}
if let Some(requested_kinds) = &requested_kinds
&& !requested_kinds.contains(&entry.kind)
{
return false;
}
true
})
.map(|entry| sanitize_descriptor(entry, options.include_schema_json))
.collect()
}
pub fn create_catalog_response(&self, request: &CultNetMessage) -> Result<CultNetMessage> {
let CultNetMessage::SchemaCatalogRequest {
message_id,
include_schema_json,
schema_ids,
kinds,
} = request
else {
return Err(anyhow!(
"expected cultnet.schema_catalog_request.v0 for schema discovery"
));
};
Ok(CultNetMessage::SchemaCatalogResponse {
message_id: message_id.clone(),
schemas: self.list(&CultNetSchemaCatalogOptions {
include_schema_json: include_schema_json.unwrap_or(false),
schema_ids: schema_ids.clone(),
kinds: kinds.clone(),
}),
})
}
}
fn sanitize_descriptor(
descriptor: &CultNetSchemaDescriptor,
include_schema_json: bool,
) -> CultNetSchemaDescriptor {
let mut clone = descriptor.clone();
if !include_schema_json {
clone.schema_json = None;
}
clone
}
fn canonical_schema_hash(schema_json: &str) -> Result<String> {
let value: Value = serde_json::from_str(schema_json)?;
let canonical = stable_stringify(&value);
Ok(format!("{:x}", Sha256::digest(canonical.as_bytes())))
}
fn stable_stringify(value: &Value) -> String {
match value {
Value::Null => "null".to_string(),
Value::Bool(value) => value.to_string(),
Value::Number(value) => value.to_string(),
Value::String(value) => serde_json::to_string(value).expect("string serialization"),
Value::Array(values) => format!(
"[{}]",
values
.iter()
.map(stable_stringify)
.collect::<Vec<_>>()
.join(",")
),
Value::Object(values) => {
let mut entries = values.iter().collect::<Vec<_>>();
entries.sort_by(|(left, _), (right, _)| left.cmp(right));
format!(
"{{{}}}",
entries
.into_iter()
.map(|(key, value)| {
format!(
"{}:{}",
serde_json::to_string(key).expect("key serialization"),
stable_stringify(value)
)
})
.collect::<Vec<_>>()
.join(",")
)
}
}
}
pub fn builtin_schema_registry() -> Result<CultNetSchemaRegistry> {
let mut registry = CultNetSchemaRegistry::new();
for registration in [
schema_registration(
include_str!("../contracts/cultnet.hello.schema.json"),
CultNetSchemaKind::WireMessage,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.hello.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.document-mutation-contract.schema.json"),
CultNetSchemaKind::SharedContract,
vec![CultNetWireContract::CultNetSchemaV0],
None,
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.login.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.login.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.register.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.register.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.verify.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.verify.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.login-success.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.login_success.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.error.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.error.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.sample-change-name.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.sample.change_name.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.sample-chat.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.sample.chat.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.document-put.schema.json"),
CultNetSchemaKind::WireMessage,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.document_put.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.document-delete.schema.json"),
CultNetSchemaKind::WireMessage,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.document_delete.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.raw-document-record.schema.json"),
CultNetSchemaKind::SharedContract,
vec![CultNetWireContract::CultNetSchemaV0],
None,
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.document-put-raw.schema.json"),
CultNetSchemaKind::WireMessage,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.document_put_raw.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.snapshot-request.schema.json"),
CultNetSchemaKind::WireMessage,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.snapshot_request.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.snapshot-response.schema.json"),
CultNetSchemaKind::WireMessage,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.snapshot_response.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.snapshot-response-raw.schema.json"),
CultNetSchemaKind::WireMessage,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.snapshot_response_raw.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.schema-catalog-request.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.schema_catalog_request.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.schema-catalog-response.schema.json"),
CultNetSchemaKind::WireMessage,
vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
Some("cultnet.schema_catalog_response.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/ghostlight.agent-state.schema.json"),
CultNetSchemaKind::DocumentPayload,
vec![CultNetWireContract::CultNetSchemaV0],
Some("ghostlight.agent_state.v0"),
Some("ghostlight.agent-state"),
)?,
] {
registry.register(registration)?;
}
Ok(registry)
}
fn schema_registration(
schema_json: &str,
kind: CultNetSchemaKind,
wire_contracts: Vec<CultNetWireContract>,
schema_version: Option<&str>,
document_type: Option<&str>,
) -> Result<CultNetSchemaRegistration> {
let value: Value = serde_json::from_str(schema_json)?;
let schema_id = value
.get("$id")
.and_then(Value::as_str)
.ok_or_else(|| anyhow!("schema is missing $id"))?;
let title = value
.get("title")
.and_then(Value::as_str)
.map(str::to_string);
Ok(CultNetSchemaRegistration {
schema_id: schema_id.to_string(),
kind,
wire_contracts,
schema_version: schema_version.map(str::to_string),
document_type: document_type.map(str::to_string),
title,
schema_json: Some(schema_json.to_string()),
})
}

270
vendor/cultnet-rs/src/security.rs vendored Normal file
View file

@ -0,0 +1,270 @@
use aes_gcm::Aes256Gcm;
use aes_gcm::KeyInit;
use aes_gcm::Nonce;
use aes_gcm::aead::Aead;
use aes_gcm::aead::Payload;
use anyhow::Result;
use anyhow::anyhow;
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use chrono::DateTime;
use chrono::TimeZone;
use chrono::Utc;
use hmac::Hmac;
use hmac::Mac;
use rand::RngCore;
use sha2::Digest;
use sha2::Sha256;
const NONCE_LENGTH: usize = 12;
type HmacSha256 = Hmac<Sha256>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CultNetClientSecurityOptions {
pub connection_key: String,
encryption_key: [u8; 32],
}
impl CultNetClientSecurityOptions {
pub fn new(connection_key: impl Into<String>) -> Result<Self> {
let connection_key = connection_key.into();
if connection_key.trim().is_empty() {
return Err(anyhow!("Connection key must be provided"));
}
let encryption_key = sha256(connection_key.as_bytes());
Ok(Self {
connection_key,
encryption_key,
})
}
pub fn development() -> Self {
Self::new("gamecult-dev-connection-key").expect("development key is valid")
}
pub fn encryption_key(&self) -> [u8; 32] {
self.encryption_key
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CultNetServerSecurityOptions {
client: CultNetClientSecurityOptions,
pub session_signing_secret: String,
session_signing_key: [u8; 32],
pub is_development: bool,
}
impl CultNetServerSecurityOptions {
pub fn new(
connection_key: impl Into<String>,
session_signing_secret: impl Into<String>,
is_development: bool,
) -> Result<Self> {
let session_signing_secret = session_signing_secret.into();
if session_signing_secret.trim().is_empty() {
return Err(anyhow!("Session signing secret must be provided"));
}
Ok(Self {
client: CultNetClientSecurityOptions::new(connection_key)?,
session_signing_key: sha256(session_signing_secret.as_bytes()),
session_signing_secret,
is_development,
})
}
pub fn development() -> Self {
Self::new(
"gamecult-dev-connection-key",
"gamecult-dev-session-signing-secret",
true,
)
.expect("development security config is valid")
}
pub fn to_client_options(&self) -> CultNetClientSecurityOptions {
self.client.clone()
}
pub fn encryption_key(&self) -> [u8; 32] {
self.client.encryption_key()
}
pub fn session_signing_key(&self) -> [u8; 32] {
self.session_signing_key
}
}
pub trait CultNetEncryptionOptions {
fn encryption_key(&self) -> [u8; 32];
}
impl CultNetEncryptionOptions for CultNetClientSecurityOptions {
fn encryption_key(&self) -> [u8; 32] {
self.encryption_key()
}
}
impl CultNetEncryptionOptions for CultNetServerSecurityOptions {
fn encryption_key(&self) -> [u8; 32] {
self.encryption_key()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ValidatedCultNetSessionToken {
pub user_id: String,
pub expires_at_utc: DateTime<Utc>,
}
pub struct CultNetSecret;
impl CultNetSecret {
pub fn new_nonce() -> [u8; NONCE_LENGTH] {
let mut nonce = [0_u8; NONCE_LENGTH];
rand::rng().fill_bytes(&mut nonce);
nonce
}
pub fn encrypt_string(
input: Option<&str>,
nonce: &[u8],
options: &impl CultNetEncryptionOptions,
) -> Result<Option<Vec<u8>>> {
let Some(input) = input.filter(|value| !value.is_empty()) else {
return Ok(None);
};
Ok(Some(Self::encrypt_bytes(input.as_bytes(), nonce, options)?))
}
pub fn decrypt_string(
encrypted: Option<&[u8]>,
nonce: Option<&[u8]>,
options: &impl CultNetEncryptionOptions,
) -> Result<Option<String>> {
let (Some(encrypted), Some(nonce)) = (encrypted, nonce) else {
return Ok(None);
};
let bytes = Self::decrypt_bytes(encrypted, nonce, options)?;
Ok(Some(String::from_utf8(bytes)?))
}
pub fn encrypt_bytes(
input: &[u8],
nonce: &[u8],
options: &impl CultNetEncryptionOptions,
) -> Result<Vec<u8>> {
let nonce = validate_nonce(nonce)?;
let cipher = Aes256Gcm::new_from_slice(&options.encryption_key())?;
let encrypted = cipher
.encrypt(
Nonce::from_slice(nonce),
Payload {
msg: input,
aad: &[],
},
)
.map_err(|_| anyhow!("AES-GCM encryption failed"))?;
Ok(encrypted)
}
pub fn decrypt_bytes(
encrypted: &[u8],
nonce: &[u8],
options: &impl CultNetEncryptionOptions,
) -> Result<Vec<u8>> {
let nonce = validate_nonce(nonce)?;
let cipher = Aes256Gcm::new_from_slice(&options.encryption_key())?;
cipher
.decrypt(
Nonce::from_slice(nonce),
Payload {
msg: encrypted,
aad: &[],
},
)
.map_err(|_| anyhow!("AES-GCM decryption failed"))
}
pub fn create_session_token(
user_id: &str,
expires_at_utc: DateTime<Utc>,
options: &CultNetServerSecurityOptions,
) -> Result<String> {
let payload = format!("{}|{}", user_id, expires_at_utc.timestamp());
let signature = hmac_sha256(&options.session_signing_key(), payload.as_bytes())?;
Ok(format!(
"{}.{}",
URL_SAFE_NO_PAD.encode(payload.as_bytes()),
URL_SAFE_NO_PAD.encode(signature)
))
}
pub fn try_validate_session_token(
token: Option<&str>,
options: &CultNetServerSecurityOptions,
) -> Result<Option<ValidatedCultNetSessionToken>> {
let Some(token) = token.filter(|value| !value.trim().is_empty()) else {
return Ok(None);
};
let Some((payload_part, signature_part)) = token.split_once('.') else {
return Ok(None);
};
let payload = URL_SAFE_NO_PAD.decode(payload_part)?;
let signature = URL_SAFE_NO_PAD.decode(signature_part)?;
let expected = hmac_sha256(&options.session_signing_key(), &payload)?;
if !constant_time_eq(&signature, &expected) {
return Ok(None);
}
let payload = String::from_utf8(payload)?;
let Some((user_id, expires_at)) = payload.split_once('|') else {
return Ok(None);
};
let expires_at_seconds: i64 = expires_at.parse()?;
let Some(expires_at_utc) = Utc.timestamp_opt(expires_at_seconds, 0).single() else {
return Ok(None);
};
if expires_at_utc <= Utc::now() {
return Ok(None);
}
Ok(Some(ValidatedCultNetSessionToken {
user_id: user_id.to_string(),
expires_at_utc,
}))
}
pub fn to_base64_url(input: &[u8]) -> String {
URL_SAFE_NO_PAD.encode(input)
}
pub fn from_base64_url(input: &str) -> Result<Vec<u8>> {
Ok(URL_SAFE_NO_PAD.decode(input)?)
}
}
fn validate_nonce(nonce: &[u8]) -> Result<&[u8]> {
if nonce.len() != NONCE_LENGTH {
return Err(anyhow!("Invalid nonce"));
}
Ok(nonce)
}
fn sha256(input: &[u8]) -> [u8; 32] {
Sha256::digest(input).into()
}
fn hmac_sha256(key: &[u8], input: &[u8]) -> Result<Vec<u8>> {
let mut mac = <HmacSha256 as Mac>::new_from_slice(key)?;
mac.update(input);
Ok(mac.finalize().into_bytes().to_vec())
}
fn constant_time_eq(left: &[u8], right: &[u8]) -> bool {
if left.len() != right.len() {
return false;
}
left.iter()
.zip(right)
.fold(0_u8, |acc, (left, right)| acc | (left ^ right))
== 0
}

397
vendor/cultnet-rs/tests/cultnet.rs vendored Normal file
View file

@ -0,0 +1,397 @@
use anyhow::Result;
use chrono::Duration;
use cultcache_rs::CultCache;
use cultcache_rs::DatabaseEntry;
use cultcache_rs::SingleFileMessagePackBackingStore;
use cultnet_rs::CultNetClientSecurityOptions;
use cultnet_rs::CultNetDocumentBinding;
use cultnet_rs::CultNetDocumentMutationContract;
use cultnet_rs::CultNetDocumentOperation;
use cultnet_rs::CultNetDocumentPutOptions;
use cultnet_rs::CultNetDocumentRegistry;
use cultnet_rs::CultNetMessage;
use cultnet_rs::CultNetMutationAuthority;
use cultnet_rs::CultNetSchemaKind;
use cultnet_rs::CultNetSchemaRegistry;
use cultnet_rs::CultNetSecret;
use cultnet_rs::CultNetServerSecurityOptions;
use cultnet_rs::CultNetWireContract;
use cultnet_rs::LengthPrefixedMessageFramer;
use cultnet_rs::builtin_schema_registry;
use cultnet_rs::decode_cultnet_message_from_slice;
use cultnet_rs::encode_cultnet_message_for_wire;
use cultnet_rs::encode_cultnet_message_to_vec;
use cultnet_rs::encode_frame;
use pretty_assertions::assert_eq;
const TS_HELLO_FRAME: &[u8] = include_bytes!("fixtures/cultnet-ts-hello.frame");
const TS_LEGACY_LOGIN_FRAME: &[u8] = include_bytes!("fixtures/cultnet-ts-legacy-login.frame");
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(
type = "ghostlight.agent-state",
schema = "GhostlightAgentStateFixture"
)]
struct GhostlightAgentStateFixture {
#[cultcache(key = 0)]
schema_version: String,
#[cultcache(key = 1)]
agent_id: String,
#[cultcache(key = 2)]
display_name: String,
}
#[test]
fn security_helpers_round_trip_encrypted_strings_and_validate_sessions() -> Result<()> {
let server_security = CultNetServerSecurityOptions::development();
let client_security = server_security.to_client_options();
let nonce = CultNetSecret::new_nonce();
let encrypted = CultNetSecret::encrypt_string(Some("hello"), &nonce, &client_security)?
.expect("non-empty input encrypts");
assert_eq!(
CultNetSecret::decrypt_string(Some(&encrypted), Some(&nonce), &server_security)?,
Some("hello".to_string())
);
let token = CultNetSecret::create_session_token(
"runtime-face",
chrono::Utc::now() + Duration::minutes(1),
&server_security,
)?;
let validated = CultNetSecret::try_validate_session_token(Some(&token), &server_security)?
.expect("token validates before expiry");
assert_eq!(validated.user_id, "runtime-face");
Ok(())
}
#[test]
fn cultnet_schema_messages_round_trip_through_messagepack_frames() -> Result<()> {
let message = CultNetMessage::Hello {
runtime_id: "voidbot-main".to_string(),
runtime_kind: "rust-worker".to_string(),
agent_id: Some("void".to_string()),
role: None,
display_name: Some("Void".to_string()),
supported_document_types: Some(vec!["ghostlight.agent-state".to_string()]),
supported_mutation_contracts: Some(vec![CultNetDocumentMutationContract {
document_type: "ghostlight.agent-state".to_string(),
payload_schema_version: Some("ghostlight.agent_state.v0".to_string()),
operations: vec![
CultNetDocumentOperation::Snapshot,
CultNetDocumentOperation::DocumentPut,
],
authority: CultNetMutationAuthority::Coordinator,
intent_document_types: Some(vec!["ghostlight.agent-state.intent.v0".to_string()]),
receipt_document_types: Some(vec!["ghostlight.agent-state.receipt.v0".to_string()]),
notes: Some(vec![
"Face may request bounded memory mutation; coordinator reviews authority."
.to_string(),
]),
}]),
supported_message_versions: None,
supports_schema_catalog: Some(true),
};
let payload = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)?;
let frame = encode_frame(&payload)?;
assert_eq!(&frame[..4], &(payload.len() as u32).to_be_bytes());
let mut framer = LengthPrefixedMessageFramer::new();
assert!(framer.push(&frame[..2]).is_empty());
let frames = framer.push(&frame[2..]);
assert_eq!(frames.len(), 1);
let decoded =
decode_cultnet_message_from_slice(&frames[0], CultNetWireContract::CultNetSchemaV0)?;
assert_eq!(decoded, message);
Ok(())
}
#[test]
fn legacy_gamecult_networking_contract_round_trips_login_union() -> Result<()> {
let message = CultNetMessage::Login {
nonce: CultNetSecret::to_base64_url(b"nonce"),
auth: CultNetSecret::to_base64_url(b"auth"),
password: CultNetSecret::to_base64_url(b"password"),
};
let wire =
encode_cultnet_message_for_wire(&message, CultNetWireContract::GameCultNetworkingV0)?;
let items = wire.as_array().expect("legacy union is an array");
assert_eq!(items[0].as_i64(), Some(0));
assert_eq!(
items[1].as_array().unwrap()[0].as_slice(),
Some(&b"nonce"[..])
);
let bytes = rmp_serde::to_vec(&wire)?;
let decoded =
decode_cultnet_message_from_slice(&bytes, CultNetWireContract::GameCultNetworkingV0)?;
assert_eq!(decoded, message);
Ok(())
}
#[test]
fn rust_decodes_typescript_generated_cultnet_frames() -> Result<()> {
let mut framer = LengthPrefixedMessageFramer::new();
let hello_frames = framer.push(TS_HELLO_FRAME);
assert_eq!(hello_frames.len(), 1);
let hello =
decode_cultnet_message_from_slice(&hello_frames[0], CultNetWireContract::CultNetSchemaV0)?;
assert_eq!(
hello,
CultNetMessage::Hello {
runtime_id: "voidbot-main".to_string(),
runtime_kind: "node-worker".to_string(),
agent_id: Some("void".to_string()),
role: None,
display_name: Some("Void".to_string()),
supported_document_types: Some(vec!["ghostlight.agent-state".to_string()]),
supported_mutation_contracts: None,
supported_message_versions: None,
supports_schema_catalog: None,
}
);
let mut framer = LengthPrefixedMessageFramer::new();
let login_frames = framer.push(TS_LEGACY_LOGIN_FRAME);
assert_eq!(login_frames.len(), 1);
let login = decode_cultnet_message_from_slice(
&login_frames[0],
CultNetWireContract::GameCultNetworkingV0,
)?;
assert_eq!(
login,
CultNetMessage::Login {
nonce: "bm9uY2U".to_string(),
auth: "YXV0aA".to_string(),
password: "cGFzc3dvcmQ".to_string(),
}
);
Ok(())
}
#[test]
fn document_registry_advertises_mutation_contracts_with_bindings() -> Result<()> {
let mut registry = CultNetDocumentRegistry::new();
let contract = CultNetDocumentMutationContract {
document_type: "ghostlight.agent-state".to_string(),
payload_schema_version: Some("ghostlight.agent_state.v0".to_string()),
operations: vec![
CultNetDocumentOperation::Snapshot,
CultNetDocumentOperation::IntentSubmit,
CultNetDocumentOperation::ReceiptWatch,
],
authority: CultNetMutationAuthority::Coordinator,
intent_document_types: Some(vec!["ghostlight.agent-state.intent.v0".to_string()]),
receipt_document_types: Some(vec!["ghostlight.agent-state.receipt.v0".to_string()]),
notes: Some(vec![
"Intent documents are reviewed before cache mutation.".to_string(),
]),
};
registry.register(
CultNetDocumentBinding::for_entry::<GhostlightAgentStateFixture>(
"ghostlight.agent_state.v0".to_string(),
)
.with_mutation_contract(contract.clone()),
);
assert_eq!(registry.mutation_contracts(), vec![contract]);
Ok(())
}
#[test]
fn document_registry_replicates_typed_cultcache_state() -> Result<()> {
let temp = tempfile::tempdir()?;
let origin_store = temp.path().join("origin.cc");
let target_store = temp.path().join("target.cc");
let payload = GhostlightAgentStateFixture {
schema_version: "ghostlight.agent_state.v0".to_string(),
agent_id: "epiphany.face".to_string(),
display_name: "Face".to_string(),
};
let mut registry = CultNetDocumentRegistry::new();
registry.register(CultNetDocumentBinding::for_entry::<
GhostlightAgentStateFixture,
>("ghostlight.agent_state.v0".to_string()));
let mut origin = CultCache::new();
origin.register_entry_type::<GhostlightAgentStateFixture>()?;
origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store));
origin.pull_all_backing_stores()?;
origin.put("epiphany.face", &payload)?;
let snapshot = registry.create_snapshot_response(&origin, "snapshot-1", None, None)?;
let mut target = CultCache::new();
target.register_entry_type::<GhostlightAgentStateFixture>()?;
target.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&target_store));
target.pull_all_backing_stores()?;
let applied =
registry.apply_snapshot_response::<GhostlightAgentStateFixture>(&mut target, &snapshot)?;
assert_eq!(applied, vec![payload.clone()]);
assert_eq!(
target.get_required::<GhostlightAgentStateFixture>("epiphany.face")?,
payload
);
let direct_put = registry.create_document_put_message(
"put-1",
"epiphany.face",
&GhostlightAgentStateFixture {
display_name: "Face Prime".to_string(),
..payload
},
CultNetDocumentPutOptions::default(),
)?;
let updated = registry
.apply_document_put_message::<GhostlightAgentStateFixture>(&mut target, &direct_put)?;
assert_eq!(updated.display_name, "Face Prime");
Ok(())
}
#[test]
fn raw_snapshot_replication_preserves_messagepack_payload_bytes() -> Result<()> {
let temp = tempfile::tempdir()?;
let origin_store = temp.path().join("origin-raw.cc");
let target_store = temp.path().join("target-raw.cc");
let payload = GhostlightAgentStateFixture {
schema_version: "ghostlight.agent_state.v0".to_string(),
agent_id: "epiphany.face".to_string(),
display_name: "Face".to_string(),
};
let mut registry = CultNetDocumentRegistry::new();
registry.register(CultNetDocumentBinding::for_entry::<
GhostlightAgentStateFixture,
>("ghostlight.agent_state.v0".to_string()));
let mut origin = CultCache::new();
origin.register_entry_type::<GhostlightAgentStateFixture>()?;
origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store));
origin.pull_all_backing_stores()?;
origin.put("epiphany.face", &payload)?;
let raw_snapshot =
registry.create_raw_snapshot_response(&origin, "raw-snapshot-1", None, None)?;
let source_envelope =
origin.get_required_envelope::<GhostlightAgentStateFixture>("epiphany.face")?;
let mut target = CultCache::new();
target.register_entry_type::<GhostlightAgentStateFixture>()?;
target.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&target_store));
target.pull_all_backing_stores()?;
let applied = registry
.apply_raw_snapshot_response::<GhostlightAgentStateFixture>(&mut target, &raw_snapshot)?;
let target_envelope =
target.get_required_envelope::<GhostlightAgentStateFixture>("epiphany.face")?;
assert_eq!(applied, vec![payload.clone()]);
assert_eq!(target_envelope.payload, source_envelope.payload);
assert_eq!(
target.get_required::<GhostlightAgentStateFixture>("epiphany.face")?,
payload
);
Ok(())
}
#[test]
fn client_security_keeps_the_connection_key_visible_without_exposing_cipher_logic() -> Result<()> {
let options = CultNetClientSecurityOptions::development();
assert_eq!(options.connection_key, "gamecult-dev-connection-key");
assert_ne!(options.encryption_key(), [0_u8; 32]);
Ok(())
}
#[test]
fn builtin_schema_registry_advertises_canonical_ghostlight_schema_without_inline_body_by_default()
-> Result<()> {
let registry = builtin_schema_registry()?;
let response = registry.create_catalog_response(&CultNetMessage::SchemaCatalogRequest {
message_id: "catalog-1".to_string(),
include_schema_json: None,
schema_ids: None,
kinds: None,
})?;
let CultNetMessage::SchemaCatalogResponse { schemas, .. } = response else {
panic!("expected catalog response");
};
let ghostlight = schemas
.iter()
.find(|schema| schema.document_type.as_deref() == Some("ghostlight.agent-state"))
.expect("ghostlight agent-state schema is advertised");
assert_eq!(ghostlight.kind, CultNetSchemaKind::DocumentPayload);
assert_eq!(
ghostlight.schema_version.as_deref(),
Some("ghostlight.agent_state.v0")
);
assert_eq!(ghostlight.schema_json, None);
assert!(!ghostlight.content_hash.is_empty());
Ok(())
}
#[test]
fn schema_discovery_round_trips_over_legacy_gamecult_contract_when_inline_schemas_are_requested()
-> Result<()> {
let registry = {
let mut registry = CultNetSchemaRegistry::new();
registry.register(cultnet_rs::CultNetSchemaRegistration {
schema_id: "https://example.test/contracts/example.schema.json".to_string(),
kind: CultNetSchemaKind::SharedContract,
wire_contracts: vec![
CultNetWireContract::CultNetSchemaV0,
CultNetWireContract::GameCultNetworkingV0,
],
schema_version: None,
document_type: None,
title: Some("Example Schema".to_string()),
schema_json: Some(
r#"{
"$schema":"https://json-schema.org/draft/2020-12/schema",
"$id":"https://example.test/contracts/example.schema.json",
"title":"Example Schema",
"type":"object",
"properties":{"value":{"type":"string"}},
"required":["value"],
"additionalProperties":false
}"#
.to_string(),
),
})?;
registry
};
let response = registry.create_catalog_response(&CultNetMessage::SchemaCatalogRequest {
message_id: "catalog-legacy".to_string(),
include_schema_json: Some(true),
schema_ids: None,
kinds: None,
})?;
let wire =
encode_cultnet_message_for_wire(&response, CultNetWireContract::GameCultNetworkingV0)?;
let bytes = rmp_serde::to_vec(&wire)?;
let decoded =
decode_cultnet_message_from_slice(&bytes, CultNetWireContract::GameCultNetworkingV0)?;
let CultNetMessage::SchemaCatalogResponse {
message_id,
schemas,
} = decoded
else {
panic!("expected legacy schema catalog response");
};
assert_eq!(message_id, "catalog-legacy");
assert_eq!(
schemas[0].schema_id,
"https://example.test/contracts/example.schema.json"
);
assert!(
schemas[0]
.schema_json
.as_deref()
.is_some_and(|schema| schema.contains("\"value\""))
);
Ok(())
}

Binary file not shown.

Binary file not shown.