Skip to content

docs(claude): record the immutable-id-over-list-position rule - #3702

Merged
max-sixty merged 1 commit into
mainfrom
autostash-sha
Aug 2, 2026
Merged

max-sixty merged 1 commit into
mainfrom
autostash-sha

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Follows #3693. That PR fixed the autostash restore; this records the rule it was an instance of, so the next one doesn't have to be found the hard way.

Why

The bug was holding stash@{0} — a position in a list any process can reorder — across a git push, then acting on it. Nothing in the guidance said not to. CLAUDE.md already has a rule for picking a robust handle over a fragile one (Structured Output Over Error-Message Parsing), so this sits next to it as a sibling.

Scope of the rule

I swept the codebase for other instances before writing it. There are none, and the three places that look like the bug shape are why the rule is worded narrowly:

  • create_safety_backup (working_tree.rs) already does it right — git stash create returns a SHA that never enters the stash list, stored under a named refs/wt-backup/<branch> ref.
  • orphan_branch_age (step/prune.rs) reads reflog show for a named ref and takes the oldest line as a creation timestamp. Single-shot, no mutation window; a pruned reflog makes a branch read younger, which skips it from pruning and so fails safe.
  • relocate.rs holds idx into pending across cycle-breaking moves. Not the bug shape: pending is built once, only ever read, and owned by wt.

So the rule is about namespaces other processes can mutate, not about indices generally. Stated loosely it would invite "fixing" relocate.rs and making it worse — hence the closing sentence drawing that line explicitly.

The one surviving positional selector, inside StashData::locate_by_message, is cited in the rule as the correct pattern: re-derive the selector from something stable immediately before the operation that needs it.

Testing

Docs-only. cargo run -- hook pre-merge --yes green.

This was written by Claude Code on behalf of max-sixty

The autostash restore bug came from holding `stash@{0}` across a mutation
window, and nothing in the guidance said not to. Write the rule down next to
the structured-output rule it belongs with — both are about picking a robust
handle over a fragile one.

Scoped deliberately: the rule is about namespaces other processes can mutate,
not about indices generally. Stated loosely it would invite "fixing"
`relocate.rs`, whose indices point into a Vec wt builds once and only reads.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 4781590 into main Aug 2, 2026
38 checks passed
@max-sixty
max-sixty deleted the autostash-sha branch August 2, 2026 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants