From 071161b6d656c64d01a2c9c7d8738dcd88f9f872 Mon Sep 17 00:00:00 2001 From: m3ta-chiron Date: Mon, 2 Nov 2026 08:00:24 +0100 Subject: [PATCH] feat(t3code): move agent identity overlay here, support two Gitea instances Co-Authored-By: Claude Opus 5.5 --- overlays/t3code-agent-identity.nix | 120 +++++++++++++++++++++++++ overlays/t3code-agent-identity.test.sh | 89 ++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 overlays/t3code-agent-identity.nix create mode 100644 overlays/t3code-agent-identity.test.sh diff --git a/overlays/t3code-agent-identity.nix b/overlays/t3code-agent-identity.nix new file mode 100644 index 0000000..7c3c906 --- /dev/null +++ b/overlays/t3code-agent-identity.nix @@ -0,0 +1,120 @@ +# T3 Code agents act as the m3ta-chiron bot on every host. Baked into the +# T3 packages rather than a session or service: everything T3 spawns +# inherits it, however T3 was launched (desktop launcher, systemd, shell), +# and any host installing T3 gets it. Must come AFTER shared-nixpkgs. +# +# Push auth needs ~/.ssh/m3ta-chiron on each host, with its pubkey on the +# m3ta-chiron account of each Gitea instance. A missing key fails the push +# instead of falling back to a personal key. Other hosts keep the user's own +# ssh config. +# +# Change requests need one agenix secret per instance, owned by the T3 user. +# Without it T3 cannot recognize that instance as Gitea; the others still work. +{tokenFiles ? {}}: final: prev: let + identity = { + name = "m3ta-chiron"; + email = "m3ta-chiron@agentmail.to"; + }; + + instances = [ + { + name = "m3ta"; + url = "https://code.m3ta.dev"; + host = "code.m3ta.dev"; + port = 22; + knownHost = "code.m3ta.dev"; + hostKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFmmqZ4GgDEXJgj8uYrZAr/EoLIA6LJGkTIyKGQD/60a"; + } + { + name = "az"; + url = "https://git.az-gruppe.com"; + host = "git.az-gruppe.com"; + port = 2022; + knownHost = "[git.az-gruppe.com]:2022"; + hostKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIItSijmU5YwcJcoshtmYxpxBaVA4TPaCMk23ws7KDkAH"; + } + ]; + + tokenFile = instance: tokenFiles.${instance.name} or "/run/agenix/agent-gitea-token-${instance.name}"; + + giteaKnownHosts = final.writeText "gitea-known-hosts" ( + final.lib.concatMapStrings (instance: "${instance.knownHost} ${instance.hostKey}\n") instances + ); + + # ssh expands ~ from passwd, so this does not depend on $HOME. + sshConfig = final.writeText "t3-agent-ssh-config" '' + ${final.lib.concatMapStrings (instance: '' + Host ${instance.host} + User gitea + Port ${toString instance.port} + IdentityFile ~/.ssh/m3ta-chiron + IdentitiesOnly yes + UserKnownHostsFile ${giteaKnownHosts} + + '') + instances} + # Conditional: IdentityFile accumulates across matching blocks, so a + # personal entry for these hosts must not be read. + Host * ${final.lib.concatMapStringsSep " " (instance: "!${instance.host}") instances} + Include ~/.ssh/config + ''; + + # T3 only recognizes a Gitea on a custom domain through a matching tea + # login, and tea picks the login by the host of the git remote, so a token + # only reaches its own instance. The config lives in a private temp dir per + # call: tokens stay out of the nix store and a personal ~/.config/tea is left + # alone. + agentTea = final.writeShellApplication { + name = "tea"; + text = '' + config="$(${final.coreutils}/bin/mktemp -d)" + trap '${final.coreutils}/bin/rm -rf "$config"' EXIT + ${final.coreutils}/bin/mkdir "$config/tea" + default=true + ${final.lib.concatMapStrings (instance: '' + if [ -r ${tokenFile instance} ]; then + if [ "$default" = true ]; then + printf 'logins:\n' >"$config/tea/config.yml" + fi + printf ' - name: %s\n url: %s\n token: %s\n user: %s\n default: %s\n' \ + ${instance.name} ${instance.url} "$(${final.coreutils}/bin/cat ${tokenFile instance})" \ + ${identity.name} "$default" >>"$config/tea/config.yml" + default=false + fi + '') + instances} + XDG_CONFIG_HOME="$config" ${final.tea}/bin/tea "$@" + ''; + }; + + wrapperArgs = final.lib.concatStringsSep " " (final.lib.mapAttrsToList (name: value: "--set ${name} ${final.lib.escapeShellArg value}") { + GIT_AUTHOR_NAME = identity.name; + GIT_AUTHOR_EMAIL = identity.email; + GIT_COMMITTER_NAME = identity.name; + GIT_COMMITTER_EMAIL = identity.email; + JJ_USER = identity.name; + JJ_EMAIL = identity.email; + # bd otherwise falls back to the user's git user.name for its audit trail. + BEADS_ACTOR = identity.name; + GIT_SSH_COMMAND = "${final.openssh}/bin/ssh -F ${sshConfig}"; + }); + + wrapT3 = package: + final.symlinkJoin { + inherit (package) name meta passthru; + paths = [package]; + nativeBuildInputs = [final.makeWrapper]; + postBuild = '' + for program in $out/bin/*; do + wrapProgram "$program" ${wrapperArgs} --prefix PATH : ${agentTea}/bin + done + ''; + }; +in { + llm-agents = + prev.llm-agents + // { + t3code = wrapT3 prev.llm-agents.t3code; + t3code-desktop = wrapT3 prev.llm-agents.t3code-desktop; + }; +} diff --git a/overlays/t3code-agent-identity.test.sh b/overlays/t3code-agent-identity.test.sh new file mode 100644 index 0000000..4cf40bd --- /dev/null +++ b/overlays/t3code-agent-identity.test.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# Checks for overlays.t3code-agent-identity with a stub t3 that runs its +# arguments inside the wrapper's environment. +# Usage: t3code-agent-identity.test.sh +set -euo pipefail + +here="$(cd "$(dirname "$0")" && pwd)" +repo="$(cd "$here/.." && pwd)" +fail=0 + +contains() { # name, haystack, needle + if [[ "$2" == *"$3"* ]]; then echo "ok $1"; else echo "FAIL $1: missing '$3' in '$2'"; fail=1; fi +} +refute() { # name, haystack, needle + if [[ "$2" == *"$3"* ]]; then echo "FAIL $1: found '$3'"; fail=1; else echo "ok $1"; fi +} + +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT +secret="fake-token-$$-do-not-leak" + +build() { # m3ta token file, az token file + nix build --no-link --print-out-paths --impure --expr " + let + flake = builtins.getFlake \"path:$repo\"; + pkgs = import flake.inputs.nixpkgs { + system = \"x86_64-linux\"; + overlays = [ + (final: prev: { + llm-agents.t3code = final.writeShellScriptBin \"t3\" ''exec \"\$@\"''; + llm-agents.t3code-desktop = final.writeShellScriptBin \"t3-desktop\" ''exec \"\$@\"''; + }) + (import $repo/overlays/t3code-agent-identity.nix { + tokenFiles = {m3ta = \"$1\"; az = \"$2\";}; + }) + ]; + }; + in pkgs.llm-agents.t3code" +} + +printf '%s' "$secret" >"$work/token" +both="$(build "$work/token" "$work/token")" +az_only="$(build "$work/missing" "$work/token")" +none="$(build "$work/missing" "$work/missing")" + +export TMPDIR="$work/tmp" +mkdir "$TMPDIR" + +t3() { "$1/bin/t3" "${@:2}"; } + +out="$(t3 "$both" tea logins)" +contains "both: m3ta login" "$out" "https://code.m3ta.dev" +contains "both: az login" "$out" "https://git.az-gruppe.com" +refute "login list hides the token" "$out" "$secret" + +out="$(t3 "$az_only" tea logins)" +contains "az only: az login" "$out" "https://git.az-gruppe.com" +refute "az only: no m3ta login" "$out" "code.m3ta.dev" +out="$(t3 "$az_only" tea logins default)" +contains "az only: az is default" "$out" "az" + +t3 "$none" tea logins >/dev/null && echo "ok no token: tea still runs" || { echo "FAIL no token: tea failed"; fail=1; } + +if [ -n "$(ls -A "$TMPDIR")" ]; then echo "FAIL temp config left behind"; fail=1; else echo "ok temp config removed"; fi +if grep -rqF "$secret" "$both/bin"/; then echo "FAIL token in the store"; fail=1; else echo "ok token not in the wrapper"; fi + +sshcmd="$(t3 "$both" printenv GIT_SSH_COMMAND)" +g() { $sshcmd -G "$1"; } + +out="$(g git.az-gruppe.com)" +contains "az: port" "$out" "port 2022" +contains "az: user" "$out" "user gitea" +contains "az: key" "$out" "identityfile ~/.ssh/m3ta-chiron" +known="$(echo "$out" | awk '/^userknownhostsfile/ {print $2}')" +contains "az: host key" "$(cat "$known")" "[git.az-gruppe.com]:2022 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIItSijmU5YwcJcoshtmYxpxBaVA4TPaCMk23ws7KDkAH" + +out="$(g code.m3ta.dev)" +contains "m3ta: port" "$out" "port 22" +contains "m3ta: user" "$out" "user gitea" +contains "m3ta: key" "$out" "identityfile ~/.ssh/m3ta-chiron" +contains "m3ta: host key" "$(cat "$known")" "code.m3ta.dev ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFmmqZ4GgDEXJgj8uYrZAr/EoLIA6LJGkTIyKGQD/60a" + +refute "other hosts keep their own key" "$(g example.org)" "m3ta-chiron" + +contains "author" "$(t3 "$both" printenv GIT_AUTHOR_NAME)" "m3ta-chiron" +contains "committer" "$(t3 "$both" printenv GIT_COMMITTER_EMAIL)" "m3ta-chiron@agentmail.to" +contains "beads actor" "$(t3 "$both" printenv BEADS_ACTOR)" "m3ta-chiron" + +exit "$fail"