blob: cfe046fbb01728ae054f3c1561a78198bce96caf [file]
#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# magpie-python — the Python every Magpie tool, skill step and hook runs on.
#
# magpie-python [python arguments …] e.g. magpie-python -m setup_preflight
#
# Installed once, from a terminal, to ~/.local/bin (on PATH, and read-only to
# the agent's sandbox):
#
# install -m 0755 <this file> ~/.local/bin/magpie-python
#
# In the Magpie repository itself, prek and CI run this file in place
# (`sh plugins/magpie-setup/bin/magpie-python …`), so a fresh clone needs uv and
# `uv python install 3.14`, nothing installed on PATH.
#
# It runs an installed Python 3.14 and nothing else, chosen so that nothing
# the agent's sandbox can write decides what runs (hooks run it outside the
# sandbox, on every tool call):
# - it asks uv for the installed interpreters (`uv python list
# --only-installed`), which only looks: no download, no virtualenv, and
# `--no-config`, so no uv.toml / [tool.uv] from the working directory,
# which for a skill is someone else's repository;
# - it skips every interpreter, and refuses a uv, whose real path lies where
# a sandboxed agent can write: the launch directory and the directories
# above it (a checkout), temporary directories, ~/.cache. The same rule
# as `agent-iso.sh`'s trusted python3 (tools/agent-isolation). Started in
# home or above it, it refuses outright: the sandbox can then write home;
# - it runs the first one left in place, by its real path, so nothing comes
# from uv's cache either.
# Installing an interpreter is `uv python install 3.14`, run by the user.
#
# When uv or a trusted Python 3.14 is missing it exits 2 with what to run. For
# an agent hook, 2 blocks the tool call: a guard that cannot start must not
# let the call through silently.
MAGPIE_PYTHON_LAUNCHER_VERSION=5
required=3.14
if [ "${1:-}" = "--launcher-version" ]; then
echo "$MAGPIE_PYTHON_LAUNCHER_VERSION"
exit 0
fi
# The launch directory, home and every sandbox-writable directory, resolved
# once, in one subshell: the launcher sits in front of every hook call, and a
# subshell per directory per check would cost more than uv and Python together.
# Line 1 is the launch directory, line 2 home, the rest the writable places
# (each resolved, or as given when it does not exist).
_resolved=$(
pwd -P
for dir in "${HOME%/}" "${TMPDIR:-/tmp}" /tmp /private/tmp /var/tmp /private/var/tmp /var/folders /private/var/folders "${HOME%/}/.cache" "${XDG_CACHE_HOME:-}"; do
[ -n "$dir" ] || continue
if cd -P -- "${dir%/}" 2>/dev/null; then pwd -P; else printf '%s\n' "${dir%/}"; fi
done
)
nl='
'
here=${_resolved%%"$nl"*}
_rest=${_resolved#*"$nl"}
home=${_rest%%"$nl"*}
writable=${_rest#*"$nl"}
# The sandbox can write the directory a session starts in. Started in home, or
# above it, that is all of home, ~/.local/bin included, so no interpreter under
# it can be trusted and the exemption below for home's own directories would
# pass a planted one. Refuse rather than guess.
case $here in
/ | "$home") _home_writable=1 ;;
*) case "$home/" in "$here"/*) _home_writable=1 ;; *) _home_writable= ;; esac ;;
esac
if [ -n "$_home_writable" ] && [ -n "$home" ]; then
echo "magpie-python: started in $here, which holds your home directory: an agent sandboxed there can write every Python under home." >&2
echo "Start the session in a project directory instead, then retry." >&2
exit 2
fi
# True when real path $1 lies where a sandboxed agent can write. No subshells:
# string matching only, against the directories resolved above.
_sandbox_writable() {
real=$1
# Any directory above the launch directory may be a checkout, up to the
# root, apart from home itself, its parents, and system install locations.
up=$here
while [ -n "$up" ] && [ "$up" != / ]; do
case $up in
"$home" | /usr | /usr/local | /opt | /opt/homebrew | /opt/local | /nix | /nix/store | /bin | /sbin | /Library | /System | /Applications) ;;
*)
case "$home/" in
"$up"/*) ;;
*) case $real in "$up"/*) return 0 ;; esac ;;
esac
;;
esac
up=${up%/*}
done
# A newline-split `for`, not a here-document: sh writes a here-document to a
# temporary file, and where it cannot (a sandbox, a read-only tmp) the loop
# would see no directories and pass everything.
_ifs=$IFS
IFS=$nl
for dir in $writable; do
[ -n "$dir" ] || continue
case $real in "$dir" | "$dir"/*)
IFS=$_ifs
return 0
;;
esac
done
IFS=$_ifs
return 1
}
# No pathname expansion: the lists below are split on newlines, never globbed.
set -f
# `-m` and `-c` put the working directory first on sys.path, ahead of
# PYTHONPATH, and for a skill that is someone else's repository: a package of
# the same name planted there would run instead. So those starts get `-P`
# (no unsafe path). A script keeps its own directory first, which is where its
# siblings are; `-P` is a flag, not PYTHONSAFEPATH, so no child inherits it.
safe=
for arg in "$@"; do
case $arg in
-m* | -c* | -[A-Za-z]*m | -[A-Za-z]*c) safe=-P; break ;;
--) break ;;
-*) ;;
*) break ;;
esac
done
uv=$(command -v uv 2>/dev/null) && uv=$(realpath "$uv" 2>/dev/null) || uv=
if [ -z "$uv" ]; then
echo "magpie-python: Magpie runs on uv, which is not on PATH." >&2
echo "Install uv (https://docs.astral.sh/uv/getting-started/installation/), then retry." >&2
exit 2
fi
if _sandbox_writable "$uv"; then
echo "magpie-python: the uv on PATH ($uv) is in a directory the agent's sandbox can write." >&2
echo "Put a uv installed outside it (e.g. ~/.local/bin/uv) first on PATH, then retry." >&2
exit 2
fi
# Fast path: uv's own first choice (no virtualenv, no config), when it is
# trusted. Only when it is not does the launcher walk every installed one.
# `--color never` on both: with FORCE_COLOR set (CI sets it) uv wraps the
# listed paths in colour codes, and no such path exists.
first=$("$uv" python find --no-config --color never --system "$required" 2>/dev/null) &&
first=$(realpath "$first" 2>/dev/null) &&
! _sandbox_writable "$first" &&
exec "$first" ${safe:+"$safe"} "$@"
skipped=
listed=$("$uv" python list --only-installed --no-config --color never "$required" 2>/dev/null) || listed=
# One "<key> <path>[ -> <link target>]" line per interpreter, best first.
_ifs=$IFS
IFS=$nl
for line in $listed; do
IFS=$_ifs
path=${line#* }
while case $path in " "*) true ;; *) false ;; esac; do path=${path# }; done
path=${path%% -> *}
[ -n "$path" ] || continue
real=$(realpath "$path" 2>/dev/null) || continue
if _sandbox_writable "$real"; then
skipped="$skipped $real"
continue
fi
exec "$real" ${safe:+"$safe"} "$@"
done
IFS=$_ifs
if [ -n "$skipped" ]; then
echo "magpie-python: the only Python $required installed lies where the agent's sandbox can write:$skipped" >&2
else
echo "magpie-python: Magpie needs Python $required, which is not installed." >&2
fi
echo "Run this once in your own terminal (not through the agent), then retry:" >&2
echo " uv python install $required" >&2
exit 2