Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(toolbox): run the toolbox from inside the flox environment (#121506)

Felipe Rosa de Almeida2601f627fd02
diff --git a/tools/infra-scripts/clitools/test_toolbox.py b/tools/infra-scripts/clitools/test_toolbox.py
index 3789c6e73261..af752bf864aa 100644
--- a/tools/infra-scripts/clitools/test_toolbox.py
+++ b/tools/infra-scripts/clitools/test_toolbox.py
@@ -46,6 +46,11 @@ class TestToolbox(unittest.TestCase):
ensure_patcher = patch.object(toolbox_script, "ensure_tailscale_connected")
self.m_ensure_tailscale = ensure_patcher.start()
self.addCleanup(ensure_patcher.stop)
+ # The real capture sends events to PostHog whenever an SDK is importable,
+ # and the SDK registers its own atexit hook that the cleanup tests count.
+ telemetry_patcher = patch.object(toolbox_script, "capture_invocation")
+ telemetry_patcher.start()
+ self.addCleanup(telemetry_patcher.stop)
def test_kubectl_cmd_without_context(self):
"""kubectl_cmd with context=None returns the same shape as a bare kubectl call."""
@@ -1355,7 +1360,6 @@ class TestToolbox(unittest.TestCase):
def _clean_env(self):
os.environ.pop("KUBE_CONTEXT", None)
os.environ.pop("KUBE_NAMESPACE", None)
- os.environ.pop("FLOX_ENV", None)
def test_main_jumphost_pool_dispatches_correct_kwargs(self):
"""--pool flags-cache-jumphost threads the jumphost app/claim labels into helpers."""
@@ -1473,7 +1477,6 @@ class TestToolbox(unittest.TestCase):
patch.dict(os.environ, env_override, clear=False),
):
os.environ.pop("KUBE_CONTEXT", None)
- os.environ.pop("FLOX_ENV", None)
if env_namespace is None:
os.environ.pop("KUBE_NAMESPACE", None)
with self.assertRaises(SystemExit):
@@ -1506,7 +1509,6 @@ class TestToolbox(unittest.TestCase):
patch.dict(os.environ, {"KUBE_CONTEXT": "posthog-dev"}, clear=False),
):
os.environ.pop("KUBE_NAMESPACE", None)
- os.environ.pop("FLOX_ENV", None)
with self.assertRaises(SystemExit):
toolbox_script.main()
@@ -1580,7 +1582,6 @@ class TestToolbox(unittest.TestCase):
patch.object(toolbox_script.sys, "argv", ["toolbox.py"]),
patch.dict(os.environ, {"KUBE_CONTEXT": "bogus"}, clear=False),
):
- os.environ.pop("FLOX_ENV", None)
with self.assertRaises(SystemExit) as ctx:
toolbox_script.main()
diff --git a/tools/infra-scripts/clitools/toolbox.py b/tools/infra-scripts/clitools/toolbox.py
index 23a44241594f..f33c3c498ccc 100755
--- a/tools/infra-scripts/clitools/toolbox.py
+++ b/tools/infra-scripts/clitools/toolbox.py
@@ -63,11 +63,6 @@ def _exit_for_signal(signum, _frame):
def main():
"""Main entry point for the toolbox command."""
try:
- # If we're in a flox environment, exit
- if "FLOX_ENV" in os.environ:
- print("⚠️ Please exit the flox environment by typing `exit` before connecting to a toolbox.") # noqa: T201
- sys.exit(0)
-
# Set up argument parser
parser = argparse.ArgumentParser(
description="Connect to a toolbox pod and manage pod claims. This script will automatically connect you to your latest claimed pod.",
diff --git a/tools/infra-scripts/clitools/toolbox/telemetry.py b/tools/infra-scripts/clitools/toolbox/telemetry.py
index ae503f1a2bee..9d9cffb03eb0 100644
--- a/tools/infra-scripts/clitools/toolbox/telemetry.py
+++ b/tools/infra-scripts/clitools/toolbox/telemetry.py
@@ -81,12 +81,17 @@ def capture_invocation(distinct_id: str, properties: dict) -> None:
try:
# Imported lazily so a missing SDK degrades to a no-op instead of breaking
# the import of this module. Install with `pip install -r requirements.txt`.
- from posthog import (
- Posthog, # type: ignore[import-not-found] # noqa: PLC0415 — optional dep, kept off the import path
- )
+ try:
+ from posthog import (
+ Posthog, # type: ignore[import-not-found] # noqa: PLC0415 — optional dep, kept off the import path
+ )
+ except ImportError:
+ # The repo's flox venv has the same SDK, published as `posthoganalytics` instead of `posthog`.
+ from posthoganalytics import Posthog # noqa: PLC0415 — optional dep, kept off the import path
resolved_distinct_id = distinct_id or "unknown"
- client = Posthog(project_api_key=api_key, host=host)
+ # shutdown() below blocks before the pod claim, and the SDK defaults (15s timeout, 3 retries) can hold it for over a minute.
+ client = Posthog(project_api_key=api_key, host=host, timeout=2, max_retries=0)
client.capture(
event=EVENT_NAME,
distinct_id=resolved_distinct_id,