From a148eabc9f295a6ead3c92a016fded33974023de Mon Sep 17 00:00:00 2001 From: cristianchiriac <80774326+cristianchiriac@users.noreply.github.com> Date: Thu, 15 Oct 2026 18:37:42 +0300 Subject: [PATCH] fix(cli): set SPT_NOENV only on Linux SPT_NOENV only keeps the title out of /proc/PID/environ on Linux. On macOS it confines the title to the argv area, where setproctitle drops the last byte, and its import-time getproctitle() then fails to decode a command line ending in a multibyte character, so every such CLI call crashed. Fixes #6231 --- nanobot/cli/process_identity.py | 6 +++--- tests/cli/test_process_identity.py | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/nanobot/cli/process_identity.py b/nanobot/cli/process_identity.py index 266753077..a5c9350b7 100644 --- a/nanobot/cli/process_identity.py +++ b/nanobot/cli/process_identity.py @@ -4,6 +4,7 @@ from __future__ import annotations import hashlib import os +import sys from pathlib import Path from typing import Final @@ -11,9 +12,8 @@ _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") + if sys.platform == "linux": + os.environ.setdefault("SPT_NOENV", "1") from setproctitle import setproctitle setproctitle(title) diff --git a/tests/cli/test_process_identity.py b/tests/cli/test_process_identity.py index 84eef72bc..0b5917362 100644 --- a/tests/cli/test_process_identity.py +++ b/tests/cli/test_process_identity.py @@ -1,13 +1,19 @@ from __future__ import annotations import os +import sys +import types from pathlib import Path import pytest from typer.testing import CliRunner from nanobot.cli.commands import app -from nanobot.cli.process_identity import named_executable, set_cli_process_identity +from nanobot.cli.process_identity import ( + _set_process_title, + named_executable, + set_cli_process_identity, +) @pytest.mark.parametrize( @@ -46,6 +52,25 @@ def test_cli_process_identity_keeps_windows_launcher_name( assert titles == [] +@pytest.mark.parametrize(("platform", "noenv"), [("linux", "1"), ("darwin", None)]) +def test_process_title_keeps_environment_only_on_linux( + monkeypatch: pytest.MonkeyPatch, + platform: str, + noenv: str | None, +) -> None: + titles: list[str] = [] + monkeypatch.setattr("nanobot.cli.process_identity.sys.platform", platform) + monkeypatch.delenv("SPT_NOENV", raising=False) + monkeypatch.setitem( + sys.modules, "setproctitle", types.SimpleNamespace(setproctitle=titles.append) + ) + + _set_process_title("nanobot-agent") + + assert os.environ.get("SPT_NOENV") == noenv + assert titles == ["nanobot-agent"] + + def test_legacy_console_entrypoint_still_sets_subcommand_identity( monkeypatch: pytest.MonkeyPatch, ) -> None: