-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cloud_tracing.py
More file actions
723 lines (557 loc) · 29.6 KB
/
test_cloud_tracing.py
File metadata and controls
723 lines (557 loc) · 29.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
"""Tests for sentience.cloud_tracing module"""
import gzip
import json
import os
import tempfile
import time
from pathlib import Path
from unittest.mock import MagicMock, Mock, patch
import pytest
from sentience.cloud_tracing import CloudTraceSink
from sentience.tracer_factory import create_tracer
from sentience.tracing import JsonlTraceSink, Tracer
class TestCloudTraceSink:
"""Test CloudTraceSink functionality."""
def test_cloud_trace_sink_upload_success(self):
"""Test CloudTraceSink successfully uploads trace to cloud."""
upload_url = "https://sentience.nyc3.digitaloceanspaces.com/user123/run456/trace.jsonl.gz"
run_id = "test-run-123"
with patch("sentience.cloud_tracing.requests.put") as mock_put:
# Mock successful response
mock_response = Mock()
mock_response.status_code = 200
mock_response.text = "Success"
mock_put.return_value = mock_response
# Create sink and emit events
sink = CloudTraceSink(upload_url, run_id=run_id)
sink.emit({"v": 1, "type": "run_start", "seq": 1, "data": {"agent": "TestAgent"}})
sink.emit({"v": 1, "type": "run_end", "seq": 2, "data": {"steps": 1}})
# Close triggers upload
sink.close()
# Verify request was made
assert mock_put.called
assert mock_put.call_count == 1
# Verify URL and headers
call_args = mock_put.call_args
assert call_args[0][0] == upload_url
assert call_args[1]["headers"]["Content-Type"] == "application/x-gzip"
assert call_args[1]["headers"]["Content-Encoding"] == "gzip"
# Verify body is gzip compressed
uploaded_data = call_args[1]["data"]
decompressed = gzip.decompress(uploaded_data)
lines = decompressed.decode("utf-8").strip().split("\n")
assert len(lines) == 2
event1 = json.loads(lines[0])
event2 = json.loads(lines[1])
assert event1["type"] == "run_start"
assert event2["type"] == "run_end"
# Verify file was deleted on successful upload
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
trace_path = cache_dir / f"{run_id}.jsonl"
assert not trace_path.exists(), "Trace file should be deleted after successful upload"
def test_cloud_trace_sink_upload_failure_preserves_trace(self, capsys):
"""Test CloudTraceSink preserves trace locally on upload failure."""
upload_url = "https://sentience.nyc3.digitaloceanspaces.com/user123/run456/trace.jsonl.gz"
run_id = "test-run-456"
with patch("sentience.cloud_tracing.requests.put") as mock_put:
# Mock failed response
mock_response = Mock()
mock_response.status_code = 500
mock_response.text = "Internal Server Error"
mock_put.return_value = mock_response
# Create sink and emit events
sink = CloudTraceSink(upload_url, run_id=run_id)
sink.emit({"v": 1, "type": "run_start", "seq": 1})
# Close triggers upload (which will fail)
sink.close()
# Verify error message printed
captured = capsys.readouterr()
assert "❌" in captured.out
assert "Upload failed: HTTP 500" in captured.out
assert "Local trace preserved" in captured.out
# Verify file was preserved on failure
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
trace_path = cache_dir / f"{run_id}.jsonl"
assert trace_path.exists(), "Trace file should be preserved on upload failure"
# Cleanup
if trace_path.exists():
os.remove(trace_path)
def test_cloud_trace_sink_emit_after_close_raises(self):
"""Test CloudTraceSink raises error when emitting after close."""
upload_url = "https://test.com/upload"
sink = CloudTraceSink(upload_url, run_id="test-run-789")
sink.close()
with pytest.raises(RuntimeError, match="CloudTraceSink is closed"):
sink.emit({"v": 1, "type": "test", "seq": 1})
def test_cloud_trace_sink_context_manager(self):
"""Test CloudTraceSink works as context manager."""
with patch("sentience.cloud_tracing.requests.put") as mock_put:
mock_put.return_value = Mock(status_code=200)
upload_url = "https://test.com/upload"
with CloudTraceSink(upload_url, run_id="test-run-context") as sink:
sink.emit({"v": 1, "type": "test", "seq": 1})
# Verify upload was called
assert mock_put.called
def test_cloud_trace_sink_network_error_graceful_degradation(self, capsys):
"""Test CloudTraceSink handles network errors gracefully."""
upload_url = "https://sentience.nyc3.digitaloceanspaces.com/user123/run456/trace.jsonl.gz"
run_id = "test-run-network-error"
with patch("sentience.cloud_tracing.requests.put") as mock_put:
# Simulate network error
mock_put.side_effect = Exception("Network error")
sink = CloudTraceSink(upload_url, run_id=run_id)
sink.emit({"v": 1, "type": "test", "seq": 1})
# Should not raise, just print warning
sink.close()
captured = capsys.readouterr()
assert "❌" in captured.out
assert "Error uploading trace" in captured.out
# Verify file was preserved
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
trace_path = cache_dir / f"{run_id}.jsonl"
assert trace_path.exists(), "Trace file should be preserved on network error"
# Cleanup
if trace_path.exists():
os.remove(trace_path)
def test_cloud_trace_sink_multiple_close_safe(self):
"""Test CloudTraceSink.close() is idempotent."""
with patch("sentience.cloud_tracing.requests.put") as mock_put:
mock_put.return_value = Mock(status_code=200)
upload_url = "https://test.com/upload"
sink = CloudTraceSink(upload_url, run_id="test-run-multiple-close")
sink.emit({"v": 1, "type": "test", "seq": 1})
# Close multiple times
sink.close()
sink.close()
sink.close()
# Upload should only be called once
assert mock_put.call_count == 1
def test_cloud_trace_sink_persistent_cache_directory(self):
"""Test CloudTraceSink uses persistent cache directory instead of temp file."""
upload_url = "https://test.com/upload"
run_id = "test-run-persistent"
sink = CloudTraceSink(upload_url, run_id=run_id)
sink.emit({"v": 1, "type": "test", "seq": 1})
# Verify file is in persistent cache directory
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
trace_path = cache_dir / f"{run_id}.jsonl"
assert trace_path.exists(), "Trace file should be in persistent cache directory"
assert cache_dir.exists(), "Cache directory should exist"
# Cleanup
sink.close()
if trace_path.exists():
os.remove(trace_path)
def test_cloud_trace_sink_non_blocking_close(self):
"""Test CloudTraceSink.close(blocking=False) returns immediately."""
upload_url = "https://test.com/upload"
run_id = "test-run-nonblocking"
with patch("sentience.cloud_tracing.requests.put") as mock_put:
mock_put.return_value = Mock(status_code=200)
sink = CloudTraceSink(upload_url, run_id=run_id)
sink.emit({"v": 1, "type": "test", "seq": 1})
# Non-blocking close should return immediately
start_time = time.time()
sink.close(blocking=False)
elapsed = time.time() - start_time
# Should return in < 0.1 seconds (much faster than upload)
assert elapsed < 0.1, "Non-blocking close should return immediately"
# Wait a bit for background thread to complete
time.sleep(0.5)
# Verify upload was called
assert mock_put.called
def test_cloud_trace_sink_progress_callback(self):
"""Test CloudTraceSink.close() with progress callback."""
upload_url = "https://test.com/upload"
run_id = "test-run-progress"
progress_calls = []
def progress_callback(uploaded: int, total: int):
progress_calls.append((uploaded, total))
with patch("sentience.cloud_tracing.requests.put") as mock_put:
mock_put.return_value = Mock(status_code=200)
sink = CloudTraceSink(upload_url, run_id=run_id)
sink.emit({"v": 1, "type": "test", "seq": 1})
sink.close(blocking=True, on_progress=progress_callback)
# Verify progress callback was called
assert len(progress_calls) > 0, "Progress callback should be called"
# Last call should have uploaded == total
assert progress_calls[-1][0] == progress_calls[-1][1], "Final progress should be 100%"
class TestTracerFactory:
"""Test create_tracer factory function."""
def test_create_tracer_pro_tier_success(self, capsys):
"""Test create_tracer returns CloudTraceSink for Pro tier."""
with patch("sentience.tracer_factory.requests.post") as mock_post:
with patch("sentience.cloud_tracing.requests.put") as mock_put:
# Mock API response
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"upload_url": "https://sentience.nyc3.digitaloceanspaces.com/upload"
}
mock_post.return_value = mock_response
# Mock upload response
mock_put.return_value = Mock(status_code=200)
tracer = create_tracer(
api_key="sk_pro_test123", run_id="test-run", upload_trace=True
)
# Verify Pro tier message
captured = capsys.readouterr()
assert "☁️ [Sentience] Cloud tracing enabled (Pro tier)" in captured.out
# Verify tracer works
assert tracer.run_id == "test-run"
assert isinstance(tracer.sink, CloudTraceSink)
assert tracer.sink.run_id == "test-run" # Verify run_id is passed
# Cleanup
tracer.close()
def test_create_tracer_free_tier_fallback(self, capsys):
"""Test create_tracer falls back to local for free tier."""
with tempfile.TemporaryDirectory():
tracer = create_tracer(run_id="test-run")
# Verify local tracing message
captured = capsys.readouterr()
assert "💾 [Sentience] Local tracing:" in captured.out
# Use os.path.join for platform-independent path checking
import os
expected_path = os.path.join("traces", "test-run.jsonl")
assert expected_path in captured.out
# Verify tracer works
assert tracer.run_id == "test-run"
assert isinstance(tracer.sink, JsonlTraceSink)
# Cleanup
tracer.close()
def test_create_tracer_api_forbidden_fallback(self, capsys):
"""Test create_tracer falls back when API returns 403 Forbidden."""
with patch("sentience.tracer_factory.requests.post") as mock_post:
# Mock API response with 403
mock_response = Mock()
mock_response.status_code = 403
mock_post.return_value = mock_response
with tempfile.TemporaryDirectory():
tracer = create_tracer(
api_key="sk_free_test123", run_id="test-run", upload_trace=True
)
# Verify warning message
captured = capsys.readouterr()
assert "⚠️ [Sentience] Cloud tracing requires Pro tier" in captured.out
assert "Falling back to local-only tracing" in captured.out
# Verify fallback to local
assert isinstance(tracer.sink, JsonlTraceSink)
tracer.close()
def test_create_tracer_api_timeout_fallback(self, capsys):
"""Test create_tracer falls back on timeout."""
import requests
with patch("sentience.tracer_factory.requests.post") as mock_post:
# Mock timeout
mock_post.side_effect = requests.exceptions.Timeout("Connection timeout")
with tempfile.TemporaryDirectory():
tracer = create_tracer(api_key="sk_test123", run_id="test-run", upload_trace=True)
# Verify warning message
captured = capsys.readouterr()
assert "⚠️ [Sentience] Cloud init timeout" in captured.out
assert "Falling back to local-only tracing" in captured.out
# Verify fallback to local
assert isinstance(tracer.sink, JsonlTraceSink)
tracer.close()
def test_create_tracer_api_connection_error_fallback(self, capsys):
"""Test create_tracer falls back on connection error."""
import requests
with patch("sentience.tracer_factory.requests.post") as mock_post:
# Mock connection error
mock_post.side_effect = requests.exceptions.ConnectionError("Connection refused")
with tempfile.TemporaryDirectory():
tracer = create_tracer(api_key="sk_test123", run_id="test-run", upload_trace=True)
# Verify warning message
captured = capsys.readouterr()
assert "⚠️ [Sentience] Cloud init connection error" in captured.out
# Verify fallback to local
assert isinstance(tracer.sink, JsonlTraceSink)
tracer.close()
def test_create_tracer_generates_run_id_if_not_provided(self):
"""Test create_tracer generates UUID if run_id not provided."""
with tempfile.TemporaryDirectory():
tracer = create_tracer()
# Verify run_id was generated
assert tracer.run_id is not None
assert len(tracer.run_id) == 36 # UUID format
tracer.close()
def test_create_tracer_uses_constant_api_url(self):
"""Test create_tracer uses constant SENTIENCE_API_URL."""
from sentience.tracer_factory import SENTIENCE_API_URL
with patch("sentience.tracer_factory.requests.post") as mock_post:
with patch("sentience.cloud_tracing.requests.put") as mock_put:
# Mock API response
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"upload_url": "https://storage.com/upload"}
mock_post.return_value = mock_response
mock_put.return_value = Mock(status_code=200)
tracer = create_tracer(api_key="sk_test123", run_id="test-run", upload_trace=True)
# Verify correct API URL was used (constant)
assert mock_post.called
call_args = mock_post.call_args
assert call_args[0][0] == f"{SENTIENCE_API_URL}/v1/traces/init"
assert SENTIENCE_API_URL == "https://api.sentienceapi.com"
tracer.close()
def test_create_tracer_custom_api_url(self):
"""Test create_tracer accepts custom api_url parameter."""
custom_api_url = "https://custom.api.example.com"
with patch("sentience.tracer_factory.requests.post") as mock_post:
with patch("sentience.cloud_tracing.requests.put") as mock_put:
# Mock API response
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"upload_url": "https://storage.com/upload"}
mock_post.return_value = mock_response
mock_put.return_value = Mock(status_code=200)
tracer = create_tracer(
api_key="sk_test123",
run_id="test-run",
api_url=custom_api_url,
upload_trace=True,
)
# Verify custom API URL was used
assert mock_post.called
call_args = mock_post.call_args
assert call_args[0][0] == f"{custom_api_url}/v1/traces/init"
tracer.close()
def test_create_tracer_missing_upload_url_in_response(self, capsys):
"""Test create_tracer handles missing upload_url gracefully."""
with patch("sentience.tracer_factory.requests.post") as mock_post:
# Mock API response without upload_url
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"message": "Success"} # Missing upload_url
mock_post.return_value = mock_response
with tempfile.TemporaryDirectory():
tracer = create_tracer(api_key="sk_test123", run_id="test-run", upload_trace=True)
# Verify warning message
captured = capsys.readouterr()
assert "⚠️ [Sentience] Cloud init response missing upload_url" in captured.out
# Verify fallback to local
assert isinstance(tracer.sink, JsonlTraceSink)
tracer.close()
def test_create_tracer_orphaned_trace_recovery(self, capsys):
"""Test create_tracer recovers and uploads orphaned traces from previous crashes."""
import gzip
from pathlib import Path
# Create orphaned trace file
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
cache_dir.mkdir(parents=True, exist_ok=True)
orphaned_run_id = "orphaned-run-123"
orphaned_path = cache_dir / f"{orphaned_run_id}.jsonl"
# Write test trace data
with open(orphaned_path, "w") as f:
f.write('{"v": 1, "type": "run_start", "seq": 1}\n')
try:
with patch("sentience.tracer_factory.requests.post") as mock_post:
with patch("sentience.tracer_factory.requests.put") as mock_put:
# Mock API response for orphaned trace recovery
mock_recovery_response = Mock()
mock_recovery_response.status_code = 200
mock_recovery_response.json.return_value = {
"upload_url": "https://storage.com/orphaned-upload"
}
# Mock API response for new tracer creation
mock_new_response = Mock()
mock_new_response.status_code = 200
mock_new_response.json.return_value = {
"upload_url": "https://storage.com/new-upload"
}
# First call for orphaned recovery, second for new tracer
mock_post.side_effect = [mock_recovery_response, mock_new_response]
mock_put.return_value = Mock(status_code=200)
# Create tracer - should trigger orphaned trace recovery
tracer = create_tracer(
api_key="sk_test123", run_id="new-run-456", upload_trace=True
)
# Verify recovery messages
captured = capsys.readouterr()
assert "Found" in captured.out and "un-uploaded trace" in captured.out
assert "Uploaded orphaned trace" in captured.out or "Failed" in captured.out
# Verify orphaned file was processed (either uploaded and deleted, or failed)
# If successful, file should be deleted
# If failed, file should still exist
# We check that recovery was attempted
assert mock_post.call_count >= 1, "Orphaned trace recovery should be attempted"
# Verify new tracer was created
assert tracer.run_id == "new-run-456"
tracer.close()
finally:
# Cleanup orphaned file if it still exists
if orphaned_path.exists():
os.remove(orphaned_path)
class TestRegressionTests:
"""Regression tests to ensure cloud tracing doesn't break existing functionality."""
def test_local_tracing_still_works(self):
"""Test existing JsonlTraceSink functionality unchanged."""
with tempfile.TemporaryDirectory() as tmpdir:
trace_path = Path(tmpdir) / "trace.jsonl"
with JsonlTraceSink(trace_path) as sink:
tracer = Tracer(run_id="test-run", sink=sink)
tracer.emit_run_start("TestAgent", "gpt-4")
tracer.emit_run_end(1)
# Verify trace file created
assert trace_path.exists()
lines = trace_path.read_text().strip().split("\n")
assert len(lines) == 2
event1 = json.loads(lines[0])
assert event1["type"] == "run_start"
def test_tracer_api_unchanged(self):
"""Test Tracer API hasn't changed."""
with tempfile.TemporaryDirectory() as tmpdir:
trace_path = Path(tmpdir) / "trace.jsonl"
sink = JsonlTraceSink(trace_path)
# All existing methods should still work
tracer = Tracer(run_id="test-run", sink=sink)
tracer.emit("custom_event", {"data": "value"})
tracer.emit_run_start("TestAgent")
tracer.emit_step_start("step-1", 1, "Test goal")
tracer.emit_error("step-1", "Test error")
tracer.emit_run_end(1)
tracer.close()
# Verify all events written
lines = trace_path.read_text().strip().split("\n")
assert len(lines) == 5
def test_cloud_trace_sink_index_upload_success(self):
"""Test CloudTraceSink uploads index file after trace upload."""
upload_url = "https://sentience.nyc3.digitaloceanspaces.com/traces/test.jsonl.gz"
run_id = "test-index-upload"
with patch("sentience.cloud_tracing.requests.put") as mock_put, \
patch("sentience.cloud_tracing.requests.post") as mock_post:
# Mock successful trace upload
trace_response = Mock()
trace_response.status_code = 200
# Mock successful index upload URL request
index_url_response = Mock()
index_url_response.status_code = 200
index_url_response.json.return_value = {
"upload_url": "https://sentience.nyc3.digitaloceanspaces.com/traces/test.index.json.gz"
}
# Mock successful /v1/traces/complete response
complete_response = Mock()
complete_response.status_code = 200
# Mock successful index upload
index_upload_response = Mock()
index_upload_response.status_code = 200
mock_put.side_effect = [trace_response, index_upload_response]
# POST is called twice: once for index_upload, once for complete
mock_post.side_effect = [index_url_response, complete_response]
# Create sink and emit events
sink = CloudTraceSink(upload_url, run_id=run_id, api_key="sk_test_123")
sink.emit({"v": 1, "type": "run_start", "seq": 1, "data": {"agent": "TestAgent"}})
sink.emit({"v": 1, "type": "step_start", "seq": 2, "data": {"step": 1}})
sink.emit({"v": 1, "type": "snapshot", "seq": 3, "data": {"url": "https://example.com"}})
sink.emit({"v": 1, "type": "run_end", "seq": 4, "data": {"steps": 1}})
# Close triggers upload
sink.close()
# Verify trace upload
assert mock_put.call_count == 2 # Once for trace, once for index
# Verify index upload URL request (first POST call)
assert mock_post.called
assert mock_post.call_count == 2 # index_upload + complete
# Check first POST call (index_upload)
first_post_call = mock_post.call_args_list[0]
assert "/v1/traces/index_upload" in first_post_call[0][0]
assert first_post_call[1]["json"] == {"run_id": run_id}
# Verify index file upload
index_call = mock_put.call_args_list[1]
assert "index.json.gz" in index_call[0][0]
assert index_call[1]["headers"]["Content-Type"] == "application/json"
assert index_call[1]["headers"]["Content-Encoding"] == "gzip"
# Cleanup
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
index_path = cache_dir / f"{run_id}.index.json"
if index_path.exists():
os.remove(index_path)
def test_cloud_trace_sink_index_upload_no_api_key(self):
"""Test CloudTraceSink skips index upload when no API key provided."""
upload_url = "https://sentience.nyc3.digitaloceanspaces.com/traces/test.jsonl.gz"
run_id = "test-no-api-key"
with patch("sentience.cloud_tracing.requests.put") as mock_put, \
patch("sentience.cloud_tracing.requests.post") as mock_post:
# Mock successful trace upload
mock_put.return_value = Mock(status_code=200)
# Create sink WITHOUT api_key
sink = CloudTraceSink(upload_url, run_id=run_id)
sink.emit({"v": 1, "type": "run_start", "seq": 1})
sink.close()
# Verify trace upload happened
assert mock_put.called
# Verify index upload was NOT attempted (no API key)
assert not mock_post.called
# Cleanup
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
trace_path = cache_dir / f"{run_id}.jsonl"
index_path = cache_dir / f"{run_id}.index.json"
if trace_path.exists():
os.remove(trace_path)
if index_path.exists():
os.remove(index_path)
def test_cloud_trace_sink_index_upload_failure_non_fatal(self, capsys):
"""Test CloudTraceSink continues gracefully if index upload fails."""
upload_url = "https://sentience.nyc3.digitaloceanspaces.com/traces/test.jsonl.gz"
run_id = "test-index-fail"
with patch("sentience.cloud_tracing.requests.put") as mock_put, \
patch("sentience.cloud_tracing.requests.post") as mock_post:
# Mock successful trace upload
trace_response = Mock()
trace_response.status_code = 200
# Mock failed index upload URL request
index_url_response = Mock()
index_url_response.status_code = 500
mock_put.return_value = trace_response
mock_post.return_value = index_url_response
# Create sink
sink = CloudTraceSink(upload_url, run_id=run_id, api_key="sk_test_123")
sink.emit({"v": 1, "type": "run_start", "seq": 1})
# Close should succeed even if index upload fails
sink.close()
# Verify trace upload succeeded
assert mock_put.called
# Verify warning was printed
captured = capsys.readouterr()
# Index upload failure is non-fatal, so main upload should succeed
assert "✅" in captured.out # Trace upload success
# Cleanup
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
trace_path = cache_dir / f"{run_id}.jsonl"
index_path = cache_dir / f"{run_id}.index.json"
if trace_path.exists():
os.remove(trace_path)
if index_path.exists():
os.remove(index_path)
def test_cloud_trace_sink_index_file_missing(self, capsys):
"""Test CloudTraceSink handles missing index file gracefully."""
upload_url = "https://sentience.nyc3.digitaloceanspaces.com/traces/test.jsonl.gz"
run_id = "test-missing-index"
with patch("sentience.cloud_tracing.requests.put") as mock_put, \
patch("sentience.cloud_tracing.requests.post") as mock_post, \
patch("sentience.trace_indexing.write_trace_index") as mock_write_index:
# Mock index generation to fail (simulating missing index)
mock_write_index.side_effect = Exception("Index generation failed")
# Mock successful trace upload
mock_put.return_value = Mock(status_code=200)
# Mock /v1/traces/complete response (this will still be called)
complete_response = Mock()
complete_response.status_code = 200
mock_post.return_value = complete_response
# Create sink
sink = CloudTraceSink(upload_url, run_id=run_id, api_key="sk_test_123")
sink.emit({"v": 1, "type": "run_start", "seq": 1})
# Close should succeed even if index generation fails
sink.close()
# Verify trace upload succeeded
assert mock_put.called
# POST is called once for /v1/traces/complete, but NOT for /v1/traces/index_upload
# (because index file is missing)
assert mock_post.call_count == 1
# Verify it was the complete call, not index_upload
assert "/v1/traces/complete" in mock_post.call_args[0][0]
# Verify warning was printed
captured = capsys.readouterr()
assert "⚠️" in captured.out
assert "Failed to generate trace index" in captured.out
# Cleanup
cache_dir = Path.home() / ".sentience" / "traces" / "pending"
trace_path = cache_dir / f"{run_id}.jsonl"
if trace_path.exists():
os.remove(trace_path)