-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_postprocessor.py
More file actions
1214 lines (980 loc) · 44.6 KB
/
test_postprocessor.py
File metadata and controls
1214 lines (980 loc) · 44.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: Apache-2.0
"""Tests for StreamingPostProcessor — the unified streaming pipeline."""
from unittest.mock import MagicMock
from vllm_mlx.service.postprocessor import StreamingPostProcessor
def _make_cfg(**overrides):
"""Create a mock ServerConfig."""
cfg = MagicMock()
cfg.engine = None
cfg.reasoning_parser = None
cfg.reasoning_parser_name = None
cfg.enable_auto_tool_choice = False
cfg.tool_call_parser = None
cfg.tool_parser_instance = None
for k, v in overrides.items():
setattr(cfg, k, v)
return cfg
def _make_output(text="", finished=False, channel=None, finish_reason=None):
"""Create a mock GenerationOutput."""
out = MagicMock()
out.new_text = text
out.finished = finished
out.channel = channel
out.finish_reason = finish_reason or ("stop" if finished else None)
out.prompt_tokens = 10
out.completion_tokens = 5
out.tokens = []
out.logprobs = None
return out
class TestStreamingPostProcessorBasic:
"""Tests for basic content streaming (no reasoning, no tools)."""
def test_simple_content(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("Hello"))
assert len(events) == 1
assert events[0].type == "content"
assert events[0].content == "Hello"
def test_empty_text_skipped(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output(""))
assert len(events) == 0
def test_finish_event(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("Done", finished=True))
# Should have content + finish
content_events = [e for e in events if e.type == "content"]
finish_events = [e for e in events if e.type == "finish"]
assert len(finish_events) == 1
assert finish_events[0].finish_reason == "stop"
def test_special_tokens_stripped(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("Hello<|endoftext|>"))
assert len(events) >= 1
content = [e for e in events if e.type == "content"]
if content:
assert "<|endoftext|>" not in content[0].content
class TestStreamingPostProcessorChannelRouted:
"""Tests for OutputRouter (channel-routed) models."""
def test_content_channel(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("Hello", channel="content"))
assert len(events) == 1
assert events[0].type == "content"
assert events[0].content == "Hello"
def test_reasoning_channel(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("thinking...", channel="reasoning"))
assert len(events) == 1
assert events[0].type == "reasoning"
assert events[0].reasoning == "thinking..."
class TestStreamingPostProcessorReasoning:
"""Tests for text-based reasoning parser integration."""
def test_reasoning_extraction(self):
"""Reasoning parser separates thinking from content."""
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = "answer"
delta_msg.reasoning = "let me think"
parser.extract_reasoning_streaming.return_value = delta_msg
cfg = _make_cfg(reasoning_parser=parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("<think>let me think</think>answer"))
content_events = [e for e in events if e.type == "content"]
reasoning_events = [e for e in events if e.type == "reasoning"]
assert len(content_events) == 1
assert len(reasoning_events) == 1
assert "answer" in content_events[0].content
def test_reasoning_suppressed_chunk(self):
"""Parser returns None (e.g., inside <think> tag) → no events."""
parser = MagicMock()
parser.extract_reasoning_streaming.return_value = None
cfg = _make_cfg(reasoning_parser=parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("<think>"))
assert len(events) == 0
class TestStreamingPostProcessorToolCalls:
"""Tests for tool call detection."""
def _make_tool_parser(self):
parser = MagicMock()
parser.extract_tool_calls_streaming.return_value = None # default: suppressed
parser.has_pending_tool_call.return_value = False
return parser
def test_tool_markup_suppresses_content(self):
"""Content is suppressed while inside tool markup."""
tool_parser = self._make_tool_parser()
tool_parser.extract_tool_calls_streaming.return_value = None
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("<tool_call>"))
assert len(events) == 0
def test_tool_call_detected(self):
"""Tool call detection emits tool_call event."""
tool_parser = self._make_tool_parser()
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "call_1",
"type": "function",
"function": {"name": "test", "arguments": "{}"},
}
]
}
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("<tool_call>test</tool_call>"))
assert len(events) == 1
assert events[0].type == "tool_call"
assert events[0].tool_calls is not None
def test_content_after_tool_calls_suppressed(self):
"""After tool calls detected, remaining content is suppressed."""
tool_parser = self._make_tool_parser()
# First call: detect tool calls
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "call_1",
"type": "function",
"function": {"name": "test", "arguments": "{}"},
}
]
}
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
# Detect tool call
pp.process_chunk(_make_output("<tool_call>"))
assert pp.tool_calls_detected
# After detection, parser returns normal content but should be suppressed
tool_parser.extract_tool_calls_streaming.return_value = {
"content": "extra text"
}
events = pp.process_chunk(_make_output("extra text"))
assert len(events) == 0
def test_fallback_tool_detection_on_finalize(self):
"""Finalize detects tool calls when streaming detection missed them."""
tool_parser = self._make_tool_parser()
tool_parser.has_pending_tool_call.return_value = True
result = MagicMock()
result.tools_called = True
result.tool_calls = [{"id": "call_1", "name": "test", "arguments": "{}"}]
tool_parser.extract_tool_calls.return_value = result
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
# Accumulate some text without triggering streaming detection
pp.tool_accumulated_text = "<tool_call>{}"
events = pp.finalize()
assert len(events) == 1
assert events[0].type == "tool_call"
assert events[0].finish_reason == "tool_calls"
class TestStreamingPostProcessorNemotron:
"""Tests for Nemotron thinking prefix."""
def test_thinking_prefix_injected(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.set_thinking_model("nemotron-nano-30b")
pp.reset()
events = pp.process_chunk(_make_output("Starting to think"))
assert len(events) >= 1
content_events = [e for e in events if e.type == "content"]
assert content_events[0].content.startswith("<think>")
def test_thinking_prefix_only_once(self):
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.set_thinking_model("nemotron-nano-30b")
pp.reset()
pp.process_chunk(_make_output("First"))
events = pp.process_chunk(_make_output("Second"))
content_events = [e for e in events if e.type == "content"]
assert not content_events[0].content.startswith("<think>")
class TestStreamingPostProcessorFinishMerging:
"""Tests for content + finish_reason merging (prevents double-emission)."""
def test_final_chunk_single_event(self):
"""Final chunk with content + finish emits ONE event, not two."""
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("final word", finished=True))
# Should be exactly one finish event with content merged in
assert len(events) == 1
assert events[0].type == "finish"
assert events[0].finish_reason == "stop"
assert events[0].content is not None
def test_finish_without_content(self):
"""Finish-only chunk (empty text) emits finish event."""
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("", finished=True))
assert len(events) == 1
assert events[0].type == "finish"
def test_channel_routed_finish_merges(self):
"""Channel-routed path also merges content into finish."""
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(
_make_output("done", finished=True, channel="content")
)
assert len(events) == 1
assert events[0].type == "finish"
assert events[0].content is not None
def test_reasoning_finish_merges(self):
"""Reasoning path merges reasoning into finish."""
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = "answer"
delta_msg.reasoning = "thought"
parser.extract_reasoning_streaming.return_value = delta_msg
cfg = _make_cfg(reasoning_parser=parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("raw", finished=True))
assert len(events) == 1
assert events[0].type == "finish"
assert events[0].reasoning == "thought"
class TestStreamingPostProcessorMiniMaxRedirect:
"""Tests for MiniMax tool-in-thinking redirect."""
def test_tool_xml_in_reasoning_redirected(self):
"""Tool call XML in reasoning stream gets redirected to content."""
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = None
delta_msg.reasoning = "<tool_call>{}"
parser.extract_reasoning_streaming.return_value = delta_msg
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {"content": ""}
tool_parser.has_pending_tool_call.return_value = False
cfg = _make_cfg(
reasoning_parser=parser,
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
pp.process_chunk(_make_output("<tool_call>{}"))
# Tool parser should have been called (reasoning was redirected to content)
assert tool_parser.extract_tool_calls_streaming.called
class TestStreamingPostProcessorToolCallChannel:
"""Tests for tool_call channel routing."""
def test_tool_call_channel_with_parser(self):
"""Tool call channel content goes through tool parser."""
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "call_1",
"type": "function",
"function": {"name": "test", "arguments": "{}"},
}
]
}
tool_parser.has_pending_tool_call.return_value = False
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("<tool_call>", channel="tool_call"))
assert len(events) == 1
assert events[0].type == "tool_call"
def test_reasoning_channel_finish(self):
"""Reasoning channel with finish emits single finish event."""
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(
_make_output("final thought", finished=True, channel="reasoning")
)
assert len(events) == 1
assert events[0].type == "finish"
assert events[0].reasoning == "final thought"
# ======================================================================
# Coverage gap tests — model-specific edge cases + error paths
# ======================================================================
class TestToolParserInit:
"""Tests for _init_tool_parser paths (lines 72-99)."""
def test_init_creates_fresh_parser_not_singleton(self):
"""Tool parser is a fresh per-request instance, NOT cfg singleton."""
singleton = MagicMock()
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_call_parser="hermes",
tool_parser_instance=singleton,
)
pp = StreamingPostProcessor(cfg, tools_requested=True)
# Must NOT be the singleton — should be a fresh HermesToolParser
assert pp.tool_parser is not singleton
assert pp.tool_parser is not None
def test_init_parser_failure_returns_none(self):
"""Failed tool parser init returns None gracefully."""
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_call_parser="nonexistent_parser_xyz",
tool_parser_instance=None,
)
pp = StreamingPostProcessor(cfg)
# Should not crash, tool_parser should be None
assert pp.tool_parser is None
def test_auto_infer_minimax_parser(self):
"""Auto-infer MiniMax tool parser from reasoning_parser_name."""
cfg = _make_cfg(
reasoning_parser_name="minimax",
engine=MagicMock(_tokenizer=MagicMock()),
)
pp = StreamingPostProcessor(cfg, tools_requested=True)
# MiniMax parser should be auto-inferred from reasoning_parser_name
assert pp.tool_parser is not None
class TestChannelRoutedEdgeCases:
"""Tests for channel-routed path edge cases."""
def test_tool_call_channel_suppressed(self):
"""Tool call channel with parser returning None suppresses output."""
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = None
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("<tool_call>", channel="content"))
assert len(events) == 0
def test_channel_content_passthrough_no_tool_parser(self):
"""Content channel without tool parser passes through."""
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("hello world", channel="content"))
assert len(events) == 1
assert events[0].type == "content"
def test_channel_empty_after_sanitize(self):
"""Channel content that sanitizes to empty is dropped."""
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
# Special tokens only → sanitize strips everything
events = pp.process_chunk(_make_output("<|endoftext|>", channel="content"))
# May produce 0 events if sanitized to empty
content_events = [e for e in events if e.type == "content"]
for e in content_events:
assert e.content # no empty content events
def test_channel_tool_calls_detected_suppresses_subsequent(self):
"""After tool_calls detected via channel, subsequent content suppressed."""
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.side_effect = [
{
"tool_calls": [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "t", "arguments": "{}"},
}
]
},
{"content": "leftover"},
]
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
# First chunk: tool call detected
events1 = pp.process_chunk(_make_output("<tc>", channel="content"))
assert events1[0].type == "tool_call"
# Second chunk: should be suppressed
events2 = pp.process_chunk(_make_output("more", channel="content"))
assert len(events2) == 0
def test_channel_tool_calls_finish_event(self):
"""After tool_calls detected, finish chunk emits finish with tool_calls reason."""
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "t", "arguments": "{}"},
}
]
}
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
pp.process_chunk(_make_output("<tc>", channel="content"))
tool_parser.extract_tool_calls_streaming.return_value = {"content": ""}
events = pp.process_chunk(_make_output("", finished=True, channel="content"))
assert len(events) == 1
assert events[0].type == "finish"
assert events[0].finish_reason == "tool_calls"
class TestReasoningPathEdgeCases:
"""Tests for reasoning parser path edge cases."""
def test_reasoning_with_tool_suppression(self):
"""Reasoning path: tool parser returns None → suppressed."""
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = "content with <tool_call>"
delta_msg.reasoning = None
parser.extract_reasoning_streaming.return_value = delta_msg
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = None
cfg = _make_cfg(
reasoning_parser=parser,
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("<tool_call>"))
assert len(events) == 0
def test_reasoning_tool_calls_detected_finish(self):
"""Reasoning path: after tool calls, finish emits tool_calls reason."""
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = (
"<tool_call>markup" # must contain < to trigger full parsing
)
delta_msg.reasoning = None
parser.extract_reasoning_streaming.return_value = delta_msg
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "t", "arguments": "{}"},
}
]
}
cfg = _make_cfg(
reasoning_parser=parser,
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
pp.process_chunk(_make_output("<tool_call>markup"))
assert pp.tool_calls_detected
# Subsequent finish — tool_calls_detected so suppressed, finish emitted
delta_msg2 = MagicMock()
delta_msg2.content = ""
delta_msg2.reasoning = None
parser.extract_reasoning_streaming.return_value = delta_msg2
tool_parser.extract_tool_calls_streaming.return_value = {"content": ""}
events = pp.process_chunk(_make_output("", finished=True))
assert len(events) == 1
assert events[0].finish_reason == "tool_calls"
def test_reasoning_finish_on_suppressed_chunk(self):
"""Reasoning parser returns None on final chunk → finish event."""
parser = MagicMock()
parser.extract_reasoning_streaming.return_value = None
cfg = _make_cfg(reasoning_parser=parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("final", finished=True))
assert len(events) == 1
assert events[0].type == "finish"
def test_minimax_tool_call_in_reasoning_with_content(self):
"""MiniMax: tool XML in reasoning WITH existing content → both merged."""
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = "\n" # boundary content from </think>
delta_msg.reasoning = "<minimax:tool_call>{}"
parser.extract_reasoning_streaming.return_value = delta_msg
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {"content": "merged"}
cfg = _make_cfg(
reasoning_parser=parser,
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
pp.process_chunk(_make_output("raw"))
# Tool parser should receive content + reasoning merged
call_args = tool_parser.extract_tool_calls_streaming.call_args
assert call_args is not None
class TestStandardPathEdgeCases:
"""Tests for standard (no reasoning, no channel) path edge cases."""
def test_tool_fast_path_no_markup(self):
"""Standard path: content without < or [ takes fast path."""
tool_parser = MagicMock()
tool_parser.has_pending_tool_call.return_value = False
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("hello world"))
# extract_tool_calls_streaming should NOT be called (fast path)
assert not tool_parser.extract_tool_calls_streaming.called
assert len(events) == 1
assert events[0].type == "content"
def test_tool_markup_triggers_full_parsing(self):
"""Standard path: < in content triggers full tool parsing."""
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {"content": "text"}
tool_parser.has_pending_tool_call.return_value = False
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_parser_instance=tool_parser,
)
pp = StreamingPostProcessor(cfg)
pp.reset()
events = pp.process_chunk(_make_output("before <tag>"))
assert tool_parser.extract_tool_calls_streaming.called
# =====================================================================
# Per-request parser isolation (P1 singleton fix)
# =====================================================================
class TestPerRequestParserIsolation:
"""Verify that each PostProcessor gets its own parser instances,
not references to the global singleton from ServerConfig."""
def test_reasoning_parser_is_per_request(self):
"""Two PostProcessors must have different reasoning parser instances."""
cfg = _make_cfg(reasoning_parser_name="qwen3")
# cfg.reasoning_parser is the singleton — should NOT be used
cfg.reasoning_parser = MagicMock()
pp1 = StreamingPostProcessor(cfg)
pp2 = StreamingPostProcessor(cfg)
# Each must have its OWN parser, not the singleton
assert pp1.reasoning_parser is not cfg.reasoning_parser
assert pp2.reasoning_parser is not cfg.reasoning_parser
assert pp1.reasoning_parser is not pp2.reasoning_parser
def test_reasoning_parser_none_when_no_name(self):
"""No parser created when reasoning_parser_name is not set."""
cfg = _make_cfg(reasoning_parser_name=None)
pp = StreamingPostProcessor(cfg)
assert pp.reasoning_parser is None
def test_tool_parser_is_per_request(self):
"""Two PostProcessors must have different tool parser instances."""
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_call_parser="hermes",
)
# cfg.tool_parser_instance is the singleton — should NOT be used
cfg.tool_parser_instance = MagicMock()
pp1 = StreamingPostProcessor(cfg, tools_requested=True)
pp2 = StreamingPostProcessor(cfg, tools_requested=True)
assert pp1.tool_parser is not cfg.tool_parser_instance
assert pp2.tool_parser is not cfg.tool_parser_instance
assert pp1.tool_parser is not pp2.tool_parser
def test_tool_parser_auto_infer_is_per_request(self):
"""Auto-inferred tool parsers are also per-request."""
cfg = _make_cfg(reasoning_parser_name="minimax")
pp1 = StreamingPostProcessor(cfg, tools_requested=True)
pp2 = StreamingPostProcessor(cfg, tools_requested=True)
if pp1.tool_parser is not None:
assert pp1.tool_parser is not pp2.tool_parser
def test_concurrent_reset_does_not_corrupt(self):
"""Simulating concurrent usage: reset on one doesn't affect other."""
cfg = _make_cfg(reasoning_parser_name="qwen3")
pp1 = StreamingPostProcessor(cfg)
pp2 = StreamingPostProcessor(cfg)
# Simulate pp1 accumulating state
pp1.reset()
pp1.process_chunk(_make_output("Hello"))
assert pp1.accumulated_text == "Hello"
# pp2 resets independently
pp2.reset()
pp2.process_chunk(_make_output("World"))
# pp1's state should be untouched
assert pp1.accumulated_text == "Hello"
assert pp2.accumulated_text == "World"
def test_reasoning_parser_state_isolated(self):
"""Reasoning parser internal state is isolated between instances."""
cfg = _make_cfg(reasoning_parser_name="qwen3")
pp1 = StreamingPostProcessor(cfg)
pp2 = StreamingPostProcessor(cfg)
pp1.reset()
pp2.reset()
# Process a thinking chunk on pp1 — mutates pp1's parser state
pp1.process_chunk(_make_output("<think>reasoning"))
# pp2's parser should NOT have any accumulated state from pp1
assert pp2.reasoning_parser is not pp1.reasoning_parser
# Verify pp2's parser has clean internal state
if hasattr(pp2.reasoning_parser, "_buffer"):
assert pp2.reasoning_parser._buffer == ""
def test_graceful_fallback_on_bad_parser_name(self):
"""Invalid parser name results in None, not crash."""
cfg = _make_cfg(reasoning_parser_name="nonexistent_parser_xyz")
pp = StreamingPostProcessor(cfg)
assert pp.reasoning_parser is None
def test_graceful_fallback_on_bad_tool_parser(self):
"""Invalid tool parser name results in None, not crash."""
cfg = _make_cfg(
enable_auto_tool_choice=True,
tool_call_parser="nonexistent_parser_xyz",
)
pp = StreamingPostProcessor(cfg, tools_requested=True)
assert pp.tool_parser is None
# =====================================================================
# Coverage gap tests — edge cases in processing paths
# =====================================================================
class TestCoverageGaps:
"""Tests targeting specific uncovered lines for 100% coverage."""
def test_create_reasoning_parser_name_not_set(self):
"""Line 89: _create_reasoning_parser returns None when no name."""
result = StreamingPostProcessor._create_reasoning_parser(
_make_cfg(reasoning_parser_name=None)
)
assert result is None
def test_auto_infer_tool_parser_failure(self):
"""Lines 124-125: auto-infer tool parser exception path."""
from unittest.mock import patch
cfg = _make_cfg(reasoning_parser_name="minimax")
# Make ToolParserManager.get_tool_parser raise for "minimax"
with patch(
"vllm_mlx.tool_parsers.ToolParserManager.get_tool_parser",
side_effect=KeyError("minimax not found"),
):
pp = StreamingPostProcessor(cfg, tools_requested=True)
assert pp.tool_parser is None # graceful fallback
def test_channel_routed_tool_detected_then_finish(self):
"""Lines 199, 276-279: tool_calls_detected + finish in channel mode."""
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "f", "arguments": "{}"},
}
]
}
cfg = _make_cfg(tool_parser_instance=tool_parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
# First chunk: tool detected via channel routing (needs "<" to trigger parsing)
out1 = _make_output("<tool_call>test", channel="content")
events1 = pp.process_chunk(out1)
assert any(e.type == "tool_call" for e in events1)
assert pp.tool_calls_detected
# After tool detected, content with text should be suppressed (line 197-201)
# Return content (not None/suppressed) so we reach the tool_calls_detected check
tool_parser.extract_tool_calls_streaming.return_value = {"content": "trailing"}
out2 = _make_output("<more>text", channel="content")
events2 = pp.process_chunk(out2)
assert len(events2) == 0 # suppressed by tool_calls_detected
# Finish chunk with text after tool detected (line 199)
out3 = _make_output("<final>", finished=True, channel="content")
events3 = pp.process_chunk(out3)
assert any(
e.type == "finish" and e.finish_reason == "tool_calls" for e in events3
)
def test_channel_routed_sanitize_empty(self):
"""Line 216: content becomes None after sanitize_output."""
from unittest.mock import patch
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
# sanitize_output returns empty → content becomes None
with patch("vllm_mlx.service.postprocessor.sanitize_output", return_value=""):
out = _make_output("some text", channel="content")
events = pp.process_chunk(out)
content_events = [e for e in events if e.type == "content"]
assert len(content_events) == 0
def test_reasoning_path_tool_detected_then_finish(self):
"""Lines 276-279: tool_calls_detected then finish in reasoning path."""
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = "<tool_call>test"
delta_msg.reasoning = None
parser.extract_reasoning_streaming.return_value = delta_msg
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "f", "arguments": "{}"},
}
]
}
cfg = _make_cfg(reasoning_parser=parser, tool_parser_instance=tool_parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
# Detect tool calls (content has "<" to trigger full parsing)
events1 = pp.process_chunk(_make_output("<tool_call>test"))
assert any(e.type == "tool_call" for e in events1)
# After tool detected, content should be suppressed (lines 276-279)
tool_parser.extract_tool_calls_streaming.return_value = {"content": "trailing"}
events2 = pp.process_chunk(_make_output("<more>text"))
assert len(events2) == 0
# Finish with text after tool detection (line 276-277)
out = _make_output("<final>", finished=True)
events3 = pp.process_chunk(out)
assert any(e.finish_reason == "tool_calls" for e in events3)
def test_reasoning_path_sanitize_to_none(self):
"""Line 294: content sanitizes to empty in reasoning path."""
from unittest.mock import patch
parser = MagicMock()
delta_msg = MagicMock()
delta_msg.content = "text that sanitizes away"
delta_msg.reasoning = None
parser.extract_reasoning_streaming.return_value = delta_msg
cfg = _make_cfg(reasoning_parser=parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
with patch("vllm_mlx.service.postprocessor.sanitize_output", return_value=""):
events = pp.process_chunk(_make_output("text"))
content_events = [e for e in events if e.type == "content"]
assert len(content_events) == 0
def test_standard_path_sanitize_to_none(self):
"""Line 350: content sanitizes to empty in standard path."""
from unittest.mock import patch
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
with patch("vllm_mlx.service.postprocessor.sanitize_output", return_value=""):
events = pp.process_chunk(_make_output("text"))
content_events = [e for e in events if e.type == "content"]
assert len(content_events) == 0
def test_standard_path_tool_detected_then_finish(self):
"""Lines 334-335: tool_calls_detected + finish in standard path."""
tool_parser = MagicMock()
tool_parser.extract_tool_calls_streaming.return_value = {
"tool_calls": [
{
"index": 0,
"id": "c1",
"type": "function",
"function": {"name": "f", "arguments": "{}"},
}
]
}
cfg = _make_cfg(tool_parser_instance=tool_parser)
pp = StreamingPostProcessor(cfg)
pp.reset()
# Detect tool call (needs "<" to trigger full parsing)
events1 = pp.process_chunk(_make_output("<tool_call>test"))
assert pp.tool_calls_detected
assert any(e.type == "tool_call" for e in events1)
# After detection, content suppressed (line 332-336)
tool_parser.extract_tool_calls_streaming.return_value = {"content": "trailing"}
events2 = pp.process_chunk(_make_output("<more>text"))
assert len(events2) == 0
# Finish with text after tool detection (line 334)
out = _make_output("<final>", finished=True)
events3 = pp.process_chunk(out)
assert any(
e.type == "finish" and e.finish_reason == "tool_calls" for e in events3
)
def test_standard_path_empty_content_filtered(self):
"""Lines 340, 345: empty string content filtered out."""
from unittest.mock import patch
cfg = _make_cfg()
pp = StreamingPostProcessor(cfg)
pp.reset()
# strip_special_tokens returns empty string → content=None, no finish → []
with patch(
"vllm_mlx.service.postprocessor.strip_special_tokens", return_value=""
):