# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

CRATES="
	allocator-api2@0.2.21
	approx@0.5.1
	arrayvec@0.7.6
	asn1-rs-derive@0.6.0
	asn1-rs-impl@0.2.0
	asn1-rs@0.7.2
	autocfg@1.5.1
	base64@0.23.1
	bit-vec@0.9.1
	bitflags@2.13.2
	block-buffer@0.10.4
	by_address@1.2.1
	castaway@0.2.4
	cc@1.4.7
	cfg-if@1.0.4
	cfg_aliases@0.2.1
	compact_str@0.9.1
	convert_case@0.10.0
	cpufeatures@0.2.17
	crossterm@0.29.0
	crossterm_winapi@0.9.1
	crypto-common@0.1.7
	darling@0.24.1
	darling_core@0.24.1
	darling_macro@0.24.1
	data-encoding@2.11.1
	der-parser@10.0.0
	deranged@0.5.9
	derive_more-impl@2.1.1
	derive_more@2.1.1
	digest@0.10.7
	displaydoc@0.2.7
	document-features@0.2.12
	either@1.19.0
	enumflags2@0.7.12
	enumflags2_derive@0.7.12
	equivalent@1.0.2
	errno@0.3.14
	find-msvc-tools@0.1.13
	foldhash@0.2.0
	generic-array@0.14.7
	getrandom@0.2.17
	hashbrown@0.16.1
	hashbrown@0.17.1
	heck@0.5.0
	ident_case@1.0.1
	indexmap@2.13.0
	indoc@2.0.8
	instability@0.3.14
	itertools@0.14.0
	itoa@1.0.17
	kasuari@0.4.13
	landlock@0.4.4
	lazy_static@1.5.1
	lexopt@0.3.2
	libc@0.2.191
	libm@0.2.16
	line-clipping@0.3.8
	linux-raw-sys@0.12.1
	litrs@1.0.1
	lock_api@0.4.14
	log@0.4.35
	lru@0.18.5
	memchr@2.8.0
	minimal-lexical@0.2.1
	mio@1.2.4
	nix@0.29.0
	nom@7.1.3
	num-bigint@0.4.8
	num-conv@0.2.2
	num-integer@0.1.47
	num-traits@0.2.19
	num_threads@0.1.8
	oid-registry@0.8.1
	once_cell@1.21.4
	palette@0.7.7
	palette_derive@0.7.7
	palette_math@0.7.7
	parking_lot@0.12.5
	parking_lot_core@0.9.12
	pem@4.0.0
	portable-atomic@1.15.0
	powerfmt@0.2.1
	proc-macro2@1.0.106
	quote@1.0.44
	ratatui-core@0.1.2
	ratatui-crossterm@0.1.2
	ratatui-widgets@0.3.2
	ratatui@0.30.2
	rcgen@0.14.10
	redox_syscall@0.5.18
	ring@0.17.14
	rustc_version@0.4.1
	rusticata-macros@4.1.0
	rustix@1.1.6
	rustls-pki-types@1.15.1
	rustls-webpki@0.103.16
	rustls@0.23.46
	rustversion@1.0.23
	ryu@1.0.23
	scopeguard@1.2.0
	seccompiler@0.5.0
	semver@1.0.28
	serde@1.0.228
	serde_core@1.0.228
	serde_derive@1.0.228
	serde_json@1.0.149
	serde_spanned@0.6.9
	sha2@0.10.9
	shlex@2.0.1
	signal-hook-mio@0.2.5
	signal-hook-registry@1.4.8
	signal-hook@0.3.18
	smallvec@1.16.4
	static_assertions@1.1.0
	strsim@0.11.1
	strum@0.28.0
	strum_macros@0.28.0
	subtle@2.6.1
	syn@2.0.117
	syn@3.0.9
	synstructure@0.13.2
	thiserror-impl@2.0.18
	thiserror@2.0.18
	time-core@0.1.9
	time-macros@0.2.32
	time@0.3.55
	toml@0.8.23
	toml_datetime@0.6.11
	toml_edit@0.22.27
	toml_write@0.1.2
	typenum@1.20.1
	unicode-ident@1.0.24
	unicode-segmentation@1.13.3
	unicode-truncate@2.0.1
	unicode-width@0.2.2
	untrusted@0.9.0
	version_check@0.9.5
	vt100@0.16.2
	vte@0.15.0
	wasi@0.11.1+wasi-snapshot-preview1
	webpki-roots@0.26.11
	webpki-roots@1.0.10
	winapi-i686-pc-windows-gnu@0.4.0
	winapi-x86_64-pc-windows-gnu@0.4.0
	winapi@0.3.9
	windows-link@0.2.1
	windows-sys@0.52.0
	windows-sys@0.61.2
	windows-targets@0.52.6
	windows_aarch64_gnullvm@0.52.6
	windows_aarch64_msvc@0.52.6
	windows_i686_gnu@0.52.6
	windows_i686_gnullvm@0.52.6
	windows_i686_msvc@0.52.6
	windows_x86_64_gnu@0.52.6
	windows_x86_64_gnullvm@0.52.6
	windows_x86_64_msvc@0.52.6
	winnow@0.7.14
	x509-parser@0.18.1
	yasna@0.6.0
	zeroize@1.9.0
	zmij@1.0.21
