Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: drop the relay subscription to the refused hive-scoped agent subject

This commit is contained in:
atlas 2026-10-04 21:43:33 +02:00 • committed by mara
commit 8a7733b3f5
3 changed files with 37 additions and 63 deletions

View file

@ -3,15 +3,13 @@
//!
//! **No `hive` in the path, on purpose.** An agent is not pinned to a
//! hive forever — it can move — so a URL naming one would go stale the
//! moment it did. The hive is resolved at request time from
//! `crate::agent_status`'s bucket instead, the same "an agent's hive
//! comes from its own last report" rule that module already documents.
//! moment it did. `crate::agent_status`'s bucket is read only to answer
//! 404 for an agent that has never reported.
//!
//! **Live tail only, on purpose.** `hive-agent` publishes each
//! already-classified `TermMsg` row to the core subject
//! `$SWARM.term.{agent}` (`$SWARM.term.{hive}.{agent}` from an agent on its
//! hive's shared credential; see `hive-agent::swarm_term`'s module
//! doc) — a core subject, not `JetStream`, so a subscriber who was not
//! `$SWARM.term.{agent}` (see `hive-agent::swarm_term`'s module doc) —
//! a core subject, not `JetStream`, so a subscriber who was not
//! listening missed the row, same as on the agent's own local SSE
//! stream. This handler relays exactly that: no replay, no last-N
//! history. The publish side's own retention half was deliberately
@ -35,8 +33,8 @@ use crate::AppState;
/// Subject family carrying agent terminal rows — must match
/// `hive-agent::swarm_term::SUBJECT_PREFIX` exactly, since the two ends
/// never see the constant together. [`agent_subjects`] spells the full
/// subjects; there is no way to check the two sides agree short of
/// never see the constant together. [`agent_subject`] spells the full
/// subject; there is no way to check the two sides agree short of
/// this comment and the module docs on both ends staying honest about it.
const SUBJECT_PREFIX: &str = "$SWARM.term";
@ -84,7 +82,7 @@ pub(crate) async fn stream_agent_term(
// One-agent "roster": reuses `AgentStatusReader::view`'s already-tested
// row logic rather than a second, narrower lookup that could disagree
// with it about what "never reported" means.
let hive = agent_status
agent_status
.view(std::slice::from_ref(&agent), SystemTime::now())
.await
.map_err(|e| {
@ -117,63 +115,41 @@ pub(crate) async fn stream_agent_term(
)
})?;
let subscriber = subscribe_all(
&client,
&agent_subjects(SUBJECT_PREFIX, &hive, &agent),
"term",
)
.await?;
let subscriber = subscribe(&client, agent_subject(SUBJECT_PREFIX, &agent), "term").await?;
let stream =
subscriber.map(|msg| Ok(Event::default().data(String::from_utf8_lossy(&msg.payload))));
Ok(Sse::new(stream).keep_alive(KeepAlive::default()))
}
/// The subjects one agent publishes on under `prefix`: its own
/// `<prefix>.<agent>`, and `<prefix>.<hive>.<agent>`, which an agent still on
/// its hive's shared queue credential publishes to.
pub(crate) fn agent_subjects(prefix: &str, hive: &str, agent: &str) -> [String; 2] {
[
format!("{prefix}.{agent}"),
format!("{prefix}.{hive}.{agent}"),
]
/// The subject one agent publishes on under `prefix`: `<prefix>.<agent>`.
pub(crate) fn agent_subject(prefix: &str, agent: &str) -> String {
format!("{prefix}.{agent}")
}
/// Subscribe to every one of `subjects`, merged into one stream. `what` names
/// the relay in logs and errors.
pub(crate) async fn subscribe_all(
/// Subscribe to `subject`. `what` names the relay in logs and errors.
pub(crate) async fn subscribe(
client: &async_nats::Client,
subjects: &[String],
subject: String,
what: &str,
) -> Result<futures_util::stream::SelectAll<async_nats::Subscriber>, problem_details::ProblemDetails>
{
let mut subscribers = Vec::with_capacity(subjects.len());
for subject in subjects {
let subscriber = client.subscribe(subject.clone()).await.map_err(|e| {
tracing::warn!(%subject, error = %e, "{what} stream: subscribe failed");
crate::error_problem(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
&format!("subscribing to {subject} failed: {e}"),
)
})?;
subscribers.push(subscriber);
}
tracing::info!(?subjects, "{what} stream: client attached");
Ok(futures_util::stream::select_all(subscribers))
) -> Result<async_nats::Subscriber, problem_details::ProblemDetails> {
let subscriber = client.subscribe(subject.clone()).await.map_err(|e| {
tracing::warn!(%subject, error = %e, "{what} stream: subscribe failed");
crate::error_problem(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
&format!("subscribing to {subject} failed: {e}"),
)
})?;
tracing::info!(%subject, "{what} stream: client attached");
Ok(subscriber)
}
#[cfg(test)]
mod tests {
use super::*;
/// Both shapes, so an agent on either credential is streamed.
/// The subject the agent's own queue credential is granted.
#[test]
fn a_relay_subscribes_to_the_agents_own_subject_and_its_hive_scoped_one() {
assert_eq!(
agent_subjects(SUBJECT_PREFIX, "h1", "atlas"),
[
"$SWARM.term.atlas".to_owned(),
"$SWARM.term.h1.atlas".to_owned(),
]
);
fn a_relay_subscribes_to_the_agents_own_subject() {
assert_eq!(agent_subject(SUBJECT_PREFIX, "atlas"), "$SWARM.term.atlas");
}
}