← All posts
Postmortems
1 min read

The habit that said zero weeks after ten ticked days

A daily habit started on a Thursday showed a 0-week streak after ten straight days. The first week had been judged against seven days when only four existed.


A screenshot came back from real use: Seven hours’ sleep, ticked every day for ten days, streak 0 wk, best 0.

Why

Streaks in PaceStreak are weekly: a week is kept when you hit your target that week. Sleep had a target of every day, seven of seven. It was started on a Thursday, so the first week had only four days left in it. The engine asked that week for seven, got four, and marked it missed. The second week was still in progress, so the streak read zero.

That’s correct by the letter of the rule and wrong by any reasonable reading of it. Nobody who starts a daily habit on a Thursday has failed their first week.

The fix

The first week asks for no more than the days that were left:

def first_week_target(weekly_target, started_on, week):
    if week <= started_on < week + timedelta(days=7):
        return max(1, min(weekly_target, 7 - (started_on - week).days))
    return weekly_target

Three days a week, started on Thursday: still three, because three fit. Every day, started on Thursday: four. Every later week asks for the full target.

The part I got wrong first

The test I wrote expected a streak of one after eleven daily ticks from a Thursday. It got two. The engine was right: eleven days from Thursday runs through the whole next week, which is kept too. The fix was to the test, and the habit page now says so in words: the first week only asks for the days left after you started.

AlzyWelzy

Building PaceStreak. GitHub

Related

  • Design

    Strength: a kinder number beside the streak

    Every habit has a weekly streak and a strength score. The first version of strength was a rolling daily average that punished the start of every week. It's now a slow average over weeks.

    1 min
  • Product

    A routine is only an order

    Habit routines and the focus timer shipped without touching the streak engine. Why a routine stores nothing but a list of ids, and why the timer stores a start time instead of counting.

    2 min

Build the habit. Keep the week.

PaceStreak is a habit and streak tracker for training and everything else worth doing regularly.