From cfc872fb52a4ab0c854c19b7a623257bff95071b Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Sun, 23 Aug 2026 16:06:48 +0800 Subject: [PATCH] fix(cli): harden process identity portability --- nanobot/cli/process_identity.py | 13 ++++++++++--- tests/cli/test_process_identity.py | 9 +++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/nanobot/cli/process_identity.py b/nanobot/cli/process_identity.py index f7828a11d..266753077 100644 --- a/nanobot/cli/process_identity.py +++ b/nanobot/cli/process_identity.py @@ -7,11 +7,18 @@ import os from pathlib import Path from typing import Final -from setproctitle import setproctitle - _ROLES: Final = {"agent", "gateway", "webui"} +def _set_process_title(title: str) -> None: + # Process titles are short; do not trade Linux /proc environment visibility for + # extra title storage. setproctitle reads this switch when it is imported. + os.environ.setdefault("SPT_NOENV", "1") + from setproctitle import setproctitle + + setproctitle(title) + + def set_cli_process_identity(args: list[str]) -> None: """Name this CLI process after the nanobot role it is running.""" if os.name == "nt": @@ -19,7 +26,7 @@ def set_cli_process_identity(args: list[str]) -> None: # which packaging already generates as ``nanobot.exe``. return role = args[0] if args and args[0] in _ROLES else None - setproctitle(f"nanobot-{role}" if role else "nanobot") + _set_process_title(f"nanobot-{role}" if role else "nanobot") def named_executable(executable: str, *, name: str, directory: Path) -> str: diff --git a/tests/cli/test_process_identity.py b/tests/cli/test_process_identity.py index b539ba530..72faf2cc9 100644 --- a/tests/cli/test_process_identity.py +++ b/tests/cli/test_process_identity.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os from pathlib import Path import pytest @@ -24,7 +25,7 @@ def test_cli_process_identity_uses_product_and_role( ) -> None: titles: list[str] = [] monkeypatch.setattr("nanobot.cli.process_identity.os.name", "posix") - monkeypatch.setattr("nanobot.cli.process_identity.setproctitle", titles.append) + monkeypatch.setattr("nanobot.cli.process_identity._set_process_title", titles.append) set_cli_process_identity(args) @@ -36,7 +37,7 @@ def test_cli_process_identity_keeps_windows_launcher_name( ) -> None: titles: list[str] = [] monkeypatch.setattr("nanobot.cli.process_identity.os.name", "nt") - monkeypatch.setattr("nanobot.cli.process_identity.setproctitle", titles.append) + monkeypatch.setattr("nanobot.cli.process_identity._set_process_title", titles.append) set_cli_process_identity(["agent"]) @@ -44,12 +45,12 @@ def test_cli_process_identity_keeps_windows_launcher_name( def test_named_executable_creates_stable_role_symlink( - monkeypatch: pytest.MonkeyPatch, tmp_path: Path, ) -> None: + if os.name == "nt": + pytest.skip("POSIX symlink naming is not used on Windows") executable = tmp_path / "bun" executable.write_text("runtime", encoding="utf-8") - monkeypatch.setattr("nanobot.cli.process_identity.os.name", "posix") first = Path( named_executable(executable.as_posix(), name="nanobot-tui", directory=tmp_path / "run")