tailrace-console 1.0.0

Management console: single-binary axum server embedding the web SPA, REST API, Swagger UI and node registration/heartbeat endpoints
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
//! Pipeline endpoints (§4.5): CRUD with the DRAFT/PENDING_CHANGES flow,
//! local SQL compilation (embedded tailrace-parser, §4.6), publish gating +
//! version snapshots, plan push-down with node auto-assignment, the
//! pause/resume/stop action proxies, stats/offsets observation proxies, the
//! SSE log bridge and the metrics trend.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};

use axum::body::Bytes;
use axum::extract::{Path, Query, State};
use axum::response::sse::{Event, KeepAlive, Sse};
use axum::response::{IntoResponse, Response};
use axum::Json;
use console_auth::{ApiError, AuthContext, M2mState};
use console_store::PipelineWrite;
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};
use utoipa::ToSchema;

use crate::api::{audit, iso, operator_of};
use crate::compile::{compile_sql, substitute_db_refs, CompileDiag, StatementOut};
use crate::state::AppState;

/// Dispatch ids: epoch-millis-based, unique per push (the engine rejects a
/// repeated dispatchId idempotently, §4.5).
static DISPATCH_SEQ: AtomicU64 = AtomicU64::new(0);

fn next_dispatch_id() -> u64 {
    let ms = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0);
    ms * 1000 + DISPATCH_SEQ.fetch_add(1, Ordering::Relaxed) % 1000
}

// ---------------- DTOs ----------------

#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PipelineConfig {
    pub reliability: Option<Value>,
    pub recovery: Option<Value>,
    pub tuning: Option<Value>,
    pub group_prefix: Option<String>,
    pub start_offset: Option<String>,
}

#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PipelineUpsertBody {
    pub name: String,
    pub sql_content: String,
    pub ds_mapping: Option<Map<String, Value>>,
    pub checkpoint_ms: Option<i64>,
    pub preferred_node_id: Option<i64>,
    pub config: Option<PipelineConfig>,
    /// PUT only: CAS generation of the datasource edit is not required for
    /// pipelines (single-writer console); accepted and ignored.
    pub generation: Option<i64>,
}

#[derive(Debug, Serialize, ToSchema)]
pub struct PipelineCreated {
    pub id: i64,
}

#[derive(Debug, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PipelineListItem {
    pub id: i64,
    pub name: String,
    pub status: String,
    pub version: i64,
    pub group_prefix: Option<String>,
    pub consumed_offset_start: String,
    pub assigned_node: Option<Value>,
    pub updated_at: String,
}

#[derive(Debug, Serialize, ToSchema)]
pub struct PipelinesListResponse {
    pub pipelines: Vec<PipelineListItem>,
    pub total: i64,
    pub page: i64,
    pub page_size: i64,
}

#[derive(Debug, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PipelineDetailOut {
    #[serde(flatten)]
    pub list_item: PipelineListItem,
    pub sql_content: String,
    pub spec_json: Option<String>,
    pub ds_mapping: Option<Map<String, Value>>,
    pub checkpoint_ms: i64,
    pub preferred_node_id: Option<i64>,
    pub deploy_error: Option<String>,
    pub created_at: String,
    /// Advanced config echo (§4.5): the editor re-sends it verbatim on PUT so
    /// a plain save must not wipe the stored five-section options.
    pub config: Option<PipelineConfig>,
}

#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct CompileBody {
    pub sql: String,
    pub ds_mapping: Option<Map<String, Value>>,
    /// Group naming uses tr.p{pipelineId}.s{n}; 0 for unsaved drafts.
    pub pipeline_id: Option<u64>,
}

#[derive(Debug, Serialize, ToSchema)]
pub struct CompileResponse {
    pub ok: bool,
    pub statements: Vec<StatementOut>,
    pub diagnostics: Vec<CompileDiag>,
}

#[derive(Debug, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PublishResponse {
    pub id: i64,
    pub version: i64,
    pub spec_json: String,
}

#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct DeployBody {
    pub node_id: Option<i64>,
}

#[derive(Debug, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct TrendPoint {
    pub ts: String,
    pub consumed: i64,
    pub flushed: i64,
    pub rate_in_rps: Option<f64>,
    pub rate_out_rps: Option<f64>,
}

#[derive(Debug, Serialize, ToSchema)]
pub struct TrendResponse {
    pub points: Vec<TrendPoint>,
}

#[derive(Debug, Serialize, ToSchema)]
pub struct SqlTemplate {
    pub name: String,
    pub label: String,
    pub sql: String,
}

#[derive(Debug, Serialize, ToSchema)]
pub struct TemplatesResponse {
    pub templates: Vec<SqlTemplate>,
}

// ---------------- shared plumbing ----------------

fn parse_json_body<T: serde::de::DeserializeOwned>(body: &Bytes) -> Result<T, Response> {
    serde_json::from_slice(body)
        .map_err(|e| ApiError::bad_request(format!("非法请求体: {e}")).into_response())
}

fn load_pipeline(state: &AppState, id: i64) -> Result<console_store::PipelineFull, Response> {
    state
        .store()
        .pipeline_full(id)
        .ok()
        .flatten()
        .ok_or_else(|| ApiError::not_found(format!("管道 #{id} 不存在")).into_response())
}

/// Normalize the dsMapping values to integers ("3" and 3 both accepted).
fn normalize_mapping(mapping: &Map<String, Value>) -> Map<String, Value> {
    mapping
        .iter()
        .map(|(k, v)| {
            let id = v
                .as_i64()
                .or_else(|| v.as_str().and_then(|s| s.parse().ok()));
            (k.clone(), Value::from(id))
        })
        .collect()
}

