test_context.py 28.3 KB
Newer Older
1
2
3
4
5
6
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

from unittest.mock import MagicMock, patch

import pytest
7
from openai_harmony import Author, Message, Role, StreamState, TextContent
8

9
from vllm.entrypoints.openai.responses.context import (
10
    HarmonyContext,
11
    SimpleContext,
12
13
14
    StreamingHarmonyContext,
    TurnMetrics,
)
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
from vllm.outputs import CompletionOutput, RequestOutput


def create_mock_request_output(
    prompt_token_ids=None,
    output_token_ids=None,
    num_cached_tokens=0,
    finished=True,
):
    """Helper function to create a mock RequestOutput object for testing."""
    outputs = []
    token_ids = output_token_ids if output_token_ids is not None else []
    outputs = [
        CompletionOutput(
            index=0,
            text="Test output",
            token_ids=token_ids,
            cumulative_logprob=0.0,
            logprobs=None,
            finish_reason=None,
            stop_reason=None,
        )
    ]

    return RequestOutput(
        request_id="test-id",
        prompt="Test prompt",
        prompt_token_ids=prompt_token_ids,
        prompt_logprobs=None,
        outputs=outputs,
        finished=finished,
        num_cached_tokens=num_cached_tokens,
    )


50
51
52
async def generate_mock_outputs(
    num_turns, prompt_token_counts, output_token_counts, cached_token_counts=None
):
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    """Generate a sequence of mock RequestOutput objects to simulate multiple
    turns."""
    if cached_token_counts is None:
        cached_token_counts = [0] * num_turns

    for i in range(num_turns):
        # Create mock prompt token IDs and output token IDs
        prompt_token_ids = list(range(1, prompt_token_counts[i] + 1))
        output_token_ids = list(range(1, output_token_counts[i] + 1))

        # Create and yield the RequestOutput
        yield create_mock_request_output(
            prompt_token_ids=prompt_token_ids,
            output_token_ids=output_token_ids,
            num_cached_tokens=cached_token_counts[i],
        )


@pytest.fixture
def mock_parser():
    """Set up a mock parser for tests."""
74
    with patch(
75
        "vllm.entrypoints.openai.responses.context.get_streamable_parser_for_assistant"
76
    ) as mock_parser_factory:
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
        # Create a mock parser object
        parser = MagicMock()
        parser.messages = []
        parser.current_channel = None
        parser.state = StreamState.EXPECT_START
        mock_parser_factory.return_value = parser
        yield parser


def test_single_turn_token_counting():
    """Test token counting behavior for a single turn."""
    # Create a context
    context = HarmonyContext(messages=[], available_tools=[])

    # Create a mock RequestOutput with specific token counts
    mock_output = create_mock_request_output(
        prompt_token_ids=[1, 2, 3, 4, 5],  # 5 prompt tokens
        output_token_ids=[6, 7, 8],  # 3 output tokens
        num_cached_tokens=2,  # 2 cached tokens
    )

    # Append the output to the context
    context.append_output(mock_output)

    # Verify the token counts
    assert context.num_prompt_tokens == 5
    assert context.num_output_tokens == 3
    assert context.num_cached_tokens == 2
    assert context.num_tool_output_tokens == 0  # No tool tokens in first turn

    # Verify internal state tracking
    assert not context.is_first_turn
109
110
111
112
113
114
    assert len(context.all_turn_metrics) == 1
    previous_turn = context.all_turn_metrics[0]
    assert previous_turn.input_tokens == 5
    assert previous_turn.output_tokens == 3
    assert previous_turn.cached_input_tokens == 2
    assert previous_turn.tool_output_tokens == 0
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129


@pytest.mark.asyncio
async def test_multi_turn_token_counting():
    """Test token counting behavior across multiple turns with tool output."""
    # Create a context
    context = HarmonyContext(messages=[], available_tools=["browser"])

    # Simulate a conversation with 3 turns
    # Turn 1: prefill 5, decode 3, tool 7
    # Turn 2: prefill 15, cached 5, decode 4, tool 1
    # Turn 3: prefill 20, cached 15, decode 5
    prompt_token_counts = [5, 15, 20]
    output_token_counts = [3, 4, 5]
    cached_token_counts = [0, 5, 15]
