Viewing files at commit e70815b9044aCommit detailsView latest

Browse files
scripts/workflow_ab.py 1744 lines · 70.8 KB · python
1
#!/usr/bin/env python3
2
"""Run deterministic agent-shaped workflow A/B tests across Git and Oak.
3
4
This complements scripts/bench.py. The core benchmark times isolated VCS
5
commands; this script runs whole task recipes that look more like common agent
6
work: search, read, edit, test/validate, status, diff, and snapshot. The
7
fixtures and edits are deterministic so different subjects only vary by VCS.
8
"""
9
10
from __future__ import annotations
11
12
import argparse
13
import json
14
import platform
15
import random
16
import shlex
17
import shutil
18
import statistics
19
import subprocess
20
import tempfile
21
import time
22
from dataclasses import dataclass
23
from datetime import datetime, timezone
24
from pathlib import Path
25
from typing import Any, Callable
26
27
from oakbench import environment as oakbench_environment
28
from oakbench import envwatch as oakbench_envwatch
29
from oakbench import fixtures as oakbench_fixtures
30
from oakbench import loadgen as oakbench_loadgen
31
from oakbench import integrity as oakbench_integrity
32
from oakbench import workflow_oracles
33
from oakbench import remotes as oakbench_remotes
34
from oakbench import tokens as oakbench_tokens
35
from oakbench.command_semantics import semantics
36
from oakbench.environment import command_display
37
from oakbench.execution import run_timed
38
from oakbench.reporting import fmt_delta_lower_better, fmt_num
39
from oakbench.results import ResultsStore
40
from oakbench.rows import SKIP_RETURNCODE, row_returncode
41
from oakbench.runlock import measurement_lock
42
from oakbench.subjects import (
43
    DEFAULT_OAK_REPO,
44
    Subject,
45
    load_subjects,
46
    source_metadata,
47
    subject_details,
48
    subject_version,
49
)
50
51
ROOT = Path(__file__).resolve().parents[1]
52
DEFAULT_WORKDIR = Path(tempfile.gettempdir()) / "oak-workflow-ab"
53
DEFAULT_ADMITTED_OUTPUT_CHARS = 20_000
54
FIXTURE_VERSION = "2026-06-09.2"
55
TRACKS = ("agent-default", "core-equivalent")
56
57
# Compatibility re-exports: agent_workflow.py and tests reference these
58
# through this module. The definitions live in oakbench (the measurement core).
59
COST_WEIGHTS_NOTE = oakbench_tokens.COST_WEIGHTS_NOTE
60
COMMAND_SEMANTICS_VERSION = semantics().version
61
_OAK_COMMIT_PUSH_SUPPORT_CACHE: dict[str, str | None] = {}
62
63
64
def base_env() -> dict[str, str]:
65
    return oakbench_environment.base_env(
66
        author_name="Oak Workflow Bench",
67
        author_email="[email protected]",
68
        oak_author="oak-workflow-bench",
69
    )
70
71
72
def python_no_bytecode_env(base: dict[str, str] | None = None) -> dict[str, str]:
73
    env = dict(base) if base is not None else base_env()
74
    env["PYTHONDONTWRITEBYTECODE"] = "1"
75
    return env
76
77
78
def step_env(step: "Step", base: dict[str, str] | None = None) -> dict[str, str]:
79
    if step.kind == "test":
80
        return python_no_bytecode_env(base)
81
    return dict(base) if base is not None else base_env()
82
83
84
def oak_commit_push_skip_reason(subject: Subject) -> str | None:
85
    if subject.kind != "oak":
86
        return None
87
    key = str(subject.bin)
88
    if key in _OAK_COMMIT_PUSH_SUPPORT_CACHE:
89
        return _OAK_COMMIT_PUSH_SUPPORT_CACHE[key]
90
    try:
91
        probe = subprocess.run(
92
            [key, "commit", "--help"],
93
            capture_output=True,
94
            text=True,
95
            env=base_env(),
96
            timeout=15,
97
            check=False,
98
        )
99
    except FileNotFoundError:
100
        reason = f"missing_binary:{key}"
101
        _OAK_COMMIT_PUSH_SUPPORT_CACHE[key] = reason
102
        return reason
103
    except subprocess.TimeoutExpired:
104
        reason = "oak_commit_push_capability_timeout"
105
        _OAK_COMMIT_PUSH_SUPPORT_CACHE[key] = reason
106
        return reason
107
    help_text = (probe.stdout or "") + "\n" + (probe.stderr or "")
108
    if probe.returncode != 0:
109
        reason = "oak_commit_push_capability_failed"
110
    elif "--push" not in help_text:
111
        reason = "oak_commit_push_not_supported:requires_current_oak"
112
    else:
113
        reason = None
114
    _OAK_COMMIT_PUSH_SUPPORT_CACHE[key] = reason
115
    return reason
116
117
118
@dataclass(frozen=True)
119
class Workflow:
120
    name: str
121
    description: str
122
    make_fixture: Callable[[Path], None]
123
    steps: Callable[..., list["Step"]]
124
    # Optional untimed-phase steps (run in the setup phase, after repo init)
125
    # for workflows that need pre-built history or pre-broken state.
126
    prepare: Callable[..., list["Step"]] | None = None
127
    # Workflows that exercise a remote set the oakbench.remotes purpose here.
128
    # Their steps/prepare callables take (subject, track, ctx); ctx carries the
129
    # per-run disposable branch and remote/clone paths. Oak subjects without
130
    # the purpose env var configured emit a skip row (returncode 77).
131
    remote_purpose: str | None = None
132
133
134
@dataclass(frozen=True)
135
class Step:
136
    operation: str
137
    kind: str
138
    command: list[str]
139
    expected_returncodes: tuple[int, ...] = (0,)
140
    # For expected-failure steps: the literal recovery command the workflow
141
    # runs next. The row records whether the error text actually mentions it —
142
    # the "error output is a prompt" actionability measurement. A misleading
143
    # hint (error suggests something other than the real fix) is a finding,
144
    # not a harness bug.
145
    recovery_hint: str | None = None
146
147
148
def parse_args() -> argparse.Namespace:
149
    parser = argparse.ArgumentParser(description=__doc__)
150
    parser.add_argument(
151
        "--workflows",
152
        default="bugfix_test_loop,wide_config_refactor,large_asset_manifest",
153
        help="Comma-separated workflow names, or 'all'.",
154
    )
155
    parser.add_argument("--subjects", help="Comma-separated subject names")
156
    parser.add_argument("--runs", type=int, default=1)
157
    parser.add_argument("--config", type=Path, default=ROOT / "config" / "subjects.toml")
158
    parser.add_argument("--workdir", type=Path, default=DEFAULT_WORKDIR)
159
    parser.add_argument("--results", type=Path, default=ROOT / "results" / "workflow-ab")
160
    parser.add_argument(
161
        "--oak-repo",
162
        type=Path,
163
        default=DEFAULT_OAK_REPO,
164
        help="Optional Oak source checkout for provenance metadata. Defaults to $OAK_REPO or ../oak.",
165
    )
166
    parser.add_argument("--keep-workdirs", action="store_true")
167
    parser.add_argument("--git-bin", type=Path)
168
    parser.add_argument("--oak-installed-bin", type=Path)
169
    parser.add_argument("--oak-local-bin", type=Path)
170
    parser.add_argument("--oak-aa-bin", type=Path, help="A/A control: the baseline Oak binary under subject oak_installed_aa.")
171
    parser.add_argument(
172
        "--track",
173
        choices=TRACKS,
174
        default="agent-default",
175
        help=(
176
            "agent-default measures natural current commands; core-equivalent "
177
            "uses the closest matching semantic output level across VCSs."
178
        ),
179
    )
180
    parser.add_argument(
181
        "--randomize-subject-order",
182
        action="store_true",
183
        help="Shuffle subject order per workflow run to reduce cache/order bias.",
184
    )
185
    parser.add_argument(
186
        "--admitted-output-chars",
187
        type=int,
188
        default=DEFAULT_ADMITTED_OUTPUT_CHARS,
189
        help="Per-command stdout/stderr characters counted as agent-visible output.",
190
    )
191
    parser.add_argument(
192
        "--load-tier",
193
        choices=sorted(oakbench_loadgen.LOAD_TIERS),
194
        default="none",
195
        help=(
196
            "Inject a machine-independent stress-ng background load tier while "
197
            "measuring (noisy-neighbor realism). Requires the optional stress-ng "
198
            "binary; when it is missing under a non-none tier every row becomes "
199
            "an explicit skip row — load that was not applied is never claimed."
200
        ),
201
    )
202
    return parser.parse_args()
203
204
205
def shell(script: str) -> list[str]:
206
    return ["/bin/zsh", "-c", script]
207
208
209
def search_command(pattern: str, target: str) -> str:
210
    # rg is not guaranteed on benchmark runners; grep -rnE has the same exit
211
    # contract (0 match, 1 no match). The row's command field records which ran.
212
    # rg skips hidden directories by default; grep must be told to skip VCS
213
    # internals or its match scope (and token counts) diverge.
214
    if shutil.which("rg"):
215
        return f"rg -n {shlex.quote(pattern)} {target}"
216
    return f"grep -rnE --exclude-dir=.git --exclude-dir=.oak {shlex.quote(pattern)} {target}"
217
218
219
def snapshot_steps(subject: Subject, operation_prefix: str, message: str) -> list[Step]:
220
    """Snapshot via the command-semantics contract: git pays stage+commit as two
221
    tool calls, oak one. Operation names follow the historical .add/.commit split."""
222
    sem = semantics()
223
    vcs = str(subject.bin)
224
    stage = sem.stage_args(subject.kind)
225
    if stage is not None:
226
        return [
227
            Step(f"{operation_prefix}.add", "vcs", [vcs, *stage]),
228
            Step(f"{operation_prefix}.commit", "vcs", [vcs, *sem.commit_args(subject.kind, message)]),
229
        ]
230
    return [Step(operation_prefix, "vcs", [vcs, *sem.commit_args(subject.kind, message)])]
