Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

206 KB
Raw Blame
#!/usr/bin/env bash
set -euo pipefail

# Fixtures model private, non-group-writable Pixel release custody regardless
# of the developer's shell umask. Keep the production permission guard strict.
umask 077

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=installers/lib/pixel-host-install.sh
source "$ROOT/installers/lib/pixel-host-install.sh"

CONFIGURE_TEST_PATH="$PATH"
PASS=0
FAIL=0
pass() { PASS=$((PASS + 1)); printf 'PASS: %s\n' "$1"; }
fail() { FAIL=$((FAIL + 1)); printf 'FAIL: %s\n' "$1" >&2; }
check() { if "$@"; then pass "$*"; else fail "$*"; fi; }

TEST_ROOT="$(mktemp -d)"
cleanup() {
    case "$TEST_ROOT" in /tmp/*|/var/tmp/*) rm -rf -- "$TEST_ROOT" ;; esac
}
trap cleanup EXIT

owner="$(id -un)"
ods_sudo_available() { return 1; }
ai_bad() { :; }
ai_ok() { :; }
ai() { :; }

# Dependency selection is isolated from the host package manager. The real
# Ubuntu 26.04 package/group transition is also exercised in a clean container.
for prereq_case in present ubuntu24 ubuntu26 debian12 python both unsupported \
    parse_failure no_helper wrong_manager install_failure still_missing dry_run before_groups; do
    if (
        have_sg=false; have_python=true; install_calls=''; mutations=0
        os_id=ubuntu; os_version=26.04; expected='util-linux-extra'; expected_status=0
        PKG_MANAGER=apt; ENABLE_PIXEL_RUNTIME=true; DRY_RUN=false
        case "$prereq_case" in
            present) have_sg=true; expected='' ;;
            ubuntu24) os_version=24.04; expected=login ;;
            debian12) os_id=debian; os_version=12; expected=login ;;
            python) have_sg=true; have_python=false; expected=python3 ;;
            both) have_python=false; expected='util-linux-extra python3' ;;
            unsupported) os_id=fedora; expected=''; expected_status=1 ;;
            parse_failure|no_helper|wrong_manager|before_groups) expected=''; expected_status=1 ;;
            install_failure|still_missing) expected_status=1 ;;
            dry_run) expected='' ;;
        esac
        command() {
            if [[ "$#" == 2 && "$1" == -v && "$2" == sg ]]; then "$have_sg"
            elif [[ "$#" == 2 && "$1" == -v && "$2" == python3 ]]; then "$have_python"
            else builtin command "$@"; fi
        }
        _ods_pixel_parse_os_release() {
            [[ "$prereq_case" != parse_failure ]] || return 1
            _ODS_PIXEL_OS_ID="$os_id"; _ODS_PIXEL_OS_VERSION_ID="$os_version"
        }
        pkg_install() {
            install_calls="$*"
            [[ "$prereq_case" != install_failure ]] || return 1
            if [[ "$prereq_case" != still_missing ]]; then have_sg=true; have_python=true; fi
        }
        case "$prereq_case" in
            no_helper|before_groups) unset -f pkg_install ;;
            wrong_manager) PKG_MANAGER=dnf ;;
        esac
        status=0
        if [[ "$prereq_case" == dry_run || "$prereq_case" == before_groups ]]; then
            [[ "$prereq_case" != dry_run ]] || DRY_RUN=true
            ods_sudo_available() { return 0; }
            ods_pixel_install_owner() { printf '%s\n' "$owner"; }
            ods_sudo() { mutations=$((mutations+1)); return 99; }
            getent() { mutations=$((mutations+1)); return 99; }
            ods_pixel_prepare_runtime_identity || status=$?
        else
            _ods_pixel_ensure_runtime_group_tools || status=$?
        fi
        [[ "$status" == "$expected_status" && "$install_calls" == "$expected" && "$mutations" == 0 ]]
    ); then
        pass "runtime group prerequisites: $prereq_case"
    else
        fail "runtime group prerequisites: $prereq_case"
    fi
done

if (
    ods_sudo_available() { return 0; }
    ods_sudo() { printf '%s\n' 'unexpected redundant sudo' >&2; return 99; }
    [[ "$(ods_pixel_run_as_owner "$owner" "$TEST_ROOT" id -un)" == "$owner" ]]
); then
    pass "current Pixel owner runs directly even when sudo is available"
else
    fail "current Pixel owner redundantly enters sudo pseudo-terminal"
fi
if (
    ods_sudo_available() { return 0; }
    ods_sudo() {
        [[ "$1" == -u && "$2" == ods_other_owner && "$3" == -- ]]
    }
    ods_pixel_run_as_owner ods_other_owner "$TEST_ROOT" true
); then
    pass "different Pixel owner still requires sudo identity switch"
else
    fail "different Pixel owner bypassed the sudo identity switch"
fi
if (
    id() {
        if [[ "$1" == -un ]]; then
            command id -un
        elif [[ "$1" == -nG && "$#" == 1 ]]; then
            printf '%s\n' 'users'
        elif [[ "$1" == -nG && "$2" == "$owner" ]]; then
            printf '%s\n' 'users docker'
        else
            command id "$@"
        fi
    }
    sg() {
        [[ "$1" == docker && "$2" == -c ]] || return 1
        bash -c "$3"
    }
    tricky_arg="space ' \" ; touch $TEST_ROOT/UNEXPECTED"
    actual="$(ods_pixel_run_as_owner "$owner" "$TEST_ROOT" python3 -c \
        'import os,sys; print(sys.argv[1] + "|" + os.environ["HOME"])' "$tricky_arg")"
    [[ "$actual" == "$tricky_arg|$TEST_ROOT" && ! -e "$TEST_ROOT/UNEXPECTED" ]]
); then
    pass "stale docker group refresh preserves argv without a new sudo tty"
else
    fail "stale docker group refresh altered arguments or skipped sg"
fi

mkdir -p "$TEST_ROOT/group-bin"
# Like util-linux sg (Ubuntu 26.04), each call replaces the primary group and
# keeps no earlier one, so PIXEL_TEST_PRIMARY is the only group a command has.
cat > "$TEST_ROOT/group-bin/sg" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
[[ "$2" == -c ]]
[[ "${PIXEL_TEST_DENY_GROUP:-}" != "$1" ]] || exit 73
export PIXEL_TEST_REFRESHED="${PIXEL_TEST_REFRESHED:-}${1} " PIXEL_TEST_PRIMARY="$1"
exec bash -c "$3"
SH
chmod +x "$TEST_ROOT/group-bin/sg"
for group_case in pixel docker both both_pixel_socket existing_pixel_socket current unregistered unsupported \
    denied denied_inner denied_outer; do
    if (
        export PATH="$TEST_ROOT/group-bin:$PATH" PIXEL_TEST_REFRESHED='' PIXEL_TEST_PRIMARY=''
        current='users docker'; account='users docker ods-pixel'; expected='ods-pixel docker |docker'
        case "$group_case" in
            docker) current='users ods-pixel'; expected='docker |docker' ;;
            both) current=users; expected='ods-pixel docker |docker' ;;
            both_pixel_socket)
                current=users; expected='docker ods-pixel |ods-pixel'
                export ODS_PIXEL_OWNER_PRIMARY_GROUP=ods-pixel
                ;;
            existing_pixel_socket)
                current='users ods-pixel'; expected='docker ods-pixel |ods-pixel'
                export ODS_PIXEL_OWNER_PRIMARY_GROUP=ods-pixel
                ;;
            current) current="$account"; expected='|' ;;
            unregistered) account="$current"; expected='|' ;;
            unsupported) current=users; export ODS_PIXEL_OWNER_PRIMARY_GROUP=wheel ;;
            denied) export PIXEL_TEST_DENY_GROUP=ods-pixel ;;
            denied_inner) current=users; export PIXEL_TEST_DENY_GROUP=docker ;;
            denied_outer) current=users; export PIXEL_TEST_DENY_GROUP=ods-pixel ;;
        esac
        id() {
            if [[ "$1" == -un ]]; then command id -un
            elif [[ "$1" == -nG && "$#" == 1 ]]; then printf '%s\n' "$current"
            elif [[ "$1" == -nG && "$2" == "$owner" ]]; then printf '%s\n' "$account"
            else command id "$@"; fi
        }
        ods_sudo() { return 99; }
        tricky_arg="space ' \" ; touch $TEST_ROOT/UNEXPECTED"
        result=0
        actual="$(ods_pixel_run_as_owner "$owner" "$TEST_ROOT" python3 -c \
            'import os,sys; print(os.environ["PIXEL_TEST_REFRESHED"] + "|" + os.environ["PIXEL_TEST_PRIMARY"] + "|" + sys.argv[1] + "|" + os.environ["HOME"])' \
            "$tricky_arg")" || result=$?
        if [[ "$group_case" == denied* ]]; then
            [[ "$result" == 73 && -z "$actual" ]]
        elif [[ "$group_case" == unsupported ]]; then
            [[ "$result" == 1 && -z "$actual" ]]
        else
            [[ "$result" == 0 && "$actual" == "$expected|$tricky_arg|$TEST_ROOT" && ! -e "$TEST_ROOT/UNEXPECTED" ]]
        fi
    ); then pass "owner runtime group refresh: $group_case"
    else fail "owner runtime group refresh: $group_case"; fi
done

# A rerun must not stall on an access coordinator an interrupted uninstall
# left stopped, but a deployment being deactivated keeps it stopped.
mkdir -p "$TEST_ROOT/access-start"
for access_case in stopped-ready stopped-installing active deactivating no-unit start-fails no-socket; do
    if (
        case_root="$TEST_ROOT/access-start/$access_case"
        mkdir -p "$case_root"
        calls="$case_root/calls"
        export ODS_PIXEL_ACCESS_UNIT_PATH="$case_root/ods-pixel-access.service"
        export ODS_PIXEL_ACCESS_SOCKET_PATH="$case_root/control.sock"
        marker_state=ready active=1 start_rc=0 binds=true expected_rc=0
        expected_calls='systemctl enable --now ods-pixel-access.service'
        case "$access_case" in
            stopped-installing) marker_state=installing ;;
            active) active=0; expected_calls='' ;;
            deactivating) marker_state=deactivating; expected_calls='' ;;
            no-unit) expected_calls='' ;;
            start-fails) start_rc=1 binds=false expected_rc=1 ;;
            no-socket) binds=false expected_rc=1 ;;
        esac
        [[ "$access_case" == no-unit ]] || : >"$ODS_PIXEL_ACCESS_UNIT_PATH"
        systemctl() { [[ "$*" == 'is-active --quiet ods-pixel-access.service' ]] || return 99; return "$active"; }
        _ods_pixel_source_transition_state() { printf '%s|%s\n' "$marker_state" 0123456789abcdef0123456789abcdef01234567; }
        ods_sudo_available() { return 0; }
        ods_sudo() {
            printf '%s\n' "$*" >>"$calls"
            if [[ "$binds" == true ]]; then
                python3 -c 'import socket,sys; socket.socket(socket.AF_UNIX).bind(sys.argv[1])' \
                    "$ODS_PIXEL_ACCESS_SOCKET_PATH"
            fi
            return "$start_rc"
        }
        sleep() { :; }
        ai() { :; }
        result=0
        _ods_pixel_start_stopped_access_service "$owner" "$TEST_ROOT" \
            0123456789abcdef0123456789abcdef01234567 || result=$?
        [[ "$result" == "$expected_rc" && "$(cat "$calls" 2>/dev/null)" == "$expected_calls" ]]
    ); then pass "stopped access coordinator start: $access_case"
    else fail "stopped access coordinator start: $access_case"; fi
done
# Helpers written before an interrupted install reached the coordinator's
# unit leave nothing to resume; a recorded transition still needs recovery.
for resume_case in no-unit-no-transition no-unit-with-transition unit-installed; do
    if (
        case_root="$TEST_ROOT/access-resume/$resume_case"
        mkdir -p "$case_root/state"
        export ODS_PIXEL_ACCESS_HELPER="$case_root/pixel_model_transition.py"
        export ODS_PIXEL_ACCESS_UNIT_PATH="$case_root/ods-pixel-access.service"
        export ODS_PIXEL_ACCESS_STATE_DIR="$case_root/state"
        export PIXEL_SOURCE_REF=0123456789abcdef0123456789abcdef01234567
        : >"$ODS_PIXEL_ACCESS_HELPER"
        expected='status'
        case "$resume_case" in
            no-unit-no-transition) expected='' ;;
            no-unit-with-transition) : >"$ODS_PIXEL_ACCESS_STATE_DIR/transition.json" ;;
            unit-installed) : >"$ODS_PIXEL_ACCESS_UNIT_PATH" ;;
        esac
        ods_sudo_available() { return 0; }
        ods_sudo() { "$@"; }
        _ods_pixel_start_stopped_access_service() { :; }
        _ods_pixel_model_transition() { printf '%s\n' "$1" >>"$case_root/calls"; return 1; }
        result=0
        _ods_pixel_resume_completed_release "$owner" "$TEST_ROOT" "$TEST_ROOT" \
            0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef 2>/dev/null || result=$?
        if [[ -z "$expected" ]]; then
            [[ "$result" == 0 && ! -e "$case_root/calls" ]]
        else
            [[ "$result" == 1 && "$(cat "$case_root/calls")" == "$expected" ]]
        fi
    ); then pass "access coordinator resume guard: $resume_case"
    else fail "access coordinator resume guard: $resume_case"; fi
done
grep -Fq '_ods_pixel_start_stopped_access_service "$owner" "$home" "${PIXEL_SOURCE_REF:?}"' \
    "$ROOT/installers/lib/pixel-host-install.sh" \
    && grep -Fq '_ods_pixel_start_stopped_access_service \' "$ROOT/installers/phases/06-directories.sh" \
    && pass "both rerun status paths start a stopped access coordinator first" \
    || fail "a rerun status path skips the stopped access coordinator start"

if (
    inspection_calls="$TEST_ROOT/inspection-owner-calls"
    INSTALL_DIR="$TEST_ROOT/inspection-install"
    ods_pixel_run_as_owner() {
        printf '%s %s\n' "${ODS_PIXEL_OWNER_PRIMARY_GROUP:-docker}" "$5" >>"$inspection_calls"
        [[ "$5" != build ]] || printf '%s\n' '{}'
    }
    ods_sudo() {
        [[ "$*" != *" health" ]] \
            || printf '%s\n' '{"schemaVersion":1,"kind":"ods-pixel-preview-inspection","status":"ready"}'
    }
    _ods_pixel_install_preview_inspection "$owner" "$TEST_ROOT" "$TEST_ROOT/inspection-source"
    [[ "$(cat "$inspection_calls")" == $'docker build\nods-pixel wait-owner-socket' ]]
); then
    pass "preview inspection builds with Docker and probes its root:ods-pixel socket with ods-pixel"
else
    fail "preview inspection owner calls did not get the group each one needs"
fi

probe_program="$TEST_ROOT/manager-probe.py"
probe_counter="$TEST_ROOT/manager-probe.count"
cat > "$probe_program" <<'PY'
import json, os, pathlib, sys

counter = pathlib.Path(os.environ["PIXEL_TEST_COUNTER"])
calls = int(counter.read_text() if counter.exists() else "0") + 1
counter.write_text(str(calls), encoding="utf-8")
if calls == 1 and os.environ.get("PIXEL_TEST_TRANSIENT") == "true":
    raise SystemExit(1)
extension_id = "wrong" if os.environ.get("PIXEL_TEST_BAD") == "true" else sys.argv[4]
print(json.dumps({
    "schemaVersion": 1,
    "kind": "ods-pixel-extension-lifecycle",
    "action": "inspect",
    "extensionId": extension_id,
    "outcome": os.environ.get("PIXEL_TEST_OUTCOME", "inspected"),
    "previousStatus": "not_installed",
    "currentStatus": "not_installed",
    "changed": False,
    "externalEffectOccurred": False,
    "requiredConfiguration": [],
    "optionalConfiguration": [],
    "missingConfiguration": [],
    "rollback": {"attempted": False, "succeeded": None},
    "boundary": "Scoped ODS extension lifecycle proxy; it grants no Docker, shell, credential, arbitrary HTTP, or data-purge authority.",
}))
PY
if (
    ods_sudo() { shift 2; "$@"; }
    export PIXEL_TEST_COUNTER="$probe_counter" PIXEL_TEST_TRANSIENT=true
    _ods_pixel_wait_extension_manager_probe "$probe_program" crewai 3 0
); then
    pass "extension manager readiness retries a transient failure"
else
    fail "extension manager readiness retries a transient failure"
fi
check test "$(cat "$probe_counter")" = 2
rm -f -- "$probe_counter"
if (
    ods_sudo() { shift 2; "$@"; }
    export PIXEL_TEST_COUNTER="$probe_counter" PIXEL_TEST_OUTCOME=ready
    _ods_pixel_wait_extension_manager_probe "$probe_program" crewai 1 0
); then
    fail "extension manager readiness rejects the obsolete ready inspect outcome"
else
    pass "extension manager readiness rejects the obsolete ready inspect outcome"
fi
rm -f -- "$probe_counter"
if (
    ods_sudo() { shift 2; "$@"; }
    export PIXEL_TEST_COUNTER="$probe_counter" PIXEL_TEST_BAD=true
    _ods_pixel_wait_extension_manager_probe "$probe_program" crewai 2 0
); then
    fail "extension manager readiness rejects structurally mismatched receipts"
else
    pass "extension manager readiness rejects structurally mismatched receipts"
fi
check test "$(cat "$probe_counter")" = 2

profile_root="$TEST_ROOT/ops-state-profiles"
mkdir -m 0750 "$profile_root"
for profile in .bash_logout .bashrc .profile; do
    printf '%s\n' fixture >"$profile_root/$profile"
    chmod 0644 "$profile_root/$profile"
done
if (
    ods_sudo() { "$@"; }
    _ods_pixel_harden_operations_state_profiles "$profile_root" "$owner"
); then
    [[ "$(stat -c '%a' "$profile_root/.bash_logout")" == 600 \
        && "$(stat -c '%a' "$profile_root/.bashrc")" == 600 \
        && "$(stat -c '%a' "$profile_root/.profile")" == 600 ]] \
        && pass "Operations Broker service profiles are hardened after Pixel account creation" \
        || fail "Operations Broker service profiles retained public modes"
else
    fail "Operations Broker service profiles could not be hardened"
fi

rm -rf -- "$profile_root"
mkdir -m 0750 "$profile_root"
printf '%s\n' fixture >"$profile_root/.profile"
chmod 0644 "$profile_root/.profile"
ln -s /etc/passwd "$profile_root/.bashrc"
if (
    ods_sudo() { "$@"; }
    _ods_pixel_harden_operations_state_profiles "$profile_root" "$owner"
); then
    fail "unsafe Operations Broker service profile was accepted"
elif [[ "$(stat -c '%a' "$profile_root/.profile")" == 644 ]]; then
    pass "Operations Broker profile hardening validates every target before mutation"
else
    fail "failed Operations Broker profile hardening caused partial mutation"
fi

home="$TEST_ROOT/home"
mkdir -p "$home/.openclaw"
printf '%s\n' '{"gateway":{"bind":"loopback"},"preserve":{"value":7}}' > "$home/.openclaw/openclaw.json"
chmod 0644 "$home/.openclaw/openclaw.json"

attempt_log="$TEST_ROOT/attempt-logs/pixel-install.log"
check test "$(_ods_pixel_prepare_attempt_log "$owner" "$home" "$attempt_log")" = "$attempt_log"
check test -f "$attempt_log"
check test ! -L "$attempt_log"
check test "$(stat -c '%a' "$attempt_log")" = 600
printf '%s\n' keep > "$TEST_ROOT/attempt-log-target"
rm -f -- "$attempt_log"
ln -s "$TEST_ROOT/attempt-log-target" "$attempt_log"
check test "$(_ods_pixel_prepare_attempt_log "$owner" "$home" "$attempt_log")" = "$attempt_log"
check test -f "$attempt_log"
check test ! -L "$attempt_log"
check test "$(cat "$TEST_ROOT/attempt-log-target")" = keep
rm -rf -- "$TEST_ROOT/attempt-logs"
mkdir -p "$TEST_ROOT/attempt-log-link-target"
ln -s "$TEST_ROOT/attempt-log-link-target" "$TEST_ROOT/attempt-logs"
if _ods_pixel_prepare_attempt_log "$owner" "$home" "$attempt_log" >/dev/null 2>&1; then
    fail "symlink Pixel attempt-log directory rejected"
else
    pass "symlink Pixel attempt-log directory rejected"
fi
rm -f -- "$TEST_ROOT/attempt-logs"

_ods_pixel_enable_chat_endpoint "$owner" "$home"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["gateway"]["http"]["endpoints"]["chatCompletions"]["enabled"] is True; assert v["preserve"]["value"] == 7' "$home/.openclaw/openclaw.json"
check test "$(stat -c '%a' "$home/.openclaw/openclaw.json")" = 600
# An already enabled endpoint leaves the file byte-for-byte alone, whatever its
# serialization: a held source upgrade compares the exact bytes it recorded
# before this step (Full Access writes the config unsorted).
printf '%s\n' '{"preserve": {"value": 7}, "gateway": {"http": {"endpoints": {"chatCompletions": {"enabled": true}}}}}' \
    > "$home/.openclaw/openclaw.json"
chat_config_before="$(sha256sum "$home/.openclaw/openclaw.json")"
_ods_pixel_enable_chat_endpoint "$owner" "$home"
check test "$(sha256sum "$home/.openclaw/openclaw.json")" = "$chat_config_before"

rm -f "$home/.openclaw/openclaw.json"
printf '%s\n' '{}' > "$TEST_ROOT/symlink-target.json"
ln -s "$TEST_ROOT/symlink-target.json" "$home/.openclaw/openclaw.json"
if _ods_pixel_enable_chat_endpoint "$owner" "$home" >/dev/null 2>&1; then
    fail "symlink OpenClaw config rejected"
else
    pass "symlink OpenClaw config rejected"
fi
rm -f "$home/.openclaw/openclaw.json"

INSTALL_DIR="$TEST_ROOT/ods"
PIXEL_SOURCE_REF="$(printf 'b%.0s' {1..40})"
ODS_PIXEL_GATEWAY_UNIT_PATH="$TEST_ROOT/openclaw-gateway.service"
export INSTALL_DIR PIXEL_SOURCE_REF ODS_PIXEL_GATEWAY_UNIT_PATH
# The access coordinator rejects a ~/.config others can write
# (model-marker-unsafe). Under Ubuntu's default umask 0002 the installer, which
# runs as the owner, must neither create nor keep a group-writable ~/.config.
coordinator_accepts_config() {
    python3 - "$1" <<'PY'
import os, stat, sys, pathlib
marker = pathlib.Path(sys.argv[1]) / ".config/ods/pixel-managed.json"
for directory, unsafe_bits in ((marker.parent.parent, 0o022), (marker.parent, 0o077)):
    info = directory.lstat()
    if not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid() or info.st_mode & unsafe_bits:
        raise SystemExit(f"model-marker-unsafe: {directory} {oct(stat.S_IMODE(info.st_mode))}")
PY
}
for umask_case in fresh group-writable; do
    umask_home="$TEST_ROOT/umask-$umask_case-home"
    mkdir -p "$umask_home"
    [[ "$umask_case" == group-writable ]] && install -d -m 0775 "$umask_home/.config"
    if ( umask 0002; _ods_pixel_assert_managed_state "$owner" "$umask_home" ) >/dev/null 2>&1 \
        && coordinator_accepts_config "$umask_home"; then
        pass "umask 0002 leaves a $umask_case ~/.config the access coordinator accepts"
    else
        fail "umask 0002 leaves a $umask_case ~/.config the access coordinator rejects"
    fi
done
check test "$(stat -c '%a' "$TEST_ROOT/umask-fresh-home/.config")" = 755

_ods_pixel_assert_managed_state "$owner" "$home"
marker="$home/.config/ods/pixel-managed.json"
check test "$(stat -c '%a' "$marker")" = 600
check test "$(stat -c '%a' "${marker%/*}")" = 700
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v == {"initial_active_state":"absent","install_dir":sys.argv[2],"manager":"ods","pixel_source_ref":sys.argv[3],"schema_version":2,"state":"installing"}' "$marker" "$INSTALL_DIR" "$PIXEL_SOURCE_REF"
if _ods_pixel_source_transition_required "$owner" "$home" "$PIXEL_SOURCE_REF"; then
    fail "matching Pixel source unexpectedly requires retirement"
else
    check test "$?" = 1
fi
next_source_ref="$(printf 'c%.0s' {1..40})"
check _ods_pixel_source_transition_required "$owner" "$home" "$next_source_ref"
python3 - "$marker" "$next_source_ref" <<'PY'
import json, pathlib, re, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text(encoding="utf-8"))
value["requested_source_ref"] = sys.argv[2]
path.write_text(json.dumps(value) + "\n", encoding="utf-8")
PY
chmod 0600 "$marker"
check _ods_pixel_source_transition_required "$owner" "$home" "$next_source_ref"
python3 - "$marker" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text(encoding="utf-8"))
value.pop("requested_source_ref", None)
path.write_text(json.dumps(value) + "\n", encoding="utf-8")
PY
chmod 0600 "$marker"
printf '%s\n' '{"gateway":{"http":{"endpoints":{"chatCompletions":{"enabled":true}}}}}' > "$home/.openclaw/openclaw.json"
chmod 0600 "$home/.openclaw/openclaw.json"
contract_sha256="$(printf 'c%.0s' {1..64})"
pixel_root="$TEST_ROOT/pixel-root"
release="$home/.local/share/pixel/releases/4.3.14"
mkdir -p "$pixel_root" "$release"
printf '%s\n' '{"sandboxImage":"openclaw-sandbox:test"}' > "$pixel_root/RELEASE-MANIFEST.json"
cat > "$release/release-identity.json" <<JSON
{"kind":"pixel-release-source-identity","pixel":"4.3.14","source":{"state":"git-clean","commit":"$PIXEL_SOURCE_REF","tree":"$(printf 'a%.0s' {1..40})"}}
JSON
printf '%s  %s\n' "$(sha256sum "$release/release-identity.json" | awk '{print $1}')" release-identity.json > "$release/install-manifest.sha256"
identity_sha256="$(sha256sum "$release/release-identity.json" | awk '{print $1}')"
manifest_sha256="$(sha256sum "$release/install-manifest.sha256" | awk '{print $1}')"
cat > "$home/.local/share/pixel/runtime-attestation.json" <<JSON
{"kind":"pixel-runtime-attestation","status":"verified","pixel":"4.3.14","source":{"state":"git-clean","commit":"$PIXEL_SOURCE_REF","tree":"$(printf 'a%.0s' {1..40})"},"release":{"sourceIdentitySha256":"$identity_sha256","installManifestSha256":"$manifest_sha256"}}
JSON
chmod 0600 "$home/.local/share/pixel/runtime-attestation.json"
ln -s "$release" "$home/.local/share/pixel/current"
mock_bin="$TEST_ROOT/mock-bin"
mkdir -p "$mock_bin"
cat > "$mock_bin/docker" <<SH
#!/usr/bin/env bash
if [[ "\$1 \$2" == "image inspect" ]]; then
    printf '%s\n' 'sha256:$(printf 'd%.0s' {1..64})'
    exit 0
fi
exit 1
SH
chmod +x "$mock_bin/docker"
PATH="$mock_bin:$PATH"
export PATH
_ods_pixel_mark_verified_installing "$owner" "$home" "$contract_sha256" "$pixel_root"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["state"] == "installing" and v["pixel_source_ref"] == sys.argv[2] and v["contract_sha256"] == sys.argv[3] and len(v["configuration_sha256"]) == 64 and v["active_release_version"] == "4.3.14" and len(v["release_identity_sha256"]) == 64 and len(v["install_manifest_sha256"]) == 64 and v["sandbox_image"] == "openclaw-sandbox:test" and v["sandbox_image_id"].startswith("sha256:")' "$marker" "$PIXEL_SOURCE_REF" "$contract_sha256"
check _ods_pixel_managed_contract_matches "$owner" "$home" "$contract_sha256"
check _ods_pixel_verified_source_matches "$owner" "$home"
_ods_pixel_mark_ready "$owner" "$home" "$contract_sha256" "$pixel_root"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["state"] == "ready" and v["pixel_source_ref"] == sys.argv[2] and v["contract_sha256"] == sys.argv[3] and len(v["configuration_sha256"]) == 64' "$marker" "$PIXEL_SOURCE_REF" "$contract_sha256"
check _ods_pixel_managed_contract_matches "$owner" "$home" "$contract_sha256"
if _ods_pixel_managed_contract_matches "$owner" "$home" "$(printf 'd%.0s' {1..64})"; then
    fail "mismatched managed Pixel contract rejected"
else
    pass "mismatched managed Pixel contract rejected"
fi
original_source_ref="$PIXEL_SOURCE_REF"
PIXEL_SOURCE_REF="$(printf 'e%.0s' {1..40})"
if _ods_pixel_managed_contract_matches "$owner" "$home" "$contract_sha256"; then
    fail "mismatched exact Pixel source commit rejected"
else
    pass "mismatched exact Pixel source commit rejected"
fi
if _ods_pixel_verified_source_matches "$owner" "$home"; then
    fail "mismatched verified Pixel source rejected for extension refresh"
else
    pass "mismatched verified Pixel source rejected for extension refresh"
fi
requested_contract_sha256="$(printf 'd%.0s' {1..64})"
_ods_pixel_mark_installing "$owner" "$home" "$requested_contract_sha256"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["pixel_source_ref"] == sys.argv[2] and v["requested_source_ref"] == sys.argv[3] and v["requested_contract_sha256"] == sys.argv[4] and v["state"] == "installing"' "$marker" "$original_source_ref" "$PIXEL_SOURCE_REF" "$requested_contract_sha256"
PIXEL_SOURCE_REF="$original_source_ref"
chmod 0644 "$marker"
if _ods_pixel_managed_contract_matches "$owner" "$home" "$contract_sha256"; then
    fail "unsafe managed Pixel marker mode rejected"
else
    pass "unsafe managed Pixel marker mode rejected"
fi
chmod 0600 "$marker"
cp "$home/.openclaw/openclaw.json" "$TEST_ROOT/openclaw.valid.json"
printf '%s\n' '{"gateway":{"http":{"endpoints":{"chatCompletions":{"enabled":false}}}}}' > "$home/.openclaw/openclaw.json"
if _ods_pixel_managed_contract_matches "$owner" "$home" "$contract_sha256"; then
    fail "drifted managed OpenClaw configuration rejected"
else
    pass "drifted managed OpenClaw configuration rejected"
fi
mv "$TEST_ROOT/openclaw.valid.json" "$home/.openclaw/openclaw.json"
candidate="$TEST_ROOT/openclaw.candidate.json"
printf '%s\n' '{"gateway":{"http":{"endpoints":{"chatCompletions":{"enabled":true}}}}}' > "$candidate"
chmod 0600 "$candidate"
check _ods_pixel_candidate_config_matches_live "$owner" "$home" "$candidate"
printf '%s\n' '{"gateway":{"http":{"endpoints":{"chatCompletions":{"enabled":false}}}}}' > "$candidate"
if _ods_pixel_candidate_config_matches_live "$owner" "$home" "$candidate"; then
    fail "drifted Pixel candidate config rejected"
else
    pass "drifted Pixel candidate config rejected"
fi
rm -f "$candidate"
ln -s "$home/.openclaw/openclaw.json" "$candidate"
if _ods_pixel_candidate_config_matches_live "$owner" "$home" "$candidate"; then
    fail "symlink Pixel candidate config rejected"
else
    pass "symlink Pixel candidate config rejected"
fi
rm -f "$candidate"
check _ods_pixel_assert_managed_state "$owner" "$home"
_ods_pixel_mark_installing "$owner" "$home" "$contract_sha256"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["state"] == "installing" and v["pixel_source_ref"] == sys.argv[2] and v["contract_sha256"] == sys.argv[3] and v["requested_contract_sha256"] == sys.argv[3]' "$marker" "$PIXEL_SOURCE_REF" "$contract_sha256"
check _ods_pixel_managed_contract_matches "$owner" "$home" "$contract_sha256"
_ods_pixel_mark_ready "$owner" "$home" "$contract_sha256" "$pixel_root"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["state"] == "ready" and "requested_source_ref" not in v and "requested_contract_sha256" not in v' "$marker"

# A runtime repair failure after the managed-runtime overlay must leave the
# overlaid config bound to the marker (the uninstaller's exact drift check),
# and another ODS build's recorded patch set must already be restored.
repair_home="$TEST_ROOT/repair-home"
repair_install="$TEST_ROOT/repair-ods"
repair_runtime="$repair_home/.npm-global/lib/node_modules/openclaw"
repair_release="$repair_home/.local/share/pixel/releases/4.3.14"
repair_pixel="$TEST_ROOT/repair-pixel-root"
repair_messages="$TEST_ROOT/repair-messages.log"
repair_foreign="$repair_home/.openclaw/ods-runtime-patches/file-operations"
repair_contract="$(printf 'f%.0s' {1..64})"
mkdir -p "$repair_home/.openclaw" "$repair_home/.npm-global/bin" "$repair_runtime/dist" \
    "$repair_release" "$repair_pixel" "$repair_install/extensions/services/pixel-agent" "$repair_foreign"
INSTALL_DIR="$repair_install" ODS_PIXEL_GATEWAY_UNIT_PATH="$TEST_ROOT/repair-gateway.service" \
    _ods_pixel_assert_managed_state "$owner" "$repair_home"
cp -R "$ROOT/extensions/services/pixel-agent/host" "$ROOT/extensions/services/pixel-agent/plugin" \
    "$repair_install/extensions/services/pixel-agent/"
printf '%s\n' '{"gateway":{"http":{"endpoints":{"chatCompletions":{"enabled":true}}}}}' \
    > "$repair_home/.openclaw/openclaw.json"
printf '%s\n' '{"sandboxImage":"openclaw-sandbox:test"}' > "$repair_pixel/RELEASE-MANIFEST.json"
cat > "$repair_pixel/pixel" <<'SH'
#!/usr/bin/env bash
if [[ "$1" == extension-hash ]]; then printf '%064d\n' 0; fi
SH
chmod 0700 "$repair_pixel/pixel"
cp "$release/release-identity.json" "$release/install-manifest.sha256" "$repair_release/"
cp "$home/.local/share/pixel/runtime-attestation.json" "$repair_home/.local/share/pixel/"
ln -s "$repair_release" "$repair_home/.local/share/pixel/current"
printf '%s\n' '{"name":"openclaw","version":"2026.6.33"}' > "$repair_runtime/package.json"
: > "$repair_runtime/openclaw.mjs"
chmod 0700 "$repair_runtime/openclaw.mjs"
ln -s ../lib/node_modules/openclaw/openclaw.mjs "$repair_home/.npm-global/bin/openclaw"
# Unreviewed bytes make the first ODS repair fail exactly as on the fleet.
printf '%s\n' 'unreviewed();' > "$repair_runtime/dist/tool-loop-detection-C0oQKkXZ.js"
printf '%s\n' 'foreign original();' > "$repair_runtime/dist/agent-tools-D1DOpg6D.js"
printf '%s\n' 'foreign original();' > "$repair_foreign/$(sha256sum "$repair_runtime/dist/agent-tools-D1DOpg6D.js" | awk '{print $1}').js"
printf '%s\n' 'foreign patched();' > "$repair_runtime/dist/agent-tools-D1DOpg6D.js"
python3 - "$repair_foreign" "$repair_runtime/dist/agent-tools-D1DOpg6D.js" <<'PY'
import hashlib, json, pathlib, sys
state, module = map(pathlib.Path, sys.argv[1:])
backup = next(state.glob("*.js"))
patched = hashlib.sha256(module.read_bytes()).hexdigest()
(state / "receipt.json").write_text(json.dumps({
    "schemaVersion": 1, "version": "2026.6.33", "module": module.name,
    "sourceSha256": backup.stem, "patchedSha256": patched, "backup": backup.name,
    "desiredSha256": patched}, sort_keys=True))
PY
# An earlier verified install bound the pre-overlay configuration.
INSTALL_DIR="$repair_install" \
    _ods_pixel_mark_verified_installing "$owner" "$repair_home" "$repair_contract" "$repair_pixel"
if (
    INSTALL_DIR="$repair_install"
    systemctl() {
        case "$*" in
            *"-p ProtectHome --value") printf 'tmpfs\n' ;;
            *"-p ProtectSystem --value") printf 'strict\n' ;;
            *) return 1 ;;
        esac
    }
    ENABLE_PIXEL_RUNTIME=true
    PIXEL_SERVICE_USER="$owner"
    DOCKER_COMPOSE_CMD=true
    COMPOSE_FLAGS_ARR=()
    LOG_FILE="$TEST_ROOT/repair-compose.log"
    ai_bad() { printf '%s\n' "$*" >> "$repair_messages"; }
    ai_warn() { printf '%s\n' "$*" >> "$repair_messages"; }
    ods_pixel_owner_home() { printf '%s\n' "$repair_home"; }
    _ods_pixel_source_checkout() { printf '%s\n' "$repair_pixel"; }
    _ods_pixel_contract_sha256() { printf '%s\n' "$repair_contract"; }
    _ods_pixel_gateway_model_alias() { printf '%s\n' ods/current; }
    # This fixture isolates runtime-package repair after exact-route reuse.
    # Real route agreement and drift are exercised in test_pixel_onboarding_upgrade.py.
    _ods_pixel_stable_alias_matches_promoted_model() { return 0; }
    _ods_pixel_write_onboarding() {
        mkdir -p "${3%/*}"
        printf '%s\n' '{}' > "$3"
        chmod 0600 "$3"
    }
    _ods_pixel_apply_runtime_budget() {
        python3 - "$3" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text(encoding="utf-8"))
value["agents"] = {"defaults": {"timeoutSeconds": 1800}}
path.write_text(json.dumps(value) + "\n", encoding="utf-8")
PY
        printf '%s\n' changed
    }
    _ods_pixel_refresh_plugin_registry() { printf '%s\n' registry-refresh >> "$repair_messages"; }
    for stub in _ods_pixel_existing_gateway_port_matches _ods_pixel_secure_plugin_tree \
        _ods_pixel_install_exec_control _ods_pixel_wait_model_gateway _ods_pixel_wait_http \
        ods_linux_node_tools_available _ods_pixel_write_extension_catalog \
        _ods_pixel_write_operations_policy _ods_pixel_write_extension_manager_unit \
        _ods_pixel_write_artifact_promoter_unit _ods_pixel_write_workspace_preview_unit \
        _ods_pixel_harden_operations_state_profiles \
        _ods_pixel_verify_operations_policy_custody _ods_pixel_sandbox_candidate_has_node; do
        eval "$stub() { :; }"
    done
    eval "_repair_run_as_owner() $(declare -f ods_pixel_run_as_owner | tail -n +2)"
    ods_pixel_run_as_owner() {
        if [[ "${4:-}" == */host/native_search.py ]]; then
            printf '%s\n' searxng
            return 0
        fi
        _repair_run_as_owner "$@"
    }
    ods_pixel_install_default_agent
); then
    fail "runtime repair failure was not reported"