130
131
132
    mock_generator = generate_mock_outputs(
        3, prompt_token_counts, output_token_counts, cached_token_counts
    )
133
134

    # First turn - initial prompt and response
135
    mock_output1 = await anext(mock_generator)
136
137
138
139
140
141
142
143
    context.append_output(mock_output1)

    # At this point, we should have 5 prompt tokens and 3 output tokens
    assert context.num_prompt_tokens == 5
    assert context.num_output_tokens == 3
    assert context.num_tool_output_tokens == 0

    # Second turn - after tool output
144
    mock_output2 = await anext(mock_generator)
145
146
147
148
149
150
151
152
153
154
155
    context.append_output(mock_output2)
    # Current prompt tokens (15) - last_turn_input_tokens (5) -
    # last_turn_output_tokens (3) = 7
    expected_tool_output = 7

    assert context.num_prompt_tokens == 5 + 15
    assert context.num_output_tokens == 3 + 4
    assert context.num_tool_output_tokens == expected_tool_output
    assert context.num_cached_tokens == 5

    # Third turn - final response
156
    mock_output3 = await anext(mock_generator)
157
158
159
160
161
162
163
164
165
166
167
    context.append_output(mock_output3)
    # Additional tool output tokens from third turn:
    # Current prompt (20) - last_turn_input_tokens (15) -
    # last_turn_output_tokens (4) = 1
    expected_tool_output = 7 + 1

    assert context.num_prompt_tokens == 5 + 15 + 20
    assert context.num_output_tokens == 3 + 4 + 5
    assert context.num_tool_output_tokens == expected_tool_output
    assert context.num_cached_tokens == 5 + 15

168
169
170
171
172
173
174
175
176
    # Validate all turn metrics
    assert len(context.all_turn_metrics) == 3
    for i, turn in enumerate(context.all_turn_metrics):
        assert turn.input_tokens == prompt_token_counts[i]
        assert turn.output_tokens == output_token_counts[i]
        assert turn.cached_input_tokens == cached_token_counts[i]
    assert context.all_turn_metrics[1].tool_output_tokens == 7
    assert context.all_turn_metrics[2].tool_output_tokens == 1

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

def test_empty_output_tokens():
    """Test behavior when RequestOutput has empty output tokens."""
    context = HarmonyContext(messages=[], available_tools=[])

    # Create a RequestOutput with empty output tokens
    mock_output = create_mock_request_output(
        prompt_token_ids=[1, 2, 3],  # 3 prompt tokens
        output_token_ids=[],  # Empty output tokens list
        num_cached_tokens=1,
    )

    context.append_output(mock_output)

    # Should handle empty outputs gracefully
    assert context.num_prompt_tokens == 3
    assert context.num_output_tokens == 0  # No output tokens
    assert context.num_cached_tokens == 1
    assert context.num_tool_output_tokens == 0


def test_missing_prompt_token_ids():
    """Test behavior when RequestOutput has None prompt_token_ids."""
    context = HarmonyContext(messages=[], available_tools=[])

    mock_output = create_mock_request_output(
        prompt_token_ids=None,  # No prompt token IDs
        output_token_ids=[1, 2],  # 2 output tokens
        num_cached_tokens=0,
    )

    # Logger.error will be called, but we don't need to check for warnings
    # here Just ensure it doesn't raise an exception
    context.append_output(mock_output)

    # Should handle missing prompt tokens gracefully
    assert context.num_prompt_tokens == 0
    assert context.num_output_tokens == 2
    assert context.num_cached_tokens == 0
    assert context.num_tool_output_tokens == 0


