diff --git a/.gitea/workflows/llm-agents.yaml b/.gitea/workflows/llm-agents.yaml new file mode 100644 index 0000000..25501f4 --- /dev/null +++ b/.gitea/workflows/llm-agents.yaml @@ -0,0 +1,39 @@ +name: Build llm-agents + +on: + pull_request: + branches: [master] + types: [opened, synchronize, reopened] + workflow_dispatch: + +jobs: + build: + if: >- + (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/automation/llm-agents') || + (github.event.pull_request.head.repo.full_name == github.repository && + github.event.pull_request.head.ref == 'automation/llm-agents') + runs-on: ubuntu-26.04 + permissions: + contents: read + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + - uses: DeterminateSystems/determinate-nix-action@v3 + with: + github-token: ${{ secrets.GH_PULL_TOKEN }} + extra-conf: | + sandbox-fallback = false + lazy-trees = true + eval-cores = 0 + - name: Build installed agent packages + shell: bash + run: | + attrs="$(nix eval --accept-flake-config --no-update-lock-file --raw \ + .#llmAgentPackages.x86_64-linux \ + --apply 'packages: builtins.concatStringsSep "\n" (builtins.attrNames packages)')" + if [[ -n "$attrs" ]]; then + mapfile -t packages <<< "$attrs" + nix build --accept-flake-config --no-update-lock-file --no-link \ + "${packages[@]/#/.#llmAgentPackages.x86_64-linux.}" + fi diff --git a/.gitea/workflows/update-llm-agents.yaml b/.gitea/workflows/update-llm-agents.yaml new file mode 100644 index 0000000..22da317 --- /dev/null +++ b/.gitea/workflows/update-llm-agents.yaml @@ -0,0 +1,30 @@ +name: Update llm-agents + +on: + schedule: + - cron: '19 */4 * * *' + workflow_dispatch: + +concurrency: + group: update-llm-agents + +jobs: + update: + if: github.ref == 'refs/heads/master' + runs-on: ubuntu-26.04 + permissions: + contents: write + pull-requests: write + actions: write + steps: + - uses: actions/checkout@v6 + with: + ref: master + persist-credentials: false + - uses: DeterminateSystems/determinate-nix-action@v3 + with: + github-token: ${{ secrets.GH_PULL_TOKEN }} + - name: Update the dependency PR + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + run: bash ci/update-llm-agents.sh diff --git a/ci/llm-agent-packages.nix b/ci/llm-agent-packages.nix new file mode 100644 index 0000000..4daba7c --- /dev/null +++ b/ci/llm-agent-packages.nix @@ -0,0 +1,29 @@ +{ lib, inputs, nixfiles, system }: +let + inherit (builtins) any attrValues filter tryEval; + inherit (lib) concatMap filterAttrs getName groupBy isDerivation; + inherit (nixfiles.config) nixos home-manager; + + systems = filter (box: box.system == system) (attrValues nixos.systems); + homes = filter (home: home.system == system) (attrValues home-manager.homes); + + installedPackages = + concatMap + (box: + let cfg = box.rendered.config; + in cfg.environment.systemPackages + ++ concatMap (home: home.home.packages) (attrValues cfg.home-manager.users)) + systems + ++ concatMap (home: home.configuration.config.home.packages) homes; + + installedByName = groupBy getName installedPackages; +in +filterAttrs + (_: package: + # Check names before forcing output paths. Some unrelated upstream exports + # are helpers or packages that cannot be evaluated on this platform. + let name = tryEval (if isDerivation package then getName package else null); + in name.success && name.value != null + && any (installed: installed.drvPath == package.drvPath) + (installedByName.${name.value} or [ ])) + (inputs.llm-agents.packages.${system} or { }) diff --git a/ci/update-llm-agents.sh b/ci/update-llm-agents.sh new file mode 100755 index 0000000..8620cd1 --- /dev/null +++ b/ci/update-llm-agents.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +set -euo pipefail + +branch=automation/llm-agents +ref="refs/heads/$branch" +title='flake: Update llm-agents' +repo_api="${GITHUB_SERVER_URL%/}/api/v1/repos/$GITHUB_REPOSITORY" +workflow="$repo_api/actions/workflows/llm-agents.yaml" + +api() { + curl --fail-with-body --silent --show-error --max-time 30 \ + --header "Authorization: token $GITEA_TOKEN" \ + --header 'Content-Type: application/json' "$@" +} + +versions() { + nix eval --accept-flake-config --no-update-lock-file --json \ + .#llmAgentPackages.x86_64-linux \ + --apply 'builtins.mapAttrs (_: package: package.version)' "$@" +} + +open_pr() { + local page=1 pulls pr + while :; do + pulls="$(api "$repo_api/pulls?state=open&base_branch=master&limit=50&page=$page")" + if [[ "$(jq 'length' <<< "$pulls")" == 0 ]]; then + return + fi + + pr="$(jq -r --arg branch "$branch" --arg repo "$GITHUB_REPOSITORY" ' + .[] | select(.head.ref == $branch and .head.repo.full_name == $repo) | .html_url + ' <<< "$pulls")" + if [[ -n "$pr" ]]; then + printf '%s\n' "$pr" + return + fi + + page=$((page + 1)) + done +} + +# Capture the branch tip before computing an update so the lease protects +# against a concurrent run publishing while we evaluate. +remote_tip="$(git ls-remote origin "$ref")" +previous="${remote_tip%%$'\t'*}" +if [[ -n "$previous" ]]; then + git fetch --no-tags --depth=2 origin "$ref" +fi + +nix flake update --accept-flake-config llm-agents +if git diff --quiet -- flake.lock; then + echo 'The llm-agents pin is current.' + exit +fi + +base_lock="$(mktemp)" +pending_lock="$(mktemp)" +trap 'rm -f "$base_lock" "$pending_lock"' EXIT +git show HEAD:flake.lock > "$base_lock" +before="$(versions --reference-lock-file "$base_lock")" +after="$(versions)" +if [[ "$before" == "$after" ]]; then + echo 'Installed llm-agents package versions are unchanged.' + exit +fi + +pending='' +if [[ -n "$previous" ]] && git diff --quiet HEAD "$previous^" -- flake.lock; then + git show "$previous:flake.lock" > "$pending_lock" + if cmp -s flake.lock "$pending_lock"; then + pending="$after" + else + pending="$(versions --reference-lock-file "$pending_lock")" + fi +fi + +head="$previous" +if [[ "$pending" != "$after" ]]; then + changes="$(jq -nr --argjson before "$before" --argjson after "$after" ' + (($before + $after) | keys[]) as $name + | select($before[$name] != $after[$name]) + | "\($name) \($before[$name] // "absent") -> \($after[$name] // "absent")" + ')" + git add flake.lock + git -c user.name='github-actions[bot]' \ + -c user.email='github-actions[bot]@users.noreply.github.com' \ + commit -m "$title" -m "$changes" + + # Pass the credential to Git without putting it in the remote URL. + auth="$(printf 'oauth2:%s' "$GITEA_TOKEN" | base64 -w0)" + GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraHeader \ + GIT_CONFIG_VALUE_0="Authorization: Basic $auth" \ + git push --force-with-lease="$ref:$previous" origin "HEAD:$ref" + head="$(git rev-parse HEAD)" +fi + +pr="$(open_pr)" +if [[ -z "$pr" ]]; then + payload="$(jq -n --arg base master --arg head "$branch" --arg title "$title" ' + {base: $base, head: $head, title: $title, + body: "Updates the llm-agents pin when an installed package changes version. See the commit message for the version changes."} + ')" + pr="$(api --data "$payload" "$repo_api/pulls" | jq -er '.html_url')" +fi +printf 'Update PR %s\n' "$pr" + +# Gitea suppresses workflow events from the Actions token. Dispatch a build +# explicitly, recovering on the next poll if a previous dispatch failed. +runs="$(api "$workflow/runs?head_sha=$head&limit=1")" +if [[ "$(jq '.total_count' <<< "$runs")" == 0 ]]; then + payload="$(jq -n --arg ref "$branch" '{ref: $ref}')" + api --data "$payload" "$workflow/dispatches" +fi diff --git a/docs/deployment.md b/docs/deployment.md index 9ffc6d1..f2f7662 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -84,9 +84,10 @@ Boxes default to `fish`, so pipe multi-statement remote scripts through `bash`, ## Coding agents -The shared GUI home-manager module installs Codex and Claude Code from the `llm-agents` input, retaining -that input's tested nixpkgs pin. Run `nix flake update llm-agents`, then switch the system -or home configuration to use the updated package. +The shared GUI home-manager module installs Codex and Claude Code from the `llm-agents` input, +retaining that input's tested nixpkgs pin. [CI proposes version updates](#llm-agents-updates). +To refresh the pin manually, run `nix flake update llm-agents`, then switch the system or home +configuration to use the updated packages. The shared cache settings in `lib.my.c.nix.cache` and the flake's `nixConfig` include the [Numtide binary cache](https://github.com/numtide/llm-agents.nix#binary-cache), so @@ -327,6 +328,23 @@ It then builds `.#ciDrv.x86_64-linux`, a `linkFarm` of all CI attributes, and pu Pushing the `installer` tag (refreshed by `update-installer`) builds `my.buildAs.iso` and `my.buildAs.netbootArchive`, then attaches both to a release. +### llm-agents updates + +[`update-llm-agents.yaml`](../.gitea/workflows/update-llm-agents.yaml) checks for updates every +four hours and can also be run manually on `master`. It matches packages directly installed +by the x86_64 Linux NixOS and home-manager configurations to derivations exported by +`llm-agents`. This discovers the tracked packages without building a whole system. + +When an installed version changes, CI commits the updated lockfile on the dedicated +`automation/llm-agents` branch. The commit message lists the version changes. New versions +refresh the branch behind the existing open PR. Upstream revisions with the same package +versions leave that branch alone. + +[`llm-agents.yaml`](../.gitea/workflows/llm-agents.yaml) builds the discovered packages for +the update commit using the flake's caches. The updater dispatches the build explicitly +because Gitea suppresses automatic workflow events from its job token. The updater needs +the repository's Actions token limits to allow writes to code, pull requests and Actions. + ### `update-docs.yaml` On pushes to `master`, excluding its own commits, this runs the assignment, option and DNS diff --git a/flake.nix b/flake.nix index 8bfbedd..18f7ede 100644 --- a/flake.nix +++ b/flake.nix @@ -287,6 +287,8 @@ (import ./pkgs { inherit lib pkgs; }) // (import ./openwrt { inherit pkgs inputs; })); + llmAgentPackages = import ./ci/llm-agent-packages.nix { inherit lib inputs nixfiles system; }; + devShells.default = shell; ci =