fix(sdk): keep query internal and return session identity

This commit is contained in:
LogosVeritas committed 2026-10-28 17:15:21 +08:00
1 parent 9976a87e97
commit 1950259ce1
25 files changed
+229 -111

No files matched your search

+31 -1
View File
@@ -7,7 +7,7 @@ from pathlib import Path
import pytest
from deepseek_harness import CommandExecution, RunResult, Session, UserQuestionRequest
from deepseek_harness import CommandExecution, RunCommandResult, RunResult, Session, UserQuestionRequest
from test_commands_runtime import (
_Completion, _Runtime, _SESSION_ID, _WAIT_SECONDS, _assert_request_permission,
_command_pair, _of_type, _permission_facts, _read_events,
@@ -214,6 +214,36 @@ def test_plan_state_survives_resume_without_changing_permissions(tmp_path: Path)
assert [e["data"] for e in _of_type(events, "plan/mode")] == [{"active": True}]
def test_top_level_plan_result_identifies_the_session_for_wait_continue_and_resume(tmp_path: Path) -> None:
first = _Completion("PLAN_READY", blocked=True)
with _runtime(tmp_path, [first, _Completion("CONTINUED_PLAN")], initial_permission="workspace-write") as runtime:
execution = runtime.call(runtime.harness.run, "/plan Design this change without tools.")
assert isinstance(execution, RunCommandResult)
assert execution.result.kind == "success"
assert first.entered.wait(_WAIT_SECONDS), runtime.model.failures
assert not first.release.is_set()
session = runtime.harness.start_session(execution.session_id)
first.release.set()
_wait_activity(runtime, session)
continued = runtime.call(runtime.harness.run, "Continue this plan without tools.", session_id=execution.session_id)
assert isinstance(continued, RunResult)
assert continued.session_id == execution.session_id
assert continued.final_response == "CONTINUED_PLAN"
assert "Design this change without tools." in _request_texts(runtime.model.captured()[1])
with _runtime(tmp_path, [_Completion("RESTORED_PLAN")], initial_permission="workspace-write") as resumed:
session = resumed.call(resumed.harness.resume_session, execution.session_id)
result = resumed.call(session.run, "Finish reviewing this plan without tools.")
assert isinstance(result, RunResult) and result.session_id == execution.session_id
assert result.final_response == "RESTORED_PLAN"
[request] = resumed.model.captured()
assert _GUIDANCE in _effective_system(request)
assert "Design this change without tools." in _request_texts(request)
assert "Continue this plan without tools." in _request_texts(request)
events = _read_events(resumed.logs, session_id=execution.session_id)
_command_pair(events, execution, "plan")
assert len(_of_type(events, "turn/start")) == 3
def test_plan_state_is_local_and_literal_prompts_do_not_toggle_it(tmp_path: Path) -> None:
with _runtime(tmp_path, [_Completion("OTHER"), _Completion("PLANNING"), _Completion("RAW_INPUT")],
initial_permission="workspace-write") as runtime:
+46 -1
View File
@@ -10,7 +10,7 @@ from concurrent.futures import ThreadPoolExecutor
import pytest
from pydantic import BaseModel, ValidationError
from deepseek_harness import CommandExecution, DeepSeekHarness, Notification, RunResult, SdkProtocolError
from deepseek_harness import CommandExecution, RunCommandResult, DeepSeekHarness, Notification, RunResult, SdkProtocolError
from deepseek_harness.errors import JsonRpcError
@@ -146,11 +146,56 @@ def test_run_returns_command_without_waiting_for_idle(sdk: DeepSeekHarness, entr
result = (sdk.run("/compact", session_id="main", on_notification=notifications.append)
if entry == "harness" else sdk.start_session("main").run("/compact", on_notification=notifications.append))
assert isinstance(result, CommandExecution)
assert isinstance(result, RunCommandResult)
assert result.session_id == "main"
assert result.result.kind == "success"
assert [notice.payload["event"]["type"] for notice in notifications] == ["command/run", "command/done"]
assert_released(sdk)
def test_top_level_command_results_identify_distinct_sessions_and_allow_continuing_one(
sdk: DeepSeekHarness, tmp_path: Path,
) -> None:
first = sdk.run("/compact")
second = sdk.run("/compact")
assert isinstance(first, RunCommandResult) and isinstance(second, RunCommandResult)
assert first.session_id and second.session_id and first.session_id != second.session_id
continued = sdk.run("continue the first session", session_id=first.session_id)
assert isinstance(continued, RunResult)
assert continued.session_id == first.session_id
sent = [json.loads(line) for line in (tmp_path / "requests.jsonl").read_text(encoding="utf-8").splitlines()]
queries = [frame["params"] for frame in sent if frame["method"] == "session/query"]
assert [query["sessionId"] for query in queries] == [first.session_id, second.session_id, first.session_id]
assert_released(sdk)
@pytest.mark.parametrize("result", [
{"kind": "success"},
{"kind": "success", "text": "Compacted.", "sourceEventSeq": 0},
{"kind": "success", "sourceEventSeq": 2**53 - 1},
{"kind": "error", "text": "The agent is busy."},
])
def test_run_adds_session_identity_without_changing_command_results(peer: Path, tmp_path: Path, result: dict) -> None:
execution = {"commandId": "custom-command", "result": result}
with DeepSeekHarness(
cwd=str(tmp_path), dsh_home=str(tmp_path / "home"), request_timeout_seconds=5,
_launch_args=(sys.executable, str(peer)),
env={"REQUEST_LOG": str(tmp_path / "requests.jsonl"), "QUERY_RESPONSE": json.dumps({
"kind": "command", "execution": execution,
})},
) as sdk:
returned = sdk.run("custom-response")
assert isinstance(returned, RunCommandResult)
assert returned.session_id
assert returned.model_dump(by_alias=True, exclude_none=True) == {
**execution, "session_id": returned.session_id,
}
low_level = sdk.client._query(returned.session_id, "custom-response", "queue")
assert isinstance(low_level, CommandExecution)
assert low_level.model_dump(by_alias=True, exclude_none=True) == execution
assert_released(sdk)
@pytest.mark.parametrize("text", ["ordinary", "fast-prompt", "/model", " /compact extra "])
def test_run_preserves_ordinary_text_and_result(sdk: DeepSeekHarness, text: str) -> None:
notifications = []