oxicode-sdk 0.67.0

oxicode AI agent SDK — build isolated, multi-agent AI systems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
# oxicode-sdk 개선 설계 문서

> **버전**: 0.23.0 → 0.24.0 마일스톤
> **작성일**: 2026-05-26
> **범위**: SDK 완성도 분석에서 도출된 Critical 3건, Important 5건, Nice-to-Have 4건

---

## 목차

1. [개요]#1-개요
2. [C-01: Token Usage 추출 파이프라인]#2-c-01-token-usage-추출-파이프라인
3. [C-02: Orchestrated 전략 구현]#3-c-02-orchestrated-전략-구현
4. [C-03: Doc Examples 컴파일 검증]#4-c-03-doc-examples-컴파일-검증
5. [I-01: 에러 타입 일관성 — SdkResult 통일]#5-i-01-에러-타입-일관성--sdkresult-통일
6. [I-02: AgentGroup Send 안전 래퍼]#6-i-02-agentgroup-send-안전-래퍼
7. [I-03: README 및 예제 현대화]#7-i-03-readme-및-예제-현대화
8. [I-04: PluginLoader 실구현]#8-i-04-pluginloader-실구현
9. [I-05: Integration Test 스위트]#9-i-05-integration-test-스위트
10. [N-01: 스트리밍 응답 API]#10-n-01-스트리밍-응답-api
11. [N-02: Configuration Hot-Reload]#11-n-02-configuration-hot-reload
12. [N-03: Metrics Export (OpenTelemetry)]#12-n-03-metrics-export-opentelemetry
13. [N-04: Agent 간 고급 협업 패턴]#13-n-04-agent-간-고급-협업-패턴
14. [마이그레이션 가이드]#14-마이그레이션-가이드
15. [구현 우선순위 및 일정]#15-구현-우선순위-및-일정

---

## 1. 개요

### 1.1 현재 상태

| 항목 | 수치 |
|------|------|
| 총 라인 수 | 9,464줄 (35개 `.rs`) |
| 공개 API | 392개 |
| 테스트 | 172개 (전부 통과) |
| 모듈 | 14개 최상위 + 하위 |
| TODO | 2개 (token count 추출) |

### 1.2 개선 매트릭스

```
┌──────────┬───────────────────────────────────────────────┬──────────┐
│ 우선순위  │ 항목                                          │ 작업 규모 │
├──────────┼───────────────────────────────────────────────┼──────────┤
│ Critical │ C-01 Token Usage 추출 파이프라인              │ M        │
│ Critical │ C-02 Orchestrated 전략 구현                   │ M        │
│ Critical │ C-03 Doc Examples 컴파일 검증                 │ S        │
│ Important│ I-01 SdkResult 에러 타입 통일                 │ M        │
│ Important│ I-02 AgentGroup Send 안전 래퍼                │ L        │
│ Important│ I-03 README 및 예제 현대화                     │ S        │
│ Important│ I-04 PluginLoader 실구현                      │ M        │
│ Important│ I-05 Integration Test 스위트                  │ M        │
│ Nice     │ N-01 스트리밍 응답 API                        │ L        │
│ Nice     │ N-02 Configuration Hot-Reload                 │ M        │
│ Nice     │ N-03 Metrics Export (OTLP)                    │ M        │
│ Nice     │ N-04 Agent 간 고급 협업 패턴                  │ L        │
└──────────┴───────────────────────────────────────────────┴──────────┘
```

---

## 2. C-01: Token Usage 추출 파이프라인

### 2.1 문제

`AgentHandle::run()`이 `metrics.record_success(elapsed, 0, events.len())`를 호출한다.
`AgentEvent::Usage { input_tokens, output_tokens }`가 이미 이벤트 스트림에 존재하지만,
이를 집계하여 metrics에 반영하지 않는다.

```rust
// 현재 코드 (supervisor.rs:207-212)
self.metrics.record_success(
    elapsed.as_millis() as u64,
    0, // ← BUG: 항상 0
    events.len() as u64,
);
// TODO: extract token/usage metrics from events  ← 이 줄
```

### 2.2 해결 설계

`AgentEvent` 스트림에서 `Usage` 이벤트를 순회하여 합산하는 헬퍼 함수를 추가한다.

```rust
// oxicode-sdk/src/metrics.rs에 추가

/// AgentEvent 벡터에서 토큰 사용량을 추출하여 합산.
pub fn extract_token_usage(events: &[oxicode_agent::AgentEvent]) -> (u64, u64) {
    let mut input: u64 = 0;
    let mut output: u64 = 0;
    for event in events {
        if let AgentEvent::Usage {
            input_tokens,
            output_tokens,
        } = event
        {
            input += *input_tokens as u64;
            output += *output_tokens as u64;
        }
    }
    (input, output)
}

/// AgentEvent 벡터에서 툴 실행 횟수를 계산.
pub fn extract_tool_call_count(events: &[oxicode_agent::AgentEvent]) -> u64 {
    events
        .iter()
        .filter(|e| matches!(e, AgentEvent::ToolExecutionStart { .. }))
        .count() as u64
}
```

### 2.3 AgentMetrics 확장

현재 `record_success`는 `total_tokens: u64` 하나만 받는다. 입력/출력을 구분해야 한다.

```rust
// oxicode-sdk/src/metrics.rs

pub struct AgentMetrics {
    pub total_runs: AtomicU64,
    pub successful_runs: AtomicU64,
    pub failed_runs: AtomicU64,
    pub total_input_tokens: AtomicU64,   // ← 추가
    pub total_output_tokens: AtomicU64,  // ← 추가
    pub total_tokens: AtomicU64,         // 기존 유지 (input + output)
    pub tool_calls: AtomicU64,
    pub total_duration_ms: AtomicU64,
}

impl AgentMetrics {
    /// Record a successful run with full token breakdown.
    pub fn record_success(
        &self,
        duration_ms: u64,
        input_tokens: u64,
        output_tokens: u64,
        tool_call_count: u64,
    ) {
        self.total_runs.fetch_add(1, Ordering::Relaxed);
        self.successful_runs.fetch_add(1, Ordering::Relaxed);
        self.total_input_tokens.fetch_add(input_tokens, Ordering::Relaxed);
        self.total_output_tokens.fetch_add(output_tokens, Ordering::Relaxed);
        self.total_tokens.fetch_add(input_tokens + output_tokens, Ordering::Relaxed);
        self.tool_calls.fetch_add(tool_call_count, Ordering::Relaxed);
        self.total_duration_ms.fetch_add(duration_ms, Ordering::Relaxed);
    }
}
```

`MetricsSnapshot`도 동일하게 확장:

```rust
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MetricsSnapshot {
    pub total_runs: u64,
    pub successful_runs: u64,
    pub failed_runs: u64,
    pub total_input_tokens: u64,   // ← 추가
    pub total_output_tokens: u64,  // ← 추가
    pub total_tokens: u64,
    pub tool_calls: u64,
    pub total_duration_ms: u64,
}
```

