nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
Documentation
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
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
//! Artifact Processor - Write task outputs to disk
//!
//! Integrates the artifact system with the task execution flow.
//! Called after successful task completion when `artifact:` is configured.
//!
//! Supports template-based artifacts via the `template:` field
//! which supports `{{with.*}}` bindings for dynamic content generation.

use std::path::PathBuf;
use std::sync::Arc;

use tracing::{debug, warn};

use crate::ast::artifact::{
    ArtifactFormat, ArtifactMode, ArtifactOutput, ArtifactSpec, ArtifactsConfig,
};
use crate::ast::OutputFormat;
use crate::binding::{template_resolve, ResolvedBindings};
use crate::error::NikaError;
use crate::event::{EventKind, EventLog};
use crate::io::atomic::{write_append, write_fail, write_unique};
use crate::io::security::DEFAULT_ARTIFACT_DIR;
use crate::io::writer::{
    ArtifactWriter, BinarySource, BinaryWriteRequest, WriteRequest, WriteResult,
};
use crate::media::MediaRef;
use crate::serde_yaml;
use crate::store::RunContext;

/// Result of processing artifacts for a task
#[derive(Debug, Clone)]
pub struct ArtifactProcessResult {
    /// Number of artifacts successfully written
    pub written: usize,
    /// Paths of written artifacts
    pub paths: Vec<PathBuf>,
    /// Any errors that occurred (non-fatal)
    pub errors: Vec<String>,
}

/// Process artifacts for a completed task
///
/// # Arguments
///
/// * `task_id` - The task ID
/// * `output` - The task output as a string
/// * `artifact_spec` - Task-level artifact configuration
/// * `workflow_config` - Workflow-level artifact defaults
/// * `base_path` - Base path for artifact resolution (workflow directory)
/// * `event_log` - Optional event log for emitting artifact events
/// * `bindings` - Resolved bindings for template resolution
/// * `datastore` - Data store for lazy binding resolution
/// * `media_refs` - Media files produced by the task (for binary artifact source resolution)
///
/// # Returns
///
/// `ArtifactProcessResult` with write status and any errors
#[allow(clippy::too_many_arguments)]
pub async fn process_task_artifacts(
    task_id: &str,
    output: &str,
    artifact_spec: &ArtifactSpec,
    workflow_config: Option<&ArtifactsConfig>,
    base_path: &std::path::Path,
    event_log: Option<&EventLog>,
    bindings: &ResolvedBindings,
    datastore: &RunContext,
    media_refs: &[MediaRef],
) -> ArtifactProcessResult {
    let mut result = ArtifactProcessResult {
        written: 0,
        paths: Vec::new(),
        errors: Vec::new(),
    };

    // Get the artifact outputs to write based on spec type
    let outputs = match artifact_spec {
        ArtifactSpec::Enabled(false) => {
            // Artifacts disabled for this task
            return result;
        }
        ArtifactSpec::Enabled(true) => {
            // Use defaults - generate single output with task_id as filename
            let format = workflow_config
                .map(|c| &c.format)
                .unwrap_or(&ArtifactFormat::Text);
            vec![ArtifactOutput {
                path: format!("{}.{}", task_id, format.extension()),
                source: None,
                template: None,
                format: Some(*format),
                mode: workflow_config.map(|c| c.mode),
            }]
        }
        ArtifactSpec::Single(output_spec) => {
            vec![output_spec.clone()]
        }
        ArtifactSpec::Multiple(outputs) => outputs.clone(),
    };

    // Resolve artifact directory
    let artifact_dir = resolve_artifact_dir(workflow_config, base_path).await;

    // Get max size from workflow config
    let max_size = workflow_config
        .map(|c| c.max_size)
        .unwrap_or(crate::ast::artifact::DEFAULT_MAX_ARTIFACT_SIZE);

    // Create artifact writer
    let writer = ArtifactWriter::new(&artifact_dir, task_id).with_max_size(max_size);

    // Process each output
    for output_spec in outputs {
        match write_single_artifact(
            task_id,
            output,
            &output_spec,
            workflow_config,
            &writer,
            bindings,
            datastore,
            media_refs,
        )
        .await
        {
            Ok(write_result) => {
                debug!(
                    task_id = %task_id,
                    path = %write_result.path.display(),
                    size = write_result.size,
                    "Artifact written"
                );

                // Emit ArtifactWritten event if event_log provided
                if let Some(log) = event_log {
                    let checksum = if write_result.format == OutputFormat::Binary {
                        resolve_binary_checksum(&output_spec, media_refs)
                    } else {
                        None
                    };
                    log.emit(EventKind::ArtifactWritten {
                        task_id: Arc::from(task_id),
                        path: write_result.path.display().to_string(),
                        size: write_result.size,
                        format: format!("{:?}", write_result.format).to_lowercase(),
                        checksum,
                    });
                }

                result.written += 1;
                result.paths.push(write_result.path);
            }
            Err(e) => {
                warn!(
                    task_id = %task_id,
                    path = %output_spec.path,
                    error = %e,
                    "Failed to write artifact"
                );

                // Emit ArtifactFailed event if event_log provided
                if let Some(log) = event_log {
                    log.emit(EventKind::ArtifactFailed {
                        task_id: Arc::from(task_id),
                        path: output_spec.path.clone(),
                        reason: e.to_string(),
                    });
                }

                result.errors.push(format!("{}: {}", output_spec.path, e));
            }
        }
    }

    result
}