231
232
233
def vcs_init_steps(subject: Subject) -> list[Step]:
234
    sem = semantics()
235
    vcs = str(subject.bin)
236
    return [
237
        Step("setup.repo_init", "vcs", [vcs, *sem.init_args(subject.kind)]),
238
        *snapshot_steps(subject, "setup.snapshot", "initial"),
239
    ]
240
241
242
def vcs_workflow_steps(subject: Subject, track: str) -> list[Step]:
243
    sem = semantics()
244
    vcs = str(subject.bin)
245
    return [
246
        Step("vcs.branch", "vcs", [vcs, *sem.branch_create_args(subject.kind)]),
247
        Step("vcs.status", "vcs", [vcs, *sem.status_args(subject.kind, track)]),
248
        Step("vcs.diff", "vcs", [vcs, *sem.diff_args(subject.kind, track)]),
249
        *snapshot_steps(subject, "vcs.snapshot", "agent task"),
250
    ]
251
252
253
def make_bugfix_fixture(path: Path) -> None:
254
    (path / "app").mkdir(parents=True)
255
    (path / "tests").mkdir()
256
    (path / "app" / "__init__.py").write_text("")
257
    (path / "app" / "pricing.py").write_text(
258
        "\n".join(
259
            [
260
                "def apply_discount(cents: int, percent: int) -> int:",
261
                "    \"\"\"Return discounted cents rounded down.\"\"\"",
262
                "    # BUG: this subtracts the percent value, not the percentage.",
263
                "    return cents - percent",
264
                "",
265
            ]
266
        )
267
    )
268
    (path / "tests" / "test_pricing.py").write_text(
269
        "\n".join(
270
            [
271
                "import unittest",
272
                "from app.pricing import apply_discount",
273
                "",
274
                "",
275
                "class PricingTest(unittest.TestCase):",
276
                "    def test_apply_discount(self):",
277
                "        self.assertEqual(apply_discount(1000, 25), 750)",
278
                "        self.assertEqual(apply_discount(999, 10), 900)",
279
                "",
280
                "",
281
                "if __name__ == '__main__':",
282
                "    unittest.main()",
283
                "",
284
            ]
285
        )
286
    )
287
    (path / "README.md").write_text("Pricing service fixture for agent workflow benchmarks.\n")
288
289
290
def bugfix_steps(subject: Subject, track: str) -> list[Step]:
291
    return [
292
        Step("search.locate_symbol", "search", shell(search_command("apply_discount|BUG|discount", "."))),
293
        Step("read.source", "read", shell("sed -n '1,120p' app/pricing.py")),
294
        Step("read.tests", "read", shell("sed -n '1,160p' tests/test_pricing.py")),
295
        Step("test.red", "test", shell("python3 -m unittest discover -s tests"), (1,)),
296
        Step(
297
            "edit.fix_bug",
298
            "edit",
299
            shell(
300
                "python3 - <<'PY'\n"
301
                "from pathlib import Path\n"
302
                "p = Path('app/pricing.py')\n"
303
                "text = p.read_text()\n"
304
                "text = text.replace('return cents - percent', 'return cents - ((cents * percent) // 100)')\n"
305
                "p.write_text(text)\n"
306
                "PY"
307
            ),
308
        ),
309
        Step("test.green", "test", shell("python3 -m unittest discover -s tests")),
310
        *vcs_workflow_steps(subject, track),
311
    ]
312
313
314
def make_wide_config_fixture(path: Path) -> None:
315
    root = path / "services"
316
    for index in range(600):
317
        service = root / f"service-{index:04d}"
318
        service.mkdir(parents=True, exist_ok=True)
319
        timeout = 2500 if index % 3 == 0 else 1800
320
        service.joinpath("config.toml").write_text(
321
            "\n".join(
322
                [
323
                    f'name = "service-{index:04d}"',
324
                    f"timeout_ms = {timeout}",
325
                    "retries = 2",
326
                    "",
327
                ]
328
            )
329
        )
330
    (path / "validate_configs.py").write_text(
331
        "\n".join(
332
            [
333
                "from pathlib import Path",
334
                "bad = []",
335
                "for p in Path('services').glob('*/config.toml'):",
336
                "    text = p.read_text()",
337
                "    if 'timeout_ms = 2500' in text:",
338
                "        bad.append(str(p))",
339
                "print(f'bad={len(bad)}')",
340
                "raise SystemExit(1 if bad else 0)",
341
                "",
342
            ]
343
        )
344
    )
345
346
347
def wide_config_steps(subject: Subject, track: str) -> list[Step]:
348
    return [
349
        Step(
350
            "search.deprecated_timeout",
351
            "search",
352
            shell(search_command("timeout_ms = 2500", "services") + " | head -40"),
353
        ),
354
        Step("read.sample_config", "read", shell("sed -n '1,80p' services/service-0000/config.toml")),
355
        Step(
356
            "edit.rewrite_configs",
357
            "edit",
358
            shell(
359
                "python3 - <<'PY'\n"
360
                "from pathlib import Path\n"
361
                "for p in Path('services').glob('*/config.toml'):\n"
362
                "    text = p.read_text()\n"
363
                "    if 'timeout_ms = 2500' in text:\n"
364
                "        p.write_text(text.replace('timeout_ms = 2500', 'timeout_ms = 3000'))\n"
365
                "PY"
366
            ),
367
        ),
368
        Step("search.verify_none_left", "search", shell("! " + search_command("timeout_ms = 2500", "services"))),
369
        Step("test.validate_configs", "test", shell("python3 validate_configs.py")),
370
        *vcs_workflow_steps(subject, track),
371
    ]
372
373
374
def write_pattern_file(path: Path, size: int, seed: str) -> None:
375
    oakbench_fixtures.write_pattern_file(path, size, seed, binary=True, prefix="oak-workflow")
376
377
378
def make_large_asset_fixture(path: Path) -> None:
379
    assets = path / "assets"
380
    assets.mkdir(parents=True)
381
    manifest: list[dict[str, Any]] = []
382
    for index in range(4):
383
        asset_path = assets / f"asset-{index:02d}.bin"
384
        write_pattern_file(asset_path, 16 * 1024 * 1024, f"asset-{index}")
385
        manifest.append({"path": str(asset_path.relative_to(path)), "bytes": asset_path.stat().st_size, "reviewed": False})
386
    (path / "manifest.json").write_text(json.dumps({"assets": manifest}, indent=2) + "\n")
387
    (path / "validate_manifest.py").write_text(
388
        "\n".join(
389
            [
390
                "import json",
391
                "from pathlib import Path",
392
                "data = json.loads(Path('manifest.json').read_text())",
393
                "for item in data['assets']:",
394
                "    p = Path(item['path'])",
395
                "    assert p.exists(), p",
396
                "    assert p.stat().st_size == item['bytes'], p",
397
                "    assert item['reviewed'] is True, p",
398
                "print('manifest ok')",
399
                "",
400
            ]
401
        )
402
    )
403
404
405
def large_asset_steps(subject: Subject, track: str) -> list[Step]:
406
    return [
407
        Step("search.assets", "search", shell("find assets -type f -maxdepth 1 | sort")),
408
        Step("read.manifest", "read", shell("sed -n '1,120p' manifest.json")),
409
        Step(
410
            "read.binary_prefix",
411
            "read",
412
            shell("python3 - <<'PY'\nfrom pathlib import Path\nprint(Path('assets/asset-00.bin').read_bytes()[:64].hex())\nPY"),
413
        ),
414
        Step(
415
            "edit.mark_reviewed",
416
            "edit",
417
            shell(
418
                "python3 - <<'PY'\n"
419
                "import json\n"
420
                "from pathlib import Path\n"
421
                "p = Path('manifest.json')\n"
422
                "data = json.loads(p.read_text())\n"
423
                "for item in data['assets']:\n"
424
                "    item['reviewed'] = True\n"
425
                "p.write_text(json.dumps(data, indent=2) + '\\n')\n"
426
                "PY"
427
            ),
428
        ),
429
        Step("test.validate_manifest", "test", shell("python3 validate_manifest.py")),
430
        *vcs_workflow_steps(subject, track),
431
    ]
432
433
434
def make_history_fixture(path: Path) -> None:
435
    (path / "app").mkdir(parents=True)
436
    (path / "app" / "pricing.py").write_text(
437
        "\n".join(
438
            [
439
                "def apply_discount(cents: int, percent: int) -> int:",
440
                "    return cents - ((cents * percent) // 100)",
441
                "",
442
            ]
443
        )
444
    )
445
    (path / "app" / "totals.py").write_text("def total(items):\n    return sum(items)\n")
446
    (path / "README.md").write_text("History archaeology fixture for agent workflow benchmarks.\n")
447
448
449
def history_prepare_steps(subject: Subject, track: str) -> list[Step]:
450
    vcs = shlex.quote(str(subject.bin))
451
    steps: list[Step] = []
452
    edits = [
453
        ("app/pricing.py", "support negative-percent surcharges", "# rev: surcharge support"),
454
        ("app/totals.py", "guard empty item lists", "# rev: empty-list guard"),
455
        ("app/pricing.py", "clamp discount to 100 percent", "# rev: discount clamp"),
456
    ]
457
    for index, (target, message, marker) in enumerate(edits):
458
        if subject.kind == "git":
459
            script = (
460
                f"echo {shlex.quote(marker)} >> {shlex.quote(target)} && "
461
                f"{vcs} add . && {vcs} commit -m {shlex.quote(message)}"
462
            )
463
        else:
464
            script = f"echo {shlex.quote(marker)} >> {shlex.quote(target)} && {vcs} commit --no-verify"
465
        steps.append(Step(f"setup.history.commit_{index}", "vcs", shell(script)))
466
    return steps
467
468
469
def history_archaeology_steps(subject: Subject, track: str) -> list[Step]:
470
    # Agents constantly ask "what changed recently, in this file, and why".
471
    # Oak lacks show/blame/pickaxe equivalents today, so its rows use the
472
    # closest available command (oak log); the token/capability gap is the
