joy-core 0.15.5

Core library for Joy product management - Git-native, terminal-first
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
// Copyright (c) 2026 Joydev GmbH (joydev.com)
// SPDX-License-Identifier: MIT

use crate::fortune::Category;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Config {
    pub version: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sync: Option<SyncConfig>,
    #[serde(default)]
    pub output: OutputConfig,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ai: Option<AiConfig>,
    #[serde(default)]
    pub workflow: WorkflowConfig,
    #[serde(default)]
    pub modes: ModesConfig,
    #[serde(default = "default_auto_sync", rename = "auto-sync")]
    pub auto_sync: bool,
    /// Editor invoked when a Joy command needs free-form input (e.g.
    /// joy comment without TEXT). Takes precedence over $VISUAL /
    /// $EDITOR; the value is run via `sh -c`, so it can carry flags.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub editor: Option<String>,
}

fn default_auto_sync() -> bool {
    true
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WorkflowConfig {
    #[serde(rename = "auto-assign", default = "default_true")]
    pub auto_assign: bool,
    #[serde(rename = "auto-git", default)]
    pub auto_git: AutoGit,
}

impl Default for WorkflowConfig {
    fn default() -> Self {
        Self {
            auto_assign: true,
            auto_git: AutoGit::default(),
        }
    }
}

/// Controls automatic git operations after Joy writes versioned files.
/// Each level implies the previous: Push = Add + Commit + Push.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AutoGit {
    Off,
    #[default]
    Add,
    Commit,
    Push,
}

impl AutoGit {
    pub fn should_add(self) -> bool {
        matches!(self, Self::Add | Self::Commit | Self::Push)
    }

    pub fn should_commit(self) -> bool {
        matches!(self, Self::Commit | Self::Push)
    }

    pub fn should_push(self) -> bool {
        matches!(self, Self::Push)
    }
}