/// Write a single artifact output
///
/// Supports `template:` field - if set, resolves template with bindings
/// instead of using task output directly.
#[allow(clippy::too_many_arguments)]
async fn write_single_artifact(
    task_id: &str,
    output: &str,
    output_spec: &ArtifactOutput,
    workflow_config: Option<&ArtifactsConfig>,
    writer: &ArtifactWriter,
    bindings: &ResolvedBindings,
    datastore: &RunContext,
    media_refs: &[MediaRef],
) -> Result<WriteResult, NikaError> {
    // Determine format (task spec > workflow default)
    let format = output_spec
        .format
        .or(workflow_config.map(|c| c.format))
        .unwrap_or(ArtifactFormat::Text);

    // Determine mode (task spec > workflow default) — computed before binary
    // branch so that binary artifacts can validate and reject unsupported modes.
    let mode = output_spec
        .mode
        .or(workflow_config.map(|c| c.mode))
        .unwrap_or(ArtifactMode::Overwrite);

    // Binary format: resolve source to CAS path and copy
    if format == ArtifactFormat::Binary {
        return write_binary_artifact(
            task_id,
            output_spec,
            mode,
            writer,
            bindings,
            datastore,
            media_refs,
        )
        .await;
    }

    // Determine content source: source binding > template > task output
    let raw_content: String = if let Some(ref source_alias) = output_spec.source {
        // Resolve from bindings (with: block or upstream task output)
        debug!(
            task_id = %task_id,
            source = %source_alias,
            "Resolving artifact source binding"
        );
        if let Some(value) = bindings.get(source_alias) {
            match value {
                serde_json::Value::String(s) => s.clone(),
                other => other.to_string(),
            }
        } else {
            // Try datastore (task outputs stored by task ID)
            match datastore.get_output(source_alias) {
                Some(arc_value) => match arc_value.as_ref() {
                    serde_json::Value::String(s) => s.clone(),
                    other => other.to_string(),
                },
                None => {
                    warn!(
                        task_id = %task_id,
                        source = %source_alias,
                        "Artifact source binding not found, falling back to task output"
                    );
                    output.to_string()
                }
            }
        }
    } else if let Some(ref tpl) = output_spec.template {
        // Replace {{output}} with actual task output before template resolution
        let tpl_with_output = tpl.replace("{{output}}", output);

        // Resolve template with bindings (handles {{with.*}}, {{inputs.*}}, etc.)
        debug!(
            task_id = %task_id,
            template = %tpl,
            "Resolving artifact template"
        );
        match template_resolve(&tpl_with_output, bindings, datastore) {
            Ok(resolved) => resolved.into_owned(),
            Err(e) => {
                warn!(
                    task_id = %task_id,
                    template = %tpl,
                    error = %e,
                    "Failed to resolve artifact template, using raw template"
                );
                // On template resolution failure, use the pre-resolved template
                tpl_with_output
            }
        }
    } else {
        // No source or template - use task output directly
        output.to_string()
    };

    // Convert content based on format
    let content = format_output(&raw_content, format)?;

    // Convert ArtifactFormat to OutputFormat for the writer
    let output_format = match format {
        ArtifactFormat::Text => OutputFormat::Text,
        ArtifactFormat::Json => OutputFormat::Json,
        ArtifactFormat::Yaml => OutputFormat::Text, // YAML treated as text for validation
        ArtifactFormat::Binary => OutputFormat::Text, // Binary bypasses format_output entirely
    };

    // Pre-resolve {{with.*}} and {{output}} binding references in the path
    // before the TemplateResolver handles {{task_id}}, {{date}}, etc.
    let resolved_path =
        resolve_artifact_path_bindings(&output_spec.path, output, bindings, datastore);

    // Normalize the artifact path to prevent doubled paths when user specifies
    // full path like ./artifacts/custom.txt instead of just custom.txt
    let artifact_dir_str = workflow_config
        .and_then(|c| c.dir.as_deref())
        .unwrap_or(DEFAULT_ARTIFACT_DIR);
    let normalized_path = normalize_artifact_path(&resolved_path, artifact_dir_str);

    // Build write request - we need to keep output_format for WriteResult
    let request = WriteRequest::new(task_id, &normalized_path)
        .with_content(content)
        .with_format(output_format.clone());

    // Handle different write modes
    match mode {
        ArtifactMode::Overwrite => writer.write(request).await,
        ArtifactMode::Append => {
            // For append mode, we need to use atomic append
            let resolved_path = writer.validate_path(task_id, &normalized_path)?;
            write_append(&resolved_path, request.content.as_bytes())
                .await
                .map_err(|e| NikaError::ArtifactWriteError {
                    path: resolved_path.display().to_string(),
                    reason: format!("Append failed: {}", e),
                })?;
            Ok(WriteResult {
                path: resolved_path,
                size: request.content.len() as u64,
                format: output_format.clone(),
            })
        }
        ArtifactMode::Unique => {
            // For unique mode, generate unique filename
            let resolved_path = writer.validate_path(task_id, &normalized_path)?;
            let unique_path = write_unique(&resolved_path, request.content.as_bytes())
                .await
                .map_err(|e| NikaError::ArtifactWriteError {
                    path: resolved_path.display().to_string(),
                    reason: format!("Unique write failed: {}", e),
                })?;
            Ok(WriteResult {
                path: unique_path,
                size: request.content.len() as u64,
                format: output_format.clone(),
            })
        }
        ArtifactMode::Fail => {
            // For fail mode, error if file exists
            let resolved_path = writer.validate_path(task_id, &normalized_path)?;
            write_fail(&resolved_path, request.content.as_bytes())
                .await
                .map_err(|e| NikaError::ArtifactWriteError {
                    path: resolved_path.display().to_string(),
                    reason: format!("Write failed (file may exist): {}", e),
                })?;
            Ok(WriteResult {
                path: resolved_path,
                size: request.content.len() as u64,
                format: output_format.clone(),
            })
        }
    }
}

/// Write a binary artifact from a media reference.
///
/// Resolves the `source` binding to a media hash or path, then copies from CAS store.
/// Falls back to the first media ref if no explicit source is specified.
///
/// # Mode support
///
/// Binary artifacts only support `Overwrite` (default) and `Fail` modes.
/// `Append` and `Unique` are rejected with NIKA-281 because binary data
/// cannot be meaningfully appended or deduplicated by filename suffix.
async fn write_binary_artifact(
    task_id: &str,
    output_spec: &ArtifactOutput,
    mode: ArtifactMode,
    writer: &ArtifactWriter,
    bindings: &ResolvedBindings,
    datastore: &RunContext,
    media_refs: &[MediaRef],
) -> Result<WriteResult, NikaError> {
    // Reject unsupported modes for binary artifacts
    match mode {
        ArtifactMode::Append => {
            return Err(NikaError::ArtifactWriteError {
                path: output_spec.path.clone(),
                reason: "Binary artifacts do not support append mode".to_string(),
            });
        }
        ArtifactMode::Unique => {
            return Err(NikaError::ArtifactWriteError {
                path: output_spec.path.clone(),
                reason: "Binary artifacts do not support unique mode".to_string(),
            });
        }
        ArtifactMode::Overwrite | ArtifactMode::Fail => {
            // Supported -- continue
        }
    }

    // Resolve source to a MediaRef:
    // 1. If source is specified, look it up in bindings/media_refs
    // 2. Otherwise, use first media ref from the task
    //
    // Resolution order for `source: alias`:
    //   a) Direct match: media_refs.created_by == alias || media_refs.hash == alias
    //   b) Binding indirection: resolve alias -> source task ID -> media_refs.created_by
    //   c) Hash indirection: binding value is a hash string -> media_refs.hash == hash
    let media_ref = if let Some(ref source_alias) = output_spec.source {
        // Try to find media by source alias (could be a task_id or hash)
        // First check if any media ref was created by a task matching the source alias
        let from_media = media_refs
            .iter()
            .find(|m| m.created_by == *source_alias || m.hash == *source_alias);
        if let Some(mr) = from_media {
            mr.clone()
        } else {
            // Try binding indirection: source_alias is a with-binding alias
            // that maps to a task (e.g., `source: img` where `with: { img: $gen_img }`)
            // Resolve the source task ID and find media by created_by.
            let from_binding_source = bindings
                .source_task_id(source_alias)
                .and_then(|task_id| media_refs.iter().find(|m| m.created_by == task_id).cloned());

            if let Some(mr) = from_binding_source {
                mr
            } else {
                // Try resolving from bindings — the value might contain a media hash
                let hash_value = if let Some(value) = bindings.get(source_alias) {
                    match value {
                        serde_json::Value::String(s) => Some(s.clone()),
                        _ => None,
                    }
                } else {
                    datastore
                        .get_output(source_alias)
                        .and_then(|v| match v.as_ref() {
                            serde_json::Value::String(s) => Some(s.clone()),
                            _ => None,
                        })
                };

                if let Some(hash) = hash_value {
                    // Find media ref by hash
                    media_refs
                        .iter()
                        .find(|m| m.hash == hash)
                        .cloned()
                        .ok_or_else(|| NikaError::ArtifactWriteError {
                            path: output_spec.path.clone(),
                            reason: format!(
                                "Binary artifact source '{}' resolved to hash '{}' but no media ref matches",
                                source_alias, hash
                            ),
                        })?
                } else {
                    return Err(NikaError::ArtifactWriteError {
                        path: output_spec.path.clone(),
                        reason: format!(
                            "Binary artifact source '{}' not found in media refs or bindings",
                            source_alias
                        ),
                    });
                }
            }
        }
    } else {
        // No explicit source — use first media ref
        media_refs
            .first()
            .cloned()
            .ok_or_else(|| NikaError::ArtifactWriteError {
                path: output_spec.path.clone(),
                reason: "Binary artifact requires media content but task produced no media"
                    .to_string(),
            })?
    };

    debug!(
        task_id = %task_id,
        hash = %media_ref.hash,
        path = %media_ref.path.display(),
        "Writing binary artifact from CAS"
    );

    // Pre-resolve binding references in the path
    let resolved_path = resolve_artifact_path_bindings(&output_spec.path, "", bindings, datastore);

    // Normalize the artifact path
    let artifact_dir_str = ""; // Binary artifacts use the raw path
    let normalized_path = normalize_artifact_path(&resolved_path, artifact_dir_str);

    // For Fail mode, check that the target does not already exist
    if mode == ArtifactMode::Fail {
        let resolved = writer.validate_path(task_id, &normalized_path)?;
        if resolved.exists() {
            return Err(NikaError::ArtifactWriteError {
                path: resolved.display().to_string(),
                reason: "File already exists and mode is 'fail'".to_string(),
            });
        }
    }

    let request = BinaryWriteRequest {
        task_id: task_id.to_string(),
        output_path: normalized_path,
        source: BinarySource::CasPath(media_ref.path.clone()),
        expected_size: media_ref.size_bytes,
    };

    writer.write_binary(request).await
}

