Skip to content

Bound task-state lock acquisition - #1652

Merged
mergify[bot] merged 3 commits into
mainfrom
lean178/1651-bound-task-state-lock
Nov 26, 2026
Merged

mergify[bot] merged 3 commits into
mainfrom
lean178/1651-bound-task-state-lock

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Bound task-state lock acquisition

Context and goal

  • Context: Task commands share an exclusive checkout lock around receipt and PR-body updates.
  • Problem: If a writer stops while holding the lock, later commands can wait indefinitely.
  • Goal: Use the existing 60-second operation budget so a contender reports the occupied checkout and can be retried. The holder retains exclusive access.

Closes #1651

Acceptance

  • Time out acquiring the same directory lock after 60 seconds and tell the operator how to retry.
  • An expired contender writes no state. Check PR refresh remains best-effort.
  • Exercise contention, expiration and reacquisition with a real directory lock and a fake clock.

Base

main @ f9514c4

Validation

Recorded for 9d17de4361b7

  • uv run --group dev ruff check src tests: exit 0
  • uv run --group dev tach check: exit 0
  • uv run --python 3.14 python scripts/check_architecture_docs.py: exit 0
  • scripts/size_budget.sh: exit 0
  • uv run --group dev pytest -q: exit 0

Review

  • primary, claude, 8771095, approve

Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Architecture-Review: unchanged; task writers keep their existing lock and fail closed if it stays occupied.
Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

loopzero primary review by claude on 8771095a742f0dc767b26fc275244cebb214f21e, 36.5s: approve (0 critical, 0 important, 2 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

Comment thread tests/test_cli.py Outdated
assert _closed_unmerged_wait(monkeypatch, reads, steps=1) == final


def test_task_state_lock_times_out_with_a_live_holder(tmp_path, monkeypatch):

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: Acceptance items 'expired contender writes no state' and 'PR refresh stays best-effort' are not exercised by a test

The new test drives _lock_task_state directly. It covers contention, expiry at exactly GIT_TIMEOUT and reacquisition with a real flock and a fake clock, but it never goes through a writer. Both behaviours do hold today. TimeoutError is raised before yield, so _save_report (cli.py:403) never reaches _write_json_atomically. TimeoutError is also a subclass of OSError, so _refresh_pr_checks (cli.py:574) and the review-summary append (cli.py:892) catch it and print a note. Nothing in the tests pins either fact, so a later change to the exception type, such as wrapping it in CliError, could make PR refresh fatal or let a write through without any test failing. Smallest fix: while the outer lock is held, call _save_report(tmp_path, report) and assert it raises and that CHECKS_FILE does not exist. Then call _refresh_pr_checks(tmp_path, config) and assert it returns normally and prints the 'could not refresh PR checks' note.

Comment thread src/loopzero/cli.py Outdated
except BlockingIOError:
_sleep(min(0.1, remaining))
else:
raise TimeoutError(f"task state is busy in {wt}; retry when the other command finishes")

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: A timeout in cmd_pr after the push leaves the PR body out of date, and the message does not name the command to rerun

At cli.py:704 the lock is taken only after _git_push has succeeded. If the lock times out there, the top-level HANDLED handler (OSError) reports 'task state is busy in ; retry when the other command finishes'. The branch has been pushed, but the PR body still shows the old Validation/Review. Rerunning loopzero pr fixes it because the push is idempotent, but the operator is not told the push already happened or which command to rerun. Optional fix: keep the lock helper generic, catch TimeoutError at the cmd_pr call site, and raise CliError(f"pushed {branch}; PR body not updated: {exc}; rerun loopzero pr").

eowca and others added 2 commits November 26, 2026 13:03
Architecture-Review: unchanged; the same local state guard still owns receipt and PR-body serialization.
Co-Authored-By: Codex <noreply@openai.com>
Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

loopzero delta review by claude on 9d17de4361b71af47572e553e045d73a7658f2e0, 28.4s: approve (0 critical, 0 important, 2 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

Comment thread src/loopzero/worktree.py
try:
if lock:
fcntl.flock(fd, fcntl.LOCK_EX)
deadline = time.monotonic() + 60

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: Lock budget is a bare 60 instead of a named operation budget

The task says the lock should reuse the existing 60-second operation budget. Before the merge, the code tied it to cli.GIT_TIMEOUT. Now _local_state_directory hardcodes 60, and the test asserts the literal now == 60. If cli.GIT_TIMEOUT (or sandbox.GIT_TIMEOUT) changes, the lock budget will silently stop matching it. worktree cannot import cli, so the smallest fix is a module constant such as LOCK_TIMEOUT = 60.0 in worktree.py, used in both the loop and the test (now == cli.worktree.LOCK_TIMEOUT).

Comment thread src/loopzero/worktree.py
if lock:
fcntl.flock(fd, fcntl.LOCK_EX)
deadline = time.monotonic() + 60
while (remaining := deadline - time.monotonic()) > 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: Lock retry loop duplicates the poll-until-deadline pattern

This adds another hand-written loop that waits with time.sleep(min(step, remaining)) until a deadline, alongside the ones in cli.py (~1417) and candidate.py (~499). It behaves correctly. On timeout it raises TimeoutError, which is an OSError, so the best-effort paths at cli.py:584 (PR checks refresh) and cli.py:872 (review summary) still just print a note. Publication at cli.py:709 propagates the error to the operator. A guarded write_local_state (where expected_head is set) also writes nothing on timeout. This is non-blocking. Consider moving the loop into a small _flock_with_deadline(fd, budget) helper, so _local_state_directory stays focused on directory validation.

@eowca
eowca marked this pull request as ready for review November 26, 2026 12:19
@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Merged into main as 94e0a72 at 12:26 UTC

5 min in the queue, 3 min of it CI.

So far

  • ✅ Entered the queue Thu 26 Nov, 12:21 · rule main · triggered by @eowca with the @mergifyio queue command
  • ✅ Passed · draft #1878
  • ✅ Merged 12:26 · 94e0a72 · squash
3 merge conditions met
  • github-review-approved · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = checks
    • check-neutral = checks
    • check-skipped = checks
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = Loop-zero Eligibility
    • check-neutral = Loop-zero Eligibility
    • check-skipped = Loop-zero Eligibility

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot added the queued label Nov 26, 2026
@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

No description provided.

@mergify
mergify Bot merged commit 94e0a72 into main Nov 26, 2026
18 checks passed
@mergify mergify Bot removed the queued label Nov 26, 2026
@eowca
eowca deleted the lean178/1651-bound-task-state-lock branch November 26, 2026 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bound waits for the local task-state lock

1 participant