fn mapping_json(mapping: Option<&Map<String, Value>>) -> Option<String> {
    mapping.map(|m| serde_json::to_string(&normalize_mapping(m)).unwrap_or_else(|_| "{}".into()))
}

/// consumedOffsetStart = config.startOffset ?? "earliest" (§4.5).
fn consumed_offset_start(config_json: Option<&str>) -> String {
    config_json
        .and_then(|s| serde_json::from_str::<Value>(s).ok())
        .and_then(|v| {
            v.get("startOffset")
                .and_then(Value::as_str)
                .map(str::to_string)
        })
        .unwrap_or_else(|| "earliest".into())
}

fn group_prefix_of(config_json: Option<&str>) -> Option<String> {
    config_json
        .and_then(|s| serde_json::from_str::<Value>(s).ok())
        .and_then(|v| {
            v.get("groupPrefix")
                .and_then(Value::as_str)
                .map(str::to_string)
        })
}

fn assigned_node_value(state: &AppState, node_id: Option<i64>) -> Option<Value> {
    let id = node_id?;
    state
        .store()
        .node(id)
        .ok()
        .flatten()
        .map(|n| json!({ "id": n.id, "name": n.name }))
}

// ---------------- CRUD ----------------

/// `GET /api/pipelines?page=1&pageSize=20` (§4.5).
#[utoipa::path(
    get,
    path = "/api/pipelines",
    params(("page" = Option<i64>, Query), ("pageSize" = Option<i64>, Query)),
    responses((status = 200, body = PipelinesListResponse))
)]
pub async fn list(
    State(state): State<AppState>,
    Query(params): Query<HashMap<String, String>>,
) -> Response {
    let page = params
        .get("page")
        .and_then(|v| v.parse::<i64>().ok())
        .unwrap_or(1)
        .max(1);
    let page_size = params
        .get("pageSize")
        .and_then(|v| v.parse::<i64>().ok())
        .unwrap_or(20)
        .clamp(1, 100);
    let store = state.store();
    let (rows, total) = match store.list_pipelines_paged(page, page_size) {
        Ok(v) => v,
        Err(e) => return ApiError::internal(&e).into_response(),
    };
    let config_jsons: HashMap<i64, Option<String>> = rows
        .iter()
        .filter_map(|p| {
            store
                .pipeline_full(p.id)
                .ok()
                .flatten()
                .map(|f| (p.id, f.config_json))
        })
        .collect();
    let pipelines = rows
        .iter()
        .map(|p| PipelineListItem {
            id: p.id,
            name: p.name.clone(),
            status: p.status.clone(),
            version: p.version,
            group_prefix: group_prefix_of(config_jsons.get(&p.id).and_then(|o| o.as_deref())),
            consumed_offset_start: consumed_offset_start(
                config_jsons.get(&p.id).and_then(|o| o.as_deref()),
            ),
            assigned_node: assigned_node_value(&state, p.assigned_node),
            updated_at: iso(Some(&p.updated_at)).unwrap_or_default(),
        })
        .collect();
    Json(PipelinesListResponse {
        pipelines,
        total,
        page,
        page_size,
    })
    .into_response()
}

/// `POST /api/pipelines` — create a draft (§4.5).
#[utoipa::path(
    post,
    path = "/api/pipelines",
    request_body = PipelineUpsertBody,
    responses(
        (status = 201, body = PipelineCreated),
        (status = 400, body = crate::api::ErrorResponse),
        (status = 409, body = crate::api::ErrorResponse, description = "name taken"),
    )
)]
pub async fn create(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    body: Bytes,
) -> Response {
    let body: PipelineUpsertBody = match parse_json_body(&body) {
        Ok(v) => v,
        Err(r) => return r,
    };
    if body.name.trim().is_empty() {
        return ApiError::bad_request("缺少管道名称").into_response();
    }
    let config = body
        .config
        .as_ref()
        .map(|c| serde_json::to_string(c).unwrap_or_else(|_| "{}".into()));
    let store = state.store();
    let id = match store.create_pipeline(
        body.name.trim(),
        &body.sql_content,
        mapping_json(body.ds_mapping.as_ref()).as_deref(),
        body.checkpoint_ms.unwrap_or(10000),
        body.preferred_node_id,
        config.as_deref(),
    ) {
        Ok(PipelineWrite::Created(id)) => id,
        Ok(PipelineWrite::NameConflict) => {
            return ApiError::new(
                axum::http::StatusCode::CONFLICT,
                "CONFLICT",
                "管道名称已存在",
            )
            .into_response()
        }
        Err(e) => return ApiError::internal(&e).into_response(),
    };
    audit(
        store,
        &operator_of(&ctx, &headers),
        "PIPELINE_CREATE",
        "pipeline",
        Some(id.to_string()),
        Some(json!({"name": body.name.trim()}).to_string()),
    );
    (
        axum::http::StatusCode::CREATED,
        Json(PipelineCreated { id }),
    )
        .into_response()
}

/// `GET /api/pipelines/{id}` — detail (§4.5).
#[utoipa::path(
    get,
    path = "/api/pipelines/{id}",
    params(("id" = i64, Path)),
    responses(
        (status = 200, body = PipelineDetailOut),
        (status = 404, body = crate::api::ErrorResponse),
    )
)]
pub async fn detail(State(state): State<AppState>, Path(id): Path<i64>) -> Response {
    let full = match load_pipeline(&state, id) {
        Ok(f) => f,
        Err(r) => return r,
    };
    let p = &full.base;
    Json(PipelineDetailOut {
        list_item: PipelineListItem {
            id: p.id,
            name: p.name.clone(),
            status: p.status.clone(),
            version: p.version,
            group_prefix: group_prefix_of(full.config_json.as_deref()),
            consumed_offset_start: consumed_offset_start(full.config_json.as_deref()),
            assigned_node: assigned_node_value(&state, p.assigned_node),
            updated_at: iso(Some(&p.updated_at)).unwrap_or_default(),
        },
        sql_content: p.sql_content.clone(),
        spec_json: p.spec_json.clone(),
        ds_mapping: p
            .ds_mapping_json
            .as_deref()
            .and_then(|s| serde_json::from_str::<Map<String, Value>>(s).ok()),
        checkpoint_ms: p.checkpoint_ms,
        preferred_node_id: p.preferred_node,
        deploy_error: full.deploy_error.clone(),
        created_at: iso(Some(&p.created_at)).unwrap_or_default(),
        config: full
            .config_json
            .as_deref()
            .and_then(|s| serde_json::from_str::<PipelineConfig>(s).ok()),
    })
    .into_response()
}

