feat(intentions): Phase 7 — if-then rules for task / time / triage triggers
Small if-then automation layer. Rules persist in SQLite; the runner lives on the frontend and binds to the Phase 6 event bus so the rule pipeline reuses the same delivery primitives (timer events, TTS, Tasks navigation). Storage: - Migration v12 adds implementation_rules (id, enabled, trigger_kind, trigger_value, actions_json, last_fired_key, created_at, updated_at) with enabled+trigger_kind index for the runner's hot path. - CRUD helpers: insert / list / get / set-enabled / mark-fired / delete, plus a round-trip test. Commands (all main-window-guarded via ensure_main_window): - list_implementation_rules - create_implementation_rule — validates HH:MM, checks the target task exists at save time for surface-task actions, caps speak-line at 240 chars, pins v1 timers to 5 minutes. - set_implementation_rule_enabled - mark_implementation_rule_fired — main-thread idempotency shim so the runner can atomically claim a fire. - delete_implementation_rule Runner (implementationIntentions.svelte.ts): - Subscribes to kon:task-completed and kon:morning-triage-finished (MorningTriageModal now emits on all three exit paths — empty, skipped, picked — so skip counts as finishing). - 30 s poll for time-of-day rules, plus an immediate check on startup so a rule whose time has already passed today catches up once. - Idempotency via last_fired_key composed as YYYY-MM-DD@HH:MM for time rules; new time rules whose HH:MM has already passed today are pre-seeded so they don't fire retroactively on save. - Rules are paused when Nudges "Mute for now" is on — a hard mute stops all rule delivery in addition to OS notifications. - Stale-task safety: if a surface-task action's target has been deleted, the runner opens Tasks and warns clearly rather than pretending to surface something that's gone. Editor (ImplementationRulesEditor.svelte): - Lives in Settings under a new "If-then rules" accordion section. - `If` picker: time of day (with time input), a task completes, morning triage finishes. - `Then` composer: optional surface (inbox / today / all tasks / specific task), optional 5-min timer, optional speak-aloud line. - Saved rules list with enable toggle + delete. Rules table integration for Phase 10b rename sweep: add implementation_rules to the kon.db → corbie.db migration shim when that phase lands. Gates: fmt, clippy -D warnings, cargo test 265/0, svelte-check 0/0, npm build green. Pre-existing Vite chunk warning on sounds.ts is unrelated to Phase 7.
This commit is contained in:
@@ -400,6 +400,36 @@ const MIGRATIONS: &[(i64, &str, &str)] = &[
|
||||
ON tasks(energy, created_at DESC);
|
||||
"#,
|
||||
),
|
||||
(
|
||||
12,
|
||||
"implementation intentions: if-then automation rules",
|
||||
r#"
|
||||
-- Phase 7 of the feature-complete roadmap. Rules are local-only,
|
||||
-- user-authored implementation intentions: "if this happens, then
|
||||
-- do this small thing". Execution stays in the frontend event bus;
|
||||
-- SQLite owns the durable definition and the once-per-day marker
|
||||
-- for time-of-day rules.
|
||||
CREATE TABLE implementation_rules (
|
||||
id TEXT PRIMARY KEY,
|
||||
enabled INTEGER NOT NULL DEFAULT 1
|
||||
CHECK (enabled IN (0, 1)),
|
||||
trigger_kind TEXT NOT NULL
|
||||
CHECK (trigger_kind IN (
|
||||
'time_of_day',
|
||||
'task_completed',
|
||||
'morning_triage_finished'
|
||||
)),
|
||||
trigger_value TEXT NOT NULL DEFAULT '',
|
||||
actions_json TEXT NOT NULL DEFAULT '[]',
|
||||
last_fired_key TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_implementation_rules_enabled_trigger
|
||||
ON implementation_rules(enabled, trigger_kind);
|
||||
"#,
|
||||
),
|
||||
];
|
||||
|
||||
/// Split SQL into individual statements, respecting BEGIN...END trigger blocks.
|
||||
@@ -549,7 +579,7 @@ mod tests {
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(count, 11);
|
||||
assert_eq!(count, 12);
|
||||
|
||||
sqlx::query("INSERT INTO settings (key, value) VALUES ('test', 'value')")
|
||||
.execute(&pool)
|
||||
@@ -568,7 +598,7 @@ mod tests {
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(count, 11);
|
||||
assert_eq!(count, 12);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -596,6 +626,44 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn migration_implementation_rules_adds_rule_table() {
|
||||
let pool = fk_test_pool().await;
|
||||
run_migrations(&pool).await.expect("migrate");
|
||||
|
||||
let info = sqlx::query("PRAGMA table_info(implementation_rules)")
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let names: Vec<String> = info.iter().map(|r| r.get::<String, _>("name")).collect();
|
||||
for col in [
|
||||
"id",
|
||||
"enabled",
|
||||
"trigger_kind",
|
||||
"trigger_value",
|
||||
"actions_json",
|
||||
"last_fired_key",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
] {
|
||||
assert!(
|
||||
names.contains(&col.to_string()),
|
||||
"implementation_rules must have {col}; got {names:?}"
|
||||
);
|
||||
}
|
||||
|
||||
let rejected = sqlx::query(
|
||||
"INSERT INTO implementation_rules (id, trigger_kind, actions_json)
|
||||
VALUES ('bad', 'calendar_event', '[]')",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await;
|
||||
assert!(
|
||||
rejected.is_err(),
|
||||
"trigger_kind CHECK constraint must reject unknown triggers"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn migration_transcripts_meta_adds_columns() {
|
||||
// Task 2.5 — verify starred / manual_tags / template / language /
|
||||
|
||||
Reference in New Issue
Block a user