Skip to content

Commit 5cc9905

Browse files
author
shahinyanm
committed
Merge fix/statusline-savings-clarity: label savings + efficiency % in the badge
2 parents 3548835 + 7490697 commit 5cc9905

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

hooks/tp-statusline.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ sum_log() {
6464
if (match($0, /"savedTokens"[[:space:]]*:[[:space:]]*-?[0-9]+/)) {
6565
t = substr($0, RSTART, RLENGTH); gsub(/[^0-9-]/, "", t); total += t + 0
6666
}
67+
} else if (mode == "wouldbe") {
68+
# Raw-equivalent tokens the structural reads stood in for (the baseline
69+
# the savings % is measured against): sum of tokensWouldBe.
70+
if (match($0, /"tokensWouldBe"[[:space:]]*:[[:space:]]*-?[0-9]+/)) {
71+
t = substr($0, RSTART, RLENGTH); gsub(/[^0-9-]/, "", t); total += t + 0
72+
}
6773
} else {
6874
rw = 0; rt = 0
6975
if (match($0, /"tokensWouldBe"[[:space:]]*:[[:space:]]*-?[0-9]+/)) {
@@ -165,13 +171,25 @@ if [ -n "$PROJECT_ROOT" ]; then
165171
SESSION_TOTAL=$(($(sum_log "$EVENTS_FILE" saved "$SESSION_ID") + $(sum_log "$TOOLS_FILE" delta "$SESSION_ID")))
166172
fi
167173

174+
# Efficiency of the structural reads: saved ÷ would-be-raw (tool-calls — the
175+
# source with a real baseline). Shown as a % so the savings number reads as a
176+
# RATIO of what those reads WOULD have cost, not a fraction of the whole context.
177+
WOULDBE=$(sum_log "$TOOLS_FILE" wouldbe "")
178+
TOOLS_SAVED=$(sum_log "$TOOLS_FILE" delta "")
179+
EFF=""
180+
if [ "$WOULDBE" -gt 0 ] 2>/dev/null; then
181+
EFF=" $((TOOLS_SAVED * 100 / WOULDBE))%"
182+
fi
183+
184+
# Label the cumulative number "saved" + append the efficiency %, so it can't be
185+
# misread as "saved / total-context". `s:` is the live per-session figure.
168186
if [ "$SESSION_TOTAL" -gt 0 ] 2>/dev/null && [ "$PROJECT_TOTAL" -gt 0 ] 2>/dev/null; then
169-
SAVED_SUFFIX=" s:$(fmt_tokens "$SESSION_TOTAL") · $(fmt_tokens "$PROJECT_TOTAL")"
187+
SAVED_SUFFIX=" s:$(fmt_tokens "$SESSION_TOTAL") · saved $(fmt_tokens "$PROJECT_TOTAL")$EFF"
170188
elif [ "$PROJECT_TOTAL" -gt 0 ] 2>/dev/null; then
171189
# Fresh session, nothing saved yet — show the project total alone.
172-
SAVED_SUFFIX=" $(fmt_tokens "$PROJECT_TOTAL")"
190+
SAVED_SUFFIX=" saved $(fmt_tokens "$PROJECT_TOTAL")$EFF"
173191
elif [ "$SESSION_TOTAL" -gt 0 ] 2>/dev/null; then
174-
SAVED_SUFFIX=" s:$(fmt_tokens "$SESSION_TOTAL")"
192+
SAVED_SUFFIX=" s:$(fmt_tokens "$SESSION_TOTAL") saved"
175193
fi
176194
fi
177195

0 commit comments

Comments
 (0)