bria 0.1.4

Multi-pipeline job orchestrator
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
use std::io::Write as _;
use std::path::PathBuf;

#[cfg(any(feature = "amqp", feature = "sqlite", feature = "postgres"))]
use std::collections::HashMap;
#[cfg(any(feature = "amqp", feature = "sqlite", feature = "postgres"))]
use std::sync::Arc;

use crate::config;
use crate::context::{Context, PipelineResult, StepResult};
use crate::error::{Error, Result};
use crate::template::TemplateEngine;
#[cfg(feature = "amqp")]
use crate::util::amqp_url_with_credentials;
#[cfg(any(feature = "sqlite", feature = "postgres"))]
use crate::util::{quote_ident, validate_identifier};

#[cfg(feature = "amqp")]
struct AmqpSinkClient {
    _connection: lapin::Connection,
    channel: lapin::Channel,
}

/// Result sink dispatcher — routes pipeline/step results to configured sinks.
pub struct SinkDispatcher {
    config: crate::config::Config,
    template: TemplateEngine,
    #[cfg(feature = "webhook")]
    http_client: reqwest::Client,
    #[cfg(feature = "sqlite")]
    sqlite_pools: tokio::sync::Mutex<HashMap<String, sqlx::SqlitePool>>,
    #[cfg(feature = "postgres")]
    pg_pools: tokio::sync::Mutex<HashMap<String, sqlx::PgPool>>,
    #[cfg(feature = "amqp")]
    amqp_clients: tokio::sync::Mutex<HashMap<String, Arc<AmqpSinkClient>>>,
    /// Broadcast channel for stream sink (server push).
    broadcast_tx: Option<tokio::sync::broadcast::Sender<serde_json::Value>>,
}

impl SinkDispatcher {
    pub fn new(
        config: crate::config::Config,
        template: TemplateEngine,
        broadcast_tx: Option<tokio::sync::broadcast::Sender<serde_json::Value>>,
    ) -> Self {
        Self {
            config,
            template,
            #[cfg(feature = "webhook")]
            http_client: reqwest::Client::new(),
            #[cfg(feature = "sqlite")]
            sqlite_pools: tokio::sync::Mutex::new(HashMap::new()),
            #[cfg(feature = "postgres")]
            pg_pools: tokio::sync::Mutex::new(HashMap::new()),
            #[cfg(feature = "amqp")]
            amqp_clients: tokio::sync::Mutex::new(HashMap::new()),
            broadcast_tx,
        }
    }

