Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion USING_GBRAIN_WITH_GSTACK.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Gbrain itself ships with these that gstack wraps:
|---|---|
| `~/.gbrain/config.json` | Engine (pglite/postgres), database URL or path, API keys. Mode 0600. Written by `gbrain init`. When `GBRAIN_HOME` is set, gbrain and gstack's helpers read `$GBRAIN_HOME/.gbrain/config.json` instead. |
| `~/.gstack/gbrain-repo-policy.json` | Per-remote trust triad. Schema v2. Mode 0600. |
| `~/.gstack/.setup-gbrain.lock.d` | Concurrent-run lock (atomic mkdir). Released on normal exit + SIGINT. |
| `~/.gstack/.setup-gbrain.lock.d` | Concurrent-run lock: a directory whose `pid` file is created with O_EXCL (`gstack_lock_take`, `bin/gstack-lock.sh`), so it holds even where `mkdir` misreports a race. Released on normal exit + SIGINT. |
| `~/.gstack/.brain-queue.d/` | Pending sync records for gstack memory sync — maildir-style spool, one file per record. A legacy `.brain-queue.jsonl` from older releases migrates automatically on the next drain. |
| `~/.gstack/.brain-last-push` | Timestamp of last sync push (for `/health` scoring) |
| `~/.gstack-artifacts-remote.txt` | URL of your gstack memory sync remote (safe to copy between machines; legacy name `~/.gstack-brain-remote.txt` still read) |
Expand Down
15 changes: 9 additions & 6 deletions bin/gstack-brain-lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@
# gstack_brain_lock_release
# Removes the lock only when this process took it and still owns it.
#
# The lock is a directory (mkdir is atomic on every POSIX filesystem; flock(1)
# is not on macOS) holding `pid` and `owner`. A lock whose pid is dead is
# stale and is cleared. A lock directory with no pid file is a holder killed
# between mkdir and the pid write once it is older than 10 minutes.
# The lock is a directory holding `pid` and `owner`, taken with
# gstack_lock_take (bin/gstack-lock.sh), which creates `pid` with O_EXCL:
# mkdir's exit status is not a lock on Ubuntu 26.04's uutils mkdir, and
# flock(1) is not on macOS. A lock whose pid is dead is stale and is cleared.
# A lock directory with no pid file is a holder killed between mkdir and the
# pid write once it is older than 10 minutes.

. "$(dirname "${BASH_SOURCE[0]}")/gstack-lock.sh" 2>/dev/null || gstack_lock_take() { echo "gstack: $(dirname "${BASH_SOURCE[0]}")/gstack-lock.sh is missing; reinstall with ./setup" >&2; return 2; }

_GSTACK_BRAIN_LOCK_DIR=""
_GSTACK_BRAIN_LOCK_MINE=0
_GSTACK_BRAIN_LOCK_HOLDER=""

_gstack_brain_lock_try_once() {
local dir="$1" pid
if mkdir "$dir" 2>/dev/null; then
if gstack_lock_take "$dir" pid "$$"; then
GSTACK_BRAIN_LOCK_TOKEN="$$-${RANDOM:-0}-$(date +%s 2>/dev/null || echo 0)"
export GSTACK_BRAIN_LOCK_TOKEN
printf '%s\n' "$$" > "$dir/pid" 2>/dev/null || true
printf '%s\n' "$GSTACK_BRAIN_LOCK_TOKEN" > "$dir/owner" 2>/dev/null || true
_GSTACK_BRAIN_LOCK_MINE=1
return 0
Expand Down
4 changes: 2 additions & 2 deletions bin/gstack-browser-ensure
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ BUN=$(command -v bun 2>/dev/null) || fail "Chromium is not installed and bun is

# Same lock ./setup takes (#2136), so two installs never race on the cache.
LOCK="${TMPDIR:-/tmp}/gstack-playwright-install.lock"
. "$ROOT/bin/gstack-lock.sh" 2>/dev/null || fail "$ROOT/bin/gstack-lock.sh is missing" "reinstall with cd $ROOT && ./setup"
tries=0
while ! mkdir "$LOCK" 2>/dev/null; do
while ! gstack_lock_take "$LOCK" pid "$$"; do
holder=$(cat "$LOCK/pid" 2>/dev/null || true)
case "$holder" in ''|*[!0-9]*) holder=0 ;; esac
if [ "$holder" -eq 0 ] || ! kill -0 "$holder" 2>/dev/null; then rm -rf "$LOCK" 2>/dev/null; continue; fi
tries=$((tries + 1))
[ "$tries" -ge "$TIMEOUT" ] && fail "another gstack install holds the Chromium lock ($LOCK, pid $holder)" "wait for it, or remove a stale lock: rm -rf \"$LOCK\""
sleep 1
done
echo "$$" > "$LOCK/pid" 2>/dev/null || true
trap 'rm -rf "$LOCK" 2>/dev/null' EXIT INT TERM

