Skip to content

Commit 5934ca6

Browse files
committed
fix(plugin): make status bar actor badge fall back to hud_state.activeAgent (#1333)
1 parent 1032f5d commit 5934ca6

2 files changed

Lines changed: 60 additions & 10 deletions

File tree

packages/claude-code-plugin/hooks/codingbuddy-hud.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Telemetry fallback order per field:
88
cost → stdin cost.total_cost_usd > estimate_cost()
99
duration → stdin cost.total_duration_ms > hud-state sessionStartTimestamp
10-
agent → stdin agent.name > CODINGBUDDY_ACTIVE_AGENT env
10+
agent → stdin agent.name > hud_state.activeAgent > CODINGBUDDY_ACTIVE_AGENT env
1111
model → stdin model.display_name > model.id
1212
"""
1313
import json
@@ -220,10 +220,13 @@ def resolve_duration(stdin_data: dict, hud_state: dict) -> str:
220220
return "0m"
221221

222222

223-
def resolve_agent(stdin_data: dict, env_agent: str = "") -> str:
224-
"""Resolve agent: stdin > env var."""
223+
def resolve_agent(stdin_data: dict, hud_state=None, env_agent: str = "") -> str:
224+
"""Resolve agent: stdin > hud_state.activeAgent > env var."""
225225
stdin_agent = (stdin_data.get("agent") or {}).get("name", "")
226-
return stdin_agent or env_agent
226+
if stdin_agent:
227+
return stdin_agent
228+
hud_agent = (hud_state or {}).get("activeAgent", "")
229+
return hud_agent or env_agent
227230

228231

229232
def resolve_model_label(stdin_data: dict) -> tuple:
@@ -336,7 +339,7 @@ def format_status_line(
336339
Fallback order per field:
337340
cost → stdin cost.total_cost_usd > estimate_cost()
338341
duration → stdin cost.total_duration_ms > hud-state sessionStartTimestamp
339-
agent → stdin agent.name > active_agent param
342+
agent → stdin agent.name > hud_state.activeAgent > active_agent param
340343
model → stdin model.display_name > model.id
341344
"""
342345
version = hud_state.get("version", "")
@@ -351,7 +354,7 @@ def format_status_line(
351354
cost, is_exact = resolve_cost(stdin_data, model_id, ctx_window)
352355
duration = resolve_duration(stdin_data, hud_state)
353356
cache = compute_cache_hit_rate(ctx_window)
354-
agent = resolve_agent(stdin_data, active_agent)
357+
agent = resolve_agent(stdin_data, hud_state, active_agent)
355358

356359
cost_prefix = "$" if is_exact else "~$"
357360

packages/claude-code-plugin/tests/test_hud.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,34 @@ def test_no_data_returns_zero(self):
229229
class TestResolveAgent:
230230
def test_stdin_agent_preferred(self):
231231
stdin = {"agent": {"name": "security-reviewer"}}
232-
assert hud.resolve_agent(stdin, "old-env-agent") == "security-reviewer"
232+
assert hud.resolve_agent(stdin, env_agent="old-env-agent") == "security-reviewer"
233+
234+
def test_stdin_overrides_hud_state(self):
235+
stdin = {"agent": {"name": "security-reviewer"}}
236+
state = {"activeAgent": "plan-mode"}
237+
assert hud.resolve_agent(stdin, state, "env-agent") == "security-reviewer"
238+
239+
def test_hud_state_fallback(self):
240+
assert hud.resolve_agent({}, {"activeAgent": "security-specialist"}) == "security-specialist"
241+
242+
def test_hud_state_overrides_env(self):
243+
state = {"activeAgent": "security-specialist"}
244+
assert hud.resolve_agent({}, state, "env-agent") == "security-specialist"
233245

234246
def test_fallback_to_env(self):
235-
assert hud.resolve_agent({}, "env-agent") == "env-agent"
247+
assert hud.resolve_agent({}, env_agent="env-agent") == "env-agent"
248+
249+
def test_env_when_hud_state_empty(self):
250+
assert hud.resolve_agent({}, {"activeAgent": ""}, "env-agent") == "env-agent"
251+
252+
def test_env_when_hud_state_none(self):
253+
assert hud.resolve_agent({}, None, "env-agent") == "env-agent"
236254

237-
def test_both_empty(self):
238-
assert hud.resolve_agent({}, "") == ""
255+
def test_all_empty(self):
256+
assert hud.resolve_agent({}, {}, "") == ""
257+
258+
def test_both_empty_no_hud(self):
259+
assert hud.resolve_agent({}, None, "") == ""
239260

240261

241262
class TestResolveModelLabel:
@@ -359,6 +380,32 @@ def test_stdin_agent_overrides_env_badge(self):
359380
assert "[\u2605 fron]" in result # [★ fron]
360381
assert "\u25d0" not in result # ◐ (backend glyph) absent
361382

383+
def test_hud_state_agent_fallback_badge(self):
384+
result = hud.format_status_line(
385+
{},
386+
{"activeAgent": "security-specialist", "focus": "auth", "blockerCount": 1},
387+
)
388+
lines = result.strip().split("\n")
389+
assert len(lines) == 2
390+
assert "[\u25ee secu]" in lines[1] # [◮ secu]
391+
assert "[auth]" in lines[1]
392+
assert "[\u26a01]" in lines[1] # [⚠1]
393+
394+
def test_stdin_agent_overrides_hud_state(self):
395+
stdin = {"agent": {"name": "frontend-developer"}}
396+
state = {"activeAgent": "security-specialist"}
397+
result = hud.format_status_line(stdin, state)
398+
assert "[\u2605 fron]" in result # [★ fron]
399+
assert "\u25ee" not in result # ◮ (security glyph) absent
400+
401+
def test_hud_state_agent_overrides_env(self):
402+
state = {"activeAgent": "security-specialist"}
403+
result = hud.format_status_line({}, state, active_agent="backend-developer")
404+
lines = result.strip().split("\n")
405+
assert len(lines) == 2
406+
assert "[\u25ee secu]" in lines[1] # [◮ secu] from hud_state
407+
assert "\u25d0" not in result # ◐ (backend glyph) absent
408+
362409
def test_no_agent_single_line(self):
363410
result = hud.format_status_line({}, {"version": "5.1.1"})
364411
assert "\n" not in result

0 commit comments

Comments
 (0)