Skip to content

Commit 7bf2d73

Browse files
authored
perf(benchmark): return focused experiment row receipts by default (#7711)
Signed-off-by: huangruiteng <huangrt01@163.com>
1 parent 00abb6c commit 7bf2d73

4 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎loopx/capabilities/benchmark_toolkit/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,19 @@ loopx benchmark experiment-board-upsert \
11051105
--format json
11061106
```
11071107

1108+
Upsert now returns the complete normalized affected `row` and `write` receipt
1109+
by default, for both preview and execution. Check `write.dry_run` and
1110+
`write.write_performed` before treating a preview as a saved update. The receipt
1111+
retains metrics, countability, protocol identity and total ledger `row_count`;
1112+
it does not imply a matched comparison or replace whole-study inspection.
1113+
Consumers of the previous board-shaped response can add `--include-board`.
1114+
`--benchmark-id` and `--study-id` filter that optional board projection; they do
1115+
not constrain the write or hide the affected row in the default receipt.
1116+
For a later comparison, call `experiment-board-show` with the same Goal/project
1117+
route and relevant benchmark/study filters. Reuse a current board already read
1118+
when it answers the decision; avoid rebuilding and returning it after every
1119+
single-row update. Validation and invalid-transition rejection still apply.
1120+
11081121
The default ledger is locked, atomically updated, and keyed by benchmark, study,
11091122
case, and run identity. A compact row carries arm role, exact and comparison
11101123
protocol ids, model, score metrics, countability, treatment fidelity, bounded

‎loopx/cli_commands/benchmark_experiment_board.py‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any
99

1010
from ..capabilities.benchmark_toolkit import (
11+
BENCHMARK_EXPERIMENT_BOARD_ROW_SCHEMA_VERSION,
1112
benchmark_experiment_board_row_key,
1213
build_benchmark_experiment_board,
1314
default_benchmark_experiment_board_path,
@@ -92,6 +93,14 @@ def register_benchmark_experiment_board_commands(
9293
required=True,
9394
help="Compact benchmark_experiment_board_row_v0 JSON path, or - for stdin.",
9495
)
96+
upsert_parser.add_argument(
97+
"--include-board",
98+
action="store_true",
99+
help=(
100+
"Return the full board projection (legacy response) instead of the "
101+
"default affected row and write receipt. Board filters apply to this projection."
102+
),
103+
)
95104
upsert_parser.add_argument(
96105
"--execute",
97106
action="store_true",
@@ -135,6 +144,16 @@ def _board_payload(
135144
)
136145

137146

147+
def _render_upsert_receipt(payload: dict[str, object]) -> str:
148+
return "\n".join([
149+
"# Benchmark experiment row",
150+
"",
151+
"```json",
152+
json.dumps(payload, ensure_ascii=False, indent=2),
153+
"```",
154+
])
155+
156+
138157
def _reconcile_experiment_board(
139158
args: argparse.Namespace,
140159
*,
@@ -200,7 +219,9 @@ def handle_benchmark_experiment_board_command(
200219
row = _read_json_object(args.row_json)
201220
if args.execute:
202221
receipt = upsert_benchmark_experiment_board_row(ledger_path, row)
203-
rows = read_benchmark_experiment_board_rows(ledger_path)
222+
affected_row = receipt["row"]
223+
if args.include_board:
224+
rows = read_benchmark_experiment_board_rows(ledger_path)
204225
write = {
205226
**receipt["write"],
206227
"dry_run": False,
@@ -209,13 +230,30 @@ def handle_benchmark_experiment_board_command(
209230
rows, preview_status = preview_benchmark_experiment_board_upsert(
210231
rows, row
211232
)
233+
affected_row = next(item for item in rows if _row_key(item) == _row_key(row))
212234
write = {
213235
"status": f"preview_{preview_status}",
214236
"write_performed": False,
215237
"row_count": len(rows),
216238
"path_recorded": False,
217239
"dry_run": True,
218240
}
241+
if not args.include_board:
242+
payload = {
243+
"ok": True,
244+
"schema_version": BENCHMARK_EXPERIMENT_BOARD_ROW_SCHEMA_VERSION,
245+
"goal_id": args.goal_id,
246+
"path_recorded": False,
247+
"row": affected_row,
248+
"write": write,
249+
"read_hint": (
250+
"Use this row and receipt for the update decision. For comparisons, "
251+
"use experiment-board-show with the same route and relevant "
252+
"--benchmark-id/--study-id; --include-board restores the full upsert response."
253+
),
254+
}
255+
print_payload(payload, output_format(args), _render_upsert_receipt)
256+
return 0
219257
elif args.benchmark_command == "experiment-board-reconcile":
220258
rows, write = _reconcile_experiment_board(
221259
args,

‎skills/loopx-benchmark/SKILL.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ separate: fewer bytes, calls or tokens are not proof of a better task outcome.
155155
The running row uses `status=running`, empty `metrics`, and
156156
`countability={integrity_qualified:false, official_result_present:false,
157157
score_countable:false}`. Keep the same stable `run_id` for every transition.
158+
Upsert returns the normalized affected `row` and `write` receipt by default.
159+
Check its identity and preview/write outcome directly. Read the board again
160+
when the next comparison needs it, using the same route and relevant
161+
`--benchmark-id`/`--study-id`; do not re-fetch a whole study merely to confirm
162+
one update. `--include-board` restores the previous full-board response for
163+
consumers that need it. A row receipt does not establish comparison eligibility.
158164

159165
4. **Preview and upsert terminal score, countability, effort, and insight.**
160166
First run integrity qualification. An automated restricted-access match is a

‎tests/capabilities/test_benchmark_experiment_board.py‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,11 +882,14 @@ def test_cli_previews_then_writes_and_reads_board(tmp_path: Path) -> None:
882882
)
883883
assert preview_payload["write"]["status"] == "preview_inserted"
884884
assert preview_payload["write"]["write_performed"] is False
885+
assert preview_payload["row"] == normalize_benchmark_experiment_board_row(_baseline())
886+
assert "runs" not in preview_payload
885887
assert not ledger.exists()
886888

887889
executed = run_source_cli([*command, "--execute"], check=True)
888890
executed_payload = json.loads(executed.stdout)
889891
assert executed_payload["write"]["status"] == "inserted"
892+
assert executed_payload["row"] == preview_payload["row"]
890893
assert executed_payload["path_recorded"] is False
891894
assert str(tmp_path) not in executed.stdout
892895
assert ledger.exists()
@@ -911,6 +914,60 @@ def test_cli_previews_then_writes_and_reads_board(tmp_path: Path) -> None:
911914
)
912915

913916

917+
@pytest.mark.parametrize("execute", [False, True])
918+
def test_cli_upsert_returns_exact_row_without_board_growth(tmp_path: Path, execute: bool) -> None:
919+
ledger = default_benchmark_experiment_board_path(project=tmp_path, goal_id="fixture-goal")
920+
ledger.parent.mkdir(parents=True, exist_ok=True)
921+
# Run ids may repeat across cases. Preserve an uncountable zero exactly.
922+
candidate = _baseline()
923+
candidate["countability"] = {
924+
"integrity_qualified": False, "official_result_present": True, "score_countable": False,
925+
}
926+
rows = [_baseline(case_id=f"case-{index}") for index in range(1000)]
927+
ledger.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8")
928+
original = ledger.read_bytes()
929+
row_path = tmp_path / "candidate.json"
930+
row_path.write_text(json.dumps(candidate), encoding="utf-8")
931+
command = ["benchmark", "experiment-board-upsert", "--goal-id", "fixture-goal",
932+
"--project", str(tmp_path), "--row-json", str(row_path), "--format", "json"]
933+
if execute:
934+
command.append("--execute")
935+
result = run_source_cli(command)
936+
payload = json.loads(result.stdout)
937+
assert payload["row"] == normalize_benchmark_experiment_board_row(candidate)
938+
assert payload["write"]["row_count"] == 1000
939+
assert payload["write"]["write_performed"] is execute
940+
assert "experiment-board-show" in payload["read_hint"]
941+
assert "runs" not in payload and "summary" not in payload
942+
assert len(result.stdout.encode()) < 6000 # One complete row, not 1000 rows.
943+
if execute:
944+
observed = read_benchmark_experiment_board_rows(ledger)
945+
assert len(observed) == 1000
946+
assert all(row["countability"]["score_countable"] is (row["case_id"] != "case-12")
947+
for row in observed)
948+
else:
949+
assert ledger.read_bytes() == original
950+
full = json.loads(run_source_cli([*command, "--include-board"]).stdout)
951+
assert full["summary"]["run_count"] == 1000
952+
assert len(full["runs"]) == 1000
953+
assert full["write"]["write_performed"] is execute
954+
955+
956+
def test_cli_compact_upsert_rejects_invalid_transition_without_writing(tmp_path: Path) -> None:
957+
ledger = default_benchmark_experiment_board_path(project=tmp_path, goal_id="fixture-goal")
958+
upsert_benchmark_experiment_board_row(ledger, _baseline())
959+
before = ledger.read_bytes()
960+
row_path = tmp_path / "row.json"
961+
row_path.write_text(json.dumps(_running_baseline()), encoding="utf-8")
962+
result = run_source_cli([
963+
"benchmark", "experiment-board-upsert", "--goal-id", "fixture-goal",
964+
"--project", str(tmp_path), "--row-json", str(row_path), "--execute", "--format", "json",
965+
], check=False)
966+
assert result.returncode != 0
967+
assert "invalid benchmark experiment board input" in result.stderr
968+
assert ledger.read_bytes() == before
969+
970+
914971
def test_cli_show_projects_factorial_contrast_from_explicit_contract(
915972
tmp_path: Path,
916973
) -> None:
@@ -1031,7 +1088,7 @@ def test_cli_omitted_project_routes_board_to_registered_goal_repo(
10311088
goal_id="fixture-goal",
10321089
)
10331090

1034-
assert payload["summary"]["run_count"] == 1
1091+
assert payload["write"]["row_count"] == 1
10351092
assert ledger.is_file()
10361093
assert not (unrelated / ".loopx").exists()
10371094

0 commit comments

Comments
 (0)