OVERRIDE=""
Expand Down
2 changes: 1 addition & 1 deletion bin/gstack-capy-session
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ EOF_OWN
report_behind "$RERUN"
export GSTACK_CAPY_BEHIND_BLOCKED="$REASON"
elif [ -n "$SWITCH" ] && ! { SETUP_LOCK="$("$ROOT/bin/gstack-paths" --get GSTACK_STATE_ROOT 2>/dev/null)/.setup-lock"; [ "$SETUP_LOCK" != /.setup-lock ] \
&& mkdir -p "$(dirname "$SETUP_LOCK")" 2>/dev/null && mkdir "$SETUP_LOCK" 2>/dev/null && echo "$$" > "$SETUP_LOCK/pid"; }; then
&& mkdir -p "$(dirname "$SETUP_LOCK")" 2>/dev/null && . "$ROOT/bin/gstack-lock.sh" && gstack_lock_take "$SETUP_LOCK" pid "$$"; }; then
# The switch and the fast-forward happen under gstack-upgrade's setup lock,
# taken here and handed over in GSTACK_SETUP_LOCK_OWNER, so a concurrent
# updater never sees the checkout between the two.
Expand Down
20 changes: 9 additions & 11 deletions bin/gstack-codex-probe
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fi
# === end gstack self-locate ===
_GSTACK_CODEX_BIN="$_gstack_helper_dir"
unset _gstack_helper _gstack_helper_dir _gstack_helper_shell
. "$_GSTACK_CODEX_BIN/gstack-lock.sh" 2>/dev/null || gstack_lock_take() { return 2; }

# --- Auth probe -------------------------------------------------------------

Expand Down Expand Up @@ -631,19 +632,16 @@ _gstack_codex_probe_cache_store() {

_gstack_codex_lock_acquire() {
# $1 lock directory, $2 seconds to wait, $3 seconds after which even a live
# owner is abandoned. mkdir is atomic on every platform bash runs on. A dead
# owner on this host is recovered at once; a missing owner file is the
# owner is abandoned. gstack_lock_take (bin/gstack-lock.sh) creates the owner
# file with O_EXCL, since mkdir's exit status is not a lock on Ubuntu 26.04's
# uutils mkdir. A dead owner on this host is recovered at once; a missing owner file is the
# creation window and becomes stale after 5 seconds. 0 = held, 1 = timed
# out, 2 = the lock or its owner record cannot be created.
local _dir="$1" _tries=$(( ${2:-45} * 10 )) _stale="${3:-90}" _host _owner _pid _ohost _ts _now _i=0 _dead
_host=$(uname -n 2>/dev/null || echo unknown)
while :; do
if mkdir "$_dir" 2>/dev/null; then
printf '%s %s %s\n' "$$" "$_host" "$(date +%s 2>/dev/null || echo 0)" > "$_dir/owner" 2>/dev/null && return 0
rm -f "$_dir/owner" 2>/dev/null
rmdir "$_dir" 2>/dev/null
return 2
fi
gstack_lock_take "$_dir" owner "$$ $_host $(date +%s 2>/dev/null || echo 0)"
case $? in 0) return 0 ;; 3) return 2 ;; esac
if [ ! -d "$_dir" ]; then
{ [ -e "$_dir" ] || [ -L "$_dir" ]; } && return 2
{ [ -d "${_dir%/*}" ] && [ -w "${_dir%/*}" ] && [ -x "${_dir%/*}" ]; } || return 2
Expand Down Expand Up @@ -677,15 +675,15 @@ _gstack_codex_lock_break() {
# Breakers serialize on $1.break and remove the lock only if it still has the
# owner they judged abandoned, so a lock re-taken meanwhile survives.
local _dir="$1" _seen="$2" _bt _now
if mkdir "$_dir.break" 2>/dev/null; then
if gstack_lock_take "$_dir.break" pid "$$"; then
[ "$(cat "$_dir/owner" 2>/dev/null)" = "$_seen" ] && rm -rf "$_dir" 2>/dev/null
rmdir "$_dir.break" 2>/dev/null
rm -rf "$_dir.break" 2>/dev/null
return 0
fi
_now=$(date +%s 2>/dev/null || echo 0)
_bt=$(stat -c %Y "$_dir.break" 2>/dev/null || stat -f %m "$_dir.break" 2>/dev/null || echo "$_now")
case "$_bt" in ''|*[!0-9]*) _bt="$_now" ;; esac
[ $((_now - _bt)) -ge 10 ] && rmdir "$_dir.break" 2>/dev/null
[ $((_now - _bt)) -ge 10 ] && rm -rf "$_dir.break" 2>/dev/null
return 0
}