def test_reasoning_tokens_counting(mock_parser):
    """Test that reasoning tokens are counted correctly."""
    context = HarmonyContext(messages=[], available_tools=[])

    # Mock parser to simulate reasoning channel
    mock_parser.current_channel = "analysis"  # Reasoning channel

    mock_output = create_mock_request_output(
        prompt_token_ids=[1, 2, 3],
        output_token_ids=[4, 5, 6, 7],  # 4 tokens, all in reasoning
        num_cached_tokens=0,
    )

    context.append_output(mock_output)

    # All output tokens should be counted as reasoning
    assert context.num_reasoning_tokens == 4
    assert context.num_output_tokens == 4


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
def test_preamble_tokens_not_counted_as_reasoning(mock_parser):
    """Preambles (commentary with no recipient) are visible user text,
    not hidden reasoning. They must NOT inflate num_reasoning_tokens."""
    context = HarmonyContext(messages=[], available_tools=[])

    mock_parser.current_channel = "commentary"
    mock_parser.current_recipient = None  # preamble

    mock_output = create_mock_request_output(
        prompt_token_ids=[1, 2, 3],
        output_token_ids=[4, 5, 6],
        num_cached_tokens=0,
    )
    context.append_output(mock_output)

    assert context.num_reasoning_tokens == 0
    assert context.num_output_tokens == 3


def test_commentary_with_recipient_counted_as_reasoning(mock_parser):
    """Commentary directed at a tool (recipient != None) is hidden from
    the user, so it should still count as reasoning tokens."""
    context = HarmonyContext(messages=[], available_tools=[])

    mock_parser.current_channel = "commentary"
    mock_parser.current_recipient = "python"

    mock_output = create_mock_request_output(
        prompt_token_ids=[1, 2, 3],
        output_token_ids=[4, 5, 6],
        num_cached_tokens=0,
    )
    context.append_output(mock_output)

    assert context.num_reasoning_tokens == 3
    assert context.num_output_tokens == 3


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
def test_zero_tokens_edge_case():
    """Test behavior with all zero token counts."""
    context = HarmonyContext(messages=[], available_tools=[])

    # Create a request with empty lists (not None) for both prompt and
    # output tokens
    mock_output = create_mock_request_output(
        prompt_token_ids=[],  # Empty prompt tokens
        output_token_ids=[],  # Empty output tokens
        num_cached_tokens=0,
    )

    context.append_output(mock_output)

    # All counts should be zero
    assert context.num_prompt_tokens == 0
    assert context.num_output_tokens == 0
    assert context.num_cached_tokens == 0
    assert context.num_tool_output_tokens == 0
    assert context.num_reasoning_tokens == 0


@pytest.mark.asyncio
async def test_single_turn_no_tool_output():
    """Test that first turn never generates tool output tokens."""
    context = HarmonyContext(
        messages=[],
304
        available_tools=["browser"],  # Tools available
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    )

    # Even with large prompt in first turn, no tool tokens should be counted
    mock_output = create_mock_request_output(
        prompt_token_ids=list(range(100)),  # 100 tokens
        output_token_ids=[1, 2, 3],
        num_cached_tokens=0,
    )

    context.append_output(mock_output)

    # First turn should never have tool output tokens
    assert context.num_tool_output_tokens == 0
    assert context.is_first_turn is False  # Should be updated after first turn


@pytest.mark.asyncio
async def test_negative_tool_tokens_edge_case():
    """Test edge case where calculation could result in negative tool
    tokens. We should log an error and clamp the value to 0."""
    # Use patch to check if logger.error was called
326
    with patch("vllm.entrypoints.openai.responses.context.logger.error") as mock_log:
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
        context = HarmonyContext(messages=[], available_tools=["browser"])

        # First turn
        mock_output1 = create_mock_request_output(
            prompt_token_ids=list(range(10)),  # 10 tokens
            output_token_ids=[1, 2, 3, 4, 5],  # 5 tokens
        )
        context.append_output(mock_output1)

        # Second turn with fewer new tokens than previous output
        # This could happen in edge cases with aggressive caching
        mock_output2 = create_mock_request_output(
            prompt_token_ids=list(range(12)),  # 12 tokens (only 2 new)
            output_token_ids=[6, 7],  # 2 tokens
        )
        context.append_output(mock_output2)

        # Calculated negative tool tokens (12 - 10 - 5 = -3) should be clamped
        # to 0 and an error should be logged
        assert context.num_tool_output_tokens == 0
        assert context.num_prompt_tokens == 10 + 12
        assert context.num_output_tokens == 5 + 2

        # Verify the error was logged properly
        mock_log.assert_called_once()

        # Extract the actual log message and arguments from the call
        args, _ = mock_log.call_args
        log_message = args[0]

        # Check for key parts of the message
        assert "Negative tool output tokens calculated" in log_message
        assert "-3" in str(args)  # Check that -3 is in the arguments


