justfile

set positional-arguments

build_dir := "build"

# One UTC build time per invocation; extra linker flags can override release identity.
build_date := `date -u +%Y-%m-%dT%H:%M:%SZ`
# Go's VCS stamp does not detect git worktrees (.git is a file there), so take
# the revision from git; a trailing * marks a modified tree, as vcs.modified does.
git_commit := `c=$(git rev-parse HEAD 2>/dev/null) || exit 0; [ -z "$(git status --porcelain 2>/dev/null)" ] || c="$c*"; printf %s "$c"`
ldflags := ""
build_ldflags := "-X git.cafe/ezterry/ez-agent/pkg/buildinfo.BuildDate=" + build_date + (if git_commit == "" { "" } else { " -X git.cafe/ezterry/ez-agent/pkg/buildinfo.GitCommit=" + git_commit }) + " " + ldflags

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

# Inputs at which self-contained AgentHub test targets last passed; cmd/teststamp writes it.
stamp_file := "tests/agenthub-stamps.txt"

# List all recipes
default:
    @just --list

# Build all binaries into build/
build:
    @mkdir -p {{ build_dir }}
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ez-agent ./cmd/ez-agent
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ez-agent-service ./cmd/ez-agent-service
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ez-agent-hub ./cmd/ez-agent-hub
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ez-agent-builtin ./cmd/ez-agent-builtin
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ezagent-muse-code-bridge ./cmd/ezagent-muse-code-bridge
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ezagent-claude-code-bridge ./cmd/ezagent-claude-code-bridge
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ez-agent-hello ./cmd/ez-agent-hello
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ez-agent-configurator ./cmd/ez-agent-configurator
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ez-agent-webfetch ./cmd/ez-agent-webfetch
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -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 -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ezagent-muse-code-bridge ./cmd/ezagent-muse-code-bridge

# Build only the Claude subscription bridge
build-claude-bridge:
    @mkdir -p {{ build_dir }}
    CGO_ENABLED=0 go build -ldflags '{{ build_ldflags }}' -o {{ build_dir }}/ezagent-claude-code-bridge ./cmd/ezagent-claude-code-bridge

# Explicit installed-native discovery check, using the built bridge without inference
test-claude-catalog-live:
    EZAGENT_CLAUDE_CATALOG_LIVE=1 go test -timeout=30s ./internal/claudebridge -run '^TestClaudeCatalogInstalled$' -v

# Installed Muse batching against a loopback fake API; private roots, no login or inference
test-muse-batch-native binary:
    EZ_AGENT_MUSE_NATIVE_BINARY="$1" go test -timeout=35s ./internal/musebridge -run '^TestInstalledMuseBatchContract$' -v

# Installed native transcript loading only; private login-free roots, no inference
test-claude-restore-native:
    EZAGENT_CLAUDE_RESTORE_NATIVE=1 go test -timeout=30s ./internal/claudebridge -run '^TestClaudeRestoreInstalled$' -v

# Installed native attribution against a local fake API; private roots, no login or inference
test-claude-entrypoint-native:
    EZAGENT_CLAUDE_ENTRYPOINT_NATIVE=1 go test -timeout=30s ./internal/claudebridge -run '^TestClaudeEntrypointInstalled$' -v

# Installed native MCP session-ID echo and mcp_reconnect against the local shim; private roots, no login or inference
test-claude-mcp-native:
    EZAGENT_CLAUDE_MCP_NATIVE=1 go test -timeout=60s ./internal/claudebridge -run '^TestClaudeMCPSessionInstalled$' -v

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

# Test distribution packaging and setup in an isolated tree with the real configurator
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
# gofmt on tracked Go files and native go vet of both modules. First stage of
# the test aggregates and of `just vet`.
check-go:
    #!/bin/sh
    set -e
    unformatted=$(git ls-files '*.go' | xargs gofmt -l)
    if [ -n "$unformatted" ]; then
        echo "gofmt needed (run gofmt -w):" >&2
        printf '%s\n' "$unformatted" >&2
        exit 1
    fi
    go vet ./...
    (cd ez-agent-ruby && go vet ./...)

# Full static check: check-go plus cross-platform compile and vet. Run at merge review.
vet:
    just check-go
    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"

# check-public.rb runs with this checkout's build/ez-agent-ruby when built,
# ignoring EZ_AGENT_RUBY, and is skipped when its stamp matches.
# 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}
    refresh=
    if [ "${EZ_AGENT_TEST_STAMPS:-}" = refresh ]; then refresh=-refresh; fi
    mkdir -p {{ build_dir }}
    go build -o {{ build_dir }}/teststamp ./cmd/teststamp
    for checker in internal/agenthub/testdata/trust/check-*.rb internal/agenthub/testdata/private/*check*.rb internal/agenthub/testdata/carrier/check.rb; do
        case "$checker" in
            # check-public.rb loads verify-records.rb before checking exclusion.
            */check-records.rb) continue ;;
            */trust/check-public.rb)
                {{ build_dir }}/teststamp ruby -stamps {{ stamp_file }} -ruby "$ruby" -tree internal/agenthub/testdata/trust $refresh "$checker"
                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.
# Self-contained packages whose committed stamp matches are skipped and listed.
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 =="
    just _go-test-stamped "$packages" "$@"

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

