Add the morning routine engine — a daily checklist, not four timers
Backlog item #3 (20-07-2026-BACKLOG.md). A morning routine is a checklist for a daily window: several items, each evidenced by a fact key, completed in any order, checked once near the end of the window. Modelling it as four independent reminder timers would stack into exactly the kind of noise Maven is supposed not to produce, so the engine nags at most once per day per routine and only for what is actually still missing. internal/morning follows the established pure-engine pattern (loop, routine, pattern): no store, no clock of its own. Evaluate answers "what's still missing" at any point; Due decides whether to nag. The impurity — reading facts under the store lock, holding the last-nudge map across ticks — stays in the tick driver, which calls Due each tick exactly as it does for loop.Rule and routine.Routine. Completion evidence is a fact key's latest non-voided value timestamped inside today's window, so manual ("выпил воды", voice-tapped) and inferred (another daemon writing the same key) are indistinguishable and both count. Weekdays scopes which days a routine applies to, so weekday/weekend variants are two routine rows rather than a special case in the engine. Exposed read-only: a MorningStatus RPC over ipc, and a /morning page in mavweb built on the same server-rendered shape as /trace — no live-update loop, since checklist state moves on the scale of minutes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5JApcrCRVGmqrxnhynSik
This commit is contained in:
+115
-2
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/kami/maven/internal/delivery"
|
||||
"github.com/kami/maven/internal/ipc"
|
||||
"github.com/kami/maven/internal/loop"
|
||||
"github.com/kami/maven/internal/morning"
|
||||
"github.com/kami/maven/internal/phraser"
|
||||
"github.com/kami/maven/internal/routine"
|
||||
"github.com/kami/maven/internal/store"
|
||||
@@ -61,6 +62,11 @@ type tickLoop struct {
|
||||
routines []routine.Routine
|
||||
routineLast map[string]time.Time
|
||||
|
||||
// morningRoutines — daily checklists (see internal/morning). morningLast
|
||||
// tracks the per-routine last-nudge day, mirroring routineLast.
|
||||
morningRoutines []morning.Routine
|
||||
morningLast map[string]time.Time
|
||||
|
||||
// digestQ — in-memory queue of eligible nudges waiting for batch flush.
|
||||
// populated when digestCfg != nil && digestCfg.Enabled.
|
||||
digestQ []QueuedNudge
|
||||
@@ -85,6 +91,7 @@ func newTickLoop(
|
||||
tickInterval, repeatInterval, autotuneInterval time.Duration,
|
||||
digestCfg *config.DigestConfig,
|
||||
routines []routine.Routine,
|
||||
morningRoutines []morning.Routine,
|
||||
) *tickLoop {
|
||||
return &tickLoop{
|
||||
store: st,
|
||||
@@ -99,6 +106,8 @@ func newTickLoop(
|
||||
digestQ: nil,
|
||||
routines: routines,
|
||||
routineLast: make(map[string]time.Time),
|
||||
morningRoutines: morningRoutines,
|
||||
morningLast: make(map[string]time.Time),
|
||||
lastPhrase: make(map[string]delivery.PhrasedNudge),
|
||||
}
|
||||
}
|
||||
@@ -177,6 +186,11 @@ func (t *tickLoop) tick(ctx context.Context, now time.Time) {
|
||||
// LLM-phrased — so a routine can't hallucinate. severity comes from config.
|
||||
t.fireRoutines(ctx, now, state)
|
||||
|
||||
// morning routines: daily checklists (medicine/water/pets/...), nagged at
|
||||
// most once per day per routine, and only for items still unevidenced at
|
||||
// nudge time. See internal/morning for the "why not four timers" rationale.
|
||||
t.fireMorningRoutines(ctx, now, state)
|
||||
|
||||
// reminders: gate-bypassing class. fired once, marked after a successful
|
||||
// delivery. a failed send leaves the reminder pending — the next tick
|
||||
// re-gathers and re-attempts.
|
||||
@@ -363,6 +377,97 @@ func (t *tickLoop) fireRoutines(ctx context.Context, now time.Time, state loop.S
|
||||
}
|
||||
}
|
||||
|
||||
// fireMorningRoutines checks each configured checklist against today's facts
|
||||
// and dispatches a nag listing exactly what's still missing, at most once per
|
||||
// routine per calendar day. Fact reads happen here (not in loop.Gatherer)
|
||||
// because the item↔fact-key mapping is morning-routine-specific, not a rule
|
||||
// concern — pulling it into the shared gather path would leak that mapping
|
||||
// into loop's "rules declare wanted keys" contract. Bodies are literal
|
||||
// operator text (item labels joined), not LLM-phrased, same rationale as
|
||||
// cron routines: deterministic, can't hallucinate a checklist item.
|
||||
func (t *tickLoop) fireMorningRoutines(ctx context.Context, now time.Time, state loop.State) {
|
||||
if len(t.morningRoutines) == 0 {
|
||||
return
|
||||
}
|
||||
facts := t.gatherMorningFacts(ctx)
|
||||
|
||||
for _, cand := range morning.Due(t.morningRoutines, facts, t.morningLast, now) {
|
||||
labels := make([]string, len(cand.Missing))
|
||||
for i, it := range cand.Missing {
|
||||
labels[i] = it.Label
|
||||
}
|
||||
body := fmt.Sprintf("%s: не сделано — %s", cand.Routine.Name, strings.Join(labels, ", "))
|
||||
pn := delivery.PhrasedNudge{
|
||||
Candidate: loop.Candidate{
|
||||
Rule: loop.Rule{Name: "morning:" + cand.Routine.Name, Severity: loop.Severity(cand.Routine.Severity)},
|
||||
Severity: loop.Severity(cand.Routine.Severity),
|
||||
State: state,
|
||||
},
|
||||
Body: body,
|
||||
Summary: body,
|
||||
}
|
||||
if _, err := t.dispatcher.DispatchNudge(ctx, pn, now); err != nil {
|
||||
log.Printf("tick: dispatch morning routine %s: %v", cand.Routine.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gatherMorningFacts reads the latest fact for every item's fact_key across
|
||||
// all configured morning routines. Shared by fireMorningRoutines (nudge
|
||||
// decision) and morningStatus (read-only query) so the two paths can never
|
||||
// disagree about what evidence exists.
|
||||
func (t *tickLoop) gatherMorningFacts(ctx context.Context) map[string]store.Fact {
|
||||
keys := make(map[string]struct{})
|
||||
for _, r := range t.morningRoutines {
|
||||
for _, it := range r.Items {
|
||||
keys[it.FactKey] = struct{}{}
|
||||
}
|
||||
}
|
||||
facts := make(map[string]store.Fact, len(keys))
|
||||
for k := range keys {
|
||||
f, err := t.store.LatestFact(ctx, k)
|
||||
if err == nil {
|
||||
facts[k] = f
|
||||
continue
|
||||
}
|
||||
if err != store.ErrNoFact {
|
||||
log.Printf("tick: morning: latest fact %s: %v", k, err)
|
||||
}
|
||||
}
|
||||
return facts
|
||||
}
|
||||
|
||||
// morningStatus is the read-only "what's missing" query the web UI (and
|
||||
// eventually a voice query) calls. Pure recompute over the current facts —
|
||||
// no dedupe/nudge-time gating, unlike fireMorningRoutines: this answers
|
||||
// "state right now," not "should we nag."
|
||||
func (t *tickLoop) morningStatus(ctx context.Context, now time.Time) []ipc.MorningRoutineStatus {
|
||||
if len(t.morningRoutines) == 0 {
|
||||
return nil
|
||||
}
|
||||
facts := t.gatherMorningFacts(ctx)
|
||||
out := make([]ipc.MorningRoutineStatus, 0, len(t.morningRoutines))
|
||||
for _, r := range t.morningRoutines {
|
||||
st := morning.Evaluate(r, facts, now)
|
||||
done := make(map[string]bool, len(st.Completed))
|
||||
for _, it := range st.Completed {
|
||||
done[it.Key] = true
|
||||
}
|
||||
items := make([]ipc.MorningRoutineItem, len(r.Items))
|
||||
for i, it := range r.Items {
|
||||
items[i] = ipc.MorningRoutineItem{Key: it.Key, Label: it.Label, Done: done[it.Key]}
|
||||
}
|
||||
out = append(out, ipc.MorningRoutineStatus{
|
||||
Name: r.Name,
|
||||
Active: st.Active,
|
||||
WindowStart: r.WindowStart,
|
||||
WindowEnd: r.WindowEnd,
|
||||
Items: items,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// tune — the feedback auto-tuner's impure step. runs on a slow cadence
|
||||
// (autotuneInterval, see run) so it doesn't write a fact every tick. for each
|
||||
// rule:
|
||||
@@ -440,8 +545,9 @@ func (t *tickLoop) trace() *loop.TickTrace {
|
||||
// daemon's in-memory tick trace cache.
|
||||
type daemonAPI struct {
|
||||
ipc.CoreAPI
|
||||
getTrace func() *loop.TickTrace
|
||||
chatFn func(ctx context.Context, text string) string
|
||||
getTrace func() *loop.TickTrace
|
||||
getMorningStatus func(ctx context.Context) []ipc.MorningRoutineStatus
|
||||
chatFn func(ctx context.Context, text string) string
|
||||
}
|
||||
|
||||
func (d *daemonAPI) Chat(ctx context.Context, text string) (string, error) {
|
||||
@@ -459,6 +565,13 @@ func (d *daemonAPI) TickTrace(ctx context.Context) (ipc.TickTrace, error) {
|
||||
return toIPCTickTrace(*trace), nil
|
||||
}
|
||||
|
||||
func (d *daemonAPI) MorningStatus(ctx context.Context) ([]ipc.MorningRoutineStatus, error) {
|
||||
if d.getMorningStatus == nil {
|
||||
return nil, errors.New("mavend: morning status not available")
|
||||
}
|
||||
return d.getMorningStatus(ctx), nil
|
||||
}
|
||||
|
||||
func toIPCTickTrace(t loop.TickTrace) ipc.TickTrace {
|
||||
rules := make([]ipc.RuleTrace, len(t.RuleTraces))
|
||||
for i, r := range t.RuleTraces {
|
||||
|
||||
Reference in New Issue
Block a user