@hivey/cli 0.9.50 → 0.9.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/native/darwin-arm64/hivey +0 -0
- package/package.json +1 -1
- package/repo/skills/hivey-agent-map/SKILL.md +19 -0
- package/repo/skills/hivey-agent-map/scripts/agent_map.py +248 -21
- package/repo/skills/hivey-agent-map/scripts/enhance.py +229 -0
- package/repo/skills/hivey-agent-map/scripts/report.py +82 -27
- package/repo/skills/hivey-agent-map/scripts/sessions_page.py +153 -0
|
Binary file
|
package/package.json
CHANGED
|
@@ -110,6 +110,25 @@ hivey agent-map schedule install [--at 06:30] [--model haiku] # | remove | sta
|
|
|
110
110
|
|
|
111
111
|
- **hivey menu → agent map** runs `hivey agent-map open`: it opens `agent-stats.html`,
|
|
112
112
|
`agent-map.html` and `agent-logs.html` from `~/.hivey/agent-map/reports/latest`.
|
|
113
|
+
- **hivey's layer on the archify pages** (`enhance.py`, after finalize; `agent_map.py enhance [folder]`
|
|
114
|
+
re-applies it): a Stats · Map · Logs navbar on every page; map boxes coloured by spend (green: none →
|
|
115
|
+
red: the most, log scale, with a legend); a hover explanation on each column header; and in each box's
|
|
116
|
+
info card what it does, spend, tokens, calls, models and last run. Boxes are tied to snapshot nodes by
|
|
117
|
+
`map-meta.json` (`{"nodes": {"<box id>": {"refs": [...], "description": "…"}}, "stages": {...}}`,
|
|
118
|
+
written by the nightly model), else by label. A job counts only its own spend, not its agent's.
|
|
119
|
+
- **Descriptions** ("what it does"): from a brief's mission, a skill's description or a script's module
|
|
120
|
+
docstring; the nightly model writes the missing or weak ones to `~/.hivey/agent-map/descriptions.json`
|
|
121
|
+
(kept, edit freely), which wins over the extracted ones. They show in the map's info cards and the run log.
|
|
122
|
+
- **Automated vs interactive:** the map, the stats page and map colours count AUTOMATED work only
|
|
123
|
+
(scheduled jobs, scripts, headless `claude -p` / `codex exec`, scripts' API calls; snapshot `usage`).
|
|
124
|
+
People chatting with agents (Claude Code, Codex, Grok in a terminal; snapshot `interactive`, with one
|
|
125
|
+
entry per session) go to **`agent-sessions.html`**: cost by agent and by day, the costliest sessions with
|
|
126
|
+
their peak context (⚠️ over 300k tokens), length and first message, and tips to spend less.
|
|
127
|
+
- **Models per job:** the scan reads each job's code for the models it uses, local ones included
|
|
128
|
+
(`nodes[].llm_api.models`: JEV-9B local or Jev on OpenRouter, Claude Opus/Sonnet/Haiku, Grok, DeepSeek,
|
|
129
|
+
Gemini, OpenAI/Codex, Muse, Whisper, Parakeet); the map's info card lists them next to recorded spend.
|
|
130
|
+
- **Per day:** the stats page charts the top agents' cost per day and lists each day's cost, tokens and
|
|
131
|
+
top agents (`usage.daily_by_agent` in the snapshot).
|
|
113
132
|
- **`agent-logs.html`** (run log, written by code): every scheduled job with ✅ ran ok · ⚠️ errors in
|
|
114
133
|
its log or a missed run (a launchd log older than its schedule allows) · ❌ non-zero exit or not
|
|
115
134
|
loaded · ⏳ running · ❔ no log, its last run, the issue, and on click the error lines and last 40
|
|
@@ -304,19 +304,44 @@ def blank():
|
|
|
304
304
|
return {"input": 0, "output": 0, "cache_read": 0, "cache_write": 0, "cost_usd": 0.0, "calls": 0}
|
|
305
305
|
|
|
306
306
|
|
|
307
|
+
def is_interactive(kind: str) -> bool:
|
|
308
|
+
"""A person chatting with an agent (Claude Code, Codex or Grok in a terminal), not an automated run
|
|
309
|
+
(scheduled job, headless `claude -p` / `codex exec`, a script's API call)."""
|
|
310
|
+
return kind.endswith("interactive")
|
|
311
|
+
|
|
312
|
+
|
|
307
313
|
class Usage:
|
|
308
314
|
def __init__(self, days: int):
|
|
309
315
|
self.cutoff = since(days)
|
|
310
316
|
self.table = prices()
|
|
311
317
|
self.rows = [] # (day, agent, provider, model, kind, usage dict)
|
|
312
|
-
self.last_seen: dict[
|
|
318
|
+
self.last_seen: dict[tuple, dt.datetime] = {}
|
|
319
|
+
self.sessions: dict[str, dict] = {} # interactive sessions, one entry each
|
|
320
|
+
self.prompts: dict[str, str] = {} # session id → its first prompt
|
|
313
321
|
|
|
314
|
-
def add(self, ts, agent, provider, model, kind, inp=0, out=0, read=0, write=0, cost_usd=None, calls=1
|
|
322
|
+
def add(self, ts, agent, provider, model, kind, inp=0, out=0, read=0, write=0, cost_usd=None, calls=1,
|
|
323
|
+
session=None):
|
|
315
324
|
if ts is None or ts < self.cutoff:
|
|
316
325
|
return
|
|
317
326
|
c = cost(model, inp, out, read, write, self.table) if cost_usd is None else cost_usd
|
|
318
|
-
if
|
|
319
|
-
self.
|
|
327
|
+
if session and is_interactive(kind):
|
|
328
|
+
se = self.sessions.setdefault(session, {
|
|
329
|
+
"session": session, "agent": agent, "tool": kind.split()[0], "start": ts, "end": ts,
|
|
330
|
+
"cost_usd": 0.0, "calls": 0, "tokens": 0, "output": 0, "max_context": 0, "models": []})
|
|
331
|
+
se.setdefault("agents", [])
|
|
332
|
+
if agent not in se["agents"]:
|
|
333
|
+
se["agents"].append(agent) # a session can move between folders
|
|
334
|
+
se["start"], se["end"] = min(se["start"], ts), max(se["end"], ts)
|
|
335
|
+
se["cost_usd"] += c
|
|
336
|
+
se["calls"] += calls
|
|
337
|
+
se["tokens"] += inp + out + read + write
|
|
338
|
+
se["output"] += out
|
|
339
|
+
se["max_context"] = max(se["max_context"], inp + read + write)
|
|
340
|
+
if model and model not in se["models"]:
|
|
341
|
+
se["models"].append(model)
|
|
342
|
+
key = (agent, is_interactive(kind))
|
|
343
|
+
if ts > self.last_seen.get(key, ts.min.replace(tzinfo=dt.timezone.utc)):
|
|
344
|
+
self.last_seen[key] = ts
|
|
320
345
|
self.rows.append((ts.astimezone().date().isoformat(), agent, provider, model or "?", kind,
|
|
321
346
|
{"input": inp, "output": out, "cache_read": read, "cache_write": write,
|
|
322
347
|
"cost_usd": c, "calls": calls}))
|
|
@@ -334,6 +359,8 @@ class Usage:
|
|
|
334
359
|
with fh:
|
|
335
360
|
for line in fh:
|
|
336
361
|
if '"usage"' not in line:
|
|
362
|
+
if '"type":"user"' in line:
|
|
363
|
+
self.first_prompt(line)
|
|
337
364
|
continue
|
|
338
365
|
try:
|
|
339
366
|
d = json.loads(line)
|
|
@@ -353,7 +380,24 @@ class Usage:
|
|
|
353
380
|
kind = "headless" if d.get("entrypoint") in ("sdk-cli", "sdk-ts", "sdk-py") else "interactive"
|
|
354
381
|
self.add(parse_ts(d.get("timestamp", "")), agent_of(d.get("cwd")), provider, model,
|
|
355
382
|
f"claude-code {kind}", u.get("input_tokens", 0) or 0, u.get("output_tokens", 0) or 0,
|
|
356
|
-
u.get("cache_read_input_tokens", 0) or 0, u.get("cache_creation_input_tokens", 0) or 0
|
|
383
|
+
u.get("cache_read_input_tokens", 0) or 0, u.get("cache_creation_input_tokens", 0) or 0,
|
|
384
|
+
session=d.get("sessionId"))
|
|
385
|
+
|
|
386
|
+
def first_prompt(self, line: str) -> None:
|
|
387
|
+
try:
|
|
388
|
+
d = json.loads(line)
|
|
389
|
+
except ValueError:
|
|
390
|
+
return
|
|
391
|
+
sid = d.get("sessionId")
|
|
392
|
+
if not sid or sid in self.prompts or d.get("isMeta"):
|
|
393
|
+
return
|
|
394
|
+
c = (d.get("message") or {}).get("content")
|
|
395
|
+
if isinstance(c, list):
|
|
396
|
+
c = " ".join(x.get("text", "") for x in c if isinstance(x, dict) and x.get("type") == "text")
|
|
397
|
+
if isinstance(c, str):
|
|
398
|
+
c = " ".join(c.split())
|
|
399
|
+
if c and not c.startswith(("<", "Caveat:", "[Request interrupted")):
|
|
400
|
+
self.prompts[sid] = c[:160]
|
|
357
401
|
|
|
358
402
|
def codex(self):
|
|
359
403
|
cutoff_epoch = self.cutoff.timestamp()
|
|
@@ -370,7 +414,7 @@ class Usage:
|
|
|
370
414
|
text = Path(f).read_text(errors="replace")
|
|
371
415
|
except OSError:
|
|
372
416
|
continue
|
|
373
|
-
cwd, model, last, ts = None, "", None, None
|
|
417
|
+
cwd, model, last, ts, origin, sid = None, "", None, None, "", None
|
|
374
418
|
for line in text.splitlines():
|
|
375
419
|
if '"session_meta"' in line or '"turn_context"' in line or '"token_count"' in line:
|
|
376
420
|
try:
|
|
@@ -380,6 +424,8 @@ class Usage:
|
|
|
380
424
|
p = d.get("payload") or {}
|
|
381
425
|
if d.get("type") == "session_meta":
|
|
382
426
|
cwd = p.get("cwd")
|
|
427
|
+
origin = str(p.get("originator", ""))
|
|
428
|
+
sid = p.get("id")
|
|
383
429
|
elif d.get("type") == "turn_context":
|
|
384
430
|
model = p.get("model") or model
|
|
385
431
|
cwd = p.get("cwd") or cwd
|
|
@@ -388,9 +434,10 @@ class Usage:
|
|
|
388
434
|
ts = parse_ts(d.get("timestamp", ""))
|
|
389
435
|
if last:
|
|
390
436
|
cached = last.get("cached_input_tokens", 0) or 0
|
|
391
|
-
self.add(ts, agent_of(cwd), "openai", model or "codex",
|
|
437
|
+
self.add(ts, agent_of(cwd), "openai", model or "codex",
|
|
438
|
+
"codex headless" if "exec" in origin else "codex interactive",
|
|
392
439
|
(last.get("input_tokens", 0) or 0) - cached, last.get("output_tokens", 0) or 0, cached,
|
|
393
|
-
last.get("cache_write_input_tokens", 0) or 0)
|
|
440
|
+
last.get("cache_write_input_tokens", 0) or 0, session=sid)
|
|
394
441
|
|
|
395
442
|
def grok(self):
|
|
396
443
|
for f in glob.glob(str(HOME / ".grok/sessions/*/*/usage.json")):
|
|
@@ -398,10 +445,12 @@ class Usage:
|
|
|
398
445
|
s = d.get("session") or {}
|
|
399
446
|
folder = Path(f).parent.parent.name.replace("%2F", "/")
|
|
400
447
|
ticks = s.get("costUsdTicks")
|
|
448
|
+
kind = "grok headless" if folder.startswith(("/tmp", "/private/tmp")) else "grok interactive"
|
|
401
449
|
self.add(parse_ts(d.get("updatedAt", "")), agent_of(folder), "xai", s.get("primaryModelId", "grok"),
|
|
402
|
-
|
|
450
|
+
kind, s.get("inputTokens", 0) - s.get("cachedReadTokens", 0), s.get("outputTokens", 0),
|
|
403
451
|
s.get("cachedReadTokens", 0), s.get("cacheCreationTokens", 0),
|
|
404
|
-
cost_usd=ticks / 1e10 if ticks is not None else None, calls=s.get("modelCalls", 1)
|
|
452
|
+
cost_usd=ticks / 1e10 if ticks is not None else None, calls=s.get("modelCalls", 1),
|
|
453
|
+
session=d.get("sessionId"))
|
|
405
454
|
|
|
406
455
|
def ledgers(self):
|
|
407
456
|
for path, provider_default in ((GUARD_LEDGER, "anthropic"), (SPEND_LOG, None)):
|
|
@@ -430,10 +479,13 @@ class Usage:
|
|
|
430
479
|
print(f"warning: {step.__name__}: {e}", file=sys.stderr)
|
|
431
480
|
return self
|
|
432
481
|
|
|
433
|
-
def summary(self) -> dict:
|
|
482
|
+
def summary(self, interactive: bool = False) -> dict:
|
|
483
|
+
"""Automated runs (default) or interactive sessions only."""
|
|
484
|
+
rows = [r for r in self.rows if is_interactive(r[4]) == interactive]
|
|
485
|
+
|
|
434
486
|
def group(keyf):
|
|
435
487
|
g = collections.defaultdict(blank)
|
|
436
|
-
for row in
|
|
488
|
+
for row in rows:
|
|
437
489
|
acc = g[keyf(row)]
|
|
438
490
|
for k, v in row[5].items():
|
|
439
491
|
acc[k] += v
|
|
@@ -443,14 +495,23 @@ class Usage:
|
|
|
443
495
|
for name, tot in sorted(by_agent.items(), key=lambda kv: -kv[1]["cost_usd"]):
|
|
444
496
|
models = collections.Counter()
|
|
445
497
|
providers = collections.Counter()
|
|
446
|
-
for r in
|
|
498
|
+
for r in rows:
|
|
447
499
|
if r[1] == name:
|
|
448
500
|
models[r[3]] += r[5]["cost_usd"] + 1e-12
|
|
449
501
|
providers[r[2]] += r[5]["cost_usd"] + 1e-12
|
|
450
|
-
seen = self.last_seen.get(name)
|
|
502
|
+
seen = self.last_seen.get((name, interactive))
|
|
451
503
|
agents.append({"agent": name, "last_active": seen.astimezone().isoformat(timespec="minutes") if seen else None,
|
|
452
504
|
**{k: round(v, 4) if isinstance(v, float) else v for k, v in tot.items()},
|
|
453
505
|
"models": [m for m, _ in models.most_common()], "providers": [p for p, _ in providers.most_common()]})
|
|
506
|
+
day_agent = collections.defaultdict(lambda: collections.defaultdict(blank))
|
|
507
|
+
for r in rows:
|
|
508
|
+
acc = day_agent[r[0]][r[1]]
|
|
509
|
+
for k, v in r[5].items():
|
|
510
|
+
acc[k] += v
|
|
511
|
+
daily_agents = {day: sorted(({"agent": a, "cost_usd": round(v["cost_usd"], 4), "calls": v["calls"],
|
|
512
|
+
"tokens": v["input"] + v["output"] + v["cache_read"] + v["cache_write"]}
|
|
513
|
+
for a, v in per.items()), key=lambda x: -x["cost_usd"])
|
|
514
|
+
for day, per in sorted(day_agent.items())}
|
|
454
515
|
daily = collections.defaultdict(dict)
|
|
455
516
|
for (day, prov), tot in group(lambda r: (r[0], r[2])).items():
|
|
456
517
|
daily[day][prov] = round(tot["cost_usd"], 4)
|
|
@@ -462,11 +523,124 @@ class Usage:
|
|
|
462
523
|
for k, v in group(lambda r: r[3]).items()},
|
|
463
524
|
"kinds": {k: round(v["cost_usd"], 4) for k, v in group(lambda r: r[4]).items()},
|
|
464
525
|
"daily_cost_by_provider": dict(sorted(daily.items())),
|
|
465
|
-
"
|
|
466
|
-
"
|
|
526
|
+
"daily_by_agent": daily_agents,
|
|
527
|
+
"total_cost_usd": round(sum(r[5]["cost_usd"] for r in rows), 2),
|
|
528
|
+
"total_tokens": sum(r[5]["input"] + r[5]["output"] + r[5]["cache_read"] + r[5]["cache_write"] for r in rows),
|
|
467
529
|
"sources_without_tokens": ["antigravity (agy): no per-session token data on disk"],
|
|
530
|
+
**({"sessions": self.session_list()} if interactive else {}),
|
|
468
531
|
}
|
|
469
532
|
|
|
533
|
+
def session_list(self, limit: int = 500) -> list[dict]:
|
|
534
|
+
out = []
|
|
535
|
+
for se in sorted(self.sessions.values(), key=lambda x: -x["cost_usd"])[:limit]:
|
|
536
|
+
out.append({**se, "start": se["start"].astimezone().isoformat(timespec="minutes"),
|
|
537
|
+
"end": se["end"].astimezone().isoformat(timespec="minutes"),
|
|
538
|
+
"hours": round((se["end"] - se["start"]).total_seconds() / 3600, 1),
|
|
539
|
+
"cost_usd": round(se["cost_usd"], 2), "prompt": self.prompts.get(se["session"], ""),
|
|
540
|
+
"count_total": len(self.sessions)})
|
|
541
|
+
return out
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
# ---------------------------------------------------------------- descriptions
|
|
546
|
+
|
|
547
|
+
DESCRIPTIONS = STATE / "descriptions.json" # {node id: "what it does"}: written by the nightly model or you
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def first_sentence(text: str, limit: int = 170) -> str:
|
|
551
|
+
text = re.sub(r"`|\*\*|__|\[([^\]]*)\]\([^)]*\)", lambda m: m.group(1) or "", text)
|
|
552
|
+
text = " ".join(text.split())
|
|
553
|
+
m = re.match(r"(.+?[.!?])(\s|$)", text)
|
|
554
|
+
out = m.group(1) if m else text
|
|
555
|
+
return out if len(out) <= limit else out[: limit - 1].rsplit(" ", 1)[0] + "…"
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
def describe_markdown(path: Path) -> str:
|
|
559
|
+
"""A brief's mission (or its first real paragraph), or a SKILL.md description."""
|
|
560
|
+
try:
|
|
561
|
+
text = path.read_text(errors="replace")
|
|
562
|
+
except OSError:
|
|
563
|
+
return ""
|
|
564
|
+
fm = re.match(r"---\n(.*?)\n---", text, re.S)
|
|
565
|
+
if fm:
|
|
566
|
+
m = re.search(r"^description:\s*\"?(.+?)\"?\s*$", fm.group(1), re.M)
|
|
567
|
+
if m:
|
|
568
|
+
return first_sentence(m.group(1))
|
|
569
|
+
text = text[fm.end():]
|
|
570
|
+
text = re.sub(r"<!-- BEGIN (GUIDELINES|GLOBAL[^>]*)-->.*?<!-- END (GUIDELINES|GLOBAL)[^>]*-->", "", text, flags=re.S)
|
|
571
|
+
text = re.sub(r"^# General guidelines.*?(?=^# (?!General))", "", text, flags=re.S | re.M)
|
|
572
|
+
m = re.search(r"^#{1,3}\s*(your mission|mission|purpose|what (this|it) does)\b.*?\n(.+?)(\n#|\Z)",
|
|
573
|
+
text, re.I | re.M | re.S)
|
|
574
|
+
body = m.group(3) if m else text
|
|
575
|
+
if BOILERPLATE.search(body[:300]) and m is None:
|
|
576
|
+
return "" # only the global guidelines: nothing about this agent
|
|
577
|
+
for para in re.split(r"\n\s*\n", body):
|
|
578
|
+
p = re.sub(r"^\s*(\d+\.|[-*])\s+", "", para.strip()) # a mission written as a list: its first item
|
|
579
|
+
if p and not p.startswith(("#", ">", "<!--", "|", "```")) and len(p) > 25 and not BOILERPLATE.search(p):
|
|
580
|
+
return first_sentence(p.split("\n")[0] if re.match(r"\s*(\d+\.|[-*])\s", para) else p)
|
|
581
|
+
return ""
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
BOILERPLATE = re.compile(r"behavioral guidelines|general guidelines|think before coding|you are claude|"
|
|
585
|
+
r"before implementing|state your assumptions|always end your final response|"
|
|
586
|
+
r"^(you are|i am) (the )?[\w -]{1,30}( agent)?\.?$|"
|
|
587
|
+
r"the user's (original )?request, verbatim|the user's words", re.I)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def describe_script(path: Path) -> str:
|
|
591
|
+
"""The module docstring or leading comment block of a script (not a function's docstring)."""
|
|
592
|
+
try:
|
|
593
|
+
lines = path.read_text(errors="replace")[:6000].splitlines()
|
|
594
|
+
except OSError:
|
|
595
|
+
return ""
|
|
596
|
+
i, comments = 0, []
|
|
597
|
+
while i < len(lines):
|
|
598
|
+
l = lines[i].strip()
|
|
599
|
+
if l.startswith("#!") or l.startswith("# ///") or (comments == [] and re.match(r"#\s*(requires-python|dependencies|\]|\[|\")", l)):
|
|
600
|
+
i += 1
|
|
601
|
+
continue
|
|
602
|
+
if l.startswith("#"):
|
|
603
|
+
text = l.lstrip("# ").strip()
|
|
604
|
+
if text and not text.startswith(("-*-", "///", "noqa")):
|
|
605
|
+
comments.append(text)
|
|
606
|
+
i += 1
|
|
607
|
+
continue
|
|
608
|
+
if not l:
|
|
609
|
+
i += 1
|
|
610
|
+
if comments:
|
|
611
|
+
break
|
|
612
|
+
continue
|
|
613
|
+
break
|
|
614
|
+
if i < len(lines) and re.match(r'^[rub]*("""|\'\'\')', lines[i].strip()):
|
|
615
|
+
q = lines[i].strip().lstrip("rub")[:3]
|
|
616
|
+
body = [lines[i].strip()[len(q) + (len(lines[i].strip()) - len(lines[i].strip().lstrip("rub"))):]]
|
|
617
|
+
j = i + 1
|
|
618
|
+
while j < len(lines) and q not in body[-1]:
|
|
619
|
+
body.append(lines[j].strip())
|
|
620
|
+
j += 1
|
|
621
|
+
text = " ".join(body).split(q)[0]
|
|
622
|
+
para = re.split(r"\s{2,}|\n\s*\n", text.strip())[0]
|
|
623
|
+
if para and not BOILERPLATE.search(para):
|
|
624
|
+
return first_sentence(para)
|
|
625
|
+
joined = " ".join(comments)
|
|
626
|
+
return first_sentence(joined) if len(joined) > 20 and not BOILERPLATE.search(joined) else ""
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
def describe_job(j: dict) -> str:
|
|
630
|
+
for p in job_run_files(j["command"]):
|
|
631
|
+
pp = Path(p)
|
|
632
|
+
if pp.parent.name == "scripts" and (pp.parent.parent / "SKILL.md").is_file():
|
|
633
|
+
d = describe_markdown(pp.parent.parent / "SKILL.md")
|
|
634
|
+
if d:
|
|
635
|
+
return d
|
|
636
|
+
d = describe_script(pp)
|
|
637
|
+
if d:
|
|
638
|
+
return d
|
|
639
|
+
m = re.search(r'claude\b.*?-p\s+["\']/([\w-]+)', j["command"])
|
|
640
|
+
if m and (HOME / ".claude/skills" / m.group(1) / "SKILL.md").is_file():
|
|
641
|
+
return describe_markdown(HOME / ".claude/skills" / m.group(1) / "SKILL.md")
|
|
642
|
+
return ""
|
|
643
|
+
|
|
470
644
|
|
|
471
645
|
# ---------------------------------------------------------------- links
|
|
472
646
|
|
|
@@ -541,6 +715,34 @@ API_MARKERS = {
|
|
|
541
715
|
}
|
|
542
716
|
|
|
543
717
|
|
|
718
|
+
MODEL_PATTERNS = [ # (regex over a script's code, the model name shown)
|
|
719
|
+
(r"jev \(openrouter\)|openrouter[^\n]{0,80}\bjev\b|\bjev\b[^\n]{0,80}openrouter", "Jev (OpenRouter)"),
|
|
720
|
+
(r"\bJEV[-_ ]?9B\b|jev-server|127\.0\.0\.1:8090|localhost:8090|JEV_BACKEND", "JEV-9B (local)"),
|
|
721
|
+
(r"claude-opus-\d|--model[= ]+[\"']?opus\b", "Claude Opus"),
|
|
722
|
+
(r"claude-sonnet-\d|--model[= ]+[\"']?sonnet\b", "Claude Sonnet"),
|
|
723
|
+
(r"claude-haiku-\d|--model[= ]+[\"']?haiku\b", "Claude Haiku"),
|
|
724
|
+
(r"\bgrok-\d[\w.-]*", "Grok"),
|
|
725
|
+
(r"\bdeepseek[\w./-]*", "DeepSeek"),
|
|
726
|
+
(r"\bgemini-[\d.]+[\w.-]*", "Gemini"),
|
|
727
|
+
(r"\bgpt-[\d.]+[\w.-]*|\bcodex exec\b", "OpenAI / Codex"),
|
|
728
|
+
(r"\bmuse[-_ ]glimmer|muse-glimmer", "Muse Glimmer (local)"),
|
|
729
|
+
(r"\bwhisper\b|mlx[_-]whisper", "Whisper (local)"),
|
|
730
|
+
(r"\bparakeet\b", "Parakeet (local)"),
|
|
731
|
+
]
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
def models_in(text: str) -> list[str]:
|
|
735
|
+
found = []
|
|
736
|
+
for rx, name in MODEL_PATTERNS:
|
|
737
|
+
if re.search(rx, text, re.I) and name not in found:
|
|
738
|
+
found.append(name)
|
|
739
|
+
if "JEV-9B (local)" in found and "Jev (OpenRouter)" in found:
|
|
740
|
+
pass # both backends: local first, hosted as fallback
|
|
741
|
+
if any(n.startswith("Claude ") for n in found) or not re.search(r"\bclaude\b", text):
|
|
742
|
+
return found
|
|
743
|
+
return found + ["Claude"] if re.search(API_MARKERS["claude-cli"], text) else found
|
|
744
|
+
|
|
745
|
+
|
|
544
746
|
def run_files(paths: list[str]) -> list[Path]:
|
|
545
747
|
"""The files a node actually runs: its referenced scripts, the local modules they import and the
|
|
546
748
|
scripts they call (one level). Agents (folders): their scripts folder."""
|
|
@@ -569,7 +771,7 @@ def run_files(paths: list[str]) -> list[Path]:
|
|
|
569
771
|
def api_usage(paths: list[str], command: str = "") -> dict:
|
|
570
772
|
"""Which LLM APIs a node's code calls, and which of those already log their spend
|
|
571
773
|
(hivey_spend logs any provider; claude-api-guard only Anthropic; claude -p is in the transcripts)."""
|
|
572
|
-
providers, logged_by, files = set(), set(), []
|
|
774
|
+
providers, logged_by, files, models = set(), set(), [], models_in(command)
|
|
573
775
|
if re.search(API_MARKERS["claude-cli"], command):
|
|
574
776
|
providers.add("claude-cli")
|
|
575
777
|
files.append("(job command)")
|
|
@@ -582,14 +784,15 @@ def api_usage(paths: list[str], command: str = "") -> dict:
|
|
|
582
784
|
if hit:
|
|
583
785
|
providers |= hit
|
|
584
786
|
files.append(short(str(f)))
|
|
787
|
+
models += [m for m in models_in(text) if m not in models]
|
|
585
788
|
if "hivey_spend" in text:
|
|
586
789
|
logged_by.add("all")
|
|
587
790
|
if re.search(r"claude[-_]api[-_]guard", text):
|
|
588
791
|
logged_by.add("anthropic")
|
|
589
|
-
if not providers:
|
|
792
|
+
if not providers and not models:
|
|
590
793
|
return {}
|
|
591
794
|
unlogged = sorted(p for p in providers if p != "claude-cli" and "all" not in logged_by and p not in logged_by)
|
|
592
|
-
return {"providers": sorted(providers), "unlogged": unlogged, "files": files[:6]}
|
|
795
|
+
return {"providers": sorted(providers), "unlogged": unlogged, "files": files[:6], "models": models}
|
|
593
796
|
|
|
594
797
|
|
|
595
798
|
def job_run_files(command: str) -> list[str]:
|
|
@@ -623,6 +826,8 @@ def build_links(jobs: list[dict], live: list[dict]) -> tuple[list[dict], dict]:
|
|
|
623
826
|
nodes.setdefault(nid, {"id": nid, "label": a["agent"], "type": "agent", "agent": a["agent"], "paths": []})
|
|
624
827
|
for n in nodes.values():
|
|
625
828
|
u = api_usage(n["run"], n["command"]) if n["type"] == "job" else api_usage(n["paths"])
|
|
829
|
+
if u and "hivey-agent-map" in n.get("command", ""):
|
|
830
|
+
u["models"] = ["Claude Haiku"] # this map's own run (its code names every model it prices)
|
|
626
831
|
if u:
|
|
627
832
|
n["llm_api"] = u
|
|
628
833
|
touches: dict[str, list[tuple[str, str]]] = collections.defaultdict(list)
|
|
@@ -653,8 +858,21 @@ def build_links(jobs: list[dict], live: list[dict]) -> tuple[list[dict], dict]:
|
|
|
653
858
|
def scan(days: int) -> dict:
|
|
654
859
|
jobs = cron_jobs() + launchd_jobs()
|
|
655
860
|
live = live_agents()
|
|
656
|
-
|
|
861
|
+
collected = Usage(days).collect()
|
|
862
|
+
usage, interactive = collected.summary(), collected.summary(interactive=True)
|
|
657
863
|
nodes, candidates = build_links(jobs, live)
|
|
864
|
+
overrides = load_json(DESCRIPTIONS, {})
|
|
865
|
+
for j in jobs:
|
|
866
|
+
j["description"] = overrides.get(j["id"]) or describe_job(j)
|
|
867
|
+
by_job = {j["id"]: j["description"] for j in jobs}
|
|
868
|
+
for n in nodes:
|
|
869
|
+
if n["id"] in overrides:
|
|
870
|
+
n["description"] = overrides[n["id"]]
|
|
871
|
+
elif n["type"] == "job":
|
|
872
|
+
n["description"] = by_job.get(n["id"], "")
|
|
873
|
+
elif n.get("dir"):
|
|
874
|
+
n["description"] = next((d for d in (describe_markdown(Path(n["dir"]) / f) for f in
|
|
875
|
+
("CLAUDE.md", "AGENTS.md", "README.md")) if d), "")
|
|
658
876
|
for n in nodes:
|
|
659
877
|
n.pop("paths", None)
|
|
660
878
|
n.pop("run", None)
|
|
@@ -665,7 +883,8 @@ def scan(days: int) -> dict:
|
|
|
665
883
|
"jobs": jobs,
|
|
666
884
|
"live_agents": live,
|
|
667
885
|
"agent_logs": agent_logs(),
|
|
668
|
-
"usage": usage,
|
|
886
|
+
"usage": usage, # automated runs only: scheduled jobs, headless runs, scripts' API calls
|
|
887
|
+
"interactive": interactive, # people chatting with agents: the Sessions page
|
|
669
888
|
"nodes": nodes,
|
|
670
889
|
"links_confirmed": load_json(LINKS, []),
|
|
671
890
|
"links_candidates": candidates,
|
|
@@ -745,6 +964,8 @@ def main() -> int:
|
|
|
745
964
|
nt.add_argument("--model", default="haiku")
|
|
746
965
|
nt.add_argument("--no-llm", action="store_true", help="no model run: last night's map, unchanged")
|
|
747
966
|
nt.add_argument("--open", action="store_true", help="open the report when done")
|
|
967
|
+
en = sub.add_parser("enhance", help="re-apply hivey's layer (navbar, colours, info) to a report")
|
|
968
|
+
en.add_argument("folder", nargs="?", default=str(STATE / "reports" / "latest"))
|
|
748
969
|
op = sub.add_parser("open", help="open the latest report (builds the first one if there is none)")
|
|
749
970
|
op.add_argument("--model", default="haiku")
|
|
750
971
|
sc = sub.add_parser("schedule", help="the nightly launchd run: install | remove | status")
|
|
@@ -776,6 +997,12 @@ def main() -> int:
|
|
|
776
997
|
if a.open:
|
|
777
998
|
report.open_latest(a.model)
|
|
778
999
|
return code
|
|
1000
|
+
if a.cmd == "enhance":
|
|
1001
|
+
import report, enhance
|
|
1002
|
+
folder = Path(a.folder).expanduser().resolve()
|
|
1003
|
+
enhance.enhance_folder(folder, json.loads((folder / "snapshot.json").read_text()), report.job_state)
|
|
1004
|
+
print(f"enhanced {folder}")
|
|
1005
|
+
return 0
|
|
779
1006
|
if a.cmd == "open":
|
|
780
1007
|
import report
|
|
781
1008
|
return report.open_latest(a.model)
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""hivey's layer on top of the archify pages, added after they are finalized:
|
|
2
|
+
|
|
3
|
+
- a navbar (Stats · Map · Logs) on every page, so each page reaches the others;
|
|
4
|
+
- on the map: each job/agent box coloured by its spend in the window (green: none, red: the most),
|
|
5
|
+
with a legend; a hover explanation on each column header; and, in the box's info card, what the
|
|
6
|
+
job or agent does, its tokens, cost, calls, models and last run.
|
|
7
|
+
|
|
8
|
+
Map boxes are tied to the snapshot by `map-meta.json` in the report folder
|
|
9
|
+
({"nodes": {"<map node id>": {"refs": ["agent:x-content", "cron:x-ai-agents-trends"],
|
|
10
|
+
"description": "…"}}, "stages": {"<stage label>": "…"}}), written by the nightly model; a box
|
|
11
|
+
without refs is matched by its label. Python standard library only.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import html
|
|
16
|
+
import json
|
|
17
|
+
import math
|
|
18
|
+
import re
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
STAGE_HELP = {
|
|
22
|
+
"scheduled jobs": "Jobs that start on their own at set times (cron or launchd), without anyone asking.",
|
|
23
|
+
"raw data": "What the jobs write: transcripts, Baserow tables, files. Other jobs and agents read them.",
|
|
24
|
+
"pipelines": "Jobs that read the raw data and turn it into something: wiki pages, reports, summaries.",
|
|
25
|
+
"knowledge & agents": "Wikis and vaults the pipelines keep up to date, and the agents that feed others.",
|
|
26
|
+
"knowledge": "Wikis and vaults the pipelines keep up to date.",
|
|
27
|
+
"agents": "hivey agents you talk to: they read the knowledge and data on the left and do the work.",
|
|
28
|
+
"outputs": "What leaves the system: posts, messages, published files.",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
NAV_CSS = """
|
|
32
|
+
.hv-nav{position:relative;z-index:50;display:flex;gap:4px;align-items:center;justify-content:center;
|
|
33
|
+
padding:6px 10px;font:600 13px/1.2 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;
|
|
34
|
+
border-bottom:1px solid rgba(127,127,127,.25);background:rgba(127,127,127,.06)}
|
|
35
|
+
.hv-nav a{color:inherit;opacity:.65;text-decoration:none;padding:4px 12px;border-radius:999px}
|
|
36
|
+
.hv-nav a:hover{opacity:1;background:rgba(127,127,127,.14)}
|
|
37
|
+
.hv-nav a.on{opacity:1;background:rgba(47,111,236,.16);color:#2f6fec}
|
|
38
|
+
.hv-nav .hv-when{margin-left:12px;opacity:.5;font-weight:400;font-size:12px}
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
MAP_CSS = """
|
|
42
|
+
.hv-legend{position:fixed;left:14px;bottom:14px;z-index:40;padding:8px 10px;border-radius:10px;
|
|
43
|
+
font:12px/1.3 -apple-system,BlinkMacSystemFont,sans-serif;background:rgba(255,255,255,.92);color:#1d2330;
|
|
44
|
+
border:1px solid rgba(127,127,127,.3);box-shadow:0 2px 10px rgba(0,0,0,.08)}
|
|
45
|
+
html[data-theme="dark"] .hv-legend{background:rgba(22,27,34,.92);color:#e6edf3}
|
|
46
|
+
@media (prefers-color-scheme:dark){html:not([data-theme="light"]) .hv-legend{background:rgba(22,27,34,.92);color:#e6edf3}}
|
|
47
|
+
.hv-legend .bar{width:180px;height:9px;border-radius:5px;margin:5px 0 3px;
|
|
48
|
+
background:linear-gradient(90deg,hsl(120,65%,45%),hsl(60,75%,48%),hsl(0,70%,50%))}
|
|
49
|
+
.hv-legend .ends{display:flex;justify-content:space-between;opacity:.75}
|
|
50
|
+
.hv-info{margin-top:.7rem;padding-top:.6rem;border-top:1px solid rgba(127,127,127,.25);font-size:.78rem;line-height:1.45}
|
|
51
|
+
.hv-info .hv-desc{margin:0 0 .5rem;font-size:.85rem}
|
|
52
|
+
.hv-info dl{display:grid;grid-template-columns:auto 1fr;gap:.18rem .7rem;margin:0}
|
|
53
|
+
.hv-info dt{opacity:.6}.hv-info dd{margin:0;font-variant-numeric:tabular-nums}
|
|
54
|
+
.hv-info .hv-models{display:flex;flex-wrap:wrap;gap:.25rem}
|
|
55
|
+
.hv-info .hv-models span{padding:.05rem .4rem;border:1px solid rgba(127,127,127,.35);border-radius:999px;font-size:.7rem}
|
|
56
|
+
svg text.hv-stage{cursor:help;text-decoration:underline dotted;text-underline-offset:2px}
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def nav(active: str, generated: str) -> str:
|
|
61
|
+
links = [("agent-stats.html", "Stats"), ("agent-map.html", "Map"), ("agent-logs.html", "Logs"),
|
|
62
|
+
("agent-sessions.html", "Sessions")]
|
|
63
|
+
items = "".join(f'<a href="{href}"{" class=on" if href == active else ""}>{label}</a>' for href, label in links)
|
|
64
|
+
return f'<nav class="hv-nav">{items}<span class="hv-when">scan {html.escape(generated[:16].replace("T", " "))}</span></nav>'
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def inject(page: str, css: str, top: str, script: str = "") -> str:
|
|
68
|
+
"""Adds hivey's pieces, each wrapped in <!--hv--> markers so a rerun replaces them."""
|
|
69
|
+
page = re.sub(r"<!--hv-->.*?<!--/hv-->", "", page, flags=re.S)
|
|
70
|
+
page = page.replace("</head>", f"<!--hv--><style>{css}</style><!--/hv--></head>", 1)
|
|
71
|
+
m = re.search(r"<body[^>]*>", page)
|
|
72
|
+
if m:
|
|
73
|
+
page = page[: m.end()] + f"<!--hv-->{top}<!--/hv-->" + page[m.end():]
|
|
74
|
+
if script:
|
|
75
|
+
block = f"<!--hv--><script>{script}</script><!--/hv-->"
|
|
76
|
+
page = page.replace("</body>", block + "</body>", 1) if "</body>" in page else page + block
|
|
77
|
+
return page
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def money(v: float) -> str:
|
|
81
|
+
return f"${v:,.2f}"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def mtok(n: int) -> str:
|
|
85
|
+
return f"{n / 1e6:,.1f}M" if n >= 1e6 else f"{n / 1e3:,.0f}k" if n >= 1e3 else str(n)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def model_name(m: str) -> str:
|
|
89
|
+
m = m.rsplit("/", 1)[-1]
|
|
90
|
+
k = re.match(r"claude-(opus|sonnet|haiku)-(\d+)(?:-(\d{1,2}))?(?!\d)", m)
|
|
91
|
+
return (f"{k.group(1).title()} {k.group(2)}" + (f".{k.group(3)}" if k.group(3) else "")) if k else m
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def node_data(snap: dict, spec: dict, meta: dict, job_state) -> dict:
|
|
95
|
+
usage = {a["agent"]: a for a in snap["usage"]["agents"]}
|
|
96
|
+
try: # what the nightly model (or you) wrote since the scan wins
|
|
97
|
+
overrides = json.loads((Path.home() / ".hivey/agent-map/descriptions.json").read_text())
|
|
98
|
+
except (OSError, ValueError):
|
|
99
|
+
overrides = {}
|
|
100
|
+
jobs = {j["id"]: j for j in snap["jobs"]}
|
|
101
|
+
jobs_by_name = {j["name"]: j for j in snap["jobs"]}
|
|
102
|
+
snodes = {n["id"]: n for n in snap["nodes"]}
|
|
103
|
+
snodes_by_label = {n["label"]: n for n in snap["nodes"]}
|
|
104
|
+
live = {}
|
|
105
|
+
for a in snap["live_agents"]:
|
|
106
|
+
live.setdefault(a["agent"], a)
|
|
107
|
+
out = {}
|
|
108
|
+
for n in spec.get("nodes", []):
|
|
109
|
+
m = (meta.get("nodes") or {}).get(n["id"], {})
|
|
110
|
+
refs = list(m.get("refs") or [])
|
|
111
|
+
if not refs:
|
|
112
|
+
label = n["label"]
|
|
113
|
+
for cand in (label, re.sub(r"\s+(vault|wiki|jobs?|agent)$", "", label)):
|
|
114
|
+
if cand in snodes_by_label:
|
|
115
|
+
refs.append(snodes_by_label[cand]["id"])
|
|
116
|
+
break
|
|
117
|
+
if cand in jobs_by_name:
|
|
118
|
+
refs.append(jobs_by_name[cand]["id"])
|
|
119
|
+
break
|
|
120
|
+
names, desc, states, code_models = [], m.get("description", ""), [], []
|
|
121
|
+
for ref in refs:
|
|
122
|
+
if overrides.get(ref) and not desc:
|
|
123
|
+
desc = overrides[ref]
|
|
124
|
+
j = jobs.get(ref)
|
|
125
|
+
if j:
|
|
126
|
+
# its own spend only: a job inside an agent's folder must not take the agent's cost
|
|
127
|
+
agent = j.get("agent") or ""
|
|
128
|
+
names += [j["name"]] + ([agent, agent.removeprefix("skill:")] if agent.startswith("skill:") else [])
|
|
129
|
+
desc = desc or j.get("description", "")
|
|
130
|
+
emo, words = job_state(j)
|
|
131
|
+
states.append(f"{emo} {words} · {(j.get('last_run') or j.get('log_modified') or '?').replace('T', ' ')}")
|
|
132
|
+
sn = snodes.get(ref)
|
|
133
|
+
if sn:
|
|
134
|
+
code_models += [m for m in (sn.get("llm_api") or {}).get("models", []) if m not in code_models]
|
|
135
|
+
if sn:
|
|
136
|
+
names.append(sn["label"])
|
|
137
|
+
desc = desc or sn.get("description", "")
|
|
138
|
+
if sn["type"] == "agent" and sn["label"] in live:
|
|
139
|
+
states.append(f"{live[sn['label']]['status']} in hivey")
|
|
140
|
+
seen, tot, models = set(), {"cost": 0.0, "tokens": 0, "out": 0, "calls": 0}, []
|
|
141
|
+
for name in names:
|
|
142
|
+
if name and name in usage and name not in seen:
|
|
143
|
+
seen.add(name)
|
|
144
|
+
a = usage[name]
|
|
145
|
+
tot["cost"] += a["cost_usd"]
|
|
146
|
+
tot["tokens"] += a["input"] + a["output"] + a["cache_read"] + a["cache_write"]
|
|
147
|
+
tot["out"] += a["output"]
|
|
148
|
+
tot["calls"] += a["calls"]
|
|
149
|
+
models += [model_name(x) for x in a["models"] if model_name(x) not in models]
|
|
150
|
+
# models seen in its code (also local ones that log no tokens), then the ones with recorded tokens
|
|
151
|
+
for mname in models:
|
|
152
|
+
if not any(mname.split()[0].lower() in c.lower() for c in code_models):
|
|
153
|
+
code_models.append(mname)
|
|
154
|
+
out[n["id"]] = {"refs": refs, "desc": desc, "cost": round(tot["cost"], 2), "tokens": tot["tokens"],
|
|
155
|
+
"out": tot["out"], "calls": tot["calls"], "models": code_models[:5], "states": states[:3],
|
|
156
|
+
"subject": bool(refs) and n.get("type") != "database"}
|
|
157
|
+
spend = [d["cost"] for d in out.values() if d["subject"]]
|
|
158
|
+
top = max(spend, default=0.0)
|
|
159
|
+
for d in out.values():
|
|
160
|
+
if d["subject"]:
|
|
161
|
+
t = math.log1p(d["cost"]) / math.log1p(top) if top > 0 else 0.0
|
|
162
|
+
d["hue"] = round(120 * (1 - t))
|
|
163
|
+
return out, top
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
MAP_JS = r"""
|
|
167
|
+
(function(){
|
|
168
|
+
const D=__DATA__, STAGES=__STAGES__, DAYS=__DAYS__;
|
|
169
|
+
const fmt=v=>'$'+v.toLocaleString(undefined,{minimumFractionDigits:2,maximumFractionDigits:2});
|
|
170
|
+
const tok=n=>n>=1e6?(n/1e6).toFixed(1)+'M':n>=1e3?Math.round(n/1e3)+'k':String(n);
|
|
171
|
+
const esc=s=>String(s).replace(/[&<>"]/g,c=>({'&':'&','<':'<','>':'>','"':'"'}[c]));
|
|
172
|
+
document.querySelectorAll('g[data-node-id]').forEach(g=>{
|
|
173
|
+
const d=D[g.dataset.nodeId]; if(!d) return;
|
|
174
|
+
if(d.hue!=null){g.querySelectorAll(':scope > rect:not(.c-mask)').forEach(r=>{
|
|
175
|
+
r.style.fill='hsla('+d.hue+',75%,50%,.18)'; r.style.stroke='hsl('+d.hue+',68%,42%)';});}
|
|
176
|
+
const t=g.querySelector(':scope > title');
|
|
177
|
+
if(t){t.textContent=[g.dataset.nodeLabel, d.desc, d.subject?(fmt(d.cost)+' in '+DAYS+' days · '+tok(d.tokens)+' tokens'+(d.models.length?' · '+d.models.join(', '):'')):''].filter(Boolean).join('\n');}
|
|
178
|
+
});
|
|
179
|
+
document.querySelectorAll('svg text').forEach(t=>{
|
|
180
|
+
const m=/^\s*\d+\s*\/\s*(.+?)\s*$/.exec(t.textContent||''); if(!m) return;
|
|
181
|
+
const help=STAGES[m[1].toLowerCase()]; if(!help) return;
|
|
182
|
+
t.classList.add('hv-stage');
|
|
183
|
+
const ti=document.createElementNS('http://www.w3.org/2000/svg','title'); ti.textContent=m[1]+': '+help; t.appendChild(ti);
|
|
184
|
+
t.style.pointerEvents='auto';
|
|
185
|
+
});
|
|
186
|
+
let current=null;
|
|
187
|
+
const pick=e=>{const g=e.target.closest&&e.target.closest('g[data-node-id]'); if(g) current=g.dataset.nodeId;};
|
|
188
|
+
document.addEventListener('pointerdown',pick,true); document.addEventListener('focusin',pick,true);
|
|
189
|
+
function fill(meta){
|
|
190
|
+
const box=meta.parentElement; if(!box||box.querySelector('.hv-info')) return;
|
|
191
|
+
const d=D[current]; if(!d) return;
|
|
192
|
+
const rows=[];
|
|
193
|
+
if(d.subject){
|
|
194
|
+
rows.push(['Spend', fmt(d.cost)+' in '+DAYS+' days, automated runs only (API-equivalent)']);
|
|
195
|
+
rows.push(['Tokens', tok(d.tokens)+' ('+tok(d.out)+' output) · '+d.calls.toLocaleString()+' calls']);
|
|
196
|
+
}
|
|
197
|
+
rows.push(['Models', d.models.length?'<span class="hv-models">'+d.models.map(m=>'<span>'+esc(m)+'</span>').join('')+'</span>':'none found']);
|
|
198
|
+
d.states.forEach(s=>rows.push(['Last run', esc(s)]));
|
|
199
|
+
const div=document.createElement('div'); div.className='hv-info';
|
|
200
|
+
div.innerHTML=(d.desc?'<p class="hv-desc">'+esc(d.desc)+'</p>':'')+(rows.length?'<dl>'+rows.map(r=>'<dt>'+r[0]+'</dt><dd>'+r[1]+'</dd>').join('')+'</dl>':'');
|
|
201
|
+
meta.insertAdjacentElement('afterend',div);
|
|
202
|
+
}
|
|
203
|
+
new MutationObserver(()=>document.querySelectorAll('.semantic-passport-meta').forEach(fill))
|
|
204
|
+
.observe(document.body,{childList:true,subtree:true});
|
|
205
|
+
})();
|
|
206
|
+
"""
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def enhance_folder(folder: Path, snap: dict, job_state) -> None:
|
|
210
|
+
generated = snap.get("generated", "")
|
|
211
|
+
stats = folder / "agent-stats.html"
|
|
212
|
+
if stats.is_file():
|
|
213
|
+
stats.write_text(inject(stats.read_text(), NAV_CSS, nav("agent-stats.html", generated)))
|
|
214
|
+
page, spec_path = folder / "agent-map.html", folder / "map.json"
|
|
215
|
+
if page.is_file() and spec_path.is_file():
|
|
216
|
+
spec = json.loads(spec_path.read_text())
|
|
217
|
+
try:
|
|
218
|
+
meta = json.loads((folder / "map-meta.json").read_text())
|
|
219
|
+
except (OSError, ValueError):
|
|
220
|
+
meta = {}
|
|
221
|
+
data, top = node_data(snap, spec, meta, job_state)
|
|
222
|
+
stages = dict(STAGE_HELP)
|
|
223
|
+
stages.update({k.lower(): v for k, v in (meta.get("stages") or {}).items()})
|
|
224
|
+
script = (MAP_JS.replace("__DATA__", json.dumps(data)).replace("__STAGES__", json.dumps(stages))
|
|
225
|
+
.replace("__DAYS__", json.dumps(snap.get("window_days", 7))))
|
|
226
|
+
legend = (f'<div class="hv-legend"><b>Spend, last {snap.get("window_days", 7)} days</b><div class="bar"></div>'
|
|
227
|
+
f'<div class="ends"><span>$0</span><span>{money(top)}</span></div>'
|
|
228
|
+
f'<div style="opacity:.65;margin-top:3px">click a box for what it does, tokens and models</div></div>')
|
|
229
|
+
page.write_text(inject(page.read_text(), NAV_CSS + MAP_CSS, nav("agent-map.html", generated) + legend, script))
|
|
@@ -96,9 +96,8 @@ def write_stats(snap: dict, folder: Path) -> None:
|
|
|
96
96
|
w.writerow([a["agent"], round(a["cost_usd"], 2)])
|
|
97
97
|
|
|
98
98
|
kinds = u["kinds"]
|
|
99
|
-
interactive = sum(v for k, v in kinds.items() if "interactive" in k)
|
|
100
99
|
headless = sum(v for k, v in kinds.items() if "headless" in k)
|
|
101
|
-
api = total -
|
|
100
|
+
api = total - headless
|
|
102
101
|
models = sorted(u["models"].items(), key=lambda kv: -kv[1]["cost_usd"])
|
|
103
102
|
jobs = snap["jobs"]
|
|
104
103
|
failing = [j for j in jobs if job_state(j)[0] == "❌"]
|
|
@@ -108,21 +107,24 @@ def write_stats(snap: dict, folder: Path) -> None:
|
|
|
108
107
|
pct = lambda v: f"{100 * v / total:.0f}%" if total else "0%"
|
|
109
108
|
|
|
110
109
|
L = []
|
|
111
|
-
|
|
110
|
+
inter = snap.get("interactive", {})
|
|
111
|
+
L.append(f"# What ran on its own on this Mac in the last {days} days, and what did it cost? {{#agent-stats}}\n")
|
|
112
112
|
if top:
|
|
113
|
-
L.append(f"Your agents used about **{money(total)} of
|
|
114
|
-
f"**{cell(top['agent'])} used {money(top['cost_usd'])}** of it ({pct(top['cost_usd'])}).\n")
|
|
113
|
+
L.append(f"Your scheduled jobs, scripts and headless agents used about **{money(total)} of tokens** in "
|
|
114
|
+
f"{days} days; **{cell(top['agent'])} used {money(top['cost_usd'])}** of it ({pct(top['cost_usd'])}).\n")
|
|
115
115
|
else:
|
|
116
|
-
L.append(f"No token use was recorded in the last {days} days.\n")
|
|
117
|
-
L.append(f"- {
|
|
118
|
-
f"
|
|
116
|
+
L.append(f"No automated token use was recorded in the last {days} days.\n")
|
|
117
|
+
L.append(f"- Headless agent runs cost {money(headless)} and scripts' direct API calls {money(api)}; local "
|
|
118
|
+
f"models (JEV, Whisper) cost nothing.")
|
|
119
|
+
if inter.get("total_cost_usd"):
|
|
120
|
+
L.append(f"- Your interactive sessions are not counted here: {money(inter['total_cost_usd'])} in "
|
|
121
|
+
f"{(inter.get('sessions') or [{}])[0].get('count_total', 0)} sessions, on the Sessions page (`agent-sessions.html`).")
|
|
119
122
|
if models:
|
|
120
123
|
L.append("- " + "; ".join(f"{cell(model_name(m))} {money(v['cost_usd'])}" for m, v in models[:3]) + ".")
|
|
121
124
|
states = [job_state(j)[0] for j in jobs]
|
|
122
125
|
tally = ", ".join(f"{emo} {states.count(emo)}" for emo in ("✅", "⚠️", "❌", "⏳", "❔") if states.count(emo))
|
|
123
|
-
L.append(f"- {len(jobs)} scheduled jobs ({n_cron} cron, {len(jobs) - n_cron} launchd): {tally}
|
|
124
|
-
|
|
125
|
-
f"{len(gaps)} with LLM calls not logged yet.\n")
|
|
126
|
+
L.append(f"- {len(jobs)} scheduled jobs ({n_cron} cron, {len(jobs) - n_cron} launchd): {tally}; "
|
|
127
|
+
f"{len(snap['links_confirmed'])} links between them; {len(gaps)} with LLM calls not logged yet.\n")
|
|
126
128
|
L.append(f'@answer scope="last {days} days to {snap["generated"][:10]} · costs are list-price estimates · '
|
|
127
129
|
f'Claude Code (Max) and Codex (Plus) are not billed per token"')
|
|
128
130
|
L.append("@meta locale=en")
|
|
@@ -160,6 +162,30 @@ def write_stats(snap: dict, folder: Path) -> None:
|
|
|
160
162
|
L.append("## How does the cost move day by day? {#daily}\n")
|
|
161
163
|
L.append('@chart type=line data=daily.csv x=day y=cost_usd unit=USD title="Daily cost, all providers '
|
|
162
164
|
'(API-equivalent USD, estimated; today is partial)"\n')
|
|
165
|
+
per_day = u.get("daily_by_agent", {})
|
|
166
|
+
if per_day:
|
|
167
|
+
top5 = [a["agent"] for a in agents[:5]]
|
|
168
|
+
cols = [re.sub(r"[^\w-]", "_", a) for a in top5]
|
|
169
|
+
with open(folder / "daily_agents.csv", "w", newline="") as fh:
|
|
170
|
+
w = csv.writer(fh)
|
|
171
|
+
w.writerow(["day"] + cols)
|
|
172
|
+
for day, rows in per_day.items():
|
|
173
|
+
by = {r["agent"]: r["cost_usd"] for r in rows}
|
|
174
|
+
w.writerow([day[5:]] + [round(by.get(a, 0.0), 2) for a in top5])
|
|
175
|
+
L.append("## What ran each day? {#per-day}\n")
|
|
176
|
+
L.append(f'@chart type=bar data=daily_agents.csv x=day y={",".join(cols)} unit=USD '
|
|
177
|
+
f'title="Cost per day of the top {len(top5)} agents (API-equivalent USD, estimated)"\n')
|
|
178
|
+
L.append("| Day | Cost | Tokens | Agents that day (top 3) |")
|
|
179
|
+
L.append("| --- | --- | --- | --- |")
|
|
180
|
+
days_cost = {d: sum(r["cost_usd"] for r in rows) for d, rows in per_day.items()}
|
|
181
|
+
peak = max(days_cost, key=days_cost.get)
|
|
182
|
+
for day, rows in per_day.items():
|
|
183
|
+
cost_d, tok_d = days_cost[day], sum(r["tokens"] for r in rows)
|
|
184
|
+
badge = " {bad:peak}" if day == peak and len(per_day) > 2 else ""
|
|
185
|
+
tops = ", ".join(f"{cell(r['agent'])} {money(r['cost_usd'])}" for r in rows[:3])
|
|
186
|
+
L.append(f"| {dt.date.fromisoformat(day):%a %d %b} | {money(cost_d)}{badge} | {mtok(tok_d)} | "
|
|
187
|
+
f"{tops} (+{max(0, len(rows) - 3)} more) |")
|
|
188
|
+
L.append("")
|
|
163
189
|
|
|
164
190
|
L.append("## How do the agents feed each other? {#map}\n")
|
|
165
191
|
L.append("The full map is `agent-map.html` in this folder.\n")
|
|
@@ -233,6 +259,7 @@ def write_logs_page(snap: dict, folder: Path) -> None:
|
|
|
233
259
|
e = html.escape
|
|
234
260
|
jobs = sorted(snap["jobs"], key=lambda j: (ORDER.get(job_state(j)[0], 9), j["name"]))
|
|
235
261
|
usage = {a["agent"]: a for a in snap["usage"]["agents"]}
|
|
262
|
+
inter_usage = {a["agent"]: a for a in (snap.get("interactive") or {}).get("agents", [])}
|
|
236
263
|
logs = snap.get("agent_logs", {})
|
|
237
264
|
live: dict[str, dict] = {}
|
|
238
265
|
for a in snap["live_agents"]:
|
|
@@ -240,7 +267,8 @@ def write_logs_page(snap: dict, folder: Path) -> None:
|
|
|
240
267
|
if not cur or ORDER.get(AGENT_STATE.get(a["status"], ("⚪",))[0], 9) < ORDER.get(AGENT_STATE.get(cur["status"], ("⚪",))[0], 9):
|
|
241
268
|
live[a["agent"]] = a
|
|
242
269
|
names = sorted(set(live) | {n["label"] for n in snap["nodes"] if n["type"] == "agent"}
|
|
243
|
-
| {a for a in usage if usage[a]["cost_usd"] >= 0.5}
|
|
270
|
+
| {a for a in usage if usage[a]["cost_usd"] >= 0.5}
|
|
271
|
+
| {a for a in inter_usage if inter_usage[a]["cost_usd"] >= 0.5})
|
|
244
272
|
|
|
245
273
|
def details(body: list[str]) -> str:
|
|
246
274
|
return "".join(body)
|
|
@@ -268,10 +296,11 @@ def write_logs_page(snap: dict, folder: Path) -> None:
|
|
|
268
296
|
if not j.get("log"):
|
|
269
297
|
body.append("<p class=meta>this job writes no log file hivey can find.</p>")
|
|
270
298
|
rows.append(f"<details class=row data-state='{emo}'><summary><span class=emo>{emo}</span>"
|
|
271
|
-
f"<b>{e(j['name'])}</b><
|
|
299
|
+
f"<span class=name><b>{e(j['name'])}</b><small class=desc>{e(j.get('description') or '')}</small></span>"
|
|
300
|
+
f"<span class=sub>{e(words)}</span><span class=when>{e(last)}</span>"
|
|
272
301
|
f"<span class=issue>{e(issue[:110])}</span></summary>{details(body)}</details>")
|
|
273
302
|
arows = []
|
|
274
|
-
dormant = [n for n in names if n not in live and n not in usage and not logs.get(n)]
|
|
303
|
+
dormant = [n for n in names if n not in live and n not in usage and n not in inter_usage and not logs.get(n)]
|
|
275
304
|
names = [n for n in names if n not in dormant]
|
|
276
305
|
for name in sorted(names, key=lambda n: (ORDER.get(AGENT_STATE.get(live.get(n, {}).get("status", "gone"), ("⚫",))[0], 9), n)):
|
|
277
306
|
a = live.get(name)
|
|
@@ -280,13 +309,17 @@ def write_logs_page(snap: dict, folder: Path) -> None:
|
|
|
280
309
|
if info.get("errors") and emo in ("⚪", "✅", "⚫"):
|
|
281
310
|
emo, words = "⚠️", words + ", errors in its log"
|
|
282
311
|
u = usage.get(name, {})
|
|
283
|
-
|
|
312
|
+
iu = inter_usage.get(name, {})
|
|
313
|
+
parts = ([f"automated ${u['cost_usd']:,.2f}"] if u else []) + ([f"chats ${iu['cost_usd']:,.2f}"] if iu else [])
|
|
314
|
+
cost = (" · ".join(parts) + f" in {snap['window_days']} days") if parts else "no token use"
|
|
284
315
|
body = [f"<p class=meta>{e(a['where'] if a else 'not open in hivey')} · {e(a.get('cwd', '') if a else '')}"
|
|
285
|
-
f" · last active {e(u.get('last_active') or '?')} · {e(cost)}"
|
|
286
|
-
+ (f" · {e(', '.join(u.get('models', [])[:2]))}" if u else "") + "</p>"]
|
|
316
|
+
f" · last active {e((u or iu).get('last_active') or '?')} · {e(cost)}"
|
|
317
|
+
+ (f" · {e(', '.join((u or iu).get('models', [])[:2]))}" if (u or iu) else "") + "</p>"]
|
|
287
318
|
body += log_block(info) if info else ["<p class=meta>no <code>logs/</code> folder in this agent.</p>"]
|
|
288
|
-
|
|
289
|
-
|
|
319
|
+
adesc = next((n.get("description", "") for n in snap["nodes"] if n["type"] == "agent" and n["label"] == name), "")
|
|
320
|
+
arows.append(f"<details class=row><summary><span class=emo>{emo}</span>"
|
|
321
|
+
f"<span class=name><b>{e(name)}</b><small class=desc>{e(adesc)}</small></span>"
|
|
322
|
+
f"<span class=sub>{e(words)}</span><span class=when>{e(((u or iu).get('last_active') or '')[:16].replace('T', ' '))}</span>"
|
|
290
323
|
f"<span class=issue>{e(cost)}</span></summary>{details(body)}</details>")
|
|
291
324
|
if dormant:
|
|
292
325
|
arows.append(f"<details class=row><summary><span class=emo>⚫</span><b>{len(dormant)} dormant agents</b>"
|
|
@@ -306,9 +339,10 @@ nav a{{color:var(--accent);margin-right:18px;text-decoration:none;font-weight:60
|
|
|
306
339
|
h1{{font-size:26px;margin:18px 0 4px}} h2{{font-size:19px;margin:34px 0 10px}}
|
|
307
340
|
.lead{{color:var(--mute);margin:0 0 6px}} .counts{{font-size:17px;margin:10px 0 0}}
|
|
308
341
|
.row{{border:1px solid var(--line);background:var(--card);border-radius:10px;margin:7px 0}}
|
|
309
|
-
summary{{display:grid;grid-template-columns:30px minmax(
|
|
342
|
+
summary{{display:grid;grid-template-columns:30px minmax(220px,2fr) minmax(120px,.8fr) 130px minmax(0,1.6fr);gap:12px;align-items:center;padding:10px 14px;cursor:pointer;list-style:none}}
|
|
310
343
|
summary::-webkit-details-marker{{display:none}}
|
|
311
|
-
.emo{{font-size:18px}} .
|
|
344
|
+
.emo{{font-size:18px}} .name{{min-width:0}} .name b{{display:block}}
|
|
345
|
+
.desc{{display:block;color:var(--mute);font-size:12px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}} .sub,.when{{color:var(--mute);font-size:13px}} .issue{{font:12.5px ui-monospace,Menlo,monospace;color:var(--mute);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}}
|
|
312
346
|
details[open] summary{{border-bottom:1px solid var(--line)}} details>*:not(summary){{margin:10px 16px}}
|
|
313
347
|
.meta{{color:var(--mute);font-size:13px}} code{{font:12.5px ui-monospace,Menlo,monospace;word-break:break-all}}
|
|
314
348
|
.errs{{margin-top:0}} .errs li{{color:#d1242f}} @media (prefers-color-scheme:dark){{.errs li{{color:#ff7b72}}}}
|
|
@@ -317,7 +351,7 @@ pre{{background:var(--pre);border:1px solid var(--line);border-radius:8px;paddin
|
|
|
317
351
|
@media (max-width:760px){{summary{{grid-template-columns:28px 1fr}} .sub,.when,.issue{{grid-column:2}}}}
|
|
318
352
|
</style>
|
|
319
353
|
<main>
|
|
320
|
-
<nav><a href="agent-stats.html">Stats</a><a href="agent-map.html">Map</a><a class=on href="agent-logs.html">Logs</a></nav>
|
|
354
|
+
<nav class=hv-top><a href="agent-stats.html">Stats</a><a href="agent-map.html">Map</a><a class=on href="agent-logs.html">Logs</a><a href="agent-sessions.html">Sessions</a></nav>
|
|
321
355
|
<h1>Did every job and agent run?</h1>
|
|
322
356
|
<p class=lead>Scan of {e(snap['generated'][:16].replace('T', ' '))}. Click a row for its issues and the last lines of its log.</p>
|
|
323
357
|
<p class=counts>Scheduled jobs: {summary}</p>
|
|
@@ -366,9 +400,22 @@ already built: do not touch stats.archify.md or agent-stats.html.
|
|
|
366
400
|
python3 -I {scripts}/agent_map.py link <from-id> <to-id> --via "<what flows>" --evidence "<file:line>"
|
|
367
401
|
Skip anything unclear or coincidental. Then rerun: python3 -I {scripts}/agent_map.py scan --out snapshot.json
|
|
368
402
|
2. Map: {map_task}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
403
|
+
The map shows AUTOMATED work only: scheduled jobs, scripts and headless agents, the data they pass
|
|
404
|
+
to each other, and the models each one uses (any model: JEV, Claude, DeepSeek, Grok, Codex, Whisper;
|
|
405
|
+
nodes[].llm_api.models in snapshot.json). People chatting with agents (interactive sessions) are on
|
|
406
|
+
another page: never show their spend; drop an agent that has no automated runs and no data links,
|
|
407
|
+
and keep a consumer agent (one a job feeds) without its interactive cost. Each node's sublabel holds
|
|
408
|
+
its schedule or role, its models and its automated cost when it has one ("cron 04:15 · Grok + Sonnet ·
|
|
409
|
+
$0.40"); spend is usage.agents (automated only). Keep at most 25 nodes on 5 stages x rows 0-4;
|
|
410
|
+
refresh the numbers in the cards (automated spend, models in use, needs attention).
|
|
411
|
+
3. Descriptions: every node in snapshot.json has a "description" (what it does). For each job and agent
|
|
412
|
+
whose description is empty or does not say what it does (an instruction to the agent, "You are X"),
|
|
413
|
+
read its brief or script (first lines only) and write ONE plain sentence of at most 18 words into
|
|
414
|
+
~/.hivey/agent-map/descriptions.json ({{"<node id>": "<sentence>"}}; keep the entries already there).
|
|
415
|
+
Then write map-meta.json in this folder: {{"nodes": {{"<map node id>": {{"refs": [<snapshot node ids
|
|
416
|
+
this box stands for>], "description": "<one sentence>"}}}}, "stages": {{"<stage label>": "<what this
|
|
417
|
+
column holds, one sentence>"}}}}. A box that folds several jobs lists all of them in refs.
|
|
418
|
+
4. Run: node {archify} finalize dataflow map.json agent-map.html --quality showcase --json --out-dir review-map-llm-1
|
|
372
419
|
If it fails, repair map.json and rerun with review-map-llm-2, -3 ... (at most 4 runs).
|
|
373
420
|
Stop there and reply with one line: what changed."""
|
|
374
421
|
|
|
@@ -424,7 +471,8 @@ def run_llm(folder: Path, model: str, has_map: bool) -> bool:
|
|
|
424
471
|
prompt = MAP_PROMPT.format(folder=folder, scripts=SCRIPTS, archify=archify, map_task=task)
|
|
425
472
|
tools = ("Read,Write,Edit,Glob,Grep,Bash(python3:*),Bash(node:*),Bash(ls:*),Bash(cat:*),"
|
|
426
473
|
"Bash(head:*),Bash(tail:*),Bash(grep:*),Bash(sed -n:*),Bash(wc:*)")
|
|
427
|
-
cmd = [claude, "-p", prompt, "--model", model, "--allowedTools", tools, "--output-format", "text"
|
|
474
|
+
cmd = [claude, "-p", prompt, "--model", model, "--allowedTools", tools, "--output-format", "text",
|
|
475
|
+
"--add-dir", str(STATE)] # descriptions.json and links.json live there
|
|
428
476
|
log(folder, f"updating the map with {model}")
|
|
429
477
|
env = {**os.environ, "HIVEY_AGENT": "hivey-agent-map"}
|
|
430
478
|
token = claude_token(folder)
|
|
@@ -458,6 +506,8 @@ def nightly(scan, days: int, model: str, use_llm: bool) -> int:
|
|
|
458
506
|
(STATE / "snapshot.json").write_text(json.dumps(snap, indent=1, default=str))
|
|
459
507
|
write_stats(snap, folder)
|
|
460
508
|
write_logs_page(snap, folder)
|
|
509
|
+
import sessions_page
|
|
510
|
+
sessions_page.write_sessions_page(snap, folder)
|
|
461
511
|
ok, msg = finalize(folder, "explainer", "stats.archify.md", "agent-stats.html")
|
|
462
512
|
log(folder, f"stats page: {'ok' if ok else 'FAILED ' + msg}")
|
|
463
513
|
|
|
@@ -477,6 +527,11 @@ def nightly(scan, days: int, model: str, use_llm: bool) -> int:
|
|
|
477
527
|
else:
|
|
478
528
|
log(folder, "map: ok" if (folder / "agent-map.html").is_file() else "map: none yet")
|
|
479
529
|
|
|
530
|
+
try:
|
|
531
|
+
import enhance
|
|
532
|
+
enhance.enhance_folder(folder, json.loads((folder / "snapshot.json").read_text()), job_state)
|
|
533
|
+
except Exception as e: # the pages still work without hivey's layer
|
|
534
|
+
log(folder, f"enhance failed: {e}")
|
|
480
535
|
if ok:
|
|
481
536
|
tmp = REPORTS / ".latest-new"
|
|
482
537
|
if tmp.is_symlink() or tmp.exists():
|
|
@@ -508,7 +563,7 @@ def notify(text: str) -> None:
|
|
|
508
563
|
def open_latest(model: str) -> int:
|
|
509
564
|
folder = latest_report()
|
|
510
565
|
if folder:
|
|
511
|
-
for page in ("agent-stats.html", "agent-map.html", "agent-logs.html"):
|
|
566
|
+
for page in ("agent-stats.html", "agent-map.html", "agent-logs.html", "agent-sessions.html"):
|
|
512
567
|
if (folder / page).is_file():
|
|
513
568
|
subprocess.run(["open", str(folder / page)], capture_output=True)
|
|
514
569
|
print(f"opened {folder}")
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""The Sessions page (agent-sessions.html): what your interactive sessions with agents cost.
|
|
2
|
+
|
|
3
|
+
Interactive = a person chatting with an agent in a terminal (Claude Code, Codex, Grok). These are
|
|
4
|
+
kept out of the map and the stats page, which show automated work only. Written from the snapshot's
|
|
5
|
+
`interactive` summary, without an LLM. Python standard library only.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import collections
|
|
10
|
+
import datetime as dt
|
|
11
|
+
import html
|
|
12
|
+
import re
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
e = html.escape
|
|
16
|
+
LONG_CONTEXT = 300_000 # tokens: above this every reply re-reads a very large session
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def money(v: float) -> str:
|
|
20
|
+
return f"${v:,.2f}"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def mtok(n: int) -> str:
|
|
24
|
+
return f"{n / 1e6:,.1f}M" if n >= 1e6 else f"{n / 1e3:,.0f}k" if n >= 1e3 else str(n)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def model_name(m: str) -> str:
|
|
28
|
+
m = m.rsplit("/", 1)[-1]
|
|
29
|
+
k = re.match(r"claude-(opus|sonnet|haiku)-(\d+)(?:-(\d{1,2}))?(?!\d)", m)
|
|
30
|
+
return (f"{k.group(1).title()} {k.group(2)}" + (f".{k.group(3)}" if k.group(3) else "")) if k else m
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def bar(value: float, top: float) -> str:
|
|
34
|
+
pct = 0 if top <= 0 else max(1.5, 100 * value / top)
|
|
35
|
+
return f'<span class=bar><i style="width:{pct:.1f}%"></i></span>'
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def write_sessions_page(snap: dict, folder: Path) -> None:
|
|
39
|
+
inter = snap.get("interactive") or {}
|
|
40
|
+
sessions = inter.get("sessions") or []
|
|
41
|
+
days = snap.get("window_days", 7)
|
|
42
|
+
total = inter.get("total_cost_usd", 0.0)
|
|
43
|
+
agents = inter.get("agents") or []
|
|
44
|
+
per_agent = collections.defaultdict(lambda: {"n": 0, "peak": 0, "long": 0})
|
|
45
|
+
for s in sessions:
|
|
46
|
+
for name in s.get("agents") or [s["agent"]]:
|
|
47
|
+
pa = per_agent[name]
|
|
48
|
+
pa["n"] += 1
|
|
49
|
+
pa["peak"] = max(pa["peak"], s["max_context"])
|
|
50
|
+
pa["long"] += s["max_context"] > LONG_CONTEXT
|
|
51
|
+
long_cost = sum(s["cost_usd"] for s in sessions if s["max_context"] > LONG_CONTEXT)
|
|
52
|
+
long_n = sum(1 for s in sessions if s["max_context"] > LONG_CONTEXT)
|
|
53
|
+
day_long = sum(1 for s in sessions if s["hours"] >= 12)
|
|
54
|
+
models = sorted((inter.get("models") or {}).items(), key=lambda kv: -kv[1]["cost_usd"])
|
|
55
|
+
|
|
56
|
+
top_agent = max((a["cost_usd"] for a in agents), default=0.0)
|
|
57
|
+
arows = []
|
|
58
|
+
for a in agents[:25]:
|
|
59
|
+
pa = per_agent[a["agent"]]
|
|
60
|
+
tok = a["input"] + a["output"] + a["cache_read"] + a["cache_write"]
|
|
61
|
+
warn = " ⚠️" if pa["long"] else ""
|
|
62
|
+
arows.append(
|
|
63
|
+
f"<tr><td><b>{e(a['agent'])}</b></td><td class=num>{pa['n']}</td>"
|
|
64
|
+
f"<td class=cost>{bar(a['cost_usd'], top_agent)}<span>{money(a['cost_usd'])}</span></td>"
|
|
65
|
+
f"<td class=num>{mtok(tok)}</td><td class=num>{mtok(pa['peak'])}{warn}</td>"
|
|
66
|
+
f"<td>{e(', '.join(model_name(m) for m in a['models'][:2]))}</td>"
|
|
67
|
+
f"<td class=dim>{e((a.get('last_active') or '')[:16].replace('T', ' '))}</td></tr>")
|
|
68
|
+
|
|
69
|
+
per_day = inter.get("daily_by_agent") or {}
|
|
70
|
+
day_cost = {d: sum(r["cost_usd"] for r in rows) for d, rows in per_day.items()}
|
|
71
|
+
top_day = max(day_cost.values(), default=0.0)
|
|
72
|
+
drows = []
|
|
73
|
+
for d, rows in per_day.items():
|
|
74
|
+
tops = ", ".join(f"{e(r['agent'])} {money(r['cost_usd'])}" for r in rows[:3])
|
|
75
|
+
drows.append(
|
|
76
|
+
f"<tr><td><b>{dt.date.fromisoformat(d):%a %d %b}</b></td>"
|
|
77
|
+
f"<td class=cost>{bar(day_cost[d], top_day)}<span>{money(day_cost[d])}</span></td>"
|
|
78
|
+
f"<td class=num>{mtok(sum(r['tokens'] for r in rows))}</td><td>{tops}</td></tr>")
|
|
79
|
+
|
|
80
|
+
srows = []
|
|
81
|
+
for s in sessions[:20]:
|
|
82
|
+
ctx_warn = " ⚠️" if s["max_context"] > LONG_CONTEXT else ""
|
|
83
|
+
srows.append(
|
|
84
|
+
f"<tr><td class=num><b>{money(s['cost_usd'])}</b></td><td>{e(s['agent'])}</td>"
|
|
85
|
+
f"<td class=dim>{e(s['start'][5:16].replace('T', ' '))} → {e(s['end'][5:16].replace('T', ' '))}"
|
|
86
|
+
f" · {s['hours']}h</td><td class=num>{s['calls']:,}</td>"
|
|
87
|
+
f"<td class=num>{mtok(s['max_context'])}{ctx_warn}</td>"
|
|
88
|
+
f"<td>{e(', '.join(model_name(m) for m in s['models'][:2]))}</td>"
|
|
89
|
+
f"<td class=prompt title=\"{e(s.get('prompt', ''))}\">{e(s.get('prompt', '') or '—')}</td></tr>")
|
|
90
|
+
|
|
91
|
+
tips = []
|
|
92
|
+
if long_n:
|
|
93
|
+
tips.append(f"<li><b>{long_n} sessions grew past {LONG_CONTEXT // 1000}k tokens of context</b> and cost "
|
|
94
|
+
f"{money(long_cost)} ({100 * long_cost / total:.0f}% of the total): every reply re-reads the whole "
|
|
95
|
+
f"session. Start a new session per task (<code>/clear</code>) and <code>/compact</code> a long one.</li>")
|
|
96
|
+
if day_long:
|
|
97
|
+
tips.append(f"<li><b>{day_long} sessions stayed open 12 hours or more.</b> After a pause the cached context "
|
|
98
|
+
"expires and is written again in full, which costs more than reading it.</li>")
|
|
99
|
+
if models and "opus" in models[0][0]:
|
|
100
|
+
tips.append(f"<li><b>{e(model_name(models[0][0]))} is {money(models[0][1]['cost_usd'])}</b> of it: drafting, "
|
|
101
|
+
"reviews and diagrams cost half on Sonnet (<code>/model sonnet</code>).</li>")
|
|
102
|
+
n_total = sessions[0]["count_total"] if sessions else 0
|
|
103
|
+
|
|
104
|
+
page = f"""<!doctype html><html lang=en><meta charset=utf-8>
|
|
105
|
+
<meta name=viewport content="width=device-width,initial-scale=1">
|
|
106
|
+
<title>Interactive sessions · {e(snap.get('generated', '')[:16])}</title>
|
|
107
|
+
<style>
|
|
108
|
+
:root{{--bg:#fff;--fg:#1d2330;--mute:#667085;--line:#e4e7ec;--card:#f8f9fb;--accent:#2f6fec}}
|
|
109
|
+
@media (prefers-color-scheme:dark){{:root{{--bg:#0d1117;--fg:#e6edf3;--mute:#8b949e;--line:#30363d;--card:#161b22;--accent:#79a8ff}}}}
|
|
110
|
+
body{{margin:0;background:var(--bg);color:var(--fg);font:15px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}}
|
|
111
|
+
main{{max-width:1180px;margin:0 auto;padding:28px 24px 60px}}
|
|
112
|
+
nav a{{color:var(--accent);margin-right:18px;text-decoration:none;font-weight:600}} nav a.on{{color:var(--fg)}}
|
|
113
|
+
h1{{font-size:26px;margin:18px 0 4px}} h2{{font-size:19px;margin:34px 0 10px}}
|
|
114
|
+
.lead{{color:var(--mute);margin:0 0 14px}}
|
|
115
|
+
.kpis{{display:flex;gap:12px;flex-wrap:wrap;margin:14px 0}}
|
|
116
|
+
.kpi{{border:1px solid var(--line);background:var(--card);border-radius:10px;padding:10px 16px;min-width:150px}}
|
|
117
|
+
.kpi b{{display:block;font-size:22px}} .kpi span{{color:var(--mute);font-size:13px}}
|
|
118
|
+
table{{width:100%;border-collapse:collapse;font-size:14px}}
|
|
119
|
+
th{{text-align:left;color:var(--mute);font-weight:600;font-size:12.5px;border-bottom:1px solid var(--line);padding:6px 8px}}
|
|
120
|
+
td{{border-bottom:1px solid var(--line);padding:7px 8px;vertical-align:middle}}
|
|
121
|
+
.num{{text-align:right;font-variant-numeric:tabular-nums;white-space:nowrap}} .dim{{color:var(--mute);font-size:13px;white-space:nowrap}}
|
|
122
|
+
.cost{{display:flex;align-items:center;gap:8px;min-width:200px}} .cost span{{font-variant-numeric:tabular-nums}}
|
|
123
|
+
.bar{{flex:1;height:8px;border-radius:4px;background:color-mix(in srgb,var(--line) 70%,transparent);overflow:hidden;max-width:160px}}
|
|
124
|
+
.bar i{{display:block;height:100%;background:linear-gradient(90deg,hsl(40,85%,52%),hsl(0,72%,52%))}}
|
|
125
|
+
.prompt{{max-width:330px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;color:var(--mute);font-size:13px}}
|
|
126
|
+
.tips{{border:1px solid var(--line);background:var(--card);border-radius:10px;padding:6px 18px}}
|
|
127
|
+
.tips li{{margin:8px 0}} code{{font:12.5px ui-monospace,Menlo,monospace}}
|
|
128
|
+
.note{{color:var(--mute);font-size:13px;margin-top:30px}}
|
|
129
|
+
</style>
|
|
130
|
+
<main>
|
|
131
|
+
<nav><a href="agent-stats.html">Stats</a><a href="agent-map.html">Map</a><a href="agent-logs.html">Logs</a><a class=on href="agent-sessions.html">Sessions</a></nav>
|
|
132
|
+
<h1>Your interactive sessions, last {days} days</h1>
|
|
133
|
+
<p class=lead>Chats with agents in a terminal (Claude Code, Codex, Grok), kept apart from the automated jobs on the map.
|
|
134
|
+
Costs are API-equivalent: a Max plan is not billed per token, but these use up its limits.</p>
|
|
135
|
+
<div class=kpis>
|
|
136
|
+
<div class=kpi><b>{money(total)}</b><span>API-equivalent cost</span></div>
|
|
137
|
+
<div class=kpi><b>{n_total}</b><span>sessions</span></div>
|
|
138
|
+
<div class=kpi><b>{mtok(inter.get('total_tokens', 0))}</b><span>tokens (mostly re-read context)</span></div>
|
|
139
|
+
<div class=kpi><b>{e(', '.join(model_name(m) for m, _ in models[:2]) or '—')}</b><span>main models</span></div>
|
|
140
|
+
</div>
|
|
141
|
+
{f'<h2>How to spend less</h2><ul class=tips>{"".join(tips)}</ul>' if tips else ''}
|
|
142
|
+
<h2>By agent</h2>
|
|
143
|
+
<table><tr><th>Agent</th><th class=num>Sessions</th><th>Cost</th><th class=num>Tokens</th><th class=num>Peak context</th><th>Models</th><th>Last active</th></tr>
|
|
144
|
+
{''.join(arows) or '<tr><td colspan=7>No interactive sessions in this window.</td></tr>'}</table>
|
|
145
|
+
<h2>By day</h2>
|
|
146
|
+
<table><tr><th>Day</th><th>Cost</th><th class=num>Tokens</th><th>Agents that day (top 3)</th></tr>{''.join(drows)}</table>
|
|
147
|
+
<h2>Costliest sessions</h2>
|
|
148
|
+
<table><tr><th class=num>Cost</th><th>Agent</th><th>When</th><th class=num>Calls</th><th class=num>Peak context</th><th>Models</th><th>First message</th></tr>
|
|
149
|
+
{''.join(srows)}</table>
|
|
150
|
+
<p class=note>⚠️ peak context over {LONG_CONTEXT // 1000}k tokens. From the Claude Code, Codex and Grok session files on this
|
|
151
|
+
Mac; Claude Code keeps 15 days of them. Scan {e(snap.get('generated', '')[:16].replace('T', ' '))}.</p>
|
|
152
|
+
</main></html>"""
|
|
153
|
+
(folder / "agent-sessions.html").write_text(page)
|