Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion agent_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ def dispatch(self, tool_name, args, response, index=0, tool_num=1):
yield f"未知工具: {tool_name}\n"
return StepOutcome(None, next_prompt=f"未知工具 {tool_name}", should_exit=False)

def tool_names_from_schema(tools_schema):
"""Collect tool names from a schema list.

Accepts both the flat form ({"name": ...}) and the OpenAI form
({"type": "function", "function": {"name": ...}}) -- the shipped
assets/tools_schema.json uses the latter, and reading only the top level
silently yields an empty set (which made text-channel recovery a no-op).
"""
names = set()
for entry in tools_schema or []:
if not isinstance(entry, dict): continue
fn = entry.get("function")
name = (fn.get("name") if isinstance(fn, dict) else None) or entry.get("name")
if name: names.add(name)
return names

def json_default(o): return list(o) if isinstance(o, set) else str(o)
def exhaust(g):
try:
Expand Down Expand Up @@ -66,7 +82,22 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
if cleaned: yield cleaned + '\n'
_hook('llm_after', locals())

if not response.tool_calls: tool_calls = [{'tool_name': 'no_tool', 'args': {}}]
if not response.tool_calls:
# #837: the model sometimes writes the call as literal markup in the text channel
# ("<code_run>...</code_run>" or a truncated "<code_run\">"). Recover and execute it
# instead of silently ending the turn; a torn marker still falls through to no_tool,
# which asks for a retry.
try:
from llmcore import split_text_tool_calls
_names = tool_names_from_schema(tools_schema)
_calls, _partial = split_text_tool_calls(response.content or '', _names)
except Exception:
_calls, _partial = [], False
if _calls:
if verbose: yield f"♻️ 文本通道工具调用已恢复执行: {', '.join(n for n, _ in _calls)}\n"
tool_calls = [{'tool_name': n, 'args': a, 'id': f'text_call_{turn}_{i}'} for i, (n, a) in enumerate(_calls)]
else:
tool_calls = [{'tool_name': 'no_tool', 'args': {}}]
else: tool_calls = [{'tool_name': tc.function.name, 'args': json.loads(tc.function.arguments), 'id': tc.id}
for tc in response.tool_calls]

Expand Down
148 changes: 148 additions & 0 deletions frontends/tests/test_dsml_marker_strip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"""Contracts for DSML tool-call marker stripping (llmcore) and the leak guard (ga.py).

Symptom: DeepSeek sometimes emits its DSML tool-call markup as plain assistant text
(issue #804). The closed PR #805 added a per-chunk content filter, and upstream f7e4cc5
added a narrow ``content.rstrip().endswith('</... calls>')`` retry in ``ga.py``. Three
cases still slipped through and stalled the agent loop:

* closing tags (``</... parameter>``) -- the pattern required ``<`` directly followed by
the DSML token, so a leading ``/`` never matched;
* the double full-width-pipe variant (``|P||P|DSML|P||P|``) -- matching started at the
second pipe, leaving a stray ``<`` / ``</`` behind;
* markers split across SSE chunks -- filtering each chunk independently cannot see them.

These tests load the DSML helpers straight from ``llmcore.py`` (no import, no mykey, no
side effects) and pin the integration points.
"""
from __future__ import annotations

import ast
import re
from pathlib import Path


ROOT = Path(__file__).resolve().parent.parent.parent
LLMCORE = ROOT / "llmcore.py"
GA = ROOT / "ga.py"

PIPE_ASCII = "|"
PIPE_FULL = chr(0xFF5C) # fullwidth vertical line
PIPE_HEAVY = chr(0x2016) # double vertical line

TAG_DOUBLE = PIPE_FULL * 2 + "DSML" + PIPE_FULL * 2 # ||DSML||
TAG_SINGLE = PIPE_FULL + "DSML" + PIPE_FULL # |DSML|
TAG_HEAVY = PIPE_HEAVY + "DSML" + PIPE_HEAVY # ||DSML|| (heavy)
TAG_ASCII = PIPE_ASCII + "DSML" + PIPE_ASCII # |DSML|
ALL_TAGS = (TAG_DOUBLE, TAG_SINGLE, TAG_HEAVY, TAG_ASCII)


def _dsml_namespace() -> dict:
"""Exec only the DSML constants/helpers from llmcore.py, without importing it."""
tree = ast.parse(LLMCORE.read_text(encoding="utf-8"))
wanted_assign = {"_DSML_WS", "_DSML_PIPES", "_DSML_TAG", "_DSML_PATTERN", "_DSML_LEAK"}
body = []
for node in tree.body:
if isinstance(node, ast.Assign) and any(
getattr(target, "id", None) in wanted_assign for target in node.targets
):
body.append(node)
elif isinstance(node, ast.FunctionDef) and node.name == "_strip_dsml_markers":
body.append(node)
elif isinstance(node, ast.ClassDef) and node.name == "_DsmlStreamFilter":
body.append(node)
names = [type(node).__name__ + ":" + getattr(node, "name", "") for node in body]
assert len(body) == 7, f"unexpected DSML declarations in llmcore.py: {names}"
namespace: dict = {"re": re}
exec(compile(ast.Module(body=body, type_ignores=[]), "llmcore_dsml.py", "exec"), namespace)
return namespace


NS = _dsml_namespace()
STRIP = NS["_strip_dsml_markers"]
STREAM_FILTER = NS["_DsmlStreamFilter"]


def _reset_latch() -> None:
NS["_DSML_LEAK"] = False


def test_strips_closing_tag_variants():
"""Regression for issue #804: '</' + token must be removed, not left as '</'."""
for tag in ALL_TAGS:
assert STRIP("</" + tag + " parameter>") == "", tag
assert STRIP("</" + tag + " calls>") == "", tag


def test_strips_invoke_and_parameter_variants():
for tag in ALL_TAGS:
assert STRIP("<" + tag + ' invoke name="ShellCommand" arguments="{}">') == "", tag
# markup is removed, the payload between the open/close markers is kept
assert STRIP("<" + tag + ' parameter name="cmd">ls -la</' + tag + " parameter>") == "ls -la", tag


def test_strips_markers_around_and_inside_text():
assert STRIP("before <" + TAG_SINGLE + " tool_calls>after") == "before after"
assert STRIP("a</" + TAG_DOUBLE + " parameter>b") == "ab"


def test_plain_text_is_untouched_and_does_not_latch():
_reset_latch()
for text in ("<summary>ok</summary>", "1 < 2 and 3 > 2", "<thinking>x</thinking>", ""):
assert STRIP(text) == text
assert NS["_DSML_LEAK"] is False


def test_latch_is_set_when_a_marker_is_removed():
"""The latch is what lets ga.do_no_tool retry even if payload text survives."""
_reset_latch()
assert NS["_DSML_LEAK"] is False
STRIP("</" + TAG_DOUBLE + " parameter>")
assert NS["_DSML_LEAK"] is True


def test_stream_filter_removes_marker_split_across_chunks():
stream_filter = STREAM_FILTER()
marker = TAG_HEAVY + "tool_calls>"
chunks = ["hello<" + marker[:3], marker[3:] + "world"]
out = "".join(stream_filter.feed(chunk) for chunk in chunks) + stream_filter.flush()
assert "DSML" not in out
assert out == "helloworld"


def test_stream_filter_removes_marker_split_right_after_open_bracket():
stream_filter = STREAM_FILTER()
out = "".join(
[
stream_filter.feed("hi <"),
stream_filter.feed(TAG_DOUBLE + " parameter>"),
stream_filter.flush(),
]
)
assert out == "hi "


def test_stream_filter_flushes_plain_text_unchanged():
stream_filter = STREAM_FILTER()
out = "".join([stream_filter.feed("plain "), stream_filter.flush()])
assert out == "plain "


def test_all_streamed_content_chunks_pass_through_the_filter():
"""Integration contract: every streamed content yield must be filtered."""
source = LLMCORE.read_text(encoding="utf-8")
# responses: delta, output_text.done, completed-snapshot fallback, incomplete/chat: delta
assert source.count("yield dsml_f.feed(") == 4, "responses delta/done/completed + chat delta"
assert source.count("dsml_f.flush()") == 2, "one flush per api_mode branch"
assert "_DSML_LEAK = False # this parse" in source, "latch reset per parse"


def test_non_streaming_paths_strip_too():
source = LLMCORE.read_text(encoding="utf-8")
assert 'content = _strip_dsml_markers(msg.get("content", ""))' in source
assert '_txt = _strip_dsml_markers(p["text"])' in source


def test_ga_no_tool_guard_uses_latch_and_variant_agnostic_tail_check():
source = GA.read_text(encoding="utf-8")
assert "getattr(response, 'dsml_leak', False)" in source
assert "</?[" + PIPE_ASCII + PIPE_FULL + PIPE_HEAVY + "]{1,2}DSML" in source
Loading