zebflow 0.9.1

Reactive web automation platform — pipelines, templates, and real-time collaboration in a single binary.
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
//! Pipeline node interfaces and built-in node implementations.
//!
//! This module is the **single source of truth** for how nodes are authored in Zebflow.
//! Every built-in node lives under [`basic`], in the folder of its DSL family:
//! `n.fs.save` is `basic/fs/save.rs`, `n.kv.get` is `basic/kv/get.rs`, and a
//! family with one node keeps it in that folder's `mod.rs`. Helpers several
//! families use live in [`shared`]. This doc is the living specification —
//! read it before creating or modifying any node.
//!
//! ---
//!
//! # Node Anatomy
//!
//! A complete node module exposes exactly four things:
//!
//! ```text
//! basic/<family>/<node>.rs
//! ├── pub fn definition() -> NodeDefinition   ← kind-level contract (schemas, flags, docs)
//! ├── pub struct Config { ... }               ← typed config with JsonSchema derive
//! ├── pub struct Node { ... }                 ← runtime instance
//! └── impl NodeHandler for Node               ← execution logic
//! ```
//!
//! Then add it to the family's `definitions()` in `basic/<family>/mod.rs`, which
//! `basic/mod.rs → builtin_node_definitions()` reads.  That's it.
//!
//! ---
//!
//! # 1. `definition()` — the node contract
//!
//! `definition()` is the **most important function in a node module**.  It populates
//! [`NodeDefinition`](crate::pipeline::NodeDefinition) — the kind-level contract that
//! drives everything: UI dialogs, DSL parsing, help text, LLM context, API docs.
//!
//! ```rust,ignore
//! pub fn definition() -> NodeDefinition {
//!     NodeDefinition {
//!         kind: NODE_KIND.to_string(),           // "n.category.action"
//!         title: "Human Title".to_string(),      // shown in UI catalogs
//!         description: "...".to_string(),        // shown in UI + fed to LLM
//!         config_schema: serde_json::json!({ ... }),  // or schemars::schema_for!(Config)
//!         input_schema:  serde_json::json!({ ... }),  // payload shape consumed
//!         output_schema: serde_json::json!({ ... }),  // payload shape produced
//!         input_pins:  vec!["in".to_string()],   // empty for trigger nodes
//!         output_pins: vec!["out".to_string()],  // ["out","error"], ["true","false"], etc.
//!         dsl_flags: vec![ ... ],                // see §4 below
//!         script_available: false,               // see §5 below
//!         script_bridge: None,
//!         ai_tool: Default::default(),           // see §6 below
//!     }
//! }
//! ```
//!
//! ## Module-level doc = node documentation
//!
//! Write the module `//!` doc block as if it IS the node's reference page.  Include:
//!
//! - One-line summary of what the node does.
//! - **Pipeline position** — is it a trigger? terminal? middle? never standalone?
//! - **User-facing config table** — only fields the user/agent sets. Never list
//!   platform-injected fields (markup, route, etc.).
//! - **Config schema** — the JSON Schema block so LLMs can read it directly.
//! - **Studio UI hint** — ASCII mockup of how the node dialog should look.
//! - **Input/output payload examples** — what flows in, what flows out.
//! - **DSL examples** — show the pipe chain with all flags.
//! - **Project-level settings** if the node is affected by `zebflow.yaml` settings.
//!
//! The module doc becomes the `description` field at `/docs/node` — **no separate
//! markdown file needed**.  The source IS the docs.
//!
//! ---
//!
//! # 2. Config struct
//!
//! ```rust,ignore
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Clone, Serialize, Deserialize, Default)]
//! pub struct Config {
//!     /// Required: path to the TSX page file relative to templates/.
//!     pub template_path: String,
//!     /// Optional: comma-separated external script URLs.
//!     #[serde(default)]
//!     pub load_scripts: String,
//! }
//! ```
//!
//! Rules:
//! - Field names are `snake_case` — they are the config JSON keys.
//! - Required fields have no `#[serde(default)]`.
//! - Optional fields have `#[serde(default)]` and use `Option<T>` or a defaultable type.
//! - Write a doc comment on every field — it becomes the `description` in `config_schema`.
//! - Platform-injected fields (markup, route, internal ids) live in the struct but must
//!   NOT appear in `dsl_flags` or `config_schema.required`.
//!
//! ---
//!
//! # 3. Config schema
//!
//! Populate `NodeDefinition::config_schema` by hand with a JSON Schema object:
//!
//! ```rust,ignore
//! config_schema: serde_json::json!({
//!     "type": "object",
//!     "required": ["template_path"],
//!     "properties": {
//!         "template_path": {
//!             "type": "string",
//!             "description": "TSX file relative to templates/. Example: pages/blog-home."
//!         },
//!         "load_scripts": {
//!             "type": "string",
//!             "description": "Comma-separated external script URLs. Each must match allow_list."
//!         }
//!     }
//! }),
//! ```
//!
//! Consumers of `config_schema`:
//! - **Studio UI** — renders form fields from `properties` without any TypeScript hardcoding.
//! - **Register validator** — checks incoming node configs before saving to disk.
//! - **LLM agent** — reads schema to know what to put in node config when authoring pipelines.
//! - **`/docs/node`** — exposes schema in the API contract document.
//!
//! ---
//!
//! # 4. DSL flags
//!
//! Every user-facing config field gets a [`DslFlag`](crate::pipeline::model::DslFlag) entry.
//!
//! ```rust,ignore
//! use crate::pipeline::model::{DslFlag, DslFlagKind};
//!
//! dsl_flags: vec![
//!     DslFlag {
//!         flag: "--template-path".to_string(),
//!         config_key: "template_path".to_string(),
//!         description: "TSX page file relative to templates/, e.g. pages/blog-home.".to_string(),
//!         kind: DslFlagKind::Scalar,
//!         required: true,
//!     },
//!     DslFlag {
//!         flag: "--load-scripts".to_string(),
//!         config_key: "load_scripts".to_string(),
//!         description: "Comma-separated external script URLs. Each must match allow_list.".to_string(),
//!         kind: DslFlagKind::CommaSeparatedList,
//!         required: false,
//!     },
//! ],
//! ```
//!
//! ## Universal parsing rule
//!
//! The DSL parser applies: `--flag-name` → `flag_name` (replace `-` with `_`) for ALL flags,
//! declared or not.  `dsl_flags` exists only for documentation, help text, required-flag
//! validation, and LLM context.  **New nodes work in the DSL without touching `parser.rs`.**
//!
//! ## Flag kind reference
//!
//! | `DslFlagKind` | DSL syntax | Config result |
//! |---|---|---|
//! | `Scalar` | `--key value` | `"value"` |
//! | `CommaSeparatedList` | `--key a,b,c` or `--key a --key b` | `["a","b"]` |
//! | `RepeatedList` | `--key a --key b` or `--key a,b` | `["a","b"]` |
//! | `Bool` | `--silent` (no value) | `true` |
//!
//! Body content (after ` -- `) is always captured separately as the node body string.
//!
//! ## Naming convention
//!
//! Flag name = `--kebab-case` of the `snake_case` config key.  Always.  No abbreviations,
//! no aliases.  `--credential-id` not `--cred`.  `--template-path` not `--template`.
//! This makes flags derivable from the schema property names — LLMs can infer them.
//!
//! ---
//!
//! # 5. Script bridge — declared, and built by nothing
//!
//! Write `script_available: false` and `script_bridge: None`.  Every node does.
//!
//! A script cannot reach a node handler.  The `n` object handed to an `n.script` body
//! is assembled by `build_capabilities_expr` in `language/engines/deno_sandbox/pool.rs`
//! from pure `time` and `math` helpers; the sandbox exposes two host ops, neither of
//! which dispatches a node, and hides `Deno.core` from user code.  Declaring `true`
//! would grant nothing and would put a false "n.script access" claim in the node
//! catalog, which is why no definition does.
//!
//! The two fields survive because `NodeDefinition` is a frozen `zebflow.com/v1`
//! contract; see `docs/contracts/kinds/node-definition/README.md`.  Building the bridge
//! is a feature, and the egress rules in `docs/contracts/confinement.md` §3 have to
//! cover it before it exists.
//!
//! ---
//!
//! # 6. AI tool — invokable by LLM agents
//!
//! Set `ai_tool.registered: true` to register the node as a callable tool for Zebtune
//! and other LLM agents running inside the platform:
//!
//! ```rust,ignore
//! ai_tool: NodeAiToolDefinition {
//!     registered: true,
//!     tool_name: "query_database".to_string(),
//!     tool_description: "Run a SQL query against a project database connection and return rows.".to_string(),
//!     tool_input_schema: serde_json::json!({
//!         "type": "object",
//!         "required": ["credential_id", "query"],
//!         "properties": {
//!             "credential_id": { "type": "string", "description": "Database connection slug." },
//!             "query":         { "type": "string", "description": "SQL query to execute." }
//!         }
//!     }),
//! },
//! ```
//!
//! `tool_input_schema` is what the LLM sees — it may differ from `config_schema` if the
//! tool accepts a simpler or different interface than the full node config.
//!
//! ---
//!
//! # 7. `NodeHandler` implementation
//!
//! ```rust,ignore
//! use async_trait::async_trait;
//! use crate::pipeline::nodes::{NodeHandler, NodeExecutionInput, NodeExecutionOutput};
//! use crate::pipeline::PipelineError;
//!
//! pub struct Node { config: Config }
//!
//! impl Node {
//!     pub fn new(config: Config) -> Self { Self { config } }
//! }
//!
//! #[async_trait]
//! impl NodeHandler for Node {
//!     fn kind(&self) -> &'static str { NODE_KIND }
//!
//!     fn input_pins(&self)  -> &'static [&'static str] { &["in"] }
//!     fn output_pins(&self) -> &'static [&'static str] { &["out"] }
//!
//!     async fn execute_async(
//!         &self,
//!         input: NodeExecutionInput,
//!     ) -> Result<NodeExecutionOutput, PipelineError> {
//!         // Read config: self.config.*
//!         // Read upstream payload: input.payload
//!         // Read owner/project: input.metadata.get("owner")
//!         // Read request id: input.metadata.get("request_id")
//!         Ok(NodeExecutionOutput {
//!             output_pins: vec!["out".to_string()],
//!             payload: serde_json::json!({ "result": "..." }),
//!             trace: vec![format!("n.my.node: done")],
//!         })
//!     }
//! }
//! ```
//!
//! Rules:
//! - Always include at least one `trace` entry — it shows in pipeline run logs.
//! - Emit `PipelineError::new("FW_MY_NODE_CODE", "message")` for recoverable errors.
//! - Use `output_pins` to control which downstream edges fire — only listed pins propagate.
//! - For conditional branching, emit `vec!["true"]` or `vec!["false"]` selectively.
//!
//! ---
//!
//! # 8. Registration
//!
//! Declare the file and add your `definition()` call to the family list in
//! `src/pipeline/nodes/basic/<family>/mod.rs` (a new family is a new folder,
//! declared in `basic/mod.rs` and appended to `family_definitions()` there):
//!
//! ```rust,ignore
//! pub mod my_node;
//!
//! pub fn definitions() -> Vec<NodeDefinition> {
//!     vec![
//!         // ... the family's other nodes ...
//!         my_node::definition(),
//!     ]
//! }
//! ```
//!
//! And add the dispatch arm in `src/pipeline/engines/basic.rs` inside the `match node.kind`
//! block so the engine knows how to build and execute your node instance.
//!
//! ---
//!
//! # Complete example
//!
//! ```rust,ignore
//! //! `n.example.echo` — passes the upstream payload through unchanged with a tag.
//! //!
//! //! # Pipeline position
//! //! Middleware node. Always between a trigger and a terminal.
//! //!
//! //! # User-facing config
//! //! | Field | Type | Required | Description |
//! //! |---|---|---|---|
//! //! | `tag` | string | ✓ | Label injected into the payload as `echo_tag` |
//! //!
//! //! # DSL
//! //! ```text
//! //! | n.trigger.webhook --path /ping
//! //! | n.example.echo --tag hello
//! //! ```
//!
//! use async_trait::async_trait;
//! use serde::{Deserialize, Serialize};
//! use serde_json::json;
//! use crate::pipeline::{PipelineError, NodeDefinition};
//! use crate::pipeline::model::{DslFlag, DslFlagKind};
//! use crate::pipeline::nodes::{NodeHandler, NodeExecutionInput, NodeExecutionOutput};
//!
//! pub const NODE_KIND: &str = "n.example.echo";
//!
//! pub fn definition() -> NodeDefinition {
//!     NodeDefinition {
//!         kind: NODE_KIND.to_string(),
//!         title: "Echo".to_string(),
//!         description: "Passes payload through, injecting echo_tag from config.".to_string(),
//!         config_schema: json!({
//!             "type": "object",
//!             "required": ["tag"],
//!             "properties": {
//!                 "tag": { "type": "string", "description": "Label to inject as echo_tag." }
//!             }
//!         }),
//!         input_schema:  json!({ "type": "object" }),
//!         output_schema: json!({ "type": "object", "properties": { "echo_tag": { "type": "string" } } }),
//!         input_pins:  vec!["in".to_string()],
//!         output_pins: vec!["out".to_string()],
//!         dsl_flags: vec![
//!             DslFlag {
//!                 flag: "--tag".to_string(),
//!                 config_key: "tag".to_string(),
//!                 description: "Label injected into the output payload as echo_tag.".to_string(),
//!                 kind: DslFlagKind::Scalar,
//!                 required: true,
//!             },
//!         ],
//!         script_available: false,
//!         script_bridge: None,
//!         ai_tool: Default::default(),
//!     }
//! }
//!
//! #[derive(Debug, Clone, Serialize, Deserialize, Default)]
//! pub struct Config { pub tag: String }
//!
//! pub struct Node { config: Config }
//! impl Node { pub fn new(config: Config) -> Self { Self { config } } }
//!
//! #[async_trait]
//! impl NodeHandler for Node {
//!     fn kind(&self) -> &'static str { NODE_KIND }
//!     fn input_pins(&self)  -> &'static [&'static str] { &["in"] }
//!     fn output_pins(&self) -> &'static [&'static str] { &["out"] }
//!     async fn execute_async(&self, input: NodeExecutionInput) -> Result<NodeExecutionOutput, PipelineError> {
//!         let mut payload = input.payload;
//!         payload["echo_tag"] = json!(self.config.tag.clone());
//!         Ok(NodeExecutionOutput {
//!             output_pins: vec!["out".to_string()],
//!             payload,
//!             trace: vec![format!("n.example.echo: tag={}", self.config.tag)],
//!         })
//!     }
//! }
//! ```