### 2.4 AgentHandle 수정

```rust
// oxicode-sdk/src/lifecycle/supervisor.rs — AgentHandle::run()

match result {
    Ok((response, events)) => {
        let (input_tokens, output_tokens) =
            crate::metrics::extract_token_usage(&events);
        let tool_count =
            crate::metrics::extract_tool_call_count(&events);

        self.metrics.record_success(
            elapsed.as_millis() as u64,
            input_tokens,
            output_tokens,
            tool_count,
        );

        // CostTracker 연동
        if let Some(tracker) = &self.cost_tracker {
            let usage = crate::observability::TokenUsage {
                input: input_tokens,
                output: output_tokens,
                cache_read: 0, // TODO: 캐시 토큰은 ProviderEvent에서 추출
                cache_write: 0,
            };
            let model_id = self.config.read().model_id.clone();
            if let Some(model) = self.resolver.resolve_model(&model_id) {
                tracker.record(&self.agent_id, &model, usage);
            }
        }
        // ... 기존 코드 계속
    }
}
```

### 2.5 AgentHandle에 cost_tracker 필드 추가

```rust
pub struct AgentHandle {
    // ... 기존 필드
    cost_tracker: Option<Arc<CostTracker>>,  // ← 추가
}
```

`AgentHandle::new()`에서 `None`으로 초기화하고, supervisor의 `spawn()`에서
주입 가능한 빌더 메서드를 추가:

```rust
impl AgentHandle {
    /// Attach a cost tracker to this handle.
    pub fn with_cost_tracker(&mut self, tracker: Arc<CostTracker>) {
        self.cost_tracker = Some(tracker);
    }
}
```

### 2.6 영향 범위

| 파일 | 변경 내용 |
|------|-----------|
| `metrics.rs` | `record_success` 시그니처 변경, `extract_*` 함수 추가 |
| `lifecycle/supervisor.rs` | `run()`에서 usage 추출 로직, `cost_tracker` 필드 |
| `lifecycle/snapshot.rs` | `MetricsSnapshot` 필드 확장 |
| 테스트 | 기존 `record_success` 호출부 업데이트 |

### 2.7 하위 호환성

`record_success(duration_ms, tokens, tools)` → `record_success(duration_ms, input, output, tools)`

기존 호출부는 모두 SDK 내부 테스트뿐이므로 breaking change가 외부에 영향을 주지 않는다.
`MetricsSnapshot`에 필드가 추가되지만 `#[serde(default)]`로 역직렬화 호환성을 유지한다.

---

## 3. C-02: Orchestrated 전략 구현

### 3.1 문제

현재 `AgentGroup::run_orchestrated()`는 leader 에이전트만 실행하고,
worker 에이전트들을 전혀 활용하지 않는다.

```rust
// 현재 (agent_group.rs:178-191)
async fn run_orchestrated(&self, prompt: String, leader_idx: usize) -> Result<...> {
    let leader = &self.agents[leader_idx];
    let (response, _events) = leader.run(prompt).await?;
    // ← worker 에이전트가 무시됨
    Ok(vec![AgentGroupOutput { ... }])
}
```

### 3.2 설계: Leader-Worker 위임 패턴

Leader 에이전트가 응답에서 "지시사항"을 파싱하여 worker들에게 분배하는 방식.

```rust
/// Worker에게 전달할 위임 작업.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DelegatedTask {
    pub task_id: String,
    pub worker_index: usize,
    pub instruction: String,
    pub context: Option<String>,
}

/// Leader 응답에서 위임 작업을 파싱하는 전략.
pub trait DelegationParser: Send + Sync {
    /// Leader 응답에서 위임 작업 목록을 추출.
    fn parse(&self, leader_response: &str, worker_count: usize) -> Vec<DelegatedTask>;
}
```

### 3.3 기본 DelegationParser 구현

Leader에게 구조화된 JSON 출력을 요청하는 방식:

```rust
/// 기본 위임 파서 — leader 응답에서 JSON 블록을 추출.
pub struct JsonDelegationParser;

impl DelegationParser for JsonDelegationParser {
    fn parse(&self, leader_response: &str, worker_count: usize) -> Vec<DelegatedTask> {
        // ```json ... ``` 블록 또는 마지막 JSON 객체 추출
        let tasks = extract_json_tasks(leader_response);
        tasks.into_iter().take(worker_count).collect()
    }
}

fn extract_json_tasks(response: &str) -> Vec<DelegatedTask> {
    // 1. ```json 코드 블록에서 배열 추출 시도
    // 2. { ... } JSON 객체 직접 파싱 시도
    // 3. 파싱 실패 시 빈 벡터 반환
    // ...
}
```

### 3.4 개선된 run_orchestrated

```rust
async fn run_orchestrated(
    &self,
    prompt: String,
    leader_idx: usize,
) -> Result<Vec<AgentGroupOutput>> {
    if leader_idx >= self.agents.len() {
        anyhow::bail!("Leader index {} out of range", leader_idx);
    }

    let leader = &self.agents[leader_idx];
    let workers: Vec<_> = self.agents.iter()
        .enumerate()
        .filter(|(i, _)| *i != leader_idx)
        .collect();

    // Phase 1: Leader가 작업을 분석하고 위임
    let orchestration_prompt = format!(
        "{prompt}\n\n\
        You are the LEADER of a team with {} workers.\n\
        Analyze the task and delegate subtasks.\n\
        Respond with a JSON array of tasks:\n\
        ```json\n\
        [{{\"worker_index\": 0, \"instruction\": \"...\", \"context\": \"...\"}}]\n\
        ```",
        workers.len()
    );

    let (leader_response, _events) = leader.run(orchestration_prompt).await?;

    let mut results = vec![AgentGroupOutput {
        name: leader.model_id(),
        content: leader_response.content.clone(),
        success: true,
        error: None,
    }];

    // Phase 2: Parse delegations
    let parser = JsonDelegationParser;
    let tasks = parser.parse(&leader_response.content, workers.len());

    if tasks.is_empty() || workers.is_empty() {
        // 위임 실패 — leader 결과만 반환
        return Ok(results);
    }

    // Phase 3: Workers execute in parallel
    let worker_handles: Vec<_> = tasks.into_iter()
        .filter_map(|task| {
            let (actual_idx, worker) = workers.get(task.worker_index)?;
            let worker = Arc::clone(worker);
            let instruction = task.instruction;
            let context = task.context.unwrap_or_default();
            Some(tokio::task::spawn_blocking(move || {
                let rt = tokio::runtime::Builder::new_current_thread()
                    .enable_all().build()?;
                rt.block_on(async {
                    let prompt = if context.is_empty() {
                        instruction
                    } else {
                        format!("Context:\n{context}\n\nTask:\n{instruction}")
                    };
                    worker.run(prompt).await
                })
            }))
        })
        .collect();

    for (i, handle) in worker_handles.into_iter().enumerate() {
        match handle.await {
            Ok(Ok((response, _))) => results.push(AgentGroupOutput {
                name: format!("worker-{i}"),
                content: response.content,
                success: true,
                error: None,
            }),
            Ok(Err(e)) => results.push(AgentGroupOutput {
                name: format!("worker-{i}"),
                content: String::new(),
                success: false,
                error: Some(e.to_string()),
            }),
            Err(e) => results.push(AgentGroupOutput {
                name: format!("worker-{i}"),
                content: String::new(),
                success: false,
                error: Some(format!("Join error: {e}")),
            }),
        }
    }

    // Phase 4: (선택) Leader가 worker 결과를 취합
    // — 현재는 각 worker 결과를 개별 반환
    Ok(results)
}
```

### 3.5 AgentGroupBuilder에 parser 주입

```rust
pub struct AgentGroup {
    agents: Vec<Arc<Agent>>,
    strategy: GroupStrategy,
    delegation_parser: Option<Arc<dyn DelegationParser>>,  // ← 추가
}