@pytest.mark.asyncio
async def test_streaming_multi_turn_token_counting(mock_parser):
    """Test token counting for streaming multi-turn conversations.
365
366
367

    This test focuses on how StreamingHarmonyContext counts tokens in a
    multi-turn conversation with streaming (token-by-token) outputs and
368
369
370
371
372
    message boundaries.
    """
    # Create a streaming context
    context = StreamingHarmonyContext(messages=[], available_tools=["browser"])

373
374
375
376
    num_prompt_tokens = [3, 8, 13]
    num_output_tokens = [3, 3, 2]
    num_cached_tokens = [0, 3, 8]

377
378
379
380
381
382
383
384
385
386
387
    # Simulate three turns of conversation:
    # Turn 1: stream tokens one by one, then finish the message
    # Turn 2: new prompt, stream more tokens with a reasoning segment
    # Turn 3: new prompt with tool output and cached tokens

    # First turn: 3 tokens streamed one by one
    # First token of first turn
    context.append_output(
        create_mock_request_output(
            prompt_token_ids=[1, 2, 3],  # 3 prompt tokens
            output_token_ids=[101],  # Single token
388
            num_cached_tokens=num_cached_tokens[0],
389
            finished=False,  # Not end of message yet
390
391
        )
    )
392
393
394
395
396
397

    # Second token of first turn
    context.append_output(
        create_mock_request_output(
            output_token_ids=[102],
            finished=False,
398
399
        )
    )
400
401
402
403
404
405

    # Last token of first turn (finished=True signals end of message)
    context.append_output(
        create_mock_request_output(
            output_token_ids=[103],
            finished=True,  # End of message
406
407
        )
    )
408
409
410
411
412
413
414
415
416
417
418
419
420
421

    # Check token counts after first turn
    assert context.num_prompt_tokens == 3  # Initial prompt tokens
    assert context.num_output_tokens == 3  # Three output tokens
    assert context.num_cached_tokens == 0
    assert context.num_tool_output_tokens == 0  # No tool output in first turn
    assert context.first_tok_of_message is True  # Ready for next message

    # Second turn: reasoning tokens in analysis channel
    mock_parser.current_channel = "analysis"  # Set to reasoning channel

    # First token of second turn
    context.append_output(
        create_mock_request_output(
422
423
424
425
426
427
428
429
430
431
            prompt_token_ids=[
                1,
                2,
                3,
                101,
                102,
                103,
                4,
                5,
            ],  # 8 tokens (includes previous)
432
            output_token_ids=[201],
433
            num_cached_tokens=num_cached_tokens[1],  # Some tokens cached
434
            finished=False,
435
436
        )
    )
437
438
439
440
441
442

    # More tokens in reasoning channel
    context.append_output(
        create_mock_request_output(
            output_token_ids=[202],
            finished=False,
443
444
        )
    )
