fix(lease): return the row grant and finalize wrote, not a post-commit lookup

Merged
RM
16d ago
+160-16

User description

Why

Follow-up to #202 / #203. Review threads on #202 (Devin, CodeAnt) pointed at a residual that lives on main: LeaseStore::grant committed its transaction and then looked the job up again on a fresh lock, and finalize_by_path (release and tombstone) did the same by worktree path. Between commit and that second lookup another connection could release or re-register the job or path, and the path lookup's released_at DESC, id DESC order could return a different released row at the same path. The caller was then told the wrong lease was granted or released.

Change

  • grant reads the granted row by job key inside its write transaction and returns that row.
  • finalize_by_path re-reads the row it updated by row id inside the transaction and returns it. The early return for an already-finalized row returns the lease found in the transaction instead of re-querying by path.
  • No schema, CLI, or behavior change beyond which row is returned. Live-path uniqueness and the BEGIN IMMEDIATE transactions are unchanged.

Tests

  • release_by_path_returns_the_row_it_finalized_not_the_newest_released_row: with another released row at the same path sorting first, release must report the row it finalized. Fails on main (returns job-b, expected job-a), passes here. Also covers tombstone through the same path.
  • grant_returns_the_row_written_in_its_own_transaction: the returned lease is the committed row for the job (row id, operation id, path).

Local: cargo fmt --check, cargo clippy -p writ-core --all-targets -D warnings, cargo test -p writ-core (544 passed).

Out of scope

Coord messages/handoff, schema changes, anything else from RM-825.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Jo7YT1F7RzUveLPhg6hP9S


Generated by Claude Code


Summary by cubic

LeaseStore::grant and finalize_by_path (release and tombstone) now return the exact row they wrote, instead of re-querying after commit and risking returning a row from another connection.

Bug Fixes

  • grant reads the granted row by job key inside its write transaction and returns it.
  • finalize_by_path re-reads the finalized row by row id inside the transaction; the early return for already-finalized rows returns the in-transaction lease rather than re-querying by path.
  • Fixes a race where, between commit and lookup, another connection could release or re-register the job/path, and the path lookup's released_at DESC, id DESC order returned a different released row.
  • Adds regression tests in row_identity.rs covering release reporting the row it finalized when another released row at the same path sorts first, and grant returning the committed row; both error paths share a missing_row_error helper.

CodeAnt-AI Description

Return the lease actually granted or finalized

What Changed

  • Grant operations return the lease they created or updated.
  • Release and tombstone operations return the specific lease they finalized, even when another lease shares the worktree path.
  • Already-finalized leases return the matching lease instead of a potentially different lease at the same path.

Impact

✅ Correct lease reported after grants
✅ Correct job reported after releases
✅ Reliable results when worktree paths are reused

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Summary by CodeRabbit

  • Bug Fixes
    • Lease grant, release, and tombstone operations now return the lease record associated with the result, including when a lease is already finalized or tombstoned. This keeps the reported lease details aligned with the operation that was performed, rather than returning a different record when multiple leases share a path.