pub enum GroupStrategy {
    Pipeline,
    Parallel { max_concurrency: usize },
    Orchestrated {
        leader: usize,
        /// 사용자 정의 위임 파서. None이면 JsonDelegationParser 사용.
        delegation_parser: Option<Arc<dyn DelegationParser>>,
    },
}
```

### 3.6 영향 범위

| 파일 | 변경 내용 |
|------|-----------|
| `agent_group.rs` | `run_orchestrated` 전면 재작성, `DelegatedTask` 타입 추가 |
| `agent_group.rs` | `GroupStrategy::Orchestrated``delegation_parser` 필드 추가 |
| `lib.rs` | `DelegatedTask`, `DelegationParser`, `JsonDelegationParser` re-export |
| `prelude.rs` | 동일 |

---

## 4. C-03: Doc Examples 컴파일 검증

### 4.1 문제

현재 20개 doc example이 모두 ` ```ignore ` 블록이다. `cargo test --doc`로 검증 불가.

### 4.2 해결 전략

doc example을 세 단계로 분류:

1. **Runnable**`oxicode_sdk`만 사용, 외부 의존 없이 컴파일 가능
2. **Requires Secrets** — API key가 필요하므로 `#[cfg(feature = "test-live")]`
3. **Conceptual** — 의사코드, `ignore` 유지

### 4.3 Runnable 예제 변환

```rust
// BEFORE:
/// ```ignore
/// let oxicode = OxicodeBuilder::new().with_builtins().build();
/// ```

// AFTER:
/// ```
/// use oxicode_sdk::OxicodeBuilder;
///
/// let oxicode = OxicodeBuilder::new().build();
/// assert!(!oxicode.has_builtins());
/// ```
```

### 4.4 변환 대상 및 방법

| 모듈 | 예제 수 | 전략 |
|------|---------|------|
| `builder.rs` | 4 | `OxicodeBuilder::new().build()`로 축소 → runnable |
| `agent_builder.rs` | 3 | workspace를 `/tmp`로 설정 → runnable (provider 없이 build만) |
| `closure_tool.rs` | 1 | sync handler만 → runnable |
| `kernel_bridge.rs` | 2 | Conceptual 유지 (kernel 의존) |
| `multi_provider.rs` | 3 | `create_builtin_provider` 없이 빌더만 → runnable |
| `tool_factory.rs` | 2 | `Path::new("/tmp")` → runnable |
| `message_bus.rs` | 2 | 이미 runnable 가능 |

### 4.5 CI 통합

```yaml
# .github/workflows/ci.yml에 추가
- name: Doc tests
  run: cargo test -p oxicode-sdk --doc --all-features
```

---

## 5. I-01: 에러 타입 일관성 — SdkResult 통일

### 5.1 문제

SDK 전체에 `anyhow::Result`와 `SdkError`가 혼재:

| 위치 | 반환 타입 | 문제 |
|------|-----------|------|
| `AgentHandle::run()` | `anyhow::Result` | `SdkError`여야 함 |
| `AgentGroup::run()` | `anyhow::Result` | `SdkError`여야 함 |
| `AgentBuilder::build()` | `anyhow::Result` | 현재는 적절 (빌더 에러) |
| `Oxicode::resolve_model()` | `anyhow::Result` | `SdkError::ModelNotFound`여야 함 |
| `Oxicode::create_provider()` | `anyhow::Result` | `SdkError::ProviderNotFound`여야 함 |
| `Authorizer::require()` | `Result<(), SdkError>` | ✅ 올바름 |

### 5.2 설계

```rust
// oxicode-sdk/src/error.rs

/// SDK의 통일 결과 타입.
pub type SdkResult<T> = Result<T, SdkError>;

// SdkError에 Agent 계층 에러 추가
#[derive(Debug, Error)]
pub enum SdkError {
    // ... 기존 variant

    // ── Agent Execution ─────────────────────────────────────
    #[error("agent execution failed: {reason}")]
    ExecutionFailed {
        reason: String,
        source: Option<Box<dyn std::error::Error + Send + Sync>>,
    },

    #[error("agent group execution failed: {failed}/{total} agents failed")]
    GroupExecutionFailed {
        failed: usize,
        total: usize,
        /// 개별 에러 (인덱스 → 에러 메시지).
        errors: HashMap<usize, String>,
    },

    #[error("run cancelled by caller")]
    Cancelled,
}
```

### 5.3 마이그레이션 대상

```rust
// Oxicode 메서드
impl Oxicode {
    pub fn resolve_model(&self, model_id: &str) -> SdkResult<Model> { ... }
    pub fn create_provider(&self, name: &str) -> SdkResult<Arc<dyn Provider>> { ... }
}

// AgentHandle
impl AgentHandle {
    pub async fn run(&self, prompt: String) -> SdkResult<(Response, Vec<AgentEvent>)> {
        // ...
        // AgentError → SdkError 변환
        result.map_err(|e| SdkError::ExecutionFailed {
            reason: e.to_string(),
            source: Some(e.into()),
        })
    }
}

