#!/usr/bin/env bash set -euo pipefail script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) repo_dir=$(cd -- "$script_dir/.." && pwd) image=${AGENT_IMAGE:-local-ai-agent-dev:fedora44} workspace=${WORKSPACE:-$PWD} engine=${CONTAINER_ENGINE:-} command=shell usage() { cat <<'EOF' Usage: scripts/agent-dev [build|shell|run] [--engine docker|podman] [--workspace PATH] [-- COMMAND...] build Build the image from versions.env. shell Start an interactive zsh session (the default). run Start the supplied command in the hardened container. Environment: AGENT_IMAGE, CONTAINER_ENGINE, WORKSPACE, AGENT_CPUS (4), AGENT_MEMORY (8g), and AGENT_PIDS_LIMIT (512). EOF } while (($#)); do case $1 in build|shell|run) command=$1; shift ;; --engine) engine=${2:?missing engine}; shift 2 ;; --workspace) workspace=${2:?missing workspace}; shift 2 ;; --help|-h) usage; exit 0 ;; --) shift; break ;; *) printf 'Unknown argument: %s\n' "$1" >&2; usage >&2; exit 2 ;; esac done if [[ -z $engine ]]; then if command -v podman >/dev/null 2>&1; then engine=podman elif command -v docker >/dev/null 2>&1; then engine=docker else printf 'Neither podman nor docker is available.\n' >&2 exit 127 fi fi case $engine in podman|docker) ;; *) printf 'Unsupported engine: %s\n' "$engine" >&2; exit 2 ;; esac command -v "$engine" >/dev/null 2>&1 || { printf '%s is not installed.\n' "$engine" >&2; exit 127; } if [[ $command == build ]]; then # shellcheck disable=SC1091 source "$repo_dir/versions.env" exec "$engine" build \ --build-arg "FEDORA_IMAGE=$FEDORA_IMAGE" \ --build-arg "BUN_VERSION=$BUN_VERSION" \ --build-arg "DENO_VERSION=$DENO_VERSION" \ --build-arg "CLAUDE_CHANNEL=$CLAUDE_CHANNEL" \ --build-arg "CLAUDE_VERSION=$CLAUDE_VERSION" \ --build-arg "CODEX_VERSION=$CODEX_VERSION" \ --build-arg "PI_PACKAGE=$PI_PACKAGE" \ --build-arg "PI_VERSION=$PI_VERSION" \ --build-arg "MATT_POCOCK_SKILLS_REF=$MATT_POCOCK_SKILLS_REF" \ --build-arg "RTK_VERSION=$RTK_VERSION" \ --build-arg "RUFF_VERSION=$RUFF_VERSION" \ --build-arg "UV_VERSION=$UV_VERSION" \ --tag "$image" --file "$repo_dir/Containerfile" "$repo_dir" fi [[ -d $workspace ]] || { printf 'Workspace is not a directory: %s\n' "$workspace" >&2; exit 2; } workspace=$(cd -- "$workspace" && pwd -P) uid=$(id -u) gid=$(id -g) cpus=${AGENT_CPUS:-4} memory=${AGENT_MEMORY:-8g} pids=${AGENT_PIDS_LIMIT:-512} state_prefix=${AGENT_STATE_PREFIX:-local-ai-agent-dev} volumes=("$state_prefix-codex" "$state_prefix-claude" "$state_prefix-pi" "$state_prefix-rtk") for volume in "${volumes[@]}"; do if ! "$engine" volume inspect "$volume" >/dev/null 2>&1; then "$engine" volume create "$volume" >/dev/null fi if [[ $engine == docker ]]; then # Docker has no Podman-style :U option. Initialize only the scoped state # volume so arbitrary host UIDs can write it as the unprivileged agent. "$engine" run --rm --user 0 --volume "$volume:/state" \ registry.fedoraproject.org/fedora:44 chown -R "$uid:$gid" /state fi done args=(run --rm --interactive --tty --init --user "$uid:$gid" --workdir /workspace --read-only --cap-drop ALL --security-opt no-new-privileges:true --pids-limit "$pids" --memory "$memory" --cpus "$cpus" --tmpfs /tmp:rw,noexec,nosuid,size=2g) if [[ $engine == podman ]]; then # :U corrects ownership inside Podman's keep-id user namespace. It applies # only to managed named volumes, never to the user-selected workspace. args+=(--userns keep-id --volume "${volumes[0]}:/state/codex:U" --volume "${volumes[1]}:/state/claude:U" --volume "${volumes[2]}:/home/agent/.pi:U" --volume "${volumes[3]}:/state/rtk:U" --volume "$workspace:/workspace:Z,rw") else args+=(--volume "${volumes[0]}:/state/codex" --volume "${volumes[1]}:/state/claude" --volume "${volumes[2]}:/home/agent/.pi" --volume "${volumes[3]}:/state/rtk" --mount "type=bind,src=$workspace,dst=/workspace") fi if [[ $command == run ]]; then (($#)) || { printf 'run requires a command after --.\n' >&2; exit 2; } args+=("$image" "$@") else (($# == 0)) || { printf 'shell does not accept a command; use run -- COMMAND.\n' >&2; exit 2; } args+=("$image" zsh) fi exec "$engine" "${args[@]}"