deciduous 0.15.0

Decision graph tooling for AI-assisted development. Track every goal, decision, and outcome. Survive context loss. Query your reasoning.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
//! MCP tool registry — definitions and dispatch.
//!
//! **Functional core**: tool definitions are pure data, dispatch is a pure
//! function mapping tool names to handler functions. No IO in this module.

use crate::mcp::protocol::ToolDefinition;
use serde_json::{json, Value};

/// Build the complete list of tool definitions for `tools/list`.
///
/// Each tool has a name, description, and JSON Schema for its parameters.
/// This is a pure function — returns fresh data each call.
pub fn all_tool_definitions() -> Vec<ToolDefinition> {
    vec![
        // -----------------------------------------------------------------
        // Graph CRUD
        // -----------------------------------------------------------------
        ToolDefinition {
            name: "add_node".to_string(),
            description: "Add a new node to the decision graph. Node types: goal, decision, option, action, outcome, observation, revisit.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_type": {
                        "type": "string",
                        "description": "Node type: goal, decision, option, action, outcome, observation, or revisit",
                        "enum": ["goal", "decision", "option", "action", "outcome", "observation", "revisit"]
                    },
                    "title": {
                        "type": "string",
                        "description": "Title of the node"
                    },
                    "description": {
                        "type": "string",
                        "description": "Optional description with more detail"
                    },
                    "confidence": {
                        "type": "integer",
                        "description": "Confidence level 0-100",
                        "minimum": 0,
                        "maximum": 100
                    },
                    "prompt": {
                        "type": "string",
                        "description": "The user prompt that triggered this node (capture verbatim for context recovery)"
                    },
                    "files": {
                        "type": "string",
                        "description": "Comma-separated list of associated files"
                    },
                    "branch": {
                        "type": "string",
                        "description": "Git branch (auto-detected if omitted)"
                    },
                    "commit": {
                        "type": "string",
                        "description": "Git commit hash to link. Use 'HEAD' to auto-detect current commit."
                    }
                },
                "required": ["node_type", "title"]
            }),
        },

        ToolDefinition {
            name: "link_nodes".to_string(),
            description: "Create an edge between two nodes in the decision graph.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "from_id": {
                        "type": "integer",
                        "description": "Source node ID"
                    },
                    "to_id": {
                        "type": "integer",
                        "description": "Target node ID"
                    },
                    "rationale": {
                        "type": "string",
                        "description": "Reason for this connection"
                    },
                    "edge_type": {
                        "type": "string",
                        "description": "Edge type (default: leads_to)",
                        "enum": ["leads_to", "requires", "chosen", "rejected", "blocks", "enables"],
                        "default": "leads_to"
                    }
                },
                "required": ["from_id", "to_id"]
            }),
        },

        ToolDefinition {
            name: "unlink_nodes".to_string(),
            description: "Remove an edge between two nodes.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "from_id": {
                        "type": "integer",
                        "description": "Source node ID"
                    },
                    "to_id": {
                        "type": "integer",
                        "description": "Target node ID"
                    }
                },
                "required": ["from_id", "to_id"]
            }),
        },

        ToolDefinition {
            name: "delete_node".to_string(),
            description: "Delete a node and all its connected edges. Use dry_run to preview.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "ID of the node to delete"
                    },
                    "dry_run": {
                        "type": "boolean",
                        "description": "If true, show what would be deleted without actually deleting",
                        "default": false
                    }
                },
                "required": ["node_id"]
            }),
        },

        ToolDefinition {
            name: "update_status".to_string(),
            description: "Update the status of a node (active, superseded, abandoned, pending, completed, rejected).".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to update"
                    },
                    "status": {
                        "type": "string",
                        "description": "New status",
                        "enum": ["pending", "active", "completed", "rejected", "superseded", "abandoned"]
                    }
                },
                "required": ["node_id", "status"]
            }),
        },

        ToolDefinition {
            name: "update_prompt".to_string(),
            description: "Add or update the verbatim user prompt stored on a node.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to update"
                    },
                    "prompt": {
                        "type": "string",
                        "description": "The verbatim user prompt text"
                    }
                },
                "required": ["node_id", "prompt"]
            }),
        },

        // -----------------------------------------------------------------
        // Graph Querying
        // -----------------------------------------------------------------
        ToolDefinition {
            name: "list_nodes".to_string(),
            description: "List all nodes in the decision graph with optional filters for branch, type, theme, and status.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "branch": {
                        "type": "string",
                        "description": "Filter by git branch"
                    },
                    "node_type": {
                        "type": "string",
                        "description": "Filter by type: goal, decision, option, action, outcome, observation, revisit"
                    },
                    "status": {
                        "type": "string",
                        "description": "Filter by status: active, pending, completed, superseded, abandoned"
                    },
                    "theme": {
                        "type": "string",
                        "description": "Filter by theme name"
                    }
                }
            }),
        },

        ToolDefinition {
            name: "list_edges".to_string(),
            description: "List all edges in the decision graph.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {}
            }),
        },

        ToolDefinition {
            name: "show_node".to_string(),
            description: "Show detailed information about a single node including its connections, themes, and documents.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to display"
                    }
                },
                "required": ["node_id"]
            }),
        },

        ToolDefinition {
            name: "get_graph".to_string(),
            description: "Export the full decision graph as JSON (all nodes and edges).".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {}
            }),
        },

        ToolDefinition {
            name: "search_nodes".to_string(),
            description: "Search nodes by text across titles, descriptions, and prompts. Returns matching nodes with context.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "Search query (matched against title, description, and prompt)"
                    },
                    "node_type": {
                        "type": "string",
                        "description": "Optional: filter results by node type"
                    },
                    "branch": {
                        "type": "string",
                        "description": "Optional: filter results by branch"
                    }
                },
                "required": ["query"]
            }),
        },

        // -----------------------------------------------------------------
        // Analysis / Reporting
        // -----------------------------------------------------------------
        ToolDefinition {
            name: "trace_chain".to_string(),
            description: "Trace the full chain of connected nodes from a starting node using BFS. Returns the complete connected subgraph — follow all edges in both directions to find the entire decision tree.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Starting node ID for the trace"
                    },
                    "max_depth": {
                        "type": "integer",
                        "description": "Maximum traversal depth (0 = unlimited)",
                        "default": 0
                    },
                    "direction": {
                        "type": "string",
                        "description": "Traversal direction: both, outgoing, incoming",
                        "enum": ["both", "outgoing", "incoming"],
                        "default": "both"
                    }
                },
                "required": ["node_id"]
            }),
        },

        ToolDefinition {
            name: "get_node_context".to_string(),
            description: "Get the full context around a node: the node itself, its parents (incoming edges), children (outgoing edges), sibling nodes, associated themes, and documents. Ideal for understanding a single decision point.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to get context for"
                    }
                },
                "required": ["node_id"]
            }),
        },

        ToolDefinition {
            name: "get_timeline".to_string(),
            description: "Get a chronological timeline of nodes, newest first. Useful for understanding what happened and when.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of nodes to return (0 = all)",
                        "default": 50
                    },
                    "node_type": {
                        "type": "string",
                        "description": "Filter by node type"
                    },
                    "branch": {
                        "type": "string",
                        "description": "Filter by git branch"
                    },
                    "since": {
                        "type": "string",
                        "description": "Only show nodes created after this date (YYYY-MM-DD)"
                    }
                }
            }),
        },

        ToolDefinition {
            name: "get_pulse".to_string(),
            description: "Get the health and status of the decision graph: summary statistics, active goals, recent activity, orphaned nodes, and connection gaps.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "branch": {
                        "type": "string",
                        "description": "Filter by git branch"
                    }
                }
            }),
        },

        ToolDefinition {
            name: "find_orphans".to_string(),
            description: "Find nodes that violate connection rules: outcomes without parent actions, actions without parent decisions, decisions without parent options, etc. Root goals are excluded (they are valid orphans).".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {}
            }),
        },

        ToolDefinition {
            name: "get_branch_summary".to_string(),
            description: "Get a summary of all decisions and activity on a specific git branch. Shows goals, decisions made, actions taken, and outcomes.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "branch": {
                        "type": "string",
                        "description": "Git branch name to summarize"
                    }
                },
                "required": ["branch"]
            }),
        },

        // -----------------------------------------------------------------
        // Documents
        // -----------------------------------------------------------------
        ToolDefinition {
            name: "attach_document".to_string(),
            description: "Attach a file to a decision graph node.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to attach the file to"
                    },
                    "file_path": {
                        "type": "string",
                        "description": "Path to the file to attach"
                    },
                    "description": {
                        "type": "string",
                        "description": "Description of the document"
                    }
                },
                "required": ["node_id", "file_path"]
            }),
        },

        ToolDefinition {
            name: "list_documents".to_string(),
            description: "List documents attached to a specific node, or all documents if no node ID given.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to list documents for (omit for all)"
                    }
                }
            }),
        },

        // -----------------------------------------------------------------
        // Themes
        // -----------------------------------------------------------------
        ToolDefinition {
            name: "list_themes".to_string(),
            description: "List all defined themes.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {}
            }),
        },

        ToolDefinition {
            name: "create_theme".to_string(),
            description: "Create a new theme for tagging nodes.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "Theme name (lowercase, dash-separated)"
                    },
                    "color": {
                        "type": "string",
                        "description": "Hex color code (e.g., '#ef4444')",
                        "default": "#6b7280"
                    },
                    "description": {
                        "type": "string",
                        "description": "Theme description"
                    }
                },
                "required": ["name"]
            }),
        },

        ToolDefinition {
            name: "tag_node".to_string(),
            description: "Add a theme tag to a node.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to tag"
                    },
                    "theme": {
                        "type": "string",
                        "description": "Theme name to apply"
                    }
                },
                "required": ["node_id", "theme"]
            }),
        },

        ToolDefinition {
            name: "untag_node".to_string(),
            description: "Remove a theme tag from a node.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "node_id": {
                        "type": "integer",
                        "description": "Node ID to untag"
                    },
                    "theme": {
                        "type": "string",
                        "description": "Theme name to remove"
                    }
                },
                "required": ["node_id", "theme"]
            }),
        },

        // -----------------------------------------------------------------
        // Export
        // -----------------------------------------------------------------
        ToolDefinition {
            name: "export_dot".to_string(),
            description: "Export the decision graph (or a subset) as DOT format for visualization.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "roots": {
                        "type": "string",
                        "description": "Root node IDs (comma-separated) — traverses children"
                    },
                    "nodes": {
                        "type": "string",
                        "description": "Specific node IDs or ranges (e.g., '1-11' or '1,3,5-10')"
                    },
                    "title": {
                        "type": "string",
                        "description": "Graph title"
                    },
                    "rankdir": {
                        "type": "string",
                        "description": "Graph direction: TB (top-bottom) or LR (left-right)",
                        "enum": ["TB", "LR"],
                        "default": "TB"
                    }
                }
            }),
        },

        ToolDefinition {
            name: "generate_writeup".to_string(),
            description: "Generate a PR writeup from the decision graph. Returns markdown text summarizing goals, decisions, and outcomes.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "title": {
                        "type": "string",
                        "description": "PR title"
                    },
                    "roots": {
                        "type": "string",
                        "description": "Root node IDs (comma-separated)"
                    },
                    "nodes": {
                        "type": "string",
                        "description": "Specific node IDs or ranges"
                    },
                    "no_dot": {
                        "type": "boolean",
                        "description": "Skip DOT graph section",
                        "default": false
                    },
                    "no_test_plan": {
                        "type": "boolean",
                        "description": "Skip test plan section",
                        "default": false
                    }
                }
            }),
        },

        // -----------------------------------------------------------------
        // Sessions (conversation-scoped trees)
        // -----------------------------------------------------------------
        ToolDefinition {
            name: "start_session".to_string(),
            description: "Start a new conversation session. Creates a root goal node for this conversation and returns a session ID. All nodes created during this session are automatically associated with it, giving each conversation its own decision tree.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "Session name (e.g., 'cowork: auth refactor' or 'chat: debugging memory leak')"
                    },
                    "goal_title": {
                        "type": "string",
                        "description": "Title for the root goal node of this session"
                    },
                    "goal_prompt": {
                        "type": "string",
                        "description": "The initial user prompt/request that started this conversation (verbatim)"
                    }
                },
                "required": ["name", "goal_title"]
            }),
        },

        ToolDefinition {
            name: "end_session".to_string(),
            description: "End the current conversation session. Marks it as complete with an optional summary.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "summary": {
                        "type": "string",
                        "description": "Brief summary of what was accomplished in this session"
                    }
                }
            }),
        },

        ToolDefinition {
            name: "get_session".to_string(),
            description: "Get details about the current session or a specific session, including all its nodes.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "session_id": {
                        "type": "integer",
                        "description": "Session ID to query (omit for current session)"
                    }
                }
            }),
        },

        ToolDefinition {
            name: "resume_session".to_string(),
            description: "Resume a previously started session by ID. Use this to reconnect to a long-running conversation's decision tree after a break or server restart.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "session_id": {
                        "type": "integer",
                        "description": "Session ID to resume"
                    }
                },
                "required": ["session_id"]
            }),
        },

        ToolDefinition {
            name: "list_sessions".to_string(),
            description: "List all conversation sessions (decision trees), optionally filtering to only active ones.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "active_only": {
                        "type": "boolean",
                        "description": "If true, only show sessions that haven't ended",
                        "default": false
                    }
                }
            }),
        },

        ToolDefinition {
            name: "events_status".to_string(),
            description: "Show the status of event-based multi-user sync: pending events, last checkpoint, teammate activity.".to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {}
            }),
        },
    ]
}

