From 4d538738bea60863f49ce375ae878c4829d939c7 Mon Sep 17 00:00:00 2001 From: davidfragnoli-lang <249433777+davidfragnoli-lang@users.noreply.github.com> Date: Wed, 14 Oct 2026 07:06:36 +0200 Subject: [PATCH] fix: gstack-config compares whole root paths and prints a runnable recovery command - report_root_disagreement compared word-split values, so two variables naming the same directory with a space were reported as disagreeing. Compare each complete value instead. - The privacy-key recovery hint single-quoted the root and left the script path bare; an apostrophe or space made it unrunnable. Quote with printf %q. - On Bash 3.2 (macOS /bin/bash) a failed `.` under set -e exits before the `||` fallback, so a missing gstack-state-root.sh exited 1 silently and test/state-root-resolver-failure.test.ts failed there. Check readability first. - The merge fixture now lives under a path with a space and an apostrophe, executes the printed command, and asserts same-directory variables agree. Co-Authored-By: Claude Opus 5.5 --- bin/gstack-config | 11 +++++------ test/state-root-config-merge.test.ts | 8 +++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/bin/gstack-config b/bin/gstack-config index 53122a6be6f..8e33aeb9a8b 100755 --- a/bin/gstack-config +++ b/bin/gstack-config @@ -27,7 +27,7 @@ # Docs: https://github.com/garrytan/gstack/blob/main/docs/state-root.md set -euo pipefail -. "$(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; } +[ -r "$(dirname "$0")/gstack-state-root.sh" ] && . "$(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; } gstack_state_root_select STATE_DIR="$_gstack_sr_root" CONFIG_FILE="$STATE_DIR/config.yaml" @@ -621,15 +621,14 @@ _SELF="$(cd "$(dirname "$0")" && pwd)/gstack-config" # One line when the root-selecting variables name different directories. report_root_disagreement() { - local _v _val _set="" _distinct="" + local _v _val _set="" _first="" _disagrees=0 for _v in GSTACK_STATE_ROOT GSTACK_HOME GSTACK_STATE_DIR; do eval "_val=\${$_v:-}" [ -n "$_val" ] || continue _set="$_set $_v=$_val" - case " $_distinct " in *" $_val "*) ;; *) _distinct="$_distinct $_val" ;; esac + if [ -z "$_first" ]; then _first="$_val"; elif [ "$_val" != "$_first" ]; then _disagrees=1; fi done - set -- $_distinct - [ "$#" -gt 1 ] || return 0 + [ "$_disagrees" -eq 1 ] || return 0 echo "# note: root-selecting variables disagree:$_set; using $STATE_DIR ($_gstack_sr_var wins). Explain: $(dirname "$_SELF")/gstack-paths --explain ($_STATE_DOC)" } @@ -971,7 +970,7 @@ $_gstack_legacy_root" gstack_config_select "$KEY" if [ -n "$_gstack_cfg_root" ] && [ "$_gstack_cfg_root" != "$STATE_DIR" ] && [ "$_gstack_cfg_value" != "$SAFE_VALUE" ]; then echo "gstack-config: set $KEY in $CONFIG_FILE, but $KEY is still '$_gstack_cfg_value': $_gstack_cfg_root/config.yaml sets that more restrictive value, and for privacy keys the most restrictive value across state roots wins." >&2 - echo "fix: GSTACK_STATE_ROOT='$_gstack_cfg_root' $_SELF set $KEY $SAFE_VALUE ($_STATE_DOC)" >&2 + printf 'fix: GSTACK_STATE_ROOT=%q %q set %q %q (%s)\n' "$_gstack_cfg_root" "$_SELF" "$KEY" "$SAFE_VALUE" "$_STATE_DOC" >&2 fi # Auto-relink skills when prefix setting changes (skip during setup to avoid recursive call) if { [ "$KEY" = "skill_prefix" ] || [ "$KEY" = "disabled_skills" ]; } && [ -z "${GSTACK_SETUP_RUNNING:-}" ]; then diff --git a/test/state-root-config-merge.test.ts b/test/state-root-config-merge.test.ts index efc614727cb..e51400e0c5e 100644 --- a/test/state-root-config-merge.test.ts +++ b/test/state-root-config-merge.test.ts @@ -20,7 +20,7 @@ const POLICY = path.join(ROOT, 'bin', 'gstack-gbrain-repo-policy'); let tmp: string, home: string, legacy: string, resolved: string, env: Record; beforeEach(() => { - tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-merge-')); + tmp = fs.mkdtempSync(path.join(os.tmpdir(), "gstack merge's-")); home = path.join(tmp, 'home'); legacy = path.join(home, '.gstack'); resolved = path.join(tmp, 'custom'); @@ -70,7 +70,7 @@ describe('merged config keys', () => { expect(r.stderr).toContain(`gstack-config: set codex_reviews in ${resolved}/config.yaml, but codex_reviews is still 'disabled': ${legacy}/config.yaml sets that more restrictive value`); expect(r.stderr).toContain('docs/state-root.md'); const fix = r.stderr.split('\n').find((l) => l.startsWith('fix: '))!; - expect(fix).toContain(`GSTACK_STATE_ROOT='${legacy}'`); + expect(fix).toStartWith('fix: GSTACK_STATE_ROOT='); const command = fix.slice('fix: '.length).replace(/ \(https:.*\)$/, ''); expect(spawnSync('bash', ['-c', command], { env, encoding: 'utf-8', timeout: 20_000 }).status).toBe(0); expect(config(['get', 'codex_reviews']).stdout).toBe('enabled'); @@ -83,13 +83,15 @@ describe('merged config keys', () => { expect(r.stderr).toBe(''); }); - test('list shows the winning root for merged keys and a disagreement line when root variables differ', () => { + test('list shows the winning root for merged keys and a disagreement line only when root variables name different directories', () => { fs.writeFileSync(path.join(legacy, 'config.yaml'), 'telemetry: off\n'); fs.writeFileSync(path.join(resolved, 'config.yaml'), 'telemetry: community\nproactive: false\n'); const plain = config(['list']).stdout; expect(plain).toContain(`telemetry: off (set, ${legacy})`); expect(plain).toContain('proactive: false (set)'); expect(plain).not.toContain('root-selecting variables disagree'); + // Two variables naming the same directory agree, even when the path contains spaces. + expect(config(['list'], { GSTACK_STATE_ROOT: resolved }).stdout).not.toContain('root-selecting variables disagree'); const other = path.join(tmp, 'other'); const disagree = config(['list'], { GSTACK_STATE_DIR: other }).stdout; expect(disagree).toContain(`# note: root-selecting variables disagree: GSTACK_HOME=${resolved} GSTACK_STATE_DIR=${other}; using ${resolved} (GSTACK_HOME wins).`);