[Sync] llm stuff
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
|
||||
One line: vim mode | dir (repo-relative, hidden at the root) |
|
||||
git(repo/branch +dirty +ahead/behind) | model (+effort) |
|
||||
context% (+compact warn) | 5h usage (+reset eta) | cost (+burn rate) | disk. If the
|
||||
context% (+compact warn) | 5h usage (+reset eta) | cost (+burn rate) |
|
||||
prompt-cache hit% (+ttl, cold marker) | disk. If the
|
||||
rendered line would overflow the terminal, trailing (lowest-priority) segments are
|
||||
dropped until it fits.
|
||||
(Dormant, re-addable in main(): ram, cpu%, temp -- retired 2026-07-26 because the
|
||||
@@ -89,7 +90,24 @@ def term_cols():
|
||||
return 0
|
||||
|
||||
|
||||
# --- shared transcript usage ------------------------------------------------
|
||||
# --- context usage ------------------------------------------------------------
|
||||
_USAGE_KEYS = ("input_tokens", "cache_creation_input_tokens", "cache_read_input_tokens")
|
||||
|
||||
|
||||
def payload_usage(data):
|
||||
"""`context_window.current_usage` from the status payload, shaped like `message.usage`.
|
||||
|
||||
Claude Code 2.1.x hands the status line its own token accounting, so the transcript
|
||||
scan below is only a fallback for payloads that predate it (or lack a transcript).
|
||||
"""
|
||||
cw = data.get("context_window")
|
||||
cu = cw.get("current_usage") if isinstance(cw, dict) else None
|
||||
if isinstance(cu, dict) and any(isinstance(cu.get(k), (int, float)) for k in _USAGE_KEYS):
|
||||
return cu
|
||||
return {}
|
||||
|
||||
|
||||
# --- shared transcript usage (fallback) ------------------------------------------
|
||||
def _scan_usage(text):
|
||||
"""Last `message.usage` block among these JSONL lines, or {}."""
|
||||
usage = {}
|
||||
@@ -113,7 +131,7 @@ TRANSCRIPT_TAIL = 1 << 20 # 1 MiB
|
||||
def last_usage(data):
|
||||
"""Most recent `message.usage` block from the transcript, or {}.
|
||||
|
||||
Reads only the tail: this runs on every status refresh and transcripts grow to
|
||||
Fallback only -- see payload_usage(). Reads only the tail: this runs on every status refresh and transcripts grow to
|
||||
tens of MB, so re-parsing the whole file each time is unbounded work for a value
|
||||
that lives at the end. Falls back to a full scan if the tail holds no usage block
|
||||
(a long stretch of tool results can push it past the window).
|
||||
@@ -292,6 +310,11 @@ def context_segment(data, usage):
|
||||
)
|
||||
window = context_window(data)
|
||||
pct = used / window * 100 if window else 0
|
||||
# Prefer Claude Code's own percentage when the payload carries it (2.1.x) -- it
|
||||
# already accounts for the auto-compact reserve and window quirks we can't see.
|
||||
cw = data.get("context_window")
|
||||
if isinstance(cw, dict) and isinstance(cw.get("used_percentage"), (int, float)):
|
||||
pct = float(cw["used_percentage"])
|
||||
col = bucket(pct, 50, 80)
|
||||
out = "📝 " + color(f"{pct:.0f}%", col) + color(f" ({used / 1000:.0f}k)", "gray", dim=True)
|
||||
if pct >= 80:
|
||||
@@ -353,20 +376,31 @@ def velocity_segment(data):
|
||||
return ""
|
||||
|
||||
|
||||
def cache_segment(usage):
|
||||
def cache_segment(usage, data=None):
|
||||
"""Prompt-cache hit % (higher is better) + the cache TTL, `·cold` when the cache has
|
||||
expired. Hit % from the 2.1.260+ payload `prompt_cache.hit_ratio` (a 0..1 fraction, null
|
||||
while all counts are zero), else computed from the usage block; ttl/cold always from the
|
||||
payload when present. A 1h TTL is what keeps a 10-minute pr-loop cadence warm."""
|
||||
try:
|
||||
if not usage:
|
||||
pc = (data or {}).get("prompt_cache")
|
||||
pc = pc if isinstance(pc, dict) else {}
|
||||
pct = None
|
||||
if isinstance(pc.get("hit_ratio"), (int, float)):
|
||||
pct = float(pc["hit_ratio"]) * 100
|
||||
elif usage:
|
||||
read = usage.get("cache_read_input_tokens", 0)
|
||||
total = usage.get("input_tokens", 0) + usage.get("cache_creation_input_tokens", 0) + read
|
||||
if total > 0:
|
||||
pct = read / total * 100
|
||||
if pct is None:
|
||||
return ""
|
||||
read = usage.get("cache_read_input_tokens", 0)
|
||||
total = (
|
||||
usage.get("input_tokens", 0)
|
||||
+ usage.get("cache_creation_input_tokens", 0)
|
||||
+ read
|
||||
)
|
||||
if total <= 0:
|
||||
return ""
|
||||
pct = read / total * 100
|
||||
return "♻️ " + color(f"{pct:.0f}%", bucket(pct, 50, 80, invert=True))
|
||||
out = "♻️ " + color(f"{pct:.0f}%", bucket(pct, 50, 80, invert=True))
|
||||
ttl = pc.get("ttl")
|
||||
if isinstance(ttl, str) and ttl:
|
||||
out += color(f" {ttl}", "gray")
|
||||
if pc.get("warm") is False:
|
||||
out += color(" ·cold", "yellow")
|
||||
return out
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
@@ -532,7 +566,7 @@ def main():
|
||||
or data.get("cwd")
|
||||
or os.getcwd()
|
||||
)
|
||||
usage = last_usage(data)
|
||||
usage = payload_usage(data) or last_usage(data)
|
||||
top = git_top(cwd)
|
||||
|
||||
# One row: work (left) then system (right), joined. If it would overflow the
|
||||
@@ -540,7 +574,7 @@ def main():
|
||||
# Dormant helpers kept above for easy re-add: ram_segment, cpu_segment,
|
||||
# temp_segment (retired -- they only refresh on message events, so they sat
|
||||
# stale; the tmux status bar owns system metrics now), velocity_segment,
|
||||
# cache_segment, api_segment, version_segment, style_segment.
|
||||
# api_segment, version_segment, style_segment.
|
||||
work = [s for s in (
|
||||
vim_segment(data),
|
||||
dir_segment(cwd, top),
|
||||
@@ -549,6 +583,7 @@ def main():
|
||||
context_segment(data, usage),
|
||||
usage_segment(data),
|
||||
cost_segment(data),
|
||||
cache_segment(usage, data),
|
||||
) if s]
|
||||
system = [s for s in (
|
||||
disk_segment(cwd),
|
||||
|
||||
Reference in New Issue
Block a user