#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Axis 2 robustness/load stress driver.

Runs several ``scripts/smoke_run.py`` processes concurrently — each with its
own workdir and spend authorization — against a local stub provider serving
hostile replies, provider faults, and rate limits, while a control matrix runs
in parallel (the stub ``valid`` scenario, or a real provider with
``--with-real``). Every workload is checked against the Axis 2 acceptance
criteria when the wave completes.

Workloads (one child process each):
  hostile-*   one hostile-reply scenario per child (truncation, oversized,
              invalid verdict, drip, echo secret, ...) over seed 1003, whose
              deterministic script builds a broken tree: a pass can only appear
              if a hostile reply manufactures one
  control-*   the valid scenario over seed 1001 (the matrix stand-in; a real
              provider child is added by --with-real)
  retry       flaky_5xx with one leading failure and raised
              ORCHESTRATOR_MAX_MODEL_CALLS, so the bounded repair loop
              re-issues real calls (each must be a fresh RETRY attempt that is
              billed and settles)
  fault-*     provider faults: 429 with and without bounded backoff, 5xx, and
              a stalled response, to observe fail-closed handling and the
              deadline bound

Acceptance checks (any failure fails the driver):
  no_unhandled_exception  documented exit codes, no traceback, parseable JSON
  deadlines_bound         every child finished inside its workload budget
  stores_complete         every ledger-recorded trial has a readable store
  no_pass_from_hostile    hostile workloads never claim or record a pass
  failures_recorded       hostile/fault workloads record their failures
  failures_settled        every attempt terminal and FINALLY_SETTLED
  ledger_consistent       committed == sum of settled actuals, nothing in flight
  retry_fresh_attempts    every repair re-issue is a fresh RETRY attempt, billed
  rate_limit_recovery     429 backoff recovers inside one attempt
  transcripts_sound       transcript lines complete; truncation visible; the
                          control transcripts contain no truncation
  no_credential_bytes     no raw credential bytes in any store

Usage:
  scripts/stress_run.py [--scale tiny|standard] [--with-real] [--root DIR]
                        [--json PATH] [--skip-conformance]