/// `PUT /api/pipelines/{id}` — non-DRAFT saves move to PENDING_CHANGES
/// (「已保存为待发布变更,需发布后部署才生效」, §4.5).
#[utoipa::path(
    put,
    path = "/api/pipelines/{id}",
    request_body = PipelineUpsertBody,
    params(("id" = i64, Path)),
    responses(
        (status = 200, body = PipelineCreated),
        (status = 404, body = crate::api::ErrorResponse),
        (status = 409, body = crate::api::ErrorResponse),
    )
)]
pub async fn update(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
    body: Bytes,
) -> Response {
    let body: PipelineUpsertBody = match parse_json_body(&body) {
        Ok(v) => v,
        Err(r) => return r,
    };
    if load_pipeline(&state, id).is_err() {
        return ApiError::not_found(format!("管道 #{id} 不存在")).into_response();
    }
    let config = body
        .config
        .as_ref()
        .map(|c| serde_json::to_string(c).unwrap_or_else(|_| "{}".into()));
    let store = state.store();
    match store.update_pipeline(
        id,
        body.name.trim(),
        &body.sql_content,
        mapping_json(body.ds_mapping.as_ref()).as_deref(),
        body.checkpoint_ms.unwrap_or(10000),
        body.preferred_node_id,
        config.as_deref(),
    ) {
        Ok(PipelineWrite::Created(_)) => {}
        Ok(PipelineWrite::NameConflict) => {
            return ApiError::new(
                axum::http::StatusCode::CONFLICT,
                "CONFLICT",
                "管道名称已存在",
            )
            .into_response()
        }
        Err(e) => return ApiError::internal(&e).into_response(),
    }
    audit(
        store,
        &operator_of(&ctx, &headers),
        "PIPELINE_UPDATE",
        "pipeline",
        Some(id.to_string()),
        None,
    );
    Json(PipelineCreated { id }).into_response()
}

/// `DELETE /api/pipelines/{id}` — RUNNING pipelines are stopped first
/// (graceful stop via the node proxy; an unreachable engine blocks the
/// delete, §4.5).
#[utoipa::path(
    delete,
    path = "/api/pipelines/{id}",
    params(("id" = i64, Path)),
    responses(
        (status = 204),
        (status = 404, body = crate::api::ErrorResponse),
        (status = 502, body = crate::api::ErrorResponse, description = "engine unreachable during auto-stop"),
    )
)]
pub async fn remove(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
) -> Response {
    let full = match load_pipeline(&state, id) {
        Ok(f) => f,
        Err(r) => return r,
    };
    let store = state.store();
    if full.base.status == "RUNNING" {
        if let Err(e) = proxy_action(&state, id, "stop").await {
            return e;
        }
    }
    if let Err(e) = store.delete_pipeline(id) {
        return ApiError::internal(&e).into_response();
    }
    audit(
        store,
        &operator_of(&ctx, &headers),
        "PIPELINE_DELETE",
        "pipeline",
        Some(id.to_string()),
        None,
    );
    axum::http::StatusCode::NO_CONTENT.into_response()
}

// ---------------- compile / publish ----------------

/// `POST /api/pipelines/compile` — local compile via the embedded
/// tailrace-parser (§4.6: offline editor diagnostics). Diagnostics ride a
/// 400 whose body is the compile shape (not the generic envelope — §4.5's
/// own 400 example).
#[utoipa::path(
    post,
    path = "/api/pipelines/compile",
    request_body = CompileBody,
    responses(
        (status = 200, body = CompileResponse, description = "ok:true with the statement set"),
        (status = 400, body = CompileResponse, description = "ok:false with diagnostics (§4.5 compile 400 shape)"),
    )
)]
pub async fn compile(State(_state): State<AppState>, body: Bytes) -> Response {
    let body: CompileBody = match parse_json_body(&body) {
        Ok(v) => v,
        Err(r) => return r,
    };
    let mapping = body.ds_mapping.unwrap_or_default();
    let substituted = substitute_db_refs(&body.sql, &normalize_mapping(&mapping));
    let (statements, diagnostics) = compile_sql(&substituted, body.pipeline_id.unwrap_or(0));
    let ok = diagnostics.is_empty();
    let resp = CompileResponse {
        ok,
        statements,
        diagnostics,
    };
    if ok {
        Json(resp).into_response()
    } else {
        (axum::http::StatusCode::BAD_REQUEST, Json(resp)).into_response()
    }
}

