Fix adaptive medication timing and update README
- Fix double notifications: remove redundant check_medication_reminders() call, use adaptive path as primary with basic as fallback - Fix nag firing immediately: require nag_interval minutes after scheduled dose time before first nag - Fix missing schedules: create on-demand if midnight window was missed - Fix wrong timezone: use user_now_for() instead of request-context user_now() in calculate_adjusted_times() - Fix immutable schedules: recalculate pending schedules on wake event detection so adaptive timing actually adapts - Fix take/skip not updating schedule: API endpoints now call mark_med_taken/skipped so nags stop after logging a dose - Fix skipped doses still triggering reminders: check both taken and skipped in adaptive reminder and log queries - Update README with tasks, AI step generation, auth refresh tokens, knowledge base improvements, and current architecture Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -263,18 +263,37 @@ def check_adaptive_medication_reminders():
|
||||
else:
|
||||
continue
|
||||
|
||||
# Get today's schedule
|
||||
# Get today's schedule (any status — we filter below)
|
||||
schedules = postgres.select(
|
||||
"medication_schedules",
|
||||
where={
|
||||
"user_uuid": user_uuid,
|
||||
"medication_id": med["id"],
|
||||
"adjustment_date": today,
|
||||
"status": "pending",
|
||||
},
|
||||
)
|
||||
|
||||
# If no schedules exist yet, create them on demand
|
||||
if not schedules:
|
||||
times = med.get("times", [])
|
||||
if times:
|
||||
try:
|
||||
adaptive_meds.create_daily_schedule(user_uuid, med["id"], times)
|
||||
schedules = postgres.select(
|
||||
"medication_schedules",
|
||||
where={
|
||||
"user_uuid": user_uuid,
|
||||
"medication_id": med["id"],
|
||||
"adjustment_date": today,
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not create on-demand schedule for {med['id']}: {e}")
|
||||
|
||||
for sched in schedules:
|
||||
# Skip already-taken or skipped schedules
|
||||
if sched.get("status") in ("taken", "skipped"):
|
||||
continue
|
||||
# Check if it's time to take this med
|
||||
if adaptive_enabled:
|
||||
# Use adjusted time
|
||||
@@ -292,24 +311,24 @@ def check_adaptive_medication_reminders():
|
||||
if check_time != current_time:
|
||||
continue
|
||||
|
||||
# Check if already taken for this specific time slot today
|
||||
# Check if already taken or skipped for this time slot today
|
||||
logs = postgres.select(
|
||||
"med_logs",
|
||||
where={
|
||||
"medication_id": med["id"],
|
||||
"user_uuid": user_uuid,
|
||||
"action": "taken",
|
||||
},
|
||||
)
|
||||
|
||||
already_taken = any(
|
||||
log.get("scheduled_time") == check_time
|
||||
already_handled = any(
|
||||
log.get("action") in ("taken", "skipped")
|
||||
and log.get("scheduled_time") == check_time
|
||||
and _utc_to_local_date(log.get("created_at"), user_tz)
|
||||
== today.isoformat()
|
||||
for log in logs
|
||||
)
|
||||
|
||||
if already_taken:
|
||||
if already_handled:
|
||||
continue
|
||||
|
||||
# Send notification
|
||||
@@ -642,13 +661,14 @@ def poll_callback():
|
||||
# Create daily schedules per-user at their local midnight
|
||||
_check_per_user_midnight_schedules()
|
||||
|
||||
# Check reminders - use both original and adaptive checks
|
||||
# Check medication reminders (adaptive path handles both adaptive and non-adaptive)
|
||||
logger.info("Checking medication reminders")
|
||||
check_medication_reminders()
|
||||
try:
|
||||
check_adaptive_medication_reminders()
|
||||
except Exception as e:
|
||||
logger.warning(f"Adaptive medication reminder check failed: {e}")
|
||||
# Fall back to basic reminders if adaptive check fails entirely
|
||||
check_medication_reminders()
|
||||
|
||||
# Check for nags - log as error to help with debugging
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user