fix(gateway): a Telegram chat's cd survives a gateway restart

The terminal tool records where each chat's last command finished
(record_session_cwd, keyed by the chat's session key) in a process-local dict, so a
cd held across turns but not across a restart: the chat's next command ran in
terminal.cwd again (#41128).

The turn now writes that record onto the chat's routing entry metadata
(terminal_cwd) when it changed, and the first turn that finds no record seeds it
back, only if the directory still exists. Local backend only: a remote or container
path cannot be checked from the host, and a stale one would fail every command
(cd -- <cwd> || exit 126) instead of falling back. /new starts a fresh entry, so a
new conversation starts in terminal.cwd as before. New sibling
gateway/session_terminal_cwd.py; run_turn_runner.run_sync calls it around the turn.

Live E2E (tests/gateway/test_session_terminal_cwd_restart.py): real daemon,
loopback Bot API, model stub driving the real terminal tool. cd sub, SIGTERM,
restart, pwd -> work/sub (base: work). Control: sub removed before the restart ->
work. Red on base for the first case.
This commit is contained in:
Teknium
2026-11-11 06:05:21 -08:00
parent 93a7181dae
commit eec7e828c0
3 changed files with 211 additions and 1 deletions

View File

@@ -1294,7 +1294,12 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
else:
scope.enter_context(api_policy_scope())
bind_turn_workspace(policy, self._ctx.session_id)
result = self._run_sync_scoped()
from gateway.session_terminal_cwd import persist_turn_cwd, restore_turn_cwd
restore_turn_cwd(self._runner, self._ctx.session_key, policy)
try:
result = self._run_sync_scoped()
finally:
persist_turn_cwd(self._runner, self._ctx.session_key, policy)
from gateway.session_results import execution_result
captured = execution_result.get()
if captured is not None:

View File

@@ -0,0 +1,68 @@
"""A chat's terminal working directory survives a gateway restart (#41128).
The terminal tool records where each command finished (``tools.terminal_tool.record_session_cwd``,
keyed by the chat's session key) in a process-local dict, so a ``cd`` made in one turn holds for the
next. A restart empties that dict and the chat's next command runs in ``terminal.cwd`` again. The
turn now writes the recorded directory onto the chat's routing entry when it changed, and a turn
that finds no record (the first after a restart) seeds it back from there.
Local backend only: a host path can be checked before it is trusted. A remote or container
directory cannot be vouched for from here, and every session-env command starts with
``cd -- <cwd> || exit 126``, so a stale one would break the chat's commands instead of falling back.
A ``/new`` starts a fresh entry, so a new conversation after a restart starts in ``terminal.cwd``.
"""
from __future__ import annotations
import json
import logging
import os
logger = logging.getLogger(__name__)
_METADATA_KEY = "terminal_cwd"
def _local_backend(policy) -> bool:
"""The backend this turn's terminal runs on: a local session's frozen policy, else (a messaging
turn, which has none) the profile's live terminal config the tool itself reads."""
if policy is not None:
backend = json.loads(policy.terminal_json).get("TERMINAL_ENV")
else:
from tools.terminal_tool import _get_env_config
backend = _get_env_config().get("env_type")
return str(backend or "local").strip().lower() == "local"
def _entry_metadata(runner, session_key):
"""The chat's routing-entry metadata, or None when this runner keeps no entry for it (a one-shot
or API runner without a routing store; the turn runner reads ``_entries`` the same way)."""
entry = getattr(getattr(runner, "session_store", None), "_entries", {}).get(session_key)
metadata = getattr(entry, "metadata", None)
return metadata if isinstance(metadata, dict) else None
def restore_turn_cwd(runner, session_key: str | None, policy) -> None:
"""Seed the chat's cwd record from its routing entry when this process has none yet."""
if not session_key or not _local_backend(policy):
return
from tools.terminal_tool import get_session_cwd, record_session_cwd
if get_session_cwd(session_key) is not None:
return
saved = (_entry_metadata(runner, session_key) or {}).get(_METADATA_KEY)
if isinstance(saved, str) and os.path.isabs(saved) and os.path.isdir(saved):
record_session_cwd(session_key, saved)
def persist_turn_cwd(runner, session_key: str | None, policy) -> None:
"""Write the chat's recorded cwd onto its routing entry when it changed (no write otherwise)."""
if not session_key or not _local_backend(policy):
return
from tools.terminal_tool import get_session_cwd
cwd = get_session_cwd(session_key)
metadata = _entry_metadata(runner, session_key) if cwd else None
if metadata is None or metadata.get(_METADATA_KEY) == cwd:
return
try:
runner.session_store.set_session_metadata(session_key, _METADATA_KEY, cwd)
except Exception as exc: # health: allow BLE001 -- runs in the turn's finally: a failed save must not fail the finished turn
logger.warning("Could not save the terminal cwd of %s: %s", session_key, exc)

View File

@@ -0,0 +1,137 @@
"""A Telegram chat's ``cd`` survives a gateway restart (#41128).
The terminal tool keeps each chat's working directory in a process-local record, so a restart sent
the chat's next command back to ``terminal.cwd``. Real daemon, loopback Bot API, and a model stub
that drives the real terminal tool: ``cd sub`` in one life, ``pwd`` in the next.
"""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import importlib.machinery
import json
from pathlib import Path
import queue
import shutil
import threading
import time
import pytest
from tests.gateway.fixtures.local_recovery_probe import child_env, daemon
from tests.gateway.test_native_telegram_startup_recovery import BotAPI, wait_for
class Model(BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
msgs = body.get('messages', [])
last = msgs[-1] if msgs else {}
user = next((m.get('content', '') for m in reversed(msgs) if m['role'] == 'user'), '')
if isinstance(user, list):
user = ' '.join(p.get('text', '') for p in user if isinstance(p, dict))
if last.get('role') == 'tool':
self.server.tool_outputs.append(last.get('content'))
msg = {'role': 'assistant', 'content': 'DONE ' + str(last.get('content'))[:300]}
finish = 'stop'
else:
cmd = self.server.sub_cmd if 'CD_SUB' in user else 'pwd'
msg = {'role': 'assistant', 'content': None, 'tool_calls': [{'id': f'c{len(self.server.tool_outputs)}', 'type': 'function',
'function': {'name': 'terminal', 'arguments': json.dumps({'command': cmd})}}]}
finish = 'tool_calls'
if body.get('stream'):
delta = dict(msg)
if 'tool_calls' in delta:
delta['tool_calls'] = [dict(tc, index=0) for tc in delta['tool_calls']]
payload = ('data: ' + json.dumps({'id': 'x', 'choices': [{'index': 0, 'delta': delta, 'finish_reason': finish}]})
+ '\n\ndata: [DONE]\n\n').encode()
kind = 'text/event-stream'
else:
payload = json.dumps({'id': 'x', 'choices': [{'index': 0, 'message': msg, 'finish_reason': finish}],
'usage': {'prompt_tokens': 1, 'completion_tokens': 1, 'total_tokens': 2}}).encode()
kind = 'application/json'
self.send_response(200)
self.send_header('Content-Type', kind)
self.send_header('Content-Length', str(len(payload)))
self.end_headers()
self.wfile.write(payload)
def _pwd_after_restart(tmp_path, *, remove_sub_before_restart):
root = Path(__file__).resolve().parents[2]
home, user, work = tmp_path / 'state', tmp_path / 'user', tmp_path / 'work'
home.mkdir(mode=0o700)
user.mkdir()
(work / 'sub').mkdir(parents=True)
model = ThreadingHTTPServer(('127.0.0.1', 0), Model)
model.tool_outputs, model.sub_cmd = [], f'cd {work / "sub"} && pwd'
bot = ThreadingHTTPServer(('127.0.0.1', 0), BotAPI)
bot.calls, bot.sent, bot.updates = [], [], queue.Queue()
for peer in (model, bot):
threading.Thread(target=peer.serve_forever, daemon=True).start()
base = f'http://127.0.0.1:{model.server_port}/v1'
cfg = {'model': {'provider': 'custom', 'default': 'stub', 'base_url': base},
'gateway': {'multiplex_profiles': False},
'platforms': {'telegram': {'enabled': True, 'extra': {
'base_url': f'http://127.0.0.1:{bot.server_port}/bot', 'dm_policy': 'allowlist'}}},
'auxiliary': {'title_generation': {'enabled': False}}, 'approvals': {'mode': 'off'},
'platform_toolsets': {'telegram': ['terminal']}, 'terminal': {'cwd': str(work), 'backend': 'local'}}
(home / 'config.yaml').write_text(json.dumps(cfg), encoding='utf-8')
env = child_env()
env.update(HOME=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root), OPENAI_API_KEY='loopback-only',
OPENAI_BASE_URL=base, PYTHONUNBUFFERED='1', TELEGRAM_BOT_TOKEN='987654321:owned-loopback-fixture',
TELEGRAM_ALLOWED_USERS='202')
update_id = 0
def push(text):
nonlocal update_id
update_id += 1
bot.updates.put({'update_id': update_id, 'message': {
'message_id': update_id, 'date': int(time.time()),
'chat': {'id': 202, 'type': 'private', 'first_name': 'Owned'},
'from': {'id': 202, 'is_bot': False, 'first_name': 'Owned'}, 'text': text}})
def diagnostic():
return repr(model.tool_outputs) + '\n' + '\n'.join(
p.read_text(encoding='utf-8')[-4000:] for p in (home / 'logs').glob('*.log'))
def pwd(index):
return json.loads(model.tool_outputs[index])['output'].strip()
try:
with daemon(root, home, env, barrier=False) as (proc, _desc):
push('CD_SUB please')
wait_for(lambda: len(model.tool_outputs) >= 1, diagnostic)
push('where now')
wait_for(lambda: len(model.tool_outputs) >= 2, diagnostic)
assert pwd(1) == str(work / 'sub'), diagnostic()
proc.terminate()
proc.wait(timeout=60)
if remove_sub_before_restart:
shutil.rmtree(work / 'sub')
with daemon(root, home, env, barrier=False) as (proc, _desc):
push('where after restart')
wait_for(lambda: len(model.tool_outputs) >= 3, diagnostic)
return pwd(2), work
finally:
for peer in (model, bot):
peer.shutdown()
peer.server_close()
pytestmark = [
pytest.mark.skipif(importlib.machinery.PathFinder.find_spec("telegram") is None,
reason="python-telegram-bot not installed (on-demand extra)"),
pytest.mark.platforms("linux"),
]
def test_a_chats_cd_survives_a_gateway_restart(tmp_path):
after, work = _pwd_after_restart(tmp_path, remove_sub_before_restart=False)
assert after == str(work / 'sub')
def test_a_directory_gone_after_the_restart_falls_back_to_terminal_cwd(tmp_path):
after, work = _pwd_after_restart(tmp_path, remove_sub_before_restart=True)
assert after == str(work)