/// `POST /api/pipelines/{id}/publish` — gate: every referenced table object
/// resolvable → materialize `${ds:id.db.table}` spec → version+1 → snapshot
/// (§4.5).
#[utoipa::path(
    post,
    path = "/api/pipelines/{id}/publish",
    params(("id" = i64, Path)),
    responses(
        (status = 200, body = PublishResponse),
        (status = 400, body = CompileResponse, description = "gate failed: diagnostics"),
        (status = 404, body = crate::api::ErrorResponse),
    )
)]
pub async fn publish(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
) -> Response {
    let full = match load_pipeline(&state, id) {
        Ok(f) => f,
        Err(r) => return r,
    };
    let p = &full.base;
    let mapping: Map<String, Value> = p
        .ds_mapping_json
        .as_deref()
        .and_then(|s| serde_json::from_str::<Map<String, Value>>(s).ok())
        .unwrap_or_default();
    let spec = substitute_db_refs(&p.sql_content, &normalize_mapping(&mapping));
    let (statements, diagnostics) = compile_sql(&spec, id as u64);
    if !diagnostics.is_empty() {
        return (
            axum::http::StatusCode::BAD_REQUEST,
            Json(CompileResponse {
                ok: false,
                statements,
                diagnostics,
            }),
        )
            .into_response();
    }
    // publish gate: every source/sink resolves to a registered table object
    let store = state.store();
    let mut gate: Vec<CompileDiag> = Vec::new();
    for (idx, st) in statements.iter().enumerate() {
        for role_ref in [(&st.source, "源"), (&st.sink, "汇")] {
            if store
                .table_object_for_reference(role_ref.0)
                .ok()
                .flatten()
                .is_none()
            {
                gate.push(CompileDiag {
                    line: idx as i64 + 1,
                    message: format!(
                        "语句 {} 的{}表对象未登记: {}(先在数据源页登记库表)",
                        idx + 1,
                        role_ref.1,
                        role_ref.0
                    ),
                });
            }
        }
    }
    if !gate.is_empty() {
        return (
            axum::http::StatusCode::BAD_REQUEST,
            Json(CompileResponse {
                ok: false,
                statements: Vec::new(),
                diagnostics: gate,
            }),
        )
            .into_response();
    }
    let version = match store.set_pipeline_published(id, &spec, &p.sql_content, &ctx.user.username)
    {
        Ok(v) => v,
        Err(e) => return ApiError::internal(&e).into_response(),
    };
    audit(
        store,
        &operator_of(&ctx, &headers),
        "PIPELINE_PUBLISH",
        "pipeline",
        Some(id.to_string()),
        Some(json!({"version": version}).to_string()),
    );
    Json(PublishResponse {
        id,
        version,
        spec_json: spec,
    })
    .into_response()
}

// ---------------- deploy / recover (plan push) ----------------

/// `POST /api/pipelines/{id}/deploy` — plan push-down with auto-assignment
/// (preferred eligible → fewest running → lowest id, §4.5) and the full plan
/// body per §4.5/engine B.2. Engine `ok:false` records DEPLOY_FAILED and
/// returns 502.
#[utoipa::path(
    post,
    path = "/api/pipelines/{id}/deploy",
    request_body = DeployBody,
    params(("id" = i64, Path)),
    responses(
        (status = 200, description = "engine verdict JSON (ok:true + statement count)"),
        (status = 400, body = crate::api::ErrorResponse),
        (status = 404, body = crate::api::ErrorResponse),
        (status = 409, body = crate::api::ErrorResponse, description = "no eligible node"),
        (status = 502, body = crate::api::ErrorResponse, description = "engine unreachable or ok:false"),
    )
)]
pub async fn deploy(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
    body: Bytes,
) -> Response {
    // {"nodeId": null} and an empty body both mean auto-assign
    let node_id: Option<i64> = serde_json::from_slice::<DeployBody>(&body)
        .ok()
        .and_then(|b| b.node_id);
    push_plan(
        &state,
        id,
        node_id,
        "PIPELINE_DEPLOY",
        &operator_of(&ctx, &headers),
    )
    .await
}

/// `POST /api/pipelines/{id}/recover` — re-push the current plan to the
/// target node (default: the currently assigned node); the engine resumes
/// from its offsets ledger (§4.5).
#[utoipa::path(
    post,
    path = "/api/pipelines/{id}/recover",
    request_body = DeployBody,
    params(("id" = i64, Path)),
    responses(
        (status = 200, description = "engine verdict JSON"),
        (status = 400, body = crate::api::ErrorResponse),
        (status = 404, body = crate::api::ErrorResponse),
        (status = 502, body = crate::api::ErrorResponse),
    )
)]
pub async fn recover(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
    body: Bytes,
) -> Response {
    let node_id: Option<i64> = serde_json::from_slice::<DeployBody>(&body)
        .ok()
        .and_then(|b| b.node_id)
        .or_else(|| {
            state
                .store()
                .pipeline(id)
                .ok()
                .flatten()
                .and_then(|p| p.assigned_node)
        });
    push_plan(
        &state,
        id,
        node_id,
        "PIPELINE_RECOVER",
        &operator_of(&ctx, &headers),
    )
    .await
}

// ---------------- plan construction + push ----------------

