diff options
| -rwxr-xr-x | agent-lamp | 71 |
1 files changed, 71 insertions, 0 deletions
@@ -38,6 +38,17 @@ COLOURS = {"permission": "#a000ff", "question": "#ffa000", "error": "#ff0000", "complete": "#00ff40"} CLEARS = ("clear", "user_message", "user_cancelled") +# Highest first: the order a multi-colour cycle walks. +PRIORITY = ("permission", "question", "error", "complete") +WAITING = ("permission", "question") + +# Calibration. Brightness 0..254. Times in seconds. +BRIGHT = 254 +DIM = 60 +ESCALATE = 120 # a wait this long starts to breathe +BREATHE = 2 # each half of a breath +CYCLE = 4 # each colour of a multi-colour cycle + def word(w): """The state a CLI or opencode-notifier event word means, or None.""" @@ -62,6 +73,34 @@ def from_claude(payload): return (state, payload["session_id"]) if state else None +def apply(sessions, session, state, now): + """Record one event in sessions, in place. True if anything changed.""" + if state == "reset": + changed = bool(sessions) + sessions.clear() + return changed + if state == "clear": + return sessions.pop(session, None) is not None + if sessions.get(session, {}).get("state") == state: + return False + sessions[session] = {"state": state, "since": now} + return True + + +def frame(sessions, now): + """(colour, brightness) the bulb shows at `now`, or None for nothing.""" + active = [p for p in PRIORITY if any(v["state"] == p for v in sessions.values())] + if not active: + return None + if len(active) > 1: + return COLOURS[active[int(now // CYCLE) % len(active)]], BRIGHT + state = active[0] + since = min(v["since"] for v in sessions.values() if v["state"] == state) + if state in WAITING and now - since > ESCALATE and int(now // BREATHE) % 2: + return COLOURS[state], DIM + return COLOURS[state], BRIGHT + + def selftest(): assert word("permission") == "permission" assert word("complete") == "complete" @@ -84,6 +123,38 @@ def selftest(): assert hook("SubagentStop") is None assert from_claude({"hook_event_name": "Stop"}) is None assert from_claude([]) is None + # apply: a session holds one state; the same state again keeps its + # start time, so repeated notifications do not reset the escalation. + s = {} + assert apply(s, "a", "permission", 10) and s == {"a": {"state": "permission", "since": 10}} + assert not apply(s, "a", "permission", 20) and s["a"]["since"] == 10 + assert apply(s, "a", "complete", 30) and s["a"] == {"state": "complete", "since": 30} + assert apply(s, "b", "error", 30) + assert apply(s, "a", "clear", 40) and list(s) == ["b"] + assert not apply(s, "a", "clear", 40) + assert apply(s, None, "reset", 50) and s == {} + assert not apply(s, None, "reset", 50) + + # frame: what the bulb shows at a moment, (colour, brightness) or None. + def st(state, since=0): + return {"state": state, "since": since} + assert frame({}, 0) is None + assert frame({"a": st("complete")}, 500) == (COLOURS["complete"], BRIGHT) + assert frame({"a": st("error"), "b": st("error")}, 5) == (COLOURS["error"], BRIGHT) + # Waiting past ESCALATE breathes; a finished agent never does. + waiting = ESCALATE + BREATHE + dim_at = waiting if int(waiting // BREATHE) % 2 else waiting + BREATHE + assert frame({"a": st("permission")}, dim_at) == (COLOURS["permission"], DIM) + assert frame({"a": st("permission")}, dim_at + BREATHE) == (COLOURS["permission"], BRIGHT) + assert frame({"a": st("question")}, dim_at) == (COLOURS["question"], DIM) + assert frame({"a": st("question", dim_at - 1)}, dim_at) == (COLOURS["question"], BRIGHT) + assert frame({"a": st("complete")}, dim_at) == (COLOURS["complete"], BRIGHT) + # Several colours cycle in priority order, at full brightness. + two = {"a": st("error"), "b": st("permission")} + assert frame(two, 0) == (COLOURS["permission"], BRIGHT) + assert frame(two, CYCLE) == (COLOURS["error"], BRIGHT) + assert frame(two, 2 * CYCLE) == (COLOURS["permission"], BRIGHT) + assert frame(two, dim_at)[1] == BRIGHT print("ok") |
