Move Idunn health lanes to shared RUDP

This commit is contained in:
Meta Vens 2026-06-16 03:59:34 +01:00
commit edca92d35f
17 changed files with 2914 additions and 386 deletions

View file

@ -1,8 +1,10 @@
use anyhow::{Context, Result, anyhow};
use cultmesh_rs::{CultMesh, CultMeshNode, CultMeshNodeOptions};
use cultnet_rs::{
CULTNET_RUDP_PROTOCOL_ID, CultNetMessage, CultNetRawPayloadEncoding, CultNetRudpOptions,
recv_cultnet_message_rudp, send_cultnet_message_rudp,
CultNetMessage, CultNetRawPayloadEncoding, CultNetRudpPacketType, CultNetRudpSession,
CultNetRudpSessionOptions, CultNetRudpSocketTransportConnection,
CultNetRudpSocketTransportOptions, CultNetWireContract, decode_cultnet_message_from_slice,
decode_rudp_packet, encode_cultnet_message_to_vec, encode_rudp_packet,
};
use odin_core::{
IdunnCommandBoundaryRecord, IdunnDaemonHealthRecord, IdunnDaemonSurgeryPlanRecord,
@ -10,6 +12,7 @@ use odin_core::{
IdunnOperatorAlarmRecord, IdunnRestartResultRecord, IdunnRudpHealthIngressRecord,
IdunnRuntimeTransportCheckRecord, IdunnSwarmSurgeryPlanRecord, OdinDocuments, plan_keepalive,
};
use std::collections::HashMap;
use std::env;
use std::net::{SocketAddr, UdpSocket};
use std::path::PathBuf;
@ -19,6 +22,9 @@ use std::thread;
use std::time::Duration;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
const CULTNET_RUDP_PROTOCOL_ID: &str = "cultnet.transport.rudp.v0";
const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001;
#[derive(Clone, Debug)]
struct DaemonTarget {
daemon_id: String,
@ -416,29 +422,56 @@ fn runtime_transport_check(observed_at: &str) -> IdunnRuntimeTransportCheckRecor
}
fn verify_rudp_loopback() -> Result<String> {
let sender = UdpSocket::bind("127.0.0.1:0")?;
let receiver = UdpSocket::bind("127.0.0.1:0")?;
let receiver_addr = receiver.local_addr()?;
let options = CultNetRudpOptions {
ack_timeout: Duration::from_millis(100),
retries: 3,
..CultNetRudpOptions::default()
};
let receiver_options = options.clone();
let server_socket = UdpSocket::bind("127.0.0.1:0")?;
server_socket.set_read_timeout(Some(Duration::from_millis(100)))?;
let server_addr = server_socket.local_addr()?;
let client_socket = UdpSocket::bind("127.0.0.1:0")?;
client_socket.set_read_timeout(Some(Duration::from_millis(100)))?;
let handle = thread::spawn(move || -> Result<()> {
let (message, _source, _transfer_id) =
recv_cultnet_message_rudp(&receiver, &receiver_options)?;
match message {
CultNetMessage::Hello { runtime_id, .. } if runtime_id == "idunn-daemon" => Ok(()),
other => Err(anyhow!("unexpected loopback CultNet message: {other:?}")),
let mut server = CultNetRudpSocketTransportConnection::new(
CultNetRudpSocketTransportOptions::server(
"idunn-rudp-loopback-server",
server_socket,
IDUNN_HEALTH_RUDP_CONNECTION_ID,
),
)?;
let deadline = Instant::now() + Duration::from_secs(2);
loop {
if let Some(frame) = server.receive_once()? {
let message =
decode_cultnet_message_from_slice(&frame.payload, CultNetWireContract::CultNetSchemaV0)?;
return match message {
CultNetMessage::Hello { runtime_id, .. } if runtime_id == "idunn-daemon" => {
Ok(())
}
other => Err(anyhow!("unexpected loopback CultNet message: {other:?}")),
};
}
if Instant::now() >= deadline {
return Err(anyhow!("Idunn RUDP loopback timed out waiting for schema frame"));
}
}
});
let transfer_id = send_cultnet_message_rudp(
&sender,
receiver_addr,
&CultNetMessage::Hello {
let mut client = CultNetRudpSocketTransportConnection::new(
CultNetRudpSocketTransportOptions::client(
"idunn-rudp-loopback-client",
client_socket,
server_addr,
IDUNN_HEALTH_RUDP_CONNECTION_ID,
),
)?;
client.connect(Vec::new())?;
let deadline = Instant::now() + Duration::from_secs(2);
while !client.connected() {
let _ = client.receive_once()?;
client.poll_resends()?;
if Instant::now() >= deadline {
return Err(anyhow!("Idunn RUDP loopback timed out during handshake"));
}
}
let message = CultNetMessage::Hello {
runtime_id: "idunn-daemon".to_string(),
runtime_kind: "keepalive".to_string(),
agent_id: None,
@ -447,14 +480,15 @@ fn verify_rudp_loopback() -> Result<String> {
supported_document_types: Some(vec!["idunn.runtime_transport_check".to_string()]),
supported_mutation_contracts: None,
supported_message_versions: Some(vec!["cultnet.hello.v0".to_string()]),
transport_profiles: Some(vec![client.profile.clone()]),
supports_schema_catalog: Some(false),
},
&options,
)?;
};
let payload = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)?;
client.send("schema", payload)?;
handle
.join()
.map_err(|_| anyhow!("Idunn RUDP loopback receiver thread panicked"))??;
Ok(transfer_id)
Ok(format!("connection:{IDUNN_HEALTH_RUDP_CONNECTION_ID:08x}"))
}
fn start_rudp_health_ingress(
@ -491,7 +525,7 @@ fn start_rudp_health_ingress(
&ingress_id,
local_addr,
"active",
"listening for raw idunn.daemon_health documents over CultNet RUDP",
"listening for idunn.daemon_health document frames over CultNet RUDP schema channel",
observed_at,
);
with_store_node(options, store_lock, |node| {
@ -533,35 +567,59 @@ fn run_rudp_health_ingress_loop(
options: CommonOptions,
store_lock: Arc<Mutex<()>>,
) -> Result<()> {
let rudp_options = CultNetRudpOptions {
ack_timeout: Duration::from_millis(100),
retries: 3,
..CultNetRudpOptions::default()
};
let local_addr = socket.local_addr()?;
let mut sessions: HashMap<SocketAddr, CultNetRudpSession> = HashMap::new();
let mut buffer = vec![0_u8; 65_535];
loop {
match recv_cultnet_message_rudp(&socket, &rudp_options) {
Ok((message, source, transfer_id)) => {
match socket.recv_from(&mut buffer) {
Ok((size, source)) => {
let observed_at = timestamp()?;
match health_from_rudp_message(&message) {
Ok(mut health) => {
health.publication_source = "daemon-published".to_string();
health.transport = CULTNET_RUDP_PROTOCOL_ID.to_string();
with_store_node(&options, &store_lock, |node| {
node.put(&health.daemon_id, &health)?;
Ok(())
})?;
println!(
"Idunn accepted RUDP health for {} from {} transfer {}.",
health.daemon_id, source, transfer_id
);
match handle_rudp_health_datagram(&socket, &mut sessions, &buffer[..size], source)
{
Ok(frames) => {
for frame in frames {
let message = decode_cultnet_message_from_slice(
&frame.payload,
CultNetWireContract::CultNetSchemaV0,
)?;
match health_from_rudp_message(&message) {
Ok(mut health) => {
health.publication_source = "daemon-published".to_string();
health.transport = CULTNET_RUDP_PROTOCOL_ID.to_string();
with_store_node(&options, &store_lock, |node| {
node.put(&health.daemon_id, &health)?;
Ok(())
})?;
println!(
"Idunn accepted RUDP health for {} from {} over {}.",
health.daemon_id, source, frame.channel_id
);
}
Err(error) => {
let failed = rudp_health_ingress_record(
"idunn-rudp-health-ingress",
local_addr,
"degraded",
&format!(
"rejected RUDP health schema frame from {source}: {error}"
),
&observed_at,
);
with_store_node(&options, &store_lock, |node| {
node.put(&failed.ingress_id, &failed)?;
Ok(())
})?;
}
}
}
}
Err(error) => {
let failed = rudp_health_ingress_record(
"idunn-rudp-health-ingress",
socket.local_addr()?,
local_addr,
"degraded",
&format!("rejected RUDP health message from {source}: {error}"),
&format!("rejected RUDP health datagram from {source}: {error}"),
&observed_at,
);
with_store_node(&options, &store_lock, |node| {
@ -572,17 +630,70 @@ fn run_rudp_health_ingress_loop(
}
}
Err(error)
if error.downcast_ref::<std::io::Error>().is_some_and(|io| {
matches!(
io.kind(),
std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
)
}) => {}
Err(error) => return Err(error),
if matches!(
error.kind(),
std::io::ErrorKind::WouldBlock
| std::io::ErrorKind::TimedOut
| std::io::ErrorKind::ConnectionReset
| std::io::ErrorKind::ConnectionAborted
) => {}
Err(error) => return Err(error.into()),
}
}
}
fn handle_rudp_health_datagram(
socket: &UdpSocket,
sessions: &mut HashMap<SocketAddr, CultNetRudpSession>,
wire: &[u8],
source: SocketAddr,
) -> Result<Vec<cultnet_rs::CultNetTransportFrame>> {
let packet = decode_rudp_packet(wire)?;
if packet.connection_id != IDUNN_HEALTH_RUDP_CONNECTION_ID {
return Err(anyhow!(
"unexpected RUDP connection id {:08x}",
packet.connection_id
));
}
let session = sessions.entry(source).or_insert_with(|| {
CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: IDUNN_HEALTH_RUDP_CONNECTION_ID,
initial_sequence: 1,
resend_delay_ms: 100,
max_pending_reliable_packets: None,
})
});
let now = unix_epoch_millis()?;
if packet.packet_type == CultNetRudpPacketType::Connect {
let accept = session.accept_connect(&packet, now, Vec::new())?;
socket.send_to(&encode_rudp_packet(&accept)?, source)?;
return Ok(Vec::new());
}
let result = session.receive(&packet, now)?;
if let Some(reply) = result.reply {
socket.send_to(&encode_rudp_packet(&reply)?, source)?;
}
let frames = result
.delivered
.into_iter()
.map(|frame| cultnet_rs::CultNetTransportFrame {
channel_id: frame.channel_id,
payload: frame.payload,
})
.collect::<Vec<_>>();
if packet.packet_type == CultNetRudpPacketType::Accept || !frames.is_empty() {
let ack = session.create_ack();
socket.send_to(&encode_rudp_packet(&ack)?, source)?;
}
if result.disconnected || !frames.is_empty() {
sessions.remove(&source);
}
Ok(frames)
}
fn health_from_rudp_message(message: &CultNetMessage) -> Result<IdunnDaemonHealthRecord> {
let CultNetMessage::DocumentPutRaw { document, .. } = message else {
return Err(anyhow!("expected cultnet.document_put_raw.v0"));
@ -636,9 +747,9 @@ fn swarm_surgery_plan(
targets: &[DaemonTarget],
updated_at: &str,
) -> IdunnSwarmSurgeryPlanRecord {
let has_odin = targets.iter().any(|target| target.daemon_id == "odin");
let next_target = if has_odin {
"odin"
let has_stonks = targets.iter().any(|target| target.daemon_id == "stonks");
let next_target = if has_stonks {
"stonks"
} else if let Some(target) = targets.iter().find(|target| target.enabled) {
target.daemon_id.as_str()
} else {
@ -672,11 +783,11 @@ fn swarm_surgery_plan(
"5. Delete or demote compatibility probes once every target has daemon-owned publication and advertised lifecycle authority.".to_string(),
],
current_phase:
"Phase 5: move Odin's own provider health from compatibility command evidence to daemon-published RUDP state."
"Phase 6: move Stonks market-provider health from HTTP compatibility evidence to daemon-published RUDP state."
.to_string(),
next_target: next_target.to_string(),
cut_line:
"All Muninn lanes now publish daemon-owned RUDP health. Odin remains the next critical provider-health cut before its local command probe can be demoted."
"Muninn, Idunn, and Odin now exercise daemon-owned RUDP health locally. Stonks is the next TypeScript provider cut; Raven task repairs remain queued until the host is reachable."
.to_string(),
verification_layer:
"CultMesh keepalive store records plus live Idunn decision cycles, not process exit codes or chat summaries."
@ -1635,6 +1746,14 @@ fn timestamp() -> Result<String> {
Ok(format!("unix:{seconds}"))
}
fn unix_epoch_millis() -> Result<u64> {
let millis = SystemTime::now()
.duration_since(UNIX_EPOCH)
.context("system clock is before Unix epoch")?
.as_millis();
u64::try_from(millis).context("system clock milliseconds exceeded u64")
}
fn help_text() -> &'static str {
"Usage: idunn --daemon <id> [--name <name>] [--verse <verse>] [--store <path>] [--health-command <command>] [--deploy-command <command>] [--restart-command <command>] [--operator-alarm-command <command>] [--rudp-health-bind <addr|none>] [--execute] [--interval-seconds <seconds>] [--command-timeout-seconds <seconds>] [--repo-root <path>] [--swarm-profile <profile>]\n\nIdunn can run one manual daemon probe lane with --daemon, or one built-in swarm supervisor with --swarm-profile starfire-local."
}
@ -1688,7 +1807,7 @@ mod tests {
}
#[test]
fn swarm_surgery_plan_names_odin_after_muninn_cuts() {
fn swarm_surgery_plan_names_stonks_after_odin_cut() {
let starfire_muninn = DaemonTarget {
daemon_id: "starfire-muninn".to_string(),
verse_id: "starfire.local".to_string(),
@ -1742,18 +1861,29 @@ mod tests {
enabled: true,
interval_seconds: 30,
};
let stonks = DaemonTarget {
daemon_id: "stonks".to_string(),
verse_id: "starfire.local".to_string(),
name: "Stonks".to_string(),
health_contract: health_contract("stonks.cultnet-rudp-market-health", "failed"),
health_command: Some("health-stonks.cmd".to_string()),
deploy_command: None,
restart_command: Some("restart-stonks.cmd".to_string()),
enabled: true,
interval_seconds: 30,
};
let plan = swarm_surgery_plan(
"starfire-local",
&[odin, starfire_muninn, nightwing_muninn, raven_muninn],
&[odin, stonks, starfire_muninn, nightwing_muninn, raven_muninn],
"unix:100",
);
assert_eq!(plan.plan_id, "swarm-surgery:starfire-local");
assert_eq!(plan.status, "active-transport-migration");
assert_eq!(plan.next_target, "odin");
assert!(plan.current_phase.contains("Odin"));
assert!(plan.cut_line.contains("All Muninn lanes"));
assert_eq!(plan.next_target, "stonks");
assert!(plan.current_phase.contains("Stonks"));
assert!(plan.cut_line.contains("Muninn, Idunn, and Odin"));
assert!(plan.verification_layer.contains("CultMesh keepalive store"));
assert!(
plan.invariants

View file

@ -4,8 +4,9 @@ use cultmesh_rs::{
CultMeshStreamCatalog, CultMeshStreamClock, CultMeshStreamDescriptor, CultMeshStreamKind,
};
use cultnet_rs::{
CULTNET_RUDP_PROTOCOL_ID, CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding,
CultNetRudpOptions, send_cultnet_message_rudp,
CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding,
CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract,
encode_cultnet_message_to_vec,
};
use odin_core::{
IdunnDaemonHealthRecord, MuninnCaptureStreamRecord, MuninnMoveControllerStateRecord,
@ -34,6 +35,9 @@ use std::os::unix::fs::OpenOptionsExt;
#[cfg(windows)]
use std::os::windows::ffi::OsStrExt;
const CULTNET_RUDP_PROTOCOL_ID: &str = "cultnet.transport.rudp.v0";
const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001;
#[derive(Clone, Debug, PartialEq, Eq)]
enum Mode {
Serve,
@ -2069,13 +2073,32 @@ fn publish_idunn_rudp_health(
};
let socket = UdpSocket::bind(bind_address)
.with_context(|| format!("binding Muninn RUDP sender at {bind_address}"))?;
send_cultnet_message_rudp(
&socket,
options.endpoint,
&message,
&CultNetRudpOptions::default(),
)
.with_context(|| format!("sending Idunn health to {}", options.endpoint))?;
socket.set_read_timeout(Some(Duration::from_millis(100)))?;
let mut transport = CultNetRudpSocketTransportConnection::new(
CultNetRudpSocketTransportOptions::client(
"muninn-daemon",
socket,
options.endpoint,
IDUNN_HEALTH_RUDP_CONNECTION_ID,
),
)?;
transport.connect(Vec::new())?;
let deadline = Instant::now() + Duration::from_secs(2);
while !transport.connected() {
let _ = transport.receive_once()?;
transport.poll_resends()?;
if Instant::now() >= deadline {
return Err(anyhow!(
"timed out connecting Muninn RUDP sender to {}",
options.endpoint
));
}
}
let payload = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)
.context("encoding Idunn health CultNet message")?;
transport
.send("schema", payload)
.with_context(|| format!("sending Idunn health to {}", options.endpoint))?;
Ok(())
}
@ -2796,16 +2819,33 @@ mod tests {
#[test]
fn idunn_rudp_health_publisher_sends_raw_daemon_health_document() {
let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
receiver
.set_read_timeout(Some(Duration::from_secs(1)))
.unwrap();
let receiver_addr = receiver.local_addr().unwrap();
let handle = thread::spawn(move || {
cultnet_rs::recv_cultnet_message_rudp(
&receiver,
&CultNetRudpOptions {
ack_timeout: Duration::from_secs(1),
..CultNetRudpOptions::default()
},
let mut transport = CultNetRudpSocketTransportConnection::new(
CultNetRudpSocketTransportOptions::server(
"idunn-test",
receiver,
IDUNN_HEALTH_RUDP_CONNECTION_ID,
),
)
.unwrap()
.unwrap();
let deadline = Instant::now() + Duration::from_secs(2);
loop {
if let Some(frame) = transport.receive_once().unwrap() {
return cultnet_rs::decode_cultnet_message_from_slice(
&frame.payload,
CultNetWireContract::CultNetSchemaV0,
)
.unwrap();
}
transport.poll_resends().unwrap();
if Instant::now() >= deadline {
panic!("timed out waiting for Muninn RUDP health frame");
}
}
});
let options = IdunnRudpHealthOptions {
endpoint: receiver_addr,
@ -2815,7 +2855,7 @@ mod tests {
publish_idunn_rudp_health(&options, "active", "Muninn healthy", "unix:100").unwrap();
let (message, _, _) = handle.join().unwrap();
let message = handle.join().unwrap();
let CultNetMessage::DocumentPutRaw { document, .. } = message else {
panic!("expected raw document put");
};

View file

@ -60,9 +60,13 @@ Odin's executable body is split by ownership:
ports, normalization, and CultMesh/CultCache repository boundaries. This is
the replacement spine; JavaScript remains legacy runtime/probe scaffolding
until each organ has crossed the typed boundary.
- `src/odin-coordinator.cjs`: process lifecycle, refresh loop, persistence, health, and transport wiring.
- `src/odin-coordinator.cjs`: process lifecycle, serialized refresh loop,
persistence, health, and transport wiring. Refreshes must not overlap because
a refresh publishes Odin's daemon health.
- `src/odin/config.cjs`: runtime paths, seed deck URLs, intervals, and CultLib module path setup.
- `src/odin/documents.cjs`: CultCache/CultMesh document definitions accepted by Odin.
- `src/odin/idunn-rudp.cjs`: daemon-owned Odin provider health publication to
Idunn over the canonical CultNet RUDP `schema` channel.
- `src/odin/probes.cjs`: local Docker/ADB probes plus demoted TCP/SSH/GPU
compatibility probes while daemon CultLib dependencies migrate to CultNet
RUDP health publication.

View file

@ -134,9 +134,10 @@ idunn.rudp_health_ingress.v1
over `cultnet.transport.rudp.v0`. TCP, HTTP, WebSocket, and ad hoc port probes
are migration debt, tolerated only at xenos/legacy boundaries and while the
daemon's CultLib dependency has not yet been updated.
- Rust now has a narrow `cultnet.transport.rudp.v0` substrate in
`vendor/cultnet-rs`: one CultNet message per acknowledged UDP datagram with
timeout/retry semantics. This removes "Rust cannot speak RUDP" as a substrate
- Rust now shares the canonical cross-runtime `cultnet.transport.rudp.v0`
substrate in `vendor/cultnet-rs`: CNR0 packets, sessions, channels, reliable
schema frames, and timeout/retry semantics matching the TypeScript/Python
CultLib implementations. This removes "Rust cannot speak RUDP" as a substrate
excuse. It does not make any daemon fully migrated until that daemon publishes
its health and command boundary through the RUDP path and Idunn consumes that
daemon-owned publication instead of the compatibility command.
@ -148,11 +149,15 @@ idunn.rudp_health_ingress.v1
`idunn.rudp_health_ingress.v1`. The Starfire local supervisor binds
`0.0.0.0:17870` so host-local publishers can use `127.0.0.1:17870` and
WireGuard peers such as Nightwing can publish to `10.77.0.2:17870`. That ingress
accepts only raw `idunn.daemon_health` CultNet document puts, decodes the
typed MessagePack payload, and writes it into the keepalive store. It does
not grant deploy/restart authority and it does not make compatibility probes
owners; it is the first daemon-owned health publication path Idunn can
consume.
accepts only raw `idunn.daemon_health` CultNet document puts on the `schema`
channel, decodes the typed MessagePack payload, and writes it into the
keepalive store. Each one-shot publisher gets its own RUDP session from its
UDP source address and the session is discarded after a delivered health
frame. Windows UDP `ConnectionReset`/`ConnectionAborted` reports from closed
one-shot clients are nonfatal ingress noise, not a reason to kill the worker.
The ingress does not grant deploy/restart authority and it does not make
compatibility probes owners; it is the first daemon-owned health publication
path Idunn can consume.
- During each target cycle, fresh daemon-published RUDP health wins over the
local compatibility command. Idunn accepts it only when the daemon id, health
contract, `publication_source=daemon-published`, transport
@ -260,16 +265,19 @@ The local swarm mode owns:
Yggdrasil checks.
Current plan surface: `idunn.swarm_surgery_plan.v1` for profile
`starfire-local` treats the Muninn Rust lanes as completed cuts. Muninn's
`--health` mode publishes `idunn.daemon_health` over RUDP; Starfire publishes to
local Idunn, while Nightwing and Raven publish over WireGuard to
`10.77.0.2:17870` using their target daemon ids and health contracts. Live Idunn
cycles accept those records before command-probe fallback. The plan's current
phase now points at Odin's own provider-health lane.
`starfire-local` treats the Muninn Rust lanes and Odin's local provider-health
lane as completed local substrate cuts. Muninn's `--health` mode publishes
`idunn.daemon_health` over RUDP; Starfire publishes to local Idunn, while
Nightwing and Raven publish over WireGuard to `10.77.0.2:17870` using their
target daemon ids and health contracts. Odin now publishes
`odin.cultnet-rudp-provider-health` over the same RUDP ingress after each
provider refresh. Live Idunn cycles accept these records before command-probe
fallback.
Next: make Odin publish its own provider health over daemon-owned RUDP state,
then continue runtime-by-runtime until compatibility probes can be deleted or
demoted.
Next: move Stonks market-provider health off HTTP compatibility evidence and
onto daemon-owned RUDP state, then continue runtime-by-runtime until
compatibility probes can be deleted or demoted. Raven Muninn task action repair
is queued separately and remains blocked while Raven is unreachable.
No ad hoc JSON manifest, HTTP endpoint, TCP socket, or WebSocket bridge may
become the live state owner. Debug projections are fine when they name the

View file

@ -1,6 +1,9 @@
param(
[int] $Port = 8797,
[string] $StateDir = "E:\Projects\Odin\scratch\odin",
[string] $IdunnRudpHealth = "127.0.0.1:17870",
[string] $IdunnDaemon = "odin",
[string] $IdunnHealthContract = "odin.cultnet-rudp-provider-health",
[switch] $Foreground
)
@ -29,7 +32,10 @@ $env:NODE_PATH = "E:\Projects\CultLib\packages"
$args = @(
$scriptPath,
"--port", "$Port",
"--stateDir", $StateDir
"--stateDir", $StateDir,
"--idunn-rudp-health", $IdunnRudpHealth,
"--idunn-daemon", $IdunnDaemon,
"--idunn-health-contract", $IdunnHealthContract
)
if ($Foreground) {

View file

@ -181,9 +181,11 @@ walk through the same door.
The local swarm also opens a RUDP health ingress at `127.0.0.1:17870` unless
started with `--rudp-health-bind none`. It accepts raw
`idunn.daemon_health` document puts over `cultnet.transport.rudp.v0` and writes
them into the keepalive store. That is daemon-owned health publication, not
restart/deploy authority.
`idunn.daemon_health` document puts on the canonical CNR0 RUDP `schema` channel
and writes them into the keepalive store. That is daemon-owned health
publication, not restart/deploy authority. On Windows, UDP reset reports from
closed one-shot publishers are treated as nonfatal ingress noise so one accepted
health frame cannot kill the listener.
When a fresh daemon-published RUDP health record exists for a target, Idunn uses
that record for keepalive planning before falling back to the local command
@ -191,17 +193,17 @@ probe. The record must match the daemon id, health contract, RUDP transport,
and freshness window; otherwise the compatibility probe remains fallback
evidence.
The next cuts are updating daemon CultLib dependencies so health and command
boundaries publish through `cultnet.transport.rudp.v0`, switching Idunn to those
daemon-owned records, promoting the built-in target catalog into Odin-ingested
service/provider records, keeping named adapters only for legacy service-manager
crossings, and replacing the local operator alarm bridge with a fully
Bifrost-owned notification request record.
The next cut is Stonks: market-provider health must publish through
`cultnet.transport.rudp.v0` instead of remaining HTTP compatibility evidence.
After that, continue daemon-by-daemon, promote the built-in target catalog into
Odin-ingested service/provider records, keep named adapters only for legacy
service-manager crossings, and replace the local operator alarm bridge with a
fully Bifrost-owned notification request record.
Rust no longer gets to claim the transport is imaginary:
`vendor/cultnet-rs` includes a narrow stop-and-wait
`cultnet.transport.rudp.v0` path for acknowledged CultNet messages over UDP.
That is substrate only. Idunn still marks daemon profiles as
`vendor/cultnet-rs` includes the canonical CNR0 `cultnet.transport.rudp.v0`
session path for acknowledged CultNet messages over UDP. That is substrate
only. Idunn still marks daemon profiles as
`migration-required` until each daemon actually publishes health and command
boundary records through that path.

View file

@ -7,12 +7,14 @@ const path = require("path");
const { buildConfig, loadCultRuntime } = require("./odin/config.cjs");
const { defineOdinDocuments } = require("./odin/documents.cjs");
const { createInterfaceDiscovery } = require("./odin/interfaces.cjs");
const { createIdunnRudpHealthPublisher, publishIdunnRudpHealth } = require("./odin/idunn-rudp.cjs");
const { createLayoutStore } = require("./odin/layout.cjs");
const { createStateBuilder } = require("./odin/state.cjs");
const { broadcastState, createDashboardServer } = require("./odin/websocket.cjs");
const config = buildConfig(process.argv.slice(2));
fs.mkdirSync(config.stateDir, { recursive: true });
const idunnRudpHealthPublisher = createIdunnRudpHealthPublisher(config.idunnRudpHealth);
const cultRuntime = loadCultRuntime();
if (cultRuntime.error) {
@ -67,8 +69,14 @@ async function main() {
console.log(`Durable surface cache: ${config.cachePath}`);
await refresh(dashboardServer.clients);
setInterval(() => {
refresh(dashboardServer.clients).catch((error) => console.error("refresh failed:", error));
scheduleRefresh(dashboardServer.clients);
}
function scheduleRefresh(clients) {
setTimeout(() => {
refresh(clients)
.catch((error) => console.error("refresh failed:", error))
.finally(() => scheduleRefresh(clients));
}, config.intervalMs);
}
@ -106,6 +114,7 @@ async function refresh(clients) {
error: null,
startedAt: new Date(started).toISOString(),
};
await publishOdinHealth("active", `Odin provider catalog refreshed in ${lastRefresh.durationMs}ms`);
} catch (error) {
lastRefresh = {
completedAt: new Date().toISOString(),
@ -113,10 +122,24 @@ async function refresh(clients) {
error: error.message,
startedAt: new Date(started).toISOString(),
};
await publishOdinHealth("failed", `Odin provider refresh failed: ${error.message}`);
throw error;
}
}
async function publishOdinHealth(state, detail) {
if (!idunnRudpHealthPublisher) return;
try {
await publishIdunnRudpHealth(idunnRudpHealthPublisher, {
state,
detail,
observedAt: new Date().toISOString(),
});
} catch (error) {
console.error("Idunn RUDP health publish failed:", error.message);
}
}
async function persistState(state) {
fs.writeFileSync(path.join(config.stateDir, "latest-surface.json"), JSON.stringify(state, null, 2), "utf8");
if (!meshNodePromise || !documents.surfaceDefinition) {

View file

@ -29,6 +29,11 @@ function buildConfig(argv) {
const gamecultTextDocumentStorePath = args.gamecultTextDocumentStorePath || path.join(repoRoot, "..", "VoidBot", ".voidbot", "private", "gamecult-text-documents.cc");
const stonksStateUrl = args.stonksStateUrl || "http://127.0.0.1:8802/market/state";
const stonksBurstSize = Math.max(1, Number(args.stonksBurstSize || 8));
const idunnRudpHealth = args["idunn-rudp-health"] ? {
endpoint: String(args["idunn-rudp-health"]),
daemonId: String(args["idunn-daemon"] || "odin"),
healthContract: String(args["idunn-health-contract"] || "odin.cultnet-rudp-provider-health"),
} : null;
const defaultInterfaceBindingStores = [
path.join(repoRoot, "..", "VoidBot", ".voidbot", "status", "cultmesh", "voidbot-swarm-state.cc"),
path.join(repoRoot, "..", "Bifrost", ".bifrost", "provider-advertisement.cc"),
@ -48,6 +53,7 @@ function buildConfig(argv) {
gamecultTextDocumentStorePath,
host,
interfaceBindingStores,
idunnRudpHealth,
intervalMs,
layoutPath,
observationFreshSeconds,

254
src/odin/idunn-rudp.cjs Normal file
View file

@ -0,0 +1,254 @@
"use strict";
const dgram = require("dgram");
const path = require("path");
const {
CultNetRudpSession,
decodeRudpPacket,
encodeCultNetMessageForWire,
encodeRudpPacket,
} = require("cultnet-ts");
const { encode } = require(path.resolve(
__dirname,
"..",
"..",
"..",
"CultLib",
"node_modules",
"@msgpack",
"msgpack",
));
const CULTNET_RUDP_PROTOCOL_ID = "cultnet.transport.rudp.v0";
const IDUNN_HEALTH_RUDP_CONNECTION_ID = 0x1d0d0001;
function createIdunnRudpHealthPublisher(options) {
if (!options) return null;
const endpoint = parseEndpoint(options.endpoint);
return {
daemonId: options.daemonId,
endpoint,
healthContract: options.healthContract,
};
}
async function publishIdunnRudpHealth(publisher, health) {
if (!publisher) return;
const socket = dgram.createSocket(endpointFamily(publisher.endpoint.host));
await bindSocket(socket, publisher.endpoint);
const receiver = createPacketReceiver(socket);
const session = new CultNetRudpSession({
connectionId: IDUNN_HEALTH_RUDP_CONNECTION_ID,
initialSequence: 1,
resendDelayMs: 100,
});
try {
const connect = session.createConnect(Date.now(), new Uint8Array());
await sendPacket(socket, publisher.endpoint, connect);
await receiveUntil(
receiver,
session,
publisher.endpoint,
(packet) => packet.packetType === "accept",
5000,
"accept",
);
const observedAt = health.observedAt || new Date().toISOString();
const record = {
daemonId: publisher.daemonId,
state: health.state,
detail: health.detail,
observedAt,
healthContract: publisher.healthContract,
publicationSource: "daemon-published",
transport: CULTNET_RUDP_PROTOCOL_ID,
};
const payload = encode([
record.daemonId,
record.state,
record.detail,
record.observedAt,
record.healthContract,
record.publicationSource,
record.transport,
]);
const message = {
schemaVersion: "cultnet.document_put_raw.v0",
messageId: `odin-health:${publisher.daemonId}:${observedAt.replace(/[:.]/g, "-")}`,
document: {
schemaId: "idunn.daemon_health",
recordKey: publisher.daemonId,
storedAt: observedAt,
payloadEncoding: "messagepack",
payload,
sourceRuntimeId: "odin-coordinator",
sourceRole: "daemon-health-publisher",
tags: [CULTNET_RUDP_PROTOCOL_ID],
},
};
const wirePayload = encode(encodeCultNetMessageForWire(message, "cultnet.schema.v0"));
const dataPackets = session.sendMany("schema", wirePayload, {
reliable: true,
ordered: true,
nowMs: Date.now(),
});
const ack = receiveUntil(
receiver,
session,
publisher.endpoint,
(packet) => packet.packetType === "ack",
500,
"ack",
).catch(() => undefined);
for (const packet of dataPackets) {
await sendPacket(socket, publisher.endpoint, packet);
}
await ack;
} finally {
receiver.close();
socket.close();
}
}
async function bindSocket(socket, endpoint) {
await new Promise((resolve, reject) => {
socket.once("error", reject);
socket.bind(0, endpoint.host.includes(":") ? "::" : "0.0.0.0", () => {
socket.off("error", reject);
resolve();
});
});
}
function parseEndpoint(value) {
const text = String(value || "").trim();
const ipv6 = text.match(/^\[([^\]]+)\]:(\d+)$/);
if (ipv6) return { host: ipv6[1], port: parsePort(ipv6[2]) };
const index = text.lastIndexOf(":");
if (index <= 0) {
throw new Error(`Idunn RUDP endpoint must be host:port, got "${value}".`);
}
return { host: text.slice(0, index), port: parsePort(text.slice(index + 1)) };
}
function parsePort(value) {
const port = Number(value);
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
throw new Error(`Idunn RUDP endpoint port is invalid: ${value}`);
}
return port;
}
function endpointFamily(host) {
return host.includes(":") ? "udp6" : "udp4";
}
async function receiveUntil(receiver, session, endpoint, predicate, timeoutMs, label) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
const packet = await receiver.next(Math.min(100, deadline - Date.now()), label);
const result = session.receive(packet, Date.now());
if (result.reply) {
throw new Error("Odin RUDP health publisher received an unexpected reply-required packet.");
}
if (predicate(packet)) {
return;
}
} catch (error) {
if (error.code !== "ETIMEDOUT") {
throw error;
}
}
for (const packet of session.dueResends(Date.now())) {
await sendPacket(receiver.socket, endpoint, packet);
}
}
throw new Error(`timed out waiting for Idunn RUDP ${label} response after ${timeoutMs}ms`);
}
function createPacketReceiver(socket) {
const packets = [];
const waiters = [];
const errors = [];
const resolveNext = () => {
while (waiters.length > 0 && (packets.length > 0 || errors.length > 0)) {
const waiter = waiters.shift();
clearTimeout(waiter.timer);
if (errors.length > 0) {
waiter.reject(errors.shift());
} else {
waiter.resolve(packets.shift());
}
}
};
const onMessage = (wire) => {
try {
packets.push(decodeRudpPacket(wire));
} catch (error) {
errors.push(error);
}
resolveNext();
};
const onError = (error) => {
errors.push(error);
resolveNext();
};
socket.on("message", onMessage);
socket.on("error", onError);
return {
socket,
next(timeoutMs, label = "packet") {
if (packets.length > 0) return Promise.resolve(packets.shift());
if (errors.length > 0) return Promise.reject(errors.shift());
return new Promise((resolve, reject) => {
const waiter = {
resolve,
reject,
timer: setTimeout(() => {
const index = waiters.indexOf(waiter);
if (index >= 0) waiters.splice(index, 1);
const error = new Error(`timed out waiting for Idunn RUDP ${label}`);
error.code = "ETIMEDOUT";
reject(error);
}, Math.max(1, timeoutMs)),
};
waiters.push(waiter);
});
},
close() {
socket.off("message", onMessage);
socket.off("error", onError);
while (waiters.length > 0) {
const waiter = waiters.shift();
clearTimeout(waiter.timer);
const error = new Error("Odin RUDP health publisher closed.");
error.code = "ECLOSED";
waiter.reject(error);
}
},
};
}
async function sendPacket(socket, endpoint, packet) {
const wire = encodeRudpPacket(packet);
await new Promise((resolve, reject) => {
socket.send(wire, endpoint.port, endpoint.host, (error) => {
if (error) reject(error);
else resolve();
});
});
}
module.exports = {
CULTNET_RUDP_PROTOCOL_ID,
createIdunnRudpHealthPublisher,
publishIdunnRudpHealth,
};

View file

@ -30,6 +30,12 @@
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"transportProfiles": {
"type": "array",
"items": {
"$ref": "./cultnet.transport-profile.schema.json"
}
},
"supportsSchemaCatalog": { "type": "boolean" }
}
}

View file

@ -0,0 +1,81 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.transport-profile.schema.json",
"title": "CultNet Transport Profile",
"type": "object",
"required": [
"schemaVersion",
"runtimeId",
"transports"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.transport_profile.v0" },
"runtimeId": { "type": "string", "minLength": 1 },
"transports": {
"type": "array",
"items": {
"type": "object",
"required": [
"transportId",
"protocol",
"channels"
],
"additionalProperties": false,
"properties": {
"transportId": { "type": "string", "minLength": 1 },
"protocol": {
"type": "string",
"enum": [
"tcp_framed",
"litenetlib",
"websocket",
"rudp"
]
},
"host": { "type": "string", "minLength": 1 },
"port": { "type": "integer", "minimum": 1, "maximum": 65535 },
"path": { "type": "string", "minLength": 1 },
"discoveryGroup": { "type": "string", "minLength": 1 },
"wireContracts": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"channels": {
"type": "array",
"items": {
"type": "object",
"required": [
"channelId",
"delivery",
"ordering"
],
"additionalProperties": false,
"properties": {
"channelId": { "type": "string", "minLength": 1 },
"delivery": {
"type": "string",
"enum": [
"reliable",
"unreliable"
]
},
"ordering": {
"type": "string",
"enum": [
"ordered",
"unordered",
"sequenced"
]
},
"maxPayloadBytes": { "type": "integer", "minimum": 1 },
"maxFragmentBytes": { "type": "integer", "minimum": 1 },
"maxPendingReliablePackets": { "type": "integer", "minimum": 1 }
}
}
}
}
}
}
}
}

View file

@ -81,6 +81,70 @@ pub struct CultNetDocumentMutationContract {
pub notes: Option<Vec<String>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CultNetTransportProfile {
pub schema_version: String,
pub runtime_id: String,
pub transports: Vec<CultNetTransportDescriptor>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CultNetTransportDescriptor {
pub transport_id: String,
pub protocol: CultNetTransportProtocol,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub host: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub port: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub discovery_group: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wire_contracts: Option<Vec<String>>,
pub channels: Vec<CultNetTransportChannel>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CultNetTransportProtocol {
TcpFramed,
Litenetlib,
Websocket,
Rudp,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CultNetTransportChannel {
pub channel_id: String,
pub delivery: CultNetTransportDelivery,
pub ordering: CultNetTransportOrdering,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_payload_bytes: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_fragment_bytes: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_pending_reliable_packets: Option<u32>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CultNetTransportDelivery {
Reliable,
Unreliable,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CultNetTransportOrdering {
Ordered,
Unordered,
Sequenced,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CultNetRawDocumentRecord {
@ -137,6 +201,8 @@ pub enum CultNetMessage {
#[serde(default, skip_serializing_if = "Option::is_none")]
supported_message_versions: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
transport_profiles: Option<Vec<CultNetTransportProfile>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
supports_schema_catalog: Option<bool>,
},
#[serde(rename = "cultnet.login.v0", rename_all = "camelCase")]
@ -287,6 +353,7 @@ fn validate_message(message: &CultNetMessage) -> Result<()> {
supported_document_types,
supported_mutation_contracts,
supported_message_versions,
transport_profiles: _,
supports_schema_catalog: _,
} => {
require_non_empty(runtime_id, "runtimeId")?;

View file

@ -4,6 +4,7 @@ mod replication;
mod rudp;
mod schema_discovery;
mod security;
mod transport;
pub use contracts::*;
pub use framing::*;
@ -11,3 +12,4 @@ pub use replication::*;
pub use rudp::*;
pub use schema_discovery::*;
pub use security::*;
pub use transport::*;

File diff suppressed because it is too large Load diff

View file

@ -207,6 +207,13 @@ pub fn builtin_schema_registry() -> Result<CultNetSchemaRegistry> {
None,
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.transport-profile.schema.json"),
CultNetSchemaKind::SharedContract,
vec![CultNetWireContract::CultNetSchemaV0],
Some("cultnet.transport_profile.v0"),
None,
)?,
schema_registration(
include_str!("../contracts/cultnet.login.schema.json"),
CultNetSchemaKind::WireMessage,

192
vendor/cultnet-rs/src/transport.rs vendored Normal file
View file

@ -0,0 +1,192 @@
use anyhow::Result;
use anyhow::anyhow;
use serde::Deserialize;
use serde::Serialize;
use std::io::Read;
use std::io::Write;
use crate::CultNetTransportChannel;
use crate::CultNetTransportDelivery;
use crate::CultNetTransportDescriptor;
use crate::CultNetTransportOrdering;
use crate::CultNetTransportProfile;
use crate::CultNetTransportProtocol;
use crate::FRAME_HEADER_BYTES;
use crate::encode_frame;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CultNetTransportStats {
pub bytes_received: u64,
pub bytes_sent: u64,
pub frames_received: u64,
pub frames_sent: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CultNetTransportFrame {
pub channel_id: String,
pub payload: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CultNetReconnectPolicy {
pub schema_version: String,
pub policy_id: String,
pub base_delay_ms: u64,
pub max_delay_ms: u64,
pub max_jitter_ms: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_attempts: Option<u32>,
}
#[derive(Clone, Debug)]
pub struct CultNetReconnectPolicyOptions {
pub policy_id: String,
pub base_delay_ms: u64,
pub max_delay_ms: u64,
pub max_jitter_ms: u64,
pub max_attempts: Option<u32>,
}
impl Default for CultNetReconnectPolicyOptions {
fn default() -> Self {
Self {
policy_id: "default".to_string(),
base_delay_ms: 1_000,
max_delay_ms: 30_000,
max_jitter_ms: 250,
max_attempts: None,
}
}
}
pub fn create_reconnect_policy(options: CultNetReconnectPolicyOptions) -> CultNetReconnectPolicy {
CultNetReconnectPolicy {
schema_version: "cultnet.reconnect_policy.v0".to_string(),
policy_id: if options.policy_id.trim().is_empty() {
"default".to_string()
} else {
options.policy_id
},
base_delay_ms: options.base_delay_ms,
max_delay_ms: options.max_delay_ms,
max_jitter_ms: options.max_jitter_ms,
max_attempts: options.max_attempts,
}
}
pub fn compute_reconnect_delay_ms(
policy: &CultNetReconnectPolicy,
attempt: u32,
jitter_ms: u64,
) -> u64 {
let normalized_attempt = attempt.max(1);
let multiplier = 2_u64.saturating_pow(normalized_attempt.saturating_sub(1));
let capped_base_delay = policy
.base_delay_ms
.saturating_mul(multiplier)
.min(policy.max_delay_ms);
capped_base_delay + jitter_ms.min(policy.max_jitter_ms)
}
#[derive(Clone, Debug, Default)]
pub struct TcpFramedTransportProfileOptions {
pub transport_id: Option<String>,
pub host: Option<String>,
pub port: Option<u16>,
pub max_payload_bytes: Option<u32>,
pub max_fragment_bytes: Option<u32>,
}
pub fn create_tcp_framed_transport_profile(
runtime_id: impl Into<String>,
options: TcpFramedTransportProfileOptions,
) -> CultNetTransportProfile {
CultNetTransportProfile {
schema_version: "cultnet.transport_profile.v0".to_string(),
runtime_id: runtime_id.into(),
transports: vec![CultNetTransportDescriptor {
transport_id: options
.transport_id
.unwrap_or_else(|| "tcp-framed".to_string()),
protocol: CultNetTransportProtocol::TcpFramed,
host: options.host,
port: options.port,
path: None,
discovery_group: None,
wire_contracts: Some(vec!["cultnet.schema.v0".to_string()]),
channels: vec![CultNetTransportChannel {
channel_id: "schema".to_string(),
delivery: CultNetTransportDelivery::Reliable,
ordering: CultNetTransportOrdering::Ordered,
max_payload_bytes: options.max_payload_bytes,
max_fragment_bytes: options.max_fragment_bytes,
max_pending_reliable_packets: None,
}],
}],
}
}
pub struct TcpFramedTransportConnection<TStream> {
stream: TStream,
pub profile: CultNetTransportProfile,
stats: CultNetTransportStats,
}
impl<TStream> TcpFramedTransportConnection<TStream> {
pub fn new(stream: TStream, profile: CultNetTransportProfile) -> Self {
Self {
stream,
profile,
stats: CultNetTransportStats::default(),
}
}
pub fn stats(&self) -> CultNetTransportStats {
self.stats.clone()
}
pub fn into_inner(self) -> TStream {
self.stream
}
}
impl<TStream> TcpFramedTransportConnection<TStream>
where
TStream: Write,
{
pub fn send(&mut self, channel_id: &str, payload: &[u8]) -> Result<()> {
if channel_id != "schema" {
return Err(anyhow!(
"tcp_framed transport only supports the schema channel, got {channel_id:?}"
));
}
let frame = encode_frame(payload)?;
self.stream.write_all(&frame)?;
self.stream.flush()?;
self.stats.bytes_sent += frame.len() as u64;
self.stats.frames_sent += 1;
Ok(())
}
}
impl<TStream> TcpFramedTransportConnection<TStream>
where
TStream: Read,
{
pub fn receive(&mut self) -> Result<CultNetTransportFrame> {
let mut header = [0_u8; FRAME_HEADER_BYTES];
self.stream.read_exact(&mut header)?;
let payload_len = u32::from_be_bytes(header) as usize;
let mut payload = vec![0_u8; payload_len];
self.stream.read_exact(&mut payload)?;
self.stats.bytes_received += (FRAME_HEADER_BYTES + payload_len) as u64;
self.stats.frames_received += 1;
Ok(CultNetTransportFrame {
channel_id: "schema".to_string(),
payload,
})
}
}

View file

@ -11,22 +11,80 @@ use cultnet_rs::CultNetDocumentPutOptions;
use cultnet_rs::CultNetDocumentRegistry;
use cultnet_rs::CultNetMessage;
use cultnet_rs::CultNetMutationAuthority;
use cultnet_rs::CultNetRudpPacket;
use cultnet_rs::CultNetRudpPacketType;
use cultnet_rs::CultNetRudpSendOptions;
use cultnet_rs::CultNetRudpSession;
use cultnet_rs::CultNetRudpSessionOptions;
use cultnet_rs::CultNetRudpSocketMode;
use cultnet_rs::CultNetRudpSocketTransportConnection;
use cultnet_rs::CultNetRudpSocketTransportOptions;
use cultnet_rs::CultNetSchemaKind;
use cultnet_rs::CultNetSchemaRegistry;
use cultnet_rs::CultNetSecret;
use cultnet_rs::CultNetServerSecurityOptions;
use cultnet_rs::CultNetTransportChannel;
use cultnet_rs::CultNetTransportDelivery;
use cultnet_rs::CultNetTransportDescriptor;
use cultnet_rs::CultNetTransportFrame;
use cultnet_rs::CultNetTransportOrdering;
use cultnet_rs::CultNetTransportProfile;
use cultnet_rs::CultNetTransportProtocol;
use cultnet_rs::CultNetWireContract;
use cultnet_rs::LengthPrefixedMessageFramer;
use cultnet_rs::TcpFramedTransportConnection;
use cultnet_rs::TcpFramedTransportProfileOptions;
use cultnet_rs::builtin_schema_registry;
use cultnet_rs::create_rudp_transport_profile;
use cultnet_rs::create_tcp_framed_transport_profile;
use cultnet_rs::decode_cultnet_message_from_slice;
use cultnet_rs::decode_rudp_packet;
use cultnet_rs::encode_cultnet_message_for_wire;
use cultnet_rs::encode_cultnet_message_to_vec;
use cultnet_rs::encode_frame;
use cultnet_rs::encode_rudp_packet;
use pretty_assertions::assert_eq;
use std::net::UdpSocket;
use std::thread;
use std::time::Duration as StdDuration;
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");
fn bind_udp_socket() -> Result<UdpSocket> {
let socket = UdpSocket::bind("127.0.0.1:0")?;
socket.set_read_timeout(Some(StdDuration::from_millis(20)))?;
Ok(socket)
}
fn pump_rudp_handshake(
client: &mut CultNetRudpSocketTransportConnection,
server: &mut CultNetRudpSocketTransportConnection,
) -> Result<()> {
for _ in 0..20 {
let _ = server.receive_once()?;
let _ = client.receive_once()?;
let _ = server.receive_once()?;
if client.connected() && server.connected() {
return Ok(());
}
thread::sleep(StdDuration::from_millis(5));
}
anyhow::bail!("RUDP socket handshake did not complete");
}
fn receive_rudp_frame(
transport: &mut CultNetRudpSocketTransportConnection,
) -> Result<CultNetTransportFrame> {
for _ in 0..20 {
if let Some(frame) = transport.receive_once()? {
return Ok(frame);
}
thread::sleep(StdDuration::from_millis(5));
}
anyhow::bail!("RUDP socket frame was not delivered")
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(
type = "ghostlight.agent-state",
@ -84,11 +142,32 @@ fn cultnet_schema_messages_round_trip_through_messagepack_frames() -> Result<()>
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."
"Persona may request bounded memory mutation; coordinator reviews authority."
.to_string(),
]),
}]),
supported_message_versions: None,
transport_profiles: Some(vec![CultNetTransportProfile {
schema_version: "cultnet.transport_profile.v0".to_string(),
runtime_id: "voidbot-main".to_string(),
transports: vec![CultNetTransportDescriptor {
transport_id: "direct-pipe".to_string(),
protocol: CultNetTransportProtocol::TcpFramed,
host: None,
port: None,
path: None,
discovery_group: None,
wire_contracts: Some(vec!["cultnet.schema.v0".to_string()]),
channels: vec![CultNetTransportChannel {
channel_id: "schema".to_string(),
delivery: CultNetTransportDelivery::Reliable,
ordering: CultNetTransportOrdering::Ordered,
max_payload_bytes: None,
max_fragment_bytes: None,
max_pending_reliable_packets: None,
}],
}],
}]),
supports_schema_catalog: Some(true),
};
let payload = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)?;
@ -105,6 +184,732 @@ fn cultnet_schema_messages_round_trip_through_messagepack_frames() -> Result<()>
Ok(())
}
#[test]
fn tcp_framed_transport_carries_schema_payloads_with_stats() -> Result<()> {
let payload = b"cultnet-payload".to_vec();
let profile = create_tcp_framed_transport_profile(
"rust-transport",
TcpFramedTransportProfileOptions {
transport_id: Some("test-tcp".to_string()),
..TcpFramedTransportProfileOptions::default()
},
);
let mut sender = TcpFramedTransportConnection::new(Vec::<u8>::new(), profile.clone());
sender.send("schema", &payload)?;
assert_eq!(sender.stats().frames_sent, 1);
assert_eq!(sender.stats().bytes_sent, (payload.len() + 4) as u64);
assert!(sender.send("unreliable", &[]).is_err());
let bytes = sender.into_inner();
let mut receiver = TcpFramedTransportConnection::new(std::io::Cursor::new(bytes), profile);
let frame = receiver.receive()?;
assert_eq!(frame.channel_id, "schema");
assert_eq!(frame.payload, payload);
assert_eq!(receiver.stats().frames_received, 1);
assert_eq!(
receiver.stats().bytes_received,
(frame.payload.len() + 4) as u64
);
assert_eq!(
receiver.profile.transports[0].protocol,
CultNetTransportProtocol::TcpFramed
);
Ok(())
}
#[test]
fn rudp_packet_codec_uses_deterministic_reliable_ordered_fixture() -> Result<()> {
let encoded = encode_rudp_packet(&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Data,
connection_id: 0x01020304,
sequence: 0x0000002a,
ack: 0x00000029,
ack_mask: 0x80000001,
channel_id: "schema".to_string(),
reliable: true,
ordered: true,
sequenced: false,
fragment_id: 7,
fragment_index: 1,
fragment_count: 3,
payload: b"hello".to_vec(),
})?;
assert_eq!(
encoded,
vec![
67, 78, 82, 48, 0, 3, 11, 42, 1, 2, 3, 4, 0, 0, 0, 42, 0, 0, 0, 41, 128, 0, 0, 1, 0, 7,
0, 1, 0, 3, 0, 0, 0, 5, 6, 0, 115, 99, 104, 101, 109, 97, 104, 101, 108, 108, 111,
]
);
let decoded = decode_rudp_packet(&encoded)?;
assert_eq!(decoded.packet_type, CultNetRudpPacketType::Data);
assert_eq!(decoded.connection_id, 0x01020304);
assert_eq!(decoded.sequence, 0x0000002a);
assert_eq!(decoded.ack, 0x00000029);
assert_eq!(decoded.ack_mask, 0x80000001);
assert_eq!(decoded.channel_id, "schema");
assert!(decoded.reliable);
assert!(decoded.ordered);
assert!(!decoded.sequenced);
assert_eq!(decoded.fragment_id, 7);
assert_eq!(decoded.fragment_index, 1);
assert_eq!(decoded.fragment_count, 3);
assert_eq!(decoded.payload, b"hello");
Ok(())
}
#[test]
fn rudp_transport_profile_advertises_state_and_realtime_channels() {
let profile = create_rudp_transport_profile(
"rust-rudp",
cultnet_rs::RudpTransportProfileOptions {
transport_id: Some("public-rudp".to_string()),
host: Some("127.0.0.1".to_string()),
port: Some(7777),
max_payload_bytes: Some(1200),
max_fragment_bytes: Some(1000),
max_pending_reliable_packets: Some(64),
},
);
assert_eq!(
profile.transports[0].protocol,
CultNetTransportProtocol::Rudp
);
let channels = profile.transports[0]
.channels
.iter()
.map(|channel| {
(
channel.channel_id.as_str(),
channel.delivery,
channel.ordering,
channel.max_pending_reliable_packets,
)
})
.collect::<Vec<_>>();
assert_eq!(
channels,
vec![
(
"schema",
CultNetTransportDelivery::Reliable,
CultNetTransportOrdering::Ordered,
Some(64)
),
(
"latest",
CultNetTransportDelivery::Unreliable,
CultNetTransportOrdering::Sequenced,
Some(64)
),
(
"realtime",
CultNetTransportDelivery::Unreliable,
CultNetTransportOrdering::Unordered,
Some(64)
),
]
);
}
#[test]
fn rudp_session_handshake_acks_reliable_connect_and_accept_packets() -> Result<()> {
let mut client = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 0x0a0b0c0d,
initial_sequence: 1,
resend_delay_ms: 50,
..CultNetRudpSessionOptions::default()
});
let mut server = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 0x0a0b0c0d,
initial_sequence: 100,
resend_delay_ms: 50,
..CultNetRudpSessionOptions::default()
});
let connect = client.create_connect(0, b"join".to_vec())?;
assert_eq!(connect.packet_type, CultNetRudpPacketType::Connect);
assert_eq!(connect.sequence, 1);
assert_eq!(client.pending_reliable_sequences(), vec![1]);
let accept = server.accept_connect(&connect, 10, b"ok".to_vec())?;
assert_eq!(accept.packet_type, CultNetRudpPacketType::Accept);
assert_eq!(accept.ack, 1);
assert!(server.connected());
assert_eq!(server.pending_reliable_sequences(), vec![100]);
client.receive(&accept, 20)?;
assert!(client.connected());
assert!(client.pending_reliable_sequences().is_empty());
let ack = client.create_ack();
assert_eq!(ack.ack, 100);
server.receive(&ack, 30)?;
assert!(server.pending_reliable_sequences().is_empty());
Ok(())
}
#[test]
fn rudp_session_computes_ack_masks_and_clears_pending_reliable_packets() -> Result<()> {
let mut sender = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 7,
initial_sequence: 10,
resend_delay_ms: 100,
..CultNetRudpSessionOptions::default()
});
let mut receiver = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 7,
initial_sequence: 200,
resend_delay_ms: 100,
..CultNetRudpSessionOptions::default()
});
sender.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 7,
sequence: 1,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
receiver.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 7,
sequence: 2,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
let options = CultNetRudpSendOptions {
reliable: true,
ordered: true,
..CultNetRudpSendOptions::default()
};
let first = sender.send("schema", b"first".to_vec(), options.clone())?;
let second = sender.send("schema", b"second".to_vec(), options.clone())?;
let third = sender.send("schema", b"third".to_vec(), options)?;
assert_eq!(sender.pending_reliable_sequences(), vec![10, 11, 12]);
receiver.receive(&first, 0)?;
receiver.receive(&third, 0)?;
let ack_with_gap = receiver.create_ack();
assert_eq!(ack_with_gap.ack, 12);
assert_eq!(ack_with_gap.ack_mask, 0b10 | (1 << 9));
sender.receive(&ack_with_gap, 0)?;
assert_eq!(sender.pending_reliable_sequences(), vec![11]);
receiver.receive(&second, 0)?;
let full_ack = receiver.create_ack();
assert_eq!(full_ack.ack, 12);
assert_eq!(full_ack.ack_mask, 0b11 | (1 << 9));
sender.receive(&full_ack, 0)?;
assert!(sender.pending_reliable_sequences().is_empty());
Ok(())
}
#[test]
fn rudp_session_schedules_reliable_resends_until_acked() -> Result<()> {
let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 99,
initial_sequence: 1,
resend_delay_ms: 100,
..CultNetRudpSessionOptions::default()
});
session.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 99,
sequence: 50,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
let sent = session.send(
"schema",
b"payload".to_vec(),
CultNetRudpSendOptions {
reliable: true,
ordered: true,
now_ms: 10,
..CultNetRudpSendOptions::default()
},
)?;
assert!(session.due_resends(90).is_empty());
assert_eq!(
session
.due_resends(110)
.iter()
.map(|packet| packet.sequence)
.collect::<Vec<_>>(),
vec![sent.sequence]
);
assert!(session.due_resends(150).is_empty());
session.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Ack,
connection_id: 99,
sequence: 51,
ack: sent.sequence,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
assert!(session.due_resends(250).is_empty());
Ok(())
}
#[test]
fn rudp_session_pings_and_detects_receive_timeout() -> Result<()> {
let mut client = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 101,
initial_sequence: 1,
..CultNetRudpSessionOptions::default()
});
let mut server = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 101,
initial_sequence: 100,
..CultNetRudpSessionOptions::default()
});
let connect = client.create_connect(0, b"join".to_vec())?;
let accept = server.accept_connect(&connect, 10, Vec::new())?;
client.receive(&accept, 20)?;
let ping = client.create_ping(b"pulse".to_vec());
let ping_result = server.receive(&ping, 30)?;
let pong = ping_result.reply.expect("ping should produce pong");
assert_eq!(pong.packet_type, CultNetRudpPacketType::Pong);
assert_eq!(pong.payload, b"pulse");
let pong_result = client.receive(&pong, 40)?;
assert!(pong_result.pong);
assert_eq!(pong_result.pong_payload, b"pulse");
assert!(!client.check_timeout(90, 50));
assert!(client.check_timeout(91, 50));
assert!(!client.connected());
Ok(())
}
#[test]
fn rudp_session_bounds_pending_reliable_packets_before_enqueue() -> Result<()> {
let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 102,
initial_sequence: 1,
max_pending_reliable_packets: Some(2),
..CultNetRudpSessionOptions::default()
});
session.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 102,
sequence: 50,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
session.send(
"schema",
b"first".to_vec(),
CultNetRudpSendOptions {
reliable: true,
ordered: true,
..CultNetRudpSendOptions::default()
},
)?;
session.send(
"schema",
b"second".to_vec(),
CultNetRudpSendOptions {
reliable: true,
ordered: true,
..CultNetRudpSendOptions::default()
},
)?;
let error = session
.send(
"schema",
b"third".to_vec(),
CultNetRudpSendOptions {
reliable: true,
ordered: true,
..CultNetRudpSendOptions::default()
},
)
.unwrap_err();
assert!(error.to_string().contains("reliable send queue is full"));
assert_eq!(session.pending_reliable_sequences(), vec![1, 2]);
let mut fragmented = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 103,
initial_sequence: 1,
max_pending_reliable_packets: Some(3),
..CultNetRudpSessionOptions::default()
});
fragmented.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 103,
sequence: 50,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
let error = fragmented
.send_many(
"schema",
b"fragment-me".to_vec(),
CultNetRudpSendOptions {
reliable: true,
ordered: true,
..CultNetRudpSendOptions::default()
},
Some(3),
)
.unwrap_err();
assert!(error.to_string().contains("reliable send queue is full"));
assert!(fragmented.pending_reliable_sequences().is_empty());
Ok(())
}
#[test]
fn rudp_session_suppresses_duplicates_and_delivers_reliable_ordered_payloads_in_sequence()
-> Result<()> {
let mut sender = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 123,
initial_sequence: 1,
..CultNetRudpSessionOptions::default()
});
let mut receiver = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 123,
initial_sequence: 100,
..CultNetRudpSessionOptions::default()
});
sender.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 123,
sequence: 90,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
receiver.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 123,
sequence: 91,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
let options = CultNetRudpSendOptions {
reliable: true,
ordered: true,
..CultNetRudpSendOptions::default()
};
let first = sender.send("schema", b"first".to_vec(), options.clone())?;
let second = sender.send("schema", b"second".to_vec(), options.clone())?;
let third = sender.send("schema", b"third".to_vec(), options)?;
assert_eq!(
receiver
.receive(&first, 0)?
.delivered
.iter()
.map(|frame| String::from_utf8(frame.payload.clone()).unwrap())
.collect::<Vec<_>>(),
vec!["first"]
);
assert!(receiver.receive(&third, 0)?.delivered.is_empty());
assert!(receiver.receive(&first, 0)?.delivered.is_empty());
assert_eq!(
receiver
.receive(&second, 0)?
.delivered
.iter()
.map(|frame| String::from_utf8(frame.payload.clone()).unwrap())
.collect::<Vec<_>>(),
vec!["second", "third"]
);
Ok(())
}
#[test]
fn rudp_session_fragments_and_reassembles_reliable_ordered_payloads() -> Result<()> {
let mut sender = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 456,
initial_sequence: 1,
..CultNetRudpSessionOptions::default()
});
let mut receiver = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 456,
initial_sequence: 100,
..CultNetRudpSessionOptions::default()
});
sender.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 456,
sequence: 90,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
receiver.receive(
&CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Accept,
connection_id: 456,
sequence: 91,
ack: 0,
ack_mask: 0,
channel_id: "control".to_string(),
reliable: false,
ordered: false,
sequenced: false,
fragment_id: 0,
fragment_index: 0,
fragment_count: 0,
payload: Vec::new(),
},
0,
)?;
let packets = sender.send_many(
"schema",
b"fragment-me-please".to_vec(),
CultNetRudpSendOptions {
reliable: true,
ordered: true,
now_ms: 10,
..CultNetRudpSendOptions::default()
},
Some(5),
)?;
assert_eq!(packets.len(), 4);
assert_eq!(
packets
.iter()
.map(|packet| packet.fragment_count)
.collect::<Vec<_>>(),
vec![4, 4, 4, 4]
);
assert_eq!(
packets
.iter()
.map(|packet| packet.fragment_index)
.collect::<Vec<_>>(),
vec![0, 1, 2, 3]
);
assert!(
packets
.iter()
.all(|packet| packet.fragment_id == packets[0].fragment_id)
);
assert!(receiver.receive(&packets[0], 0)?.delivered.is_empty());
assert!(receiver.receive(&packets[1], 0)?.delivered.is_empty());
assert!(receiver.receive(&packets[2], 0)?.delivered.is_empty());
let delivered = receiver.receive(&packets[3], 0)?.delivered;
assert_eq!(delivered.len(), 1);
assert_eq!(delivered[0].payload, b"fragment-me-please");
assert_eq!(delivered[0].sequence, packets[0].sequence);
Ok(())
}
#[test]
fn rudp_socket_transport_handshakes_and_carries_reliable_ordered_schema_frames() -> Result<()> {
let server_socket = bind_udp_socket()?;
let client_socket = bind_udp_socket()?;
let server_addr = server_socket.local_addr()?;
let connection_id = 0x1020_3040;
let mut server =
CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions {
runtime_id: "rust-rudp-server".to_string(),
socket: server_socket,
mode: CultNetRudpSocketMode::Server,
remote_addr: None,
connection_id,
initial_sequence: 100,
resend_delay_ms: 25,
transport_id: None,
max_payload_bytes: None,
max_fragment_bytes: None,
max_pending_reliable_packets: None,
})?;
let mut client =
CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions {
runtime_id: "rust-rudp-client".to_string(),
socket: client_socket,
mode: CultNetRudpSocketMode::Client,
remote_addr: Some(server_addr),
connection_id,
initial_sequence: 1,
resend_delay_ms: 25,
transport_id: None,
max_payload_bytes: None,
max_fragment_bytes: None,
max_pending_reliable_packets: None,
})?;
client.connect(b"join".to_vec())?;
pump_rudp_handshake(&mut client, &mut server)?;
assert!(client.connected());
assert!(server.connected());
client.send("schema", b"client-state".to_vec())?;
let server_frame = receive_rudp_frame(&mut server)?;
assert_eq!(server_frame.channel_id, "schema");
assert_eq!(server_frame.payload, b"client-state");
server.send("schema", b"server-state".to_vec())?;
let client_frame = receive_rudp_frame(&mut client)?;
assert_eq!(client_frame.channel_id, "schema");
assert_eq!(client_frame.payload, b"server-state");
assert_eq!(
server.profile.transports[0].protocol,
CultNetTransportProtocol::Rudp
);
assert_eq!(client.stats().frames_sent, 1);
assert_eq!(server.stats().frames_received, 1);
Ok(())
}
#[test]
fn rudp_socket_transport_carries_fragmented_reliable_ordered_schema_frames() -> Result<()> {
let server_socket = bind_udp_socket()?;
let client_socket = bind_udp_socket()?;
let server_addr = server_socket.local_addr()?;
let connection_id = 0x1020_3041;
let mut server =
CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions {
runtime_id: "rust-rudp-fragment-server".to_string(),
socket: server_socket,
mode: CultNetRudpSocketMode::Server,
remote_addr: None,
connection_id,
initial_sequence: 100,
resend_delay_ms: 25,
transport_id: None,
max_payload_bytes: None,
max_fragment_bytes: Some(8),
max_pending_reliable_packets: None,
})?;
let mut client =
CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions {
runtime_id: "rust-rudp-fragment-client".to_string(),
socket: client_socket,
mode: CultNetRudpSocketMode::Client,
remote_addr: Some(server_addr),
connection_id,
initial_sequence: 1,
resend_delay_ms: 25,
transport_id: None,
max_payload_bytes: None,
max_fragment_bytes: Some(8),
max_pending_reliable_packets: None,
})?;
client.connect(b"join".to_vec())?;
pump_rudp_handshake(&mut client, &mut server)?;
let payload = b"this-schema-frame-is-larger-than-one-rudp-fragment".to_vec();
client.send("schema", payload.clone())?;
let server_frame = receive_rudp_frame(&mut server)?;
assert_eq!(server_frame.channel_id, "schema");
assert_eq!(server_frame.payload, payload);
assert_eq!(client.stats().frames_sent, 1);
assert_eq!(server.stats().frames_received, 1);
Ok(())
}
#[test]
fn legacy_gamecult_networking_contract_round_trips_login_union() -> Result<()> {
let message = CultNetMessage::Login {
@ -146,6 +951,7 @@ fn rust_decodes_typescript_generated_cultnet_frames() -> Result<()> {
supported_document_types: Some(vec!["ghostlight.agent-state".to_string()]),
supported_mutation_contracts: None,
supported_message_versions: None,
transport_profiles: None,
supports_schema_catalog: None,
}
);
@ -200,12 +1006,12 @@ fn document_registry_advertises_mutation_contracts_with_bindings() -> Result<()>
#[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 origin_store = temp.path().join("origin.msgpack");
let target_store = temp.path().join("target.msgpack");
let payload = GhostlightAgentStateFixture {
schema_version: "ghostlight.agent_state.v0".to_string(),
agent_id: "epiphany.face".to_string(),
display_name: "Face".to_string(),
agent_id: "epiphany.persona".to_string(),
display_name: "Persona".to_string(),
};
let mut registry = CultNetDocumentRegistry::new();
@ -217,7 +1023,7 @@ fn document_registry_replicates_typed_cultcache_state() -> Result<()> {
origin.register_entry_type::<GhostlightAgentStateFixture>()?;
origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store));
origin.pull_all_backing_stores()?;
origin.put("epiphany.face", &payload)?;
origin.put("epiphany.persona", &payload)?;
let snapshot = registry.create_snapshot_response(&origin, "snapshot-1", None, None)?;
@ -229,34 +1035,34 @@ fn document_registry_replicates_typed_cultcache_state() -> Result<()> {
registry.apply_snapshot_response::<GhostlightAgentStateFixture>(&mut target, &snapshot)?;
assert_eq!(applied, vec![payload.clone()]);
assert_eq!(
target.get_required::<GhostlightAgentStateFixture>("epiphany.face")?,
target.get_required::<GhostlightAgentStateFixture>("epiphany.persona")?,
payload
);
let direct_put = registry.create_document_put_message(
"put-1",
"epiphany.face",
"epiphany.persona",
&GhostlightAgentStateFixture {
display_name: "Face Prime".to_string(),
display_name: "Persona 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");
assert_eq!(updated.display_name, "Persona 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 origin_store = temp.path().join("origin-raw.msgpack");
let target_store = temp.path().join("target-raw.msgpack");
let payload = GhostlightAgentStateFixture {
schema_version: "ghostlight.agent_state.v0".to_string(),
agent_id: "epiphany.face".to_string(),
display_name: "Face".to_string(),
agent_id: "epiphany.persona".to_string(),
display_name: "Persona".to_string(),
};
let mut registry = CultNetDocumentRegistry::new();
@ -268,12 +1074,12 @@ fn raw_snapshot_replication_preserves_messagepack_payload_bytes() -> Result<()>
origin.register_entry_type::<GhostlightAgentStateFixture>()?;
origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store));
origin.pull_all_backing_stores()?;
origin.put("epiphany.face", &payload)?;
origin.put("epiphany.persona", &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")?;
origin.get_required_envelope::<GhostlightAgentStateFixture>("epiphany.persona")?;
let mut target = CultCache::new();
target.register_entry_type::<GhostlightAgentStateFixture>()?;
@ -282,12 +1088,12 @@ fn raw_snapshot_replication_preserves_messagepack_payload_bytes() -> Result<()>
let applied = registry
.apply_raw_snapshot_response::<GhostlightAgentStateFixture>(&mut target, &raw_snapshot)?;
let target_envelope =
target.get_required_envelope::<GhostlightAgentStateFixture>("epiphany.face")?;
target.get_required_envelope::<GhostlightAgentStateFixture>("epiphany.persona")?;
assert_eq!(applied, vec![payload.clone()]);
assert_eq!(target_envelope.payload, source_envelope.payload);
assert_eq!(
target.get_required::<GhostlightAgentStateFixture>("epiphany.face")?,
target.get_required::<GhostlightAgentStateFixture>("epiphany.persona")?,
payload
);
Ok(())
@ -328,6 +1134,21 @@ fn builtin_schema_registry_advertises_canonical_ghostlight_schema_without_inline
);
assert_eq!(ghostlight.schema_json, None);
assert!(!ghostlight.content_hash.is_empty());
let transport_profile = schemas
.iter()
.find(|schema| schema.schema_version.as_deref() == Some("cultnet.transport_profile.v0"))
.expect("transport profile schema is advertised");
assert_eq!(transport_profile.kind, CultNetSchemaKind::SharedContract);
assert_eq!(
transport_profile.schema_id,
"https://github.com/GameCult/cultnet-ts/contracts/cultnet.transport-profile.schema.json"
);
assert_eq!(
transport_profile.wire_contracts,
vec![CultNetWireContract::CultNetSchemaV0]
);
Ok(())
}