Files
deepseek-harness/python/sdk/tests/test_working_directory.py
T
LegGasai ed30d274f1 refactor(working-directory): move the workspace only through Host operations
Remove the model-facing working_directory tool and the cwd parameter of the
subagent tools, so only Host operations move a Session's directory. Every
change the service records carries `workspace: true`, and the workspace root
is the directory of the last marked change, or the original project. The
anchor field, the anchor list, relocate, inherit, and anchorsOf are gone;
get, ensure, set, and rootOf remain.

A change without the marker, recorded while the model could move the
directory, moves only the current directory, so stored Sessions keep the
root they had.

- worktree: create enters the checkout through set; exit returns the Session
  to the source checkout of the recorded worktree that contains its directory,
  and exit_worktree exposes it to the model.
- subagent: a child starts in the parent's workspace root or a Host-chosen
  directory through set.
- SDK: the working-directory methods return { cwd } again and
  getWorkspaceRoot / get_workspace_root are removed.
- Recorded sessions: the worktree scenario leaves through exit_worktree and
  covers recovery from a removed worktree; the two scenarios that only drove
  the removed tool are deleted; three compaction scenarios lower their tuned
  context window by the removed tool-schema tokens.
2026-11-11 20:10:17 +08:00

55 lines
2.0 KiB
Python

from __future__ import annotations
import sys
from pathlib import Path
import pytest
from pydantic import ValidationError
from deepseek_harness import DeepSeekHarness
def test_session_directories_are_independent_and_relative_paths_stay_on_the_wire(tmp_path: Path) -> None:
runtime = tmp_path / "directory_runtime.py"
runtime.write_text(
"""
import json
import os
import sys
directories = {}
origin = None
for line in sys.stdin:
frame = json.loads(line)
method = frame['method']
params = frame.get('params') or {}
if method == 'initialize':
origin = params['cwd']
result = {'serverInfo': {'name': 'directory-runtime', 'version': '1'}}
elif method == 'shutdown':
print(json.dumps({'jsonrpc': '2.0', 'id': frame['id'], 'result': {}}), flush=True)
break
elif method == 'session/working-directory/get':
result = {'cwd': directories.get(params['sessionId'], origin)}
elif method == 'session/working-directory/set':
assert not os.path.isabs(params['path']), 'client must preserve relative paths'
previous = directories.get(params['sessionId'], origin)
current = os.path.normpath(os.path.join(previous, params['path']))
directories[params['sessionId']] = current
result = {'cwd': 7 if params['path'] == 'malformed' else current}
else:
raise AssertionError(method)
print(json.dumps({'jsonrpc': '2.0', 'id': frame['id'], 'result': result}), flush=True)
""".strip()
)
with DeepSeekHarness(cwd=str(tmp_path), _launch_args=(sys.executable, str(runtime))) as harness:
first = harness.start_session("first")
second = harness.start_session("second")
assert first.get_working_directory() == str(tmp_path)
assert first.set_working_directory("child") == str(tmp_path / "child")
assert first.get_working_directory() == str(tmp_path / "child")
assert second.get_working_directory() == str(tmp_path)
with pytest.raises(ValidationError):
second.set_working_directory("malformed")