473
    # measurement, and the command field records what actually ran.
474
    vcs = str(subject.bin)
475
    if subject.kind == "git":
476
        return [
477
            Step("history.recent", "vcs", [vcs, "log", "--oneline", "-15"]),
478
            Step("history.show_last", "vcs", [vcs, "show", "--stat", "HEAD"]),
479
            Step("history.file_history", "vcs", [vcs, "log", "--oneline", "-5", "--", "app/pricing.py"]),
480
            Step("history.pickaxe", "vcs", [vcs, "log", "-S", "surcharge", "--oneline"], (0, 1, 2)),
481
        ]
482
    return [
483
        Step("history.recent", "vcs", [vcs, "log", "-n", "15"]),
484
        Step("history.show_last", "vcs", [vcs, "log", "-n", "1", "-v"]),
485
        Step("history.file_history", "vcs", [vcs, "log", "-v"], (0, 1, 2)),
486
        Step("history.pickaxe", "vcs", [vcs, "log", "-v"], (0, 1, 2)),
487
    ]
488
489
490
def error_recovery_steps(subject: Subject, track: str) -> list[Step]:
491
    # Error output is a prompt: its size and clarity set how many tokens an
492
    # agent burns recovering. Failure-path rows are tolerated (wide expected
493
    # returncodes) so each subject's error text cost is recorded, not skipped.
494
    sem = semantics()
495
    vcs = str(subject.bin)
496
    if subject.kind == "git":
497
        commit_nothing = [vcs, "commit", "-m", "empty"]
498
    else:
499
        commit_nothing = [vcs, *sem.commit_args(subject.kind, "empty")]
500
    return [
501
        Step("error.commit_nothing", "vcs", commit_nothing, (0, 1, 2)),
502
        Step("error.unknown_subcommand", "vcs", [vcs, "stage-all-now"], (0, 1, 2, 101)),
503
        Step("error.status_after", "vcs", [vcs, "status"]),
504
        Step(
505
            "edit.recovery_change",
506
            "edit",
507
            shell("echo '# recovered' >> README.md"),
508
        ),
509
        *snapshot_steps(subject, "vcs.snapshot", "recovered"),
510
    ]
511
512
513
def make_sync_fixture(path: Path) -> None:
514
    (path / "work.txt").write_text("base line\n")
515
    (path / "notes.txt").write_text("shared notes\n")
516
    (path / "README.md").write_text("Sync/divergence recovery fixture for agent workflow benchmarks.\n")
517
518
519
def sync_prepare_steps(subject: Subject, track: str, ctx: dict[str, str]) -> list[Step]:
520
    """Untimed topology: link the repo to a remote, stand up a second checkout
521
    B, and move upstream from B so the measured checkout is behind.
522
523
    Git uses a local bare remote (local_file transport); oak uses the real
524
    server (network transport). Latency rows across subjects in sync workflows
525
    are therefore never comparable — the cross-subject evidence is error-output
526
    tokens, tool calls, and recovery step counts, which transport cannot change.
527
    """
528
    vcs = shlex.quote(str(subject.bin))
529
    bdir = shlex.quote(ctx["clone_b"])
530
    if subject.kind == "git":
531
        rdir = shlex.quote(ctx["remote_dir"])
532
        return [
533
            Step("setup.remote.create", "vcs", shell(f"{vcs} init -q --bare {rdir}")),
534
            Step(
535
                "setup.remote.link",
536
                "vcs",
537
                shell(f"{vcs} remote add origin {rdir} && {vcs} push -q -u origin HEAD"),
538
            ),
539
            Step("setup.remote.clone_b", "vcs", shell(f"{vcs} clone -q {rdir} {bdir}")),
540
            Step(
541
                "setup.upstream.move",
542
                "vcs",
543
                shell(
544
                    f"cd {bdir} && echo 'upstream change' >> notes.txt && "
545
                    f"{vcs} add . && {vcs} commit -q -m upstream && {vcs} push -q origin HEAD"
546
                ),
547
            ),
548
        ]
549
    return [
550
        *oak_clone_a_steps(subject, ctx),
551
        Step("setup.remote.clone_b", "vcs", shell(f"{vcs} clone {shlex.quote(ctx['oak_repo'])} {bdir}")),
552
        # B joins the shared branch (it descends from seeded main, so switch
553
        # pulls it) and moves it. Commit is local-only in current Oak, so setup
554
        # explicitly uses --push to publish the upstream actor's change.
555
        Step(
556
            "setup.upstream.move",
557
            "vcs",
558
            shell(
559
                f"cd {bdir} && {vcs} switch {shlex.quote(ctx['branch'])} && "
560
                f"echo 'upstream change' >> notes.txt && "
561
                f"{vcs} commit --no-verify --push"
562
            ),
563
        ),
564
    ]
565
566
567
# Reads the auto-generated current branch name so it can be renamed to the
568
# run's collision-safe bench branch (rename syncs to the server).
569
OAK_CURRENT_BRANCH_SNIPPET = (
570
    "branch list --json | /usr/bin/env python3 -c "
571
    '\'import json,sys; print(next(b["name"] for b in json.load(sys.stdin) if b.get("current")))\''
572
)
573
574
575
def oak_clone_a_steps(subject: Subject, ctx: dict[str, str]) -> list[Step]:
576
    """Untimed oak topology: clone A, seed the disposable repo's main with the
577
    static fixture once (idempotent — identical bytes merge as a no-op), and
578
    publish the run's shared bench branch via rename + explicit commit --push."""
579
    vcs = shlex.quote(str(subject.bin))
580
    adir = shlex.quote(ctx["clone_a"])
581
    repo = shlex.quote(ctx["oak_repo"])
582
    branch = shlex.quote(ctx["branch"])
583
    fixture_dir = shlex.quote(f"../{ctx['repo_dir']}")
584
    return [
585
        Step("setup.remote.clone_a", "vcs", shell(f"{vcs} clone {repo} {adir}")),
586
        Step(
587
            "setup.remote.seed_main",
588
            "vcs",
589
            shell(
590
                f"cd {adir} && if [ ! -f work.txt ]; then "
591
                f"cp {fixture_dir}/work.txt {fixture_dir}/notes.txt {fixture_dir}/README.md . && "
592
                f"{vcs} commit --no-verify --push && {vcs} merge; fi"
593
            ),
594
        ),
595
        # Commit and publish the auto-named branch, then rename: rename only
596
        # resolves a branch that exists server-side.
597
        Step(
598
            "setup.remote.link",
599
            "vcs",
600
            shell(
601
                f"cd {adir} && echo 'link line' >> README.md && {vcs} commit --no-verify --push && "
602
                f"cur=$({vcs} {OAK_CURRENT_BRANCH_SNIPPET}) && "
603
                f"{vcs} branch rename \"$cur\" {branch}"
604
            ),
605
        ),
606
    ]
607
608
609
def sync_push_divergence_steps(subject: Subject, track: str, ctx: dict[str, str]) -> list[Step]:
610
    # The trap an agent hits when any other actor pushed first. Expected
611
    # returncodes encode each subject's observed remote-failure contract; a
612
    # step that "fails to fail" still converges at verify.payload. Error
613
    # output is a prompt: the rejected push's text is what the agent reasons
614
    # from, and its cost is the measurement.
615
    vcs = str(subject.bin)
616
    verify = Step(
617
        "verify.payload",
618
        "read",
619
        shell("grep -q 'local change' work.txt && grep -q 'upstream change' notes.txt"),
620
    )
621
    if subject.kind == "git":
622
        return [
623
            Step("edit.local_change", "edit", shell("echo 'local change' >> work.txt")),
624
            *snapshot_steps(subject, "vcs.snapshot", "local change"),
625
            # Rejected non-fast-forward push; stderr says to pull.
626
            Step("error.push_diverged", "vcs", [vcs, "push"], (1,), recovery_hint="git pull"),
627
            # Following the hint verbatim fails again until a strategy is
628
            # chosen: fatal "Need to specify how to reconcile divergent
629
            # branches" (exit 128).
630
            Step("error.pull_unconfigured", "vcs", [vcs, "pull"], (128,), recovery_hint="--rebase"),
631
            Step("recovery.pull_rebase", "vcs", [vcs, "pull", "--rebase"]),
632
            Step("recovery.push_after", "vcs", [vcs, "push"]),
633
            verify,
634
        ]
635
    return [
636
        Step("edit.local_change", "edit", shell("echo 'local change' >> work.txt")),
637
        # oak commit is a local checkpoint. The publish failure is measured in
638
        # the explicit push row below.
639
        Step("vcs.snapshot", "vcs", [vcs, "commit", "--no-verify"], (0,)),
640
        Step("error.push_diverged", "vcs", [vcs, "push"], (0, 1, 5), recovery_hint="pull"),
641
        # The push error says "Pull changes first" — but pull also exits 5 on
642
        # a diverged branch and suggests only the destructive pull --force.
643
        Step("error.pull_diverged", "vcs", [vcs, "pull"], (5,), recovery_hint="pull --force"),
644
        # pull --force discards the local commit (observed: committed content
645
        # reverts). Recovery therefore includes redoing the lost work.
646
        Step("recovery.pull_force", "vcs", [vcs, "pull", "--force"]),
647
        Step("edit.redo_change", "edit", shell("echo 'local change' >> work.txt")),
648
        *snapshot_steps(subject, "vcs.snapshot.redo", "local change redo"),
649
        Step("recovery.push_after", "vcs", [vcs, "push"]),
650
        verify,
651
    ]
652
653
654
def sync_pull_upstream_steps(subject: Subject, track: str, ctx: dict[str, str]) -> list[Step]:
655
    # The everyday task-start motion: upstream moved, local tree clean.
656
    vcs = str(subject.bin)
657
    sem = semantics()
658
    return [
659
        Step("vcs.status", "vcs", [vcs, *sem.status_args(subject.kind, track)]),
660
        Step("sync.pull_upstream", "vcs", [vcs, "pull"]),
661
        Step("verify.payload", "read", shell("grep -q 'upstream change' notes.txt")),
662
    ]