/// Resolve the blake3 checksum for a binary artifact from media refs.
///
/// Looks up the matching MediaRef by source alias or falls back to the first ref.
/// Returns `Some("blake3:...")` if found, `None` otherwise.
fn resolve_binary_checksum(
    output_spec: &ArtifactOutput,
    media_refs: &[MediaRef],
) -> Option<String> {
    if let Some(ref source_alias) = output_spec.source {
        // Match by creator task_id or by hash
        media_refs
            .iter()
            .find(|m| m.created_by == *source_alias || m.hash == *source_alias)
            .map(|m| m.hash.clone())
    } else {
        // No explicit source -- use first media ref (same logic as write_binary_artifact)
        media_refs.first().map(|m| m.hash.clone())
    }
}

/// Format output content based on artifact format
fn format_output(output: &str, format: ArtifactFormat) -> Result<String, NikaError> {
    match format {
        ArtifactFormat::Text => Ok(output.to_string()),
        ArtifactFormat::Json => {
            // Try to parse as JSON and pretty-print
            match serde_json::from_str::<serde_json::Value>(output) {
                Ok(value) => serde_json::to_string_pretty(&value).map_err(|e| {
                    NikaError::ArtifactWriteError {
                        path: "".to_string(),
                        reason: format!("Failed to format JSON: {}", e),
                    }
                }),
                Err(_) => {
                    // If not valid JSON, wrap as string
                    Ok(serde_json::to_string_pretty(&output)
                        .unwrap_or_else(|_| format!("\"{}\"", output)))
                }
            }
        }
        ArtifactFormat::Yaml => {
            // Try to parse as JSON first, then convert to YAML
            match serde_json::from_str::<serde_json::Value>(output) {
                Ok(value) => {
                    serde_yaml::to_string(&value).map_err(|e| NikaError::ArtifactWriteError {
                        path: "".to_string(),
                        reason: format!("Failed to format YAML: {}", e),
                    })
                }
                Err(_) => {
                    // If not valid JSON, just use as-is
                    Ok(output.to_string())
                }
            }
        }
        ArtifactFormat::Binary => {
            // Binary artifacts are handled separately via write_binary()
            // This path should not be reached for binary format
            Err(NikaError::ArtifactWriteError {
                path: "".to_string(),
                reason: "Binary format must be written via write_binary(), not format_output()"
                    .to_string(),
            })
        }
    }
}

/// Resolve artifact directory from workflow config
///
/// Creates the directory if it doesn't exist and canonicalizes the path
/// to avoid macOS symlink issues (e.g., /var -> /private/var).
async fn resolve_artifact_dir(
    workflow_config: Option<&ArtifactsConfig>,
    base_path: &std::path::Path,
) -> PathBuf {
    let dir_str = workflow_config
        .and_then(|c| c.dir.as_deref())
        .unwrap_or(DEFAULT_ARTIFACT_DIR);

    let artifact_dir = base_path.join(dir_str);

    // Create directory if it doesn't exist (non-blocking)
    if !artifact_dir.exists() {
        if let Err(e) = tokio::fs::create_dir_all(&artifact_dir).await {
            tracing::warn!(
                path = %artifact_dir.display(),
                error = %e,
                "Failed to create artifact directory"
            );
            return artifact_dir;
        }
    }

    // Canonicalize to resolve symlinks (important for macOS /var -> /private/var)
    artifact_dir.canonicalize().unwrap_or(artifact_dir)
}

/// Sanitize a value for safe use in file paths.
///
/// Replaces path-dangerous characters with underscores and truncates
/// to prevent excessively long paths. This is the security boundary
/// where user-controlled binding values enter the filesystem path context.
fn sanitize_for_path(value: &str) -> String {
    value
        .replace(['/', '\\', ':'], "_")
        .replace('\0', "")
        .replace("..", "_")
        .replace('~', "_")
        .chars()
        .take(200)
        .collect::<String>()
        .trim()
        .to_string()
}