// AgentGroup
impl AgentGroup {
    pub async fn run(&self, prompt: String) -> SdkResult<GroupResult> { ... }
}
```

### 5.4 하위 호환성

`anyhow::Result` → `SdkResult` 변경은 공개 API 시그니처 변경이다.
그러나 `SdkError: From<anyhow::Error>`이므로 `?` 연산자로 `anyhow` 에러를
자동 변환할 수 있어 외부 사용자의 마이그레이션 부담이 적다.

---

## 6. I-02: AgentGroup Send 안전 래퍼

### 6.1 문제

`Agent::run()`이 내부적으로 `Rc` 기반 타입을 사용하여 future가 `!Send`이다.
이 때문에 `AgentGroup::run_parallel()`이 `spawn_blocking` + `new_current_thread`
런타임 생성이라는 비용이 큰 워크어라운드를 사용한다.

```rust
// 현재 (agent_group.rs:192-206)
handles.push(tokio::task::spawn_blocking(move || {
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .expect("Failed to create runtime");  // ← 에이전트마다 런타임 생성
    rt.block_on(async move {
        // ...
    })
}));
```

### 6.2 설계: AgentRunner 래퍼

`!Send` future를 `Send` future로 변환하는 전용 런타임 풀:

```rust
// oxicode-sdk/src/runner.rs — 신규 파일

use oxicode_agent::{Agent, AgentEvent};
use oxicode_agent::types::Response;
use std::sync::Arc;
use tokio::runtime::Runtime;

/// `!Send` agent future를 안전하게 실행하는 런타임 풀.
///
/// 내부적으로 전용 스레드 풀에서 agent를 실행하고,
/// 결과를 `Send` 채널로 반환한다.
pub struct AgentRunner {
    /// 에이전트 실행 전용 런타임 (multi-thread).
    runtime: Arc<Runtime>,
}

impl AgentRunner {
    /// 새 런타임을 생성.
    pub fn new(worker_threads: usize) -> Self {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(worker_threads)
            .enable_all()
            .build()
            .expect("Failed to create agent runtime");
        Self {
            runtime: Arc::new(runtime),
        }
    }

    /// 기본 4개 워커 스레드.
    pub fn default_pool() -> Self {
        Self::new(4)
    }

    /// Agent를 실행하고 `Send` future를 반환.
    ///
    /// 내부적으로 `spawn_blocking`으로 실행하여
    /// `tokio::task::spawn`이 가능한 future를 반환.
    pub fn run(
        &self,
        agent: Arc<Agent>,
        prompt: String,
    ) -> tokio::task::JoinHandle<anyhow::Result<(Response, Vec<AgentEvent>)>> {
        let runtime = Arc::clone(&self.runtime);
        tokio::task::spawn_blocking(move || {
            runtime.block_on(async move { agent.run(prompt).await })
        })
    }
}
```

### 6.3 AgentGroup에 Runner 주입

```rust
pub struct AgentGroup {
    agents: Vec<Arc<Agent>>,
    strategy: GroupStrategy,
    runner: Option<Arc<AgentRunner>>,  // ← 추가
}

impl AgentGroup {
    /// 커스텀 러너를 사용하는 그룹 생성.
    pub fn with_runner(runner: Arc<AgentRunner>) -> AgentGroupBuilder {
        AgentGroupBuilder {
            agents: Vec::new(),
            strategy: GroupStrategy::default(),
            runner: Some(runner),
        }
    }
}
```

### 6.4 개선된 run_parallel

```rust
async fn run_parallel(
    &self,
    prompt: String,
    max_concurrency: usize,
) -> Result<Vec<AgentGroupOutput>> {
    let semaphore = Arc::new(tokio::sync::Semaphore::new(max_concurrency));

    // 러너가 있으면 공유 런타임 재사용, 없으면 기존 방식
    let handles: Vec<_> = self.agents.iter().map(|agent| {
        let agent = Arc::clone(agent);
        let prompt = prompt.clone();
        let sem = Arc::clone(&semaphore);

        match &self.runner {
            Some(runner) => {
                // 공유 런타임 사용 — 스레드 생성 비용 절약
                                let _permit = sem.acquire().await.unwrap();
                                match agent.run(prompt).await {
                                    Ok((response, _)) => AgentGroupOutput {
                                        name: agent.model_id(),
                                        content: response.content,
                                        success: true,
                                        error: None,
                                    },
                                    Err(e) => AgentGroupOutput {
                                        name: agent.model_id(),
                                        content: String::new(),
                                        success: false,
                                        error: Some(e.to_string()),
                                    },
                                }
                            }
                        })
                    }
                    None => {
                        // 기존 방식 (호환성)
                        tokio::task::spawn_blocking(move || {
                            let rt = tokio::runtime::Builder::new_current_thread()
                                .enable_all().build()?;
                            rt.block_on(async {
                                let _permit = sem.acquire().await.unwrap();
                                match agent.run(prompt).await {
                                    Ok((response, _)) => AgentGroupOutput {
                                        name: agent.model_id(),
                                        content: response.content,
                                        success: true,
                                        error: None,
                                    },
                                    Err(e) => AgentGroupOutput {
                                        name: agent.model_id(),
                                        content: String::new(),
                                        success: false,
                                        error: Some(e.to_string()),
                                    },
                                }
                            })
                        })
                    }
                }
    }).collect();
    // ... 결과 수집
}
```

### 6.5 장기적 해결 (oxicode-agent)

근본적으로 `oxicode-agent`의 `Agent::run()`이 `Send` future를 반환하도록
내부 `Rc`를 `Arc`로 교체하는 작업이 필요하다. 이는 별도 마일스톤으로 분리한다.

---

## 7. I-03: README 및 예제 현대화

### 7.1 문제

- README.md가 구버전 API (`include_builtins`, `model_id`) 사용
- 예제가 `builder_demo.rs` 하나뿐, 사실상 println 데모
- 아키텍처 다이어그램 없음

### 7.2 README 재작성

```markdown
# oxicode-sdk

Multi-agent SDK for oxicode — build isolated, secure, observable AI agent systems in Rust.

## Quick Start

```rust
use oxicode_sdk::prelude::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // 1. Build the engine
    let oxicode = OxicodeBuilder::new()
        .with_builtins()
        .api_key("anthropic", "sk-ant-...")
        .build();

    // 2. Build an agent
    let agent = oxicode.agent(AgentConfig {
        model_id: "anthropic/claude-sonnet-4-20250514".into(),
        max_iterations: 20,
        ..Default::default()
    })
    .workspace("/my/project")
    .coding_tools()
    .system_prompt("You are a senior Rust developer.")
    .build()?;

    // 3. Run
    let (response, events) = agent.run("Refactor main.rs".into()).await?;
    println!("{}", response.content);
    Ok(())
}
```

## Architecture

```
┌─────────────────────────────────────────────┐
│  OxicodeBuilder → Oxicode                           │  Engine + Registry
├─────────────────────────────────────────────┤
│  AgentBuilder → Agent                       │  Per-agent config
├─────────────────────────────────────────────┤
│  AgentGroup  │ MessageBus │ Supervisor      │  Orchestration
├─────────────────────────────────────────────┤
│  Security │ Middleware │ Observability      │  Cross-cutting
├─────────────────────────────────────────────┤
│  Coordination (Queue + Memory + Consensus)  │  Inter-agent
└─────────────────────────────────────────────┘
```

## Feature Flags

