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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# SPDX-License-Identifier: FSL-1.1-MIT
# Copyright (c) 2026 Cua AI, Inc.

"""Pure logic for the Claude Code benchmark runner: schedule, quota gate, spend ledger, backoff,
wall-clock cutoff and round bookkeeping. No GUI, no model calls, so it is unit-tested.

Pre-registered rules implemented here:

* Task-major blocks in a fixed priority order, never shuffled. Phase 1: for each task, runs 1-3, each
  run a pair of back-to-back trials (one per arm). Phase 2 (only after phase 1 is complete for every
  task and quota allows): for each task, runs 4-5 as a block of 2 pairs.
* For the task at 0-based position ``i`` and 0-based run ``r`` the first arm of the pair is
  ``arms[0]`` when ``(i + r)`` is even and ``arms[1]`` otherwise. Both arms share the run's seed.
* A block is the unit of completeness. Only complete blocks are analysed; rows of an interrupted
  block are kept and flagged ``block_incomplete``.
* No dollar cap by default (subscription). ``total_cost_usd`` is recorded as equivalent cost.
* Stop starting trials when the 7-day quota reaches 0.95 or a quota event reads "rejected" on
  the 7-day window. A rejected 5-hour window waits for its reset instead.
"""

from __future__ import annotations

import fcntl
import json
import os
import re
import time
from datetime import datetime, timezone
from pathlib import Path
from typing import IO, Any, Iterable

SEVEN_DAY_STOP = 0.95
BACKOFF_BASE_S = 60.0
BACKOFF_CAP_S = 900.0
RESET_WAIT_MAX_S = 3 * 3600.0
OVERLOAD_RETRIES = 3


# ---------------------------------------------------------------- tasks and schedule


def priority_key(task_id: str) -> tuple[int, int, str]:
    """CDB-* first (primary result), then MB-NN probes in numeric order, anything else after, by name."""
    if task_id.startswith("CDB-"):
        return (0, 0, task_id)
    match = re.fullmatch(r"MB-(\d+)", task_id)
    if match:
        return (1, int(match.group(1)), task_id)
    return (2, 0, task_id)


def order_tasks(task_ids: Iterable[str], explicit: bool = False) -> list[str]:
    ids = list(task_ids)
    return ids if explicit else sorted(ids, key=priority_key)


def first_arm_index(task_pos: int, run_index: int, n_arms: int = 2) -> int:
    """Which arm runs first in the pair for task ``task_pos`` and run ``run_index`` (both 0-based):
    arms[0] when (task_pos + run_index) is even, else arms[1]."""
    if n_arms == 2:
        return 0 if (task_pos + run_index) % 2 == 0 else 1
    return (task_pos + run_index) % n_arms


def arm_order(task_pos: int, run_index: int, arms: list[str]) -> list[str]:
    start = first_arm_index(task_pos, run_index, len(arms))
    return arms[start:] + arms[:start]


def trial_id(task: str, run_index: int, arm: str) -> str:
    return f"{task}-r{run_index + 1}-{arm}"


def block_id(phase: int, task: str) -> str:
    return f"P{phase}-{task}"


def build_pair(
    task_ids: list[str], task: str, run_index: int, arms: list[str], schedule_seed: int, phase: int
) -> list[dict[str, Any]]:
    """The two trials (one per arm) of one run of one task, in execution order."""
    pos = task_ids.index(task)
    entries = []
    for slot, arm in enumerate(arm_order(pos, run_index, arms)):
        entries.append(
            {
                "trial_id": trial_id(task, run_index, arm),
                "phase": phase,
                "block": block_id(phase, task),
                "run_index": run_index,
                "run": run_index + 1,
                "task": task,
                "arm": arm,
                "task_pos": pos,
                "arm_slot": slot,
                "first_arm": slot == 0,
                "schedule_seed": schedule_seed,
            }
        )
    return entries