    /// Send pipeline result to all configured pipeline-level sinks.
    pub async fn send_pipeline_result(&self, result: &PipelineResult, ctx: &Context) {
        let pipeline = self
            .config
            .pipelines
            .iter()
            .find(|p| p.id == result.pipeline_id);

        let Some(pipeline) = pipeline else {
            return;
        };

        // ── Sink precedence per step: routing → step.sinks → pipeline-level ──

        // Collect step IDs that have been handled by routing or step.sinks.
        // These steps will NOT be included in the pipeline-level full send.
        let mut handled: std::collections::HashSet<String> = std::collections::HashSet::new();

        // Pass 1: step routing rules have highest precedence.
        for step in &pipeline.steps {
            if let Some(step_result) = result.steps.get(&step.id)
                && !step.routing.is_empty()
            {
                let evaluator = crate::expression::Evaluator::with_pipeline_id(&result.pipeline_id);
                let mut routed = false;
                for route in &step.routing {
                    match evaluator.eval_bool(&route.condition, ctx) {
                        Ok(true) => {
                            for sink_id in &route.sinks {
                                if let Some(sink) = self.config.get_sink(sink_id) {
                                    let step_msg = make_step_synthetic(result, step, step_result);
                                    if let Err(e) = self.send_to_sink(sink, &step_msg, ctx).await {
                                        log_sink_error(sink, &e);
                                    }
                                }
                            }
                            routed = true;
                        }
                        Ok(false) => {}
                        Err(e) => {
                            tracing::warn!("Step '{}' routing condition error: {e}", step.id);
                        }
                    }
                }
                if routed {
                    handled.insert(step.id.clone());
                }
            }
        }

        // Pass 2: step.sinks — only for steps not already handled by routing.
        for step in &pipeline.steps {
            if handled.contains(&step.id) {
                continue;
            }
            if !step.sinks.is_empty()
                && let Some(step_result) = result.steps.get(&step.id)
            {
                for sink_id in &step.sinks {
                    if let Some(sink) = self.config.get_sink(sink_id) {
                        let step_msg = make_step_synthetic(result, step, step_result);
                        if let Err(e) = self.send_to_sink(sink, &step_msg, ctx).await {
                            log_sink_error(sink, &e);
                        }
                    }
                }
                handled.insert(step.id.clone());
            }
        }

        // Pass 3: pipeline-level sinks — only for unhandled steps.
        let remaining_steps: std::collections::HashMap<String, StepResult> = result
            .steps
            .iter()
            .filter(|(id, _)| !handled.contains(*id))
            .map(|(id, r)| (id.clone(), r.clone()))
            .collect();

        if !remaining_steps.is_empty() {
            for sink_id in &pipeline.sinks {
                if let Some(sink) = self.config.get_sink(sink_id) {
                    let pipeline_msg = PipelineResult {
                        pipeline_id: result.pipeline_id.clone(),
                        job: result.job.clone(),
                        status: result.status.clone(),
                        duration_ms: result.duration_ms,
                        steps: remaining_steps.clone(),
                        occurred_at: result.occurred_at.clone(),
                    };
                    if let Err(e) = self.send_to_sink(sink, &pipeline_msg, ctx).await {
                        log_sink_error(sink, &e);
                    }
                }
            }
        }

        // ── Failure dead-letter: always sent ──
        if result.status == "failure"
            && pipeline.failure.action == config::FailureAction::DeadLetter
            && let Some(ref sink_id) = pipeline.failure.sink
            && let Some(sink) = self.config.get_sink(sink_id)
            && let Err(e) = self.send_to_sink(sink, result, ctx).await
        {
            log_sink_error(sink, &e);
        }

        // Step-level failure dead-letter sinks are independent of the pipeline-level
        // failure action. They receive a synthetic single-step result for each failed
        // step configured with `failure.action = "dead_letter"`.
        if result.status == "failure" {
            for step in &pipeline.steps {
                if step.failure.action != config::FailureAction::DeadLetter {
                    continue;
                }
                let Some(step_result) = result.steps.get(&step.id) else {
                    continue;
                };
                if step_result.exit_code == 0 {
                    continue;
                }
                let Some(ref sink_id) = step.failure.sink else {
                    continue;
                };
                if let Some(sink) = self.config.get_sink(sink_id) {
                    let step_msg = make_step_synthetic(result, step, step_result);
                    if let Err(e) = self.send_to_sink(sink, &step_msg, ctx).await {
                        log_sink_error(sink, &e);
                    }
                }
            }
        }
    }

