Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(init): raise the ensure hook's timeout above cold-start latency (#3438)
Sudhindra Desai9a1110997a13
diff --git a/headroom/cli/init.py b/headroom/cli/init.pyindex 8914900d8..0063d9d69 100644--- a/headroom/cli/init.py+++ b/headroom/cli/init.py@@ -63,6 +63,12 @@ _SUPPORTED_TARGETS = ("claude", "copilot", "codex", "openclaw")_LOCAL_TARGETS = {"claude", "codex"}_GLOBAL_TARGETS = {"claude", "copilot", "codex", "openclaw"}_STARTUP_READY_TIMEOUT_SECONDS = 15+# External kill timeout Claude/Copilot/Codex apply to the `headroom init hook+# ensure` command itself. Must stay above the internal wait_ready(45s) call in+# _ensure_profile_running for a cold start (measured 15.9-36.9s in #3417) --+# otherwise the host kills the hook before a first-ever proxy start can ever+# report ready, and every session is permanently cold.+_HOOK_ENSURE_TIMEOUT_SECONDS = 60_TOML_TABLE_HEADER_RE = re.compile(r"^[ \t]*(?:\[\[[^\]\r\n]+\]\]|\[[^\]\r\n]+\])[ \t]*(?:#.*)?$")_TOML_FEATURES_NAME_RE = r"(?:features|\"features\"|'features')"_TOML_CODEX_HOOKS_NAME_RE = r"(?:codex_hooks|\"codex_hooks\"|'codex_hooks')"@@ -221,7 +227,7 @@ def _ensure_claude_hooks(path: Path, profile: str, port: int) -> None:{"type": "command","command": f"{command} --marker {_CLAUDE_HOOK_MARKER}",- "timeout": 15,+ "timeout": _HOOK_ENSURE_TIMEOUT_SECONDS,}],}@@ -245,7 +251,14 @@ def _ensure_copilot_hooks(path: Path, profile: str) -> None:isinstance(entry, dict) and _COPILOT_HOOK_MARKER in str(entry.get("command", "")))]- retained.append({"type": "command", "command": command, "cwd": ".", "timeout": 15})+ retained.append(+ {+ "type": "command",+ "command": command,+ "cwd": ".",+ "timeout": _HOOK_ENSURE_TIMEOUT_SECONDS,+ }+ )hooks[event] = retainedpayload["hooks"] = hooks_write_json(path, payload)@@ -524,7 +537,13 @@ def _ensure_codex_hooks(path: Path, profile: str) -> None:retained.append({"matcher": matcher,- "hooks": [{"type": "command", "command": command, "timeout": 15}],+ "hooks": [+ {+ "type": "command",+ "command": command,+ "timeout": _HOOK_ENSURE_TIMEOUT_SECONDS,+ }+ ],})hooks[event] = retaineddiff --git a/plugins/headroom-agent-hooks/hooks/hooks.json b/plugins/headroom-agent-hooks/hooks/hooks.jsonindex bf14a3132..11b1d35ac 100644--- a/plugins/headroom-agent-hooks/hooks/hooks.json+++ b/plugins/headroom-agent-hooks/hooks/hooks.json@@ -8,7 +8,7 @@{"type": "command","command": "headroom init hook ensure",- "timeout": 15+ "timeout": 60}]}@@ -20,7 +20,7 @@{"type": "command","command": "headroom init hook ensure",- "timeout": 15+ "timeout": 60}]}diff --git a/tests/test_cli/test_init_cli.py b/tests/test_cli/test_init_cli.pyindex bfcf1aa77..d8d53b931 100644--- a/tests/test_cli/test_init_cli.py+++ b/tests/test_cli/test_init_cli.py@@ -452,6 +452,46 @@ def test_ensure_claude_hooks_rewrites_existing_entries(monkeypatch, tmp_path: Paassert session_entries[-1]["hooks"][0]["command"].endswith("--marker headroom-init-claude")+def test_ensure_claude_hooks_timeout_exceeds_cold_start_wait(monkeypatch, tmp_path: Path) -> None:+ """The external hook timeout must stay above the internal wait_ready(45s)+ call _ensure_profile_running makes after a cold start (#3417), or the host+ kills the hook before a first-ever proxy start can ever report ready."""+ init_cli, _ = _load_init_module(monkeypatch)+ settings_path = tmp_path / "settings.json"+ monkeypatch.setattr(init_cli, "_hook_command", lambda *parts: "headroom init hook ensure")++ init_cli._ensure_claude_hooks(settings_path, "init-local-demo", 9001)++ payload = json.loads(settings_path.read_text(encoding="utf-8"))+ for event in ("SessionStart", "PreToolUse"):+ timeout = payload["hooks"][event][-1]["hooks"][0]["timeout"]+ assert timeout > 45+++def test_ensure_copilot_hooks_timeout_exceeds_cold_start_wait(monkeypatch, tmp_path: Path) -> None:+ init_cli, _ = _load_init_module(monkeypatch)+ config_path = tmp_path / "copilot.json"+ monkeypatch.setattr(init_cli, "_hook_command", lambda *parts: "headroom init hook ensure")++ init_cli._ensure_copilot_hooks(config_path, "init-user")++ payload = json.loads(config_path.read_text(encoding="utf-8"))+ for event in ("SessionStart", "PreToolUse"):+ assert payload["hooks"][event][-1]["timeout"] > 45+++def test_ensure_codex_hooks_timeout_exceeds_cold_start_wait(monkeypatch, tmp_path: Path) -> None:+ init_cli, _ = _load_init_module(monkeypatch)+ path = tmp_path / "hooks.json"+ monkeypatch.setattr(init_cli, "_hook_command", lambda *parts: "headroom init hook ensure")++ init_cli._ensure_codex_hooks(path, "init-user")++ payload = json.loads(path.read_text(encoding="utf-8"))+ for event in ("SessionStart", "PreToolUse"):+ assert payload["hooks"][event][-1]["hooks"][0]["timeout"] > 45++def test_ensure_copilot_hooks_replaces_existing_marker(monkeypatch, tmp_path: Path) -> None:init_cli, _ = _load_init_module(monkeypatch)config_path = tmp_path / "copilot.json"diff --git a/tests/test_plugin_manifests.py b/tests/test_plugin_manifests.pyindex 999ceb923..e348db7ce 100644--- a/tests/test_plugin_manifests.py+++ b/tests/test_plugin_manifests.py@@ -27,6 +27,20 @@ def test_plugin_manifests_share_core_metadata() -> None:assert copilot["hooks"] == "./hooks"+def test_plugin_hooks_timeout_exceeds_cold_start_wait() -> None:+ """The plugin's `ensure` hooks are killed by the host (Claude Code, etc.)+ at their declared `timeout`, independent of anything inside the process.+ Headroom's own cold-start path can take up to wait_ready's 45s ceiling+ (headroom/cli/init.py), so a shorter external timeout makes the hook+ unwinnable by construction on every session start (#3417)."""+ hooks = _load_json("plugins/headroom-agent-hooks/hooks/hooks.json")+ assert isinstance(hooks, dict)+ for event in ("SessionStart", "PreToolUse"):+ for entry in hooks["hooks"][event]:+ for hook in entry["hooks"]:+ assert hook["timeout"] > 45++def test_marketplace_entry_points_to_plugin_root() -> None:marketplace = _load_json(".claude-plugin/marketplace.json")assert isinstance(marketplace, dict)