File size: 22,568 Bytes
116524e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for the Claude SDK integration step."""

from __future__ import annotations

import logging
from dataclasses import dataclass
from types import SimpleNamespace
from typing import Any, List, Optional
from unittest.mock import MagicMock, patch

import pytest
from pydantic import ValidationError

from ace.core.context import ACEStepContext, SkillbookView
from ace.core.skillbook import Skillbook
from ace.integrations.claude_sdk import (
    ClaudeSDKExecuteStep,
    ClaudeSDKResult,
    ClaudeSDKToTrace,
    ToolCall,
)

# ------------------------------------------------------------------ #
# Helpers — mock Anthropic API objects
# ------------------------------------------------------------------ #


def _make_text_block(text: str) -> SimpleNamespace:
    return SimpleNamespace(type="text", text=text)


def _make_tool_block(

    tool_id: str = "toolu_01", name: str = "calculator", inp: Any = None

) -> SimpleNamespace:
    return SimpleNamespace(type="tool_use", id=tool_id, name=name, input=inp or {})


def _make_usage(input_tokens: int = 100, output_tokens: int = 50) -> SimpleNamespace:
    return SimpleNamespace(input_tokens=input_tokens, output_tokens=output_tokens)


def _make_response(

    content: list | None = None,

    model: str = "claude-sonnet-4-20250514",

    stop_reason: str = "end_turn",

    input_tokens: int = 100,

    output_tokens: int = 50,

) -> SimpleNamespace:
    if content is None:
        content = [_make_text_block("Hello, world!")]
    return SimpleNamespace(
        content=content,
        model=model,
        stop_reason=stop_reason,
        usage=_make_usage(input_tokens, output_tokens),
    )


def _make_mock_client(response: SimpleNamespace | None = None) -> MagicMock:
    client = MagicMock()
    client.messages.create.return_value = response or _make_response()
    return client


@dataclass
class FakeSample:
    question: str = "What is 2+2?"
    context: str = "math quiz"
    ground_truth: str = "4"
    metadata: dict = None  # type: ignore[assignment]

    def __post_init__(self) -> None:
        if self.metadata is None:
            self.metadata = {}


# ------------------------------------------------------------------ #
# ClaudeSDKResult
# ------------------------------------------------------------------ #


class TestClaudeSDKResult:
    def test_defaults(self):
        r = ClaudeSDKResult(task="test", success=True)
        assert r.task == "test"
        assert r.success is True
        assert r.output == ""
        assert r.error is None
        assert r.input_tokens == 0
        assert r.output_tokens == 0
        assert r.total_tokens == 0
        assert r.latency_seconds == 0.0
        assert r.tool_calls == []
        assert r.cited_skill_ids == []
        assert r.raw_response is None

    def test_full(self):
        tc = ToolCall(id="toolu_01", name="calc", input={"expr": "1+1"})
        r = ClaudeSDKResult(
            task="hello",
            success=True,
            output="world",
            model="claude-sonnet-4-20250514",
            stop_reason="end_turn",
            input_tokens=100,
            output_tokens=50,
            total_tokens=150,
            latency_seconds=1.5,
            tool_calls=[tc],
            cited_skill_ids=["math-001"],
        )
        assert r.total_tokens == 150
        assert r.tool_calls[0].name == "calc"

    def test_auto_compute_total_tokens(self):
        """total_tokens is auto-computed from input + output when left at 0."""
        r = ClaudeSDKResult(
            task="test", success=True, input_tokens=100, output_tokens=50
        )
        assert r.total_tokens == 150

    def test_explicit_total_not_overwritten(self):
        """An explicitly set total_tokens is preserved."""
        r = ClaudeSDKResult(
            task="test",
            success=True,
            input_tokens=100,
            output_tokens=50,
            total_tokens=999,
        )
        assert r.total_tokens == 999

    def test_negative_tokens_rejected(self):
        """Negative token counts should be rejected by validation."""
        with pytest.raises(Exception):
            ClaudeSDKResult(task="test", success=True, input_tokens=-1)

    def test_negative_latency_rejected(self):
        """Negative latency should be rejected by validation."""
        with pytest.raises(Exception):
            ClaudeSDKResult(task="test", success=True, latency_seconds=-0.1)

    def test_serialization(self):
        """Result should serialise to dict/JSON (raw_response excluded)."""
        r = ClaudeSDKResult(
            task="test",
            success=True,
            input_tokens=10,
            output_tokens=5,
            raw_response=object(),
        )
        d = r.model_dump()
        assert d["task"] == "test"
        assert "raw_response" not in d  # excluded

    def test_tool_call_validation(self):
        """ToolCall should validate required fields."""
        tc = ToolCall(id="toolu_01", name="calc")
        assert tc.input == {}

        with pytest.raises(Exception):
            ToolCall(name="calc")  # missing id


# ------------------------------------------------------------------ #
# ClaudeSDKExecuteStep — contracts
# ------------------------------------------------------------------ #


class TestClaudeSDKExecuteStepContracts:
    def test_requires_and_provides(self):
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(client=client)
        assert "sample" in step.requires
        assert "skillbook" in step.requires
        assert "trace" in step.provides

    def test_not_available_raises_without_client(self):
        with patch("ace.integrations.claude_sdk.ANTHROPIC_SDK_AVAILABLE", False):
            with pytest.raises(ImportError, match="anthropic SDK not installed"):
                ClaudeSDKExecuteStep()

    def test_injected_client_skips_availability_check(self):
        with patch("ace.integrations.claude_sdk.ANTHROPIC_SDK_AVAILABLE", False):
            step = ClaudeSDKExecuteStep(client=_make_mock_client())
            assert "sample" in step.requires

    def test_invalid_max_tokens_rejected(self):
        with pytest.raises(ValidationError):
            ClaudeSDKExecuteStep(client=_make_mock_client(), max_tokens=0)

    def test_invalid_temperature_rejected(self):
        with pytest.raises(ValidationError):
            ClaudeSDKExecuteStep(client=_make_mock_client(), temperature=1.5)


# ------------------------------------------------------------------ #
# ClaudeSDKExecuteStep — execution
# ------------------------------------------------------------------ #


class TestClaudeSDKExecuteStepExecution:
    def test_basic_call(self):
        response = _make_response(
            content=[_make_text_block("The answer is 4")],
            input_tokens=80,
            output_tokens=20,
        )
        client = _make_mock_client(response)
        step = ClaudeSDKExecuteStep(client=client, model="claude-sonnet-4-20250514")

        ctx = ACEStepContext(sample="What is 2+2?", skillbook=None)
        result_ctx = step(ctx)

        r: ClaudeSDKResult = result_ctx.trace  # type: ignore[assignment]
        assert r.success is True
        assert r.output == "The answer is 4"
        assert r.input_tokens == 80
        assert r.output_tokens == 20
        assert r.total_tokens == 100
        assert r.latency_seconds >= 0
        assert r.model == "claude-sonnet-4-20250514"
        assert r.stop_reason == "end_turn"

        # Verify API was called correctly
        call_kwargs = client.messages.create.call_args[1]
        assert call_kwargs["model"] == "claude-sonnet-4-20250514"
        assert call_kwargs["messages"] == [{"role": "user", "content": "What is 2+2?"}]

    def test_with_system_prompt(self):
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(
            client=client,
            system_prompt="You are a math tutor.",
        )

        ctx = ACEStepContext(sample="What is 2+2?", skillbook=None)
        step(ctx)

        call_kwargs = client.messages.create.call_args[1]
        assert call_kwargs["system"] == "You are a math tutor."

    def test_skillbook_injection(self):
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(client=client, inject_skillbook=True)

        sb = Skillbook()
        sb.add_skill("math", "Always show your work")
        ctx = ACEStepContext(sample="What is 2+2?", skillbook=SkillbookView(sb))
        step(ctx)

        call_kwargs = client.messages.create.call_args[1]
        assert "system" in call_kwargs
        assert "Strategic Knowledge" in call_kwargs["system"]

    def test_skillbook_injection_with_system_prompt(self):
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(
            client=client,
            system_prompt="You are a tutor.",
            inject_skillbook=True,
        )

        sb = Skillbook()
        sb.add_skill("math", "Show work")
        ctx = ACEStepContext(sample="test", skillbook=SkillbookView(sb))
        step(ctx)

        call_kwargs = client.messages.create.call_args[1]
        system = call_kwargs["system"]
        assert "You are a tutor." in system
        assert "Strategic Knowledge" in system

    def test_skillbook_injection_disabled(self):
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(client=client, inject_skillbook=False)

        sb = Skillbook()
        sb.add_skill("math", "Show work")
        ctx = ACEStepContext(sample="test", skillbook=SkillbookView(sb))
        step(ctx)

        call_kwargs = client.messages.create.call_args[1]
        assert "system" not in call_kwargs

    def test_empty_skillbook_no_system(self):
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(client=client, inject_skillbook=True)

        sb = Skillbook()
        ctx = ACEStepContext(sample="test", skillbook=SkillbookView(sb))
        step(ctx)

        call_kwargs = client.messages.create.call_args[1]
        assert "system" not in call_kwargs

    def test_with_tools(self):
        tools = [
            {
                "name": "calculator",
                "description": "A calculator",
                "input_schema": {
                    "type": "object",
                    "properties": {"expr": {"type": "string"}},
                },
            }
        ]
        response = _make_response(
            content=[
                _make_tool_block("toolu_01", "calculator", {"expr": "2+2"}),
                _make_text_block("The result is 4"),
            ]
        )
        client = _make_mock_client(response)
        step = ClaudeSDKExecuteStep(client=client, tools=tools)

        ctx = ACEStepContext(sample="Calculate 2+2", skillbook=None)
        result_ctx = step(ctx)

        r: ClaudeSDKResult = result_ctx.trace  # type: ignore[assignment]
        assert r.success is True
        assert r.output == "The result is 4"
        assert len(r.tool_calls) == 1
        assert r.tool_calls[0].name == "calculator"
        assert r.tool_calls[0].input == {"expr": "2+2"}

        call_kwargs = client.messages.create.call_args[1]
        assert call_kwargs["tools"] == tools

    def test_api_error_handled(self):
        client = _make_mock_client()
        client.messages.create.side_effect = RuntimeError("API down")
        step = ClaudeSDKExecuteStep(client=client)

        ctx = ACEStepContext(sample="test", skillbook=None)
        result_ctx = step(ctx)

        r: ClaudeSDKResult = result_ctx.trace  # type: ignore[assignment]
        assert r.success is False
        assert "API down" in r.error
        assert r.latency_seconds >= 0

    def test_temperature_and_max_tokens(self):
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(client=client, temperature=0.7, max_tokens=1024)

        ctx = ACEStepContext(sample="test", skillbook=None)
        step(ctx)

        call_kwargs = client.messages.create.call_args[1]
        assert call_kwargs["temperature"] == 0.7
        assert call_kwargs["max_tokens"] == 1024


# ------------------------------------------------------------------ #
# ClaudeSDKExecuteStep — task extraction
# ------------------------------------------------------------------ #


class TestClaudeSDKTaskExtraction:
    def test_string_sample(self):
        assert ClaudeSDKExecuteStep._extract_task("hello") == "hello"

    def test_sample_with_question(self):
        sample = FakeSample(question="What is 2+2?", context="")
        result = ClaudeSDKExecuteStep._extract_task(sample)
        assert result == "What is 2+2?"

    def test_sample_with_context(self):
        sample = FakeSample(question="What is 2+2?", context="math quiz")
        result = ClaudeSDKExecuteStep._extract_task(sample)
        assert "What is 2+2?" in result
        assert "Context: math quiz" in result

    def test_arbitrary_object(self):
        result = ClaudeSDKExecuteStep._extract_task(42)
        assert result == "42"


# ------------------------------------------------------------------ #
# ClaudeSDKExecuteStep — skill ID extraction
# ------------------------------------------------------------------ #


class TestClaudeSDKSkillExtraction:
    def test_extracts_skill_ids(self):
        response = _make_response(
            content=[
                _make_text_block(
                    "Following [math-00001], the answer is 4. "
                    "Also [general-00042] applies."
                )
            ]
        )
        client = _make_mock_client(response)
        step = ClaudeSDKExecuteStep(client=client)

        ctx = ACEStepContext(sample="test", skillbook=None)
        result_ctx = step(ctx)

        r: ClaudeSDKResult = result_ctx.trace  # type: ignore[assignment]
        assert "math-00001" in r.cited_skill_ids
        assert "general-00042" in r.cited_skill_ids

    def test_no_skill_ids(self):
        response = _make_response(content=[_make_text_block("No citations here")])
        client = _make_mock_client(response)
        step = ClaudeSDKExecuteStep(client=client)

        ctx = ACEStepContext(sample="test", skillbook=None)
        result_ctx = step(ctx)

        r: ClaudeSDKResult = result_ctx.trace  # type: ignore[assignment]
        assert r.cited_skill_ids == []


# ------------------------------------------------------------------ #
# ClaudeSDKExecuteStep — observability logging
# ------------------------------------------------------------------ #


class TestClaudeSDKObservability:
    def test_auto_instruments_anthropic_when_logfire_configured(self):
        mock_logfire = MagicMock()
        mock_ctx = MagicMock()
        mock_logfire.instrument_anthropic.return_value = mock_ctx

        with (
            patch("ace.observability.is_configured", return_value=True),
            patch.dict("sys.modules", {"logfire": mock_logfire}),
        ):
            step = ClaudeSDKExecuteStep(client=_make_mock_client())

        mock_logfire.instrument_anthropic.assert_called_once_with(step._client)
        mock_ctx.__enter__.assert_not_called()

    def test_logs_metrics(self, caplog):
        response = _make_response(input_tokens=200, output_tokens=100)
        client = _make_mock_client(response)
        step = ClaudeSDKExecuteStep(client=client)

        with caplog.at_level(logging.INFO, logger="ace.integrations.claude_sdk"):
            ctx = ACEStepContext(sample="test", skillbook=None)
            step(ctx)

        assert "ClaudeSDK:" in caplog.text
        assert "tokens=" in caplog.text

    def test_logs_error(self, caplog):
        client = _make_mock_client()
        client.messages.create.side_effect = RuntimeError("boom")
        step = ClaudeSDKExecuteStep(client=client)

        with caplog.at_level(logging.ERROR, logger="ace.integrations.claude_sdk"):
            ctx = ACEStepContext(sample="test", skillbook=None)
            step(ctx)

        assert "failed" in caplog.text.lower()

    def test_logfire_span_on_success(self):
        """When Logfire is configured, __call__ opens a span with attributes."""
        mock_span = MagicMock()
        mock_logfire = MagicMock()
        mock_logfire.span.return_value.__enter__ = MagicMock(return_value=mock_span)
        mock_logfire.span.return_value.__exit__ = MagicMock(return_value=False)

        response = _make_response(input_tokens=50, output_tokens=25)
        client = _make_mock_client(response)
        step = ClaudeSDKExecuteStep(client=client)

        with (
            patch(
                "ace.integrations.claude_sdk._get_logfire", return_value=mock_logfire
            ),
        ):
            ctx = ACEStepContext(sample="What is 2+2?", skillbook=None)
            result_ctx = step(ctx)

        # Span was opened
        mock_logfire.span.assert_called_once()
        call_kwargs = mock_logfire.span.call_args
        assert call_kwargs[0][0] == "ClaudeSDKExecuteStep"
        assert call_kwargs[1]["model"] == "claude-sonnet-4-20250514"

        # Attributes were set on the span
        attr_calls = {c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list}
        assert attr_calls["success"] is True
        assert attr_calls["input_tokens"] == 50
        assert attr_calls["output_tokens"] == 25
        assert attr_calls["total_tokens"] == 75
        assert "error" not in attr_calls

        # logfire.info was called with metrics
        mock_logfire.info.assert_called_once()
        info_kwargs = mock_logfire.info.call_args[1]
        assert info_kwargs["input_tokens"] == 50
        assert info_kwargs["output_tokens"] == 25

    def test_logfire_span_on_failure(self):
        """On API error, span captures error attribute and logfire.error is called."""
        mock_span = MagicMock()
        mock_logfire = MagicMock()
        mock_logfire.span.return_value.__enter__ = MagicMock(return_value=mock_span)
        mock_logfire.span.return_value.__exit__ = MagicMock(return_value=False)

        client = _make_mock_client()
        client.messages.create.side_effect = RuntimeError("rate limited")
        step = ClaudeSDKExecuteStep(client=client)

        with (
            patch(
                "ace.integrations.claude_sdk._get_logfire", return_value=mock_logfire
            ),
        ):
            ctx = ACEStepContext(sample="test", skillbook=None)
            step(ctx)

        # Span captured the error attribute
        attr_calls = {c[0][0]: c[0][1] for c in mock_span.set_attribute.call_args_list}
        assert attr_calls["success"] is False
        assert "rate limited" in attr_calls["error"]

        # logfire.error was called
        mock_logfire.error.assert_called_once()
        error_kwargs = mock_logfire.error.call_args[1]
        assert "rate limited" in error_kwargs["error"]

    def test_no_logfire_noop(self):
        """When Logfire is not configured, execution proceeds without spans."""
        client = _make_mock_client()
        step = ClaudeSDKExecuteStep(client=client)

        with patch("ace.integrations.claude_sdk._get_logfire", return_value=None):
            ctx = ACEStepContext(sample="test", skillbook=None)
            result_ctx = step(ctx)

        r: ClaudeSDKResult = result_ctx.trace  # type: ignore[assignment]
        assert r.success is True


# ------------------------------------------------------------------ #
# ClaudeSDKToTrace
# ------------------------------------------------------------------ #


class TestClaudeSDKToTrace:
    def test_requires_and_provides(self):
        step = ClaudeSDKToTrace()
        assert "trace" in step.requires
        assert "trace" in step.provides

    def test_success_trace(self):
        r = ClaudeSDKResult(
            task="What is 2+2?",
            success=True,
            output="4",
            model="claude-sonnet-4-20250514",
            stop_reason="end_turn",
            input_tokens=100,
            output_tokens=50,
            total_tokens=150,
            latency_seconds=1.2,
            cited_skill_ids=["math-001"],
        )
        ctx = ACEStepContext(trace=r)
        result_ctx = ClaudeSDKToTrace()(ctx)

        trace = result_ctx.trace
        assert trace["question"] == "What is 2+2?"
        assert trace["answer"] == "4"
        assert trace["skill_ids"] == ["math-001"]
        assert "succeeded" in trace["reasoning"]
        assert "claude-sonnet-4-20250514" in trace["reasoning"]
        assert "100" in trace["reasoning"]  # input tokens
        assert "50" in trace["reasoning"]  # output tokens
        assert "1.2" in trace["reasoning"]  # latency
        assert "succeeded" in trace["feedback"]
        assert trace["ground_truth"] is None

    def test_failure_trace(self):
        r = ClaudeSDKResult(
            task="fail",
            success=False,
            error="API timeout",
            model="claude-sonnet-4-20250514",
            latency_seconds=30.0,
        )
        ctx = ACEStepContext(trace=r)
        result_ctx = ClaudeSDKToTrace()(ctx)

        trace = result_ctx.trace
        assert trace["question"] == "fail"
        assert trace["answer"] == ""
        assert "failed" in trace["reasoning"]
        assert "API timeout" in trace["reasoning"]
        assert "failed" in trace["feedback"]
        assert "API timeout" in trace["feedback"]

    def test_tool_calls_in_reasoning(self):
        r = ClaudeSDKResult(
            task="calc",
            success=True,
            output="4",
            model="claude-sonnet-4-20250514",
            tool_calls=[
                ToolCall(id="toolu_01", name="calculator", input={"expr": "2+2"}),
                ToolCall(id="toolu_02", name="formatter"),
            ],
        )
        ctx = ACEStepContext(trace=r)
        result_ctx = ClaudeSDKToTrace()(ctx)

        trace = result_ctx.trace
        assert "Tool calls (2)" in trace["reasoning"]
        assert "calculator" in trace["reasoning"]
        assert "formatter" in trace["reasoning"]