Skip to content

refact: reduce AI coach token use + decouple routine context from creation#24

Merged
joaoferrete merged 1 commit into
mainfrom
jf-refact/ai-token-economy
Jun 17, 2026
Merged

refact: reduce AI coach token use + decouple routine context from creation#24
joaoferrete merged 1 commit into
mainfrom
jf-refact/ai-token-economy

Conversation

@joaoferrete

Copy link
Copy Markdown
Owner

What changed

  • Cut AI token use: structured JSON output (no raw-JSON dump), gate the exercise-library/saved-routines context blocks behind include_routine, defer exercise benefits to push time, and cache the report context in the system block.
  • Decouple including existing routines in context (default on, configurable in Settings → AI via ai_include_routines) from creating a new routine (always explicit — report confirm / chat tool only when asked); plus a 7-day auto report at startup (analysis only).
  • Fix chat input: pass the prompt into input() with ANSI escapes wrapped in readline \001/\002 markers, fixing line-wrap overwrite and backspace eating into the "Você"/"You" prompt.

Why

Reduce token spend on the AI coach without losing input data or insight quality, make routine creation a deliberate user action, and fix a long-standing chat input glitch on line wrap/backspace.

How to test

  1. pytest tests/ -v — new tests cover context gating (tests/test_coach.py), complete_json (tests/test_provider.py), and auto-report timing (tests/test_db_goals.py).
  2. Run the app → Coach: confirm the raw JSON no longer appears; answer "no" to the routine prompt and verify only the analysis is shown.
  3. Toggle Settings → AI → Include saved routines in context and Auto report every 7 days; confirm both persist.
  4. Open Chat, type a long line that wraps and hold backspace — text no longer overwrites and the Você: prompt is protected.

Type of change

  • Bug fix
  • New feature / integration
  • Refactor
  • Documentation
  • Security fix

Checklist

  • pytest tests/ -v passes locally
  • ruff check . --ignore E501,E402,F401 --exclude tests/ passes
  • Tests added or updated for any logic touching the DB or analytics
  • No sensitive files committed (.env, *.db, fit_token.json, fit_credentials.json)
  • No new dependencies added without justification in the PR description

🤖 Generated with Claude Code

…ation

Token-economy and UX refactor for the AI coaching report and chat:

- Report no longer streams raw JSON to the screen; uses structured JSON
  output (complete_json) per provider instead of free-text json.loads.
- Gate the exercise library + saved routines context blocks behind
  include_routine in _build_context (saves input tokens when unused).
- Defer exercise "benefits" generation to push time (_generate_benefits)
  instead of producing them in every report.
- Move report context into the cacheable system block (prompt caching).
- Decouple "include existing routines in context" (default on, configurable
  via ai_include_routines in Settings → AI) from "create a new routine"
  (always explicit: report confirm / chat tool only when asked).
- Auto-generate a coaching report every 7 days at startup (analysis only;
  auto_report pref + report_last_generated timestamp).
- Fix chat input: pass the prompt to input() with ANSI escapes wrapped in
  readline \001/\002 markers, fixing line-wrap overwrite and backspace
  eating into the "Você"/"You" prompt.

Adds tests for context gating, complete_json, and auto-report timing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@joaoferrete joaoferrete self-assigned this Jun 17, 2026
@joaoferrete joaoferrete merged commit 6c08822 into main Jun 17, 2026
5 checks passed
@joaoferrete joaoferrete deleted the jf-refact/ai-token-economy branch June 23, 2026 18:32
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.

1 participant