feat(session): centralize working directories and directory context

This commit is contained in:
Tianyi Cui committed 2026-10-06 01:27:54 +08:00
1 parent 3d9cd9fa95
commit 79bd3d8da7
497 files changed
+5670 -1187

No files matched your search

+4 -4
View File
@@ -11,7 +11,7 @@
"@deepseek-ai/cordis-plugin-loader": "workspace:~",
"@deepseek-ai/cordis-plugin-timer": "workspace:~",
"@deepseek-ai/cosmokit": "workspace:~",
"@deepseek-ai/dsh": "workspace:*",
"@deepseek-ai/dsh": "workspace:~",
"@deepseek-ai/dsh-acp": "workspace:*",
"@deepseek-ai/dsh-agent": "workspace:*",
"@deepseek-ai/dsh-agent-default-model": "workspace:*",
@@ -79,8 +79,6 @@
"@deepseek-ai/dsh-plan-mode": "workspace:*",
"@deepseek-ai/dsh-plugin-manager": "workspace:*",
"@deepseek-ai/dsh-plugin-package-inventory-deepseek": "workspace:*",
"@deepseek-ai/dsh-ptc-runtime": "workspace:*",
"@deepseek-ai/dsh-ptc-runtime-node": "workspace:*",
"@deepseek-ai/dsh-pwsh-local": "workspace:*",
"@deepseek-ai/dsh-repeat-tool-reminder": "workspace:*",
"@deepseek-ai/dsh-sandbox": "workspace:*",
@@ -163,6 +161,8 @@
"@deepseek-ai/dsh-workflow-ptc": "workspace:*",
"@deepseek-ai/dsh-otel": "workspace:*",
"@deepseek-ai/dsh-workspace": "workspace:*",
"@deepseek-ai/schemastery": "workspace:~"
"@deepseek-ai/schemastery": "workspace:~",
"@deepseek-ai/dsh-tool-working-directory": "workspace:*",
"@deepseek-ai/dsh-working-directory": "workspace:*"
}
}
+2
View File
@@ -70,3 +70,5 @@ The shipped `sdk-minimal` profile is a standalone explicit tree rather than an o
The selected home stores profiles, plugins, and every profile-owned durable resource. The full `sdk` profile uses its credentials, settings, and session stores; `sdk-minimal` uses only its JSONL session store. Use a fresh home when those resources must be isolated, and a fresh session id for independent work. Reusing both a harness and session id continues the durable conversation and session-owned resources.
See the [Python tutorial](../../docs/user/guide/python-sdk.md), [runnable example](examples/README.md), and [runtime wheel reference](../sdk-runtime/README.md).
`Session.get_working_directory()` reads the effective working directory; `set_working_directory(path)` changes it and returns the validated absolute path. Both create an unknown Session without running a model turn. Relative paths use that Session's current directory. Origin metadata, permissions, and existing processes remain unchanged. The low-level client provides the same methods with a leading `session_id` argument.
+2
View File
@@ -70,3 +70,5 @@ with DeepSeekHarness(
所选 home 保存 profile、插件与每个 profile 自有的持久资源。完整 `sdk` profile 使用其中的凭据、设置与会话存储;`sdk-minimal` 只使用自己的 JSONL 会话存储。需要隔离这些资源时应使用新的 home;独立工作应使用新的会话 ID。同时复用 harness 与会话 ID 会延续持久对话和会话资源。
另见 [Python 教程](../../docs/user/guide/python-sdk.zh.md)、[可运行示例](examples/README.zh.md) 和 [运行时 wheel 包参考](../sdk-runtime/README.zh.md)。
`Session.get_working_directory()` 读取有效工作目录;`set_working_directory(path)` 修改目录并返回校验后的绝对路径。两者都可创建未知 Session,而不运行模型轮次。相对路径以该 Session 当前目录解析。起始目录元数据、权限与已有进程保持不变。底层客户端提供同名方法,并以 `session_id` 作为首个参数。
+10
View File
@@ -136,6 +136,16 @@ class Session:
self.harness = harness
self.id = session_id
def get_working_directory(self) -> str:
"""Read the effective directory, recovering a missing directory to the Session origin."""
self.harness.start()
return self.harness.client.get_working_directory(self.id)
def set_working_directory(self, path: str) -> str:
"""Change the effective directory while existing processes retain their own directories."""
self.harness.start()
return self.harness.client.set_working_directory(self.id, path)
def run(
self,
input: str | list[JsonObject],
+22
View File
@@ -188,6 +188,24 @@ class HarnessClient:
)
return response.messageId
def get_working_directory(self, session_id: str) -> str:
"""Read the effective directory, recovering a missing directory to the Session origin."""
response = self.request(
"session/working-directory/get",
{"sessionId": session_id},
response_model=_WorkingDirectoryResponse,
)
return response.cwd
def set_working_directory(self, session_id: str, path: str) -> str:
"""Enter an absolute or Session-relative directory without changing origin or permissions."""
response = self.request(
"session/working-directory/set",
{"sessionId": session_id, "path": path},
response_model=_WorkingDirectoryResponse,
)
return response.cwd
def request(
self,
method: str,
@@ -581,6 +599,10 @@ class _SessionPromptResponse(BaseModel):
messageId: str
class _WorkingDirectoryResponse(BaseModel):
cwd: str
class _ShutdownResponse(BaseModel):
pass
@@ -0,0 +1,54 @@
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")