@giamme/forge 0.12.0 → 0.13.0
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/CHANGELOG.md +77 -0
- package/README.md +1 -1
- package/SKILL.md +31 -12
- package/VERSION +1 -1
- package/docs/add-ons.md +11 -6
- package/docs/artifacts.md +18 -6
- package/docs/commands.md +189 -17
- package/docs/decomposed-runs.md +13 -5
- package/docs/forge-ui.md +34 -4
- package/docs/memory.md +2 -1
- package/docs/models.md +4 -2
- package/docs/reviews.md +81 -19
- package/docs/troubleshooting.md +3 -1
- package/forge +17 -1
- package/package.json +1 -1
- package/references/dataset.md +40 -14
- package/references/decision-models.md +759 -0
- package/references/decompose.md +197 -28
- package/references/harnesses.md +10 -3
- package/references/planning.md +8 -0
- package/references/solo.md +13 -3
- package/references/ui.md +11 -0
- package/scripts/forge-artifact.sh +30 -4
- package/scripts/forge-config.py +238 -29
- package/scripts/forge-contract.py +127 -58
- package/scripts/forge-control.py +40 -9
- package/scripts/forge-decide.py +6 -0
- package/scripts/forge-dispatch.sh +47 -15
- package/scripts/forge-fail-first.py +2 -2
- package/scripts/forge-gate-addressed.sh +40 -0
- package/scripts/forge-gate-message.sh +64 -0
- package/scripts/forge-issues.py +368 -36
- package/scripts/forge-jev-options.sh +269 -42
- package/scripts/forge-lib-attribute.sh +447 -0
- package/scripts/forge-lib-gates.sh +32 -0
- package/scripts/forge-lib-plans.sh +17 -6
- package/scripts/forge-lib-review.sh +24 -19
- package/scripts/forge-lib-task.sh +219 -39
- package/scripts/forge-lib-verify.sh +45 -8
- package/scripts/forge-memory.sh +3 -3
- package/scripts/forge-parallel-options.sh +44 -5
- package/scripts/forge-parallel.sh +503 -304
- package/scripts/forge-plan-check.py +35 -1
- package/scripts/forge-pre-review.py +437 -0
- package/scripts/forge-prompt.py +25 -5
- package/scripts/forge-route.py +2 -2
- package/scripts/forge-runtime.py +55 -1
- package/scripts/forge-schedule.py +70 -9
- package/scripts/forge-shadow.py +2 -2
- package/scripts/forge-solo.sh +41 -10
- package/scripts/forge-submodules.py +389 -0
- package/scripts/forge-sweep.py +542 -0
- package/scripts/forge_dataset/cli.py +12 -6
- package/scripts/forge_dataset/export.py +55 -45
- package/scripts/forge_dataset/forecast.py +4 -1
- package/scripts/forge_dataset/harvest.py +33 -5
- package/scripts/{forge_jev/outcome.py → forge_dataset/states.py} +21 -7
- package/scripts/forge_decide/__init__.py +141 -0
- package/scripts/forge_decide/budget.py +149 -0
- package/scripts/forge_decide/cache.py +66 -0
- package/scripts/forge_decide/cli.py +953 -0
- package/scripts/forge_decide/doctor.py +221 -0
- package/scripts/forge_decide/label_addressed.py +75 -0
- package/scripts/forge_decide/label_attribution.py +62 -0
- package/scripts/forge_decide/label_coverage.py +81 -0
- package/scripts/forge_decide/label_message.py +38 -0
- package/scripts/forge_decide/label_prompt.py +139 -0
- package/scripts/forge_decide/labels.py +328 -0
- package/scripts/forge_decide/metrics.py +125 -0
- package/scripts/forge_decide/migrate.py +226 -0
- package/scripts/forge_decide/providers.py +292 -0
- package/scripts/forge_decide/replay.py +350 -0
- package/scripts/forge_decide/report.py +382 -0
- package/scripts/forge_decide/resolve.py +397 -0
- package/scripts/forge_decide/sites.py +377 -0
- package/scripts/forge_jev/__init__.py +202 -104
- package/scripts/forge_jev/addressed.py +171 -0
- package/scripts/forge_jev/attribution.py +198 -0
- package/scripts/forge_jev/cli.py +460 -382
- package/scripts/forge_jev/client.py +155 -77
- package/scripts/forge_jev/coupling.py +19 -87
- package/scripts/forge_jev/coverage.py +303 -0
- package/scripts/forge_jev/findings.py +219 -0
- package/scripts/forge_jev/gates.py +225 -27
- package/scripts/forge_jev/memory.py +65 -4
- package/scripts/forge_jev/message.py +134 -0
- package/scripts/forge_jev/plan_gates.py +294 -0
- package/scripts/forge_jev/questions.py +173 -313
- package/scripts/forge_jev/verify.py +130 -104
- package/ui/README.md +41 -10
- package/ui/docs/api.md +212 -44
- package/ui/forge_ui/checks.py +331 -0
- package/ui/forge_ui/control.py +60 -11
- package/ui/forge_ui/export.py +8 -3
- package/ui/forge_ui/models.py +1 -0
- package/ui/forge_ui/reader.py +436 -26
- package/ui/forge_ui/server.py +13 -0
- package/ui/forge_ui/static/app.js +15 -8
- package/ui/forge_ui/static/checksview.js +449 -0
- package/ui/forge_ui/static/controls.js +20 -13
- package/ui/forge_ui/static/convo.js +11 -2
- package/ui/forge_ui/static/findingsview.js +6 -4
- package/ui/forge_ui/static/index.html +1 -0
- package/ui/forge_ui/static/panel.js +105 -17
- package/ui/forge_ui/static/runmodel.js +40 -2
- package/ui/forge_ui/static/scene3d.js +2 -15
- package/ui/forge_ui/static/settings.js +336 -41
- package/ui/forge_ui/static/style.css +72 -7
- package/ui/forge_ui/transcripts.py +225 -54
- package/references/jev.md +0 -865
- package/scripts/forge_jev/backtest.py +0 -322
- package/scripts/forge_jev/corpus.py +0 -156
- package/scripts/forge_jev/features.py +0 -24
- package/scripts/forge_jev/predict.py +0 -204
- package/scripts/forge_jev/review.py +0 -73
- package/scripts/forge_jev/routing.py +0 -271
- package/scripts/forge_jev/subset.py +0 -123
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
"""`forge decide report`: what each decision-model site cost and how often it was right (#37).
|
|
2
|
+
|
|
3
|
+
collect_runs(...) which run dirs to read: --run DIRs, else the newest N of the repo's
|
|
4
|
+
run registry (every repo's with --all), newest first by mtime
|
|
5
|
+
summarize(...) one record per site over every jev.jsonl of those runs
|
|
6
|
+
render(...) the text table
|
|
7
|
+
|
|
8
|
+
Each run dir's jev.jsonl files are the plan or run dir's own, `tasks/*/` and `integrate-*/`;
|
|
9
|
+
a path with a `jev-backtests` component is never read. Per site:
|
|
10
|
+
|
|
11
|
+
calls, cached, errors, skipped: {reason: n}
|
|
12
|
+
every entry is a call; a failed one with a reason is a skip, one without is an error
|
|
13
|
+
input_tokens, output_tokens
|
|
14
|
+
from the entry's usage
|
|
15
|
+
cost_usd
|
|
16
|
+
tokens x the user decide.json price of the entry's provider; None when any entry
|
|
17
|
+
names a provider with no price
|
|
18
|
+
fired, fired_unknown
|
|
19
|
+
calls the site's `fired` turns into a non-empty list at the threshold for the entry's
|
|
20
|
+
model. verify-discover needs the stored request body, looked up in the dataset by
|
|
21
|
+
`key`; a call whose body is not there counts under fired_unknown instead. The
|
|
22
|
+
request a `fired` gets is {model, questions: the ids asked}, so one that resolves its
|
|
23
|
+
own thresholds reads them for the model the entry was measured on
|
|
24
|
+
effects: {prefix: n}
|
|
25
|
+
what the fired ids did: the count of fired qids by prefix (the letters before any
|
|
26
|
+
digits) -- drift {f: n}, memory-curate {durable: n, same: n}, memory-stale {still: n};
|
|
27
|
+
None for a retired site
|
|
28
|
+
labelled: {decisions, fired_right, fired_wrong, missed}
|
|
29
|
+
labels.collect's labelled decisions of these runs (run uid = first line of the run
|
|
30
|
+
dir's dataset.uid), joined to the run's entry by key; None for a site with no label
|
|
31
|
+
mode, forced
|
|
32
|
+
the gate's resolved mode for the current repo, and whether it is `on` with no passing
|
|
33
|
+
replay of its provider and model (cli.status_data's rule)
|
|
34
|
+
|
|
35
|
+
A site with no registry entry (retired: jev-score-plan, clef-first-pass, ...) gets calls,
|
|
36
|
+
tokens and cost; its fired, labelled and mode are None.
|
|
37
|
+
|
|
38
|
+
Read-only: the dataset store is opened read-only, and nothing here asks a model.
|
|
39
|
+
"""
|
|
40
|
+
from __future__ import annotations
|
|
41
|
+
|
|
42
|
+
from collections import Counter
|
|
43
|
+
import json
|
|
44
|
+
import os
|
|
45
|
+
from pathlib import Path
|
|
46
|
+
import re
|
|
47
|
+
|
|
48
|
+
from . import load_user
|
|
49
|
+
from . import labels as labels_module
|
|
50
|
+
from . import sites as sites_module
|
|
51
|
+
from .resolve import resolve
|
|
52
|
+
|
|
53
|
+
DEFAULT_RUNS = 10
|
|
54
|
+
BACKTESTS = 'jev-backtests'
|
|
55
|
+
COUNTS = ('calls', 'cached', 'errors', 'input_tokens', 'output_tokens', 'fired', 'fired_unknown')
|
|
56
|
+
LABELLED = ('decisions', 'fired_right', 'fired_wrong', 'missed')
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# ------------------------------------------------------------------------------- runs
|
|
60
|
+
|
|
61
|
+
def _registered(path: Path) -> list[str]:
|
|
62
|
+
"""The run dirs one registry file lists (absolute paths, comments skipped)."""
|
|
63
|
+
try:
|
|
64
|
+
lines = path.read_text(encoding='utf-8', errors='replace').splitlines()
|
|
65
|
+
except OSError:
|
|
66
|
+
return []
|
|
67
|
+
return [line.strip() for line in lines
|
|
68
|
+
if line.strip() and not line.startswith('#') and os.path.isabs(line.strip())]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _mtime(path: str) -> float:
|
|
72
|
+
try:
|
|
73
|
+
return os.stat(path).st_mtime
|
|
74
|
+
except OSError:
|
|
75
|
+
return 0.0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def collect_runs(*, repo=None, dirs=(), every=False, limit=DEFAULT_RUNS) -> tuple[list[str], int]:
|
|
79
|
+
"""(run dirs newest first, how many listed dirs no longer exist).
|
|
80
|
+
|
|
81
|
+
`dirs` names the runs directly and ignores the registry. Otherwise the registry of
|
|
82
|
+
`repo`, or every repo's with `every`, gives the dirs, of which the newest `limit` are kept.
|
|
83
|
+
"""
|
|
84
|
+
import forge_registry
|
|
85
|
+
if dirs:
|
|
86
|
+
listed = [os.path.abspath(d) for d in dirs]
|
|
87
|
+
limit = None
|
|
88
|
+
elif every:
|
|
89
|
+
root = forge_registry.registry_dir()
|
|
90
|
+
files = sorted(root.glob('*.txt')) if root.is_dir() else []
|
|
91
|
+
listed = [d for path in files for d in _registered(path)]
|
|
92
|
+
else:
|
|
93
|
+
listed = _registered(forge_registry.runs_registry(repo))
|
|
94
|
+
seen, present, missing = set(), [], 0
|
|
95
|
+
for d in listed:
|
|
96
|
+
key = os.path.realpath(d)
|
|
97
|
+
if key in seen:
|
|
98
|
+
continue
|
|
99
|
+
seen.add(key)
|
|
100
|
+
if os.path.isdir(d):
|
|
101
|
+
present.append(d)
|
|
102
|
+
else:
|
|
103
|
+
missing += 1
|
|
104
|
+
if limit is not None:
|
|
105
|
+
present = sorted(present, key=_mtime, reverse=True)[:limit]
|
|
106
|
+
return present, missing
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def jev_logs(run_dir) -> list[Path]:
|
|
110
|
+
"""The run dir's jev.jsonl files: its own, tasks/*/ and integrate-*/, none under
|
|
111
|
+
jev-backtests."""
|
|
112
|
+
root = Path(os.path.abspath(run_dir))
|
|
113
|
+
found = [root / 'jev.jsonl', *sorted(root.glob('tasks/*/jev.jsonl')), *sorted(root.glob('integrate-*/jev.jsonl'))]
|
|
114
|
+
return [path for path in found if path.is_file() and BACKTESTS not in path.parts]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def run_uid(run_dir) -> str | None:
|
|
118
|
+
"""The run's dataset uid: the first line of its dataset.uid."""
|
|
119
|
+
try:
|
|
120
|
+
with open(Path(run_dir) / 'dataset.uid', encoding='utf-8', errors='replace') as handle:
|
|
121
|
+
return handle.readline().strip() or None
|
|
122
|
+
except OSError:
|
|
123
|
+
return None
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _entries(path: Path):
|
|
127
|
+
try:
|
|
128
|
+
with path.open(encoding='utf-8', errors='replace') as handle:
|
|
129
|
+
for line in handle:
|
|
130
|
+
try:
|
|
131
|
+
entry = json.loads(line)
|
|
132
|
+
except ValueError:
|
|
133
|
+
continue
|
|
134
|
+
if isinstance(entry, dict):
|
|
135
|
+
yield entry
|
|
136
|
+
except OSError:
|
|
137
|
+
return
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# ------------------------------------------------------------------------------- summary
|
|
141
|
+
|
|
142
|
+
def _at(entry: dict) -> float:
|
|
143
|
+
"""The entry's log time; an entry without one sorts last."""
|
|
144
|
+
value = entry.get('at')
|
|
145
|
+
return float(value) if isinstance(value, (int, float)) and not isinstance(value, bool) else float('inf')
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _tokens(usage, field) -> int:
|
|
149
|
+
value = usage.get(field) if isinstance(usage, dict) else None
|
|
150
|
+
return int(value) if isinstance(value, (int, float)) and not isinstance(value, bool) and value > 0 else 0
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _prices() -> dict:
|
|
154
|
+
"""{provider: USD per million tokens} from the user decide.json."""
|
|
155
|
+
stored = load_user()[0].get('providers')
|
|
156
|
+
out = {}
|
|
157
|
+
for name, entry in (stored.items() if isinstance(stored, dict) else ()):
|
|
158
|
+
price = entry.get('price_usd_per_mtok') if isinstance(entry, dict) else None
|
|
159
|
+
if isinstance(price, (int, float)) and not isinstance(price, bool) and price >= 0:
|
|
160
|
+
out[name] = float(price)
|
|
161
|
+
return out
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class _Store:
|
|
165
|
+
"""The dataset store, opened read-only once, on first use; None when there is none."""
|
|
166
|
+
|
|
167
|
+
def __init__(self):
|
|
168
|
+
self._store = False
|
|
169
|
+
self._bodies = None
|
|
170
|
+
|
|
171
|
+
def get(self):
|
|
172
|
+
if self._store is False:
|
|
173
|
+
try:
|
|
174
|
+
self._store = labels_module._open_store()
|
|
175
|
+
except Exception:
|
|
176
|
+
self._store = None
|
|
177
|
+
return self._store
|
|
178
|
+
|
|
179
|
+
def body(self, key) -> dict | None:
|
|
180
|
+
"""The stored request body of a request key."""
|
|
181
|
+
if self._bodies is None:
|
|
182
|
+
self._bodies = {}
|
|
183
|
+
store = self.get()
|
|
184
|
+
for event in (store.of('jev') if store is not None else ()):
|
|
185
|
+
d = event['d']
|
|
186
|
+
if d.get('key') and d.get('key') not in self._bodies:
|
|
187
|
+
body = labels_module._body(store, (d.get('blobs') or {}).get('request'))
|
|
188
|
+
if body is not None:
|
|
189
|
+
self._bodies[d['key']] = body
|
|
190
|
+
return self._bodies.get(key)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _blank(retired: bool) -> dict:
|
|
194
|
+
row = dict(calls=0, cached=0, errors=0, skipped={}, input_tokens=0, output_tokens=0, cost_usd=0.0,
|
|
195
|
+
fired=0, fired_unknown=0, effects={}, labelled=None, mode=None, forced=None, retired=retired)
|
|
196
|
+
if retired:
|
|
197
|
+
row.update(fired=None, fired_unknown=None, effects=None)
|
|
198
|
+
return row
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def summarize(run_dirs, *, repo=None, site=None) -> dict:
|
|
202
|
+
"""{runs: [{dir, uid}], sites: {site: record}, totals: record} over the run dirs
|
|
203
|
+
(record shape in the module docstring). `site` keeps one site only."""
|
|
204
|
+
import forge_jev
|
|
205
|
+
forge_jev.use_repo(repo)
|
|
206
|
+
prices = _prices()
|
|
207
|
+
store = _Store()
|
|
208
|
+
thresholds = {}
|
|
209
|
+
rows: dict = {}
|
|
210
|
+
# (site, run uid, key) -> (at, fired list) of the run's earliest answered entry for that
|
|
211
|
+
# key by `at`, the request labels.collect labels (the first per key by ts)
|
|
212
|
+
answered: dict = {}
|
|
213
|
+
runs = []
|
|
214
|
+
for run_dir in run_dirs:
|
|
215
|
+
uid = run_uid(run_dir)
|
|
216
|
+
runs.append(dict(dir=str(run_dir), uid=uid))
|
|
217
|
+
for path in jev_logs(run_dir):
|
|
218
|
+
for entry in _entries(path):
|
|
219
|
+
name = str(entry.get('site') or 'unknown')
|
|
220
|
+
if site is not None and name != site:
|
|
221
|
+
continue
|
|
222
|
+
record = sites_module.site(name)
|
|
223
|
+
row = rows.get(name)
|
|
224
|
+
if row is None:
|
|
225
|
+
row = rows[name] = _blank(record is None)
|
|
226
|
+
_count(row, entry, prices)
|
|
227
|
+
if record is None:
|
|
228
|
+
continue
|
|
229
|
+
fired = _fired(record, entry, thresholds, store)
|
|
230
|
+
if fired is None:
|
|
231
|
+
row['fired_unknown'] += 1
|
|
232
|
+
elif fired:
|
|
233
|
+
row['fired'] += 1
|
|
234
|
+
for qid in fired:
|
|
235
|
+
effect = re.match(r'[^0-9]*', str(qid)).group()
|
|
236
|
+
row['effects'][effect] = row['effects'].get(effect, 0) + 1
|
|
237
|
+
key = entry.get('key')
|
|
238
|
+
if fired is not None and entry.get('ok') is True and uid and key:
|
|
239
|
+
at = _at(entry)
|
|
240
|
+
held = answered.get((name, uid, key))
|
|
241
|
+
if held is None or at < held[0]:
|
|
242
|
+
answered[(name, uid, key)] = (at, set(fired))
|
|
243
|
+
|
|
244
|
+
if site is not None and site not in rows and sites_module.site(site) is not None:
|
|
245
|
+
rows[site] = _blank(False)
|
|
246
|
+
uids = {run['uid'] for run in runs if run['uid']}
|
|
247
|
+
resolved = resolve(repo)['gates']
|
|
248
|
+
forced = None
|
|
249
|
+
for name, row in rows.items():
|
|
250
|
+
record = sites_module.site(name)
|
|
251
|
+
if record is None:
|
|
252
|
+
continue
|
|
253
|
+
gate = resolved.get(record.gate)
|
|
254
|
+
row['mode'] = gate['mode'] if gate else None
|
|
255
|
+
if gate and gate['mode'] == 'on':
|
|
256
|
+
if forced is None:
|
|
257
|
+
from .cli import status_data
|
|
258
|
+
forced = {g: v['forced'] for g, v in status_data(repo)['gates'].items()}
|
|
259
|
+
row['forced'] = forced.get(record.gate, False)
|
|
260
|
+
elif gate:
|
|
261
|
+
row['forced'] = False
|
|
262
|
+
if record.label is not None:
|
|
263
|
+
row['labelled'] = _labelled(name, uids, answered, store)
|
|
264
|
+
ordered = dict(sorted(rows.items(), key=lambda item: (-item[1]['input_tokens'], item[0])))
|
|
265
|
+
return dict(runs=runs, sites=ordered, totals=_totals(ordered.values()))
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _count(row: dict, entry: dict, prices: dict) -> None:
|
|
269
|
+
row['calls'] += 1
|
|
270
|
+
if entry.get('cached') is True:
|
|
271
|
+
row['cached'] += 1
|
|
272
|
+
if entry.get('ok') is not True:
|
|
273
|
+
reason = entry.get('reason')
|
|
274
|
+
if reason:
|
|
275
|
+
row['skipped'][str(reason)] = row['skipped'].get(str(reason), 0) + 1
|
|
276
|
+
else:
|
|
277
|
+
row['errors'] += 1
|
|
278
|
+
usage = entry.get('usage')
|
|
279
|
+
tokens_in, tokens_out = _tokens(usage, 'input_tokens'), _tokens(usage, 'output_tokens')
|
|
280
|
+
row['input_tokens'] += tokens_in
|
|
281
|
+
row['output_tokens'] += tokens_out
|
|
282
|
+
if row['cost_usd'] is not None:
|
|
283
|
+
price = prices.get(entry.get('provider'))
|
|
284
|
+
row['cost_usd'] = None if price is None else row['cost_usd'] + (tokens_in + tokens_out) * price / 1e6
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _fired(record, entry: dict, thresholds: dict, store: _Store) -> list | None:
|
|
288
|
+
"""The question ids the entry's answers fired; None when that cannot be known."""
|
|
289
|
+
answers = entry.get('answers')
|
|
290
|
+
if entry.get('ok') is not True or not isinstance(answers, dict):
|
|
291
|
+
return []
|
|
292
|
+
model = entry.get('model')
|
|
293
|
+
if (record.name, model) not in thresholds:
|
|
294
|
+
import forge_jev
|
|
295
|
+
thresholds[(record.name, model)] = (
|
|
296
|
+
forge_jev.threshold(record.threshold, site=record.name, model=model) if record.threshold else None)
|
|
297
|
+
value = thresholds[(record.name, model)]
|
|
298
|
+
if record.threshold and value is None:
|
|
299
|
+
return None
|
|
300
|
+
asked = entry.get('questions') if isinstance(entry.get('questions'), list) else []
|
|
301
|
+
request = dict(model=model, questions=asked)
|
|
302
|
+
if record.name == 'jev-verify-discover':
|
|
303
|
+
request = store.body(entry.get('key'))
|
|
304
|
+
if request is None:
|
|
305
|
+
return None
|
|
306
|
+
elif record.name == 'jev-attribution':
|
|
307
|
+
# a tie at the top is broken by the merge order, which only the stored request's state keeps
|
|
308
|
+
request = store.body(entry.get('key')) or request
|
|
309
|
+
fired = record.fired(answers, value, request)
|
|
310
|
+
return None if fired is None else list(fired)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _labelled(name: str, uids: set, answered: dict, store: _Store) -> dict:
|
|
314
|
+
out = dict.fromkeys(LABELLED, 0)
|
|
315
|
+
data = store.get() if uids else None
|
|
316
|
+
if data is None:
|
|
317
|
+
return out
|
|
318
|
+
for row in labels_module.collect(name, store=data, runs=uids):
|
|
319
|
+
held = answered.get((name, row['run'], row['key']))
|
|
320
|
+
if held is None:
|
|
321
|
+
continue
|
|
322
|
+
fired = held[1]
|
|
323
|
+
for qid, truth in row['labels'].items():
|
|
324
|
+
out['decisions'] += 1
|
|
325
|
+
if qid in fired:
|
|
326
|
+
out['fired_right' if truth else 'fired_wrong'] += 1
|
|
327
|
+
elif truth:
|
|
328
|
+
out['missed'] += 1
|
|
329
|
+
return out
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _totals(rows) -> dict:
|
|
333
|
+
rows = list(rows)
|
|
334
|
+
out = {field: sum(row[field] or 0 for row in rows) for field in COUNTS}
|
|
335
|
+
skipped = Counter()
|
|
336
|
+
for row in rows:
|
|
337
|
+
skipped.update(row['skipped'])
|
|
338
|
+
out['skipped'] = dict(sorted(skipped.items()))
|
|
339
|
+
costs = [row['cost_usd'] for row in rows]
|
|
340
|
+
out['cost_usd'] = None if any(cost is None for cost in costs) else sum(costs)
|
|
341
|
+
labelled = [row['labelled'] for row in rows if row['labelled'] is not None]
|
|
342
|
+
out['labelled'] = {field: sum(item[field] for item in labelled) for field in LABELLED}
|
|
343
|
+
return out
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
# ------------------------------------------------------------------------------- text
|
|
347
|
+
|
|
348
|
+
COLUMNS = ('site', 'mode', 'calls', 'cached', 'errors', 'skipped', 'in tokens', 'out tokens', 'cost $',
|
|
349
|
+
'fired', 'effects', 'right', 'wrong', 'missed')
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _cells(name: str, row: dict, mode=True) -> list[str]:
|
|
353
|
+
def number(value):
|
|
354
|
+
return '-' if value is None else f'{value:,}'
|
|
355
|
+
labelled = row.get('labelled')
|
|
356
|
+
fired = number(row.get('fired'))
|
|
357
|
+
if row.get('fired_unknown'):
|
|
358
|
+
fired += f' (+{row["fired_unknown"]}?)'
|
|
359
|
+
effects = ', '.join(f'{name} {n}' for name, n in sorted((row.get('effects') or {}).items())) or '-'
|
|
360
|
+
shown = (row.get('mode') or '-') + (' (forced)' if row.get('forced') else '')
|
|
361
|
+
return [name, shown if mode else '', number(row['calls']), number(row['cached']),
|
|
362
|
+
number(row['errors']), number(sum(row['skipped'].values())), number(row['input_tokens']),
|
|
363
|
+
number(row['output_tokens']), '-' if row['cost_usd'] is None else '%.4f' % row['cost_usd'], fired,
|
|
364
|
+
effects,
|
|
365
|
+
*(number(labelled[field]) if labelled else '-' for field in LABELLED[1:])]
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def render(data: dict) -> str:
|
|
369
|
+
"""One row per site (as summarize ordered them), a totals row, then the runs read."""
|
|
370
|
+
table = [list(COLUMNS)] + [_cells(name, row) for name, row in data['sites'].items()]
|
|
371
|
+
table.append(_cells('total', data['totals'], mode=False))
|
|
372
|
+
widths = [max(len(line[i]) for line in table) for i in range(len(COLUMNS))]
|
|
373
|
+
lines = [' '.join(cell.ljust(widths[i]) if i < 2 else cell.rjust(widths[i]) for i, cell in enumerate(line)).rstrip()
|
|
374
|
+
for line in table]
|
|
375
|
+
skips = data['totals']['skipped']
|
|
376
|
+
if skips:
|
|
377
|
+
lines.append('skipped: ' + ', '.join(f'{reason} {n}' for reason, n in skips.items()))
|
|
378
|
+
runs = data['runs']
|
|
379
|
+
gone = data.get('skipped_runs') or 0
|
|
380
|
+
lines.append('runs read: %d%s%s' % (len(runs), ' (%d listed dirs no longer exist)' % gone if gone else '',
|
|
381
|
+
': ' + ', '.join(run['dir'] for run in runs) if runs else ''))
|
|
382
|
+
return '\n'.join(lines)
|