/// Build and push the plan (§4.5 deploy / recover shared path). Steps:
/// compile the materialized spec → resolve every ref against registered
/// table objects (+ datasource connection params) → pick the node → PUT the
/// plan → record the outcome.
async fn push_plan(
    state: &AppState,
    id: i64,
    requested_node: Option<i64>,
    audit_action: &str,
    operator: &str,
) -> Response {
    let full = match load_pipeline(state, id) {
        Ok(f) => f,
        Err(r) => return r,
    };
    let p = &full.base;
    let Some(spec) = p.spec_json.clone() else {
        return ApiError::bad_request("管道尚未发布(无 spec 快照),先执行发布").into_response();
    };
    let (statements, diagnostics) = compile_sql(&spec, id as u64);
    if !diagnostics.is_empty() {
        return (
            axum::http::StatusCode::BAD_REQUEST,
            Json(CompileResponse {
                ok: false,
                statements,
                diagnostics,
            }),
        )
            .into_response();
    }

    // tables: one plan entry per unique reference
    let store = state.store();
    let mut refs: Vec<String> = Vec::new();
    for st in &statements {
        for r in [&st.source, &st.sink] {
            if !refs.contains(r) {
                refs.push(r.clone());
            }
        }
    }
    let mut tables: Vec<Value> = Vec::new();
    let mut missing: Vec<String> = Vec::new();
    for reference in &refs {
        match build_plan_table(store, reference) {
            Ok(Some(t)) => tables.push(t),
            Ok(None) => missing.push(reference.clone()),
            Err(e) => return ApiError::internal(&e).into_response(),
        }
    }
    if !missing.is_empty() {
        return (
            axum::http::StatusCode::BAD_REQUEST,
            Json(CompileResponse {
                ok: false,
                statements: Vec::new(),
                diagnostics: vec![CompileDiag {
                    line: 1,
                    message: format!("表对象未登记(先在数据源页登记): {}", missing.join(", ")),
                }],
            }),
        )
            .into_response();
    }

    // node selection: explicit → preferred-eligible → fewest running → id
    let node = match pick_node(state, requested_node.or(p.preferred_node)).await {
        Ok(n) => n,
        Err(r) => return r,
    };
    let Some(api_url) = node.api_url.clone() else {
        return ApiError::new(
            axum::http::StatusCode::CONFLICT,
            "NO_ELIGIBLE_NODE",
            format!("节点 {} 未上报 api_url(等待首次心跳)", node.name),
        )
        .into_response();
    };

    // plan body per §4.5 (fields map 1:1 onto the engine's PUT /v1/.../plan)
    let mut plan = json!({
        "pipelineId": id,
        "checkpointMs": p.checkpoint_ms,
        "dispatchId": next_dispatch_id(),
        "tables": tables,
        "sqlContent": spec,
    });
    if p.status == "PAUSED" {
        plan["initialState"] = json!("PAUSED");
    }
    if let Some(config) = full
        .config_json
        .as_deref()
        .and_then(|s| serde_json::from_str::<Value>(s).ok())
    {
        if config.as_object().map(|o| !o.is_empty()).unwrap_or(false) {
            plan["options"] = config;
        }
    }

    let endpoint =
        console_proxy::NodeEndpoint::with_token(api_url, state.bootstrap().global_token());
    match endpoint.put_plan(state.node_client(), id, &plan).await {
        Ok(verdict) if verdict.get("ok").and_then(Value::as_bool).unwrap_or(false) => {
            if let Err(e) = store.set_pipeline_deployed(id, node.id) {
                return ApiError::internal(&e).into_response();
            }
            audit(
                store,
                operator,
                audit_action,
                "pipeline",
                Some(id.to_string()),
                Some(json!({"nodeId": node.id, "nodeName": node.name, "dispatchId": plan["dispatchId"]}).to_string()),
            );
            Json(verdict).into_response()
        }
        Ok(verdict) => {
            let error = verdict
                .get("error")
                .and_then(Value::as_str)
                .map(str::to_string)
                .unwrap_or_else(|| verdict.to_string());
            let _ = store.set_pipeline_deploy_failed(id, &error);
            ApiError::new(
                axum::http::StatusCode::BAD_GATEWAY,
                "ENGINE_REJECTED",
                format!("引擎装配失败: {error}"),
            )
            .into_response()
        }
        Err(e) => {
            let _ = store.set_pipeline_deploy_failed(id, &e.to_string());
            ApiError::new(
                axum::http::StatusCode::BAD_GATEWAY,
                "ENGINE_UNREACHABLE",
                e.to_string(),
            )
            .into_response()
        }
    }
}

/// Resolve one `"dsId.db.table"` reference into the plan `tables[]` entry
/// (§4.5: kind/format/columns from the table object, bootstrap /
/// streamLoadUrl from the datasource conn, authEnv bound per datasource —
/// values never enter the plan). Ok(None) = not registered.
fn build_plan_table(
    store: &console_store::Store,
    reference: &str,
) -> anyhow::Result<Option<Value>> {
    let Some(table) = store.table_object_for_reference(reference)? else {
        return Ok(None);
    };
    let Some(ds) = store.datasource(table.datasource_id)? else {
        return Ok(None);
    };
    let conn: Value = serde_json::from_str(&ds.conn_json).unwrap_or(Value::Null);
    let columns: Vec<(String, String)> = store
        .table_object_columns_json(table.id)?
        .flatten()
        .and_then(|s| serde_json::from_str(&s).ok())
        .unwrap_or_default();
    let kind = tailrace_catalog::normalize_kind(&table.kind);
    let has_credential = store.credential(ds.id)?.is_some();
    // authEnv: the engine's credential_from_env only accepts TAILRACE_-prefixed
    // names (engine-v1.md §四); the design doc §4.5 example ("TR_DS_4") predates
    // that constraint — see docs/protocol/console-api.md deviations.
    let auth_env = matches!(kind.as_str(), "doris" | "starrocks")
        .then(|| format!("TAILRACE_DS_{}", ds.id))
        .filter(|_| has_credential);
    // stream_load_url: the engine's DorisSink PUTs this verbatim, and the
    // /v1 contract (docs/protocol/engine-v1.md example) is the FULL endpoint
    // `http://be:8040/api/{db}/{table}/_stream_load` — compose it from the
    // datasource's BE base + this table's db/table.
    let stream_load_url = conn
        .get("streamLoadUrl")
        .and_then(Value::as_str)
        .filter(|_| matches!(kind.as_str(), "doris" | "starrocks"))
        .map(|base| {
            format!(
                "{}/api/{}/{}/_stream_load",
                base.trim_end_matches('/'),
                table.db_name,
                table.table_name
            )
        });
    Ok(Some(json!({
        "reference": reference,
        "kind": kind,
        "format": table.format,
        "columns": columns,
        "bootstrap": conn.get("bootstrap").and_then(Value::as_str),
        // table-entry keys follow the engine /v1 plan contract (snake_case —
        // tailrace-catalog PlanTableObject has no serde renames; the camelCase
        // keys here were silently dropped, killing every doris deploy with
        // "must declare auth_env" — T11 first-run finding)
        "stream_load_url": stream_load_url,
        "auth_env": auth_env,
        "sequence_column": null,
    })))
}

