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:| Setting | Description | Default |
|---|---|---|
| Preferred | The ideal duration for this habit | Required |
| Minimum | Shortest acceptable duration | 50% of preferred (min 15 min) |
| Maximum | Longest beneficial duration | 150% of preferred (max 180 min) |
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 specifiedideal_time (e.g., “07:00”):
2. Time Preference Match
When the slot falls within your preferred time-of-day:| Preference | Time Range |
|---|---|
| Morning | 6:00 AM - 12:00 PM |
| Afternoon | 12:00 PM - 5:00 PM |
| Evening | 5:00 PM - 9:00 PM |
| Night | 9:00 PM - 12:00 AM |
3. Constrained Slot
When the available slot is smaller than your preferred duration:4. No Special Conditions
When no preferences match:Slot Scoring
When multiple time slots are available, the system scores each one to find the best:Examples
Morning Meditation
| Slot | Duration | Reason |
|---|---|---|
| 6:00 AM - 7:00 AM | 30 min | Contains ideal time, maximize |
| 8:00 AM - 9:00 AM | 20 min | Morning preference, use preferred |
| 12:00 PM - 1:00 PM | 20 min | No match, use preferred |
| 6:00 AM - 6:15 AM | 15 min | Constrained, use minimum |
Evening Workout
| Slot | Duration | Reason |
|---|---|---|
| 5:00 PM - 7:00 PM | 60 min | Evening preference, use preferred |
| 8:00 PM - 9:00 PM | 60 min | Evening preference, use preferred |
| 5:00 PM - 5:45 PM | 45 min | Constrained, fit to slot |
| 10:00 AM - 12:00 PM | 60 min | No match, use preferred |
Implementation Details
The duration optimizer is implemented inpackages/ai/src/scheduling/duration-optimizer.ts:
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 is tested with 54 test cases covering:- Duration bounds calculation
- Time matching and preference detection
- Slot scoring and selection
- Integration scenarios for real-world habits