From c3b51b3546fb7de08e284530b5b3d176427b843e Mon Sep 17 00:00:00 2001 From: m3tam3re Date: Sun, 25 Oct 2026 12:47:09 +0100 Subject: [PATCH] feat(coding): add ai-memory client module, drop Pi and babysitter coding.aiMemory wires Claude Code and OpenCode2 to the central ai-memory server: Nix owns the client config (server URL + bearer token file), the OpenCode MCP entry, and the scope markers (home workspace plus one marker per workspaceDirs entry); activation runs ai-memory's own installers for hooks, the Claude MCP bridge and the skills. The token never lands in a generated file: hooks and the bridge read [auth].bearer_token_file, the OpenCode plugin reads the data-dir auth-token symlink. Pi (and its babysitter profile) are no longer used; LifeOS is being removed from the hosts as well. Co-Authored-By: Claude Fable 5.1 --- .../plans/2026-10-24-ai-memory-rollout.md | 258 ++++++++++++++++++ profiles/sets/coding/agents/agents.nix | 11 +- profiles/sets/coding/agents/ai-memory.nix | 142 ++++++++++ profiles/sets/coding/agents/ai-memory.test.sh | 61 +++++ profiles/sets/coding/agents/babysitter.nix | 142 ---------- profiles/sets/coding/agents/pi.nix | 239 ---------------- 6 files changed, 464 insertions(+), 389 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-24-ai-memory-rollout.md create mode 100644 profiles/sets/coding/agents/ai-memory.nix create mode 100755 profiles/sets/coding/agents/ai-memory.test.sh delete mode 100644 profiles/sets/coding/agents/babysitter.nix delete mode 100644 profiles/sets/coding/agents/pi.nix diff --git a/docs/superpowers/plans/2026-10-24-ai-memory-rollout.md b/docs/superpowers/plans/2026-10-24-ai-memory-rollout.md new file mode 100644 index 0000000..13f60f3 --- /dev/null +++ b/docs/superpowers/plans/2026-10-24-ai-memory-rollout.md @@ -0,0 +1,258 @@ +# ai-memory Rollout Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Ein zentraler, selbst gehosteter ai-memory-Server (aimemory.sh) auf m3-hermes, an den Claude Code + OpenCode2 auf m3-kratos und AZ-LT-NIX sowie Hermes auf m3-hermes deklarativ angebunden sind. + +**Architecture:** Server via Upstream-NixOS-Modul `services.ai-memory` in `nixos-config` (m3-hermes), öffentlich über Traefik auf m3-atlas → Netbird. Clients über ein neues Home-Manager-Modul `coding.aiMemory` in `m3ta-home` (Hybrid: Nix verwaltet Paket, Env, Marker und OpenCode-MCP; eine Activation ruft `ai-memory install-hooks`/`install-mcp`/`install-skills` auf). Host-Repos liefern pro Host ein agenix-Token und schalten das Modul ein. + +**Tech Stack:** NixOS, Home-Manager, agenix, Traefik, Netbird, ai-memory 3.2.x (`github:akitaonrails/ai-memory`), hermes-agent NixOS-Modul. + +**Spec:** Die Entscheidungen aus der Grilling-Session vom 2026-10-24 stehen unten unter „Entscheidungen“. Upstream-Doku: geklont unter `github.com/akitaonrails/ai-memory` → `README.md#nixos`, `nix/nixos-module.nix`, `docs/install.md` (Auth ab Z. ~3590), `docs/marker-file.md`, `docs/support-matrix.md` (Hermes-Zeile), `docs/llm-providers.md`. + +## Entscheidungen (Spec) + +- Es gibt **einen** Server auf m3-hermes für privat **und** Arbeit. Getrennt wird nur über Projekte in ai-memory. +- Upstream-Modul `services.ai-memory` als System-Service unter `/var/lib/ai-memory`. Auf Clients läuft **kein** Server. +- Alle Clients nutzen `https://memory.m3ta.dev` (MCP, Hooks, Web-UI). Hermes ist auf demselben Host und nutzt `http://127.0.0.1:`. +- Ein Bearer-Token pro Host (kratos, AZ-LT-NIX, hermes) als `aim_`-API-Key, im Host-Repo mit agenix verschlüsselt. Der Admin-Login für das Web-UI läuft über das Root-Passwort (einmalig per Env). +- LLM-Konsolidierung über `openai-compat` mit dem privaten z.ai/GLM-Key in einem **eigenen** Secret, nicht in `hermes-env`. +- Client = Hybrid in m3ta-home: Skills ja, **keine** `install-instructions`-Snippets in CLAUDE.md/AGENTS.md. +- Capture-Ausschlüsse: `/run/agenix/**`, `~/.config/sops/**`, `~/.ssh/**`, `**/.env*`. +- Hermes: Hooks + MCP + Skills deklarativ. **Honcho bleibt** Memory-Provider. Das Community-Plugin wird nicht genutzt. +- Agents: kratos + AZ-LT-NIX = Claude Code + OpenCode2. m3-hermes = nur Hermes. +- Imperatives Test-Setup auf kratos (User-Unit, `~/.local/share/ai-memory`, MCP-Eintrag auf Port 49374) wird **ohne Backup** entfernt. +- Rollout: (1) Server + Traefik → (2) kratos → (3) Hermes → (4) AZ-LT-NIX. Nach jeder Stufe `ai-memory doctor`. +- **Nicht in diesem Plan:** Transkript-Backfill (später manuell), `ai-memory-wikisync`-Backup (Phase 2). + +## Global Constraints + +- Server-Port: `49374` (Upstream-Default), registriert als `ai-memory` in `hosts/common/ports.nix` (nixos-config). +- Öffentliche URL: `https://memory.m3ta.dev`. Die MCP-URL ist `https://memory.m3ta.dev/mcp`. +- Netbird-Range für die Firewall: `100.64.0.0/16`. m3-hermes hat die Netbird-IP `100.81.231.152` (wie beim hermes-dashboard-Service in `hosts/m3-atlas/services/traefik.nix`). +- Server-Secrets landen nur in `services.ai-memory.ageSecret`, **nie** in `settings` (das liegt sonst world-readable im Nix-Store). +- Client-Token-Pfad (Default): `/run/agenix/ai-memory-token`. Er muss für den jeweiligen User lesbar sein (`owner = "m3tam3re"` bzw. `"sascha.koenig"`). +- Im Client-Modul gibt es keine fest verdrahteten `/home/m3tam3re`-Pfade, nur `config.home.homeDirectory`. +- Server und Clients müssen dieselbe Major/Minor-Version von ai-memory haben. Clients nutzen `pkgs.llm-agents.ai-memory`, der Server das Flake-Input. Bei Abweichung wird der Server per `services.ai-memory.package` auf das llm-agents-Paket umgestellt. +- `git` muss neben dem Client-Binary im PATH liegen (die Hooks rufen es auf). + +## Review Focus + +1. **Token-Datei fehlt oder ist nicht lesbar** (z.B. Host ohne Secret, erster Switch vor agenix): Die Activation darf nicht fehlschlagen. Sie überspringt mit einer klaren Warnung und schreibt keine Hooks mit leerem Token. Den Test dafür gibt es in Task 4, Step 1. +2. **Home-Manager überschreibt Dateien, die die Activation geschrieben hat:** `~/.config/opencode/opencode.json` ist ein HM-Symlink. `install-mcp --client opencode2` darf dort **nicht** schreiben, der MCP-Eintrag läuft über `programs.mcp`. Den Test dafür gibt es in Task 4, Step 6. +3. **Server nicht erreichbar** (AZ-LT-NIX offline, m3-hermes down): Die Hooks puffern lokal, und die Agents starten trotzdem. Das prüft Task 5, Step 6. +4. **Port/URL-Drift**, wie im Test, bei dem MCP auf 49374 und die Hooks auf 49375 zeigten: Hooks, MCP und `AI_MEMORY_SERVER_URL` müssen aus derselben Option `serverUrl` kommen. Das prüft `ai-memory doctor` in Task 5, Step 5. +5. **Secrets im Capture:** Ein Agent liest `/run/agenix/*` oder `.env`, und die Inhalte landen im Wiki. Den Test über den Home-Marker gibt es in Task 5, Step 7. + +--- + +## Phase 1: Server (Repo `nixos-config`) + +### Task 1: Secrets und Flake-Input für den Server + +**Files:** +- Modify: `nixos-config/flake.nix` (Input `ai-memory`, Modul in die m3-hermes-Module) +- Modify: `nixos-config/secrets.nix` (neue `.age`-Einträge) +- Create: `nixos-config/secrets/ai-memory-server-env.age` +- Modify: `nixos-config/hosts/m3-hermes/secrets.nix` +- Modify: `nixos-config/hosts/common/ports.nix` (`ai-memory = 49374;`) + +**Interfaces:** +- Produces: `config.age.secrets."ai-memory-server-env"` auf m3-hermes. `inputs.ai-memory` steht in `specialArgs` zur Verfügung. `config.m3ta.ports.get "ai-memory"` liefert `49374`. + +- [ ] **Step 1: Flake-Input ergänzen**: `ai-memory.url = "github:akitaonrails/ai-memory";` mit `inputs.nixpkgs.follows = "nixpkgs"`. `ai-memory.nixosModules.default` kommt **nur** in die Modulliste von m3-hermes. +- [ ] **Step 2: Secret-Datei anlegen** (`agenix -e secrets/ai-memory-server-env.age`, publicKeys = `[m3-hermes] ++ users`) mit: + ```env + AI_MEMORY_AUTH_TOKEN= # Root-Bearer (Admin-CLI, nie an Clients) + AI_MEMORY_AUTH__TOKEN_PEPPER= # nötig für aim_-API-Keys + AI_MEMORY_AUTH__RECOVERY_TOKEN= + AI_MEMORY_AUTH__INITIAL_ROOT_PASSWORD=<≥12 Zeichen, nur beim ersten Start genutzt> + AI_MEMORY_AUTH__SECURE_COOKIE=true + AI_MEMORY_AUTH__TRUSTED_PROXY_CIDRS=100.64.0.0/16 + AI_MEMORY_LLM_PROVIDER=openai-compat + AI_MEMORY_LLM_BASE_URL=https://api.z.ai/api/coding/paas/v4 + AI_MEMORY_LLM_MODEL=glm-5.3-flash + LLM_API_KEY= + ``` + Ob die Env-Namen `AI_MEMORY_AUTH__TOKEN_PEPPER` bzw. `__BEARER_TOKEN` stimmen, wird gegen `nix/nixos-module.nix` (`secretKeysInSettings`) und `docs/install.md` geprüft, bevor das Secret verschlüsselt wird. +- [ ] **Step 3: Eval prüfen** + Run: `nix eval .#nixosConfigurations.m3-hermes.config.age.secrets.ai-memory-server-env.path` + Expected: `"/run/agenix/ai-memory-server-env"` +- [ ] **Step 4: Commit** `feat(m3-hermes): add ai-memory input, ports and server secret` + +### Task 2: ai-memory-Server auf m3-hermes + +**Files:** +- Create: `nixos-config/hosts/m3-hermes/services/ai-memory.nix` +- Modify: `nixos-config/hosts/m3-hermes/services/default.nix` (Import) + +**Interfaces:** +- Consumes: Task 1 (Secret, Port, Modul) +- Produces: Server lauscht auf `0.0.0.0:49374`. Die Firewall lässt den Port nur aus `100.64.0.0/16` durch, Loopback ist für Hermes offen. + +- [ ] **Step 1: Modul schreiben** im Stil von `hermes-dashboard.nix` (`let`-Block für `netbirdRange`/`port`): + - `services.ai-memory = { enable = true; bind = "0.0.0.0"; port = config.m3ta.ports.get "ai-memory"; enableWeb = true; ageSecret = "ai-memory-server-env"; openFirewall = false; settings.allowed_hosts = ["memory.m3ta.dev" "localhost" "127.0.0.1" "::1"]; }` + - `networking.firewall.extraCommands`: `ip46tables -A nixos-fw -p tcp --dport -s 100.64.0.0/16 -j nixos-fw-accept` +- [ ] **Step 2: Bauen** + Run: `nix build .#nixosConfigurations.m3-hermes.config.system.build.toplevel` + Expected: Build ok, ohne Assertion zu Secret-Quellen bei Non-Loopback-Bind. +- [ ] **Step 3: Deployen und Health prüfen** (auf m3-hermes) + Run: `systemctl is-active ai-memory && curl -s http://127.0.0.1:49374/healthz` + Expected: `active` und `{"status":"ok"}` +- [ ] **Step 4: Root-Passwort ändern und Bootstrap-Secret entfernen**: Einmal einloggen (das erzwingt den Passwortwechsel). Danach `AI_MEMORY_AUTH__INITIAL_ROOT_PASSWORD` aus dem Secret löschen, neu verschlüsseln und deployen. +- [ ] **Step 5: Commit** `feat(m3-hermes): run ai-memory server` + +### Task 3: Traefik-Route `memory.m3ta.dev` und Host-Tokens + +**Files:** +- Create: `nixos-config/hosts/m3-atlas/services/ai-memory.nix` (Muster: `vaultwarden.nix`, also `services.traefik.dynamicConfigOptions.http.{services,routers}.ai-memory`) +- Modify: `nixos-config/hosts/m3-atlas/services/default.nix` + +**Interfaces:** +- Consumes: Task 2 (Server auf `100.81.231.152:49374`) +- Produces: `https://memory.m3ta.dev` sowie drei `aim_`-Keys (`kratos`, `az-lt-nix`, `hermes`), die vorerst nur im Passwortmanager liegen und in Task 5/6/7 in agenix kommen. + +- [ ] **Step 1: Route anlegen**: `services.ai-memory.loadBalancer.servers = [{url = "http://100.81.231.152:49374";}]` und `routers.ai-memory = { rule = "Host(\`memory.m3ta.dev\`)"; tls.certResolver = "godaddy"; service = "ai-memory"; entrypoints = "websecure"; }`. Der DNS-Eintrag `memory.m3ta.dev` → m3-atlas wird gesetzt. +- [ ] **Step 2: Von außen prüfen** (von einem Gerät außerhalb von Netbird) + Run: `curl -s https://memory.m3ta.dev/healthz; curl -s -o /dev/null -w '%{http_code}\n' https://memory.m3ta.dev/mcp` + Expected: `{"status":"ok"}` und `401` (ohne Token kein Zugriff) +- [ ] **Step 3: Host-Keys erzeugen** (auf m3-hermes, als Service-User mit Root-Bearer): + Run: `for h in kratos az-lt-nix hermes; do sudo -u ai-memory ai-memory --data-dir /var/lib/ai-memory api-key add --username root --label $h; done` + Expected: drei `aim_…`-Tokens, einmalig angezeigt und sofort sicher abgelegt. +- [ ] **Step 4: Web-UI prüfen**: `https://memory.m3ta.dev/web` → Login als Root funktioniert, und das Cookie trägt das `Secure`-Flag. +- [ ] **Step 5: Commit** `feat(m3-atlas): route memory.m3ta.dev to ai-memory` + +--- + +## Phase 2: Client-Modul (Repo `m3ta-home`) + +### Task 4: Home-Manager-Modul `coding.aiMemory` + +**Files:** +- Create: `m3ta-home/profiles/sets/coding/agents/ai-memory.nix` +- Modify: `m3ta-home/profiles/sets/coding/agents/agents.nix` (Import ergänzen, `llm-agents.ai-memory` aus `home.packages` entfernen; das Paket kommt jetzt aus dem Modul) +- Test: `m3ta-home/profiles/sets/coding/agents/ai-memory.test.sh` (Eval-Checks gegen eine Test-HM-Konfiguration bzw. eine bestehende `homeConfigurations`-Ausgabe) + +**Interfaces:** +- Produces (Optionen, die die Host-Repos setzen): + - `coding.aiMemory.enable : bool` (default `false`) + - `coding.aiMemory.serverUrl : str` (default `"https://memory.m3ta.dev"`) + - `coding.aiMemory.tokenFile : str` (default `"/run/agenix/ai-memory-token"`) + - `coding.aiMemory.agents : listOf (enum ["claude-code" "opencode2"])` (default `["claude-code" "opencode2"]`) + - `coding.aiMemory.ignorePaths : listOf str` (default `["/run/agenix/**" "~/.config/sops/**" "~/.ssh/**" "**/.env*"]`) + +- [ ] **Step 1: Failing Eval-Test schreiben** (`ai-memory.test.sh`, nutzt `nix eval` gegen eine Konfiguration mit `coding.aiMemory.enable = true`). Er prüft: + - `home.sessionVariables.AI_MEMORY_SERVER_URL == "https://memory.m3ta.dev"` + - `programs.mcp.servers.ai-memory.command == "bash"`, und `args` enthält `mcp-bridge --no-session-id --server-url https://memory.m3ta.dev/mcp` sowie `$(cat /run/agenix/ai-memory-token)` + - `home.file.".ai-memory.toml".text` enthält `[capture]` und alle vier `ignorePaths` + - `home.activation ? aiMemoryInstall` + - Activation-Skript: Bei fehlender Token-Datei steht `ai-memory: token file … missing, skipping` auf stderr und der Exit-Code ist 0. Getestet wird mit dem extrahierten Skript, `tokenFile` zeigt auf einen nicht existierenden Pfad, und `ai-memory` ist per PATH-Stub ersetzt. +- [ ] **Step 2: Test laufen lassen** + Run: `bash profiles/sets/coding/agents/ai-memory.test.sh` + Expected: FAIL (Option `coding.aiMemory` existiert nicht) +- [ ] **Step 3: Modul implementieren** (Stil wie `packages.nix`: `cfg = config.coding.aiMemory`, `mkEnableOption`, `mkIf`): + - `home.packages = [pkgs.llm-agents.ai-memory pkgs.git]` + - `home.sessionVariables.AI_MEMORY_SERVER_URL = cfg.serverUrl` + - `programs.mcp.servers.ai-memory` nach dem Muster des `exa`-Eintrags: `bash -c "AI_MEMORY_AUTH_TOKEN=$(cat ) exec ai-memory mcp-bridge --no-session-id --server-url /mcp"`. OpenCode2 übernimmt das über `enableMcpIntegration`. + - `home.file.".ai-memory.toml".text` mit `[capture]` und `ignore_paths = [...]` (der Home-Marker gilt für alles unterhalb von `$HOME` ohne eigenen Marker) + - `home.activation.aiMemoryInstall = lib.hm.dag.entryAfter ["writeBoundary" "claudeSkillsSync"]`. Das Skript macht Folgendes: + 1. Ist `tokenFile` nicht lesbar: Warnung auf stderr und `return 0`. + 2. `tok=$(cat tokenFile)` + 3. Für jeden Agent in `cfg.agents`: `$DRY_RUN_CMD ai-memory install-hooks --agent --apply --server-url --auth-token "$tok"` + 4. Falls `claude-code` in `agents` steht: `claude mcp remove ai-memory -s user || true` und danach `ai-memory install-mcp --client claude-code --apply --server-url /mcp --auth-token "$tok"` (session-aware Bridge) + 5. `ai-memory install-skills --scope global --apply`. Die Skills landen in `~/.agents/skills`, und `claude-skills-sync` spiegelt sie. + - **Kein** `install-mcp --client opencode2` (sonst Konflikt mit dem HM-Symlink `opencode.json`), **kein** `install-instructions`. +- [ ] **Step 4: Test laufen lassen** + Run: `bash profiles/sets/coding/agents/ai-memory.test.sh` + Expected: PASS +- [ ] **Step 5: Ob die Flags stimmen, gegen die installierte Version prüfen** + Run: `ai-memory install-skills --help; ai-memory install-mcp --help | grep -A3 auth-token` + Expected: `--scope global`, `--apply` und `--auth-token` gibt es. Sonst wird Step 3 an die echten Flags angepasst. +- [ ] **Step 6: HM-Konflikt-Check** (Review Focus #2) + Run: `nix eval` auf `xdg.configFile."opencode/opencode.json".text` der Test-Konfiguration und dort nach `ai-memory` greppen + Expected: Eintrag vorhanden (kommt aus `programs.mcp`), und die Activation enthält **kein** `install-mcp --client opencode`. +- [ ] **Step 7: Commit** `feat(coding): add ai-memory client module` + +--- + +## Phase 3: Rollout + +### Task 5: m3-kratos anbinden (Repo `nixos-config`) + +**Files:** +- Create: `nixos-config/secrets/ai-memory-token-kratos.age` (Inhalt: der `aim_`-Key `kratos` aus Task 3) +- Modify: `nixos-config/secrets.nix`, `nixos-config/hosts/m3-kratos/secrets.nix` (`ai-memory-token = { file = ../../secrets/ai-memory-token-kratos.age; owner = "m3tam3re"; };`) +- Modify: `nixos-config/hosts/common/users/m3tam3re.nix` (in den kratos-`hostFlags` den Eintrag `coding.aiMemory.enable = true;` setzen) +- Modify: `nixos-config/flake.lock` (`nix flake update m3ta-home`) + +**Interfaces:** +- Consumes: Task 3 (Key `kratos`), Task 4 (`coding.aiMemory`) + +- [ ] **Step 1: Imperatives Test-Setup abräumen** (auf kratos, einmalig): + ```bash + systemctl --user disable --now ai-memory.service + rm ~/.config/systemd/user/ai-memory.service && systemctl --user daemon-reload + claude mcp remove ai-memory -s user + rm -rf ~/.local/share/ai-memory ~/.config/opencode/plugins/ai-memory-opencode2.ts + ``` + Die Hook-Einträge in `~/.claude/settings.json` überschreibt `install-hooks --apply` in Step 3. Danach prüfen, dass keine Einträge mit `127.0.0.1:49375` übrig sind. +- [ ] **Step 2: Secret + Flag + Lock**, dann `nix build .#nixosConfigurations.m3-kratos.config.system.build.toplevel`. Expected: Build ok. +- [ ] **Step 3: Switch** + Run: `sudo nixos-rebuild switch --flake .#m3-kratos` + Expected: In der Activation-Ausgabe steht keine `skipping`-Warnung. +- [ ] **Step 4: Kein Drift** + Run: `grep -c 'memory.m3ta.dev' ~/.claude/settings.json; grep -c '4937[45]' ~/.claude/settings.json ~/.claude.json` + Expected: >0 bzw. `0` +- [ ] **Step 5: Doctor** + Run: `cd ~/p/NIX/m3ta-home && ai-memory doctor` + Expected: Server erreichbar, Auth ok. Nach einer kurzen Claude-Code- und einer OpenCode2-Session: beide Harnesses haben Sessions erfasst. +- [ ] **Step 6: Offline-Verhalten** (Review Focus #3): `sudo systemctl stop ai-memory` auf m3-hermes. Danach in Claude Code einen Prompt absetzen. Expected: Die Session startet ohne Fehler, und `~/.local/share/ai-memory/hook-spool` wächst. Nach dem Neustart des Servers sind die Events nachgeliefert (`ai-memory doctor`). +- [ ] **Step 7: Ausschluss-Check** (Review Focus #5): In Claude Code `cat /run/agenix/exa-key` ausführen lassen. Expected: `ai-memory hook --check-capture` bzw. die Session-Seite im Web-UI enthält weder den Befehl noch den Inhalt. +- [ ] **Step 8: Commit** `feat(m3-kratos): enable ai-memory client` + +### Task 6: Hermes anbinden (Repo `nixos-config`) + +**Files:** +- Create: `nixos-config/secrets/ai-memory-hermes-env.age` (Inhalt: `AI_MEMORY_AUTH_TOKEN=`) +- Modify: `nixos-config/hosts/m3-hermes/secrets.nix`, `nixos-config/secrets.nix` +- Modify: `nixos-config/hosts/m3-hermes/services/hermes-agent.nix` + +**Interfaces:** +- Consumes: Task 2 (Server auf Loopback), Task 3 (Key `hermes`) + +- [ ] **Step 1: Snippets erzeugen** (auf m3-hermes): + Run: `ai-memory install-hooks --agent hermes --server-url http://127.0.0.1:49374; ai-memory install-mcp --client hermes --server-url http://127.0.0.1:49374/mcp` + Expected: YAML-Blöcke `hooks:` (sechs Events) und `mcp_servers:`. Diese werden 1:1 als Nix-Attrsets übernommen. +- [ ] **Step 2: Einbauen** in `services.hermes-agent`: + - `environmentFiles` um `config.age.secrets."ai-memory-hermes-env".path` ergänzen + - `extraPackages` um `inputs.ai-memory.packages.${pkgs.stdenv.hostPlatform.system}.default` ergänzen (gleiche Version wie der Server) + - `settings.hooks = `, `settings.hooks_auto_accept = true` + - `settings.mcp_servers.ai-memory = ` (Bridge mit `--no-session-id`, ohne Token in der Datei) + - `settings.memory.provider` bleibt `"honcho"` +- [ ] **Step 3: Skills installieren**: ein `systemd.services.hermes-ai-memory-skills` (oneshot, `User = "hermes"`, `HOME=/var/lib/hermes`, `HERMES_HOME=/var/lib/hermes/.hermes`, `before = ["hermes-agent.service"]`), das `ai-memory install-skills --scope global --agent hermes --apply` ausführt. +- [ ] **Step 4: Bauen + Switch**, dann: + Run: `grep -A2 'ai-memory' /var/lib/hermes/.hermes/config.yaml | head; ls /var/lib/hermes/.hermes/skills | grep ai-memory` + Expected: Hooks und MCP-Eintrag stehen in der Datei, die Skills sind vorhanden. +- [ ] **Step 5: End-to-End-Test** (laut Doku von Upstream noch nicht verifiziert): Hermes über das Gateway eine kleine Aufgabe machen lassen, z.B. ein Repo listen, und danach `/new`. Expected: Im Web-UI erscheint eine Hermes-Session mit Prompt und Tool-Calls. Bei der nächsten Session bekommt Hermes über `pre_llm_call` den Handoff. Läuft das nicht, Abweichungen dokumentieren und im Upstream-Issue-Tracker melden. +- [ ] **Step 6: Commit** `feat(m3-hermes): wire hermes-agent into ai-memory` + +### Task 7: AZ-LT-NIX anbinden (Repo `AZ-NIX-CLIENTS`) + +**Files:** +- Create: `secrets/ai-memory-token.age` (publicKeys `[AZ-LT-NIX sascha.koenig]`, Inhalt: der `aim_`-Key `az-lt-nix`) +- Modify: `secrets.nix`, `hosts/AZ-LT-NIX/secrets.nix` (`ai-memory-token = { file = ../../secrets/ai-memory-token.age; owner = "sascha.koenig"; };`) +- Modify: `hosts/common/users/sascha.koenig.nix` (in den AZ-LT-NIX-`hostFlags` unter `coding` den Eintrag `aiMemory.enable = true;` setzen) +- Modify: `flake.lock` (`nix flake update m3ta-home`) + +**Interfaces:** +- Consumes: Task 3 (Key `az-lt-nix`), Task 4 (`coding.aiMemory`) + +- [ ] **Step 1: Secret + Flag + Lock**, dann `nix build .#nixosConfigurations.AZ-LT-NIX.config.system.build.toplevel`. Expected: Build ok. +- [ ] **Step 2: Switch** auf AZ-LT-NIX, **ohne** dass das private Netbird verbunden ist. +- [ ] **Step 3: Doctor** in einem Arbeits-Repo + Run: `ai-memory doctor` + Expected: `https://memory.m3ta.dev` ist erreichbar, und Claude-Code- sowie OpenCode2-Sessions werden erfasst. Der Projektname im Web-UI ist ein anderer als bei den privaten Repos. +- [ ] **Step 4: Pfade prüfen** + Run: `grep -c '/home/m3tam3re' ~/.claude/settings.json ~/.config/opencode/plugins/ai-memory-opencode2.ts` + Expected: `0` +- [ ] **Step 5: Commit + Push** `feat(AZ-LT-NIX): enable ai-memory client` diff --git a/profiles/sets/coding/agents/agents.nix b/profiles/sets/coding/agents/agents.nix index 2b96edf..ceb85e9 100644 --- a/profiles/sets/coding/agents/agents.nix +++ b/profiles/sets/coding/agents/agents.nix @@ -1,4 +1,4 @@ -# AI agent system — OpenCode, Pi, and MCP server configuration. +# AI agent system — OpenCode, Claude Code, ai-memory and MCP server configuration. # Relies on coding.agents options provided by home/common/default.nix # (m3taHomeInputs.m3ta-nixpkgs.homeManagerModules.default). { @@ -17,15 +17,12 @@ }; imports = [ - # Babysitter agent and OpenCode/Pi tool configurations - ./babysitter.nix + # ai-memory client, skill mirror and OpenCode config + ./ai-memory.nix ./claude-skills.nix ./opencode.nix - ./pi.nix ]; - coding.agents.babysitter.enable = true; - programs.mcp = { enable = true; servers = { @@ -73,14 +70,12 @@ agenix-cli agent-lib llm-agents.agent-browser - llm-agents.ai-memory llm-agents.beads llm-agents.claude-code llm-agents.claude-desktop crunch docker-sbx llm-agents.herdr - llm-agents.pi (llm-agents.qmd.override { vulkanSupport = videoDrivers == ["amdgpu"]; cudaSupport = videoDrivers == ["nvidia"]; diff --git a/profiles/sets/coding/agents/ai-memory.nix b/profiles/sets/coding/agents/ai-memory.nix new file mode 100644 index 0000000..f2c88a5 --- /dev/null +++ b/profiles/sets/coding/agents/ai-memory.nix @@ -0,0 +1,142 @@ +# ai-memory client (https://aimemory.sh) — shared agent memory. +# +# The server runs on m3-hermes (nixos-config) and is reached at +# memory.m3ta.dev; no server runs on clients. Hybrid setup: +# - Nix owns the package, the client config (server URL + bearer token +# file), the OpenCode MCP entry (via programs.mcp, since opencode.json +# is a home-manager symlink) and the scope markers (~/.ai-memory.toml +# plus one marker per workspace directory). +# - Activation runs ai-memory's own installers for the lifecycle hooks, +# the session-aware Claude Code MCP entry and the skills, so generated +# files always match the installed ai-memory version. +# +# The bearer token (an `aim_` API key, one per host) is an agenix secret the +# host repo provides at `tokenFile`. It is never copied into a generated +# file: hooks and the MCP bridge read `[auth].bearer_token_file` from the +# client config, and the OpenCode plugin reads `/auth-token`, +# which activation symlinks to the secret. Without the token activation +# skips the installers instead of writing hooks that cannot authenticate. +{ + config, + lib, + pkgs, + ... +}: +with lib; let + cfg = config.coding.aiMemory; + package = pkgs.llm-agents.ai-memory; + exe = "${package}/bin/ai-memory"; + dataDir = "${config.home.homeDirectory}/.local/share/ai-memory"; + tomlList = xs: "[${concatMapStringsSep ", " (x: ''"${x}"'') xs}]"; + + # Markers are not merged with their ancestors, so every marker repeats + # the capture exclusions. + marker = workspace: '' + workspace = "${workspace}" + + [capture] + ignore_paths = ${tomlList cfg.ignorePaths} + ''; +in { + options.coding.aiMemory = { + enable = mkEnableOption "the ai-memory client (hooks, MCP and skills for coding agents)"; + + serverUrl = mkOption { + type = types.str; + default = "https://memory.m3ta.dev"; + description = "Base URL of the ai-memory server; MCP is served at /mcp."; + }; + + tokenFile = mkOption { + type = types.str; + default = "/run/agenix/ai-memory-token"; + description = "File holding this host's `aim_` API key, readable by the user."; + }; + + agents = mkOption { + type = types.listOf (types.enum ["claude-code" "opencode2"]); + default = ["claude-code" "opencode2"]; + description = "Agents whose lifecycle hooks are installed."; + }; + + workspace = mkOption { + type = types.str; + default = "private"; + description = '' + ai-memory workspace for every directory below $HOME that has no + marker of its own (and no entry in `workspaceDirs`). Projects are + still derived per repository (git remote), only the workspace is + fixed here. Work hosts set this to the work workspace. + ''; + }; + + workspaceDirs = mkOption { + type = types.attrsOf types.str; + default = {}; + example = {"p/AZ" = "az";}; + description = '' + Home-relative directories whose subtree belongs to another + workspace. Each gets its own `.ai-memory.toml` marker. + ''; + }; + + ignorePaths = mkOption { + type = types.listOf types.str; + default = ["/run/agenix/**" "~/.config/sops/**" "~/.ssh/**" "**/.env*"]; + description = "Paths kept out of capture via the markers' [capture] ignore_paths."; + }; + }; + + config = mkIf cfg.enable { + # The llm-agents build wraps the binary with git on PATH (hooks need it). + home.packages = [package]; + + home.sessionVariables.AI_MEMORY_SERVER_URL = cfg.serverUrl; + + # OpenCode2 picks this up via programs.opencode.enableMcpIntegration. + programs.mcp.servers.ai-memory = { + command = exe; + args = ["mcp-bridge" "--no-session-id" "--server-url" "${cfg.serverUrl}/mcp"]; + }; + + home.file = + { + # Client config: `ai-memory hook`, `mcp-bridge` and the installers + # read server_url and the token from here, so no generated file + # carries the token. + ".local/share/ai-memory/config.toml".text = '' + server_url = "${cfg.serverUrl}" + + [auth] + bearer_token_file = "${cfg.tokenFile}" + ''; + + # Scope marker: $HOME governs everything without a closer marker. + ".ai-memory.toml".text = marker cfg.workspace; + } + # One marker per directory that belongs to another workspace. + // mapAttrs' (dir: ws: nameValuePair "${dir}/.ai-memory.toml" {text = marker ws;}) cfg.workspaceDirs; + + home.activation.aiMemoryInstall = lib.hm.dag.entryAfter ["writeBoundary" "claudeSkillsSync"] '' + if [ ! -r ${escapeShellArg cfg.tokenFile} ]; then + echo "ai-memory: token file ${cfg.tokenFile} missing, skipping" >&2 + else + # The OpenCode plugin reads /auth-token; point it at the secret. + run mkdir -p ${escapeShellArg dataDir} + run ln -sfn ${escapeShellArg cfg.tokenFile} ${escapeShellArg "${dataDir}/auth-token"} + ${concatMapStringsSep "\n" (agent: '' + run ${exe} install-hooks --agent ${agent} --apply --server-url ${escapeShellArg cfg.serverUrl} + '') + cfg.agents} + ${optionalString (elem "claude-code" cfg.agents) '' + export PATH="${pkgs.llm-agents.claude-code}/bin:$PATH" + run claude mcp remove ai-memory -s user >/dev/null 2>&1 || true + run ${exe} install-mcp --client claude-code --apply \ + --server-url ${escapeShellArg "${cfg.serverUrl}/mcp"} + ''} + # Into ~/.agents/skills; claude-skills-sync mirrors them to Claude. + run ${exe} install-skills --scope global --agent agents + fi + ''; + }; +} diff --git a/profiles/sets/coding/agents/ai-memory.test.sh b/profiles/sets/coding/agents/ai-memory.test.sh new file mode 100755 index 0000000..8dde62f --- /dev/null +++ b/profiles/sets/coding/agents/ai-memory.test.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# Eval checks for coding.aiMemory against m3-kratos in nixos-config, using +# this checkout of m3ta-home. Usage: ai-memory.test.sh [path/to/nixos-config] +set -euo pipefail + +home_repo="$(cd "$(dirname "$0")/../../../.." && pwd)" +nixos_repo="${1:-$home_repo/../nixos-config}" +hm="#nixosConfigurations.m3-kratos.config.home-manager.users.m3tam3re" +fail=0 + +ev() { # attr [extra nix eval args...] + local attr="$1"; shift + nix eval --raw --override-input m3ta-home "path:$home_repo" "$nixos_repo$hm.$attr" "$@" 2>/dev/null +} + +check() { # name, haystack, needle + if [[ "$2" == *"$3"* ]]; then echo "ok $1"; else echo "FAIL $1: missing '$3'"; fail=1; fi +} +refute() { # name, haystack, needle + if [[ "$2" == *"$3"* ]]; then echo "FAIL $1: found '$3'"; fail=1; else echo "ok $1"; fi +} + +check "server url" "$(ev home.sessionVariables.AI_MEMORY_SERVER_URL)" "https://memory.m3ta.dev" + +conf="$(ev 'home.file.".local/share/ai-memory/config.toml".text')" +check "client config server_url" "$conf" 'server_url = "https://memory.m3ta.dev"' +check "client config token file" "$conf" 'bearer_token_file = "/run/agenix/ai-memory-token"' + +mcp="$(ev programs.mcp.servers.ai-memory.args --apply 'builtins.concatStringsSep " "')" +check "mcp bridge" "$mcp" "mcp-bridge --no-session-id --server-url https://memory.m3ta.dev/mcp" +refute "mcp carries no token" "$mcp" "ai-memory-token" + +opencode="$(cat "$(nix build --no-link --print-out-paths --override-input m3ta-home "path:$home_repo" \ + "$nixos_repo$hm.xdg.configFile.\"opencode/opencode.json\".source" 2>/dev/null)")" +check "opencode gets mcp" "$opencode" "mcp-bridge" + +marker="$(ev 'home.file.".ai-memory.toml".text')" +check "home marker workspace" "$marker" 'workspace = "private"' +check "marker section" "$marker" "[capture]" +for p in "/run/agenix/**" "~/.config/sops/**" "~/.ssh/**" "**/.env*"; do + check "marker $p" "$marker" "\"$p\"" +done + +az="$(ev 'home.file."p/AZ/.ai-memory.toml".text' 2>/dev/null || true)" +check "AZ marker workspace" "$az" 'workspace = "az"' +check "AZ marker repeats capture" "$az" "[capture]" + +act="$(ev home.activation.aiMemoryInstall.data)" +check "hooks claude-code" "$act" "install-hooks --agent claude-code --apply" +check "hooks opencode2" "$act" "install-hooks --agent opencode2 --apply" +check "auth-token symlink" "$act" "ln -sfn /run/agenix/ai-memory-token " +refute "no token on command lines" "$act" "--auth-token" +refute "activation must not write opencode.json (home-manager symlink)" "$act" "install-mcp --client opencode" + +# Missing token file: skip with a warning, exit 0, never call ai-memory. +script="$(printf '%s\n' "$act" | sed 's|/run/agenix/ai-memory-token|/nonexistent/ai-memory-token|g')" +out="$(run() { echo "CALLED $*"; }; eval "$script" 2>&1)" +check "skip warning" "$out" "token file /nonexistent/ai-memory-token missing, skipping" +refute "no installer without token" "$out" "CALLED" + +exit "$fail" diff --git a/profiles/sets/coding/agents/babysitter.nix b/profiles/sets/coding/agents/babysitter.nix deleted file mode 100644 index 12216df..0000000 --- a/profiles/sets/coding/agents/babysitter.nix +++ /dev/null @@ -1,142 +0,0 @@ -# Babysitter user profile for Pi/agent workflows. -{ - config, - lib, - ... -}: let - cfg = config.coding.agents.babysitter; - defaultTimestamp = "2026-05-29T00:00:00.000Z"; - - defaultUserProfile = { - name = config.home.username; - specialties = [ - { - domain = "declarative-system-configuration"; - subdomains = ["nix" "home-manager" "agents"]; - } - ]; - expertiseLevels = {}; - goals = [ - { - id = "goal-agents-declarative-profile"; - description = "Keep Babysitter user preferences declarative and reproducible across systems."; - category = "agents"; - priority = "medium"; - status = "active"; - } - ]; - preferences = { - verbosity = "concise"; - autonomyLevel = "semi-autonomous"; - riskTolerance = "conservative"; - }; - toolPreferences = { - packageManagers = ["nix"]; - languages = ["nix"]; - operatingSystem = "nixos"; - }; - breakpointTolerance = { - global = "moderate"; - skipBreakpointsForKnownPatterns = false; - alwaysBreakOn = ["destructive-operations" "secrets" "external-publication"]; - }; - communicationStyle = { - tone = "technical"; - language = "en"; - useEmojis = false; - explanationDepth = "brief"; - preferredResponseFormat = "markdown"; - }; - experience = {}; - installedPlugins = ["babysitter-pi"]; - installedSkills = ["babysit" "call" "plan" "resume" "doctor" "yolo"]; - installedAgents = []; - createdAt = defaultTimestamp; - updatedAt = defaultTimestamp; - version = 1; - }; - - effectiveProfile = lib.recursiveUpdate cfg.profile cfg.profileOverrides; - profileJsonTarget = "${cfg.profileDirectory}/user-profile.json"; - profileMarkdownTarget = "${cfg.profileDirectory}/user-profile.md"; - profilePreferences = effectiveProfile.preferences or {}; - profileBreakpointTolerance = effectiveProfile.breakpointTolerance or {}; - - listMarkdown = values: - lib.concatMapStringsSep "\n" (value: "- ${toString value}") values; - - specialtyDomains = map (specialty: specialty.domain or "unspecified") (effectiveProfile.specialties or []); - - profileMarkdown = '' - # Babysitter User Profile - - This file is generated by Home Manager from `coding.agents.babysitter`. - - - Name: ${effectiveProfile.name or "unspecified"} - - Breakpoint tolerance: ${profileBreakpointTolerance.global or "unspecified"} - - Autonomy level: ${profilePreferences.autonomyLevel or "unspecified"} - - Risk tolerance: ${profilePreferences.riskTolerance or "unspecified"} - - ## Specialties - - ${listMarkdown specialtyDomains} - - ## Installed Babysitter Pi Skills - - ${listMarkdown (effectiveProfile.installedSkills or [])} - ''; -in { - options.coding.agents.babysitter = { - enable = lib.mkEnableOption "Babysitter user profile for Pi/agent workflows"; - - profileDirectory = lib.mkOption { - type = lib.types.str; - default = ".a5c"; - description = '' - Home-relative directory where Babysitter reads the user profile. - Babysitter Pi documents the user profile at ~/.a5c/user-profile.json. - ''; - }; - - profile = lib.mkOption { - type = lib.types.attrsOf lib.types.anything; - default = defaultUserProfile; - description = "Complete non-secret Babysitter user profile JSON."; - }; - - profileOverrides = lib.mkOption { - type = lib.types.attrsOf lib.types.anything; - default = {}; - description = '' - Recursive overrides merged into the default Babysitter user profile. - Keep this non-secret; the result is written into the Nix store. - ''; - }; - }; - - config = lib.mkIf cfg.enable { - assertions = [ - { - assertion = cfg.profileDirectory != "" && !lib.hasPrefix "/" cfg.profileDirectory; - message = "coding.agents.babysitter.profileDirectory must be a non-empty home-relative path."; - } - { - assertion = effectiveProfile ? name && builtins.isString effectiveProfile.name; - message = "coding.agents.babysitter profile must include a string name."; - } - { - assertion = effectiveProfile ? specialties && builtins.isList effectiveProfile.specialties; - message = "coding.agents.babysitter profile must include a specialties list."; - } - { - assertion = effectiveProfile ? breakpointTolerance && effectiveProfile.breakpointTolerance ? global; - message = "coding.agents.babysitter profile must include breakpointTolerance.global."; - } - ]; - - home.file = { - "${profileJsonTarget}".text = builtins.toJSON effectiveProfile + "\n"; - "${profileMarkdownTarget}".text = profileMarkdown; - }; - }; -} diff --git a/profiles/sets/coding/agents/pi.nix b/profiles/sets/coding/agents/pi.nix deleted file mode 100644 index efb52e8..0000000 --- a/profiles/sets/coding/agents/pi.nix +++ /dev/null @@ -1,239 +0,0 @@ -{...}: { - coding.agents.pi = { - enable = true; - - settings = { - packages = [ - "npm:@dreadedzombie/pi-init" - "git:github.com/a5c-ai/babysitter-pi" - "npm:@plannotator/pi-extension" - "npm:pi-agent-browser-native" - "npm:pi-beads-extension" - "npm:pi-lens" - "npm:pi-markdown-preview" - "npm:pi-mcp-adapter" - "npm:pi-powerline-footer" - "npm:pi-prompt-template-model" - "npm:pi-subagents" - "npm:pi-tool-display" - "npm:pi-web-access" - ]; - - defaultProvider = "openai-codex"; - defaultModel = "gpt-5.5"; - defaultThinkingLevel = "high"; - }; - - # pi-guardrails: strict security config - # NOTE: Path access checks are lexical (not symlink-safe). - # NOTE: Local project .pi/extensions/guardrails.json can override same rule IDs. - # For immutable global policies, consider a wrapper or upstream patch. - guardrails = { - enable = false; - config = { - enabled = true; - applyBuiltinDefaults = true; - - onboarding = { - completed = true; - }; - - features = { - policies = true; - permissionGate = true; - pathAccess = true; - }; - - pathAccess = { - mode = "ask"; - allowedPaths = [ - "/nix/store/" - "/tmp/" - ]; - }; - - policies = { - rules = [ - # ── SSH keys ─────────────────────────────────────────── - { - id = "home-ssh"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "~/.ssh/**";} - {pattern = "~/.ssh/*_rsa";} - {pattern = "~/.ssh/*_ed25519";} - {pattern = "~/.ssh/*.pem";} - ]; - allowedPatterns = [ - {pattern = "~/.ssh/*.pub";} - ]; - } - - # ── GPG keys ───────────────────────────────────────── - { - id = "home-gpg"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "~/.gnupg/**";} - {pattern = "~/*.gpg";} - {pattern = "~/.gpg-agent.conf";} - ]; - } - - # ── AWS credentials ──────────────────────────────────── - { - id = "home-aws"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "~/.aws/**";} - {pattern = "~/.aws/credentials";} - {pattern = "~/.aws/config";} - ]; - } - - # ── Kubernetes configs ──────────────────────────────── - { - id = "home-kube"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "~/.kube/**";} - {pattern = "*kubeconfig*";} - ]; - } - - # ── Cloud CLI configs ──────────────────────────────── - { - id = "home-config"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "~/.config/gh/**";} - {pattern = "~/.config/gcloud/**";} - {pattern = "~/.config/op/**";} - {pattern = "~/.config/sops/**";} - ]; - } - - # ── agenix secrets ─────────────────────────────────── - { - id = "agenix-secrets"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "/run/agenix/**";} - ]; - } - - # ── Pi auth and sessions ──────────────────────────── - { - id = "pi-auth-sessions"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "~/.pi/agent/auth.json";} - {pattern = "~/.pi/agent/sessions/**";} - ]; - } - - # ── Environment files ───────────────────────────────── - { - id = "secret-files"; - enabled = true; - protection = "noAccess"; - onlyIfExists = true; - patterns = [ - {pattern = ".env";} - {pattern = ".env.*";} - {pattern = ".dev.vars";} - ]; - allowedPatterns = [ - {pattern = "*.example.env";} - {pattern = "*.sample.env";} - {pattern = "*.test.env";} - {pattern = ".env.example";} - {pattern = ".env.sample";} - {pattern = ".env.test";} - ]; - } - - # ── Private keys and certificates ─────────────────── - { - id = "private-keys"; - enabled = true; - protection = "noAccess"; - onlyIfExists = false; - patterns = [ - {pattern = "*.pem";} - {pattern = "*.key";} - {pattern = "*.p12";} - {pattern = "*.pfx";} - {pattern = "*id_rsa*";} - {pattern = "*id_ed25519*";} - {pattern = "*id_ecdsa*";} - ]; - allowedPatterns = [ - {pattern = "*.pub";} - {pattern = "*.csr";} - ]; - } - ]; - }; - - permissionGate = { - explainCommands = false; - # Auto-deny patterns: env leakage and credential dumping - autoDenyPatterns = [ - { - pattern = "\\benv\\b"; - regex = true; - description = "env command (may dump environment)"; - } - { - pattern = "\\bprintenv\\b"; - regex = true; - description = "printenv command (dumps environment variables)"; - } - { - pattern = "/proc/[0-9]+/environ"; - regex = true; - description = "reading process environment files"; - } - { - pattern = "gpg\\s+--export-secret-keys"; - regex = true; - description = "GPG secret key export"; - } - { - pattern = "gpg\\s+--export-secret-subkeys"; - regex = true; - description = "GPG secret subkey export"; - } - { - pattern = "ssh-add\\s+-D"; - regex = true; - description = "delete all SSH identities"; - } - { - pattern = "\\b(op|pass)\\s+(read|show|get)"; - regex = true; - description = "password manager read operations"; - } - ]; - }; - }; - }; - - # MCP servers auto-inherited from programs.mcp in default.nix - }; -}