← Back to Pull requests

fix(stash): restore hk's own stash entry by commit id so concurrent stashes are never touched #1637

Closed
unknown closed this pull request without merging on Oct 03, 2026 23:49 UTC
View on GitHub
unknown unknown commented on Oct 03, 2026 23:21 UTC
Author

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

Problem

The git stash stack is shared by every worktree, editor and process using a repository. hk's pre-commit stash found its own entry by reading stash@{0} right after pushing, and fell back to stash@{0} when it could not find it on restore. If anything else pushed a stash in that window, hk could record, apply and drop someone else's entry, or fail to restore its own while leaving the user's unstaged edits stashed.

Example: while git commit runs hk's pre-commit hook, another worktree runs git stash push -m foreign-entry.

Behavior after this change

hk never pops or drops a stash entry it cannot prove is its own, and never silently leaves your unstaged edits stashed.

  • Ownership is proven, not inferred. With shell git, hk's git stash push carries a message unique to the run (hk: <pid>-<nonce>), and hk finds its entry by scanning the stash list for exactly that message, never by comparing the list before and after. With libgit2 (HK_LIBGIT2=1, whole-worktree stash), hk uses the id stash_save returns. From then on the entry is identified only by commit id, never by stash@{n} or a shared message.

  • Nothing to stash is a true no-op. If git stash push reports "No local changes to save", hk records nothing and a foreign push that lands meanwhile is never mistaken for hk's.

  • Unclear ownership is an error, not a no-op. If git stashed your changes but hk cannot find the entry with its message, finds several, or cannot read the stash list, hk stops with an error that says your unstaged changes may be in git stash list and to look for the hk: <pid>-<nonce> message and git stash apply it.

  • Restore reads the recorded commit directly. If the entry is no longer in the stash list, hk restores nothing, leaves every entry alone and prints git stash apply <sha> to recover.

  • Drops are verified. git stash drop takes a moving stash@{n} and git has no compare-and-delete, so hk looks the position up immediately before dropping, reads the commit git reports as dropped (Dropped stash@{n} (<sha>)), and if it is not hk's (another process pushed in between) puts that entry back with git stash store -m <its message> <sha> and retries, up to three times. It never leaves a drop unverified. If it cannot put a foreign entry back it says so and prints the git stash store <sha> command.

  • Reverted-path and intent-to-add stash entries drop through the same verified path.
  • Residual race, stated plainly: the verify-and-restore step closes the race after the fact, but between a foreign push displacing hk's position and hk's git stash store, the foreign entry is briefly missing, and once put back it is at the top instead of its old position. This needs another process to act within milliseconds of hk. The cross-process lock in #1636 serializes hk against hk but not foreign git commands.

    Validation

  • cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean; cargo test git:: passes (19 tests), including the unique-message lookup (match, none, several), message uniqueness and parsing git's Dropped line.

  • test/stash_by_commit_id.bats (6 of 6 ok): a foreign worktree pushes a stash between hk's stash and restore, with shell git, with libgit2 via a named-path stash (which uses shell git), and with libgit2 whole-worktree (an untracked file forces the no-path stash; the test checks the debug log that stash_save was used); a nothing-to-stash run with a foreign push in between touches no entry; and a vanished entry is reported while other entries survive.

  • bats test/stash.bats: 102 ok of a 104 plan. stash default for pre-commit and stash default for fix fail in a fresh worktree because the generated, gitignored pkl/Builtins.pkl is missing, which is independent of this change.

  • Not covered: the drop-verification race itself cannot be forced deterministically; it is covered by the parser unit test only. Not run: the full mise run test and Windows.

  • Recovery hints printed on failure still show stash@{n} names, as before, because existing tests match them.
  • 🤖 Generated with Claude Code

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

    Diff: 545 lines (+408 / -137) across 6 files Reviewer: jdx Review duration: 28m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.48) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 545 lines diff across 6 files
    0.48
    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.377
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1637"
    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: 48% (MEDIUM) · Higher = larger blast radius
    48%
    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: 20.8% · Safe to merge
    20.8%
    Repository