fix: pre-landing review fixes

Use the PID in the lock token, anchor a relative state root once so do_derive's cd cannot leak the lock, and note the stale-takeover tradeoff.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
garrytanandcapy-ai[bot] committed 2026-10-31 18:57:05 +00:00
1 parent c3d045f609
commit 484f2e655c
2 files changed
+21 -3

No files matched your search

+8 -3
View File
@@ -41,6 +41,10 @@ ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
. "$(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
GSTACK_HOME="$_gstack_sr_root"
# A relative state root is anchored to the starting directory once, here:
# do_derive changes directory, and the profile lock must name the same path
# when it is taken and when it is released.
case "$GSTACK_HOME" in /*) ;; *) GSTACK_HOME="$PWD/$GSTACK_HOME" ;; esac
# Windows git-bash: GSTACK_HOME resolves to an MSYS path (/c/Users/...), which
# Bun on Windows cannot open as a filesystem path (bites --derive). Normalize
# once, here, before PROFILE_FILE/LEGACY_FILE are derived from it below —
@@ -66,7 +70,8 @@ shift || true
# their rename, and write a unique temp file. A lock older than 30 s belongs
# to a crashed writer and is taken over by atomic rename. A writer that
# cannot get the lock within the give-up window writes nothing and exits 5.
# Same shape as bin/gstack-settings-hook's mutation lock.
# Same shape as bin/gstack-settings-hook's mutation lock, with its accepted
# tradeoff: two waiters that take over the same stale lock at once can race.
# -----------------------------------------------------------------------
LOCK_DIR="$PROFILE_FILE.lock"
LOCK_TOKEN=""
@@ -87,13 +92,13 @@ acquire_lock() {
mkdir -p "$GSTACK_HOME"
# GSTACK_PROFILE_LOCK_TIMEOUT_MS: test-only override for the give-up.
local give_up_ms="${GSTACK_PROFILE_LOCK_TIMEOUT_MS:-10000}" waited_ms=0 mtime stale
local token="$-$RANDOM$RANDOM"
local token="$$-$RANDOM$RANDOM"
while ! mkdir "$LOCK_DIR" 2>/dev/null; do
# GNU stat first: BSD-style stat -f on Linux prints a filesystem block.
mtime=$(stat -c %Y "$LOCK_DIR" 2>/dev/null || stat -f %m "$LOCK_DIR" 2>/dev/null || echo "")
case "$mtime" in *[!0-9]*) mtime="" ;; esac
if [ -n "$mtime" ] && [ $(( $(date +%s) - mtime )) -gt 30 ]; then
stale="$LOCK_DIR.stale.$-$RANDOM"
stale="$LOCK_DIR.stale.$$-$RANDOM"
if mv "$LOCK_DIR" "$stale" 2>/dev/null; then rm -rf "$stale"; fi
continue
fi
+13
View File
@@ -813,6 +813,19 @@ describe('gstack-developer-profile concurrent writers (#3437)', () => {
expect(fs.existsSync(lockDir())).toBe(false);
});
test('a relative state root does not leave the lock behind', () => {
const env = { ...process.env, GSTACK_HOME: 'state' };
for (const args of [['--derive'], ['--log-session', JSON.stringify({ date: '2026-10-31', mode: 'builder', project_slug: 'p' })]]) {
const res = spawnSync(BIN_DEV, args, { env, encoding: 'utf-8', cwd: tmpHome, timeout: 30_000 });
expect(res.stderr).toBe('');
expect(res.status).toBe(0);
}
const p = JSON.parse(fs.readFileSync(path.join(tmpHome, 'state', 'developer-profile.json'), 'utf-8'));
expect(p.sessions.length).toBe(1);
expect(typeof p.derived_at).toBe('string');
expect(fs.existsSync(path.join(tmpHome, 'state', 'developer-profile.json.lock'))).toBe(false);
});
test('overlapping derives and log-sessions keep every session and leave valid JSON', async () => {
for (let i = 0; i < 5; i++) {
logQuestion({ skill: 'plan-ceo-review', question_id: 'plan-ceo-review-mode', question_summary: 'mode', user_choice: 'expand', session_id: 's' + i });