feat(health): add caffeine metabolic half-life and bedtime sleep cutoff calculator (#616) by Aryan9891 · Pull Request #626 · TheVicky1/Pact_OS · GitHub
Skip to content

feat(health): add caffeine metabolic half-life and bedtime sleep cutoff calculator (#616) - #626

Merged
TheVicky1 merged 1 commit into
TheVicky1:mainfrom
Aryan9891:feat/health-caffeine-cutoff
Oct 19, 2026
Merged

TheVicky1 merged 1 commit into
TheVicky1:mainfrom
Aryan9891:feat/health-caffeine-cutoff

Conversation

@Aryan9891

Copy link
Copy Markdown
Contributor

Summary

Implements an evidence-based pharmacokinetic caffeine clearance calculator in src/lib/health/caffeine-cutoff.ts to compute remaining active caffeine at bedtime, estimate sleep onset latency risk, and calculate daily personalized caffeine cutoff recommendations based on user sleep schedules.

Key Features

  • Exponential Metabolic Decay Engine:
    • calculateRemainingCaffeine(doses, targetTimeIso, halfLifeHours?): Uses exponential clearance $C(t) = C_0 \cdot (0.5)^{\Delta t / t_{1/2}}$ across multiple doses throughout the day.
    • Automatically ignores doses consumed in the future relative to assessment time.
  • Bedtime Sleep Cutoff Calculator:
    • calculateCaffeineCutoffTime(targetBedtimeIso, plannedDoseMg, options?): Derives exact cutoff timestamps backwards from bedtime using required half-lives $\Delta t = t_{1/2} \cdot \log_2(\text{dose} / \text{threshold})$. (e.g., 200mg coffee before 23:00 bedtime -> 08:00 AM cutoff).
  • Sleep Disruption Risk Evaluator:
    • <= 25.0 mg -> 'minimal'
    • 25.1 mg to 60.0 mg -> 'moderate'
    • > 60.0 mg -> 'high'
  • Daily Clearance Report Generator:
    • generateCaffeineClearanceReport(doses, targetBedtimeIso, options?): Aggregates total intake, bedtime residual, disruption risk, hours to safe clearance, and last intake recommendations.
  • Pure Implementation: Zero external dependencies, pure TypeScript math and ISO 8601 validation.

Verification & Test Results

  • Added tests/caffeine-cutoff.test.ts with 9 authoritative unit tests.
  • node scratch/run-tests.mjs caffeine-cutoff.test.ts -> 9 passed, 0 failed.
  • npm run typecheck -> 0 errors.

Resolves #616

@Aryan9891
Aryan9891 requested a review from TheVicky1 as a code owner October 18, 2026 20:23
@vercel

vercel Bot commented Oct 18, 2026

Copy link
Copy Markdown

@TheVicky1
TheVicky1 merged commit b2d1ba6 into TheVicky1:main Oct 19, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Health] Add Caffeine Metabolic Half-Life & Bedtime Sleep Cutoff Calculator in src/lib/health/caffeine-cutoff.ts

2 participants