Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

M-F3: name the zone in server-written 'out until' times; RFC 3339 retry_at (#245)

Oleksandr Ivanchenko790107d76b2c
diff --git a/src/routines.rs b/src/routines.rs
index d6462788..dd0d075b 100644
--- a/src/routines.rs
+++ b/src/routines.rs
@@ -217,6 +217,23 @@ pub fn now_in(tz: &str, now: DateTime<Utc>) -> Result<DateTime<chrono::FixedOffs
Ok(now.with_timezone(&offset))
}
+/// `at` as "09:00 CET": the wall-clock time in `tz` and that zone's abbreviation at that instant
+/// ("CEST" in summer, "+03" where the database only has a number). `Err` for an unknown zone or
+/// one with no abbreviation for `at`, so the caller can say it in UTC instead.
+pub fn clock_with_zone(tz: &str, at: DateTime<Utc>) -> Result<String, String> {
+ let zone = parse_tz(tz)?;
+ let local = zone
+ .0
+ .find_local_time_type(at.timestamp())
+ .map_err(|_| format!("time zone '{tz}' has no offset for that time"))?;
+ let name = local.time_zone_designation();
+ if name.is_empty() {
+ return Err(format!("time zone '{tz}' has no abbreviation for that time"));
+ }
+ let offset = chrono::FixedOffset::east_opt(local.ut_offset()).ok_or_else(|| format!("time zone '{tz}' has an odd offset"))?;
+ Ok(format!("{} {name}", at.with_timezone(&offset).format("%H:%M")))
+}
+
/// `date` at wall-clock `time` in `tz` (see [`resolve_local`] for DST gaps), for reminders that
/// are not routines. `Err` for an unknown zone.
pub fn local_instant(tz: &str, date: NaiveDate, time: NaiveTime) -> Result<Option<DateTime<Utc>>, String> {
diff --git a/src/server/profile_turn.rs b/src/server/profile_turn.rs
index cea1aacb..d8feb7c6 100644
--- a/src/server/profile_turn.rs
+++ b/src/server/profile_turn.rs
@@ -512,17 +512,25 @@ pub fn engine_label(p: &LlmProfile) -> String {
}
}
-/// `at` as a wall-clock time in the person's zone ("03:00"), else the server's, else "03:00 UTC".
+/// `at` as a clock time in the person's zone that names it: "09:00 CET". The zone is the saved one,
+/// else this computer's, else UTC (`zombie::settings::local_tz`).
pub async fn clock_text(state: &AppState, at: chrono::DateTime<chrono::Utc>) -> String {
- let tz = crate::zombie::settings::local_tz(&state.db).await;
- match crate::routines::now_in(&tz, at) {
- Ok(local) => local.format("%H:%M").to_string(),
- Err(_) => format!("{} UTC", at.format("%H:%M")),
- }
+ clock_text_in(&crate::zombie::settings::local_tz(&state.db).await, at)
+}
+
+/// [`clock_text`] in `tz`. A zone that cannot be read, or has no abbreviation, gives the time
+/// converted to UTC and labelled "UTC", never a local time under the wrong label.
+pub fn clock_text_in(tz: &str, at: chrono::DateTime<chrono::Utc>) -> String {
+ crate::routines::clock_with_zone(tz, at).unwrap_or_else(|_| format!("{} UTC", at.format("%H:%M")))
+}
+
+/// `at` as RFC 3339 with an explicit offset ("2026-11-03T08:00:00+00:00"), for `meta.failover`.
+pub fn rfc3339(at: chrono::DateTime<chrono::Utc>) -> String {
+ at.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)
}
/// The quiet line when a turn runs on another engine because its own is out:
-/// "Claude is out until 03:00 · Architect is answering on Codex (gpt-6.1)".
+/// "Claude is out until 03:00 UTC · Architect is answering on Codex (gpt-6.1)".
pub fn cross_engine_notice(
from: &LlmProfile,
until_text: Option<&str>,
@@ -607,6 +615,37 @@ mod tests {
assert!(give_up_notice("Ann", &p, &quota, 1).contains("Settings › This space › Accounts"));
}
+ #[test]
+ fn out_until_text_names_its_zone() {
+ let winter = Utc.with_ymd_and_hms(2026, 11, 3, 8, 0, 0).unwrap();
+ let summer = Utc.with_ymd_and_hms(2026, 7, 3, 8, 0, 0).unwrap();
+ // The person's zone, with its abbreviation at that instant
+ assert_eq!(clock_text_in("Europe/Ljubljana", winter), "09:00 CET");
+ assert_eq!(clock_text_in("Europe/Ljubljana", summer), "10:00 CEST");
+ assert_eq!(clock_text_in("America/New_York", winter), "03:00 EST");
+ assert_eq!(clock_text_in("Europe/Istanbul", winter), "11:00 +03");
+ assert_eq!(clock_text_in("UTC", winter), "08:00 UTC");
+ // Unreadable zones are converted to UTC, not labelled local
+ for bad in ["Not/AZone", "", "../etc/passwd", "Europe/Ljubljana/../x"] {
+ assert_eq!(clock_text_in(bad, winter), "08:00 UTC", "{bad:?}");
+ }
+ let line = all_out_line(&clock_text_in("Europe/Ljubljana", winter));
+ assert_eq!(line, "Every model is out until 09:00 CET; I'll answer then.");
+ let claude = profile("c", "This machine", "system", 0, false);
+ let codex = profile("x", "Codex", "system", 1, false);
+ let line = cross_engine_notice(&claude, Some(&clock_text_in("Europe/Ljubljana", winter)), "Ann", &codex, Some("gpt-6.1"));
+ assert!(line.contains("is out until 09:00 CET · Ann is answering on"), "{line}");
+ let line = cross_engine_notice(&claude, None, "Ann", &codex, None);
+ assert!(line.contains("is unavailable") && !line.contains("until"), "{line}");
+ }
+
+ #[test]
+ fn retry_at_is_rfc3339_with_an_offset() {
+ let at = Utc.with_ymd_and_hms(2026, 11, 3, 8, 0, 5).unwrap();
+ assert_eq!(rfc3339(at), "2026-11-03T08:00:05+00:00");
+ assert_eq!(DateTime::parse_from_rfc3339(&rfc3339(at)).unwrap().with_timezone(&Utc), at);
+ }
+
fn report(id: EngineId, enabled: bool, status: EngineStatus) -> EngineReport {
EngineReport {
id,
diff --git a/src/server/routes.rs b/src/server/routes.rs
index bfb3eb8a..76fddfa5 100644
--- a/src/server/routes.rs
+++ b/src/server/routes.rs
@@ -2612,7 +2612,7 @@ async fn run_kwant_turn_inner(
"to_profile_id": to.id,
"to_name": to.name,
"class": class,
- "retry_at": from.until,
+ "retry_at": from.until.map(super::profile_turn::rfc3339),
"from_engine": from_family,
"to_engine": to_family,
"from_model": from.model,
diff --git a/tests/model_failover_test.rs b/tests/model_failover_test.rs
index cedf0209..5e1fd14c 100644
--- a/tests/model_failover_test.rs
+++ b/tests/model_failover_test.rs
@@ -127,6 +127,23 @@ async fn last_reply(state: &AppState, kwant_id: &str) -> (String, Value) {
(m.text.clone(), m.meta.as_deref().and_then(|s| serde_json::from_str(s).ok()).unwrap_or(Value::Null))
}
+/// "03:00 CET" / "04:00 CEST": `at` on a Europe/Ljubljana wall clock, with the zone's abbreviation.
+/// Written from the EU rule (summer time runs 01:00 UTC on the last Sunday of March to the same
+/// moment in October), not from the code under test.
+fn ljubljana(at: chrono::DateTime<Utc>) -> String {
+ use chrono::{Datelike, NaiveDate, TimeZone, Weekday};
+ let last_sunday = |month: u32| {
+ let mut d = NaiveDate::from_ymd_opt(at.year(), month, 31).unwrap();
+ while d.weekday() != Weekday::Sun {
+ d = d.pred_opt().unwrap();
+ }
+ Utc.from_utc_datetime(&d.and_hms_opt(1, 0, 0).unwrap())
+ };
+ let summer = at >= last_sunday(3) && at < last_sunday(10);
+ let (hours, name) = if summer { (2, "CEST") } else { (1, "CET") };
+ format!("{} {name}", (at + Span::hours(hours)).format("%H:%M"))
+}
+
#[tokio::test]
async fn a_session_limit_moves_the_bot_to_the_next_engine_and_back() {
let tmp = tempfile::tempdir().unwrap();
@@ -160,6 +177,8 @@ async fn a_session_limit_moves_the_bot_to_the_next_engine_and_back() {
PermissionBroker::new(),
LayaRouter::new(config.laya_url.clone()),
);
+ // The person's saved zone: server text reads in it and names it
+ state.db.set_setting("timezone", "Europe/Ljubljana").await.unwrap();
let app = create_router(state.clone());
let (s, v) = call(&app, "POST", "/api/engines/recheck", None).await;
assert_eq!(s, StatusCode::OK, "{v}");
@@ -205,6 +224,12 @@ async fn a_session_limit_moves_the_bot_to_the_next_engine_and_back() {
let ps = &meta["failover"][0];
assert_eq!((ps["from_engine"].clone(), ps["to_engine"].clone()), (json!("claude"), json!("codex")), "{meta}");
assert!(ps["to_model"].is_string() && ps["from_profile_id"] == "prf_system_claude" && ps["retry_at"].is_string(), "{meta}");
+ // retry_at is RFC 3339 with an offset (so the client localises it) and the notice text names
+ // its zone, never the server's or the person's own clock unlabelled
+ let stamp = ps["retry_at"].as_str().unwrap();
+ let parsed = chrono::DateTime::parse_from_rfc3339(stamp).unwrap_or_else(|e| panic!("retry_at {stamp:?}: {e}"));
+ assert!(stamp.ends_with("+00:00") && stamp.contains('T'), "{stamp}");
+ assert!(ps["notice"].as_str().unwrap().contains(&format!("is out until {}", ljubljana(parsed.with_timezone(&Utc)))), "{ps}");
let saved = state.db.list_messages(&kid, 50).await.unwrap();
assert!(!saved.iter().any(|m| m.sender_role == "kwant" && m.text.contains("session limit")), "{saved:?}");
@@ -255,6 +280,10 @@ async fn a_session_limit_moves_the_bot_to_the_next_engine_and_back() {
let events = run_turn(&app, &state, &kid).await;
let text = text_of(&events);
assert!(text.contains("Every model is out until") && text.contains("I'll answer then."), "{text}");
+ // The person's zone, named, and the earliest reset (Codex's, in two hours) as written in it
+ let wait = Utc::now() + Span::hours(2);
+ let near = [wait, wait - Span::minutes(1), wait + Span::minutes(1)].map(|t| format!("Every model is out until {}", ljubljana(t)));
+ assert!(near.iter().any(|n| text.contains(n)), "{text}");
assert_eq!((runs(tmp.path(), "claude"), runs(tmp.path(), "codex")), (2, 2), "nothing was attempted");
let parked = state.db.list_parked().await.unwrap();
assert_eq!(parked.len(), 1, "{parked:?}");
@@ -264,7 +293,7 @@ async fn a_session_limit_moves_the_bot_to_the_next_engine_and_back() {
// A second message while everything is out is parked with the same line, never a setup card
let events = run_turn(&app, &state, &kid).await;
let text = text_of(&events);
- assert!(text.contains("Every model is out until") && !text.contains("No account can run bots"), "{text}");
+ assert!(text.contains("Every model is out until") && (text.contains(" CET") || text.contains(" CEST")) && !text.contains("No account can run bots"), "{text}");
let (reply, meta) = last_reply(&state, &kid).await;
assert!(!reply.contains("Settings") && meta.get("kind") != Some(&json!("setup_needed")), "{reply} {meta}");
assert_eq!((runs(tmp.path(), "claude"), runs(tmp.path(), "codex")), (2, 2), "nothing was attempted");