refact: reduce AI coach token use + decouple routine context from creation#24
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
include_routine, defer exercisebenefitsto push time, and cache the report context in the system block.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).input()with ANSI escapes wrapped in readline\001/\002markers, 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
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).Você:prompt is protected.Type of change
Checklist
pytest tests/ -vpasses locallyruff check . --ignore E501,E402,F401 --exclude tests/passes.env,*.db,fit_token.json,fit_credentials.json)🤖 Generated with Claude Code