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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Unit tests for usage tracking (ring buffer, stream capture, prompt capture)."""

import asyncio
import json
import time

import pytest

from free_claude_code.core.anthropic import (
    Message,
    MessagesRequest,
)
from free_claude_code.core.usage_tracking import (
    PendingUsageRecord,
    UsageRecord,
    UsageRingBuffer,
    UsageTrackingStream,
    extract_prompt,
    get_buffer,
    init_buffer,
    reset_buffer,
)


def _record(
    request_id: str, *, timestamp: float | None = None, **overrides
) -> UsageRecord:
    values = {
        "request_id": request_id,
        "timestamp": time.time() if timestamp is None else timestamp,
        "provider": "openai_compatible_1",
        "provider_model": "gpt-4o",
        "gateway_model": "gpt-4o",
        "wire_api": "messages",
        "input_tokens": 10,
        "output_tokens": 20,
        "cache_creation_tokens": 5,
        "cache_read_tokens": 3,
        "reasoning_tokens": 2,
        "duration_ms": 100,
        "status": "success",
        "error_type": None,
        "prompt": "hello",
    }
    values.update(overrides)
    return UsageRecord(**values)


def test_push_and_query_newest_first(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(_record("r1", timestamp=100.0))
    buffer.push(_record("r2", timestamp=200.0))

    records = buffer.query()

    assert [r.request_id for r in records] == ["r2", "r1"]


def test_query_since_filters(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(_record("r1", timestamp=100.0))
    buffer.push(_record("r2", timestamp=200.0))
    buffer.push(_record("r3", timestamp=300.0))

    records = buffer.query(since=200.0)

    assert [r.request_id for r in records] == ["r3", "r2"]


def test_ring_evicts_oldest_when_full(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json", max_entries=2)
    buffer.push(_record("r1", timestamp=100.0))
    buffer.push(_record("r2", timestamp=200.0))
    buffer.push(_record("r3", timestamp=300.0))

    records = buffer.query()

    assert [r.request_id for r in records] == ["r3", "r2"]
    assert all(r.request_id != "r1" for r in buffer.records())


def test_stats_aggregates(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(
        _record(
            "r1",
            timestamp=100.0,
            input_tokens=10,
            output_tokens=20,
            cache_creation_tokens=5,
            cache_read_tokens=3,
            reasoning_tokens=2,
            status="success",
        )
    )
    # r2/r3 override the default cache/reasoning values set by _record().
    buffer.push(
        _record(
            "r2",
            timestamp=200.0,
            input_tokens=100,
            output_tokens=200,
            cache_creation_tokens=10,
            cache_read_tokens=0,
            reasoning_tokens=0,
            status="error",
            error_type="upstream_error",
        )
    )
    buffer.push(
        _record(
            "r3",
            timestamp=300.0,
            input_tokens=1,
            output_tokens=1,
            cache_creation_tokens=0,
            cache_read_tokens=0,
            reasoning_tokens=0,
            status="cancelled",
        )
    )

    stats = buffer.stats()

    assert stats["total_requests"] == 3
    assert stats["total_input_tokens"] == 111
    assert stats["total_output_tokens"] == 221
    assert stats["total_cache_creation_tokens"] == 15
    assert stats["total_cache_read_tokens"] == 3
    assert stats["total_reasoning_tokens"] == 2
    assert stats["errors"] == 1
    assert stats["cancelled"] == 1


def test_stats_since_filters(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(_record("r1", timestamp=100.0, input_tokens=1))
    buffer.push(_record("r2", timestamp=300.0, input_tokens=10))

    stats = buffer.stats(since=200.0)

    assert stats["total_requests"] == 1
    assert stats["total_input_tokens"] == 10


def test_tpm_tps_known_window(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(
        _record(
            "r1", timestamp=100.0, input_tokens=10, output_tokens=20, duration_ms=100
        )
    )
    buffer.push(
        _record(
            "r2", timestamp=110.0, input_tokens=30, output_tokens=40, duration_ms=100
        )
    )

    # Both records sit inside the trailing 60s window ending at now=110.
    tpm, tps = buffer.tpm_tps(now=110.0)

    # 100 total tokens over the 60s window -> 100 tokens/minute; the
    # per-second rate counts output tokens only (60 output tokens ->
    # 1 token/second).
    assert tpm == pytest.approx(100.0)
    assert tps == pytest.approx(1.0)


def test_tpm_tps_custom_window(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(
        _record(
            "r1", timestamp=100.0, input_tokens=10, output_tokens=20, duration_ms=100
        )
    )
    buffer.push(
        _record(
            "r2", timestamp=110.0, input_tokens=30, output_tokens=40, duration_ms=100
        )
    )

    # A 10s window ending at now=110 still contains both records.
    tpm, tps = buffer.tpm_tps(now=110.0, window_seconds=10)

    # 100 total tokens over the 10s window -> 600 tokens/minute; the
    # per-second rate counts output tokens only (60 output tokens ->
    # 6 tokens/second).
    assert tpm == pytest.approx(600.0)
    assert tps == pytest.approx(6.0)


def test_tps_counts_output_tokens_only(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(
        _record(
            "r1", timestamp=100.0, input_tokens=90, output_tokens=10, duration_ms=100
        )
    )

    # A 100s window ending at now=200 keeps the record in scope.
    tpm, tps = buffer.tpm_tps(now=200.0, window_seconds=100)

    # 100 total tokens over the 100s window -> 60 tokens/minute; output-only
    # 10 tokens over 100 seconds -> 0.1 tokens/second.
    assert tpm == pytest.approx(60.0)
    assert tps == pytest.approx(0.1)


def test_tpm_tps_ignores_records_older_than_window(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(
        _record(
            "r1", timestamp=100.0, input_tokens=90, output_tokens=10, duration_ms=100
        )
    )
    buffer.push(
        _record(
            "r2", timestamp=300.0, input_tokens=50, output_tokens=50, duration_ms=100
        )
    )

    # r2 is inside the window, r1 is too old to count.
    tpm, tps = buffer.tpm_tps(now=320.0, window_seconds=60)

    assert tpm == pytest.approx(100.0)
    assert tps == pytest.approx(50.0 / 60.0)


def test_tpm_tps_zero_when_window_quiet(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(_record("r1", timestamp=100.0, input_tokens=10, output_tokens=20))

    # The only record is older than the window, so traffic is idle.
    tpm, tps = buffer.tpm_tps(now=200.0)

    assert tpm == 0.0
    assert tps == 0.0


def test_tpm_tps_empty_buffer():
    buffer = UsageRingBuffer()

    assert buffer.tpm_tps() == (0.0, 0.0)


def test_file_round_trip(tmp_path):
    path = tmp_path / "usage.json"
    buffer = UsageRingBuffer(path)
    buffer.push(_record("r1", timestamp=100.0, prompt="first prompt"))

    reloaded = UsageRingBuffer(path)
    records = reloaded.query()

    assert len(records) == 1
    record = records[0]
    assert record.request_id == "r1"
    assert record.input_tokens == 10
    assert record.output_tokens == 20
    assert record.prompt == "first prompt"


def test_persistence_contains_prompt(tmp_path):
    path = tmp_path / "usage.json"
    buffer = UsageRingBuffer(path)
    buffer.push(_record("r1", prompt="a captured prompt"))

    data = json.loads(path.read_text(encoding="utf-8"))

    assert data["version"] == 1
    assert data["records"][0]["prompt"] == "a captured prompt"


def test_init_and_reset_buffer(tmp_path):
    reset_buffer()
    assert get_buffer() is None

    buffer = init_buffer(tmp_path / "usage.json")
    assert get_buffer() is buffer

    reset_buffer()
    assert get_buffer() is None


@pytest.fixture
def _isolated_singleton(tmp_path):
    reset_buffer()
    yield
    reset_buffer()


def _sample_request() -> MessagesRequest:
    return MessagesRequest(
        model="gpt-4o",
        system="You are a helpful assistant.",
        messages=[
            Message(role="user", content="Hello there"),
            Message(
                role="assistant",
                content=[
                    {
                        "type": "text",
                        "text": "Hi! How can I help?",
                    }
                ],
            ),
            Message(
                role="user",
                content=[
                    {"type": "text", "text": "Summarize this"},
                    {"type": "image", "source": {"type": "base64", "data": "abc"}},
                    {
                        "type": "tool_use",
                        "id": "tool_1",
                        "name": "search",
                        "input": {"query": "usage"},
                    },
                    {
                        "type": "tool_result",
                        "tool_use_id": "tool_1",
                        "content": "results",
                    },
                ],
            ),
        ],
    )


def test_extract_prompt_includes_system_and_messages():
    prompt = extract_prompt(_sample_request())

    assert "You are a helpful assistant." in prompt
    assert "<user> Hello there" in prompt
    assert "<assistant> Hi! How can I help?" in prompt
    assert "Summarize this" in prompt
    assert "[image]" in prompt
    assert "[tool_use: search]" in prompt
    assert "[tool_result]" in prompt


def test_extract_prompt_caps_at_limit():
    long_text = "x" * (80 * 1024)
    request = MessagesRequest(
        model="gpt-4o",
        messages=[Message(role="user", content=long_text)],
    )

    prompt = extract_prompt(request)

    assert len(prompt.encode("utf-8")) <= 64 * 1024
    assert prompt.endswith("[truncated]")


def test_extract_prompt_handles_missing_optional_fields():
    request = MessagesRequest(
        model="gpt-4o",
        messages=[Message(role="user", content="simple")],
    )

    assert "<user> simple" in extract_prompt(request)


def test_usage_tracking_stream_captures_message_delta(tmp_path, _isolated_singleton):
    buffer = init_buffer(tmp_path / "usage.json")
    pending = PendingUsageRecord(
        request_id="req-1",
        started_at=time.time(),
        provider="openai_compatible_1",
        provider_model="gpt-4o",
        gateway_model="gpt-4o",
        wire_api="messages",
        input_tokens=10,
        prompt="prompt text",
    )

    async def inner():
        yield 'event: content_block_start\ndata: {"type":"content_block_start"}\n\n'
        yield (
            'event: message_delta\ndata: {"type":"message_delta","usage":'
            '{"input_tokens":10,"output_tokens":25,"cache_creation_input_tokens":5,'
            '"cache_read_input_tokens":3,"reasoning_tokens":2}}\n\n'
        )

    async def drive():
        stream = UsageTrackingStream(inner(), pending)
        return [chunk async for chunk in stream]

    chunks = asyncio.run(drive())

    assert len(chunks) == 2
    record = buffer.query()[0]
    assert record.request_id == "req-1"
    assert record.output_tokens == 25
    assert record.cache_creation_tokens == 5
    assert record.cache_read_tokens == 3
    assert record.reasoning_tokens == 2
    assert record.status == "success"


def test_usage_tracking_stream_marks_error(tmp_path, _isolated_singleton):
    buffer = init_buffer(tmp_path / "usage.json")
    pending = PendingUsageRecord(
        request_id="req-2",
        started_at=time.time(),
        provider="openai_compatible_1",
        provider_model="gpt-4o",
        gateway_model="gpt-4o",
        wire_api="messages",
        input_tokens=1,
        prompt="",
    )

    class Boom(RuntimeError):
        pass

    async def inner():
        yield 'event: message_delta\ndata: {"type":"message_delta"}\n\n'
        raise Boom("provider failed")

    async def drive():
        stream = UsageTrackingStream(inner(), pending)
        async for _ in stream:
            pass

    with pytest.raises(Boom):
        asyncio.run(drive())

    record = buffer.query()[0]
    assert record.status == "error"
    assert record.error_type == "Boom"


def test_usage_tracking_stream_marks_cancelled(tmp_path, _isolated_singleton):
    buffer = init_buffer(tmp_path / "usage.json")
    pending = PendingUsageRecord(
        request_id="req-3",
        started_at=time.time(),
        provider="openai_compatible_1",
        provider_model="gpt-4o",
        gateway_model="gpt-4o",
        wire_api="messages",
        input_tokens=1,
        prompt="",
    )

    async def inner():
        yield 'data: {"type":"message_start"}\n\n'
        raise asyncio.CancelledError()

    async def drive():
        stream = UsageTrackingStream(inner(), pending)
        async for _ in stream:
            pass

    with pytest.raises(asyncio.CancelledError):
        asyncio.run(drive())

    record = buffer.query()[0]
    assert record.status == "cancelled"


def test_usage_tracking_stream_aclose_releases_inner_and_records_cancelled(
    tmp_path, _isolated_singleton
):
    buffer = init_buffer(tmp_path / "usage.json")
    pending = PendingUsageRecord(
        request_id="req-close",
        started_at=time.time(),
        provider="p",
        provider_model="m",
        gateway_model="g",
        wire_api="messages",
        input_tokens=1,
        prompt="",
    )
    closed: list[bool] = []

    async def inner():
        try:
            yield 'data: {"type":"message_start"}\n\n'
        finally:
            closed.append(True)

    async def drive():
        stream = UsageTrackingStream(inner(), pending)
        # Consume a chunk so the inner generator is suspended at a yield, as
        # happens in the real executor flow, before closing it early.
        assert await anext(stream) == 'data: {"type":"message_start"}\n\n'
        await stream.aclose()

    asyncio.run(drive())

    assert closed == [True]
    record = buffer.query()[0]
    assert record.status == "cancelled"


def test_usage_tracking_stream_aclose_after_completion_is_idempotent(
    tmp_path, _isolated_singleton
):
    buffer = init_buffer(tmp_path / "usage.json")
    pending = PendingUsageRecord(
        request_id="req-close-after",
        started_at=time.time(),
        provider="p",
        provider_model="m",
        gateway_model="g",
        wire_api="messages",
        input_tokens=1,
        prompt="",
    )

    async def inner():
        yield (
            'event: message_delta\ndata: {"type":"message_delta",'
            '"usage":{"output_tokens":5}}\n\n'
        )

    async def drive():
        stream = UsageTrackingStream(inner(), pending)
        async for _ in stream:
            pass
        await stream.aclose()

    asyncio.run(drive())

    records = buffer.query()
    assert len(records) == 1
    assert records[0].status == "success"
    assert records[0].output_tokens == 5


def test_usage_tracking_stream_ignores_unrelated_events(tmp_path, _isolated_singleton):
    buffer = init_buffer(tmp_path / "usage.json")
    pending = PendingUsageRecord(
        request_id="req-4",
        started_at=time.time(),
        provider="p",
        provider_model="m",
        gateway_model="g",
        wire_api="messages",
        input_tokens=0,
        prompt="",
    )

    async def inner():
        yield 'data: {"type":"content_block_delta","delta":{"type":"text_delta"}}\n\n'

    async def drive():
        stream = UsageTrackingStream(inner(), pending)
        async for _ in stream:
            pass

    asyncio.run(drive())

    record = buffer.query()[0]
    assert record.output_tokens == 0
    assert record.status == "success"


def test_ring_buffer_records_ordering(tmp_path):
    buffer = UsageRingBuffer(tmp_path / "usage.json")
    buffer.push(_record("r1", timestamp=100.0))
    buffer.push(_record("r2", timestamp=200.0))

    assert tuple(r.request_id for r in buffer.records()) == ("r1", "r2")