compactlab 0.2.0__py3-none-any.whl

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.
Files changed (67) hide show
  1. compactlab/__init__.py +3 -0
  2. compactlab/adapters/__init__.py +2 -0
  3. compactlab/adapters/base.py +212 -0
  4. compactlab/adapters/cache.py +396 -0
  5. compactlab/adapters/claude_code.py +226 -0
  6. compactlab/adapters/codex.py +298 -0
  7. compactlab/adapters/dsh.py +287 -0
  8. compactlab/adapters/gemini_cli.py +259 -0
  9. compactlab/adapters/opencode.py +375 -0
  10. compactlab/apply/__init__.py +582 -0
  11. compactlab/cli/__init__.py +5 -0
  12. compactlab/cli/main.py +2436 -0
  13. compactlab/data/config_knobs.json +158 -0
  14. compactlab/data/harness_defaults.json +72 -0
  15. compactlab/data/harness_formats.json +300 -0
  16. compactlab/data/pricing.json +51 -0
  17. compactlab/engine/__init__.py +138 -0
  18. compactlab/engine/live.py +306 -0
  19. compactlab/engine/settings.py +175 -0
  20. compactlab/engine/watch.py +82 -0
  21. compactlab/eval/__init__.py +1 -0
  22. compactlab/eval/deep.py +390 -0
  23. compactlab/eval/executors.py +84 -0
  24. compactlab/eval/factcheck.py +75 -0
  25. compactlab/lens.py +80 -0
  26. compactlab/model/__init__.py +285 -0
  27. compactlab/pricing/__init__.py +211 -0
  28. compactlab/pricing/auto.py +118 -0
  29. compactlab/pricing/ids.py +148 -0
  30. compactlab/pricing/refresh.py +299 -0
  31. compactlab/publish.py +125 -0
  32. compactlab/py.typed +0 -0
  33. compactlab/recommend/__init__.py +201 -0
  34. compactlab/recommend/claude_code.py +172 -0
  35. compactlab/recommend/codex.py +104 -0
  36. compactlab/recommend/dsh.py +89 -0
  37. compactlab/recommend/gemini_cli.py +85 -0
  38. compactlab/recommend/opencode.py +117 -0
  39. compactlab/recommend/units.py +63 -0
  40. compactlab/report/__init__.py +3 -0
  41. compactlab/report/html.py +27 -0
  42. compactlab/report/template.html +1189 -0
  43. compactlab/service/__init__.py +1 -0
  44. compactlab/service/core.py +220 -0
  45. compactlab/service/http.py +133 -0
  46. compactlab/service/lock.py +92 -0
  47. compactlab/service/policy.py +258 -0
  48. compactlab/service/schedule.py +212 -0
  49. compactlab/share.py +553 -0
  50. compactlab/sim/__init__.py +1 -0
  51. compactlab/sim/baseline.py +214 -0
  52. compactlab/sim/calibrate.py +108 -0
  53. compactlab/sim/engine.py +1130 -0
  54. compactlab/sim/select.py +238 -0
  55. compactlab/sim/timing.py +134 -0
  56. compactlab/synth.py +416 -0
  57. compactlab/term/__init__.py +0 -0
  58. compactlab/term/style.py +134 -0
  59. compactlab/term/top.py +531 -0
  60. compactlab/term/views.py +598 -0
  61. compactlab/term/widgets.py +238 -0
  62. compactlab/verify.py +462 -0
  63. compactlab-0.2.0.dist-info/METADATA +243 -0
  64. compactlab-0.2.0.dist-info/RECORD +67 -0
  65. compactlab-0.2.0.dist-info/WHEEL +4 -0
  66. compactlab-0.2.0.dist-info/entry_points.txt +2 -0
  67. compactlab-0.2.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,238 @@