def build_blocks(
    task_ids: list[str], arms: list[str], phase1_runs: int, phase2_runs: int, schedule_seed: int
) -> list[dict[str, Any]]:
    """Task-major blocks. Phase 1: every task in priority order, runs 0..phase1_runs-1, each run a pair of
    back-to-back trials (one per arm). Phase 2: every task again, the next ``phase2_runs`` runs. A block is
    the unit of completeness. Order is deterministic; no randomness is used (the seed is only recorded)."""
    blocks: list[dict[str, Any]] = []
    for phase, first, count in ((1, 0, phase1_runs), (2, phase1_runs, phase2_runs)):
        if count <= 0:
            continue
        for task in task_ids:
            entries: list[dict[str, Any]] = []
            for run_index in range(first, first + count):
                entries.extend(build_pair(task_ids, task, run_index, arms, schedule_seed, phase))
            blocks.append(
                {
                    "id": block_id(phase, task),
                    "phase": phase,
                    "task": task,
                    "runs": list(range(first, first + count)),
                    "entries": entries,
                }
            )
    index = 0
    for block in blocks:
        for entry in block["entries"]:
            entry["order_index"] = index
            index += 1
    return blocks


def flat_entries(blocks: list[dict[str, Any]]) -> list[dict[str, Any]]:
    return [e for b in blocks for e in b["entries"]]


def probe_seed(task_id: str, run_index: int) -> int:
    """Task seed, identical for both arms: sha256(task:run_index) mod 10^6 (as in the pilot)."""
    import hashlib

    digest = hashlib.sha256(f"{task_id}:{run_index}".encode()).hexdigest()
    return int(digest[:8], 16) % 1_000_000


def phase2_allowed(blocks: list[dict[str, Any]], final_ids: set[str], quota_ok: bool) -> bool:
    """Phase 2 starts only when every phase-1 block is complete and quota allows."""
    if not quota_ok:
        return False
    return all(e["trial_id"] in final_ids for b in blocks if b["phase"] == 1 for e in b["entries"])


def block_status(
    rows: Iterable[dict[str, Any]], blocks: list[dict[str, Any]]
) -> dict[str, dict[str, Any]]:
    """Completeness per block: every (run, arm) trial has a final row. Smoke rows are ignored."""
    final: dict[str, dict[str, Any]] = {}
    for row in rows:
        if row.get("smoke") or not row.get("final", True):
            continue
        final[row["trial_id"]] = row
    out: dict[str, dict[str, Any]] = {}
    for block in blocks:
        want = [e["trial_id"] for e in block["entries"]]
        have = [t for t in want if t in final]
        out[block["id"]] = {
            "complete": len(have) == len(want),
            "n_trials": len(have),
            "n_expected": len(want),
            "n_excluded": sum(1 for t in have if final[t].get("excluded")),
        }
    return out


def flag_incomplete(
    rows: list[dict[str, Any]], blocks: list[dict[str, Any]]
) -> list[dict[str, Any]]:
    status = block_status(rows, blocks)
    where = {e["trial_id"]: b["id"] for b in blocks for e in b["entries"]}
    for row in rows:
        if row.get("smoke"):
            row["block_incomplete"] = False
            continue
        bid = where.get(row["trial_id"])
        row["block"] = bid
        row["block_incomplete"] = not (bid and status[bid]["complete"])
    return rows


# ---------------------------------------------------------------- quota