| Flag | Description |
|------|-------------|
| `default` | Core SDK |
| `native-browser` | Built-in headless browser tools |

## Modules

| Module | Purpose |
|--------|---------|
| `security` | Capability-based access control |
| `middleware` | Pipeline hooks (rate limit, logging, content filter) |
| `observability` | Tracing, audit, cost tracking, event store |
| `coordination` | Work queue, shared memory, consensus |
| `lifecycle` | Supervisor, snapshot, suspend/resume |
```

### 7.3 예제 추가

| 파일 | 내용 |
|------|------|
| `examples/minimal.rs` | 최소 에이전트 빌드 + 실행 |
| `examples/multi_agent.rs` | AgentGroup 병렬/파이프라인 |
| `examples/security.rs` | Capability + Authorizer + SecurityMiddleware |
| `examples/observability.rs` | Tracer + AuditLog + CostTracker 통합 |
| `examples/coordination.rs` | WorkQueue + SharedMemory + Consensus |

**`examples/multi_agent.rs` 예시:**

```rust
//! Multi-agent parallel execution example.
//!
//! Run with: cargo run -p oxicode-sdk --example multi_agent

use oxicode_sdk::prelude::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let oxicode = OxicodeBuilder::new().with_builtins().build();

    let config = AgentConfig {
        model_id: "anthropic/claude-sonnet-4-20250514".into(),
        max_iterations: 10,
        ..Default::default()
    };

    let reviewer = oxicode.agent(config.clone())
        .system_prompt("You are a code reviewer.")
        .workspace("/tmp")
        .build()?;

    let tester = oxicode.agent(config)
        .system_prompt("You are a test engineer.")
        .workspace("/tmp")
        .build()?;

    let group = AgentGroup::new(
        GroupStrategy::Parallel { max_concurrency: 2 }
    )
    .agent(Arc::new(reviewer))
    .agent(Arc::new(tester));

    let result = group.run("Analyze this codebase for issues.".into()).await?;

    for output in &result.results {
        println!("=== {} ({}) ===", output.name, output.success);
        println!("{}", &output.content[..output.content.len().min(200)]);
    }

    Ok(())
}
```

---

## 8. I-04: PluginLoader 실구현

### 8.1 문제

`PluginLoader`가 manifest 로딩만 하고, `loaded` 맵이 항상 비어 있다.
실제 동적 라이브러리 로딩이 구현되지 않았다.

### 8.2 설계: trait 기반 플러그인 아키텍처

동적 라이브러리 로딩 대신, trait 기반 접근으로 안전성을 확보:

```rust
// oxicode-sdk/src/middleware/plugin.rs

/// 플러그인이 구현해야 하는 trait.
///
/// 동적 로딩(unsafe) 대신, 플러그인은 이 trait을 구현하는
/// struct를 제공하고 `register_plugin()`으로 등록한다.
pub trait Plugin: Send + Sync {
    /// 플러그인 이름.
    fn name(&self) -> &str;

    /// 플러그인이 제공하는 미들웨어 목록.
    fn middlewares(&self) -> Vec<Arc<dyn Middleware>>;

    /// (선택) 초기화. 엔진 컨텍스트에 접근 가능.
    fn initialize(&mut self, _ctx: &PluginContext) -> anyhow::Result<()> {
        Ok(())
    }

    /// (선택) 종료 시 정리.
    fn shutdown(&self) {}
}

/// 플러그인 초기화 컨텍스트.
pub struct PluginContext {
    /// 엔진의 모델 레지스트리 접근.
    pub models: Arc<oxicode_ai::ModelRegistry>,
    /// 메시지 버스 (선택).
    pub message_bus: Option<Arc<crate::MessageBus>>,
}
```

### 8.3 PluginLoader 재구현

```rust
pub struct PluginLoader {
    plugins: RwLock<Vec<Box<dyn Plugin>>>,
    manifests: RwLock<HashMap<String, PluginManifest>>,
}

impl PluginLoader {
    pub fn new(_plugins_dir: impl Into<PathBuf>) -> Self {
        Self {
            plugins: RwLock::new(Vec::new()),
            manifests: RwLock::new(HashMap::new()),
        }
    }

    /// trait 기반 플러그인 등록.
    pub fn register_plugin(&self, plugin: impl Plugin + 'static) -> anyhow::Result<()> {
        let name = plugin.name().to_string();
        let mut plugins = self.plugins.write();

        // 초기화
        let ctx = PluginContext {
            models: Arc::new(oxicode_ai::ModelRegistry::new()),
            message_bus: None,
        };

        plugins.push(Box::new(plugin));
        tracing::info!(plugin = %name, "Plugin registered");
        Ok(())
    }

    /// manifest에서 메타데이터 로딩 (기존 API 유지).
    pub async fn load(&self, manifest_path: &Path) -> anyhow::Result<String> {
        let manifest = PluginManifest::from_file(manifest_path)?;
        let name = manifest.name.clone();
        self.manifests.write().insert(name.clone(), manifest);
        Ok(name)
    }

    /// 등록된 모든 플러그인의 미들웨어를 반환.
    pub fn middlewares(&self) -> Vec<Arc<dyn Middleware>> {
        self.plugins
            .read()
            .iter()
            .flat_map(|p| p.middlewares())
            .collect()
    }

    /// 특정 플러그인 제거.
    pub fn unload(&self, name: &str) -> bool {
        let mut plugins = self.plugins.write();
        let len_before = plugins.len();
        plugins.retain(|p| p.name() != name);
        plugins.len() != len_before
    }
}
```

### 8.4 (향후) unsafe 동적 로딩 지원

별도 feature flag `unsafe-plugins`로 분리:

```toml
[features]
unsafe-plugins = ["libloading"]

[dependencies]
libloading = { version = "0.8", optional = true }
```

---

## 9. I-05: Integration Test 스위트

### 9.1 문제

현재 테스트는 모두 단위 테스트이며, MockProvider 기반 end-to-end 테스트가 없다.

### 9.2 설계: MockProvider 기반 E2E 테스트

```rust
// oxicode-sdk/tests/integration.rs — 신규 파일

use oxicode_sdk::prelude::*;
use oxicode_ai::{Provider, ProviderEvent, Model, Api};
use futures::Stream;

struct EchoProvider;

#[async_trait::async_trait]
impl Provider for EchoProvider {
    fn name(&self) -> &str { "echo" }

    async fn stream(
        &self,
        _model: &Model,
        context: &oxicode_ai::Context,
        _options: Option<oxicode_ai::StreamOptions>,
    ) -> Result<Pin<Box<dyn Stream<Item = ProviderEvent> + Send>>, oxicode_ai::ProviderError> {
        // 사용자 마지막 메시지를 에코
        let last_msg = context.messages.last()
            .and_then(|m| m.content().first())
            .and_then(|b| b.as_text())
            .unwrap_or("echo")
            .to_string();

        let stream = futures::stream::iter(vec![
            ProviderEvent::TextDelta { text: last_msg },
            ProviderEvent::EndTurn,
        ]);
        Ok(Box::pin(stream))
    }
}