663
664
665
def sync_amend_prepare_steps(subject: Subject, track: str, ctx: dict[str, str]) -> list[Step]:
666
    """Untimed: link the repo to a remote and push — no second actor. The
667
    divergence in this workflow is self-inflicted (history rewrite after
668
    push), so upstream never moves."""
669
    vcs = shlex.quote(str(subject.bin))
670
    if subject.kind == "git":
671
        rdir = shlex.quote(ctx["remote_dir"])
672
        return [
673
            Step("setup.remote.create", "vcs", shell(f"{vcs} init -q --bare {rdir}")),
674
            Step(
675
                "setup.remote.link",
676
                "vcs",
677
                shell(f"{vcs} remote add origin {rdir} && {vcs} push -q -u origin HEAD"),
678
            ),
679
        ]
680
    return oak_clone_a_steps(subject, ctx)
681
682
683
def sync_non_ff_after_amend_steps(subject: Subject, track: str, ctx: dict[str, str]) -> list[Step]:
684
    # The self-inflicted trap: fix the last commit after it was pushed. Git
685
    # reproduces it exactly (amend rewrites the pushed tip; push is rejected;
686
    # the error text suggests pull — which would merge the old tip back — not
687
    # the --force-with-lease an agent actually needs, so the actionability
688
    # check is expected to FAIL and that failure is the finding). Oak cannot
689
    # rewrite history (commits carry no messages; `oak desc` edits the branch
690
    # description), so the same intent is a follow-up commit with no trap:
691
    # zero error rows and zero recovery steps IS oak's measurement here.
692
    vcs = str(subject.bin)
693
    if subject.kind == "git":
694
        return [
695
            Step("edit.fix_last_commit", "edit", shell("echo 'amended line' >> work.txt")),
696
            Step(
697
                "vcs.amend",
698
                "vcs",
699
                shell(f"{shlex.quote(vcs)} add . && {shlex.quote(vcs)} commit -q --amend --no-edit"),
700
            ),
701
            Step("error.push_non_ff", "vcs", [vcs, "push"], (1,), recovery_hint="--force-with-lease"),
702
            Step("recovery.push_force_lease", "vcs", [vcs, "push", "--force-with-lease"]),
703
            Step("verify.payload", "read", shell("grep -q 'amended line' work.txt")),
704
        ]
705
    return [
706
        Step("edit.fix_last_commit", "edit", shell("echo 'amended line' >> work.txt")),
707
        *snapshot_steps(subject, "vcs.snapshot", "amended line"),
708
        Step("recovery_free.desc_update", "vcs", [vcs, "desc", "amended: follow-up fix"]),
709
        Step("recovery_free.push_after", "vcs", [vcs, "push"], (0,)),
710
        Step("verify.payload", "read", shell("grep -q 'amended line' work.txt")),
711
    ]
712
713
714
def sync_pull_dirty_steps(subject: Subject, track: str, ctx: dict[str, str]) -> list[Step]:
715
    # Same motion with uncommitted local edits to a different file — the
716
    # working state agents are usually in when they remember to sync.
717
    # The oracle is split so a failure names what was lost. The pull row
718
    # accepts both refusal and success contracts; the follow-up payload checks
719
    # report whether upstream and local dirty content survived.
720
    vcs = str(subject.bin)
721
    sem = semantics()
722
    return [
723
        Step("edit.dirty_change", "edit", shell("echo 'dirty local edit' >> work.txt")),
724
        Step("vcs.status", "vcs", [vcs, *sem.status_args(subject.kind, track)]),
725
        Step("sync.pull_dirty", "vcs", [vcs, "pull"], (0, 1, 2, 4, 5)),
726
        Step("verify.upstream_payload", "read", shell("grep -q 'upstream change' notes.txt")),
727
        Step("verify.local_payload", "read", shell("grep -q 'dirty local edit' work.txt")),
728
    ]
729
730
731
WORKFLOWS: dict[str, Workflow] = {
732
    "bugfix_test_loop": Workflow(
733
        "bugfix_test_loop",
734
        "Search, reproduce failing test, patch one source file, rerun tests, inspect and snapshot.",
735
        make_bugfix_fixture,
736
        bugfix_steps,
737
    ),
738
    "wide_config_refactor": Workflow(
739
        "wide_config_refactor",
740
        "Search and rewrite hundreds of small config files, validate, inspect and snapshot.",
741
        make_wide_config_fixture,
742
        wide_config_steps,
743
    ),
744
    "large_asset_manifest": Workflow(
745
        "large_asset_manifest",
746
        "Inspect large binary asset repo, edit small manifest only, validate, inspect and snapshot.",
747
        make_large_asset_fixture,
748
        large_asset_steps,
749
    ),
750
    "history_archaeology": Workflow(
751
        "history_archaeology",
752
        "Answer common history questions: recent commits, last change detail, file history, term search.",
753
        make_history_fixture,
754
        history_archaeology_steps,
755
        prepare=history_prepare_steps,
756
    ),
757
    "vcs_error_recovery": Workflow(
758
        "vcs_error_recovery",
759
        "Hit common VCS errors (empty commit, unknown subcommand), then recover; measures error-output token cost.",
760
        make_history_fixture,
761
        error_recovery_steps,
762
    ),
763
    "sync_push_divergence": Workflow(
764
        "sync_push_divergence",
765
        "Push rejected because upstream moved, then the full recovery arc. "
766
        "Cross-subject evidence is tokens/calls/steps, never latency: git syncs a "
767
        "local bare remote, oak a real server.",
768
        make_sync_fixture,
769
        sync_push_divergence_steps,
770
        prepare=sync_prepare_steps,
771
        remote_purpose="sync",
772
    ),
773
    "sync_pull_upstream": Workflow(
774
        "sync_pull_upstream",
775
        "Clean-tree pull with upstream changes — the task-start sync motion. "
776
        "Latency is transport-dependent; compare tokens and calls only.",
777
        make_sync_fixture,
778
        sync_pull_upstream_steps,
779
        prepare=sync_prepare_steps,
780
        remote_purpose="sync",
781
    ),
782
    "sync_pull_dirty": Workflow(
783
        "sync_pull_dirty",
784
        "Pull with upstream changes while holding uncommitted local edits. "
785
        "Latency is transport-dependent; compare tokens and calls only.",
786
        make_sync_fixture,
787
        sync_pull_dirty_steps,
788
        prepare=sync_prepare_steps,
789
        remote_purpose="sync",
790
    ),
791
    "sync_non_ff_after_amend": Workflow(
792
        "sync_non_ff_after_amend",
793
        "Fix the last commit after pushing. Git: amend rewrites the pushed tip, "
794
        "push is rejected, recovery needs --force-with-lease (which the error "
795
        "text does not suggest). Oak: history rewrite does not exist, so the "
796
        "same intent is a trap-free follow-up commit — zero recovery steps is "
797
        "the measurement, not a missing row.",
798
        make_sync_fixture,
799
        sync_non_ff_after_amend_steps,
800
        prepare=sync_amend_prepare_steps,
801
        remote_purpose="sync",
802
    ),
803
}
804
805
806
def sync_context(
807
    subject: Subject,
808
    workflow: Workflow,
809
    run_index: int,
810
    metadata: dict[str, Any],
811
) -> tuple[dict[str, str] | None, str | None, dict[str, Any]]:
812
    """Per-run remote context for remote_purpose workflows.
813
814
    Returns (ctx, skip_reason, remote_row_fields). ctx is None when the
815
    subject's remote is not configured; the lane emits a skip row instead.
816
    """
817
    branch = oakbench_remotes.disposable_branch(
818
        str(metadata.get("bench_id", "bench")), workflow.name, subject.name, run_index
819
    )
820
    ctx: dict[str, str] = {
821
        "branch": branch,
822
        "remote_dir": f"../remote-run-{run_index}.git",
823
        "clone_b": f"../clone-b-run-{run_index}",
824
    }
825
    if subject.kind != "git":
826
        resolution = oakbench_remotes.resolve_oak_remote(workflow.remote_purpose or "default")
827
        if not resolution.resolved:
828
            return None, resolution.skip_reason, {}
829
        capability_skip = oak_commit_push_skip_reason(subject)
830
        if capability_skip is not None:
831
            return None, capability_skip, resolution.row_fields()
832
        ctx["oak_repo"] = resolution.repo or ""
833
        # Oak topology is clone-based: every branch must descend from the
834
        # disposable repo's seeded main (locally-init'ed history is invisible
835
        # to other clones). Measured steps run inside clone A, the checkout
836
        # that hits the trap; the local fixture copy only seeds main.
837
        ctx["clone_a"] = f"../clone-a-run-{run_index}"
838
        ctx["repo_dir"] = f"run-{run_index}"
839
        ctx["measure_dir"] = ctx["clone_a"]
840
        return ctx, None, resolution.row_fields()
841
    return ctx, None, {
842
        "remote_repo": ctx["remote_dir"],
843
        "remote_transport": oakbench_remotes.TRANSPORT_LOCAL_FILE,
844
        "remote_server": "local",
845
    }
846
847
848
def workflow_skip_row(
849
    subject: Subject,
850
    workflow: Workflow,
851
    run_index: int,
852
    metadata: dict[str, Any],
853
    reason: str,
854
) -> dict[str, Any]:
855
    return {
856
        **metadata,
857
        "subject": subject.name,
858
        "subject_kind": subject.kind,
859
        "subject_label": subject.label,
860
        "scenario": workflow.name,
861
        "workflow": workflow.name,
862
        "workflow_description": workflow.description,
863
        "run": run_index,
864
        "phase": "workflow",
865
        "operation": "workflow.skipped",
866
        "step_kind": "summary",
867
        "elapsed_ms": 0.0,
868
        "returncode": 77,
869
        "process_returncode": 77,
870
        "command": [],
871
        "skipped": True,
872
        "skip_reason": reason,
873
        "tool_call_count": 0,
874
        "terminal_tool_call_count": 0,
875
        "vcs_tool_call_count": 0,
876
        "test_tool_call_count": 0,
877
    }