fn default_true() -> bool {
    true
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ModesConfig {
    #[serde(default)]
    pub default: InteractionLevel,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InteractionLevel {
    Autonomous,
    Supervised,
    #[default]
    Collaborative,
    Interactive,
    Pairing,
}

impl std::fmt::Display for InteractionLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Autonomous => write!(f, "autonomous"),
            Self::Supervised => write!(f, "supervised"),
            Self::Collaborative => write!(f, "collaborative"),
            Self::Interactive => write!(f, "interactive"),
            Self::Pairing => write!(f, "pairing"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SyncConfig {
    pub remote: String,
    pub auto: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OutputConfig {
    pub color: ColorMode,
    pub emoji: bool,
    #[serde(default)]
    pub short: bool,
    #[serde(default = "default_fortune")]
    pub fortune: bool,
    #[serde(
        rename = "fortune-category",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub fortune_category: Option<Category>,
}

fn default_fortune() -> bool {
    true
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ColorMode {
    Auto,
    Always,
    Never,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AiConfig {
    pub tool: String,
    pub command: String,
    pub model: String,
    pub max_cost_per_job: f64,
    pub currency: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            version: 1,
            sync: None,
            output: OutputConfig::default(),
            ai: None,
            workflow: WorkflowConfig::default(),
            modes: ModesConfig::default(),
            auto_sync: default_auto_sync(),
            editor: None,
        }
    }
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            color: ColorMode::Auto,
            emoji: false,
            short: true,
            fortune: true,
            fortune_category: None,
        }
    }
}

/// One-line description for a config (key, value) pair. Returned by
/// `joy config get --describe` so the CLI is the single source of truth
/// for the semantics of each setting. New keys/values should be added
/// here when introduced so the help surface stays complete.
pub fn describe_value(key: &str, value: &serde_json::Value) -> Option<String> {
    let s = value.as_str();
    let b = value.as_bool();
    let text = match (key, s, b) {
        ("modes.default", Some("autonomous"), _) => {
            "work independently, stop only at governance gates"
        }
        ("modes.default", Some("supervised"), _) => "confirm before irreversible actions",
        ("modes.default", Some("collaborative"), _) => {
            "propose approach, proceed after confirmation"
        }
        ("modes.default", Some("interactive"), _) => {
            "present options with rationale, wait for decision"
        }
        ("modes.default", Some("pairing"), _) => "step by step, question by question",

        ("workflow.auto-git", Some("off"), _) => "never stage, commit, or push automatically",
        ("workflow.auto-git", Some("add"), _) => "git add changed files after each write",
        ("workflow.auto-git", Some("commit"), _) => "add + commit after each write",
        ("workflow.auto-git", Some("push"), _) => "add + commit + push after each write",

        ("output.color", Some("auto"), _) => "color on TTY, plain when piped",
        ("output.color", Some("always"), _) => "force color even when output is piped",
        ("output.color", Some("never"), _) => "plain output, no ANSI escapes",

        ("workflow.auto-assign", _, Some(true)) => "assign yourself when running `joy start`",
        ("workflow.auto-assign", _, Some(false)) => "leave assignment unchanged on `joy start`",

        ("auto-sync", _, Some(true)) => "reassert hooks/instructions on every joy invocation",
        ("auto-sync", _, Some(false)) => "skip auto-sync of hooks/instructions",

        ("output.emoji", _, Some(true)) => "use emoji glyphs in styled output",
        ("output.emoji", _, Some(false)) => "no emoji in output",

        ("output.short", _, Some(true)) => "compact listings (single line per item)",
        ("output.short", _, Some(false)) => "verbose listings (multi-line per item)",

        ("output.fortune", _, Some(true)) => "show a short fortune after init and on idle",
        ("output.fortune", _, Some(false)) => "no fortune banners",

        _ => return None,
    };
    Some(text.to_string())
}

/// Flatten the nested config tree under `prefix` into a list of
/// `(dotted_key, leaf_value)` pairs. The prefix itself is included in
/// the emitted keys so callers can render them verbatim. Used by
/// `joy config get <prefix>.*`.
pub fn flatten_under(value: &serde_json::Value, prefix: &str) -> Vec<(String, serde_json::Value)> {
    let mut out = Vec::new();
    let start = if prefix.is_empty() {
        Some(value)
    } else {
        navigate_json(value, prefix)
    };
    if let Some(start) = start {
        walk(prefix, start, &mut out);
    }
    out.sort_by(|a, b| a.0.cmp(&b.0));
    out
}

fn walk(prefix: &str, value: &serde_json::Value, out: &mut Vec<(String, serde_json::Value)>) {
    match value {
        serde_json::Value::Object(map) => {
            for (k, v) in map {
                let next = if prefix.is_empty() {
                    k.clone()
                } else {
                    format!("{prefix}.{k}")
                };
                walk(&next, v, out);
            }
        }
        scalar => out.push((prefix.to_string(), scalar.clone())),
    }
}

/// Return a human-readable hint for a config key, listing allowed values when
/// the field is an enum or constrained type. Derived from the Config struct
/// rather than a hand-maintained map.
pub fn field_hint(key: &str) -> Option<String> {
    let defaults = serde_json::to_value(Config::default()).ok()?;
    // Try navigating with the original key; if not found (e.g. optional fields
    // omitted by skip_serializing_if), fall back to probing directly.
    let current = navigate_json(&defaults, key);

    // Probe for enum variants regardless of whether the field is in defaults
    let candidates = probe_string_field(key);
    if !candidates.is_empty() {
        return Some(format!("allowed values: {}", candidates.join(", ")));
    }

    if let Some(current) = current {
        return match current {
            serde_json::Value::Bool(_) => Some("expected: true or false".to_string()),
            serde_json::Value::Number(_) => Some("expected: a number".to_string()),
            serde_json::Value::String(_) => Some("expected: a string".to_string()),
            _ => None,
        };
    }

    None
}

fn navigate_json<'a>(value: &'a serde_json::Value, key: &str) -> Option<&'a serde_json::Value> {
    let mut current = value;
    for part in key.split('.') {
        // Try as-is first, then with hyphens/underscores swapped (YAML uses
        // hyphens, serde_json serializes Rust field names with underscores).
        current = current
            .get(part)
            .or_else(|| current.get(part.replace('-', "_")))
            .or_else(|| current.get(part.replace('_', "-")))?;
    }
    Some(current)
}

/// Try setting a config field to various string values to discover which ones
/// the schema accepts -- this reveals enum variants without hard-coding them.
/// Validates via YAML round-trip to correctly handle hyphen/underscore key
/// variants and optional fields.
fn probe_string_field(key: &str) -> Vec<String> {
    const PROBES: &[&str] = &[
        "auto",
        "always",
        "never",
        "none",
        "true",
        "false",
        "yes",
        "no",
        "on",
        "add",
        "commit",
        "push",
        "off",
        "list",
        "board",
        "calendar",
        "all",
        "tech",
        "science",
        "humor",
        "low",
        "medium",
        "high",
        "critical",
        "autonomous",
        "supervised",
        "collaborative",
        "interactive",
        "pairing",
    ];

    let mut accepted = Vec::new();
    for &candidate in PROBES {
        // Build a minimal YAML snippet with the candidate value and try
        // deserializing as Config. This uses the same path as load_config,
        // so hyphen/underscore handling matches real behavior.
        let yaml = build_yaml_for_key(key, candidate);
        let defaults_yaml = serde_yaml_ng::to_string(&Config::default()).unwrap_or_default();
        let Ok(mut base): Result<serde_json::Value, _> = serde_yaml_ng::from_str(&defaults_yaml)
        else {
            continue;
        };
        let Ok(overlay): Result<serde_json::Value, _> = serde_yaml_ng::from_str(&yaml) else {
            continue;
        };
        crate::store::deep_merge_value(&mut base, &overlay);
        if serde_json::from_value::<Config>(base).is_ok() {
            accepted.push(candidate.to_string());
        }
    }
    accepted
}