    /// Send a result to a specific sink.
    async fn send_to_sink(
        &self,
        sink: &config::SinkConfig,
        result: &PipelineResult,
        ctx: &Context,
    ) -> Result<()> {
        match sink.r#type {
            config::SinkType::File => self.send_to_file(sink, result, ctx).await,
            #[cfg(feature = "webhook")]
            config::SinkType::Webhook => self.send_to_webhook(sink, result, ctx).await,
            #[cfg(not(feature = "webhook"))]
            config::SinkType::Webhook => Err(Error::Unsupported(
                "Sink type 'webhook' requires the 'webhook' feature".to_string(),
            )),
            #[cfg(feature = "sqlite")]
            config::SinkType::Sqlite => self.send_to_sqlite(sink, result, ctx).await,
            #[cfg(not(feature = "sqlite"))]
            config::SinkType::Sqlite => Err(Error::Unsupported(
                "Sink type 'sqlite' requires the 'sqlite' feature".to_string(),
            )),
            config::SinkType::Stream => self.send_to_stream(sink, result, ctx).await,
            #[cfg(feature = "amqp")]
            config::SinkType::Queue => self.send_to_queue(sink, result, ctx).await,
            #[cfg(not(feature = "amqp"))]
            config::SinkType::Queue => Err(Error::Unsupported(
                "Sink type 'queue' requires the 'amqp' feature".to_string(),
            )),
            #[cfg(feature = "postgres")]
            config::SinkType::Pg => self.send_to_pg(sink, result, ctx).await,
            #[cfg(not(feature = "postgres"))]
            config::SinkType::Pg => Err(Error::Unsupported(
                "Sink type 'pg' requires the 'postgres' feature".to_string(),
            )),
        }
    }

    async fn send_to_file(
        &self,
        sink: &config::SinkConfig,
        result: &PipelineResult,
        ctx: &Context,
    ) -> Result<()> {
        // Render sink.path as a template
        let path_str = self.template.render_result(
            &sink.path,
            ctx,
            &result.pipeline_id,
            &result.status,
            result.duration_ms,
            &result.occurred_at,
        )?;
        let path = PathBuf::from(&path_str);

        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        let line = if let Some(ref tpl) = sink.template {
            self.template.render_result(
                tpl,
                ctx,
                &result.pipeline_id,
                &result.status,
                result.duration_ms,
                &result.occurred_at,
            )?
        } else {
            serde_json::to_string(result)?
        };

        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)?;

        writeln!(file, "{line}")?;

        Ok(())
    }

    #[cfg(feature = "webhook")]
    async fn send_to_webhook(
        &self,
        sink: &config::SinkConfig,
        result: &PipelineResult,
        ctx: &Context,
    ) -> Result<()> {
        let body_string = serde_json::to_string(result)?;
        let body = body_string.as_bytes().to_vec();

        // Render sink.url as a template
        let rendered_url = self.template.render_result(
            &sink.url,
            ctx,
            &result.pipeline_id,
            &result.status,
            result.duration_ms,
            &result.occurred_at,
        )?;

        let mut rendered_headers = Vec::with_capacity(sink.headers.len());
        for (key, value) in &sink.headers {
            let rendered = self.template.render_result(
                value,
                ctx,
                &result.pipeline_id,
                &result.status,
                result.duration_ms,
                &result.occurred_at,
            )?;
            rendered_headers.push((key.clone(), rendered));
        }

        let signature = (!sink.secret.is_empty()).then(|| compute_hmac(&sink.secret, &body_string));

        let mut last_error = None;
        for attempt in 0..=sink.max_retries {
            let mut request = self
                .http_client
                .post(&rendered_url)
                .header("Content-Type", &sink.content_type)
                .body(body.clone());

            for (key, value) in &rendered_headers {
                request = request.header(key.as_str(), value.as_str());
            }

            if let Some(signature) = &signature {
                request = request.header(&sink.signature_header, signature.as_str());
            }

            match tokio::time::timeout(
                std::time::Duration::from_secs(sink.timeout_secs),
                request.send(),
            )
            .await
            {
                Ok(Ok(response)) => {
                    if response.status().is_success() {
                        return Ok(());
                    }
                    last_error = Some(Error::Sink {
                        sink_id: sink.id.clone(),
                        message: format!("Webhook returned status {}", response.status()),
                    });
                }
                Ok(Err(e)) => {
                    last_error = Some(Error::sink_err(&sink.id, "Webhook error", e));
                }
                Err(_) => {
                    last_error = Some(Error::Sink {
                        sink_id: sink.id.clone(),
                        message: "Webhook request timed out".to_string(),
                    });
                }
            }

            if attempt < sink.max_retries {
                let delay = sink.retry_base_ms * 2u64.pow(attempt);
                tokio::time::sleep(std::time::Duration::from_millis(delay)).await;
            }
        }

        Err(last_error.unwrap_or_else(|| Error::Sink {
            sink_id: sink.id.clone(),
            message: "Webhook sink failed all retries".to_string(),
        }))
    }

    #[cfg(feature = "sqlite")]
    async fn get_sqlite_pool(
        &self,
        sink: &config::SinkConfig,
        table: &config::TableSinkConfig,
    ) -> Result<sqlx::SqlitePool> {
        let mut pools = self.sqlite_pools.lock().await;
        if let Some(pool) = pools.get(&sink.id) {
            return Ok(pool.clone());
        }

        let pool = sqlx::SqlitePool::connect(&format!("sqlite:{}?mode=rwc", sink.path))
            .await
            .map_err(|e| Error::sink_err(&sink.id, "SQLite connect error", e))?;
        sqlx::query("PRAGMA journal_mode = WAL")
            .execute(&pool)
            .await
            .map_err(|e| Error::sink_err(&sink.id, "SQLite WAL setup error", e))?;
        sqlx::query("PRAGMA busy_timeout = 5000")
            .execute(&pool)
            .await
            .map_err(|e| Error::sink_err(&sink.id, "SQLite busy-timeout setup error", e))?;
        create_sqlite_result_table(sink, table, &pool).await?;
        pools.insert(sink.id.clone(), pool.clone());
        Ok(pool)
    }

    #[cfg(feature = "postgres")]
    async fn get_pg_pool(
        &self,
        sink: &config::SinkConfig,
        table: &config::TableSinkConfig,
    ) -> Result<sqlx::PgPool> {
        let mut pools = self.pg_pools.lock().await;
        if let Some(pool) = pools.get(&sink.id) {
            return Ok(pool.clone());
        }

        let pool = sqlx::PgPool::connect(&sink.url)
            .await
            .map_err(|e| Error::sink_err(&sink.id, "PG connect error", e))?;
        create_pg_result_table(sink, table, &pool).await?;
        pools.insert(sink.id.clone(), pool.clone());
        Ok(pool)
    }

    #[cfg(feature = "sqlite")]
    async fn send_to_sqlite(
        &self,
        sink: &config::SinkConfig,
        result: &PipelineResult,
        _ctx: &Context,
    ) -> Result<()> {
        let table = sink.table.as_ref().ok_or_else(|| Error::Sink {
            sink_id: sink.id.clone(),
            message: "SQLite sink requires [sinks.table] configuration".to_string(),
        })?;

        let pool = self.get_sqlite_pool(sink, table).await?;

        let tbl = quote_ident("table", &table.name)?;
        let col_result_id = quote_ident("column", &table.columns.result_id)?;
        let col_job_id = quote_ident("column", &table.columns.job_id)?;
        let col_pipeline_id = quote_ident("column", &table.columns.pipeline_id)?;
        let col_step_id = quote_ident("column", &table.columns.step_id)?;
        let col_occurred_at = quote_ident("column", &table.columns.occurred_at)?;
        let col_exit_code = quote_ident("column", &table.columns.exit_code)?;
        let col_stdout = quote_ident("column", &table.columns.stdout)?;
        let col_stderr = quote_ident("column", &table.columns.stderr)?;
        let col_duration_ms = quote_ident("column", &table.columns.duration_ms)?;
        let col_attempt = quote_ident("column", &table.columns.attempt)?;
        let col_status = quote_ident("column", &table.columns.status)?;

        let insert_sql = format!(
            "INSERT INTO {tbl} (\
             {col_result_id}, {col_job_id}, {col_pipeline_id}, {col_step_id}, \
             {col_occurred_at}, {col_exit_code}, {col_stdout}, {col_stderr}, \
             {col_duration_ms}, {col_attempt}, {col_status}\
             ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        );

        let insert_sql_arc = std::sync::Arc::new(insert_sql);
        for (step_id, step_result) in &result.steps {
            let result_id = ulid::Ulid::new().to_string();
            sqlx::query(sqlx::AssertSqlSafe(insert_sql_arc.clone()))
                .bind(&result_id)
                .bind(&result.job.id)
                .bind(&result.pipeline_id)
                .bind(step_id)
                .bind(&result.occurred_at)
                .bind(step_result.exit_code)
                .bind(step_result.stdout.as_deref().unwrap_or(""))
                .bind(step_result.stderr.as_deref().unwrap_or(""))
                .bind(step_result.duration_ms as i64)
                .bind(step_result.attempt as i64)
                .bind(&result.status)
                .execute(&pool)
                .await
                .map_err(|e| Error::sink_err(&sink.id, "SQLite insert error", e))?;
        }

        Ok(())
    }

    async fn send_to_stream(
        &self,
        sink: &config::SinkConfig,
        result: &PipelineResult,
        _ctx: &Context,
    ) -> Result<()> {
        if let Some(ref tx) = self.broadcast_tx {
            let value = serde_json::to_value(result)
                .map_err(|e| Error::sink_err(&sink.id, "Serialization error", e))?;
            let _ = tx.send(value);
        } else {
            tracing::warn!(
                "Stream sink '{}' has no broadcast channel (server may not be enabled)",
                sink.id
            );
        }
        Ok(())
    }

    #[cfg(feature = "amqp")]
    async fn get_amqp_channel(&self, sink: &config::SinkConfig) -> Result<lapin::Channel> {
        let mut clients = self.amqp_clients.lock().await;
        if let Some(client) = clients.get(&sink.id) {
            return Ok(client.channel.clone());
        }

        let conn_url = amqp_url_with_credentials(&sink.url, &sink.username, &sink.password)?;
        let connection =
            lapin::Connection::connect(&conn_url, lapin::ConnectionProperties::default())
                .await
                .map_err(|e| Error::sink_err(&sink.id, "AMQP connection error", e))?;

        let channel = connection
            .create_channel()
            .await
            .map_err(|e| Error::sink_err(&sink.id, "AMQP channel error", e))?;

        channel
            .exchange_declare(
                lapin::types::ShortString::from(sink.exchange.as_str()),
                lapin::ExchangeKind::Topic,
                lapin::options::ExchangeDeclareOptions::default(),
                lapin::types::FieldTable::default(),
            )
            .await
            .map_err(|e| Error::sink_err(&sink.id, "AMQP exchange error", e))?;

        clients.insert(
            sink.id.clone(),
            Arc::new(AmqpSinkClient {
                _connection: connection,
                channel: channel.clone(),
            }),
        );

        Ok(channel)
    }

    #[cfg(feature = "amqp")]
    async fn send_to_queue(
        &self,
        sink: &config::SinkConfig,
        result: &PipelineResult,
        ctx: &Context,
    ) -> Result<()> {
        use lapin::types::ShortString;

        let max_retries = sink.max_retries.max(1);

        let routing_key_template = if result.status == "success" {
            &sink.success_routing_key
        } else {
            &sink.failure_routing_key
        };
        let routing_key_rendered = self.template.render_result(
            routing_key_template,
            ctx,
            &result.pipeline_id,
            &result.status,
            result.duration_ms,
            &result.occurred_at,
        )?;
        let routing_key = ShortString::from(routing_key_rendered.as_str());

        let body = serde_json::to_vec(result)
            .map_err(|e| Error::sink_err(&sink.id, "Serialization error", e))?;

        let mut last_error: Option<Error> = None;

        for attempt in 0..max_retries {
            if attempt > 0 {
                let delay_secs = sink
                    .reconnect_secs
                    .max(1)
                    .saturating_mul(2u64.saturating_pow((attempt - 1).min(5)));
                tracing::warn!(
                    sink_id = %sink.id,
                    attempt = attempt + 1,
                    max_retries,
                    delay_secs,
                    "Retrying AMQP sink publish after reconnect backoff"
                );
                tokio::time::sleep(std::time::Duration::from_secs(delay_secs)).await;
            }

            let channel = match self.get_amqp_channel(sink).await {
                Ok(channel) => channel,
                Err(e) => {
                    last_error = Some(e);
                    self.amqp_clients.lock().await.remove(&sink.id);
                    continue;
                }
            };

            match channel
                .basic_publish(
                    ShortString::from(sink.exchange.as_str()),
                    routing_key.clone(),
                    lapin::options::BasicPublishOptions::default(),
                    &body,
                    lapin::BasicProperties::default(),
                )
                .await
            {
                Ok(confirm) => match confirm.await {
                    Ok(_confirmation) => return Ok(()),
                    Err(e) => {
                        self.amqp_clients.lock().await.remove(&sink.id);
                        last_error =
                            Some(Error::sink_err(&sink.id, "AMQP publish confirm error", e));
                    }
                },
                Err(e) => {
                    self.amqp_clients.lock().await.remove(&sink.id);
                    last_error = Some(Error::sink_err(&sink.id, "AMQP publish error", e));
                }
            }
        }

        Err(last_error.unwrap_or_else(|| Error::Sink {
            sink_id: sink.id.clone(),
            message: "Queue sink failed all retries".to_string(),
        }))
    }

    #[cfg(feature = "postgres")]
    async fn send_to_pg(
        &self,
        sink: &config::SinkConfig,
        result: &PipelineResult,
        _ctx: &Context,
    ) -> Result<()> {
        let table = sink.table.as_ref().ok_or_else(|| Error::Sink {
            sink_id: sink.id.clone(),
            message: "PG sink requires [sinks.table] configuration".to_string(),
        })?;

        let pool = self.get_pg_pool(sink, table).await?;

        // Safe-quoted identifiers (validated via sink config validation; we trust the config)
        let tbl = quote_ident("table", &table.name)?;
        let col_result_id = quote_ident("column", &table.columns.result_id)?;
        let col_job_id = quote_ident("column", &table.columns.job_id)?;
        let col_pipeline_id = quote_ident("column", &table.columns.pipeline_id)?;
        let col_step_id = quote_ident("column", &table.columns.step_id)?;
        let col_occurred_at = quote_ident("column", &table.columns.occurred_at)?;
        let col_exit_code = quote_ident("column", &table.columns.exit_code)?;
        let col_stdout = quote_ident("column", &table.columns.stdout)?;
        let col_stderr = quote_ident("column", &table.columns.stderr)?;
        let col_duration_ms = quote_ident("column", &table.columns.duration_ms)?;
        let col_attempt = quote_ident("column", &table.columns.attempt)?;
        let col_status = quote_ident("column", &table.columns.status)?;

        let insert_sql = format!(
            "INSERT INTO {tbl} (\
             {col_result_id}, {col_job_id}, {col_pipeline_id}, {col_step_id}, \
             {col_occurred_at}, {col_exit_code}, {col_stdout}, {col_stderr}, \
             {col_duration_ms}, {col_attempt}, {col_status}\
             ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)"
        );

        let insert_sql_arc = std::sync::Arc::new(insert_sql);
        for (step_id, step_result) in &result.steps {
            let result_id = ulid::Ulid::new().to_string();
            sqlx::query(sqlx::AssertSqlSafe(insert_sql_arc.clone()))
                .bind(&result_id)
                .bind(&result.job.id)
                .bind(&result.pipeline_id)
                .bind(step_id)
                .bind(&result.occurred_at)
                .bind(step_result.exit_code)
                .bind(step_result.stdout.as_deref().unwrap_or(""))
                .bind(step_result.stderr.as_deref().unwrap_or(""))
                .bind(step_result.duration_ms as i64)
                .bind(step_result.attempt as i32)
                .bind(&result.status)
                .execute(&pool)
                .await
                .map_err(|e| Error::sink_err(&sink.id, "PG insert error", e))?;
        }

        Ok(())
    }
}

