fix(slug): gstack-slug matches remote-slug's owner-repo canonical form (live misfile bug)

Found live during this wave's CEO review: bin/gstack-slug emitted
SLUG=garrytan for this garrytan/gstack worktree while remote-slug correctly
gave garrytan-gstack — decisions, timeline, ceo-plans, and learnings were
filing into the wrong project store (observed polluting Context Recovery with
another repo's decisions). Root cause: a stray empty ~/.git directory made
the walk-up crown $HOME as the outermost project root; the remote lookup ran
only against that root, failed silently, and the basename fallback cached
'garrytan' sticky. NOT worktree-specific — any strong marker on a non-repo
ancestor triggered it.

Fix: the walk now finds the outermost ancestor whose .git actually resolves
an origin remote and derives owner-repo with remote-slug's byte-identical
parse; marker-only ancestors keep anchoring the basename fallback but can no
longer shadow a real remote. A new cache self-heal recomputes the poisoned
shape (cached == basename of a marker root while a remote-bearing repo exists
below), preserving legit #2212 stickiness. Nested-repo walk-up, no-remote and
non-git fallbacks, and the SLUG=/BRANCH= eval contract are unchanged, pinned
by a 10-case parity suite. Store migration for pre-fix data is tracked in
TODOS.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-08-17 10:43:54 -07:00
co-authored by Claude Fable 5
parent d8a207fdd7
commit e57b2798fd
2 changed files with 325 additions and 13 deletions
+89 -13
View File
@@ -13,11 +13,30 @@
# Without this walk-up, running gstack-slug from a subdir whose only
# "marker" is a deploy artifact silently resolves to the subdir's basename,
# misfiling all session state under a phantom slug. (2026-05-25 bug fix.)
# 2. If the resolved project root has a git remote, derive the slug from it.
# 2. Derive the slug from the canonical git remote: the OUTERMOST ancestor
# that is an actual git repo (`.git` directory, or `.git` FILE for
# worktrees/submodules) with an `origin` remote wins. The slug is
# `owner-repo`, parsed EXACTLY like browse/bin/remote-slug so the two
# bins can never disagree on a canonical-remote repo. Marker-only
# ancestors that are NOT remote-bearing repos (a stray empty ~/.git,
# a stray package.json in $HOME) still anchor the basename FALLBACK,
# but they can no longer shadow a real remote. (2026-08-17 bug fix:
# a stray empty ~/.git made the walk-up pick $HOME as project root;
# $HOME has no origin, so EVERY repo under it degraded to
# SLUG=<username> — one shared bucket for all projects.)
# 3. Otherwise use the basename of the resolved project root.
# 4. If no project root was found anywhere on the chain, fall back to the
# basename of $(pwd) (preserves prior behavior for plain folders).
#
# MIGRATION NOTE (2026-08-17): sessions run BEFORE the remote-first fix above,
# in repos below a stray marker-bearing ancestor, filed their session state
# (decisions / timeline / ceo-plans / learnings) under the DEGRADED slug —
# ~/.gstack/projects/<ancestor-basename>/ (e.g. `garrytan`) instead of the
# canonical ~/.gstack/projects/<owner-repo>/. The slug cache self-heals on the
# next invocation (see 1b), but already-written store data does NOT move.
# Whether/how to merge those stores is tracked in TODOS.md — do not add data
# migration code here.
#
# Caching is self-healing: a cache entry for the literal pwd that differs from
# the freshly-computed slug gets opportunistically rewritten (single-shot, key-
# local — never sweeps other entries). This lets pre-existing poisoned caches
@@ -112,6 +131,32 @@ _outermost_project_root() {
fi
}
# 1a. Outermost REMOTE-BEARING repo root: walk the same ancestor chain and
# track the outermost dir that has a `.git` entry (directory for normal
# clones, FILE for git-worktrees/submodules — `git -C` resolves a
# worktree's remote through its main clone) AND whose `origin` remote
# resolves. This is the canonical-identity walk: a marker-only ancestor
# with no resolvable origin (stray empty ~/.git, stray package.json)
# cannot win here, so it cannot hijack remote-derived identity the way
# it can hijack the marker walk above. Nested-repo semantics preserved:
# an inner repo under an outer canonical-remote repo still resolves to
# the OUTER repo's remote (outermost wins), same as before.
# Note: git spawns only at `.git`-bearing ancestors — typically one.
_outermost_remote_repo() {
local dir="$1"
local outermost="" parent="" depth=0
while [[ -n "$dir" && "$dir" != "/" && $depth -lt 64 ]]; do
if [[ -e "$dir/.git" ]] && git -C "$dir" remote get-url origin >/dev/null 2>&1; then
outermost="$dir"
fi
parent=$(dirname "$dir")
[[ "$parent" == "$dir" ]] && break # dirname fixed point (C:/, ., //srv)
dir="$parent"
depth=$((depth + 1))
done
printf '%s' "$outermost"
}
# Only compute the project root if we don't already have a slug (env override
# took precedence). The walk is cheap (~10 stats on the deepest realistic cwd).
PROJECT_ROOT=""
@@ -119,34 +164,65 @@ if [[ -z "$SLUG" ]]; then
PROJECT_ROOT=$(_outermost_project_root "$PROJECT_DIR")
fi
# Lazy, memoized remote discovery. Needed on exactly two paths: fresh
# resolution (no usable cache) and the degraded-ancestor heal check below.
# Gating it keeps ordinary cache hits git-spawn-free.
REMOTE_ROOT=""
REMOTE_URL=""
_REMOTE_RESOLVED=0
_resolve_remote() {
if [[ "$_REMOTE_RESOLVED" -eq 1 ]]; then return 0; fi
_REMOTE_RESOLVED=1
REMOTE_ROOT=$(_outermost_remote_repo "$PROJECT_DIR")
if [[ -n "$REMOTE_ROOT" ]]; then
REMOTE_URL=$(git -C "$REMOTE_ROOT" remote get-url origin 2>/dev/null) || REMOTE_URL=""
fi
return 0
}
# 1b. Cached identity is STICKY (#2212): a project that used gstack before it
# adopted a git remote keeps its pre-origin slug — recomputing from the
# remote here would rename the project mid-life and orphan everything
# under ~/.gstack/projects/<slug>/. The ONE exception is the provable
# old-bug shape (#1125): the pre-walk-up resolver cached basename(pwd)
# for a SUBDIRECTORY of the real project — if the cached value equals this
# pwd's basename while the walk-up says pwd is NOT the project root, the
# cache came from that bug, not from legitimate identity; fall through and
# recompute so it heals.
# under ~/.gstack/projects/<slug>/. TWO provable bug shapes are exempt
# and fall through to recompute (self-heal):
# - Old-bug shape (#1125): the pre-walk-up resolver cached basename(pwd)
# for a SUBDIRECTORY of the real project — cached == pwd basename while
# the walk-up says pwd is NOT the project root.
# - Degraded-ancestor shape (2026-08-17): the pre-remote-first resolver
# cached basename(PROJECT_ROOT) for a marker-only ancestor (stray
# ~/.git) that is NOT the remote-bearing repo — cached == the marker
# root's basename while a remote-bearing repo BELOW it exists. Legit
# #2212 stickiness is safe: there the repo that adopted the remote IS
# the marker root (REMOTE_ROOT == PROJECT_ROOT), so the heal never fires.
if [[ -z "$SLUG" && -f "$CACHE_FILE" ]]; then
_CACHED=$(cat "$CACHE_FILE" 2>/dev/null | tr -cd 'a-zA-Z0-9._-')
if [[ -n "$_CACHED" ]]; then
_PWD_BASE=$(basename "$PROJECT_DIR" | tr -cd 'a-zA-Z0-9._-')
_ROOT_BASE=""
if [[ -n "$PROJECT_ROOT" ]]; then
_ROOT_BASE=$(basename "$PROJECT_ROOT" | tr -cd 'a-zA-Z0-9._-')
fi
if [[ "$_CACHED" == "$_PWD_BASE" && -n "$PROJECT_ROOT" && "$PROJECT_ROOT" != "$PROJECT_DIR" ]]; then
: # old-bug shape — recompute below and self-heal the cache
elif [[ -n "$PROJECT_ROOT" && "$_CACHED" == "$_ROOT_BASE" ]] \
&& { _resolve_remote; [[ -n "$REMOTE_URL" && "$REMOTE_ROOT" != "$PROJECT_ROOT" ]]; }; then
: # degraded-ancestor shape — recompute below and self-heal the cache
else
SLUG="$_CACHED"
fi
fi
fi
# 2. If we found a project root and it has a git remote, derive slug from the
# remote URL (existing logic — kept verbatim, just rooted at PROJECT_ROOT
# instead of $PWD so a subdir without its own remote inherits the parent's).
if [[ -z "$SLUG" && -n "$PROJECT_ROOT" ]]; then
REMOTE_URL=$(git -C "$PROJECT_ROOT" remote get-url origin 2>/dev/null) || REMOTE_URL=""
# 2. Canonical remote-derived slug. Sourced from the outermost remote-bearing
# repo (see 1a) — NOT from PROJECT_ROOT, which may be a marker-only
# ancestor with no remote. Parse kept byte-identical to
# browse/bin/remote-slug (strip trailing .git, then owner/repo → owner-repo)
# so the two bins agree on every canonical-remote repo, worktrees included.
# Parity pinned by test/gstack-slug-parity.test.ts.
if [[ -z "$SLUG" ]]; then
_resolve_remote
if [[ -n "$REMOTE_URL" ]]; then
RAW_SLUG=$(printf '%s' "$REMOTE_URL" | sed 's|.*[:/]\([^/]*/[^/]*\)\.git$|\1|;s|.*[:/]\([^/]*/[^/]*\)$|\1|' | tr '/' '-')
RAW_SLUG=$(printf '%s' "${REMOTE_URL%.git}" | sed -E 's#.*[:/]([^/]+)/([^/]+)$#\1-\2#')
SLUG=$(printf '%s' "$RAW_SLUG" | tr -cd 'a-zA-Z0-9._-')
fi
fi