diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..fe0bdf2 --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,109 @@ +name: build +on: + push: + branches: [main] + paths-ignore: [bin.json] + workflow_dispatch: +env: + MACOS_SDK: "14.5" +jobs: + # next version = highest vX.Y.Z tag with the patch bumped; push a tag by hand for minor/major + version: + runs-on: ubuntu-latest + outputs: + version: ${{ steps.next.outputs.version }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - id: next + run: | + last=$(git tag -l 'v[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n1) + if [ -z "$last" ]; then + next=0.1.0 + else + last=${last#v} + next="${last%.*}.$(( ${last##*.} + 1 ))" + fi + echo "version=$next" >> "$GITHUB_OUTPUT" + build: + needs: version + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + target: [x86_64-unknown-linux-musl, aarch64-apple-darwin] + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + with: + targets: ${{ matrix.target }} + # caches ~/.cargo registry/git and target/, keyed on Cargo.lock + toolchain + - uses: Swatinem/rust-cache@v2 + with: + key: ${{ matrix.target }} + # zig as cross C compiler/linker for both musl and macOS (rusqlite bundles sqlite) + - run: pip install ziglang cargo-zigbuild + # macOS: Apple SDK for system frameworks + - if: matrix.target == 'aarch64-apple-darwin' + id: sdk-cache + uses: actions/cache@v4 + with: + path: ~/MacOSX${{ env.MACOS_SDK }}.sdk + key: macos-sdk-${{ env.MACOS_SDK }} + - if: matrix.target == 'aarch64-apple-darwin' && steps.sdk-cache.outputs.cache-hit != 'true' + run: curl -fsSL "https://github.com/joseluisq/macosx-sdks/releases/download/${MACOS_SDK}/MacOSX${MACOS_SDK}.sdk.tar.xz" | tar -xJ -C "$HOME" + - if: matrix.target == 'aarch64-apple-darwin' + run: echo "SDKROOT=$HOME/MacOSX${MACOS_SDK}.sdk" >> "$GITHUB_ENV" + - run: cargo zigbuild --release --locked --target ${{ matrix.target }} + env: + ORCH_VERSION: ${{ needs.version.outputs.version }} + - uses: actions/upload-artifact@v3 + with: + name: orch-${{ matrix.target }} + path: target/${{ matrix.target }}/release/orch + release: + needs: [build, version] + runs-on: ubuntu-latest + steps: + # main, not the build sha: bin.json is committed on top of it + - uses: actions/checkout@v4 + with: + ref: main + - uses: actions/download-artifact@v3 + with: + path: dist + # artifacts lose the executable bit, restore it before packaging + - working-directory: dist + run: | + for t in orch-*; do + chmod +x "$t/orch" + tar -czf "$t.tar.gz" -C "$t" orch + done + sha256sum *.tar.gz > SHA256SUMS + - env: + TOKEN: ${{ secrets.GITEA_TOKEN }} + API: ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }} + TAG: v${{ needs.version.outputs.version }} + SHA: ${{ gitea.sha }} + # Gitea creates the tag at target_commitish when it doesn't exist + run: | + id=$(curl -fsS -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ + -d "{\"tag_name\":\"$TAG\",\"name\":\"$TAG\",\"target_commitish\":\"$SHA\"}" "$API/releases" | jq -r .id) + for f in dist/*.tar.gz dist/SHA256SUMS; do + curl -fsS -X POST -H "Authorization: token $TOKEN" \ + -F "attachment=@$f" "$API/releases/$id/assets?name=$(basename "$f")" + done + # flake.nix reads bin.json to fetch these tarballs instead of building from source + - env: + VERSION: ${{ needs.version.outputs.version }} + run: | + jq -Rn --arg v "$VERSION" \ + '{version: $v, sha256: ([inputs | split(" ") | {(.[1] | ltrimstr("orch-") | rtrimstr(".tar.gz")): .[0]}] | add)}' \ + < dist/SHA256SUMS > bin.json + git config user.name gitea-actions + git config user.email gitea-actions@git.jokresner.de + git add bin.json + git commit -m "ci: bin.json v$VERSION" + git pull --rebase origin main + git push origin HEAD:main diff --git a/.gitignore b/.gitignore index 95c847d..9f1b9e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ +result __pycache__/ *.jsonl diff --git a/bin.json b/bin.json new file mode 100644 index 0000000..9672095 --- /dev/null +++ b/bin.json @@ -0,0 +1,4 @@ +{ + "version": "0.0.0", + "sha256": {} +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..28a0c8b --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1794386569, + "narHash": "sha256-x9sADJvoVG0L6RejZB+/BB4YiGZFBZrfTlhF4uBq1tQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d4f25a3e562008648d45bcf2d70bcc7422cad84e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..b264c13 --- /dev/null +++ b/flake.nix @@ -0,0 +1,77 @@ +{ + description = "orch: orchestrator that runs many hx coding agents"; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + + outputs = + { self, nixpkgs }: + let + systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ]; + forAll = f: nixpkgs.lib.genAttrs systems (s: f nixpkgs.legacyPackages.${s}); + # release binaries, bin.json is rewritten by the release job in .gitea/workflows/build.yml + bin = builtins.fromJSON (builtins.readFile ./bin.json); + targets = { + x86_64-linux = "x86_64-unknown-linux-musl"; + aarch64-darwin = "aarch64-apple-darwin"; + }; + in + { + packages = forAll (pkgs: let + target = targets.${pkgs.stdenv.hostPlatform.system} or ""; + hasBin = bin.sha256 ? ${target}; + runtimePath = pkgs.lib.makeBinPath [ pkgs.git ]; + + # only forced when hasBin, bin.sha256.${target} would throw otherwise + orch-bin = pkgs.runCommand "orch-${bin.version}" + { nativeBuildInputs = [ pkgs.makeWrapper ]; meta.mainProgram = "orch"; } + '' + mkdir -p $out/bin + tar -xzf ${ + pkgs.fetchurl { + url = "https://git.jokresner.de/johannes/orch/releases/download/v${bin.version}/orch-${target}.tar.gz"; + sha256 = bin.sha256.${target}; + } + } -C $out/bin orch + wrapProgram $out/bin/orch --prefix PATH : ${runtimePath} + ''; + in rec { + orch = pkgs.rustPlatform.buildRustPackage { + pname = "orch"; + version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version; + src = pkgs.lib.cleanSource ./.; + cargoLock.lockFile = ./Cargo.lock; + # ponytail: tests skipped (python e2e needs git/hx); enable when hermetic. + doCheck = false; + nativeBuildInputs = [ pkgs.makeWrapper pkgs.pkg-config ]; + # nixpkgs links libsqlite3-sys against the system sqlite + buildInputs = [ pkgs.sqlite ]; + # orch shells out to git; hx is supplied by the user. + postInstall = '' + wrapProgram $out/bin/orch --prefix PATH : ${runtimePath} + ''; + meta = { + description = "Orchestrator that runs many hx coding agents"; + mainProgram = "orch"; + }; + }; + + # prebuilt binary where the release has one, source build elsewhere + default = if hasBin then orch-bin else orch; + } // pkgs.lib.optionalAttrs hasBin { inherit orch-bin; }); + + apps = forAll (pkgs: rec { + orch = { type = "app"; program = "${self.packages.${pkgs.system}.default}/bin/orch"; }; + default = orch; + }); + + overlays.default = _: prev: { orch = self.packages.${prev.system}.default; }; + + devShells = forAll (pkgs: { + default = pkgs.mkShell { + packages = with pkgs; [ + cargo rustc clippy rustfmt rust-analyzer git sqlite markdownlint-cli lychee + ]; + }; + }); + }; +} diff --git a/src/main.rs b/src/main.rs index cfb736b..97229c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,8 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; #[derive(Parser)] #[command( - version, + // CI sets ORCH_VERSION to the release version; local builds use Cargo's + version = option_env!("ORCH_VERSION").unwrap_or(env!("CARGO_PKG_VERSION")), about = "Run hx coding agents on tasks; without a subcommand, opens the TUI" )] struct Cli {