/// Build a nested YAML string from a dotted key and value.
/// e.g. "output.color" + "auto" -> "output:\n  color: auto\n"
fn build_yaml_for_key(key: &str, value: &str) -> String {
    let parts: Vec<&str> = key.split('.').collect();
    let mut yaml = String::new();
    for (i, part) in parts.iter().enumerate() {
        for _ in 0..i {
            yaml.push_str("  ");
        }
        if i == parts.len() - 1 {
            yaml.push_str(&format!("{part}: {value}\n"));
        } else {
            yaml.push_str(&format!("{part}:\n"));
        }
    }
    yaml
}

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

    #[test]
    fn default_config_roundtrip() {
        let config = Config::default();
        let yaml = serde_yaml_ng::to_string(&config).unwrap();
        let parsed: Config = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(config, parsed);
    }

    #[test]
    fn default_config_snapshot() {
        let config = Config::default();
        let yaml = serde_yaml_ng::to_string(&config).unwrap();
        insta::assert_snapshot!(yaml);
    }

    #[test]
    fn modes_config_get_default() {
        let config = Config::default();
        assert_eq!(config.modes.default, InteractionLevel::Collaborative);
    }

    #[test]
    fn modes_config_set_default() {
        let yaml = "modes:\n  default: pairing\n";
        let mut base = serde_json::to_value(Config::default()).unwrap();
        let overlay: serde_json::Value = serde_yaml_ng::from_str(yaml).unwrap();
        crate::store::deep_merge_value(&mut base, &overlay);
        let config: Config = serde_json::from_value(base).unwrap();
        assert_eq!(config.modes.default, InteractionLevel::Pairing);
    }

    #[test]
    fn old_agents_key_does_not_deserialize_to_modes() {
        let yaml = "agents:\n  default:\n    mode: pairing\n";
        let mut base = serde_json::to_value(Config::default()).unwrap();
        let overlay: serde_json::Value = serde_yaml_ng::from_str(yaml).unwrap();
        crate::store::deep_merge_value(&mut base, &overlay);
        let config: Config = serde_json::from_value(base).unwrap();
        // modes.default should still be the default, not pairing
        assert_eq!(config.modes.default, InteractionLevel::Collaborative);
    }

    #[test]
    fn describe_value_modes_default() {
        let v = serde_json::Value::String("collaborative".to_string());
        let d = describe_value("modes.default", &v).expect("known variant");
        assert!(d.contains("propose"));
        let unknown = serde_json::Value::String("zzz".to_string());
        assert!(describe_value("modes.default", &unknown).is_none());
    }

    #[test]
    fn flatten_under_modes_returns_default() {
        let cfg = serde_json::to_value(Config::default()).unwrap();
        let leaves = flatten_under(&cfg, "modes");
        let keys: Vec<&str> = leaves.iter().map(|(k, _)| k.as_str()).collect();
        assert!(keys.contains(&"modes.default"));
    }

    #[test]
    fn flatten_under_output_lists_scalars_only() {
        let cfg = serde_json::to_value(Config::default()).unwrap();
        let leaves = flatten_under(&cfg, "output");
        assert!(leaves.iter().all(|(_, v)| !v.is_object()));
        assert!(leaves.iter().any(|(k, _)| k == "output.color"));
    }

    #[test]
    fn field_hint_modes_default() {
        let hint = field_hint("modes.default");
        assert!(hint.is_some());
        let values = hint.unwrap();
        assert!(values.contains("collaborative"));
        assert!(values.contains("pairing"));
    }

    #[test]
    fn old_agents_key_has_no_effect_on_modes() {
        // Even if agents key is present in YAML, it should not affect modes
        let yaml = "agents:\n  default:\n    mode: pairing\nmodes:\n  default: interactive\n";
        let mut base = serde_json::to_value(Config::default()).unwrap();
        let overlay: serde_json::Value = serde_yaml_ng::from_str(yaml).unwrap();
        crate::store::deep_merge_value(&mut base, &overlay);
        let config: Config = serde_json::from_value(base).unwrap();
        // modes.default takes the explicit value, agents is ignored
        assert_eq!(config.modes.default, InteractionLevel::Interactive);
    }
}