878
879
880
def load_tier_skip_rows(
881
    subjects: list[Subject],
882
    workflows: list[Workflow],
883
    runs: int,
884
    metadata: dict[str, Any],
885
    reason: str,
886
    load_tier: str,
887
) -> list[dict[str, Any]]:
888
    """Explicit skip rows for a load tier the host cannot apply.
889
890
    One row per workflow x run x subject; environment_suspect is None because
891
    the suspicion machinery never ran (ADR-0002), and load_tier records the
892
    REQUESTED tier — load that was not applied is never claimed.
893
    """
894
    rows: list[dict[str, Any]] = []
895
    for workflow in workflows:
896
        for run_index in range(runs):
897
            for subject in subjects:
898
                row = workflow_skip_row(subject, workflow, run_index, metadata, reason)
899
                row["load_tier"] = load_tier
900
                row["environment_suspect"] = None
901
                rows.append(row)
902
    return rows
903
904
905
def interaction_metrics(
906
    command: list[str],
907
    stdout_text: str,
908
    stderr_text: str,
909
    stdout_bytes: int,
910
    stderr_bytes: int,
911
    stdout_truncated: bool,
912
    stderr_truncated: bool,
913
    step: Step,
914
) -> dict[str, Any]:
915
    is_vcs = 1 if step.kind == "vcs" else 0
916
    is_test = 1 if step.kind == "test" else 0
917
    return {
918
        "tool_call_count": 1,
919
        "terminal_tool_call_count": 1,
920
        "vcs_tool_call_count": is_vcs,
921
        "test_tool_call_count": is_test,
922
        **oakbench_tokens.interaction_token_fields(
923
            command_display(command),
924
            stdout_text,
925
            stderr_text,
926
            stdout_bytes,
927
            stderr_bytes,
928
            stdout_truncated,
929
            stderr_truncated,
930
        ),
931
        "tool_calls": {
932
            "total": 1,
933
            "terminal": 1,
934
            "vcs": is_vcs,
935
            "test": is_test,
936
            step.kind: 1,
937
        },
938
    }
939
940
941
def run_step(
942
    subject: Subject,
943
    workflow: Workflow,
944
    step: Step,
945
    cwd: Path,
946
    run_index: int,
947
    phase: str,
948
    metadata: dict[str, Any],
949
    admitted_output_chars: int,
950
) -> dict[str, Any]:
951
    capture = run_timed(step.command, cwd, step_env(step), admitted_output_chars)
952
    expected = capture.returncode in step.expected_returncodes
953
    row = {
954
        **metadata,
955
        "subject": subject.name,
956
        "subject_kind": subject.kind,
957
        "subject_label": subject.label,
958
        "scenario": workflow.name,
959
        "workflow": workflow.name,
960
        "workflow_description": workflow.description,
961
        "run": run_index,
962
        "phase": phase,
963
        "operation": step.operation,
964
        "step_kind": step.kind,
965
        "elapsed_ms": round(capture.elapsed_ms, 3),
966
        "returncode": 0 if expected else (capture.returncode or 1),
967
        "process_returncode": capture.returncode,
968
        "expected_returncodes": list(step.expected_returncodes),
969
        "command": step.command,
970
        **interaction_metrics(
971
            step.command,
972
            capture.stdout_text,
973
            capture.stderr_text,
974
            capture.stdout_bytes,
975
            capture.stderr_bytes,
976
            capture.stdout_truncated,
977
            capture.stderr_truncated,
978
            step,
979
        ),
980
    }
981
    if not expected:
982
        row["stderr"] = capture.stderr_text[-4000:]
983
    if step.recovery_hint is not None:
984
        combined = (capture.stdout_text + capture.stderr_text).lower()
985
        row["recovery_hint"] = step.recovery_hint
986
        row["error_mentions_recovery_command"] = step.recovery_hint.lower() in combined
987
        row["error_actionability_source"] = "output_contains_recovery_command"
988
    return row
989
990
991
def snapshot_step_requires_state_change(operation: str) -> bool:
992
    return operation in {"setup.snapshot", "vcs.snapshot", "vcs.snapshot.redo"} or (
993
        ".snapshot" in operation and operation.endswith(".commit")
994
    )
995
996
997
def branch_step_requires_state_change(operation: str) -> bool:
998
    return operation == "vcs.branch"
999
1000
1001
def apply_state_change_attestation(
1002
    row: dict[str, Any],
1003
    repo: Path,
1004
    before_fingerprint: str | None,
1005
    operation: str,
1006
    *,
1007
    expected_hashes: dict[str, str] | None = None,
1008
    expected_payloads: dict[str, str | bytes] | None = None,
1009
) -> dict[str, Any]:
1010
    raw_process_returncode = row.get("process_returncode")
1011
    if raw_process_returncode is None:
1012
        process_returncode = row_returncode(row)
1013
    else:
1014
        try:
1015
            process_returncode = int(raw_process_returncode)
1016
        except (TypeError, ValueError):
1017
            process_returncode = 1
1018
    fields = oakbench_integrity.state_change_fields(
1019
        repo,
1020
        before_fingerprint,
1021
        operation=operation,
1022
        process_returncode=process_returncode,
1023
    )
1024
    row.update(fields)
1025
    failure_reason = fields["integrity_failure_reason"] if not fields["integrity_check_passed"] else None
1026
    if expected_hashes or expected_payloads:
1027
        content_fields = oakbench_integrity.content_integrity_fields(
1028
            repo,
1029
            operation=operation,
1030
            process_returncode=process_returncode,
1031
            expected_hashes=expected_hashes,
1032
            expected_payloads=expected_payloads,
1033
        )
1034
        row.update(content_fields)
1035
        if not content_fields["content_integrity_check_passed"] and failure_reason is None:
1036
            failure_reason = content_fields["content_integrity_failure_reason"]
1037
    if process_returncode == 0 and failure_reason is not None:
1038
        row["returncode"] = 1
1039
        row["stderr"] = str(failure_reason or "integrity_check_failed")
1040
    return row
1041
1042
1043
def workflow_step_payload_requirements(workflow: Workflow, operation: str) -> list[tuple[str, str]]:
1044
    """Content payloads required at an intermediate step.
1045
1046
    Final workflow integrity can require the fully converged payload. Snapshot
1047
    steps must only require payloads that are expected to exist in that phase.
1048
    """
1049
    if workflow.name == "sync_push_divergence" and operation in {
1050
        "vcs.snapshot",
1051
        "vcs.snapshot.commit",
1052
        "vcs.snapshot.redo",
1053
        "vcs.snapshot.redo.commit",
1054
    }:
1055
        return [("work.txt", "local change")]
1056
    return []
1057
1058
1059
def apply_content_payload_attestation(
1060
    row: dict[str, Any],
1061
    repo: Path,
1062
    expected_payloads: list[tuple[str, str]],
1063
) -> dict[str, Any]:
1064
    if not expected_payloads:
1065
        return row
1066
    fields = oakbench_integrity.content_payload_fields(repo, expected_payloads)
1067
    row.update(fields)
1068
    if row_returncode(row) == 0 and not fields["content_payload_check_passed"]:
1069
        reason = str(fields["content_payload_failure_reason"] or "content_payload_check_failed")
1070
        row["returncode"] = 1
1071
        row["process_returncode"] = 1
1072
        row["stderr"] = reason
1073
        row["integrity_check_passed"] = False
1074
        row["integrity_failure_reason"] = reason
1075
        row["integrity_check_source"] = "content_payload_attestation"
1076
    return row
1077
1078
1079
def run_attested_step(
1080
    subject: Subject,
1081
    workflow: Workflow,
1082
    step: Step,
1083
    cwd: Path,
1084
    run_index: int,
1085
    phase: str,
1086
    metadata: dict[str, Any],
1087
    admitted_output_chars: int,
1088
) -> dict[str, Any]:
1089
    requires_state_change = snapshot_step_requires_state_change(step.operation) or branch_step_requires_state_change(
1090
        step.operation
1091
    )
1092
    before = oakbench_integrity.repository_state_fingerprint(cwd) if requires_state_change else None
1093
    expected_hashes = None
1094
    expected_payloads = None
1095
    if snapshot_step_requires_state_change(step.operation):
1096
        expected_hashes = oakbench_integrity.sample_worktree_payload_hashes(cwd)
1097
        if phase == "workflow":
1098
            expected_payloads = workflow_snapshot_payload_expectations(workflow)
1099
    row = run_step(subject, workflow, step, cwd, run_index, phase, metadata, admitted_output_chars)
1100
    if requires_state_change:
1101
        apply_state_change_attestation(
1102
            row,
1103
            cwd,
1104
            before,
1105
            step.operation,
1106
            expected_hashes=expected_hashes,
1107
            expected_payloads=expected_payloads,
1108
        )
1109
    apply_content_payload_attestation(
1110
        row,
1111
        cwd,
1112
        workflow_step_payload_requirements(workflow, step.operation),
1113
    )
1114
    return row
1115
1116
1117
def _read_text(path: Path) -> str | None:
1118
    try:
1119
        return path.read_text(errors="replace")
1120
    except OSError:
1121
        return None
1122
1123
1124
def workflow_snapshot_payload_expectations(workflow: Workflow) -> dict[str, str]:
1125
    return workflow_oracles.COMMITTED_PAYLOADS.get(workflow.name, {
1126
        "wide_config_refactor": {"services/service-0000/config.toml": "timeout_ms = 3000"},
1127
        "large_asset_manifest": {"manifest.json": '\"reviewed\": true'},
1128
    }.get(workflow.name, {}))
1129
1130
1131
def workflow_worktree_payload_failure(workflow: Workflow, repo: Path) -> str | None:
1132
    name = workflow.name
1133
    if name == "bugfix_test_loop":
1134
        text = _read_text(repo / "app" / "pricing.py")
1135
        expected = "return cents - ((cents * percent) // 100)"
1136
        return None if text is not None and expected in text else "missing bugfix payload in app/pricing.py"
