🔧 Fix: Robustere Token-Berechnung für Budget-Check

Problem:
- check_token_budget() hatte Probleme mit inkonsistenten Log-Formaten
- Manche Logs haben usage als String (escaped JSON)
- Andere haben usage als Objekt
- grep-Pattern war zu strikt (\\"$today\\" fand nichts)

Lösung:
- Grep ohne escaped quotes (grep "$today" statt grep \"\\"$today\\"\")
- jq-Abfrage unterstützt BEIDE Formate:
  * usage als String → fromjson → total_tokens
  * usage als Objekt → .total_tokens
- Validierung: Nur valide Zahlen werden addiert
- awk summiert alle Tokens pro Log-Datei

Test-Ergebnis:
 Budget = 0 (unbegrenzt) → Erlaubt
 Budget = 100, Verbrauch = 150 → Blockiert mit Nachricht
 Budget = 1000, Verbrauch = 50 → Erlaubt

Kinderfreundliche Blockierungs-Nachricht:
"Liebes Kind, heute hast du schon X Tokens verwendet.
 💚 Jede Frage ist wertvoll - aber auch Pausen sind wichtig."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Branko May Trinkwald
2025-12-22 00:06:44 +01:00
parent 4005bb9b18
commit 223fc5f37d

View File

@@ -630,9 +630,22 @@ function check_token_budget() {
for token_file in "${CRUMB_LOGS_DIR}"/*/token_log.json; do
if [[ -f "$token_file" ]]; then
# Summiere nur Tokens von heute
local tokens=$(jq -r --arg today "$today" '[.[] | select(.zeit | startswith($today)) | .usage.total_tokens // 0] | add // 0' "$token_file" 2>/dev/null || echo 0)
total_today=$((total_today + tokens))
# Summiere nur Tokens von heute (robuster Ansatz für verschiedene Log-Formate)
local tokens=$(grep "$today" "$token_file" 2>/dev/null | grep -v '^\[\]$' | while read line; do
# Versuche total_tokens zu extrahieren (unterstützt usage als String oder Objekt)
echo "$line" | jq -r '
if .usage | type == "string" then
.usage | fromjson | .total_tokens // 0
else
.usage.total_tokens // 0
end
' 2>/dev/null || echo 0
done | awk '{sum+=$1} END {print sum+0}')
# Nur addieren wenn tokens eine gültige Zahl ist
if [[ "$tokens" =~ ^[0-9]+$ ]]; then
total_today=$((total_today + tokens))
fi
fi
done