"

# Upstream pins channel 1.97.1 in rust-toolchain.toml; the crate is edition 2024.
RUST_MIN_VER="1.97.1"

inherit cargo desktop xdg-utils

DESCRIPTION="Sandbox for AI coding agents (bubblewrap on Linux, sandbox-exec on macOS)"
HOMEPAGE="https://github.com/akitaonrails/ai-jail"
SRC_URI="
	https://github.com/akitaonrails/ai-jail/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	${CARGO_CRATE_URIS}
"

# License for the package itself
LICENSE="GPL-3"
# Dependent crate licenses
LICENSE+=" Apache-2.0 BSD CDLA-Permissive-2.0 ISC MIT Unicode-3.0 ZLIB"
SLOT="0"
KEYWORDS="~amd64"
IUSE="chromium-launcher"

# bwrap is the Linux sandbox backend; ai-jail is a thin wrapper around it and is
# useless without it, so this is a hard runtime dependency, not an optional one.
#
# The block against sys-apps/ai-jail-bin is declared on that package alone --
# see the reasoning there.  One side is enough, and repeating it here would only
# give portage a second edge to reason about.
RDEPEND="
	sys-apps/bubblewrap
	chromium-launcher? ( www-client/chromium )
"

DOCS=( README.md docs )

# Upstream's desktop launcher runs `ai-jail --browser=soft chromium` and nothing
# else, which on Linux leaves the sandbox with an unshared network namespace and
# no display socket -- a browser that cannot open a window or load a page.  The
# patch grants both explicitly and picks --display or --x11 from the session in
# use.  Named without a version so a bump cannot orphan it: the autoupdate
# applier renames ebuilds, never files/.
PATCHES=( "${FILESDIR}"/${PN}-chromium-launcher-caps.patch )

# Zeo's jail (Zeo next proposal §6.54) needs two features the fork carries and
# upstream has not released: whole-sandbox cgroup limits (--memory,
# --max-tasks, --cpu-quota) and --state-root (the sandbox's $HOME, /tmp and
# Claude config on disk). --cpus and --rlimit are upstream's since 3.6.0; the
# cgroup limits build on them. Both are opt-in flags: a launch that names
# neither behaves as upstream 3.7.1. Rebased from the fork's zeo/ebuild-371;
# drop these when a release carries them. Named without a version for the
# same reason as the launcher patch.
PATCHES+=(
	"${FILESDIR}"/${PN}-resource-limits.patch
	"${FILESDIR}"/${PN}-state-root.patch
)

# --mask-host (Zeo next proposal §6.63): synthetic /proc/cpuinfo, meminfo,
# stat, loadavg, a live uptime and /sys/devices/system/cpu sized to the limits
# above, only the sandbox's own cgroup (--unshare-cgroup), no DMI, a machine id
# kept under --state-root. Opt-in: a launch without the flag behaves as upstream.
# From the fork's feat/mask-host plus its wiring to --cpus and --state-root, so
# it must follow the two patches above. Upstream PR only after local testing.
PATCHES+=( "${FILESDIR}"/${PN}-mask-host.patch )