fn echo_model() -> Model {
    Model::new("echo/model", "Echo", Api::OpenAiChat, "echo", "http://localhost")
}

fn echo_oxicode() -> Oxicode {
    OxicodeBuilder::new()
        .provider("echo", EchoProvider)
        .model(echo_model())
        .build()
}

#[tokio::test]
async fn full_pipeline_builder_to_run() {
    let oxicode = echo_oxicode();

    let agent = oxicode.agent(AgentConfig {
        model_id: "echo/model".into(),
        max_iterations: 5,
        ..Default::default()
    })
    .workspace("/tmp")
    .system_prompt("Echo agent")
    .build()
    .expect("build should succeed");

    let (response, events) = agent.run("Hello world".into()).await
        .expect("run should succeed");

    assert!(!response.content.is_empty());
    assert!(!events.is_empty());
}

#[tokio::test]
async fn security_blocks_unauthorized_tool() {
    let oxicode = echo_oxicode();
    let audit = Arc::new(AuditLog::new(64));
    let authorizer = Arc::new(Authorizer::new(Arc::clone(&audit)));

    // read-only 권한만 부여
    authorizer.grant(
        CapabilitySubject::Agent("secured-agent".into()),
        CapabilitySet::read_only("/workspace"),
    );

    let agent = oxicode.agent(AgentConfig {
        model_id: "echo/model".into(),
        name: "secured-agent".into(),
        max_iterations: 5,
        ..Default::default()
    })
    .workspace("/workspace")
    .readonly_tools()
    .authorizer(authorizer)
    .audit_log(audit)
    .build()
    .unwrap();

    // agent가 도구를 호출하려 하면 security middleware가 차단하는지 확인
    let _ = agent.run("Read /workspace/file.txt".into()).await;
    // audit log에 security decision이 기록되었는지 확인
}

#[tokio::test]
async fn supervisor_spawn_run_terminate() {
    let oxicode = echo_oxicode();
    let resolver: Arc<dyn ProviderResolver> = Arc::new(oxicode.clone());
    let snapshot_store: Arc<dyn SnapshotStore> = Arc::new(
        FileSnapshotStore::new(std::env::temp_dir().join("oxicode-test-snapshots")).unwrap()
    );
    let supervisor = AgentSupervisor::new(resolver, snapshot_store);

    let handle = supervisor.spawn(AgentConfig {
        model_id: "echo/model".into(),
        max_iterations: 5,
        ..Default::default()
    }).unwrap();

    assert_eq!(handle.status(), AgentStatus::Created);

    let (response, _) = handle.run("test".into()).await.unwrap();
    assert!(!response.content.is_empty());

    handle.terminate().unwrap();
    assert!(handle.status().is_terminal());
}

#[tokio::test]
async fn message_bus_pub_sub() {
    let bus = MessageBus::new(16);
    let mut rx = bus.subscribe();

    bus.publish(InterAgentMessage::broadcast(
        "coordinator",
        "start",
        serde_json::json!({"phase": 1}),
    ));

    let msg = rx.recv().await.unwrap();
    assert_eq!(msg.message_type, "start");
}

#[tokio::test]
async fn work_queue_claim_complete() {
    let queue = WorkQueue::new(WorkQueueConfig::default());
    let id = queue.enqueue("review", serde_json::json!({"file": "main.rs"}), 5);

    let item = queue.claim("agent-1", None).unwrap();
    assert_eq!(item.id, id);

    queue.start(&id).unwrap();
    queue.complete(&id, WorkResult {
        success: true,
        content: "LGTM".into(),
        error: None,
        duration_ms: 100,
        tokens_used: None,
    }).unwrap();

    assert_eq!(queue.stats().completed, 1);
}

#[tokio::test]
async fn observability_pipeline() {
    let tracer = Arc::new(Tracer::new());
    let audit = Arc::new(AuditLog::new(64));
    let registry = Arc::new(ModelRegistry::new());
    let cost = Arc::new(CostTracker::new(registry, CostTrackerConfig::default()));

    // Tracer span
    {
        let _span = tracer.start("test-run", SpanKind::Agent);
    }

    // Audit entry
    audit.log(AuditEntry::lifecycle("agent-1".into(), "started".into()));

    // Cost record
    let model = echo_model();
    cost.record("agent-1", &model, TokenUsage {
        input: 1000,
        output: 500,
        ..Default::default()
    });

    // Verify
    let spans: Vec<_> = { tracer.spans.read().clone() };
    assert!(!spans.is_empty());
    assert_eq!(audit.entries().len(), 1);
    assert!(cost.snapshot("agent-1").is_some());
}
```

### 9.3 테스트 인프라 공통화

```rust
// oxicode-sdk/tests/common/mod.rs

pub fn echo_oxicode() -> Oxicode { ... }
pub fn echo_model() -> Model { ... }
```

---

## 10. N-01: 스트리밍 응답 API

### 10.1 설계

`Agent::run_streaming()`이 `oxicode-agent`에 이미 존재한다.
SDK 레이어에서 이를 래핑하여 콜백 기반 + 채널 기반 API를 제공한다.

```rust
// oxicode-sdk/src/streaming.rs — 신규 파일

use oxicode_agent::AgentEvent;
use tokio::sync::mpsc;

/// 스트리밍 실행 결과 수신기.
pub struct EventStream {
    rx: mpsc::Receiver<AgentEvent>,
}

impl EventStream {
    /// 다음 이벤트를 비동기 대기.
    pub async fn next(&mut self) -> Option<AgentEvent> {
        self.rx.recv().await
    }

    /// 이벤트를 동기적으로 peek (non-blocking).
    pub fn try_next(&mut self) -> Option<AgentEvent> {
        self.rx.try_recv().ok()
    }
}

// AgentHandle에 추가
impl AgentHandle {
    /// 스트리밍 실행 — 이벤트를 실시간으로 수신.
    pub async fn run_streaming(
        &self,
        prompt: String,
    ) -> SdkResult<(EventStream, tokio::task::JoinHandle<SdkResult<Response>>)> {
        let (tx, rx) = mpsc::channel(256);

        let agent = Arc::clone(&self.agent);
        let terminate = Arc::new(AtomicBool::new(false));

        // CAS 상태 전환
        // ...

        let handle = tokio::task::spawn_blocking(move || {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all().build()?;

            let tx_clone = tx.clone();
            rt.block_on(async {
                agent.run_streaming(prompt, move |event| {
                    let _ = tx_clone.blocking_send(event);
                }).await
            })
        });

        let stream = EventStream { rx };
        // ... 결과 조립
        Ok((stream, handle))
    }
}
```

### 10.2 사용 예시

```rust
let (mut stream, handle) = agent.run_streaming("Explain Rust".into()).await?;

