justfile

set positional-arguments

build_dir := "build"

# Component-owned packages; shared CLI/frontend/TUI regressions stay in core.
agenthub_go_pattern := "/(cmd/ez-agent-hub|internal/agenthub|internal/hubfeature|pkg/ezhubapi)(/|$)"

# List all recipes
default:
    @just --list

# Build all binaries into build/
build:
    @mkdir -p {{ build_dir }}
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent ./cmd/ez-agent
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent-service ./cmd/ez-agent-service
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent-hub ./cmd/ez-agent-hub
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent-builtin ./cmd/ez-agent-builtin
    CGO_ENABLED=0 go build -o {{ build_dir }}/ezagent-muse-code-bridge ./cmd/ezagent-muse-code-bridge
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent-hello ./cmd/ez-agent-hello
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent-configurator ./cmd/ez-agent-configurator
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent-webfetch ./cmd/ez-agent-webfetch
    CGO_ENABLED=0 go build -o {{ build_dir }}/ez-agent-tunnel ./cmd/ez-agent-tunnel
    ./ez-agent-ruby/runtime/build.sh
    cp ez-agent-ruby/build/ez-agent-ruby {{ build_dir }}/ez-agent-ruby

# Build the native Muse bridge without rebuilding unrelated targets
build-muse-bridge:
    @mkdir -p {{ build_dir }}
    CGO_ENABLED=0 go build -o {{ build_dir }}/ezagent-muse-code-bridge ./cmd/ezagent-muse-code-bridge

# Build release archive and self-extracting installer into dist/
dist: build
    ./scripts/build-dist.sh

# Test distribution packaging with isolated fixture binaries
test-dist:
    sh scripts/test-build-dist.sh

# Remove generated binaries and payload; preserve the expensive Ruby build cache
clean:
    rm -rf {{ build_dir }}
    rm -rf dist
    rm -rf ez-agent-ruby/build
    rm -f ez-agent-ruby/cmd/ez-agent-ruby/payload.zip

# Remove the downloaded/extracted Ruby and musl-cross build cache
clean-ruby-cache:
    rm -rf ez-agent-ruby/.build

# Remove orphaned binaries from project root
clean-orphaned:
    @find . -maxdepth 1 -name 'ez-agent*' ! -type d -exec rm -v {} \;

# Refresh/check the committed third-party notice embedded in ez-agent.
licenses:
    go run ./cmd/genlicenses -output pkg/buildinfo/third_party_licenses.txt

licenses-check:
    go run ./cmd/genlicenses -check -output pkg/buildinfo/third_party_licenses.txt

# Run go vet on all packages
vet:
    go vet ./...
    just vet-agenthub-platforms
    just vet-frontend-platforms

# Unix frontend targets supported by the current dependencies. No runtime claim.
vet-frontend-platforms:
    #!/bin/sh
    set -e
    mkdir -p {{ build_dir }}
    for target in darwin freebsd openbsd; do
        echo "== Frontend/TUI ${target}/amd64 test compile =="
        CGO_ENABLED=0 GOOS="$target" GOARCH=amd64 go test -c -o {{ build_dir }}/frontend-$target.test ./internal/frontend
        CGO_ENABLED=0 GOOS="$target" GOARCH=amd64 go test -c -o {{ build_dir }}/tui-$target.test ./pkg/tui
        echo "== Frontend/TUI ${target}/amd64 vet =="
        CGO_ENABLED=0 GOOS="$target" GOARCH=amd64 go vet ./internal/frontend ./pkg/tui ./cmd/ez-agent
    done

# Real shipped-process AgentHub journey, using the standard isolated daemon fixture.
test-agenthub-e2e: build
    python3 tests/run_tests.py tests/suites/test_agenthub_process.py --no-llm

# Compile and vet AgentHub on each supported or explicitly fail-closed target.
vet-agenthub-platforms:
    #!/bin/sh
    set -e
    for target in windows darwin freebsd; do
        echo "== AgentHub ${target}/amd64 compile =="
        CGO_ENABLED=0 GOOS="$target" GOARCH=amd64 go build ./cmd/ez-agent-hub ./internal/agenthub/...
        echo "== AgentHub ${target}/amd64 vet =="
        CGO_ENABLED=0 GOOS="$target" GOARCH=amd64 go vet ./cmd/ez-agent-hub ./internal/agenthub/...
    done

# Start the service daemon in the foreground
serve: build
    {{ build_dir }}/ez-agent-service

# Stop a running service daemon
stop: build
    {{ build_dir }}/ez-agent --stop

# Run with a local provider (assumes provider is running)
run: build
    {{ build_dir }}/ez-agent -p "Hello"

# Fast Ruby library tests against source; requires an existing bundled interpreter.
test-ruby-source:
    ./ez-agent-ruby/tests/ruby-source-test.sh