/// Auto-assignment (§4.5): explicit id → preferred (if ONLINE) → eligible
/// node with fewest RUNNING pipelines → lowest id. Eligible = ONLINE by
/// heartbeat within 90s and advertising an api_url.
async fn pick_node(
    state: &AppState,
    preferred: Option<i64>,
) -> Result<console_store::Node, Response> {
    let store = state.store();
    let nodes = store
        .list_nodes()
        .map_err(|e| ApiError::internal(&e).into_response())?;
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);
    let eligible = |n: &console_store::Node| -> bool {
        n.status == "ONLINE"
            && n.api_url.is_some()
            && n.last_heartbeat
                .as_deref()
                .and_then(crate::api::nodes_admin::sqlite_ts_to_epoch)
                .map(|ts| now - ts <= 90)
                .unwrap_or(false)
    };
    if let Some(id) = preferred {
        if let Some(n) = nodes.iter().find(|n| n.id == id) {
            if eligible(n) {
                return Ok(n.clone());
            }
        }
    }
    let running: HashMap<i64, i64> = store
        .running_pipeline_counts()
        .unwrap_or_default()
        .into_iter()
        .collect();
    nodes
        .iter()
        .filter(|n| eligible(n))
        .min_by_key(|n| (running.get(&n.id).copied().unwrap_or(0), n.id))
        .cloned()
        .ok_or_else(|| {
            ApiError::new(
                axum::http::StatusCode::CONFLICT,
                "NO_ELIGIBLE_NODE",
                "没有可指派的在线节点(需心跳 90s 内在线且已上报 api_url)",
            )
            .into_response()
        })
}

// ---------------- actions + observation proxies ----------------

/// Shared pause/resume/stop proxy (§4.5): engine POST
/// `/v1/pipelines/{id}/{action}` → console DB status write-back.
async fn proxy_action(state: &AppState, id: i64, action: &str) -> Result<Value, Response> {
    let full = load_pipeline(state, id)?;
    let store = state.store();
    let Some(node_id) = full.base.assigned_node else {
        if action == "stop" {
            // stop of an undeployed pipeline: idempotent return-to-PUBLISHED
            let _ = store.set_pipeline_status(id, "PUBLISHED", true);
            return Ok(json!({"pipelineId": id, "status": "PUBLISHED"}));
        }
        return Err(
            ApiError::bad_request(format!("管道 #{id} 未部署(无指派节点)")).into_response(),
        );
    };
    let node = store.node(node_id).ok().flatten().ok_or_else(|| {
        ApiError::not_found(format!("指派节点 #{node_id} 不存在")).into_response()
    })?;
    let Some(api_url) = node.api_url else {
        return Err(ApiError::new(
            axum::http::StatusCode::BAD_GATEWAY,
            "ENGINE_UNREACHABLE",
            "节点未上报 api_url",
        )
        .into_response());
    };
    let endpoint =
        console_proxy::NodeEndpoint::with_token(api_url, state.bootstrap().global_token());
    match endpoint
        .pipeline_action(state.node_client(), id, action)
        .await
    {
        Ok(verdict) => {
            let ok = verdict.get("ok").and_then(Value::as_bool).unwrap_or(false);
            let status = verdict.get("status").and_then(Value::as_str).unwrap_or("");
            if ok {
                let (db_status, clear) = match action {
                    "pause" => ("PAUSED", false),
                    "resume" => ("RUNNING", false),
                    "stop" => ("PUBLISHED", true), // 回已发布态,可再部署
                    _ => (status, false),
                };
                let _ = store.set_pipeline_status(id, db_status, clear);
            }
            Ok(verdict)
        }
        Err(e) => Err(ApiError::new(
            axum::http::StatusCode::BAD_GATEWAY,
            "ENGINE_UNREACHABLE",
            e.to_string(),
        )
        .into_response()),
    }
}

/// `POST /api/pipelines/{id}/pause` — §4.5 action proxy.
#[utoipa::path(
    post,
    path = "/api/pipelines/{id}/pause",
    params(("id" = i64, Path)),
    responses(
        (status = 200, description = "engine verdict: {\"pipelineId\":42,\"status\":\"PAUSED\"}"),
        (status = 400, body = crate::api::ErrorResponse),
        (status = 404, body = crate::api::ErrorResponse),
        (status = 502, body = crate::api::ErrorResponse, description = "engine unreachable"),
    )
)]
pub async fn pause(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
) -> Response {
    run_action(state, ctx, headers, id, "pause").await
}

/// `POST /api/pipelines/{id}/resume` — §4.5 action proxy.
#[utoipa::path(
    post,
    path = "/api/pipelines/{id}/resume",
    params(("id" = i64, Path)),
    responses(
        (status = 200, description = "engine verdict: {\"pipelineId\":42,\"status\":\"RUNNING\"}"),
        (status = 400, body = crate::api::ErrorResponse),
        (status = 404, body = crate::api::ErrorResponse),
        (status = 502, body = crate::api::ErrorResponse),
    )
)]
pub async fn resume(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
) -> Response {
    run_action(state, ctx, headers, id, "resume").await
}

