Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions bin/omarchy-agent-usage-codex
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def rpc_request(proc, request_id, method, params=None, timeout=8):
raise TimeoutError(method)


def limit_window(window):
def limit_window(window, limit_name=""):
if not isinstance(window, dict):
return None
used = window.get("usedPercent")
Expand All @@ -510,12 +510,17 @@ def limit_window(window):
label = f"{mins}m window"
else:
label = "Limit"
title = "Weekly limit" if mins == 10080 else label
if limit_name:
title = f"{limit_name} {title}"
reset = window.get("resetsAt")
return {
"label": label,
result = {
"label": f"{limit_name} · {label}" if limit_name else label,
"percent": float(used) / 100.0,
"resetsAt": datetime.fromtimestamp(number(reset), timezone.utc).isoformat() if reset else "",
"title": title,
}
return result


def fetch_codex_rpc():
Expand All @@ -528,7 +533,7 @@ def fetch_codex_rpc():

try:
proc = subprocess.Popen(
[codex, "-s", "read-only", "-a", "untrusted", "app-server"],
[codex, "-s", "read-only", "-a", "never", "app-server"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
Expand All @@ -548,14 +553,32 @@ def fetch_codex_rpc():
limits_msg = rpc_request(proc, 3, "account/rateLimits/read", timeout=4)

account = (account_msg.get("result") or {}).get("account") or {}
limits = (limits_msg.get("result") or {}).get("rateLimits") or {}
limits_result = limits_msg.get("result") or {}
limits = limits_result.get("rateLimits") or {}
plan = limits.get("planType") or account.get("planType") or account.get("type") or ""
result["tierLabel"] = str(plan) if plan else ""

for window in (limits.get("primary"), limits.get("secondary")):
entry = limit_window(window)
if entry:
result["limits"].append(entry)
limits_by_id = limits_result.get("rateLimitsByLimitId") or {}
if isinstance(limits_by_id, dict) and limits_by_id:
default_limit_id = limits.get("limitId")
ordered_limit_ids = list(limits_by_id)
if default_limit_id in limits_by_id:
ordered_limit_ids.remove(default_limit_id)
ordered_limit_ids.insert(0, default_limit_id)
for limit_id in ordered_limit_ids:
grouped_limits = limits_by_id.get(limit_id)
if not isinstance(grouped_limits, dict):
continue
limit_name = str(grouped_limits.get("limitName") or "")
for window in (grouped_limits.get("primary"), grouped_limits.get("secondary")):
entry = limit_window(window, limit_name)
if entry:
result["limits"].append(entry)
if not result["limits"]:
for window in (limits.get("primary"), limits.get("secondary")):
entry = limit_window(window)
if entry:
result["limits"].append(entry)
except Exception as exc:
result["usageStatusText"] = "Codex limits unavailable"
result["authHelpText"] = str(exc)
Expand Down
7 changes: 6 additions & 1 deletion shell/plugins/agents/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ Panel {
for (var i = 0; i < list.length; i++) {
var entry = list[i] || {}
var percent = Number(entry.percent)
if (percent >= 0) out.push(limitWindow(entry.label, percent, entry.resetsAt, entry.title))
// Codex can advertise additional model pools before they have been
// touched. Keep those five-hour meters quiet until usage begins.
var isUnusedFiveHour = p.providerId === "codex"
&& windowSpanMs(entry.label) === 5 * 3600 * 1000 && percent === 0
if (percent >= 0 && !isUnusedFiveHour)
out.push(limitWindow(entry.label, percent, entry.resetsAt, entry.title))
}
return out
}
Expand Down
1 change: 1 addition & 0 deletions test/shell.d/agent-usage-claude-limits-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,5 @@ assertDeepEqual(
[{ title: 'Weekly', percent: 0.12, resetAt: '' }],
'agents panel still reads a window out of a label that carries no title'
)

JS
45 changes: 40 additions & 5 deletions test/shell.d/agent-usage-codex-scanner-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mkdir -p "$TEST_HOME/.codex/sessions/$(date +%Y/%m/%d)" "$TEST_HOME/bin"
cat >"$TEST_HOME/bin/codex" <<'EOF'
#!/bin/bash

[[ $* == "-s read-only -a never app-server" ]] || exit 64

while read -r request; do
id=$(jq -r '.id // empty' <<<"$request")
method=$(jq -r '.method // empty' <<<"$request")
Expand All @@ -22,10 +24,36 @@ while read -r request; do
jq -cn --argjson id "$id" '{id: $id, result: {}}'
;;
account/read)
jq -cn --argjson id "$id" '{id: $id, result: {account: {}}}'
jq -cn --argjson id "$id" '{id: $id, result: {account: {planType: "pro"}}}'
;;
account/rateLimits/read)
jq -cn --argjson id "$id" '{id: $id, result: {rateLimits: {}}}'
if [[ ${LEGACY_LIMITS:-false} == "true" ]]; then
jq -cn --argjson id "$id" '{id: $id, result: {rateLimits: {
limitId: "codex",
planType: "pro",
primary: {usedPercent: 94, windowDurationMins: 10080, resetsAt: 1787196799}
}}}'
else
jq -cn --argjson id "$id" '{id: $id, result: {
rateLimits: {
limitId: "codex",
planType: "pro",
primary: {usedPercent: 94, windowDurationMins: 10080, resetsAt: 1787196799}
},
rateLimitsByLimitId: {
codex: {
limitId: "codex",
limitName: null,
primary: {usedPercent: 94, windowDurationMins: 10080, resetsAt: 1787196799}
},
codex_bengalfox: {
limitId: "codex_bengalfox",
limitName: "GPT-5.3-Codex-Spark",
primary: {usedPercent: 12, windowDurationMins: 10080, resetsAt: 1787357885}
}
}
}}'
fi
;;
esac
done
Expand All @@ -51,9 +79,16 @@ pass "Codex collector counts each turn once"
fail "Codex collector does not double-count cache or reasoning tokens" "$result"
pass "Codex collector does not double-count cache or reasoning tokens"