445
446
447
448
449

    context.append_output(
        create_mock_request_output(
            output_token_ids=[203],
            finished=True,  # End of reasoning message
450
451
        )
    )
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469

    # Check counts after second turn (reasoning message)
    assert context.num_prompt_tokens == 3 + 8  # Initial + second prompt
    assert context.num_output_tokens == 3 + 3  # First turn + second turn
    assert context.num_reasoning_tokens == 3  # All tokens in analysis channel
    assert context.num_cached_tokens == 3  # Cached tokens from second turn

    # Formula: this turn prompt tokens - last turn prompt - last turn output
    expected_tool_tokens = 8 - 3 - 3  # = 2
    assert context.num_tool_output_tokens == expected_tool_tokens

    # Third turn: regular output channel
    mock_parser.current_channel = "final"  # Switch back to regular channel

    # Third turn (with more cached tokens)
    context.append_output(
        create_mock_request_output(
            prompt_token_ids=[
470
471
472
473
474
475
476
477
478
479
480
481
482
                1,
                2,
                3,
                101,
                102,
                103,
                4,
                5,
                201,
                202,
                203,
                6,
                7,
483
484
            ],  # 13 tokens
            output_token_ids=[301],
485
            num_cached_tokens=num_cached_tokens[2],  # More cached tokens
486
            finished=False,
487
488
        )
    )
489
490
491
492
493

    context.append_output(
        create_mock_request_output(
            output_token_ids=[302],
            finished=True,
494
495
        )
    )
496
497

    # Final token counts check
498
499
    assert context.num_prompt_tokens == sum(num_prompt_tokens)  # All prompts
    assert context.num_output_tokens == sum(num_output_tokens)  # All outputs
500
    assert context.num_reasoning_tokens == 3  # Unchanged from second turn
501
502
503
    assert context.num_cached_tokens == sum(
        num_cached_tokens
    )  # Accumulated cached tokens
504
505
506
507

    # Additional tool tokens from third turn
    # Formula: this turn prompt - last turn prompt - last turn output
    additional_tool_tokens = 13 - 8 - 3  # = 2
508
509
510
    assert (
        context.num_tool_output_tokens == expected_tool_tokens + additional_tool_tokens
    )
511

512
513
514
515
516
517
518
519
520
    # Validate all turn metrics
    assert len(context.all_turn_metrics) == 3
    for i, turn in enumerate(context.all_turn_metrics):
        assert turn.input_tokens == num_prompt_tokens[i]
        assert turn.output_tokens == num_output_tokens[i]
        assert turn.cached_input_tokens == num_cached_tokens[i]
    assert context.all_turn_metrics[1].tool_output_tokens == 2
    assert context.all_turn_metrics[2].tool_output_tokens == 2

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538

@pytest.mark.asyncio
async def test_streaming_message_synchronization(mock_parser):
    """Test message synchronization logic from lines 413-417 in context.py.

    This test verifies that when parser.messages contains more messages than
    the context's _messages (minus initial messages), the context properly
    extends its message list with the new parser messages.
    """

    # Create a streaming context with some initial messages
    initial_messages = [
        Message(
            author=Author(role=Role.USER, name="user"),
            content=[TextContent(text="Hello")],
            recipient=Role.ASSISTANT,
        )
    ]
539
    context = StreamingHarmonyContext(messages=initial_messages, available_tools=[])
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

    # Verify initial state
    assert len(context._messages) == 1
    assert context.num_init_messages == 1

    # Mock parser to have more messages than context
    # Simulate parser having processed 3 new messages
    mock_parser.messages = [
        Message(
            author=Author(role=Role.ASSISTANT, name="assistant"),
            content=[TextContent(text="Response 1")],
            recipient=Role.USER,
        ),
    ]

    # This should trigger the message synchronization logic
    context.append_output(
557
558
559
560
        create_mock_request_output(
            prompt_token_ids=[1, 2, 3], output_token_ids=[101], finished=False
        )
    )
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581

    # Verify that messages were synchronized
    assert len(context._messages) == 2

    # Verify the new messages were added correctly
    assert context._messages[1].content[0].text == "Response 1"

    # Test the specific condition from line 413-414:
    # len(self._messages) - self.num_init_messages < len(self.parser.messages)
    messages_minus_init = len(context._messages) - context.num_init_messages
    parser_messages_count = len(mock_parser.messages)

    # After synchronization, they should be equal (no longer less than)
    assert messages_minus_init == parser_messages_count

    # Test edge case: add one more parser message
    mock_parser.messages.append(
        Message(
            author=Author(role=Role.ASSISTANT, name="assistant"),
            content=[TextContent(text="Response 4")],
            recipient=Role.USER,
582
583
        )
    )