/// `POST /api/pipelines/{id}/stop` — §4.5 action proxy (graceful stop; the
/// pipeline returns to PUBLISHED and can be re-deployed).
#[utoipa::path(
    post,
    path = "/api/pipelines/{id}/stop",
    params(("id" = i64, Path)),
    responses(
        (status = 200, description = "engine verdict: {\"pipelineId\":42,\"status\":\"STOPPED\"}"),
        (status = 400, body = crate::api::ErrorResponse),
        (status = 404, body = crate::api::ErrorResponse),
        (status = 502, body = crate::api::ErrorResponse),
    )
)]
pub async fn stop(
    State(state): State<AppState>,
    axum::Extension(ctx): axum::Extension<AuthContext>,
    headers: axum::http::HeaderMap,
    Path(id): Path<i64>,
) -> Response {
    run_action(state, ctx, headers, id, "stop").await
}

async fn run_action(
    state: AppState,
    ctx: AuthContext,
    headers: axum::http::HeaderMap,
    id: i64,
    action: &'static str,
) -> Response {
    match proxy_action(&state, id, action).await {
        Ok(v) => {
            audit(
                state.store(),
                &operator_of(&ctx, &headers),
                "PIPELINE_ACTION",
                "pipeline",
                Some(id.to_string()),
                Some(json!({"action": action}).to_string()),
            );
            Json(v).into_response()
        }
        Err(r) => r,
    }
}

/// `GET /api/pipelines/{id}/stats` — engine stats proxy, body verbatim; the
/// engine's own ok:false ("not registered") passes through on 200 for the
/// frontend's degrade branch (§4.5).
#[utoipa::path(
    get,
    path = "/api/pipelines/{id}/stats",
    params(("id" = i64, Path)),
    responses(
        (status = 200, description = "engine stats JSON verbatim (ok:false = not registered)"),
        (status = 502, body = crate::api::ErrorResponse, description = "engine unreachable"),
    )
)]
pub async fn stats(State(state): State<AppState>, Path(id): Path<i64>) -> Response {
    proxy_get(&state, id, "stats").await
}

/// `GET /api/pipelines/{id}/offsets` — engine offsets proxy (§4.5/§4.6: the
/// one planned engine-side addition; ledger truth, works for stopped
/// pipelines).
#[utoipa::path(
    get,
    path = "/api/pipelines/{id}/offsets",
    params(("id" = i64, Path)),
    responses(
        (status = 200, description = "engine offsets JSON verbatim"),
        (status = 502, body = crate::api::ErrorResponse),
    )
)]
pub async fn offsets(State(state): State<AppState>, Path(id): Path<i64>) -> Response {
    proxy_get(&state, id, "offsets").await
}

async fn proxy_get(state: &AppState, id: i64, resource: &str) -> Response {
    let full = match load_pipeline(state, id) {
        Ok(f) => f,
        Err(r) => return r,
    };
    let Some(node_id) = full.base.assigned_node else {
        return Json(json!({
            "ok": false,
            "error": format!("pipeline #{id} 未部署(无指派节点)"),
        }))
        .into_response();
    };
    let node = match state.store().node(node_id).ok().flatten() {
        Some(n) => n,
        None => return ApiError::not_found(format!("指派节点 #{node_id} 不存在")).into_response(),
    };
    let Some(api_url) = node.api_url else {
        return ApiError::new(
            axum::http::StatusCode::BAD_GATEWAY,
            "ENGINE_UNREACHABLE",
            "节点未上报 api_url",
        )
        .into_response();
    };
    let endpoint =
        console_proxy::NodeEndpoint::with_token(api_url, state.bootstrap().global_token());
    match endpoint
        .get_json(
            state.node_client(),
            &format!("/v1/pipelines/{id}/{resource}"),
        )
        .await
    {
        Ok(v) => Json(v).into_response(),
        Err(e) => ApiError::new(
            axum::http::StatusCode::BAD_GATEWAY,
            "ENGINE_UNREACHABLE",
            e.to_string(),
        )
        .into_response(),
    }
}

// ---------------- SSE log bridge (§4.5 / A5) ----------------