[[ $(jq -c '.id + "/" + (.limits|tostring)' <<<"$result") == '"codex/[]"' ]] ||
fail "Codex collector identifies itself with an empty limits list" "$result"
pass "Codex collector identifies itself with an empty limits list"
[[ $(jq -c '{id, tierLabel, limits}' <<<"$result") == '{"id":"codex","tierLabel":"pro","limits":[{"label":"Weekly (7-day)","percent":0.94,"resetsAt":"2026-08-20T03:33:19+00:00","title":"Weekly limit"},{"label":"GPT-5.3-Codex-Spark · Weekly (7-day)","percent":0.12,"resetsAt":"2026-08-22T00:18:05+00:00","title":"GPT-5.3-Codex-Spark Weekly limit"}]}' ]] ||
fail "Codex collector includes every named usage pool without duplicating the default pool" "$result"
pass "Codex collector includes every named usage pool without duplicating the default pool"

legacy_result=$(HOME="$TEST_HOME" CODEX_HOME="$TEST_HOME/.codex" XDG_DATA_HOME="$TEST_HOME/.local/share" \
LEGACY_LIMITS=true PATH="$TEST_HOME/bin:$PATH" "$ROOT/bin/omarchy-agent-usage-codex" --force)

[[ $(jq -c '.limits' <<<"$legacy_result") == '[{"label":"Weekly (7-day)","percent":0.94,"resetsAt":"2026-08-20T03:33:19+00:00","title":"Weekly limit"}]' ]] ||
fail "Codex collector supports legacy top-level rate limits" "$legacy_result"
pass "Codex collector supports legacy top-level rate limits"

# Pi and omp can both spend a Codex subscription without creating native
# Codex sessions. Their compatible JSONL transcripts must be included.
Expand Down
24 changes: 24 additions & 0 deletions test/shell.d/agents-panel-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,28 @@ assert(/if \(buttonCode === Qt\.RightButton\) root\.launchAgent\(\)/.test(panelS
assert(/else if \(buttonCode === Qt\.MiddleButton\) root\.selectProvider\(root\.providerIndex \+ 1\)/.test(panelSource), 'agents middle click still advances the subscription')
assert(/else root\.toggle\(\)/.test(panelSource), 'agents left click still toggles the panel')
assert(!/if \(buttonCode === Qt\.RightButton\) root\.refreshNow\(\)/.test(panelSource), 'agents right click no longer refreshes')

const start = panelSource.indexOf('function windowIsLong')
const end = panelSource.indexOf('// The window that decides')
assert(start > 0 && end > start, 'agents panel exposes its limit-window helpers')
eval(panelSource.slice(start, end))

const untouchedFiveHour = { label: '5h window', percent: 0, resetsAt: '' }
assertDeepEqual(
limitWindows({ providerId: 'codex', limits: [
untouchedFiveHour,
{ label: 'Weekly (7-day)', percent: 0, resetsAt: '' },
{ label: 'Session (5-hour)', percent: 0.01, resetsAt: '' }
] }),
[
{ title: 'Weekly', percent: 0, resetAt: '' },
{ title: 'Session', percent: 0.01, resetAt: '' }
],
'agents panel hides only untouched Codex five-hour windows'
)
assertDeepEqual(
limitWindows({ providerId: 'claude', limits: [untouchedFiveHour] }),
[{ title: 'Session', percent: 0, resetAt: '' }],
'agents panel leaves other providers unchanged'
)
JS