: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
chore(hogli): classify telemetry actor and git hook (#114582)
Raúl Negrón-Oterof2ee0dbf1654
diff --git a/.husky/pre-push b/.husky/pre-pushindex 7aed0283f6d2..95dc1c42295a 100755--- a/.husky/pre-push+++ b/.husky/pre-push@@ -95,7 +95,7 @@ fi# Advisories never block; HOGLI_PREFLIGHT_DISABLED=1 makes it a no-op; no hogli → skip.if command -v hogli >/dev/null 2>&1then- hogli ci:preflight --strict || {+ HOGLI_GIT_HOOK=pre-push hogli ci:preflight --strict || {echo "pre-push: ci:preflight found failures — run 'hogli ci:preflight --fix', resolve, and push again."exit 1}diff --git a/tools/hogli-commands/README.md b/tools/hogli-commands/README.mdindex e1251854e92b..644382759af6 100644--- a/tools/hogli-commands/README.md+++ b/tools/hogli-commands/README.md@@ -50,7 +50,7 @@ That's it. No side-effect imports, no central registration list to maintain.`config.boot_modules` in `hogli.yaml` lists modules imported once at startup. They register hooks via `hogli.hooks`:- `hogli_commands.prechecks` — declares the `migrations` precheck (used by `dev:start`).-- `hogli_commands.telemetry_props` — adds PostHog environment props (`environment`, `agent`, `is_agent`, `in_flox`, `is_worktree`, `is_posthog_dev`, `process_manager`, `has_devenv_config`, `repo_sha`, `repo_commit_date`) to every `command_started` / `command_completed` event.+- `hogli_commands.telemetry_props` — adds PostHog environment props (`environment`, `actor`, `agent`, `is_agent`, `git_hook`, `in_flox`, `is_worktree`, `is_posthog_dev`, `process_manager`, `has_devenv_config`, `repo_sha`, `repo_commit_date`) to every `command_started` / `command_completed` event.- `hogli_commands.hint_hook` — shows a contextual hint after successful commands.Add a new boot module by creating a file that calls one of the `register_*` helpers from `hogli.hooks` at import time, then list it under `config.boot_modules:`. Keep these modules import-light: the precheck handler in `prechecks.py` is the canonical example of deferring its heavy import until the handler actually fires.diff --git a/tools/hogli-commands/hogli_commands/telemetry_props.py b/tools/hogli-commands/hogli_commands/telemetry_props.pyindex f979fc39d789..05816ea02e24 100644--- a/tools/hogli-commands/hogli_commands/telemetry_props.py+++ b/tools/hogli-commands/hogli_commands/telemetry_props.py@@ -9,13 +9,14 @@ boot.from __future__ import annotationsimport os+import reimport sysimport timeimport functoolsimport subprocessimport configparserfrom pathlib import Path-from typing import Any+from typing import Any, Literalfrom hogli.hooks import register_telemetry_propertiesfrom hogli.manifest import REPO_ROOT@@ -35,14 +36,24 @@ _DEVBOX_ENV_MARKERS = ("CODER", "CODER_WORKSPACE_NAME")# Ambient markers set by agent harnesses in the shells they spawn. Ordered# most-specific first: posthog-code drives claude/codex under the hood, so its-# marker must win over theirs. Harnesses without an ambient marker (e.g.-# non-sandboxed codex) can self-declare via HOGLI_AGENT instead.+# marker must win over theirs. Harnesses without an ambient marker can+# self-declare via HOGLI_AGENT instead._AGENT_ENV_MARKERS = (("POSTHOG_CODE_VERSION", "posthog-code"),("CLAUDECODE", "claude-code"),("CODEX_SANDBOX", "codex"),+ ("CODEX_THREAD_ID", "codex"),+ ("CODEX_CI", "codex"),+ ("CURSOR_AGENT", "cursor"),+ ("GEMINI_CLI", "gemini-cli"),+ ("OPENCODE", "opencode"),)+# Cross-tool convention for a harness to name itself, see https://huggingface.co/api/agent-harnesses+_STANDARD_AGENT_ENV_VARS = ("AI_AGENT", "AGENT")++_ACTOR_ENV_VAR = "HOGLI_ACTOR"+def _declared(var: str) -> str:"""Self-declared label from *var*, normalized so a sloppy export ('Sandbox ')@@ -72,16 +83,36 @@ def _detect_environment() -> str:def _detect_agent() -> str | None:- """Name of the agent harness driving this invocation, or None for a human."""+ """Name of the agent harness driving this invocation, or None when no harness declares itself."""declared = _declared("HOGLI_AGENT")if declared:return declaredfor var, agent in _AGENT_ENV_MARKERS:if os.environ.get(var):return agent+ for var in _STANDARD_AGENT_ENV_VARS:+ declared = _declared(var)+ if declared:+ # A version suffix (`[email protected]`, `name_1-2_agent`) would give each release its own value.+ name = re.split(r"[@_]\d", declared, maxsplit=1)[0]+ # `AGENT=1` is a bare flag, not a name.+ return "unknown" if name in {"", "1", "true"} else namereturn None+def _detect_actor(agent: str | None) -> Literal["agent", "human", "unknown"]:+ """Who drives this invocation. Human needs a terminal, so no agent and no terminal is unknown."""+ if agent is not None:+ return "agent"+ # A nested hogli command has captured streams, so it inherits the outermost verdict.+ if _declared(_ACTOR_ENV_VAR) == "human":+ return "human"+ # git pipes the pushed refs into a pre-push hook's stdin, so stdin alone misses a person's push.+ if any(os.isatty(fd) for fd in (0, 1, 2)):+ return "human"+ return "unknown"++@functools.cachedef _repo_commit() -> tuple[str, str] | None:"""Short SHA and committer date of HEAD, identifying the checkout's vintage.@@ -195,8 +226,10 @@ def _posthog_telemetry_properties(command: str | None = None) -> dict[str, Any]:# swamps human usage stats.agent = _detect_agent()return {+ "actor": _detect_actor(agent),"agent": agent,"environment": _detect_environment(),+ "git_hook": _declared("HOGLI_GIT_HOOK") or None,"has_devenv_config": (REPO_ROOT / ".posthog" / ".generated" / "mprocs.yaml").exists(),"in_flox": os.environ.get("FLOX_ENV") is not None,"is_agent": agent is not None,@@ -209,3 +242,7 @@ def _posthog_telemetry_properties(command: str | None = None) -> dict[str, Any]:register_telemetry_properties(_posthog_telemetry_properties)++# Set at import, because an environment write is unsafe once the telemetry send thread runs.+if _detect_actor(_detect_agent()) == "human":+ os.environ[_ACTOR_ENV_VAR] = "human"diff --git a/tools/hogli-commands/hogli_commands/tests/test_commands.py b/tools/hogli-commands/hogli_commands/tests/test_commands.pyindex 47007a5473c8..2bdaebf791c2 100644--- a/tools/hogli-commands/hogli_commands/tests/test_commands.py+++ b/tools/hogli-commands/hogli_commands/tests/test_commands.py@@ -11,6 +11,8 @@ from unittest.mock import patchfrom hogli_commands.telemetry_props import (_AGENT_ENV_MARKERS,_DEVBOX_ENV_MARKERS,+ _STANDARD_AGENT_ENV_VARS,+ _detect_actor,_detect_agent,_detect_environment,_infer_process_manager,@@ -90,8 +92,7 @@ class TestDetectEnvironment:class TestDetectAgent:@pytest.fixture(autouse=True)def _no_ambient_agent(self, monkeypatch):- monkeypatch.delenv("HOGLI_AGENT", raising=False)- for var, _ in _AGENT_ENV_MARKERS:+ for var in ("HOGLI_AGENT", *dict(_AGENT_ENV_MARKERS), *_STANDARD_AGENT_ENV_VARS):monkeypatch.delenv(var, raising=False)@pytest.mark.parametrize(@@ -104,15 +105,23 @@ class TestDetectAgent:({"POSTHOG_CODE_VERSION": "1.2.3", "CLAUDECODE": "1"}, "posthog-code"),({"HOGLI_AGENT": " Goose "}, "goose"),({"HOGLI_AGENT": "goose", "CLAUDECODE": "1"}, "goose"),+ ({"AI_AGENT": "[email protected]"}, "cursor-cli"),+ ({"AI_AGENT": "claude-code_2-1-295_agent"}, "claude-code"),+ ({"AGENT": "1"}, "unknown"),+ ({"AGENT": "1", "OPENCODE": "1"}, "opencode"),],ids=[- "human",+ "undeclared","claude_code","codex","posthog_code","posthog_code_beats_claude","declared_normalized","declared_beats_sniffed",+ "standard_var_drops_at_version",+ "standard_var_drops_underscore_version",+ "standard_var_bare_flag",+ "marker_beats_standard_var",],)def test_detection(self, monkeypatch, env_vars, expected) -> None:@@ -121,6 +130,31 @@ class TestDetectAgent:assert _detect_agent() == expected+class TestDetectActor:+ @pytest.mark.parametrize(+ ("agent", "terminal_fds", "inherited", "expected"),+ [+ ("claude-code", {0, 1, 2}, "human", "agent"),+ (None, {1, 2}, None, "human"),+ (None, set(), None, "unknown"),+ (None, set(), "human", "human"),+ ],+ ids=[+ "agent_beats_terminal_and_inherited",+ "pre_push_hook_stdin_piped",+ "no_terminal_no_agent",+ "nested_command_inherits_human",+ ],+ )+ def test_classification(self, monkeypatch, agent, terminal_fds, inherited, expected) -> None:+ monkeypatch.setattr("hogli_commands.telemetry_props.os.isatty", lambda fd: fd in terminal_fds)+ if inherited is None:+ monkeypatch.delenv("HOGLI_ACTOR", raising=False)+ else:+ monkeypatch.setenv("HOGLI_ACTOR", inherited)+ assert _detect_actor(agent) == expected++class TestRepoCommitProperties:@pytest.fixture(autouse=True)def _fresh_cache(self):diff --git a/tools/hogli/AGENTS.md b/tools/hogli/AGENTS.mdindex 6df51666f94a..e13f53c264f7 100644--- a/tools/hogli/AGENTS.md+++ b/tools/hogli/AGENTS.md@@ -92,6 +92,6 @@ Core and consumers share the `HOGLI_` env prefix; check both lists before mintinCore-reserved (defined in `tools/hogli/src/hogli/`): `HOGLI_DEBUG`, `HOGLI_MANIFEST`, `HOGLI_SECRETS_WRAPPED`, `HOGLI_NESTED_INVOCATION`.-Consumer-owned (defined in `tools/hogli-commands/`): `HOGLI_ENVIRONMENT`, `HOGLI_AGENT`, `HOGLI_PROCESS_MANAGER` (telemetry self-declaration), `HOGLI_NO_HINTS`, `HOGLI_MPROCS_PATH`, `HOGLI_DEVBOX_CODER_URL`, `HOGLI_DEVBOX_CODER_VERSION`.+Consumer-owned (defined in `tools/hogli-commands/`): `HOGLI_ENVIRONMENT`, `HOGLI_AGENT`, `HOGLI_ACTOR`, `HOGLI_GIT_HOOK`, `HOGLI_PROCESS_MANAGER` (telemetry self-declaration), `HOGLI_NO_HINTS`, `HOGLI_MPROCS_PATH`, `HOGLI_DEVBOX_CODER_URL`, `HOGLI_DEVBOX_CODER_VERSION`.If core needs a name a consumer already uses (or vice versa), rename rather than overload — bootstraps in other repos export these and silently changing semantics breaks them.