def quota_gate(
    quota: dict[str, Any] | None, seven_day_stop: float = SEVEN_DAY_STOP
) -> dict[str, Any]:
    """Decide from the latest normalised quota event (see claude_events.quota_from_event).

    Returns ``{"action": "go" | "stop" | "wait", "reason": str, "wait_until": epoch | None}``.
    """
    if not quota:
        return {"action": "go", "reason": "no quota data yet", "wait_until": None}
    seven = quota.get("seven_day")
    five = quota.get("five_hour")
    status = quota.get("status")
    kind = quota.get("type")
    if seven is not None and seven >= seven_day_stop:
        return {
            "action": "stop",
            "reason": f"seven_day utilization {seven:.3f} >= {seven_day_stop}",
            "wait_until": None,
        }
    if status == "rejected":
        if kind == "five_hour" and (seven is None or seven < seven_day_stop):
            return {
                "action": "wait",
                "reason": "five_hour window rejected",
                "wait_until": quota.get("five_hour_resets_at") or quota.get("resets_at"),
            }
        return {"action": "stop", "reason": f"quota rejected ({kind})", "wait_until": None}
    if five is not None and five >= 1.0 and (seven is None or seven < seven_day_stop):
        return {
            "action": "wait",
            "reason": f"five_hour utilization {five:.3f}",
            "wait_until": quota.get("five_hour_resets_at"),
        }
    return {"action": "go", "reason": "ok", "wait_until": None}


# ---------------------------------------------------------------- backoff, cutoff


def backoff_seconds(
    attempt: int, base: float = BACKOFF_BASE_S, cap: float = BACKOFF_CAP_S
) -> float:
    """Exponential backoff: base, 2*base, 4*base ... capped. ``attempt`` is 0-based."""
    return min(cap, base * (2 ** max(0, attempt)))


def wait_seconds(
    attempt: int,
    reset_epoch: float | None,
    now: float | None = None,
    max_wait: float = RESET_WAIT_MAX_S,
) -> float:
    """Wait until the reset time when the message carries one (plus a small margin, at most
    ``max_wait``); otherwise exponential backoff."""
    now = time.time() if now is None else now
    if reset_epoch and reset_epoch > now:
        return min(max_wait, reset_epoch - now + 30.0)
    return backoff_seconds(attempt)


def parse_utc(text: str) -> float:
    """ISO-8601 -> epoch seconds. A value without a zone is read as UTC."""
    value = datetime.fromisoformat(text.strip().replace("Z", "+00:00"))
    if value.tzinfo is None:
        value = value.replace(tzinfo=timezone.utc)
    return value.timestamp()


def past_cutoff(cutoff_epoch: float | None, now: float | None = None) -> bool:
    return cutoff_epoch is not None and (time.time() if now is None else now) >= cutoff_epoch


def block_fits(
    cutoff_epoch: float | None,
    est_block_s: float | None,
    now: float | None = None,
    margin: float = 1.1,
) -> bool:
    """For phase-2 blocks: start only if a whole block is expected to finish before the cutoff so a
    block is not wasted as incomplete. Unknown estimate means do not start."""
    if cutoff_epoch is None:
        return True
    if not est_block_s:
        return False
    now = time.time() if now is None else now
    return now + est_block_s * margin <= cutoff_epoch


# ---------------------------------------------------------------- ledger


class Ledger:
    """Shared append-only JSONL ledger of model spend (equivalent cost), safe across processes."""

    def __init__(self, path: Path) -> None:
        self.path = path
        self.path.parent.mkdir(parents=True, exist_ok=True)

    def append(
        self, who: str, purpose: str, model: str | None, cost_usd: float | None, **extra: Any
    ) -> dict[str, Any]:
        entry = {
            "ts": datetime.now(timezone.utc).isoformat(timespec="seconds"),
            "who": who,
            "purpose": purpose,
            "model": model,
            "cost_usd": cost_usd,
            **extra,
        }
        with self.path.open("a", encoding="utf-8") as handle:
            fcntl.flock(handle, fcntl.LOCK_EX)
            try:
                handle.write(json.dumps(entry) + "\n")
                handle.flush()
            finally:
                fcntl.flock(handle, fcntl.LOCK_UN)
        return entry

    def entries(self) -> list[dict[str, Any]]:
        if not self.path.exists():
            return []
        out = []
        for line in self.path.read_text("utf-8").splitlines():
            try:
                out.append(json.loads(line))
            except json.JSONDecodeError:
                continue
        return out

    def cumulative(self, who: str | None = None) -> float:
        return round(
            sum(
                float(e.get("cost_usd") or 0.0)
                for e in self.entries()
                if who is None or e.get("who") == who
            ),
            6,
        )