else
    pass "runtime repair failure fails the Pixel install"
fi
check grep -Fqx "Pixel's runtime recovery repair could not verify its package bytes. See $repair_install/logs/pixel-install.log." "$repair_messages"
check grep -Fq 'OpenClaw recovery module differs from reviewed bytes' "$repair_install/logs/pixel-install.log"
check test "$(grep -c registry-refresh "$repair_messages" || true)" = 0
check python3 -c '
import hashlib, json, sys
marker = json.load(open(sys.argv[1]))
config = json.load(open(sys.argv[2]))
canonical = json.dumps(config, sort_keys=True, separators=(",", ":")).encode()
assert config["agents"]["defaults"]["timeoutSeconds"] == 1800
assert marker["state"] == "installing" and marker["contract_sha256"] == sys.argv[3]
assert marker["configuration_sha256"] == hashlib.sha256(b"ods-pixel-openclaw-v1\0" + canonical).hexdigest()
' "$repair_home/.config/ods/pixel-managed.json" "$repair_home/.openclaw/openclaw.json" "$repair_contract"
if INSTALL_DIR="$repair_install" \
    _ods_pixel_managed_contract_matches "$owner" "$repair_home" "$repair_contract"; then
    pass "failed runtime repair leaves the overlaid config bound to its marker"
else
    fail "failed runtime repair left unbound managed-runtime config drift"
fi
check test "$(cat "$repair_runtime/dist/agent-tools-D1DOpg6D.js")" = 'foreign original();'
check test ! -e "$repair_foreign"
check test -f "$(find "$repair_home/.openclaw/ods-runtime-patches.retired" \
    -path '*/file-operations/receipt.json' -print -quit 2>/dev/null)"

ambient_home="$TEST_ROOT/ambient-home"
mkdir -p "$ambient_home/.openclaw"
printf '%s\n' '{}' > "$ambient_home/.openclaw/openclaw.json"
if _ods_pixel_assert_managed_state "$owner" "$ambient_home" >/dev/null 2>&1; then
    fail "ambient OpenClaw deployment rejected"
else
    pass "ambient OpenClaw deployment rejected"
fi
check test ! -e "$ambient_home/.config/ods/pixel-managed.json"

ambient_active_home="$TEST_ROOT/ambient-active-home"
mkdir -p "$ambient_active_home/.local/share/pixel/releases/4.3.14"
ln -s "$ambient_active_home/.local/share/pixel/releases/4.3.14" "$ambient_active_home/.local/share/pixel/current"
if _ods_pixel_assert_managed_state "$owner" "$ambient_active_home" >/dev/null 2>&1; then
    fail "ambient active Pixel release rejected before marker creation"
else
    pass "ambient active Pixel release rejected before marker creation"
fi
check test ! -e "$ambient_active_home/.config/ods/pixel-managed.json"

inactive_home="$TEST_ROOT/inactive-conflict-home"
inactive_pixel_root="$TEST_ROOT/inactive-conflict-source"
inactive_release="$inactive_home/.local/share/pixel/releases/4.3.27"
inactive_log="$TEST_ROOT/inactive-conflict-apply.log"
for shared_error in \
    'Shared live sandbox tag exists without an active Pixel release and is not valid for the reviewed candidate' \
    'Shared live sandbox tag exists without an active Pixel release and does not match the reviewed candidate'; do
    printf '[pixel] ERROR: %s\n' "$shared_error" >"$inactive_log"
    check _ods_pixel_shared_sandbox_conflict "$owner" "$TEST_ROOT" "$inactive_log"
done
printf '%s\n' '[pixel] ERROR: Release already exists but is not byte-exact to the reviewed plan: /example' >"$inactive_log"
if _ods_pixel_shared_sandbox_conflict "$owner" "$TEST_ROOT" "$inactive_log"; then
    fail "inactive release directory was misclassified as a shared Docker tag conflict"
else
    pass "inactive release directory is distinct from retained Docker state"
fi
printf '%s\n' 'quoted [pixel] ERROR: Shared live sandbox tag exists without an active Pixel release and is not valid for the reviewed candidate' >"$inactive_log"
if _ods_pixel_shared_sandbox_conflict "$owner" "$TEST_ROOT" "$inactive_log"; then
    fail "non-exact shared tag diagnostic was accepted"
else
    pass "shared tag diagnostic requires the exact Pixel failure"
fi
mkdir -p "$inactive_home/.config/ods" "$inactive_release" "$inactive_pixel_root"
chmod 0700 "$inactive_home/.config/ods" "$inactive_home/.local/share/pixel" \
    "$inactive_home/.local/share/pixel/releases"
printf '%s\n' 4.3.27 >"$inactive_pixel_root/VERSION"
write_inactive_marker() {
    cat >"$inactive_home/.config/ods/pixel-managed.json" <<JSON
{"schema_version":2,"manager":"ods","state":"installing","initial_active_state":"absent","install_dir":"$INSTALL_DIR","pixel_source_ref":"$PIXEL_SOURCE_REF"}
JSON
    chmod 0600 "$inactive_home/.config/ods/pixel-managed.json"
}
write_inactive_marker
cat >"$inactive_release/release-identity.json" <<JSON
{"kind":"pixel-release-source-identity","pixel":"4.3.27","source":{"state":"git-clean","commit":"$PIXEL_SOURCE_REF","tree":"$(printf 'a%.0s' {1..40})"}}
JSON
printf '%s\n' 'reviewed inactive release' >"$inactive_release/payload.txt"
printf '%s\n' 4.3.27 >"$inactive_release/VERSION"
(
    cd "$inactive_release"
    sha256sum ./VERSION ./payload.txt ./release-identity.json >install-manifest.sha256
)
printf '%s\n' '[pixel] ERROR: unrelated apply failure' >"$inactive_log"
chmod 0600 "$inactive_log"
if _ods_pixel_retire_inactive_conflicting_release \
    "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log" >/dev/null 2>&1; then
    fail "unrelated Pixel apply failure triggered inactive-release retirement"
else
    check test -d "$inactive_release"
fi
printf '[pixel] ERROR: Release already exists but is not byte-exact to the reviewed plan: %s\n' \
    "$inactive_release" >"$inactive_log"
printf '%s\n' tampered >>"$inactive_release/payload.txt"
if _ods_pixel_retire_inactive_conflicting_release \
    "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log" >/dev/null 2>&1; then
    fail "manifest-drifted inactive Pixel release was retired"
else
    check test -d "$inactive_release"
fi
printf '%s\n' 'reviewed inactive release' >"$inactive_release/payload.txt"
(
    cd "$inactive_release"
    sha256sum ./VERSION ./payload.txt ./release-identity.json >install-manifest.sha256
)
for marker_case in initial-active verified-state wrong-requested-source wrong-schema wrong-manager; do
    write_inactive_marker
    python3 - "$inactive_home/.config/ods/pixel-managed.json" "$marker_case" <<'PY'
import json
import pathlib
import sys

path = pathlib.Path(sys.argv[1])
case = sys.argv[2]
value = json.loads(path.read_text())
if case == "initial-active":
    value["initial_active_state"] = "running"
elif case == "verified-state":
    value["active_release_version"] = "4.3.27"
elif case == "wrong-requested-source":
    value["requested_source_ref"] = "f" * 40
elif case == "wrong-schema":
    value["schema_version"] = 1
elif case == "wrong-manager":
    value["manager"] = "ambient"
path.write_text(json.dumps(value, separators=(",", ":")) + "\n")
PY
    if _ods_pixel_retire_inactive_conflicting_release \
        "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log" >/dev/null 2>&1; then
        fail "unsafe $marker_case marker triggered inactive-release retirement"
    else
        pass "unsafe $marker_case marker is rejected before inactive-release retirement"
    fi
    check test -d "$inactive_release"
done
write_inactive_marker
inactive_gateway_unit="$TEST_ROOT/inactive-openclaw-gateway.service"
printf '%s\n' '[Unit]' >"$inactive_gateway_unit"
if ODS_PIXEL_GATEWAY_UNIT_PATH="$inactive_gateway_unit" \
    _ods_pixel_retire_inactive_conflicting_release \
        "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log" >/dev/null 2>&1; then
    fail "existing Pixel gateway unit allowed inactive-release retirement"
else
    pass "existing Pixel gateway unit blocks inactive-release retirement"
fi
rm -f -- "$inactive_gateway_unit"
printf '%s\n' '{}' >"$inactive_home/.local/share/pixel/runtime-attestation.json"
if _ods_pixel_retire_inactive_conflicting_release \
    "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log" >/dev/null 2>&1; then
    fail "runtime-attested Pixel allowed inactive-release retirement"
else
    pass "runtime-attested Pixel blocks inactive-release retirement"
fi
rm -f -- "$inactive_home/.local/share/pixel/runtime-attestation.json"
mv -- "$inactive_release" "$inactive_release.real"
ln -s -- "$inactive_release.real" "$inactive_release"
if _ods_pixel_retire_inactive_conflicting_release \
    "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log" >/dev/null 2>&1; then
    fail "symlinked Pixel release root allowed inactive-release retirement"
else
    pass "symlinked Pixel release root blocks inactive-release retirement"
fi
rm -f -- "$inactive_release"
mv -- "$inactive_release.real" "$inactive_release"
ln -s "$inactive_release" "$inactive_home/.local/share/pixel/current"
if _ods_pixel_retire_inactive_conflicting_release \
    "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log" >/dev/null 2>&1; then
    fail "active Pixel release was retired by inactive conflict recovery"
else
    check test -d "$inactive_release"
fi
rm -f -- "$inactive_home/.local/share/pixel/current"
inactive_retired="$(_ods_pixel_retire_inactive_conflicting_release \
    "$owner" "$inactive_home" "$inactive_pixel_root" "$inactive_log")"
check test -d "$inactive_retired"
check test ! -e "$inactive_release"
check test "${inactive_retired%/release}" != "$inactive_retired"
check test "$(stat -c '%a' "$inactive_home/.local/share/pixel/retired-ods-releases")" = 700
check test "$(sha256sum "$inactive_retired/payload.txt" | awk '{print $1}')" \
    = "$(printf '%s\n' 'reviewed inactive release' | sha256sum | awk '{print $1}')"

plugin_tree="$INSTALL_DIR/extensions/services/pixel-agent/plugin"
mkdir -p "$plugin_tree/nested"
printf '%s\n' '{"id":"pixel-ods"}' > "$plugin_tree/openclaw.plugin.json"
printf '%s\n' 'export default {};' > "$plugin_tree/nested/index.js"
chmod 0777 "$INSTALL_DIR" "$INSTALL_DIR/extensions" "$INSTALL_DIR/extensions/services" \
    "$INSTALL_DIR/extensions/services/pixel-agent" "$plugin_tree" \
    "$plugin_tree/nested" "$plugin_tree/openclaw.plugin.json" "$plugin_tree/nested/index.js"
check _ods_pixel_secure_plugin_tree "$owner" "$home" "$plugin_tree"
check test -z "$(find -P "$plugin_tree" -perm /022 -print -quit)"
check test "$(stat -c '%a' "$INSTALL_DIR")" = 755
check test "$(stat -c '%a' "$plugin_tree")" = 755
check test "$(stat -c '%a' "$plugin_tree/nested/index.js")" = 644
ln -s "$plugin_tree/openclaw.plugin.json" "$plugin_tree/linked.json"
if _ods_pixel_secure_plugin_tree "$owner" "$home" "$plugin_tree" >/dev/null 2>&1; then
    fail "symlink in ODS Pixel plugin tree rejected"
else
    pass "symlink in ODS Pixel plugin tree rejected"
fi
rm -f "$plugin_tree/linked.json"

exec_control_home="$TEST_ROOT/exec-control-home"
exec_control_source="$TEST_ROOT/cancellable-exec.sh"
exec_control_sudo_source="$TEST_ROOT/noninteractive-sudo.sh"
install -m 0644 "$ROOT/extensions/services/pixel-agent/host/cancellable-exec.sh" \
    "$exec_control_source"
install -m 0644 "$ROOT/extensions/services/pixel-agent/host/noninteractive-sudo.sh" \
    "$exec_control_sudo_source"
mkdir -m 0700 -p "$exec_control_home"
check _ods_pixel_install_exec_control "$owner" "$exec_control_home" \
    "$exec_control_source" "$exec_control_sudo_source"
check test -d "$exec_control_home/.openclaw"
check test "$(stat -c '%a' "$exec_control_home/.openclaw")" = 700
check test "$(stat -c '%a' "$exec_control_home/.openclaw/.ods-exec-control")" = 700
check test "$(stat -c '%a' "$exec_control_home/.openclaw/.ods-exec-control/cancellable-exec.sh")" = 500
check test "$(stat -c '%a' "$exec_control_home/.openclaw/.ods-exec-control/sudo")" = 500
exec_control_bad_home="$TEST_ROOT/exec-control-bad-home"
mkdir -m 0700 -p "$exec_control_bad_home/.openclaw" "$TEST_ROOT/exec-control-link-target"
ln -s "$TEST_ROOT/exec-control-link-target" \
    "$exec_control_bad_home/.openclaw/.ods-exec-control"
if _ods_pixel_install_exec_control "$owner" "$exec_control_bad_home" \
    "$exec_control_source" "$exec_control_sudo_source" >/dev/null 2>&1; then
    fail "symlink Pixel execution control root rejected"
else
    pass "symlink Pixel execution control root rejected"
fi
exec_control_bad_wrapper_home="$TEST_ROOT/exec-control-bad-wrapper-home"
mkdir -m 0700 -p "$exec_control_bad_wrapper_home/.openclaw/.ods-exec-control"
ln -s "$TEST_ROOT/exec-control-link-target" \
    "$exec_control_bad_wrapper_home/.openclaw/.ods-exec-control/cancellable-exec.sh"
if _ods_pixel_install_exec_control "$owner" "$exec_control_bad_wrapper_home" \
    "$exec_control_source" "$exec_control_sudo_source" >/dev/null 2>&1; then
    fail "symlink Pixel execution wrapper rejected"
else
    pass "symlink Pixel execution wrapper rejected"
fi
exec_control_bad_sudo_home="$TEST_ROOT/exec-control-bad-sudo-home"
mkdir -m 0700 -p "$exec_control_bad_sudo_home/.openclaw/.ods-exec-control"
ln -s "$TEST_ROOT/exec-control-link-target" \
    "$exec_control_bad_sudo_home/.openclaw/.ods-exec-control/sudo"
if _ods_pixel_install_exec_control "$owner" "$exec_control_bad_sudo_home" \
    "$exec_control_source" "$exec_control_sudo_source" >/dev/null 2>&1; then
    fail "symlink Pixel noninteractive sudo adapter rejected"
else
    pass "symlink Pixel noninteractive sudo adapter rejected"
fi

plugin_list_bin="$TEST_ROOT/openclaw-plugin-list"
cat > "$plugin_list_bin" <<SH
#!/usr/bin/env bash
printf '%s\n' '{"plugins":[{"id":"pixel-ods","status":"loaded","rootDir":"$plugin_tree","contracts":{"tools":["pixel_ods_status","pixel_ods_apps_list","pixel_ods_extensions", "pixel_ods_host_observe","pixel_ods_host_command_propose","pixel_ods_evidence_report","pixel_ods_evidence_readback","pixel_ods_research","pixel_ods_web_extract","pixel_ods_download_promote","pixel_ods_workspace_preview","pixel_ods_project_build","pixel_ods_workspace_artifact", "pixel_ods_workspace_bundle","pixel_ods_workspace_preview_inspect","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal", "pixel_ods_source_proposal", "pixel_ods_python_library_proposal", "pixel_ods_extension_request_status", "pixel_ods_extension_request_prepare", "pixel_ods_extension_request_advance", "pixel_ods_extension_request_retry"]}}]}'
SH
chmod 0755 "$plugin_list_bin"
check _ods_pixel_verify_plugin_loaded "$owner" "$home" "$plugin_list_bin" "$plugin_tree"
cat > "$plugin_list_bin" <<SH
#!/usr/bin/env bash
printf '%s\n' '{"plugins":[{"id":"pixel-ods","status":"blocked","rootDir":"$plugin_tree","contracts":{"tools":["pixel_ods_status","pixel_ods_apps_list","pixel_ods_extensions", "pixel_ods_host_observe","pixel_ods_host_command_propose","pixel_ods_evidence_report","pixel_ods_evidence_readback","pixel_ods_research","pixel_ods_web_extract","pixel_ods_download_promote","pixel_ods_workspace_preview","pixel_ods_project_build","pixel_ods_workspace_artifact", "pixel_ods_workspace_bundle","pixel_ods_workspace_preview_inspect","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal", "pixel_ods_source_proposal", "pixel_ods_python_library_proposal", "pixel_ods_extension_request_status", "pixel_ods_extension_request_prepare", "pixel_ods_extension_request_advance", "pixel_ods_extension_request_retry"]}}]}'
SH
if _ods_pixel_verify_plugin_loaded "$owner" "$home" "$plugin_list_bin" "$plugin_tree" >/dev/null 2>&1; then
    fail "blocked ODS Pixel plugin rejected"
else
    pass "blocked ODS Pixel plugin rejected"
fi

sandbox_recreate_bin="$TEST_ROOT/openclaw-sandbox-recreate"
cat > "$sandbox_recreate_bin" <<'SH'
#!/usr/bin/env bash
exit 0
SH
chmod 0755 "$sandbox_recreate_bin"
if (
    ods_pixel_run_as_owner() {
        if [[ "$3" == "$sandbox_recreate_bin" ]]; then
            [[ "$4 $5 $6 $7 $8" == "sandbox recreate --agent pixel --force" ]]
        elif [[ "$3 $4 $5 $6 $7" == "docker ps --all --quiet --filter" \
            && "$8" == 'name=^/pixel-sbx-agent-pixel-' ]]; then
            return 0
        else
            return 1
        fi
    }
    _ods_pixel_recreate_agent_sandbox "$owner" "$home" "$sandbox_recreate_bin"
); then
    pass "Pixel sandbox recreation proves no stale agent container remains"