# One go test run of PACKAGES. Without ARGS, AgentHub packages whose stamp
# matches are skipped and passes are recorded; EZ_AGENT_TEST_STAMPS=refresh or
# a non-empty EZ_AGENT_HUB_DEEP (read by teststamp) runs all and records.
# Go flags or EZ_AGENT_MAXIMUM_CAPACITY bypass stamps: everything selected runs,
# no pass is recorded, and stamped packages that FAIL are marked failed.
# teststamp applies the same rule to one package when a variable its own Go
# files read by literal name is set.
# The tool is built rather than run with `go run` so go test's exit status passes through.
_go-test-stamped PACKAGES *ARGS:
    #!/bin/sh
    set -e
    packages=$1
    shift
    timeout="${EZ_AGENT_GO_TEST_TIMEOUT:-30m}"
    mkdir -p {{ build_dir }}
    go build -o {{ build_dir }}/teststamp ./cmd/teststamp
    if [ "$#" -gt 0 ] || [ -n "${EZ_AGENT_MAXIMUM_CAPACITY:-}" ]; then
        echo "== Test stamps bypassed (Go test flags or EZ_AGENT_MAXIMUM_CAPACITY): all selected packages run; no pass is recorded; stamped packages that FAIL are marked failed =="
        exec {{ build_dir }}/teststamp go-bypass -stamps {{ stamp_file }} -packages "$packages" -- -timeout="$timeout" "$@"
    fi
    refresh=
    if [ "${EZ_AGENT_TEST_STAMPS:-}" = refresh ]; then refresh=-refresh; fi
    exec {{ build_dir }}/teststamp go-test -stamps {{ stamp_file }} -hub '{{agenthub_go_pattern}}' -timeout="$timeout" $refresh $packages

# Re-run every stamped AgentHub target, ignoring stamps, and record the passes.
test-stamps-refresh:
    EZ_AGENT_TEST_STAMPS=refresh just test-go-agenthub
    EZ_AGENT_TEST_STAMPS=refresh just test-agenthub-fixtures

# EZ_AGENT_HUB_DEEP un-skips slow scenarios; the profile is a superset of the
# default run, so Go passes record stamps.
# Deep AgentHub: all hub Go packages ignoring stamps, every checker, hub Python suites.
test-hub-deep: build
    #!/bin/sh
    set -e
    export EZ_AGENT_HUB_DEEP=1
    EZ_AGENT_TEST_STAMPS=refresh just test-go-agenthub
    EZ_AGENT_TEST_STAMPS=refresh just test-agenthub-fixtures
    echo "== AgentHub Python suites =="
    python3 tests/run_tests.py tests/suites/test_agenthub_deployment.py --no-llm
    python3 tests/run_tests.py tests/suites/test_agenthub_process.py --no-llm

# 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 check-go
    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 check-go
    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 check-go
    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 "$@"

# Opt-in real host-only Claude through the existing AF_UNIX provider relay.
# Requires existing service/bridge artifacts and official native login on the host.
test-remote-claude-live *ARGS:
    #!/bin/sh
    exec python3 tests/run_tests.py remote-claude-live --live-claude -s "$@"

# Opt-in bounded Claude structured-interface checks through the shared MCP proxy.
# Requires Claude on PATH, its existing native subscription setup, and Haiku access.
test-claude-native-live:
    EZAGENT_CLAUDE_LIVE=1 go test ./internal/claudebridge -run '^TestClaudeNativeInterface$' -v -timeout 6m

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

# Opt-in bounded Haiku bridge checks. Requires build/ezagent-claude-code-bridge.
test-claude-bridge-live:
    EZAGENT_CLAUDE_LIVE=1 go test ./internal/claudebridge -run '^TestClaudeBridge(CatalogAndUsage|ErrorMedia)Live$' -v -timeout 3m
    EZAGENT_CLAUDE_LIVE=1 go test ./pkg/service/agent -run '^TestClaudeLoadedAgent(Live|SteerDuringHeldToolLive|AttachmentsLive)$' -v -timeout 5m

# Only the bounded attachment journey, using the existing built Claude bridge.
test-claude-media-live:
    EZAGENT_CLAUDE_LIVE=1 go test ./pkg/service/agent -run '^TestClaudeLoadedAgentAttachmentsLive$' -v -timeout 3m

# Only the daemon/client, builtin and selected native bridge for two-file acceptance.
build-two-file-acceptance family="claude":
    #!/usr/bin/env bash
    set -euo pipefail
    case '{{family}}' in muse|claude) ;; *) echo 'family must be muse or claude' >&2; exit 2;; esac
    mkdir -p {{build_dir}}
    for target in ez-agent ez-agent-service ez-agent-builtin ezagent-{{family}}-code-bridge; do
        CGO_ENABLED=0 go build -o "{{build_dir}}/$target" "./cmd/$target"
    done

# Opt-in ONE two-file prompt, at most two provider requests. Never part of aggregates.
test-native-two-file-live model *ARGS:
    #!/usr/bin/env bash
    set -euo pipefail
    model="$1"
    shift
    case "$model" in muse/*|claude/*) ;; *) echo 'select muse/MODEL or claude/MODEL exactly' >&2; exit 2;; esac
    just build-two-file-acceptance "${model%%/*}"
    exec python3 tests/run_tests.py native-two-file::test_native_two_file_live --live-two-file --model="$model" -s "$@"