← Back to Pull requests

fix(stash): serialize stash and restore across hk processes and linked worktrees #1636

Closed
unknown closed this pull request without merging on Oct 04, 2026 01:15 UTC
View on GitHub
unknown unknown commented on Oct 03, 2026 23:20 UTC
Author

<!-- entire-trail-link-start -->
https://entire.io/gh/jdx/hk/trails/212
<!-- entire-trail-link-end -->

Two hk processes in one repository, such as pre-commit hooks running in two linked worktrees, share one stash stack. hk's locks were in-memory per process, so their stash-and-restore phases could interleave: one process could stash while the other had its changes set aside, and then restore or drop the wrong entry.

Before: committing in two worktrees at once could mix up stashes.

# worktree A and B, both with unstaged edits, both run hooks with stash = "git"
begin wt-a
begin wt-b     # B stashed while A's changes were still set aside
end wt-a
end wt-b

After: the second process waits until the first has restored its changes.

hk: waiting for another hk process to finish stashing (lock: /repo/.git/hk-stash.lock)

begin wt-a
end wt-a
begin wt-b
end wt-b

hk now holds an OS advisory file lock, hk-stash.lock, in the repository's common git directory (shared by all linked worktrees) from before the stash is created until it is restored. The operating system releases it if hk crashes or is killed. It uses std::fs::File::try_lock (stable since Rust 1.89, MSRV is 1.94), so there is no new dependency and it works on Windows.

Waiting is bounded: after HK_STASH_LOCK_TIMEOUT seconds (also git config hk.stashLockTimeout, default 300) hk fails with an error naming the lock file. 0 fails immediately. Hooks with nothing to stash never take the lock.

Docs: docs/hooks.md and docs/environment_variables.md, with pirate variants restamped.

Validation:

  • Unit tests for contention/timeout, zero timeout, release on drop, and a waiter acquiring after the holder drops: pass on Linux and on jdx-win-ci (Windows).

  • New bats test runs two hooks concurrently in two linked worktrees and asserts the begin/end records never interleave, plus a timeout test with an external process holding the lock. 2/2 ok with libgit2 on and off; it fails without the lock. Neighboring stash bats tests pass.

  • cargo fmt, cargo clippy --all-targets -- -D warnings clean; mise run render produced no further changes.

  • Not done: cargo check --target x86_64-pc-windows-gnu locally (C dependencies need a mingw toolchain), so Windows was verified by building and testing on jdx-win-ci instead. The bats test was not run on Windows. A nested hk invoked from inside a stashing hook would wait on the lock until the timeout.
  • 🤖 Generated with Claude Code

    AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: unavailable.

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Changes the stashing path used by hooks when unstaged work exists; mistakes could affect working-tree state, but behavior is bounded by timeouts and covered by new unit and bats tests.

    > Overview

    Concurrent hk runs in one repo (e.g. pre-commit in two linked worktrees) used to share one Git stash stack without coordination, so stash/restore could interleave and restore the wrong changes. This PR serializes that window with an OS advisory lock on hk-stash.lock in the repo’s common git directory, held from just before stash until after restore.

    > Waiters log that they’re blocked; after HK_STASH_LOCK_TIMEOUT (default 300s, also git config hk.stashLockTimeout, 0 = fail immediately) hk errors and names the lock path. Ctrl-C while waiting aborts without stashing. Lock file creation respects core.sharedRepository; read-only existing lock files can still be locked.

    > Docs (including pirate variants) and stash_lock_timeout in settings.toml document the behavior; Git config loading now treats 0 as valid only for this timeout.

    > <sup>Reviewed by Cursor Bugbot for commit aa7c1122488e966f71a8601dad7a210a96d63827. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    New Features
    Concurrent hook runs in linked worktrees now take turns stashing and restoring unstaged changes, helping prevent changes from interfering with one another.
    Configure how long hk waits for another process with a timeout that defaults to 300 seconds. Set it to 0 to fail immediately; if the wait times out, hk reports the lock file.
    Documentation
    * Added guidance on stash locking, timeout configuration, and lock-file behavior.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 588 lines (+441 / -147) across 7 files Reviewer: jdx Review duration: 114m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 588 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1636"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.0% (MEDIUM) · Higher = larger blast radius
    50.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.6% · Safe to merge
    21.6%
    Repository