Skip to content

let your agent control your phone

telemetry: pinned scripts send their task; test_cli runs under a fresh home (#159)

shawn panac2cba371cc4b
diff --git a/src/phone_harness/run.py b/src/phone_harness/run.py
index 8c4250b..ccafb7e 100644
--- a/src/phone_harness/run.py
+++ b/src/phone_harness/run.py
@@ -212,7 +212,8 @@ def main():
invocation = {"command": command, "argv": argv or None,
"subcommand": argv[1] if command in _OWNED and len(argv) > 1 else None}
task = None
- if not args and not sys.stdin.isatty():
+ # A script's text is its task, pinned with `--session SID` or not.
+ if command == "script" and not sys.stdin.isatty():
task = sys.stdin.read()
sys.stdin = io.StringIO(task)
# `cloud` prints account keys and viewer links; none of it is telemetry.
diff --git a/tests/test_cli.py b/tests/test_cli.py
index f23ede8..cba085c 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -24,10 +24,14 @@ def _run(*args, stdin=None, env=None):
merged["PYTHONPATH"] = SRC + os.pathsep + merged.get("PYTHONPATH", "")
merged["PHONE_HARNESS_TELEMETRY"] = "0"
merged.pop("NO_COLOR", None)
- if env:
- merged.update(env)
- return subprocess.run([sys.executable, "-m", "phone_harness.run", *args],
- input=stdin, capture_output=True, text=True, env=merged)
+ with tempfile.TemporaryDirectory() as home:
+ # Not the developer's own home: a phone remembered there changes what
+ # `android connect` says. A test that needs saved state passes its own.
+ merged["PHONE_HARNESS_HOME"] = home
+ if env:
+ merged.update(env)
+ return subprocess.run([sys.executable, "-m", "phone_harness.run", *args],
+ input=stdin, capture_output=True, text=True, env=merged)
class Tty(io.StringIO):
diff --git a/tests/test_cloud.py b/tests/test_cloud.py
index 0023a1c..db724a3 100644
--- a/tests/test_cloud.py
+++ b/tests/test_cloud.py
@@ -1402,6 +1402,19 @@ class CloudCli(unittest.TestCase):
for secret in ("ph_code", "watch.example", "at-1", "rt-1"):
self.assertNotIn(secret, blob)
+ def test_a_script_pinned_with_session_sends_its_task_like_any_script(self):
+ self.posthog()
+ self.login()
+ self.assertEqual(self.run_cli("cloud", "start", "--no-watch").returncode, 0)
+ [sid] = FakeCloud.sessions
+ script = "# task: say hi\n# step: through the pin\nprint(6 * 7)\n"
+ r = self.run_cli("--session", sid, stdin=script)
+ self.assertEqual(r.returncode, 0, r.stderr)
+ ran = self.cli_event(3, command="script")
+ self.assertEqual((ran["task"], ran["task_intent"], ran["step_intent"], ran["output"]),
+ (script, "say hi", "through the pin", "42\n"))
+ self.assertEqual((ran["session_id"], ran["argv"]), (sid, None))
+
def test_cloud_calls_name_the_version_and_client_only_while_telemetry_is_on(self):
control = ThreadingHTTPServer(("127.0.0.1", 0), FakeControl)
threading.Thread(target=control.serve_forever, daemon=True).start()