session-status.sh

Session status report

Tier 2engineeringWarns only

Generated files older than their sources, so you work from the previous build without knowing. Also reports problems that cannot block offline work.

SESSION-STATUS.SHPRINTS, DOES NOT BLOCK GENERATED FILES SOURCES STALE BUILD
Add this to your project
Add "Session status report" from ntent to this repo.

Fetch https://ntent.app/r/f/session-status as plain text. Write it verbatim to scripts/session-status.sh. Then make it executable.

Then check it: It prints when someone opens the repo, which is the moment they can act on it. Touch a token source without rebuilding and it tells you that you are looking at the previous build.

Reads https://ntent.app/r/f/session-status

Read the code
session-status.shscripts/session-status.sh · make it executable
257 lines
#!/usr/bin/env bash
#
# Session start banner. Runs when a coding session opens, and by hand any time.
#
# Two jobs. It orients whoever just sat down: are you behind, what is running,
# where do you pick up. And it is where the checks that cannot be commit gates
# get their audience, because a check that needs credentials or the network has
# to nag somewhere, and the moment someone opens the repo is the moment they can
# act on it.
#
# THREE RULES.
#
# 1. Never block on the network. The git sync line reads local refs only, then
#    fetches in the background so the NEXT session is accurate. A banner that
#    takes four seconds is a banner people disable.
#
# 2. Silent unless it matters. Lines that have nothing to say print nothing. A
#    banner that always prints eight lines gets skimmed and then ignored, which
#    means the one time it says something urgent, nobody reads it.
#
# 3. Under a second, always. Everything here is a local file read or a git
#    plumbing command.
#
# Wire it into your agent's session-start hook, and add it to the README so
# people can run it directly:  bash scripts/session-status.sh

set -uo pipefail
cd "$(git rev-parse --show-toplevel 2>/dev/null || echo .)" || exit 0

# ---------------------------------------------------- THE REPO'S TOOLING ---
# Print only commands that exist here. A banner that says `pnpm run check` in a
# repo with no such script, or `cp .env.example .env.local` with no example file,
# sends someone to a "command not found" and costs the rest of it its credit.
# (Same block in doctor.sh and session-status.sh: each stays a single file.)

# The package manager: the packageManager field first, then the lockfile.
pm=""
if [ -f package.json ]; then
  pm=$(sed -n 's/.*"packageManager"[[:space:]]*:[[:space:]]*"\([a-z]*\)@.*/\1/p' package.json | head -n 1)
fi
if [ -z "$pm" ]; then
  if   [ -f pnpm-lock.yaml ] || [ -f pnpm-workspace.yaml ]; then pm=pnpm
  elif [ -f yarn.lock ]; then pm=yarn
  elif [ -f bun.lockb ] || [ -f bun.lock ]; then pm=bun
  elif [ -f package-lock.json ]; then pm=npm
  elif [ -f package.json ]; then pm=npm
  fi
fi

# Does package.json define this script? node when there is one, grep when not.
has_script() {
  [ -f package.json ] || return 1
  if command -v node >/dev/null 2>&1; then
    node -e 'try { process.exit(require("./package.json").scripts?.[process.argv[1]] ? 0 : 1) } catch { process.exit(1) }' "$1" 2>/dev/null
  else
    grep -q "\"$1\"[[:space:]]*:" package.json 2>/dev/null
  fi
}

# `pnpm dev`, `yarn dev`, `npm run dev`, `bun run dev`. npm and bun need `run`.
run_cmd() {
  case "$pm" in
    npm|bun) echo "$pm run $1" ;;
    *)       echo "$pm $1" ;;
  esac
}

# Is the ntent hook reached from the hook this repo actually runs? Prints how,
# and prints nothing when it is not. Three ways it is: core.hooksPath is ours;
# a hook in the folder core.hooksPath names (Husky's .husky/pre-commit, a
# .githooks folder) calls scripts/git-hooks/pre-commit; or the Python
# pre-commit framework has a local hook that does.
hook_wired() {
  hp="$(git config core.hooksPath 2>/dev/null)"
  if [ "$hp" = "scripts/git-hooks" ]; then echo "core.hooksPath = scripts/git-hooks"; return 0; fi
  for f in .husky/pre-commit .githooks/pre-commit "${hp:+$hp/pre-commit}" "${hp:+$hp/../pre-commit}" .git/hooks/pre-commit; do
    [ -n "$f" ] && [ -f "$f" ] && grep -q 'scripts/git-hooks/pre-commit' "$f" 2>/dev/null \
      && { echo "chained from $f"; return 0; }
  done
  if [ -f .pre-commit-config.yaml ] && grep -q 'scripts/git-hooks/pre-commit' .pre-commit-config.yaml 2>/dev/null; then
    echo "a local hook in .pre-commit-config.yaml"; return 0
  fi
  return 1
}

