deciduous 1.0.12

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
//! Configuration file support for deciduous
//!
//! Reads from .deciduous/config.toml

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Configuration structure
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct Config {
    /// Branch settings
    #[serde(default)]
    pub branch: BranchConfig,

    /// GitHub settings for external repository references
    #[serde(default)]
    pub github: GithubConfig,

    /// Claude Code hooks configuration
    #[serde(default)]
    pub hooks: HooksConfig,

    /// Claude Code commands configuration
    #[serde(default)]
    pub commands: CommandsConfig,

    /// Claude Code skills configuration
    #[serde(default)]
    pub skills: SkillsConfig,

    /// Shared graph server. Holds a URL only — the token is read from
    /// DECIDUOUS_MCP_TOKEN so that committing this file leaks nothing.
    #[serde(default)]
    pub remote: crate::remote::RemoteConfig,
}

/// Hooks configuration for Claude Code integration
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct HooksConfig {
    /// Whether hooks are enabled
    #[serde(default = "default_true")]
    pub enabled: bool,

    /// Pre-tool-use hooks (run before Edit/Write/etc)
    #[serde(default = "default_pre_tool_use_hooks")]
    pub pre_tool_use: Vec<Hook>,

    /// Post-tool-use hooks (run after Bash/etc)
    #[serde(default = "default_post_tool_use_hooks")]
    pub post_tool_use: Vec<Hook>,
}

// No hooks by default. require-action-node and post-commit-reminder were
// removed in 1.0.3; only hooks a project lists in config.toml are installed.
fn default_pre_tool_use_hooks() -> Vec<Hook> {
    Vec::new()
}

fn default_post_tool_use_hooks() -> Vec<Hook> {
    Vec::new()
}

impl Default for HooksConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            pre_tool_use: default_pre_tool_use_hooks(),
            post_tool_use: default_post_tool_use_hooks(),
        }
    }
}

/// A single hook definition
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Hook {
    /// Hook name (used for filename)
    pub name: String,

    /// Description of what this hook does
    #[serde(default)]
    pub description: String,

    /// Regex pattern for matching tools (e.g., "Edit|Write" or "Bash")
    pub matcher: String,

    /// Whether this hook is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,

    /// Script content (if inline) - mutually exclusive with script_path
    #[serde(default)]
    pub script: Option<String>,

    /// Path to script file (relative to .deciduous/) - mutually exclusive with script
    #[serde(default)]
    pub script_path: Option<String>,
}

impl Hook {
    /// Check if this hook uses a built-in script
    pub fn uses_builtin(&self) -> bool {
        self.script.is_none() && self.script_path.is_none()
    }
}

/// Commands configuration for Claude Code slash commands
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct CommandsConfig {
    /// Whether to install default commands
    #[serde(default = "default_true")]
    pub install_defaults: bool,

    /// Custom commands to install (paths relative to .deciduous/commands/)
    #[serde(default)]
    pub custom: Vec<String>,
}

impl Default for CommandsConfig {
    fn default() -> Self {
        Self {
            install_defaults: true,
            custom: vec![],
        }
    }
}

/// Skills configuration for Claude Code skills
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SkillsConfig {
    /// Whether to install default skills
    #[serde(default = "default_true")]
    pub install_defaults: bool,

    /// Custom skills to install (paths relative to .deciduous/skills/)
    #[serde(default)]
    pub custom: Vec<String>,
}

impl Default for SkillsConfig {
    fn default() -> Self {
        Self {
            install_defaults: true,
            custom: vec![],
        }
    }
}

/// GitHub-related configuration for commit/PR links
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct GithubConfig {
    /// External repository for commit links (e.g., "phoenixframework/phoenix")
    /// When set, commit hashes in nodes will link to this repo instead of the local one.
    /// Format: "owner/repo"
    #[serde(default)]
    pub commit_repo: Option<String>,
}

/// Branch-related configuration
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct BranchConfig {
    /// Main/default branch names (nodes on these branches won't trigger special grouping)
    /// Default: ["main", "master"]
    #[serde(default = "default_main_branches")]
    pub main_branches: Vec<String>,

    /// Whether to auto-detect and store branch on node creation
    /// Default: true
    #[serde(default = "default_true")]
    pub auto_detect: bool,
}

fn default_main_branches() -> Vec<String> {
    vec!["main".to_string(), "master".to_string()]
}

fn default_true() -> bool {
    true
}

impl Default for BranchConfig {
    fn default() -> Self {
        Self {
            main_branches: default_main_branches(),
            auto_detect: true,
        }
    }
}

