Skip to content

Confine local task state to the worktree - #1408

Closed
eowca wants to merge 5 commits into
mainfrom
lean178/1027-state-lock-integration
Closed

eowca wants to merge 5 commits into
mainfrom
lean178/1027-state-lock-integration

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confine local task state to the worktree

Context and goal

  • Goal: Local check and review files stay inside their worktree.
  • Problem: Symbolic links can redirect local state reads and writes.
  • Context: The ignored .loopzero directory holds the handoff and receipts used by checks, readiness, and review.

Closes #1027

Acceptance

  • Reject tracked local state and symbolic links at the directory and artifact levels.
  • Check, review, repost, and readiness reject unsafe state before reading or changing its target.
  • A normal directory remains usable. External sentinel contents and permissions stay unchanged.

Notes

Reject tracked state before using it as evidence. Check the state directory and direct child at the point of use. Use a directory descriptor for reads, writes and chmod so path replacement cannot redirect them.

This replaces #1356 and #1306. It carries their local-state change and routes the PR-body updates added by #1230 through the same guarded directory lock as receipt publication.

Validation

Recorded for 171175017ce8 (not current head) (unverified, run loopzero check)

  • 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

The previous full-suite rerun passed, including the earlier intermittent readiness case. The conflict resolution passed the size budget and 16 focused cases.

Base

Base: c5232bb

Review

  • primary, claude, 1711750, approve

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

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 171175017ce86c18f5ba434d38c234ed4aeb2b8e, 102.7s: approve (0 critical, 0 important, 3 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


def read_local_state(wt: Path, name: str) -> str:
with _local_state_directory(wt) as fd, os.fdopen(

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: Symlinked state files surface as a bare ELOOP error with only the relative name

read_local_state opens the file with O_NOFOLLOW but does not translate the resulting OSError(ELOOP). Paths that skip the local_state_files pre-check therefore show [Errno 40] Too many levels of symbolic links: 'checks.json' with no worktree path. That covers ready/status via _load_report, and pr/review via task_text. The directory-level and _regular_state paths raise a WorktreeError that names the full .loopzero/... path. In _load_review, the same ELOOP is reported as "not runner-produced: invalid saved review", which hides the real cause. The command still exits 1, so this is not a safety gap. Smallest fix: in read_local_state, catch OSError with errno.ELOOP and re-raise WorktreeError(f"local state file is a symbolic link: {wt / '.loopzero' / name}"). That gives every caller one consistent diagnostic.

Comment thread src/loopzero/cli.py
with _lock_task_state(wt):
with worktree.lock_local_state(wt):
_refresh_existing_pr_checks(wt, config)
except OSError as exc:

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: Best-effort lock/refresh paths no longer absorb directory-level rejections

Before this change, _lock_task_state failures were plain OSErrors, which _refresh_pr_checks (line 504) and the PR review-summary append in cmd_review (line 820) catch and print as notes. _local_state_directory now raises WorktreeError for tracked or symlinked state, and WorktreeError is not an OSError. _refresh_existing_pr_checks (line 519) can also receive WorktreeError from _load_report/task_text. The up-front checks make this reachable only if the state is swapped during the command. Even so, in cmd_review the review has already been posted at that point, so the command ends with an error instead of the review's verdict exit code. Smallest fix: add worktree.WorktreeError to the except tuples at lines 504, 519 and 820 so these steps stay best-effort.

Comment thread src/loopzero/worktree.py
@contextmanager
def _local_state_directory(wt: Path, *, create: bool = False, lock: bool = False):
path = wt / ".loopzero"
if _git(wt, "ls-files", "-z", "--", ".loopzero"):

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: Every local-state access runs another git ls-files subprocess

_local_state_directory runs git ls-files on every read, write, check and lock. The diff's own test changes show the cost: review goes from 14 to 18 git calls, and ready and status each gain one. cmd_check also pays this on every partial-report checkpoint during run_checks. This is correct, but the git cost keeps growing with each new state access. Possible fix: check tracked status once per command, or pass the open directory descriptor through within a command. Then repeated accesses only re-check the directory's file type, which is cheap.

@eowca
eowca marked this pull request as ready for review November 26, 2026 02:49
@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

❌ Left the queue · checks failed

8 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Thu 26 Nov, 02:49 · rule main · triggered by @eowca with the @mergifyio queue command
  • ❌ Checks failed · draft #1435
  • 🚫 Left the queue 02:57 · 1711750

Not proven to be this pull request's failure. It failed on top of the pull requests ahead of it, and was not tested alone.

Reason

The merge conditions cannot be satisfied due to failing checks

  • checks

Failing checks:

To get back in the queue

  • If the failure comes from the pull requests it was tested with, or is a flake, comment @mergifyio queue to queue it again as it is.
  • If it is this pull request's, push a fix: the pull request is queued again once its queue conditions match.

Merge conditions not met when it left the queue

  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-neutral = checks
    • check-skipped = checks
    • check-success = checks
2 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 = 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.

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Parked at approved head 17117501. The full local gate and required hosted checks passed. loopzero merge --wait exited 1 when Mergify removed the PR after candidate #1435 counted 13,887 test lines against the 13,850 cap. A clean integration with current main also projects 13,887. The head and approval are preserved. Resume when the candidate fits the existing cap.

eowca and others added 3 commits November 26, 2026 10:56
…ktree

Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; the existing worktree-local state and review authority boundaries apply to the resolved receipt paths.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; reviewer diagnostics follow the existing worktree-local receipt boundary.
@mergify mergify Bot removed the dequeued label Nov 26, 2026
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 325d9cc5d5c908a6d71f9240b1b4ed6aa4bbc00d, 29.1s: approve (0 critical, 0 important, 1 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
monkeypatch.setattr(cli.runners, "review_with", malformed)
if not save:
def fail(*args):
def fail(*args, **kwargs):

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: Diagnostic-failure test never reaches the new WorktreeError branch

The test replaces the whole worktree.write_local_state with a stub that raises PermissionError. That only covers the OSError half of the new except (OSError, worktree.WorktreeError) in _save_review_failure_output (cli.py:1137). The case this branch was added for never runs: an unsafe .loopzero directory, a symlinked or non-regular reviewer-output-*.json, or tracked local state. Each of these raises WorktreeError. If that exception were dropped from the tuple, a malformed-review run with a redirected state directory would crash. It would no longer report "Could not save reviewer output", and every test would still pass. Smallest fix: add a parametrized case that creates wt/.loopzero/reviewer-output-<head12>-primary.json as a symlink to an external file, without stubbing the writer. Then assert that the error output contains "Could not save reviewer output" and "symbolic link", and that the external file is unchanged.

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1793. The delta approved the resolution at 325d9cc. Hosted CI then found that the readiness test's Git-call count needed to include the new index read. The replacement carries the reviewed code and that test correction.

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.

security: Keep local receipts inside the managed worktree

1 participant