mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-28 10:54:30 +01:00
train S3: merge #3738 at 022210eed2 v1.92.69.0
This commit is contained in:
commit
5186dc85fa
8 files changed
+288
-84
No files matched your search
+121
-25
@@ -25,6 +25,10 @@
|
||||
# `completed` events in projects/*/timeline.jsonl.
|
||||
# Idempotent; backfilled rows count for tenure only.
|
||||
#
|
||||
# Writers (--derive, --log-session, --reconcile, --migrate) hold
|
||||
# developer-profile.json.lock for their read-modify-write and exit 5 when
|
||||
# another writer keeps it past the give-up window (#3437).
|
||||
#
|
||||
# Profile file: ~/.gstack/developer-profile.json (unified schema — see
|
||||
# docs/designs/PLAN_TUNING_V0.md). Event file: ~/.gstack/projects/{SLUG}/
|
||||
# question-events.jsonl.
|
||||
@@ -37,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 /*|[A-Za-z]:[\\/]*) ;; *) 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 —
|
||||
@@ -52,10 +60,100 @@ SLUG="${SLUG:-unknown}"
|
||||
CMD="${1:---read}"
|
||||
shift || true
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# Profile lock (#3437). Every subcommand that rewrites the profile reads,
|
||||
# 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
|
||||
# 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
|
||||
# (<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
|
||||
# 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.
|
||||
# -----------------------------------------------------------------------
|
||||
LOCK_DIR="$PROFILE_FILE.lock"
|
||||
TAKEOVER_DIR="$LOCK_DIR.takeover"
|
||||
LOCK_TOKEN=""
|
||||
HOLDS_TAKEOVER=""
|
||||
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 "$LOCK_TOKEN" ] && [ "$(cat "$LOCK_DIR/owner" 2>/dev/null || true)" = "$LOCK_TOKEN" ]; then
|
||||
rm -rf "$LOCK_DIR"
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
|
||||
dir_age_s() {
|
||||
local mtime
|
||||
# GNU stat first: BSD-style stat -f on Linux prints a filesystem block.
|
||||
mtime=$(stat -c %Y "$1" 2>/dev/null || stat -f %m "$1" 2>/dev/null || echo "")
|
||||
case "$mtime" in ""|*[!0-9]*) echo 0 ;; *) echo $(( $(date +%s) - mtime )) ;; esac
|
||||
}
|
||||
|
||||
lock_abandoned() {
|
||||
if [ -n "$1" ]; then
|
||||
! kill -0 "${1%%-*}" 2>/dev/null || [ "$(dir_age_s "$LOCK_DIR")" -gt 300 ]
|
||||
else
|
||||
[ "$(dir_age_s "$LOCK_DIR")" -gt 30 ]
|
||||
fi
|
||||
}
|
||||
|
||||
acquire_lock() {
|
||||
if [ -n "$LOCK_TOKEN" ]; then return 0; fi
|
||||
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 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
|
||||
# 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
|
||||
echo "gstack-developer-profile: cannot create $LOCK_DIR (is $GSTACK_HOME writable?), so nothing was written (exit 5)." >&2
|
||||
exit 5
|
||||
fi
|
||||
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
|
||||
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
|
||||
HOLDS_TAKEOVER=""
|
||||
if [ ! -e "$LOCK_DIR" ]; then continue; fi
|
||||
fi
|
||||
fi
|
||||
if [ "$waited_ms" -ge "$give_up_ms" ] || [ $(( SECONDS - started )) -gt $(( give_up_ms / 1000 )) ]; then
|
||||
echo "gstack-developer-profile: another writer held $LOCK_DIR for ${give_up_ms} ms, so nothing was written (exit 5)." >&2
|
||||
exit 5
|
||||
fi
|
||||
sleep 0.05
|
||||
waited_ms=$(( waited_ms + 50 ))
|
||||
done
|
||||
printf '%s\n' "$token" > "$LOCK_DIR/owner"
|
||||
LOCK_TOKEN="$token"
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# Migration: builder-profile.jsonl → developer-profile.json
|
||||
# -----------------------------------------------------------------------
|
||||
do_migrate() {
|
||||
acquire_lock
|
||||
if [ ! -f "$LEGACY_FILE" ]; then
|
||||
echo "MIGRATE: no legacy file to migrate"
|
||||
return 0
|
||||
@@ -68,9 +166,7 @@ do_migrate() {
|
||||
fi
|
||||
|
||||
# Run migration in a temp file, then atomic rename.
|
||||
local TMPOUT
|
||||
TMPOUT=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX")
|
||||
trap 'rm -f "$TMPOUT"' EXIT
|
||||
PROFILE_TMP=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX")
|
||||
|
||||
cat "$LEGACY_FILE" | bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e "
|
||||
const lines = (await Bun.stdin.text()).trim().split('\n').filter(Boolean);
|
||||
@@ -113,11 +209,11 @@ do_migrate() {
|
||||
schema_version: 1,
|
||||
};
|
||||
console.log(JSON.stringify(profile, null, 2));
|
||||
" > "$TMPOUT"
|
||||
" > "$PROFILE_TMP"
|
||||
|
||||
# Atomic rename.
|
||||
mv "$TMPOUT" "$PROFILE_FILE"
|
||||
trap - EXIT
|
||||
mv "$PROFILE_TMP" "$PROFILE_FILE"
|
||||
PROFILE_TMP=""
|
||||
|
||||
# gbrain-sync: enqueue the migrated file for cross-machine sync (no-op if off).
|
||||
SCRIPT_DIR_E="$(cd "$(dirname "$0")" && pwd)"
|
||||
@@ -205,13 +301,12 @@ do_log_session() {
|
||||
*) echo "gstack-developer-profile: bun printed no verdict while validating the session; nothing was logged" >&2; return 1 ;;
|
||||
esac
|
||||
|
||||
acquire_lock
|
||||
ensure_profile
|
||||
|
||||
local TMPOUT
|
||||
TMPOUT=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX")
|
||||
trap 'rm -f "$TMPOUT"' EXIT
|
||||
PROFILE_TMP=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX")
|
||||
|
||||
PROFILE_FILE_PATH="$PROFILE_FILE" RECORD_INPUT="$INPUT" TMPOUT_PATH="$TMPOUT" bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e "
|
||||
PROFILE_FILE_PATH="$PROFILE_FILE" RECORD_INPUT="$INPUT" TMPOUT_PATH="$PROFILE_TMP" bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e "
|
||||
const fs = require('fs');
|
||||
const entry = JSON.parse(process.env.RECORD_INPUT);
|
||||
if (!entry.ts) entry.ts = new Date().toISOString();
|
||||
@@ -238,8 +333,8 @@ do_log_session() {
|
||||
fs.writeFileSync(process.env.TMPOUT_PATH, JSON.stringify(profile, null, 2));
|
||||
"
|
||||
|
||||
mv "$TMPOUT" "$PROFILE_FILE"
|
||||
trap - EXIT
|
||||
mv "$PROFILE_TMP" "$PROFILE_FILE"
|
||||
PROFILE_TMP=""
|
||||
"$SCRIPT_DIR/gstack-brain-enqueue" "developer-profile.json" 2>/dev/null &
|
||||
}
|
||||
|
||||
@@ -252,13 +347,12 @@ do_log_session() {
|
||||
# rewritten only when something was added.
|
||||
# -----------------------------------------------------------------------
|
||||
do_reconcile() {
|
||||
acquire_lock
|
||||
ensure_profile
|
||||
|
||||
local TMPOUT
|
||||
TMPOUT=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX")
|
||||
trap 'rm -f "$TMPOUT"' EXIT
|
||||
PROFILE_TMP=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX")
|
||||
|
||||
PROFILE_FILE_PATH="$PROFILE_FILE" PROJECTS_DIR="$GSTACK_HOME/projects" TMPOUT_PATH="$TMPOUT" bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e "
|
||||
PROFILE_FILE_PATH="$PROFILE_FILE" PROJECTS_DIR="$GSTACK_HOME/projects" TMPOUT_PATH="$PROFILE_TMP" bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e "
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const profile = JSON.parse(fs.readFileSync(process.env.PROFILE_FILE_PATH, 'utf-8'));
|
||||
@@ -313,13 +407,13 @@ do_reconcile() {
|
||||
console.log('RECONCILE: ' + added + ' backfilled from ' + runs.length + ' office-hours run(s)');
|
||||
"
|
||||
|
||||
if [ -s "$TMPOUT" ]; then
|
||||
mv "$TMPOUT" "$PROFILE_FILE"
|
||||
trap - EXIT
|
||||
if [ -s "$PROFILE_TMP" ]; then
|
||||
mv "$PROFILE_TMP" "$PROFILE_FILE"
|
||||
PROFILE_TMP=""
|
||||
"$SCRIPT_DIR/gstack-brain-enqueue" "developer-profile.json" 2>/dev/null &
|
||||
else
|
||||
rm -f "$TMPOUT"
|
||||
trap - EXIT
|
||||
rm -f "$PROFILE_TMP"
|
||||
PROFILE_TMP=""
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -506,6 +600,7 @@ function accumulateSignals(lines, QUESTIONS, SIGNAL_MAP, dims) {
|
||||
# Derive: recompute inferred dimensions from question-events.jsonl
|
||||
# -----------------------------------------------------------------------
|
||||
do_derive() {
|
||||
acquire_lock
|
||||
ensure_profile
|
||||
local EVENTS="$GSTACK_HOME/projects/$SLUG/question-log.jsonl"
|
||||
local REGISTRY="$ROOT_DIR/scripts/question-registry.ts"
|
||||
@@ -515,8 +610,9 @@ do_derive() {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PROFILE_TMP=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX")
|
||||
cd "$ROOT_DIR"
|
||||
PROFILE_FILE_PATH="$PROFILE_FILE" EVENTS_PATH="$EVENTS" bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e "
|
||||
PROFILE_FILE_PATH="$PROFILE_FILE" EVENTS_PATH="$EVENTS" TMPOUT_PATH="$PROFILE_TMP" bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e "
|
||||
import('./scripts/question-registry.ts').then(async (regmod) => {
|
||||
const sigmod = await import('./scripts/psychographic-signals.ts');
|
||||
const fs = require('fs');
|
||||
@@ -568,12 +664,12 @@ do_derive() {
|
||||
profile.gap = gap;
|
||||
profile.derived_at = new Date().toISOString();
|
||||
|
||||
const tmp = profilePath + '.tmp';
|
||||
fs.writeFileSync(tmp, JSON.stringify(profile, null, 2));
|
||||
fs.renameSync(tmp, profilePath);
|
||||
fs.writeFileSync(process.env.TMPOUT_PATH, JSON.stringify(profile, null, 2));
|
||||
console.log('DERIVE: ok — ' + count + ' events, ' + skills.size + ' skills, ' + qids.size + ' questions');
|
||||
}).catch(err => { console.error('DERIVE:', err.message); process.exit(1); });
|
||||
"
|
||||
mv "$PROFILE_TMP" "$PROFILE_FILE"
|
||||
PROFILE_TMP=""
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user