Files
loki/scripts/db/sync-agent-projects-conf.sh
CatoandClaude Opus 5 58174f45ef fix(hardcoded): give every duplicated constant one home (#758)
* fix(hardcoded): give every duplicated constant one home

A sweep of all 44 repos' default branches for literals that duplicate an
existing source of truth. Loki's share, in four groups.

WRONG, and live on the public footer. src/config/fleet-site-copy.ts still
called the Zürich clinic "VitaReBa", a name retired on 2026-09-10 — its own
title says "Vita · Metabolische Psychiatrie & Systemische Longevity". The
generated fleet-sites.ts carried it onto loki.orangecat.ch, where it was
serving until this commit. Fixed at the copy file, which is the one place that
owns a site's own words, and the footer regenerated from it.

WOULD HAVE WRITTEN STALE DATA. scripts/seed-fleet-site-urls.ts kept a hand-typed
slug → URL map "read 2026-08-06". Two of its fifteen entries pointed at hosts
retired since (aoz-wohnen.orangecat.ch, revampit.orangecat.ch), so running it
would have filled empty live_url columns with addresses that only 308. It now
derives the map from apps.conf — the same register the footer is generated from
— which also fixes the six live sites the hand list never had. Verified: 19
sites, zero retired hosts.

THE BOX ADDRESS, in seven places. scripts/hetzner/_box-env.sh has been the SSOT
for it all along and sixteen scripts source it; four did not, and the shared
deploy workflow — reused by ~20 repos — had the IP inline twice, so moving the
box meant twenty edits. The workflow now reads the org-level Actions variable
HETZNER_IP and fails loudly if it is unset, rather than ssh-ing to an empty
string. loki's own deploy.yml had `vars.HETZNER_IP || '<literal>'`, a fallback
that guaranteed the variable was never needed and so never noticed if wrong.

ONE LAPTOP, baked into shipped code. site-cd-local.ts listed /home/g/.ssh and
/home/ubuntu/.ssh as deploy-key candidates beside os.homedir() — which already
IS those paths, since loki-app runs as `ubuntu` and a laptop runs as its owner;
both spelled one homedir for one named person. install-push-deploy.sh wired
hooks to /home/g/dev/loki, link-prod-runtime.ts to /home/g/dev/bitbaum, and
scripts/fleet printed an install line only true on one machine: all now DEV_ROOT
or the script's own resolved path.

The two scripts in scripts/local are INSTALLED BY COPY into ~/.local/bin and run
there by systemd timers, so they resolve _box-env.sh beside the real file first,
then under DEV_ROOT, and exit loudly if neither exists — a security check that
cannot find its target must not look like one that found nothing.

Deliberately unchanged: the slug alias map (the mechanism that makes a rename
safe), an executed migration's data row, the retired-handle detector's own
handle list, and test fixtures reproducing past incidents.

Verified: fleet-sites-derived in sync (14 sites), bash -n on every edited
script, and the branch's only remaining literals are in scripts/test fixtures.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(push-deploy): the generated hook must not bake one laptop's path

install-push-deploy.sh writes the hook body through an UNQUOTED heredoc, so
$HERE expanded at generation time and the absolute path of one machine was
committed into every repo's .husky/pre-push. kivvi and botsmann both carried it;
anyone cloning those repos got a hook pointing into a stranger's home directory,
and re-running the installer would have put it straight back after those repos
were cleaned.

The hook now resolves ${DEV_ROOT:-$HOME/dev}/loki/... at RUN time, so the
generator stops re-introducing what the cleanup just removed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 02:03:58 -07:00

89 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Regenerate ~/.config/agent-projects.conf from Loki's user_projects
# table — the actual SSOT for project name/dirPath/agent/model prefs.
#
# WHY THIS EXISTS: agent-projects.conf's own header used to claim "the
# daemon syncs this file". No such sync ever existed in the codebase — it
# was aspirational documentation. A rename or retirement in the DB (Control
# page, or scripts/db/retire-stale-projects.ts) silently left this file
# stale, and every local consumer (home/projects.ts dispatch resolution,
# fleet-runner's watcher) kept reading the old name/path until someone
# noticed and hand-edited it. That's how `revampit|/home/g/dev/revampit`
# outlived the project's rename to `evig` by three weeks, and how
# `swiss-longevity-hub`/`truthseeker-tmp` are still in the file today even
# though retire-stale-projects.ts merged them away in the DB weeks ago.
# This script is the real sync. Run it (or let the installed timer run it —
# see install-agent-projects-sync.sh) and the file matches the DB, always.
#
# Runs the query ON THE BOX over SSH rather than connecting to Postgres
# directly: the DB port has been closed to external connections since the
# 2026-07-17 security audit, reachable only from 127.0.0.1 on the box
# itself. That constraint is permanent, not a workaround — don't "fix" this
# by reopening the port.
#
# Usage:
# scripts/db/sync-agent-projects-conf.sh # write ~/.config/agent-projects.conf
# scripts/db/sync-agent-projects-conf.sh --check # exit 1 if the file would change; no write
# AGENT_PROJECTS_CONF=/tmp/x.conf scripts/db/sync-agent-projects-conf.sh # write elsewhere (testing)
set -euo pipefail
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../hetzner/_box-env.sh" # SSOT: BOX_UBUNTU
BOX="${SYNC_AGENT_PROJECTS_BOX:-$BOX_UBUNTU}"
OUT="${AGENT_PROJECTS_CONF:-$HOME/.config/agent-projects.conf}"
CHECK_ONLY=false
[[ "${1:-}" == "--check" ]] && CHECK_ONLY=true
sql_file="$(mktemp)"
tmp_out="$(mktemp)"
trap 'rm -f "$sql_file" "$tmp_out"' EXIT
cat > "$sql_file" <<'SQL'
select up.name || '|' || up.dir_path || '|' || coalesce(up.agent_pref, '') || '|' || coalesce(up.model_pref, '')
from user_projects up
where up.is_active = true and up.dir_path is not null
order by up.position, up.name;
SQL
rows="$(ssh -o ConnectTimeout=10 "$BOX" \
'sudo bash -c "set -a; . /opt/loki/app/.env; set +a; psql -At \"\$DATABASE_URL\""' \
< "$sql_file" 2>/dev/null)" || {
echo "sync-agent-projects-conf: could not reach $BOX or query failed — leaving $OUT untouched" >&2
exit 1
}
if [[ -z "$rows" ]]; then
echo "sync-agent-projects-conf: empty result from DB query — refusing to overwrite $OUT" >&2
exit 1
fi
{
echo "# Generated by loki/scripts/db/sync-agent-projects-conf.sh from the"
echo "# Loki user_projects table — the SSOT. Do not hand-edit; rename or"
echo "# retire projects via the Control page, then re-run the sync."
echo "# Format: tab_name|/absolute/path/to/project|agent|model"
echo "$rows"
} > "$tmp_out"
missing=0
while IFS='|' read -r name dir _rest; do
[[ -z "$name" || "$name" == \#* ]] && continue
if [[ -n "$dir" && ! -d "$dir" ]]; then
echo "⚠ $name → $dir does not exist on disk (stale registry entry?)" >&2
missing=$((missing + 1))
fi
done < "$tmp_out"
if diff -q "$tmp_out" "$OUT" >/dev/null 2>&1; then
echo "up to date ($(grep -vc '^#' "$tmp_out") projects, $missing missing on disk)"
exit 0
fi
if $CHECK_ONLY; then
echo "would rewrite $OUT ($(grep -vc '^#' "$tmp_out") projects, $missing missing on disk)"
exit 1
fi
chmod 600 "$tmp_out"
mv "$tmp_out" "$OUT"
echo "wrote $OUT ($(grep -vc '^#' "$OUT") projects, $missing missing on disk)"