: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(devbox): track devbox:open, ssh and forward (#117875)
Raúl Negrón-Otero0672cabe0dd5
diff --git a/hogli.yaml b/hogli.yamlindex 6749e46fb99b..96f9230b33b3 100644--- a/hogli.yaml+++ b/hogli.yaml@@ -172,8 +172,6 @@ devbox:devbox:ssh:click: hogli_commands.devbox.cli:devbox_sshdescription: SSH into your devbox- # exec-replaces the process (os.execvp): completed events can't fire- untracked: truedevbox:exec:click: hogli_commands.devbox.cli:devbox_remote_execdescription: Run a command inside your devbox (non-interactive)@@ -181,11 +179,9 @@ devbox:devbox:open:click: hogli_commands.devbox.cli:devbox_opendescription: Open devbox in browser, VS Code, or Cursor- untracked: truedevbox:logs:click: hogli_commands.devbox.cli:devbox_logsdescription: Tail devbox build and agent logs- # exec-replaces the process (os.execvp): completed events can't fireuntracked: truedevbox:destroy:click: hogli_commands.devbox.cli:devbox_destroy@@ -196,8 +192,6 @@ devbox:devbox:forward:click: hogli_commands.devbox.cli:devbox_forwarddescription: Forward PostHog UI to localhost- # exec-replaces the process (os.execvp): completed events can't fire- untracked: truedevbox:users:click: hogli_commands.devbox.cli:devbox_usersdescription: List Coder users (for devbox sharing)diff --git a/tools/hogli-commands/hogli_commands/devbox/cli.py b/tools/hogli-commands/hogli_commands/devbox/cli.pyindex 3059abd47107..f2c748144635 100644--- a/tools/hogli-commands/hogli_commands/devbox/cli.py+++ b/tools/hogli-commands/hogli_commands/devbox/cli.py@@ -1703,6 +1703,8 @@ def devbox_open(workspace: str | None, vscode: bool, cursor: bool, web: bool) ->))+ target = "vscode" if vscode else "cursor" if cursor else "web" if web else "browser"+ telemetry.add_command_properties(devbox_open_target=target)if vscode:click.echo(f"Opening '{name}' in VS Code...")open_vscode(name)diff --git a/tools/hogli-commands/hogli_commands/devbox/coder.py b/tools/hogli-commands/hogli_commands/devbox/coder.pyindex adb7b4307e44..dc0bb3376949 100644--- a/tools/hogli-commands/hogli_commands/devbox/coder.py+++ b/tools/hogli-commands/hogli_commands/devbox/coder.py@@ -29,6 +29,7 @@ from typing import Any, NoReturnimport clickimport requestsfrom hogli import telemetry+from hogli.cli import send_completion_before_execfrom hogli.manifest import load_manifest_MACOS_TAILSCALE_CLI = "/Applications/Tailscale.app/Contents/MacOS/Tailscale"@@ -235,12 +236,18 @@ def _run(return subprocess.run(_resolve_coder(args), capture_output=capture_output, text=True, timeout=timeout)+def _execvp(file: str, args: list[str]) -> NoReturn:+ """Replace the current process, sending hogli's command telemetry first."""+ send_completion_before_exec()+ os.execvp(file, args)++def _run_or_exit(args: list[str]) -> None:"""Replace the current process with a Coder command or exit with its status."""resolved = _resolve_coder(args)coder_path = resolved[0] if resolved else shutil.which("coder")if coder_path:- os.execvp(coder_path, resolved)+ _execvp(coder_path, resolved)sys.exit(_run(args).returncode)@@ -1651,7 +1658,7 @@ def ssh_replace(name: str) -> None:The wildcard ``Host coder.*`` block written by ``coder config-ssh`` keepsthe Coder tunnel via its ``ProxyCommand``."""- os.execvp("ssh", ["ssh", _ssh_host_alias(name)])+ _execvp("ssh", ["ssh", _ssh_host_alias(name)])def exec_replace(name: str, command: list[str]) -> None:@@ -1671,7 +1678,7 @@ def exec_replace(name: str, command: list[str]) -> None:shell preserves argument boundaries -- ssh otherwise space-joins argv,which would split a token like ``"uname -sm"`` back into two."""- os.execvp("ssh", ["ssh", _ssh_host_alias(name), shlex.join(command)])+ _execvp("ssh", ["ssh", _ssh_host_alias(name), shlex.join(command)])def coder_ssh_alias_configured(name: str) -> bool:@@ -1727,7 +1734,7 @@ def open_cursor(name: str) -> None:cursor = shutil.which("cursor")if not cursor:_fail("`cursor` CLI is not on PATH. Open Cursor and enable Shell Integration from the Command Palette.")- os.execvp(cursor, ["cursor", "--remote", f"ssh-remote+{_ssh_host_alias(name)}", "/home/coder/posthog"])+ _execvp(cursor, ["cursor", "--remote", f"ssh-remote+{_ssh_host_alias(name)}", "/home/coder/posthog"])def open_web_ide(name: str) -> None:diff --git a/tools/hogli/src/hogli/cli.py b/tools/hogli/src/hogli/cli.pyindex 2a7674a49cf7..22962f996d31 100644--- a/tools/hogli/src/hogli/cli.py+++ b/tools/hogli/src/hogli/cli.py@@ -690,6 +690,26 @@ def _fire_telemetry(ctx: click.Context, exit_code: int) -> None:pass+def send_completion_before_exec(timeout: float = 0.5) -> None:+ """Send this invocation's ``command_completed`` before ``os.exec*`` replaces the process.++ hogli sends ``command_completed`` after the command returns, and a command+ that exec-replaces itself never returns. The wait is bounded by *timeout*.+ The invocation is marked inactive afterwards, so an exec that fails and+ falls through to a normal exit does not send a second event. Never raises.+ """+ ctx = click.get_current_context(silent=True)+ if ctx is None:+ return+ root = ctx.find_root()+ _fire_telemetry(root, 0)+ root.meta["hogli.telemetry_active"] = False+ try:+ telemetry.flush(timeout)+ except Exception:+ pass++def _fire_post_command_hooks(command: str | None, exit_code: int) -> None:"""Run registered post-command hooks. Never raises."""for hook in post_command_hooks:diff --git a/tools/hogli/tests/test_telemetry.py b/tools/hogli/tests/test_telemetry.pyindex 3145d43a721f..51dba24be887 100644--- a/tools/hogli/tests/test_telemetry.py+++ b/tools/hogli/tests/test_telemetry.py@@ -11,7 +11,7 @@ from unittest.mock import patchimport clickfrom click.testing import CliRunnerfrom hogli import telemetry-from hogli.cli import _fire_telemetry, _outcome, _should_track, cli+from hogli.cli import _fire_telemetry, _outcome, _should_track, cli, send_completion_before_exec_TELEMETRY_ENV_VARS = (# Every CI marker the gate checks must be cleared, otherwise the suite@@ -281,6 +281,25 @@ class TestCommandProperties:props = mock_track.call_args.args[1]assert props["failure_cause"] == "unreachable"+ def test_completion_before_exec_sends_once(self, monkeypatch: pytest.MonkeyPatch):+ monkeypatch.setattr("hogli.cli.get_category_for_command", lambda command: "core")+ monkeypatch.setattr("hogli.cli._env_properties", lambda command: {})+ ctx = click.Context(cli)+ ctx.invoked_subcommand = "quickstart"+ ctx.meta["hogli.telemetry_active"] = True++ with patch.object(telemetry._client, "track") as mock_track, patch.object(telemetry._client, "flush"):+ with click.Context(click.Command("quickstart"), parent=ctx):+ telemetry.add_command_properties(target="cursor")+ send_completion_before_exec()+ _fire_telemetry(ctx, 1)++ assert mock_track.call_count == 1+ event, props = mock_track.call_args.args+ assert event == "command_completed"+ assert props["exit_code"] == 0+ assert props["target"] == "cursor"+def _patch_cli_manifest(monkeypatch: pytest.MonkeyPatch, command_config: dict | None) -> None:"""Point hogli.cli at a fake manifest whose every command has *command_config*.