Fire accepted routines every interval, not once (Vikunja #366)
The tick loop now reads accepted routines from the store and nudges when their interval has passed; accepting no longer builds a one-shot reminder. Look at routine.DueAccepted for the schedule rule (no catch-up backlog) and at fireAcceptedRoutines for the restraint gate — routines do not bypass it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGeSZxh1DCtRxmFVSYVGvJ
This commit is contained in:
+61
-3
@@ -186,9 +186,9 @@ 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)
|
||||
|
||||
// TODO(vikunja#366): fire accepted routines here — read
|
||||
// store.ListAcceptedRoutines, pick the ones whose interval has passed, nudge
|
||||
// them through the gate, then MarkRoutineFired.
|
||||
// accepted routines: patterns the user confirmed. read straight from the
|
||||
// store each tick so the schedule survives a restart.
|
||||
t.fireAcceptedRoutines(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
|
||||
@@ -381,6 +381,64 @@ func (t *tickLoop) fireRoutines(ctx context.Context, now time.Time, state loop.S
|
||||
}
|
||||
}
|
||||
|
||||
// fireAcceptedRoutines nudges about the routines the user accepted, once per
|
||||
// interval (Vikunja #366). Accepting used to create a single reminder, so a
|
||||
// non-weekly routine fired once and went quiet forever; the schedule lives in
|
||||
// the proposed_routines row now and the loop re-reads it every tick.
|
||||
//
|
||||
// A routine is a care-class nudge and goes through the restraint gate like any
|
||||
// other: quiet hours, away presence and snooze all suppress it. Reminders bypass
|
||||
// that gate; routines must not. A suppressed nudge is NOT marked fired, so it
|
||||
// goes out on the next tick that the gate allows — one nudge, held, not dropped
|
||||
// and not repeated.
|
||||
//
|
||||
// The body is literal text built from the detected action and object, not
|
||||
// LLM-phrased, so a routine can't hallucinate. It nudges; it never acts.
|
||||
func (t *tickLoop) fireAcceptedRoutines(ctx context.Context, now time.Time, state loop.State) {
|
||||
rows, err := t.store.ListAcceptedRoutines(ctx)
|
||||
if err != nil {
|
||||
log.Printf("tick: list accepted routines: %v", err)
|
||||
return
|
||||
}
|
||||
accepted := make([]routine.Accepted, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
if r.AcceptedTs == nil {
|
||||
continue // accepted before the schedule column existed — no clock to start from.
|
||||
}
|
||||
accepted = append(accepted, routine.Accepted{
|
||||
ID: r.ID,
|
||||
Name: r.Action + " " + r.Object,
|
||||
IntervalDays: r.IntervalDays,
|
||||
Accepted: *r.AcceptedTs,
|
||||
LastFired: r.LastFiredTs,
|
||||
})
|
||||
}
|
||||
|
||||
for _, a := range routine.DueAccepted(accepted, now) {
|
||||
rule := loop.Rule{Name: "routine:" + a.Name, Severity: loop.Sev1}
|
||||
if !loop.Gate(state, rule) {
|
||||
continue
|
||||
}
|
||||
body := "пора: " + a.Name
|
||||
pn := delivery.PhrasedNudge{
|
||||
Candidate: loop.Candidate{Rule: rule, Severity: rule.Severity, State: state},
|
||||
Body: body,
|
||||
Summary: body,
|
||||
}
|
||||
sent, err := t.dispatcher.DispatchNudge(ctx, pn, now)
|
||||
if err != nil {
|
||||
log.Printf("tick: dispatch accepted routine %d: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
if len(sent) == 0 {
|
||||
continue // routing dropped it — leave it due.
|
||||
}
|
||||
if err := t.store.MarkRoutineFired(ctx, a.ID, now); err != nil {
|
||||
log.Printf("tick: mark routine %d fired: %v", a.ID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
@@ -96,6 +96,115 @@ func TestTickFiresRoutineWhenScheduleCrosses(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestTickFiresAcceptedRoutineEveryInterval — Vikunja #366. An accepted routine
|
||||
// with a 3-day interval must nudge every 3 days, not once. It also must not
|
||||
// replay the occurrences it slept through: after a 30-day gap it nudges once.
|
||||
func TestTickFiresAcceptedRoutineEveryInterval(t *testing.T) {
|
||||
st := newTestStore(t)
|
||||
ctx := context.Background()
|
||||
accepted := refNow()
|
||||
|
||||
id, err := st.CreateProposedRoutine(ctx, "полить", "цветы", 3.0, accepted)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateProposedRoutine: %v", err)
|
||||
}
|
||||
if err := st.AcceptProposedRoutine(ctx, id, accepted); err != nil {
|
||||
t.Fatalf("AcceptProposedRoutine: %v", err)
|
||||
}
|
||||
|
||||
sink := &fakeSink{}
|
||||
tl := newTestTickLoop(t, st, sink, nil)
|
||||
const rule = "routine:полить цветы"
|
||||
|
||||
// Same day as the accept: not due yet.
|
||||
markPresent(t, st, ctx, accepted)
|
||||
tl.tick(ctx, accepted.Add(time.Hour))
|
||||
if n := countSends(sink, rule); n != 0 {
|
||||
t.Fatalf("routine fired %d times before its first interval passed, want 0", n)
|
||||
}
|
||||
|
||||
// Three days later: the first nudge.
|
||||
first := accepted.Add(3 * 24 * time.Hour)
|
||||
markPresent(t, st, ctx, first)
|
||||
tl.tick(ctx, first)
|
||||
if n := countSends(sink, rule); n != 1 {
|
||||
t.Fatalf("first interval: sends = %d, want 1", n)
|
||||
}
|
||||
|
||||
// Next day: still inside the interval, silent.
|
||||
sink.sends = nil
|
||||
markPresent(t, st, ctx, first.Add(24*time.Hour))
|
||||
tl.tick(ctx, first.Add(24*time.Hour))
|
||||
if n := countSends(sink, rule); n != 0 {
|
||||
t.Fatalf("mid-interval: sends = %d, want 0", n)
|
||||
}
|
||||
|
||||
// Three days after the first nudge: it fires again. This is the bug —
|
||||
// a one-shot reminder would never come back.
|
||||
second := first.Add(3 * 24 * time.Hour)
|
||||
markPresent(t, st, ctx, second)
|
||||
tl.tick(ctx, second)
|
||||
if n := countSends(sink, rule); n != 1 {
|
||||
t.Fatalf("second interval: sends = %d, want 1 (a routine repeats)", n)
|
||||
}
|
||||
|
||||
// A long silence must not turn into a backlog of missed nudges.
|
||||
sink.sends = nil
|
||||
late := second.Add(30 * 24 * time.Hour)
|
||||
markPresent(t, st, ctx, late)
|
||||
tl.tick(ctx, late)
|
||||
if n := countSends(sink, rule); n != 1 {
|
||||
t.Fatalf("after a 30-day gap: sends = %d, want exactly 1 (no backlog)", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTickAcceptedRoutineRespectsQuietHours — routines are not reminders: they
|
||||
// do not inherit the reminder gate bypass. Away presence drops a care-class
|
||||
// nudge, and the routine stays due so it nudges once the user is back.
|
||||
func TestTickAcceptedRoutineRespectsGate(t *testing.T) {
|
||||
st := newTestStore(t)
|
||||
ctx := context.Background()
|
||||
accepted := refNow()
|
||||
|
||||
id, err := st.CreateProposedRoutine(ctx, "полить", "цветы", 3.0, accepted)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateProposedRoutine: %v", err)
|
||||
}
|
||||
if err := st.AcceptProposedRoutine(ctx, id, accepted); err != nil {
|
||||
t.Fatalf("AcceptProposedRoutine: %v", err)
|
||||
}
|
||||
|
||||
sink := &fakeSink{}
|
||||
tl := newTestTickLoop(t, st, sink, nil)
|
||||
const rule = "routine:полить цветы"
|
||||
|
||||
// No presence probes at all ⇒ away ⇒ the care gate blocks the nudge.
|
||||
due := accepted.Add(3 * 24 * time.Hour)
|
||||
tl.tick(ctx, due)
|
||||
if n := countSends(sink, rule); n != 0 {
|
||||
t.Fatalf("away: sends = %d, want 0 (routine must not bypass the gate)", n)
|
||||
}
|
||||
|
||||
// Back at the desk a minute later: the nudge that was held now goes out.
|
||||
back := due.Add(time.Minute)
|
||||
markPresent(t, st, ctx, back)
|
||||
tl.tick(ctx, back)
|
||||
if n := countSends(sink, rule); n != 1 {
|
||||
t.Fatalf("present again: sends = %d, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
// countSends counts captured sends for one rule name.
|
||||
func countSends(sink *fakeSink, rule string) int {
|
||||
n := 0
|
||||
for _, s := range sink.sends {
|
||||
if s.RuleName == rule {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// refNow — fixed tick time so presence decay + since durations are deterministic.
|
||||
func refNow() time.Time { return time.Date(2026, 6, 30, 12, 0, 0, 0, time.UTC) }
|
||||
|
||||
|
||||
+4
-16
@@ -1414,24 +1414,12 @@ func (h *reactiveHandler) resolveConfirm(ctx context.Context, text string) (stri
|
||||
switch classifyConfirm(text) {
|
||||
case confirmYes:
|
||||
h.pendingRoutine = nil
|
||||
// Create a recurring reminder at the detected interval.
|
||||
// Weekly patterns get a cron expression; arbitrary intervals
|
||||
// fire once and the detector re-proposes on the next cycle.
|
||||
intervalDur := time.Duration(pr.interval * 24 * float64(time.Hour))
|
||||
fire := h.now().Add(intervalDur)
|
||||
cron := ""
|
||||
if pr.interval >= 6.5 && pr.interval <= 7.5 {
|
||||
cron = fmt.Sprintf("0 %d * * %d", fire.Hour(), int(fire.Weekday()))
|
||||
}
|
||||
payload := fmt.Sprintf(`{"text":"%s %s"}`, pr.action, pr.object)
|
||||
remID, err := h.api.CreateReminder(ctx, fire, payload, cron)
|
||||
if err != nil {
|
||||
log.Printf("voice: create routine reminder: %v", err)
|
||||
return "не получилось поставить напоминание.", true
|
||||
}
|
||||
_ = remID // TODO(vikunja#366): stop creating a reminder here; the tick loop fires accepted routines.
|
||||
// Only record the acceptance. The tick loop reads accepted
|
||||
// routines and nudges on their own interval. Building a reminder
|
||||
// here made a routine fire exactly once (Vikunja #366).
|
||||
if err := h.dataStore.AcceptProposedRoutine(ctx, pr.routineID, h.now()); err != nil {
|
||||
log.Printf("voice: accept proposed routine: %v", err)
|
||||
return "не получилось запомнить рутину.", true
|
||||
}
|
||||
return "буду напоминать.", true
|
||||
case confirmNo:
|
||||
|
||||
@@ -55,6 +55,47 @@ func Validate(routines []Routine) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Accepted — an accepted routine proposal as the tick driver sees it. This is a
|
||||
// different shape from Routine: the schedule is a plain interval the pattern
|
||||
// detector measured, not an operator-written cron expression. Accepted is when
|
||||
// the human said yes; LastFired is nil until the first nudge.
|
||||
type Accepted struct {
|
||||
ID int64
|
||||
Name string
|
||||
IntervalDays float64
|
||||
Accepted time.Time
|
||||
LastFired *time.Time
|
||||
}
|
||||
|
||||
// DueAccepted returns the accepted routines whose interval has passed. It does
|
||||
// not mutate anything — the caller persists the new last-fired time, because
|
||||
// that has to survive a restart (unlike Due's in-memory map).
|
||||
//
|
||||
// The clock starts at LastFired, or at Accepted for a routine that has never
|
||||
// nudged. A routine with a non-positive interval never fires: a bad interval
|
||||
// should mean silence, not a nudge every tick.
|
||||
//
|
||||
// One occurrence per call, no catch-up: the caller stamps the fire time as now,
|
||||
// so a routine that was silent for a month nudges once and then waits a full
|
||||
// interval. Never a backlog.
|
||||
func DueAccepted(rs []Accepted, now time.Time) []Accepted {
|
||||
var out []Accepted
|
||||
for _, r := range rs {
|
||||
if r.IntervalDays <= 0 {
|
||||
continue
|
||||
}
|
||||
since := r.Accepted
|
||||
if r.LastFired != nil {
|
||||
since = *r.LastFired
|
||||
}
|
||||
gap := time.Duration(r.IntervalDays * 24 * float64(time.Hour))
|
||||
if !now.Before(since.Add(gap)) {
|
||||
out = append(out, r)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Due returns the routines whose schedule crossed since their last fire and
|
||||
// records now as the new last-fire time for each one returned. The caller owns
|
||||
// `last` (the tick driver holds it across ticks); Due mutates it in place.
|
||||
|
||||
@@ -5,6 +5,40 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDueAcceptedFiresOncePerInterval(t *testing.T) {
|
||||
accepted := time.Date(2026, 7, 1, 9, 0, 0, 0, time.UTC)
|
||||
fired := accepted.Add(3 * 24 * time.Hour)
|
||||
rs := []Accepted{
|
||||
{ID: 1, Name: "полить цветы", IntervalDays: 3, Accepted: accepted},
|
||||
{ID: 2, Name: "покормить рыб", IntervalDays: 3, Accepted: accepted, LastFired: &fired},
|
||||
{ID: 3, Name: "битый интервал", IntervalDays: 0, Accepted: accepted},
|
||||
}
|
||||
|
||||
// One day in: nothing has waited a full interval.
|
||||
if got := DueAccepted(rs, accepted.Add(24*time.Hour)); len(got) != 0 {
|
||||
t.Fatalf("want nothing due after 1 day, got %+v", got)
|
||||
}
|
||||
|
||||
// Three days in: the never-fired one is due. The one that already fired at
|
||||
// day 3 starts its next three days from there. A zero interval never fires.
|
||||
got := DueAccepted(rs, fired)
|
||||
if len(got) != 1 || got[0].ID != 1 {
|
||||
t.Fatalf("want only routine 1 due at day 3, got %+v", got)
|
||||
}
|
||||
|
||||
// Six days in: both real routines are due.
|
||||
if got := DueAccepted(rs, accepted.Add(6*24*time.Hour)); len(got) != 2 {
|
||||
t.Fatalf("want both routines due at day 6, got %+v", got)
|
||||
}
|
||||
|
||||
// A month later the zero-interval routine is still silent.
|
||||
for _, r := range DueAccepted(rs, accepted.Add(30*24*time.Hour)) {
|
||||
if r.ID == 3 {
|
||||
t.Fatal("a routine with a zero interval must never fire")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
ok := []Routine{{Name: "morning", Cron: "0 8 * * *", Body: "доброе утро"}}
|
||||
if err := Validate(ok); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user