/// Parses a config for editing, with `[name]` present. A missing table is
/// appended to the text, after everything in it, before parsing.
///
/// Why not `doc[name].or_insert(table())`: toml_edit keeps comments that
/// follow the last table's keys as the document's trailing text, and a table
/// it inserts goes before that text. `init`'s config ends in `[updates]`
/// followed only by comments, so `remote init` produced `[updates]`, blank,
/// `[remote]`, url, workspace, and then `[updates]`'s comments, in a file
/// every clone commits.
pub fn parse_with_table(existing: &str, name: &str) -> Result<toml_edit::DocumentMut, String> {
    let doc = existing
        .parse::<toml_edit::DocumentMut>()
        .map_err(|e| e.to_string())?;
    if doc.contains_key(name) {
        return Ok(doc);
    }
    let mut text = existing.to_string();
    if !text.is_empty() && !text.ends_with('\n') {
        text.push('\n');
    }
    if !text.is_empty() {
        text.push('\n');
    }
    text.push_str(&format!("[{name}]\n"));
    text.parse::<toml_edit::DocumentMut>()
        .map_err(|e| e.to_string())
}

impl Config {
    /// Load config from .deciduous/config.toml
    /// Returns default config if file doesn't exist
    ///
    /// A linked worktree whose config has no `[remote]` url takes the main
    /// checkout's ([`crate::remote::inherited_remote`]).
    pub fn load() -> Self {
        let Some(dir) = Self::find_deciduous_dir() else {
            return Self::default();
        };
        let mut config: Self = std::fs::read_to_string(dir.join("config.toml"))
            .ok()
            .and_then(|contents| toml::from_str(&contents).ok())
            .unwrap_or_default();
        if !config.remote.is_configured() {
            if let Some(inherited) = crate::remote::inherited_remote(&dir) {
                config.remote = inherited.remote;
            }
        }
        config
    }

