let your agent control your phone
Do not send telemetry when CI is set. (#127)
shawn pana764cd72dc53f
diff --git a/install.md b/install.mdindex db8a39b..640b437 100644--- a/install.md+++ b/install.md@@ -138,6 +138,7 @@ the iPhone is driven through the mirroring window, the Android over adb. Acloud iPhone is a third path: HTTPS ops, covered in the skill.`phone-harness config set telemetry false` turns off anonymous usage telemetry.+Runs in CI do not send telemetry.## If It Failsdiff --git a/integrations/hermes/README.md b/integrations/hermes/README.mdindex e7c12dc..2f0f1e0 100644--- a/integrations/hermes/README.md+++ b/integrations/hermes/README.md@@ -50,7 +50,7 @@ covers each.- Plugin setting `classifier_auto_run` (boolean, default false).- Or export `PHONE_HARNESS_CLASSIFIER_AUTO_RUN=1` in the environment Hermesruns in. When that variable is set, it wins over the plugin setting.-- Telemetry is off for Hermes runs unless you opt in with `phone-harness config set telemetry true`.+- Telemetry is off for Hermes runs unless you opt in with `phone-harness config set telemetry true`. Runs in CI do not send telemetry.- `PHONE_HARNESS_PLATFORM`, when you have exported it, is passed through.- The agent workspace is one folder, `agent-workspace`, in Hermes's plugindata directory. If `PH_AGENT_WORKSPACE` is already set, that path is used@@ -69,7 +69,7 @@ covers each.After 120 s the tool reports the phone as busy.4. The plugin pipes the script on stdin to the phone-harness Hermes installed,with `PH_AGENT_WORKSPACE` and `PH_CLIENT=hermes`. It does not set a platform.- Telemetry is off for Hermes runs unless you opt in with `phone-harness config set telemetry true`.+ Telemetry is off for Hermes runs unless you opt in with `phone-harness config set telemetry true`. Runs in CI do not send telemetry.5. The result is what the script printed, plus the exit code and the tail ofstderr. `screenshot()` prints the path it saved. The last absolute PNG,JPEG, or WebP path in that output that exists and was written during thediff --git a/src/phone_harness/telemetry.py b/src/phone_harness/telemetry.pyindex 876146f..d8be858 100644--- a/src/phone_harness/telemetry.py+++ b/src/phone_harness/telemetry.py@@ -4,7 +4,8 @@ One `cli_event` per CLI invocation, sent to PostHog from a detached helperprocess so the CLI never blocks on the network.Off: `phone-harness config set telemetry false`, or PHONE_HARNESS_TELEMETRY=0-for one call. The install id lives in the state dir; see config.py.+for one call. A run in CI does not send telemetry. The install id lives in+the state dir; see config.py."""from __future__ import annotations@@ -30,7 +31,21 @@ def _version() -> str:return ""+def _in_ci() -> bool:+ """GitHub Actions, GitLab, CircleCI, and others set CI=true.++ Empty, 0, and false are not CI. Anything else set on CI is.+ """+ value = os.environ.get("CI")+ if value is None:+ return False+ return value.strip().lower() not in ("", "0", "false")++def is_enabled() -> bool:+ # CI never sends. An explicit opt-in does not turn it back on.+ if _in_ci():+ return Falsereturn bool(_config.get("telemetry"))diff --git a/tests/test_cli.py b/tests/test_cli.pyindex 1f22d6c..8df3fcb 100644--- a/tests/test_cli.py+++ b/tests/test_cli.py@@ -472,6 +472,8 @@ class TelemetryDefault(unittest.TestCase):with mock.patch.dict(os.environ, {"PHONE_HARNESS_HOME": home.name}, clear=False):os.environ.pop("PHONE_HARNESS_TELEMETRY", None)+ # GitHub Actions sets CI=true. This test is the non-CI default.+ os.environ.pop("CI", None)self.assertEqual(lookup("telemetry"), (True, "default"))self.assertTrue(get("telemetry"))self.assertTrue(is_enabled())diff --git a/tests/test_hermes_plugin.py b/tests/test_hermes_plugin.pyindex 03b5f51..95fc72c 100644--- a/tests/test_hermes_plugin.py+++ b/tests/test_hermes_plugin.py@@ -463,6 +463,9 @@ class TelemetryOptIn(Base):self.saved = dict(os.environ)os.environ["PHONE_HARNESS_HOME"] = str(self.data)os.environ.pop("PHONE_HARNESS_TELEMETRY", None)+ # GitHub Actions sets CI=true. These checks are the non-CI opt-in rules,+ # including the child process that would otherwise inherit CI and not send.+ os.environ.pop("CI", None)self.addCleanup(self.restore_env)def restore_env(self):diff --git a/tests/test_telemetry.py b/tests/test_telemetry.pynew file mode 100644index 0000000..6915fee--- /dev/null+++ b/tests/test_telemetry.py@@ -0,0 +1,66 @@+"""CI runs send no telemetry. An ordinary run still does.++ python -m unittest tests.test_telemetry+"""+import os+import sys+import tempfile+import unittest+from pathlib import Path+from unittest import mock++SRC = str(Path(__file__).resolve().parents[1] / "src")+if SRC not in sys.path:+ sys.path.insert(0, SRC)+++class CiTelemetry(unittest.TestCase):+ def setUp(self):+ home = tempfile.TemporaryDirectory()+ self.addCleanup(home.cleanup)+ self._env = mock.patch.dict(+ os.environ, {"PHONE_HARNESS_HOME": home.name}, clear=False,+ )+ self._env.start()+ self.addCleanup(self._env.stop)+ os.environ.pop("PHONE_HARNESS_TELEMETRY", None)+ # The suite itself runs with CI=true. Clear it here so a case that+ # expects a send is not turned into a no-op by the runner.+ os.environ.pop("CI", None)++ def test_ci_true_does_not_send_or_write_an_install_id(self):+ from phone_harness.config import paths+ from phone_harness.telemetry import capture_cli_event, is_enabled++ os.environ["CI"] = "true"+ os.environ["PHONE_HARNESS_TELEMETRY"] = "1"+ self.assertFalse(is_enabled())+ with mock.patch("phone_harness.telemetry._send_detached") as send:+ capture_cli_event(action="completed", command="version")+ send.assert_not_called()+ self.assertFalse(paths()["telemetry"].exists())++ def test_ci_unset_still_sends(self):+ from phone_harness.config import paths+ from phone_harness.telemetry import capture_cli_event, is_enabled++ self.assertNotIn("CI", os.environ)+ self.assertTrue(is_enabled())+ with mock.patch("phone_harness.telemetry._send_detached") as send:+ capture_cli_event(action="completed", command="version")+ send.assert_called_once()+ payload = send.call_args.args[0]+ self.assertEqual(payload["event"], "cli_event")+ self.assertTrue(payload["distinct_id"])+ self.assertTrue(paths()["telemetry"].is_file())++ def test_empty_zero_and_false_are_not_ci(self):+ from phone_harness.telemetry import capture_cli_event, is_enabled++ for value in ("", "0", "false", "False"):+ os.environ["CI"] = value+ self.assertTrue(is_enabled(), value)+ with mock.patch("phone_harness.telemetry._send_detached") as send:+ capture_cli_event(action="completed", command="version")+ send.assert_called_once()+ self.assertEqual(send.call_args.args[0]["event"], "cli_event")