# --mask-host's second layer (Zeo next proposal §6.63, docs/HOST-VIEW-EXTRA-
# LAYER.md §4.2): kernel build strings, os-release, swap, disks, crypttab,
# NUMA, modules, the boot id and a time namespace; --overlay-system for the
# system trees. From the fork's feat/host-view-extra on top of the patch
# above; a launch without --mask-host only loses AI_JAIL_QUIET in the agent.
PATCHES+=( "${FILESDIR}"/${PN}-host-view-extra.patch )

# --syscall-view (docs/HOST-VIEW-EXTRA-LAYER.md §4.3): uname(2) and sysinfo(2)
# answered by ai-jail through a seccomp user-notify filter, under --mask-host.
# From the fork's feat/syscall-view on top of the patch above; opt-in.
PATCHES+=( "${FILESDIR}"/${PN}-syscall-view.patch )

# --minimal-etc (with --mask-host): /etc as a read-only tmpfs holding only an
# allowlist of the host's entries programs read at run time; the rest of /etc
# (hostname, fstab, ssh/, every service's configuration) is absent. From the
# fork's feat/minimal-etc on top of the patch above; opt-in.
PATCHES+=( "${FILESDIR}"/${PN}-minimal-etc.patch )

# --hostname: the sandbox's host name instead of ai-sandbox, one per project
# for a caller that passes it (Zeo's hidden-host jails); the sandbox also gets
# an /etc/hostname of its own, so the host's name stops showing there. From
# the fork's feat/hostname on top of the patch above; opt-in.
PATCHES+=( "${FILESDIR}"/${PN}-hostname.patch )

# Explicit ports in --allow-host (Zeo next §6.9): HOST:PORT opens one port, a
# bare host keeps 443 and 80 -- upstream opened every port of a listed host.
# From the fork's feat/allow-host-ports; candidate for an upstream PR.
PATCHES+=( "${FILESDIR}"/${PN}-host-ports.patch )

# --secret into HTTPS (Zeo next §6.53): a per-launch CA, in memory only and
# name-constrained to the bound hosts, so the egress proxy can put the real
# value into HTTPS requests to them; several hosts per key, Basic auth, chunked
# bodies. Adds rcgen (and the crates above it in CRATES). From the fork's
# feat/secret-tls-inject on top of the patch above.
PATCHES+=( "${FILESDIR}"/${PN}-secret-https.patch )

# SSH through filtered egress (Zeo next §6.9): an allowlisted HOST:22 gets a
# ProxyCommand through the egress proxy, GIT_SSH_COMMAND and the user's
# known_hosts filtered to those hosts. From the fork's feat/ssh-proxy-connect
# on top of the patch above.
PATCHES+=( "${FILESDIR}"/${PN}-ssh-proxy.patch )

# Scoped secret substitution (2026-11-27 security review of Zeo 0.31.0): the real
# value only in Authorization, x-api-key and named query parameters, a request
# carrying the placeholder anywhere else refused, the response head scrubbed, the
# Host checked against the CONNECT origin, interception on 443 only. From the
# fork's fix/secret-scope on top of the patch above.
PATCHES+=( "${FILESDIR}"/${PN}-secret-scope.patch )

src_prepare() {
	default

	# [profile.release] sets strip = true, which hands Portage an already
	# stripped binary: that trips the pre-stripped QA check and makes
	# FEATURES=splitdebug produce empty debug objects. Let Portage strip.
	grep -qF 'strip = true' Cargo.toml \
		|| die "Cargo.toml no longer sets strip = true; drop this sed"
	sed -i '/^strip = true$/d' Cargo.toml || die
}

src_test() {
	# Unit tests only. tests/sandbox_escape.rs (and the other integration
	# tests) need working unprivileged user namespaces, which the Portage
	# sandbox does not provide; upstream's own PKGBUILD skips them for the
	# same reason.
	cargo_src_test --frozen --bin ai-jail
}

src_install() {
	dobin target/release/${PN}
	einstalldocs

	if use chromium-launcher; then
		dobin dist/desktop/${PN}-chromium
		domenu dist/desktop/${PN}-chromium.desktop
	fi
}

pkg_postinst() {
	use chromium-launcher && xdg_desktop_database_update
}

pkg_postrm() {
	use chromium-launcher && xdg_desktop_database_update
}