1
+ """Terminal widgets built from segment lines: header with a mark, boxed panels, threshold gauge, was/now bars,
2
+ sparkline, braille line chart, delta chips and chip packing. Everything takes a width and stays inside it."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from collections.abc import Sequence
7
+
8
+ from .style import Line, clip, cw, pad, width
9
+
10
+ MINUS = "−"
11
+ MARK = ["▗▄▄▄▄▄▖", "▐█████▌", "▐███▌ ", "▐██▌ ", "▝▀▀▘ "] # a context shrinking at a threshold
12
+ SPARK = "▁▂▃▄▅▆▇█"
13
+
14
+
15
+ # ---------- numbers: percent first, signed amounts, arrows on deltas only ----------
16
+
17
+
18
+ def k(n: float | None) -> str:
19
+ if n is None:
20
+ return "–"
21
+ return f"{n / 1e6:g}M" if n >= 1_000_000 else f"{round(n / 1000)}k"
22
+
23
+
24
+ def pabs(v: float) -> str:
25
+ a = abs(v * 100)
26
+ return f"{a:.1f}" if a < 10 else f"{a:.0f}"
27
+
28
+
29
+ def fpct(v: float | None, arrow: bool = True) -> str:
30
+ """'▼ −21%' / '▲ +3.0%' (a share, as a fraction); without the arrow: '−21%'."""
31
+ if v is None:
32
+ return "–"
33
+ if abs(v * 100) < 0.05:
34
+ return "0%"
35
+ sign = MINUS if v < 0 else "+"
36
+ return (("▼ " if v < 0 else "▲ ") if arrow else "") + f"{sign}{pabs(v)}%"
37
+
38
+
39
+ def fusd(v: float, arrow: bool = False) -> str:
40
+ if abs(v) < 0.05:
41
+ return "$0"
42
+ a = abs(v)
43
+ body = f"${a:.1f}" if a < 1000 else f"${a:,.0f}"
44
+ return (("▼ " if v < 0 else "▲ ") if arrow else "") + (MINUS if v < 0 else "+") + body
45
+
46
+
47
+ def fmin(v: float, arrow: bool = False) -> str:
48
+ if abs(v) < 0.05:
49
+ return "0 min"
50
+ a = abs(v)
51
+ return (("▼ " if v < 0 else "▲ ") if arrow else "") + (MINUS if v < 0 else "+") + f"{a:.1f} min"
52
+
53
+
54
+ def money(v: float) -> str:
55
+ """An absolute amount (no sign, no arrow): '$619', '$43.0'."""
56
+ return f"${v:,.0f}" if v >= 100 else f"${v:.1f}"
57
+
58
+
59
+ def tone(v: float | None) -> str | None:
60
+ if v is None or abs(v) < 1e-9:
61
+ return None
62
+ return "good" if v < 0 else "warn"
63
+
64
+
65
+ def chip(pct: float | None, amount: str | None, unit: str = "pct") -> Line:
66
+ """A delta: the lead figure bold and coloured by sign, the other one dim. `unit` picks the lead."""
67
+ t = tone(pct) if pct is not None else None
68
+ if unit == "pct" and pct is not None:
69
+ return [(fpct(pct), (t or "ink") + "_b")] + ([(" " + amount, "dim")] if amount else [])
70
+ return [(amount or "–", (t or "ink") + "_b")] + ([(" " + fpct(pct, arrow=False), "dim")] if pct is not None else [])
71
+
72
+
73
+ # ---------- layout ----------
74
+
75
+
76
+ def pack(tokens: Sequence[Line], cols: int, sep: str = " ") -> list[Line]:
77
+ """Tokens on as few rows as fit `cols`, breaking only between tokens (an over-wide token is clipped)."""
78
+ rows: list[Line] = []
79
+ row: Line = []
80
+ for tok in tokens:
81
+ tok = clip(tok, cols)
82
+ if row and width(row) + cw(sep) + width(tok) > cols:
83
+ rows.append(row)
84
+ row = []
85
+ row = row + ([(sep, None)] if row else []) + tok
86
+ if row:
87
+ rows.append(row)
88
+ return rows
89
+
90
+
91
+ def panel(title: Line, body: list[Line], cols: int) -> list[Line]:
92
+ """A rounded box `cols` wide with the title in the top edge."""
93
+ inner = max(cols - 4, 1)
94
+ t = clip(title, max(inner - 2, 1))
95
+ top: Line = [("╭─ ", "frame"), *t, (" " + "─" * max(inner - width(t) - 1, 0) + "╮", "frame")]
96
+ out = [top]
97
+ for ln in body:
98
+ out.append([("│ ", "frame"), *pad(ln, inner), (" │", "frame")])
99
+ out.append([("╰" + "─" * (cols - 2) + "╯", "frame")])
100
+ return out
101
+
102
+
103
+ def header(facts: list[tuple[str, Line]], cols: int, title: str = "compactlab") -> list[Line]:
104
+ """fastfetch-style: the mark on the left, key facts on the right (stacked under the mark when narrow)."""
105
+ mark: list[Line] = [[(m, "acc")] for m in MARK]
106
+ rows: list[Line] = [[(title, "acc_b")], [("─" * min(len(title) + 6, max(cols, 1)), "frame")]]
107
+ keyw = max((cw(key) for key, _ in facts), default=0)
108
+ for key, val in facts:
109
+ rows.append([(key + " " * (keyw - cw(key)) + " ", "dim"), *val])
110
+ if cols < 46:
111
+ return [clip(r, cols) for r in [*mark, [("", None)], *rows]]
112
+ gap = 3
113
+ side = cols - 7 - gap
114
+ out = []
115
+ for i in range(max(len(mark), len(rows))):
116
+ left = mark[i] if i < len(mark) else [(" " * 7, None)]
117
+ right = clip(rows[i], side) if i < len(rows) else []
118
+ out.append([*pad(left, 7), (" " * gap, None), *right])
119
+ return out
120
+
121
+
122
+ # ---------- charts ----------
123
+
124
+
125
+ def gauge(cols: int, lo: float, hi: float, marks: list[tuple[float, str, str]], unsafe: list[tuple[float, float]]
126
+ ) -> list[Line]: # fmt: skip
127
+ """A threshold axis lo..hi: '░' red where recall is below the floor, marks drawn on the bar, labels under it.
128
+
129
+ marks: (threshold, glyph, role); the label of an 'acc_b' mark (the selection) is placed first."""
130
+ n = max(cols, 10)
131
+ pos = lambda v: min(n - 1, max(0, round((v - lo) / ((hi - lo) or 1) * (n - 1)))) # noqa: E731
132
+ cells: list[tuple[str, str | None]] = [("─", "frame")] * n
133
+ for a, b in unsafe:
134
+ for i in range(pos(a), pos(b) + 1):
135
+ cells[i] = ("░", "bad")
136
+ labels: list[tuple[int, str, str]] = []
137
+ for v, glyph, role in marks:
138
+ cells[pos(v)] = (glyph, role)
139
+ labels.append((pos(v), k(v), role))
140
+ bar: Line = list(cells)
141
+ under: Line = [(" ", None)] * n
142
+ taken = [False] * n
143
+ for p, text, role in sorted(labels, key=lambda x: (x[2] != "acc_b", x[0])): # the selection's label first
144
+ s = max(0, min(p - len(text) // 2, n - len(text)))
145
+ if any(taken[s : s + len(text) + 1]):
146
+ continue
147
+ for j, ch in enumerate(text):
148
+ under[s + j] = (ch, role.replace("_b", ""))
149
+ taken[s + j] = True
150
+ # the axis ends go on the label row where they do not collide with a mark's label
151
+ for text, start in ((k(lo), 0), (k(hi), n - len(k(hi)))):
152
+ if not any(taken[max(start - 1, 0) : start + len(text) + 1]):
153
+ for j, ch in enumerate(text):
154
+ under[start + j] = (ch, "frame")
155
+ return [_merge(bar), _merge(under)]
156
+
157
+
158
+ def _merge(cells: Sequence[tuple[str, str | None]]) -> Line:
159
+ out: Line = []
160
+ for t, r in cells:
161
+ if out and out[-1][1] == r:
162
+ out[-1] = (out[-1][0] + t, r)
163
+ else:
164
+ out.append((t, r))
165
+ return out
166
+
167
+
168
+ def was_now(was: float, now: float, cols: int, scale: float | None = None) -> tuple[Line, Line]:
169
+ """Two bars on one scale: grey 'was', accent 'now' with the saving hatched green (or the addition amber)."""
170
+ top = max(scale or 0, was, now, 1e-9)
171
+ n = max(cols, 4)
172
+ wc, nc = max(round(was / top * n), 1 if was > 0 else 0), max(round(now / top * n), 1 if now > 0 else 0)
173
+ a: Line = [("█" * wc, "was"), ("·" * (n - wc), "frame")]
174
+ if nc <= wc:
175
+ b: Line = [("█" * nc, "acc"), ("░" * (wc - nc), "good"), ("·" * (n - wc), "frame")]
176
+ else:
177
+ b = [("█" * wc, "acc"), ("█" * (nc - wc), "warn"), ("·" * (n - nc), "frame")]
178
+ return _merge(a), _merge(b)
179
+
180
+
181
+ def spark(values: Sequence[float], cols: int, sel: int | None = None) -> Line:
182
+ """Block sparkline resampled to `cols`; the cell holding index `sel` is drawn in the accent."""
183
+ if not values:
184
+ return []
185
+ n = max(cols, 1)
186
+ lo, hi = min(values), max(values)
187
+ cells: list[tuple[str, str | None]] = []
188
+ for c in range(n):
189
+ i = min(len(values) - 1, int(c * len(values) / n))
190
+ v = values[i]
191
+ ch = SPARK[min(7, int((v - lo) / ((hi - lo) or 1) * 7.999))]
192
+ hit = sel is not None and min(len(values) - 1, int((c + 1) * len(values) / n)) > sel >= i
193
+ cells.append((ch, "acc_b" if hit else "dim"))
194
+ return _merge(cells)
195
+
196
+
197
+ BRAILLE = 0x2800
198
+ DOTS = ((0x01, 0x08), (0x02, 0x10), (0x04, 0x20), (0x40, 0x80)) # [row][col] bits of a 2x4 braille cell
199
+
200
+
201
+ def braille(series: list[tuple[Sequence[float], str]], cols: int, rows: int, lo: float, hi: float,
202
+ vlines: Sequence[tuple[int, str] | tuple[int, str, str]] = ()) -> list[Line]: # fmt: skip
203
+ """Line chart in braille dots (2x4 per cell). Each series: (values over the x range, role). `vlines`:
204
+ (column, role[, glyph]) markers drawn as the glyph (default '│') where no dot falls. Later series win a shared
205
+ cell's colour."""
206
+ w, h = max(cols, 2), max(rows, 1)
207
+ bits = [[0] * w for _ in range(h)]
208
+ role: list[list[str | None]] = [[None] * w for _ in range(h)]
209
+ for vals, r in series:
210
+ if not vals:
211
+ continue
212
+ pts = []
213
+ for x in range(w * 2):
214
+ fi = x / (w * 2 - 1 or 1) * (len(vals) - 1) # linear between samples, so the curve reads as a line
215
+ i = min(int(fi), len(vals) - 2) if len(vals) > 1 else 0
216
+ v = vals[i] + (vals[i + 1] - vals[i]) * (fi - i) if len(vals) > 1 else vals[0]
217
+ y = (v - lo) / ((hi - lo) or 1)
218
+ pts.append(min(h * 4 - 1, max(0, round((1 - y) * (h * 4 - 1)))))
219
+ for x in range(w * 2):
220
+ y0, y1 = pts[x], pts[x - 1] if x else pts[x]
221
+ for y in range(min(y0, y1), max(y0, y1) + 2): # join steps; one extra dot row makes the line heavier
222
+ y = min(y, h * 4 - 1)
223
+ cy, cx = divmod(y, 4)[0], x // 2
224
+ bits[cy][cx] |= DOTS[y % 4][x % 2]
225
+ role[cy][cx] = r
226
+ out: list[Line] = []
227
+ vl = {v[0]: (v[1], v[2] if len(v) > 2 else "│") for v in vlines}
228
+ for cy in range(h):
229
+ cells = []
230
+ for cx in range(w):
231
+ if bits[cy][cx]:
232
+ cells.append((chr(BRAILLE + bits[cy][cx]), role[cy][cx]))
233
+ elif cx in vl:
234
+ cells.append((vl[cx][1], vl[cx][0]))
235
+ else:
236
+ cells.append((" ", None))
237
+ out.append(_merge(cells))
238
+ return out