pub mod basic;
mod interface;
pub mod shared;

pub use interface::{NodeExecutionInput, NodeExecutionOutput, NodeHandler};

use std::collections::HashSet;

use crate::pipeline::model::{
    DslFlagKind, LayoutItem, NodeCapability, NodeDefinition, NodeFieldType,
};

/// Returns all built-in node definitions.
pub fn builtin_node_definitions() -> Vec<crate::pipeline::NodeDefinition> {
    basic::builtin_node_definitions()
}

/// What every native node kind can reach, keyed by kind.
///
/// This is the compiled-in half of the answer the package safety review needs,
/// and it is a constant of the build: nothing installed can add to it, remove
/// from it, or disagree with it. Bundle-provided kinds are resolved separately,
/// by deriving them from the nodes they compose.
///
/// A kind with an empty set is present and empty, never absent. "This node
/// reaches nothing" and "nobody asked this node" are different answers and the
/// review reports them differently.
pub fn native_node_capabilities()
-> &'static std::collections::BTreeMap<String, std::collections::BTreeSet<NodeCapability>> {
    static NATIVE: std::sync::LazyLock<
        std::collections::BTreeMap<String, std::collections::BTreeSet<NodeCapability>>,
    > = std::sync::LazyLock::new(|| {
        builtin_node_definitions()
            .into_iter()
            .map(|definition| {
                (
                    definition.kind,
                    definition.capabilities.into_iter().collect(),
                )
            })
            .collect()
    });
    &NATIVE
}

