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
This commit is contained in:
cristianchiriac authored and chengyongru committed 2026-10-16 00:17:47 +08:00
1 parent 12e869d047
commit a148eabc9f
2 files changed
+29 -4

No files matched your search

+3 -3
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import hashlib import hashlib
import os import os
import sys
from pathlib import Path from pathlib import Path
from typing import Final from typing import Final
@@ -11,9 +12,8 @@ _ROLES: Final = {"agent", "gateway", "webui"}
def _set_process_title(title: str) -> None: def _set_process_title(title: str) -> None:
# Process titles are short; do not trade Linux /proc environment visibility for if sys.platform == "linux":
# extra title storage. setproctitle reads this switch when it is imported. os.environ.setdefault("SPT_NOENV", "1")
os.environ.setdefault("SPT_NOENV", "1")
from setproctitle import setproctitle from setproctitle import setproctitle
setproctitle(title) setproctitle(title)
+26 -1
View File
@@ -1,13 +1,19 @@
from __future__ import annotations from __future__ import annotations
import os import os
import sys
import types
from pathlib import Path from pathlib import Path
import pytest import pytest
from typer.testing import CliRunner from typer.testing import CliRunner
from nanobot.cli.commands import app 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( @pytest.mark.parametrize(
@@ -46,6 +52,25 @@ def test_cli_process_identity_keeps_windows_launcher_name(
assert titles == [] 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( def test_legacy_console_entrypoint_still_sets_subcommand_identity(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None: