M plugins/gitea/.claude-plugin/plugin.json M plugins/gitea/.codex-plugin/plugin.json M plugins/gitea/README.md M plugins/gitea/skills/gitea/SKILL.md M plugins/gitea/skills/gitea/scripts/fetch_ticket.py M plugins/gitea/skills/gitea/scripts/start_ticket.py A plugins/spire/UPGRADES.md A plugins/spire/agents/designer.md M plugins/spire/agents/planner.md A plugins/spire/providers/README.md A plugins/spire/providers/hosts/claude-desktop/spawning.md A plugins/spire/providers/hosts/t3-code/spawning.md A plugins/spire/providers/runtimes/codex/spawning.md M plugins/spire/skills/gremlins/grm/call.py M plugins/spire/skills/gremlins/grm/cli.py A plugins/spire/skills/gremlins/grm/codex.py A plugins/spire/skills/gremlins/grm/errors.py A plugins/spire/skills/gremlins/grm/harness.py M plugins/spire/skills/gremlins/grm/launch.py A plugins/spire/skills/gremlins/grm/scrub.py M plugins/spire/skills/gremlins/grm/stream.py A plugins/spire/skills/gremlins/grm/title.py A plugins/spire/skills/gremlins/tests/_pin.py M plugins/spire/skills/gremlins/tests/fake_claude.py A plugins/spire/skills/gremlins/tests/fake_codex.py M plugins/spire/skills/gremlins/tests/test_cli.py A plugins/spire/skills/gremlins/tests/test_cli_codex.py A plugins/spire/skills/gremlins/tests/test_codex.py A plugins/spire/skills/gremlins/tests/test_harness.py A plugins/spire/skills/gremlins/tests/test_scrub.py A plugins/spire/skills/gremlins/tests/test_title.py A plugins/spire/skills/igor/references/upgrades.md A plugins/spire/skills/igor/tests/test_upgrade_notes.py A plugins/spire/skills/igor/upgrade-notes.sh R plugins/spire/skills/implementation/references/spawning-claude-code.md -> plugins/spire/providers/runtimes/claude-code/spawning.md M plugins/spire/skills/project/SKILL.md
277 lines
12 KiB
Python
277 lines
12 KiB
Python
"""Stream readers and accounting for one `claude -p` or `codex exec --json` call: pure
|
|
functions, no IO (the Codex rollout reader lives in `codex.py`)."""
|
|
import datetime as _dt
|
|
import json
|
|
import re
|
|
import time
|
|
|
|
TTL_1H, TTL_5M = 3600, 300
|
|
CACHE_HIT_RATIO = 0.9
|
|
MU_FIELDS = ("inputTokens", "outputTokens", "cacheReadInputTokens", "cacheCreationInputTokens")
|
|
TOKEN_KEYS = ("input", "output", "cache_creation", "cache_read", "cache_creation_1h", "cache_creation_5m")
|
|
|
|
|
|
def iso(t: float | None = None) -> str:
|
|
"""ISO 8601 with the local offset, from epoch seconds (now if not given)."""
|
|
if t is None:
|
|
t = time.time()
|
|
return _dt.datetime.fromtimestamp(t).astimezone().isoformat(timespec="seconds")
|
|
|
|
|
|
def parse_iso(s) -> float | None:
|
|
"""Epoch seconds of an ISO string written by `iso`, or None."""
|
|
if not s or not isinstance(s, str):
|
|
return None
|
|
try:
|
|
return _dt.datetime.fromisoformat(s).timestamp()
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
class StreamState:
|
|
"""What one `claude -p --output-format stream-json --verbose` stream told us so far."""
|
|
|
|
def __init__(self):
|
|
self.session_id = None
|
|
self.first_usage = None # usage of the first assistant event (cache verdict)
|
|
self.first_at = None # its arrival time (the base prefix read)
|
|
self.last_usage = None # usage of the last assistant event (base context)
|
|
self.last_assistant_at = None
|
|
self.result = None # the result event
|
|
|
|
def feed(self, ev: dict, t: float | None) -> None:
|
|
if not isinstance(ev, dict):
|
|
return
|
|
ty = ev.get("type")
|
|
if ty == "system":
|
|
st = ev.get("subtype")
|
|
if st == "init" and self.session_id is None:
|
|
self.session_id = ev.get("session_id")
|
|
elif ty == "assistant":
|
|
m = ev.get("message") or {}
|
|
u = m.get("usage") or {}
|
|
if self.first_usage is None:
|
|
self.first_usage, self.first_at = u, t
|
|
self.last_usage, self.last_assistant_at = u, t
|
|
elif ty == "result":
|
|
self.result = ev
|
|
self.session_id = ev.get("session_id") or self.session_id
|
|
|
|
|
|
def context_tokens(usage: dict | None) -> int:
|
|
"""Prompt-side tokens of one request: input + cache creation + cache read."""
|
|
if not usage:
|
|
return 0
|
|
return (int(usage.get("input_tokens") or 0) + int(usage.get("cache_creation_input_tokens") or 0)
|
|
+ int(usage.get("cache_read_input_tokens") or 0))
|
|
|
|
|
|
_API_ERROR_TEXT = ( # checked in this order, after the status code
|
|
("prompt_too_long", re.compile(r"prompt is too long|prompt too long|prompt_too_long", re.I)),
|
|
("usage_limit", re.compile(r"usage limit|rate[ _-]?limit|hit your limit|api error:?\s*429", re.I)),
|
|
("overloaded", re.compile(r"overloaded|api error:?\s*529", re.I)),
|
|
("api_error", re.compile(r"api error", re.I)),
|
|
)
|
|
|
|
|
|
def api_error_reason(res: dict | None) -> tuple[str, str] | None:
|
|
"""(reason, detail) for a result that carries an API error, else None. `reason` is
|
|
`usage_limit` (429), `overloaded` (529), `prompt_too_long` or `api_error`."""
|
|
if not res:
|
|
return None
|
|
status = res.get("api_error_status")
|
|
try:
|
|
status = int(status) if status not in (None, "") else None
|
|
except (TypeError, ValueError):
|
|
status = None
|
|
if not res.get("is_error") and status is None:
|
|
return None
|
|
parts = [res.get("result"), res.get("error"), res.get("errors")]
|
|
text = " | ".join(p if isinstance(p, str) else json.dumps(p) for p in parts if p)
|
|
detail = "api_error_status %s: %s" % (status, text[:200] if text else "(no text)")
|
|
if status == 429:
|
|
return "usage_limit", detail
|
|
if status == 529:
|
|
return "overloaded", detail
|
|
for reason, rx in _API_ERROR_TEXT:
|
|
if rx.search(text):
|
|
return reason, detail
|
|
if status is not None:
|
|
return "api_error", detail
|
|
return None
|
|
|
|
|
|
def classify(ss: StreamState, exit_code, stopped: bool) -> tuple[str, str | None]:
|
|
"""(status, reason) of a finished call; the first matching case wins."""
|
|
res = ss.result
|
|
match (bool(stopped), res, api_error_reason(res), exit_code):
|
|
case (True, _, _, _):
|
|
return "stopped", "stopped"
|
|
case (_, None, _, code):
|
|
return "error", "exit_code" if code else "no_result"
|
|
case (_, _, (reason, _), _):
|
|
return "error", reason
|
|
case (_, dict() as r, _, _) if r.get("is_error"):
|
|
return "error", "is_error"
|
|
case (_, _, _, code) if code:
|
|
return "error", "exit_code"
|
|
case _:
|
|
return "ok", None
|
|
|
|
|
|
def usage_from_result(result: dict, before_mu: dict, cost_before) -> dict:
|
|
"""Tokens (with the 1h/5m split) from `result.usage`; `models_used` = modelUsage entries that
|
|
grew against the session's totals before the call, largest first; cost delta against the
|
|
session's cost before the call. Costs are rounded here, once, at 6 decimals."""
|
|
result = result or {}
|
|
usage = result.get("usage") or {}
|
|
cc = usage.get("cache_creation") or {}
|
|
tokens = {
|
|
"input": int(usage.get("input_tokens") or 0),
|
|
"output": int(usage.get("output_tokens") or 0),
|
|
"cache_creation": int(usage.get("cache_creation_input_tokens") or 0),
|
|
"cache_read": int(usage.get("cache_read_input_tokens") or 0),
|
|
"cache_creation_1h": int(cc.get("ephemeral_1h_input_tokens") or 0),
|
|
"cache_creation_5m": int(cc.get("ephemeral_5m_input_tokens") or 0),
|
|
}
|
|
after = result.get("modelUsage") or {}
|
|
before_mu = before_mu or {}
|
|
delta = {}
|
|
for m, v in after.items():
|
|
b = before_mu.get(m) or {}
|
|
delta[m] = {f: int((v or {}).get(f) or 0) - int(b.get(f) or 0) for f in MU_FIELDS}
|
|
grew = [m for m, d in delta.items() if any(x > 0 for x in d.values())]
|
|
models_used = sorted(grew, key=lambda m: (-sum(delta[m].values()), m))
|
|
total = result.get("total_cost_usd")
|
|
if total is None:
|
|
cost_delta = None
|
|
else:
|
|
cost_delta = round(float(total) - float(cost_before or 0.0), 6)
|
|
total = round(float(total), 6)
|
|
return {"tokens": tokens, "models_used": models_used,
|
|
"cost_delta": cost_delta, "total": total, "model_usage": after}
|
|
|
|
|
|
def cache_verdict(first_usage: dict | None, base_ctx) -> str | None:
|
|
"""The first request's cache read against the base's context tokens."""
|
|
if first_usage is None:
|
|
return None
|
|
cr = int(first_usage.get("cache_read_input_tokens") or 0)
|
|
if base_ctx and cr >= CACHE_HIT_RATIO * float(base_ctx):
|
|
return "hit"
|
|
return "partial" if cr > 0 else "miss"
|
|
|
|
|
|
def infer_ttl(tokens: dict | None, prev):
|
|
"""The TTL of a base's cache entries from one call's tokens: 1h written -> 3600, only 5m
|
|
written -> 300, otherwise (read only, or nothing) `prev`."""
|
|
if not tokens:
|
|
return prev
|
|
if int(tokens.get("cache_creation_1h") or 0) > 0:
|
|
return TTL_1H
|
|
if int(tokens.get("cache_creation_5m") or 0) > 0:
|
|
return TTL_5M
|
|
return prev
|
|
|
|
|
|
class CodexStreamState:
|
|
"""What one `codex exec --json` stream told us so far; the same attributes as StreamState, so
|
|
`classify` and the cli read it unchanged (design §3.3). `first_usage`/`last_usage` are not
|
|
used on Codex (the verdict comes from the turn delta)."""
|
|
|
|
def __init__(self, model: str):
|
|
self.model = model
|
|
self.session_id = None
|
|
self.first_usage = None
|
|
self.first_at = None # first item.* event: the base prefix was read
|
|
self.last_usage = None # the last turn.completed usage (running totals)
|
|
self.last_assistant_at = None
|
|
self.last_text = None # the last agent_message
|
|
self.result = None
|
|
|
|
def feed(self, ev: dict, t: float | None) -> None:
|
|
if not isinstance(ev, dict):
|
|
return
|
|
ty = ev.get("type")
|
|
if not isinstance(ty, str):
|
|
return
|
|
if ty == "thread.started":
|
|
if self.session_id is None:
|
|
self.session_id = ev.get("thread_id")
|
|
elif ty.startswith("item."):
|
|
if self.first_at is None:
|
|
self.first_at = t
|
|
item = ev.get("item") or {}
|
|
if ty == "item.completed" and isinstance(item, dict) and item.get("type") == "agent_message":
|
|
self.last_text = item.get("text")
|
|
elif ty == "turn.completed":
|
|
usage = ev.get("usage") if isinstance(ev.get("usage"), dict) else {}
|
|
self.last_usage, self.last_assistant_at = usage, t
|
|
self.result = {"type": "result", "is_error": False, "session_id": self.session_id,
|
|
"result": self.last_text, "usage": usage,
|
|
"modelUsage": {self.model: codex_model_usage(usage)}, "total_cost_usd": None}
|
|
if self.session_id is None: # a base without an id would make every fork `fork None`
|
|
self.result.update(is_error=True, result="no thread.started event")
|
|
elif ty in ("turn.failed", "error"):
|
|
err = ev.get("error") if ty == "turn.failed" else ev
|
|
msg = err.get("message") if isinstance(err, dict) else None
|
|
self.result = {"type": "result", "is_error": True, "session_id": self.session_id,
|
|
"result": msg if isinstance(msg, str) and msg else ty, "usage": None,
|
|
"modelUsage": {}, "total_cost_usd": None}
|
|
|
|
|
|
def codex_model_usage(usage: dict | None) -> dict:
|
|
"""Codex running totals in Claude's `modelUsage` keys; cached input is a subset of input."""
|
|
usage = usage or {}
|
|
inp, cached = int(usage.get("input_tokens") or 0), int(usage.get("cached_input_tokens") or 0)
|
|
return {"inputTokens": inp - cached, "outputTokens": int(usage.get("output_tokens") or 0),
|
|
"cacheReadInputTokens": cached,
|
|
"cacheCreationInputTokens": int(usage.get("cache_write_input_tokens") or 0)}
|
|
|
|
|
|
def codex_usage(result: dict, before_mu: dict | None, cost_before) -> dict:
|
|
"""`usage_from_result`'s shape from a Codex result. `turn.completed.usage` is the thread's
|
|
running total, base included, so `tokens` = mapped totals minus `before_mu` (None for train:
|
|
the whole total counts). No 1h/5m split and no cost on Codex. A result without usage (a
|
|
failed turn) gives zero tokens and no model usage."""
|
|
result = result or {}
|
|
after = result.get("modelUsage") if isinstance(result.get("usage"), dict) else None
|
|
after = after or {}
|
|
before_mu = before_mu or {}
|
|
delta = {}
|
|
for m, v in after.items():
|
|
b = before_mu.get(m) or {}
|
|
delta[m] = {f: int((v or {}).get(f) or 0) - int(b.get(f) or 0) for f in MU_FIELDS}
|
|
tot = {f: sum(d[f] for d in delta.values()) for f in MU_FIELDS}
|
|
tokens = {"input": tot["inputTokens"], "output": tot["outputTokens"],
|
|
"cache_creation": tot["cacheCreationInputTokens"], "cache_read": tot["cacheReadInputTokens"],
|
|
"cache_creation_1h": 0, "cache_creation_5m": 0}
|
|
grew = [m for m, d in delta.items() if any(x > 0 for x in d.values())]
|
|
models_used = sorted(grew, key=lambda m: (-sum(delta[m].values()), m))
|
|
return {"tokens": tokens, "models_used": models_used, "cost_delta": None, "total": None,
|
|
"model_usage": after}
|
|
|
|
|
|
def codex_cache_verdict(tokens: dict | None, base_ctx) -> str | None:
|
|
"""An estimate from the turn delta (ephemeral forks write no rollout): `hit` when the call
|
|
read at least 0.9 of the base context from cache and its uncached input stayed under half of
|
|
it; `partial` on any cache read; else `miss`. A large fork reads `partial`."""
|
|
if tokens is None:
|
|
return None
|
|
cr, inp = int(tokens.get("cache_read") or 0), int(tokens.get("input") or 0)
|
|
if base_ctx and cr >= CACHE_HIT_RATIO * float(base_ctx) and inp < 0.5 * float(base_ctx):
|
|
return "hit"
|
|
return "partial" if cr > 0 else "miss"
|
|
|
|
|
|
def warmth(last_hit_ts: float | None, ttl_s, now: float) -> dict:
|
|
"""warm until `margin` before last_hit + ttl, expiring until last_hit + ttl, then cold;
|
|
margin = max(120, ttl/10). No hit at all is cold."""
|
|
if last_hit_ts is None:
|
|
return {"base_state": "cold", "warm_until": None, "ttl_left_s": None}
|
|
ttl = float(ttl_s or TTL_1H)
|
|
margin = max(120.0, ttl / 10.0)
|
|
wu = float(last_hit_ts) + ttl
|
|
state = "warm" if now < wu - margin else ("expiring" if now < wu else "cold")
|
|
return {"base_state": state, "warm_until": iso(wu), "ttl_left_s": int(max(0.0, wu - now))}
|