1137
    if name in workflow_oracles.SCENARIOS:
1138
        return workflow_oracles.validate(name, repo)
1139
    if name == "vcs_error_recovery":
1140
        text = _read_text(repo / "README.md")
1141
        return None if text is not None and "# recovered" in text else "recovery payload missing in README.md"
1142
    if name == "sync_push_divergence":
1143
        work = _read_text(repo / "work.txt") or ""
1144
        notes = _read_text(repo / "notes.txt") or ""
1145
        if "local change" not in work:
1146
            return "local pushed payload missing from work.txt"
1147
        if "upstream change" not in notes:
1148
            return "upstream pulled payload missing from notes.txt"
1149
        return None
1150
    if name == "sync_pull_upstream":
1151
        notes = _read_text(repo / "notes.txt") or ""
1152
        return None if "upstream change" in notes else "upstream pulled payload missing from notes.txt"
1153
    if name == "sync_pull_dirty":
1154
        work = _read_text(repo / "work.txt") or ""
1155
        notes = _read_text(repo / "notes.txt") or ""
1156
        if "dirty local edit" not in work:
1157
            return "dirty local payload missing from work.txt"
1158
        if "upstream change" not in notes:
1159
            return "upstream pulled payload missing from notes.txt"
1160
        return None
1161
    if name == "sync_non_ff_after_amend":
1162
        work = _read_text(repo / "work.txt") or ""
1163
        return None if "amended line" in work else "amended payload missing from work.txt"
1164
    return None
1165
1166
1167
def _git_head_text(repo: Path, path: str, git_binary: str | None = None, resolved_instrument=None) -> tuple[str | None, str | None]:
1168
    return workflow_oracles.git_head_text(repo, path, git_binary, resolved_instrument=resolved_instrument)
1169
1170
1171
def workflow_git_head_payload_failure(workflow: Workflow, repo: Path, git_binary: str | None = None, resolved_instrument=None) -> str | None:
1172
    if workflow.name in workflow_oracles.SCENARIOS:
1173
        return workflow_oracles.validate_git_head(workflow.name, repo, git_binary, resolved_instrument=resolved_instrument)
1174
    checks = workflow_snapshot_payload_expectations(workflow)
1175
    if not checks:
1176
        return None
1177
    for path, expected in checks.items():
1178
        text, error = _git_head_text(repo, path, git_binary, resolved_instrument)
1179
        if error is not None:
1180
            return error
1181
        if text is None or expected not in text:
1182
            return f"committed_payload_missing:{path}"
1183
    return None
1184
1185
1186
def workflow_integrity_fields(
1187
    subject: Subject,
1188
    workflow: Workflow,
1189
    repo: Path,
1190
    workflow_rows: list[dict[str, Any]],
1191
) -> dict[str, Any]:
1192
    failures = [row for row in workflow_rows if row_returncode(row) != 0]
1193
    snapshot_failures = [
1194
        row
1195
        for row in workflow_rows
1196
        if row.get("integrity_check_passed") is False or row.get("content_integrity_check_passed") is False
1197
    ]
1198
    if workflow.name in workflow_oracles.SCENARIOS and not failures and not snapshot_failures:
1199
        evidence = workflow_oracles.task_evidence(workflow.name, repo, subject.kind, str(subject.bin))
1200
        return {**evidence, "correctness_passed": evidence["workflow_integrity_passed"],
1201
                "correctness_failure_reason": evidence["workflow_integrity_failure_reason"]}
1202
    reason: str | None = None
1203
    source = "trusted_exact_task_worktree" if workflow.name in workflow_oracles.SCENARIOS else "worktree_payload_oracle"
1204
    if failures:
1205
        first = failures[0]
1206
        reason = str(
1207
            first.get("integrity_failure_reason")
1208
            or first.get("content_integrity_failure_reason")
1209
            or f"workflow_step_failed:{first.get('operation')}"
1210
        )
1211
    elif snapshot_failures:
1212
        first = snapshot_failures[0]
1213
        reason = str(
1214
            first.get("integrity_failure_reason")
1215
            or first.get("content_integrity_failure_reason")
1216
            or f"snapshot_integrity_failed:{first.get('operation')}"
1217
        )
1218
1219
    if reason is None:
1220
        reason = workflow_worktree_payload_failure(workflow, repo)
1221
1222
    reader_fields = {}
1223
    evidence_class = "worktree_payload"
1224
    if reason is None and subject.kind == "git" and workflow_snapshot_payload_expectations(workflow):
1225
        instrument, reader_fields = workflow_oracles.resolve_git_instrument()
1226
        reason = "git_head_oracle_unavailable" if instrument is None else workflow_git_head_payload_failure(workflow, repo, resolved_instrument=(instrument, reader_fields))
1227
        if instrument is not None and reason is not None:
1228
            evidence_class = "committed_payload_attempt"
1229
            source += ";git_head_payload_read_attempt"
1230
        if reason is None:
1231
            reader_fields.update(workflow_oracles.reader_evidence("git", str(subject.bin), reader_fields))
1232
            evidence_class = reader_fields.pop("workflow_integrity_evidence_class").replace("_tree", "_payload")
1233
            source += ";" + evidence_class
1234
    elif reason is None and any(row.get("integrity_check_passed") is True for row in workflow_rows):
1235
        source += ";direct_repo_metadata_fingerprint"
1236
        evidence_class = "repository_metadata"
1237
1238
    unavailable = reason in {"git_head_oracle_unavailable", "known_good_git_unavailable_for_head_payload_check"}
1239
    passed = None if unavailable else reason is None
1240
    return {
1241
        "oracle_version": workflow_oracles.VERSION,
1242
        "workflow_integrity_passed": passed,
1243
        "workflow_integrity_failure_reason": reason,
1244
        "workflow_integrity_source": source,
1245
        **reader_fields,
1246
        "workflow_integrity_evidence_class": evidence_class,
1247
        "workflow_integrity_unmeasured_reason": reason if unavailable else None,
1248
        "correctness_passed": passed,
1249
        "correctness_failure_reason": reason,
1250
    }
1251
1252
1253
def recovery_metrics_row(
1254
    workflow_rows: list[dict[str, Any]],
1255
    metadata: dict[str, Any],
1256
    subject: Subject,
1257
    workflow: Workflow,
1258
    run_index: int,
1259
) -> dict[str, Any] | None:
1260
    """Derived recovery-cost row for workflows that hit expected failures.
1261
1262
    Error output is a prompt: the rejected command's text is what the agent
1263
    reasons from. This row prices that text (model-input tokens) and the
1264
    recovery arc that follows it. tool_call_count is 0 — the row performs no
1265
    calls and must not perturb tool-call or token deltas (bench.py probe-row
1266
    precedent).
1267
    """
1268
    error_rows = [r for r in workflow_rows if str(r.get("operation", "")).startswith("error.")]
1269
    recovery_prefixes = ("recovery.", "edit.redo", "vcs.snapshot.redo")
1270
    recovery_free_prefixes = ("recovery_free.",)
1271
    recovery_rows = [
1272
        r for r in workflow_rows if str(r.get("operation", "")).startswith(recovery_prefixes)
1273
    ]
1274
    recovery_free_rows = [
1275
        r for r in workflow_rows if str(r.get("operation", "")).startswith(recovery_free_prefixes)
1276
    ]
1277
    if not error_rows and not recovery_rows and not recovery_free_rows:
1278
        return None
1279
    hinted = [r for r in error_rows if "error_mentions_recovery_command" in r]
1280
    actionable = [r for r in hinted if r.get("error_mentions_recovery_command")]
1281
    return {
1282
        **metadata,
1283
        "subject": subject.name,
1284
        "subject_kind": subject.kind,
1285
        "subject_label": subject.label,
1286
        "scenario": workflow.name,
1287
        "workflow": workflow.name,
1288
        "workflow_description": workflow.description,
1289
        "run": run_index,
1290
        "phase": "workflow",
1291
        "operation": "sync.recovery_metrics",
1292
        "step_kind": "summary",
1293
        "elapsed_ms": 0.0,
1294
        "returncode": 0,
1295
        "process_returncode": 0,
1296
        "command": [],
1297
        "tool_call_count": 0,
1298
        "terminal_tool_call_count": 0,
1299
        "vcs_tool_call_count": 0,
1300
        "test_tool_call_count": 0,
1301
        "error_steps_count": len(error_rows),
1302
        # The failure text an agent must ingest before it can act, priced as
1303
        # model input (output of the tool, input to the model).
1304
        "error_output_tokens": sum(int(r.get("estimated_tokens_output", 0)) for r in error_rows),
1305
        "recovery_steps_count": len(recovery_rows),
1306
        "recovery_wall_ms": round(sum(float(r.get("elapsed_ms", 0)) for r in recovery_rows), 3),
1307
        "recovery_tokens_total": round(
1308
            sum(float(r.get("estimated_cost_weighted_tokens_with_envelope", 0)) for r in recovery_rows), 1
1309
        ),
1310
        "error_steps_with_hint": len(hinted),
1311
        "error_steps_hint_actionable": len(actionable),
1312
        # Null when no step declared a hint: unmeasured, never a fabricated pass.
1313
        "error_actionability": (len(actionable) == len(hinted)) if hinted else None,
1314
        "measurement_source": "derived_from_workflow_rows; hint check is output_contains_recovery_command",
1315
        "summarized_operations": [r["operation"] for r in error_rows + recovery_rows + recovery_free_rows],
1316
    }
1317
1318
1319
def sum_rows(
1320
    rows: list[dict[str, Any]],
1321
    metadata: dict[str, Any],
1322
    subject: Subject,
1323
    workflow: Workflow,
1324
    run_index: int,
1325
    phase: str,
1326
    operation: str,
1327
    extra_fields: dict[str, Any] | None = None,
1328
) -> dict[str, Any]:
1329
    elapsed_ms = sum(float(row.get("elapsed_ms", 0)) for row in rows)
1330
    tool_calls = sum(oakbench_tokens.int_or_zero(row.get("tool_call_count")) for row in rows)
