diff --git a/.claude/CLAUDE.md b/.claude/CLAUDE.md index 9c873a07..cc4abddf 100755 --- a/.claude/CLAUDE.md +++ b/.claude/CLAUDE.md @@ -24,7 +24,7 @@ Violations cause state drift, which causes future applies to break or silently revert changes. ## Instructions -- **"remember X"**: store to the remote claude-memory store via the **`homelab memory` CLI**: `homelab memory store "content" --category facts --tags "tag1,tag2"` (also `recall "query"` / `update ` / `list` / `delete `). For shared knowledge, also update the relevant CLAUDE.md / `AGENTS.md`. (Supersedes the old `memory-tool` CLI **and** the claude-memory MCP — both retired 2026-06-21; the homelab CLI hits the same remote HTTP API. Recall also runs automatically each turn via a UserPromptSubmit hook.) +- **"remember X"**: Use `memory-tool store "content" --category facts --tags "tag1,tag2"` (via exec) for persistent cross-session memory. Also update this file + `AGENTS.md` (if shared knowledge), commit with `[ci skip]`. To recall: `memory-tool recall "query"`. To list: `memory-tool list`. To delete: `memory-tool delete `. The native `memory_search` and `memory_get` tools are also available for searching indexed memory files. For **storing** new memories, always use the `memory-tool` CLI via exec. - **Apply**: Authenticate via `vault login -method=oidc`, then use `scripts/tg` (preferred — handles state decrypt/encrypt) or `terragrunt` directly. `scripts/tg` adds `-auto-approve` for `--non-interactive` applies, and `-lock-timeout` (default `5m`, override via `TG_LOCK_TIMEOUT`) on every state-locking verb (`plan`/`apply`/`destroy`/`refresh`) so a contended state lock **waits** instead of failing instantly with `Error acquiring the state lock`. - **New services need CI/CD** and **monitoring** (Prometheus/Uptime Kuma). CI = a GHA workflow on the repo's GitHub mirror (build + tests off-infra, ADR-0002); Woodpecker gets a deploy-only pipeline — never an in-cluster build. - **New service**: Use `setup-project` skill for full workflow @@ -63,7 +63,7 @@ Violations cause state drift, which causes future applies to break or silently r - **`secret/viktor`** — go-to path for ALL personal secrets (135 keys). Contains every API key, token, password, SSH key, and config from the old terraform.tfvars. Check here first: `vault kv get -field=KEY secret/viktor`. - **Auth**: `vault login -method=oidc` (Authentik SSO) → `~/.vault-token` → read by Vault TF provider. - **Vault stack self-reads**: `data "vault_kv_secret_v2" "vault"` reads its own OIDC creds from `secret/vault`. -- **ESO (External Secrets Operator)**: `stacks/external-secrets/` — chart **2.6.0 / app v2.6.0** (migrated 0.12.1→2.6.0 on 2026-06-22, one minor at a time; helm_release has `atomic=true`). **~104 ExternalSecrets across 73 files**, all on **API version `v1`** (migrated v1beta1→v1 on 2026-06-22 — there is NO v1beta1→v1 conversion webhook, so all CRs were rewritten to v1 on chart 0.16.2 before 0.17 removed v1beta1; see `docs/plans/2026-06-21-eso-0.12-to-2.x-migration-design.md`). Two ClusterSecretStores: `vault-kv` and `vault-database`. (2 pre-existing dead ESs — instagram-poster, payslip-ingest — fail "cannot find secret data" on missing Vault keys, unrelated.) +- **ESO (External Secrets Operator)**: `stacks/external-secrets/` — 43 ExternalSecrets + 9 DB-creds ExternalSecrets. API version `v1beta1`. Two ClusterSecretStores: `vault-kv` and `vault-database`. - **Plan-time pattern**: Former plan-time stacks use `data "kubernetes_secret"` to read ESO-created K8s Secrets at plan time (no Vault dependency). First-apply gotcha: must `terragrunt apply -target=kubernetes_manifest.external_secret` first, then full apply. `count` on resources using secret values fails — remove conditional counts. - **14 hybrid stacks** still keep `data "vault_kv_secret_v2"` for plan-time needs (job commands, Helm templatefile, module inputs). Platform has 48 plan-time refs — no migration possible without restructuring modules. - **Database rotation**: Vault DB engine rotates passwords every 7 days (604800s). MySQL: speedtest, wrongmove, codimd, nextcloud, shlink, grafana, phpipam. PostgreSQL: health, linkwarden, affine, woodpecker, claude_memory, crowdsec, technitium. Excluded: authentik (PgBouncer), root users. **Apps that read a rotated secret only at startup** (env var / initContainer, not a hot-reloaded mount) MUST carry a Reloader annotation (`secret.reloader.stakater.com/reload: `) or they keep the stale password and silently fail DB auth on each rotation until manually restarted — matrix's Synapse `inject-db-password` initContainer hit exactly this (found via Loki 2026-06-05, ~12.9k auth-fail lines/hr); matrix has since migrated to tuwunel (RocksDB, no Postgres) on 2026-06-08 and is no longer in the rotation list above. Technitium uses a password-sync CronJob (every 6h) to push rotated password to the Technitium app config via API, disable SQLite + MySQL logging, check PG plugin is loaded, configure PG query logging (90-day retention), and disable SQLite on secondary/tertiary instances. @@ -202,7 +202,7 @@ the workflow's built-in `GITHUB_TOKEN` (`packages: write`). - **Critical path services scaled to 3**: Traefik, Authentik, CrowdSec LAPI, PgBouncer, Cloudflared. - **PDBs**: minAvailable=2 on Traefik and Authentik. - **Fallback proxies**: basicAuth when Authentik is down, fail-open when poison-fountain is down. -- **CrowdSec enforcement is out-of-band** (no Traefik plugin/middleware — the dead Yaegi `crowdsec-bouncer-traefik-plugin` was removed on Traefik 3.7.5): banned IPs are dropped **in-kernel via nftables** by the `cs-firewall-bouncer` DaemonSet on **direct** hosts (drops in BOTH the `input` and `forward` hooks — Traefik is ETP=Local so client traffic is DNAT'd to the pod via `forward`; pulls ALL decisions incl. the ~31k CAPI blocklist), and **blocked at the Cloudflare edge** for **proxied** hosts (one `crowdsec_ban` Rules List + a zone WAF block rule, fed by the `crowdsec-cf-sync` CronJob in `rybbit` ns every 2 min — excludes CAPI). Zero per-request latency; **fails open** (LAPI down → no new bans, existing drops persist, legit traffic never blocked). Whitelist covers RFC1918 + tailnet + internal CIDRs. Full as-built: `docs/architecture/security.md`. +- **CrowdSec bouncer**: graceful degradation mode (fail-open on error). - **Rate limiting**: Return 429 (not 503). Per-service tuning via dedicated middleware + `skip_default_rate_limit` (default 10/s burst 50): Immich 1000/20000, ActualBudget 50/300 (app boot = ~70 parallel revalidations). - **Retry middleware**: 2 attempts, 100ms — in default ingress chain. - **Entrypoint transport timeouts** (`websecure` `respondingTimeouts`): `writeTimeout=0` (unlimited download duration), `readTimeout=3600s` (uploads ≤1h), `idleTimeout=600s`. These are **HARD total-duration caps**, not nginx-style per-read idle timeouts — a finite `writeTimeout` truncates *any* large download at that wall-clock mark (a prior `writeTimeout=60s` silently cut Immich videos at 60s). **Do NOT re-tighten `writeTimeout`**; keep `readTimeout` finite (slow-loris backstop) but ≥ longest expected upload. Full rationale: `docs/architecture/networking.md` → "Entrypoint Transport Timeouts". @@ -216,7 +216,7 @@ the workflow's built-in `GITHUB_TOKEN` (`packages: write`). |---------|--------------------------| | Nextcloud | MaxRequestWorkers=150, needs 8Gi limit (Apache transient memory spikes, see commit eb94144), very generous startup probe | | Immich | ML on SSD (CUDA), disable ModSecurity (breaks streaming), frequent upgrades. **`immich-machine-learning` MUST run with `MACHINE_LEARNING_MODEL_TTL > 0`** (set to `600` in `stacks/immich/main.tf`, env on the `immich-machine-learning` deployment). At `0`, no model ever unloads and onnxruntime's CUDA arena (OCR's dynamic input shapes inflate it to ~10 GB) is held forever on the **time-sliced T4 it shares with llama-swap/frigate/immich-server** — which has no VRAM isolation, so immich-ml starved llama-swap (qwen3-8b) and silently broke recruiter-responder triage for ~5 h on 2026-06-02 (post-mortem `docs/post-mortems/2026-06-02-immich-ml-ttl-gpu-oom-recruiter.md`). TTL>0 lets idle models (OCR, face — AND CLIP) free VRAM. The TTL is a single GLOBAL knob (no per-model pin), so CLIP would also unload after 600s idle; the `clip-keepalive` CronJob (`*/5 * * * *`, same stack) pings the CLIP textual encoder so smart-search stays warm without pinning the ad-hoc models. **Smart search has a SECOND warmth layer in Postgres** (don't conflate it with the ML model): the ~665MB vchord `clip_index` must stay resident in PG `shared_buffers`, else an ANN probe that lands on an evicted list pays a ~1.8s cold storage read vs ~4ms warm. The `postStart` hook prewarms it ONCE at pod start and `pg_prewarm.autoprewarm` only re-warms at *startup*, so the index decays out of cache over days under job buffer-pressure (observed ~33% resident after 9d uptime → slow context search, easily misattributed to the ML model). The `clip-index-prewarm` CronJob (`*/5`, same stack) re-runs `pg_prewarm('clip_index')` to pin it hot; `immich-search-probe` (`*/5`) measures live latency + residency → Pushgateway gauges (`immich_smart_search_db_seconds`, `immich_clip_index_cached_pct`) → alerts `ImmichSmartSearchSlow`/`ImmichClipIndexColdCache`/`ImmichSearchProbeStale` + cluster-health check #46 (`check_immich_search`). immich PG role is a superuser so the CronJobs can run `pg_prewarm`/`pg_buffercache`. **Video transcoding is GPU-accelerated**: `immich-server` is pinned to GPU node1 (nodeSelector `nvidia.com/gpu.present` + NoSchedule toleration + `gpu-workload` priority) with a time-sliced `nvidia.com/gpu=1` slice — the stock immich-server image's ffmpeg already ships h264/hevc_nvenc + NVDEC. Activated via `ffmpeg.accel=nvenc` + `accelDecode=true` in the **DB** system-config (`system_metadata` table, key `system-config`, JSONB — NOT Terraform; app config is DB-managed here like oauth/smtp). Direct DB edits need a pod **recreate** to reload (config is cached at boot; only API-driven changes broadcast a reload). **Streaming bitrate is capped** to keep 4K playback smooth on the contended HDD and over remote uplinks: `ffmpeg.maxBitrate=20000k` + `preset=medium` + `transcode=bitrate` (set 2026-06-01 — was uncapped `maxBitrate=0` + `ultrafast` + `targetResolution=original`, which produced 77–264 Mbps 4K transcodes that stuttered for every client, local and remote, since even a single stream needs ~10–13.5 MB/s off the shared `sdc` spindle). 4K resolution is preserved (`targetResolution=original`); originals are NEVER modified — only the `encoded-video/` streaming copy. To re-apply transcode settings to EXISTING videos (config changes only affect new/missing ones): delete the offenders' `asset_file` rows `WHERE type='encoded_video'` (derived/regenerable — never touches originals) then run videoConversion `force=false` (admin Jobs API → "Missing"); it regenerates them to the deterministic `.mp4` path at concurrency 1 (gentle on sdc). See `docs/runbooks/immich-transcode-bitrate.md`. If Immich is ever reinstalled fresh (not restored), re-set these keys (accel, accelDecode, **maxBitrate=20000k, preset=medium, transcode=bitrate**). Thumbnails/previews live on SSD NFS (sdb) — do NOT move to block storage (HDD sdc = slower + the contended IO domain). **Background-job concurrency is capped to protect sdc** (DB-managed system-config, `system_metadata` key `system-config`, JSONB `job.*.concurrency`; re-set on fresh install): `thumbnailGeneration=2`, `metadataExtraction=2`, `library=2` — these jobs read ORIGINALS off the HDD library. Left uncapped (were 8/4/4) a library-wide job (e.g. Duplicate Detection on 2026-06-01) fans the ML/thumbnail backfill out into a read storm that saturates sdc and starves etcd → apiserver down. `sidecar`/`smartSearch`/`faceDetection` stay at Immich defaults (small `.xmp` / SSD previews). Apply via Job Settings UI or the `system-config` API; **direct DB edits need an `immich-server` pod recreate to reload** (config cached at boot). See `docs/post-mortems/2026-05-25-immich-anca-elements-io-storm.md`. | -| CrowdSec | Pin version, disable Metabase when not needed (CPU hog), LAPI scaled to 3, **DB on PostgreSQL** (migrated from MySQL), flush config: max_items=10000/max_age=7d/agents_autodelete=30d, DECISION_DURATION=168h in blocklist CronJob. **Enforcement is out-of-band, NOT a Traefik plugin** (the Yaegi `crowdsec-bouncer-traefik-plugin` was dead on Traefik 3.7.5 and removed): `cs-firewall-bouncer` DaemonSet drops in-kernel via nftables on direct hosts (bouncer key `firewall`, v0.0.34 binary fetched at runtime, hostNetwork+NET_ADMIN, `stacks/crowdsec/modules/crowdsec/firewall_bouncer.tf`); `crowdsec-cf-sync` CronJob blocks at the CF edge for proxied hosts (bouncer key `kvsync`, `stacks/rybbit/crowdsec_edge.tf`). Both fail open. See `docs/architecture/security.md` | +| CrowdSec | Pin version, disable Metabase when not needed (CPU hog), LAPI scaled to 3, **DB on PostgreSQL** (migrated from MySQL), flush config: max_items=10000/max_age=7d/agents_autodelete=30d, DECISION_DURATION=168h in blocklist CronJob | | Frigate | GPU stall detection in liveness probe (inference speed check), high CPU | | Authentik | 3 server replicas + 2-replica embedded outpost (PG-backed sessions), PgBouncer in front of PostgreSQL, strip auth headers before forwarding. **`authentik.*` Helm values are INERT** (existingSecret skips chart env rendering) — tune via `server.env`/`worker.env` in `modules/authentik/values.yaml`. Single-screen login (password embedded in identification stage); all first-party OIDC apps use implicit consent (2026-06-10). `/static` ingress carve-out serves assets with immutable Cache-Control. | | Kyverno | failurePolicy=Ignore to prevent blocking cluster, pin chart version | diff --git a/.claude/skills/home-assistant/SKILL.md b/.claude/skills/home-assistant/SKILL.md index ab07a27f..61aaa6af 100644 --- a/.claude/skills/home-assistant/SKILL.md +++ b/.claude/skills/home-assistant/SKILL.md @@ -11,8 +11,8 @@ description: | There are TWO Home Assistant deployments: ha-london (default) and ha-sofia. Always use Home Assistant for smart home control. author: Claude Code -version: 2.1.0 -date: 2026-06-24 +version: 2.0.0 +date: 2026-02-07 --- # Home Assistant Control @@ -395,27 +395,14 @@ Advanced SSH, File Editor, Studio Code Server, InfluxDB, Mosquitto, Node-RED, Fr ## ha-london Knowledge Map ### Overview -- **HA Version**: 2026.5.2 on **Home Assistant OS** (HAOS — managed appliance, NOT a `docker run` container). Latest is 2026.6.4 (update available, deliberately not applied). +- **HA Version**: 2025.9.1 (Docker container on Raspberry Pi) - **Location**: London, UK -- **Platform**: Raspberry Pi 4, HA OS -- **Access from the Sofia devvm**: london is **remote** — `homelab ha ssh --instance london` generally WON'T connect (ADR-0012). Drive it via the API: `homelab ha token --instance london` + `https://ha-london.viktorbarzin.me/api/...`, and the WebSocket API `wss://ha-london.viktorbarzin.me/api/websocket` for dashboards / config-entries / HACS installs. -- **SSH (only from the London LAN)**: `ssh hassio@192.168.8.103` (requires `sudo` for file access) -- **Config path**: `/config/` +- **Platform**: Raspberry Pi 4, HA OS (not Docker standalone) +- **SSH**: `ssh hassio@192.168.8.103` (requires `sudo` for file access) +- **Config path**: `/config/` (requires `sudo` for file access) - **3 tracked people**: Viktor Barzin, Anca Milea, Gheorghe Milea - **Zone**: London (home) -### Dashboards (redesigned 2026-06-24) -**Glossary** (HA terms — keep distinct): -- **Dashboard** = a sidebar entry (Overview, Air Quality, Map). Sidebar *order* is a per-USER frontend preference, not in any dashboard config. -- **View** = a tab inside a dashboard. View order is global (stored in the dashboard config). -- **Card** = a widget inside a view. - -- **Overview** (`lovelace`, the default): responsive **sections** views, styled with Mushroom + mini-graph-card. - - **Home** tab: *Who's home* · *Comfort & Air* (CO₂/temp/humidity/PM2.5/VOC chips + CO₂ and temp/humidity trend graphs + link to Air Quality) · *Cowboy* (battery/range/last-ride) · *Energy* (5 Kasa plugs + power trend) · *Quick actions* (Netflix/Stremio/Night). - - **More** tab: *Network* (GL-MT6000 router) · *System* (HA version/update, last backup, RPi power) · *Phones*. -- **Air Quality** (`air-quality`): deep-dive (views: Home, Detailed). (`detialed`→`detailed` path typo fixed 2026-06-24.) -- Built via the WS `lovelace/config/save` API (london is remote — no SSH path). - ### Key Systems #### 1. Smart Plugs (TP-Link Kasa) — Energy Monitoring @@ -437,15 +424,10 @@ Named plugs with power/energy tracking: - PM1.0/2.5/4.0/10 particulate sensors - VOC, NOx, ammonia, CO, ethanol, hydrogen, methane, NO2 gas sensors -#### 3. Cowboy E-Bike (`elsbrock/cowboy-ha`) -Bike named **"Classic Performance"** → entities are `sensor.classic_performance_*` (26 total). The old `sensor.bike_*` names are GONE (they were the dead `jdejaegh` integration). -- `sensor.classic_performance_remaining_battery`: Battery % (was `sensor.bike_state_of_charge`) -- `sensor.classic_performance_remaining_range`: Range km -- `sensor.classic_performance_mileage`: Total km (was `sensor.bike_total_distance`) -- `sensor.classic_performance_saved_co2`: Lifetime CO2 saved (was `sensor.bike_total_co2_saved`) -- Plus `_distance_today`, `_last_trip_*`, `_battery_health`, `device_tracker.classic_performance`, etc. -- **GOTCHA**: live battery/range/mileage read `unknown` while the bike is parked/asleep — Cowboy only reports live SoC when awake (ridden/charging); trip-history + `distance_today` stay live regardless. -- Auth: account **email+password** (no AWS Cognito — that was the dead `jdejaegh`/`cowboybike` lineage). Setup via UI config flow / REST `config_entries/flow`. Creds in Vaultwarden item **"cowboy bike"** (`homelab vault get "cowboy bike"`). +#### 3. Cowboy E-Bike +- `sensor.bike_state_of_charge`: Battery % +- `sensor.bike_total_distance`: Total km +- `sensor.bike_total_co2_saved`: CO2 saved (grams) #### 4. Uptime Monitoring (UptimeRobot) - `sensor.blog`: blog uptime @@ -464,17 +446,12 @@ Bike named **"Classic Performance"** → entities are `sensor.classic_performanc - Scripts: `script.start_netflix`, `script.start_stremio` - Scene: `scene.night` (turns off Livia + Michelle plugs) -### Custom Components (HACS integrations) -- **cowboy** (`elsbrock/cowboy-ha` v1.2.0): Cowboy e-bike — revived 2026-06-24. The old `jdejaegh/home-assistant-cowboy` repo is **dead (404)**; don't chase it. -- **hildebrandglow_dcc**: UK smart meter DCC energy — **DISABLED by user** (config entry `disabled_by: user`), not broken. - -### HACS frontend cards (plugins) -- **Mushroom** (`piitaya/lovelace-mushroom`), **mini-graph-card** (`kalkih/mini-graph-card`), **plotly-graph-card** (`dbuezas/lovelace-plotly-graph-card`) — used by the redesigned Overview. Install over WS `hacs/repository/download`; resources auto-register in storage mode. +### Custom Components +- **cowboy**: Cowboy e-bike integration (HACS) +- **hildebrandglow_dcc**: UK smart meter DCC energy data (HACS) ### Integrations -ESPHome, TP-Link Kasa, Tapo, UptimeRobot, **Cowboy** (elsbrock), Oral-B BLE, Ookla Speedtest (exposes only an `update` entity, no live speed sensors), HACS, OpenRouter (free LLMs), Piper (TTS), Whisper (STT), Android TV/ADB. -- **Disabled by user (NOT broken)**: `met` + `metoffice` (weather — so `weather.*` entities are ABSENT), `roomba` (Rumi vacuum), `hildebrandglow_dcc` (energy). -- **Failing**: `tplink` **Tapo P100** projector plug — `setup_retry`, 403 KLAP handshake from 192.168.8.108 (plug off / firmware). Left as-is. +ESPHome, TP-Link Kasa, Tapo, UptimeRobot, Cowboy, Hildebrand Glow DCC, Oral-B BLE, Ookla Speedtest, HACS, OpenRouter (multiple free LLMs), Piper (local TTS), Whisper (local STT), Android TV/ADB ### AI / Voice Assistants - 5 free LLM conversation agents: Google Gemma 3 27B, Meta Llama 3.2 3B, Mistral Devstral 2, OpenAI GPT-OSS-20B, Z.AI GLM 4.5 Air @@ -489,8 +466,15 @@ ESPHome, TP-Link Kasa, Tapo, UptimeRobot, **Cowboy** (elsbrock), Oral-B BLE, Ook - Anca arrival/departure notifications - Night scene: turns off Livia + Michelle -### Platform (HAOS — ignore any legacy `docker run` snippet) -ha-london runs **Home Assistant OS** (managed appliance), NOT a hand-run Docker container. There is no `docker run homeassistant/home-assistant` to manage. Install HACS components over the WebSocket API (`hacs/repository/download` with the repo's HACS id), then restart via `POST /api/services/homeassistant/restart` — a HAOS restart drops automations for ~1–2 min and resets `sensor.uptime` (use that as the "back up" marker). +### Docker Setup +```bash +docker run -d --name homeassistant --privileged \ + -e TZ=Europe/London \ + -v /home/pi/docker/homeAssistant:/config \ + -v /run/dbus:/run/dbus:ro \ + --network=host --restart=unless-stopped \ + homeassistant/home-assistant:2025.9 +``` ### SSH Access ```bash diff --git a/.github/workflows/build-chrome-service-browser.yml b/.github/workflows/build-chrome-service-browser.yml deleted file mode 100644 index 9d2129c8..00000000 --- a/.github/workflows/build-chrome-service-browser.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Build chrome-service-browser - -# ADR-0002: infra-owned image built off-infra on GHA → ghcr. Playwright base + -# real Google Chrome (proprietary H.264/AAC codecs) for the chrome-service -# browser container, so the noVNC view can play H.264 video (Reels). Rebuilds -# are rare → dispatch + path trigger. NOTE: after the first push, set the ghcr -# package `chrome-service-browser` to PUBLIC (same as chrome-service-novnc) so -# the pod pulls it without credentials. -on: - push: - branches: [master] - paths: - - 'stacks/chrome-service/files/chrome/**' - workflow_dispatch: {} - -permissions: - contents: read - packages: write - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: docker/setup-buildx-action@v3 - - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - uses: docker/build-push-action@v6 - with: - context: stacks/chrome-service/files/chrome - platforms: linux/amd64 - provenance: false - push: true - tags: | - ghcr.io/viktorbarzin/chrome-service-browser:latest - ghcr.io/viktorbarzin/chrome-service-browser:${{ github.sha }} diff --git a/CONTEXT.md b/CONTEXT.md index 2b9bb8b3..d700f9ab 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -117,17 +117,9 @@ The bare-metal load-balancer that assigns external IPs to `type=LoadBalancer` Se _Avoid_: calling `.200` "the cluster IP" or assuming all ingress shares one LB IP. **Calico**: -The cluster CNI and **NetworkPolicy** engine (also GlobalNetworkPolicy + flow logs; live flow observability via **Goldmane / Whisker**). Egress lockdown follows an **observe-then-enforce** rollout — flow logs build an empirical allowlist, then default-deny egress is enforced per-namespace, tier by tier (wave 1 began at `recruiter-responder`; Tier 0/1/2 deferred). +The cluster CNI and **NetworkPolicy** engine (also GlobalNetworkPolicy + flow logs). Egress lockdown follows an **observe-then-enforce** rollout — flow logs build an empirical allowlist, then default-deny egress is enforced per-namespace, tier by tier (wave 1 began at `recruiter-responder`; Tier 0/1/2 deferred). _Avoid_: "firewall" (it's pod-level policy, not a perimeter); conflating a Calico **NetworkPolicy** (enforced in the data path) with a **Kyverno policy** (enforced at admission) — different layers. -**Service identity**: -How a **Service** is named in flow/audit data — its **namespace** is the primary identity (Goldmane stamps it natively, and "one Service ≈ one namespace" holds for ~87 namespaces), refined by an explicit identity label (e.g. `service-identity`) only in the handful of genuinely multi-Service namespaces (`monitoring`, `kube-system`, `dbaas`). Deliberately NOT a per-Service **ServiceAccount** (deferred — 56% of pods share `default`; revisit only if principal-based enforcement or mTLS is adopted) and NOT a SPIFFE/mesh identity (rejected — attribution-grade audit on a trusted single-tenant cluster doesn't justify a mesh). -_Avoid_: equating "service identity" with a workload's **ServiceAccount** (that's the deferred enforcement principal, not the attribution key) or with cryptographic/SPIFFE identity; "Service" here is the domain **Service**, not the K8s `Service` object. - -**Goldmane / Whisker**: -Calico 3.30's OSS flow-observability pair — **Goldmane** aggregates identity-stamped flows (namespace/pod/workload/labels + allow-deny + policy trace) streamed from Felix over gRPC into an in-memory ~60-min ring buffer (no etcd/API writes); **Whisker** is its live web UI. The east-west "who-talks-to-whom" data plane, succeeding raw iptables-`LOG`→journald lines (which carry no identity). Durable history requires emitting Goldmane flows to **Loki**; the in-memory buffer alone is not an audit trail. -_Avoid_: assuming Goldmane persists (it's a ring buffer — lost on restart); expecting a ServiceAccount field in its schema (it carries labels, not SA); confusing it with Cilium **Hubble** (needs the Cilium datapath, unusable on Calico) or **Kiali** (needs an Istio mesh). - ### Storage **proxmox-lvm-encrypted**: diff --git a/cli/README.md b/cli/README.md index 186c1ee5..adc06920 100644 --- a/cli/README.md +++ b/cli/README.md @@ -171,37 +171,6 @@ prints the bare token to stdout so it composes in `$(…)`; it's read-tier like not tied to whoever first wrote the workflow (the user's key must be enrolled on the HA host). -### v0.8 verbs — browser (headful anti-bot automation) - -Drive the cluster's **headful** Chrome (`chrome-service`, real Chrome under Xvfb) -from the devvm over CDP, for sites that detect and block headless automation. The -headless `@playwright/mcp` browser can *load* such a site and fill its forms, but -the gated action (submit/login) silently fails — the motivating case was the -Stirling Ackroyd Fixflo tenant portal, whose pre-submit check returned -`net::ERR_FILE_NOT_FOUND` and hung. This path connects via `connect_over_cdp`, -injects the same `stealth.js` the in-cluster callers use, and submits first try. - -The command owns only the *mechanics* (port-forward, stealth, lifecycle); the -agent supplies the Playwright script — judgment stays out of the CLI. - -| Command | Tier | What it does | -|---|---|---| -| `browser run [--url U] [--shared-context] [--keep-open] [--port N] [--timeout S]` | write | port-forward `svc/chrome-service:9222`, assert it's a real (non-headless) Chrome via `/json/version`, `connect_over_cdp`, `addInitScript(stealth.js)`, then run the script with `page`/`context`/`browser`/`log` in scope (top-level await ok; return a value to print it). Always tears the forward down. | -| `browser open [--shared-context] [--timeout S]` | write | open `` headful and print title + visible text + a screenshot path — a quick check. | -| `browser --help` | read | when-to-use signature + the error-code cheat-sheet (`ERR_FILE_NOT_FOUND` = automation-layer intercept, not egress; `ERR_CONNECTION_REFUSED`/`_TIMED_OUT`/`_NAME_NOT_RESOLVED` = real egress; one endpoint 500 while siblings 200 = bot rejection). | - -Default context is a **fresh incognito** one (closed on exit) — safe for the -shared browser and concurrent callers (e.g. tripit's fare scrape); `--shared-context` -reuses the warmed persistent profile when a pre-logged-in session is needed. -`port-forward` tunnels API-server→pod, so it bypasses the `:9222` NetworkPolicy -that gates in-cluster callers — no namespace label needed. The node CDP client is -pinned to **`playwright-core@1.48.2`** to match the chrome-service image minor -(Chromium 130; protocol changes between minors) and is installed once, lazily, -into `~/.cache/homelab/browser-client/` (no per-user setup). Because the client -runs on the devvm, `setInputFiles` streams local files to the remote browser over -CDP — no `chmod`/staging-dir workaround. See `docs/architecture/chrome-service.md` -and `docs/adr/0013`. - ## Build / install Built from source to `/usr/local/bin/homelab` during devvm provisioning @@ -221,4 +190,4 @@ original flag-based path unchanged, so the webhook handler is unaffected. ## Design -See `infra/docs/adr/0004`–`0013` for the architecture decisions. +See `infra/docs/adr/0004`–`0012` for the architecture decisions. diff --git a/cli/VERSION b/cli/VERSION index 85f7059b..63f2359f 100644 --- a/cli/VERSION +++ b/cli/VERSION @@ -1 +1 @@ -v0.8.1 +v0.7.1 diff --git a/cli/browser.go b/cli/browser.go deleted file mode 100644 index 39b6b0a0..00000000 --- a/cli/browser.go +++ /dev/null @@ -1,388 +0,0 @@ -package main - -import ( - _ "embed" - "encoding/json" - "fmt" - "io" - "net" - "net/http" - "os" - "os/exec" - "os/signal" - "path/filepath" - "strconv" - "strings" - "sync" - "syscall" - "time" -) - -// playwrightVersion pins the node CDP client to the chrome-service image minor -// (mcr.microsoft.com/playwright:v1.48.0-noble → Chromium 130). connect_over_cdp -// speaks the browser's CDP, so the client minor must track the server minor; -// see docs/architecture/chrome-service.md "Image pin". -const playwrightVersion = "1.48.2" - -// defaultBrowserTimeout is how long (seconds) to wait for the port-forwarded CDP -// endpoint to become ready before giving up. -const defaultBrowserTimeout = 60 - -const ( - chromeServiceNamespace = "chrome-service" - chromeServiceName = "chrome-service" - chromeServiceCDPPort = 9222 -) - -// stealthJS is vendored verbatim from stacks/chrome-service/files/stealth.js (the -// source of truth the in-cluster callers use). TestStealthJSEmbeddedMatchesCanonical -// guards against drift. -// -//go:embed browser_stealth.js -var stealthJS string - -// runnerJS is the node wrapper that connects to the port-forwarded CDP endpoint, -// installs the stealth init script, and runs the user's Playwright script. -// -//go:embed browser_runner.js -var runnerJS string - -// browserOpts is the parsed form of `homelab browser run|open` arguments. -type browserOpts struct { - mode string // "run" | "open" - script string // path to the user Playwright script (run mode) - url string // initial URL (run: optional; open: required positional) - sharedCtx bool // use the warmed persistent profile instead of a fresh context - keepOpen bool // leave the created context/pages open on exit - port int // explicit local port for the forward (0 = auto) - timeout int // CDP readiness timeout, seconds - help bool -} - -// parseBrowserArgs parses the args after `browser run` / `browser open`. -func parseBrowserArgs(mode string, args []string) (browserOpts, error) { - o := browserOpts{mode: mode, timeout: defaultBrowserTimeout} - var positionals []string - atoi := func(s, flag string) (int, error) { - n, err := strconv.Atoi(s) - if err != nil { - return 0, fmt.Errorf("%s expects an integer, got %q", flag, s) - } - return n, nil - } - for i := 0; i < len(args); i++ { - a := args[i] - switch { - case a == "-h" || a == "--help": - o.help = true - case a == "--shared-context": - o.sharedCtx = true - case a == "--keep-open": - o.keepOpen = true - case a == "--url": - if i+1 < len(args) { - o.url = args[i+1] - i++ - } - case strings.HasPrefix(a, "--url="): - o.url = strings.TrimPrefix(a, "--url=") - case a == "--port": - if i+1 < len(args) { - n, err := atoi(args[i+1], "--port") - if err != nil { - return o, err - } - o.port = n - i++ - } - case strings.HasPrefix(a, "--port="): - n, err := atoi(strings.TrimPrefix(a, "--port="), "--port") - if err != nil { - return o, err - } - o.port = n - case a == "--timeout": - if i+1 < len(args) { - n, err := atoi(args[i+1], "--timeout") - if err != nil { - return o, err - } - o.timeout = n - i++ - } - case strings.HasPrefix(a, "--timeout="): - n, err := atoi(strings.TrimPrefix(a, "--timeout="), "--timeout") - if err != nil { - return o, err - } - o.timeout = n - case strings.HasPrefix(a, "-"): - return o, fmt.Errorf("unknown flag %q (try: homelab browser --help)", a) - default: - positionals = append(positionals, a) - } - } - if o.help { - return o, nil - } - switch mode { - case "run": - if len(positionals) == 0 { - return o, fmt.Errorf("usage: homelab browser run [--url URL] [--shared-context] [--keep-open] [--port N] [--timeout S]") - } - o.script = positionals[0] - case "open": - if len(positionals) == 0 { - return o, fmt.Errorf("usage: homelab browser open [--shared-context] [--timeout S]") - } - o.url = positionals[0] - } - return o, nil -} - -// cdpHealthy parses a CDP /json/version body and reports whether the endpoint is -// a real (non-headless) Chrome — the entire reason chrome-service exists. -func cdpHealthy(jsonBody []byte) (browser string, healthy bool, err error) { - var v struct { - Browser string `json:"Browser"` - UserAgent string `json:"User-Agent"` - } - if e := json.Unmarshal(jsonBody, &v); e != nil { - return "", false, fmt.Errorf("parse /json/version: %w", e) - } - if v.Browser == "" { - return "", false, fmt.Errorf("/json/version had no Browser field") - } - healthy = strings.HasPrefix(v.Browser, "Chrome/") && - !strings.Contains(v.Browser, "Headless") && - !strings.Contains(v.UserAgent, "Headless") - return v.Browser, healthy, nil -} - -// buildPortForwardArgs is the kubectl invocation that exposes chrome-service's -// CDP locally. port-forward tunnels API-server→pod, so it bypasses the :9222 -// NetworkPolicy that gates in-cluster callers. -func buildPortForwardArgs(localPort int) []string { - return []string{"-n", chromeServiceNamespace, "port-forward", - "svc/" + chromeServiceName, fmt.Sprintf("%d:%d", localPort, chromeServiceCDPPort)} -} - -// browserClientPackageJSON is the auto-managed manifest for the pinned node CDP -// client kept under the user cache dir. -func browserClientPackageJSON() string { - return fmt.Sprintf(`{ - "name": "homelab-browser-client", - "private": true, - "description": "Pinned CDP client for 'homelab browser' — auto-managed, do not edit.", - "dependencies": { - "playwright-core": "%s" - } -} -`, playwrightVersion) -} - -// freePort asks the kernel for an unused ephemeral TCP port. -func freePort() (int, error) { - l, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - return 0, err - } - defer l.Close() - return l.Addr().(*net.TCPAddr).Port, nil -} - -// browserClientDir is where the pinned node client + managed runner files live. -func browserClientDir() (string, error) { - cache, err := os.UserCacheDir() - if err != nil || cache == "" { - home, herr := os.UserHomeDir() - if herr != nil { - return "", fmt.Errorf("locate cache dir: %v / %v", err, herr) - } - cache = filepath.Join(home, ".cache") - } - return filepath.Join(cache, "homelab", "browser-client"), nil -} - -// installedPlaywrightVersion reads the version of the playwright-core already -// installed in dir, or "" if absent/unreadable. -func installedPlaywrightVersion(dir string) string { - b, err := os.ReadFile(filepath.Join(dir, "node_modules", "playwright-core", "package.json")) - if err != nil { - return "" - } - var v struct { - Version string `json:"version"` - } - if json.Unmarshal(b, &v) != nil { - return "" - } - return v.Version -} - -// ensureBrowserClient writes the managed runner/stealth/package files into dir -// and lazily installs the pinned playwright-core (only when missing/mismatched), -// so no per-user setup is needed and the client tracks the binary version. -func ensureBrowserClient(dir string) error { - if err := os.MkdirAll(dir, 0o755); err != nil { - return err - } - files := map[string]string{ - "package.json": browserClientPackageJSON(), - "browser_runner.js": runnerJS, - "stealth.js": stealthJS, - } - for name, content := range files { - if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { - return err - } - } - if installedPlaywrightVersion(dir) == playwrightVersion { - return nil - } - fmt.Fprintf(os.Stderr, "homelab browser: installing pinned playwright-core@%s (one-time, ~a few seconds)…\n", playwrightVersion) - cmd := exec.Command("npm", "install", "--no-audit", "--no-fund", "--silent") - cmd.Dir = dir - cmd.Stdout = os.Stderr - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return fmt.Errorf("npm install playwright-core@%s in %s: %w (is node/npm installed?)", playwrightVersion, dir, err) - } - if got := installedPlaywrightVersion(dir); got != playwrightVersion { - return fmt.Errorf("playwright-core install mismatch in %s: want %s, got %q", dir, playwrightVersion, got) - } - return nil -} - -// waitForCDP polls the local CDP endpoint until it answers as a healthy -// (non-headless) Chrome, or the timeout elapses. -func waitForCDP(cdpURL string, timeout time.Duration) (string, error) { - deadline := time.Now().Add(timeout) - client := &http.Client{Timeout: 3 * time.Second} - var lastErr error - for time.Now().Before(deadline) { - resp, err := client.Get(cdpURL + "/json/version") - if err != nil { - lastErr = err - time.Sleep(300 * time.Millisecond) - continue - } - body, _ := io.ReadAll(resp.Body) - resp.Body.Close() - browser, healthy, herr := cdpHealthy(body) - if herr != nil { - lastErr = herr - time.Sleep(300 * time.Millisecond) - continue - } - if !healthy { - return browser, fmt.Errorf("CDP reports %q — expected a non-headless Chrome (wrong target?)", browser) - } - return browser, nil - } - if lastErr == nil { - lastErr = fmt.Errorf("timed out after %s", timeout) - } - return "", lastErr -} - -// runBrowser is the orchestration: pick a port, ensure the pinned client, start -// (and ALWAYS tear down) a CDP port-forward, wait for readiness, then run node. -func runBrowser(o browserOpts) error { - port := o.port - if port == 0 { - p, err := freePort() - if err != nil { - return fmt.Errorf("pick local port: %w", err) - } - port = p - } - - dir, err := browserClientDir() - if err != nil { - return err - } - if err := ensureBrowserClient(dir); err != nil { - return err - } - - // Start the forward in its own process group so the whole tree dies on cleanup. - pf := exec.Command("kubectl", buildPortForwardArgs(port)...) - pf.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} - var pfLog strings.Builder - pf.Stdout = &pfLog - pf.Stderr = &pfLog - if err := pf.Start(); err != nil { - return fmt.Errorf("start kubectl port-forward (kubeconfig set?): %w", err) - } - - var once sync.Once - teardown := func() { - once.Do(func() { - if pf.Process != nil { - _ = syscall.Kill(-pf.Process.Pid, syscall.SIGKILL) - } - _ = pf.Wait() - }) - } - defer teardown() - - // Tear down on Ctrl-C / SIGTERM too, then exit non-zero. - sigCh := make(chan os.Signal, 1) - signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) - defer signal.Stop(sigCh) - go func() { - if _, ok := <-sigCh; ok { - teardown() - os.Exit(130) - } - }() - - cdpURL := fmt.Sprintf("http://127.0.0.1:%d", port) - browser, err := waitForCDP(cdpURL, time.Duration(o.timeout)*time.Second) - if err != nil { - return fmt.Errorf("chrome-service CDP not ready on %s: %w\n--- port-forward log ---\n%s", cdpURL, err, pfLog.String()) - } - fmt.Fprintf(os.Stderr, "homelab browser: connected to %s via %s\n", browser, cdpURL) - - return runBrowserNode(dir, cdpURL, o) -} - -// runBrowserNode invokes the managed node runner with inputs passed via env. -func runBrowserNode(dir, cdpURL string, o browserOpts) error { - env := append(os.Environ(), - "HOMELAB_CDP_URL="+cdpURL, - "HOMELAB_BROWSER_MODE="+o.mode, - "HOMELAB_STEALTH_PATH="+filepath.Join(dir, "stealth.js"), - "NODE_PATH="+filepath.Join(dir, "node_modules"), - ) - if o.url != "" { - env = append(env, "HOMELAB_BROWSER_URL="+o.url) - } - if o.script != "" { - abs, err := filepath.Abs(o.script) - if err != nil { - return err - } - if _, err := os.Stat(abs); err != nil { - return fmt.Errorf("script %s: %w", o.script, err) - } - env = append(env, "HOMELAB_BROWSER_SCRIPT="+abs) - } - if o.sharedCtx { - env = append(env, "HOMELAB_BROWSER_SHARED=1") - } - if o.keepOpen { - env = append(env, "HOMELAB_BROWSER_KEEP_OPEN=1") - } - if o.mode == "open" { - shot := filepath.Join(os.TempDir(), fmt.Sprintf("homelab-browser-%d.png", os.Getpid())) - env = append(env, "HOMELAB_BROWSER_SCREENSHOT="+shot) - } - cmd := exec.Command("node", filepath.Join(dir, "browser_runner.js")) - cmd.Env = env - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - cmd.Stdin = os.Stdin - return cmd.Run() -} diff --git a/cli/browser_runner.js b/cli/browser_runner.js deleted file mode 100644 index 24a2db6b..00000000 --- a/cli/browser_runner.js +++ /dev/null @@ -1,106 +0,0 @@ -// homelab browser — node CDP runner (auto-managed; regenerated each run from the -// homelab binary — DO NOT EDIT here). Connects to the port-forwarded -// chrome-service CDP endpoint, installs the stealth init script, then runs the -// user's Playwright script (run mode) or opens a URL (open mode). All inputs -// arrive via HOMELAB_* env vars set by the Go CLI. -'use strict'; -const fs = require('fs'); -const { chromium } = require('playwright-core'); - -async function main() { - const cdpURL = process.env.HOMELAB_CDP_URL; - if (!cdpURL) throw new Error('HOMELAB_CDP_URL not set'); - const mode = process.env.HOMELAB_BROWSER_MODE || 'run'; - const stealthPath = process.env.HOMELAB_STEALTH_PATH || ''; - const initURL = process.env.HOMELAB_BROWSER_URL || ''; - const scriptPath = process.env.HOMELAB_BROWSER_SCRIPT || ''; - const shared = process.env.HOMELAB_BROWSER_SHARED === '1'; - const keepOpen = process.env.HOMELAB_BROWSER_KEEP_OPEN === '1'; - const screenshotPath = process.env.HOMELAB_BROWSER_SCREENSHOT || ''; - - const browser = await chromium.connectOverCDP(cdpURL); - - // Fresh isolated context by default (safe for the shared browser + concurrent - // callers); --shared-context reuses the warmed persistent profile. - let context; - let createdContext = false; - if (shared) { - const existing = browser.contexts(); - if (existing.length) { - context = existing[0]; - } else { - context = await browser.newContext(); - createdContext = true; - } - } else { - context = await browser.newContext(); - createdContext = true; - } - - if (stealthPath) { - const stealth = fs.readFileSync(stealthPath, 'utf8'); - if (stealth.trim()) await context.addInitScript(stealth); - } - - const page = await context.newPage(); - const log = (...a) => console.error('[browser]', ...a); - - let exitCode = 0; - try { - if (initURL) { - await page.goto(initURL, { waitUntil: 'domcontentloaded' }); - } - if (mode === 'open') { - console.log('url: ' + page.url()); - console.log('title: ' + (await page.title())); - const text = (await page.evaluate(() => (document.body ? document.body.innerText : ''))).trim(); - console.log('--- visible text (truncated to 4000 chars) ---'); - console.log(text.slice(0, 4000)); - if (screenshotPath) { - await page.screenshot({ path: screenshotPath, fullPage: true }); - console.log('screenshot: ' + screenshotPath); - } - } else { - if (!scriptPath) throw new Error('run mode requires HOMELAB_BROWSER_SCRIPT'); - const src = fs.readFileSync(scriptPath, 'utf8'); - // Run the user's source with page/context/browser/log in lexical scope. - // AsyncFunction body permits top-level await. - const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor; - const fn = new AsyncFunction('page', 'context', 'browser', 'log', src); - const result = await fn(page, context, browser, log); - if (result !== undefined) { - let out; - try { - out = typeof result === 'string' ? result : JSON.stringify(result, null, 2); - } catch (_) { - out = String(result); - } - console.log(out); - } - } - } catch (e) { - console.error('homelab browser: script error:', e && e.stack ? e.stack : e); - exitCode = 1; - } finally { - if (!keepOpen) { - try { - // Close only what we created; never tear down the shared persistent context. - if (createdContext) { - await context.close(); - } else { - await page.close(); - } - } catch (_) { /* ignore */ } - } - // Disconnect from the CDP endpoint; this does NOT kill the remote browser. - try { - await browser.close(); - } catch (_) { /* ignore */ } - } - process.exit(exitCode); -} - -main().catch((e) => { - console.error('homelab browser: fatal:', e && e.stack ? e.stack : e); - process.exit(1); -}); diff --git a/cli/browser_stealth.js b/cli/browser_stealth.js deleted file mode 100644 index dfae98a8..00000000 --- a/cli/browser_stealth.js +++ /dev/null @@ -1,54 +0,0 @@ -// Minimal stealth init script for Playwright-driven Chromium. -// Vendored from puppeteer-extra-plugin-stealth/evasions/* (MIT) — covers: -// webdriver, chrome.runtime, navigator.plugins, navigator.languages, -// Permissions.query, WebGL getParameter (vendor + renderer spoof). -// Run via context.add_init_script() so it executes before any page script. -(() => { - // navigator.webdriver — most common detection, removed entirely. - Object.defineProperty(Navigator.prototype, 'webdriver', { get: () => undefined }); - - // window.chrome.runtime — many sites check that real Chrome exposes this. - if (!window.chrome) window.chrome = {}; - window.chrome.runtime = window.chrome.runtime || {}; - - // navigator.plugins — headless reports zero; spoof a plausible PDF viewer. - Object.defineProperty(navigator, 'plugins', { - get: () => [{ name: 'Chrome PDF Plugin' }, { name: 'Chrome PDF Viewer' }, { name: 'Native Client' }], - }); - - // navigator.languages — headless returns empty array. - Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); - - // Permissions.query — headless returns 'denied' for notifications instead of 'default'. - const origQuery = window.navigator.permissions && window.navigator.permissions.query; - if (origQuery) { - window.navigator.permissions.query = (parameters) => - parameters && parameters.name === 'notifications' - ? Promise.resolve({ state: Notification.permission }) - : origQuery(parameters); - } - - // WebGL getParameter — spoof vendor + renderer strings to a real GPU. - const spoofGl = (proto) => { - if (!proto) return; - const orig = proto.getParameter; - proto.getParameter = function (parameter) { - if (parameter === 37445) return 'Intel Inc.'; // UNMASKED_VENDOR_WEBGL - if (parameter === 37446) return 'Intel Iris OpenGL Engine'; // UNMASKED_RENDERER_WEBGL - return orig.apply(this, arguments); - }; - }; - spoofGl(window.WebGLRenderingContext && window.WebGLRenderingContext.prototype); - spoofGl(window.WebGL2RenderingContext && window.WebGL2RenderingContext.prototype); - - // disable-devtool.js (theajack/disable-devtool) auto-inits via a script - // tag with `disable-devtool-auto`. Its Performance detector trips under - // Playwright (CDP adds console.log latency vs console.table) and the - // redirect URL is hard-coded — for hmembeds that's google.com. - // Hide the auto-init marker so the library's IIFE exits early. - const origQS = Document.prototype.querySelector; - Document.prototype.querySelector = function (sel) { - if (typeof sel === 'string' && sel.indexOf('disable-devtool-auto') !== -1) return null; - return origQS.apply(this, arguments); - }; -})(); diff --git a/cli/cmd_browser.go b/cli/cmd_browser.go deleted file mode 100644 index 4263e4d0..00000000 --- a/cli/cmd_browser.go +++ /dev/null @@ -1,117 +0,0 @@ -package main - -import "fmt" - -// browser verbs drive the cluster's HEADFUL Chrome (ns chrome-service) over CDP -// from outside the cluster, for sites that detect/block headless automation. -// The headless @playwright/mcp browser can load such sites but their gated -// actions (submit/login) silently fail; this path submits first try. Mechanics -// only — the agent supplies the Playwright script. See docs/adr/0013. - -func browserCommands() []Command { - return []Command{ - {Path: []string{"browser"}, Tier: TierRead, - Summary: "headful cluster-Chrome automation for anti-bot sites (run `browser --help`)", Run: browserTopHelp}, - {Path: []string{"browser", "run"}, Tier: TierWrite, - Summary: "run a Playwright script against headful cluster Chrome: browser run [--url U] [--shared-context]", Run: browserRun}, - {Path: []string{"browser", "open"}, Tier: TierWrite, - Summary: "open a URL in headful cluster Chrome; print title + text + screenshot: browser open ", Run: browserOpen}, - } -} - -func browserTopHelp([]string) error { - fmt.Print(browserHelp()) - return nil -} - -func browserRun(args []string) error { - o, err := parseBrowserArgs("run", args) - if err != nil { - return err - } - if o.help { - fmt.Print(browserHelp()) - return nil - } - return runBrowser(o) -} - -func browserOpen(args []string) error { - o, err := parseBrowserArgs("open", args) - if err != nil { - return err - } - if o.help { - fmt.Print(browserHelp()) - return nil - } - return runBrowser(o) -} - -// browserHelp carries the discoverability payload: WHEN to reach for this, and -// the diagnostic cheat-sheet that lets the agent self-correct instead of -// retrying a deterministic form blind (the failure mode that motivated this). -func browserHelp() string { - return `homelab browser — drive the cluster's HEADFUL Chrome (anti-bot) over CDP - -The shared chrome-service (ns chrome-service) runs a REAL, headed Chrome under -Xvfb. This connects to it via a port-forward + Playwright connect_over_cdp, -injects the same stealth.js the in-cluster callers use, and runs your script. - -USAGE - homelab browser run [--url URL] [--shared-context] [--keep-open] [--port N] [--timeout S] - homelab browser open [--shared-context] [--timeout S] - -WHEN TO USE THIS — escalation only; DEFAULT to the headless/MCP browser - Default to the Playwright MCP / headless browser for ALL routine browsing and - automation — it's interactive (snapshot per step), fast to start, isolated. - Reach for THIS command ONLY when headless is demonstrably blocked: a site - LOADS fine but a gated action FAILS or HANGS — a submit/login/checkout spins - forever, or ONE request errors while its siblings 200. That is the signature - of headless / anti-bot detection (navigator.webdriver, UA "HeadlessChrome", - disable-devtool traps). It presents as a real Chrome and usually succeeds - first try — but it's the shared cluster browser (slower startup, one batch - run, no per-step feedback), so it's the escalation path, never the default. - -ERROR-CODE CHEAT-SHEET (diagnose BEFORE retrying) - ERR_FILE_NOT_FOUND (-6) request intercepted/resolved locally by the - automation layer — NOT a network/egress problem. - (This is what silently broke the headless submit.) - ERR_CONNECTION_REFUSED / real egress failure (DNS/route/firewall). These also - ERR_TIMED_OUT / break the initial page load — if the page loaded, - ERR_NAME_NOT_RESOLVED egress is fine and the cause is elsewhere. - one endpoint 500s while server-side bot rejection of the automation, not - its siblings 200 your payload. - -HABITS - - Inspect the network panel BEFORE retrying a deterministic form; a blind - retry just repeats the same silent failure. - - Don't park a half-filled multi-step form across a user pause — the session - can expire; re-run the whole flow from this command in one shot. - - Uploads stream over CDP via setInputFiles from THIS host — no chmod/staging - of $HOME needed; just point setInputFiles at a local path. - -CONTEXT - Default: a FRESH incognito context, closed on exit — safe for the shared - browser and concurrent callers (e.g. tripit). Your script does its own login. - --shared-context: reuse the warmed PERSISTENT profile (cookies from a manual - noVNC login at chrome.viktorbarzin.me) when you need a pre-logged-in session. - -SCRIPT CONTRACT (run mode) - Your file's body runs with page, context, browser and log() already in scope - (top-level await allowed). Return a value to print it. Example flow.js: - - await page.goto('https://portal.example.com/login'); - await page.fill('#user', 'me'); await page.fill('#pass', process.env.PW); - await page.click('button[type=submit]'); - await page.waitForURL('**/dashboard'); - return 'logged in: ' + page.url(); - - Run it: homelab browser run flow.js - -NOTES - - The Playwright client is pinned to playwright-core@` + playwrightVersion + ` to match the - chrome-service image (Chrome 130); installed once into ~/.cache/homelab/. - - The port-forward is always torn down, on success and on error. -` -} diff --git a/cli/cmd_browser_test.go b/cli/cmd_browser_test.go deleted file mode 100644 index 668897d3..00000000 --- a/cli/cmd_browser_test.go +++ /dev/null @@ -1,172 +0,0 @@ -package main - -import ( - "os" - "reflect" - "strings" - "testing" -) - -func TestParseBrowserArgsRun(t *testing.T) { - got, err := parseBrowserArgs("run", []string{ - "flow.js", "--url", "https://example.com", "--shared-context", - "--port", "19999", "--timeout", "45", "--keep-open", - }) - if err != nil { - t.Fatalf("parseBrowserArgs run: unexpected err: %v", err) - } - want := browserOpts{ - mode: "run", script: "flow.js", url: "https://example.com", - sharedCtx: true, keepOpen: true, port: 19999, timeout: 45, - } - if !reflect.DeepEqual(got, want) { - t.Fatalf("parseBrowserArgs run =\n %+v\nwant\n %+v", got, want) - } -} - -func TestParseBrowserArgsRunDefaults(t *testing.T) { - got, err := parseBrowserArgs("run", []string{"flow.js"}) - if err != nil { - t.Fatalf("unexpected err: %v", err) - } - if got.script != "flow.js" || got.sharedCtx || got.keepOpen || got.port != 0 { - t.Fatalf("defaults wrong: %+v", got) - } - if got.timeout != defaultBrowserTimeout { - t.Fatalf("timeout default = %d, want %d", got.timeout, defaultBrowserTimeout) - } -} - -func TestParseBrowserArgsRunRequiresScript(t *testing.T) { - if _, err := parseBrowserArgs("run", []string{"--url", "https://x"}); err == nil { - t.Fatalf("run without a script path should error") - } -} - -func TestParseBrowserArgsOpenRequiresURL(t *testing.T) { - got, err := parseBrowserArgs("open", []string{"https://example.com"}) - if err != nil { - t.Fatalf("unexpected err: %v", err) - } - if got.url != "https://example.com" || got.mode != "open" { - t.Fatalf("open parse wrong: %+v", got) - } - if _, err := parseBrowserArgs("open", []string{}); err == nil { - t.Fatalf("open without a URL should error") - } -} - -func TestParseBrowserArgsHelp(t *testing.T) { - for _, a := range [][]string{{"--help"}, {"-h"}, {"flow.js", "--help"}} { - got, err := parseBrowserArgs("run", a) - if err != nil { - t.Fatalf("help parse %v: %v", a, err) - } - if !got.help { - t.Fatalf("args %v should set help", a) - } - } -} - -func TestParseBrowserArgsEqualsForm(t *testing.T) { - got, err := parseBrowserArgs("run", []string{"flow.js", "--url=https://x", "--port=8123", "--timeout=10"}) - if err != nil { - t.Fatalf("unexpected err: %v", err) - } - if got.url != "https://x" || got.port != 8123 || got.timeout != 10 { - t.Fatalf("--flag=value form not parsed: %+v", got) - } -} - -func TestCDPHealthy(t *testing.T) { - real := []byte(`{"Browser":"Chrome/130.0.6723.31","User-Agent":"Mozilla/5.0 (X11; Linux x86_64) Chrome/130.0.0.0 Safari/537.36","webSocketDebuggerUrl":"ws://127.0.0.1/devtools/browser/x"}`) - browser, ok, err := cdpHealthy(real) - if err != nil || !ok { - t.Fatalf("real Chrome should be healthy: ok=%v err=%v", ok, err) - } - if !strings.HasPrefix(browser, "Chrome/") { - t.Fatalf("browser = %q, want Chrome/ prefix", browser) - } - - headless := []byte(`{"Browser":"HeadlessChrome/130.0.6723.31","User-Agent":"Mozilla/5.0 HeadlessChrome/130.0.0.0"}`) - if _, ok, _ := cdpHealthy(headless); ok { - t.Fatalf("HeadlessChrome must be reported unhealthy (the whole point of chrome-service)") - } - - if _, _, err := cdpHealthy([]byte("not json")); err == nil { - t.Fatalf("malformed /json/version body should error") - } -} - -func TestBuildPortForwardArgs(t *testing.T) { - got := buildPortForwardArgs(18080) - want := []string{"-n", "chrome-service", "port-forward", "svc/chrome-service", "18080:9222"} - if !reflect.DeepEqual(got, want) { - t.Fatalf("buildPortForwardArgs =\n %v\nwant\n %v", got, want) - } -} - -func TestBrowserClientPackageJSONPinsVersion(t *testing.T) { - pj := browserClientPackageJSON() - if !strings.Contains(pj, `"playwright-core": "`+playwrightVersion+`"`) { - t.Fatalf("package.json must pin playwright-core to %s; got:\n%s", playwrightVersion, pj) - } -} - -func TestPlaywrightVersionPinnedToServerMinor(t *testing.T) { - // chrome-service runs mcr.microsoft.com/playwright:v1.48.0-noble; the CDP - // client minor MUST match (protocol changes between minors). - if !strings.HasPrefix(playwrightVersion, "1.48.") { - t.Fatalf("playwrightVersion = %q, must be 1.48.x to match the chrome-service image", playwrightVersion) - } -} - -func TestBrowserHelpHasDiagnosticCheatSheet(t *testing.T) { - h := browserHelp() - for _, want := range []string{ - "homelab browser run", - "ERR_FILE_NOT_FOUND", - "ERR_CONNECTION_REFUSED", - "network panel", - "headless", - "--shared-context", - } { - if !strings.Contains(h, want) { - t.Errorf("browser --help is missing %q (the discoverability/self-correction payload)", want) - } - } -} - -func TestBrowserHelpIsTiered(t *testing.T) { - // --help must frame this as the ESCALATION path (default to headless first), - // matching ~/code/CLAUDE.md and chrome-service.md — non-conflicting agent - // instructions. Guard against a regression to "co-equal choice" wording. - h := browserHelp() - for _, want := range []string{"Default to the", "escalation"} { - if !strings.Contains(h, want) { - t.Errorf("browser --help must carry the tiered/default-headless framing; missing %q", want) - } - } -} - -func TestStealthJSEmbeddedMatchesCanonical(t *testing.T) { - // The embedded copy must never drift from the source of truth that the - // in-cluster callers use, else the CLI's stealth and the cluster's diverge. - canonical, err := os.ReadFile("../stacks/chrome-service/files/stealth.js") - if err != nil { - t.Fatalf("read canonical stealth.js: %v", err) - } - if stealthJS != string(canonical) { - t.Fatalf("cli/browser_stealth.js has drifted from stacks/chrome-service/files/stealth.js — re-copy it") - } -} - -func TestFreePortReturnsUsablePort(t *testing.T) { - p, err := freePort() - if err != nil { - t.Fatalf("freePort: %v", err) - } - if p <= 1024 || p > 65535 { - t.Fatalf("freePort returned %d, want an ephemeral port", p) - } -} diff --git a/cli/cmd_vault.go b/cli/cmd_vault.go deleted file mode 100644 index bf270886..00000000 --- a/cli/cmd_vault.go +++ /dev/null @@ -1,663 +0,0 @@ -package main - -import ( - "bufio" - "encoding/base64" - "encoding/json" - "fmt" - "os" - "os/exec" - "strings" - "syscall" -) - -// vault verbs give each unix user no-HITL access to THEIR OWN Vaultwarden vault. -// Identity is the kernel UID; per-user creds live in that user's isolated Vault -// path (secret/workstation/claude-users/) read via their scoped token, and -// decryption is done by the official `bw` CLI. See -// docs/superpowers/specs/2026-06-24-homelab-vault-design.md. -func vaultCommands() []Command { - return []Command{ - {Path: []string{"vault", "setup"}, Tier: TierWrite, - Summary: "one-time: store your Vaultwarden master password + API key in your Vault path", Run: vaultSetup}, - {Path: []string{"vault", "status"}, Tier: TierRead, - Summary: "show whether your vault is configured/reachable (no secrets)", Run: vaultStatus}, - {Path: []string{"vault", "list"}, Tier: TierRead, - Summary: "list your item names: vault list [--search Q]", Run: vaultList}, - {Path: []string{"vault", "get"}, Tier: TierRead, - Summary: "fetch one item: vault get [--field password|username|uri|notes|totp] [--json]", Run: vaultGet}, - {Path: []string{"vault", "search"}, Tier: TierRead, - Summary: "search your item names: vault search ", Run: vaultSearch}, - {Path: []string{"vault", "code"}, Tier: TierRead, - Summary: "current TOTP code for an item: vault code ", Run: vaultCode}, - {Path: []string{"vault", "lock"}, Tier: TierWrite, - Summary: "lock/log out the local bw session", Run: vaultLock}, - {Path: []string{"vault"}, Tier: TierRead, - Summary: "Vaultwarden access for your own vault (run `homelab vault` for help)", - Run: func([]string) error { fmt.Print(vaultHelp()); return nil }}, - } -} - -// vaultHelp is shown for bare `homelab vault`. -func vaultHelp() string { - return `homelab vault — read YOUR OWN Vaultwarden logins (no-HITL after one-time setup) - - homelab vault setup one-time: store your master password + API key in your Vault path - homelab vault status configured / unlocked / reachable (no secrets) - homelab vault list [--search Q] list your item names (no secrets) - homelab vault get [--field password|username|uri|notes|totp] [--json] - TTY → clipboard (auto-clears); piped → stdout - homelab vault code current TOTP code - homelab vault lock lock / log out the local bw session - -Creds live only in your own Vault path; the admin never sees them. Identity is -your unix UID. Security model: docs/superpowers/specs/2026-06-24-homelab-vault-design.md -(note: anything running as your user can decrypt your vault — the accepted no-HITL trade). -` -} - -const vwUserPathPrefix = "secret/workstation/claude-users/" - -// vwCreds is one user's Vaultwarden auth material, read from their Vault path. -type vwCreds struct { - Email string - MasterPassword string - ClientID string - ClientSecret string -} - -// cmdRunner shells out to an external command with an explicit environment and -// returns trimmed stdout. Secrets are passed via envv, NEVER argv. Tests inject -// a fake; realRunner is the production implementation. -type cmdRunner func(name string, argv, envv []string) (string, error) - -func realRunner(name string, argv, envv []string) (string, error) { - cmd := exec.Command(name, argv...) - if envv != nil { - cmd.Env = envv - } - out, err := cmd.Output() - // Trim only the trailing newline the tool appends — NOT all whitespace, so a - // fetched secret with significant leading/trailing spaces is preserved. - return strings.TrimRight(string(out), "\r\n"), err -} - -// realRunnerStdin runs a command feeding `stdin` to it, for secret values that -// must NOT appear in argv (visible via ps / /proc//cmdline to same-UID -// processes). Used by setup to write the master password / client_secret. -func realRunnerStdin(name string, argv, envv []string, stdin string) (string, error) { - cmd := exec.Command(name, argv...) - if envv != nil { - cmd.Env = envv - } - cmd.Stdin = strings.NewReader(stdin) - out, err := cmd.Output() - return strings.TrimRight(string(out), "\r\n"), err -} - -func vwCredsPath(user string) string { return vwUserPathPrefix + user } - -func bwAppDataDir(uid string) string { return "/run/user/" + uid + "/homelab-bw" } - -// readVaultField returns one field from a KV-v2 path, "" if absent/error. -func readVaultField(run cmdRunner, field, path string) string { - out, err := run("vault", []string{"kv", "get", "-field=" + field, path}, nil) - if err != nil { - return "" - } - return out -} - -// loadCreds reads the four vaultwarden_* keys from the user's isolated path. -// A missing master password means the user hasn't onboarded. -func loadCreds(run cmdRunner, user string) (vwCreds, error) { - p := vwCredsPath(user) - c := vwCreds{ - Email: readVaultField(run, "vaultwarden_email", p), - MasterPassword: readVaultField(run, "vaultwarden_master_password", p), - ClientID: readVaultField(run, "vaultwarden_client_id", p), - ClientSecret: readVaultField(run, "vaultwarden_client_secret", p), - } - if c.MasterPassword == "" { - return vwCreds{}, fmt.Errorf("vault not configured for this user — run `homelab vault setup`") - } - return c, nil -} - -// vaultCurrentUser/vaultCurrentUID are seams for tests (avoid conflict with repo.go's currentUser func). -var vaultCurrentUser = func() string { return os.Getenv("USER") } -var vaultCurrentUID = func() string { return fmt.Sprintf("%d", os.Getuid()) } - -// bwBaseEnv is the minimal non-secret environment bw/node need. We deliberately -// do NOT inherit the full parent env (keeps stray secrets out of the child). -func bwBaseEnv(appdata string) []string { - path := os.Getenv("PATH") - if path == "" { - path = "/usr/local/bin:/usr/bin:/bin" - } - return []string{ - "PATH=" + path, - "HOME=" + os.Getenv("HOME"), - "BITWARDENCLI_APPDATA_DIR=" + appdata, - "BW_NOINTERACTION=true", - } -} - -// bwSecretEnv adds the secret-bearing vars. session may be "" (pre-unlock). -func bwSecretEnv(appdata string, c vwCreds, session string) []string { - env := bwBaseEnv(appdata) - env = append(env, - "BW_CLIENTID="+c.ClientID, - "BW_CLIENTSECRET="+c.ClientSecret, - "BW_PASSWORD="+c.MasterPassword, - ) - if session != "" { - env = append(env, "BW_SESSION="+session) - } - return env -} - -func bwLoginArgs() []string { return []string{"login", "--apikey"} } -func bwUnlockArgs() []string { return []string{"unlock", "--passwordenv", "BW_PASSWORD", "--raw"} } -func bwGetArgs(field, name string) []string { return []string{"get", field, name} } -func bwStatusArgs() []string { return []string{"status"} } - -// bwNeedsLogin parses `bw status` JSON and reports whether a `bw login` is -// required. Unparseable/empty output → true (safer to attempt login). -func bwNeedsLogin(statusJSON string) bool { - var s struct { - Status string `json:"status"` - } - if err := json.Unmarshal([]byte(statusJSON), &s); err != nil { - return true - } - return s.Status == "unauthenticated" || s.Status == "" -} - -func bwListArgs(search string) []string { - a := []string{"list", "items"} - if search != "" { - a = append(a, "--search", search) - } - return a -} - -// bwUnlock runs `bw unlock` and returns the raw session key. -func bwUnlock(run cmdRunner, env []string) (string, error) { - out, err := run("bw", bwUnlockArgs(), env) - if err != nil { - return "", fmt.Errorf("bw unlock failed (wrong master password? run `homelab vault setup`): %w", err) - } - return out, nil -} - -// bwGet fetches one field of one item; session must be present in env. -func bwGet(run cmdRunner, env []string, field, name string) (string, error) { - return run("bw", bwGetArgs(field, name), env) -} - -func returnMode(isTTY bool) string { - if isTTY { - return "clipboard" - } - return "stdout" -} - -// stdoutIsTTY reports whether stdout is a character device (a terminal). -func stdoutIsTTY() bool { - fi, err := os.Stdout.Stat() - if err != nil { - return false - } - return fi.Mode()&os.ModeCharDevice != 0 -} - -// stderrIsTTY reports whether stderr is a terminal (the OSC52 escape is written -// to stderr, so the clipboard path is only viable when stderr is a terminal). -func stderrIsTTY() bool { - fi, err := os.Stderr.Stat() - if err != nil { - return false - } - return fi.Mode()&os.ModeCharDevice != 0 -} - -// osc52 returns the OSC 52 escape that makes the local terminal copy payload to -// the system clipboard (works over SSH; no X11). osc52clear copies empty. -func osc52(payload string) string { - return "\x1b]52;c;" + base64.StdEncoding.EncodeToString([]byte(payload)) + "\a" -} -func osc52clear() string { return "\x1b]52;c;\a" } - -// terminalAllowed gates OSC 52: only terminals known to honor clipboard writes, -// else we'd dump the secret's base64 into scrollback on unsupported terminals. -func terminalAllowed(term, termProgram string) bool { - t := strings.ToLower(term) - p := strings.ToLower(termProgram) - for _, ok := range []string{"kitty", "alacritty", "foot", "wezterm", "ghostty", "tmux", "screen"} { - if strings.Contains(t, ok) || strings.Contains(p, ok) { - return true - } - } - // xterm proper supports it only when the program is a known-good emulator. - return false -} - -// opRecord is one CLI operation. ItemName is accepted for the caller's -// convenience but is INTENTIONALLY never rendered into the log line — auditing -// which of your own logins you opened is itself sensitive, and per-item reads -// are invisible server-side anyway (spec §9a). -type opRecord struct { - User string - Verb string - PID int - PPID int - ParentComm string - ItemName string // never logged -} - -func opLogLine(r opRecord) string { - return fmt.Sprintf("user=%s verb=%s pid=%d ppid=%d parent=%s", - r.User, r.Verb, r.PID, r.PPID, r.ParentComm) -} - -// parentComm reads /proc//comm (best-effort; "" on failure). -func parentComm(ppid int) string { - b, err := os.ReadFile(fmt.Sprintf("/proc/%d/comm", ppid)) - if err != nil { - return "" - } - return strings.TrimSpace(string(b)) -} - -// writeOpLog appends one privacy-aware line to the user's op-log (best-effort; -// never blocks or fails the command). Goes to syslog so it ships to Loki. -func writeOpLog(r opRecord) { - exec.Command("logger", "-t", "homelab-vault", opLogLine(r)).Run() // best-effort -} - -func vaultLockPath(uid string) string { return "/run/user/" + uid + "/homelab-vault.lock" } - -// hardenProcess disables core dumps so a bw/homelab crash can't spill the master -// password to a core file. Best-effort. -func hardenProcess() { - _ = syscall.Setrlimit(syscall.RLIMIT_CORE, &syscall.Rlimit{Cur: 0, Max: 0}) -} - -// withUserLock serializes bw mutations for this user (concurrent Claude sessions -// as the same user otherwise race bw's appdata). Returns an unlock func. -func withUserLock(uid string) (func(), error) { - f, err := os.OpenFile(vaultLockPath(uid), os.O_CREATE|os.O_RDWR, 0600) - if err != nil { - return nil, err - } - if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil { - f.Close() - return nil, err - } - return func() { syscall.Flock(int(f.Fd()), syscall.LOCK_UN); f.Close() }, nil -} - -// session is one usable bw context: the env (with BW_SESSION) ready for `bw get`. -type session struct { - env []string -} - -// openSession resolves creds, ensures login, unlocks, and returns a ready env. -// Caller must hold the user lock. appdata is created on tmpfs (0700). -func openSession(run cmdRunner, user, uid string) (session, error) { - creds, err := loadCreds(run, user) - if err != nil { - return session{}, err - } - appdata := bwAppDataDir(uid) - if err := os.MkdirAll(appdata, 0700); err != nil { - return session{}, fmt.Errorf("create bw appdata %s: %w", appdata, err) - } - loginEnv := bwSecretEnv(appdata, creds, "") - // Ensure server is set and we're logged in (idempotent; ignore "already"). - _, _ = run("bw", []string{"config", "server", "https://vaultwarden.viktorbarzin.me"}, loginEnv) - st, _ := run("bw", bwStatusArgs(), loginEnv) - if bwNeedsLogin(st) { - if _, err := run("bw", bwLoginArgs(), loginEnv); err != nil { - return session{}, fmt.Errorf("bw login --apikey failed (API key valid? run `homelab vault setup`): %w", err) - } - } - sess, err := bwUnlock(run, loginEnv) - if err != nil { - return session{}, err - } - return session{env: bwSecretEnv(appdata, creds, sess)}, nil -} - -type getOpts struct { - name string - field string - json bool -} - -var validGetFields = map[string]bool{"password": true, "username": true, "uri": true, "notes": true, "totp": true} - -func parseGetArgs(args []string) (getOpts, error) { - o := getOpts{field: "password"} - for i := 0; i < len(args); i++ { - a := args[i] - switch { - case a == "--json": - o.json = true - case a == "--field" && i+1 < len(args): - o.field = args[i+1] - i++ - case strings.HasPrefix(a, "--field="): - o.field = strings.TrimPrefix(a, "--field=") - case !strings.HasPrefix(a, "-") && o.name == "": - o.name = a - } - } - if o.name == "" { - return o, fmt.Errorf("usage: homelab vault get [--field password|username|uri|notes|totp] [--json]") - } - if !validGetFields[o.field] { - return o, fmt.Errorf("invalid --field %q (want password|username|uri|notes|totp)", o.field) - } - return o, nil -} - -// getValue opens a session and fetches one field. Pure of I/O side effects -// besides the runner, so it is unit-tested with a fake runner. -func getValue(run cmdRunner, user, uid string, o getOpts) (string, error) { - s, err := openSession(run, user, uid) - if err != nil { - return "", err - } - return bwGet(run, s.env, o.field, o.name) -} - -// clipboardDecision picks how to return a secret value. "stdout" prints it (a -// pipe/agent — the intended machine path); "clipboard" copies via OSC52; -// "refuse" emits nothing sensitive (would otherwise risk dumping the secret's -// base64 into scrollback, or silently fail because the OSC52 escape goes to a -// non-terminal stderr). -func clipboardDecision(stdoutTTY, stderrTTY bool, term, termProgram string) string { - if !stdoutTTY { - return "stdout" - } - if terminalAllowed(term, termProgram) && stderrTTY { - return "clipboard" - } - return "refuse" -} - -// jsonToStdoutOK reports whether `--json` may print the secret to stdout — only -// when stdout is NOT a terminal (i.e. piped to a machine consumer). -func jsonToStdoutOK(stdoutTTY bool) bool { return !stdoutTTY } - -// emitSecret returns a value TTY-aware (see clipboardDecision). Never prints the -// secret to a terminal's stdout/scrollback. -func emitSecret(value string) { - switch clipboardDecision(stdoutIsTTY(), stderrIsTTY(), os.Getenv("TERM"), os.Getenv("TERM_PROGRAM")) { - case "stdout": - fmt.Println(value) - case "clipboard": - fmt.Fprint(os.Stderr, osc52(value)) - fmt.Fprintln(os.Stderr, "copied to clipboard; clearing in 30s") - clearClipboardAfter(30) - default: // refuse - fmt.Fprintln(os.Stderr, "refusing to print secret: this terminal can't do OSC52 clipboard safely; pipe the command (e.g. | cat) or use a supported terminal") - } -} - -// clearClipboardAfter spawns a detached background clear so the secret doesn't -// linger in the clipboard. Best-effort. -func clearClipboardAfter(seconds int) { - exec.Command("sh", "-c", fmt.Sprintf("sleep %d; printf '%s'", seconds, osc52clear())).Start() -} - -// listNames extracts "name (id)" from `bw list items` JSON; never values. -func listNames(jsonOut string) []string { - var items []struct { - ID string `json:"id"` - Name string `json:"name"` - } - if err := json.Unmarshal([]byte(jsonOut), &items); err != nil { - return nil - } - out := make([]string, 0, len(items)) - for _, it := range items { - out = append(out, fmt.Sprintf("%s (%s)", it.Name, it.ID)) - } - return out -} - -func runList(run cmdRunner, user, uid, search string) ([]string, error) { - s, err := openSession(run, user, uid) - if err != nil { - return nil, err - } - out, err := run("bw", bwListArgs(search), s.env) - if err != nil { - return nil, err - } - return listNames(out), nil -} - -func vaultList(args []string) error { - hardenProcess() - search := "" - for i := 0; i < len(args); i++ { - if args[i] == "--search" && i+1 < len(args) { - search = args[i+1] - i++ - } else if strings.HasPrefix(args[i], "--search=") { - search = strings.TrimPrefix(args[i], "--search=") - } - } - uid := vaultCurrentUID() - unlock, err := withUserLock(uid) - if err != nil { - return err - } - defer unlock() - names, err := runList(realRunner, vaultCurrentUser(), uid, search) - if err != nil { - return err - } - for _, n := range names { - fmt.Println(n) - } - return nil -} - -func vaultSearch(args []string) error { - if len(args) == 0 { - return fmt.Errorf("usage: homelab vault search ") - } - return vaultList([]string{"--search", strings.Join(args, " ")}) -} - -func vaultCode(args []string) error { - hardenProcess() - if len(args) == 0 { - return fmt.Errorf("usage: homelab vault code ") - } - name := args[0] - uid := vaultCurrentUID() - unlock, err := withUserLock(uid) - if err != nil { - return err - } - defer unlock() - user := vaultCurrentUser() - val, err := getValue(realRunner, user, uid, getOpts{name: name, field: "totp"}) - if err != nil { - return err - } - // TOTP is the most sensitive op: log AND emit an ntfy-bound marker (spec §9a-d). - writeOpLog(opRecord{User: user, Verb: "code", PID: os.Getpid(), PPID: os.Getppid(), ParentComm: parentComm(os.Getppid()), ItemName: name}) - exec.Command("logger", "-t", "homelab-vault-totp", "user="+user+" totp-fetch parent="+parentComm(os.Getppid())).Run() - emitSecret(val) - return nil -} - -// statusSummary reports config/reachability without revealing secrets. -func statusSummary(run cmdRunner, user, uid string) string { - if _, err := loadCreds(run, user); err != nil { - return "vault: not configured — run `homelab vault setup`" - } - s, err := openSession(run, user, uid) - if err != nil { - return "vault: configured, but unlock/login FAILED (creds stale? run `homelab vault setup`): " + err.Error() - } - if _, err := run("bw", []string{"sync"}, s.env); err != nil { - return "vault: configured + unlocked, but sync/reachability failed: " + err.Error() - } - return "vault: configured, unlocked, reachable ✓" -} - -func vaultStatus(args []string) error { - hardenProcess() - uid := vaultCurrentUID() - unlock, err := withUserLock(uid) - if err != nil { - return err - } - defer unlock() - fmt.Println(statusSummary(realRunner, vaultCurrentUser(), uid)) - return nil -} - -func vaultLock(args []string) error { - uid := vaultCurrentUID() - unlock, err := withUserLock(uid) // logout mutates bw state — serialize with get/list - if err != nil { - return err - } - defer unlock() - appdata := bwAppDataDir(uid) - _, _ = realRunner("bw", []string{"lock"}, bwBaseEnv(appdata)) - _, logoutErr := realRunner("bw", []string{"logout"}, bwBaseEnv(appdata)) - if logoutErr == nil { - fmt.Println("locked") - } - return nil // lock/logout best-effort; never error the caller -} - -// vaultPatchPublicArgs writes the non-secret identifiers via argv. Neither the -// email nor the API client_id is a usable credential on its own. -func vaultPatchPublicArgs(user, email, clientID string) []string { - return []string{"kv", "patch", vwCredsPath(user), - "vaultwarden_email=" + email, - "vaultwarden_client_id=" + clientID, - } -} - -// vaultPatchSecretArgs writes ONE secret value via the `key=-` stdin form, so -// the value never appears in argv (ps / /proc//cmdline). The value is fed -// on stdin by realRunnerStdin. -func vaultPatchSecretArgs(user, key string) []string { - return []string{"kv", "patch", vwCredsPath(user), key + "=-"} -} - -// writeCreds stores all four fields in the user's Vault path. The two real -// secrets (master password, API client_secret) go via stdin — never argv. -func writeCreds(user string, c vwCreds) error { - if _, err := realRunner("vault", vaultPatchPublicArgs(user, c.Email, c.ClientID), nil); err != nil { - return err - } - if _, err := realRunnerStdin("vault", vaultPatchSecretArgs(user, "vaultwarden_master_password"), nil, c.MasterPassword); err != nil { - return err - } - if _, err := realRunnerStdin("vault", vaultPatchSecretArgs(user, "vaultwarden_client_secret"), nil, c.ClientSecret); err != nil { - return err - } - return nil -} - -// promptNoEcho reads one line without terminal echo (for the master password). -func promptNoEcho(prompt string) (string, error) { - fmt.Fprint(os.Stderr, prompt) - exec.Command("stty", "-echo").Run() - defer func() { exec.Command("stty", "echo").Run(); fmt.Fprintln(os.Stderr) }() - r := bufio.NewReader(os.Stdin) - line, err := r.ReadString('\n') - // Trim only the line terminator — a master password / API secret may - // legitimately contain leading/trailing spaces. - return strings.TrimRight(line, "\r\n"), err -} - -func promptLine(prompt string) (string, error) { - fmt.Fprint(os.Stderr, prompt) - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - return strings.TrimSpace(line), err -} - -func vaultSetup(args []string) error { - hardenProcess() - fmt.Fprintln(os.Stderr, "One-time setup. Stored ONLY in your own Vault path; the admin never sees it.") - fmt.Fprintln(os.Stderr, "Get your API key at https://vaultwarden.viktorbarzin.me → Settings → Security → Keys → View API key.") - email, err := promptLine("Vaultwarden email: ") - if err != nil { - return err - } - clientID, err := promptLine("API key client_id (user.xxxx): ") - if err != nil { - return err - } - clientSecret, err := promptNoEcho("API key client_secret: ") - if err != nil { - return err - } - master, err := promptNoEcho("Master password: ") - if err != nil { - return err - } - if master == "" || clientID == "" || clientSecret == "" { - return fmt.Errorf("all fields are required") - } - c := vwCreds{Email: email, MasterPassword: master, ClientID: clientID, ClientSecret: clientSecret} - if err := writeCreds(vaultCurrentUser(), c); err != nil { - return fmt.Errorf("writing creds to your Vault path failed (scoped token present?): %w", err) - } - fmt.Fprintln(os.Stderr, "Stored. Verifying unlock…") - uid := vaultCurrentUID() - unlock, err := withUserLock(uid) - if err != nil { - return err - } - defer unlock() - if _, err := openSession(realRunner, vaultCurrentUser(), uid); err != nil { - return fmt.Errorf("stored, but verification failed — double-check master password / API key: %w", err) - } - fmt.Fprintln(os.Stderr, "✓ Verified. Fetches are now AFK.") - return nil -} - -func vaultGet(args []string) error { - hardenProcess() - o, err := parseGetArgs(args) - if err != nil { - return err - } - uid := vaultCurrentUID() - unlock, err := withUserLock(uid) - if err != nil { - return err - } - defer unlock() - user := vaultCurrentUser() - val, err := getValue(realRunner, user, uid, o) - if err != nil { - return err - } - writeOpLog(opRecord{User: user, Verb: "get", PID: os.Getpid(), PPID: os.Getppid(), ParentComm: parentComm(os.Getppid()), ItemName: o.name}) - if o.json { - if !jsonToStdoutOK(stdoutIsTTY()) { - return fmt.Errorf("refusing to print a secret as JSON to a terminal; pipe it (e.g. | cat) or drop --json") - } - fmt.Printf("{%q:%q}\n", o.field, val) - return nil - } - emitSecret(val) - return nil -} - diff --git a/cli/cmd_vault_test.go b/cli/cmd_vault_test.go deleted file mode 100644 index 36aab1f4..00000000 --- a/cli/cmd_vault_test.go +++ /dev/null @@ -1,368 +0,0 @@ -package main - -import ( - "encoding/base64" - "fmt" - "os" - "reflect" - "strings" - "testing" -) - -func TestVaultCommandsRegistered(t *testing.T) { - want := map[string]Tier{ - "vault setup": TierWrite, - "vault status": TierRead, - "vault list": TierRead, - "vault get": TierRead, - "vault search": TierRead, - "vault code": TierRead, - "vault lock": TierWrite, - } - got := map[string]Tier{} - for _, c := range vaultCommands() { - got[c.name()] = c.Tier - } - for name, tier := range want { - if got[name] != tier { - t.Errorf("command %q: tier=%q, want %q (registered=%v)", name, got[name], tier, got[name] != "") - } - } -} - -func TestVaultGroupInRegistry(t *testing.T) { - if !isCommandGroup(buildRegistry(), "vault") { - t.Fatal("`vault` group not wired into buildRegistry()") - } -} - -func TestVaultCredsPath(t *testing.T) { - if got := vwCredsPath("emo"); got != "secret/workstation/claude-users/emo" { - t.Fatalf("vwCredsPath = %q", got) - } -} - -func TestBwAppDataDir(t *testing.T) { - if got := bwAppDataDir("1001"); got != "/run/user/1001/homelab-bw" { - t.Fatalf("bwAppDataDir = %q", got) - } -} - -// fakeRunner records calls and returns canned stdout/err keyed by argv[0]+first arg. -type fakeRunner struct { - calls [][]string - out map[string]string // key: name+" "+strings.Join(argv," ") prefix-matched - err map[string]error - lastEnv []string -} - -func (f *fakeRunner) run(name string, argv, envv []string) (string, error) { - f.calls = append(f.calls, append([]string{name}, argv...)) - f.lastEnv = envv - key := name + " " + strings.Join(argv, " ") - for k, v := range f.out { - if strings.HasPrefix(key, k) { - return v, f.err[k] - } - } - return "", f.err[key] -} - -func TestLoadCredsReadsFourFields(t *testing.T) { - f := &fakeRunner{out: map[string]string{ - "vault kv get -field=vaultwarden_email secret/workstation/claude-users/emo": "emo@x.me", - "vault kv get -field=vaultwarden_master_password secret/workstation/claude-users/emo": "hunter2", - "vault kv get -field=vaultwarden_client_id secret/workstation/claude-users/emo": "user.abc", - "vault kv get -field=vaultwarden_client_secret secret/workstation/claude-users/emo": "sek", - }} - c, err := loadCreds(f.run, "emo") - if err != nil { - t.Fatalf("loadCreds: %v", err) - } - want := vwCreds{Email: "emo@x.me", MasterPassword: "hunter2", ClientID: "user.abc", ClientSecret: "sek"} - if !reflect.DeepEqual(c, want) { - t.Fatalf("loadCreds = %+v want %+v", c, want) - } -} - -func TestLoadCredsUnconfigured(t *testing.T) { - f := &fakeRunner{out: map[string]string{}} // every field empty - if _, err := loadCreds(f.run, "emo"); err == nil || !strings.Contains(err.Error(), "not configured") { - t.Fatalf("want 'not configured' error, got %v", err) - } -} - -func TestBwEnvCarriesSecretsNotArgv(t *testing.T) { - c := vwCreds{ClientID: "user.abc", ClientSecret: "sek", MasterPassword: "hunter2"} - env := bwSecretEnv("/run/user/1001/homelab-bw", c, "SESSIONKEY") - joined := strings.Join(env, "\n") - for _, want := range []string{ - "BW_CLIENTID=user.abc", "BW_CLIENTSECRET=sek", "BW_PASSWORD=hunter2", - "BW_SESSION=SESSIONKEY", "BITWARDENCLI_APPDATA_DIR=/run/user/1001/homelab-bw", - } { - if !strings.Contains(joined, want) { - t.Errorf("bwSecretEnv missing %q", want) - } - } - if strings.Contains(joined, "PATH=") == false { - t.Error("bwSecretEnv must keep a PATH so node/bw resolve") - } -} - -func TestBwGetArgsHasNoSessionInArgv(t *testing.T) { - argv := bwGetArgs("password", "github") - for _, a := range argv { - if strings.Contains(a, "SESSION") || a == "--session" { - t.Fatalf("session must travel via env, not argv: %v", argv) - } - } - if !reflect.DeepEqual(argv, []string{"get", "password", "github"}) { - t.Fatalf("bwGetArgs = %v", argv) - } -} - -func TestBwListArgs(t *testing.T) { - if got := bwListArgs(""); !reflect.DeepEqual(got, []string{"list", "items"}) { - t.Fatalf("bwListArgs('') = %v", got) - } - if got := bwListArgs("git"); !reflect.DeepEqual(got, []string{"list", "items", "--search", "git"}) { - t.Fatalf("bwListArgs('git') = %v", got) - } -} - -func TestBwUnlockReturnsSession(t *testing.T) { - f := &fakeRunner{out: map[string]string{"bw unlock": "THE-SESSION-KEY"}} - env := bwSecretEnv("/run/user/1001/homelab-bw", vwCreds{MasterPassword: "pw"}, "") - sess, err := bwUnlock(f.run, env) - if err != nil || sess != "THE-SESSION-KEY" { - t.Fatalf("bwUnlock = %q, %v", sess, err) - } - // argv must use --passwordenv + --raw, never the password literal - last := f.calls[len(f.calls)-1] - if strings.Join(last, " ") != "bw unlock --passwordenv BW_PASSWORD --raw" { - t.Fatalf("unlock argv = %v", last) - } -} - -func TestReturnMode(t *testing.T) { - if returnMode(true) != "clipboard" || returnMode(false) != "stdout" { - t.Fatal("returnMode wrong") - } -} - -func TestOSC52Encode(t *testing.T) { - got := osc52("secret") - want := "\x1b]52;c;" + base64.StdEncoding.EncodeToString([]byte("secret")) + "\a" - if got != want { - t.Fatalf("osc52 = %q want %q", got, want) - } - if osc52clear() != "\x1b]52;c;\a" { - t.Fatalf("osc52clear wrong: %q", osc52clear()) - } -} - -func TestTerminalAllowed(t *testing.T) { - allow := []struct{ term, prog string }{ - {"xterm-kitty", ""}, {"alacritty", ""}, {"foot", ""}, {"tmux-256color", ""}, - {"screen-256color", ""}, {"xterm-256color", "WezTerm"}, {"xterm-256color", "ghostty"}, - } - for _, c := range allow { - if !terminalAllowed(c.term, c.prog) { - t.Errorf("terminalAllowed(%q,%q) = false, want true", c.term, c.prog) - } - } - deny := []struct{ term, prog string }{{"dumb", ""}, {"", ""}, {"vt100", ""}} - for _, c := range deny { - if terminalAllowed(c.term, c.prog) { - t.Errorf("terminalAllowed(%q,%q) = true, want false", c.term, c.prog) - } - } -} - -func TestOpLogLineHasNoSecretOrItem(t *testing.T) { - line := opLogLine(opRecord{User: "emo", Verb: "get", PID: 10, PPID: 9, ParentComm: "claude", ItemName: "Chase Bank"}) - for _, must := range []string{"user=emo", "verb=get", "ppid=9", "parent=claude"} { - if !strings.Contains(line, must) { - t.Errorf("op-log missing %q: %s", must, line) - } - } - for _, mustNot := range []string{"Chase", "password", "secret"} { - if strings.Contains(line, mustNot) { - t.Fatalf("op-log LEAKS %q (privacy violation): %s", mustNot, line) - } - } -} - -func TestLockPath(t *testing.T) { - if got := vaultLockPath("1001"); got != "/run/user/1001/homelab-vault.lock" { - t.Fatalf("vaultLockPath = %q", got) - } -} - -func TestParseGetArgs(t *testing.T) { - o, err := parseGetArgs([]string{"github", "--field", "username", "--json"}) - if err != nil || o.name != "github" || o.field != "username" || !o.json { - t.Fatalf("parseGetArgs = %+v err=%v", o, err) - } - d, _ := parseGetArgs([]string{"github"}) - if d.field != "password" || d.json { - t.Fatalf("defaults wrong: %+v", d) - } - if _, err := parseGetArgs([]string{}); err == nil { - t.Fatal("get with no name must error") - } - if _, err := parseGetArgs([]string{"x", "--field", "evil"}); err == nil { - t.Fatal("invalid --field must error") - } -} - -func TestListNamesParsing(t *testing.T) { - // bw list items returns JSON; listNames extracts name + id only. - js := `[{"id":"1","name":"GitHub","login":{"username":"u"}},{"id":"2","name":"AWS"}]` - names := listNames(js) - if len(names) != 2 || names[0] != "GitHub (1)" || names[1] != "AWS (2)" { - t.Fatalf("listNames = %v", names) - } -} - -func TestStatusSummaryUnconfigured(t *testing.T) { - f := &fakeRunner{out: map[string]string{}} // no creds - s := statusSummary(f.run, "emo", "1001") - if !strings.Contains(s, "not configured") { - t.Fatalf("status = %q", s) - } -} - -func TestVaultPatchPublicArgs(t *testing.T) { - got := vaultPatchPublicArgs("emo", "e@x.me", "user.ci") - want := []string{"kv", "patch", "secret/workstation/claude-users/emo", - "vaultwarden_email=e@x.me", "vaultwarden_client_id=user.ci"} - if !reflect.DeepEqual(got, want) { - t.Fatalf("vaultPatchPublicArgs = %v", got) - } - for _, a := range got { - if strings.Contains(a, "master_password") || strings.Contains(a, "client_secret") { - t.Fatalf("secret key leaked into public argv: %v", got) - } - } -} - -func TestVaultPatchSecretArgsNoValueInArgv(t *testing.T) { - for _, key := range []string{"vaultwarden_master_password", "vaultwarden_client_secret"} { - got := vaultPatchSecretArgs("emo", key) - want := []string{"kv", "patch", "secret/workstation/claude-users/emo", key + "=-"} - if !reflect.DeepEqual(got, want) { - t.Fatalf("vaultPatchSecretArgs(%q) = %v", key, got) - } - if got[len(got)-1] != key+"=-" { - t.Fatalf("secret value must be read from stdin (`%s=-`), got %v", key, got) - } - } -} - -// TestNoSecretInArgvAcrossFlow is the load-bearing security test: across the -// whole get flow (vault reads, bw config/status/login/unlock/get) NO secret -// value may appear in any command's argv — secrets travel via env/stdin only. -func TestNoSecretInArgvAcrossFlow(t *testing.T) { - uid := fmt.Sprintf("%d", os.Getuid()) - f := &fakeRunner{out: map[string]string{ - "vault kv get -field=vaultwarden_master_password secret/workstation/claude-users/emo": "SUPERSECRETPW", - "vault kv get -field=vaultwarden_client_id secret/workstation/claude-users/emo": "user.x", - "vault kv get -field=vaultwarden_client_secret secret/workstation/claude-users/emo": "CLIENTSEKRET", - "bw status": `{"status":"locked"}`, - "bw unlock": "SESSIONXYZ", - "bw get password github": "p@ss", - }} - if _, err := getValue(f.run, "emo", uid, getOpts{name: "github", field: "password"}); err != nil { - t.Fatalf("getValue: %v", err) - } - for _, call := range f.calls { - for _, arg := range call { - for _, s := range []string{"SUPERSECRETPW", "CLIENTSEKRET", "SESSIONXYZ"} { - if strings.Contains(arg, s) { - t.Errorf("secret %q leaked into argv: %v", s, call) - } - } - } - } - if !strings.Contains(strings.Join(f.lastEnv, "\n"), "BW_SESSION=SESSIONXYZ") { - t.Error("expected BW_SESSION in the bw get env (test would be vacuous otherwise)") - } -} - -func TestClipboardDecision(t *testing.T) { - cases := []struct { - stdoutTTY, stderrTTY bool - term, prog, want string - }{ - {false, true, "xterm-kitty", "", "stdout"}, - {true, true, "xterm-kitty", "", "clipboard"}, - {true, true, "dumb", "", "refuse"}, - {true, false, "xterm-kitty", "", "refuse"}, - } - for _, c := range cases { - if got := clipboardDecision(c.stdoutTTY, c.stderrTTY, c.term, c.prog); got != c.want { - t.Errorf("clipboardDecision(%v,%v,%q) = %q, want %q", c.stdoutTTY, c.stderrTTY, c.term, got, c.want) - } - } -} - -func TestJSONToStdoutOK(t *testing.T) { - if jsonToStdoutOK(true) { - t.Error("must refuse JSON secret on a terminal") - } - if !jsonToStdoutOK(false) { - t.Error("must allow JSON when piped") - } -} - -func TestBwNeedsLogin(t *testing.T) { - if !bwNeedsLogin(`{"status":"unauthenticated"}`) { - t.Error("unauthenticated → needs login") - } - if bwNeedsLogin(`{"status":"locked"}`) { - t.Error("locked → no login (just unlock)") - } - if bwNeedsLogin(`{"status":"unlocked"}`) { - t.Error("unlocked → no login") - } - if !bwNeedsLogin(`not json`) { - t.Error("unparseable → attempt login") - } -} - -func TestVaultHelpMentionsSecurity(t *testing.T) { - h := vaultHelp() - for _, want := range []string{"homelab vault get", "no-HITL", "your own", "setup"} { - if !strings.Contains(h, want) { - t.Errorf("vault help missing %q", want) - } - } -} - -func TestVaultBareGroupRegistered(t *testing.T) { - for _, c := range vaultCommands() { - if len(c.Path) == 1 && c.Path[0] == "vault" { - return - } - } - t.Fatal("bare `vault` help command not registered") -} - -// getValue is the testable core: given a runner + opts, returns the secret value. -func TestGetValueFlow(t *testing.T) { - f := &fakeRunner{out: map[string]string{ - "vault kv get -field=vaultwarden_master_password secret/workstation/claude-users/emo": "pw", - "vault kv get -field=vaultwarden_client_id secret/workstation/claude-users/emo": "user.x", - "vault kv get -field=vaultwarden_client_secret secret/workstation/claude-users/emo": "cs", - "bw status": `{"status":"locked"}`, - "bw unlock": "SESS", - "bw get password github": "p@ss", - }} - // Use real UID so os.MkdirAll(/run/user//homelab-bw) succeeds. - uid := fmt.Sprintf("%d", os.Getuid()) - val, err := getValue(f.run, "emo", uid, getOpts{name: "github", field: "password"}) - if err != nil || val != "p@ss" { - t.Fatalf("getValue = %q, %v", val, err) - } -} diff --git a/cli/homelab.go b/cli/homelab.go index 62c0c8aa..fb12a169 100644 --- a/cli/homelab.go +++ b/cli/homelab.go @@ -22,8 +22,6 @@ func buildRegistry() []Command { reg = append(reg, obsCommands()...) reg = append(reg, usageCommands()...) reg = append(reg, haCommands()...) - reg = append(reg, browserCommands()...) - reg = append(reg, vaultCommands()...) return reg } diff --git a/docs/adr/0013-homelab-browser-verbs.md b/docs/adr/0013-homelab-browser-verbs.md deleted file mode 100644 index bba4e8e7..00000000 --- a/docs/adr/0013-homelab-browser-verbs.md +++ /dev/null @@ -1,75 +0,0 @@ -# homelab browser verbs: headful (anti-bot) web automation via cluster Chrome - -v0.8 adds `browser run`, `browser open`, and `browser --help`. They package a -capability that already existed but was undiscoverable: driving the cluster's -**headful** Chrome (`chrome-service` — real Chrome under Xvfb, CDP on -`svc/chrome-service:9222`) from the devvm, for sites that detect and block -headless automation. - -## Motivating incident (2026-06-22) - -Logging a washing-machine repair on the Stirling Ackroyd **Fixflo** tenant -portal: the headless `@playwright/mcp` browser loaded the site and filled the -entire multi-step form, but the **final submit silently failed** — Fixflo's -pre-submit `POST /IssuePreCreationCheck` returned `net::ERR_FILE_NOT_FOUND`, the -spinner hung, no issue was created. Root cause = headless-Chrome detection. The -fix was to drive the headful `chrome-service` over `connect_over_cdp` — it -submitted first try (Fixflo ref IS22657587). That capability was documented -(`docs/architecture/chrome-service.md`) but **not packaged or discoverable**, so -it took ~40 min, three redundant full form re-runs, and a user hint. The agent -also misread `ERR_FILE_NOT_FOUND` as "network egress" and retried blind instead -of inspecting the network panel. - -## Decisions - -- **Mechanics in `homelab`, not a `~/.claude` skill.** A standalone skill was - rejected: the CLI is run every session (so the verb is *discoverable*), is - versioned, multi-user, and test-covered. A private, untested skill is none of - those. The command owns only the deterministic *mechanics* (port-forward, - stealth injection, lifecycle) — the agent supplies the Playwright script, so - *judgment* stays out of the CLI (the founding rule, ADR-0004/0005). -- **The failure was judgment, not setup friction**, so the CLI is paired with a - one-line pointer in always-in-context `~/code/CLAUDE.md` and a diagnostic - payload in `browser --help`: the *when-to-use* signature (a site loads but a - gated action fails/hangs, or one request 500s/aborts while siblings 200 → - suspect headless detection) and an error-code cheat-sheet (`ERR_FILE_NOT_FOUND` - = request resolved/intercepted by the automation layer, **not** egress; - egress failures are `ERR_CONNECTION_REFUSED`/`_TIMED_OUT`/`_NAME_NOT_RESOLVED` - and would break the page load too). A command the agent doesn't think to run is - useless; the cheat-sheet is the actual fix for the misdiagnosis. -- **Reach the pod via `kubectl port-forward`, then `connect_over_cdp` to - localhost.** port-forward tunnels API-server→pod, so it **bypasses the `:9222` - NetworkPolicy** that gates in-cluster callers — the devvm needs no namespace - label. Readiness is asserted against `/json/version`: the endpoint must report - a real `Chrome/…`, never `HeadlessChrome` (the whole point). The forward is - **always** torn down (process-group kill + signal handler), on success and on - error — an acceptance requirement. -- **Default to a fresh incognito context; `--shared-context` opts into the warmed - profile.** chrome-service is a single shared browser with a persistent profile. - A fresh, always-closed context is safe for concurrent callers (tripit's fare - scrape connects per-quote) and is what production already does. The warmed - persistent profile (cookies from a manual noVNC login) is opt-in for flows that - need a pre-logged-in session. -- **Pin the node CDP client to `playwright-core@1.48.2`** to match the - chrome-service image minor (`mcr.microsoft.com/playwright:v1.48.0-noble`, - Chromium 130). `connect_over_cdp` speaks the browser's CDP, and protocol - changes between Playwright minors — the devvm's ambient Python Playwright was - 1.58, a 10-minor skew. The pin makes behaviour deterministic across the fleet - regardless of local drift. `playwright-core` (not `playwright`) because no - browser binary is needed — we connect to the remote one. -- **Self-provision the client lazily, no per-user setup.** The pinned client is - installed once into `~/.cache/homelab/browser-client/` (idempotent, version- - guarded) on first use, alongside the embedded runner + stealth files. node is - already fleet-wide; this avoids coupling the feature to a provisioner change - and keeps it self-contained and self-healing. The client runs on the devvm, so - `setInputFiles` streams local files to the remote browser over CDP — no - `chmod`/staging-dir workaround on the CDP path. -- **Vendor `stealth.js`, guard against drift.** The CLI embeds a byte-for-byte - copy of `stacks/chrome-service/files/stealth.js` (the source of truth the - in-cluster callers use) via `go:embed`; a unit test fails if the copy drifts. - `go:embed` can't reach outside the package dir, hence the vendored copy rather - than a path reference. -- **Scope held at two action verbs + help.** `run` (arbitrary script — the - workhorse) and `open` (navigate + title/text/screenshot — a quick check) cover - the surface. Both are write-tier; the bare `browser`/`--help` is read. Re-measure - via `usage top` (ADR-0011) before adding more. diff --git a/docs/adr/0014-service-identity-and-east-west-observability.md b/docs/adr/0014-service-identity-and-east-west-observability.md deleted file mode 100644 index 5eb1c83a..00000000 --- a/docs/adr/0014-service-identity-and-east-west-observability.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -status: accepted -date: 2026-06-24 ---- - -# Service identity is namespace + label; east-west observability via Calico Goldmane; no service mesh - -As the Service count grows we want an audit-grade record of which Service talks to which — the "service mesh evaluation" `docs/plans/2026-04-20-infra-audit-design.md` flagged as never done ("worth a design doc even if the answer is no, too much complexity for the gain"). We evaluated the full design space against two constraints: the trust model is a single-tenant cluster needing **attribution-grade** forensics (reconstruct events in a cluster we trust), not cryptographic non-repudiation against a hostile pod; and we are acutely **etcd-constrained** (we removed VPA/Goldilocks for exactly this, and carry open beads `code-oflt`/`code-at4f` on etcd starvation). Decision: **service identity = the workload's namespace** (primary; Goldmane stamps it natively and "one Service ≈ one namespace" holds for ~87 of our namespaces), refined by an explicit `service-identity` label only in the few genuinely multi-Service namespaces (`monitoring`, `kube-system`, `dbaas`). **East-west observability = Calico 3.30 Goldmane + Whisker** (already in our Calico v3.30.7, currently `enabled = false` in `stacks/calico/main.tf`), with Goldmane's emitter shipping flows to **Loki** for a durable trail. **Enforcement reuses the existing Wave 1 observe-then-enforce egress track**, now selecting on namespace/label and fed by Goldmane's allow/deny + policy-trace flows. We explicitly **reject** a service mesh, mTLS, SPIFFE/SPIRE, and dedicated per-Service ServiceAccounts for now. - -## Considered options - -- **Dedicated per-Service ServiceAccount as the identity primitive** — initially chosen, then reversed. 56% of pods (257/458) run as `default`, so it is a ~116-stack rollout; and Goldmane (the chosen flow source) carries pod/namespace/workload **labels but no ServiceAccount field**, so SAs would not even reach the audit trail without a custom pod→SA mapping. The cheaper, etcd-inert path (namespace is free; a handful of static labels) delivers the same attribution. Deferred until identity-aware NetworkPolicy needs a principal finer than namespace/label, or mTLS is adopted. -- **Service mesh (Istio / Linkerd / Cilium-mesh) + mTLS + SPIFFE/SPIRE** — the only thing that makes the trail cryptographically non-repudiable against a hostile pod. The trust model does not justify it, east-west stays single-tenant plaintext, and it is precisely the "too much complexity for the gain" the audit doc predicted. Rejected. -- **Microsoft Retina (CNI-agnostic eBPF)** — more capable (DNS, drops, Hubble UI) and GA, runs on Calico without a CNI change. But identity-rich mode writes **one `RetinaEndpoint` CRD per pod to etcd** (continuous, pod-proportional churn — the exact axis we guard), and it is metrics-first, not log-first (no per-flow Loki records without custom glue). Rejected for this use case; noted as the fallback if DNS/drop-level detail is ever needed. -- **Cilium Hubble** — reads Cilium's eBPF datapath maps; unusable on Calico without migrating the CNI. A CNI migration is not justified. Rejected. -- **Kiali** — builds its graph entirely from an Istio mesh's Prometheus telemetry; no mesh, no graph. Rejected. -- **Custom Grafana Alloy enrichment exporter over raw iptables-`LOG` flow lines** — Alloy has no IP→identity dictionary-lookup primitive (`loki.process` lacks a lookup stage; `k8sattributes` can't do per-line/dual-IP association), so this is a multi-day custom build that also has to beat pod-IP churn. Goldmane delivers identity-stamped flows natively and obviates it. Rejected. -- **Kyverno generate+mutate to provision/assign identity** — rejected on etcd grounds: background scans + PolicyReports + UpdateRequests are continuous writes, the VPA-class cost we shed. Identity stays static. - -## Consequences - -- **No etcd cost from the flow plane.** Goldmane streams flows from Felix (the existing `calico-node` DaemonSet) over gRPC into a ~60-minute in-memory ring buffer — nothing written to etcd or the K8s API. Steady-state cost is two Deployments (`goldmane`, `whisker`) + RAM/CPU on the goldmane pod. -- **The ring buffer is not a trail.** Durable, queryable history depends on the emitter→Loki path (reuse the 90-day security-stream retention); on a Goldmane restart the in-memory window is lost. -- **Goldmane is tech-preview** in OSS Calico 3.30 — the main risk. Enabling it is a reversible toggle in `stacks/calico/main.tf`, but the toggle interacts with the operator-managed Installation CR (only namespaces are TF-adopted today; verify how Goldmane/Whisker are enabled before applying). -- **Attribution is namespace-grained for free** across ~87 single-Service namespaces. Multi-Service namespaces (`monitoring`, `kube-system`, `dbaas`) need a `service-identity` label to disambiguate; most are platform/infra and already on the Wave 1 enforcement exclude list. -- **The trail is attribution-grade, not cryptographic.** It reliably reconstructs events in a trusted cluster but cannot prove identity against a pod that spoofs its source — an accepted limit of the trust model. This ADR does not change the east-west encryption posture (still plaintext, no mTLS). -- **Enforcement gains a better data source.** Goldmane's allow/deny + policy-trace flows build the Wave 1 empirical egress allowlist faster than the current iptables-`LOG`→journald→Loki path, and policies select on namespace/label with no SA dependency. -- **New ubiquitous language** recorded in `CONTEXT.md`: **Service identity** and **Goldmane / Whisker**. -- **Revisit triggers:** adopt dedicated per-Service SAs if identity-aware NetworkPolicy needs a principal finer than namespace/label, or if mTLS is ever required; reconsider Retina if DNS/drop-level flow detail becomes necessary. diff --git a/docs/architecture/automated-upgrades.md b/docs/architecture/automated-upgrades.md index c0200d84..0b8837cb 100644 --- a/docs/architecture/automated-upgrades.md +++ b/docs/architecture/automated-upgrades.md @@ -319,7 +319,7 @@ each Job's pod and its drain target are always different nodes. - `K8sVersionSkew` — kubelet/apiserver `gitVersion` count >1 for 30m. Catches a half-done rollout. - `EtcdPreUpgradeSnapshotMissing` — `k8s_upgrade_in_flight==1 && k8s_upgrade_snapshot_taken==0` for 10m. Catches preflight failing silently. - `K8sUpgradeStalled` — `k8s_upgrade_in_flight==1 && time()-k8s_upgrade_started_timestamp > 5400` for 5m. Catches a chain Job dying without spawning its successor. - - `K8sUpgradeChainJobFailed` — `(kube_job_status_failed{namespace="k8s-upgrade",job_name=~"k8s-upgrade-(preflight|master|worker|postflight)-.*",reason=~"BackoffLimitExceeded|DeadlineExceeded"} > 0) unless on() (k8s_upgrade_blocked == 1)` for 15m (warning). Catches a phase Job that terminally failed **before `in_flight` was set** (the preflight gates exit pre-metric) — invisible to the two `in_flight`-based alerts above; this was the blind spot behind the 5-day 1.34.9 preflight wedge. Reason-scoped so a retry-success doesn't false-positive (and so it doesn't needlessly block kured). The `unless k8s_upgrade_blocked == 1` clause (2026-06-21) excludes a deliberate compat-gate refusal (owned by `K8sUpgradeBlocked`) so a block doesn't double-fire as a wedge. + - `K8sUpgradeChainJobFailed` — `kube_job_status_failed{namespace="k8s-upgrade",job_name=~"k8s-upgrade-.*",reason=~"BackoffLimitExceeded|DeadlineExceeded"} > 0` for 15m (warning). Catches a phase Job that terminally failed **before `in_flight` was set** (the preflight gates exit pre-metric) — invisible to the two `in_flight`-based alerts above; this was the blind spot behind the 5-day 1.34.9 preflight wedge. Reason-scoped so a retry-success doesn't false-positive (and so it doesn't needlessly block kured). - **Pushgateway metrics**: - `k8s_upgrade_in_flight` (set in preflight, cleared in postflight) - `k8s_upgrade_snapshot_taken` (set after etcd snapshot Job completes with ≥1 KiB) diff --git a/docs/architecture/chrome-service.md b/docs/architecture/chrome-service.md index 6f9c1ee4..7b95d4a0 100644 --- a/docs/architecture/chrome-service.md +++ b/docs/architecture/chrome-service.md @@ -112,32 +112,17 @@ External caller (dev box): @playwright/mcp --isolated --storage-state ~/.cache/...storage-state.json ``` -## Browser binary — real Google Chrome (for proprietary codecs) - -The chrome-service container runs **real Google Chrome**, not the bundled -Chromium, via the infra-owned image `ghcr.io/viktorbarzin/chrome-service-browser` -(`files/chrome/Dockerfile` = `mcr.microsoft.com/playwright:v1.48.0-noble` + -`google-chrome-stable`, built by `.github/workflows/build-chrome-service-browser.yml`). -The launch resolves `CHROMIUM=/opt/google/chrome/chrome`. - -**Why:** the Playwright-bundled Chromium has proprietary codecs **compiled out**, -so H.264/AAC video (Instagram Reels, X, most `.mp4`) fails in the noVNC view with -`MEDIA_ERR_SRC_NOT_SUPPORTED` (the bytes download `200 video/mp4` but there's no -decoder — NOT a GPU issue). Royalty-free codecs (VP9/VP8/AV1 → YouTube) always -worked. Swapping `libffmpeg.so` does NOT help (codecs are compiled out, not just -the lib stripped) and Chrome-for-Testing is also codec-less — only -`google-chrome-stable` carries them. - ## Image pin -The Playwright base + the Python client (`playwright==1.48.0` in callers' -`requirements.txt`) and the snapshot sidecars -(`mcr.microsoft.com/playwright/python:v1.48.0-noble`) historically had to match -minor-versions. The chrome-service browser is now real Google Chrome (a newer -milestone than the 1.48 Chromium), but the `connect_over_cdp` callers (tripit -fare scrape, `homelab browser`, snapshot-harvester) attach over raw CDP, which is -version-tolerant — verified working against this Chrome. If a future Chrome -milestone breaks a caller, pin Chrome in the Dockerfile or bump the clients. +Both the server image (`mcr.microsoft.com/playwright:v1.48.0-noble` in +`stacks/chrome-service/main.tf`) and the Python client +(`playwright==1.48.0` in callers' `requirements.txt`) **must match +minor-versions**. Bump in lockstep — Playwright protocol changes between +minors and the client cannot connect to a mismatched server. + +The harvester + snapshot-server sidecar use +`mcr.microsoft.com/playwright/python:v1.48.0-noble` — same playwright +minor, with Python-side bindings pre-installed. ## Storage @@ -182,29 +167,7 @@ milestone breaks a caller, pin Chrome in the Dockerfile or bump the clients. `x11vnc` (connected to Xvfb on `localhost:6099`) bridged to `websockify` on port 6080. Service `chrome` maps :80 → :6080 and is exposed via `ingress_factory` at `chrome.viktorbarzin.me`, - Authentik-gated. The bare host serves `vnc.html` (image symlinks - `index.html → vnc.html`); add `?autoconnect=true&resize=scale&path=websockify` - to skip the Connect button. The view is **black when no browser window is - open** (idle) — that is normal, not a failed connection. Chrome is launched - with `--window-size=1280,720 --window-position=0,0` to fill the Xvfb screen - (no window manager runs, so without it Chrome opens at its profile-persisted - size and the rest of the framebuffer shows as a black cut-off). - -### noVNC fd-sweep gotcha (stuck "Connecting") - -If the noVNC client hangs on **"Connecting" forever then times out**, the cause -is almost always x11vnc's fd-table sweep: containerd grants pods -`RLIMIT_NOFILE = 2^31`, and x11vnc `fcntl`-sweeps the **entire** fd table on -every client connection, so the RFB handshake never completes (websockify -accepts the WS and logs `connecting to: localhost:5900`, but x11vnc never sends -the `RFB 003.008` banner). Diagnose: `grep "open files" /proc/$(pgrep -n -x11vnc)/limits` (huge = bad) and time the handshake from a sibling container -(`python3 -c "import socket;s=socket.socket();s.connect(('127.0.0.1',5900));print(s.recv(12))"` — -healthy <0.3s, broken hangs). **Fix: cap `ulimit -n 65536` before x11vnc starts** -— done both in `files/novnc/entrypoint.sh` (root) and via the container `command` -wrapper in `main.tf` (so it applies deterministically even though the image is -`:latest`/`IfNotPresent` and won't re-pull a rebuilt entrypoint). Same bug + fix -as the android-emulator stack. + Authentik-gated. - **snapshot-server sidecar** (`mcr.microsoft.com/playwright/python:v1.48.0-noble`) serves `GET /api/snapshot` from `/profile/snapshots/storage-state.json`, bearer-gated by `PW_TOKEN`. Service `chrome-snapshot` maps :8088 → :8088 @@ -217,45 +180,6 @@ as the android-emulator stack. See `stacks/chrome-service/README.md` for the recipe (label namespace, inject `CHROME_CDP_URL`, vendor `stealth.js`). -## Driving from OUTSIDE the cluster (`homelab browser`) - -Agents on the devvm reach this browser through the **`homelab browser`** CLI -(`cli/`, ADR-0013) — the packaged, discoverable form of the ad-hoc -`connect_over_cdp` recipe. It is the **escalation path, not the default**: -agents default to the Playwright MCP / headless browser for all routine -automation, and reach for `homelab browser` ONLY when headless is blocked — a -site loads but a gated action (submit/login) silently fails or hangs, the -signature of headless / anti-bot detection. (Same tiered rule lives in -`~/code/CLAUDE.md` and `homelab browser --help`.) - -```text -devvm: homelab browser run flow.js - │ kubectl port-forward svc/chrome-service :9222 (random local port) - ▼ - http://127.0.0.1: ──► chrome-service pod :9222 (CDP) - │ assert /json/version Browser is "Chrome/…", not "HeadlessChrome" - │ node + playwright-core@1.48.2 → connectOverCDP - │ context.addInitScript(stealth.js) ← same vendored file as in-cluster - │ run the user's Playwright script with page/context/browser in scope - └─ port-forward always torn down (success or error) -``` - -Key facts: - -- **port-forward bypasses the `:9222` NetworkPolicy.** It tunnels - API-server→pod, so the devvm needs no `chrome-service.viktorbarzin.me/client` - label — unlike in-cluster callers. -- **Client pinned to the image minor.** The node client is - `playwright-core@1.48.2` (matches `v1.48.0-noble` / Chromium 130), installed - lazily into `~/.cache/homelab/browser-client/`. Bump it in lockstep when the - server image bumps (same rule as the in-cluster Python clients — see "Image - pin" above). -- **Default context is a fresh incognito one** (closed on exit), safe for the - shared browser; `--shared-context` reuses the warmed persistent profile. -- **`stealth.js` is vendored** into the CLI (`cli/browser_stealth.js`) as a - byte-identical copy of `files/stealth.js`, guarded by a drift test — so the - CLI's stealth never diverges from the in-cluster callers'. - ## Limits + risks - **Anti-bot vs stealth arms race** — when an upstream beats us (DRM diff --git a/docs/architecture/multi-tenancy.md b/docs/architecture/multi-tenancy.md index c64a146c..4f5136f9 100644 --- a/docs/architecture/multi-tenancy.md +++ b/docs/architecture/multi-tenancy.md @@ -543,10 +543,6 @@ Separate from the in-cluster namespace-owner model above, the **devvm** (`10.0.1 **Config inheritance (live):** wizard authors the base (his chezmoi-versioned `~/.claude`). Two native layers carry it to every user — the enforced org `claudeMd` in `/etc/claude-code/managed-settings.json` (top precedence, all sessions) and per-user `~/.claude/{skills,rules,…}` **symlinks** to the base (seeded via `/etc/skel`; edits propagate live). Secrets stay per-user at mode 600, never symlinked. **The managed config self-deploys from the repo** (2026-06-10): the hourly reconcile's `sync_managed_config` installs `scripts/workstation/managed-settings.json` to `/etc/claude-code/` whenever the repo copy changes — so editing the claudeMd = edit + commit, no manual install — and `refresh_codex_mirror` regenerates each user's `~/.codex/AGENTS.md` (a static mirror of the claudeMd; only files carrying the mirror header are touched, user-customized ones are left alone). Repo-level guidance (`.claude/CLAUDE.md`, `AGENTS.md`, `CONTEXT.md` in the infra repo) reaches non-admins through their auto-freshened clones — commit + push and every user has it within the hour. -**Memory — homelab CLI hooks (rolled out 2026-06-21, deploy-fixed 2026-06-22):** the per-user `claude_memory` MCP was retired for the **homelab-memory hooks** — the reconcile's `install_memory` (re)installs four scripts into `~/.claude/hooks/` each run (`homelab-memory-recall.py` UserPromptSubmit recall, `auto-learn.py` Stop-hook extraction, `pre-compact-backup.sh`/`post-compact-recovery.sh`), wires them into `settings.json` if-absent + additive, and removes the old `claude_memory` MCP. **The provisioner binary itself now self-deploys from the repo** (step 0: `bash -n`-gated `install` + re-exec when `scripts/t3-provision-users.sh` differs from `/usr/local/bin/t3-provision-users`, guarded against re-exec loops / DRY_RUN mutation) — added after this very rollout sat committed-but-undeployed for a day (only the manual `setup-devvm.sh` had ever deployed the binary), so the hourly reconcile kept running the pre-memory version and emo/anca silently lost memory (recall + auto-learn never wired). A latent `set -e` abort in `install_memory` (a bare `[[ -d plugin-dir ]] && …` returning non-zero) was also fixed; it had killed the reconcile after the first user the first time it actually ran. The hooks need a `MEMORY_API_KEY` (or `CLAUDE_MEMORY_API_KEY`) in the user's `settings.json` env — the `homelab` CLI defaults the API URL, so **the key is the only hard requirement**; `install_memory` reuses an existing key and only WARNs if absent (it does NOT mint one — that's an admin Vault step, see Remaining). wizard + emo carry a key from their original MCP setup; **ancamilea is keyless → her memory no-ops until a key is minted.** (`auto-learn.py`'s passive store calls the API directly, so it additionally needs `*_API_URL` in env to avoid its local-SQLite fallback; recall + manual `homelab memory store` go through the URL-defaulting CLI and need only the key.) - -**Agent skills — vendored own-copies for an allowlist (2026-06-23):** beyond the config-inheritance base (above, which symlinks the admin's `~/.claude/skills` into every user), the reconcile's `install_skills` gives users on the `SKILL_USERS` allowlist (currently `emo`) their OWN copies of a curated skill set vendored in-repo at `scripts/workstation/claude-skills/` (16: the admin's 15 `mattpocock/skills` + `find-skills` from `vercel-labs/skills`). It copies each into `~/.agents/skills/` (owned by the user, parent `~/.agents` chowned too — `install -d` leaves intermediates root-owned) and points `~/.claude/skills/` at it with a **relative** symlink (`../../.agents/skills/` — the layout `skills add -g` produces; Claude Code reads `~/.claude/skills/`). **Vendored, NOT `npx skills add`:** upstream drifted off this exact set (`diagnose`→`diagnosing-bugs`, `write-a-skill`→`writing-great-skills` renamed; `caveman` + `zoom-out` unpublished), so npx can't reproduce it — and a per-reconcile GitHub clone + unpinned-CLI dependency has no place in the hourly root job; refresh by re-snapshotting (`claude-skills/README.md`). **if-absent keys on the user's OWN copy** (a real dir under `~/.agents/skills`), so a steady-state reconcile is a no-op AND a stale or cross-user `~/.claude/skills` symlink is healed to the own copy — emo had `grill-me`/`file-issue` symlinked into the admin's home; `grill-me` is now emo's own (`file-issue` is outside the set, left as-is). A real dir squatting a name is never clobbered. Best-effort tail (`return 0`, like `install_memory`). Extend coverage = edit `SKILL_USERS`. - **Onboarding state self-heals (2026-06-15):** `~/.claude.json` is a single file that ALL of a user's concurrent `claude` processes (the ttyd terminal + their `t3-serve` instance + agent/SDK sessions) read-modify-write, so a stale writer periodically drops top-level keys — including `hasCompletedOnboarding` — which bounces the next *interactive* session back to the first-run "Choose the text style" wizard even though the user is fully logged in (credentials live in the SEPARATE `~/.claude/.credentials.json`, untouched by the race; first observed for emo 2026-06-15). The launcher (`skel/start-claude.sh`) now idempotently re-asserts `hasCompletedOnboarding` (+ `lastOnboardingVersion`) in `~/.claude.json` right before it runs `claude` — merge-only, never clobbers other keys, no-op if jq is missing or the file is empty/corrupt. And since the launcher is a per-user copy that `/etc/skel` only seeds at account creation, the reconcile's new `deploy_user_launcher` step re-copies `skel/start-claude.sh` into every non-admin home (copy-if-changed) so launcher edits now reach EXISTING users within the hour — `.tmux.conf` is deliberately NOT re-copied (terminal-lobby appends its own managed section to it). **Claude Code runtime — native, per-user (2026-06-15):** `claude` is the **native** install (`~/.local/bin/claude` → `~/.local/share/claude/versions/`, self-updating; `installMethod: native`) — NOT npm-global or npx. It is the runtime for both the ttyd launcher and each `t3-serve` instance. `setup-devvm.sh` installs node ONLY for the `t3` CLI (not claude); per-user native claude is provisioned by the reconcile's `install_user_claude_native` (covers terminal + t3, idempotent, skip-if-present) and self-bootstrapped by `start-claude.sh` on first launch — both via the official `https://claude.ai/install.sh`. The legacy machine-wide `npm install -g @anthropic-ai/claude-code` bootstrap and the launcher's `npx` fallback were removed; existing users had already auto-migrated to native, and the npm-global dir was empty. **PATH (`~/.local/bin`, where the native binary lives):** ensured three ways — `/etc/profile.d/10-local-bin.sh` for login shells (machine-wide, fresh-user-safe), `start-claude.sh` itself (the launcher runs in tmux's non-login env that skips the user's shell rc), and `t3-serve@.service` (`Environment=PATH=…:/home/%i/.local/bin`). diff --git a/docs/architecture/networking.md b/docs/architecture/networking.md index 4659038a..e2c0ac2d 100644 --- a/docs/architecture/networking.md +++ b/docs/architecture/networking.md @@ -4,7 +4,7 @@ Last updated: 2026-04-19 (WS E — Kea DHCP pushes dual DNS per subnet; Kea DDNS ## Overview -The homelab network is built on a dual-VLAN architecture with pfSense providing gateway services, Technitium for internal DNS, and Cloudflare for external DNS. Traefik serves as the Kubernetes ingress controller with a middleware chain of anti-AI bot-blocking, Authentik forward-auth, rate limiting, and retry. CrowdSec IP-reputation enforcement is **out-of-band** (not a Traefik hop): banned IPs are dropped in-kernel via nftables on direct hosts and blocked at the Cloudflare edge on proxied hosts (see `docs/architecture/security.md`). All HTTP traffic flows through Cloudflared tunnels, avoiding the need for port forwarding or exposing public IPs. +The homelab network is built on a dual-VLAN architecture with pfSense providing gateway services, Technitium for internal DNS, and Cloudflare for external DNS. Traefik serves as the Kubernetes ingress controller with a comprehensive middleware chain including CrowdSec bot protection, Authentik forward-auth, and rate limiting. All HTTP traffic flows through Cloudflared tunnels, avoiding the need for port forwarding or exposing public IPs. ## Architecture Diagram @@ -16,14 +16,12 @@ graph TB Traefik[Traefik Ingress
3 replicas + PDB] subgraph "Middleware Chain" - AntiAI[Anti-AI bot-block
fail-open] + CS[CrowdSec Bouncer
fail-open] Auth[Authentik Forward-Auth
3 replicas + PDB] RL[Rate Limiter
429 response] Retry[Retry
2 attempts, 100ms] end - CSdrop[CrowdSec drop
nftables / CF edge
out-of-band, pre-Traefik] - subgraph "Proxmox Host (eno1)" vmbr0[vmbr0 Bridge
192.168.1.127/24] vmbr1[vmbr1 Internal
VLAN-aware] @@ -55,9 +53,8 @@ graph TB Internet -->|DNS query| CF CF -->|CNAME to tunnel| CFD CFD --> Traefik - CSdrop -.->|banned IPs dropped before Traefik| Traefik - Traefik --> AntiAI - AntiAI --> Auth + Traefik --> CS + CS --> Auth Auth --> RL RL --> Retry Retry --> Service @@ -85,7 +82,7 @@ graph TB | Cloudflare DNS | SaaS | External | ~50 public domains under viktorbarzin.me | | Cloudflared | Container | K8s (3 replicas) | Tunnel ingress, replaces port forwarding | | Traefik | Helm chart | K8s (3 replicas + PDB) | Ingress controller, HTTP/3 enabled | -| CrowdSec | Helm chart | K8s (LAPI: 3 replicas) | IP reputation. Out-of-band enforcement: `cs-firewall-bouncer` DaemonSet (in-kernel nftables drop, direct hosts) + Cloudflare edge WAF rule (proxied hosts). Fail-open | +| CrowdSec | Helm chart | K8s (LAPI: 3 replicas) | Bot protection, fail-open bouncer | | Authentik | Helm chart | K8s (3 replicas + PDB) | SSO, forward-auth middleware | | MetalLB | v0.15.3 Helm chart | K8s | LoadBalancer IPs (10.0.20.200-10.0.20.220), all services on 10.0.20.200 | | Registry Cache | Container | 10.0.20.10 | Pull-through for docker.io:5000, ghcr.io:5010 | @@ -211,31 +208,24 @@ VMs tag traffic on vmbr1 to isolate workloads. pfSense bridges VLAN 20 to the up ### Ingress Flow -CrowdSec is **not** a step in this chain — banned IPs are dropped before the -request ever reaches Traefik (Cloudflare edge WAF rule on proxied hosts; host -nftables on direct hosts). The flow below is for a request that survives that -out-of-band gate. - ```mermaid sequenceDiagram participant Client - participant CFedge as Cloudflare (edge WAF: crowdsec_ban block) + participant Cloudflare participant Cloudflared participant Traefik - participant AntiAI + participant CrowdSec participant Authentik participant RateLimit participant Retry participant Service participant Pod - Client->>CFedge: HTTPS request to blog.viktorbarzin.me - Note over CFedge: banned IP → blocked here (proxied hosts) - CFedge->>Cloudflared: Forward via tunnel (QUIC) + Client->>Cloudflare: HTTPS request to blog.viktorbarzin.me + Cloudflare->>Cloudflared: Forward via tunnel (QUIC) Cloudflared->>Traefik: HTTP to LoadBalancer IP - Note over Traefik: on direct hosts, banned IPs already dropped in-kernel (nftables forward hook) - Traefik->>AntiAI: anti-AI bot-block (fail-open) - AntiAI->>Authentik: If allowed, check auth (protected=true) + Traefik->>CrowdSec: Apply bouncer middleware + CrowdSec->>Authentik: If allowed, check auth (protected=true) Authentik->>RateLimit: If authenticated, check rate limit RateLimit->>Retry: If within limit, continue Retry->>Service: Forward to Service @@ -244,27 +234,24 @@ sequenceDiagram Service-->>Retry: Response Retry-->>RateLimit: Response RateLimit-->>Authentik: Response (strip auth headers) - Authentik-->>AntiAI: Response - AntiAI-->>Traefik: Response + Authentik-->>CrowdSec: Response + CrowdSec-->>Traefik: Response Traefik-->>Cloudflared: Response - Cloudflared-->>CFedge: Response via tunnel - CFedge-->>Client: HTTPS response + Cloudflared-->>Cloudflare: Response via tunnel + Cloudflare-->>Client: HTTPS response ``` ### Middleware Chain -CrowdSec IP-reputation enforcement is **not** in this chain — it is out-of-band -(host nftables on direct hosts; the Cloudflare edge WAF `crowdsec_ban` rule on -proxied hosts), so banned IPs never reach the chain and there is no per-request -CrowdSec hop. Every ingress created by the `ingress_factory` module follows this -Traefik chain: +Every ingress created by the `ingress_factory` module follows this chain: -1. **Anti-AI bot-block** (`ai-bot-block` ForwardAuth, on by default via `ingress_factory`): blocks/tarpits known AI crawlers. **Fail-open** (currently a no-op `return 200` — poison-fountain scaled to 0; see `docs/architecture/security.md`). +1. **CrowdSec Bouncer**: Checks IP against threat database. **Fail-open** mode — if LAPI is unreachable, traffic passes through to prevent outages. 2. **Authentik Forward-Auth** (if `protected = true`): SSO authentication via OIDC. Non-authenticated users are redirected to login. Auth headers are stripped before forwarding to backend. 3. **Rate Limiting**: Per-IP throttling. Returns **429 Too Many Requests** (not 503) when limit exceeded. Default is `rate-limit` (average 10 req/s, burst 50). Services whose clients legitimately burst harder get a dedicated middleware via `skip_default_rate_limit = true` + `extra_middlewares`: Immich (`immich-rate-limit`, 1000/20000, photo uploads) and ActualBudget (`actualbudget-rate-limit`, 50/300 — the Actual web app boots with ~70 parallel asset/migration revalidations; the default burst 429'd the tail and stalled every page load). 4. **Retry**: 2 attempts with 100ms delay on transient failures (5xx errors, connection errors). Additional middleware: +- **Anti-AI**: On by default via `ingress_factory`. Blocks common AI crawler user-agents. - **HTTP/3 (QUIC)**: Enabled globally on Traefik. ### Entrypoint Transport Timeouts @@ -361,7 +348,7 @@ Containerd on all K8s nodes uses `hosts.toml` to redirect pulls to the local cac | pfSense | `stacks/pfsense/` | VM + cloud-init config | | Technitium | `stacks/technitium/` | Deployment, Service, PVC | | Traefik | `stacks/platform/` (sub-module) | Helm release, IngressRoute CRDs | -| CrowdSec | `stacks/crowdsec/` (+ edge in `stacks/rybbit/`) | Helm release, LAPI + agent; `cs-firewall-bouncer` DaemonSet (nftables, direct hosts) + Cloudflare edge sync (proxied hosts) | +| CrowdSec | `stacks/platform/` (sub-module) | Helm release, LAPI + bouncer | | Authentik | `stacks/authentik/` | Helm release, ingress, OIDC configs | | MetalLB | `stacks/platform/` (sub-module) | Helm release, IPAddressPool | | Cloudflared | `stacks/cloudflared/` | Deployment (3 replicas), tunnel config; runs `--no-autoupdate` (in-place self-updates exited the pods and severed all tunnel WebSockets, 2026-06-09/10) | @@ -449,30 +436,13 @@ Containerd on all K8s nodes uses `hosts.toml` to redirect pulls to the local cac **Decision**: Technitium handles internal `.lan` domains with near-zero latency. Cloudflare handles public domains with global DNS. K8s nodes use Technitium as primary, which forwards non-.lan queries to Cloudflare. -### Why CrowdSec Enforcement Is Out-of-Band (and Fails Open) +### Why Fail-Open on CrowdSec Bouncer? -CrowdSec used to enforce inline as a Traefik middleware (the -`crowdsec-bouncer-traefik-plugin`). On Traefik 3.7.5 the Yaegi plugin handler was -never invoked, so it enforced nothing; the plugin was removed and enforcement -moved off the request path entirely (full history in -`docs/architecture/security.md`). It now runs on two surfaces: +**Alternatives considered**: +1. **Fail-closed**: Maximum security, but LAPI downtime blocks all traffic. +2. **Redundant LAPI**: Already scaled to 3 replicas, but resource pressure can still cause outages. -- **Direct hosts** → `cs-firewall-bouncer` DaemonSet drops banned IPs in the host - nftables, in **both the `input` and `forward` hooks**. The `forward` hook is - the load-bearing one: with Traefik on a dedicated LB IP at - `externalTrafficPolicy=Local`, client packets are DNAT'd to the Traefik **pod** - and transit the node's `forward` chain (not `input`) — which is exactly why the - ingress must preserve the **real client IP** end-to-end (ETP=Local + PROXY-v2 - for IPv6; see the Traefik LB IP and IPv6 ingress notes above). Without the real - client IP the firewall-bouncer (and the CF edge rule) would have nothing to - match on. -- **Proxied hosts** → a Cloudflare edge WAF rule (`ip.src in $crowdsec_ban`) fed - by the `crowdsec-cf-sync` CronJob. - -Both **fail open**: if LAPI is unreachable, the firewall-bouncer simply stops -receiving new decisions (existing drops persist) and the CF sync skips a run — -neither ever blocks legitimate traffic. Availability > strict bot blocking, and -out-of-band enforcement adds **zero per-request latency** (no Traefik hop). +**Decision**: Availability > strict bot blocking. CrowdSec LAPI is scaled to 3 replicas for resilience, but during cluster-wide resource exhaustion (e.g., memory pressure), bouncer falls back to allowing traffic. This prevents a complete service outage due to a security add-on. ### Why HTTP/3 (QUIC)? @@ -503,10 +473,9 @@ out-of-band enforcement adds **zero per-request latency** (no Traefik hop). **Symptoms**: All ingress routes return 503, Traefik dashboard shows no backends available. -**Diagnosis**: Middleware chain is blocking traffic. (CrowdSec is **not** in the -chain — a CrowdSec/LAPI outage cannot cause 503s; it only stops new bans.) Check: -1. Authentik status: `kubectl get pod -n authentik` (ForwardAuth fails closed if the auth server is unreachable) -2. `bot-block-proxy` status: `kubectl get pod -n traefik -l app=bot-block-proxy` (anti-AI ForwardAuth target — also fails closed if down) +**Diagnosis**: Middleware chain is blocking traffic. Check: +1. Authentik status: `kubectl get pod -n authentik` +2. CrowdSec LAPI status: `kubectl get pod -n crowdsec` 3. Traefik logs: `kubectl logs -n kube-system deploy/traefik` **Fix**: If Authentik is down and ingress uses forward-auth, pods won't pass health checks. Scale Authentik to 3 replicas or temporarily disable forward-auth middleware. diff --git a/docs/architecture/security.md b/docs/architecture/security.md index 7d3043ea..f8afd5e4 100644 --- a/docs/architecture/security.md +++ b/docs/architecture/security.md @@ -2,50 +2,40 @@ ## Overview -The homelab implements defense-in-depth security using CrowdSec for threat intelligence and IP reputation, Kyverno for policy enforcement and resource governance, and a 3-layer anti-AI scraping defense (reduced from 5 in April 2026 after removing the rewrite-body plugin). CrowdSec enforcement is **out-of-band** (not a per-request Traefik hop — see the CrowdSec section): banned IPs are dropped in-kernel via nftables on direct hosts, and blocked at the Cloudflare edge on proxied hosts, so enforcement adds **zero per-request latency**. All security components fail open (a CrowdSec outage stops new bans but never blocks legitimate traffic). Security policies are deployed in audit mode first, then selectively enforced after validation. +The homelab implements defense-in-depth security at the application layer (L7) using CrowdSec for threat intelligence and IP reputation, Kyverno for policy enforcement and resource governance, and a 3-layer anti-AI scraping defense (reduced from 5 in April 2026 after removing the rewrite-body plugin). All security components operate in graceful degradation mode (fail-open) to prevent cascading failures. Security policies are deployed in audit mode first, then selectively enforced after validation. ## Architecture Diagram -CrowdSec enforcement is out-of-band (NOT an inline Traefik middleware hop). The -Traefik request chain is anti-AI → Authentik ForwardAuth → rate-limit → retry; -CrowdSec drops banned IPs *before* (direct hosts) or *off* (proxied hosts) that -chain entirely. - ```mermaid -graph TB +graph LR Internet[Internet] - - subgraph "Proxied hosts (orange-cloud)" - CFedge[Cloudflare edge
WAF rule: ip.src in $crowdsec_ban → block] - end - subgraph "Direct hosts (grey-cloud / internal)" - NFT[Host nftables
table crowdsec/crowdsec6
drop in input + forward] - end - + CF[Cloudflare WAF] Tunnel[Cloudflared Tunnel] - Traefik[Traefik
anti-AI → Authentik → rate-limit → retry] + CrowdSec[CrowdSec Bouncer
Traefik Plugin] + AntiAI[Anti-AI Check
poison-fountain] + ForwardAuth[Authentik ForwardAuth] + RateLimit[Rate Limit Middleware] + Retry[Retry Middleware
2 attempts, 100ms] Backend[Backend Service] LAPI[CrowdSec LAPI
3 replicas] - Agent[CrowdSec Agent
parses Traefik logs] - FWB[cs-firewall-bouncer
DaemonSet, every node] - CFsync[crowdsec-cf-sync
CronJob, every 2 min] + Agent[CrowdSec Agent] - Internet -->|proxied| CFedge - Internet -->|direct| NFT - CFedge -->|allowed| Tunnel - Tunnel --> Traefik - NFT -->|allowed| Traefik - Traefik --> Backend + Internet -->|1| CF + CF -->|2| Tunnel + Tunnel -->|3| CrowdSec + CrowdSec -.->|Query| LAPI + Agent -.->|Report| LAPI + CrowdSec -->|4. Pass/Block| AntiAI + AntiAI -->|5. Human/Bot| ForwardAuth + ForwardAuth -->|6. Authenticated| RateLimit + RateLimit -->|7. Under Limit| Retry + Retry -->|8. Success/Retry| Backend - Agent -.->|report| LAPI - LAPI -.->|all decisions incl. CAPI| FWB - FWB -.->|program drop rules| NFT - LAPI -.->|ban/captcha decisions, CAPI excluded| CFsync - CFsync -.->|push IP list| CFedge - - style CFedge fill:#f9f,stroke:#333 - style NFT fill:#f9f,stroke:#333 + style CrowdSec fill:#f9f,stroke:#333 + style AntiAI fill:#ff9,stroke:#333 + style ForwardAuth fill:#9f9,stroke:#333 + style RateLimit fill:#99f,stroke:#333 ``` ## Components @@ -54,8 +44,7 @@ graph TB |-----------|---------|----------|---------| | CrowdSec LAPI | Pinned | `stacks/crowdsec/` | Local API, threat intelligence aggregation (3 replicas) | | CrowdSec Agent | Pinned | `stacks/crowdsec/` | Log parser, scenario detection | -| cs-firewall-bouncer | v0.0.34 | `stacks/crowdsec/modules/crowdsec/firewall_bouncer.tf` | In-kernel nftables drop on every node (DIRECT hosts). Bouncer key `firewall` | -| crowdsec-cf-sync | — | `stacks/rybbit/crowdsec_edge.tf` | LAPI→Cloudflare-IP-List sync CronJob (PROXIED hosts). Bouncer key `kvsync` | +| CrowdSec Traefik Bouncer | Plugin | Traefik config | Plugin-based IP reputation check | | Kyverno | Pinned chart | `stacks/kyverno/` | Policy engine for K8s admission control | | poison-fountain | Latest | `stacks/poison-fountain/` | Anti-AI bot detection and tarpit service | | cert-manager/certbot | - | `stacks/cert-manager/` | TLS certificate management | @@ -65,15 +54,11 @@ graph TB ### Request Security Layers -CrowdSec IP-reputation enforcement happens **before** a request reaches the -Traefik chain (banned IPs are dropped in-kernel on direct hosts, or blocked at -the Cloudflare edge on proxied hosts — see CrowdSec Threat Intelligence below). -A request that survives that out-of-band gate then passes through the Traefik -middleware chain: +Every incoming request passes through 6 security layers: -1. **Cloudflare WAF / edge** - DDoS protection, bot detection, firewall rules incl. the CrowdSec `crowdsec_ban` block rule (proxied hosts only) -2. **Cloudflared Tunnel** - Zero Trust tunnel, hides origin IP (proxied hosts) -3. **CrowdSec out-of-band drop** - nftables on direct hosts; *not* a Traefik hop (zero per-request latency) +1. **Cloudflare WAF** - DDoS protection, bot detection, firewall rules (external) +2. **Cloudflared Tunnel** - Zero Trust tunnel, hides origin IP +3. **CrowdSec Bouncer** - IP reputation check against LAPI (fail-open on error) 4. **Anti-AI Scraping** - 3-layer bot defense (optional per service, updated 2026-04-17) 5. **Authentik ForwardAuth** - Authentication check (if `protected = true`) 6. **Rate Limiting** - Per-source IP rate limits (returns 429 on breach) @@ -95,71 +80,58 @@ CrowdSec operates in a hub-and-agent model: - Reports malicious IPs to LAPI - Shares threat intel with CrowdSec community (anonymized) -Enforcement is split across **two out-of-band surfaces**, neither of which adds -any per-request latency. (See "Why the Traefik bouncer plugin was removed" below -for the supersession history — there is no longer an inline Traefik bouncer.) +**Traefik Bouncer Plugin** (`crowdsec-bouncer-traefik-plugin`, `stacks/traefik/modules/traefik/middleware.tf`): +- Integrated as Traefik middleware (in the default ingress chain) +- Queries LAPI for IP reputation on each request +- **Registered with LAPI** via `BOUNCER_KEY_traefik` env on the LAPI container + (`stacks/crowdsec/modules/crowdsec/values.yaml`), seeded from the same Vault key + the middleware presents (`ingress_crowdsec_api_key`). **Before 2026-06-19 the + bouncer was never registered → LAPI returned 403 → the plugin failed open and + enforced nothing (no bans, no captcha).** The seed re-registers automatically on + every LAPI start, so a DB wipe (e.g. the MySQL→PostgreSQL migration that lost the + original registration) can't silently disable enforcement again. +- **Fail-open mode**: If LAPI unreachable, allows traffic (graceful degradation) +- **Only sees non-proxied (direct) apps' real client IPs** (ETP=Local). Proxied + apps arrive from cloudflared's pod IP (in `clientTrustedIPs`) and are bypassed — + extending enforcement to proxied apps needs `forwardedHeadersTrustedIPs` (future). +- Honours two LAPI remediation types (profiles in `stacks/crowdsec/modules/crowdsec/values.yaml`): + - **`ban`** → HTTP 403 (serious attacks: CVE exploits, scanners, brute force) + - **`captcha`** → **Cloudflare Turnstile challenge** so the flagged user can + self-unblock (lower-severity abuse: `http-429-abuse`, `http-403-abuse`, + `http-crawl-non_statics`, `http-sensitive-files`). The plugin is configured + with `captchaProvider=turnstile` + the widget keys; the `captcha.html` + template is mounted into the Traefik pod at `/captcha`. The widget is + Terraform-managed in `stacks/traefik/main.tf` + (`cloudflare_turnstile_widget.crowdsec_captcha`, scoped to `viktorbarzin.me` + so it covers every subdomain). **Before 2026-06-19 no captcha provider was + configured, so `captcha` decisions silently degraded to a 403 ban** — users + had no way to self-unblock; wiring Turnstile fixed that. -**Surface 1 — DIRECT (non-Cloudflare-proxied) hosts → in-kernel nftables drop** -(`cs-firewall-bouncer` DaemonSet, `stacks/crowdsec/modules/crowdsec/firewall_bouncer.tf`): -- Runs on **every node** (no nodeSelector). Programs the HOST nftables — `table ip - crowdsec` / `table ip6 crowdsec6` — with drop rules in **both the `input` AND - the `forward` hooks**. The `forward` hook is required because Traefik is a - LoadBalancer with `externalTrafficPolicy=Local`: client traffic is DNAT'd to the - Traefik **pod** and transits the node's `forward` hook (not `input`) with the - real client IP preserved. Chains use `policy accept` (only set members drop — - it can never blackhole normal traffic). -- Pulls **all** decisions from LAPI, **including the CAPI community blocklist - (~31k IPs)**. Packets from banned IPs are dropped **in-kernel before reaching - Traefik** → zero per-request hops, no Traefik involvement at all. -- **Packaging**: cs-firewall-bouncer publishes no container image, so the - **v0.0.34** static binary is fetched at runtime by an initContainer onto a - `debian:bookworm-slim` runtime container. Needs `hostNetwork` + - `NET_ADMIN`/`NET_RAW` to talk netlink directly. Registered bouncer key: - **`firewall`**. -- **Fail-open**: if LAPI is unreachable it just stops receiving new decisions - (existing drop rules persist); it never blocks legitimate traffic. - -**Surface 2 — PROXIED (Cloudflare orange-cloud) hosts → Cloudflare edge block** -(`stacks/rybbit/crowdsec_edge.tf` + `lapi_kv_sync.py`): -- Proxied hosts terminate at the Cloudflare edge, so a host-level nftables drop - would never see them. Enforcement is instead a single Cloudflare Rules List - **`crowdsec_ban`** + a zone-scoped WAF custom rule `(ip.src in $crowdsec_ban)` - → **block** action, which covers every proxied host in the zone. -- Fed by the **`crowdsec-cf-sync` CronJob** (namespace `rybbit`, every 2 min, - pure-stdlib Python in a ConfigMap). It pulls local **ban/captcha ip-scoped** - decisions and pushes them into the CF list, but **EXCLUDES the ~31k CAPI - community blocklist** — that set is far too large for a CF Rules List (the CF - account hard-limits to **one** list), and CAPI is already covered in-kernel on - direct hosts and by Cloudflare's own managed protections on proxied hosts. - Registered bouncer key: **`kvsync`**. -- **Block-only**: the single-list limit precludes a separate - captcha/managed-challenge list, so both ban and captcha decisions are enforced - as a plain block at the edge. -- **Auth carve-out:** the WAF rule excludes `authentik.viktorbarzin.me` + - `public-auth.viktorbarzin.me` (`… and not (http.host in {…})`). A CrowdSec hit - must never wall a user out of the login / WebAuthn flow they authenticate - through; auth keeps `traefik-rate-limit` for brute-force protection. - -**Whitelist** (`stacks/crowdsec/whitelist.yaml`): a CrowdSec whitelist covers -RFC1918 + the tailnet + internal CIDRs (plus one specific external IP), so -internal users are never enforced. Internal access uses split-horizon DNS -straight to Traefik, and direct internal clients are RFC1918 — both whitelisted. - -#### Why the Traefik bouncer plugin was removed - -Enforcement used to run as an inline Traefik middleware — the -`crowdsec-bouncer-traefik-plugin` (Yaegi/Lua), which queried LAPI on every -request and could serve a Cloudflare Turnstile captcha for soft remediations. -On **Traefik 3.7.5 the Yaegi handler was never invoked**, so the bouncer was -registered but enforced **nothing** despite appearing healthy. Rather than chase -the Yaegi runtime, the whole plugin path was **removed** (2026-06): the plugin -static config + initContainer download, the `crowdsec` Middleware CRD, the -`captcha.html` template + its ConfigMap and volume mount, and the Cloudflare -Turnstile widget (`cloudflare_turnstile_widget.crowdsec_captcha`). It was -replaced by the two out-of-band surfaces above, which add zero per-request -latency and fail open. (The earlier `crowdsec-cf-sync` cursor-pagination / -IP-List-capacity issues are also moot now that CAPI is excluded from the edge -list and dropped in-kernel instead.) +**Cloudflare Edge Enforcement for proxied hosts** (`stacks/rybbit/crowdsec_edge.tf` + `lapi_kv_sync.py`): +- Proxied (orange-cloud) hosts terminate at the Cloudflare edge, so the in-cluster + bouncer above never decides on them. Edge enforcement instead syncs LAPI + decisions into **one Cloudflare account IP List (`crowdsec_ban`)** + a single + **zone-scoped WAF custom rule** blocking `(ip.src in $crowdsec_ban)` across every + proxied host. CronJob `crowdsec-cf-sync` (rybbit ns, every 2 min) reconciles it. +- **BAN-ONLY (2026-06-20):** only `type=ban` decisions sync to the edge. `captcha` + decisions are deliberately NOT pushed — the CF account allows only ONE Rules List + with a single block action, so folding captcha in would hard-block a soft + challenge on every proxied host. (Before 2026-06-20 captcha was downgraded to a + hard block at the edge.) +- **Auth carve-out (2026-06-20):** the WAF rule excludes `authentik.viktorbarzin.me` + + `public-auth.viktorbarzin.me` (`… and not (http.host in {…})`), and the + Authentik UI ingress sets `exclude_crowdsec = true` for the in-cluster bouncer. A + CrowdSec hit must never wall a user out of the login / WebAuthn flow they + authenticate through; auth keeps `traefik-rate-limit` for brute-force protection. +- **⚠️ Currently NON-FUNCTIONAL (known issue, pre-existing since the 2026-06-20 + rollout):** `crowdsec-cf-sync` fails every run — `cf_list_items()` pagination + gets CF `HTTP 400 code 10027 "invalid or expired cursor"`, so the list never + populates (`num_items=0`) and the edge rule blocks nothing. LAPI also returns + ~31k ban IPs, likely exceeding CF IP-List capacity even once pagination is fixed. + **Edge enforcement for proxied hosts is therefore inert pending a fix** (the + in-cluster bouncer still protects direct apps; the auth carve-out is correct + regardless). Fix needs: (1) correct CF cursor pagination, (2) a capacity strategy + for the ban set. **Metabase** (disabled by default): - Dashboard for CrowdSec analytics @@ -405,12 +377,10 @@ Beads: `code-8ywc` W1.6 + W1.7. **Status: planned.** | Path | Purpose | |------|---------| -| `stacks/crowdsec/` | CrowdSec LAPI, agent config + `whitelist.yaml` | -| `stacks/crowdsec/modules/crowdsec/firewall_bouncer.tf` | cs-firewall-bouncer DaemonSet (in-kernel nftables drop, direct hosts) | -| `stacks/rybbit/crowdsec_edge.tf` + `lapi_kv_sync.py` | Cloudflare IP-List + WAF block rule + LAPI→CF sync CronJob (proxied hosts) | +| `stacks/crowdsec/` | CrowdSec LAPI, agent, bouncer config | | `stacks/kyverno/` | Kyverno deployment + policies | | `stacks/poison-fountain/` | Anti-AI service + CronJob | -| `stacks/traefik/modules/traefik/middleware.tf` | Security middleware definitions (no longer includes a CrowdSec bouncer) | +| `stacks/platform/modules/traefik/middleware.tf` | Security middleware definitions | | `stacks/platform/modules/ingress_factory/` | Per-service security toggles | ### Vault Paths @@ -520,11 +490,7 @@ spec: **Fix**: 1. Check LAPI decisions: `kubectl exec -it crowdsec-lapi-0 -- cscli decisions list` 2. Remove ban: `kubectl exec -it crowdsec-lapi-0 -- cscli decisions delete --ip ` - — the in-kernel drop clears as soon as `cs-firewall-bouncer` reconciles (direct - hosts); for proxied hosts the `crowdsec-cf-sync` CronJob removes it from the - `crowdsec_ban` CF list within ~2 min. -3. Whitelist if needed: Add to `stacks/crowdsec/whitelist.yaml` (RFC1918 + tailnet - + internal CIDRs are already whitelisted, so internal clients are never banned). +3. Whitelist if needed: Add to `stacks/crowdsec/whitelist.yaml` ### Kyverno Policy Blocking Deployment diff --git a/docs/plans/2026-06-21-eso-0.12-to-2.x-migration-design.md b/docs/plans/2026-06-21-eso-0.12-to-2.x-migration-design.md deleted file mode 100644 index 64a28d1c..00000000 --- a/docs/plans/2026-06-21-eso-0.12-to-2.x-migration-design.md +++ /dev/null @@ -1,243 +0,0 @@ -# External Secrets Operator: 0.12.1 → 2.6.0 Migration (v1beta1 → v1) — Design Doc - -> **Status:** ✅ **COMPLETE (2026-06-22).** ESO at chart/app **2.6.0**; all 104 ExternalSecrets + 2 ClusterSecretStores on `external-secrets.io/v1`; 109 ESs SecretSynced (2 pre-existing dead); compat-gate now returns `OK: cluster is safe to upgrade to 1.35.6` (EXIT 0) — the last k8s-1.35 blocker is cleared. Executed Phase 1 (climb to 0.16.2) → Phase 2 (v1 rewrite, validated GC-survival on tandoor) → Phase 3 (climb 0.16.2→2.6.0 across the 0.17 cutoff, ES sync held at 109 every hop). Side-finding fixed: repo-wide stale `.terraform.lock.hcl` files (missing gavinbunney/kubectl + telmate/proxmox from the generated providers.tf) had broken `terragrunt apply` for ~28 stacks (this is what failed CI pipeline 332) — reconciled via `init -upgrade` + committed. -> **Scope:** Upgrade the ESO Helm chart `0.12.1` (app `v0.12.1`) to `2.6.0` (app `v2.6.0`) and migrate every `external-secrets.io/v1beta1` custom resource to `external-secrets.io/v1`. -> **Owner:** Viktor Barzin. **Author:** Claude (research + design only — no changes applied). -> -> **EXECUTION CORRECTION + STATUS (2026-06-21 — "let's do the ESO migration"):** The cluster is already on **k8s 1.34.9** (all 7 nodes), NOT ≤1.31 as §4.3 assumed. ESO 0.12 runs fine on 1.34 (the support-matrix bands are conservative *tested* ranges, not hard limits). **The entire ESO climb 0.12→2.6 therefore happens on k8s 1.34 — there is NO k8s interleave; IGNORE the "advance k8s to 1.32/1.33" steps in §4.3 / Phase 1 / Phase 3.** Only AFTER ESO reaches 2.x does the nightly version-check chain take k8s 1.34→1.35 (gate clears). Exact hop sequence (latest patch per minor): **0.13.0 → 0.14.4 → 0.15.1 → 0.16.2** [rewrite all 104 CRs to `v1` here] → **0.17.0 → 0.18.2 → 0.19.2 → 0.20.4 → 1.0.0 → 1.1.1 → 1.2.1 → 1.3.2 → 2.0.1 → 2.1.0 → 2.2.0 → 2.3.0 → 2.4.1 → 2.5.0 → 2.6.0**. Pre-flight done: CRD `storedVersions` are `["v1beta1"]` only (no v1alpha1 patch needed). -> -> **EXECUTION LOG:** -> - **✅ Phase 1 DONE (2026-06-21):** ESO climbed 0.12.1 → 0.13.0 → 0.14.4 → 0.15.1 → **0.16.2**, one hop at a time, each applied + verified (controller healthy; 108 live ExternalSecrets stayed SecretSynced; 2 pre-existing dead — `instagram-poster/instagram-poster-secrets` False since 2026-05-10, `payslip-ingest/payslip-ingest-secrets` False since 2026-04-25, both missing Vault data, untouched). Added `atomic=true` + `timeout=600` to the helm_release. At 0.16.2 **both `v1beta1` and `v1` are served** (110 each) and `storedVersions = ["v1beta1","v1"]`. Committed (`eso: Phase 1 …`); state auto-committed per hop by `scripts/tg`. -> - **⏳ Phase 2 PENDING — findings confirmed (decisive for execution):** (a) bumping a `kubernetes_manifest` ExternalSecret's apiVersion v1beta1→v1 **forces a REPLACE** (verified live on instagram-poster: `-/+ must be replaced`), NOT in-place. (b) Our ExternalSecrets use **`creationPolicy=Owner`** (default; confirmed on nextcloud) → target Secrets carry an ownerReference, so the replace's delete step can **cascade-GC the Secret** before ESO recreates it. → **Phase 2 must be done carefully, NOT a blind bulk apply:** (1) snapshot ALL target Secrets first (backstop); (2) **empirically validate on the FIRST live stack** — migrate one ES while watching its target Secret; ESO re-syncs the identical spec fast and should re-adopt before GC, but confirm before proceeding; (3) then the per-stack two-phase `-target`-then-full apply (the 15 plan-time-coupled stacks need `-target` first). If validation shows GC wins, pivot to `state rm` + `import {}` (adopts the already-v1-served object with zero delete → zero GC). Repo is clean at v1beta1 (the lone test edit was reverted, never applied). -> - **Phase 3 PENDING:** hops 0.17.0 → 0.18.2 → 0.19.2 → 0.20.4 → 1.0.0 → 1.1.1 → 1.2.1 → 1.3.2 → 2.0.1 → 2.1.0 → 2.2.0 → 2.3.0 → 2.4.1 → 2.5.0 → 2.6.0 (all on k8s 1.34, CRs already v1). Crossing **0.17 is the point of no return**. - ---- - -## 1. Goal & why - -ESO is the **last remaining compatibility gate blocking the autonomous k8s 1.35 upgrade** (Kyverno was cleared to 1.18.1 earlier today). The installed ESO `0.12.x` supports only Kubernetes **1.19 → 1.31** ([support matrix](https://external-secrets.io/latest/introduction/stability-support/)); the k8s-version-check chain will refuse to advance the cluster past 1.31 while ESO sits at 0.12. The `2.x` series supports **k8s 1.34–1.35**, which clears the gate. - -The hard part is not the chart bump itself — it is that **ESO removed the `external-secrets.io/v1beta1` API**, and every one of our ExternalSecret / ClusterSecretStore resources is currently declared `v1beta1`. If we upgrade past the removal version without first rewriting the manifests to `v1`, ESO stops reconciling and synced Secrets go stale (apps keep their last-good Secret, but rotations and new secrets break). - -**Downtime tolerance:** brief, recoverable downtime of the ESO *controller* is acceptable. What must NOT happen is loss/corruption of the downstream Kubernetes `Secret` objects that apps mount (DB creds, API keys). Those must survive continuously. - ---- - -## 2. Current state - -### 2.1 Versions -| Component | Current | Target | -|---|---|---| -| Helm chart `external-secrets` | **0.12.1** | **2.6.0** | -| App / controller image | **v0.12.1** | **v2.6.0** | -| API version of all CRs | **`external-secrets.io/v1beta1`** | **`external-secrets.io/v1`** | -| Repo: `https://charts.external-secrets.io` | (unchanged) | (unchanged) | - -ESO stack: `stacks/external-secrets/main.tf`. `helm_release.external_secrets` pins `version = "0.12.1"`, namespace `external-secrets` (separate `kubernetes_namespace` resource, not `create_namespace`), and the **only** chart value set is `installCRDs = true` (via `yamlencode({ installCRDs = true })`). No webhook/replica/resource overrides. - -### 2.2 Inventory (live, from `stacks/`) -| Kind | Count | apiVersion | Where | -|---|---|---|---| -| **ExternalSecret** (`kubernetes_manifest`) | **104** | all `v1beta1` (0 mismatches) | 73 `.tf` files | -| **ClusterSecretStore** (definitions) | **2** | both `v1beta1` | `stacks/external-secrets/main.tf` | -| SecretStore | 0 | — | — | -| PushSecret | 0 | — | — | -| ClusterExternalSecret | 0 | — | — | - -- **Only ONE apiVersion string exists in the whole tree:** `external-secrets.io/v1beta1` (106 occurrences = 104 ExternalSecret + 2 ClusterSecretStore). Zero `v1`, zero `v1alpha1`. → a clean single-target rewrite. -- **`secretStoreRef` split:** 78 ExternalSecrets → `vault-kv`, 26 → `vault-database` (78 + 26 = 104). The `kind = "ClusterSecretStore"` string also appears inside every `secretStoreRef`, so a naive `grep 'kind = "ClusterSecretStore"'` returns 106 — only **2** are real store definitions. -- **22 files carry >1 ExternalSecret** (max: `stacks/fire-planner/main.tf` = 5; then wealthfolio / real-estate-crawler / phpipam / payslip-ingest / n8n / job-hunter / ebooks = 3 each; 13 files = 2). The 104-vs-73 gap is these multi-secret files. -- **Nested-module ExternalSecrets** (easy to miss when scripting the bump): `stacks/instagram-poster/modules/instagram-poster/main.tf`, `stacks/postiz/modules/postiz/main.tf`, `stacks/technitium/modules/technitium/main.tf`, `stacks/mailserver/modules/mailserver/main.tf`, `stacks/monitoring/modules/monitoring/grafana.tf`, `stacks/proxmox-csi/modules/proxmox-csi/main.tf`. -- **Docs are STALE:** `.claude/CLAUDE.md` says "43 ExternalSecrets + 9 DB-creds". Live count is **104 ExternalSecrets / 73 files / 26 db-refs**. Fix in the migration PR. - -### 2.3 The two ClusterSecretStores (`stacks/external-secrets/main.tf`) -Both `kubernetes_manifest`, both `external-secrets.io/v1beta1`, both `depends_on = [helm_release.external_secrets]`: -- **`vault-kv`** → Vault KV **v2** at `path = "secret"`, server `http://vault-active.vault.svc.cluster.local:8200`, auth `kubernetes` mount `kubernetes`, role `eso`, SA `external-secrets/external-secrets`. -- **`vault-database`** → identical except `path = "database"`, **`version = "v1"`** (Vault DB engine, KV-v1-style). - -ESO's Vault auth role `eso` (`stacks/vault/main.tf:486-511`): policy `eso-reader` (`secret/data/*` read+list, deny `secret/data/vault`, `database/static-creds/*` read), `token_ttl = token_period = 864000` (10d, periodic/auto-renew). - -### 2.4 Tier-0 / state -ESO is **Tier-0 (bootstrap)** (`.claude/CLAUDE.md` "Terraform State — Two-Tier Backend"; root `terragrunt.hcl` `tier0_stacks = ["infra","platform","cnpg","vault","dbaas","external-secrets"]`). Tier-0 ⇒ **local SOPS-encrypted state in git** (`state/stacks/external-secrets/terraform.tfstate`), NOT the PG backend. Workflow: `git pull` → `scripts/tg plan` → `scripts/tg apply` → `git push`; SOPS decrypt via Vault Transit (primary) → age fallback. **Tier-0 must apply before PG is reachable**, so the ESO upgrade cannot depend on PG. - -### 2.5 Provider versions (`stacks/external-secrets/providers.tf`) -- `required_providers` declares **only** `vault = hashicorp/vault, ~> 4.0`. -- `provider "kubernetes"` and `provider "helm"` are declared **without version constraints** (resolve from root / `.terraform.lock.hcl`). The `helm` block already uses the **v3-style nested `kubernetes = {…}` argument** (not the legacy `kubernetes {}` block) ⇒ helm provider is **v3.x or v4.x** in the lockfile. **No `kubectl` provider** in this stack. No `required_version` pinned here. -- ⚠️ **Verify the resolved helm provider version** in `.terraform.lock.hcl` before starting — the prompt referenced `~> 4.0` for helm; the *stack* only pins that for `vault`. Either way the v3-syntax helm block + an SDK-v3 provider is compatible with the chart (see §4.5). - -### 2.6 Plan-time coupling (the cross-cutting risk) -**15 stacks read ESO-created Secrets at plan time** via `data "kubernetes_secret"` (avoids a Vault dependency at plan): `actualbudget, affine, changedetection, coturn, ebooks, fire-planner, freedify, freshrss, grampsweb, k8s-dashboard (dashboard_injector.tf), navidrome, owntracks, real-estate-crawler, servarr, technitium (modules/technitium)`. - -The documented **first-apply gotcha** (`.claude/CLAUDE.md`, `docs/architecture/secrets.md:360`, `stacks/fire-planner/main.tf:574`): the Secret must exist before the `data "kubernetes_secret"` plans, so on first creation you must `terragrunt apply -target=kubernetes_manifest.` first, then full apply. **Why this matters for the migration:** the `kubernetes_manifest` provider treats `apiVersion` as part of resource identity, so bumping `v1beta1`→`v1` **forces a replace** of all 104 ExternalSecrets. During replace there is a window where the new CR (and thus the synced Secret) may not yet be materialized when the same stack's `data "kubernetes_secret"` plans → the two-phase `-target` apply is needed **fleet-wide for the v1 rewrite step, not just fire-planner.** - -### 2.7 Vault DB rotation (rotation interplay) -`stacks/vault/main.tf`: **25 `vault_database_secret_backend_static_role`, every one `rotation_period = 604800` (7 days)** (8 MySQL + 17 PostgreSQL static roles). ESO syncs these via `vault-database` → `remoteRef.key = "static-creds/"`. Apps reading a rotated secret only at startup carry a Stakater Reloader annotation. **Implication:** any ESO controller downtime longer than the gap to the next rotation could leave a Secret stale across a rotation; keep controller downtime short and re-sync promptly. - -### 2.8 git-crypt landmine (adjacent, not in ESO stack) -`.claude/CLAUDE.md:146` + `docs/architecture/ci-cd.md:108` + `stacks/kyverno/modules/kyverno/tls-secret-sync.tf`: on a **git-crypt-locked clone**, `kubernetes_secret.tls_secret` reads `secrets/fullchain.pem`/`privkey.pem` via `file()` which returns **ciphertext**, corrupting the wildcard TLS secret Kyverno clones cluster-wide. **The ESO stack itself has NO `file()` reads of git-crypt secrets** — so this landmine does not bite the ESO upgrade directly. It is listed here only as a guardrail: do not piggyback unrelated kyverno applies during this work, and run all applies from an **unlocked** checkout. - ---- - -## 3. Target - -- Helm chart **`external-secrets` 2.6.0** (app **v2.6.0**), repo `https://charts.external-secrets.io`. -- All ExternalSecret + ClusterSecretStore CRs on **`external-secrets.io/v1`**. -- Cluster ESO compatible with **k8s 1.34–1.35** ⇒ unblocks the autonomous 1.35 upgrade. - ---- - -## 4. Key findings (the decisive facts) - -> Sourced from ESO official docs + GitHub release notes; verbatim quotes below. - -### 4.1 Chart version == app version (premise check) -The chart version and app version are released **in lockstep and are the same number**. `Chart.yaml`: `version: 0.12.1 / appVersion: v0.12.1`; `version: 2.6.0 / appVersion: v2.6.0`. The app series ran `…0.20.4 → 1.0.0 → … → 2.0.0 → … → 2.6.0`. **Crucially, the `v1.0.0` and `v2.0.0` APP releases are NOT the `external-secrets.io/v1` API** — `v1.0.0` is just "continuation after 0.20.4" (release diff `v0.20.4...v1.0.0`, no API change), and `v2.0.0`'s only breaking change is removing the unmaintained **Alibaba + Device42** providers (we use neither — only Vault). The API migration happened back at **0.16/0.17**. Source: [v1.0.0 notes](https://github.com/external-secrets/external-secrets/releases/tag/v1.0.0) · [v2.0.0 notes](https://github.com/external-secrets/external-secrets/releases/tag/v2.0.0). - -### 4.2 Version path: **NO skipping minors — step one minor at a time** -Official policy, verbatim ([stability-support](https://external-secrets.io/latest/introduction/stability-support/)): -> "**Upgrade version by version** — We strongly recommend upgrading one minor version at a time (e.g., 0.18.x → 0.19.x → 0.20.x) rather than skipping versions." - -Maintainer (issue [#4785](https://github.com/external-secrets/external-secrets/issues/4785), @gusfcarvalho): *"We are pre release… Every minor bump should be treated as a major bump until we go 1.0."* ⇒ **You CANNOT helm-upgrade 0.12.1 → 2.6.0 directly.** You must step each minor: `0.12 → 0.13 → 0.14 → 0.15 → 0.16 → 0.17 → 0.18 → 0.19 → 0.20 → 1.x → 2.x`. - -### 4.3 k8s ↔ ESO must advance roughly in lockstep -Each ESO release targets a **narrow** k8s band ([support matrix](https://external-secrets.io/latest/introduction/stability-support/)): - -| ESO | k8s band | -|---|---| -| 0.12.x | 1.19 → 1.31 | -| 0.16.x | 1.32 | -| 0.17.x | 1.33 | -| 2.0 – 2.5 | 1.34 – 1.35 | -| 2.6 (latest) | (matrix row not yet appended; 2.x band is consistently 1.34–1.35 — see Open Questions) | - -**This is the single most important sequencing constraint.** ESO doesn't "support only ≤ its max k8s" in a wide range — older ESO may not run cleanly on a *much newer* k8s either. The bands imply the ESO upgrade and the k8s upgrade need to be **interleaved**, not "finish ESO, then bump k8s in one jump." Practical reading: the cluster is currently on k8s ≤1.31 (ESO 0.12 blocks past it). The 0.16/0.17 steps want k8s 1.32/1.33; the 2.x steps want 1.34/1.35. So this is a **coordinated ESO+k8s climb**, e.g. ESO→0.16 alongside k8s→1.32, ESO→0.17 alongside k8s→1.33, then ESO→2.x alongside k8s→1.34→1.35. (The k8s climb is itself sequential via the version-check chain; this doc focuses on the ESO half but flags the coupling — see Open Questions for who drives the interleave.) - -### 4.4 API migration: **must rewrite manifests to `v1` FIRST — there is NO v1beta1→v1 conversion webhook** -- **`external-secrets.io/v1` promoted to STORAGE version: v0.16.0.** v0.16.0 release notes "BREAKING CHANGES": *"Promotion of ExternalSecret/v1 and SecretStore/v1 and their cluster counterparts"* and *"Removal of Conversion Webhooks and …/v1alpha1…"*. From 0.16, **etcd stores `v1`**. Source: [v0.16.0 notes](https://github.com/external-secrets/external-secrets/releases/tag/v0.16.0). -- **`external-secrets.io/v1beta1` STOPS BEING SERVED (hard cutoff): v0.17.0.** Verbatim ([v0.17.0 notes](https://github.com/external-secrets/external-secrets/releases/tag/v0.17.0)): - > "v0.17.0 Stops serving `v1beta1` apis. You need to update your manifests from `v1beta1` to `v1` prior to updating from `v0.16` to `v0.17`. The only change needed is upgrading your manifests to `v1` (i.e. removing the `beta1` from `v1beta1`). … Be sure to do that to all your manifests prior to bumping to `v0.17.0`! `v0.16.2` already supports `v1` so this process should be smooth." -- **No v1beta1→v1 conversion webhook.** The only conversion webhook that ever existed was v1alpha1→v1beta1, **removed in 0.16**. Maintainer (issue [#5478](https://github.com/external-secrets/external-secrets/issues/5478), @gusfcarvalho): the post-0.16 "drift" is simply that etcd now stores v1 — *"This isn't really a conversion issue."* ⇒ **old v1beta1 manifests do NOT keep working past 0.17 via any auto-conversion.** - - **Verdict: MUST-REWRITE-FIRST.** Rewrite all CRs to `v1` while on **0.16.x** (which serves *both* v1beta1 and v1), then upgrade to 0.17. Real-world confirmation (issue [#4785](https://github.com/external-secrets/external-secrets/issues/4785), @Dutchy-): *"I was able to change v1beta1 to v1 on 0.16 without issues. After that I was able to upgrade to 0.17."* - - There is a deprecated escape hatch in chart 2.6.0 — `unsafeServeV1Beta1: true` re-enables v1beta1 serving for stragglers — but its own values comment says *"This flag will be removed on 2026.05.01"* (i.e. **already past**, do not rely on it). -- **Schema change is a PURE apiVersion string bump — ZERO field changes.** CRD `openAPIV3Schema` diff (v0.16.2 bundle, which serves both): ExternalSecret / SecretStore / ClusterSecretStore / ClusterExternalSecret have **byte-identical** spec field sets between v1beta1 and v1 (`{data, dataFrom, refreshInterval, refreshPolicy, secretStoreRef, target}` for ExternalSecret). Maintainer (issue #4785, @Skarlso): *"Just change your manifests to be v1 and upgrade… We don't have anything fancy that you need to do."* PushSecret only ever had `v1alpha1` (no v1beta1) — **unaffected** (we have 0 anyway). - -### 4.5 Helm chart values + CRD handling (0.12 → 2.6) -- **No top-level values removed or renamed.** `values.yaml` diff 0.12.1↔2.6.0 is **additive only** (new keys: `enableHTTP2, extraInitContainers, genericTargets, grafanaDashboard, hostAliases, hostUsers, leaderElectionID, livenessProbe, openshiftFinalizers, processClusterGenerator, processClusterPushSecret, processSecretStore, readinessProbe, strategy, systemAuthDelegator, vault`). Our single value `installCRDs = true` survives. -- **`installCRDs` still works** in 2.6.0 (defaults `true`, "install and upgrade CRDs through helm chart"). CRDs are **templated into the single `external-secrets` chart** and **upgraded by `helm upgrade`** automatically — there is **no separate CRDs subchart**, and no manual `kubectl apply` of CRDs is required by default. (Out-of-band bundle, if ever needed, lives at `deploy/crds/bundle.yaml` per release tag.) The only CRD-value change: `crds.conversion.enabled` defaults `true` in 0.12.1 (for the old v1alpha1 webhook) → `false` in 2.6.0 ("we stopped supporting v1alpha1"). We don't set it, so the new default is fine. -- **CRD storedVersions bookkeeping (the one real pre-flight check):** v0.16.0 notes warn to ensure no CRD still lists `v1alpha1` in `.status.storedVersions` before/at 0.16, with a `kubectl patch` to set it to `["v1","v1beta1"]` if needed. This is CRD metadata hygiene, NOT secret deletion. -- **Helm provider:** `Chart.yaml apiVersion: v2` (Helm 3 chart) in both 0.12.1 and 2.6.0; **no minimum Helm version declared** (only `kubeVersion: ">= 1.19.0-0"`). The Terraform helm provider on Helm SDK v3 (v3.x/v4.x) is compatible. **The 2.x chart does NOT require a newer helm provider than 0.12 did** — the v3-style helm block in `providers.tf` already satisfies it. (Still: pin/verify the resolved version in the lockfile; see Open Questions.) - -### 4.6 Data migration: **downstream Secrets survive** -The synced Kubernetes `Secret` objects are **not deleted or force-resynced** by these upgrades. The change is an apiVersion bump on the *custom resources*, whose `spec` is schema-identical, so the controller keeps reconciling the same target Secrets. A controller restart triggers a normal **reconcile (re-assert, not delete)**. Caveat: no release note says verbatim "synced Secrets are preserved"; the conclusion is from (a) schema identity, (b) maintainers calling it "100% compatible" (issue #5478), (c) absence of any "secrets recreated/deleted" note. **Standard caution: snapshot/back up all ESO-created Secrets before the 0.16→0.17 step** (see §8 verification). Unrelated watch-item: v0.14.0 flagged a stateful-**generators** change — we use no generators, so N/A. - ---- - -## 5. Migration strategy (ordered, do-this-then-that) - -> **Pre-reqs every step:** run from an **unlocked** infra checkout (git-crypt unlocked); `vault login -method=oidc`; ESO is **Tier-0** so use `scripts/tg plan` / `scripts/tg apply` against `stacks/external-secrets` and **`git push`** after each apply (SOPS state). Claim presence before each apply: `~/code/scripts/presence claim stack:external-secrets --purpose "ESO 0.12→2.x migration step N"`. Wait for the controller `Deployment` to roll out healthy before the next hop. - -### Phase 0 — Pre-flight (no changes) -1. Confirm cluster k8s version and the version-check chain's current target; **coordinate with the k8s climb** (see §4.3 / Open Questions). Decide who drives the interleave. -2. `kubectl get crd | grep external-secrets.io` and for each: `kubectl get crd -o jsonpath='{.status.storedVersions}'` — confirm none still list `v1alpha1`. If any do, plan the `kubectl patch …/status storedVersions=["v1beta1"]` per the v0.16.0 note (do this *before* reaching 0.16). -3. **Snapshot all ESO-managed Secrets** (rollback safety net): - `kubectl get externalsecrets -A` (record the 104) and `for ns/secret in : kubectl get secret -n -o yaml > backup/-.yaml`. Keep outside git-crypt or encrypt. -4. Inspect `.terraform.lock.hcl` in `stacks/external-secrets` — record resolved `helm` + `kubernetes` provider versions. If helm provider < what 2.6.0 needs (it doesn't appear to need anything beyond SDK v3), bump the constraint as its own committed change first. -5. Read `docs/architecture/secrets.md` + the fire-planner first-apply comment to re-confirm the `-target` pattern for the v1 rewrite step. - -### Phase 1 — Climb to 0.16.x (chart bump only, NO manifest change yet) -ESO `0.16.x` is the **transition version** that serves *both* v1beta1 and v1. Climb to it one minor at a time, leaving all CRs as `v1beta1`: -6. For `v` in `0.13.0, 0.14.0, 0.15.x, 0.16.2` (use latest patch of each minor): set `helm_release.external_secrets.version = ""`, `scripts/tg plan` (expect: chart upgrade + CRD upgrade in place; **no `kubernetes_manifest` replacements** — apiVersion unchanged), `scripts/tg apply`, `git push`, wait for rollout, verify `kubectl get externalsecrets -A` all `SecretSynced=True`. - - **Interleave k8s as required:** before/at 0.16 the cluster should be on **k8s 1.32** (0.16 band). Advance k8s via the normal version-check chain to 1.32 around this point. - - Watch the **0.14.0** notes (generators) — N/A for us, but eyeball the plan diff anyway. -7. **Land on 0.16.2 and STOP.** Verify both APIs are served: `kubectl get externalsecrets.v1.external-secrets.io -A` and `kubectl get externalsecrets.v1beta1.external-secrets.io -A` both work. - -### Phase 2 — Rewrite all 104 CRs + 2 stores to `v1` (while on 0.16.2) -This is the MUST-DO-FIRST API migration, done in the safe window where both versions are served. -8. **Mechanical rewrite** across `stacks/`: replace the apiVersion string `external-secrets.io/v1beta1` → `external-secrets.io/v1` in every ExternalSecret and ClusterSecretStore `kubernetes_manifest` (104 + 2 = 106 occurrences across 73 files, **including the 6 nested-module files** in §2.2). **No other field changes** (schema identical). Do this in a worktree, committed file-by-file. - - Leave `secretStoreRef.kind = "ClusterSecretStore"` (that's a kind reference, not an apiVersion — unaffected). -9. **Two-phase apply because `kubernetes_manifest` replace + plan-time `data "kubernetes_secret"`:** - a. **Stores first:** `scripts/tg apply -target='kubernetes_manifest.css_vault_kv' -target='kubernetes_manifest.css_vault_db'` in `stacks/external-secrets` (they get replaced to v1; ESO still serves v1beta1 too, so in-flight ExternalSecrets keep syncing). `git push`. - b. **ExternalSecrets, per stack:** for each of the 73 stacks, `scripts/tg apply -target=kubernetes_manifest.` FIRST (materializes the replaced v1 CR + its Secret), THEN a full `scripts/tg apply` for that stack (lets the 15 plan-time `data "kubernetes_secret"` reads resolve against the now-existing Secret). The **15 plan-time-coupled stacks** (§2.6) absolutely need the `-target` first; the rest are lower-risk but follow the same pattern for safety. `git push` per stack (Tier-1 stacks use PG state; ESO stack is Tier-0). - - Because the spec is identical, the *replace* re-creates an identical CR; ESO reconciles and re-asserts the same target Secret (no value change) → apps keep their Secret throughout. -10. **Verify the rewrite fully landed:** `grep -rc 'external-secrets.io/v1beta1' stacks/` returns **0**; `kubectl get externalsecrets -A -o jsonpath used to confirm all served as v1`; all `SecretSynced=True`; spot-check a rotated DB cred (e.g. `nextcloud-db-creds`) still valid. - -### Phase 3 — Cross the 0.17 cutoff, then climb to 2.6.0 -Only after Phase 2 is 100% applied (zero v1beta1 in repo AND in etcd): -11. Bump chart `0.16.2 → 0.17.x`. `scripts/tg plan` (expect chart/CRD upgrade; **no manifest replacements** — already v1), apply, push, rollout, verify all synced. **k8s should be 1.33** (0.17 band) around here. -12. Continue one minor at a time: `0.18.x → 0.19.x → 0.20.x → 1.0.0 → 1.x (latest) → 2.0.0 → … → 2.6.0`. At each: bump `version`, plan, apply, push, rollout, verify synced. **k8s reaches 1.34 then 1.35** across the 2.x steps. - - **At 2.0.0:** confirm the plan shows nothing odd from the Alibaba/Device42 provider removal (we use only Vault — should be a no-op). -13. **Land on 2.6.0.** Verify: controller image `v2.6.0`, all 104 ExternalSecrets `SecretSynced=True`, both ClusterSecretStores `Valid=True`. - -### Phase 4 — Close the gate + docs -14. Advance k8s to **1.35** via the version-check chain if not already; confirm the **compat-gate now lists ESO as compatible** and 1.35 is unblocked. -15. Update `.claude/CLAUDE.md` Secrets Management section: correct counts (**104 ExternalSecrets / 73 files / 26 db-refs**), apiVersion now `v1`. Update `docs/architecture/secrets.md`. Commit as part of the work (audit trail). - ---- - -## 6. Risks & mitigations - -| Risk | Likelihood | Mitigation | -|---|---|---| -| **Secret-sync outage → app DB/API auth failures** during controller restarts or the replace window | Med | Spec is identical so re-sync re-asserts the same value; keep each controller restart short; do Phase-2 replaces **per stack** (small blast radius); the 15 plan-time stacks use `-target` first so the Secret exists before dependents plan. Pre-step Secret snapshot (Phase 0.3) for instant restore. | -| **Crossing 0.17 with any CR still v1beta1** → ESO stops reconciling those, secrets go stale | High if rushed | Phase 2 gate: `grep -rc v1beta1 stacks/` **must be 0** AND `kubectl get …v1beta1…` returns nothing live before Phase 3. Do not skip 0.16. | -| **CRD removal/replace by helm dropping data** | Low | Chart manages CRDs in-place via `installCRDs=true` (upgrade, not delete-recreate); CRs are the data and they're untouched by a CRD *upgrade*. Snapshot anyway. Never `helm uninstall` (that can GC CRDs). | -| **No conversion webhook safety net** (must-rewrite-first) | Certain (by design) | Whole strategy is built on rewriting at 0.16. The deprecated `unsafeServeV1Beta1` is already past its 2026-05-01 removal — do NOT rely on it. | -| **`kubernetes_manifest` forces replace on apiVersion bump** → transient gap + plan-time read failures | High | Two-phase `-target` apply fleet-wide (Phase 2.9); identical spec ⇒ replacement CR is equivalent. | -| **Vault 7-day DB rotation lands mid-migration** → a Secret stale across rotation if controller down | Med | Keep controller downtime < rotation gap; re-sync immediately after each hop; Reloader annotations already re-roll pods on Secret change; if a rotation is imminent, sequence the affected db stacks last and verify those creds explicitly. | -| **git-crypt tls-secret-sync landmine** | Low (not in ESO stack) | ESO stack has no `file()` git-crypt reads; run from an **unlocked** checkout; do **not** piggyback kyverno applies during this work. | -| **helm/k8s provider in lockfile too old for 2.x chart** | Low | Phase 0.4 verify; bump constraint as a separate committed change if needed (chart needs only Helm SDK v3, already satisfied). | -| **k8s/ESO band mismatch** (e.g. ESO 0.12 on k8s 1.33) | Med | Interleave the climbs per §4.3; don't jump k8s far ahead of ESO or vice-versa. | -| **Many small applies = long, error-prone session** | Med | Script the per-stack `-target`-then-full loop; checkpoint with `kubectl get externalsecrets -A` after each; the rewrite itself is a single `sed`-class change so low semantic risk. | - ---- - -## 7. Rollback plan (per hop) - -- **During Phase 1 (chart climb, still v1beta1):** revert `version` to the previous minor in `stacks/external-secrets/main.tf`, `scripts/tg apply`, `git push`. Helm rolls the controller back; CRs unchanged. Clean. -- **During Phase 2 (v1 rewrite, on 0.16.2):** 0.16.2 serves both APIs, so you can `git revert` the apiVersion-bump commits and re-apply — the CRs flip back to v1beta1 cleanly (both served). Secrets unaffected (identical spec). This is the **last point of easy rollback**. -- **After Phase 3 (≥0.17, v1beta1 no longer served):** **rollback is HARD** — once etcd stores v1-only and the controller is ≥0.17, downgrading cannot re-serve v1beta1 and v1 objects can't be auto-converted back ([general guidance + maintainer position](https://github.com/external-secrets/external-secrets/issues/5478)). Treat **crossing 0.17 as the point of no return.** If you must recover: re-install 0.16.2 (serves both), restore CRs from the Phase-0 manifest snapshot, and restore Secrets from the Secret snapshot. This is a disaster-recovery path, not a routine rollback — hence the Phase-2 gate must be airtight. -- **Always available:** the Phase-0.3 Secret backups let you `kubectl apply` the last-good Secret to keep an app authenticating while you fix ESO. - ---- - -## 8. Verification - -**Per hop:** -- `kubectl -n external-secrets get deploy,po` healthy; controller image tag == target. -- `kubectl get externalsecrets -A` → all 104 `STATUS=SecretSynced` / `READY=True`. -- `kubectl get clustersecretstores` → `vault-kv` + `vault-database` `Valid=True`. - -**After Phase 2 (v1 rewrite):** -- `grep -rc 'external-secrets.io/v1beta1' stacks/` → **0**. -- `kubectl get externalsecrets.v1beta1.external-secrets.io -A` → still served on 0.16 (sanity), but `kubectl get externalsecrets.v1.external-secrets.io -A` is the real check. -- Spot-check a rotated DB cred end-to-end: e.g. `nextcloud-db-creds` value matches `vault read database/static-creds/mysql-nextcloud` and the app authenticates. - -**Final (2.6.0):** -- Controller image `v2.6.0`; all ExternalSecrets synced; both stores valid. -- Diff a sample of the 104 target Secrets against the Phase-0 backups → values unchanged (continuity proof). -- App health: spot-check 3–4 high-value consumers (nextcloud, immich, grafana, a `vault-database` consumer) — pods running, no auth errors in logs. -- **Compat-gate:** run the upgrade-state / k8s-version-check audit — ESO no longer flagged as a 1.35 blocker; k8s 1.35 upgrade proceeds. - ---- - -## 9. Open questions - -1. **k8s/ESO interleave ownership.** §4.3 shows narrow per-version k8s bands (0.16→1.32, 0.17→1.33, 2.x→1.34-1.35). The cluster is currently ≤1.31. **Who drives the interleave** — does this migration also advance k8s step-by-step, or does the autonomous version-check chain advance k8s and we time ESO hops to it? Need the exact current k8s version and the chain's behavior when ESO is the only gate. (Decisive for sequencing Phases 1/3.) -2. **2.6.0 ↔ k8s 1.35 explicit support.** The support matrix table currently ends at **2.5** (k8s 1.34-1.35). 2.6.0 exists on GitHub but the matrix row isn't appended yet; the whole 2.x band is consistently 1.34-1.35, so 2.6 on 1.35 is a *strong inference* not a quoted row. Confirm via `Chart.yaml` `kubeVersion` of 2.6.0 or a 2.6 release note before relying on it. ([matrix](https://external-secrets.io/latest/introduction/stability-support/)) -3. **Resolved helm provider version.** The stack only pins `vault ~> 4.0`; helm/k8s are unpinned (lockfile-resolved). Confirm the lockfile version and whether to pin it explicitly as part of this work. (Chart needs only Helm SDK v3 — likely a no-op, but verify.) -4. **Intermediate-minor patch selection.** Use latest patch of each minor (0.13.x, 0.14.x, 0.15.x). Confirm 0.16.**2** specifically (the note says 0.16.2 already supports v1) vs a later 0.16 patch. -5. **Per-stack apply automation.** 73 stacks × (target + full) apply is large. Acceptable to script a loop, or prefer manual per-stack with checkpoints? Some stacks have other in-flight drift that a full apply would also push — needs a clean-plan check per stack first. -6. **Stateful generators / advanced features.** Confirmed we use none (0 SecretStore/PushSecret/ClusterExternalSecret/generators), so the v0.14 generator and v2.0 provider-removal breaking changes are N/A — but re-confirm no generator usage crept in before Phase 3. - ---- - -## 10. Sources (decisive facts) - -- Skip-version policy + k8s support matrix: -- `v1` promoted to storage version (0.16.0): -- `v1beta1` removed / "rewrite manifests to v1 first" (0.17.0): -- No conversion webhook / "not a conversion issue" (#5478): -- v1beta1↔v1 schema identical / "nothing fancy" (#4785): -- App v1.0.0 ≠ API v1: -- v2.0.0 only removes Alibaba/Device42: -- Chart 2.6.0 on ArtifactHub: diff --git a/docs/post-mortems/2026-06-22-devvm-mem-io-overload-containment.md b/docs/post-mortems/2026-06-22-devvm-mem-io-overload-containment.md deleted file mode 100644 index 664869fa..00000000 --- a/docs/post-mortems/2026-06-22-devvm-mem-io-overload-containment.md +++ /dev/null @@ -1,131 +0,0 @@ -# 2026-06-22 — devvm memory/IO overload: per-user containment + OOM backstop - -## Impact - -- devvm (VM 102, the shared multi-user Claude Code workstation) became - unresponsive under combined memory + IO pressure and had to be **hard-killed + - rebooted** by the admin on 2026-06-22 (morning). All ssh/tmux + t3 sessions for - wizard/emo/anca lost, in-flight agents killed. -- Signature on the last htop before the kill: **load avg ~60** on 32 vCPU, **RAM - 22.5/23.5G**, **swap 13.9/14.0G (full)**, a wall of **D-state** (uninterruptible - IO-wait) processes, and a single `ugrep` in emo's tmux holding **~10G RES / - 64% CPU**. Many `claude --effort max/xhigh` sessions + playwright-chrome MCP - instances across three users on top. - -## This is the "crawl" class, not the QEMU-stall class - -The 2026-06-11 post-mortem (`2026-06-11-devvm-qemu-io-stall.md`) fixed a -*different* failure mode — a QEMU-userspace block-path wedge on the legacy LSI -controller. That fix shipped (verified 2026-06-22: the guest now boots on -`virtio_scsi`, `scsihw: virtio-scsi-single + iothread`). Its post-mortem -explicitly deferred **this** class: - -> The recurring *crawl* class (agent storms → swap-thrash; journald -> watchdog-killed 3× on 2026-06-10) is a separate failure mode — ssh/tmux -> sessions remain memory-uncontained by **explicit decision (swap-only, -> 2026-06-10)**. - -That explicit decision is the root cause closed here. - -## Root cause - -Work on the devvm lives in **two independent cgroup-v2 trees per user**, and only -one was capped: - -| Tree | cgroup | Cap before today | -|---|---|---| -| t3 web sessions | `system.slice/system-t3\x2dserve.slice/t3-serve@` | `MemoryHigh=12G MemoryMax=16G MemorySwapMax=0 OOMPolicy=continue` ✓ | -| **ssh/tmux sessions** | `user.slice/user-.slice` | **`MemoryMax=infinity`, swap unlimited** ✗ | - -The uncapped `user-.slice` was the hole. A runaway there (the 10G `ugrep`; -stacked max-effort agents) grew unbounded, spilled into the **14G disk swap**, and -swap-thrashed the **host-mbps-throttled (60/60 MB/s) virtual disk**. That is the -overload chain: - -``` -uncapped tmux growth → disk-swap thrash on a throttled spindle - → IO storm (D-state pileup) → load ~60 → box unresponsive → hard kill -``` - -i.e. **memory pressure becomes the IO storm**. There was also **no global OOM -backstop** (no systemd-oomd / earlyoom) to shed the worst offender before the -kernel OOM or the thrash-wedge. And even the existing t3 caps don't sum safely -(3 users × 16G = 48G > 32G RAM) — nothing reasoned about the *whole box*. - -## Fix (`setup-devvm.sh` §10, applied live 2026-06-22) - -Design decisions (interviewed with the admin via `/grill-me`): **soft-generous -per-user caps + a hard ceiling + a kill-the-worst backstop**, maximising -single-user utilisation while making a box-wide wedge impossible. (The backstop -was first built on systemd-oomd, then switched to earlyoom mid-rollout when oomd -proved inert with `swap=0` — see Verification + Lessons.) - -| Layer | What | -|---|---| -| **Per-user caps, BOTH trees** | `user-.slice.d` drop-in gives every `user-.slice` the same `MemoryHigh=12G / MemoryMax=16G / MemorySwapMax=0` the t3 tree already had. A user is now bounded in whichever surface they work in. | -| **No disk swap for work** | `MemorySwapMax=0` on every work cgroup → a spike OOMs **locally** at the ceiling instead of thrashing the throttled disk. Kills the IO-storm-via-swap mechanism at the source. The 14G swapfile stays for system cold pages only. | -| **earlyoom backstop (free-RAM threshold)** | New package — used **instead of systemd-oomd** (which is inert with `swap=0`; see Lessons). Watches `MemAvailable%` and SIGTERMs the biggest task at **5%**, SIGKILL at **3%**, swap ignored (`-s 100`). `--avoid` keeps sshd/systemd/dockerd/containerd/t3-dispatch/tmux off the victim list (**the admin's way in always survives**); `--prefer` targets the agent/browser hogs (python3/node/chrome/…). Swap-independent and reliable, where oomd's pressure-kill was not. | -| **Fair-share CPU/IO** | `CPUWeight`/`IOWeight` per slice (system.slice 200, users + docker 100 each). Work-conserving — a lone user still gets all 32 cores + the full IO budget when others idle; weights only bite under contention. No hard CPU/IO caps. | -| **Docker containment** | Containers previously landed in `system.slice` — uncapped. Now `cgroup-parent: docker.slice` in `daemon.json` routes every container into a capped (`MemoryMax=8G`, swap 0) slice, so a runaway container is cgroup-OOM'd locally instead of escaping into the uncapped `system.slice`. | - -Durable in `setup-devvm.sh` (survives a VM rebuild); `earlyoom` added to -`packages.txt`. The numbers are tunable — `MemoryHigh=12G` will throttle a *lone* -heavy user between 12–16G even with RAM free; bump to 16/20 if that bites. - -## Verification (live, 2026-06-22) - -- **Caps live on running cgroups**: all three `user-.slice` report - `memory.high=12G memory.max=16G memory.swap.max=0`; `docker.slice` `memory.max=8G`; - daemon.json kept buildkit/nvidia/insecure-registries; paperless-mcp recovered - under `docker.slice`. -- **Stress test A (hard cap)** — the PRIMARY guard: a 2G-capped, swap=0 balloon was - killed at exactly 2G by the cgroup-local OOM (`constraint=CONSTRAINT_MEMCG`) with - **swap flat at 0MB throughout** — no thrash. Same mechanism protects every user - slice (16G) and `docker.slice` (8G). -- **Soft cap observed**: a balloon pushed past `MemoryHigh` sat at ~220M / 99% - memory.pressure, throttled to a crawl, making no progress and harming nothing — - a runaway is throttled, not just killed. -- **systemd-oomd disproven, then dropped**: a self-policed balloon held - `memory.pressure full avg10 = 96–99%` (≫ its 20% limit) for >70s but oomd never - killed it — `Pgscan: 0`. oomd's pressure-kill only acts on cgroups doing active - reclaim, which a `swap=0` anon workload never does. oomd was purged. -- **earlyoom backstop** — verified via `--dryrun`: at the threshold it logs - `low memory! … mem 90% swap 100%` (fires on RAM alone, swap ignored) and selects - `SIGTERM … "chrome"` (a `--prefer` hog), never an `--avoid`'d daemon. Live - earlyoom v1.7 confirms `SIGTERM mem<=5% / SIGKILL mem<=3%, swap<=100%`. - -## Out of scope / follow-ups - -- **Alerting** (tracked, fast-follow bead): `DevvmDown` (closes the 90-min - detection gap the 2026-06-11 PM flagged), sustained-memory-PSI/swap pressure - early-warning, and an "earlyoom-killed-something" alert (earlyoom logs each kill; - `-N /script` can push a metric). devvm node-exporter is already scraped - (`job=devvm`, `10.0.10.10:9100`), so only alert *rules* are new (a - monitoring-stack Terraform change). -- **zram cushion**: considered, deferred. Could let work cgroups absorb spikes in - compressed RAM instead of OOMing at the ceiling; not needed for the wedge fix. -- **Per-user docker isolation**: containers share one `docker.slice` budget, not - per-user. Fine for current usage (krr + short-lived tools). -- **Host-side IO**: the 60/60 mbps cap + the shared `sdc` HDD IO domain are - host-level (bead `code-oflt`); unchanged here. - -## Lessons - -- **"Swap as the safety valve" is an IO-storm amplifier on a throttled disk.** - Leaving ssh/tmux memory-uncontained (the 2026-06-10 decision) traded a clean - local OOM for a box-wide swap-thrash wedge. `MemorySwapMax=0` + a hard cap turns - the failure back into a contained, local kill. -- **Cap the box, not one surface.** t3 sessions were capped for months while the - same user's tmux was unbounded — and the caps that existed didn't sum to < RAM. - Containment has to reason about every tree and the aggregate. -- **A backstop must protect the operator's way in.** earlyoom `--avoid`s - sshd/systemd/dockerd/containerd/t3-dispatch/tmux, so the box always stays - reachable to recover; only the agent/browser hogs are eligible victims. -- **systemd-oomd is the wrong backstop for a no-swap box — verify, don't assume.** - oomd's memory-pressure killer only fires on cgroups doing active reclaim - (`pgscan` rising). With `MemorySwapMax=0` + anonymous memory there is nothing to - reclaim, so a cgroup sat at 99% `memory.pressure` indefinitely and oomd never - acted (proven with `oomctl` + a balloon). The very `swap=0` that kills the IO - storm also neuters oomd. earlyoom (free-RAM threshold, swap-independent) is the - correct pairing. A famous tool that "does OOM" still has to be proven to fire - under *your* configuration. diff --git a/docs/post-mortems/2026-06-24-kubeadm-oidc-drift-apiserver-upgrade-stall.md b/docs/post-mortems/2026-06-24-kubeadm-oidc-drift-apiserver-upgrade-stall.md deleted file mode 100644 index e6b11816..00000000 --- a/docs/post-mortems/2026-06-24-kubeadm-oidc-drift-apiserver-upgrade-stall.md +++ /dev/null @@ -1,97 +0,0 @@ -# Post-mortem: k8s 1.34→1.35 upgrade stalled — etcd IO starvation (2026-06-24) - -> Filename kept for inbound links. The originally-suspected cause (kubeadm-config -> OIDC drift) turned out **not** to be the crash — see "Correction" below. The OIDC -> drift was a real *separate* latent bug fixed in the same change. - -**Impact:** The autonomous k8s-version-upgrade chain (23:00 UTC nightly) reached -the master control-plane phase for the first time — preflight passed, etcd -snapshot taken, master cordoned + drained, etcd upgraded 3.6.5→3.6.6 — then the -kube-apiserver upgrade to v1.35.6 **crash-looped**. kubeadm waited its 5-minute -static-pod-hash window across all internal retries, then auto-rolled-back to -v1.34.9. The cluster stayed healthy on 1.34.9 (apiserver, all 7 nodes Ready), but -the run left **k8s-master cordoned** and the chain **wedged on `in_flight=1`**. -No data loss; no user-facing outage (the master carries control-plane taints, so -no workloads were displaced). - -**Trigger:** the first *minor* upgrade the chain ever attempted (1.34→1.35) — the -first time kubeadm upgrades etcd (3.6.5→3.6.6) and regenerates the control-plane -static pods, i.e. the first time the upgrade pushes real write-IO at etcd. - -## Root cause — etcd IO starvation on the shared HDD - -The new kube-apiserver could not establish/keep a working connection to etcd -during the upgrade because **etcd was IO-starved**. etcd's surviving container log -from the crash window (`/var/log/pods/.../etcd/0.log`, 23:04–23:20 UTC) shows: - -- **1,180** `apply request took too long` warnings in 16 minutes; -- individual applies of **4.3s / 2.9s / 2.7s / 1.8s** (healthy is <100ms), - clustered at **23:18:51 UTC** — exactly when kubeadm's final attempt was trying - to bring the new apiserver up. - -A reproduced 1.35.6 apiserver with no etcd dies with -`F instance.go:233 Error creating leases: error creating storage factory: context -deadline exceeded` — the same failure mode a multi-second etcd produces. etcd -lives on the contended `sdc` HDD (**beads code-oflt**: "etcd/critical VM disks on -shared sdc HDD — recurring IO-storm root cause"). The upgrade itself piled IO onto -that spindle: - -1. etcd's own upgrade-restart + WAL/db re-read (it restarted ~23:04, re-elected); -2. kubeadm dumping a full **~400MB etcd DB backup** to - `/etc/kubernetes/tmp/kubeadm-backup-etcd-/` (on the same HDD) before the - etcd upgrade — and **145 of these had accumulated to 28GB** (kubeadm never - cleans them up), pushing master root fs to **73%**, above the 70% kubelet - image-GC threshold, so image GC churned during the drain too; -3. master-drain pod evictions. - -### Correction — it was NOT the OIDC flag swap - -`kubeadm upgrade diff v1.35.6` showed the regenerated manifest also swaps -`--authentication-config` (structured multi-issuer OIDC) back to legacy -single-issuer `--oidc-*` flags (kubeadm-config drift, see secondary finding). That -was the *first* hypothesis — but an isolated repro of the 1.35.6 apiserver with -those exact `--oidc-*` flags **and authentik reachable** initialised OIDC cleanly -(`oidc.go:313`, no error) and ran fine until it hit the (deliberately dead) test -etcd. So the auth swap does **not** crash the apiserver; it was a red herring for -the crash. Image pull (all v1.35.6 images pre-pulled), OOM (none), and disk-full -were also ruled out. - -## Secondary finding (real, fixed separately) — kubeadm-config OIDC drift - -apiserver auth is configured in three places that must agree: -(1) `/etc/kubernetes/pki/auth-config.yaml` (structured, two issuers: `kubernetes` -+ `k8s-dashboard`, added 2026-06-19); (2) the live static-pod manifest -(`--authentication-config`); (3) the kubeadm-config `ClusterConfiguration` CM — -which still carried the legacy `--oidc-*` extraArgs. `kubeadm upgrade` regenerates -the manifest from (3), so it would have reverted structured auth → **dashboard + -kubectl SSO break after a successful upgrade** (recoverable: the chain's -post-master `restore.sh` re-adds the flag). This is a real bug, just not the crash. - -## Resolution - -1. **Reclaimed the 28GB kubeadm scratch** on master (`/etc/kubernetes/tmp/kubeadm-backup-*`) — root fs 73% → 23%. -2. **Reconciled kubeadm-config live** (zero cluster impact — CM only read at upgrade time): dropped `--oidc-*`, added `--authentication-config` via `kubeadm init phase upload-config kubeadm`. `kubeadm upgrade diff` then shows only the control-plane image bumps. -3. **Recovered:** uncordoned k8s-master, cleared the stuck `in_flight` gauge + annotation, deleted last night's Complete/Failed `1-35-6` phase jobs (a Complete preflight would otherwise make the detector idempotent-skip the re-run). - -## Prevention (landed in this change) - -| Gap | Fix | -|-----|-----| -| kubeadm leaks ~400MB etcd-DB backups into `/etc/kubernetes/tmp` forever (→ disk fills, image-GC churn, write-IO on etcd's spindle) | **`upgrade-step.sh` preflight now prunes** `/etc/kubernetes/tmp/kubeadm-backup-*` + `kubeadm-upgraded-manifests*` older than 3 days on master, every run. Best-effort, never aborts. | -| kubeadm-config drift would silently break SSO after an upgrade | `apiserver-oidc.tf`'s remote script now **also reconciles kubeadm-config** (`kubeadm init phase upload-config`), delivered via the `apiserver-oidc-restore` ConfigMap the chain re-runs (CI needs no ssh) or a local `-replace` apply. Preflight **alerts** (not blocks — SSO drift is recoverable) if `kubeadm upgrade diff` would still drop `--authentication-config`. | -| etcd on the contended `sdc` HDD starves under upgrade IO | **Durable fix is beads code-oflt** (move etcd/critical VM disks off `sdc`). Not in this change. Mitigations above reduce the upgrade's own IO; reclaimed disk removes the image-GC variable. | - -## Lessons - -- **Capture the failing component's own logs before concluding.** The `kubeadm - upgrade diff` made the OIDC swap look like the cause; only etcd's log (multi-second - applies) + an isolated apiserver repro showed the truth (etcd IO). A clean diff is - "what config changes," not "why it crashed." -- **etcd on shared HDD is the cluster's recurring fragility** (immich IO storm - 2026-05-25, this stall). Upgrades concentrate IO (etcd restart + kubeadm's 400MB - backup copy + drain) onto that spindle. code-oflt is the real fix. -- **Tools that leave per-operation scratch must be reaped.** kubeadm's - `/etc/kubernetes/tmp` etcd backups are throwaway (real backups → NFS) but never - GC'd; 28GB had silently accumulated. -- **Out-of-band control-plane edits must be written back to kubeadm-config** — else - `kubeadm upgrade` silently reverts them (here: SSO; could be admission/audit/API flags). diff --git a/docs/runbooks/k8s-version-upgrade.md b/docs/runbooks/k8s-version-upgrade.md index 021c588f..f611ccd2 100644 --- a/docs/runbooks/k8s-version-upgrade.md +++ b/docs/runbooks/k8s-version-upgrade.md @@ -41,8 +41,6 @@ Job 0 — preflight (pinned: k8s-node1) ├── halt-on-alert (kured-style ignore-list) ├── 24h-quiet baseline (no Ready transitions <24h ago) ├── kubeadm upgrade plan matches target (skipped when master already at target — partial-resume) - ├── apiserver-OIDC drift check: kubeadm upgrade diff drops --authentication-config? → Slack WARN (recoverable; not a block) - ├── reclaim kubeadm scratch: prune /etc/kubernetes/tmp/kubeadm-backup-* >3d on master (kubeadm leaks ~400MB etcd-db backups) ├── Push k8s_upgrade_in_flight=1, k8s_upgrade_started_timestamp=$(date +%s) ├── Trigger backup-etcd Job, wait, verify snapshot byte count ├── SSH master: containerd skew fix (if master < workers) @@ -173,27 +171,10 @@ Pushed by upgrade-step.sh during phase execution; observed by the - **`K8sVersionSkew`** — distinct kubelet/apiserver `gitVersion` count > 1 for 30m. Catches a half-done rollout. - **`EtcdPreUpgradeSnapshotMissing`** — `k8s_upgrade_in_flight==1 && k8s_upgrade_snapshot_taken==0` for 10m. Catches preflight Stage 2 failing silently. - **`K8sUpgradeStalled`** — `k8s_upgrade_in_flight==1 && time()-k8s_upgrade_started_timestamp > 5400` for 5m. Catches a Job in the chain dying without spawning its successor. -- **`K8sUpgradeChainJobFailed`** — `(kube_job_status_failed{namespace="k8s-upgrade",job_name=~"k8s-upgrade-(preflight|master|worker|postflight)-.*",reason=~"BackoffLimitExceeded|DeadlineExceeded"} > 0) unless on() (k8s_upgrade_blocked == 1)` for 15m (warning). Catches a phase Job that **terminally failed before `k8s_upgrade_in_flight` was set** — the preflight gates exit pre-metric, so the two `in_flight`-based alerts above are blind to a failed preflight (this is what hid the 5-day 1.34.9 wedge on 2026-06-12). Reason-scoped to terminal job conditions so a retry-success doesn't false-positive (a bare failed-pod-count would otherwise also block kured for the Job's 7d TTL). The `unless k8s_upgrade_blocked == 1` clause (added 2026-06-21) excludes a preflight that failed because the **compat gate deliberately refused** the target — that's owned by `K8sUpgradeBlocked` and was double-firing here; a genuine wedge exits without setting the blocked gauge, so it still fires. +- **`K8sUpgradeChainJobFailed`** — `kube_job_status_failed{namespace="k8s-upgrade",job_name=~"k8s-upgrade-.*",reason=~"BackoffLimitExceeded|DeadlineExceeded"} > 0` for 15m (warning). Catches a phase Job that **terminally failed before `k8s_upgrade_in_flight` was set** — the preflight gates exit pre-metric, so the two `in_flight`-based alerts above are blind to a failed preflight (this is what hid the 5-day 1.34.9 wedge on 2026-06-12). Reason-scoped to terminal job conditions so a retry-success doesn't false-positive (a bare failed-pod-count would otherwise also block kured for the Job's 7d TTL). - **`K8sUpgradeBlocked`** — `k8s_upgrade_blocked == 1` (warning). A k8s **auto-upgrade was refused** by the compat gate because a critical addon, an in-use deprecated API, or a node's containerd is too old for the detected target. The **specific reasons are in Slack**; clear it by upgrading the named addon / migrating the API caller / bumping containerd, after which the next nightly run proceeds (see "Auto-upgrade compat gate"). No upgrade was attempted, so this is not a half-done-rollout alert. - The first four alerts ALSO block kured (same `--prometheus-url` halt-on-alert mechanism) so the OS-reboot pipeline can't run on top of a half-done version upgrade. -### Nightly upgrade report (Slack) - -CronJob `k8s-upgrade-nightly-report` (k8s-upgrade ns, `var.report_schedule`, -default `7 6 * * *` = 06:07 UTC — after the 23:00 chain, before the 08:00 London -alert-digest) posts ONE Slack summary each morning of the previous night's run: -running version, detector freshness, detected target + kind, the outcome -(⚪ no upgrade needed / 🔴 blocked + live blocker reasons / 🟢 upgraded / -🟡 in progress / ⚠️ detector stale), and recent chain jobs. Read-only — it reads -the Pushgateway gauges + live nodes/jobs and re-runs `compat-gate.py` for fresh -blocker reasons; reuses the chain's SA + `slack_webhook` + scripts ConfigMap. -Logic + unit tests: `scripts/nightly-report.py`, `scripts/test_nightly_report.py`. -This is the day-to-day visibility layer (it does NOT replace the alerts above — -those fire on problems; this reports the outcome every night). Manual run: -`kubectl -n k8s-upgrade create job --from=cronjob/k8s-upgrade-nightly-report nightly-report-test` -(name it WITHOUT a `k8s-upgrade-{phase}-` prefix so a failure can't trip -`K8sUpgradeChainJobFailed`). - ### CoreDNS is NOT upgraded by kubeadm here CoreDNS runs a **custom split-horizon Corefile** (owned by the technitium stack) @@ -224,34 +205,22 @@ Exposed in K8s via ExternalSecret `k8s-upgrade-creds` in the `k8s-upgrade` names ## Common Operations -### apiserver OIDC + kubeadm upgrades (kubeadm-config reconciliation since 2026-06-24) +### Post-upgrade: apiserver OIDC restore (AUTOMATED by the chain since 2026-06-19) `kubeadm upgrade apply` **regenerates `/etc/kubernetes/manifests/kube-apiserver.yaml` -from kubeadm-config**. apiserver auth uses a structured multi-issuer -`--authentication-config` (kubectl + dashboard SSO), but kubeadm-config used to -still carry the legacy single-issuer `--oidc-*` extraArgs — so every upgrade -reverted the flag, **silently breaking SSO after the upgrade** (the apiserver does -NOT crash on this — verified by isolated repro; it's recoverable via the restore -script below). NB: the **1.34→1.35 stall on 2026-06-24 was a *separate* issue — -etcd IO starvation**, not this drift; post-mortem: -`docs/post-mortems/2026-06-24-kubeadm-oidc-drift-apiserver-upgrade-stall.md`. +and drops the `--authentication-config` flag**, silently disabling apiserver +OIDC (kubectl/kubelogin CLI **and** the web dashboard SSO break — tokens get +401). This used to require a manual re-apply after **every** control-plane bump. -**Primary fix (2026-06-24):** `stacks/rbac/modules/rbac/apiserver-oidc.tf` now -**reconciles kubeadm-config** (`kubeadm init phase upload-config kubeadm`, rewriting -`apiServer.extraArgs`: drop `--oidc-*`, add `--authentication-config`) as part of -its remote script. So kubeadm regenerates a **correct** manifest and the apiserver -upgrades with a pure image bump — `kubeadm upgrade diff ` shows only the -image change. Zero live impact (the CM is read only during an upgrade). - -**Backstops:** -- **Preflight check 4b** runs `kubeadm upgrade diff` and **alerts** (Slack WARN, does - NOT block — the drift only breaks SSO, which is recoverable) if - `--authentication-config` would still be dropped. -- The `rbac` stack still publishes its restore script to the - `kube-system/apiserver-oidc-restore` ConfigMap, and `phase_master` re-runs it on - master right after `kubeadm upgrade apply` (idempotent, `/livez`-gated with - auto-rollback, non-fatal) — now redundant belt-and-suspenders that *also* - re-reconciles kubeadm-config. Self-skips when master is already at target. +**Now automated:** the `rbac` stack publishes its OIDC restore script to the +`kube-system/apiserver-oidc-restore` ConfigMap, and the version-upgrade chain's +`phase_master` re-runs it on master immediately after `kubeadm upgrade apply` +(while tigera-operator is still quiesced, so the flag-add apiserver restart can't +crashloop the operator). It's idempotent, health-gates `/livez` with +auto-rollback, and is **non-fatal** — a failure only lags SSO until the next rbac +apply (the version upgrade itself already succeeded). So a chain-driven +control-plane bump no longer breaks SSO. The master phase self-skips when master +is already at target, so this only runs when master was actually upgraded. **Manual fallback** — only for an out-of-band/manual `kubeadm` upgrade, or if the chain logged `WARN: --authentication-config absent after re-apply`: diff --git a/scripts/t3-provision-users.sh b/scripts/t3-provision-users.sh index 9cbc6c1e..bd06b8e8 100644 --- a/scripts/t3-provision-users.sh +++ b/scripts/t3-provision-users.sh @@ -29,9 +29,6 @@ REPO_REMOTE_BASE="${REPO_REMOTE_BASE:-https://forgejo.viktorbarzin.me/viktor}" # Per-user OIDC kubeconfig (kubelogin/PKCE; cluster server+CA copied from the admin kubeconfig). OIDC_ISSUER="${OIDC_ISSUER:-https://authentik.viktorbarzin.me/application/o/kubernetes/}" ADMIN_KUBECONFIG="${ADMIN_KUBECONFIG:-/home/wizard/.kube/config}" -# OS users (space-separated) that receive the vendored agent skills (scripts/workstation/claude-skills). -# Allowlist: install_skills no-ops for anyone not listed. Extend here to roll out to more users. -SKILL_USERS="${SKILL_USERS:-emo}" log() { echo "[t3-provision] $*"; } run() { if [[ "$DRY_RUN" == 1 ]]; then echo "[dry-run] $*"; else "$@"; fi; } @@ -384,133 +381,9 @@ install_playwright() { run systemctl enable --now "playwright-snapshot-refresh@$user.timer" >/dev/null 2>&1 || true } -# Per-user homelab-memory setup — migrate off the claude-memory MCP/plugin to the -# homelab CLI hooks (auto-recall + auto-learn + compaction backup/recovery). -# Idempotent, if-absent, ADDITIVE: never clobbers `env` (the per-user -# MEMORY_API_KEY) or other MCP servers; removes ONLY the `claude_memory` MCP. -# Reuses the user's existing key — does NOT mint one (per-user isolation stays -# deferred, design 2026-06-08). The homelab CLI (/usr/local/bin/homelab) hits the -# same remote HTTP API the MCP used. Hook scripts: $WORKSTATION_DIR/claude-hooks. -install_memory() { - local user="$1" home - home="$(getent passwd "$user" | cut -d: -f6)" - [[ -n "$home" && -d "$home" ]] || return 0 - local src="$WORKSTATION_DIR/claude-hooks" hooks_dst="$home/.claude/hooks" settings="$home/.claude/settings.json" - [[ -d "$src" ]] || { log "WARN: $src missing -> skip memory setup for $user"; return 0; } - - if [[ "$DRY_RUN" == 1 ]]; then echo "[dry-run] memory: hooks + settings wire + claude_memory MCP removal -> $user"; return 0; fi - - # (1) (re)install the 4 hook scripts, owned by the user (refreshed each reconcile so fixes land) - install -d -o "$user" -g "$user" -m 0755 "$hooks_dst" - local h - for h in homelab-memory-recall.py auto-learn.py pre-compact-backup.sh post-compact-recovery.sh; do - install -o "$user" -g "$user" -m 0755 "$src/$h" "$hooks_dst/$h" - done - - # (2) wire the hooks in settings.json, if-absent + additive. Run the helper as ROOT: - # it must read $src under the admin's hardened home (mode 700), which a - # runuser-as-$user CANNOT traverse — so chown the result back to the user and - # enforce 0600 (it holds the per-user MEMORY_API_KEY). - if python3 "$src/wire-memory-hooks.py" "$home" >/dev/null 2>&1; then - [[ -f "$settings" ]] && chown "$user:$user" "$settings" 2>/dev/null || true - log "memory hooks wired -> $user" - else - log "WARN: memory hook wiring failed for $user (retries next reconcile)" - fi - [[ -f "$settings" ]] && chmod 600 "$settings" || true - - # (2b) reuse the user's existing key; warn (do NOT mint — needs an admin vault write) if absent. - if [[ -f "$settings" ]] && ! grep -q 'MEMORY_API_KEY' "$settings"; then - log "WARN: $user has no MEMORY_API_KEY in settings.json — homelab memory no-ops until an admin mints one" - fi - - # (3) remove the now-superseded claude_memory MCP (AS the user, if-present) + the plugin dir. - if runuser -u "$user" -- bash -lc 'command -v claude >/dev/null 2>&1 && claude mcp get claude_memory >/dev/null 2>&1'; then - runuser -u "$user" -- bash -lc 'claude mcp remove claude_memory >/dev/null 2>&1' && log "removed claude_memory MCP -> $user" || true - fi - if [[ -d "$home/.claude/plugins/claude-memory" ]]; then - rm -rf "$home/.claude/plugins/claude-memory" && log "removed claude-memory plugin dir -> $user" - fi - return 0 # best-effort tail must never return non-zero, else set -euo pipefail aborts the whole reconcile -} - -# Per-user agent skills, vendored from the in-repo snapshot ($WORKSTATION_DIR/claude-skills) — the -# `npx skills` upstream drifted off this exact set, so we reproduce it offline + deterministically. -# if-absent + ADDITIVE: copies a skill dir into ~/.agents/skills/ (owned by the user) and -# symlinks ~/.claude/skills/ -> ../../.agents/skills/ (the layout `skills add -g` -# produces; Claude Code reads ~/.claude/skills/). Scoped to SKILL_USERS. if-absent keys on the -# user's OWN copy, so it heals a stale/cross-user ~/.claude/skills symlink but never clobbers a real -# skill dir. Best-effort tail: must return 0 or set -euo pipefail aborts the whole reconcile. -install_skills() { - local user="$1" home - home="$(getent passwd "$user" | cut -d: -f6)" - [[ -n "$home" && -d "$home" ]] || return 0 - case " $SKILL_USERS " in *" $user "*) ;; *) return 0 ;; esac - local src_root="$WORKSTATION_DIR/claude-skills" - [[ -d "$src_root" ]] || { log "WARN: $src_root missing -> skip skills for $user"; return 0; } - - if [[ "$DRY_RUN" == 1 ]]; then - local d names="" - for d in "$src_root"/*/; do [[ -d "$d" ]] && names+="$(basename "$d") "; done - echo "[dry-run] vendor skills if-absent -> $user: ${names}" - return 0 - fi - - local agents_dir="$home/.agents/skills" claude_dir="$home/.claude/skills" - # own the parent ~/.agents too (install -d leaves created intermediates root-owned) - install -d -o "$user" -g "$user" -m 0755 "$home/.agents" "$agents_dir" "$claude_dir" - chown "$user:$user" "$home/.agents" || true - - local skill name dst link n=0 - for skill in "$src_root"/*/; do - [[ -d "$skill" ]] || continue - name="$(basename "$skill")" - dst="$agents_dir/$name" - link="$claude_dir/$name" - # if-absent keys on the user's OWN copy (a real dir under ~/.agents/skills), NOT on any - # pre-existing ~/.claude/skills entry — so a stale or cross-user symlink gets healed. - if [[ ! -d "$dst" ]]; then - cp -a "$src_root/$name" "$dst" || { log "WARN: copy skill $name -> $user failed"; continue; } - chown -R "$user:$user" "$dst" || true - n=$((n+1)) - fi - # point ~/.claude/skills/ at the user's own copy (replacing a stale/cross-user symlink); - # never clobber a real dir/file squatting that name. - if [[ -d "$link" && ! -L "$link" ]]; then - log "WARN: $claude_dir/$name is a real dir (left as-is) for $user" - elif [[ "$(readlink "$link" 2>/dev/null)" != "../../.agents/skills/$name" ]]; then - ln -sfn "../../.agents/skills/$name" "$link" && chown -h "$user:$user" "$link" || log "WARN: link skill $name -> $user failed" - fi - done - if [[ "$n" -gt 0 ]]; then log "vendored/healed $n skill(s) -> $user"; fi - return 0 # best-effort tail must never return non-zero, else set -euo pipefail aborts the reconcile -} - [[ $EUID -eq 0 ]] || { echo "t3-provision-users: must run as root" >&2; exit 1; } for bin in python3 jq; do command -v "$bin" >/dev/null || { echo "missing $bin" >&2; exit 1; }; done [[ -f "$ROSTER" && -f "$ENGINE" ]] || { echo "roster/engine not under $WORKSTATION_DIR" >&2; exit 1; } - -# 0) self-deploy: the repo is the authoring surface (like sync_managed_config / -# deploy_user_launcher below). Nothing else redeploys /usr/local/bin (only the -# manual setup-devvm.sh did) — so a committed edit silently never reached the -# hourly run until now (the homelab-memory rollout sat undeployed for a day). -# If the repo copy differs, install it and re-exec the fresh binary. Guarded: -# re-exec flag (no loop), bash -n (never deploy a broken script), DRY_RUN (no -# mutation), cmp (no churn when unchanged). -SELF_SRC="$WORKSTATION_DIR/../t3-provision-users.sh" -SELF_DST=/usr/local/bin/t3-provision-users -if [[ -z "${T3_PROVISION_SELF_DEPLOYED:-}" && -r "$SELF_SRC" ]] && ! cmp -s "$SELF_SRC" "$SELF_DST"; then - if [[ "$DRY_RUN" == 1 ]]; then - echo "[dry-run] self-deploy $SELF_DST from repo (changed)" - elif bash -n "$SELF_SRC" 2>/dev/null; then - install -m 0755 "$SELF_SRC" "$SELF_DST" - log "self-deployed $SELF_DST from repo (changed) — re-exec" - exec env T3_PROVISION_SELF_DEPLOYED=1 "$SELF_DST" "$@" - else - log "WARN: repo t3-provision-users.sh fails 'bash -n' — keeping deployed copy" - fi -fi - install -d -m 0755 "$ENVDIR" # 1) current sticky ports from existing .env files -> {os_user: port} @@ -621,21 +494,6 @@ while IFS=$'\t' read -r os_user pw_port; do install_playwright "$os_user" done < <(jq -r '.playwright_ports | to_entries[] | [.key, .value] | @tsv' "$desired_file") -# 5d) per-user homelab-memory (ALL users): replace the claude-memory MCP/plugin with the -# homelab CLI memory hooks. Idempotent + additive + if-absent; never touches the -# per-user MEMORY_API_KEY or other MCP servers (removes ONLY claude_memory). -while IFS=$'\t' read -r os_user; do - id "$os_user" >/dev/null 2>&1 || continue - install_memory "$os_user" -done < <(jq -r '.accounts[].os_user' "$desired_file") - -# 5e) per-user agent skills (SKILL_USERS allowlist only): vendored snapshot -> ~/.agents/skills -# + ~/.claude/skills symlinks. if-absent + additive; best-effort (never aborts the reconcile). -while IFS=$'\t' read -r os_user; do - id "$os_user" >/dev/null 2>&1 || continue - install_skills "$os_user" -done < <(jq -r '.accounts[].os_user' "$desired_file") - # 5b) machine-wide (once, not per-user): keep the t3 gated nightly TRACKER timer enabled (it # follows t3@nightly daily, gated; see t3-autoupdate.sh / docs/runbooks/t3-version-bump.md). # NEVER --now: the tracker installs a NEW build + migrates DBs + restarts serves, so firing diff --git a/scripts/workstation/claude-hooks/auto-learn.py b/scripts/workstation/claude-hooks/auto-learn.py deleted file mode 100755 index 174431f9..00000000 --- a/scripts/workstation/claude-hooks/auto-learn.py +++ /dev/null @@ -1,184 +0,0 @@ -#!/usr/bin/env python3 -""" -Stop hook (async): automatic learning extraction via haiku-as-judge. - -After each Claude response, sends the user message + assistant response to -haiku to detect corrections, preferences, decisions, or facts worth storing. -If learning events are detected, stores them via the `homelab memory` CLI — the -only sanctioned memory path on the devvm (no direct HTTP, no local SQLite). - -Runs with async: true — does NOT block the user. -""" - -import io -import json -import logging -import os -import shutil -import subprocess -import sys - -logger = logging.getLogger(__name__) - -JUDGE_PROMPT = """You are a memory extraction judge. Analyze this exchange between a user and an AI assistant. - -USER MESSAGE: -{user_message} - -ASSISTANT RESPONSE: -{assistant_response} - -Your job: determine if any of these learning events occurred: -1. USER CORRECTION — user corrected the assistant's mistake or misunderstanding -2. PREFERENCE — user stated a preference, habit, or "I like/prefer/want" statement -3. DECISION — a decision was reached about how to do something -4. FACT — user shared a durable fact about themselves, their team, tools, or environment - -If ANY learning event occurred, return JSON: -{{"events": [{{"type": "correction|preference|decision|fact", "content": "concise fact to remember (one sentence)", "importance": 0.7, "expanded_keywords": "space-separated semantically related search terms for recall (minimum 5 words)", "supersedes": null}}]}} - -If NO learning event occurred, return: -{{"events": []}} - -Rules: -- Only extract DURABLE facts, not transient task details -- Corrections are highest value (0.8-0.9) -- Be conservative — false negatives are better than false positives -- "expanded_keywords" should include synonyms, related concepts, and adjacent topics that would help find this memory later -- "supersedes" should be a search query to find the old outdated memory, or null -- Return ONLY valid JSON, no other text""" - - -def _store_via_homelab_cli(content, category, tags, importance, expanded_keywords): - """Store one memory via the homelab CLI — the only sanctioned memory path on - the devvm (no direct HTTP, no local SQLite). The CLI defaults the API URL and - reads CLAUDE_MEMORY_API_KEY / MEMORY_API_KEY from the environment; if neither - is set (e.g. a user without a minted key) it no-ops silently.""" - homelab = shutil.which("homelab") or "/usr/local/bin/homelab" - if not os.path.exists(homelab): - return - if not (os.environ.get("CLAUDE_MEMORY_API_KEY") or os.environ.get("MEMORY_API_KEY")): - return - cmd = [ - homelab, "memory", "store", content, - "--category", category, - "--tags", tags, - "--importance", str(importance), - ] - if expanded_keywords: - # CLI wants comma-separated keywords; the judge emits space-separated terms. - keywords = ",".join(expanded_keywords.replace(",", " ").split()) - if keywords: - cmd += ["--keywords", keywords] - subprocess.run(cmd, capture_output=True, text=True, timeout=15, env=os.environ) - - -def main() -> None: - # Graceful exit if claude CLI is not available - if not shutil.which("claude"): - return - - try: - hook_input = json.load(sys.stdin) - except (json.JSONDecodeError, EOFError): - return - - if isinstance(hook_input, dict) and hook_input.get("stop_hook_active", False): - return - - transcript_path = "" - if isinstance(hook_input, dict): - transcript_path = hook_input.get("transcript_path", "") - - if not transcript_path or not os.path.exists(transcript_path): - return - - user_message = "" - assistant_response = "" - try: - MAX_TAIL_BYTES = 50_000 - with open(transcript_path, "rb") as f: - f.seek(0, io.SEEK_END) - size = f.tell() - f.seek(max(0, size - MAX_TAIL_BYTES)) - tail = f.read().decode("utf-8", errors="replace") - lines = tail.split("\n") - - for line in reversed(lines): - line = line.strip() - if not line: - continue - try: - entry = json.loads(line) - except json.JSONDecodeError: - continue - role = entry.get("role", "") - content = entry.get("content", "") - if isinstance(content, list): - content = " ".join( - b.get("text", "") for b in content - if isinstance(b, dict) and b.get("type") == "text" - ) - content = str(content)[:2000] - if role == "assistant" and not assistant_response: - assistant_response = content - elif role == "user" and not user_message: - user_message = content - if user_message and assistant_response: - break - except Exception: - return - - if not user_message or len(user_message.strip()) < 10: - return - - prompt = JUDGE_PROMPT.format( - user_message=user_message, - assistant_response=assistant_response[:1000], - ) - - try: - result = subprocess.run( - ["claude", "-p", prompt, "--model", "haiku"], - capture_output=True, text=True, timeout=30, - env={**os.environ, "CLAUDECODE": ""}, - ) - if result.returncode != 0: - return - response_text = result.stdout.strip() - if response_text.startswith("```"): - lines = response_text.split("\n") - lines = [l for l in lines if not l.strip().startswith("```")] - response_text = "\n".join(lines).strip() - judge_result = json.loads(response_text) - events = judge_result.get("events", []) - if not events: - return - except (subprocess.TimeoutExpired, json.JSONDecodeError, OSError): - return - - category_map = { - "correction": "preferences", - "preference": "preferences", - "decision": "decisions", - "fact": "facts", - } - - for event in events: - content = event.get("content", "") - if not content: - continue - event_type = event.get("type", "fact") - importance = max(0.0, min(1.0, float(event.get("importance", 0.7)))) - category = category_map.get(event_type, "facts") - tags = f"auto-learned,{event_type}" - expanded_keywords = event.get("expanded_keywords", "") - - try: - _store_via_homelab_cli(content, category, tags, importance, expanded_keywords) - except Exception: - pass # Never crash the async hook - - -if __name__ == "__main__": - main() diff --git a/scripts/workstation/claude-hooks/homelab-memory-recall.py b/scripts/workstation/claude-hooks/homelab-memory-recall.py deleted file mode 100755 index 7315f116..00000000 --- a/scripts/workstation/claude-hooks/homelab-memory-recall.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -"""UserPromptSubmit hook: inject relevant memories via `homelab memory recall`. - -Replaces the claude-memory MCP recall path. Instead of instructing the model to -call the memory_recall MCP tool, this hook runs the homelab CLI (a direct client -to the same claude-memory HTTP API) and injects the ACTUAL results as context — -so recall is automatic, needs no model tool-call, and works with the MCP -uninstalled. Best-effort: any failure exits 0 silently (recall just doesn't -happen that turn, exactly like the MCP being unavailable). - -Wizard-only trial of the MCP deprecation (2026-06-20). Reversible: restore the -plugin command in ~/.claude/settings.json (backup: settings.json.bak-pre-homelab-memory). -""" - -import json -import os -import shutil -import subprocess -import sys - - -def main() -> None: - try: - hook_input = json.load(sys.stdin) - except (json.JSONDecodeError, EOFError): - return - - prompt = "" - if isinstance(hook_input, dict): - prompt = hook_input.get("prompt") or hook_input.get("user_prompt") or "" - if not prompt and isinstance(hook_input.get("content"), str): - prompt = hook_input["content"] - prompt = (prompt or "").strip() - - # Same gates as the original recall hook: skip short prompts, code/JSON/XML blobs. - if len(prompt) < 10 or prompt[0] in "`{<": - return - - homelab = shutil.which("homelab") or "/usr/local/bin/homelab" - if not os.path.exists(homelab): - return - if not (os.environ.get("CLAUDE_MEMORY_API_KEY") or os.environ.get("MEMORY_API_KEY")): - return - - try: - res = subprocess.run( - [homelab, "memory", "recall", prompt, "--limit", "5"], - capture_output=True, text=True, timeout=4, env=os.environ, - ) - except (subprocess.TimeoutExpired, OSError): - return - - out = (res.stdout or "").strip() - if res.returncode != 0 or not out: - return - - context = ( - "Relevant stored memories (via `homelab memory recall`) — incorporate " - "naturally if useful; do NOT mention this lookup to the user:\n\n" + out - ) - print(json.dumps({ - "hookSpecificOutput": { - "hookEventName": "UserPromptSubmit", - "additionalContext": context, - } - })) - - -if __name__ == "__main__": - main() diff --git a/scripts/workstation/claude-hooks/post-compact-recovery.sh b/scripts/workstation/claude-hooks/post-compact-recovery.sh deleted file mode 100755 index 4687d951..00000000 --- a/scripts/workstation/claude-hooks/post-compact-recovery.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/bash -# UserPromptSubmit hook: Inject recovery context after compaction -# This hook runs on each user prompt, but only injects context once after compaction. - -# Read hook input from stdin -INPUT=$(cat) - -# Extract session ID -SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // .sessionId // "unknown"') - -# Define marker path -MEMORY_HOME="${MEMORY_HOME:-$HOME/.claude/claude-memory}" -MARKER_DIR="${MEMORY_HOME}/state/compaction-markers" -MARKER_FILE="${MARKER_DIR}/${SESSION_ID}.json" - -# Fast path: no marker means no recent compaction, exit immediately -if [ ! -f "$MARKER_FILE" ]; then - exit 0 -fi - -# Read marker contents -MARKER=$(cat "$MARKER_FILE") - -# Validate JSON before processing -if ! echo "$MARKER" | jq -e . >/dev/null 2>&1; then - rm -f "$MARKER_FILE" - exit 0 -fi - -# Extract data from marker -COMPACTED_AT=$(echo "$MARKER" | jq -r '.compactedAt // "unknown"') -PERSONALITY=$(echo "$MARKER" | jq -r '.personalityReminder // ""') - -# Build remembered facts summary (limit to ~500 chars) -FACTS_SUMMARY=$(echo "$MARKER" | jq -r ' - .rememberedFacts[:10] | - map("- [\(.category // "fact")] \(.content)") | - join("\n") -' 2>/dev/null || echo "") - -# Build recovery context (kept under 1000 tokens) -RECOVERY_CONTEXT="[Claude Memory Recovery - Context compacted at ${COMPACTED_AT}] - -${PERSONALITY} - -Key memories from before compaction: -${FACTS_SUMMARY} - -Use the memory_recall MCP tool if you need more context about past conversations." - -# Output JSON with additional context for injection -cat << EOF -{ - "hookSpecificOutput": { - "hookEventName": "UserPromptSubmit", - "additionalContext": $(echo "$RECOVERY_CONTEXT" | jq -Rs .) - } -} -EOF - -# Delete marker file (one-time injection) -rm -f "$MARKER_FILE" - -exit 0 diff --git a/scripts/workstation/claude-hooks/pre-compact-backup.sh b/scripts/workstation/claude-hooks/pre-compact-backup.sh deleted file mode 100755 index 1194b12d..00000000 --- a/scripts/workstation/claude-hooks/pre-compact-backup.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash -# PreCompact hook: Save key memories before compaction -set -e - -INPUT=$(cat) -SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // .sessionId // "unknown"') - -MEMORY_HOME="${MEMORY_HOME:-$HOME/.claude/claude-memory}" -MARKER_DIR="${MEMORY_HOME}/state/compaction-markers" -MEMORY_DB="${MEMORY_HOME}/memory/memory.db" -MARKER_FILE="${MARKER_DIR}/${SESSION_ID}.json" - -mkdir -p "$MARKER_DIR" - -TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") - -# Try API first, fall back to SQLite -REMEMBERED_FACTS="[]" -if [ -n "${MEMORY_API_KEY:-${CLAUDE_MEMORY_API_KEY:-}}" ]; then - API_KEY="${MEMORY_API_KEY:-${CLAUDE_MEMORY_API_KEY:-}}" - API_URL="${MEMORY_API_URL:-${CLAUDE_MEMORY_API_URL:-}}" - if [ -n "$API_URL" ]; then - REMEMBERED_FACTS=$(curl -sf -H "Authorization: Bearer ${API_KEY}" \ - "${API_URL}/api/memories?limit=20" 2>/dev/null | \ - jq '[.memories[] | {content, category, importance}]' 2>/dev/null || echo "[]") - fi -elif [ -f "$MEMORY_DB" ]; then - REMEMBERED_FACTS=$(sqlite3 -json "$MEMORY_DB" \ - "SELECT content, category, importance FROM memories ORDER BY importance DESC, created_at DESC LIMIT 20" 2>/dev/null || echo "[]") -fi - -if ! echo "$REMEMBERED_FACTS" | jq empty 2>/dev/null; then - REMEMBERED_FACTS="[]" -fi - -jq -n \ - --arg sid "$SESSION_ID" \ - --arg ts "$TIMESTAMP" \ - --argjson facts "$REMEMBERED_FACTS" \ - '{sessionId: $sid, compactedAt: $ts, rememberedFacts: $facts}' \ - > "$MARKER_FILE" - -exit 0 diff --git a/scripts/workstation/claude-hooks/wire-memory-hooks.py b/scripts/workstation/claude-hooks/wire-memory-hooks.py deleted file mode 100644 index c33b504c..00000000 --- a/scripts/workstation/claude-hooks/wire-memory-hooks.py +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env python3 -"""Wire the homelab-memory hooks into a user's ~/.claude/settings.json. - -Part of the claude-memory MCP -> homelab CLI migration (all-users rollout). -Two passes, idempotent, never touching `env` (the per-user MEMORY_API_KEY) or any -other setting: - (0) PRUNE any hook command still pointing at the retired claude-memory plugin - (`plugins/claude-memory/hooks/`). install_memory() rm -rf's that dir, so - those entries are dangling — and a missing UserPromptSubmit hook exits 2, - a BLOCKING error that erases the prompt and freezes the session (devvm emo - incident 2026-06-22). Must run BEFORE the additive pass: the plugin shares - basenames with the homelab hooks, so without pruning, the "already present" - check below matches the dead plugin path and skips the real install. - (1) ADD each homelab hook group when no existing command references its script. - -Usage: wire-memory-hooks.py -Exit 0 on success (changed or already-present); 1 only on an unreadable settings file. -""" -import json -import os -import sys - -home = sys.argv[1] -settings = os.path.join(home, ".claude", "settings.json") -hooks_dir = os.path.join(home, ".claude", "hooks") - -# (event, script-basename used for the if-absent check, full command, extra fields) -WANT = [ - ("PreCompact", "pre-compact-backup.sh", f"{hooks_dir}/pre-compact-backup.sh", {"timeout": 30}), - ("UserPromptSubmit", "post-compact-recovery.sh", f"{hooks_dir}/post-compact-recovery.sh", {"timeout": 10}), - ("UserPromptSubmit", "homelab-memory-recall.py", f"python3 {hooks_dir}/homelab-memory-recall.py", {"timeout": 8}), - ("Stop", "auto-learn.py", f"python3 {hooks_dir}/auto-learn.py", {"async": True}), -] - -try: - if os.path.exists(settings) and os.path.getsize(settings) > 0: - with open(settings) as fh: - data = json.load(fh) - else: - data = {} -except (json.JSONDecodeError, OSError) as e: - print(f"ERROR: cannot read {settings}: {e}", file=sys.stderr) - sys.exit(1) - -hooks = data.setdefault("hooks", {}) -changed = False - -# (0) Prune dead claude-memory plugin hooks (see module docstring). Must precede -# the additive pass so shared basenames don't mask a needed install. -DEAD_REF = "plugins/claude-memory/hooks/" -for event in list(hooks.keys()): - new_groups = [] - removed_any = False - for g in (hooks.get(event) or []): - original = g.get("hooks") or [] - kept = [h for h in original if DEAD_REF not in (h.get("command", "") or "")] - if len(kept) != len(original): - removed_any = True - if kept: - new_groups.append({**g, "hooks": kept}) - if removed_any: - changed = True - if new_groups: - hooks[event] = new_groups - else: - del hooks[event] - -# (1) Additively wire each homelab hook, if no command already references it. -for event, basename, command, extra in WANT: - groups = hooks.setdefault(event, []) - already = any( - basename in (h.get("command", "") or "") - for g in groups - for h in (g.get("hooks", []) or []) - ) - if already: - continue - entry = {"type": "command", "command": command} - entry.update(extra) - groups.append({"hooks": [entry]}) - changed = True - -if changed: - tmp = settings + ".tmp" - with open(tmp, "w") as fh: - json.dump(data, fh, indent=2) - os.replace(tmp, settings) - print(f"wired memory hooks -> {settings}") -else: - print(f"memory hooks already present -> {settings} (no change)") diff --git a/scripts/workstation/claude-skills/README.md b/scripts/workstation/claude-skills/README.md deleted file mode 100644 index 816cbcb7..00000000 --- a/scripts/workstation/claude-skills/README.md +++ /dev/null @@ -1,31 +0,0 @@ -# claude-skills — vendored agent-skill snapshot - -Point-in-time snapshot of the admin's (`wizard`) Claude Code agent skills, deployed -per-user by `install_skills()` in `../../t3-provision-users.sh` (scoped to the -`SKILL_USERS` allowlist). Each subdirectory is one skill (`SKILL.md` + any bundled -references). The provisioner copies a skill into `~/.agents/skills//` (owned by -the user) and symlinks `~/.claude/skills/ -> ../../.agents/skills/` — the -layout the `skills` CLI's `-g` install produces; Claude Code reads `~/.claude/skills/`. - -## Why vendored (not `npx skills add` at provision time) - -Upstream drifted from this set: on `mattpocock/skills` master, `diagnose` → -`diagnosing-bugs` and `write-a-skill` → `writing-great-skills` were renamed, and -`caveman` + `zoom-out` are no longer published — so `npx skills` cannot reproduce this -exact set. Vendoring is also offline/deterministic and keeps GitHub-clone + -unpinned-CLI dependencies out of the hourly **root** reconcile. - -## Sources - -- `mattpocock/skills` (https://github.com/mattpocock/skills) — all except `find-skills` -- `vercel-labs/skills` (https://github.com/vercel-labs/skills) — `find-skills` - -## Refreshing - -Re-snapshot from a current install and commit the diff: - -```sh -cp -a ~/.agents/skills/. scripts/workstation/claude-skills/ -``` - -Snapshot taken 2026-06-23. diff --git a/scripts/workstation/claude-skills/caveman/SKILL.md b/scripts/workstation/claude-skills/caveman/SKILL.md deleted file mode 100644 index 85770a38..00000000 --- a/scripts/workstation/claude-skills/caveman/SKILL.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -name: caveman -description: > - Ultra-compressed communication mode. Cuts token usage ~75% by dropping - filler, articles, and pleasantries while keeping full technical accuracy. - Use when user says "caveman mode", "talk like caveman", "use caveman", - "less tokens", "be brief", or invokes /caveman. ---- - -Respond terse like smart caveman. All technical substance stay. Only fluff die. - -## Persistence - -ACTIVE EVERY RESPONSE once triggered. No revert after many turns. No filler drift. Still active if unsure. Off only when user says "stop caveman" or "normal mode". - -## Rules - -Drop: articles (a/an/the), filler (just/really/basically/actually/simply), pleasantries (sure/certainly/of course/happy to), hedging. Fragments OK. Short synonyms (big not extensive, fix not "implement a solution for"). Abbreviate common terms (DB/auth/config/req/res/fn/impl). Strip conjunctions. Use arrows for causality (X -> Y). One word when one word enough. - -Technical terms stay exact. Code blocks unchanged. Errors quoted exact. - -Pattern: `[thing] [action] [reason]. [next step].` - -Not: "Sure! I'd be happy to help you with that. The issue you're experiencing is likely caused by..." -Yes: "Bug in auth middleware. Token expiry check use `<` not `<=`. Fix:" - -### Examples - -**"Why React component re-render?"** - -> Inline obj prop -> new ref -> re-render. `useMemo`. - -**"Explain database connection pooling."** - -> Pool = reuse DB conn. Skip handshake -> fast under load. - -## Auto-Clarity Exception - -Drop caveman temporarily for: security warnings, irreversible action confirmations, multi-step sequences where fragment order risks misread, user asks to clarify or repeats question. Resume caveman after clear part done. - -Example -- destructive op: - -> **Warning:** This will permanently delete all rows in the `users` table and cannot be undone. -> -> ```sql -> DROP TABLE users; -> ``` -> -> Caveman resume. Verify backup exist first. diff --git a/scripts/workstation/claude-skills/diagnose/SKILL.md b/scripts/workstation/claude-skills/diagnose/SKILL.md deleted file mode 100644 index ed55bda2..00000000 --- a/scripts/workstation/claude-skills/diagnose/SKILL.md +++ /dev/null @@ -1,117 +0,0 @@ ---- -name: diagnose -description: Disciplined diagnosis loop for hard bugs and performance regressions. Reproduce → minimise → hypothesise → instrument → fix → regression-test. Use when user says "diagnose this" / "debug this", reports a bug, says something is broken/throwing/failing, or describes a performance regression. ---- - -# Diagnose - -A discipline for hard bugs. Skip phases only when explicitly justified. - -When exploring the codebase, use the project's domain glossary to get a clear mental model of the relevant modules, and check ADRs in the area you're touching. - -## Phase 1 — Build a feedback loop - -**This is the skill.** Everything else is mechanical. If you have a fast, deterministic, agent-runnable pass/fail signal for the bug, you will find the cause — bisection, hypothesis-testing, and instrumentation all just consume that signal. If you don't have one, no amount of staring at code will save you. - -Spend disproportionate effort here. **Be aggressive. Be creative. Refuse to give up.** - -### Ways to construct one — try them in roughly this order - -1. **Failing test** at whatever seam reaches the bug — unit, integration, e2e. -2. **Curl / HTTP script** against a running dev server. -3. **CLI invocation** with a fixture input, diffing stdout against a known-good snapshot. -4. **Headless browser script** (Playwright / Puppeteer) — drives the UI, asserts on DOM/console/network. -5. **Replay a captured trace.** Save a real network request / payload / event log to disk; replay it through the code path in isolation. -6. **Throwaway harness.** Spin up a minimal subset of the system (one service, mocked deps) that exercises the bug code path with a single function call. -7. **Property / fuzz loop.** If the bug is "sometimes wrong output", run 1000 random inputs and look for the failure mode. -8. **Bisection harness.** If the bug appeared between two known states (commit, dataset, version), automate "boot at state X, check, repeat" so you can `git bisect run` it. -9. **Differential loop.** Run the same input through old-version vs new-version (or two configs) and diff outputs. -10. **HITL bash script.** Last resort. If a human must click, drive _them_ with `scripts/hitl-loop.template.sh` so the loop is still structured. Captured output feeds back to you. - -Build the right feedback loop, and the bug is 90% fixed. - -### Iterate on the loop itself - -Treat the loop as a product. Once you have _a_ loop, ask: - -- Can I make it faster? (Cache setup, skip unrelated init, narrow the test scope.) -- Can I make the signal sharper? (Assert on the specific symptom, not "didn't crash".) -- Can I make it more deterministic? (Pin time, seed RNG, isolate filesystem, freeze network.) - -A 30-second flaky loop is barely better than no loop. A 2-second deterministic loop is a debugging superpower. - -### Non-deterministic bugs - -The goal is not a clean repro but a **higher reproduction rate**. Loop the trigger 100×, parallelise, add stress, narrow timing windows, inject sleeps. A 50%-flake bug is debuggable; 1% is not — keep raising the rate until it's debuggable. - -### When you genuinely cannot build a loop - -Stop and say so explicitly. List what you tried. Ask the user for: (a) access to whatever environment reproduces it, (b) a captured artifact (HAR file, log dump, core dump, screen recording with timestamps), or (c) permission to add temporary production instrumentation. Do **not** proceed to hypothesise without a loop. - -Do not proceed to Phase 2 until you have a loop you believe in. - -## Phase 2 — Reproduce - -Run the loop. Watch the bug appear. - -Confirm: - -- [ ] The loop produces the failure mode the **user** described — not a different failure that happens to be nearby. Wrong bug = wrong fix. -- [ ] The failure is reproducible across multiple runs (or, for non-deterministic bugs, reproducible at a high enough rate to debug against). -- [ ] You have captured the exact symptom (error message, wrong output, slow timing) so later phases can verify the fix actually addresses it. - -Do not proceed until you reproduce the bug. - -## Phase 3 — Hypothesise - -Generate **3–5 ranked hypotheses** before testing any of them. Single-hypothesis generation anchors on the first plausible idea. - -Each hypothesis must be **falsifiable**: state the prediction it makes. - -> Format: "If is the cause, then will make the bug disappear / will make it worse." - -If you cannot state the prediction, the hypothesis is a vibe — discard or sharpen it. - -**Show the ranked list to the user before testing.** They often have domain knowledge that re-ranks instantly ("we just deployed a change to #3"), or know hypotheses they've already ruled out. Cheap checkpoint, big time saver. Don't block on it — proceed with your ranking if the user is AFK. - -## Phase 4 — Instrument - -Each probe must map to a specific prediction from Phase 3. **Change one variable at a time.** - -Tool preference: - -1. **Debugger / REPL inspection** if the env supports it. One breakpoint beats ten logs. -2. **Targeted logs** at the boundaries that distinguish hypotheses. -3. Never "log everything and grep". - -**Tag every debug log** with a unique prefix, e.g. `[DEBUG-a4f2]`. Cleanup at the end becomes a single grep. Untagged logs survive; tagged logs die. - -**Perf branch.** For performance regressions, logs are usually wrong. Instead: establish a baseline measurement (timing harness, `performance.now()`, profiler, query plan), then bisect. Measure first, fix second. - -## Phase 5 — Fix + regression test - -Write the regression test **before the fix** — but only if there is a **correct seam** for it. - -A correct seam is one where the test exercises the **real bug pattern** as it occurs at the call site. If the only available seam is too shallow (single-caller test when the bug needs multiple callers, unit test that can't replicate the chain that triggered the bug), a regression test there gives false confidence. - -**If no correct seam exists, that itself is the finding.** Note it. The codebase architecture is preventing the bug from being locked down. Flag this for the next phase. - -If a correct seam exists: - -1. Turn the minimised repro into a failing test at that seam. -2. Watch it fail. -3. Apply the fix. -4. Watch it pass. -5. Re-run the Phase 1 feedback loop against the original (un-minimised) scenario. - -## Phase 6 — Cleanup + post-mortem - -Required before declaring done: - -- [ ] Original repro no longer reproduces (re-run the Phase 1 loop) -- [ ] Regression test passes (or absence of seam is documented) -- [ ] All `[DEBUG-...]` instrumentation removed (`grep` the prefix) -- [ ] Throwaway prototypes deleted (or moved to a clearly-marked debug location) -- [ ] The hypothesis that turned out correct is stated in the commit / PR message — so the next debugger learns - -**Then ask: what would have prevented this bug?** If the answer involves architectural change (no good test seam, tangled callers, hidden coupling) hand off to the `/improve-codebase-architecture` skill with the specifics. Make the recommendation **after** the fix is in, not before — you have more information now than when you started. diff --git a/scripts/workstation/claude-skills/diagnose/scripts/hitl-loop.template.sh b/scripts/workstation/claude-skills/diagnose/scripts/hitl-loop.template.sh deleted file mode 100644 index 40afc465..00000000 --- a/scripts/workstation/claude-skills/diagnose/scripts/hitl-loop.template.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env bash -# Human-in-the-loop reproduction loop. -# Copy this file, edit the steps below, and run it. -# The agent runs the script; the user follows prompts in their terminal. -# -# Usage: -# bash hitl-loop.template.sh -# -# Two helpers: -# step "" → show instruction, wait for Enter -# capture VAR "" → show question, read response into VAR -# -# At the end, captured values are printed as KEY=VALUE for the agent to parse. - -set -euo pipefail - -step() { - printf '\n>>> %s\n' "$1" - read -r -p " [Enter when done] " _ -} - -capture() { - local var="$1" question="$2" answer - printf '\n>>> %s\n' "$question" - read -r -p " > " answer - printf -v "$var" '%s' "$answer" -} - -# --- edit below --------------------------------------------------------- - -step "Open the app at http://localhost:3000 and sign in." - -capture ERRORED "Click the 'Export' button. Did it throw an error? (y/n)" - -capture ERROR_MSG "Paste the error message (or 'none'):" - -# --- edit above --------------------------------------------------------- - -printf '\n--- Captured ---\n' -printf 'ERRORED=%s\n' "$ERRORED" -printf 'ERROR_MSG=%s\n' "$ERROR_MSG" diff --git a/scripts/workstation/claude-skills/find-skills/SKILL.md b/scripts/workstation/claude-skills/find-skills/SKILL.md deleted file mode 100644 index 114c6637..00000000 --- a/scripts/workstation/claude-skills/find-skills/SKILL.md +++ /dev/null @@ -1,142 +0,0 @@ ---- -name: find-skills -description: Helps users discover and install agent skills when they ask questions like "how do I do X", "find a skill for X", "is there a skill that can...", or express interest in extending capabilities. This skill should be used when the user is looking for functionality that might exist as an installable skill. ---- - -# Find Skills - -This skill helps you discover and install skills from the open agent skills ecosystem. - -## When to Use This Skill - -Use this skill when the user: - -- Asks "how do I do X" where X might be a common task with an existing skill -- Says "find a skill for X" or "is there a skill for X" -- Asks "can you do X" where X is a specialized capability -- Expresses interest in extending agent capabilities -- Wants to search for tools, templates, or workflows -- Mentions they wish they had help with a specific domain (design, testing, deployment, etc.) - -## What is the Skills CLI? - -The Skills CLI (`npx skills`) is the package manager for the open agent skills ecosystem. Skills are modular packages that extend agent capabilities with specialized knowledge, workflows, and tools. - -**Key commands:** - -- `npx skills find [query]` - Search for skills interactively or by keyword -- `npx skills add ` - Install a skill from GitHub or other sources -- `npx skills check` - Check for skill updates -- `npx skills update` - Update all installed skills - -**Browse skills at:** https://skills.sh/ - -## How to Help Users Find Skills - -### Step 1: Understand What They Need - -When a user asks for help with something, identify: - -1. The domain (e.g., React, testing, design, deployment) -2. The specific task (e.g., writing tests, creating animations, reviewing PRs) -3. Whether this is a common enough task that a skill likely exists - -### Step 2: Check the Leaderboard First - -Before running a CLI search, check the [skills.sh leaderboard](https://skills.sh/) to see if a well-known skill already exists for the domain. The leaderboard ranks skills by total installs, surfacing the most popular and battle-tested options. - -For example, top skills for web development include: -- `vercel-labs/agent-skills` — React, Next.js, web design (100K+ installs each) -- `anthropics/skills` — Frontend design, document processing (100K+ installs) - -### Step 3: Search for Skills - -If the leaderboard doesn't cover the user's need, run the find command: - -```bash -npx skills find [query] -``` - -For example: - -- User asks "how do I make my React app faster?" → `npx skills find react performance` -- User asks "can you help me with PR reviews?" → `npx skills find pr review` -- User asks "I need to create a changelog" → `npx skills find changelog` - -### Step 4: Verify Quality Before Recommending - -**Do not recommend a skill based solely on search results.** Always verify: - -1. **Install count** — Prefer skills with 1K+ installs. Be cautious with anything under 100. -2. **Source reputation** — Official sources (`vercel-labs`, `anthropics`, `microsoft`) are more trustworthy than unknown authors. -3. **GitHub stars** — Check the source repository. A skill from a repo with <100 stars should be treated with skepticism. - -### Step 5: Present Options to the User - -When you find relevant skills, present them to the user with: - -1. The skill name and what it does -2. The install count and source -3. The install command they can run -4. A link to learn more at skills.sh - -Example response: - -``` -I found a skill that might help! The "react-best-practices" skill provides -React and Next.js performance optimization guidelines from Vercel Engineering. -(185K installs) - -To install it: -npx skills add vercel-labs/agent-skills@react-best-practices - -Learn more: https://skills.sh/vercel-labs/agent-skills/react-best-practices -``` - -### Step 6: Offer to Install - -If the user wants to proceed, you can install the skill for them: - -```bash -npx skills add -g -y -``` - -The `-g` flag installs globally (user-level) and `-y` skips confirmation prompts. - -## Common Skill Categories - -When searching, consider these common categories: - -| Category | Example Queries | -| --------------- | ---------------------------------------- | -| Web Development | react, nextjs, typescript, css, tailwind | -| Testing | testing, jest, playwright, e2e | -| DevOps | deploy, docker, kubernetes, ci-cd | -| Documentation | docs, readme, changelog, api-docs | -| Code Quality | review, lint, refactor, best-practices | -| Design | ui, ux, design-system, accessibility | -| Productivity | workflow, automation, git | - -## Tips for Effective Searches - -1. **Use specific keywords**: "react testing" is better than just "testing" -2. **Try alternative terms**: If "deploy" doesn't work, try "deployment" or "ci-cd" -3. **Check popular sources**: Many skills come from `vercel-labs/agent-skills` or `ComposioHQ/awesome-claude-skills` - -## When No Skills Are Found - -If no relevant skills exist: - -1. Acknowledge that no existing skill was found -2. Offer to help with the task directly using your general capabilities -3. Suggest the user could create their own skill with `npx skills init` - -Example: - -``` -I searched for skills related to "xyz" but didn't find any matches. -I can still help you with this task directly! Would you like me to proceed? - -If this is something you do often, you could create your own skill: -npx skills init my-xyz-skill -``` diff --git a/scripts/workstation/claude-skills/grill-me/SKILL.md b/scripts/workstation/claude-skills/grill-me/SKILL.md deleted file mode 100644 index bd04394c..00000000 --- a/scripts/workstation/claude-skills/grill-me/SKILL.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -name: grill-me -description: Interview the user relentlessly about a plan or design until reaching shared understanding, resolving each branch of the decision tree. Use when user wants to stress-test a plan, get grilled on their design, or mentions "grill me". ---- - -Interview me relentlessly about every aspect of this plan until we reach a shared understanding. Walk down each branch of the design tree, resolving dependencies between decisions one-by-one. For each question, provide your recommended answer. - -Ask the questions one at a time. - -If a question can be answered by exploring the codebase, explore the codebase instead. diff --git a/scripts/workstation/claude-skills/grill-with-docs/ADR-FORMAT.md b/scripts/workstation/claude-skills/grill-with-docs/ADR-FORMAT.md deleted file mode 100644 index da7e78ec..00000000 --- a/scripts/workstation/claude-skills/grill-with-docs/ADR-FORMAT.md +++ /dev/null @@ -1,47 +0,0 @@ -# ADR Format - -ADRs live in `docs/adr/` and use sequential numbering: `0001-slug.md`, `0002-slug.md`, etc. - -Create the `docs/adr/` directory lazily — only when the first ADR is needed. - -## Template - -```md -# {Short title of the decision} - -{1-3 sentences: what's the context, what did we decide, and why.} -``` - -That's it. An ADR can be a single paragraph. The value is in recording *that* a decision was made and *why* — not in filling out sections. - -## Optional sections - -Only include these when they add genuine value. Most ADRs won't need them. - -- **Status** frontmatter (`proposed | accepted | deprecated | superseded by ADR-NNNN`) — useful when decisions are revisited -- **Considered Options** — only when the rejected alternatives are worth remembering -- **Consequences** — only when non-obvious downstream effects need to be called out - -## Numbering - -Scan `docs/adr/` for the highest existing number and increment by one. - -## When to offer an ADR - -All three of these must be true: - -1. **Hard to reverse** — the cost of changing your mind later is meaningful -2. **Surprising without context** — a future reader will look at the code and wonder "why on earth did they do it this way?" -3. **The result of a real trade-off** — there were genuine alternatives and you picked one for specific reasons - -If a decision is easy to reverse, skip it — you'll just reverse it. If it's not surprising, nobody will wonder why. If there was no real alternative, there's nothing to record beyond "we did the obvious thing." - -### What qualifies - -- **Architectural shape.** "We're using a monorepo." "The write model is event-sourced, the read model is projected into Postgres." -- **Integration patterns between contexts.** "Ordering and Billing communicate via domain events, not synchronous HTTP." -- **Technology choices that carry lock-in.** Database, message bus, auth provider, deployment target. Not every library — just the ones that would take a quarter to swap out. -- **Boundary and scope decisions.** "Customer data is owned by the Customer context; other contexts reference it by ID only." The explicit no-s are as valuable as the yes-s. -- **Deliberate deviations from the obvious path.** "We're using manual SQL instead of an ORM because X." Anything where a reasonable reader would assume the opposite. These stop the next engineer from "fixing" something that was deliberate. -- **Constraints not visible in the code.** "We can't use AWS because of compliance requirements." "Response times must be under 200ms because of the partner API contract." -- **Rejected alternatives when the rejection is non-obvious.** If you considered GraphQL and picked REST for subtle reasons, record it — otherwise someone will suggest GraphQL again in six months. diff --git a/scripts/workstation/claude-skills/grill-with-docs/CONTEXT-FORMAT.md b/scripts/workstation/claude-skills/grill-with-docs/CONTEXT-FORMAT.md deleted file mode 100644 index eaf2a185..00000000 --- a/scripts/workstation/claude-skills/grill-with-docs/CONTEXT-FORMAT.md +++ /dev/null @@ -1,60 +0,0 @@ -# CONTEXT.md Format - -## Structure - -```md -# {Context Name} - -{One or two sentence description of what this context is and why it exists.} - -## Language - -**Order**: -{A one or two sentence description of the term} -_Avoid_: Purchase, transaction - -**Invoice**: -A request for payment sent to a customer after delivery. -_Avoid_: Bill, payment request - -**Customer**: -A person or organization that places orders. -_Avoid_: Client, buyer, account -``` - -## Rules - -- **Be opinionated.** When multiple words exist for the same concept, pick the best one and list the others under `_Avoid_`. -- **Keep definitions tight.** One or two sentences max. Define what it IS, not what it does. -- **Only include terms specific to this project's context.** General programming concepts (timeouts, error types, utility patterns) don't belong even if the project uses them extensively. Before adding a term, ask: is this a concept unique to this context, or a general programming concept? Only the former belongs. -- **Group terms under subheadings** when natural clusters emerge. If all terms belong to a single cohesive area, a flat list is fine. - -## Single vs multi-context repos - -**Single context (most repos):** One `CONTEXT.md` at the repo root. - -**Multiple contexts:** A `CONTEXT-MAP.md` at the repo root lists the contexts, where they live, and how they relate to each other: - -```md -# Context Map - -## Contexts - -- [Ordering](./src/ordering/CONTEXT.md) — receives and tracks customer orders -- [Billing](./src/billing/CONTEXT.md) — generates invoices and processes payments -- [Fulfillment](./src/fulfillment/CONTEXT.md) — manages warehouse picking and shipping - -## Relationships - -- **Ordering → Fulfillment**: Ordering emits `OrderPlaced` events; Fulfillment consumes them to start picking -- **Fulfillment → Billing**: Fulfillment emits `ShipmentDispatched` events; Billing consumes them to generate invoices -- **Ordering ↔ Billing**: Shared types for `CustomerId` and `Money` -``` - -The skill infers which structure applies: - -- If `CONTEXT-MAP.md` exists, read it to find contexts -- If only a root `CONTEXT.md` exists, single context -- If neither exists, create a root `CONTEXT.md` lazily when the first term is resolved - -When multiple contexts exist, infer which one the current topic relates to. If unclear, ask. diff --git a/scripts/workstation/claude-skills/grill-with-docs/SKILL.md b/scripts/workstation/claude-skills/grill-with-docs/SKILL.md deleted file mode 100644 index 5ea0aa91..00000000 --- a/scripts/workstation/claude-skills/grill-with-docs/SKILL.md +++ /dev/null @@ -1,88 +0,0 @@ ---- -name: grill-with-docs -description: Grilling session that challenges your plan against the existing domain model, sharpens terminology, and updates documentation (CONTEXT.md, ADRs) inline as decisions crystallise. Use when user wants to stress-test a plan against their project's language and documented decisions. ---- - - - -Interview me relentlessly about every aspect of this plan until we reach a shared understanding. Walk down each branch of the design tree, resolving dependencies between decisions one-by-one. For each question, provide your recommended answer. - -Ask the questions one at a time, waiting for feedback on each question before continuing. - -If a question can be answered by exploring the codebase, explore the codebase instead. - - - - - -## Domain awareness - -During codebase exploration, also look for existing documentation: - -### File structure - -Most repos have a single context: - -``` -/ -├── CONTEXT.md -├── docs/ -│ └── adr/ -│ ├── 0001-event-sourced-orders.md -│ └── 0002-postgres-for-write-model.md -└── src/ -``` - -If a `CONTEXT-MAP.md` exists at the root, the repo has multiple contexts. The map points to where each one lives: - -``` -/ -├── CONTEXT-MAP.md -├── docs/ -│ └── adr/ ← system-wide decisions -├── src/ -│ ├── ordering/ -│ │ ├── CONTEXT.md -│ │ └── docs/adr/ ← context-specific decisions -│ └── billing/ -│ ├── CONTEXT.md -│ └── docs/adr/ -``` - -Create files lazily — only when you have something to write. If no `CONTEXT.md` exists, create one when the first term is resolved. If no `docs/adr/` exists, create it when the first ADR is needed. - -## During the session - -### Challenge against the glossary - -When the user uses a term that conflicts with the existing language in `CONTEXT.md`, call it out immediately. "Your glossary defines 'cancellation' as X, but you seem to mean Y — which is it?" - -### Sharpen fuzzy language - -When the user uses vague or overloaded terms, propose a precise canonical term. "You're saying 'account' — do you mean the Customer or the User? Those are different things." - -### Discuss concrete scenarios - -When domain relationships are being discussed, stress-test them with specific scenarios. Invent scenarios that probe edge cases and force the user to be precise about the boundaries between concepts. - -### Cross-reference with code - -When the user states how something works, check whether the code agrees. If you find a contradiction, surface it: "Your code cancels entire Orders, but you just said partial cancellation is possible — which is right?" - -### Update CONTEXT.md inline - -When a term is resolved, update `CONTEXT.md` right there. Don't batch these up — capture them as they happen. Use the format in [CONTEXT-FORMAT.md](./CONTEXT-FORMAT.md). - -`CONTEXT.md` should be totally devoid of implementation details. Do not treat `CONTEXT.md` as a spec, a scratch pad, or a repository for implementation decisions. It is a glossary and nothing else. - -### Offer ADRs sparingly - -Only offer to create an ADR when all three are true: - -1. **Hard to reverse** — the cost of changing your mind later is meaningful -2. **Surprising without context** — a future reader will wonder "why did they do it this way?" -3. **The result of a real trade-off** — there were genuine alternatives and you picked one for specific reasons - -If any of the three is missing, skip the ADR. Use the format in [ADR-FORMAT.md](./ADR-FORMAT.md). - - diff --git a/scripts/workstation/claude-skills/handoff/SKILL.md b/scripts/workstation/claude-skills/handoff/SKILL.md deleted file mode 100644 index 28bfb3ab..00000000 --- a/scripts/workstation/claude-skills/handoff/SKILL.md +++ /dev/null @@ -1,13 +0,0 @@ ---- -name: handoff -description: Compact the current conversation into a handoff document for another agent to pick up. -argument-hint: "What will the next session be used for?" ---- - -Write a handoff document summarising the current conversation so a fresh agent can continue the work. Save it to a path produced by `mktemp -t handoff-XXXXXX.md` (read the file before you write to it). - -Suggest the skills to be used, if any, by the next session. - -Do not duplicate content already captured in other artifacts (PRDs, plans, ADRs, issues, commits, diffs). Reference them by path or URL instead. - -If the user passed arguments, treat them as a description of what the next session will focus on and tailor the doc accordingly. diff --git a/scripts/workstation/claude-skills/improve-codebase-architecture/DEEPENING.md b/scripts/workstation/claude-skills/improve-codebase-architecture/DEEPENING.md deleted file mode 100644 index ecaf5d7d..00000000 --- a/scripts/workstation/claude-skills/improve-codebase-architecture/DEEPENING.md +++ /dev/null @@ -1,37 +0,0 @@ -# Deepening - -How to deepen a cluster of shallow modules safely, given its dependencies. Assumes the vocabulary in [LANGUAGE.md](LANGUAGE.md) — **module**, **interface**, **seam**, **adapter**. - -## Dependency categories - -When assessing a candidate for deepening, classify its dependencies. The category determines how the deepened module is tested across its seam. - -### 1. In-process - -Pure computation, in-memory state, no I/O. Always deepenable — merge the modules and test through the new interface directly. No adapter needed. - -### 2. Local-substitutable - -Dependencies that have local test stand-ins (PGLite for Postgres, in-memory filesystem). Deepenable if the stand-in exists. The deepened module is tested with the stand-in running in the test suite. The seam is internal; no port at the module's external interface. - -### 3. Remote but owned (Ports & Adapters) - -Your own services across a network boundary (microservices, internal APIs). Define a **port** (interface) at the seam. The deep module owns the logic; the transport is injected as an **adapter**. Tests use an in-memory adapter. Production uses an HTTP/gRPC/queue adapter. - -Recommendation shape: *"Define a port at the seam, implement an HTTP adapter for production and an in-memory adapter for testing, so the logic sits in one deep module even though it's deployed across a network."* - -### 4. True external (Mock) - -Third-party services (Stripe, Twilio, etc.) you don't control. The deepened module takes the external dependency as an injected port; tests provide a mock adapter. - -## Seam discipline - -- **One adapter means a hypothetical seam. Two adapters means a real one.** Don't introduce a port unless at least two adapters are justified (typically production + test). A single-adapter seam is just indirection. -- **Internal seams vs external seams.** A deep module can have internal seams (private to its implementation, used by its own tests) as well as the external seam at its interface. Don't expose internal seams through the interface just because tests use them. - -## Testing strategy: replace, don't layer - -- Old unit tests on shallow modules become waste once tests at the deepened module's interface exist — delete them. -- Write new tests at the deepened module's interface. The **interface is the test surface**. -- Tests assert on observable outcomes through the interface, not internal state. -- Tests should survive internal refactors — they describe behaviour, not implementation. If a test has to change when the implementation changes, it's testing past the interface. diff --git a/scripts/workstation/claude-skills/improve-codebase-architecture/HTML-REPORT.md b/scripts/workstation/claude-skills/improve-codebase-architecture/HTML-REPORT.md deleted file mode 100644 index 8adc368f..00000000 --- a/scripts/workstation/claude-skills/improve-codebase-architecture/HTML-REPORT.md +++ /dev/null @@ -1,123 +0,0 @@ -# HTML Report Format - -The architectural review is rendered as a single self-contained HTML file in the OS temp directory. Tailwind and Mermaid both come from CDNs. Mermaid handles graph-shaped diagrams reliably; hand-built divs and inline SVG handle the more editorial visuals (mass diagrams, cross-sections). Mix the two — don't lean on Mermaid for everything, it'll start to look generic. - -## Scaffold - -```html - - - - - Architecture review — {{repo name}} - - - - - -
-
...
-
...
-
...
-
- - -``` - -## Header - -Repo name, date, and a compact legend: solid box = module, dashed line = seam, red arrow = leakage, thick dark box = deep module. No introduction paragraph — straight into the candidates. - -## Candidate card - -The diagrams carry the weight. Prose is sparse, plain, and uses the glossary terms ([LANGUAGE.md](LANGUAGE.md)) without ceremony. - -Each candidate is one `
`: - -- **Title** — short, names the deepening (e.g. "Collapse the Order intake pipeline"). -- **Badge row** — recommendation strength (`Strong` = emerald, `Worth exploring` = amber, `Speculative` = slate), plus a tag for the dependency category (`in-process`, `local-substitutable`, `ports & adapters`, `mock`). -- **Files** — monospaced list, `font-mono text-sm`. -- **Before / After diagram** — the centrepiece. Two columns, side by side. See patterns below. -- **Problem** — one sentence. What hurts. -- **Solution** — one sentence. What changes. -- **Wins** — bullets, ≤6 words each. e.g. "Tests hit one interface", "Pricing logic stops leaking", "Delete 4 shallow wrappers". -- **ADR callout** (if applicable) — one line in an amber-tinted box. - -No paragraphs of explanation. If the diagram needs a paragraph to be understood, redraw the diagram. - -## Diagram patterns - -Pick the pattern that fits the candidate. Mix them. Don't make every diagram look the same — variety is part of the point. - -### Mermaid graph (the workhorse for dependencies / call flow) - -Use a Mermaid `flowchart` or `graph` when the point is "X calls Y calls Z, and look at the mess." Wrap it in a Tailwind-styled card so it doesn't feel parachuted in. Style with classDef to colour leakage edges red and the deep module dark. Sequence diagrams work well for "before: 6 round-trips; after: 1." - -```html -
-
-    flowchart LR
-      A[OrderHandler] --> B[OrderValidator]
-      B --> C[OrderRepo]
-      C -.leak.-> D[PricingClient]
-      classDef leak stroke:#dc2626,stroke-width:2px;
-      class C,D leak
-  
-
-``` - -### Hand-built boxes-and-arrows (when Mermaid's layout fights you) - -Modules as `
`s with borders and labels. Arrows as inline SVG `` or `` elements positioned absolutely over a relative container. Reach for this when you want the "after" diagram to feel like one thick-bordered deep module with greyed-out internals — Mermaid won't render that with the right weight. - -### Cross-section (good for layered shallowness) - -Stack horizontal bands (`h-12 border-l-4`) to show layers a call passes through. Before: 6 thin layers each doing nothing. After: 1 thick band labelled with the consolidated responsibility. - -### Mass diagram (good for "interface as wide as implementation") - -Two rectangles per module — one for interface surface area, one for implementation. Before: interface rectangle is nearly as tall as the implementation rectangle (shallow). After: interface rectangle is short, implementation rectangle is tall (deep). - -### Call-graph collapse - -Before: a tree of function calls rendered as nested boxes. After: the same tree collapsed into one box, with the now-internal calls shown faded inside it. - -## Style guidance - -- Lean editorial, not corporate-dashboard. Generous whitespace. Serif optional for headings (`font-serif` works well with stone/slate). -- Colour sparingly: one accent (emerald or indigo) plus red for leakage and amber for warnings. -- Keep diagrams ~320px tall so before/after sits comfortably side by side without scrolling. -- Use `text-xs uppercase tracking-wider` for module labels inside diagrams — they should read as schematic, not as UI. -- The only scripts are the Tailwind CDN and the Mermaid ESM import. The report is otherwise static — no app code, no interactivity beyond Mermaid's own rendering. - -## Top recommendation section - -One larger card. Candidate name, one sentence on why, anchor link to its card. That's it. - -## Tone - -Plain English, concise — but the architectural nouns and verbs come straight from [LANGUAGE.md](LANGUAGE.md). Concision is not an excuse to drift. - -**Use exactly:** module, interface, implementation, depth, deep, shallow, seam, adapter, leverage, locality. - -**Never substitute:** component, service, unit (for module) · API, signature (for interface) · boundary (for seam) · layer, wrapper (for module, when you mean module). - -**Phrasings that fit the style:** - -- "Order intake module is shallow — interface nearly matches the implementation." -- "Pricing leaks across the seam." -- "Deepen: one interface, one place to test." -- "Two adapters justify the seam: HTTP in prod, in-memory in tests." - -**Wins bullets** name the gain in glossary terms: *"locality: bugs concentrate in one module"*, *"leverage: one interface, N call sites"*, *"interface shrinks; implementation absorbs the wrappers"*. Don't write *"easier to maintain"* or *"cleaner code"* — those terms aren't in the glossary and don't earn their place. - -No hedging, no throat-clearing, no "it's worth noting that…". If a sentence could be a bullet, make it a bullet. If a bullet could be cut, cut it. If a term isn't in [LANGUAGE.md](LANGUAGE.md), reach for one that is before inventing a new one. diff --git a/scripts/workstation/claude-skills/improve-codebase-architecture/INTERFACE-DESIGN.md b/scripts/workstation/claude-skills/improve-codebase-architecture/INTERFACE-DESIGN.md deleted file mode 100644 index 3197723a..00000000 --- a/scripts/workstation/claude-skills/improve-codebase-architecture/INTERFACE-DESIGN.md +++ /dev/null @@ -1,44 +0,0 @@ -# Interface Design - -When the user wants to explore alternative interfaces for a chosen deepening candidate, use this parallel sub-agent pattern. Based on "Design It Twice" (Ousterhout) — your first idea is unlikely to be the best. - -Uses the vocabulary in [LANGUAGE.md](LANGUAGE.md) — **module**, **interface**, **seam**, **adapter**, **leverage**. - -## Process - -### 1. Frame the problem space - -Before spawning sub-agents, write a user-facing explanation of the problem space for the chosen candidate: - -- The constraints any new interface would need to satisfy -- The dependencies it would rely on, and which category they fall into (see [DEEPENING.md](DEEPENING.md)) -- A rough illustrative code sketch to ground the constraints — not a proposal, just a way to make the constraints concrete - -Show this to the user, then immediately proceed to Step 2. The user reads and thinks while the sub-agents work in parallel. - -### 2. Spawn sub-agents - -Spawn 3+ sub-agents in parallel using the Agent tool. Each must produce a **radically different** interface for the deepened module. - -Prompt each sub-agent with a separate technical brief (file paths, coupling details, dependency category from [DEEPENING.md](DEEPENING.md), what sits behind the seam). The brief is independent of the user-facing problem-space explanation in Step 1. Give each agent a different design constraint: - -- Agent 1: "Minimize the interface — aim for 1–3 entry points max. Maximise leverage per entry point." -- Agent 2: "Maximise flexibility — support many use cases and extension." -- Agent 3: "Optimise for the most common caller — make the default case trivial." -- Agent 4 (if applicable): "Design around ports & adapters for cross-seam dependencies." - -Include both [LANGUAGE.md](LANGUAGE.md) vocabulary and CONTEXT.md vocabulary in the brief so each sub-agent names things consistently with the architecture language and the project's domain language. - -Each sub-agent outputs: - -1. Interface (types, methods, params — plus invariants, ordering, error modes) -2. Usage example showing how callers use it -3. What the implementation hides behind the seam -4. Dependency strategy and adapters (see [DEEPENING.md](DEEPENING.md)) -5. Trade-offs — where leverage is high, where it's thin - -### 3. Present and compare - -Present designs sequentially so the user can absorb each one, then compare them in prose. Contrast by **depth** (leverage at the interface), **locality** (where change concentrates), and **seam placement**. - -After comparing, give your own recommendation: which design you think is strongest and why. If elements from different designs would combine well, propose a hybrid. Be opinionated — the user wants a strong read, not a menu. diff --git a/scripts/workstation/claude-skills/improve-codebase-architecture/LANGUAGE.md b/scripts/workstation/claude-skills/improve-codebase-architecture/LANGUAGE.md deleted file mode 100644 index 530c2763..00000000 --- a/scripts/workstation/claude-skills/improve-codebase-architecture/LANGUAGE.md +++ /dev/null @@ -1,53 +0,0 @@ -# Language - -Shared vocabulary for every suggestion this skill makes. Use these terms exactly — don't substitute "component," "service," "API," or "boundary." Consistent language is the whole point. - -## Terms - -**Module** -Anything with an interface and an implementation. Deliberately scale-agnostic — applies equally to a function, class, package, or tier-spanning slice. -_Avoid_: unit, component, service. - -**Interface** -Everything a caller must know to use the module correctly. Includes the type signature, but also invariants, ordering constraints, error modes, required configuration, and performance characteristics. -_Avoid_: API, signature (too narrow — those refer only to the type-level surface). - -**Implementation** -What's inside a module — its body of code. Distinct from **Adapter**: a thing can be a small adapter with a large implementation (a Postgres repo) or a large adapter with a small implementation (an in-memory fake). Reach for "adapter" when the seam is the topic; "implementation" otherwise. - -**Depth** -Leverage at the interface — the amount of behaviour a caller (or test) can exercise per unit of interface they have to learn. A module is **deep** when a large amount of behaviour sits behind a small interface. A module is **shallow** when the interface is nearly as complex as the implementation. - -**Seam** _(from Michael Feathers)_ -A place where you can alter behaviour without editing in that place. The *location* at which a module's interface lives. Choosing where to put the seam is its own design decision, distinct from what goes behind it. -_Avoid_: boundary (overloaded with DDD's bounded context). - -**Adapter** -A concrete thing that satisfies an interface at a seam. Describes *role* (what slot it fills), not substance (what's inside). - -**Leverage** -What callers get from depth. More capability per unit of interface they have to learn. One implementation pays back across N call sites and M tests. - -**Locality** -What maintainers get from depth. Change, bugs, knowledge, and verification concentrate at one place rather than spreading across callers. Fix once, fixed everywhere. - -## Principles - -- **Depth is a property of the interface, not the implementation.** A deep module can be internally composed of small, mockable, swappable parts — they just aren't part of the interface. A module can have **internal seams** (private to its implementation, used by its own tests) as well as the **external seam** at its interface. -- **The deletion test.** Imagine deleting the module. If complexity vanishes, the module wasn't hiding anything (it was a pass-through). If complexity reappears across N callers, the module was earning its keep. -- **The interface is the test surface.** Callers and tests cross the same seam. If you want to test *past* the interface, the module is probably the wrong shape. -- **One adapter means a hypothetical seam. Two adapters means a real one.** Don't introduce a seam unless something actually varies across it. - -## Relationships - -- A **Module** has exactly one **Interface** (the surface it presents to callers and tests). -- **Depth** is a property of a **Module**, measured against its **Interface**. -- A **Seam** is where a **Module**'s **Interface** lives. -- An **Adapter** sits at a **Seam** and satisfies the **Interface**. -- **Depth** produces **Leverage** for callers and **Locality** for maintainers. - -## Rejected framings - -- **Depth as ratio of implementation-lines to interface-lines** (Ousterhout): rewards padding the implementation. We use depth-as-leverage instead. -- **"Interface" as the TypeScript `interface` keyword or a class's public methods**: too narrow — interface here includes every fact a caller must know. -- **"Boundary"**: overloaded with DDD's bounded context. Say **seam** or **interface**. diff --git a/scripts/workstation/claude-skills/improve-codebase-architecture/SKILL.md b/scripts/workstation/claude-skills/improve-codebase-architecture/SKILL.md deleted file mode 100644 index c12b263b..00000000 --- a/scripts/workstation/claude-skills/improve-codebase-architecture/SKILL.md +++ /dev/null @@ -1,81 +0,0 @@ ---- -name: improve-codebase-architecture -description: Find deepening opportunities in a codebase, informed by the domain language in CONTEXT.md and the decisions in docs/adr/. Use when the user wants to improve architecture, find refactoring opportunities, consolidate tightly-coupled modules, or make a codebase more testable and AI-navigable. ---- - -# Improve Codebase Architecture - -Surface architectural friction and propose **deepening opportunities** — refactors that turn shallow modules into deep ones. The aim is testability and AI-navigability. - -## Glossary - -Use these terms exactly in every suggestion. Consistent language is the point — don't drift into "component," "service," "API," or "boundary." Full definitions in [LANGUAGE.md](LANGUAGE.md). - -- **Module** — anything with an interface and an implementation (function, class, package, slice). -- **Interface** — everything a caller must know to use the module: types, invariants, error modes, ordering, config. Not just the type signature. -- **Implementation** — the code inside. -- **Depth** — leverage at the interface: a lot of behaviour behind a small interface. **Deep** = high leverage. **Shallow** = interface nearly as complex as the implementation. -- **Seam** — where an interface lives; a place behaviour can be altered without editing in place. (Use this, not "boundary.") -- **Adapter** — a concrete thing satisfying an interface at a seam. -- **Leverage** — what callers get from depth. -- **Locality** — what maintainers get from depth: change, bugs, knowledge concentrated in one place. - -Key principles (see [LANGUAGE.md](LANGUAGE.md) for the full list): - -- **Deletion test**: imagine deleting the module. If complexity vanishes, it was a pass-through. If complexity reappears across N callers, it was earning its keep. -- **The interface is the test surface.** -- **One adapter = hypothetical seam. Two adapters = real seam.** - -This skill is _informed_ by the project's domain model. The domain language gives names to good seams; ADRs record decisions the skill should not re-litigate. - -## Process - -### 1. Explore - -Read the project's domain glossary and any ADRs in the area you're touching first. - -Then use the Agent tool with `subagent_type=Explore` to walk the codebase. Don't follow rigid heuristics — explore organically and note where you experience friction: - -- Where does understanding one concept require bouncing between many small modules? -- Where are modules **shallow** — interface nearly as complex as the implementation? -- Where have pure functions been extracted just for testability, but the real bugs hide in how they're called (no **locality**)? -- Where do tightly-coupled modules leak across their seams? -- Which parts of the codebase are untested, or hard to test through their current interface? - -Apply the **deletion test** to anything you suspect is shallow: would deleting it concentrate complexity, or just move it? A "yes, concentrates" is the signal you want. - -### 2. Present candidates as an HTML report - -Write a self-contained HTML file to the OS temp directory so nothing lands in the repo. Resolve the temp dir from `$TMPDIR`, falling back to `/tmp` (or `%TEMP%` on Windows), and write to `/architecture-review-.html` so each run gets a fresh file. Open it for the user — `xdg-open ` on Linux, `open ` on macOS, `start ` on Windows — and tell them the absolute path. - -The report uses **Tailwind via CDN** for layout and styling, and **Mermaid via CDN** for diagrams where a graph/flow/sequence reliably communicates the structure. Mix Mermaid with hand-crafted CSS/SVG visuals — use Mermaid when relationships are graph-shaped (call graphs, dependencies, sequences), and hand-built divs/SVG when you want something more editorial (mass diagrams, cross-sections, collapse animations). Each candidate gets a **before/after visualisation**. Be visual. - -For each candidate, the same template as before, but rendered as a card: - -- **Files** — which files/modules are involved -- **Problem** — why the current architecture is causing friction -- **Solution** — plain English description of what would change -- **Benefits** — explained in terms of locality and leverage, and how tests would improve -- **Before / After diagram** — side-by-side, custom-drawn, illustrating the shallowness and the deepening -- **Recommendation strength** — one of `Strong`, `Worth exploring`, `Speculative`, rendered as a badge - -End the report with a **Top recommendation** section: which candidate you'd tackle first and why. - -**Use CONTEXT.md vocabulary for the domain, and [LANGUAGE.md](LANGUAGE.md) vocabulary for the architecture.** If `CONTEXT.md` defines "Order," talk about "the Order intake module" — not "the FooBarHandler," and not "the Order service." - -**ADR conflicts**: if a candidate contradicts an existing ADR, only surface it when the friction is real enough to warrant revisiting the ADR. Mark it clearly in the card (e.g. a warning callout: _"contradicts ADR-0007 — but worth reopening because…"_). Don't list every theoretical refactor an ADR forbids. - -See [HTML-REPORT.md](HTML-REPORT.md) for the full HTML scaffold, diagram patterns, and styling guidance. - -Do NOT propose interfaces yet. After the file is written, ask the user: "Which of these would you like to explore?" - -### 3. Grilling loop - -Once the user picks a candidate, drop into a grilling conversation. Walk the design tree with them — constraints, dependencies, the shape of the deepened module, what sits behind the seam, what tests survive. - -Side effects happen inline as decisions crystallize: - -- **Naming a deepened module after a concept not in `CONTEXT.md`?** Add the term to `CONTEXT.md` — same discipline as `/grill-with-docs` (see [CONTEXT-FORMAT.md](../grill-with-docs/CONTEXT-FORMAT.md)). Create the file lazily if it doesn't exist. -- **Sharpening a fuzzy term during the conversation?** Update `CONTEXT.md` right there. -- **User rejects the candidate with a load-bearing reason?** Offer an ADR, framed as: _"Want me to record this as an ADR so future architecture reviews don't re-suggest it?"_ Only offer when the reason would actually be needed by a future explorer to avoid re-suggesting the same thing — skip ephemeral reasons ("not worth it right now") and self-evident ones. See [ADR-FORMAT.md](../grill-with-docs/ADR-FORMAT.md). -- **Want to explore alternative interfaces for the deepened module?** See [INTERFACE-DESIGN.md](INTERFACE-DESIGN.md). diff --git a/scripts/workstation/claude-skills/prototype/LOGIC.md b/scripts/workstation/claude-skills/prototype/LOGIC.md deleted file mode 100644 index 526ecb18..00000000 --- a/scripts/workstation/claude-skills/prototype/LOGIC.md +++ /dev/null @@ -1,79 +0,0 @@ -# Logic Prototype - -A tiny interactive terminal app that lets the user drive a state model by hand. Use this when the question is about **business logic, state transitions, or data shape** — the kind of thing that looks reasonable on paper but only feels wrong once you push it through real cases. - -## When this is the right shape - -- "I'm not sure if this state machine handles the edge case where X then Y." -- "Does this data model actually let me represent the case where..." -- "I want to feel out what the API should look like before writing it." -- Anything where the user wants to **press buttons and watch state change**. - -If the question is "what should this look like" — wrong branch. Use [UI.md](UI.md). - -## Process - -### 1. State the question - -Before writing code, write down what state model and what question you're prototyping. One paragraph, in the prototype's README or a comment at the top of the file. A logic prototype that answers the wrong question is pure waste — make the question explicit so it can be checked later, whether the user is watching now or returning to it AFK. - -### 2. Pick the language - -Use whatever the host project uses. If the project has no obvious runtime (e.g. a docs repo), ask. - -Match the project's existing conventions for tooling — don't add a new package manager or runtime just for the prototype. - -### 3. Isolate the logic in a portable module - -Put the actual logic — the bit that's answering the question — behind a small, pure interface that could be lifted out and dropped into the real codebase later. The TUI around it is throwaway; the logic module shouldn't be. - -The right shape depends on the question: - -- **A pure reducer** — `(state, action) => state`. Good when actions are discrete events and state is a single value. -- **A state machine** — explicit states and transitions. Good when "which actions are even legal right now" is part of the question. -- **A small set of pure functions** over a plain data type. Good when there's no implicit current state — just transformations. -- **A class or module with a clear method surface** when the logic genuinely owns ongoing internal state. - -Pick whichever shape best fits the question being asked, *not* whichever is easiest to wire to a TUI. Keep it pure: no I/O, no terminal code, no `console.log` for control flow. The TUI imports it and calls into it; nothing flows the other direction. - -This is what makes the prototype useful past its own lifetime. When the question's been answered, the validated reducer / machine / function set can be lifted into the real module — the TUI shell gets deleted. - -### 4. Build the smallest TUI that exposes the state - -Build it as a **lightweight TUI** — on every tick, clear the screen (`console.clear()` / `print("\033[2J\033[H")` / equivalent) and re-render the whole frame. The user should always see one stable view, not an ever-growing scrollback. - -Each frame has two parts, in this order: - -1. **Current state**, pretty-printed and diff-friendly (one field per line, or formatted JSON). Use **bold** for field names or section headers and **dim** for less important context (timestamps, IDs, derived values). Native ANSI escape codes are fine — `\x1b[1m` bold, `\x1b[2m` dim, `\x1b[0m` reset. No need to pull in a styling library unless one is already in the project. -2. **Keyboard shortcuts**, listed at the bottom: `[a] add user [d] delete user [t] tick clock [q] quit`. Bold the key, dim the description, or vice-versa — whatever reads cleanly. - -Behaviour: - -1. **Initialise state** — a single in-memory object/struct. Render the first frame on start. -2. **Read one keystroke (or one line)** at a time, dispatch to a handler that mutates state. -3. **Re-render** the full frame after every action — don't append, replace. -4. **Loop until quit.** - -The whole frame should fit on one screen. - -### 5. Make it runnable in one command - -Add a script to the project's existing task runner (`package.json` scripts, `Makefile`, `justfile`, `pyproject.toml`). The user should run `pnpm run ` or equivalent — never need to remember a path. - -If the host project has no task runner, just put the command at the top of the prototype's README. - -### 6. Hand it over - -Give the user the run command. They'll drive it themselves; the interesting moments are when they say "wait, that shouldn't be possible" or "huh, I assumed X would be different" — those are the bugs in the _idea_, which is the whole point. If they want new actions added, add them. Prototypes evolve. - -### 7. Capture the answer - -When the prototype has done its job, the answer to the question is the only thing worth keeping. If the user is around, ask what it taught them. If not, leave a `NOTES.md` next to the prototype so the answer can be filled in (or filled in by you, if you've watched the session) before the prototype gets deleted. - -## Anti-patterns - -- **Don't add tests.** A prototype that needs tests is no longer a prototype. -- **Don't wire it to the real database.** Use an in-memory store unless the question is specifically about persistence. -- **Don't generalise.** No "what if we wanted to support X later." The prototype answers one question. -- **Don't blur the logic and the TUI together.** If the reducer / state machine references `console.log`, prompts, or terminal escape codes, it's no longer portable. Keep the TUI as a thin shell over a pure module. -- **Don't ship the TUI shell into production.** The shell is optimised for being driven by hand from a terminal. The logic module behind it is the bit worth keeping. diff --git a/scripts/workstation/claude-skills/prototype/SKILL.md b/scripts/workstation/claude-skills/prototype/SKILL.md deleted file mode 100644 index 64f3e611..00000000 --- a/scripts/workstation/claude-skills/prototype/SKILL.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -name: prototype -description: Build a throwaway prototype to flesh out a design before committing to it. Routes between two branches — a runnable terminal app for state/business-logic questions, or several radically different UI variations toggleable from one route. Use when the user wants to prototype, sanity-check a data model or state machine, mock up a UI, explore design options, or says "prototype this", "let me play with it", "try a few designs". ---- - -# Prototype - -A prototype is **throwaway code that answers a question**. The question decides the shape. - -## Pick a branch - -Identify which question is being answered — from the user's prompt, the surrounding code, or by asking if the user is around: - -- **"Does this logic / state model feel right?"** → [LOGIC.md](LOGIC.md). Build a tiny interactive terminal app that pushes the state machine through cases that are hard to reason about on paper. -- **"What should this look like?"** → [UI.md](UI.md). Generate several radically different UI variations on a single route, switchable via a URL search param and a floating bottom bar. - -The two branches produce very different artifacts — getting this wrong wastes the whole prototype. If the question is genuinely ambiguous and the user isn't reachable, default to whichever branch better matches the surrounding code (a backend module → logic; a page or component → UI) and state the assumption at the top of the prototype. - -## Rules that apply to both - -1. **Throwaway from day one, and clearly marked as such.** Locate the prototype code close to where it will actually be used (next to the module or page it's prototyping for) so context is obvious — but name it so a casual reader can see it's a prototype, not production. For throwaway UI routes, obey whatever routing convention the project already uses; don't invent a new top-level structure. -2. **One command to run.** Whatever the project's existing task runner supports — `pnpm `, `python `, `bun `, etc. The user must be able to start it without thinking. -3. **No persistence by default.** State lives in memory. Persistence is the thing the prototype is _checking_, not something it should depend on. If the question explicitly involves a database, hit a scratch DB or a local file with a clear "PROTOTYPE — wipe me" name. -4. **Skip the polish.** No tests, no error handling beyond what makes the prototype _runnable_, no abstractions. The point is to learn something fast and then delete it. -5. **Surface the state.** After every action (logic) or on every variant switch (UI), print or render the full relevant state so the user can see what changed. -6. **Delete or absorb when done.** When the prototype has answered its question, either delete it or fold the validated decision into the real code — don't leave it rotting in the repo. - -## When done - -The _answer_ is the only thing worth keeping from a prototype. Capture it somewhere durable (commit message, ADR, issue, or a `NOTES.md` next to the prototype) along with the question it was answering. If the user is around, that capture is a quick conversation; if not, leave the placeholder so they (or you, on the next pass) can fill in the verdict before deleting the prototype. diff --git a/scripts/workstation/claude-skills/prototype/UI.md b/scripts/workstation/claude-skills/prototype/UI.md deleted file mode 100644 index f3b6e640..00000000 --- a/scripts/workstation/claude-skills/prototype/UI.md +++ /dev/null @@ -1,112 +0,0 @@ -# UI Prototype - -Generate **several radically different UI variations** on a single route, switchable from a floating bottom bar. The user flips between variants in the browser, picks one (or steals bits from each), then throws the rest away. - -If the question is about logic/state rather than what something looks like — wrong branch. Use [LOGIC.md](LOGIC.md). - -## When this is the right shape - -- "What should this page look like?" -- "I want to see a few options for this dashboard before committing." -- "Try a different layout for the settings screen." -- Any time the user would otherwise spend a day picking between three vague mockups in their head. - -## Two sub-shapes — strongly prefer sub-shape A - -A UI prototype is much easier to judge when it's **butting up against the rest of the app** — real header, real sidebar, real data, real density. A throwaway route on its own is a vacuum: every variant looks fine in isolation. Default to sub-shape A whenever there's a plausible existing page to host the variants. Only reach for sub-shape B if the prototype genuinely has no nearby home. - -### Sub-shape A — adjustment to an existing page (preferred) - -The route already exists. Variants are rendered **on the same route**, gated by a `?variant=` URL search param. The existing data fetching, params, and auth all stay — only the rendering swaps. This is the default; pick it unless there's a specific reason not to. - -If the prototype is for something that doesn't yet have a page but *would naturally live inside one* (a new section of the dashboard, a new card on the settings screen, a new step in an existing flow) — that's still sub-shape A. Mount the variants inside the host page. - -### Sub-shape B — a new page (last resort) - -Only use this when the thing being prototyped genuinely has no existing page to live inside — e.g. an entirely new top-level surface, or a flow that can't be embedded anywhere sensible. - -Create a **throwaway route** following whatever routing convention the project already uses — don't invent a new top-level structure. Name it so it's obviously a prototype (e.g. include the word `prototype` in the path or filename). Same `?variant=` pattern. - -Before committing to sub-shape B, sanity-check: is there really no existing page this could be embedded in? An empty route hides design problems that a populated one would expose. - -In both sub-shapes the floating bottom bar is identical. - -## Process - -### 1. State the question and pick N - -Default to **3 variants**. More than 5 stops being radically different and starts being noise — cap there. - -Write down the plan in one line, in the prototype's location or a top-of-file comment: - -> "Three variants of the settings page, switchable via `?variant=`, on the existing `/settings` route." - -This works whether the user is here to push back or not. - -### 2. Generate radically different variants - -Draft each variant. Hold each one to: - -- The page's purpose and the data it has access to. -- The project's component library / styling system (TailwindCSS, shadcn, MUI, plain CSS, whatever). -- A clear exported component name, e.g. `VariantA`, `VariantB`, `VariantC`. - -Variants must be **structurally different** — different layout, different information hierarchy, different primary affordance, not just different colours. Three slightly-tweaked card grids isn't a UI prototype, it's wallpaper. If two drafts come out too similar, redo one with explicit "do not use a card grid" guidance. - -### 3. Wire them together - -Create a single switcher component on the route: - -```tsx -// pseudo-code — adapt to the project's framework -const variant = searchParams.get('variant') ?? 'A'; -return ( - <> - {variant === 'A' && } - {variant === 'B' && } - {variant === 'C' && } - - -); -``` - -For sub-shape A (existing page): keep all the existing data fetching above the switcher; only the rendered subtree changes per variant. - -For sub-shape B (new page): the throwaway route under `/prototype/` mounts the same switcher. - -### 4. Build the floating switcher - -A small fixed-position bar at the bottom-centre of the screen with three pieces: - -- **Left arrow** — cycles to the previous variant (wraps around). -- **Variant label** — shows the current variant key and, if the variant exports a name, that name too. e.g. `B — Sidebar layout`. -- **Right arrow** — cycles forward (wraps around). - -Behaviour: - -- Clicking an arrow updates the URL search param (use the framework's router — `router.replace` on Next, `navigate` on React Router, etc) so the variant is shareable and reload-stable. -- Keyboard: `←` and `→` arrow keys also cycle. Don't intercept arrow keys when an ``, `