while let Some(event) = stream.next().await {
    match event {
        AgentEvent::TextChunk { text } => print!("{text}"),
        AgentEvent::ToolExecutionStart { tool_name, .. } => {
            println!("\n[Tool: {tool_name}]")
        }
        AgentEvent::Usage { input_tokens, output_tokens } => {
            println!("\n[Usage: {input_tokens} in, {output_tokens} out]")
        }
        _ => {}
    }
}

let response = handle.await??;
println!("\nDone: {}", response.content);
```

---

## 11. N-02: Configuration Hot-Reload

### 11.1 설계

실행 중인 에이전트의 설정을 파일 변경 감지로 자동 갱신.

```rust
// oxicode-sdk/src/config_watcher.rs — 신규 파일

use notify::{Watcher, RecommendedWatcher, Event};
use std::path::PathBuf;
use std::sync::Arc;

/// 설정 파일 변경 감시기.
pub struct ConfigWatcher {
    watcher: RecommendedWatcher,
    tx: tokio::sync::watch::Sender<ConfigUpdate>,
}

#[derive(Debug, Clone)]
pub enum ConfigUpdate {
    SystemPrompt(String),
    ModelSwitch(String),
    RateLimit(usize),
    TokenBudget(usize),
}

impl ConfigWatcher {
    pub fn new(path: PathBuf) -> anyhow::Result<(Self, tokio::sync::watch::Receiver<ConfigUpdate>)> {
        let (tx, rx) = tokio::sync::watch::channel(ConfigUpdate::SystemPrompt(String::new()));

        let tx_clone = tx.clone();
        let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| {
            if let Ok(event) = res {
                if event.kind.is_modify() {
                    // 파일 재로드 + 파싱
                    if let Ok(update) = Self::parse_config(&path) {
                        let _ = tx_clone.send(update);
                    }
                }
            }
        })?;

        watcher.watch(&path, notify::RecursiveMode::NonRecursive)?;

        Ok((Self { watcher, tx }, rx))
    }

    fn parse_config(path: &PathBuf) -> anyhow::Result<ConfigUpdate> {
        // TOML/JSON 파싱
        // ...
    }
}
```

### 11.2 AgentHandle에 watch 통합

```rust
impl AgentHandle {
    /// 설정 변경을 감시하여 자동 적용.
    pub async fn watch_config(&self, path: PathBuf) -> anyhow::Result<()> {
        let (_, mut rx) = ConfigWatcher::new(path)?;
        let handle = self.clone();

        tokio::spawn(async move {
            while rx.changed().await.is_ok() {
                let update = rx.borrow().clone();
                match update {
                    ConfigUpdate::SystemPrompt(p) => handle.set_system_prompt(p),
                    ConfigUpdate::ModelSwitch(m) => {
                        if let Err(e) = handle.switch_model(&m, None) {
                            tracing::warn!("Config hot-reload model switch failed: {e}");
                        }
                    }
                    _ => {}
                }
            }
        });

        Ok(())
    }
}
```

---

## 12. N-03: Metrics Export (OpenTelemetry)

### 12.1 설계

`AgentMetrics`의 atomic counter를 OpenTelemetry 포맷으로 내보내는 익스포터.

```rust
// oxicode-sdk/src/export/mod.rs — 신규 파일

pub mod otlp;
pub mod prometheus;
pub mod json;

/// 메트릭 익스포터 trait.
pub trait MetricsExporter: Send + Sync {
    /// 현재 스냅샷을 내보냄.
    fn export(&self, snapshot: &MetricsSnapshot, agent_id: &str);

    /// 모든 에이전트의 글로벌 스냅샷을 내보냄.
    fn export_global(&self, snapshot: &GlobalMetricsSnapshot);
}

// oxicode-sdk/src/export/otlp.rs
pub struct OtlpExporter {
    endpoint: String,
    client: reqwest::Client,
}

impl MetricsExporter for OtlpExporter {
    fn export(&self, snapshot: &MetricsSnapshot, agent_id: &str) {
        // OpenTelemetry OTLP HTTP payload 구성
        // ...
    }
}
```

### 12.2 AgentRunner에 익스포터 연결

```rust
impl AgentRunner {
    /// 주기적 메트릭 익스포트 시작.
    pub fn start_export(
        &self,
        exporter: Arc<dyn MetricsExporter>,
        interval: std::time::Duration,
        metrics: Arc<AgentMetrics>,
        agent_id: String,
    ) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(interval);
            loop {
                interval.tick().await;
                let snapshot = metrics.snapshot();
                exporter.export(&snapshot, &agent_id);
            }
        })
    }
}
```

---

## 13. N-04: Agent 간 고급 협업 패턴

### 13.1 Delegation Chain

```rust
// oxicode-sdk/src/coordination/delegation.rs — 신규 파일

/// 위임 체인 — 에이전트가 다른 에이전트에게 작업을 위임할 수 있는 메커니즘.
pub struct DelegationChain {
    bus: Arc<MessageBus>,
    /// agent_id → 위임받은 에이전트 목록.
    delegates: RwLock<HashMap<String, Vec<String>>>,
}

impl DelegationChain {
    /// A가 B에게 작업 위임.
    pub async fn delegate(
        &self,
        from: &str,
        to: &str,
        task: DelegatedTask,
    ) -> anyhow::Result<()> {
        self.delegates
            .write()
            .entry(from.into())
            .or_default()
            .push(to.into());

        self.bus.publish(InterAgentMessage::direct(
            from, to,
            "delegation",
            serde_json::to_value(&task)?,
        ));

        Ok(())
    }

    /// 위임받은 작업의 결과를 보고.
    pub async fn report(
        &self,
        from: &str,
        to: &str,
        result: DelegationResult,
    ) -> PublishResult {
        self.bus.publish(InterAgentMessage::direct(
            from, to,
            "delegation_result",
            serde_json::to_value(&result).unwrap(),
        ))
    }
}
```

### 13.2 Recursive Spawn

```rust
impl AgentSupervisor {
    /// 에이전트가 자신의 자식을 동적으로 스폰할 수 있도록 허용.
    pub fn enable_recursive_spawn(
        &self,
        max_depth: usize,
        max_children_per_agent: usize,
    ) -> RecursiveSpawnGuard {
        RecursiveSpawnGuard {
            supervisor: self.clone(),
            max_depth,
            max_children_per_agent,
            depth: Arc::new(AtomicUsize::new(0)),
        }
    }
}

pub struct RecursiveSpawnGuard {
    supervisor: AgentSupervisor,
    max_depth: usize,
    max_children_per_agent: usize,
    depth: Arc<AtomicUsize>,
}