/// Build a synthetic single-step PipelineResult for step-level sink dispatch.
fn make_step_synthetic(
    result: &PipelineResult,
    step: &config::StepConfig,
    step_result: &StepResult,
) -> PipelineResult {
    let mut steps = std::collections::HashMap::new();
    steps.insert(step.id.clone(), step_result.clone());
    PipelineResult {
        pipeline_id: result.pipeline_id.clone(),
        job: result.job.clone(),
        status: if step_result.exit_code == 0 {
            "success".to_string()
        } else {
            "failure".to_string()
        },
        duration_ms: step_result.duration_ms,
        steps,
        occurred_at: chrono::Utc::now().to_rfc3339(),
    }
}

#[cfg(feature = "sqlite")]
async fn create_sqlite_result_table(
    sink: &config::SinkConfig,
    table: &config::TableSinkConfig,
    pool: &sqlx::SqlitePool,
) -> Result<()> {
    validate_table_sink_identifiers(table)
        .map_err(|e| Error::sink_err(&sink.id, "SQLite sink identifier error", e))?;
    let tbl = quote_ident("table", &table.name)?;
    let col_result_id = quote_ident("column", &table.columns.result_id)?;
    let col_job_id = quote_ident("column", &table.columns.job_id)?;
    let col_pipeline_id = quote_ident("column", &table.columns.pipeline_id)?;
    let col_step_id = quote_ident("column", &table.columns.step_id)?;
    let col_occurred_at = quote_ident("column", &table.columns.occurred_at)?;
    let col_exit_code = quote_ident("column", &table.columns.exit_code)?;
    let col_stdout = quote_ident("column", &table.columns.stdout)?;
    let col_stderr = quote_ident("column", &table.columns.stderr)?;
    let col_duration_ms = quote_ident("column", &table.columns.duration_ms)?;
    let col_attempt = quote_ident("column", &table.columns.attempt)?;
    let col_status = quote_ident("column", &table.columns.status)?;

    let create_sql = format!(
        "CREATE TABLE IF NOT EXISTS {tbl} (\
         {col_result_id} TEXT PRIMARY KEY, \
         {col_job_id} TEXT NOT NULL, \
         {col_pipeline_id} TEXT NOT NULL, \
         {col_step_id} TEXT NOT NULL, \
         {col_occurred_at} TEXT NOT NULL, \
         {col_exit_code} INTEGER NOT NULL, \
         {col_stdout} TEXT, \
         {col_stderr} TEXT, \
         {col_duration_ms} INTEGER NOT NULL, \
         {col_attempt} INTEGER NOT NULL, \
         {col_status} TEXT NOT NULL\
         )"
    );

    sqlx::query(sqlx::AssertSqlSafe(Arc::new(create_sql)))
        .execute(pool)
        .await
        .map_err(|e| Error::sink_err(&sink.id, "SQLite create table error", e))?;
    Ok(())
}