584
585

    # Create another output to trigger synchronization again
586
587
588
    mock_output2 = create_mock_request_output(
        prompt_token_ids=[1, 2, 3], output_token_ids=[102], finished=True
    )
589
590
591
592
593
594
595

    context.append_output(mock_output2)

    # Verify the fourth message was added, num_init_messages is still 1
    assert len(context._messages) == 3
    assert context.num_init_messages == 1
    assert context._messages[2].content[0].text == "Response 4"
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


def test_turn_metrics_copy_and_reset():
    """Test TurnMetrics copy and reset methods work correctly."""
    # Create a TurnMetrics with specific values
    original_metrics = TurnMetrics(
        input_tokens=10,
        output_tokens=20,
        cached_input_tokens=5,
        tool_output_tokens=3,
    )

    # Test copy functionality
    copied_metrics = original_metrics.copy()

    # Verify copy has same values
    assert copied_metrics.input_tokens == 10
    assert copied_metrics.output_tokens == 20
    assert copied_metrics.cached_input_tokens == 5
    assert copied_metrics.tool_output_tokens == 3

    # Verify they are separate objects
    assert copied_metrics is not original_metrics

    # Modify copy to ensure independence
    copied_metrics.input_tokens = 999
    assert original_metrics.input_tokens == 10  # Original unchanged
    assert copied_metrics.input_tokens == 999

    # Test reset functionality
    original_metrics.reset()

    # Verify all fields are reset to zero
    assert original_metrics.input_tokens == 0
    assert original_metrics.output_tokens == 0
    assert original_metrics.cached_input_tokens == 0
    assert original_metrics.tool_output_tokens == 0

    # Verify copied metrics are unaffected by reset
    assert copied_metrics.input_tokens == 999
    assert copied_metrics.output_tokens == 20
    assert copied_metrics.cached_input_tokens == 5
    assert copied_metrics.tool_output_tokens == 3
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


# ==================== SimpleContext Tests ====================


def create_simple_context_output(
    text="",
    token_ids=None,
    prompt="Test prompt",
    prompt_token_ids=None,
    num_cached_tokens=0,
    logprobs=None,
    finished=True,
):
    """Helper to create a RequestOutput with customizable text for
    SimpleContext tests."""
    if token_ids is None:
        token_ids = []
    return RequestOutput(
        request_id="test-id",
        prompt=prompt,
        prompt_token_ids=prompt_token_ids,
        prompt_logprobs=None,
        outputs=[
            CompletionOutput(
                index=0,
                text=text,
                token_ids=token_ids,
                cumulative_logprob=0.0,
                logprobs=logprobs,
                finish_reason=None,
                stop_reason=None,
            )
        ],
        finished=finished,
        num_cached_tokens=num_cached_tokens,
    )


def test_simple_context_output_messages_empty():
    """output_messages should be empty before any output is appended."""
    context = SimpleContext()
    assert context.output_messages == []


def test_simple_context_output_messages_single_call():
    """Non-streaming: single append_output produces a single output message."""
    context = SimpleContext()
    output = create_simple_context_output(
        text="Hello world",
        token_ids=[10, 20, 30],
        prompt_token_ids=[1, 2, 3],
    )
    context.append_output(output)

    messages = context.output_messages
    assert len(messages) == 1
    assert messages[0].message == "Hello world"
    assert messages[0].tokens == [10, 20, 30]
    assert messages[0].type == "raw_message_tokens"


def test_simple_context_output_messages_streaming_consolidation():
    """Streaming: multiple append_output calls consolidate into one message."""
    context = SimpleContext()

    # Simulate 3 streaming deltas
    context.append_output(
        create_simple_context_output(
            text="Hello",
            token_ids=[10],
            prompt_token_ids=[1, 2, 3],
        )
    )
    context.append_output(
        create_simple_context_output(
            text=" world",
            token_ids=[20],
            prompt_token_ids=[1, 2, 3],
        )
    )
    context.append_output(
        create_simple_context_output(
            text="!",
            token_ids=[30],
            prompt_token_ids=[1, 2, 3],
        )
    )

    messages = context.output_messages
    assert len(messages) == 1
    assert messages[0].message == "Hello world!"
    assert messages[0].tokens == [10, 20, 30]