/// Pre-resolve `{{with.*}}` and `{{output}}` binding references in an artifact path.
///
/// This is a targeted pre-pass that resolves binding-based templates in artifact
/// paths before they reach the `TemplateResolver` (which handles `{{task_id}}`,
/// `{{date}}`, etc.). The two template systems remain independent.
///
/// Supported patterns:
/// - `{{with.alias}}` — Resolves from the task's `with:` bindings
/// - `{{output}}` — Resolves to the current task's output (sanitized)
///
/// Values are sanitized via `sanitize_for_path()` to prevent path traversal.
/// Unresolved `{{with.*}}` references are left as-is (will error in TemplateResolver).
fn resolve_artifact_path_bindings(
    path: &str,
    output: &str,
    bindings: &ResolvedBindings,
    datastore: &RunContext,
) -> String {
    let mut result = path.to_string();
    let mut pos = 0;

    while let Some(start) = result[pos..].find("{{") {
        let start = pos + start;
        let Some(end) = result[start..].find("}}") else {
            break;
        };
        let end = start + end + 2;

        let var_name = result[start + 2..end - 2].trim();

        if var_name == "output" {
            let sanitized = sanitize_for_path(output.trim());
            result.replace_range(start..end, &sanitized);
            pos = start + sanitized.len();
        } else if let Some(alias) = var_name.strip_prefix("with.") {
            // Extract top-level alias (e.g., "with.timestamp" → "timestamp")
            let top_alias = alias.split('.').next().unwrap_or(alias);

            // Check for media paths: {{with.alias.media[N].field}}
            // Media refs live in the TaskResult side-channel, not in the task
            // output value, so we must resolve via datastore.resolve_path()
            // using the original source task ID.
            let nested_path = alias.split_once('.').map(|x| x.1).unwrap_or("");
            let is_media_path = nested_path == "media"
                || nested_path.starts_with("media.")
                || nested_path.starts_with("media[");

            if is_media_path {
                // Resolve media path via datastore using source task ID
                if let Some(source_task_id) = bindings.source_task_id(top_alias) {
                    let full_path = format!("{}.{}", source_task_id, nested_path);
                    if let Some(value) = datastore.resolve_path(&full_path) {
                        let raw_value = match &value {
                            serde_json::Value::String(s) => s.clone(),
                            other => other.to_string(),
                        };
                        let sanitized = sanitize_for_path(&raw_value);
                        result.replace_range(start..end, &sanitized);
                        pos = start + sanitized.len();
                    } else {
                        pos = end;
                    }
                } else {
                    pos = end;
                }
            } else if let Some(value) = bindings.get(top_alias) {
                // For nested paths like "with.data.name", do JSONPath-like access
                let raw_value = if alias.contains('.') {
                    // Navigate into the JSON value
                    let parts: Vec<&str> = alias.splitn(2, '.').collect();
                    if parts.len() == 2 {
                        json_path_value(value, parts[1])
                    } else {
                        value_to_string(value)
                    }
                } else {
                    value_to_string(value)
                };
                let sanitized = sanitize_for_path(&raw_value);
                result.replace_range(start..end, &sanitized);
                pos = start + sanitized.len();
            } else {
                // Unknown alias — leave as-is for TemplateResolver to handle/error
                pos = end;
            }
        } else if let Some(input_path) = var_name.strip_prefix("inputs.") {
            // Resolve {{inputs.param}} from datastore
            let full_path = format!("inputs.{}", input_path);
            if let Some(value) = datastore.resolve_input_path(&full_path) {
                let raw_value = match &value {
                    serde_json::Value::String(s) => s.clone(),
                    other => other.to_string(),
                };
                let sanitized = sanitize_for_path(&raw_value);
                result.replace_range(start..end, &sanitized);
                pos = start + sanitized.len();
            } else {
                pos = end;
            }
        } else {
            // Not a binding reference (e.g., {{task_id}}, {{date}}) — skip
            pos = end;
        }
    }

    result
}

/// Convert a serde_json::Value to a path-friendly string
fn value_to_string(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Null => "null".to_string(),
        // Arrays and objects get compact JSON representation
        other => other.to_string(),
    }
}

/// Simple dot-path navigation into a serde_json::Value
fn json_path_value(value: &serde_json::Value, path: &str) -> String {
    let mut current = value;
    for part in path.split('.') {
        match current {
            serde_json::Value::Object(map) => {
                if let Some(next) = map.get(part) {
                    current = next;
                } else {
                    return format!("{{{{with.{}}}}}", path);
                }
            }
            serde_json::Value::Array(arr) => {
                if let Ok(idx) = part.parse::<usize>() {
                    if let Some(next) = arr.get(idx) {
                        current = next;
                    } else {
                        return format!("{{{{with.{}}}}}", path);
                    }
                } else {
                    return format!("{{{{with.{}}}}}", path);
                }
            }
            _ => return format!("{{{{with.{}}}}}", path),
        }
    }
    value_to_string(current)
}

