Skip to main content

Duration Optimization

Duration optimization is a key feature that makes habit scheduling intelligent. Instead of always using a fixed duration, the system adapts based on the available time slot and your preferences.

Flexible Duration Bounds

Each habit can have three duration settings:
If you only set the preferred duration, the system calculates sensible defaults for min and max.

Optimization Strategy

The duration optimizer chooses the optimal duration based on slot characteristics:

1. Ideal Time Match

When the slot contains your specified ideal_time (e.g., “07:00”):
The system maximizes duration because you’re getting your most valuable time slot. This is when you’ll get the most benefit from the habit.

2. Time Preference Match

When the slot falls within your preferred time-of-day:
The system uses your standard preferred duration since this is still a good time for the habit.

3. Constrained Slot

When the available slot is smaller than your preferred duration:
The system shrinks to fit while ensuring you still get value from the habit.

4. No Special Conditions

When no preferences match:
Falls back to your standard preferred duration.

Slot Scoring

When multiple time slots are available, the system scores each one to find the best:

Examples

Morning Meditation

Evening Workout

Implementation Details

The duration optimizer is implemented in packages/ai/src/scheduling/duration-optimizer.ts:
Most matching and scoring functions accept an optional timezone so that ideal_time and time-of-day preferences are interpreted in the user’s local time rather than the server’s. scoreSlotForHabit and findBestSlotForHabit also accept optional SchedulingWeights to override the default ideal-time and preference bonuses.

Configuration in Habit Form

When creating or editing a habit, you can configure duration optimization:
1

Set Preferred Duration

The standard duration for this habit (required)
2

Enable Flexible Duration

Toggle to allow min/max bounds
3

Set Minimum Duration

Shortest acceptable duration (optional)
4

Set Maximum Duration

Longest beneficial duration (optional)
5

Set Ideal Time

Specific time of day (optional, format: HH:MM)
6

Set Time Preference

Morning, Afternoon, Evening, or Night (optional)

Testing

The duration optimizer ships with an extensive unit-test suite (currently 127 cases) covering:
  • Duration bounds calculation
  • Time matching and preference detection (including timezone-aware extraction)
  • Slot scoring and selection
  • Integration scenarios for real-world habits
Run tests with: