返回 DeepSeek-TUI-2026
protocol_recovery.rs
根目录 / crates / tui / tests / protocol_recovery.rs
1 //! Protocol-recovery contract tests.
2 //!
3 //! These tests exist to keep the engine hostile to fake tool-call wrappers
4 //! (XML/Replit/markdown pseudo-calls in assistant text). Their job is to make
5 //! sure that:
6 //!
7 //! 1. The known wrapper markers are still present in `core/engine.rs` so the
8 //! streaming filter has something to scrub.
9 //! 2. The legacy text-based `tool_parser` does NOT treat the newer
10 //! `<function_calls>` wrapper as a real tool call — only the legacy
11 //! `[TOOL_CALL]` and `<invoke>` shapes ever produced structured calls, and
12 //! nothing should silently re-enable text-based execution.
13 //! 3. The closing-marker list stays the same length as the start-marker list,
14 //! so filter logic cannot get stuck in tool-call mode forever.
15 //!
16 //! The point is that protocol drift in the model output should be visible (we
17 //! still strip it and emit a status notice), not silently turned into tool
18 //! execution.
19
20 use std::fs;
21
22 #[path = "../src/core/tool_parser.rs"]
23 #[allow(dead_code)]
24 mod tool_parser;
25
26 // `engine.rs` was decomposed into submodules under `core/engine/`. The
27 // protocol-scrubbing strings the tests below assert on are now spread
28 // across `engine.rs` and several `engine/*.rs` files. We compile-time
29 // include each so a contributor moving a marker into a sibling submodule
30 // does not silently break these regression checks.
31 const ENGINE_SOURCES: &[&str] = &[
32 include_str!("../src/core/engine.rs"),
33 include_str!("../src/core/engine/streaming.rs"),
34 include_str!("../src/core/engine/turn_loop.rs"),
35 include_str!("../src/core/engine/dispatch.rs"),
36 include_str!("../src/core/engine/tool_setup.rs"),
37 include_str!("../src/core/engine/tool_execution.rs"),
38 include_str!("../src/core/engine/tool_catalog.rs"),
39 include_str!("../src/core/engine/context.rs"),
40 include_str!("../src/core/engine/approval.rs"),
41 include_str!("../src/core/engine/capacity_flow.rs"),
42 include_str!("../src/core/engine/lsp_hooks.rs"),
43 ];
44
45 fn any_engine_source_contains(needle: &str) -> bool {
46 ENGINE_SOURCES.iter().any(|src| src.contains(needle))
47 }
48
49 const EXPECTED_START_MARKERS: &[&str] = &[
50 "[TOOL_CALL]",
51 "<deepseek:tool_call",
52 "<tool_call",
53 "<invoke ",
54 "<function_calls>",
55 ];
56
57 const EXPECTED_END_MARKERS: &[&str] = &[
58 "[/TOOL_CALL]",
59 "</deepseek:tool_call>",
60 "</tool_call>",
61 "</invoke>",
62 "</function_calls>",
63 ];
64
65 #[test]
66 fn engine_keeps_known_fake_wrapper_start_markers() {
67 for marker in EXPECTED_START_MARKERS {
68 let needle = format!("\"{marker}\"");
69 assert!(
70 any_engine_source_contains(&needle),
71 "no engine source file still mentions start marker `{marker}` — \
72 protocol scrubbing may have regressed. Searched for {needle:?} \
73 across engine.rs and engine/* submodules."
74 );
75 }
76 }
77
78 #[test]
79 fn engine_keeps_known_fake_wrapper_end_markers() {
80 for marker in EXPECTED_END_MARKERS {
81 let needle = format!("\"{marker}\"");
82 assert!(
83 any_engine_source_contains(&needle),
84 "no engine source file still mentions end marker `{marker}` — \
85 protocol scrubbing may have regressed. Searched for {needle:?} \
86 across engine.rs and engine/* submodules."
87 );
88 }
89 }
90
91 #[test]
92 fn engine_marker_counts_stay_paired() {
93 // A future contributor could quietly drop a closing marker and leave the
94 // filter able to enter tool-call mode without ever leaving it. Lock the
95 // count to whatever the constants currently declare.
96 assert_eq!(EXPECTED_START_MARKERS.len(), EXPECTED_END_MARKERS.len());
97 assert!(any_engine_source_contains("TOOL_CALL_START_MARKERS"));
98 assert!(any_engine_source_contains("TOOL_CALL_END_MARKERS"));
99 }
100
101 #[test]
102 fn engine_emits_compact_fake_wrapper_notice() {
103 assert!(
104 any_engine_source_contains("FAKE_WRAPPER_NOTICE"),
105 "no engine source file references the protocol-recovery notice constant"
106 );
107 assert!(
108 any_engine_source_contains("API tool channel"),
109 "the protocol-recovery notice should mention the API tool channel"
110 );
111 }
112
113 #[test]
114 fn legacy_parser_extracts_bracket_tool_call() {
115 let result = tool_parser::parse_tool_calls(
116 "intro [TOOL_CALL]\n{\"tool\":\"x\",\"args\":{}}\n[/TOOL_CALL]",
117 );
118 assert_eq!(result.tool_calls.len(), 1);
119 assert_eq!(result.tool_calls[0].name, "x");
120 assert_eq!(result.clean_text, "intro");
121 }
122
123 #[test]
124 fn legacy_parser_extracts_invoke_block() {
125 let result = tool_parser::parse_tool_calls(
126 "before <invoke name=\"do_thing\"><parameter name=\"k\">v</parameter></invoke> after",
127 );
128 assert_eq!(result.tool_calls.len(), 1);
129 assert_eq!(result.tool_calls[0].name, "do_thing");
130 }
131
132 #[test]
133 fn legacy_parser_does_not_execute_function_calls_wrapper() {
134 // The newer `<function_calls>` wrapper is the kind of forged shape that
135 // shows up in non-DeepSeek tool-call leakage. The legacy text parser must
136 // NOT turn it into a structured tool call (the engine's filter still
137 // strips it from visible text and the model is expected to use the API
138 // tool channel instead).
139 let raw = "narrative <function_calls>\n{\"name\":\"x\",\"input\":{}}\n</function_calls> end";
140 let result = tool_parser::parse_tool_calls(raw);
141 assert!(
142 result.tool_calls.is_empty(),
143 "function_calls wrapper must not be parsed as a real tool call: {:?}",
144 result.tool_calls
145 );
146 }
147
148 #[test]
149 fn legacy_parser_has_marker_helper_for_legacy_shapes_only() {
150 // The legacy parser's `has_tool_call_markers` is documentation of which
151 // shapes it ever knew how to handle. If it ever starts returning true for
152 // `<function_calls>`, the parser may also have started producing fake
153 // tool calls — we want to fail loudly in that case.
154 assert!(tool_parser::has_tool_call_markers(
155 "noise [TOOL_CALL]x[/TOOL_CALL]"
156 ));
157 assert!(tool_parser::has_tool_call_markers(
158 "noise <invoke name=\"x\"></invoke>"
159 ));
160 assert!(!tool_parser::has_tool_call_markers(
161 "noise <function_calls>{}</function_calls>"
162 ));
163 }
164
165 #[test]
166 fn engine_source_file_still_exists_and_is_non_trivial() {
167 // Sanity check so the `include_str!` above is meaningful — if the engine
168 // module ever moves, this test must be updated alongside it.
169 let metadata = fs::metadata("src/core/engine.rs").expect("engine.rs must exist next to tests");
170 assert!(
171 metadata.len() > 10_000,
172 "engine.rs is unexpectedly small ({} bytes); did the file move?",
173 metadata.len()
174 );
175 }
176
176 lines RUST