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
Automation Discovery step 2: the miner and a read-only scan
Lin (kwant bot)046cb11ee9b3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6ce7837c..5b089c1d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).run that passes its checks is verified on its own. Only the person sets the class and the checks; abot that tries gets a 403. Routines without them run as before. This is the first step ofAutomation Discovery (Mission Control Phase D4).+- **Automation Discovery can preview routines from your habits.** With Automation Discovery on in+ Settings (off by default), a scan of a space looks at the board tasks you made over the last 120 days+ and finds ones you ask for daily, on weekdays, weekly on up to three days or monthly, in your time+ zone across daylight-saving changes. It says which routine it would suggest (bot, schedule, first+ run, checks, follow-up steps) and why every other repeated task gets none. Only habits that were+ mostly verified, mostly done by one bot and not already covered by a routine, on or off, qualify.+ Tasks made by bots, routines, hand-offs or chats never count. The scan is a preview: it creates no+ offer and no routine yet. This is step 2 of Automation Discovery.### Changed- **kwant.si footer says "Made in Germany with ❤️".** The line sits under the trademark note on everydiff --git a/Cargo.lock b/Cargo.lockindex 91ec19d4..1729cf64 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1457,6 +1457,7 @@ dependencies = ["tracing","tracing-subscriber","tz-rs",+ "unicode-normalization","url","uuid","webpki-roots 1.0.9",diff --git a/Cargo.toml b/Cargo.tomlindex 919b121f..2d467430 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -45,6 +45,7 @@ thiserror = "1.0"dirs = "5.0"tokio-tungstenite = { version = "0.24", features = ["connect"] }sha2 = "0.10"+unicode-normalization = "0.1"qrcode = { version = "0.14", default-features = false }ring = "0.17"base64 = "0.22"diff --git a/docs/mission-control.md b/docs/mission-control.mdindex c32d0bbb..c4688291 100644--- a/docs/mission-control.md+++ b/docs/mission-control.md@@ -870,8 +870,8 @@ tool.## 20. Automation Discovery (Phase D4)Design note: `phase-d/automation-discovery.md`. v1 learns only from board tasks the person created.-**Build step 1 (shipped): what a routine's runs are held to.** The scanner, offers and Accept come-later.+**Build step 1 (shipped): what a routine's runs are held to. Step 2: the miner and a read-only scan.**+Offers, Accept and Dismiss come in step 3.**Routine outcomes.** Every routine can carry, besides its instructions:- `task_class`: the Flight School class of each run's task (§14). `null`, `""` or `general` clear it.@@ -892,6 +892,43 @@ everything else, the acceptance included, and their changes keep the person's clundone delete brings back the class, the acceptance and the checks. The exception is a check whose specheld a secret: its stored spec is never shown, so the undo can't restore it.+**The miner** (`src/automation_discovery.rs`, pure: no model, clock or database). It reads the+person's board tasks of the last 120 days: `created_by = person`, `source_kind` `person` or `mission`,+not forks, not cancelled. Tasks a bot, a routine, a hand-off or a chat made never count. Tasks group by+habit: `class:<class>` for a Flight School class other than `general`, else `title:` and 12 hex digits+of the SHA-256 of the normalised title (NFKC, lowercase, without numbers, dates, ordinals, English and+German weekday and month names, today/tomorrow/heute/morgen; too vague under 2 words and 8 letters).+Asks are counted once per local day in the person's time zone, so daylight saving never moves a habit.+Cadences, most specific first, each needing coverage ≥ 0.75 (slots asked, first to last) and precision+≥ 0.80 (asks on a slot):+- monthly, day N (a short month's last day stands in) or the last day: 120 days, ±3 days, ≥ 3 asks;+- weekly on 1–3 weekdays, each asked twice and each covering ≥ 0.75 of its own slots: 60 days, ±1 day,+ ≥ 3 asks over ≥ 14 days;+- weekdays (Monday–Friday, weekend asks count against it): 60 days, ≥ 5 asks on ≥ 4 weekdays;+- daily: 60 days, ≥ 5 asks including a weekend day.++A habit is `stopped` once two of its slots (plus their shift) passed without an ask. Then the gates: at+least 2 verified and verified ≥ 2/3 of verified plus failed (`not_verified`); one bot did ≥ 2/3 of the+verified ones (`mixed_owners`) and is still in the space (`bot_unavailable`); no routine of the space,+on or off, does it (`covered`: accepted from this habit, or the same bot with the same class or ≥ 0.6+word overlap with its name or first instruction line); not dismissed or deleted within 30 days+(`cooling_off`); no live offer (`already_offered`); not a step of another offer (`in_chain`); within+`automation_discovery_daily_cap` per 24 hours and 5 live offers per space (`capped`, strongest habits+first). Too few asks is `too_short`, no rhythm `not_regular`. A follow-up task created after ≥ 3 and ≥+80% of the last 20 asks, within 24 hours of each being done, is a chain step: written into the+instructions when the same bot did it, only mentioned when another bot did. The time is the median ask+time, down to the quarter hour ("time varies" past 4 hours apart); the first run is the next one after+now and at least half a period after the last ask. Checks that passed with the same spec on ≥ 2 verified+asks are kept (≤ 10); `person`/`peer_review` checks, ones whose spec changes every time and ones that+passed once are listed as left out.++**Scan** (`POST /api/automation-discovery/scan?space=`, person only): `{blocked_by, preview: true,+timezone, offered, skipped: [{cluster_key, title, asks, reason}]}`. `blocked_by` is `off`, `board_off` or+`no_timezone` (none set, or one this machine can't read), with empty lists; with the setting off nothing+is read. It writes nothing: no offer, no event, no routine. A thread or channel that no longer fits+falls back to the bot's direct chat, and check specs are shown with secrets masked. Settings:+`automation_discovery` `off` (default) or `on`, `automation_discovery_daily_cap` 0–10 (default 2).+**Storage** (add-only, idempotent): `routines` gains nullable `origin` (`discovery` for an acceptedoffer), `suggestion_id`, `task_class`, `acceptance` and `checks` (JSON). `routine_suggestions` holdsoffers, with one live offer per `(space_id, cluster_key)`. `routine_suggestion_events` is theirdiff --git a/src/automation_discovery.rs b/src/automation_discovery.rsnew file mode 100644index 00000000..2876ee83--- /dev/null+++ b/src/automation_discovery.rs@@ -0,0 +1,1578 @@+//! Automation Discovery's miner (Mission Control Phase D4): which repeated board tasks look like a+//! habit worth a routine, and what that routine would be. Deterministic and pure: no model, no+//! clock, no database, so tests pin every instant, daylight-saving changes included. The server+//! loads the rows (`db::automation`) and decides what to do with the result. Design:+//! mission-control/phase-d/automation-discovery.md §4–§5; contract: docs/mission-control.md §20.++use crate::routines::{now_in, run_time_text, MonthDay, Schedule};+use chrono::{DateTime, Datelike, Duration, NaiveDate, Timelike, Utc, Weekday};+use serde::Serialize;+use serde_json::Value;+use sha2::{Digest, Sha256};+use std::collections::{BTreeMap, HashMap, HashSet};+use unicode_normalization::UnicodeNormalization;++/// How far back daily, weekday and weekly habits are read.+pub const WINDOW_DAYS: i64 = 60;+/// Monthly habits need longer: three months span at least 59 days.+pub const MONTHLY_WINDOW_DAYS: i64 = 120;+/// Share of a pattern's slots, first to last, that had an ask: one skipped week in four passes.+pub const MIN_COVERAGE: f64 = 0.75;+/// Share of the asks that fit the pattern: one off-pattern ask in five passes.+pub const MIN_PRECISION: f64 = 0.80;+/// A habit is stopped once this many of its slots in a row have passed without an ask.+pub const STOPPED_AFTER_MISSED: usize = 2;+pub const MIN_VERIFIED: usize = 2;+/// Asks whose times lie further apart than this are offered with "time varies".+pub const TIME_VARIES_MINUTES: u32 = 4 * 60;+/// Word overlap at which a routine's name already covers a habit.+pub const COVERED_JACCARD: f64 = 0.6;+pub const DISMISS_COOL_OFF_DAYS: i64 = 30;+pub const DEFAULT_DAILY_CAP: i64 = 2;+pub const DAILY_CAP_RANGE: (i64, i64) = (0, 10);+pub const MAX_LIVE_PER_SPACE: usize = 5;+/// A follow-up joins the routine when it came after at least this many asks, and this share.+pub const CHAIN_MIN: usize = 3;+pub const CHAIN_SHARE: f64 = 0.80;+pub const CHAIN_MAX_STEPS: usize = 2;+/// Most recent asks a chain is looked for after.+pub const CHAIN_MAX_SOURCES: usize = 20;+/// How long after an ask is done its follow-up may come.+pub const CHAIN_HOURS: i64 = 24;+/// What a routine may carry (as `POST /api/routines` allows).+pub const MAX_CHECKS: usize = 10;+const NAME_CHARS: usize = 80;+const SOURCES_IN_INSTRUCTIONS: usize = 5;++/// Words that say when, not what: dropped from titles before they are compared.+const WHEN_WORDS: &[&str] = &[+ "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday",+ "mon", "tue", "tues", "wed", "thu", "thur", "thurs", "fri", "sat", "sun",+ "montag", "dienstag", "mittwoch", "donnerstag", "freitag", "samstag", "sonnabend", "sonntag",+ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december",+ "jan", "feb", "mar", "apr", "jun", "jul", "aug", "sep", "sept", "oct", "nov", "dec",+ "januar", "jänner", "februar", "märz", "mai", "juni", "juli", "oktober", "dezember", "okt", "dez",+ "today", "tomorrow", "heute", "morgen",+];++// Titles++/// A title reduced to what was asked: NFKC, lowercase, without dates, numbers, ordinals,+/// weekday and month names (English and German), "today"/"tomorrow", or punctuation.+pub fn norm(title: &str) -> String {+ let text: String = title.nfkc().collect::<String>().to_lowercase();+ text.split(|c: char| !c.is_alphanumeric())+ .filter(|w| !w.is_empty() && !w.chars().any(|c| c.is_numeric()) && !WHEN_WORDS.contains(w))+ .collect::<Vec<_>>()+ .join(" ")+}++/// Too little to be a habit ("fix", "todo"): fewer than 2 words and fewer than 8 letters.+fn too_vague(normed: &str) -> bool {+ normed.split(' ').filter(|w| !w.is_empty()).count() < 2 && normed.chars().filter(|c| c.is_alphabetic()).count() < 8+}++/// The habit a task belongs to: `class:<class>` for a Flight School class other than+/// `general`, else `title:<hash of norm(title)>`. `None` when the title is too vague.+pub fn cluster_key(task_class: &str, title: &str) -> Option<String> {+ let class = task_class.trim();+ if !class.is_empty() && class != "general" {+ return Some(format!("class:{class}"));+ }+ let normed = norm(title);+ if normed.is_empty() || too_vague(&normed) {+ return None;+ }+ let hash = Sha256::digest(normed.as_bytes());+ Some(format!("title:{}", hash.iter().take(6).map(|b| format!("{b:02x}")).collect::<String>()))+}++fn words(s: &str) -> HashSet<String> {+ norm(s).split(' ').filter(|w| !w.is_empty()).map(str::to_string).collect()+}++/// Word overlap of two titles after [`norm`]: shared words over all words.+pub fn jaccard(a: &str, b: &str) -> f64 {+ let (a, b) = (words(a), words(b));+ let union = a.union(&b).count();+ if union == 0 {+ return 0.0;+ }+ a.intersection(&b).count() as f64 / union as f64+}++/// A title without its date and number words, for a routine's name.+pub fn routine_name(title: &str) -> String {+ let kept: Vec<&str> = title.split_whitespace().filter(|w| !w.chars().any(|c| c.is_numeric())).collect();+ let name = kept.join(" ");+ let name = name.trim_matches(|c: char| !c.is_alphanumeric() && c != ')' && c != '"' && c != '\'');+ let name = if name.is_empty() { title.trim() } else { name };+ if name.chars().count() <= NAME_CHARS {+ name.to_string()+ } else {+ name.chars().take(NAME_CHARS).collect::<String>().trim_end().to_string()+ }+}++// What the miner reads++/// A board task the person created (see `Database::discovery_tasks` for the filter).+#[derive(Debug, Clone, PartialEq)]+pub struct TaskRow {+ pub id: String,+ pub title: String,+ pub acceptance: String,+ pub state: String,+ pub owner_kwant_id: Option<String>,+ pub task_class: String,+ pub channel_id: Option<String>,+ pub direct_kwant_id: Option<String>,+ pub thread_id: Option<String>,+ pub created_at: DateTime<Utc>,+ pub finished_at: Option<DateTime<Utc>>,+}++/// A check that passed on one of those tasks. `spec` is its JSON text.+#[derive(Debug, Clone, PartialEq)]+pub struct PassedCheck {+ pub task_id: String,+ pub kind: String,+ pub spec: String,+ pub required: bool,+}++/// An existing routine of the space, on or off.+#[derive(Debug, Clone, PartialEq)]+pub struct RoutineRef {+ pub kwant_id: String,+ pub name: String,+ pub instructions: String,+ pub task_class: Option<String>,+ /// The habit it was accepted from, for a discovered routine.+ pub cluster_key: Option<String>,+}++/// What earlier offers in the space say about this scan.+#[derive(Debug, Clone, Default, PartialEq)]+pub struct Past {+ /// Habits dismissed, or whose discovered routine was deleted, still cooling off.+ pub cooling: HashSet<String>,+ /// Habits with a live offer.+ pub live: HashSet<String>,+ /// Follow-ups written into a live offer's instructions.+ pub live_chain: HashSet<String>,+ /// Offers made in the last 24 hours.+ pub offered_24h: usize,+ /// Live offers now.+ pub live_count: usize,+}++pub struct Input<'a> {+ /// The person's time zone (IANA).+ pub tz: &'a str,+ pub now: DateTime<Utc>,+ pub tasks: &'a [TaskRow],+ pub checks: &'a [PassedCheck],+ pub routines: &'a [RoutineRef],+ /// Bots that may run a routine here: in the space.+ pub bots: &'a HashSet<String>,+ pub past: &'a Past,+ pub daily_cap: usize,+ pub max_live: usize,+}++// What it finds++#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]+#[serde(rename_all = "snake_case")]+pub enum Pattern {+ Monthly,+ Weekly,+ Weekdays,+ Daily,+}++/// Why a habit gets no offer.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]+#[serde(rename_all = "snake_case")]+pub enum Reason {+ /// Too few asks, or over too short a time, to tell.+ TooShort,+ /// No daily, weekday, weekly or monthly rhythm.+ NotRegular,+ /// It was regular, then two of its slots in a row passed without an ask.+ Stopped,+ /// Fewer than 2, or under 2/3, of its tasks were verified.+ NotVerified,+ /// No one bot did at least 2/3 of the verified ones.+ MixedOwners,+ /// The bot that did them isn't in the space any more.+ BotUnavailable,+ /// A routine, on or off, already does it.+ Covered,+ /// Dismissed, or its discovered routine deleted, within 30 days.+ CoolingOff,+ /// It already has a live offer.+ AlreadyOffered,+ /// It is a follow-up step of another offer.+ InChain,+ /// Over the daily cap, or the space has 5 live offers.+ Capped,+}++#[derive(Debug, Clone, PartialEq, Serialize)]+pub struct Skip {+ pub cluster_key: String,+ pub title: String,+ pub asks: usize,+ pub reason: Reason,+}++/// Where the routine would post.+#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]+pub struct Delivery {+ #[serde(skip_serializing_if = "Option::is_none")]+ pub channel_id: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub thread_id: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub direct_kwant_id: Option<String>,+}++#[derive(Debug, Clone, PartialEq, Serialize)]+pub struct CheckSpec {+ pub kind: String,+ pub spec: Value,+ pub required: bool,+}++#[derive(Debug, Clone, PartialEq, Serialize)]+pub struct LeftOut {+ pub kind: String,+ pub spec: Value,+ pub reason: &'static str,+}++#[derive(Debug, Clone, PartialEq, Serialize)]+pub struct ChainStep {+ pub key: String,+ pub title: String,+ pub kwant_id: Option<String>,+ /// Same bot: a step of the routine's instructions. Another bot: shown only.+ pub in_instructions: bool,+ /// After how many of the asks it came.+ pub after: usize,+}++#[derive(Debug, Clone, PartialEq, Serialize)]+pub struct Source {+ pub task_id: String,+ pub title: String,+ pub local_date: NaiveDate,+ pub local_time: String,+ pub state: String,+}++#[derive(Debug, Clone, PartialEq, Serialize)]+pub struct Evidence {+ pub pattern: Pattern,+ pub matched: usize,+ pub expected: usize,+ pub coverage: f64,+ pub precision: f64,+ pub verified: usize,+ /// Verified plus failed: the tasks that have an outcome.+ pub settled: usize,+ /// Asks in the window, one per local day.+ pub total: usize,+ pub time_spread: String,+ pub time_varies: bool,+}++/// A routine the miner would suggest. Nothing here is stored or created by the miner.+#[derive(Debug, Clone, PartialEq, Serialize)]+pub struct Offer {+ pub cluster_key: String,+ pub kwant_id: String,+ pub name: String,+ pub instructions: String,+ pub schedule: Schedule,+ pub first_run_at: DateTime<Utc>,+ pub first_run_text: String,+ pub delivery: Delivery,+ pub task_class: String,+ pub acceptance: String,+ pub checks: Vec<CheckSpec>,+ pub left_out_checks: Vec<LeftOut>,+ pub chain: Vec<ChainStep>,+ pub source_task_ids: Vec<String>,+ pub sources: Vec<Source>,+ pub evidence: Evidence,+}++#[derive(Debug, Clone, Default, PartialEq, Serialize)]+pub struct Scan {+ pub offered: Vec<Offer>,+ pub skipped: Vec<Skip>,+}++// Cadence++/// One local day with an ask: the earliest task of that day.+#[derive(Debug, Clone, Copy, PartialEq)]+pub struct Day {+ pub date: NaiveDate,+ /// Local minutes since midnight.+ pub minutes: u32,+ /// Index into the cluster's tasks.+ pub task: usize,+}++/// A rhythm that fits: which days matched which slot, and how well.+#[derive(Debug, Clone, PartialEq)]+pub struct Cadence {+ pub pattern: Pattern,+ /// Weekly: the weekdays, Monday first.+ pub weekdays: Vec<Weekday>,+ pub month_day: Option<MonthDay>,+ /// Indices into the days given to [`detect`].+ pub matched: Vec<usize>,+ pub expected: usize,+ pub coverage: f64,+ pub precision: f64,+}++/// How one pattern fared against the days.+#[derive(Debug, Clone)]+struct Fit {+ matched: Vec<usize>,+ slots: Vec<NaiveDate>,+ expected: usize,+ coverage: f64,+ precision: f64,+ exact: usize,+ missed_since: usize,+}++/// Matches `days` (sorted, one per date) to the dates `is_slot` accepts. An ask on a slot matches+/// it; an ask off the slots matches the nearest slot within `shift` days that no ask is on. Each+/// slot takes one ask. `today` counts the slots passed since the last match.+fn fit(days: &[Day], is_slot: &dyn Fn(NaiveDate) -> bool, shift: i64, today: NaiveDate) -> Fit {+ let mut taken: BTreeMap<NaiveDate, usize> = BTreeMap::new();+ let mut exact = 0;+ for (i, d) in days.iter().enumerate() {+ if is_slot(d.date) {+ taken.insert(d.date, i);+ exact += 1;+ }+ }+ for (i, d) in days.iter().enumerate() {+ if is_slot(d.date) {+ continue;+ }+ let near = (1..=shift)+ .flat_map(|n| [d.date - Duration::days(n), d.date + Duration::days(n)])+ .find(|s| is_slot(*s) && !taken.contains_key(s));+ if let Some(s) = near {+ taken.insert(s, i);+ }+ }+ let (Some(first), Some(last)) = (taken.keys().next().copied(), taken.keys().next_back().copied()) else {+ return Fit { matched: vec![], slots: vec![], expected: 0, coverage: 0.0, precision: 0.0, exact: 0, missed_since: 0 };+ };+ let expected = first.iter_days().take_while(|d| *d <= last).filter(|d| is_slot(*d)).count();+ // Slots after the last match whose time to be done in (with the shift) has passed+ let missed_since = last+ .iter_days()+ .skip(1)+ .take_while(|d| *d + Duration::days(shift) < today)+ .filter(|d| is_slot(*d))+ .count();+ let mut matched: Vec<usize> = taken.values().copied().collect();+ matched.sort_unstable();+ Fit {+ coverage: taken.len() as f64 / expected.max(1) as f64,+ precision: taken.len() as f64 / days.len().max(1) as f64,+ slots: taken.keys().copied().collect(),+ matched,+ expected,+ exact,+ missed_since,+ }+}++impl Fit {+ fn regular(&self) -> bool {+ self.coverage >= MIN_COVERAGE && self.precision >= MIN_PRECISION+ }++ fn span_days(&self) -> i64 {+ match (self.slots.first(), self.slots.last()) {+ (Some(a), Some(b)) => (*b - *a).num_days(),+ _ => 0,+ }+ }+}++const WEEK: [Weekday; 7] = [Weekday::Mon, Weekday::Tue, Weekday::Wed, Weekday::Thu, Weekday::Fri, Weekday::Sat, Weekday::Sun];++fn in_window(days: &[Day], today: NaiveDate, window: i64) -> Vec<Day> {+ days.iter().copied().filter(|d| (today - d.date).num_days() < window).collect()+}++/// Indices into `within` mapped back to indices into `all` (both sorted, `within` a suffix).+fn reindex(all: &[Day], within: &[Day], matched: &[usize]) -> Vec<usize> {+ let offset = all.len() - within.len();+ matched.iter().map(|i| i + offset).collect()+}++/// The first rhythm that fits `days` (sorted by date, one per date, within+/// [`MONTHLY_WINDOW_DAYS`] of `today`), most specific first: monthly, weekly, weekdays, daily.+/// `Err` says why none does.+pub fn detect(days: &[Day], today: NaiveDate) -> Result<Cadence, Reason> {+ if days.len() < 3 {+ return Err(Reason::TooShort);+ }+ let mut why = Reason::NotRegular;+ let mut note = |f: &Fit, enough: bool| -> bool {+ if !f.regular() {+ return false;+ }+ if !enough {+ if why == Reason::NotRegular {+ why = Reason::TooShort;+ }+ return false;+ }+ if f.missed_since >= STOPPED_AFTER_MISSED {+ why = Reason::Stopped;+ return false;+ }+ true+ };++ // Monthly: day N (a short month's last day stands in for N past it), or the last day+ let monthly = in_window(days, today, MONTHLY_WINDOW_DAYS);+ let last_of = |d: NaiveDate| crate_last_day(d);+ let mut best: Option<(MonthDay, Fit)> = None;+ for md in (1..=31).map(MonthDay::Day).chain([MonthDay::Last]) {+ let is_slot = move |d: NaiveDate| match md {+ MonthDay::Last => d.day() == last_of(d),+ MonthDay::Day(n) => d.day() == n.min(last_of(d)),+ };+ let f = fit(&monthly, &is_slot, 3, today);+ let better = match &best {+ None => true,+ Some((_, b)) => (f.matched.len(), f.exact) > (b.matched.len(), b.exact),+ };+ if better {+ best = Some((md, f));+ }+ }+ if let Some((md, f)) = best {+ if note(&f, f.matched.len() >= 3) {+ let all_last = f.matched.iter().all(|&i| monthly[i].date.day() == last_of(monthly[i].date));+ let short = f.matched.iter().any(|&i| last_of(monthly[i].date) < 31);+ let md = if all_last && short { MonthDay::Last } else { md };+ return Ok(Cadence {+ pattern: Pattern::Monthly,+ weekdays: vec![],+ month_day: Some(md),+ matched: reindex(days, &monthly, &f.matched),+ expected: f.expected,+ coverage: f.coverage,+ precision: f.precision,+ });+ }+ }++ let recent = in_window(days, today, WINDOW_DAYS);+ if recent.len() < 3 {+ // Older asks that fit no month aren't a habit cut short+ return Err(if why == Reason::NotRegular && monthly.len() < 3 { Reason::TooShort } else { why });+ }++ // Weekly: one weekday, or up to three each asked twice; each must hold its own slots+ let mut counts: Vec<(Weekday, usize)> = WEEK.iter().map(|w| (*w, recent.iter().filter(|d| d.date.weekday() == *w).count())).collect();+ counts.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.num_days_from_monday().cmp(&b.0.num_days_from_monday())));+ for k in 1..=3 {+ let set: Vec<Weekday> = counts.iter().take(k).map(|(w, _)| *w).collect();+ if k > 1 && counts[..k].iter().any(|(_, n)| *n < 2) {+ break;+ }+ let f = fit(&recent, &|d: NaiveDate| set.contains(&d.weekday()), 1, today);+ let each_holds = set.iter().all(|w| {+ let mine = f.slots.iter().filter(|s| s.weekday() == *w).count();+ let expected = match (f.slots.first(), f.slots.last()) {+ (Some(a), Some(b)) => a.iter_days().take_while(|d| d <= b).filter(|d| d.weekday() == *w).count(),+ _ => 0,+ };+ mine as f64 >= MIN_COVERAGE * expected as f64+ });+ if !each_holds {+ continue;+ }+ if note(&f, f.matched.len() >= 3 && f.span_days() >= 14) {+ let mut weekdays = set.clone();+ weekdays.sort_by_key(|w| w.num_days_from_monday());+ return Ok(Cadence {+ pattern: Pattern::Weekly,+ weekdays,+ month_day: None,+ matched: reindex(days, &recent, &f.matched),+ expected: f.expected,+ coverage: f.coverage,+ precision: f.precision,+ });+ }+ }++ // Weekdays: Monday to Friday, at least four of them seen+ let workday = |d: NaiveDate| d.weekday().num_days_from_monday() < 5;+ let f = fit(&recent, &workday, 0, today);+ let distinct: HashSet<Weekday> = f.slots.iter().map(|d| d.weekday()).collect();+ if note(&f, f.matched.len() >= 5 && distinct.len() >= 4) {+ return Ok(Cadence {+ pattern: Pattern::Weekdays,+ weekdays: WEEK[..5].to_vec(),+ month_day: None,+ matched: reindex(days, &recent, &f.matched),+ expected: f.expected,+ coverage: f.coverage,+ precision: f.precision,+ });+ }++ // Daily: any day, a weekend day among them+ let f = fit(&recent, &|_| true, 0, today);+ let weekend = f.slots.iter().any(|d| !workday(*d));+ if note(&f, f.matched.len() >= 5 && weekend) {+ return Ok(Cadence {+ pattern: Pattern::Daily,+ weekdays: vec![],+ month_day: None,+ matched: reindex(days, &recent, &f.matched),+ expected: f.expected,+ coverage: f.coverage,+ precision: f.precision,+ });+ }+ Err(why)+}++fn crate_last_day(date: NaiveDate) -> u32 {+ let (y, m) = (date.year(), date.month());+ let first_next = if m == 12 { NaiveDate::from_ymd_opt(y + 1, 1, 1) } else { NaiveDate::from_ymd_opt(y, m + 1, 1) };+ first_next.and_then(|d| d.pred_opt()).map_or(28, |d| d.day())+}++fn weekday_name(w: Weekday) -> &'static str {+ ["mon", "tue", "wed", "thu", "fri", "sat", "sun"][w.num_days_from_monday() as usize]+}++fn hhmm(minutes: u32) -> String {+ format!("{:02}:{:02}", minutes / 60, minutes % 60)+}++/// The usual time of the matched asks: their median, down to the quarter hour.+pub fn usual_time(minutes: &[u32]) -> u32 {+ let mut m = minutes.to_vec();+ m.sort_unstable();+ let median = match m.len() {+ 0 => 9 * 60,+ n if n % 2 == 1 => m[n / 2],+ n => (m[n / 2 - 1] + m[n / 2]) / 2,+ };+ median - median % 15+}++/// The routine schedule for a cadence at `minutes` local time in `tz`.+pub fn schedule_for(c: &Cadence, minutes: u32, tz: &str) -> Schedule {+ let time = hhmm(minutes);+ let tz = tz.to_string();+ match c.pattern {+ Pattern::Daily => Schedule::Daily { time, tz },+ Pattern::Weekly | Pattern::Weekdays => Schedule::Weekly { time, weekdays: c.weekdays.iter().map(|w| weekday_name(*w).to_string()).collect(), tz },+ Pattern::Monthly => Schedule::Monthly { time, days: vec![c.month_day.unwrap_or(MonthDay::Day(1))], tz },+ }+ .canonical()+}++/// How often a schedule runs, roughly: half of it is how long after an ask its first run waits.+fn period(s: &Schedule) -> Duration {+ match s {+ Schedule::Weekly { weekdays, .. } => Duration::minutes(7 * 24 * 60 / weekdays.len().max(1) as i64),+ Schedule::Monthly { days, .. } => Duration::days(30 / days.len().max(1) as i64),+ Schedule::Interval { every_minutes } => Duration::minutes(*every_minutes),+ _ => Duration::days(1),+ }+}++/// When a suggested routine would first run: the schedule's next run after now, but not within+/// half a period of the last ask, so this period's ask, already done, isn't run again. Accept+/// calls this again with the final schedule (design §5).+pub fn first_run_at(schedule: &Schedule, last_ask: DateTime<Utc>, now: DateTime<Utc>) -> Option<DateTime<Utc>> {+ schedule.next_after(now.max(last_ask + period(schedule) / 2), None)+}++// The miner++struct Cluster<'a> {+ key: String,+ tasks: Vec<&'a TaskRow>,+}++/// The local date and minute of `at` in `tz`; `None` for an unreadable zone.+fn local(tz: &str, at: DateTime<Utc>) -> Option<(NaiveDate, u32)> {+ let l = now_in(tz, at).ok()?;+ Some((l.date_naive(), l.hour() * 60 + l.minute()))+}++/// The cluster's asks, one per local day (the earliest), oldest first.+fn days_of(c: &Cluster, tz: &str) -> Vec<Day> {+ let mut by_date: BTreeMap<NaiveDate, Day> = BTreeMap::new();+ for (i, t) in c.tasks.iter().enumerate() {+ let Some((date, minutes)) = local(tz, t.created_at) else { continue };+ let day = Day { date, minutes, task: i };+ by_date+ .entry(date)+ .and_modify(|d| {+ if t.created_at < c.tasks[d.task].created_at {+ *d = day;+ }+ })+ .or_insert(day);+ }+ by_date.into_values().collect()+}++fn most_common<T: Eq + std::hash::Hash + Clone>(items: impl IntoIterator<Item = T>) -> Option<(T, usize)> {+ let mut counts: Vec<(T, usize)> = Vec::new();+ for item in items {+ match counts.iter_mut().find(|(t, _)| *t == item) {+ Some((_, n)) => *n += 1,+ None => counts.push((item, 1)),+ }+ }+ // First seen wins a tie: callers pass newest first+ counts.into_iter().fold(None, |best, (t, n)| match best {+ Some((_, bn)) if bn >= n => best,+ _ => Some((t, n)),+ })+}++/// Checks that passed with the same spec on at least two of the verified asks; the rest are+/// listed with why they were left out.+fn checks_of(verified: &[&TaskRow], passed: &[PassedCheck]) -> (Vec<CheckSpec>, Vec<LeftOut>) {+ let ids: Vec<&str> = verified.iter().map(|t| t.id.as_str()).collect();+ // (kind, spec) → tasks it passed on, in the order first seen (newest task first)+ let mut groups: Vec<((String, Value), HashSet<&str>, bool)> = Vec::new();+ for id in &ids {+ for c in passed.iter().filter(|c| c.task_id == *id) {+ let spec: Value = serde_json::from_str(&c.spec).unwrap_or(Value::String(c.spec.clone()));+ let key = (c.kind.clone(), spec);+ match groups.iter_mut().find(|(k, _, _)| *k == key) {+ Some((_, tasks, required)) => {+ tasks.insert(id);+ *required |= c.required;+ }+ None => groups.push((key, HashSet::from([*id]), c.required)),+ }+ }+ }+ let (mut keep, mut left) = (Vec::new(), Vec::<LeftOut>::new());+ for ((kind, spec), tasks, required) in &groups {+ let reason = if matches!(kind.as_str(), "person" | "peer_review") {+ Some("needs a person or a reviewer")+ } else if tasks.len() >= 2 {+ None+ } else if groups.iter().any(|((k, s), t, _)| k == kind && s != spec && !t.is_subset(tasks)) {+ Some("changes every time")+ } else {+ Some("passed only once")+ };+ match reason {+ None if keep.len() < MAX_CHECKS => keep.push(CheckSpec { kind: kind.clone(), spec: spec.clone(), required: *required }),+ None => left.push(LeftOut { kind: kind.clone(), spec: spec.clone(), reason: "more than 10 checks" }),+ // One line per kind and reason is enough to explain it+ Some(r) if !left.iter().any(|l| l.kind == *kind && l.reason == r) => left.push(LeftOut { kind: kind.clone(), spec: spec.clone(), reason: r }),+ Some(_) => {}+ }+ }+ (keep, left)+}++/// Follow-ups the person asks for after this habit's asks, in up to [`CHAIN_MAX_STEPS`].+fn chain_of(key: &str, asks: &[&TaskRow], all: &[(Option<String>, &TaskRow)], bot: &str) -> Vec<ChainStep> {+ let asks: Vec<&TaskRow> = asks.iter().rev().take(CHAIN_MAX_SOURCES).copied().collect();+ // key → (asks it followed, its tasks newest first, minutes after the ask)+ let mut follows: HashMap<&str, (usize, Vec<&TaskRow>, Vec<i64>)> = HashMap::new();+ for a in &asks {+ let until = a.finished_at.unwrap_or(a.created_at) + Duration::hours(CHAIN_HOURS);+ let mut seen: HashSet<&str> = HashSet::new();+ for (k, b) in all {+ let Some(k) = k.as_deref().filter(|k| *k != key) else { continue };+ if b.created_at <= a.created_at || b.created_at > until || b.id == a.id {+ continue;+ }+ let e = follows.entry(k).or_default();+ if seen.insert(k) {+ e.0 += 1;+ e.2.push((b.created_at - a.created_at).num_minutes());+ }+ e.1.push(b);+ }+ }+ let mut steps: Vec<(&str, usize, i64, &Vec<&TaskRow>)> = follows+ .iter()+ .filter(|(_, (n, _, _))| *n >= CHAIN_MIN && *n as f64 >= CHAIN_SHARE * asks.len() as f64)+ .map(|(k, (n, tasks, delays))| {+ let mut d = delays.clone();+ d.sort_unstable();+ (*k, *n, d[d.len() / 2], tasks)+ })+ .collect();+ steps.sort_by(|a, b| b.1.cmp(&a.1).then(a.2.cmp(&b.2)).then(a.0.cmp(b.0)));+ steps+ .into_iter()+ .take(CHAIN_MAX_STEPS)+ .map(|(k, n, _, tasks)| {+ let mut newest = tasks.clone();+ newest.sort_by_key(|t| std::cmp::Reverse(t.created_at));+ let owner = most_common(newest.iter().filter_map(|t| t.owner_kwant_id.clone())).map(|(o, _)| o);+ ChainStep {+ key: k.to_string(),+ title: newest.first().map(|t| t.title.trim().to_string()).unwrap_or_default(),+ in_instructions: owner.as_deref() == Some(bot),+ kwant_id: owner,+ after: n,+ }+ })+ .collect()+}++fn instructions(name: &str, acceptance: &str, chain: &[ChainStep], sources: &[Source]) -> String {+ let mut out = name.to_string();+ if !acceptance.trim().is_empty() {+ out.push_str(&format!("\n\nA good result: {}", acceptance.trim()));+ }+ let steps: Vec<&ChainStep> = chain.iter().filter(|s| s.in_instructions).collect();+ if !steps.is_empty() {+ out.push_str("\n\nThen, in the same run:");+ for (i, s) in steps.iter().enumerate() {+ out.push_str(&format!("\n{}. {}", i + 1, s.title));+ }+ }+ let past: Vec<String> = sources.iter().rev().take(SOURCES_IN_INSTRUCTIONS).map(|s| format!("\"{}\" ({})", s.title, s.local_date)).collect();+ out.push_str(&format!("\n\nBased on your tasks: {}", past.join(", ")));+ out+}++/// Whether an existing routine already does this habit for this bot.+fn covered(key: &str, class: &str, bot: &str, title: &str, routines: &[RoutineRef]) -> bool {+ routines.iter().any(|r| {+ r.cluster_key.as_deref() == Some(key)+ || (r.kwant_id == bot+ && ((class != "general" && r.task_class.as_deref() == Some(class))+ || jaccard(&r.name, title) >= COVERED_JACCARD+ || jaccard(r.instructions.lines().next().unwrap_or(""), title) >= COVERED_JACCARD))+ })+}++/// One scan of a space: the offers it would make and why every other habit gets none.+/// Reads only its input; the caller decides whether to store anything.+pub fn mine(input: &Input) -> Scan {+ let Some((today, _)) = local(input.tz, input.now) else { return Scan::default() };+ let keyed: Vec<(Option<String>, &TaskRow)> = input.tasks.iter().map(|t| (cluster_key(&t.task_class, &t.title), t)).collect();+ let mut clusters: Vec<Cluster> = Vec::new();+ for (k, t) in &keyed {+ let Some(k) = k else { continue };+ match clusters.iter_mut().find(|c| c.key == *k) {+ Some(c) => c.tasks.push(t),+ None => clusters.push(Cluster { key: k.clone(), tasks: vec![t] }),+ }+ }+ let mut passing: Vec<Offer> = Vec::new();+ let mut skipped: Vec<Skip> = Vec::new();+ for c in &mut clusters {+ c.tasks.sort_by(|a, b| a.created_at.cmp(&b.created_at).then(a.id.cmp(&b.id)));+ let days = days_of(c, input.tz);+ let newest_title = c.tasks.last().map(|t| t.title.trim().to_string()).unwrap_or_default();+ let skip = |reason| Skip { cluster_key: c.key.clone(), title: newest_title.clone(), asks: days.len(), reason };+ let cadence = match detect(&days, today) {+ Ok(cd) => cd,+ Err(reason) => {+ skipped.push(skip(reason));+ continue;+ }+ };+ let asks: Vec<&TaskRow> = cadence.matched.iter().map(|&i| c.tasks[days[i].task]).collect();+ let verified: Vec<&TaskRow> = asks.iter().rev().filter(|t| t.state == "verified").copied().collect();+ let settled = verified.len() + asks.iter().filter(|t| t.state == "failed").count();+ if verified.len() < MIN_VERIFIED || verified.len() * 3 < settled * 2 {+ skipped.push(skip(Reason::NotVerified));+ continue;+ }+ let Some((bot, owned)) = most_common(verified.iter().filter_map(|t| t.owner_kwant_id.clone())) else {+ skipped.push(skip(Reason::MixedOwners));+ continue;+ };+ if owned * 3 < verified.len() * 2 {+ skipped.push(skip(Reason::MixedOwners));+ continue;+ }+ if !input.bots.contains(&bot) {+ skipped.push(skip(Reason::BotUnavailable));+ continue;+ }+ let latest = verified[0];+ let class = if c.key.starts_with("class:") { latest.task_class.trim().to_string() } else { "general".to_string() };+ if covered(&c.key, &class, &bot, &latest.title, input.routines) {+ skipped.push(skip(Reason::Covered));+ continue;+ }+ if input.past.cooling.contains(&c.key) {+ skipped.push(skip(Reason::CoolingOff));+ continue;+ }+ if input.past.live.contains(&c.key) {+ skipped.push(skip(Reason::AlreadyOffered));+ continue;+ }++ let minutes: Vec<u32> = cadence.matched.iter().map(|&i| days[i].minutes).collect();+ let (lo, hi) = (minutes.iter().copied().min().unwrap_or(0), minutes.iter().copied().max().unwrap_or(0));+ let schedule = schedule_for(&cadence, usual_time(&minutes), input.tz);+ let last_ask = asks.last().map_or(input.now, |t| t.created_at);+ let Some(first_run_at) = first_run_at(&schedule, last_ask, input.now) else {+ skipped.push(skip(Reason::NotRegular));+ continue;+ };+ let name = routine_name(&latest.title);+ let acceptance = latest.acceptance.trim().to_string();+ let (checks, left_out_checks) = checks_of(&verified, input.checks);+ let chain = chain_of(&c.key, &asks, &keyed, &bot);+ let sources: Vec<Source> = cadence+ .matched+ .iter()+ .map(|&i| {+ let t = c.tasks[days[i].task];+ Source { task_id: t.id.clone(), title: t.title.trim().to_string(), local_date: days[i].date, local_time: hhmm(days[i].minutes), state: t.state.clone() }+ })+ .collect();+ let newest_first: Vec<&TaskRow> = asks.iter().rev().copied().collect();+ let place = most_common(newest_first.iter().map(|t| (t.thread_id.clone(), t.channel_id.clone()))).map(|(p, _)| p).unwrap_or_default();+ let delivery = match place {+ (Some(thread), _) => Delivery { thread_id: Some(thread), ..Default::default() },+ (None, Some(channel)) => Delivery { channel_id: Some(channel), ..Default::default() },+ (None, None) => Delivery { direct_kwant_id: Some(bot.clone()), ..Default::default() },+ };+ passing.push(Offer {+ cluster_key: c.key.clone(),+ instructions: instructions(&name, &acceptance, &chain, &sources),+ first_run_text: run_time_text(first_run_at, schedule.tz()),+ kwant_id: bot,+ name,+ schedule,+ first_run_at,+ delivery,+ task_class: class,+ acceptance,+ checks,+ left_out_checks,+ chain,+ source_task_ids: sources.iter().map(|s| s.task_id.clone()).collect(),+ evidence: Evidence {+ pattern: cadence.pattern,+ matched: cadence.matched.len(),+ expected: cadence.expected,+ coverage: (cadence.coverage * 100.0).round() / 100.0,+ precision: (cadence.precision * 100.0).round() / 100.0,+ verified: verified.len(),+ settled,+ total: days.len(),+ time_spread: if lo == hi { hhmm(lo) } else { format!("{}–{}", hhmm(lo), hhmm(hi)) },+ time_varies: hi - lo > TIME_VARIES_MINUTES,+ },+ sources,+ });+ }++ // A follow-up written into another offer's steps isn't offered on its own+ let steps: HashSet<String> = passing+ .iter()+ .flat_map(|o| o.chain.iter().filter(|s| s.in_instructions).map(|s| s.key.clone()))+ .chain(input.past.live_chain.iter().cloned())+ .collect();+ let (in_chain, mut passing): (Vec<Offer>, Vec<Offer>) = passing.into_iter().partition(|o| steps.contains(&o.cluster_key));+ skipped.extend(in_chain.into_iter().map(|o| Skip { title: o.name.clone(), asks: o.evidence.total, cluster_key: o.cluster_key, reason: Reason::InChain }));++ // The strongest habits first, then the cap+ passing.sort_by(|a, b| {+ b.evidence.matched.cmp(&a.evidence.matched).then(b.sources.last().map(|s| s.local_date).cmp(&a.sources.last().map(|s| s.local_date))).then(a.cluster_key.cmp(&b.cluster_key))+ });+ let room = input.daily_cap.saturating_sub(input.past.offered_24h).min(input.max_live.saturating_sub(input.past.live_count));+ let capped = passing.split_off(room.min(passing.len()));+ skipped.extend(capped.into_iter().map(|o| Skip { title: o.name.clone(), asks: o.evidence.total, cluster_key: o.cluster_key, reason: Reason::Capped }));+ skipped.sort_by(|a, b| a.cluster_key.cmp(&b.cluster_key));+ Scan { offered: passing, skipped }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::routines::local_instant;+ use chrono::NaiveTime;++ const BERLIN: &str = "Europe/Berlin";++ fn date(s: &str) -> NaiveDate {+ NaiveDate::parse_from_str(s, "%Y-%m-%d").unwrap()+ }++ fn at(tz: &str, d: NaiveDate, time: &str) -> DateTime<Utc> {+ local_instant(tz, d, NaiveTime::parse_from_str(time, "%H:%M").unwrap()).unwrap().unwrap()+ }++ /// `n` dates from `start`, `step` days apart.+ fn every(start: &str, step: i64, n: usize) -> Vec<NaiveDate> {+ (0..n).map(|i| date(start) + Duration::days(step * i as i64)).collect()+ }++ fn task(id: &str, title: &str, created_at: DateTime<Utc>) -> TaskRow {+ TaskRow {+ id: id.into(),+ title: title.into(),+ acceptance: "The report is in the shared folder".into(),+ state: "verified".into(),+ owner_kwant_id: Some("bot-a".into()),+ task_class: "general".into(),+ channel_id: None,+ direct_kwant_id: None,+ thread_id: None,+ created_at,+ finished_at: Some(created_at + Duration::minutes(30)),+ }+ }++ /// One verified ask by bot-a per date, at `time` local, titled with its date.+ fn asks(tz: &str, prefix: &str, title: &str, dates: &[NaiveDate], time: &str) -> Vec<TaskRow> {+ dates.iter().enumerate().map(|(i, d)| task(&format!("{prefix}-{i}"), &format!("{title} {d}"), at(tz, *d, time))).collect()+ }++ struct Fx {+ tz: &'static str,+ now: DateTime<Utc>,+ tasks: Vec<TaskRow>,+ checks: Vec<PassedCheck>,+ routines: Vec<RoutineRef>,+ bots: HashSet<String>,+ past: Past,+ cap: usize,+ }++ impl Fx {+ fn new(tz: &'static str, now: DateTime<Utc>, tasks: Vec<TaskRow>) -> Self {+ Fx {+ tz,+ now,+ tasks,+ checks: vec![],+ routines: vec![],+ bots: HashSet::from(["bot-a".to_string(), "bot-b".to_string()]),+ past: Past::default(),+ cap: DEFAULT_DAILY_CAP as usize,+ }+ }++ fn scan(&self) -> Scan {+ mine(&Input {+ tz: self.tz,+ now: self.now,+ tasks: &self.tasks,+ checks: &self.checks,+ routines: &self.routines,+ bots: &self.bots,+ past: &self.past,+ daily_cap: self.cap,+ max_live: MAX_LIVE_PER_SPACE,+ })+ }++ fn only_offer(&self) -> Offer {+ let scan = self.scan();+ assert_eq!(scan.offered.len(), 1, "skipped: {:?}", scan.skipped);+ scan.offered.into_iter().next().unwrap()+ }++ fn only_skip(&self) -> Reason {+ let scan = self.scan();+ assert!(scan.offered.is_empty(), "offered: {:?}", scan.offered.iter().map(|o| &o.name).collect::<Vec<_>>());+ assert_eq!(scan.skipped.len(), 1, "{:?}", scan.skipped);+ scan.skipped[0].reason+ }+ }++ fn days(dates: &[NaiveDate]) -> Vec<Day> {+ dates.iter().enumerate().map(|(i, d)| Day { date: *d, minutes: 9 * 60, task: i }).collect()+ }++ fn local_hm(tz: &str, at: DateTime<Utc>) -> String {+ now_in(tz, at).unwrap().format("%a %H:%M").to_string()+ }++ // Titles++ #[test]+ fn norm_drops_dates_numbers_weekdays_and_months_in_english_and_german() {+ assert_eq!(norm("Weekly report — Mon 3 March 2026"), "weekly report");+ assert_eq!(norm("weekly REPORT (2026-10-05)"), "weekly report");+ assert_eq!(norm("Send invoices for 31st Oct, today"), "send invoices for");+ assert_eq!(norm("Wochenbericht für Montag, 3. März"), "wochenbericht für");+ assert_eq!(norm("Rechnungen schicken: Dezember / heute"), "rechnungen schicken");+ // NFKC: full-width letters and ligatures read like plain ones+ assert_eq!(norm("Weekly report"), "weekly report");+ assert_eq!(norm("file backups"), "file backups");+ }++ #[test]+ fn cluster_key_groups_titles_by_what_was_asked_or_by_class() {+ let a = cluster_key("general", "Weekly report 2026-10-05").unwrap();+ assert_eq!(cluster_key("", "weekly report (Oct 12)"), Some(a.clone()));+ assert!(a.starts_with("title:") && a.len() == "title:".len() + 12, "{a}");+ assert_ne!(cluster_key("general", "Monthly report"), Some(a));+ assert_eq!(cluster_key("invoice", "anything at all"), Some("class:invoice".into()));+ // Too vague to be a habit+ assert_eq!(cluster_key("general", "Fix"), None);+ assert_eq!(cluster_key("general", "todo 3 Oct"), None);+ assert_eq!(cluster_key("general", "Mon 12"), None);+ // One long word is enough+ assert!(cluster_key("general", "Bookkeeping").is_some());+ }++ #[test]+ fn routine_name_drops_date_words_and_is_capped() {+ assert_eq!(routine_name("Weekly sales report 2026-10-05"), "Weekly sales report");+ assert_eq!(routine_name(" Invoices (Oct 3rd) "), "Invoices (Oct");+ assert_eq!(routine_name("2026-10-05"), "2026-10-05");+ assert_eq!(routine_name(&"long ".repeat(40)).chars().count(), 79);+ }++ // Daylight saving: the same local time on both sides of the switch++ #[test]+ fn weekly_across_berlin_spring_forward_keeps_the_local_time() {+ // Mondays 16 Feb to 13 Apr 2026; clocks go forward on Sunday 29 March+ let dates = every("2026-02-16", 7, 9);+ assert!(dates.iter().all(|d| d.weekday() == Weekday::Mon));+ let tasks = asks(BERLIN, "r", "Weekly report", &dates, "09:00");+ // The asks are an hour apart in UTC+ assert_eq!(tasks[0].created_at.hour(), 8);+ assert_eq!(tasks[8].created_at.hour(), 7);+ let fx = Fx::new(BERLIN, at(BERLIN, date("2026-04-14"), "10:00"), tasks);+ let o = fx.only_offer();+ assert_eq!(o.schedule, Schedule::Weekly { time: "09:00".into(), weekdays: vec!["mon".into()], tz: BERLIN.into() });+ assert_eq!(o.evidence.pattern, Pattern::Weekly);+ assert_eq!((o.evidence.matched, o.evidence.expected), (9, 9));+ assert_eq!(o.evidence.time_spread, "09:00");+ assert!(!o.evidence.time_varies);+ assert_eq!(o.first_run_at, at(BERLIN, date("2026-04-20"), "09:00"));+ assert_eq!(o.first_run_text, "Mon 20 Apr 09:00 Europe/Berlin");+ assert_eq!(o.name, "Weekly report");+ assert_eq!(o.kwant_id, "bot-a");+ }++ #[test]+ fn twice_weekly_across_new_york_spring_forward_keeps_the_local_time() {+ // Tuesdays and Thursdays 3 Feb to 26 Mar 2026; clocks go forward on Sunday 8 March+ let dates: Vec<NaiveDate> = every("2026-02-03", 7, 8).into_iter().flat_map(|d| [d, d + Duration::days(2)]).collect();+ let tz = "America/New_York";+ let tasks = asks(tz, "r", "Client status update", &dates, "17:30");+ assert_ne!(tasks[0].created_at.hour(), tasks[15].created_at.hour());+ let fx = Fx::new(tz, at(tz, date("2026-03-27"), "09:00"), tasks);+ let o = fx.only_offer();+ assert_eq!(o.schedule, Schedule::Weekly { time: "17:30".into(), weekdays: vec!["tue".into(), "thu".into()], tz: tz.into() });+ assert_eq!(o.evidence.matched, 16);+ assert_eq!(local_hm(tz, o.first_run_at), "Tue 17:30");+ }++ #[test]+ fn weekly_across_auckland_fall_back_keeps_the_local_day_and_time() {+ // Sundays 1 Mar to 26 Apr 2026 at 08:00, which is Saturday evening in UTC; clocks go+ // back on Sunday 5 April+ let tz = "Pacific/Auckland";+ let dates = every("2026-03-01", 7, 9);+ let tasks = asks(tz, "r", "Plan the week", &dates, "08:00");+ assert_eq!(tasks[0].created_at.weekday(), Weekday::Sat);+ assert_eq!(tasks[0].created_at.hour(), 19);+ assert_eq!(tasks[8].created_at.hour(), 20);+ let fx = Fx::new(tz, at(tz, date("2026-04-27"), "12:00"), tasks);+ let o = fx.only_offer();+ assert_eq!(o.schedule, Schedule::Weekly { time: "08:00".into(), weekdays: vec!["sun".into()], tz: tz.into() });+ assert_eq!(o.first_run_at, at(tz, date("2026-05-03"), "08:00"));+ assert_eq!(o.sources[0].local_date, date("2026-03-01"));+ assert_eq!(o.sources[0].local_time, "08:00");+ }++ // Every cadence kind++ #[test]+ fn daily_needs_every_day_weekends_included() {+ let dates = every("2026-09-12", 1, 21);+ let mut tasks = asks(BERLIN, "d", "Check the inbox", &dates, "07:05");+ tasks[10].created_at += Duration::minutes(20);+ let now = at(BERLIN, date("2026-10-02"), "12:00");+ let o = Fx::new(BERLIN, now, tasks).only_offer();+ assert_eq!(o.schedule, Schedule::Daily { time: "07:00".into(), tz: BERLIN.into() });+ assert_eq!(o.evidence.time_spread, "07:05–07:25");+ // Asked today already: the first run is tomorrow+ assert_eq!(o.first_run_at, at(BERLIN, date("2026-10-03"), "07:00"));+ }++ #[test]+ fn weekdays_is_monday_to_friday_and_a_weekend_skips_no_slot() {+ // Four working weeks, 7 Sep to 2 Oct 2026+ let dates: Vec<NaiveDate> = every("2026-09-07", 1, 26).into_iter().filter(|d| d.weekday().num_days_from_monday() < 5).collect();+ assert_eq!(dates.len(), 20);+ let tasks = asks(BERLIN, "w", "Standup notes", &dates, "08:45");+ // Monday after the last Friday: no slot missed yet+ let o = Fx::new(BERLIN, at(BERLIN, date("2026-10-05"), "07:00"), tasks).only_offer();+ assert_eq!(o.evidence.pattern, Pattern::Weekdays);+ assert_eq!(o.schedule, Schedule::Weekly { time: "08:45".into(), weekdays: ["mon", "tue", "wed", "thu", "fri"].map(String::from).to_vec(), tz: BERLIN.into() });+ assert_eq!(o.first_run_at, at(BERLIN, date("2026-10-05"), "08:45"));+ }++ #[test]+ fn monthly_on_a_day_of_the_month() {+ let dates = ["2026-07-03", "2026-08-03", "2026-09-03", "2026-10-03"].map(date);+ let tasks = asks(BERLIN, "m", "Send invoices", &dates, "10:00");+ let o = Fx::new(BERLIN, at(BERLIN, date("2026-10-10"), "10:00"), tasks).only_offer();+ assert_eq!(o.schedule, Schedule::Monthly { time: "10:00".into(), days: vec![MonthDay::Day(3)], tz: BERLIN.into() });+ assert_eq!(o.first_run_at, at(BERLIN, date("2026-11-03"), "10:00"));+ }++ #[test]+ fn monthly_on_the_last_day_when_short_months_end_earlier() {+ let dates = ["2026-06-30", "2026-07-31", "2026-08-31", "2026-09-30"].map(date);+ let tasks = asks(BERLIN, "m", "Close the books", &dates, "16:00");+ let o = Fx::new(BERLIN, at(BERLIN, date("2026-10-02"), "10:00"), tasks).only_offer();+ assert_eq!(o.schedule, Schedule::Monthly { time: "16:00".into(), days: vec![MonthDay::Last], tz: BERLIN.into() });+ assert_eq!(o.first_run_at, at(BERLIN, date("2026-10-31"), "16:00"));+ }++ #[test]+ fn monthly_reads_four_months_back() {+ // 120-day window: 3 Jun is 129 days before 10 Oct and out; the rest still fit+ let dates = ["2026-06-03", "2026-07-03", "2026-08-03", "2026-09-03", "2026-10-03"].map(date);+ let d = detect(&days(&dates), date("2026-10-10")).unwrap();+ assert_eq!((d.pattern, d.month_day, d.matched.clone()), (Pattern::Monthly, Some(MonthDay::Day(3)), vec![1, 2, 3, 4]));+ }++ // Near-misses at each tolerance edge++ #[test]+ fn monthly_shift_of_three_days_passes_more_does_not() {+ let today = date("2026-09-14");+ // Three days late still counts for the 3rd+ let three = ["2026-07-03", "2026-08-03", "2026-09-06"].map(date);+ assert_eq!(detect(&days(&three), today).unwrap().month_day, Some(MonthDay::Day(3)));+ // Six days apart still has a day within three of each (the 6th)+ let six = ["2026-07-03", "2026-08-03", "2026-09-09"].map(date);+ assert_eq!(detect(&days(&six), today).unwrap().month_day, Some(MonthDay::Day(6)));+ // Seven apart has none+ let seven = ["2026-07-03", "2026-08-03", "2026-09-10"].map(date);+ assert_eq!(detect(&days(&seven), today), Err(Reason::NotRegular));+ }++ #[test]+ fn weekly_shift_of_one_day_passes_two_does_not() {+ let today = date("2026-10-06");+ let mut dates = every("2026-09-07", 7, 4);+ dates[2] += Duration::days(1);+ let d = detect(&days(&dates), today).unwrap();+ assert_eq!((d.pattern, d.weekdays.clone(), d.matched.len()), (Pattern::Weekly, vec![Weekday::Mon], 4));+ dates[2] += Duration::days(1);+ assert_eq!(detect(&days(&dates), today), Err(Reason::NotRegular));+ }++ #[test]+ fn weekly_coverage_three_in_four_passes_less_does_not() {+ let today = date("2026-10-27");+ // Mondays 7 Sep to 26 Oct: 8 slots+ let all = every("2026-09-07", 7, 8);+ let six: Vec<NaiveDate> = all.iter().enumerate().filter(|(i, _)| ![2, 5].contains(i)).map(|(_, d)| *d).collect();+ let d = detect(&days(&six), today).unwrap();+ assert_eq!((d.expected, d.coverage), (8, 0.75));+ let five: Vec<NaiveDate> = all.iter().enumerate().filter(|(i, _)| ![2, 4, 5].contains(i)).map(|(_, d)| *d).collect();+ assert_eq!(detect(&days(&five), today), Err(Reason::NotRegular));+ }++ #[test]+ fn weekly_precision_one_stray_in_five_passes_more_does_not() {+ let today = date("2026-10-27");+ let mondays = every("2026-09-07", 7, 8);+ // Wednesdays, too far from a Monday to shift onto one+ let strays = every("2026-09-09", 14, 3);+ let mut two: Vec<NaiveDate> = mondays.iter().copied().chain(strays[..2].iter().copied()).collect();+ two.sort();+ let d = detect(&days(&two), today).unwrap();+ assert_eq!((d.pattern, d.weekdays.clone(), d.precision), (Pattern::Weekly, vec![Weekday::Mon], 0.8));+ let mut three: Vec<NaiveDate> = mondays.iter().copied().chain(strays.iter().copied()).collect();+ three.sort();+ // Nor are the Wednesdays a second weekday: three of eight+ assert_eq!(detect(&days(&three), today), Err(Reason::NotRegular));+ }++ #[test]+ fn a_weekday_joins_a_weekly_set_only_when_it_keeps_its_own_slots() {+ let today = date("2026-10-27");+ let mondays = every("2026-09-07", 7, 8);+ let wednesdays = every("2026-09-09", 7, 8);+ let mut both: Vec<NaiveDate> = mondays.iter().chain(&wednesdays).copied().collect();+ both.sort();+ let d = detect(&days(&both), today).unwrap();+ assert_eq!(d.weekdays, vec![Weekday::Mon, Weekday::Wed]);+ // Wednesdays on 6 of 8 weeks still hold their own slots+ let mut most: Vec<NaiveDate> = mondays.iter().chain(&wednesdays[2..]).copied().collect();+ most.sort();+ assert_eq!(detect(&days(&most), today).unwrap().weekdays, vec![Weekday::Mon, Weekday::Wed]);+ }++ #[test]+ fn weekdays_and_daily_need_their_minimums() {+ let today = date("2026-09-12");+ // Four working days: too few to tell weekdays from anything else+ let four = every("2026-09-07", 1, 4);+ assert_eq!(detect(&days(&four), today), Err(Reason::TooShort));+ // Five days in a row, all working days: weekdays, not daily+ let five = every("2026-09-07", 1, 5);+ assert_eq!(detect(&days(&five), today).unwrap().pattern, Pattern::Weekdays);+ // Seven days in a row: daily+ let seven = every("2026-09-05", 1, 7);+ assert_eq!(detect(&days(&seven), date("2026-09-12")).unwrap().pattern, Pattern::Daily);+ // Weekend asks are strays for weekdays: two workweeks and three of their four weekend+ // days are 10 of 13 (under 80%), and read as daily (13 of 14 days) instead+ let mut most = every("2026-09-07", 1, 14);+ most.retain(|d| *d != date("2026-09-13"));+ assert_eq!(detect(&days(&most), date("2026-09-21")).unwrap().pattern, Pattern::Daily);+ // Workdays with a gap each week and weekend asks: neither+ let mut gappy: Vec<NaiveDate> = every("2026-09-07", 1, 21).into_iter().filter(|d| !matches!(d.weekday(), Weekday::Wed | Weekday::Thu)).collect();+ gappy.retain(|d| *d != date("2026-09-12") && *d != date("2026-09-19"));+ assert_eq!(detect(&days(&gappy), date("2026-09-28")), Err(Reason::NotRegular));+ assert_eq!(detect(&days(&gappy[..2]), today), Err(Reason::TooShort));+ }++ // A stopped habit++ #[test]+ fn a_habit_is_stopped_once_two_of_its_slots_pass_without_an_ask() {+ // Mondays 7 Sep to 12 Oct; one missed Monday is a skipped week+ let dates = every("2026-09-07", 7, 6);+ assert_eq!(detect(&days(&dates), date("2026-10-27")).unwrap().pattern, Pattern::Weekly);+ // The 26 Oct slot is still within its one-day shift on the 27th; on the 28th it has passed+ assert_eq!(detect(&days(&dates), date("2026-10-28")), Err(Reason::Stopped));+ let tasks = asks(BERLIN, "s", "Weekly report", &dates, "09:00");+ assert_eq!(Fx::new(BERLIN, at(BERLIN, date("2026-11-02"), "09:00"), tasks).only_skip(), Reason::Stopped);+ }++ #[test]+ fn a_stopped_daily_or_weekdays_habit() {+ let daily = every("2026-09-01", 1, 14);+ assert_eq!(detect(&days(&daily), date("2026-09-16")).unwrap().pattern, Pattern::Daily);+ assert_eq!(detect(&days(&daily), date("2026-09-17")), Err(Reason::Stopped));+ // Weekdays ending Friday 2 Oct: the weekend misses nothing, Monday and Tuesday do+ let weekdays: Vec<NaiveDate> = every("2026-09-07", 1, 26).into_iter().filter(|d| d.weekday().num_days_from_monday() < 5).collect();+ assert!(detect(&days(&weekdays), date("2026-10-06")).is_ok());+ assert_eq!(detect(&days(&weekdays), date("2026-10-07")), Err(Reason::Stopped));+ }++ #[test]+ fn too_few_asks_or_none_regular() {+ let today = date("2026-10-27");+ assert_eq!(detect(&days(&every("2026-10-05", 7, 2)), today), Err(Reason::TooShort));+ let scattered = ["2026-09-01", "2026-09-04", "2026-09-15", "2026-09-26", "2026-10-09"].map(date);+ assert_eq!(detect(&days(&scattered), today), Err(Reason::NotRegular));+ // Several asks on one day are one ask+ let t = at(BERLIN, date("2026-10-05"), "09:00");+ let tasks: Vec<TaskRow> = (0..5).map(|i| task(&format!("x{i}"), "Weekly report", t + Duration::minutes(i))).collect();+ let fx = Fx::new(BERLIN, t + Duration::days(1), tasks);+ assert_eq!(fx.only_skip(), Reason::TooShort);+ assert_eq!(fx.scan().skipped[0].asks, 1);+ }++ // Gates++ fn weekly(prefix: &str, title: &str) -> Vec<TaskRow> {+ asks(BERLIN, prefix, title, &every("2026-09-07", 7, 6), "09:00")+ }++ fn after_weekly() -> DateTime<Utc> {+ at(BERLIN, date("2026-10-13"), "10:00")+ }++ #[test]+ fn needs_two_verified_and_two_thirds_of_those_with_an_outcome() {+ let mut tasks = weekly("v", "Weekly report");+ for (i, t) in tasks.iter_mut().enumerate() {+ t.state = if i < 4 { "failed" } else { "verified" }.into();+ }+ // 2 verified, 4 failed+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks.clone()).only_skip(), Reason::NotVerified);+ // 4 verified, 2 failed: exactly two thirds+ for t in tasks.iter_mut().take(2) {+ t.state = "verified".into();+ }+ let o = Fx::new(BERLIN, after_weekly(), tasks.clone()).only_offer();+ assert_eq!((o.evidence.verified, o.evidence.settled), (4, 6));+ // 1 verified, the rest still open: too few+ for (i, t) in tasks.iter_mut().enumerate() {+ t.state = if i == 5 { "verified" } else { "review" }.into();+ }+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks.clone()).only_skip(), Reason::NotVerified);+ // 2 verified, the rest open: enough+ tasks[4].state = "verified".into();+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks).only_offer().evidence.settled, 2);+ }++ #[test]+ fn one_bot_must_have_done_two_thirds_of_the_verified_ones() {+ let mut tasks = weekly("o", "Weekly report");+ for t in tasks.iter_mut().take(2) {+ t.owner_kwant_id = Some("bot-b".into());+ }+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks.clone()).only_offer().kwant_id, "bot-a");+ tasks[2].owner_kwant_id = Some("bot-b".into());+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks.clone()).only_skip(), Reason::MixedOwners);+ for t in &mut tasks {+ t.owner_kwant_id = None;+ }+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks).only_skip(), Reason::MixedOwners);+ }++ #[test]+ fn a_bot_no_longer_in_the_space_gets_no_offer() {+ let mut fx = Fx::new(BERLIN, after_weekly(), weekly("u", "Weekly report"));+ fx.bots.remove("bot-a");+ assert_eq!(fx.only_skip(), Reason::BotUnavailable);+ }++ fn routine(kwant: &str, name: &str) -> RoutineRef {+ RoutineRef { kwant_id: kwant.into(), name: name.into(), instructions: String::new(), task_class: None, cluster_key: None }+ }++ #[test]+ fn covered_by_a_routine_on_or_off_of_the_same_habit() {+ let tasks = weekly("c", "Weekly sales report");+ let key = cluster_key("general", "Weekly sales report").unwrap();+ let mut fx = Fx::new(BERLIN, after_weekly(), tasks);+ // Accepted from this habit, whatever its bot or name+ fx.routines = vec![RoutineRef { cluster_key: Some(key), ..routine("bot-b", "Something else") }];+ assert_eq!(fx.only_skip(), Reason::Covered);+ // Named for it, same bot+ fx.routines = vec![routine("bot-a", "Sales report weekly")];+ assert_eq!(fx.only_skip(), Reason::Covered);+ // Its instructions start with it: 3 of 5 words is the 0.6 edge+ fx.routines = vec![RoutineRef { instructions: "Weekly sales report, as usual\nUse the CRM.".into(), ..routine("bot-a", "Mondays") }];+ assert_eq!(fx.only_skip(), Reason::Covered);+ // Another bot's routine of that name, or an unrelated one, doesn't cover it+ fx.routines = vec![routine("bot-b", "Weekly sales report"), routine("bot-a", "Weekly hiring report")];+ assert_eq!(fx.only_offer().cluster_key, cluster_key("general", "Weekly sales report").unwrap());+ }++ #[test]+ fn covered_by_class() {+ let mut tasks = weekly("k", "Invoices");+ for t in &mut tasks {+ t.task_class = "invoice".into();+ }+ let mut fx = Fx::new(BERLIN, after_weekly(), tasks);+ let o = fx.only_offer();+ assert_eq!((o.cluster_key.as_str(), o.task_class.as_str()), ("class:invoice", "invoice"));+ fx.routines = vec![RoutineRef { task_class: Some("invoice".into()), ..routine("bot-a", "Billing") }];+ assert_eq!(fx.only_skip(), Reason::Covered);+ }++ #[test]+ fn cooling_off_and_already_offered() {+ let key = cluster_key("general", "Weekly report").unwrap();+ let mut fx = Fx::new(BERLIN, after_weekly(), weekly("d", "Weekly report"));+ fx.past.cooling.insert(key.clone());+ assert_eq!(fx.only_skip(), Reason::CoolingOff);+ fx.past = Past { live: HashSet::from([key]), live_count: 1, ..Past::default() };+ assert_eq!(fx.only_skip(), Reason::AlreadyOffered);+ }++ #[test]+ fn the_cap_keeps_the_strongest_habits() {+ let mut tasks = weekly("a", "Weekly sales report");+ tasks.extend(asks(BERLIN, "b", "Check server backups", &every("2026-08-25", 7, 8), "11:00"));+ tasks.extend(asks(BERLIN, "c", "Update hiring pipeline", &every("2026-09-09", 7, 5), "15:00"));+ let mut fx = Fx::new(BERLIN, after_weekly(), tasks);+ let names = |s: &Scan| s.offered.iter().map(|o| o.name.clone()).collect::<Vec<_>>();+ fx.cap = 3;+ assert_eq!(names(&fx.scan()), ["Check server backups", "Weekly sales report", "Update hiring pipeline"]);+ // Two a day+ fx.cap = 2;+ let scan = fx.scan();+ assert_eq!(names(&scan), ["Check server backups", "Weekly sales report"]);+ assert_eq!(scan.skipped.iter().map(|s| (s.title.as_str(), s.reason)).collect::<Vec<_>>(), [("Update hiring pipeline", Reason::Capped)]);+ // One made today already+ fx.past.offered_24h = 1;+ assert_eq!(names(&fx.scan()), ["Check server backups"]);+ // Five live in the space+ fx.past = Past { live_count: 5, ..Past::default() };+ let scan = fx.scan();+ assert!(scan.offered.is_empty());+ assert!(scan.skipped.iter().all(|s| s.reason == Reason::Capped));+ // Cap 0: never+ fx.past = Past::default();+ fx.cap = 0;+ assert!(fx.scan().offered.is_empty());+ }++ // Chains++ fn with_follow_up(owner: &str) -> Vec<TaskRow> {+ let mut tasks = weekly("a", "Weekly sales report");+ let follow: Vec<TaskRow> = tasks+ .iter()+ .enumerate()+ .map(|(i, a)| TaskRow { owner_kwant_id: Some(owner.into()), ..task(&format!("b-{i}"), "Post the sales summary to the team", a.finished_at.unwrap() + Duration::hours(2)) })+ .collect();+ tasks.extend(follow);+ tasks+ }++ #[test]+ fn a_follow_up_by_the_same_bot_becomes_a_step() {+ let scan = Fx::new(BERLIN, after_weekly(), with_follow_up("bot-a")).scan();+ assert_eq!(scan.offered.len(), 1);+ let o = &scan.offered[0];+ assert_eq!(o.name, "Weekly sales report");+ assert_eq!(o.chain.len(), 1);+ assert!(o.chain[0].in_instructions);+ assert_eq!(o.chain[0].after, 6);+ assert!(o.instructions.contains("Then, in the same run:\n1. Post the sales summary to the team"), "{}", o.instructions);+ // The follow-up isn't offered on its own as well+ let b = cluster_key("general", "Post the sales summary to the team").unwrap();+ assert_eq!(scan.skipped.iter().map(|s| (s.cluster_key.clone(), s.reason)).collect::<Vec<_>>(), [(b, Reason::InChain)]);+ }++ #[test]+ fn a_follow_up_by_another_bot_is_mentioned_not_a_step() {+ let scan = Fx::new(BERLIN, after_weekly(), with_follow_up("bot-b")).scan();+ let a = scan.offered.iter().find(|o| o.name == "Weekly sales report").unwrap();+ assert_eq!(a.chain.len(), 1);+ assert_eq!(a.chain[0].kwant_id.as_deref(), Some("bot-b"));+ assert!(!a.chain[0].in_instructions);+ assert!(!a.instructions.contains("Post the sales summary"), "{}", a.instructions);+ assert!(!a.instructions.contains("Then, in the same run"));+ // It is its own habit, of its own bot+ let b = scan.offered.iter().find(|o| o.name == "Post the sales summary to the team").unwrap();+ assert_eq!(b.kwant_id, "bot-b");+ }++ #[test]+ fn a_follow_up_needs_to_come_after_most_asks_within_a_day() {+ // After 4 of 6 asks: under 80%+ let mut tasks = with_follow_up("bot-a");+ tasks.retain(|t| t.id != "b-0" && t.id != "b-3");+ let scan = Fx::new(BERLIN, after_weekly(), tasks).scan();+ assert!(scan.offered.iter().find(|o| o.name == "Weekly sales report").unwrap().chain.is_empty());+ // More than a day after the ask was done+ let mut late = with_follow_up("bot-a");+ for t in late.iter_mut().filter(|t| t.id.starts_with("b-")) {+ t.created_at += Duration::hours(23);+ }+ let scan = Fx::new(BERLIN, after_weekly(), late).scan();+ assert!(scan.offered.iter().all(|o| o.chain.is_empty()));+ }++ #[test]+ fn a_live_offers_step_is_not_offered_again() {+ let mut fx = Fx::new(BERLIN, after_weekly(), weekly("b", "Post the sales summary to the team"));+ fx.past.live_chain.insert(cluster_key("general", "Post the sales summary to the team").unwrap());+ assert_eq!(fx.only_skip(), Reason::InChain);+ }++ // What the offer carries++ #[test]+ fn checks_that_passed_with_the_same_spec_twice_are_kept() {+ let tasks = weekly("t", "Weekly report");+ let mut checks = Vec::new();+ for (i, t) in tasks.iter().enumerate() {+ let c = |kind: &str, spec: String| PassedCheck { task_id: t.id.clone(), kind: kind.into(), spec, required: true };+ checks.push(c("file_exists", r#"{"path":"reports/weekly.md"}"#.into()));+ checks.push(c("person", r#"{"question":"Looks right?"}"#.into()));+ checks.push(c("file_contains", format!(r#"{{"path":"reports/{i}.md","text":"Total"}}"#)));+ if i == 0 {+ checks.push(c("http", r#"{"url":"https://example.com/health"}"#.into()));+ }+ }+ let mut fx = Fx::new(BERLIN, after_weekly(), tasks);+ fx.checks = checks;+ let o = fx.only_offer();+ assert_eq!(o.checks, vec![CheckSpec { kind: "file_exists".into(), spec: serde_json::json!({"path": "reports/weekly.md"}), required: true }]);+ let left: Vec<(&str, &str)> = o.left_out_checks.iter().map(|l| (l.kind.as_str(), l.reason)).collect();+ assert_eq!(left, [("person", "needs a person or a reviewer"), ("file_contains", "changes every time"), ("http", "passed only once")]);+ assert_eq!(o.acceptance, "The report is in the shared folder");+ assert!(o.instructions.starts_with("Weekly report\n\nA good result: The report is in the shared folder"));+ assert!(o.instructions.contains("Based on your tasks: \"Weekly report 2026-10-12\" (2026-10-12)"), "{}", o.instructions);+ }++ #[test]+ fn delivery_follows_where_the_asks_were_made() {+ let mut tasks = weekly("p", "Weekly report");+ let fx = Fx::new(BERLIN, after_weekly(), tasks.clone());+ assert_eq!(fx.only_offer().delivery, Delivery { direct_kwant_id: Some("bot-a".into()), ..Default::default() });+ for t in &mut tasks {+ t.channel_id = Some("room-1".into());+ }+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks.clone()).only_offer().delivery, Delivery { channel_id: Some("room-1".into()), ..Default::default() });+ for t in tasks.iter_mut().skip(2) {+ t.thread_id = Some("thread-9".into());+ }+ assert_eq!(Fx::new(BERLIN, after_weekly(), tasks).only_offer().delivery, Delivery { thread_id: Some("thread-9".into()), ..Default::default() });+ }++ #[test]+ fn times_far_apart_are_marked_as_varying() {+ let mut tasks = weekly("t", "Weekly report");+ tasks[1].created_at += Duration::hours(5);+ let o = Fx::new(BERLIN, after_weekly(), tasks).only_offer();+ assert_eq!(o.evidence.time_spread, "09:00–14:00");+ assert!(o.evidence.time_varies);+ assert_eq!(o.schedule.tz(), Some(BERLIN));+ }++ #[test]+ fn first_run_skips_this_periods_ask_already_done() {+ let s = Schedule::Daily { time: "07:00".into(), tz: BERLIN.into() };+ let asked = at(BERLIN, date("2026-10-02"), "07:10");+ // Asked at 07:10, now 06:00 the next day: the next run is today 07:00+ assert_eq!(first_run_at(&s, asked, at(BERLIN, date("2026-10-03"), "06:00")), Some(at(BERLIN, date("2026-10-03"), "07:00")));+ // Asked at 06:00 before the usual time: not again at 07:00 the same day+ let early = at(BERLIN, date("2026-10-02"), "06:00");+ assert_eq!(first_run_at(&s, early, early + Duration::minutes(5)), Some(at(BERLIN, date("2026-10-03"), "07:00")));+ let w = Schedule::Weekly { time: "09:00".into(), weekdays: vec!["mon".into()], tz: BERLIN.into() };+ let monday = at(BERLIN, date("2026-10-05"), "08:00");+ assert_eq!(first_run_at(&w, monday, monday + Duration::hours(1)), Some(at(BERLIN, date("2026-10-12"), "09:00")));+ }++ #[test]+ fn an_unreadable_time_zone_finds_nothing() {+ let fx = Fx { tz: "Mars/Olympus", ..Fx::new(BERLIN, after_weekly(), weekly("z", "Weekly report")) };+ assert_eq!(fx.scan(), Scan::default());+ }++ #[test]+ fn the_same_rows_give_the_same_scan() {+ let mut tasks = with_follow_up("bot-b");+ tasks.extend(asks(BERLIN, "c", "Update hiring pipeline", &every("2026-09-09", 7, 5), "15:00"));+ let fx = Fx::new(BERLIN, after_weekly(), tasks.clone());+ tasks.reverse();+ let reversed = Fx::new(BERLIN, after_weekly(), tasks);+ assert_eq!(fx.scan(), reversed.scan());+ }+}diff --git a/src/db/automation.rs b/src/db/automation.rsindex 336e386b..946ea60f 100644--- a/src/db/automation.rs+++ b/src/db/automation.rs@@ -4,12 +4,26 @@//! nothing. See docs/mission-control.md §20.use super::{Database, DbError};+use crate::automation_discovery::{self as miner, PassedCheck, Past, RoutineRef, TaskRow};+use chrono::{DateTime, Duration, Utc};+use serde_json::Value;+use std::collections::HashSet;/// Routine columns this migration adds, all nullable: a routine without them runs as before./// `origin` is `NULL` (made by hand or by a bot) or `discovery`; `checks` is JSON/// `[{kind, spec, required}]`.const ROUTINE_COLUMNS: [&str; 5] = ["origin", "suggestion_id", "task_class", "acceptance", "checks"];+/// The tasks the miner learns from (design §4): created by the person on the board, as a task or+/// in a mission, in the space, since `?2`. Not tasks a bot, a routine, a hand-off or a chat made,+/// not retries (forks) of another task, and not cancelled ones.+const DISCOVERY_TASKS: &str = "FROM tasks t+ WHERE t.space_id = ?1 AND t.created_by = 'person' AND t.source_kind IN ('person', 'mission')+ AND t.forked_from IS NULL AND t.state <> 'cancelled' AND t.created_at >= ?2";++type TaskTuple = (String, String, String, String, Option<String>, String, Option<String>, Option<String>, Option<String>, DateTime<Utc>, Option<DateTime<Utc>>);+type RoutineTuple = (String, String, String, Option<String>, Option<String>);+impl Database {pub(super) async fn migrate_automation_discovery(&self) -> Result<(), DbError> {let cols: Vec<(String,)> = sqlx::query_as("SELECT name FROM pragma_table_info('routines')").fetch_all(&self.pool).await?;@@ -69,4 +83,101 @@ impl Database {}Ok(())}++ /// The person's board tasks of the last [`miner::MONTHLY_WINDOW_DAYS`], oldest first.+ pub async fn discovery_tasks(&self, space: &str, now: DateTime<Utc>) -> Result<Vec<TaskRow>, DbError> {+ let rows: Vec<TaskTuple> = sqlx::query_as(&format!(+ "SELECT t.id, t.title, t.acceptance, t.state, t.owner_kwant_id, t.task_class, t.channel_id, t.direct_kwant_id,+ t.thread_id, t.created_at, t.finished_at+ {DISCOVERY_TASKS} ORDER BY t.created_at, t.id"+ ))+ .bind(space)+ .bind(now - Duration::days(miner::MONTHLY_WINDOW_DAYS))+ .fetch_all(&self.pool)+ .await?;+ Ok(rows+ .into_iter()+ .map(|(id, title, acceptance, state, owner_kwant_id, task_class, channel_id, direct_kwant_id, thread_id, created_at, finished_at)| TaskRow {+ id,+ title,+ acceptance,+ state,+ owner_kwant_id,+ task_class,+ channel_id,+ direct_kwant_id,+ thread_id,+ created_at,+ finished_at,+ })+ .collect())+ }++ /// Checks that passed on those tasks.+ pub async fn discovery_checks(&self, space: &str, now: DateTime<Utc>) -> Result<Vec<PassedCheck>, DbError> {+ let rows: Vec<(String, String, String, bool)> = sqlx::query_as(&format!(+ "SELECT c.task_id, c.kind, c.spec, c.required FROM outcome_checks c+ WHERE c.status = 'passed' AND c.task_id IN (SELECT t.id {DISCOVERY_TASKS})+ ORDER BY c.created_at, c.id"+ ))+ .bind(space)+ .bind(now - Duration::days(miner::MONTHLY_WINDOW_DAYS))+ .fetch_all(&self.pool)+ .await?;+ Ok(rows.into_iter().map(|(task_id, kind, spec, required)| PassedCheck { task_id, kind, spec, required }).collect())+ }++ /// Every routine of the space, on or off, with the habit a discovered one came from.+ pub async fn discovery_routines(&self, space: &str) -> Result<Vec<RoutineRef>, DbError> {+ let rows: Vec<RoutineTuple> = sqlx::query_as(+ "SELECT r.kwant_id, r.name, r.instructions, r.task_class, s.cluster_key FROM routines r+ LEFT JOIN routine_suggestions s ON s.id = r.suggestion_id+ WHERE r.space_id = ? ORDER BY r.created_at, r.id",+ )+ .bind(space)+ .fetch_all(&self.pool)+ .await?;+ Ok(rows+ .into_iter()+ .map(|(kwant_id, name, instructions, task_class, cluster_key)| RoutineRef { kwant_id, name, instructions, task_class, cluster_key })+ .collect())+ }++ /// What earlier offers in the space mean for a scan now: habits cooling off (dismissed, or+ /// their discovered routine deleted, within [`miner::DISMISS_COOL_OFF_DAYS`]), live offers and+ /// the follow-ups written into them, and how many were made in the last 24 hours.+ pub async fn discovery_past(&self, space: &str, now: DateTime<Utc>) -> Result<Past, DbError> {+ let cooling: Vec<(String,)> = sqlx::query_as(+ "SELECT DISTINCT cluster_key FROM routine_suggestions+ WHERE space_id = ?1 AND ((status = 'dismissed' AND cooldown_until > ?2) OR routine_deleted_at > ?3)",+ )+ .bind(space)+ .bind(now)+ .bind(now - Duration::days(miner::DISMISS_COOL_OFF_DAYS))+ .fetch_all(&self.pool)+ .await?;+ let live: Vec<(String, String)> = sqlx::query_as("SELECT cluster_key, chain FROM routine_suggestions WHERE space_id = ? AND status = 'offered'")+ .bind(space)+ .fetch_all(&self.pool)+ .await?;+ let (offered_24h,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM routine_suggestions WHERE space_id = ? AND offered_at > ?")+ .bind(space)+ .bind(now - Duration::hours(24))+ .fetch_one(&self.pool)+ .await?;+ let live_chain: HashSet<String> = live+ .iter()+ .filter_map(|(_, chain)| serde_json::from_str::<Vec<Value>>(chain).ok())+ .flatten()+ .filter(|step| step.get("in_instructions").and_then(Value::as_bool) == Some(true))+ .filter_map(|step| step.get("key").and_then(Value::as_str).map(str::to_string))+ .collect();+ Ok(Past {+ cooling: cooling.into_iter().map(|(k,)| k).collect(),+ live_count: live.len(),+ live: live.into_iter().map(|(k, _)| k).collect(),+ live_chain,+ offered_24h: usize::try_from(offered_24h).unwrap_or(0),+ })+ }}diff --git a/src/lib.rs b/src/lib.rsindex 3bc4745d..537a0346 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -1,4 +1,5 @@pub mod app_update;+pub mod automation_discovery;pub mod broker;pub mod config;pub mod db;diff --git a/src/server/auth.rs b/src/server/auth.rsindex 573a418e..f564b578 100644--- a/src/server/auth.rs+++ b/src/server/auth.rs@@ -371,6 +371,8 @@ pub fn person_only(method: &Method, path: &str) -> bool {// or rejecting one is the person's call: bots only propose (`skill_propose`)// Skill Foundry clusters and drafts: the person's review queue, read too["", "api", "skills", "foundry", ..] | ["", "api", "skills", _, "foundry"] => true,+ // Automation Discovery reads every board task the person made in the space+ ["", "api", "automation-discovery", ..] => true,["", "api", "skills", ..] => *method != Method::GET && *method != Method::HEAD,["", "api", "bot-actions" | "imports", _, "undo"] => *method == Method::POST,_ => false,@@ -866,6 +868,8 @@ mod tests {assert!(person_only(&get, "/api/skills/s1/foundry"));assert!(person_only(&post, "/api/skills/s1/rollback"));assert!(person_only(&post, "/api/skills/s1/replay"));+ assert!(person_only(&post, "/api/automation-discovery/scan"));+ assert!(person_only(&get, "/api/automation-discovery/scan?space=s1"));assert!(!person_only(&get, "/api/skills/s1"));assert!(person_only(&post, "/api/missions"));assert!(person_only(&post, "/api/missions/m1/approve"));diff --git a/src/server/missions/discovery.rs b/src/server/missions/discovery.rsnew file mode 100644index 00000000..34e2dd13--- /dev/null+++ b/src/server/missions/discovery.rs@@ -0,0 +1,132 @@+//! Mission Control Phase D4: Automation Discovery's scan. Reads the person's board tasks of one+//! space, runs the miner (`crate::automation_discovery`) and says which routines it would suggest+//! and why every other habit gets none. Step 2 is a preview: it stores nothing, neither an offer+//! nor a routine, and offering, Accept and Dismiss come in step 3. Off by default. See+//! docs/mission-control.md §20 and mission-control/phase-d/automation-discovery.md.++use super::routes::internal;+use crate::automation_discovery::{self as miner, Delivery, Input, Offer, Scan};+use crate::db::spaces::SpaceId;+use crate::db::DbError;+use crate::routines::now_in;+use crate::server::spaces::Scope;+use crate::server::AppState;+use axum::{+ extract::State,+ response::{IntoResponse, Response},+ Json,+};+use chrono::{DateTime, Utc};+use serde_json::{json, Value};+use std::collections::HashSet;++pub const SETTING: &str = "automation_discovery";+pub const MODES: [&str; 2] = ["off", "on"];+pub const CAP_SETTING: &str = "automation_discovery_daily_cap";++pub async fn enabled(state: &AppState) -> bool {+ matches!(state.db.get_setting(SETTING).await.ok().flatten().as_deref().map(str::trim), Some("on"))+}++/// Offers made per space per 24 hours (0–10; 0 offers none).+pub async fn daily_cap(state: &AppState) -> i64 {+ super::int_setting(state, CAP_SETTING, miner::DAILY_CAP_RANGE, miner::DEFAULT_DAILY_CAP).await+}++pub fn settings_error(mode: Option<&str>, cap: Option<i64>) -> Option<String> {+ if let Some(m) = mode.filter(|m| !MODES.contains(m)) {+ return Some(format!("{SETTING} must be off or on (got {m})"));+ }+ let (lo, hi) = miner::DAILY_CAP_RANGE;+ if let Some(n) = cap.filter(|n| !(lo..=hi).contains(n)) {+ return Some(format!("{CAP_SETTING} must be {lo}–{hi} (got {n})"));+ }+ None+}++/// Why no scan runs: `off` (the setting), `board_off` (the Mission board is off, so there are no+/// board tasks to learn from) or `no_timezone` (habits are local times, and none is set, or it is+/// one this machine can't read). `Ok` with the zone when a scan can run.+async fn blocked_by(state: &AppState, now: DateTime<Utc>) -> Result<String, &'static str> {+ if !enabled(state).await {+ return Err("off");+ }+ if !super::enabled(state).await {+ return Err("board_off");+ }+ let tz = state.db.get_setting(crate::server::bot_actions::TIMEZONE_SETTING).await.ok().flatten().map(|t| t.trim().to_string()).unwrap_or_default();+ if tz.is_empty() || now_in(&tz, now).is_err() {+ return Err("no_timezone");+ }+ Ok(tz)+}++/// Where an offer would deliver, if that place is still there: its bot's own unarchived thread,+/// or a channel of the space; else the bot's direct chat.+async fn deliverable(state: &AppState, space: &str, offer: &Offer) -> Delivery {+ let direct = Delivery { direct_kwant_id: Some(offer.kwant_id.clone()), ..Default::default() };+ if let Some(t) = &offer.delivery.thread_id {+ return match state.db.get_thread(t).await {+ Ok(Some(t)) if t.kwant_id == offer.kwant_id && !t.archived => offer.delivery.clone(),+ _ => direct,+ };+ }+ if let Some(c) = &offer.delivery.channel_id {+ return match state.db.get_channel(c).await {+ Ok(Some(c)) if c.space_id == space => offer.delivery.clone(),+ _ => direct,+ };+ }+ direct+}++/// One scan of `space` at `now`, read-only: what would be offered and what was skipped.+pub async fn preview(state: &AppState, space_id: &SpaceId, now: DateTime<Utc>) -> Result<Value, DbError> {+ let space = space_id.as_str();+ let tz = match blocked_by(state, now).await {+ Ok(tz) => tz,+ Err(why) => return Ok(json!({ "blocked_by": why, "preview": true, "offered": [], "skipped": [] })),+ };+ let tasks = state.db.discovery_tasks(space, now).await?;+ let checks = state.db.discovery_checks(space, now).await?;+ let routines = state.db.discovery_routines(space).await?;+ let past = state.db.discovery_past(space, now).await?;+ let bots: HashSet<String> = state.db.list_kwants(space_id).await?.into_iter().map(|k| k.id).collect();+ let Scan { mut offered, skipped } = miner::mine(&Input {+ tz: &tz,+ now,+ tasks: &tasks,+ checks: &checks,+ routines: &routines,+ bots: &bots,+ past: &past,+ daily_cap: usize::try_from(daily_cap(state).await).unwrap_or(0),+ max_live: miner::MAX_LIVE_PER_SPACE,+ });+ for o in &mut offered {+ o.delivery = deliverable(state, space, o).await;+ // Shown on a card: secrets in a check's spec never are+ for c in &mut o.checks {+ c.spec = super::autofix::redact_value(&c.spec);+ }+ for c in &mut o.left_out_checks {+ c.spec = super::autofix::redact_value(&c.spec);+ }+ }+ Ok(json!({+ "blocked_by": null,+ "preview": true,+ "timezone": tz,+ "offered": offered,+ "skipped": skipped,+ }))+}++/// `POST /api/automation-discovery/scan?space=` (person only): what a scan would offer now, and+/// why every other habit gets none. Writes nothing; with the setting off it reads nothing either.+pub async fn scan(State(state): State<AppState>, Scope(space): Scope) -> Response {+ match preview(&state, &space, Utc::now()).await {+ Ok(v) => Json(v).into_response(),+ Err(e) => internal(e),+ }+}diff --git a/src/server/missions/mod.rs b/src/server/missions/mod.rsindex 6395e9e4..93b24964 100644--- a/src/server/missions/mod.rs+++ b/src/server/missions/mod.rs@@ -16,6 +16,7 @@ pub mod auditor;pub mod autofix;pub mod checkpoints;pub mod checks;+pub mod discovery;pub mod flight_school;pub mod foundry;pub mod graph;diff --git a/src/server/mod.rs b/src/server/mod.rsindex bffbb3e4..3fa402b9 100644--- a/src/server/mod.rs+++ b/src/server/mod.rs@@ -404,6 +404,7 @@ pub fn create_router(state: AppState) -> Router {.route("/api/human-steps/:id", get(human_steps::get)).route("/api/human-steps/:id/done", post(human_steps::done)).route("/api/human-steps/:id/decline", post(human_steps::decline))+ .route("/api/automation-discovery/scan", post(missions::discovery::scan)).route("/api/skills/foundry", get(missions::foundry::overview)).route("/api/skills/foundry/:cluster/draft", post(missions::foundry::draft_now)).route("/api/skills/:id/foundry", get(missions::foundry::detail))diff --git a/src/server/routes.rs b/src/server/routes.rsindex 89289667..f109ecef 100644--- a/src/server/routes.rs+++ b/src/server/routes.rs@@ -3485,6 +3485,10 @@ pub struct SettingsResponse {pub skill_foundry_replays: i64,/// Fresh tokens one replay turn may use before it is stopped (5,000–500,000).pub skill_foundry_replay_tokens: i64,+ /// Automation Discovery: `off` (default) or `on` (see `super::missions::discovery`).+ pub automation_discovery: String,+ /// Routines suggested per space per 24 hours (0–10).+ pub automation_discovery_daily_cap: i64,}/// Unknown keys are refused, so a typo or a key from another version fails loudly instead of@@ -3536,6 +3540,8 @@ pub struct SaveSettingsPayload {pub skill_foundry_daily_drafts: Option<i64>,pub skill_foundry_replays: Option<i64>,pub skill_foundry_replay_tokens: Option<i64>,+ pub automation_discovery: Option<String>,+ pub automation_discovery_daily_cap: Option<i64>,}pub async fn get_settings(State(state): State<AppState>) -> Result<Json<SettingsResponse>, StatusCode> {@@ -3596,6 +3602,8 @@ pub async fn get_settings(State(state): State<AppState>) -> Result<Json<Settingsskill_foundry_daily_drafts: super::missions::foundry::daily_drafts(&state).await,skill_foundry_replays: super::missions::foundry::replays(&state).await,skill_foundry_replay_tokens: super::missions::foundry::replay_tokens(&state).await,+ automation_discovery: if super::missions::discovery::enabled(&state).await { "on" } else { "off" }.into(),+ automation_discovery_daily_cap: super::missions::discovery::daily_cap(&state).await,}))}@@ -3666,6 +3674,9 @@ pub async fn save_settings() {return bad(e);}+ if let Some(e) = super::missions::discovery::settings_error(payload.automation_discovery.as_deref(), payload.automation_discovery_daily_cap) {+ return bad(e);+ }if payload.chain_token_budget.is_some_and(|n| n > super::budget::MAX) {return bad(format!("chain_token_budget must be 0–{}", super::budget::MAX));}@@ -3811,6 +3822,12 @@ pub async fn save_settings(}}}+ if let Some(m) = &payload.automation_discovery {+ let _ = state.db.set_setting(super::missions::discovery::SETTING, m).await;+ }+ if let Some(n) = payload.automation_discovery_daily_cap {+ let _ = state.db.set_setting(super::missions::discovery::CAP_SETTING, &n.to_string()).await;+ }if let Some(on) = payload.agent_tools {let _ = state.db.set_setting("agent_tools", if on { "on" } else { "off" }).await;}diff --git a/tests/automation_discovery_test.rs b/tests/automation_discovery_test.rsnew file mode 100644index 00000000..ca6723f3--- /dev/null+++ b/tests/automation_discovery_test.rs@@ -0,0 +1,285 @@+//! Automation Discovery step 2 (Phase D4): the read-only scan route. It is off by default and+//! then reads nothing; on, it previews offers from the person's board tasks and never writes an+//! offer or a routine; bots can't call it; it only learns from tasks the person made. The miner's+//! own cases are unit tests in `src/automation_discovery.rs`. Contract: docs/mission-control.md §20.++use axum::{+ body::{to_bytes, Body},+ http::{Request, StatusCode},+ Router,+};+use chrono::{DateTime, Datelike, Duration, NaiveTime, Utc};+use kwnt::{+ automation_discovery::cluster_key,+ broker::PermissionBroker,+ config::Config,+ db::{+ continuity::NewRoutine,+ missions::{NewCheck, NewTask},+ models::{Kwant, NewKwant},+ spaces::SpaceId,+ Database,+ },+ router::LayaRouter,+ routines::{local_date, local_instant},+ server::{+ continuity::{SelfScope, TurnOrigin},+ create_router, mcp_runtime, AppState,+ },+ tools::ToolRunner,+};+use serde_json::{json, Value};+use sqlx::{sqlite::SqlitePoolOptions, SqlitePool};+use tempfile::{tempdir, TempDir};+use tower::ServiceExt;++const TZ: &str = "Europe/Berlin";+const SCAN: &str = "/api/automation-discovery/scan";++struct Env {+ app: Router,+ state: AppState,+ general: String,+ pool: SqlitePool,+ _tmp: TempDir,+}++async fn setup() -> Env {+ let tmp = tempdir().unwrap();+ let data = tmp.path().join("data");+ let db = Database::init(&data).await.unwrap();+ let config = Config { data_dir: data.clone(), laya_url: "http://127.0.0.1:9".into(), browser_cdp_url: Some("http://127.0.0.1:9".into()), ..Config::default() };+ let state = AppState::new(config.clone(), db, ToolRunner::new(data.join("workspace")), PermissionBroker::new(), LayaRouter::new(config.laya_url.clone()));+ let general = state.db.list_channels(&SpaceId::default_space()).await.unwrap().into_iter().find(|c| c.name == "general").unwrap().id;+ let pool = SqlitePoolOptions::new().max_connections(1).connect(&format!("sqlite://{}", data.join("kwnt.db").display())).await.unwrap();+ let e = Env { app: create_router(state.clone()), state, general, pool, _tmp: tmp };+ person(&e, "POST", "/api/settings", Some(json!({"mission_board": true, "timezone": TZ}))).await;+ e+}++async fn call(app: &Router, method: &str, uri: &str, auth: Option<&str>, body: Option<Value>) -> (StatusCode, Value) {+ let mut req = Request::builder().method(method).uri(uri).header("content-type", "application/json");+ if let Some(token) = auth {+ req = req.header("authorization", format!("Bearer {token}"));+ }+ let body = body.map(|b| Body::from(b.to_string())).unwrap_or_else(Body::empty);+ let resp = app.clone().oneshot(req.body(body).unwrap()).await.unwrap();+ let status = resp.status();+ let bytes = to_bytes(resp.into_body(), usize::MAX).await.unwrap();+ (status, serde_json::from_slice(&bytes).unwrap_or(Value::Null))+}++async fn person(e: &Env, method: &str, uri: &str, body: Option<Value>) -> Value {+ let (status, v) = call(&e.app, method, uri, None, body).await;+ assert!(status.is_success(), "{method} {uri}: {status} {v}");+ v+}++async fn discovery_on(e: &Env) {+ person(e, "POST", "/api/settings", Some(json!({"automation_discovery": "on"}))).await;+}++async fn bot(e: &Env, name: &str) -> Kwant {+ let input = NewKwant {+ name: name.into(),+ title: "Tester".into(),+ persona_prompt: String::new(),+ soul: String::new(),+ avatar_sprite: "s".into(),+ driver_type: "cli".into(),+ preferred_model: "codex".into(),+ };+ e.state.db.create_kwant(&SpaceId::default_space(), &input).await.unwrap()+}++/// Noon in Berlin, `weeks` weeks before today there: a weekly habit on today's weekday.+fn weeks_ago(weeks: i64) -> DateTime<Utc> {+ let today = local_date(TZ, Utc::now()).unwrap().unwrap();+ local_instant(TZ, today - Duration::weeks(weeks), NaiveTime::from_hms_opt(12, 0, 0).unwrap()).unwrap().unwrap()+}++/// A task `owner` verified, made `created_by` from `source_kind` at `at`.+async fn done_task(e: &Env, owner: &Kwant, title: &str, at: DateTime<Utc>, source_kind: &str, created_by: &str) -> String {+ let t = NewTask {+ title: title.into(),+ acceptance: "The report is in the shared folder".into(),+ state: "ready".into(),+ priority: 1,+ owner_kwant_id: Some(owner.id.clone()),+ max_attempts: 3,+ source_kind: source_kind.into(),+ created_by: created_by.into(),+ ..Default::default()+ };+ let t = e.state.db.create_task(&SpaceId::default_space(), &t).await.unwrap();+ let check = NewCheck {+ task_id: t.id.clone(),+ kind: "file_exists".into(),+ spec: json!({"path": "reports/weekly.md"}),+ required: true,+ approved: true,+ requested_by: "person".into(),+ approved_by: None,+ };+ let c = e.state.db.add_check(&check).await.unwrap();+ e.state.db.finish_check(&c.id, "passed", None, "person", None).await.unwrap();+ sqlx::query("UPDATE tasks SET state = 'verified', created_at = ?, finished_at = ? WHERE id = ?")+ .bind(at)+ .bind(at + Duration::minutes(30))+ .bind(&t.id)+ .execute(&e.pool)+ .await+ .unwrap();+ t.id+}++/// Six weekly asks of `title`, made by the person on the board.+async fn habit(e: &Env, owner: &Kwant, title: &str) {+ for w in 1..=6 {+ done_task(e, owner, &format!("{title} {}", weeks_ago(w).date_naive()), weeks_ago(w), "person", "person").await;+ }+}++async fn count(e: &Env, table: &str) -> i64 {+ sqlx::query_as::<_, (i64,)>(&format!("SELECT COUNT(*) FROM {table}")).fetch_one(&e.pool).await.unwrap().0+}++async fn token(e: &Env, bot: &Kwant) -> String {+ let turn_id = format!("t-{}", uuid::Uuid::new_v4());+ let origin = TurnOrigin { channel_id: Some(e.general.clone()), ..Default::default() };+ let scope = SelfScope { kwant_id: bot.id.clone(), kwant_name: bot.name.clone(), origin: Some(origin), turn_id: Some(turn_id.clone()), ..Default::default() };+ mcp_runtime::prepare_turn_with_self(&e.state, bot, Some(turn_id), None, Some(scope)).await.unwrap().1+}++fn titles(v: &Value, list: &str) -> Vec<String> {+ v[list].as_array().unwrap().iter().map(|o| o[if list == "offered" { "name" } else { "title" }].as_str().unwrap().to_string()).collect()+}++#[tokio::test]+async fn the_scan_is_off_by_default_and_then_finds_nothing() {+ let e = setup().await;+ let a = bot(&e, "Ana").await;+ habit(&e, &a, "Weekly report").await;+ let s = person(&e, "GET", "/api/settings", None).await;+ assert_eq!((s["automation_discovery"].as_str(), s["automation_discovery_daily_cap"].as_i64()), (Some("off"), Some(2)));+ let v = person(&e, "POST", SCAN, None).await;+ assert_eq!(v, json!({"blocked_by": "off", "preview": true, "offered": [], "skipped": []}));+}++#[tokio::test]+async fn the_scan_needs_the_board_and_a_time_zone() {+ let e = setup().await;+ discovery_on(&e).await;+ person(&e, "POST", "/api/settings", Some(json!({"timezone": ""}))).await;+ assert_eq!(person(&e, "POST", SCAN, None).await["blocked_by"], "no_timezone");+ person(&e, "POST", "/api/settings", Some(json!({"mission_board": false, "timezone": TZ}))).await;+ assert_eq!(person(&e, "POST", SCAN, None).await["blocked_by"], "board_off");+}++#[tokio::test]+async fn a_scan_previews_a_habit_and_writes_nothing() {+ let e = setup().await;+ let a = bot(&e, "Ana").await;+ discovery_on(&e).await;+ habit(&e, &a, "Weekly report").await;+ // A one-off, and the same habit made by a bot, a routine or a hand-off: none of it counts+ done_task(&e, &a, "Fix the login page", weeks_ago(1), "person", "person").await;+ for w in 1..=6 {+ done_task(&e, &a, "Weekly digest", weeks_ago(w), "routine", "person").await;+ done_task(&e, &a, "Weekly digest", weeks_ago(w), "delegation", &a.id).await;+ done_task(&e, &a, "Weekly digest", weeks_ago(w), "mission", &a.id).await;+ done_task(&e, &a, "Weekly digest", weeks_ago(w), "chat", "person").await;+ }+ let before: Vec<i64> = written_rows(&e).await;++ let v = person(&e, "POST", SCAN, None).await;+ assert_eq!((v["blocked_by"].clone(), v["preview"].clone(), v["timezone"].clone()), (Value::Null, json!(true), json!(TZ)), "{v}");+ assert_eq!(titles(&v, "offered"), ["Weekly report"], "{v}");+ let o = &v["offered"][0];+ let local = local_date(TZ, weeks_ago(1)).unwrap().unwrap();+ let weekday = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"][local.weekday().num_days_from_monday() as usize];+ assert_eq!(o["schedule"], json!({"type": "weekly", "time": "12:00", "weekdays": [weekday], "tz": TZ}), "{o}");+ assert_eq!(o["kwant_id"], a.id);+ assert_eq!(o["cluster_key"], cluster_key("general", "Weekly report").unwrap());+ assert_eq!(o["delivery"], json!({"direct_kwant_id": a.id}));+ assert_eq!(o["checks"], json!([{"kind": "file_exists", "spec": {"path": "reports/weekly.md"}, "required": true}]));+ assert_eq!(o["evidence"]["pattern"], "weekly");+ assert_eq!(o["source_task_ids"].as_array().unwrap().len(), 6);+ assert!(o["first_run_at"].as_str().is_some() && o["first_run_text"].as_str().unwrap().ends_with(TZ), "{o}");+ // The one-off is skipped; the digest made by others isn't seen at all+ assert_eq!(v["skipped"], json!([{"cluster_key": cluster_key("general", "Fix the login page").unwrap(), "title": "Fix the login page", "asks": 1, "reason": "too_short"}]));++ // Nothing written: no offer, no history, no routine, no setting+ assert_eq!(written_rows(&e).await, before);+ assert_eq!(before[..3], [0, 0, 0]);+ // And the same again+ assert_eq!(person(&e, "POST", SCAN, None).await, v);+}++/// Rows in routine_suggestions, routine_suggestion_events, routines and settings.+async fn written_rows(e: &Env) -> Vec<i64> {+ let mut n = Vec::new();+ for t in ["routine_suggestions", "routine_suggestion_events", "routines", "settings"] {+ n.push(count(e, t).await);+ }+ n+}++#[tokio::test]+async fn a_routine_on_or_off_covers_the_habit() {+ let e = setup().await;+ let a = bot(&e, "Ana").await;+ discovery_on(&e).await;+ habit(&e, &a, "Weekly report").await;+ let r = NewRoutine {+ kwant_id: a.id.clone(),+ name: "Weekly report".into(),+ instructions: "Write the weekly report".into(),+ schedule: json!({"type": "weekly", "time": "12:00", "weekdays": ["mon"], "tz": TZ}).to_string(),+ enabled: false,+ ..Default::default()+ };+ let routine = e.state.db.create_routine(&r).await.unwrap();+ let key = cluster_key("general", "Weekly report").unwrap();+ let skipped = |v: &Value| v["skipped"].as_array().unwrap().iter().map(|s| (s["cluster_key"].as_str().unwrap().to_string(), s["reason"].as_str().unwrap().to_string())).collect::<Vec<_>>();+ let v = person(&e, "POST", SCAN, None).await;+ assert!(v["offered"].as_array().unwrap().is_empty(), "{v}");+ assert_eq!(skipped(&v), [(key.clone(), "covered".to_string())]);+ sqlx::query("UPDATE routines SET enabled = 1 WHERE id = ?").bind(&routine.id).execute(&e.pool).await.unwrap();+ assert_eq!(skipped(&person(&e, "POST", SCAN, None).await), [(key.clone(), "covered".to_string())]);+}++#[tokio::test]+async fn bots_cannot_scan() {+ let e = setup().await;+ let a = bot(&e, "Ana").await;+ discovery_on(&e).await;+ let t = token(&e, &a).await;+ let (status, _) = call(&e.app, "POST", SCAN, Some(&t), None).await;+ assert_eq!(status, StatusCode::FORBIDDEN);+ let (status, _) = call(&e.app, "POST", "/api/settings", Some(&t), Some(json!({"automation_discovery": "on"}))).await;+ assert_eq!(status, StatusCode::FORBIDDEN);+}++#[tokio::test]+async fn settings_are_checked() {+ let e = setup().await;+ for bad in [json!({"automation_discovery": "maybe"}), json!({"automation_discovery_daily_cap": 11}), json!({"automation_discovery_daily_cap": -1})] {+ let (status, v) = call(&e.app, "POST", "/api/settings", None, Some(bad.clone())).await;+ assert_eq!(status, StatusCode::BAD_REQUEST, "{bad}: {v}");+ }+ person(&e, "POST", "/api/settings", Some(json!({"automation_discovery": "on", "automation_discovery_daily_cap": 0}))).await;+ let s = person(&e, "GET", "/api/settings", None).await;+ assert_eq!((s["automation_discovery"].as_str(), s["automation_discovery_daily_cap"].as_i64()), (Some("on"), Some(0)));+}++#[tokio::test]+async fn a_daily_cap_of_zero_offers_nothing() {+ let e = setup().await;+ let a = bot(&e, "Ana").await;+ person(&e, "POST", "/api/settings", Some(json!({"automation_discovery": "on", "automation_discovery_daily_cap": 0}))).await;+ habit(&e, &a, "Weekly report").await;+ let v = person(&e, "POST", SCAN, None).await;+ assert!(v["offered"].as_array().unwrap().is_empty());+ assert_eq!(v["skipped"][0]["reason"], "capped");+}