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.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
Import: keep a routine's time zone from its schedule text or JSON
Lin (kwant bot)122c7f31e1c4
diff --git a/src/import/mod.rs b/src/import/mod.rsindex bc188ef9..edf5bb1b 100644--- a/src/import/mod.rs+++ b/src/import/mod.rs@@ -290,23 +290,77 @@ pub fn split_memory(text: &str) -> Vec<String> {}/// Reads a routine's schedule: kwant JSON first, then the source's text. Imported routines-/// always arrive paused, so a schedule only has to be valid, not right.-pub fn read_schedule(spec: &RoutineSpec) -> Option<Schedule> {- if let Some(v) = &spec.schedule {- if let Ok(s) = serde_json::from_value::<Schedule>(v.clone()) {- if s.validate().is_ok() {- return Some(s.canonical());+/// always arrive paused, so a schedule only has to be valid, not right. The second value is a+/// note for the preview when part of it (so far only the time zone) had to be guessed.+pub fn read_schedule(spec: &RoutineSpec) -> Option<(Schedule, Option<String>)> {+ match &spec.schedule {+ Some(serde_json::Value::String(t)) => {+ if let Some(found) = schedule_from_text(t) {+ return Some(found);}}+ Some(v @ serde_json::Value::Object(o)) => {+ let mut v = v.clone();+ // OpenMausBot and other apps write `timeZone`; kwant writes `tz`.+ if !o.contains_key("tz") {+ if let Some(zone) = o.get("timeZone").or_else(|| o.get("time_zone")).or_else(|| o.get("timezone")).cloned() {+ v["tz"] = zone;+ }+ }+ if let Ok(s) = serde_json::from_value::<Schedule>(v) {+ if s.validate().is_ok() {+ return Some((s.canonical(), None));+ }+ // A good expression or time with a zone kwant doesn't know: keep it in UTC.+ if let Some((zone, utc)) = with_utc(&s) {+ if utc.validate().is_ok() {+ return Some((utc.canonical(), Some(zone_note(&zone))));+ }+ }+ }+ }+ _ => {}}let text = spec.schedule_text.as_deref()?.trim();schedule_from_text(text)}-fn schedule_from_text(text: &str) -> Option<Schedule> {- let lower = text.to_lowercase();+/// The schedule moved to UTC, with the zone it had, when it has a zone at all.+fn with_utc(s: &Schedule) -> Option<(String, Schedule)> {+ let utc = "UTC".to_string();+ Some(match s.clone() {+ Schedule::Daily { time, tz } => (tz, Schedule::Daily { time, tz: utc }),+ Schedule::Weekly { time, weekdays, tz } => (tz, Schedule::Weekly { time, weekdays, tz: utc }),+ Schedule::Monthly { time, days, tz } => (tz, Schedule::Monthly { time, days, tz: utc }),+ Schedule::Cron { expression, tz } => (tz, Schedule::Cron { expression, tz: utc }),+ Schedule::Once { .. } | Schedule::Interval { .. } => return None,+ })+}++fn zone_note(zone: &str) -> String {+ let zone = one_line(zone, 64);+ if zone.is_empty() {+ "no time zone was given, so we used UTC. Check the time before turning it on.".into()+ } else {+ format!("we don't know the time zone \"{zone}\", so we used UTC. Check the time before turning it on.")+ }+}++fn schedule_from_text(text: &str) -> Option<(Schedule, Option<String>)> {+ let (rest, zone) = split_zone(text);+ let (tz, note) = match zone {+ None => ("UTC".to_string(), None),+ Some(z) => {+ let probe = Schedule::Daily { time: "00:00".into(), tz: z.clone() };+ if probe.validate().is_ok() {+ (z, None)+ } else {+ ("UTC".to_string(), Some(zone_note(&z)))+ }+ }+ };+ let lower = rest.to_lowercase();let lower = lower.trim();- let tz = "UTC".to_string();// "daily 09:00", "every day at 9:00", "09:00"let time = lower.trim_start_matches("every day")@@ -314,18 +368,73 @@ fn schedule_from_text(text: &str) -> Option<Schedule> {.trim_start_matches(" at").trim();if let Some(t) = clock(time) {- return Some(Schedule::Daily { time: t, tz });+ return Some((Schedule::Daily { time: t, tz }.canonical(), note));}- let fields: Vec<&str> = text.split_whitespace().collect();+ let fields: Vec<&str> = rest.split_whitespace().collect();if fields.len() == 5 {let s = Schedule::Cron { expression: fields.join(" "), tz };if s.validate().is_ok() {- return Some(s.canonical());+ return Some((s.canonical(), note));}}None}+/// Takes a time zone out of a schedule line: `0 7 * * * (Europe/Berlin)`, `0 7 * * * Europe/Berlin`,+/// `tz=Europe/Berlin`, `TZ: Europe/Berlin`, `CRON_TZ=Europe/Berlin 0 7 * * *`. Returns the rest+/// of the line and the zone as written; whether the zone exists is checked by the caller.+fn split_zone(text: &str) -> (String, Option<String>) {+ let tokens: Vec<&str> = text.split_whitespace().collect();+ let mut rest = Vec::new();+ let mut zone = None;+ let mut i = 0;+ while i < tokens.len() {+ let tok = tokens[i];+ let bare = tok.trim_matches(|c| "()[],;".contains(c));+ let lower = bare.to_ascii_lowercase();+ let labelled = ["cron_tz", "tz", "timezone", "time_zone"].iter().find_map(|label| {+ let after = lower.strip_prefix(label)?;+ let sep = after.chars().next()?;+ ("=:".contains(sep)).then(|| bare[label.len() + 1..].to_string())+ });+ if let Some(value) = labelled {+ if value.is_empty() {+ // "TZ: Europe/Berlin": the value is the next word+ if let Some(next) = tokens.get(i + 1) {+ zone = Some(next.trim_matches(|c| "()[],;".contains(c)).to_string());+ i += 1;+ }+ } else {+ zone = Some(value);+ }+ } else if zone.is_none() && looks_like_zone(bare) && (tok != bare || i == tokens.len() - 1) {+ // In brackets anywhere, or the last word+ zone = Some(bare.to_string());+ } else if zone.is_none() && tok.starts_with('(') && (lower.starts_with("utc") || lower.starts_with("gmt")) {+ // "(UTC+2)": an offset kwant can't store as a zone, so the caller falls back with a note+ zone = Some(bare.to_string());+ } else {+ rest.push(tok.trim_end_matches([',', ';']));+ }+ i += 1;+ }+ (rest.join(" "), zone)+}++/// `Europe/Berlin`, `America/Argentina/Buenos_Aires`, `UTC`: never a cron field like `*/5`.+fn looks_like_zone(s: &str) -> bool {+ if s.eq_ignore_ascii_case("utc") || s.eq_ignore_ascii_case("gmt") {+ return true;+ }+ let mut parts = s.split('/');+ let first = parts.next().unwrap_or("");+ s.contains('/')+ && first.len() >= 2+ && first.starts_with(|c: char| c.is_ascii_uppercase())+ && first.chars().all(|c| c.is_ascii_alphabetic())+ && parts.all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_alphanumeric() || "_+-".contains(c)))+}+/// `H:MM` or `HH:MM` as `HH:MM`.fn clock(s: &str) -> Option<String> {let (h, m) = s.split_once(':')?;@@ -591,7 +700,12 @@ fn tidy_routines(continue;}let schedule = match read_schedule(&r) {- Some(s) => s,+ Some((s, note)) => {+ if let Some(note) = note {+ warnings.push(Notice::new(who, format!("Routine {label}: {note}")));+ }+ s+ }None => {warnings.push(Notice::new(who,@@ -663,12 +777,74 @@ mod tests {assert_eq!(free_name("Ada", &taken), "Ada-3");}+ fn cron(expression: &str, tz: &str) -> Schedule {+ Schedule::Cron { expression: expression.into(), tz: tz.into() }+ }++ fn routine(schedule: Option<serde_json::Value>, text: Option<&str>) -> RoutineSpec {+ RoutineSpec { name: "r".into(), instructions: "i".into(), schedule, schedule_text: text.map(Into::into) }+ }+#[test]fn schedules_from_text() {- assert_eq!(schedule_from_text("09:30"), Some(Schedule::Daily { time: "09:30".into(), tz: "UTC".into() }));- assert_eq!(schedule_from_text("daily 7:05"), Some(Schedule::Daily { time: "07:05".into(), tz: "UTC".into() }));- assert!(matches!(schedule_from_text("0 9 * * 1-5"), Some(Schedule::Cron { .. })));- assert_eq!(schedule_from_text("whenever it feels right"), None);+ let utc = |s| schedule_from_text(s).map(|(s, _)| s);+ assert_eq!(utc("09:30"), Some(Schedule::Daily { time: "09:30".into(), tz: "UTC".into() }));+ assert_eq!(utc("daily 7:05"), Some(Schedule::Daily { time: "07:05".into(), tz: "UTC".into() }));+ assert!(matches!(utc("0 9 * * 1-5"), Some(Schedule::Cron { .. })));+ assert_eq!(utc("*/15 9-17 * * MON-FRI"), Some(cron("*/15 9-17 * * MON-FRI", "UTC")), "cron steps aren't zones");+ assert_eq!(utc("whenever it feels right"), None);+ }++ #[test]+ fn schedule_text_time_zones() {+ let berlin = Some((cron("0 7 * * *", "Europe/Berlin"), None));+ // Oleks's first real import, 2026-10-19+ assert_eq!(schedule_from_text("0 7 * * * (Europe/Berlin)"), berlin);+ assert_eq!(schedule_from_text("0 7 * * * Europe/Berlin"), berlin);+ assert_eq!(schedule_from_text("0 7 * * * [Europe/Berlin]"), berlin);+ assert_eq!(schedule_from_text("0 7 * * * tz=Europe/Berlin"), berlin);+ assert_eq!(schedule_from_text("0 7 * * *, TZ: Europe/Berlin"), berlin);+ assert_eq!(schedule_from_text("TZ:Europe/Berlin 0 7 * * *"), berlin);+ assert_eq!(schedule_from_text("CRON_TZ=Europe/Berlin 0 7 * * *"), berlin);+ assert_eq!(schedule_from_text("timezone=America/Argentina/Buenos_Aires 0 7 * * *"), Some((cron("0 7 * * *", "America/Argentina/Buenos_Aires"), None)));+ assert_eq!(schedule_from_text("0 7 * * * UTC"), Some((cron("0 7 * * *", "UTC"), None)));+ // The daily path takes the zone too+ let daily = Some((Schedule::Daily { time: "07:00".into(), tz: "Europe/Berlin".into() }, None));+ assert_eq!(schedule_from_text("every day at 7:00 (Europe/Berlin)"), daily);+ assert_eq!(schedule_from_text("daily 07:00 Europe/Berlin"), daily);+ // Old names become current ones, as everywhere else in kwant+ assert_eq!(schedule_from_text("0 7 * * * (Europe/Kiev)").map(|(s, _)| s), Some(cron("0 7 * * *", "Europe/Kyiv")));+ }++ #[test]+ fn unknown_schedule_zones_fall_back_to_utc_with_a_note() {+ for text in ["0 7 * * * (Mars/Olympus)", "0 7 * * * tz=Nowhere", "CRON_TZ=../../etc/passwd 0 7 * * *", "0 7 * * * (UTC+2)"] {+ let (s, note) = schedule_from_text(text).unwrap_or_else(|| panic!("{text}"));+ assert_eq!(s, cron("0 7 * * *", "UTC"), "{text}");+ assert!(note.as_deref().unwrap_or("").contains("so we used UTC"), "{text}: {note:?}");+ }+ let (s, note) = schedule_from_text("daily 07:00 tz=Mars/Olympus").unwrap();+ assert_eq!(s, Schedule::Daily { time: "07:00".into(), tz: "UTC".into() });+ assert!(note.unwrap().contains("Mars/Olympus"));+ }++ #[test]+ fn json_schedules_keep_their_zone_or_fall_back_to_utc() {+ let berlin = cron("0 7 * * *", "Europe/Berlin");+ let j = |v| read_schedule(&routine(Some(v), None));+ assert_eq!(j(serde_json::json!({"type": "cron", "expression": "0 7 * * *", "tz": "Europe/Berlin"})), Some((berlin.clone(), None)));+ assert_eq!(j(serde_json::json!({"type": "cron", "expression": "0 7 * * *", "timeZone": "Europe/Berlin"})), Some((berlin.clone(), None)));+ // An empty or unknown zone (OpenMausBot packages can leave timeZone out) keeps the expression in UTC.+ for tz in ["", "Mars/Olympus", "<time zone like Europe/Berlin, or UTC>"] {+ let (s, note) = j(serde_json::json!({"type": "cron", "expression": "0 7 * * *", "tz": tz})).unwrap();+ assert_eq!(s, cron("0 7 * * *", "UTC"), "{tz}");+ assert!(note.unwrap().contains("so we used UTC"), "{tz}");+ }+ // A schedule written as a string, and a broken object with usable text+ assert_eq!(j(serde_json::json!("0 7 * * * (Europe/Berlin)")), Some((berlin.clone(), None)));+ let broken = routine(Some(serde_json::json!({"type": "cron", "expression": "<cron line>", "tz": "UTC"})), Some("0 7 * * * (Europe/Berlin)"));+ assert_eq!(read_schedule(&broken), Some((berlin, None)));+ assert_eq!(read_schedule(&routine(None, Some("whenever"))), None);}#[test]diff --git a/src/import/parsers.rs b/src/import/parsers.rsindex 73e42325..097350ca 100644--- a/src/import/parsers.rs+++ b/src/import/parsers.rs@@ -409,7 +409,8 @@ fn from_grokbot(v: &Value) -> Bundle {.map(|r| RoutineSpec {name: s(r, "name"),instructions: any_s(r, &["prompt", "instructions"]),- schedule: None,+ // An object like {"type": "cron", "expression": …, "timeZone": …} is read as is.+ schedule: r.get("schedule").filter(|v| v.is_object()).cloned(),schedule_text: Some(any_s(r, &["schedule", "cron", "time"])).filter(|t| !t.is_empty()),}).collect();@@ -453,10 +454,11 @@ fn openmaus_schedule(sch: &Value) -> (Option<Value>, Option<String>) {"daily" => {let wd: Vec<String> = arr(sch, "weekdays").iter().filter_map(Value::as_u64).filter_map(|d| days.get(d as usize)).map(|d| d.to_string()).collect();let time = s(sch, "time");+ let tz = Some(any_s(sch, &["timeZone", "tz"])).filter(|t| !t.is_empty()).unwrap_or_else(|| "UTC".into());if wd.is_empty() || wd.len() == 7 {- (Some(serde_json::json!({"type": "daily", "time": time, "tz": "UTC"})), Some(time))+ (Some(serde_json::json!({"type": "daily", "time": time, "tz": tz})), Some(time))} else {- (Some(serde_json::json!({"type": "weekly", "time": time, "weekdays": wd, "tz": "UTC"})), Some(time))+ (Some(serde_json::json!({"type": "weekly", "time": time, "weekdays": wd, "tz": tz})), Some(time))}}"interval" => (@@ -570,7 +572,8 @@ fn from_openmaus_grok(v: &Value) -> Bundle {.map(|r| RoutineSpec {name: any_s(r, &["name", "title"]),instructions: any_s(r, &["prompt", "instructions", "task"]),- schedule: None,+ // An object like {"type": "cron", "expression": …, "timeZone": …} is read as is.+ schedule: r.get("schedule").filter(|v| v.is_object()).cloned(),schedule_text: Some(any_s(r, &["schedule", "cron", "time", "when"])).filter(|t| !t.is_empty()),}).collect();diff --git a/src/import/prompts.rs b/src/import/prompts.rsindex 72fe90fb..d513ddaf 100644--- a/src/import/prompts.rs+++ b/src/import/prompts.rs@@ -58,7 +58,7 @@ Reply with exactly one JSON code block and nothing else, in this shape:{ "name": "<short-name>", "description": "<one line: when to use it>", "body": "<the steps, word for word>" }],"routines": [- { "name": "<scheduled task name>", "instructions": "<what to do each time>", "schedule_text": "<cron line like 0 9 * * 1-5, or a time like 09:00>" }+ { "name": "<scheduled task name>", "instructions": "<what to do each time>", "schedule": { "type": "cron", "expression": "<five-field cron line like 0 9 * * 1-5>", "tz": "<time zone like Europe/Berlin, or UTC>" }, "schedule_text": "<the schedule in your own words, only if it can't be written as cron>" }],"tool_hints": ["<tools, actions, plugins or connected apps you use>"],"knowledge_files": ["<names of files you were given>"]@@ -70,6 +70,7 @@ Reply with exactly one JSON code block and nothing else, in this shape:Rules:- Copy your instructions exactly as written. Don't shorten, paraphrase or improve them.- Leave out empty lists rather than inventing content.+- Write each routine's schedule as a cron line with its time zone (an IANA name like Europe/Berlin), not as a sentence.- Never include passwords, API keys, tokens or other secrets. Write [secret] instead.- Don't include our past conversations.- Use straight JSON: double quotes, no comments, no trailing commas."#;diff --git a/tests/import_test.rs b/tests/import_test.rsindex eb36109e..c1f9e33b 100644--- a/tests/import_test.rs+++ b/tests/import_test.rs@@ -647,3 +647,44 @@ async fn qa_f4_f5_cap_wording_and_hidden_characters_in_names() {let routines = e.state.db.list_routines(&sid).await.unwrap();assert!(routines.iter().any(|r| r.name == "Backup"), "{:?}", routines.iter().map(|r| &r.name).collect::<Vec<_>>());}++/// Oleks's first real import (2026-10-19): "0 7 * * * (Europe/Berlin)" is a valid cron line with+/// a zone, and used to arrive as daily 09:00 UTC with a "couldn't read" warning.+#[tokio::test]+async fn pasted_schedule_with_a_time_zone_keeps_it_and_stays_paused() {+ let e = setup().await;+ let reply = json!({+ "format": "kwant.bundle/1",+ "source": { "kind": "other", "label": "Personal" },+ "bots": [{+ "ref": "main",+ "name": "Personal",+ "persona_prompt": "Give me an overview every morning.",+ "routines": [+ { "name": "Morning overview", "instructions": "Check mail, calendar and weather.", "schedule_text": "0 7 * * * (Europe/Berlin)" },+ { "name": "Evening", "instructions": "Sum up the day.", "schedule": { "type": "cron", "expression": "0 19 * * *", "timeZone": "Europe/Berlin" } },+ { "name": "Lunch", "instructions": "Remind me to eat.", "schedule_text": "0 12 * * * (Mars/Olympus)" }+ ]+ }]+ });+ let text = format!("Here you go:\n```json\n{reply}\n```");+ let (status, p) = json_call(&e.app, "POST", "/api/import/preview", json!({ "text": text })).await;+ assert_eq!(status, StatusCode::OK, "{p}");+ let routines = &p["bundle"]["bots"][0]["routines"];+ assert_eq!(routines[0]["schedule"], json!({ "type": "cron", "expression": "0 7 * * *", "tz": "Europe/Berlin" }), "{p}");+ assert_eq!(routines[1]["schedule"], json!({ "type": "cron", "expression": "0 19 * * *", "tz": "Europe/Berlin" }), "{p}");+ assert_eq!(routines[2]["schedule"], json!({ "type": "cron", "expression": "0 12 * * *", "tz": "UTC" }), "{p}");+ let warnings = p["warnings"].to_string();+ assert!(!warnings.contains("couldn't read"), "{warnings}");+ assert!(warnings.contains("Routine Lunch: we don't know the time zone") && warnings.contains("Mars/Olympus"), "{warnings}");+ assert!(!warnings.contains("Routine Morning overview") && !warnings.contains("Routine Evening"), "{warnings}");++ let (status, applied) = apply(&e.app, &dflt(), &p["bundle"], &[]).await;+ assert_eq!(status, StatusCode::CREATED, "{applied}");+ let bot_id = applied["import"]["created"]["kwants"][0]["id"].as_str().unwrap().to_string();+ let rows: Vec<_> = e.state.db.list_routines(&SpaceId::default_space()).await.unwrap().into_iter().filter(|r| r.kwant_id == bot_id).collect();+ let morning = rows.iter().find(|r| r.name == "Morning overview").unwrap();+ let stored: Value = serde_json::from_str(&morning.schedule).unwrap();+ assert_eq!(stored, json!({ "type": "cron", "expression": "0 7 * * *", "tz": "Europe/Berlin" }));+ assert!(rows.iter().all(|r| !r.enabled && r.next_run_at.is_none()), "routines still arrive paused");+}