Exit status: 0 when every acceptance check passed, 1 otherwise, 2 on driver
misconfiguration. The JSON report always names each check and its findings.
"""
from __future__ import annotations

import argparse
import concurrent.futures
import dataclasses
import json
import os
import subprocess
import sys
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Tuple

REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT / "src"))

from orchestrator import conformance, glossary, tables  # noqa: E402
from orchestrator.store import EventStore  # noqa: E402

sys.path.insert(0, str(REPO_ROOT / "scripts"))
import provider_stub  # noqa: E402

SMOKE = REPO_ROOT / "scripts" / "smoke_run.py"
PASS_CLASSIFICATIONS = {"CORRECT_ACCEPTANCE", "FALSE_SUCCESS"}
# Exit 1 is a refused configuration (SystemExit from smoke_run.validate); an
# unhandled exception is caught separately by the traceback scan.
DOCUMENTED_EXIT_CODES = {0, 1, 2, 3, 4, 5, 6, 7}
# Replay-safe semantic fields a SEMANTIC_EVALUATION body may carry; anything
# else is hostile content that must never be admitted as a pass.
SEMANTIC_FIELDS = {"subject_reference", "verdict", "rationale"}
VERDICTS = set(glossary.semantic_verdicts())

HOSTILE_REPLY = "hostile_reply"
HOSTILE_CONTENT = "hostile_content"
CONTROL = "control"
RETRY = "retry"
FAULT = "fault"


@dataclasses.dataclass(frozen=True)
class Workload:
    name: str
    kind: str
    path: str
    seed: int
    secret: str
    env: Tuple[Tuple[str, str], ...] = ()
    wall_budget: float = 240.0
    real: bool = False
    expect_admitted_outputs: Optional[int] = 0
    expect_pass: bool = False


@dataclasses.dataclass
class ChildResult:
    workload: Workload
    exit_code: int
    document: Optional[Dict[str, Any]]
    stderr: str
    wall_seconds: float
    timed_out: bool = False


@dataclasses.dataclass
class CheckResult:
    check: str
    ok: bool
    findings: List[str]

    def to_dict(self) -> Dict[str, Any]:
        return {"check": self.check, "ok": self.ok, "findings": self.findings}


def _secret(label: str) -> str:
    return f"axis2-secret-{label}-{os.urandom(4).hex()}"


def build_workloads(scale: str, with_real: bool, real_env: Dict[str, str]) -> List[Workload]:
    hostile_reply_scenarios = {
        "tiny": ("truncated", "bad_verdict", "slow_drip", "oversized_body"),
        "standard": provider_stub.HOSTILE_REPLY_SCENARIOS,
    }[scale]
    hostile_content_scenarios = {
        "tiny": ("echo_secret",),
        "standard": provider_stub.HOSTILE_CONTENT_SCENARIOS,
    }[scale]
    faults = {
        "tiny": ("http_500", "stall"),
        "standard": ("http_500", "http_429", "stall", "flaky_429"),
    }[scale]
    controls = 1 if scale == "tiny" else 2
    workloads: List[Workload] = []
    short = (("ORCHESTRATOR_WALL_SECONDS", "15"), ("ORCHESTRATOR_PROVIDER_TIMEOUT", "3"))
    for scenario in hostile_reply_scenarios:
        env: List[Tuple[str, str]] = []
        budget = 240.0
        if scenario in ("slow_drip", "drip_forever", "stall"):
            env.extend(short)
            budget = 120.0
        path = f"/s/{scenario}"
        if scenario == "slow_drip":
            path = "/s/slow_drip?drip=0.5"
        if scenario == "oversized_reply":
            # Larger than ORCHESTRATOR_MAX_REPLY_BYTES so the hard reply-size
            # cap is what refuses it (the parse layer never sees the garbage).
            path = "/s/oversized_reply?size=2000000"
        workloads.append(Workload(
            name=f"hostile-{scenario}",
            kind=HOSTILE_REPLY,
            path=path,
            seed=1003,
            secret=_secret(scenario),
            env=tuple(env),
            wall_budget=budget,
        ))
    for scenario in hostile_content_scenarios:
        workloads.append(Workload(
            name=f"hostile-{scenario}",
            kind=HOSTILE_CONTENT,
            path=f"/s/{scenario}",
            seed=1003,
            secret=_secret(scenario),
            wall_budget=240.0,
            # A well-formed evaluation may be admitted (as untrusted output);
            # it must still never produce a pass over the broken seed-1003 tree.
            expect_admitted_outputs=None,
        ))
    for index in range(controls):
        workloads.append(Workload(
            name=f"control-{index}",
            kind=CONTROL,
            path="/s/valid",
            seed=1001,
            secret=_secret(f"control-{index}"),
            wall_budget=300.0,
            expect_admitted_outputs=None,
            expect_pass=True,
        ))
    workloads.append(Workload(
        name="retry-flaky-5xx",
        kind=RETRY,
        path="/s/flaky_5xx?fails=1",
        seed=1001,
        secret=_secret("retry"),
        env=(
            ("ORCHESTRATOR_MAX_MODEL_CALLS", "24"),
            ("ORCHESTRATOR_WALL_SECONDS", "120"),
        ),
        wall_budget=300.0,
        expect_admitted_outputs=None,
    ))
    for fault in faults:
        env: List[Tuple[str, str]] = [("ORCHESTRATOR_MAX_MODEL_CALLS", "24")]
        budget = 300.0
        path = f"/s/{fault}"
        if fault == "stall":
            env = [c for c in env] + [("ORCHESTRATOR_WALL_SECONDS", "15"),
                                      ("ORCHESTRATOR_PROVIDER_TIMEOUT", "2")]
            budget = 150.0
        if fault == "http_429":
            env = [c for c in env] + [("ORCHESTRATOR_MAX_RATE_LIMIT_RETRIES", "2")]
            path = "/s/http_429?retry_after=1"
            budget = 150.0
        if fault == "flaky_429":
            env = [c for c in env] + [("ORCHESTRATOR_MAX_RATE_LIMIT_RETRIES", "2")]
            path = "/s/flaky_429?fails=2"
        workloads.append(Workload(
            name=f"fault-{fault}",
            kind=FAULT,
            path=path,
            seed=1003,
            secret=_secret(fault),
            env=tuple(env),
            wall_budget=budget,
        ))
    if with_real:
        workloads.append(Workload(
            name="control-real",
            kind=CONTROL,
            path=real_env.get("ORCHESTRATOR_PROVIDER_PATH", "/v1/chat/completions"),
            seed=1001,
            secret=real_env.get("ORCHESTRATOR_PROVIDER_API_KEY", ""),
            # Four trials x the 120s per-trial deadline plus sandbox and oracle
            # time, under the whole wave's CPU load.
            wall_budget=900.0,
            expect_admitted_outputs=None,
            expect_pass=True,
            real=True,
        ))
    return workloads


def child_env(workload: Workload, stub_base: str, root: Path,
              real_env: Dict[str, str], authorized_at: str) -> Dict[str, str]:
    env = {key: value for key, value in os.environ.items()
           if not key.startswith("ORCHESTRATOR_")}
    workdir = root / workload.name
    env.update({
        "ORCHESTRATOR_MODE": "real",
        "ORCHESTRATOR_CONFIRM_REAL": "yes",
        "ORCHESTRATOR_OWNER": "axis2-stress",
        "ORCHESTRATOR_CAP": "500",
        # One authorisation stamp for the whole wave: a workdir can later be
        # recovered with --resume under the same recorded authorisation.
        "ORCHESTRATOR_AUTHORIZED_AT": authorized_at,
        "ORCHESTRATOR_AUTH_METHOD": "axis2-stress-wave",
        "ORCHESTRATOR_WORKDIR": str(workdir),
        "ORCHESTRATOR_SEEDS": str(workload.seed),
        "ORCHESTRATOR_PRICE_INPUT_PER_MTOK": "0.3",
        "ORCHESTRATOR_PRICE_OUTPUT_PER_MTOK": "1.2",
        "ORCHESTRATOR_PROVIDER_UNITS": "USD",
        "ORCHESTRATOR_PROVIDER_MODEL": "stub-model",
        "ORCHESTRATOR_REQUIRE_CONFORMANCE": "0",
        "ORCHESTRATOR_DEBUG_TRANSCRIPT": "1",
        "ORCHESTRATOR_CREDENTIAL_TTL_SECONDS": "900",
        "ORCHESTRATOR_MAX_MODEL_CALLS": "8",
        "ORCHESTRATOR_REQUIRE_SANDBOX": "1",
    })
    if workload.real:
        env.update({
            key: value for key, value in real_env.items()
            if key.startswith("ORCHESTRATOR_PROVIDER_") or key.startswith("ORCHESTRATOR_PRICE_")
        })
        env["ORCHESTRATOR_PROVIDER_BASE_URL"] = real_env["ORCHESTRATOR_PROVIDER_BASE_URL"]
    else:
        env["ORCHESTRATOR_PROVIDER_BASE_URL"] = stub_base
        env["ORCHESTRATOR_PROVIDER_PATH"] = workload.path
        env["ORCHESTRATOR_PROVIDER_API_KEY"] = workload.secret
    for key, value in workload.env:
        env[key] = value
    return env


def run_child(workload: Workload, stub_base: str, root: Path,
              real_env: Dict[str, str], authorized_at: str) -> ChildResult:
    env = child_env(workload, stub_base, root, real_env, authorized_at)
    started = time.monotonic()
    try:
        process = subprocess.run(
            [sys.executable, str(SMOKE), "--json"],
            env=env,
            capture_output=True,
            text=True,
            timeout=workload.wall_budget,
        )
    except subprocess.TimeoutExpired:
        return ChildResult(
            workload=workload,
            exit_code=-1,
            document=None,
            stderr="subprocess killed at the workload wall budget",
            wall_seconds=time.monotonic() - started,
            timed_out=True,
        )
    wall = time.monotonic() - started
    document = None
    try:
        document = json.loads(process.stdout)
    except json.JSONDecodeError:
        document = None
    return ChildResult(
        workload=workload,
        exit_code=process.returncode,
        document=document,
        stderr=process.stderr,
        wall_seconds=wall,
    )


def _trial_dirs(workdir: Path) -> List[Path]:
    trials = workdir / "trials"
    if not trials.is_dir():
        return []
    return sorted(path for path in trials.iterdir() if (path / "events.db").exists())


def _trial_records(workdir: Path):
    """Return (records, problems) for each durable trial in a workdir.

    ``records`` are ``(trial_dir, identity, run_id, events, state)`` tuples;
    ``problems`` names every store that is missing required content or cannot
    be read, so a deleted or corrupted store is a loud finding rather than a
    silently skipped iteration (Axis 2 review F1).
    """
    records = []
    problems = []
    for trial in _trial_dirs(workdir):
        try:
            identity = json.loads((trial / "identity.json").read_text())
            run_id = f"EXP-{identity['arm']}-{identity['seed']}"
            store = EventStore(str(trial / "events.db"))
            try:
                records.append(
                    (trial, identity, run_id, store.history(run_id), store.state(run_id))
                )
            finally:
                store.close()
        except Exception as error:  # noqa: BLE001 - reported as a finding
            problems.append(
                f"{workdir.name}/{trial.name}: store unreadable: "
                f"{type(error).__name__}: {error}"
            )
    return records, problems


def check_stores_complete(results: Sequence[ChildResult], root: Path) -> CheckResult:
    """Every trial the spend ledger recorded must have a readable store, and
    every store must belong to a recorded trial: the trial-derived checks read
    the stores, so a missing store would otherwise pass them vacuously."""
    findings: List[str] = []
    for result in results:
        workdir = root / result.workload.name
        ledger_path = workdir / "experiment-spend.json"
        if not ledger_path.exists():
            findings.append(f"{result.workload.name}: no spend ledger")
            continue
        recorded = {
            trial_id.removeprefix("EXP-")
            for trial_id in json.loads(ledger_path.read_text()).get("trials", {})
        }
        trials_root = workdir / "trials"
        stores = (
            {path.parent.name for path in trials_root.glob("*/events.db")}
            if trials_root.is_dir()
            else set()
        )
        for name in sorted(recorded - stores):
            findings.append(
                f"{result.workload.name}: ledger-recorded trial {name} has no store"
            )
        for name in sorted(stores - recorded):
            findings.append(
                f"{result.workload.name}: store {name} has no ledger trial record"
            )
        _records, problems = _trial_records(workdir)
        findings.extend(problems)
    return CheckResult("stores_complete", not findings, findings)


def check_no_unhandled_exception(results: Sequence[ChildResult]) -> CheckResult:
    findings: List[str] = []
    for result in results:
        if result.exit_code not in DOCUMENTED_EXIT_CODES:
            findings.append(
                f"{result.workload.name}: undocumented exit code {result.exit_code}"
            )
        if "Traceback (most recent call last)" in result.stderr:
            findings.append(f"{result.workload.name}: traceback escaped the runner")
        if result.document is None:
            findings.append(f"{result.workload.name}: stdout is not a JSON document")
    return CheckResult("no_unhandled_exception", not findings, findings)


def check_deadlines_bound(results: Sequence[ChildResult]) -> CheckResult:
    findings: List[str] = []
    for result in results:
        if result.timed_out:
            findings.append(
                f"{result.workload.name}: exceeded its workload budget of "
                f"{result.workload.wall_budget:.0f}s and was killed"
            )
        elif result.wall_seconds > result.workload.wall_budget:
            findings.append(
                f"{result.workload.name}: ran {result.wall_seconds:.1f}s "
                f"over the {result.workload.wall_budget:.0f}s budget"
            )
    return CheckResult("deadlines_bound", not findings, findings)


def check_pass_and_failures(results: Sequence[ChildResult], root: Path) -> List[CheckResult]:
    no_pass: List[str] = []
    recorded: List[str] = []
    admitted: List[str] = []
    for result in results:
        workdir = root / result.workload.name
        hostile = result.workload.kind in (HOSTILE_REPLY, HOSTILE_CONTENT)
        records, _problems = _trial_records(workdir)
        for _trial, _identity, run_id, events, state in records:
            labels = f"{result.workload.name}/{run_id}"
            outcome = state.outcome
            classification = None
            for event in events:
                if event.event_type == "EVIDENCE_ADMITTED" and (
                    event.payload.get("check_class") == "HIDDEN"
                ):
                    versions = event.payload.get("versions") or {}
                    classification = versions.get("classification")
            failure_events = [
                event for event in events
                if event.event_type in (
                    "DISPATCH_FAILED", "EXECUTOR_FAILED", "MODEL_OUTPUT_REJECTED",
                    "RESULT_REJECTED", "FAILURE_RECORDED",
                )
            ]
            admitted_outputs = [
                event for event in events if event.event_type == "MODEL_OUTPUT_ADMITTED"
            ]
            if hostile:
                if outcome == "ACCEPTANCE_CLAIMED":
                    no_pass.append(f"{labels}: hostile workload claimed acceptance")
                if classification in PASS_CLASSIFICATIONS:
                    no_pass.append(f"{labels}: hostile workload classified {classification}")
                if result.workload.expect_admitted_outputs == 0 and admitted_outputs:
                    admitted.append(
                        f"{labels}: {len(admitted_outputs)} model output(s) admitted "
                        "from an unadmittable hostile reply"
                    )
                if not failure_events:
                    recorded.append(f"{labels}: no recorded failure event")
            for event in admitted_outputs:
                body = event.payload.get("output") or {}
                extra = sorted(set(body) - SEMANTIC_FIELDS)
                if extra:
                    admitted.append(f"{labels}: admitted body carries extra fields {extra}")
                if body.get("verdict") not in VERDICTS:
                    admitted.append(f"{labels}: admitted verdict {body.get('verdict')!r}")
    results_ = [
        CheckResult("no_pass_from_hostile", not no_pass, no_pass),
        CheckResult("failures_recorded", not recorded, recorded),
        CheckResult("admitted_bodies_clean", not admitted, admitted),
    ]
    return results_


def check_failures_settled(results: Sequence[ChildResult], root: Path) -> CheckResult:
    findings: List[str] = []
    attempt_table = tables.attempt_table()
    for result in results:
        workdir = root / result.workload.name
        records, _problems = _trial_records(workdir)
        for _trial, _identity, run_id, _events, state in records:
            labels = f"{result.workload.name}/{run_id}"
            for attempt in state.attempts.values():
                if not attempt_table.is_terminal(attempt.state):
                    findings.append(f"{labels}: attempt {attempt.attempt_id} not terminal")
                if attempt.accounting_state != "FINALLY_SETTLED":
                    findings.append(
                        f"{labels}: attempt {attempt.attempt_id} accounting "
                        f"{attempt.accounting_state!r} not FINALLY_SETTLED"
                    )
                if attempt.actual_cost is None:
                    findings.append(f"{labels}: attempt {attempt.attempt_id} has no actual")
    return CheckResult("failures_settled", not findings, findings)


def check_ledger_consistent(results: Sequence[ChildResult], root: Path) -> CheckResult:
    findings: List[str] = []
    for result in results:
        workdir = root / result.workload.name
        ledger_path = workdir / "experiment-spend.json"
        if not ledger_path.exists():
            findings.append(f"{result.workload.name}: no spend ledger")
            continue
        ledger = json.loads(ledger_path.read_text())
        actuals = [
            float(record.get("actual", 0.0))
            for record in ledger.get("trials", {}).values()
        ]
        if ledger.get("in_flight"):
            findings.append(
                f"{result.workload.name}: outstanding reservations {sorted(ledger['in_flight'])}"
            )
        if abs(float(ledger.get("committed", 0.0)) - sum(actuals)) > 1e-9:
            findings.append(
                f"{result.workload.name}: committed {ledger.get('committed')} != "
                f"sum of settled actuals {sum(actuals)}"
            )
        expected_trials = 4
        if len(actuals) != expected_trials:
            findings.append(
                f"{result.workload.name}: {len(actuals)} trial records, "
                f"expected {expected_trials}"
            )
    return CheckResult("ledger_consistent", not findings, findings)


def check_retry_fresh_attempts(root: Path) -> CheckResult:
    findings: List[str] = []
    workdir = root / "retry-flaky-5xx"
    saw_retry_lineage = False
    records, _problems = _trial_records(workdir)
    for _trial, _identity, run_id, events, state in records:
        labels = f"retry-flaky-5xx/{run_id}"
        planned = [
            event for event in events
            if event.event_type == "ATTEMPT_PLANNED"
            and event.payload.get("action_type") == "REVIEW"
        ]
        causes = [event.payload.get("cause") for event in planned]
        if "RETRY" in causes:
            saw_retry_lineage = True
            if causes.count("INITIAL") + causes.count("RETRY") != len(causes):
                findings.append(f"{labels}: unexpected attempt causes {causes}")
            # A retry must be a fresh attempt: distinct planned attempt ids,
            # each with its own commitment and settlement.
            ids = [event.payload.get("attempt_id") for event in planned]
            if len(ids) != len(set(ids)):
                findings.append(f"{labels}: attempt ids are not distinct")
            for attempt in state.attempts.values():
                if attempt.cause == "RETRY" and attempt.actual_cost is None:
                    findings.append(
                        f"{labels}: retry {attempt.attempt_id} was not billed"
                    )
    if not saw_retry_lineage:
        findings.append("retry-flaky-5xx: no RETRY attempt was planned")
    return CheckResult("retry_fresh_attempts", not findings, findings)


def check_rate_limit_recovery(root: Path) -> CheckResult:
    """The bounded 429 backoff recovers inside one attempt: the flaky-429
    workload must show client-level bounded retries, no failed dispatch from a
    rate limit, and no attempt-level REVIEW retry at all."""
    findings: List[str] = []
    workdir = root / "fault-flaky_429"
    if not (workdir / "experiment-spend.json").exists():
        return CheckResult("rate_limit_recovery", True, ["fault-flaky_429 not run"])
    transcript = workdir / "debug" / "transcript.jsonl"
    retries_seen = 0
    if transcript.exists():
        for line in transcript.read_text().splitlines():
            entry = json.loads(line) if line.strip() else {}
            error = entry.get("error")
            if isinstance(error, str) and "bounded retry" in error:
                retries_seen += 1
    if retries_seen == 0:
        findings.append("fault-flaky_429: no client-level bounded retry was recorded")
    records, _problems = _trial_records(workdir)
    for _trial, _identity, run_id, events, _state in records:
        for event in events:
            if event.event_type in ("DISPATCH_FAILED", "EXECUTOR_FAILED"):
                findings.append(
                    f"fault-flaky_429/{run_id}: {event.event_type} escaped the "
                    "client-level 429 backoff"
                )
            if (
                event.event_type == "ATTEMPT_PLANNED"
                and event.payload.get("action_type") == "REVIEW"
                and event.payload.get("cause") == "RETRY"
            ):
                findings.append(
                    f"fault-flaky_429/{run_id}: a review was re-issued even "
                    "though the client-level 429 backoff is in effect"
                )
    return CheckResult("rate_limit_recovery", not findings, findings)


def check_transcripts(results: Sequence[ChildResult], root: Path) -> CheckResult:
    findings: List[str] = []
    for result in results:
        workdir = root / result.workload.name
        transcript = workdir / "debug" / "transcript.jsonl"
        if not transcript.exists():
            findings.append(f"{result.workload.name}: no debug transcript")
            continue
        truncation_errors = 0
        for number, line in enumerate(transcript.read_text().splitlines(), 1):
            if not line.strip():
                continue
            try:
                entry = json.loads(line)
            except json.JSONDecodeError:
                findings.append(
                    f"{result.workload.name}: transcript line {number} is torn/truncated"
                )
                continue
            error = entry.get("error")
            if isinstance(error, str) and "truncated at max_tokens" in error:
                truncation_errors += 1
        expects_truncation = result.workload.path.startswith("/s/truncated")
        if expects_truncation and truncation_errors == 0:
            findings.append(
                f"{result.workload.name}: truncated completions were not recorded"
            )
        if result.workload.kind == CONTROL and truncation_errors:
            findings.append(
                f"{result.workload.name}: {truncation_errors} truncated completion(s) "
                "in a control transcript"
            )
    return CheckResult("transcripts_sound", not findings, findings)


def check_no_credential_bytes(results: Sequence[ChildResult], root: Path,
                              capture: Optional[Path]) -> CheckResult:
    findings: List[str] = []
    secrets = {result.workload.secret for result in results if result.workload.secret}
    for result in results:
        workdir = root / result.workload.name
        for path in workdir.rglob("*"):
            if not path.is_file():
                continue
            try:
                blob = path.read_bytes()
            except OSError:
                continue
            for secret in secrets:
                if secret.encode() in blob:
                    findings.append(
                        f"{result.workload.name}: credential bytes in {path.name}"
                    )
                    break
    if capture and capture.exists():
        blob = capture.read_bytes()
        for secret in secrets:
            if secret.encode() in blob:
                findings.append(f"stub capture holds credential bytes")
                break
    return CheckResult("no_credential_bytes", not findings, findings)


def run_wave(workloads: Sequence[Workload], stub_base: str, root: Path,
             real_env: Dict[str, str], authorized_at: str) -> List[ChildResult]:
    results: List[ChildResult] = []
    with concurrent.futures.ThreadPoolExecutor(max_workers=len(workloads)) as pool:
        futures = {
            pool.submit(run_child, workload, stub_base, root, real_env, authorized_at): workload
            for workload in workloads
        }
        for future in concurrent.futures.as_completed(futures):
            workload = futures[future]
            result = future.result()
            results.append(result)
            status = "json" if result.document is not None else "no-json"
            killed = " KILLED" if result.timed_out else ""
            print(
                f"[child] {workload.name:28s} exit={result.exit_code} "
                f"{status} {result.wall_seconds:6.1f}s{killed}",
                flush=True,
            )
    return sorted(results, key=lambda item: item.workload.name)


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--scale", choices=("tiny", "standard"), default="standard")
    parser.add_argument("--with-real", action="store_true",
                        help="also run a control matrix against the real provider "
                             "configured in this process's ORCHESTRATOR_PROVIDER_* env")
    parser.add_argument("--root", default="",
                        help="output root (default evidence/axis2-stress-<utc>)")
    parser.add_argument("--json", default="", help="report path (default <root>/stress-report.json)")
    parser.add_argument("--skip-conformance", action="store_true")
    args = parser.parse_args()

    real_env = {
        key: value for key, value in os.environ.items()
        if key.startswith(("ORCHESTRATOR_PROVIDER_", "ORCHESTRATOR_PRICE_"))
    }
    if args.with_real:
        missing = [
            key for key in ("ORCHESTRATOR_PROVIDER_BASE_URL", "ORCHESTRATOR_PROVIDER_API_KEY")
            if not real_env.get(key)
        ]
        if missing:
            print(f"error: --with-real needs {missing} in the environment", file=sys.stderr)
            return 2

    if not args.skip_conformance:
        gate = conformance.run_full_suite()
        if not gate.passed:
            print("error: conformance suite failed; refusing to stress", file=sys.stderr)
            for failure in gate.failures():
                print(f"  {failure}", file=sys.stderr)
            return 2

    stamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime())
    root = Path(args.root) if args.root else REPO_ROOT / "evidence" / f"axis2-stress-{stamp}"
    root.mkdir(parents=True, exist_ok=True)
    report_path = Path(args.json) if args.json else root / "stress-report.json"
    capture = root / "stub-capture.jsonl"

    workloads = build_workloads(args.scale, args.with_real, real_env)
    authorized_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
    stub = provider_stub.StubServer(port=0, capture=str(capture)).start()
    print(f"[stub] ready on {stub.base_url}; wave of {len(workloads)} children "
          f"-> {root}", flush=True)
    try:
        results = run_wave(workloads, stub.base_url, root, real_env, authorized_at)
    finally:
        stub.stop()

    checks: List[CheckResult] = [check_no_unhandled_exception(results),
                                 check_deadlines_bound(results),
                                 check_stores_complete(results, root)]
    checks.extend(check_pass_and_failures(results, root))
    checks.append(check_failures_settled(results, root))
    checks.append(check_ledger_consistent(results, root))
    checks.append(check_retry_fresh_attempts(root))
    checks.append(check_rate_limit_recovery(root))
    checks.append(check_transcripts(results, root))
    checks.append(check_no_credential_bytes(results, root, capture))

    report = {
        "schema_version": 1,
        "axis": "Axis 2 robustness/load stress",
        "scale": args.scale,
        "with_real": bool(args.with_real),
        "root": str(root),
        "authorized_at": authorized_at,
        "children": [
            {
                "name": result.workload.name,
                "kind": result.workload.kind,
                "path": result.workload.path,
                "exit_code": result.exit_code,
                "wall_seconds": round(result.wall_seconds, 3),
                "trials": [
                    {
                        "run_id": trial.get("run_id"),
                        "classification": trial.get("classification"),
                        "controller_outcome": trial.get("controller_outcome"),
                        "budget_exhaustion": trial.get("budget_exhaustion"),
                        "total_cost": trial.get("total_cost"),
                    }
                    for trial in ((result.document or {}).get("trials") or [])
                ],
            }
            for result in results
        ],
        "checks": [check.to_dict() for check in checks],
    }
    report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n")

    failed = [check for check in checks if not check.ok]
    for check in checks:
        mark = "PASS" if check.ok else "FAIL"
        print(f"[{mark}] {check.check}")
        for finding in check.findings:
            print(f"       - {finding}")
    print(f"[report] {report_path}")
    return 1 if failed else 0


if __name__ == "__main__":
    raise SystemExit(main())
