fix(preamble): skills start in worktree-isolated Claude Code sessions — one literal skill-start line, context-recovery helper, no eval in skill blocks, $N token tripwire, honest plan-mode precedence (#2763, #2896, #2851)

This commit is contained in:
garrytan committed 2026-10-02 19:24:31 +00:00
1 parent dd8c1b09e2
commit b8acf1584d
137 files changed
+1088 -1682

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
# gstack-context-recovery — recent project context for the skill preamble's
# "Context Recovery" step: newest plans/checkpoints, review count, recent
# timeline, last completed session, active decisions. Read-only; safe to run
# again after compaction.
#
# Replaces the inline fence every generated SKILL.md carried (#2763):
# worktree-isolated Claude Code sessions refuse its `eval "$(gstack-slug)"`
# and git-in-a-pipe forms, but run this one literal command.
#
# Branch-form discipline (#2550/#1851): review filenames use the slugged
# BRANCH that gstack-review-log writes; timeline filters use the raw branch
# (_BRANCH), which is what timeline JSON values hold.
#
# Passthrough text (timeline lines, file paths) is sanitized like
# gstack-skill-start's (OV4): it cannot mint instruction blocks.
_BIN=$(cd "$(dirname "$0")" 2>/dev/null && pwd)
. "$_BIN/gstack-state-root.sh" 2>/dev/null || { echo "$0: cannot resolve the gstack state root: $_BIN/gstack-state-root.sh is missing. fix: reinstall with ./setup or /gstack-upgrade (docs/state-root.md)" >&2; exit 1; }
gstack_state_root_select
_GH="$_gstack_sr_root"
_sanitize() { sed -e 's/GSTACK_INSTRUCTION/GSTACK-INSTRUCTION-(stripped)/g' -e 's/^SESSION_ID:/SESSION-ID-(stripped):/'; }
SLUG=""; BRANCH=""
eval "$("$_BIN/gstack-slug" 2>/dev/null)" 2>/dev/null || true
_BRANCH=$(git branch --show-current 2>/dev/null | tr -cd 'a-zA-Z0-9._/-')
_BRANCH=${_BRANCH:-unknown}
_PROJ="$_GH/projects/${SLUG:-unknown}"
[ -d "$_PROJ" ] || exit 0
{
echo "--- RECENT ARTIFACTS ---"
find "$_PROJ/ceo-plans" "$_PROJ/checkpoints" -type f -name "*.md" 2>/dev/null | xargs -r ls -t 2>/dev/null | head -3
[ -f "$_PROJ/${BRANCH:-unknown}-reviews.jsonl" ] && echo "REVIEWS: $(wc -l < "$_PROJ/${BRANCH:-unknown}-reviews.jsonl" | tr -d ' ') entries"
if [ -f "$_PROJ/timeline.jsonl" ]; then
tail -5 "$_PROJ/timeline.jsonl"
_LAST=$(grep "\"branch\":\"${_BRANCH}\"" "$_PROJ/timeline.jsonl" 2>/dev/null | grep '"event":"completed"' | tail -1)
[ -n "$_LAST" ] && echo "LAST_SESSION: $_LAST"
_RECENT_SKILLS=$(grep "\"branch\":\"${_BRANCH}\"" "$_PROJ/timeline.jsonl" 2>/dev/null | grep '"event":"completed"' | tail -3 | grep -o '"skill":"[^"]*"' | sed 's/"skill":"//;s/"//' | tr '\n' ',')
[ -n "$_RECENT_SKILLS" ] && echo "RECENT_PATTERN: $_RECENT_SKILLS"
fi
_LATEST_CP=$(find "$_PROJ/checkpoints" -name "*.md" -type f 2>/dev/null | xargs -r ls -t 2>/dev/null | head -1)
[ -n "$_LATEST_CP" ] && echo "LATEST_CHECKPOINT: $_LATEST_CP"
if [ -f "$_PROJ/decisions.active.json" ]; then
echo "--- ACTIVE DECISIONS (recent, scope-relevant) ---"
"$_BIN/gstack-decision-search" --recent 5 2>/dev/null
echo "--- END DECISIONS ---"
fi
echo "--- END ARTIFACTS ---"
} | _sanitize
+16
View File
@@ -3,6 +3,8 @@
# Usage: eval "$(gstack-paths)" → sets GSTACK_STATE_ROOT, PLAN_ROOT, TMP_ROOT
# Or: gstack-paths → prints GSTACK_STATE_ROOT=... etc.
# gstack-paths --explain → which variable selected the state root, and why
# gstack-paths --get NAME → print one raw value (NAME = GSTACK_STATE_ROOT,
# PLAN_ROOT or TMP_ROOT); exits 1 on failure
#
# Resolves three roots with explicit fallback chains so skills work the same
# whether installed as a Claude Code plugin (CLAUDE_PLUGIN_DATA / CLAUDE_PLANS_DIR
@@ -22,6 +24,10 @@
#
# Callers guard the eval, because eval "$(f)" succeeds even when f fails:
# eval "$(gstack-paths)"; : "${GSTACK_STATE_ROOT:?gstack-paths failed; reinstall with ./setup or /gstack-upgrade}"
# Skill bash blocks use --get instead (#2763): worktree-isolated Claude Code
# sessions refuse any eval/source, but run a plain assignment from a literal
# command, guarded the same way:
# GSTACK_STATE_ROOT=$(gstack-paths --get GSTACK_STATE_ROOT); : "${GSTACK_STATE_ROOT:?gstack-paths failed; reinstall with ./setup or /gstack-upgrade}"
#
# Output: values are emitted shell-quoted (printf %q) so `eval` round-trips them
# byte-for-byte. This matters on Windows, where $TMP is a backslash path like
@@ -139,6 +145,16 @@ _tmp_root="${_tmp_root%/}"
# will discover that on their own write attempt. Don't fail the eval here.
mkdir -p "$_tmp_root" 2>/dev/null || true
if [ "${1:-}" = "--get" ]; then
case "${2:-}" in
GSTACK_STATE_ROOT) printf '%s\n' "$_state_root" ;;
PLAN_ROOT) printf '%s\n' "$_plan_root" ;;
TMP_ROOT) printf '%s\n' "$_tmp_root" ;;
*) echo "usage: gstack-paths --get GSTACK_STATE_ROOT|PLAN_ROOT|TMP_ROOT" >&2; exit 2 ;;
esac
exit 0
fi
printf 'GSTACK_STATE_ROOT=%q\n' "$_state_root"
printf 'PLAN_ROOT=%q\n' "$_plan_root"
printf 'TMP_ROOT=%q\n' "$_tmp_root"
+24 -3
View File
@@ -11,8 +11,11 @@
# Enumerated divergences from the inline original (plan EOV5):
# - Paths resolve $0-relative (works for every host + install layout) instead
# of gen-time interpolation.
# - --parent-pid carries the HARNESS pid for session counting ($PPID inside
# this script is the ephemeral tool-call shell — wrong identity).
# - Session identity is the HARNESS pid, derived here: the nearest non-shell
# ancestor ($PPID inside this script is the ephemeral tool-call shell —
# wrong identity). --parent-pid still overrides it. The rendered command
# passes no "$PPID" (#2763: worktree-isolated Claude Code sessions refuse
# variables and fallbacks in the start command).
# - State paths honor ${GSTACK_HOME} (EOV7); the inline original hardcoded
# ~/.gstack in the bootstrap half.
# - SESSION_ID / TEL_START are echoed (the inline shell vars never survived
@@ -36,7 +39,7 @@ BASH_COMPAT=50
SKILL_NAME=""
MODEL_OVERLAY="none"
PARENT_PID="$PPID"
PARENT_PID=""
BRAIN_HEALTH="no"
while [ $# -gt 0 ]; do
case "$1" in
@@ -49,6 +52,24 @@ while [ $# -gt 0 ]; do
done
[ -n "$SKILL_NAME" ] || SKILL_NAME="unknown"
# Harness pid: walk up from the calling shell past shell wrappers. Claude
# Code runs this under a per-call `bash -c`, whose parent is the harness; a
# host that execs the command directly is already the parent. Without a
# usable ps (some Windows shells) the immediate parent stands.
if [ -z "$PARENT_PID" ]; then
PARENT_PID="$PPID"
_hops=0
while [ "$_hops" -lt 8 ]; do
_comm=$(ps -o comm= -p "$PARENT_PID" 2>/dev/null | head -1) || break
_comm=${_comm##*/}; _comm=${_comm#-}
case "$_comm" in bash|sh|zsh|dash|ksh|mksh|ash|fish|busybox) ;; *) break ;; esac
_up=$(ps -o ppid= -p "$PARENT_PID" 2>/dev/null | tr -d ' ')
case "$_up" in ''|*[!0-9]*|0|1) break ;; esac
PARENT_PID="$_up"
_hops=$((_hops + 1))
done
fi
_SCRIPT_DIR=$(cd "$(dirname "$0")" 2>/dev/null && pwd)
_BIN="$_SCRIPT_DIR"
. "$(dirname "$0")/gstack-state-root.sh" 2>/dev/null || { echo "$0: cannot resolve the gstack state root: $(dirname "$0")/gstack-state-root.sh is missing. fix: reinstall with ./setup or /gstack-upgrade (docs/state-root.md)" >&2; exit 1; }
+22
View File
@@ -2,6 +2,10 @@
# gstack-slug — output project slug and sanitized branch name
# Usage: eval "$(gstack-slug)" → sets SLUG and BRANCH variables
# Or: gstack-slug → prints SLUG=... and BRANCH=... lines
# gstack-slug --get NAME → print one raw value (SLUG, BRANCH or an
# --identity field); skill bash blocks
# use this form (#2763: worktree-isolated Claude
# Code sessions refuse eval/source)
# Modes (not on the hot path; see `gstack-slug --help`):
# --identity also print PROJECT_REMOTE, PROJECT_ROOT, LEGACY_SLUG
# --stamp-checkpoint <file> write remote:/project_root: into a checkpoint's frontmatter
@@ -86,6 +90,10 @@ COPY_PATHS=()
case "${1:-}" in
"") ;;
--identity) MODE=identity ;;
--get) MODE=get; MODE_ARG="${2:-}"
case "$MODE_ARG" in SLUG|BRANCH|PROJECT_REMOTE|PROJECT_ROOT|LEGACY_SLUG) ;;
*) echo "usage: gstack-slug --get SLUG|BRANCH|PROJECT_REMOTE|PROJECT_ROOT|LEGACY_SLUG" >&2; exit 2 ;;
esac ;;
--stamp-checkpoint) MODE=stamp; MODE_ARG="${2:-}"
[[ -n "$MODE_ARG" ]] || { echo "usage: gstack-slug --stamp-checkpoint <file>" >&2; exit 2; } ;;
--classify-checkpoints) MODE=classify ;;
@@ -101,6 +109,8 @@ case "${1:-}" in
cat <<'USAGE'
gstack-slug — print SLUG=<project slug> and BRANCH=<branch> for the current directory.
eval "$(gstack-slug)" set SLUG and BRANCH
SLUG=$(gstack-slug --get SLUG) print one raw value (SLUG, BRANCH, or an --identity
field); no eval needed
gstack-slug --identity also print PROJECT_REMOTE (canonical, credential-free),
PROJECT_ROOT and LEGACY_SLUG (pre-#3003 bucket, if different)
gstack-slug --stamp-checkpoint FILE write remote:/project_root: into FILE's frontmatter
@@ -371,6 +381,10 @@ RAW_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || RAW_BRANCH=""
BRANCH=$(printf '%s' "${RAW_BRANCH:-}" | tr '/' '-' | tr -cd 'a-zA-Z0-9._-')
BRANCH="${BRANCH:-unknown}"
if [[ "$MODE" == "get" && ( "$MODE_ARG" == SLUG || "$MODE_ARG" == BRANCH ) ]]; then
if [[ "$MODE_ARG" == SLUG ]]; then printf '%s\n' "$SLUG"; else printf '%s\n' "$BRANCH"; fi
exit 0
fi
if [[ "$MODE" == "default" ]]; then
echo "SLUG=$SLUG"
echo "BRANCH=$BRANCH"
@@ -432,6 +446,14 @@ _classify_checkpoint() {
_resolve_identity
case "$MODE" in
get)
case "$MODE_ARG" in
PROJECT_REMOTE) printf '%s\n' "$ID_REMOTE" ;;
PROJECT_ROOT) printf '%s\n' "$ID_ROOT" ;;
LEGACY_SLUG) printf '%s\n' "$ID_LEGACY" ;;
esac
;;
identity)
echo "SLUG=$SLUG"
echo "BRANCH=$BRANCH"