fix(sdk): tighten slash command observation and regression coverage

This commit is contained in:
LogosVeritas committed 2026-10-25 21:55:28 +08:00
1 parent 9c3971618a
commit f7379ef366
29 files changed
+346 -199

No files matched your search

+11 -6
View File
@@ -493,7 +493,7 @@ def test_unimplemented_slash_remains_plain_text_without_recording_feedback(tmp_p
_PERMISSION_FACTS = {"permission/preset", "sandbox/mode", "approval/policy"}
_ASK_POLICY = "Approval policy: ask."
_ASK_POLICY = "Approval prompts are enabled in this session"
_NEVER_POLICY = "Approval prompts are disabled in this session"
@@ -562,8 +562,6 @@ def test_permission_changes_only_the_selected_session_and_retains_prompt_semanti
_assert_request_permission(requests[index], "workspace-write", "ask")
_assert_request_permission(requests[2], "read-only", "ask")
_assert_request_permission(requests[3], "danger-full-access", "never")
assert 'The approval policy changed from "ask" to "never" (changed by the user).' in _request_texts(requests[3])
assert 'The approval policy changed from "never" to "ask" (changed by the user).' in _request_texts(requests[4])
for index in [2, 3, 4]:
assert requests[index]["system"] == requests[0]["system"]
assert requests[index]["tools"] == requests[0]["tools"]
@@ -571,6 +569,11 @@ def test_permission_changes_only_the_selected_session_and_retains_prompt_semanti
assert "/permission" in _request_texts(requests[8])
events = _read_events(runtime.logs)
notices = [event["data"]["content"] for event in _of_type(events, "user/message")
if event["data"]["source"]["kind"] == "user-approval"]
assert len(notices) == 2
for content, request in zip(notices, requests[3:5]):
assert all(text in _request_texts(request) for text in _text_blocks(content))
executions = [readonly, invalid, repeated, full, restored]
assert len(_of_type(events, "command/run")) == len(executions)
for execution in executions:
@@ -613,12 +616,14 @@ def test_running_permission_change_keeps_the_native_extra_step_and_notifications
_assert_request_permission(requests[1], "danger-full-access", "never")
assert requests[0]["system"] == requests[1]["system"]
assert requests[0]["tools"] == requests[1]["tools"]
notice = 'The approval policy changed from "ask" to "never" (changed by the user).'
assert notice not in _request_texts(requests[0])
assert notice in _request_texts(requests[1])
assert runtime.harness.config.env["DSH_PERMISSION_MODE"] == "workspace-write"
events = _read_events(runtime.logs)
_command_pair(events, execution, "permission")
[notice] = [event["data"]["content"] for event in _of_type(events, "user/message")
if event["data"]["source"]["kind"] == "user-approval"]
for text in _text_blocks(notice):
assert text not in _request_texts(requests[0])
assert text in _request_texts(requests[1])
assert [event["data"] for event in _of_type(events, "step/start")] == [
{"turn": 1, "step": 1}, {"turn": 1, "step": 2},
]
+7 -5
View File
@@ -13,7 +13,7 @@ from test_commands_runtime import (
_command_pair, _of_type, _permission_facts, _read_events, _receive_result,
_request_texts, _runtime, _text_blocks,
)
from test_plan_runtime import _WaitResult, _wait_activity
from test_plan_runtime import _wait_activity
def _command(runtime: _Runtime, session: Session, text: str, *, kind: str = "success") -> CommandExecution:
@@ -73,11 +73,13 @@ def test_goal_continues_rounds_and_finishes_wrapup_after_completion_event(tmp_pa
completed = _until(events, "goal/change", "complete")
assert completed["goal"]["phase"] == "complete" and completed["roundsStarted"] == 2
assert final.entered.wait(_WAIT_SECONDS)
waiting = runtime.workers.submit(runtime.harness.client.request, "session/wait",
{"sessionId": session.id}, response_model=_WaitResult)
assert not waiting.done() and not final.release.is_set()
waiting = runtime.harness.client._begin_request("session/wait", {"sessionId": session.id})
# A later RPC confirms admission on the same pipe; keep the wait's
# response queued so worker scheduling cannot hide an early return.
runtime.call(runtime.harness.client.get_working_directory, session.id)
assert waiting.waiter.empty() and not final.release.is_set()
final.release.set()
waiting.result(timeout=_WAIT_SECONDS)
assert runtime.call(runtime.harness.client._wait_request, waiting) == {}
assert _until(events, "turn/end")["turn"] == 2
requests = runtime.model.captured()
assert len(requests) == 4
+85
View File
@@ -0,0 +1,85 @@
"""Run the plan example against the SDK profile with controlled model replies."""
from __future__ import annotations
import json
import runpy
import sys
from pathlib import Path
import pytest
from deepseek_harness import DeepSeekHarness, Notification
import test_commands_runtime as runtime_helpers
from test_commands_runtime import _Completion, _ModelServer, _dsh_bin, _of_type, _read_events
_EXAMPLE = Path(__file__).resolve().parents[1] / "examples/plan.py"
_PLAN = "# Small change\n\n- Inspect the code.\n- Implement after approval."
@pytest.mark.parametrize("outcome", ["max-tokens", "dismiss"])
def test_plan_example_reports_truncation_and_accepts_explicit_dismissal(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], outcome: str,
) -> None:
example = runpy.run_path(str(_EXAMPLE))
workspace = tmp_path / "workspace"
workspace.mkdir()
logs = tmp_path / "sessions"
patch = tmp_path / "persistence.yml"
patch.write_text(
"- id: session-persistence-jsonl\n config:\n root: " + json.dumps(str(logs))
+ "\n compression: none\n", encoding="utf-8",
)
completion = (_Completion("PARTIAL_PLAN") if outcome == "max-tokens"
else _Completion("", tool_call=("exit_plan_mode", {"plan": _PLAN})))
if outcome == "max-tokens":
response = runtime_helpers._messages_response
def truncated_response(*args: object) -> bytes:
return response(*args).replace(b'"stop_reason": "end_turn"', b'"stop_reason": "max_tokens"')
monkeypatch.setattr(runtime_helpers, "_messages_response", truncated_response)
choices: list[str] = []
def dismiss(prompt: str) -> str:
choices.append(prompt)
return "d"
monkeypatch.setattr("builtins.input", dismiss)
monkeypatch.setenv("DSH_TELEMETRY_DISABLED", "1")
monkeypatch.setenv("DSH_TOOLS_MODE", "native")
monkeypatch.setattr(sys, "argv", [str(_EXAMPLE), "--dsh-home", str(tmp_path / "home"),
"--workspace", str(workspace), "Plan a small change."])
with _ModelServer([completion]) as model:
def configured_harness(**options: object) -> DeepSeekHarness:
return DeepSeekHarness(
**options, dsh_bin=_dsh_bin(), patches=(str(patch),),
base_url=model.url, api_key="keyless-plan-example",
initialize_timeout_seconds=30, request_timeout_seconds=20, shutdown_timeout_seconds=20,
)
monkeypatch.setitem(example["main"].__globals__, "DeepSeekHarness", configured_harness)
if outcome == "max-tokens":
with pytest.raises(RuntimeError, match="max-tokens"):
example["main"]()
else:
example["main"]()
assert len(model.captured()) == 1
[log] = logs.rglob("session*.jsonl")
session_id = json.loads(log.read_text(encoding="utf-8").splitlines()[0])["id"]
events = _read_events(logs, session_id=session_id)
assert [event["data"]["reason"]["kind"] for event in _of_type(events, "turn/end")] == [
"max-tokens" if outcome == "max-tokens" else "completed",
]
assert [event["data"] for event in _of_type(events, "plan/mode")] == [{"active": True}]
assert len(choices) == (0 if outcome == "max-tokens" else 1)
assert ("PARTIAL_PLAN" if outcome == "max-tokens" else _PLAN) in capsys.readouterr().out
def test_plan_display_rejects_root_abort_without_rejecting_descendant_abort() -> None:
display = runpy.run_path(str(_EXAMPLE))["display"]
event = {"type": "turn/end", "data": {"turn": 1, "reason": {"kind": "aborted"}}}
display(Notification(method="session.event", payload={"sessionId": "child", "event": event}), "root")
with pytest.raises(RuntimeError, match="aborted"):
display(Notification(method="session.event", payload={"sessionId": "root", "event": event}), "root")