File size: 7,964 Bytes
c8e03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
874eec2
c8e03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
874eec2
 
 
 
 
 
 
 
 
 
c8e03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Claude via the Claude Code CLI in headless mode, with a content-addressed cache, a cost ledger and a hard budget.

Locked down for cost and isolation: no tools, a replaced system prompt, no user/project settings or MCP servers,
prompt caching disabled. Images go inline as base64 blocks. Cost per call is the CLI's `total_cost_usd`, a
list-price estimate. Identical requests are served from the cache at zero cost.
"""
from __future__ import annotations

import base64
import contextlib
import hashlib
import io
import json
import os
import subprocess
import threading
import time
from pathlib import Path

from . import config as C

_lock = threading.Lock()
_spent_lock = threading.Lock()
_tl = threading.local()   # per-thread diagnosis model override; unset means C.MODEL, so existing cache keys do not change
_budget = {"usd": 0.0}
LEDGER = C.RUNS / "cost_ledger.jsonl"


def active_model() -> str:
    return getattr(_tl, "model", None) or C.MODEL


@contextlib.contextmanager
def use_model(model: str | None):
    """Run the calls of this thread on another model. Cache keys carry the model, so replies never mix across models."""
    prev = getattr(_tl, "model", None); _tl.model = model
    try:
        yield
    finally:
        _tl.model = prev


class BudgetExceeded(RuntimeError):
    pass


class CallFailed(RuntimeError):
    """The CLI answered with an error (for example the account's usage-limit message). Never cached."""


_reserved = {"usd": 0.0}
_spent_cache = {"off": 0, "sum": 0.0}
LIMIT_MARKERS = ("usage limit", "limit reached", "rate limit", "hit your limit", "out of extra usage", "credit balance")


def looks_failed(text: str, is_error) -> bool:
    t = (text or "").lower()
    return bool(is_error) or (len(t) < 400 and any(m in t for m in LIMIT_MARKERS))


def sha(obj) -> str:
    s = obj if isinstance(obj, (bytes, str)) else json.dumps(obj, sort_keys=True)
    return hashlib.sha256(s.encode() if isinstance(s, str) else s).hexdigest()


def set_budget(total_usd: float) -> None:
    """Absolute ceiling on the ledger total (all paid calls ever made from this codebase)."""
    _budget["usd"] = float(total_usd)


def spent() -> float:
    """Ledger total of paid calls, read incrementally (the ledger is append-only)."""
    if not LEDGER.exists():
        return 0.0
    with _spent_lock:   # threads share the read offset; without the lock two readers can seek into the middle of a line
        with LEDGER.open() as f:
            f.seek(_spent_cache["off"])
            for line in f:
                if not line.endswith("\n"):
                    break
                r = json.loads(line); _spent_cache["off"] += len(line.encode())
                if not r["cache_hit"]:
                    _spent_cache["sum"] += r["cost_usd"] or 0.0
        return _spent_cache["sum"]


def reserve(usd: float) -> None:
    """Reserve a conservative maximum charge before dispatch; raise if spent + reserved would pass the budget."""
    with _lock:
        if spent() + _reserved["usd"] + usd > _budget["usd"]:
            raise BudgetExceeded(f"spent {spent():.2f} + reserved {_reserved['usd']:.2f} + {usd:.2f} > budget {_budget['usd']:.2f}")
        _reserved["usd"] += usd


def release(usd: float) -> None:
    with _lock:
        _reserved["usd"] = max(0.0, _reserved["usd"] - usd)


def image_block(path: str | Path, max_side: int = C.IMAGE_MAX_SIDE) -> dict:
    from PIL import Image
    p = C.CACHE / "img" / (sha(f"{Path(path).resolve()}|{max_side}") + ".jpg")
    if not p.exists():
        p.parent.mkdir(parents=True, exist_ok=True)
        im = Image.open(path).convert("RGB"); im.thumbnail((max_side, max_side))
        buf = io.BytesIO(); im.save(buf, "JPEG", quality=85); p.write_bytes(buf.getvalue())
    return {"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": base64.b64encode(p.read_bytes()).decode()}}