# Spot tests for one root-module package or package pattern; ARGS are Go flags.
test-go-package PACKAGE *ARGS:
    #!/bin/sh
    set -e
    package=$1
    shift
    go test -timeout="${EZ_AGENT_GO_TEST_TIMEOUT:-30m}" "$@" "$package"

# Independent literal checks. Go package tests consume the same checked-in data.
test-agenthub-fixtures:
    #!/bin/sh
    set -e
    ruby=${EZ_AGENT_RUBY:-./build/ez-agent-ruby}
    for checker in internal/agenthub/testdata/trust/check-*.rb internal/agenthub/testdata/private/*check*.rb internal/agenthub/testdata/carrier/check.rb; do
        # check-public.rb loads verify-records.rb before checking exclusion.
        case "$checker" in */check-records.rb) continue ;; esac
        "$ruby" "$checker"
    done
    "$ruby" internal/agenthub/testdata/trust/check-keys.rb --self-test

# Check local Markdown links without running product tests or building binaries.
docs-check:
    #!/bin/sh
    set -e
    "${EZ_AGENT_RUBY:-./build/ez-agent-ruby}" scripts/check-doc-links.rb

# Default Go tests exclude AgentHub-owned packages. ARGS are Go test flags.
# EZ_AGENT_GO_TEST_TIMEOUT bounds each package, not production deadlines.
test-go *ARGS:
    #!/bin/sh
    set -e
    packages=$(go list ./...)
    packages=$(printf '%s\n' "$packages" | grep -Ev '{{agenthub_go_pattern}}')
    echo "== Go tests (excluding AgentHub) =="
    go test -timeout="${EZ_AGENT_GO_TEST_TIMEOUT:-30m}" "$@" $packages
    (cd ez-agent-ruby && go test -timeout="${EZ_AGENT_GO_TEST_TIMEOUT:-30m}" "$@" ./...)

# AgentHub-owned Go tests, for Hub changes and affected shared dependencies.
test-go-agenthub *ARGS:
    #!/bin/sh
    set -e
    packages=$(go list ./...)
    packages=$(printf '%s\n' "$packages" | grep -E '{{agenthub_go_pattern}}')
    echo "== AgentHub Go tests =="
    go test -timeout="${EZ_AGENT_GO_TEST_TIMEOUT:-30m}" "$@" $packages

# Explicit complete Go coverage, including AgentHub and the nested module.
test-go-all *ARGS:
    #!/bin/sh
    set -e
    echo "== Go tests =="
    go test -timeout="${EZ_AGENT_GO_TEST_TIMEOUT:-30m}" "$@" ./...
    (cd ez-agent-ruby && go test -timeout="${EZ_AGENT_GO_TEST_TIMEOUT:-30m}" "$@" ./...)

# Fresh default Go evidence, excluding AgentHub; build caching is unchanged.
test-go-fresh:
    just test-go -count=1

# Run the complete suite. A Python selector skips the language-wide stages.
test *ARGS: build
    #!/bin/sh
    set -e
    if [ "$#" -gt 0 ] && [ "${1#-}" = "$1" ]; then
        exec python3 tests/run_tests.py "$@"
    fi
    just test-go-all
    echo "== Ruby unit tests =="
    ./ez-agent-ruby/tests/ruby-source-test.sh
    just test-agenthub-fixtures
    echo "== Python integration tests =="
    exec python3 tests/run_tests.py "$@"

# Run the development suite: non-AgentHub Go, Ruby, and quick offline Python.
test-quick *ARGS: build
    #!/bin/sh
    set -e
    if [ "$#" -gt 0 ] && [ "${1#-}" = "$1" ]; then
        exec python3 tests/run_tests.py "$@" --no-llm -m quick
    fi
    just test-go
    echo "== Ruby unit tests =="
    ./ez-agent-ruby/tests/ruby-source-test.sh
    echo "== Python integration tests =="
    exec python3 tests/run_tests.py "$@" --no-llm -m quick

# Run every provider-free test. A Python selector skips the language-wide stages.
test-full-offline *ARGS: build
    #!/bin/sh
    set -e
    if [ "$#" -gt 0 ] && [ "${1#-}" = "$1" ]; then
        exec python3 tests/run_tests.py "$@" --no-llm -m "not llm"
    fi
    just test-go-all
    echo "== Ruby unit tests =="
    ./ez-agent-ruby/tests/ruby-source-test.sh
    just test-agenthub-fixtures
    echo "== Python integration tests =="
    exec python3 tests/run_tests.py "$@" --no-llm -m "not llm"

# Run provider-dependent Python tests. Live OpenAI still requires explicit selection.
test-llm *ARGS: build
    #!/bin/sh
    exec python3 tests/run_tests.py "$@" -m llm

# Opt-in real local + AF_UNIX relay Muse Spark responses. Requires existing artifacts/login.
test-remote-muse-live *ARGS:
    #!/bin/sh
    exec python3 tests/run_tests.py remote-muse-live --live-muse -s "$@"

# Clean up test temp directories
test-clean:
    rm -rf /tmp/ez-test-runtime.* /tmp/ez-test-state.*