Implement chrs-backbeat: Temporal orchestration with Pulse/Reverb and agent synchronization

This commit is contained in:
anthonyrawlins
2026-03-04 03:07:01 +11:00
parent ffe37a4292
commit 00623ac125
4 changed files with 204 additions and 0 deletions

13
chrs-backbeat/Cargo.toml Normal file
View File

@@ -0,0 +1,13 @@
[package]
name = "chrs-backbeat"
version = "0.1.0"
edition = "2021"
[dependencies]
chrs-mail = { path = "../chrs-mail" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
chrono = { version = "0.4", features = ["serde"] }
tokio = { version = "1.0", features = ["full"] }
uuid = { version = "1.0", features = ["v4", "serde"] }

110
chrs-backbeat/src/lib.rs Normal file
View File

@@ -0,0 +1,110 @@
//! chrs-backbeat: Temporal Orchestration (Pulse/Reverb) for CHORUS.
use chrs_mail::{Mailbox, Message};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
use std::time::Duration;
use tokio::time::sleep;
/// Represents a single rhythmic unit in the CHORUS cluster.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BeatFrame {
pub cluster_id: String,
pub tempo_bpm: u32,
pub beat_index: u32,
pub beat_epoch: DateTime<Utc>,
pub downbeat: bool,
pub phase: String,
}
/// Represents an agent's reported status relative to a beat.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct StatusClaim {
pub agent_id: String,
pub task_id: Option<Uuid>,
pub beat_index: u32,
pub state: String,
pub progress: f32,
}
#[derive(Debug, Error)]
pub enum BackbeatError {
#[error("Mailbox error: {0}")]
Mailbox(#[from] chrs_mail::MailError),
}
/// The Pulse component broadcasts the cluster rhythm.
pub struct Pulse {
pub cluster_id: String,
pub tempo_bpm: u32,
mailbox: Mailbox,
}
impl Pulse {
pub fn new(cluster_id: &str, tempo_bpm: u32, mailbox: Mailbox) -> Self {
Self {
cluster_id: cluster_id.into(),
tempo_bpm,
mailbox,
}
}
/// Starts the pulse loop, emitting a BeatFrame every beat.
pub async fn run(&self) -> Result<(), BackbeatError> {
let mut beat_index = 1;
let beat_duration = Duration::from_secs(60) / self.tempo_bpm;
println!("[PULSE] Starting at {} BPM ({:?} per beat)", self.tempo_bpm, beat_duration);
loop {
let now = Utc::now();
let frame = BeatFrame {
cluster_id: self.cluster_id.clone(),
tempo_bpm: self.tempo_bpm,
beat_index,
beat_epoch: now,
downbeat: beat_index == 1,
phase: "active".into(), // Simplified for POC
};
let msg = Message {
id: Uuid::new_v4(),
from_peer: "pulse".into(),
to_peer: "council".into(),
topic: "beat_frame".into(),
payload: serde_json::to_value(&frame).unwrap(),
sent_at: now,
read_at: None,
};
self.mailbox.send(&msg)?;
// Increment and cycle beat_index (default bar length = 8)
beat_index = if beat_index >= 8 { 1 } else { beat_index + 1 };
sleep(beat_duration).await;
}
}
}
/// The Reverb component aggregates status claims into bar reports.
pub struct Reverb {
mailbox: Mailbox,
}
impl Reverb {
pub fn new(mailbox: Mailbox) -> Self {
Self { mailbox }
}
/// Collects and summarizes status claims since a given time.
pub fn collect_report(&self, since: DateTime<Utc>) -> Result<Vec<StatusClaim>, BackbeatError> {
let messages = self.mailbox.receive_broadcasts("status_claim", since)?;
let claims: Vec<StatusClaim> = messages.into_iter()
.filter_map(|m| serde_json::from_value(m.payload).ok())
.collect();
Ok(claims)
}
}