else
    fail "Pixel sandbox recreation proves no stale agent container remains"
fi
if (
    ods_pixel_run_as_owner() {
        if [[ "$3" == "$sandbox_recreate_bin" ]]; then
            return 0
        fi
        printf '%s\n' deadbeefdead
    }
    _ods_pixel_recreate_agent_sandbox "$owner" "$home" "$sandbox_recreate_bin"
) >/dev/null 2>&1; then
    fail "Pixel sandbox recreation rejects a stale unregistered container"
else
    pass "Pixel sandbox recreation rejects a stale unregistered container"
fi

# Bootstrap's sandbox Node.js proof failure names the candidate to remove; only
# this run's final error counts, and a malformed tag is never echoed.
proof_log="$TEST_ROOT/pixel-proof.log"
printf '%s\n' '[pixel] Building the pinned Pixel sandbox candidate image' \
    '[pixel] ERROR: Sandbox runtime proof failed: pixel-sandbox-candidate:4.3.32-uid-1000 did not run the pinned Node.js 24.21.0 for this architecture without npm, npx or corepack. Remove it with docker image rm pixel-sandbox-candidate:4.3.32-uid-1000, then re-run with --apply.' \
    > "$proof_log"
check test "$(_ods_pixel_failed_sandbox_proof_candidate "$proof_log")" = pixel-sandbox-candidate:4.3.32-uid-1000
for proof_tail in '[pixel] ERROR: Building the Pixel sandbox image failed.' \
    '[pixel] ERROR: Sandbox runtime proof failed: pixel-sandbox-candidate:4.3.32-uid-1000;id did not run'; do
    printf '%s\n' "$proof_tail" > "$proof_log"
    if _ods_pixel_failed_sandbox_proof_candidate "$proof_log" >/dev/null; then
        fail "sandbox proof failure parsed from: $proof_tail"
    else
        pass "no sandbox candidate parsed from: $proof_tail"
    fi
done
{ printf '%s\n' '[pixel] ERROR: Sandbox runtime proof failed: pixel-sandbox-candidate:4.3.32-uid-1000 did not run'
  printf '[pixel] later line %s\n' 1 2 3 4 5; } > "$proof_log"
if _ods_pixel_failed_sandbox_proof_candidate "$proof_log" >/dev/null; then
    fail "an earlier run's sandbox proof failure was treated as this run's"
else
    pass "only the final lines of the Pixel log name a failed sandbox proof"
fi

# A bootstrap that succeeds without the sandbox Node.js label (a developer
# source older than 4.3.32) stops before apply.
node_label_root="$TEST_ROOT/node-label-pixel"
mkdir -p "$node_label_root"
printf '4.3.32\n' > "$node_label_root/VERSION"
for node_label in 24.21.0 '<no value>' ''; do
    if (
        ods_pixel_run_as_owner() {
            shift 2
            if [[ "$1" == docker ]]; then
                [[ "$*" == "docker image inspect --format {{index .Config.Labels \"org.osmantic.pixel.sandbox-node\"}} pixel-sandbox-candidate:4.3.32-uid-$(id -u "$owner")" \
                    && -n "$node_label" ]] || return 1
                printf '%s\n' "$node_label"
                return 0
            fi
            "$@"
        }
        _ods_pixel_sandbox_candidate_has_node "$owner" "$home" "$node_label_root"
    ); then
        node_label_result=accepted
    else
        node_label_result=refused
    fi
    if [[ "$node_label_result" == "$([[ "$node_label" == 24.21.0 ]] && echo accepted || echo refused)" ]]; then
        pass "sandbox candidate with label '$node_label' $node_label_result"
    else
        fail "sandbox candidate with label '$node_label' $node_label_result"
    fi
done

plugin_registry_bin="$TEST_ROOT/openclaw-plugin-registry"
cat > "$plugin_registry_bin" <<SH
#!/usr/bin/env bash
printf '%s\n' '{"refreshed":true,"registry":{"version":1,"refreshReason":"manual","plugins":[{"pluginId":"pixel-ods","enabled":true,"rootDir":"$plugin_tree","contributions":{"contracts":{"tools":["pixel_ods_apps_list","pixel_ods_extensions", "pixel_ods_host_observe","pixel_ods_host_command_propose","pixel_ods_evidence_report","pixel_ods_evidence_readback","pixel_ods_status","pixel_ods_research","pixel_ods_web_extract","pixel_ods_download_promote","pixel_ods_workspace_preview","pixel_ods_project_build","pixel_ods_workspace_artifact", "pixel_ods_workspace_bundle","pixel_ods_workspace_preview_inspect","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal", "pixel_ods_source_proposal", "pixel_ods_python_library_proposal", "pixel_ods_extension_request_status", "pixel_ods_extension_request_prepare", "pixel_ods_extension_request_advance", "pixel_ods_extension_request_retry"]}}}]}}'
SH
chmod 0755 "$plugin_registry_bin"
check _ods_pixel_refresh_plugin_registry "$owner" "$home" "$plugin_registry_bin" "$plugin_tree"
cat > "$plugin_registry_bin" <<SH
#!/usr/bin/env bash
printf '%s\n' '{"refreshed":true,"registry":{"version":1,"refreshReason":"manual","plugins":[{"pluginId":"pixel-ods","enabled":true,"rootDir":"$plugin_tree","contributions":{"contracts":{"tools":["pixel_ods_status"]}}}]}}'
SH
if _ods_pixel_refresh_plugin_registry "$owner" "$home" "$plugin_registry_bin" "$plugin_tree" >/dev/null 2>&1; then
    fail "stale ODS Pixel plugin registry rejected"
else
    pass "stale ODS Pixel plugin registry rejected"
fi

restart_probe="$TEST_ROOT/restart-probe"
mkdir -p "$restart_probe/pixel-root"
original_installed_gateway_port="$(declare -f _ods_pixel_installed_gateway_port)"
_ods_pixel_installed_gateway_port() {
    [[ "$1" == "$owner" && "$2" == "$home" ]] || return 1
    printf '%s\n' 18790
}
if (
    restart_state="$restart_probe/state"
    systemctl() {
        case "$*" in
            *"-p ProtectHome --value") printf "tmpfs\n"; return 0 ;;
            *"-p ProtectSystem --value") printf "strict\n"; return 0 ;;
        esac
        if [[ "$1" == show ]]; then
            if [[ -e "$restart_state" ]]; then
                printf '%s\n' 4242
            else
                : > "$restart_state"
                printf '%s\n' 0
            fi
        elif [[ "$1" == is-active ]]; then
            return 0
        else
            return 1
        fi
    }
    ods_sudo_available() { return 0; }
    ods_sudo() { [[ "$*" == "systemctl restart openclaw-gateway.service" ]]; }
    curl() {
        [[ "$*" == *"http://127.0.0.1:18790/health"* ]] || return 1
        printf '%s\n' '{"ok":true,"status":"live"}'
    }
    sleep() { :; }
    ods_pixel_run_as_owner() {
        [[ "$1" == "$owner" && "$2" == "$home" \
            && "$3" == "$restart_probe/pixel-root/pixel" && "$4" == verify ]]
    }
    PIXEL_GATEWAY_PORT=18789 _ods_pixel_restart_gateway_and_verify "$owner" "$home" "$restart_probe/pixel-root"
); then
    pass "privileged Pixel restart uses installed port and tolerates transient MainPID zero"
else
    fail "privileged Pixel restart uses installed port and tolerates transient MainPID zero"
fi

if (
    retry_state="$restart_probe/retry-state"
    retry_calls="$restart_probe/retry-calls"
    : > "$retry_calls"
    systemctl() {
        case "$*" in
            *"-p ProtectHome --value") printf "tmpfs\n"; return 0 ;;
            *"-p ProtectSystem --value") printf "strict\n"; return 0 ;;
        esac
        if [[ "$1" == show ]]; then
            if [[ -e "$retry_state" ]]; then
                printf '%s\n' 4343
            else
                : > "$retry_state"
                printf '%s\n' 0
            fi
        elif [[ "$1" == is-active ]]; then
            return 0
        else
            return 1
        fi
    }
    ods_sudo_available() { return 0; }
    ods_sudo() { [[ "$*" == "systemctl restart openclaw-gateway.service" ]]; }
    curl() {
        [[ "$*" == *"http://127.0.0.1:18790/health"* ]] || return 1
        printf '%s\n' '{"ok":true,"status":"live"}'
    }
    sleep() { :; }
    ods_pixel_run_as_owner() {
        [[ "$1" == "$owner" && "$2" == "$home" \
            && "$3" == "$restart_probe/pixel-root/pixel" && "$4" == verify ]] || return 1
        printf 'x' >> "$retry_calls"
        [[ "$(wc -c < "$retry_calls")" -ge 2 ]]
    }
    _ods_pixel_restart_gateway_and_verify "$owner" "$home" "$restart_probe/pixel-root" \
        && [[ "$(wc -c < "$retry_calls")" -eq 2 ]]
); then
    pass "Pixel restart retries one transient complete verification failure"
else
    fail "Pixel restart retries one transient complete verification failure"
fi

if (
    persistent_state="$restart_probe/persistent-state"
    persistent_calls="$restart_probe/persistent-calls"
    : > "$persistent_calls"
    systemctl() {
        case "$*" in
            *"-p ProtectHome --value") printf "tmpfs\n"; return 0 ;;
            *"-p ProtectSystem --value") printf "strict\n"; return 0 ;;
        esac
        if [[ "$1" == show ]]; then
            if [[ -e "$persistent_state" ]]; then
                printf '%s\n' 4444
            else
                : > "$persistent_state"
                printf '%s\n' 0
            fi
        elif [[ "$1" == is-active ]]; then
            return 0
        else
            return 1
        fi
    }
    ods_sudo_available() { return 0; }
    ods_sudo() { [[ "$*" == "systemctl restart openclaw-gateway.service" ]]; }
    curl() {
        [[ "$*" == *"http://127.0.0.1:18790/health"* ]] || return 1
        printf '%s\n' '{"ok":true,"status":"live"}'
    }
    sleep() { :; }
    ods_pixel_run_as_owner() {
        [[ "$1" == "$owner" && "$2" == "$home" \
            && "$3" == "$restart_probe/pixel-root/pixel" && "$4" == verify ]] || return 1
        printf 'x' >> "$persistent_calls"
        return 1
    }
    if _ods_pixel_restart_gateway_and_verify \
        "$owner" "$home" "$restart_probe/pixel-root" >/dev/null 2>&1; then
        return 1
    fi
    [[ "$(wc -c < "$persistent_calls")" -eq 3 ]]
); then
    pass "Pixel restart remains fail closed after bounded verification retries"
else
    fail "Pixel restart remains fail closed after bounded verification retries"
fi

if (
    systemctl() {
        case "$*" in
            *"-p ProtectHome --value") printf "tmpfs\n"; return 0 ;;
            *"-p ProtectSystem --value") printf "strict\n"; return 0 ;;
        esac
        [[ "$1" == show ]] && printf '%s\n' 0
    }
    ods_sudo_available() { return 1; }
    _ods_pixel_restart_gateway_and_verify "$owner" "$home" "$restart_probe/pixel-root"
) >/dev/null 2>&1; then
    fail "unprivileged Pixel restart still rejects a missing owned process"
else
    pass "unprivileged Pixel restart still rejects a missing owned process"
fi
eval "$original_installed_gateway_port"

ingress_restart_answers="$restart_probe/onboarding.json"
ingress_restart_status="$restart_probe/ods-status.json"
python3 - "$ingress_restart_answers" "$ingress_restart_status" <<'PY'
import json, pathlib, sys

answers, status = map(pathlib.Path, sys.argv[1:])
answers.write_text(json.dumps({
    "modelProvider": "ods-gateway",
    "modelId": "ods/current",
    "modelName": "ODS Current (org/model:variant)",
    "modelContextWindow": 2_000_000,
}) + "\n")
status.write_text(json.dumps({
    "runtime": {"model": "org/model:variant", "context_length": 2_000_000},
}) + "\n")
PY
chmod 0600 "$ingress_restart_answers" "$ingress_restart_status"
if (
    ingress_restart_state="$restart_probe/ingress-restarted"
    systemctl() {
        case "$*" in
            *"-p ProtectHome --value") printf "tmpfs\n"; return 0 ;;
            *"-p ProtectSystem --value") printf "strict\n"; return 0 ;;
        esac
        if [[ "$1" == is-active ]]; then
            return 0
        fi
        if [[ "$1" == show && "$3" == -p && "$4" == MainPID ]]; then
            [[ -e "$ingress_restart_state" ]] && printf '%s\n' 222 || printf '%s\n' 111
            return 0
        fi
        return 1
    }
    ods_sudo_available() { return 0; }
    ods_sudo() {
        [[ "$*" == "systemctl restart pixel-ingress.service" ]] || return 1
        : > "$ingress_restart_state"
    }
    _ods_pixel_wait_ingress() { return 0; }
    ods_pixel_run_as_owner() {
        local run_owner="$1" run_home="$2"
        shift 2
        [[ "$run_owner" == "$owner" && "$run_home" == "$home" ]] || return 1
        if [[ "$1" == python3 && "$4" == /run/ods-pixel/ods-status.json ]]; then
            set -- "$1" "$2" "$3" "$ingress_restart_status"
        fi
        "$@"
    }
    _ods_pixel_restart_ingress_and_verify "$owner" "$home" "$ingress_restart_answers"
); then
    pass "Pixel ingress restart refreshes and verifies the selected runtime"
else
    fail "Pixel ingress restart refreshes and verifies the selected runtime"
fi

source_fixture="$TEST_ROOT/pixel-source-fixture"
mkdir -p "$source_fixture"
git -C "$source_fixture" init -q
printf '%s\n' fixture > "$source_fixture/pixel"
git -C "$source_fixture" add pixel
git -C "$source_fixture" -c user.name=test -c [email protected] commit -qm fixture
PIXEL_SOURCE_URL="$source_fixture"
PIXEL_SOURCE_REF="$(git -C "$source_fixture" rev-parse HEAD)"
source_checkout="$TEST_ROOT/pixel-checkouts/source-$PIXEL_SOURCE_REF"
saved_umask="$(umask)"
umask 0002
check test "$(_ods_pixel_source_checkout "$owner" "$home" "$source_checkout")" = "$source_checkout"
umask "$saved_umask"
check test "$(git -C "$source_checkout" rev-parse HEAD)" = "$PIXEL_SOURCE_REF"
check test -z "$(git -C "$source_checkout" status --porcelain)"
check test "$(stat -c '%a' "$source_checkout/pixel")" = 644

previous_source_ref="$PIXEL_SOURCE_REF"
printf '%s\n' next >"$source_fixture/next"
git -C "$source_fixture" add next
git -C "$source_fixture" -c user.name=test -c [email protected] commit -qm next
requested_source_ref="$(git -C "$source_fixture" rev-parse HEAD)"
transition_home="$TEST_ROOT/transition-home"
transition_install="$TEST_ROOT/transition-ods"
mkdir -p "$transition_home/.config/ods" "$transition_install"
cat >"$transition_home/.config/ods/pixel-managed.json" <<JSON
{"schema_version":2,"manager":"ods","state":"installing","initial_active_state":"absent","install_dir":"$transition_install","pixel_source_ref":"$previous_source_ref"}
JSON
chmod 0600 "$transition_home/.config/ods/pixel-managed.json"
if (
    INSTALL_DIR="$transition_install"
    PIXEL_SOURCE_URL="$source_fixture"
    PIXEL_SOURCE_REF="$requested_source_ref"
    restored="$(_ods_pixel_restore_transition_source \
        "$owner" "$transition_home" "$requested_source_ref")"
    [[ "$restored" == "$transition_install/data/pixel/source-$previous_source_ref" \
        && "$(git -C "$restored" rev-parse HEAD)" == "$previous_source_ref" \
        && -z "$(git -C "$restored" status --porcelain)" \
        && "$PIXEL_SOURCE_REF" == "$requested_source_ref" ]]
); then
    pass "missing prior Pixel validation source is reconstructed from the authorized exact history"
else
    fail "missing prior Pixel validation source was not reconstructed safely"
fi

# A normal upgrade has no shell-level Pixel ref. Its persisted bundled ref is
# the old release, so the new verified bundle must advance it before the
# pre-copy transition verifies the old checkout and acquires a source hold.
printf 'PIXEL_SOURCE_URL=bundled\nPIXEL_SOURCE_REF=%s\n' \
    "$previous_source_ref" > "$transition_install/.env"
phase06_pre_copy="$(sed -n '/^    _env_existing=""/,/^    unset _phase06_pixel_marker _phase06_pixel_source_transition/p' \
    "$ROOT/installers/phases/06-directories.sh")"
_phase06_pre_copy_fixture() { eval "$phase06_pre_copy"; }
_phase06_source_hold_fixture() {
    # Disposable adapters: exercise the literal phase without sudo, services,
    # source replacement or the protected coordinator on the test host.
    ods_sudo() { return 1; }
    _ods_pixel_openclaw_bin() { printf '%s\n' /fixture/openclaw; }
    _ods_pixel_install_access_service() { printf '%s\n' guard >> "$INSTALL_DIR/upgrade-steps"; }
    _ods_pixel_source_upgrade() {
        printf '%s\n' "$1" >> "$INSTALL_DIR/upgrade-steps"
        case "$1" in
            status) printf '%s\n' '{"transaction":null,"phase":"staged"}' ;;
            hold) printf '%064d\n' 1 ;;
            stage|copy|downstream) ;;
            *) return 1 ;;
        esac
    }
}
if (
    unset PIXEL_SOURCE_URL PIXEL_SOURCE_REF PIXEL_SOURCE_DIR
    HOME="$transition_home"
    INSTALL_DIR="$transition_install"
    SCRIPT_DIR="$ROOT"
    ENABLE_PIXEL_RUNTIME=true
    ai() { :; }
    error() { printf '%s\n' "$*" >&2; return 1; }
    _phase06_step() { :; }
    ods_pixel_install_owner() { printf '%s\n' "$owner"; }
    ods_pixel_owner_home() { printf '%s\n' "$transition_home"; }
    ods_pixel_uninstall_managed() { : > "$transition_install/retired"; }
    _phase06_source_hold_fixture
    source "$ROOT/lib/safe-env.sh"
    _phase06_pre_copy_fixture
    [[ "$_phase06_requested_pixel_url" == bundled \
        && "$_phase06_requested_pixel_ref" == "$ODS_PIXEL_BUNDLED_REF" \
        && ! -e "$transition_install/retired" \
        && "$(cat "$INSTALL_DIR/upgrade-steps")" == $'stage\nstatus\nguard\nhold\ncopy\ndownstream' \
        && "$ODS_PIXEL_SOURCE_TRANSACTION" == "$(printf '%064d' 1)" ]]
); then
    pass "persisted old bundled Pixel ref advances and transitions before source copy"
else
    fail "persisted old bundled Pixel ref did not advance and transition"
fi
if (
    PIXEL_SOURCE_REF="$previous_source_ref"
    HOME="$TEST_ROOT/no-marker-home"
    INSTALL_DIR="$transition_install"
    SCRIPT_DIR="$ROOT"
    ENABLE_PIXEL_RUNTIME=true
    ai() { :; }
    _phase06_step() { :; }
    error() { :; return 1; }
    source "$ROOT/lib/safe-env.sh"
    _phase06_pre_copy_fixture
); then
    fail "an explicit old bundled Pixel pin was silently advanced"
else
    pass "an explicit old bundled Pixel pin fails closed"
fi
printf 'PIXEL_SOURCE_URL=https://github.com/Osmantic/Pixel.git\nPIXEL_SOURCE_REF=%s\n' \
    b33730436baf5d98bf58f7d57c090318fe19f433 > "$transition_install/.env"
if (
    unset PIXEL_SOURCE_URL PIXEL_SOURCE_REF PIXEL_SOURCE_DIR
    HOME="$TEST_ROOT/no-marker-home"
    INSTALL_DIR="$transition_install"
    SCRIPT_DIR="$ROOT"
    ENABLE_PIXEL_RUNTIME=true
    ai() { :; }
    _phase06_step() { :; }
    error() { printf '%s\n' "$*" >&2; return 1; }
    source "$ROOT/lib/safe-env.sh"
    _phase06_pre_copy_fixture
    [[ "$_phase06_requested_pixel_url" == bundled \
        && "$_phase06_requested_pixel_ref" == "$ODS_PIXEL_BUNDLED_REF" ]]
); then
    pass "old canonical Pixel environment pin resolves to bundle before source copy"
else
    fail "old canonical Pixel environment pin did not migrate before source copy"
fi
legacy_ref=b33730436baf5d98bf58f7d57c090318fe19f433
legacy_install="$TEST_ROOT/legacy-ods"
legacy_home="$TEST_ROOT/legacy-home"
legacy_checkout="$legacy_install/data/pixel/source-$legacy_ref"
mkdir -p "$legacy_home/.config/ods" "$legacy_checkout/.git"
printf 'PIXEL_SOURCE_URL=https://github.com/Osmantic/Pixel.git\nPIXEL_SOURCE_REF=%s\n' \
    "$legacy_ref" > "$legacy_install/.env"
printf '{"schema_version":2,"manager":"ods","state":"ready","initial_active_state":"absent","install_dir":"%s","pixel_source_ref":"%s"}\n' \
    "$legacy_install" "$legacy_ref" > "$legacy_home/.config/ods/pixel-managed.json"
chmod 0600 "$legacy_home/.config/ods/pixel-managed.json"
if (
    unset PIXEL_SOURCE_URL PIXEL_SOURCE_REF PIXEL_SOURCE_DIR
    HOME="$legacy_home"
    INSTALL_DIR="$legacy_install"
    SCRIPT_DIR="$ROOT"
    ENABLE_PIXEL_RUNTIME=true
    ai() { :; }
    error() { printf '%s\n' "$*" >&2; return 1; }
    _phase06_step() { :; }
    ods_pixel_install_owner() { printf '%s\n' "$owner"; }
    ods_pixel_owner_home() { printf '%s\n' "$legacy_home"; }
    _ods_pixel_source_checkout() {
        [[ "$PIXEL_SOURCE_URL" == "$legacy_checkout" \
            && "$PIXEL_SOURCE_REF" == "$legacy_ref" \
            && "$3" == "$legacy_checkout" ]] || return 1
        : > "$legacy_install/old-checkout-verified"
        printf '%s\n' "$legacy_checkout"
    }
    ods_pixel_uninstall_managed() { : > "$legacy_install/retired"; }
    _phase06_source_hold_fixture
    source "$ROOT/lib/safe-env.sh"
    _phase06_pre_copy_fixture
    [[ "$_phase06_requested_pixel_url" == bundled \
        && "$_phase06_requested_pixel_ref" == "$ODS_PIXEL_BUNDLED_REF" \
        && -f "$legacy_install/old-checkout-verified" \
        && ! -e "$legacy_install/retired" \
        && "$(cat "$INSTALL_DIR/upgrade-steps")" == $'stage\nstatus\nguard\nhold\ncopy\ndownstream' ]]
); then
    pass "canonical legacy environment verifies local checkout and holds source upgrade"
else
    fail "canonical legacy environment did not safely hold its managed Pixel source upgrade"
fi
printf 'PIXEL_SOURCE_URL=https://example.invalid/custom-pixel.git\nPIXEL_SOURCE_REF=%s\n' \
    b33730436baf5d98bf58f7d57c090318fe19f433 > "$transition_install/.env"
if (
    unset PIXEL_SOURCE_URL PIXEL_SOURCE_REF PIXEL_SOURCE_DIR
    HOME="$TEST_ROOT/no-marker-home"
    INSTALL_DIR="$transition_install"
    SCRIPT_DIR="$ROOT"
    ENABLE_PIXEL_RUNTIME=true
    ai() { :; }
    _phase06_step() { :; }
    error() { :; return 1; }
    source "$ROOT/lib/safe-env.sh"
    _phase06_pre_copy_fixture
); then
    fail "custom remote Pixel source was silently replaced by the ODS bundle"
else
    pass "custom remote Pixel source fails closed instead of silently changing releases"
fi
printf 'PIXEL_SOURCE_URL=bundled\nPIXEL_SOURCE_REF=%s\n' \
    "$ODS_PIXEL_BUNDLED_REF" > "$transition_install/.env"
rm -f -- "$transition_install/retired"
mv -- "$transition_install/data/pixel/source-$previous_source_ref" \
    "$transition_install/data/pixel/source-$previous_source_ref.saved"
if (
    unset PIXEL_SOURCE_URL PIXEL_SOURCE_REF PIXEL_SOURCE_DIR
    HOME="$transition_home"
    INSTALL_DIR="$transition_install"
    SCRIPT_DIR="$ROOT"
    ENABLE_PIXEL_RUNTIME=true
    ai() { :; }
    error() { printf '%s\n' "$*" >&2; return 1; }
    _phase06_step() { :; }
    ods_pixel_install_owner() { printf '%s\n' "$owner"; }
    ods_pixel_owner_home() { printf '%s\n' "$transition_home"; }
    ods_pixel_uninstall_managed() { : > "$transition_install/retired"; }
    ods_sudo() { return 1; }
    source "$ROOT/lib/safe-env.sh"
    _phase06_pre_copy_fixture
); then
    fail "missing prior Pixel checkout was accepted for retirement"
elif [[ ! -e "$transition_install/retired" ]]; then
    pass "missing prior Pixel checkout fails before retirement or source copy"
else
    fail "missing prior Pixel checkout retired the installed release"
fi
mv -- "$transition_install/data/pixel/source-$previous_source_ref.saved" \
    "$transition_install/data/pixel/source-$previous_source_ref"
if (
    INSTALL_DIR="$transition_install"
    PIXEL_SOURCE_URL=bundled
    PIXEL_SOURCE_REF="$requested_source_ref"
    restored="$(_ods_pixel_restore_transition_source \
        "$owner" "$transition_home" "$requested_source_ref")"
    [[ "$restored" == "$transition_install/data/pixel/source-$previous_source_ref" \
        && "$(git -C "$restored" rev-parse HEAD)" == "$previous_source_ref" \
        && -z "$(git -C "$restored" status --porcelain)" ]]
); then
    pass "prior Pixel checkout verifies locally even with bundled target source"
else
    fail "prior Pixel checkout verification still depends on the former remote source"
fi

sudo_mask_probe="$TEST_ROOT/sudo-mask-probe"
if (
    ods_sudo_available() { return 0; }
    ods_sudo() {
        [[ "$1" == -u && "$3" == -- ]] || return 1
        shift 3
        umask 0002
        "$@"
    }
    ods_pixel_run_as_owner_with_umask "$owner" "$home" 0022 sh -c \
        'printf probe > "$1"' sh "$sudo_mask_probe"
); then
    check test "$(stat -c '%a' "$sudo_mask_probe")" = 644
else
    fail "owner process umask survives sudo policy reset"
fi

cat > "$mock_bin/git" <<'SH'
#!/usr/bin/env bash
sleep 10
SH
chmod +x "$mock_bin/git"
PIXEL_SOURCE_URL="https://github.com/Osmantic/Pixel.git"
PIXEL_SOURCE_REF="$previous_source_ref"
if _ods_pixel_source_checkout "$owner" "$home" "$source_checkout" >/dev/null 2>&1; then
    fail "remote Pixel source is rejected even with an existing checkout"
else
    pass "remote Pixel source is rejected before reusing an existing checkout"
fi
PIXEL_SOURCE_URL="$source_fixture"
PIXEL_SOURCE_REF="$(printf 'f%.0s' {1..40})"
timed_checkout="$TEST_ROOT/timed-checkouts/source-$PIXEL_SOURCE_REF"
if PATH="$mock_bin:$PATH" ODS_PIXEL_SOURCE_TIMEOUT_SECONDS=1 \
    _ods_pixel_source_checkout "$owner" "$home" "$timed_checkout" >/dev/null 2>&1; then
    fail "hung Pixel source clone is bounded and rejected"
elif [[ ! -e "$timed_checkout" ]] \
    && ! find "${timed_checkout%/*}" -mindepth 1 -print -quit | grep -q .; then
    pass "hung Pixel source clone is bounded and leaves no partial checkout"
