fix(stash): serialize stash and restore across hk processes and linked worktrees #1636
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
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.382
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1636"
general
weight: 0.35
jdx
<!-- 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-commithooks 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.
After: the second process waits until the first has restored its changes.
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 usesstd::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_TIMEOUTseconds (alsogit config hk.stashLockTimeout, default 300) hk fails with an error naming the lock file.0fails immediately. Hooks with nothing to stash never take the lock.Docs:
docs/hooks.mdanddocs/environment_variables.md, with pirate variants restamped.Validation:
cargo fmt,cargo clippy --all-targets -- -D warningsclean;mise run renderproduced no further changes.cargo check --target x86_64-pc-windows-gnulocally (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 -->
---
<!-- /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
0to 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 -->