Skip to content

Commit 2f153b2

Browse files
timvisher-ddclaude
andcommitted
Add usage tests and defend against ACP used > size bug
Add comprehensive ERT tests for agent-shell-usage.el covering notification updates, context indicator scaling/colors, compaction replay, token saving, and number formatting. The ACP server has a bug where model switches cause used to exceed size in session/update notifications. Rather than clamping, signal unreliable data: indicator shows ? with warning face, format shows (?) instead of a bogus percentage. A regression test replays real observed traffic from the Opus 1M -> Sonnet 200k switch scenario. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ff93549 commit 2f153b2

2 files changed

Lines changed: 362 additions & 25 deletions

File tree

agent-shell-usage.el

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ When MULTILINE is non-nil, format as right-aligned labeled rows."
145145
(if (> (or (map-elt usage :context-size) 0) 0)
146146
(agent-shell--format-number-compact (or (map-elt usage :context-size) 0))
147147
"?")
148-
(if (and (map-elt usage :context-size)
149-
(> (map-elt usage :context-size) 0))
150-
(format " (%.1f%%)" (* 100.0 (/ (float (or (map-elt usage :context-used) 0))
151-
(map-elt usage :context-size))))
152-
"")))
148+
(let ((used (or (map-elt usage :context-used) 0))
149+
(size (or (map-elt usage :context-size) 0)))
150+
(cond
151+
((< size used) " (?)")
152+
((< 0 size) (format " (%.1f%%)" (* 100.0 (/ (float used) size))))
153+
(t "")))))
153154
(total
154155
(let ((n (or (map-elt usage :total-tokens) 0)))
155156
(if (> n 0)
@@ -201,26 +202,30 @@ Only returns an indicator if enabled and usage data is available."
201202
(context-used (map-elt usage :context-used))
202203
(context-size (map-elt usage :context-size))
203204
((> context-size 0)))
204-
(let* ((percentage (/ (* 100.0 context-used) context-size))
205-
;; Unicode vertical block characters from empty to full
206-
(indicator (cond
207-
((>= percentage 100) "") ; Full
208-
((>= percentage 87.5) "")
209-
((>= percentage 75) "")
210-
((>= percentage 62.5) "")
211-
((>= percentage 50) "")
212-
((>= percentage 37.5) "")
213-
((>= percentage 25) "")
214-
((> percentage 0) "")
215-
(t nil))) ; Return nil for no usage
216-
(face (cond
217-
((>= percentage 85) 'error) ; Red for critical
218-
((>= percentage 60) 'warning) ; Yellow/orange for warning
219-
(t 'success)))) ; Green for normal
220-
(when indicator
221-
(propertize indicator
222-
'face face
223-
'help-echo (agent-shell--format-usage usage))))))
205+
(if (< context-size context-used)
206+
(propertize "?"
207+
'face 'warning
208+
'help-echo (agent-shell--format-usage usage))
209+
(let* ((percentage (/ (* 100.0 context-used) context-size))
210+
;; Unicode vertical block characters from empty to full
211+
(indicator (cond
212+
((>= percentage 100) "") ; Full
213+
((>= percentage 87.5) "")
214+
((>= percentage 75) "")
215+
((>= percentage 62.5) "")
216+
((>= percentage 50) "")
217+
((>= percentage 37.5) "")
218+
((>= percentage 25) "")
219+
((> percentage 0) "")
220+
(t nil))) ; Return nil for no usage
221+
(face (cond
222+
((>= percentage 85) 'error) ; Red for critical
223+
((>= percentage 60) 'warning) ; Yellow/orange for warning
224+
(t 'success)))) ; Green for normal
225+
(when indicator
226+
(propertize indicator
227+
'face face
228+
'help-echo (agent-shell--format-usage usage)))))))
224229

225230
(provide 'agent-shell-usage)
226231
;;; agent-shell-usage.el ends here

0 commit comments

Comments
 (0)