else
    fail "failed Pixel source clone left a partial checkout"
fi

PIXEL_SOURCE_URL="https://github.com/Osmantic/Pixel.git"
if _ods_pixel_source_checkout "$owner" "$home" "$timed_checkout" >/dev/null 2>&1; then
    fail "private Pixel source is rejected before checkout"
elif [[ ! -e "$timed_checkout" ]]; then
    pass "private Pixel source is rejected before checkout"
else
    fail "private Pixel source rejection left a checkout"
fi

answers="$TEST_ROOT/onboarding.json"
export MAX_CONTEXT=32768
export LLM_MODEL=qwen-test
export LLAMA_REASONING=off
export OLLAMA_PORT=11434
export SEARXNG_PORT=8888
export LITELLM_PORT=4000
export LITELLM_KEY=test-litellm-secret
export PIXEL_MODEL_RELAY_PORT=4006
export PIXEL_MODEL_RELAY_KEY=test-pixel-relay-secret
export ODS_MODEL_SWITCHBOARD=observe
digest="$(printf 'a%.0s' {1..64})"
mkdir -p "$INSTALL_DIR/bin" "$INSTALL_DIR/config" \
    "$INSTALL_DIR/extensions/library/services/notebook" \
    "$INSTALL_DIR/extensions/services/pixel-agent/host" "$INSTALL_DIR/data/pixel"
  cp "$ROOT/bin/ods-pixel-approve" "$INSTALL_DIR/bin/ods-pixel-approve"
  cp "$ROOT/extensions/services/pixel-agent/host/extension_search.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/extension_search.py"
  cp "$ROOT/extensions/services/pixel-agent/host/extension_manager.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/extension_manager.py"
  cp "$ROOT/extensions/services/pixel-agent/host/pixel-extension-manager.service" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-extension-manager.service"
  cp "$ROOT/extensions/services/pixel-agent/host/artifact_promoter.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/artifact_promoter.py"
  cp "$ROOT/extensions/services/pixel-agent/host/pixel-artifact-promoter.service" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-artifact-promoter.service"
  cp "$ROOT/extensions/services/pixel-agent/host/workspace_preview.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/workspace_preview.py"
  cp "$ROOT/extensions/services/pixel-agent/host/system_observe.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/system_observe.py"
  cp "$ROOT/extensions/services/pixel-agent/host/unix_peer.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/unix_peer.py"
  cp "$ROOT/extensions/services/pixel-agent/host/pixel-workspace-preview.service" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-workspace-preview.service"
  cp "$ROOT/extensions/services/pixel-agent/host/pixel-ops-broker-ods.conf" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-ops-broker-ods.conf"
  chmod 0644 "$INSTALL_DIR/extensions/services/pixel-agent/host/extension_search.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/extension_manager.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-extension-manager.service" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/artifact_promoter.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-artifact-promoter.service" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/workspace_preview.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/system_observe.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/unix_peer.py" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-workspace-preview.service" \
      "$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-ops-broker-ods.conf"
  chmod 0755 "$INSTALL_DIR/bin/ods-pixel-approve"
printf '%s\n' 'services: {}' >"$INSTALL_DIR/extensions/library/services/notebook/compose.yaml"
cat >"$INSTALL_DIR/config/extensions-catalog.json" <<'JSON'
{"schema_version":"1.0.0","extensions":[{"id":"notebook","name":"Notebook Lab","description":"Private notebooks for data science.","category":"optional","gpu_backends":["nvidia","amd"],"compose_file":"compose.yaml","depends_on":["litellm"],"env_vars":[{"key":"NOTEBOOK_TOKEN","required":true},{"key":"NOTEBOOK_THEME","required":false}],"tags":["notebook","data-science"],"features":[{"name":"Interactive Notebooks"}]},{"id":"reference-only","name":"Reference Only","description":"Not currently deployable.","category":"optional","gpu_backends":[],"compose_file":"compose.yaml","depends_on":[],"env_vars":[],"tags":[],"features":[]}]}
JSON
extension_catalog="$INSTALL_DIR/data/pixel/extension-catalog.json"
extension_manager_unit="$INSTALL_DIR/data/pixel/extension-manager.service"
artifact_promoter_unit="$INSTALL_DIR/data/pixel/artifact-promoter.service"
workspace_preview_unit="$INSTALL_DIR/data/pixel/workspace-preview.service"
_ods_pixel_write_extension_catalog "$owner" "$home" "$extension_catalog"
_ods_pixel_write_extension_manager_unit "$owner" "$home" "$extension_manager_unit"
_ods_pixel_write_artifact_promoter_unit "$owner" "$home" "$artifact_promoter_unit"
_ods_pixel_write_workspace_preview_unit "$owner" "$home" "$workspace_preview_unit" 9437
check test "$(stat -c '%a' "$extension_catalog")" = 600
check test "$(stat -c '%a' "$extension_manager_unit")" = 600
check test "$(stat -c '%a' "$artifact_promoter_unit")" = 600
check test "$(stat -c '%a' "$workspace_preview_unit")" = 600
check grep -F "User=$owner" "$extension_manager_unit"
check grep -F "Group=pixel-ops" "$extension_manager_unit"
check grep -F "${INSTALL_DIR}/.env" "$extension_manager_unit"
check grep -F " 3002" "$extension_manager_unit"
check grep -F "CapabilityBoundingSet=" "$extension_manager_unit"
check grep -F "ProtectSystem=strict" "$extension_manager_unit"
check grep -F "ProtectProc=invisible" "$extension_manager_unit"
check grep -F "RestrictNamespaces=true" "$extension_manager_unit"
check grep -F "ReadOnlyPaths=/var/lib/pixel-ops-broker/results" "$extension_manager_unit"
check grep -F "InaccessiblePaths=/var/lib/pixel-ops-broker/plans" "$extension_manager_unit"
if grep -F "__PIXEL_" "$artifact_promoter_unit" >/dev/null; then
    fail "artifact promoter placeholders resolved"
else
    pass "artifact promoter placeholders resolved"
fi
check grep -F "User=root" "$artifact_promoter_unit"
check grep -F "$home/.openclaw/workspace-pixel" "$artifact_promoter_unit"
check grep -F "RestrictAddressFamilies=AF_UNIX" "$artifact_promoter_unit"
check grep -F "CapabilityBoundingSet=CAP_CHOWN CAP_DAC_OVERRIDE" "$artifact_promoter_unit"
check grep -F "ReadOnlyPaths=/var/lib/pixel-ops-broker/results /var/lib/pixel-ops-broker/artifacts" "$artifact_promoter_unit"
check grep -F "User=$owner" "$workspace_preview_unit"
check grep -F "Group=ods-pixel" "$workspace_preview_unit"
check grep -F " 9437" "$workspace_preview_unit"
check grep -F "RuntimeDirectoryMode=0750" "$workspace_preview_unit"
check grep -F "BindReadOnlyPaths=\"$home/.openclaw/workspace-pixel\"" "$workspace_preview_unit"
check grep -F "RestrictAddressFamilies=AF_UNIX AF_INET" "$workspace_preview_unit"
check grep -F "IPAddressAllow=localhost" "$workspace_preview_unit"
check grep -F 'HTTP_SOCKET_PATH = pathlib.Path("/run/ods-pixel-preview/http.sock")' \
    "$INSTALL_DIR/extensions/services/pixel-agent/host/workspace_preview.py"
check python3 -c '
import importlib.util,json,sys
catalog=json.load(open(sys.argv[1]))
assert catalog["schemaVersion"] == 1 and catalog["kind"] == "ods-pixel-extension-catalog"
assert len(catalog["sourceSha256"]) == 64
assert catalog["extensions"] == [{"catalogSource":"library","configurationScope":"declared-environment-keys","category":"optional","dependsOn":["litellm"],"description":"Private notebooks for data science.","featureNames":["Interactive Notebooks"],"gpuBackends":["amd","nvidia"],"id":"notebook","name":"Notebook Lab","optionalConfiguration":["NOTEBOOK_THEME"],"requiredConfiguration":["NOTEBOOK_TOKEN"],"tags":["data-science","notebook"]}]
spec=importlib.util.spec_from_file_location("extension_search",sys.argv[2]); module=importlib.util.module_from_spec(spec); spec.loader.exec_module(module)
assert [item["id"] for item in module._matches(catalog["extensions"],"data science")] == ["notebook"]
assert [item["id"] for item in module._matches(catalog["extensions"],"all")] == ["notebook"]
' "$extension_catalog" "$INSTALL_DIR/extensions/services/pixel-agent/host/extension_search.py"
printf '%s\n' '{}' >"$TEST_ROOT/extension-catalog-symlink-target.json"
ln -s "$TEST_ROOT/extension-catalog-symlink-target.json" "$TEST_ROOT/linked-extension-catalog.json"
if _ods_pixel_write_extension_catalog "$owner" "$home" "$TEST_ROOT/linked-extension-catalog.json" >/dev/null 2>&1; then
    fail "symlink extension catalog rejected"
else
    pass "symlink extension catalog rejected"
fi
printf '%s\n' 'unsafe' >"$TEST_ROOT/extension-manager-unit-target.service"
ln -s "$TEST_ROOT/extension-manager-unit-target.service" \
    "$TEST_ROOT/linked-extension-manager.service"
if _ods_pixel_write_extension_manager_unit "$owner" "$home" \
    "$TEST_ROOT/linked-extension-manager.service" >/dev/null 2>&1; then
    fail "symlink extension manager unit rejected"
else
    pass "symlink extension manager unit rejected"
fi
printf '%s\n' 'unsafe' >"$TEST_ROOT/artifact-promoter-unit-target.service"
ln -s "$TEST_ROOT/artifact-promoter-unit-target.service" \
    "$TEST_ROOT/linked-artifact-promoter.service"
if _ods_pixel_write_artifact_promoter_unit "$owner" "$home" \
    "$TEST_ROOT/linked-artifact-promoter.service" >/dev/null 2>&1; then
    fail "symlink artifact promoter unit rejected"
else
    pass "symlink artifact promoter unit rejected"
fi
printf '%s\n' 'unsafe' >"$TEST_ROOT/workspace-preview-unit-target.service"
ln -s "$TEST_ROOT/workspace-preview-unit-target.service" \
    "$TEST_ROOT/linked-workspace-preview.service"
if _ods_pixel_write_workspace_preview_unit "$owner" "$home" \
    "$TEST_ROOT/linked-workspace-preview.service" 9437 >/dev/null 2>&1; then
    fail "symlink workspace preview unit rejected"
else
    pass "symlink workspace preview unit rejected"
fi
operations_policy="$TEST_ROOT/operations-policy.json"
_ods_pixel_write_operations_policy "$owner" "$home" "$operations_policy"
check test "$(stat -c '%a' "$operations_policy")" = 600
check python3 -c '
import json,pathlib,shutil,socket,sys
v=json.load(open(sys.argv[1]))
assert v["schemaVersion"] == 2 and v["deployment"] == "ods-default"
assert v["download"]["stagingRoot"] == "/var/lib/pixel-ops-broker/artifacts"
assert v["download"]["maxBytes"] == 536870912 and v["download"]["maxRedirects"] == 5
assert {"example.com","github.com","githubusercontent.com","hf.co","huggingface.co","nodejs.org","npmjs.org","pypi.org","pythonhosted.org"} == set(v["download"]["allowedDomains"])
target=v["targets"]["ods-host"]
assert target["backend"] == "local" and target["expectedHostname"] == socket.gethostname()
assert target["allowRaw"] is True and target["writableRoots"] == [sys.argv[3]]
assert target["defaultCwd"] == "/var/lib/pixel-ops-broker"
assert target["allowedRoots"] == [sys.argv[2],sys.argv[3],"/var/lib/pixel-ops-broker","/run/ods-pixel-manager"]
assert set(target["capabilities"]) == {"inspect","manage-extensions","stage-download","approved-host-command"}
broker=v["targets"]["broker"]
assert broker["backend"] == "local" and broker["environment"] == "lab"
assert broker["expectedHostname"] == socket.gethostname() and broker["allowRaw"] is False
assert broker["allowedRoots"] == ["/var/lib/pixel-ops-broker"]
assert broker["writableRoots"] == ["/var/lib/pixel-ops-broker/artifacts"]
host_inventory={"host.uptime","host.processes","host.services","host.cpu","host.gpu","host.memory","host.storage","host.network-addresses","host.network-routes","host.listening-ports","host.tailscale","host.network-peer"}
assert set(v["actions"]) == {"host.identity","host.kernel","host.architecture","host.platform","host.os-release",*host_inventory,"ods.extensions.search","ods.extensions.list","ods.extensions.github-inspect","ods.extensions.github-file","ods.extensions.github-validate","ods.extensions.github-validate-parts","ods.extensions.github-draft-read","ods.extensions.github-draft-save","ods.extensions.github-draft-save-parts","ods.extensions.github-draft-prepare","ods.extensions.inspect","ods.extensions.install","ods.extensions.install-next","ods.extensions.enable","ods.extensions.disable","ods.extensions.remove"}
for name in {"host.identity","host.kernel","host.architecture","host.platform","host.os-release",*host_inventory,"ods.extensions.search","ods.extensions.list","ods.extensions.github-inspect","ods.extensions.github-file","ods.extensions.github-validate","ods.extensions.github-validate-parts","ods.extensions.github-draft-read","ods.extensions.inspect"}:
    assert v["actions"][name]["tier"] == "read" and v["actions"][name]["defaultAuthority"] == "observe"
hostname_binary=str(pathlib.Path(shutil.which("hostname")).resolve())
uname_binary=str(pathlib.Path(shutil.which("uname")).resolve())
uptime_binary=str(pathlib.Path(shutil.which("uptime")).resolve())
cat_binary=str(pathlib.Path(shutil.which("cat")).resolve())
assert v["actions"]["host.identity"]["argv"] == [hostname_binary]
assert v["actions"]["host.kernel"]["argv"] == [uname_binary, "-sr"]
assert v["actions"]["host.architecture"]["argv"] == [uname_binary, "-m"]
assert v["actions"]["host.platform"]["argv"] == [uname_binary, "-a"]
assert v["actions"]["host.uptime"]["argv"] == [uptime_binary]
assert v["actions"]["host.os-release"]["argv"] == [cat_binary, "/etc/os-release"]
assert pathlib.Path(v["actions"]["host.processes"]["argv"][0]).name == "ps"
assert v["actions"]["host.processes"]["argv"][1:] == ["-eo","pid=,ppid=,user=,stat=,%cpu=,%mem=,comm=","--sort=-%cpu"]
assert pathlib.Path(v["actions"]["host.services"]["argv"][0]).name == "systemctl"
assert v["actions"]["host.cpu"]["argv"][1:] == ["--json"]
assert v["actions"]["host.gpu"]["argv"] == [str(pathlib.Path("/usr/bin/python3").resolve()), "/usr/local/libexec/ods-pixel-system-observe.py", "gpu"]
assert v["actions"]["host.memory"]["argv"][1:] == ["--bytes"]
assert v["actions"]["host.storage"]["argv"][1:] == ["--block-size=1","--output=fstype,size,used,avail,pcent,target"]
assert v["actions"]["host.network-addresses"]["argv"][1:] == ["-j","address","show"]
assert v["actions"]["host.network-routes"]["argv"][1:] == ["-j","route","show"]
assert v["actions"]["host.listening-ports"]["argv"][1:] == ["-H","-lntu"]
assert v["actions"]["host.tailscale"]["argv"] == [str(pathlib.Path("/usr/bin/python3").resolve()), "/usr/local/libexec/ods-pixel-system-observe.py", "tailscale"]
network_peer=v["actions"]["host.network-peer"]
assert network_peer["parameters"] == {
    "peer":{"pattern":"^[A-Za-z0-9.:-]{1,253}$","maxLength":253},
    "ports":{"pattern":"^[0-9,]{1,47}$","maxLength":47},
}
assert network_peer["argv"] == [str(pathlib.Path("/usr/bin/python3").resolve()), "/usr/local/libexec/ods-pixel-system-observe.py", "network-peer", "{peer}", "{ports}"]
extension_search=v["actions"]["ods.extensions.search"]
assert extension_search["parameters"] == {"query":{"pattern":"^[A-Za-z0-9 _/+:#.-]{1,80}$","maxLength":80}}
assert extension_search["argv"] == [str(pathlib.Path("/usr/bin/python3").resolve()),"/opt/pixel-ops-broker/ods-extension-search.py","/opt/pixel-ops-broker/ods-extension-catalog.json","{query}"]
assert v["actions"]["ods.extensions.list"]["argv"] == [str(pathlib.Path("/usr/bin/python3").resolve()),"/opt/pixel-ops-broker/ods-extension-manager.py","client","/run/ods-pixel-manager/extension-manager.sock","list","all"]
parameter={"serviceId":{"pattern":"^([a-z0-9]|[a-z0-9][a-z0-9._-]{0,62}[a-z0-9])$","maxLength":64}}
for action in ("inspect","install","install-next","enable","disable","remove"):
    item=v["actions"][f"ods.extensions.{action}"]
    assert item["parameters"] == parameter
    assert item["argv"] == [str(pathlib.Path("/usr/bin/python3").resolve()),"/opt/pixel-ops-broker/ods-extension-manager.py","client","/run/ods-pixel-manager/extension-manager.sock",action,"{serviceId}"]
assert v["actions"]["ods.extensions.install"] | {"tier":"managed","effect":"manage","defaultAuthority":"propose","idempotent":True,"reversible":True,"rollbackAction":"ods.extensions.remove","verificationAction":"ods.extensions.inspect","timeoutSeconds":900,"exclusiveTarget":True} == v["actions"]["ods.extensions.install"]
assert v["actions"]["ods.extensions.enable"]["rollbackAction"] == "ods.extensions.disable"
coordinator=v["actions"]["ods.extensions.install-next"]
assert coordinator["defaultAuthority"] == "propose" and coordinator["exclusiveTarget"] is True
assert coordinator["reversible"] is False and "rollbackAction" not in coordinator
assert coordinator["timeoutSeconds"] == 240
assert v["actions"]["ods.extensions.inspect"]["timeoutSeconds"] == 90
preparation = v["actions"]["ods.extensions.github-draft-prepare"]
assert preparation["defaultAuthority"] == "propose" and preparation["effect"] == "manage"
assert preparation["argv"][-3:] == ["repository-draft-prepare", "/run/ods-pixel-manager/extension-manager.sock", "{draftId}"]
assert preparation["parameters"]["draftId"]["maxLength"] == 64
recovery_action = v["actions"]["ods.extensions.github-draft-read"]
assert recovery_action["argv"][-3:] == ["repository-draft-read", "/run/ods-pixel-manager/extension-manager.sock", "{draftId}"]
assert recovery_action["parameters"]["draftId"]["pattern"] == "^[a-f0-9]{64}$"
draft_action = v["actions"]["ods.extensions.github-draft-save"]
assert draft_action["argv"][-3:] == ["repository-draft-save", "/run/ods-pixel-manager/extension-manager.sock", "{recipeJson}"]
assert draft_action["defaultAuthority"] == "propose"
assert draft_action["effect"] == "manage"
assert draft_action["idempotent"] is True
validation_action = v["actions"]["ods.extensions.github-validate"]
assert validation_action["argv"][-3:] == ["repository-validate", "/run/ods-pixel-manager/extension-manager.sock", "{recipeJson}"]
assert set(validation_action["parameters"]) == {"recipeJson"}
assert validation_action["parameters"]["recipeJson"]["maxLength"] == 4096
for mode in ("validate", "draft-save"):
    parts_action = v["actions"]["ods.extensions.github-" + mode + "-parts"]
    assert parts_action["argv"][-8:] == ["{part" + str(i) + "}" for i in range(1, 9)]
    assert parts_action["argv"][-10] == "repository-" + mode + "-parts"
    assert len(parts_action["parameters"]) == 8
    assert all(rule["maxLength"] == 4096 for rule in parts_action["parameters"].values())
    assert parts_action["defaultAuthority"] == ("observe" if mode == "validate" else "propose")
repository_action = v["actions"]["ods.extensions.github-inspect"]
assert repository_action["argv"] == [str(pathlib.Path("/usr/bin/python3").resolve()),"/opt/pixel-ops-broker/ods-extension-manager.py","repository","/run/ods-pixel-manager/extension-manager.sock","{repositoryUrl}"]
assert set(repository_action["parameters"]) == {"repositoryUrl"}
assert repository_action["parameters"]["repositoryUrl"]["maxLength"] == 512
file_action = v["actions"]["ods.extensions.github-file"]
assert file_action["argv"][-4:] == ["/run/ods-pixel-manager/extension-manager.sock", "{repositoryUrl}", "{commit}", "{path}"]
assert set(file_action["parameters"]) == {"repositoryUrl", "commit", "path"}
assert file_action["parameters"]["commit"]["pattern"] == "^[a-f0-9]{40}$"
assert v["actions"]["ods.extensions.disable"]["rollbackAction"] == "ods.extensions.enable"
assert v["actions"]["ods.extensions.remove"]["tier"] == "change"
assert v["actions"]["ods.extensions.remove"]["effect"] == "change"
assert v["actions"]["ods.extensions.remove"]["defaultAuthority"] == "propose"
assert v["actions"]["ods.extensions.remove"]["reversible"] is False
assert v["authority"]["defaultLevel"] == "propose"
assert v["authority"]["grants"] == [{"id":"ods-approved-downloads","level":"bounded-auto","actions":["download.stage"],"targets":["broker"],"tiers":["staging"],"environments":["lab"],"maxExecutions":100,"windowSeconds":86400,"maxConcurrent":2,"maxRuntimeSeconds":600,"maxFailures":10,"maxArtifactBytes":536870912}]
' "$operations_policy" "$INSTALL_DIR" "$home/.openclaw/workspace-pixel"
printf '%s\n' '{}' > "$TEST_ROOT/policy-symlink-target.json"
ln -s "$TEST_ROOT/policy-symlink-target.json" "$TEST_ROOT/linked-operations-policy.json"
if _ods_pixel_write_operations_policy "$owner" "$home" "$TEST_ROOT/linked-operations-policy.json" >/dev/null 2>&1; then
    fail "symlink Operations policy rejected"
else
    pass "symlink Operations policy rejected"
fi
_ods_pixel_write_onboarding "$owner" "$home" "$answers" /usr/bin/openclaw /opt/ods/pixel-plugin "$digest"
check env PATH="$CONFIGURE_TEST_PATH" python3 "$ROOT/tests/test_pixel_gateway_extension_configure.py" "$answers"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["gatewayPort"] == 18789; assert v["webSearchProvider"] == "searxng"; assert not any(e["id"] == "parallel" for e in v["gatewayExtensions"])' "$answers"
alternate_gateway_answers="$TEST_ROOT/alternate-gateway-onboarding.json"
PIXEL_GATEWAY_PORT=18790 _ods_pixel_write_onboarding "$owner" "$home" \
    "$alternate_gateway_answers" /usr/bin/openclaw /opt/ods/pixel-plugin "$digest"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["gatewayPort"] == 18790' \
    "$alternate_gateway_answers"
for invalid_gateway_port in 0 65536 invalid; do
    if PIXEL_GATEWAY_PORT="$invalid_gateway_port" _ods_pixel_write_onboarding "$owner" "$home" \
        "$TEST_ROOT/invalid-gateway-onboarding.json" /usr/bin/openclaw \
        /opt/ods/pixel-plugin "$digest" >/dev/null 2>&1; then
        fail "invalid Pixel gateway port rejected: $invalid_gateway_port"
    else
        pass "invalid Pixel gateway port rejected: $invalid_gateway_port"
    fi
done
gateway_port_home="$TEST_ROOT/gateway-port-home"
mkdir -p "$gateway_port_home/.config/pixel-deployment"
chmod 0700 "$gateway_port_home/.config" "$gateway_port_home/.config/pixel-deployment"
cp "$answers" "$gateway_port_home/.config/pixel-deployment/onboarding.json"
chmod 0600 "$gateway_port_home/.config/pixel-deployment/onboarding.json"
check _ods_pixel_existing_gateway_port_matches "$owner" "$gateway_port_home" 18789
check test "$(_ods_pixel_installed_gateway_port "$owner" "$gateway_port_home")" = 18789
cp "$alternate_gateway_answers" "$gateway_port_home/.config/pixel-deployment/onboarding.json"
check test "$(_ods_pixel_installed_gateway_port "$owner" "$gateway_port_home")" = 18790
cp "$answers" "$gateway_port_home/.config/pixel-deployment/onboarding.json"
if _ods_pixel_existing_gateway_port_matches "$owner" "$gateway_port_home" 18790 \
    >/dev/null 2>&1; then
    fail "installed Pixel gateway port change rejected before reconciliation"
else
    check test "$?" = 2
fi
fresh_gateway_port_home="$TEST_ROOT/fresh-gateway-port-home"
mkdir -p "$fresh_gateway_port_home"
check _ods_pixel_existing_gateway_port_matches "$owner" "$fresh_gateway_port_home" 18790
native_answers="$TEST_ROOT/native-search-onboarding.json"
_ods_pixel_write_onboarding "$owner" "$home" "$native_answers" /usr/bin/openclaw \
    /opt/ods/pixel-plugin "$digest" parallel-free /opt/ods/native-search/parallel-2026.6.33 "$digest"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["webSearchProvider"] == "parallel-free"; assert {"id":"parallel","path":"/opt/ods/native-search/parallel-2026.6.33","sha256":sys.argv[2]} in v["gatewayExtensions"]' "$native_answers" "$digest"
for invalid_search in parallel unknown; do
    if _ods_pixel_write_onboarding "$owner" "$home" "$native_answers" /usr/bin/openclaw \
        /opt/ods/pixel-plugin "$digest" "$invalid_search" >/dev/null 2>&1; then
        fail "unsupported search provider rejected: $invalid_search"
    else
        pass "unsupported search provider rejected: $invalid_search"
    fi
done
if _ods_pixel_write_onboarding "$owner" "$home" "$native_answers" /usr/bin/openclaw \
    /opt/ods/pixel-plugin "$digest" parallel-free /opt/unverified '' >/dev/null 2>&1; then
    fail "native search without verified extension rejected"
else
    pass "native search without verified extension rejected"
fi
observed_contract_sha256="$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")"
check test "$observed_contract_sha256" = "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")"
check test "${#observed_contract_sha256}" = 64
cp "$operations_policy" "$TEST_ROOT/operations-policy.original.json"
python3 - "$operations_policy" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text(encoding="utf-8"))
value["targets"]["ods-host"]["defaultCwd"] = "/var/lib/pixel-ops-broker/runtime"
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n", encoding="utf-8")
PY
chmod 0600 "$operations_policy"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds Operations policy bytes"
else
    pass "managed contract hash binds Operations policy bytes"
fi
mv "$TEST_ROOT/operations-policy.original.json" "$operations_policy"
chmod 0600 "$operations_policy"
cp "$extension_catalog" "$TEST_ROOT/extension-catalog.original.json"
python3 - "$extension_catalog" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text(encoding="utf-8"))
value["extensions"][0]["description"] = "Changed projection."
path.write_text(json.dumps(value, sort_keys=True, separators=(",", ":")) + "\n", encoding="utf-8")
PY
chmod 0600 "$extension_catalog"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds extension catalog bytes"
else
    pass "managed contract hash binds extension catalog bytes"
fi
mv "$TEST_ROOT/extension-catalog.original.json" "$extension_catalog"
chmod 0600 "$extension_catalog"
extension_helper="$INSTALL_DIR/extensions/services/pixel-agent/host/extension_search.py"
cp "$extension_helper" "$TEST_ROOT/extension-search.original.py"
printf '\n# changed\n' >>"$extension_helper"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds extension helper bytes"
else
    pass "managed contract hash binds extension helper bytes"
fi
mv "$TEST_ROOT/extension-search.original.py" "$extension_helper"
chmod 0644 "$extension_helper"
extension_manager="$INSTALL_DIR/extensions/services/pixel-agent/host/extension_manager.py"
cp "$extension_manager" "$TEST_ROOT/extension-manager.original.py"
printf '\n# changed\n' >>"$extension_manager"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds extension manager bytes"
else
    pass "managed contract hash binds extension manager bytes"