def budget_allows(cumulative: float, per_trial_cap: float, cap: float | None) -> bool:
    """Optional dollar gate. ``cap=None`` (the default) never refuses."""
    if cap is None:
        return True
    return cumulative + per_trial_cap <= cap


# ---------------------------------------------------------------- resume


def completed_trial_ids(rows: Iterable[dict[str, Any]], smoke: bool = False) -> set[str]:
    """Trial ids that are final: a row exists that is not a retryable infra failure awaiting retry. A smoke run
    counts only its own smoke rows, a real run only real rows."""
    done: set[str] = set()
    for row in rows:
        if bool(row.get("smoke")) != smoke:
            continue
        if row.get("final", True):
            done.add(row["trial_id"])
    return done


def prior_attempts(rows: Iterable[dict[str, Any]], trial: str) -> tuple[int, list[str | None]]:
    """The highest attempt number recorded for ``trial`` and the infra kinds of its attempts since its last final
    row, so a resumed run numbers the next attempt after the recorded ones and keeps the retry budgets."""
    last, kinds = 0, []
    for row in rows:
        if row.get("trial_id") != trial:
            continue
        last = max(last, int(row.get("attempt") or 0))
        kinds = [] if row.get("final", True) else [*kinds, row.get("infra_failure")]
    return last, kinds


def fsync_dir(directory: Path) -> None:
    """Persist a directory's entries (a created or renamed file) across a power loss."""
    fd = os.open(directory, os.O_RDONLY)
    try:
        os.fsync(fd)
    finally:
        os.close(fd)


def append_row(path: Path, row: dict[str, Any]) -> None:
    """One durable line: a row is on disk once this returns."""
    created = not path.exists()
    with path.open("a", encoding="utf-8") as handle:
        handle.write(json.dumps(row) + "\n")
        handle.flush()
        os.fsync(handle.fileno())
    if created:
        fsync_dir(path.parent)


def drop_torn_tail(path: Path) -> int:
    """Cut a last line that has no newline (a write cut short by a crash), so the next row starts on its own line.
    Returns the bytes dropped. Only the run's single owner (``lock_run``) may call this."""
    if not path.exists():
        return 0
    data = path.read_bytes()
    keep = data.rfind(b"\n") + 1
    if keep == len(data):
        return 0
    with path.open("r+b") as handle:
        handle.truncate(keep)
        os.fsync(handle.fileno())
    return len(data) - keep


def lock_run(run_dir: Path) -> IO[str] | None:
    """Take the run directory's exclusive lock for this process's lifetime, or None when another process holds it.
    The kernel drops the lock when the holder dies, however it dies, so there is no stale lock to clear."""
    handle = (run_dir / "run.lock").open("a", encoding="utf-8")
    try:
        fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except BlockingIOError:
        handle.close()
        return None
    return handle


def write_atomic(path: Path, text: str) -> None:
    """Replace ``path`` so a reader sees the old or the new content, never a part, even after a power loss."""
    tmp = path.with_name(path.name + ".tmp")
    with tmp.open("w", encoding="utf-8") as handle:
        handle.write(text)
        handle.flush()
        os.fsync(handle.fileno())
    tmp.replace(path)
    directory = os.open(path.parent, os.O_RDONLY)
    try:
        os.fsync(
            directory
        )  # the rename itself survives a power loss only once its directory is synced
    finally:
        os.close(directory)


def load_rows(path: Path) -> list[dict[str, Any]]:
    if not path.exists():
        return []
    rows = []
    for line in path.read_text("utf-8").splitlines():
        try:
            rows.append(json.loads(line))
        except json.JSONDecodeError:
            continue
    return rows


# ---------------------------------------------------------------- pins