def _cli(system: str, content: list[dict]) -> dict:
    cmd = ["claude", "-p", "--model", active_model(), "--input-format", "stream-json", "--output-format", "stream-json", "--verbose",
           "--no-session-persistence", "--tools", "", "--strict-mcp-config", "--setting-sources", "", "--system-prompt", system]
    env = {k: v for k, v in os.environ.items() if not k.startswith(("CLAUDE", "CURSOR", "MCP_CONNECTION", "VSCODE", "ELECTRON"))}
    env["DISABLE_PROMPT_CACHING"] = "1"
    C.CACHE.mkdir(parents=True, exist_ok=True); t0 = time.time()
    msg = {"type": "user", "message": {"role": "user", "content": content}}
    proc = subprocess.run(cmd, input=json.dumps(msg).encode(), capture_output=True, env=env, timeout=300, cwd=str(C.CACHE))
    res = {}
    for line in proc.stdout.decode(errors="replace").splitlines():
        try:
            ev = json.loads(line)
        except json.JSONDecodeError:
            continue
        if ev.get("type") == "result":
            res = ev
    if not res:
        raise RuntimeError(f"no result event; rc={proc.returncode} {proc.stderr.decode()[:300]}")
    u = res.get("usage", {})
    return {"text": res.get("result", ""), "cost_usd": float(res.get("total_cost_usd") or 0.0),
            "input_tokens": u.get("input_tokens", 0), "output_tokens": u.get("output_tokens", 0),
            "model_ids": list((res.get("modelUsage") or {}).keys()), "is_error": res.get("is_error"),
            "elapsed_s": round(time.time() - t0, 2)}


RUNNER = _cli   # tests replace this with a fake


def _ledger(row: dict) -> None:
    LEDGER.parent.mkdir(parents=True, exist_ok=True)
    with _lock, LEDGER.open("a") as f:
        f.write(json.dumps(row) + "\n")


def call(system: str, content: list[dict], *, stage: str, tag: str = "", retries: int = 1, reserve_usd: float = 0.05) -> dict:
    """Cached call. Error and usage-limit answers are never cached; at most `retries` retries; each attempt is
    reserved against the budget before dispatch, and failed attempts that were charged are written to the ledger."""
    key = sha({"m": active_model(), "s": system, "c": content})
    cp = C.CACHE / "llm" / f"{key}.json"
    if cp.exists():
        out = json.loads(cp.read_text())
        if not looks_failed(out.get("text"), out.get("is_error")):
            out["cache_hit"] = True
            _ledger({"key": key, "stage": stage, "tag": tag, "cache_hit": True, "cost_usd": 0.0, "t": time.time()})
            return out
    err = None
    for attempt in range(retries + 1):
        reserve(reserve_usd)
        try:
            out = RUNNER(system, content)
        except Exception as e:  # transient CLI or network failure
            err = e; release(reserve_usd); time.sleep(2 * (attempt + 1)); continue
        release(reserve_usd)
        if looks_failed(out.get("text"), out.get("is_error")):
            _ledger({"key": key, "stage": stage, "tag": tag, "cache_hit": False, "failed": True, "cost_usd": out.get("cost_usd") or 0.0,
                     "elapsed_s": out.get("elapsed_s"), "text": (out.get("text") or "")[:200], "t": time.time()})
            err = CallFailed(out.get("text")); time.sleep(5 * (attempt + 1)); continue
        break
    else:
        raise err if isinstance(err, CallFailed) else RuntimeError(f"call failed after {retries + 1} attempts: {err}")
    out["parsed"] = parse_json(out["text"])
    cp.parent.mkdir(parents=True, exist_ok=True); cp.write_text(json.dumps(out))
    _ledger({"key": key, "stage": stage, "tag": tag, "cache_hit": False,
             **{k: out.get(k) for k in ("cost_usd", "input_tokens", "output_tokens", "elapsed_s", "model_ids")}, "t": time.time()})
    out["cache_hit"] = False
    return out


def parse_json(text: str):
    t = (text or "").strip()
    if "```" in t:
        t = next((s[4:] if s.startswith("json") else s for s in t.split("```") if "{" in s), t)
    i, j = t.find("{"), t.rfind("}")
    if i != -1 and j > i:
        try:
            return json.loads(t[i:j + 1])
        except json.JSONDecodeError:
            return None
    return None