1331
    terminal_calls = sum(oakbench_tokens.int_or_zero(row.get("terminal_tool_call_count")) for row in rows)
1332
    vcs_calls = sum(oakbench_tokens.int_or_zero(row.get("vcs_tool_call_count")) for row in rows)
1333
    test_calls = sum(oakbench_tokens.int_or_zero(row.get("test_tool_call_count")) for row in rows)
1334
    failures = [row for row in rows if row_returncode(row) != 0]
1335
    steps_total = len(rows)
1336
    steps_failed = len(failures)
1337
    result = {
1338
        **metadata,
1339
        "subject": subject.name,
1340
        "subject_kind": subject.kind,
1341
        "subject_label": subject.label,
1342
        "scenario": workflow.name,
1343
        "workflow": workflow.name,
1344
        "workflow_description": workflow.description,
1345
        "run": run_index,
1346
        "phase": phase,
1347
        "operation": operation,
1348
        "step_kind": "summary",
1349
        "elapsed_ms": round(elapsed_ms, 3),
1350
        "returncode": 1 if failures else 0,
1351
        "process_returncode": 1 if failures else 0,
1352
        "command": [row["command"] for row in rows],
1353
        "tool_call_count": tool_calls,
1354
        "terminal_tool_call_count": terminal_calls,
1355
        "vcs_tool_call_count": vcs_calls,
1356
        "test_tool_call_count": test_calls,
1357
        **oakbench_tokens.summed_token_fields(
1358
            rows, "sum_of_workflow_steps_command_plus_admitted_output_chars_div_4"
1359
        ),
1360
        "tool_calls": {
1361
            "total": tool_calls,
1362
            "terminal": terminal_calls,
1363
            "vcs": vcs_calls,
1364
            "test": test_calls,
1365
        },
1366
        "steps_total": steps_total,
1367
        "steps_succeeded": steps_total - steps_failed,
1368
        "steps_failed": steps_failed,
1369
        "step_failure_rate": (steps_failed / steps_total) if steps_total else None,
1370
        "summarized_operations": [row["operation"] for row in rows],
1371
    }
1372
    if extra_fields:
1373
        result.update(extra_fields)
1374
        if extra_fields.get("workflow_integrity_passed") is False:
1375
            result["returncode"] = 1
1376
            result["process_returncode"] = 1
1377
        elif extra_fields.get("workflow_integrity_unmeasured_reason"):
1378
            result["returncode"] = 77
1379
            result["skipped"] = True
1380
            result["skip_reason"] = extra_fields["workflow_integrity_unmeasured_reason"]
1381
    return result
1382
1383
1384
def copy_fixture(src: Path, dest: Path) -> None:
1385
    oakbench_fixtures.copy_fixture(src, dest)
1386
1387
1388
def prepare_fixture(root: Path, workflow: Workflow) -> Path:
1389
    fixture = root / "fixtures" / workflow.name
1390
    marker = fixture / ".fixture-ready"
1391
    if marker.exists() and marker.read_text().strip() == FIXTURE_VERSION:
1392
        return fixture
1393
    if fixture.exists():
1394
        shutil.rmtree(fixture)
1395
    fixture.mkdir(parents=True)
1396
    workflow.make_fixture(fixture)
1397
    marker.write_text(FIXTURE_VERSION + "\n")
1398
    return fixture
1399
1400
1401
def run_subject_workflow(
1402
    subject: Subject,
1403
    workflow: Workflow,
1404
    fixture: Path,
1405
    run_index: int,
1406
    run_root: Path,
1407
    metadata: dict[str, Any],
1408
    admitted_output_chars: int,
1409
    track: str,
1410
) -> list[dict[str, Any]]:
1411
    repo = run_root / workflow.name / subject.name / f"run-{run_index}"
1412
    copy_fixture(fixture, repo)
1413
1414
    ctx: dict[str, str] | None = None
1415
    if workflow.remote_purpose is not None:
1416
        ctx, skip_reason, remote_fields = sync_context(subject, workflow, run_index, metadata)
1417
        if ctx is None:
1418
            return [workflow_skip_row(subject, workflow, run_index, metadata, skip_reason or "")]
1419
        metadata = {**metadata, **remote_fields}
1420
1421
    setup_steps = list(vcs_init_steps(subject))
1422
    if workflow.prepare is not None:
1423
        setup_steps.extend(
1424
            workflow.prepare(subject, track) if ctx is None else workflow.prepare(subject, track, ctx)
1425
        )
1426
    setup_rows = [
1427
        run_attested_step(subject, workflow, step, repo, run_index, "setup", metadata, admitted_output_chars)
1428
        for step in setup_steps
1429
    ]
1430
1431
    workflow_rows: list[dict[str, Any]] = []
1432
    if any(int(row["returncode"]) != 0 for row in setup_rows):
1433
        return setup_rows + [
1434
            sum_rows(setup_rows, metadata, subject, workflow, run_index, "setup", "setup.total"),
1435
        ]
1436
1437
    # Oak remote workflows measure inside clone A (the trap-hitting checkout);
1438
    # everything else measures in the fixture repo itself.
1439
    measure_dir = repo
1440
    if ctx is not None and "measure_dir" in ctx:
1441
        measure_dir = (repo / ctx["measure_dir"]).resolve()
1442
    for step in workflow.steps(subject, track) if ctx is None else workflow.steps(subject, track, ctx):
1443
        row = run_attested_step(
1444
            subject, workflow, step, measure_dir, run_index, "workflow", metadata, admitted_output_chars
1445
        )
1446
        workflow_rows.append(row)
1447
        if int(row["returncode"]) != 0:
1448
            break
1449
1450
    derived = recovery_metrics_row(workflow_rows, metadata, subject, workflow, run_index)
1451
    integrity = workflow_integrity_fields(subject, workflow, measure_dir, workflow_rows)
1452
    return [
1453
        *setup_rows,
1454
        sum_rows(setup_rows, metadata, subject, workflow, run_index, "setup", "setup.total"),
1455
        *workflow_rows,
1456
        sum_rows(
1457
            workflow_rows,
1458
            metadata,
1459
            subject,
1460
            workflow,
1461
            run_index,
1462
            "workflow",
1463
            "workflow.total",
1464
            integrity,
1465
        ),
1466
        *([derived] if derived else []),
1467
    ]
1468
1469
1470
def select_workflows(raw: str) -> list[Workflow]:
1471
    if raw == "all":
1472
        return list(WORKFLOWS.values())
1473
    names = [item.strip() for item in raw.split(",") if item.strip()]
1474
    missing = [name for name in names if name not in WORKFLOWS]
1475
    if missing:
1476
        raise SystemExit("Unknown workflows: " + ", ".join(missing))
1477
    return [WORKFLOWS[name] for name in names]
1478
1479
1480
def remote_preflight_requirements(
1481
    subjects: list[Subject],
1482
    workflows: list[Workflow],
1483
) -> list[oakbench_remotes.RemoteRequirement]:
1484
    if not any(subject.kind == "oak" for subject in subjects):
1485
        return []
1486
    grouped: dict[str, list[str]] = {}
1487
    for workflow in workflows:
1488
        if workflow.remote_purpose is None:
1489
            continue
1490
        grouped.setdefault(workflow.remote_purpose, []).append(f"{workflow.name}/workflow.skipped")
1491
    return [
1492
        oakbench_remotes.RemoteRequirement(
1493
            purpose=purpose,
1494
            lane="workflow",
1495
            operations=tuple(operations),
1496
        )
1497
        for purpose, operations in sorted(grouped.items())
1498
    ]
1499
1500
1501
def metric_average(rows: list[dict[str, Any]], metric: str) -> dict[tuple[str, str], float]:
1502
    grouped: dict[tuple[str, str], list[float]] = {}
1503
    for row in rows:
1504
        if row.get("operation") != "workflow.total" or row_returncode(row) != 0:
1505
            continue
1506
        try:
1507
            value = float(row[metric])
1508
        except (KeyError, TypeError, ValueError):
1509
            continue
1510
        grouped.setdefault((str(row["subject"]), str(row["workflow"])), []).append(value)
1511
    return {key: statistics.mean(values) for key, values in grouped.items()}
1512
1513
1514
def metric_transport(rows: list[dict[str, Any]]) -> dict[tuple[str, str], tuple[str | None, str | None]]:
1515
    transports: dict[tuple[str, str], set[tuple[str | None, str | None]]] = {}
1516
    for row in rows:
1517
        if row.get("operation") != "workflow.total" or row_returncode(row) != 0:
1518
            continue
1519
        key = (str(row["subject"]), str(row["workflow"]))
1520
        transport = row.get("remote_transport") or row.get("workspace_transport")
1521
        server = row.get("remote_server") or row.get("workspace_server")
1522
        transports.setdefault(key, set()).add(
1523
            (
1524
                str(transport) if transport is not None else None,
1525
                str(server) if server is not None else None,
1526
            )
1527
        )
1528
1529
    result: dict[tuple[str, str], tuple[str | None, str | None]] = {}
1530
    for key, values in transports.items():
1531
        if len(values) == 1:
1532
            result[key] = next(iter(values))
1533
        else:
1534
            result[key] = ("mixed", "mixed")
1535
    return result
1536
1537
1538
def workflow_total_stats(rows: list[dict[str, Any]]) -> dict[tuple[str, str], dict[str, int | float]]:
1539
    grouped: dict[tuple[str, str], list[dict[str, Any]]] = {}
1540
    for row in rows:
1541
        if row.get("operation") != "workflow.total":
1542
            continue
1543
        grouped.setdefault((str(row["subject"]), str(row["workflow"])), []).append(row)
1544
    stats: dict[tuple[str, str], dict[str, int | float]] = {}
1545
    for key, total_rows in grouped.items():
1546
        runs = len(total_rows)
1547
        failures = sum(1 for row in total_rows if row_returncode(row) != 0)
1548
        stats[key] = {
1549
            "runs": runs,
1550
            "successes": runs - failures,
1551
            "failures": failures,
1552
            "failure_rate": failures / runs if runs else 0.0,
1553
        }