/// Normalize artifact output path to prevent doubled paths
///
/// If the artifact path starts with `./` and contains the artifact_dir path,
/// strip the redundant prefix to prevent paths like:
/// `artifacts/./artifacts/custom.txt` → `artifacts/custom.txt`
///
/// This handles the common user mistake of specifying full paths in artifact spec.
fn normalize_artifact_path(path: &str, artifact_dir_str: &str) -> String {
    let path = path.trim();
    let artifact_dir = artifact_dir_str
        .trim_start_matches("./")
        .trim_end_matches('/');

    // Check if path starts with ./ and contains the artifact_dir
    if path.starts_with("./") {
        let path_without_dot = path.trim_start_matches("./");
        // If path starts with artifact_dir, strip it to get the relative part
        if path_without_dot.starts_with(artifact_dir) {
            let relative = path_without_dot
                .trim_start_matches(artifact_dir)
                .trim_start_matches('/');
            if !relative.is_empty() {
                debug!(
                    original = %path,
                    normalized = %relative,
                    "Normalized artifact path (removed redundant prefix)"
                );
                return relative.to_string();
            }
        }
    }

    path.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_format_output_text() {
        let result = format_output("hello world", ArtifactFormat::Text);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "hello world");
    }

    #[test]
    fn test_format_output_json_valid() {
        let result = format_output(r#"{"key":"value"}"#, ArtifactFormat::Json);
        assert!(result.is_ok());
        let formatted = result.unwrap();
        assert!(formatted.contains("key"));
        assert!(formatted.contains("value"));
    }

    #[test]
    fn test_format_output_json_invalid() {
        let result = format_output("not json", ArtifactFormat::Json);
        assert!(result.is_ok());
        // Should be wrapped as JSON string
        let formatted = result.unwrap();
        assert!(formatted.contains("not json"));
    }

    #[test]
    fn test_format_output_yaml() {
        let result = format_output(r#"{"key":"value"}"#, ArtifactFormat::Yaml);
        assert!(result.is_ok());
        let formatted = result.unwrap();
        assert!(formatted.contains("key"));
    }

    #[tokio::test]
    async fn test_resolve_artifact_dir_default() {
        let base = PathBuf::from("/project");
        let dir = resolve_artifact_dir(None, &base).await;
        assert_eq!(dir, PathBuf::from("/project/.nika/artifacts"));
    }

    #[tokio::test]
    async fn test_resolve_artifact_dir_custom() {
        let base = PathBuf::from("/project");
        let config = ArtifactsConfig {
            dir: Some("output".to_string()),
            ..Default::default()
        };
        let dir = resolve_artifact_dir(Some(&config), &base).await;
        assert_eq!(dir, PathBuf::from("/project/output"));
    }

    #[tokio::test]
    async fn test_process_task_artifacts_disabled() {
        let base = tempdir().unwrap();
        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();
        let result = process_task_artifacts(
            "task1",
            "output",
            &ArtifactSpec::Enabled(false),
            None,
            base.path(),
            None, // No event log for tests
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(result.written, 0);
        assert!(result.paths.is_empty());
        assert!(result.errors.is_empty());
    }

    #[tokio::test]
    async fn test_process_task_artifacts_enabled() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        let result = process_task_artifacts(
            "task1",
            "test output",
            &ArtifactSpec::Enabled(true),
            None,
            base.path(),
            None, // No event log for tests
            &bindings,
            &datastore,
            &[],
        )
        .await;

        // Print errors for debugging
        if !result.errors.is_empty() {
            eprintln!("Artifact errors: {:?}", result.errors);
        }

        assert_eq!(
            result.written, 1,
            "Expected 1 artifact written, errors: {:?}",
            result.errors
        );
        assert!(!result.paths.is_empty());
        assert!(
            result.errors.is_empty(),
            "Unexpected errors: {:?}",
            result.errors
        );
    }

    #[tokio::test]
    async fn test_process_task_artifacts_single() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.json".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Json),
            mode: None,
        });

        let result = process_task_artifacts(
            "task1",
            r#"{"result": "success"}"#,
            &spec,
            None,
            base.path(),
            None, // No event log for tests
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(result.written, 1);
        assert!(result.paths[0].ends_with("output.json"));
    }

    #[tokio::test]
    async fn test_process_task_artifacts_multiple() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        let spec = ArtifactSpec::Multiple(vec![
            ArtifactOutput {
                path: "raw.txt".to_string(),
                source: None,
                template: None,
                format: Some(ArtifactFormat::Text),
                mode: None,
            },
            ArtifactOutput {
                path: "processed.json".to_string(),
                source: None,
                template: None,
                format: Some(ArtifactFormat::Json),
                mode: None,
            },
        ]);

        let result = process_task_artifacts(
            "task1",
            "test data",
            &spec,
            None,
            base.path(),
            None, // No event log for tests
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(result.written, 2);
        assert_eq!(result.paths.len(), 2);
    }

    // ========== BUG-3: artifact source resolution ==========

    #[tokio::test]
    async fn test_artifact_source_from_binding() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();

        // Set up bindings with a "report_data" alias
        let mut bindings = ResolvedBindings::new();
        bindings.set(
            "report_data".to_string(),
            serde_json::Value::String("Content from binding source".to_string()),
        );
        let datastore = RunContext::new();

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "report.txt".to_string(),
            source: Some("report_data".to_string()),
            template: None,
            format: Some(ArtifactFormat::Text),
            mode: None,
        });

        let result = process_task_artifacts(
            "task1",
            "this is the task output (should NOT be written)",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(result.written, 1, "artifact should be written");
        assert!(result.errors.is_empty(), "no errors expected");

        // Verify file content comes from source binding, not task output
        let content = std::fs::read_to_string(&result.paths[0]).unwrap();
        assert_eq!(content, "Content from binding source");
        assert!(!content.contains("should NOT be written"));
    }

    #[tokio::test]
    async fn test_artifact_source_fallback_to_task_output() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::new();
        let datastore = RunContext::new();

        // source points to a non-existent binding → should fall back to task output
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "fallback.txt".to_string(),
            source: Some("nonexistent".to_string()),
            template: None,
            format: Some(ArtifactFormat::Text),
            mode: None,
        });

        let result = process_task_artifacts(
            "task1",
            "task output fallback",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(result.written, 1);
        let content = std::fs::read_to_string(&result.paths[0]).unwrap();
        assert_eq!(content, "task output fallback");
    }

    // ========== normalize_artifact_path tests ==========

    #[test]
    fn test_normalize_artifact_path_simple_filename() {
        // Simple filename should not be modified
        let result = normalize_artifact_path("custom.txt", "./examples/.test-output/artifacts");
        assert_eq!(result, "custom.txt");
    }

    #[test]
    fn test_normalize_artifact_path_doubled_path() {
        // Doubled path should be normalized
        let result = normalize_artifact_path(
            "./examples/.test-output/artifacts/custom.txt",
            "./examples/.test-output/artifacts",
        );
        assert_eq!(result, "custom.txt");
    }

    #[test]
    fn test_normalize_artifact_path_nested_doubled() {
        // Nested doubled path should be normalized
        let result =
            normalize_artifact_path("./output/artifacts/subdir/file.json", "./output/artifacts");
        assert_eq!(result, "subdir/file.json");
    }

    #[test]
    fn test_normalize_artifact_path_no_leading_dot() {
        // Path without leading ./ should not be modified
        let result = normalize_artifact_path("subdir/file.txt", "./artifacts");
        assert_eq!(result, "subdir/file.txt");
    }

    #[test]
    fn test_normalize_artifact_path_different_prefix() {
        // Path that doesn't match artifact_dir should not be modified
        let result = normalize_artifact_path("./other/path/file.txt", "./artifacts");
        assert_eq!(result, "./other/path/file.txt");
    }

    #[test]
    fn test_normalize_artifact_path_default_dir() {
        // Works with default artifact directory
        let result = normalize_artifact_path("./.nika/artifacts/output.json", ".nika/artifacts");
        assert_eq!(result, "output.json");
    }

    // ========== Template resolution tests ==========

    #[tokio::test]
    async fn test_artifact_template_resolution() {
        use crate::store::TaskResult;
        use std::sync::Arc;
        use std::time::Duration;

        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();

        // Create datastore with task result that has JSON data
        let datastore = RunContext::new();
        let task_result = TaskResult::success_str(
            r#"{"name": "Alice", "age": 30}"#.to_string(),
            Duration::from_millis(100),
        );
        datastore.insert(Arc::from("generate_data"), task_result);

        // Create bindings that reference the upstream task
        let mut bindings = ResolvedBindings::default();
        bindings.set("data", serde_json::json!({"name": "Alice", "age": 30}));

        // Create artifact spec with template
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "report.md".to_string(),
            source: None,
            template: Some(
                "# Report\n\nUser: {{with.data.name}}, Age: {{with.data.age}}".to_string(),
            ),
            format: Some(ArtifactFormat::Text),
            mode: None,
        });

        let result = process_task_artifacts(
            "generate_report",
            "task output (ignored when template is set)",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(
            result.written, 1,
            "Expected 1 artifact written, errors: {:?}",
            result.errors
        );
        assert!(
            result.errors.is_empty(),
            "Unexpected errors: {:?}",
            result.errors
        );

        // Read the artifact content and verify template was resolved
        let artifact_content = std::fs::read_to_string(&result.paths[0]).unwrap();
        assert_eq!(artifact_content, "# Report\n\nUser: Alice, Age: 30");
    }

    #[tokio::test]
    async fn test_artifact_without_template_uses_output() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        // Create artifact spec WITHOUT template
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.txt".to_string(),
            source: None,
            template: None, // No template - should use task output
            format: Some(ArtifactFormat::Text),
            mode: None,
        });

        let result = process_task_artifacts(
            "task1",
            "This is the task output",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(result.written, 1);

        // Read the artifact content and verify it's the task output
        let artifact_content = std::fs::read_to_string(&result.paths[0]).unwrap();
        assert_eq!(artifact_content, "This is the task output");
    }

    #[tokio::test]
    async fn test_artifact_template_with_missing_binding() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::default(); // Empty bindings
        let datastore = RunContext::new();

        // Create artifact spec with template that references missing binding
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "report.md".to_string(),
            source: None,
            template: Some("Hello {{with.missing}}!".to_string()),
            format: Some(ArtifactFormat::Text),
            mode: None,
        });

        let result = process_task_artifacts(
            "task1",
            "fallback output",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[],
        )
        .await;

        // Should still write, but with raw template (on resolution error)
        assert_eq!(result.written, 1);

        let artifact_content = std::fs::read_to_string(&result.paths[0]).unwrap();
        // On template resolution failure, it uses the raw template
        assert_eq!(artifact_content, "Hello {{with.missing}}!");
    }

    // ========== resolve_artifact_path_bindings tests ==========

    #[test]
    fn test_path_bindings_with_alias() {
        let mut bindings = ResolvedBindings::default();
        bindings.set("timestamp", serde_json::json!("2024-01-15_14-30-00"));

        let result = resolve_artifact_path_bindings(
            "./outputs/result-{{with.timestamp}}.json",
            "task output",
            &bindings,
            &RunContext::new(),
        );
        assert_eq!(result, "./outputs/result-2024-01-15_14-30-00.json");
    }

    #[test]
    fn test_path_bindings_output() {
        let bindings = ResolvedBindings::default();

        let result = resolve_artifact_path_bindings(
            "./outputs/{{output}}.json",
            "my-report",
            &bindings,
            &RunContext::new(),
        );
        assert_eq!(result, "./outputs/my-report.json");
    }

    #[test]
    fn test_path_bindings_mixed_with_builtins() {
        let mut bindings = ResolvedBindings::default();
        bindings.set("locale", serde_json::json!("fr-FR"));

        // {{with.locale}} should resolve, {{task_id}} should be left for TemplateResolver
        let result = resolve_artifact_path_bindings(
            "{{task_id}}/{{with.locale}}/output.json",
            "",
            &bindings,
            &RunContext::new(),
        );
        assert_eq!(result, "{{task_id}}/fr-FR/output.json");
    }

    #[test]
    fn test_path_bindings_nested_json() {
        let mut bindings = ResolvedBindings::default();
        bindings.set("meta", serde_json::json!({"slug": "qr-code", "version": 2}));

        let result = resolve_artifact_path_bindings(
            "./outputs/{{with.meta.slug}}-v{{with.meta.version}}.json",
            "",
            &bindings,
            &RunContext::new(),
        );
        assert_eq!(result, "./outputs/qr-code-v2.json");
    }

    #[test]
    fn test_path_bindings_sanitizes_slashes() {
        let mut bindings = ResolvedBindings::default();
        bindings.set("name", serde_json::json!("../../etc/passwd"));

        let result = resolve_artifact_path_bindings(
            "./outputs/{{with.name}}.txt",
            "",
            &bindings,
            &RunContext::new(),
        );
        // Path traversal characters should be sanitized
        assert!(!result.contains(".."));
        assert!(!result.contains("etc/passwd"));
    }

    #[test]
    fn test_path_bindings_sanitizes_output() {
        let bindings = ResolvedBindings::default();

        let result = resolve_artifact_path_bindings(
            "./outputs/{{output}}.txt",
            "../../../etc/passwd",
            &bindings,
            &RunContext::new(),
        );
        assert!(!result.contains("../"));
        assert!(!result.contains("etc/passwd"));
    }

    #[test]
    fn test_path_bindings_unknown_alias_preserved() {
        let bindings = ResolvedBindings::default();

        // Unknown binding should be left as-is
        let result = resolve_artifact_path_bindings(
            "./outputs/{{with.unknown}}.json",
            "",
            &bindings,
            &RunContext::new(),
        );
        assert_eq!(result, "./outputs/{{with.unknown}}.json");
    }

    #[test]
    fn test_path_bindings_no_bindings_passthrough() {
        let bindings = ResolvedBindings::default();

        // Path with no binding references should pass through unchanged
        let result = resolve_artifact_path_bindings(
            "{{task_id}}/{{date}}/output.json",
            "",
            &bindings,
            &RunContext::new(),
        );
        assert_eq!(result, "{{task_id}}/{{date}}/output.json");
    }

    #[test]
    fn test_path_bindings_truncates_long_values() {
        let mut bindings = ResolvedBindings::default();
        let long_value = "a".repeat(300);
        bindings.set("name", serde_json::json!(long_value));

        let result =
            resolve_artifact_path_bindings("{{with.name}}.txt", "", &bindings, &RunContext::new());
        // sanitize_for_path truncates to 200 chars
        assert!(result.len() <= 204); // 200 + ".txt"
    }

    #[tokio::test]
    async fn test_e2e_artifact_path_with_bindings() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();

        let mut bindings = ResolvedBindings::default();
        bindings.set("timestamp", serde_json::json!("2024-01-15_14-30-00"));

        let datastore = RunContext::new();

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "result-{{with.timestamp}}.json".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Json),
            mode: None,
        });

        let result = process_task_artifacts(
            "save_result",
            r#"{"status": "ok"}"#,
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[],
        )
        .await;

        assert_eq!(
            result.written, 1,
            "Expected 1 artifact written, errors: {:?}",
            result.errors
        );
        assert!(
            result.paths[0]
                .display()
                .to_string()
                .contains("result-2024-01-15_14-30-00.json"),
            "Expected resolved path, got: {}",
            result.paths[0].display()
        );
    }

    // ========== sanitize_for_path tests ==========

    #[test]
    fn test_sanitize_for_path_clean() {
        assert_eq!(sanitize_for_path("hello-world"), "hello-world");
    }

    #[test]
    fn test_sanitize_for_path_slashes() {
        assert_eq!(sanitize_for_path("a/b/c"), "a_b_c");
    }

    #[test]
    fn test_sanitize_for_path_backslashes() {
        assert_eq!(sanitize_for_path("a\\b\\c"), "a_b_c");
    }

    #[test]
    fn test_sanitize_for_path_dotdot() {
        assert_eq!(sanitize_for_path("../escape"), "__escape");
    }

    #[test]
    fn test_sanitize_for_path_null() {
        assert_eq!(sanitize_for_path("a\0b"), "ab");
    }

    #[test]
    fn test_sanitize_for_path_tilde() {
        assert_eq!(sanitize_for_path("~/home"), "__home");
    }

    #[test]
    fn test_sanitize_for_path_truncation() {
        let long = "x".repeat(300);
        assert_eq!(sanitize_for_path(&long).len(), 200);
    }

    // ========== Binary artifact tests ==========

    #[tokio::test]
    async fn test_process_binary_artifact_from_media_ref() {
        use crate::media::MediaRef;

        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();

        // Create a fake CAS file
        let cas_dir = base.path().join(".nika/media/store/ab");
        std::fs::create_dir_all(&cas_dir).unwrap();
        let cas_file = cas_dir.join("cdef1234");
        let binary_data = b"\x89PNG\r\n\x1a\n fake image data";
        std::fs::write(&cas_file, binary_data).unwrap();

        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        let media_refs = vec![MediaRef {
            hash: "blake3:abcdef1234".to_string(),
            mime_type: "image/png".to_string(),
            size_bytes: binary_data.len() as u64,
            path: cas_file.clone(),
            extension: "png".to_string(),
            created_by: "gen_img".to_string(),
            metadata: serde_json::Map::new(),
        }];

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output/image.bin".to_string(),
            source: None, // Use first media ref
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: None,
        });

        let result = process_task_artifacts(
            "gen_img",
            "text output (ignored for binary)",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;

        assert_eq!(
            result.written, 1,
            "Expected 1 binary artifact, errors: {:?}",
            result.errors
        );
        assert!(
            result.errors.is_empty(),
            "Unexpected errors: {:?}",
            result.errors
        );

        // Verify file was copied correctly
        let written = std::fs::read(&result.paths[0]).unwrap();
        assert_eq!(written, binary_data);
    }

    #[tokio::test]
    async fn test_process_binary_artifact_with_source() {
        use crate::media::MediaRef;

        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();

        // Create two fake CAS files
        let cas_dir = base.path().join(".nika/media/store/ab");
        std::fs::create_dir_all(&cas_dir).unwrap();
        let cas_file1 = cas_dir.join("file1");
        let cas_file2 = cas_dir.join("file2");
        std::fs::write(&cas_file1, b"image data 1").unwrap();
        std::fs::write(&cas_file2, b"image data 2").unwrap();

        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        let media_refs = vec![
            MediaRef {
                hash: "blake3:hash1".to_string(),
                mime_type: "image/png".to_string(),
                size_bytes: 12,
                path: cas_file1,
                extension: "png".to_string(),
                created_by: "gen_img".to_string(),
                metadata: serde_json::Map::new(),
            },
            MediaRef {
                hash: "blake3:hash2".to_string(),
                mime_type: "image/jpeg".to_string(),
                size_bytes: 12,
                path: cas_file2.clone(),
                extension: "jpg".to_string(),
                created_by: "gen_thumb".to_string(),
                metadata: serde_json::Map::new(),
            },
        ];

        // Specify source by creator task_id
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output/thumb.bin".to_string(),
            source: Some("gen_thumb".to_string()),
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: None,
        });

        let result = process_task_artifacts(
            "save_thumb",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;

        assert_eq!(result.written, 1, "errors: {:?}", result.errors);
        let written = std::fs::read(&result.paths[0]).unwrap();
        assert_eq!(written, b"image data 2");
    }

    // ========== Binary artifact edge cases ==========

    #[tokio::test]
    async fn test_binary_artifact_missing_source_binding_error() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.bin".to_string(),
            source: Some("nonexistent_source".to_string()),
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: None,
        });

        let result = process_task_artifacts(
            "task1",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[], // No media refs
        )
        .await;

        assert_eq!(result.written, 0);
        assert_eq!(result.errors.len(), 1);
        assert!(
            result.errors[0].contains("not found"),
            "Error should mention source not found: {}",
            result.errors[0]
        );
    }

    #[tokio::test]
    async fn test_binary_artifact_no_media_no_source_error() {
        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();
        let bindings = ResolvedBindings::default();
        let datastore = RunContext::new();

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.bin".to_string(),
            source: None, // No source specified
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: None,
        });

        let result = process_task_artifacts(
            "task1",
            "text output",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &[], // No media refs either
        )
        .await;

        assert_eq!(result.written, 0);
        assert_eq!(result.errors.len(), 1);
        assert!(
            result.errors[0].contains("no media"),
            "Error should mention no media: {}",
            result.errors[0]
        );
    }

    // ═══════════════════════════════════════════════════
    // Binary artifact mode validation tests
    // ═══════════════════════════════════════════════════

    fn setup_binary_mode_fixtures() -> (
        tempfile::TempDir,
        Vec<crate::media::MediaRef>,
        ResolvedBindings,
        RunContext,
    ) {
        use crate::media::MediaRef;
        let base = tempdir().unwrap();
        std::fs::create_dir_all(base.path().join(".nika/artifacts")).unwrap();
        let cas_dir = base.path().join(".nika/media/store/ab");
        std::fs::create_dir_all(&cas_dir).unwrap();
        let cas_file = cas_dir.join("testbin");
        std::fs::write(&cas_file, b"binary payload").unwrap();
        let media_refs = vec![MediaRef {
            hash: "blake3:testbin".to_string(),
            mime_type: "application/octet-stream".to_string(),
            size_bytes: 14,
            path: cas_file,
            extension: "bin".to_string(),
            created_by: "producer".to_string(),
            metadata: serde_json::Map::new(),
        }];
        (
            base,
            media_refs,
            ResolvedBindings::default(),
            RunContext::new(),
        )
    }

    #[tokio::test]
    async fn test_binary_mode_append_is_rejected() {
        let (base, media_refs, bindings, datastore) = setup_binary_mode_fixtures();
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.bin".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: Some(ArtifactMode::Append),
        });
        let result = process_task_artifacts(
            "producer",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;
        assert_eq!(result.written, 0, "Append mode must be rejected for binary");
        assert_eq!(result.errors.len(), 1);
        assert!(
            result.errors[0].contains("Binary artifacts do not support append mode"),
            "got: {}",
            result.errors[0]
        );
    }

    #[tokio::test]
    async fn test_binary_mode_unique_is_rejected() {
        let (base, media_refs, bindings, datastore) = setup_binary_mode_fixtures();
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.bin".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: Some(ArtifactMode::Unique),
        });
        let result = process_task_artifacts(
            "producer",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;
        assert_eq!(result.written, 0, "Unique mode must be rejected for binary");
        assert_eq!(result.errors.len(), 1);
        assert!(
            result.errors[0].contains("Binary artifacts do not support unique mode"),
            "got: {}",
            result.errors[0]
        );
    }

    #[tokio::test]
    async fn test_binary_mode_overwrite_succeeds() {
        let (base, media_refs, bindings, datastore) = setup_binary_mode_fixtures();
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.bin".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: Some(ArtifactMode::Overwrite),
        });
        let result = process_task_artifacts(
            "producer",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;
        assert_eq!(
            result.written, 1,
            "Overwrite should work, errors: {:?}",
            result.errors
        );
        assert!(result.errors.is_empty());
        assert_eq!(std::fs::read(&result.paths[0]).unwrap(), b"binary payload");
    }

    #[tokio::test]
    async fn test_binary_mode_fail_rejects_existing_file() {
        let (base, media_refs, bindings, datastore) = setup_binary_mode_fixtures();
        // Pre-create the target so fail mode triggers
        let target = base.path().join(".nika/artifacts/output.bin");
        std::fs::create_dir_all(target.parent().unwrap()).unwrap();
        std::fs::write(&target, b"existing data").unwrap();
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output.bin".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: Some(ArtifactMode::Fail),
        });
        let result = process_task_artifacts(
            "producer",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;
        assert_eq!(result.written, 0, "Fail mode should reject existing file");
        assert_eq!(result.errors.len(), 1);
        assert!(
            result.errors[0].contains("already exists"),
            "got: {}",
            result.errors[0]
        );
    }

    #[tokio::test]
    async fn test_binary_mode_fail_succeeds_for_new_file() {
        let (base, media_refs, bindings, datastore) = setup_binary_mode_fixtures();
        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "fresh_output.bin".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: Some(ArtifactMode::Fail),
        });
        let result = process_task_artifacts(
            "producer",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;
        assert_eq!(
            result.written, 1,
            "Fail mode should succeed for new file, errors: {:?}",
            result.errors
        );
        assert!(result.errors.is_empty());
        assert_eq!(std::fs::read(&result.paths[0]).unwrap(), b"binary payload");
    }

    // ========== Media binding template tests (source_task_id tracking) ==========

    #[test]
    fn test_path_bindings_media_hash_via_source_task() {
        use crate::media::MediaRef;
        use crate::store::TaskResult;
        use std::sync::Arc;
        use std::time::Duration;

        let datastore = RunContext::new();
        let mut task_result =
            TaskResult::success_str("LLM text output".to_string(), Duration::from_millis(100));
        task_result.media = vec![MediaRef {
            hash: "blake3:af1349b9".to_string(),
            mime_type: "image/png".to_string(),
            size_bytes: 4096,
            path: std::path::PathBuf::from("/tmp/cas/af/1349b9"),
            extension: "png".to_string(),
            created_by: "gen_img".to_string(),
            metadata: serde_json::Map::new(),
        }];
        datastore.insert(Arc::from("gen_img"), task_result);

        let mut bindings = ResolvedBindings::new();
        bindings.set_with_source("img", serde_json::json!("LLM text output"), "gen_img");

        let result = resolve_artifact_path_bindings(
            "output/{{with.img.media[0].hash}}.bin",
            "",
            &bindings,
            &datastore,
        );
        assert_eq!(
            result, "output/blake3_af1349b9.bin",
            "Media hash should resolve via source task ID, with : sanitized to _"
        );
    }

    #[test]
    fn test_path_bindings_media_extension_via_source_task() {
        use crate::media::MediaRef;
        use crate::store::TaskResult;
        use std::sync::Arc;
        use std::time::Duration;

        let datastore = RunContext::new();
        let mut task_result =
            TaskResult::success_str("output".to_string(), Duration::from_millis(50));
        task_result.media = vec![MediaRef {
            hash: "blake3:deadbeef".to_string(),
            mime_type: "image/png".to_string(),
            size_bytes: 1024,
            path: std::path::PathBuf::from("/tmp/cas/de/adbeef"),
            extension: "png".to_string(),
            created_by: "gen_img".to_string(),
            metadata: serde_json::Map::new(),
        }];
        datastore.insert(Arc::from("gen_img"), task_result);

        let mut bindings = ResolvedBindings::new();
        bindings.set_with_source("img", serde_json::json!("output"), "gen_img");

        let result = resolve_artifact_path_bindings(
            "output/{{with.img.media[0].extension}}/result.bin",
            "",
            &bindings,
            &datastore,
        );
        assert_eq!(
            result, "output/png/result.bin",
            "Media extension should resolve via source task ID"
        );
    }

    #[test]
    fn test_path_bindings_media_without_source_task_unresolved() {
        let bindings = ResolvedBindings::new();
        let datastore = RunContext::new();

        let result = resolve_artifact_path_bindings(
            "output/{{with.img.media[0].hash}}.bin",
            "",
            &bindings,
            &datastore,
        );
        assert_eq!(
            result, "output/{{with.img.media[0].hash}}.bin",
            "Without source task tracking, media path should remain unresolved"
        );
    }

    #[tokio::test]
    async fn test_binary_artifact_source_via_binding_alias() {
        use crate::media::MediaRef;
        use crate::store::TaskResult;
        use std::sync::Arc;
        use std::time::Duration;

        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();

        let cas_dir = base.path().join(".nika/media/store/ab");
        std::fs::create_dir_all(&cas_dir).unwrap();
        let cas_file = cas_dir.join("cdef1234");
        let binary_data = b"\x89PNG fake image";
        std::fs::write(&cas_file, binary_data).unwrap();

        let datastore = RunContext::new();
        let mut task_result =
            TaskResult::success_str("generated image".to_string(), Duration::from_millis(100));
        task_result.media = vec![MediaRef {
            hash: "blake3:abcdef1234".to_string(),
            mime_type: "image/png".to_string(),
            size_bytes: binary_data.len() as u64,
            path: cas_file.clone(),
            extension: "png".to_string(),
            created_by: "gen_img".to_string(),
            metadata: serde_json::Map::new(),
        }];
        datastore.insert(Arc::from("gen_img"), task_result);

        let mut bindings = ResolvedBindings::new();
        bindings.set_with_source("img", serde_json::json!("generated image"), "gen_img");

        let media_refs = vec![MediaRef {
            hash: "blake3:abcdef1234".to_string(),
            mime_type: "image/png".to_string(),
            size_bytes: binary_data.len() as u64,
            path: cas_file,
            extension: "png".to_string(),
            created_by: "gen_img".to_string(),
            metadata: serde_json::Map::new(),
        }];

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output/image.bin".to_string(),
            source: Some("img".to_string()),
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: None,
        });

        let result = process_task_artifacts(
            "save_img",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;

        assert_eq!(
            result.written, 1,
            "Binary artifact should resolve via binding alias indirection, errors: {:?}",
            result.errors
        );
        assert!(
            result.errors.is_empty(),
            "No errors expected: {:?}",
            result.errors
        );

        let written = std::fs::read(&result.paths[0]).unwrap();
        assert_eq!(
            written, binary_data,
            "Binary content should match CAS source"
        );
    }

    #[tokio::test]
    async fn test_binary_artifact_path_with_media_extension_template() {
        use crate::media::MediaRef;
        use crate::store::TaskResult;
        use std::sync::Arc;
        use std::time::Duration;

        let base = tempdir().unwrap();
        let artifact_dir = base.path().join(".nika/artifacts");
        std::fs::create_dir_all(&artifact_dir).unwrap();

        let cas_dir = base.path().join(".nika/media/store/xx");
        std::fs::create_dir_all(&cas_dir).unwrap();
        let cas_file = cas_dir.join("yy1234");
        let binary_data = b"image bytes";
        std::fs::write(&cas_file, binary_data).unwrap();

        let datastore = RunContext::new();
        let mut task_result =
            TaskResult::success_str("done".to_string(), Duration::from_millis(50));
        task_result.media = vec![MediaRef {
            hash: "blake3:xxyy1234".to_string(),
            mime_type: "image/jpeg".to_string(),
            size_bytes: binary_data.len() as u64,
            path: cas_file.clone(),
            extension: "jpg".to_string(),
            created_by: "gen_img".to_string(),
            metadata: serde_json::Map::new(),
        }];
        datastore.insert(Arc::from("gen_img"), task_result);

        let mut bindings = ResolvedBindings::new();
        bindings.set_with_source("img", serde_json::json!("done"), "gen_img");

        let media_refs = vec![MediaRef {
            hash: "blake3:xxyy1234".to_string(),
            mime_type: "image/jpeg".to_string(),
            size_bytes: binary_data.len() as u64,
            path: cas_file,
            extension: "jpg".to_string(),
            created_by: "gen_img".to_string(),
            metadata: serde_json::Map::new(),
        }];

        let spec = ArtifactSpec::Single(ArtifactOutput {
            path: "output/result.{{with.img.media[0].extension}}".to_string(),
            source: None,
            template: None,
            format: Some(ArtifactFormat::Binary),
            mode: None,
        });

        let result = process_task_artifacts(
            "gen_img",
            "",
            &spec,
            None,
            base.path(),
            None,
            &bindings,
            &datastore,
            &media_refs,
        )
        .await;

        assert_eq!(result.written, 1, "errors: {:?}", result.errors);

        let path_str = result.paths[0].display().to_string();
        assert!(
            path_str.ends_with("result.jpg"),
            "Path should end with resolved extension 'result.jpg', got: {}",
            path_str
        );

        let written = std::fs::read(&result.paths[0]).unwrap();
        assert_eq!(written, binary_data);
    }
}