Skip to content

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.

fix(install): close parent log fd in start_detached_agent (#1576)

Abhay Singh816cb85fa8ee
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 94d86d103..62840aeca 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug Fixes
+* **install:** stop leaking a file descriptor on every `headroom install start`. `start_detached_agent()` opened the agent log file and handed it to `subprocess.Popen` but never closed the parent's copy, so each call leaked one fd (and pinned the log file open against rotation). The parent now closes its copy in a `try/finally` once the child has inherited it — the close also runs if `Popen` raises ([#1554](https://github.com/headroomlabs-ai/headroom/issues/1554)).
* **proxy:** include the system prompt, tools, and the response-shaping request fields in the SemanticCache key. `_compute_key` hashed only `{model, messages}`, so two non-streaming requests with identical messages but a different top-level `system` prompt, tool set, sampling config, or output-shaping field collided on one key and the second caller was served the first's cached response — generated under different request semantics, in the default config (`cache_enabled` defaults on). The key now folds the request fields that shape generation — `temperature`/`top_p`/`top_k`/`max_tokens`/`stop`, plus OpenAI `tool_choice`/`response_format`/`parallel_tool_calls`/`seed`/`presence_penalty`/`frequency_penalty`/`logit_bias`/`n`/`logprobs`/`top_logprobs`/`reasoning_effort`/`verbosity`/`modalities` and Anthropic `thinking`/`tool_choice`/`output_config` — canonicalizing `system`/`tools` so a moved `cache_control` breakpoint does not fragment it, and the handlers snapshot the fields once at the cache read and reuse them at write so a body mutated by the pipeline cannot diverge the key. Non-streaming path only.
* **learn (verbosity):** `--verbosity --apply --all` now aggregates the savings baseline across every project instead of overwriting it per project (last-project-wins), which previously left the output shaper with a tiny, unrepresentative baseline. The applied verbosity level comes from the project with the most samples ([#1288](https://github.com/headroomlabs-ai/headroom/pull/1288)).
* **proxy/anthropic:** restore token-mode compression on continued Claude Code turns with a frozen prefix and deferred CCR tool injection. Token mode now runs request-side compression even when the client did not pre-register `headroom_retrieve`, relying on the existing marker-triggered injection override to keep emitted CCR markers redeemable ([#1487](https://github.com/headroomlabs-ai/headroom/issues/1487)).
diff --git a/headroom/install/runtime.py b/headroom/install/runtime.py
index 02a979fea..d6fd560a5 100644
--- a/headroom/install/runtime.py
+++ b/headroom/install/runtime.py
@@ -275,7 +275,15 @@ def start_detached_agent(profile: str) -> subprocess.Popen[str]:
)
else:
kwargs["start_new_session"] = True
- return subprocess.Popen(command, **kwargs)
+ try:
+ proc = subprocess.Popen(command, **kwargs)
+ finally:
+ # The child has inherited the log file descriptor, so the parent's
+ # copy is dead weight. Closing it (even when Popen raises) avoids
+ # leaking one fd per `headroom install start` and lets the log file
+ # be rotated. Wrapped in try/finally so a Popen failure can't leak.
+ log_file.close()
+ return proc
def start_persistent_docker(manifest: DeploymentManifest) -> None:
diff --git a/tests/test_install/test_runtime.py b/tests/test_install/test_runtime.py
index d0c751711..c9f144fc4 100644
--- a/tests/test_install/test_runtime.py
+++ b/tests/test_install/test_runtime.py
@@ -7,6 +7,8 @@ import sys
import types
from pathlib import Path
+import pytest
+
from headroom.install.models import DeploymentManifest, InstallPreset
from headroom.install.runtime import (
_clear_pid,
@@ -361,6 +363,56 @@ def test_run_foreground_and_detached_helpers(monkeypatch, tmp_path: Path) -> Non
assert start_detached_agent("demo") is fake_proc_posix
+def test_start_detached_agent_closes_parent_log_fd(monkeypatch, tmp_path: Path) -> None:
+ """The parent must close its copy of the log file after Popen.
+
+ The child inherits the descriptor, so leaving the parent's copy open
+ leaks one fd per call and pins the log file open against rotation.
+ """
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setattr("headroom.install.runtime.resolve_headroom_command", lambda: ["headroom"])
+ monkeypatch.setattr("headroom.install.runtime.sys.platform", "linux")
+
+ captured: dict[str, object] = {}
+
+ class FakeProc:
+ pid = 999
+
+ def fake_popen(command: list[str], **kwargs):
+ captured["stdout"] = kwargs["stdout"]
+ captured["stderr"] = kwargs["stderr"]
+ return FakeProc()
+
+ monkeypatch.setattr("headroom.install.runtime.subprocess.Popen", fake_popen)
+
+ start_detached_agent("demo")
+
+ log_handle = captured["stdout"]
+ # Same handle is passed to both streams, and the parent closed it.
+ assert captured["stderr"] is log_handle
+ assert log_handle.closed is True
+
+
+def test_start_detached_agent_closes_log_fd_when_popen_raises(monkeypatch, tmp_path: Path) -> None:
+ """A Popen failure must not leak the just-opened log file handle."""
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setattr("headroom.install.runtime.resolve_headroom_command", lambda: ["headroom"])
+ monkeypatch.setattr("headroom.install.runtime.sys.platform", "linux")
+
+ captured: dict[str, object] = {}
+
+ def boom(command: list[str], **kwargs):
+ captured["stdout"] = kwargs["stdout"]
+ raise OSError("spawn failed")
+
+ monkeypatch.setattr("headroom.install.runtime.subprocess.Popen", boom)
+
+ with pytest.raises(OSError, match="spawn failed"):
+ start_detached_agent("demo")
+
+ assert captured["stdout"].closed is True
+
+
def test_start_stop_wait_and_runtime_status_branches(monkeypatch, tmp_path: Path) -> None:
calls: list[list[str]] = []
monkeypatch.setattr(