Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions src/loopzero/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO
missing, preparations = (None, ()) if _resolved else _preflight(config, worktree)
results = [missing] if missing else []
commands = () if missing else (*preparations, *config.checks)
for index, command in enumerate(commands):
for ordinal, command in enumerate(commands, 1):
if progress:
progress(command)
key = _input_key(config, worktree, command, "" if dirty else base,
Expand All @@ -138,19 +138,22 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO
if status:
status(f"reused {(old.reused_from or head)[:8]}")
else:
with _stage_status(status, index + 1, len(commands)):
with _stage_status(status, ordinal, len(commands)):
result = (_dispatch_one(config, worktree, head, base, command, timeout)
if config.check_runner else _run_one(config, worktree, prefix, command, timeout))
if key:
result = replace(result, input_key=key)
if index >= len(preparations) or result.exit_code:
result = replace(result, input_key=key)
if ordinal > len(preparations) or result.exit_code:
results.append(result)
_checkpoint_success(
checkpoint, CheckReport(head, dirty, tuple(results), settings_sha256),
previous, commands[index + 1:],
)
if checkpoint is not None and result.input_key and result.exit_code == 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Moving the checkpoint helper into run_checks makes the loop nest deeper

This change is meant to reduce complexity in run_checks. Moving the checkpoint logic into the loop keeps Ruff's branch count at 16, but the code now nests four levels deep in the main loop (with → for → if → if). It also builds the checkpoint report inside the loop. I checked the behaviour and it matches the old helper. input_key defaults to None, and _input_key returns None or a hash, so the unconditional replace(result, input_key=key) gives the same values as the old if key: path. commands[ordinal:] is the same slice as the old commands[index + 1:]. Removing the not local.results guard is safe: with _resolved=True and checks=(command,), the loop always appends exactly one result or raises. A smaller option is to keep a helper that takes (results, commands[ordinal:]) and is called only when checkpoint is not None and result.input_key and result.exit_code == 0. That still avoids building an unused report and keeps the nested block out of the hotspot function. This is optional.

# The receipt stays nonpassing until final validation.
incomplete = CheckResult(INCOMPLETE_CHECK_COMMAND, 1, 0.0, "")
unvisited = (
previous[stage] for stage in commands[ordinal:] if stage in previous
)
checkpoint(CheckReport(head, dirty, (*results, *unvisited, incomplete),
settings_sha256))
if status:
status(f"stage {index + 1}/{len(commands)}: exit {result.exit_code} "
status(f"stage {ordinal}/{len(commands)}: exit {result.exit_code} "
f"({result.duration_s:.1f}s)")
if result.exit_code != 0:
break
Expand All @@ -173,17 +176,6 @@ def run_checks(config: Config, worktree: Path, *, timeout: float = DEFAULT_TIMEO
INCOMPLETE_CHECK_COMMAND = "<checks incomplete>"


def _checkpoint_success(checkpoint: Callable[[CheckReport], None] | None,
report: CheckReport, previous: dict[str, CheckResult],
remaining: tuple[str, ...]) -> None:
stage = report.results[-1]
if checkpoint is not None and stage.input_key and stage.exit_code == 0:
# Even the last stage leaves a nonpassing receipt until final validation.
incomplete = CheckResult(INCOMPLETE_CHECK_COMMAND, 1, 0.0, "")
unvisited = tuple(previous[command] for command in remaining if command in previous)
checkpoint(replace(report, results=(*report.results, *unvisited, incomplete)))


def _previous(config: Config, worktree: Path, settings_sha256: str) -> dict[str, CheckResult]:
if _git(config, worktree, "ls-files", "--", ".loopzero"):
return {}
Expand Down Expand Up @@ -640,7 +632,7 @@ def _dispatch_one(
raise ValueError("Worktree changed before fallback")
local = run_checks(replace(config, check_runner=(), checks=(command,), inputs={}),
worktree, timeout=timeout, _resolved=True)
if local.dirty or not local.results:
if local.dirty:
raise ValueError("Local fallback failed")
return local.results[-1]
return CheckResult(command, frame["exit_code"], frame["duration_s"],
Expand Down
Loading