fi
mv "$TEST_ROOT/extension-manager.original.py" "$extension_manager"
chmod 0644 "$extension_manager"
cp "$extension_manager_unit" "$TEST_ROOT/extension-manager-unit.original.service"
printf '\n# changed\n' >>"$extension_manager_unit"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds rendered extension manager unit bytes"
else
    pass "managed contract hash binds rendered extension manager unit bytes"
fi
mv "$TEST_ROOT/extension-manager-unit.original.service" "$extension_manager_unit"
chmod 0600 "$extension_manager_unit"
approval_helper="$INSTALL_DIR/bin/ods-pixel-approve"
cp "$approval_helper" "$TEST_ROOT/ods-pixel-approve.original"
printf '\n# changed\n' >>"$approval_helper"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds approval helper bytes"
else
    pass "managed contract hash binds approval helper bytes"
fi
mv "$TEST_ROOT/ods-pixel-approve.original" "$approval_helper"
chmod 0755 "$approval_helper"
artifact_promoter="$INSTALL_DIR/extensions/services/pixel-agent/host/artifact_promoter.py"
cp "$artifact_promoter" "$TEST_ROOT/artifact-promoter.original.py"
printf '\n# changed\n' >>"$artifact_promoter"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds artifact promoter bytes"
else
    pass "managed contract hash binds artifact promoter bytes"
fi
mv "$TEST_ROOT/artifact-promoter.original.py" "$artifact_promoter"
chmod 0644 "$artifact_promoter"
cp "$artifact_promoter_unit" "$TEST_ROOT/artifact-promoter-unit.original.service"
printf '\n# changed\n' >>"$artifact_promoter_unit"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds rendered artifact promoter unit bytes"
else
    pass "managed contract hash binds rendered artifact promoter unit bytes"
fi
mv "$TEST_ROOT/artifact-promoter-unit.original.service" "$artifact_promoter_unit"
chmod 0600 "$artifact_promoter_unit"
workspace_preview="$INSTALL_DIR/extensions/services/pixel-agent/host/workspace_preview.py"
cp "$workspace_preview" "$TEST_ROOT/workspace-preview.original.py"
printf '\n# changed\n' >>"$workspace_preview"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds workspace preview bytes"
else
    pass "managed contract hash binds workspace preview bytes"
fi
mv "$TEST_ROOT/workspace-preview.original.py" "$workspace_preview"
chmod 0644 "$workspace_preview"
cp "$workspace_preview_unit" "$TEST_ROOT/workspace-preview-unit.original.service"
printf '\n# changed\n' >>"$workspace_preview_unit"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds rendered workspace preview unit bytes"
else
    pass "managed contract hash binds rendered workspace preview unit bytes"
fi
mv "$TEST_ROOT/workspace-preview-unit.original.service" "$workspace_preview_unit"
chmod 0600 "$workspace_preview_unit"
operations_service_dropin="$INSTALL_DIR/extensions/services/pixel-agent/host/pixel-ops-broker-ods.conf"
cp "$operations_service_dropin" "$TEST_ROOT/pixel-ops-broker-ods.original.conf"
printf '\n# changed\n' >>"$operations_service_dropin"
if [[ "$observed_contract_sha256" == "$(_ods_pixel_contract_sha256 "$owner" "$home" "$answers")" ]]; then
    fail "managed contract hash binds Operations service drop-in bytes"
else
    pass "managed contract hash binds Operations service drop-in bytes"
fi
mv "$TEST_ROOT/pixel-ops-broker-ods.original.conf" "$operations_service_dropin"
chmod 0644 "$operations_service_dropin"
installed_policy="$TEST_ROOT/installed-operations-policy.json"
cp "$operations_policy" "$installed_policy"
chmod 0640 "$installed_policy"
if (
    ods_sudo() { "$@"; }
    _ods_pixel_verify_operations_policy_custody "$owner" "$home" "$operations_policy" \
        "$installed_policy" "$(id -u)"
); then
    pass "matching root-custodied Operations policy accepted"
else
    fail "matching root-custodied Operations policy accepted"
fi
printf '\n' >> "$installed_policy"
if (
    ods_sudo() { "$@"; }
    _ods_pixel_verify_operations_policy_custody "$owner" "$home" "$operations_policy" \
        "$installed_policy" "$(id -u)"
) >/dev/null 2>&1; then
    fail "stale root-custodied Operations policy rejected"
else
    pass "stale root-custodied Operations policy rejected"
fi
rm -f "$installed_policy"
ln -s "$operations_policy" "$installed_policy"
if (
    ods_sudo() { "$@"; }
    _ods_pixel_verify_operations_policy_custody "$owner" "$home" "$operations_policy" \
        "$installed_policy" "$(id -u)"
) >/dev/null 2>&1; then
    fail "symlink root-custodied Operations policy rejected"
else
    pass "symlink root-custodied Operations policy rejected"
