mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-21 15:43:24 +01:00
v1.92.72.0 stamp: merge main 69c14b17d5 into #3747 (land)
This commit is contained in:
commit
1c818038e7
48 files changed
+1251
-278
No files matched your search
+12
-2
@@ -1,14 +1,16 @@
|
||||
#!/usr/bin/env -S bun --no-env-file --config=/dev/null
|
||||
/**
|
||||
* gstack-checks — the build-and-verify loop's checks for /verify. Logic: lib/checks.ts.
|
||||
* gstack-checks — the build-and-verify loop's checks for /verify, and /health's tool detection. Logic: lib/checks.ts.
|
||||
*/
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { cancelActive } from '../lib/pregate/supervise';
|
||||
import { MAX_FAIL_LINES, TESTS, isTrusted, ledgerPath, load, nothingToRun, readLedger, recordTrust, runChecks, status, type Loaded } from '../lib/checks';
|
||||
import { MAX_FAIL_LINES, TESTS, healthDimensions, isTrusted, ledgerPath, load, nothingToRun, readLedger, recordTrust, runChecks, status, type Loaded } from '../lib/checks';
|
||||
|
||||
const USAGE = `Usage: gstack-checks <detect|trust|run|status> [options]
|
||||
|
||||
detect list the checks run would execute and whether they are trusted (reads files only)
|
||||
detect --for health /health's dimensions: DIMENSION <name> <command> (<source>), or DIMENSION <name> none
|
||||
(<reason>); a CLAUDE.md Health Stack section wins (reads files and PATH only)
|
||||
trust trust the current command set for this repo (checks, the pregate runner and its
|
||||
selection manifest commands); any change to one of them needs trust again
|
||||
run [--only a,b] [--allow-repo-commands] [--timeout <s>]
|
||||
@@ -43,6 +45,14 @@ async function main(argv: string[]): Promise<number> {
|
||||
const [verb, ...args] = argv;
|
||||
if (!verb || verb === '--help' || verb === '-h') { process.stdout.write(USAGE); return verb ? 0 : 64; }
|
||||
if (!['detect', 'trust', 'run', 'status'].includes(verb)) { process.stderr.write(USAGE); return 64; }
|
||||
const mode = flag(args, '--for');
|
||||
if (args.includes('--for')) {
|
||||
if (verb !== 'detect' || mode !== 'health') { process.stderr.write(USAGE); return 64; }
|
||||
const d = healthDimensions(process.cwd());
|
||||
for (const c of d.checks) process.stdout.write(`DIMENSION ${c.dimension} ${c.command} (${c.source})\n`);
|
||||
for (const m of d.missing) process.stdout.write(`DIMENSION ${m.dimension} none (${m.reason})\n`);
|
||||
return 0;
|
||||
}
|
||||
const root = repoRoot();
|
||||
if (!root) { process.stderr.write('gstack-checks: not inside a git repository\n'); return 3; }
|
||||
const timeoutS = Number(flag(args, '--timeout') ?? 600);
|
||||
|
||||
+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=""
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
|
||||
@@ -8,13 +8,15 @@ DO_PREFIX="$2"
|
||||
|
||||
# Normalize prefix arg
|
||||
case "$DO_PREFIX" in true|1) DO_PREFIX=1 ;; *) DO_PREFIX=0 ;; esac
|
||||
# Names are read with a bash builtin loop: no process per skill (#2120).
|
||||
. "$(dirname "$0")/gstack-skill-name.sh"
|
||||
|
||||
PATCHED=0
|
||||
for skill_dir in "$GSTACK_DIR"/*/; do
|
||||
[ -f "$skill_dir/SKILL.md" ] || continue
|
||||
dir_name="$(basename "$skill_dir")"
|
||||
dir_name=${skill_dir%/}; dir_name=${dir_name##*/}
|
||||
[ "$dir_name" = "node_modules" ] && continue
|
||||
cur=$(grep -m1 '^name:' "$skill_dir/SKILL.md" 2>/dev/null | sed 's/^name:[[:space:]]*//' | tr -d '[:space:]' || true)
|
||||
gstack_skill_md_name "$skill_dir/SKILL.md"; cur=$GSTACK_SKILL_NAME
|
||||
[ -z "$cur" ] && continue
|
||||
[ "$cur" = "gstack" ] && continue # never prefix root skill
|
||||
if [ "$DO_PREFIX" -eq 1 ]; then
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
# shellcheck shell=bash
|
||||
# gstack-skill-name.sh: sourced helper (bash 3.2). gstack_skill_md_name FILE
|
||||
# sets GSTACK_SKILL_NAME to the first column-0 "name:" line of FILE with every
|
||||
# whitespace character removed (CR included), or to "" when there is none or
|
||||
# FILE cannot be read. Always returns 0.
|
||||
#
|
||||
# It starts no process: no subshell, pipe or command substitution, and callers
|
||||
# must not wrap it in $( ). Setup reads ~55 names per run, and the
|
||||
# grep | sed | tr pipeline it replaces deadlocked MSYS2's fork() under x64
|
||||
# emulation on Windows ARM64 (#2120).
|
||||
gstack_skill_md_name() {
|
||||
GSTACK_SKILL_NAME=
|
||||
[ -f "$1" ] && [ -r "$1" ] || return 0
|
||||
local _gsn_line
|
||||
while IFS= read -r _gsn_line || [ -n "$_gsn_line" ]; do
|
||||
case "$_gsn_line" in
|
||||
name:*) _gsn_line=${_gsn_line#name:}; GSTACK_SKILL_NAME=${_gsn_line//[[:space:]]/}; return 0 ;;
|
||||
esac
|
||||
done < "$1"
|
||||
return 0
|
||||
}
|
||||
@@ -60,6 +60,9 @@ GSTACK_DIR="${GSTACK_DIR:-$(cd "$(dirname "$0")/.." && pwd)}"
|
||||
STATE_DIR="$HOME/.gstack"
|
||||
_STATE_DOC="https://github.com/garrytan/gstack/blob/main/docs/state-root.md"
|
||||
. "$(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 ($_STATE_DOC)" >&2; exit 1; }
|
||||
# Skill names without a process per skill (#2120); an older copy lacking the helper keeps the pipeline.
|
||||
. "$(dirname "$0")/gstack-skill-name.sh" 2>/dev/null \
|
||||
|| gstack_skill_md_name() { GSTACK_SKILL_NAME=$(grep -m1 '^name:' "$1" 2>/dev/null | sed 's/^name:[[:space:]]*//' | tr -d '[:space:]' || true); }
|
||||
gstack_state_root_select
|
||||
RESOLVED_ROOT="$_gstack_sr_root"
|
||||
RESOLVED_VAR="$_gstack_sr_var"
|
||||
@@ -290,8 +293,8 @@ _INVENTORY=" _gstack-command connect-chrome gstack-connect-chrome "
|
||||
if [ -d "$GSTACK_DIR" ]; then
|
||||
for _SRC in "$GSTACK_DIR"/*/; do
|
||||
[ -f "$_SRC/SKILL.md" ] || continue
|
||||
_SRC_NAME="$(basename "$_SRC")"
|
||||
_FM_NAME=$(grep -m1 '^name:' "$_SRC/SKILL.md" 2>/dev/null | sed 's/^name:[[:space:]]*//' | tr -d '[:space:]' || true)
|
||||
_SRC_NAME=${_SRC%/}; _SRC_NAME=${_SRC_NAME##*/}
|
||||
gstack_skill_md_name "$_SRC/SKILL.md"; _FM_NAME=$GSTACK_SKILL_NAME
|
||||
for _N in "$_SRC_NAME" "$_FM_NAME"; do
|
||||
[ -n "$_N" ] || continue
|
||||
case "$_INVENTORY" in *" $_N "*) ;; *) _INVENTORY="$_INVENTORY$_N gstack-$_N " ;; esac
|
||||
|
||||
Reference in new issue
Block a user