1554
    return stats
1555
1556
1557
def lower_is_better_pct(base: float | None, value: float | None) -> str:
1558
    return fmt_delta_lower_better(base, value)
1559
1560
1561
def comparable_time_delta(
1562
    base: float | None,
1563
    value: float | None,
1564
    base_transport: tuple[str | None, str | None] | None,
1565
    value_transport: tuple[str | None, str | None] | None,
1566
) -> str:
1567
    if base is None or value is None:
1568
        return ""
1569
    if base_transport != value_transport:
1570
        return "n/c transport"
1571
    return lower_is_better_pct(base, value)
1572
1573
1574
def fmt(value: float | None, precision: int = 1) -> str:
1575
    return fmt_num(value, precision)
1576
1577
1578
def summary_text(rows: list[dict[str, Any]], subjects: list[Subject], workflows: list[Workflow]) -> str:
1579
    avg_ms = metric_average(rows, "elapsed_ms")
1580
    avg_tokens = metric_average(rows, "estimated_tokens_total")
1581
    avg_tools = metric_average(rows, "tool_call_count")
1582
    avg_output = metric_average(rows, "raw_output_bytes")
1583
    transports = metric_transport(rows)
1584
    total_stats = workflow_total_stats(rows)
1585
    git_name = next((subject.name for subject in subjects if subject.kind == "git"), None)
1586
    oak_baseline = next((subject.name for subject in subjects if subject.name in {"oak_installed", "oak_main"}), None)
1587
1588
    lines = [
1589
        "# Oak Workflow A/B Summary",
1590
        "",
1591
        f"Track: `{rows[0].get('benchmark_track', 'unknown') if rows else 'unknown'}`.",
1592
        "Rows compare whole deterministic agent-shaped workflows. Positive deltas mean the row subject is lower/better.",
1593
        "Time deltas are not comparable across different remote/workspace transports; token and tool-call deltas remain comparable.",
1594
        "Output bytes are full stdout/stderr bytes, even when admitted token text was capped.",
1595
        "",
1596
        "| Workflow | Subject | Runs | Successes | Failures | Failure rate | Avg ms | Avg est tokens | Avg output bytes | Avg tool calls | vs Git time | vs Git tokens | vs Git tools | vs Oak time | vs Oak tokens | vs Oak tools |",
1597
        "| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |",
1598
    ]
1599
    for workflow in workflows:
1600
        git_ms = avg_ms.get((git_name, workflow.name)) if git_name else None
1601
        git_tokens = avg_tokens.get((git_name, workflow.name)) if git_name else None
1602
        git_tools = avg_tools.get((git_name, workflow.name)) if git_name else None
1603
        git_transport = transports.get((git_name, workflow.name)) if git_name else None
1604
        oak_ms = avg_ms.get((oak_baseline, workflow.name)) if oak_baseline else None
1605
        oak_tokens = avg_tokens.get((oak_baseline, workflow.name)) if oak_baseline else None
1606
        oak_tools = avg_tools.get((oak_baseline, workflow.name)) if oak_baseline else None
1607
        oak_transport = transports.get((oak_baseline, workflow.name)) if oak_baseline else None
1608
        for subject in subjects:
1609
            key = (subject.name, workflow.name)
1610
            value_ms = avg_ms.get(key)
1611
            stats = total_stats.get(key)
1612
            if value_ms is None and stats is None:
1613
                continue
1614
            value_tokens = avg_tokens.get(key)
1615
            value_tools = avg_tools.get(key)
1616
            value_output = avg_output.get(key)
1617
            value_transport = transports.get(key)
1618
            runs = int(stats["runs"]) if stats else 0
1619
            successes = int(stats["successes"]) if stats else 0
1620
            failures = int(stats["failures"]) if stats else 0
1621
            failure_rate = float(stats["failure_rate"]) if stats else 0.0
1622
            skip_git = subject.name == git_name
1623
            skip_oak = subject.name == oak_baseline or subject.kind == "git"
1624
            lines.append(
1625
                f"| `{workflow.name}` | `{subject.name}` | {runs} | {successes} | {failures} | "
1626
                f"{failure_rate:.0%} | {fmt(value_ms)} | {fmt(value_tokens)} | "
1627
                f"{fmt(value_output, 0)} | {fmt(value_tools, 2)} | "
1628
                f"{'' if skip_git else comparable_time_delta(git_ms, value_ms, git_transport, value_transport)} | "
1629
                f"{'' if skip_git else lower_is_better_pct(git_tokens, value_tokens)} | "
1630
                f"{'' if skip_git else lower_is_better_pct(git_tools, value_tools)} | "
1631
                f"{'' if skip_oak else comparable_time_delta(oak_ms, value_ms, oak_transport, value_transport)} | "
1632
                f"{'' if skip_oak else lower_is_better_pct(oak_tokens, value_tokens)} | "
1633
                f"{'' if skip_oak else lower_is_better_pct(oak_tools, value_tools)} |"
1634
            )
1635
    return "\n".join(lines) + "\n"
1636
1637
1638
def has_lane_failures(rows: list[dict[str, Any]]) -> bool:
1639
    return any(row_returncode(row) not in (0, SKIP_RETURNCODE) for row in rows)
1640
1641
1642
def main() -> int:
1643
    args = parse_args()
1644
    subjects = load_subjects(args)
1645
    workflows = select_workflows(args.workflows)
1646
    oakbench_remotes.print_remote_preflight_warnings(
1647
        oakbench_remotes.oak_remote_preflight_warnings(
1648
            remote_preflight_requirements(subjects, workflows)
1649
        )
1650
    )
1651
1652
    timestamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
1653
    args.results.mkdir(parents=True, exist_ok=True)
1654
    args.workdir.mkdir(parents=True, exist_ok=True)
1655
    run_root = args.workdir / "runs" / timestamp
1656
    fixture_root = args.workdir
1657
1658
    metadata = {
1659
        "bench_id": timestamp,
1660
        "profile": "workflow-ab",
1661
        "benchmark_track": args.track,
1662
        "command_semantics_version": COMMAND_SEMANTICS_VERSION,
1663
        "timestamp_utc": timestamp,
1664
        "host": platform.node(),
1665
        "platform": platform.platform(),
1666
        "machine": platform.machine(),
1667
        "python": platform.python_version(),
1668
        "env_isolation_version": oakbench_environment.ENV_ISOLATION_VERSION,
1669
        "subject_versions": {subject.name: subject_version(subject) for subject in subjects},
1670
        "subject_details": subject_details(subjects),
1671
        "source": source_metadata(args.oak_repo),
1672
    }
1673
1674
    metadata["load_tier"] = args.load_tier
1675
1676
    rows: list[dict[str, Any]] = []
1677
    # Background load (untimed start/stop). Failing to start under a non-none
1678
    # tier converts the whole invocation to skip rows: load that was not
1679
    # applied is never claimed.
1680
    load_generator: oakbench_loadgen.LoadGenerator | None = None
1681
    if args.load_tier != "none":
1682
        load_generator = oakbench_loadgen.LoadGenerator(
1683
            args.load_tier, args.workdir / "loadgen-temp"
1684
        )
1685
        load_start = load_generator.start()
1686
        if not load_start["started"]:
1687
            reason = oakbench_loadgen.load_tier_unavailable_reason(str(load_start["reason"]))
1688
            rows = load_tier_skip_rows(subjects, workflows, args.runs, metadata, reason, args.load_tier)
1689
            load_generator = None
1690
    try:
1691
        if not rows:
1692
            for workflow in workflows:
1693
                fixture = prepare_fixture(fixture_root, workflow)
1694
                print(f"[fixture] {workflow.name}: {workflow.description}", flush=True)
1695
                with measurement_lock("workflow_ab") as lock_info:
1696
                    metadata["measurement_lock_wait_ms"] = lock_info.wait_ms
1697
                    metadata["measurement_lock"] = "held" if lock_info.enabled else "disabled"
1698
                    for run_index in range(args.runs):
1699
                        run_subjects = list(subjects)
1700
                        if args.randomize_subject_order:
1701
                            random.Random(f"{timestamp}:{workflow.name}:{run_index}").shuffle(run_subjects)
1702
                        metadata["randomized_subject_order"] = args.randomize_subject_order
1703
                        metadata["subject_order"] = [subject.name for subject in run_subjects]
1704
                        for subject in run_subjects:
1705
                            print(f"[run] {workflow.name} run={run_index} subject={subject.name}", flush=True)
1706
                            env_before = oakbench_envwatch.sample_environment()
1707
                            subject_rows = run_subject_workflow(
1708
                                subject,
1709
                                workflow,
1710
                                fixture,
1711
                                run_index,
1712
                                run_root,
1713
                                metadata,
1714
                                args.admitted_output_chars,
1715
                                args.track,
1716
                            )
1717
                            env_after = oakbench_envwatch.sample_environment()
1718
                            boundary = oakbench_loadgen.environment_boundary_fields(
1719
                                args.load_tier, env_before, env_after
1720
                            )
1721
                            for row in subject_rows:
1722
                                row.update(boundary)
1723
                            rows.extend(subject_rows)
1724
    finally:
1725
        if load_generator is not None:
1726
            load_stop = load_generator.stop()
1727
            # bogo-ops parsed ONLY as did-the-load-run verification, never
1728
            # reported as a benchmark number.
1729
            for row in rows:
1730
                row["load_verified"] = load_stop.get("verified")
1731
1732
    store = ResultsStore(args.results, lane="workflow")
1733
    raw_path, summary_path = store.write(timestamp, rows, summary_text(rows, subjects, workflows))
1734
1735
    if not args.keep_workdirs and run_root.exists():
1736
        shutil.rmtree(run_root)
1737
1738
    print(f"[result] {raw_path}")
1739
    print(f"[summary] {summary_path}")
1740
    return 1 if has_lane_failures(rows) else 0
1741
1742
1743
if __name__ == "__main__":
1744
    raise SystemExit(main())