# 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

# kekkai is ai-jail with a patch series on top: the source is upstream's tagged
# release, and everything kekkai adds is in PATCHES below. The series lives in
# the zeo-workspace kekkai repository (patches/kekkai-${PV}/), which proves it
# against this file with scripts/check-sync.sh.
MY_PN="ai-jail"
MY_P="${MY_PN}-${PV}"

DESCRIPTION="Sandbox for AI coding agents: ai-jail with the kekkai patch series"
HOMEPAGE="https://github.com/akitaonrails/ai-jail"
SRC_URI="
	https://github.com/akitaonrails/ai-jail/archive/refs/tags/v${PV}.tar.gz -> ${MY_P}.tar.gz
	${CARGO_CRATE_URIS}
"
S="${WORKDIR}/${MY_P}"

# 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="+ai-jail-symlink chromium-launcher"

# bwrap is the Linux sandbox backend; without it kekkai cannot run anything.
#
# kekkai installs /usr/bin/kekkai and, with USE ai-jail-symlink, /usr/bin/ai-jail
# -- the path both ai-jail packages install -- so it blocks both. Without the
# symlink the files would not collide, but two sandboxes answering to two names
# with different flags is exactly the confusion the rename ends.
RDEPEND="
	!sys-apps/ai-jail
	!sys-apps/ai-jail-bin
	sys-apps/bubblewrap
	chromium-launcher? ( www-client/chromium )
"

DOCS=( README.md docs )

# The packaging patch: upstream's desktop launcher runs the browser with no
# network and no display socket (see the patch header).
PATCHES=( "${FILESDIR}"/ai-jail-chromium-launcher-caps.patch )

# Zeo's jail (Zeo next proposal §6.54): whole-sandbox cgroup limits, then
# --state-root.
PATCHES+=(
	"${FILESDIR}"/ai-jail-resource-limits.patch
	"${FILESDIR}"/ai-jail-state-root.patch
)

# Hiding the host (§6.63): --mask-host and its layers, each on the one above:
# the identity layer and --overlay-system, --syscall-view, --minimal-etc,
# --hostname.
PATCHES+=(
	"${FILESDIR}"/ai-jail-mask-host.patch
	"${FILESDIR}"/ai-jail-host-view-extra.patch
	"${FILESDIR}"/ai-jail-syscall-view.patch
	"${FILESDIR}"/ai-jail-minimal-etc.patch
	"${FILESDIR}"/ai-jail-hostname.patch
)

# Egress (§6.9, §6.53, the 2026-11-27 review): explicit ports in --allow-host,
# --secret into HTTPS through a per-launch CA (adds rcgen to CRATES), SSH
# through filtered egress, scoped secret substitution.
PATCHES+=(
	"${FILESDIR}"/ai-jail-host-ports.patch
	"${FILESDIR}"/ai-jail-secret-https.patch
	"${FILESDIR}"/ai-jail-ssh-proxy.patch
	"${FILESDIR}"/ai-jail-secret-scope.patch
)

# Maps: a read-only --map nested directly inside a --rw-map stays read-only
# instead of the writable parent being dropped; deeper, missing or symlinked
# children still refuse the parent. Written against ai-jail (an upstream
# candidate). -r1 also adds a trust-store test to ai-jail-secret-https.patch.
PATCHES+=( "${FILESDIR}"/ai-jail-nested-ro-maps.patch )

# kekkai's own: under --mask-host, /sys as a tmpfs with an allow-list instead of
# the host's whole sysfs, /proc/{modules,devices,misc} emptied and /proc/driver
# hidden, and --kernel-release. Written against ai-jail (an upstream candidate),
# so it comes before the rename.
PATCHES+=( "${FILESDIR}"/kekkai-sys-allowlist.patch )

# The rename, last: binary and package kekkai, the trusted store and data
# directory under the new name with the ai-jail ones still read, KEKKAI_QUIET
# before AI_JAIL_QUIET, the kekkai-chromium launcher.
PATCHES+=( "${FILESDIR}"/kekkai-rename.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 ${PN}
}

src_install() {
	dobin target/release/${PN}
	# One release of grace for callers that still run ai-jail (Zeo before it
	# learnt the new name, scripts, desktop entries): same binary, same flags.
	use ai-jail-symlink && dosym ${PN} /usr/bin/${MY_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

	if use ai-jail-symlink; then
		elog "kekkai is ai-jail renamed. /usr/bin/ai-jail is a symlink to kekkai for"
		elog "this release only: move callers to 'kekkai' (USE=-ai-jail-symlink drops it)."
	fi
	elog "An existing ~/.config/ai-jail store and ~/.local/share/ai-jail data directory"
	elog "are still used, with a deprecation warning, until moved to the kekkai names."
}

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