/// Look up a tool definition by name. Returns None if not found.
pub fn find_tool(name: &str) -> Option<ToolDefinition> {
    all_tool_definitions().into_iter().find(|t| t.name == name)
}

/// Get all tool names as a sorted list.
pub fn tool_names() -> Vec<String> {
    let mut names: Vec<String> = all_tool_definitions().into_iter().map(|t| t.name).collect();
    names.sort();
    names
}

/// Validate that a tool name exists in the registry.
pub fn is_valid_tool(name: &str) -> bool {
    all_tool_definitions().iter().any(|t| t.name == name)
}

/// Validate tool arguments against the tool's input schema.
/// Returns Ok(()) if valid, Err(message) if invalid.
pub fn validate_tool_args(tool_name: &str, args: &Value) -> Result<(), String> {
    let tool = find_tool(tool_name).ok_or_else(|| format!("Unknown tool: {tool_name}"))?;

    let schema = &tool.input_schema;

    // Check required fields
    if let Some(required) = schema.get("required").and_then(Value::as_array) {
        for field in required {
            if let Some(field_name) = field.as_str() {
                let empty = serde_json::Map::new();
                let args_obj = args.as_object().unwrap_or(&empty);
                if !args_obj.contains_key(field_name) {
                    return Err(format!("Missing required parameter: {field_name}"));
                }
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_all_tools_have_valid_schemas() {
        let tools = all_tool_definitions();
        for tool in &tools {
            assert!(!tool.name.is_empty(), "Tool name must not be empty");
            assert!(
                !tool.description.is_empty(),
                "Tool {} must have a description",
                tool.name
            );
            assert!(
                tool.input_schema.is_object(),
                "Tool {} input_schema must be an object",
                tool.name
            );
            assert_eq!(
                tool.input_schema["type"], "object",
                "Tool {} input_schema type must be 'object'",
                tool.name
            );
        }
    }

    #[test]
    fn test_no_duplicate_tool_names() {
        let tools = all_tool_definitions();
        let mut seen = std::collections::HashSet::new();
        for tool in &tools {
            assert!(
                seen.insert(&tool.name),
                "Duplicate tool name: {}",
                tool.name
            );
        }
    }

    #[test]
    fn test_tool_count() {
        let tools = all_tool_definitions();
        // 6 CRUD + 5 Query + 6 Analysis + 2 Docs + 4 Themes + 5 Sessions + 3 Export = 31
        assert_eq!(tools.len(), 31, "Expected 31 tools, got {}", tools.len());
    }

    #[test]
    fn test_find_tool_exists() {
        assert!(find_tool("add_node").is_some());
        assert!(find_tool("link_nodes").is_some());
        assert!(find_tool("trace_chain").is_some());
        assert!(find_tool("get_pulse").is_some());
    }

    #[test]
    fn test_find_tool_not_exists() {
        assert!(find_tool("nonexistent_tool").is_none());
        assert!(find_tool("").is_none());
    }

    #[test]
    fn test_is_valid_tool() {
        assert!(is_valid_tool("add_node"));
        assert!(is_valid_tool("list_nodes"));
        assert!(!is_valid_tool("fake_tool"));
    }

    #[test]
    fn test_tool_names_sorted() {
        let names = tool_names();
        let mut sorted = names.clone();
        sorted.sort();
        assert_eq!(names, sorted, "tool_names() must return sorted names");
    }

    #[test]
    fn test_validate_tool_args_valid() {
        let args = serde_json::json!({"node_type": "goal", "title": "Test"});
        assert!(validate_tool_args("add_node", &args).is_ok());
    }

    #[test]
    fn test_validate_tool_args_missing_required() {
        // add_node requires node_type and title
        let args = serde_json::json!({"node_type": "goal"});
        let result = validate_tool_args("add_node", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("title"));
    }

    #[test]
    fn test_validate_tool_args_no_required_fields() {
        // list_nodes has no required fields
        let args = serde_json::json!({});
        assert!(validate_tool_args("list_nodes", &args).is_ok());
    }

    #[test]
    fn test_validate_tool_args_unknown_tool() {
        let args = serde_json::json!({});
        let result = validate_tool_args("fake_tool", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Unknown tool"));
    }

    #[test]
    fn test_crud_tools_present() {
        let names = tool_names();
        for name in &[
            "add_node",
            "link_nodes",
            "unlink_nodes",
            "delete_node",
            "update_status",
            "update_prompt",
        ] {
            assert!(names.contains(&name.to_string()), "Missing CRUD tool: {name}");
        }
    }

    #[test]
    fn test_query_tools_present() {
        let names = tool_names();
        for name in &[
            "list_nodes",
            "list_edges",
            "show_node",
            "get_graph",
            "search_nodes",
        ] {
            assert!(
                names.contains(&name.to_string()),
                "Missing query tool: {name}"
            );
        }
    }

    #[test]
    fn test_analysis_tools_present() {
        let names = tool_names();
        for name in &[
            "trace_chain",
            "get_node_context",
            "get_timeline",
            "get_pulse",
            "find_orphans",
            "get_branch_summary",
        ] {
            assert!(
                names.contains(&name.to_string()),
                "Missing analysis tool: {name}"
            );
        }
    }

    #[test]
    fn test_add_node_schema_has_enum() {
        let tool = find_tool("add_node").unwrap();
        let node_type_enum = &tool.input_schema["properties"]["node_type"]["enum"];
        assert!(node_type_enum.is_array());
        let types: Vec<&str> = node_type_enum
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap())
            .collect();
        assert!(types.contains(&"goal"));
        assert!(types.contains(&"decision"));
        assert!(types.contains(&"observation"));
        assert!(types.contains(&"revisit"));
    }

    #[test]
    fn test_link_nodes_schema_has_edge_types() {
        let tool = find_tool("link_nodes").unwrap();
        let edge_enum = &tool.input_schema["properties"]["edge_type"]["enum"];
        assert!(edge_enum.is_array());
        let types: Vec<&str> = edge_enum
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap())
            .collect();
        assert!(types.contains(&"leads_to"));
        assert!(types.contains(&"rejected"));
    }

    #[test]
    fn test_all_tools_serialize_cleanly() {
        let tools = all_tool_definitions();
        for tool in &tools {
            let serialized = serde_json::to_string(tool);
            assert!(
                serialized.is_ok(),
                "Tool {} failed to serialize: {:?}",
                tool.name,
                serialized.err()
            );
        }
    }
}