fn validate_layout_item(
    item: &LayoutItem,
    field_names: &HashSet<String>,
    errors: &mut Vec<String>,
) {
    match item {
        LayoutItem::Field(name) => {
            if !field_names.contains(name) {
                errors.push(format!("layout references unknown field '{}'", name));
            }
        }
        LayoutItem::Row { row } => {
            if row.is_empty() {
                errors.push("layout row must not be empty".to_string());
            }
            for child in row {
                validate_layout_item(child, field_names, errors);
            }
        }
        LayoutItem::Col { col } => {
            if col.is_empty() {
                errors.push("layout col must not be empty".to_string());
            }
            for child in col {
                validate_layout_item(child, field_names, errors);
            }
        }
    }
}

/// Validates that a node definition is complete enough to serve as API and docs source.
pub fn validate_node_definition_contract(def: &NodeDefinition) -> Result<(), Vec<String>> {
    let mut errors = Vec::new();
    let kind = def.kind.trim();
    if kind.is_empty() {
        errors.push("kind must not be empty".to_string());
    } else if !kind.starts_with("n.") {
        errors.push(format!("kind '{}' must start with 'n.'", kind));
    }
    if def.title.trim().is_empty() {
        errors.push("title must not be empty".to_string());
    }
    if def.description.trim().is_empty() {
        errors.push("description must not be empty".to_string());
    }

    for (pin_role, pins) in [
        ("input", def.input_pins.as_slice()),
        ("output", def.output_pins.as_slice()),
    ] {
        for pin in pins {
            let trimmed = pin.trim();
            if trimmed.is_empty() {
                errors.push(format!("{} pin must not be empty", pin_role));
            } else if trimmed != pin || trimmed.contains(char::is_whitespace) {
                errors.push(format!(
                    "{} pin '{}' must be a compact token",
                    pin_role, pin
                ));
            }
        }
    }

    let mut flag_keys = HashSet::new();
    for flag in &def.dsl_flags {
        if !flag.flag.starts_with("--") || flag.flag.trim() != flag.flag {
            errors.push(format!(
                "DSL flag '{}' must start with -- and have no surrounding space",
                flag.flag
            ));
        }
        if flag.config_key.trim().is_empty() {
            errors.push(format!(
                "DSL flag '{}' config_key must not be empty",
                flag.flag
            ));
        } else {
            flag_keys.insert(flag.config_key.clone());
        }
        if flag.description.trim().is_empty() {
            errors.push(format!(
                "DSL flag '{}' description must not be empty",
                flag.flag
            ));
        }
    }

    let mut field_names = HashSet::new();
    for field in &def.fields {
        if field.name.trim().is_empty() {
            errors.push("field name must not be empty".to_string());
            continue;
        }
        if !field_names.insert(field.name.clone()) {
            errors.push(format!("field '{}' is declared more than once", field.name));
        }
        if field.label.trim().is_empty() {
            errors.push(format!("field '{}' label must not be empty", field.name));
        }
        if !matches!(field.field_type, NodeFieldType::Section)
            && field.help.as_deref().unwrap_or("").trim().is_empty()
        {
            errors.push(format!("field '{}' help must not be empty", field.name));
        }
    }

    for item in &def.layout {
        validate_layout_item(item, &field_names, &mut errors);
    }

    if let Some(properties) = def
        .config_schema
        .get("properties")
        .and_then(|value| value.as_object())
    {
        for key in properties.keys() {
            if key == "title" || key == "timeout_secs" {
                continue;
            }
            if !field_names.contains(key) && !flag_keys.contains(key) {
                errors.push(format!(
                    "config property '{}' must be documented by a field or DSL flag",
                    key
                ));
            }
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

pub fn format_node_definition_markdown(def: &NodeDefinition) -> String {
    let mut s = String::new();
    s.push_str(&format!("### `{}` — {}\n\n", def.kind, def.title));
    s.push_str(def.description.trim());
    s.push_str("\n\n");
    let ip = if def.input_pins.is_empty() {
        "*(none — trigger / entry)*".to_string()
    } else {
        format!("`{}`", def.input_pins.join("`, `"))
    };
    let op = if def.output_pins.is_empty() {
        "*(dynamic / graph-defined)*".to_string()
    } else {
        format!("`{}`", def.output_pins.join("`, `"))
    };
    s.push_str(&format!(
        "- **Input pins:** {ip}\n- **Output pins:** {op}\n\n"
    ));
    if !def.dsl_flags.is_empty() {
        s.push_str("| DSL flag | Config key | Required | Kind | Description |\n");
        s.push_str("|----------|------------|----------|------|-------------|\n");
        for f in &def.dsl_flags {
            let req = if f.required { "yes" } else { "no" };
            let kind_s = match f.kind {
                DslFlagKind::Scalar => "scalar",
                DslFlagKind::CommaSeparatedList => "comma-list",
                DslFlagKind::RepeatedList => "repeated-list",
                DslFlagKind::Bool => "bool",
                DslFlagKind::KeyValuePairs => "key-value-pairs",
                DslFlagKind::SchemaField => "schema-field",
            };
            let desc = f.description.replace('|', "\\|");
            s.push_str(&format!(
                "| `{}` | `{}` | {} | {} | {} |\n",
                f.flag, f.config_key, req, kind_s, desc
            ));
        }
        s.push_str("\n");
    }
    if !def.input_schema.is_null() {
        s.push_str("**Input payload (schema):**\n```json\n");
        if let Ok(pretty) = serde_json::to_string_pretty(&def.input_schema) {
            s.push_str(&pretty);
        }
        s.push_str("\n```\n\n");
    }
    if !def.output_schema.is_null() {
        s.push_str("**Output payload (schema):**\n```json\n");
        if let Ok(pretty) = serde_json::to_string_pretty(&def.output_schema) {
            s.push_str(&pretty);
        }
        s.push_str("\n```\n\n");
    }
    for example in &def.examples {
        let title = if example.title.is_empty() { "Example" } else { example.title.as_str() };
        s.push_str(&format!("**{title}:**\n"));
        if !example.dsl.is_empty() {
            s.push_str(&format!("```\n| {}\n```\n", example.dsl));
        }
        if !example.description.is_empty() {
            s.push_str(example.description.trim());
            s.push_str("\n");
        }
        if !example.input.is_null() || !example.output.is_null() {
            s.push_str("```json\n");
            if !example.input.is_null() {
                s.push_str(&format!("// input\n{}\n", example.input));
            }
            if !example.output.is_null() {
                s.push_str(&format!("// output\n{}\n", example.output));
            }
            s.push_str("```\n");
        }
        s.push_str("\n");
    }
    s.push_str(&format!(
        "**MCP:** `help_nodes` with `kind=\"{}\"` for this section only.\n\n",
        def.kind
    ));
    s
}

pub fn kind_query_matches_def(def: &NodeDefinition, query: &str) -> bool {
    let q = query.trim();
    if q.is_empty() {
        return false;
    }
    let kn = def.kind.to_lowercase();
    let qn = q.to_lowercase();
    if kn == qn {
        return true;
    }
    if qn.starts_with("n.") {
        return kn == qn;
    }
    if kn == format!("n.{qn}") {
        return true;
    }
    kn.strip_prefix("n.").is_some_and(|tail| tail == qn)
}

/// One node section — same source as [`builtin_node_definitions`].
pub fn node_markdown_by_kind_query(query: &str) -> Option<String> {
    basic::builtin_node_definitions()
        .into_iter()
        .find(|d| kind_query_matches_def(d, query))
        .map(|d| format_node_definition_markdown(&d))
}

/// Full catalog for `help_pipeline` / `help_nodes` — generated from Rust `definition()`, not hand-written markdown.
pub fn builtin_nodes_markdown_reference() -> String {
    let mut s = String::from(
        "## Node kinds (live — from `builtin_node_definitions()`)\n\n\
         This block matches the pipeline editor / node API: titles, descriptions, pins, DSL flags, and input/output schemas.\n\n\
         - **Full catalog:** `help_nodes` with no `kind` (same as this section).\n\
         - **One kind:** `help_nodes` with `kind=\"n.script\"` (or `script`, `trigger.webhook`, etc.).\n\n\
         ---\n\n",
    );
    for def in basic::builtin_node_definitions() {
        s.push_str(&format_node_definition_markdown(&def));
        s.push_str("---\n\n");
    }
    s
}

#[cfg(test)]
mod tests {
    #[test]
    fn native_node_definitions_have_complete_contracts() {
        let mut failures = Vec::new();
        for def in super::builtin_node_definitions() {
            if let Err(error) = crate::contracts::kinds::validate_normalized_node_definition(&def) {
                failures.push(format!("{}: {}", def.kind, error));
            }
        }
        assert!(
            failures.is_empty(),
            "incomplete node definitions:\n{}",
            failures.join("\n")
        );
    }

    /// The canvas-preview flags are injected into every node, the two table
    /// nodes included: their row sample moved to `--preview-rows` /
    /// `preview_rows`, so `--preview` means one thing everywhere.
    #[test]
    fn every_node_takes_the_canvas_preview_flags() {
        let defs = super::builtin_node_definitions();
        let flags_of = |kind: &str| {
            defs.iter()
                .find(|d| d.kind == kind)
                .unwrap_or_else(|| panic!("{kind} missing"))
                .dsl_flags
                .iter()
                .map(|f| (f.flag.clone(), f.config_key.clone()))
                .collect::<Vec<_>>()
        };

        for kind in ["n.table.query", "n.table.convert", "n.script"] {
            let flags = flags_of(kind);
            assert!(
                flags.iter().any(|(flag, key)| flag == "--preview" && key == "preview.out"),
                "{kind} takes --preview: {flags:?}"
            );
            assert!(
                flags.iter().any(|(flag, key)| flag == "--preview-in" && key == "preview.in"),
                "{kind} takes --preview-in: {flags:?}"
            );
            assert!(
                !flags.iter().any(|(_, key)| key == "preview"),
                "{kind} no longer stores anything flat at `preview`: {flags:?}"
            );
        }
        for kind in ["n.table.query", "n.table.convert"] {
            let flags = flags_of(kind);
            assert!(
                flags.iter().any(|(flag, key)| flag == "--preview-rows" && key == "preview_rows"),
                "{kind} samples rows with --preview-rows: {flags:?}"
            );
        }
    }

    /// The source layout is the catalogue's family list: one folder per DSL
    /// family under `basic/`, one file per node, nothing loose beside
    /// `basic/mod.rs`. A family with one node keeps it in that folder's
    /// `mod.rs`; helpers several families share live in `nodes/shared/`.
    #[test]
    fn every_node_file_lives_in_its_family_folder() {
        let basic = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/pipeline/nodes/basic");
        let families: std::collections::BTreeSet<String> = super::builtin_node_definitions()
            .iter()
            .filter_map(|def| def.kind.strip_prefix("n."))
            .map(|rest| rest.split('.').next().unwrap_or(rest).to_string())
            .collect();

        let mut failures = Vec::new();
        let mut folders = std::collections::BTreeSet::new();
        for entry in std::fs::read_dir(&basic).expect("basic/ is readable") {
            let entry = entry.expect("entry");
            let name = entry.file_name().to_string_lossy().to_string();
            if entry.path().is_dir() {
                if !families.contains(&name) {
                    failures.push(format!(
                        "basic/{name}/ is not a DSL family of the catalogue (families: {})",
                        families.iter().cloned().collect::<Vec<_>>().join(", ")
                    ));
                }
                folders.insert(name);
            } else if name != "mod.rs" {
                failures.push(format!(
                    "basic/{name} sits beside mod.rs; a node file is basic/<family>/<node>.rs"
                ));
            }
        }
        for family in &families {
            if !folders.contains(family) {
                failures.push(format!("family `{family}` has nodes in the catalogue but no basic/{family}/ folder"));
            }
        }
        assert!(failures.is_empty(), "node layout:\n{}", failures.join("\n"));
    }

    #[test]
    fn embedded_official_node_definitions_have_complete_contracts() {
        let mut failures = Vec::new();
        for def in crate::platform::services::node_registry::NodeRegistryService::embedded_official_definitions() {
            if let Err(error) = crate::contracts::kinds::validate_normalized_node_definition(&def) {
                failures.push(format!("{}: {}", def.kind, error));
            }
        }
        assert!(
            failures.is_empty(),
            "incomplete embedded official node definitions:\n{}",
            failures.join("\n")
        );
    }
}