fix(stash): restore hk's own stash entry by commit id so concurrent stashes are never touched #1637
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
jdx
<!-- 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 tostash@{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 commitruns hk's pre-commit hook, another worktree runsgit 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.
git stash pushcarries 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 idstash_savereturns. From then on the entry is identified only by commit id, never bystash@{n}or a shared message.git stash pushreports "No local changes to save", hk records nothing and a foreign push that lands meanwhile is never mistaken for hk's.git stash listand to look for thehk: <pid>-<nonce>message andgit stash applyit.git stash apply <sha>to recover.git stash droptakes a movingstash@{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 withgit 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 thegit stash store <sha>command.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 --checkandcargo clippy --all-targets -- -D warningsare clean;cargo test git::passes (19 tests), including the unique-message lookup (match, none, several), message uniqueness and parsing git'sDroppedline.test/stash_by_commit_id.bats(6 of 6ok): 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 thatstash_savewas 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: 102okof a 104 plan.stash default for pre-commitandstash default for fixfail in a fresh worktree because the generated, gitignoredpkl/Builtins.pklis missing, which is independent of this change.mise run testand Windows.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.*