mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
refactor: remove write-only and test-only code in subscription and install
Remove private code that nothing reads: - subscription/tracker.py: the _PERSIST_FILE_ENV, _DEFAULT_PERSIST_DIR and _DEFAULT_PERSIST_FILE constants (the path comes from paths.subscription_state_path()), and the write-only _full_tokens counter, which kept an 8-character OAuth token prefix in memory. - install/runtime.py: _process_matches_runtime, a test-only wrapper for _process_identity(...) is True. The tests now assert on _process_identity directly. Evidence: rg over headroom, tests, crates, plugins, sdk, docs, wiki, scripts and the consumer repos finds only the definitions and the tests changed here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a9815bbb5f
commit
06377018c6
@@ -369,11 +369,6 @@ def _proxy_deployment_markers_match(cmdline: list[str], manifest: DeploymentMani
|
||||
return True
|
||||
|
||||
|
||||
def _process_matches_runtime(pid: int, manifest: DeploymentManifest) -> bool:
|
||||
"""Verify the PID's deployment identity before lifecycle operations."""
|
||||
return _process_identity(pid, manifest) is True
|
||||
|
||||
|
||||
def _process_identity(pid: int, manifest: DeploymentManifest) -> bool | None:
|
||||
"""Return whether a PID matches, or ``None`` when identity is unavailable."""
|
||||
if pid == os.getpid():
|
||||
|
||||
@@ -49,9 +49,6 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
_DEFAULT_POLL_INTERVAL_S = 300
|
||||
_DEFAULT_ACTIVE_WINDOW_S = 60
|
||||
_PERSIST_FILE_ENV = _paths.HEADROOM_SUBSCRIPTION_STATE_PATH_ENV
|
||||
_DEFAULT_PERSIST_DIR = ".headroom"
|
||||
_DEFAULT_PERSIST_FILE = "subscription_state.json"
|
||||
|
||||
# Singleton on-demand poll floor (seconds): the dashboard may request a fresh
|
||||
# poll if the cached snapshot is stale, but we cap how often we will actually
|
||||
@@ -127,7 +124,6 @@ class SubscriptionTracker(QuotaTracker):
|
||||
self._lock = threading.Lock()
|
||||
self._state = SubscriptionState()
|
||||
self._current_token: str | None = None
|
||||
self._full_tokens: dict[str, int] = {} # token_prefix -> count of requests
|
||||
|
||||
self._stop_event: asyncio.Event | None = None
|
||||
self._poll_task: asyncio.Task[None] | None = None
|
||||
@@ -200,8 +196,6 @@ class SubscriptionTracker(QuotaTracker):
|
||||
if not from_local_operator:
|
||||
return
|
||||
self._current_token = raw
|
||||
prefix = raw[:8]
|
||||
self._full_tokens[prefix] = self._full_tokens.get(prefix, 0) + 1
|
||||
|
||||
def update_contribution(
|
||||
self,
|
||||
|
||||
@@ -15,7 +15,6 @@ from headroom.install.runtime import (
|
||||
_deployment_env,
|
||||
_mount_source,
|
||||
_process_identity,
|
||||
_process_matches_runtime,
|
||||
_read_pid,
|
||||
_runtime_env,
|
||||
_write_pid,
|
||||
@@ -847,7 +846,7 @@ def test_process_identity_must_match_headroom_command(monkeypatch) -> None:
|
||||
}
|
||||
|
||||
monkeypatch.setitem(sys.modules, "psutil", types.SimpleNamespace(Process=FakeProcess))
|
||||
assert not _process_matches_runtime(4321, _python_service_manifest())
|
||||
assert _process_identity(4321, _python_service_manifest()) is False
|
||||
|
||||
class MatchingProcess(FakeProcess):
|
||||
def cmdline(self) -> list[str]:
|
||||
@@ -860,7 +859,7 @@ def test_process_identity_must_match_headroom_command(monkeypatch) -> None:
|
||||
}
|
||||
|
||||
monkeypatch.setitem(sys.modules, "psutil", types.SimpleNamespace(Process=MatchingProcess))
|
||||
assert _process_matches_runtime(4321, _python_service_manifest())
|
||||
assert _process_identity(4321, _python_service_manifest()) is True
|
||||
|
||||
|
||||
def test_process_identity_falls_back_without_psutil(monkeypatch) -> None:
|
||||
@@ -876,7 +875,7 @@ def test_process_identity_falls_back_without_psutil(monkeypatch) -> None:
|
||||
),
|
||||
)
|
||||
monkeypatch.setitem(sys.modules, "psutil", None)
|
||||
assert _process_matches_runtime(4321, manifest)
|
||||
assert _process_identity(4321, manifest) is True
|
||||
|
||||
|
||||
def test_process_identity_uses_macos_ps_fallback_without_psutil(monkeypatch) -> None:
|
||||
@@ -893,7 +892,7 @@ def test_process_identity_uses_macos_ps_fallback_without_psutil(monkeypatch) ->
|
||||
)
|
||||
|
||||
monkeypatch.setattr("headroom.install.runtime.run", lambda *a, **k: Result())
|
||||
assert _process_matches_runtime(4321, manifest)
|
||||
assert _process_identity(4321, manifest) is True
|
||||
|
||||
|
||||
def test_process_identity_uses_windows_wmi_command_line_without_psutil(monkeypatch) -> None:
|
||||
@@ -914,7 +913,7 @@ def test_process_identity_uses_windows_wmi_command_line_without_psutil(monkeypat
|
||||
lambda command, **kwargs: calls.append(command) or Result(),
|
||||
)
|
||||
|
||||
assert _process_matches_runtime(4321, manifest)
|
||||
assert _process_identity(4321, manifest) is True
|
||||
assert calls[0][:3] == ["powershell.exe", "-NoProfile", "-NonInteractive"]
|
||||
|
||||
|
||||
@@ -1010,7 +1009,7 @@ def test_runtime_status_does_not_use_ready_probe_for_failed_identity(
|
||||
|
||||
def test_process_identity_rejects_current_pid(monkeypatch) -> None:
|
||||
monkeypatch.setattr("headroom.install.runtime.os.getpid", lambda: 4321)
|
||||
assert not _process_matches_runtime(4321, _python_service_manifest())
|
||||
assert _process_identity(4321, _python_service_manifest()) is False
|
||||
|
||||
|
||||
def test_darwin_task_preserves_popen_path(monkeypatch, tmp_path: Path) -> None:
|
||||
|
||||
@@ -59,7 +59,6 @@ def test_tracker_notify_active_update_and_basic_state(monkeypatch: pytest.Monkey
|
||||
|
||||
tracker.notify_active("Bearer oauth-token-123", from_local_operator=True)
|
||||
assert tracker._current_token == "oauth-token-123"
|
||||
assert tracker._full_tokens["oauth-to"] == 1
|
||||
assert tracker.is_active() is True
|
||||
|
||||
tracker.update_contribution(
|
||||
|
||||
Reference in New Issue
Block a user