fi
rm -f "$installed_policy"
check python3 -c '
import json,sys
v=json.load(open(sys.argv[1]))
assert v["capabilityProfile"] == "engineering-operator"
assert v["modelProvider"] == "ods-gateway"
assert v["modelBaseUrl"] == "http://127.0.0.1:4006/v1"
assert v["modelApiKey"] == "test-pixel-relay-secret"
assert v["modelId"] == "ods/current"
assert v["modelName"] == "ODS Current (qwen-test)"
assert v["modelContextWindow"] == 32768
assert v["modelMaxTokens"] == 8192
assert v["modelReasoning"] is False
assert v["frontierBudgetProfile"] == "starter"
assert v["operationsPolicyFile"] == sys.argv[2]
assert v["gatewayExtensions"] == [{"id":"pixel-ods","path":"/opt/ods/pixel-plugin","sha256":"a"*64,"tools":["pixel_ods_status","pixel_ods_apps_list","pixel_ods_extensions", "pixel_ods_host_observe","pixel_ods_host_command_propose","pixel_ods_evidence_report","pixel_ods_evidence_readback","pixel_ods_research","pixel_ods_web_extract","pixel_ods_download_promote","pixel_ods_workspace_preview","pixel_ods_project_build","pixel_ods_workspace_artifact", "pixel_ods_workspace_bundle","pixel_ods_workspace_preview_inspect","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal", "pixel_ods_source_proposal", "pixel_ods_python_library_proposal", "pixel_ods_extension_request_status", "pixel_ods_extension_request_prepare", "pixel_ods_extension_request_advance", "pixel_ods_extension_request_retry"]}]
assert v["operationsLimbEnabled"] is True
assert all(v[name] is False for name in ("emailLimbEnabled","calendarLimbEnabled","socialLimbEnabled","webLimbEnabled","frontierLimbEnabled"))
' "$answers" "$operations_policy"
check test "$(stat -c '%a' "$answers")" = 600
check test -z "$(find "${answers%/*}" -maxdepth 1 -name '.pixel-gateway-key.*' -print -quit)"

ODS_MODEL_SWITCHBOARD=enabled
_ods_pixel_write_onboarding "$owner" "$home" "$TEST_ROOT/switchboard-onboarding.json" \
    /usr/bin/openclaw /opt/ods/pixel-plugin "$digest"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelProvider"] == "ods-gateway" and v["modelId"] == "ods/current" and v["modelName"] == "ODS Current (qwen-test)" and v["modelBaseUrl"] == "http://127.0.0.1:4006/v1"' \
    "$TEST_ROOT/switchboard-onboarding.json"
ODS_MODEL_SWITCHBOARD=observe
if PIXEL_MODEL_RELAY_KEY='' _ods_pixel_write_onboarding "$owner" "$home" \
    "$TEST_ROOT/missing-gateway-key-onboarding.json" /usr/bin/openclaw \
    /opt/ods/pixel-plugin "$digest" >/dev/null 2>&1; then
    fail "Pixel onboarding without a model relay key rejected"
else
    pass "Pixel onboarding without a model relay key rejected"
fi
if (
    curl() {
        [[ "$*" != *"test-litellm-secret"* && "$*" == *"@/dev/fd/"* ]] || return 1
        printf '%s\n' '{"data":[{"id":"ods/current"}]}'
    }
    sleep() { :; }
    _ods_pixel_wait_model_gateway "test gateway" 4000 test-litellm-secret ods/current 1
); then
    pass "authenticated Pixel model-gateway alias preflight accepted"
else
    fail "authenticated Pixel model-gateway alias preflight accepted"
fi
if _ods_pixel_wait_model_gateway "test gateway" 4000 "" default 1 >/dev/null 2>&1; then
    fail "Pixel model-gateway preflight without authentication rejected"
else
    pass "Pixel model-gateway preflight without authentication rejected"
fi

cp "$answers" "$TEST_ROOT/gateway-name-control.json"
python3 - "$TEST_ROOT/gateway-name-control.json" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
value["modelName"] = "ODS Current (qwen-test)\nforged"
path.write_text(json.dumps(value) + "\n")
PY
chmod 0600 "$TEST_ROOT/gateway-name-control.json"
if _ods_pixel_update_onboarding_model "$owner" "$home" \
    "$TEST_ROOT/gateway-name-control.json" qwen-next >/dev/null 2>&1; then
    fail "forged Pixel gateway model display name rejected"
else
    pass "forged Pixel gateway model display name rejected"
fi

legacy_gateway_answers="$TEST_ROOT/legacy-gateway-onboarding.json"
cp "$answers" "$legacy_gateway_answers"
python3 - "$legacy_gateway_answers" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
value["modelId"] = "default"
value["modelName"] = "ODS Default (qwen-test)"
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$legacy_gateway_answers"
_ods_pixel_update_onboarding_model "$owner" "$home" \
    "$legacy_gateway_answers" 'org/qwen+tools:remote' 131072 8192 true
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelId"] == "ods/current" and v["modelName"] == "ODS Current (org/qwen+tools:remote)"' \
    "$legacy_gateway_answers"

MAX_CONTEXT=16384
LLM_MODEL=NVIDIA-Nemotron3-Nano-4B
_ods_pixel_write_onboarding "$owner" "$home" "$TEST_ROOT/nemotron-onboarding.json" \
    /usr/bin/openclaw /opt/ods/pixel-plugin "$digest"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelContextWindow"] == 16384 and v["modelMaxTokens"] == 4096 and v["modelReasoning"] is False' \
    "$TEST_ROOT/nemotron-onboarding.json"
LLAMA_REASONING=on
LLM_MODEL=qwen-reasoning-enabled
_ods_pixel_write_onboarding "$owner" "$home" "$TEST_ROOT/qwen-reasoning-onboarding.json" \
    /usr/bin/openclaw /opt/ods/pixel-plugin "$digest"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelReasoning"] is True' \
    "$TEST_ROOT/qwen-reasoning-onboarding.json"
# Case-insensitive, as the host agent reads LLAMA_REASONING: OFF is off.
LLAMA_REASONING=OFF
_ods_pixel_write_onboarding "$owner" "$home" "$TEST_ROOT/qwen-reasoning-upper-off.json" \
    /usr/bin/openclaw /opt/ods/pixel-plugin "$digest"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelReasoning"] is False' \
    "$TEST_ROOT/qwen-reasoning-upper-off.json"
LLAMA_REASONING=off
MAX_CONTEXT=4096
if _ods_pixel_write_onboarding "$owner" "$home" "$TEST_ROOT/undersized-onboarding.json" \
    /usr/bin/openclaw /opt/ods/pixel-plugin "$digest" >/dev/null 2>&1; then
    pass "Pixel onboarding accepts the minimum 4K adaptive context"
else
    fail "Pixel onboarding accepts the minimum 4K adaptive context"
fi
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelContextWindow"] == 4096 and v["modelMaxTokens"] == 1024' \
    "$TEST_ROOT/undersized-onboarding.json"
MAX_CONTEXT=2048
if _ods_pixel_write_onboarding "$owner" "$home" "$TEST_ROOT/below-openclaw-minimum.json" \
    /usr/bin/openclaw /opt/ods/pixel-plugin "$digest" >/dev/null 2>&1; then
    fail "Pixel onboarding below the OpenClaw 4K minimum rejected"
else
    pass "Pixel onboarding below the OpenClaw 4K minimum rejected"
fi
MAX_CONTEXT=32768
LLM_MODEL=qwen-test

runtime_home="$TEST_ROOT/runtime-home"
runtime_config="$runtime_home/.openclaw/openclaw.json"
runtime_validator="$TEST_ROOT/openclaw-validator"
mkdir -p "$runtime_home/.openclaw"
chmod 0700 "$runtime_home/.openclaw"
cat > "$runtime_config" <<'JSON'
{
  "agents": {
    "defaults": {"bootstrapMaxChars": 32000},
    "list": [{
      "id": "pixel",
      "model": "ods-local/qwen-test",
      "tools": {"deny": ["web_fetch", "web_search", "pixel_web_extract"]}
    }]
  },
  "models": {
    "providers": {
      "ods-local": {
        "api": "openai-completions",
        "apiKey": "local-no-auth",
        "baseUrl": "http://127.0.0.1:11434/v1",
        "models": [{
          "id": "qwen-test",
          "name": "ODS Local qwen-test",
          "contextWindow": 32768,
          "maxTokens": 4096
        }]
      }
    }
  },
  "plugins": {
    "entries": {
      "searxng": {
        "enabled": true,
        "config": {"webSearch": {"baseUrl": "http://127.0.0.1:8888"}}
      }
    }
  },
  "session": {"dmScope": "per-account-channel-peer"},
  "tools": {
    "profile": "coding",
    "alsoAllow": ["pixel_web_extract"],
    "sandbox": {"tools": {"allow": ["exec", "pixel_ods_status", "pixel_web_extract"]}},
    "web": {"search": {"provider": "searxng"}}
  }
}
JSON
chmod 0600 "$runtime_config"
cat > "$runtime_validator" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
[[ "$1 $2" == "config validate" ]]
python3 - "$OPENCLAW_CONFIG_PATH" <<'PY'
import json, os, pathlib, re, sys
value = json.load(open(sys.argv[1], encoding="utf-8"))
assert value["agents"]["defaults"]["timeoutSeconds"] == 1800
assert value["agents"]["defaults"]["bootstrapMaxChars"] == 32000
assert value["agents"]["defaults"]["bootstrapTotalMaxChars"] == 96000
assert value["agents"]["defaults"]["contextInjection"] == "continuation-skip"
agent = value["agents"]["list"][0]
provider_id = agent["model"].split("/", 1)[0]
provider = value["models"]["providers"][provider_id]
assert provider["timeoutSeconds"] == 1800
model = provider["models"][0]
assert agent["experimental"] == {"localModelLean": False}
for tool in ("create_goal", "get_goal", "update_goal", "update_plan"):
    assert tool in value["tools"]["alsoAllow"]
    assert tool in value["tools"]["sandbox"]["tools"]["allow"]
compact_context = model["contextWindow"] < 32768
model_label = "{} {}".format(model.get("id", ""), model.get("name", "")).casefold()
parameter_markers = re.findall(
    r"(?<![a-z0-9.])(\d+(?:\.\d+)?)\s*b(?![a-z0-9])", model_label,
)
small_model = (
    any(float(marker) <= 4 for marker in parameter_markers)
    or (not parameter_markers and re.search(
        r"(?<![a-z0-9])(mini|micro|tiny)(?![a-z0-9])", model_label,
    ) is not None)
)
lean_prompt = compact_context or small_model
assert agent["bootstrapMaxChars"] == (2000 if lean_prompt else 14000)
assert agent["bootstrapTotalMaxChars"] == (6000 if lean_prompt else 36000)
assert agent["contextInjection"] == ("never" if lean_prompt else "continuation-skip")
assert agent["contextLimits"] == {
    "toolResultMaxChars": max(4000, min(16000, model["contextWindow"] // 4)),
}
assert value["tools"]["toolSearch"] == {"enabled": True, "mode": "tools", "searchDefaultLimit": 5, "maxSearchLimit": 10}
assert "cron" in value["tools"]["alsoAllow"]
assert "cron" in value["tools"]["sandbox"]["tools"]["allow"]
assert agent["tools"]["deny"] == []
assert {"pixel_ods_status", "pixel_ods_apps_list", "pixel_ods_extensions", "pixel_ods_host_observe", "pixel_ods_host_command_propose", "pixel_ods_evidence_report", "pixel_ods_evidence_readback", "pixel_ods_research","pixel_ods_web_extract", "pixel_ods_download_promote", "pixel_ods_workspace_preview","pixel_ods_workspace_bundle","pixel_ods_workspace_artifact","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal"}.issubset(value["tools"]["alsoAllow"])
assert {"web_search", "web_fetch", "pixel_ods_status", "pixel_ods_apps_list", "pixel_ods_extensions", "pixel_ods_host_observe", "pixel_ods_host_command_propose", "pixel_ods_evidence_report", "pixel_ods_evidence_readback", "pixel_ods_research","pixel_ods_web_extract", "pixel_ods_download_promote", "pixel_ods_workspace_preview","pixel_ods_workspace_bundle","pixel_ods_workspace_artifact","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal"}.issubset(value["tools"]["sandbox"]["tools"]["allow"])
assert value["plugins"]["entries"]["pixel-ods"]["hooks"]["allowConversationAccess"] is True
pixel_config = value["plugins"]["entries"]["pixel-ods"]["config"]
expected_pixel_config = {
    "modelContextWindow": model["contextWindow"],
    "leanPrompt": lean_prompt,
    "perplexicaPort": value["plugins"]["entries"]["pixel-ods"]["config"]["perplexicaPort"],
}
if "modelImageInput" in pixel_config:
    image_input = pixel_config["modelImageInput"]
    assert image_input in ("supported", "unsupported", "unknown")
    expected_pixel_config["modelImageInput"] = image_input
    assert model["input"] == (["text"] if image_input == "unsupported" else ["text", "image"])
if "modelRouteFingerprint" in pixel_config:
    assert re.fullmatch(r"[a-f0-9]{64}", pixel_config["modelRouteFingerprint"])
    expected_pixel_config["modelRouteFingerprint"] = pixel_config["modelRouteFingerprint"]
assert pixel_config == expected_pixel_config
assert 1 <= value["plugins"]["entries"]["pixel-ods"]["config"]["perplexicaPort"] <= 65535
assert "pixel_web_extract" not in value["tools"]["alsoAllow"]
assert "pixel_web_extract" not in value["tools"]["sandbox"]["tools"]["allow"]
assert value["tools"]["web"]["fetch"] == {
    "enabled": True,
    "maxChars": 12000,
    "maxCharsCap": 20000,
    "maxResponseBytes": 1000000,
    "timeoutSeconds": 20,
    "cacheTtlMinutes": 15,
    "maxRedirects": 3,
    "readability": True,
    "useTrustedEnvProxy": False,
    "ssrfPolicy": {
        "allowRfc2544BenchmarkRange": False,
        "allowIpv6UniqueLocalRange": False,
    },
}
assert value["tools"]["loopDetection"] == {
    "enabled": True,
    "historySize": 12,
    "warningThreshold": 2,
    "unknownToolThreshold": 2,
    "criticalThreshold": 4,
    "globalCircuitBreakerThreshold": 6,
    "detectors": {
        "genericRepeat": True,
        "knownPollNoProgress": True,
        "pingPong": True,
    },
}
context_window = model["contextWindow"]
assert value["agents"]["defaults"]["compaction"] == {
    "reserveTokens": (context_window + 4 * model["maxTokens"] + 4) // 5,
    "reserveTokensFloor": 0,
    "timeoutSeconds": 1800,
    "keepRecentTokens": max(512, min(20000, context_window // 16)),
}
# A case that renders a measured thinking control names it here; the config
# itself never carries it. Without one, the "qwen" name rule applies.
thinking_control = os.environ.get("ODS_TEST_THINKING_CONTROL")
enable_thinking_switch = ("qwen" in model["id"].lower() if thinking_control is None
                          else thinking_control == "enable_thinking")
if thinking_control in ("always", "none"):
    assert model["reasoning"] is (thinking_control == "always")
    assert "thinkingDefault" not in agent
    assert "compat" not in model
    assert "chat_template_kwargs" not in agent.get("params", {})
elif enable_thinking_switch and model["reasoning"] is True:
    assert agent["thinkingDefault"] == "low"
    assert model["compat"] == {"thinkingFormat": "qwen-chat-template"}
    assert agent["params"]["chat_template_kwargs"]["enable_thinking"] is True
else:
    assert "thinkingDefault" not in agent
    assert model["reasoning"] is False
    assert "compat" not in model
    if enable_thinking_switch:
        assert agent["params"]["chat_template_kwargs"]["enable_thinking"] is False
if lean_prompt:
    params = agent["params"]
    assert params["temperature"] == 0.7
    assert params["topP"] == 0.8
    assert params["frequencyPenalty"] == 0.6
    assert params["presencePenalty"] == 0.2
else:
    assert all(name not in agent.get("params", {}) for name in (
        "temperature", "topP", "frequencyPenalty", "presencePenalty"
    ))
assert value["diagnostics"]["stuckSessionAbortMs"] == 1860000
assert value["session"]["writeLock"] == {"maxHoldMs": 1920000, "staleMs": 3600000}
assert value["agents"]["defaults"]["sandbox"]["docker"]["binds"] == [
    "{}:/run/pixel-ods-control:ro".format(
        pathlib.Path.home() / ".openclaw" / ".ods-exec-control"
    )
]
assert value["agents"]["defaults"]["sandbox"]["docker"]["dangerouslyAllowExternalBindSources"] is True
PY
SH
chmod 0755 "$runtime_validator"
runtime_recovery_candidate="$runtime_home/.openclaw/recovery-candidate.json"
cp "$runtime_config" "$runtime_recovery_candidate"
chmod 0600 "$runtime_recovery_candidate"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_config" "$runtime_validator")" = changed
runtime_sha256="$(sha256sum "$runtime_config" | awk '{print $1}')"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); d=v["agents"]["defaults"]; assert d["timeoutSeconds"] == 1800 and d["bootstrapMaxChars"] == 32000 and d["bootstrapTotalMaxChars"] == 96000 and d["contextInjection"] == "continuation-skip"; assert d["compaction"] == {"reserveTokens":9831,"reserveTokensFloor":0,"timeoutSeconds":1800,"keepRecentTokens":2048}; assert d["sandbox"]["docker"]["binds"] == [sys.argv[2] + "/.openclaw/.ods-exec-control:/run/pixel-ods-control:ro"] and d["sandbox"]["docker"]["dangerouslyAllowExternalBindSources"] is True; assert d["sandbox"]["docker"]["pidsLimit"] == 1024 and "ulimits" not in d["sandbox"]["docker"]; a=v["agents"]["list"][0]; assert "thinkingDefault" not in a and a["tools"]["deny"] == [] and a["experimental"] == {"localModelLean":False} and a["bootstrapMaxChars"] == 14000 and a["bootstrapTotalMaxChars"] == 36000 and a["contextInjection"] == "continuation-skip" and a["contextLimits"] == {"toolResultMaxChars":8192} and a["params"]["chat_template_kwargs"]["enable_thinking"] is False; assert v["models"]["providers"]["ods-local"]["timeoutSeconds"] == 1800; m=v["models"]["providers"]["ods-local"]["models"][0]; assert m["reasoning"] is False and "compat" not in m; assert v["diagnostics"]["stuckSessionAbortMs"] == 1860000; assert v["session"]["writeLock"] == {"maxHoldMs":1920000,"staleMs":3600000}; assert {"pixel_ods_status","pixel_ods_apps_list","pixel_ods_extensions", "pixel_ods_host_observe","pixel_ods_host_command_propose","pixel_ods_evidence_report","pixel_ods_evidence_readback","pixel_ods_research","pixel_ods_web_extract","pixel_ods_download_promote","pixel_ods_workspace_preview","pixel_ods_workspace_bundle","pixel_ods_workspace_artifact","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal"}.issubset(v["tools"]["alsoAllow"]); assert v["tools"]["toolSearch"] == {"enabled":True,"mode":"tools","searchDefaultLimit":5,"maxSearchLimit":10}; assert {"web_search","web_fetch","pixel_ods_status","pixel_ods_apps_list","pixel_ods_extensions", "pixel_ods_host_observe","pixel_ods_host_command_propose","pixel_ods_evidence_report","pixel_ods_evidence_readback","pixel_ods_research","pixel_ods_web_extract","pixel_ods_download_promote","pixel_ods_workspace_preview","pixel_ods_workspace_bundle","pixel_ods_workspace_artifact","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal"}.issubset(v["tools"]["sandbox"]["tools"]["allow"]) and v["tools"]["loopDetection"]["globalCircuitBreakerThreshold"] == 6; assert v["plugins"]["entries"]["pixel-ods"]["hooks"]["allowConversationAccess"] is True and v["plugins"]["entries"]["pixel-ods"]["config"] == {"modelContextWindow":32768,"leanPrompt":False,"perplexicaPort":3004}; assert v["tools"]["web"]["fetch"]["enabled"] is True and v["tools"]["web"]["fetch"]["maxChars"] == 12000 and v["tools"]["web"]["fetch"]["timeoutSeconds"] == 20 and v["tools"]["web"]["fetch"]["ssrfPolicy"] == {"allowRfc2544BenchmarkRange":False,"allowIpv6UniqueLocalRange":False}' "$runtime_config" "$runtime_home"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_config" "$runtime_validator")" = unchanged
check test "$(sha256sum "$runtime_config" | awk '{print $1}')" = "$runtime_sha256"
runtime_nproc_config="$runtime_home/.openclaw/nproc-config.json"
cp "$runtime_config" "$runtime_nproc_config"
check python3 - "$runtime_nproc_config" <<'PY'
import json, sys

path = sys.argv[1]
with open(path, encoding="utf-8") as source:
    value = json.load(source)
docker = value["agents"]["defaults"]["sandbox"]["docker"]
docker["pidsLimit"] = 77
docker["ulimits"] = {
    "nofile": {"soft": 4096, "hard": 4096},
    "nproc": {"soft": 1024, "hard": 1024},
}
with open(path, "w", encoding="utf-8") as target:
    json.dump(value, target, indent=2)
    target.write("\n")
PY
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_nproc_config" "$runtime_validator")" = changed
check python3 -c 'import json,sys; d=json.load(open(sys.argv[1]))["agents"]["defaults"]["sandbox"]["docker"]; assert d["pidsLimit"] == 1024 and d["ulimits"] == {"nofile":{"soft":4096,"hard":4096}}' "$runtime_nproc_config"
runtime_nproc_sha256="$(sha256sum "$runtime_nproc_config" | awk '{print $1}')"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_nproc_config" "$runtime_validator")" = unchanged
check test "$(sha256sum "$runtime_nproc_config" | awk '{print $1}')" = "$runtime_nproc_sha256"
check test "$(PERPLEXICA_PORT=43004 _ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_config" "$runtime_validator")" = changed
check python3 -c 'import json,sys; assert json.load(open(sys.argv[1]))["plugins"]["entries"]["pixel-ods"]["config"]["perplexicaPort"] == 43004' "$runtime_config"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_config" "$runtime_validator")" = changed
check test "$(sha256sum "$runtime_config" | awk '{print $1}')" = "$runtime_sha256"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_recovery_candidate" "$runtime_validator")" = changed
check _ods_pixel_candidate_config_matches_live "$owner" "$runtime_home" "$runtime_recovery_candidate"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); required={"create_goal","get_goal","update_goal","update_plan"}; assert required.issubset(v["tools"]["alsoAllow"]) and required.issubset(v["tools"]["sandbox"]["tools"]["allow"])' "$runtime_config"
check test -z "$(find "$runtime_home/.openclaw" -maxdepth 1 -name '.ods-pixel-runtime-budget.*' -print -quit)"

for search_case in native-provider automatic-provider disabled-searxng; do
    runtime_search_config="$runtime_home/.openclaw/$search_case.json"
    python3 - "$runtime_config" "$runtime_search_config" "$search_case" <<'PY'
import json, pathlib, sys

source, destination = map(pathlib.Path, sys.argv[1:3])
value = json.loads(source.read_text(encoding="utf-8"))
entries = value["plugins"]["entries"]
entries.pop("searxng", None)
value["tools"]["web"]["search"] = {"enabled": True, "provider": "parallel-free"}
entries["parallel"] = {"enabled": True}
if sys.argv[3] == "automatic-provider":
    value["tools"]["web"].pop("search")
elif sys.argv[3] == "disabled-searxng":
    entries["searxng"] = {"enabled": False}
destination.write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8")
destination.chmod(0o600)
PY
    runtime_search_sha256="$(sha256sum "$runtime_search_config" | awk '{print $1}')"
    check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_search_config" "$runtime_validator")" = unchanged
    check test "$(sha256sum "$runtime_search_config" | awk '{print $1}')" = "$runtime_search_sha256"
done

runtime_compact_config="$runtime_home/.openclaw/compact-context.json"
python3 - "$runtime_config" "$runtime_compact_config" <<'PY'
import json, pathlib, sys

source, destination = map(pathlib.Path, sys.argv[1:])
value = json.loads(source.read_text(encoding="utf-8"))
model = value["models"]["providers"]["ods-local"]["models"][0]
model["contextWindow"] = 8192
model["maxTokens"] = 1024
destination.write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8")
destination.chmod(0o600)
PY
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_compact_config" "$runtime_validator")" = changed
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); a=v["agents"]["list"][0]; assert a["bootstrapMaxChars"] == 2000 and a["bootstrapTotalMaxChars"] == 6000 and a["contextInjection"] == "never" and a["contextLimits"] == {"toolResultMaxChars":4000}; assert {k:a["params"][k] for k in ("temperature","topP","frequencyPenalty","presencePenalty")} == {"temperature":0.7,"topP":0.8,"frequencyPenalty":0.6,"presencePenalty":0.2}; assert v["agents"]["defaults"]["compaction"] == {"reserveTokens":2458,"reserveTokensFloor":0,"timeoutSeconds":1800,"keepRecentTokens":512}; assert v["plugins"]["entries"]["pixel-ods"]["config"] == {"modelContextWindow":8192,"leanPrompt":True,"perplexicaPort":3004}' "$runtime_compact_config"

runtime_small_large_config="$runtime_home/.openclaw/small-model-large-context.json"
python3 - "$runtime_config" "$runtime_small_large_config" <<'PY'
import json, pathlib, sys

source, destination = map(pathlib.Path, sys.argv[1:])
value = json.loads(source.read_text(encoding="utf-8"))
model = value["models"]["providers"]["ods-local"]["models"][0]
model.update({
    "id": "qwen3.5-2b-q4",
    "name": "ODS Local qwen3.5-2b-q4",
    "contextWindow": 65536,
    "maxTokens": 4096,
})
value["agents"]["list"][0]["model"] = "ods-local/qwen3.5-2b-q4"
destination.write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8")
destination.chmod(0o600)
PY
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_small_large_config" "$runtime_validator")" = changed
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); a=v["agents"]["list"][0]; m=v["models"]["providers"]["ods-local"]["models"][0]; assert m["contextWindow"] == 65536; assert a["bootstrapMaxChars"] == 2000 and a["bootstrapTotalMaxChars"] == 6000 and a["contextInjection"] == "never" and a["contextLimits"] == {"toolResultMaxChars":16000}; assert {k:a["params"][k] for k in ("temperature","topP","frequencyPenalty","presencePenalty")} == {"temperature":0.7,"topP":0.8,"frequencyPenalty":0.6,"presencePenalty":0.2}; assert v["plugins"]["entries"]["pixel-ods"]["config"] == {"modelContextWindow":65536,"leanPrompt":True,"perplexicaPort":3004}' "$runtime_small_large_config"
for size_class_id in phi4-mini-q4 granite4.0-h-micro-q4 granite4.0-h-tiny-q4 micro-70b-q4; do
    runtime_size_class_config="$runtime_home/.openclaw/size-class-$size_class_id.json"
    python3 - "$runtime_config" "$runtime_size_class_config" "$size_class_id" <<'PY'
import json, pathlib, sys

source, destination = map(pathlib.Path, sys.argv[1:3])
model_id = sys.argv[3]
value = json.loads(source.read_text(encoding="utf-8"))
model = value["models"]["providers"]["ods-local"]["models"][0]
model.update({
    "id": model_id,
    "name": f"ODS Local {model_id}",
    "contextWindow": 65536,
    "maxTokens": 4096,
})
value["agents"]["list"][0]["model"] = f"ods-local/{model_id}"
destination.write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8")
destination.chmod(0o600)
PY
    check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_size_class_config" "$runtime_validator")" = changed
    expected_lean=true
    if [[ "$size_class_id" == micro-70b-q4 ]]; then expected_lean=false; fi
    check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); expected=sys.argv[2]=="true"; a=v["agents"]["list"][0]; assert v["plugins"]["entries"]["pixel-ods"]["config"]["leanPrompt"] is expected; assert a["bootstrapMaxChars"] == (2000 if expected else 14000); assert a["bootstrapTotalMaxChars"] == (6000 if expected else 36000)' "$runtime_size_class_config" "$expected_lean"
done
runtime_full_candidate="$TEST_ROOT/runtime-full-candidate.json"
runtime_transition_answers="$TEST_ROOT/runtime-transition-onboarding.json"
cp "$runtime_config" "$runtime_full_candidate"
cp "$runtime_compact_config" "$runtime_config"
python3 - "$answers" "$runtime_transition_answers" <<'PY'
import json, pathlib, sys
source, target = map(pathlib.Path, sys.argv[1:])
value = json.loads(source.read_text())
value.update({
    "modelProvider": "ods-local",
    "modelId": "qwen-test",
    "modelName": "ODS Local qwen-test",
    "modelBaseUrl": "http://127.0.0.1:11434/v1",
    "modelApiKey": "local-no-auth",
    "modelContextWindow": 32768,
    "modelMaxTokens": 4096,
    "modelReasoning": False,
})
target.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$runtime_transition_answers"
# Render the native image-input metadata from this exact route contract.
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$runtime_home" \
    "$runtime_full_candidate" "$runtime_validator" "$runtime_transition_answers")" = changed
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$runtime_home" \
    "$runtime_full_candidate" "$runtime_transition_answers"
cp "$runtime_full_candidate" "$runtime_config"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); a=v["agents"]["list"][0]; assert all(k not in a.get("params", {}) for k in ("temperature","topP","frequencyPenalty","presencePenalty"))' "$runtime_config"

runtime_unsafe_bind="$runtime_home/.openclaw/unsafe-bind.json"
python3 - "$runtime_config" "$runtime_unsafe_bind" <<'PY'
import json, pathlib, sys
source, target = map(pathlib.Path, sys.argv[1:])
value = json.loads(source.read_text())
value["agents"]["defaults"]["sandbox"]["docker"]["binds"] = [
    "/:/host:rw"
]
target.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$runtime_unsafe_bind"
if _ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_unsafe_bind" \
    "$runtime_validator" >/dev/null 2>&1; then
    fail "unmanaged Pixel sandbox bind rejected"
else
    pass "unmanaged Pixel sandbox bind rejected"
fi

runtime_target="$TEST_ROOT/runtime-target.json"
runtime_link="$TEST_ROOT/runtime-link.json"
cp "$runtime_config" "$runtime_target"
ln -s "$runtime_target" "$runtime_link"
if _ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_link" "$runtime_validator" >/dev/null 2>&1; then
    fail "symlink ODS Pixel runtime config rejected"
else
    pass "symlink ODS Pixel runtime config rejected"
fi
chmod 0644 "$runtime_config"
if _ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_config" "$runtime_validator" >/dev/null 2>&1; then
    fail "unsafe ODS Pixel runtime config mode rejected"
else
    pass "unsafe ODS Pixel runtime config mode rejected"
fi
chmod 0600 "$runtime_config"

runtime_unvalidated="$runtime_home/.openclaw/unvalidated.json"
python3 - "$runtime_config" "$runtime_unvalidated" <<'PY'
import json, pathlib, sys
source, target = map(pathlib.Path, sys.argv[1:])
value = json.loads(source.read_text())
value["agents"]["defaults"].pop("timeoutSeconds")
value["models"]["providers"]["ods-local"].pop("timeoutSeconds")
value.pop("diagnostics")
value["session"].pop("writeLock")
target.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$runtime_unvalidated"
runtime_unvalidated_sha256="$(sha256sum "$runtime_unvalidated" | awk '{print $1}')"
if _ods_pixel_apply_runtime_budget "$owner" "$runtime_home" "$runtime_unvalidated" /bin/false >/dev/null 2>&1; then
    fail "invalid OpenClaw runtime budget candidate rejected"
else
    pass "invalid OpenClaw runtime budget candidate rejected"
fi
check test "$(sha256sum "$runtime_unvalidated" | awk '{print $1}')" = "$runtime_unvalidated_sha256"
check test -z "$(find "$runtime_home/.openclaw" -maxdepth 1 -name '.ods-pixel-runtime-budget.*' -print -quit)"

reconcile_home="$TEST_ROOT/reconcile-home"
reconcile_answers="$TEST_ROOT/reconcile-onboarding.json"
reconcile_candidate="$TEST_ROOT/reconcile-candidate.json"
reconcile_marker="$reconcile_home/.config/ods/pixel-managed.json"
reconcile_config="$reconcile_home/.openclaw/openclaw.json"
reconcile_ref="$(printf '9%.0s' {1..40})"
mkdir -p "$reconcile_home/.config/ods" "$reconcile_home/.openclaw/backups" \
    "$reconcile_home/.local/share/pixel"
chmod 0700 "$reconcile_home/.openclaw" "$reconcile_home/.openclaw/backups"
chmod 0700 "$reconcile_home/.config/ods"
cp "$answers" "$reconcile_answers"
python3 - "$reconcile_answers" "$reconcile_config" "$reconcile_candidate" <<'PY'
import copy, json, pathlib, sys

answers_path, live_path, candidate_path = map(pathlib.Path, sys.argv[1:])
answers = json.loads(answers_path.read_text())
answers["modelProvider"] = "ods-local"
answers["modelId"] = "qwen-old"
answers["modelName"] = "ODS Local qwen-old"
answers["modelBaseUrl"] = "http://127.0.0.1:11434/v1"
answers["modelApiKey"] = "local-no-auth"
answers_path.write_text(json.dumps(answers, indent=2, sort_keys=True) + "\n")
base = {
    "agents": {
        "defaults": {"bootstrapMaxChars": 32000},
        "list": [{
            "id": "pixel",
            "model": "ods-local/qwen-old",
            "preserve": 7,
            "tools": {"deny": ["web_fetch", "web_search"]},
        }],
    },
    "gateway": {"bind": "loopback"},
    "models": {"providers": {"ods-local": {
        "api": "openai-completions",
        "apiKey": "local-no-auth",
        "baseUrl": "http://127.0.0.1:11434/v1",
        "models": [{
            "id": "qwen-old",
            "name": "ODS Local qwen-old",
            "contextWindow": answers["modelContextWindow"],
            "maxTokens": answers["modelMaxTokens"],
            "reasoning": answers["modelReasoning"],
            "input": ["text"],
        }],
    }}},
    "plugins": {"entries": {"searxng": {
        "enabled": True,
        "config": {"webSearch": {"baseUrl": "http://127.0.0.1:8888"}},
    }}},
    "session": {"dmScope": "per-account-channel-peer"},
    "tools": {
        "profile": "coding",
        "sandbox": {"tools": {"allow": ["exec", "pixel_ods_status"]}},
        "web": {"search": {"provider": "searxng"}},
    },
}
candidate = copy.deepcopy(base)
candidate["agents"]["list"][0]["model"] = "ods-local/qwen-new"
candidate_model = candidate["models"]["providers"]["ods-local"]["models"][0]
candidate_model["id"] = "qwen-new"
candidate_model["name"] = "ODS Local qwen-new"
candidate_model["contextWindow"] = 65536
candidate_model["maxTokens"] = 2048
candidate_model["reasoning"] = True
live_path.write_text(json.dumps(base, indent=2, sort_keys=True) + "\n")
candidate_path.write_text(json.dumps(candidate, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$reconcile_answers" "$reconcile_config" "$reconcile_candidate"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$reconcile_home" "$reconcile_config" "$runtime_validator")" = changed
python3 - "$reconcile_config" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
agent = value["agents"]["list"][0]
model = value["models"]["providers"]["ods-local"]["models"][0]
agent.pop("thinkingDefault", None)
model.pop("compat", None)
model["reasoning"] = False
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
printf '%s\n' '{"kind":"pixel-runtime-attestation"}' > "$reconcile_home/.local/share/pixel/runtime-attestation.json"
chmod 0600 "$reconcile_home/.local/share/pixel/runtime-attestation.json"
python3 - "$reconcile_marker" "$reconcile_config" "$INSTALL_DIR" "$reconcile_ref" <<'PY'
import hashlib, json, pathlib, sys

marker, config, install_dir, source_ref = sys.argv[1:]
value = json.loads(pathlib.Path(config).read_text())
canonical = json.dumps(value, sort_keys=True, separators=(",", ":")).encode()
payload = {
    "schema_version": 2,
    "manager": "ods",
    "state": "ready",
    "initial_active_state": "absent",
    "install_dir": install_dir,
    "pixel_source_ref": source_ref,
    "contract_sha256": "a" * 64,
    "configuration_sha256": hashlib.sha256(b"ods-pixel-openclaw-v1\0" + canonical).hexdigest(),
    "active_release_version": "4.3.14",
    "release_identity_sha256": "b" * 64,
    "install_manifest_sha256": "c" * 64,
    "sandbox_image": "openclaw-sandbox:test",
    "sandbox_image_id": "sha256:" + "d" * 64,
}
pathlib.Path(marker).write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$reconcile_marker"
check test "$(_ods_pixel_managed_source_ref "$owner" "$reconcile_home")" = "$reconcile_ref"
if (
    unset PIXEL_SOURCE_URL
    [[ "$(_ods_pixel_reconciliation_source_url "$ODS_PIXEL_BUNDLED_REF")" == bundled ]]
    ! _ods_pixel_reconciliation_source_url b33730436baf5d98bf58f7d57c090318fe19f433 >/dev/null 2>&1
    PIXEL_SOURCE_URL=bundled
    ! _ods_pixel_reconciliation_source_url b33730436baf5d98bf58f7d57c090318fe19f433 >/dev/null 2>&1
    PIXEL_SOURCE_URL=https://github.com/Osmantic/Pixel.git
    ! _ods_pixel_reconciliation_source_url "$ODS_PIXEL_BUNDLED_REF" >/dev/null 2>&1
    PIXEL_SOURCE_URL=/safe/developer-checkout
    [[ "$(_ods_pixel_reconciliation_source_url "$ODS_PIXEL_BUNDLED_REF")" == /safe/developer-checkout ]]
); then
    pass "model reconciliation uses bundled or explicit local source and rejects private or mismatched pins"
else
    fail "model reconciliation accepted a private or mismatched source"
fi
mkdir -p "$reconcile_home/.config/pixel-deployment"
chmod 0700 "$reconcile_home/.config/pixel-deployment"
cp "$reconcile_answers" "$reconcile_home/.config/pixel-deployment/onboarding.json"
chmod 0600 "$reconcile_home/.config/pixel-deployment/onboarding.json"
reconcile_backup="$(_ods_pixel_model_reconciliation_snapshot "$owner" "$reconcile_home" "$reconcile_answers")"
check cmp -s "$reconcile_backup/installed-onboarding.json" "$reconcile_answers"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelId"] == "qwen-old" and v["modelName"] == "ODS Local qwen-old"' "$reconcile_backup/rollback-onboarding.json"
rm -f "$reconcile_home/.local/share/pixel/runtime-attestation.json"
missing_attestation_backup="$(_ods_pixel_model_reconciliation_snapshot \
    "$owner" "$reconcile_home" "$reconcile_answers")"
check test ! -e "$missing_attestation_backup/runtime-attestation.json"
ln -s "$reconcile_config" "$reconcile_home/.local/share/pixel/runtime-attestation.json"
if _ods_pixel_model_reconciliation_snapshot "$owner" "$reconcile_home" \
    "$reconcile_answers" >/dev/null 2>&1; then
    fail "symlink Pixel runtime attestation rejected during model snapshot"
else
    pass "symlink Pixel runtime attestation rejected during model snapshot"
fi
rm -f "$reconcile_home/.local/share/pixel/runtime-attestation.json"
printf '%s\n' '{"kind":"pixel-runtime-attestation"}' \
    > "$reconcile_home/.local/share/pixel/runtime-attestation.json"
chmod 0600 "$reconcile_home/.local/share/pixel/runtime-attestation.json"
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$reconcile_answers" \
    qwen-new 65536 2048 true
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelId"] == "qwen-new" and v["modelName"] == "ODS Local qwen-new" and v["modelContextWindow"] == 65536 and v["modelMaxTokens"] == 2048 and v["modelReasoning"] is True' "$reconcile_answers"
check _ods_pixel_install_onboarding_mirror "$owner" "$reconcile_home" "$reconcile_answers"
check cmp -s "$reconcile_answers" "$reconcile_home/.config/pixel-deployment/onboarding.json"
check test "$(stat -c '%a' "$reconcile_home/.config/pixel-deployment/onboarding.json")" = 600
mv "$reconcile_home/.config/pixel-deployment/onboarding.json" "$TEST_ROOT/mirror-before-symlink"
ln -s "$reconcile_config" "$reconcile_home/.config/pixel-deployment/onboarding.json"
if _ods_pixel_install_onboarding_mirror "$owner" "$reconcile_home" "$reconcile_answers" >/dev/null 2>&1; then
    fail "symlink installed onboarding mirror rejected"
else
    pass "symlink installed onboarding mirror rejected"
fi
rm "$reconcile_home/.config/pixel-deployment/onboarding.json"
mv "$TEST_ROOT/mirror-before-symlink" "$reconcile_home/.config/pixel-deployment/onboarding.json"
if _ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$reconcile_answers" \
    qwen-invalid 2048 1024 false >/dev/null 2>&1; then
    fail "undersized Pixel model context rejected"
else
    pass "undersized Pixel model context rejected"
fi
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$reconcile_home" "$reconcile_candidate" "$runtime_validator" "$reconcile_answers")" = changed
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); a=v["agents"]["list"][0]; m=v["models"]["providers"]["ods-local"]["models"][0]; assert m["reasoning"] is True and m["compat"] == {"thinkingFormat":"qwen-chat-template"} and a["thinkingDefault"] == "low" and a["params"]["chat_template_kwargs"]["enable_thinking"] is True' "$reconcile_candidate"
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" "$reconcile_candidate" "$reconcile_answers"
# A measured thinking control in the answers replaces the "qwen" name rule for
# the same route. The validator accepts exactly what the renderer writes for
# each control and refuses a candidate rendered for another one.
for thinking_control in enable_thinking always none; do
    control_answers="$TEST_ROOT/thinking-$thinking_control-onboarding.json"
    control_candidate="$TEST_ROOT/thinking-$thinking_control-candidate.json"
    cp "$reconcile_answers" "$control_answers"
    cp "$reconcile_candidate" "$control_candidate"
    chmod 0600 "$control_answers" "$control_candidate"
    check _ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$control_answers" \
        qwen-new 65536 2048 true "" unknown "$thinking_control"
    check python3 -c 'import json,sys; assert json.load(open(sys.argv[1]))["modelThinkingControl"] == sys.argv[2]' \
        "$control_answers" "$thinking_control"
    export ODS_TEST_THINKING_CONTROL="$thinking_control"
    check test -n "$(_ods_pixel_apply_runtime_budget "$owner" "$reconcile_home" "$control_candidate" "$runtime_validator" "$control_answers")"
    unset ODS_TEST_THINKING_CONTROL
    check python3 - "$control_candidate" "$thinking_control" <<'PY'
import json, sys
value = json.load(open(sys.argv[1]))
agent = value["agents"]["list"][0]
model = value["models"]["providers"]["ods-local"]["models"][0]
thinking = (model["reasoning"], model.get("compat"), agent.get("thinkingDefault"),
            agent.get("params", {}).get("chat_template_kwargs"))
assert thinking == {
    "enable_thinking": (True, {"thinkingFormat": "qwen-chat-template"}, "low", {"enable_thinking": True}),
    "always": (True, None, None, None),
    "none": (False, None, None, None),
}[sys.argv[2]], thinking
assert "modelThinkingControl" not in value["plugins"]["entries"]["pixel-ods"]["config"]
PY
    check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" "$control_candidate" "$control_answers"
    if [[ "$thinking_control" != enable_thinking ]]; then
        if _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
            "$control_candidate" "$reconcile_answers" >/dev/null 2>&1; then
            fail "candidate rendered for thinking control $thinking_control rejected under the name rule"
        else
            pass "candidate rendered for thinking control $thinking_control rejected under the name rule"
        fi
    fi
done
# A model promoted without a measured control never keeps an earlier one.
check _ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$control_answers" qwen-new 65536 2048 true
check python3 -c 'import json,sys; assert "modelThinkingControl" not in json.load(open(sys.argv[1]))' "$control_answers"
cp "$control_answers" "$TEST_ROOT/thinking-before-invalid.json"
if _ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$control_answers" \
    qwen-new 65536 2048 true "" unknown sometimes >/dev/null 2>&1; then
    fail "invalid promoted Pixel thinking control rejected"
else
    pass "invalid promoted Pixel thinking control rejected"
fi
check cmp -s "$control_answers" "$TEST_ROOT/thinking-before-invalid.json"
cp "$reconcile_config" "$TEST_ROOT/reconcile-config-with-control-bind.json"
python3 - "$reconcile_config" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
value["agents"]["defaults"].pop("sandbox", None)
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$reconcile_config"
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" "$reconcile_candidate" "$reconcile_answers"
cp "$TEST_ROOT/reconcile-config-with-control-bind.json" "$reconcile_config"
chmod 0600 "$reconcile_config"
python3 - "$reconcile_candidate" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
value["gateway"]["bind"] = "lan"
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
if _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" "$reconcile_candidate" "$reconcile_answers" >/dev/null 2>&1; then
    fail "unmanaged Pixel candidate change rejected"
else
    pass "unmanaged Pixel candidate change rejected"
fi
python3 - "$reconcile_candidate" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
value["gateway"]["bind"] = "loopback"
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$reconcile_candidate"
check _ods_pixel_atomic_replace_managed_file "$owner" "$reconcile_home" "$reconcile_candidate" "$reconcile_config"
check _ods_pixel_candidate_config_matches_live "$owner" "$reconcile_home" "$reconcile_candidate"
linked_reconcile_candidate="$TEST_ROOT/reconcile-candidate-link.json"
ln -s "$reconcile_candidate" "$linked_reconcile_candidate"
if _ods_pixel_atomic_replace_managed_file "$owner" "$reconcile_home" "$linked_reconcile_candidate" "$reconcile_config" >/dev/null 2>&1; then
    fail "symlink model reconciliation source rejected"
else
    pass "symlink model reconciliation source rejected"
fi

# A model-family change is part of the supported ODS swap contract, not only a
# Qwen-to-Qwen rename. The exact model limits may change and Qwen-only runtime
# policy must disappear, while every unrelated Pixel field stays identical.
non_qwen_answers="$TEST_ROOT/non-qwen-onboarding.json"
non_qwen_candidate="$TEST_ROOT/non-qwen-candidate.json"
cp "$reconcile_answers" "$non_qwen_answers"
cp "$reconcile_config" "$non_qwen_candidate"
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$non_qwen_answers" \
    phi-4-mini 128000 4096 false
python3 - "$non_qwen_candidate" <<'PY'
import json, pathlib, sys

path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
agent = next(item for item in value["agents"]["list"] if item.get("id") == "pixel")
model = value["models"]["providers"]["ods-local"]["models"][0]
agent["model"] = "ods-local/phi-4-mini"
agent.pop("thinkingDefault", None)
model.update({
    "id": "phi-4-mini",
    "name": "ODS Local phi-4-mini",
    "contextWindow": 128000,
    "maxTokens": 4096,
    "reasoning": False,
})
model.pop("compat", None)
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$non_qwen_answers" "$non_qwen_candidate"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$reconcile_home" "$non_qwen_candidate" "$runtime_validator" "$non_qwen_answers")" = changed
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$non_qwen_candidate" "$non_qwen_answers"
check _ods_pixel_atomic_replace_managed_file "$owner" "$reconcile_home" \
    "$non_qwen_candidate" "$reconcile_config"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); a=v["agents"]["list"][0]; m=v["models"]["providers"]["ods-local"]["models"][0]; assert m["id"] == "phi-4-mini" and m["contextWindow"] == 128000 and m["maxTokens"] == 4096 and m["reasoning"] is False and "compat" not in m and "thinkingDefault" not in a; assert a["bootstrapMaxChars"] == 2000 and a["bootstrapTotalMaxChars"] == 6000 and a["contextInjection"] == "never"; assert {k:a["params"][k] for k in ("temperature","topP","frequencyPenalty","presencePenalty")} == {"temperature":0.7,"topP":0.8,"frequencyPenalty":0.6,"presencePenalty":0.2}; assert v["plugins"]["entries"]["pixel-ods"]["config"]["leanPrompt"] is True' "$reconcile_config"

# Migrate an already verified direct llama.cpp route to the authenticated ODS
# model gateway. Only the provider route, stable alias, concrete-model metadata,
# and deterministic runtime policy may change; every unrelated Pixel field must
# remain byte-equivalent after normalization. The rollback contract must still
# describe the exact prior direct route.
gateway_answers="$TEST_ROOT/gateway-reconcile-onboarding.json"
gateway_candidate="$TEST_ROOT/gateway-reconcile-candidate.json"
cp "$answers" "$gateway_answers"
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$gateway_answers" \
    qwen-gateway 65536 4096 false
python3 - "$reconcile_config" "$gateway_candidate" <<'PY'
import json, pathlib, sys

source, target = map(pathlib.Path, sys.argv[1:])
value = json.loads(source.read_text())
providers = value["models"]["providers"]
provider = providers.pop("ods-local")
providers["ods-gateway"] = provider
provider["apiKey"] = "test-pixel-relay-secret"
provider["baseUrl"] = "http://127.0.0.1:4006/v1"
model = provider["models"][0]
model.update({
    "id": "ods/current",
    "name": "ODS Current (qwen-gateway)",
    "contextWindow": 65536,
    "maxTokens": 4096,
    "reasoning": False,
})
model.pop("compat", None)
agent = next(item for item in value["agents"]["list"] if item.get("id") == "pixel")
agent["model"] = "ods-gateway/ods/current"
agent.pop("thinkingDefault", None)
agent["params"] = {"chat_template_kwargs": {"enable_thinking": False}}
value["agents"]["defaults"]["compaction"] = {
    "reserveTokens": (model["contextWindow"] + 4 * model["maxTokens"] + 4) // 5,
    "reserveTokensFloor": 0,
    "timeoutSeconds": 1800,
    "keepRecentTokens": 4096,
}
target.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$gateway_answers" "$gateway_candidate"
gateway_budget_status="$(_ods_pixel_apply_runtime_budget "$owner" "$reconcile_home" \
    "$gateway_candidate" "$runtime_validator" "$gateway_answers")"
check test "$gateway_budget_status" = changed
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$gateway_candidate" "$gateway_answers"
cp "$reconcile_config" "$TEST_ROOT/pre-gateway-alias-config.json"
cp "$gateway_candidate" "$reconcile_config"
chmod 0600 "$reconcile_config"
search_parallel_live="$TEST_ROOT/search-parallel-live.json"
search_searx_candidate="$TEST_ROOT/search-searx-candidate.json"
search_parallel_answers="$TEST_ROOT/search-parallel-answers.json"
search_searx_answers="$TEST_ROOT/search-searx-answers.json"
python3 - "$gateway_candidate" "$gateway_answers" "$INSTALL_DIR" \
    "$search_parallel_live" "$search_searx_candidate" \
    "$search_parallel_answers" "$search_searx_answers" <<'PY'
import copy, json, pathlib, sys

source, contract_source, install_root, parallel_file, searx_file, parallel_answers, searx_answers = map(pathlib.Path, sys.argv[1:])
parallel_path = str(install_root / "data/pixel/native-search/parallel-2026.6.33")
base = json.loads(source.read_text(encoding="utf-8"))
contract = json.loads(contract_source.read_text(encoding="utf-8"))
parallel = copy.deepcopy(base)
parallel.setdefault("tools", {}).setdefault("web", {})["search"] = {"provider": "parallel-free"}
plugins = parallel.setdefault("plugins", {})
plugins["allow"] = ["pixel-ods", "parallel", "llama-cpp"]
plugins.setdefault("entries", {}).pop("searxng", None)
plugins["entries"]["parallel"] = {"enabled": True}
plugins["load"] = {"paths": ["/opt/ods/pixel-ods", parallel_path]}
searx = copy.deepcopy(parallel)
searx["tools"]["web"]["search"] = {"provider": "searxng"}
searx["plugins"]["allow"] = ["pixel-ods", "searxng", "llama-cpp"]
searx["plugins"]["entries"].pop("parallel")
searx["plugins"]["entries"]["searxng"] = {
    "enabled": True, "config": {"webSearch": {"baseUrl": "http://127.0.0.1:8888"}}}
searx["plugins"]["load"]["paths"] = ["/opt/ods/pixel-ods"]
bound = [item for item in contract["gatewayExtensions"] if item["id"] != "parallel"]
parallel_contract = dict(contract, webSearchProvider="parallel-free",
                         gatewayExtensions=bound + [{"id": "parallel", "path": parallel_path,
                                                     "sha256": "b" * 64}])
searx_contract = dict(contract, webSearchProvider="searxng",
                      searxngBaseUrl="http://127.0.0.1:8888", gatewayExtensions=bound)
for path, value in ((parallel_file, parallel), (searx_file, searx),
                    (parallel_answers, parallel_contract), (searx_answers, searx_contract)):
    path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    path.chmod(0o600)
PY
cp "$search_parallel_live" "$reconcile_config"
chmod 0600 "$reconcile_config"
check _ods_pixel_search_provider_matches_contract "$owner" "$reconcile_home" "$search_parallel_answers"
if _ods_pixel_search_provider_matches_contract "$owner" "$reconcile_home" "$search_searx_answers" >/dev/null 2>&1; then
    fail "changed Pixel search provider skipped the model-only shortcut"
else
    pass "changed Pixel search provider requires rendered reconciliation"
fi
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$search_searx_candidate" "$search_searx_answers"
cp "$search_searx_candidate" "$TEST_ROOT/search-searx-clean.json"
python3 - "$search_searx_candidate" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text(encoding="utf-8"))
value["gateway"]["bind"] = "lan"
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n", encoding="utf-8")
PY
if _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$search_searx_candidate" "$search_searx_answers" >/dev/null 2>&1; then
    fail "search change allowed an unrelated gateway bind change"
else
    pass "search change rejects unrelated Pixel configuration drift"
fi
cp "$TEST_ROOT/search-searx-clean.json" "$search_searx_candidate"
cp "$search_searx_candidate" "$reconcile_config"
chmod 0600 "$reconcile_config"
check _ods_pixel_search_provider_matches_contract "$owner" "$reconcile_home" "$search_searx_answers"
search_searx_port_answers="$TEST_ROOT/search-searx-port-onboarding.json"
search_searx_port_candidate="$TEST_ROOT/search-searx-port-candidate.json"
cp "$search_searx_answers" "$search_searx_port_answers"
cp "$search_searx_candidate" "$search_searx_port_candidate"
python3 - "$search_searx_port_answers" "$search_searx_port_candidate" <<'PY'
import json, pathlib, sys
answers_path, candidate_path = map(pathlib.Path, sys.argv[1:])
origin = "http://127.0.0.1:8899"
for path in (answers_path, candidate_path):
    value = json.loads(path.read_text(encoding="utf-8"))
    if path == answers_path:
        value["searxngBaseUrl"] = origin
    else:
        value["plugins"]["entries"]["searxng"]["config"]["webSearch"]["baseUrl"] = origin
    path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n", encoding="utf-8")
PY
chmod 0600 "$search_searx_port_answers" "$search_searx_port_candidate"
if _ods_pixel_search_provider_matches_contract "$owner" "$reconcile_home" \
    "$search_searx_port_answers" >/dev/null 2>&1; then
    fail "changed SearXNG origin skipped rendered reconciliation"
else
    pass "changed SearXNG origin requires rendered reconciliation"
fi
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$search_searx_port_candidate" "$search_searx_port_answers"
cp "$search_searx_port_candidate" "$reconcile_config"
chmod 0600 "$reconcile_config"
check _ods_pixel_search_provider_matches_contract "$owner" "$reconcile_home" \
    "$search_searx_port_answers"
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$search_parallel_live" "$search_parallel_answers"
cp "$gateway_candidate" "$reconcile_config"
chmod 0600 "$reconcile_config"
check _ods_pixel_uses_stable_model_alias "$owner" "$reconcile_home" "$gateway_answers"
check _ods_pixel_stable_alias_matches_promoted_model "$owner" "$reconcile_home" \
    "$gateway_answers" qwen-gateway 65536 4096 false
route_alias_answers="$TEST_ROOT/route-alias-onboarding.json"
cp "$gateway_answers" "$route_alias_answers"
route_fingerprint="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$route_alias_answers" \
    qwen-gateway 65536 4096 false "$route_fingerprint"
route_alias_candidate="$(_ods_pixel_stage_stable_alias_candidate \
    "$owner" "$reconcile_home" "$route_alias_answers")"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$reconcile_home" \
    "$route_alias_candidate" "$runtime_validator" "$route_alias_answers")" = unchanged
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$route_alias_candidate" "$route_alias_answers"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["plugins"]["entries"]["pixel-ods"]["config"]["modelRouteFingerprint"] == sys.argv[2]' \
    "$route_alias_candidate" "$route_fingerprint"
rm -f -- "$route_alias_candidate"
stale_alias_answers="$TEST_ROOT/stale-alias-onboarding.json"
cp "$gateway_answers" "$stale_alias_answers"
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$stale_alias_answers" \
    qwen-gateway-next 32768 4096 false
staged_alias_candidate="$(_ods_pixel_stage_stable_alias_candidate \
    "$owner" "$reconcile_home" "$stale_alias_answers")"
check test "$(_ods_pixel_apply_runtime_budget "$owner" "$reconcile_home" \
    "$staged_alias_candidate" "$runtime_validator")" = changed
check _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$staged_alias_candidate" "$stale_alias_answers"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); m=v["models"]["providers"]["ods-gateway"]["models"][0]; assert m["id"] == "ods/current" and m["name"] == "ODS Current (qwen-gateway-next)" and m["contextWindow"] == 32768 and m["maxTokens"] == 4096 and m["reasoning"] is False; assert v["agents"]["defaults"]["compaction"] == {"reserveTokens":9831,"reserveTokensFloor":0,"timeoutSeconds":1800,"keepRecentTokens":2048}; assert v["agents"]["list"][0]["contextLimits"] == {"toolResultMaxChars":8192}' \
    "$staged_alias_candidate"
rm -f -- "$staged_alias_candidate"
if _ods_pixel_stable_alias_matches_promoted_model "$owner" "$reconcile_home" \
    "$gateway_answers" qwen-gateway-next 32768 4096 false >/dev/null 2>&1; then
    fail "stable Pixel alias rejected stale concrete-model limits"
else
    pass "stable Pixel alias detects stale concrete-model limits"
fi
cp "$TEST_ROOT/pre-gateway-alias-config.json" "$reconcile_config"
chmod 0600 "$reconcile_config"
if _ods_pixel_uses_stable_model_alias "$owner" "$reconcile_home" "$gateway_answers" >/dev/null 2>&1; then
    fail "stable Pixel model alias rejected a direct-model live route"
else
    pass "stable Pixel model alias rejects a direct-model live route"
fi
python3 - "$reconcile_marker" "$reconcile_config" <<'PY'
import hashlib, json, pathlib, sys

marker_path, config_path = map(pathlib.Path, sys.argv[1:])
marker = json.loads(marker_path.read_text())
config = json.loads(config_path.read_text())
canonical = json.dumps(config, sort_keys=True, separators=(",", ":")).encode()
marker["configuration_sha256"] = hashlib.sha256(b"ods-pixel-openclaw-v1\0" + canonical).hexdigest()
marker_path.write_text(json.dumps(marker, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$reconcile_marker"
gateway_migration_backup="$(_ods_pixel_model_reconciliation_snapshot \
    "$owner" "$reconcile_home" "$gateway_answers")"
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelProvider"] == "ods-local" and v["modelId"] == "phi-4-mini" and v["modelName"] == "ODS Local phi-4-mini" and v["modelBaseUrl"] == "http://127.0.0.1:11434/v1" and v["modelApiKey"] == "local-no-auth" and v["modelContextWindow"] == 128000 and v["modelMaxTokens"] == 4096 and v["modelReasoning"] is False' \
    "$gateway_migration_backup/rollback-onboarding.json"
cp "$gateway_candidate" "$TEST_ROOT/gateway-candidate-unsafe-route.json"
python3 - "$TEST_ROOT/gateway-candidate-unsafe-route.json" <<'PY'
import json, pathlib, sys
path = pathlib.Path(sys.argv[1])
value = json.loads(path.read_text())
value["models"]["providers"]["ods-gateway"]["baseUrl"] = "http://example.invalid/v1"
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
PY
chmod 0600 "$TEST_ROOT/gateway-candidate-unsafe-route.json"
if _ods_pixel_candidate_is_managed_runtime_update "$owner" "$reconcile_home" \
    "$TEST_ROOT/gateway-candidate-unsafe-route.json" "$gateway_answers" >/dev/null 2>&1; then
    fail "non-loopback Pixel gateway migration rejected"
else
    pass "non-loopback Pixel gateway migration rejected"
fi
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$gateway_answers" \
    qwen-gateway-next 131072 8192 true
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelProvider"] == "ods-gateway" and v["modelId"] == "ods/current" and v["modelName"] == "ODS Current (qwen-gateway-next)" and v["modelContextWindow"] == 131072 and v["modelMaxTokens"] == 8192 and v["modelReasoning"] is True' \
    "$gateway_answers"
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$gateway_answers" \
    'org/qwen+tools:remote' 131072 8192 true
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelId"] == "ods/current" and v["modelName"] == "ODS Current (org/qwen+tools:remote)"' \
    "$gateway_answers"
_ods_pixel_update_onboarding_model "$owner" "$reconcile_home" "$gateway_answers" \
    'My Custom Model (Q4_K_M).gguf' 32768 4096 false
check python3 -c 'import json,sys; v=json.load(open(sys.argv[1])); assert v["modelId"] == "ods/current" and v["modelName"] == "ODS Current (My Custom Model (Q4_K_M).gguf)" and v["modelContextWindow"] == 32768' \
    "$gateway_answers"
check test "$(GGUF_FILE='My Custom Model (Q4_K_M).gguf' \
    LLM_MODEL='friendly-library-alias' GPU_BACKEND=nvidia \
    _ods_pixel_runtime_model_identity)" = 'My Custom Model (Q4_K_M).gguf'
# AMD runs llama.cpp like every GPU: the served id is the GGUF file name
# (--alias), with no Lemonade "extra." prefix.
check test "$(GGUF_FILE='My Custom Model (Q4_K_M).gguf' \
    LLM_MODEL='friendly-library-alias' GPU_BACKEND=amd LLM_BACKEND=llama-server \
    AMD_INFERENCE_RUNTIME=llama-server \
    _ods_pixel_runtime_model_identity)" = 'My Custom Model (Q4_K_M).gguf'
# The Windows Portal's host-native llama-server serves the GGUF Windows chose,
# which phase 02 records in GGUF_FILE.
check test "$(GGUF_FILE='Qwen3.6-35B-A3B-UD-Q4_K_M.gguf' GPU_BACKEND=cpu \
    NATIVE_LLM_BASE_URL=http://localhost:8080 \
    _ods_pixel_runtime_model_identity)" = 'Qwen3.6-35B-A3B-UD-Q4_K_M.gguf'
if EXTERNAL_LLM_URL='http://10.0.2.2:18080' EXTERNAL_LLM_MODEL='' GGUF_FILE='stale-local.gguf' \
    _ods_pixel_runtime_model_identity >/dev/null 2>&1; then
    fail "external runtime identity requires the served model"
else
    pass "external runtime identity requires the served model"
fi
check test "$(EXTERNAL_LLM_URL='http://10.0.2.2:18080' \
    EXTERNAL_LLM_MODEL='org/qwen+tools:remote' GGUF_FILE='stale-local.gguf' \
    _ods_pixel_runtime_model_identity)" = 'org/qwen+tools:remote'
if EXTERNAL_LLM_URL='http://10.0.2.2:18080' EXTERNAL_LLM_MODEL='' \
    _ods_pixel_runtime_model_identity >/dev/null 2>&1; then
    fail "external runtime identity requires the selected upstream model"
else
    pass "external runtime identity requires the selected upstream model"
fi

linked_answers="$TEST_ROOT/onboarding-linked.json"
printf '%s\n' 'sentinel' > "$TEST_ROOT/onboarding-link-target"
ln -s "$TEST_ROOT/onboarding-link-target" "$linked_answers"
if _ods_pixel_write_onboarding "$owner" "$home" "$linked_answers" /usr/bin/openclaw /opt/ods/pixel-plugin "$digest" >/dev/null 2>&1; then
    fail "symlink Pixel onboarding contract rejected"
else
    pass "symlink Pixel onboarding contract rejected"
fi
check test "$(cat "$TEST_ROOT/onboarding-link-target")" = sentinel

original_run_as_owner="$(declare -f ods_pixel_run_as_owner)"
mock_ingress_attempts="$TEST_ROOT/ingress-attempts"
ods_pixel_run_as_owner() {
    local count=0
    [[ -f "$mock_ingress_attempts" ]] && read -r count < "$mock_ingress_attempts"
    count=$((count + 1))
    printf '%s\n' "$count" > "$mock_ingress_attempts"
    (( count >= 3 )) || return 7
    printf '%s\n' '{"status":"ok"}'
}
check _ods_pixel_wait_ingress "$owner" "$home" 3 0
check test "$(cat "$mock_ingress_attempts")" = 3
ods_pixel_run_as_owner() { printf '%s\n' '{"status":"starting"}'; }
if _ods_pixel_wait_ingress "$owner" "$home" 2 0; then
    fail "non-ready Pixel ingress status rejected"
else
    pass "non-ready Pixel ingress status rejected"
fi
eval "$original_run_as_owner"

# The access coordinator restart is asynchronous. Prove the fresh-install
# helper retries a bounded number of fail-closed attempts and stops immediately
# after the protected reconciliation succeeds.
access_reconcile_attempts="$TEST_ROOT/access-reconcile-attempts"
printf '0\n' > "$access_reconcile_attempts"
ods_pixel_run_as_owner() {
    local count
    read -r count < "$access_reconcile_attempts"
    count=$((count + 1))
    printf '%s\n' "$count" > "$access_reconcile_attempts"
    (( count >= 3 ))
}
sleep() { :; }
check _ods_pixel_wait_access_reconcile "$owner" "$home" /protected/reconcile.py 5 1
check test "$(cat "$access_reconcile_attempts")" = 3
printf '0\n' > "$access_reconcile_attempts"
if _ods_pixel_wait_access_reconcile "$owner" "$home" /protected/reconcile.py 2 1; then
    fail "Pixel access reconciliation exhausts its bounded retry budget"
else
    pass "Pixel access reconciliation exhausts its bounded retry budget"
fi
check test "$(cat "$access_reconcile_attempts")" = 2
unset -f sleep
eval "$original_run_as_owner"

# The long-lived host agent normally lacks an active sudo credential. Prove
# its fallback can only terminate the same-owner PID of the exact hardened
# Restart=always unit, then waits for systemd to replace it before verification.
gateway_mock_root="$TEST_ROOT/gateway-restart-mock"
mkdir -p "$gateway_mock_root"
printf '0\n' > "$gateway_mock_root/mainpid-calls"
original_installed_gateway_port="$(declare -f _ods_pixel_installed_gateway_port)"
_ods_pixel_installed_gateway_port() {
    [[ "$1" == "$owner" && "$2" == "$reconcile_home" ]] || return 1
    printf '%s\n' 18790
}
systemctl() {
    case "$*" in
        *"-p ProtectHome --value") printf "tmpfs\n"; return 0 ;;
        *"-p ProtectSystem --value") printf "strict\n"; return 0 ;;
    esac
    case "$*" in
        'show openclaw-gateway.service -p MainPID --value')
            local count
            read -r count < "$gateway_mock_root/mainpid-calls"
            count=$((count + 1))
            printf '%s\n' "$count" > "$gateway_mock_root/mainpid-calls"
            if (( count <= 2 )); then printf '111\n'; else printf '222\n'; fi
            ;;
        'show openclaw-gateway.service -p User --value') printf '%s\n' "$owner" ;;
        'show openclaw-gateway.service -p Restart --value') printf 'always\n' ;;
        'is-active --quiet openclaw-gateway.service') return 0 ;;
        *) return 1 ;;
    esac
}
id() {
    case "$1" in
        -u) printf '1000\n' ;;
        -un) printf '%s\n' "$owner" ;;
        *) return 1 ;;
    esac
}
awk() {
    [[ "$*" == *'/proc/111/status'* ]] || return 1
    printf '1000\n'
}
kill() {
    printf '%s\n' "$*" >> "$gateway_mock_root/kills"
}
curl() {
    printf '%s\n' '{"ok":true,"status":"live"}'
}
jq() {
    command jq "$@"
}
sleep() { :; }
ods_sudo_available() { return 1; }
ods_pixel_run_as_owner() {
    printf '%s\n' "$*" >> "$gateway_mock_root/owner-runs"
}
check _ods_pixel_restart_gateway_and_verify "$owner" "$reconcile_home" /verified/pixel
check test "$(cat "$gateway_mock_root/kills")" = '-TERM 111'
check grep -F '/verified/pixel/pixel verify' "$gateway_mock_root/owner-runs"
printf '0\n' > "$gateway_mock_root/mainpid-calls"
verification_transaction=$(printf 'a%.0s' {1..64})
check _ods_pixel_restart_gateway_and_verify "$owner" "$reconcile_home" /verified/pixel "$verification_transaction"
check grep -F "/verified/pixel/pixel verify --ods-model-transaction $verification_transaction" "$gateway_mock_root/owner-runs"
if _ods_pixel_restart_gateway_and_verify "$owner" "$reconcile_home" /verified/pixel invalid >/dev/null 2>&1; then
    fail "invalid verification transaction rejected"
else
    pass "invalid verification transaction rejected"
fi

# A mismatched systemd User must fail before any signal is sent.
printf '0\n' > "$gateway_mock_root/mainpid-calls"
: > "$gateway_mock_root/kills"
systemctl() {
    case "$*" in
        *"-p ProtectHome --value") printf "tmpfs\n"; return 0 ;;
        *"-p ProtectSystem --value") printf "strict\n"; return 0 ;;
    esac
    case "$*" in
        'show openclaw-gateway.service -p MainPID --value') printf '111\n' ;;
        'show openclaw-gateway.service -p User --value') printf 'someone-else\n' ;;
        'show openclaw-gateway.service -p Restart --value') printf 'always\n' ;;
        *) return 1 ;;
    esac
}
if _ods_pixel_restart_gateway_and_verify "$owner" "$reconcile_home" /verified/pixel >/dev/null 2>&1; then
    fail "mismatched Pixel gateway unit owner rejected"
else
    pass "mismatched Pixel gateway unit owner rejected"
fi
check test ! -s "$gateway_mock_root/kills"
unset -f systemctl id awk kill curl jq sleep ods_sudo_available
eval "$original_installed_gateway_port"
eval "$original_run_as_owner"

plugin="$ROOT/extensions/services/pixel-agent/plugin"
check node --check "$plugin/index.js"
check node --check "$plugin/projection.mjs"
check node --check "$plugin/prompt-contract.mjs"
check node --check "$plugin/playground-projects.mjs"
check node --check "$plugin/tool-content.mjs"
check node --check "$plugin/tool-loop-guard.mjs"
check node --check "$plugin/web-extract.mjs"
check node --check "$plugin/workspace-preview.mjs"
check node --check "$plugin/download-promote.mjs"
check node --check "$plugin/host-observe.mjs"
check python3 -m py_compile "$ROOT/extensions/services/pixel-agent/host/artifact_promoter.py"
check sh -n "$ROOT/extensions/services/pixel-agent/host/cancellable-exec.sh"
check bash -n "$ROOT/extensions/services/pixel-agent/host/noninteractive-sudo.sh"
check python3 -c '
import pathlib,sys
lines=[line for line in pathlib.Path(sys.argv[1]).read_text().splitlines() if line and not line.startswith("#")]
assert lines[:5] == ["[Service]","RestrictAddressFamilies=","RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK AF_VSOCK","PrivateDevices=false","DevicePolicy=closed"]
assert "DeviceAllow=/dev/dxg rw" in lines
assert "DeviceAllow=/dev/nvidiactl rw" in lines
assert "DeviceAllow=/dev/nvidia0 rw" in lines
assert all(line.startswith("DeviceAllow=/dev/") for line in lines[5:])
' "$ROOT/extensions/services/pixel-agent/host/pixel-ops-broker-ods.conf"
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
assert "sudo_entry=/usr/bin/sudo" in text
assert "trusted_sudo=\"$(readlink -e -- \"$sudo_entry\")\"" in text
assert "/usr/bin/sudo|/usr/lib/cargo/bin/sudo" in text
assert "stat -Lc" in text and "\"$trusted_parent\"" in text
assert "if [[ $# == 1 && \"$1\" == -v ]]" in text
assert "exec \"$trusted_sudo\" -n \"$@\"" in text
' "$ROOT/extensions/services/pixel-agent/host/noninteractive-sudo.sh"
check python3 -c '
import json,sys
p=json.load(open(sys.argv[1])); m=json.load(open(sys.argv[2]))
assert p["type"] == "module" and p["openclaw"]["extensions"] == ["./index.js"]
assert "dependencies" not in p
assert sorted(m["contracts"]["tools"]) == sorted(["pixel_ods_apps_list","pixel_ods_download_promote","pixel_ods_evidence_readback","pixel_ods_evidence_report","pixel_ods_extensions", "pixel_ods_host_command_propose","pixel_ods_host_observe","pixel_ods_research","pixel_ods_status","pixel_ods_web_extract","pixel_ods_workspace_preview","pixel_ods_project_build","pixel_ods_workspace_artifact", "pixel_ods_workspace_bundle","pixel_ods_workspace_preview_inspect","pixel_ods_ask_user","pixel_ods_goal", "pixel_ods_activity", "pixel_ods_history", "pixel_ods_image_read", "pixel_ods_skill", "pixel_ods_extension_proposal", "pixel_ods_source_proposal", "pixel_ods_python_library_proposal", "pixel_ods_extension_request_status", "pixel_ods_extension_request_prepare", "pixel_ods_extension_request_advance", "pixel_ods_extension_request_retry"])
import re
reserved = re.compile(r"^pixel_(?:gmail|calendar|social|web|ops|frontier)_")
assert all(name != "pixel_limb_status" and not reserved.match(name) for name in m["contracts"]["tools"])
assert m["toolMetadata"] == {
    "pixel_ods_project_build": {"replaySafe": False},
    "pixel_ods_workspace_bundle": {"replaySafe": False},
    "pixel_ods_workspace_artifact": {"replaySafe": False},
    "pixel_ods_workspace_preview_inspect": {"replaySafe": True},
    "pixel_ods_ask_user": {"replaySafe": True},
    "pixel_ods_goal": {"replaySafe": True},
    "pixel_ods_activity": {"replaySafe": True},
    "pixel_ods_history": {"replaySafe": True},
    "pixel_ods_image_read": {"replaySafe": True},
    "pixel_ods_skill": {"replaySafe": True},
    "pixel_ods_status": {"replaySafe": True},
    "pixel_ods_apps_list": {"replaySafe": True},
    "pixel_ods_extensions": {"replaySafe": True},
    "pixel_ods_host_observe": {"replaySafe": True},
}
' "$plugin/package.json" "$plugin/openclaw.plugin.json"
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
assert "api.on(\"before_prompt_build\"" in text
assert "api.on(\"model_call_started\"" in text
assert "api.on(\"before_agent_finalize\"" in text
assert "api.on(\"tool_result_persist\"" in text
assert "promptContractForAgent(context, AGENT_ID, ownerEvent, {" in text
assert "ownerEvent = toolLoopGuard.ownerIntentEventForRun(context?.runId ?? event?.runId, event)" in text
assert "verificationStatus: toolLoopGuard.verificationStatus(context?.runId)" in text
' "$plugin/index.js"
# Dollar expressions below are literal source-code assertions.
# shellcheck disable=SC2016
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
access_bridge=pathlib.Path(sys.argv[2]).read_text()
installer=text[text.index("ods_pixel_install_default_agent() {"):]
assert "local -a pixel_prerequisites=(litellm dashboard-api pixel-edge pixel-model-relay)" in installer
assert "if [[ \"$web_search_provider\" == searxng ]]; then\n        pixel_prerequisites+=(searxng)" in installer
assert "if [[ \"$web_search_provider\" == searxng ]]; then\n        _ods_pixel_wait_http \"ODS local search\"" in installer
assert installer.index("\"ODS local search\"") < installer.index("\"$pixel_root/pixel\" plan")
assert "ods_pixel_run_as_owner \"$owner\" \"$home\" curl" in text
assert "_ods_pixel_wait_ingress \"$owner\" \"$home\"" in installer
assert installer.index("_ods_pixel_wait_ingress \"$owner\" \"$home\"") < installer.index("_ods_pixel_mark_ready \"$owner\" \"$home\"")
assert "pixel\" configure --answers \"$answers\" --force" in text
assert "pixel\" plan" in text
native_search_provision = installer.index("--base-dir \"$INSTALL_DIR/data/pixel/native-search\"")
pixel_bootstrap = installer.index("\"$pixel_root/pixel\" bootstrap --apply")
native_search_digest = installer.index("\"$pixel_root/pixel\" extension-hash \"$parallel_path\"")
assert native_search_provision < pixel_bootstrap < native_search_digest
# A bootstrap failure names the downloads it needs, the recovery and the help link.
bootstrap_failure = installer[pixel_bootstrap:native_search_digest]
assert "Node.js runtime from nodejs.org" in bootstrap_failure
assert "then run the ODS install or update again" in bootstrap_failure
assert "https://discord.gg/4ntNp9MAwC" in bootstrap_failure
# A failed check of an existing candidate names that image and its removal
# instead of blaming the network.
stale = bootstrap_failure.index("stale_sandbox=\"$(_ods_pixel_failed_sandbox_proof_candidate \"$pixel_log\")\"")
assert stale < bootstrap_failure.index("Remove it with docker image rm $stale_sandbox, then run the ODS install or update again") \
    < bootstrap_failure.index("internet access and free disk space")
assert bootstrap_failure.count("https://discord.gg/4ntNp9MAwC") == 3
# A developer source without the sandbox Node.js runtime stops before apply.
node_label = installer.index("if ! _ods_pixel_sandbox_candidate_has_node \"$owner\" \"$home\" \"$pixel_root\"")
assert pixel_bootstrap < node_label < installer.index("\"$pixel_root/pixel\" apply --confirm")
assert "leave PIXEL_SOURCE_URL and PIXEL_SOURCE_REF unset" in installer[node_label:native_search_digest]
assert "Pixel configure failed. See $pixel_log" in text
assert "Pixel plan failed. See $pixel_log" in text
assert "Pixel configure or plan failed" not in text
assert text.count("pixel\" ops-broker --confirm") == 2
assert "Pixel could not install and verify the isolated Operations Broker" in text
assert text.count("_ods_pixel_verify_operations_policy_custody \"$owner\" \"$home\" \"$operations_policy\"") == 2
assert "PATH=\"$home/.openclaw/.ods-exec-control:$PATH\"" in text
assert text.count("pixel\" apply --confirm \"${release_arguments[@]}\" </dev/null &&") == 2
assert "_ods_pixel_write_operations_policy" in text
assert "Could not write the owner-private ODS Pixel Operations policy" in text
assert "_ods_pixel_write_extension_catalog" in text
assert "secret-free ODS extension catalog" in text
assert "_ods_pixel_write_extension_manager_unit" in text
assert "owner-private ODS Pixel extension manager service" in text
assert "_ods_pixel_write_artifact_promoter_unit" in text
assert "owner-private ODS Pixel artifact promoter service" in text
assert "ods-pixel-contract-v10" in text
assert "ods-pixel-system-observe.py" in text
assert "pixel-ops-broker-ods.conf" in text
for family in ("AF_UNIX", "AF_INET", "AF_INET6", "AF_NETLINK", "AF_VSOCK"):
    assert family in text
assert "CapabilityBoundingSet --value" in text
assert "bin/ods-pixel-approve" in text
assert "ods_sudo install -o root -g pixel-ops -m 0640 \"$extension_catalog\" \"$installed_extension_catalog\"" in text
assert "ods-extension-manager.py" in text
assert "pixel-extension-manager.service" in text
assert "manage-extensions" in text
assert "ods_sudo -u pixel-ops-broker /usr/bin/python3" in text
assert "_ods_pixel_wait_extension_manager_probe" in text
assert "_ods_pixel_wait_artifact_promoter_probe" in text
assert "_ods_pixel_wait_workspace_preview_probe" in text
assert "_ods_pixel_managed_contract_matches" in text
assert "_ods_pixel_verified_source_matches" in text
assert "_ods_pixel_candidate_config_matches_live" in text
assert "_ods_pixel_apply_runtime_budget" in text
assert "_ods_pixel_install_exec_control" in text
assert "_ods_pixel_recreate_agent_sandbox" in text
assert "_ods_pixel_refresh_plugin_registry" in text
assert "plugins registry --refresh --json" in text
assert "ods_pixel_reconcile_promoted_model" in text
stable_alias = text.index("if _ods_pixel_uses_stable_model_alias")
reconcile_snapshot = text.index("_ods_pixel_model_reconciliation_snapshot", stable_alias)
controller_install = text.index("_ods_pixel_install_access_service \"$owner\" \"$openclaw_bin\"", reconcile_snapshot)
model_begin = text.index("model_transaction=\"$(_ods_pixel_model_transition begin", controller_install)
model_mutation = text.index("_ods_pixel_update_onboarding_model", model_begin)
reconcile_restart = text.index("_ods_pixel_restart_gateway_and_verify", reconcile_snapshot)
model_finish = text.index("_ods_pixel_model_transition finish", reconcile_restart)
assert stable_alias < reconcile_snapshot < controller_install < model_begin < model_mutation < reconcile_restart < model_finish
transaction = text[model_begin:model_finish]
assert "_ods_pixel_reverify_unless_model_held \"$owner\" \"$home\" \"$model_transaction\"" in transaction
assert "_ods_pixel_reverify_access_after_gateway_restart \"$owner\" \"$home\";" not in transaction
stable_branch = text[stable_alias:reconcile_snapshot]
assert "Pixel stable model alias remains active" in stable_branch
assert "_ods_pixel_restart_gateway_and_verify" not in stable_branch
assert "\"$pixel_root/pixel\" verify" not in stable_branch
assert "_ods_pixel_wait_ingress \"$owner\" \"$home\" 6 1" in stable_branch
assert "_ods_pixel_verify_plugin_loaded \"$owner\" \"$home\" \"$openclaw_bin\"" in stable_branch
assert "if _ods_pixel_managed_contract_matches \"$owner\" \"$home\" \"$contract_sha256\"; then" in stable_branch
assert "_ods_pixel_managed_contract_matches \"$owner\" \"$home\" \"$contract_sha256\" || return 1" not in stable_branch
assert stable_branch.index("_ods_pixel_managed_contract_matches") < stable_branch.index("_ods_pixel_wait_ingress")
assert stable_branch.index("_ods_pixel_wait_ingress") < stable_branch.index("_ods_pixel_verify_plugin_loaded")
assert stable_branch.index("_ods_pixel_verify_plugin_loaded") < stable_branch.index("_ods_pixel_mark_ready")
assert stable_branch.index("Pixel stable model alias remains active") < stable_branch.index("stable_alias=true")
assert "failure_phase=\"onboarding-update\"" in text
assert "failure_phase=\"onboarding-mirror-install\"" in text
mirror_install = text.index("&& ! _ods_pixel_install_onboarding_mirror", reconcile_snapshot)
assert reconcile_snapshot < mirror_install < reconcile_restart
assert "installed-onboarding.json" in text
assert "failure_phase=\"pixel-configure\"" in text
assert "failure_phase=\"pixel-plan\"" in text
assert "failure_phase=\"stable-alias-candidate\"" in text
assert "failure_phase=\"runtime-budget\"" in text
assert "failure_phase=\"managed-update-validation\"" in text
assert "failure_phase=\"config-install\"" in text
assert "failure_phase=\"gateway-restart-verify\"" in text
assert "failure_phase=\"access-runtime-reproof\"" in text
assert "failure_phase=\"ingress-runtime-refresh\"" in text
assert "failure_phase=\"sandbox-recreate\"" in text
assert "failure_phase=\"contract-hash\"" in text
assert "failure_phase=\"ready-marker\"" in text
assert "failure_phase=\"installing-marker\"" in text
assert "failure_phase=\"model-transition-finish\"" in text
release_failure = text.index("transition release failed; recovery-required")
rollback_restore = text.index("_ods_pixel_restore_model_reconciliation", model_finish)
assert model_finish < release_failure < rollback_restore
assert chr(39) + "gateway_port" + chr(39) + ": gateway_port" in text
assert "pixel_access_reconcile.py" in text
assert "pixel_model_transition.py" in text
assert "def inspect(self, *, allow_installing=False):" in access_bridge
assert "self.discover(allow_installing=allow_installing)" in access_bridge
assert "snapshot = self.inspect(allow_installing=True)" in access_bridge
access_install = text.index("_ods_pixel_install_access_service()")
access_install_end = text.index("_ods_pixel_mark_installing()", access_install)
access_install_body = text[access_install:access_install_end]
access_restart = access_install_body.index("subprocess.run([")
access_socket_wait = access_install_body.index("socket_path = pathlib.Path(", access_restart)
assert access_restart < access_socket_wait
assert "systemctl" in access_install_body[access_restart:access_socket_wait]
assert "restart" in access_install_body[access_restart:access_socket_wait]
assert "ods-pixel-access.service" in access_install_body[access_restart:access_socket_wait]
assert "/run/ods-pixel-access/control.sock" in access_install_body[access_socket_wait:]
assert "stat.S_IMODE(directory_info.st_mode) == 0o711" in access_install_body
assert "socket_info.st_gid == owner.pw_gid" in access_install_body
assert "stat.S_IMODE(socket_info.st_mode) == 0o660" in access_install_body
assert "probe.connect(str(socket_path))" in access_install_body
assert "probe.sendall(b" in access_install_body
assert "operation" in access_install_body and "status" in access_install_body
assert "range(30)" in access_install_body
assert "Pixel access coordinator socket did not become owner-ready" in access_install_body
assert "_ods_pixel_reverify_access_after_gateway_restart \"$owner\" \"$home\" true" in text
assert "rollback=verified" in text
assert "rollback=failed" in text
assert installer.index("if _ods_pixel_verified_source_matches") < installer.index("_ods_pixel_mark_installing")
assert "The exact ODS-managed Pixel contract is already active" in text
assert "refreshing the verified ODS extension without reapplying the release" in text
assert "repairing the interrupted ownership checkpoint" in text
candidate_recovery = installer.index("Could not validate the exact-source Pixel runtime candidate")
model_reconcile = installer.index("ods_pixel_reconcile_promoted_model", candidate_recovery)
assert installer.index("_ods_pixel_apply_runtime_budget", candidate_recovery - 1000) < candidate_recovery < model_reconcile
resume_start = installer.index("if [[ \"$same_source_resume\" == true ]]")
resume_end = installer.index("ai \"The exact Pixel release is active with an older ODS route", resume_start)
resume = installer[resume_start:resume_end]
stop = resume.index("systemctl stop openclaw-gateway.service")
retire = resume.index("_ods_pixel_recreate_agent_sandbox \"$owner\" \"$home\" \"$openclaw_bin\"")
start = resume.index("systemctl start openclaw-gateway.service", retire)
health = resume.index("_ods_pixel_wait_gateway", start)
verify = resume.index("_ods_pixel_verify_current_runtime", health)
assert stop < retire < start < health < verify
helper_start = text.index("_ods_pixel_recreate_agent_sandbox()")
helper_end = text.index("_ods_pixel_apply_runtime_budget()", helper_start)
helper = text[helper_start:helper_end]
assert helper.index("sandbox recreate --agent pixel --force") < helper.index("docker ps --all --quiet")
for diagnostic in (
    "could not enter maintenance mode",
    "could not retire its stale agent sandbox",
    "could not restart after sandbox recovery",
    "did not become healthy after sandbox recovery",
    "failed verification",
):
    assert diagnostic in resume
assert "_ods_pixel_verify_current_runtime \"$owner\" \"$home\" \"$pixel_root\" >>\"$pixel_log\"" in text
assert "if ! _ods_pixel_install_ingress" in text
assert "systemctl restart pixel-ingress.service" in text
assert "RestartForceExitStatus --value" in text
assert chr(34) + "$restart_force" + chr(34) + " =~ (^|[[:space:]])HUP($|[[:space:]])" in text
assert "if ! _ods_pixel_mark_verified_installing" in text
assert text.index("_ods_pixel_mark_verified_installing \"$owner\"") < text.index("_ods_pixel_install_ingress \"$owner\"")
assert "if ! _ods_pixel_mark_ready" in text
runtime_overlay = installer.index("runtime_budget_status=\"")
runtime_checkpoint = installer.index(
    "Could not bind the verified Pixel ODS managed-runtime configuration.",
    runtime_overlay,
)
registry_refresh = installer.index("_ods_pixel_refresh_plugin_registry", runtime_overlay)
assert runtime_overlay < runtime_checkpoint < registry_refresh
# Bind the overlaid config before any fallible runtime repair, then restore
# patch sets from other ODS builds before this version applies its own.
foreign_restore = installer.index("--restore-foreign \"$home/.openclaw/ods-runtime-patches\"", runtime_overlay)
first_repair = installer.index("openclaw_tool_recovery.py", runtime_overlay)
own_repair = installer.index("--state-dir \"$home/.openclaw/ods-runtime-patches/", runtime_overlay)
assert runtime_checkpoint < first_repair < foreign_restore < own_repair
assert installer.count("Could not bind the verified Pixel ODS managed-runtime configuration.") == 1
import re
managed = re.findall(r"--state-dir \"\$home/\.openclaw/ods-runtime-patches/([a-z0-9-]+)\"", installer)
assert "for mkdir_module in bridge secure; do" in installer
assert "--sandbox-mkdir \"$mkdir_module\"" in installer
assert "--state-dir \"$home/.openclaw/ods-runtime-patches/sandbox-mkdir-$mkdir_module\"" in installer
managed.extend(["sandbox-mkdir-bridge", "sandbox-mkdir-secure"])
known = installer[foreign_restore:own_repair].split("--known", 1)[1].split(">>", 1)[0]
assert sorted(known.replace("\\", " ").split()) == sorted(managed)
assert len(set(managed)) == len(managed) == 23
assert "no-tools-ingress" in managed
assert {"context-usage", "yield-usage", "compaction-empty", "compaction-no-work", "subagent-admission", "hook-provenance", "run-id-redaction"}.issubset(managed)
assert "utf8-output" in managed
assert "--utf8-output" in installer
assert "host/openclaw-utf8-output.json" in installer
assert "command-attempt-warning" in managed
assert "subagent-session" in managed
assert "--subagent-session" in installer
assert "host/openclaw-subagent-session.json" in installer
assert "--command-attempt-warning" in installer
assert "host/openclaw-command-attempt-warning.json" in installer
assert installer.index("_ods_pixel_refresh_plugin_registry") < installer.index("_ods_pixel_mark_ready")
assert "ods_linux_node_tools_available" in text
assert "runtime_token_file=\"/run/ods-pixel/openclaw.json\"" in text
assert "PIXEL_GATEWAY_TOKEN_FILE=$runtime_token_file" in text
assert "PIXEL_GATEWAY_PORT=$gateway_port" in text
assert "(( preview_port != gateway_port )) || return 1" in text
assert "_ods_pixel_gateway_health \"$gateway_port\"" in text
assert "\"http://${host}:${port}/health\"" in text
assert "_ods_pixel_wait_gateway 60 \"$pixel_gateway_port\"" in text
assert installer.index("_ods_pixel_existing_gateway_port_matches") < installer.index("_ods_pixel_prepare_attempt_log")
assert "_ods_pixel_installed_gateway_port" in text[text.index("_ods_pixel_install_access_service() {"):]
assert "The installed Pixel gateway unit port differs from onboarding" in text
assert "PIXEL_ODS_VERSION=$ods_version" in text
assert "PIXEL_ODS_N8N_PORT=${N8N_PORT:-5678}" in text
assert "PIXEL_ODS_WHISPER_PORT=${WHISPER_PORT:-9000}" in text
prerequisites = installer.index("\"${pixel_prerequisites[@]}\"")
control_health = installer.index("_ods_pixel_wait_http \"ODS control API\"", prerequisites)
bootstrap = installer.index("ai \"Bootstrapping the exact Pixel source", control_health)
access_service = installer.index("if ! _ods_pixel_install_access_service", bootstrap)
access_reproof = installer.index("_ods_pixel_reverify_access_after_gateway_restart \"$owner\" \"$home\" true", access_service)
assert prerequisites < control_health < bootstrap < access_service < access_reproof
assert "exact ODS prerequisite services" in installer
' "$ROOT/installers/lib/pixel-host-install.sh" "$ROOT/bin/pixel_access_bridge.py"
check python3 -c '
import pathlib,sys
phase=pathlib.Path(sys.argv[1]).read_text()
handoff = (
    "ods_pixel_activate_source_contract \\\n"
    "            \"$PIXEL_SOURCE_URL_VALUE\" \"$PIXEL_SOURCE_REF_VALUE\" \"$PIXEL_SOURCE_DIR_VALUE\""
)
assert handoff in phase
assert phase.index(handoff) < phase.index("PIXEL_SOURCE_URL=$(dotenv_quote")
legacy_upgrade = "if [[ \"$_phase06_requested_pixel_url\" == " + chr(39) + "https://github.com/Osmantic/Pixel.git" + chr(39)
assert legacy_upgrade in phase
assert "\"$_phase06_requested_pixel_ref\" == " + chr(39) + "b33730436baf5d98bf58f7d57c090318fe19f433" + chr(39) in phase
assert phase.index(legacy_upgrade) < phase.index(handoff)
assert phase.index("_env_existing=\"\"") < phase.index("_phase06_step \"rebind-pixel-source\"")
assert "\"$_phase06_pixel_owner\" \"$_phase06_pixel_home\" \"$_phase06_requested_pixel_ref\"" in phase
assert "&& -n \"${PIXEL_SOURCE_REF:-}\"" not in phase
preflight = phase.index("_phase06_step \"preflight-pixel-source\"")
checkout = phase.index("if ! _ods_pixel_source_checkout", preflight)
assert phase.index(handoff) < preflight < checkout < phase.index("PIXEL_SOURCE_URL=$(dotenv_quote")
rebind = phase.index("_phase06_step \"rebind-pixel-source\"")
restore = phase.index("_ods_pixel_restore_transition_source", rebind)
retire = phase.index("ods_pixel_uninstall_managed", restore)
assert rebind < restore < retire < preflight
assert "Pixel source is unavailable. Verify the bundled source" in phase
assert "_phase06_requested_pixel_ref=\"$(_env_get_explicit_first PIXEL_SOURCE_REF \"$ODS_PIXEL_BUNDLED_REF\")\"" in phase
assert "PIXEL_SOURCE_REF_VALUE=\"$_phase06_requested_pixel_ref\"" in phase
assert "PIXEL_GATEWAY_PORT_VALUE=\"$(_env_get_explicit_first PIXEL_GATEWAY_PORT \"18789\")\"" in phase
assert "PIXEL_PREVIEW_PORT_VALUE=\"$(_env_get_explicit_first PIXEL_PREVIEW_PORT \"9437\")\"" in phase
assert "export PIXEL_GATEWAY_PORT=\"$PIXEL_GATEWAY_PORT_VALUE\"" in phase
assert "export PIXEL_PREVIEW_PORT=\"$PIXEL_PREVIEW_PORT_VALUE\"" in phase
assert "PIXEL_GATEWAY_PORT=$(dotenv_value \"${PIXEL_GATEWAY_PORT_VALUE}\")" in phase
assert "PIXEL_PREVIEW_PORT=$(dotenv_value \"${PIXEL_PREVIEW_PORT_VALUE}\")" in phase
assert "PIXEL_MODEL_RELAY_PORT=$(dotenv_value \"${PIXEL_MODEL_RELAY_PORT_VALUE}\")" in phase
assert "PIXEL_GATEWAY_PORT and PIXEL_PREVIEW_PORT must be different" in phase
' "$ROOT/installers/phases/06-directories.sh"
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
assert "ProtectHome=true" in text
assert "RestrictNamespaces=true" in text
assert "RuntimeDirectoryPreserve=yes" in text
assert "Restart=on-failure" in text
assert "RestartForceExitStatus=SIGHUP" in text
assert "BindReadOnlyPaths=__PIXEL_GATEWAY_TOKEN_SOURCE__:__PIXEL_GATEWAY_TOKEN_FILE__" in text
' "$ROOT/extensions/services/pixel-agent/host/pixel-ingress.service"
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
assert "ProtectHome=tmpfs" in text
assert "BindReadOnlyPaths=\"__ODS_INSTALL_DIR__\"" in text
assert "IPAddressDeny=any" in text and "IPAddressAllow=localhost" in text
assert "RestrictAddressFamilies=AF_UNIX AF_INET" in text
' "$ROOT/extensions/services/pixel-agent/host/pixel-extension-manager.service"
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
assert "User=root" in text
assert "RestrictAddressFamilies=AF_UNIX" in text
assert "CapabilityBoundingSet=CAP_CHOWN CAP_DAC_OVERRIDE" in text
assert "BindPaths=\"__PIXEL_WORKSPACE__\"" in text
assert "ReadOnlyPaths=/var/lib/pixel-ops-broker/results /var/lib/pixel-ops-broker/artifacts" in text
assert "IPAddressAllow" not in text
' "$ROOT/extensions/services/pixel-agent/host/pixel-artifact-promoter.service"
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
assert "User=__PIXEL_SERVICE_USER__" in text
assert "Group=__PIXEL_SERVICE_USER__" not in text
assert "BindReadOnlyPaths=\"__PIXEL_WORKSPACE__\"" in text
assert "IPAddressDeny=any" in text and "IPAddressAllow=localhost" in text
assert "RestrictAddressFamilies=AF_UNIX AF_INET" in text
assert "CapabilityBoundingSet=" in text
' "$ROOT/extensions/services/pixel-agent/host/pixel-workspace-preview.service"
check python3 -c '
import pathlib,sys
text=pathlib.Path(sys.argv[1]).read_text()
reconcile=text.index("if ! reconcile_ods_managed_pixel_model")
discard=text.index("discard_active_model_config_snapshot", reconcile)
cleanup=text.index("# ── Phase 5b: Remove bootstrap model", reconcile)
assert reconcile < discard < cleanup
' "$ROOT/scripts/bootstrap-upgrade.sh"

# Behavior regression: the reuse_active branch must stay unchanged without
# FORCE and must run the ordered sandbox recovery only when FORCE=true. The
# production branch is extracted into a fixture (not the two branches copied)
# so these tests exercise the real installer code paths and diagnostics.
installer_source="$ROOT/installers/lib/pixel-host-install.sh"
reuse_fixture="$TEST_ROOT/reuse-active-fixture.sh"
python3 - "$installer_source" "$reuse_fixture" <<'PY'
import pathlib, sys
source = pathlib.Path(sys.argv[1]).read_text()
start = source.index('if [[ "$reuse_active" == true ]]; then')
stop = source.index('\n    else\n', source.index('ai "The exact ODS-managed Pixel contract is already active'))
pathlib.Path(sys.argv[2]).write_text(
    '#!/usr/bin/env bash\nset -euo pipefail\n\n# Extracted verbatim from pixel-host-install.sh.\n'
    + source[start:stop]
    + '\nfi\n'
)
PY

reuse_log="$TEST_ROOT/reuse-actions.log"
reuse_verify_log="$TEST_ROOT/reuse-verify.log"
run_reuse_auth_branch() {
    local force_value="$1"
    ( set -euo pipefail
        owner=test-owner
        home="/home/$owner"
        pixel_root="$TEST_ROOT/pixel-root"
        pixel_log="$TEST_ROOT/pixel.log"
        operations_policy="$TEST_ROOT/ops-policy"
        answers="$TEST_ROOT/answers.json"
        pixel_gateway_port=18789
        openclaw_bin="$TEST_ROOT/openclaw"
        FORCE="$force_value"
        reuse_active=true
        : >"$reuse_log"
        : >"$reuse_verify_log"
        ai() { :; }
        ai_bad() { :; }
        journal() { printf '%s\n' "$*" >>"$reuse_log"; }
        # This extracted branch exercises ordering; the transaction-aware
        # verifier has separate tests. Never invoke its host probes here.
        _ods_pixel_verify_current_runtime() { ods_pixel_run_as_owner "$1" "$2" "$3/pixel" verify; }
        ods_pixel_run_as_owner() {
            local source_owner="$1" target_home="$2"; shift 2
            if [[ "${2:-}" == ops-broker ]]; then
                journal run-as-owner "$source_owner" ops-broker
                return 0
            fi
            if [[ "${2:-}" == verify ]]; then
                journal run-as-owner "$source_owner" verify
                return 0
            fi
            journal run-as-owner "$source_owner" "${2:-}"
            return 0
        }
        _ods_pixel_harden_operations_state_profiles() { journal harden operations; return 0; }
        _ods_pixel_verify_operations_policy_custody() { journal verify-custody "$1" "$2"; return 0; }
        _ods_pixel_recreate_agent_sandbox() {
            journal recreate-sandbox "$1" "$2"
            return 0
        }
        _ods_pixel_wait_gateway() { journal wait-gateway "$1" "$2"; return 0; }
        ods_pixel_reconcile_promoted_model() { journal reconcile-model "$1" "$2"; return 0; }
        ods_sudo() {
            journal ods-sudo "$*"
            return 0
        }
        source "$reuse_fixture"
    ) >>"$reuse_verify_log" 2>&1
}

if run_reuse_auth_branch false; then
    if ! grep -qE 'systemctl|recreate-sandbox|wait-gateway' "$reuse_log" \
        && grep -q 'run-as-owner .* verify' "$reuse_log"; then
        pass "reuse_active without FORCE verifies without sandbox recovery"
    else
        fail "reuse_active without FORCE ran recovery or skipped verify"
    fi
else
    fail "reuse_active without FORCE rejected a valid exact contract"
fi

if run_reuse_auth_branch true; then
    if grep -qx 'ods-sudo systemctl stop openclaw-gateway.service' "$reuse_log" \
        && grep -qx 'recreate-sandbox test-owner /home/test-owner' "$reuse_log" \
        && grep -qx 'ods-sudo systemctl start openclaw-gateway.service' "$reuse_log" \
        && grep -qx 'wait-gateway 60 18789' "$reuse_log" \
        && grep -qx 'run-as-owner test-owner verify' "$reuse_log"; then
        stop_line="$(grep -n 'ods-sudo systemctl stop openclaw-gateway.service' "$reuse_log" | head -1 | cut -d: -f1)"
        retire_line="$(grep -n 'recreate-sandbox' "$reuse_log" | head -1 | cut -d: -f1)"
        start_line="$(grep -n 'ods-sudo systemctl start openclaw-gateway.service' "$reuse_log" | head -1 | cut -d: -f1)"
        health_line="$(grep -n 'wait-gateway' "$reuse_log" | head -1 | cut -d: -f1)"
        verify_line="$(grep -n 'run-as-owner test-owner verify' "$reuse_log" | head -1 | cut -d: -f1)"
        if (( stop_line < retire_line && retire_line < start_line \
            && start_line < health_line && health_line < verify_line )); then
            pass "reuse_active with FORCE runs ordered stop/retire/start/wait/verify"
        else
            fail "reuse_active FORCE recovery steps ran out of order"
        fi
    else
        fail "reuse_active with FORCE did not run the full recovery sequence"
    fi
else
    fail "reuse_active with FORCE failed an otherwise healthy recovery"
fi

inject_reuse_action_failure() {
    local action="$1" force_value="${2:-true}"
    ( set -euo pipefail
        owner=test-owner
        home="/home/$owner"
        pixel_root="$TEST_ROOT/pixel-root"
        pixel_log="$TEST_ROOT/pixel.log"
        operations_policy="$TEST_ROOT/ops-policy"
        answers="$TEST_ROOT/answers.json"
        pixel_gateway_port=18789
        openclaw_bin="$TEST_ROOT/openclaw"
        FORCE="$force_value"
        reuse_active=true
        last_bad=""
        : >"$reuse_log"
        : >"$reuse_verify_log"
        ai() { :; }
        ai_bad() { last_bad="$1"; }
        journal() { printf '%s\n' "$*" >>"$reuse_log"; }
        _ods_pixel_verify_current_runtime() { ods_pixel_run_as_owner "$1" "$2" "$3/pixel" verify; }
        ods_pixel_run_as_owner() {
            shift 2
            if [[ "${2:-}" == ops-broker ]]; then return 0; fi
            if [[ "${2:-}" == verify ]]; then
                journal run-as-owner verify
                [[ "$action" == verify ]] && return 1
                return 0
            fi
            return 0
        }
        _ods_pixel_harden_operations_state_profiles() { return 0; }
        _ods_pixel_verify_operations_policy_custody() { return 0; }
        _ods_pixel_recreate_agent_sandbox() {
            journal recreate-sandbox "$1" "$2"
            [[ "$action" == retire ]] && return 1
            return 0
        }
        _ods_pixel_wait_gateway() { journal wait-gateway; [[ "$action" == wait ]] && return 1; return 0; }
        ods_sudo() {
            journal ods-sudo "$*"
            if [[ "$action" == stop && "$*" == "systemctl stop"* ]]; then return 1; fi
            if [[ "$action" == start && "$*" == "systemctl start"* ]]; then return 1; fi
            return 0
        }
        local branch_status=0
        source "$reuse_fixture" || branch_status=$?
        printf 'RESULT bad=%s\n' "$last_bad"
        return "$branch_status"
    ) >>"$reuse_verify_log" 2>&1
}

if ! inject_reuse_action_failure stop; then
    if grep -q 'could not enter maintenance mode' "$reuse_verify_log" \
        && ! grep -q 'recreate-sandbox' "$reuse_log"; then
        pass "reuse_active FORCE fails closed when the gateway cannot stop"
    else
        fail "reuse_active FORCE stop failure did not fail closed before retire"
    fi
else
    fail "reuse_active FORCE stop failure was not propagated"
fi

if ! inject_reuse_action_failure retire; then
    if grep -q 'could not retire its stale agent sandbox' "$reuse_verify_log" \
        && grep -q 'ods-sudo systemctl start' "$reuse_log" \
        && ! grep -q 'wait-gateway' "$reuse_log"; then
        pass "reuse_active FORCE restores the gateway on sandbox cleanup failure"
    else
        fail "reuse_active FORCE did not restore the gateway after cleanup failure"
    fi
else
    fail "reuse_active FORCE sandbox cleanup failure was not propagated"
fi

if ! inject_reuse_action_failure start; then
    if grep -q 'could not restart after forced sandbox recovery' "$reuse_verify_log" \
        && ! grep -q 'wait-gateway' "$reuse_log"; then
        pass "reuse_active FORCE fails closed when the gateway cannot restart"
    else
        fail "reuse_active FORCE restart failure did not fail closed before wait"
    fi
else
    fail "reuse_active FORCE restart failure was not propagated"
fi

if ! inject_reuse_action_failure wait; then
    if grep -q 'did not become healthy after forced sandbox recovery' "$reuse_verify_log" \
        && ! grep -q 'run-as-owner verify' "$reuse_log"; then
        pass "reuse_active FORCE fails closed when the gateway does not become healthy"
    else
        fail "reuse_active FORCE health failure did not fail closed before verify"
    fi
else
    fail "reuse_active FORCE health failure was not propagated"
fi

if ! inject_reuse_action_failure verify; then
    if grep -q 'failed exact-source verification' "$reuse_verify_log" \
        && grep -q 'run-as-owner verify' "$reuse_log"; then
        pass "reuse_active FORCE still fails closed on verifier failure"
    else
        fail "reuse_active FORCE verifier failure did not fail closed"
    fi
else
    fail "reuse_active FORCE verifier failure was not propagated"
fi

if ! inject_reuse_action_failure verify false; then
    if grep -q 'failed exact-source verification' "$reuse_verify_log" \
        && ! grep -qE 'systemctl|recreate-sandbox|wait-gateway' "$reuse_log"; then
        pass "non-force exact reuse still rejects verifier failure without recovery"
    else
        fail "non-force reuse bypassed verification or ran recovery"
    fi
else
    fail "non-force reuse swallowed verifier failure"
fi

check python3 "$ROOT/tests/test_pixel_access_program_modes.py"

printf '\nResults: %d passed, %d failed\n' "$PASS" "$FAIL"
[[ "$FAIL" -eq 0 ]]