#[cfg(feature = "postgres")]
async fn create_pg_result_table(
    sink: &config::SinkConfig,
    table: &config::TableSinkConfig,
    pool: &sqlx::PgPool,
) -> Result<()> {
    validate_table_sink_identifiers(table)
        .map_err(|e| Error::sink_err(&sink.id, "PG sink identifier error", e))?;
    let tbl = quote_ident("table", &table.name)?;
    let col_result_id = quote_ident("column", &table.columns.result_id)?;
    let col_job_id = quote_ident("column", &table.columns.job_id)?;
    let col_pipeline_id = quote_ident("column", &table.columns.pipeline_id)?;
    let col_step_id = quote_ident("column", &table.columns.step_id)?;
    let col_occurred_at = quote_ident("column", &table.columns.occurred_at)?;
    let col_exit_code = quote_ident("column", &table.columns.exit_code)?;
    let col_stdout = quote_ident("column", &table.columns.stdout)?;
    let col_stderr = quote_ident("column", &table.columns.stderr)?;
    let col_duration_ms = quote_ident("column", &table.columns.duration_ms)?;
    let col_attempt = quote_ident("column", &table.columns.attempt)?;
    let col_status = quote_ident("column", &table.columns.status)?;

    let create_sql = format!(
        "CREATE TABLE IF NOT EXISTS {tbl} (\
         {col_result_id} TEXT PRIMARY KEY, \
         {col_job_id} TEXT NOT NULL, \
         {col_pipeline_id} TEXT NOT NULL, \
         {col_step_id} TEXT NOT NULL, \
         {col_occurred_at} TEXT NOT NULL, \
         {col_exit_code} INTEGER NOT NULL, \
         {col_stdout} TEXT, \
         {col_stderr} TEXT, \
         {col_duration_ms} BIGINT NOT NULL, \
         {col_attempt} INTEGER NOT NULL, \
         {col_status} TEXT NOT NULL\
         )"
    );

    sqlx::query(sqlx::AssertSqlSafe(Arc::new(create_sql)))
        .execute(pool)
        .await
        .map_err(|e| Error::sink_err(&sink.id, "PG create table error", e))?;
    Ok(())
}