impl RecursiveSpawnGuard {
    /// 깊이 제한 내에서 자식 에이전트 스폰.
    pub fn spawn_child(&self, parent_id: &str, config: AgentConfig) -> anyhow::Result<AgentHandle> {
        let current = self.depth.load(Ordering::SeqCst);
        if current >= self.max_depth {
            return Err(SdkError::ExecutionFailed {
                reason: format!("Max recursion depth ({}) reached", self.max_depth),
                source: None,
            }.into());
        }

        self.depth.fetch_add(1, Ordering::SeqCst);
        let handle = self.supervisor.spawn_child(parent_id, config)?;
        Ok(handle)
    }
}
```

---

## 14. 마이그레이션 가이드

### 14.1 v0.23 → v0.24 Breaking Changes

#### `AgentMetrics::record_success` 시그니처 변경

```rust
// BEFORE (v0.23)
metrics.record_success(duration_ms, total_tokens, tool_calls);

// AFTER (v0.24)
metrics.record_success(duration_ms, input_tokens, output_tokens, tool_calls);
```

SDK 외부에서 `AgentMetrics`를 직접 호출하는 경우만 영향.
`AgentHandle::run()`을 사용하면 자동 처리됨.

#### `SdkResult` 도입

```rust
// BEFORE
let (response, events) = handle.run(prompt).await?;  // anyhow::Result

// AFTER
let (response, events) = handle.run(prompt).await?;  // SdkResult

// match로 세분화 가능
match handle.run(prompt).await {
    Ok((response, events)) => { ... },
    Err(SdkError::ExecutionFailed { reason, .. }) => { ... },
    Err(SdkError::Cancelled) => { ... },
    Err(e) => { ... },
}
```

`anyhow` 에러가 `SdkError::Internal`로 자동 래핑되므로 기존 `?` 사용은 그대로 작동.

#### `GroupStrategy::Orchestrated` variant 변경

```rust
// BEFORE
GroupStrategy::Orchestrated { leader: 0 }

// AFTER
GroupStrategy::Orchestrated {
    leader: 0,
    delegation_parser: None,  // Option<Arc<dyn DelegationParser>>
}
```

#### `MetricsSnapshot` 필드 추가

```rust
// v0.24에 추가된 필드 (#[serde(default)]로 역호환)
pub struct MetricsSnapshot {
    pub total_input_tokens: u64,   // NEW
    pub total_output_tokens: u64,  // NEW
    // ... 기존 필드 유지
}
```

### 14.2 Non-Breaking Additions

다음은 순수 추가이므로 기존 코드에 영향 없음:

- `AgentRunner`, `EventStream`
- `DelegationParser`, `JsonDelegationParser`, `DelegatedTask`
- `SdkResult<T>` type alias
- `extract_token_usage()`, `extract_tool_call_count()`
- `Plugin` trait
- `ConfigWatcher`
- `MetricsExporter` trait
- `DelegationChain`, `RecursiveSpawnGuard`

---

## 15. 구현 우선순위 및 일정

### 15.1 Phase 1 — Critical (v0.24.0)

```
Week 1-2
├── C-01 Token Usage 추출 (metrics.rs, supervisor.rs)
├── C-03 Doc Examples (20개 ignore → runnable 변환)
└── I-01 SdkResult 통일 (error.rs, Oxicode, AgentHandle, AgentGroup)

Week 3-4
├── C-02 Orchestrated 전략 (agent_group.rs 전면 재작성)
├── I-03 README + examples/ (5개 예제 추가)
└── I-05 Integration tests (tests/integration.rs)
```

### 15.2 Phase 2 — Important (v0.25.0)

```
Week 5-6
├── I-02 AgentRunner (runner.rs, agent_group.rs 리팩터)
├── I-04 PluginLoader 재구현 (middleware/plugin.rs)
└── N-01 스트리밍 API (streaming.rs)

Week 7-8
├── N-02 ConfigWatcher (config_watcher.rs)
└── N-04 고급 협업 (coordination/delegation.rs)
```

### 15.3 Phase 3 — Nice-to-Have (v0.26.0)

```
Week 9-10
├── N-03 Metrics Export (export/*.rs)
└── oxicode-agent Send future 근본 해결
```

### 15.4 검증 체크리스트

각 Phase 종료 시:

```bash
# 1. 전체 테스트 통과
cargo test -p oxicode-sdk --all-features

# 2. Doc test 통과
cargo test -p oxicode-sdk --doc --all-features

# 3. Clippy clean
cargo clippy -p oxicode-sdk --all-features -- -D warnings

# 4. Format
cargo fmt -p oxicode-sdk -- --check

# 5. Integration tests
cargo test -p oxicode-sdk --test integration

# 6. 예제 빌드
cargo build -p oxicode-sdk --examples --all-features
```

---

## Appendix A: 신규 파일 목록

```
oxicode-sdk/
├── src/
│   ├── runner.rs                  # AgentRunner (I-02)
│   ├── streaming.rs               # EventStream (N-01)
│   ├── config_watcher.rs          # ConfigWatcher (N-02)
│   ├── export/
│   │   ├── mod.rs                 # MetricsExporter trait (N-03)
│   │   ├── otlp.rs
│   │   ├── prometheus.rs
│   │   └── json.rs
│   └── coordination/
│       └── delegation.rs          # DelegationChain (N-04)
├── tests/
│   ├── common/
│   │   └── mod.rs                 # 테스트 유틸리티 (I-05)
│   └── integration.rs             # E2E 테스트 (I-05)
├── examples/
│   ├── minimal.rs                 # (I-03)
│   ├── multi_agent.rs             # (I-03)
│   ├── security.rs                # (I-03)
│   ├── observability.rs           # (I-03)
│   └── coordination.rs            # (I-03)
└── DESIGN_IMPROVEMENTS.md         # 이 문서
```

## Appendix B: 신규 공개 타입 요약

| 타입 | 모듈 | 설명 |
|------|------|------|
| `SdkResult<T>` | `error` | 통일 결과 타입 |
| `AgentRunner` | `runner` | `!Send` agent 실행 래퍼 |
| `EventStream` | `streaming` | 스트리밍 이벤트 수신기 |
| `DelegatedTask` | `agent_group` | 위임 작업 DTO |
| `DelegationParser` | `agent_group` | 위임 파싱 trait |
| `JsonDelegationParser` | `agent_group` | 기본 JSON 위임 파서 |
| `Plugin` | `middleware` | 플러그인 trait |
| `PluginContext` | `middleware` | 플러그인 초기화 컨텍스트 |
| `ConfigWatcher` | `config_watcher` | 설정 파일 감시 |
| `ConfigUpdate` | `config_watcher` | 설정 변경 이벤트 |
| `MetricsExporter` | `export` | 메트릭 익스포터 trait |
| `DelegationChain` | `coordination` | 에이전트 위임 체인 |
| `RecursiveSpawnGuard` | `coordination` | 재귀 스폰 가드 |