Watch
0
0
Fork
You've already forked hyperhive
0

hive-c0re/swarm-*: drop clippy allows and dedupe unix_now + KV open_or_create

No behaviour change. Casts replaced by try_from / Duration math; schedules.rs
reuses agent_renewal::unix_now; the five swarm-queue-client KV open-or-create
copies share one private kv::open_or_create.
This commit is contained in:
atlas 2026-10-09 09:48:03 +02:00 • committed by mara
commit b874d21cda
11 changed files with 74 additions and 133 deletions

View file

@ -62,27 +62,13 @@ pub async fn open_or_create(
client: &async_nats::Client,
) -> Result<async_nats::jetstream::kv::Store, Error> {
let js = async_nats::jetstream::new(client.clone());
match js.get_key_value(BUCKET).await {
Ok(store) => Ok(store),
Err(e) => {
tracing::info!(
bucket = BUCKET,
reason = %e,
"agent-icon bucket not available, creating it"
);
js.create_key_value(async_nats::jetstream::kv::Config {
bucket: BUCKET.to_owned(),
description: "Current icon published by each agent".to_owned(),
history: 1,
..Default::default()
})
.await
.map_err(|source| Error::CreateBucket {
bucket: BUCKET.to_owned(),
source,
})
}
}
crate::kv::open_or_create(
&js,
BUCKET,
"agent-icon",
"Current icon published by each agent".to_owned(),
)
.await
}
#[cfg(test)]

View file

@ -97,28 +97,13 @@ pub async fn open_or_create(
client: &async_nats::Client,
) -> Result<async_nats::jetstream::kv::Store, crate::Error> {
let js = async_nats::jetstream::new(client.clone());
match js.get_key_value(BUCKET).await {
Ok(store) => Ok(store),
Err(e) => {
tracing::info!(
bucket = BUCKET,
reason = %e,
"agent-schedule bucket not available, creating it"
);
js.create_key_value(async_nats::jetstream::kv::Config {
bucket: BUCKET.to_owned(),
description: "Scheduled prompts the swarm controller declares for each agent"
.to_owned(),
history: 1,
..Default::default()
})
.await
.map_err(|source| crate::Error::CreateBucket {
bucket: BUCKET.to_owned(),
source,
})
}
}
crate::kv::open_or_create(
&js,
BUCKET,
"agent-schedule",
"Scheduled prompts the swarm controller declares for each agent".to_owned(),
)
.await
}
/// What reading one agent's key found.

View file

@ -109,27 +109,13 @@ pub async fn open_or_create(
client: &async_nats::Client,
) -> Result<async_nats::jetstream::kv::Store, Error> {
let js = async_nats::jetstream::new(client.clone());
match js.get_key_value(BUCKET).await {
Ok(store) => Ok(store),
Err(e) => {
tracing::info!(
bucket = BUCKET,
reason = %e,
"agent-status bucket not available, creating it"
);
js.create_key_value(async_nats::jetstream::kv::Config {
bucket: BUCKET.to_owned(),
description: "Last status snapshot offered by each agent".to_owned(),
history: 1,
..Default::default()
})
.await
.map_err(|source| Error::CreateBucket {
bucket: BUCKET.to_owned(),
source,
})
}
}
crate::kv::open_or_create(
&js,
BUCKET,
"agent-status",
"Last status snapshot offered by each agent".to_owned(),
)
.await
}
#[cfg(test)]

View file

@ -0,0 +1,34 @@
//! The open-or-create sequence every KV bucket module shares.
use crate::Error;
/// Open `bucket`, creating it with `history: 1` and `description` when it
/// does not exist yet. `label` names the bucket in the creation log line.
pub(crate) async fn open_or_create(
js: &async_nats::jetstream::Context,
bucket: &str,
label: &str,
description: String,
) -> Result<async_nats::jetstream::kv::Store, Error> {
match js.get_key_value(bucket).await {
Ok(store) => Ok(store),
Err(e) => {
tracing::info!(
bucket,
reason = %e,
"{label} bucket not available, creating it"
);
js.create_key_value(async_nats::jetstream::kv::Config {
bucket: bucket.to_owned(),
description,
history: 1,
..Default::default()
})
.await
.map_err(|source| Error::CreateBucket {
bucket: bucket.to_owned(),
source,
})
}
}
}

View file

@ -201,6 +201,9 @@ pub mod agent_icon;
/// writing needs `kv` and reading needs `agent-schedules`.
pub mod agent_schedules;
#[cfg(feature = "kv")]
mod kv;
/// The subject the swarm controller publishes on when the hive-wide knowledge
/// repository has changed. One writer, many readers — every hive subscribes.
///

View file

@ -51,25 +51,11 @@ pub async fn open_or_create(
client: &async_nats::Client,
) -> Result<async_nats::jetstream::kv::Store, Error> {
let js = async_nats::jetstream::new(client.clone());
match js.get_key_value(BUCKET).await {
Ok(store) => Ok(store),
Err(e) => {
tracing::info!(
bucket = BUCKET,
reason = %e,
"status bucket not available, creating it"
);
js.create_key_value(async_nats::jetstream::kv::Config {
bucket: BUCKET.to_owned(),
description: "Last status snapshot offered by each hive".to_owned(),
history: 1,
..Default::default()
})
.await
.map_err(|source| Error::CreateBucket {
bucket: BUCKET.to_owned(),
source,
})
}
}
crate::kv::open_or_create(
&js,
BUCKET,
"status",
"Last status snapshot offered by each hive".to_owned(),
)
.await
}

View file

@ -125,24 +125,8 @@ pub async fn open_or_create(
) -> Result<async_nats::jetstream::kv::Store, Error> {
let js = async_nats::jetstream::new(client.clone());
let bucket = bucket(hive);
match js.get_key_value(&bucket).await {
Ok(store) => Ok(store),
Err(e) => {
tracing::info!(
%bucket,
reason = %e,
"wanted-state bucket not available, creating it"
);
js.create_key_value(async_nats::jetstream::kv::Config {
bucket: bucket.clone(),
description: format!("Agent set the swarm controller declares for hive {hive}"),
history: 1,
..Default::default()
})
.await
.map_err(|source| Error::CreateBucket { bucket, source })
}
}
let description = format!("Agent set the swarm controller declares for hive {hive}");
crate::kv::open_or_create(&js, &bucket, "wanted-state", description).await
}
/// Open the wanted-state bucket for reading, or `None` when it does not exist.