#[cfg(feature = "webhook")]
fn compute_hmac(secret: &str, message: &str) -> String {
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;

    let mut mac =
        Hmac::<Sha256>::new_from_slice(secret.as_bytes()).expect("HMAC can take key of any size");
    mac.update(message.as_bytes());
    let result = mac.finalize();
    hex::encode(result.into_bytes())
}

fn log_sink_error(sink: &config::SinkConfig, error: &Error) {
    tracing::error!(
        sink_id = %sink.id,
        sink_type = ?sink.r#type,
        error = %error,
        "Sink dispatch failed"
    );
}

#[cfg(any(feature = "sqlite", feature = "postgres"))]
fn validate_table_sink_identifiers(table: &config::TableSinkConfig) -> Result<()> {
    validate_identifier("table", &table.name)?;
    validate_identifier("column", &table.columns.result_id)?;
    validate_identifier("column", &table.columns.job_id)?;
    validate_identifier("column", &table.columns.pipeline_id)?;
    validate_identifier("column", &table.columns.step_id)?;
    validate_identifier("column", &table.columns.occurred_at)?;
    validate_identifier("column", &table.columns.exit_code)?;
    validate_identifier("column", &table.columns.stdout)?;
    validate_identifier("column", &table.columns.stderr)?;
    validate_identifier("column", &table.columns.duration_ms)?;
    validate_identifier("column", &table.columns.attempt)?;
    validate_identifier("column", &table.columns.status)?;
    Ok(())
}