PIN_INFO_KEYS = (
    "release",
    "cua_driver_git_sha",
    "cua_driver_tarball",
    "cua_skills_tarball",
)  # recorded, checked elsewhere


def compare_pins(
    expected: dict[str, Any], observed: dict[str, Any]
) -> list[tuple[str, Any, Any, bool]]:
    """One row per expected pin: (name, expected, observed, matches). A pin that was not observed fails."""
    rows = []
    for key, want in expected.items():
        if key.startswith("_") or key in PIN_INFO_KEYS:
            continue
        got = observed.get(key)
        rows.append((key, want, got, got == want))
    return rows


# ---------------------------------------------------------------- latest known quota (no model call)


def persist_quota(path: Path, quota: dict[str, Any], source: str) -> None:
    """Remember the latest quota reading so the launcher can gate without a model call."""
    path.parent.mkdir(parents=True, exist_ok=True)
    keep = {
        k: quota.get(k)
        for k in (
            "five_hour",
            "seven_day",
            "status",
            "type",
            "five_hour_resets_at",
            "seven_day_resets_at",
            "resets_at",
        )
    }
    keep.update(ts=datetime.now(timezone.utc).isoformat(timespec="seconds"), source=source)
    tmp = path.with_suffix(".tmp")
    tmp.write_text(json.dumps(keep, indent=2) + "\n", "utf-8")
    tmp.replace(path)


def latest_known_quota(latest_path: Path, ledger_path: Path) -> dict[str, Any] | None:
    """The most recent seven-day reading from quota_latest.json or from ledger entries that carry
    ``seven_day_after``. Returns None when nothing is known."""
    candidates: list[dict[str, Any]] = []
    if latest_path.exists():
        try:
            data = json.loads(latest_path.read_text("utf-8"))
            if data.get("seven_day") is not None:
                candidates.append(data)
        except json.JSONDecodeError:
            pass
    if ledger_path.exists():
        for line in ledger_path.read_text("utf-8").splitlines():
            try:
                entry = json.loads(line)
            except json.JSONDecodeError:
                continue
            if entry.get("seven_day_after") is not None:
                candidates.append(
                    {
                        "ts": entry["ts"],
                        "seven_day": entry["seven_day_after"],
                        "five_hour": None,
                        "source": f"ledger:{entry.get('purpose', '')[:40]}",
                    }
                )
    if not candidates:
        return None
    return max(candidates, key=lambda c: c.get("ts", ""))


def gate_decision(
    latest: dict[str, Any] | None, stop: float = SEVEN_DAY_STOP, now: float | None = None
) -> dict[str, Any]:
    """Launch gate from the latest known quota. The seven-day figure only grows until its reset, so a
    reading at or above ``stop`` blocks the launch until the window has reset."""
    now = time.time() if now is None else now
    if not latest or latest.get("seven_day") is None:
        return {
            "action": "unknown",
            "reason": "no quota reading on file; the runner's preflight probes the quota itself",
        }
    seven = float(latest["seven_day"])
    resets = latest.get("seven_day_resets_at") or latest.get("resets_at")
    window_over = bool(resets and float(resets) < now and latest.get("status") != "rejected")
    if seven >= stop and not window_over:
        until = (
            datetime.fromtimestamp(float(resets), timezone.utc).isoformat(timespec="minutes")
            if resets
            else "unknown"
        )
        return {
            "action": "gated",
            "seven_day": seven,
            "as_of": latest.get("ts"),
            "reason": f"seven_day utilization {seven:.3f} >= {stop} as of {latest.get('ts')}; window resets {until}",
        }
    if window_over:
        return {
            "action": "unknown",
            "reason": f"last reading ({seven:.3f}) is from a window that has reset; the runner will probe",
        }
    return {
        "action": "go",
        "seven_day": seven,
        "as_of": latest.get("ts"),
        "reason": f"seven_day utilization {seven:.3f} < {stop} as of {latest.get('ts')}",
    }