def test_simple_context_output_messages_many_deltas():
    """Streaming with many small deltas still produces a single message."""
    context = SimpleContext()

    words = ["The", " quick", " brown", " fox", " jumps"]
    for i, word in enumerate(words):
        context.append_output(
            create_simple_context_output(
                text=word,
                token_ids=[100 + i],
                prompt_token_ids=[1, 2],
            )
        )

    messages = context.output_messages
    assert len(messages) == 1
    assert messages[0].message == "The quick brown fox jumps"
    assert messages[0].tokens == [100, 101, 102, 103, 104]


def test_simple_context_input_messages():
    """input_messages is populated on the first append_output call."""
    context = SimpleContext()
    assert context.input_messages == []

    context.append_output(
        create_simple_context_output(
            text="Hi",
            token_ids=[10],
            prompt="My prompt text",
            prompt_token_ids=[1, 2, 3],
        )
    )

    assert len(context.input_messages) == 1
    assert context.input_messages[0].message == "My prompt text"
    assert context.input_messages[0].tokens == [1, 2, 3]

    # Second call should not add another input message
    context.append_output(
        create_simple_context_output(
            text=" there",
            token_ids=[20],
            prompt="My prompt text",
            prompt_token_ids=[1, 2, 3],
        )
    )

    assert len(context.input_messages) == 1


def test_simple_context_token_counting():
    """Token counting accumulates across streaming deltas."""
    context = SimpleContext()

    context.append_output(
        create_simple_context_output(
            text="a",
            token_ids=[10, 11],
            prompt_token_ids=[1, 2, 3, 4, 5],
            num_cached_tokens=2,
        )
    )
    context.append_output(
        create_simple_context_output(
            text="b",
            token_ids=[12],
            prompt_token_ids=[1, 2, 3, 4, 5],
            num_cached_tokens=2,
        )
    )

    assert context.num_prompt_tokens == 5
    assert context.num_output_tokens == 3  # 2 + 1
    assert context.num_cached_tokens == 2


def test_simple_context_final_output():
    """final_output reconstructs accumulated text and token_ids."""
    context = SimpleContext()

    context.append_output(
        create_simple_context_output(
            text="foo",
            token_ids=[1, 2],
            prompt_token_ids=[10],
        )
    )
    context.append_output(
        create_simple_context_output(
            text="bar",
            token_ids=[3],
            prompt_token_ids=[10],
        )
    )

    final = context.final_output
    assert final is not None
    assert final.outputs[0].text == "foobar"
    assert final.outputs[0].token_ids == (1, 2, 3)


def test_simple_context_output_messages_empty_text_with_tokens():
    """output_messages should be returned when tokens exist even if text is
    empty (e.g. special tokens)."""
    context = SimpleContext()
    context.append_output(
        create_simple_context_output(
            text="",
            token_ids=[99],
            prompt_token_ids=[1],
        )
    )

    messages = context.output_messages
    assert len(messages) == 1
    assert messages[0].message == ""
    assert messages[0].tokens == [99]


def test_simple_context_output_messages_no_mutation():
    """Each call to output_messages returns a fresh list; callers can't
    corrupt internal state."""
    context = SimpleContext()
    context.append_output(
        create_simple_context_output(
            text="hello",
            token_ids=[1],
            prompt_token_ids=[10],
        )
    )

    msgs1 = context.output_messages
    msgs2 = context.output_messages
    assert msgs1 is not msgs2
    assert msgs1[0].message == msgs2[0].message

    # Appending more output updates the property
    context.append_output(
        create_simple_context_output(
            text=" world",
            token_ids=[2],
            prompt_token_ids=[10],
        )
    )

    msgs3 = context.output_messages
    assert len(msgs3) == 1
    assert msgs3[0].message == "hello world"
    assert msgs3[0].tokens == [1, 2]