Expand Down
14 changes: 8 additions & 6 deletions bin/gstack-config
Original file line number Diff line number Diff line change
Expand Up @@ -552,18 +552,21 @@ read_config_value() {

# ── Config writes: one lock, one read-modify-write rename ──────────────────
# Every mutation of config.yaml (set, unset, the transcript_repos pair, the
# user-slug writer) holds an atomic mkdir lock and replaces the file with one
# user-slug writer) holds one lock directory and replaces the file with one
# rename, so concurrent writers never lose each other's keys and an
# interrupted write leaves the old file. The pid file holds "<pid> <epoch>";
# a lock whose holder is dead or older than 10 s is taken over. No flock(1):
# stock macOS does not ship it.
# interrupted write leaves the old file. The lock is taken with
# gstack_lock_take (bin/gstack-lock.sh), which creates the pid file with
# O_EXCL because mkdir's exit status is not a lock on Ubuntu 26.04's uutils
# mkdir. The pid file holds "<pid> <epoch>"; a lock whose holder is dead or
# older than 10 s is taken over. No flock(1): stock macOS does not ship it.
CONFIG_LOCK="$STATE_DIR/.config-yaml.lock"
_config_locked=""

config_lock() {
mkdir -p "$STATE_DIR"
. "$(dirname "$0")/gstack-lock.sh" 2>/dev/null || { echo "Error: $(dirname "$0")/gstack-lock.sh is missing. Existing value left unchanged. fix: reinstall with ./setup or /gstack-upgrade" >&2; exit 1; }
local waited=0 held pid ts stale
while ! mkdir "$CONFIG_LOCK" 2>/dev/null; do
while ! gstack_lock_take "$CONFIG_LOCK" pid "$$ $(date +%s)"; do
if [ ! -w "$STATE_DIR" ]; then
echo "Error: cannot write to $STATE_DIR (its config lock could not be created). Existing value left unchanged." >&2
exit 1
Expand All @@ -589,7 +592,6 @@ config_lock() {
fi
sleep 0.05
done
printf '%s %s\n' "$$" "$(date +%s)" > "$CONFIG_LOCK/pid"
_config_locked=1
trap 'config_unlock' EXIT
}
Expand Down
22 changes: 12 additions & 10 deletions bin/gstack-developer-profile
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ shift || true
# changes and renames the whole file, and gstack-question-log starts a
# background --derive after every append, so writers overlap: a shared temp
# name made renames fail and an unlocked rewrite dropped a concurrent
# --log-session. Writers hold a mkdir lock (<profile>.lock/, whose owner file
# --log-session. Writers hold a lock directory (<profile>.lock/, whose owner file
# names the holder's pid and a random token; release is ownership-checked)
# from before their read until after their rename, and write a unique temp
# file. A lock whose holder process is gone is taken over at once; one older
# than 5 minutes is taken over even when its pid is alive, since no writer
# runs that long and the pid may have been reused. Takeovers are serialized by a second mkdir lock
# runs that long and the pid may have been reused. Takeovers are serialized by a second lock
# (<profile>.lock.takeover/) and re-read the owner under it, so two waiters
# cannot both take over or remove a lock a third writer just took. A lock or
# takeover directory older than 30 s with no owner belongs to a writer that
# died before writing one; cleanup removes a takeover lock this writer holds. A writer that cannot get the lock within the
# died before writing one; cleanup removes a takeover lock this writer holds. Both locks
# are taken with gstack_lock_take (bin/gstack-lock.sh): mkdir's exit status is not
# a lock on Ubuntu 26.04's uutils mkdir, so the owner file is created with O_EXCL. A writer that cannot get the lock within the
# give-up window writes nothing and exits 5. Writers outside this binary
# (gstack-distill-apply, /plan-tune's declared edits) do not take it yet.
# -----------------------------------------------------------------------
Expand All @@ -87,7 +89,7 @@ PROFILE_TMP=""

cleanup() {
if [ -n "$PROFILE_TMP" ]; then rm -f "$PROFILE_TMP"; fi
if [ -n "$HOLDS_TAKEOVER" ]; then rmdir "$TAKEOVER_DIR" 2>/dev/null || true; fi
if [ -n "$HOLDS_TAKEOVER" ]; then rm -rf "$TAKEOVER_DIR" 2>/dev/null || true; fi
if [ -n "$LOCK_TOKEN" ] && [ "$(cat "$LOCK_DIR/owner" 2>/dev/null || true)" = "$LOCK_TOKEN" ]; then
rm -rf "$LOCK_DIR"
fi
Expand Down Expand Up @@ -117,8 +119,9 @@ acquire_lock() {
# 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 started=$SECONDS vanished=0 owner
local token="$$-$RANDOM$RANDOM"
until mkdir "$LOCK_DIR" 2>/dev/null; do
# mkdir failed with no lock to wait for: retry once in case a holder just
. "$SCRIPT_DIR/gstack-lock.sh" 2>/dev/null || { echo "gstack-developer-profile: $SCRIPT_DIR/gstack-lock.sh is missing, so nothing was written (exit 5). fix: reinstall with ./setup or /gstack-upgrade" >&2; exit 5; }
until gstack_lock_take "$LOCK_DIR" owner "$token"; do
# The take failed with no lock to wait for: retry once in case a holder just
# released it, then report the directory instead of waiting out the window.
if [ ! -e "$LOCK_DIR" ]; then
if [ "$vanished" -eq 0 ]; then vanished=1; continue; fi
Expand All @@ -128,13 +131,13 @@ acquire_lock() {
vanished=0
owner=$(cat "$LOCK_DIR/owner" 2>/dev/null || true)
if [ -d "$LOCK_DIR" ] && lock_abandoned "$owner"; then
if [ -d "$TAKEOVER_DIR" ] && [ "$(dir_age_s "$TAKEOVER_DIR")" -gt 30 ]; then rmdir "$TAKEOVER_DIR" 2>/dev/null || true; fi
if mkdir "$TAKEOVER_DIR" 2>/dev/null; then
if [ -d "$TAKEOVER_DIR" ] && [ "$(dir_age_s "$TAKEOVER_DIR")" -gt 30 ]; then rm -rf "$TAKEOVER_DIR" 2>/dev/null || true; fi
if gstack_lock_take "$TAKEOVER_DIR" pid "$$"; then
HOLDS_TAKEOVER=1
if [ "$(cat "$LOCK_DIR/owner" 2>/dev/null || true)" = "$owner" ] && lock_abandoned "$owner"; then
rm -rf "$LOCK_DIR" 2>/dev/null || true
fi
rmdir "$TAKEOVER_DIR" 2>/dev/null || true
rm -rf "$TAKEOVER_DIR" 2>/dev/null || true
HOLDS_TAKEOVER=""
if [ ! -e "$LOCK_DIR" ]; then continue; fi
fi
Expand All @@ -146,7 +149,6 @@ acquire_lock() {
sleep 0.05
waited_ms=$(( waited_ms + 50 ))
done
printf '%s\n' "$token" > "$LOCK_DIR/owner"
LOCK_TOKEN="$token"
}

Expand Down
28 changes: 24 additions & 4 deletions bin/gstack-ff-update
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# the new revision (git show FETCH_HEAD:bin/gstack-ff-update) and run it in a
# private temp directory, so it sources nothing. The lock and the
# flatten-and-strip sanitizer are inlined; test/gstack-ff-update.test.ts pins
# them to their twins in bin/gstack-session-update.
# them to their twins in bin/gstack-session-update, and gstack_lock_take to
# bin/gstack-lock.sh.
#
# Inside an install-scoped lock (<git-common-dir>/gstack-ff.lock):
# 1. The working tree is captured as-is through a private index (tracked
Expand Down Expand Up @@ -115,18 +116,38 @@ LOCK_DIR="$COMMON/gstack-ff.lock"
LOCK_TTL_MINUTES=30
MYPID=$$
OWN_INDEX_LOCK="" TMP="" HB_PID=""
# Twin of bin/gstack-lock.sh (test-pinned): mkdir's exit status is not a lock
# on Ubuntu 26.04's uutils mkdir, so the pid file is created with O_EXCL.
gstack_lock_take() {
local _gl_dir="$1" _gl_file="${2:-pid}" _gl_content="${3:-$$}"
if [ -e "$_gl_dir" ] || [ -L "$_gl_dir" ]; then return 1; fi
mkdir "$_gl_dir" 2>/dev/null || :
if [ ! -d "$_gl_dir" ] || [ -L "$_gl_dir" ]; then
if [ -e "$_gl_dir" ] || [ -L "$_gl_dir" ]; then return 1; fi
return 2
fi
local _gl_opened
_gl_opened=$(set -o noclobber; { echo o; printf '%s\n' "$_gl_content" >&3; } 2>/dev/null 3> "$_gl_dir/$_gl_file") && return 0
if [ "$_gl_opened" = o ]; then
rm -f "$_gl_dir/$_gl_file"; rmdir "$_gl_dir" 2>/dev/null || :
return 3
fi
if [ -e "$_gl_dir/$_gl_file" ] || [ -L "$_gl_dir/$_gl_file" ] || [ ! -d "$_gl_dir" ]; then return 1; fi
rmdir "$_gl_dir" 2>/dev/null || :
return 3
}
lock_is_expired() {
_hb="$LOCK_DIR/pid"
[ -f "$_hb" ] || _hb="$LOCK_DIR"
[ -n "$(find "$_hb" -maxdepth 0 -mmin +$LOCK_TTL_MINUTES 2>/dev/null)" ]
}
lock_try() {
mkdir "$LOCK_DIR" 2>/dev/null && return 0
gstack_lock_take "$LOCK_DIR" pid "$MYPID" && return 0
_lp=$(cat "$LOCK_DIR/pid" 2>/dev/null)
if lock_is_expired || { [ "${_lp:-0}" -gt 0 ] 2>/dev/null && ! kill -0 "$_lp" 2>/dev/null; }; then
mv "$LOCK_DIR" "$LOCK_DIR.reap.$$" 2>/dev/null || return 1
rm -rf "$LOCK_DIR.reap.$$" 2>/dev/null
mkdir "$LOCK_DIR" 2>/dev/null && return 0
gstack_lock_take "$LOCK_DIR" pid "$MYPID" && return 0
fi
return 1
}
Expand All @@ -145,7 +166,6 @@ until lock_try; do
[ "$_wait" -gt 0 ] || refuse 75 ff-busy "another gstack update is moving $DIR (lock $LOCK_DIR held by pid $(cat "$LOCK_DIR/pid" 2>/dev/null || echo unknown)); nothing was changed, retry in a minute"
sleep 1; _wait=$((_wait - 1))
done
echo "$MYPID" > "$LOCK_DIR/pid" 2>/dev/null
trap cleanup_ff_update EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
Expand Down
13 changes: 8 additions & 5 deletions bin/gstack-install-registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
# render render directory, or 'committed'
#
# Callers must set GSTACK_STATE_ROOT first (bin/gstack-state-root.sh). Bash 3.2
# clean; no flock (macOS lacks it), so the lock is an atomic mkdir.
# clean; no flock (macOS lacks it), so the lock is gstack_lock_take
# (bin/gstack-lock.sh).

# Same-path and containment checks (bin/gstack-path-spelling.sh).
. "$(dirname "${BASH_SOURCE[0]}")/gstack-path-spelling.sh" 2>/dev/null || true
. "$(dirname "${BASH_SOURCE[0]}")/gstack-lock.sh" 2>/dev/null || gstack_lock_take() { echo "gstack: $(dirname "${BASH_SOURCE[0]}")/gstack-lock.sh is missing; reinstall with ./setup" >&2; return 2; }

# gstack_install_move_hint SRC HOST — when HOST's runtime root ROOT is SRC (the
# checkout) or contains it, `./setup --host HOST` would replace the checkout
Expand All @@ -53,15 +55,17 @@ gstack_install_registry_file() {
printf '%s/installs.tsv\n' "${GSTACK_STATE_ROOT:?gstack-install-registry: GSTACK_STATE_ROOT is not set}"
}

# _gstack_registry_lock — take the registry lock (mkdir is atomic). A lock whose
# owner pid is gone is stale and is broken. Gives up after ~10 s.
# _gstack_registry_lock — take the registry lock (gstack_lock_take, which
# creates the pid file with O_EXCL; mkdir's exit status is not a lock on Ubuntu
# 26.04's uutils mkdir). A lock whose owner pid is gone is stale and is broken.
# Gives up after ~10 s.
_gstack_registry_lock() {
local file lock tries owner
file="$(gstack_install_registry_file)" || return 1
lock="$file.lock"
mkdir -p "$(dirname "$file")" || return 1
tries=0
while ! mkdir "$lock" 2>/dev/null; do
while ! gstack_lock_take "$lock" pid "$$"; do
owner="$(cat "$lock/pid" 2>/dev/null || true)"
if [ -n "$owner" ] && ! kill -0 "$owner" 2>/dev/null; then
rm -rf "$lock"
Expand All @@ -74,7 +78,6 @@ _gstack_registry_lock() {
fi
sleep 0.1
done
echo "$$" > "$lock/pid"
}

_gstack_registry_unlock() {
Expand Down
Loading
Loading