From 2953739f1e4afea09c8e81c0cf30dc7b47a6bd80 Mon Sep 17 00:00:00 2001 From: KeegantheVeganat0r <58369898+KeegantheVeganat0r@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:55:33 +0000 Subject: [PATCH] fix(lab): target the active app package and repair build setup (#2494) * fix(lab): handle SDK licenses and cache Gradle planning * fix(lab): target the package owning the agent session * fix(lab): preserve package overrides across build handoff --- docs/lab-builds.md | 5 +++ tools/agent/dd-lab-build | 31 +++++++++++--- tools/agent/setup | 2 +- tools/tests/test_dd_lab_build.py | 69 ++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 tools/tests/test_dd_lab_build.py diff --git a/docs/lab-builds.md b/docs/lab-builds.md index 4f2ddfb5f0..177133e14d 100644 --- a/docs/lab-builds.md +++ b/docs/lab-builds.md @@ -25,6 +25,11 @@ change. Updates lists them under Lab builds: the one in use can be turned off, one built for this install can be used again, and any can be deleted. +The build targets the package owning `DD_AGENT_INBOX` when run from the Agent tab. +For a manual build of another package, set `DROIDDECK_PACKAGE`, for example +`DROIDDECK_PACKAGE=com.tencent.ig dd-lab-build`. This also selects the control provider, +Lab storage, and package to restart. + ## What a Lab build cannot change The manifest, native code (`cpp`, `jniLibs`, Rust), Gradle files, `App`, `AppLanguage`, and `LabOverlay` itself: diff --git a/tools/agent/dd-lab-build b/tools/agent/dd-lab-build index 44b62bd6b8..b29c6bc5a6 100755 --- a/tools/agent/dd-lab-build +++ b/tools/agent/dd-lab-build @@ -5,12 +5,17 @@ # compile what changed. Setup runs it once; it takes the longest. # --explain logs why each task ran and Kotlin's build report, to find out what makes a build slow. set -euo pipefail -pkg=com.droiddeck.launcher +pkg=${DROIDDECK_PACKAGE:-} +if [ -z "$pkg" ] && [[ ${DD_AGENT_INBOX:-} =~ ^/data/(user/[0-9]+|data)/([^/]+)/files/agent$ ]]; then + pkg=${BASH_REMATCH[2]} +fi +pkg=${pkg:-com.droiddeck.launcher} serial=${ADB_SERIAL:-127.0.0.1:5555} mode=build explain=() for arg in "$@"; do case $arg in + --package=*) pkg=${arg#--package=} ;; --no-restart) mode=no-restart ;; --prepare) mode=prepare ;; --explain) explain=(--info -Pkotlin.build.report.output=file) ;; @@ -31,16 +36,18 @@ progress() { # stage [permille task done total eta] fail() { progress failed 0 "$*"; echo "dd-lab-build: $*" >&2; exit 1; } trap 'progress failed 0 stopped; exit 143' INT TERM trap 'progress failed 0 "a command failed on line $LINENO"' ERR +[[ $pkg =~ ^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$ ]] || fail "invalid Android package: $pkg" +export DROIDDECK_PACKAGE=$pkg device() { adb -s "$serial" shell "$@" | tr -d '\r'; } # The app's agent loses every process it started when its turn ends, a build included, so it asks # the app to run the build in a process of its own and follows that build here. -agent=/data/user/0/$pkg/files/agent +agent=${DD_AGENT_INBOX:-/data/user/0/$pkg/files/agent} if [ -n "${DD_AGENT:-}" ] && [ -z "${DD_LAB_DETACHED:-}" ] && [ -d "$agent" ]; then trap - ERR log=$cache/dd-lab-build.log rm -f "$progress_file" - python3 -c 'import json,os,sys; print(json.dumps({"cwd": os.getcwd(), "args": sys.argv[1:]}))' "$@" > "$agent/lab-request.part" + python3 -c 'import json,os,sys; print(json.dumps({"cwd": os.getcwd(), "args": [*sys.argv[1:], "--package=" + os.environ["DROIDDECK_PACKAGE"]]}))' "$@" > "$agent/lab-request.part" mv "$agent/lab-request.part" "$agent/lab-request" echo "DroidDeck runs this build itself, so it keeps going if this turn ends. Its log follows." for _ in $(seq 1 30); do [ -f "$progress_file" ] && break; sleep 1; done @@ -102,7 +109,7 @@ export DROIDDECK_SOURCE_COMMIT=$base DROIDDECK_BUILD_LABEL="lab on $installed_la printf 'sdk.dir=%s\n' "$ANDROID_HOME" > local.properties if [ ! -d "$ANDROID_HOME/platforms/android-34" ]; then progress sdk - yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null + (set +o pipefail; yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null) "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null fi @@ -125,7 +132,19 @@ gradle=(./gradlew :app:processReleaseResources :app:mergeDexRelease --max-worker -Pandroid.aapt2FromMavenOverride=$HOME/aapt2/aapt2 -Pkotlin.compiler.execution.strategy=daemon -Pkotlin.daemon.jvmargs=-Xmx${kotlin_heap}m -Dorg.gradle.jvmargs=-Xmx${gradle_heap}m --console=plain) progress planning -mapfile -t plan < <("${gradle[@]}" --dry-run -q 2>/dev/null | awk '/^:/ {print $1}' || true) +plan_file=$cache/dd-lab-build-plan +plan_key=$( + { + printf '%s\n' "$repo" "${gradle[@]}" + git ls-files -z -- '*gradle*' | xargs -0 -r sha256sum + } | sha256sum | cut -d ' ' -f1 +) +if [ "$(head -n 1 "$plan_file" 2>/dev/null)" != "$plan_key" ]; then + plan_output=$("${gradle[@]}" --dry-run) || fail "Gradle planning failed: $plan_output" + { printf '%s\n' "$plan_key"; awk '/^:/ {print $1}' <<<"$plan_output"; } > "$plan_file.part" + mv "$plan_file.part" "$plan_file" +fi +mapfile -t plan < <(tail -n +2 "$plan_file") total=${#plan[@]} # Each task's estimate is how long it took last time; a full compile of the app takes about 15 minutes. @@ -221,6 +240,6 @@ device cmd package compile -m verify -f $pkg >/dev/null progress restarting 1000 "$name" echo "Restarting DroidDeck on the Lab build; it shows a notification when it is back" # The app's own agent ends with the app, so it is told to carry on once the app is back. -if [ -n "${DD_AGENT:-}" ]; then mkdir -p "/data/user/0/$pkg/files/agent" && : > "/data/user/0/$pkg/files/agent/resume"; fi +if [ -n "${DD_AGENT:-}" ]; then mkdir -p "$agent" && : > "$agent/resume"; fi device "nohup sh -c 'sleep 3; monkey -p $pkg -c android.intent.category.LAUNCHER 1' >/dev/null 2>&1 &" for pid in $(device pidof $pkg); do kill "$pid" 2>/dev/null || true; done diff --git a/tools/agent/setup b/tools/agent/setup index 0b1f66f6a0..5c2405d8d6 100755 --- a/tools/agent/setup +++ b/tools/agent/setup @@ -61,7 +61,7 @@ if [ ! -x "$sdk/cmdline-tools/latest/bin/sdkmanager" ]; then mv "$sdk/cmdline-tools/cmdline-tools" "$sdk/cmdline-tools/latest" fi export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-arm64 -yes | "$sdk/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null +(set +o pipefail; yes | "$sdk/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null) "$sdk/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null adb connect 127.0.0.1:5555 || true diff --git a/tools/tests/test_dd_lab_build.py b/tools/tests/test_dd_lab_build.py new file mode 100644 index 0000000000..9cf6d2a2c8 --- /dev/null +++ b/tools/tests/test_dd_lab_build.py @@ -0,0 +1,69 @@ +import json +import os +from pathlib import Path +import subprocess +import tempfile +import time +import unittest + + +SCRIPT = Path(__file__).resolve().parents[1] / 'agent/dd-lab-build' + + +class LabBuildPackageTest(unittest.TestCase): + def test_package_override_survives_app_handoff_without_its_environment(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + inbox = root / 'agent' + inbox.mkdir() + cache = root / '.cache' + env = {**os.environ, 'HOME': tmp, 'DD_AGENT': '1', + 'DD_AGENT_INBOX': str(inbox), 'DROIDDECK_PACKAGE': 'com.example.target'} + env.pop('DD_LAB_DETACHED', None) + child = subprocess.Popen(['bash', str(SCRIPT), '--prepare'], env=env, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + try: + request = inbox / 'lab-request' + deadline = time.monotonic() + 10 + while not request.exists() and child.poll() is None and time.monotonic() < deadline: + time.sleep(0.01) + self.assertTrue(request.exists()) + payload = json.loads(request.read_text()) + self.assertEqual(['--prepare', '--package=com.example.target'], payload['args']) + progress = cache / 'dd-lab-build.json' + progress.write_text('{"stage":"prepared"}') + stdout, stderr = child.communicate(timeout=10) + self.assertEqual(0, child.returncode, stdout + stderr) + finally: + if child.poll() is None: + child.kill() + child.communicate() + + bin_dir = root / 'bin' + bin_dir.mkdir() + (bin_dir / 'adb').write_text('#!/bin/sh\nexit 0\n') + (bin_dir / 'python3').write_text( + '#!/bin/sh\nprintf "%s" "$DROIDDECK_PACKAGE" > "$HOME/selected-package"\nexit 1\n') + for tool in bin_dir.iterdir(): + tool.chmod(0o755) + env.pop('DROIDDECK_PACKAGE') + env['DD_LAB_DETACHED'] = '1' + env['PATH'] = str(bin_dir) + os.pathsep + env['PATH'] + result = subprocess.run(['bash', str(SCRIPT), *payload['args']], env=env, + capture_output=True, text=True, timeout=10, cwd=SCRIPT.parents[2]) + self.assertNotEqual(0, result.returncode) + self.assertIn('could not read DroidDeck', result.stderr) + self.assertEqual('com.example.target', (root / 'selected-package').read_text()) + + def test_invalid_packages_are_rejected_before_handoff(self): + with tempfile.TemporaryDirectory() as tmp: + inbox = Path(tmp) / 'agent' + inbox.mkdir() + for package in ('bad;touch injected', '../other', '', 'com.example/other'): + with self.subTest(package=package): + env = {**os.environ, 'HOME': tmp, 'DD_AGENT': '1', 'DD_AGENT_INBOX': str(inbox)} + result = subprocess.run(['bash', str(SCRIPT), '--package=' + package], env=env, + capture_output=True, text=True, timeout=10) + self.assertNotEqual(0, result.returncode) + self.assertIn('invalid Android package', result.stderr) + self.assertFalse((inbox / 'lab-request').exists())