/// `GET /api/pipelines/{id}/logs?lines=200&after_seq=N` — SSE bridge over
/// the engine's cursor API: initial `tail=1&limit` (clamped 50..500), then
/// 1.5s `after_seq` polling; keepalive every 15s; session ends at 300s or
/// after 5 consecutive engine failures (eof event, §4.5). A client-supplied
/// `after_seq>0` seeds the cursor (log continuation on reconnect — no tail
/// replay; the engine only returns entries past that global seq).
#[utoipa::path(
    get,
    path = "/api/pipelines/{id}/logs",
    params(
        ("id" = i64, Path),
        ("lines" = Option<usize>, Query, description = "first-screen tail, clamped 50..500"),
        ("after_seq" = Option<u64>, Query, description = "resume cursor: skip the tail pull, stream only entries past this global seq"),
    ),
    responses((status = 200, description = "text/event-stream: events log / eof, : keepalive"))
)]
pub async fn logs(
    State(state): State<AppState>,
    Path(id): Path<i64>,
    Query(params): Query<HashMap<String, String>>,
) -> Response {
    let lines = params
        .get("lines")
        .and_then(|v| v.parse::<usize>().ok())
        .unwrap_or(200)
        .clamp(50, 500);
    // Reconnect cursor (§4.5 log continuation): 0/absent = fresh first
    // screen (tail pull); >0 = resume past that seq (incremental only).
    let after_seq = params
        .get("after_seq")
        .and_then(|v| v.parse::<u64>().ok())
        .unwrap_or(0);
    let full = match load_pipeline(&state, id) {
        Ok(f) => f,
        Err(r) => return r,
    };
    let api_url = full.base.assigned_node.and_then(|node_id| {
        state
            .store()
            .node(node_id)
            .ok()
            .flatten()
            .and_then(|n| n.api_url)
    });

    let (tx, rx) = tokio::sync::mpsc::channel::<Result<Event, std::convert::Infallible>>(64);
    let stream_client = state.stream_client().clone();
    // engine H1: the /v1 proxy calls carry the console's shared node token
    // (captured before the spawn — the engine middleware requires it)
    let node_token = state.bootstrap().global_token().to_string();
    tokio::spawn(async move {
        let mut consecutive_failures = 0u32;
        let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(300);
        let Some(api_url) = api_url else {
            let _ = send_event(&tx, "eof", &json!({"reason": "not_deployed"})).await;
            return;
        };
        let endpoint = console_proxy::NodeEndpoint::with_token(api_url, node_token);
        // Client cursor seed: after_seq>0 skips the tail pull entirely (the
        // `cursor == 0` branch below) so a reconnect never replays the tail.
        let mut cursor: u64 = after_seq;
        loop {
            let page = if cursor == 0 {
                endpoint
                    .get_json(
                        &stream_client,
                        &format!("/v1/pipelines/{id}/logs?tail=1&limit={lines}"),
                    )
                    .await
            } else {
                endpoint
                    .get_json(
                        &stream_client,
                        &format!("/v1/pipelines/{id}/logs?after_seq={cursor}&limit=500"),
                    )
                    .await
            };
            match page {
                Ok(v) => {
                    consecutive_failures = 0;
                    cursor = v.get("lastSeq").and_then(Value::as_u64).unwrap_or(cursor);
                    if let Some(entries) = v.get("entries").and_then(Value::as_array) {
                        let mut dropped = false;
                        for e in entries {
                            if send_event(&tx, "log", e).await.is_err() {
                                dropped = true; // client went away
                                break;
                            }
                        }
                        if dropped {
                            return;
                        }
                    }
                }
                Err(_) => {
                    consecutive_failures += 1;
                    if consecutive_failures >= 5 {
                        let _ =
                            send_event(&tx, "eof", &json!({"reason": "engine_unreachable"})).await;
                        return;
                    }
                }
            }
            // poll cadence 1.5s; hard session end at 300s (§4.5). Keepalive
            // frames come from the SSE layer's KeepAlive (15s, ": keepalive").
            tokio::select! {
                _ = tokio::time::sleep(std::time::Duration::from_millis(1500)) => {}
                _ = tokio::time::sleep_until(deadline) => {
                    let _ = send_event(&tx, "eof", &json!({"reason": "timeout"})).await;
                    return;
                }
            }
        }
    });

    let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
    Sse::new(stream)
        .keep_alive(
            KeepAlive::new()
                .interval(std::time::Duration::from_secs(15))
                .text("keepalive"),
        )
        .into_response()
}

async fn send_event(
    tx: &tokio::sync::mpsc::Sender<Result<Event, std::convert::Infallible>>,
    name: &str,
    data: &Value,
) -> Result<(), tokio::sync::mpsc::error::SendError<Result<Event, std::convert::Infallible>>> {
    tx.send(Ok(Event::default().event(name).data(data.to_string())))
        .await
}

// ---------------- trend + templates ----------------

/// `GET /api/pipelines/{id}/metrics/trend?hours=24` — console-side 5-minute
/// buckets from heartbeat-piggybacked pipeline_metrics; rates derived from
/// consecutive bucket deltas (§4.5).
#[utoipa::path(
    get,
    path = "/api/pipelines/{id}/metrics/trend",
    params(("id" = i64, Path), ("hours" = Option<i64>, Query, description = "window, clamped 1..168")),
    responses(
        (status = 200, body = TrendResponse),
        (status = 404, body = crate::api::ErrorResponse),
    )
)]
pub async fn metrics_trend(
    State(state): State<AppState>,
    Path(id): Path<i64>,
    Query(params): Query<HashMap<String, String>>,
) -> Response {
    if load_pipeline(&state, id).is_err() {
        return ApiError::not_found(format!("管道 #{id} 不存在")).into_response();
    }
    let hours = params
        .get("hours")
        .and_then(|v| v.parse::<i64>().ok())
        .unwrap_or(24)
        .clamp(1, 168);
    let buckets = match state.store().pipeline_metrics_trend(id, hours) {
        Ok(v) => v,
        Err(e) => return ApiError::internal(&e).into_response(),
    };
    let mut points: Vec<TrendPoint> = Vec::with_capacity(buckets.len());
    let mut prev: Option<(i64, i64)> = None;
    for b in buckets {
        let rate_in = prev.map(|(c, _)| (b.consumed - c) as f64 / 300.0);
        let rate_out = prev.map(|(_, f)| (b.flushed - f) as f64 / 300.0);
        points.push(TrendPoint {
            ts: format!("{}:00Z", b.ts),
            consumed: b.consumed,
            flushed: b.flushed,
            rate_in_rps: rate_in,
            rate_out_rps: rate_out,
        });
        prev = Some((b.consumed, b.flushed));
    }
    Json(TrendResponse { points }).into_response()
}

/// `GET /api/pipelines/templates` — SQL templates embedded in the binary
/// (§4.5; the db.table form — dsMapping applies at compile time).
#[utoipa::path(get, path = "/api/pipelines/templates", responses((status = 200, body = TemplatesResponse)))]
pub async fn templates() -> Response {
    Json(TemplatesResponse {
        templates: vec![SqlTemplate {
            name: "kafka-ogg-to-doris".into(),
            label: "OGG 单表直通".into(),
            sql: "-- 模板:OGG 单表直通(kafka ogg → doris;dsMapping 映射 shop/ods 两库)\n\
                  INSERT INTO ods.orders SELECT id, payload, op_type FROM shop.orders WHERE op_type IN ('I','U')"
                .into(),
        }],
    })
    .into_response()
}