hermes-agent/tests/tui_gateway/test_session_cwd_profile_key.py
teknium1 124b5c7d45 fix(review): key cwd/override records by the session's own profile home; keep /p/<launch>/ ids stable
Independent-review minors on #126157 (#123989 class):

- session.create is a plain @method, so tui_gateway/session_workdir.py::_register_session_cwd
  wrote the cwd record under the RAW session key while the scoped turn read
  profile:<p>:<key> and missed it until the first `cd`. The writer now binds the
  session's own profile_home around register_task_env_overrides.
- tools/terminal_tool.py::_task_env_overrides stayed raw-keyed, so two profiles
  registering the same task id (docker_image / cwd) still collided. Writer, clear
  and both readers (_has_isolation_overrides, resolve_task_overrides) now use
  _qualify_task_key; the isolation-override branch of _resolve_container_task_id
  returns the qualified key so the env cache and the override record agree.
- gateway/platforms/api_server.py::_derive_chat_session_id prefixed the seed for
  the NAMED LAUNCH profile addressed via /p/<launch>/, so prefixed and
  un-prefixed requests on one profile derived different ids. The prefix is now
  skipped when the routed name is the process's launch profile
  (_names_launch_profile vs get_routing_process_hermes_home()).

Tests (red on 3720519199, green here):
  tests/tui_gateway/test_session_cwd_profile_key.py::test_secondary_profile_session_cwd_is_found_inside_its_scope
  tests/gateway/test_api_server.py::TestDeriveChatSessionId::test_launch_profile_prefix_keeps_the_unprefixed_id
2026-09-28 04:34:30 -07:00

39 lines
1.8 KiB
Python

"""A secondary profile's session.create cwd record is readable inside that profile's turn scope.
``session.create`` is a plain ``@method`` (no profile scope bound), while the turn that reads the
record runs under the session's profile home. Since the terminal keys its cwd/override records by
the routed home (#123989), the writer must bind the same home or the record lands under the raw
key and ``get_session_cwd`` misses until the first ``cd``.
"""
from __future__ import annotations
import tools.terminal_tool as terminal_tool
import tui_gateway.server as server
def test_secondary_profile_session_cwd_is_found_inside_its_scope(monkeypatch, tmp_path):
from agent import secret_scope
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
monkeypatch.setattr(terminal_tool, "_task_env_overrides", {})
monkeypatch.setattr(terminal_tool, "_session_cwd", {})
monkeypatch.setattr(server, "_effective_terminal_backend", lambda: "local")
home = tmp_path / "profiles" / "research"
home.mkdir(parents=True)
workspace = tmp_path / "ws"
workspace.mkdir()
session = {"session_key": "sess-b", "cwd": str(workspace), "explicit_cwd": True,
"source": "desktop", "profile_home": str(home)}
secret_scope.set_multiplex_active(True)
try:
server._register_session_cwd(session) # session.create path: no scope bound by the caller
token = set_hermes_home_override(str(home))
try:
assert terminal_tool.get_session_cwd("sess-b") == str(workspace)
assert terminal_tool.resolve_task_overrides("sess-b")["cwd"] == str(workspace)
finally:
reset_hermes_home_override(token)
assert terminal_tool.get_session_cwd("sess-b") is None # not leaked onto the launch profile's key
finally:
secret_scope.set_multiplex_active(False)