# What to say when it is not. Never a command that would switch the repo's own
# hooks off: setting core.hooksPath over Husky's disables Husky.
hook_fix() {
  hp="$(git config core.hooksPath 2>/dev/null)"
  if [ -n "$hp" ]; then
    echo "core.hooksPath is $hp, so add one line to the pre-commit hook there: bash scripts/git-hooks/pre-commit || exit 1"
  elif [ -f .pre-commit-config.yaml ]; then
    echo "add a local hook to .pre-commit-config.yaml whose entry is scripts/git-hooks/pre-commit"
  else
    echo "run: git config core.hooksPath scripts/git-hooks"
  fi
}


# ---------------------------------------------------------------- CONFIG ---
# Build outputs that are generated and not committed, and the source that
# generates them.
#
# The missing case is the loud one and doctor.sh covers it too. STALENESS is the
# half only this script can catch: the file is there, the app starts, everything
# renders, and you are looking at the previous build. Nothing anywhere says so.
# You debug a change you are not running. It costs an hour every time.
#
# One entry per line, three fields separated by | :
#
#   generated output | the source directory that produces it | the command that rebuilds it
#
# Point the second field at the source directory, not at the repo root. It is
# walked on every session start and rule 3 is one second for the whole banner.
#
# An empty list is fine. Nothing here prints when there is nothing in it.
GENERATED=(
  # "src/styles/tokens.css|tokens/src|pnpm tokens:build"
  # "src/generated/schema.ts|prisma|pnpm codegen"
)

# ------------------------------------------------------- 1. git position ---
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  branch=$(git branch --show-current 2>/dev/null || echo detached)
  if upstream=$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null); then
    behind=$(git rev-list --count "HEAD..$upstream" 2>/dev/null || echo 0)
    ahead=$(git rev-list --count "$upstream..HEAD" 2>/dev/null || echo 0)
    if   [ "$behind" -gt 0 ] && [ "$ahead" -gt 0 ]; then
      echo "git:   diverged from $upstream. $behind behind, $ahead ahead"
    elif [ "$behind" -gt 0 ]; then
      echo "git:   $behind behind $upstream. Pull before you start"
    elif [ "$ahead" -gt 0 ]; then
      echo "git:   $ahead unpushed commit(s) on $branch"
    fi
    # Silent when in sync. Rule 2.
  else
    echo "git:   $branch has no upstream branch set"
  fi
  dirty=$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')
  [ "$dirty" -gt 0 ] && echo "git:   $dirty uncommitted change(s)"

  # Refresh origin for next time. Never blocks.
  ( nohup git fetch --quiet >/dev/null 2>&1 & ) >/dev/null 2>&1
fi

# ----------------------------------------------------- 2. setup problems ---
# The two that silently disable everything else, so they are worth one line each.

# Only when the hook is in this repo, and wired counts however it is wired: a
# line in Husky's pre-commit calling it is as good as core.hooksPath being ours,
# and the old fix here, repointing core.hooksPath, would have turned Husky off.
if [ -f scripts/git-hooks/pre-commit ] && ! hook_wired >/dev/null; then
  echo "setup: git hooks are NOT wired, so no checks run on commit."
  echo "       fix: $(hook_fix)"
fi

if [ -f .env.example ] && [ ! -f .env.local ] && [ ! -f .env ]; then
  echo "setup: no .env.local. Run: cp .env.example .env.local"
fi

# Generated artefacts: missing, or older than the source that produces them.
if [ "${#GENERATED[@]}" -gt 0 ]; then
  for entry in "${GENERATED[@]}"; do
    IFS='|' read -r gen_out gen_src gen_cmd <<EOF
$entry
EOF
    if [ ! -f "$gen_out" ]; then
      echo "setup: $gen_out is not built. The app will start and look BROKEN rather"
      echo "       than unbuilt, so you will debug the wrong thing. fix: $gen_cmd"
    elif [ -n "$(find "$gen_src" -newer "$gen_out" -type f -print -quit 2>/dev/null)" ]; then
      # -print -quit stops at the first hit, so this stays a partial walk however
      # large the source tree is.
      echo "setup: a file in $gen_src is newer than $gen_out. You are looking at the"
      echo "       PREVIOUS build and nothing else will tell you. fix: $gen_cmd"
    fi
  done
fi

# ---------------------------------------------------------- 3. the nags ----
# Checks that cannot be commit gates because the person committing may not be
# able to satisfy them. See chapter 2 on the gate-versus-nag distinction.

# Contract coverage, if there is a contract. Offline and instant.
if [ -f contract.json ] && [ -f scripts/check-contract-coverage.mjs ]; then
  cov=$(node scripts/check-contract-coverage.mjs 2>/dev/null | grep -o 'Overall type-layer coverage: [0-9]*%' || true)
  [ -n "$cov" ] && echo "build: ${cov#Overall type-layer coverage: } of the contract is implemented  (node scripts/check-contract-coverage.mjs for the breakdown)"
fi

# Notes that point at a file that is no longer there. Paths only and no git, so
# it stays inside rule 3; the full run also follows renames and checks names.
if [ -f scripts/check-decision-refs.mjs ]; then
  stale=$(node scripts/check-decision-refs.mjs --count --fast 2>/dev/null || echo 0)
  [ "${stale:-0}" -gt 0 ] 2>/dev/null && echo "notes: $stale path(s) in the decision log or session notes point at nothing  (node scripts/check-decision-refs.mjs says where they went)"
fi

# Stale build-log stubs. A queue that grows forever is a queue nobody drains,
# so say the number out loud once it stops being small.
# One inbox per committer, all of them local to this checkout (they are
# gitignored), so the count is the sum. Draining is each author's own job.
if ls docs/build-log/_pending-*.md >/dev/null 2>&1; then
  pending=$(cat docs/build-log/_pending-*.md 2>/dev/null | grep -c '^- \[ \]')
  [ "${pending:-0}" -gt 10 ] && echo "log:   $pending undrained build-log stub(s). Worth 10 minutes before they go cold"
fi

# Rapid mode left on. It is meant to be temporary, and forgetting it on is how
# the feed quietly stops.
if [ "$(git config --bool myproject.rapid 2>/dev/null)" = "true" ]; then
  spooled=$(wc -l < "$(git rev-parse --git-dir)/rapid-skipped-stubs" 2>/dev/null | tr -d ' ' || echo 0)
  echo "log:   rapid mode is ON, $spooled stub(s) spooled. Drain and: git config --unset myproject.rapid"
fi

# Flags past their removal date. Cheap to compute, and flag debt is the quiet
# killer of a long-lived SaaS codebase.
if [ -f src/lib/flags.json ]; then
  today=$(date +%Y-%m-%d)
  expired=$(node -e "
    const f=require('./src/lib/flags.json');
    const n=Object.entries(f).filter(([,v])=>v.removeBy && v.removeBy < '$today').map(([k])=>k);
    if(n.length) console.log(n.length + ' flag(s) past removal date: ' + n.join(', '));
  " 2>/dev/null || true)
  [ -n "$expired" ] && echo "flags: $expired"
fi

# ------------------------------------------------------- 4. how to run -----
# Only what exists. Each line is a script this repo defines, run the way its own
# package manager runs it; a doctor with no package.json script is the file.
run_lines=()
has_script dev && run_lines+=("$(printf '%-22s %s' "$(run_cmd dev)" 'the app')")
has_script check && run_lines+=("$(printf '%-22s %s' "$(run_cmd check)" 'every gate, the way CI runs them')")
if has_script doctor; then
  run_lines+=("$(printf '%-22s %s' "$(run_cmd doctor)" 'what your machine is missing')")
elif [ -f scripts/doctor.sh ]; then
  run_lines+=("$(printf '%-22s %s' 'bash scripts/doctor.sh' 'what your machine is missing')")
fi
if [ "${#run_lines[@]}" -gt 0 ]; then
  first=1
  for l in "${run_lines[@]}"; do
    if [ "$first" = 1 ]; then echo "run:   $l"; first=0; else echo "       $l"; fi
  done
fi

# ------------------------------------------------------- 5. where to pick --
# Print the resume pointer, if there is one. Everything between the markers goes
# into the session verbatim, so the file has a hard size cap. See chapter 7.
# No markers means everything above the archive.
for f in STATE.md docs/PROGRESS.md; do
  [ -f "$f" ] || continue
  if grep -q '<!-- RESUME:START -->' "$f"; then
    awk '/<!-- RESUME:START -->/{flag=1;next}/<!-- RESUME:END -->/{flag=0}flag' "$f" | head -40
  else
    awk '/^#+[[:space:]]+Archive/{exit}{print}' "$f" | head -40
  fi
  break
done

exit 0

How to tell it works: It prints when someone opens the repo, which is the moment they can act on it. Touch a token source without rebuilding and it tells you that you are looking at the previous build.

Included in