    /// Write the `[remote]` section into `.deciduous/config.toml`, leaving
    /// everything else in the file exactly as it was.
    ///
    /// Done with `toml_edit` rather than by deserializing and re-serializing.
    /// A round-trip through `toml::Value` drops every comment and reflows
    /// arrays, so adding one two-line table to a hand-written config rewrote
    /// the whole file:
    ///
    /// ```text
    /// -# Deciduous Configuration
    /// -# This file controls branch detection and grouping behavior
    /// -main_branches = ["main", "master"]
    /// +main_branches = [
    /// +    "main",
    /// +]
    /// ```
    ///
    /// Nothing was lost semantically, which is what makes it easy to miss.
    pub fn save_remote(&self) -> std::io::Result<PathBuf> {
        let dir = Self::find_deciduous_dir().ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "no .deciduous directory found; run `deciduous init` first",
            )
        })?;
        let path = dir.join("config.toml");

        let existing = std::fs::read_to_string(&path).unwrap_or_default();
        let mut doc = parse_with_table(&existing, "remote").map_err(|e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("config.toml is not valid TOML: {e}"),
            )
        })?;

        let table = doc["remote"].or_insert(toml_edit::table());
        if let Some(url) = &self.remote.url {
            table["url"] = toml_edit::value(url.clone());
        }
        match &self.remote.workspace {
            Some(ws) => table["workspace"] = toml_edit::value(ws.clone()),
            // Clearing the override has to remove the key; leaving a stale one
            // behind would silently keep writing to the old workspace.
            None => {
                if let Some(t) = table.as_table_mut() {
                    t.remove("workspace");
                }
            }
        }

        std::fs::write(&path, doc.to_string())?;
        Ok(path)
    }

    /// Find .deciduous directory by walking up directory tree
    pub fn find_deciduous_dir() -> Option<PathBuf> {
        let current_dir = std::env::current_dir().ok()?;
        let mut dir = current_dir.as_path();

        loop {
            let deciduous_dir = dir.join(".deciduous");
            if deciduous_dir.exists() {
                return Some(deciduous_dir);
            }

            match dir.parent() {
                Some(parent) => dir = parent,
                None => break,
            }
        }
        None
    }

    /// Find the project root (parent of .deciduous)
    pub fn find_project_root() -> Option<PathBuf> {
        Self::find_deciduous_dir().and_then(|d| d.parent().map(|p| p.to_path_buf()))
    }

    /// Check if a branch is considered a "main" branch
    pub fn is_main_branch(&self, branch: &str) -> bool {
        self.branch.main_branches.iter().any(|b| b == branch)
    }

    /// Get all enabled pre-tool-use hooks
    pub fn enabled_pre_hooks(&self) -> Vec<&Hook> {
        if !self.hooks.enabled {
            return vec![];
        }
        self.hooks
            .pre_tool_use
            .iter()
            .filter(|h| h.enabled)
            .collect()
    }

    /// Get all enabled post-tool-use hooks
    pub fn enabled_post_hooks(&self) -> Vec<&Hook> {
        if !self.hooks.enabled {
            return vec![];
        }
        self.hooks
            .post_tool_use
            .iter()
            .filter(|h| h.enabled)
            .collect()
    }
}

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

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert!(config.is_main_branch("main"));
        assert!(config.is_main_branch("master"));
        assert!(!config.is_main_branch("feature-x"));
        assert!(config.branch.auto_detect);
    }

    #[test]
    fn test_parse_config() {
        let toml = r#"
[branch]
main_branches = ["main", "master", "develop"]
auto_detect = true
"#;
        let config: Config = toml::from_str(toml).unwrap();
        assert!(config.is_main_branch("develop"));
        assert!(!config.is_main_branch("feature-x"));
    }

    #[test]
    fn test_default_hooks() {
        let config = Config::default();
        assert!(config.hooks.enabled);

        // No logging hooks by default since 1.0.3.
        assert!(config.hooks.pre_tool_use.is_empty());
        assert!(config.hooks.post_tool_use.is_empty());
    }

    #[test]
    fn test_parse_hooks_config() {
        let toml = r#"
[hooks]
enabled = true

[[hooks.pre_tool_use]]
name = "my-custom-hook"
description = "A custom pre-edit hook"
matcher = "Edit"
enabled = true
script = "echo 'hello'"

[[hooks.post_tool_use]]
name = "my-post-hook"
description = "A custom post hook"
matcher = "Bash"
enabled = false
"#;
        let config: Config = toml::from_str(toml).unwrap();
        assert!(config.hooks.enabled);
        assert_eq!(config.hooks.pre_tool_use.len(), 1);
        assert_eq!(config.hooks.pre_tool_use[0].name, "my-custom-hook");
        assert_eq!(
            config.hooks.pre_tool_use[0].script,
            Some("echo 'hello'".to_string())
        );

        // Check enabled_post_hooks filters correctly
        assert_eq!(config.enabled_post_hooks().len(), 0);
    }

    #[test]
    fn test_hooks_disabled() {
        let toml = r#"
[hooks]
enabled = false
"#;
        let config: Config = toml::from_str(toml).unwrap();
        assert!(!config.hooks.enabled);
        // Even with default hooks, enabled_*_hooks should return empty
        assert_eq!(config.enabled_pre_hooks().len(), 0);
        assert_eq!(config.enabled_post_hooks().len(), 0);
    }

    #[test]
    fn test_hook_uses_builtin() {
        let hook = Hook {
            name: "require-action-node".to_string(),
            description: "".to_string(),
            matcher: "Edit".to_string(),
            enabled: true,
            script: None,
            script_path: None,
        };
        assert!(hook.uses_builtin());

        let custom_hook = Hook {
            name: "custom".to_string(),
            description: "".to_string(),
            matcher: "Edit".to_string(),
            enabled: true,
            script: Some("echo hi".to_string()),
            script_path: None,
        };
        assert!(!custom_hook.uses_builtin());
    }
}

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

    /// Regression: the first version round-tripped through `toml::Value`,
    /// which silently deleted every comment and reflowed arrays. Nothing was
    /// lost semantically, so it looked fine until the diff was read.
    #[test]
    fn writing_the_remote_table_keeps_comments_and_formatting() {
        let dir = std::env::temp_dir().join(format!("deciduous-cfg-{}", std::process::id()));
        let deciduous = dir.join(".deciduous");
        std::fs::create_dir_all(&deciduous).unwrap();
        let path = deciduous.join("config.toml");

        let original = "# Deciduous Configuration\n\
                        # This file controls branch detection\n\
                        \n\
                        [branch]\n\
                        main_branches = [\"main\", \"master\"]\n\
                        auto_detect = true\n";
        std::fs::write(&path, original).unwrap();

        // save_remote resolves the directory itself, so drive the edit the way
        // it does rather than depending on the process's cwd.
        let mut doc = std::fs::read_to_string(&path)
            .unwrap()
            .parse::<toml_edit::DocumentMut>()
            .unwrap();
        doc["remote"].or_insert(toml_edit::table())["url"] =
            toml_edit::value("https://example.com/mcp");
        std::fs::write(&path, doc.to_string()).unwrap();

        let after = std::fs::read_to_string(&path).unwrap();
        assert!(
            after.contains("# Deciduous Configuration"),
            "comment dropped:\n{after}"
        );
        assert!(after.contains("# This file controls branch detection"));
        assert!(
            after.contains("main_branches = [\"main\", \"master\"]"),
            "array was reflowed:\n{after}"
        );
        assert!(after.contains("[remote]"));
        assert!(after.contains("https://example.com/mcp"));

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn a_config_with_no_remote_is_not_configured() {
        assert!(!Config::default().remote.is_configured());
    }
}