gstack icon indicating copy to clipboard operation
gstack copied to clipboard

/context-restore can load the wrong worktree checkpoint (shared origin-derived slug + newest-across-all-branches restore)

Open robertvitali opened this issue 5 months ago • 3 comments

Summary

With multiple git worktrees of the same repo, /context-restore in one worktree can load a checkpoint saved by a different worktree. Two design choices combine.

Root cause

  1. bin/gstack-slug derives the slug from git remote get-url origin. All worktrees of a repo share one origin → same slug → same ~/.gstack/projects/<slug>/checkpoints/ dir. (The per-cwd slug cache just stores that same shared value under each worktree's path key.)
  2. /context-restore by default "loads the most recent saved context across ALL branches" and explicitly "does NOT filter the candidate set by current branch" (per context-restore SKILL.md — intentional, for Conductor handoff).

Together: each worktree's /context-save writes into the same shared dir, and /context-restore picks the newest file across all branches — which may be another worktree's.

Repro

  1. Repo with origin set; git worktree add ../repo-b feature-b.
  2. Worktree B: /context-save (checkpoint B).
  3. Worktree A (older/no checkpoint for its branch): /context-restore.
  4. A restores B's checkpoint.

Impact

Cross-worktree context contamination — the agent resumes from the wrong worktree's state. Most visible in concurrent multi-session/multi-worktree usage.

Proposed fix (options)

  • Default /context-restore to branch-scoped selection (filter candidates by frontmatter branch: == current branch), with an explicit --all-branches opt-in to keep the cross-branch handoff use case. Checkpoints already record branch: in frontmatter.
  • And/or make the slug worktree-aware so each worktree gets its own checkpoints dir.
  • A --branch <name> / explicit-path selector for deterministic pinning.

Related

#1125 (slug derivation), #1191 (concurrent same-repo state), #1161 (repo-local artifacts)

Environment: gstack 1.48.0.0

robertvitali avatar Jun 19 '26 05:06 robertvitali

Opened #2054 for this. It orders restore candidates current-branch-first (read from each checkpoint's branch: frontmatter) so a sibling worktree's newer save can't shadow the current branch's own checkpoint, while keeping the full cross-branch set as a fallback — so Conductor handoff (restore when the current branch has no checkpoint) is unchanged. Implements the first proposed option without flipping the default to a hard branch filter. +5 regression tests against the Step-1 pipeline.

jbetala7 avatar Jun 19 '26 06:06 jbetala7

+1 — the same root cause hits /qa and /qa-only too, just for a different artifact. Both select the test-plan with:

ls -t ~/.gstack/projects/$SLUG/*-test-plan-*.md 2>/dev/null | head -1

(qa/SKILL.md:1115, qa-only/SKILL.md:883) — newest-by-mtime, slug-scoped, no branch filter. With two worktrees on different branches each running /autoplan, whichever writes the newer *-test-plan-*.md wins, so /qa in the other worktree picks up its sibling's plan.

The good news is #2054's approach generalizes cleanly: the branch is already in the test-plan filename (<user>-<branch>-eng-review-test-plan-<ts>.md), so the same current-branch-first preference (with a cross-branch fallback) would resolve it without any new metadata. Might be worth extending #2054's fix to the test-plan lookup as well, or tracking it as a sibling. Thanks for the clear write-up here.

yolo-jared avatar Jun 26 '26 06:06 yolo-jared

@16francej — recommend closing this issue only after a revised #2054 passes real two-worktree selection and large sibling-checkpoint burst checks. The current selector can still choose another branch’s checkpoint when worktrees share a repository identifier. Keep current-branch preference, explicit cross-branch fallback, and the shared-origin contamination case as acceptance criteria.

time-attack avatar Jul 15 '26 13:07 time-attack